]> git.tdb.fi Git - libs/core.git/blob - source/debug/backtrace.cpp
Add missing includes
[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 <dlfcn.h>
9 #include <execinfo.h>
10 #endif
11 #include "backtrace.h"
12 #include "demangle.h"
13
14 using namespace std;
15
16 namespace Msp {
17 namespace Debug {
18
19 Backtrace Backtrace::create()
20 {
21 #ifndef WIN32
22         void *addresses[50];
23         int count=::backtrace(addresses, 50);
24
25         //char **symbols=backtrace_symbols(addresses, count);
26
27         Backtrace bt;
28         Dl_info dli;
29         for(int i=0; i<count; ++i)
30         {
31                 StackFrame frame;
32                 frame.address=addresses[i];
33                 if(dladdr(addresses[i], &dli))
34                 {
35                         frame.file=dli.dli_fname;
36                         if(dli.dli_sname)
37                                 frame.symbol=demangle(dli.dli_sname);
38                 }
39                 else
40                         frame.file="<unknown>";
41                 bt.frames.push_back(frame);
42         }
43
44         //free(symbols);
45
46         return bt;
47 #else
48         return Backtrace();
49 #endif
50 }
51
52 ostream &operator<<(ostream &out, const Backtrace &bt)
53 {
54         const Backtrace::FrameSeq &frames=bt.get_frames();
55         for(Backtrace::FrameSeq::const_iterator i=frames.begin(); i!=frames.end(); ++i)
56                 out<<i->address<<" in "<<i->symbol<<" from "<<i->file<<'\n';
57
58         return out;
59 }
60
61 } // namespace Debug
62 } // namespace Msp