-
Notifications
You must be signed in to change notification settings - Fork 10
/
kprobe.go
206 lines (179 loc) · 6.16 KB
/
kprobe.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
package manager
import (
"errors"
"fmt"
"os"
"strconv"
"strings"
"github.com/DataDog/ebpf-manager/tracefs"
)
const (
UnknownProbeType = ""
ProbeType = "p"
RetProbeType = "r"
)
type KprobeAttachMethod = AttachMethod
const (
AttachKprobeMethodNotSet = AttachMethodNotSet
AttachKprobeWithPerfEventOpen = AttachWithPerfEventOpen
AttachKprobeWithKprobeEvents = AttachWithProbeEvents
)
// GetKprobeType - Identifies the probe type of the provided KProbe section
func (p *Probe) GetKprobeType() string {
if len(p.kprobeType) == 0 {
if strings.HasPrefix(p.programSpec.SectionName, "kretprobe/") {
p.kprobeType = RetProbeType
} else if strings.HasPrefix(p.programSpec.SectionName, "kprobe/") {
p.kprobeType = ProbeType
} else {
p.kprobeType = UnknownProbeType
}
}
return p.kprobeType
}
// attachKprobe - Attaches the probe to its kprobe
func (p *Probe) attachKprobe() error {
var err error
if len(p.HookFuncName) == 0 {
return errors.New("HookFuncName, MatchFuncName or SyscallFuncName is required")
}
if p.GetKprobeType() == UnknownProbeType {
// this might actually be a UProbe
return p.attachUprobe()
}
isKRetProbe := p.GetKprobeType() == RetProbeType
// currently the perf event open ABI doesn't allow to specify the max active parameter
if p.KProbeMaxActive > 0 && isKRetProbe {
if err = p.attachWithKprobeEvents(); err != nil {
if p.perfEventFD, err = perfEventOpenPMU(p.HookFuncName, 0, -1, "kprobe", isKRetProbe, 0); err != nil {
return err
}
}
} else if p.KprobeAttachMethod == AttachKprobeWithPerfEventOpen {
if p.perfEventFD, err = perfEventOpenPMU(p.HookFuncName, 0, -1, "kprobe", isKRetProbe, 0); err != nil {
if err = p.attachWithKprobeEvents(); err != nil {
return err
}
}
} else if p.KprobeAttachMethod == AttachKprobeWithKprobeEvents {
if err = p.attachWithKprobeEvents(); err != nil {
if p.perfEventFD, err = perfEventOpenPMU(p.HookFuncName, 0, -1, "kprobe", isKRetProbe, 0); err != nil {
return err
}
}
} else {
return fmt.Errorf("invalid kprobe attach method: %d", p.KprobeAttachMethod)
}
// enable perf event
if err = ioctlPerfEventSetBPF(p.perfEventFD, p.program.FD()); err != nil {
return fmt.Errorf("couldn't set perf event bpf %s: %w", p.ProbeIdentificationPair, err)
}
if err = ioctlPerfEventEnable(p.perfEventFD); err != nil {
return fmt.Errorf("couldn't enable perf event %s: %w", p.ProbeIdentificationPair, err)
}
return nil
}
// attachWithKprobeEvents attaches the kprobe using the kprobes_events ABI
func (p *Probe) attachWithKprobeEvents() error {
if p.kprobeHookPointNotExist {
return ErrKProbeHookPointNotExist
}
// Prepare kprobe_events line parameters
var maxActiveStr string
if p.GetKprobeType() == RetProbeType {
if p.KProbeMaxActive > 0 {
maxActiveStr = fmt.Sprintf("%d", p.KProbeMaxActive)
}
}
// Fallback to debugfs, write kprobe_events line to register kprobe
var kprobeID int
kprobeID, err := registerKprobeEvent(p.GetKprobeType(), p.HookFuncName, p.UID, maxActiveStr, p.attachPID)
if errors.Is(err, ErrKprobeIDNotExist) {
// The probe might have been loaded under a kernel generated event name. Clean up just in case.
_ = unregisterTraceFSEvent("kprobe_events", getKernelGeneratedEventName(p.GetKprobeType(), p.HookFuncName))
// fallback without KProbeMaxActive
kprobeID, err = registerKprobeEvent(p.GetKprobeType(), p.HookFuncName, p.UID, "", p.attachPID)
}
if err != nil {
if errors.Is(err, os.ErrNotExist) {
p.kprobeHookPointNotExist = true
}
return fmt.Errorf("couldn't enable kprobe %s: %w", p.ProbeIdentificationPair, err)
}
// create perf event fd
p.perfEventFD, err = perfEventOpenTracingEvent(kprobeID, -1)
if err != nil {
return fmt.Errorf("couldn't open perf event fd for %s: %w", p.ProbeIdentificationPair, err)
}
p.attachedWithDebugFS = true
return nil
}
// detachKprobe - Detaches the probe from its kprobe
func (p *Probe) detachKprobe() error {
// Prepare kprobe_events line parameters
if p.GetKprobeType() == UnknownProbeType {
// this might be a `uprobe`
return p.detachUprobe()
}
if !p.attachedWithDebugFS {
// nothing to do
return nil
}
// Write kprobe_events line to remove hook point
return unregisterKprobeEvent(p.GetKprobeType(), p.HookFuncName, p.UID, p.attachPID)
}
func (p *Probe) pauseKprobe() error {
if err := ioctlPerfEventDisable(p.perfEventFD); err != nil {
return fmt.Errorf("pause kprobe: %w", err)
}
return nil
}
func (p *Probe) resumeKprobe() error {
if err := ioctlPerfEventEnable(p.perfEventFD); err != nil {
return fmt.Errorf("resume kprobe: %w", err)
}
return nil
}
// registerKprobeEvent - Writes a new kprobe in kprobe_events with the provided parameters. Call DisableKprobeEvent
// to remove the kprobe.
func registerKprobeEvent(probeType, funcName, UID, maxActiveStr string, kprobeAttachPID int) (int, error) {
// Generate event name
eventName, err := generateEventName(probeType, funcName, UID, kprobeAttachPID)
if err != nil {
return -1, err
}
// Write line to kprobe_events
f, err := tracefs.OpenFile("kprobe_events", os.O_APPEND|os.O_WRONLY, 0666)
if err != nil {
return -1, fmt.Errorf("cannot open kprobe_events: %w", err)
}
defer f.Close()
cmd := fmt.Sprintf("%s%s:%s %s\n", probeType, maxActiveStr, eventName, funcName)
if _, err = f.WriteString(cmd); err != nil && !os.IsExist(err) {
return -1, fmt.Errorf("cannot write %q to kprobe_events: %w", cmd, err)
}
// Retrieve kprobe ID
kprobeIDFile := fmt.Sprintf("events/kprobes/%s/id", eventName)
kprobeIDBytes, err := tracefs.ReadFile(kprobeIDFile)
if err != nil {
if os.IsNotExist(err) {
return -1, ErrKprobeIDNotExist
}
return -1, fmt.Errorf("cannot read kprobe id: %w", err)
}
id := strings.TrimSpace(string(kprobeIDBytes))
kprobeID, err := strconv.Atoi(id)
if err != nil {
return -1, fmt.Errorf("invalid kprobe id: '%s': %w", id, err)
}
return kprobeID, nil
}
// unregisterKprobeEvent - Removes a kprobe from kprobe_events
func unregisterKprobeEvent(probeType, funcName, UID string, kprobeAttachPID int) error {
// Generate event name
eventName, err := generateEventName(probeType, funcName, UID, kprobeAttachPID)
if err != nil {
return err
}
return unregisterTraceFSEvent("kprobe_events", eventName)
}