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 "i387-tdep.h"
35 #include "arch-utils.h"
38 #include "darwin-nat.h"
39 #include "i386-darwin-tdep.h"
42 #include "amd64-nat.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 GP registers for thread 0x%ulx"), current_thread
);
72 MACH_CHECK_ERROR (ret
);
74 amd64_supply_native_gregset (regcache
, &gp_regs
.uts
, -1);
78 if (regno
== -1 || !amd64_native_gregset_supplies_p (gdbarch
, regno
))
80 x86_float_state_t fp_regs
;
81 unsigned int fp_count
= x86_FLOAT_STATE_COUNT
;
84 ret
= thread_get_state
85 (current_thread
, x86_FLOAT_STATE
, (thread_state_t
) & fp_regs
,
87 if (ret
!= KERN_SUCCESS
)
89 printf_unfiltered (_("Error calling thread_get_state for float registers for thread 0x%ulx"), current_thread
);
90 MACH_CHECK_ERROR (ret
);
92 i387_supply_fxsave (regcache
, -1, &fp_regs
.ufs
.fs64
);
99 if (regno
== -1 || regno
< I386_NUM_GREGS
)
101 i386_thread_state_t gp_regs
;
102 unsigned int gp_count
= i386_THREAD_STATE_COUNT
;
106 ret
= thread_get_state
107 (current_thread
, i386_THREAD_STATE
, (thread_state_t
) & gp_regs
,
109 if (ret
!= KERN_SUCCESS
)
111 printf_unfiltered (_("Error calling thread_get_state for GP registers for thread 0x%ulx"), current_thread
);
112 MACH_CHECK_ERROR (ret
);
114 for (i
= 0; i
< I386_NUM_GREGS
; i
++)
117 (char *)&gp_regs
+ i386_darwin_thread_state_reg_offset
[i
]);
123 || (regno
>= I386_ST0_REGNUM
&& regno
< I386_SSE_NUM_REGS
))
125 i386_float_state_t fp_regs
;
126 unsigned int fp_count
= i386_FLOAT_STATE_COUNT
;
129 ret
= thread_get_state
130 (current_thread
, i386_FLOAT_STATE
, (thread_state_t
) & fp_regs
,
132 if (ret
!= KERN_SUCCESS
)
134 printf_unfiltered (_("Error calling thread_get_state for float registers for thread 0x%ulx"), current_thread
);
135 MACH_CHECK_ERROR (ret
);
137 i387_supply_fxsave (regcache
, -1, &fp_regs
.__fpu_fcw
);
144 warning (_("unknown register %d"), regno
);
145 regcache_raw_supply (regcache
, regno
, NULL
);
149 /* Store our register values back into the inferior.
150 If REGNO is -1, do this for all registers.
151 Otherwise, REGNO specifies which register (so we can save time). */
154 i386_darwin_store_inferior_registers (struct target_ops
*ops
,
155 struct regcache
*regcache
, int regno
)
157 thread_t current_thread
= ptid_get_tid (inferior_ptid
);
158 struct gdbarch
*gdbarch
= get_regcache_arch (regcache
);
161 if (gdbarch_ptr_bit (gdbarch
) == 64)
163 if (regno
== -1 || amd64_native_gregset_supplies_p (gdbarch
, regno
))
165 x86_thread_state_t gp_regs
;
167 unsigned int gp_count
= x86_THREAD_STATE_COUNT
;
169 ret
= thread_get_state
170 (current_thread
, x86_THREAD_STATE
, (thread_state_t
) &gp_regs
,
172 MACH_CHECK_ERROR (ret
);
173 gdb_assert (gp_regs
.tsh
.flavor
== x86_THREAD_STATE64
);
174 gdb_assert (gp_regs
.tsh
.count
== x86_THREAD_STATE64_COUNT
);
176 amd64_collect_native_gregset (regcache
, &gp_regs
.uts
, regno
);
178 ret
= thread_set_state (current_thread
, x86_THREAD_STATE
,
179 (thread_state_t
) &gp_regs
,
180 x86_THREAD_STATE_COUNT
);
181 MACH_CHECK_ERROR (ret
);
184 if (regno
== -1 || !amd64_native_gregset_supplies_p (gdbarch
, regno
))
186 x86_float_state_t fp_regs
;
188 unsigned int fp_count
= x86_FLOAT_STATE_COUNT
;
190 ret
= thread_get_state
191 (current_thread
, x86_FLOAT_STATE
, (thread_state_t
) & fp_regs
,
193 MACH_CHECK_ERROR (ret
);
194 gdb_assert (fp_regs
.fsh
.flavor
== x86_FLOAT_STATE64
);
195 gdb_assert (fp_regs
.fsh
.count
== x86_FLOAT_STATE64_COUNT
);
197 i387_collect_fxsave (regcache
, regno
, &fp_regs
.ufs
.fs64
.__fpu_fcw
);
199 ret
= thread_set_state (current_thread
, x86_FLOAT_STATE
,
200 (thread_state_t
) & fp_regs
,
201 x86_FLOAT_STATE_COUNT
);
202 MACH_CHECK_ERROR (ret
);
208 if (regno
== -1 || regno
< I386_NUM_GREGS
)
210 i386_thread_state_t gp_regs
;
212 unsigned int gp_count
= i386_THREAD_STATE_COUNT
;
215 ret
= thread_get_state
216 (current_thread
, i386_THREAD_STATE
, (thread_state_t
) & gp_regs
,
218 MACH_CHECK_ERROR (ret
);
220 for (i
= 0; i
< I386_NUM_GREGS
; i
++)
221 if (regno
== -1 || regno
== i
)
224 (char *)&gp_regs
+ i386_darwin_thread_state_reg_offset
[i
]);
226 ret
= thread_set_state (current_thread
, i386_THREAD_STATE
,
227 (thread_state_t
) & gp_regs
,
228 i386_THREAD_STATE_COUNT
);
229 MACH_CHECK_ERROR (ret
);
233 || (regno
>= I386_ST0_REGNUM
&& regno
< I386_SSE_NUM_REGS
))
235 i386_float_state_t fp_regs
;
236 unsigned int fp_count
= i386_FLOAT_STATE_COUNT
;
239 ret
= thread_get_state
240 (current_thread
, i386_FLOAT_STATE
, (thread_state_t
) & fp_regs
,
242 MACH_CHECK_ERROR (ret
);
244 i387_collect_fxsave (regcache
, regno
, &fp_regs
.__fpu_fcw
);
246 ret
= thread_set_state (current_thread
, i386_FLOAT_STATE
,
247 (thread_state_t
) & fp_regs
,
248 i386_FLOAT_STATE_COUNT
);
249 MACH_CHECK_ERROR (ret
);
255 /* Support for debug registers, boosted mostly from i386-linux-nat.c. */
258 #define DR_FIRSTADDR 0
262 #define DR_LASTADDR 3
275 i386_darwin_dr_set (int regnum
, uint32_t value
)
278 thread_t current_thread
;
279 x86_debug_state_t dr_regs
;
281 unsigned int dr_count
= x86_DEBUG_STATE_COUNT
;
283 gdb_assert (regnum
>= 0 && regnum
<= DR_CONTROL
);
285 current_thread
= ptid_get_tid (inferior_ptid
);
287 dr_regs
.dsh
.flavor
= x86_DEBUG_STATE32
;
288 dr_regs
.dsh
.count
= x86_DEBUG_STATE32_COUNT
;
289 dr_count
= x86_DEBUG_STATE_COUNT
;
290 ret
= thread_get_state (current_thread
, x86_DEBUG_STATE
,
291 (thread_state_t
) &dr_regs
, &dr_count
);
293 if (ret
!= KERN_SUCCESS
)
295 printf_unfiltered (_("Error reading debug registers thread 0x%x via thread_get_state\n"), (int) current_thread
);
296 MACH_CHECK_ERROR (ret
);
302 dr_regs
.uds
.ds32
.__dr0
= value
;
305 dr_regs
.uds
.ds32
.__dr1
= value
;
308 dr_regs
.uds
.ds32
.__dr2
= value
;
311 dr_regs
.uds
.ds32
.__dr3
= value
;
314 dr_regs
.uds
.ds32
.__dr4
= value
;
317 dr_regs
.uds
.ds32
.__dr5
= value
;
320 dr_regs
.uds
.ds32
.__dr6
= value
;
323 dr_regs
.uds
.ds32
.__dr7
= value
;
327 ret
= thread_set_state (current_thread
, x86_DEBUG_STATE
,
328 (thread_state_t
) &dr_regs
, dr_count
);
330 if (ret
!= KERN_SUCCESS
)
332 printf_unfiltered (_("Error writing debug registers thread 0x%x via thread_get_state\n"), (int) current_thread
);
333 MACH_CHECK_ERROR (ret
);
338 i386_darwin_dr_get (int regnum
)
340 thread_t current_thread
;
341 x86_debug_state_t dr_regs
;
343 unsigned int dr_count
= x86_DEBUG_STATE_COUNT
;
345 gdb_assert (regnum
>= 0 && regnum
<= DR_CONTROL
);
347 current_thread
= ptid_get_tid (inferior_ptid
);
349 dr_regs
.dsh
.flavor
= x86_DEBUG_STATE32
;
350 dr_regs
.dsh
.count
= x86_DEBUG_STATE32_COUNT
;
351 dr_count
= x86_DEBUG_STATE_COUNT
;
352 ret
= thread_get_state (current_thread
, x86_DEBUG_STATE
,
353 (thread_state_t
) &dr_regs
, &dr_count
);
355 if (ret
!= KERN_SUCCESS
)
357 printf_unfiltered (_("Error reading debug registers thread 0x%x via thread_get_state\n"), (int) current_thread
);
358 MACH_CHECK_ERROR (ret
);
364 return dr_regs
.uds
.ds32
.__dr0
;
366 return dr_regs
.uds
.ds32
.__dr1
;
368 return dr_regs
.uds
.ds32
.__dr2
;
370 return dr_regs
.uds
.ds32
.__dr3
;
372 return dr_regs
.uds
.ds32
.__dr4
;
374 return dr_regs
.uds
.ds32
.__dr5
;
376 return dr_regs
.uds
.ds32
.__dr6
;
378 return dr_regs
.uds
.ds32
.__dr7
;
385 i386_darwin_dr_set_control (unsigned long control
)
387 i386_darwin_dr_set (DR_CONTROL
, control
);
391 i386_darwin_dr_set_addr (int regnum
, CORE_ADDR addr
)
393 gdb_assert (regnum
>= 0 && regnum
<= DR_LASTADDR
- DR_FIRSTADDR
);
395 i386_darwin_dr_set (DR_FIRSTADDR
+ regnum
, addr
);
399 i386_darwin_dr_reset_addr (int regnum
)
401 gdb_assert (regnum
>= 0 && regnum
<= DR_LASTADDR
- DR_FIRSTADDR
);
403 i386_darwin_dr_set (DR_FIRSTADDR
+ regnum
, 0L);
407 i386_darwin_dr_get_status (void)
409 return i386_darwin_dr_get (DR_STATUS
);
413 darwin_check_osabi (darwin_inferior
*inf
, thread_t thread
)
415 if (gdbarch_osabi (target_gdbarch
) == GDB_OSABI_UNKNOWN
)
417 /* Attaching to a process. Let's figure out what kind it is. */
418 x86_thread_state_t gp_regs
;
419 struct gdbarch_info info
;
420 unsigned int gp_count
= x86_THREAD_STATE_COUNT
;
423 ret
= thread_get_state (thread
, x86_THREAD_STATE
,
424 (thread_state_t
) &gp_regs
, &gp_count
);
425 if (ret
!= KERN_SUCCESS
)
427 MACH_CHECK_ERROR (ret
);
431 gdbarch_info_init (&info
);
432 gdbarch_info_fill (&info
);
433 info
.byte_order
= gdbarch_byte_order (target_gdbarch
);
434 info
.osabi
= GDB_OSABI_DARWIN
;
435 if (gp_regs
.tsh
.flavor
== x86_THREAD_STATE64
)
436 info
.bfd_arch_info
= bfd_lookup_arch (bfd_arch_i386
,
439 info
.bfd_arch_info
= bfd_lookup_arch (bfd_arch_i386
,
441 gdbarch_update_p (info
);
445 #define X86_EFLAGS_T 0x100UL
447 /* Returning from a signal trampoline is done by calling a
448 special system call (sigreturn). This system call
449 restores the registers that were saved when the signal was
450 raised, including %eflags/%rflags. That means that single-stepping
451 won't work. Instead, we'll have to modify the signal context
452 that's about to be restored, and set the trace flag there. */
455 i386_darwin_sstep_at_sigreturn (x86_thread_state_t
*regs
)
457 enum bfd_endian byte_order
= gdbarch_byte_order (target_gdbarch
);
458 static const gdb_byte darwin_syscall
[] = { 0xcd, 0x80 }; /* int 0x80 */
459 gdb_byte buf
[sizeof (darwin_syscall
)];
461 /* Check if PC is at a sigreturn system call. */
462 if (target_read_memory (regs
->uts
.ts32
.__eip
, buf
, sizeof (buf
)) == 0
463 && memcmp (buf
, darwin_syscall
, sizeof (darwin_syscall
)) == 0
464 && regs
->uts
.ts32
.__eax
== 0xb8 /* SYS_sigreturn */)
471 uctx_addr
= read_memory_unsigned_integer
472 (regs
->uts
.ts32
.__esp
+ 4, 4, byte_order
);
473 mctx_addr
= read_memory_unsigned_integer
474 (uctx_addr
+ 28, 4, byte_order
);
476 flags_addr
= mctx_addr
+ 12 + 9 * 4;
477 read_memory (flags_addr
, (gdb_byte
*) &eflags
, 4);
478 eflags
|= X86_EFLAGS_T
;
479 write_memory (flags_addr
, (gdb_byte
*) &eflags
, 4);
488 amd64_darwin_sstep_at_sigreturn (x86_thread_state_t
*regs
)
490 enum bfd_endian byte_order
= gdbarch_byte_order (target_gdbarch
);
491 static const gdb_byte darwin_syscall
[] = { 0x0f, 0x05 }; /* syscall */
492 gdb_byte buf
[sizeof (darwin_syscall
)];
494 /* Check if PC is at a sigreturn system call. */
495 if (target_read_memory (regs
->uts
.ts64
.__rip
, buf
, sizeof (buf
)) == 0
496 && memcmp (buf
, darwin_syscall
, sizeof (darwin_syscall
)) == 0
497 && (regs
->uts
.ts64
.__rax
& 0xffffffff) == 0x20000b8 /* SYS_sigreturn */)
503 mctx_addr
= read_memory_unsigned_integer
504 (regs
->uts
.ts64
.__rdi
+ 48, 8, byte_order
);
505 flags_addr
= mctx_addr
+ 16 + 17 * 8;
507 /* AMD64 is little endian. */
508 read_memory (flags_addr
, (gdb_byte
*) &rflags
, 4);
509 rflags
|= X86_EFLAGS_T
;
510 write_memory (flags_addr
, (gdb_byte
*) &rflags
, 4);
519 darwin_set_sstep (thread_t thread
, int enable
)
521 x86_thread_state_t regs
;
522 unsigned int count
= x86_THREAD_STATE_COUNT
;
525 kret
= thread_get_state (thread
, x86_THREAD_STATE
,
526 (thread_state_t
) ®s
, &count
);
527 if (kret
!= KERN_SUCCESS
)
529 printf_unfiltered (_("darwin_set_sstep: error %x, thread=%x\n"),
534 switch (regs
.tsh
.flavor
)
536 case x86_THREAD_STATE32
:
538 __uint32_t bit
= enable
? X86_EFLAGS_T
: 0;
540 if (enable
&& i386_darwin_sstep_at_sigreturn (®s
))
542 if ((regs
.uts
.ts32
.__eflags
& X86_EFLAGS_T
) == bit
)
544 regs
.uts
.ts32
.__eflags
= (regs
.uts
.ts32
.__eflags
& ~X86_EFLAGS_T
) | bit
;
545 kret
= thread_set_state (thread
, x86_THREAD_STATE
,
546 (thread_state_t
) ®s
, count
);
547 MACH_CHECK_ERROR (kret
);
551 case x86_THREAD_STATE64
:
553 __uint64_t bit
= enable
? X86_EFLAGS_T
: 0;
555 if (enable
&& amd64_darwin_sstep_at_sigreturn (®s
))
557 if ((regs
.uts
.ts64
.__rflags
& X86_EFLAGS_T
) == bit
)
559 regs
.uts
.ts64
.__rflags
= (regs
.uts
.ts64
.__rflags
& ~X86_EFLAGS_T
) | bit
;
560 kret
= thread_set_state (thread
, x86_THREAD_STATE
,
561 (thread_state_t
) ®s
, count
);
562 MACH_CHECK_ERROR (kret
);
567 error (_("darwin_set_sstep: unknown flavour: %d\n"), regs
.tsh
.flavor
);
572 darwin_complete_target (struct target_ops
*target
)
575 amd64_native_gregset64_reg_offset
= amd64_darwin_thread_state_reg_offset
;
576 amd64_native_gregset64_num_regs
= amd64_darwin_thread_state_num_regs
;
577 amd64_native_gregset32_reg_offset
= i386_darwin_thread_state_reg_offset
;
578 amd64_native_gregset32_num_regs
= i386_darwin_thread_state_num_regs
;
581 target
->to_fetch_registers
= i386_darwin_fetch_inferior_registers
;
582 target
->to_store_registers
= i386_darwin_store_inferior_registers
;