Peeking at the stack

We can take a peek into the process stack (technically, the stack of main()) in different ways. Here, we show two possibilities:

  • Automatically via the gstack(1) utility
  • Manually with the GDB debugger

Peek at the usermode stack, first, via gstack(1):

WARNING! Ubuntu users, you might face an issue here. At the time of writing (Ubuntu 18.04), gstack does not seem to be available for Ubuntu (and its alternative, pstack, does not work well either!). Please use the second method (via GDB), as follows.

As a quick example, we look up the stack of bash (the parameter is the PID of the process):

$ gstack 14654#0  0x00007f3539ece7ea in waitpid () from /lib64/libc.so.6#1  0x000056474b4b41d9 in waitchld.isra ()#2 0x000056474b4b595d ...

Get Hands-On System Programming with Linux now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.