2078996c41d234bb138f8cb29c4805e7894cd9ab
[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 #include <execinfo.h>
8 #include "backtrace.h"
9
10 using namespace std;
11
12 namespace Msp {
13 namespace Debug {
14
15 Backtrace Backtrace::create()
16 {
17 #ifndef WIN32
18         void *addresses[50];
19         int count=::backtrace(addresses, 50);
20
21         char **symbols=backtrace_symbols(addresses, count);
22
23         Backtrace bt;
24         for(int i=0; i<count; ++i)
25                 bt.frames.push_back(StackFrame(addresses[i], symbols[i]));
26
27         free(symbols);
28
29         return bt;
30 #else
31         return Backtrace();
32 #endif
33 }
34
35 ostream &operator<<(ostream &out, const Backtrace &bt)
36 {
37         const Backtrace::FrameSeq &frames=bt.get_frames();
38         for(Backtrace::FrameSeq::const_iterator i=frames.begin(); i!=frames.end(); ++i)
39                 out<<i->address<<" in "<<i->symbol<<'\n';
40
41         return out;
42 }
43
44 } // namespace Debug
45 } // namespace Msp