2009-11-23 12:33:39 -05:00
|
|
|
/*
|
|
|
|
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.
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
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>
|
2009-11-23 12:33:39 -05:00
|
|
|
#include <glibmm/thread.h>
|
|
|
|
#include <boost/shared_ptr.hpp>
|
|
|
|
|
|
|
|
#include "pbd/stacktrace.h"
|
|
|
|
|
|
|
|
class Backtrace {
|
|
|
|
public:
|
2009-11-27 12:30:39 -05:00
|
|
|
Backtrace ();
|
2009-11-23 12:33:39 -05:00
|
|
|
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); }
|
|
|
|
|
|
|
|
|
2009-11-27 12:30:39 -05: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);
|
2009-11-23 19:09:15 -05:00
|
|
|
#endif
|
2009-11-23 12:33:39 -05:00
|
|
|
if (strings) {
|
2009-11-27 12:30:39 -05:00
|
|
|
for (i = 5; i < 5+18 && i < size; i++) {
|
2009-11-23 12:33:39 -05:00
|
|
|
str << strings[i] << std::endl;
|
|
|
|
}
|
|
|
|
free (strings);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
2009-11-23 19:09:15 -05:00
|
|
|
struct BTPair {
|
2009-11-23 12:33:39 -05:00
|
|
|
|
2009-11-23 19:09:15 -05:00
|
|
|
Backtrace* ref;
|
|
|
|
Backtrace* rel;
|
2009-11-27 12:30:39 -05:00
|
|
|
|
|
|
|
BTPair (Backtrace* bt) : ref (bt), rel (0) {}
|
2009-11-23 19:09:15 -05:00
|
|
|
~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;
|
2009-11-23 12:33:39 -05:00
|
|
|
}
|
|
|
|
|
2009-11-23 19:09:15 -05:00
|
|
|
struct SPDebug {
|
|
|
|
Backtrace* constructor;
|
|
|
|
Backtrace* destructor;
|
2009-11-27 12:30:39 -05:00
|
|
|
|
2009-11-23 19:09:15 -05:00
|
|
|
SPDebug (Backtrace* c) : constructor (c), destructor (0) {}
|
|
|
|
~SPDebug () {
|
|
|
|
delete constructor;
|
|
|
|
delete destructor;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
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
|
|
|
|
2009-11-27 12:30:39 -05:00
|
|
|
typedef std::multimap<void const*,SPDebug*> PointerMap;
|
|
|
|
typedef std::map<void const*,const char*> IPointerMap;
|
2009-11-23 12:33:39 -05:00
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
|
2009-11-27 12:30:39 -05:00
|
|
|
PointerMap sptrs;
|
|
|
|
IPointerMap interesting_pointers;
|
|
|
|
|
|
|
|
static Glib::StaticMutex the_lock;
|
2009-11-23 12:33:39 -05:00
|
|
|
|
|
|
|
static bool
|
|
|
|
is_interesting_object (void const* ptr)
|
|
|
|
{
|
|
|
|
if (ptr == 0) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return interesting_pointers.find (ptr) != interesting_pointers.end();
|
|
|
|
}
|
|
|
|
|
|
|
|
/* ------------------------------- */
|
|
|
|
|
|
|
|
void
|
|
|
|
boost_debug_shared_ptr_mark_interesting (void* ptr, const char* type)
|
|
|
|
{
|
|
|
|
Glib::Mutex::Lock guard (the_lock);
|
|
|
|
pair<void*,const char*> newpair (ptr, type);
|
|
|
|
interesting_pointers.insert (newpair);
|
2009-11-27 19:49:04 -05:00
|
|
|
// cerr << "Interesting object @ " << ptr << " of type " << type << endl;
|
2009-11-23 12:33:39 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2009-11-27 12:30:39 -05:00
|
|
|
boost_debug_shared_ptr_operator_equals (void const *sp, void const *obj, int)
|
2009-11-23 12:33:39 -05:00
|
|
|
{
|
2009-11-27 12:30:39 -05:00
|
|
|
if (is_interesting_object (obj)) {
|
|
|
|
cerr << "sp @ " << sp << " assigned\n";
|
|
|
|
}
|
|
|
|
}
|
2009-11-23 12:33:39 -05:00
|
|
|
|
2009-11-27 12:30:39 -05:00
|
|
|
void
|
|
|
|
boost_debug_shared_ptr_reset (void const *sp, void const *obj, int)
|
|
|
|
{
|
|
|
|
if (is_interesting_object (obj)) {
|
|
|
|
cerr << "sp @ " << sp << " reset\n";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
boost_debug_shared_ptr_destructor (void const *sp, void const *obj, int use_count)
|
|
|
|
{
|
|
|
|
Glib::Mutex::Lock guard (the_lock);
|
|
|
|
PointerMap::iterator x = sptrs.find (sp);
|
2009-11-23 12:33:39 -05:00
|
|
|
|
2009-11-27 12:30:39 -05:00
|
|
|
if (x != sptrs.end()) {
|
|
|
|
sptrs.erase (x);
|
2009-11-27 19:49:04 -05:00
|
|
|
// cerr << "Removed sp for " << obj << " @ " << sp << endl;
|
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
|
|
|
|
boost_debug_shared_ptr_constructor (void const *sp, void const *obj, int use_count)
|
|
|
|
{
|
|
|
|
if (is_interesting_object (obj)) {
|
|
|
|
Glib::Mutex::Lock guard (the_lock);
|
|
|
|
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());
|
|
|
|
|
|
|
|
sptrs.insert (newpair);
|
2009-11-27 19:49:04 -05:00
|
|
|
// cerr << "Stored constructor for " << obj << " @ " << sp << endl;
|
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
|
|
|
|
boost_debug_list_ptrs ()
|
|
|
|
{
|
|
|
|
Glib::Mutex::Lock guard (the_lock);
|
2009-11-27 19:49:04 -05:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
2009-11-23 12:33:39 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
namespace boost {
|
|
|
|
|
|
|
|
void sp_scalar_constructor_hook( void * object, std::size_t size, void * pn )
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void sp_scalar_destructor_hook( void * object, std::size_t size, void * pn )
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void sp_counter_ref_hook (void* pn, long use_count)
|
|
|
|
{
|
|
|
|
}
|
2009-11-27 12:30:39 -05:00
|
|
|
|
2009-11-23 12:33:39 -05:00
|
|
|
void sp_counter_release_hook (void* pn, long use_count)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void sp_array_constructor_hook(void * p)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void sp_array_destructor_hook(void * p)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void sp_scalar_constructor_hook(void * p)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void sp_scalar_destructor_hook(void * p)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|