RamNalamothu created this revision.
RamNalamothu added reviewers: JDevlieghere, DavidSpickett, zturner, teemperor.
RamNalamothu requested review of this revision.
Herald added a project: LLDB.
Herald added a subscriber: lldb-commits.

Currently the 'memory write' command allows specifying the values when
writing the file contents to memory but the values are actually ignored. This
patch fixes that by erroring out when values are specified in such cases.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D114544

Files:
  lldb/source/Commands/CommandObjectMemory.cpp
  lldb/source/Interpreter/CommandObject.cpp
  lldb/test/API/commands/memory/write/Makefile
  lldb/test/API/commands/memory/write/TestMemoryWrite.py
  lldb/test/API/commands/memory/write/file.txt
  lldb/test/API/commands/memory/write/main.cpp

Index: lldb/test/API/commands/memory/write/main.cpp
===================================================================
--- /dev/null
+++ lldb/test/API/commands/memory/write/main.cpp
@@ -0,0 +1,13 @@
+#include <stdio.h>
+#include <stdint.h>
+
+int main (int argc, char const *argv[])
+{
+    char my_string[] = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 0};
+    double my_double = 1234.5678;
+    int my_ints[] = {2,4,6,8,10,12,14,16,18,20,22};
+    uint64_t my_uint64s[] = {0, 1, 2, 3, 4, 5, 6, 7};
+    printf("my_string=%s\n", my_string); // Set break point at this line.
+    printf("my_double=%g\n", my_double);
+    return 0;
+}
Index: lldb/test/API/commands/memory/write/file.txt
===================================================================
--- /dev/null
+++ lldb/test/API/commands/memory/write/file.txt
@@ -0,0 +1 @@
+abcdefg
Index: lldb/test/API/commands/memory/write/TestMemoryWrite.py
===================================================================
--- /dev/null
+++ lldb/test/API/commands/memory/write/TestMemoryWrite.py
@@ -0,0 +1,98 @@
+"""
+Test the 'memory write' command.
+"""
+
+import lldb
+import lldbsuite.test.lldbutil as lldbutil
+
+from lldbsuite.test.decorators import *
+from lldbsuite.test.lldbtest import *
+
+
+class MemoryWriteTestCase(TestBase):
+
+    mydir = TestBase.compute_mydir(__file__)
+
+    def setUp(self):
+        # Call super's setUp().
+        TestBase.setUp(self)
+        # Find the line number to break inside main().
+        self.line = line_number('main.cpp', '// Set break point at this line.')
+
+    def build_run_stop(self):
+        self.build()
+        exe = self.getBuildArtifact("a.out")
+        self.runCmd("file " + exe, CURRENT_EXECUTABLE_SET)
+
+        # Break in main() after the variables are assigned values.
+        lldbutil.run_break_set_by_file_and_line(self,
+                                                "main.cpp",
+                                                self.line,
+                                                num_expected_locations=1,
+                                                loc_exact=True)
+
+        self.runCmd("run", RUN_SUCCEEDED)
+
+        # The stop reason of the thread should be breakpoint.
+        self.expect("thread list",
+                    STOPPED_DUE_TO_BREAKPOINT,
+                    substrs=['stopped', 'stop reason = breakpoint'])
+
+        # The breakpoint should have a hit count of 1.
+        lldbutil.check_breakpoint(self, bpno = 1, expected_hit_count = 1)
+
+    @no_debug_info_test
+    def test_memory_write(self):
+        """Test the 'memory write' command for writing values and file contents."""
+        self.build_run_stop()
+
+        # (lldb) memory read --format c --size 7 --count 1 `&my_string`
+        # 0x7fff5fbff990: abcdefg
+        self.expect(
+            "memory read --format c --size 7 --count 1 `&my_string`",
+            substrs=['abcdefg'])
+
+        # (lldb) memory write --format c --size 7 `&my_string` ABCDEFG
+        self.expect(
+            "memory write --format c --size 7 `&my_string` ABCDEFG", error=False)
+
+        # (lldb) memory read --format c --size 7 --count 1 `&my_string`
+        # 0x7fff5fbff990: ABCDEFG
+        self.expect(
+            "memory read --format c --size 7 --count 1 `&my_string`",
+            substrs=['ABCDEFG'])
+
+        # (lldb) memory write --infile file.txt --size 7 `&my_string`
+        # 0x7fff5fbff990: 7 bytes were written
+        self.expect(
+            "memory write --infile file.txt --size 7 `&my_string`",
+            substrs=['7 bytes were written'])
+
+        # (lldb) memory read --format c --size 7 --count 1 `&my_string`
+        # 0x7fff5fbff990: abcdefg
+        self.expect(
+            "memory read --format c --size 7 --count 1 `&my_string`",
+            substrs=['abcdefg'])
+
+        # (lldb) memory write --infile file.txt --size 7 `&my_string` ABCDEFG
+        # 0x7fff5fbff990: error: memory write takes only a destination address when writing file contents.
+        self.expect(
+            "memory write --infile file.txt --size 7 `&my_string` ABCDEFG", error=True,
+            substrs=['memory write takes only a destination address when writing file contents'])
+
+        # (lldb) memory write --infile file.txt --size 7
+        # 0x7fff5fbff990: error: memory write takes a destination address when writing file contents.
+        self.expect(
+            "memory write --infile file.txt --size 7", error=True,
+            substrs=['memory write takes a destination address when writing file contents'])
+
+    @no_debug_info_test
+    def test_memory_write_command_usage_syntax(self):
+        """Test that 'memory write' command usage syntax shows it does not take values when writing file contents."""
+        self.expect(
+            "help memory write",
+            matching=True,
+            substrs=[
+                "Command Options Usage:",
+                "memory write [-f <format>] [-s <byte-size>] <address> <value> [<value> [...]]",
+                "memory write -i <filename> [-s <byte-size>] [-o <offset>] <address>"])
Index: lldb/test/API/commands/memory/write/Makefile
===================================================================
--- /dev/null
+++ lldb/test/API/commands/memory/write/Makefile
@@ -0,0 +1,3 @@
+CXX_SOURCES := main.cpp
+
+include Makefile.rules
Index: lldb/source/Interpreter/CommandObject.cpp
===================================================================
--- lldb/source/Interpreter/CommandObject.cpp
+++ lldb/source/Interpreter/CommandObject.cpp
@@ -454,6 +454,8 @@
         opt_set_mask == LLDB_OPT_SET_ALL
             ? m_arguments[i]
             : OptSetFiltered(opt_set_mask, m_arguments[i]);
