@@ -11,7 +11,6 @@ import (
11
11
"errors"
12
12
"fmt"
13
13
"path"
14
- "sync"
15
14
16
15
"github.com/cilium/ebpf"
17
16
"github.com/cilium/tetragon/pkg/api/ops"
@@ -48,10 +47,7 @@ const (
48
47
)
49
48
50
49
var (
51
- // Tracepoint information (genericTracepoint) is needed at load time
52
- // and at the time we process the perf event from bpf-side. We keep
53
- // this information on a table index by a (unique) tracepoint id.
54
- genericTracepointTable = tracepointTable {}
50
+ genericTracepointTable idtable.Table
55
51
56
52
tracepointLog logrus.FieldLogger
57
53
)
@@ -70,6 +66,8 @@ func init() {
70
66
71
67
// genericTracepoint is the internal representation of a tracepoint
72
68
type genericTracepoint struct {
69
+ tableId idtable.EntryID
70
+
73
71
Info * tracepoint.Tracepoint
74
72
args []genericTracepointArg
75
73
@@ -103,6 +101,10 @@ type genericTracepoint struct {
103
101
raw bool
104
102
}
105
103
104
+ func (tp * genericTracepoint ) SetID (id idtable.EntryID ) {
105
+ tp .tableId = id
106
+ }
107
+
106
108
// genericTracepointArg is the internal representation of an output value of a
107
109
// generic tracepoint.
108
110
type genericTracepointArg struct {
@@ -138,30 +140,16 @@ type genericTracepointArg struct {
138
140
btf [tracingapi .MaxBTFArgDepth ]tracingapi.ConfigBTFArg
139
141
}
140
142
141
- // tracepointTable is, for now, an array.
142
- type tracepointTable struct {
143
- mu sync.Mutex
144
- arr []* genericTracepoint
145
- }
146
-
147
- // addTracepoint adds a tracepoint to the table, and sets its .tableIdx field
148
- // to be the index to retrieve it from the table.
149
- func (t * tracepointTable ) addTracepoint (tp * genericTracepoint ) {
150
- t .mu .Lock ()
151
- defer t .mu .Unlock ()
152
- idx := len (t .arr )
153
- t .arr = append (t .arr , tp )
154
- tp .tableIdx = idx
155
- }
156
-
157
- // getTracepoint retrieves a tracepoint from the table using its id
158
- func (t * tracepointTable ) getTracepoint (idx int ) (* genericTracepoint , error ) {
159
- t .mu .Lock ()
160
- defer t .mu .Unlock ()
161
- if idx < len (t .arr ) {
162
- return t .arr [idx ], nil
143
+ func genericTracepointTableGet (id idtable.EntryID ) (* genericTracepoint , error ) {
144
+ entry , err := genericTracepointTable .GetEntry (id )
145
+ if err != nil {
146
+ return nil , fmt .Errorf ("getting entry from genericTracepointTable failed with: %w" , err )
163
147
}
164
- return nil , fmt .Errorf ("tracepoint table: invalid id:%d (len=%d)" , idx , len (t .arr ))
148
+ val , ok := entry .(* genericTracepoint )
149
+ if ! ok {
150
+ return nil , fmt .Errorf ("getting entry from genericTracepointTable failed with: got invalid type: %T (%v)" , entry , entry )
151
+ }
152
+ return val , nil
165
153
}
166
154
167
155
func (out * genericTracepointArg ) String () string {
@@ -408,6 +396,7 @@ func createGenericTracepoint(
408
396
}
409
397
410
398
ret := & genericTracepoint {
399
+ tableId : idtable .UninitializedEntryID ,
411
400
Info : & tp ,
412
401
Spec : conf ,
413
402
args : tpArgs ,
@@ -419,7 +408,7 @@ func createGenericTracepoint(
419
408
raw : conf .Raw ,
420
409
}
421
410
422
- genericTracepointTable .addTracepoint (ret )
411
+ genericTracepointTable .AddEntry (ret )
423
412
ret .pinPathPrefix = sensors .PathJoin (sensorName , fmt .Sprintf ("gtp-%d" , ret .tableIdx ))
424
413
return ret , nil
425
414
}
@@ -563,7 +552,7 @@ func createGenericTracepointSensor(
563
552
564
553
has .fdInstall = selectorsHaveFDInstall (tp .Spec .Selectors )
565
554
566
- prog0 .LoaderData = tp .tableIdx
555
+ prog0 .LoaderData = tp .tableId
567
556
progs = append (progs , prog0 )
568
557
569
558
fdinstall := program .MapBuilderSensor ("fdinstall_map" , prog0 )
@@ -664,6 +653,19 @@ func createGenericTracepointSensor(
664
653
665
654
ret .Progs = progs
666
655
ret .Maps = maps
656
+
657
+ ret .DestroyHook = func () error {
658
+ var errs error
659
+
660
+ for _ , tp := range tracepoints {
661
+ _ , err := genericTracepointTable .RemoveEntry (tp .tableId )
662
+ if err != nil {
663
+ errs = errors .Join (errs , err )
664
+ }
665
+ }
666
+ return errs
667
+ }
668
+
667
669
return ret , nil
668
670
}
669
671
@@ -769,12 +771,12 @@ func LoadGenericTracepointSensor(bpfDir string, load *program.Program, maps []*p
769
771
770
772
tracepointLog = logger .GetLogger ()
771
773
772
- tpIdx , ok := load .LoaderData .(int )
774
+ id , ok := load .LoaderData .(idtable. EntryID )
773
775
if ! ok {
774
776
return fmt .Errorf ("loaderData for genericTracepoint %s is %T (%v) (not an int)" , load .Name , load .LoaderData , load .LoaderData )
775
777
}
776
778
777
- tp , err := genericTracepointTable . getTracepoint ( tpIdx )
779
+ tp , err := genericTracepointTableGet ( id )
778
780
if err != nil {
779
781
return fmt .Errorf ("could not find generic tracepoint information for %s: %w" , load .Attach , err )
780
782
}
@@ -820,7 +822,7 @@ func handleGenericTracepoint(r *bytes.Reader) ([]observer.Event, error) {
820
822
Event : "UNKNOWN" ,
821
823
}
822
824
823
- tp , err := genericTracepointTable . getTracepoint ( int (m .FuncId ))
825
+ tp , err := genericTracepointTableGet (idtable. EntryID { ID : int (m .FuncId )} )
824
826
if err != nil {
825
827
logger .GetLogger ().WithField ("id" , m .FuncId ).WithError (err ).Warnf ("genericTracepoint info not found" )
826
828
return []observer.Event {unix }, nil
0 commit comments