In ClrProfiler, how to get managed object from ObjectID

423 Views Asked by At

I am building a .Net Profiler for some custom requirement where I need to capture the exception details even though it got handled properly in the code. To do so-

  • I have implemented ICorProfilerCallback
  • SetEventsMask for COR_PRF_MONITOR_EXCEPTIONS
  • Implemented the ExceptionThrown callback

So far so good, I am getting callback for every exception being thrown. However, it gives OjbectID that is a pointer to the actual exception object. I want more details like the message, call stack, etc. about the exception.

How do I get object details from ObjectID?

1

There are 1 best solutions below

4
On

As @HansPassant mentioned, what you are doing smells like a debugger feature more than a profiler. However, you can do what you want using the profiler if that is a hard requirement - I believe IL re-writing is possible via the ICorDebug interfaces, but I am a profiler dev and haven't used the debugger interfaces as much.

David Broman's blog has a great description of taking a managed stack walk using the ICorProfilerInfo2 interface. In order to get the native parts you need to do a lot more work.

Navigating the object instance is also done via the ICorProfilerInfo2 interface.

  1. Get the class ID of the object using ICorProfilerInfo::GetClassFromObject()

  2. Using the class ID get the class layout via ICorProfilerInfo2::GetClassLayout()

    • You will need to parse the object metadata to determine which field you want.
  3. Index into the object to grab the desired data.