views:

138

answers:

5

When an assert fails or there is a segmentation fault, it would be very convenient that one of the following happens:

  • Program ask whether to run a debugger.
  • Program waits with crashing until debugger is attached.
  • Program leaves something (core dump?) that we can resume execution from this point and investigate.

The question is quite general due to variety of platforms, languages and debuggers. I'm asking about C++ and I guess that Windows (VS), Linux (gdb), Mac (gdb?) solutions would be most useful for community. I'm interested in Linux + gdb.

+2  A: 

On Windows there is DebugBreak() (and IsDebuggerPresent()), which is one of the options of what can happen when an assert fails.

On MacOS there are similar API calls (Debugger() or SysBreak()).

I don't know much about Linux, but AFAIK a failed assertion on Linux will cause a coredump, which can be looked at in the debugger.

sbi
`int 3` is the universal assembler instruction to break to a debugger, and is what the functions you named call internally.
Blindy
If by 'universal',you mean the x86 instruction for causing a break, sure ;)
Falaina
+1  A: 

On Linux, Basically when something horrible happens, your program receives a signal, There is a default behavior for the program if you do not 'mask' this signal, but you can usually 'mask' it to do something else, such as opening the gdb. You can find how to mask and a lot more from here, specifically here.

Regarding the assert, you can easily create your own version of assert, to do whatever you want.

Liran Orevi
IIRC, one of the defaults is to core dump.
Richard Corden
+3  A: 

On Linux (and probably OSX and other unixen), you can allow programs to leave a coredump with the ulimit utility.

Here's a quick howto.

gnud
+1  A: 

Unfortunately, my response only extends to Windows but it would make sense that Linux would also have some way to signal a debugger.

Any machine with Visual Studio installed on it should have Just in Time debugging enabled. This essentially means that the debugger does not have to be running when a process encounters a fatal exception.

Just in Time debugging is enabled through a registry key. Check out the link above for additional details.

If your looking to capture a process snap, for review later, then this is generally accomplished via Adplus.vbs (attended) or DebugDiag (unattended). Adplus is available through the Debugging Toolkit for Windows, but DebugDiag is a separate download.

Zach Bonham
A: 

Additionally to ulimit suggested by gnud, it could be a good idea to use a crash reporter: http://code.google.com/p/google-breakpad/w/list

Valentin Heinitz