-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathproc.go
113 lines (92 loc) · 2.15 KB
/
proc.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
package usdt
import (
"bufio"
"errors"
"fmt"
"os"
"runtime"
"strconv"
"strings"
"syscall"
"golang.org/x/sys/unix"
)
// Info eventually needed to calculate semaphore offset (userspace).
type regionAddrInfo struct {
start, offset uint64
}
// Fetch all shared objects from /proc/<pid>/maps.
func sharedObjects(pid int) (map[string]*regionAddrInfo, error) {
objs := make(map[string]*regionAddrInfo)
m, err := os.Open(fmt.Sprintf("/proc/%d/maps", pid))
if err != nil {
return objs, err
}
defer m.Close()
s := bufio.NewScanner(m)
for s.Scan() {
ss := strings.Split(s.Text(), " ")
path := ss[len(ss)-1]
// Inode.
if ss[4] == "0" {
// Region not mapped from a file.
continue
}
objs[path] = nil
// Perms.
if string(ss[1][1]) != "w" {
// Skip non writable regions.
continue
}
regionAddr := strings.Split(ss[0], "-")
start, err := strconv.ParseUint(regionAddr[0], 16, 64)
if err != nil {
return objs, err
}
off, err := strconv.ParseUint(ss[2], 16, 64)
if err != nil {
return objs, err
}
objs[path] = ®ionAddrInfo{start, off}
}
return objs, nil
}
func updateSemaphore(note *sdtNote, pid int, inc bool) error {
runtime.LockOSThread()
defer runtime.UnlockOSThread()
if err := unix.PtraceAttach(pid); err != nil {
return fmt.Errorf("ptrace attach: %w", err)
}
defer func() { _ = unix.PtraceDetach(pid) }()
for {
_, err := syscall.Wait4(pid, nil, 0, nil)
if !errors.Is(err, syscall.EINTR) {
break
}
}
// Number of expected bytes for unsigned short.
b := 2
semb := make([]byte, b)
c, err := unix.PtracePeekData(pid, uintptr(note.semaphoreOffsetUser), semb)
if err != nil {
return fmt.Errorf("ptrace peek: %w", err)
}
if c != b {
return fmt.Errorf("ptrace peek: wrong number of bytes read: %d", c)
}
sem := note.bo.Uint16(semb)
// In normal cases, this should never underflow.
if inc {
sem += 1
} else {
sem -= 1
}
note.bo.PutUint16(semb, sem)
c, err = unix.PtracePokeData(pid, uintptr(note.semaphoreOffsetUser), semb)
if err != nil {
return fmt.Errorf("ptrace poke: %w", err)
}
if c != b {
return fmt.Errorf("ptrace poke: wrong number of bytes written: %d", c)
}
return nil
}