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);
 
 
   84  const bool IsBadAccess = 
m_value == 1;            
 
   85  const bool IsMTETagFault = (
m_exc_code == 0x106); 
 
   86  if (!IsBadAccess || !IsMTETagFault)
 
   95  strm.
Printf(
"EXC_ARM_MTE_TAG_FAULT (code=%" PRIu64 
", address=0x%" PRIx64
 
  102      "Note: MTE tag mismatch detected: pointer tag=%d, address=0x%" PRIx64,
 
  103      tag, canonical_addr);
 
 
  110  bool IsBreakpoint = 
m_value == 6; 
 
  111  bool IsBadAccess = 
m_value == 1;  
 
  112  if (!IsBreakpoint && !IsBadAccess)
 
  121  StackFrameSP current_frame = thread.GetStackFrameAtIndex(0);
 
  130  const bool ptrauth_enabled_target =
 
  132  if (!ptrauth_enabled_target)
 
  137  auto emit_ptrauth_prologue = [&](uint64_t at_address) {
 
  138    strm.
Printf(
"EXC_BAD_ACCESS (code=%" PRIu64 
", address=0x%" PRIx64 
")\n",
 
  140    strm.
Printf(
"Note: Possible pointer authentication failure detected.\n");
 
  144  assert(abi_sp && 
"Missing ABI info");
 
  148  Address current_address = current_frame->GetFrameCodeAddress();
 
  160    uint64_t fixed_bad_address = abi_sp->FixCodeAddress(bad_address);
 
  165    auto brk_ptrauth_info =
 
  167    if (brk_ptrauth_info && brk_ptrauth_info->IsAuthenticated) {
 
  168      emit_ptrauth_prologue(bad_address);
 
  169      strm.
Printf(
"Found value that failed to authenticate ");
 
  177  assert(IsBadAccess && 
"Handle EXC_BAD_ACCESS only after this point");
 
  188  uint64_t fixed_bad_address = abi_sp->FixCodeAddress(bad_address);
 
  195  if (bad_address != current_pc && fixed_bad_address != current_pc) {
 
  198    if (ptrauth_info && ptrauth_info->IsAuthenticated && ptrauth_info->IsLoad) {
 
  199      emit_ptrauth_prologue(bad_address);
 
  200      strm.
Printf(
"Found authenticated load instruction ");
 
  217  if (bad_address != current_pc && fixed_bad_address == current_pc) {
 
  218    if (
StackFrameSP parent_frame = thread.GetStackFrameAtIndex(1)) {
 
  220          parent_frame->GetFrameCodeAddress().GetLoadAddress(&target);
 
  225      auto blr_ptrauth_info =
 
  227      if (blr_ptrauth_info && blr_ptrauth_info->IsAuthenticated &&
 
  228          blr_ptrauth_info->DoesBranch) {
 
  229        emit_ptrauth_prologue(bad_address);
 
  230        strm.
Printf(
"Found authenticated indirect branch ");
 
 
  250    return "invalid stop reason!";
 
  254  const llvm::Triple::ArchType cpu =
 
  256             : llvm::Triple::UnknownArch;
 
  258  const char *exc_desc = 
nullptr;
 
  259  const char *code_label = 
"code";
 
  260  const char *code_desc = 
nullptr;
 
  261  const char *subcode_label = 
"subcode";
 
  262  const char *subcode_desc = 
nullptr;
 
  264#if defined(__APPLE__) 
  265  char code_desc_buf[32];
 
  266  char subcode_desc_buf[32];
 
  271    exc_desc = 
"EXC_BAD_ACCESS";
 
  272    subcode_label = 
"address";
 
  274    case llvm::Triple::x86:
 
  275    case llvm::Triple::x86_64:
 
  278        code_desc = 
"EXC_I386_GPFLT";
 
  283    case llvm::Triple::arm:
 
  284    case llvm::Triple::thumb:
 
  287        code_desc = 
"EXC_ARM_DA_ALIGN";
 
  290        code_desc = 
"EXC_ARM_DA_DEBUG";
 
  295    case llvm::Triple::aarch64:
 
  308    exc_desc = 
"EXC_BAD_INSTRUCTION";
 
  310    case llvm::Triple::x86:
 
  311    case llvm::Triple::x86_64:
 
  313        code_desc = 
"EXC_I386_INVOP";
 
  316    case llvm::Triple::arm:
 
  317    case llvm::Triple::thumb:
 
  319        code_desc = 
"EXC_ARM_UNDEFINED";
 
  328    exc_desc = 
"EXC_ARITHMETIC";
 
  330    case llvm::Triple::x86:
 
  331    case llvm::Triple::x86_64:
 
  334        code_desc = 
"EXC_I386_DIV";
 
  337        code_desc = 
"EXC_I386_INTO";
 
  340        code_desc = 
"EXC_I386_NOEXT";
 
  343        code_desc = 
"EXC_I386_EXTOVR";
 
  346        code_desc = 
"EXC_I386_EXTERR";
 
  349        code_desc = 
"EXC_I386_EMERR";
 
  352        code_desc = 
"EXC_I386_BOUND";
 
  355        code_desc = 
"EXC_I386_SSEEXTERR";
 
  366    exc_desc = 
"EXC_EMULATION";
 
  370    exc_desc = 
"EXC_SOFTWARE";
 
  372      subcode_desc = 
"EXC_SOFT_SIGNAL";
 
  373      subcode_label = 
"signo";
 
  379    exc_desc = 
"EXC_BREAKPOINT";
 
  381    case llvm::Triple::x86:
 
  382    case llvm::Triple::x86_64:
 
  385        code_desc = 
"EXC_I386_SGL";
 
  388        code_desc = 
"EXC_I386_BPT";
 
  393    case llvm::Triple::arm:
 
  394    case llvm::Triple::thumb:
 
  397        code_desc = 
"EXC_ARM_DA_ALIGN";
 
  400        code_desc = 
"EXC_ARM_DA_DEBUG";
 
  403        code_desc = 
"EXC_ARM_BREAKPOINT";
 
  408        code_desc = 
"EXC_ARM_BREAKPOINT";
 
  413    case llvm::Triple::aarch64:
 
  424    exc_desc = 
"EXC_SYSCALL";
 
  428    exc_desc = 
"EXC_MACH_SYSCALL";
 
  432    exc_desc = 
"EXC_RPC_ALERT";
 
  436    exc_desc = 
"EXC_CRASH";
 
  439    exc_desc = 
"EXC_RESOURCE";
 
  440#if defined(__APPLE__) 
  442      int resource_type = EXC_RESOURCE_DECODE_RESOURCE_TYPE(
m_exc_code);
 
  444      code_label = 
"limit";
 
  445      code_desc = code_desc_buf;
 
  446      subcode_label = 
"observed";
 
  447      subcode_desc = subcode_desc_buf;
 
  449      switch (resource_type) {
 
  450      case RESOURCE_TYPE_CPU:
 
  452            "EXC_RESOURCE (RESOURCE_TYPE_CPU: CPU usage monitor tripped)";
 
  453        snprintf(code_desc_buf, 
sizeof(code_desc_buf), 
"%d%%",
 
  454                 (
int)EXC_RESOURCE_CPUMONITOR_DECODE_PERCENTAGE(
m_exc_code));
 
  455        snprintf(subcode_desc_buf, 
sizeof(subcode_desc_buf), 
"%d%%",
 
  456                 (
int)EXC_RESOURCE_CPUMONITOR_DECODE_PERCENTAGE_OBSERVED(
 
  459      case RESOURCE_TYPE_WAKEUPS:
 
  460        exc_desc = 
"EXC_RESOURCE (RESOURCE_TYPE_WAKEUPS: idle wakeups monitor " 
  463            code_desc_buf, 
sizeof(code_desc_buf), 
"%d w/s",
 
  464            (
int)EXC_RESOURCE_CPUMONITOR_DECODE_WAKEUPS_PERMITTED(
m_exc_code));
 
  465        snprintf(subcode_desc_buf, 
sizeof(subcode_desc_buf), 
"%d w/s",
 
  466                 (
int)EXC_RESOURCE_CPUMONITOR_DECODE_WAKEUPS_OBSERVED(
 
  469      case RESOURCE_TYPE_MEMORY:
 
  470        exc_desc = 
"EXC_RESOURCE (RESOURCE_TYPE_MEMORY: high watermark memory " 
  472        snprintf(code_desc_buf, 
sizeof(code_desc_buf), 
"%d MB",
 
  473                 (
int)EXC_RESOURCE_HWM_DECODE_LIMIT(
m_exc_code));
 
  474        subcode_desc = 
nullptr;
 
  475        subcode_label = 
nullptr;
 
  477#if defined(RESOURCE_TYPE_IO) 
  479      case RESOURCE_TYPE_IO:
 
  480        exc_desc = 
"EXC_RESOURCE RESOURCE_TYPE_IO";
 
  481        snprintf(code_desc_buf, 
sizeof(code_desc_buf), 
"%d MB",
 
  482                 (
int)EXC_RESOURCE_IO_DECODE_LIMIT(
m_exc_code));
 
  483        snprintf(subcode_desc_buf, 
sizeof(subcode_desc_buf), 
"%d MB",
 
  493    exc_desc = 
"EXC_GUARD";
 
  506      strm.
Printf(
" (%s=%s", code_label, code_desc);
 
  512    if (subcode_label && subcode_desc)
 
  513      strm.
Printf(
", %s=%s", subcode_label, subcode_desc);
 
  514    else if (subcode_label)
 
 
  525#if defined(__APPLE__) 
  527StopInfoMachException::MachException::Name(exception_type_t exc_type) {
 
  530    return "EXC_BAD_ACCESS";
 
  531  case EXC_BAD_INSTRUCTION:
 
  532    return "EXC_BAD_INSTRUCTION";
 
  534    return "EXC_ARITHMETIC";
 
  536    return "EXC_EMULATION";
 
  538    return "EXC_SOFTWARE";
 
  540    return "EXC_BREAKPOINT";
 
  542    return "EXC_SYSCALL";
 
  543  case EXC_MACH_SYSCALL:
 
  544    return "EXC_MACH_SYSCALL";
 
  546    return "EXC_RPC_ALERT";
 
  552    return "EXC_RESOURCE";
 
  557#ifdef EXC_CORPSE_NOTIFY 
  558  case EXC_CORPSE_NOTIFY:
 
  559    return "EXC_CORPSE_NOTIFY";
 
  561#ifdef EXC_CORPSE_VARIANT_BIT 
  562  case EXC_CORPSE_VARIANT_BIT:
 
  563    return "EXC_CORPSE_VARIANT_BIT";
 
  571std::optional<exception_type_t>
 
  572StopInfoMachException::MachException::ExceptionCode(
const char *name) {
 
  573  return llvm::StringSwitch<std::optional<exception_type_t>>(name)
 
  574      .Case(
"EXC_BAD_ACCESS", EXC_BAD_ACCESS)
 
  575      .Case(
"EXC_BAD_INSTRUCTION", EXC_BAD_INSTRUCTION)
 
  576      .Case(
"EXC_ARITHMETIC", EXC_ARITHMETIC)
 
  577      .Case(
"EXC_EMULATION", EXC_EMULATION)
 
  578      .Case(
"EXC_SOFTWARE", EXC_SOFTWARE)
 
  579      .Case(
"EXC_BREAKPOINT", EXC_BREAKPOINT)
 
  580      .Case(
"EXC_SYSCALL", EXC_SYSCALL)
 
  581      .Case(
"EXC_MACH_SYSCALL", EXC_MACH_SYSCALL)
 
  582      .Case(
"EXC_RPC_ALERT", EXC_RPC_ALERT)
 
  584      .Case(
"EXC_CRASH", EXC_CRASH)
 
  586      .Case(
"EXC_RESOURCE", EXC_RESOURCE)
 
  588      .Case(
"EXC_GUARD", EXC_GUARD)
 
  590#ifdef EXC_CORPSE_NOTIFY 
  591      .Case(
"EXC_CORPSE_NOTIFY", EXC_CORPSE_NOTIFY)
 
  593      .Default(std::nullopt);
 
  598    Thread &thread, uint32_t exc_type, uint32_t exc_data_count,
 
  599    uint64_t exc_code, uint64_t exc_sub_code, uint64_t exc_sub_sub_code,
 
  600    bool pc_already_adjusted, 
bool adjust_pc_if_needed) {
 
  604  bool not_stepping_but_got_singlestep_exception = 
false;
 
  605  uint32_t pc_decrement = 0;
 
  608  const llvm::Triple::ArchType cpu =
 
  610             : llvm::Triple::UnknownArch;
 
  612  ProcessSP process_sp(thread.GetProcess());
 
  627      process_sp->GetBreakpointSiteList().FindByAddress(
pc);
 
  628  if (bp_site_sp && bp_site_sp->IsEnabled())
 
  629    thread.SetThreadStoppedAtUnexecutedBP(
pc);
 
  639    if (exc_code == 0x10003) 
 
  641      if (exc_sub_code == 5) {
 
  644        ProcessSP process_sp(thread.GetProcess());
 
  646          DynamicLoader *dynamic_loader = process_sp->GetDynamicLoader();
 
  705    bool stopped_by_hitting_breakpoint = 
false;
 
  706    bool stopped_by_completing_stepi = 
false;
 
  707    bool stopped_watchpoint = 
false;
 
  708    std::optional<addr_t> address;
 
  712      if (exc_sub_code == 0) {
 
  713        stopped_by_completing_stepi = 
true;
 
  717        stopped_by_hitting_breakpoint = 
true;
 
  718        stopped_watchpoint = 
true;
 
  719        address = exc_sub_code;
 
  725      if (exc_sub_code == 0)
 
  726        stopped_by_hitting_breakpoint = 
true;
 
  728        stopped_by_hitting_breakpoint = 
true;
 
  730        if (!pc_already_adjusted)
 
  737      stopped_by_completing_stepi = 
true;
 
  740    if (exc_code == 0x102 && exc_sub_code != 0) {
 
  741      if (cpu == llvm::Triple::arm || cpu == llvm::Triple::thumb) {
 
  742        stopped_by_hitting_breakpoint = 
true;
 
  743        stopped_by_completing_stepi = 
true;
 
  745      stopped_watchpoint = 
true;
 
  746      address = exc_sub_code;
 
  754    if (stopped_by_hitting_breakpoint) {
 
  755      addr_t pc = reg_ctx_sp->GetPC() - pc_decrement;
 
  759            process_sp->GetBreakpointSiteList().FindByAddress(*address);
 
  760      if (!bp_site_sp && reg_ctx_sp) {
 
  761        bp_site_sp = process_sp->GetBreakpointSiteList().FindByAddress(
pc);
 
  763      if (bp_site_sp && bp_site_sp->IsEnabled()) {
 
  766        thread.SetThreadHitBreakpointSite();
 
  768        if (bp_site_sp->ValidForThisThread(thread)) {
 
  772          if (pc_decrement > 0 && adjust_pc_if_needed && reg_ctx_sp)
 
  773            reg_ctx_sp->SetPC(
pc);
 
  776              thread, bp_site_sp->GetID());
 
  786    if (stopped_watchpoint && address) {
 
  788          target->
GetProcessSP()->GetWatchpointResourceList().FindByAddress(
 
  790      if (wp_rsrc_sp && wp_rsrc_sp->GetNumberOfConstituents() > 0) {
 
  792            thread, wp_rsrc_sp->GetConstituentAtIndex(0)->GetID());
 
  798    if (stopped_by_completing_stepi) {
 
  800        not_stepping_but_got_singlestep_exception = 
true;
 
  814  return std::make_shared<StopInfoMachException>(
 
  815      thread, exc_type, exc_data_count, exc_code, exc_sub_code,
 
  816      not_stepping_but_got_singlestep_exception);
 
 
  837  std::optional<addr_t> prev_pc = thread.GetPreviousFrameZeroPC();
 
  838  if (!reg_ctx_sp || !prev_pc)
 
  842  if (*prev_pc != reg_ctx_sp->GetPC())
 
  846  ProcessSP process_sp = thread.GetProcess();
 
  847  if (process_sp->GetWatchpointResourceList().GetSize()) {
 
  849              "Thread stopped with insn-step completed mach exception but " 
  850              "thread was not stepping; there is a hardware watchpoint set.");
 
  855  auto &bp_site_list = process_sp->GetBreakpointSiteList();
 
  856  for (
auto &site : bp_site_list.Sites()) {
 
  857    if (site->IsHardware() && site->IsEnabled()) {
 
  859                "Thread stopped with insn-step completed mach exception but " 
  860                "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
bool DetermineTagMismatch(ExecutionContext &exe_ctx)
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, 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),...