225

Is there an equivalent command in GDB to that of WinDbg's !process 0 7?

I want to extract all the threads in a dump file along with their backtraces in GDB. info threads doesn't output the stack traces. So, is there a command that does?

Guy Avraham
  • 3,482
  • 3
  • 38
  • 50
srikantha ks
  • 2,251
  • 2
  • 13
  • 3

4 Answers4

381

Generally, the backtrace is used to get the stack of the current thread, but if there is a necessity to get the stack trace of all the threads, use the following command.

thread apply all bt
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Sharad
  • 3,843
  • 1
  • 11
  • 2
  • 20
    To save the output to a file: `gdb -ex "thread apply all bt" -ex "quit" > output.log` – Doomsday Mar 28 '18 at 09:32
  • 8
    You can shorten this to `t a a bt` – qbolec Mar 13 '20 at 09:25
  • 5
    The command in @Doomsday comment hangs for me. Better try with `gdb -ex "thread apply all bt" -ex "detach" -ex "quit" > output.log`, to avoid a question from gdb that blocks the command waiting for input. – Mariano Paniga Aug 12 '20 at 12:52
  • The more generic syntax is `thread apply [threadno] [all] args`, where `[threadno]` can also be a space-separated list of thread identifiers (e.g. `2 3 4`), or a range (e.g. `2-6`). – nnunes Sep 20 '21 at 16:32
  • @MarianoPaniga, or use `--batch`. – Stephane Chazelas Oct 27 '21 at 09:15
68

Is there a command that does?

thread apply all where
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Employed Russian
  • 199,314
  • 34
  • 295
  • 362
17

When debugging with several threads, it is also useful to switch to a particular thread number and get the backtrace for that thread only.

From the GNU GDB threads documentation

For debugging purposes, GDB associates its own thread number--a small integer assigned in thread-creation order--with each thread in your program.

Usage:

info threads

Then identify the thread that you want to look at.

thread <thread_id>

Finally, use backtrace for just that thread:

bt
diviquery
  • 569
  • 5
  • 19
-2

If your process is running:

pstack $pid
EricRen
  • 7
  • 2
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Feb 25 '22 at 08:46