1 /* Darwin support for GDB, the GNU debugger.
2 Copyright (C) 1997-2024 Free Software Foundation, Inc.
4 Contributed by Apple Computer, Inc.
6 This file is part of GDB.
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>. */
27 #include "cli/cli-cmds.h"
29 #include "i386-tdep.h"
30 #include "i387-tdep.h"
32 #include "arch-utils.h"
36 #include "darwin-nat.h"
37 #include "i386-darwin-tdep.h"
40 #include "amd64-nat.h"
41 #include "amd64-tdep.h"
42 #include "amd64-darwin-tdep.h"
45 struct i386_darwin_nat_target final
: public x86_nat_target
<darwin_nat_target
>
47 /* Add our register access methods. */
48 void fetch_registers (struct regcache
*, int) override
;
49 void store_registers (struct regcache
*, int) override
;
52 static struct i386_darwin_nat_target darwin_target
;
54 /* Read register values from the inferior process.
55 If REGNO is -1, do this for all registers.
56 Otherwise, REGNO specifies which register (so we can save time). */
59 i386_darwin_nat_target::fetch_registers (struct regcache
*regcache
, int regno
)
61 thread_t current_thread
= regcache
->ptid ().tid ();
63 struct gdbarch
*gdbarch
= regcache
->arch ();
66 if (gdbarch_ptr_bit (gdbarch
) == 64)
68 if (regno
== -1 || amd64_native_gregset_supplies_p (gdbarch
, regno
))
70 x86_thread_state_t gp_regs
;
71 unsigned int gp_count
= x86_THREAD_STATE_COUNT
;
74 ret
= thread_get_state
75 (current_thread
, x86_THREAD_STATE
, (thread_state_t
) & gp_regs
,
77 if (ret
!= KERN_SUCCESS
)
79 warning (_("Error calling thread_get_state for "
80 "GP registers for thread 0x%lx\n"),
81 (unsigned long) current_thread
);
82 MACH_CHECK_ERROR (ret
);
85 /* Some kernels don't sanitize the values. */
86 gp_regs
.uts
.ts64
.__fs
&= 0xffff;
87 gp_regs
.uts
.ts64
.__gs
&= 0xffff;
89 amd64_supply_native_gregset (regcache
, &gp_regs
.uts
, -1);
93 if (regno
== -1 || !amd64_native_gregset_supplies_p (gdbarch
, regno
))
95 x86_float_state_t fp_regs
;
96 unsigned int fp_count
= x86_FLOAT_STATE_COUNT
;
99 ret
= thread_get_state
100 (current_thread
, x86_FLOAT_STATE
, (thread_state_t
) & fp_regs
,
102 if (ret
!= KERN_SUCCESS
)
104 warning (_("Error calling thread_get_state for "
105 "float registers for thread 0x%lx\n"),
106 (unsigned long) current_thread
);
107 MACH_CHECK_ERROR (ret
);
109 amd64_supply_fxsave (regcache
, -1, &fp_regs
.ufs
.fs64
.__fpu_fcw
);
116 if (regno
== -1 || regno
< I386_NUM_GREGS
)
118 x86_thread_state32_t gp_regs
;
119 unsigned int gp_count
= x86_THREAD_STATE32_COUNT
;
123 ret
= thread_get_state
124 (current_thread
, x86_THREAD_STATE32
, (thread_state_t
) &gp_regs
,
126 if (ret
!= KERN_SUCCESS
)
128 warning (_("Error calling thread_get_state for "
129 "GP registers for thread 0x%lx\n"),
130 (unsigned long) current_thread
);
131 MACH_CHECK_ERROR (ret
);
133 for (i
= 0; i
< I386_NUM_GREGS
; i
++)
135 (i
, (char *) &gp_regs
+ i386_darwin_thread_state_reg_offset
[i
]);
141 || (regno
>= I386_ST0_REGNUM
&& regno
< I386_SSE_NUM_REGS
))
143 x86_float_state32_t fp_regs
;
144 unsigned int fp_count
= x86_FLOAT_STATE32_COUNT
;
147 ret
= thread_get_state
148 (current_thread
, x86_FLOAT_STATE32
, (thread_state_t
) &fp_regs
,
150 if (ret
!= KERN_SUCCESS
)
152 warning (_("Error calling thread_get_state for "
153 "float registers for thread 0x%lx\n"),
154 (unsigned long) current_thread
);
155 MACH_CHECK_ERROR (ret
);
157 i387_supply_fxsave (regcache
, -1, &fp_regs
.__fpu_fcw
);
164 warning (_("unknown register %d"), regno
);
165 regcache
->raw_supply (regno
, NULL
);
169 /* Store our register values back into the inferior.
170 If REGNO is -1, do this for all registers.
171 Otherwise, REGNO specifies which register (so we can save time). */
174 i386_darwin_nat_target::store_registers (struct regcache
*regcache
,
177 thread_t current_thread
= regcache
->ptid ().tid ();
178 struct gdbarch
*gdbarch
= regcache
->arch ();
181 if (gdbarch_ptr_bit (gdbarch
) == 64)
183 if (regno
== -1 || amd64_native_gregset_supplies_p (gdbarch
, regno
))
185 x86_thread_state_t gp_regs
;
187 unsigned int gp_count
= x86_THREAD_STATE_COUNT
;
189 ret
= thread_get_state
190 (current_thread
, x86_THREAD_STATE
, (thread_state_t
) &gp_regs
,
192 MACH_CHECK_ERROR (ret
);
193 gdb_assert (gp_regs
.tsh
.flavor
== x86_THREAD_STATE64
);
194 gdb_assert (gp_regs
.tsh
.count
== x86_THREAD_STATE64_COUNT
);
196 amd64_collect_native_gregset (regcache
, &gp_regs
.uts
, regno
);
198 /* Some kernels don't sanitize the values. */
199 gp_regs
.uts
.ts64
.__fs
&= 0xffff;
200 gp_regs
.uts
.ts64
.__gs
&= 0xffff;
202 ret
= thread_set_state (current_thread
, x86_THREAD_STATE
,
203 (thread_state_t
) &gp_regs
,
204 x86_THREAD_STATE_COUNT
);
205 MACH_CHECK_ERROR (ret
);
208 if (regno
== -1 || !amd64_native_gregset_supplies_p (gdbarch
, regno
))
210 x86_float_state_t fp_regs
;
212 unsigned int fp_count
= x86_FLOAT_STATE_COUNT
;
214 ret
= thread_get_state
215 (current_thread
, x86_FLOAT_STATE
, (thread_state_t
) & fp_regs
,
217 MACH_CHECK_ERROR (ret
);
218 gdb_assert (fp_regs
.fsh
.flavor
== x86_FLOAT_STATE64
);
219 gdb_assert (fp_regs
.fsh
.count
== x86_FLOAT_STATE64_COUNT
);
221 amd64_collect_fxsave (regcache
, regno
, &fp_regs
.ufs
.fs64
.__fpu_fcw
);
223 ret
= thread_set_state (current_thread
, x86_FLOAT_STATE
,
224 (thread_state_t
) & fp_regs
,
225 x86_FLOAT_STATE_COUNT
);
226 MACH_CHECK_ERROR (ret
);
232 if (regno
== -1 || regno
< I386_NUM_GREGS
)
234 x86_thread_state32_t gp_regs
;
236 unsigned int gp_count
= x86_THREAD_STATE32_COUNT
;
239 ret
= thread_get_state
240 (current_thread
, x86_THREAD_STATE32
, (thread_state_t
) &gp_regs
,
242 MACH_CHECK_ERROR (ret
);
244 for (i
= 0; i
< I386_NUM_GREGS
; i
++)
245 if (regno
== -1 || regno
== i
)
246 regcache
->raw_collect
247 (i
, (char *) &gp_regs
+ i386_darwin_thread_state_reg_offset
[i
]);
249 ret
= thread_set_state (current_thread
, x86_THREAD_STATE32
,
250 (thread_state_t
) &gp_regs
,
251 x86_THREAD_STATE32_COUNT
);
252 MACH_CHECK_ERROR (ret
);
256 || (regno
>= I386_ST0_REGNUM
&& regno
< I386_SSE_NUM_REGS
))
258 x86_float_state32_t fp_regs
;
259 unsigned int fp_count
= x86_FLOAT_STATE32_COUNT
;
262 ret
= thread_get_state
263 (current_thread
, x86_FLOAT_STATE32
, (thread_state_t
) & fp_regs
,
265 MACH_CHECK_ERROR (ret
);
267 i387_collect_fxsave (regcache
, regno
, &fp_regs
.__fpu_fcw
);
269 ret
= thread_set_state (current_thread
, x86_FLOAT_STATE32
,
270 (thread_state_t
) &fp_regs
,
271 x86_FLOAT_STATE32_COUNT
);
272 MACH_CHECK_ERROR (ret
);
277 /* Support for debug registers, boosted mostly from i386-linux-nat.c. */
280 i386_darwin_dr_set (int regnum
, CORE_ADDR value
)
282 thread_t current_thread
;
283 x86_debug_state_t dr_regs
;
285 unsigned int dr_count
;
287 gdb_assert (regnum
>= 0 && regnum
<= DR_CONTROL
);
289 current_thread
= inferior_ptid
.tid ();
291 dr_regs
.dsh
.flavor
= x86_DEBUG_STATE
;
292 dr_regs
.dsh
.count
= x86_DEBUG_STATE_COUNT
;
293 dr_count
= x86_DEBUG_STATE_COUNT
;
294 ret
= thread_get_state (current_thread
, x86_DEBUG_STATE
,
295 (thread_state_t
) &dr_regs
, &dr_count
);
296 MACH_CHECK_ERROR (ret
);
298 switch (dr_regs
.dsh
.flavor
)
300 case x86_DEBUG_STATE32
:
304 dr_regs
.uds
.ds32
.__dr0
= value
;
307 dr_regs
.uds
.ds32
.__dr1
= value
;
310 dr_regs
.uds
.ds32
.__dr2
= value
;
313 dr_regs
.uds
.ds32
.__dr3
= value
;
316 dr_regs
.uds
.ds32
.__dr4
= value
;
319 dr_regs
.uds
.ds32
.__dr5
= value
;
322 dr_regs
.uds
.ds32
.__dr6
= value
;
325 dr_regs
.uds
.ds32
.__dr7
= value
;
330 case x86_DEBUG_STATE64
:
334 dr_regs
.uds
.ds64
.__dr0
= value
;
337 dr_regs
.uds
.ds64
.__dr1
= value
;
340 dr_regs
.uds
.ds64
.__dr2
= value
;
343 dr_regs
.uds
.ds64
.__dr3
= value
;
346 dr_regs
.uds
.ds64
.__dr4
= value
;
349 dr_regs
.uds
.ds64
.__dr5
= value
;
352 dr_regs
.uds
.ds64
.__dr6
= value
;
355 dr_regs
.uds
.ds64
.__dr7
= value
;
362 ret
= thread_set_state (current_thread
, dr_regs
.dsh
.flavor
,
363 (thread_state_t
) &dr_regs
.uds
, dr_count
);
365 MACH_CHECK_ERROR (ret
);
369 i386_darwin_dr_get (int regnum
)
371 thread_t current_thread
;
372 x86_debug_state_t dr_regs
;
374 unsigned int dr_count
;
376 gdb_assert (regnum
>= 0 && regnum
<= DR_CONTROL
);
378 current_thread
= inferior_ptid
.tid ();
380 dr_regs
.dsh
.flavor
= x86_DEBUG_STATE
;
381 dr_regs
.dsh
.count
= x86_DEBUG_STATE_COUNT
;
382 dr_count
= x86_DEBUG_STATE_COUNT
;
383 ret
= thread_get_state (current_thread
, x86_DEBUG_STATE
,
384 (thread_state_t
) &dr_regs
, &dr_count
);
385 MACH_CHECK_ERROR (ret
);
387 switch (dr_regs
.dsh
.flavor
)
389 case x86_DEBUG_STATE32
:
393 return dr_regs
.uds
.ds32
.__dr0
;
395 return dr_regs
.uds
.ds32
.__dr1
;
397 return dr_regs
.uds
.ds32
.__dr2
;
399 return dr_regs
.uds
.ds32
.__dr3
;
401 return dr_regs
.uds
.ds32
.__dr4
;
403 return dr_regs
.uds
.ds32
.__dr5
;
405 return dr_regs
.uds
.ds32
.__dr6
;
407 return dr_regs
.uds
.ds32
.__dr7
;
413 case x86_DEBUG_STATE64
:
417 return dr_regs
.uds
.ds64
.__dr0
;
419 return dr_regs
.uds
.ds64
.__dr1
;
421 return dr_regs
.uds
.ds64
.__dr2
;
423 return dr_regs
.uds
.ds64
.__dr3
;
425 return dr_regs
.uds
.ds64
.__dr4
;
427 return dr_regs
.uds
.ds64
.__dr5
;
429 return dr_regs
.uds
.ds64
.__dr6
;
431 return dr_regs
.uds
.ds64
.__dr7
;
443 i386_darwin_dr_set_control (unsigned long control
)
445 i386_darwin_dr_set (DR_CONTROL
, control
);
449 i386_darwin_dr_set_addr (int regnum
, CORE_ADDR addr
)
451 gdb_assert (regnum
>= 0 && regnum
<= DR_LASTADDR
- DR_FIRSTADDR
);
453 i386_darwin_dr_set (DR_FIRSTADDR
+ regnum
, addr
);
457 i386_darwin_dr_get_addr (int regnum
)
459 return i386_darwin_dr_get (regnum
);
463 i386_darwin_dr_get_status (void)
465 return i386_darwin_dr_get (DR_STATUS
);
469 i386_darwin_dr_get_control (void)
471 return i386_darwin_dr_get (DR_CONTROL
);
475 darwin_check_osabi (darwin_inferior
*inf
, thread_t thread
)
477 if (gdbarch_osabi (current_inferior ()->arch ()) == GDB_OSABI_UNKNOWN
)
479 /* Attaching to a process. Let's figure out what kind it is. */
480 x86_thread_state_t gp_regs
;
481 unsigned int gp_count
= x86_THREAD_STATE_COUNT
;
484 ret
= thread_get_state (thread
, x86_THREAD_STATE
,
485 (thread_state_t
) &gp_regs
, &gp_count
);
486 if (ret
!= KERN_SUCCESS
)
488 MACH_CHECK_ERROR (ret
);
493 gdbarch_info_fill (&info
);
494 info
.byte_order
= gdbarch_byte_order (current_inferior ()->arch ());
495 info
.osabi
= GDB_OSABI_DARWIN
;
496 if (gp_regs
.tsh
.flavor
== x86_THREAD_STATE64
)
497 info
.bfd_arch_info
= bfd_lookup_arch (bfd_arch_i386
,
500 info
.bfd_arch_info
= bfd_lookup_arch (bfd_arch_i386
,
502 gdbarch_update_p (current_inferior (), info
);
506 #define X86_EFLAGS_T 0x100UL
508 /* Returning from a signal trampoline is done by calling a
509 special system call (sigreturn). This system call
510 restores the registers that were saved when the signal was
511 raised, including %eflags/%rflags. That means that single-stepping
512 won't work. Instead, we'll have to modify the signal context
513 that's about to be restored, and set the trace flag there. */
516 i386_darwin_sstep_at_sigreturn (x86_thread_state_t
*regs
)
518 bfd_endian byte_order
= gdbarch_byte_order (current_inferior ()->arch ());
519 static const gdb_byte darwin_syscall
[] = { 0xcd, 0x80 }; /* int 0x80 */
520 gdb_byte buf
[sizeof (darwin_syscall
)];
522 /* Check if PC is at a sigreturn system call. */
523 if (target_read_memory (regs
->uts
.ts32
.__eip
, buf
, sizeof (buf
)) == 0
524 && memcmp (buf
, darwin_syscall
, sizeof (darwin_syscall
)) == 0
525 && regs
->uts
.ts32
.__eax
== 0xb8 /* SYS_sigreturn */)
532 uctx_addr
= read_memory_unsigned_integer
533 (regs
->uts
.ts32
.__esp
+ 4, 4, byte_order
);
534 mctx_addr
= read_memory_unsigned_integer
535 (uctx_addr
+ 28, 4, byte_order
);
537 flags_addr
= mctx_addr
+ 12 + 9 * 4;
538 read_memory (flags_addr
, (gdb_byte
*) &eflags
, 4);
539 eflags
|= X86_EFLAGS_T
;
540 write_memory (flags_addr
, (gdb_byte
*) &eflags
, 4);
549 amd64_darwin_sstep_at_sigreturn (x86_thread_state_t
*regs
)
551 bfd_endian byte_order
= gdbarch_byte_order (current_inferior ()->arch ());
552 static const gdb_byte darwin_syscall
[] = { 0x0f, 0x05 }; /* syscall */
553 gdb_byte buf
[sizeof (darwin_syscall
)];
555 /* Check if PC is at a sigreturn system call. */
556 if (target_read_memory (regs
->uts
.ts64
.__rip
, buf
, sizeof (buf
)) == 0
557 && memcmp (buf
, darwin_syscall
, sizeof (darwin_syscall
)) == 0
558 && (regs
->uts
.ts64
.__rax
& 0xffffffff) == 0x20000b8 /* SYS_sigreturn */)
564 mctx_addr
= read_memory_unsigned_integer
565 (regs
->uts
.ts64
.__rdi
+ 48, 8, byte_order
);
566 flags_addr
= mctx_addr
+ 16 + 17 * 8;
568 /* AMD64 is little endian. */
569 read_memory (flags_addr
, (gdb_byte
*) &rflags
, 4);
570 rflags
|= X86_EFLAGS_T
;
571 write_memory (flags_addr
, (gdb_byte
*) &rflags
, 4);
580 darwin_set_sstep (thread_t thread
, int enable
)
582 x86_thread_state_t regs
;
583 unsigned int count
= x86_THREAD_STATE_COUNT
;
586 kret
= thread_get_state (thread
, x86_THREAD_STATE
,
587 (thread_state_t
) ®s
, &count
);
588 if (kret
!= KERN_SUCCESS
)
590 warning (_("darwin_set_sstep: error %x, thread=%x\n"),
595 switch (regs
.tsh
.flavor
)
597 case x86_THREAD_STATE32
:
599 __uint32_t bit
= enable
? X86_EFLAGS_T
: 0;
601 if (enable
&& i386_darwin_sstep_at_sigreturn (®s
))
603 if ((regs
.uts
.ts32
.__eflags
& X86_EFLAGS_T
) == bit
)
605 regs
.uts
.ts32
.__eflags
606 = (regs
.uts
.ts32
.__eflags
& ~X86_EFLAGS_T
) | bit
;
607 kret
= thread_set_state (thread
, x86_THREAD_STATE
,
608 (thread_state_t
) ®s
, count
);
609 MACH_CHECK_ERROR (kret
);
613 case x86_THREAD_STATE64
:
615 __uint64_t bit
= enable
? X86_EFLAGS_T
: 0;
617 if (enable
&& amd64_darwin_sstep_at_sigreturn (®s
))
619 if ((regs
.uts
.ts64
.__rflags
& X86_EFLAGS_T
) == bit
)
621 regs
.uts
.ts64
.__rflags
622 = (regs
.uts
.ts64
.__rflags
& ~X86_EFLAGS_T
) | bit
;
623 kret
= thread_set_state (thread
, x86_THREAD_STATE
,
624 (thread_state_t
) ®s
, count
);
625 MACH_CHECK_ERROR (kret
);
630 error (_("darwin_set_sstep: unknown flavour: %d"), regs
.tsh
.flavor
);
634 void _initialize_i386_darwin_nat ();
636 _initialize_i386_darwin_nat ()
639 amd64_native_gregset64_reg_offset
= amd64_darwin_thread_state_reg_offset
;
640 amd64_native_gregset64_num_regs
= amd64_darwin_thread_state_num_regs
;
641 amd64_native_gregset32_reg_offset
= i386_darwin_thread_state_reg_offset
;
642 amd64_native_gregset32_num_regs
= i386_darwin_thread_state_num_regs
;
645 x86_dr_low
.set_control
= i386_darwin_dr_set_control
;
646 x86_dr_low
.set_addr
= i386_darwin_dr_set_addr
;
647 x86_dr_low
.get_addr
= i386_darwin_dr_get_addr
;
648 x86_dr_low
.get_status
= i386_darwin_dr_get_status
;
649 x86_dr_low
.get_control
= i386_darwin_dr_get_control
;
651 /* Let's assume that the kernel is 64 bits iff the executable is. */
653 x86_set_debug_register_length (8);
655 x86_set_debug_register_length (4);
658 add_inf_child_target (&darwin_target
);