LLDB mainline
StopInfoMachException.cpp
Go to the documentation of this file.
1//===-- StopInfoMachException.cpp -----------------------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
10
11#include "lldb/lldb-forward.h"
12
13#if defined(__APPLE__)
14// Needed for the EXC_RESOURCE interpretation macros
15#include <kern/exc_resource.h>
16#endif
17
19#include "lldb/Symbol/Symbol.h"
20#include "lldb/Target/ABI.h"
23#include "lldb/Target/Process.h"
25#include "lldb/Target/Target.h"
26#include "lldb/Target/Thread.h"
30#include "lldb/Utility/Log.h"
32#include <optional>
33
34using namespace lldb;
35using namespace lldb_private;
36
37/// Information about a pointer-authentication related instruction.
43
44/// Get any pointer-authentication related information about the instruction
45/// at address \p at_addr.
46static std::optional<PtrauthInstructionInfo>
48 const Address &at_addr) {
49 const char *plugin_name = nullptr;
50 const char *flavor = nullptr;
51 const char *cpu = nullptr;
52 const char *features = nullptr;
53 AddressRange range_bounds(at_addr, 4);
54 const bool prefer_file_cache = true;
55 DisassemblerSP disassembler_sp =
56 Disassembler::DisassembleRange(arch, plugin_name, flavor, cpu, features,
57 target, range_bounds, prefer_file_cache);
58 if (!disassembler_sp)
59 return std::nullopt;
60
61 InstructionList &insn_list = disassembler_sp->GetInstructionList();
62 InstructionSP insn = insn_list.GetInstructionAtIndex(0);
63 if (!insn)
64 return std::nullopt;
65
66 return PtrauthInstructionInfo{insn->IsAuthenticated(), insn->IsLoad(),
67 insn->DoesBranch()};
68}
69
70/// Describe the load address of \p addr using the format filename:line:col.
71static void DescribeAddressBriefly(Stream &strm, const Address &addr,
72 Target &target) {
73 strm.Printf("at address=0x%" PRIx64, addr.GetLoadAddress(&target));
75 if (addr.GetDescription(s, target, eDescriptionLevelBrief))
76 strm.Printf(" %s", s.GetString().data());
77 strm.Printf(".\n");
78}
79
80std::optional<addr_t> StopInfoMachException::GetTagFaultAddress() const {
81 const bool bad_access =
82 (m_value == 1 || m_value == 12); // EXC_BAD_ACCESS or EXC_GUARD
83 const bool tag_fault = (m_exc_code == 0x106); // EXC_ARM_MTE_TAG_FAULT
84 // Whether the subcode (m_exc_subcode) holds the fault address.
85 const bool has_fault_addr = (m_exc_data_count >= 2);
86
87 if (bad_access && tag_fault && has_fault_addr)
88 return m_exc_subcode; // The subcode is the fault address.
89
90 return std::nullopt;
91}
92
93static constexpr uint8_t g_mte_tag_shift = 64 - 8;
94static constexpr addr_t g_mte_tag_mask = (addr_t)0x0f << g_mte_tag_shift;
95
97 std::optional<addr_t> fault_address = GetTagFaultAddress();
98 if (!fault_address)
99 return false;
100
101 const uint64_t bad_address = *fault_address;
102
103 StreamString strm;
104 strm.Printf("EXC_ARM_MTE_TAG_FAULT (code=%" PRIu64 ", address=0x%" PRIx64
105 ")\n",
106 m_exc_code, bad_address);
107
108 const uint8_t tag = (bad_address & g_mte_tag_mask) >> g_mte_tag_shift;
109 const addr_t canonical_addr = bad_address & ~g_mte_tag_mask;
110 strm.Printf(
111 "Note: MTE tag mismatch detected: pointer tag=%d, address=0x%" PRIx64,
112 tag, canonical_addr);
113 m_description = std::string(strm.GetString());
114
115 return true;
116}
117
119 bool IsBreakpoint = m_value == 6; // EXC_BREAKPOINT
120 bool IsBadAccess = m_value == 1; // EXC_BAD_ACCESS
121 if (!IsBreakpoint && !IsBadAccess)
122 return false;
123
124 // Check that we have a live process.
125 if (!exe_ctx.HasProcessScope() || !exe_ctx.HasThreadScope() ||
126 !exe_ctx.HasTargetScope())
127 return false;
128
129 Thread &thread = *exe_ctx.GetThreadPtr();
130 StackFrameSP current_frame = thread.GetStackFrameAtIndex(0);
131 if (!current_frame)
132 return false;
133
134 Target &target = *exe_ctx.GetTargetPtr();
135 Process &process = *exe_ctx.GetProcessPtr();
136 const ArchSpec &arch = target.GetArchitecture();
137
138 // Check for a ptrauth-enabled target.
139 const bool ptrauth_enabled_target =
141 if (!ptrauth_enabled_target)
142 return false;
143
144 // Set up a stream we can write a diagnostic into.
145 StreamString strm;
146 auto emit_ptrauth_prologue = [&](uint64_t at_address) {
147 strm.Printf("EXC_BAD_ACCESS (code=%" PRIu64 ", address=0x%" PRIx64 ")\n",
148 m_exc_code, at_address);
149 strm.Printf("Note: Possible pointer authentication failure detected.\n");
150 };
151
152 ABISP abi_sp = process.GetABI();
153 assert(abi_sp && "Missing ABI info");
154
155 // Check if we have a "brk 0xc47x" trap, where the value that failed to
156 // authenticate is in x16.
157 Address current_address = current_frame->GetFrameCodeAddress();
158 if (IsBreakpoint) {
159 RegisterContext *reg_ctx = exe_ctx.GetRegisterContext();
160 if (!reg_ctx)
161 return false;
162
163 const RegisterInfo *X16Info = reg_ctx->GetRegisterInfoByName("x16");
164 RegisterValue X16Val;
165 if (!reg_ctx->ReadRegister(X16Info, X16Val))
166 return false;
167 uint64_t bad_address = X16Val.GetAsUInt64();
168
169 uint64_t fixed_bad_address = abi_sp->FixCodeAddress(bad_address);
170 Address brk_address;
171 if (!target.ResolveLoadAddress(fixed_bad_address, brk_address))
172 return false;
173
174 auto brk_ptrauth_info =
175 GetPtrauthInstructionInfo(target, arch, current_address);
176 if (brk_ptrauth_info && brk_ptrauth_info->IsAuthenticated) {
177 emit_ptrauth_prologue(bad_address);
178 strm.Printf("Found value that failed to authenticate ");
179 DescribeAddressBriefly(strm, brk_address, target);
180 m_description = std::string(strm.GetString());
181 return true;
182 }
183 return false;
184 }
185
186 assert(IsBadAccess && "Handle EXC_BAD_ACCESS only after this point");
187
188 // Check that we have the "bad address" from an EXC_BAD_ACCESS.
189 if (m_exc_data_count < 2)
190 return false;
191
192 // Ok, we know the Target is valid and that it describes a ptrauth-enabled
193 // device. Now, we need to determine whether this exception was caused by a
194 // ptrauth failure.
195
196 uint64_t bad_address = m_exc_subcode;
197 uint64_t fixed_bad_address = abi_sp->FixCodeAddress(bad_address);
198 uint64_t current_pc = current_address.GetLoadAddress(&target);
199
200 // Detect: LDRAA, LDRAB (Load Register, with pointer authentication).
201 //
202 // If an authenticated load results in an exception, the instruction at the
203 // current PC should be one of LDRAx.
204 if (bad_address != current_pc && fixed_bad_address != current_pc) {
205 auto ptrauth_info =
206 GetPtrauthInstructionInfo(target, arch, current_address);
207 if (ptrauth_info && ptrauth_info->IsAuthenticated && ptrauth_info->IsLoad) {
208 emit_ptrauth_prologue(bad_address);
209 strm.Printf("Found authenticated load instruction ");
210 DescribeAddressBriefly(strm, current_address, target);
211 m_description = std::string(strm.GetString());
212 return true;
213 }
214 }
215
216 // Detect: BLRAA, BLRAAZ, BLRAB, BLRABZ (Branch with Link to Register, with
217 // pointer authentication).
218 //
219 // TODO: Detect: BRAA, BRAAZ, BRAB, BRABZ (Branch to Register, with pointer
220 // authentication). At a minimum, this requires call site info support for
221 // indirect calls.
222 //
223 // If an authenticated call or tail call results in an exception, stripping
224 // the bad address should give the current PC, which points to the address
225 // we tried to branch to.
226 if (bad_address != current_pc && fixed_bad_address == current_pc) {
227 if (StackFrameSP parent_frame = thread.GetStackFrameAtIndex(1)) {
228 addr_t return_pc =
229 parent_frame->GetFrameCodeAddress().GetLoadAddress(&target);
230 Address blr_address;
231 if (!target.ResolveLoadAddress(return_pc - 4, blr_address))
232 return false;
233
234 auto blr_ptrauth_info =
235 GetPtrauthInstructionInfo(target, arch, blr_address);
236 if (blr_ptrauth_info && blr_ptrauth_info->IsAuthenticated &&
237 blr_ptrauth_info->DoesBranch) {
238 emit_ptrauth_prologue(bad_address);
239 strm.Printf("Found authenticated indirect branch ");
240 DescribeAddressBriefly(strm, blr_address, target);
241 m_description = std::string(strm.GetString());
242 return true;
243 }
244 }
245 }
246
247 // TODO: Detect: RETAA, RETAB (Return from subroutine, with pointer
248 // authentication).
249 //
250 // Is there a motivating, non-malicious code snippet that corrupts LR?
251
252 return false;
253}
254
256 if (!m_description.empty())
257 return m_description.c_str();
259 return "invalid stop reason!";
260
261 ExecutionContext exe_ctx(m_thread_wp.lock());
262 Target *target = exe_ctx.GetTargetPtr();
263 const llvm::Triple::ArchType cpu =
264 target ? target->GetArchitecture().GetMachine()
265 : llvm::Triple::UnknownArch;
266
267 const char *exc_desc = nullptr;
268 const char *code_label = "code";
269 const char *code_desc = nullptr;
270 const char *subcode_label = "subcode";
271 const char *subcode_desc = nullptr;
272
273#if defined(__APPLE__)
274 char code_desc_buf[32];
275 char subcode_desc_buf[32];
276#endif
277
278 switch (m_value) {
279 case 1: // EXC_BAD_ACCESS
280 exc_desc = "EXC_BAD_ACCESS";
281 subcode_label = "address";
282 switch (cpu) {
283 case llvm::Triple::x86:
284 case llvm::Triple::x86_64:
285 switch (m_exc_code) {
286 case 0xd:
287 code_desc = "EXC_I386_GPFLT";
289 break;
290 }
291 break;
292 case llvm::Triple::arm:
293 case llvm::Triple::thumb:
294 switch (m_exc_code) {
295 case 0x101:
296 code_desc = "EXC_ARM_DA_ALIGN";
297 break;
298 case 0x102:
299 code_desc = "EXC_ARM_DA_DEBUG";
300 break;
301 }
302 break;
303
304 case llvm::Triple::aarch64:
305 if (DeterminePtrauthFailure(exe_ctx))
306 return m_description.c_str();
308 return m_description.c_str();
309 break;
310
311 default:
312 break;
313 }
314 break;
315
316 case 2: // EXC_BAD_INSTRUCTION
317 exc_desc = "EXC_BAD_INSTRUCTION";
318 switch (cpu) {
319 case llvm::Triple::x86:
320 case llvm::Triple::x86_64:
321 if (m_exc_code == 1)
322 code_desc = "EXC_I386_INVOP";
323 break;
324
325 case llvm::Triple::arm:
326 case llvm::Triple::thumb:
327 if (m_exc_code == 1)
328 code_desc = "EXC_ARM_UNDEFINED";
329 break;
330
331 default:
332 break;
333 }
334 break;
335
336 case 3: // EXC_ARITHMETIC
337 exc_desc = "EXC_ARITHMETIC";
338 switch (cpu) {
339 case llvm::Triple::x86:
340 case llvm::Triple::x86_64:
341 switch (m_exc_code) {
342 case 1:
343 code_desc = "EXC_I386_DIV";
344 break;
345 case 2:
346 code_desc = "EXC_I386_INTO";
347 break;
348 case 3:
349 code_desc = "EXC_I386_NOEXT";
350 break;
351 case 4:
352 code_desc = "EXC_I386_EXTOVR";
353 break;
354 case 5:
355 code_desc = "EXC_I386_EXTERR";
356 break;
357 case 6:
358 code_desc = "EXC_I386_EMERR";
359 break;
360 case 7:
361 code_desc = "EXC_I386_BOUND";
362 break;
363 case 8:
364 code_desc = "EXC_I386_SSEEXTERR";
365 break;
366 }
367 break;
368
369 default:
370 break;
371 }
372 break;
373
374 case 4: // EXC_EMULATION
375 exc_desc = "EXC_EMULATION";
376 break;
377
378 case 5: // EXC_SOFTWARE
379 exc_desc = "EXC_SOFTWARE";
380 if (m_exc_code == 0x10003) {
381 subcode_desc = "EXC_SOFT_SIGNAL";
382 subcode_label = "signo";
383 }
384 break;
385
386 case 6: // EXC_BREAKPOINT
387 {
388 exc_desc = "EXC_BREAKPOINT";
389 switch (cpu) {
390 case llvm::Triple::x86:
391 case llvm::Triple::x86_64:
392 switch (m_exc_code) {
393 case 1:
394 code_desc = "EXC_I386_SGL";
395 break;
396 case 2:
397 code_desc = "EXC_I386_BPT";
398 break;
399 }
400 break;
401
402 case llvm::Triple::arm:
403 case llvm::Triple::thumb:
404 switch (m_exc_code) {
405 case 0x101:
406 code_desc = "EXC_ARM_DA_ALIGN";
407 break;
408 case 0x102:
409 code_desc = "EXC_ARM_DA_DEBUG";
410 break;
411 case 1:
412 code_desc = "EXC_ARM_BREAKPOINT";
413 break;
414 // FIXME temporary workaround, exc_code 0 does not really mean
415 // EXC_ARM_BREAKPOINT
416 case 0:
417 code_desc = "EXC_ARM_BREAKPOINT";
418 break;
419 }
420 break;
421
422 case llvm::Triple::aarch64:
423 if (DeterminePtrauthFailure(exe_ctx))
424 return m_description.c_str();
425 break;
426
427 default:
428 break;
429 }
430 } break;
431
432 case 7:
433 exc_desc = "EXC_SYSCALL";
434 break;
435
436 case 8:
437 exc_desc = "EXC_MACH_SYSCALL";
438 break;
439
440 case 9:
441 exc_desc = "EXC_RPC_ALERT";
442 break;
443
444 case 10:
445 exc_desc = "EXC_CRASH";
446 break;
447 case 11:
448 exc_desc = "EXC_RESOURCE";
449#if defined(__APPLE__)
450 {
451 int resource_type = EXC_RESOURCE_DECODE_RESOURCE_TYPE(m_exc_code);
452
453 code_label = "limit";
454 code_desc = code_desc_buf;
455 subcode_label = "observed";
456 subcode_desc = subcode_desc_buf;
457
458 switch (resource_type) {
459 case RESOURCE_TYPE_CPU:
460 exc_desc =
461 "EXC_RESOURCE (RESOURCE_TYPE_CPU: CPU usage monitor tripped)";
462 snprintf(code_desc_buf, sizeof(code_desc_buf), "%d%%",
463 (int)EXC_RESOURCE_CPUMONITOR_DECODE_PERCENTAGE(m_exc_code));
464 snprintf(subcode_desc_buf, sizeof(subcode_desc_buf), "%d%%",
465 (int)EXC_RESOURCE_CPUMONITOR_DECODE_PERCENTAGE_OBSERVED(
467 break;
468 case RESOURCE_TYPE_WAKEUPS:
469 exc_desc = "EXC_RESOURCE (RESOURCE_TYPE_WAKEUPS: idle wakeups monitor "
470 "tripped)";
471 snprintf(
472 code_desc_buf, sizeof(code_desc_buf), "%d w/s",
473 (int)EXC_RESOURCE_CPUMONITOR_DECODE_WAKEUPS_PERMITTED(m_exc_code));
474 snprintf(subcode_desc_buf, sizeof(subcode_desc_buf), "%d w/s",
475 (int)EXC_RESOURCE_CPUMONITOR_DECODE_WAKEUPS_OBSERVED(
477 break;
478 case RESOURCE_TYPE_MEMORY:
479 exc_desc = "EXC_RESOURCE (RESOURCE_TYPE_MEMORY: high watermark memory "
480 "limit exceeded)";
481 snprintf(code_desc_buf, sizeof(code_desc_buf), "%d MB",
482 (int)EXC_RESOURCE_HWM_DECODE_LIMIT(m_exc_code));
483 subcode_desc = nullptr;
484 subcode_label = nullptr;
485 break;
486#if defined(RESOURCE_TYPE_IO)
487 // RESOURCE_TYPE_IO is introduced in macOS SDK 10.12.
488 case RESOURCE_TYPE_IO:
489 exc_desc = "EXC_RESOURCE RESOURCE_TYPE_IO";
490 snprintf(code_desc_buf, sizeof(code_desc_buf), "%d MB",
491 (int)EXC_RESOURCE_IO_DECODE_LIMIT(m_exc_code));
492 snprintf(subcode_desc_buf, sizeof(subcode_desc_buf), "%d MB",
493 (int)EXC_RESOURCE_IO_OBSERVED(m_exc_subcode));
494 ;
495 break;
496#endif
497 }
498 }
499#endif
500 break;
501 case 12:
503 return m_description.c_str();
504 exc_desc = "EXC_GUARD";
505 break;
506 }
507
508 StreamString strm;
509
510 if (exc_desc)
511 strm.PutCString(exc_desc);
512 else
513 strm.Printf("EXC_??? (%" PRIu64 ")", m_value);
514
515 if (m_exc_data_count >= 1) {
516 if (code_desc)
517 strm.Printf(" (%s=%s", code_label, code_desc);
518 else
519 strm.Printf(" (%s=%" PRIu64, code_label, m_exc_code);
520 }
521
522 if (m_exc_data_count >= 2) {
523 if (subcode_label && subcode_desc)
524 strm.Printf(", %s=%s", subcode_label, subcode_desc);
525 else if (subcode_label)
526 strm.Printf(", %s=0x%" PRIx64, subcode_label, m_exc_subcode);
527 }
528
529 if (m_exc_data_count > 0)
530 strm.PutChar(')');
531
532 m_description = std::string(strm.GetString());
533 return m_description.c_str();
534}
535
536#if defined(__APPLE__)
537const char *
538StopInfoMachException::MachException::Name(exception_type_t exc_type) {
539 switch (exc_type) {
540 case EXC_BAD_ACCESS:
541 return "EXC_BAD_ACCESS";
542 case EXC_BAD_INSTRUCTION:
543 return "EXC_BAD_INSTRUCTION";
544 case EXC_ARITHMETIC:
545 return "EXC_ARITHMETIC";
546 case EXC_EMULATION:
547 return "EXC_EMULATION";
548 case EXC_SOFTWARE:
549 return "EXC_SOFTWARE";
550 case EXC_BREAKPOINT:
551 return "EXC_BREAKPOINT";
552 case EXC_SYSCALL:
553 return "EXC_SYSCALL";
554 case EXC_MACH_SYSCALL:
555 return "EXC_MACH_SYSCALL";
556 case EXC_RPC_ALERT:
557 return "EXC_RPC_ALERT";
558#ifdef EXC_CRASH
559 case EXC_CRASH:
560 return "EXC_CRASH";
561#endif
562 case EXC_RESOURCE:
563 return "EXC_RESOURCE";
564#ifdef EXC_GUARD
565 case EXC_GUARD:
566 return "EXC_GUARD";
567#endif
568#ifdef EXC_CORPSE_NOTIFY
569 case EXC_CORPSE_NOTIFY:
570 return "EXC_CORPSE_NOTIFY";
571#endif
572#ifdef EXC_CORPSE_VARIANT_BIT
573 case EXC_CORPSE_VARIANT_BIT:
574 return "EXC_CORPSE_VARIANT_BIT";
575#endif
576 default:
577 break;
578 }
579 return NULL;
580}
581
582std::optional<exception_type_t>
583StopInfoMachException::MachException::ExceptionCode(const char *name) {
584 return llvm::StringSwitch<std::optional<exception_type_t>>(name)
585 .Case("EXC_BAD_ACCESS", EXC_BAD_ACCESS)
586 .Case("EXC_BAD_INSTRUCTION", EXC_BAD_INSTRUCTION)
587 .Case("EXC_ARITHMETIC", EXC_ARITHMETIC)
588 .Case("EXC_EMULATION", EXC_EMULATION)
589 .Case("EXC_SOFTWARE", EXC_SOFTWARE)
590 .Case("EXC_BREAKPOINT", EXC_BREAKPOINT)
591 .Case("EXC_SYSCALL", EXC_SYSCALL)
592 .Case("EXC_MACH_SYSCALL", EXC_MACH_SYSCALL)
593 .Case("EXC_RPC_ALERT", EXC_RPC_ALERT)
594#ifdef EXC_CRASH
595 .Case("EXC_CRASH", EXC_CRASH)
596#endif
597 .Case("EXC_RESOURCE", EXC_RESOURCE)
598#ifdef EXC_GUARD
599 .Case("EXC_GUARD", EXC_GUARD)
600#endif
601#ifdef EXC_CORPSE_NOTIFY
602 .Case("EXC_CORPSE_NOTIFY", EXC_CORPSE_NOTIFY)
603#endif
604 .Default(std::nullopt);
605}
606#endif
607
609 Thread &thread, uint32_t exc_type, uint32_t exc_data_count,
610 uint64_t exc_code, uint64_t exc_sub_code, uint64_t exc_sub_sub_code,
611 bool pc_already_adjusted, bool adjust_pc_if_needed) {
612 if (exc_type == 0)
613 return StopInfoSP();
614
615 bool not_stepping_but_got_singlestep_exception = false;
616 uint32_t pc_decrement = 0;
617 ExecutionContext exe_ctx(thread.shared_from_this());
618 Target *target = exe_ctx.GetTargetPtr();
619 const llvm::Triple::ArchType cpu =
620 target ? target->GetArchitecture().GetMachine()
621 : llvm::Triple::UnknownArch;
622
623 ProcessSP process_sp(thread.GetProcess());
624 RegisterContextSP reg_ctx_sp(thread.GetRegisterContext());
625 // Caveat: with x86 KDP if we've hit a breakpoint, the pc we
626 // receive is past the breakpoint instruction.
627 // If we have a breakpoints at 0x100 and 0x101, we hit the
628 // 0x100 breakpoint and the pc is reported at 0x101.
629 // We will initially mark this thread as being stopped at an
630 // unexecuted breakpoint at 0x101. Later when we see that
631 // we stopped for a Breakpoint reason, we will decrement the
632 // pc, and update the thread to record that we hit the
633 // breakpoint at 0x100.
634 // The fact that the pc may be off by one at this point
635 // (for an x86 KDP breakpoint hit) is not a problem.
636 addr_t pc = reg_ctx_sp->GetPC();
637 BreakpointSiteSP bp_site_sp =
638 process_sp->GetBreakpointSiteList().FindByAddress(pc);
639 if (bp_site_sp && bp_site_sp->IsEnabled())
640 thread.SetThreadStoppedAtUnexecutedBP(pc);
641
642 switch (exc_type) {
643 case 1: // EXC_BAD_ACCESS
644 case 2: // EXC_BAD_INSTRUCTION
645 case 3: // EXC_ARITHMETIC
646 case 4: // EXC_EMULATION
647 break;
648
649 case 5: // EXC_SOFTWARE
650 if (exc_code == 0x10003) // EXC_SOFT_SIGNAL
651 {
652 if (exc_sub_code == 5) {
653 // On MacOSX, a SIGTRAP can signify that a process has called exec,
654 // so we should check with our dynamic loader to verify.
655 ProcessSP process_sp(thread.GetProcess());
656 if (process_sp) {
657 DynamicLoader *dynamic_loader = process_sp->GetDynamicLoader();
658 if (dynamic_loader && dynamic_loader->ProcessDidExec()) {
659 // The program was re-exec'ed
661 }
662 }
663 }
664 return StopInfo::CreateStopReasonWithSignal(thread, exc_sub_code);
665 }
666 break;
667
668 // A mach exception comes with 2-4 pieces of data.
669 // The sub-codes are only provided for certain types
670 // of mach exceptions.
671 // [exc_type, exc_code, exc_sub_code, exc_sub_sub_code]
672 //
673 // Here are all of the EXC_BREAKPOINT, exc_type==6,
674 // exceptions we can receive.
675 //
676 // Instruction step:
677 // [6, 1, 0]
678 // Intel KDP [6, 3, ??]
679 // armv7 [6, 0x102, <stop-pc>] Same as software breakpoint!
680 //
681 // Software breakpoint:
682 // x86 [6, 2, 0]
683 // Intel KDP [6, 2, <bp-addr + 1>]
684 // arm64 [6, 1, <bp-addr>]
685 // armv7 [6, 0x102, <bp-addr>] Same as instruction step!
686 //
687 // Hardware breakpoint:
688 // x86 [6, 1, <bp-addr>, 0]
689 // x86/Rosetta not implemented, see software breakpoint
690 // arm64 [6, 1, <bp-addr>]
691 // armv7 not implemented, see software breakpoint
692 //
693 // Hardware watchpoint:
694 // x86 [6, 1, <accessed-addr>, 0] (both Intel hw and Rosetta)
695 // arm64 [6, 0x102, <accessed-addr>, 0]
696 // armv7 [6, 0x102, <accessed-addr>, 0]
697 //
698 // arm64 BRK instruction (imm arg not reflected in the ME)
699 // [ 6, 1, <addr-of-BRK-insn>]
700 //
701 // In order of codes mach exceptions:
702 // [6, 1, 0] - instruction step
703 // [6, 1, <bp-addr>] - hardware breakpoint or watchpoint
704 //
705 // [6, 2, 0] - software breakpoint
706 // [6, 2, <bp-addr + 1>] - software breakpoint
707 //
708 // [6, 3] - instruction step
709 //
710 // [6, 0x102, <stop-pc>] armv7 instruction step
711 // [6, 0x102, <bp-addr>] armv7 software breakpoint
712 // [6, 0x102, <accessed-addr>, 0] arm64/armv7 watchpoint
713
714 case 6: // EXC_BREAKPOINT
715 {
716 bool stopped_by_hitting_breakpoint = false;
717 bool stopped_by_completing_stepi = false;
718 bool stopped_watchpoint = false;
719 std::optional<addr_t> address;
720
721 // exc_code 1
722 if (exc_code == 1) {
723 if (exc_sub_code == 0) {
724 stopped_by_completing_stepi = true;
725 } else {
726 // Ambiguous: could be signalling a
727 // breakpoint or watchpoint hit.
728 stopped_by_hitting_breakpoint = true;
729 stopped_watchpoint = true;
730 address = exc_sub_code;
731 }
732 }
733
734 // exc_code 2
735 if (exc_code == 2) {
736 if (exc_sub_code == 0)
737 stopped_by_hitting_breakpoint = true;
738 else {
739 stopped_by_hitting_breakpoint = true;
740 // Intel KDP software breakpoint
741 if (!pc_already_adjusted)
742 pc_decrement = 1;
743 }
744 }
745
746 // exc_code 3
747 if (exc_code == 3)
748 stopped_by_completing_stepi = true;
749
750 // exc_code 0x102
751 if (exc_code == 0x102 && exc_sub_code != 0) {
752 if (cpu == llvm::Triple::arm || cpu == llvm::Triple::thumb) {
753 stopped_by_hitting_breakpoint = true;
754 stopped_by_completing_stepi = true;
755 }
756 stopped_watchpoint = true;
757 address = exc_sub_code;
758 }
759
760 // The Mach Exception may have been ambiguous --
761 // e.g. we stopped either because of a breakpoint
762 // or a watchpoint. We'll disambiguate which it
763 // really was.
764
765 if (stopped_by_hitting_breakpoint) {
766 addr_t pc = reg_ctx_sp->GetPC() - pc_decrement;
767
768 if (address)
769 bp_site_sp =
770 process_sp->GetBreakpointSiteList().FindByAddress(*address);
771 if (!bp_site_sp && reg_ctx_sp) {
772 bp_site_sp = process_sp->GetBreakpointSiteList().FindByAddress(pc);
773 }
774 if (bp_site_sp && bp_site_sp->IsEnabled()) {
775 // We've hit this breakpoint, whether it was intended for this thread
776 // or not. Clear this in the Tread object so we step past it on resume.
777 thread.SetThreadHitBreakpointSite();
778
779 if (bp_site_sp->ValidForThisThread(thread)) {
780 // Update the PC if we were asked to do so, but only do so if we find
781 // a breakpoint that we know about because this could be a trap
782 // instruction in the code.
783 if (pc_decrement > 0 && adjust_pc_if_needed && reg_ctx_sp)
784 reg_ctx_sp->SetPC(pc);
785
787 thread, bp_site_sp->GetID());
788 } else {
789 return StopInfoSP();
790 }
791 }
792 }
793
794 // Breakpoint-hit events are handled.
795 // Now handle watchpoints.
796
797 if (stopped_watchpoint && address) {
798 WatchpointResourceSP wp_rsrc_sp =
799 target->GetProcessSP()->GetWatchpointResourceList().FindByAddress(
800 *address);
801 if (wp_rsrc_sp && wp_rsrc_sp->GetNumberOfConstituents() > 0) {
803 thread, wp_rsrc_sp->GetConstituentAtIndex(0)->GetID());
804 }
805 }
806
807 // Finally, handle instruction step.
808
809 if (stopped_by_completing_stepi) {
810 if (thread.GetTemporaryResumeState() != eStateStepping)
811 not_stepping_but_got_singlestep_exception = true;
812 else
814 }
815
816 } break;
817
818 case 7: // EXC_SYSCALL
819 case 8: // EXC_MACH_SYSCALL
820 case 9: // EXC_RPC_ALERT
821 case 10: // EXC_CRASH
822 break;
823 }
824
825 return std::make_shared<StopInfoMachException>(
826 thread, exc_type, exc_data_count, exc_code, exc_sub_code,
827 not_stepping_but_got_singlestep_exception);
828}
829
830// Detect an unusual situation on Darwin where:
831//
832// 0. We did an instruction-step before this.
833// 1. We have a hardware breakpoint or watchpoint set.
834// 2. We resumed the process, but not with an instruction-step.
835// 3. The thread gets an "instruction-step completed" mach exception.
836// 4. The pc has not advanced - it is the same as before.
837//
838// This method returns true for that combination of events.
840 Log *log = GetLog(LLDBLog::Step);
841
842 // We got an instruction-step completed mach exception but we were not
843 // doing an instruction step on this thread.
845 return false;
846
847 RegisterContextSP reg_ctx_sp(thread.GetRegisterContext());
848 std::optional<addr_t> prev_pc = thread.GetPreviousFrameZeroPC();
849 if (!reg_ctx_sp || !prev_pc)
850 return false;
851
852 // The previous pc value and current pc value are the same.
853 if (*prev_pc != reg_ctx_sp->GetPC())
854 return false;
855
856 // We have a watchpoint -- this is the kernel bug.
857 ProcessSP process_sp = thread.GetProcess();
858 if (process_sp->GetWatchpointResourceList().GetSize()) {
859 LLDB_LOGF(log,
860 "Thread stopped with insn-step completed mach exception but "
861 "thread was not stepping; there is a hardware watchpoint set.");
862 return true;
863 }
864
865 // We have a hardware breakpoint -- this is the kernel bug.
866 auto &bp_site_list = process_sp->GetBreakpointSiteList();
867 for (auto &site : bp_site_list.Sites()) {
868 if (site->IsHardware() && site->IsEnabled()) {
869 LLDB_LOGF(log,
870 "Thread stopped with insn-step completed mach exception but "
871 "thread was not stepping; there is a hardware breakpoint set.");
872 return true;
873 }
874 }
875
876 return false;
877}
#define LLDB_LOGF(log,...)
Definition Log.h:376
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.
Definition Address.h:62
lldb::addr_t GetLoadAddress(Target *target) const
Get the load address.
Definition Address.cpp:301
bool GetDescription(Stream &s, Target &target, lldb::DescriptionLevel level) const
Write a description of this object to a Stream.
Definition Address.cpp:383
An architecture specification class.
Definition ArchSpec.h:31
llvm::Triple::ArchType GetMachine() const
Returns a machine family for the current architecture.
Definition ArchSpec.cpp:677
Core GetCore() const
Definition ArchSpec.h:447
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.
Definition Process.h:354
const lldb::ABISP & GetABI()
Definition Process.cpp:1481
const RegisterInfo * GetRegisterInfoByName(llvm::StringRef reg_name, uint32_t start_idx=0)
virtual bool ReadRegister(const RegisterInfo *reg_info, RegisterValue &reg_value)=0
uint64_t GetAsUInt64(uint64_t fail_value=UINT64_MAX, bool *success_ptr=nullptr) const
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.
std::optional< lldb::addr_t > GetTagFaultAddress() const
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
Definition StopInfo.h:227
uint64_t GetValue() const
Definition StopInfo.h:46
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)
friend class Thread
Definition StopInfo.h:245
lldb::ThreadWP m_thread_wp
Definition StopInfo.h:222
llvm::StringRef GetString() const
A stream class that can stream formatted output to a file.
Definition Stream.h:28
size_t Printf(const char *format,...) __attribute__((format(printf
Output printf formatted output to the stream.
Definition Stream.cpp:134
size_t PutCString(llvm::StringRef cstr)
Output a C string to the stream.
Definition Stream.cpp:65
size_t PutChar(char ch)
Definition Stream.cpp:131
const lldb::ProcessSP & GetProcessSP() const
Definition Target.cpp:312
bool ResolveLoadAddress(lldb::addr_t load_addr, Address &so_addr, uint32_t stop_id=SectionLoadHistory::eStopIDNow, bool allow_section_end=false)
Definition Target.cpp:3323
const ArchSpec & GetArchitecture() const
Definition Target.h:1153
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.
Definition Log.h:332
std::shared_ptr< lldb_private::ABI > ABISP
std::shared_ptr< lldb_private::StackFrame > StackFrameSP
std::shared_ptr< lldb_private::BreakpointSite > BreakpointSiteSP
@ eDescriptionLevelBrief
@ 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
uint64_t addr_t
Definition lldb-types.h:80
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),...