gdb, testsuite: Fix return value in gdb.base/foll-fork.exp
[binutils-gdb.git] / gdb / i386-linux-nat.c
blob7278dd91ff0b93fd27e3a0794e9d28716f20ce06
1 /* Native-dependent code for GNU/Linux i386.
3 Copyright (C) 1999-2024 Free Software Foundation, Inc.
5 This file is part of GDB.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
20 #include "inferior.h"
21 #include "gdbcore.h"
22 #include "regcache.h"
23 #include "elf/common.h"
24 #include "nat/gdb_ptrace.h"
25 #include <sys/uio.h>
26 #include "gregset.h"
27 #include "gdb_proc_service.h"
29 #include "i386-linux-nat.h"
30 #include "i387-tdep.h"
31 #include "i386-tdep.h"
32 #include "i386-linux-tdep.h"
33 #include "gdbsupport/x86-xstate.h"
35 #include "x86-linux-nat.h"
36 #include "nat/linux-ptrace.h"
37 #include "inf-ptrace.h"
39 struct i386_linux_nat_target final : public x86_linux_nat_target
41 /* Add our register access methods. */
42 void fetch_registers (struct regcache *, int) override;
43 void store_registers (struct regcache *, int) override;
45 /* Override the default ptrace resume method. */
46 void low_resume (ptid_t ptid, int step, enum gdb_signal sig) override;
49 static i386_linux_nat_target the_i386_linux_nat_target;
51 /* The register sets used in GNU/Linux ELF core-dumps are identical to
52 the register sets in `struct user' that is used for a.out
53 core-dumps, and is also used by `ptrace'. The corresponding types
54 are `elf_gregset_t' for the general-purpose registers (with
55 `elf_greg_t' the type of a single GP register) and `elf_fpregset_t'
56 for the floating-point registers.
58 Those types used to be available under the names `gregset_t' and
59 `fpregset_t' too, and this file used those names in the past. But
60 those names are now used for the register sets used in the
61 `mcontext_t' type, and have a different size and layout. */
63 /* Which ptrace request retrieves which registers?
64 These apply to the corresponding SET requests as well. */
66 #define GETREGS_SUPPLIES(regno) \
67 ((0 <= (regno) && (regno) <= 15) || (regno) == I386_LINUX_ORIG_EAX_REGNUM)
69 #define GETFPXREGS_SUPPLIES(regno) \
70 (I386_ST0_REGNUM <= (regno) && (regno) < I386_SSE_NUM_REGS)
72 #define GETXSTATEREGS_SUPPLIES(regno) \
73 (I386_ST0_REGNUM <= (regno) && (regno) < I386_PKEYS_NUM_REGS)
75 /* Does the current host support the GETREGS request? */
76 int have_ptrace_getregs =
77 #ifdef HAVE_PTRACE_GETREGS
79 #else
81 #endif
84 /* Does the current host support the GETFPXREGS request? The header
85 file may or may not define it, and even if it is defined, the
86 kernel will return EIO if it's running on a pre-SSE processor.
88 My instinct is to attach this to some architecture- or
89 target-specific data structure, but really, a particular GDB
90 process can only run on top of one kernel at a time. So it's okay
91 for this to be a simple variable. */
92 int have_ptrace_getfpxregs =
93 #ifdef HAVE_PTRACE_GETFPXREGS
95 #else
97 #endif
101 /* Accessing registers through the U area, one at a time. */
103 /* Fetch one register. */
105 static void
106 fetch_register (struct regcache *regcache, int regno)
108 pid_t tid;
109 int val;
111 gdb_assert (!have_ptrace_getregs);
112 if (i386_linux_gregset_reg_offset[regno] == -1)
114 regcache->raw_supply (regno, NULL);
115 return;
118 tid = get_ptrace_pid (regcache->ptid ());
120 errno = 0;
121 val = ptrace (PTRACE_PEEKUSER, tid,
122 i386_linux_gregset_reg_offset[regno], 0);
123 if (errno != 0)
124 error (_("Couldn't read register %s (#%d): %s."),
125 gdbarch_register_name (regcache->arch (), regno),
126 regno, safe_strerror (errno));
128 regcache->raw_supply (regno, &val);
131 /* Store one register. */
133 static void
134 store_register (const struct regcache *regcache, int regno)
136 pid_t tid;
137 int val;
139 gdb_assert (!have_ptrace_getregs);
140 if (i386_linux_gregset_reg_offset[regno] == -1)
141 return;
143 tid = get_ptrace_pid (regcache->ptid ());
145 errno = 0;
146 regcache->raw_collect (regno, &val);
147 ptrace (PTRACE_POKEUSER, tid,
148 i386_linux_gregset_reg_offset[regno], val);
149 if (errno != 0)
150 error (_("Couldn't write register %s (#%d): %s."),
151 gdbarch_register_name (regcache->arch (), regno),
152 regno, safe_strerror (errno));
156 /* Transfering the general-purpose registers between GDB, inferiors
157 and core files. */
159 /* Fill GDB's register array with the general-purpose register values
160 in *GREGSETP. */
162 void
163 supply_gregset (struct regcache *regcache, const elf_gregset_t *gregsetp)
165 const gdb_byte *regp = (const gdb_byte *) gregsetp;
166 int i;
168 for (i = 0; i < I386_NUM_GREGS; i++)
169 regcache->raw_supply (i, regp + i386_linux_gregset_reg_offset[i]);
171 if (I386_LINUX_ORIG_EAX_REGNUM
172 < gdbarch_num_regs (regcache->arch ()))
173 regcache->raw_supply
174 (I386_LINUX_ORIG_EAX_REGNUM,
175 regp + i386_linux_gregset_reg_offset[I386_LINUX_ORIG_EAX_REGNUM]);
178 /* Fill register REGNO (if it is a general-purpose register) in
179 *GREGSETPS with the value in GDB's register array. If REGNO is -1,
180 do this for all registers. */
182 void
183 fill_gregset (const struct regcache *regcache,
184 elf_gregset_t *gregsetp, int regno)
186 gdb_byte *regp = (gdb_byte *) gregsetp;
187 int i;
189 for (i = 0; i < I386_NUM_GREGS; i++)
190 if (regno == -1 || regno == i)
191 regcache->raw_collect (i, regp + i386_linux_gregset_reg_offset[i]);
193 if ((regno == -1 || regno == I386_LINUX_ORIG_EAX_REGNUM)
194 && I386_LINUX_ORIG_EAX_REGNUM
195 < gdbarch_num_regs (regcache->arch ()))
196 regcache->raw_collect
197 (I386_LINUX_ORIG_EAX_REGNUM,
198 regp + i386_linux_gregset_reg_offset[I386_LINUX_ORIG_EAX_REGNUM]);
201 #ifdef HAVE_PTRACE_GETREGS
203 /* Fetch all general-purpose registers from process/thread TID and
204 store their values in GDB's register array. */
206 static void
207 fetch_regs (struct regcache *regcache, int tid)
209 elf_gregset_t regs;
210 elf_gregset_t *regs_p = &regs;
212 if (ptrace (PTRACE_GETREGS, tid, 0, (int) &regs) < 0)
214 if (errno == EIO)
216 /* The kernel we're running on doesn't support the GETREGS
217 request. Reset `have_ptrace_getregs'. */
218 have_ptrace_getregs = 0;
219 return;
222 perror_with_name (_("Couldn't get registers"));
225 supply_gregset (regcache, (const elf_gregset_t *) regs_p);
228 /* Store all valid general-purpose registers in GDB's register array
229 into the process/thread specified by TID. */
231 static void
232 store_regs (const struct regcache *regcache, int tid, int regno)
234 elf_gregset_t regs;
236 if (ptrace (PTRACE_GETREGS, tid, 0, (int) &regs) < 0)
237 perror_with_name (_("Couldn't get registers"));
239 fill_gregset (regcache, &regs, regno);
241 if (ptrace (PTRACE_SETREGS, tid, 0, (int) &regs) < 0)
242 perror_with_name (_("Couldn't write registers"));
245 #else
247 static void fetch_regs (struct regcache *regcache, int tid) {}
248 static void store_regs (const struct regcache *regcache, int tid, int regno) {}
250 #endif
253 /* Transfering floating-point registers between GDB, inferiors and cores. */
255 /* Fill GDB's register array with the floating-point register values in
256 *FPREGSETP. */
258 void
259 supply_fpregset (struct regcache *regcache, const elf_fpregset_t *fpregsetp)
261 i387_supply_fsave (regcache, -1, fpregsetp);
264 /* Fill register REGNO (if it is a floating-point register) in
265 *FPREGSETP with the value in GDB's register array. If REGNO is -1,
266 do this for all registers. */
268 void
269 fill_fpregset (const struct regcache *regcache,
270 elf_fpregset_t *fpregsetp, int regno)
272 i387_collect_fsave (regcache, regno, fpregsetp);
275 #ifdef HAVE_PTRACE_GETREGS
277 /* Fetch all floating-point registers from process/thread TID and store
278 thier values in GDB's register array. */
280 static void
281 fetch_fpregs (struct regcache *regcache, int tid)
283 elf_fpregset_t fpregs;
285 if (ptrace (PTRACE_GETFPREGS, tid, 0, (int) &fpregs) < 0)
286 perror_with_name (_("Couldn't get floating point status"));
288 supply_fpregset (regcache, (const elf_fpregset_t *) &fpregs);
291 /* Store all valid floating-point registers in GDB's register array
292 into the process/thread specified by TID. */
294 static void
295 store_fpregs (const struct regcache *regcache, int tid, int regno)
297 elf_fpregset_t fpregs;
299 if (ptrace (PTRACE_GETFPREGS, tid, 0, (int) &fpregs) < 0)
300 perror_with_name (_("Couldn't get floating point status"));
302 fill_fpregset (regcache, &fpregs, regno);
304 if (ptrace (PTRACE_SETFPREGS, tid, 0, (int) &fpregs) < 0)
305 perror_with_name (_("Couldn't write floating point status"));
308 #else
310 static void
311 fetch_fpregs (struct regcache *regcache, int tid)
315 static void
316 store_fpregs (const struct regcache *regcache, int tid, int regno)
320 #endif
323 /* Transfering floating-point and SSE registers to and from GDB. */
325 /* Fetch all registers covered by the PTRACE_GETREGSET request from
326 process/thread TID and store their values in GDB's register array.
327 Return non-zero if successful, zero otherwise. */
329 static int
330 fetch_xstateregs (struct regcache *regcache, int tid)
332 struct gdbarch *gdbarch = regcache->arch ();
333 const i386_gdbarch_tdep *tdep = gdbarch_tdep<i386_gdbarch_tdep> (gdbarch);
334 char xstateregs[tdep->xsave_layout.sizeof_xsave];
335 struct iovec iov;
337 if (have_ptrace_getregset != TRIBOOL_TRUE)
338 return 0;
340 iov.iov_base = xstateregs;
341 iov.iov_len = sizeof(xstateregs);
342 if (ptrace (PTRACE_GETREGSET, tid, (unsigned int) NT_X86_XSTATE,
343 &iov) < 0)
344 perror_with_name (_("Couldn't read extended state status"));
346 i387_supply_xsave (regcache, -1, xstateregs);
347 return 1;
350 /* Store all valid registers in GDB's register array covered by the
351 PTRACE_SETREGSET request into the process/thread specified by TID.
352 Return non-zero if successful, zero otherwise. */
354 static int
355 store_xstateregs (const struct regcache *regcache, int tid, int regno)
357 struct gdbarch *gdbarch = regcache->arch ();
358 const i386_gdbarch_tdep *tdep = gdbarch_tdep<i386_gdbarch_tdep> (gdbarch);
359 char xstateregs[tdep->xsave_layout.sizeof_xsave];
360 struct iovec iov;
362 if (have_ptrace_getregset != TRIBOOL_TRUE)
363 return 0;
365 iov.iov_base = xstateregs;
366 iov.iov_len = sizeof(xstateregs);
367 if (ptrace (PTRACE_GETREGSET, tid, (unsigned int) NT_X86_XSTATE,
368 &iov) < 0)
369 perror_with_name (_("Couldn't read extended state status"));
371 i387_collect_xsave (regcache, regno, xstateregs, 0);
373 if (ptrace (PTRACE_SETREGSET, tid, (unsigned int) NT_X86_XSTATE,
374 (int) &iov) < 0)
375 perror_with_name (_("Couldn't write extended state status"));
377 return 1;
380 #ifdef HAVE_PTRACE_GETFPXREGS
382 /* Fetch all registers covered by the PTRACE_GETFPXREGS request from
383 process/thread TID and store their values in GDB's register array.
384 Return non-zero if successful, zero otherwise. */
386 static int
387 fetch_fpxregs (struct regcache *regcache, int tid)
389 elf_fpxregset_t fpxregs;
391 if (! have_ptrace_getfpxregs)
392 return 0;
394 if (ptrace (PTRACE_GETFPXREGS, tid, 0, (int) &fpxregs) < 0)
396 if (errno == EIO)
398 have_ptrace_getfpxregs = 0;
399 return 0;
402 perror_with_name (_("Couldn't read floating-point and SSE registers"));
405 i387_supply_fxsave (regcache, -1, (const elf_fpxregset_t *) &fpxregs);
406 return 1;
409 /* Store all valid registers in GDB's register array covered by the
410 PTRACE_SETFPXREGS request into the process/thread specified by TID.
411 Return non-zero if successful, zero otherwise. */
413 static int
414 store_fpxregs (const struct regcache *regcache, int tid, int regno)
416 elf_fpxregset_t fpxregs;
418 if (! have_ptrace_getfpxregs)
419 return 0;
421 if (ptrace (PTRACE_GETFPXREGS, tid, 0, &fpxregs) == -1)
423 if (errno == EIO)
425 have_ptrace_getfpxregs = 0;
426 return 0;
429 perror_with_name (_("Couldn't read floating-point and SSE registers"));
432 i387_collect_fxsave (regcache, regno, &fpxregs);
434 if (ptrace (PTRACE_SETFPXREGS, tid, 0, &fpxregs) == -1)
435 perror_with_name (_("Couldn't write floating-point and SSE registers"));
437 return 1;
440 #else
442 static int
443 fetch_fpxregs (struct regcache *regcache, int tid)
445 return 0;
448 static int
449 store_fpxregs (const struct regcache *regcache, int tid, int regno)
451 return 0;
454 #endif /* HAVE_PTRACE_GETFPXREGS */
457 /* Transferring arbitrary registers between GDB and inferior. */
459 /* Fetch register REGNO from the child process. If REGNO is -1, do
460 this for all registers (including the floating point and SSE
461 registers). */
463 void
464 i386_linux_nat_target::fetch_registers (struct regcache *regcache, int regno)
466 pid_t tid;
468 /* Use the old method of peeking around in `struct user' if the
469 GETREGS request isn't available. */
470 if (!have_ptrace_getregs)
472 int i;
474 for (i = 0; i < gdbarch_num_regs (regcache->arch ()); i++)
475 if (regno == -1 || regno == i)
476 fetch_register (regcache, i);
478 return;
481 tid = get_ptrace_pid (regcache->ptid ());
483 /* Use the PTRACE_GETFPXREGS request whenever possible, since it
484 transfers more registers in one system call, and we'll cache the
485 results. But remember that fetch_fpxregs can fail, and return
486 zero. */
487 if (regno == -1)
489 fetch_regs (regcache, tid);
491 /* The call above might reset `have_ptrace_getregs'. */
492 if (!have_ptrace_getregs)
494 fetch_registers (regcache, regno);
495 return;
498 if (fetch_xstateregs (regcache, tid))
499 return;
500 if (fetch_fpxregs (regcache, tid))
501 return;
502 fetch_fpregs (regcache, tid);
503 return;
506 if (GETREGS_SUPPLIES (regno))
508 fetch_regs (regcache, tid);
509 return;
512 if (GETXSTATEREGS_SUPPLIES (regno))
514 if (fetch_xstateregs (regcache, tid))
515 return;
518 if (GETFPXREGS_SUPPLIES (regno))
520 if (fetch_fpxregs (regcache, tid))
521 return;
523 /* Either our processor or our kernel doesn't support the SSE
524 registers, so read the FP registers in the traditional way,
525 and fill the SSE registers with dummy values. It would be
526 more graceful to handle differences in the register set using
527 gdbarch. Until then, this will at least make things work
528 plausibly. */
529 fetch_fpregs (regcache, tid);
530 return;
533 internal_error (_("Got request for bad register number %d."), regno);
536 /* Store register REGNO back into the child process. If REGNO is -1,
537 do this for all registers (including the floating point and SSE
538 registers). */
539 void
540 i386_linux_nat_target::store_registers (struct regcache *regcache, int regno)
542 pid_t tid;
544 /* Use the old method of poking around in `struct user' if the
545 SETREGS request isn't available. */
546 if (!have_ptrace_getregs)
548 int i;
550 for (i = 0; i < gdbarch_num_regs (regcache->arch ()); i++)
551 if (regno == -1 || regno == i)
552 store_register (regcache, i);
554 return;
557 tid = get_ptrace_pid (regcache->ptid ());
559 /* Use the PTRACE_SETFPXREGS requests whenever possible, since it
560 transfers more registers in one system call. But remember that
561 store_fpxregs can fail, and return zero. */
562 if (regno == -1)
564 store_regs (regcache, tid, regno);
565 if (store_xstateregs (regcache, tid, regno))
566 return;
567 if (store_fpxregs (regcache, tid, regno))
568 return;
569 store_fpregs (regcache, tid, regno);
570 return;
573 if (GETREGS_SUPPLIES (regno))
575 store_regs (regcache, tid, regno);
576 return;
579 if (GETXSTATEREGS_SUPPLIES (regno))
581 if (store_xstateregs (regcache, tid, regno))
582 return;
585 if (GETFPXREGS_SUPPLIES (regno))
587 if (store_fpxregs (regcache, tid, regno))
588 return;
590 /* Either our processor or our kernel doesn't support the SSE
591 registers, so just write the FP registers in the traditional
592 way. */
593 store_fpregs (regcache, tid, regno);
594 return;
597 internal_error (_("Got request to store bad register number %d."), regno);
601 /* Called by libthread_db. Returns a pointer to the thread local
602 storage (or its descriptor). */
604 ps_err_e
605 ps_get_thread_area (struct ps_prochandle *ph,
606 lwpid_t lwpid, int idx, void **base)
608 unsigned int base_addr;
609 ps_err_e result;
611 result = x86_linux_get_thread_area (lwpid, (void *) idx, &base_addr);
613 if (result == PS_OK)
614 *(int *) base = base_addr;
616 return result;
620 /* The instruction for a GNU/Linux system call is:
621 int $0x80
622 or 0xcd 0x80. */
624 static const unsigned char linux_syscall[] = { 0xcd, 0x80 };
626 #define LINUX_SYSCALL_LEN (sizeof linux_syscall)
628 /* The system call number is stored in the %eax register. */
629 #define LINUX_SYSCALL_REGNUM I386_EAX_REGNUM
631 /* We are specifically interested in the sigreturn and rt_sigreturn
632 system calls. */
634 #ifndef SYS_sigreturn
635 #define SYS_sigreturn 0x77
636 #endif
637 #ifndef SYS_rt_sigreturn
638 #define SYS_rt_sigreturn 0xad
639 #endif
641 /* Offset to saved processor flags, from <asm/sigcontext.h>. */
642 #define LINUX_SIGCONTEXT_EFLAGS_OFFSET (64)
644 /* Resume execution of the inferior process.
645 If STEP is nonzero, single-step it.
646 If SIGNAL is nonzero, give it that signal. */
648 void
649 i386_linux_nat_target::low_resume (ptid_t ptid, int step, enum gdb_signal signal)
651 int pid = ptid.lwp ();
652 int request;
654 if (catch_syscall_enabled ())
655 request = PTRACE_SYSCALL;
656 else
657 request = PTRACE_CONT;
659 if (step)
661 struct regcache *regcache = get_thread_regcache (this, ptid);
662 struct gdbarch *gdbarch = regcache->arch ();
663 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
664 ULONGEST pc;
665 gdb_byte buf[LINUX_SYSCALL_LEN];
667 request = PTRACE_SINGLESTEP;
669 regcache_cooked_read_unsigned (regcache,
670 gdbarch_pc_regnum (gdbarch), &pc);
672 /* Returning from a signal trampoline is done by calling a
673 special system call (sigreturn or rt_sigreturn, see
674 i386-linux-tdep.c for more information). This system call
675 restores the registers that were saved when the signal was
676 raised, including %eflags. That means that single-stepping
677 won't work. Instead, we'll have to modify the signal context
678 that's about to be restored, and set the trace flag there. */
680 /* First check if PC is at a system call. */
681 if (target_read_memory (pc, buf, LINUX_SYSCALL_LEN) == 0
682 && memcmp (buf, linux_syscall, LINUX_SYSCALL_LEN) == 0)
684 ULONGEST syscall;
685 regcache_cooked_read_unsigned (regcache,
686 LINUX_SYSCALL_REGNUM, &syscall);
688 /* Then check the system call number. */
689 if (syscall == SYS_sigreturn || syscall == SYS_rt_sigreturn)
691 ULONGEST sp, addr;
692 unsigned long int eflags;
694 regcache_cooked_read_unsigned (regcache, I386_ESP_REGNUM, &sp);
695 if (syscall == SYS_rt_sigreturn)
696 addr = read_memory_unsigned_integer (sp + 8, 4, byte_order)
697 + 20;
698 else
699 addr = sp;
701 /* Set the trace flag in the context that's about to be
702 restored. */
703 addr += LINUX_SIGCONTEXT_EFLAGS_OFFSET;
704 read_memory (addr, (gdb_byte *) &eflags, 4);
705 eflags |= 0x0100;
706 write_memory (addr, (gdb_byte *) &eflags, 4);
711 if (ptrace (request, pid, 0, gdb_signal_to_host (signal)) == -1)
712 perror_with_name (("ptrace"));
715 void _initialize_i386_linux_nat ();
716 void
717 _initialize_i386_linux_nat ()
719 linux_target = &the_i386_linux_nat_target;
721 /* Add the target. */
722 add_inf_child_target (linux_target);