Skip to content

Commit a73673b

Browse files
authored
Fix issues when porting alloy/pyroscope to android (#3582)
* Fix when CONFIG_PID_NS is not supported * Fix unresovled kernel symbols when /proc/kallsyms is not sorted
1 parent 784d4de commit a73673b

File tree

3 files changed

+21
-11
lines changed

3 files changed

+21
-11
lines changed

ebpf/session.go

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -167,16 +167,19 @@ func (s *session) Start() error {
167167
}
168168

169169
_, nsIno, err := getPIDNamespace()
170-
if err != nil {
171-
return fmt.Errorf("unable to get pid namespace %w", err)
172-
}
173-
err = spec.RewriteConstants(map[string]interface{}{
174-
"global_config": pyrobpf.ProfileGlobalConfigT{
175-
NsPidIno: nsIno,
176-
},
177-
})
178-
if err != nil {
179-
return fmt.Errorf("pyrobpf rewrite constants %w", err)
170+
// if the file does not exist, CONFIG_PID_NS is not supported, so we just ignore the error
171+
if !os.IsNotExist(err) {
172+
if err != nil {
173+
return fmt.Errorf("unable to get pid namespace %w", err)
174+
}
175+
err = spec.RewriteConstants(map[string]interface{}{
176+
"global_config": pyrobpf.ProfileGlobalConfigT{
177+
NsPidIno: nsIno,
178+
},
179+
})
180+
if err != nil {
181+
return fmt.Errorf("pyrobpf rewrite constants %w", err)
182+
}
180183
}
181184
err = spec.LoadAndAssign(&s.bpf, opts)
182185
if err != nil {

ebpf/symtab/kallsyms.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"fmt"
66
"os"
77
"runtime"
8+
"sort"
89
"strconv"
910
)
1011

@@ -94,5 +95,9 @@ func NewKallsymsFromData(kallsyms []byte) (*SymbolTab, error) {
9495
if allZeros {
9596
return NewSymbolTab(nil), nil
9697
}
98+
// kallsyms maybe unsorted when bpf/modules are loaded from userspace after kernel boot.
99+
sort.Slice(syms, func(i, j int) bool {
100+
return syms[i].Start < syms[j].Start
101+
})
97102
return NewSymbolTab(syms), nil
98103
}

ebpf/symtab/kallsyms_test.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,9 @@ ffffffff81001750 T memblock_find
2525
ffffffff81001780 T __memblock_alloc_base
2626
ffffffff810017d0 T memblock_alloc
2727
ffffffff81001820 T early_memtest
28-
ffffffff810018a0 T early_memtest_report`
28+
ffffffff810018a0 T early_memtest_report
29+
ffffffff81000800 T unordered_symbol_0
30+
ffffffff81000100 T unordered_symbol_1`
2931

3032
func TestKallsyms(t *testing.T) {
3133
kallsyms, err := NewKallsymsFromData([]byte(testdata))

0 commit comments

Comments
 (0)