[Lldb-commits] [lldb] 1d6fa41 - [lldb] Have Disassembler::ParseInstructions take a Target&

Pavel Labath via lldb-commits lldb-commits at lists.llvm.org
Tue Mar 3 05:02:25 PST 2020


Author: Pavel Labath
Date: 2020-03-03T13:58:56+01:00
New Revision: 1d6fa41f40da5f5a615497eac818623779c46556

URL: https://github.com/llvm/llvm-project/commit/1d6fa41f40da5f5a615497eac818623779c46556
DIFF: https://github.com/llvm/llvm-project/commit/1d6fa41f40da5f5a615497eac818623779c46556.diff

LOG: [lldb] Have Disassembler::ParseInstructions take a Target&

Instead of a ExecutionContext*. All it needs is the target so it can
read the memory.

This removes some defensive checks from the function. I've added
equivalent checks to the callers in cases where a non-null target
pointer was not guaranteed to be available.

Added: 
    

Modified: 
    lldb/include/lldb/Core/Disassembler.h
    lldb/source/Core/Disassembler.cpp
    lldb/source/Plugins/Architecture/Mips/ArchitectureMips.cpp
    lldb/source/Plugins/Architecture/Mips/ArchitectureMips.h

Removed: 
    


################################################################################
diff  --git a/lldb/include/lldb/Core/Disassembler.h b/lldb/include/lldb/Core/Disassembler.h
index 98f34f3e0cfa..521c8be2bbf8 100644
--- a/lldb/include/lldb/Core/Disassembler.h
+++ b/lldb/include/lldb/Core/Disassembler.h
@@ -446,13 +446,11 @@ class Disassembler : public std::enable_shared_from_this<Disassembler>,
                                 uint32_t num_mixed_context_lines,
                                 uint32_t options, Stream &strm);
 
-  size_t ParseInstructions(const ExecutionContext *exe_ctx,
-                           const AddressRange &range, Stream *error_strm_ptr,
-                           bool prefer_file_cache);
+  size_t ParseInstructions(Target &target, const AddressRange &range,
+                           Stream *error_strm_ptr, bool prefer_file_cache);
 
