[Lldb-commits] [lldb] 3245dd5 - [lldb] Reduce duplication in CommandObjectDisassemble

Pavel Labath via lldb-commits lldb-commits at lists.llvm.org
Wed Mar 4 04:47:44 PST 2020


Author: Pavel Labath
Date: 2020-03-04T13:47:35+01:00
New Revision: 3245dd59b124cbfce8cdce1f2c90e843d57d8722

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

LOG: [lldb] Reduce duplication in CommandObjectDisassemble

This command had nearly identical code for the "then" and "else"
branches of the "if (m_options.num_instructions != 0)" condition.

This patch factors out the common parts of the two blocks to reduce
duplication.

Added: 
    

Modified: 
    lldb/source/Commands/CommandObjectDisassemble.cpp
    lldb/test/Shell/Commands/Inputs/command-disassemble-process.lldbinit
    lldb/test/Shell/Commands/command-disassemble-process.yaml

Removed: 
    


################################################################################
diff  --git a/lldb/source/Commands/CommandObjectDisassemble.cpp b/lldb/source/Commands/CommandObjectDisassemble.cpp
index d51313357fee..0645119767da 100644
--- a/lldb/source/Commands/CommandObjectDisassemble.cpp
+++ b/lldb/source/Commands/CommandObjectDisassemble.cpp
@@ -424,94 +424,64 @@ bool CommandObjectDisassemble::DoExecute(Args &command,
     } else
       ranges.push_back(range);
 
-    if (m_options.num_instructions != 0) {
-      if (ranges.empty()) {
-        // The default action is to disassemble the current frame function.
-        if (frame) {
-          SymbolContext sc(frame->GetSymbolContext(eSymbolContextFunction |
-                                                   eSymbolContextSymbol));
-          if (sc.function)
-            range.GetBaseAddress() =
-                sc.function->GetAddressRange().GetBaseAddress();
-          else if (sc.symbol && sc.symbol->ValueIsAddress())
-            range.GetBaseAddress() = sc.symbol->GetAddress();
-          else
-            range.GetBaseAddress() = frame->GetFrameCodeAddress();
-        }
-
-        if (!range.GetBaseAddress().IsValid()) {
-          result.AppendError("invalid frame");
-          result.SetStatus(eReturnStatusFailed);
-          return false;
-        }
+    if (ranges.empty()) {
+      // The default action is to disassemble the current frame function.
+      if (frame) {
+        SymbolContext sc(frame->GetSymbolContext(eSymbolContextFunction |
+                                                 eSymbolContextSymbol));
+        if (sc.function)
+          range = sc.function->GetAddressRange();
+        else if (sc.symbol && sc.symbol->ValueIsAddress()) {
+          range.GetBaseAddress() = sc.symbol->GetAddress();
+          range.SetByteSize(sc.symbol->GetByteSize());
+        } else
+          range.GetBaseAddress() = frame->GetFrameCodeAddress();
       }
-
-      bool print_sc_header = ranges.size() > 1;
-      for (AddressRange cur_range : ranges) {
-        if (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())) {
-          result.SetStatus(eReturnStatusSuccessFinishResult);
-        } else {
-          if (m_options.start_addr != LLDB_INVALID_ADDRESS)
-            result.AppendErrorWithFormat(
-                "Failed to disassemble memory at 0x%8.8" PRIx64 ".\n",
-                m_options.start_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);
-          result.SetStatus(eReturnStatusFailed);
-        }
-      }
-      if (print_sc_header)
-        result.AppendMessage("\n");
-    } else {
-      if (ranges.empty()) {
-        // The default action is to disassemble the current frame function.
-        if (frame) {
-          SymbolContext sc(frame->GetSymbolContext(eSymbolContextFunction |
-                                                   eSymbolContextSymbol));
-          if (sc.function)
-            range = sc.function->GetAddressRange();
-          else if (sc.symbol && sc.symbol->ValueIsAddress()) {
-            range.GetBaseAddress() = sc.symbol->GetAddress();
-            range.SetByteSize(sc.symbol->GetByteSize());
-          } else
-            range.GetBaseAddress() = frame->GetFrameCodeAddress();
-        } else {
-          result.AppendError("invalid frame");
-          result.SetStatus(eReturnStatusFailed);
-          return false;
-        }
-        ranges.push_back(range);
+      if (!range.GetBaseAddress().IsValid()) {
+        result.AppendError("invalid frame");
+        result.SetStatus(eReturnStatusFailed);
+        return false;
       }
+      ranges.push_back(range);
+    }
 
-      bool print_sc_header = ranges.size() > 1;
-      for (AddressRange cur_range : ranges) {
+    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);
 
-        if (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())) {
-          result.SetStatus(eReturnStatusSuccessFinishResult);
+        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 {
           result.AppendErrorWithFormat(
               "Failed to disassemble memory at 0x%8.8" PRIx64 ".\n",
               cur_range.GetBaseAddress().GetLoadAddress(target));
-          result.SetStatus(eReturnStatusFailed);
         }
-        if (print_sc_header)
-          result.AppendMessage("\n");
+        result.SetStatus(eReturnStatusFailed);
       }
+      if (print_sc_header)
+        result.AppendMessage("\n");
     }
   }
 

diff  --git a/lldb/test/Shell/Commands/Inputs/command-disassemble-process.lldbinit b/lldb/test/Shell/Commands/Inputs/command-disassemble-process.lldbinit
index a8f314a7759f..71f3906c3e6c 100644
--- a/lldb/test/Shell/Commands/Inputs/command-disassemble-process.lldbinit
+++ b/lldb/test/Shell/Commands/Inputs/command-disassemble-process.lldbinit
@@ -4,4 +4,5 @@ disassemble --frame
 disassemble --pc
 disassemble --address 0x4004
 disassemble --address 0xdead
+disassemble --count 7
 disassemble --pc --count 7

diff  --git a/lldb/test/Shell/Commands/command-disassemble-process.yaml b/lldb/test/Shell/Commands/command-disassemble-process.yaml
index 9f932dca0289..7dc14517c24c 100644
--- a/lldb/test/Shell/Commands/command-disassemble-process.yaml
+++ b/lldb/test/Shell/Commands/command-disassemble-process.yaml
@@ -40,6 +40,15 @@
 # CHECK-NEXT:     0x4008 <+6>: addb   %al, (%rsi)
 # CHECK-NEXT: (lldb) disassemble --address 0xdead
 # CHECK-NEXT: error: Could not find function bounds for address 0xdead
+# CHECK-NEXT: (lldb) disassemble --count 7
+# CHECK-NEXT: command-disassemble-process.exe`main:
+# CHECK-NEXT:     0x4002 <+0>: addb   %al, (%rcx)
+# CHECK-NEXT: ->  0x4004 <+2>: addb   %al, (%rdx)
+# CHECK-NEXT:     0x4006 <+4>: addb   %al, (%rbx)
+# CHECK-NEXT:     0x4008 <+6>: addb   %al, (%rsi)
+# CHECK-NEXT:     0x400a:      addb   %al, (%rdi)
+# CHECK-NEXT:     0x400c:      addb   %cl, (%rax)
+# CHECK-NEXT:     0x400e:      addb   %cl, (%rcx)
 # CHECK-NEXT: (lldb) disassemble --pc --count 7
 # CHECK-NEXT: command-disassemble-process.exe`main:
 # CHECK-NEXT: ->  0x4004 <+2>: addb   %al, (%rdx)


        


More information about the lldb-commits mailing list