none
How to get managed object from ObjectID RRS feed

  • Question

  • 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 a 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?


    Hitesh

    Thursday, October 5, 2017 12:55 PM

All replies

  • I'm afraid your out of luck.  If the properties were simply wrapping fields, then you could use GetClassLayout to find the offset where you can read the field value (though this would be fragile if the field is not externally visible). Unfortunately, both the StackTrace and Message properties involve calculations; and to make things worse, the Message property is virtual and overridden by some exception types.

    If you really need the message and stack trace, then you will need to inject IL somewhere that has access to the exception to call the property accessor methods and pass the values to your profiler using something like pinvoke or a calli opcode. (you can't call the methods from within your callback)

    Saturday, October 7, 2017 1:48 AM
  • If you do not insist on a profiler you can get the exception message, type, call stack and in which method it was catched with ETW.

    See

    https://github.com/Microsoft/dotnet-samples/blob/master/Microsoft.Diagnostics.Tracing/TraceEvent/TraceEvent/41_TraceLogMonitor.cs

    for a sample. This works for all processes system wide without the need to attach to anything. If your custom requirements are not needed much further this is much easier than to write your own profiler.

    Yours,

      Alois Kraus

    Saturday, October 7, 2017 6:20 PM
  • Thanks Alois for the response and providing completely new approach. However, since there are many applications, I don't have the luxury to modify the code. I have to attach the process and collect the all the exceptions are being thrown, even though it is handled properly. 

    Hitesh

    Tuesday, October 10, 2017 5:47 AM
  • Thanks Brian for the response.

    I don't have any specific place where I can inject the code as the requirement is to do it application wide. I want a notification for an exception that being thrown anywhere in the application, even though it gets handled properly. 


    Hitesh

    Tuesday, October 10, 2017 5:55 AM
  • Hi Hitesh Kesharia,

    If you want to call stack for custom .net profiler, the profiling API provides two ways to obtain call stacks: a stack snapshot method, which enables sparse gathering of call stacks, and a shadow stack method, which tracks the call stack at every instant.

    For mode details, please refer to the following link.

    https://docs.microsoft.com/en-us/dotnet/framework/unmanaged-api/profiling/profiling-overview

    And you could download the source file of custom .net profiler from code project for reference.

    https://www.codeproject.com/Articles/15410/Creating-a-Custom-NET-Profiler

    Best Regards,

    Wendy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, October 12, 2017 8:02 AM
    Moderator