]> git.tdb.fi Git - libs/core.git/blobdiff - source/debug/backtrace.cpp
Assimilate exceptions and RefPtr from mspmisc
[libs/core.git] / source / debug / backtrace.cpp
diff --git a/source/debug/backtrace.cpp b/source/debug/backtrace.cpp
new file mode 100644 (file)
index 0000000..2078996
--- /dev/null
@@ -0,0 +1,45 @@
+/* $Id$
+
+This file is part of libmspcore
+Copyright © 2007 Mikko Rasa, Mikkosoft Productions
+Distributed under the LGPL
+*/
+#include <execinfo.h>
+#include "backtrace.h"
+
+using namespace std;
+
+namespace Msp {
+namespace Debug {
+
+Backtrace Backtrace::create()
+{
+#ifndef WIN32
+       void *addresses[50];
+       int count=::backtrace(addresses, 50);
+
+       char **symbols=backtrace_symbols(addresses, count);
+
+       Backtrace bt;
+       for(int i=0; i<count; ++i)
+               bt.frames.push_back(StackFrame(addresses[i], symbols[i]));
+
+       free(symbols);
+
+       return bt;
+#else
+       return Backtrace();
+#endif
+}
+
+ostream &operator<<(ostream &out, const Backtrace &bt)
+{
+       const Backtrace::FrameSeq &frames=bt.get_frames();
+       for(Backtrace::FrameSeq::const_iterator i=frames.begin(); i!=frames.end(); ++i)
+               out<<i->address<<" in "<<i->symbol<<'\n';
+
+       return out;
+}
+
+} // namespace Debug
+} // namespace Msp