added a printTrace function to print out the current stack
authorPatrik Gornicz <Gornicz.P@gmail.com>
Sun, 31 May 2009 02:49:44 +0000 (22:49 -0400)
committerPatrik Gornicz <Gornicz.P@gmail.com>
Sun, 31 May 2009 02:49:44 +0000 (22:49 -0400)
inc/debug.h
src/debug.cpp

index 90ab59e..fb5ac94 100644 (file)
@@ -26,6 +26,8 @@ namespace bear
     {
         void init();
         void fini();
+
+        void printTrace();
     }
 
     void DASSERT(bool fAssert);
index 8d523dd..ffdfa47 100644 (file)
@@ -23,6 +23,7 @@ using std::cout;
 using std::endl;
 
 #include <assert.h>
+#include <execinfo.h>
 
 #include "Lock.h"
 #include "Autolock.h"
@@ -34,13 +35,6 @@ using namespace bear;
 
 Lock muDPF;
 
-void bear::DPF(int iLevel, const char* pstr)
-{
-    Autolock lock(&muDPF);
-
-    cout << pstr << endl;
-}
-
 void bear::debug::init()
 {
     muDPF.init();
@@ -51,6 +45,39 @@ void bear::debug::fini()
     muDPF.fini();
 }
 
+void bear::debug::printTrace()
+{
+    int     size;
+    char**  strings;
+
+    {
+        void*   array[50];
+
+        size = backtrace (array, 50);
+        strings = backtrace_symbols (array, size);
+    }
+
+    {
+        Autolock lock(&muDPF);
+
+        cout << "Obtained " << size << " stack frames." << endl;
+
+        for (int i = 0; i < size; i++)
+        {
+            cout << strings[i] << endl;
+        }
+    }
+
+    free (strings);
+}
+
+void bear::DPF(int iLevel, const char* pstr)
+{
+    Autolock lock(&muDPF);
+
+    cout << pstr << endl;
+}
+
 void bear::DASSERT(bool fAssert)
 {
     assert(fAssert);