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 assignments
15 // and allow them to be queried using a pointer by a call to:
19 #include "lldb/Host/Mutex.h"
21 #include "llvm/ADT/STLExtras.h"
40 std::vector<void *> m_frames;
44 Backtrace::Backtrace () : m_frames()
48 Backtrace::~Backtrace ()
53 Backtrace::GetFrames ()
56 const int count = ::backtrace (frames, llvm::array_lengthof(frames));
58 m_frames.assign (frames + 2, frames + (count - 2));
62 Backtrace::Dump () const
64 if (!m_frames.empty())
65 ::backtrace_symbols_fd (m_frames.data(), m_frames.size(), STDOUT_FILENO);
66 write (STDOUT_FILENO, "\n\n", 2);
69 extern "C" void track_sp (void *sp_this, void *ptr, long use_count)
71 typedef std::pair<void *, Backtrace> PtrBacktracePair;
72 typedef std::map<void *, PtrBacktracePair> PtrToBacktraceMap;
73 static lldb_private::Mutex g_mutex(lldb_private::Mutex::eMutexTypeNormal);
74 lldb_private::Mutex::Locker locker (g_mutex);
75 static PtrToBacktraceMap g_map;
79 printf ("sp(%p) -> %p %lu\n", sp_this, ptr, use_count);
85 g_map[sp_this] = std::make_pair(ptr, bt);
89 g_map.erase (sp_this);
95 printf ("Searching for shared pointers that are tracking %p: ", ptr);
97 printf ("Dump all live shared pointres: ");
100 PtrToBacktraceMap::iterator pos, end = g_map.end();
101 for (pos = g_map.begin(); pos != end; ++pos)
103 if (ptr == NULL || pos->second.first == ptr)
106 printf ("\nsp(%p): %p\n", pos->first, pos->second.first);
107 pos->second.second.Dump();
116 // Put dump_sp_refs in the lldb namespace to it gets through our exports lists filter in the LLDB.framework or lldb.so
119 void dump_sp_refs (void *ptr)
121 // Use a specially crafted call to "track_sp" which will
122 // dump info on all live shared pointers that reference "ptr"
123 track_sp (NULL, ptr, 0);
130 namespace lldb_private {
136 shared_count::~shared_count()
141 shared_count::add_shared()
144 _InterlockedIncrement(&shared_owners_);
151 shared_count::release_shared()
154 if (_InterlockedDecrement(&shared_owners_) == -1)
156 if (--shared_owners_ == -1)