-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pcontrol_linux_amd64.go
54 lines (43 loc) · 1.4 KB
/
pcontrol_linux_amd64.go
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 && amd64
// +build linux,amd64
package pcontrol
import (
"encoding/binary"
"gitlab.com/tozd/go/errors"
"golang.org/x/sys/unix"
)
var nativeEndian = binary.LittleEndian //nolint:gochecknoglobals
// Call a syscall and a breakpoint. 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{0x0F, 0x05, 0xCC} //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")
}
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.Rdi = arg0
newRegs.Rsi = arg1
newRegs.Rdx = arg2
newRegs.R10 = arg3
newRegs.R8 = arg4
newRegs.R9 = arg5
newRegs.Rax = uint64(call)
return newRegs
}
func getSyscallResultReg(regs *processRegs) uint64 {
return regs.Rax
}
func getProcessPC(regs *processRegs) uint64 {
return (*unix.PtraceRegs)(regs).PC()
}