blob: 5999e13fb70364702cacec2bef27cff5475ee393 [file] [log] [blame]
Greg Claytonf4b47e12010-08-04 01:40:35 +00001//===-- 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
Johnny Chen01a67862011-10-14 00:42:25 +000016#include "lldb/Breakpoint/Watchpoint.h"
Greg Claytonf4b47e12010-08-04 01:40:35 +000017#include "lldb/Core/ArchSpec.h"
18#include "lldb/Core/StreamString.h"
Greg Clayton90ba8112012-12-05 00:16:59 +000019#include "lldb/Symbol/Symbol.h"
20#include "lldb/Target/DynamicLoader.h"
Greg Clayton1ac04c32012-02-21 00:09:25 +000021#include "lldb/Target/ExecutionContext.h"
Greg Claytonf4b47e12010-08-04 01:40:35 +000022#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
29using namespace lldb;
30using namespace lldb_private;
31
32const char *
33StopInfoMachException::GetDescription ()
34{
35 if (m_description.empty() && m_value != 0)
36 {
Greg Clayton1ac04c32012-02-21 00:09:25 +000037 ExecutionContext exe_ctx (m_thread.shared_from_this());
38 Target *target = exe_ctx.GetTargetPtr();
Greg Clayton0c90ef42012-02-21 18:40:07 +000039 const llvm::Triple::ArchType cpu = target ? target->GetArchitecture().GetMachine() : llvm::Triple::UnknownArch;
Greg Claytonf4b47e12010-08-04 01:40:35 +000040
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 {
Greg Clayton64195a22011-02-23 00:35:02 +000053 case llvm::Triple::arm:
Greg Claytonf4b47e12010-08-04 01:40:35 +000054 switch (m_exc_code)
55 {
56 case 0x101: code_desc = "EXC_ARM_DA_ALIGN"; break;
57 case 0x102: code_desc = "EXC_ARM_DA_DEBUG"; break;
58 }
59 break;
60
Greg Clayton64195a22011-02-23 00:35:02 +000061 case llvm::Triple::ppc:
62 case llvm::Triple::ppc64:
Greg Claytonf4b47e12010-08-04 01:40:35 +000063 switch (m_exc_code)
64 {
65 case 0x101: code_desc = "EXC_PPC_VM_PROT_READ"; break;
66 case 0x102: code_desc = "EXC_PPC_BADSPACE"; break;
67 case 0x103: code_desc = "EXC_PPC_UNALIGNED"; break;
68 }
69 break;
70
71 default:
72 break;
73 }
74 break;
75
76 case 2: // EXC_BAD_INSTRUCTION
77 exc_desc = "EXC_BAD_INSTRUCTION";
78 switch (cpu)
79 {
Greg Clayton64195a22011-02-23 00:35:02 +000080 case llvm::Triple::x86:
81 case llvm::Triple::x86_64:
Greg Claytonf4b47e12010-08-04 01:40:35 +000082 if (m_exc_code == 1)
83 code_desc = "EXC_I386_INVOP";
84 break;
85
Greg Clayton64195a22011-02-23 00:35:02 +000086 case llvm::Triple::ppc:
87 case llvm::Triple::ppc64:
Greg Claytonf4b47e12010-08-04 01:40:35 +000088 switch (m_exc_code)
89 {
90 case 1: code_desc = "EXC_PPC_INVALID_SYSCALL"; break;
91 case 2: code_desc = "EXC_PPC_UNIPL_INST"; break;
92 case 3: code_desc = "EXC_PPC_PRIVINST"; break;
93 case 4: code_desc = "EXC_PPC_PRIVREG"; break;
94 case 5: code_desc = "EXC_PPC_TRACE"; break;
95 case 6: code_desc = "EXC_PPC_PERFMON"; break;
96 }
97 break;
98
Greg Clayton64195a22011-02-23 00:35:02 +000099 case llvm::Triple::arm:
Greg Claytonf4b47e12010-08-04 01:40:35 +0000100 if (m_exc_code == 1)
101 code_desc = "EXC_ARM_UNDEFINED";
102 break;
103
104 default:
105 break;
106 }
107 break;
108
109 case 3: // EXC_ARITHMETIC
110 exc_desc = "EXC_ARITHMETIC";
111 switch (cpu)
112 {
Greg Clayton64195a22011-02-23 00:35:02 +0000113 case llvm::Triple::x86:
114 case llvm::Triple::x86_64:
Greg Claytonf4b47e12010-08-04 01:40:35 +0000115 switch (m_exc_code)
116 {
117 case 1: code_desc = "EXC_I386_DIV"; break;
118 case 2: code_desc = "EXC_I386_INTO"; break;
119 case 3: code_desc = "EXC_I386_NOEXT"; break;
120 case 4: code_desc = "EXC_I386_EXTOVR"; break;
121 case 5: code_desc = "EXC_I386_EXTERR"; break;
122 case 6: code_desc = "EXC_I386_EMERR"; break;
123 case 7: code_desc = "EXC_I386_BOUND"; break;
124 case 8: code_desc = "EXC_I386_SSEEXTERR"; break;
125 }
126 break;
127
Greg Clayton64195a22011-02-23 00:35:02 +0000128 case llvm::Triple::ppc:
129 case llvm::Triple::ppc64:
Greg Claytonf4b47e12010-08-04 01:40:35 +0000130 switch (m_exc_code)
131 {
132 case 1: code_desc = "EXC_PPC_OVERFLOW"; break;
133 case 2: code_desc = "EXC_PPC_ZERO_DIVIDE"; break;
134 case 3: code_desc = "EXC_PPC_FLT_INEXACT"; break;
135 case 4: code_desc = "EXC_PPC_FLT_ZERO_DIVIDE"; break;
136 case 5: code_desc = "EXC_PPC_FLT_UNDERFLOW"; break;
137 case 6: code_desc = "EXC_PPC_FLT_OVERFLOW"; break;
138 case 7: code_desc = "EXC_PPC_FLT_NOT_A_NUMBER"; break;
139 }
140 break;
141
142 default:
143 break;
144 }
145 break;
146
147 case 4: // EXC_EMULATION
148 exc_desc = "EXC_EMULATION";
149 break;
150
151
152 case 5: // EXC_SOFTWARE
153 exc_desc = "EXC_SOFTWARE";
154 if (m_exc_code == 0x10003)
155 {
156 subcode_desc = "EXC_SOFT_SIGNAL";
157 subcode_label = "signo";
158 }
159 break;
160
161 case 6: // EXC_BREAKPOINT
162 {
163 exc_desc = "EXC_BREAKPOINT";
164 switch (cpu)
165 {
Greg Clayton64195a22011-02-23 00:35:02 +0000166 case llvm::Triple::x86:
167 case llvm::Triple::x86_64:
Greg Claytonf4b47e12010-08-04 01:40:35 +0000168 switch (m_exc_code)
169 {
Greg Clayton32e0a752011-03-30 18:16:51 +0000170 case 1: code_desc = "EXC_I386_SGL"; break;
171 case 2: code_desc = "EXC_I386_BPT"; break;
Greg Claytonf4b47e12010-08-04 01:40:35 +0000172 }
173 break;
174
Greg Clayton64195a22011-02-23 00:35:02 +0000175 case llvm::Triple::ppc:
176 case llvm::Triple::ppc64:
Greg Claytonf4b47e12010-08-04 01:40:35 +0000177 switch (m_exc_code)
178 {
Greg Clayton32e0a752011-03-30 18:16:51 +0000179 case 1: code_desc = "EXC_PPC_BREAKPOINT"; break;
Greg Claytonf4b47e12010-08-04 01:40:35 +0000180 }
181 break;
182
Greg Clayton64195a22011-02-23 00:35:02 +0000183 case llvm::Triple::arm:
Greg Claytonf4b47e12010-08-04 01:40:35 +0000184 switch (m_exc_code)
185 {
Greg Clayton8d400e172011-05-23 18:04:09 +0000186 case 0x101: code_desc = "EXC_ARM_DA_ALIGN"; break;
187 case 0x102: code_desc = "EXC_ARM_DA_DEBUG"; break;
Greg Clayton32e0a752011-03-30 18:16:51 +0000188 case 1: code_desc = "EXC_ARM_BREAKPOINT"; break;
Greg Claytonf4b47e12010-08-04 01:40:35 +0000189 }
190 break;
191
192 default:
193 break;
194 }
195 }
196 break;
197
198 case 7:
199 exc_desc = "EXC_SYSCALL";
200 break;
201
202 case 8:
203 exc_desc = "EXC_MACH_SYSCALL";
204 break;
205
206 case 9:
207 exc_desc = "EXC_RPC_ALERT";
208 break;
209
210 case 10:
211 exc_desc = "EXC_CRASH";
212 break;
213 }
214
215 StreamString strm;
216
217 if (exc_desc)
218 strm.PutCString(exc_desc);
219 else
Daniel Malead01b2952012-11-29 21:49:15 +0000220 strm.Printf("EXC_??? (%" PRIu64 ")", m_value);
Greg Claytonf4b47e12010-08-04 01:40:35 +0000221
222 if (m_exc_data_count >= 1)
223 {
224 if (code_desc)
225 strm.Printf(" (%s=%s", code_label, code_desc);
226 else
Daniel Malead01b2952012-11-29 21:49:15 +0000227 strm.Printf(" (%s=%" PRIu64, code_label, m_exc_code);
Greg Claytonf4b47e12010-08-04 01:40:35 +0000228 }
229
230 if (m_exc_data_count >= 2)
231 {
232 if (subcode_desc)
233 strm.Printf(", %s=%s", subcode_label, subcode_desc);
234 else
Daniel Malead01b2952012-11-29 21:49:15 +0000235 strm.Printf(", %s=0x%" PRIx64, subcode_label, m_exc_subcode);
Greg Claytonf4b47e12010-08-04 01:40:35 +0000236 }
237
238 if (m_exc_data_count > 0)
239 strm.PutChar(')');
240
241 m_description.swap (strm.GetString());
242 }
243 return m_description.c_str();
244}
245
246
Greg Clayton97d5cf02012-09-25 02:40:06 +0000247
248
249
Greg Claytonf4b47e12010-08-04 01:40:35 +0000250StopInfoSP
251StopInfoMachException::CreateStopReasonWithMachException
252(
253 Thread &thread,
254 uint32_t exc_type,
255 uint32_t exc_data_count,
256 uint64_t exc_code,
Johnny Chen236888d2011-09-17 01:05:03 +0000257 uint64_t exc_sub_code,
Greg Clayton97d5cf02012-09-25 02:40:06 +0000258 uint64_t exc_sub_sub_code,
259 bool pc_already_adjusted,
260 bool adjust_pc_if_needed
Greg Claytonf4b47e12010-08-04 01:40:35 +0000261)
262{
263 if (exc_type != 0)
264 {
Greg Clayton97d5cf02012-09-25 02:40:06 +0000265 uint32_t pc_decrement = 0;
Greg Clayton1ac04c32012-02-21 00:09:25 +0000266 ExecutionContext exe_ctx (thread.shared_from_this());
267 Target *target = exe_ctx.GetTargetPtr();
Greg Clayton0c90ef42012-02-21 18:40:07 +0000268 const llvm::Triple::ArchType cpu = target ? target->GetArchitecture().GetMachine() : llvm::Triple::UnknownArch;
Greg Claytonf4b47e12010-08-04 01:40:35 +0000269
270 switch (exc_type)
271 {
272 case 1: // EXC_BAD_ACCESS
273 break;
274
275 case 2: // EXC_BAD_INSTRUCTION
276 switch (cpu)
277 {
Greg Clayton64195a22011-02-23 00:35:02 +0000278 case llvm::Triple::ppc:
279 case llvm::Triple::ppc64:
Greg Claytonf4b47e12010-08-04 01:40:35 +0000280 switch (exc_code)
281 {
282 case 1: // EXC_PPC_INVALID_SYSCALL
283 case 2: // EXC_PPC_UNIPL_INST
284 case 3: // EXC_PPC_PRIVINST
285 case 4: // EXC_PPC_PRIVREG
286 break;
287 case 5: // EXC_PPC_TRACE
288 return StopInfo::CreateStopReasonToTrace (thread);
289 case 6: // EXC_PPC_PERFMON
290 break;
291 }
292 break;
293
294 default:
295 break;
296 }
297 break;
298
299 case 3: // EXC_ARITHMETIC
300 case 4: // EXC_EMULATION
301 break;
302
303 case 5: // EXC_SOFTWARE
304 if (exc_code == 0x10003) // EXC_SOFT_SIGNAL
Greg Clayton90ba8112012-12-05 00:16:59 +0000305 {
306 if (exc_sub_code == 5)
307 {
308 // On MacOSX, a SIGTRAP can signify that a process has called
309 // exec, so we should check with our dynamic loader to verify.
310 ProcessSP process_sp (thread.GetProcess());
311 if (process_sp)
312 {
313 DynamicLoader *dynamic_loader = process_sp->GetDynamicLoader();
314 if (dynamic_loader && dynamic_loader->ProcessDidExec())
315 {
316 // The program was re-exec'ed
317 return StopInfo::CreateStopReasonWithExec (thread);
318 }
319// if (!process_did_exec)
320// {
321// // We have a SIGTRAP, make sure we didn't exec by checking
322// // for the PC being at "_dyld_start"...
323// lldb::StackFrameSP frame_sp (thread.GetStackFrameAtIndex(0));
324// if (frame_sp)
325// {
326// const Symbol *symbol = frame_sp->GetSymbolContext(eSymbolContextSymbol).symbol;
327// if (symbol)
328// {
329// if (symbol->GetName() == ConstString("_dyld_start"))
330// process_did_exec = true;
331// }
332// }
333// }
334 }
335 }
Greg Claytonf4b47e12010-08-04 01:40:35 +0000336 return StopInfo::CreateStopReasonWithSignal (thread, exc_sub_code);
Greg Clayton90ba8112012-12-05 00:16:59 +0000337 }
Greg Claytonf4b47e12010-08-04 01:40:35 +0000338 break;
339
340 case 6: // EXC_BREAKPOINT
341 {
342 bool is_software_breakpoint = false;
Greg Clayton97d5cf02012-09-25 02:40:06 +0000343 bool is_trace_if_software_breakpoint_missing = false;
Greg Claytonf4b47e12010-08-04 01:40:35 +0000344 switch (cpu)
345 {
Greg Clayton64195a22011-02-23 00:35:02 +0000346 case llvm::Triple::x86:
347 case llvm::Triple::x86_64:
Greg Claytonf4b47e12010-08-04 01:40:35 +0000348 if (exc_code == 1) // EXC_I386_SGL
349 {
Johnny Chen47f43da2011-09-08 20:52:34 +0000350 if (!exc_sub_code)
351 return StopInfo::CreateStopReasonToTrace(thread);
352
353 // It's a watchpoint, then.
Johnny Chen236888d2011-09-17 01:05:03 +0000354 // The exc_sub_code indicates the data break address.
Greg Clayton1ac04c32012-02-21 00:09:25 +0000355 lldb::WatchpointSP wp_sp;
356 if (target)
357 wp_sp = target->GetWatchpointList().FindByAddress((lldb::addr_t)exc_sub_code);
Johnny Chen72ee62e2012-03-21 18:28:25 +0000358 if (wp_sp && wp_sp->IsEnabled())
Johnny Chen236888d2011-09-17 01:05:03 +0000359 {
360 // Debugserver may piggyback the hardware index of the fired watchpoint in the exception data.
361 // Set the hardware index if that's the case.
362 if (exc_data_count >=3)
Johnny Chen01a67862011-10-14 00:42:25 +0000363 wp_sp->SetHardwareIndex((uint32_t)exc_sub_sub_code);
364 return StopInfo::CreateStopReasonWithWatchpointID(thread, wp_sp->GetID());
Johnny Chen236888d2011-09-17 01:05:03 +0000365 }
Greg Claytonf4b47e12010-08-04 01:40:35 +0000366 }
Greg Clayton97d5cf02012-09-25 02:40:06 +0000367 else if (exc_code == 2 || // EXC_I386_BPT
368 exc_code == 3) // EXC_I386_BPTFLT
Greg Claytonf4b47e12010-08-04 01:40:35 +0000369 {
Greg Clayton97d5cf02012-09-25 02:40:06 +0000370 // KDP returns EXC_I386_BPTFLT for trace breakpoints
371 if (exc_code == 3)
372 is_trace_if_software_breakpoint_missing = true;
373
Greg Claytonf4b47e12010-08-04 01:40:35 +0000374 is_software_breakpoint = true;
Greg Clayton97d5cf02012-09-25 02:40:06 +0000375 if (!pc_already_adjusted)
376 pc_decrement = 1;
Greg Claytonf4b47e12010-08-04 01:40:35 +0000377 }
378 break;
379
Greg Clayton64195a22011-02-23 00:35:02 +0000380 case llvm::Triple::ppc:
381 case llvm::Triple::ppc64:
Greg Claytonf4b47e12010-08-04 01:40:35 +0000382 is_software_breakpoint = exc_code == 1; // EXC_PPC_BREAKPOINT
383 break;
384
Greg Clayton64195a22011-02-23 00:35:02 +0000385 case llvm::Triple::arm:
Greg Clayton8d400e172011-05-23 18:04:09 +0000386 if (exc_code == 0x102)
387 {
Johnny Chen72ee62e2012-03-21 18:28:25 +0000388 // It's a watchpoint, then, if the exc_sub_code indicates a known/enabled
389 // data break address from our watchpoint list.
390 lldb::WatchpointSP wp_sp;
391 if (target)
392 wp_sp = target->GetWatchpointList().FindByAddress((lldb::addr_t)exc_sub_code);
393 if (wp_sp && wp_sp->IsEnabled())
394 {
395 // Debugserver may piggyback the hardware index of the fired watchpoint in the exception data.
396 // Set the hardware index if that's the case.
397 if (exc_data_count >=3)
398 wp_sp->SetHardwareIndex((uint32_t)exc_sub_sub_code);
399 return StopInfo::CreateStopReasonWithWatchpointID(thread, wp_sp->GetID());
400 }
Greg Clayton8d400e172011-05-23 18:04:09 +0000401 // EXC_ARM_DA_DEBUG seems to be reused for EXC_BREAKPOINT as well as EXC_BAD_ACCESS
Jim Ingham4dc613b2012-10-27 02:52:04 +0000402 if (thread.GetTemporaryResumeState() == eStateStepping)
403 return StopInfo::CreateStopReasonToTrace(thread);
Greg Clayton8d400e172011-05-23 18:04:09 +0000404 }
Greg Clayton97d5cf02012-09-25 02:40:06 +0000405 else if (exc_code == 1)
406 {
407 is_software_breakpoint = true;
408 is_trace_if_software_breakpoint_missing = true;
409 }
Greg Claytonf4b47e12010-08-04 01:40:35 +0000410 break;
411
412 default:
413 break;
414 }
415
416 if (is_software_breakpoint)
417 {
Greg Clayton97d5cf02012-09-25 02:40:06 +0000418 RegisterContextSP reg_ctx_sp (thread.GetRegisterContext());
419 addr_t pc = reg_ctx_sp->GetPC() - pc_decrement;
420
Greg Clayton1ac04c32012-02-21 00:09:25 +0000421 ProcessSP process_sp (thread.CalculateProcess());
422
423 lldb::BreakpointSiteSP bp_site_sp;
424 if (process_sp)
425 bp_site_sp = process_sp->GetBreakpointSiteList().FindByAddress(pc);
Greg Clayton97d5cf02012-09-25 02:40:06 +0000426 if (bp_site_sp && bp_site_sp->IsEnabled())
Greg Claytonf4b47e12010-08-04 01:40:35 +0000427 {
Greg Clayton97d5cf02012-09-25 02:40:06 +0000428 // Update the PC if we were asked to do so, but only do
429 // so if we find a breakpoint that we know about cause
430 // this could be a trap instruction in the code
431 if (pc_decrement > 0 && adjust_pc_if_needed)
432 reg_ctx_sp->SetPC (pc);
433
Jim Ingham65a0e592010-10-22 23:28:32 +0000434 // If the breakpoint is for this thread, then we'll report the hit, but if it is for another thread,
435 // we can just report no reason. We don't need to worry about stepping over the breakpoint here, that
436 // will be taken care of when the thread resumes and notices that there's a breakpoint under the pc.
Greg Claytonf4b47e12010-08-04 01:40:35 +0000437 if (bp_site_sp->ValidForThisThread (&thread))
438 return StopInfo::CreateStopReasonWithBreakpointSiteID (thread, bp_site_sp->GetID());
Jim Ingham65a0e592010-10-22 23:28:32 +0000439 else
440 return StopInfoSP();
Greg Claytonf4b47e12010-08-04 01:40:35 +0000441 }
Greg Clayton97d5cf02012-09-25 02:40:06 +0000442
Jim Ingham4dc613b2012-10-27 02:52:04 +0000443 // Don't call this a trace if we weren't single stepping this thread.
444 if (is_trace_if_software_breakpoint_missing && thread.GetTemporaryResumeState() == eStateStepping)
Greg Clayton32e0a752011-03-30 18:16:51 +0000445 {
446 return StopInfo::CreateStopReasonToTrace (thread);
447 }
Greg Claytonf4b47e12010-08-04 01:40:35 +0000448 }
449 }
450 break;
451
452 case 7: // EXC_SYSCALL
453 case 8: // EXC_MACH_SYSCALL
454 case 9: // EXC_RPC_ALERT
455 case 10: // EXC_CRASH
456 break;
457 }
458
459 return StopInfoSP(new StopInfoMachException (thread, exc_type, exc_data_count, exc_code, exc_sub_code));
460 }
461 return StopInfoSP();
462}