1"""
2Test the 'memory region' command.
3"""
4
5
6
7import lldb
8from lldbsuite.test.decorators import *
9from lldbsuite.test.lldbtest import *
10from lldbsuite.test import lldbutil
11
12
13class MemoryCommandRegion(TestBase):
14
15    mydir = TestBase.compute_mydir(__file__)
16
17    NO_DEBUG_INFO_TESTCASE = True
18
19    def setUp(self):
20        TestBase.setUp(self)
21        # Find the line number to break for main.c.
22        self.line = line_number(
23            'main.cpp',
24            '// Run here before printing memory regions')
25
26    def test(self):
27        self.build()
28
29        # Set breakpoint in main and run
30        self.runCmd("file " + self.getBuildArtifact("a.out"), CURRENT_EXECUTABLE_SET)
31        lldbutil.run_break_set_by_file_and_line(
32            self, "main.cpp", self.line, num_expected_locations=-1, loc_exact=True)
33
34        self.runCmd("run", RUN_SUCCEEDED)
35
36        interp = self.dbg.GetCommandInterpreter()
37        result = lldb.SBCommandReturnObject()
38
39        # Test that the first 'memory region' command prints the usage.
40        interp.HandleCommand("memory region", result)
41        self.assertFalse(result.Succeeded())
42        self.assertRegexpMatches(result.GetError(), "Usage: memory region ADDR")
43
44        # Test that when the address fails to parse, we show an error and do not continue
45        interp.HandleCommand("memory region not_an_address", result)
46        self.assertFalse(result.Succeeded())
47        self.assertEqual(result.GetError(),
48                "error: invalid address argument \"not_an_address\": address expression \"not_an_address\" evaluation failed\n")
49
50        # Now let's print the memory region starting at 0 which should always work.
51        interp.HandleCommand("memory region 0x0", result)
52        self.assertTrue(result.Succeeded())
53        self.assertRegexpMatches(result.GetOutput(), "\\[0x0+-")
54
55        # Keep printing memory regions until we printed all of them.
56        while True:
57            interp.HandleCommand("memory region", result)
58            if not result.Succeeded():
59                break
60
61        # Now that we reached the end, 'memory region' should again print the usage.
62        interp.HandleCommand("memory region", result)
63        self.assertFalse(result.Succeeded())
64        self.assertRegexpMatches(result.GetError(), "Usage: memory region ADDR")
65