github.com/inspektor-gadget/inspektor-gadget@v0.28.1/pkg/operators/ebpf/vars.go (about) 1 // Copyright 2024 The Inspektor Gadget authors 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package ebpfoperator 16 17 import ( 18 "fmt" 19 "reflect" 20 21 "github.com/cilium/ebpf/btf" 22 23 "github.com/inspektor-gadget/inspektor-gadget/pkg/btfhelpers" 24 ) 25 26 type ebpfVar struct { 27 name string 28 refType reflect.Type 29 tags []string 30 } 31 32 func (i *ebpfInstance) populateVar(t btf.Type, varName string) error { 33 btfVar, ok := t.(*btf.Var) 34 if !ok { 35 return fmt.Errorf("%q not of type *btf.Var", varName) 36 } 37 38 btfConst, ok := btfVar.Type.(*btf.Const) 39 if !ok { 40 return fmt.Errorf("%q not a const", varName) 41 } 42 43 btfVolatile, ok := btfConst.Type.(*btf.Volatile) 44 if !ok { 45 return fmt.Errorf("%q not volatile", varName) 46 } 47 48 refType, tags := btfhelpers.GetType(btfVolatile.Type) 49 if refType == nil { 50 i.logger.Warnf("unknown type for variable %q: %s (%T)", varName, btfVolatile.Type.TypeName(), btfVolatile.Type) 51 return nil 52 } 53 54 i.vars[varName] = &ebpfVar{ 55 name: varName, 56 refType: refType, 57 tags: tags, 58 } 59 60 i.gadgetCtx.Logger().Debugf("variable %q %v %+v", varName, refType, t) 61 i.gadgetCtx.SetVar(varName, reflect.New(refType).Elem().Interface()) 62 return nil 63 }