1 /* Darwin support for GDB, the GNU debugger.
2 Copyright (C) 1997-2015 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);
77 /* Some kernels don't sanitize the values. */
78 gp_regs.uts.ts64.__fs &= 0xffff;
79 gp_regs.uts.ts64.__gs &= 0xffff;
81 amd64_supply_native_gregset (regcache, &gp_regs.uts, -1);
85 if (regno == -1 || !amd64_native_gregset_supplies_p (gdbarch, regno))
87 x86_float_state_t fp_regs;
88 unsigned int fp_count = x86_FLOAT_STATE_COUNT;
91 ret = thread_get_state
92 (current_thread, x86_FLOAT_STATE, (thread_state_t) & fp_regs,
94 if (ret != KERN_SUCCESS)
96 printf_unfiltered (_("Error calling thread_get_state for "
97 "float registers for thread 0x%lx\n"),
98 (unsigned long) current_thread);
99 MACH_CHECK_ERROR (ret);
101 amd64_supply_fxsave (regcache, -1, &fp_regs.ufs.fs64.__fpu_fcw);
108 if (regno == -1 || regno < I386_NUM_GREGS)
110 x86_thread_state32_t gp_regs;
111 unsigned int gp_count = x86_THREAD_STATE32_COUNT;
115 ret = thread_get_state
116 (current_thread, x86_THREAD_STATE32, (thread_state_t) &gp_regs,
118 if (ret != KERN_SUCCESS)
120 printf_unfiltered (_("Error calling thread_get_state for "
121 "GP registers for thread 0x%lx\n"),
122 (unsigned long) current_thread);
123 MACH_CHECK_ERROR (ret);
125 for (i = 0; i < I386_NUM_GREGS; i++)
128 (char *)&gp_regs + i386_darwin_thread_state_reg_offset[i]);
134 || (regno >= I386_ST0_REGNUM && regno < I386_SSE_NUM_REGS))
136 x86_float_state32_t fp_regs;
137 unsigned int fp_count = x86_FLOAT_STATE32_COUNT;
140 ret = thread_get_state
141 (current_thread, x86_FLOAT_STATE32, (thread_state_t) &fp_regs,
143 if (ret != KERN_SUCCESS)
145 printf_unfiltered (_("Error calling thread_get_state for "
146 "float registers for thread 0x%lx\n"),
147 (unsigned long) current_thread);
148 MACH_CHECK_ERROR (ret);
150 i387_supply_fxsave (regcache, -1, &fp_regs.__fpu_fcw);
157 warning (_("unknown register %d"), regno);
158 regcache_raw_supply (regcache, regno, NULL);
162 /* Store our register values back into the inferior.
163 If REGNO is -1, do this for all registers.
164 Otherwise, REGNO specifies which register (so we can save time). */
167 i386_darwin_store_inferior_registers (struct target_ops *ops,
168 struct regcache *regcache, int regno)
170 thread_t current_thread = ptid_get_tid (inferior_ptid);
171 struct gdbarch *gdbarch = get_regcache_arch (regcache);
174 if (gdbarch_ptr_bit (gdbarch) == 64)
176 if (regno == -1 || amd64_native_gregset_supplies_p (gdbarch, regno))
178 x86_thread_state_t gp_regs;
180 unsigned int gp_count = x86_THREAD_STATE_COUNT;
182 ret = thread_get_state
183 (current_thread, x86_THREAD_STATE, (thread_state_t) &gp_regs,
185 MACH_CHECK_ERROR (ret);
186 gdb_assert (gp_regs.tsh.flavor == x86_THREAD_STATE64);
187 gdb_assert (gp_regs.tsh.count == x86_THREAD_STATE64_COUNT);
189 amd64_collect_native_gregset (regcache, &gp_regs.uts, regno);
191 /* Some kernels don't sanitize the values. */
192 gp_regs.uts.ts64.__fs &= 0xffff;
193 gp_regs.uts.ts64.__gs &= 0xffff;
195 ret = thread_set_state (current_thread, x86_THREAD_STATE,
196 (thread_state_t) &gp_regs,
197 x86_THREAD_STATE_COUNT);
198 MACH_CHECK_ERROR (ret);
201 if (regno == -1 || !amd64_native_gregset_supplies_p (gdbarch, regno))
203 x86_float_state_t fp_regs;
205 unsigned int fp_count = x86_FLOAT_STATE_COUNT;
207 ret = thread_get_state
208 (current_thread, x86_FLOAT_STATE, (thread_state_t) & fp_regs,
210 MACH_CHECK_ERROR (ret);
211 gdb_assert (fp_regs.fsh.flavor == x86_FLOAT_STATE64);
212 gdb_assert (fp_regs.fsh.count == x86_FLOAT_STATE64_COUNT);
214 amd64_collect_fxsave (regcache, regno, &fp_regs.ufs.fs64.__fpu_fcw);
216 ret = thread_set_state (current_thread, x86_FLOAT_STATE,
217 (thread_state_t) & fp_regs,
218 x86_FLOAT_STATE_COUNT);
219 MACH_CHECK_ERROR (ret);
225 if (regno == -1 || regno < I386_NUM_GREGS)
227 x86_thread_state32_t gp_regs;
229 unsigned int gp_count = x86_THREAD_STATE32_COUNT;
232 ret = thread_get_state
233 (current_thread, x86_THREAD_STATE32, (thread_state_t) &gp_regs,
235 MACH_CHECK_ERROR (ret);
237 for (i = 0; i < I386_NUM_GREGS; i++)
238 if (regno == -1 || regno == i)
241 (char *)&gp_regs + i386_darwin_thread_state_reg_offset[i]);
243 ret = thread_set_state (current_thread, x86_THREAD_STATE32,
244 (thread_state_t) &gp_regs,
245 x86_THREAD_STATE32_COUNT);
246 MACH_CHECK_ERROR (ret);
250 || (regno >= I386_ST0_REGNUM && regno < I386_SSE_NUM_REGS))
252 x86_float_state32_t fp_regs;
253 unsigned int fp_count = x86_FLOAT_STATE32_COUNT;
256 ret = thread_get_state
257 (current_thread, x86_FLOAT_STATE32, (thread_state_t) & fp_regs,
259 MACH_CHECK_ERROR (ret);
261 i387_collect_fxsave (regcache, regno, &fp_regs.__fpu_fcw);
263 ret = thread_set_state (current_thread, x86_FLOAT_STATE32,
264 (thread_state_t) &fp_regs,
265 x86_FLOAT_STATE32_COUNT);
266 MACH_CHECK_ERROR (ret);
271 /* Support for debug registers, boosted mostly from i386-linux-nat.c. */
274 i386_darwin_dr_set (int regnum, CORE_ADDR value)
277 thread_t current_thread;
278 x86_debug_state_t dr_regs;
280 unsigned int dr_count;
282 gdb_assert (regnum >= 0 && regnum <= DR_CONTROL);
284 current_thread = ptid_get_tid (inferior_ptid);
286 dr_regs.dsh.flavor = x86_DEBUG_STATE;
287 dr_regs.dsh.count = x86_DEBUG_STATE_COUNT;
288 dr_count = x86_DEBUG_STATE_COUNT;
289 ret = thread_get_state (current_thread, x86_DEBUG_STATE,
290 (thread_state_t) &dr_regs, &dr_count);
291 MACH_CHECK_ERROR (ret);
293 switch (dr_regs.dsh.flavor)
295 case x86_DEBUG_STATE32:
299 dr_regs.uds.ds32.__dr0 = value;
302 dr_regs.uds.ds32.__dr1 = value;
305 dr_regs.uds.ds32.__dr2 = value;
308 dr_regs.uds.ds32.__dr3 = value;
311 dr_regs.uds.ds32.__dr4 = value;
314 dr_regs.uds.ds32.__dr5 = value;
317 dr_regs.uds.ds32.__dr6 = value;
320 dr_regs.uds.ds32.__dr7 = value;
325 case x86_DEBUG_STATE64:
329 dr_regs.uds.ds64.__dr0 = value;
332 dr_regs.uds.ds64.__dr1 = value;
335 dr_regs.uds.ds64.__dr2 = value;
338 dr_regs.uds.ds64.__dr3 = value;
341 dr_regs.uds.ds64.__dr4 = value;
344 dr_regs.uds.ds64.__dr5 = value;
347 dr_regs.uds.ds64.__dr6 = value;
350 dr_regs.uds.ds64.__dr7 = value;
357 ret = thread_set_state (current_thread, dr_regs.dsh.flavor,
358 (thread_state_t) &dr_regs.uds, dr_count);
360 MACH_CHECK_ERROR (ret);
364 i386_darwin_dr_get (int regnum)
366 thread_t current_thread;
367 x86_debug_state_t dr_regs;
369 unsigned int dr_count;
371 gdb_assert (regnum >= 0 && regnum <= DR_CONTROL);
373 current_thread = ptid_get_tid (inferior_ptid);
375 dr_regs.dsh.flavor = x86_DEBUG_STATE;
376 dr_regs.dsh.count = x86_DEBUG_STATE_COUNT;
377 dr_count = x86_DEBUG_STATE_COUNT;
378 ret = thread_get_state (current_thread, x86_DEBUG_STATE,
379 (thread_state_t) &dr_regs, &dr_count);
380 MACH_CHECK_ERROR (ret);
382 switch (dr_regs.dsh.flavor)
384 case x86_DEBUG_STATE32:
388 return dr_regs.uds.ds32.__dr0;
390 return dr_regs.uds.ds32.__dr1;
392 return dr_regs.uds.ds32.__dr2;
394 return dr_regs.uds.ds32.__dr3;
396 return dr_regs.uds.ds32.__dr4;
398 return dr_regs.uds.ds32.__dr5;
400 return dr_regs.uds.ds32.__dr6;
402 return dr_regs.uds.ds32.__dr7;
408 case x86_DEBUG_STATE64:
412 return dr_regs.uds.ds64.__dr0;
414 return dr_regs.uds.ds64.__dr1;
416 return dr_regs.uds.ds64.__dr2;
418 return dr_regs.uds.ds64.__dr3;
420 return dr_regs.uds.ds64.__dr4;
422 return dr_regs.uds.ds64.__dr5;
424 return dr_regs.uds.ds64.__dr6;
426 return dr_regs.uds.ds64.__dr7;
438 i386_darwin_dr_set_control (unsigned long control)
440 i386_darwin_dr_set (DR_CONTROL, control);
444 i386_darwin_dr_set_addr (int regnum, CORE_ADDR addr)
446 gdb_assert (regnum >= 0 && regnum <= DR_LASTADDR - DR_FIRSTADDR);
448 i386_darwin_dr_set (DR_FIRSTADDR + regnum, addr);
452 i386_darwin_dr_get_addr (int regnum)
454 return i386_darwin_dr_get (regnum);
458 i386_darwin_dr_get_status (void)
460 return i386_darwin_dr_get (DR_STATUS);
464 i386_darwin_dr_get_control (void)
466 return i386_darwin_dr_get (DR_CONTROL);
470 darwin_check_osabi (darwin_inferior *inf, thread_t thread)
472 if (gdbarch_osabi (target_gdbarch ()) == GDB_OSABI_UNKNOWN)
474 /* Attaching to a process. Let's figure out what kind it is. */
475 x86_thread_state_t gp_regs;
476 struct gdbarch_info info;
477 unsigned int gp_count = x86_THREAD_STATE_COUNT;
480 ret = thread_get_state (thread, x86_THREAD_STATE,
481 (thread_state_t) &gp_regs, &gp_count);
482 if (ret != KERN_SUCCESS)
484 MACH_CHECK_ERROR (ret);
488 gdbarch_info_init (&info);
489 gdbarch_info_fill (&info);
490 info.byte_order = gdbarch_byte_order (target_gdbarch ());
491 info.osabi = GDB_OSABI_DARWIN;
492 if (gp_regs.tsh.flavor == x86_THREAD_STATE64)
493 info.bfd_arch_info = bfd_lookup_arch (bfd_arch_i386,
496 info.bfd_arch_info = bfd_lookup_arch (bfd_arch_i386,
498 gdbarch_update_p (info);
502 #define X86_EFLAGS_T 0x100UL
504 /* Returning from a signal trampoline is done by calling a
505 special system call (sigreturn). This system call
506 restores the registers that were saved when the signal was
507 raised, including %eflags/%rflags. That means that single-stepping
508 won't work. Instead, we'll have to modify the signal context
509 that's about to be restored, and set the trace flag there. */
512 i386_darwin_sstep_at_sigreturn (x86_thread_state_t *regs)
514 enum bfd_endian byte_order = gdbarch_byte_order (target_gdbarch ());
515 static const gdb_byte darwin_syscall[] = { 0xcd, 0x80 }; /* int 0x80 */
516 gdb_byte buf[sizeof (darwin_syscall)];
518 /* Check if PC is at a sigreturn system call. */
519 if (target_read_memory (regs->uts.ts32.__eip, buf, sizeof (buf)) == 0
520 && memcmp (buf, darwin_syscall, sizeof (darwin_syscall)) == 0
521 && regs->uts.ts32.__eax == 0xb8 /* SYS_sigreturn */)
528 uctx_addr = read_memory_unsigned_integer
529 (regs->uts.ts32.__esp + 4, 4, byte_order);
530 mctx_addr = read_memory_unsigned_integer
531 (uctx_addr + 28, 4, byte_order);
533 flags_addr = mctx_addr + 12 + 9 * 4;
534 read_memory (flags_addr, (gdb_byte *) &eflags, 4);
535 eflags |= X86_EFLAGS_T;
536 write_memory (flags_addr, (gdb_byte *) &eflags, 4);
545 amd64_darwin_sstep_at_sigreturn (x86_thread_state_t *regs)
547 enum bfd_endian byte_order = gdbarch_byte_order (target_gdbarch ());
548 static const gdb_byte darwin_syscall[] = { 0x0f, 0x05 }; /* syscall */
549 gdb_byte buf[sizeof (darwin_syscall)];
551 /* Check if PC is at a sigreturn system call. */
552 if (target_read_memory (regs->uts.ts64.__rip, buf, sizeof (buf)) == 0
553 && memcmp (buf, darwin_syscall, sizeof (darwin_syscall)) == 0
554 && (regs->uts.ts64.__rax & 0xffffffff) == 0x20000b8 /* SYS_sigreturn */)
560 mctx_addr = read_memory_unsigned_integer
561 (regs->uts.ts64.__rdi + 48, 8, byte_order);
562 flags_addr = mctx_addr + 16 + 17 * 8;
564 /* AMD64 is little endian. */
565 read_memory (flags_addr, (gdb_byte *) &rflags, 4);
566 rflags |= X86_EFLAGS_T;
567 write_memory (flags_addr, (gdb_byte *) &rflags, 4);
576 darwin_set_sstep (thread_t thread, int enable)
578 x86_thread_state_t regs;
579 unsigned int count = x86_THREAD_STATE_COUNT;
582 kret = thread_get_state (thread, x86_THREAD_STATE,
583 (thread_state_t) ®s, &count);
584 if (kret != KERN_SUCCESS)
586 printf_unfiltered (_("darwin_set_sstep: error %x, thread=%x\n"),
591 switch (regs.tsh.flavor)
593 case x86_THREAD_STATE32:
595 __uint32_t bit = enable ? X86_EFLAGS_T : 0;
597 if (enable && i386_darwin_sstep_at_sigreturn (®s))
599 if ((regs.uts.ts32.__eflags & X86_EFLAGS_T) == bit)
601 regs.uts.ts32.__eflags
602 = (regs.uts.ts32.__eflags & ~X86_EFLAGS_T) | bit;
603 kret = thread_set_state (thread, x86_THREAD_STATE,
604 (thread_state_t) ®s, count);
605 MACH_CHECK_ERROR (kret);
609 case x86_THREAD_STATE64:
611 __uint64_t bit = enable ? X86_EFLAGS_T : 0;
613 if (enable && amd64_darwin_sstep_at_sigreturn (®s))
615 if ((regs.uts.ts64.__rflags & X86_EFLAGS_T) == bit)
617 regs.uts.ts64.__rflags
618 = (regs.uts.ts64.__rflags & ~X86_EFLAGS_T) | bit;
619 kret = thread_set_state (thread, x86_THREAD_STATE,
620 (thread_state_t) ®s, count);
621 MACH_CHECK_ERROR (kret);
626 error (_("darwin_set_sstep: unknown flavour: %d"), regs.tsh.flavor);
631 darwin_complete_target (struct target_ops *target)
634 amd64_native_gregset64_reg_offset = amd64_darwin_thread_state_reg_offset;
635 amd64_native_gregset64_num_regs = amd64_darwin_thread_state_num_regs;
636 amd64_native_gregset32_reg_offset = i386_darwin_thread_state_reg_offset;
637 amd64_native_gregset32_num_regs = i386_darwin_thread_state_num_regs;
640 x86_use_watchpoints (target);
642 x86_dr_low.set_control = i386_darwin_dr_set_control;
643 x86_dr_low.set_addr = i386_darwin_dr_set_addr;
644 x86_dr_low.get_addr = i386_darwin_dr_get_addr;
645 x86_dr_low.get_status = i386_darwin_dr_get_status;
646 x86_dr_low.get_control = i386_darwin_dr_get_control;
648 /* Let's assume that the kernel is 64 bits iff the executable is. */
650 x86_set_debug_register_length (8);
652 x86_set_debug_register_length (4);
655 target->to_fetch_registers = i386_darwin_fetch_inferior_registers;
656 target->to_store_registers = i386_darwin_store_inferior_registers;