Getting the method name of a Delegate instance #35

JeffCyr opened this Issue Dec 3, 2015 · 1 comment


None yet

2 participants

JeffCyr commented Dec 3, 2015

It's difficult to find the corresponding method of a Delegate instance while debugging with clrmd, sos have the same limitation.

This blog explained how to do it with windbg + sos:

I translated that to ClrMD and it work with instance delegate (I think it won't work with static methods).

ulong magicPtr = ...// Delegate._methodPtr + 5;
ulong magicValue1, magicValue2;
session.Heap.ReadPointer(magicPtr+1, out magicValue1);
session.Heap.ReadPointer(magicPtr+2, out magicValue2);

ulong mysticPtr = magicPtr + 8 * (magicValue2 & 0xFF) + 3;
ulong mysticOffset = 8 * (magicValue1 & 0xFF);

ulong mysticValue;
session.Runtime.ReadPointer(mysticPtr, out mysticValue);
ulong methodDescriptorPtr = mysticValue + mysticOffset;

return (string)session.Runtime.GetType().GetMethod("GetNameForMD", BindingFlags.Instance | BindingFlags.NonPublic).Invoke(session.Runtime, new object[] { methodDescriptorPtr });

This is very hacky and does not work with all Delegate instance, do you think a proper implementation should be implemented in ClrMD?

leculver commented Dec 3, 2015

I agree there should be a method in ClrMD to do the dirty work for you. I'll add that to the todo list.

Also, your last line of code you should be able to replace with this:

ClrMethod method = clrRuntime.GetMethodByHandle(methodDescriptorPtr);
return method.Name;  // or return the method itself

As for it not working on all instances...sometimes _methodPtrAux is set and it means something slightly different (I think it's the MethodDesc itself but that's based on my fuzzy memory). If you handle that case then you should be able to handle all delegate types.

In any case I agree this should be part of ClrMD itself so I'll try to get to it next time I have a chance to work on ClrMD (probably a big effort the last week of December).

@fremag fremag referenced this issue in fremag/MemoScope.Net Mar 10, 2016

Delegates: display targets: type, method signature #84

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment