blob: f64d7e3995b82564a12d6ab724e79f626dc3fdc4 [file] [log] [blame]
Greg Claytone49f79d2010-06-12 17:45:57 +00001//===---------------------SharingPtr.cpp ------------------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
Eli Friedman17b71362010-06-12 18:29:53 +000010#include "lldb/Utility/SharingPtr.h"
Greg Claytone49f79d2010-06-12 17:45:57 +000011
Greg Clayton666c5532012-01-19 04:44:00 +000012#if defined (ENABLE_SP_LOGGING)
13
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:
16#include <execinfo.h>
17#include <map>
18#include <assert.h>
19#include "lldb/Host/Mutex.h"
20
21#include <vector>
22
23class Backtrace
24{
25public:
26 Backtrace ();
27
28 ~Backtrace ();
29
30 void
31 GetFrames ();
32
33 void
34 Dump () const;
35
36private:
37 void *m_sp_this;
38 std::vector<void *> m_frames;
39};
40
41
42Backtrace::Backtrace () : m_frames()
43{
44}
45
46Backtrace::~Backtrace ()
47{
48}
49
50void
51Backtrace::GetFrames ()
52{
53 void *frames[1024];
54 const int count = ::backtrace (frames, sizeof(frames)/sizeof(void*));
55 if (count > 2)
56 m_frames.assign (frames + 2, frames + (count - 2));
57}
58
59void
60Backtrace::Dump () const
61{
62 if (!m_frames.empty())
63 ::backtrace_symbols_fd (m_frames.data(), m_frames.size(), STDOUT_FILENO);
64 write (STDOUT_FILENO, "\n\n", 2);
65}
66
67extern "C" void track_sp (void *sp_this, void *ptr, long use_count)
68{
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;
74
75 if (sp_this)
76 {
77 printf ("sp(%p) -> %p %lu\n", sp_this, ptr, use_count);
78
79 if (ptr)
80 {
81 Backtrace bt;
82 bt.GetFrames();
83 g_map[sp_this] = std::make_pair(ptr, bt);
84 }
85 else
86 {
87 g_map.erase (sp_this);
88 }
89 }
90 else
91 {
92 if (ptr)
93 printf ("Searching for shared pointers that are tracking %p: ", ptr);
94 else
95 printf ("Dump all live shared pointres: ");
96
97 uint32_t matches = 0;
98 PtrToBacktraceMap::iterator pos, end = g_map.end();
99 for (pos = g_map.begin(); pos != end; ++pos)
100 {
101 if (ptr == NULL || pos->second.first == ptr)
102 {
103 ++matches;
104 printf ("\nsp(%p): %p\n", pos->first, pos->second.first);
105 pos->second.second.Dump();
106 }
107 }
108 if (matches == 0)
109 {
110 printf ("none.\n");
111 }
112 }
113}
Greg Clayton08205a62012-01-27 18:45:39 +0000114// Put dump_sp_refs in the lldb namespace to it gets through our exports lists filter in the LLDB.framework or lldb.so
115namespace lldb {
116
117 void dump_sp_refs (void *ptr)
118 {
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);
122 }
123
Greg Clayton666c5532012-01-19 04:44:00 +0000124}
125
126#endif
127
Greg Clayton6a5aa8a2010-09-24 23:07:41 +0000128namespace lldb_private {
Greg Claytone49f79d2010-06-12 17:45:57 +0000129
130namespace imp
131{
132
Greg Clayton6e0101c2011-09-17 06:21:20 +0000133
134 shared_count::~shared_count()
135 {
136 }
137
138 void
139 shared_count::add_shared()
140 {
141 increment(shared_owners_);
142 }
143
144 void
145 shared_count::release_shared()
146 {
147 if (decrement(shared_owners_) == -1)
148 {
149 on_zero_shared();
150 delete this;
151 }
152 }
Greg Claytone49f79d2010-06-12 17:45:57 +0000153
154} // imp
155
Greg Clayton6e0101c2011-09-17 06:21:20 +0000156
Greg Claytone49f79d2010-06-12 17:45:57 +0000157} // namespace lldb
Greg Clayton6e0101c2011-09-17 06:21:20 +0000158