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