1 /* Darwin support for GDB, the GNU debugger.
2 Copyright 1997-2002, 2008-2012 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"
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%ulx"),
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%ulx"),
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 i386_thread_state_t gp_regs;
106 unsigned int gp_count = i386_THREAD_STATE_COUNT;
110 ret = thread_get_state
111 (current_thread, i386_THREAD_STATE, (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%ulx"),
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 i386_float_state_t fp_regs;
132 unsigned int fp_count = i386_FLOAT_STATE_COUNT;
135 ret = thread_get_state
136 (current_thread, i386_FLOAT_STATE, (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%ulx"),
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 i386_thread_state_t gp_regs;
220 unsigned int gp_count = i386_THREAD_STATE_COUNT;
223 ret = thread_get_state
224 (current_thread, i386_THREAD_STATE, (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, i386_THREAD_STATE,
235 (thread_state_t) & gp_regs,
236 i386_THREAD_STATE_COUNT);
237 MACH_CHECK_ERROR (ret);
241 || (regno >= I386_ST0_REGNUM && regno < I386_SSE_NUM_REGS))
243 i386_float_state_t fp_regs;
244 unsigned int fp_count = i386_FLOAT_STATE_COUNT;
247 ret = thread_get_state
248 (current_thread, i386_FLOAT_STATE, (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, i386_FLOAT_STATE,
255 (thread_state_t) & fp_regs,
256 i386_FLOAT_STATE_COUNT);
257 MACH_CHECK_ERROR (ret);
263 /* Support for debug registers, boosted mostly from i386-linux-nat.c. */
266 i386_darwin_dr_set (int regnum, uint32_t value)
269 thread_t current_thread;
270 x86_debug_state_t dr_regs;
272 unsigned int dr_count = x86_DEBUG_STATE_COUNT;
274 gdb_assert (regnum >= 0 && regnum <= DR_CONTROL);
276 current_thread = ptid_get_tid (inferior_ptid);
278 dr_regs.dsh.flavor = x86_DEBUG_STATE32;
279 dr_regs.dsh.count = x86_DEBUG_STATE32_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);
284 if (ret != KERN_SUCCESS)
286 printf_unfiltered (_("Error reading debug registers "
287 "thread 0x%x via thread_get_state\n"),
288 (int) current_thread);
289 MACH_CHECK_ERROR (ret);
295 dr_regs.uds.ds32.__dr0 = value;
298 dr_regs.uds.ds32.__dr1 = value;
301 dr_regs.uds.ds32.__dr2 = value;
304 dr_regs.uds.ds32.__dr3 = value;
307 dr_regs.uds.ds32.__dr4 = value;
310 dr_regs.uds.ds32.__dr5 = value;
313 dr_regs.uds.ds32.__dr6 = value;
316 dr_regs.uds.ds32.__dr7 = value;
320 ret = thread_set_state (current_thread, x86_DEBUG_STATE,
321 (thread_state_t) &dr_regs, dr_count);
323 if (ret != KERN_SUCCESS)
325 printf_unfiltered (_("Error writing debug registers "
326 "thread 0x%x via thread_get_state\n"),
327 (int) current_thread);
328 MACH_CHECK_ERROR (ret);
333 i386_darwin_dr_get (int regnum)
335 thread_t current_thread;
336 x86_debug_state_t dr_regs;
338 unsigned int dr_count = x86_DEBUG_STATE_COUNT;
340 gdb_assert (regnum >= 0 && regnum <= DR_CONTROL);
342 current_thread = ptid_get_tid (inferior_ptid);
344 dr_regs.dsh.flavor = x86_DEBUG_STATE32;
345 dr_regs.dsh.count = x86_DEBUG_STATE32_COUNT;
346 dr_count = x86_DEBUG_STATE_COUNT;
347 ret = thread_get_state (current_thread, x86_DEBUG_STATE,
348 (thread_state_t) &dr_regs, &dr_count);
350 if (ret != KERN_SUCCESS)
352 printf_unfiltered (_("Error reading debug registers "
353 "thread 0x%x via thread_get_state\n"),
354 (int) current_thread);
355 MACH_CHECK_ERROR (ret);
361 return dr_regs.uds.ds32.__dr0;
363 return dr_regs.uds.ds32.__dr1;
365 return dr_regs.uds.ds32.__dr2;
367 return dr_regs.uds.ds32.__dr3;
369 return dr_regs.uds.ds32.__dr4;
371 return dr_regs.uds.ds32.__dr5;
373 return dr_regs.uds.ds32.__dr6;
375 return dr_regs.uds.ds32.__dr7;
382 i386_darwin_dr_set_control (unsigned long control)
384 i386_darwin_dr_set (DR_CONTROL, control);
388 i386_darwin_dr_set_addr (int regnum, CORE_ADDR addr)
390 gdb_assert (regnum >= 0 && regnum <= DR_LASTADDR - DR_FIRSTADDR);
392 i386_darwin_dr_set (DR_FIRSTADDR + regnum, addr);
396 i386_darwin_dr_get_addr (int regnum)
398 return i386_darwin_dr_get (regnum);
402 i386_darwin_dr_get_status (void)
404 return i386_darwin_dr_get (DR_STATUS);
408 i386_darwin_dr_get_control (void)
410 return i386_darwin_dr_get (DR_CONTROL);
414 darwin_check_osabi (darwin_inferior *inf, thread_t thread)
416 if (gdbarch_osabi (target_gdbarch) == GDB_OSABI_UNKNOWN)
418 /* Attaching to a process. Let's figure out what kind it is. */
419 x86_thread_state_t gp_regs;
420 struct gdbarch_info info;
421 unsigned int gp_count = x86_THREAD_STATE_COUNT;
424 ret = thread_get_state (thread, x86_THREAD_STATE,
425 (thread_state_t) &gp_regs, &gp_count);
426 if (ret != KERN_SUCCESS)
428 MACH_CHECK_ERROR (ret);
432 gdbarch_info_init (&info);
433 gdbarch_info_fill (&info);
434 info.byte_order = gdbarch_byte_order (target_gdbarch);
435 info.osabi = GDB_OSABI_DARWIN;
436 if (gp_regs.tsh.flavor == x86_THREAD_STATE64)
437 info.bfd_arch_info = bfd_lookup_arch (bfd_arch_i386,
440 info.bfd_arch_info = bfd_lookup_arch (bfd_arch_i386,
442 gdbarch_update_p (info);
446 #define X86_EFLAGS_T 0x100UL
448 /* Returning from a signal trampoline is done by calling a
449 special system call (sigreturn). This system call
450 restores the registers that were saved when the signal was
451 raised, including %eflags/%rflags. That means that single-stepping
452 won't work. Instead, we'll have to modify the signal context
453 that's about to be restored, and set the trace flag there. */
456 i386_darwin_sstep_at_sigreturn (x86_thread_state_t *regs)
458 enum bfd_endian byte_order = gdbarch_byte_order (target_gdbarch);
459 static const gdb_byte darwin_syscall[] = { 0xcd, 0x80 }; /* int 0x80 */
460 gdb_byte buf[sizeof (darwin_syscall)];
462 /* Check if PC is at a sigreturn system call. */
463 if (target_read_memory (regs->uts.ts32.__eip, buf, sizeof (buf)) == 0
464 && memcmp (buf, darwin_syscall, sizeof (darwin_syscall)) == 0
465 && regs->uts.ts32.__eax == 0xb8 /* SYS_sigreturn */)
472 uctx_addr = read_memory_unsigned_integer
473 (regs->uts.ts32.__esp + 4, 4, byte_order);
474 mctx_addr = read_memory_unsigned_integer
475 (uctx_addr + 28, 4, byte_order);
477 flags_addr = mctx_addr + 12 + 9 * 4;
478 read_memory (flags_addr, (gdb_byte *) &eflags, 4);
479 eflags |= X86_EFLAGS_T;
480 write_memory (flags_addr, (gdb_byte *) &eflags, 4);
489 amd64_darwin_sstep_at_sigreturn (x86_thread_state_t *regs)
491 enum bfd_endian byte_order = gdbarch_byte_order (target_gdbarch);
492 static const gdb_byte darwin_syscall[] = { 0x0f, 0x05 }; /* syscall */
493 gdb_byte buf[sizeof (darwin_syscall)];
495 /* Check if PC is at a sigreturn system call. */
496 if (target_read_memory (regs->uts.ts64.__rip, buf, sizeof (buf)) == 0
497 && memcmp (buf, darwin_syscall, sizeof (darwin_syscall)) == 0
498 && (regs->uts.ts64.__rax & 0xffffffff) == 0x20000b8 /* SYS_sigreturn */)
504 mctx_addr = read_memory_unsigned_integer
505 (regs->uts.ts64.__rdi + 48, 8, byte_order);
506 flags_addr = mctx_addr + 16 + 17 * 8;
508 /* AMD64 is little endian. */
509 read_memory (flags_addr, (gdb_byte *) &rflags, 4);
510 rflags |= X86_EFLAGS_T;
511 write_memory (flags_addr, (gdb_byte *) &rflags, 4);
520 darwin_set_sstep (thread_t thread, int enable)
522 x86_thread_state_t regs;
523 unsigned int count = x86_THREAD_STATE_COUNT;
526 kret = thread_get_state (thread, x86_THREAD_STATE,
527 (thread_state_t) ®s, &count);
528 if (kret != KERN_SUCCESS)
530 printf_unfiltered (_("darwin_set_sstep: error %x, thread=%x\n"),
535 switch (regs.tsh.flavor)
537 case x86_THREAD_STATE32:
539 __uint32_t bit = enable ? X86_EFLAGS_T : 0;
541 if (enable && i386_darwin_sstep_at_sigreturn (®s))
543 if ((regs.uts.ts32.__eflags & X86_EFLAGS_T) == bit)
545 regs.uts.ts32.__eflags
546 = (regs.uts.ts32.__eflags & ~X86_EFLAGS_T) | bit;
547 kret = thread_set_state (thread, x86_THREAD_STATE,
548 (thread_state_t) ®s, count);
549 MACH_CHECK_ERROR (kret);
553 case x86_THREAD_STATE64:
555 __uint64_t bit = enable ? X86_EFLAGS_T : 0;
557 if (enable && amd64_darwin_sstep_at_sigreturn (®s))
559 if ((regs.uts.ts64.__rflags & X86_EFLAGS_T) == bit)
561 regs.uts.ts64.__rflags
562 = (regs.uts.ts64.__rflags & ~X86_EFLAGS_T) | bit;
563 kret = thread_set_state (thread, x86_THREAD_STATE,
564 (thread_state_t) ®s, count);
565 MACH_CHECK_ERROR (kret);
570 error (_("darwin_set_sstep: unknown flavour: %d"), regs.tsh.flavor);
575 darwin_complete_target (struct target_ops *target)
578 amd64_native_gregset64_reg_offset = amd64_darwin_thread_state_reg_offset;
579 amd64_native_gregset64_num_regs = amd64_darwin_thread_state_num_regs;
580 amd64_native_gregset32_reg_offset = i386_darwin_thread_state_reg_offset;
581 amd64_native_gregset32_num_regs = i386_darwin_thread_state_num_regs;
584 target->to_fetch_registers = i386_darwin_fetch_inferior_registers;
585 target->to_store_registers = i386_darwin_store_inferior_registers;