]> git.tdb.fi Git - libs/core.git/blobdiff - source/debug/backtrace.cpp
Use #ifdef _WIN32 rather than WIN32
[libs/core.git] / source / debug / backtrace.cpp
index 643fdf2bb03288dd16cdd3605e473d675541a479..f9057dce833d95b9231917bd763a489f7a856fb1 100644 (file)
@@ -1,10 +1,6 @@
-/* $Id$
-
-This file is part of libmspcore
-Copyright © 2007 Mikko Rasa, Mikkosoft Productions
-Distributed under the LGPL
-*/
-#ifndef WIN32
+// Must include something to test for glibc
+#include <cstdlib>
+#if !defined(_WIN32) && defined(__GLIBC__)
 #include <dlfcn.h>
 #include <execinfo.h>
 #endif
@@ -18,31 +14,27 @@ namespace Debug {
 
 Backtrace Backtrace::create()
 {
-#ifndef WIN32
+#if !defined(_WIN32) && defined(__GLIBC__)
        void *addresses[50];
-       int count=::backtrace(addresses, 50);
-
-       //char **symbols=backtrace_symbols(addresses, count);
+       int count = ::backtrace(addresses, 50);
 
        Backtrace bt;
        Dl_info dli;
        for(int i=0; i<count; ++i)
        {
                StackFrame frame;
-               frame.address=addresses[i];
+               frame.address = addresses[i];
                if(dladdr(addresses[i], &dli))
                {
-                       frame.file=dli.dli_fname;
+                       frame.file = dli.dli_fname;
                        if(dli.dli_sname)
-                               frame.symbol=demangle(dli.dli_sname);
+                               frame.symbol = demangle(dli.dli_sname);
                }
                else
-                       frame.file="<unknown>";
+                       frame.file = "<unknown>";
                bt.frames.push_back(frame);
        }
 
-       //free(symbols);
-
        return bt;
 #else
        return Backtrace();
@@ -51,9 +43,19 @@ Backtrace Backtrace::create()
 
 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<<" from "<<i->file<<'\n';
+       const list<Backtrace::StackFrame> &frames = bt.get_frames();
+       for(list<Backtrace::StackFrame>::const_iterator i=frames.begin(); i!=frames.end(); ++i)
+               out<<*i<<'\n';
+
+       return out;
+}
+
+ostream &operator<<(ostream &out, const Backtrace::StackFrame &sf)
+{
+       out<<sf.address;
+       if(!sf.symbol.empty())
+               out<<" in "<<sf.symbol;
+       out<<" from "<<sf.file;
 
        return out;
 }