diff --git a/lldb/cmake/modules/LLDBFramework.cmake b/lldb/cmake/modules/LLDBFramework.cmake --- a/lldb/cmake/modules/LLDBFramework.cmake +++ b/lldb/cmake/modules/LLDBFramework.cmake @@ -59,8 +59,7 @@ set(lldb_header_staging ${CMAKE_CURRENT_BINARY_DIR}/FrameworkHeaders) foreach(header ${public_headers} - ${root_public_headers} - ${LLDB_SOURCE_DIR}/include/lldb/Utility/SharingPtr.h) + ${root_public_headers}) get_filename_component(basename ${header} NAME) set(staged_header ${lldb_header_staging}/${basename}) diff --git a/lldb/include/lldb/Core/ValueObject.h b/lldb/include/lldb/Core/ValueObject.h --- a/lldb/include/lldb/Core/ValueObject.h +++ b/lldb/include/lldb/Core/ValueObject.h @@ -902,7 +902,7 @@ // Use this constructor to create a "root variable object". The ValueObject // will be locked to this context through-out its lifespan. - ValueObject(ExecutionContextScope *exe_scope, + ValueObject(ExecutionContextScope *exe_scope, ValueObjectManager &manager, AddressType child_ptr_or_ref_addr_type = eAddressTypeLoad); // Use this constructor to create a ValueObject owned by another ValueObject. diff --git a/lldb/include/lldb/Core/ValueObjectConstResult.h b/lldb/include/lldb/Core/ValueObjectConstResult.h --- a/lldb/include/lldb/Core/ValueObjectConstResult.h +++ b/lldb/include/lldb/Core/ValueObjectConstResult.h @@ -121,30 +121,34 @@ friend class ValueObjectConstResultImpl; ValueObjectConstResult(ExecutionContextScope *exe_scope, + ValueObjectManager &manager, lldb::ByteOrder byte_order, uint32_t addr_byte_size, lldb::addr_t address); ValueObjectConstResult(ExecutionContextScope *exe_scope, - const CompilerType &compiler_type, - ConstString name, const DataExtractor &data, - lldb::addr_t address); + ValueObjectManager &manager, + const CompilerType &compiler_type, ConstString name, + const DataExtractor &data, lldb::addr_t address); ValueObjectConstResult(ExecutionContextScope *exe_scope, - const CompilerType &compiler_type, - ConstString name, + ValueObjectManager &manager, + const CompilerType &compiler_type, ConstString name, const lldb::DataBufferSP &result_data_sp, lldb::ByteOrder byte_order, uint32_t addr_size, lldb::addr_t address); ValueObjectConstResult(ExecutionContextScope *exe_scope, - const CompilerType &compiler_type, - ConstString name, lldb::addr_t address, - AddressType address_type, uint32_t addr_byte_size); + ValueObjectManager &manager, + const CompilerType &compiler_type, ConstString name, + lldb::addr_t address, AddressType address_type, + uint32_t addr_byte_size); - ValueObjectConstResult(ExecutionContextScope *exe_scope, const Value &value, + ValueObjectConstResult(ExecutionContextScope *exe_scope, + ValueObjectManager &manager, const Value &value, ConstString name, Module *module = nullptr); - ValueObjectConstResult(ExecutionContextScope *exe_scope, const Status &error); + ValueObjectConstResult(ExecutionContextScope *exe_scope, + ValueObjectManager &manager, const Status &error); DISALLOW_COPY_AND_ASSIGN(ValueObjectConstResult); }; diff --git a/lldb/include/lldb/Core/ValueObjectDynamicValue.h b/lldb/include/lldb/Core/ValueObjectDynamicValue.h --- a/lldb/include/lldb/Core/ValueObjectDynamicValue.h +++ b/lldb/include/lldb/Core/ValueObjectDynamicValue.h @@ -14,7 +14,6 @@ #include "lldb/Symbol/CompilerType.h" #include "lldb/Symbol/Type.h" #include "lldb/Utility/ConstString.h" -#include "lldb/Utility/SharingPtr.h" #include "lldb/lldb-defines.h" #include "lldb/lldb-enumerations.h" #include "lldb/lldb-forward.h" diff --git a/lldb/include/lldb/Core/ValueObjectMemory.h b/lldb/include/lldb/Core/ValueObjectMemory.h --- a/lldb/include/lldb/Core/ValueObjectMemory.h +++ b/lldb/include/lldb/Core/ValueObjectMemory.h @@ -64,10 +64,12 @@ CompilerType m_compiler_type; private: - ValueObjectMemory(ExecutionContextScope *exe_scope, llvm::StringRef name, + ValueObjectMemory(ExecutionContextScope *exe_scope, + ValueObjectManager &manager, llvm::StringRef name, const Address &address, lldb::TypeSP &type_sp); - ValueObjectMemory(ExecutionContextScope *exe_scope, llvm::StringRef name, + ValueObjectMemory(ExecutionContextScope *exe_scope, + ValueObjectManager &manager, llvm::StringRef name, const Address &address, const CompilerType &ast_type); // For ValueObject only DISALLOW_COPY_AND_ASSIGN(ValueObjectMemory); diff --git a/lldb/include/lldb/Core/ValueObjectRegister.h b/lldb/include/lldb/Core/ValueObjectRegister.h --- a/lldb/include/lldb/Core/ValueObjectRegister.h +++ b/lldb/include/lldb/Core/ValueObjectRegister.h @@ -69,6 +69,7 @@ friend class ValueObjectRegisterContext; ValueObjectRegisterSet(ExecutionContextScope *exe_scope, + ValueObjectManager &manager, lldb::RegisterContextSP ®_ctx_sp, uint32_t set_idx); // For ValueObject only @@ -123,6 +124,7 @@ ValueObjectRegister(ValueObject &parent, lldb::RegisterContextSP ®_ctx_sp, uint32_t reg_num); ValueObjectRegister(ExecutionContextScope *exe_scope, + ValueObjectManager &manager, lldb::RegisterContextSP ®_ctx_sp, uint32_t reg_num); // For ValueObject only diff --git a/lldb/include/lldb/Core/ValueObjectVariable.h b/lldb/include/lldb/Core/ValueObjectVariable.h --- a/lldb/include/lldb/Core/ValueObjectVariable.h +++ b/lldb/include/lldb/Core/ValueObjectVariable.h @@ -77,6 +77,7 @@ private: ValueObjectVariable(ExecutionContextScope *exe_scope, + ValueObjectManager &manager, const lldb::VariableSP &var_sp); // For ValueObject only DISALLOW_COPY_AND_ASSIGN(ValueObjectVariable); diff --git a/lldb/include/lldb/Utility/SharedCluster.h b/lldb/include/lldb/Utility/SharedCluster.h --- a/lldb/include/lldb/Utility/SharedCluster.h +++ b/lldb/include/lldb/Utility/SharedCluster.h @@ -10,46 +10,24 @@ #define utility_SharedCluster_h_ #include "lldb/Utility/LLDBAssert.h" -#include "lldb/Utility/SharingPtr.h" - #include "llvm/ADT/STLExtras.h" #include "llvm/ADT/SmallVector.h" +#include #include namespace lldb_private { -namespace imp { -template -class shared_ptr_refcount : public lldb_private::imp::shared_count { +template +class ClusterManager : public std::enable_shared_from_this> { public: - template - shared_ptr_refcount(Y *in) : shared_count(0), manager(in) {} - - shared_ptr_refcount() : shared_count(0) {} - - ~shared_ptr_refcount() override {} - - void on_zero_shared() override { manager->DecrementRefCount(); } - -private: - T *manager; -}; - -} // namespace imp - -template class ClusterManager { -public: - ClusterManager() : m_objects(), m_external_ref(0), m_mutex() {} + static std::shared_ptr Create() { + return std::shared_ptr(new ClusterManager()); + } ~ClusterManager() { for (T *obj : m_objects) delete obj; - - // Decrement refcount should have been called on this ClusterManager, and - // it should have locked the mutex, now we will unlock it before we destroy - // it... - m_mutex.unlock(); } void ManageObject(T *new_object) { @@ -59,33 +37,20 @@ m_objects.push_back(new_object); } - typename lldb_private::SharingPtr GetSharedPointer(T *desired_object) { - { - std::lock_guard guard(m_mutex); - m_external_ref++; - if (!llvm::is_contained(m_objects, desired_object)) { - lldbassert(false && "object not found in shared cluster when expected"); - desired_object = nullptr; - } + std::shared_ptr GetSharedPointer(T *desired_object) { + std::lock_guard guard(m_mutex); + auto this_sp = this->shared_from_this(); + if (!llvm::is_contained(m_objects, desired_object)) { + lldbassert(false && "object not found in shared cluster when expected"); + desired_object = nullptr; } - return typename lldb_private::SharingPtr( - desired_object, new imp::shared_ptr_refcount(this)); + return {std::move(this_sp), desired_object}; } private: - void DecrementRefCount() { - m_mutex.lock(); - m_external_ref--; - if (m_external_ref == 0) - delete this; - else - m_mutex.unlock(); - } - - friend class imp::shared_ptr_refcount; + ClusterManager() : m_objects(), m_mutex() {} llvm::SmallVector m_objects; - int m_external_ref; std::mutex m_mutex; }; diff --git a/lldb/include/lldb/Utility/SharingPtr.h b/lldb/include/lldb/Utility/SharingPtr.h deleted file mode 100644 --- a/lldb/include/lldb/Utility/SharingPtr.h +++ /dev/null @@ -1,359 +0,0 @@ -//===---------------------SharingPtr.h --------------------------*- C++ -*-===// -// -// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. -// See https://llvm.org/LICENSE.txt for license information. -// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception -// -//===----------------------------------------------------------------------===// - -#ifndef utility_SharingPtr_h_ -#define utility_SharingPtr_h_ - -#include - -// Microsoft Visual C++ currently does not enable std::atomic to work in CLR -// mode - as such we need to "hack around it" for MSVC++ builds only using -// Windows specific intrinsics instead of the C++11 atomic support -#ifdef _MSC_VER -#include -#else -#include -#endif - -#include - - -//#define ENABLE_SP_LOGGING 1 // DON'T CHECK THIS LINE IN UNLESS COMMENTED OUT -#if defined(ENABLE_SP_LOGGING) - -extern "C" void track_sp(void *sp_this, void *ptr, long count); - -#endif - -namespace lldb_private { - -namespace imp { - -class shared_count { - shared_count(const shared_count &) = delete; - shared_count &operator=(const shared_count &) = delete; - -public: - explicit shared_count(long refs = 0) : shared_owners_(refs) {} - - void add_shared(); - void release_shared(); - long use_count() const { return shared_owners_ + 1; } - -protected: -#ifdef _MSC_VER - long shared_owners_; -#else - std::atomic shared_owners_; -#endif - virtual ~shared_count(); - -private: - virtual void on_zero_shared() = 0; -}; - -template class shared_ptr_pointer : public shared_count { - T data_; - -public: - shared_ptr_pointer(T p) : data_(p) {} - -private: - void on_zero_shared() override; - - shared_ptr_pointer(const shared_ptr_pointer &) = delete; - shared_ptr_pointer &operator=(const shared_ptr_pointer &) = delete; -}; - -template void shared_ptr_pointer::on_zero_shared() { - delete data_; -} - -template class shared_ptr_emplace : public shared_count { - T data_; - -public: - shared_ptr_emplace() : data_() {} - - template shared_ptr_emplace(A0 &a0) : data_(a0) {} - - template - shared_ptr_emplace(A0 &a0, A1 &a1) : data_(a0, a1) {} - - template - shared_ptr_emplace(A0 &a0, A1 &a1, A2 &a2) : data_(a0, a1, a2) {} - - template - shared_ptr_emplace(A0 &a0, A1 &a1, A2 &a2, A3 &a3) : data_(a0, a1, a2, a3) {} - - template - shared_ptr_emplace(A0 &a0, A1 &a1, A2 &a2, A3 &a3, A4 &a4) - : data_(a0, a1, a2, a3, a4) {} - -private: - void on_zero_shared() override; - -public: - T *get() { return &data_; } -}; - -template void shared_ptr_emplace::on_zero_shared() {} - -} // namespace imp - -template class SharingPtr { -public: - typedef T element_type; - -private: - element_type *ptr_; - imp::shared_count *cntrl_; - - struct nat { - int for_bool_; - }; - -public: - SharingPtr(); - SharingPtr(std::nullptr_t); - template explicit SharingPtr(Y *p); - template explicit SharingPtr(Y *p, imp::shared_count *ctrl_block); - template SharingPtr(const SharingPtr &r, element_type *p); - SharingPtr(const SharingPtr &r); - template SharingPtr(const SharingPtr &r); - - ~SharingPtr(); - - SharingPtr &operator=(const SharingPtr &r); - template SharingPtr &operator=(const SharingPtr &r); - - void swap(SharingPtr &r); - void reset(); - template void reset(Y *p); - - element_type *get() const { return ptr_; } - element_type &operator*() const { return *ptr_; } - element_type *operator->() const { return ptr_; } - long use_count() const { return cntrl_ ? cntrl_->use_count() : 0; } - bool unique() const { return use_count() == 1; } - bool empty() const { return cntrl_ == nullptr; } - operator nat *() const { return (nat *)get(); } - - static SharingPtr make_shared(); - - template static SharingPtr make_shared(A0 &); - - template static SharingPtr make_shared(A0 &, A1 &); - - template - static SharingPtr make_shared(A0 &, A1 &, A2 &); - - template - static SharingPtr make_shared(A0 &, A1 &, A2 &, A3 &); - - template - static SharingPtr make_shared(A0 &, A1 &, A2 &, A3 &, A4 &); - -private: - template friend class SharingPtr; -}; - -template -inline SharingPtr::SharingPtr() : ptr_(nullptr), cntrl_(nullptr) {} - -template -inline SharingPtr::SharingPtr(std::nullptr_t) - : ptr_(nullptr), cntrl_(nullptr) {} - -template -template -SharingPtr::SharingPtr(Y *p) : ptr_(p), cntrl_(nullptr) { - std::unique_ptr hold(p); - typedef imp::shared_ptr_pointer _CntrlBlk; - cntrl_ = new _CntrlBlk(p); - hold.release(); -} - -template -template -SharingPtr::SharingPtr(Y *p, imp::shared_count *cntrl_block) - : ptr_(p), cntrl_(cntrl_block) {} - -template -template -inline SharingPtr::SharingPtr(const SharingPtr &r, element_type *p) - : ptr_(p), cntrl_(r.cntrl_) { - if (cntrl_) - cntrl_->add_shared(); -} - -template -inline SharingPtr::SharingPtr(const SharingPtr &r) - : ptr_(r.ptr_), cntrl_(r.cntrl_) { - if (cntrl_) - cntrl_->add_shared(); -} - -template -template -inline SharingPtr::SharingPtr(const SharingPtr &r) - : ptr_(r.ptr_), cntrl_(r.cntrl_) { - if (cntrl_) - cntrl_->add_shared(); -} - -template SharingPtr::~SharingPtr() { - if (cntrl_) - cntrl_->release_shared(); -} - -template -inline SharingPtr &SharingPtr::operator=(const SharingPtr &r) { - SharingPtr(r).swap(*this); - return *this; -} - -template -template -inline SharingPtr &SharingPtr::operator=(const SharingPtr &r) { - SharingPtr(r).swap(*this); - return *this; -} - -template inline void SharingPtr::swap(SharingPtr &r) { - std::swap(ptr_, r.ptr_); - std::swap(cntrl_, r.cntrl_); -} - -template inline void SharingPtr::reset() { - SharingPtr().swap(*this); -} - -template template inline void SharingPtr::reset(Y *p) { - SharingPtr(p).swap(*this); -} - -template SharingPtr SharingPtr::make_shared() { - typedef imp::shared_ptr_emplace CntrlBlk; - SharingPtr r; - r.cntrl_ = new CntrlBlk(); - r.ptr_ = static_cast(r.cntrl_)->get(); - return r; -} - -template -template -SharingPtr SharingPtr::make_shared(A0 &a0) { - typedef imp::shared_ptr_emplace CntrlBlk; - SharingPtr r; - r.cntrl_ = new CntrlBlk(a0); - r.ptr_ = static_cast(r.cntrl_)->get(); - return r; -} - -template -template -SharingPtr SharingPtr::make_shared(A0 &a0, A1 &a1) { - typedef imp::shared_ptr_emplace CntrlBlk; - SharingPtr r; - r.cntrl_ = new CntrlBlk(a0, a1); - r.ptr_ = static_cast(r.cntrl_)->get(); - return r; -} - -template -template -SharingPtr SharingPtr::make_shared(A0 &a0, A1 &a1, A2 &a2) { - typedef imp::shared_ptr_emplace CntrlBlk; - SharingPtr r; - r.cntrl_ = new CntrlBlk(a0, a1, a2); - r.ptr_ = static_cast(r.cntrl_)->get(); - return r; -} - -template -template -SharingPtr SharingPtr::make_shared(A0 &a0, A1 &a1, A2 &a2, A3 &a3) { - typedef imp::shared_ptr_emplace CntrlBlk; - SharingPtr r; - r.cntrl_ = new CntrlBlk(a0, a1, a2, a3); - r.ptr_ = static_cast(r.cntrl_)->get(); - return r; -} - -template -template -SharingPtr SharingPtr::make_shared(A0 &a0, A1 &a1, A2 &a2, A3 &a3, - A4 &a4) { - typedef imp::shared_ptr_emplace CntrlBlk; - SharingPtr r; - r.cntrl_ = new CntrlBlk(a0, a1, a2, a3, a4); - r.ptr_ = static_cast(r.cntrl_)->get(); - return r; -} - -template inline SharingPtr make_shared() { - return SharingPtr::make_shared(); -} - -template inline SharingPtr make_shared(A0 &a0) { - return SharingPtr::make_shared(a0); -} - -template -inline SharingPtr make_shared(A0 &a0, A1 &a1) { - return SharingPtr::make_shared(a0, a1); -} - -template -inline SharingPtr make_shared(A0 &a0, A1 &a1, A2 &a2) { - return SharingPtr::make_shared(a0, a1, a2); -} - -template -inline SharingPtr make_shared(A0 &a0, A1 &a1, A2 &a2, A3 &a3) { - return SharingPtr::make_shared(a0, a1, a2, a3); -} - -template -inline SharingPtr make_shared(A0 &a0, A1 &a1, A2 &a2, A3 &a3, A4 &a4) { - return SharingPtr::make_shared(a0, a1, a2, a3, a4); -} - -template -inline bool operator==(const SharingPtr &__x, const SharingPtr &__y) { - return __x.get() == __y.get(); -} - -template -inline bool operator!=(const SharingPtr &__x, const SharingPtr &__y) { - return !(__x == __y); -} - -template -inline bool operator<(const SharingPtr &__x, const SharingPtr &__y) { - return __x.get() < __y.get(); -} - -template inline void swap(SharingPtr &__x, SharingPtr &__y) { - __x.swap(__y); -} - -template -inline SharingPtr static_pointer_cast(const SharingPtr &r) { - return SharingPtr(r, static_cast(r.get())); -} - -template -SharingPtr const_pointer_cast(const SharingPtr &r) { - return SharingPtr(r, const_cast(r.get())); -} - -} // namespace lldb_private - -#endif // utility_SharingPtr_h_ diff --git a/lldb/include/lldb/lldb-forward.h b/lldb/include/lldb/lldb-forward.h --- a/lldb/include/lldb/lldb-forward.h +++ b/lldb/include/lldb/lldb-forward.h @@ -11,7 +11,7 @@ #if defined(__cplusplus) -#include "lldb/Utility/SharingPtr.h" +#include // lldb forward declarations namespace lldb_private { @@ -452,7 +452,7 @@ typedef std::shared_ptr UnwindAssemblySP; typedef std::shared_ptr UnwindPlanSP; typedef std::shared_ptr UtilityFunctionSP; -typedef lldb_private::SharingPtr ValueObjectSP; +typedef std::shared_ptr ValueObjectSP; typedef std::shared_ptr ValueSP; typedef std::shared_ptr ValueListSP; typedef std::shared_ptr VariableSP; diff --git a/lldb/source/Core/FormatEntity.cpp b/lldb/source/Core/FormatEntity.cpp --- a/lldb/source/Core/FormatEntity.cpp +++ b/lldb/source/Core/FormatEntity.cpp @@ -46,7 +46,6 @@ #include "lldb/Utility/Log.h" #include "lldb/Utility/Logging.h" #include "lldb/Utility/RegisterValue.h" -#include "lldb/Utility/SharingPtr.h" #include "lldb/Utility/Stream.h" #include "lldb/Utility/StreamString.h" #include "lldb/Utility/StringList.h" diff --git a/lldb/source/Core/ValueObject.cpp b/lldb/source/Core/ValueObject.cpp --- a/lldb/source/Core/ValueObject.cpp +++ b/lldb/source/Core/ValueObject.cpp @@ -45,7 +45,6 @@ #include "lldb/Utility/Log.h" #include "lldb/Utility/Logging.h" #include "lldb/Utility/Scalar.h" -#include "lldb/Utility/SharingPtr.h" #include "lldb/Utility/Stream.h" #include "lldb/Utility/StreamString.h" #include "lldb/lldb-private-types.h" @@ -105,12 +104,13 @@ // ValueObject constructor ValueObject::ValueObject(ExecutionContextScope *exe_scope, + ValueObjectManager &manager, AddressType child_ptr_or_ref_addr_type) : UserID(++g_value_obj_uid), // Unique identifier for every value object m_parent(nullptr), m_root(nullptr), m_update_point(exe_scope), m_name(), m_data(), m_value(), m_error(), m_value_str(), m_old_value_str(), m_location_str(), m_summary_str(), m_object_desc_str(), - m_manager(), m_children(), m_synthetic_children(), + m_manager(&manager), m_children(), m_synthetic_children(), m_dynamic_value(nullptr), m_synthetic_value(nullptr), m_deref_valobj(nullptr), m_format(eFormatDefault), m_last_format(eFormatDefault), m_last_format_mgr_revision(0), @@ -134,7 +134,6 @@ m_data.SetAddressByteSize(arch.GetAddressByteSize()); } } - m_manager = new ValueObjectManager(); m_manager->ManageObject(this); } diff --git a/lldb/source/Core/ValueObjectConstResult.cpp b/lldb/source/Core/ValueObjectConstResult.cpp --- a/lldb/source/Core/ValueObjectConstResult.cpp +++ b/lldb/source/Core/ValueObjectConstResult.cpp @@ -29,16 +29,18 @@ ByteOrder byte_order, uint32_t addr_byte_size, lldb::addr_t address) { - return (new ValueObjectConstResult(exe_scope, byte_order, addr_byte_size, - address)) + auto manager_sp = ValueObjectManager::Create(); + return (new ValueObjectConstResult(exe_scope, *manager_sp, byte_order, + addr_byte_size, address)) ->GetSP(); } ValueObjectConstResult::ValueObjectConstResult(ExecutionContextScope *exe_scope, + ValueObjectManager &manager, ByteOrder byte_order, uint32_t addr_byte_size, lldb::addr_t address) - : ValueObject(exe_scope), m_type_name(), m_byte_size(0), + : ValueObject(exe_scope, manager), m_type_name(), m_byte_size(0), m_impl(this, address) { SetIsConstant(); SetValueIsValid(true); @@ -52,15 +54,17 @@ ConstString name, const DataExtractor &data, lldb::addr_t address) { - return (new ValueObjectConstResult(exe_scope, compiler_type, name, data, - address)) + auto manager_sp = ValueObjectManager::Create(); + return (new ValueObjectConstResult(exe_scope, *manager_sp, compiler_type, + name, data, address)) ->GetSP(); } ValueObjectConstResult::ValueObjectConstResult( - ExecutionContextScope *exe_scope, const CompilerType &compiler_type, - ConstString name, const DataExtractor &data, lldb::addr_t address) - : ValueObject(exe_scope), m_type_name(), m_byte_size(0), + ExecutionContextScope *exe_scope, ValueObjectManager &manager, + const CompilerType &compiler_type, ConstString name, + const DataExtractor &data, lldb::addr_t address) + : ValueObject(exe_scope, manager), m_type_name(), m_byte_size(0), m_impl(this, address) { m_data = data; @@ -86,8 +90,10 @@ lldb::ByteOrder data_byte_order, uint32_t data_addr_size, lldb::addr_t address) { - return (new ValueObjectConstResult(exe_scope, compiler_type, name, data_sp, - data_byte_order, data_addr_size, address)) + auto manager_sp = ValueObjectManager::Create(); + return (new ValueObjectConstResult(exe_scope, *manager_sp, compiler_type, + name, data_sp, data_byte_order, + data_addr_size, address)) ->GetSP(); } @@ -95,15 +101,18 @@ Value &value, ConstString name, Module *module) { - return (new ValueObjectConstResult(exe_scope, value, name, module))->GetSP(); + auto manager_sp = ValueObjectManager::Create(); + return (new ValueObjectConstResult(exe_scope, *manager_sp, value, name, + module)) + ->GetSP(); } ValueObjectConstResult::ValueObjectConstResult( - ExecutionContextScope *exe_scope, const CompilerType &compiler_type, - ConstString name, const lldb::DataBufferSP &data_sp, - lldb::ByteOrder data_byte_order, uint32_t data_addr_size, - lldb::addr_t address) - : ValueObject(exe_scope), m_type_name(), m_byte_size(0), + ExecutionContextScope *exe_scope, ValueObjectManager &manager, + const CompilerType &compiler_type, ConstString name, + const lldb::DataBufferSP &data_sp, lldb::ByteOrder data_byte_order, + uint32_t data_addr_size, lldb::addr_t address) + : ValueObject(exe_scope, manager), m_type_name(), m_byte_size(0), m_impl(this, address) { m_data.SetByteOrder(data_byte_order); m_data.SetAddressByteSize(data_addr_size); @@ -123,16 +132,18 @@ lldb::addr_t address, AddressType address_type, uint32_t addr_byte_size) { - return (new ValueObjectConstResult(exe_scope, compiler_type, name, address, - address_type, addr_byte_size)) + auto manager_sp = ValueObjectManager::Create(); + return (new ValueObjectConstResult(exe_scope, *manager_sp, compiler_type, + name, address, address_type, + addr_byte_size)) ->GetSP(); } ValueObjectConstResult::ValueObjectConstResult( - ExecutionContextScope *exe_scope, const CompilerType &compiler_type, - ConstString name, lldb::addr_t address, AddressType address_type, - uint32_t addr_byte_size) - : ValueObject(exe_scope), m_type_name(), m_byte_size(0), + ExecutionContextScope *exe_scope, ValueObjectManager &manager, + const CompilerType &compiler_type, ConstString name, lldb::addr_t address, + AddressType address_type, uint32_t addr_byte_size) + : ValueObject(exe_scope, manager), m_type_name(), m_byte_size(0), m_impl(this, address) { m_value.GetScalar() = address; m_data.SetAddressByteSize(addr_byte_size); @@ -161,21 +172,25 @@ ValueObjectSP ValueObjectConstResult::Create(ExecutionContextScope *exe_scope, const Status &error) { - return (new ValueObjectConstResult(exe_scope, error))->GetSP(); + auto manager_sp = ValueObjectManager::Create(); + return (new ValueObjectConstResult(exe_scope, *manager_sp, error))->GetSP(); } ValueObjectConstResult::ValueObjectConstResult(ExecutionContextScope *exe_scope, + ValueObjectManager &manager, const Status &error) - : ValueObject(exe_scope), m_type_name(), m_byte_size(0), m_impl(this) { + : ValueObject(exe_scope, manager), m_type_name(), m_byte_size(0), + m_impl(this) { m_error = error; SetIsConstant(); } ValueObjectConstResult::ValueObjectConstResult(ExecutionContextScope *exe_scope, + ValueObjectManager &manager, const Value &value, - ConstString name, - Module *module) - : ValueObject(exe_scope), m_type_name(), m_byte_size(0), m_impl(this) { + ConstString name, Module *module) + : ValueObject(exe_scope, manager), m_type_name(), m_byte_size(0), + m_impl(this) { m_value = value; m_name = name; ExecutionContext exe_ctx; diff --git a/lldb/source/Core/ValueObjectConstResultImpl.cpp b/lldb/source/Core/ValueObjectConstResultImpl.cpp --- a/lldb/source/Core/ValueObjectConstResultImpl.cpp +++ b/lldb/source/Core/ValueObjectConstResultImpl.cpp @@ -18,7 +18,6 @@ #include "lldb/Utility/DataBufferHeap.h" #include "lldb/Utility/Endian.h" #include "lldb/Utility/Scalar.h" -#include "lldb/Utility/SharingPtr.h" #include diff --git a/lldb/source/Core/ValueObjectList.cpp b/lldb/source/Core/ValueObjectList.cpp --- a/lldb/source/Core/ValueObjectList.cpp +++ b/lldb/source/Core/ValueObjectList.cpp @@ -10,7 +10,6 @@ #include "lldb/Core/ValueObject.h" #include "lldb/Utility/ConstString.h" -#include "lldb/Utility/SharingPtr.h" #include diff --git a/lldb/source/Core/ValueObjectMemory.cpp b/lldb/source/Core/ValueObjectMemory.cpp --- a/lldb/source/Core/ValueObjectMemory.cpp +++ b/lldb/source/Core/ValueObjectMemory.cpp @@ -32,21 +32,27 @@ llvm::StringRef name, const Address &address, lldb::TypeSP &type_sp) { - return (new ValueObjectMemory(exe_scope, name, address, type_sp))->GetSP(); + auto manager_sp = ValueObjectManager::Create(); + return (new ValueObjectMemory(exe_scope, *manager_sp, name, address, type_sp)) + ->GetSP(); } ValueObjectSP ValueObjectMemory::Create(ExecutionContextScope *exe_scope, llvm::StringRef name, const Address &address, const CompilerType &ast_type) { - return (new ValueObjectMemory(exe_scope, name, address, ast_type))->GetSP(); + auto manager_sp = ValueObjectManager::Create(); + return (new ValueObjectMemory(exe_scope, *manager_sp, name, address, + ast_type)) + ->GetSP(); } ValueObjectMemory::ValueObjectMemory(ExecutionContextScope *exe_scope, + ValueObjectManager &manager, llvm::StringRef name, const Address &address, lldb::TypeSP &type_sp) - : ValueObject(exe_scope), m_address(address), m_type_sp(type_sp), + : ValueObject(exe_scope, manager), m_address(address), m_type_sp(type_sp), m_compiler_type() { // Do not attempt to construct one of these objects with no variable! assert(m_type_sp.get() != nullptr); @@ -70,10 +76,11 @@ } ValueObjectMemory::ValueObjectMemory(ExecutionContextScope *exe_scope, + ValueObjectManager &manager, llvm::StringRef name, const Address &address, const CompilerType &ast_type) - : ValueObject(exe_scope), m_address(address), m_type_sp(), + : ValueObject(exe_scope, manager), m_address(address), m_type_sp(), m_compiler_type(ast_type) { // Do not attempt to construct one of these objects with no variable! assert(m_compiler_type.GetTypeSystem()); diff --git a/lldb/source/Core/ValueObjectRegister.cpp b/lldb/source/Core/ValueObjectRegister.cpp --- a/lldb/source/Core/ValueObjectRegister.cpp +++ b/lldb/source/Core/ValueObjectRegister.cpp @@ -41,14 +41,18 @@ ValueObjectRegisterSet::Create(ExecutionContextScope *exe_scope, lldb::RegisterContextSP ®_ctx_sp, uint32_t set_idx) { - return (new ValueObjectRegisterSet(exe_scope, reg_ctx_sp, set_idx))->GetSP(); + auto manager_sp = ValueObjectManager::Create(); + return (new ValueObjectRegisterSet(exe_scope, *manager_sp, reg_ctx_sp, + set_idx)) + ->GetSP(); } ValueObjectRegisterSet::ValueObjectRegisterSet(ExecutionContextScope *exe_scope, + ValueObjectManager &manager, lldb::RegisterContextSP ®_ctx, uint32_t reg_set_idx) - : ValueObject(exe_scope), m_reg_ctx_sp(reg_ctx), m_reg_set(nullptr), - m_reg_set_idx(reg_set_idx) { + : ValueObject(exe_scope, manager), m_reg_ctx_sp(reg_ctx), + m_reg_set(nullptr), m_reg_set_idx(reg_set_idx) { assert(reg_ctx); m_reg_set = reg_ctx->GetRegisterSet(m_reg_set_idx); if (m_reg_set) { @@ -174,13 +178,16 @@ ValueObjectSP ValueObjectRegister::Create(ExecutionContextScope *exe_scope, lldb::RegisterContextSP ®_ctx_sp, uint32_t reg_num) { - return (new ValueObjectRegister(exe_scope, reg_ctx_sp, reg_num))->GetSP(); + auto manager_sp = ValueObjectManager::Create(); + return (new ValueObjectRegister(exe_scope, *manager_sp, reg_ctx_sp, reg_num)) + ->GetSP(); } ValueObjectRegister::ValueObjectRegister(ExecutionContextScope *exe_scope, + ValueObjectManager &manager, lldb::RegisterContextSP ®_ctx, uint32_t reg_num) - : ValueObject(exe_scope), m_reg_ctx_sp(reg_ctx), m_reg_info(), + : ValueObject(exe_scope, manager), m_reg_ctx_sp(reg_ctx), m_reg_info(), m_reg_value(), m_type_name(), m_compiler_type() { assert(reg_ctx); ConstructObject(reg_num); diff --git a/lldb/source/Core/ValueObjectSyntheticFilter.cpp b/lldb/source/Core/ValueObjectSyntheticFilter.cpp --- a/lldb/source/Core/ValueObjectSyntheticFilter.cpp +++ b/lldb/source/Core/ValueObjectSyntheticFilter.cpp @@ -14,7 +14,6 @@ #include "lldb/Target/ExecutionContext.h" #include "lldb/Utility/Log.h" #include "lldb/Utility/Logging.h" -#include "lldb/Utility/SharingPtr.h" #include "lldb/Utility/Status.h" #include "llvm/ADT/STLExtras.h" diff --git a/lldb/source/Core/ValueObjectVariable.cpp b/lldb/source/Core/ValueObjectVariable.cpp --- a/lldb/source/Core/ValueObjectVariable.cpp +++ b/lldb/source/Core/ValueObjectVariable.cpp @@ -50,12 +50,14 @@ lldb::ValueObjectSP ValueObjectVariable::Create(ExecutionContextScope *exe_scope, const lldb::VariableSP &var_sp) { - return (new ValueObjectVariable(exe_scope, var_sp))->GetSP(); + auto manager_sp = ValueObjectManager::Create(); + return (new ValueObjectVariable(exe_scope, *manager_sp, var_sp))->GetSP(); } ValueObjectVariable::ValueObjectVariable(ExecutionContextScope *exe_scope, + ValueObjectManager &manager, const lldb::VariableSP &var_sp) - : ValueObject(exe_scope), m_variable_sp(var_sp) { + : ValueObject(exe_scope, manager), m_variable_sp(var_sp) { // Do not attempt to construct one of these objects with no variable! assert(m_variable_sp.get() != nullptr); m_name = var_sp->GetName(); diff --git a/lldb/source/Expression/IRInterpreter.cpp b/lldb/source/Expression/IRInterpreter.cpp --- a/lldb/source/Expression/IRInterpreter.cpp +++ b/lldb/source/Expression/IRInterpreter.cpp @@ -1510,7 +1510,7 @@ lldb_private::ValueObject *vobj = retVal.get(); // Check if the return value is valid - if (vobj == nullptr || retVal.empty()) { + if (vobj == nullptr || !retVal) { error.SetErrorToGenericError(); error.SetErrorStringWithFormat("unable to get the return value"); return false; diff --git a/lldb/source/Utility/CMakeLists.txt b/lldb/source/Utility/CMakeLists.txt --- a/lldb/source/Utility/CMakeLists.txt +++ b/lldb/source/Utility/CMakeLists.txt @@ -39,7 +39,6 @@ ReproducerInstrumentation.cpp Scalar.cpp SelectHelper.cpp - SharingPtr.cpp State.cpp Status.cpp Stream.cpp diff --git a/lldb/source/Utility/SharingPtr.cpp b/lldb/source/Utility/SharingPtr.cpp deleted file mode 100644 --- a/lldb/source/Utility/SharingPtr.cpp +++ /dev/null @@ -1,134 +0,0 @@ -//===-- SharingPtr.cpp ----------------------------------------------------===// -// -// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. -// See https://llvm.org/LICENSE.txt for license information. -// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception -// -//===----------------------------------------------------------------------===// - -#include "lldb/Utility/SharingPtr.h" - -#if defined(ENABLE_SP_LOGGING) - -// If ENABLE_SP_LOGGING is defined, then log all shared pointer assignments and -// allow them to be queried using a pointer by a call to: -#include -#include - -#include "llvm/ADT/STLExtras.h" - -#include -#include -#include - -class Backtrace { -public: - Backtrace(); - - ~Backtrace(); - - void GetFrames(); - - void Dump() const; - -private: - void *m_sp_this; - std::vector m_frames; -}; - -Backtrace::Backtrace() : m_frames() {} - -Backtrace::~Backtrace() {} - -void Backtrace::GetFrames() { - void *frames[1024]; - const int count = ::backtrace(frames, llvm::array_lengthof(frames)); - if (count > 2) - m_frames.assign(frames + 2, frames + (count - 2)); -} - -void Backtrace::Dump() const { - if (!m_frames.empty()) - ::backtrace_symbols_fd(m_frames.data(), m_frames.size(), STDOUT_FILENO); - write(STDOUT_FILENO, "\n\n", 2); -} - -extern "C" void track_sp(void *sp_this, void *ptr, long use_count) { - typedef std::pair PtrBacktracePair; - typedef std::map PtrToBacktraceMap; - static std::mutex g_mutex; - std::lock_guard guard(g_mutex); - static PtrToBacktraceMap g_map; - - if (sp_this) { - printf("sp(%p) -> %p %lu\n", sp_this, ptr, use_count); - - if (ptr) { - Backtrace bt; - bt.GetFrames(); - g_map[sp_this] = std::make_pair(ptr, bt); - } else { - g_map.erase(sp_this); - } - } else { - if (ptr) - printf("Searching for shared pointers that are tracking %p: ", ptr); - else - printf("Dump all live shared pointres: "); - - uint32_t matches = 0; - PtrToBacktraceMap::iterator pos, end = g_map.end(); - for (pos = g_map.begin(); pos != end; ++pos) { - if (ptr == NULL || pos->second.first == ptr) { - ++matches; - printf("\nsp(%p): %p\n", pos->first, pos->second.first); - pos->second.second.Dump(); - } - } - if (matches == 0) { - printf("none.\n"); - } - } -} -// Put dump_sp_refs in the lldb namespace to it gets through our exports lists -// filter in the LLDB.framework or lldb.so -namespace lldb { - -void dump_sp_refs(void *ptr) { - // Use a specially crafted call to "track_sp" which will dump info on all - // live shared pointers that reference "ptr" - track_sp(NULL, ptr, 0); -} -} - -#endif - -namespace lldb_private { - -namespace imp { - -shared_count::~shared_count() {} - -void shared_count::add_shared() { -#ifdef _MSC_VER - _InterlockedIncrement(&shared_owners_); -#else - ++shared_owners_; -#endif -} - -void shared_count::release_shared() { -#ifdef _MSC_VER - if (_InterlockedDecrement(&shared_owners_) == -1) -#else - if (--shared_owners_ == -1) -#endif - { - on_zero_shared(); - delete this; - } -} - -} // imp - -} // namespace lldb diff --git a/lldb/unittests/Utility/SharedClusterTest.cpp b/lldb/unittests/Utility/SharedClusterTest.cpp --- a/lldb/unittests/Utility/SharedClusterTest.cpp +++ b/lldb/unittests/Utility/SharedClusterTest.cpp @@ -25,30 +25,33 @@ TEST(SharedCluster, ClusterManager) { std::vector Queue; - auto *CM = new ClusterManager(); - auto *One = new DestructNotifier(Queue, 1); - auto *Two = new DestructNotifier(Queue, 2); - CM->ManageObject(One); - CM->ManageObject(Two); - - ASSERT_THAT(Queue, testing::IsEmpty()); { - SharingPtr OnePtr = CM->GetSharedPointer(One); - ASSERT_EQ(OnePtr->Key, 1); - ASSERT_THAT(Queue, testing::IsEmpty()); + auto CM = ClusterManager::Create(); + auto *One = new DestructNotifier(Queue, 1); + auto *Two = new DestructNotifier(Queue, 2); + CM->ManageObject(One); + CM->ManageObject(Two); + ASSERT_THAT(Queue, testing::IsEmpty()); { - SharingPtr OnePtrCopy = OnePtr; - ASSERT_EQ(OnePtrCopy->Key, 1); + std::shared_ptr OnePtr = CM->GetSharedPointer(One); + ASSERT_EQ(OnePtr->Key, 1); ASSERT_THAT(Queue, testing::IsEmpty()); - } - { - SharingPtr TwoPtr = CM->GetSharedPointer(Two); - ASSERT_EQ(TwoPtr->Key, 2); + { + std::shared_ptr OnePtrCopy = OnePtr; + ASSERT_EQ(OnePtrCopy->Key, 1); + ASSERT_THAT(Queue, testing::IsEmpty()); + } + + { + std::shared_ptr TwoPtr = CM->GetSharedPointer(Two); + ASSERT_EQ(TwoPtr->Key, 2); + ASSERT_THAT(Queue, testing::IsEmpty()); + } + ASSERT_THAT(Queue, testing::IsEmpty()); } - ASSERT_THAT(Queue, testing::IsEmpty()); } ASSERT_THAT(Queue, testing::ElementsAre(1, 2));