Debugging with Attach to Process
Visual Studio allows you to execute a program in debug mode and use various debugging tools to monitor its activity. When you are running a program outside of Visual Studio, it can still be possible to attach to that software to investigate its behaviour.
Visual Studio Debugger
It is common, when writing software using Visual Studio, to compile and execute a program directly from the integrated development environment (IDE) in debug mode. This allows you to step through the code line by line or to insert breakpoints or tracepoints that permit you to pause the program at a key point and interrogate the state of variables and objects. This tool can be invaluable when debugging your code.
Some software cannot be started directly from Visual Studio, perhaps because it must run in a specific environment or be executed using credentials other than those of the developer. Examples of such software are Windows services and COM applications. Such programs and libraries must be debugged in a slightly different manner.
Attaching to a Process
It is possible to attach the Visual Studio debugger to processes that are already running. However, there are some limitations, principally that the assembly to be monitored must be compiled in debug mode, not release mode. The key requirements for attaching to a running program are:
- The software was compiled in debug mode and the program database file (*.PDB) is available.
- The source code for the assembly, as referenced in the PDB file, is available and matches that of the compiled code.
- The version of Visual Studio that you are using supports attaching to processes. This facility is not available in the Express editions.
If the conditions are met, you can attach Visual Studio to the running process. You can then add breakpoints or tracepoints to halt the code at an important location and use the usual tools such as the immediate, locals and watches windows to assist in finding problems or confirming the correct operation of the code.
Showing the Attach to Process Dialog Box
To debug a running program you can use the Attach to Process dialog box. To display the dialog box when using Visual Studio with an open project, select "Attach to Process" from the Debug menu. If you do not currently have a project open, the Debug menu will not be visible. In this case, select "Attach to Process" from the Tools menu.
NB: If this menu option is missing, it may be because you have SQL Server Business Intelligence Studio installed. To show the option and other missing commands, follow the instructions in the knowledgebase article KB929664.
The main area of the window shows the processes that are executing in the context of the current user. If you wish to debug a program that is running with different credentials, tick the "Show processes from all users" checkbox. The list of processes is loaded when the dialog box is first displayed and does not update in real time. You can, however, refresh the list by clicking the refresh button.
Above the list of available processes is the "Attach to" box. This shows the type of process that you will be connecting to. For example, managed code or native code. By default, the selection is made automatically. As you select processes in the list, the contents of the box change to show the appropriate type. If you wish to be more specific about the process types that you wish to debug, you can click the Select button and choose from a list.
Once you have located and selected the process, or several processes, that you wish to debug, click the Attach button. The Visual Studio screen will change to show the debugging controls as if you had started debugging from within the IDE. You can then use the debugging toolbar and commands as well as all of the other usual debugging utilities.
When you have completed the debugging session, you can detach from the processes individually using the Processes window, which is displayed by selecting Processes from the Debug menu's Window submenu. Simply right-click a process in the list and select "Detach Process". You can also disconnect from all attached processes using the "Detach All" command from the Debug menu. Note that detaching does not terminate the process.
13 April 2009