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