1 /* Darwin support for GDB, the GNU debugger.
2 Copyright 1997, 1998, 1999, 2000, 2001, 2002, 2008
3 Free Software Foundation, Inc.
5 Contributed by Apple Computer, Inc.
7 This file is part of GDB.
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program. If not, see <http://www.gnu.org/licenses/>. */
31 #include "gdb_assert.h"
32 #include "i386-tdep.h"
33 #include "amd64-nat.h"
34 #include "i387-tdep.h"
36 #include "arch-utils.h"
38 #include "darwin-nat.h"
39 #include "i386-darwin-tdep.h"
41 /* Read register values from the inferior process.
42 If REGNO is -1, do this for all registers.
43 Otherwise, REGNO specifies which register (so we can save time). */
45 i386_darwin_fetch_inferior_registers (struct regcache *regcache, int regno)
47 thread_t current_thread = ptid_get_tid (inferior_ptid);
49 struct gdbarch *gdbarch = get_regcache_arch (regcache);
51 if (gdbarch_ptr_bit (gdbarch) == 64)
53 if (regno == -1 || amd64_native_gregset_supplies_p (gdbarch, regno))
55 x86_thread_state_t gp_regs;
56 unsigned int gp_count = x86_THREAD_STATE_COUNT;
59 ret = thread_get_state
60 (current_thread, x86_THREAD_STATE, (thread_state_t) & gp_regs,
62 if (ret != KERN_SUCCESS)
64 printf_unfiltered (_("Error calling thread_get_state for GP registers for thread 0x%ulx"), current_thread);
65 MACH_CHECK_ERROR (ret);
67 amd64_supply_native_gregset (regcache, &gp_regs.uts, -1);
71 if (regno == -1 || !amd64_native_gregset_supplies_p (gdbarch, regno))
73 x86_float_state_t fp_regs;
74 unsigned int fp_count = x86_FLOAT_STATE_COUNT;
77 ret = thread_get_state
78 (current_thread, x86_FLOAT_STATE, (thread_state_t) & fp_regs,
80 if (ret != KERN_SUCCESS)
82 printf_unfiltered (_("Error calling thread_get_state for float registers for thread 0x%ulx"), current_thread);
83 MACH_CHECK_ERROR (ret);
85 i387_supply_fxsave (regcache, -1, &fp_regs.ufs.fs64);
91 if (regno == -1 || regno < I386_NUM_GREGS)
93 i386_thread_state_t gp_regs;
94 unsigned int gp_count = i386_THREAD_STATE_COUNT;
98 ret = thread_get_state
99 (current_thread, i386_THREAD_STATE, (thread_state_t) & gp_regs,
101 if (ret != KERN_SUCCESS)
103 printf_unfiltered (_("Error calling thread_get_state for GP registers for thread 0x%ulx"), current_thread);
104 MACH_CHECK_ERROR (ret);
106 for (i = 0; i < I386_NUM_GREGS; i++)
109 (char *)&gp_regs + i386_darwin_thread_state_reg_offset[i]);
115 || (regno >= I386_ST0_REGNUM && regno < I386_SSE_NUM_REGS))
117 i386_float_state_t fp_regs;
118 unsigned int fp_count = i386_FLOAT_STATE_COUNT;
121 ret = thread_get_state
122 (current_thread, i386_FLOAT_STATE, (thread_state_t) & fp_regs,
124 if (ret != KERN_SUCCESS)
126 printf_unfiltered (_("Error calling thread_get_state for float registers for thread 0x%ulx"), current_thread);
127 MACH_CHECK_ERROR (ret);
129 i387_supply_fxsave (regcache, -1, &fp_regs.__fpu_fcw);
136 warning (_("unknown register %d"), regno);
137 regcache_raw_supply (regcache, regno, NULL);
141 /* Store our register values back into the inferior.
142 If REGNO is -1, do this for all registers.
143 Otherwise, REGNO specifies which register (so we can save time). */
146 i386_darwin_store_inferior_registers (struct regcache *regcache, int regno)
148 thread_t current_thread = ptid_get_tid (inferior_ptid);
149 struct gdbarch *gdbarch = get_regcache_arch (regcache);
151 if (gdbarch_ptr_bit (gdbarch) == 64)
153 if (regno == -1 || amd64_native_gregset_supplies_p (gdbarch, regno))
155 x86_thread_state_t gp_regs;
157 unsigned int gp_count = x86_THREAD_STATE_COUNT;
159 ret = thread_get_state
160 (current_thread, x86_THREAD_STATE, (thread_state_t) &gp_regs,
162 MACH_CHECK_ERROR (ret);
163 gdb_assert (gp_regs.tsh.flavor == x86_THREAD_STATE64);
164 gdb_assert (gp_regs.tsh.count == x86_THREAD_STATE64_COUNT);
166 amd64_collect_native_gregset (regcache, &gp_regs.uts, regno);
168 ret = thread_set_state (current_thread, x86_THREAD_STATE,
169 (thread_state_t) &gp_regs,
170 x86_THREAD_STATE_COUNT);
171 MACH_CHECK_ERROR (ret);
174 if (regno == -1 || !amd64_native_gregset_supplies_p (gdbarch, regno))
176 x86_float_state_t fp_regs;
178 unsigned int fp_count = x86_FLOAT_STATE_COUNT;
180 ret = thread_get_state
181 (current_thread, x86_FLOAT_STATE, (thread_state_t) & fp_regs,
183 MACH_CHECK_ERROR (ret);
184 gdb_assert (fp_regs.fsh.flavor == x86_FLOAT_STATE64);
185 gdb_assert (fp_regs.fsh.count == x86_FLOAT_STATE64_COUNT);
187 i387_collect_fxsave (regcache, regno, &fp_regs.ufs.fs64.__fpu_fcw);
189 ret = thread_set_state (current_thread, x86_FLOAT_STATE,
190 (thread_state_t) & fp_regs,
191 x86_FLOAT_STATE_COUNT);
192 MACH_CHECK_ERROR (ret);
197 if (regno == -1 || regno < I386_NUM_GREGS)
199 i386_thread_state_t gp_regs;
201 unsigned int gp_count = i386_THREAD_STATE_COUNT;
204 ret = thread_get_state
205 (current_thread, i386_THREAD_STATE, (thread_state_t) & gp_regs,
207 MACH_CHECK_ERROR (ret);
209 for (i = 0; i < I386_NUM_GREGS; i++)
210 if (regno == -1 || regno == i)
213 (char *)&gp_regs + i386_darwin_thread_state_reg_offset[i]);
215 ret = thread_set_state (current_thread, i386_THREAD_STATE,
216 (thread_state_t) & gp_regs,
217 i386_THREAD_STATE_COUNT);
218 MACH_CHECK_ERROR (ret);
222 || (regno >= I386_ST0_REGNUM && regno < I386_SSE_NUM_REGS))
224 i386_float_state_t fp_regs;
225 unsigned int fp_count = i386_FLOAT_STATE_COUNT;
228 ret = thread_get_state
229 (current_thread, i386_FLOAT_STATE, (thread_state_t) & fp_regs,
231 MACH_CHECK_ERROR (ret);
233 i387_collect_fxsave (regcache, regno, &fp_regs.__fpu_fcw);
235 ret = thread_set_state (current_thread, i386_FLOAT_STATE,
236 (thread_state_t) & fp_regs,
237 i386_FLOAT_STATE_COUNT);
238 MACH_CHECK_ERROR (ret);
244 /* Support for debug registers, boosted mostly from i386-linux-nat.c. */
247 #define DR_FIRSTADDR 0
251 #define DR_LASTADDR 3
264 i386_darwin_dr_set (int regnum, uint32_t value)
267 thread_t current_thread;
268 x86_debug_state_t dr_regs;
270 unsigned int dr_count = x86_DEBUG_STATE_COUNT;
272 gdb_assert (regnum >= 0 && regnum <= DR_CONTROL);
274 current_thread = ptid_get_tid (inferior_ptid);
276 dr_regs.dsh.flavor = x86_DEBUG_STATE32;
277 dr_regs.dsh.count = x86_DEBUG_STATE32_COUNT;
278 dr_count = x86_DEBUG_STATE_COUNT;
279 ret = thread_get_state (current_thread, x86_DEBUG_STATE,
280 (thread_state_t) &dr_regs, &dr_count);
282 if (ret != KERN_SUCCESS)
284 printf_unfiltered (_("Error reading debug registers thread 0x%x via thread_get_state\n"), (int) current_thread);
285 MACH_CHECK_ERROR (ret);
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;
316 ret = thread_set_state (current_thread, x86_DEBUG_STATE,
317 (thread_state_t) &dr_regs, dr_count);
319 if (ret != KERN_SUCCESS)
321 printf_unfiltered (_("Error writing debug registers thread 0x%x via thread_get_state\n"), (int) current_thread);
322 MACH_CHECK_ERROR (ret);
327 i386_darwin_dr_get (int regnum)
329 thread_t current_thread;
330 x86_debug_state_t dr_regs;
332 unsigned int dr_count = x86_DEBUG_STATE_COUNT;
334 gdb_assert (regnum >= 0 && regnum <= DR_CONTROL);
336 current_thread = ptid_get_tid (inferior_ptid);
338 dr_regs.dsh.flavor = x86_DEBUG_STATE32;
339 dr_regs.dsh.count = x86_DEBUG_STATE32_COUNT;
340 dr_count = x86_DEBUG_STATE_COUNT;
341 ret = thread_get_state (current_thread, x86_DEBUG_STATE,
342 (thread_state_t) &dr_regs, &dr_count);
344 if (ret != KERN_SUCCESS)
346 printf_unfiltered (_("Error reading debug registers thread 0x%x via thread_get_state\n"), (int) current_thread);
347 MACH_CHECK_ERROR (ret);
353 return dr_regs.uds.ds32.__dr0;
355 return dr_regs.uds.ds32.__dr1;
357 return dr_regs.uds.ds32.__dr2;
359 return dr_regs.uds.ds32.__dr3;
361 return dr_regs.uds.ds32.__dr4;
363 return dr_regs.uds.ds32.__dr5;
365 return dr_regs.uds.ds32.__dr6;
367 return dr_regs.uds.ds32.__dr7;
374 i386_darwin_dr_set_control (unsigned long control)
376 i386_darwin_dr_set (DR_CONTROL, control);
380 i386_darwin_dr_set_addr (int regnum, CORE_ADDR addr)
382 gdb_assert (regnum >= 0 && regnum <= DR_LASTADDR - DR_FIRSTADDR);
384 i386_darwin_dr_set (DR_FIRSTADDR + regnum, addr);
388 i386_darwin_dr_reset_addr (int regnum)
390 gdb_assert (regnum >= 0 && regnum <= DR_LASTADDR - DR_FIRSTADDR);
392 i386_darwin_dr_set (DR_FIRSTADDR + regnum, 0L);
396 i386_darwin_dr_get_status (void)
398 return i386_darwin_dr_get (DR_STATUS);
402 darwin_check_osabi (darwin_inferior *inf, thread_t thread)
404 if (gdbarch_osabi (current_gdbarch) == GDB_OSABI_UNKNOWN)
406 /* Attaching to a process. Let's figure out what kind it is. */
407 x86_thread_state_t gp_regs;
408 struct gdbarch_info info;
409 unsigned int gp_count = x86_THREAD_STATE_COUNT;
412 ret = thread_get_state (thread, x86_THREAD_STATE,
413 (thread_state_t) &gp_regs, &gp_count);
414 if (ret != KERN_SUCCESS)
416 MACH_CHECK_ERROR (ret);
420 gdbarch_info_init (&info);
421 gdbarch_info_fill (&info);
422 info.byte_order = gdbarch_byte_order (current_gdbarch);
423 info.osabi = GDB_OSABI_DARWIN;
424 if (gp_regs.tsh.flavor == x86_THREAD_STATE64)
425 info.bfd_arch_info = bfd_lookup_arch (bfd_arch_i386,
428 info.bfd_arch_info = bfd_lookup_arch (bfd_arch_i386,
430 gdbarch_update_p (info);
434 #define X86_EFLAGS_T 0x100UL
437 darwin_set_sstep (thread_t thread, int enable)
439 x86_thread_state_t regs;
440 unsigned int count = x86_THREAD_STATE_COUNT;
443 kret = thread_get_state (thread, x86_THREAD_STATE,
444 (thread_state_t) ®s, &count);
445 if (kret != KERN_SUCCESS)
447 printf_unfiltered (_("darwin_set_sstep: error %x, thread=%x\n"),
451 switch (regs.tsh.flavor)
453 case x86_THREAD_STATE32:
455 __uint32_t bit = enable ? X86_EFLAGS_T : 0;
457 if ((regs.uts.ts32.__eflags & X86_EFLAGS_T) == bit)
459 regs.uts.ts32.__eflags = (regs.uts.ts32.__eflags & ~X86_EFLAGS_T) | bit;
460 kret = thread_set_state (thread, x86_THREAD_STATE,
461 (thread_state_t) ®s, count);
462 MACH_CHECK_ERROR (kret);
465 case x86_THREAD_STATE64:
467 __uint64_t bit = enable ? X86_EFLAGS_T : 0;
469 if ((regs.uts.ts64.__rflags & X86_EFLAGS_T) == bit)
471 regs.uts.ts64.__rflags = (regs.uts.ts64.__rflags & ~X86_EFLAGS_T) | bit;
472 kret = thread_set_state (thread, x86_THREAD_STATE,
473 (thread_state_t) ®s, count);
474 MACH_CHECK_ERROR (kret);
478 error (_("darwin_set_sstep: unknown flavour: %d\n"), regs.tsh.flavor);
483 darwin_complete_target (struct target_ops *target)
485 amd64_native_gregset64_reg_offset = amd64_darwin_thread_state_reg_offset;
486 amd64_native_gregset64_num_regs = amd64_darwin_thread_state_num_regs;
487 amd64_native_gregset32_reg_offset = i386_darwin_thread_state_reg_offset;
488 amd64_native_gregset32_num_regs = i386_darwin_thread_state_num_regs;
490 target->to_fetch_registers = i386_darwin_fetch_inferior_registers;
491 target->to_store_registers = i386_darwin_store_inferior_registers;