2009-11-23 12:33:39 -05:00
|
|
|
/*
|
2019-08-02 23:10:55 -04:00
|
|
|
* Copyright (C) 2009-2018 Paul Davis <paul@linuxaudiosystems.com>
|
|
|
|
* 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.
|
|
|
|
*/
|
2009-11-23 12:33:39 -05:00
|
|
|
|
2009-11-23 19:09:15 -05:00
|
|
|
#include "libpbd-config.h"
|
|
|
|
|
|
|
|
#ifdef HAVE_EXECINFO
|
|
|
|
#include <execinfo.h>
|
|
|
|
#endif
|
|
|
|
|
2009-11-23 12:33:39 -05:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <iostream>
|
|
|
|
#include <map>
|
|
|
|
#include <set>
|
2009-11-23 19:09:15 -05:00
|
|
|
#include <vector>
|
2012-07-25 13:48:55 -04:00
|
|
|
#include <glibmm/threads.h>
|
2009-11-23 12:33:39 -05:00
|
|
|
|
|
|
|
#include "pbd/stacktrace.h"
|
2013-10-17 13:51:02 -04:00
|
|
|
#include "pbd/boost_debug.h"
|
2009-11-23 12:33:39 -05:00
|
|
|
|
|
|
|
class Backtrace {
|
|
|
|
public:
|
2019-04-13 13:19:29 -04:00
|
|
|
Backtrace ();
|
|
|
|
std::ostream& print (std::ostream& str) const;
|
2009-11-23 12:33:39 -05:00
|
|
|
|
|
|
|
private:
|
2019-04-13 13:19:29 -04:00
|
|
|
void* trace[200];
|
|
|
|
size_t size;
|
2009-11-23 12:33:39 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
std::ostream& operator<< (std::ostream& str, const Backtrace& bt) { return bt.print (str); }
|
|
|
|
|
2015-10-04 14:51:05 -04:00
|
|
|
Backtrace::Backtrace()
|
|
|
|
{
|
2009-11-23 19:09:15 -05:00
|
|
|
#ifdef HAVE_EXECINFO
|
2009-11-23 12:33:39 -05:00
|
|
|
size = ::backtrace (trace, 200);
|
2009-11-23 19:09:15 -05:00
|
|
|
#endif
|
2009-11-23 12:33:39 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
std::ostream&
|
|
|
|
Backtrace::print (std::ostream& str) const
|
|
|
|
{
|
2009-11-23 19:09:15 -05:00
|
|
|
char **strings = 0;
|
2009-11-23 12:33:39 -05:00
|
|
|
size_t i;
|
|
|
|
|
|
|
|
if (size) {
|
2009-11-23 19:09:15 -05:00
|
|
|
#ifdef HAVE_EXECINFO
|
2009-11-23 12:33:39 -05:00
|
|
|
strings = ::backtrace_symbols (trace, size);
|
2015-10-05 10:17:49 -04:00
|
|
|
#endif
|
2009-11-23 12:33:39 -05:00
|
|
|
if (strings) {
|
2009-12-26 11:15:11 -05:00
|
|
|
for (i = 3; i < 5+18 && i < size; i++) {
|
2009-11-23 12:33:39 -05:00
|
|
|
str << strings[i] << std::endl;
|
|
|
|
}
|
|
|
|
free (strings);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
2015-10-04 14:51:05 -04:00
|
|
|
struct BTPair {
|
2009-11-23 12:33:39 -05:00
|
|
|
|
2019-04-13 13:19:29 -04:00
|
|
|
Backtrace* ref;
|
|
|
|
Backtrace* rel;
|
2009-11-23 19:09:15 -05:00
|
|
|
|
2019-04-13 13:19:29 -04:00
|
|
|
BTPair (Backtrace* bt) : ref (bt), rel (0) {}
|
|
|
|
~BTPair () { }
|
2009-11-23 19:09:15 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
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;
|
2009-11-23 12:33:39 -05:00
|
|
|
}
|
|
|
|
|
2015-10-04 14:51:05 -04:00
|
|
|
struct SPDebug {
|
2019-04-13 13:19:29 -04:00
|
|
|
Backtrace* constructor;
|
|
|
|
Backtrace* destructor;
|
|
|
|
|
|
|
|
SPDebug (Backtrace* c) : constructor (c), destructor (0) {}
|
|
|
|
~SPDebug () {
|
|
|
|
delete constructor;
|
|
|
|
delete destructor;
|
|
|
|
}
|
2009-11-23 19:09:15 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
std::ostream& operator<< (std::ostream& str, const SPDebug& spd)
|
2009-11-23 12:33:39 -05:00
|
|
|
{
|
2009-11-27 12:30:39 -05:00
|
|
|
str << "Constructor :" << std::endl;
|
2009-11-23 19:09:15 -05:00
|
|
|
if (spd.constructor) {
|
|
|
|
str << *spd.constructor << std::endl;
|
|
|
|
}
|
2009-11-23 12:33:39 -05:00
|
|
|
|
2009-11-23 19:09:15 -05:00
|
|
|
return str;
|
|
|
|
}
|
2009-11-23 12:33:39 -05:00
|
|
|
|
2018-12-19 16:19:38 -05:00
|
|
|
typedef std::multimap<volatile void const*,SPDebug*> PointerMap;
|
|
|
|
typedef std::map<volatile void const*,const char*> IPointerMap;
|
2009-11-23 12:33:39 -05:00
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
|
2011-02-10 08:41:41 -05:00
|
|
|
static PointerMap* _sptrs;
|
2019-04-13 13:19:29 -04:00
|
|
|
PointerMap& sptrs()
|
|
|
|
{
|
|
|
|
if (_sptrs == 0) {
|
|
|
|
_sptrs = new PointerMap;
|
|
|
|
}
|
|
|
|
return *_sptrs;
|
2011-02-10 08:41:41 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
static IPointerMap* _interesting_pointers;
|
2019-04-13 13:19:29 -04:00
|
|
|
IPointerMap& interesting_pointers()
|
|
|
|
{
|
|
|
|
if (_interesting_pointers == 0) {
|
|
|
|
_interesting_pointers = new IPointerMap;
|
|
|
|
}
|
|
|
|
return *_interesting_pointers;
|
2011-02-10 08:41:41 -05:00
|
|
|
}
|
|
|
|
|
2012-07-25 13:48:55 -04:00
|
|
|
static Glib::Threads::Mutex* _the_lock;
|
2019-04-13 13:19:29 -04:00
|
|
|
static Glib::Threads::Mutex& the_lock()
|
|
|
|
{
|
|
|
|
if (_the_lock == 0) {
|
|
|
|
_the_lock = new Glib::Threads::Mutex;
|
|
|
|
}
|
|
|
|
return *_the_lock;
|
2011-02-10 08:41:41 -05:00
|
|
|
}
|
2009-11-27 12:30:39 -05:00
|
|
|
|
2009-11-23 12:33:39 -05:00
|
|
|
|
|
|
|
static bool
|
2018-12-19 16:19:38 -05:00
|
|
|
is_interesting_object (volatile void const* ptr)
|
2009-11-23 12:33:39 -05:00
|
|
|
{
|
|
|
|
if (ptr == 0) {
|
|
|
|
return false;
|
|
|
|
}
|
2015-10-05 10:17:49 -04:00
|
|
|
|
2011-02-10 08:41:41 -05:00
|
|
|
return interesting_pointers().find (ptr) != interesting_pointers().end();
|
2009-11-23 12:33:39 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/* ------------------------------- */
|
|
|
|
|
2009-12-25 16:06:52 -05:00
|
|
|
static bool debug_out = false;
|
|
|
|
|
|
|
|
void
|
|
|
|
boost_debug_shared_ptr_show_live_debugging (bool yn)
|
|
|
|
{
|
|
|
|
debug_out = yn;
|
|
|
|
}
|
|
|
|
|
2009-11-23 12:33:39 -05:00
|
|
|
void
|
|
|
|
boost_debug_shared_ptr_mark_interesting (void* ptr, const char* type)
|
|
|
|
{
|
2012-07-25 13:48:55 -04:00
|
|
|
Glib::Threads::Mutex::Lock guard (the_lock());
|
2019-04-13 13:19:29 -04:00
|
|
|
pair<void*,const char*> newpair (ptr, type);
|
2011-02-10 08:41:41 -05:00
|
|
|
interesting_pointers().insert (newpair);
|
2009-12-25 16:06:52 -05:00
|
|
|
if (debug_out) {
|
|
|
|
cerr << "Interesting object @ " << ptr << " of type " << type << endl;
|
|
|
|
}
|
2009-11-23 12:33:39 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2009-12-23 23:04:01 -05:00
|
|
|
boost_debug_shared_ptr_operator_equals (void const *sp, void const *old_obj, int old_use_count, void const *obj, int new_use_count)
|
2009-11-23 12:33:39 -05:00
|
|
|
{
|
2009-12-23 23:04:01 -05:00
|
|
|
if (old_obj == 0 && obj == 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-07-25 13:48:55 -04:00
|
|
|
Glib::Threads::Mutex::Lock guard (the_lock());
|
2009-12-23 23:04:01 -05:00
|
|
|
|
|
|
|
if (is_interesting_object (old_obj) || is_interesting_object (obj)) {
|
2009-12-25 16:06:52 -05:00
|
|
|
if (debug_out) {
|
|
|
|
cerr << "ASSIGN SWAPS " << old_obj << " & " << obj << endl;
|
|
|
|
}
|
2009-12-23 23:04:01 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
if (is_interesting_object (old_obj)) {
|
2009-12-25 16:06:52 -05:00
|
|
|
if (debug_out) {
|
2015-10-04 14:51:05 -04:00
|
|
|
cerr << "\tlost old sp @ " << sp << " for " << old_obj << " UC = " << old_use_count << " now for " << obj << " UC = " << new_use_count
|
2015-10-05 10:17:49 -04:00
|
|
|
<< " (total sp's = " << sptrs().size() << ')' << endl;
|
2009-12-25 16:06:52 -05:00
|
|
|
}
|
2011-02-10 08:41:41 -05:00
|
|
|
PointerMap::iterator x = sptrs().find (sp);
|
2015-10-05 10:17:49 -04:00
|
|
|
|
2011-02-10 08:41:41 -05:00
|
|
|
if (x != sptrs().end()) {
|
|
|
|
sptrs().erase (x);
|
2009-12-25 16:06:52 -05:00
|
|
|
if (debug_out) {
|
2013-03-19 12:24:34 -04:00
|
|
|
cerr << "\tRemoved (by assignment) sp for " << old_obj << " @ " << sp << " UC = " << old_use_count << " (total sp's = " << sptrs().size() << ')' << endl;
|
2009-12-25 16:06:52 -05:00
|
|
|
}
|
2009-12-23 23:04:01 -05:00
|
|
|
}
|
2009-11-27 12:30:39 -05:00
|
|
|
}
|
2009-12-23 23:04:01 -05:00
|
|
|
|
|
|
|
if (is_interesting_object (obj)) {
|
|
|
|
|
|
|
|
pair<void const*, SPDebug*> newpair;
|
|
|
|
|
|
|
|
newpair.first = sp;
|
|
|
|
newpair.second = new SPDebug (new Backtrace());
|
|
|
|
|
2011-02-10 08:41:41 -05:00
|
|
|
sptrs().insert (newpair);
|
2015-10-05 10:17:49 -04:00
|
|
|
|
2009-12-25 16:06:52 -05:00
|
|
|
if (debug_out) {
|
2015-10-04 14:51:05 -04:00
|
|
|
cerr << "assignment created sp for " << obj << " @ " << sp << " used to point to " << old_obj << " UC = " << old_use_count
|
|
|
|
<< " UC = " << new_use_count
|
2015-10-05 10:17:49 -04:00
|
|
|
<< " (total sp's = " << sptrs().size() << ')' << endl;
|
2009-12-25 16:06:52 -05:00
|
|
|
cerr << *newpair.second << endl;
|
|
|
|
}
|
2015-10-04 14:51:05 -04:00
|
|
|
}
|
2009-11-27 12:30:39 -05:00
|
|
|
}
|
2009-11-23 12:33:39 -05:00
|
|
|
|
2009-11-27 12:30:39 -05:00
|
|
|
void
|
2009-12-26 11:15:11 -05:00
|
|
|
boost_debug_shared_ptr_reset (void const *sp, void const *old_obj, int old_use_count, void const *obj, int new_use_count)
|
2009-11-27 12:30:39 -05:00
|
|
|
{
|
2009-12-26 11:15:11 -05:00
|
|
|
if (old_obj == 0 && obj == 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-07-25 13:48:55 -04:00
|
|
|
Glib::Threads::Mutex::Lock guard (the_lock());
|
2009-12-26 11:15:11 -05:00
|
|
|
|
|
|
|
if (is_interesting_object (old_obj) || is_interesting_object (obj)) {
|
2009-12-25 16:06:52 -05:00
|
|
|
if (debug_out) {
|
2009-12-26 11:15:11 -05:00
|
|
|
cerr << "RESET SWAPS " << old_obj << " & " << obj << endl;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (is_interesting_object (old_obj)) {
|
|
|
|
if (debug_out) {
|
2015-10-04 14:51:05 -04:00
|
|
|
cerr << "\tlost old sp @ " << sp << " for " << old_obj << " UC = " << old_use_count << " now for " << obj << " UC = " << new_use_count
|
2015-10-05 10:17:49 -04:00
|
|
|
<< " (total sp's = " << sptrs().size() << ')' << endl;
|
2009-12-26 11:15:11 -05:00
|
|
|
}
|
2011-02-10 08:41:41 -05:00
|
|
|
PointerMap::iterator x = sptrs().find (sp);
|
2015-10-05 10:17:49 -04:00
|
|
|
|
2011-02-10 08:41:41 -05:00
|
|
|
if (x != sptrs().end()) {
|
|
|
|
sptrs().erase (x);
|
2009-12-26 11:15:11 -05:00
|
|
|
if (debug_out) {
|
2011-02-10 08:41:41 -05:00
|
|
|
cerr << "\tRemoved (by reset) sp for " << old_obj << " @ " << sp << " UC = " << old_use_count << " (total sp's = " << sptrs().size() << ')' << endl;
|
2009-12-26 11:15:11 -05:00
|
|
|
}
|
2009-12-25 16:06:52 -05:00
|
|
|
}
|
2009-11-27 12:30:39 -05:00
|
|
|
}
|
2009-12-26 11:15:11 -05:00
|
|
|
|
|
|
|
if (is_interesting_object (obj)) {
|
|
|
|
|
|
|
|
pair<void const*, SPDebug*> newpair;
|
|
|
|
|
|
|
|
newpair.first = sp;
|
|
|
|
newpair.second = new SPDebug (new Backtrace());
|
|
|
|
|
2011-02-10 08:41:41 -05:00
|
|
|
sptrs().insert (newpair);
|
2015-10-05 10:17:49 -04:00
|
|
|
|
2009-12-26 11:15:11 -05:00
|
|
|
if (debug_out) {
|
2015-10-04 14:51:05 -04:00
|
|
|
cerr << "reset created sp for " << obj << " @ " << sp << " used to point to " << old_obj << " UC = " << old_use_count
|
|
|
|
<< " UC = " << new_use_count
|
2015-10-05 10:17:49 -04:00
|
|
|
<< " (total sp's = " << sptrs().size() << ')' << endl;
|
2009-12-26 11:15:11 -05:00
|
|
|
cerr << *newpair.second << endl;
|
|
|
|
}
|
2015-10-04 14:51:05 -04:00
|
|
|
}
|
2009-11-27 12:30:39 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2018-12-19 16:19:38 -05:00
|
|
|
boost_debug_shared_ptr_destructor (void const *sp, volatile void const *obj, int use_count)
|
2009-11-27 12:30:39 -05:00
|
|
|
{
|
2012-07-25 13:48:55 -04:00
|
|
|
Glib::Threads::Mutex::Lock guard (the_lock());
|
2011-02-10 08:41:41 -05:00
|
|
|
PointerMap::iterator x = sptrs().find (sp);
|
2009-11-23 12:33:39 -05:00
|
|
|
|
2011-02-10 08:41:41 -05:00
|
|
|
if (x != sptrs().end()) {
|
|
|
|
sptrs().erase (x);
|
2009-12-25 16:06:52 -05:00
|
|
|
if (debug_out) {
|
2011-02-10 08:41:41 -05:00
|
|
|
cerr << "Removed sp for " << obj << " @ " << sp << " UC = " << use_count << " (total sp's = " << sptrs().size() << ')' << endl;
|
2009-12-25 16:06:52 -05:00
|
|
|
}
|
2009-11-23 12:33:39 -05:00
|
|
|
}
|
2009-11-27 12:30:39 -05:00
|
|
|
}
|
2009-11-27 19:49:04 -05:00
|
|
|
|
2009-11-27 12:30:39 -05:00
|
|
|
void
|
2018-12-19 16:19:38 -05:00
|
|
|
boost_debug_shared_ptr_constructor (void const *sp, volatile void const *obj, int use_count)
|
2009-11-27 12:30:39 -05:00
|
|
|
{
|
|
|
|
if (is_interesting_object (obj)) {
|
2012-07-25 13:48:55 -04:00
|
|
|
Glib::Threads::Mutex::Lock guard (the_lock());
|
2009-11-27 12:30:39 -05:00
|
|
|
pair<void const*, SPDebug*> newpair;
|
2009-11-23 12:33:39 -05:00
|
|
|
|
2009-11-27 12:30:39 -05:00
|
|
|
newpair.first = sp;
|
|
|
|
newpair.second = new SPDebug (new Backtrace());
|
|
|
|
|
2011-02-10 08:41:41 -05:00
|
|
|
sptrs().insert (newpair);
|
2009-12-25 16:06:52 -05:00
|
|
|
if (debug_out) {
|
2011-02-10 08:41:41 -05:00
|
|
|
cerr << "Stored constructor for " << obj << " @ " << sp << " UC = " << use_count << " (total sp's = " << sptrs().size() << ')' << endl;
|
2009-12-25 16:06:52 -05:00
|
|
|
cerr << *newpair.second << endl;
|
|
|
|
}
|
2009-11-27 12:30:39 -05:00
|
|
|
}
|
|
|
|
}
|
2009-11-23 12:33:39 -05:00
|
|
|
|
2009-12-23 23:04:01 -05:00
|
|
|
void
|
|
|
|
boost_debug_count_ptrs ()
|
|
|
|
{
|
2012-07-25 13:48:55 -04:00
|
|
|
Glib::Threads::Mutex::Lock guard (the_lock());
|
2011-02-10 08:41:41 -05:00
|
|
|
// cerr << "Tracking " << interesting_pointers().size() << " interesting objects with " << sptrs().size () << " shared ptrs\n";
|
2009-12-23 23:04:01 -05:00
|
|
|
}
|
|
|
|
|
2009-11-27 12:30:39 -05:00
|
|
|
void
|
|
|
|
boost_debug_list_ptrs ()
|
|
|
|
{
|
2012-07-25 13:48:55 -04:00
|
|
|
Glib::Threads::Mutex::Lock guard (the_lock());
|
2009-11-27 19:49:04 -05:00
|
|
|
|
2011-02-10 08:41:41 -05:00
|
|
|
if (sptrs().empty()) {
|
2009-11-27 19:49:04 -05:00
|
|
|
cerr << "There are no dangling shared ptrs\n";
|
|
|
|
} else {
|
2011-02-10 08:41:41 -05:00
|
|
|
for (PointerMap::iterator x = sptrs().begin(); x != sptrs().end(); ++x) {
|
2009-11-27 19:49:04 -05:00
|
|
|
cerr << "Shared ptr @ " << x->first << " history: "
|
|
|
|
<< *x->second
|
|
|
|
<< endl;
|
|
|
|
}
|
2009-11-23 12:33:39 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
namespace boost {
|
|
|
|
|
2011-09-30 13:55:14 -04:00
|
|
|
void sp_scalar_constructor_hook( void *, std::size_t, void *)
|
2009-11-23 12:33:39 -05:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2011-09-30 13:55:14 -04:00
|
|
|
void sp_scalar_destructor_hook( void *, std::size_t, void *)
|
2009-11-23 12:33:39 -05:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2011-09-30 13:55:14 -04:00
|
|
|
void sp_counter_ref_hook (void* /*pn*/, long /* use count */)
|
2009-11-23 12:33:39 -05:00
|
|
|
{
|
|
|
|
}
|
2009-11-27 12:30:39 -05:00
|
|
|
|
2015-10-04 14:51:05 -04:00
|
|
|
void sp_counter_release_hook (void* /*pn*/, long /*use_count*/)
|
2009-11-23 12:33:39 -05:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2011-09-30 13:55:14 -04:00
|
|
|
void sp_array_constructor_hook(void *)
|
2009-11-23 12:33:39 -05:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2011-09-30 13:55:14 -04:00
|
|
|
void sp_array_destructor_hook(void *)
|
2009-11-23 12:33:39 -05:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2011-09-30 13:55:14 -04:00
|
|
|
void sp_scalar_constructor_hook(void *)
|
2009-11-23 12:33:39 -05:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2011-09-30 13:55:14 -04:00
|
|
|
void sp_scalar_destructor_hook(void *)
|
2009-11-23 12:33:39 -05:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|