Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
2.1k 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 Reply

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...