none
Getting class objects using ICoreprofiler RRS feed

  • Question

  • Hi, 

    I am trying to get the parent class object of the called function using ICoreprofilerCallback/ICoreprofilerInfo2 APIs.

    What i want to do is, get the 'this' pointer of the following class when its member function is called. So that i can inspect its state. In the following class, i need to get the class instance from profiling, type cast it to the type so that i can read its properties.

    public class TestManagedClass{

    public int somevalue;

    public SecondDotNetClass cls;

    ......

    public void MyFunction(some args);

    }

    I am using ICoreprofiler and getting the call back when the the function 'MyFunction' being called with functionId,clientdata,frameinfo and argument info.

    Once i am here, i call GetFunctionInfo2 and get the class id. Now what i want to do is get this information and pass it on to the manged layer and pass some kind of pointer there so that i can do something like this.

    IntPtr p = (IntPtr)Ptr;
            GCHandle handle2 = (GCHandle)p;
            TestManagedClass objOfInterest= (handle2.Target as TestManagedClass);

    I am looking at GetBoxClassLayout or GetClassLayout kind of methods so that i can extract the GCHandle or something similar to it, with which i can get the managed object back. Does any of these sound reasonable ? Or do i have to hook to some other call back so that i can map the class id to GCHandle? 

    Thank in advance for your advice!


    Gp


    • Edited by _gP_ Thursday, July 2, 2015 8:05 AM
    Thursday, July 2, 2015 5:51 AM

Answers

  • If you want to access properties or call unmanaged code in a profiler, then you need to get managed code to do for you rather than make the call from a callback and that means instrumenting an appropriate method.

    You could insert some IL at the beginning of Form.Show() to call the managed properties and collect whatever information you want, then pass that information to a native method in the profiler.

    Thursday, July 2, 2015 10:02 PM

All replies

  • First of all, what sort of callback are you using? If you are using the ELT callbacks or any of the callbacks provided by the ICorProfilerCallback interfaces, then you cant safely call back into managed code anyway (could deadlock on jit/garbage collection).

    If on the other hand, you are instrumenting the function to call an unmanaged function provided by your profiler, then you could just use something like this.

    ldarg.0
    call object get_Property()
    calli void <ptr-to-profiler-function>(object)


    Thursday, July 2, 2015 8:50 AM
  • Thanks Brian for the reply.

    I am using ETL callbacks, but what i want to inspect is not my code. Its s standard .Net library code. For instance, say i am interested in Windows forms, i will wait for the myform.Show to execute and when it does i want to get the 'myform' instance.  Since its difficult to probe the myform object in C++ i want to pass this object to my own .Net helper method that could look at this pointer and cast it to Form type so that i can look at say color, position etc set by the calling application.


    Gp

    Thursday, July 2, 2015 10:41 AM
  • Your 'this' value will be passed as the first argument in an instance method.

    If you are using FunctionEnter2 (.NET 2+) then you can use the passed in argumentInfo to get the memory locations where the arguments can be found.

    If you are using FunctionEnter3WithInfo (.NET 4+), then you will need to pass the eltInfo reference to GetFunctionEnter3Info to get the argument info.

    Either way, you will need to include COR_PRF_ENABLE_FUNCTION_ARGS in your call to SetEventMask and you will need to get the reference before the callback returns.

    I believe object references gained in this way should be treated as ObjectIDs, a pointer to the object in the heap. Note: the GC may move objects around so in most cases you should assume the id has become invalid as soon as you return from whatever callback retrieved it. If you need to track ObjectIDs across multiple callbacks then you can register for GC callbacks and manually update your tracked references when they are moved/collected.

    You won't be able to call properties on the object from your callback, but you can get the type of the object (GetClassFromObject) and you should be able to access the objects fields via their offsets (GetClassLayout, GetBoxClassLayout, GetArrayObjectInfo, GetStringLayout).

    In the case of accessing properties when Form.Show() is called, you might be better off instrumenting the method to call the properties for you and pass their values to a native method in your profiler.

    Thursday, July 2, 2015 12:33 PM
  • Since these are the object pointers for the objected created in managed space, i was hoping that these would be managed object pointers and was hoping that it could be easily boxed back in the managed layer. So i was passing it to managed layer as intptr to get something meaningful out of it. 

    But your explanation sounds like its not a feasible option. Since many of the properties i want to extract from this class objects are managed class instances, it would be real messy getting though these in un-managed layer.

    Do you have any suggestion on what i could do?


    Gp

    Thursday, July 2, 2015 4:54 PM
  • If you want to access properties or call unmanaged code in a profiler, then you need to get managed code to do for you rather than make the call from a callback and that means instrumenting an appropriate method.

    You could insert some IL at the beginning of Form.Show() to call the managed properties and collect whatever information you want, then pass that information to a native method in the profiler.

    Thursday, July 2, 2015 10:02 PM
  • Hello _gP_,

    Any update? I have marked Brian reply as answer since I think it is helpful, if you think it provides no help, please unmark it.

    Thank you for your understanding and support.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Friday, July 10, 2015 2:15 AM
    Moderator
  • Yes i followed Brian's approach of Injecting IL and calling my managed method and able to extract parameter. Thanks again!

    Gp

    Sunday, July 12, 2015 8:33 PM