Archive for May 2011

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

Silverlight IsolatedStorageFile.IncreaseQuotaTo

leave a comment »

I had a lot of troubles using IsolatedStorageFile.IncreaseQuotaTo

Msdn documentation for IsolatedStorageFile.IncreaseQuotaTo states that:
To increase the quota, you must call this method from a user-initiated event, such as in an event handler for a button-click event. When you call the IncreaseQuotaTo method, the common language runtime in Silverlight presents a dialog box for the user to approve the request. If the user declines the request, this method returns false and the quota remains the same size.

Now that seems like half-baked documentation to me since it doesn’t accurately describe what a user-initiated event is, it only gives one example.
After much digging, I did find out what a user initiated event is. Seems that the msdn documentation specifies what a user initiated event in the section related to “events overview“, but there’s no link between documentation of IsolatedStorageFile.IncreaseQuotaTo and Events Overview.
So here goes the definition:

Silverlight user-initiated events include the mouse events (such as MouseLeftButtonDown), and the keyboard events (such as KeyDown). Events of controls that are based on such events (such as Click) are also considered user-initiated.

API calls that require user initiation should be called as soon as possible in an event handler. This is because the Silverlight user initiation concept also requires that the calls occur within a certain time window after the event occurrence. In Silverlight 4, this time window is approximately one second.

User-initiated event restrictions also apply to usages of JavaScript API for Silverlight.

When Silverlight is in full-screen mode, some input events are deliberately limited for security reasons, although this can be mitigated for out-of-browser applications using elevated trust. For more information, see Full-Screen Support.

At first these limitations seemed draconian to me.
Thinking it more about it, I guess Microsoft didn’t want a user to have many tabs open in a browser and then poof: I call automatically IncreaseQuotaTo.
Since the IncreaseQuotaTo is a modal dialog, meaning you can’t navigate to other tabs from it, if the user is now on page google.com, and I show automatically IncreaseQuotaTo, the user might think that google.com is asking for more storage :)). This would be a security breach indeed.

Had they implemented this with a page level dialog, then that would have probably been more easily hacked (worked around). I can already start imagining drawing some evil forged “Silverlight needs to be updated” image over the IncreaseQuotaTo text, hence tricking the user in pressing Yes . Or maybe some other evil scenario.

I was curios though how exactly does silverlight know what a user initiated event is, but after digging through .net framework source code I’ve got to a dead end:

if ((browserService == null) || !browserService.InPrivateMode())
return false; //means that IncreaseQuota will fail
where browser.IsInPrivateMode is:

public bool InPrivateMode()
    bool privateMode = false;
    return (NativeMethods.SUCCEEDED(UnsafeNativeMethods.DOM_InPrivateMode(this._browserServiceHandle, out privateMode)) && privateMode);

where DOM_InPrivateMode is in a DllImport[“agcore”], which according to microsoft is confidential :(
So it looks like I won’t find out soon how they’re detecting user initiated events, although I’m guessing they have some centralized private method that detects clicks for example, and then probably sets a flag that this was indeed “a user initiated event”, and since you can’t forge clicks or keypresses using javascript and since you can’t call those private methods using reflection, it’s “safe”.

Written by Liviu Trifoi

May 18, 2011 at 6:33 pm

Posted in .NET, C#, Silverlight

Tagged with , , ,

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