I am trying to debug a program that unexpectedly shuts down. When I say "shuts down, I mean one moment I am seeing all the windows being displayed, each of which is showing all the right data,then suddenly all the windows disappear. The is no messagebox reporting anything wrong. So I tried running the program in the debugger hoping that it would somehow trap whatever was causing the program to abort, but even within the debugger the program simply ends abruptly. The last line in the debugger is:
The program '[5500] test.exe: Native' has exited with code 0 (0x0).
My program, which is extremely large and extremely old, has a lot of self diagnostics. My suspicion is that perhaps a self test has failed and maybe I just called "exit()", forgetting to pop up a dialog explaining why.
My question now is, how can I find out from which point in the code, my program quit?
Set a breakpoint on exit() and terminate() (maybe one calls the other, but I'm not sure).
Marcelo's answer is great. If for some reason you can't break on exit, install a function (takes no arguments, returns void) with atexit and break inside that.
Related
So I am trying to use GDB. I compile my code with -g, then gdb ./a.out
GNU gdb starts, but after I type r to start, the program runs like it normally would if I just called ./a.out.
Do you know what could cause this? I don't know much about gdb and I use it lightly , I've been using it the same way for a while and never encountered this type of behavior.
Edit: It works when I set up breakpoints. But I am still confused as to why I was able to use it for months without setting any breakpoints before.
Do you know what could cause this?
This is intended behavior. The run command starts the execution of the inferior (being debugged) program.
That program may encounter a bug (e.g. a crash), in which case GDB will be notified and would stop the execution of the inferior, and let you look around.
The program may also encounter a breakpoint that you have inserted earlier, again allowing you to look around at the current state.
Or the program may run to completion (if it doesn't execute any code in which you have set breakpoints, or if you didn't set any, and if it doesn't have any bugs that manifest in a fatal signal). If that happens, you'll get a 'program exited normally' message.
I am still confused as to why I was able to use it for months without setting any breakpoints before.
Your program was probably crashing, and now doesn't.
My Qt app uses cURL library to send HTTP requests and sometimes cURL sends SIGSEGV and after that my app crashes.
Is it possible to catch this signal and prevent segmentation fault ?
TL;DR:
Don't attempt to block or ignore SIGSEGV. It will just bring you pain.
Explanation:
SIGSEGV means very bad things have happened, and your program has wandered into memory that it is not allowed to access. This means your program is utterly hosed.
Pardon my Canadian.
Your program is broken. You can't trust it to behave in any rational fashion anymore and should actually thank that SIGSEGV for letting you know this. As much as you don't want to see SIGSEGV, it's a hell of a lot better than the alternative of a broken program continuing to run and spewing out false information.
You can catch a SIGSEGV with a signal handler, but say you do catch and try to handle it. Odds are the program goes right back into the code that triggered the SIGSEGV and very likely raises it again as soon as the signal handler. Or does something else weird. Possibly something worse.
It's not really even worth catching SIGSEGV with a signal handler to make an attempt to output a message, because what's the message going to say? "Oh Smurf. Something very bad happened."?
So, yes, you can catch it, but you can't do anything to recover. The program is already too damaged to continue. That leaves preventing SIGSEGV in the first place, and that means fixing your code.
The best thing you can do is determine why the program crashed. The easiest way to do that is run your program with whatever debugger came with your development environment, wait for the crash, then inspect the backtrace for hints on how the program came to meet its doom.
Typically a link to Eric Lippert's How to debug small programs can be found right about here, and I can't think of a good reason to leave it out.
One other thing, though. cURL is a pretty solid library. Odds are overwhelmingly good that you are using it wrong or passed it bad information: a dead pointer, an unterminated C-style string, an pointer to an explosive function. I'd start by looking at how you are using cURL.
No, it's not. Instead, fix the bug and prevent the segmentation fault that way. Presumably your platform supplies some kind of debugger.
When debugging a C++ program emit a SIGSEGV with gdb,it is possible to handle the signal and asked to nostop.
How gdb handles this kind of scenario ??
Have searched gdb source code and couldn't find a starting point.
You cannot automatically ignore SIGSEGV. I also wouldn't recommend doing that anyway. Although you can make gdb ignore the signal and not pass it to the program, the kernel will attempt to re-run the offending instruction once the signal handler returns and results in an infinite loop. See this answer for more information.
One way to work around it is to the skip the instruction or change register values so that it does not segfault. The link shows an example of setting a register. You can also use the jump command to skip over an instruction.
is possible to handle the signal and asked to nostop.
It's unclear whether you want GDB to handle the signal, or the program itself.
If the latter, gdb handle SIGSEGV nostop noprint pass will do exactly that.
This is actually something the OS does. On Windows, if a program has a debugger attached and an exception is thrown, Windows will ask the debugger if it wants to handle it. If/when it declines, it passes it to the program. If the program doesn't handle it, Windows passes it to the debugger again.
I'm not sure if anyone uses Borland c++ 3.1, but I have to do it.
I have a program which implements simple threads and changes context of those threads through timer interrupt.
I have an infinite loop and 2 threads that do their job and change between each other and main's thread. Their job is to produce some output, to write something on console.
Problem is that every time I run the program, different thing happens.
Sometimes it works for half a minute and it just stops writing what it should. Write just stops and there is no error and borland doesn't crash.
Sometimes it stops and borland crashes without message.
Sometimes it stops and borland crashes with message "illegal instruction"
Sometimes in the last line it writes before it stops are some weird characters that shouldn't be in output.
Is it the console that is "full" and borland acts weird?
What can be a problem?
If I remember correctly, is was not safe to write to the console (or use file I/O) under DOS when called from an interrupt. To do it properly, you must check something called "DOS re-entrancy flag" and only write to the console if it is zero (See http://cs.smith.edu/~thiebaut/ArtOfAssembly/CH18/CH18-3.html or search the web for more information)
In real and virtual 8086 modes programs aren't protected from each other. So, if your program screws something up, for example:
overwrites memory that does not belong to it (or to the appropriate thread in itself), including memory corruptions due to stack overflows in the program or its ISRs
fails to preserve (=save, then restore) CPU registers in any of its ISRs
changes hardware states to something unexpected to the rest of the system
alters timer frequency in obvious to the rest of the system ways
if it does any of that, it should be no surprise that something crashes or hangs or misbehaves in some other way.
I'm guessing that you're having issues 1 and/or 2 above. You can have a race condition there as well.
Unfortunately, without seeing any of your code we can't be of any more help. Think of it, it's like treating a new patient by phone.
I have a very strange situation.
I'm running IOCP Server Program programmed by Visual studio 2010 in C++.
It uses 'minidump', so When there is a logical bug like pointer misuse, Program crashes with dump file so I can find out where is the crash point in codes.
Sometimes (very rarely), the program crashes and there are no dump files.
What situation makes SetUnhandledExceptionFilter() not working?
Anybody knows this problem? I can't figure out.
Well, of course you don't know because you don't have a minidump to look at. You should do the absolute minimum when the SetUnhandledExceptionFilter callback fires. The process is in a perilous state. It crashed. Locks might be held, the heap locks are particularly troublesome. You can't expect MiniDumpWriteDump() to succeed.
What you need is a little guard process that waits on a named event. Start it up as early as possible in your main() function and pass it your process ID. The guard process waits on both that event and your process handle. In your exception callback, just signal the event and immediately sleep for a long time. That wakes up the guard process, it calls MiniDumpWriteDump() plus whatever else is necessary to let you know about the crash. And kills your main program.