This time, I would like to share some notes gathered from different sources about debugging: how Visual Studio handles different builds (Debug and Release) and how we can debug code that has been deployed to a remote machine.
Debug, Release Build
Like most of you know, Visual Studio projects have some predefined build configurations: Debug and Release. Using Debug, the program is compiled with full symbolic debug information and no optimizations.
With Release, the program is compiled using Optimize code and pdb-only options. The pdb-only option does not generate the DebuggableAttribute that tells the JIT compiler that debug information is available, but generates a .pdb (program database) file to allow viewing information such as source filenames and line numbers in the application’s stacktrace.
Optimized code is harder to debug since the compiler repositions and reorganizes instructions to get a more efficient compiled code, so generated instructions may not correspond directly to the source code. Some optimizations are always performed by the compiler and others only performed when the Optimized code option is set; optimization may include things like: constant propagation, dead code elimination. .NET runs optimizations in 2 steps: one optimization run by the compiler when generating the IL code and other when running the application and transforming IL to machine code, most optimizations are left to the JIT compiler.
While reviewing more about it, it seems that optimization is separate from pdb generation so pdb-only generation shouldn't affect performance in most scenarios and it is recommended to produce PDB files, even if you don't want to ship them with the executable.
In order to debug an application with Visual Studio, we require the matching pdb file: The debugger looks for the PDB files using the DLL or executable name and looks for [applicationname].pdb. When .dll, executable, and PDB files are generated, it creates and stores identical
GUIDs in each file. The
GUID is used to determine if a given PDB file matches a DLL or an executable file.
You can use DUMPBIN to see if PDB files are found for a given DLL, for example:
e:\Program Files (x86)\Microsoft Visual Studio 9.0\VC>dumpbin /pdbpath:verbose
Microsoft (R) COFF/PE Dumper Version 9.00.30729.01
Copyright (C) Microsoft Corporation. All rights reserved.
Dump of file
File Type: EXECUTABLE IMAGE
PDB file found at
e:\Program Files (x86)\Microsoft Visual Studio 9.0\VC>
and GUID information can be inspected using DUMPBIN as well:
e:\Program Files (x86)\Microsoft Visual Studio 9.0\VC>dumpbin /headers
Dump of file
Time Type Size RVA Pointer
-------- ------ -------- -------- --------
4D97EE82 cv 6C 00003708 1908 Format: RSDS,
Another interesting feature around .NET debugging is configuring an executable image for debugging, if you want to debug [application].exe, create a text file named [application].ini, in the same folder having this information:
[.NET Framework Debugging Control]
This tells JIT compiler to generate tracking information and not run optimizations, making it possible for the debugger to match up MSIL and not optimizing resulting machine code.
To remote debug a .NET application, you can use the Visual Studio remote debugger (msvsmon.exe),
Both machines should have access/permissions to connect to each other. Start Visual Studio remote debugger on the machine running the application, this will start Visual Studio remote debugging monitor displaying the following information.
Msvsmon started a new server named 'Domain\user@machinename'. Waiting for new connections.
On the machine you want to debug, start Visual Studio and on the attach to process windows, set Qualifier to the remote debugger server name. In this example, 'Domain\user@machinename' use the name as it appears.