1""" 2Test basics of Minidump debugging. 3""" 4 5from six import iteritems 6 7import shutil 8 9import lldb 10from lldbsuite.test.decorators import * 11from lldbsuite.test.lldbtest import * 12from lldbsuite.test import lldbutil 13 14 15class MiniDumpNewTestCase(TestBase): 16 17 mydir = TestBase.compute_mydir(__file__) 18 19 NO_DEBUG_INFO_TESTCASE = True 20 21 _linux_x86_64_pid = 29917 22 _linux_x86_64_not_crashed_pid = 29939 23 _linux_x86_64_not_crashed_pid_offset = 0xD967 24 25 def process_from_yaml(self, yaml_file): 26 minidump_path = self.getBuildArtifact(os.path.basename(yaml_file) + ".dmp") 27 self.yaml2obj(yaml_file, minidump_path) 28 self.target = self.dbg.CreateTarget(None) 29 self.process = self.target.LoadCore(minidump_path) 30 return self.process 31 32 @skipIfReproducer # lldb::FileSP used in typemap cannot be instrumented. 33 def check_state(self): 34 with open(os.devnull) as devnul: 35 # sanitize test output 36 self.dbg.SetOutputFileHandle(devnul, False) 37 self.dbg.SetErrorFileHandle(devnul, False) 38 39 self.assertTrue(self.process.is_stopped) 40 41 # Process.Continue 42 error = self.process.Continue() 43 self.assertFalse(error.Success()) 44 self.assertTrue(self.process.is_stopped) 45 46 # Thread.StepOut 47 thread = self.process.GetSelectedThread() 48 thread.StepOut() 49 self.assertTrue(self.process.is_stopped) 50 51 # command line 52 self.dbg.HandleCommand('s') 53 self.assertTrue(self.process.is_stopped) 54 self.dbg.HandleCommand('c') 55 self.assertTrue(self.process.is_stopped) 56 57 # restore file handles 58 self.dbg.SetOutputFileHandle(None, False) 59 self.dbg.SetErrorFileHandle(None, False) 60 61 def test_loadcore_error_status(self): 62 """Test the SBTarget.LoadCore(core, error) overload.""" 63 minidump_path = self.getBuildArtifact("linux-x86_64.dmp") 64 self.yaml2obj("linux-x86_64.yaml", minidump_path) 65 self.target = self.dbg.CreateTarget(None) 66 error = lldb.SBError() 67 self.process = self.target.LoadCore(minidump_path, error) 68 self.assertTrue(self.process, PROCESS_IS_VALID) 69 self.assertTrue(error.Success()) 70 71 def test_loadcore_error_status_failure(self): 72 """Test the SBTarget.LoadCore(core, error) overload.""" 73 self.target = self.dbg.CreateTarget(None) 74 error = lldb.SBError() 75 self.process = self.target.LoadCore("non-existent.dmp", error) 76 self.assertFalse(self.process, PROCESS_IS_VALID) 77 self.assertTrue(error.Fail()) 78 79 def test_process_info_in_minidump(self): 80 """Test that lldb can read the process information from the Minidump.""" 81 self.process_from_yaml("linux-x86_64.yaml") 82 self.assertTrue(self.process, PROCESS_IS_VALID) 83 self.assertEqual(self.process.GetNumThreads(), 1) 84 self.assertEqual(self.process.GetProcessID(), self._linux_x86_64_pid) 85 self.check_state() 86 87 def test_memory_region_name(self): 88 self.process_from_yaml("regions-linux-map.yaml") 89 result = lldb.SBCommandReturnObject() 90 addr_region_name_pairs = [ 91 ("0x400d9000", "/system/bin/app_process"), 92 ("0x400db000", "/system/bin/app_process"), 93 ("0x400dd000", "/system/bin/linker"), 94 ("0x400ed000", "/system/bin/linker"), 95 ("0x400ee000", "/system/bin/linker"), 96 ("0x400fb000", "/system/lib/liblog.so"), 97 ("0x400fc000", "/system/lib/liblog.so"), 98 ("0x400fd000", "/system/lib/liblog.so"), 99 ("0x400ff000", "/system/lib/liblog.so"), 100 ("0x40100000", "/system/lib/liblog.so"), 101 ("0x40101000", "/system/lib/libc.so"), 102 ("0x40122000", "/system/lib/libc.so"), 103 ("0x40123000", "/system/lib/libc.so"), 104 ("0x40167000", "/system/lib/libc.so"), 105 ("0x40169000", "/system/lib/libc.so"), 106 ] 107 ci = self.dbg.GetCommandInterpreter() 108 for (addr, region_name) in addr_region_name_pairs: 109 command = 'memory region ' + addr 110 ci.HandleCommand(command, result, False) 111 message = 'Ensure memory "%s" shows up in output for "%s"' % ( 112 region_name, command) 113 self.assertIn(region_name, result.GetOutput(), message) 114 115 def test_thread_info_in_minidump(self): 116 """Test that lldb can read the thread information from the Minidump.""" 117 self.process_from_yaml("linux-x86_64.yaml") 118 self.check_state() 119 # This process crashed due to a segmentation fault in its 120 # one and only thread. 121 self.assertEqual(self.process.GetNumThreads(), 1) 122 thread = self.process.GetThreadAtIndex(0) 123 self.assertEqual(thread.GetStopReason(), lldb.eStopReasonSignal) 124 stop_description = thread.GetStopDescription(256) 125 self.assertIn("SIGSEGV", stop_description) 126 127 @skipIfLLVMTargetMissing("X86") 128 def test_stack_info_in_minidump(self): 129 """Test that we can see a trivial stack in a breakpad-generated Minidump.""" 130 # target create linux-x86_64 -c linux-x86_64.dmp 131 self.dbg.CreateTarget("linux-x86_64") 132 self.target = self.dbg.GetSelectedTarget() 133 self.process = self.target.LoadCore("linux-x86_64.dmp") 134 self.check_state() 135 self.assertEqual(self.process.GetNumThreads(), 1) 136 self.assertEqual(self.process.GetProcessID(), self._linux_x86_64_pid) 137 thread = self.process.GetThreadAtIndex(0) 138 # frame #0: linux-x86_64`crash() 139 # frame #1: linux-x86_64`_start 140 self.assertEqual(thread.GetNumFrames(), 2) 141 frame = thread.GetFrameAtIndex(0) 142 self.assertTrue(frame.IsValid()) 143 self.assertTrue(frame.GetModule().IsValid()) 144 pc = frame.GetPC() 145 eip = frame.FindRegister("pc") 146 self.assertTrue(eip.IsValid()) 147 self.assertEqual(pc, eip.GetValueAsUnsigned()) 148 149 def test_snapshot_minidump_dump_requested(self): 150 """Test that if we load a snapshot minidump file (meaning the process 151 did not crash) with exception code "DUMP_REQUESTED" there is no stop reason.""" 152 # target create -c linux-x86_64_not_crashed.dmp 153 self.dbg.CreateTarget(None) 154 self.target = self.dbg.GetSelectedTarget() 155 self.process = self.target.LoadCore("linux-x86_64_not_crashed.dmp") 156 self.check_state() 157 self.assertEqual(self.process.GetNumThreads(), 1) 158 thread = self.process.GetThreadAtIndex(0) 159 self.assertEqual(thread.GetStopReason(), lldb.eStopReasonNone) 160 stop_description = thread.GetStopDescription(256) 161 self.assertEqual(stop_description, "") 162 163 def test_snapshot_minidump_null_exn_code(self): 164 """Test that if we load a snapshot minidump file (meaning the process 165 did not crash) with exception code zero there is no stop reason.""" 166 self.process_from_yaml("linux-x86_64_null_signal.yaml") 167 self.check_state() 168 self.assertEqual(self.process.GetNumThreads(), 1) 169 thread = self.process.GetThreadAtIndex(0) 170 self.assertEqual(thread.GetStopReason(), lldb.eStopReasonNone) 171 stop_description = thread.GetStopDescription(256) 172 self.assertEqual(stop_description, "") 173 174 def check_register_unsigned(self, set, name, expected): 175 reg_value = set.GetChildMemberWithName(name) 176 self.assertTrue(reg_value.IsValid(), 177 'Verify we have a register named "%s"' % (name)) 178 self.assertEqual(reg_value.GetValueAsUnsigned(), expected, 179 'Verify "%s" == %i' % (name, expected)) 180 181 def check_register_string_value(self, set, name, expected, format): 182 reg_value = set.GetChildMemberWithName(name) 183 self.assertTrue(reg_value.IsValid(), 184 'Verify we have a register named "%s"' % (name)) 185 if format is not None: 186 reg_value.SetFormat(format) 187 self.assertEqual(reg_value.GetValue(), expected, 188 'Verify "%s" has string value "%s"' % (name, 189 expected)) 190 191 def test_arm64_registers(self): 192 """Test ARM64 registers from a breakpad created minidump.""" 193 self.process_from_yaml("arm64-macos.yaml") 194 self.check_state() 195 self.assertEqual(self.process.GetNumThreads(), 1) 196 thread = self.process.GetThreadAtIndex(0) 197 self.assertEqual(thread.GetStopReason(), lldb.eStopReasonNone) 198 stop_description = thread.GetStopDescription(256) 199 self.assertEqual(stop_description, "") 200 registers = thread.GetFrameAtIndex(0).GetRegisters() 201 # Verify the GPR registers are all correct 202 # Verify x0 - x31 register values 203 gpr = registers.GetValueAtIndex(0) 204 for i in range(32): 205 v = i+1 | i+2 << 32 | i+3 << 48 206 w = i+1 207 self.check_register_unsigned(gpr, 'x%i' % (i), v) 208 self.check_register_unsigned(gpr, 'w%i' % (i), w) 209 # Verify arg1 - arg8 register values 210 for i in range(1, 9): 211 v = i | i+1 << 32 | i+2 << 48 212 self.check_register_unsigned(gpr, 'arg%i' % (i), v) 213 i = 29 214 v = i+1 | i+2 << 32 | i+3 << 48 215 self.check_register_unsigned(gpr, 'fp', v) 216 i = 30 217 v = i+1 | i+2 << 32 | i+3 << 48 218 self.check_register_unsigned(gpr, 'lr', v) 219 i = 31 220 v = i+1 | i+2 << 32 | i+3 << 48 221 self.check_register_unsigned(gpr, 'sp', v) 222 self.check_register_unsigned(gpr, 'pc', 0x1000) 223 self.check_register_unsigned(gpr, 'cpsr', 0x11223344) 224 self.check_register_unsigned(gpr, 'psr', 0x11223344) 225 226 # Verify the FPR registers are all correct 227 fpr = registers.GetValueAtIndex(1) 228 for i in range(32): 229 v = "0x" 230 d = "0x" 231 s = "0x" 232 h = "0x" 233 for j in range(i+15, i-1, -1): 234 v += "%2.2x" % (j) 235 for j in range(i+7, i-1, -1): 236 d += "%2.2x" % (j) 237 for j in range(i+3, i-1, -1): 238 s += "%2.2x" % (j) 239 for j in range(i+1, i-1, -1): 240 h += "%2.2x" % (j) 241 self.check_register_string_value(fpr, "v%i" % (i), v, 242 lldb.eFormatHex) 243 self.check_register_string_value(fpr, "d%i" % (i), d, 244 lldb.eFormatHex) 245 self.check_register_string_value(fpr, "s%i" % (i), s, 246 lldb.eFormatHex) 247 self.check_register_string_value(fpr, "h%i" % (i), h, 248 lldb.eFormatHex) 249 self.check_register_unsigned(gpr, 'fpsr', 0x55667788) 250 self.check_register_unsigned(gpr, 'fpcr', 0x99aabbcc) 251 252 def verify_arm_registers(self, apple=False): 253 """ 254 Verify values of all ARM registers from a breakpad created 255 minidump. 256 """ 257 if apple: 258 self.process_from_yaml("arm-macos.yaml") 259 else: 260 self.process_from_yaml("arm-linux.yaml") 261 self.check_state() 262 self.assertEqual(self.process.GetNumThreads(), 1) 263 thread = self.process.GetThreadAtIndex(0) 264 self.assertEqual(thread.GetStopReason(), lldb.eStopReasonNone) 265 stop_description = thread.GetStopDescription(256) 266 self.assertEqual(stop_description, "") 267 registers = thread.GetFrameAtIndex(0).GetRegisters() 268 # Verify the GPR registers are all correct 269 # Verify x0 - x31 register values 270 gpr = registers.GetValueAtIndex(0) 271 for i in range(1, 16): 272 self.check_register_unsigned(gpr, 'r%i' % (i), i+1) 273 # Verify arg1 - arg4 register values 274 for i in range(1, 5): 275 self.check_register_unsigned(gpr, 'arg%i' % (i), i) 276 if apple: 277 self.check_register_unsigned(gpr, 'fp', 0x08) 278 else: 279 self.check_register_unsigned(gpr, 'fp', 0x0c) 280 self.check_register_unsigned(gpr, 'lr', 0x0f) 281 self.check_register_unsigned(gpr, 'sp', 0x0e) 282 self.check_register_unsigned(gpr, 'pc', 0x10) 283 self.check_register_unsigned(gpr, 'cpsr', 0x11223344) 284 285 # Verify the FPR registers are all correct 286 fpr = registers.GetValueAtIndex(1) 287 # Check d0 - d31 288 self.check_register_unsigned(gpr, 'fpscr', 0x55667788aabbccdd) 289 for i in range(32): 290 value = (i+1) | (i+1) << 8 | (i+1) << 32 | (i+1) << 48 291 self.check_register_unsigned(fpr, "d%i" % (i), value) 292 # Check s0 - s31 293 for i in range(32): 294 i_val = (i >> 1) + 1 295 if i & 1: 296 value = "%#8.8x" % (i_val | i_val << 16) 297 else: 298 value = "%#8.8x" % (i_val | i_val << 8) 299 self.check_register_string_value(fpr, "s%i" % (i), value, 300 lldb.eFormatHex) 301 # Check q0 - q15 302 for i in range(15): 303 a = i * 2 + 1 304 b = a + 1 305 value = ("0x00%2.2x00%2.2x0000%2.2x%2.2x" 306 "00%2.2x00%2.2x0000%2.2x%2.2x") % (b, b, b, b, a, a, a, a) 307 self.check_register_string_value(fpr, "q%i" % (i), value, 308 lldb.eFormatHex) 309 310 def test_linux_arm_registers(self): 311 """Test Linux ARM registers from a breakpad created minidump. 312 313 The frame pointer is R11 for linux. 314 """ 315 self.verify_arm_registers(apple=False) 316 317 def test_apple_arm_registers(self): 318 """Test Apple ARM registers from a breakpad created minidump. 319 320 The frame pointer is R7 for linux. 321 """ 322 self.verify_arm_registers(apple=True) 323 324 def do_test_deeper_stack(self, binary, core, pid): 325 target = self.dbg.CreateTarget(binary) 326 process = target.LoadCore(core) 327 thread = process.GetThreadAtIndex(0) 328 329 self.assertEqual(process.GetProcessID(), pid) 330 331 expected_stack = {1: 'bar', 2: 'foo', 3: '_start'} 332 self.assertGreaterEqual(thread.GetNumFrames(), len(expected_stack)) 333 for index, name in iteritems(expected_stack): 334 frame = thread.GetFrameAtIndex(index) 335 self.assertTrue(frame.IsValid()) 336 function_name = frame.GetFunctionName() 337 self.assertIn(name, function_name) 338 339 @skipIfLLVMTargetMissing("X86") 340 def test_deeper_stack_in_minidump(self): 341 """Test that we can examine a more interesting stack in a Minidump.""" 342 # Launch with the Minidump, and inspect the stack. 343 # target create linux-x86_64_not_crashed -c linux-x86_64_not_crashed.dmp 344 self.do_test_deeper_stack("linux-x86_64_not_crashed", 345 "linux-x86_64_not_crashed.dmp", 346 self._linux_x86_64_not_crashed_pid) 347 348 @skipIfReproducer # VFS is a snapshot. 349 def do_change_pid_in_minidump(self, core, newcore, offset, oldpid, newpid): 350 """ This assumes that the minidump is breakpad generated on Linux - 351 meaning that the PID in the file will be an ascii string part of 352 /proc/PID/status which is written in the file 353 """ 354 shutil.copyfile(core, newcore) 355 with open(newcore, "rb+") as f: 356 f.seek(offset) 357 currentpid = f.read(5).decode('utf-8') 358 self.assertEqual(currentpid, oldpid) 359 360 f.seek(offset) 361 if len(newpid) < len(oldpid): 362 newpid += " " * (len(oldpid) - len(newpid)) 363 newpid += "\n" 364 f.write(newpid.encode('utf-8')) 365 366 @skipIfLLVMTargetMissing("X86") 367 def test_deeper_stack_in_minidump_with_same_pid_running(self): 368 """Test that we read the information from the core correctly even if we 369 have a running process with the same PID""" 370 new_core = self.getBuildArtifact("linux-x86_64_not_crashed-pid.dmp") 371 self.do_change_pid_in_minidump("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 self.do_test_deeper_stack("linux-x86_64_not_crashed", new_core, os.getpid()) 377 378 @skipIfLLVMTargetMissing("X86") 379 def test_two_cores_same_pid(self): 380 """Test that we handle the situation if we have two core files with the same PID """ 381 new_core = self.getBuildArtifact("linux-x86_64_not_crashed-pid.dmp") 382 self.do_change_pid_in_minidump("linux-x86_64_not_crashed.dmp", 383 new_core, 384 self._linux_x86_64_not_crashed_pid_offset, 385 str(self._linux_x86_64_not_crashed_pid), 386 str(self._linux_x86_64_pid)) 387 self.do_test_deeper_stack("linux-x86_64_not_crashed", 388 new_core, self._linux_x86_64_pid) 389 self.test_stack_info_in_minidump() 390 391 @skipIfLLVMTargetMissing("X86") 392 def test_local_variables_in_minidump(self): 393 """Test that we can examine local variables in a Minidump.""" 394 # Launch with the Minidump, and inspect a local variable. 395 # target create linux-x86_64_not_crashed -c linux-x86_64_not_crashed.dmp 396 self.target = self.dbg.CreateTarget("linux-x86_64_not_crashed") 397 self.process = self.target.LoadCore("linux-x86_64_not_crashed.dmp") 398 self.check_state() 399 thread = self.process.GetThreadAtIndex(0) 400 frame = thread.GetFrameAtIndex(1) 401 value = frame.EvaluateExpression('x') 402 self.assertEqual(value.GetValueAsSigned(), 3) 403 404 def test_memory_regions_in_minidump(self): 405 """Test memory regions from a Minidump""" 406 self.process_from_yaml("regions-linux-map.yaml") 407 self.check_state() 408 409 regions_count = 19 410 region_info_list = self.process.GetMemoryRegions() 411 self.assertEqual(region_info_list.GetSize(), regions_count) 412 413 def check_region(index, start, end, read, write, execute, mapped, name): 414 region_info = lldb.SBMemoryRegionInfo() 415 self.assertTrue( 416 self.process.GetMemoryRegionInfo(start, region_info).Success()) 417 self.assertEqual(start, region_info.GetRegionBase()) 418 self.assertEqual(end, region_info.GetRegionEnd()) 419 self.assertEqual(read, region_info.IsReadable()) 420 self.assertEqual(write, region_info.IsWritable()) 421 self.assertEqual(execute, region_info.IsExecutable()) 422 self.assertEqual(mapped, region_info.IsMapped()) 423 self.assertEqual(name, region_info.GetName()) 424 425 # Ensure we have the same regions as SBMemoryRegionInfoList contains. 426 if index >= 0 and index < regions_count: 427 region_info_from_list = lldb.SBMemoryRegionInfo() 428 self.assertTrue(region_info_list.GetMemoryRegionAtIndex( 429 index, region_info_from_list)) 430 self.assertEqual(region_info_from_list, region_info) 431 432 a = "/system/bin/app_process" 433 b = "/system/bin/linker" 434 c = "/system/lib/liblog.so" 435 d = "/system/lib/libc.so" 436 n = None 437 max_int = 0xffffffffffffffff 438 439 # Test address before the first entry comes back with nothing mapped up 440 # to first valid region info 441 check_region(-1, 0x00000000, 0x400d9000, False, False, False, False, n) 442 check_region( 0, 0x400d9000, 0x400db000, True, False, True, True, a) 443 check_region( 1, 0x400db000, 0x400dc000, True, False, False, True, a) 444 check_region( 2, 0x400dc000, 0x400dd000, True, True, False, True, n) 445 check_region( 3, 0x400dd000, 0x400ec000, True, False, True, True, b) 446 check_region( 4, 0x400ec000, 0x400ed000, True, False, False, True, n) 447 check_region( 5, 0x400ed000, 0x400ee000, True, False, False, True, b) 448 check_region( 6, 0x400ee000, 0x400ef000, True, True, False, True, b) 449 check_region( 7, 0x400ef000, 0x400fb000, True, True, False, True, n) 450 check_region( 8, 0x400fb000, 0x400fc000, True, False, True, True, c) 451 check_region( 9, 0x400fc000, 0x400fd000, True, True, True, True, c) 452 check_region(10, 0x400fd000, 0x400ff000, True, False, True, True, c) 453 check_region(11, 0x400ff000, 0x40100000, True, False, False, True, c) 454 check_region(12, 0x40100000, 0x40101000, True, True, False, True, c) 455 check_region(13, 0x40101000, 0x40122000, True, False, True, True, d) 456 check_region(14, 0x40122000, 0x40123000, True, True, True, True, d) 457 check_region(15, 0x40123000, 0x40167000, True, False, True, True, d) 458 check_region(16, 0x40167000, 0x40169000, True, False, False, True, d) 459 check_region(17, 0x40169000, 0x4016b000, True, True, False, True, d) 460 check_region(18, 0x4016b000, 0x40176000, True, True, False, True, n) 461 check_region(-1, 0x40176000, max_int, False, False, False, False, n) 462 463 @skipIfLLVMTargetMissing("X86") 464 def test_minidump_sysroot(self): 465 """Test that lldb can find a module referenced in an i386 linux minidump using the sysroot.""" 466 467 # Copy linux-x86_64 executable to tmp_sysroot/temp/test/ (since it was compiled as 468 # /tmp/test/linux-x86_64) 469 tmp_sysroot = os.path.join( 470 self.getBuildDir(), "lldb_i386_mock_sysroot") 471 executable = os.path.join( 472 tmp_sysroot, "tmp", "test", "linux-x86_64") 473 exe_dir = os.path.dirname(executable) 474 lldbutil.mkdir_p(exe_dir) 475 shutil.copyfile("linux-x86_64", executable) 476 477 # Set sysroot and load core 478 self.runCmd("platform select remote-linux --sysroot '%s'" % 479 tmp_sysroot) 480 self.process_from_yaml("linux-x86_64.yaml") 481 self.check_state() 482 483 # Check that we loaded the module from the sysroot 484 self.assertEqual(self.target.GetNumModules(), 1) 485 module = self.target.GetModuleAtIndex(0) 486 spec_dir_norm = os.path.normcase(module.GetFileSpec().GetDirectory()) 487 exe_dir_norm = os.path.normcase(exe_dir) 488 self.assertEqual(spec_dir_norm, exe_dir_norm) 489