Skip to content

Commit

Permalink
Fix issues when porting alloy/pyroscope to android (#3582)
Browse files Browse the repository at this point in the history
* Fix when CONFIG_PID_NS is not supported

* Fix unresovled kernel symbols when /proc/kallsyms is not sorted
  • Loading branch information
zmj64351508 authored Sep 24, 2024
1 parent 784d4de commit a73673b
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 11 deletions.
23 changes: 13 additions & 10 deletions ebpf/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,16 +167,19 @@ func (s *session) Start() error {
}

_, nsIno, err := getPIDNamespace()
if err != nil {
return fmt.Errorf("unable to get pid namespace %w", err)
}
err = spec.RewriteConstants(map[string]interface{}{
"global_config": pyrobpf.ProfileGlobalConfigT{
NsPidIno: nsIno,
},
})
if err != nil {
return fmt.Errorf("pyrobpf rewrite constants %w", err)
// if the file does not exist, CONFIG_PID_NS is not supported, so we just ignore the error
if !os.IsNotExist(err) {
if err != nil {
return fmt.Errorf("unable to get pid namespace %w", err)
}
err = spec.RewriteConstants(map[string]interface{}{
"global_config": pyrobpf.ProfileGlobalConfigT{
NsPidIno: nsIno,
},
})
if err != nil {
return fmt.Errorf("pyrobpf rewrite constants %w", err)
}
}
err = spec.LoadAndAssign(&s.bpf, opts)
if err != nil {
Expand Down
5 changes: 5 additions & 0 deletions ebpf/symtab/kallsyms.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"os"
"runtime"
"sort"
"strconv"
)

Expand Down Expand Up @@ -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
}
4 changes: 3 additions & 1 deletion ebpf/symtab/kallsyms_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down

0 comments on commit a73673b

Please sign in to comment.