-  size_t ParseInstructions(const ExecutionContext *exe_ctx,
-                           const Address &range, uint32_t num_instructions,
-                           bool prefer_file_cache);
+  size_t ParseInstructions(Target &target, const Address &range,
+                           uint32_t num_instructions, bool prefer_file_cache);
 
   virtual size_t DecodeInstructions(const Address &base_addr,
                                     const DataExtractor &data,

diff  --git a/lldb/source/Core/Disassembler.cpp b/lldb/source/Core/Disassembler.cpp
index 60247cfdd99e..268e25fb6697 100644
--- a/lldb/source/Core/Disassembler.cpp
+++ b/lldb/source/Core/Disassembler.cpp
@@ -193,7 +193,7 @@ lldb::DisassemblerSP Disassembler::DisassembleRange(
     const ArchSpec &arch, const char *plugin_name, const char *flavor,
     const ExecutionContext &exe_ctx, const AddressRange &range,
     bool prefer_file_cache) {
-  if (range.GetByteSize() <= 0)
+  if (range.GetByteSize() <= 0 || !exe_ctx.GetTargetPtr())
     return {};
 
   if (!range.GetBaseAddress().IsValid())
@@ -205,8 +205,8 @@ lldb::DisassemblerSP Disassembler::DisassembleRange(
   if (!disasm_sp)
     return {};
 
-  const size_t bytes_disassembled =
-      disasm_sp->ParseInstructions(&exe_ctx, range, nullptr, prefer_file_cache);
+  const size_t bytes_disassembled = disasm_sp->ParseInstructions(
+      exe_ctx.GetTargetRef(), range, nullptr, prefer_file_cache);
   if (bytes_disassembled == 0)
     return {};
 
@@ -243,7 +243,7 @@ bool Disassembler::Disassemble(Debugger &debugger, const ArchSpec &arch,
                                bool mixed_source_and_assembly,
                                uint32_t num_mixed_context_lines,
                                uint32_t options, Stream &strm) {
-  if (!disasm_range.GetByteSize())
+  if (!disasm_range.GetByteSize() || !exe_ctx.GetTargetPtr())
     return false;
 
   lldb::DisassemblerSP disasm_sp(Disassembler::FindPluginForTarget(
@@ -257,8 +257,8 @@ bool Disassembler::Disassemble(Debugger &debugger, const ArchSpec &arch,
                  range.GetBaseAddress());
   range.SetByteSize(disasm_range.GetByteSize());
   const bool prefer_file_cache = false;
-  size_t bytes_disassembled =
-      disasm_sp->ParseInstructions(&exe_ctx, range, &strm, prefer_file_cache);
+  size_t bytes_disassembled = disasm_sp->ParseInstructions(
+      exe_ctx.GetTargetRef(), range, &strm, prefer_file_cache);
   if (bytes_disassembled == 0)
     return false;
 
@@ -275,7 +275,7 @@ bool Disassembler::Disassemble(Debugger &debugger, const ArchSpec &arch,
                                bool mixed_source_and_assembly,
                                uint32_t num_mixed_context_lines,
                                uint32_t options, Stream &strm) {
-  if (num_instructions == 0)
+  if (num_instructions == 0 || !exe_ctx.GetTargetPtr())
     return false;
 
   lldb::DisassemblerSP disasm_sp(Disassembler::FindPluginForTarget(
@@ -288,7 +288,7 @@ bool Disassembler::Disassemble(Debugger &debugger, const ArchSpec &arch,
 
   const bool prefer_file_cache = false;
   size_t bytes_disassembled = disasm_sp->ParseInstructions(
-      &exe_ctx, addr, num_instructions, prefer_file_cache);
+      exe_ctx.GetTargetRef(), addr, num_instructions, prefer_file_cache);
   if (bytes_disassembled == 0)
     return false;
 
@@ -1182,59 +1182,51 @@ InstructionList::GetIndexOfInstructionAtLoadAddress(lldb::addr_t load_addr,
   return GetIndexOfInstructionAtAddress(address);
 }
 
-size_t Disassembler::ParseInstructions(const ExecutionContext *exe_ctx,
+size_t Disassembler::ParseInstructions(Target &target,
                                        const AddressRange &range,
                                        Stream *error_strm_ptr,
                                        bool prefer_file_cache) {
-  if (exe_ctx) {
-    Target *target = exe_ctx->GetTargetPtr();
-    const addr_t byte_size = range.GetByteSize();
-    if (target == nullptr || byte_size == 0 ||
-        !range.GetBaseAddress().IsValid())
-      return 0;
-
-    auto data_sp = std::make_shared<DataBufferHeap>(byte_size, '\0');
-
-    Status error;
-    lldb::addr_t load_addr = LLDB_INVALID_ADDRESS;
-    const size_t bytes_read = target->ReadMemory(
-        range.GetBaseAddress(), prefer_file_cache, data_sp->GetBytes(),
-        data_sp->GetByteSize(), error, &load_addr);
-
-    if (bytes_read > 0) {
-      if (bytes_read != data_sp->GetByteSize())
-        data_sp->SetByteSize(bytes_read);
-      DataExtractor data(data_sp, m_arch.GetByteOrder(),
-                         m_arch.GetAddressByteSize());
-      const bool data_from_file = load_addr == LLDB_INVALID_ADDRESS;
-      return DecodeInstructions(range.GetBaseAddress(), data, 0, UINT32_MAX,
-                                false, data_from_file);
-    } else if (error_strm_ptr) {
-      const char *error_cstr = error.AsCString();
-      if (error_cstr) {
-        error_strm_ptr->Printf("error: %s\n", error_cstr);
-      }
-    }
+  const addr_t byte_size = range.GetByteSize();
+  if (byte_size == 0 || !range.GetBaseAddress().IsValid())
+    return 0;
+
+  auto data_sp = std::make_shared<DataBufferHeap>(byte_size, '\0');
+
+  Status error;
+  lldb::addr_t load_addr = LLDB_INVALID_ADDRESS;
+  const size_t bytes_read = target.ReadMemory(
+      range.GetBaseAddress(), prefer_file_cache, data_sp->GetBytes(),
+      data_sp->GetByteSize(), error, &load_addr);
+
+  if (bytes_read > 0) {
+    if (bytes_read != data_sp->GetByteSize())
+      data_sp->SetByteSize(bytes_read);
+    DataExtractor data(data_sp, m_arch.GetByteOrder(),
+                       m_arch.GetAddressByteSize());
+    const bool data_from_file = load_addr == LLDB_INVALID_ADDRESS;
+    return DecodeInstructions(range.GetBaseAddress(), data, 0, UINT32_MAX,
+                              false, data_from_file);
   } else if (error_strm_ptr) {
-    error_strm_ptr->PutCString("error: invalid execution context\n");
+    const char *error_cstr = error.AsCString();
+    if (error_cstr) {
+      error_strm_ptr->Printf("error: %s\n", error_cstr);
+    }
   }
   return 0;
 }
 
-size_t Disassembler::ParseInstructions(const ExecutionContext *exe_ctx,
-                                       const Address &start,
+size_t Disassembler::ParseInstructions(Target &target, const Address &start,
                                        uint32_t num_instructions,
                                        bool prefer_file_cache) {
   m_instruction_list.Clear();
 
-  if (exe_ctx == nullptr || num_instructions == 0 || !start.IsValid())
+  if (num_instructions == 0 || !start.IsValid())
     return 0;
 
-  Target *target = exe_ctx->GetTargetPtr();
   // Calculate the max buffer size we will need in order to disassemble
   const addr_t byte_size = num_instructions * m_arch.GetMaximumOpcodeByteSize();
 
-  if (target == nullptr || byte_size == 0)
+  if (byte_size == 0)
     return 0;
 
   DataBufferHeap *heap_buffer = new DataBufferHeap(byte_size, '\0');
@@ -1243,8 +1235,8 @@ size_t Disassembler::ParseInstructions(const ExecutionContext *exe_ctx,
   Status error;
   lldb::addr_t load_addr = LLDB_INVALID_ADDRESS;
   const size_t bytes_read =
-      target->ReadMemory(start, prefer_file_cache, heap_buffer->GetBytes(),
-                         byte_size, error, &load_addr);
+      target.ReadMemory(start, prefer_file_cache, heap_buffer->GetBytes(),
+                        byte_size, error, &load_addr);
 
   const bool data_from_file = load_addr == LLDB_INVALID_ADDRESS;
 

diff  --git a/lldb/source/Plugins/Architecture/Mips/ArchitectureMips.cpp b/lldb/source/Plugins/Architecture/Mips/ArchitectureMips.cpp
index f426ac63e4b5..e3d1aa3b11dd 100644
--- a/lldb/source/Plugins/Architecture/Mips/ArchitectureMips.cpp
+++ b/lldb/source/Plugins/Architecture/Mips/ArchitectureMips.cpp
@@ -120,9 +120,7 @@ lldb::addr_t ArchitectureMips::GetBreakableLoadAddress(lldb::addr_t addr,
   if (current_offset == 0)
     return addr;
 
-  ExecutionContext ctx;
-  target.CalculateExecutionContext(ctx);
-  auto insn = GetInstructionAtAddress(ctx, current_offset, addr);
+  auto insn = GetInstructionAtAddress(target, current_offset, addr);
 
   if (nullptr == insn || !insn->HasDelaySlot())
     return addr;
@@ -138,8 +136,7 @@ lldb::addr_t ArchitectureMips::GetBreakableLoadAddress(lldb::addr_t addr,
 }
 
 Instruction *ArchitectureMips::GetInstructionAtAddress(
-    const ExecutionContext &exe_ctx, const Address &resolved_addr,
-    addr_t symbol_offset) const {
+    Target &target, const Address &resolved_addr, addr_t symbol_offset) const {
 
   auto loop_count = symbol_offset / 2;
 
@@ -174,7 +171,7 @@ Instruction *ArchitectureMips::GetInstructionAtAddress(
     AddressRange range(addr, i * 2);
     uint32_t insn_size = 0;
 
-    disasm_sp->ParseInstructions(&exe_ctx, range, nullptr, prefer_file_cache);
+    disasm_sp->ParseInstructions(target, range, nullptr, prefer_file_cache);
 
     uint32_t num_insns = disasm_sp->GetInstructionList().GetSize();
     if (num_insns) {

diff  --git a/lldb/source/Plugins/Architecture/Mips/ArchitectureMips.h b/lldb/source/Plugins/Architecture/Mips/ArchitectureMips.h
index 40bcc23fd8cd..71ee60184b69 100644
--- a/lldb/source/Plugins/Architecture/Mips/ArchitectureMips.h
+++ b/lldb/source/Plugins/Architecture/Mips/ArchitectureMips.h
@@ -35,11 +35,10 @@ class ArchitectureMips : public Architecture {
                                     AddressClass addr_class) const override;
 
 private:
-  Instruction *GetInstructionAtAddress(const ExecutionContext &exe_ctx,
+  Instruction *GetInstructionAtAddress(Target &target,
                                        const Address &resolved_addr,
                                        lldb::addr_t symbol_offset) const;
 
-
   static std::unique_ptr<Architecture> Create(const ArchSpec &arch);
   ArchitectureMips(const ArchSpec &arch) : m_arch(arch) {}
 


        


More information about the lldb-commits mailing list