xref: /llvm-project/lldb/test/API/commands/memory/write/TestMemoryWrite.py (revision 2238dcc39358353cac21df75c3c3286ab20b8f53)
1"""
2Test the 'memory write' command.
3"""
4
5import lldb
6import lldbsuite.test.lldbutil as lldbutil
7
8from lldbsuite.test.decorators import *
9from lldbsuite.test.lldbtest import *
10
11
12class MemoryWriteTestCase(TestBase):
13    def setUp(self):
14        # Call super's setUp().
15        TestBase.setUp(self)
16        # Find the line number to break inside main().
17        self.line = line_number("main.c", "// Set break point at this line.")
18
19    def build_run_stop(self):
20        self.build()
21        exe = self.getBuildArtifact("a.out")
22        self.runCmd("file " + exe, CURRENT_EXECUTABLE_SET)
23
24        # Break in main() after the variables are assigned values.
25        lldbutil.run_break_set_by_file_and_line(
26            self, "main.c", self.line, num_expected_locations=1, loc_exact=True
27        )
28
29        self.runCmd("run", RUN_SUCCEEDED)
30
31        # The stop reason of the thread should be breakpoint.
32        self.expect(
33            "thread list",
34            STOPPED_DUE_TO_BREAKPOINT,
35            substrs=["stopped", "stop reason = breakpoint"],
36        )
37
38        # The breakpoint should have a hit count of 1.
39        lldbutil.check_breakpoint(self, bpno=1, expected_hit_count=1)
40
41    @no_debug_info_test
42    def test_memory_write(self):
43        """Test the 'memory write' command for writing values and file contents."""
44        self.build_run_stop()
45
46        self.expect(
47            "memory read --format c --size 7 --count 1 `&my_string`",
48            substrs=["abcdefg"],
49        )
50
51        self.expect("memory write --format c --size 7 `&my_string` ABCDEFG")
52
53        self.expect(
54            "memory read --format c --size 7 --count 1 `&my_string`",
55            substrs=["ABCDEFG"],
56        )
57
58        self.expect(
59            "memory write --infile file.txt --size 7 `&my_string`",
60            substrs=["7 bytes were written"],
61        )
62
63        self.expect(
64            "memory read --format c --size 7 --count 1 `&my_string`",
65            substrs=["abcdefg"],
66        )
67
68        self.expect(
69            "memory write --infile file.txt --size 7 `&my_string` ABCDEFG",
70            error=True,
71            substrs=[
72                "error: memory write takes only a destination address when writing file contents"
73            ],
74        )
75
76        self.expect(
77            "memory write --infile file.txt --size 7",
78            error=True,
79            substrs=[
80                "error: memory write takes a destination address when writing file contents"
81            ],
82        )
83
84    @no_debug_info_test
85    def test_memory_write_command_usage_syntax(self):
86        """Test that 'memory write' command usage syntax shows it does not take values when writing file contents."""
87        self.expect(
88            "help memory write",
89            substrs=[
90                "memory write [-f <format>] [-s <byte-size>] <address> <value> [<value> [...]]",
91                "memory write -i <filename> [-s <byte-size>] [-o <offset>] <address>",
92            ],
93        )
94