Greg Clayton | ef59f82 | 2010-06-12 17:45:57 +0000 | [diff] [blame] | 1 | //===---------------------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 Friedman | 81ad726 | 2010-06-12 18:29:53 +0000 | [diff] [blame] | 10 | #include "lldb/Utility/SharingPtr.h" |
Greg Clayton | ef59f82 | 2010-06-12 17:45:57 +0000 | [diff] [blame] | 11 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 12 | #if defined(ENABLE_SP_LOGGING) |
Greg Clayton | bdf3a01 | 2012-01-19 04:44:00 +0000 | [diff] [blame] | 13 | |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 14 | // If ENABLE_SP_LOGGING is defined, then log all shared pointer assignments and |
| 15 | // allow them to be queried using a pointer by a call to: |
Greg Clayton | bdf3a01 | 2012-01-19 04:44:00 +0000 | [diff] [blame] | 16 | #include <assert.h> |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 17 | #include <execinfo.h> |
Greg Clayton | bdf3a01 | 2012-01-19 04:44:00 +0000 | [diff] [blame] | 18 | |
Saleem Abdulrasool | 2860695 | 2014-06-27 05:17:41 +0000 | [diff] [blame] | 19 | #include "llvm/ADT/STLExtras.h" |
| 20 | |
Saleem Abdulrasool | 16ff860 | 2016-05-18 01:59:10 +0000 | [diff] [blame] | 21 | #include <map> |
| 22 | #include <mutex> |
Greg Clayton | bdf3a01 | 2012-01-19 04:44:00 +0000 | [diff] [blame] | 23 | #include <vector> |
| 24 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 25 | class Backtrace { |
Greg Clayton | bdf3a01 | 2012-01-19 04:44:00 +0000 | [diff] [blame] | 26 | public: |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 27 | Backtrace(); |
| 28 | |
| 29 | ~Backtrace(); |
| 30 | |
| 31 | void GetFrames(); |
| 32 | |
| 33 | void Dump() const; |
| 34 | |
Greg Clayton | bdf3a01 | 2012-01-19 04:44:00 +0000 | [diff] [blame] | 35 | private: |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 36 | void *m_sp_this; |
| 37 | std::vector<void *> m_frames; |
Greg Clayton | bdf3a01 | 2012-01-19 04:44:00 +0000 | [diff] [blame] | 38 | }; |
| 39 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 40 | Backtrace::Backtrace() : m_frames() {} |
Greg Clayton | bdf3a01 | 2012-01-19 04:44:00 +0000 | [diff] [blame] | 41 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 42 | Backtrace::~Backtrace() {} |
| 43 | |
| 44 | void Backtrace::GetFrames() { |
| 45 | void *frames[1024]; |
| 46 | const int count = ::backtrace(frames, llvm::array_lengthof(frames)); |
| 47 | if (count > 2) |
| 48 | m_frames.assign(frames + 2, frames + (count - 2)); |
Greg Clayton | bdf3a01 | 2012-01-19 04:44:00 +0000 | [diff] [blame] | 49 | } |
| 50 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 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); |
Greg Clayton | bdf3a01 | 2012-01-19 04:44:00 +0000 | [diff] [blame] | 55 | } |
| 56 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 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; |
Greg Clayton | bdf3a01 | 2012-01-19 04:44:00 +0000 | [diff] [blame] | 63 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 64 | if (sp_this) { |
| 65 | printf("sp(%p) -> %p %lu\n", sp_this, ptr, use_count); |
Greg Clayton | bdf3a01 | 2012-01-19 04:44:00 +0000 | [diff] [blame] | 66 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 67 | if (ptr) { |
| 68 | Backtrace bt; |
| 69 | bt.GetFrames(); |
| 70 | g_map[sp_this] = std::make_pair(ptr, bt); |
| 71 | } else { |
| 72 | g_map.erase(sp_this); |
Greg Clayton | bdf3a01 | 2012-01-19 04:44:00 +0000 | [diff] [blame] | 73 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 74 | } else { |
| 75 | if (ptr) |
| 76 | printf("Searching for shared pointers that are tracking %p: ", ptr); |
| 77 | else |
| 78 | printf("Dump all live shared pointres: "); |
Greg Clayton | bdf3a01 | 2012-01-19 04:44:00 +0000 | [diff] [blame] | 79 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 80 | uint32_t matches = 0; |
| 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) { |
| 84 | ++matches; |
| 85 | printf("\nsp(%p): %p\n", pos->first, pos->second.first); |
| 86 | pos->second.second.Dump(); |
| 87 | } |
Greg Clayton | bdf3a01 | 2012-01-19 04:44:00 +0000 | [diff] [blame] | 88 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 89 | if (matches == 0) { |
| 90 | printf("none.\n"); |
| 91 | } |
| 92 | } |
Greg Clayton | bdf3a01 | 2012-01-19 04:44:00 +0000 | [diff] [blame] | 93 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 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 |
Greg Clayton | 29ad7b9 | 2012-01-27 18:45:39 +0000 | [diff] [blame] | 96 | namespace lldb { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 97 | |
| 98 | void dump_sp_refs(void *ptr) { |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 99 | // Use a specially crafted call to "track_sp" which will dump info on all |
| 100 | // live shared pointers that reference "ptr" |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 101 | track_sp(NULL, ptr, 0); |
| 102 | } |
Greg Clayton | bdf3a01 | 2012-01-19 04:44:00 +0000 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | #endif |
| 106 | |
Greg Clayton | 5573fde | 2010-09-24 23:07:41 +0000 | [diff] [blame] | 107 | namespace lldb_private { |
Greg Clayton | ef59f82 | 2010-06-12 17:45:57 +0000 | [diff] [blame] | 108 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 109 | namespace imp { |
Greg Clayton | ef59f82 | 2010-06-12 17:45:57 +0000 | [diff] [blame] | 110 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 111 | shared_count::~shared_count() {} |
Greg Clayton | 747bcb0 | 2011-09-17 06:21:20 +0000 | [diff] [blame] | 112 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 113 | void shared_count::add_shared() { |
Enrico Granata | fcb37ae | 2013-10-18 18:57:49 +0000 | [diff] [blame] | 114 | #ifdef _MSC_VER |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 115 | _InterlockedIncrement(&shared_owners_); |
Enrico Granata | fcb37ae | 2013-10-18 18:57:49 +0000 | [diff] [blame] | 116 | #else |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 117 | ++shared_owners_; |
Enrico Granata | fcb37ae | 2013-10-18 18:57:49 +0000 | [diff] [blame] | 118 | #endif |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 119 | } |
Greg Clayton | 747bcb0 | 2011-09-17 06:21:20 +0000 | [diff] [blame] | 120 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 121 | void shared_count::release_shared() { |
Enrico Granata | fcb37ae | 2013-10-18 18:57:49 +0000 | [diff] [blame] | 122 | #ifdef _MSC_VER |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 123 | if (_InterlockedDecrement(&shared_owners_) == -1) |
Enrico Granata | fcb37ae | 2013-10-18 18:57:49 +0000 | [diff] [blame] | 124 | #else |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 125 | if (--shared_owners_ == -1) |
Enrico Granata | fcb37ae | 2013-10-18 18:57:49 +0000 | [diff] [blame] | 126 | #endif |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 127 | { |
| 128 | on_zero_shared(); |
| 129 | delete this; |
| 130 | } |
| 131 | } |
Greg Clayton | ef59f82 | 2010-06-12 17:45:57 +0000 | [diff] [blame] | 132 | |
| 133 | } // imp |
| 134 | |
| 135 | } // namespace lldb |