Starting the Debugger Using the debugBreak Function

Bugs frequently occur in places that are time consuming for debuggers to reach. It is often more efficient to run a problematic program unobstructed until it needs the debugger, and then start the debugger.

You can add the debugBreak function to any program. When the debugBreak function is called, it creates a debugger process with the initialization needed to connect the debugger (via attach) to the program. This process invokes the debugger, positioned at the prompt, with control over the program just after the debugBreak call.

This chapter contains code for the debugBreak function that you can add to your program and the procedure for implementing it. It also notes several restrictions.

The following example shows the output from a trivial C program called c_debugBreak, which is started without the debugger. When the debugBreak function is called, the debugger starts up, taking control:

How to Use debugBreak

To use debugBreak, complete the following steps:
  1. Add the debugBreak function to the program source.
  2. Add calls to the debugBreak function in strategic locations. The debugBreak function takes one argument: the name of the program executable file.
    
    debugBreak(argv[0]); /* Something is wrong! */
    

    argv[0] provides the correct information for most programs. If you do not want to use argv[0], pass the pathname of your executable as a literal string, for example:

    /usr/users/smith/foo
  3. Recompile and relink the program (with -g) to include these changes.
  4. Run the program. The debugger is activated when debugBreak is called. The debugger connects to the program via attach, which means:
See the manual for more information on the attach command.

Restrictions

Following are several minor restrictions to using debugBreak:

debugBreak Source Code

The debugBreak function is written in C, for widest compatibility. You can compile it into a .o file for inclusion by entering the following: This produces a debugBreak.o file that must be included in the link step for your program. You can also compile it to start the debugger with the GUI, as follows: The debugbreak.c code follows:

To simplify adding calls to debugBreak in the code, you can include the debugbreak.h file where needed to provide the proper function prototype: