547 lines · python
1"""2Test basics of Minidump debugging.3"""4 5import shutil6 7import lldb8from lldbsuite.test.decorators import *9from lldbsuite.test.lldbtest import *10from lldbsuite.test import lldbutil11 12 13class MiniDumpNewTestCase(TestBase):14 NO_DEBUG_INFO_TESTCASE = True15 16 _linux_x86_64_pid = 2991717 _linux_x86_64_not_crashed_pid = 2993918 _linux_x86_64_not_crashed_pid_offset = 0xD96719 20 def process_from_yaml(self, yaml_file):21 minidump_path = self.getBuildArtifact(os.path.basename(yaml_file) + ".dmp")22 self.yaml2obj(yaml_file, minidump_path)23 self.target = self.dbg.CreateTarget(None)24 self.process = self.target.LoadCore(minidump_path)25 return self.process26 27 def check_state(self):28 with open(os.devnull) as devnul:29 # sanitize test output30 self.dbg.SetOutputFileHandle(devnul, False)31 self.dbg.SetErrorFileHandle(devnul, False)32 33 self.assertTrue(self.process.is_stopped)34 35 # Process.Continue36 error = self.process.Continue()37 self.assertFalse(error.Success())38 self.assertTrue(self.process.is_stopped)39 40 # Thread.StepOut41 thread = self.process.GetSelectedThread()42 thread.StepOut()43 self.assertTrue(self.process.is_stopped)44 45 # command line46 self.dbg.HandleCommand("s")47 self.assertTrue(self.process.is_stopped)48 self.dbg.HandleCommand("c")49 self.assertTrue(self.process.is_stopped)50 51 # restore file handles52 self.dbg.SetOutputFileHandle(None, False)53 self.dbg.SetErrorFileHandle(None, False)54 55 def test_loadcore_error_status(self):56 """Test the SBTarget.LoadCore(core, error) overload."""57 minidump_path = self.getBuildArtifact("linux-x86_64.dmp")58 self.yaml2obj("linux-x86_64.yaml", minidump_path)59 self.target = self.dbg.CreateTarget(None)60 error = lldb.SBError()61 self.process = self.target.LoadCore(minidump_path, error)62 self.assertTrue(self.process, PROCESS_IS_VALID)63 self.assertSuccess(error)64 65 def test_loadcore_error_status_failure(self):66 """Test the SBTarget.LoadCore(core, error) overload."""67 self.target = self.dbg.CreateTarget(None)68 error = lldb.SBError()69 self.process = self.target.LoadCore("non-existent.dmp", error)70 self.assertFalse(self.process, PROCESS_IS_VALID)71 self.assertTrue(error.Fail())72 73 def test_process_info_in_minidump(self):74 """Test that lldb can read the process information from the Minidump."""75 self.process_from_yaml("linux-x86_64.yaml")76 self.assertTrue(self.process, PROCESS_IS_VALID)77 self.assertEqual(self.process.GetNumThreads(), 1)78 self.assertEqual(self.process.GetProcessID(), self._linux_x86_64_pid)79 self.check_state()80 81 def test_memory_region_name(self):82 self.process_from_yaml("regions-linux-map.yaml")83 result = lldb.SBCommandReturnObject()84 addr_region_name_pairs = [85 ("0x400d9000", "/system/bin/app_process"),86 ("0x400db000", "/system/bin/app_process"),87 ("0x400dd000", "/system/bin/linker"),88 ("0x400ed000", "/system/bin/linker"),89 ("0x400ee000", "/system/bin/linker"),90 ("0x400fb000", "/system/lib/liblog.so"),91 ("0x400fc000", "/system/lib/liblog.so"),92 ("0x400fd000", "/system/lib/liblog.so"),93 ("0x400ff000", "/system/lib/liblog.so"),94 ("0x40100000", "/system/lib/liblog.so"),95 ("0x40101000", "/system/lib/libc.so"),96 ("0x40122000", "/system/lib/libc.so"),97 ("0x40123000", "/system/lib/libc.so"),98 ("0x40167000", "/system/lib/libc.so"),99 ("0x40169000", "/system/lib/libc.so"),100 ]101 ci = self.dbg.GetCommandInterpreter()102 for addr, region_name in addr_region_name_pairs:103 command = "memory region " + addr104 ci.HandleCommand(command, result, False)105 message = 'Ensure memory "%s" shows up in output for "%s"' % (106 region_name,107 command,108 )109 self.assertIn(region_name, result.GetOutput(), message)110 111 def test_thread_info_in_minidump(self):112 """Test that lldb can read the thread information from the Minidump."""113 self.process_from_yaml("linux-x86_64.yaml")114 self.check_state()115 # This process crashed due to a segmentation fault in its116 # one and only thread.117 self.assertEqual(self.process.GetNumThreads(), 1)118 thread = self.process.GetThreadAtIndex(0)119 self.assertStopReason(thread.GetStopReason(), lldb.eStopReasonSignal)120 stop_description = thread.stop_description121 self.assertIn("SIGSEGV", stop_description)122 123 @skipIfLLVMTargetMissing("X86")124 def test_stack_info_in_minidump(self):125 """Test that we can see a trivial stack in a breakpad-generated Minidump."""126 # target create linux-x86_64 -c linux-x86_64.dmp127 self.dbg.CreateTarget("linux-x86_64")128 self.target = self.dbg.GetSelectedTarget()129 self.process = self.target.LoadCore("linux-x86_64.dmp")130 self.check_state()131 self.assertEqual(self.process.GetNumThreads(), 1)132 self.assertEqual(self.process.GetProcessID(), self._linux_x86_64_pid)133 thread = self.process.GetThreadAtIndex(0)134 # frame #0: linux-x86_64`crash()135 # frame #1: linux-x86_64`_start136 self.assertEqual(thread.GetNumFrames(), 2)137 frame = thread.GetFrameAtIndex(0)138 self.assertTrue(frame.IsValid())139 self.assertTrue(frame.GetModule().IsValid())140 pc = frame.GetPC()141 eip = frame.FindRegister("pc")142 self.assertTrue(eip.IsValid())143 self.assertEqual(pc, eip.GetValueAsUnsigned())144 145 def test_snapshot_minidump_dump_requested(self):146 """Test that if we load a snapshot minidump file (meaning the process147 did not crash) with exception code "DUMP_REQUESTED" there is no stop reason."""148 # target create -c linux-x86_64_not_crashed.dmp149 self.dbg.CreateTarget(None)150 self.target = self.dbg.GetSelectedTarget()151 self.process = self.target.LoadCore("linux-x86_64_not_crashed.dmp")152 self.check_state()153 self.assertEqual(self.process.GetNumThreads(), 1)154 thread = self.process.GetThreadAtIndex(0)155 self.assertStopReason(thread.GetStopReason(), lldb.eStopReasonNone)156 stop_description = thread.stop_description157 self.assertEqual(stop_description, "")158 159 def test_snapshot_minidump_null_exn_code(self):160 """Test that if we load a snapshot minidump file (meaning the process161 did not crash) with exception code zero there is no stop reason."""162 self.process_from_yaml("linux-x86_64_null_signal.yaml")163 self.check_state()164 self.assertEqual(self.process.GetNumThreads(), 1)165 thread = self.process.GetThreadAtIndex(0)166 self.assertStopReason(thread.GetStopReason(), lldb.eStopReasonNone)167 stop_description = thread.stop_description168 self.assertEqual(stop_description, "")169 170 def check_register_unsigned(self, set, name, expected):171 reg_value = set.GetChildMemberWithName(name)172 self.assertTrue(173 reg_value.IsValid(), 'Verify we have a register named "%s"' % (name)174 )175 self.assertEqual(176 reg_value.GetValueAsUnsigned(),177 expected,178 'Verify "%s" == %i' % (name, expected),179 )180 181 def check_register_string_value(self, set, name, expected, format):182 reg_value = set.GetChildMemberWithName(name)183 self.assertTrue(184 reg_value.IsValid(), 'Verify we have a register named "%s"' % (name)185 )186 if format is not None:187 reg_value.SetFormat(format)188 self.assertEqual(189 reg_value.GetValue(),190 expected,191 'Verify "%s" has string value "%s"' % (name, expected),192 )193 194 def test_arm64_registers(self):195 """Test ARM64 registers from a breakpad created minidump."""196 self.process_from_yaml("arm64-macos.yaml")197 self.check_state()198 self.assertEqual(self.process.GetNumThreads(), 1)199 thread = self.process.GetThreadAtIndex(0)200 self.assertStopReason(thread.GetStopReason(), lldb.eStopReasonNone)201 stop_description = thread.stop_description202 self.assertEqual(stop_description, "")203 registers = thread.GetFrameAtIndex(0).GetRegisters()204 # Verify the GPR registers are all correct205 # Verify x0 - x31 register values206 gpr = registers.GetValueAtIndex(0)207 for i in range(32):208 v = i + 1 | i + 2 << 32 | i + 3 << 48209 w = i + 1210 self.check_register_unsigned(gpr, "x%i" % (i), v)211 self.check_register_unsigned(gpr, "w%i" % (i), w)212 # Verify arg1 - arg8 register values213 for i in range(1, 9):214 v = i | i + 1 << 32 | i + 2 << 48215 self.check_register_unsigned(gpr, "arg%i" % (i), v)216 i = 29217 v = i + 1 | i + 2 << 32 | i + 3 << 48218 self.check_register_unsigned(gpr, "fp", v)219 i = 30220 v = i + 1 | i + 2 << 32 | i + 3 << 48221 self.check_register_unsigned(gpr, "lr", v)222 i = 31223 v = i + 1 | i + 2 << 32 | i + 3 << 48224 self.check_register_unsigned(gpr, "sp", v)225 self.check_register_unsigned(gpr, "pc", 0x1000)226 self.check_register_unsigned(gpr, "cpsr", 0x11223344)227 self.check_register_unsigned(gpr, "psr", 0x11223344)228 229 # Verify the FPR registers are all correct230 fpr = registers.GetValueAtIndex(1)231 for i in range(32):232 v = "0x"233 d = "0x"234 s = "0x"235 h = "0x"236 for j in range(i + 15, i - 1, -1):237 v += "%2.2x" % (j)238 for j in range(i + 7, i - 1, -1):239 d += "%2.2x" % (j)240 for j in range(i + 3, i - 1, -1):241 s += "%2.2x" % (j)242 for j in range(i + 1, i - 1, -1):243 h += "%2.2x" % (j)244 self.check_register_string_value(fpr, "v%i" % (i), v, lldb.eFormatHex)245 self.check_register_string_value(fpr, "d%i" % (i), d, lldb.eFormatHex)246 self.check_register_string_value(fpr, "s%i" % (i), s, lldb.eFormatHex)247 self.check_register_string_value(fpr, "h%i" % (i), h, lldb.eFormatHex)248 self.check_register_unsigned(gpr, "fpsr", 0x55667788)249 self.check_register_unsigned(gpr, "fpcr", 0x99AABBCC)250 251 def verify_arm_registers(self, apple=False):252 """253 Verify values of all ARM registers from a breakpad created254 minidump.255 """256 if apple:257 self.process_from_yaml("arm-macos.yaml")258 else:259 self.process_from_yaml("arm-linux.yaml")260 self.check_state()261 self.assertEqual(self.process.GetNumThreads(), 1)262 thread = self.process.GetThreadAtIndex(0)263 self.assertStopReason(thread.GetStopReason(), lldb.eStopReasonNone)264 stop_description = thread.stop_description265 self.assertEqual(stop_description, "")266 registers = thread.GetFrameAtIndex(0).GetRegisters()267 # Verify the GPR registers are all correct268 # Verify x0 - x31 register values269 gpr = registers.GetValueAtIndex(0)270 for i in range(1, 16):271 self.check_register_unsigned(gpr, "r%i" % (i), i + 1)272 # Verify arg1 - arg4 register values273 for i in range(1, 5):274 self.check_register_unsigned(gpr, "arg%i" % (i), i)275 if apple:276 self.check_register_unsigned(gpr, "fp", 0x08)277 else:278 self.check_register_unsigned(gpr, "fp", 0x0C)279 self.check_register_unsigned(gpr, "lr", 0x0F)280 self.check_register_unsigned(gpr, "sp", 0x0E)281 self.check_register_unsigned(gpr, "pc", 0x10)282 self.check_register_unsigned(gpr, "cpsr", 0x11223344)283 284 # Verify the FPR registers are all correct285 fpr = registers.GetValueAtIndex(1)286 # Check d0 - d31287 self.check_register_unsigned(gpr, "fpscr", 0x55667788AABBCCDD)288 for i in range(32):289 value = (i + 1) | (i + 1) << 8 | (i + 1) << 32 | (i + 1) << 48290 self.check_register_unsigned(fpr, "d%i" % (i), value)291 # Check s0 - s31292 for i in range(32):293 i_val = (i >> 1) + 1294 if i & 1:295 value = "%#8.8x" % (i_val | i_val << 16)296 else:297 value = "%#8.8x" % (i_val | i_val << 8)298 self.check_register_string_value(fpr, "s%i" % (i), value, lldb.eFormatHex)299 # Check q0 - q15300 for i in range(15):301 a = i * 2 + 1302 b = a + 1303 value = (304 "0x00%2.2x00%2.2x0000%2.2x%2.2x" "00%2.2x00%2.2x0000%2.2x%2.2x"305 ) % (b, b, b, b, a, a, a, a)306 self.check_register_string_value(fpr, "q%i" % (i), value, lldb.eFormatHex)307 308 def test_linux_arm_registers(self):309 """Test Linux ARM registers from a breakpad created minidump.310 311 The frame pointer is R11 for linux.312 """313 self.verify_arm_registers(apple=False)314 315 def test_apple_arm_registers(self):316 """Test Apple ARM registers from a breakpad created minidump.317 318 The frame pointer is R7 for linux.319 """320 self.verify_arm_registers(apple=True)321 322 def do_test_deeper_stack(self, binary, core, pid):323 target = self.dbg.CreateTarget(binary)324 process = target.LoadCore(core)325 thread = process.GetThreadAtIndex(0)326 327 self.assertEqual(process.GetProcessID(), pid)328 329 expected_stack = {1: "bar", 2: "foo", 3: "_start"}330 self.assertGreaterEqual(thread.GetNumFrames(), len(expected_stack))331 for index, name in expected_stack.items():332 frame = thread.GetFrameAtIndex(index)333 self.assertTrue(frame.IsValid())334 function_name = frame.GetFunctionName()335 self.assertIn(name, function_name)336 337 @skipIfLLVMTargetMissing("X86")338 def test_deeper_stack_in_minidump(self):339 """Test that we can examine a more interesting stack in a Minidump."""340 # Launch with the Minidump, and inspect the stack.341 # target create linux-x86_64_not_crashed -c linux-x86_64_not_crashed.dmp342 self.do_test_deeper_stack(343 "linux-x86_64_not_crashed",344 "linux-x86_64_not_crashed.dmp",345 self._linux_x86_64_not_crashed_pid,346 )347 348 def do_change_pid_in_minidump(self, core, newcore, offset, oldpid, newpid):349 """This assumes that the minidump is breakpad generated on Linux -350 meaning that the PID in the file will be an ascii string part of351 /proc/PID/status which is written in the file352 """353 shutil.copyfile(core, newcore)354 with open(newcore, "rb+") as f:355 f.seek(offset)356 currentpid = f.read(5).decode("utf-8")357 self.assertEqual(currentpid, oldpid)358 359 f.seek(offset)360 if len(newpid) < len(oldpid):361 newpid += " " * (len(oldpid) - len(newpid))362 newpid += "\n"363 f.write(newpid.encode("utf-8"))364 365 @skipIfLLVMTargetMissing("X86")366 def test_deeper_stack_in_minidump_with_same_pid_running(self):367 """Test that we read the information from the core correctly even if we368 have a running process with the same PID"""369 new_core = self.getBuildArtifact("linux-x86_64_not_crashed-pid.dmp")370 self.do_change_pid_in_minidump(371 "linux-x86_64_not_crashed.dmp",372 new_core,373 self._linux_x86_64_not_crashed_pid_offset,374 str(self._linux_x86_64_not_crashed_pid),375 str(os.getpid()),376 )377 self.do_test_deeper_stack("linux-x86_64_not_crashed", new_core, os.getpid())378 379 @skipIfLLVMTargetMissing("X86")380 def test_two_cores_same_pid(self):381 """Test that we handle the situation if we have two core files with the same PID"""382 new_core = self.getBuildArtifact("linux-x86_64_not_crashed-pid.dmp")383 self.do_change_pid_in_minidump(384 "linux-x86_64_not_crashed.dmp",385 new_core,386 self._linux_x86_64_not_crashed_pid_offset,387 str(self._linux_x86_64_not_crashed_pid),388 str(self._linux_x86_64_pid),389 )390 self.do_test_deeper_stack(391 "linux-x86_64_not_crashed", new_core, self._linux_x86_64_pid392 )393 self.test_stack_info_in_minidump()394 395 @skipIfLLVMTargetMissing("X86")396 def test_local_variables_in_minidump(self):397 """Test that we can examine local variables in a Minidump."""398 # Launch with the Minidump, and inspect a local variable.399 # target create linux-x86_64_not_crashed -c linux-x86_64_not_crashed.dmp400 self.target = self.dbg.CreateTarget("linux-x86_64_not_crashed")401 self.process = self.target.LoadCore("linux-x86_64_not_crashed.dmp")402 self.check_state()403 thread = self.process.GetThreadAtIndex(0)404 frame = thread.GetFrameAtIndex(1)405 value = frame.EvaluateExpression("x")406 self.assertEqual(value.GetValueAsSigned(), 3)407 408 def test_memory_regions_in_minidump(self):409 """Test memory regions from a Minidump"""410 self.process_from_yaml("regions-linux-map.yaml")411 self.check_state()412 413 regions_count = 19414 region_info_list = self.process.GetMemoryRegions()415 self.assertEqual(region_info_list.GetSize(), regions_count)416 417 def check_region(index, start, end, read, write, execute, mapped, name):418 region_info = lldb.SBMemoryRegionInfo()419 self.assertTrue(420 self.process.GetMemoryRegionInfo(start, region_info).Success()421 )422 self.assertEqual(start, region_info.GetRegionBase())423 self.assertEqual(end, region_info.GetRegionEnd())424 self.assertEqual(read, region_info.IsReadable())425 self.assertEqual(write, region_info.IsWritable())426 self.assertEqual(execute, region_info.IsExecutable())427 self.assertEqual(mapped, region_info.IsMapped())428 self.assertEqual(name, region_info.GetName())429 430 # Ensure we have the same regions as SBMemoryRegionInfoList contains.431 if index >= 0 and index < regions_count:432 region_info_from_list = lldb.SBMemoryRegionInfo()433 self.assertTrue(434 region_info_list.GetMemoryRegionAtIndex(435 index, region_info_from_list436 )437 )438 self.assertEqual(region_info_from_list, region_info)439 440 a = "/system/bin/app_process"441 b = "/system/bin/linker"442 c = "/system/lib/liblog.so"443 d = "/system/lib/libc.so"444 n = None445 max_int = 0xFFFFFFFFFFFFFFFF446 447 # Test address before the first entry comes back with nothing mapped up448 # to first valid region info449 check_region(-1, 0x00000000, 0x400D9000, False, False, False, False, n)450 check_region(0, 0x400D9000, 0x400DB000, True, False, True, True, a)451 check_region(1, 0x400DB000, 0x400DC000, True, False, False, True, a)452 check_region(2, 0x400DC000, 0x400DD000, True, True, False, True, n)453 check_region(3, 0x400DD000, 0x400EC000, True, False, True, True, b)454 check_region(4, 0x400EC000, 0x400ED000, True, False, False, True, n)455 check_region(5, 0x400ED000, 0x400EE000, True, False, False, True, b)456 check_region(6, 0x400EE000, 0x400EF000, True, True, False, True, b)457 check_region(7, 0x400EF000, 0x400FB000, True, True, False, True, n)458 check_region(8, 0x400FB000, 0x400FC000, True, False, True, True, c)459 check_region(9, 0x400FC000, 0x400FD000, True, True, True, True, c)460 check_region(10, 0x400FD000, 0x400FF000, True, False, True, True, c)461 check_region(11, 0x400FF000, 0x40100000, True, False, False, True, c)462 check_region(12, 0x40100000, 0x40101000, True, True, False, True, c)463 check_region(13, 0x40101000, 0x40122000, True, False, True, True, d)464 check_region(14, 0x40122000, 0x40123000, True, True, True, True, d)465 check_region(15, 0x40123000, 0x40167000, True, False, True, True, d)466 check_region(16, 0x40167000, 0x40169000, True, False, False, True, d)467 check_region(17, 0x40169000, 0x4016B000, True, True, False, True, d)468 check_region(18, 0x4016B000, 0x40176000, True, True, False, True, n)469 check_region(-1, 0x40176000, max_int, False, False, False, False, n)470 471 @skipIfLLVMTargetMissing("X86")472 def test_minidump_sysroot(self):473 """Test that lldb can find a module referenced in an i386 linux minidump using the sysroot."""474 475 # Copy linux-x86_64 executable to tmp_sysroot/temp/test/ (since it was compiled as476 # /tmp/test/linux-x86_64)477 tmp_sysroot = os.path.join(self.getBuildDir(), "lldb_i386_mock_sysroot")478 executable = os.path.join(tmp_sysroot, "tmp", "test", "linux-x86_64")479 exe_dir = os.path.dirname(executable)480 lldbutil.mkdir_p(exe_dir)481 shutil.copyfile("linux-x86_64", executable)482 483 # Set sysroot and load core484 self.runCmd("platform select remote-linux --sysroot '%s'" % tmp_sysroot)485 self.process_from_yaml("linux-x86_64.yaml")486 self.check_state()487 488 # Check that we loaded the module from the sysroot489 self.assertEqual(self.target.GetNumModules(), 1)490 module = self.target.GetModuleAtIndex(0)491 spec_dir_norm = os.path.normcase(module.GetFileSpec().GetDirectory())492 exe_dir_norm = os.path.normcase(exe_dir)493 self.assertEqual(spec_dir_norm, exe_dir_norm)494 495 def test_minidump_memory64list(self):496 """Test that lldb can read from the memory64list in a minidump."""497 self.process_from_yaml("linux-x86_64_mem64.yaml")498 499 region_count = 3500 region_info_list = self.process.GetMemoryRegions()501 self.assertEqual(region_info_list.GetSize(), region_count)502 503 region = lldb.SBMemoryRegionInfo()504 self.assertTrue(region_info_list.GetMemoryRegionAtIndex(0, region))505 self.assertEqual(region.GetRegionBase(), 0x7FFF12A84030)506 self.assertTrue(region.GetRegionEnd(), 0x7FFF12A84030 + 0x2FD0)507 self.assertTrue(region_info_list.GetMemoryRegionAtIndex(1, region))508 self.assertEqual(region.GetRegionBase(), 0x00007FFF12A87000)509 self.assertTrue(region.GetRegionEnd(), 0x00007FFF12A87000 + 0x00000018)510 self.assertTrue(region_info_list.GetMemoryRegionAtIndex(2, region))511 self.assertEqual(region.GetRegionBase(), 0x00007FFF12A87018)512 self.assertTrue(region.GetRegionEnd(), 0x00007FFF12A87018 + 0x00000400)513 514 def test_multiple_exceptions_or_signals(self):515 """Test that lldb can read the exception information from the Minidump."""516 print("Starting to read multiple-sigsev.yaml")517 self.process_from_yaml("multiple-sigsev.yaml")518 print("Done reading multiple-sigsev.yaml")519 self.check_state()520 # This process crashed due to a segmentation fault in both it's threads.521 self.assertEqual(self.process.GetNumThreads(), 2)522 for i in range(2):523 thread = self.process.GetThreadAtIndex(i)524 self.assertStopReason(thread.GetStopReason(), lldb.eStopReasonSignal)525 stop_description = thread.stop_description526 self.assertIn("SIGSEGV", stop_description)527 528 def test_breakpoint_on_minidump(self):529 """530 Test that LLDB breakpoints are recorded in Minidumps531 """532 yaml = "linux-x86_64-exceptiondescription.yaml"533 core = self.getBuildArtifact("breakpoint.core.dmp")534 self.yaml2obj(yaml, core)535 try:536 # Create a target with the object file we just created from YAML537 target = self.dbg.CreateTarget(None)538 self.assertTrue(target, VALID_TARGET)539 process = target.LoadCore(core)540 self.assertTrue(process, VALID_PROCESS)541 thread = process.GetThreadAtIndex(0)542 stop_reason = thread.stop_description543 self.assertIn("breakpoint 1.1", stop_reason)544 finally:545 if os.path.isfile(core):546 os.unlink(core)547