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_help(self):
27        """ Test that help shows you must have one of address or --all, not both."""
28        self.expect("help memory region",
29            substrs=["memory region <address-expression>",
30                     "memory region -a"])
31
32    def test(self):
33        self.build()
34
35        # Set breakpoint in main and run
36        self.runCmd("file " + self.getBuildArtifact("a.out"), CURRENT_EXECUTABLE_SET)
37        lldbutil.run_break_set_by_file_and_line(
38            self, "main.cpp", self.line, num_expected_locations=-1, loc_exact=True)
39
40        self.runCmd("run", RUN_SUCCEEDED)
41
42        interp = self.dbg.GetCommandInterpreter()
43        result = lldb.SBCommandReturnObject()
44
45        # Test that the first 'memory region' command prints the usage.
46        interp.HandleCommand("memory region", result)
47        self.assertFalse(result.Succeeded())
48        self.assertEqual(result.GetError(),
49                    "error: 'memory region' takes one argument or \"--all\" option:\n"
50                    "Usage: memory region <address-expression> (or --all)\n")
51
52        # We allow --all or an address argument, not both
53        interp.HandleCommand("memory region --all 0", result)
54        self.assertFalse(result.Succeeded())
55        self.assertRegexpMatches(result.GetError(),
56                "The \"--all\" option cannot be used when an address argument is given")
57
58        # Test that when the address fails to parse, we show an error and do not continue
59        interp.HandleCommand("memory region not_an_address", result)
60        self.assertFalse(result.Succeeded())
61        self.assertEqual(result.GetError(),
62                "error: invalid address argument \"not_an_address\": address expression \"not_an_address\" evaluation failed\n")
63
64        # Accumulate the results to compare with the --all output
65        all_regions = ""
66
67        # Now let's print the memory region starting at 0 which should always work.
68        interp.HandleCommand("memory region 0x0", result)
69        self.assertTrue(result.Succeeded())
70        self.assertRegexpMatches(result.GetOutput(), "\\[0x0+-")
71        all_regions += result.GetOutput()
72
73        # Keep printing memory regions until we printed all of them.
74        while True:
75            interp.HandleCommand("memory region", result)
76            if not result.Succeeded():
77                break
78            all_regions += result.GetOutput()
79
80        # Now that we reached the end, 'memory region' should again print the usage.
81        interp.HandleCommand("memory region", result)
82        self.assertFalse(result.Succeeded())
83        self.assertRegexpMatches(result.GetError(), "Usage: memory region <address\-expression> \(or \-\-all\)")
84
85        # --all should match what repeating the command gives you
86        interp.HandleCommand("memory region --all", result)
87        self.assertTrue(result.Succeeded())
88        self.assertEqual(result.GetOutput(), all_regions)
89