none
.NET Profiler enter/leave function hooks does not get called in case of exception RRS feed

  • Question

  • I am building a .Net Profiler for some custom requirement.

    I want to hook at Enter and Leave for some specific methods. To achieve this, I have tried below two approaches.

    1. IL Rewrite - I am able to inject the custom code at both the places. It is successfully getting injected and calling the custom code. I am also able to get the input arguments, 'this' and return value. Injecting at the Enter is not much difficult. However, it's complex to inject at Leave as there could be return at multiple places in a method. I have to inject the code at every place where return statement is written.

    It's bit complex but somewhat doable. But, if there any exception, the execution is not reaching to return statement and hence my injected code is not getting invoked.

    2. Subscribe to Enter/Leave through SetEnterLeaveFunctionHooks2 in ICorProfilerInfo2 as per the sample code given at https://www.codeproject.com/Articles/15410/Creating-a-Custom-NET-Profiler.

    In both the cases, hook at the Leave is not getting invoked in case of exception in the method.

    How to handle this? I want a return value in all the scenarios. In case of an exception, I should know there is an exception; I will consider as 'No return value'. Probably, I may need exception details as well.

    Below is a sample method. I want to hook at Enter and Leave for GetString method. It has multiple returns. I am able to capture the return value in a normal scenario. But in case of an exception, execution stops immediately and due to that, the hook at return is not getting invoked.

        public int GetInt()
        {
            //int retVal = 10;
            int retVal = 1010;
            //throw new Exception("test");
            return retVal;
        }
    
        public string GetString()
        {
            var retunValue = "Return string ";
    
            if (GetInt() > 100)
            {
                retunValue += " inside IF > 100";
                return retunValue;
            }
    
            return retunValue + " at last return";
        }
    

    Friday, September 22, 2017 5:22 AM

All replies

  • Option 1:

    If you use IL rewriting, you could add a try-finally block (or try-catch-throw). Hovever, this will exacerbate the complexity as you cant return from within a try block and will need to replace the return instructions with leave instructions and (potentially) a local variable.

    Option 2:

    Pass COR_PRF_MONITOR_EXCEPTIONS into your call to SetEventMask and listen on the ExceptionUnwindFunctionLeave callback. This of course has its own issues, the unwind callbacks don't provide exception details. You could try tracking the exception from the ExceptionThrown callback, but this can result in misleading information if the exception leaves a filter block (the runtime will consider the filter to have returned false and continue with the previous exception, but IIRC, there is no callback to indicate that this has happened.)

    Friday, September 22, 2017 11:33 AM
  • I am building a .Net Profiler for some custom requirement where I need to inject the code at the start of a method and end of a method. I want to inject the code into few specific methods, not all. And it will be running on the production environment. As per my knowledge, I can do it using one of the following method -

    1. IL Rewrite
    2. SetEnterLeaveFunctionHooks

    With respect to IL Rewrite, it is the same as just adding additional method call in the code, no performance impact.

    In case of hooking, COR Profiling API will hook into all the CLR Methods. However, I can check whether the profiled method is the interested method or not, and call hooked code if so. But, it will have the performance impact as I want to hook only few methods and CLR will callback for each method.

    I can use SetFunctionIDMapper to filter the callback for only interesting methods. But, does it the same as I check using if condition? Will it have any performance impact? Or it will be more or less same (with respect to performance) as IL Rewrite?

    Tuesday, September 26, 2017 1:19 AM
  • Hi HiteshDK,

    It is similar with the the thread you post before. I will merge it.

    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.

    Tuesday, September 26, 2017 7:21 AM
    Moderator
  • Hi HiteshDK,

    To further help you about this issue, I am trying to invoke someone experienced to help look into this thread, this may take some time and as soon as we get any result, we will post back.

    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.

    Tuesday, September 26, 2017 7:33 AM
    Moderator
  • Hi Wendy,

    I posted the separate question as I want to understand which approach is better or there is any other better approach.

    Regards,

    Hitesh

    Wednesday, September 27, 2017 5:36 AM
  • Dear HiteshDK,

    Thanks for your information.

    Actually, if you use SetFunctionIDMapper to filter the callback for only interesting methods, I think it would be some performance impact due to FunctionIDMapper mechanism. I suggest you use IL rewriting in your scenario.

    Some FunctionIDMapper document for your reference: https://blogs.msdn.microsoft.com/davbr/2007/03/22/enter-leave-tailcall-hooks-part-1-the-basics/ 

    By the way, Your question falls into a category which requires a more in-depth level of support.  Please visit the below link to see the various free and paid support options that are available to better meet your needs. http://support.microsoft.com/default.aspx?id=fh;en-us;offerprophone

    Regards,

    Tianmao


    Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread.

    Thursday, October 12, 2017 6:23 AM