Monday, June 16, 2014

Remote Debugging with Visual Studio

Problem

In a normal software development process, when we are done with our software development, we deploy our application to the test environment for system integration testing (SIT) and then user acceptance testing (UAT). Sometime tester found a bug in the SIT or UAT environment, but the bug does not happen in our local machine. And then we would give a typical developer response: "I don't know, it works on my machine.".

The challenge here is due to the environment difference, it is difficult to trace the bug root cause. We can either guess or use trial and error method to fix the bug, otherwise, install Visual Studio into the server and then step into the code. But, we should not install any IDE tool or copy the source code to the server for debug.

Solution

So, today topic is how to remote debugging with Visual Studio. By using remote debugger, developer can remote attach the process in the SIT server and then step into the code from the Visual Studio in local machine.

Tool Setup

Remote Debugger exist since Visual Studio 2003. First, you need to download the Remote Debugger installer base on your Visual Studio version. For my case, I am using Visual Studio 2013, so I need to download Remote Debugger for Visual Studio 2013 from HERE. And then, install it on the server.

Application Deployment

First, compile your source code in Debug mode. Deploy all the compiled assemblies and generated symbol files to the server. The symbol files are those with .pdb extension, it also known as program database, those file contains the debug information that you need to step in to code. These files are normally generated at the same "bin" folder with your dlls.

Server Side Setup

Remote Debugger can be run as a program or windows service. If you prefer to turn on the remote debugger whenever you need it only, then run the Remote Debugger like running a normal program. Otherwise, run Remote Debugger Configuration Wizard to install the Remote Debugger as a windows service.


In today topic, I only cover about running Remote Debugger as a program. So, for the first time, when you run the Remote Debugger, you will be prompted for unblocking some firewall to allow remote debugging.


Just click the Configure remote debugging button to proceed.


The Visual Studio Remote Debugging Monitor must be running all time whenever you need to do remote debugging. Take a look at the description, it actually tells you the port number that it used to listen. You need to connect to that server name and port from Visual Studio.

You can change the port if the default port 4018 is being used by other program by opening the Options from the Tools menu.


And then, by default, Remote Debugger uses Windows Authentication mode. Make sure you have given permission to the account that you use to run Visual Studio. Click the Permissions button and then give the Debug privilege to the account that you want.

Local Machine Setup

Now, open Visual Studio and then open your project, then put some breakpoints in your code. Click the Debug menu, then open the Attach to Process window.



At the Qualifier field, enter the server IP address and port, then click the Find button.


A list of processes on the server will be populated. Now, you can find your process and then click the Attach button.

If you are running a windows application, windows service or console application on your server, you can straight away look for the executable to attach. If you are hosting an ASP.NET application or WCF web service, then you should look for IIS worker process (w3wp.exe) to attach. For IIS worker process, it is recommended to configure running under an application pool identity. So that it is easier to detect and attach the correct IIS worker process.

Once you click the Attach button, the Remote Debugger will show you or someone has connected to it.


Now, you can start playing with your application to purposely reproduce the bug. The breakpoint that you put in your code in Visual Studio will be hit. From there you can debug your code base on the assemblies that running on your server.



Happy debugging!


No comments:

Post a Comment

Send Transactional SMS with API

This post cover how to send transactional SMS using the Alibaba Cloud Short Message Service API. Transactional SMS usually come with One Tim...