Advertisement Jump to content
  • Advertisement
  • 10/22/02 08:33 PM
    Sign in to follow this  

    General Purpose Call Stack Tracer

    General and Gameplay Programming

    Myopic Rhino
    (Editor's Note: Source code for this article is currently not available)


    This is a short little snippet whose purpose is to aid in debugging, by keeping track of which function the thread of execution is currently in, using a staticly allocated doubly-linked list (for performance reasons).


    To use, include CallStackTrace.h (this file) and CallStackTrace.cpp in your project. Then, ensure that in any source file that you'd like to trace the stack you put
    [bquote][font="Courier New"][color="#000080"]#include "CallStackTrace.h"[/color][/font][/bquote]
    near the top.

    Then, to enable the stack tracing, at the top of the function (preferrably the first line of the function), put

    [font="Courier New"] TRACE_ENTER_FN(functionName)[/font]

    where functionName is (obviously) the name of the function being traced. Then, at the end of the function, put

    [font="Courier New"]TRACE_LEAVE_FN()[/font]

    The only function you shouldn't have to put that in is the [font="Courier New"]main()[/font] function. Note that no functionName is required.

    If an error occurs, just call [font="Courier New"]CallStackTrace::Dump()[/font] to dump the current call stack (with file names and line numbers) to stdout. You can optionally pass a std::ostream to [font="Courier New"]CallStackTrace::Dump()[/font] to dump the stack trace to a file or other ostream derived object.

    [size="5"]Catching Unhandled Exceptions

    To report the call stack when an unhandled exception is thrown, structure your [font="Courier New"]main()[/font] function like the following:

    run program here

    cout << "Unhandled Exception!" << endl;

    [size="5"]Handling "Expected" Exceptions

    "But wait!" you say. "What if I handle an exception in a [font="Courier New"]catch()[/font] block? Well, in that case, just make the first line of your catch block the [font="Courier New"]TRACE_UNWIND()[/font] macro (no function name needed, as long as it has a [font="Courier New"]TRACE_ENTER_FN(functionName)[/font] at the top of the function). That will unwind the Call Stack Trace, and allow normal functioning from that point forward.

      Report Article
    Sign in to follow this  

    User Feedback

    There are no comments to display.

    Create an account or sign in to comment

    You need to be a member in order to leave a comment

    Create an account

    Sign up for a new account in our community. It's easy!

    Register a new account

    Sign in

    Already have an account? Sign in here.

    Sign In Now

  • Advertisement

Important Information

By using, you agree to our community Guidelines, Terms of Use, and Privacy Policy. is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!