Debugging .NET exceptions *without* VS installed using WinDbg
If your .NET app is crashing on a machine that doesn’t have Visual Studio, here’s the easy way to get the managed exception - people have written way better articles on SOS and WinDbg, but here’s a 10sec overview; even if you have no idea how WinDbg works, often just having the managed exception will give you enough good info, and WinDbg is way easier to install and lightweight than VS.
Install the Debugging Tool for Windows, then spin up WinDbg (or cdb, the command-line version if you’re even more metal). Either attach to a running instance of your program, or start a new one. Run these commands to get started once you get the prompt (if you don’t get the prompt, and you’ve attached, you need to break-in by hitting Ctrl-C):
.sympath srv*http://msdl.microsoft.com/download/symbols
.sympath+ C:\path\to\your\binary\directory
.reload -f
g
Ok, your app is running - one thing that will totally fool you if you’re used to using VS is, if you start the program with WinDbg, your program will break-in immediately once you start it, before any code in your app has run (even the .NET startup code). Now, crash your app; you should’ve been kicked back into the debugger now and back at the prompt, with something like:
CLR exception type: System.Collections.Generic.KeyNotFoundException
"The given key was not present in the dictionary."
eax=001fe078 ebx=e0434f4d ecx=00000001 edx=00000000 esi=001fe100 edi=005461d8
eip=76c5f35f esp=001fe078 ebp=001fe0c8 iopl=0 nv up ei pl nz ac pe nc
cs=0023 ss=002b ds=002b es=002b fs=0053 gs=002b efl=00000216
KERNEL32!RaiseException+0×58:
76c5f35f c9 leave
0:000>
Now, here’s where we get to the real work - to debug .NET framework apps, we need the SOS extension, so we’ll pull that in, then use one of the functions that SOS defines (to see them all, run !help):
0:000> .loadby sos mscorwks
0:000> !printexception
*** ERROR: Symbol file could not be found. Defaulted to export symbols for C:\Windows\Microsoft.NET\Framework\v2.0.50727\mscorwks.dll -
PDB symbol for mscorwks.dll not loaded
Exception object: 22e0580c
Exception type: System.Collections.Generic.KeyNotFoundException
Message: The given key was not present in the dictionary.
InnerException: [none]
StackTrace (generated):
SP IP Function
001FE1F4 6BFB480D mscorlib_ni!System.ThrowHelper.ThrowKeyNotFoundException()+0×1d
001FE1FC 06DF1E20 mscorlib_ni!System.Collections.Generic.Dictionary`2[[LogDebugger.EventGroupingKeys, LogDebugger.Engine],[System.__Canon, mscorlib]].get_Item(LogDebugger.EventGroupingKeys)+0×38
001FE208 06494B36 LogDebuggerUI!LogDebugger.Ui.FilterEnvironment.[buildhighlightbrushmap]b__e(LogDebugger.LDEvent)+0xe
[ [ rest of stack trace… ] ]
There’s way more you can do with WinDbg and SOS (it’s the way we debug Windows programs at Microsoft), but this should get anyone who wants a quick way to see an exception the way to do it. Using Mono, you can actually get the exception trace even easier (but you can’t do any debugging), as any app you run via the command line (i.e. via mono ./path/to/app.exe) will print the same information to STDERR.

Entries RSS