LinuxCommandLibrary

stack

Display/manipulate call stacks

TLDR

Create a new package

$ stack new [package] [template]
copy

Compile a package
$ stack build
copy

Run tests inside a package
$ stack test
copy

Compile a project and re-compile every time a file changes
$ stack build --file-watch
copy

Compile a project and execute a command after compilation
$ stack build --exec "[command]"
copy

Run a program and pass an argument to it
$ stack exec [program] -- [argument]
copy

SYNOPSIS

pstack PID [...]

PARAMETERS

PID
    The Process ID of the running process for which to display the stack trace. Multiple PIDs can often be specified.

DESCRIPTION

The pstack command (often a script or utility wrapping gdb) is used to display the call stack of a running process. It attaches to a specified Process ID (PID) and outputs a symbolic stack trace for all threads within that process. This is invaluable for debugging live processes, understanding where a program is currently executing, or diagnosing deadlocks and infinite loops. pstack simplifies the process of obtaining a stack trace, as it automates the steps that would typically be performed manually using a debugger like gdb, such as attaching to the process, getting a backtrace, and detaching. It's particularly useful when you need a quick snapshot of a process's state without launching a full debugging session.

CAVEATS

pstack typically relies on gdb being installed and accessible in the system's PATH.
It requires appropriate permissions to attach to the target process, often necessitating root privileges or being the owner of the process.
Attaching to a process with pstack (or gdb) momentarily pauses the process. While usually brief, this can impact real-time or performance-sensitive applications.
The quality of the stack trace (e.g., symbol resolution, line numbers) depends on whether the target program was compiled with debugging information (e.g., using the -g flag in GCC).
It may not work reliably or at all for certain types of processes, such as kernel threads or processes that are protected against debugging.

USAGE EXAMPLE

To use pstack, first find the Process ID (PID) of the application you want to inspect. You can use commands like ps or top.

1. Find the PID:
ps aux | grep my_application
(Let's say the PID is 12345)

2. Run pstack:
sudo pstack 12345
(Use sudo if you don't own the process or need root privileges)

This will output the call stack for all threads within process 12345, showing the function calls leading to the current execution point.

HISTORY

The pstack utility is not a standalone command with a long independent development history. Instead, it emerged as a convenience wrapper around the powerful GNU Debugger (gdb). Debuggers have long been able to inspect process stacks, but the commands could be cumbersome. pstack automates the common task of attaching, printing a backtrace, and detaching, making it quicker for system administrators and developers to get a snapshot of a process's state. Its exact origins are somewhat varied, with different Unix-like systems and Linux distributions providing their own versions or scripts, often as part of gdb's ancillary utilities or standalone packages like procps-ng.

SEE ALSO

gdb(1), strace(1), ltrace(1), ps(1), top(1), kill(1)

Copied to clipboard