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 ");
259 return "invalid stop reason!";
263 const llvm::Triple::ArchType cpu =
265 : llvm::Triple::UnknownArch;
267 const char *exc_desc =
nullptr;
268 const char *code_label =
"code";
269 const char *code_desc =
nullptr;
270 const char *subcode_label =
"subcode";
271 const char *subcode_desc =
nullptr;
273#if defined(__APPLE__)
274 char code_desc_buf[32];
275 char subcode_desc_buf[32];
280 exc_desc =
"EXC_BAD_ACCESS";
281 subcode_label =
"address";
283 case llvm::Triple::x86:
284 case llvm::Triple::x86_64:
287 code_desc =
"EXC_I386_GPFLT";
292 case llvm::Triple::arm:
293 case llvm::Triple::thumb:
296 code_desc =
"EXC_ARM_DA_ALIGN";
299 code_desc =
"EXC_ARM_DA_DEBUG";
304 case llvm::Triple::aarch64:
317 exc_desc =
"EXC_BAD_INSTRUCTION";
319 case llvm::Triple::x86:
320 case llvm::Triple::x86_64:
322 code_desc =
"EXC_I386_INVOP";
325 case llvm::Triple::arm:
326 case llvm::Triple::thumb:
328 code_desc =
"EXC_ARM_UNDEFINED";
337 exc_desc =
"EXC_ARITHMETIC";
339 case llvm::Triple::x86:
340 case llvm::Triple::x86_64:
343 code_desc =
"EXC_I386_DIV";
346 code_desc =
"EXC_I386_INTO";
349 code_desc =
"EXC_I386_NOEXT";
352 code_desc =
"EXC_I386_EXTOVR";
355 code_desc =
"EXC_I386_EXTERR";
358 code_desc =
"EXC_I386_EMERR";
361 code_desc =
"EXC_I386_BOUND";
364 code_desc =
"EXC_I386_SSEEXTERR";
375 exc_desc =
"EXC_EMULATION";
379 exc_desc =
"EXC_SOFTWARE";
381 subcode_desc =
"EXC_SOFT_SIGNAL";
382 subcode_label =
"signo";
388 exc_desc =
"EXC_BREAKPOINT";
390 case llvm::Triple::x86:
391 case llvm::Triple::x86_64:
394 code_desc =
"EXC_I386_SGL";
397 code_desc =
"EXC_I386_BPT";
402 case llvm::Triple::arm:
403 case llvm::Triple::thumb:
406 code_desc =
"EXC_ARM_DA_ALIGN";
409 code_desc =
"EXC_ARM_DA_DEBUG";
412 code_desc =
"EXC_ARM_BREAKPOINT";
417 code_desc =
"EXC_ARM_BREAKPOINT";
422 case llvm::Triple::aarch64:
433 exc_desc =
"EXC_SYSCALL";
437 exc_desc =
"EXC_MACH_SYSCALL";
441 exc_desc =
"EXC_RPC_ALERT";
445 exc_desc =
"EXC_CRASH";
448 exc_desc =
"EXC_RESOURCE";
449#if defined(__APPLE__)
451 int resource_type = EXC_RESOURCE_DECODE_RESOURCE_TYPE(
m_exc_code);
453 code_label =
"limit";
454 code_desc = code_desc_buf;
455 subcode_label =
"observed";
456 subcode_desc = subcode_desc_buf;
458 switch (resource_type) {
459 case RESOURCE_TYPE_CPU:
461 "EXC_RESOURCE (RESOURCE_TYPE_CPU: CPU usage monitor tripped)";
462 snprintf(code_desc_buf,
sizeof(code_desc_buf),
"%d%%",
463 (
int)EXC_RESOURCE_CPUMONITOR_DECODE_PERCENTAGE(
m_exc_code));
464 snprintf(subcode_desc_buf,
sizeof(subcode_desc_buf),
"%d%%",
465 (
int)EXC_RESOURCE_CPUMONITOR_DECODE_PERCENTAGE_OBSERVED(
468 case RESOURCE_TYPE_WAKEUPS:
469 exc_desc =
"EXC_RESOURCE (RESOURCE_TYPE_WAKEUPS: idle wakeups monitor "
472 code_desc_buf,
sizeof(code_desc_buf),
"%d w/s",
473 (
int)EXC_RESOURCE_CPUMONITOR_DECODE_WAKEUPS_PERMITTED(
m_exc_code));
474 snprintf(subcode_desc_buf,
sizeof(subcode_desc_buf),
"%d w/s",
475 (
int)EXC_RESOURCE_CPUMONITOR_DECODE_WAKEUPS_OBSERVED(
478 case RESOURCE_TYPE_MEMORY:
479 exc_desc =
"EXC_RESOURCE (RESOURCE_TYPE_MEMORY: high watermark memory "
481 snprintf(code_desc_buf,
sizeof(code_desc_buf),
"%d MB",
482 (
int)EXC_RESOURCE_HWM_DECODE_LIMIT(
m_exc_code));
483 subcode_desc =
nullptr;
484 subcode_label =
nullptr;
486#if defined(RESOURCE_TYPE_IO)
488 case RESOURCE_TYPE_IO:
489 exc_desc =
"EXC_RESOURCE RESOURCE_TYPE_IO";
490 snprintf(code_desc_buf,
sizeof(code_desc_buf),
"%d MB",
491 (
int)EXC_RESOURCE_IO_DECODE_LIMIT(
m_exc_code));
492 snprintf(subcode_desc_buf,
sizeof(subcode_desc_buf),
"%d MB",
504 exc_desc =
"EXC_GUARD";
517 strm.
Printf(
" (%s=%s", code_label, code_desc);
523 if (subcode_label && subcode_desc)
524 strm.
Printf(
", %s=%s", subcode_label, subcode_desc);
525 else if (subcode_label)
536#if defined(__APPLE__)
538StopInfoMachException::MachException::Name(exception_type_t exc_type) {
541 return "EXC_BAD_ACCESS";
542 case EXC_BAD_INSTRUCTION:
543 return "EXC_BAD_INSTRUCTION";
545 return "EXC_ARITHMETIC";
547 return "EXC_EMULATION";
549 return "EXC_SOFTWARE";
551 return "EXC_BREAKPOINT";
553 return "EXC_SYSCALL";
554 case EXC_MACH_SYSCALL:
555 return "EXC_MACH_SYSCALL";
557 return "EXC_RPC_ALERT";
563 return "EXC_RESOURCE";
568#ifdef EXC_CORPSE_NOTIFY
569 case EXC_CORPSE_NOTIFY:
570 return "EXC_CORPSE_NOTIFY";
572#ifdef EXC_CORPSE_VARIANT_BIT
573 case EXC_CORPSE_VARIANT_BIT:
574 return "EXC_CORPSE_VARIANT_BIT";
582std::optional<exception_type_t>
583StopInfoMachException::MachException::ExceptionCode(
const char *name) {
584 return llvm::StringSwitch<std::optional<exception_type_t>>(name)
585 .Case(
"EXC_BAD_ACCESS", EXC_BAD_ACCESS)
586 .Case(
"EXC_BAD_INSTRUCTION", EXC_BAD_INSTRUCTION)
587 .Case(
"EXC_ARITHMETIC", EXC_ARITHMETIC)
588 .Case(
"EXC_EMULATION", EXC_EMULATION)
589 .Case(
"EXC_SOFTWARE", EXC_SOFTWARE)
590 .Case(
"EXC_BREAKPOINT", EXC_BREAKPOINT)
591 .Case(
"EXC_SYSCALL", EXC_SYSCALL)
592 .Case(
"EXC_MACH_SYSCALL", EXC_MACH_SYSCALL)
593 .Case(
"EXC_RPC_ALERT", EXC_RPC_ALERT)
595 .Case(
"EXC_CRASH", EXC_CRASH)
597 .Case(
"EXC_RESOURCE", EXC_RESOURCE)
599 .Case(
"EXC_GUARD", EXC_GUARD)
601#ifdef EXC_CORPSE_NOTIFY
602 .Case(
"EXC_CORPSE_NOTIFY", EXC_CORPSE_NOTIFY)
604 .Default(std::nullopt);
609 Thread &thread, uint32_t exc_type, uint32_t exc_data_count,
610 uint64_t exc_code, uint64_t exc_sub_code, uint64_t exc_sub_sub_code,
611 bool pc_already_adjusted,
bool adjust_pc_if_needed) {
615 bool not_stepping_but_got_singlestep_exception =
false;
616 uint32_t pc_decrement = 0;
619 const llvm::Triple::ArchType cpu =
621 : llvm::Triple::UnknownArch;
623 ProcessSP process_sp(thread.GetProcess());
638 process_sp->GetBreakpointSiteList().FindByAddress(
pc);
639 if (bp_site_sp && bp_site_sp->IsEnabled())
640 thread.SetThreadStoppedAtUnexecutedBP(
pc);
650 if (exc_code == 0x10003)
652 if (exc_sub_code == 5) {
655 ProcessSP process_sp(thread.GetProcess());
657 DynamicLoader *dynamic_loader = process_sp->GetDynamicLoader();
716 bool stopped_by_hitting_breakpoint =
false;
717 bool stopped_by_completing_stepi =
false;
718 bool stopped_watchpoint =
false;
719 std::optional<addr_t> address;
723 if (exc_sub_code == 0) {
724 stopped_by_completing_stepi =
true;
728 stopped_by_hitting_breakpoint =
true;
729 stopped_watchpoint =
true;
730 address = exc_sub_code;
736 if (exc_sub_code == 0)
737 stopped_by_hitting_breakpoint =
true;
739 stopped_by_hitting_breakpoint =
true;
741 if (!pc_already_adjusted)
748 stopped_by_completing_stepi =
true;
751 if (exc_code == 0x102 && exc_sub_code != 0) {
752 if (cpu == llvm::Triple::arm || cpu == llvm::Triple::thumb) {
753 stopped_by_hitting_breakpoint =
true;
754 stopped_by_completing_stepi =
true;
756 stopped_watchpoint =
true;
757 address = exc_sub_code;
765 if (stopped_by_hitting_breakpoint) {
766 addr_t pc = reg_ctx_sp->GetPC() - pc_decrement;
770 process_sp->GetBreakpointSiteList().FindByAddress(*address);
771 if (!bp_site_sp && reg_ctx_sp) {
772 bp_site_sp = process_sp->GetBreakpointSiteList().FindByAddress(
pc);
774 if (bp_site_sp && bp_site_sp->IsEnabled()) {
777 thread.SetThreadHitBreakpointSite();
779 if (bp_site_sp->ValidForThisThread(thread)) {
783 if (pc_decrement > 0 && adjust_pc_if_needed && reg_ctx_sp)
784 reg_ctx_sp->SetPC(
pc);
787 thread, bp_site_sp->GetID());
797 if (stopped_watchpoint && address) {
799 target->
GetProcessSP()->GetWatchpointResourceList().FindByAddress(
801 if (wp_rsrc_sp && wp_rsrc_sp->GetNumberOfConstituents() > 0) {
803 thread, wp_rsrc_sp->GetConstituentAtIndex(0)->GetID());
809 if (stopped_by_completing_stepi) {
811 not_stepping_but_got_singlestep_exception =
true;
825 return std::make_shared<StopInfoMachException>(
826 thread, exc_type, exc_data_count, exc_code, exc_sub_code,
827 not_stepping_but_got_singlestep_exception);
848 std::optional<addr_t> prev_pc = thread.GetPreviousFrameZeroPC();
849 if (!reg_ctx_sp || !prev_pc)
853 if (*prev_pc != reg_ctx_sp->GetPC())
857 ProcessSP process_sp = thread.GetProcess();
858 if (process_sp->GetWatchpointResourceList().GetSize()) {
860 "Thread stopped with insn-step completed mach exception but "
861 "thread was not stepping; there is a hardware watchpoint set.");
866 auto &bp_site_list = process_sp->GetBreakpointSiteList();
867 for (
auto &site : bp_site_list.Sites()) {
868 if (site->IsHardware() && site->IsEnabled()) {
870 "Thread stopped with insn-step completed mach exception but "
871 "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
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()
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
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),...