LinuxCommandLibrary

pstack

Show stack trace of running processes

SYNOPSIS

pstack pid | pstack core

PARAMETERS

pid
    The process ID of the running process to analyze.

core
    The path to a core file generated by a crashed process. Instead of a running process, pstack can analyze core dumps to find the same issues.

DESCRIPTION

The `pstack` command in Linux is used to display the stack trace of a running process or a core file. This information is invaluable for debugging purposes, as it shows the sequence of function calls that led to the current state of the process. It allows developers to understand the execution path and identify potential issues such as deadlocks, infinite loops, or crashes.

By examining the stack trace, developers can pinpoint the exact location in the code where the program is failing or exhibiting unexpected behavior. The output typically includes function names, memory addresses, and arguments passed to the functions. `pstack` uses the process ID to attach to the target process and retrieve the necessary information from the process's memory space. It relies on debugging information generated during the compilation process to accurately map memory addresses to function names. It's primarily useful on single and multithreaded application debugging.

CAVEATS

Requires appropriate permissions to access the process's memory space (usually root or the user owning the process). Requires debugging symbols be available to accurately map addresses to function names.

OUTPUT INTERPRETATION

The output of `pstack` shows a call stack. Each line in the stack trace represents a function call. The most recently called function is at the top of the stack, and the first function called is at the bottom. The output also includes the address of the function in memory and the arguments passed to the function (if available).

DEBUGGING MULTITHREADED APPLICATIONS

When used with multithreaded applications, `pstack` displays the stack trace for each thread within the process, allowing developers to identify issues in specific threads.

HISTORY

The `pstack` command is historically associated with Solaris systems. A similar command was later implemented for Linux. It provides a simpler alternative to using full-fledged debuggers like GDB for quickly examining the stack of a process.

SEE ALSO

gdb(1), strace(1), ltrace(1)

Copied to clipboard