Skip to content

Commit

Permalink
[third-party/py-spy/0.3.14] Don't include undefined symbols in symbol…
Browse files Browse the repository at this point in the history
… index

Summary:
Don't count undefined symbols in the index of symbols that py-spy builds.
This can causes e.g. py-spy to misattribute an undefined ref to `_PyRuntime`
in some location other than `libpython.so` as the definition.

Upstreamed as: benfred#629

Test Plan:
Ran on `/packages/cpu.xlformers.train/penv.par`.  Before, we'd die
with:

```
$ RUST_LOG=info ./fbpy-spy dump -p 1162
[2023-10-31T18:04:04.658254536Z INFO  py_spy::config] Command line args: ArgMatches { args: {}, subcommand: Some(SubCommand { id: [hash: B8461C91A07ADDC8], name: "dump", matches: ArgMatches { args: {[hash: CD5160AB4406C427]: MatchedArg { occurs: 1, source: Some(CommandLine), indices: [2], type_id: Some(TypeId { t: 69534013883876418352099503721857626982 }), vals: [[AnyValue { inner: TypeId { t: 69534013883876418352099503721857626982 } }]], raw_vals: [["1162"]], ignore_case: false }}, subcommand: None } }) }
[2023-10-31T18:04:04.660694834Z INFO  py_spy::python_spy] Got virtual memory maps from pid 1162:
[2023-10-31T18:04:07.033385523Z INFO  py_spy::python_spy] Found libpython binary @ /usr/local/fbcode/platform010/lib/libpython3.10.so.1.0
[2023-10-31T18:04:07.038415315Z INFO  py_spy::python_spy] got symbol Py_GetVersion.version (0x00007fa5a425acf0) from libpython binary
[2023-10-31T18:04:07.038425108Z INFO  py_spy::python_spy] Getting version from symbol address
[2023-10-31T18:04:07.039366641Z INFO  py_spy::version] Found matching version string '3.10.9+fb (3.10:1dd9be6, May  4 2022, 01:23:45) [Clang 12.0.1 (mononoke://'
[2023-10-31T18:04:07.039374857Z INFO  py_spy::python_spy] python version 3.10.9 detected
[2023-10-31T18:04:07.039380427Z INFO  py_spy::python_spy] got symbol _PyRuntime (0x000056301cf89000) from python binary
[2023-10-31T18:04:07.039498251Z WARN  py_spy::python_spy] Interpreter address from _PyRuntime symbol is invalid 0000000000000040
[2023-10-31T18:04:07.039503358Z INFO  py_spy::python_spy] Failed to get interp_head from symbols, scanning BSS section from main binary
[2023-10-31T18:04:07.154577459Z INFO  py_spy::python_spy] Failed to get interpreter from binary BSS, scanning libpython BSS
Error: Failed to find a python interpreter in the .data section
```

