Generating a stack trace on exit in gdb - c++

My C++ application is giving me some strange output but it runs to completion, I'd like to examine the stack trace but since it isn't segfaulting it is harder to pinpoint where it is. I've tried setting break points on exit, _exit, and abort, but when I call the stack I get something like this
#0 0x00002aaaab1a7620 in exit () from /lib64/libc.so.6
#1 0x000000000041f19e in main ()
This is probably because my application has a perl front end wrapped with sig, is there another way to generate a stack upon completion?

That's what the stack trace should look like. main calls exit at the end and you print the stack trace inside exit. You cannot find where something happens with a stack trace. Once you find where something happens, you can get the stack trace there and find out how the execution got there.
Whatever output you're looking for happened before exit and that function has already returned by the time you get the stack trace. So, you need to put your break point before the output happens. Then you can go over the code line by line to find out which line does the output.

did you build it in debug?
add a breakpoint with "b function_name"
press c to continue
type bt to print the backtrace

Related

Automatically printing the stack trace in GDB

My code on a cluster running PBS fails with a "floating point exception". That means, when I submit the code via qsub, it returns a fail status and aborts. I would like to attach GDB to the program and configure it in a way that when it hits an exception, prints the stack trace before abortion. However, I haven't found out how to do that.
Any thoughts on that?

gdb asking to continue/quit?

I am new to GDB, using to debug an issue with my program(C++). I used gdb to find the backtrace and then print frames information. During one of the print, let's say the command is something like:
1) frame 2
2) print *this
In between the output of the print call, I am getting the following line:
---Type <return> to continue, or q <return> to quit---
Is this expected from the gdb? We should just continue on that message or it means something?
Note: I didn't set any breakpoint or anything, so didn't expect any message.
When there is too much to print to fit inside the current buffer, gdb will pause after it's hit the limit. If you press enter, it'll continue printing.
I assume this points to an object that has a lot of "stuff" in it, so it's perfectly normal that you get this message. Just keep pressing return until you see the information you're looking for or until there is nothing more to print!
(In other words, there is no breakpoint or anything of the sort getting triggered here and nothing happens to the control flow of the debugged program.)

GDB function call tracing

Is there a way to attach a print statement to the call of a function? I would like to debug a x64 program with nested loops and logic and it would be faster to see the sequence of function calls by printing them as they occur, rather than setting breakpoints.
Can this be done with a post hook in gdb or a different technique?
Is there a way to attach a print statement to the call of a function?
Yes: attach a breakpoint to every function you want to trace, and attach commands to each of these breakpoints:
(gdb) break foo
(gdb) commands $bpnum
continue
end
Now every time foo is called, GDB will print the usual "Breakpoint N ..." message, and then continue.
Obviously you could print additional info (argument values, call stack, thread-id, etc.).
You will probably want to set height 0 to disable pagination. You will also probably want to log this to a file (see set logging file, set logging on, etc.)

GDB backtrace without stopping

I am trying to let my program run continously with GDB.
Currently I have a bash script which starts GDB with my program and when it crashes it prints the backtrace and starts GDB again (endless loop).
Now I added a signal handler for my program which kills specific threads when the handler gets a signal from them. Now I can achieve that GDB does not stop by doing this:
handle SIGSEGV nostop
But this leads me to the problem that I do not get a GDB backtrace which I would like to print automatically without stopping the program (or at least continuing automatically).
Any help would be appreciated!
Continue to use handle to suppress ordinary stops from SEGV. Then set a catchpoint that does what you want:
(gdb) catch signal SIGSEGV
(gdb) commands
> silent # this bit is optional
> bt
> continue
> end
This will print a backtrace on SIGSEGV but not otherwise interfere with normal operation. You may also want handle SIGSEGV noprint.

Get functions called with GDB

I am using GDB to understand a C++ program. I put a break in the middle of the running which turns to be something like:
break main.cpp:500
and I would like to see which functions have been called before. I tried "backtrace" but it shows only info about main, as previous calls to previous functions have already finished.
My question is how can I get (with GDB or another method) the info about which functions have been called before this point, even if the call has been returned.
Thanks
A gdb script might be a solution for your problem.
Create a script that puts break point to each possibly called function.
At break prints the stack with 'bt' and continue the execution.
You should put an other break point to main.cpp:500 to quit from debugging.
b 'main.cpp::500'
commands 1
detach
quit
end
break 'A::f1()'
break 'A::f2()'
while true
continue
bt
end
You can start the script like this:
gdb --command ./gdbscript.gdb fpmanager
If you have too much possibly called function you can grep the code to find all.