Linux

pstack — Seeing Where a Stuck Process Is Stuck

Contents

Sometimes a process on a server just hangs: no CPU, no response. Before killing it, pstack shows where it is stuck.

Tested on Ubuntu 24.04 with gdb 15.1.

What it is

Give it the PID of a running process and it prints the call stack of each thread. It does not kill or restart anything.

1
pstack <pid>
  • It started as a Solaris command
  • On CentOS and RHEL it ships in the gdb package. pstack is a symlink to gstack, a script that attaches gdb and runs thread apply all bt
  • Since gdb 16, gstack is part of upstream gdb

Think of it as jstack for C and C++ programs.

A program that deadlocks

Two threads take two mutexes in opposite order. After one second they wait on each other forever.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include <pthread.h>
#include <stdio.h>
#include <unistd.h>

pthread_mutex_t a = PTHREAD_MUTEX_INITIALIZER;
pthread_mutex_t b = PTHREAD_MUTEX_INITIALIZER;

void *worker1(void *arg) {
    pthread_mutex_lock(&a);
    sleep(1);
    pthread_mutex_lock(&b);   /* worker2 holds b */
    return NULL;
}

void *worker2(void *arg) {
    pthread_mutex_lock(&b);
    sleep(1);
    pthread_mutex_lock(&a);   /* worker1 holds a */
    return NULL;
}

int main(void) {
    pthread_t t1, t2;
    pthread_create(&t1, NULL, worker1, NULL);
    pthread_create(&t2, NULL, worker2, NULL);
    printf("pid %d\n", getpid());
    fflush(stdout);
    pthread_join(t1, NULL);
    pthread_join(t2, NULL);
    return 0;
}
1
2
gcc -g -O0 -pthread -o hang hang.c
./hang &
1
2
3
$ ps -o pid,stat,nlwp,cmd -p 601
  PID STAT NLWP CMD
  601 Sl      3 ./hang

Three threads (NLWP), state S (sleeping). From the outside it looks like a normal idle process.

Ubuntu’s pstack package does not work

Ubuntu does have a pstack package.

1
apt install pstack
1
2
3
4
5
$ pstack 601
pstack: Input/output error
failed to read target.

601: ./hang

It is version 1.3.1, a very old tool that cannot read 64-bit processes. On Ubuntu, use gdb directly.

With gdb

This is exactly what pstack does.

1
gdb -q -batch -ex "thread apply all bt" -p 601
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
Thread 3 (Thread 0x7fdf5ffff6c0 (LWP 603) "hang"):
#0  futex_wait (private=0, expected=2, futex_word=0x55ebdb842080 <b>) at ../sysdeps/nptl/futex-internal.h:146
#1  __GI___lll_lock_wait (futex=futex@entry=0x55ebdb842080 <b>, private=0) at ./nptl/lowlevellock.c:49
#2  0x00007fdf600a01c1 in lll_mutex_lock_optimized (mutex=0x55ebdb842080 <b>) at ./nptl/pthread_mutex_lock.c:48
#3  ___pthread_mutex_lock (mutex=0x55ebdb842080 <b>) at ./nptl/pthread_mutex_lock.c:93
#4  0x000055ebdb83f261 in worker1 (arg=0x0) at hang.c:11
#5  0x00007fdf6009cb64 in start_thread (arg=<optimized out>) at ./nptl/pthread_create.c:447
#6  0x00007fdf60129e8c in clone3 () at ../sysdeps/unix/sysv/linux/x86_64/clone3.S:78
Thread 2 (Thread 0x7fdf5f7fe6c0 (LWP 604) "hang"):
#0  futex_wait (private=0, expected=2, futex_word=0x55ebdb842040 <a>) at ../sysdeps/nptl/futex-internal.h:146
#1  __GI___lll_lock_wait (futex=futex@entry=0x55ebdb842040 <a>, private=0) at ./nptl/lowlevellock.c:49
#2  0x00007fdf600a01c1 in lll_mutex_lock_optimized (mutex=0x55ebdb842040 <a>) at ./nptl/pthread_mutex_lock.c:48
#3  ___pthread_mutex_lock (mutex=0x55ebdb842040 <a>) at ./nptl/pthread_mutex_lock.c:93
#4  0x000055ebdb83f2a0 in worker2 (arg=0x0) at hang.c:18
#5  0x00007fdf6009cb64 in start_thread (arg=<optimized out>) at ./nptl/pthread_create.c:447
#6  0x00007fdf60129e8c in clone3 () at ../sysdeps/unix/sysv/linux/x86_64/clone3.S:78
Thread 1 (Thread 0x7fdf603de740 (LWP 601) "hang"):
#0  0x00007fdf60098e31 in __futex_abstimed_wait_common64 (...) at ./nptl/futex-internal.c:57
...
#3  0x00007fdf6009e863 in __pthread_clockjoin_ex (...) at ./nptl/pthread_join_common.c:102
#4  0x000055ebdb83f337 in main () at hang.c:28
[Inferior 1 (process 601) detached]

