-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
86 lines (70 loc) · 1.47 KB
/
main.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
package main
//go:generate go run gen/syscall_gen.go
import (
"flag"
"fmt"
"os"
"syscall"
"golang.org/x/sys/unix"
)
var pid int
func main() {
flag.IntVar(&pid, "p", 0, "PID of the process to trace")
flag.Parse()
if pid == 0 {
fmt.Println("Provide a valid process ID with -p")
os.Exit(1)
}
err := unix.PtraceAttach(pid)
if err != nil {
panic(err)
}
fmt.Printf("Attached to process %d...\n", pid)
// PtraceAttach sends a SIGSTOP to the child; ensure it has properly
// stopped
s := new(unix.WaitStatus)
unix.Wait4(pid, s, 0, new(unix.Rusage))
err = unix.PtraceSetOptions(pid, syscall.PTRACE_O_TRACESYSGOOD)
if err != nil {
panic(err)
}
for {
regs := new(unix.PtraceRegs)
exitCode := waitSyscall(pid)
if exitCode != 0 {
fmt.Println("Process exited with", exitCode)
os.Exit(0)
}
err = unix.PtraceGetRegs(pid, regs)
if err != nil {
panic(err)
}
fmt.Printf("%s = ", Syscalls[regs.Orig_rax])
exitCode = waitSyscall(pid)
if exitCode != 0 {
fmt.Println("Process exited with", exitCode)
os.Exit(0)
}
err = unix.PtraceGetRegs(pid, regs)
if err != nil {
panic(err)
}
fmt.Printf("%d\n", regs.Rax)
}
}
func waitSyscall(pid int) int {
s := new(unix.WaitStatus)
for {
err := unix.PtraceSyscall(pid, 0)
if err != nil {
panic(err)
}
unix.Wait4(pid, s, 0, new(unix.Rusage))
if s.Stopped() && (s.StopSignal()&0x80 > 0) {
return 0
} else if s.Exited() {
fmt.Println("process exited")
return 1
}
}
}