1 /* Darwin support for GDB, the GNU debugger.
2 Copyright 1997, 1998, 1999, 2000, 2001, 2002, 2008, 2009
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"
39 #include "darwin-nat.h"
40 #include "i386-darwin-tdep.h"
42 /* Read register values from the inferior process.
43 If REGNO is -1, do this for all registers.
44 Otherwise, REGNO specifies which register (so we can save time). */
46 i386_darwin_fetch_inferior_registers (struct target_ops
*ops
,
47 struct regcache
*regcache
, int regno
)
49 thread_t current_thread
= ptid_get_tid (inferior_ptid
);
51 struct gdbarch
*gdbarch
= get_regcache_arch (regcache
);
53 if (gdbarch_ptr_bit (gdbarch
) == 64)
55 if (regno
== -1 || amd64_native_gregset_supplies_p (gdbarch
, regno
))
57 x86_thread_state_t gp_regs
;
58 unsigned int gp_count
= x86_THREAD_STATE_COUNT
;
61 ret
= thread_get_state
62 (current_thread
, x86_THREAD_STATE
, (thread_state_t
) & gp_regs
,
64 if (ret
!= KERN_SUCCESS
)
66 printf_unfiltered (_("Error calling thread_get_state for GP registers for thread 0x%ulx"), current_thread
);
67 MACH_CHECK_ERROR (ret
);
69 amd64_supply_native_gregset (regcache
, &gp_regs
.uts
, -1);
73 if (regno
== -1 || !amd64_native_gregset_supplies_p (gdbarch
, regno
))
75 x86_float_state_t fp_regs
;
76 unsigned int fp_count
= x86_FLOAT_STATE_COUNT
;
79 ret
= thread_get_state
80 (current_thread
, x86_FLOAT_STATE
, (thread_state_t
) & fp_regs
,
82 if (ret
!= KERN_SUCCESS
)
84 printf_unfiltered (_("Error calling thread_get_state for float registers for thread 0x%ulx"), current_thread
);
85 MACH_CHECK_ERROR (ret
);
87 i387_supply_fxsave (regcache
, -1, &fp_regs
.ufs
.fs64
);
93 if (regno
== -1 || regno
< I386_NUM_GREGS
)
95 i386_thread_state_t gp_regs
;
96 unsigned int gp_count
= i386_THREAD_STATE_COUNT
;
100 ret
= thread_get_state
101 (current_thread
, i386_THREAD_STATE
, (thread_state_t
) & gp_regs
,
103 if (ret
!= KERN_SUCCESS
)
105 printf_unfiltered (_("Error calling thread_get_state for GP registers for thread 0x%ulx"), current_thread
);
106 MACH_CHECK_ERROR (ret
);
108 for (i
= 0; i
< I386_NUM_GREGS
; i
++)
111 (char *)&gp_regs
+ i386_darwin_thread_state_reg_offset
[i
]);
117 || (regno
>= I386_ST0_REGNUM
&& regno
< I386_SSE_NUM_REGS
))
119 i386_float_state_t fp_regs
;
120 unsigned int fp_count
= i386_FLOAT_STATE_COUNT
;
123 ret
= thread_get_state
124 (current_thread
, i386_FLOAT_STATE
, (thread_state_t
) & fp_regs
,
126 if (ret
!= KERN_SUCCESS
)
128 printf_unfiltered (_("Error calling thread_get_state for float registers for thread 0x%ulx"), current_thread
);
129 MACH_CHECK_ERROR (ret
);
131 i387_supply_fxsave (regcache
, -1, &fp_regs
.__fpu_fcw
);
138 warning (_("unknown register %d"), regno
);
139 regcache_raw_supply (regcache
, regno
, NULL
);
143 /* Store our register values back into the inferior.
144 If REGNO is -1, do this for all registers.
145 Otherwise, REGNO specifies which register (so we can save time). */
148 i386_darwin_store_inferior_registers (struct target_ops
*ops
,
149 struct regcache
*regcache
, int regno
)
151 thread_t current_thread
= ptid_get_tid (inferior_ptid
);
152 struct gdbarch
*gdbarch
= get_regcache_arch (regcache
);
154 if (gdbarch_ptr_bit (gdbarch
) == 64)
156 if (regno
== -1 || amd64_native_gregset_supplies_p (gdbarch
, regno
))
158 x86_thread_state_t gp_regs
;
160 unsigned int gp_count
= x86_THREAD_STATE_COUNT
;
162 ret
= thread_get_state
163 (current_thread
, x86_THREAD_STATE
, (thread_state_t
) &gp_regs
,
165 MACH_CHECK_ERROR (ret
);
166 gdb_assert (gp_regs
.tsh
.flavor
== x86_THREAD_STATE64
);
167 gdb_assert (gp_regs
.tsh
.count
== x86_THREAD_STATE64_COUNT
);
169 amd64_collect_native_gregset (regcache
, &gp_regs
.uts
, regno
);
171 ret
= thread_set_state (current_thread
, x86_THREAD_STATE
,
172 (thread_state_t
) &gp_regs
,
173 x86_THREAD_STATE_COUNT
);
174 MACH_CHECK_ERROR (ret
);
177 if (regno
== -1 || !amd64_native_gregset_supplies_p (gdbarch
, regno
))
179 x86_float_state_t fp_regs
;
181 unsigned int fp_count
= x86_FLOAT_STATE_COUNT
;
183 ret
= thread_get_state
184 (current_thread
, x86_FLOAT_STATE
, (thread_state_t
) & fp_regs
,
186 MACH_CHECK_ERROR (ret
);
187 gdb_assert (fp_regs
.fsh
.flavor
== x86_FLOAT_STATE64
);
188 gdb_assert (fp_regs
.fsh
.count
== x86_FLOAT_STATE64_COUNT
);
190 i387_collect_fxsave (regcache
, regno
, &fp_regs
.ufs
.fs64
.__fpu_fcw
);
192 ret
= thread_set_state (current_thread
, x86_FLOAT_STATE
,
193 (thread_state_t
) & fp_regs
,
194 x86_FLOAT_STATE_COUNT
);
195 MACH_CHECK_ERROR (ret
);
200 if (regno
== -1 || regno
< I386_NUM_GREGS
)
202 i386_thread_state_t gp_regs
;
204 unsigned int gp_count
= i386_THREAD_STATE_COUNT
;
207 ret
= thread_get_state
208 (current_thread
, i386_THREAD_STATE
, (thread_state_t
) & gp_regs
,
210 MACH_CHECK_ERROR (ret
);
212 for (i
= 0; i
< I386_NUM_GREGS
; i
++)
213 if (regno
== -1 || regno
== i
)
216 (char *)&gp_regs
+ i386_darwin_thread_state_reg_offset
[i
]);
218 ret
= thread_set_state (current_thread
, i386_THREAD_STATE
,
219 (thread_state_t
) & gp_regs
,
220 i386_THREAD_STATE_COUNT
);
221 MACH_CHECK_ERROR (ret
);
225 || (regno
>= I386_ST0_REGNUM
&& regno
< I386_SSE_NUM_REGS
))
227 i386_float_state_t fp_regs
;
228 unsigned int fp_count
= i386_FLOAT_STATE_COUNT
;
231 ret
= thread_get_state
232 (current_thread
, i386_FLOAT_STATE
, (thread_state_t
) & fp_regs
,
234 MACH_CHECK_ERROR (ret
);
236 i387_collect_fxsave (regcache
, regno
, &fp_regs
.__fpu_fcw
);
238 ret
= thread_set_state (current_thread
, i386_FLOAT_STATE
,
239 (thread_state_t
) & fp_regs
,
240 i386_FLOAT_STATE_COUNT
);
241 MACH_CHECK_ERROR (ret
);
247 /* Support for debug registers, boosted mostly from i386-linux-nat.c. */
250 #define DR_FIRSTADDR 0
254 #define DR_LASTADDR 3
267 i386_darwin_dr_set (int regnum
, uint32_t value
)
270 thread_t current_thread
;
271 x86_debug_state_t dr_regs
;
273 unsigned int dr_count
= x86_DEBUG_STATE_COUNT
;
275 gdb_assert (regnum
>= 0 && regnum
<= DR_CONTROL
);
277 current_thread
= ptid_get_tid (inferior_ptid
);
279 dr_regs
.dsh
.flavor
= x86_DEBUG_STATE32
;
280 dr_regs
.dsh
.count
= x86_DEBUG_STATE32_COUNT
;
281 dr_count
= x86_DEBUG_STATE_COUNT
;
282 ret
= thread_get_state (current_thread
, x86_DEBUG_STATE
,
283 (thread_state_t
) &dr_regs
, &dr_count
);
285 if (ret
!= KERN_SUCCESS
)
287 printf_unfiltered (_("Error reading debug registers thread 0x%x via thread_get_state\n"), (int) current_thread
);
288 MACH_CHECK_ERROR (ret
);
294 dr_regs
.uds
.ds32
.__dr0
= value
;
297 dr_regs
.uds
.ds32
.__dr1
= value
;
300 dr_regs
.uds
.ds32
.__dr2
= value
;
303 dr_regs
.uds
.ds32
.__dr3
= value
;
306 dr_regs
.uds
.ds32
.__dr4
= value
;
309 dr_regs
.uds
.ds32
.__dr5
= value
;
312 dr_regs
.uds
.ds32
.__dr6
= value
;
315 dr_regs
.uds
.ds32
.__dr7
= value
;
319 ret
= thread_set_state (current_thread
, x86_DEBUG_STATE
,
320 (thread_state_t
) &dr_regs
, dr_count
);
322 if (ret
!= KERN_SUCCESS
)
324 printf_unfiltered (_("Error writing debug registers thread 0x%x via thread_get_state\n"), (int) current_thread
);
325 MACH_CHECK_ERROR (ret
);
330 i386_darwin_dr_get (int regnum
)
332 thread_t current_thread
;
333 x86_debug_state_t dr_regs
;
335 unsigned int dr_count
= x86_DEBUG_STATE_COUNT
;
337 gdb_assert (regnum
>= 0 && regnum
<= DR_CONTROL
);
339 current_thread
= ptid_get_tid (inferior_ptid
);
341 dr_regs
.dsh
.flavor
= x86_DEBUG_STATE32
;
342 dr_regs
.dsh
.count
= x86_DEBUG_STATE32_COUNT
;
343 dr_count
= x86_DEBUG_STATE_COUNT
;
344 ret
= thread_get_state (current_thread
, x86_DEBUG_STATE
,
345 (thread_state_t
) &dr_regs
, &dr_count
);
347 if (ret
!= KERN_SUCCESS
)
349 printf_unfiltered (_("Error reading debug registers thread 0x%x via thread_get_state\n"), (int) current_thread
);
350 MACH_CHECK_ERROR (ret
);
356 return dr_regs
.uds
.ds32
.__dr0
;
358 return dr_regs
.uds
.ds32
.__dr1
;
360 return dr_regs
.uds
.ds32
.__dr2
;
362 return dr_regs
.uds
.ds32
.__dr3
;
364 return dr_regs
.uds
.ds32
.__dr4
;
366 return dr_regs
.uds
.ds32
.__dr5
;
368 return dr_regs
.uds
.ds32
.__dr6
;
370 return dr_regs
.uds
.ds32
.__dr7
;
377 i386_darwin_dr_set_control (unsigned long control
)
379 i386_darwin_dr_set (DR_CONTROL
, control
);
383 i386_darwin_dr_set_addr (int regnum
, CORE_ADDR addr
)
385 gdb_assert (regnum
>= 0 && regnum
<= DR_LASTADDR
- DR_FIRSTADDR
);
387 i386_darwin_dr_set (DR_FIRSTADDR
+ regnum
, addr
);
391 i386_darwin_dr_reset_addr (int regnum
)
393 gdb_assert (regnum
>= 0 && regnum
<= DR_LASTADDR
- DR_FIRSTADDR
);
395 i386_darwin_dr_set (DR_FIRSTADDR
+ regnum
, 0L);
399 i386_darwin_dr_get_status (void)
401 return i386_darwin_dr_get (DR_STATUS
);
405 darwin_check_osabi (darwin_inferior
*inf
, thread_t thread
)
407 if (gdbarch_osabi (current_gdbarch
) == GDB_OSABI_UNKNOWN
)
409 /* Attaching to a process. Let's figure out what kind it is. */
410 x86_thread_state_t gp_regs
;
411 struct gdbarch_info info
;
412 unsigned int gp_count
= x86_THREAD_STATE_COUNT
;
415 ret
= thread_get_state (thread
, x86_THREAD_STATE
,
416 (thread_state_t
) &gp_regs
, &gp_count
);
417 if (ret
!= KERN_SUCCESS
)
419 MACH_CHECK_ERROR (ret
);
423 gdbarch_info_init (&info
);
424 gdbarch_info_fill (&info
);
425 info
.byte_order
= gdbarch_byte_order (current_gdbarch
);
426 info
.osabi
= GDB_OSABI_DARWIN
;
427 if (gp_regs
.tsh
.flavor
== x86_THREAD_STATE64
)
428 info
.bfd_arch_info
= bfd_lookup_arch (bfd_arch_i386
,
431 info
.bfd_arch_info
= bfd_lookup_arch (bfd_arch_i386
,
433 gdbarch_update_p (info
);
437 #define X86_EFLAGS_T 0x100UL
439 /* Returning from a signal trampoline is done by calling a
440 special system call (sigreturn). This system call
441 restores the registers that were saved when the signal was
442 raised, including %eflags/%rflags. That means that single-stepping
443 won't work. Instead, we'll have to modify the signal context
444 that's about to be restored, and set the trace flag there. */
447 i386_darwin_sstep_at_sigreturn (x86_thread_state_t
*regs
)
449 static const gdb_byte darwin_syscall
[] = { 0xcd, 0x80 }; /* int 0x80 */
450 gdb_byte buf
[sizeof (darwin_syscall
)];
452 /* Check if PC is at a sigreturn system call. */
453 if (target_read_memory (regs
->uts
.ts32
.__eip
, buf
, sizeof (buf
)) == 0
454 && memcmp (buf
, darwin_syscall
, sizeof (darwin_syscall
)) == 0
455 && regs
->uts
.ts32
.__eax
== 0xb8 /* SYS_sigreturn */)
462 uctx_addr
= read_memory_unsigned_integer (regs
->uts
.ts32
.__esp
+ 4, 4);
463 mctx_addr
= read_memory_unsigned_integer (uctx_addr
+ 28, 4);
465 flags_addr
= mctx_addr
+ 12 + 9 * 4;
466 read_memory (flags_addr
, (gdb_byte
*) &eflags
, 4);
467 eflags
|= X86_EFLAGS_T
;
468 write_memory (flags_addr
, (gdb_byte
*) &eflags
, 4);
476 amd64_darwin_sstep_at_sigreturn (x86_thread_state_t
*regs
)
478 static const gdb_byte darwin_syscall
[] = { 0x0f, 0x05 }; /* syscall */
479 gdb_byte buf
[sizeof (darwin_syscall
)];
481 /* Check if PC is at a sigreturn system call. */
482 if (target_read_memory (regs
->uts
.ts64
.__rip
, buf
, sizeof (buf
)) == 0
483 && memcmp (buf
, darwin_syscall
, sizeof (darwin_syscall
)) == 0
484 && (regs
->uts
.ts64
.__rax
& 0xffffffff) == 0x20000b8 /* SYS_sigreturn */)
490 mctx_addr
= read_memory_unsigned_integer (regs
->uts
.ts64
.__rdi
+ 48, 8);
491 flags_addr
= mctx_addr
+ 16 + 17 * 8;
493 /* AMD64 is little endian. */
494 read_memory (flags_addr
, (gdb_byte
*) &rflags
, 4);
495 rflags
|= X86_EFLAGS_T
;
496 write_memory (flags_addr
, (gdb_byte
*) &rflags
, 4);
504 darwin_set_sstep (thread_t thread
, int enable
)
506 x86_thread_state_t regs
;
507 unsigned int count
= x86_THREAD_STATE_COUNT
;
510 kret
= thread_get_state (thread
, x86_THREAD_STATE
,
511 (thread_state_t
) ®s
, &count
);
512 if (kret
!= KERN_SUCCESS
)
514 printf_unfiltered (_("darwin_set_sstep: error %x, thread=%x\n"),
519 switch (regs
.tsh
.flavor
)
521 case x86_THREAD_STATE32
:
523 __uint32_t bit
= enable
? X86_EFLAGS_T
: 0;
525 if (enable
&& i386_darwin_sstep_at_sigreturn (®s
))
527 if ((regs
.uts
.ts32
.__eflags
& X86_EFLAGS_T
) == bit
)
529 regs
.uts
.ts32
.__eflags
= (regs
.uts
.ts32
.__eflags
& ~X86_EFLAGS_T
) | bit
;
530 kret
= thread_set_state (thread
, x86_THREAD_STATE
,
531 (thread_state_t
) ®s
, count
);
532 MACH_CHECK_ERROR (kret
);
535 case x86_THREAD_STATE64
:
537 __uint64_t bit
= enable
? X86_EFLAGS_T
: 0;
539 if (enable
&& amd64_darwin_sstep_at_sigreturn (®s
))
541 if ((regs
.uts
.ts64
.__rflags
& X86_EFLAGS_T
) == bit
)
543 regs
.uts
.ts64
.__rflags
= (regs
.uts
.ts64
.__rflags
& ~X86_EFLAGS_T
) | bit
;
544 kret
= thread_set_state (thread
, x86_THREAD_STATE
,
545 (thread_state_t
) ®s
, count
);
546 MACH_CHECK_ERROR (kret
);
550 error (_("darwin_set_sstep: unknown flavour: %d\n"), regs
.tsh
.flavor
);
555 darwin_complete_target (struct target_ops
*target
)
557 amd64_native_gregset64_reg_offset
= amd64_darwin_thread_state_reg_offset
;
558 amd64_native_gregset64_num_regs
= amd64_darwin_thread_state_num_regs
;
559 amd64_native_gregset32_reg_offset
= i386_darwin_thread_state_reg_offset
;
560 amd64_native_gregset32_num_regs
= i386_darwin_thread_state_num_regs
;
562 target
->to_fetch_registers
= i386_darwin_fetch_inferior_registers
;
563 target
->to_store_registers
= i386_darwin_store_inferior_registers
;