1 //===-- StopInfoMachException.cpp -----------------------------------------===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 #include "StopInfoMachException.h"
11 #include "lldb/lldb-forward.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/ABI.h"
21 #include "lldb/Target/DynamicLoader.h"
22 #include "lldb/Target/ExecutionContext.h"
23 #include "lldb/Target/Process.h"
24 #include "lldb/Target/RegisterContext.h"
25 #include "lldb/Target/Target.h"
26 #include "lldb/Target/Thread.h"
27 #include "lldb/Target/ThreadPlan.h"
28 #include "lldb/Target/UnixSignals.h"
29 #include "lldb/Utility/StreamString.h"
33 using namespace lldb_private
;
35 /// Information about a pointer-authentication related instruction.
36 struct PtrauthInstructionInfo
{
42 /// Get any pointer-authentication related information about the instruction
43 /// at address \p at_addr.
44 static std::optional
<PtrauthInstructionInfo
>
45 GetPtrauthInstructionInfo(Target
&target
, const ArchSpec
&arch
,
46 const Address
&at_addr
) {
47 const char *plugin_name
= nullptr;
48 const char *flavor
= nullptr;
49 AddressRange
range_bounds(at_addr
, 4);
50 const bool prefer_file_cache
= true;
51 DisassemblerSP disassembler_sp
= Disassembler::DisassembleRange(
52 arch
, plugin_name
, flavor
, target
, range_bounds
, prefer_file_cache
);
56 InstructionList
&insn_list
= disassembler_sp
->GetInstructionList();
57 InstructionSP insn
= insn_list
.GetInstructionAtIndex(0);
61 return PtrauthInstructionInfo
{insn
->IsAuthenticated(), insn
->IsLoad(),
65 /// Describe the load address of \p addr using the format filename:line:col.
66 static void DescribeAddressBriefly(Stream
&strm
, const Address
&addr
,
68 strm
.Printf("at address=0x%" PRIx64
, addr
.GetLoadAddress(&target
));
70 if (addr
.GetDescription(s
, target
, eDescriptionLevelBrief
))
71 strm
.Printf(" %s", s
.GetString().data());
75 bool StopInfoMachException::DeterminePtrauthFailure(ExecutionContext
&exe_ctx
) {
76 bool IsBreakpoint
= m_value
== 6; // EXC_BREAKPOINT
77 bool IsBadAccess
= m_value
== 1; // EXC_BAD_ACCESS
78 if (!IsBreakpoint
&& !IsBadAccess
)
81 // Check that we have a live process.
82 if (!exe_ctx
.HasProcessScope() || !exe_ctx
.HasThreadScope() ||
83 !exe_ctx
.HasTargetScope())
86 Thread
&thread
= *exe_ctx
.GetThreadPtr();
87 StackFrameSP current_frame
= thread
.GetStackFrameAtIndex(0);
91 Target
&target
= *exe_ctx
.GetTargetPtr();
92 Process
&process
= *exe_ctx
.GetProcessPtr();
93 ABISP abi_sp
= process
.GetABI();
94 const ArchSpec
&arch
= target
.GetArchitecture();
95 assert(abi_sp
&& "Missing ABI info");
97 // Check for a ptrauth-enabled target.
98 const bool ptrauth_enabled_target
=
99 arch
.GetCore() == ArchSpec::eCore_arm_arm64e
;
100 if (!ptrauth_enabled_target
)
103 // Set up a stream we can write a diagnostic into.
105 auto emit_ptrauth_prologue
= [&](uint64_t at_address
) {
106 strm
.Printf("EXC_BAD_ACCESS (code=%" PRIu64
", address=0x%" PRIx64
")\n",
107 m_exc_code
, at_address
);
108 strm
.Printf("Note: Possible pointer authentication failure detected.\n");
111 // Check if we have a "brk 0xc47x" trap, where the value that failed to
112 // authenticate is in x16.
113 Address current_address
= current_frame
->GetFrameCodeAddress();
115 RegisterContext
*reg_ctx
= exe_ctx
.GetRegisterContext();
119 const RegisterInfo
*X16Info
= reg_ctx
->GetRegisterInfoByName("x16");
120 RegisterValue X16Val
;
121 if (!reg_ctx
->ReadRegister(X16Info
, X16Val
))
123 uint64_t bad_address
= X16Val
.GetAsUInt64();
125 uint64_t fixed_bad_address
= abi_sp
->FixCodeAddress(bad_address
);
127 if (!target
.ResolveLoadAddress(fixed_bad_address
, brk_address
))
130 auto brk_ptrauth_info
=
131 GetPtrauthInstructionInfo(target
, arch
, current_address
);
132 if (brk_ptrauth_info
&& brk_ptrauth_info
->IsAuthenticated
) {
133 emit_ptrauth_prologue(bad_address
);
134 strm
.Printf("Found value that failed to authenticate ");
135 DescribeAddressBriefly(strm
, brk_address
, target
);
136 m_description
= std::string(strm
.GetString());
142 assert(IsBadAccess
&& "Handle EXC_BAD_ACCESS only after this point");
144 // Check that we have the "bad address" from an EXC_BAD_ACCESS.
145 if (m_exc_data_count
< 2)
148 // Ok, we know the Target is valid and that it describes a ptrauth-enabled
149 // device. Now, we need to determine whether this exception was caused by a
152 uint64_t bad_address
= m_exc_subcode
;
153 uint64_t fixed_bad_address
= abi_sp
->FixCodeAddress(bad_address
);
154 uint64_t current_pc
= current_address
.GetLoadAddress(&target
);
156 // Detect: LDRAA, LDRAB (Load Register, with pointer authentication).
158 // If an authenticated load results in an exception, the instruction at the
159 // current PC should be one of LDRAx.
160 if (bad_address
!= current_pc
&& fixed_bad_address
!= current_pc
) {
162 GetPtrauthInstructionInfo(target
, arch
, current_address
);
163 if (ptrauth_info
&& ptrauth_info
->IsAuthenticated
&& ptrauth_info
->IsLoad
) {
164 emit_ptrauth_prologue(bad_address
);
165 strm
.Printf("Found authenticated load instruction ");
166 DescribeAddressBriefly(strm
, current_address
, target
);
167 m_description
= std::string(strm
.GetString());
172 // Detect: BLRAA, BLRAAZ, BLRAB, BLRABZ (Branch with Link to Register, with
173 // pointer authentication).
175 // TODO: Detect: BRAA, BRAAZ, BRAB, BRABZ (Branch to Register, with pointer
176 // authentication). At a minimum, this requires call site info support for
179 // If an authenticated call or tail call results in an exception, stripping
180 // the bad address should give the current PC, which points to the address
181 // we tried to branch to.
182 if (bad_address
!= current_pc
&& fixed_bad_address
== current_pc
) {
183 if (StackFrameSP parent_frame
= thread
.GetStackFrameAtIndex(1)) {
185 parent_frame
->GetFrameCodeAddress().GetLoadAddress(&target
);
187 if (!target
.ResolveLoadAddress(return_pc
- 4, blr_address
))
190 auto blr_ptrauth_info
=
191 GetPtrauthInstructionInfo(target
, arch
, blr_address
);
192 if (blr_ptrauth_info
&& blr_ptrauth_info
->IsAuthenticated
&&
193 blr_ptrauth_info
->DoesBranch
) {
194 emit_ptrauth_prologue(bad_address
);
195 strm
.Printf("Found authenticated indirect branch ");
196 DescribeAddressBriefly(strm
, blr_address
, target
);
197 m_description
= std::string(strm
.GetString());
203 // TODO: Detect: RETAA, RETAB (Return from subroutine, with pointer
206 // Is there a motivating, non-malicious code snippet that corrupts LR?
211 const char *StopInfoMachException::GetDescription() {
212 if (!m_description
.empty())
213 return m_description
.c_str();
214 if (GetValue() == eStopReasonInvalid
)
215 return "invalid stop reason!";
217 ExecutionContext
exe_ctx(m_thread_wp
.lock());
218 Target
*target
= exe_ctx
.GetTargetPtr();
219 const llvm::Triple::ArchType cpu
=
220 target
? target
->GetArchitecture().GetMachine()
221 : llvm::Triple::UnknownArch
;
223 const char *exc_desc
= nullptr;
224 const char *code_label
= "code";
225 const char *code_desc
= nullptr;
226 const char *subcode_label
= "subcode";
227 const char *subcode_desc
= nullptr;
229 #if defined(__APPLE__)
230 char code_desc_buf
[32];
231 char subcode_desc_buf
[32];
235 case 1: // EXC_BAD_ACCESS
236 exc_desc
= "EXC_BAD_ACCESS";
237 subcode_label
= "address";
239 case llvm::Triple::x86
:
240 case llvm::Triple::x86_64
:
241 switch (m_exc_code
) {
243 code_desc
= "EXC_I386_GPFLT";
244 m_exc_data_count
= 1;
248 case llvm::Triple::arm
:
249 case llvm::Triple::thumb
:
250 switch (m_exc_code
) {
252 code_desc
= "EXC_ARM_DA_ALIGN";
255 code_desc
= "EXC_ARM_DA_DEBUG";
260 case llvm::Triple::aarch64
:
261 if (DeterminePtrauthFailure(exe_ctx
))
262 return m_description
.c_str();
270 case 2: // EXC_BAD_INSTRUCTION
271 exc_desc
= "EXC_BAD_INSTRUCTION";
273 case llvm::Triple::x86
:
274 case llvm::Triple::x86_64
:
276 code_desc
= "EXC_I386_INVOP";
279 case llvm::Triple::arm
:
280 case llvm::Triple::thumb
:
282 code_desc
= "EXC_ARM_UNDEFINED";
290 case 3: // EXC_ARITHMETIC
291 exc_desc
= "EXC_ARITHMETIC";
293 case llvm::Triple::x86
:
294 case llvm::Triple::x86_64
:
295 switch (m_exc_code
) {
297 code_desc
= "EXC_I386_DIV";
300 code_desc
= "EXC_I386_INTO";
303 code_desc
= "EXC_I386_NOEXT";
306 code_desc
= "EXC_I386_EXTOVR";
309 code_desc
= "EXC_I386_EXTERR";
312 code_desc
= "EXC_I386_EMERR";
315 code_desc
= "EXC_I386_BOUND";
318 code_desc
= "EXC_I386_SSEEXTERR";
328 case 4: // EXC_EMULATION
329 exc_desc
= "EXC_EMULATION";
332 case 5: // EXC_SOFTWARE
333 exc_desc
= "EXC_SOFTWARE";
334 if (m_exc_code
== 0x10003) {
335 subcode_desc
= "EXC_SOFT_SIGNAL";
336 subcode_label
= "signo";
340 case 6: // EXC_BREAKPOINT
342 exc_desc
= "EXC_BREAKPOINT";
344 case llvm::Triple::x86
:
345 case llvm::Triple::x86_64
:
346 switch (m_exc_code
) {
348 code_desc
= "EXC_I386_SGL";
351 code_desc
= "EXC_I386_BPT";
356 case llvm::Triple::arm
:
357 case llvm::Triple::thumb
:
358 switch (m_exc_code
) {
360 code_desc
= "EXC_ARM_DA_ALIGN";
363 code_desc
= "EXC_ARM_DA_DEBUG";
366 code_desc
= "EXC_ARM_BREAKPOINT";
368 // FIXME temporary workaround, exc_code 0 does not really mean
369 // EXC_ARM_BREAKPOINT
371 code_desc
= "EXC_ARM_BREAKPOINT";
376 case llvm::Triple::aarch64
:
377 if (DeterminePtrauthFailure(exe_ctx
))
378 return m_description
.c_str();
387 exc_desc
= "EXC_SYSCALL";
391 exc_desc
= "EXC_MACH_SYSCALL";
395 exc_desc
= "EXC_RPC_ALERT";
399 exc_desc
= "EXC_CRASH";
402 exc_desc
= "EXC_RESOURCE";
403 #if defined(__APPLE__)
405 int resource_type
= EXC_RESOURCE_DECODE_RESOURCE_TYPE(m_exc_code
);
407 code_label
= "limit";
408 code_desc
= code_desc_buf
;
409 subcode_label
= "observed";
410 subcode_desc
= subcode_desc_buf
;
412 switch (resource_type
) {
413 case RESOURCE_TYPE_CPU
:
415 "EXC_RESOURCE (RESOURCE_TYPE_CPU: CPU usage monitor tripped)";
416 snprintf(code_desc_buf
, sizeof(code_desc_buf
), "%d%%",
417 (int)EXC_RESOURCE_CPUMONITOR_DECODE_PERCENTAGE(m_exc_code
));
418 snprintf(subcode_desc_buf
, sizeof(subcode_desc_buf
), "%d%%",
419 (int)EXC_RESOURCE_CPUMONITOR_DECODE_PERCENTAGE_OBSERVED(
422 case RESOURCE_TYPE_WAKEUPS
:
423 exc_desc
= "EXC_RESOURCE (RESOURCE_TYPE_WAKEUPS: idle wakeups monitor "
426 code_desc_buf
, sizeof(code_desc_buf
), "%d w/s",
427 (int)EXC_RESOURCE_CPUMONITOR_DECODE_WAKEUPS_PERMITTED(m_exc_code
));
428 snprintf(subcode_desc_buf
, sizeof(subcode_desc_buf
), "%d w/s",
429 (int)EXC_RESOURCE_CPUMONITOR_DECODE_WAKEUPS_OBSERVED(
432 case RESOURCE_TYPE_MEMORY
:
433 exc_desc
= "EXC_RESOURCE (RESOURCE_TYPE_MEMORY: high watermark memory "
435 snprintf(code_desc_buf
, sizeof(code_desc_buf
), "%d MB",
436 (int)EXC_RESOURCE_HWM_DECODE_LIMIT(m_exc_code
));
437 subcode_desc
= nullptr;
438 subcode_label
= nullptr;
440 #if defined(RESOURCE_TYPE_IO)
441 // RESOURCE_TYPE_IO is introduced in macOS SDK 10.12.
442 case RESOURCE_TYPE_IO
:
443 exc_desc
= "EXC_RESOURCE RESOURCE_TYPE_IO";
444 snprintf(code_desc_buf
, sizeof(code_desc_buf
), "%d MB",
445 (int)EXC_RESOURCE_IO_DECODE_LIMIT(m_exc_code
));
446 snprintf(subcode_desc_buf
, sizeof(subcode_desc_buf
), "%d MB",
447 (int)EXC_RESOURCE_IO_OBSERVED(m_exc_subcode
));
456 exc_desc
= "EXC_GUARD";
463 strm
.PutCString(exc_desc
);
465 strm
.Printf("EXC_??? (%" PRIu64
")", m_value
);
467 if (m_exc_data_count
>= 1) {
469 strm
.Printf(" (%s=%s", code_label
, code_desc
);
471 strm
.Printf(" (%s=%" PRIu64
, code_label
, m_exc_code
);
474 if (m_exc_data_count
>= 2) {
475 if (subcode_label
&& subcode_desc
)
476 strm
.Printf(", %s=%s", subcode_label
, subcode_desc
);
477 else if (subcode_label
)
478 strm
.Printf(", %s=0x%" PRIx64
, subcode_label
, m_exc_subcode
);
481 if (m_exc_data_count
> 0)
484 m_description
= std::string(strm
.GetString());
485 return m_description
.c_str();
488 static StopInfoSP
GetStopInfoForHardwareBP(Thread
&thread
, Target
*target
,
489 uint32_t exc_data_count
,
490 uint64_t exc_sub_code
,
491 uint64_t exc_sub_sub_code
) {
492 // Try hardware watchpoint.
494 // The exc_sub_code indicates the data break address.
495 lldb::WatchpointSP wp_sp
=
496 target
->GetWatchpointList().FindByAddress((lldb::addr_t
)exc_sub_code
);
497 if (wp_sp
&& wp_sp
->IsEnabled()) {
498 // Debugserver may piggyback the hardware index of the fired watchpoint
499 // in the exception data. Set the hardware index if that's the case.
500 if (exc_data_count
>= 3)
501 wp_sp
->SetHardwareIndex((uint32_t)exc_sub_sub_code
);
502 return StopInfo::CreateStopReasonWithWatchpointID(thread
, wp_sp
->GetID());
506 // Try hardware breakpoint.
507 ProcessSP
process_sp(thread
.GetProcess());
509 // The exc_sub_code indicates the data break address.
510 lldb::BreakpointSiteSP bp_sp
=
511 process_sp
->GetBreakpointSiteList().FindByAddress(
512 (lldb::addr_t
)exc_sub_code
);
513 if (bp_sp
&& bp_sp
->IsEnabled()) {
514 // Debugserver may piggyback the hardware index of the fired breakpoint
515 // in the exception data. Set the hardware index if that's the case.
516 if (exc_data_count
>= 3)
517 bp_sp
->SetHardwareIndex((uint32_t)exc_sub_sub_code
);
518 return StopInfo::CreateStopReasonWithBreakpointSiteID(thread
,
526 #if defined(__APPLE__)
528 StopInfoMachException::MachException::Name(exception_type_t exc_type
) {
531 return "EXC_BAD_ACCESS";
532 case EXC_BAD_INSTRUCTION
:
533 return "EXC_BAD_INSTRUCTION";
535 return "EXC_ARITHMETIC";
537 return "EXC_EMULATION";
539 return "EXC_SOFTWARE";
541 return "EXC_BREAKPOINT";
543 return "EXC_SYSCALL";
544 case EXC_MACH_SYSCALL
:
545 return "EXC_MACH_SYSCALL";
547 return "EXC_RPC_ALERT";
553 return "EXC_RESOURCE";
558 #ifdef EXC_CORPSE_NOTIFY
559 case EXC_CORPSE_NOTIFY
:
560 return "EXC_CORPSE_NOTIFY";
562 #ifdef EXC_CORPSE_VARIANT_BIT
563 case EXC_CORPSE_VARIANT_BIT
:
564 return "EXC_CORPSE_VARIANT_BIT";
572 std::optional
<exception_type_t
>
573 StopInfoMachException::MachException::ExceptionCode(const char *name
) {
574 return llvm::StringSwitch
<std::optional
<exception_type_t
>>(name
)
575 .Case("EXC_BAD_ACCESS", EXC_BAD_ACCESS
)
576 .Case("EXC_BAD_INSTRUCTION", EXC_BAD_INSTRUCTION
)
577 .Case("EXC_ARITHMETIC", EXC_ARITHMETIC
)
578 .Case("EXC_EMULATION", EXC_EMULATION
)
579 .Case("EXC_SOFTWARE", EXC_SOFTWARE
)
580 .Case("EXC_BREAKPOINT", EXC_BREAKPOINT
)
581 .Case("EXC_SYSCALL", EXC_SYSCALL
)
582 .Case("EXC_MACH_SYSCALL", EXC_MACH_SYSCALL
)
583 .Case("EXC_RPC_ALERT", EXC_RPC_ALERT
)
585 .Case("EXC_CRASH", EXC_CRASH
)
587 .Case("EXC_RESOURCE", EXC_RESOURCE
)
589 .Case("EXC_GUARD", EXC_GUARD
)
591 #ifdef EXC_CORPSE_NOTIFY
592 .Case("EXC_CORPSE_NOTIFY", EXC_CORPSE_NOTIFY
)
594 .Default(std::nullopt
);
598 StopInfoSP
StopInfoMachException::CreateStopReasonWithMachException(
599 Thread
&thread
, uint32_t exc_type
, uint32_t exc_data_count
,
600 uint64_t exc_code
, uint64_t exc_sub_code
, uint64_t exc_sub_sub_code
,
601 bool pc_already_adjusted
, bool adjust_pc_if_needed
) {
605 uint32_t pc_decrement
= 0;
606 ExecutionContext
exe_ctx(thread
.shared_from_this());
607 Target
*target
= exe_ctx
.GetTargetPtr();
608 const llvm::Triple::ArchType cpu
=
609 target
? target
->GetArchitecture().GetMachine()
610 : llvm::Triple::UnknownArch
;
613 case 1: // EXC_BAD_ACCESS
614 case 2: // EXC_BAD_INSTRUCTION
615 case 3: // EXC_ARITHMETIC
616 case 4: // EXC_EMULATION
619 case 5: // EXC_SOFTWARE
620 if (exc_code
== 0x10003) // EXC_SOFT_SIGNAL
622 if (exc_sub_code
== 5) {
623 // On MacOSX, a SIGTRAP can signify that a process has called exec,
624 // so we should check with our dynamic loader to verify.
625 ProcessSP
process_sp(thread
.GetProcess());
627 DynamicLoader
*dynamic_loader
= process_sp
->GetDynamicLoader();
628 if (dynamic_loader
&& dynamic_loader
->ProcessDidExec()) {
629 // The program was re-exec'ed
630 return StopInfo::CreateStopReasonWithExec(thread
);
634 return StopInfo::CreateStopReasonWithSignal(thread
, exc_sub_code
);
638 case 6: // EXC_BREAKPOINT
640 bool is_actual_breakpoint
= false;
641 bool is_trace_if_actual_breakpoint_missing
= false;
643 case llvm::Triple::x86
:
644 case llvm::Triple::x86_64
:
645 if (exc_code
== 1) // EXC_I386_SGL
648 // This looks like a plain trap.
649 // Have to check if there is a breakpoint here as well. When you
650 // single-step onto a trap, the single step stops you not to trap.
651 // Since we also do that check below, let's just use that logic.
652 is_actual_breakpoint
= true;
653 is_trace_if_actual_breakpoint_missing
= true;
655 if (StopInfoSP stop_info
=
656 GetStopInfoForHardwareBP(thread
, target
, exc_data_count
,
657 exc_sub_code
, exc_sub_sub_code
))
660 } else if (exc_code
== 2 || // EXC_I386_BPT
661 exc_code
== 3) // EXC_I386_BPTFLT
663 // KDP returns EXC_I386_BPTFLT for trace breakpoints
665 is_trace_if_actual_breakpoint_missing
= true;
667 is_actual_breakpoint
= true;
668 if (!pc_already_adjusted
)
673 case llvm::Triple::arm
:
674 case llvm::Triple::thumb
:
675 if (exc_code
== 0x102) // EXC_ARM_DA_DEBUG
677 // It's a watchpoint, then, if the exc_sub_code indicates a
678 // known/enabled data break address from our watchpoint list.
679 lldb::WatchpointSP wp_sp
;
681 wp_sp
= target
->GetWatchpointList().FindByAddress(
682 (lldb::addr_t
)exc_sub_code
);
683 if (wp_sp
&& wp_sp
->IsEnabled()) {
684 // Debugserver may piggyback the hardware index of the fired
685 // watchpoint in the exception data. Set the hardware index if
687 if (exc_data_count
>= 3)
688 wp_sp
->SetHardwareIndex((uint32_t)exc_sub_sub_code
);
689 return StopInfo::CreateStopReasonWithWatchpointID(thread
,
692 is_actual_breakpoint
= true;
693 is_trace_if_actual_breakpoint_missing
= true;
695 } else if (exc_code
== 1) // EXC_ARM_BREAKPOINT
697 is_actual_breakpoint
= true;
698 is_trace_if_actual_breakpoint_missing
= true;
699 } else if (exc_code
== 0) // FIXME not EXC_ARM_BREAKPOINT but a kernel
700 // is currently returning this so accept it
701 // as indicating a breakpoint until the
704 is_actual_breakpoint
= true;
705 is_trace_if_actual_breakpoint_missing
= true;
709 case llvm::Triple::aarch64_32
:
710 case llvm::Triple::aarch64
: {
711 // xnu describes three things with type EXC_BREAKPOINT:
713 // exc_code 0x102 [EXC_ARM_DA_DEBUG], exc_sub_code addr-of-insn
714 // Watchpoint access. exc_sub_code is the address of the
715 // instruction which trigged the watchpoint trap.
716 // debugserver may add the watchpoint number that was triggered
717 // in exc_sub_sub_code.
719 // exc_code 1 [EXC_ARM_BREAKPOINT], exc_sub_code 0
720 // Instruction step has completed.
722 // exc_code 1 [EXC_ARM_BREAKPOINT], exc_sub_code address-of-instruction
723 // Software breakpoint instruction executed.
725 if (exc_code
== 1 && exc_sub_code
== 0) // EXC_ARM_BREAKPOINT
727 // This is hit when we single instruction step aka MDSCR_EL1 SS bit 0
729 is_actual_breakpoint
= true;
730 is_trace_if_actual_breakpoint_missing
= true;
732 if (thread
.GetTemporaryResumeState() != eStateStepping
) {
734 s
.Printf("CreateStopReasonWithMachException got EXC_BREAKPOINT [1,0] "
735 "indicating trace event, but thread is not tracing, it has "
737 thread
.GetTemporaryResumeState());
738 if (RegisterContextSP regctx
= thread
.GetRegisterContext()) {
739 if (const RegisterInfo
*ri
= regctx
->GetRegisterInfoByName("esr")) {
741 (uint32_t)regctx
->ReadRegisterAsUnsigned(ri
, UINT32_MAX
);
742 if (esr
!= UINT32_MAX
) {
743 s
.Printf(" esr value: 0x%" PRIx32
, esr
);
747 thread
.GetProcess()->DumpPluginHistory(s
);
748 llvm::report_fatal_error(s
.GetData());
751 "CreateStopReasonWithMachException got EXC_BREAKPOINT [1,0] "
752 "indicating trace event, but thread was not doing a step.");
756 if (exc_code
== 0x102) // EXC_ARM_DA_DEBUG
758 // It's a watchpoint, then, if the exc_sub_code indicates a
759 // known/enabled data break address from our watchpoint list.
760 lldb::WatchpointSP wp_sp
;
762 wp_sp
= target
->GetWatchpointList().FindByAddress(
763 (lldb::addr_t
)exc_sub_code
);
764 if (wp_sp
&& wp_sp
->IsEnabled()) {
765 // Debugserver may piggyback the hardware index of the fired
766 // watchpoint in the exception data. Set the hardware index if
768 if (exc_data_count
>= 3)
769 wp_sp
->SetHardwareIndex((uint32_t)exc_sub_sub_code
);
770 return StopInfo::CreateStopReasonWithWatchpointID(thread
,
773 // EXC_ARM_DA_DEBUG seems to be reused for EXC_BREAKPOINT as well as
775 if (thread
.GetTemporaryResumeState() == eStateStepping
)
776 return StopInfo::CreateStopReasonToTrace(thread
);
778 // It looks like exc_sub_code has the 4 bytes of the instruction that
779 // triggered the exception, i.e. our breakpoint opcode
780 is_actual_breakpoint
= exc_code
== 1;
788 if (is_actual_breakpoint
) {
789 RegisterContextSP
reg_ctx_sp(thread
.GetRegisterContext());
790 addr_t pc
= reg_ctx_sp
->GetPC() - pc_decrement
;
792 ProcessSP
process_sp(thread
.CalculateProcess());
794 lldb::BreakpointSiteSP bp_site_sp
;
796 bp_site_sp
= process_sp
->GetBreakpointSiteList().FindByAddress(pc
);
797 if (bp_site_sp
&& bp_site_sp
->IsEnabled()) {
798 // Update the PC if we were asked to do so, but only do so if we find
799 // a breakpoint that we know about cause this could be a trap
800 // instruction in the code
801 if (pc_decrement
> 0 && adjust_pc_if_needed
)
802 reg_ctx_sp
->SetPC(pc
);
804 // If the breakpoint is for this thread, then we'll report the hit,
805 // but if it is for another thread, we can just report no reason. We
806 // don't need to worry about stepping over the breakpoint here, that
807 // will be taken care of when the thread resumes and notices that
808 // there's a breakpoint under the pc. If we have an operating system
809 // plug-in, we might have set a thread specific breakpoint using the
810 // operating system thread ID, so we can't make any assumptions about
811 // the thread ID so we must always report the breakpoint regardless
813 if (bp_site_sp
->ValidForThisThread(thread
) ||
814 thread
.GetProcess()->GetOperatingSystem() != nullptr)
815 return StopInfo::CreateStopReasonWithBreakpointSiteID(
816 thread
, bp_site_sp
->GetID());
817 else if (is_trace_if_actual_breakpoint_missing
)
818 return StopInfo::CreateStopReasonToTrace(thread
);
823 // Don't call this a trace if we weren't single stepping this thread.
824 if (is_trace_if_actual_breakpoint_missing
&&
825 thread
.GetTemporaryResumeState() == eStateStepping
) {
826 return StopInfo::CreateStopReasonToTrace(thread
);
831 case 7: // EXC_SYSCALL
832 case 8: // EXC_MACH_SYSCALL
833 case 9: // EXC_RPC_ALERT
834 case 10: // EXC_CRASH
838 return StopInfoSP(new StopInfoMachException(thread
, exc_type
, exc_data_count
,
839 exc_code
, exc_sub_code
));