]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - source/Plugins/Process/Utility/StopInfoMachException.cpp
Import LLDB as of upstream SVN r225923 (git 2b588ecd)
[FreeBSD/FreeBSD.git] / source / Plugins / Process / Utility / StopInfoMachException.cpp
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                         {
367                             // This looks like a plain trap.
368                             // Have to check if there is a breakpoint here as well.  When you single-step onto a trap,
369                             // the single step stops you not to trap.  Since we also do that check below, let's just use
370                             // that logic.
371                             is_actual_breakpoint = true;
372                             is_trace_if_actual_breakpoint_missing = true;
373                         }
374                         else
375                         {
376
377                             // It's a watchpoint, then.
378                             // The exc_sub_code indicates the data break address.
379                             lldb::WatchpointSP wp_sp;
380                             if (target)
381                                 wp_sp = target->GetWatchpointList().FindByAddress((lldb::addr_t)exc_sub_code);
382                             if (wp_sp && wp_sp->IsEnabled())
383                             {
384                                 // Debugserver may piggyback the hardware index of the fired watchpoint in the exception data.
385                                 // Set the hardware index if that's the case.
386                                 if (exc_data_count >=3)
387                                     wp_sp->SetHardwareIndex((uint32_t)exc_sub_sub_code);
388                                 return StopInfo::CreateStopReasonWithWatchpointID(thread, wp_sp->GetID());
389                             }
390                         }
391                     }
392                     else if (exc_code == 2 ||   // EXC_I386_BPT
393                              exc_code == 3)     // EXC_I386_BPTFLT
394                     {
395                         // KDP returns EXC_I386_BPTFLT for trace breakpoints
396                         if (exc_code == 3)
397                             is_trace_if_actual_breakpoint_missing = true;
398
399                         is_actual_breakpoint = true;
400                         if (!pc_already_adjusted)
401                             pc_decrement = 1;
402                     }
403                     break;
404
405                 case llvm::Triple::ppc:
406                 case llvm::Triple::ppc64:
407                     is_actual_breakpoint = exc_code == 1; // EXC_PPC_BREAKPOINT
408                     break;
409                 
410                 case llvm::Triple::arm:
411                     if (exc_code == 0x102) // EXC_ARM_DA_DEBUG
412                     {
413                         // It's a watchpoint, then, if the exc_sub_code indicates a known/enabled
414                         // data break address from our watchpoint list.
415                         lldb::WatchpointSP wp_sp;
416                         if (target)
417                             wp_sp = target->GetWatchpointList().FindByAddress((lldb::addr_t)exc_sub_code);
418                         if (wp_sp && wp_sp->IsEnabled())
419                         {
420                             // Debugserver may piggyback the hardware index of the fired watchpoint in the exception data.
421                             // Set the hardware index if that's the case.
422                             if (exc_data_count >=3)
423                                 wp_sp->SetHardwareIndex((uint32_t)exc_sub_sub_code);
424                             return StopInfo::CreateStopReasonWithWatchpointID(thread, wp_sp->GetID());
425                         }
426                         else
427                         {
428                             is_actual_breakpoint = true;
429                             is_trace_if_actual_breakpoint_missing = true;
430                         }
431                     }
432                     else if (exc_code == 1) // EXC_ARM_BREAKPOINT
433                     {
434                         is_actual_breakpoint = true;
435                         is_trace_if_actual_breakpoint_missing = true;
436                     }
437                     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
438                     {
439                         is_actual_breakpoint = true;
440                         is_trace_if_actual_breakpoint_missing = true;
441                     }
442                     break;
443
444                 case llvm::Triple::aarch64:
445                 {
446                     if (exc_code == 1 && exc_sub_code == 0) // EXC_ARM_BREAKPOINT
447                     {
448                         // This is hit when we single instruction step aka MDSCR_EL1 SS bit 0 is set
449                         return StopInfo::CreateStopReasonToTrace(thread);
450                     }
451                     if (exc_code == 0x102) // EXC_ARM_DA_DEBUG
452                     {
453                         // It's a watchpoint, then, if the exc_sub_code indicates a known/enabled
454                         // data break address from our watchpoint list.
455                         lldb::WatchpointSP wp_sp;
456                         if (target)
457                             wp_sp = target->GetWatchpointList().FindByAddress((lldb::addr_t)exc_sub_code);
458                         if (wp_sp && wp_sp->IsEnabled())
459                         {
460                             // Debugserver may piggyback the hardware index of the fired watchpoint in the exception data.
461                             // Set the hardware index if that's the case.
462                             if (exc_data_count >= 3)
463                                 wp_sp->SetHardwareIndex((uint32_t)exc_sub_sub_code);
464                             return StopInfo::CreateStopReasonWithWatchpointID(thread, wp_sp->GetID());
465                         }
466                         // EXC_ARM_DA_DEBUG seems to be reused for EXC_BREAKPOINT as well as EXC_BAD_ACCESS
467                         if (thread.GetTemporaryResumeState() == eStateStepping)
468                             return StopInfo::CreateStopReasonToTrace(thread);
469                     }
470                     // It looks like exc_sub_code has the 4 bytes of the instruction that triggered the 
471                     // exception, i.e. our breakpoint opcode
472                     is_actual_breakpoint = exc_code == 1;
473                     break;
474                 }
475
476                 default:
477                     break;
478                 }
479
480                 if (is_actual_breakpoint)
481                 {
482                     RegisterContextSP reg_ctx_sp (thread.GetRegisterContext());
483                     addr_t pc = reg_ctx_sp->GetPC() - pc_decrement;
484
485                     ProcessSP process_sp (thread.CalculateProcess());
486
487                     lldb::BreakpointSiteSP bp_site_sp;
488                     if (process_sp)
489                         bp_site_sp = process_sp->GetBreakpointSiteList().FindByAddress(pc);
490                     if (bp_site_sp && bp_site_sp->IsEnabled())
491                     {
492                         // Update the PC if we were asked to do so, but only do
493                         // so if we find a breakpoint that we know about cause
494                         // this could be a trap instruction in the code
495                         if (pc_decrement > 0 && adjust_pc_if_needed)
496                             reg_ctx_sp->SetPC (pc);
497
498                         // If the breakpoint is for this thread, then we'll report the hit, but if it is for another thread,
499                         // we can just report no reason.  We don't need to worry about stepping over the breakpoint here, that
500                         // will be taken care of when the thread resumes and notices that there's a breakpoint under the pc.
501                         if (bp_site_sp->ValidForThisThread (&thread))
502                             return StopInfo::CreateStopReasonWithBreakpointSiteID (thread, bp_site_sp->GetID());
503                         else
504                             return StopInfoSP();
505                     }
506                     
507                     // Don't call this a trace if we weren't single stepping this thread.
508                     if (is_trace_if_actual_breakpoint_missing && thread.GetTemporaryResumeState() == eStateStepping)
509                     {
510                         return StopInfo::CreateStopReasonToTrace (thread);
511                     }
512                 }
513             }
514             break;
515
516         case 7:     // EXC_SYSCALL
517         case 8:     // EXC_MACH_SYSCALL
518         case 9:     // EXC_RPC_ALERT
519         case 10:    // EXC_CRASH
520             break;
521         }
522         
523         return StopInfoSP(new StopInfoMachException (thread, exc_type, exc_data_count, exc_code, exc_sub_code));
524     }
525     return StopInfoSP();
526 }