tags:

views:

276

answers:

3

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?

+8  A: 

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.

Charlie Martin
Unless they were cruel enough to call _exit() or _Exit() instead of exit()...probably best to trap all three. truss/strace won't identify where the system call is called from.
Jonathan Leffler
No, but I usually find that the sequence from a truss, strace, or ltrace (*another* option) gives me enough of a clue to find it. The find/grep I put in a comment would work if all else fails.
Charlie Martin
+3  A: 

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.

Greg Hewgill
A: 

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.

lothar