Living .NET...

Musings on .NET, and the like - Manoj G [MVP, Connected Systems Developer]

StackTrace and StackFrame

All of us would be aware of the Call Stack, which we usually inspect while debugging any type of application. It would be great if we could really inspect the Call Stack in some way in our code at runtime. System.Diagnostics namespace gives you just that in the form of two very useful classes : The StackFrame and the StackTrace.

A Stack frame is created when a method call is made. Typically, the stack frame contains the information related to the method call and this can be inspected by calling the GetMethod method of a StackFrame instance. This method just returns an instance of type MethodBase. Also, for debug builds, you can get information like source file name, line number etc.

StackTrace is nothing but a collection of StackFrame instances that are on the stack. One place where you would have used this is the StackTrace of the exception object which is just a string representation of the StackTrace. Infact, you can create a Stacktrace instance out of an exception object.

Here's a simple usage which just prints out the call stack till the current method:

StackTrace st  = new StackTrace();
for(int i  = 0; i < st.FrameCount ; i ++)
{
   Console.WriteLine(st.GetFrame(i).GetMethod().Name );
}

There is one small catch. As you all know, as a way of runtime optimization, the CLR may decide to inline a method call. In such cases, you will not have a Stack Frame created (there is no method call at all here).

I can think of few uses of Stacktrace: (There may be many more..)
1. Inspect the metadata of the caller of your method, maybe for security purposes.
2. Check the depth of a method call. This can be useful when the method call is recursive.
3. Log call patterns at runtime for analysis purposes.

 

Posted: Thu, Mar 25 2004 6:49 by Manoj G | with 3 comment(s)
Filed under:

Comments

Manoj G said:

hi manoj,
nice blog.
a few things on this :-
1. When the compiler inlines a method, it almost certainly does it in case of
loops.( i would certainly like a counter-arg[0])
2. The most important use is debugging. Even though u may have VS.net on ur
machine, i guess its much easier for u to know which line throws an error, and
fix the error(if possible) without pressing F5.
3. When u r checking the method call depth( even for security purposes) ,
will this be able to track if a COM client calls this method?

There may be many other uses, but i guess a call stack profiler would be the most
useful application with this, as u mentioned.


# June 30, 2004 4:45 AM

Manoj G said:

Another use would be to use StackFrame and StackTrace to do whitebox testing.
# September 24, 2004 1:10 AM

Jeremy said:

Hello,

i'm trying to do something like this, and i want to acces to the argument's instance of the invoked method.

I can access to the metadata (Type, Name) but i can't access to the value when the method is invoked.

How to do this ?

(Sorry for langage i'm not english :P)

# January 17, 2008 4:22 AM
Leave a Comment

(required) 

(required) 

(optional)

(required)