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