How get value of parameters in stacktrace

14,137

Solution 1

There are two ways. The more powerful is the COM API for .NET Debugging. For example, arguments and local variables of function in the call stack are both accessible from ICorDebugILFrame. But this must be run from a separate process that is attached to your process as the debugger.

For in-process introspection, there's the Profiler API, which also can find information about function arguments. Look at the information about "shadow stacks".

Solution 2

Not with C# except if you are creating a highly CLR (patch) version dependant solution. But what does work is to attach Windbg to your process, load sos.dll switch to your thread and type

!ClrStack -p

to show the managed call stack and the method parameters for most methods. This is even under the debugger not foolproof because due to inlining and JIT optimizations the stack layout is very flexible. Besides this some parameters might not even show up in the stack memory because they are passed via registers (which is even more common under the x64 platform).

To answer your first question. No it is not possible with the approach you are trying. A working solution would be possible but not at all portable. A managed debugger can partially retrieve your arguments but there is no 100% working solution (even under the debugger).

Share:
14,137

Related videos on Youtube

Jonny Piazzi
Author by

Jonny Piazzi

Updated on September 14, 2022

Comments

  • Jonny Piazzi
    Jonny Piazzi 4 months

    I can get information about a parameter by StackTrace using something like this:

    catch (Exception ex)
    {
        var st = new StackTrace(ex);
    
        System.Reflection.ParameterInfo pi = st.GetFrame(0).GetMethod().GetParameters().First();
    }
    

    I want know how i get the value of parameter. Example:

    If my method in stack trace was like:

    void MyMethod(object value)
    

    And the call was like:

    MyMethod(10);
    

    I want to get the value 10. How i do that?

  • Ben Voigt
    Ben Voigt over 7 years
    @Fredou: Thanks for letting me know, I've updates the link to the new address of the information.
  • Demodave
    Demodave over 5 years
    Example please!