211 lines · python
1import gdbremote_testcase2from lldbsuite.test.decorators import *3from lldbsuite.test.lldbtest import *4from lldbsuite.test.lldbdwarf import *5 6 7class TestGdbRemote_qMemoryRegion(gdbremote_testcase.GdbRemoteTestCaseBase):8 def test_qMemoryRegionInfo_is_supported(self):9 self.build()10 self.set_inferior_startup_launch()11 # Start up the inferior.12 procs = self.prep_debug_monitor_and_inferior()13 14 # Ask if it supports $qMemoryRegionInfo.15 self.test_sequence.add_log_lines(16 ["read packet: $qMemoryRegionInfo#00", "send packet: $OK#00"], True17 )18 self.expect_gdbremote_sequence()19 20 @skipIfWindows # No pty support to test any inferior output21 def test_qMemoryRegionInfo_reports_code_address_as_executable(self):22 self.build()23 self.set_inferior_startup_launch()24 25 # Start up the inferior.26 procs = self.prep_debug_monitor_and_inferior(27 inferior_args=["get-code-address-hex:hello", "sleep:5"]28 )29 30 # Run the process31 self.test_sequence.add_log_lines(32 [33 # Start running after initial stop.34 "read packet: $c#63",35 # Match output line that prints the memory address of the message buffer within the inferior.36 # Note we require launch-only testing so we can get inferior otuput.37 {38 "type": "output_match",39 "regex": self.maybe_strict_output_regex(40 r"code address: 0x([0-9a-fA-F]+)\r\n"41 ),42 "capture": {1: "code_address"},43 },44 # Now stop the inferior.45 "read packet: {}".format(chr(3)),46 # And wait for the stop notification.47 {48 "direction": "send",49 "regex": r"^\$T([0-9a-fA-F]{2})thread:([0-9a-fA-F]+);",50 "capture": {1: "stop_signo", 2: "stop_thread_id"},51 },52 ],53 True,54 )55 56 # Run the packet stream.57 context = self.expect_gdbremote_sequence()58 self.assertIsNotNone(context)59 60 # Grab the code address.61 self.assertIsNotNone(context.get("code_address"))62 code_address = int(context.get("code_address"), 16)63 64 # Grab memory region info from the inferior.65 self.reset_test_sequence()66 self.add_query_memory_region_packets(code_address)67 68 # Run the packet stream.69 context = self.expect_gdbremote_sequence()70 self.assertIsNotNone(context)71 mem_region_dict = self.parse_memory_region_packet(context)72 73 # Ensure there are no errors reported.74 self.assertNotIn("error", mem_region_dict)75 76 # Ensure code address is readable and executable.77 self.assertIn("permissions", mem_region_dict)78 self.assertIn("r", mem_region_dict["permissions"])79 self.assertIn("x", mem_region_dict["permissions"])80 81 # Ensure the start address and size encompass the address we queried.82 self.assert_address_within_memory_region(code_address, mem_region_dict)83 84 @skipIfWindows # No pty support to test any inferior output85 def test_qMemoryRegionInfo_reports_stack_address_as_rw(self):86 self.build()87 self.set_inferior_startup_launch()88 89 # Start up the inferior.90 procs = self.prep_debug_monitor_and_inferior(91 inferior_args=["get-stack-address-hex:", "sleep:5"]92 )93 94 # Run the process95 self.test_sequence.add_log_lines(96 [97 # Start running after initial stop.98 "read packet: $c#63",99 # Match output line that prints the memory address of the message buffer within the inferior.100 # Note we require launch-only testing so we can get inferior otuput.101 {102 "type": "output_match",103 "regex": self.maybe_strict_output_regex(104 r"stack address: 0x([0-9a-fA-F]+)\r\n"105 ),106 "capture": {1: "stack_address"},107 },108 # Now stop the inferior.109 "read packet: {}".format(chr(3)),110 # And wait for the stop notification.111 {112 "direction": "send",113 "regex": r"^\$T([0-9a-fA-F]{2})thread:([0-9a-fA-F]+);",114 "capture": {1: "stop_signo", 2: "stop_thread_id"},115 },116 ],117 True,118 )119 120 # Run the packet stream.121 context = self.expect_gdbremote_sequence()122 self.assertIsNotNone(context)123 124 # Grab the address.125 self.assertIsNotNone(context.get("stack_address"))126 stack_address = int(context.get("stack_address"), 16)127 128 # Grab memory region info from the inferior.129 self.reset_test_sequence()130 self.add_query_memory_region_packets(stack_address)131 132 # Run the packet stream.133 context = self.expect_gdbremote_sequence()134 self.assertIsNotNone(context)135 mem_region_dict = self.parse_memory_region_packet(context)136 137 # Ensure there are no errors reported.138 self.assertNotIn("error", mem_region_dict)139 140 # Ensure address is readable and executable.141 self.assertIn("permissions", mem_region_dict)142 self.assertIn("r", mem_region_dict["permissions"])143 self.assertIn("w", mem_region_dict["permissions"])144 145 # Ensure the start address and size encompass the address we queried.146 self.assert_address_within_memory_region(stack_address, mem_region_dict)147 148 @skipIfWindows # No pty support to test any inferior output149 def test_qMemoryRegionInfo_reports_heap_address_as_rw(self):150 self.build()151 self.set_inferior_startup_launch()152 153 # Start up the inferior.154 procs = self.prep_debug_monitor_and_inferior(155 inferior_args=["get-heap-address-hex:", "sleep:5"]156 )157 158 # Run the process159 self.test_sequence.add_log_lines(160 [161 # Start running after initial stop.162 "read packet: $c#63",163 # Match output line that prints the memory address of the message buffer within the inferior.164 # Note we require launch-only testing so we can get inferior otuput.165 {166 "type": "output_match",167 "regex": self.maybe_strict_output_regex(168 r"heap address: 0x([0-9a-fA-F]+)\r\n"169 ),170 "capture": {1: "heap_address"},171 },172 # Now stop the inferior.173 "read packet: {}".format(chr(3)),174 # And wait for the stop notification.175 {176 "direction": "send",177 "regex": r"^\$T([0-9a-fA-F]{2})thread:([0-9a-fA-F]+);",178 "capture": {1: "stop_signo", 2: "stop_thread_id"},179 },180 ],181 True,182 )183 184 # Run the packet stream.185 context = self.expect_gdbremote_sequence()186 self.assertIsNotNone(context)187 188 # Grab the address.189 self.assertIsNotNone(context.get("heap_address"))190 heap_address = int(context.get("heap_address"), 16)191 192 # Grab memory region info from the inferior.193 self.reset_test_sequence()194 self.add_query_memory_region_packets(heap_address)195 196 # Run the packet stream.197 context = self.expect_gdbremote_sequence()198 self.assertIsNotNone(context)199 mem_region_dict = self.parse_memory_region_packet(context)200 201 # Ensure there are no errors reported.202 self.assertNotIn("error", mem_region_dict)203 204 # Ensure address is readable and executable.205 self.assertIn("permissions", mem_region_dict)206 self.assertIn("r", mem_region_dict["permissions"])207 self.assertIn("w", mem_region_dict["permissions"])208 209 # Ensure the start address and size encompass the address we queried.210 self.assert_address_within_memory_region(heap_address, mem_region_dict)211