1 /* Darwin support for GDB, the GNU debugger.
2 Copyright (C) 1997-2018 Free Software Foundation, Inc.
4 Contributed by Apple Computer, Inc.
6 This file is part of GDB.
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>. */
30 #include "i386-tdep.h"
31 #include "i387-tdep.h"
33 #include "arch-utils.h"
37 #include "darwin-nat.h"
38 #include "i386-darwin-tdep.h"
41 #include "amd64-nat.h"
42 #include "amd64-tdep.h"
43 #include "amd64-darwin-tdep.h"
46 struct i386_darwin_nat_target final : public x86_nat_target<darwin_nat_target>
48 /* Add our register access methods. */
49 void fetch_registers (struct regcache *, int) override;
50 void store_registers (struct regcache *, int) override;
53 static struct i386_darwin_nat_target darwin_target;
55 /* Read register values from the inferior process.
56 If REGNO is -1, do this for all registers.
57 Otherwise, REGNO specifies which register (so we can save time). */
60 i386_darwin_nat_target::fetch_registers (struct regcache *regcache, int regno)
62 thread_t current_thread = ptid_get_tid (regcache->ptid ());
64 struct gdbarch *gdbarch = regcache->arch ();
67 if (gdbarch_ptr_bit (gdbarch) == 64)
69 if (regno == -1 || amd64_native_gregset_supplies_p (gdbarch, regno))
71 x86_thread_state_t gp_regs;
72 unsigned int gp_count = x86_THREAD_STATE_COUNT;
75 ret = thread_get_state
76 (current_thread, x86_THREAD_STATE, (thread_state_t) & gp_regs,
78 if (ret != KERN_SUCCESS)
80 printf_unfiltered (_("Error calling thread_get_state for "
81 "GP registers for thread 0x%lx\n"),
82 (unsigned long) current_thread);
83 MACH_CHECK_ERROR (ret);
86 /* Some kernels don't sanitize the values. */
87 gp_regs.uts.ts64.__fs &= 0xffff;
88 gp_regs.uts.ts64.__gs &= 0xffff;
90 amd64_supply_native_gregset (regcache, &gp_regs.uts, -1);
94 if (regno == -1 || !amd64_native_gregset_supplies_p (gdbarch, regno))
96 x86_float_state_t fp_regs;
97 unsigned int fp_count = x86_FLOAT_STATE_COUNT;
100 ret = thread_get_state
101 (current_thread, x86_FLOAT_STATE, (thread_state_t) & fp_regs,
103 if (ret != KERN_SUCCESS)
105 printf_unfiltered (_("Error calling thread_get_state for "
106 "float registers for thread 0x%lx\n"),
107 (unsigned long) current_thread);
108 MACH_CHECK_ERROR (ret);
110 amd64_supply_fxsave (regcache, -1, &fp_regs.ufs.fs64.__fpu_fcw);
117 if (regno == -1 || regno < I386_NUM_GREGS)
119 x86_thread_state32_t gp_regs;
120 unsigned int gp_count = x86_THREAD_STATE32_COUNT;
124 ret = thread_get_state
125 (current_thread, x86_THREAD_STATE32, (thread_state_t) &gp_regs,
127 if (ret != KERN_SUCCESS)
129 printf_unfiltered (_("Error calling thread_get_state for "
130 "GP registers for thread 0x%lx\n"),
131 (unsigned long) current_thread);
132 MACH_CHECK_ERROR (ret);
134 for (i = 0; i < I386_NUM_GREGS; i++)
136 (i, (char *) &gp_regs + i386_darwin_thread_state_reg_offset[i]);
142 || (regno >= I386_ST0_REGNUM && regno < I386_SSE_NUM_REGS))
144 x86_float_state32_t fp_regs;
145 unsigned int fp_count = x86_FLOAT_STATE32_COUNT;
148 ret = thread_get_state
149 (current_thread, x86_FLOAT_STATE32, (thread_state_t) &fp_regs,
151 if (ret != KERN_SUCCESS)
153 printf_unfiltered (_("Error calling thread_get_state for "
154 "float registers for thread 0x%lx\n"),
155 (unsigned long) current_thread);
156 MACH_CHECK_ERROR (ret);
158 i387_supply_fxsave (regcache, -1, &fp_regs.__fpu_fcw);
165 warning (_("unknown register %d"), regno);
166 regcache->raw_supply (regno, NULL);
170 /* Store our register values back into the inferior.
171 If REGNO is -1, do this for all registers.
172 Otherwise, REGNO specifies which register (so we can save time). */
175 i386_darwin_nat_target::store_registers (struct regcache *regcache,
178 thread_t current_thread = ptid_get_tid (regcache->ptid ());
179 struct gdbarch *gdbarch = regcache->arch ();
182 if (gdbarch_ptr_bit (gdbarch) == 64)
184 if (regno == -1 || amd64_native_gregset_supplies_p (gdbarch, regno))
186 x86_thread_state_t gp_regs;
188 unsigned int gp_count = x86_THREAD_STATE_COUNT;
190 ret = thread_get_state
191 (current_thread, x86_THREAD_STATE, (thread_state_t) &gp_regs,
193 MACH_CHECK_ERROR (ret);
194 gdb_assert (gp_regs.tsh.flavor == x86_THREAD_STATE64);
195 gdb_assert (gp_regs.tsh.count == x86_THREAD_STATE64_COUNT);
197 amd64_collect_native_gregset (regcache, &gp_regs.uts, regno);
199 /* Some kernels don't sanitize the values. */
200 gp_regs.uts.ts64.__fs &= 0xffff;
201 gp_regs.uts.ts64.__gs &= 0xffff;
203 ret = thread_set_state (current_thread, x86_THREAD_STATE,
204 (thread_state_t) &gp_regs,
205 x86_THREAD_STATE_COUNT);
206 MACH_CHECK_ERROR (ret);
209 if (regno == -1 || !amd64_native_gregset_supplies_p (gdbarch, regno))
211 x86_float_state_t fp_regs;
213 unsigned int fp_count = x86_FLOAT_STATE_COUNT;
215 ret = thread_get_state
216 (current_thread, x86_FLOAT_STATE, (thread_state_t) & fp_regs,
218 MACH_CHECK_ERROR (ret);
219 gdb_assert (fp_regs.fsh.flavor == x86_FLOAT_STATE64);
220 gdb_assert (fp_regs.fsh.count == x86_FLOAT_STATE64_COUNT);
222 amd64_collect_fxsave (regcache, regno, &fp_regs.ufs.fs64.__fpu_fcw);
224 ret = thread_set_state (current_thread, x86_FLOAT_STATE,
225 (thread_state_t) & fp_regs,
226 x86_FLOAT_STATE_COUNT);
227 MACH_CHECK_ERROR (ret);
233 if (regno == -1 || regno < I386_NUM_GREGS)
235 x86_thread_state32_t gp_regs;
237 unsigned int gp_count = x86_THREAD_STATE32_COUNT;
240 ret = thread_get_state
241 (current_thread, x86_THREAD_STATE32, (thread_state_t) &gp_regs,
243 MACH_CHECK_ERROR (ret);
245 for (i = 0; i < I386_NUM_GREGS; i++)
246 if (regno == -1 || regno == i)
247 regcache->raw_collect
248 (i, (char *) &gp_regs + i386_darwin_thread_state_reg_offset[i]);
250 ret = thread_set_state (current_thread, x86_THREAD_STATE32,
251 (thread_state_t) &gp_regs,
252 x86_THREAD_STATE32_COUNT);
253 MACH_CHECK_ERROR (ret);
257 || (regno >= I386_ST0_REGNUM && regno < I386_SSE_NUM_REGS))
259 x86_float_state32_t fp_regs;
260 unsigned int fp_count = x86_FLOAT_STATE32_COUNT;
263 ret = thread_get_state
264 (current_thread, x86_FLOAT_STATE32, (thread_state_t) & fp_regs,
266 MACH_CHECK_ERROR (ret);
268 i387_collect_fxsave (regcache, regno, &fp_regs.__fpu_fcw);
270 ret = thread_set_state (current_thread, x86_FLOAT_STATE32,
271 (thread_state_t) &fp_regs,
272 x86_FLOAT_STATE32_COUNT);
273 MACH_CHECK_ERROR (ret);
278 /* Support for debug registers, boosted mostly from i386-linux-nat.c. */
281 i386_darwin_dr_set (int regnum, CORE_ADDR value)
284 thread_t current_thread;
285 x86_debug_state_t dr_regs;
287 unsigned int dr_count;
289 gdb_assert (regnum >= 0 && regnum <= DR_CONTROL);
291 current_thread = ptid_get_tid (inferior_ptid);
293 dr_regs.dsh.flavor = x86_DEBUG_STATE;
294 dr_regs.dsh.count = x86_DEBUG_STATE_COUNT;
295 dr_count = x86_DEBUG_STATE_COUNT;
296 ret = thread_get_state (current_thread, x86_DEBUG_STATE,
297 (thread_state_t) &dr_regs, &dr_count);
298 MACH_CHECK_ERROR (ret);
300 switch (dr_regs.dsh.flavor)
302 case x86_DEBUG_STATE32:
306 dr_regs.uds.ds32.__dr0 = value;
309 dr_regs.uds.ds32.__dr1 = value;
312 dr_regs.uds.ds32.__dr2 = value;
315 dr_regs.uds.ds32.__dr3 = value;
318 dr_regs.uds.ds32.__dr4 = value;
321 dr_regs.uds.ds32.__dr5 = value;
324 dr_regs.uds.ds32.__dr6 = value;
327 dr_regs.uds.ds32.__dr7 = value;
332 case x86_DEBUG_STATE64:
336 dr_regs.uds.ds64.__dr0 = value;
339 dr_regs.uds.ds64.__dr1 = value;
342 dr_regs.uds.ds64.__dr2 = value;
345 dr_regs.uds.ds64.__dr3 = value;
348 dr_regs.uds.ds64.__dr4 = value;
351 dr_regs.uds.ds64.__dr5 = value;
354 dr_regs.uds.ds64.__dr6 = value;
357 dr_regs.uds.ds64.__dr7 = value;
364 ret = thread_set_state (current_thread, dr_regs.dsh.flavor,
365 (thread_state_t) &dr_regs.uds, dr_count);
367 MACH_CHECK_ERROR (ret);
371 i386_darwin_dr_get (int regnum)
373 thread_t current_thread;
374 x86_debug_state_t dr_regs;
376 unsigned int dr_count;
378 gdb_assert (regnum >= 0 && regnum <= DR_CONTROL);
380 current_thread = ptid_get_tid (inferior_ptid);
382 dr_regs.dsh.flavor = x86_DEBUG_STATE;
383 dr_regs.dsh.count = x86_DEBUG_STATE_COUNT;
384 dr_count = x86_DEBUG_STATE_COUNT;
385 ret = thread_get_state (current_thread, x86_DEBUG_STATE,
386 (thread_state_t) &dr_regs, &dr_count);
387 MACH_CHECK_ERROR (ret);
389 switch (dr_regs.dsh.flavor)
391 case x86_DEBUG_STATE32:
395 return dr_regs.uds.ds32.__dr0;
397 return dr_regs.uds.ds32.__dr1;
399 return dr_regs.uds.ds32.__dr2;
401 return dr_regs.uds.ds32.__dr3;
403 return dr_regs.uds.ds32.__dr4;
405 return dr_regs.uds.ds32.__dr5;
407 return dr_regs.uds.ds32.__dr6;
409 return dr_regs.uds.ds32.__dr7;
415 case x86_DEBUG_STATE64:
419 return dr_regs.uds.ds64.__dr0;
421 return dr_regs.uds.ds64.__dr1;
423 return dr_regs.uds.ds64.__dr2;
425 return dr_regs.uds.ds64.__dr3;
427 return dr_regs.uds.ds64.__dr4;
429 return dr_regs.uds.ds64.__dr5;
431 return dr_regs.uds.ds64.__dr6;
433 return dr_regs.uds.ds64.__dr7;
445 i386_darwin_dr_set_control (unsigned long control)
447 i386_darwin_dr_set (DR_CONTROL, control);
451 i386_darwin_dr_set_addr (int regnum, CORE_ADDR addr)
453 gdb_assert (regnum >= 0 && regnum <= DR_LASTADDR - DR_FIRSTADDR);
455 i386_darwin_dr_set (DR_FIRSTADDR + regnum, addr);
459 i386_darwin_dr_get_addr (int regnum)
461 return i386_darwin_dr_get (regnum);
465 i386_darwin_dr_get_status (void)
467 return i386_darwin_dr_get (DR_STATUS);
471 i386_darwin_dr_get_control (void)
473 return i386_darwin_dr_get (DR_CONTROL);
477 darwin_check_osabi (darwin_inferior *inf, thread_t thread)
479 if (gdbarch_osabi (target_gdbarch ()) == GDB_OSABI_UNKNOWN)
481 /* Attaching to a process. Let's figure out what kind it is. */
482 x86_thread_state_t gp_regs;
483 struct gdbarch_info info;
484 unsigned int gp_count = x86_THREAD_STATE_COUNT;
487 ret = thread_get_state (thread, x86_THREAD_STATE,
488 (thread_state_t) &gp_regs, &gp_count);
489 if (ret != KERN_SUCCESS)
491 MACH_CHECK_ERROR (ret);
495 gdbarch_info_init (&info);
496 gdbarch_info_fill (&info);
497 info.byte_order = gdbarch_byte_order (target_gdbarch ());
498 info.osabi = GDB_OSABI_DARWIN;
499 if (gp_regs.tsh.flavor == x86_THREAD_STATE64)
500 info.bfd_arch_info = bfd_lookup_arch (bfd_arch_i386,
503 info.bfd_arch_info = bfd_lookup_arch (bfd_arch_i386,
505 gdbarch_update_p (info);
509 #define X86_EFLAGS_T 0x100UL
511 /* Returning from a signal trampoline is done by calling a
512 special system call (sigreturn). This system call
513 restores the registers that were saved when the signal was
514 raised, including %eflags/%rflags. That means that single-stepping
515 won't work. Instead, we'll have to modify the signal context
516 that's about to be restored, and set the trace flag there. */
519 i386_darwin_sstep_at_sigreturn (x86_thread_state_t *regs)
521 enum bfd_endian byte_order = gdbarch_byte_order (target_gdbarch ());
522 static const gdb_byte darwin_syscall[] = { 0xcd, 0x80 }; /* int 0x80 */
523 gdb_byte buf[sizeof (darwin_syscall)];
525 /* Check if PC is at a sigreturn system call. */
526 if (target_read_memory (regs->uts.ts32.__eip, buf, sizeof (buf)) == 0
527 && memcmp (buf, darwin_syscall, sizeof (darwin_syscall)) == 0
528 && regs->uts.ts32.__eax == 0xb8 /* SYS_sigreturn */)
535 uctx_addr = read_memory_unsigned_integer
536 (regs->uts.ts32.__esp + 4, 4, byte_order);
537 mctx_addr = read_memory_unsigned_integer
538 (uctx_addr + 28, 4, byte_order);
540 flags_addr = mctx_addr + 12 + 9 * 4;
541 read_memory (flags_addr, (gdb_byte *) &eflags, 4);
542 eflags |= X86_EFLAGS_T;
543 write_memory (flags_addr, (gdb_byte *) &eflags, 4);
552 amd64_darwin_sstep_at_sigreturn (x86_thread_state_t *regs)
554 enum bfd_endian byte_order = gdbarch_byte_order (target_gdbarch ());
555 static const gdb_byte darwin_syscall[] = { 0x0f, 0x05 }; /* syscall */
556 gdb_byte buf[sizeof (darwin_syscall)];
558 /* Check if PC is at a sigreturn system call. */
559 if (target_read_memory (regs->uts.ts64.__rip, buf, sizeof (buf)) == 0
560 && memcmp (buf, darwin_syscall, sizeof (darwin_syscall)) == 0
561 && (regs->uts.ts64.__rax & 0xffffffff) == 0x20000b8 /* SYS_sigreturn */)
567 mctx_addr = read_memory_unsigned_integer
568 (regs->uts.ts64.__rdi + 48, 8, byte_order);
569 flags_addr = mctx_addr + 16 + 17 * 8;
571 /* AMD64 is little endian. */
572 read_memory (flags_addr, (gdb_byte *) &rflags, 4);
573 rflags |= X86_EFLAGS_T;
574 write_memory (flags_addr, (gdb_byte *) &rflags, 4);
583 darwin_set_sstep (thread_t thread, int enable)
585 x86_thread_state_t regs;
586 unsigned int count = x86_THREAD_STATE_COUNT;
589 kret = thread_get_state (thread, x86_THREAD_STATE,
590 (thread_state_t) ®s, &count);
591 if (kret != KERN_SUCCESS)
593 printf_unfiltered (_("darwin_set_sstep: error %x, thread=%x\n"),
598 switch (regs.tsh.flavor)
600 case x86_THREAD_STATE32:
602 __uint32_t bit = enable ? X86_EFLAGS_T : 0;
604 if (enable && i386_darwin_sstep_at_sigreturn (®s))
606 if ((regs.uts.ts32.__eflags & X86_EFLAGS_T) == bit)
608 regs.uts.ts32.__eflags
609 = (regs.uts.ts32.__eflags & ~X86_EFLAGS_T) | bit;
610 kret = thread_set_state (thread, x86_THREAD_STATE,
611 (thread_state_t) ®s, count);
612 MACH_CHECK_ERROR (kret);
616 case x86_THREAD_STATE64:
618 __uint64_t bit = enable ? X86_EFLAGS_T : 0;
620 if (enable && amd64_darwin_sstep_at_sigreturn (®s))
622 if ((regs.uts.ts64.__rflags & X86_EFLAGS_T) == bit)
624 regs.uts.ts64.__rflags
625 = (regs.uts.ts64.__rflags & ~X86_EFLAGS_T) | bit;
626 kret = thread_set_state (thread, x86_THREAD_STATE,
627 (thread_state_t) ®s, count);
628 MACH_CHECK_ERROR (kret);
633 error (_("darwin_set_sstep: unknown flavour: %d"), regs.tsh.flavor);
638 _initialize_i386_darwin_nat (void)
641 amd64_native_gregset64_reg_offset = amd64_darwin_thread_state_reg_offset;
642 amd64_native_gregset64_num_regs = amd64_darwin_thread_state_num_regs;
643 amd64_native_gregset32_reg_offset = i386_darwin_thread_state_reg_offset;
644 amd64_native_gregset32_num_regs = i386_darwin_thread_state_num_regs;
647 x86_dr_low.set_control = i386_darwin_dr_set_control;
648 x86_dr_low.set_addr = i386_darwin_dr_set_addr;
649 x86_dr_low.get_addr = i386_darwin_dr_get_addr;
650 x86_dr_low.get_status = i386_darwin_dr_get_status;
651 x86_dr_low.get_control = i386_darwin_dr_get_control;
653 /* Let's assume that the kernel is 64 bits iff the executable is. */
655 x86_set_debug_register_length (8);
657 x86_set_debug_register_length (4);
660 add_inf_child_target (&darwin_target);