Processes
pstack
Print stack traces of a running process.
Additional Notes
pstack attaches to a running process and prints a stack trace for each thread. It shows the function call chain at the point of inspection, including function names and addresses. This is useful for understanding what a process is doing at a specific moment, diagnosing hangs, deadlocks, or unexpected behavior.
pstack works by using ptrace to attach to the target process, reading its stack frames, and symbolizing the addresses using the process's symbol table. On modern systems, pstack is often a shell script that invokes gdb with a batch command to dump thread backtraces.
Syntax
pstack [options] [pid...]
Parameters
pid: Process ID(s) to examine.
Common Options
-p pid: Specify the PID (alternative syntax).-w: Wait if the process is stopped.-D: Show debug information.-M: Enable multi-process mode.--help: Show help and exit.--version: Show version information.
Examples
pstack 1234
Print the stack trace of PID 1234.
pstack $(pidof mysqld)
Stack trace of all mysqld processes.
pstack -p 5678
Stack trace using the -p syntax.
for pid in $(pidof java); do pstack $pid >> java_stacks.log; done
Collect stack traces from all Java processes over time for analysis.
Practical Notes
pstackis part of thegdborpstackpackage. Install withsudo apt install gdborsudo apt install pstack.- The target process must have read permissions.
pstacktypically needs the same user or root. - Stacks from optimized code may show incomplete or incorrect backtraces due to inlining and frame pointer omission.
- For Java processes, use
jstackinstead, which understands JVM internals and produces Java-level stack traces. - Continuous sampling with
pstackcan help identify where a process is spending time. Collect multiple samples over a period. - For kernel-level stack traces of threads, use
/proc/PID/stackorcat /proc/PID/wchan. - The
gdb-basedpstackscript is more portable than compiled alternatives, but it is slower because it startsgdbfor each invocation.