Sunday, March 13, 2011

Visual Studio Debugging - Attach to Process

Debugging!!!

I am sure this is the every good programmers way of finding and fixing bugs. Now I am going to give you all a brief idea about this nice feature called Attach to Process in Visual Studio. You can find this under Debug menu in Visual Studio. We can use Attach to Process to break into a running application and begin a debug session. This feature can be used mainly for debugging web applications which can be a  running instance of a web application or a web service hosted by IIS. ASP.NET applications will run in either an aspnet_wp.exe process (for IIS 5.x), or a w3wp.exe process (for IIS 6.0 and after).

Now when we click the Attch to Process it will display the following window.

Attach To Process
In here it shows all the available processses. There are several options you can select. If the program that you want to debug is running on another computer, you must first select the remote computer by selecting the Qualifier.

In the Attach to box, make sure that the type of code you will debug is listed. The default Automatic setting tries to determine what type of code you want to debug. If the automatic setting is not appropriate click Select and Select the code type and click Ok. In here I am selecting Managed (v4.0) code, Native code and T-SQL code. Actually when we click on the process, you can see the code type changes according to the selected process.

Select Attach to
Managed (v4.0) code, Native code, T-SQL code


We can select either to show processes from current user or show processes from all users by clicking the tick on Show processes from all users.

And the next option is bit important and that is Show processes in all sessions. Sometimes when you click the Attach to Process you might not see a single w3wp.exe running. The reason behind this can be one of the following.
  • The web application/web service might not be running.
  • Show processes in all sessions might not be ticked.
So if your web application/web service is already running and still you can't see the w3wp.exe under Available Processes you should tick the Show processes in all sessions and now you will be able to see running w3wp.exe process. You can always click Refresh to get tge current list of processes.

Now open the .cs file you want to debug using Visual Studio and set a break point to where you want to start debugging. Then go to Attach To Process and select the w3wp.exe and in your web application/web service go to the page where you have set the break point. Now when you enter to the break point, you can start debugging.

Things to Note:

You might wonder how to identify Which w3wp.exe PID corresponds to which application pool ?

     1.   In IIS 6 (Windows 2003):

The easy way to solve this problem was to call a Visual Basic script file that was located in the C:\Windows\system32\ folder. The file was called iisapp.vbs, and the output of this command listed the instances of w3wp.exe present on the system, their PIDs and to which application pool they belonged to.

     2.   In IIS 7 (Windows 2008) and IIS 7.5 (Windows 2008 R2)

The Visual Basic script file has been removed. Most of the IIS 6 script files that were dedicated to a single task (like iisapp.vbs) have been integrated into a command line utility that can be used to administer the IIS server as a whole. This utility is called appcmd.exe and it is located in the C:\Windows\system32\intesrv\ folder.

CAUTION: to access the tool you must launch the command line prompt with administrator rights, otherwise it will not work. To do this simply right click on a command line short cut and chose 'Run as Administrator' from the context menu.

Then navigate to the folder where appcmd is located (C:\Windows\system32\intesrv\) and run the following command: 
appcmd list wp –
This will list all the instances of the w3wp.exe process (worker process) that are present on your server with their respective PIDs and application pools.

For example, lets say you have several w3wp.exes and one's PID is 6772. Type the following command.
appcmd list wp 6772
It will show what application pool it is belonged to.

You can always type iisreset from C:\Windows\system32\intesrv\ to reset and IIS and update the PIDs.

And that's a brief introduction about Visual Studio Attach to Process feature. Appreciate your feedback.

Happy Coding.

Regards,
Jaliya

No comments:

Post a Comment