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 } 214 215 StreamString strm; 216 217 if (exc_desc) 218 strm.PutCString(exc_desc); 219 else 220 strm.Printf("EXC_??? (%" PRIu64 ")", m_value); 221 222 if (m_exc_data_count >= 1) 223 { 224 if (code_desc) 225 strm.Printf(" (%s=%s", code_label, code_desc); 226 else 227 strm.Printf(" (%s=%" PRIu64, code_label, m_exc_code); 228 } 229 230 if (m_exc_data_count >= 2) 231 { 232 if (subcode_desc) 233 strm.Printf(", %s=%s", subcode_label, subcode_desc); 234 else 235 strm.Printf(", %s=0x%" PRIx64, subcode_label, m_exc_subcode); 236 } 237 238 if (m_exc_data_count > 0) 239 strm.PutChar(')'); 240 241 m_description.swap (strm.GetString()); 242 } 243 return m_description.c_str(); 244 } 245 246 247 248 249 250 StopInfoSP 251 StopInfoMachException::CreateStopReasonWithMachException 252 ( 253 Thread &thread, 254 uint32_t exc_type, 255 uint32_t exc_data_count, 256 uint64_t exc_code, 257 uint64_t exc_sub_code, 258 uint64_t exc_sub_sub_code, 259 bool pc_already_adjusted, 260 bool adjust_pc_if_needed 261 ) 262 { 263 if (exc_type != 0) 264 { 265 uint32_t pc_decrement = 0; 266 ExecutionContext exe_ctx (thread.shared_from_this()); 267 Target *target = exe_ctx.GetTargetPtr(); 268 const llvm::Triple::ArchType cpu = target ? target->GetArchitecture().GetMachine() : llvm::Triple::UnknownArch; 269 270 switch (exc_type) 271 { 272 case 1: // EXC_BAD_ACCESS 273 break; 274 275 case 2: // EXC_BAD_INSTRUCTION 276 switch (cpu) 277 { 278 case llvm::Triple::ppc: 279 case llvm::Triple::ppc64: 280 switch (exc_code) 281 { 282 case 1: // EXC_PPC_INVALID_SYSCALL 283 case 2: // EXC_PPC_UNIPL_INST 284 case 3: // EXC_PPC_PRIVINST 285 case 4: // EXC_PPC_PRIVREG 286 break; 287 case 5: // EXC_PPC_TRACE 288 return StopInfo::CreateStopReasonToTrace (thread); 289 case 6: // EXC_PPC_PERFMON 290 break; 291 } 292 break; 293 294 default: 295 break; 296 } 297 break; 298 299 case 3: // EXC_ARITHMETIC 300 case 4: // EXC_EMULATION 301 break; 302 303 case 5: // EXC_SOFTWARE 304 if (exc_code == 0x10003) // EXC_SOFT_SIGNAL 305 { 306 if (exc_sub_code == 5) 307 { 308 // On MacOSX, a SIGTRAP can signify that a process has called 309 // exec, so we should check with our dynamic loader to verify. 310 ProcessSP process_sp (thread.GetProcess()); 311 if (process_sp) 312 { 313 DynamicLoader *dynamic_loader = process_sp->GetDynamicLoader(); 314 if (dynamic_loader && dynamic_loader->ProcessDidExec()) 315 { 316 // The program was re-exec'ed 317 return StopInfo::CreateStopReasonWithExec (thread); 318 } 319 // if (!process_did_exec) 320 // { 321 // // We have a SIGTRAP, make sure we didn't exec by checking 322 // // for the PC being at "_dyld_start"... 323 // lldb::StackFrameSP frame_sp (thread.GetStackFrameAtIndex(0)); 324 // if (frame_sp) 325 // { 326 // const Symbol *symbol = frame_sp->GetSymbolContext(eSymbolContextSymbol).symbol; 327 // if (symbol) 328 // { 329 // if (symbol->GetName() == ConstString("_dyld_start")) 330 // process_did_exec = true; 331 // } 332 // } 333 // } 334 } 335 } 336 return StopInfo::CreateStopReasonWithSignal (thread, exc_sub_code); 337 } 338 break; 339 340 case 6: // EXC_BREAKPOINT 341 { 342 bool is_software_breakpoint = false; 343 bool is_trace_if_software_breakpoint_missing = false; 344 switch (cpu) 345 { 346 case llvm::Triple::x86: 347 case llvm::Triple::x86_64: 348 if (exc_code == 1) // EXC_I386_SGL 349 { 350 if (!exc_sub_code) 351 return StopInfo::CreateStopReasonToTrace(thread); 352 353 // It's a watchpoint, then. 354 // The exc_sub_code indicates the data break address. 355 lldb::WatchpointSP wp_sp; 356 if (target) 357 wp_sp = target->GetWatchpointList().FindByAddress((lldb::addr_t)exc_sub_code); 358 if (wp_sp && wp_sp->IsEnabled()) 359 { 360 // Debugserver may piggyback the hardware index of the fired watchpoint in the exception data. 361 // Set the hardware index if that's the case. 362 if (exc_data_count >=3) 363 wp_sp->SetHardwareIndex((uint32_t)exc_sub_sub_code); 364 return StopInfo::CreateStopReasonWithWatchpointID(thread, wp_sp->GetID()); 365 } 366 } 367 else if (exc_code == 2 || // EXC_I386_BPT 368 exc_code == 3) // EXC_I386_BPTFLT 369 { 370 // KDP returns EXC_I386_BPTFLT for trace breakpoints 371 if (exc_code == 3) 372 is_trace_if_software_breakpoint_missing = true; 373 374 is_software_breakpoint = true; 375 if (!pc_already_adjusted) 376 pc_decrement = 1; 377 } 378 break; 379 380 case llvm::Triple::ppc: 381 case llvm::Triple::ppc64: 382 is_software_breakpoint = exc_code == 1; // EXC_PPC_BREAKPOINT 383 break; 384 385 case llvm::Triple::arm: 386 if (exc_code == 0x102) 387 { 388 // It's a watchpoint, then, if the exc_sub_code indicates a known/enabled 389 // data break address from our watchpoint list. 390 lldb::WatchpointSP wp_sp; 391 if (target) 392 wp_sp = target->GetWatchpointList().FindByAddress((lldb::addr_t)exc_sub_code); 393 if (wp_sp && wp_sp->IsEnabled()) 394 { 395 // Debugserver may piggyback the hardware index of the fired watchpoint in the exception data. 396 // Set the hardware index if that's the case. 397 if (exc_data_count >=3) 398 wp_sp->SetHardwareIndex((uint32_t)exc_sub_sub_code); 399 return StopInfo::CreateStopReasonWithWatchpointID(thread, wp_sp->GetID()); 400 } 401 // EXC_ARM_DA_DEBUG seems to be reused for EXC_BREAKPOINT as well as EXC_BAD_ACCESS 402 if (thread.GetTemporaryResumeState() == eStateStepping) 403 return StopInfo::CreateStopReasonToTrace(thread); 404 } 405 else if (exc_code == 1) 406 { 407 is_software_breakpoint = true; 408 is_trace_if_software_breakpoint_missing = true; 409 } 410 break; 411 412 default: 413 break; 414 } 415 416 if (is_software_breakpoint) 417 { 418 RegisterContextSP reg_ctx_sp (thread.GetRegisterContext()); 419 addr_t pc = reg_ctx_sp->GetPC() - pc_decrement; 420 421 ProcessSP process_sp (thread.CalculateProcess()); 422 423 lldb::BreakpointSiteSP bp_site_sp; 424 if (process_sp) 425 bp_site_sp = process_sp->GetBreakpointSiteList().FindByAddress(pc); 426 if (bp_site_sp && bp_site_sp->IsEnabled()) 427 { 428 // Update the PC if we were asked to do so, but only do 429 // so if we find a breakpoint that we know about cause 430 // this could be a trap instruction in the code 431 if (pc_decrement > 0 && adjust_pc_if_needed) 432 reg_ctx_sp->SetPC (pc); 433 434 // If the breakpoint is for this thread, then we'll report the hit, but if it is for another thread, 435 // we can just report no reason. We don't need to worry about stepping over the breakpoint here, that 436 // will be taken care of when the thread resumes and notices that there's a breakpoint under the pc. 437 if (bp_site_sp->ValidForThisThread (&thread)) 438 return StopInfo::CreateStopReasonWithBreakpointSiteID (thread, bp_site_sp->GetID()); 439 else 440 return StopInfoSP(); 441 } 442 443 // Don't call this a trace if we weren't single stepping this thread. 444 if (is_trace_if_software_breakpoint_missing && thread.GetTemporaryResumeState() == eStateStepping) 445 { 446 return StopInfo::CreateStopReasonToTrace (thread); 447 } 448 } 449 } 450 break; 451 452 case 7: // EXC_SYSCALL 453 case 8: // EXC_MACH_SYSCALL 454 case 9: // EXC_RPC_ALERT 455 case 10: // EXC_CRASH 456 break; 457 } 458 459 return StopInfoSP(new StopInfoMachException (thread, exc_type, exc_data_count, exc_code, exc_sub_code)); 460 } 461 return StopInfoSP(); 462 } 463