Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.9k views
in Technique[技术] by (71.8m points)

.net - Inspect the managed stack

A .NET application can get a managed StackTrace that describes which methods were called and holds references to them to get their name, token and signature, and at which IL offset in the method body the call was made. But it does not contain the argument values that were passed to each method.

The argument values are in the process's stack memory somewhere for sure, but that's the native representation which may be a bit unhandy and unpredictable to evaluate.

There is also the managed stack, the one that the CLR essentially executes on, before the JIT compiler. In MSIL code, arguments are put on that stack before the call opcode is executed. So these values should also be on the CLR stack.

The question is, can a managed application inspect its own managed stack at runtime to extract such information?

I'm not talking about separate debugger processes like Visual Studio. I want to do this from within the process. I also understand that any executed code will add to the stack, so this thing would need to set a certain "entry point" from which I could inspect the stack upwards (i. e. towards to root, if the CLR also keeps stacks hanging from the ceiling...), ignoring what my current method and its called methods do.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

The reason is described by Raymond Chen here.

The short of it:

A parameter to a method can become eligible for collection while the method is still executing.

It's not intuitive, read the part about JIT and GC working together. So the limitation of the StackTrace info is by design.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...