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 "nat/i386-linux.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
85 /* Accessing registers through the U area, one at a time. */
87 /* Fetch one register. */
90 fetch_register (struct regcache
*regcache
, int regno
)
95 gdb_assert (!have_ptrace_getregs
);
96 if (i386_linux_gregset_reg_offset
[regno
] == -1)
98 regcache
->raw_supply (regno
, NULL
);
102 tid
= get_ptrace_pid (regcache
->ptid ());
105 val
= ptrace (PTRACE_PEEKUSER
, tid
,
106 i386_linux_gregset_reg_offset
[regno
], 0);
108 error (_("Couldn't read register %s (#%d): %s."),
109 gdbarch_register_name (regcache
->arch (), regno
),
110 regno
, safe_strerror (errno
));
112 regcache
->raw_supply (regno
, &val
);
115 /* Store one register. */
118 store_register (const struct regcache
*regcache
, int regno
)
123 gdb_assert (!have_ptrace_getregs
);
124 if (i386_linux_gregset_reg_offset
[regno
] == -1)
127 tid
= get_ptrace_pid (regcache
->ptid ());
130 regcache
->raw_collect (regno
, &val
);
131 ptrace (PTRACE_POKEUSER
, tid
,
132 i386_linux_gregset_reg_offset
[regno
], val
);
134 error (_("Couldn't write register %s (#%d): %s."),
135 gdbarch_register_name (regcache
->arch (), regno
),
136 regno
, safe_strerror (errno
));
140 /* Transferring the general-purpose registers between GDB, inferiors
143 /* Fill GDB's register array with the general-purpose register values
147 supply_gregset (struct regcache
*regcache
, const elf_gregset_t
*gregsetp
)
149 const gdb_byte
*regp
= (const gdb_byte
*) gregsetp
;
152 for (i
= 0; i
< I386_NUM_GREGS
; i
++)
153 regcache
->raw_supply (i
, regp
+ i386_linux_gregset_reg_offset
[i
]);
155 if (I386_LINUX_ORIG_EAX_REGNUM
156 < gdbarch_num_regs (regcache
->arch ()))
158 (I386_LINUX_ORIG_EAX_REGNUM
,
159 regp
+ i386_linux_gregset_reg_offset
[I386_LINUX_ORIG_EAX_REGNUM
]);
162 /* Fill register REGNO (if it is a general-purpose register) in
163 *GREGSETPS with the value in GDB's register array. If REGNO is -1,
164 do this for all registers. */
167 fill_gregset (const struct regcache
*regcache
,
168 elf_gregset_t
*gregsetp
, int regno
)
170 gdb_byte
*regp
= (gdb_byte
*) gregsetp
;
173 for (i
= 0; i
< I386_NUM_GREGS
; i
++)
174 if (regno
== -1 || regno
== i
)
175 regcache
->raw_collect (i
, regp
+ i386_linux_gregset_reg_offset
[i
]);
177 if ((regno
== -1 || regno
== I386_LINUX_ORIG_EAX_REGNUM
)
178 && I386_LINUX_ORIG_EAX_REGNUM
179 < gdbarch_num_regs (regcache
->arch ()))
180 regcache
->raw_collect
181 (I386_LINUX_ORIG_EAX_REGNUM
,
182 regp
+ i386_linux_gregset_reg_offset
[I386_LINUX_ORIG_EAX_REGNUM
]);
185 #ifdef HAVE_PTRACE_GETREGS
187 /* Fetch all general-purpose registers from process/thread TID and
188 store their values in GDB's register array. */
191 fetch_regs (struct regcache
*regcache
, int tid
)
194 elf_gregset_t
*regs_p
= ®s
;
196 if (ptrace (PTRACE_GETREGS
, tid
, 0, (int) ®s
) < 0)
200 /* The kernel we're running on doesn't support the GETREGS
201 request. Reset `have_ptrace_getregs'. */
202 have_ptrace_getregs
= 0;
206 perror_with_name (_("Couldn't get registers"));
209 supply_gregset (regcache
, (const elf_gregset_t
*) regs_p
);
212 /* Store all valid general-purpose registers in GDB's register array
213 into the process/thread specified by TID. */
216 store_regs (const struct regcache
*regcache
, int tid
, int regno
)
220 if (ptrace (PTRACE_GETREGS
, tid
, 0, (int) ®s
) < 0)
221 perror_with_name (_("Couldn't get registers"));
223 fill_gregset (regcache
, ®s
, regno
);
225 if (ptrace (PTRACE_SETREGS
, tid
, 0, (int) ®s
) < 0)
226 perror_with_name (_("Couldn't write registers"));
231 static void fetch_regs (struct regcache
*regcache
, int tid
) {}
232 static void store_regs (const struct regcache
*regcache
, int tid
, int regno
) {}
237 /* Transferring floating-point registers between GDB, inferiors and cores. */
239 /* Fill GDB's register array with the floating-point register values in
243 supply_fpregset (struct regcache
*regcache
, const elf_fpregset_t
*fpregsetp
)
245 i387_supply_fsave (regcache
, -1, fpregsetp
);
248 /* Fill register REGNO (if it is a floating-point register) in
249 *FPREGSETP with the value in GDB's register array. If REGNO is -1,
250 do this for all registers. */
253 fill_fpregset (const struct regcache
*regcache
,
254 elf_fpregset_t
*fpregsetp
, int regno
)
256 i387_collect_fsave (regcache
, regno
, fpregsetp
);
259 #ifdef HAVE_PTRACE_GETREGS
261 /* Fetch all floating-point registers from process/thread TID and store
262 their values in GDB's register array. */
265 fetch_fpregs (struct regcache
*regcache
, int tid
)
267 elf_fpregset_t fpregs
;
269 if (ptrace (PTRACE_GETFPREGS
, tid
, 0, (int) &fpregs
) < 0)
270 perror_with_name (_("Couldn't get floating point status"));
272 supply_fpregset (regcache
, (const elf_fpregset_t
*) &fpregs
);
275 /* Store all valid floating-point registers in GDB's register array
276 into the process/thread specified by TID. */
279 store_fpregs (const struct regcache
*regcache
, int tid
, int regno
)
281 elf_fpregset_t fpregs
;
283 if (ptrace (PTRACE_GETFPREGS
, tid
, 0, (int) &fpregs
) < 0)
284 perror_with_name (_("Couldn't get floating point status"));
286 fill_fpregset (regcache
, &fpregs
, regno
);
288 if (ptrace (PTRACE_SETFPREGS
, tid
, 0, (int) &fpregs
) < 0)
289 perror_with_name (_("Couldn't write floating point status"));
295 fetch_fpregs (struct regcache
*regcache
, int tid
)
300 store_fpregs (const struct regcache
*regcache
, int tid
, int regno
)
307 /* Transferring floating-point and SSE registers to and from GDB. */
309 /* Fetch all registers covered by the PTRACE_GETREGSET request from
310 process/thread TID and store their values in GDB's register array.
311 Return non-zero if successful, zero otherwise. */
314 fetch_xstateregs (struct regcache
*regcache
, int tid
)
316 struct gdbarch
*gdbarch
= regcache
->arch ();
317 const i386_gdbarch_tdep
*tdep
= gdbarch_tdep
<i386_gdbarch_tdep
> (gdbarch
);
318 gdb::byte_vector
xstateregs (tdep
->xsave_layout
.sizeof_xsave
);
321 if (have_ptrace_getregset
!= TRIBOOL_TRUE
)
324 iov
.iov_base
= xstateregs
.data ();
325 iov
.iov_len
= xstateregs
.size ();
326 if (ptrace (PTRACE_GETREGSET
, tid
, (unsigned int) NT_X86_XSTATE
,
328 perror_with_name (_("Couldn't read extended state status"));
330 i387_supply_xsave (regcache
, -1, xstateregs
.data ());
334 /* Store all valid registers in GDB's register array covered by the
335 PTRACE_SETREGSET request into the process/thread specified by TID.
336 Return non-zero if successful, zero otherwise. */
339 store_xstateregs (const struct regcache
*regcache
, int tid
, int regno
)
341 struct gdbarch
*gdbarch
= regcache
->arch ();
342 const i386_gdbarch_tdep
*tdep
= gdbarch_tdep
<i386_gdbarch_tdep
> (gdbarch
);
343 gdb::byte_vector
xstateregs (tdep
->xsave_layout
.sizeof_xsave
);
346 if (have_ptrace_getregset
!= TRIBOOL_TRUE
)
349 iov
.iov_base
= xstateregs
.data ();
350 iov
.iov_len
= xstateregs
.size ();
351 if (ptrace (PTRACE_GETREGSET
, tid
, (unsigned int) NT_X86_XSTATE
,
353 perror_with_name (_("Couldn't read extended state status"));
355 i387_collect_xsave (regcache
, regno
, xstateregs
.data (), 0);
357 if (ptrace (PTRACE_SETREGSET
, tid
, (unsigned int) NT_X86_XSTATE
,
359 perror_with_name (_("Couldn't write extended state status"));
364 #ifdef HAVE_PTRACE_GETFPXREGS
366 /* Fetch all registers covered by the PTRACE_GETFPXREGS request from
367 process/thread TID and store their values in GDB's register array.
368 Return non-zero if successful, zero otherwise. */
371 fetch_fpxregs (struct regcache
*regcache
, int tid
)
373 elf_fpxregset_t fpxregs
;
375 if (have_ptrace_getfpxregs
== TRIBOOL_FALSE
)
378 if (ptrace (PTRACE_GETFPXREGS
, tid
, 0, (int) &fpxregs
) < 0)
382 have_ptrace_getfpxregs
= TRIBOOL_FALSE
;
386 perror_with_name (_("Couldn't read floating-point and SSE registers"));
389 i387_supply_fxsave (regcache
, -1, (const elf_fpxregset_t
*) &fpxregs
);
393 /* Store all valid registers in GDB's register array covered by the
394 PTRACE_SETFPXREGS request into the process/thread specified by TID.
395 Return non-zero if successful, zero otherwise. */
398 store_fpxregs (const struct regcache
*regcache
, int tid
, int regno
)
400 elf_fpxregset_t fpxregs
;
402 if (have_ptrace_getfpxregs
== TRIBOOL_FALSE
)
405 if (ptrace (PTRACE_GETFPXREGS
, tid
, 0, &fpxregs
) == -1)
409 have_ptrace_getfpxregs
= TRIBOOL_FALSE
;
413 perror_with_name (_("Couldn't read floating-point and SSE registers"));
416 i387_collect_fxsave (regcache
, regno
, &fpxregs
);
418 if (ptrace (PTRACE_SETFPXREGS
, tid
, 0, &fpxregs
) == -1)
419 perror_with_name (_("Couldn't write floating-point and SSE registers"));
427 fetch_fpxregs (struct regcache
*regcache
, int tid
)
433 store_fpxregs (const struct regcache
*regcache
, int tid
, int regno
)
438 #endif /* HAVE_PTRACE_GETFPXREGS */
441 /* Transferring arbitrary registers between GDB and inferior. */
443 /* Fetch register REGNO from the child process. If REGNO is -1, do
444 this for all registers (including the floating point and SSE
448 i386_linux_nat_target::fetch_registers (struct regcache
*regcache
, int regno
)
452 /* Use the old method of peeking around in `struct user' if the
453 GETREGS request isn't available. */
454 if (!have_ptrace_getregs
)
458 for (i
= 0; i
< gdbarch_num_regs (regcache
->arch ()); i
++)
459 if (regno
== -1 || regno
== i
)
460 fetch_register (regcache
, i
);
465 tid
= get_ptrace_pid (regcache
->ptid ());
467 /* Use the PTRACE_GETFPXREGS request whenever possible, since it
468 transfers more registers in one system call, and we'll cache the
469 results. But remember that fetch_fpxregs can fail, and return
473 fetch_regs (regcache
, tid
);
475 /* The call above might reset `have_ptrace_getregs'. */
476 if (!have_ptrace_getregs
)
478 fetch_registers (regcache
, regno
);
482 if (fetch_xstateregs (regcache
, tid
))
484 if (fetch_fpxregs (regcache
, tid
))
486 fetch_fpregs (regcache
, tid
);
490 if (GETREGS_SUPPLIES (regno
))
492 fetch_regs (regcache
, tid
);
496 if (GETXSTATEREGS_SUPPLIES (regno
))
498 if (fetch_xstateregs (regcache
, tid
))
502 if (GETFPXREGS_SUPPLIES (regno
))
504 if (fetch_fpxregs (regcache
, tid
))
507 /* Either our processor or our kernel doesn't support the SSE
508 registers, so read the FP registers in the traditional way,
509 and fill the SSE registers with dummy values. It would be
510 more graceful to handle differences in the register set using
511 gdbarch. Until then, this will at least make things work
513 fetch_fpregs (regcache
, tid
);
517 internal_error (_("Got request for bad register number %d."), regno
);
520 /* Store register REGNO back into the child process. If REGNO is -1,
521 do this for all registers (including the floating point and SSE
524 i386_linux_nat_target::store_registers (struct regcache
*regcache
, int regno
)
528 /* Use the old method of poking around in `struct user' if the
529 SETREGS request isn't available. */
530 if (!have_ptrace_getregs
)
534 for (i
= 0; i
< gdbarch_num_regs (regcache
->arch ()); i
++)
535 if (regno
== -1 || regno
== i
)
536 store_register (regcache
, i
);
541 tid
= get_ptrace_pid (regcache
->ptid ());
543 /* Use the PTRACE_SETFPXREGS requests whenever possible, since it
544 transfers more registers in one system call. But remember that
545 store_fpxregs can fail, and return zero. */
548 store_regs (regcache
, tid
, regno
);
549 if (store_xstateregs (regcache
, tid
, regno
))
551 if (store_fpxregs (regcache
, tid
, regno
))
553 store_fpregs (regcache
, tid
, regno
);
557 if (GETREGS_SUPPLIES (regno
))
559 store_regs (regcache
, tid
, regno
);
563 if (GETXSTATEREGS_SUPPLIES (regno
))
565 if (store_xstateregs (regcache
, tid
, regno
))
569 if (GETFPXREGS_SUPPLIES (regno
))
571 if (store_fpxregs (regcache
, tid
, regno
))
574 /* Either our processor or our kernel doesn't support the SSE
575 registers, so just write the FP registers in the traditional
577 store_fpregs (regcache
, tid
, regno
);
581 internal_error (_("Got request to store bad register number %d."), regno
);
585 /* Called by libthread_db. Returns a pointer to the thread local
586 storage (or its descriptor). */
589 ps_get_thread_area (struct ps_prochandle
*ph
,
590 lwpid_t lwpid
, int idx
, void **base
)
592 unsigned int base_addr
;
595 result
= x86_linux_get_thread_area (lwpid
, (void *) idx
, &base_addr
);
598 *(int *) base
= base_addr
;
604 /* The instruction for a GNU/Linux system call is:
608 static const unsigned char linux_syscall
[] = { 0xcd, 0x80 };
610 #define LINUX_SYSCALL_LEN (sizeof linux_syscall)
612 /* The system call number is stored in the %eax register. */
613 #define LINUX_SYSCALL_REGNUM I386_EAX_REGNUM
615 /* We are specifically interested in the sigreturn and rt_sigreturn
618 #ifndef SYS_sigreturn
619 #define SYS_sigreturn 0x77
621 #ifndef SYS_rt_sigreturn
622 #define SYS_rt_sigreturn 0xad
625 /* Offset to saved processor flags, from <asm/sigcontext.h>. */
626 #define LINUX_SIGCONTEXT_EFLAGS_OFFSET (64)
628 /* Resume execution of the inferior process.
629 If STEP is nonzero, single-step it.
630 If SIGNAL is nonzero, give it that signal. */
633 i386_linux_nat_target::low_resume (ptid_t ptid
, int step
, enum gdb_signal signal
)
635 int pid
= ptid
.lwp ();
638 if (catch_syscall_enabled ())
639 request
= PTRACE_SYSCALL
;
641 request
= PTRACE_CONT
;
645 struct regcache
*regcache
= get_thread_regcache (this, ptid
);
646 struct gdbarch
*gdbarch
= regcache
->arch ();
647 enum bfd_endian byte_order
= gdbarch_byte_order (gdbarch
);
649 gdb_byte buf
[LINUX_SYSCALL_LEN
];
651 request
= PTRACE_SINGLESTEP
;
653 regcache_cooked_read_unsigned (regcache
,
654 gdbarch_pc_regnum (gdbarch
), &pc
);
656 /* Returning from a signal trampoline is done by calling a
657 special system call (sigreturn or rt_sigreturn, see
658 i386-linux-tdep.c for more information). This system call
659 restores the registers that were saved when the signal was
660 raised, including %eflags. That means that single-stepping
661 won't work. Instead, we'll have to modify the signal context
662 that's about to be restored, and set the trace flag there. */
664 /* First check if PC is at a system call. */
665 if (target_read_memory (pc
, buf
, LINUX_SYSCALL_LEN
) == 0
666 && memcmp (buf
, linux_syscall
, LINUX_SYSCALL_LEN
) == 0)
669 regcache_cooked_read_unsigned (regcache
,
670 LINUX_SYSCALL_REGNUM
, &syscall
);
672 /* Then check the system call number. */
673 if (syscall
== SYS_sigreturn
|| syscall
== SYS_rt_sigreturn
)
676 unsigned long int eflags
;
678 regcache_cooked_read_unsigned (regcache
, I386_ESP_REGNUM
, &sp
);
679 if (syscall
== SYS_rt_sigreturn
)
680 addr
= read_memory_unsigned_integer (sp
+ 8, 4, byte_order
)
685 /* Set the trace flag in the context that's about to be
687 addr
+= LINUX_SIGCONTEXT_EFLAGS_OFFSET
;
688 read_memory (addr
, (gdb_byte
*) &eflags
, 4);
690 write_memory (addr
, (gdb_byte
*) &eflags
, 4);
695 if (ptrace (request
, pid
, 0, gdb_signal_to_host (signal
)) == -1)
696 perror_with_name (("ptrace"));
699 void _initialize_i386_linux_nat ();
701 _initialize_i386_linux_nat ()
703 linux_target
= &the_i386_linux_nat_target
;
705 /* Add the target. */
706 add_inf_child_target (linux_target
);