Identify exact line on which application got crashed? - c++

How can I identify exact line of program on which my application crashed? Is there any tool to tell me which line in which source file has crashed the application?
I am using C/C++, MFC, and VC++.

Use gdb to see where your application crash. For linux, use gdb --args (Application command line)
use breakpoints for stepping into various functions. And if you run into crash, use bt to backtrace the code.
Though there are many many things in gdb, my aim was to give just the pointer, where you can begin.

For run time (non-debugger attached) exceptions I like using "BOOST_EXCEPTIONS" because the BOOST_THROW_EXCEPTION macro attaches BOOST_CURRENT_FUNCTION, FILE and LINE to the exception.
But the best way is to compile in debug mode and run with a debugger attached. If you're using visual studio (which with VC++, you probably already are) move to debug mode and run it. When an un-handled exception is thrown, it should bring you right there.
If you want to catch handled exceptions, from the Menu bar, Debug->Exceptions. Checking all of these will make the exceptions caught by the debugger, even when they are handled. Though people using exceptions for non-fatal errors can make this SUPER ANNOYING...

It depends a lot by what you mean by 'crash'. But assuming that you mean an exception occured in your app, then the EXCEPTION_RECORD structure will contain the exact address where the exception occurred, in other words the IP that was executed when the exception was raised. On Windows C++ exception use SEH. Read the timeless A Crash Course on the Depths of Win32™ Structured Exception Handling to understand SEH.
You can retrieve the exception info from a crash dump (see ecxr Display Exception Context Record) or you can instruct debuggers to break on exception. Dr. Watson will create dumps for you when crash occurs.

Related

Why is the throw location not in the backtrace / callstack?

I am attempting to make post-mortem life a little easier for a very large and cross-platform project that makes use of many 3rd party libraries. On Linux, I use backtrace() to dump the backtrace/callstack to console. On Windows, I use EXCEPTION_POINTERS to create a minidump and that StalkWalker class to dump the callstack to console. Now, with all the platform-specific signal handlers registered and SEH enabled on Windows, I catch and report a lot of helpful information, most importantly, the file and line number of where the bad thing happened. However, in the callstack, I have noticed that I never actually get the location of any throw statements, only where it was caught, i.e. a catch block. As you can imagine, if your try-catch encompasses a gigantic amount of code, where the exception was caught is not terribly helpful. So, why is that?
And now for the obvious followup question: How do I get the location of throw statements into my callstack console dumps?
I will stop anyone who tries to tell me to change all the throw statements to custom exceptions with __FILE__ and __LINE__ macros right here. Please refer back to my opening sentence. As such, present uses of throw will not be changed.

A program I support is crashing with SIGSEGV but I can't from the .dmp file

As per the title, I can't locate any dump files when this program I support is crashing.
The application's logs clearly mention its a SIGSEGV exception, but I have searched my entire hard drive, and there are no .dmp files anywhere to be found.
The developers of the program have seen similar issues elsewhere but have so far been unable to explain why this is happening - and we're kind of a bit stuck at the moment.
The last section in the application logs reads as :
Received signal SIGSEGV, segmentation violation.
OurApplication::sigHandler 11.
Removing signal handlers.
OurApplication::signalCatched.
OurApplication::sigHandler: exiting application.
Removing signal handlers.
My limited understanding of this is that our application's signal handler might be 'neutralising' the SIGSEGV exception that got thrown. And therefore no core dump is getting generated... I did raise this idea with the developers but they never really seemed have investigated if this might be the reason. The theory they raised in counter was that they think the reason the dmp isn't getting generated is because the program may be crashing twice very close together.
So the questions I have at this point are:
Are there any Windows7 parameters that control the creation of a .dmp file?
Are there any requirements/flags that need to be compiled into a program in order for it (or windows) to create a core dump file if it crashes?
I'm 99% sure it must be windows that is responsible for creating the core file, since the program itself would be dead/terminated when it crashed, correct?
Are there any other things I should be aware of, or check for, or 'evidence' I can collect and then show our developers?
Many thanks in advance
Are there any Windows7 parameters that control the creation of a .dmp file?
There are parameters which control the creation of a crash dump: see MSDN on Collecting user-mode dumps.
Are there any requirements/flags that need to be compiled into a program in order for it (or windows) to create a core dump file if it crashes?
You don't need to compile anything in for the previous answer to work. However, the program needs to terminate due to an unhandled exception, which means you need to let the exception bubble up and not being handled by the unhandled exception handler.
I'm 99% sure it must be Windows that is responsible for creating the core file, since the program itself would be dead/terminated when it crashed, correct?
As stated above, Windows can handle that and it's a good idea to have Windows handle the crash. Imagine that your program is broken due to a memory leak. Some memory has been overwritten. In that case, your unhandled exception handler can be destroyed. Windows, however, still has full control over the process, can suspend it and create a dump from outside (rather from inside).
Are there any other things I should be aware of, or check for, or 'evidence' I can collect and then show our developers?
Well, suggest letting the dump be created by Windows due to above reasons. Then they also don't need to implement a configuration setting (you don't want the crash dump file to be always created, do you?). You don't need to implement a limiting number for the files. You don't need to implement a check for disk space, etc.
And you can suggest to read the Windows Internals 6 books.
Consider creating your own minidump file programatically. Should be plenty of code around showing how to do it. You can try here:
https://stackoverflow.com/search?q=minidump
This way, you're not relying on Dr. Watson or any other settings to create a dump file. Instead you will be calling the functions in DBGHELP.DLL to create the dump file.

