I'm working on a big app (ns2) and somewhere someone put an exit(1) in there without any debug or print statements and it is being executed. I don't want to have to manually check every file that calls exit to figure out why the program is exiting. Is is possible to determine where the program exited? This is running on Linux and coded in C++. Is it possible to do something like this in gdb?
Sure. Put a breakpoint at the start of exit(3). When it breaks, look at the stack.
Second choice, run it under truss(1) (I'm pretty sure there's a Linux version of truss.)
Or strace(1).
Update
In fact, I ran across another method in another question: here's a link.
If you supply your own implementation of exit()
, then that one will be used in preference to the one in the library. Build that, load it in the debugger, put a breakpoint on your exit()
, run the program, and see where it came from.
Or as Charlie says, just put a breakpoint on the library exit()
. Use the above method if you want to run some other specific bit of code instead.
As you seem to have access to the source code I would simply search for the call to exit and analyze the code. Then you can most likely also fix it by replacing the exit with something more appropriate.
If all your code is in the directory src the a
grep -r exit src
should find all occurences of the word exit
If you do not have access to the code go with the suggestion(s) of Charlie.