Trouble displaying C# stack trace in WinDbg
c#, handle-leak, stack-trace, windbg
Solution
WinDbg attempts to interpret the native call stack as best it can, however to fully interpret the stack of a CLR application WinDbg needs to use an extension called SOS. This extension has a separate command `CLRStack` for viewing the stack information of CLR stacks. You will need to load the SOS extension first however using the `.loadby sos clr` command (or similar, I remember getting the correct version SOS to load could be a bit of a pain)
For more information see
- WinDbg 101–A step by step guide to finding a simple memory leak in your .Net application
- WinDbg / SOS Cheat Sheet
Problem
I have a handle leak in a C# program. I'm trying to diagnose it using WinDbg using !htrace, roughly as presented in this answer, but when I run !htrace -diff in WinDbg I'm presented with stack traces that don't show the names of my C# functions (or even my .net assembly). I created a small test program to illustrate my difficulty. This program does nothing except "leak" handles. ``` class Program { static List<Semaphore> handles = new List<Semaphore>(); static void Main(string[] args) { while (true) { Fun1(); Thread.Sleep(100); } } static void Fun1() { handles.Add(new Semaphore(0, 10)); } } ``` I compiled the assembly, and then in WinDbg I go "File" -> "Open Executable" and select my program (D:\Projects\Sandpit\bin\Debug\Sandpit.exe). I continue program execution, break it, and run "!htrace -enable", then continue for a bit longer, and then break and run "!htrace -diff". This is what I get: ``` 0:004> !htrace -enable Handle tracing enabled. Handle tracing information snapshot successfully taken. 0:004> g (1bd4.1c80): Break instruction exception - code 80000003 (first chance) eax=7ffda000 ebx=00000000 ecx=00000000 edx=77b2f17d esi=00000000 edi=00000000 eip=77ac410c esp=0403fc20 ebp=0403fc4c iopl=0 nv up ei pl zr na pe nc cs=001b ss=0023 ds=0023 es=0023 fs=003b gs=0000 efl=00000246 ntdll!DbgBreakPoint: 77ac410c cc int 3 0:004> !htrace -diff Handle tracing information snapshot successfully taken. 0xd new stack traces since the previous snapshot. Ignoring handles that were already closed... Outstanding handles opened since the previous snapshot: -------------------------------------- Handle = 0x00000250 - OPEN Thread ID = 0x00001b58, Process ID = 0x00001bd4 0x77ad5704: ntdll!ZwCreateSemaphore+0x0000000c 0x75dcdcf9: KERNELBASE!CreateSemaphoreExW+0x0000005e 0x75f5e359: KERNEL32!CreateSemaphoreW+0x0000001d *** WARNING: Unable to verify checksum for C:\Windows\assembly\NativeImages_v4.0.30319_32\System\13c079cdc1f4f4cb2f8f1b66c8642faa\System.ni.dll 0x65d7e805: System_ni+0x0020e805 0x65d47843: System_ni+0x001d7843 0x65d477ef: System_ni+0x001d77ef 0x004700c9: +0x004700c9 0x67d73dd2: clr!CallDescrWorkerInternal+0x00000034 0x67d9cf6d: clr!CallDescrWorkerWithHandler+0x0000006b 0x67d9d267: clr!MethodDescCallSite::CallTargetWorker+0x00000152 0x67eb10e0: clr!RunMain+0x000001aa 0x67eb1200: clr!Assembly::ExecuteMainMethod+0x00000124 -------------------------------------- Handle = 0x0000024c - OPEN Thread ID = 0x00001b58, Process ID = 0x00001bd4 0x77ad5704: ntdll!ZwCreateSemaphore+0x0000000c 0x75dcdcf9: KERNELBASE!CreateSemaphoreExW+0x0000005e 0x75f5e359: KERNEL32!CreateSemaphoreW+0x0000001d 0x65d7e805: System_ni+0x0020e805 0x65d47843: System_ni+0x001d7843 0x65d477ef: System_ni+0x001d77ef 0x004700c9: +0x004700c9 0x67d73dd2: clr!CallDescrWorkerInternal+0x00000034 0x67d9cf6d: clr!CallDescrWorkerWithHandler+0x0000006b 0x67d9d267: clr!MethodDescCallSite::CallTargetWorker+0x00000152 0x67eb10e0: clr!RunMain+0x000001aa 0x67eb1200: clr!Assembly::ExecuteMainMethod+0x00000124 ... -------------------------------------- Displayed 0xd stack traces for outstanding handles opened since the previous snapshot. ``` As you can see, the stack trace is missing my C# function names "Main" and "Fun1". I believe "System_ni+0x..." frames may be my function frames, but I don't know. My question is, how do I get WinDbg to display function names for my C# functions in the stack trace? Extra information: My WinDbg symbol search path is SRVC:/symbolshttp://msdl.microsoft.com/download/symbols;D:\Projects\Sandpit\bin\Debug;srv* I don't get any errors when I open the executable in WinDbg. There is a file called "Sandpit.pdb" in the output directory ("D:\Projects\Sandpit\bin\Debug"). The project is built locally so the pdb file should match the exe. I downloaded WinDbg from here. I have "Enable native code debugging" checked in the project settings in Visual Studio.