2 Copyright (C) 2000-2007 Paul Davis
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.
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.
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.
20 #ifndef __libpbd_stacktrace_h__
21 #define __libpbd_stacktrace_h__
24 #include "libpbd-config.h"
29 #include <glibmm/threads.h>
37 #include "pbd/libpbd_visibility.h"
42 LIBPBD_API void stacktrace (std::ostream& out, int levels = 0);
43 LIBPBD_API void trace_twb();
46 class /*LIBPBD_API*/ thing_with_backtrace
49 thing_with_backtrace () {
52 allocation_backtrace = new void*[50];
53 allocation_backtrace_size = backtrace (allocation_backtrace, 50);
55 allocation_backtrace_size = 0;
57 Glib::Threads::Mutex::Lock lm (all_mutex);
61 thing_with_backtrace (const thing_with_backtrace<T>& other) {
64 allocation_backtrace = new void*[50];
65 allocation_backtrace_size = backtrace (allocation_backtrace, 50);
67 allocation_backtrace_size = 0;
69 Glib::Threads::Mutex::Lock lm (all_mutex);
73 ~thing_with_backtrace() {
74 if (allocation_backtrace_size) {
75 delete [] allocation_backtrace;
77 Glib::Threads::Mutex::Lock lm (all_mutex);
81 thing_with_backtrace<T>& operator= (const thing_with_backtrace<T>& other) {
82 /* no copyable members */
86 static void peek_a_boo (std::ostream& stream) {
88 typename std::list<thing_with_backtrace<T>*>::iterator x;
89 for (x = all.begin(); x != all.end(); ++x) {
93 strings = backtrace_symbols ((*x)->allocation_backtrace, (*x)->allocation_backtrace_size);
96 stream << "--- ALLOCATED SHARED_PTR @ " << (*x) << std::endl;
97 for (i = 0; i < (*x)->allocation_backtrace_size && i < 50U; i++) {
98 stream << strings[i] << std::endl;
104 stream << "execinfo not defined for this platform" << std::endl;
109 void** allocation_backtrace;
110 int allocation_backtrace_size;
111 static std::list<thing_with_backtrace<T>* > all;
112 static Glib::Threads::Mutex all_mutex;
115 template<typename T> /*LIBPBD_API*/ std::list<PBD::thing_with_backtrace<T> *> PBD::thing_with_backtrace<T>::all;
116 template<typename T> /*LIBPBD_API*/ Glib::Threads::Mutex PBD::thing_with_backtrace<T>::all_mutex;
120 #endif /* __libpbd_stacktrace_h__ */