pstack Command in Linux


The pstack command in Linux prints the stack trace of a running process. A stack trace is a list of function calls that shows how a program reached a certain point. It helps find errors by showing which functions were called before a crash or freeze happened.

Table of Contents

Here is a comprehensive guide to the options available with the pstack command −

Note − The pstack command is not available for ARM systems and is restricted to Linux environments running on x86 machines. For ARM systems, use the gdb command as an alternative.

Installation of pstack Command in Linux

The pstack command may not be preinstalled on all Linux distributions. To install it on Ubuntu, Kali Linux, Raspberry Pi OS, Debian, and other Debian-based distributions, use the following command −

sudo apt install pstack

Note that on Ubuntu, gdb or gstack can be used as substitutes for pstack.

To install pstack on Fedora, use the following command −

sudo dnf install pstack

On RHEL, pstack is included as part of the gdb package and does not require separate installation.

To verify the installation of the pstack command, check its bin file −

which pstack
pstack Command in Linux1

Syntax of pstack Command

The syntax of the pstack command in Linux is as follows −

pstack [pid…]

In the above syntax, the [pid…] field specifies one or more process IDs (PIDs) for which stack traces are printed.

Examples of pstack Command in Linux

This section demonstrates the usage of the pstack command in Linux with examples −

  • Displaying Stack Traces of a Process
  • Displaying Stack Traces of Multiple Processes
  • Displaying Usage Help

Displaying Stack Traces of a Process

To print the stack traces of a process, use the pstack command with the process ID. For example, to find the stack traces of PID 1726, use the following command −

sudo pstack 2652

Note that the target process must be running and accessible. Permissions may require sudo.

Displaying Stack Traces of Multiple Processes

To display the stack traces of multiple processes, use the following command −

sudo pstack 2652 7262

Displaying Usage Help

To display the usage help of the pstack command, execute the following command −

man pstack

Alternative of pstack Command in Linux

If the pstack command is not available, the parent tool gdb can also be used for a similar purpose. For example, to list the stack traces of a process, use the gdb command in the following way −

sudo gdb -p 2652
pstack Command in Linux2

The above output image shows the stack traces of a process with PID 2652. A stack trace is a list of function calls that shows how a program reached a certain point. It helps find errors by showing which functions were called before a crash or freeze happened.

Conclusion

The pstack command in Linux provides stack traces for running processes, helping diagnose errors by showing function calls leading to a crash or freeze. It is primarily available on x86 Linux systems, with gdb as an alternative for ARM-based environments.

Installation varies by distribution: pstack is part of the gdb package on RHEL and requires separate installation on Debian-based and Fedora systems.

The syntax of pstack command involves specifying process IDs, and it can display stack traces for one or multiple processes. If pstack is unavailable, the gdb command offers similar functionality.

Advertisements