Fixes issue where the 'next' command in the CLI fails.

PiperOrigin-RevId: 657634577
Change-Id: I1d937a678e78133212be1b59faad0cc5cba3bf1d
diff --git a/riscv/riscv_top.cc b/riscv/riscv_top.cc
index 79ad0eb..036fbc0 100644
--- a/riscv/riscv_top.cc
+++ b/riscv/riscv_top.cc
@@ -669,7 +669,21 @@
 }
 
 absl::StatusOr<Instruction *> RiscVTop::GetInstruction(uint64_t address) {
-  auto inst = rv_decode_cache_->GetDecodedInstruction(address);
+  // If requesting the instruction at an action point, we need to write the
+  // original instruction back to memory before getting the disassembly.
+  bool inst_swap = rv_action_point_manager_->IsActionPointActive(address);
+  if (inst_swap) {
+    (void)rv_action_point_manager_->ap_memory_interface()
+        ->WriteOriginalInstruction(address);
+  }
+  // Get the decoded instruction.
+  Instruction *inst = rv_decode_cache_->GetDecodedInstruction(address);
+  inst->IncRef();
+  // Swap back if required.
+  if (inst_swap) {
+    (void)rv_action_point_manager_->ap_memory_interface()
+        ->WriteBreakpointInstruction(address);
+  }
   return inst;
 }
 
@@ -679,25 +693,11 @@
     return absl::FailedPreconditionError("GetDissasembly: Core must be halted");
   }
 
-  Instruction *inst = nullptr;
-  // If requesting the disassembly for an instruction at an action point, return
-  // that of the original instruction instead.
-  if (rv_action_point_manager_->IsActionPointActive(address)) {
-    // Write the original instruction back to memory.
-    (void)rv_action_point_manager_->ap_memory_interface()
-        ->WriteOriginalInstruction(address);
-    // Get the real instruction.
-    inst = rv_decode_cache_->GetDecodedInstruction(address);
-    auto disasm = inst != nullptr ? inst->AsString() : "Invalid instruction";
-    // Restore the breakpoint instruction.
-    (void)rv_action_point_manager_->ap_memory_interface()
-        ->WriteBreakpointInstruction(address);
-    return disasm;
-  }
-
-  // If not at the breakpoint, or requesting a different instruction,
-  inst = rv_decode_cache_->GetDecodedInstruction(address);
+  auto res = GetInstruction(address);
+  if (!res.ok()) return res.status();
+  Instruction *inst = res.value();
   auto disasm = inst != nullptr ? inst->AsString() : "Invalid instruction";
+  inst->DecRef();
   return disasm;
 }
 
diff --git a/riscv/riscv_top.h b/riscv/riscv_top.h
index e18b0a4..5103ac2 100644
--- a/riscv/riscv_top.h
+++ b/riscv/riscv_top.h
@@ -103,6 +103,9 @@
   absl::Status ClearDataWatchpoint(uint64_t address,
                                    AccessType access_type) override;
 
+  // If successful, returns a pointer to the instruction at the given address.
+  // The instruction object is IncRef'ed, and the caller must DecRef the object
+  // when it is done with it.
   absl::StatusOr<Instruction *> GetInstruction(uint64_t address) override;
   absl::StatusOr<std::string> GetDisassembly(uint64_t address) override;
 
diff --git a/riscv/rv64g_test_sim.cc b/riscv/rv64g_test_sim.cc
index f7e5b9a..797e792 100644
--- a/riscv/rv64g_test_sim.cc
+++ b/riscv/rv64g_test_sim.cc
@@ -147,6 +147,7 @@
     halt_reason = halt_status.value();
     ok = true;
     if (commit_trace) {
+      // This IncRef's the inst instance. Need to DecRef it when we are done.
       auto *inst = riscv_top.GetInstruction(pc).value();
       std::string trace_str;
       absl::StrAppend(
@@ -204,6 +205,7 @@
       trace_str.append(test_watcher->trace_str());
       test_watcher->clear_trace_str();
       std::cerr << trace_str << std::endl;
+      inst->DecRef();
     }
     pc = riscv_top.ReadRegister("pc").value();
   } while (halt_reason == *HaltReason::kNone);