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