How do I get the backtrace for all the threads in GDB?
Asked Answered
A

4

241

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?

Araby answered 22/8, 2013 at 22:27 Comment(4)
The corresponding command for LLDB is bt all — in case someone found this via Google (who thinks GDB == LLDB).Devanagari
With python the following works " (gdb) python for thread in gdb.selected_inferior().threads(): thread.switch(); print(thread.num);gdb.execute('where')"Finney
@Devanagari this is good enough to be put in a question IMOLuminous
In fact, I created the exact question: #67820906Luminous
M
408

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
Monaural answered 10/4, 2014 at 6:36 Comment(5)
To save the output to a file: gdb <binary> <coredump> -ex "thread apply all bt" -ex "quit" > output.logEdana
You can shorten this to t a a btBrunn
The command in @Edana comment hangs for me. Better try with gdb <binary> <coredump> -ex "thread apply all bt" -ex "detach" -ex "quit" > output.log, to avoid a question from gdb that blocks the command waiting for input.Sangfroid
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).Muss
@MarianoPaniga, or use --batch.Introspect
I
72

Is there a command that does?

thread apply all where
Ingunna answered 22/8, 2013 at 22:46 Comment(3)
A synonym for backtrace.Inconsonant
Just tried, this is not a synonym for backtrace.Maida
From info gdb where: The names 'where' and 'info stack' (abbreviated 'info s') are additional aliases for 'backtrace'.Introspect
R
22

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
Relish answered 18/5, 2021 at 8:23 Comment(0)
I
-2

If your process is running:

pstack $pid
Incurrent answered 24/2, 2022 at 11:52 Comment(1)
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.Gidgetgie

© 2022 - 2024 — McMap. All rights reserved.