Archive for the ‘debugging’ Category

Visual Studio 2010 – No Source available: Browse To Find Source

leave a comment »

Seems there’s a lot of confusion about this “Browse to find source” feature of visual studio 2010.

I had a colleague which was convinced he didn’t need pdbs to debug an .net app, because of the “Browse to find source” :) Anyone else want to take a bet with me on that?
In this post I want to clarify when to use this “Browse to find source” feature since the mdsn doc is missing, and sometimes is grayed out for no (apparent) reason.

If you experiment a little with process monitor, you’ll see that every time visual studio displays the “Browse to find source” page, it looks inside the pdb file corresponding to the top stacktrace instruction your are trying to step into.

For example in the image below, it looks into the pdb of the DateTime.Now (which is mscorlib.pdb), since I’m trying to step into DateTime.Now:
pdb load info

But what is it looking for there?
Well, if the pdb file is for a private build, then it includes information on where to find the source file from which it was built.
And that’s what visual studio is searching for inside the pdb.

For example I built the following simple library consisting of just one class:

    public class Class1
        public static DateTime GetCurrentDateTime()
            return DateTime.Now;

Class1.cs was located at: d:\ClassLibrary\Class1.cs

If you build this program and then look inside the pdb file that was generated for the build, using dia2dump, you’ll see, among other things, the Files section:


Compiland = ClassLibrary.Class1


Suppose I reference the ClassLibrary1.dll which I built, in another simple console project called Console1 located at D:\Console1:

    class Program
        static void Main(string[] args)
            var currentDateTime = ClassLibrary1.Class1.GetCurrentDateTime();

If I build Console1I will have in D:\Console1\bin\debug a Console1.exe, Console1.pdb, and a ClassLibrary1.dll,
If I try to step into the ClassLibrary1.Class1.GetCurrentDateTime() above, the first thing visual studio will do, is look for the ClassLibrary1.pdb at: D:\Console1\bin\debug\ClassLibrary1.pdb, and then in a couple of other locations.

If it doesn’t find the pdb in any of those locations, then you’ll get a disabled (grayed out) “Browse to source”. Why? Because it’s impossible for the visual studio debugger to know which function goes into what class and at what line number having just an binary and the source code files from which it was built. You need to understand that if you just have an .net executable(or any other assembly) and its source code visual studio doesn’t magically know how to match the instructions from the exe with their corresponding functions/line numbers from source files. That’s what pdbs help do. They store this “mapping”.
You’ll would never be able to do do source code level stepping inside the debugger if you have the source files but don’t have the pdb.
So the first step you do if you get “No source code available” is right click on the top call-stack instruction, choose “Symbol Load Information” and be sure you have a “Symbols loaded” message somewhere there. If you don’t see any “Symbols loaded”, then you should try placing the pdb at one of the locations which is listed there.

Now if visual studio finds the pdb file it might still be possible, using our above example, that the file ” D:\ClassLibrary\Class1.cs” which it finds in the pdb, is no longer there.
You either moved it at a another location, say C:\ClassLibrary, or it was built by someone else on another machine at D:\ClassLibrary\Class1.cs and you don’t even have that directory on your computer.
In those cases you get a nice browse to source file dialog:

If you cancel this accidentally (or intentionally) you will get a “Browse to source” enabled:

