1 /* Darwin support for GDB, the GNU debugger.
2 Copyright 1997, 1998, 1999, 2000, 2001, 2002, 2008, 2009, 2010
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-tdep.h"
44 #include "amd64-darwin-tdep.h"
47 /* Read register values from the inferior process.
48 If REGNO is -1, do this for all registers.
49 Otherwise, REGNO specifies which register (so we can save time). */
51 i386_darwin_fetch_inferior_registers (struct target_ops
*ops
,
52 struct regcache
*regcache
, int regno
)
54 thread_t current_thread
= ptid_get_tid (inferior_ptid
);
56 struct gdbarch
*gdbarch
= get_regcache_arch (regcache
);
59 if (gdbarch_ptr_bit (gdbarch
) == 64)
61 if (regno
== -1 || amd64_native_gregset_supplies_p (gdbarch
, regno
))
63 x86_thread_state_t gp_regs
;
64 unsigned int gp_count
= x86_THREAD_STATE_COUNT
;
67 ret
= thread_get_state
68 (current_thread
, x86_THREAD_STATE
, (thread_state_t
) & gp_regs
,
70 if (ret
!= KERN_SUCCESS
)
72 printf_unfiltered (_("Error calling thread_get_state for GP registers for thread 0x%ulx"), current_thread
);
73 MACH_CHECK_ERROR (ret
);
75 amd64_supply_native_gregset (regcache
, &gp_regs
.uts
, -1);
79 if (regno
== -1 || !amd64_native_gregset_supplies_p (gdbarch
, regno
))
81 x86_float_state_t fp_regs
;
82 unsigned int fp_count
= x86_FLOAT_STATE_COUNT
;
85 ret
= thread_get_state
86 (current_thread
, x86_FLOAT_STATE
, (thread_state_t
) & fp_regs
,
88 if (ret
!= KERN_SUCCESS
)
90 printf_unfiltered (_("Error calling thread_get_state for float registers for thread 0x%ulx"), current_thread
);
91 MACH_CHECK_ERROR (ret
);
93 amd64_supply_fxsave (regcache
, -1, &fp_regs
.ufs
.fs64
.__fpu_fcw
);
100 if (regno
== -1 || regno
< I386_NUM_GREGS
)
102 i386_thread_state_t gp_regs
;
103 unsigned int gp_count
= i386_THREAD_STATE_COUNT
;
107 ret
= thread_get_state
108 (current_thread
, i386_THREAD_STATE
, (thread_state_t
) & gp_regs
,
110 if (ret
!= KERN_SUCCESS
)
112 printf_unfiltered (_("Error calling thread_get_state for GP registers for thread 0x%ulx"), current_thread
);
113 MACH_CHECK_ERROR (ret
);
115 for (i
= 0; i
< I386_NUM_GREGS
; i
++)
118 (char *)&gp_regs
+ i386_darwin_thread_state_reg_offset
[i
]);
124 || (regno
>= I386_ST0_REGNUM
&& regno
< I386_SSE_NUM_REGS
))
126 i386_float_state_t fp_regs
;
127 unsigned int fp_count
= i386_FLOAT_STATE_COUNT
;
130 ret
= thread_get_state
131 (current_thread
, i386_FLOAT_STATE
, (thread_state_t
) & fp_regs
,
133 if (ret
!= KERN_SUCCESS
)
135 printf_unfiltered (_("Error calling thread_get_state for float registers for thread 0x%ulx"), current_thread
);
136 MACH_CHECK_ERROR (ret
);
138 i387_supply_fxsave (regcache
, -1, &fp_regs
.__fpu_fcw
);
145 warning (_("unknown register %d"), regno
);
146 regcache_raw_supply (regcache
, regno
, NULL
);
150 /* Store our register values back into the inferior.
151 If REGNO is -1, do this for all registers.
152 Otherwise, REGNO specifies which register (so we can save time). */
155 i386_darwin_store_inferior_registers (struct target_ops
*ops
,
156 struct regcache
*regcache
, int regno
)
158 thread_t current_thread
= ptid_get_tid (inferior_ptid
);
159 struct gdbarch
*gdbarch
= get_regcache_arch (regcache
);
162 if (gdbarch_ptr_bit (gdbarch
) == 64)
164 if (regno
== -1 || amd64_native_gregset_supplies_p (gdbarch
, regno
))
166 x86_thread_state_t gp_regs
;
168 unsigned int gp_count
= x86_THREAD_STATE_COUNT
;
170 ret
= thread_get_state
171 (current_thread
, x86_THREAD_STATE
, (thread_state_t
) &gp_regs
,
173 MACH_CHECK_ERROR (ret
);
174 gdb_assert (gp_regs
.tsh
.flavor
== x86_THREAD_STATE64
);
175 gdb_assert (gp_regs
.tsh
.count
== x86_THREAD_STATE64_COUNT
);
177 amd64_collect_native_gregset (regcache
, &gp_regs
.uts
, regno
);
179 ret
= thread_set_state (current_thread
, x86_THREAD_STATE
,
180 (thread_state_t
) &gp_regs
,
181 x86_THREAD_STATE_COUNT
);
182 MACH_CHECK_ERROR (ret
);
185 if (regno
== -1 || !amd64_native_gregset_supplies_p (gdbarch
, regno
))
187 x86_float_state_t fp_regs
;
189 unsigned int fp_count
= x86_FLOAT_STATE_COUNT
;
191 ret
= thread_get_state
192 (current_thread
, x86_FLOAT_STATE
, (thread_state_t
) & fp_regs
,
194 MACH_CHECK_ERROR (ret
);
195 gdb_assert (fp_regs
.fsh
.flavor
== x86_FLOAT_STATE64
);
196 gdb_assert (fp_regs
.fsh
.count
== x86_FLOAT_STATE64_COUNT
);
198 amd64_collect_fxsave (regcache
, regno
, &fp_regs
.ufs
.fs64
.__fpu_fcw
);
200 ret
= thread_set_state (current_thread
, x86_FLOAT_STATE
,
201 (thread_state_t
) & fp_regs
,
202 x86_FLOAT_STATE_COUNT
);
203 MACH_CHECK_ERROR (ret
);
209 if (regno
== -1 || regno
< I386_NUM_GREGS
)
211 i386_thread_state_t gp_regs
;
213 unsigned int gp_count
= i386_THREAD_STATE_COUNT
;
216 ret
= thread_get_state
217 (current_thread
, i386_THREAD_STATE
, (thread_state_t
) & gp_regs
,
219 MACH_CHECK_ERROR (ret
);
221 for (i
= 0; i
< I386_NUM_GREGS
; i
++)
222 if (regno
== -1 || regno
== i
)
225 (char *)&gp_regs
+ i386_darwin_thread_state_reg_offset
[i
]);
227 ret
= thread_set_state (current_thread
, i386_THREAD_STATE
,
228 (thread_state_t
) & gp_regs
,
229 i386_THREAD_STATE_COUNT
);
230 MACH_CHECK_ERROR (ret
);
234 || (regno
>= I386_ST0_REGNUM
&& regno
< I386_SSE_NUM_REGS
))
236 i386_float_state_t fp_regs
;
237 unsigned int fp_count
= i386_FLOAT_STATE_COUNT
;
240 ret
= thread_get_state
241 (current_thread
, i386_FLOAT_STATE
, (thread_state_t
) & fp_regs
,
243 MACH_CHECK_ERROR (ret
);
245 i387_collect_fxsave (regcache
, regno
, &fp_regs
.__fpu_fcw
);
247 ret
= thread_set_state (current_thread
, i386_FLOAT_STATE
,
248 (thread_state_t
) & fp_regs
,
249 i386_FLOAT_STATE_COUNT
);
250 MACH_CHECK_ERROR (ret
);
256 /* Support for debug registers, boosted mostly from i386-linux-nat.c. */
259 #define DR_FIRSTADDR 0
263 #define DR_LASTADDR 3
276 i386_darwin_dr_set (int regnum
, uint32_t value
)
279 thread_t current_thread
;
280 x86_debug_state_t dr_regs
;
282 unsigned int dr_count
= x86_DEBUG_STATE_COUNT
;
284 gdb_assert (regnum
>= 0 && regnum
<= DR_CONTROL
);
286 current_thread
= ptid_get_tid (inferior_ptid
);
288 dr_regs
.dsh
.flavor
= x86_DEBUG_STATE32
;
289 dr_regs
.dsh
.count
= x86_DEBUG_STATE32_COUNT
;
290 dr_count
= x86_DEBUG_STATE_COUNT
;
291 ret
= thread_get_state (current_thread
, x86_DEBUG_STATE
,
292 (thread_state_t
) &dr_regs
, &dr_count
);
294 if (ret
!= KERN_SUCCESS
)
296 printf_unfiltered (_("Error reading debug registers thread 0x%x via thread_get_state\n"), (int) current_thread
);
297 MACH_CHECK_ERROR (ret
);
303 dr_regs
.uds
.ds32
.__dr0
= value
;
306 dr_regs
.uds
.ds32
.__dr1
= value
;
309 dr_regs
.uds
.ds32
.__dr2
= value
;
312 dr_regs
.uds
.ds32
.__dr3
= value
;
315 dr_regs
.uds
.ds32
.__dr4
= value
;
318 dr_regs
.uds
.ds32
.__dr5
= value
;
321 dr_regs
.uds
.ds32
.__dr6
= value
;
324 dr_regs
.uds
.ds32
.__dr7
= value
;
328 ret
= thread_set_state (current_thread
, x86_DEBUG_STATE
,
329 (thread_state_t
) &dr_regs
, dr_count
);
331 if (ret
!= KERN_SUCCESS
)
333 printf_unfiltered (_("Error writing debug registers thread 0x%x via thread_get_state\n"), (int) current_thread
);
334 MACH_CHECK_ERROR (ret
);
339 i386_darwin_dr_get (int regnum
)
341 thread_t current_thread
;
342 x86_debug_state_t dr_regs
;
344 unsigned int dr_count
= x86_DEBUG_STATE_COUNT
;
346 gdb_assert (regnum
>= 0 && regnum
<= DR_CONTROL
);
348 current_thread
= ptid_get_tid (inferior_ptid
);
350 dr_regs
.dsh
.flavor
= x86_DEBUG_STATE32
;
351 dr_regs
.dsh
.count
= x86_DEBUG_STATE32_COUNT
;
352 dr_count
= x86_DEBUG_STATE_COUNT
;
353 ret
= thread_get_state (current_thread
, x86_DEBUG_STATE
,
354 (thread_state_t
) &dr_regs
, &dr_count
);
356 if (ret
!= KERN_SUCCESS
)
358 printf_unfiltered (_("Error reading debug registers thread 0x%x via thread_get_state\n"), (int) current_thread
);
359 MACH_CHECK_ERROR (ret
);
365 return dr_regs
.uds
.ds32
.__dr0
;
367 return dr_regs
.uds
.ds32
.__dr1
;
369 return dr_regs
.uds
.ds32
.__dr2
;
371 return dr_regs
.uds
.ds32
.__dr3
;
373 return dr_regs
.uds
.ds32
.__dr4
;
375 return dr_regs
.uds
.ds32
.__dr5
;
377 return dr_regs
.uds
.ds32
.__dr6
;
379 return dr_regs
.uds
.ds32
.__dr7
;
386 i386_darwin_dr_set_control (unsigned long control
)
388 i386_darwin_dr_set (DR_CONTROL
, control
);
392 i386_darwin_dr_set_addr (int regnum
, CORE_ADDR addr
)
394 gdb_assert (regnum
>= 0 && regnum
<= DR_LASTADDR
- DR_FIRSTADDR
);
396 i386_darwin_dr_set (DR_FIRSTADDR
+ regnum
, addr
);
400 i386_darwin_dr_reset_addr (int regnum
)
402 gdb_assert (regnum
>= 0 && regnum
<= DR_LASTADDR
- DR_FIRSTADDR
);
404 i386_darwin_dr_set (DR_FIRSTADDR
+ regnum
, 0L);
408 i386_darwin_dr_get_status (void)
410 return i386_darwin_dr_get (DR_STATUS
);
414 darwin_check_osabi (darwin_inferior
*inf
, thread_t thread
)
416 if (gdbarch_osabi (target_gdbarch
) == GDB_OSABI_UNKNOWN
)
418 /* Attaching to a process. Let's figure out what kind it is. */
419 x86_thread_state_t gp_regs
;
420 struct gdbarch_info info
;
421 unsigned int gp_count
= x86_THREAD_STATE_COUNT
;
424 ret
= thread_get_state (thread
, x86_THREAD_STATE
,
425 (thread_state_t
) &gp_regs
, &gp_count
);
426 if (ret
!= KERN_SUCCESS
)
428 MACH_CHECK_ERROR (ret
);
432 gdbarch_info_init (&info
);
433 gdbarch_info_fill (&info
);
434 info
.byte_order
= gdbarch_byte_order (target_gdbarch
);
435 info
.osabi
= GDB_OSABI_DARWIN
;
436 if (gp_regs
.tsh
.flavor
== x86_THREAD_STATE64
)
437 info
.bfd_arch_info
= bfd_lookup_arch (bfd_arch_i386
,
440 info
.bfd_arch_info
= bfd_lookup_arch (bfd_arch_i386
,
442 gdbarch_update_p (info
);
446 #define X86_EFLAGS_T 0x100UL
448 /* Returning from a signal trampoline is done by calling a
449 special system call (sigreturn). This system call
450 restores the registers that were saved when the signal was
451 raised, including %eflags/%rflags. That means that single-stepping
452 won't work. Instead, we'll have to modify the signal context
453 that's about to be restored, and set the trace flag there. */
456 i386_darwin_sstep_at_sigreturn (x86_thread_state_t
*regs
)
458 enum bfd_endian byte_order
= gdbarch_byte_order (target_gdbarch
);
459 static const gdb_byte darwin_syscall
[] = { 0xcd, 0x80 }; /* int 0x80 */
460 gdb_byte buf
[sizeof (darwin_syscall
)];
462 /* Check if PC is at a sigreturn system call. */
463 if (target_read_memory (regs
->uts
.ts32
.__eip
, buf
, sizeof (buf
)) == 0
464 && memcmp (buf
, darwin_syscall
, sizeof (darwin_syscall
)) == 0
465 && regs
->uts
.ts32
.__eax
== 0xb8 /* SYS_sigreturn */)
472 uctx_addr
= read_memory_unsigned_integer
473 (regs
->uts
.ts32
.__esp
+ 4, 4, byte_order
);
474 mctx_addr
= read_memory_unsigned_integer
475 (uctx_addr
+ 28, 4, byte_order
);
477 flags_addr
= mctx_addr
+ 12 + 9 * 4;
478 read_memory (flags_addr
, (gdb_byte
*) &eflags
, 4);
479 eflags
|= X86_EFLAGS_T
;
480 write_memory (flags_addr
, (gdb_byte
*) &eflags
, 4);
489 amd64_darwin_sstep_at_sigreturn (x86_thread_state_t
*regs
)
491 enum bfd_endian byte_order
= gdbarch_byte_order (target_gdbarch
);
492 static const gdb_byte darwin_syscall
[] = { 0x0f, 0x05 }; /* syscall */
493 gdb_byte buf
[sizeof (darwin_syscall
)];
495 /* Check if PC is at a sigreturn system call. */
496 if (target_read_memory (regs
->uts
.ts64
.__rip
, buf
, sizeof (buf
)) == 0
497 && memcmp (buf
, darwin_syscall
, sizeof (darwin_syscall
)) == 0
498 && (regs
->uts
.ts64
.__rax
& 0xffffffff) == 0x20000b8 /* SYS_sigreturn */)
504 mctx_addr
= read_memory_unsigned_integer
505 (regs
->uts
.ts64
.__rdi
+ 48, 8, byte_order
);
506 flags_addr
= mctx_addr
+ 16 + 17 * 8;
508 /* AMD64 is little endian. */
509 read_memory (flags_addr
, (gdb_byte
*) &rflags
, 4);
510 rflags
|= X86_EFLAGS_T
;
511 write_memory (flags_addr
, (gdb_byte
*) &rflags
, 4);
520 darwin_set_sstep (thread_t thread
, int enable
)
522 x86_thread_state_t regs
;
523 unsigned int count
= x86_THREAD_STATE_COUNT
;
526 kret
= thread_get_state (thread
, x86_THREAD_STATE
,
527 (thread_state_t
) ®s
, &count
);
528 if (kret
!= KERN_SUCCESS
)
530 printf_unfiltered (_("darwin_set_sstep: error %x, thread=%x\n"),
535 switch (regs
.tsh
.flavor
)
537 case x86_THREAD_STATE32
:
539 __uint32_t bit
= enable
? X86_EFLAGS_T
: 0;
541 if (enable
&& i386_darwin_sstep_at_sigreturn (®s
))
543 if ((regs
.uts
.ts32
.__eflags
& X86_EFLAGS_T
) == bit
)
545 regs
.uts
.ts32
.__eflags
= (regs
.uts
.ts32
.__eflags
& ~X86_EFLAGS_T
) | bit
;
546 kret
= thread_set_state (thread
, x86_THREAD_STATE
,
547 (thread_state_t
) ®s
, count
);
548 MACH_CHECK_ERROR (kret
);
552 case x86_THREAD_STATE64
:
554 __uint64_t bit
= enable
? X86_EFLAGS_T
: 0;
556 if (enable
&& amd64_darwin_sstep_at_sigreturn (®s
))
558 if ((regs
.uts
.ts64
.__rflags
& X86_EFLAGS_T
) == bit
)
560 regs
.uts
.ts64
.__rflags
= (regs
.uts
.ts64
.__rflags
& ~X86_EFLAGS_T
) | bit
;
561 kret
= thread_set_state (thread
, x86_THREAD_STATE
,
562 (thread_state_t
) ®s
, count
);
563 MACH_CHECK_ERROR (kret
);
568 error (_("darwin_set_sstep: unknown flavour: %d\n"), regs
.tsh
.flavor
);
573 darwin_complete_target (struct target_ops
*target
)
576 amd64_native_gregset64_reg_offset
= amd64_darwin_thread_state_reg_offset
;
577 amd64_native_gregset64_num_regs
= amd64_darwin_thread_state_num_regs
;
578 amd64_native_gregset32_reg_offset
= i386_darwin_thread_state_reg_offset
;
579 amd64_native_gregset32_num_regs
= i386_darwin_thread_state_num_regs
;
582 target
->to_fetch_registers
= i386_darwin_fetch_inferior_registers
;
583 target
->to_store_registers
= i386_darwin_store_inferior_registers
;