xref: /llvm-project/lldb/test/API/commands/memory/write/TestMemoryWrite.py (revision 2238dcc39358353cac21df75c3c3286ab20b8f53)
194038c57SVenkata Ramanaiah Nalamothu"""
294038c57SVenkata Ramanaiah NalamothuTest the 'memory write' command.
394038c57SVenkata Ramanaiah Nalamothu"""
494038c57SVenkata Ramanaiah Nalamothu
594038c57SVenkata Ramanaiah Nalamothuimport lldb
694038c57SVenkata Ramanaiah Nalamothuimport lldbsuite.test.lldbutil as lldbutil
794038c57SVenkata Ramanaiah Nalamothu
894038c57SVenkata Ramanaiah Nalamothufrom lldbsuite.test.decorators import *
994038c57SVenkata Ramanaiah Nalamothufrom lldbsuite.test.lldbtest import *
1094038c57SVenkata Ramanaiah Nalamothu
1194038c57SVenkata Ramanaiah Nalamothu
1294038c57SVenkata Ramanaiah Nalamothuclass MemoryWriteTestCase(TestBase):
1394038c57SVenkata Ramanaiah Nalamothu    def setUp(self):
1494038c57SVenkata Ramanaiah Nalamothu        # Call super's setUp().
1594038c57SVenkata Ramanaiah Nalamothu        TestBase.setUp(self)
1694038c57SVenkata Ramanaiah Nalamothu        # Find the line number to break inside main().
17*2238dcc3SJonas Devlieghere        self.line = line_number("main.c", "// Set break point at this line.")
1894038c57SVenkata Ramanaiah Nalamothu
1994038c57SVenkata Ramanaiah Nalamothu    def build_run_stop(self):
2094038c57SVenkata Ramanaiah Nalamothu        self.build()
2194038c57SVenkata Ramanaiah Nalamothu        exe = self.getBuildArtifact("a.out")
2294038c57SVenkata Ramanaiah Nalamothu        self.runCmd("file " + exe, CURRENT_EXECUTABLE_SET)
2394038c57SVenkata Ramanaiah Nalamothu
2494038c57SVenkata Ramanaiah Nalamothu        # Break in main() after the variables are assigned values.
25*2238dcc3SJonas Devlieghere        lldbutil.run_break_set_by_file_and_line(
26*2238dcc3SJonas Devlieghere            self, "main.c", self.line, num_expected_locations=1, loc_exact=True
27*2238dcc3SJonas Devlieghere        )
2894038c57SVenkata Ramanaiah Nalamothu
2994038c57SVenkata Ramanaiah Nalamothu        self.runCmd("run", RUN_SUCCEEDED)
3094038c57SVenkata Ramanaiah Nalamothu
3194038c57SVenkata Ramanaiah Nalamothu        # The stop reason of the thread should be breakpoint.
32*2238dcc3SJonas Devlieghere        self.expect(
33*2238dcc3SJonas Devlieghere            "thread list",
3494038c57SVenkata Ramanaiah Nalamothu            STOPPED_DUE_TO_BREAKPOINT,
35*2238dcc3SJonas Devlieghere            substrs=["stopped", "stop reason = breakpoint"],
36*2238dcc3SJonas Devlieghere        )
3794038c57SVenkata Ramanaiah Nalamothu
3894038c57SVenkata Ramanaiah Nalamothu        # The breakpoint should have a hit count of 1.
3994038c57SVenkata Ramanaiah Nalamothu        lldbutil.check_breakpoint(self, bpno=1, expected_hit_count=1)
4094038c57SVenkata Ramanaiah Nalamothu
4194038c57SVenkata Ramanaiah Nalamothu    @no_debug_info_test
4294038c57SVenkata Ramanaiah Nalamothu    def test_memory_write(self):
4394038c57SVenkata Ramanaiah Nalamothu        """Test the 'memory write' command for writing values and file contents."""
4494038c57SVenkata Ramanaiah Nalamothu        self.build_run_stop()
4594038c57SVenkata Ramanaiah Nalamothu
4694038c57SVenkata Ramanaiah Nalamothu        self.expect(
4794038c57SVenkata Ramanaiah Nalamothu            "memory read --format c --size 7 --count 1 `&my_string`",
48*2238dcc3SJonas Devlieghere            substrs=["abcdefg"],
49*2238dcc3SJonas Devlieghere        )
5094038c57SVenkata Ramanaiah Nalamothu
51*2238dcc3SJonas Devlieghere        self.expect("memory write --format c --size 7 `&my_string` ABCDEFG")
5294038c57SVenkata Ramanaiah Nalamothu
5394038c57SVenkata Ramanaiah Nalamothu        self.expect(
5494038c57SVenkata Ramanaiah Nalamothu            "memory read --format c --size 7 --count 1 `&my_string`",
55*2238dcc3SJonas Devlieghere            substrs=["ABCDEFG"],
56*2238dcc3SJonas Devlieghere        )
5794038c57SVenkata Ramanaiah Nalamothu
5894038c57SVenkata Ramanaiah Nalamothu        self.expect(
5994038c57SVenkata Ramanaiah Nalamothu            "memory write --infile file.txt --size 7 `&my_string`",
60*2238dcc3SJonas Devlieghere            substrs=["7 bytes were written"],
61*2238dcc3SJonas Devlieghere        )
6294038c57SVenkata Ramanaiah Nalamothu
6394038c57SVenkata Ramanaiah Nalamothu        self.expect(
6494038c57SVenkata Ramanaiah Nalamothu            "memory read --format c --size 7 --count 1 `&my_string`",
65*2238dcc3SJonas Devlieghere            substrs=["abcdefg"],
66*2238dcc3SJonas Devlieghere        )
6794038c57SVenkata Ramanaiah Nalamothu
6894038c57SVenkata Ramanaiah Nalamothu        self.expect(
69*2238dcc3SJonas Devlieghere            "memory write --infile file.txt --size 7 `&my_string` ABCDEFG",
70*2238dcc3SJonas Devlieghere            error=True,
71*2238dcc3SJonas Devlieghere            substrs=[
72*2238dcc3SJonas Devlieghere                "error: memory write takes only a destination address when writing file contents"
73*2238dcc3SJonas Devlieghere            ],
74*2238dcc3SJonas Devlieghere        )
7594038c57SVenkata Ramanaiah Nalamothu
7694038c57SVenkata Ramanaiah Nalamothu        self.expect(
77*2238dcc3SJonas Devlieghere            "memory write --infile file.txt --size 7",
78*2238dcc3SJonas Devlieghere            error=True,
79*2238dcc3SJonas Devlieghere            substrs=[
80*2238dcc3SJonas Devlieghere                "error: memory write takes a destination address when writing file contents"
81*2238dcc3SJonas Devlieghere            ],
82*2238dcc3SJonas Devlieghere        )
8394038c57SVenkata Ramanaiah Nalamothu
8494038c57SVenkata Ramanaiah Nalamothu    @no_debug_info_test
8594038c57SVenkata Ramanaiah Nalamothu    def test_memory_write_command_usage_syntax(self):
8694038c57SVenkata Ramanaiah Nalamothu        """Test that 'memory write' command usage syntax shows it does not take values when writing file contents."""
8794038c57SVenkata Ramanaiah Nalamothu        self.expect(
8894038c57SVenkata Ramanaiah Nalamothu            "help memory write",
8994038c57SVenkata Ramanaiah Nalamothu            substrs=[
9094038c57SVenkata Ramanaiah Nalamothu                "memory write [-f <format>] [-s <byte-size>] <address> <value> [<value> [...]]",
91*2238dcc3SJonas Devlieghere                "memory write -i <filename> [-s <byte-size>] [-o <offset>] <address>",
92*2238dcc3SJonas Devlieghere            ],
93*2238dcc3SJonas Devlieghere        )
94