Ardour  9.0-pre0-582-g084a23a80d
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 #pragma once
22 
23 #ifdef HAVE_WAFBUILD
24 #include "libpbd-config.h"
25 #endif
26 #include <cstddef>
27 
28 #include <iostream>
29 #include <ostream>
30 #include <glibmm/threads.h>
31 #include <list>
32 
33 #ifdef HAVE_EXECINFO
34 #include <execinfo.h>
35 #include <cstdlib>
36 #endif
37 
38 #include "pbd/libpbd_visibility.h"
39 
40 
41 namespace PBD {
42 
43  LIBPBD_API void stacktrace (std::ostream& out, int levels = 0, size_t start_level = 0);
45 
46 template<typename T>
47 class /*LIBPBD_API*/ thing_with_backtrace
48 {
49 public:
51  trace_twb();
52 #ifdef HAVE_EXECINFO
53  allocation_backtrace = new void*[50];
55 #else
57 #endif
59  all.push_back (this);
60  }
61 
63  trace_twb();
64 #ifdef HAVE_EXECINFO
65  allocation_backtrace = new void*[50];
67 #else
69 #endif
71  all.push_back (this);
72  }
73 
76  delete [] allocation_backtrace;
77  }
79  all.remove (this);
80  }
81 
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:
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 
thing_with_backtrace(const thing_with_backtrace< T > &other)
Definition: stacktrace.h:62
static Glib::Threads::Mutex all_mutex
Definition: stacktrace.h:113
static void peek_a_boo(std::ostream &stream)
Definition: stacktrace.h:87
static std::list< thing_with_backtrace< T > * > all
Definition: stacktrace.h:112
thing_with_backtrace< T > & operator=(const thing_with_backtrace< T > &other)
Definition: stacktrace.h:82
#define LIBPBD_API
Definition: axis_view.h:42
void stacktrace(std::ostream &out, int levels=0, size_t start_level=0)
void trace_twb()