-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathprogram_tracepoint.go
78 lines (62 loc) · 2.09 KB
/
program_tracepoint.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
package gobpfld
import (
"fmt"
"github.com/dylandreimerink/gobpfld/bpfsys"
"github.com/dylandreimerink/gobpfld/bpftypes"
"github.com/dylandreimerink/gobpfld/perf"
)
var _ BPFProgram = (*ProgramTracepoint)(nil)
type ProgramTracepoint struct {
AbstractBPFProgram
// DefaultCategory is the tracepoint category used if no category is specified during attaching.
// It can be set when loading from ELF file.
DefaultCategory string
// DefaultName is the tracepoint name used if no name is specified during attaching.
// It can be set when loading from ELF file.
DefaultName string
attachedEvent *perf.Event
}
type ProgTPLoadOpts struct {
VerifierLogLevel bpftypes.BPFLogLevel
VerifierLogSize int
}
func (p *ProgramTracepoint) Load(opts ProgTPLoadOpts) (log string, err error) {
return p.load(bpfsys.BPFAttrProgramLoad{
LogLevel: opts.VerifierLogLevel,
LogSize: uint32(opts.VerifierLogSize),
})
}
// Unpin captures the file descriptor of the program at the given 'relativePath' from the kernel.
// If 'deletePin' is true the bpf FS pin will be removed after successfully loading the program, thus transferring
// ownership of the program in a scenario where the program is not shared between multiple userspace programs.
// Otherwise the pin will keep existing which will cause the map to not be deleted when this program exits.
func (p *ProgramTracepoint) Unpin(relativePath string, deletePin bool) error {
return p.unpin(relativePath, deletePin)
}
type ProgTPAttachOpts struct {
Category string
Name string
}
func (p *ProgramTracepoint) Attach(opts ProgTPAttachOpts) error {
category := p.DefaultCategory
if opts.Category != "" {
category = opts.Category
}
name := p.DefaultName
if opts.Category != "" {
name = opts.Name
}
var err error
p.attachedEvent, err = perf.OpenTracepointEvent(category, name)
if err != nil {
return fmt.Errorf("open tracepoint: %w", err)
}
err = p.attachedEvent.AttachBPFProgram(p.fd)
if err != nil {
return fmt.Errorf("attach program: %w", err)
}
return nil
}
func (p *ProgramTracepoint) Detach() error {
return p.attachedEvent.DetachBPFProgram()
}