Skip to content

Commit

Permalink
fixup: dynamic symbol prefix lookup for kprobe
Browse files Browse the repository at this point in the history
Signed-off-by: Florian Lehner <[email protected]>
  • Loading branch information
florianl committed Oct 29, 2024
1 parent 9c29d4b commit f27fa68
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
12 changes: 12 additions & 0 deletions libpf/symbol.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package libpf // import "go.opentelemetry.io/ebpf-profiler/libpf"
import (
"fmt"
"sort"
"strings"
)

// SymbolValue represents the value associated with a symbol, e.g. either an
Expand Down Expand Up @@ -81,6 +82,17 @@ func (symmap *SymbolMap) LookupSymbol(symbolName SymbolName) (*Symbol, error) {
return nil, fmt.Errorf("symbol %v not present in map", symbolName)
}

// LookupSymbolByPrefix loops over all known symbols and returns the first symbol
// that starts with the given prefix.
func (symmap *SymbolMap) LookupSymbolByPrefix(prefix string) (*Symbol, error) {
for name, sym := range symmap.nameToSymbol {
if strings.HasPrefix(string(name), prefix) {
return sym, nil
}
}
return nil, fmt.Errorf("no symbol not present that starts with '%s' in map", prefix)
}

// LookupSymbolAddress returns the address of a symbol.
// Returns SymbolValueInvalid and error if not found.
func (symmap *SymbolMap) LookupSymbolAddress(symbolName SymbolName) (SymbolValue, error) {
Expand Down
7 changes: 6 additions & 1 deletion tracer/tracer.go
Original file line number Diff line number Diff line change
Expand Up @@ -1277,7 +1277,12 @@ func (t *Tracer) StartOffCPUProfiling() error {
return errors.New("off-cpu program finish_task_switch is not available")
}

kprobeLink, err := link.Kprobe("finish_task_switch.isra.0", kprobeProg, nil)
kprobeSymbol, err := t.kernelSymbols.LookupSymbolByPrefix("finish_task_switch")
if err != nil {
return errors.New("failed to find kernel symbol for finish_task_switch")
}

kprobeLink, err := link.Kprobe(string(kprobeSymbol.Name), kprobeProg, nil)
if err != nil {
return err
}
Expand Down

0 comments on commit f27fa68

Please sign in to comment.