1 /* Darwin support for GDB, the GNU debugger.
2 Copyright (C) 1997-2014 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 /* Read register values from the inferior process.
47 If REGNO is -1, do this for all registers.
48 Otherwise, REGNO specifies which register (so we can save time). */
50 i386_darwin_fetch_inferior_registers (struct target_ops *ops,
51 struct regcache *regcache, int regno)
53 thread_t current_thread = ptid_get_tid (inferior_ptid);
55 struct gdbarch *gdbarch = get_regcache_arch (regcache);
58 if (gdbarch_ptr_bit (gdbarch) == 64)
60 if (regno == -1 || amd64_native_gregset_supplies_p (gdbarch, regno))
62 x86_thread_state_t gp_regs;
63 unsigned int gp_count = x86_THREAD_STATE_COUNT;
66 ret = thread_get_state
67 (current_thread, x86_THREAD_STATE, (thread_state_t) & gp_regs,
69 if (ret != KERN_SUCCESS)
71 printf_unfiltered (_("Error calling thread_get_state for "
72 "GP registers for thread 0x%lx\n"),
73 (unsigned long) current_thread);
74 MACH_CHECK_ERROR (ret);
76 amd64_supply_native_gregset (regcache, &gp_regs.uts, -1);
80 if (regno == -1 || !amd64_native_gregset_supplies_p (gdbarch, regno))
82 x86_float_state_t fp_regs;
83 unsigned int fp_count = x86_FLOAT_STATE_COUNT;
86 ret = thread_get_state
87 (current_thread, x86_FLOAT_STATE, (thread_state_t) & fp_regs,
89 if (ret != KERN_SUCCESS)
91 printf_unfiltered (_("Error calling thread_get_state for "
92 "float registers for thread 0x%lx\n"),
93 (unsigned long) current_thread);
94 MACH_CHECK_ERROR (ret);
96 amd64_supply_fxsave (regcache, -1, &fp_regs.ufs.fs64.__fpu_fcw);
103 if (regno == -1 || regno < I386_NUM_GREGS)
105 x86_thread_state32_t gp_regs;
106 unsigned int gp_count = x86_THREAD_STATE32_COUNT;
110 ret = thread_get_state
111 (current_thread, x86_THREAD_STATE32, (thread_state_t) &gp_regs,
113 if (ret != KERN_SUCCESS)
115 printf_unfiltered (_("Error calling thread_get_state for "
116 "GP registers for thread 0x%lx\n"),
117 (unsigned long) current_thread);
118 MACH_CHECK_ERROR (ret);
120 for (i = 0; i < I386_NUM_GREGS; i++)
123 (char *)&gp_regs + i386_darwin_thread_state_reg_offset[i]);
129 || (regno >= I386_ST0_REGNUM && regno < I386_SSE_NUM_REGS))
131 x86_float_state32_t fp_regs;
132 unsigned int fp_count = x86_FLOAT_STATE32_COUNT;
135 ret = thread_get_state
136 (current_thread, x86_FLOAT_STATE32, (thread_state_t) &fp_regs,
138 if (ret != KERN_SUCCESS)
140 printf_unfiltered (_("Error calling thread_get_state for "
141 "float registers for thread 0x%lx\n"),
142 (unsigned long) current_thread);
143 MACH_CHECK_ERROR (ret);
145 i387_supply_fxsave (regcache, -1, &fp_regs.__fpu_fcw);
152 warning (_("unknown register %d"), regno);
153 regcache_raw_supply (regcache, regno, NULL);
157 /* Store our register values back into the inferior.
158 If REGNO is -1, do this for all registers.
159 Otherwise, REGNO specifies which register (so we can save time). */
162 i386_darwin_store_inferior_registers (struct target_ops *ops,
163 struct regcache *regcache, int regno)
165 thread_t current_thread = ptid_get_tid (inferior_ptid);
166 struct gdbarch *gdbarch = get_regcache_arch (regcache);
169 if (gdbarch_ptr_bit (gdbarch) == 64)
171 if (regno == -1 || amd64_native_gregset_supplies_p (gdbarch, regno))
173 x86_thread_state_t gp_regs;
175 unsigned int gp_count = x86_THREAD_STATE_COUNT;
177 ret = thread_get_state
178 (current_thread, x86_THREAD_STATE, (thread_state_t) &gp_regs,
180 MACH_CHECK_ERROR (ret);
181 gdb_assert (gp_regs.tsh.flavor == x86_THREAD_STATE64);
182 gdb_assert (gp_regs.tsh.count == x86_THREAD_STATE64_COUNT);
184 amd64_collect_native_gregset (regcache, &gp_regs.uts, regno);
186 ret = thread_set_state (current_thread, x86_THREAD_STATE,
187 (thread_state_t) &gp_regs,
188 x86_THREAD_STATE_COUNT);
189 MACH_CHECK_ERROR (ret);
192 if (regno == -1 || !amd64_native_gregset_supplies_p (gdbarch, regno))
194 x86_float_state_t fp_regs;
196 unsigned int fp_count = x86_FLOAT_STATE_COUNT;
198 ret = thread_get_state
199 (current_thread, x86_FLOAT_STATE, (thread_state_t) & fp_regs,
201 MACH_CHECK_ERROR (ret);
202 gdb_assert (fp_regs.fsh.flavor == x86_FLOAT_STATE64);
203 gdb_assert (fp_regs.fsh.count == x86_FLOAT_STATE64_COUNT);
205 amd64_collect_fxsave (regcache, regno, &fp_regs.ufs.fs64.__fpu_fcw);
207 ret = thread_set_state (current_thread, x86_FLOAT_STATE,
208 (thread_state_t) & fp_regs,
209 x86_FLOAT_STATE_COUNT);
210 MACH_CHECK_ERROR (ret);
216 if (regno == -1 || regno < I386_NUM_GREGS)
218 x86_thread_state32_t gp_regs;
220 unsigned int gp_count = x86_THREAD_STATE32_COUNT;
223 ret = thread_get_state
224 (current_thread, x86_THREAD_STATE32, (thread_state_t) &gp_regs,
226 MACH_CHECK_ERROR (ret);
228 for (i = 0; i < I386_NUM_GREGS; i++)
229 if (regno == -1 || regno == i)
232 (char *)&gp_regs + i386_darwin_thread_state_reg_offset[i]);
234 ret = thread_set_state (current_thread, x86_THREAD_STATE32,
235 (thread_state_t) &gp_regs,
236 x86_THREAD_STATE32_COUNT);
237 MACH_CHECK_ERROR (ret);
241 || (regno >= I386_ST0_REGNUM && regno < I386_SSE_NUM_REGS))
243 x86_float_state32_t fp_regs;
244 unsigned int fp_count = x86_FLOAT_STATE32_COUNT;
247 ret = thread_get_state
248 (current_thread, x86_FLOAT_STATE32, (thread_state_t) & fp_regs,
250 MACH_CHECK_ERROR (ret);
252 i387_collect_fxsave (regcache, regno, &fp_regs.__fpu_fcw);
254 ret = thread_set_state (current_thread, x86_FLOAT_STATE32,
255 (thread_state_t) &fp_regs,
256 x86_FLOAT_STATE32_COUNT);
257 MACH_CHECK_ERROR (ret);
262 /* Support for debug registers, boosted mostly from i386-linux-nat.c. */
265 i386_darwin_dr_set (int regnum, CORE_ADDR value)
268 thread_t current_thread;
269 x86_debug_state_t dr_regs;
271 unsigned int dr_count;
273 gdb_assert (regnum >= 0 && regnum <= DR_CONTROL);
275 current_thread = ptid_get_tid (inferior_ptid);
277 dr_regs.dsh.flavor = x86_DEBUG_STATE;
278 dr_regs.dsh.count = x86_DEBUG_STATE_COUNT;
279 dr_count = x86_DEBUG_STATE_COUNT;
280 ret = thread_get_state (current_thread, x86_DEBUG_STATE,
281 (thread_state_t) &dr_regs, &dr_count);
282 MACH_CHECK_ERROR (ret);
284 switch (dr_regs.dsh.flavor)
286 case x86_DEBUG_STATE32:
290 dr_regs.uds.ds32.__dr0 = value;
293 dr_regs.uds.ds32.__dr1 = value;
296 dr_regs.uds.ds32.__dr2 = value;
299 dr_regs.uds.ds32.__dr3 = value;
302 dr_regs.uds.ds32.__dr4 = value;
305 dr_regs.uds.ds32.__dr5 = value;
308 dr_regs.uds.ds32.__dr6 = value;
311 dr_regs.uds.ds32.__dr7 = value;
316 case x86_DEBUG_STATE64:
320 dr_regs.uds.ds64.__dr0 = value;
323 dr_regs.uds.ds64.__dr1 = value;
326 dr_regs.uds.ds64.__dr2 = value;
329 dr_regs.uds.ds64.__dr3 = value;
332 dr_regs.uds.ds64.__dr4 = value;
335 dr_regs.uds.ds64.__dr5 = value;
338 dr_regs.uds.ds64.__dr6 = value;
341 dr_regs.uds.ds64.__dr7 = value;
348 ret = thread_set_state (current_thread, dr_regs.dsh.flavor,
349 (thread_state_t) &dr_regs.uds, dr_count);
351 MACH_CHECK_ERROR (ret);
355 i386_darwin_dr_get (int regnum)
357 thread_t current_thread;
358 x86_debug_state_t dr_regs;
360 unsigned int dr_count;
362 gdb_assert (regnum >= 0 && regnum <= DR_CONTROL);
364 current_thread = ptid_get_tid (inferior_ptid);
366 dr_regs.dsh.flavor = x86_DEBUG_STATE;
367 dr_regs.dsh.count = x86_DEBUG_STATE_COUNT;
368 dr_count = x86_DEBUG_STATE_COUNT;
369 ret = thread_get_state (current_thread, x86_DEBUG_STATE,
370 (thread_state_t) &dr_regs, &dr_count);
371 MACH_CHECK_ERROR (ret);
373 switch (dr_regs.dsh.flavor)
375 case x86_DEBUG_STATE32:
379 return dr_regs.uds.ds32.__dr0;
381 return dr_regs.uds.ds32.__dr1;
383 return dr_regs.uds.ds32.__dr2;
385 return dr_regs.uds.ds32.__dr3;
387 return dr_regs.uds.ds32.__dr4;
389 return dr_regs.uds.ds32.__dr5;
391 return dr_regs.uds.ds32.__dr6;
393 return dr_regs.uds.ds32.__dr7;
399 case x86_DEBUG_STATE64:
403 return dr_regs.uds.ds64.__dr0;
405 return dr_regs.uds.ds64.__dr1;
407 return dr_regs.uds.ds64.__dr2;
409 return dr_regs.uds.ds64.__dr3;
411 return dr_regs.uds.ds64.__dr4;
413 return dr_regs.uds.ds64.__dr5;
415 return dr_regs.uds.ds64.__dr6;
417 return dr_regs.uds.ds64.__dr7;
429 i386_darwin_dr_set_control (unsigned long control)
431 i386_darwin_dr_set (DR_CONTROL, control);
435 i386_darwin_dr_set_addr (int regnum, CORE_ADDR addr)
437 gdb_assert (regnum >= 0 && regnum <= DR_LASTADDR - DR_FIRSTADDR);
439 i386_darwin_dr_set (DR_FIRSTADDR + regnum, addr);
443 i386_darwin_dr_get_addr (int regnum)
445 return i386_darwin_dr_get (regnum);
449 i386_darwin_dr_get_status (void)
451 return i386_darwin_dr_get (DR_STATUS);
455 i386_darwin_dr_get_control (void)
457 return i386_darwin_dr_get (DR_CONTROL);
461 darwin_check_osabi (darwin_inferior *inf, thread_t thread)
463 if (gdbarch_osabi (target_gdbarch ()) == GDB_OSABI_UNKNOWN)
465 /* Attaching to a process. Let's figure out what kind it is. */
466 x86_thread_state_t gp_regs;
467 struct gdbarch_info info;
468 unsigned int gp_count = x86_THREAD_STATE_COUNT;
471 ret = thread_get_state (thread, x86_THREAD_STATE,
472 (thread_state_t) &gp_regs, &gp_count);
473 if (ret != KERN_SUCCESS)
475 MACH_CHECK_ERROR (ret);
479 gdbarch_info_init (&info);
480 gdbarch_info_fill (&info);
481 info.byte_order = gdbarch_byte_order (target_gdbarch ());
482 info.osabi = GDB_OSABI_DARWIN;
483 if (gp_regs.tsh.flavor == x86_THREAD_STATE64)
484 info.bfd_arch_info = bfd_lookup_arch (bfd_arch_i386,
487 info.bfd_arch_info = bfd_lookup_arch (bfd_arch_i386,
489 gdbarch_update_p (info);
493 #define X86_EFLAGS_T 0x100UL
495 /* Returning from a signal trampoline is done by calling a
496 special system call (sigreturn). This system call
497 restores the registers that were saved when the signal was
498 raised, including %eflags/%rflags. That means that single-stepping
499 won't work. Instead, we'll have to modify the signal context
500 that's about to be restored, and set the trace flag there. */
503 i386_darwin_sstep_at_sigreturn (x86_thread_state_t *regs)
505 enum bfd_endian byte_order = gdbarch_byte_order (target_gdbarch ());
506 static const gdb_byte darwin_syscall[] = { 0xcd, 0x80 }; /* int 0x80 */
507 gdb_byte buf[sizeof (darwin_syscall)];
509 /* Check if PC is at a sigreturn system call. */
510 if (target_read_memory (regs->uts.ts32.__eip, buf, sizeof (buf)) == 0
511 && memcmp (buf, darwin_syscall, sizeof (darwin_syscall)) == 0
512 && regs->uts.ts32.__eax == 0xb8 /* SYS_sigreturn */)
519 uctx_addr = read_memory_unsigned_integer
520 (regs->uts.ts32.__esp + 4, 4, byte_order);
521 mctx_addr = read_memory_unsigned_integer
522 (uctx_addr + 28, 4, byte_order);
524 flags_addr = mctx_addr + 12 + 9 * 4;
525 read_memory (flags_addr, (gdb_byte *) &eflags, 4);
526 eflags |= X86_EFLAGS_T;
527 write_memory (flags_addr, (gdb_byte *) &eflags, 4);
536 amd64_darwin_sstep_at_sigreturn (x86_thread_state_t *regs)
538 enum bfd_endian byte_order = gdbarch_byte_order (target_gdbarch ());
539 static const gdb_byte darwin_syscall[] = { 0x0f, 0x05 }; /* syscall */
540 gdb_byte buf[sizeof (darwin_syscall)];
542 /* Check if PC is at a sigreturn system call. */
543 if (target_read_memory (regs->uts.ts64.__rip, buf, sizeof (buf)) == 0
544 && memcmp (buf, darwin_syscall, sizeof (darwin_syscall)) == 0
545 && (regs->uts.ts64.__rax & 0xffffffff) == 0x20000b8 /* SYS_sigreturn */)
551 mctx_addr = read_memory_unsigned_integer
552 (regs->uts.ts64.__rdi + 48, 8, byte_order);
553 flags_addr = mctx_addr + 16 + 17 * 8;
555 /* AMD64 is little endian. */
556 read_memory (flags_addr, (gdb_byte *) &rflags, 4);
557 rflags |= X86_EFLAGS_T;
558 write_memory (flags_addr, (gdb_byte *) &rflags, 4);
567 darwin_set_sstep (thread_t thread, int enable)
569 x86_thread_state_t regs;
570 unsigned int count = x86_THREAD_STATE_COUNT;
573 kret = thread_get_state (thread, x86_THREAD_STATE,
574 (thread_state_t) ®s, &count);
575 if (kret != KERN_SUCCESS)
577 printf_unfiltered (_("darwin_set_sstep: error %x, thread=%x\n"),
582 switch (regs.tsh.flavor)
584 case x86_THREAD_STATE32:
586 __uint32_t bit = enable ? X86_EFLAGS_T : 0;
588 if (enable && i386_darwin_sstep_at_sigreturn (®s))
590 if ((regs.uts.ts32.__eflags & X86_EFLAGS_T) == bit)
592 regs.uts.ts32.__eflags
593 = (regs.uts.ts32.__eflags & ~X86_EFLAGS_T) | bit;
594 kret = thread_set_state (thread, x86_THREAD_STATE,
595 (thread_state_t) ®s, count);
596 MACH_CHECK_ERROR (kret);
600 case x86_THREAD_STATE64:
602 __uint64_t bit = enable ? X86_EFLAGS_T : 0;
604 if (enable && amd64_darwin_sstep_at_sigreturn (®s))
606 if ((regs.uts.ts64.__rflags & X86_EFLAGS_T) == bit)
608 regs.uts.ts64.__rflags
609 = (regs.uts.ts64.__rflags & ~X86_EFLAGS_T) | bit;
610 kret = thread_set_state (thread, x86_THREAD_STATE,
611 (thread_state_t) ®s, count);
612 MACH_CHECK_ERROR (kret);
617 error (_("darwin_set_sstep: unknown flavour: %d"), regs.tsh.flavor);
622 darwin_complete_target (struct target_ops *target)
625 amd64_native_gregset64_reg_offset = amd64_darwin_thread_state_reg_offset;
626 amd64_native_gregset64_num_regs = amd64_darwin_thread_state_num_regs;
627 amd64_native_gregset32_reg_offset = i386_darwin_thread_state_reg_offset;
628 amd64_native_gregset32_num_regs = i386_darwin_thread_state_num_regs;
631 x86_use_watchpoints (target);
633 x86_dr_low.set_control = i386_darwin_dr_set_control;
634 x86_dr_low.set_addr = i386_darwin_dr_set_addr;
635 x86_dr_low.get_addr = i386_darwin_dr_get_addr;
636 x86_dr_low.get_status = i386_darwin_dr_get_status;
637 x86_dr_low.get_control = i386_darwin_dr_get_control;
639 /* Let's assume that the kernel is 64 bits iff the executable is. */
641 x86_set_debug_register_length (8);
643 x86_set_debug_register_length (4);
646 target->to_fetch_registers = i386_darwin_fetch_inferior_registers;
647 target->to_store_registers = i386_darwin_store_inferior_registers;