From f27fa68807007b0f6c0c5bab67d218588e1ec71f Mon Sep 17 00:00:00 2001 From: Florian Lehner Date: Tue, 29 Oct 2024 10:45:53 +0100 Subject: [PATCH] fixup: dynamic symbol prefix lookup for kprobe Signed-off-by: Florian Lehner --- libpf/symbol.go | 12 ++++++++++++ tracer/tracer.go | 7 ++++++- 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/libpf/symbol.go b/libpf/symbol.go index d71a5f8f..bb33af2f 100644 --- a/libpf/symbol.go +++ b/libpf/symbol.go @@ -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 @@ -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) { diff --git a/tracer/tracer.go b/tracer/tracer.go index 34ba1819..9c8d317b 100644 --- a/tracer/tracer.go +++ b/tracer/tracer.go @@ -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 }