First of all, is it possible to view local variables in C# code in WinDbg.exe by adding SOS.dll?
I loaded the SOS.dll extension into WinDbg.exe by using .cordll -ve -u -l
command. The machine is x86. The result of the command is:
CLRDLL: Loaded DLL C:\Windows\Microsoft.NET\Framework\v4.0.30319\mscordacwks.dll
Automatically loaded SOS Extension
CLR DLL status: Loaded DLL C:\Windows\Microsoft.NET\Framework\v4.0.30319\mscordacwks.dll
I thought the SOS.dll has been loaded successfully. Then I want to insert a BreakPoint in the code. By testing the F9 seems not work. So I use the !bpmd command to insert a break point, like this:
0:004> !bpmd MyCode.exe Program.Main
PDB symbol for clr.dll not loaded
Found 1 methods in module 00714044...
MethodDesc = 00714d50
Setting breakpoint: bp 00760869 [Demo.Program.Main(System.String[])]
Adding pending breakpoints...
0:004> bl
1 e Disable Clear 00760869 0001 (0001) 0:****
But the result is:
0:004> g
eax=00000024 ebx=00000001 ecx=00eff73c edx=77592740 esi=00000000 edi=77621a20
eip=77592740 esp=00eff73c ebp=00eff750 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!KiFastSystemCallRet:
77592740 c3
Why the breakpoint is not hit?
Hope anyone could give me some help!
You didn't post the full debug session, but let me draw a few conclusions:
!bpmdworked immediately, which means the Main method was already jitted.MRE
In the following example, I am using this program:
Please provide such an example yourself next time, so we don't need to come up with an example ourselves.
I compile it in Release mode, AnyCPU with 32 bit preferred.
I am debugging with WinDbg Preview 1.2103.01004.0
How to break on Program.Main()?
Launch the application in WinDbg, so that it will stop at the initial breakpoint like this:
We need .NET SOS, so
And some time later, .NET is available
Get yourself a copy of the SOSEX extension and load it. It simplifies things.
Set a breakpoint at Program.Main:
Note the "pending JIT", which means
Main()has not been run yet. Let's wait for it:We are in
Main()!How to see local variables?
If you step throught the code with
!mtand!mgu, you'll reach the secondConsole.WriteLine()call:Have a look at local variables and parameters using
!clrstack -l -p:So: there is only 1 local variable with a value of 10. And the parameter is
a concatenated string made of "10" and the name of the object.
Try this in debug mode and you'll see non-optimized version of variables.