Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does the pstack command work?

Tags:

system

libc

I am curious to find how does the pstack command prints the stack trace of all the threads running under the PID?

It has to be someway different than the way gdb does since the process runs inside the gdb environment, but pstack is executed after the execution of the process.

like image 649
g__k Avatar asked Jul 07 '10 10:07

g__k


2 Answers

It's the same general idea as gdb. pstack uses ptrace, which allows an external process to attach to a known pid and print out the information (stack is known via the current registers).

If you want to know exactly how it's done, look for information about ptrace.

Also, processes don't really run "inside the gdb". You can attach gdb to a running process without much trouble by running gdb executable pid.

like image 193
viraptor Avatar answered Nov 09 '22 10:11

viraptor


pstack print similar output as cat /proc/"pid"/tasks/*/stack so it most likely that it read the procfs rather than using the ptrace.

like image 36
Shashank Kukreti Avatar answered Nov 09 '22 08:11

Shashank Kukreti