Getting stacktrace of all threads without attaching GDB

后端 未结 6 850
甜味超标
甜味超标 2021-02-13 16:24

Is there a way to print stacktrace of all threads without attaching GDB?

Or is there a command which I can use as gdb batch mode to print stacktrace of all threads?

6条回答
  •  有刺的猬
    2021-02-13 16:53

    There is a thread apply all command in GDB:

    (gdb) thread apply all bt
    Thread 12 (Thread 0x7f7fe2116700 (LWP 5466)):
    #0  sem_wait () at ../nptl/sysdeps/unix/sysv/linux/x86_64/sem_wait.S:86
    #1  0x0000000000425358 in ?? ()
    ...
    Thread 1 (Thread 0x7f7feabc27c0 (LWP 5465)):
    #0  0x00007f7fe76c5203 in select () at ../sysdeps/unix/syscall-template.S:82
    

    Sadly, GDB seems not to be able to read the commands from a pipe, so to run the commands in its batch mode, a temporary file must be used:

    $ gdbbt() {
      tmp=$(tempfile)
      echo thread apply all bt >"$tmp"
      gdb -batch -nx -q -x "$tmp" -p "$1"
      rm -f "$tmp"
    }
    $ gdbbt $(pidof $SHELL)
    

提交回复
热议问题