[Lldb-commits] [lldb] c6a3895 - [lldb] Delete two overloads of Disassembler::Disassemble

Pavel Labath via lldb-commits lldb-commits at lists.llvm.org
Thu Mar 5 02:00:48 PST 2020


Author: Pavel Labath
Date: 2020-03-05T11:00:37+01:00
New Revision: c6a38957a7e5e2ec837470a62aff25abfbf6d397

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

LOG: [lldb] Delete two overloads of Disassembler::Disassemble

by "inlining" them into their single caller (CommandObjectDisassemble).
The functions mainly consist of long argument lists and defensive
checks. These become unnecessary after inlining, so the end result is
less code. Additionally, this makes the implementation of
CommandObjectDisassemble more uniform (first figure out what you're
going to disassemble, then actually do it), which enables further
cleanups.

Added: 
    

Modified: 
    lldb/include/lldb/Core/Disassembler.h
    lldb/source/Commands/CommandObjectDisassemble.cpp
    lldb/source/Core/Disassembler.cpp

Removed: 
    


################################################################################
diff  --git a/lldb/include/lldb/Core/Disassembler.h b/lldb/include/lldb/Core/Disassembler.h
index 521c8be2bbf8..b03376b72388 100644
--- a/lldb/include/lldb/Core/Disassembler.h
+++ b/lldb/include/lldb/Core/Disassembler.h
@@ -410,20 +410,6 @@ class Disassembler : public std::enable_shared_from_this<Disassembler>,
                           uint32_t num_mixed_context_lines, uint32_t options,
                           Stream &strm);
 
