Attaching to a Running Program

If your program is already running, you can "attach to" the program to debug it. To attach to a running program you must know its process identifier or PID. In the examples that follow, the PID of the program is 8492.

Examples:

DBX Mode

 

% idb -pid 8492 a.out

 

or

 

% idb

(idb) attach 8492 a.out

GDB Mode

 

% idb -gdb -pid 8492 a.out

 

or

 

% idb -gdb (idb) file a.out (idb) attach 8492

 

Once you attach to a process, the process continues execution until it raises a signal that the debugger intercepts (for example, SEGV). If you have set the $stoponattach preference variable, it stops immediately.

One method you can use to attach to a process at a predictable location is to add a looping function to your program that keeps executing until the debugger takes control and you interrupt it (for example, with Ctrl+C). For example:

1. Add code such as the following to your application:

 

volatile int endStallForDebugger=0;

void stallForDebugger()

{

        while (!endStallForDebugger) ;

}

int main()

{

        ...

        stallForDebugger();

        ...

}

 

2. Run this version of your program.

3. Attach the debugger to the running process as described above.

4. Stop the program with Ctrl+C or by use of $stoponattach.

5. Use the debugger to assign to the stallForDebugger variable, and continue the execution of the process, so that it exits from the loop:

 

(idb) assign endStallForDebugger = 1

(idb) # set any needed breakpoints, and so on

(idb) cont