When you attach to a running program, it is immediately suspended. In many
cases, the program is suspended in operating system code or in the compiled
language run time. You might need to add a breakpoint or breakpoints and resume
the program to get to a known point where you want to start debugging.
There are two main reasons for attaching the debugger to a process or already
running program:
- You anticipate a problem at a particular point in your program, and you
do not want to step through the program or set breakpoints. In this situation,
you can run your program, and during a program pause shortly before the anticipated
failure (for example, while the program is waiting for keyboard input), you
attach the debugger. You can then provide the input, and debug from that point
on.
- You are developing or maintaining a program that hangs sporadically, and
you want to find out why it is hanging. In this situation, you
can attach the debugger, and look for infinite loops or other problems that
might be causing your program to hang.