-  static size_t
-  Disassemble(Debugger &debugger, const ArchSpec &arch, const char *plugin_name,
-              const char *flavor, const ExecutionContext &exe_ctx,
-              SymbolContextList &sc_list, uint32_t num_instructions,
-              bool mixed_source_and_assembly, uint32_t num_mixed_context_lines,
-              uint32_t options, Stream &strm);
-
-  static bool
-  Disassemble(Debugger &debugger, const ArchSpec &arch, const char *plugin_name,
-              const char *flavor, const ExecutionContext &exe_ctx,
-              ConstString name, Module *module,
-              uint32_t num_instructions, bool mixed_source_and_assembly,
-              uint32_t num_mixed_context_lines, uint32_t options, Stream &strm);
-
   static bool
   Disassemble(Debugger &debugger, const ArchSpec &arch, const char *plugin_name,
               const char *flavor, const ExecutionContext &exe_ctx,

diff  --git a/lldb/source/Commands/CommandObjectDisassemble.cpp b/lldb/source/Commands/CommandObjectDisassemble.cpp
index 0645119767da..a11af68835d9 100644
--- a/lldb/source/Commands/CommandObjectDisassemble.cpp
+++ b/lldb/source/Commands/CommandObjectDisassemble.cpp
@@ -281,24 +281,35 @@ bool CommandObjectDisassemble::DoExecute(Args &command,
   if (m_options.raw)
     options |= Disassembler::eOptionRawOuput;
 
+  std::vector<AddressRange> ranges;
   if (!m_options.func_name.empty()) {
     ConstString name(m_options.func_name.c_str());
+    const bool include_symbols = true;
+    const bool include_inlines = true;
 
-    if (Disassembler::Disassemble(
-            GetDebugger(), m_options.arch, plugin_name, flavor_string,
-            m_exe_ctx, name,
-            nullptr, // Module *
-            m_options.num_instructions, m_options.show_mixed,
-            m_options.show_mixed ? m_options.num_lines_context : 0, options,
-            result.GetOutputStream())) {
-      result.SetStatus(eReturnStatusSuccessFinishResult);
-    } else {
+    // Find functions matching the given name.
+    SymbolContextList sc_list;
+    target->GetImages().FindFunctions(
+        name, eFunctionNameTypeAuto, include_symbols, include_inlines, sc_list);
+
+    AddressRange range;
+    const uint32_t scope =
+        eSymbolContextBlock | eSymbolContextFunction | eSymbolContextSymbol;
+    const bool use_inline_block_range = true;
+    for (SymbolContext sc : sc_list.SymbolContexts()) {
+      for (uint32_t range_idx = 0;
+           sc.GetAddressRange(scope, range_idx, use_inline_block_range, range);
+           ++range_idx) {
+        ranges.push_back(range);
+      }
+    }
+    if (ranges.empty()) {
       result.AppendErrorWithFormat("Unable to find symbol with name '%s'.\n",
                                    name.GetCString());
       result.SetStatus(eReturnStatusFailed);
+      return result.Succeeded();
     }
   } else {
-    std::vector<AddressRange> ranges;
     AddressRange range;
     StackFrame *frame = m_exe_ctx.GetFramePtr();
     if (m_options.frame_line) {
@@ -444,45 +455,44 @@ bool CommandObjectDisassemble::DoExecute(Args &command,
       }
       ranges.push_back(range);
     }
+  }
 
-    bool print_sc_header = ranges.size() > 1;
-    for (AddressRange cur_range : ranges) {
-      bool success;
-      if (m_options.num_instructions != 0) {
-        success = Disassembler::Disassemble(
-            GetDebugger(), m_options.arch, plugin_name, flavor_string,
-            m_exe_ctx, cur_range.GetBaseAddress(), m_options.num_instructions,
-            m_options.show_mixed,
-            m_options.show_mixed ? m_options.num_lines_context : 0, options,
-            result.GetOutputStream());
-      } else {
-        if (cur_range.GetByteSize() == 0)
-          cur_range.SetByteSize(DEFAULT_DISASM_BYTE_SIZE);
-
-        success = Disassembler::Disassemble(
-            GetDebugger(), m_options.arch, plugin_name, flavor_string,
-            m_exe_ctx, cur_range, m_options.num_instructions,
-            m_options.show_mixed,
-            m_options.show_mixed ? m_options.num_lines_context : 0, options,
-            result.GetOutputStream());
-      }
-      if (success) {
-        result.SetStatus(eReturnStatusSuccessFinishResult);
+  bool print_sc_header = ranges.size() > 1;
+  for (AddressRange cur_range : ranges) {
+    bool success;
+    if (m_options.num_instructions != 0) {
+      success = Disassembler::Disassemble(
+          GetDebugger(), m_options.arch, plugin_name, flavor_string, m_exe_ctx,
+          cur_range.GetBaseAddress(), m_options.num_instructions,
+          m_options.show_mixed,
+          m_options.show_mixed ? m_options.num_lines_context : 0, options,
+          result.GetOutputStream());
+    } else {
+      if (cur_range.GetByteSize() == 0)
+        cur_range.SetByteSize(DEFAULT_DISASM_BYTE_SIZE);
+
+      success = Disassembler::Disassemble(
+          GetDebugger(), m_options.arch, plugin_name, flavor_string, m_exe_ctx,
+          cur_range, m_options.num_instructions, m_options.show_mixed,
+          m_options.show_mixed ? m_options.num_lines_context : 0, options,
+          result.GetOutputStream());
+    }
+    if (success) {
+      result.SetStatus(eReturnStatusSuccessFinishResult);
+    } else {
+      if (m_options.symbol_containing_addr != LLDB_INVALID_ADDRESS) {
+        result.AppendErrorWithFormat(
+            "Failed to disassemble memory in function at 0x%8.8" PRIx64 ".\n",
+            m_options.symbol_containing_addr);
       } else {
-        if (m_options.symbol_containing_addr != LLDB_INVALID_ADDRESS) {
-          result.AppendErrorWithFormat(
-              "Failed to disassemble memory in function at 0x%8.8" PRIx64 ".\n",
-              m_options.symbol_containing_addr);
-        } else {
-          result.AppendErrorWithFormat(
-              "Failed to disassemble memory at 0x%8.8" PRIx64 ".\n",
-              cur_range.GetBaseAddress().GetLoadAddress(target));
-        }
-        result.SetStatus(eReturnStatusFailed);
+        result.AppendErrorWithFormat(
+            "Failed to disassemble memory at 0x%8.8" PRIx64 ".\n",
+            cur_range.GetBaseAddress().GetLoadAddress(target));
       }
-      if (print_sc_header)
-        result.AppendMessage("\n");
+      result.SetStatus(eReturnStatusFailed);
     }
+    if (print_sc_header)
+      result.AppendMessage("\n");
   }
 
   return result.Succeeded();

diff  --git a/lldb/source/Core/Disassembler.cpp b/lldb/source/Core/Disassembler.cpp
index 268e25fb6697..c08dcc9739db 100644
--- a/lldb/source/Core/Disassembler.cpp
+++ b/lldb/source/Core/Disassembler.cpp
@@ -126,69 +126,6 @@ static void ResolveAddress(const ExecutionContext &exe_ctx, const Address &addr,
   resolved_addr = addr;
 }
 
-size_t Disassembler::Disassemble(Debugger &debugger, const ArchSpec &arch,
-                                 const char *plugin_name, const char *flavor,
-                                 const ExecutionContext &exe_ctx,
-                                 SymbolContextList &sc_list,
-                                 uint32_t num_instructions,
-                                 bool mixed_source_and_assembly,
-                                 uint32_t num_mixed_context_lines,
-                                 uint32_t options, Stream &strm) {
-  size_t success_count = 0;
-  const size_t count = sc_list.GetSize();
-  SymbolContext sc;
-  AddressRange range;
-  const uint32_t scope =
-      eSymbolContextBlock | eSymbolContextFunction | eSymbolContextSymbol;
-  const bool use_inline_block_range = true;
-  for (size_t i = 0; i < count; ++i) {
-    if (!sc_list.GetContextAtIndex(i, sc))
-      break;
-    for (uint32_t range_idx = 0;
-         sc.GetAddressRange(scope, range_idx, use_inline_block_range, range);
-         ++range_idx) {
-      if (Disassemble(debugger, arch, plugin_name, flavor, exe_ctx, range,
-                      num_instructions, mixed_source_and_assembly,
-                      num_mixed_context_lines, options, strm)) {
-        ++success_count;
-        strm.EOL();
-      }
-    }
-  }
-  return success_count;
-}
-
-bool Disassembler::Disassemble(
-    Debugger &debugger, const ArchSpec &arch, const char *plugin_name,
-    const char *flavor, const ExecutionContext &exe_ctx, ConstString name,
-    Module *module, uint32_t num_instructions, bool mixed_source_and_assembly,
-    uint32_t num_mixed_context_lines, uint32_t options, Stream &strm) {
-  // If no name is given there's nothing to disassemble.
-  if (!name)
-    return false;
-
-  const bool include_symbols = true;
-  const bool include_inlines = true;
-
-  // Find functions matching the given name.
-  SymbolContextList sc_list;
-  if (module) {
-    module->FindFunctions(name, CompilerDeclContext(), eFunctionNameTypeAuto,
-                          include_symbols, include_inlines, sc_list);
-  } else if (exe_ctx.GetTargetPtr()) {
-    exe_ctx.GetTargetPtr()->GetImages().FindFunctions(
-        name, eFunctionNameTypeAuto, include_symbols, include_inlines, sc_list);
-  }
-
-  // If no functions were found there's nothing to disassemble.
-  if (sc_list.IsEmpty())
-    return false;
-
-  return Disassemble(debugger, arch, plugin_name, flavor, exe_ctx, sc_list,
-                     num_instructions, mixed_source_and_assembly,
-                     num_mixed_context_lines, options, strm);
-}
-
 lldb::DisassemblerSP Disassembler::DisassembleRange(
     const ArchSpec &arch, const char *plugin_name, const char *flavor,
     const ExecutionContext &exe_ctx, const AddressRange &range,


        


More information about the lldb-commits mailing list