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"
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 regcache
*regcache
, int regno
)
48 thread_t current_thread
= ptid_get_tid (inferior_ptid
);
50 struct gdbarch
*gdbarch
= get_regcache_arch (regcache
);
52 if (gdbarch_ptr_bit (gdbarch
) == 64)
54 if (regno
== -1 || amd64_native_gregset_supplies_p (gdbarch
, regno
))
56 x86_thread_state_t gp_regs
;
57 unsigned int gp_count
= x86_THREAD_STATE_COUNT
;
60 ret
= thread_get_state
61 (current_thread
, x86_THREAD_STATE
, (thread_state_t
) & gp_regs
,
63 if (ret
!= KERN_SUCCESS
)
65 printf_unfiltered (_("Error calling thread_get_state for GP registers for thread 0x%ulx"), current_thread
);
66 MACH_CHECK_ERROR (ret
);
68 amd64_supply_native_gregset (regcache
, &gp_regs
.uts
, -1);
72 if (regno
== -1 || !amd64_native_gregset_supplies_p (gdbarch
, regno
))
74 x86_float_state_t fp_regs
;
75 unsigned int fp_count
= x86_FLOAT_STATE_COUNT
;
78 ret
= thread_get_state
79 (current_thread
, x86_FLOAT_STATE
, (thread_state_t
) & fp_regs
,
81 if (ret
!= KERN_SUCCESS
)
83 printf_unfiltered (_("Error calling thread_get_state for float registers for thread 0x%ulx"), current_thread
);
84 MACH_CHECK_ERROR (ret
);
86 i387_supply_fxsave (regcache
, -1, &fp_regs
.ufs
.fs64
);
92 if (regno
== -1 || regno
< I386_NUM_GREGS
)
94 i386_thread_state_t gp_regs
;
95 unsigned int gp_count
= i386_THREAD_STATE_COUNT
;
99 ret
= thread_get_state
100 (current_thread
, i386_THREAD_STATE
, (thread_state_t
) & gp_regs
,
102 if (ret
!= KERN_SUCCESS
)
104 printf_unfiltered (_("Error calling thread_get_state for GP registers for thread 0x%ulx"), current_thread
);
105 MACH_CHECK_ERROR (ret
);
107 for (i
= 0; i
< I386_NUM_GREGS
; i
++)
110 (char *)&gp_regs
+ i386_darwin_thread_state_reg_offset
[i
]);
116 || (regno
>= I386_ST0_REGNUM
&& regno
< I386_SSE_NUM_REGS
))
118 i386_float_state_t fp_regs
;
119 unsigned int fp_count
= i386_FLOAT_STATE_COUNT
;
122 ret
= thread_get_state
123 (current_thread
, i386_FLOAT_STATE
, (thread_state_t
) & fp_regs
,
125 if (ret
!= KERN_SUCCESS
)
127 printf_unfiltered (_("Error calling thread_get_state for float registers for thread 0x%ulx"), current_thread
);
128 MACH_CHECK_ERROR (ret
);
130 i387_supply_fxsave (regcache
, -1, &fp_regs
.__fpu_fcw
);
137 warning (_("unknown register %d"), regno
);
138 regcache_raw_supply (regcache
, regno
, NULL
);
142 /* Store our register values back into the inferior.
143 If REGNO is -1, do this for all registers.
144 Otherwise, REGNO specifies which register (so we can save time). */
147 i386_darwin_store_inferior_registers (struct regcache
*regcache
, int regno
)
149 thread_t current_thread
= ptid_get_tid (inferior_ptid
);
150 struct gdbarch
*gdbarch
= get_regcache_arch (regcache
);
152 if (gdbarch_ptr_bit (gdbarch
) == 64)
154 if (regno
== -1 || amd64_native_gregset_supplies_p (gdbarch
, regno
))
156 x86_thread_state_t gp_regs
;
158 unsigned int gp_count
= x86_THREAD_STATE_COUNT
;
160 ret
= thread_get_state
161 (current_thread
, x86_THREAD_STATE
, (thread_state_t
) &gp_regs
,
163 MACH_CHECK_ERROR (ret
);
164 gdb_assert (gp_regs
.tsh
.flavor
== x86_THREAD_STATE64
);
165 gdb_assert (gp_regs
.tsh
.count
== x86_THREAD_STATE64_COUNT
);
167 amd64_collect_native_gregset (regcache
, &gp_regs
.uts
, regno
);
169 ret
= thread_set_state (current_thread
, x86_THREAD_STATE
,
170 (thread_state_t
) &gp_regs
,
171 x86_THREAD_STATE_COUNT
);
172 MACH_CHECK_ERROR (ret
);
175 if (regno
== -1 || !amd64_native_gregset_supplies_p (gdbarch
, regno
))
177 x86_float_state_t fp_regs
;
179 unsigned int fp_count
= x86_FLOAT_STATE_COUNT
;
181 ret
= thread_get_state
182 (current_thread
, x86_FLOAT_STATE
, (thread_state_t
) & fp_regs
,
184 MACH_CHECK_ERROR (ret
);
185 gdb_assert (fp_regs
.fsh
.flavor
== x86_FLOAT_STATE64
);
186 gdb_assert (fp_regs
.fsh
.count
== x86_FLOAT_STATE64_COUNT
);
188 i387_collect_fxsave (regcache
, regno
, &fp_regs
.ufs
.fs64
.__fpu_fcw
);
190 ret
= thread_set_state (current_thread
, x86_FLOAT_STATE
,
191 (thread_state_t
) & fp_regs
,
192 x86_FLOAT_STATE_COUNT
);
193 MACH_CHECK_ERROR (ret
);
198 if (regno
== -1 || regno
< I386_NUM_GREGS
)
200 i386_thread_state_t gp_regs
;
202 unsigned int gp_count
= i386_THREAD_STATE_COUNT
;
205 ret
= thread_get_state
206 (current_thread
, i386_THREAD_STATE
, (thread_state_t
) & gp_regs
,
208 MACH_CHECK_ERROR (ret
);
210 for (i
= 0; i
< I386_NUM_GREGS
; i
++)
211 if (regno
== -1 || regno
== i
)
214 (char *)&gp_regs
+ i386_darwin_thread_state_reg_offset
[i
]);
216 ret
= thread_set_state (current_thread
, i386_THREAD_STATE
,
217 (thread_state_t
) & gp_regs
,
218 i386_THREAD_STATE_COUNT
);
219 MACH_CHECK_ERROR (ret
);
223 || (regno
>= I386_ST0_REGNUM
&& regno
< I386_SSE_NUM_REGS
))
225 i386_float_state_t fp_regs
;
226 unsigned int fp_count
= i386_FLOAT_STATE_COUNT
;
229 ret
= thread_get_state
230 (current_thread
, i386_FLOAT_STATE
, (thread_state_t
) & fp_regs
,
232 MACH_CHECK_ERROR (ret
);
234 i387_collect_fxsave (regcache
, regno
, &fp_regs
.__fpu_fcw
);
236 ret
= thread_set_state (current_thread
, i386_FLOAT_STATE
,
237 (thread_state_t
) & fp_regs
,
238 i386_FLOAT_STATE_COUNT
);
239 MACH_CHECK_ERROR (ret
);
245 /* Support for debug registers, boosted mostly from i386-linux-nat.c. */
248 #define DR_FIRSTADDR 0
252 #define DR_LASTADDR 3
265 i386_darwin_dr_set (int regnum
, uint32_t value
)
268 thread_t current_thread
;
269 x86_debug_state_t dr_regs
;
271 unsigned int dr_count
= x86_DEBUG_STATE_COUNT
;
273 gdb_assert (regnum
>= 0 && regnum
<= DR_CONTROL
);
275 current_thread
= ptid_get_tid (inferior_ptid
);
277 dr_regs
.dsh
.flavor
= x86_DEBUG_STATE32
;
278 dr_regs
.dsh
.count
= x86_DEBUG_STATE32_COUNT
;
279 dr_count
= x86_DEBUG_STATE_COUNT
;
280 ret
= thread_get_state (current_thread
, x86_DEBUG_STATE
,
281 (thread_state_t
) &dr_regs
, &dr_count
);
283 if (ret
!= KERN_SUCCESS
)
285 printf_unfiltered (_("Error reading debug registers thread 0x%x via thread_get_state\n"), (int) current_thread
);
286 MACH_CHECK_ERROR (ret
);
292 dr_regs
.uds
.ds32
.__dr0
= value
;
295 dr_regs
.uds
.ds32
.__dr1
= value
;
298 dr_regs
.uds
.ds32
.__dr2
= value
;
301 dr_regs
.uds
.ds32
.__dr3
= value
;
304 dr_regs
.uds
.ds32
.__dr4
= value
;
307 dr_regs
.uds
.ds32
.__dr5
= value
;
310 dr_regs
.uds
.ds32
.__dr6
= value
;
313 dr_regs
.uds
.ds32
.__dr7
= value
;
317 ret
= thread_set_state (current_thread
, x86_DEBUG_STATE
,
318 (thread_state_t
) &dr_regs
, dr_count
);
320 if (ret
!= KERN_SUCCESS
)
322 printf_unfiltered (_("Error writing debug registers thread 0x%x via thread_get_state\n"), (int) current_thread
);
323 MACH_CHECK_ERROR (ret
);
328 i386_darwin_dr_get (int regnum
)
330 thread_t current_thread
;
331 x86_debug_state_t dr_regs
;
333 unsigned int dr_count
= x86_DEBUG_STATE_COUNT
;
335 gdb_assert (regnum
>= 0 && regnum
<= DR_CONTROL
);
337 current_thread
= ptid_get_tid (inferior_ptid
);
339 dr_regs
.dsh
.flavor
= x86_DEBUG_STATE32
;
340 dr_regs
.dsh
.count
= x86_DEBUG_STATE32_COUNT
;
341 dr_count
= x86_DEBUG_STATE_COUNT
;
342 ret
= thread_get_state (current_thread
, x86_DEBUG_STATE
,
343 (thread_state_t
) &dr_regs
, &dr_count
);
345 if (ret
!= KERN_SUCCESS
)
347 printf_unfiltered (_("Error reading debug registers thread 0x%x via thread_get_state\n"), (int) current_thread
);
348 MACH_CHECK_ERROR (ret
);
354 return dr_regs
.uds
.ds32
.__dr0
;
356 return dr_regs
.uds
.ds32
.__dr1
;
358 return dr_regs
.uds
.ds32
.__dr2
;
360 return dr_regs
.uds
.ds32
.__dr3
;
362 return dr_regs
.uds
.ds32
.__dr4
;
364 return dr_regs
.uds
.ds32
.__dr5
;
366 return dr_regs
.uds
.ds32
.__dr6
;
368 return dr_regs
.uds
.ds32
.__dr7
;
375 i386_darwin_dr_set_control (unsigned long control
)
377 i386_darwin_dr_set (DR_CONTROL
, control
);
381 i386_darwin_dr_set_addr (int regnum
, CORE_ADDR addr
)
383 gdb_assert (regnum
>= 0 && regnum
<= DR_LASTADDR
- DR_FIRSTADDR
);
385 i386_darwin_dr_set (DR_FIRSTADDR
+ regnum
, addr
);
389 i386_darwin_dr_reset_addr (int regnum
)
391 gdb_assert (regnum
>= 0 && regnum
<= DR_LASTADDR
- DR_FIRSTADDR
);
393 i386_darwin_dr_set (DR_FIRSTADDR
+ regnum
, 0L);
397 i386_darwin_dr_get_status (void)
399 return i386_darwin_dr_get (DR_STATUS
);
403 darwin_check_osabi (darwin_inferior
*inf
, thread_t thread
)
405 if (gdbarch_osabi (current_gdbarch
) == GDB_OSABI_UNKNOWN
)
407 /* Attaching to a process. Let's figure out what kind it is. */
408 x86_thread_state_t gp_regs
;
409 struct gdbarch_info info
;
410 unsigned int gp_count
= x86_THREAD_STATE_COUNT
;
413 ret
= thread_get_state (thread
, x86_THREAD_STATE
,
414 (thread_state_t
) &gp_regs
, &gp_count
);
415 if (ret
!= KERN_SUCCESS
)
417 MACH_CHECK_ERROR (ret
);
421 gdbarch_info_init (&info
);
422 gdbarch_info_fill (&info
);
423 info
.byte_order
= gdbarch_byte_order (current_gdbarch
);
424 info
.osabi
= GDB_OSABI_DARWIN
;
425 if (gp_regs
.tsh
.flavor
== x86_THREAD_STATE64
)
426 info
.bfd_arch_info
= bfd_lookup_arch (bfd_arch_i386
,
429 info
.bfd_arch_info
= bfd_lookup_arch (bfd_arch_i386
,
431 gdbarch_update_p (info
);
435 #define X86_EFLAGS_T 0x100UL
437 /* Returning from a signal trampoline is done by calling a
438 special system call (sigreturn). This system call
439 restores the registers that were saved when the signal was
440 raised, including %eflags/%rflags. That means that single-stepping
441 won't work. Instead, we'll have to modify the signal context
442 that's about to be restored, and set the trace flag there. */
445 i386_darwin_sstep_at_sigreturn (x86_thread_state_t
*regs
)
447 static const gdb_byte darwin_syscall
[] = { 0xcd, 0x80 }; /* int 0x80 */
448 gdb_byte buf
[sizeof (darwin_syscall
)];
450 /* Check if PC is at a sigreturn system call. */
451 if (target_read_memory (regs
->uts
.ts32
.__eip
, buf
, sizeof (buf
)) == 0
452 && memcmp (buf
, darwin_syscall
, sizeof (darwin_syscall
)) == 0
453 && regs
->uts
.ts32
.__eax
== 0xb8 /* SYS_sigreturn */)
460 uctx_addr
= read_memory_unsigned_integer (regs
->uts
.ts32
.__esp
+ 4, 4);
461 mctx_addr
= read_memory_unsigned_integer (uctx_addr
+ 28, 4);
463 flags_addr
= mctx_addr
+ 12 + 9 * 4;
464 read_memory (flags_addr
, (gdb_byte
*) &eflags
, 4);
465 eflags
|= X86_EFLAGS_T
;
466 write_memory (flags_addr
, (gdb_byte
*) &eflags
, 4);
474 amd64_darwin_sstep_at_sigreturn (x86_thread_state_t
*regs
)
476 static const gdb_byte darwin_syscall
[] = { 0x0f, 0x05 }; /* syscall */
477 gdb_byte buf
[sizeof (darwin_syscall
)];
479 /* Check if PC is at a sigreturn system call. */
480 if (target_read_memory (regs
->uts
.ts64
.__rip
, buf
, sizeof (buf
)) == 0
481 && memcmp (buf
, darwin_syscall
, sizeof (darwin_syscall
)) == 0
482 && (regs
->uts
.ts64
.__rax
& 0xffffffff) == 0x20000b8 /* SYS_sigreturn */)
488 mctx_addr
= read_memory_unsigned_integer (regs
->uts
.ts64
.__rdi
+ 48, 8);
489 flags_addr
= mctx_addr
+ 16 + 17 * 8;
491 /* AMD64 is little endian. */
492 read_memory (flags_addr
, (gdb_byte
*) &rflags
, 4);
493 rflags
|= X86_EFLAGS_T
;
494 write_memory (flags_addr
, (gdb_byte
*) &rflags
, 4);
502 darwin_set_sstep (thread_t thread
, int enable
)
504 x86_thread_state_t regs
;
505 unsigned int count
= x86_THREAD_STATE_COUNT
;
508 kret
= thread_get_state (thread
, x86_THREAD_STATE
,
509 (thread_state_t
) ®s
, &count
);
510 if (kret
!= KERN_SUCCESS
)
512 printf_unfiltered (_("darwin_set_sstep: error %x, thread=%x\n"),
517 switch (regs
.tsh
.flavor
)
519 case x86_THREAD_STATE32
:
521 __uint32_t bit
= enable
? X86_EFLAGS_T
: 0;
523 if (enable
&& i386_darwin_sstep_at_sigreturn (®s
))
525 if ((regs
.uts
.ts32
.__eflags
& X86_EFLAGS_T
) == bit
)
527 regs
.uts
.ts32
.__eflags
= (regs
.uts
.ts32
.__eflags
& ~X86_EFLAGS_T
) | bit
;
528 kret
= thread_set_state (thread
, x86_THREAD_STATE
,
529 (thread_state_t
) ®s
, count
);
530 MACH_CHECK_ERROR (kret
);
533 case x86_THREAD_STATE64
:
535 __uint64_t bit
= enable
? X86_EFLAGS_T
: 0;
537 if (enable
&& amd64_darwin_sstep_at_sigreturn (®s
))
539 if ((regs
.uts
.ts64
.__rflags
& X86_EFLAGS_T
) == bit
)
541 regs
.uts
.ts64
.__rflags
= (regs
.uts
.ts64
.__rflags
& ~X86_EFLAGS_T
) | bit
;
542 kret
= thread_set_state (thread
, x86_THREAD_STATE
,
543 (thread_state_t
) ®s
, count
);
544 MACH_CHECK_ERROR (kret
);
548 error (_("darwin_set_sstep: unknown flavour: %d\n"), regs
.tsh
.flavor
);
553 darwin_complete_target (struct target_ops
*target
)
555 amd64_native_gregset64_reg_offset
= amd64_darwin_thread_state_reg_offset
;
556 amd64_native_gregset64_num_regs
= amd64_darwin_thread_state_num_regs
;
557 amd64_native_gregset32_reg_offset
= i386_darwin_thread_state_reg_offset
;
558 amd64_native_gregset32_num_regs
= i386_darwin_thread_state_num_regs
;
560 target
->to_fetch_registers
= i386_darwin_fetch_inferior_registers
;
561 target
->to_store_registers
= i386_darwin_store_inferior_registers
;