none
.Net Profiler - Getting the uncaught Error details and stack trace. RRS feed

  • Question

  • Hi,

    I am developing a .Net profiler using instrumentation by setting necessary event masks for the even callbacks and injecting my helper functions.

    I am able to get the call graphs and time taken of the functions in a web request. And also if the code has caught exceptions i am able to get that using "COR_PRF_MONITOR_EXCEPTIONS" . 

    But if there are any uncaught exceptions in request how to get the details of it. (the details that will be displayed on the browser )

    How to get the snapshot and reason of the error.? Is there any particular function in .Net that i should instrument and analyse it's parameters to get the details of the exceptions thrown?

    Example: I can get the DB connection name from the parameter of SqlCommand:Open(..) method. Likewise is there any particular function i should instrument into? 

    Or can some one tell how to get the error details from the call backs that "ICorProfilerCallback*" provides ?  like ExceptionThrown,ExceptionSearch* callbacks..

    ./Selva


    • Edited by Selva VS Monday, July 20, 2015 12:31 PM
    Monday, July 20, 2015 12:29 PM

Answers

  • If the exception was unhanded, then I would expect the thread/process to be terminated. If the exception detail is being sent to a browser, I would expect that it is being caught somewhere and you could instrument the catch handler.

    I don't believe there is a direct callback for when an exception is unhanded, you might be able track exception objects that have been thrown and not caught, then recording them if they are collected. This, however, will probably:

    • Cause false positives when an exception crosses an AppDomain boundary (as it gets serialised/deserialised for the transition - you might be able to mitigate this with the remoting callbacks, but probably not worth it).
    • Cause false negatives if something holds a reference to the exception (IIRC, the runtime pre-allocates the StackOverflowException and OutOfMemoryException, holds a static reference to them and re-uses them.)

    A better option might be to instrument the code that raises the AppDomain.UnhandledException event (some scheduling mechanisms will have their own equivalent)

    If you can't instrument the code that catches the exception (maybe it's unmanaged) then your options become limited. You can get the type of exception from the exception object (GetClassFromObject & GetClassIDInfo2) and you can get the stack trace at the time the exception is thrown (by using DoStackSnapshot or by maintaining a shadow stack) but you wont be able to (reliably) get the exception message since Exception.Message is virtual and some exception types will override it to return slightly modified messages.

    Monday, July 20, 2015 10:12 PM
  • Hi Brian,

    Why cant we do as below,

    HttpContext.Current.ApplicationInstance.Error += ErrorHandlerFunc;

    I am doing this inside my helper function which i Inject from the profiler. 

    Inside the "ErrorHandlerFunc" function I will take the exception object and take the stack-trace..!

    This works perfectly for ASP.NET applications but for MVC application I don't know how to register for error events.

    Couldn't find any clue for doing the same in MVC apps.

    ./Selva


    • Marked as answer by Selva VS Tuesday, July 28, 2015 11:26 AM
    Thursday, July 23, 2015 5:26 AM

All replies

  • If the exception was unhanded, then I would expect the thread/process to be terminated. If the exception detail is being sent to a browser, I would expect that it is being caught somewhere and you could instrument the catch handler.

    I don't believe there is a direct callback for when an exception is unhanded, you might be able track exception objects that have been thrown and not caught, then recording them if they are collected. This, however, will probably:

    • Cause false positives when an exception crosses an AppDomain boundary (as it gets serialised/deserialised for the transition - you might be able to mitigate this with the remoting callbacks, but probably not worth it).
    • Cause false negatives if something holds a reference to the exception (IIRC, the runtime pre-allocates the StackOverflowException and OutOfMemoryException, holds a static reference to them and re-uses them.)

    A better option might be to instrument the code that raises the AppDomain.UnhandledException event (some scheduling mechanisms will have their own equivalent)

    If you can't instrument the code that catches the exception (maybe it's unmanaged) then your options become limited. You can get the type of exception from the exception object (GetClassFromObject & GetClassIDInfo2) and you can get the stack trace at the time the exception is thrown (by using DoStackSnapshot or by maintaining a shadow stack) but you wont be able to (reliably) get the exception message since Exception.Message is virtual and some exception types will override it to return slightly modified messages.

    Monday, July 20, 2015 10:12 PM
  • Hi Brian,

    Why cant we do as below,

    HttpContext.Current.ApplicationInstance.Error += ErrorHandlerFunc;

    I am doing this inside my helper function which i Inject from the profiler. 

    Inside the "ErrorHandlerFunc" function I will take the exception object and take the stack-trace..!

    This works perfectly for ASP.NET applications but for MVC application I don't know how to register for error events.

    Couldn't find any clue for doing the same in MVC apps.

    ./Selva


    • Marked as answer by Selva VS Tuesday, July 28, 2015 11:26 AM
    Thursday, July 23, 2015 5:26 AM
  • Sure, as long as you attach the handler from managed code and don't call any managed code from any callback provided by the profiler, it should be fine.

    I'm not familiar with this MVC thing you mentioned so I can't really say what exception handling mechanisms it has.

    Thursday, July 23, 2015 8:50 AM
  • I did a quick google search for "asp.net mvc exception handling" and encountered an article on how MVC handles exceptions.

    By the sounds of things, you could hook HttpApplication.Error in the same way you do for asp. Though, depending on how you choose to define 'unhandled', you may want to also investigate the other mechanisms so that you can get notification even when someone else decides to add a more specific handler.

    Of course, I have no experience with MVC and no means to test any of this, so you should consider it wild speculation.

    Friday, July 24, 2015 11:18 PM