summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGreg Clayton <gclayton@apple.com>2012-01-19 04:44:00 +0000
committerGreg Clayton <gclayton@apple.com>2012-01-19 04:44:00 +0000
commitbdf3a01bb0d2f612acb2383a91c26142e18e5490 (patch)
tree5b2f4f3c5bd7bde176b2e50e74a4ccc3d1cf1941
parente42ae8497f1863087e32f3325f46d02960f9b5c2 (diff)
downloadbcm5719-llvm-bdf3a01bb0d2f612acb2383a91c26142e18e5490.tar.gz
bcm5719-llvm-bdf3a01bb0d2f612acb2383a91c26142e18e5490.zip
Allow a way to track all allocations for our intrusive ref counted pointers.
It is disabled by default, but can be enabled to track down shared pointer cycles. llvm-svn: 148461
-rw-r--r--lldb/include/lldb/Utility/SharingPtr.h23
-rw-r--r--lldb/source/Utility/SharingPtr.cpp111
2 files changed, 133 insertions, 1 deletions
diff --git a/lldb/include/lldb/Utility/SharingPtr.h b/lldb/include/lldb/Utility/SharingPtr.h
index 5a3197aeeb5..57c980659ab 100644
--- a/lldb/include/lldb/Utility/SharingPtr.h
+++ b/lldb/include/lldb/Utility/SharingPtr.h
@@ -13,6 +13,13 @@
#include <algorithm>
#include <memory>
+//#define ENABLE_SP_LOGGING 1 // DON'T CHECK THIS LINE IN UNLESS COMMENTED OUT
+#if defined (ENABLE_SP_LOGGING)
+
+extern "C" void track_sp (void *sp_this, void *ptr, long count);
+
+#endif
+
namespace lldb_private {
namespace imp {
@@ -706,6 +713,10 @@ public:
swap (IntrusiveSharingPtr& rhs)
{
std::swap(ptr_, rhs.ptr_);
+#if defined (ENABLE_SP_LOGGING)
+ track_sp (this, ptr_, use_count());
+ track_sp (&rhs, rhs.ptr_, rhs.use_count());
+#endif
}
void
@@ -730,18 +741,28 @@ public:
private:
element_type *ptr_;
-
+
void
add_shared()
{
if (ptr_)
+ {
ptr_->add_shared();
+#if defined (ENABLE_SP_LOGGING)
+ track_sp (this, ptr_, ptr_->use_count());
+#endif
+ }
}
void
release_shared()
{
if (ptr_)
+ {
+#if defined (ENABLE_SP_LOGGING)
+ track_sp (this, NULL, ptr_->use_count() - 1);
+#endif
ptr_->release_shared();
+ }
}
};
diff --git a/lldb/source/Utility/SharingPtr.cpp b/lldb/source/Utility/SharingPtr.cpp
index 26c8d1ffd88..36c79b4b1f3 100644
--- a/lldb/source/Utility/SharingPtr.cpp
+++ b/lldb/source/Utility/SharingPtr.cpp
@@ -9,6 +9,117 @@
#include "lldb/Utility/SharingPtr.h"
+#if defined (ENABLE_SP_LOGGING)
+
+// If ENABLE_SP_LOGGING is defined, then log all shared pointer assignements
+// and allow them to be queried using a pointer by a call to:
+#include <execinfo.h>
+#include <map>
+#include <assert.h>
+#include "lldb/Host/Mutex.h"
+
+#include <vector>
+
+class Backtrace
+{
+public:
+ Backtrace ();
+
+ ~Backtrace ();
+
+ void
+ GetFrames ();
+
+ void
+ Dump () const;
+
+private:
+ void *m_sp_this;
+ std::vector<void *> m_frames;
+};
+
+
+Backtrace::Backtrace () : m_frames()
+{
+}
+
+Backtrace::~Backtrace ()
+{
+}
+
+void
+Backtrace::GetFrames ()
+{
+ void *frames[1024];
+ const int count = ::backtrace (frames, sizeof(frames)/sizeof(void*));
+ if (count > 2)
+ m_frames.assign (frames + 2, frames + (count - 2));
+}
+
+void
+Backtrace::Dump () const
+{
+ if (!m_frames.empty())
+ ::backtrace_symbols_fd (m_frames.data(), m_frames.size(), STDOUT_FILENO);
+ write (STDOUT_FILENO, "\n\n", 2);
+}
+
+extern "C" void track_sp (void *sp_this, void *ptr, long use_count)
+{
+ typedef std::pair<void *, Backtrace> PtrBacktracePair;
+ typedef std::map<void *, PtrBacktracePair> PtrToBacktraceMap;
+ static lldb_private::Mutex g_mutex(lldb_private::Mutex::eMutexTypeNormal);
+ lldb_private::Mutex::Locker locker (g_mutex);
+ static PtrToBacktraceMap g_map;
+
+ if (sp_this)
+ {
+ printf ("sp(%p) -> %p %lu\n", sp_this, ptr, use_count);
+
+ if (ptr)
+ {
+ Backtrace bt;
+ bt.GetFrames();
+ g_map[sp_this] = std::make_pair(ptr, bt);
+ }
+ else
+ {
+ g_map.erase (sp_this);
+ }
+ }
+ else
+ {
+ if (ptr)
+ printf ("Searching for shared pointers that are tracking %p: ", ptr);
+ else
+ printf ("Dump all live shared pointres: ");
+
+ uint32_t matches = 0;
+ PtrToBacktraceMap::iterator pos, end = g_map.end();
+ for (pos = g_map.begin(); pos != end; ++pos)
+ {
+ if (ptr == NULL || pos->second.first == ptr)
+ {
+ ++matches;
+ printf ("\nsp(%p): %p\n", pos->first, pos->second.first);
+ pos->second.second.Dump();
+ }
+ }
+ if (matches == 0)
+ {
+ printf ("none.\n");
+ }
+ }
+}
+extern "C" void dump_sp_refs (void *ptr)
+{
+ // Use a specially crafted call to "track_sp" which will
+ // dump info on all live shared pointers that reference "ptr"
+ track_sp (NULL, ptr, 0);
+}
+
+#endif
+
namespace lldb_private {
namespace imp
OpenPOWER on IntegriCloud