add export visibility control to libpbd (works thus far on linux/gcc)
[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         LIBPBD_API void stacktrace (std::ostream& out, int levels = 0);
42         LIBPBD_API void trace_twb();
43
44 template<typename T>
45 class LIBPBD_API thing_with_backtrace 
46 {
47   public:
48     thing_with_backtrace () {
49             trace_twb();
50 #ifdef HAVE_EXECINFO
51             allocation_backtrace = new void*[50];
52             allocation_backtrace_size = backtrace (allocation_backtrace, 50);
53 #else 
54             allocation_backtrace_size = 0;
55 #endif
56             Glib::Threads::Mutex::Lock lm (all_mutex);
57             all.push_back (this);
58     }
59
60     thing_with_backtrace (const thing_with_backtrace<T>& other) {
61             trace_twb();
62 #ifdef HAVE_EXECINFO
63             allocation_backtrace = new void*[50];
64             allocation_backtrace_size = backtrace (allocation_backtrace, 50);
65 #else 
66             allocation_backtrace_size = 0;
67 #endif
68             Glib::Threads::Mutex::Lock lm (all_mutex);
69             all.push_back (this);
70     }
71
72     ~thing_with_backtrace() { 
73             if (allocation_backtrace_size) {
74                     delete [] allocation_backtrace;
75             }
76             Glib::Threads::Mutex::Lock lm (all_mutex);
77             all.remove (this);
78     }
79
80     thing_with_backtrace<T>& operator= (const thing_with_backtrace<T>& other) {
81             /* no copyable members */
82             return *this;
83     }
84
85     static void peek_a_boo (std::ostream& stream) {
86 #ifdef HAVE_EXECINFO
87             typename std::list<thing_with_backtrace<T>*>::iterator x;
88             for (x = all.begin(); x != all.end(); ++x) {
89                     char **strings;
90                     size_t i;
91                     
92                     strings = backtrace_symbols ((*x)->allocation_backtrace, (*x)->allocation_backtrace_size);
93                     
94                     if (strings) {
95                             stream << "--- ALLOCATED SHARED_PTR @ " << (*x) << std::endl;
96                             for (i = 0; i < (*x)->allocation_backtrace_size && i < 50U; i++) {
97                                     stream << strings[i] << std::endl;
98                             }
99                             free (strings);
100                     }
101             }
102 #else
103             stream << "execinfo not defined for this platform" << std::endl;
104 #endif
105     }
106
107 private:
108     void** allocation_backtrace;
109     int allocation_backtrace_size;
110     static std::list<thing_with_backtrace<T>* > all;
111     static Glib::Threads::Mutex all_mutex;
112 };
113
114 template<typename T> LIBPBD_API std::list<PBD::thing_with_backtrace<T> *> PBD::thing_with_backtrace<T>::all;
115 template<typename T> LIBPBD_API Glib::Threads::Mutex PBD::thing_with_backtrace<T>::all_mutex;
116
117 } // namespace PBD
118
119
120
121 #endif /* __libpbd_stacktrace_h__ */