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