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 "llvm/ADT/STLExtras.h"
37 std::vector<void *> m_frames;
40 Backtrace::Backtrace() : m_frames() {}
42 Backtrace::~Backtrace() {}
44 void Backtrace::GetFrames() {
46 const int count = ::backtrace(frames, llvm::array_lengthof(frames));
48 m_frames.assign(frames + 2, frames + (count - 2));
51 void Backtrace::Dump() const {
52 if (!m_frames.empty())
53 ::backtrace_symbols_fd(m_frames.data(), m_frames.size(), STDOUT_FILENO);
54 write(STDOUT_FILENO, "\n\n", 2);
57 extern "C" void track_sp(void *sp_this, void *ptr, long use_count) {
58 typedef std::pair<void *, Backtrace> PtrBacktracePair;
59 typedef std::map<void *, PtrBacktracePair> PtrToBacktraceMap;
60 static std::mutex g_mutex;
61 std::lock_guard<std::mutex> guard(g_mutex);
62 static PtrToBacktraceMap g_map;
65 printf("sp(%p) -> %p %lu\n", sp_this, ptr, use_count);
70 g_map[sp_this] = std::make_pair(ptr, bt);
76 printf("Searching for shared pointers that are tracking %p: ", ptr);
78 printf("Dump all live shared pointres: ");
81 PtrToBacktraceMap::iterator pos, end = g_map.end();
82 for (pos = g_map.begin(); pos != end; ++pos) {
83 if (ptr == NULL || pos->second.first == ptr) {
85 printf("\nsp(%p): %p\n", pos->first, pos->second.first);
86 pos->second.second.Dump();
94 // Put dump_sp_refs in the lldb namespace to it gets through our exports lists
95 // filter in the LLDB.framework or lldb.so
98 void dump_sp_refs(void *ptr) {
99 // Use a specially crafted call to "track_sp" which will
100 // dump info on all live shared pointers that reference "ptr"
101 track_sp(NULL, ptr, 0);
107 namespace lldb_private {
111 shared_count::~shared_count() {}
113 void shared_count::add_shared() {
115 _InterlockedIncrement(&shared_owners_);
121 void shared_count::release_shared() {
123 if (_InterlockedDecrement(&shared_owners_) == -1)
125 if (--shared_owners_ == -1)