For each thread, read down from #0 until you hit the first frame from your own code.

  • Thread 3: worker1, hang.c:11, waiting on mutex b
  • Thread 2: worker2, hang.c:18, waiting on mutex a
  • Thread 1: main, hang.c:28, waiting in pthread_join

Thread 3 waits for b, thread 2 waits for a, and the source shows each one holds what the other wants. A deadlock.

A pstack-like wrapper

1
2
3
#!/bin/sh
# usage: mypstack <pid>
exec gdb -q -batch -ex "set pagination off" -ex "thread apply all bt" -p "$1" 2>/dev/null | grep -E '^(Thread|#)'

With gdb 16 or later you can just run gstack <pid>. Ubuntu 24.04 ships gdb 15, so it is not there.

eu-stack

eu-stack from elfutils does the same thing with shorter output.

1
2
apt install elfutils
eu-stack -p 601
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
PID 601 - process
TID 601:
#0  0x00007fdf60098e31 __GI___futex_abstimed_wait_cancelable64
#1  0x00007fdf6009e863 __pthread_clockjoin_ex
#2  0x000055ebdb83f337 main
...
TID 603:
#0  0x00007fdf60099030 __GI___lll_lock_wait
#1  0x00007fdf600a01c1 __pthread_mutex_lock@GLIBC_2.2.5
#2  0x000055ebdb83f261 worker1
...
TID 604:
#0  0x00007fdf60099030 __GI___lll_lock_wait
#1  0x00007fdf600a01c1 __pthread_mutex_lock@GLIBC_2.2.5
#2  0x000055ebdb83f2a0 worker2
...

You get function names only: no file, no line number, no mutex name. Enough to see roughly where things stopped.

Things to watch

The process pauses

While gdb is attached through ptrace, the process is stopped. Here the first run took about 1 second and later runs about 0.22 seconds. Most of that is loading symbols, so a big program takes longer. Don’t run it several times a second on a production box.

Without debug symbols

Built without -g and stripped, your own functions show up as ??.

1
2
3
#3  ___pthread_mutex_lock (mutex=0x55797918a040) at ./nptl/pthread_mutex_lock.c:93
#4  0x00005579791872fa in ?? ()
#5  0x00007f206d29cb64 in start_thread (arg=<optimized out>) at ./nptl/pthread_create.c:447

libc still has symbols, so you can tell it is waiting on a mutex, but not in which function, and the mutex names (<a>, <b>) are gone. Keep symbol files for production binaries somewhere.

Permissions

You cannot attach to another user’s process. As a normal user against a root process:

1
2
3
4
Could not attach to process.  If your uid matches the uid of the target
process, check the setting of /proc/sys/kernel/yama/ptrace_scope, or try
again as the root user.  For more details, see /etc/sysctl.d/10-ptrace.conf
ptrace: Inappropriate ioctl for device.
1
eu-stack: dwfl_linux_proc_report pid 601: Permission denied

If it fails even for your own process, check /proc/sys/kernel/yama/ptrace_scope. Running as root usually works.

Java and Python

pstack only sees native frames. It will not show where your own code is.

I tried a Java program that just sleeps in a loop.

1
2
3
4
5
6
7
8
public class Slow {
    static void waitForever() throws Exception {
        while (true) Thread.sleep(10000);
    }
    public static void main(String[] args) throws Exception {
        waitForever();
    }
}

18 threads show up. Below JVM_Sleep the main thread is all ??, and Slow or waitForever appear nowhere in the output.

1
2
3
4
5
6
#5  0x00007fe88dd1bf37 in PlatformEvent::park_nanos(long) () from .../libjvm.so
#6  0x00007fe88d934a04 in JavaThread::sleep_nanos(long) () from .../libjvm.so
#7  0x00007fe88d9f89e5 in JVM_Sleep () from .../libjvm.so
#8  0x00007fe877d43ac0 in ?? ()
#9  0x00007fe88cffe840 in ?? ()
#10 0x00007fe877d437a5 in ?? ()

jstack on the same process shows it right away.

1
2
3
4
5
"main" #1 [857] prio=5 os_prio=0 cpu=36.96ms elapsed=3.22s ... waiting on condition
   java.lang.Thread.State: TIMED_WAITING (sleeping)
	at java.lang.Thread.sleep0(java.base@21.0.13.1/Native Method)
	at java.lang.Thread.sleep(java.base@21.0.13.1/Thread.java:509)
	at Slow.waitForever(Slow.java:3)

Python 3.13 is the same story: above clock_nanosleep you get interpreter functions like PyEval_EvalCode and ??, never the Python function name. Use the language’s own tool: jstack for Java, something like py-spy dump for Python. I did not try py-spy.

References