Skip to content

Commit

Permalink
Add debugBacktrace() for easier debugging.
Browse files Browse the repository at this point in the history
  • Loading branch information
Cyp committed Dec 4, 2011
1 parent e5d7318 commit a58591d
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
21 changes: 21 additions & 0 deletions lib/framework/debug.cpp
Expand Up @@ -33,6 +33,10 @@
#include "lib/framework/wzapp_c.h"
#include "lib/gamelib/gtime.h"

#ifdef WZ_OS_LINUX
#include <execinfo.h> // Nonfatal runtime backtraces.
#endif //WZ_OS_LINUX

extern void NotifyUserOfError(char *); // will throw up a notifier on error

#define MAX_LEN_LOG_LINE 512
Expand Down Expand Up @@ -477,6 +481,23 @@ void _debug( code_part part, const char *function, const char *str, ... )
useInputBuffer1 = !useInputBuffer1; // Swap buffers
}

void _debugBacktrace(code_part part)
{
#ifdef WZ_OS_LINUX
void *btv[20];
unsigned num = backtrace(btv, sizeof(btv)/sizeof(*btv));
char **btc = backtrace_symbols(btv, num);
unsigned i;
for (i = 1; i + 2 < num; ++i) // =1: Don't print "src/warzone2100(syncDebugBacktrace+0x16) [0x6312d1]". +2: Don't print last two lines of backtrace such as "/lib/libc.so.6(__libc_start_main+0xe6) [0x7f91e040ea26]", since the address varies (even with the same binary).
{
_debug(part, "BT", "%s", btc[i]);
}
free(btc);
#else
// debugBacktrace not implemented.
#endif
}

bool debugPartEnabled(code_part codePart)
{
return enabled_debug[codePart];
Expand Down
3 changes: 3 additions & 0 deletions lib/framework/debug.h
Expand Up @@ -256,6 +256,9 @@ bool debug_enable_switch(const char *str);
void _debug( code_part part, const char *function, const char *str, ...)
WZ_DECL_FORMAT(printf, 3, 4);

#define debugBacktrace(part, ...) do { if (enabled_debug[part]) { _debug(part, __FUNCTION__, __VA_ARGS__); _debugBacktrace(part); }} while(0)
void _debugBacktrace(code_part part);

/** Global to keep track of which game object to trace. */
extern UDWORD traceID;

Expand Down

0 comments on commit a58591d

Please sign in to comment.