Why? Well because the pdb is OK, and contains source line information, just that the source files path inside pdb, are not at the right location and since you canceled “browse to source file”, visual studio has no idea where to look for the source files.
One more thing to look out for is that not all pdbs have source line number information: see for example this. Those are pdb’s which you cannot use for stepping into source code, since they don’t contain the necessary information.
In those cases, you’ll also get a grayed out “Browse to source”.
In that case, we’re out of luck my friend and the only available solution is to use Reflector Pro’s ability to debug and step into those assemblies from visual studio at runtime :(


Written by Liviu Trifoi

May 19, 2011 at 8:14 pm

Debugging .Net Framework source stepping problems using dia2dump

with 2 comments

So you are trying to step into .net framework source code.
You configured debugging options, triple-checked everything, consulted this comprehensive FAQ here: http://blogs.msdn.com/b/sburke/archive/2008/01/16/configuring-visual-studio-to-debug-net-framework-source-code.aspx.
Still nothing seems to work.

You are not alone :(
The most likely cause is that Microsoft has released some patches to the .net framework, and didn’t publish pdbs that you could use for stepping into .net framework source code.
So supposing that you are trying to debug DateTime.Now.
First look on where the pdb-file downloaded from Microsoft Symbol Server is located on your computer, by right clicking in the Call-Stack -> Symbol Load Information:

Now we’ll look inside this pdb file using dia2dump which is a tool that comes with visual studio at: c:\Program Files (x86)\Microsoft Visual Studio 10.0\DIA SDK\Samples\DIA2Dump\

It’s not compiled by default, so you’ll have to open it’s sln and compile it. Fortunately it’s just a small C++ proj, and compiles easily. (Just press compile).
After compiling you should see it at c:\Program Files (x86)\Microsoft Visual Studio 10.0\DIA SDK\Samples\DIA2Dump\Debug\.
To look inside a pdb file you just open a command prompt and write: dia2dump.exe xxx.pdb >xxx.txt . This will write the contents of the pdb inside a xxx.txt.

So if we run dia2dump on the mscorlib.pdb file which was downloaded we can see that it doesn’t contain any source files information or start /end addresses like the mscorlib.pdb you get from: http://referencesource.microsoft.com/netframework.aspx and it’s ~300 KB vs 10 MB.
Now I’m no pdb file contents expert, since the format of the pdb keeps changing and there’s no public specification according to microsoft (you can read more about it here: http://msdn.microsoft.com/en-us/library/x93ctkx8%28v=VS.80%29.aspx)
But the fact that there’s no addresses and source code mappings does ring a bell to me that the pdb is not one that we could use for .net framework source code stepping purposes.

So how do we make it work?
My first idea was of using that “Browse to source” option in visual studio 2010, and browse to .net framework source code you have downloaded manually from referencesource.microsoft.com. But after looking at the information inside the pdb, or rather the lack of it, it’s now clear why it’s always grayed out.

So one solution is to use Reflector Pro, which is commercial, and use it’s runtime debugging feature to step into microsoft .net framework source code.
This is not an ideal solution (especially because of the optimized code), and I just don’t understand why Microsoft doesn’t publish automatically source code according to their latest hotfixes/patches of the .net framework :(
I mean, I need to step into .net framework because I’m having problems today, not in two months when they decide to actually publish it. What am I supposed to do? Disable windows update so I don’t install any hotfixes? :(
This might be a second solution: Just have a virtual machine with visual studio installed, and a .net framework version which has .net framework source stepping support. Then disable any updates on that virtual machine and do your debugging there. (Really painfull, I know)

Written by Liviu Trifoi

May 17, 2011 at 8:06 pm

Silverlight stack trace line numbers

with 8 comments

As you probably know, to have stack traces line numbers in a .net app you need pdb files.
In Silverlight though, pdb files don’t get included in the XAP. Even if it was possible to include them you probably wouldn’t want them there because they tend to be huge and you want to have a fast loading web-site.

So what will you do when your client calls you and says he received an error message like the one below?
at ChaptersView.ChaptersTree_OnMouseMove
at ChaptersView.GetPreviousCommandButton

After I had a look today at the answer to this question, it became obvious:

Step 1)
In the place you now log or show exceptions to the user, you stop writing a normal stack trace using exception.ToString() or exception.StackTrace.
You should write a stacktrace that includes IL offsets using the code below:

catch (Exception exception)
            string errorDescription = string.Empty;
                StackTrace st = new System.Diagnostics.StackTrace(ex);
                string stackTrace = "";

                if (st != null)
                    foreach (StackFrame frame in st.GetFrames())
                        stackTrace = "at " + frame.GetMethod().Module.Name + "." + frame.GetMethod().ReflectedType.Name +
                                     "." + frame.GetMethod().Name + "  (IL offset: 0x" +
                                     frame.GetILOffset().ToString("x") + ")\n" + stackTrace;
                errorDescription = stackTrace;
            } catch //sometimes there are no stackframes, just use ex.ToString() in those cases
                errorDescription = ex.ToString();
            //do something with error description, like show it to the user or send it to your log server  

If we pass the exception that generated the stacktrace in our first example, it will look like:

at ChaptersView.ChaptersTree_OnMouseMove (IL offset: 0x44)
at ChaptersView.GetPreviousCommandButton (IL offset: 0x28)

Step 2: Client sends you an exception/log that includes IL offsets
You can use Reflector or IL Disassembler (which comes with visual studio) to find out the piece of code that generated the exception.
I prefer IL Disassembler because Reflector will only show you IL code from those IL offsets, and there’s a large chance you don’t read IL as well as you read C#.

So you have to perform the following steps:
1) Go to start menu and start IL Disassembler
2) Inside IL Disassembler go to File->Open and load the dll (not the XAP) where the exception was thrown. (e.g. …\Release\Client.dll)
3) Go to View->Show source lines
4) Now navigate to the last method that appears in the stacktrace and we look after the IL offset from the stacktrace.
In our example above this is ChaptersView.GetPreviousCommandButton and has offset 0x28

