Tuesday, July 31, 2007

Debugging Tips in C#

One of the most time consuming thing in developer’s life is debugging. Here are few notes that can help when it comes to debugging. The first thing that one should understand is pdb files

Importance of pdb files

As per MSDN, “a program database (PDB) file holds debugging and project state information that allows incremental linking of a debug configuration of your program.” Please note that pdb files are not specific to C#, they are available for .Net languages as well

There are lots of things about the pdb files, but to keep it short, pdb files enables debugging of the code. Visual Studio uses the pdb files when you start debugging a program. The yellow highlighting of the code lines that we see when we are debugging is due to the information stored in the debug file.

Now let’s take a look at few common debugging related issues

My yellow highlighting does not match the code line in length

When you are debugging a complex application, which is build on one machine and is being debugged on a different machine, you might find that the yellow highlighting is not matching the code line’s length. This is due to the mismatch between the code files and the deployed binaries.

To fix this issues, rebuild the whole set of projects, using the code files which you intend to use for the purpose of debugging. Now redeploy the newly built binaries along with the pdbs to the deploy location. If it involves GACing, copy the pdbs to GAC as well. Open a new instance of CLR debugger or visual studio on the machine where the binaries were deployed and open the code files, which you used to compile the deployed build, in it. Now hook up the debugger and put the break points, you will see that the length of yellow highlight is equal to the code line length

How do I debug a windows service?

  1. Open a new Debugger(CLR or studio).
  2. Click on the Tools menu, and click on “Attach to Process” menu item
  3. You will get a list of processes that are running on the local machine
  4. Select the process that matches the name of your windows service and click on attach.

Note that this is a general way of debugging any running process. Also note that you cannot debug an application whose type is not managed

How do I Debug a Windows Service which always fails before starting?

A windows Service will not fail to initialize unless there are unhandled exceptions in one the following three places

  1. Main function
  2. Constructor
  3. OnStart() method

What you can do is put a Debug.Assert(false); as the first line in your main function of the windows service and redeploy it. Now as you try and start the win service, you will get and empty Assert Window. Don’t dismiss it. Open up a debugger and hook up into the service. Put the break points at the suspect places and then dismiss the assert by ignoring it. The control will break at the breakpoint. Debug your service from here on

How do I debug an installer?

Debugging an installer is similar to debugging the windows service. Put the Debug.Assert where you want to start debugging and attach to the msiexec.exe process.

How do I debug an application hosted in IIS?

Hook up to aspnet_wp.exe in xp or w3wp.exe in Windows 2003 server. Haven’t taken a look at the process name under vista.


rajesh said...

Nice tips. It comes only from experience, but posts like this can encourage new developers to go deep in to debugging.
Thanks dhawal. U rock..

Sudheer Kumar said...

Thank Mr.Dhawal,

This post is very useful.

When you want to debug a windows service using visual studio 2005
1. Go to Debug
2. Select Attach to Process.
3. It opens a dialogue box, select Show Process from all users. (It shows all the available process)
4. Then select the process.

Player said...

Nice blog man. Keep posting such techie blogs.