summaryrefslogtreecommitdiff
path: root/libs/pbd/boost_debug.cc
blob: e1c6571f9cf135fcaacff22afb99351c2ee16802 (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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
/*
    Copyright (C) 2009 Paul Davis
    From an idea by Carl Hetherington.

    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., 675 Mass Ave, Cambridge, MA 02139, USA.

*/

#include "libpbd-config.h"

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

#include <stdlib.h>
#include <iostream>
#include <map>
#include <set>
#include <vector>
#include <glibmm/threads.h>
#include <boost/shared_ptr.hpp>

#include "pbd/stacktrace.h"
#include "pbd/boost_debug.h"

class Backtrace {
public:
    Backtrace ();
    std::ostream& print (std::ostream& str) const;

private:
    void* trace[200];
    size_t size;
};

std::ostream& operator<< (std::ostream& str, const Backtrace& bt) { return bt.print (str); }


Backtrace::Backtrace()
{
#ifdef HAVE_EXECINFO
	size = ::backtrace (trace, 200);
#endif
}

std::ostream&
Backtrace::print (std::ostream& str) const
{
	char **strings = 0;
	size_t i;

	if (size) {
#ifdef HAVE_EXECINFO
		strings = ::backtrace_symbols (trace, size);
#endif
		if (strings) {
			for (i = 3; i < 5+18 && i < size; i++) {
				str << strings[i] << std::endl;
			}
			free (strings);
		}
	}

	return str;
}

struct BTPair {

    Backtrace* ref;
    Backtrace* rel;

    BTPair (Backtrace* bt) : ref (bt), rel (0) {}
    ~BTPair () { }

};

std::ostream& operator<<(std::ostream& str, const BTPair& btp) {
	str << "*********************************************\n";
	if (btp.ref) str << *btp.ref << std::endl;
	str << "Rel:\n";
	if (btp.rel) str << *btp.rel << std::endl;
	return str;
}

struct SPDebug {
    Backtrace* constructor;
    Backtrace* destructor;

    SPDebug (Backtrace* c) : constructor (c), destructor (0) {}
    ~SPDebug () {
	    delete constructor;
	    delete destructor;
    }
};

std::ostream& operator<< (std::ostream& str, const SPDebug& spd)
{
	str << "Constructor :" << std::endl;
	if (spd.constructor) {
		str << *spd.constructor << std::endl;
	}

	return str;
}

typedef std::multimap<void const*,SPDebug*> PointerMap;
typedef std::map<void const*,const char*> IPointerMap;

using namespace std;

static PointerMap* _sptrs;
PointerMap& sptrs() {
        if (_sptrs == 0) {
                _sptrs = new PointerMap;
        }
        return *_sptrs;
}

static IPointerMap* _interesting_pointers;
IPointerMap& interesting_pointers() {
        if (_interesting_pointers == 0) {
                _interesting_pointers = new IPointerMap;
        }
        return *_interesting_pointers;
}

static Glib::Threads::Mutex* _the_lock;
static Glib::Threads::Mutex& the_lock() {
        if (_the_lock == 0) {
                _the_lock = new Glib::Threads::Mutex;
        }
        return *_the_lock;
}


static bool
is_interesting_object (void const* ptr)
{
	if (ptr == 0) {
		return false;
	}

	return interesting_pointers().find (ptr) != interesting_pointers().end();
}

/* ------------------------------- */

static bool debug_out = false;

void
boost_debug_shared_ptr_show_live_debugging (bool yn)
{
	debug_out = yn;
}

void
boost_debug_shared_ptr_mark_interesting (void* ptr, const char* type)
{
	Glib::Threads::Mutex::Lock guard (the_lock());
 	pair<void*,const char*> newpair (ptr, type);
	interesting_pointers().insert (newpair);
	if (debug_out) {
		cerr << "Interesting object @ " << ptr << " of type " << type << endl;
	}
}

void
boost_debug_shared_ptr_operator_equals (void const *sp, void const *old_obj, int old_use_count,  void const *obj, int new_use_count)
{
	if (old_obj == 0 && obj == 0) {
		return;
	}

	Glib::Threads::Mutex::Lock guard (the_lock());

	if (is_interesting_object  (old_obj) || is_interesting_object (obj)) {
		if (debug_out) {
			cerr << "ASSIGN SWAPS " << old_obj << " & " << obj << endl;
		}
	}

	if (is_interesting_object (old_obj)) {
		if (debug_out) {
			cerr << "\tlost old sp @ " << sp << " for " << old_obj << " UC = " << old_use_count << " now for " << obj << " UC = " << new_use_count
			     << " (total sp's = " << sptrs().size() << ')' << endl;
		}
		PointerMap::iterator x = sptrs().find (sp);

		if (x != sptrs().end()) {
			sptrs().erase (x);
			if (debug_out) {
				cerr << "\tRemoved (by assignment) sp for " << old_obj << " @ " << sp << " UC = " << old_use_count << " (total sp's = " << sptrs().size() << ')' << endl;
			}
		}
	}

	if (is_interesting_object (obj)) {

		pair<void const*, SPDebug*> newpair;

		newpair.first = sp;
		newpair.second = new SPDebug (new Backtrace());

		sptrs().insert (newpair);

		if (debug_out) {
			cerr << "assignment created sp for " << obj << " @ " << sp << " used to point to " << old_obj << " UC = " << old_use_count
			     << " UC = " << new_use_count
			     << " (total sp's = " << sptrs().size() << ')' << endl;
			cerr << *newpair.second << endl;
		}
	}
}

void
boost_debug_shared_ptr_reset (void const *sp, void const *old_obj, int old_use_count,  void const *obj, int new_use_count)
{
	if (old_obj == 0 && obj == 0) {
		return;
	}

	Glib::Threads::Mutex::Lock guard (the_lock());

	if (is_interesting_object  (old_obj) || is_interesting_object (obj)) {
		if (debug_out) {
			cerr << "RESET SWAPS " << old_obj << " & " << obj << endl;
		}
	}

	if (is_interesting_object (old_obj)) {
		if (debug_out) {
			cerr << "\tlost old sp @ " << sp << " for " << old_obj << " UC = " << old_use_count << " now for " << obj << " UC = " << new_use_count
			     << " (total sp's = " << sptrs().size() << ')' << endl;
		}
		PointerMap::iterator x = sptrs().find (sp);

		if (x != sptrs().end()) {
			sptrs().erase (x);
			if (debug_out) {
				cerr << "\tRemoved (by reset) sp for " << old_obj << " @ " << sp << " UC = " << old_use_count << " (total sp's = " << sptrs().size() << ')' << endl;
			}
		}
	}

	if (is_interesting_object (obj)) {

		pair<void const*, SPDebug*> newpair;

		newpair.first = sp;
		newpair.second = new SPDebug (new Backtrace());

		sptrs().insert (newpair);

		if (debug_out) {
			cerr << "reset created sp for " << obj << " @ " << sp << " used to point to " << old_obj << " UC = " << old_use_count
			     << " UC = " << new_use_count
			     << " (total sp's = " << sptrs().size() << ')' << endl;
			cerr << *newpair.second << endl;
		}
	}
}

void
boost_debug_shared_ptr_destructor (void const *sp, void const *obj, int use_count)
{
	Glib::Threads::Mutex::Lock guard (the_lock());
	PointerMap::iterator x = sptrs().find (sp);

	if (x != sptrs().end()) {
		sptrs().erase (x);
		if (debug_out) {
			cerr << "Removed sp for " << obj << " @ " << sp << " UC = " << use_count << " (total sp's = " << sptrs().size() << ')' << endl;
		}
	}
}

void
boost_debug_shared_ptr_constructor (void const *sp, void const *obj, int use_count)
{
	if (is_interesting_object (obj)) {
		Glib::Threads::Mutex::Lock guard (the_lock());
		pair<void const*, SPDebug*> newpair;

		newpair.first = sp;
		newpair.second = new SPDebug (new Backtrace());

		sptrs().insert (newpair);
		if (debug_out) {
			cerr << "Stored constructor for " << obj << " @ " << sp << " UC = " << use_count << " (total sp's = " << sptrs().size() << ')' << endl;
			cerr << *newpair.second << endl;
		}
	}
}

void
boost_debug_count_ptrs ()
{
	Glib::Threads::Mutex::Lock guard (the_lock());
	// cerr << "Tracking " << interesting_pointers().size() << " interesting objects with " << sptrs().size () << " shared ptrs\n";
}

void
boost_debug_list_ptrs ()
{
	Glib::Threads::Mutex::Lock guard (the_lock());

	if (sptrs().empty()) {
		cerr << "There are no dangling shared ptrs\n";
	} else {
		for (PointerMap::iterator x = sptrs().begin(); x != sptrs().end(); ++x) {
			cerr << "Shared ptr @ " << x->first << " history: "
			     << *x->second
			     << endl;
		}
	}
}

namespace boost {

void sp_scalar_constructor_hook( void *, std::size_t, void *)
{
}

void sp_scalar_destructor_hook( void *, std::size_t, void *)
{
}

void sp_counter_ref_hook (void* /*pn*/, long /* use count */)
{
}

void sp_counter_release_hook (void* /*pn*/, long /*use_count*/)
{
}

void sp_array_constructor_hook(void *)
{
}

void sp_array_destructor_hook(void *)
{
}

void sp_scalar_constructor_hook(void *)
{
}

void sp_scalar_destructor_hook(void *)
{
}

}