Clean up NativeRegisterContext

Summary:
This commit removes the concrete_frame_idx member from
NativeRegisterContext and related functions, which was always set to
zero and never used.

I also change the native thread class to store a NativeRegisterContext
as a unique_ptr (documenting the ownership) and make sure it is always
initialized (most of the code was already blindly dereferencing the
register context pointer, assuming it would always be present -- this
makes its treatment consistent).

Reviewers: eugene, clayborg, krytarowski

Subscribers: aemerson, sdardis, nemanjai, javed.absar, arichardson, kristof.beyls, kbarton, uweigand, alexandreyy, lldb-commits

Differential Revision: https://reviews.llvm.org/D39837

llvm-svn: 317881
diff --git a/lldb/source/Plugins/Process/NetBSD/NativeProcessNetBSD.cpp b/lldb/source/Plugins/Process/NetBSD/NativeProcessNetBSD.cpp
index 40d0396..387f04a 100644
--- a/lldb/source/Plugins/Process/NetBSD/NativeProcessNetBSD.cpp
+++ b/lldb/source/Plugins/Process/NetBSD/NativeProcessNetBSD.cpp
@@ -249,7 +249,7 @@
     uint32_t wp_index;
     Status error = static_cast<NativeThreadNetBSD &>(*m_threads[info.psi_lwpid])
                        .GetRegisterContext()
