Preparing Your Program for Debugging: Modifying Your Program to Wait for the Debugger

To modify your program to wait for the debugger, complete the following steps:

1. Add some code similar to the following:

void loopForDebugger()

{

    if (!getenv("loopForDebugger")) return;

    static volatile int debuggerPresent = 0;

    while (!debuggerPresent);

}

2. Add in code to call this function at some convenient point early in your application's execution.

3. Set the environment variable when you want to debug the application

4. Use the debugger's attach capability to get the process under control.

5. When you have the process under debugger control, you can set examine values and set breakpoints. When you are ready to proceed, assign a non-zero value to debuggerPresent, and continue your program. For example:

% setenv loopForDebugger ""

% a.out arg1 arg2 &

% idb -pid 1709 a.out

^C

(idb) assign debuggerPresent = 1

...

(idb) cont