From 94ff4bc4aaad8bcd3a4eaf0e536610bed1b87833 Mon Sep 17 00:00:00 2001 From: "mingjun.zhou" Date: Fri, 20 Sep 2024 13:54:19 +0800 Subject: [PATCH] Fix unresovled kernel symbols when /proc/kallsyms is not sorted --- ebpf/symtab/kallsyms.go | 5 +++++ ebpf/symtab/kallsyms_test.go | 4 +++- 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/ebpf/symtab/kallsyms.go b/ebpf/symtab/kallsyms.go index 3601dafa96..722c1acc28 100644 --- a/ebpf/symtab/kallsyms.go +++ b/ebpf/symtab/kallsyms.go @@ -5,6 +5,7 @@ import ( "fmt" "os" "runtime" + "sort" "strconv" ) @@ -94,5 +95,9 @@ func NewKallsymsFromData(kallsyms []byte) (*SymbolTab, error) { if allZeros { return NewSymbolTab(nil), nil } + // kallsyms maybe unsorted when bpf/modules are loaded from userspace after kernel boot. + sort.Slice(syms, func(i, j int) bool { + return syms[i].Start < syms[j].Start + }) return NewSymbolTab(syms), nil } diff --git a/ebpf/symtab/kallsyms_test.go b/ebpf/symtab/kallsyms_test.go index fbb34bb3ae..155f409563 100644 --- a/ebpf/symtab/kallsyms_test.go +++ b/ebpf/symtab/kallsyms_test.go @@ -25,7 +25,9 @@ ffffffff81001750 T memblock_find ffffffff81001780 T __memblock_alloc_base ffffffff810017d0 T memblock_alloc ffffffff81001820 T early_memtest -ffffffff810018a0 T early_memtest_report` +ffffffff810018a0 T early_memtest_report +ffffffff81000800 T unordered_symbol_0 +ffffffff81000100 T unordered_symbol_1` func TestKallsyms(t *testing.T) { kallsyms, err := NewKallsymsFromData([]byte(testdata))