@@ -6,91 +6,95 @@ import (
6
6
"github.com/iovisor/gobpf/bcc"
7
7
)
8
8
9
- // attach attaches functions to tracing points in provided module
10
- func attach (module * bcc.Module , kprobes , kretprobes , tracepoints , rawTracepoints map [string ]string ) error {
11
- if err := attachKprobes (module , kprobes ); err != nil {
12
- return fmt .Errorf ("failed to attach kprobes: %s" , err )
13
- }
9
+ // attacher attaches some sort of tracepoints or probes
10
+ type attacher func (* bcc.Module , map [string ]string ) (map [string ]uint64 , error )
14
11
15
- if err := attachKretprobes ( module , kretprobes ); err != nil {
16
- return fmt . Errorf ( "failed to attach kretprobes: %s" , err )
17
- }
12
+ // mergeTags runs attacher and merges produced tags
13
+ func mergedTags ( dst map [ string ] uint64 , attach attacher , module * bcc. Module , attachments map [ string ] string ) error {
14
+ src , err := attach ( module , attachments )
18
15
19
- if err := attachTracepoints ( module , tracepoints ); err != nil {
20
- return fmt . Errorf ( "failed to attach tracepoints: %s" , err )
16
+ if err != nil {
17
+ return err
21
18
}
22
19
23
- if err := attachRawTracepoints ( module , rawTracepoints ); err != nil {
24
- return fmt . Errorf ( "failed to attach raw tracepoints: %s" , err )
20
+ for name , tag := range src {
21
+ dst [ name ] = tag
25
22
}
26
23
27
24
return nil
28
25
}
29
26
30
- // attachKprobes attaches functions to their kprobles in provided module
31
- func attachKprobes (module * bcc.Module , kprobes map [string ]string ) error {
32
- for kprobeName , targetName := range kprobes {
33
- target , err := module .LoadKprobe (targetName )
34
- if err != nil {
35
- return fmt .Errorf ("failed to load target %q: %s" , targetName , err )
36
- }
27
+ // attach attaches functions to tracing points in provided module
28
+ func attach (module * bcc.Module , kprobes , kretprobes , tracepoints , rawTracepoints map [string ]string ) (map [string ]uint64 , error ) {
29
+ tags := map [string ]uint64 {}
37
30
38
- err = module .AttachKprobe (kprobeName , target )
39
- if err != nil {
40
- return fmt .Errorf ("failed to attach kprobe %q to %q: %s" , kprobeName , targetName , err )
41
- }
31
+ if err := mergedTags (tags , attachKprobes , module , kprobes ); err != nil {
32
+ return nil , fmt .Errorf ("failed to attach kprobes: %s" , err )
42
33
}
43
34
44
- return nil
45
- }
35
+ if err := mergedTags (tags , attachKretprobes , module , kretprobes ); err != nil {
36
+ return nil , fmt .Errorf ("failed to attach kretprobes: %s" , err )
37
+ }
46
38
47
- // attachKretprobes attaches functions to their kretprobles in provided module
48
- func attachKretprobes (module * bcc.Module , kretprobes map [string ]string ) error {
49
- for kretprobeName , targetName := range kretprobes {
50
- target , err := module .LoadKprobe (targetName )
51
- if err != nil {
52
- return fmt .Errorf ("failed to load target %q: %s" , targetName , err )
53
- }
39
+ if err := mergedTags (tags , attachTracepoints , module , tracepoints ); err != nil {
40
+ return nil , fmt .Errorf ("failed to attach tracepoints: %s" , err )
41
+ }
54
42
55
- err = module .AttachKretprobe (kretprobeName , target )
56
- if err != nil {
57
- return fmt .Errorf ("failed to attach kretprobe %q to %q: %s" , kretprobeName , targetName , err )
58
- }
43
+ if err := mergedTags (tags , attachRawTracepoints , module , rawTracepoints ); err != nil {
44
+ return nil , fmt .Errorf ("failed to attach raw tracepoints: %s" , err )
59
45
}
60
46
61
- return nil
47
+ return tags , nil
62
48
}
63
49
64
- // attachTracepoints attaches functions to their tracepoints in provided module
65
- func attachTracepoints (module * bcc.Module , tracepoints map [string ]string ) error {
66
- for tracepointName , targetName := range tracepoints {
67
- target , err := module .LoadTracepoint (targetName )
68
- if err != nil {
69
- return fmt .Errorf ("failed to load target %q: %s" , targetName , err )
70
- }
50
+ // probeLoader attaches some sort of probe
51
+ type probeLoader func (string ) (int , error )
71
52
72
- err = module .AttachTracepoint (tracepointName , target )
53
+ // probeAttacher attaches loaded some sort of probe to some sort of tracepoint
54
+ type probeAttacher func (string , int ) error
55
+
56
+ // attachSomething attaches some kind of probes and returns program tags
57
+ func attachSomething (module * bcc.Module , loader probeLoader , attacher probeAttacher , probes map [string ]string ) (map [string ]uint64 , error ) {
58
+ tags := map [string ]uint64 {}
59
+
60
+ for probe , targetName := range probes {
61
+ target , err := loader (targetName )
73
62
if err != nil {
74
- return fmt .Errorf ("failed to attach tracepoint %q to %q : %s" , tracepointName , targetName , err )
63
+ return nil , fmt .Errorf ("failed to load probe %q: %s" , targetName , err )
75
64
}
76
- }
77
-
78
- return nil
79
- }
80
65
81
- // attachRawTracepoints attaches functions to their tracepoints in provided module
82
- func attachRawTracepoints (module * bcc.Module , tracepoints map [string ]string ) error {
83
- for tracepointName , targetName := range tracepoints {
84
- target , err := module .LoadRawTracepoint (targetName )
66
+ tag , err := module .GetProgramTag (target )
85
67
if err != nil {
86
- return fmt .Errorf ("failed to load target %q : %s" , targetName , err )
68
+ return nil , fmt .Errorf ("failed to get program tag for %q (fd=%d) : %s" , targetName , target , err )
87
69
}
88
70
89
- err = module .AttachRawTracepoint (tracepointName , target )
71
+ tags [targetName ] = tag
72
+
73
+ err = attacher (probe , target )
90
74
if err != nil {
91
- return fmt .Errorf ("failed to attach raw tracepoint %q to %q: %s" , tracepointName , targetName , err )
75
+ return nil , fmt .Errorf ("failed to attach probe %q to %q: %s" , probe , targetName , err )
92
76
}
93
77
}
94
78
95
- return nil
79
+ return tags , nil
80
+ }
81
+
82
+ // attachKprobes attaches functions to their kprobles in provided module
83
+ func attachKprobes (module * bcc.Module , kprobes map [string ]string ) (map [string ]uint64 , error ) {
84
+ return attachSomething (module , module .LoadKprobe , module .AttachKprobe , kprobes )
85
+ }
86
+
87
+ // attachKretprobes attaches functions to their kretprobles in provided module
88
+ func attachKretprobes (module * bcc.Module , kretprobes map [string ]string ) (map [string ]uint64 , error ) {
89
+ return attachSomething (module , module .LoadKprobe , module .AttachKretprobe , kretprobes )
90
+ }
91
+
92
+ // attachTracepoints attaches functions to their tracepoints in provided module
93
+ func attachTracepoints (module * bcc.Module , tracepoints map [string ]string ) (map [string ]uint64 , error ) {
94
+ return attachSomething (module , module .LoadTracepoint , module .AttachTracepoint , tracepoints )
95
+ }
96
+
97
+ // attachRawTracepoints attaches functions to their tracepoints in provided module
98
+ func attachRawTracepoints (module * bcc.Module , tracepoints map [string ]string ) (map [string ]uint64 , error ) {
99
+ return attachSomething (module , module .LoadRawTracepoint , module .AttachRawTracepoint , tracepoints )
96
100
}
0 commit comments