Programmatically break into GDB

You may want to stop into the debugger if something bad happened while executing your program. The easier way to do that is to have a breakpoint always set and to run your program in the debugger.

But sometimes you want to break even if there are no breakpoint. For example you know that something is wrong now and your program will crash later.

The following BreakIntoGDB() function checks if the application runs with GDB attached. If you are running your application with the debugger attached, BreakIntoGDB calls the Debugger() function. If you are running without GDB attached, BreakIntoGDB opens a Terminal window and attaches your application to the debugger using an Applescript. In both cases, your program stops into GDB and your can examine what's wrong.