C++ Stack Traces 
by Noam Cohen 


Listing One 
#define DECLARE_STACKTRACE  StackTrace  g_StackTracer;
// helper class for making life easier for the users of the StackTrace
// When entering a function, simply construct an object of this class
class AutoStackTrackEntry
{
public:
    AutoStackTrackEntry(IN const char* message ,const char* _ebp)
    {
        g_StackTracer.Add(message,_ebp);
    }
    ~AutoStackTrackEntry()
    {
        g_StackTracer.Remove();
    }
};
// macro to make life even easier using AutoStackTrackEntry.
// You should have #define DECLARE_STACKTRACE  
//                before any use of the STACK_TRACE() macro
#define STACK_TRACE(msg)    \
    unsigned int _ebp;      \
    {__asm mov _ebp,ebp }   \
    AutoStackTrackEntry  _stackEntry(msg, 
                                reinterpret_cast<char*>(_ebp + 4))

Listing Two
Add( function_name, args_buffer)
{
   tls_index =  GetTlsForThread(GetCurrentThreadId()) ;
if(NOT_FOUND == tls_index)
{
   allocate structure for the call stack, put it in  the TLS;
   insert the mapping 'tid -> tls' to the map.
}
StackTraceStruct  *pStruct = data_block_from(tls_index);
Update pstruct with function_name, args_buffer
If( should write function trace)
    Notify worker thread with the current parameters.
}


Listing Three
int Tester::g(int k)
{
    STACK_TRACE("Tester::g");
    ...
}








1


