Stack Trace [modified]
-
Hello everybody, is it possible to determine at realtime the function which has called the current function? Example
void A(void)
{
DoWork();
}void B(void)
{
DoWork();
}void DoWork(void)
{
char *trace = GetTraceOf5LastFunction();
OutputDebugString("DoWork called from function : ");
OutputDebugString(trace);
}Is it possible? Big thanks for any help :) Edit: I have found a fonction called "backtrace" which is placed into execinfo.h But i don't have a such header-file on my system (Visual Studio)
modified on Tuesday, January 27, 2009 6:26 AM
-
Hello everybody, is it possible to determine at realtime the function which has called the current function? Example
void A(void)
{
DoWork();
}void B(void)
{
DoWork();
}void DoWork(void)
{
char *trace = GetTraceOf5LastFunction();
OutputDebugString("DoWork called from function : ");
OutputDebugString(trace);
}Is it possible? Big thanks for any help :) Edit: I have found a fonction called "backtrace" which is placed into execinfo.h But i don't have a such header-file on my system (Visual Studio)
modified on Tuesday, January 27, 2009 6:26 AM
Can you use of dll's you can load your functions of dll on the current function.
Of one Essence is the human race thus has Creation put the base One Limb impacted is sufficient For all Others to feel the Mace (Saadi )
-
Can you use of dll's you can load your functions of dll on the current function.
Of one Essence is the human race thus has Creation put the base One Limb impacted is sufficient For all Others to feel the Mace (Saadi )
-
Hello everybody, is it possible to determine at realtime the function which has called the current function? Example
void A(void)
{
DoWork();
}void B(void)
{
DoWork();
}void DoWork(void)
{
char *trace = GetTraceOf5LastFunction();
OutputDebugString("DoWork called from function : ");
OutputDebugString(trace);
}Is it possible? Big thanks for any help :) Edit: I have found a fonction called "backtrace" which is placed into execinfo.h But i don't have a such header-file on my system (Visual Studio)
modified on Tuesday, January 27, 2009 6:26 AM
Hi, the easiest way may well be the low-tech one: add a string parameter to the function, and pass the caller's name. That works no matter what.
DoWork("A");
:)Luc Pattyn [Forum Guidelines] [My Articles]
- before you ask a question here, search CodeProject, then Google - the quality and detail of your question reflects on the effectiveness of the help you are likely to get - use the code block button (PRE tags) to preserve formatting when showing multi-line code snippets
modified on Tuesday, January 27, 2009 8:10 AM
-
Hi, the easiest way may well be the low-tech one: add a string parameter to the function, and pass the caller's name. That works no matter what.
DoWork("A");
:)Luc Pattyn [Forum Guidelines] [My Articles]
- before you ask a question here, search CodeProject, then Google - the quality and detail of your question reflects on the effectiveness of the help you are likely to get - use the code block button (PRE tags) to preserve formatting when showing multi-line code snippets
modified on Tuesday, January 27, 2009 8:10 AM
-
Hello everybody, is it possible to determine at realtime the function which has called the current function? Example
void A(void)
{
DoWork();
}void B(void)
{
DoWork();
}void DoWork(void)
{
char *trace = GetTraceOf5LastFunction();
OutputDebugString("DoWork called from function : ");
OutputDebugString(trace);
}Is it possible? Big thanks for any help :) Edit: I have found a fonction called "backtrace" which is placed into execinfo.h But i don't have a such header-file on my system (Visual Studio)
modified on Tuesday, January 27, 2009 6:26 AM
You need a stack walking function. Here are some[^] links[^] that may be useful. The second is probably best - John Robbins is a well regarded expert on debugging and things like that. His SUPERASSERT code includes stack tracing code - see the dialog screenshot about half way through the article for an example of what you can get. Obviously you only need the last item in the trace. One last thing - IIRC, without debug symbols, all you're going to get is the address that called you, not a function name.
-
This method is the simplest, but i can't write over 100 Debug-Lines into the code :-) But thanks anyway ;)
Hi baerten, I agree with Luc. However you may be able to use a pre-defined macro[^] something like below:
VOID FunctionB(int iVal,char *caller,int line) { char szBuf[MAX_PATH]; sprintf(szBuf,"I was called by %s on line %d",caller,line); MessageBoxA(NULL,szBuf,0,0); } VOID FunctionA() { FunctionB(1,__FUNCTION__,__LINE__); }
baerten wrote:
i can't write over 100 Debug-Lines into the code
A stack trace is several times more expensive at runtime. Best Wishes, -David Delaune