1 /* Native-dependent code for GNU/Linux i386.
3 Copyright (C) 1999-2017 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 "x86-xstate.h"
36 #include "linux-nat.h"
37 #include "x86-linux-nat.h"
38 #include "nat/linux-ptrace.h"
39 #include "inf-ptrace.h"
41 /* The register sets used in GNU/Linux ELF core-dumps are identical to
42 the register sets in `struct user' that is used for a.out
43 core-dumps, and is also used by `ptrace'. The corresponding types
44 are `elf_gregset_t' for the general-purpose registers (with
45 `elf_greg_t' the type of a single GP register) and `elf_fpregset_t'
46 for the floating-point registers.
48 Those types used to be available under the names `gregset_t' and
49 `fpregset_t' too, and this file used those names in the past. But
50 those names are now used for the register sets used in the
51 `mcontext_t' type, and have a different size and layout. */
53 /* Which ptrace request retrieves which registers?
54 These apply to the corresponding SET requests as well. */
56 #define GETREGS_SUPPLIES(regno) \
57 ((0 <= (regno) && (regno) <= 15) || (regno) == I386_LINUX_ORIG_EAX_REGNUM)
59 #define GETFPXREGS_SUPPLIES(regno) \
60 (I386_ST0_REGNUM <= (regno) && (regno) < I386_SSE_NUM_REGS)
62 #define GETXSTATEREGS_SUPPLIES(regno) \
63 (I386_ST0_REGNUM <= (regno) && (regno) < I386_PKEYS_NUM_REGS)
65 /* Does the current host support the GETREGS request? */
66 int have_ptrace_getregs =
67 #ifdef HAVE_PTRACE_GETREGS
74 /* Does the current host support the GETFPXREGS request? The header
75 file may or may not define it, and even if it is defined, the
76 kernel will return EIO if it's running on a pre-SSE processor.
78 My instinct is to attach this to some architecture- or
79 target-specific data structure, but really, a particular GDB
80 process can only run on top of one kernel at a time. So it's okay
81 for this to be a simple variable. */
82 int have_ptrace_getfpxregs =
83 #ifdef HAVE_PTRACE_GETFPXREGS
91 /* Accessing registers through the U area, one at a time. */
93 /* Fetch one register. */
96 fetch_register (struct regcache *regcache, int regno)
101 gdb_assert (!have_ptrace_getregs);
102 if (i386_linux_gregset_reg_offset[regno] == -1)
104 regcache_raw_supply (regcache, regno, NULL);
108 tid = get_ptrace_pid (regcache_get_ptid (regcache));
111 val = ptrace (PTRACE_PEEKUSER, tid,
112 i386_linux_gregset_reg_offset[regno], 0);
114 error (_("Couldn't read register %s (#%d): %s."),
115 gdbarch_register_name (regcache->arch (), regno),
116 regno, safe_strerror (errno));
118 regcache_raw_supply (regcache, regno, &val);
121 /* Store one register. */
124 store_register (const struct regcache *regcache, int regno)
129 gdb_assert (!have_ptrace_getregs);
130 if (i386_linux_gregset_reg_offset[regno] == -1)
133 tid = get_ptrace_pid (regcache_get_ptid (regcache));
136 regcache_raw_collect (regcache, regno, &val);
137 ptrace (PTRACE_POKEUSER, tid,
138 i386_linux_gregset_reg_offset[regno], val);
140 error (_("Couldn't write register %s (#%d): %s."),
141 gdbarch_register_name (regcache->arch (), regno),
142 regno, safe_strerror (errno));
146 /* Transfering the general-purpose registers between GDB, inferiors
149 /* Fill GDB's register array with the general-purpose register values
153 supply_gregset (struct regcache *regcache, const elf_gregset_t *gregsetp)
155 const gdb_byte *regp = (const gdb_byte *) gregsetp;
158 for (i = 0; i < I386_NUM_GREGS; i++)
159 regcache_raw_supply (regcache, i,
160 regp + i386_linux_gregset_reg_offset[i]);
162 if (I386_LINUX_ORIG_EAX_REGNUM
163 < gdbarch_num_regs (regcache->arch ()))
164 regcache_raw_supply (regcache, I386_LINUX_ORIG_EAX_REGNUM, regp
165 + i386_linux_gregset_reg_offset[I386_LINUX_ORIG_EAX_REGNUM]);
168 /* Fill register REGNO (if it is a general-purpose register) in
169 *GREGSETPS with the value in GDB's register array. If REGNO is -1,
170 do this for all registers. */
173 fill_gregset (const struct regcache *regcache,
174 elf_gregset_t *gregsetp, int regno)
176 gdb_byte *regp = (gdb_byte *) gregsetp;
179 for (i = 0; i < I386_NUM_GREGS; i++)
180 if (regno == -1 || regno == i)
181 regcache_raw_collect (regcache, i,
182 regp + i386_linux_gregset_reg_offset[i]);
184 if ((regno == -1 || regno == I386_LINUX_ORIG_EAX_REGNUM)
185 && I386_LINUX_ORIG_EAX_REGNUM
186 < gdbarch_num_regs (regcache->arch ()))
187 regcache_raw_collect (regcache, I386_LINUX_ORIG_EAX_REGNUM, regp
188 + i386_linux_gregset_reg_offset[I386_LINUX_ORIG_EAX_REGNUM]);
191 #ifdef HAVE_PTRACE_GETREGS
193 /* Fetch all general-purpose registers from process/thread TID and
194 store their values in GDB's register array. */
197 fetch_regs (struct regcache *regcache, int tid)
200 elf_gregset_t *regs_p = ®s;
202 if (ptrace (PTRACE_GETREGS, tid, 0, (int) ®s) < 0)
206 /* The kernel we're running on doesn't support the GETREGS
207 request. Reset `have_ptrace_getregs'. */
208 have_ptrace_getregs = 0;
212 perror_with_name (_("Couldn't get registers"));
215 supply_gregset (regcache, (const elf_gregset_t *) regs_p);
218 /* Store all valid general-purpose registers in GDB's register array
219 into the process/thread specified by TID. */
222 store_regs (const struct regcache *regcache, int tid, int regno)
226 if (ptrace (PTRACE_GETREGS, tid, 0, (int) ®s) < 0)
227 perror_with_name (_("Couldn't get registers"));
229 fill_gregset (regcache, ®s, regno);
231 if (ptrace (PTRACE_SETREGS, tid, 0, (int) ®s) < 0)
232 perror_with_name (_("Couldn't write registers"));
237 static void fetch_regs (struct regcache *regcache, int tid) {}
238 static void store_regs (const struct regcache *regcache, int tid, int regno) {}
243 /* Transfering floating-point registers between GDB, inferiors and cores. */
245 /* Fill GDB's register array with the floating-point register values in
249 supply_fpregset (struct regcache *regcache, const elf_fpregset_t *fpregsetp)
251 i387_supply_fsave (regcache, -1, fpregsetp);
254 /* Fill register REGNO (if it is a floating-point register) in
255 *FPREGSETP with the value in GDB's register array. If REGNO is -1,
256 do this for all registers. */
259 fill_fpregset (const struct regcache *regcache,
260 elf_fpregset_t *fpregsetp, int regno)
262 i387_collect_fsave (regcache, regno, fpregsetp);
265 #ifdef HAVE_PTRACE_GETREGS
267 /* Fetch all floating-point registers from process/thread TID and store
268 thier values in GDB's register array. */
271 fetch_fpregs (struct regcache *regcache, int tid)
273 elf_fpregset_t fpregs;
275 if (ptrace (PTRACE_GETFPREGS, tid, 0, (int) &fpregs) < 0)
276 perror_with_name (_("Couldn't get floating point status"));
278 supply_fpregset (regcache, (const elf_fpregset_t *) &fpregs);
281 /* Store all valid floating-point registers in GDB's register array
282 into the process/thread specified by TID. */
285 store_fpregs (const struct regcache *regcache, int tid, int regno)
287 elf_fpregset_t fpregs;
289 if (ptrace (PTRACE_GETFPREGS, tid, 0, (int) &fpregs) < 0)
290 perror_with_name (_("Couldn't get floating point status"));
292 fill_fpregset (regcache, &fpregs, regno);
294 if (ptrace (PTRACE_SETFPREGS, tid, 0, (int) &fpregs) < 0)
295 perror_with_name (_("Couldn't write floating point status"));
301 fetch_fpregs (struct regcache *regcache, int tid)
306 store_fpregs (const struct regcache *regcache, int tid, int regno)
313 /* Transfering floating-point and SSE registers to and from GDB. */
315 /* Fetch all registers covered by the PTRACE_GETREGSET request from
316 process/thread TID and store their values in GDB's register array.
317 Return non-zero if successful, zero otherwise. */
320 fetch_xstateregs (struct regcache *regcache, int tid)
322 char xstateregs[X86_XSTATE_MAX_SIZE];
325 if (have_ptrace_getregset != TRIBOOL_TRUE)
328 iov.iov_base = xstateregs;
329 iov.iov_len = sizeof(xstateregs);
330 if (ptrace (PTRACE_GETREGSET, tid, (unsigned int) NT_X86_XSTATE,
332 perror_with_name (_("Couldn't read extended state status"));
334 i387_supply_xsave (regcache, -1, xstateregs);
338 /* Store all valid registers in GDB's register array covered by the
339 PTRACE_SETREGSET request into the process/thread specified by TID.
340 Return non-zero if successful, zero otherwise. */
343 store_xstateregs (const struct regcache *regcache, int tid, int regno)
345 char xstateregs[X86_XSTATE_MAX_SIZE];
348 if (have_ptrace_getregset != TRIBOOL_TRUE)
351 iov.iov_base = xstateregs;
352 iov.iov_len = sizeof(xstateregs);
353 if (ptrace (PTRACE_GETREGSET, tid, (unsigned int) NT_X86_XSTATE,
355 perror_with_name (_("Couldn't read extended state status"));
357 i387_collect_xsave (regcache, regno, xstateregs, 0);
359 if (ptrace (PTRACE_SETREGSET, tid, (unsigned int) NT_X86_XSTATE,
361 perror_with_name (_("Couldn't write extended state status"));
366 #ifdef HAVE_PTRACE_GETFPXREGS
368 /* Fetch all registers covered by the PTRACE_GETFPXREGS request from
369 process/thread TID and store their values in GDB's register array.
370 Return non-zero if successful, zero otherwise. */
373 fetch_fpxregs (struct regcache *regcache, int tid)
375 elf_fpxregset_t fpxregs;
377 if (! have_ptrace_getfpxregs)
380 if (ptrace (PTRACE_GETFPXREGS, tid, 0, (int) &fpxregs) < 0)
384 have_ptrace_getfpxregs = 0;
388 perror_with_name (_("Couldn't read floating-point and SSE registers"));
391 i387_supply_fxsave (regcache, -1, (const elf_fpxregset_t *) &fpxregs);
395 /* Store all valid registers in GDB's register array covered by the
396 PTRACE_SETFPXREGS request into the process/thread specified by TID.
397 Return non-zero if successful, zero otherwise. */
400 store_fpxregs (const struct regcache *regcache, int tid, int regno)
402 elf_fpxregset_t fpxregs;
404 if (! have_ptrace_getfpxregs)
407 if (ptrace (PTRACE_GETFPXREGS, tid, 0, &fpxregs) == -1)
411 have_ptrace_getfpxregs = 0;
415 perror_with_name (_("Couldn't read floating-point and SSE registers"));
418 i387_collect_fxsave (regcache, regno, &fpxregs);
420 if (ptrace (PTRACE_SETFPXREGS, tid, 0, &fpxregs) == -1)
421 perror_with_name (_("Couldn't write floating-point and SSE registers"));
429 fetch_fpxregs (struct regcache *regcache, int tid)
435 store_fpxregs (const struct regcache *regcache, int tid, int regno)
440 #endif /* HAVE_PTRACE_GETFPXREGS */
443 /* Transferring arbitrary registers between GDB and inferior. */
445 /* Fetch register REGNO from the child process. If REGNO is -1, do
446 this for all registers (including the floating point and SSE
450 i386_linux_fetch_inferior_registers (struct target_ops *ops,
451 struct regcache *regcache, int regno)
455 /* Use the old method of peeking around in `struct user' if the
456 GETREGS request isn't available. */
457 if (!have_ptrace_getregs)
461 for (i = 0; i < gdbarch_num_regs (regcache->arch ()); i++)
462 if (regno == -1 || regno == i)
463 fetch_register (regcache, i);
468 tid = get_ptrace_pid (regcache_get_ptid (regcache));
470 /* Use the PTRACE_GETFPXREGS request whenever possible, since it
471 transfers more registers in one system call, and we'll cache the
472 results. But remember that fetch_fpxregs can fail, and return
476 fetch_regs (regcache, tid);
478 /* The call above might reset `have_ptrace_getregs'. */
479 if (!have_ptrace_getregs)
481 i386_linux_fetch_inferior_registers (ops, regcache, regno);
485 if (fetch_xstateregs (regcache, tid))
487 if (fetch_fpxregs (regcache, tid))
489 fetch_fpregs (regcache, tid);
493 if (GETREGS_SUPPLIES (regno))
495 fetch_regs (regcache, tid);
499 if (GETXSTATEREGS_SUPPLIES (regno))
501 if (fetch_xstateregs (regcache, tid))
505 if (GETFPXREGS_SUPPLIES (regno))
507 if (fetch_fpxregs (regcache, tid))
510 /* Either our processor or our kernel doesn't support the SSE
511 registers, so read the FP registers in the traditional way,
512 and fill the SSE registers with dummy values. It would be
513 more graceful to handle differences in the register set using
514 gdbarch. Until then, this will at least make things work
516 fetch_fpregs (regcache, tid);
520 internal_error (__FILE__, __LINE__,
521 _("Got request for bad register number %d."), regno);
524 /* Store register REGNO back into the child process. If REGNO is -1,
525 do this for all registers (including the floating point and SSE
528 i386_linux_store_inferior_registers (struct target_ops *ops,
529 struct regcache *regcache, int regno)
533 /* Use the old method of poking around in `struct user' if the
534 SETREGS request isn't available. */
535 if (!have_ptrace_getregs)
539 for (i = 0; i < gdbarch_num_regs (regcache->arch ()); i++)
540 if (regno == -1 || regno == i)
541 store_register (regcache, i);
546 tid = get_ptrace_pid (regcache_get_ptid (regcache));
548 /* Use the PTRACE_SETFPXREGS requests whenever possible, since it
549 transfers more registers in one system call. But remember that
550 store_fpxregs can fail, and return zero. */
553 store_regs (regcache, tid, regno);
554 if (store_xstateregs (regcache, tid, regno))
556 if (store_fpxregs (regcache, tid, regno))
558 store_fpregs (regcache, tid, regno);
562 if (GETREGS_SUPPLIES (regno))
564 store_regs (regcache, tid, regno);
568 if (GETXSTATEREGS_SUPPLIES (regno))
570 if (store_xstateregs (regcache, tid, regno))
574 if (GETFPXREGS_SUPPLIES (regno))
576 if (store_fpxregs (regcache, tid, regno))
579 /* Either our processor or our kernel doesn't support the SSE
580 registers, so just write the FP registers in the traditional
582 store_fpregs (regcache, tid, regno);
586 internal_error (__FILE__, __LINE__,
587 _("Got request to store bad register number %d."), regno);
591 /* Called by libthread_db. Returns a pointer to the thread local
592 storage (or its descriptor). */
595 ps_get_thread_area (struct ps_prochandle *ph,
596 lwpid_t lwpid, int idx, void **base)
598 unsigned int base_addr;
601 result = x86_linux_get_thread_area (lwpid, (void *) idx, &base_addr);
604 *(int *) base = base_addr;
610 /* The instruction for a GNU/Linux system call is:
614 static const unsigned char linux_syscall[] = { 0xcd, 0x80 };
616 #define LINUX_SYSCALL_LEN (sizeof linux_syscall)
618 /* The system call number is stored in the %eax register. */
619 #define LINUX_SYSCALL_REGNUM I386_EAX_REGNUM
621 /* We are specifically interested in the sigreturn and rt_sigreturn
624 #ifndef SYS_sigreturn
625 #define SYS_sigreturn 0x77
627 #ifndef SYS_rt_sigreturn
628 #define SYS_rt_sigreturn 0xad
631 /* Offset to saved processor flags, from <asm/sigcontext.h>. */
632 #define LINUX_SIGCONTEXT_EFLAGS_OFFSET (64)
634 /* Resume execution of the inferior process.
635 If STEP is nonzero, single-step it.
636 If SIGNAL is nonzero, give it that signal. */
639 i386_linux_resume (struct target_ops *ops,
640 ptid_t ptid, int step, enum gdb_signal signal)
642 int pid = ptid_get_lwp (ptid);
645 if (catch_syscall_enabled () > 0)
646 request = PTRACE_SYSCALL;
648 request = PTRACE_CONT;
652 struct regcache *regcache = get_thread_regcache (ptid);
653 struct gdbarch *gdbarch = regcache->arch ();
654 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
656 gdb_byte buf[LINUX_SYSCALL_LEN];
658 request = PTRACE_SINGLESTEP;
660 regcache_cooked_read_unsigned (regcache,
661 gdbarch_pc_regnum (gdbarch), &pc);
663 /* Returning from a signal trampoline is done by calling a
664 special system call (sigreturn or rt_sigreturn, see
665 i386-linux-tdep.c for more information). This system call
666 restores the registers that were saved when the signal was
667 raised, including %eflags. That means that single-stepping
668 won't work. Instead, we'll have to modify the signal context
669 that's about to be restored, and set the trace flag there. */
671 /* First check if PC is at a system call. */
672 if (target_read_memory (pc, buf, LINUX_SYSCALL_LEN) == 0
673 && memcmp (buf, linux_syscall, LINUX_SYSCALL_LEN) == 0)
676 regcache_cooked_read_unsigned (regcache,
677 LINUX_SYSCALL_REGNUM, &syscall);
679 /* Then check the system call number. */
680 if (syscall == SYS_sigreturn || syscall == SYS_rt_sigreturn)
683 unsigned long int eflags;
685 regcache_cooked_read_unsigned (regcache, I386_ESP_REGNUM, &sp);
686 if (syscall == SYS_rt_sigreturn)
687 addr = read_memory_unsigned_integer (sp + 8, 4, byte_order)
692 /* Set the trace flag in the context that's about to be
694 addr += LINUX_SIGCONTEXT_EFLAGS_OFFSET;
695 read_memory (addr, (gdb_byte *) &eflags, 4);
697 write_memory (addr, (gdb_byte *) &eflags, 4);
702 if (ptrace (request, pid, 0, gdb_signal_to_host (signal)) == -1)
703 perror_with_name (("ptrace"));
707 _initialize_i386_linux_nat (void)
709 /* Create a generic x86 GNU/Linux target. */
710 struct target_ops *t = x86_linux_create_target ();
712 /* Override the default ptrace resume method. */
713 t->to_resume = i386_linux_resume;
715 /* Add our register access methods. */
716 t->to_fetch_registers = i386_linux_fetch_inferior_registers;
717 t->to_store_registers = i386_linux_store_inferior_registers;
719 /* Add the target. */
720 x86_linux_add_target (t);