Arun Satyarth
Arun Satyarth

Reputation: 454

Can we do reflection on debugee from debugger in .Net

I want to get the native(x86) code of a debugee function from the debugger using ICorDebug APIs. After getting an ICorDebugFunction, I can call GetNativeCode on it but it returns the native code only if it has been Jited. So I need to forcefully JIt it. The RuntimeHelpers.PrepareMethod can do that but it needs a methodhandle( not a method token).From the debugger I have the Method Token but not the method handle. So can we get the real MethodInfo object(which can get me the handle) of a debugee function from the debugger? In other words, is it possible to do reflection on debugee from debugger?

Upvotes: 1

Views: 137

Answers (1)

Brian Reichle
Brian Reichle

Reputation: 2856

Not exactly.

You can use IMetadataImport et. al. to get the static information and some dynamic information is available directly through the ICorDebug api, but any other sort of reflection would need to be run on the debuggee (which would normally use ICorDebugeEval).

If you don't mind using ICorDebugEval to execute your reflection on the debuggee, you could use the following sequence.

  1. ICorDebugAppDomain::GetObject to get an ICorDebugValue representing the AppDomain object in the debuggee.
  2. AppDomain.GetAssemblies() and Assembly.Module to find the Module object.
  3. Pass the metadata token into Module.ResolveMember(int) to get the MethodBase.
  4. Use MethodBase.MethodHandle to get your runtime handle.

Depending on your use case, it might be beneficial to combine last 3 steps with your call to PrepareMethod into a helper method in a separate assembly that you have the debugge load.

Upvotes: 3

Related Questions