How to dump the call stack?

25 05 2008


While debugging you can see the call stack at any time you wish by pressing Alt+7. But what to see the callstack when your executable is running without a debugger?


You can dump the call stack by calling – AfxDumpStack(). By default the output will be send by TRACE macro in debug build and if its release build, the output will be copied to the clipboard. You can specify the target destination to where the call stack information is to be copied. See the sample code snippet below.

// Dump the call stack.
AfxDumpStack( AFX_STACK_DUMP_TARGET_BOTH |
              AFX_STACK_DUMP_TARGET_ODS );

See the sample call stack dump[Taken from MSDN].

=== begin AfxDumpStack output ===

00427D55: DUMP2\DEBUG\DUMP2.EXE! void AfxDumpStack(unsigned long) + 181 bytes
0040160B: DUMP2\DEBUG\DUMP2.EXE! void CDump2Dlg::OnClipboard(void) + 14 bytes
BFF73663: WINDOWS\SYSTEM\KERNEL32.DLL! ThunkConnect32 + 2148 bytes
BFF928E0: WINDOWS\SYSTEM\KERNEL32.DLL! UTUnRegister + 2492 bytes

=== end AfxDumpStack() output ===

You might notice that Its not same as the Call stack that we see in Debugger. The syntax for each entry is as follows.

[Address]: [Module FileName ]           [FunctionName] + [Offset]
 BFF73663: WINDOWS\SYSTEM\KERNEL32.DLL! ThunkConnect32 + 2148 bytes


You can use the DebugView application to get the call stack when the executable is running standalone. Download it from http://technet.microsoft.com/en-us/sysinternals/bb896647.aspx.


Targeted Audience – Intermediate.


Actions

Information

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s




%d bloggers like this: