benfred / py-spy

Sampling profiler for Python programs
MIT License
12.14k stars 400 forks source link

Elf sbss issue #621

Closed wynemo closed 8 months ago

wynemo commented 8 months ago

Hello!

I noticed that when using py-spy to read a Python library for mips64el from https://github.com/benfred/py-spy/blob/master/src/binary_parser.rs#L96, it seems to be reading "sbss".

bash-4.2# readelf -S /tmp/mips64.txt | grep -i 'NOBITS'
[24] .sbss NOBITS 900000000034ad28 0033ad28
[25] .bss NOBITS 900000000034ad30 0033ad28

i also try to use goblin to check:

image

I'm not sure if it can be adjusted slightly here, maybe like this:

diff --git a/src/binary_parser.rs b/src/binary_parser.rs
index a8d977a..bceaf9a 100644
--- a/src/binary_parser.rs
+++ b/src/binary_parser.rs
@@ -93,7 +93,9 @@ pub fn parse_binary(filename: &Path, addr: u64, size: u64) -> Result<BinaryInfo,
             let bss_header = elf
                 .section_headers
                 .iter()
-                .find(|header| header.sh_type == goblin::elf::section_header::SHT_NOBITS)
+                .filter(|header| header.sh_type == goblin::elf::section_header::SHT_NOBITS)
+                .max_by_key(|header| header.sh_size)
                 .ok_or_else(|| {
                     format_err!(
                         "Failed to find BSS section header in {}"

thanks.

benfred commented 8 months ago

thanks for the bug report! I also hit this issue when I was trying to add python 3.12 support - py-spy was picking up the .tbss section in the python 3.12 binary from conda, and failing.

The fix you suggested works here (thanks!) - but I also added an additional check on the section name in #624, trying to make sure the section is named .bss and not only taking the largest section.