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 setup_program(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    def test_command(self):
41        self.setup_program()
42
43        interp = self.dbg.GetCommandInterpreter()
44        result = lldb.SBCommandReturnObject()
45
46        # Test that the first 'memory region' command prints the usage.
47        interp.HandleCommand("memory region", result)
48        self.assertFalse(result.Succeeded())
49        self.assertEqual(result.GetError(),
50                    "error: 'memory region' takes one argument or \"--all\" option:\n"
51                    "Usage: memory region <address-expression> (or --all)\n")
52
53        # We allow --all or an address argument, not both
54        interp.HandleCommand("memory region --all 0", result)
55        self.assertFalse(result.Succeeded())
56        self.assertRegexpMatches(result.GetError(),
57                "The \"--all\" option cannot be used when an address argument is given")
58
59        # Test that when the address fails to parse, we show an error and do not continue
60        interp.HandleCommand("memory region not_an_address", result)
61        self.assertFalse(result.Succeeded())
62        self.assertEqual(result.GetError(),
63                "error: invalid address argument \"not_an_address\": address expression \"not_an_address\" evaluation failed\n")
64
65        # Accumulate the results to compare with the --all output
66        all_regions = ""
67
68        # Now let's print the memory region starting at 0 which should always work.
69        interp.HandleCommand("memory region 0x0", result)
70        self.assertTrue(result.Succeeded())
71        self.assertRegexpMatches(result.GetOutput(), "\\[0x0+-")
72        all_regions += result.GetOutput()
73
74        # Keep printing memory regions until we printed all of them.
75        while True:
76            interp.HandleCommand("memory region", result)
77            if not result.Succeeded():
78                break
79            all_regions += result.GetOutput()
80
81        # Now that we reached the end, 'memory region' should again print the usage.
82        interp.HandleCommand("memory region", result)
83        self.assertFalse(result.Succeeded())
84        self.assertRegexpMatches(result.GetError(), "Usage: memory region <address\-expression> \(or \-\-all\)")
85
86        # --all should match what repeating the command gives you
87        interp.HandleCommand("memory region --all", result)
88        self.assertTrue(result.Succeeded())
89        self.assertEqual(result.GetOutput(), all_regions)
90
91    def test_no_overlapping_regions(self):
92        # In the past on Windows we were recording AllocationBase as the base address
93        # of the current region, not BaseAddress. So if a range of pages was split
94        # into regions you would see several regions with the same base address.
95        # This checks that this no longer happens (and it shouldn't happen on any
96        # other OS either).
97        self.setup_program()
98
99        regions = self.process().GetMemoryRegions()
100        num_regions = regions.GetSize()
101
102        if num_regions:
103            region = lldb.SBMemoryRegionInfo()
104            regions.GetMemoryRegionAtIndex(0, region)
105            previous_base = region.GetRegionBase()
106            previous_end = region.GetRegionEnd()
107
108            for idx in range(1, regions.GetSize()):
109                regions.GetMemoryRegionAtIndex(idx, region)
110
111                # Check that it does not overlap the previous region.
112                # This could happen if we got the base addresses or size wrong.
113                # Also catches the base addresses being the same.
114                region_base = region.GetRegionBase()
115                region_end = region.GetRegionEnd()
116
117                self.assertFalse(
118                    (region_base < previous_end) and (previous_base < region_end),
119                    "Unexpected overlapping memory region found.")
120
121                previous_base = region_base
122                previous_end = region_end