1 //===-- StopInfoMachException.cpp -------------------------------*- C++ -*-===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 10 #include "StopInfoMachException.h" 11 12 // C Includes 13 // C++ Includes 14 // Other libraries and framework includes 15 // Project includes 16 #include "lldb/Breakpoint/Watchpoint.h" 17 #include "lldb/Symbol/Symbol.h" 18 #include "lldb/Target/DynamicLoader.h" 19 #include "lldb/Target/ExecutionContext.h" 20 #include "lldb/Target/Process.h" 21 #include "lldb/Target/RegisterContext.h" 22 #include "lldb/Target/Target.h" 23 #include "lldb/Target/Thread.h" 24 #include "lldb/Target/ThreadPlan.h" 25 #include "lldb/Target/UnixSignals.h" 26 #include "lldb/Utility/StreamString.h" 27 28 using namespace lldb; 29 using namespace lldb_private; 30 31 const char *StopInfoMachException::GetDescription() { 32 if (m_description.empty() && m_value != 0) { 33 ExecutionContext exe_ctx(m_thread_wp.lock()); 34 Target *target = exe_ctx.GetTargetPtr(); 35 const llvm::Triple::ArchType cpu = 36 target ? target->GetArchitecture().GetMachine() 37 : llvm::Triple::UnknownArch; 38 39 const char *exc_desc = NULL; 40 const char *code_label = "code"; 41 const char *code_desc = NULL; 42 const char *subcode_label = "subcode"; 43 const char *subcode_desc = NULL; 44 switch (m_value) { 45 case 1: // EXC_BAD_ACCESS 46 exc_desc = "EXC_BAD_ACCESS"; 47 subcode_label = "address"; 48 switch (cpu) { 49 case llvm::Triple::x86: 50 case llvm::Triple::x86_64: 51 switch (m_exc_code) { 52 case 0xd: 53 code_desc = "EXC_I386_GPFLT"; 54 m_exc_data_count = 1; 55 break; 56 } 57 break; 58 case llvm::Triple::arm: 59 case llvm::Triple::thumb: 60 switch (m_exc_code) { 61 case 0x101: 62 code_desc = "EXC_ARM_DA_ALIGN"; 63 break; 64 case 0x102: 65 code_desc = "EXC_ARM_DA_DEBUG"; 66 break; 67 } 68 break; 69 70 case llvm::Triple::ppc: 71 case llvm::Triple::ppc64: 72 switch (m_exc_code) { 73 case 0x101: 74 code_desc = "EXC_PPC_VM_PROT_READ"; 75 break; 76 case 0x102: 77 code_desc = "EXC_PPC_BADSPACE"; 78 break; 79 case 0x103: 80 code_desc = "EXC_PPC_UNALIGNED"; 81 break; 82 } 83 break; 84 85 default: 86 break; 87 } 88 break; 89 90 case 2: // EXC_BAD_INSTRUCTION 91 exc_desc = "EXC_BAD_INSTRUCTION"; 92 switch (cpu) { 93 case llvm::Triple::x86: 94 case llvm::Triple::x86_64: 95 if (m_exc_code == 1) 96 code_desc = "EXC_I386_INVOP"; 97 break; 98 99 case llvm::Triple::ppc: 100 case llvm::Triple::ppc64: 101 switch (m_exc_code) { 102 case 1: 103 code_desc = "EXC_PPC_INVALID_SYSCALL"; 104 break; 105 case 2: 106 code_desc = "EXC_PPC_UNIPL_INST"; 107 break; 108 case 3: 109 code_desc = "EXC_PPC_PRIVINST"; 110 break; 111 case 4: 112 code_desc = "EXC_PPC_PRIVREG"; 113 break; 114 case 5: 115 code_desc = "EXC_PPC_TRACE"; 116 break; 117 case 6: 118 code_desc = "EXC_PPC_PERFMON"; 119 break; 120 } 121 break; 122 123 case llvm::Triple::arm: 124 case llvm::Triple::thumb: 125 if (m_exc_code == 1) 126 code_desc = "EXC_ARM_UNDEFINED"; 127 break; 128 129 default: 130 break; 131 } 132 break; 133 134 case 3: // EXC_ARITHMETIC 135 exc_desc = "EXC_ARITHMETIC"; 136 switch (cpu) { 137 case llvm::Triple::x86: 138 case llvm::Triple::x86_64: 139 switch (m_exc_code) { 140 case 1: 141 code_desc = "EXC_I386_DIV"; 142 break; 143 case 2: 144 code_desc = "EXC_I386_INTO"; 145 break; 146 case 3: 147 code_desc = "EXC_I386_NOEXT"; 148 break; 149 case 4: 150 code_desc = "EXC_I386_EXTOVR"; 151 break; 152 case 5: 153 code_desc = "EXC_I386_EXTERR"; 154 break; 155 case 6: 156 code_desc = "EXC_I386_EMERR"; 157 break; 158 case 7: 159 code_desc = "EXC_I386_BOUND"; 160 break; 161 case 8: 162 code_desc = "EXC_I386_SSEEXTERR"; 163 break; 164 } 165 break; 166 167 case llvm::Triple::ppc: 168 case llvm::Triple::ppc64: 169 switch (m_exc_code) { 170 case 1: 171 code_desc = "EXC_PPC_OVERFLOW"; 172 break; 173 case 2: 174 code_desc = "EXC_PPC_ZERO_DIVIDE"; 175 break; 176 case 3: 177 code_desc = "EXC_PPC_FLT_INEXACT"; 178 break; 179 case 4: 180 code_desc = "EXC_PPC_FLT_ZERO_DIVIDE"; 181 break; 182 case 5: 183 code_desc = "EXC_PPC_FLT_UNDERFLOW"; 184 break; 185 case 6: 186 code_desc = "EXC_PPC_FLT_OVERFLOW"; 187 break; 188 case 7: 189 code_desc = "EXC_PPC_FLT_NOT_A_NUMBER"; 190 break; 191 } 192 break; 193 194 default: 195 break; 196 } 197 break; 198 199 case 4: // EXC_EMULATION 200 exc_desc = "EXC_EMULATION"; 201 break; 202 203 case 5: // EXC_SOFTWARE 204 exc_desc = "EXC_SOFTWARE"; 205 if (m_exc_code == 0x10003) { 206 subcode_desc = "EXC_SOFT_SIGNAL"; 207 subcode_label = "signo"; 208 } 209 break; 210 211 case 6: // EXC_BREAKPOINT 212 { 213 exc_desc = "EXC_BREAKPOINT"; 214 switch (cpu) { 215 case llvm::Triple::x86: 216 case llvm::Triple::x86_64: 217 switch (m_exc_code) { 218 case 1: 219 code_desc = "EXC_I386_SGL"; 220 break; 221 case 2: 222 code_desc = "EXC_I386_BPT"; 223 break; 224 } 225 break; 226 227 case llvm::Triple::ppc: 228 case llvm::Triple::ppc64: 229 switch (m_exc_code) { 230 case 1: 231 code_desc = "EXC_PPC_BREAKPOINT"; 232 break; 233 } 234 break; 235 236 case llvm::Triple::arm: 237 case llvm::Triple::thumb: 238 switch (m_exc_code) { 239 case 0x101: 240 code_desc = "EXC_ARM_DA_ALIGN"; 241 break; 242 case 0x102: 243 code_desc = "EXC_ARM_DA_DEBUG"; 244 break; 245 case 1: 246 code_desc = "EXC_ARM_BREAKPOINT"; 247 break; 248 // FIXME temporary workaround, exc_code 0 does not really mean 249 // EXC_ARM_BREAKPOINT 250 case 0: 251 code_desc = "EXC_ARM_BREAKPOINT"; 252 break; 253 } 254 break; 255 256 default: 257 break; 258 } 259 } break; 260 261 case 7: 262 exc_desc = "EXC_SYSCALL"; 263 break; 264 265 case 8: 266 exc_desc = "EXC_MACH_SYSCALL"; 267 break; 268 269 case 9: 270 exc_desc = "EXC_RPC_ALERT"; 271 break; 272 273 case 10: 274 exc_desc = "EXC_CRASH"; 275 break; 276 case 11: 277 exc_desc = "EXC_RESOURCE"; 278 break; 279 case 12: 280 exc_desc = "EXC_GUARD"; 281 break; 282 } 283 284 StreamString strm; 285 286 if (exc_desc) 287 strm.PutCString(exc_desc); 288 else 289 strm.Printf("EXC_??? (%" PRIu64 ")", m_value); 290 291 if (m_exc_data_count >= 1) { 292 if (code_desc) 293 strm.Printf(" (%s=%s", code_label, code_desc); 294 else 295 strm.Printf(" (%s=%" PRIu64, code_label, m_exc_code); 296 } 297 298 if (m_exc_data_count >= 2) { 299 if (subcode_desc) 300 strm.Printf(", %s=%s", subcode_label, subcode_desc); 301 else 302 strm.Printf(", %s=0x%" PRIx64, subcode_label, m_exc_subcode); 303 } 304 305 if (m_exc_data_count > 0) 306 strm.PutChar(')'); 307 308 m_description = strm.GetString(); 309 } 310 return m_description.c_str(); 311 } 312 313 StopInfoSP StopInfoMachException::CreateStopReasonWithMachException( 314 Thread &thread, uint32_t exc_type, uint32_t exc_data_count, 315 uint64_t exc_code, uint64_t exc_sub_code, uint64_t exc_sub_sub_code, 316 bool pc_already_adjusted, bool adjust_pc_if_needed) { 317 if (exc_type != 0) { 318 uint32_t pc_decrement = 0; 319 ExecutionContext exe_ctx(thread.shared_from_this()); 320 Target *target = exe_ctx.GetTargetPtr(); 321 const llvm::Triple::ArchType cpu = 322 target ? target->GetArchitecture().GetMachine() 323 : llvm::Triple::UnknownArch; 324 325 switch (exc_type) { 326 case 1: // EXC_BAD_ACCESS 327 break; 328 329 case 2: // EXC_BAD_INSTRUCTION 330 switch (cpu) { 331 case llvm::Triple::ppc: 332 case llvm::Triple::ppc64: 333 switch (exc_code) { 334 case 1: // EXC_PPC_INVALID_SYSCALL 335 case 2: // EXC_PPC_UNIPL_INST 336 case 3: // EXC_PPC_PRIVINST 337 case 4: // EXC_PPC_PRIVREG 338 break; 339 case 5: // EXC_PPC_TRACE 340 return StopInfo::CreateStopReasonToTrace(thread); 341 case 6: // EXC_PPC_PERFMON 342 break; 343 } 344 break; 345 346 default: 347 break; 348 } 349 break; 350 351 case 3: // EXC_ARITHMETIC 352 case 4: // EXC_EMULATION 353 break; 354 355 case 5: // EXC_SOFTWARE 356 if (exc_code == 0x10003) // EXC_SOFT_SIGNAL 357 { 358 if (exc_sub_code == 5) { 359 // On MacOSX, a SIGTRAP can signify that a process has called 360 // exec, so we should check with our dynamic loader to verify. 361 ProcessSP process_sp(thread.GetProcess()); 362 if (process_sp) { 363 DynamicLoader *dynamic_loader = process_sp->GetDynamicLoader(); 364 if (dynamic_loader && dynamic_loader->ProcessDidExec()) { 365 // The program was re-exec'ed 366 return StopInfo::CreateStopReasonWithExec(thread); 367 } 368 // if (!process_did_exec) 369 // { 370 // // We have a SIGTRAP, make sure we 371 // didn't exec by checking 372 // // for the PC being at 373 // "_dyld_start"... 374 // lldb::StackFrameSP frame_sp 375 // (thread.GetStackFrameAtIndex(0)); 376 // if (frame_sp) 377 // { 378 // const Symbol *symbol = 379 // frame_sp->GetSymbolContext(eSymbolContextSymbol).symbol; 380 // if (symbol) 381 // { 382 // if (symbol->GetName() == 383 // ConstString("_dyld_start")) 384 // process_did_exec = true; 385 // } 386 // } 387 // } 388 } 389 } 390 return StopInfo::CreateStopReasonWithSignal(thread, exc_sub_code); 391 } 392 break; 393 394 case 6: // EXC_BREAKPOINT 395 { 396 bool is_actual_breakpoint = false; 397 bool is_trace_if_actual_breakpoint_missing = false; 398 switch (cpu) { 399 case llvm::Triple::x86: 400 case llvm::Triple::x86_64: 401 if (exc_code == 1) // EXC_I386_SGL 402 { 403 if (!exc_sub_code) { 404 // This looks like a plain trap. 405 // Have to check if there is a breakpoint here as well. When you 406 // single-step onto a trap, 407 // the single step stops you not to trap. Since we also do that 408 // check below, let's just use 409 // that logic. 410 is_actual_breakpoint = true; 411 is_trace_if_actual_breakpoint_missing = true; 412 } else { 413 414 // It's a watchpoint, then. 415 // The exc_sub_code indicates the data break address. 416 lldb::WatchpointSP wp_sp; 417 if (target) 418 wp_sp = target->GetWatchpointList().FindByAddress( 419 (lldb::addr_t)exc_sub_code); 420 if (wp_sp && wp_sp->IsEnabled()) { 421 // Debugserver may piggyback the hardware index of the fired 422 // watchpoint in the exception data. 423 // Set the hardware index if that's the case. 424 if (exc_data_count >= 3) 425 wp_sp->SetHardwareIndex((uint32_t)exc_sub_sub_code); 426 return StopInfo::CreateStopReasonWithWatchpointID(thread, 427 wp_sp->GetID()); 428 } 429 } 430 } else if (exc_code == 2 || // EXC_I386_BPT 431 exc_code == 3) // EXC_I386_BPTFLT 432 { 433 // KDP returns EXC_I386_BPTFLT for trace breakpoints 434 if (exc_code == 3) 435 is_trace_if_actual_breakpoint_missing = true; 436 437 is_actual_breakpoint = true; 438 if (!pc_already_adjusted) 439 pc_decrement = 1; 440 } 441 break; 442 443 case llvm::Triple::ppc: 444 case llvm::Triple::ppc64: 445 is_actual_breakpoint = exc_code == 1; // EXC_PPC_BREAKPOINT 446 break; 447 448 case llvm::Triple::arm: 449 case llvm::Triple::thumb: 450 if (exc_code == 0x102) // EXC_ARM_DA_DEBUG 451 { 452 // It's a watchpoint, then, if the exc_sub_code indicates a 453 // known/enabled 454 // data break address from our watchpoint list. 455 lldb::WatchpointSP wp_sp; 456 if (target) 457 wp_sp = target->GetWatchpointList().FindByAddress( 458 (lldb::addr_t)exc_sub_code); 459 if (wp_sp && wp_sp->IsEnabled()) { 460 // Debugserver may piggyback the hardware index of the fired 461 // watchpoint in the exception data. 462 // Set the hardware index if that's the case. 463 if (exc_data_count >= 3) 464 wp_sp->SetHardwareIndex((uint32_t)exc_sub_sub_code); 465 return StopInfo::CreateStopReasonWithWatchpointID(thread, 466 wp_sp->GetID()); 467 } else { 468 is_actual_breakpoint = true; 469 is_trace_if_actual_breakpoint_missing = true; 470 } 471 } else if (exc_code == 1) // EXC_ARM_BREAKPOINT 472 { 473 is_actual_breakpoint = true; 474 is_trace_if_actual_breakpoint_missing = true; 475 } else if (exc_code == 0) // FIXME not EXC_ARM_BREAKPOINT but a kernel 476 // is currently returning this so accept it as 477 // indicating a breakpoint until the kernel is 478 // fixed 479 { 480 is_actual_breakpoint = true; 481 is_trace_if_actual_breakpoint_missing = true; 482 } 483 break; 484 485 case llvm::Triple::aarch64: { 486 if (exc_code == 1 && exc_sub_code == 0) // EXC_ARM_BREAKPOINT 487 { 488 // This is hit when we single instruction step aka MDSCR_EL1 SS bit 0 489 // is set 490 is_actual_breakpoint = false; 491 is_trace_if_actual_breakpoint_missing = true; 492 } 493 if (exc_code == 0x102) // EXC_ARM_DA_DEBUG 494 { 495 // It's a watchpoint, then, if the exc_sub_code indicates a 496 // known/enabled 497 // data break address from our watchpoint list. 498 lldb::WatchpointSP wp_sp; 499 if (target) 500 wp_sp = target->GetWatchpointList().FindByAddress( 501 (lldb::addr_t)exc_sub_code); 502 if (wp_sp && wp_sp->IsEnabled()) { 503 // Debugserver may piggyback the hardware index of the fired 504 // watchpoint in the exception data. 505 // Set the hardware index if that's the case. 506 if (exc_data_count >= 3) 507 wp_sp->SetHardwareIndex((uint32_t)exc_sub_sub_code); 508 return StopInfo::CreateStopReasonWithWatchpointID(thread, 509 wp_sp->GetID()); 510 } 511 // EXC_ARM_DA_DEBUG seems to be reused for EXC_BREAKPOINT as well as 512 // EXC_BAD_ACCESS 513 if (thread.GetTemporaryResumeState() == eStateStepping) 514 return StopInfo::CreateStopReasonToTrace(thread); 515 } 516 // It looks like exc_sub_code has the 4 bytes of the instruction that 517 // triggered the 518 // exception, i.e. our breakpoint opcode 519 is_actual_breakpoint = exc_code == 1; 520 break; 521 } 522 523 default: 524 break; 525 } 526 527 if (is_actual_breakpoint) { 528 RegisterContextSP reg_ctx_sp(thread.GetRegisterContext()); 529 addr_t pc = reg_ctx_sp->GetPC() - pc_decrement; 530 531 ProcessSP process_sp(thread.CalculateProcess()); 532 533 lldb::BreakpointSiteSP bp_site_sp; 534 if (process_sp) 535 bp_site_sp = process_sp->GetBreakpointSiteList().FindByAddress(pc); 536 if (bp_site_sp && bp_site_sp->IsEnabled()) { 537 // Update the PC if we were asked to do so, but only do 538 // so if we find a breakpoint that we know about cause 539 // this could be a trap instruction in the code 540 if (pc_decrement > 0 && adjust_pc_if_needed) 541 reg_ctx_sp->SetPC(pc); 542 543 // If the breakpoint is for this thread, then we'll report the hit, 544 // but if it is for another thread, 545 // we can just report no reason. We don't need to worry about 546 // stepping over the breakpoint here, that 547 // will be taken care of when the thread resumes and notices that 548 // there's a breakpoint under the pc. 549 // If we have an operating system plug-in, we might have set a thread 550 // specific breakpoint using the 551 // operating system thread ID, so we can't make any assumptions about 552 // the thread ID so we must always 553 // report the breakpoint regardless of the thread. 554 if (bp_site_sp->ValidForThisThread(&thread) || 555 thread.GetProcess()->GetOperatingSystem() != NULL) 556 return StopInfo::CreateStopReasonWithBreakpointSiteID( 557 thread, bp_site_sp->GetID()); 558 else if (is_trace_if_actual_breakpoint_missing) 559 return StopInfo::CreateStopReasonToTrace(thread); 560 else 561 return StopInfoSP(); 562 } 563 564 // Don't call this a trace if we weren't single stepping this thread. 565 if (is_trace_if_actual_breakpoint_missing && 566 thread.GetTemporaryResumeState() == eStateStepping) { 567 return StopInfo::CreateStopReasonToTrace(thread); 568 } 569 } 570 } break; 571 572 case 7: // EXC_SYSCALL 573 case 8: // EXC_MACH_SYSCALL 574 case 9: // EXC_RPC_ALERT 575 case 10: // EXC_CRASH 576 break; 577 } 578 579 return StopInfoSP(new StopInfoMachException( 580 thread, exc_type, exc_data_count, exc_code, exc_sub_code)); 581 } 582 return StopInfoSP(); 583 } 584