Win32 doesn't have execinfo.h, so don't include it
[libs/core.git] / source / debug / backtrace.cpp
1 /* $Id$
2
3 This file is part of libmspcore
4 Copyright © 2007 Mikko Rasa, Mikkosoft Productions
5 Distributed under the LGPL
6 */
7 #ifndef WIN32
8 #include <execinfo.h>
9 #endif
10 #include "backtrace.h"
11
12 using namespace std;
13
14 namespace Msp {
15 namespace Debug {
16
17 Backtrace Backtrace::create()
18 {
19 #ifndef WIN32
20         void *addresses[50];
21         int count=::backtrace(addresses, 50);
22
23         char **symbols=backtrace_symbols(addresses, count);
24
25         Backtrace bt;
26         for(int i=0; i<count; ++i)
27                 bt.frames.push_back(StackFrame(addresses[i], symbols[i]));
28
29         free(symbols);
30
31         return bt;
32 #else
33         return Backtrace();
34 #endif
35 }
36
37 ostream &operator<<(ostream &out, const Backtrace &bt)
38 {
39         const Backtrace::FrameSeq &frames=bt.get_frames();
40         for(Backtrace::FrameSeq::const_iterator i=frames.begin(); i!=frames.end(); ++i)
41                 out<<i->address<<" in "<<i->symbol<<'\n';
42
43         return out;
44 }
45
46 } // namespace Debug
47 } // namespace Msp