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