summaryrefslogtreecommitdiff
path: root/libs/pbd/pbd/stacktrace.h
blob: b64b116beffca54c36f3a0d061e92b59af8d842e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
/*
 * Copyright (C) 2000-2015 Paul Davis <paul@linuxaudiosystems.com>
 * Copyright (C) 2013-2014 John Emmas <john@creativepost.co.uk>
 * Copyright (C) 2015-2019 Robin Gareus <robin@gareus.org>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

#ifndef __libpbd_stacktrace_h__
#define __libpbd_stacktrace_h__

#ifdef HAVE_WAFBUILD
#include "libpbd-config.h"
#endif

#include <iostream>
#include <ostream>
#include <glibmm/threads.h>
#include <list>

#ifdef HAVE_EXECINFO
#include <execinfo.h>
#include <cstdlib>
#endif

#include "pbd/libpbd_visibility.h"


namespace PBD {

	LIBPBD_API void stacktrace (std::ostream& out, int levels = 0);
	LIBPBD_API void trace_twb();

template<typename T>
class /*LIBPBD_API*/ thing_with_backtrace
{
public:
	thing_with_backtrace () {
		trace_twb();
#ifdef HAVE_EXECINFO
		allocation_backtrace = new void*[50];
		allocation_backtrace_size = backtrace (allocation_backtrace, 50);
#else
		allocation_backtrace_size = 0;
#endif
		Glib::Threads::Mutex::Lock lm (all_mutex);
		all.push_back (this);
	}

	thing_with_backtrace (const thing_with_backtrace<T>& other) {
		trace_twb();
#ifdef HAVE_EXECINFO
		allocation_backtrace = new void*[50];
		allocation_backtrace_size = backtrace (allocation_backtrace, 50);
#else
		allocation_backtrace_size = 0;
#endif
		Glib::Threads::Mutex::Lock lm (all_mutex);
		all.push_back (this);
	}

	~thing_with_backtrace() {
		if (allocation_backtrace_size) {
			delete [] allocation_backtrace;
		}
		Glib::Threads::Mutex::Lock lm (all_mutex);
		all.remove (this);
	}

	thing_with_backtrace<T>& operator= (const thing_with_backtrace<T>& other) {
		/* no copyable members */
		return *this;
	}

	static void peek_a_boo (std::ostream& stream) {
#ifdef HAVE_EXECINFO
		typename std::list<thing_with_backtrace<T>*>::iterator x;
		for (x = all.begin(); x != all.end(); ++x) {
			char **strings;
			size_t i;

			strings = backtrace_symbols ((*x)->allocation_backtrace, (*x)->allocation_backtrace_size);

			if (strings) {
				stream << "--- ALLOCATED SHARED_PTR @ " << (*x) << std::endl;
				for (i = 0; i < (*x)->allocation_backtrace_size && i < 50U; i++) {
					stream << strings[i] << std::endl;
				}
				free (strings);
			}
		}
#else
		stream << "execinfo not defined for this platform" << std::endl;
#endif
	}

private:
	void** allocation_backtrace;
	int allocation_backtrace_size;
	static std::list<thing_with_backtrace<T>* > all;
	static Glib::Threads::Mutex all_mutex;
};

template<typename T> /*LIBPBD_API*/ std::list<PBD::thing_with_backtrace<T> *> PBD::thing_with_backtrace<T>::all;
template<typename T> /*LIBPBD_API*/ Glib::Threads::Mutex PBD::thing_with_backtrace<T>::all_mutex;

} // namespace PBD

#endif /* __libpbd_stacktrace_h__ */