blob: 7f1278567ff114e3ba2ab206aa482e2fd1846387 [file] [log] [blame]
Greg Claytonef59f822010-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 Friedman81ad7262010-06-12 18:29:53 +000010#include "lldb/Utility/SharingPtr.h"
Greg Claytonef59f822010-06-12 17:45:57 +000011
Greg Claytonbdf3a012012-01-19 04:44:00 +000012#if defined (ENABLE_SP_LOGGING)
13
Bruce Mitchenere171da52015-07-22 00:16:02 +000014// If ENABLE_SP_LOGGING is defined, then log all shared pointer assignments
Greg Claytonbdf3a012012-01-19 04:44:00 +000015// 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
Saleem Abdulrasool28606952014-06-27 05:17:41 +000021#include "llvm/ADT/STLExtras.h"
22
Greg Claytonbdf3a012012-01-19 04:44:00 +000023#include <vector>
24
25class Backtrace
26{
27public:
28 Backtrace ();
29
30 ~Backtrace ();
31
32 void
33 GetFrames ();
34
35 void
36 Dump () const;
37
38private:
39 void *m_sp_this;
40 std::vector<void *> m_frames;
41};
42
43
44Backtrace::Backtrace () : m_frames()
45{
46}
47
48Backtrace::~Backtrace ()
49{
50}
51
52void
53Backtrace::GetFrames ()
54{
55 void *frames[1024];
Saleem Abdulrasool28606952014-06-27 05:17:41 +000056 const int count = ::backtrace (frames, llvm::array_lengthof(frames));
Greg Claytonbdf3a012012-01-19 04:44:00 +000057 if (count > 2)
58 m_frames.assign (frames + 2, frames + (count - 2));
59}
60
61void
62Backtrace::Dump () const
63{
64 if (!m_frames.empty())
65 ::backtrace_symbols_fd (m_frames.data(), m_frames.size(), STDOUT_FILENO);
66 write (STDOUT_FILENO, "\n\n", 2);
67}
68
69extern "C" void track_sp (void *sp_this, void *ptr, long use_count)
70{
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;
76
77 if (sp_this)
78 {
79 printf ("sp(%p) -> %p %lu\n", sp_this, ptr, use_count);
80
81 if (ptr)
82 {
83 Backtrace bt;
84 bt.GetFrames();
85 g_map[sp_this] = std::make_pair(ptr, bt);
86 }
87 else
88 {
89 g_map.erase (sp_this);
90 }
91 }
92 else
93 {
94 if (ptr)
95 printf ("Searching for shared pointers that are tracking %p: ", ptr);
96 else
97 printf ("Dump all live shared pointres: ");
98
99 uint32_t matches = 0;
100 PtrToBacktraceMap::iterator pos, end = g_map.end();
101 for (pos = g_map.begin(); pos != end; ++pos)
102 {
103 if (ptr == NULL || pos->second.first == ptr)
104 {
105 ++matches;
106 printf ("\nsp(%p): %p\n", pos->first, pos->second.first);
107 pos->second.second.Dump();
108 }
109 }
110 if (matches == 0)
111 {
112 printf ("none.\n");
113 }
114 }
115}
Greg Clayton29ad7b92012-01-27 18:45:39 +0000116// Put dump_sp_refs in the lldb namespace to it gets through our exports lists filter in the LLDB.framework or lldb.so
117namespace lldb {
118
119 void dump_sp_refs (void *ptr)
120 {
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);
124 }
125
Greg Claytonbdf3a012012-01-19 04:44:00 +0000126}
127
128#endif
129
Greg Clayton5573fde2010-09-24 23:07:41 +0000130namespace lldb_private {
Greg Claytonef59f822010-06-12 17:45:57 +0000131
132namespace imp
133{
134
Greg Clayton747bcb02011-09-17 06:21:20 +0000135
136 shared_count::~shared_count()
137 {
138 }
139
140 void
141 shared_count::add_shared()
142 {
Enrico Granatafcb37ae2013-10-18 18:57:49 +0000143#ifdef _MSC_VER
144 _InterlockedIncrement(&shared_owners_);
145#else
146 ++shared_owners_;
147#endif
Greg Clayton747bcb02011-09-17 06:21:20 +0000148 }
149
150 void
151 shared_count::release_shared()
152 {
Enrico Granatafcb37ae2013-10-18 18:57:49 +0000153#ifdef _MSC_VER
154 if (_InterlockedDecrement(&shared_owners_) == -1)
155#else
156 if (--shared_owners_ == -1)
157#endif
Greg Clayton747bcb02011-09-17 06:21:20 +0000158 {
159 on_zero_shared();
160 delete this;
161 }
162 }
Greg Claytonef59f822010-06-12 17:45:57 +0000163
164} // imp
165
Greg Clayton747bcb02011-09-17 06:21:20 +0000166
Greg Claytonef59f822010-06-12 17:45:57 +0000167} // namespace lldb
Greg Clayton747bcb02011-09-17 06:21:20 +0000168