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 "gdb_assert.h"
31 #include "i386-tdep.h"
32 #include "i387-tdep.h"
34 #include "arch-utils.h"
38 #include "darwin-nat.h"
39 #include "i386-darwin-tdep.h"
42 #include "amd64-nat.h"
43 #include "amd64-tdep.h"
44 #include "amd64-darwin-tdep.h"
47 /* Read register values from the inferior process.
48 If REGNO is -1, do this for all registers.
49 Otherwise, REGNO specifies which register (so we can save time). */
51 i386_darwin_fetch_inferior_registers (struct target_ops *ops,
52 struct regcache *regcache, int regno)
54 thread_t current_thread = ptid_get_tid (inferior_ptid);
56 struct gdbarch *gdbarch = get_regcache_arch (regcache);
59 if (gdbarch_ptr_bit (gdbarch) == 64)
61 if (regno == -1 || amd64_native_gregset_supplies_p (gdbarch, regno))
63 x86_thread_state_t gp_regs;
64 unsigned int gp_count = x86_THREAD_STATE_COUNT;
67 ret = thread_get_state
68 (current_thread, x86_THREAD_STATE, (thread_state_t) & gp_regs,
70 if (ret != KERN_SUCCESS)
72 printf_unfiltered (_("Error calling thread_get_state for "
73 "GP registers for thread 0x%lx\n"),
74 (unsigned long) current_thread);
75 MACH_CHECK_ERROR (ret);
77 amd64_supply_native_gregset (regcache, &gp_regs.uts, -1);
81 if (regno == -1 || !amd64_native_gregset_supplies_p (gdbarch, regno))
83 x86_float_state_t fp_regs;
84 unsigned int fp_count = x86_FLOAT_STATE_COUNT;
87 ret = thread_get_state
88 (current_thread, x86_FLOAT_STATE, (thread_state_t) & fp_regs,
90 if (ret != KERN_SUCCESS)
92 printf_unfiltered (_("Error calling thread_get_state for "
93 "float registers for thread 0x%lx\n"),
94 (unsigned long) current_thread);
95 MACH_CHECK_ERROR (ret);
97 amd64_supply_fxsave (regcache, -1, &fp_regs.ufs.fs64.__fpu_fcw);
104 if (regno == -1 || regno < I386_NUM_GREGS)
106 x86_thread_state32_t gp_regs;
107 unsigned int gp_count = x86_THREAD_STATE32_COUNT;
111 ret = thread_get_state
112 (current_thread, x86_THREAD_STATE32, (thread_state_t) &gp_regs,
114 if (ret != KERN_SUCCESS)
116 printf_unfiltered (_("Error calling thread_get_state for "
117 "GP registers for thread 0x%lx\n"),
118 (unsigned long) current_thread);
119 MACH_CHECK_ERROR (ret);
121 for (i = 0; i < I386_NUM_GREGS; i++)
124 (char *)&gp_regs + i386_darwin_thread_state_reg_offset[i]);
130 || (regno >= I386_ST0_REGNUM && regno < I386_SSE_NUM_REGS))
132 x86_float_state32_t fp_regs;
133 unsigned int fp_count = x86_FLOAT_STATE32_COUNT;
136 ret = thread_get_state
137 (current_thread, x86_FLOAT_STATE32, (thread_state_t) &fp_regs,
139 if (ret != KERN_SUCCESS)
141 printf_unfiltered (_("Error calling thread_get_state for "
142 "float registers for thread 0x%lx\n"),
143 (unsigned long) current_thread);
144 MACH_CHECK_ERROR (ret);
146 i387_supply_fxsave (regcache, -1, &fp_regs.__fpu_fcw);
153 warning (_("unknown register %d"), regno);
154 regcache_raw_supply (regcache, regno, NULL);
158 /* Store our register values back into the inferior.
159 If REGNO is -1, do this for all registers.
160 Otherwise, REGNO specifies which register (so we can save time). */
163 i386_darwin_store_inferior_registers (struct target_ops *ops,
164 struct regcache *regcache, int regno)
166 thread_t current_thread = ptid_get_tid (inferior_ptid);
167 struct gdbarch *gdbarch = get_regcache_arch (regcache);
170 if (gdbarch_ptr_bit (gdbarch) == 64)
172 if (regno == -1 || amd64_native_gregset_supplies_p (gdbarch, regno))
174 x86_thread_state_t gp_regs;
176 unsigned int gp_count = x86_THREAD_STATE_COUNT;
178 ret = thread_get_state
179 (current_thread, x86_THREAD_STATE, (thread_state_t) &gp_regs,
181 MACH_CHECK_ERROR (ret);
182 gdb_assert (gp_regs.tsh.flavor == x86_THREAD_STATE64);
183 gdb_assert (gp_regs.tsh.count == x86_THREAD_STATE64_COUNT);
185 amd64_collect_native_gregset (regcache, &gp_regs.uts, regno);
187 ret = thread_set_state (current_thread, x86_THREAD_STATE,
188 (thread_state_t) &gp_regs,
189 x86_THREAD_STATE_COUNT);
190 MACH_CHECK_ERROR (ret);
193 if (regno == -1 || !amd64_native_gregset_supplies_p (gdbarch, regno))
195 x86_float_state_t fp_regs;
197 unsigned int fp_count = x86_FLOAT_STATE_COUNT;
199 ret = thread_get_state
200 (current_thread, x86_FLOAT_STATE, (thread_state_t) & fp_regs,
202 MACH_CHECK_ERROR (ret);
203 gdb_assert (fp_regs.fsh.flavor == x86_FLOAT_STATE64);
204 gdb_assert (fp_regs.fsh.count == x86_FLOAT_STATE64_COUNT);
206 amd64_collect_fxsave (regcache, regno, &fp_regs.ufs.fs64.__fpu_fcw);
208 ret = thread_set_state (current_thread, x86_FLOAT_STATE,
209 (thread_state_t) & fp_regs,
210 x86_FLOAT_STATE_COUNT);
211 MACH_CHECK_ERROR (ret);
217 if (regno == -1 || regno < I386_NUM_GREGS)
219 x86_thread_state32_t gp_regs;
221 unsigned int gp_count = x86_THREAD_STATE32_COUNT;
224 ret = thread_get_state
225 (current_thread, x86_THREAD_STATE32, (thread_state_t) &gp_regs,
227 MACH_CHECK_ERROR (ret);
229 for (i = 0; i < I386_NUM_GREGS; i++)
230 if (regno == -1 || regno == i)
233 (char *)&gp_regs + i386_darwin_thread_state_reg_offset[i]);
235 ret = thread_set_state (current_thread, x86_THREAD_STATE32,
236 (thread_state_t) &gp_regs,
237 x86_THREAD_STATE32_COUNT);
238 MACH_CHECK_ERROR (ret);
242 || (regno >= I386_ST0_REGNUM && regno < I386_SSE_NUM_REGS))
244 x86_float_state32_t fp_regs;
245 unsigned int fp_count = x86_FLOAT_STATE32_COUNT;
248 ret = thread_get_state
249 (current_thread, x86_FLOAT_STATE32, (thread_state_t) & fp_regs,
251 MACH_CHECK_ERROR (ret);
253 i387_collect_fxsave (regcache, regno, &fp_regs.__fpu_fcw);
255 ret = thread_set_state (current_thread, x86_FLOAT_STATE32,
256 (thread_state_t) &fp_regs,
257 x86_FLOAT_STATE32_COUNT);
258 MACH_CHECK_ERROR (ret);
263 /* Support for debug registers, boosted mostly from i386-linux-nat.c. */
266 i386_darwin_dr_set (int regnum, CORE_ADDR value)
269 thread_t current_thread;
270 x86_debug_state_t dr_regs;
272 unsigned int dr_count;
274 gdb_assert (regnum >= 0 && regnum <= DR_CONTROL);
276 current_thread = ptid_get_tid (inferior_ptid);
278 dr_regs.dsh.flavor = x86_DEBUG_STATE;
279 dr_regs.dsh.count = x86_DEBUG_STATE_COUNT;
280 dr_count = x86_DEBUG_STATE_COUNT;
281 ret = thread_get_state (current_thread, x86_DEBUG_STATE,
282 (thread_state_t) &dr_regs, &dr_count);
283 MACH_CHECK_ERROR (ret);
285 switch (dr_regs.dsh.flavor)
287 case x86_DEBUG_STATE32:
291 dr_regs.uds.ds32.__dr0 = value;
294 dr_regs.uds.ds32.__dr1 = value;
297 dr_regs.uds.ds32.__dr2 = value;
300 dr_regs.uds.ds32.__dr3 = value;
303 dr_regs.uds.ds32.__dr4 = value;
306 dr_regs.uds.ds32.__dr5 = value;
309 dr_regs.uds.ds32.__dr6 = value;
312 dr_regs.uds.ds32.__dr7 = value;
317 case x86_DEBUG_STATE64:
321 dr_regs.uds.ds64.__dr0 = value;
324 dr_regs.uds.ds64.__dr1 = value;
327 dr_regs.uds.ds64.__dr2 = value;
330 dr_regs.uds.ds64.__dr3 = value;
333 dr_regs.uds.ds64.__dr4 = value;
336 dr_regs.uds.ds64.__dr5 = value;
339 dr_regs.uds.ds64.__dr6 = value;
342 dr_regs.uds.ds64.__dr7 = value;
349 ret = thread_set_state (current_thread, dr_regs.dsh.flavor,
350 (thread_state_t) &dr_regs.uds, dr_count);
352 MACH_CHECK_ERROR (ret);
356 i386_darwin_dr_get (int regnum)
358 thread_t current_thread;
359 x86_debug_state_t dr_regs;
361 unsigned int dr_count;
363 gdb_assert (regnum >= 0 && regnum <= DR_CONTROL);
365 current_thread = ptid_get_tid (inferior_ptid);
367 dr_regs.dsh.flavor = x86_DEBUG_STATE;
368 dr_regs.dsh.count = x86_DEBUG_STATE_COUNT;
369 dr_count = x86_DEBUG_STATE_COUNT;
370 ret = thread_get_state (current_thread, x86_DEBUG_STATE,
371 (thread_state_t) &dr_regs, &dr_count);
372 MACH_CHECK_ERROR (ret);
374 switch (dr_regs.dsh.flavor)
376 case x86_DEBUG_STATE32:
380 return dr_regs.uds.ds32.__dr0;
382 return dr_regs.uds.ds32.__dr1;
384 return dr_regs.uds.ds32.__dr2;
386 return dr_regs.uds.ds32.__dr3;
388 return dr_regs.uds.ds32.__dr4;
390 return dr_regs.uds.ds32.__dr5;
392 return dr_regs.uds.ds32.__dr6;
394 return dr_regs.uds.ds32.__dr7;
400 case x86_DEBUG_STATE64:
404 return dr_regs.uds.ds64.__dr0;
406 return dr_regs.uds.ds64.__dr1;
408 return dr_regs.uds.ds64.__dr2;
410 return dr_regs.uds.ds64.__dr3;
412 return dr_regs.uds.ds64.__dr4;
414 return dr_regs.uds.ds64.__dr5;
416 return dr_regs.uds.ds64.__dr6;
418 return dr_regs.uds.ds64.__dr7;
430 i386_darwin_dr_set_control (unsigned long control)
432 i386_darwin_dr_set (DR_CONTROL, control);
436 i386_darwin_dr_set_addr (int regnum, CORE_ADDR addr)
438 gdb_assert (regnum >= 0 && regnum <= DR_LASTADDR - DR_FIRSTADDR);
440 i386_darwin_dr_set (DR_FIRSTADDR + regnum, addr);
444 i386_darwin_dr_get_addr (int regnum)
446 return i386_darwin_dr_get (regnum);
450 i386_darwin_dr_get_status (void)
452 return i386_darwin_dr_get (DR_STATUS);
456 i386_darwin_dr_get_control (void)
458 return i386_darwin_dr_get (DR_CONTROL);
462 darwin_check_osabi (darwin_inferior *inf, thread_t thread)
464 if (gdbarch_osabi (target_gdbarch ()) == GDB_OSABI_UNKNOWN)
466 /* Attaching to a process. Let's figure out what kind it is. */
467 x86_thread_state_t gp_regs;
468 struct gdbarch_info info;
469 unsigned int gp_count = x86_THREAD_STATE_COUNT;
472 ret = thread_get_state (thread, x86_THREAD_STATE,
473 (thread_state_t) &gp_regs, &gp_count);
474 if (ret != KERN_SUCCESS)
476 MACH_CHECK_ERROR (ret);
480 gdbarch_info_init (&info);
481 gdbarch_info_fill (&info);
482 info.byte_order = gdbarch_byte_order (target_gdbarch ());
483 info.osabi = GDB_OSABI_DARWIN;
484 if (gp_regs.tsh.flavor == x86_THREAD_STATE64)
485 info.bfd_arch_info = bfd_lookup_arch (bfd_arch_i386,
488 info.bfd_arch_info = bfd_lookup_arch (bfd_arch_i386,
490 gdbarch_update_p (info);
494 #define X86_EFLAGS_T 0x100UL
496 /* Returning from a signal trampoline is done by calling a
497 special system call (sigreturn). This system call
498 restores the registers that were saved when the signal was
499 raised, including %eflags/%rflags. That means that single-stepping
500 won't work. Instead, we'll have to modify the signal context
501 that's about to be restored, and set the trace flag there. */
504 i386_darwin_sstep_at_sigreturn (x86_thread_state_t *regs)
506 enum bfd_endian byte_order = gdbarch_byte_order (target_gdbarch ());
507 static const gdb_byte darwin_syscall[] = { 0xcd, 0x80 }; /* int 0x80 */
508 gdb_byte buf[sizeof (darwin_syscall)];
510 /* Check if PC is at a sigreturn system call. */
511 if (target_read_memory (regs->uts.ts32.__eip, buf, sizeof (buf)) == 0
512 && memcmp (buf, darwin_syscall, sizeof (darwin_syscall)) == 0
513 && regs->uts.ts32.__eax == 0xb8 /* SYS_sigreturn */)
520 uctx_addr = read_memory_unsigned_integer
521 (regs->uts.ts32.__esp + 4, 4, byte_order);
522 mctx_addr = read_memory_unsigned_integer
523 (uctx_addr + 28, 4, byte_order);
525 flags_addr = mctx_addr + 12 + 9 * 4;
526 read_memory (flags_addr, (gdb_byte *) &eflags, 4);
527 eflags |= X86_EFLAGS_T;
528 write_memory (flags_addr, (gdb_byte *) &eflags, 4);
537 amd64_darwin_sstep_at_sigreturn (x86_thread_state_t *regs)
539 enum bfd_endian byte_order = gdbarch_byte_order (target_gdbarch ());
540 static const gdb_byte darwin_syscall[] = { 0x0f, 0x05 }; /* syscall */
541 gdb_byte buf[sizeof (darwin_syscall)];
543 /* Check if PC is at a sigreturn system call. */
544 if (target_read_memory (regs->uts.ts64.__rip, buf, sizeof (buf)) == 0
545 && memcmp (buf, darwin_syscall, sizeof (darwin_syscall)) == 0
546 && (regs->uts.ts64.__rax & 0xffffffff) == 0x20000b8 /* SYS_sigreturn */)
552 mctx_addr = read_memory_unsigned_integer
553 (regs->uts.ts64.__rdi + 48, 8, byte_order);
554 flags_addr = mctx_addr + 16 + 17 * 8;
556 /* AMD64 is little endian. */
557 read_memory (flags_addr, (gdb_byte *) &rflags, 4);
558 rflags |= X86_EFLAGS_T;
559 write_memory (flags_addr, (gdb_byte *) &rflags, 4);
568 darwin_set_sstep (thread_t thread, int enable)
570 x86_thread_state_t regs;
571 unsigned int count = x86_THREAD_STATE_COUNT;
574 kret = thread_get_state (thread, x86_THREAD_STATE,
575 (thread_state_t) ®s, &count);
576 if (kret != KERN_SUCCESS)
578 printf_unfiltered (_("darwin_set_sstep: error %x, thread=%x\n"),
583 switch (regs.tsh.flavor)
585 case x86_THREAD_STATE32:
587 __uint32_t bit = enable ? X86_EFLAGS_T : 0;
589 if (enable && i386_darwin_sstep_at_sigreturn (®s))
591 if ((regs.uts.ts32.__eflags & X86_EFLAGS_T) == bit)
593 regs.uts.ts32.__eflags
594 = (regs.uts.ts32.__eflags & ~X86_EFLAGS_T) | bit;
595 kret = thread_set_state (thread, x86_THREAD_STATE,
596 (thread_state_t) ®s, count);
597 MACH_CHECK_ERROR (kret);
601 case x86_THREAD_STATE64:
603 __uint64_t bit = enable ? X86_EFLAGS_T : 0;
605 if (enable && amd64_darwin_sstep_at_sigreturn (®s))
607 if ((regs.uts.ts64.__rflags & X86_EFLAGS_T) == bit)
609 regs.uts.ts64.__rflags
610 = (regs.uts.ts64.__rflags & ~X86_EFLAGS_T) | bit;
611 kret = thread_set_state (thread, x86_THREAD_STATE,
612 (thread_state_t) ®s, count);
613 MACH_CHECK_ERROR (kret);
618 error (_("darwin_set_sstep: unknown flavour: %d"), regs.tsh.flavor);
623 darwin_complete_target (struct target_ops *target)
626 amd64_native_gregset64_reg_offset = amd64_darwin_thread_state_reg_offset;
627 amd64_native_gregset64_num_regs = amd64_darwin_thread_state_num_regs;
628 amd64_native_gregset32_reg_offset = i386_darwin_thread_state_reg_offset;
629 amd64_native_gregset32_num_regs = i386_darwin_thread_state_num_regs;
632 i386_use_watchpoints (target);
634 i386_dr_low.set_control = i386_darwin_dr_set_control;
635 i386_dr_low.set_addr = i386_darwin_dr_set_addr;
636 i386_dr_low.get_addr = i386_darwin_dr_get_addr;
637 i386_dr_low.get_status = i386_darwin_dr_get_status;
638 i386_dr_low.get_control = i386_darwin_dr_get_control;
640 /* Let's assume that the kernel is 64 bits iff the executable is. */
642 i386_set_debug_register_length (8);
644 i386_set_debug_register_length (4);
647 target->to_fetch_registers = i386_darwin_fetch_inferior_registers;
648 target->to_store_registers = i386_darwin_store_inferior_registers;