================
@@ -7093,3 +7126,121 @@ void Process::SetAddressableBitMasks(AddressableBits 
bit_masks) {
     SetHighmemDataAddressMask(high_addr_mask);
   }
 }
+
+// Check if a weak_ptr was never initialized.
+// This is useful to distinguish between a weak_ptr that was never initialized
+// and a weak_ptr that was initialized and then expired.
+// 
https://stackoverflow.com/questions/45507041/how-to-check-if-weak-ptr-is-empty-non-assigned
+template <typename T>
+static bool weak_ptr_is_uninitialized(std::weak_ptr<T> const &weak) {
+  using wt = std::weak_ptr<T>;
+  return !weak.owner_before(wt{}) && !wt{}.owner_before(weak);
+}
+
+llvm::Expected<lldb::ModuleSP> AddressSpec::GetModule() const {
+  if (m_module_wp.expired() && !weak_ptr_is_uninitialized(m_module_wp))
+    return llvm::createStringError("module has expired");
+  return m_module_wp.lock();
+}
+
+llvm::Expected<lldb::ThreadSP> AddressSpec::GetThread() const {
+  if (m_thread_wp.expired() && !weak_ptr_is_uninitialized(m_thread_wp))
+    return llvm::createStringError("module has expired");
+  return m_thread_wp.lock();
+}
+
+llvm::Expected<AddressSpaceInfo>
+AddressSpec::GetAddressSpaceInfo(lldb_private::Process &process) const {
+  if (m_addr_space_id.has_value())
+    return process.GetAddressSpaceInfo(*m_addr_space_id);
+  if (m_addr_space_name.has_value())
+    return process.GetAddressSpaceInfo(GetSpaceName());
+  return llvm::createStringError("AddressSpec has no address space info");
+}
+
+llvm::Expected<lldb::addr_t> AddressSpec::ResolveAddressInDefaultAddressSpace(
+    lldb_private::Process &process) const {
+  if (!IsInDefaultAddressSpace())
+    return llvm::createStringError("address is not in the default address "
+                                   "space");
+  // This object holds a weak pointer to a module. We need to make sure the
+  // module hasn't been destroyed.
+  auto exp_module = GetModule();
+  if (!exp_module)
+    return exp_module.takeError();
+  ModuleSP module_sp = *exp_module;
+  if (module_sp) {
+    // This object holds a weak pointer to a thread and we need to make sure
+    // thread is still around.
+    auto exp_thread = GetThread();
+    if (!exp_thread)
+      return exp_thread.takeError();
+    ThreadSP thread_sp = *exp_thread;
+    if (thread_sp) {
+      // We have thread local storage address specification. Try to resolve the
+      // TLS address via the dynamic loader.
+      DynamicLoader *dyld = process.GetDynamicLoader();
+      if (dyld) {
+        addr_t load_addr =
+            dyld->GetThreadLocalData(module_sp, thread_sp, m_value);
+        if (load_addr != LLDB_INVALID_ADDRESS)
+          return load_addr;
+        return llvm::createStringError(
+            "dynamic loader was unable to resolve the thread local address");
+      }
+      return llvm::createStringError(
+          "no dynamic loader, unable to resolve the thread local address");
+    } else {
+      // m_value is a file address within a module.
+      Address so_addr;
+      if (module_sp->ResolveFileAddress(m_value, so_addr)) {
+        addr_t load_addr = so_addr.GetLoadAddress(&process.GetTarget());
+        if (load_addr != LLDB_INVALID_ADDRESS)
+          return load_addr;
+        return llvm::createStringError(
+            "section for module file address is not loaded in the target");
+      }
+      return llvm::createStringError(
+          "unable to resolve file address in module");
+    }
+  }
+  // We just have a plain load address already
----------------
Teemperor wrote:

Comment without period at the end.

https://github.com/llvm/llvm-project/pull/206370
_______________________________________________
lldb-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/lldb-commits

Reply via email to