1 //===-- StopInfoMachException.cpp -------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "StopInfoMachException.h"
14 // Other libraries and framework 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"
29 using namespace lldb_private;
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;
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;
45 case 1: // EXC_BAD_ACCESS
46 exc_desc = "EXC_BAD_ACCESS";
47 subcode_label = "address";
49 case llvm::Triple::x86:
50 case llvm::Triple::x86_64:
53 code_desc = "EXC_I386_GPFLT";
58 case llvm::Triple::arm:
59 case llvm::Triple::thumb:
62 code_desc = "EXC_ARM_DA_ALIGN";
65 code_desc = "EXC_ARM_DA_DEBUG";
70 case llvm::Triple::ppc:
71 case llvm::Triple::ppc64:
74 code_desc = "EXC_PPC_VM_PROT_READ";
77 code_desc = "EXC_PPC_BADSPACE";
80 code_desc = "EXC_PPC_UNALIGNED";
90 case 2: // EXC_BAD_INSTRUCTION
91 exc_desc = "EXC_BAD_INSTRUCTION";
93 case llvm::Triple::x86:
94 case llvm::Triple::x86_64:
96 code_desc = "EXC_I386_INVOP";
99 case llvm::Triple::ppc:
100 case llvm::Triple::ppc64:
101 switch (m_exc_code) {
103 code_desc = "EXC_PPC_INVALID_SYSCALL";
106 code_desc = "EXC_PPC_UNIPL_INST";
109 code_desc = "EXC_PPC_PRIVINST";
112 code_desc = "EXC_PPC_PRIVREG";
115 code_desc = "EXC_PPC_TRACE";
118 code_desc = "EXC_PPC_PERFMON";
123 case llvm::Triple::arm:
124 case llvm::Triple::thumb:
126 code_desc = "EXC_ARM_UNDEFINED";
134 case 3: // EXC_ARITHMETIC
135 exc_desc = "EXC_ARITHMETIC";
137 case llvm::Triple::x86:
138 case llvm::Triple::x86_64:
139 switch (m_exc_code) {
141 code_desc = "EXC_I386_DIV";
144 code_desc = "EXC_I386_INTO";
147 code_desc = "EXC_I386_NOEXT";
150 code_desc = "EXC_I386_EXTOVR";
153 code_desc = "EXC_I386_EXTERR";
156 code_desc = "EXC_I386_EMERR";
159 code_desc = "EXC_I386_BOUND";
162 code_desc = "EXC_I386_SSEEXTERR";
167 case llvm::Triple::ppc:
168 case llvm::Triple::ppc64:
169 switch (m_exc_code) {
171 code_desc = "EXC_PPC_OVERFLOW";
174 code_desc = "EXC_PPC_ZERO_DIVIDE";
177 code_desc = "EXC_PPC_FLT_INEXACT";
180 code_desc = "EXC_PPC_FLT_ZERO_DIVIDE";
183 code_desc = "EXC_PPC_FLT_UNDERFLOW";
186 code_desc = "EXC_PPC_FLT_OVERFLOW";
189 code_desc = "EXC_PPC_FLT_NOT_A_NUMBER";
199 case 4: // EXC_EMULATION
200 exc_desc = "EXC_EMULATION";
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";
211 case 6: // EXC_BREAKPOINT
213 exc_desc = "EXC_BREAKPOINT";
215 case llvm::Triple::x86:
216 case llvm::Triple::x86_64:
217 switch (m_exc_code) {
219 code_desc = "EXC_I386_SGL";
222 code_desc = "EXC_I386_BPT";
227 case llvm::Triple::ppc:
228 case llvm::Triple::ppc64:
229 switch (m_exc_code) {
231 code_desc = "EXC_PPC_BREAKPOINT";
236 case llvm::Triple::arm:
237 case llvm::Triple::thumb:
238 switch (m_exc_code) {
240 code_desc = "EXC_ARM_DA_ALIGN";
243 code_desc = "EXC_ARM_DA_DEBUG";
246 code_desc = "EXC_ARM_BREAKPOINT";
248 // FIXME temporary workaround, exc_code 0 does not really mean
249 // EXC_ARM_BREAKPOINT
251 code_desc = "EXC_ARM_BREAKPOINT";
262 exc_desc = "EXC_SYSCALL";
266 exc_desc = "EXC_MACH_SYSCALL";
270 exc_desc = "EXC_RPC_ALERT";
274 exc_desc = "EXC_CRASH";
277 exc_desc = "EXC_RESOURCE";
280 exc_desc = "EXC_GUARD";
287 strm.PutCString(exc_desc);
289 strm.Printf("EXC_??? (%" PRIu64 ")", m_value);
291 if (m_exc_data_count >= 1) {
293 strm.Printf(" (%s=%s", code_label, code_desc);
295 strm.Printf(" (%s=%" PRIu64, code_label, m_exc_code);
298 if (m_exc_data_count >= 2) {
300 strm.Printf(", %s=%s", subcode_label, subcode_desc);
302 strm.Printf(", %s=0x%" PRIx64, subcode_label, m_exc_subcode);
305 if (m_exc_data_count > 0)
308 m_description = strm.GetString();
310 return m_description.c_str();
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) {
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;
326 case 1: // EXC_BAD_ACCESS
329 case 2: // EXC_BAD_INSTRUCTION
331 case llvm::Triple::ppc:
332 case llvm::Triple::ppc64:
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
339 case 5: // EXC_PPC_TRACE
340 return StopInfo::CreateStopReasonToTrace(thread);
341 case 6: // EXC_PPC_PERFMON
351 case 3: // EXC_ARITHMETIC
352 case 4: // EXC_EMULATION
355 case 5: // EXC_SOFTWARE
356 if (exc_code == 0x10003) // EXC_SOFT_SIGNAL
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());
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);
368 // if (!process_did_exec)
370 // // We have a SIGTRAP, make sure we
371 // didn't exec by checking
372 // // for the PC being at
374 // lldb::StackFrameSP frame_sp
375 // (thread.GetStackFrameAtIndex(0));
378 // const Symbol *symbol =
379 // frame_sp->GetSymbolContext(eSymbolContextSymbol).symbol;
382 // if (symbol->GetName() ==
383 // ConstString("_dyld_start"))
384 // process_did_exec = true;
390 return StopInfo::CreateStopReasonWithSignal(thread, exc_sub_code);
394 case 6: // EXC_BREAKPOINT
396 bool is_actual_breakpoint = false;
397 bool is_trace_if_actual_breakpoint_missing = false;
399 case llvm::Triple::x86:
400 case llvm::Triple::x86_64:
401 if (exc_code == 1) // EXC_I386_SGL
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
410 is_actual_breakpoint = true;
411 is_trace_if_actual_breakpoint_missing = true;
414 // It's a watchpoint, then.
415 // The exc_sub_code indicates the data break address.
416 lldb::WatchpointSP wp_sp;
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,
430 } else if (exc_code == 2 || // EXC_I386_BPT
431 exc_code == 3) // EXC_I386_BPTFLT
433 // KDP returns EXC_I386_BPTFLT for trace breakpoints
435 is_trace_if_actual_breakpoint_missing = true;
437 is_actual_breakpoint = true;
438 if (!pc_already_adjusted)
443 case llvm::Triple::ppc:
444 case llvm::Triple::ppc64:
445 is_actual_breakpoint = exc_code == 1; // EXC_PPC_BREAKPOINT
448 case llvm::Triple::arm:
449 case llvm::Triple::thumb:
450 if (exc_code == 0x102) // EXC_ARM_DA_DEBUG
452 // It's a watchpoint, then, if the exc_sub_code indicates a
454 // data break address from our watchpoint list.
455 lldb::WatchpointSP wp_sp;
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,
468 is_actual_breakpoint = true;
469 is_trace_if_actual_breakpoint_missing = true;
471 } else if (exc_code == 1) // EXC_ARM_BREAKPOINT
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
480 is_actual_breakpoint = true;
481 is_trace_if_actual_breakpoint_missing = true;
485 case llvm::Triple::aarch64: {
486 if (exc_code == 1 && exc_sub_code == 0) // EXC_ARM_BREAKPOINT
488 // This is hit when we single instruction step aka MDSCR_EL1 SS bit 0
490 is_actual_breakpoint = false;
491 is_trace_if_actual_breakpoint_missing = true;
493 if (exc_code == 0x102) // EXC_ARM_DA_DEBUG
495 // It's a watchpoint, then, if the exc_sub_code indicates a
497 // data break address from our watchpoint list.
498 lldb::WatchpointSP wp_sp;
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,
511 // EXC_ARM_DA_DEBUG seems to be reused for EXC_BREAKPOINT as well as
513 if (thread.GetTemporaryResumeState() == eStateStepping)
514 return StopInfo::CreateStopReasonToTrace(thread);
516 // It looks like exc_sub_code has the 4 bytes of the instruction that
518 // exception, i.e. our breakpoint opcode
519 is_actual_breakpoint = exc_code == 1;
527 if (is_actual_breakpoint) {
528 RegisterContextSP reg_ctx_sp(thread.GetRegisterContext());
529 addr_t pc = reg_ctx_sp->GetPC() - pc_decrement;
531 ProcessSP process_sp(thread.CalculateProcess());
533 lldb::BreakpointSiteSP bp_site_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);
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);
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);
572 case 7: // EXC_SYSCALL
573 case 8: // EXC_MACH_SYSCALL
574 case 9: // EXC_RPC_ALERT
575 case 10: // EXC_CRASH
579 return StopInfoSP(new StopInfoMachException(
580 thread, exc_type, exc_data_count, exc_code, exc_sub_code));