tags:

views:

24

answers:

1

Hello,

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.

A: 

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.

viraptor
yeah, sorry for the use of language process dont run inside gdb... but my doubt still remains, ptrace traces the system calls made by the process, so how can it give the stack trace of all the threads inside the pid??
g__k
It's `strace` that takes care of tracing system calls. `ptrace` can do much more http://linux.die.net/man/2/ptrace - it's a general way of poking around another process. It can get/set the registers, memory ranges, instruction pointer of the foreign process and much more. So poking around the stack is almost as simple as getting the SP and reading up (down) the memory.
viraptor