Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

gdb backtrace with no user input?

Tags:

c++

linux

bash

gdb

I'm wondering if it's possible to launch an application via GDB, on a SegFault write the backtrace to a file (to look at later), and then exit GDB all without any user input.

I'm running an application from a shell script in an infinite loop (so if it crashes it reloads) on OS boot from a non-interactive session. The application is crashing in a non-reproducible way so I need a backtrace from the crash to debug the issue. Ideally, I'd just modify the shell script to include the GDB debugging + backtracing functionality and preserve the automatic restarting of the application following a crash.

Is this possible to do?

like image 613
Matt Razza Avatar asked Nov 24 '13 18:11

Matt Razza


People also ask

What does backtrace do in GDB?

A backtrace is a summary of how your program got where it is. It shows one line per frame, for many frames, starting with the currently executing frame (frame zero), followed by its caller (frame one), and on up the stack. Print a backtrace of the entire stack: one line per frame for all frames in the stack.

How do you collect backtrace?

To print a backtrace of the entire stack, use the backtrace command, or its alias bt . This command will print one line per frame for frames in the stack. By default, all stack frames are printed. You can stop the backtrace at any time by typing the system interrupt character, normally Ctrl-c .

How do I backtrace for all threads?

To display the backtrace for several or all of the threads, use the command thread apply (see thread apply). For example, if you type thread apply all backtrace , gdb will display the backtrace for all the threads; this is handy when you debug a core dump of a multi-threaded program.


1 Answers

Thanks to Aditya Kumar; acceptable solution:

gdb -batch -ex "run" -ex "bt" ${my_program} 2>&1 | grep -v ^"No stack."$

like image 53
Matt Razza Avatar answered Sep 16 '22 11:09

Matt Razza