blob: 36c79b4b1f33b0004f7bc7a253eab1c0746b0e05 [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}
114extern "C" void dump_sp_refs (void *ptr)
115{
116 // Use a specially crafted call to "track_sp" which will
117 // dump info on all live shared pointers that reference "ptr"
118 track_sp (NULL, ptr, 0);
119}
120
121#endif
122
Greg Clayton6a5aa8a2010-09-24 23:07:41 +0000123namespace lldb_private {
Greg Claytone49f79d2010-06-12 17:45:57 +0000124
125namespace imp
126{
127
Greg Clayton6e0101c2011-09-17 06:21:20 +0000128
129 shared_count::~shared_count()
130 {
131 }
132
133 void
134 shared_count::add_shared()
135 {
136 increment(shared_owners_);
137 }
138
139 void
140 shared_count::release_shared()
141 {
142 if (decrement(shared_owners_) == -1)
143 {
144 on_zero_shared();
145 delete this;
146 }
147 }
Greg Claytone49f79d2010-06-12 17:45:57 +0000148
149} // imp
150
Greg Clayton6e0101c2011-09-17 06:21:20 +0000151
Greg Claytone49f79d2010-06-12 17:45:57 +0000152} // namespace lldb
Greg Clayton6e0101c2011-09-17 06:21:20 +0000153