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"
13 #if defined(__APPLE__)
14 // Needed for the EXC_RESOURCE interpretation macros
15 #include <kern/exc_resource.h>
18 #include "lldb/Breakpoint/Watchpoint.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 #include "lldb/Utility/StreamString.h"
31 using namespace lldb_private;
33 const char *StopInfoMachException::GetDescription() {
34 if (m_description.empty() && m_value != 0) {
35 ExecutionContext exe_ctx(m_thread_wp.lock());
36 Target *target = exe_ctx.GetTargetPtr();
37 const llvm::Triple::ArchType cpu =
38 target ? target->GetArchitecture().GetMachine()
39 : llvm::Triple::UnknownArch;
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;
47 #if defined(__APPLE__)
48 char code_desc_buf[32];
49 char subcode_desc_buf[32];
53 case 1: // EXC_BAD_ACCESS
54 exc_desc = "EXC_BAD_ACCESS";
55 subcode_label = "address";
57 case llvm::Triple::x86:
58 case llvm::Triple::x86_64:
61 code_desc = "EXC_I386_GPFLT";
66 case llvm::Triple::arm:
67 case llvm::Triple::thumb:
70 code_desc = "EXC_ARM_DA_ALIGN";
73 code_desc = "EXC_ARM_DA_DEBUG";
78 case llvm::Triple::ppc:
79 case llvm::Triple::ppc64:
82 code_desc = "EXC_PPC_VM_PROT_READ";
85 code_desc = "EXC_PPC_BADSPACE";
88 code_desc = "EXC_PPC_UNALIGNED";
98 case 2: // EXC_BAD_INSTRUCTION
99 exc_desc = "EXC_BAD_INSTRUCTION";
101 case llvm::Triple::x86:
102 case llvm::Triple::x86_64:
104 code_desc = "EXC_I386_INVOP";
107 case llvm::Triple::ppc:
108 case llvm::Triple::ppc64:
109 switch (m_exc_code) {
111 code_desc = "EXC_PPC_INVALID_SYSCALL";
114 code_desc = "EXC_PPC_UNIPL_INST";
117 code_desc = "EXC_PPC_PRIVINST";
120 code_desc = "EXC_PPC_PRIVREG";
123 code_desc = "EXC_PPC_TRACE";
126 code_desc = "EXC_PPC_PERFMON";
131 case llvm::Triple::arm:
132 case llvm::Triple::thumb:
134 code_desc = "EXC_ARM_UNDEFINED";
142 case 3: // EXC_ARITHMETIC
143 exc_desc = "EXC_ARITHMETIC";
145 case llvm::Triple::x86:
146 case llvm::Triple::x86_64:
147 switch (m_exc_code) {
149 code_desc = "EXC_I386_DIV";
152 code_desc = "EXC_I386_INTO";
155 code_desc = "EXC_I386_NOEXT";
158 code_desc = "EXC_I386_EXTOVR";
161 code_desc = "EXC_I386_EXTERR";
164 code_desc = "EXC_I386_EMERR";
167 code_desc = "EXC_I386_BOUND";
170 code_desc = "EXC_I386_SSEEXTERR";
175 case llvm::Triple::ppc:
176 case llvm::Triple::ppc64:
177 switch (m_exc_code) {
179 code_desc = "EXC_PPC_OVERFLOW";
182 code_desc = "EXC_PPC_ZERO_DIVIDE";
185 code_desc = "EXC_PPC_FLT_INEXACT";
188 code_desc = "EXC_PPC_FLT_ZERO_DIVIDE";
191 code_desc = "EXC_PPC_FLT_UNDERFLOW";
194 code_desc = "EXC_PPC_FLT_OVERFLOW";
197 code_desc = "EXC_PPC_FLT_NOT_A_NUMBER";
207 case 4: // EXC_EMULATION
208 exc_desc = "EXC_EMULATION";
211 case 5: // EXC_SOFTWARE
212 exc_desc = "EXC_SOFTWARE";
213 if (m_exc_code == 0x10003) {
214 subcode_desc = "EXC_SOFT_SIGNAL";
215 subcode_label = "signo";
219 case 6: // EXC_BREAKPOINT
221 exc_desc = "EXC_BREAKPOINT";
223 case llvm::Triple::x86:
224 case llvm::Triple::x86_64:
225 switch (m_exc_code) {
227 code_desc = "EXC_I386_SGL";
230 code_desc = "EXC_I386_BPT";
235 case llvm::Triple::ppc:
236 case llvm::Triple::ppc64:
237 switch (m_exc_code) {
239 code_desc = "EXC_PPC_BREAKPOINT";
244 case llvm::Triple::arm:
245 case llvm::Triple::thumb:
246 switch (m_exc_code) {
248 code_desc = "EXC_ARM_DA_ALIGN";
251 code_desc = "EXC_ARM_DA_DEBUG";
254 code_desc = "EXC_ARM_BREAKPOINT";
256 // FIXME temporary workaround, exc_code 0 does not really mean
257 // EXC_ARM_BREAKPOINT
259 code_desc = "EXC_ARM_BREAKPOINT";
270 exc_desc = "EXC_SYSCALL";
274 exc_desc = "EXC_MACH_SYSCALL";
278 exc_desc = "EXC_RPC_ALERT";
282 exc_desc = "EXC_CRASH";
285 exc_desc = "EXC_RESOURCE";
286 #if defined(__APPLE__)
288 int resource_type = EXC_RESOURCE_DECODE_RESOURCE_TYPE(m_exc_code);
290 code_label = "limit";
291 code_desc = code_desc_buf;
292 subcode_label = "observed";
293 subcode_desc = subcode_desc_buf;
295 switch (resource_type) {
296 case RESOURCE_TYPE_CPU:
297 exc_desc = "EXC_RESOURCE RESOURCE_TYPE_CPU";
298 snprintf(code_desc_buf, sizeof(code_desc_buf), "%d%%",
299 (int)EXC_RESOURCE_CPUMONITOR_DECODE_PERCENTAGE(m_exc_code));
300 snprintf(subcode_desc_buf, sizeof(subcode_desc_buf), "%d%%",
301 (int)EXC_RESOURCE_CPUMONITOR_DECODE_PERCENTAGE_OBSERVED(m_exc_subcode));
303 case RESOURCE_TYPE_WAKEUPS:
304 exc_desc = "EXC_RESOURCE RESOURCE_TYPE_WAKEUPS";
305 snprintf(code_desc_buf, sizeof(code_desc_buf), "%d w/s",
306 (int)EXC_RESOURCE_CPUMONITOR_DECODE_WAKEUPS_PERMITTED(m_exc_code));
307 snprintf(subcode_desc_buf, sizeof(subcode_desc_buf), "%d w/s",
308 (int)EXC_RESOURCE_CPUMONITOR_DECODE_WAKEUPS_OBSERVED(m_exc_subcode));
310 case RESOURCE_TYPE_MEMORY:
311 exc_desc = "EXC_RESOURCE RESOURCE_TYPE_MEMORY";
312 snprintf(code_desc_buf, sizeof(code_desc_buf), "%d MB",
313 (int)EXC_RESOURCE_HWM_DECODE_LIMIT(m_exc_code));
314 subcode_desc = nullptr;
315 subcode_label = "unused";
317 case RESOURCE_TYPE_IO:
318 exc_desc = "EXC_RESOURCE RESOURCE_TYPE_IO";
319 snprintf(code_desc_buf, sizeof(code_desc_buf), "%d MB",
320 (int)EXC_RESOURCE_IO_DECODE_LIMIT(m_exc_code));
321 snprintf(subcode_desc_buf, sizeof(subcode_desc_buf), "%d MB",
322 (int)EXC_RESOURCE_IO_OBSERVED(m_exc_subcode));;
329 exc_desc = "EXC_GUARD";
336 strm.PutCString(exc_desc);
338 strm.Printf("EXC_??? (%" PRIu64 ")", m_value);
340 if (m_exc_data_count >= 1) {
342 strm.Printf(" (%s=%s", code_label, code_desc);
344 strm.Printf(" (%s=%" PRIu64, code_label, m_exc_code);
347 if (m_exc_data_count >= 2) {
349 strm.Printf(", %s=%s", subcode_label, subcode_desc);
351 strm.Printf(", %s=0x%" PRIx64, subcode_label, m_exc_subcode);
354 if (m_exc_data_count > 0)
357 m_description = strm.GetString();
359 return m_description.c_str();
362 StopInfoSP StopInfoMachException::CreateStopReasonWithMachException(
363 Thread &thread, uint32_t exc_type, uint32_t exc_data_count,
364 uint64_t exc_code, uint64_t exc_sub_code, uint64_t exc_sub_sub_code,
365 bool pc_already_adjusted, bool adjust_pc_if_needed) {
367 uint32_t pc_decrement = 0;
368 ExecutionContext exe_ctx(thread.shared_from_this());
369 Target *target = exe_ctx.GetTargetPtr();
370 const llvm::Triple::ArchType cpu =
371 target ? target->GetArchitecture().GetMachine()
372 : llvm::Triple::UnknownArch;
375 case 1: // EXC_BAD_ACCESS
378 case 2: // EXC_BAD_INSTRUCTION
380 case llvm::Triple::ppc:
381 case llvm::Triple::ppc64:
383 case 1: // EXC_PPC_INVALID_SYSCALL
384 case 2: // EXC_PPC_UNIPL_INST
385 case 3: // EXC_PPC_PRIVINST
386 case 4: // EXC_PPC_PRIVREG
388 case 5: // EXC_PPC_TRACE
389 return StopInfo::CreateStopReasonToTrace(thread);
390 case 6: // EXC_PPC_PERFMON
400 case 3: // EXC_ARITHMETIC
401 case 4: // EXC_EMULATION
404 case 5: // EXC_SOFTWARE
405 if (exc_code == 0x10003) // EXC_SOFT_SIGNAL
407 if (exc_sub_code == 5) {
408 // On MacOSX, a SIGTRAP can signify that a process has called exec,
409 // so we should check with our dynamic loader to verify.
410 ProcessSP process_sp(thread.GetProcess());
412 DynamicLoader *dynamic_loader = process_sp->GetDynamicLoader();
413 if (dynamic_loader && dynamic_loader->ProcessDidExec()) {
414 // The program was re-exec'ed
415 return StopInfo::CreateStopReasonWithExec(thread);
417 // if (!process_did_exec)
419 // // We have a SIGTRAP, make sure we
420 // didn't exec by checking
421 // // for the PC being at
423 // lldb::StackFrameSP frame_sp
424 // (thread.GetStackFrameAtIndex(0));
427 // const Symbol *symbol =
428 // frame_sp->GetSymbolContext(eSymbolContextSymbol).symbol;
431 // if (symbol->GetName() ==
432 // ConstString("_dyld_start"))
433 // process_did_exec = true;
439 return StopInfo::CreateStopReasonWithSignal(thread, exc_sub_code);
443 case 6: // EXC_BREAKPOINT
445 bool is_actual_breakpoint = false;
446 bool is_trace_if_actual_breakpoint_missing = false;
448 case llvm::Triple::x86:
449 case llvm::Triple::x86_64:
450 if (exc_code == 1) // EXC_I386_SGL
453 // This looks like a plain trap.
454 // Have to check if there is a breakpoint here as well. When you
455 // single-step onto a trap, the single step stops you not to trap.
456 // Since we also do that check below, let's just use that logic.
457 is_actual_breakpoint = true;
458 is_trace_if_actual_breakpoint_missing = true;
461 // It's a watchpoint, then.
462 // The exc_sub_code indicates the data break address.
463 lldb::WatchpointSP wp_sp;
465 wp_sp = target->GetWatchpointList().FindByAddress(
466 (lldb::addr_t)exc_sub_code);
467 if (wp_sp && wp_sp->IsEnabled()) {
468 // Debugserver may piggyback the hardware index of the fired
469 // watchpoint in the exception data. Set the hardware index if
471 if (exc_data_count >= 3)
472 wp_sp->SetHardwareIndex((uint32_t)exc_sub_sub_code);
473 return StopInfo::CreateStopReasonWithWatchpointID(thread,
477 } else if (exc_code == 2 || // EXC_I386_BPT
478 exc_code == 3) // EXC_I386_BPTFLT
480 // KDP returns EXC_I386_BPTFLT for trace breakpoints
482 is_trace_if_actual_breakpoint_missing = true;
484 is_actual_breakpoint = true;
485 if (!pc_already_adjusted)
490 case llvm::Triple::ppc:
491 case llvm::Triple::ppc64:
492 is_actual_breakpoint = exc_code == 1; // EXC_PPC_BREAKPOINT
495 case llvm::Triple::arm:
496 case llvm::Triple::thumb:
497 if (exc_code == 0x102) // EXC_ARM_DA_DEBUG
499 // It's a watchpoint, then, if the exc_sub_code indicates a
500 // known/enabled data break address from our watchpoint list.
501 lldb::WatchpointSP wp_sp;
503 wp_sp = target->GetWatchpointList().FindByAddress(
504 (lldb::addr_t)exc_sub_code);
505 if (wp_sp && wp_sp->IsEnabled()) {
506 // Debugserver may piggyback the hardware index of the fired
507 // watchpoint in the exception data. Set the hardware index if
509 if (exc_data_count >= 3)
510 wp_sp->SetHardwareIndex((uint32_t)exc_sub_sub_code);
511 return StopInfo::CreateStopReasonWithWatchpointID(thread,
514 is_actual_breakpoint = true;
515 is_trace_if_actual_breakpoint_missing = true;
517 } else if (exc_code == 1) // EXC_ARM_BREAKPOINT
519 is_actual_breakpoint = true;
520 is_trace_if_actual_breakpoint_missing = true;
521 } else if (exc_code == 0) // FIXME not EXC_ARM_BREAKPOINT but a kernel
522 // is currently returning this so accept it
523 // as indicating a breakpoint until the
526 is_actual_breakpoint = true;
527 is_trace_if_actual_breakpoint_missing = true;
531 case llvm::Triple::aarch64: {
532 if (exc_code == 1 && exc_sub_code == 0) // EXC_ARM_BREAKPOINT
534 // This is hit when we single instruction step aka MDSCR_EL1 SS bit 0
536 is_actual_breakpoint = false;
537 is_trace_if_actual_breakpoint_missing = true;
539 if (exc_code == 0x102) // EXC_ARM_DA_DEBUG
541 // It's a watchpoint, then, if the exc_sub_code indicates a
542 // known/enabled data break address from our watchpoint list.
543 lldb::WatchpointSP wp_sp;
545 wp_sp = target->GetWatchpointList().FindByAddress(
546 (lldb::addr_t)exc_sub_code);
547 if (wp_sp && wp_sp->IsEnabled()) {
548 // Debugserver may piggyback the hardware index of the fired
549 // watchpoint in the exception data. Set the hardware index if
551 if (exc_data_count >= 3)
552 wp_sp->SetHardwareIndex((uint32_t)exc_sub_sub_code);
553 return StopInfo::CreateStopReasonWithWatchpointID(thread,
556 // EXC_ARM_DA_DEBUG seems to be reused for EXC_BREAKPOINT as well as
558 if (thread.GetTemporaryResumeState() == eStateStepping)
559 return StopInfo::CreateStopReasonToTrace(thread);
561 // It looks like exc_sub_code has the 4 bytes of the instruction that
562 // triggered the exception, i.e. our breakpoint opcode
563 is_actual_breakpoint = exc_code == 1;
571 if (is_actual_breakpoint) {
572 RegisterContextSP reg_ctx_sp(thread.GetRegisterContext());
573 addr_t pc = reg_ctx_sp->GetPC() - pc_decrement;
575 ProcessSP process_sp(thread.CalculateProcess());
577 lldb::BreakpointSiteSP bp_site_sp;
579 bp_site_sp = process_sp->GetBreakpointSiteList().FindByAddress(pc);
580 if (bp_site_sp && bp_site_sp->IsEnabled()) {
581 // Update the PC if we were asked to do so, but only do so if we find
582 // a breakpoint that we know about cause this could be a trap
583 // instruction in the code
584 if (pc_decrement > 0 && adjust_pc_if_needed)
585 reg_ctx_sp->SetPC(pc);
587 // If the breakpoint is for this thread, then we'll report the hit,
588 // but if it is for another thread, we can just report no reason. We
589 // don't need to worry about stepping over the breakpoint here, that
590 // will be taken care of when the thread resumes and notices that
591 // there's a breakpoint under the pc. If we have an operating system
592 // plug-in, we might have set a thread specific breakpoint using the
593 // operating system thread ID, so we can't make any assumptions about
594 // the thread ID so we must always report the breakpoint regardless
596 if (bp_site_sp->ValidForThisThread(&thread) ||
597 thread.GetProcess()->GetOperatingSystem() != NULL)
598 return StopInfo::CreateStopReasonWithBreakpointSiteID(
599 thread, bp_site_sp->GetID());
600 else if (is_trace_if_actual_breakpoint_missing)
601 return StopInfo::CreateStopReasonToTrace(thread);
606 // Don't call this a trace if we weren't single stepping this thread.
607 if (is_trace_if_actual_breakpoint_missing &&
608 thread.GetTemporaryResumeState() == eStateStepping) {
609 return StopInfo::CreateStopReasonToTrace(thread);
614 case 7: // EXC_SYSCALL
615 case 8: // EXC_MACH_SYSCALL
616 case 9: // EXC_RPC_ALERT
617 case 10: // EXC_CRASH
621 return StopInfoSP(new StopInfoMachException(
622 thread, exc_type, exc_data_count, exc_code, exc_sub_code));