iovisor / bcc

BCC - Tools for BPF-based Linux IO analysis, networking, monitoring, and more
Apache License 2.0
20.36k stars 3.86k forks source link

Fix userspace stack unwinding on powerpc #4881

Closed rnav closed 8 months ago

rnav commented 8 months ago

Nysal reported that the bcc tool offcputime.py does not display userspace stack traces on powerpc. Looking at the bpf program maps showed that stack traces were indeed being captured by the kernel, but were not being displayed by bcc userspace. This turned out to be the case since we were having the second entry in the stack trace as zero, and bcc's stack walker correctly assumes that the stack trace ends when a zero entry is encountered.

However, on powerpc, a perf callchain includes two additional entries after the first so as not to miss any data that may be required for stack unwinding. The first entry is always the nip (next instruction pointer) which is always valid. The second entry is LR, the link register, and the third entry is the value in the LR save area in the (second) stack frame. Due to how stack frames are setup, LR or the entry in the stack frame may be zero'ed out. In addition, with support for system call vectored in the kernel, we are setting LR to zero due to how the syscall interface now works. To disambiguate this, access to debuginfo would be necessary to understand which entry is the correct one to use. Since that isn't always possible, simply allow the stack unwinding to proceed when encountering zero in the second or third entry.

Before this patch: $ sudo ./offcputime.py -uU Tracing off-CPU time (us) of user threads by user stack... Hit Ctrl-C to end.

^C write

After this patch: $ sudo ./offcputime.py -uU Tracing off-CPU time (us) of user threads by user stack... Hit Ctrl-C to end.

^C write [unknown] [unknown] sudo_ev_loop_v1 sudo_ev_dispatch_v1 [unknown] [unknown] [unknown] [unknown] __libc_start_main