Inside IL Disassembler we will see a mix of C# code and corresponding IL code:

  IL_001c:  brtrue.s   IL_0029
//000193:             {
//000194:                 throw new NullReferenceException(“Test Exception”);
  IL_001e:  ldstr      "Test Exception"
  IL_0023:  newobj     instance void [mscorlib]System.Exception::.ctor(string)
  IL_0028:  throw
//000195:             }
//000197:             var previousCommandButton =

As you can see, at IL_0028 there’s the instruction that caused the exception.
This is IL code, but because we’ve checked “View->Show source lines” we also get the corresponding C# code just a few lines above.

//000193: {
//000194: throw new NullReferenceException();

So, in this case the exception was thrown because I’ve thrown it explicitly in order to show how IL offsets and IL Disassembler can be used to get an alternative to stack trace line numbers in silverlight.

Also please don’t just put that piece of code in every catch block. Create a method like “string GetStackTraceFromException(Exception ex)”
Another good idea would be to have some centralized error service, on the server side. You would then send those exception descriptions automatically along with method parameter values logged with PostSharp, whenever an error happens.
So by the time the client calls you, you have fixed the problem and he’s impressed ;)

Happy Debugging.

Written by Liviu Trifoi

April 21, 2011 at 6:15 pm

Using Process Monitor to debug applications

leave a comment »

Every once in a while there comes a bug that requires the use of Process Monitor
Process monitor is great but sometimes it is difficult to correlate its event stream with the source code you are debugging.

Using it for quite some time, I don’t really read it’s help anymore, but accidentally I’ve looked into it recently and was pleasantly surprised to see  that it now supports injecting debug messages into it’s event stream using an API and there’s even a managed wrapper for this (read diagnostic tracing process monitor)
A few of my colleagues asked me for scenarios in which to use it, so I’ll describe a few from my personal experience.

I mainly used it for debugging bugs that involve the file system or registry in some way.

Example 1:

I was maintaining a real-time legacy system that was doing a series of processing on xml files. Each processing involved creating  a temporary file in a certain directory. The problem was that sometimes certain files were lost.

Code looked similar to this:

string filename = string.Format(“{0}-{1}.xml”,
DateTime.Now.ToString(“yyyy-MM-dd-HH-mm”), Environment.TickCount);
filename = Path.Combine(dir, filename);
// synchronize access to file
lock (m_cs.MainAppDomain)
	using (StreamWriter sw = File.CreateText(filename))

Can you spot the problem? I stepped through the code many times, and I wasn’t able to reproduce the bug.

After using Process Monitor I was able to spot it: the unique filename made from the number of miliseconds since the system has started is not really unique. In some cases more than 1 file/milisecond can be created, in which case the second file overwrites the first one.

Example 2:

I had to do production debugging on a system accessible via Remote Desktop (via a special Remote Desktop infrastructure), but located behind an inaccessible subnet. So remote debugging was out of the question. It wouldn’t have helped anyway for this particular bug.

I had a piece of code that was installing an X.509 Certificate which was working under one Administrator user, but wasn’t working under another Administrator user.

The error I was receiving was “File not found exception” (the filename not being found was an empty string). Exception stacktrace was ending in an internal CAPICOM library (windows library).

