benfred / py-spy

Sampling profiler for Python programs
MIT License
12.16k stars 401 forks source link

Display native thread ID in py-spy dump #567

Closed Jongy closed 11 months ago

Jongy commented 1 year ago

py-spy dump gives lines like:

Thread 0x7ED3637AE700 (active): "ThreadPoolExecutor-275_6"

This is the thread object address in the process, and its Pythonic name.

My use case is - I am debugging a hung Python process, I want to correlate Python stacks to native stacks taken with GDB. It'd be convenient if py-spy outputs the Linux TID. I prefer to use native stacks from GDB which are more complete, compared to the native stacks emitted by the --native flag.

benfred commented 1 year ago

I believe py-spy will dump out the OS thread id if it has it on linux/windows - but the problem is that it's not always available right now unless you are using python 3.11. ( https://github.com/benfred/py-spy/blob/c325f41b6c9a2ba984acc131b7d764c424ad44e8/src/stack_trace.rs#L156-L168 )

For python 3.10 and below - py-spy will not be able to figure out the OS thread id if you are using --nonblocking, if the processor isn't x86_64 on linux, and if you are profiling from the host OS into a docker container etc.