]> git.tdb.fi Git - libs/core.git/blobdiff - source/debug/backtrace.cpp
MSVC compatibility fixes
[libs/core.git] / source / debug / backtrace.cpp
index 643fdf2bb03288dd16cdd3605e473d675541a479..fe7dbffcd51a39216c783bdcd47f7d2bb45200dd 100644 (file)
@@ -4,7 +4,10 @@ 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,12 +21,10 @@ 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);
-
        Backtrace bt;
        Dl_info dli;
        for(int i=0; i<count; ++i)
@@ -41,8 +42,6 @@ Backtrace Backtrace::create()
                bt.frames.push_back(frame);
        }
 
-       //free(symbols);
-
        return bt;
 #else
        return Backtrace();
@@ -53,7 +52,17 @@ 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';
+               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;
 }