+    if (arg_entry.empty())
+      continue;
     int num_alternatives = arg_entry.size();
 
     if ((num_alternatives == 2) && IsPairType(arg_entry[0].arg_repetition)) {
Index: lldb/source/Commands/CommandObjectMemory.cpp
===================================================================
--- lldb/source/Commands/CommandObjectMemory.cpp
+++ lldb/source/Commands/CommandObjectMemory.cpp
@@ -1240,6 +1240,7 @@
     // Define the first (and only) variant of this arg.
     value_arg.arg_type = eArgTypeValue;
     value_arg.arg_repetition = eArgRepeatPlus;
+    value_arg.arg_opt_set_association = LLDB_OPT_SET_1;
 
     // There is only one variant this argument could be; put it into the
     // argument entry.
@@ -1278,6 +1279,12 @@
             m_cmd_name.c_str());
         return false;
       }
+      if (argc > 1) {
+        result.AppendErrorWithFormat(
+            "%s takes only a destination address when writing file contents.\n",
+            m_cmd_name.c_str());
+        return false;
+      }
     } else if (argc < 2) {
       result.AppendErrorWithFormat(
           "%s takes a destination address and at least one value.\n",
_______________________________________________
lldb-commits mailing list
lldb-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/lldb-commits
  • [Lldb-commits... Venkata Ramanaiah Nalamothu via Phabricator via lldb-commits
    • [Lldb-co... David Spickett via Phabricator via lldb-commits
    • [Lldb-co... Venkata Ramanaiah Nalamothu via Phabricator via lldb-commits
    • [Lldb-co... Venkata Ramanaiah Nalamothu via Phabricator via lldb-commits
    • [Lldb-co... David Spickett via Phabricator via lldb-commits
    • [Lldb-co... Venkat via Phabricator via lldb-commits

Reply via email to