1"""
2Test the 'memory read' command.
3"""
4
5import os, time
6import re
7import unittest2
8import lldb
9from lldbtest import *
10import lldbutil
11
12class MemoryReadTestCase(TestBase):
13
14    mydir = os.path.join("functionalities", "memory", "read")
15
16    @unittest2.skipUnless(sys.platform.startswith("darwin"), "requires Darwin")
17    @dsym_test
18    def test_memory_read_with_dsym(self):
19        """Test the 'memory read' command with plain and vector formats."""
20        self.buildDsym()
21        self.memory_read_command()
22
23    @dwarf_test
24    def test_memory_read_with_dwarf(self):
25        """Test the 'memory read' command with plain and vector formats."""
26        self.buildDwarf()
27        self.memory_read_command()
28
29    def setUp(self):
30        # Call super's setUp().
31        TestBase.setUp(self)
32        # Find the line number to break inside main().
33        self.line = line_number('main.cpp', '// Set break point at this line.')
34
35    def memory_read_command(self):
36        """Test the 'memory read' command with plain and vector formats."""
37        exe = os.path.join(os.getcwd(), "a.out")
38        self.runCmd("file " + exe, CURRENT_EXECUTABLE_SET)
39
40        # Break in main() aftre the variables are assigned values.
41        lldbutil.run_break_set_by_file_and_line (self, "main.cpp", self.line, num_expected_locations=1, loc_exact=True)
42
43        self.runCmd("run", RUN_SUCCEEDED)
44
45        # The stop reason of the thread should be breakpoint.
46        self.expect("thread list", STOPPED_DUE_TO_BREAKPOINT,
47            substrs = ['stopped', 'stop reason = breakpoint'])
48
49        # The breakpoint should have a hit count of 1.
50        self.expect("breakpoint list -f", BREAKPOINT_HIT_ONCE,
51            substrs = [' resolved, hit count = 1'])
52
53        # Test the memory read commands.
54
55        # (lldb) memory read -f d -c 1 `&argc`
56        # 0x7fff5fbff9a0: 1
57        self.runCmd("memory read -f d -c 1 `&argc`")
58
59        # Find the starting address for variable 'argc' to verify later that the
60        # '--format uint32_t[] --size 4 --count 4' option increments the address
61        # correctly.
62        line = self.res.GetOutput().splitlines()[0]
63        items = line.split(':')
64        address = int(items[0], 0)
65        argc = int(items[1], 0)
66        self.assertTrue(address > 0 and argc == 1)
67
68        # (lldb) memory read --format uint32_t[] --size 4 --count 4 `&argc`
69        # 0x7fff5fbff9a0: {0x00000001}
70        # 0x7fff5fbff9a4: {0x00000000}
71        # 0x7fff5fbff9a8: {0x0ec0bf27}
72        # 0x7fff5fbff9ac: {0x215db505}
73        self.runCmd("memory read --format uint32_t[] --size 4 --count 4 `&argc`")
74        lines = self.res.GetOutput().splitlines()
75        for i in range(4):
76            if i == 0:
77                # Verify that the printout for argc is correct.
78                self.assertTrue(argc == int(lines[i].split(':')[1].strip(' {}'), 0))
79            addr = int(lines[i].split(':')[0], 0)
80            # Verify that the printout for addr is incremented correctly.
81            self.assertTrue(addr == (address + i*4))
82
83        # (lldb) memory read --format char[] --size 7 --count 1 `&my_string`
84        # 0x7fff5fbff990: {abcdefg}
85        self.expect("memory read --format char[] --size 7 --count 1 `&my_string`",
86            substrs = ['abcdefg'])
87
88        # (lldb) memory read --format 'hex float' --size 16 `&argc`
89        # 0x7fff5fbff5b0: error: unsupported byte size (16) for hex float format
90        self.expect("memory read --format 'hex float' --size 16 `&argc`",
91            substrs = ['unsupported byte size (16) for hex float format'])
92
93        self.expect("memory read --format 'float' --count 1 --size 8 `&my_double`",
94            substrs = ['1234.'])
95
96        # (lldb) memory read --format 'float' --count 1 --size 20 `&my_double`
97        # 0x7fff5fbff598: error: unsupported byte size (20) for float format
98        self.expect("memory read --format 'float' --count 1 --size 20 `&my_double`",
99            substrs = ['unsupported byte size (20) for float format'])
100
101
102if __name__ == '__main__':
103    import atexit
104    lldb.SBDebugger.Initialize()
105    atexit.register(lambda: lldb.SBDebugger.Terminate())
106    unittest2.main()
107