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 bool IsBreakpoint =
m_value == 6;
82 bool IsBadAccess =
m_value == 1;
83 if (!IsBreakpoint && !IsBadAccess)
101 const bool ptrauth_enabled_target =
103 if (!ptrauth_enabled_target)
108 auto emit_ptrauth_prologue = [&](uint64_t at_address) {
109 strm.
Printf(
"EXC_BAD_ACCESS (code=%" PRIu64
", address=0x%" PRIx64
")\n",
111 strm.
Printf(
"Note: Possible pointer authentication failure detected.\n");
115 assert(abi_sp &&
"Missing ABI info");
119 Address current_address = current_frame->GetFrameCodeAddress();
131 uint64_t fixed_bad_address = abi_sp->FixCodeAddress(bad_address);
136 auto brk_ptrauth_info =
138 if (brk_ptrauth_info && brk_ptrauth_info->IsAuthenticated) {
139 emit_ptrauth_prologue(bad_address);
140 strm.
Printf(
"Found value that failed to authenticate ");
148 assert(IsBadAccess &&
"Handle EXC_BAD_ACCESS only after this point");
159 uint64_t fixed_bad_address = abi_sp->FixCodeAddress(bad_address);
166 if (bad_address != current_pc && fixed_bad_address != current_pc) {
169 if (ptrauth_info && ptrauth_info->IsAuthenticated && ptrauth_info->IsLoad) {
170 emit_ptrauth_prologue(bad_address);
171 strm.
Printf(
"Found authenticated load instruction ");
188 if (bad_address != current_pc && fixed_bad_address == current_pc) {
191 parent_frame->GetFrameCodeAddress().GetLoadAddress(&target);
196 auto blr_ptrauth_info =
198 if (blr_ptrauth_info && blr_ptrauth_info->IsAuthenticated &&
199 blr_ptrauth_info->DoesBranch) {
200 emit_ptrauth_prologue(bad_address);
201 strm.
Printf(
"Found authenticated indirect branch ");
221 return "invalid stop reason!";
225 const llvm::Triple::ArchType cpu =
227 : llvm::Triple::UnknownArch;
229 const char *exc_desc =
nullptr;
230 const char *code_label =
"code";
231 const char *code_desc =
nullptr;
232 const char *subcode_label =
"subcode";
233 const char *subcode_desc =
nullptr;
235#if defined(__APPLE__)
236 char code_desc_buf[32];
237 char subcode_desc_buf[32];
242 exc_desc =
"EXC_BAD_ACCESS";
243 subcode_label =
"address";
245 case llvm::Triple::x86:
246 case llvm::Triple::x86_64:
249 code_desc =
"EXC_I386_GPFLT";
254 case llvm::Triple::arm:
255 case llvm::Triple::thumb:
258 code_desc =
"EXC_ARM_DA_ALIGN";
261 code_desc =
"EXC_ARM_DA_DEBUG";
266 case llvm::Triple::aarch64:
277 exc_desc =
"EXC_BAD_INSTRUCTION";
279 case llvm::Triple::x86:
280 case llvm::Triple::x86_64:
282 code_desc =
"EXC_I386_INVOP";
285 case llvm::Triple::arm:
286 case llvm::Triple::thumb:
288 code_desc =
"EXC_ARM_UNDEFINED";
297 exc_desc =
"EXC_ARITHMETIC";
299 case llvm::Triple::x86:
300 case llvm::Triple::x86_64:
303 code_desc =
"EXC_I386_DIV";
306 code_desc =
"EXC_I386_INTO";
309 code_desc =
"EXC_I386_NOEXT";
312 code_desc =
"EXC_I386_EXTOVR";
315 code_desc =
"EXC_I386_EXTERR";
318 code_desc =
"EXC_I386_EMERR";
321 code_desc =
"EXC_I386_BOUND";
324 code_desc =
"EXC_I386_SSEEXTERR";
335 exc_desc =
"EXC_EMULATION";
339 exc_desc =
"EXC_SOFTWARE";
341 subcode_desc =
"EXC_SOFT_SIGNAL";
342 subcode_label =
"signo";
348 exc_desc =
"EXC_BREAKPOINT";
350 case llvm::Triple::x86:
351 case llvm::Triple::x86_64:
354 code_desc =
"EXC_I386_SGL";
357 code_desc =
"EXC_I386_BPT";
362 case llvm::Triple::arm:
363 case llvm::Triple::thumb:
366 code_desc =
"EXC_ARM_DA_ALIGN";
369 code_desc =
"EXC_ARM_DA_DEBUG";
372 code_desc =
"EXC_ARM_BREAKPOINT";
377 code_desc =
"EXC_ARM_BREAKPOINT";
382 case llvm::Triple::aarch64:
393 exc_desc =
"EXC_SYSCALL";
397 exc_desc =
"EXC_MACH_SYSCALL";
401 exc_desc =
"EXC_RPC_ALERT";
405 exc_desc =
"EXC_CRASH";
408 exc_desc =
"EXC_RESOURCE";
409#if defined(__APPLE__)
411 int resource_type = EXC_RESOURCE_DECODE_RESOURCE_TYPE(
m_exc_code);
413 code_label =
"limit";
414 code_desc = code_desc_buf;
415 subcode_label =
"observed";
416 subcode_desc = subcode_desc_buf;
418 switch (resource_type) {
419 case RESOURCE_TYPE_CPU:
421 "EXC_RESOURCE (RESOURCE_TYPE_CPU: CPU usage monitor tripped)";
422 snprintf(code_desc_buf,
sizeof(code_desc_buf),
"%d%%",
423 (
int)EXC_RESOURCE_CPUMONITOR_DECODE_PERCENTAGE(
m_exc_code));
424 snprintf(subcode_desc_buf,
sizeof(subcode_desc_buf),
"%d%%",
425 (
int)EXC_RESOURCE_CPUMONITOR_DECODE_PERCENTAGE_OBSERVED(
428 case RESOURCE_TYPE_WAKEUPS:
429 exc_desc =
"EXC_RESOURCE (RESOURCE_TYPE_WAKEUPS: idle wakeups monitor "
432 code_desc_buf,
sizeof(code_desc_buf),
"%d w/s",
433 (
int)EXC_RESOURCE_CPUMONITOR_DECODE_WAKEUPS_PERMITTED(
m_exc_code));
434 snprintf(subcode_desc_buf,
sizeof(subcode_desc_buf),
"%d w/s",
435 (
int)EXC_RESOURCE_CPUMONITOR_DECODE_WAKEUPS_OBSERVED(
438 case RESOURCE_TYPE_MEMORY:
439 exc_desc =
"EXC_RESOURCE (RESOURCE_TYPE_MEMORY: high watermark memory "
441 snprintf(code_desc_buf,
sizeof(code_desc_buf),
"%d MB",
442 (
int)EXC_RESOURCE_HWM_DECODE_LIMIT(
m_exc_code));
443 subcode_desc =
nullptr;
444 subcode_label =
nullptr;
446#if defined(RESOURCE_TYPE_IO)
448 case RESOURCE_TYPE_IO:
449 exc_desc =
"EXC_RESOURCE RESOURCE_TYPE_IO";
450 snprintf(code_desc_buf,
sizeof(code_desc_buf),
"%d MB",
451 (
int)EXC_RESOURCE_IO_DECODE_LIMIT(
m_exc_code));
452 snprintf(subcode_desc_buf,
sizeof(subcode_desc_buf),
"%d MB",
462 exc_desc =
"EXC_GUARD";
475 strm.
Printf(
" (%s=%s", code_label, code_desc);
481 if (subcode_label && subcode_desc)
482 strm.
Printf(
", %s=%s", subcode_label, subcode_desc);
483 else if (subcode_label)
495 uint32_t exc_data_count,
496 uint64_t exc_sub_code,
497 uint64_t exc_sub_sub_code) {
502 target->
GetProcessSP()->GetWatchpointResourceList().FindByAddress(
504 if (wp_rsrc_sp && wp_rsrc_sp->GetNumberOfConstituents() > 0) {
506 thread, wp_rsrc_sp->GetConstituentAtIndex(0)->
GetID());
515 process_sp->GetBreakpointSiteList().FindByAddress(
517 if (bp_sp && bp_sp->IsEnabled()) {
526#if defined(__APPLE__)
528StopInfoMachException::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";
572std::optional<exception_type_t>
573StopInfoMachException::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);
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 bool not_stepping_but_got_singlestep_exception =
false;
606 uint32_t pc_decrement = 0;
609 const llvm::Triple::ArchType cpu =
611 : llvm::Triple::UnknownArch;
621 if (exc_code == 0x10003)
623 if (exc_sub_code == 5) {
628 DynamicLoader *dynamic_loader = process_sp->GetDynamicLoader();
641 bool is_actual_breakpoint =
false;
642 bool is_trace_if_actual_breakpoint_missing =
false;
644 case llvm::Triple::x86:
645 case llvm::Triple::x86_64:
653 is_actual_breakpoint =
true;
654 is_trace_if_actual_breakpoint_missing =
true;
658 exc_sub_code, exc_sub_sub_code))
661 }
else if (exc_code == 2 ||
666 is_trace_if_actual_breakpoint_missing =
true;
668 is_actual_breakpoint =
true;
669 if (!pc_already_adjusted)
674 case llvm::Triple::arm:
675 case llvm::Triple::thumb:
676 if (exc_code == 0x102)
687 if (wp_sp && wp_sp->IsEnabled()) {
691 is_actual_breakpoint =
true;
692 is_trace_if_actual_breakpoint_missing =
true;
694 }
else if (exc_code == 1)
696 is_actual_breakpoint =
true;
697 is_trace_if_actual_breakpoint_missing =
true;
698 }
else if (exc_code == 0)
703 is_actual_breakpoint =
true;
704 is_trace_if_actual_breakpoint_missing =
true;
708 case llvm::Triple::aarch64_32:
709 case llvm::Triple::aarch64: {
724 if (exc_code == 1 && exc_sub_code == 0)
728 is_actual_breakpoint =
true;
729 is_trace_if_actual_breakpoint_missing =
true;
731 not_stepping_but_got_singlestep_exception =
true;
733 if (exc_code == 0x102)
744 if (wp_sp && wp_sp->IsEnabled()) {
755 is_actual_breakpoint = exc_code == 1;
763 if (is_actual_breakpoint) {
765 addr_t pc = reg_ctx_sp->GetPC() - pc_decrement;
771 bp_site_sp = process_sp->GetBreakpointSiteList().FindByAddress(
pc);
772 if (bp_site_sp && bp_site_sp->IsEnabled()) {
776 if (pc_decrement > 0 && adjust_pc_if_needed)
777 reg_ctx_sp->SetPC(
pc);
788 if (bp_site_sp->ValidForThisThread(thread) ||
789 thread.
GetProcess()->GetOperatingSystem() !=
nullptr)
791 thread, bp_site_sp->
GetID());
792 else if (is_trace_if_actual_breakpoint_missing)
799 if (is_trace_if_actual_breakpoint_missing &&
813 return std::make_shared<StopInfoMachException>(
814 thread, exc_type, exc_data_count, exc_code, exc_sub_code,
815 not_stepping_but_got_singlestep_exception);
837 if (!reg_ctx_sp || !prev_pc)
841 if (*prev_pc != reg_ctx_sp->GetPC())
846 if (process_sp->GetWatchpointResourceList().GetSize()) {
848 "Thread stopped with insn-step completed mach exception but "
849 "thread was not stepping; there is a hardware watchpoint set.");
854 auto &bp_site_list = process_sp->GetBreakpointSiteList();
855 for (
auto &site : bp_site_list.Sites()) {
856 if (site->IsHardware() && site->IsEnabled()) {
858 "Thread stopped with insn-step completed mach exception but "
859 "thread was not stepping; there is a hardware breakpoint set.");
#define LLDB_LOGF(log,...)
static StopInfoSP GetStopInfoForHardwareBP(Thread &thread, Target *target, uint32_t exc_data_count, uint64_t exc_sub_code, uint64_t exc_sub_sub_code)
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 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, const AddressRange &disasm_range, 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.
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)
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)
const ArchSpec & GetArchitecture() const
WatchpointList & GetWatchpointList()
std::optional< lldb::addr_t > GetPreviousFrameZeroPC()
Request the pc value the thread had when previously stopped.
virtual lldb::StackFrameSP GetStackFrameAtIndex(uint32_t idx)
virtual lldb::RegisterContextSP GetRegisterContext()=0
lldb::ProcessSP CalculateProcess() override
lldb::StateType GetTemporaryResumeState() const
lldb::ProcessSP GetProcess() const
const lldb::WatchpointSP FindByAddress(lldb::addr_t addr) const
Returns a shared pointer to the watchpoint at address addr - const version.
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::Watchpoint > WatchpointSP
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),...
lldb::user_id_t GetID() const
Get accessor for the user ID.