74 lines · python
1"""2Test the memory commands operating on memory regions with holes (inaccessible3pages)4"""5 6import lldb7from lldbsuite.test.lldbtest import *8import lldbsuite.test.lldbutil as lldbutil9from lldbsuite.test.decorators import *10 11 12class MemoryHolesTestCase(TestBase):13 NO_DEBUG_INFO_TESTCASE = True14 15 def setUp(self):16 super().setUp()17 # Find the line number to break inside main().18 self.line = line_number("main.cpp", "// break here")19 20 def _prepare_inferior(self):21 self.build()22 exe = self.getBuildArtifact("a.out")23 self.runCmd("file " + exe, CURRENT_EXECUTABLE_SET)24 25 # Break in main() after the variables are assigned values.26 lldbutil.run_break_set_by_file_and_line(27 self, "main.cpp", self.line, num_expected_locations=1, loc_exact=True28 )29 30 self.runCmd("run", RUN_SUCCEEDED)31 32 # The stop reason of the thread should be breakpoint.33 self.expect(34 "thread list",35 STOPPED_DUE_TO_BREAKPOINT,36 substrs=["stopped", "stop reason = breakpoint"],37 )38 39 # The breakpoint should have a hit count of 1.40 lldbutil.check_breakpoint(self, bpno=1, expected_hit_count=1)41 42 # Avoid the expression evaluator, as it can allocate allocate memory43 # inside the holes we've deliberately left empty.44 self.memory = self.frame().FindVariable("mem_with_holes").GetValueAsUnsigned()45 self.pagesize = self.frame().FindVariable("pagesize").GetValueAsUnsigned()46 self.num_pages = (47 self.target().FindFirstGlobalVariable("num_pages").GetValueAsUnsigned()48 )49 positions = self.frame().FindVariable("positions")50 self.positions = [51 positions.GetChildAtIndex(i).GetValueAsUnsigned()52 for i in range(positions.GetNumChildren())53 ]54 self.assertEqual(len(self.positions), 5)55 56 def test_memory_read(self):57 self._prepare_inferior()58 59 error = lldb.SBError()60 content = self.process().ReadMemory(self.memory, 2 * self.pagesize, error)61 self.assertEqual(len(content), self.pagesize)62 self.assertEqual(content[0:7], b"needle\0")63 self.assertTrue(error.Fail())64 65 def test_memory_find(self):66 self._prepare_inferior()67 68 matches = [f"data found at location: {p:#x}" for p in self.positions]69 self.expect(70 f'memory find --count {len(self.positions)+1} --string "needle" '71 f"{self.memory:#x} {self.memory+self.pagesize*self.num_pages:#x}",72 substrs=matches + ["no more matches within the range"],73 )74