How to tell the XCode debugger to only stop at breakpoints? (and not at exceptions)

I'm writing a 3rd party plug-in for a closed-source application. I'm running into a problem with the XCode debugger.
I'd like to use the debugger to step through the code of the plug-in I'm writing. To do this, I've set a breakpoint in my code, I've launched the host app, and then I've attached the XCode debugger to the host app's process. My problem is that, rather than stopping at my breakpoint, the debugger stops instead at an exception thrown by another part of the program. (Specifically, this: Thread 2: EXC_ARITHMETIC (code=EXC_i386_DIV, subcode=0x0))
This exception is thrown so often that it effectively makes it impossible to step through the code I'm actually interested in.
I can't catch and handle this exception, because I don't have the source code for the part of the app that throws it.
In this situation, can anyone suggest how I might be able to debug my code? Is there any way to tell the XCode debugger to simply "ignore" this particular EXC_ARITHMETIC exception, and, instead, to stop only on the breakpoints that I've explicitly set?
UPDATE
#user1118321 suggested turning off exception breakpoints using the breakpoints pane. I've tried to do so using the settings shown here.
I thought that selecting "Automatically continue after evaluating" might keep the debugger from stopping. It didn't.
I also tried issuing cont to the debugger after stopping. This doesn't seem to work either.
Can anyone you advise me on what settings I should enter here to keep the debugger from stopping on my EXC_ARITHMETIC exception?
That's an integer "division by zero" error exception. They aren't propagated as C++ exceptions as far as I'm aware, which is why you can't not break on them.
Does that happen even if your plugin isn't loaded? If it doesn't, your plugin is causing a division by zero somehow.
I followed the links suggested by #user1118321. After several hours of head scratching, I couldn't figure out how to either turn off or handle the integer division by zero exception. If anyone has any ideas I'd be happy to hear them.
I did find a workaround, though, which is to use GDB instead of XCode. Unlike XCode, GDB doesn't seem to stop on the EXC_ARITHMETIC and lets me step into my code. So, for the time being I'm going to switch over to GDB for debugging.
EDIT
A second (better) workaround, in my case, involved suspending the thread that was throwing the exception. This also allowed me to step through my plugin's code.

Can I get a stack trace for un-handled (Objective) C++ exceptions?

