Home | History | Annotate | Download | only in Utility
      1 //===-- StopInfoMachException.cpp -------------------------------*- C++ -*-===//
      2 //
      3 //                     The LLVM Compiler Infrastructure
      4 //
      5 // This file is distributed under the University of Illinois Open Source
      6 // License. See LICENSE.TXT for details.
      7 //
      8 //===----------------------------------------------------------------------===//
      9 
     10 #include "StopInfoMachException.h"
     11 
     12 // C Includes
     13 // C++ Includes
     14 // Other libraries and framework includes
     15 // Project includes
     16 #include "lldb/Breakpoint/Watchpoint.h"
     17 #include "lldb/Core/ArchSpec.h"
     18 #include "lldb/Core/StreamString.h"
     19 #include "lldb/Symbol/Symbol.h"
     20 #include "lldb/Target/DynamicLoader.h"
     21 #include "lldb/Target/ExecutionContext.h"
     22 #include "lldb/Target/Process.h"
     23 #include "lldb/Target/RegisterContext.h"
     24 #include "lldb/Target/Target.h"
     25 #include "lldb/Target/Thread.h"
     26 #include "lldb/Target/ThreadPlan.h"
     27 #include "lldb/Target/UnixSignals.h"
     28 
     29 using namespace lldb;
     30 using namespace lldb_private;
     31 
     32 const char *
     33 StopInfoMachException::GetDescription ()
     34 {
     35     if (m_description.empty() && m_value != 0)
     36     {
     37         ExecutionContext exe_ctx (m_thread_wp.lock());
     38         Target *target = exe_ctx.GetTargetPtr();
     39         const llvm::Triple::ArchType cpu = target ? target->GetArchitecture().GetMachine() : llvm::Triple::UnknownArch;
     40 
     41         const char *exc_desc = NULL;
     42         const char *code_label = "code";
     43         const char *code_desc = NULL;
     44         const char *subcode_label = "subcode";
     45         const char *subcode_desc = NULL;
     46         switch (m_value)
     47         {
     48         case 1: // EXC_BAD_ACCESS
     49             exc_desc = "EXC_BAD_ACCESS";
     50             subcode_label = "address";
     51             switch (cpu)
     52             {
     53             case llvm::Triple::x86:
     54             case llvm::Triple::x86_64:
     55                 switch (m_exc_code)
     56                 {
     57                 case 0xd: code_desc = "EXC_I386_GPFLT"; m_exc_data_count = 1; break;
     58                 }
     59                 break;
     60             case llvm::Triple::arm:
     61                 switch (m_exc_code)
     62                 {
     63                 case 0x101: code_desc = "EXC_ARM_DA_ALIGN"; break;
     64                 case 0x102: code_desc = "EXC_ARM_DA_DEBUG"; break;
     65                 }
     66                 break;
     67 
     68             case llvm::Triple::ppc:
     69             case llvm::Triple::ppc64:
     70                 switch (m_exc_code)
     71                 {
     72                 case 0x101: code_desc = "EXC_PPC_VM_PROT_READ"; break;
     73                 case 0x102: code_desc = "EXC_PPC_BADSPACE";     break;
     74                 case 0x103: code_desc = "EXC_PPC_UNALIGNED";    break;
     75                 }
     76                 break;
     77 
     78             default:
     79                 break;
     80             }
     81             break;
     82 
     83         case 2: // EXC_BAD_INSTRUCTION
     84             exc_desc = "EXC_BAD_INSTRUCTION";
     85             switch (cpu)
     86             {
     87             case llvm::Triple::x86:
     88             case llvm::Triple::x86_64:
     89                 if (m_exc_code == 1)
     90                     code_desc = "EXC_I386_INVOP";
     91                 break;
     92 
     93             case llvm::Triple::ppc:
     94             case llvm::Triple::ppc64:
     95                 switch (m_exc_code)
     96                 {
     97                 case 1: code_desc = "EXC_PPC_INVALID_SYSCALL"; break;
     98                 case 2: code_desc = "EXC_PPC_UNIPL_INST"; break;
     99                 case 3: code_desc = "EXC_PPC_PRIVINST"; break;
    100                 case 4: code_desc = "EXC_PPC_PRIVREG"; break;
    101                 case 5: code_desc = "EXC_PPC_TRACE"; break;
    102                 case 6: code_desc = "EXC_PPC_PERFMON"; break;
    103                 }
    104                 break;
    105 
    106             case llvm::Triple::arm:
    107                 if (m_exc_code == 1)
    108                     code_desc = "EXC_ARM_UNDEFINED";
    109                 break;
    110 
    111             default:
    112                 break;
    113             }
    114             break;
    115 
    116         case 3: // EXC_ARITHMETIC
    117             exc_desc = "EXC_ARITHMETIC";
    118             switch (cpu)
    119             {
    120             case llvm::Triple::x86:
    121             case llvm::Triple::x86_64:
    122                 switch (m_exc_code)
    123                 {
    124                 case 1: code_desc = "EXC_I386_DIV"; break;
    125                 case 2: code_desc = "EXC_I386_INTO"; break;
    126                 case 3: code_desc = "EXC_I386_NOEXT"; break;
    127                 case 4: code_desc = "EXC_I386_EXTOVR"; break;
    128                 case 5: code_desc = "EXC_I386_EXTERR"; break;
    129                 case 6: code_desc = "EXC_I386_EMERR"; break;
    130                 case 7: code_desc = "EXC_I386_BOUND"; break;
    131                 case 8: code_desc = "EXC_I386_SSEEXTERR"; break;
    132                 }
    133                 break;
    134 
    135             case llvm::Triple::ppc:
    136             case llvm::Triple::ppc64:
    137                 switch (m_exc_code)
    138                 {
    139                 case 1: code_desc = "EXC_PPC_OVERFLOW"; break;
    140                 case 2: code_desc = "EXC_PPC_ZERO_DIVIDE"; break;
    141                 case 3: code_desc = "EXC_PPC_FLT_INEXACT"; break;
    142                 case 4: code_desc = "EXC_PPC_FLT_ZERO_DIVIDE"; break;
    143                 case 5: code_desc = "EXC_PPC_FLT_UNDERFLOW"; break;
    144                 case 6: code_desc = "EXC_PPC_FLT_OVERFLOW"; break;
    145                 case 7: code_desc = "EXC_PPC_FLT_NOT_A_NUMBER"; break;
    146                 }
    147                 break;
    148 
    149             default:
    150                 break;
    151             }
    152             break;
    153 
    154         case 4: // EXC_EMULATION
    155             exc_desc = "EXC_EMULATION";
    156             break;
    157 
    158 
    159         case 5: // EXC_SOFTWARE
    160             exc_desc = "EXC_SOFTWARE";
    161             if (m_exc_code == 0x10003)
    162             {
    163                 subcode_desc = "EXC_SOFT_SIGNAL";
    164                 subcode_label = "signo";
    165             }
    166             break;
    167 
    168         case 6: // EXC_BREAKPOINT
    169             {
    170                 exc_desc = "EXC_BREAKPOINT";
    171                 switch (cpu)
    172                 {
    173                 case llvm::Triple::x86:
    174                 case llvm::Triple::x86_64:
    175                     switch (m_exc_code)
    176                     {
    177                     case 1: code_desc = "EXC_I386_SGL"; break;
    178                     case 2: code_desc = "EXC_I386_BPT"; break;
    179                     }
    180                     break;
    181 
    182                 case llvm::Triple::ppc:
    183                 case llvm::Triple::ppc64:
    184                     switch (m_exc_code)
    185                     {
    186                     case 1: code_desc = "EXC_PPC_BREAKPOINT"; break;
    187                     }
    188                     break;
    189 
    190                 case llvm::Triple::arm:
    191                     switch (m_exc_code)
    192                     {
    193                     case 0x101: code_desc = "EXC_ARM_DA_ALIGN"; break;
    194                     case 0x102: code_desc = "EXC_ARM_DA_DEBUG"; break;
    195                     case 1: code_desc = "EXC_ARM_BREAKPOINT"; break;
    196                     // FIXME temporary workaround, exc_code 0 does not really mean EXC_ARM_BREAKPOINT
    197                     case 0: code_desc = "EXC_ARM_BREAKPOINT"; break;
    198                     }
    199                     break;
    200 
    201                 default:
    202                     break;
    203                 }
    204             }
    205             break;
    206 
    207         case 7:
    208             exc_desc = "EXC_SYSCALL";
    209             break;
    210 
    211         case 8:
    212             exc_desc = "EXC_MACH_SYSCALL";
    213             break;
    214 
    215         case 9:
    216             exc_desc = "EXC_RPC_ALERT";
    217             break;
    218 
    219         case 10:
    220             exc_desc = "EXC_CRASH";
    221             break;
    222         case 11:
    223             exc_desc = "EXC_RESOURCE";
    224             break;
    225         case 12:
    226             exc_desc = "EXC_GUARD";
    227             break;
    228         }
    229 
    230         StreamString strm;
    231 
    232         if (exc_desc)
    233             strm.PutCString(exc_desc);
    234         else
    235             strm.Printf("EXC_??? (%" PRIu64 ")", m_value);
    236 
    237         if (m_exc_data_count >= 1)
    238         {
    239             if (code_desc)
    240                 strm.Printf(" (%s=%s", code_label, code_desc);
    241             else
    242                 strm.Printf(" (%s=%" PRIu64, code_label, m_exc_code);
    243         }
    244 
    245         if (m_exc_data_count >= 2)
    246         {
    247             if (subcode_desc)
    248                 strm.Printf(", %s=%s", subcode_label, subcode_desc);
    249             else
    250                 strm.Printf(", %s=0x%" PRIx64, subcode_label, m_exc_subcode);
    251         }
    252 
    253         if (m_exc_data_count > 0)
    254             strm.PutChar(')');
    255 
    256         m_description.swap (strm.GetString());
    257     }
    258     return m_description.c_str();
    259 }
    260 
    261 
    262 
    263 
    264 
    265 StopInfoSP
    266 StopInfoMachException::CreateStopReasonWithMachException
    267 (
    268     Thread &thread,
    269     uint32_t exc_type,
    270     uint32_t exc_data_count,
    271     uint64_t exc_code,
    272     uint64_t exc_sub_code,
    273     uint64_t exc_sub_sub_code,
    274     bool pc_already_adjusted,
    275     bool adjust_pc_if_needed
    276 )
    277 {
    278     if (exc_type != 0)
    279     {
    280         uint32_t pc_decrement = 0;
    281         ExecutionContext exe_ctx (thread.shared_from_this());
    282         Target *target = exe_ctx.GetTargetPtr();
    283         const llvm::Triple::ArchType cpu = target ? target->GetArchitecture().GetMachine() : llvm::Triple::UnknownArch;
    284 
    285         switch (exc_type)
    286         {
    287         case 1: // EXC_BAD_ACCESS
    288             break;
    289 
    290         case 2: // EXC_BAD_INSTRUCTION
    291             switch (cpu)
    292             {
    293             case llvm::Triple::ppc:
    294             case llvm::Triple::ppc64:
    295                 switch (exc_code)
    296                 {
    297                 case 1: // EXC_PPC_INVALID_SYSCALL
    298                 case 2: // EXC_PPC_UNIPL_INST
    299                 case 3: // EXC_PPC_PRIVINST
    300                 case 4: // EXC_PPC_PRIVREG
    301                     break;
    302                 case 5: // EXC_PPC_TRACE
    303                     return StopInfo::CreateStopReasonToTrace (thread);
    304                 case 6: // EXC_PPC_PERFMON
    305                     break;
    306                 }
    307                 break;
    308 
    309             default:
    310                 break;
    311             }
    312             break;
    313 
    314         case 3: // EXC_ARITHMETIC
    315         case 4: // EXC_EMULATION
    316             break;
    317 
    318         case 5: // EXC_SOFTWARE
    319             if (exc_code == 0x10003) // EXC_SOFT_SIGNAL
    320             {
    321                 if (exc_sub_code == 5)
    322                 {
    323                     // On MacOSX, a SIGTRAP can signify that a process has called
    324                     // exec, so we should check with our dynamic loader to verify.
    325                     ProcessSP process_sp (thread.GetProcess());
    326                     if (process_sp)
    327                     {
    328                         DynamicLoader *dynamic_loader = process_sp->GetDynamicLoader();
    329                         if (dynamic_loader && dynamic_loader->ProcessDidExec())
    330                         {
    331                             // The program was re-exec'ed
    332                             return StopInfo::CreateStopReasonWithExec (thread);
    333                         }
    334 //                        if (!process_did_exec)
    335 //                        {
    336 //                            // We have a SIGTRAP, make sure we didn't exec by checking
    337 //                            // for the PC being at "_dyld_start"...
    338 //                            lldb::StackFrameSP frame_sp (thread.GetStackFrameAtIndex(0));
    339 //                            if (frame_sp)
    340 //                            {
    341 //                                const Symbol *symbol = frame_sp->GetSymbolContext(eSymbolContextSymbol).symbol;
    342 //                                if (symbol)
    343 //                                {
    344 //                                    if (symbol->GetName() == ConstString("_dyld_start"))
    345 //                                        process_did_exec = true;
    346 //                                }
    347 //                            }
    348 //                        }
    349                     }
    350                 }
    351                 return StopInfo::CreateStopReasonWithSignal (thread, exc_sub_code);
    352             }
    353             break;
    354 
    355         case 6: // EXC_BREAKPOINT
    356             {
    357                 bool is_actual_breakpoint = false;
    358                 bool is_trace_if_actual_breakpoint_missing = false;
    359                 switch (cpu)
    360                 {
    361                 case llvm::Triple::x86:
    362                 case llvm::Triple::x86_64:
    363                     if (exc_code == 1) // EXC_I386_SGL
    364                     {
    365                         if (!exc_sub_code)
    366                             return StopInfo::CreateStopReasonToTrace(thread);
    367 
    368                         // It's a watchpoint, then.
    369                         // The exc_sub_code indicates the data break address.
    370                         lldb::WatchpointSP wp_sp;
    371                         if (target)
    372                             wp_sp = target->GetWatchpointList().FindByAddress((lldb::addr_t)exc_sub_code);
    373                         if (wp_sp && wp_sp->IsEnabled())
    374                         {
    375                             // Debugserver may piggyback the hardware index of the fired watchpoint in the exception data.
    376                             // Set the hardware index if that's the case.
    377                             if (exc_data_count >=3)
    378                                 wp_sp->SetHardwareIndex((uint32_t)exc_sub_sub_code);
    379                             return StopInfo::CreateStopReasonWithWatchpointID(thread, wp_sp->GetID());
    380                         }
    381                     }
    382                     else if (exc_code == 2 ||   // EXC_I386_BPT
    383                              exc_code == 3)     // EXC_I386_BPTFLT
    384                     {
    385                         // KDP returns EXC_I386_BPTFLT for trace breakpoints
    386                         if (exc_code == 3)
    387                             is_trace_if_actual_breakpoint_missing = true;
    388 
    389                         is_actual_breakpoint = true;
    390                         if (!pc_already_adjusted)
    391                             pc_decrement = 1;
    392                     }
    393                     break;
    394 
    395                 case llvm::Triple::ppc:
    396                 case llvm::Triple::ppc64:
    397                     is_actual_breakpoint = exc_code == 1; // EXC_PPC_BREAKPOINT
    398                     break;
    399 
    400                 case llvm::Triple::arm:
    401                     if (exc_code == 0x102) // EXC_ARM_DA_DEBUG
    402                     {
    403                         // It's a watchpoint, then, if the exc_sub_code indicates a known/enabled
    404                         // data break address from our watchpoint list.
    405                         lldb::WatchpointSP wp_sp;
    406                         if (target)
    407                             wp_sp = target->GetWatchpointList().FindByAddress((lldb::addr_t)exc_sub_code);
    408                         if (wp_sp && wp_sp->IsEnabled())
    409                         {
    410                             // Debugserver may piggyback the hardware index of the fired watchpoint in the exception data.
    411                             // Set the hardware index if that's the case.
    412                             if (exc_data_count >=3)
    413                                 wp_sp->SetHardwareIndex((uint32_t)exc_sub_sub_code);
    414                             return StopInfo::CreateStopReasonWithWatchpointID(thread, wp_sp->GetID());
    415                         }
    416                         // EXC_ARM_DA_DEBUG seems to be reused for EXC_BREAKPOINT as well as EXC_BAD_ACCESS
    417                         if (thread.GetTemporaryResumeState() == eStateStepping)
    418                             return StopInfo::CreateStopReasonToTrace(thread);
    419                     }
    420                     else if (exc_code == 1) // EXC_ARM_BREAKPOINT
    421                     {
    422                         is_actual_breakpoint = true;
    423                         is_trace_if_actual_breakpoint_missing = true;
    424                     }
    425                     else if (exc_code == 0) // FIXME not EXC_ARM_BREAKPOINT but a kernel is currently returning this so accept it as indicating a breakpoint until the kernel is fixed
    426                     {
    427                         is_actual_breakpoint = true;
    428                         is_trace_if_actual_breakpoint_missing = true;
    429                     }
    430                     break;
    431 
    432                 default:
    433                     break;
    434                 }
    435 
    436                 if (is_actual_breakpoint)
    437                 {
    438                     RegisterContextSP reg_ctx_sp (thread.GetRegisterContext());
    439                     addr_t pc = reg_ctx_sp->GetPC() - pc_decrement;
    440 
    441                     ProcessSP process_sp (thread.CalculateProcess());
    442 
    443                     lldb::BreakpointSiteSP bp_site_sp;
    444                     if (process_sp)
    445                         bp_site_sp = process_sp->GetBreakpointSiteList().FindByAddress(pc);
    446                     if (bp_site_sp && bp_site_sp->IsEnabled())
    447                     {
    448                         // Update the PC if we were asked to do so, but only do
    449                         // so if we find a breakpoint that we know about cause
    450                         // this could be a trap instruction in the code
    451                         if (pc_decrement > 0 && adjust_pc_if_needed)
    452                             reg_ctx_sp->SetPC (pc);
    453 
    454                         // If the breakpoint is for this thread, then we'll report the hit, but if it is for another thread,
    455                         // we can just report no reason.  We don't need to worry about stepping over the breakpoint here, that
    456                         // will be taken care of when the thread resumes and notices that there's a breakpoint under the pc.
    457                         if (bp_site_sp->ValidForThisThread (&thread))
    458                             return StopInfo::CreateStopReasonWithBreakpointSiteID (thread, bp_site_sp->GetID());
    459                         else
    460                             return StopInfoSP();
    461                     }
    462 
    463                     // Don't call this a trace if we weren't single stepping this thread.
    464                     if (is_trace_if_actual_breakpoint_missing && thread.GetTemporaryResumeState() == eStateStepping)
    465                     {
    466                         return StopInfo::CreateStopReasonToTrace (thread);
    467                     }
    468                 }
    469             }
    470             break;
    471 
    472         case 7:     // EXC_SYSCALL
    473         case 8:     // EXC_MACH_SYSCALL
    474         case 9:     // EXC_RPC_ALERT
    475         case 10:    // EXC_CRASH
    476             break;
    477         }
    478 
    479         return StopInfoSP(new StopInfoMachException (thread, exc_type, exc_data_count, exc_code, exc_sub_code));
    480     }
    481     return StopInfoSP();
    482 }
    483