15#include <kern/exc_resource.h>
46static std::optional<PtrauthInstructionInfo>
49 const char *plugin_name =
nullptr;
50 const char *flavor =
nullptr;
52 const bool prefer_file_cache =
true;
54 arch, plugin_name, flavor, target, range_bounds, prefer_file_cache);
78 bool IsBreakpoint =
m_value == 6;
79 bool IsBadAccess =
m_value == 1;
80 if (!IsBreakpoint && !IsBadAccess)
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");
112 assert(abi_sp &&
"Missing ABI info");
116 Address current_address = current_frame->GetFrameCodeAddress();
128 uint64_t fixed_bad_address = abi_sp->FixCodeAddress(bad_address);
133 auto brk_ptrauth_info =
135 if (brk_ptrauth_info && brk_ptrauth_info->IsAuthenticated) {
136 emit_ptrauth_prologue(bad_address);
137 strm.
Printf(
"Found value that failed to authenticate ");
145 assert(IsBadAccess &&
"Handle EXC_BAD_ACCESS only after this point");
156 uint64_t fixed_bad_address = abi_sp->FixCodeAddress(bad_address);
163 if (bad_address != current_pc && fixed_bad_address != current_pc) {
166 if (ptrauth_info && ptrauth_info->IsAuthenticated && ptrauth_info->IsLoad) {
167 emit_ptrauth_prologue(bad_address);
168 strm.
Printf(
"Found authenticated load instruction ");
185 if (bad_address != current_pc && fixed_bad_address == current_pc) {
188 parent_frame->GetFrameCodeAddress().GetLoadAddress(&target);
193 auto blr_ptrauth_info =
195 if (blr_ptrauth_info && blr_ptrauth_info->IsAuthenticated &&
196 blr_ptrauth_info->DoesBranch) {
197 emit_ptrauth_prologue(bad_address);
198 strm.
Printf(
"Found authenticated indirect branch ");
218 return "invalid stop reason!";
222 const llvm::Triple::ArchType cpu =
224 : llvm::Triple::UnknownArch;
226 const char *exc_desc =
nullptr;
227 const char *code_label =
"code";
228 const char *code_desc =
nullptr;
229 const char *subcode_label =
"subcode";
230 const char *subcode_desc =
nullptr;
232#if defined(__APPLE__)
233 char code_desc_buf[32];
234 char subcode_desc_buf[32];
239 exc_desc =
"EXC_BAD_ACCESS";
240 subcode_label =
"address";
242 case llvm::Triple::x86:
243 case llvm::Triple::x86_64:
246 code_desc =
"EXC_I386_GPFLT";
251 case llvm::Triple::arm:
252 case llvm::Triple::thumb:
255 code_desc =
"EXC_ARM_DA_ALIGN";
258 code_desc =
"EXC_ARM_DA_DEBUG";
263 case llvm::Triple::aarch64:
274 exc_desc =
"EXC_BAD_INSTRUCTION";
276 case llvm::Triple::x86:
277 case llvm::Triple::x86_64:
279 code_desc =
"EXC_I386_INVOP";
282 case llvm::Triple::arm:
283 case llvm::Triple::thumb:
285 code_desc =
"EXC_ARM_UNDEFINED";
294 exc_desc =
"EXC_ARITHMETIC";
296 case llvm::Triple::x86:
297 case llvm::Triple::x86_64:
300 code_desc =
"EXC_I386_DIV";
303 code_desc =
"EXC_I386_INTO";
306 code_desc =
"EXC_I386_NOEXT";
309 code_desc =
"EXC_I386_EXTOVR";
312 code_desc =
"EXC_I386_EXTERR";
315 code_desc =
"EXC_I386_EMERR";
318 code_desc =
"EXC_I386_BOUND";
321 code_desc =
"EXC_I386_SSEEXTERR";
332 exc_desc =
"EXC_EMULATION";
336 exc_desc =
"EXC_SOFTWARE";
338 subcode_desc =
"EXC_SOFT_SIGNAL";
339 subcode_label =
"signo";
345 exc_desc =
"EXC_BREAKPOINT";
347 case llvm::Triple::x86:
348 case llvm::Triple::x86_64:
351 code_desc =
"EXC_I386_SGL";
354 code_desc =
"EXC_I386_BPT";
359 case llvm::Triple::arm:
360 case llvm::Triple::thumb:
363 code_desc =
"EXC_ARM_DA_ALIGN";
366 code_desc =
"EXC_ARM_DA_DEBUG";
369 code_desc =
"EXC_ARM_BREAKPOINT";
374 code_desc =
"EXC_ARM_BREAKPOINT";
379 case llvm::Triple::aarch64:
390 exc_desc =
"EXC_SYSCALL";
394 exc_desc =
"EXC_MACH_SYSCALL";
398 exc_desc =
"EXC_RPC_ALERT";
402 exc_desc =
"EXC_CRASH";
405 exc_desc =
"EXC_RESOURCE";
406#if defined(__APPLE__)
408 int resource_type = EXC_RESOURCE_DECODE_RESOURCE_TYPE(
m_exc_code);
410 code_label =
"limit";
411 code_desc = code_desc_buf;
412 subcode_label =
"observed";
413 subcode_desc = subcode_desc_buf;
415 switch (resource_type) {
416 case RESOURCE_TYPE_CPU:
418 "EXC_RESOURCE (RESOURCE_TYPE_CPU: CPU usage monitor tripped)";
419 snprintf(code_desc_buf,
sizeof(code_desc_buf),
"%d%%",
420 (
int)EXC_RESOURCE_CPUMONITOR_DECODE_PERCENTAGE(
m_exc_code));
421 snprintf(subcode_desc_buf,
sizeof(subcode_desc_buf),
"%d%%",
422 (
int)EXC_RESOURCE_CPUMONITOR_DECODE_PERCENTAGE_OBSERVED(
425 case RESOURCE_TYPE_WAKEUPS:
426 exc_desc =
"EXC_RESOURCE (RESOURCE_TYPE_WAKEUPS: idle wakeups monitor "
429 code_desc_buf,
sizeof(code_desc_buf),
"%d w/s",
430 (
int)EXC_RESOURCE_CPUMONITOR_DECODE_WAKEUPS_PERMITTED(
m_exc_code));
431 snprintf(subcode_desc_buf,
sizeof(subcode_desc_buf),
"%d w/s",
432 (
int)EXC_RESOURCE_CPUMONITOR_DECODE_WAKEUPS_OBSERVED(
435 case RESOURCE_TYPE_MEMORY:
436 exc_desc =
"EXC_RESOURCE (RESOURCE_TYPE_MEMORY: high watermark memory "
438 snprintf(code_desc_buf,
sizeof(code_desc_buf),
"%d MB",
439 (
int)EXC_RESOURCE_HWM_DECODE_LIMIT(
m_exc_code));
440 subcode_desc =
nullptr;
441 subcode_label =
nullptr;
443#if defined(RESOURCE_TYPE_IO)
445 case RESOURCE_TYPE_IO:
446 exc_desc =
"EXC_RESOURCE RESOURCE_TYPE_IO";
447 snprintf(code_desc_buf,
sizeof(code_desc_buf),
"%d MB",
448 (
int)EXC_RESOURCE_IO_DECODE_LIMIT(
m_exc_code));
449 snprintf(subcode_desc_buf,
sizeof(subcode_desc_buf),
"%d MB",
459 exc_desc =
"EXC_GUARD";
472 strm.
Printf(
" (%s=%s", code_label, code_desc);
478 if (subcode_label && subcode_desc)
479 strm.
Printf(
", %s=%s", subcode_label, subcode_desc);
480 else if (subcode_label)
492 uint32_t exc_data_count,
493 uint64_t exc_sub_code,
494 uint64_t exc_sub_sub_code) {
499 target->
GetProcessSP()->GetWatchpointResourceList().FindByAddress(
501 if (wp_rsrc_sp && wp_rsrc_sp->GetNumberOfConstituents() > 0) {
503 thread, wp_rsrc_sp->GetConstituentAtIndex(0)->
GetID());
512 process_sp->GetBreakpointSiteList().FindByAddress(
514 if (bp_sp && bp_sp->IsEnabled()) {
523#if defined(__APPLE__)
525StopInfoMachException::MachException::Name(exception_type_t exc_type) {
528 return "EXC_BAD_ACCESS";
529 case EXC_BAD_INSTRUCTION:
530 return "EXC_BAD_INSTRUCTION";
532 return "EXC_ARITHMETIC";
534 return "EXC_EMULATION";
536 return "EXC_SOFTWARE";
538 return "EXC_BREAKPOINT";
540 return "EXC_SYSCALL";
541 case EXC_MACH_SYSCALL:
542 return "EXC_MACH_SYSCALL";
544 return "EXC_RPC_ALERT";
550 return "EXC_RESOURCE";
555#ifdef EXC_CORPSE_NOTIFY
556 case EXC_CORPSE_NOTIFY:
557 return "EXC_CORPSE_NOTIFY";
559#ifdef EXC_CORPSE_VARIANT_BIT
560 case EXC_CORPSE_VARIANT_BIT:
561 return "EXC_CORPSE_VARIANT_BIT";
569std::optional<exception_type_t>
570StopInfoMachException::MachException::ExceptionCode(
const char *name) {
571 return llvm::StringSwitch<std::optional<exception_type_t>>(name)
572 .Case(
"EXC_BAD_ACCESS", EXC_BAD_ACCESS)
573 .Case(
"EXC_BAD_INSTRUCTION", EXC_BAD_INSTRUCTION)
574 .Case(
"EXC_ARITHMETIC", EXC_ARITHMETIC)
575 .Case(
"EXC_EMULATION", EXC_EMULATION)
576 .Case(
"EXC_SOFTWARE", EXC_SOFTWARE)
577 .Case(
"EXC_BREAKPOINT", EXC_BREAKPOINT)
578 .Case(
"EXC_SYSCALL", EXC_SYSCALL)
579 .Case(
"EXC_MACH_SYSCALL", EXC_MACH_SYSCALL)
580 .Case(
"EXC_RPC_ALERT", EXC_RPC_ALERT)
582 .Case(
"EXC_CRASH", EXC_CRASH)
584 .Case(
"EXC_RESOURCE", EXC_RESOURCE)
586 .Case(
"EXC_GUARD", EXC_GUARD)
588#ifdef EXC_CORPSE_NOTIFY
589 .Case(
"EXC_CORPSE_NOTIFY", EXC_CORPSE_NOTIFY)
591 .Default(std::nullopt);
596 Thread &thread, uint32_t exc_type, uint32_t exc_data_count,
597 uint64_t exc_code, uint64_t exc_sub_code, uint64_t exc_sub_sub_code,
598 bool pc_already_adjusted,
bool adjust_pc_if_needed) {
602 bool not_stepping_but_got_singlestep_exception =
false;
603 uint32_t pc_decrement = 0;
606 const llvm::Triple::ArchType cpu =
608 : llvm::Triple::UnknownArch;
618 if (exc_code == 0x10003)
620 if (exc_sub_code == 5) {
625 DynamicLoader *dynamic_loader = process_sp->GetDynamicLoader();
638 bool is_actual_breakpoint =
false;
639 bool is_trace_if_actual_breakpoint_missing =
false;
641 case llvm::Triple::x86:
642 case llvm::Triple::x86_64:
650 is_actual_breakpoint =
true;
651 is_trace_if_actual_breakpoint_missing =
true;
655 exc_sub_code, exc_sub_sub_code))
658 }
else if (exc_code == 2 ||
663 is_trace_if_actual_breakpoint_missing =
true;
665 is_actual_breakpoint =
true;
666 if (!pc_already_adjusted)
671 case llvm::Triple::arm:
672 case llvm::Triple::thumb:
673 if (exc_code == 0x102)
684 if (wp_sp && wp_sp->IsEnabled()) {
688 is_actual_breakpoint =
true;
689 is_trace_if_actual_breakpoint_missing =
true;
691 }
else if (exc_code == 1)
693 is_actual_breakpoint =
true;
694 is_trace_if_actual_breakpoint_missing =
true;
695 }
else if (exc_code == 0)
700 is_actual_breakpoint =
true;
701 is_trace_if_actual_breakpoint_missing =
true;
705 case llvm::Triple::aarch64_32:
706 case llvm::Triple::aarch64: {
721 if (exc_code == 1 && exc_sub_code == 0)
725 is_actual_breakpoint =
true;
726 is_trace_if_actual_breakpoint_missing =
true;
728 not_stepping_but_got_singlestep_exception =
true;
730 if (exc_code == 0x102)
741 if (wp_sp && wp_sp->IsEnabled()) {
752 is_actual_breakpoint = exc_code == 1;
760 if (is_actual_breakpoint) {
762 addr_t pc = reg_ctx_sp->GetPC() - pc_decrement;
768 bp_site_sp = process_sp->GetBreakpointSiteList().FindByAddress(
pc);
769 if (bp_site_sp && bp_site_sp->IsEnabled()) {
773 if (pc_decrement > 0 && adjust_pc_if_needed)
774 reg_ctx_sp->SetPC(
pc);
785 if (bp_site_sp->ValidForThisThread(thread) ||
786 thread.
GetProcess()->GetOperatingSystem() !=
nullptr)
788 thread, bp_site_sp->
GetID());
789 else if (is_trace_if_actual_breakpoint_missing)
796 if (is_trace_if_actual_breakpoint_missing &&
810 return std::make_shared<StopInfoMachException>(
811 thread, exc_type, exc_data_count, exc_code, exc_sub_code,
812 not_stepping_but_got_singlestep_exception);
834 if (!reg_ctx_sp || !prev_pc)
838 if (*prev_pc != reg_ctx_sp->GetPC())
843 if (process_sp->GetWatchpointResourceList().GetSize()) {
845 "Thread stopped with insn-step completed mach exception but "
846 "thread was not stepping; there is a hardware watchpoint set.");
851 auto &bp_site_list = process_sp->GetBreakpointSiteList();
852 for (
auto &site : bp_site_list.Sites()) {
853 if (site->IsHardware() && site->IsEnabled()) {
855 "Thread stopped with insn-step completed mach exception but "
856 "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, 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.