This is the same general idea as gdb. pstack uses ptrace, which allows an external process to join the known pid and print information (the stack is known through the current registers).
If you want to know exactly how to do this, find information on ptrace.
, " gdb". gdb , gdb executable pid.