At first I thought it was a security issue, so I enabled Windows security auditing but no security events were being logged, so this wasn’t a security issue.

So what I did was: take the output of process monitor when code was running fine and take the output of process monitor when the code was not working.

I compared these two and it revealed that, when it was not working, code didn’t found an AppData registry values in
HKEY_USERS\S-1-5-21-218976661-61539242-33973980-1010\Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders\

AppData normally points to the Application Data environment folder and should have been there. Seems somebody corrupted the registry, because lots of other shell folder values were missing too and is used by CAPICOM to locate the physical certificate store when installing a new certificate.

So I manually re-created the missing values by looking at a working system, and everything worked.

So registry corruption is another good scenario where you can use it.

Written by Liviu Trifoi

June 21, 2010 at 5:42 pm

AppDomain.Unload Garbage Collection

with 2 comments

It all started while I was investigating memory usage in a .net 2.0 system. The system was organized into independent components that were loaded into Application Domains.

The components performed their jobs in the isolated environment an AppDomain provides and, when their job finished, they were unloaded using AppDomain.Unload.

The problem was that memory didn’t seem to be recollected when I was doing AppDomain.Unload so I turned to read again sections from Jeffrey Richter’s, CLR via C# 3rd edition, since that was the book from which I’ve learned about Application Domains and Garbage Collection. It stated clearly “The CLR forces a garbage collection to occur, reclaiming the memory used by any objects that were created by the now unloaded AppDomain. The Finalize methods for these objects are called, giving the objects a chance to clean themselves up properly.”

After some mail exchange with Jeffrey, which you can read, along with an example here, he suggested:

A better test would be to look at some object addresses in the debugger and see if compaction actually occurs. If it does (and I suspect it does), then the collection count is just not being updated correctly.

To keep it short I had a small program that did this:

1) Create and application domain,

2) Create some object instances inside so It has some allocated memory.

2) Call AppDomain.Unload

3) Perform an explicit GC.Collect() call.

I took Visual Studio and loaded Son of Strike into it and it revealed this:

Before AppDomain.Unload:

!EEHeap -gc
Number of GC Heaps: 1
generation 0 starts at 0x0180b1f0
generation 1 starts at 0x017d100c
generation 2 starts at 0x017d1000
ephemeral segment allocation context: none
segment    begin allocated     size
017d0000 017d1000  01811ff4 0x00040ff4(266228)
Large object heap starts at 0x027d1000
segment    begin allocated     size
027d0000 027d1000  02f75470 0x007a4470(8012912)
Total Size  0x7e5464(8279140)
GC Heap Size  0x7e5464(8279140)

After AppDomain.Unload (same addresses, no heap compaction was done)

!EEHeap -gc
Number of GC Heaps: 1
generation 0 starts at 0x0180b1f0
generation 1 starts at 0x017d100c
generation 2 starts at 0x017d1000
ephemeral segment allocation context: none
segment    begin allocated     size
017d0000 017d1000  01811ff4 0x00040ff4(266228)
Large object heap starts at 0x027d1000
segment    begin allocated     size
027d0000 027d1000  02f75470 0x007a4470(8012912)
Total Size  0x7e5464(8279140)
GC Heap Size  0x7e5464(8279140)

After GC.Collect(), addresses differ indicating heap compaction was done.

!EEHeap -gc
Number of GC Heaps: 1
generation 0 starts at 0x01811234
generation 1 starts at 0x0180b1f0
generation 2 starts at 0x017d1000
ephemeral segment allocation context: none
segment    begin allocated     size
017d0000 017d1000  01811ff4 0x00040ff4(266228)
Large object heap starts at 0x027d1000
segment    begin allocated     size
027d0000 027d1000  027d3240 0x00002240(8768)
Total Size   0x43234(274996)
GC Heap Size   0x43234(274996)

So to conclusion is that heap compaction is not done and the only thing you can really be sure during an AppDomain unload is that objects will get to be marked as unreachable.

Memory for those objects will be reclaimed whenever the next garbage collection happens, so, in this case it’s not a good idea to call for GC.Collect() yourself.

Written by Liviu Trifoi

May 21, 2010 at 5:24 pm

Posted in .NET, AppDomain, C#, debugging

Tagged with