1 /* Native-dependent code for GNU/Linux i386.
3 Copyright (C) 1999-2020 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/>. */
24 #include "elf/common.h"
25 #include "nat/gdb_ptrace.h"
28 #include "gdb_proc_service.h"
30 #include "i386-linux-nat.h"
31 #include "i387-tdep.h"
32 #include "i386-tdep.h"
33 #include "i386-linux-tdep.h"
34 #include "gdbsupport/x86-xstate.h"
36 #include "x86-linux-nat.h"
37 #include "nat/linux-ptrace.h"
38 #include "inf-ptrace.h"
40 struct i386_linux_nat_target final
: public x86_linux_nat_target
42 /* Add our register access methods. */
43 void fetch_registers (struct regcache
*, int) override
;
44 void store_registers (struct regcache
*, int) override
;
46 /* Override the default ptrace resume method. */
47 void low_resume (ptid_t ptid
, int step
, enum gdb_signal sig
) override
;
50 static i386_linux_nat_target the_i386_linux_nat_target
;
52 /* The register sets used in GNU/Linux ELF core-dumps are identical to
53 the register sets in `struct user' that is used for a.out
54 core-dumps, and is also used by `ptrace'. The corresponding types
55 are `elf_gregset_t' for the general-purpose registers (with
56 `elf_greg_t' the type of a single GP register) and `elf_fpregset_t'
57 for the floating-point registers.
59 Those types used to be available under the names `gregset_t' and
60 `fpregset_t' too, and this file used those names in the past. But
61 those names are now used for the register sets used in the
62 `mcontext_t' type, and have a different size and layout. */
64 /* Which ptrace request retrieves which registers?
65 These apply to the corresponding SET requests as well. */
67 #define GETREGS_SUPPLIES(regno) \
68 ((0 <= (regno) && (regno) <= 15) || (regno) == I386_LINUX_ORIG_EAX_REGNUM)
70 #define GETFPXREGS_SUPPLIES(regno) \
71 (I386_ST0_REGNUM <= (regno) && (regno) < I386_SSE_NUM_REGS)
73 #define GETXSTATEREGS_SUPPLIES(regno) \
74 (I386_ST0_REGNUM <= (regno) && (regno) < I386_PKEYS_NUM_REGS)
76 /* Does the current host support the GETREGS request? */
77 int have_ptrace_getregs
=
78 #ifdef HAVE_PTRACE_GETREGS
85 /* Does the current host support the GETFPXREGS request? The header
86 file may or may not define it, and even if it is defined, the
87 kernel will return EIO if it's running on a pre-SSE processor.
89 My instinct is to attach this to some architecture- or
90 target-specific data structure, but really, a particular GDB
91 process can only run on top of one kernel at a time. So it's okay
92 for this to be a simple variable. */
93 int have_ptrace_getfpxregs
=
94 #ifdef HAVE_PTRACE_GETFPXREGS
102 /* Accessing registers through the U area, one at a time. */
104 /* Fetch one register. */
107 fetch_register (struct regcache
*regcache
, int regno
)
112 gdb_assert (!have_ptrace_getregs
);
113 if (i386_linux_gregset_reg_offset
[regno
] == -1)
115 regcache
->raw_supply (regno
, NULL
);
119 tid
= get_ptrace_pid (regcache
->ptid ());
122 val
= ptrace (PTRACE_PEEKUSER
, tid
,
123 i386_linux_gregset_reg_offset
[regno
], 0);
125 error (_("Couldn't read register %s (#%d): %s."),
126 gdbarch_register_name (regcache
->arch (), regno
),
127 regno
, safe_strerror (errno
));
129 regcache
->raw_supply (regno
, &val
);
132 /* Store one register. */
135 store_register (const struct regcache
*regcache
, int regno
)
140 gdb_assert (!have_ptrace_getregs
);
141 if (i386_linux_gregset_reg_offset
[regno
] == -1)
144 tid
= get_ptrace_pid (regcache
->ptid ());
147 regcache
->raw_collect (regno
, &val
);
148 ptrace (PTRACE_POKEUSER
, tid
,
149 i386_linux_gregset_reg_offset
[regno
], val
);
151 error (_("Couldn't write register %s (#%d): %s."),
152 gdbarch_register_name (regcache
->arch (), regno
),
153 regno
, safe_strerror (errno
));
157 /* Transfering the general-purpose registers between GDB, inferiors
160 /* Fill GDB's register array with the general-purpose register values
164 supply_gregset (struct regcache
*regcache
, const elf_gregset_t
*gregsetp
)
166 const gdb_byte
*regp
= (const gdb_byte
*) gregsetp
;
169 for (i
= 0; i
< I386_NUM_GREGS
; i
++)
170 regcache
->raw_supply (i
, regp
+ i386_linux_gregset_reg_offset
[i
]);
172 if (I386_LINUX_ORIG_EAX_REGNUM
173 < gdbarch_num_regs (regcache
->arch ()))
175 (I386_LINUX_ORIG_EAX_REGNUM
,
176 regp
+ i386_linux_gregset_reg_offset
[I386_LINUX_ORIG_EAX_REGNUM
]);
179 /* Fill register REGNO (if it is a general-purpose register) in
180 *GREGSETPS with the value in GDB's register array. If REGNO is -1,
181 do this for all registers. */
184 fill_gregset (const struct regcache
*regcache
,
185 elf_gregset_t
*gregsetp
, int regno
)
187 gdb_byte
*regp
= (gdb_byte
*) gregsetp
;
190 for (i
= 0; i
< I386_NUM_GREGS
; i
++)
191 if (regno
== -1 || regno
== i
)
192 regcache
->raw_collect (i
, regp
+ i386_linux_gregset_reg_offset
[i
]);
194 if ((regno
== -1 || regno
== I386_LINUX_ORIG_EAX_REGNUM
)
195 && I386_LINUX_ORIG_EAX_REGNUM
196 < gdbarch_num_regs (regcache
->arch ()))
197 regcache
->raw_collect
198 (I386_LINUX_ORIG_EAX_REGNUM
,
199 regp
+ i386_linux_gregset_reg_offset
[I386_LINUX_ORIG_EAX_REGNUM
]);
202 #ifdef HAVE_PTRACE_GETREGS
204 /* Fetch all general-purpose registers from process/thread TID and
205 store their values in GDB's register array. */
208 fetch_regs (struct regcache
*regcache
, int tid
)
211 elf_gregset_t
*regs_p
= ®s
;
213 if (ptrace (PTRACE_GETREGS
, tid
, 0, (int) ®s
) < 0)
217 /* The kernel we're running on doesn't support the GETREGS
218 request. Reset `have_ptrace_getregs'. */
219 have_ptrace_getregs
= 0;
223 perror_with_name (_("Couldn't get registers"));
226 supply_gregset (regcache
, (const elf_gregset_t
*) regs_p
);
229 /* Store all valid general-purpose registers in GDB's register array
230 into the process/thread specified by TID. */
233 store_regs (const struct regcache
*regcache
, int tid
, int regno
)
237 if (ptrace (PTRACE_GETREGS
, tid
, 0, (int) ®s
) < 0)
238 perror_with_name (_("Couldn't get registers"));
240 fill_gregset (regcache
, ®s
, regno
);
242 if (ptrace (PTRACE_SETREGS
, tid
, 0, (int) ®s
) < 0)
243 perror_with_name (_("Couldn't write registers"));
248 static void fetch_regs (struct regcache
*regcache
, int tid
) {}
249 static void store_regs (const struct regcache
*regcache
, int tid
, int regno
) {}
254 /* Transfering floating-point registers between GDB, inferiors and cores. */
256 /* Fill GDB's register array with the floating-point register values in
260 supply_fpregset (struct regcache
*regcache
, const elf_fpregset_t
*fpregsetp
)
262 i387_supply_fsave (regcache
, -1, fpregsetp
);
265 /* Fill register REGNO (if it is a floating-point register) in
266 *FPREGSETP with the value in GDB's register array. If REGNO is -1,
267 do this for all registers. */
270 fill_fpregset (const struct regcache
*regcache
,
271 elf_fpregset_t
*fpregsetp
, int regno
)
273 i387_collect_fsave (regcache
, regno
, fpregsetp
);
276 #ifdef HAVE_PTRACE_GETREGS
278 /* Fetch all floating-point registers from process/thread TID and store
279 thier values in GDB's register array. */
282 fetch_fpregs (struct regcache
*regcache
, int tid
)
284 elf_fpregset_t fpregs
;
286 if (ptrace (PTRACE_GETFPREGS
, tid
, 0, (int) &fpregs
) < 0)
287 perror_with_name (_("Couldn't get floating point status"));
289 supply_fpregset (regcache
, (const elf_fpregset_t
*) &fpregs
);
292 /* Store all valid floating-point registers in GDB's register array
293 into the process/thread specified by TID. */
296 store_fpregs (const struct regcache
*regcache
, int tid
, int regno
)
298 elf_fpregset_t fpregs
;
300 if (ptrace (PTRACE_GETFPREGS
, tid
, 0, (int) &fpregs
) < 0)
301 perror_with_name (_("Couldn't get floating point status"));
303 fill_fpregset (regcache
, &fpregs
, regno
);
305 if (ptrace (PTRACE_SETFPREGS
, tid
, 0, (int) &fpregs
) < 0)
306 perror_with_name (_("Couldn't write floating point status"));
312 fetch_fpregs (struct regcache
*regcache
, int tid
)
317 store_fpregs (const struct regcache
*regcache
, int tid
, int regno
)
324 /* Transfering floating-point and SSE registers to and from GDB. */
326 /* Fetch all registers covered by the PTRACE_GETREGSET request from
327 process/thread TID and store their values in GDB's register array.
328 Return non-zero if successful, zero otherwise. */
331 fetch_xstateregs (struct regcache
*regcache
, int tid
)
333 char xstateregs
[X86_XSTATE_MAX_SIZE
];
336 if (have_ptrace_getregset
!= TRIBOOL_TRUE
)
339 iov
.iov_base
= xstateregs
;
340 iov
.iov_len
= sizeof(xstateregs
);
341 if (ptrace (PTRACE_GETREGSET
, tid
, (unsigned int) NT_X86_XSTATE
,
343 perror_with_name (_("Couldn't read extended state status"));
345 i387_supply_xsave (regcache
, -1, xstateregs
);
349 /* Store all valid registers in GDB's register array covered by the
350 PTRACE_SETREGSET request into the process/thread specified by TID.
351 Return non-zero if successful, zero otherwise. */
354 store_xstateregs (const struct regcache
*regcache
, int tid
, int regno
)
356 char xstateregs
[X86_XSTATE_MAX_SIZE
];
359 if (have_ptrace_getregset
!= TRIBOOL_TRUE
)
362 iov
.iov_base
= xstateregs
;
363 iov
.iov_len
= sizeof(xstateregs
);
364 if (ptrace (PTRACE_GETREGSET
, tid
, (unsigned int) NT_X86_XSTATE
,
366 perror_with_name (_("Couldn't read extended state status"));
368 i387_collect_xsave (regcache
, regno
, xstateregs
, 0);
370 if (ptrace (PTRACE_SETREGSET
, tid
, (unsigned int) NT_X86_XSTATE
,
372 perror_with_name (_("Couldn't write extended state status"));
377 #ifdef HAVE_PTRACE_GETFPXREGS
379 /* Fetch all registers covered by the PTRACE_GETFPXREGS request from
380 process/thread TID and store their values in GDB's register array.
381 Return non-zero if successful, zero otherwise. */
384 fetch_fpxregs (struct regcache
*regcache
, int tid
)
386 elf_fpxregset_t fpxregs
;
388 if (! have_ptrace_getfpxregs
)
391 if (ptrace (PTRACE_GETFPXREGS
, tid
, 0, (int) &fpxregs
) < 0)
395 have_ptrace_getfpxregs
= 0;
399 perror_with_name (_("Couldn't read floating-point and SSE registers"));
402 i387_supply_fxsave (regcache
, -1, (const elf_fpxregset_t
*) &fpxregs
);
406 /* Store all valid registers in GDB's register array covered by the
407 PTRACE_SETFPXREGS request into the process/thread specified by TID.
408 Return non-zero if successful, zero otherwise. */
411 store_fpxregs (const struct regcache
*regcache
, int tid
, int regno
)
413 elf_fpxregset_t fpxregs
;
415 if (! have_ptrace_getfpxregs
)
418 if (ptrace (PTRACE_GETFPXREGS
, tid
, 0, &fpxregs
) == -1)
422 have_ptrace_getfpxregs
= 0;
426 perror_with_name (_("Couldn't read floating-point and SSE registers"));
429 i387_collect_fxsave (regcache
, regno
, &fpxregs
);
431 if (ptrace (PTRACE_SETFPXREGS
, tid
, 0, &fpxregs
) == -1)
432 perror_with_name (_("Couldn't write floating-point and SSE registers"));
440 fetch_fpxregs (struct regcache
*regcache
, int tid
)
446 store_fpxregs (const struct regcache
*regcache
, int tid
, int regno
)
451 #endif /* HAVE_PTRACE_GETFPXREGS */
454 /* Transferring arbitrary registers between GDB and inferior. */
456 /* Fetch register REGNO from the child process. If REGNO is -1, do
457 this for all registers (including the floating point and SSE
461 i386_linux_nat_target::fetch_registers (struct regcache
*regcache
, int regno
)
465 /* Use the old method of peeking around in `struct user' if the
466 GETREGS request isn't available. */
467 if (!have_ptrace_getregs
)
471 for (i
= 0; i
< gdbarch_num_regs (regcache
->arch ()); i
++)
472 if (regno
== -1 || regno
== i
)
473 fetch_register (regcache
, i
);
478 tid
= get_ptrace_pid (regcache
->ptid ());
480 /* Use the PTRACE_GETFPXREGS request whenever possible, since it
481 transfers more registers in one system call, and we'll cache the
482 results. But remember that fetch_fpxregs can fail, and return
486 fetch_regs (regcache
, tid
);
488 /* The call above might reset `have_ptrace_getregs'. */
489 if (!have_ptrace_getregs
)
491 fetch_registers (regcache
, regno
);
495 if (fetch_xstateregs (regcache
, tid
))
497 if (fetch_fpxregs (regcache
, tid
))
499 fetch_fpregs (regcache
, tid
);
503 if (GETREGS_SUPPLIES (regno
))
505 fetch_regs (regcache
, tid
);
509 if (GETXSTATEREGS_SUPPLIES (regno
))
511 if (fetch_xstateregs (regcache
, tid
))
515 if (GETFPXREGS_SUPPLIES (regno
))
517 if (fetch_fpxregs (regcache
, tid
))
520 /* Either our processor or our kernel doesn't support the SSE
521 registers, so read the FP registers in the traditional way,
522 and fill the SSE registers with dummy values. It would be
523 more graceful to handle differences in the register set using
524 gdbarch. Until then, this will at least make things work
526 fetch_fpregs (regcache
, tid
);
530 internal_error (__FILE__
, __LINE__
,
531 _("Got request for bad register number %d."), regno
);
534 /* Store register REGNO back into the child process. If REGNO is -1,
535 do this for all registers (including the floating point and SSE
538 i386_linux_nat_target::store_registers (struct regcache
*regcache
, int regno
)
542 /* Use the old method of poking around in `struct user' if the
543 SETREGS request isn't available. */
544 if (!have_ptrace_getregs
)
548 for (i
= 0; i
< gdbarch_num_regs (regcache
->arch ()); i
++)
549 if (regno
== -1 || regno
== i
)
550 store_register (regcache
, i
);
555 tid
= get_ptrace_pid (regcache
->ptid ());
557 /* Use the PTRACE_SETFPXREGS requests whenever possible, since it
558 transfers more registers in one system call. But remember that
559 store_fpxregs can fail, and return zero. */
562 store_regs (regcache
, tid
, regno
);
563 if (store_xstateregs (regcache
, tid
, regno
))
565 if (store_fpxregs (regcache
, tid
, regno
))
567 store_fpregs (regcache
, tid
, regno
);
571 if (GETREGS_SUPPLIES (regno
))
573 store_regs (regcache
, tid
, regno
);
577 if (GETXSTATEREGS_SUPPLIES (regno
))
579 if (store_xstateregs (regcache
, tid
, regno
))
583 if (GETFPXREGS_SUPPLIES (regno
))
585 if (store_fpxregs (regcache
, tid
, regno
))
588 /* Either our processor or our kernel doesn't support the SSE
589 registers, so just write the FP registers in the traditional
591 store_fpregs (regcache
, tid
, regno
);
595 internal_error (__FILE__
, __LINE__
,
596 _("Got request to store bad register number %d."), regno
);
600 /* Called by libthread_db. Returns a pointer to the thread local
601 storage (or its descriptor). */
604 ps_get_thread_area (struct ps_prochandle
*ph
,
605 lwpid_t lwpid
, int idx
, void **base
)
607 unsigned int base_addr
;
610 result
= x86_linux_get_thread_area (lwpid
, (void *) idx
, &base_addr
);
613 *(int *) base
= base_addr
;
619 /* The instruction for a GNU/Linux system call is:
623 static const unsigned char linux_syscall
[] = { 0xcd, 0x80 };
625 #define LINUX_SYSCALL_LEN (sizeof linux_syscall)
627 /* The system call number is stored in the %eax register. */
628 #define LINUX_SYSCALL_REGNUM I386_EAX_REGNUM
630 /* We are specifically interested in the sigreturn and rt_sigreturn
633 #ifndef SYS_sigreturn
634 #define SYS_sigreturn 0x77
636 #ifndef SYS_rt_sigreturn
637 #define SYS_rt_sigreturn 0xad
640 /* Offset to saved processor flags, from <asm/sigcontext.h>. */
641 #define LINUX_SIGCONTEXT_EFLAGS_OFFSET (64)
643 /* Resume execution of the inferior process.
644 If STEP is nonzero, single-step it.
645 If SIGNAL is nonzero, give it that signal. */
648 i386_linux_nat_target::low_resume (ptid_t ptid
, int step
, enum gdb_signal signal
)
650 int pid
= ptid
.lwp ();
653 if (catch_syscall_enabled () > 0)
654 request
= PTRACE_SYSCALL
;
656 request
= PTRACE_CONT
;
660 struct regcache
*regcache
= get_thread_regcache (ptid
);
661 struct gdbarch
*gdbarch
= regcache
->arch ();
662 enum bfd_endian byte_order
= gdbarch_byte_order (gdbarch
);
664 gdb_byte buf
[LINUX_SYSCALL_LEN
];
666 request
= PTRACE_SINGLESTEP
;
668 regcache_cooked_read_unsigned (regcache
,
669 gdbarch_pc_regnum (gdbarch
), &pc
);
671 /* Returning from a signal trampoline is done by calling a
672 special system call (sigreturn or rt_sigreturn, see
673 i386-linux-tdep.c for more information). This system call
674 restores the registers that were saved when the signal was
675 raised, including %eflags. That means that single-stepping
676 won't work. Instead, we'll have to modify the signal context
677 that's about to be restored, and set the trace flag there. */
679 /* First check if PC is at a system call. */
680 if (target_read_memory (pc
, buf
, LINUX_SYSCALL_LEN
) == 0
681 && memcmp (buf
, linux_syscall
, LINUX_SYSCALL_LEN
) == 0)
684 regcache_cooked_read_unsigned (regcache
,
685 LINUX_SYSCALL_REGNUM
, &syscall
);
687 /* Then check the system call number. */
688 if (syscall
== SYS_sigreturn
|| syscall
== SYS_rt_sigreturn
)
691 unsigned long int eflags
;
693 regcache_cooked_read_unsigned (regcache
, I386_ESP_REGNUM
, &sp
);
694 if (syscall
== SYS_rt_sigreturn
)
695 addr
= read_memory_unsigned_integer (sp
+ 8, 4, byte_order
)
700 /* Set the trace flag in the context that's about to be
702 addr
+= LINUX_SIGCONTEXT_EFLAGS_OFFSET
;
703 read_memory (addr
, (gdb_byte
*) &eflags
, 4);
705 write_memory (addr
, (gdb_byte
*) &eflags
, 4);
710 if (ptrace (request
, pid
, 0, gdb_signal_to_host (signal
)) == -1)
711 perror_with_name (("ptrace"));
715 _initialize_i386_linux_nat (void)
717 linux_target
= &the_i386_linux_nat_target
;
719 /* Add the target. */
720 add_inf_child_target (linux_target
);