13

GDBをアタッチせずにすべてのスレッドのスタックトレースを出力する方法はありますか?

または、すべてのスレッドのスタックトレースを出力するためにgdbバッチモードとして使用できるコマンドはありますか?

4

6 に答える 6

11

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)

Then you'll only "temporarily" attach with gdb and then detach.

于 2012-09-12T19:28:26.557 に答える
3

pstack?

使用法:

pstack <pid>

マニュアルページから:

pstack - print a stack trace of a running process
...
If the process is part of a thread group, then pstack will print out a stack trace for each of the threads in the group.
于 2012-09-12T19:40:15.140 に答える
1

私はこれを使用しています:

pidof program | xargs -n1 sudo gdb --batch -ex "thread apply all bt" -p
于 2017-01-12T14:09:48.907 に答える
0

Linux と同様に、すべてのスレッドは基本的に軽量プロセスであり、個々の LWP PID を取得します。psまたはtopベースのスクリプト を使用するかechowin を実行することで、これらのステータスをダンプできます/proc/sysrq-trigger。これI/Oにより、 内のスレッドの状態が得られます/var/log/messages

ただし、これでは完全なスタック トレースは得られません。しかし、定期的にスレッドの状態をデバッグするには良い方法です。

/proc/sched_debugどのタスクがスケジュールされているかなどを調べて、現在のスケジューラの状態を調べることもできます。

于 2012-09-12T20:44:30.163 に答える