I'm developing an iOS application that has recently grown a large C++ base. C++ is not my forte, and I'm getting frustrated by exceptions. What I'm looking for is a way to get a stack track to the site of an (un-handled) exception throw. I'll say that the "un-handled" qualifier is optional; I would settle for breaking on any exception throw as a last resort, though un-handled exceptions are ideal.
What I currently get is useless. Assuming I don't have any appropriate exception handlers higher up the callstack, and I do something like
std::vector<int> my_vector;
my_vector.at(40) = 2; // Throws std::out_of_range
The app will break in main() and I'll get a log message saying "terminate called throwing an exception." Not helpful.
Putting generic try/catch blocks higher up the callstack doesn't help either, because the callstack is unwound during exception handling up to the point of the catch block, leaving me ignorant to actual origin of the exception. This also applies to providing my own terminate_handler. Asserts are more useful, but they require me to anticipate error conditions to some extent, which I cannot always do. I would still like the debugger to be able to step in even if an unexpected exception makes it past my pre-emptive assert()s.
What I want to avoid is having to wrap every call that might possibly throw an exception in a try/catch block just to get the stack trace to the error. At runtime, I'm really not interested in catching these exceptions. When they occur, it means there's a fatal flaw in the program execution, and there's no way it can continue normally. I just want to be notified so I can determine the cause and mend the issue so it won't happen again.
In Objective C, I can put a symbolic breakpoint on objc_exception_throw, and any time I screw something up I'll immediately break execution and be presented with a nice stack trace so I know where the issue is. Very helpful.
I realize this behavior is really only useful because of a philosophical difference in exception handling between the two languages. Objective C exceptions are intended only to signify unrecoverable errors. The task of routine error handling is accomplished via error return codes. This means that any Objective C exception is a great candidate for a breakpoint to the developer.
C++ seems to have a different use for Exceptions. They're used to handle both fatal errors and routine errors (at least in the 3rd party libs I'm using). This means I might not actually want to break on every exception that's thrown in C++, but I would still find the ability useful if I can't break only on un-handled exceptions.
You can quickly establish a break on all C++ throw conditions in Xcode:
cmd+6
"+" button -> Add Exception Breakpoint
C++ -> std::out_of_range
On Throw
Update
If you have a lot of them tho filter out, you may prefer to:
Create a Symbolic Breakpoint
Symbol = __cxa_throw (may vary by std library)
Action > Debugger Command = bt
Automatically continue after eval = On
The bt command logs the backtrace. Configured this way, it will automatically continue.
So, this will just log the backtrace of every thrown exception - when your program terminates due to an unhandled exception, the clues will be among the final logged backtraces (often the last, unless the library rethrows).
In the app I get to debug with many c++ exceptions, I leave the "Catch C++ Exceptions on Throw" off until I get to the point in the app where it will throw the exception, then I turn that option on and usually the next exception that is thrown is what I'm looking for. This will break a few levels deeper than where the error is, but the stack is intact so you can figure out what is going on.
Check PLCrashReporter. We use it with our application (which relies heavily on C++) and it produces stack traces even for the C++ code.
The only problem you might have is when using assembly routines which were not written natively for iOS (Apple's compiler is using R7 to hold the stack frame for tracing back symbols which is not according to official ARM EBI)

Unhandled Win32 exception

At runtime, when myApp.exe crashes i receive "Unhandled Win32 exception" but how would i know which exception was occurred? where did something went wrong?
For a Native C++ app see my earlier answer here: Detect/Redirect core dumps (when a software crashes) on Windows for catching the unhandled exception (that also gives code for creating a crash dump that you can use to analyse the crash later. If the crash is happening on a development system then in Visual Studio (I'm assuming you're using that, if not other IDEs will have something similar), in Debug/Exceptions tick the 'Thrown' box for 'Win32 Exceptions'.
Typically, Windows will give you several hexadecimal numbers as well. Chances are that the exception code will be 0xC0000005. This is the code of an Access Violation. When that happens, you also will have three additional bits of information: the violating address, the violated address, and the type of violation (read, write or execute).
Windows won't narrow it down any further than that, and often it couldn't anyway. For instance, if you walk past the end of an array in your program, Windows likely won't realize that you were even iterating over an array. It just sees "read:OK, read:OK, read:out of bounds => page fault => ACCESS VIOLATION". You will have to figure that out from the violating address (your array iteration code) and the violated address (the out-of-bounds address behind your array).
If it's a .Net app you could try to put in a handledr for the UnhandledException event. You can find more information about it and some sample code here.
In general it's a good sign that your exception handling is broken though, so might be worth going through your code and finding places that could throw but where you don't handle exceptions.
Use the debugger. You can run the program and see what exception is been thrown that kills your application. It might be able to pinpoint the location of the throw. I have not used the VS debugger for this, but in gdb you can use catch throw to force a breakpoint when an exception is thrown, there should be something similar.