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_get_ptid (regcache));
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++)
137 (char *)&gp_regs + i386_darwin_thread_state_reg_offset[i]);
143 || (regno >= I386_ST0_REGNUM && regno < I386_SSE_NUM_REGS))
145 x86_float_state32_t fp_regs;
146 unsigned int fp_count = x86_FLOAT_STATE32_COUNT;
149 ret = thread_get_state
150 (current_thread, x86_FLOAT_STATE32, (thread_state_t) &fp_regs,
152 if (ret != KERN_SUCCESS)
154 printf_unfiltered (_("Error calling thread_get_state for "
155 "float registers for thread 0x%lx\n"),
156 (unsigned long) current_thread);
157 MACH_CHECK_ERROR (ret);
159 i387_supply_fxsave (regcache, -1, &fp_regs.__fpu_fcw);
166 warning (_("unknown register %d"), regno);
167 regcache_raw_supply (regcache, regno, NULL);
171 /* Store our register values back into the inferior.
172 If REGNO is -1, do this for all registers.
173 Otherwise, REGNO specifies which register (so we can save time). */
176 i386_darwin_nat_target::store_registers (struct regcache *regcache,
179 thread_t current_thread = ptid_get_tid (regcache_get_ptid (regcache));
180 struct gdbarch *gdbarch = regcache->arch ();
183 if (gdbarch_ptr_bit (gdbarch) == 64)
185 if (regno == -1 || amd64_native_gregset_supplies_p (gdbarch, regno))
187 x86_thread_state_t gp_regs;
189 unsigned int gp_count = x86_THREAD_STATE_COUNT;
191 ret = thread_get_state
192 (current_thread, x86_THREAD_STATE, (thread_state_t) &gp_regs,
194 MACH_CHECK_ERROR (ret);
195 gdb_assert (gp_regs.tsh.flavor == x86_THREAD_STATE64);
196 gdb_assert (gp_regs.tsh.count == x86_THREAD_STATE64_COUNT);
198 amd64_collect_native_gregset (regcache, &gp_regs.uts, regno);
200 /* Some kernels don't sanitize the values. */
201 gp_regs.uts.ts64.__fs &= 0xffff;
202 gp_regs.uts.ts64.__gs &= 0xffff;
204 ret = thread_set_state (current_thread, x86_THREAD_STATE,
205 (thread_state_t) &gp_regs,
206 x86_THREAD_STATE_COUNT);
207 MACH_CHECK_ERROR (ret);
210 if (regno == -1 || !amd64_native_gregset_supplies_p (gdbarch, regno))
212 x86_float_state_t fp_regs;
214 unsigned int fp_count = x86_FLOAT_STATE_COUNT;
216 ret = thread_get_state
217 (current_thread, x86_FLOAT_STATE, (thread_state_t) & fp_regs,
219 MACH_CHECK_ERROR (ret);
220 gdb_assert (fp_regs.fsh.flavor == x86_FLOAT_STATE64);
221 gdb_assert (fp_regs.fsh.count == x86_FLOAT_STATE64_COUNT);
223 amd64_collect_fxsave (regcache, regno, &fp_regs.ufs.fs64.__fpu_fcw);
225 ret = thread_set_state (current_thread, x86_FLOAT_STATE,
226 (thread_state_t) & fp_regs,
227 x86_FLOAT_STATE_COUNT);
228 MACH_CHECK_ERROR (ret);
234 if (regno == -1 || regno < I386_NUM_GREGS)
236 x86_thread_state32_t gp_regs;
238 unsigned int gp_count = x86_THREAD_STATE32_COUNT;
241 ret = thread_get_state
242 (current_thread, x86_THREAD_STATE32, (thread_state_t) &gp_regs,
244 MACH_CHECK_ERROR (ret);
246 for (i = 0; i < I386_NUM_GREGS; i++)
247 if (regno == -1 || regno == i)
250 (char *)&gp_regs + i386_darwin_thread_state_reg_offset[i]);
252 ret = thread_set_state (current_thread, x86_THREAD_STATE32,
253 (thread_state_t) &gp_regs,
254 x86_THREAD_STATE32_COUNT);
255 MACH_CHECK_ERROR (ret);
259 || (regno >= I386_ST0_REGNUM && regno < I386_SSE_NUM_REGS))
261 x86_float_state32_t fp_regs;
262 unsigned int fp_count = x86_FLOAT_STATE32_COUNT;
265 ret = thread_get_state
266 (current_thread, x86_FLOAT_STATE32, (thread_state_t) & fp_regs,
268 MACH_CHECK_ERROR (ret);
270 i387_collect_fxsave (regcache, regno, &fp_regs.__fpu_fcw);
272 ret = thread_set_state (current_thread, x86_FLOAT_STATE32,
273 (thread_state_t) &fp_regs,
274 x86_FLOAT_STATE32_COUNT);
275 MACH_CHECK_ERROR (ret);
280 /* Support for debug registers, boosted mostly from i386-linux-nat.c. */
283 i386_darwin_dr_set (int regnum, CORE_ADDR value)
286 thread_t current_thread;
287 x86_debug_state_t dr_regs;
289 unsigned int dr_count;
291 gdb_assert (regnum >= 0 && regnum <= DR_CONTROL);
293 current_thread = ptid_get_tid (inferior_ptid);
295 dr_regs.dsh.flavor = x86_DEBUG_STATE;
296 dr_regs.dsh.count = x86_DEBUG_STATE_COUNT;
297 dr_count = x86_DEBUG_STATE_COUNT;
298 ret = thread_get_state (current_thread, x86_DEBUG_STATE,
299 (thread_state_t) &dr_regs, &dr_count);
300 MACH_CHECK_ERROR (ret);
302 switch (dr_regs.dsh.flavor)
304 case x86_DEBUG_STATE32:
308 dr_regs.uds.ds32.__dr0 = value;
311 dr_regs.uds.ds32.__dr1 = value;
314 dr_regs.uds.ds32.__dr2 = value;
317 dr_regs.uds.ds32.__dr3 = value;
320 dr_regs.uds.ds32.__dr4 = value;
323 dr_regs.uds.ds32.__dr5 = value;
326 dr_regs.uds.ds32.__dr6 = value;
329 dr_regs.uds.ds32.__dr7 = value;
334 case x86_DEBUG_STATE64:
338 dr_regs.uds.ds64.__dr0 = value;
341 dr_regs.uds.ds64.__dr1 = value;
344 dr_regs.uds.ds64.__dr2 = value;
347 dr_regs.uds.ds64.__dr3 = value;
350 dr_regs.uds.ds64.__dr4 = value;
353 dr_regs.uds.ds64.__dr5 = value;
356 dr_regs.uds.ds64.__dr6 = value;
359 dr_regs.uds.ds64.__dr7 = value;
366 ret = thread_set_state (current_thread, dr_regs.dsh.flavor,
367 (thread_state_t) &dr_regs.uds, dr_count);
369 MACH_CHECK_ERROR (ret);
373 i386_darwin_dr_get (int regnum)
375 thread_t current_thread;
376 x86_debug_state_t dr_regs;
378 unsigned int dr_count;
380 gdb_assert (regnum >= 0 && regnum <= DR_CONTROL);
382 current_thread = ptid_get_tid (inferior_ptid);
384 dr_regs.dsh.flavor = x86_DEBUG_STATE;
385 dr_regs.dsh.count = x86_DEBUG_STATE_COUNT;
386 dr_count = x86_DEBUG_STATE_COUNT;
387 ret = thread_get_state (current_thread, x86_DEBUG_STATE,
388 (thread_state_t) &dr_regs, &dr_count);
389 MACH_CHECK_ERROR (ret);
391 switch (dr_regs.dsh.flavor)
393 case x86_DEBUG_STATE32:
397 return dr_regs.uds.ds32.__dr0;
399 return dr_regs.uds.ds32.__dr1;
401 return dr_regs.uds.ds32.__dr2;
403 return dr_regs.uds.ds32.__dr3;
405 return dr_regs.uds.ds32.__dr4;
407 return dr_regs.uds.ds32.__dr5;
409 return dr_regs.uds.ds32.__dr6;
411 return dr_regs.uds.ds32.__dr7;
417 case x86_DEBUG_STATE64:
421 return dr_regs.uds.ds64.__dr0;
423 return dr_regs.uds.ds64.__dr1;
425 return dr_regs.uds.ds64.__dr2;
427 return dr_regs.uds.ds64.__dr3;
429 return dr_regs.uds.ds64.__dr4;
431 return dr_regs.uds.ds64.__dr5;
433 return dr_regs.uds.ds64.__dr6;
435 return dr_regs.uds.ds64.__dr7;
447 i386_darwin_dr_set_control (unsigned long control)
449 i386_darwin_dr_set (DR_CONTROL, control);
453 i386_darwin_dr_set_addr (int regnum, CORE_ADDR addr)
455 gdb_assert (regnum >= 0 && regnum <= DR_LASTADDR - DR_FIRSTADDR);
457 i386_darwin_dr_set (DR_FIRSTADDR + regnum, addr);
461 i386_darwin_dr_get_addr (int regnum)
463 return i386_darwin_dr_get (regnum);
467 i386_darwin_dr_get_status (void)
469 return i386_darwin_dr_get (DR_STATUS);
473 i386_darwin_dr_get_control (void)
475 return i386_darwin_dr_get (DR_CONTROL);
479 darwin_check_osabi (darwin_inferior *inf, thread_t thread)
481 if (gdbarch_osabi (target_gdbarch ()) == GDB_OSABI_UNKNOWN)
483 /* Attaching to a process. Let's figure out what kind it is. */
484 x86_thread_state_t gp_regs;
485 struct gdbarch_info info;
486 unsigned int gp_count = x86_THREAD_STATE_COUNT;
489 ret = thread_get_state (thread, x86_THREAD_STATE,
490 (thread_state_t) &gp_regs, &gp_count);
491 if (ret != KERN_SUCCESS)
493 MACH_CHECK_ERROR (ret);
497 gdbarch_info_init (&info);
498 gdbarch_info_fill (&info);
499 info.byte_order = gdbarch_byte_order (target_gdbarch ());
500 info.osabi = GDB_OSABI_DARWIN;
501 if (gp_regs.tsh.flavor == x86_THREAD_STATE64)
502 info.bfd_arch_info = bfd_lookup_arch (bfd_arch_i386,
505 info.bfd_arch_info = bfd_lookup_arch (bfd_arch_i386,
507 gdbarch_update_p (info);
511 #define X86_EFLAGS_T 0x100UL
513 /* Returning from a signal trampoline is done by calling a
514 special system call (sigreturn). This system call
515 restores the registers that were saved when the signal was
516 raised, including %eflags/%rflags. That means that single-stepping
517 won't work. Instead, we'll have to modify the signal context
518 that's about to be restored, and set the trace flag there. */
521 i386_darwin_sstep_at_sigreturn (x86_thread_state_t *regs)
523 enum bfd_endian byte_order = gdbarch_byte_order (target_gdbarch ());
524 static const gdb_byte darwin_syscall[] = { 0xcd, 0x80 }; /* int 0x80 */
525 gdb_byte buf[sizeof (darwin_syscall)];
527 /* Check if PC is at a sigreturn system call. */
528 if (target_read_memory (regs->uts.ts32.__eip, buf, sizeof (buf)) == 0
529 && memcmp (buf, darwin_syscall, sizeof (darwin_syscall)) == 0
530 && regs->uts.ts32.__eax == 0xb8 /* SYS_sigreturn */)
537 uctx_addr = read_memory_unsigned_integer
538 (regs->uts.ts32.__esp + 4, 4, byte_order);
539 mctx_addr = read_memory_unsigned_integer
540 (uctx_addr + 28, 4, byte_order);
542 flags_addr = mctx_addr + 12 + 9 * 4;
543 read_memory (flags_addr, (gdb_byte *) &eflags, 4);
544 eflags |= X86_EFLAGS_T;
545 write_memory (flags_addr, (gdb_byte *) &eflags, 4);
554 amd64_darwin_sstep_at_sigreturn (x86_thread_state_t *regs)
556 enum bfd_endian byte_order = gdbarch_byte_order (target_gdbarch ());
557 static const gdb_byte darwin_syscall[] = { 0x0f, 0x05 }; /* syscall */
558 gdb_byte buf[sizeof (darwin_syscall)];
560 /* Check if PC is at a sigreturn system call. */
561 if (target_read_memory (regs->uts.ts64.__rip, buf, sizeof (buf)) == 0
562 && memcmp (buf, darwin_syscall, sizeof (darwin_syscall)) == 0
563 && (regs->uts.ts64.__rax & 0xffffffff) == 0x20000b8 /* SYS_sigreturn */)
569 mctx_addr = read_memory_unsigned_integer
570 (regs->uts.ts64.__rdi + 48, 8, byte_order);
571 flags_addr = mctx_addr + 16 + 17 * 8;
573 /* AMD64 is little endian. */
574 read_memory (flags_addr, (gdb_byte *) &rflags, 4);
575 rflags |= X86_EFLAGS_T;
576 write_memory (flags_addr, (gdb_byte *) &rflags, 4);
585 darwin_set_sstep (thread_t thread, int enable)
587 x86_thread_state_t regs;
588 unsigned int count = x86_THREAD_STATE_COUNT;
591 kret = thread_get_state (thread, x86_THREAD_STATE,
592 (thread_state_t) ®s, &count);
593 if (kret != KERN_SUCCESS)
595 printf_unfiltered (_("darwin_set_sstep: error %x, thread=%x\n"),
600 switch (regs.tsh.flavor)
602 case x86_THREAD_STATE32:
604 __uint32_t bit = enable ? X86_EFLAGS_T : 0;
606 if (enable && i386_darwin_sstep_at_sigreturn (®s))
608 if ((regs.uts.ts32.__eflags & X86_EFLAGS_T) == bit)
610 regs.uts.ts32.__eflags
611 = (regs.uts.ts32.__eflags & ~X86_EFLAGS_T) | bit;
612 kret = thread_set_state (thread, x86_THREAD_STATE,
613 (thread_state_t) ®s, count);
614 MACH_CHECK_ERROR (kret);
618 case x86_THREAD_STATE64:
620 __uint64_t bit = enable ? X86_EFLAGS_T : 0;
622 if (enable && amd64_darwin_sstep_at_sigreturn (®s))
624 if ((regs.uts.ts64.__rflags & X86_EFLAGS_T) == bit)
626 regs.uts.ts64.__rflags
627 = (regs.uts.ts64.__rflags & ~X86_EFLAGS_T) | bit;
628 kret = thread_set_state (thread, x86_THREAD_STATE,
629 (thread_state_t) ®s, count);
630 MACH_CHECK_ERROR (kret);
635 error (_("darwin_set_sstep: unknown flavour: %d"), regs.tsh.flavor);
640 _initialize_i386_darwin_nat (void)
643 amd64_native_gregset64_reg_offset = amd64_darwin_thread_state_reg_offset;
644 amd64_native_gregset64_num_regs = amd64_darwin_thread_state_num_regs;
645 amd64_native_gregset32_reg_offset = i386_darwin_thread_state_reg_offset;
646 amd64_native_gregset32_num_regs = i386_darwin_thread_state_num_regs;
649 x86_dr_low.set_control = i386_darwin_dr_set_control;
650 x86_dr_low.set_addr = i386_darwin_dr_set_addr;
651 x86_dr_low.get_addr = i386_darwin_dr_get_addr;
652 x86_dr_low.get_status = i386_darwin_dr_get_status;
653 x86_dr_low.get_control = i386_darwin_dr_get_control;
655 /* Let's assume that the kernel is 64 bits iff the executable is. */
657 x86_set_debug_register_length (8);
659 x86_set_debug_register_length (4);
662 add_inf_child_target (&darwin_target);