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