-                       ->GetWatchpointHitIndex(
+                       .GetWatchpointHitIndex(
                            wp_index, (uintptr_t)info.psi_siginfo.si_addr);
     if (error.Fail())
       LLDB_LOG(log,
@@ -268,7 +268,7 @@
     uint32_t bp_index;
     error = static_cast<NativeThreadNetBSD &>(*m_threads[info.psi_lwpid])
                 .GetRegisterContext()
-                ->GetHardwareBreakHitIndex(bp_index,
+                .GetHardwareBreakHitIndex(bp_index,
                                            (uintptr_t)info.psi_siginfo.si_addr);
     if (error.Fail())
       LLDB_LOG(log,
@@ -341,12 +341,7 @@
   Status error;
   // Find out the size of a breakpoint (might depend on where we are in the
   // code).
-  NativeRegisterContextSP context_sp = thread.GetRegisterContext();
-  if (!context_sp) {
-    error.SetErrorString("cannot get a NativeRegisterContext for the thread");
-    LLDB_LOG(log, "failed: {0}", error);
-    return error;
-  }
+  NativeRegisterContext& context = thread.GetRegisterContext();
   uint32_t breakpoint_size = 0;
   error = GetSoftwareBreakpointPCOffset(breakpoint_size);
   if (error.Fail()) {
@@ -357,7 +352,7 @@
   // First try probing for a breakpoint at a software breakpoint location: PC
   // - breakpoint size.
   const lldb::addr_t initial_pc_addr =
-      context_sp->GetPCfromBreakpointLocation();
+      context.GetPCfromBreakpointLocation();
   lldb::addr_t breakpoint_addr = initial_pc_addr;
   if (breakpoint_size > 0) {
     // Do not allow breakpoint probe to wrap around.
@@ -410,7 +405,7 @@
   // Change the program counter.
   LLDB_LOG(log, "pid {0} tid {1}: changing PC from {2:x} to {3:x}", GetID(),
            thread.GetID(), initial_pc_addr, breakpoint_addr);
-  error = context_sp->SetPC(breakpoint_addr);
+  error = context.SetPC(breakpoint_addr);
   if (error.Fail()) {
     LLDB_LOG(log, "pid {0} tid {1}: failed to set PC: {2}", GetID(),
              thread.GetID(), error);
diff --git a/lldb/source/Plugins/Process/NetBSD/NativeRegisterContextNetBSD.cpp b/lldb/source/Plugins/Process/NetBSD/NativeRegisterContextNetBSD.cpp
index dde8688..d4fef63 100644
--- a/lldb/source/Plugins/Process/NetBSD/NativeRegisterContextNetBSD.cpp
+++ b/lldb/source/Plugins/Process/NetBSD/NativeRegisterContextNetBSD.cpp
@@ -20,9 +20,9 @@
 // clang-format on
 
 NativeRegisterContextNetBSD::NativeRegisterContextNetBSD(
-    NativeThreadProtocol &native_thread, uint32_t concrete_frame_idx,
+    NativeThreadProtocol &native_thread,
     RegisterInfoInterface *reg_info_interface_p)
-    : NativeRegisterContextRegisterInfo(native_thread, concrete_frame_idx,
+    : NativeRegisterContextRegisterInfo(native_thread,
                                         reg_info_interface_p) {}
 
 Status NativeRegisterContextNetBSD::ReadGPR() {
diff --git a/lldb/source/Plugins/Process/NetBSD/NativeRegisterContextNetBSD.h b/lldb/source/Plugins/Process/NetBSD/NativeRegisterContextNetBSD.h
index d96b7ae..b81430e 100644
--- a/lldb/source/Plugins/Process/NetBSD/NativeRegisterContextNetBSD.h
+++ b/lldb/source/Plugins/Process/NetBSD/NativeRegisterContextNetBSD.h
@@ -21,7 +21,6 @@
 class NativeRegisterContextNetBSD : public NativeRegisterContextRegisterInfo {
 public:
   NativeRegisterContextNetBSD(NativeThreadProtocol &native_thread,
-                              uint32_t concrete_frame_idx,
                               RegisterInfoInterface *reg_info_interface_p);
 
   // This function is implemented in the NativeRegisterContextNetBSD_*
@@ -31,8 +30,7 @@
   // executable.
   static NativeRegisterContextNetBSD *
   CreateHostNativeRegisterContextNetBSD(const ArchSpec &target_arch,
-                                        NativeThreadProtocol &native_thread,
-                                        uint32_t concrete_frame_idx);
+                                        NativeThreadProtocol &native_thread);
 
 protected:
   virtual Status ReadGPR();
diff --git a/lldb/source/Plugins/Process/NetBSD/NativeRegisterContextNetBSD_x86_64.cpp b/lldb/source/Plugins/Process/NetBSD/NativeRegisterContextNetBSD_x86_64.cpp
index 9690da0..347c15a 100644
--- a/lldb/source/Plugins/Process/NetBSD/NativeRegisterContextNetBSD_x86_64.cpp
+++ b/lldb/source/Plugins/Process/NetBSD/NativeRegisterContextNetBSD_x86_64.cpp
@@ -149,10 +149,8 @@
 
 NativeRegisterContextNetBSD *
 NativeRegisterContextNetBSD::CreateHostNativeRegisterContextNetBSD(
-    const ArchSpec &target_arch, NativeThreadProtocol &native_thread,
-    uint32_t concrete_frame_idx) {
-  return new NativeRegisterContextNetBSD_x86_64(target_arch, native_thread,
-                                                concrete_frame_idx);
+    const ArchSpec &target_arch, NativeThreadProtocol &native_thread) {
+  return new NativeRegisterContextNetBSD_x86_64(target_arch, native_thread);
 }
 
 // ----------------------------------------------------------------------------
@@ -169,9 +167,8 @@
 }
 
 NativeRegisterContextNetBSD_x86_64::NativeRegisterContextNetBSD_x86_64(
-    const ArchSpec &target_arch, NativeThreadProtocol &native_thread,
-    uint32_t concrete_frame_idx)
-    : NativeRegisterContextNetBSD(native_thread, concrete_frame_idx,
+    const ArchSpec &target_arch, NativeThreadProtocol &native_thread)
+    : NativeRegisterContextNetBSD(native_thread,
                                   CreateRegisterInfoInterface(target_arch)),
       m_gpr_x86_64(), m_fpr_x86_64(), m_dbr_x86_64() {}
 
diff --git a/lldb/source/Plugins/Process/NetBSD/NativeRegisterContextNetBSD_x86_64.h b/lldb/source/Plugins/Process/NetBSD/NativeRegisterContextNetBSD_x86_64.h
index 5f5a6a0..c55ddfe 100644
--- a/lldb/source/Plugins/Process/NetBSD/NativeRegisterContextNetBSD_x86_64.h
+++ b/lldb/source/Plugins/Process/NetBSD/NativeRegisterContextNetBSD_x86_64.h
@@ -30,8 +30,7 @@
 class NativeRegisterContextNetBSD_x86_64 : public NativeRegisterContextNetBSD {
 public:
   NativeRegisterContextNetBSD_x86_64(const ArchSpec &target_arch,
-                                     NativeThreadProtocol &native_thread,
-                                     uint32_t concrete_frame_idx);
+                                     NativeThreadProtocol &native_thread);
   uint32_t GetRegisterSetCount() const override;
 
   const RegisterSet *GetRegisterSet(uint32_t set_index) const override;
diff --git a/lldb/source/Plugins/Process/NetBSD/NativeThreadNetBSD.cpp b/lldb/source/Plugins/Process/NetBSD/NativeThreadNetBSD.cpp
index b64935a..83f1da7 100644
--- a/lldb/source/Plugins/Process/NetBSD/NativeThreadNetBSD.cpp
+++ b/lldb/source/Plugins/Process/NetBSD/NativeThreadNetBSD.cpp
@@ -27,7 +27,9 @@
 NativeThreadNetBSD::NativeThreadNetBSD(NativeProcessNetBSD &process,
                                        lldb::tid_t tid)
     : NativeThreadProtocol(process, tid), m_state(StateType::eStateInvalid),
-      m_stop_info(), m_reg_context_sp(), m_stop_description() {}
+      m_stop_info(), m_reg_context_up(
+NativeRegisterContextNetBSD::CreateHostNativeRegisterContextNetBSD(process.GetArchitecture(), *this)
+), m_stop_description() {}
 
 void NativeThreadNetBSD::SetStoppedBySignal(uint32_t signo,
                                             const siginfo_t *info) {
@@ -77,10 +79,10 @@
   lldbassert(wp_index != LLDB_INVALID_INDEX32 && "wp_index cannot be invalid");
 
   std::ostringstream ostr;
-  ostr << GetRegisterContext()->GetWatchpointAddress(wp_index) << " ";
+  ostr << GetRegisterContext().GetWatchpointAddress(wp_index) << " ";
   ostr << wp_index;
 
-  ostr << " " << GetRegisterContext()->GetWatchpointHitAddress(wp_index);
+  ostr << " " << GetRegisterContext().GetWatchpointHitAddress(wp_index);
 
   m_stop_description = ostr.str();
 
@@ -139,17 +141,9 @@
   llvm_unreachable("unhandled StateType!");
 }
 
-NativeRegisterContextSP NativeThreadNetBSD::GetRegisterContext() {
-  // Return the register context if we already created it.
-  if (m_reg_context_sp)
-    return m_reg_context_sp;
-
-  const uint32_t concrete_frame_idx = 0;
-  m_reg_context_sp.reset(
-      NativeRegisterContextNetBSD::CreateHostNativeRegisterContextNetBSD(
-          m_process.GetArchitecture(), *this, concrete_frame_idx));
-
-  return m_reg_context_sp;
+NativeRegisterContext& NativeThreadNetBSD::GetRegisterContext() {
+  assert(m_reg_context_up);
+return  *m_reg_context_up;
 }
 
 Status NativeThreadNetBSD::SetWatchpoint(lldb::addr_t addr, size_t size,
@@ -161,8 +155,7 @@
   Status error = RemoveWatchpoint(addr);
   if (error.Fail())
     return error;
-  NativeRegisterContextSP reg_ctx = GetRegisterContext();
-  uint32_t wp_index = reg_ctx->SetHardwareWatchpoint(addr, size, watch_flags);
+  uint32_t wp_index = GetRegisterContext().SetHardwareWatchpoint(addr, size, watch_flags);
   if (wp_index == LLDB_INVALID_INDEX32)
     return Status("Setting hardware watchpoint failed.");
   m_watchpoint_index_map.insert({addr, wp_index});
@@ -175,7 +168,7 @@
     return Status();
   uint32_t wp_index = wp->second;
   m_watchpoint_index_map.erase(wp);
-  if (GetRegisterContext()->ClearHardwareWatchpoint(wp_index))
+  if (GetRegisterContext().ClearHardwareWatchpoint(wp_index))
     return Status();
   return Status("Clearing hardware watchpoint failed.");
 }
@@ -189,8 +182,7 @@
   if (error.Fail())
     return error;
 
-  NativeRegisterContextSP reg_ctx = GetRegisterContext();
-  uint32_t bp_index = reg_ctx->SetHardwareBreakpoint(addr, size);
+  uint32_t bp_index = GetRegisterContext().SetHardwareBreakpoint(addr, size);
 
   if (bp_index == LLDB_INVALID_INDEX32)
     return Status("Setting hardware breakpoint failed.");
@@ -205,7 +197,7 @@
     return Status();
 
   uint32_t bp_index = bp->second;
-  if (GetRegisterContext()->ClearHardwareBreakpoint(bp_index)) {
+  if (GetRegisterContext().ClearHardwareBreakpoint(bp_index)) {
     m_hw_break_index_map.erase(bp);
     return Status();
   }
diff --git a/lldb/source/Plugins/Process/NetBSD/NativeThreadNetBSD.h b/lldb/source/Plugins/Process/NetBSD/NativeThreadNetBSD.h
index 1e3f587..7242624 100644
--- a/lldb/source/Plugins/Process/NetBSD/NativeThreadNetBSD.h
+++ b/lldb/source/Plugins/Process/NetBSD/NativeThreadNetBSD.h
@@ -37,7 +37,7 @@
   bool GetStopReason(ThreadStopInfo &stop_info,
                      std::string &description) override;
 
-  NativeRegisterContextSP GetRegisterContext() override;
+  NativeRegisterContext& GetRegisterContext() override;
 
   Status SetWatchpoint(lldb::addr_t addr, size_t size, uint32_t watch_flags,
                        bool hardware) override;
@@ -67,7 +67,7 @@
   // ---------------------------------------------------------------------
   lldb::StateType m_state;
   ThreadStopInfo m_stop_info;
-  NativeRegisterContextSP m_reg_context_sp;
+  std::unique_ptr<NativeRegisterContext> m_reg_context_up;
   std::string m_stop_description;
   using WatchpointIndexMap = std::map<lldb::addr_t, uint32_t>;
   WatchpointIndexMap m_watchpoint_index_map;