Title kinda says it all. The usual SOS command !bpmd doesn’t do a lot of good without a name.
Some ideas I had:
- dump every method, then use !bpmd -md when you find the corresponding MethodDesc
- not practical in real world usage, from what I can tell. Even if I wrote a macro to limit the dump to anonymous types/methods, there’s no obvious way to tell them apart.
- use Reflector to dump the MSIL name
- doesn’t help when dealing with dynamic assemblies and/or Reflection.Emit. Visual Studio’s inability to read local vars inside such scenarios is the whole reason I turned to Windbg in the first place…
- set the breakpoint in VS, wait for it to hit, then change to Windbg using the noninvasive trick
- attempting to detach from VS causes it to hang (along with the app). I think this is due to the fact that the managed debugger is a “soft” debugger via thread injection instead of a standard “hard” debugger. Or maybe it’s just a VS bug specific to Silverlight (would hardly be the first I’ve encountered).
- set a breakpoint on some other location known to call into the anonymous method, then single-step your way in
- my backup plan, though I’d rather not resort to it if this Q&A reveals a better way
The anonymous method isn’t really anonymous. It just hides behind a compiler generated name.
Consider this small example:
To find the return value, we need to find the name of the method implementation. To do that we need to locate the MethodDesc of the surrounding method. In this example it is
Main(), so:Via the MethodDesc we can dump the IL for
Main()Notice the funny looking names. They are the names of the generate delegate type and the actual method. The method is called
<Main>b__0. Let’s look at the method:There you have it. MethodDesc is 00153024 and as the comment say, you can use !bpmd to set the breakpoint using the MethodDesc.