How get value of parameters in stacktrace
Asked Answered
P

2

19

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?

President answered 26/5, 2012 at 20:49 Comment(1)
possible duplicate of Is it possible to get parameters' values for each frame in call stack in .NETSugarplum
H
10

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".

Helotism answered 26/5, 2012 at 20:58 Comment(3)
fyi, your second link is deadUnsettled
@Fredou: Thanks for letting me know, I've updates the link to the new address of the information.Helotism
Example please!Frisian
T
3

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).

Tull answered 26/5, 2012 at 20:55 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.