15#include <kern/exc_resource.h>
44static std::optional<PtrauthInstructionInfo>
47 const char *plugin_name =
nullptr;
48 const char *flavor =
nullptr;
50 const bool prefer_file_cache =
true;
52 arch, plugin_name, flavor, target, range_bounds, prefer_file_cache);
76 bool IsBreakpoint =
m_value == 6;
77 bool IsBadAccess =
m_value == 1;
78 if (!IsBreakpoint && !IsBadAccess)
95 assert(abi_sp &&
"Missing ABI info");
98 const bool ptrauth_enabled_target =
100 if (!ptrauth_enabled_target)
105 auto emit_ptrauth_prologue = [&](uint64_t at_address) {
106 strm.
Printf(
"EXC_BAD_ACCESS (code=%" PRIu64
", address=0x%" PRIx64
")\n",
108 strm.
Printf(
"Note: Possible pointer authentication failure detected.\n");
113 Address current_address = current_frame->GetFrameCodeAddress();
125 uint64_t fixed_bad_address = abi_sp->FixCodeAddress(bad_address);
130 auto brk_ptrauth_info =
132 if (brk_ptrauth_info && brk_ptrauth_info->IsAuthenticated) {
133 emit_ptrauth_prologue(bad_address);
134 strm.
Printf(
"Found value that failed to authenticate ");
142 assert(IsBadAccess &&
"Handle EXC_BAD_ACCESS only after this point");
153 uint64_t fixed_bad_address = abi_sp->FixCodeAddress(bad_address);
160 if (bad_address != current_pc && fixed_bad_address != current_pc) {
163 if (ptrauth_info && ptrauth_info->IsAuthenticated && ptrauth_info->IsLoad) {
164 emit_ptrauth_prologue(bad_address);
165 strm.
Printf(
"Found authenticated load instruction ");
182 if (bad_address != current_pc && fixed_bad_address == current_pc) {
185 parent_frame->GetFrameCodeAddress().GetLoadAddress(&target);
190 auto blr_ptrauth_info =
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 ");
215 return "invalid stop reason!";
219 const llvm::Triple::ArchType cpu =
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];
236 exc_desc =
"EXC_BAD_ACCESS";
237 subcode_label =
"address";
239 case llvm::Triple::x86:
240 case llvm::Triple::x86_64:
243 code_desc =
"EXC_I386_GPFLT";
248 case llvm::Triple::arm:
249 case llvm::Triple::thumb:
252 code_desc =
"EXC_ARM_DA_ALIGN";
255 code_desc =
"EXC_ARM_DA_DEBUG";
260 case llvm::Triple::aarch64:
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";
291 exc_desc =
"EXC_ARITHMETIC";
293 case llvm::Triple::x86:
294 case llvm::Triple::x86_64:
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";
329 exc_desc =
"EXC_EMULATION";
333 exc_desc =
"EXC_SOFTWARE";
335 subcode_desc =
"EXC_SOFT_SIGNAL";
336 subcode_label =
"signo";
342 exc_desc =
"EXC_BREAKPOINT";
344 case llvm::Triple::x86:
345 case llvm::Triple::x86_64:
348 code_desc =
"EXC_I386_SGL";
351 code_desc =
"EXC_I386_BPT";
356 case llvm::Triple::arm:
357 case llvm::Triple::thumb:
360 code_desc =
"EXC_ARM_DA_ALIGN";
363 code_desc =
"EXC_ARM_DA_DEBUG";
366 code_desc =
"EXC_ARM_BREAKPOINT";
371 code_desc =
"EXC_ARM_BREAKPOINT";
376 case llvm::Triple::aarch64:
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)
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",
456 exc_desc =
"EXC_GUARD";
469 strm.
Printf(
" (%s=%s", code_label, code_desc);
475 if (subcode_label && subcode_desc)
476 strm.
Printf(
", %s=%s", subcode_label, subcode_desc);
477 else if (subcode_label)
489 uint32_t exc_data_count,
490 uint64_t exc_sub_code,
491 uint64_t exc_sub_sub_code) {
497 if (wp_sp && wp_sp->IsEnabled()) {
500 if (exc_data_count >= 3)
501 wp_sp->SetHardwareIndex((uint32_t)exc_sub_sub_code);
511 process_sp->GetBreakpointSiteList().FindByAddress(
513 if (bp_sp && bp_sp->IsEnabled()) {
516 if (exc_data_count >= 3)
517 bp_sp->SetHardwareIndex((uint32_t)exc_sub_sub_code);
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 uint32_t pc_decrement = 0;
608 const llvm::Triple::ArchType cpu =
610 : llvm::Triple::UnknownArch;
620 if (exc_code == 0x10003)
622 if (exc_sub_code == 5) {
627 DynamicLoader *dynamic_loader = process_sp->GetDynamicLoader();
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:
652 is_actual_breakpoint =
true;
653 is_trace_if_actual_breakpoint_missing =
true;
657 exc_sub_code, exc_sub_sub_code))
660 }
else if (exc_code == 2 ||
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)
683 if (wp_sp && wp_sp->IsEnabled()) {
687 if (exc_data_count >= 3)
688 wp_sp->SetHardwareIndex((uint32_t)exc_sub_sub_code);
692 is_actual_breakpoint =
true;
693 is_trace_if_actual_breakpoint_missing =
true;
695 }
else if (exc_code == 1)
697 is_actual_breakpoint =
true;
698 is_trace_if_actual_breakpoint_missing =
true;
699 }
else if (exc_code == 0)
704 is_actual_breakpoint =
true;
705 is_trace_if_actual_breakpoint_missing =
true;
709 case llvm::Triple::aarch64_32:
710 case llvm::Triple::aarch64: {
725 if (exc_code == 1 && exc_sub_code == 0)
729 is_actual_breakpoint =
true;
730 is_trace_if_actual_breakpoint_missing =
true;
734 s.
Printf(
"CreateStopReasonWithMachException got EXC_BREAKPOINT [1,0] "
735 "indicating trace event, but thread is not tracing, it has "
739 if (
const RegisterInfo *ri = regctx->GetRegisterInfoByName(
"esr")) {
741 (uint32_t)regctx->ReadRegisterAsUnsigned(ri,
UINT32_MAX);
743 s.
Printf(
" esr value: 0x%" PRIx32, esr);
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)
764 if (wp_sp && wp_sp->IsEnabled()) {
768 if (exc_data_count >= 3)
769 wp_sp->SetHardwareIndex((uint32_t)exc_sub_sub_code);
780 is_actual_breakpoint = exc_code == 1;
788 if (is_actual_breakpoint) {
790 addr_t pc = reg_ctx_sp->GetPC() - pc_decrement;
796 bp_site_sp = process_sp->GetBreakpointSiteList().FindByAddress(
pc);
797 if (bp_site_sp && bp_site_sp->IsEnabled()) {
801 if (pc_decrement > 0 && adjust_pc_if_needed)
802 reg_ctx_sp->SetPC(
pc);
813 if (bp_site_sp->ValidForThisThread(thread) ||
814 thread.
GetProcess()->GetOperatingSystem() !=
nullptr)
816 thread, bp_site_sp->
GetID());
817 else if (is_trace_if_actual_breakpoint_missing)
824 if (is_trace_if_actual_breakpoint_missing &&
839 exc_code, exc_sub_code));
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, 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.
uint32_t m_exc_data_count
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
const char * GetData() const
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.
bool ResolveLoadAddress(lldb::addr_t load_addr, Address &so_addr, uint32_t stop_id=SectionLoadHistory::eStopIDNow)
const ArchSpec & GetArchitecture() const
WatchpointList & GetWatchpointList()
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.
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::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.