1 //===---------------------SharingPtr.cpp ------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "lldb/Utility/SharingPtr.h"
12 #if defined (ENABLE_SP_LOGGING)
14 // If ENABLE_SP_LOGGING is defined, then log all shared pointer assignements
15 // and allow them to be queried using a pointer by a call to:
19 #include "lldb/Host/Mutex.h"
38 std::vector<void *> m_frames;
42 Backtrace::Backtrace () : m_frames()
46 Backtrace::~Backtrace ()
51 Backtrace::GetFrames ()
54 const int count = ::backtrace (frames, sizeof(frames)/sizeof(void*));
56 m_frames.assign (frames + 2, frames + (count - 2));
60 Backtrace::Dump () const
62 if (!m_frames.empty())
63 ::backtrace_symbols_fd (m_frames.data(), m_frames.size(), STDOUT_FILENO);
64 write (STDOUT_FILENO, "\n\n", 2);
67 extern "C" void track_sp (void *sp_this, void *ptr, long use_count)
69 typedef std::pair<void *, Backtrace> PtrBacktracePair;
70 typedef std::map<void *, PtrBacktracePair> PtrToBacktraceMap;
71 static lldb_private::Mutex g_mutex(lldb_private::Mutex::eMutexTypeNormal);
72 lldb_private::Mutex::Locker locker (g_mutex);
73 static PtrToBacktraceMap g_map;
77 printf ("sp(%p) -> %p %lu\n", sp_this, ptr, use_count);
83 g_map[sp_this] = std::make_pair(ptr, bt);
87 g_map.erase (sp_this);
93 printf ("Searching for shared pointers that are tracking %p: ", ptr);
95 printf ("Dump all live shared pointres: ");
98 PtrToBacktraceMap::iterator pos, end = g_map.end();
99 for (pos = g_map.begin(); pos != end; ++pos)
101 if (ptr == NULL || pos->second.first == ptr)
104 printf ("\nsp(%p): %p\n", pos->first, pos->second.first);
105 pos->second.second.Dump();
114 // Put dump_sp_refs in the lldb namespace to it gets through our exports lists filter in the LLDB.framework or lldb.so
117 void dump_sp_refs (void *ptr)
119 // Use a specially crafted call to "track_sp" which will
120 // dump info on all live shared pointers that reference "ptr"
121 track_sp (NULL, ptr, 0);
128 namespace lldb_private {
134 shared_count::~shared_count()
139 shared_count::add_shared()
141 increment(shared_owners_);
145 shared_count::release_shared()
147 if (decrement(shared_owners_) == -1)