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/>. */
23 #include "elf/common.h"
24 #include "nat/gdb_ptrace.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
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
101 /* Accessing registers through the U area, one at a time. */
103 /* Fetch one register. */
106 fetch_register (struct regcache
*regcache
, int regno
)
111 gdb_assert (!have_ptrace_getregs
);
112 if (i386_linux_gregset_reg_offset
[regno
] == -1)
114 regcache
->raw_supply (regno
, NULL
);
118 tid
= get_ptrace_pid (regcache
->ptid ());
121 val
= ptrace (PTRACE_PEEKUSER
, tid
,
122 i386_linux_gregset_reg_offset
[regno
], 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. */
134 store_register (const struct regcache
*regcache
, int regno
)
139 gdb_assert (!have_ptrace_getregs
);
140 if (i386_linux_gregset_reg_offset
[regno
] == -1)
143 tid
= get_ptrace_pid (regcache
->ptid ());
146 regcache
->raw_collect (regno
, &val
);
147 ptrace (PTRACE_POKEUSER
, tid
,
148 i386_linux_gregset_reg_offset
[regno
], val
);
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
159 /* Fill GDB's register array with the general-purpose register values
163 supply_gregset (struct regcache
*regcache
, const elf_gregset_t
*gregsetp
)
165 const gdb_byte
*regp
= (const gdb_byte
*) gregsetp
;
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 ()))
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. */
183 fill_gregset (const struct regcache
*regcache
,
184 elf_gregset_t
*gregsetp
, int regno
)
186 gdb_byte
*regp
= (gdb_byte
*) gregsetp
;
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. */
207 fetch_regs (struct regcache
*regcache
, int tid
)
210 elf_gregset_t
*regs_p
= ®s
;
212 if (ptrace (PTRACE_GETREGS
, tid
, 0, (int) ®s
) < 0)
216 /* The kernel we're running on doesn't support the GETREGS
217 request. Reset `have_ptrace_getregs'. */
218 have_ptrace_getregs
= 0;
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. */
232 store_regs (const struct regcache
*regcache
, int tid
, int regno
)
236 if (ptrace (PTRACE_GETREGS
, tid
, 0, (int) ®s
) < 0)
237 perror_with_name (_("Couldn't get registers"));
239 fill_gregset (regcache
, ®s
, regno
);
241 if (ptrace (PTRACE_SETREGS
, tid
, 0, (int) ®s
) < 0)
242 perror_with_name (_("Couldn't write registers"));
247 static void fetch_regs (struct regcache
*regcache
, int tid
) {}
248 static void store_regs (const struct regcache
*regcache
, int tid
, int regno
) {}
253 /* Transfering floating-point registers between GDB, inferiors and cores. */
255 /* Fill GDB's register array with the floating-point register values in
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. */
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. */
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. */
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"));
311 fetch_fpregs (struct regcache
*regcache
, int tid
)
316 store_fpregs (const struct regcache
*regcache
, int tid
, int regno
)
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. */
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
];
337 if (have_ptrace_getregset
!= TRIBOOL_TRUE
)
340 iov
.iov_base
= xstateregs
;
341 iov
.iov_len
= sizeof(xstateregs
);
342 if (ptrace (PTRACE_GETREGSET
, tid
, (unsigned int) NT_X86_XSTATE
,
344 perror_with_name (_("Couldn't read extended state status"));
346 i387_supply_xsave (regcache
, -1, xstateregs
);
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. */
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
];
362 if (have_ptrace_getregset
!= TRIBOOL_TRUE
)
365 iov
.iov_base
= xstateregs
;
366 iov
.iov_len
= sizeof(xstateregs
);
367 if (ptrace (PTRACE_GETREGSET
, tid
, (unsigned int) NT_X86_XSTATE
,
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
,
375 perror_with_name (_("Couldn't write extended state status"));
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. */
387 fetch_fpxregs (struct regcache
*regcache
, int tid
)
389 elf_fpxregset_t fpxregs
;
391 if (! have_ptrace_getfpxregs
)
394 if (ptrace (PTRACE_GETFPXREGS
, tid
, 0, (int) &fpxregs
) < 0)
398 have_ptrace_getfpxregs
= 0;
402 perror_with_name (_("Couldn't read floating-point and SSE registers"));
405 i387_supply_fxsave (regcache
, -1, (const elf_fpxregset_t
*) &fpxregs
);
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. */
414 store_fpxregs (const struct regcache
*regcache
, int tid
, int regno
)
416 elf_fpxregset_t fpxregs
;
418 if (! have_ptrace_getfpxregs
)
421 if (ptrace (PTRACE_GETFPXREGS
, tid
, 0, &fpxregs
) == -1)
425 have_ptrace_getfpxregs
= 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"));
443 fetch_fpxregs (struct regcache
*regcache
, int tid
)
449 store_fpxregs (const struct regcache
*regcache
, int tid
, int regno
)
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
464 i386_linux_nat_target::fetch_registers (struct regcache
*regcache
, int regno
)
468 /* Use the old method of peeking around in `struct user' if the
469 GETREGS request isn't available. */
470 if (!have_ptrace_getregs
)
474 for (i
= 0; i
< gdbarch_num_regs (regcache
->arch ()); i
++)
475 if (regno
== -1 || regno
== i
)
476 fetch_register (regcache
, i
);
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
489 fetch_regs (regcache
, tid
);
491 /* The call above might reset `have_ptrace_getregs'. */
492 if (!have_ptrace_getregs
)
494 fetch_registers (regcache
, regno
);
498 if (fetch_xstateregs (regcache
, tid
))
500 if (fetch_fpxregs (regcache
, tid
))
502 fetch_fpregs (regcache
, tid
);
506 if (GETREGS_SUPPLIES (regno
))
508 fetch_regs (regcache
, tid
);
512 if (GETXSTATEREGS_SUPPLIES (regno
))
514 if (fetch_xstateregs (regcache
, tid
))
518 if (GETFPXREGS_SUPPLIES (regno
))
520 if (fetch_fpxregs (regcache
, tid
))
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
529 fetch_fpregs (regcache
, tid
);
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
540 i386_linux_nat_target::store_registers (struct regcache
*regcache
, int regno
)
544 /* Use the old method of poking around in `struct user' if the
545 SETREGS request isn't available. */
546 if (!have_ptrace_getregs
)
550 for (i
= 0; i
< gdbarch_num_regs (regcache
->arch ()); i
++)
551 if (regno
== -1 || regno
== i
)
552 store_register (regcache
, i
);
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. */
564 store_regs (regcache
, tid
, regno
);
565 if (store_xstateregs (regcache
, tid
, regno
))
567 if (store_fpxregs (regcache
, tid
, regno
))
569 store_fpregs (regcache
, tid
, regno
);
573 if (GETREGS_SUPPLIES (regno
))
575 store_regs (regcache
, tid
, regno
);
579 if (GETXSTATEREGS_SUPPLIES (regno
))
581 if (store_xstateregs (regcache
, tid
, regno
))
585 if (GETFPXREGS_SUPPLIES (regno
))
587 if (store_fpxregs (regcache
, tid
, regno
))
590 /* Either our processor or our kernel doesn't support the SSE
591 registers, so just write the FP registers in the traditional
593 store_fpregs (regcache
, tid
, regno
);
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). */
605 ps_get_thread_area (struct ps_prochandle
*ph
,
606 lwpid_t lwpid
, int idx
, void **base
)
608 unsigned int base_addr
;
611 result
= x86_linux_get_thread_area (lwpid
, (void *) idx
, &base_addr
);
614 *(int *) base
= base_addr
;
620 /* The instruction for a GNU/Linux system call is:
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
634 #ifndef SYS_sigreturn
635 #define SYS_sigreturn 0x77
637 #ifndef SYS_rt_sigreturn
638 #define SYS_rt_sigreturn 0xad
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. */
649 i386_linux_nat_target::low_resume (ptid_t ptid
, int step
, enum gdb_signal signal
)
651 int pid
= ptid
.lwp ();
654 if (catch_syscall_enabled ())
655 request
= PTRACE_SYSCALL
;
657 request
= PTRACE_CONT
;
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
);
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)
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
)
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
)
701 /* Set the trace flag in the context that's about to be
703 addr
+= LINUX_SIGCONTEXT_EFLAGS_OFFSET
;
704 read_memory (addr
, (gdb_byte
*) &eflags
, 4);
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 ();
717 _initialize_i386_linux_nat ()
719 linux_target
= &the_i386_linux_nat_target
;
721 /* Add the target. */
722 add_inf_child_target (linux_target
);