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