gdb, testsuite: Fix return value in gdb.base/foll-fork.exp
[binutils-gdb.git] / gdb / i386-darwin-nat.c
blob59655996565c44f43e1509d00912588c57642214
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/>. */
21 #include "frame.h"
22 #include "inferior.h"
23 #include "target.h"
24 #include "symfile.h"
25 #include "symtab.h"
26 #include "objfiles.h"
27 #include "cli/cli-cmds.h"
28 #include "regcache.h"
29 #include "i386-tdep.h"
30 #include "i387-tdep.h"
31 #include "gdbarch.h"
32 #include "arch-utils.h"
33 #include "gdbcore.h"
35 #include "x86-nat.h"
36 #include "darwin-nat.h"
37 #include "i386-darwin-tdep.h"
39 #ifdef BFD64
40 #include "amd64-nat.h"
41 #include "amd64-tdep.h"
42 #include "amd64-darwin-tdep.h"
43 #endif
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). */
58 void
59 i386_darwin_nat_target::fetch_registers (struct regcache *regcache, int regno)
61 thread_t current_thread = regcache->ptid ().tid ();
62 int fetched = 0;
63 struct gdbarch *gdbarch = regcache->arch ();
65 #ifdef BFD64
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;
72 kern_return_t ret;
74 ret = thread_get_state
75 (current_thread, x86_THREAD_STATE, (thread_state_t) & gp_regs,
76 &gp_count);
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);
90 fetched++;
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;
97 kern_return_t ret;
99 ret = thread_get_state
100 (current_thread, x86_FLOAT_STATE, (thread_state_t) & fp_regs,
101 &fp_count);
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);
110 fetched++;
113 else
114 #endif
116 if (regno == -1 || regno < I386_NUM_GREGS)
118 x86_thread_state32_t gp_regs;
119 unsigned int gp_count = x86_THREAD_STATE32_COUNT;
120 kern_return_t ret;
121 int i;
123 ret = thread_get_state
124 (current_thread, x86_THREAD_STATE32, (thread_state_t) &gp_regs,
125 &gp_count);
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++)
134 regcache->raw_supply
135 (i, (char *) &gp_regs + i386_darwin_thread_state_reg_offset[i]);
137 fetched++;
140 if (regno == -1
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;
145 kern_return_t ret;
147 ret = thread_get_state
148 (current_thread, x86_FLOAT_STATE32, (thread_state_t) &fp_regs,
149 &fp_count);
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);
158 fetched++;
162 if (! fetched)
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). */
173 void
174 i386_darwin_nat_target::store_registers (struct regcache *regcache,
175 int regno)
177 thread_t current_thread = regcache->ptid ().tid ();
178 struct gdbarch *gdbarch = regcache->arch ();
180 #ifdef BFD64
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;
186 kern_return_t ret;
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,
191 &gp_count);
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;
211 kern_return_t ret;
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,
216 &fp_count);
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);
229 else
230 #endif
232 if (regno == -1 || regno < I386_NUM_GREGS)
234 x86_thread_state32_t gp_regs;
235 kern_return_t ret;
236 unsigned int gp_count = x86_THREAD_STATE32_COUNT;
237 int i;
239 ret = thread_get_state
240 (current_thread, x86_THREAD_STATE32, (thread_state_t) &gp_regs,
241 &gp_count);
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);
255 if (regno == -1
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;
260 kern_return_t ret;
262 ret = thread_get_state
263 (current_thread, x86_FLOAT_STATE32, (thread_state_t) & fp_regs,
264 &fp_count);
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. */
279 static void
280 i386_darwin_dr_set (int regnum, CORE_ADDR value)
282 thread_t current_thread;
283 x86_debug_state_t dr_regs;
284 kern_return_t ret;
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:
301 switch (regnum)
303 case 0:
304 dr_regs.uds.ds32.__dr0 = value;
305 break;
306 case 1:
307 dr_regs.uds.ds32.__dr1 = value;
308 break;
309 case 2:
310 dr_regs.uds.ds32.__dr2 = value;
311 break;
312 case 3:
313 dr_regs.uds.ds32.__dr3 = value;
314 break;
315 case 4:
316 dr_regs.uds.ds32.__dr4 = value;
317 break;
318 case 5:
319 dr_regs.uds.ds32.__dr5 = value;
320 break;
321 case 6:
322 dr_regs.uds.ds32.__dr6 = value;
323 break;
324 case 7:
325 dr_regs.uds.ds32.__dr7 = value;
326 break;
328 break;
329 #ifdef BFD64
330 case x86_DEBUG_STATE64:
331 switch (regnum)
333 case 0:
334 dr_regs.uds.ds64.__dr0 = value;
335 break;
336 case 1:
337 dr_regs.uds.ds64.__dr1 = value;
338 break;
339 case 2:
340 dr_regs.uds.ds64.__dr2 = value;
341 break;
342 case 3:
343 dr_regs.uds.ds64.__dr3 = value;
344 break;
345 case 4:
346 dr_regs.uds.ds64.__dr4 = value;
347 break;
348 case 5:
349 dr_regs.uds.ds64.__dr5 = value;
350 break;
351 case 6:
352 dr_regs.uds.ds64.__dr6 = value;
353 break;
354 case 7:
355 dr_regs.uds.ds64.__dr7 = value;
356 break;
358 break;
359 #endif
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);
368 static CORE_ADDR
369 i386_darwin_dr_get (int regnum)
371 thread_t current_thread;
372 x86_debug_state_t dr_regs;
373 kern_return_t ret;
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:
390 switch (regnum)
392 case 0:
393 return dr_regs.uds.ds32.__dr0;
394 case 1:
395 return dr_regs.uds.ds32.__dr1;
396 case 2:
397 return dr_regs.uds.ds32.__dr2;
398 case 3:
399 return dr_regs.uds.ds32.__dr3;
400 case 4:
401 return dr_regs.uds.ds32.__dr4;
402 case 5:
403 return dr_regs.uds.ds32.__dr5;
404 case 6:
405 return dr_regs.uds.ds32.__dr6;
406 case 7:
407 return dr_regs.uds.ds32.__dr7;
408 default:
409 return -1;
411 break;
412 #ifdef BFD64
413 case x86_DEBUG_STATE64:
414 switch (regnum)
416 case 0:
417 return dr_regs.uds.ds64.__dr0;
418 case 1:
419 return dr_regs.uds.ds64.__dr1;
420 case 2:
421 return dr_regs.uds.ds64.__dr2;
422 case 3:
423 return dr_regs.uds.ds64.__dr3;
424 case 4:
425 return dr_regs.uds.ds64.__dr4;
426 case 5:
427 return dr_regs.uds.ds64.__dr5;
428 case 6:
429 return dr_regs.uds.ds64.__dr6;
430 case 7:
431 return dr_regs.uds.ds64.__dr7;
432 default:
433 return -1;
435 break;
436 #endif
437 default:
438 return -1;
442 static void
443 i386_darwin_dr_set_control (unsigned long control)
445 i386_darwin_dr_set (DR_CONTROL, control);
448 static void
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);
456 static CORE_ADDR
457 i386_darwin_dr_get_addr (int regnum)
459 return i386_darwin_dr_get (regnum);
462 static unsigned long
463 i386_darwin_dr_get_status (void)
465 return i386_darwin_dr_get (DR_STATUS);
468 static unsigned long
469 i386_darwin_dr_get_control (void)
471 return i386_darwin_dr_get (DR_CONTROL);
474 void
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;
482 kern_return_t ret;
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);
489 return;
492 gdbarch_info info;
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,
498 bfd_mach_x86_64);
499 else
500 info.bfd_arch_info = bfd_lookup_arch (bfd_arch_i386,
501 bfd_mach_i386_i386);
502 gdbarch_update_p (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. */
515 static int
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 */)
527 ULONGEST uctx_addr;
528 ULONGEST mctx_addr;
529 ULONGEST flags_addr;
530 unsigned int eflags;
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);
542 return 1;
544 return 0;
547 #ifdef BFD64
548 static int
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 */)
560 ULONGEST mctx_addr;
561 ULONGEST flags_addr;
562 unsigned int rflags;
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);
573 return 1;
575 return 0;
577 #endif
579 void
580 darwin_set_sstep (thread_t thread, int enable)
582 x86_thread_state_t regs;
583 unsigned int count = x86_THREAD_STATE_COUNT;
584 kern_return_t kret;
586 kret = thread_get_state (thread, x86_THREAD_STATE,
587 (thread_state_t) &regs, &count);
588 if (kret != KERN_SUCCESS)
590 warning (_("darwin_set_sstep: error %x, thread=%x\n"),
591 kret, thread);
592 return;
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 (&regs))
602 return;
603 if ((regs.uts.ts32.__eflags & X86_EFLAGS_T) == bit)
604 return;
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) &regs, count);
609 MACH_CHECK_ERROR (kret);
611 break;
612 #ifdef BFD64
613 case x86_THREAD_STATE64:
615 __uint64_t bit = enable ? X86_EFLAGS_T : 0;
617 if (enable && amd64_darwin_sstep_at_sigreturn (&regs))
618 return;
619 if ((regs.uts.ts64.__rflags & X86_EFLAGS_T) == bit)
620 return;
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) &regs, count);
625 MACH_CHECK_ERROR (kret);
627 break;
628 #endif
629 default:
630 error (_("darwin_set_sstep: unknown flavour: %d"), regs.tsh.flavor);
634 void _initialize_i386_darwin_nat ();
635 void
636 _initialize_i386_darwin_nat ()
638 #ifdef BFD64
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;
643 #endif
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. */
652 #ifdef __x86_64__
653 x86_set_debug_register_length (8);
654 #else
655 x86_set_debug_register_length (4);
656 #endif
658 add_inf_child_target (&darwin_target);