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