-
Notifications
You must be signed in to change notification settings - Fork 104
/
trace.go
211 lines (179 loc) · 4.71 KB
/
trace.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
207
208
209
210
211
package main
import (
"context"
"errors"
"fmt"
"log"
"os"
"os/signal"
"syscall"
"github.com/cilium/ebpf"
"github.com/cilium/ebpf/btf"
"github.com/cilium/ebpf/link"
"github.com/cilium/ebpf/rlimit"
"golang.org/x/sync/errgroup"
"golang.org/x/sys/unix"
)
var errNotFound = errors.New("not found")
func trace(spec *ebpf.CollectionSpec, prog *ebpf.Program, tracingName string, opts ebpf.CollectionOptions) (link.Link, error) {
entryFn, progName, tag, err := getBpfProgInfo(prog)
if err != nil {
if errors.Is(err, errNotFound) {
log.Printf("Skip tracing bpf prog %s because cannot find its entry function name", prog)
return nil, nil
}
return nil, fmt.Errorf("failed to get entry function name: %w", err)
}
tSpec := spec.Copy()
tSpec.Programs[tracingName].AttachTarget = prog
tSpec.Programs[tracingName].AttachTo = entryFn
coll, err := ebpf.NewCollectionWithOptions(tSpec, opts)
if err != nil {
return nil, fmt.Errorf("failed to create collection: %w", err)
}
defer coll.Close()
tracing, err := link.AttachTracing(link.TracingOptions{
Program: coll.Programs[tracingName],
})
if err != nil {
return nil, fmt.Errorf("failed to attach tracing: %w", err)
}
log.Printf("Tracing %s XDP prog %s (entry: %s, tag: %s)", tracingName, progName, entryFn, tag)
return tracing, nil
}
func listBpfProgs(typ ebpf.ProgramType) ([]*ebpf.Program, error) {
var (
id ebpf.ProgramID
err error
)
var progs []*ebpf.Program
for id, err = ebpf.ProgramGetNextID(id); err == nil; id, err = ebpf.ProgramGetNextID(id) {
prog, err := ebpf.NewProgramFromID(id)
if err != nil {
return nil, err
}
if prog.Type() == typ {
progs = append(progs, prog)
} else {
_ = prog.Close()
}
}
if !errors.Is(err, unix.ENOENT) {
return nil, err
}
return progs, nil
}
func getBpfProgInfo(prog *ebpf.Program) (entryFuncName, progName, tag string, err error) {
info, err := prog.Info()
if err != nil {
err = fmt.Errorf("failed to get program info: %w", err)
return
}
_, ok := info.BTFID()
if !ok {
// FENTRY/FEXIT program can only be attached to another program
// annotated with BTF. So if the BTF ID is not found, it means
// the program is not annotated with BTF.
err = errNotFound
return
}
insns, err := info.Instructions()
if err != nil {
err = fmt.Errorf("failed to get program instructions: %w", err)
return
}
for _, insn := range insns {
sym := insn.Symbol()
if sym != "" {
return sym, info.Name, info.Tag, nil
}
}
err = errNotFound
return
}
//go:generate go run github.com/cilium/ebpf/cmd/bpf2go xdp_trace xdp_trace.bpf.c
func main() {
if err := unix.Setrlimit(unix.RLIMIT_NOFILE, &unix.Rlimit{
Cur: 8192,
Max: 8192,
}); err != nil {
log.Fatalf("failed to set temporary rlimit: %s", err)
}
if err := rlimit.RemoveMemlock(); err != nil {
log.Fatalf("Failed to set temporary rlimit: %s", err)
}
btfSpec, err := btf.LoadKernelSpec()
if err != nil {
log.Fatalf("Failed to load BTF spec: %s", err)
}
bpfSpec, err := loadXdp_trace()
if err != nil {
log.Fatalf("Failed to load BPF spec: %s", err)
}
var opts ebpf.CollectionOptions
opts.Programs.KernelTypes = btfSpec
opts.Programs.LogLevel = ebpf.LogLevelInstruction
bpfSpecFentryXdp := bpfSpec.Copy()
bpfSpecFentryXdp.Programs = map[string]*ebpf.ProgramSpec{
"fentry_xdp": bpfSpecFentryXdp.Programs["fentry_xdp"],
}
bpfSpecFexitXdp := bpfSpec.Copy()
bpfSpecFexitXdp.Programs = map[string]*ebpf.ProgramSpec{
"fexit_xdp": bpfSpecFexitXdp.Programs["fexit_xdp"],
}
progs, err := listBpfProgs(ebpf.XDP)
if err != nil {
log.Fatalf("failed to list XDP progs: %v", err)
}
var errg errgroup.Group
var traces []link.Link
for _, prog := range progs {
prog := prog
errg.Go(func() error {
tracing, err := trace(bpfSpecFentryXdp, prog, "fentry_xdp", opts)
if err != nil {
return err
}
traces = append(traces, tracing)
tracing, err = trace(bpfSpecFexitXdp, prog, "fexit_xdp", opts)
if err != nil {
return err
}
traces = append(traces, tracing)
return nil
})
}
if err := errg.Wait(); err != nil {
log.Fatalf("failed to trace XDP progs: %v", err)
}
ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM)
defer stop()
go func() {
tracePipe, err := os.Open("/sys/kernel/debug/tracing/trace_pipe")
if err != nil {
log.Fatalf("failed to open trace_pipe: %v", err)
}
defer tracePipe.Close()
buf := make([]byte, 4096)
for {
n, err := tracePipe.Read(buf)
if err != nil {
log.Fatalf("failed to read from trace_pipe: %v", err)
}
if n > 0 {
fmt.Print(string(buf[:n]))
}
}
}()
for {
select {
case <-ctx.Done():
log.Println("Shutting down...")
for _, tracing := range traces {
tracing.Close()
}
return
default:
}
}
}