15#include <kern/exc_resource.h>
46static std::optional<PtrauthInstructionInfo>
49 const char *plugin_name =
nullptr;
50 const char *flavor =
nullptr;
51 const char *cpu =
nullptr;
52 const char *features =
nullptr;
54 const bool prefer_file_cache =
true;
57 target, range_bounds, prefer_file_cache);
81 const bool bad_access =
87 if (bad_access && tag_fault && has_fault_addr)
101 const uint64_t bad_address = *fault_address;
104 strm.
Printf(
"EXC_ARM_MTE_TAG_FAULT (code=%" PRIu64
", address=0x%" PRIx64
111 "Note: MTE tag mismatch detected: pointer tag=%d, address=0x%" PRIx64,
112 tag, canonical_addr);
119 bool IsBreakpoint =
m_value == 6;
120 bool IsBadAccess =
m_value == 1;
121 if (!IsBreakpoint && !IsBadAccess)
130 StackFrameSP current_frame = thread.GetStackFrameAtIndex(0);
139 const bool ptrauth_enabled_target =
141 if (!ptrauth_enabled_target)
146 auto emit_ptrauth_prologue = [&](uint64_t at_address) {
147 strm.
Printf(
"EXC_BAD_ACCESS (code=%" PRIu64
", address=0x%" PRIx64
")\n",
149 strm.
Printf(
"Note: Possible pointer authentication failure detected.\n");
153 assert(abi_sp &&
"Missing ABI info");
157 Address current_address = current_frame->GetFrameCodeAddress();
169 uint64_t fixed_bad_address = abi_sp->FixCodeAddress(bad_address);
174 auto brk_ptrauth_info =
176 if (brk_ptrauth_info && brk_ptrauth_info->IsAuthenticated) {
177 emit_ptrauth_prologue(bad_address);
178 strm.
Printf(
"Found value that failed to authenticate ");
186 assert(IsBadAccess &&
"Handle EXC_BAD_ACCESS only after this point");
197 uint64_t fixed_bad_address = abi_sp->FixCodeAddress(bad_address);
204 if (bad_address != current_pc && fixed_bad_address != current_pc) {
207 if (ptrauth_info && ptrauth_info->IsAuthenticated && ptrauth_info->IsLoad) {
208 emit_ptrauth_prologue(bad_address);
209 strm.
Printf(
"Found authenticated load instruction ");
226 if (bad_address != current_pc && fixed_bad_address == current_pc) {
227 if (
StackFrameSP parent_frame = thread.GetStackFrameAtIndex(1)) {
229 parent_frame->GetFrameCodeAddress().GetLoadAddress(&target);
234 auto blr_ptrauth_info =
236 if (blr_ptrauth_info && blr_ptrauth_info->IsAuthenticated &&
237 blr_ptrauth_info->DoesBranch) {
238 emit_ptrauth_prologue(bad_address);
239 strm.
Printf(
"Found authenticated indirect branch ");
256 const bool is_bad_access = (
m_value == 1);
257 const bool is_cpa2_fault = (
m_exc_code == 0x108);
262 if (!is_bad_access || !is_cpa2_fault || !has_fault_addr)
268 strm.
Printf(
"EXC_ARM_CPA_FAIL (code=%" PRIu64
", address=0x%" PRIx64
")\n",
270 strm.
Printf(
"Note: Checked Pointer Arithmetic failure detected.\n");
280 return "invalid stop reason!";
284 const llvm::Triple::ArchType cpu =
286 : llvm::Triple::UnknownArch;
288 const char *exc_desc =
nullptr;
289 const char *code_label =
"code";
290 const char *code_desc =
nullptr;
291 const char *subcode_label =
"subcode";
292 const char *subcode_desc =
nullptr;
294#if defined(__APPLE__)
295 char code_desc_buf[32];
296 char subcode_desc_buf[32];
301 exc_desc =
"EXC_BAD_ACCESS";
302 subcode_label =
"address";
304 case llvm::Triple::x86:
305 case llvm::Triple::x86_64:
308 code_desc =
"EXC_I386_GPFLT";
313 case llvm::Triple::arm:
314 case llvm::Triple::thumb:
317 code_desc =
"EXC_ARM_DA_ALIGN";
320 code_desc =
"EXC_ARM_DA_DEBUG";
325 case llvm::Triple::aarch64:
340 exc_desc =
"EXC_BAD_INSTRUCTION";
342 case llvm::Triple::x86:
343 case llvm::Triple::x86_64:
345 code_desc =
"EXC_I386_INVOP";
348 case llvm::Triple::arm:
349 case llvm::Triple::thumb:
351 code_desc =
"EXC_ARM_UNDEFINED";
360 exc_desc =
"EXC_ARITHMETIC";
362 case llvm::Triple::x86:
363 case llvm::Triple::x86_64:
366 code_desc =
"EXC_I386_DIV";
369 code_desc =
"EXC_I386_INTO";
372 code_desc =
"EXC_I386_NOEXT";
375 code_desc =
"EXC_I386_EXTOVR";
378 code_desc =
"EXC_I386_EXTERR";
381 code_desc =
"EXC_I386_EMERR";
384 code_desc =
"EXC_I386_BOUND";
387 code_desc =
"EXC_I386_SSEEXTERR";
398 exc_desc =
"EXC_EMULATION";
402 exc_desc =
"EXC_SOFTWARE";
404 subcode_desc =
"EXC_SOFT_SIGNAL";
405 subcode_label =
"signo";
411 exc_desc =
"EXC_BREAKPOINT";
413 case llvm::Triple::x86:
414 case llvm::Triple::x86_64:
417 code_desc =
"EXC_I386_SGL";
420 code_desc =
"EXC_I386_BPT";
425 case llvm::Triple::arm:
426 case llvm::Triple::thumb:
429 code_desc =
"EXC_ARM_DA_ALIGN";
432 code_desc =
"EXC_ARM_DA_DEBUG";
435 code_desc =
"EXC_ARM_BREAKPOINT";
440 code_desc =
"EXC_ARM_BREAKPOINT";
445 case llvm::Triple::aarch64:
456 exc_desc =
"EXC_SYSCALL";
460 exc_desc =
"EXC_MACH_SYSCALL";
464 exc_desc =
"EXC_RPC_ALERT";
468 exc_desc =
"EXC_CRASH";
471 exc_desc =
"EXC_RESOURCE";
472#if defined(__APPLE__)
474 int resource_type = EXC_RESOURCE_DECODE_RESOURCE_TYPE(
m_exc_code);
476 code_label =
"limit";
477 code_desc = code_desc_buf;
478 subcode_label =
"observed";
479 subcode_desc = subcode_desc_buf;
481 switch (resource_type) {
482 case RESOURCE_TYPE_CPU:
484 "EXC_RESOURCE (RESOURCE_TYPE_CPU: CPU usage monitor tripped)";
485 snprintf(code_desc_buf,
sizeof(code_desc_buf),
"%d%%",
486 (
int)EXC_RESOURCE_CPUMONITOR_DECODE_PERCENTAGE(
m_exc_code));
487 snprintf(subcode_desc_buf,
sizeof(subcode_desc_buf),
"%d%%",
488 (
int)EXC_RESOURCE_CPUMONITOR_DECODE_PERCENTAGE_OBSERVED(
491 case RESOURCE_TYPE_WAKEUPS:
492 exc_desc =
"EXC_RESOURCE (RESOURCE_TYPE_WAKEUPS: idle wakeups monitor "
495 code_desc_buf,
sizeof(code_desc_buf),
"%d w/s",
496 (
int)EXC_RESOURCE_CPUMONITOR_DECODE_WAKEUPS_PERMITTED(
m_exc_code));
497 snprintf(subcode_desc_buf,
sizeof(subcode_desc_buf),
"%d w/s",
498 (
int)EXC_RESOURCE_CPUMONITOR_DECODE_WAKEUPS_OBSERVED(
501 case RESOURCE_TYPE_MEMORY:
502 exc_desc =
"EXC_RESOURCE (RESOURCE_TYPE_MEMORY: high watermark memory "
504 snprintf(code_desc_buf,
sizeof(code_desc_buf),
"%d MB",
505 (
int)EXC_RESOURCE_HWM_DECODE_LIMIT(
m_exc_code));
506 subcode_desc =
nullptr;
507 subcode_label =
nullptr;
509#if defined(RESOURCE_TYPE_IO)
511 case RESOURCE_TYPE_IO:
512 exc_desc =
"EXC_RESOURCE RESOURCE_TYPE_IO";
513 snprintf(code_desc_buf,
sizeof(code_desc_buf),
"%d MB",
514 (
int)EXC_RESOURCE_IO_DECODE_LIMIT(
m_exc_code));
515 snprintf(subcode_desc_buf,
sizeof(subcode_desc_buf),
"%d MB",
527 exc_desc =
"EXC_GUARD";
540 strm.
Printf(
" (%s=%s", code_label, code_desc);
546 if (subcode_label && subcode_desc)
547 strm.
Printf(
", %s=%s", subcode_label, subcode_desc);
548 else if (subcode_label)
559#if defined(__APPLE__)
561StopInfoMachException::MachException::Name(exception_type_t exc_type) {
564 return "EXC_BAD_ACCESS";
565 case EXC_BAD_INSTRUCTION:
566 return "EXC_BAD_INSTRUCTION";
568 return "EXC_ARITHMETIC";
570 return "EXC_EMULATION";
572 return "EXC_SOFTWARE";
574 return "EXC_BREAKPOINT";
576 return "EXC_SYSCALL";
577 case EXC_MACH_SYSCALL:
578 return "EXC_MACH_SYSCALL";
580 return "EXC_RPC_ALERT";
586 return "EXC_RESOURCE";
591#ifdef EXC_CORPSE_NOTIFY
592 case EXC_CORPSE_NOTIFY:
593 return "EXC_CORPSE_NOTIFY";
595#ifdef EXC_CORPSE_VARIANT_BIT
596 case EXC_CORPSE_VARIANT_BIT:
597 return "EXC_CORPSE_VARIANT_BIT";
605std::optional<exception_type_t>
606StopInfoMachException::MachException::ExceptionCode(
const char *name) {
607 return llvm::StringSwitch<std::optional<exception_type_t>>(name)
608 .Case(
"EXC_BAD_ACCESS", EXC_BAD_ACCESS)
609 .Case(
"EXC_BAD_INSTRUCTION", EXC_BAD_INSTRUCTION)
610 .Case(
"EXC_ARITHMETIC", EXC_ARITHMETIC)
611 .Case(
"EXC_EMULATION", EXC_EMULATION)
612 .Case(
"EXC_SOFTWARE", EXC_SOFTWARE)
613 .Case(
"EXC_BREAKPOINT", EXC_BREAKPOINT)
614 .Case(
"EXC_SYSCALL", EXC_SYSCALL)
615 .Case(
"EXC_MACH_SYSCALL", EXC_MACH_SYSCALL)
616 .Case(
"EXC_RPC_ALERT", EXC_RPC_ALERT)
618 .Case(
"EXC_CRASH", EXC_CRASH)
620 .Case(
"EXC_RESOURCE", EXC_RESOURCE)
622 .Case(
"EXC_GUARD", EXC_GUARD)
624#ifdef EXC_CORPSE_NOTIFY
625 .Case(
"EXC_CORPSE_NOTIFY", EXC_CORPSE_NOTIFY)
627 .Default(std::nullopt);
632 Thread &thread, uint32_t exc_type, uint32_t exc_data_count,
633 uint64_t exc_code, uint64_t exc_sub_code, uint64_t exc_sub_sub_code,
634 bool pc_already_adjusted,
bool adjust_pc_if_needed) {
638 bool not_stepping_but_got_singlestep_exception =
false;
639 uint32_t pc_decrement = 0;
642 const llvm::Triple::ArchType cpu =
644 : llvm::Triple::UnknownArch;
646 ProcessSP process_sp(thread.GetProcess());
661 process_sp->GetBreakpointSiteList().FindByAddress(
pc);
662 if (bp_site_sp && process_sp->IsBreakpointSitePhysicallyEnabled(*bp_site_sp))
663 thread.SetThreadStoppedAtUnexecutedBP(
pc);
673 if (exc_code == 0x10003)
675 if (exc_sub_code == 5) {
678 ProcessSP process_sp(thread.GetProcess());
680 DynamicLoader *dynamic_loader = process_sp->GetDynamicLoader();
739 bool stopped_by_hitting_breakpoint =
false;
740 bool stopped_by_completing_stepi =
false;
741 bool stopped_watchpoint =
false;
742 std::optional<addr_t> address;
746 if (exc_sub_code == 0) {
747 stopped_by_completing_stepi =
true;
751 stopped_by_hitting_breakpoint =
true;
752 stopped_watchpoint =
true;
753 address = exc_sub_code;
759 if (exc_sub_code == 0)
760 stopped_by_hitting_breakpoint =
true;
762 stopped_by_hitting_breakpoint =
true;
764 if (!pc_already_adjusted)
771 stopped_by_completing_stepi =
true;
774 if (exc_code == 0x102 && exc_sub_code != 0) {
775 if (cpu == llvm::Triple::arm || cpu == llvm::Triple::thumb) {
776 stopped_by_hitting_breakpoint =
true;
777 stopped_by_completing_stepi =
true;
779 stopped_watchpoint =
true;
780 address = exc_sub_code;
788 if (stopped_by_hitting_breakpoint) {
789 addr_t pc = reg_ctx_sp->GetPC() - pc_decrement;
793 process_sp->GetBreakpointSiteList().FindByAddress(*address);
794 if (!bp_site_sp && reg_ctx_sp) {
795 bp_site_sp = process_sp->GetBreakpointSiteList().FindByAddress(
pc);
798 process_sp->IsBreakpointSitePhysicallyEnabled(*bp_site_sp)) {
801 thread.SetThreadHitBreakpointSite();
803 if (bp_site_sp->ValidForThisThread(thread)) {
807 if (pc_decrement > 0 && adjust_pc_if_needed && reg_ctx_sp)
808 reg_ctx_sp->SetPC(
pc);
811 thread, bp_site_sp->GetID());
821 if (stopped_watchpoint && address) {
823 target->
GetProcessSP()->GetWatchpointResourceList().FindByAddress(
825 if (wp_rsrc_sp && wp_rsrc_sp->GetNumberOfConstituents() > 0) {
827 thread, wp_rsrc_sp->GetConstituentAtIndex(0)->GetID());
833 if (stopped_by_completing_stepi) {
835 not_stepping_but_got_singlestep_exception =
true;
849 return std::make_shared<StopInfoMachException>(
850 thread, exc_type, exc_data_count, exc_code, exc_sub_code,
851 not_stepping_but_got_singlestep_exception);
882 std::optional<addr_t> prev_pc = thread.GetPreviousFrameZeroPC();
883 if (!reg_ctx_sp || !prev_pc)
887 if (*prev_pc != reg_ctx_sp->GetPC())
891 ProcessSP process_sp = thread.GetProcess();
892 if (process_sp->GetWatchpointResourceList().GetSize()) {
894 "Thread stopped with insn-step completed mach exception but "
895 "thread was not stepping; there is a hardware watchpoint set.");
900 auto &bp_site_list = process_sp->GetBreakpointSiteList();
901 for (
auto &site : bp_site_list.Sites()) {
902 if (site->IsHardware() &&
903 process_sp->IsBreakpointSitePhysicallyEnabled(*site)) {
905 "Thread stopped with insn-step completed mach exception but "
906 "thread was not stepping; there is a hardware breakpoint set.");
#define LLDB_LOGF(log,...)
static std::optional< PtrauthInstructionInfo > GetPtrauthInstructionInfo(Target &target, const ArchSpec &arch, const Address &at_addr)
Get any pointer-authentication related information about the instruction at address at_addr.
static constexpr uint8_t g_mte_tag_shift
static constexpr addr_t g_mte_tag_mask
static void DescribeAddressBriefly(Stream &strm, const Address &addr, Target &target)
Describe the load address of addr using the format filename:line:col.
A section + offset based address range class.
A section + offset based address class.
lldb::addr_t GetLoadAddress(Target *target) const
Get the load address.
bool GetDescription(Stream &s, Target &target, lldb::DescriptionLevel level) const
Write a description of this object to a Stream.
An architecture specification class.
llvm::Triple::ArchType GetMachine() const
Returns a machine family for the current architecture.
static lldb::DisassemblerSP DisassembleRange(const ArchSpec &arch, const char *plugin_name, const char *flavor, const char *cpu, const char *features, Target &target, llvm::ArrayRef< AddressRange > disasm_ranges, bool force_live_memory=false)
A plug-in interface definition class for dynamic loaders.
virtual bool ProcessDidExec()
Helper function that can be used to detect when a process has called exec and is now a new and differ...
"lldb/Target/ExecutionContext.h" A class that contains an execution context.
bool HasThreadScope() const
Returns true the ExecutionContext object contains a valid target, process, and thread.
bool HasProcessScope() const
Returns true the ExecutionContext object contains a valid target and process.
Target * GetTargetPtr() const
Returns a pointer to the target object.
bool HasTargetScope() const
Returns true the ExecutionContext object contains a valid target.
Process * GetProcessPtr() const
Returns a pointer to the process object.
RegisterContext * GetRegisterContext() const
Thread * GetThreadPtr() const
Returns a pointer to the thread object.
lldb::InstructionSP GetInstructionAtIndex(size_t idx) const
A plug-in interface definition class for debugging a process.
const lldb::ABISP & GetABI()
const RegisterInfo * GetRegisterInfoByName(llvm::StringRef reg_name, uint32_t start_idx=0)
virtual bool ReadRegister(const RegisterInfo *reg_info, RegisterValue ®_value)=0
uint64_t GetAsUInt64(uint64_t fail_value=UINT64_MAX, bool *success_ptr=nullptr) const
void PerformAction(Event *event_ptr) override
Allow this plugin to respond to stop events to enable skip-over-trap behaviour on AArch64.
const char * GetDescription() override
bool DeterminePtrauthFailure(ExecutionContext &exe_ctx)
Determine the pointer-authentication related failure that caused this exception.
bool WasContinueInterrupted(Thread &thread) override
A Continue operation can result in a false stop event before any execution has happened.
std::optional< lldb::addr_t > GetTagFaultAddress() const
uint32_t m_exc_data_count
bool m_not_stepping_but_got_singlestep_exception
static lldb::StopInfoSP CreateStopReasonWithMachException(Thread &thread, uint32_t exc_type, uint32_t exc_data_count, uint64_t exc_code, uint64_t exc_sub_code, uint64_t exc_sub_sub_code, bool pc_already_adjusted=true, bool adjust_pc_if_needed=false)
bool DetermineTagMismatch()
bool DetermineCPA2Failure()
std::string m_description
uint64_t GetValue() const
static lldb::StopInfoSP CreateStopReasonToTrace(Thread &thread)
static lldb::StopInfoSP CreateStopReasonWithSignal(Thread &thread, int signo, const char *description=nullptr, std::optional< int > code=std::nullopt)
static lldb::StopInfoSP CreateStopReasonWithWatchpointID(Thread &thread, lldb::break_id_t watch_id, bool silently_continue=false)
static lldb::StopInfoSP CreateStopReasonWithBreakpointSiteID(Thread &thread, lldb::break_id_t break_id)
static lldb::StopInfoSP CreateStopReasonWithExec(Thread &thread)
lldb::ThreadWP m_thread_wp
void SkipOverTrapInstruction()
llvm::StringRef GetString() const
A stream class that can stream formatted output to a file.
size_t Printf(const char *format,...) __attribute__((format(printf
Output printf formatted output to the stream.
size_t PutCString(llvm::StringRef cstr)
Output a C string to the stream.
const lldb::ProcessSP & GetProcessSP() const
bool ResolveLoadAddress(lldb::addr_t load_addr, Address &so_addr, uint32_t stop_id=SectionLoadHistory::eStopIDNow, bool allow_section_end=false)
const ArchSpec & GetArchitecture() const
A class that represents a running process on the host machine.
Log * GetLog(Cat mask)
Retrieve the Log object for the channel associated with the given log enum.
std::shared_ptr< lldb_private::ABI > ABISP
std::shared_ptr< lldb_private::StackFrame > StackFrameSP
std::shared_ptr< lldb_private::BreakpointSite > BreakpointSiteSP
@ eStateStepping
Process or thread is in the process of stepping and can not be examined.
std::shared_ptr< lldb_private::Instruction > InstructionSP
std::shared_ptr< lldb_private::Process > ProcessSP
std::shared_ptr< lldb_private::Disassembler > DisassemblerSP
std::shared_ptr< lldb_private::WatchpointResource > WatchpointResourceSP
std::shared_ptr< lldb_private::StopInfo > StopInfoSP
std::shared_ptr< lldb_private::RegisterContext > RegisterContextSP
Information about a pointer-authentication related instruction.
Every register is described in detail including its name, alternate name (optional),...