merge with master and fix 4 conflicts by hand
[ardour.git] / libs / pbd / pbd / stacktrace.h
1 /*
2     Copyright (C) 2000-2007 Paul Davis 
3
4     This program is free software; you can redistribute it and/or modify
5     it under the terms of the GNU General Public License as published by
6     the Free Software Foundation; either version 2 of the License, or
7     (at your option) any later version.
8
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12     GNU General Public License for more details.
13
14     You should have received a copy of the GNU General Public License
15     along with this program; if not, write to the Free Software
16     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17
18 */
19
20 #ifndef __libpbd_stacktrace_h__
21 #define __libpbd_stacktrace_h__
22
23 #ifdef HAVE_WAFBUILD
24 #include "libpbd-config.h"
25 #endif
26
27 #include <iostream>
28 #include <ostream>
29 #include <glibmm/threads.h>
30 #include <list>
31
32 #ifdef HAVE_EXECINFO
33 #include <execinfo.h>
34 #include <cstdlib>
35 #endif
36
37 #include "pbd/libpbd_visibility.h"
38
39
40 namespace PBD {
41
42         LIBPBD_API void stacktrace (std::ostream& out, int levels = 0);
43         LIBPBD_API void trace_twb();
44         LIBPBD_API std::string demangle (const std::string&);
45
46 template<typename T>
47 class /*LIBPBD_API*/ thing_with_backtrace 
48 {
49   public:
50     thing_with_backtrace () {
51             trace_twb();
52 #ifdef HAVE_EXECINFO
53             allocation_backtrace = new void*[50];
54             allocation_backtrace_size = backtrace (allocation_backtrace, 50);
55 #else 
56             allocation_backtrace_size = 0;
57 #endif
58             Glib::Threads::Mutex::Lock lm (all_mutex);
59             all.push_back (this);
60     }
61
62     thing_with_backtrace (const thing_with_backtrace<T>& other) {
63             trace_twb();
64 #ifdef HAVE_EXECINFO
65             allocation_backtrace = new void*[50];
66             allocation_backtrace_size = backtrace (allocation_backtrace, 50);
67 #else 
68             allocation_backtrace_size = 0;
69 #endif
70             Glib::Threads::Mutex::Lock lm (all_mutex);
71             all.push_back (this);
72     }
73
74     ~thing_with_backtrace() { 
75             if (allocation_backtrace_size) {
76                     delete [] allocation_backtrace;
77             }
78             Glib::Threads::Mutex::Lock lm (all_mutex);
79             all.remove (this);
80     }
81
82     thing_with_backtrace<T>& operator= (const thing_with_backtrace<T>& other) {
83             /* no copyable members */
84             return *this;
85     }
86
87     static void peek_a_boo (std::ostream& stream) {
88 #ifdef HAVE_EXECINFO
89             typename std::list<thing_with_backtrace<T>*>::iterator x;
90             for (x = all.begin(); x != all.end(); ++x) {
91                     char **strings;
92                     size_t i;
93                     
94                     strings = backtrace_symbols ((*x)->allocation_backtrace, (*x)->allocation_backtrace_size);
95                     
96                     if (strings) {
97                             stream << "--- ALLOCATED SHARED_PTR @ " << (*x) << std::endl;
98                             for (i = 0; i < (*x)->allocation_backtrace_size && i < 50U; i++) {
99                                     stream << strings[i] << std::endl;
100                             }
101                             free (strings);
102                     }
103             }
104 #else
105             stream << "execinfo not defined for this platform" << std::endl;
106 #endif
107     }
108
109 private:
110     void** allocation_backtrace;
111     int allocation_backtrace_size;
112     static std::list<thing_with_backtrace<T>* > all;
113     static Glib::Threads::Mutex all_mutex;
114 };
115
116 template<typename T> /*LIBPBD_API*/ std::list<PBD::thing_with_backtrace<T> *> PBD::thing_with_backtrace<T>::all;
117 template<typename T> /*LIBPBD_API*/ Glib::Threads::Mutex PBD::thing_with_backtrace<T>::all_mutex;
118
119 } // namespace PBD
120
121 // JE - !!!!#include "../pbd/stacktrace.impl"
122
123 #endif /* __libpbd_stacktrace_h__ */