After:
```
$ RUST_LOG=info ./py-spy dump -p 1162
[2023-10-31T18:04:20.036236603Z INFO  py_spy::config] Command line args: ArgMatches { args: {}, subcommand: Some(SubCommand { id: [hash: B8461C91A07ADDC8], name: "dump", matches: ArgMatches { args: {[hash: CD5160AB4406C427]: MatchedArg { occurs: 1, source: Some(CommandLine), indices: [2], type_id: Some(TypeId { t: 69534013883876418352099503721857626982 }), vals: [[AnyValue { inner: TypeId { t: 69534013883876418352099503721857626982 } }]], raw_vals: [["1162"]], ignore_case: false }}, subcommand: None } }) }
[2023-10-31T18:04:20.038355392Z INFO  py_spy::python_spy] Got virtual memory maps from pid 1162:
[2023-10-31T18:04:22.319161826Z INFO  py_spy::python_spy] Found libpython binary @ /usr/local/fbcode/platform010/lib/libpython3.10.so.1.0
[2023-10-31T18:04:22.323992753Z INFO  py_spy::python_spy] got symbol Py_GetVersion.version (0x00007fa5a425acf0) from libpython binary
[2023-10-31T18:04:22.324001859Z INFO  py_spy::python_spy] Getting version from symbol address
[2023-10-31T18:04:22.324937137Z INFO  py_spy::version] Found matching version string '3.10.9+fb (3.10:1dd9be6, May  4 2022, 01:23:45) [Clang 12.0.1 (mononoke://'
[2023-10-31T18:04:22.324946474Z INFO  py_spy::python_spy] python version 3.10.9 detected
[2023-10-31T18:04:22.324951227Z INFO  py_spy::python_spy] got symbol _PyRuntime (0x00007fa5a42531b0) from libpython binary
[2023-10-31T18:04:22.325348234Z INFO  py_spy::python_spy] Found interpreter at 0x00007fa57daea000
[2023-10-31T18:04:22.325352986Z INFO  py_spy::python_spy] got symbol _PyRuntime (0x00007fa5a42531b0) from libpython binary
[2023-10-31T18:04:22.325356193Z INFO  py_spy::python_spy] Found _PyRuntime @ 0x00007fa5a42531b0, getting gilstate.tstate_current from offset 0x238
Process 1162: [xarexec] /packages/cpu.xlformers.train/penv.par -tt /dev/shm/uid-0/894107fb-seed-nspid4026533351_cgpid8628534-ns-4026533348/__run_xar_main__.py --model=genesis220B_kv8 --model.non_linearity=swiglu --model.use_rope=True --model.init.use_gaussian=True --model.init.use_depth=current --model.alpha_depth=disabled --optim.lr=0.00015 --optim.lr_min_ratio=0.1 --optim.warmup=2000 --seq_len=4096 --batch_size=4 --steps=476000 --unlimited_steps=False --log_freq=10 --eval_freq=-1 --profile_freq=-1 --dump_freq=50 --iter_type=multi --fp32_reduce_scatter=False --checkpoint_destination=directio --model_entity_id=-1 --do_checkpoint=True --model_parallel_size=8 --log_all_steps=True --gpu_check_level=-1 --tokenizer_dir=/mnt/wsfuse/tokenizers --periodic_gpu_check=False --data=/mnt/wsfuse/fair_llm_v2/shuffled/stackexchange:0.88,/mnt/wsfuse/fair_llm_v2/shuffled/b3g:3.15,/mnt/wsfuse/fair_llm_v2/shuffled/arxiv:1.14,/mnt/wsfuse/fair_llm_v2/shuffled/github_oss_with_stack:4,/mnt/wsfuse/fair_llm_v2/shuffled/c4/en:6,/mnt/wsfuse/fair_llm_v2/edouard_cc_20220927_new:24.7,/mnt/wsfuse/fair_llm_v2/ccnet_new:28.3,/mnt/wsfuse/fair_llm_v2/shuffled/wikipedia:3.5 --use_libuv=True --model_ckpt_multiplier=1 --optim_ckpt_multiplier=1 --dump_dir=/mnt/wsfuse/outputs/torchx-cpu-xlformers-h514mwh
Python v3.10.9 (/dev/shm/uid-0/894107fb-seed-nspid4026533351_cgpid8628534-ns-4026533348/runtime/bin/train#native-main#platform-runtime#python#py_version_3_10)

Thread 0x7FA5B5B8E000 (active): "MainThread"
    _single_tensor_adamw (torch/optim/adamw.py:466)
    adamw (torch/optim/adamw.py:335)
    step (torch/optim/adamw.py:184)
    _use_grad (torch/optim/optimizer.py:76)
    wrapper (torch/optim/optimizer.py:373)
    wrapper (torch/optim/lr_scheduler.py:68)
    main (train.py:761)
    manifoldfs_main_wrapper (train.py:296)
    inner (contextlib.py:79)
    <module> (train.py:1204)
    _run_code (runpy.py:86)
    _run_module_as_main (runpy.py:196)
    run_as_main (__par__/bootstrap.py:58)
    run_as_main (__par__/meta_only/bootstrap.py:76)
    __invoke_main (__run_xar_main__.py:91)
    <module> (__run_xar_main__.py:140)
Thread 0x7FA55F400000 (idle): "Thread-1"
    wait (threading.py:324)
    get (queue.py:180)
    _run (tensorboard/summary/writer/event_file_writer.py:225)
    run (tensorboard/summary/writer/event_file_writer.py:253)
    _bootstrap_inner (threading.py:1016)
    _bootstrap (threading.py:973)
Thread 0x7FA562C00000 (idle): "Thread-2"
    wait (threading.py:324)
    get (queue.py:180)
    _run (tensorboard/summary/writer/event_file_writer.py:225)
    run (tensorboard/summary/writer/event_file_writer.py:253)
    _bootstrap_inner (threading.py:1016)
    _bootstrap (threading.py:973)
Thread 0x7F9FC4600000 (idle): "Thread-3"
    wait (threading.py:324)
    get (queue.py:180)
    _run (tensorboard/summary/writer/event_file_writer.py:225)
    run (tensorboard/summary/writer/event_file_writer.py:253)
    _bootstrap_inner (threading.py:1016)
    _bootstrap (threading.py:973)
Thread 0x7F9F85A00000 (idle): "Thread-4"
    wait (threading.py:324)
    get (queue.py:180)
    _run (tensorboard/summary/writer/event_file_writer.py:225)
    run (tensorboard/summary/writer/event_file_writer.py:253)
    _bootstrap_inner (threading.py:1016)
    _bootstrap (threading.py:973)
Thread 0x7F9F85000000 (idle): "Thread-5"
    wait (threading.py:324)
    get (queue.py:180)
    _run (tensorboard/summary/writer/event_file_writer.py:225)
    run (tensorboard/summary/writer/event_file_writer.py:253)
    _bootstrap_inner (threading.py:1016)
    _bootstrap (threading.py:973)
Thread 0x7F9F84600000 (idle): "Thread-6"
    wait (threading.py:324)
    get (queue.py:180)
    _run (tensorboard/summary/writer/event_file_writer.py:225)
    run (tensorboard/summary/writer/event_file_writer.py:253)
    _bootstrap_inner (threading.py:1016)
    _bootstrap (threading.py:973)
```

Reviewers: bmaurer, kunalb, wenyinfu

Reviewed By: bmaurer

Subscribers: mzlee

Differential Revision: https://phabricator.intern.facebook.com/D50847131
  • Loading branch information
andrewjcg committed Sep 3, 2024
1 parent d8e41bf commit b5f3780
Showing 1 changed file with 8 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/binary_parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,10 +130,18 @@ pub fn parse_binary(filename: &Path, addr: u64, size: u64) -> Result<BinaryInfo,
let offset = offset - aligned_vaddr;

for sym in elf.syms.iter() {
// Only count defined symbols.
if sym.st_shndx == goblin::elf::section_header::SHN_UNDEF as usize {
continue;
}
let name = elf.strtab[sym.st_name].to_string();
symbols.insert(name, sym.st_value + offset);
}
for dynsym in elf.dynsyms.iter() {
// Only count defined symbols.
if dynsym.st_shndx == goblin::elf::section_header::SHN_UNDEF as usize {
continue;
}
let name = elf.dynstrtab[dynsym.st_name].to_string();
symbols.insert(name, dynsym.st_value + offset);
}
Expand Down

0 comments on commit b5f3780

Please sign in to comment.