-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpcontrol_linux_arm64.go
More file actions
54 lines (43 loc) · 1.51 KB
/
Copy pathpcontrol_linux_arm64.go
File metadata and controls
54 lines (43 loc) · 1.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
//go:build linux && arm64
// +build linux,arm64
package pcontrol
import (
"encoding/binary"
"gitlab.com/tozd/go/errors"
"golang.org/x/sys/unix"
)
var nativeEndian = binary.LittleEndian //nolint:gochecknoglobals
// Call a syscall (svc #0) and a breakpoint (brk #0). We do not use ptrace single step but ptrace cont
// until a breakpoint so that it is easier to allow signal handlers in process to run.
var syscallInstruction = [...]byte{0x01, 0x00, 0x00, 0xD4, 0x00, 0x00, 0x20, 0xD4} //nolint:gochecknoglobals
type processRegs unix.PtraceRegs
func getProcessRegs(pid int) (processRegs, errors.E) {
var regs unix.PtraceRegs
err := unix.PtraceGetRegs(pid, ®s)
if err != nil {
return processRegs{}, errors.WithMessage(err, "ptrace get regs") //nolint:exhaustruct
}
return processRegs(regs), nil
}
func setProcessRegs(pid int, regs *processRegs) errors.E {
err := unix.PtraceSetRegs(pid, (*unix.PtraceRegs)(regs))
return errors.WithMessage(err, "ptrace set regs")
}
func newSyscallRegs(originalRegs *processRegs, ip uint64, call int, arg0, arg1, arg2, arg3, arg4, arg5 uint64) processRegs {
newRegs := *originalRegs
(*unix.PtraceRegs)(&newRegs).SetPC(ip)
newRegs.Regs[0] = arg0
newRegs.Regs[1] = arg1
newRegs.Regs[2] = arg2
newRegs.Regs[3] = arg3
newRegs.Regs[4] = arg4
newRegs.Regs[5] = arg5
newRegs.Regs[8] = uint64(call) //nolint:gosec
return newRegs
}
func getSyscallResultReg(regs *processRegs) uint64 {
return regs.Regs[0]
}
func getProcessPC(regs *processRegs) uint64 {
return (*unix.PtraceRegs)(regs).PC()
}