1 /* Native-dependent code for GNU/Linux i386.
3 Copyright (C) 1999-2014 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 <sys/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 "x86-linux-nat.h"
38 /* The register sets used in GNU/Linux ELF core-dumps are identical to
39 the register sets in `struct user' that is used for a.out
40 core-dumps, and is also used by `ptrace'. The corresponding types
41 are `elf_gregset_t' for the general-purpose registers (with
42 `elf_greg_t' the type of a single GP register) and `elf_fpregset_t'
43 for the floating-point registers.
45 Those types used to be available under the names `gregset_t' and
46 `fpregset_t' too, and this file used those names in the past. But
47 those names are now used for the register sets used in the
48 `mcontext_t' type, and have a different size and layout. */
50 /* Which ptrace request retrieves which registers?
51 These apply to the corresponding SET requests as well. */
53 #define GETREGS_SUPPLIES(regno) \
54 ((0 <= (regno) && (regno) <= 15) || (regno) == I386_LINUX_ORIG_EAX_REGNUM)
56 #define GETFPXREGS_SUPPLIES(regno) \
57 (I386_ST0_REGNUM <= (regno) && (regno) < I386_SSE_NUM_REGS)
59 #define GETXSTATEREGS_SUPPLIES(regno) \
60 (I386_ST0_REGNUM <= (regno) && (regno) < I386_AVX512_NUM_REGS)
62 /* Does the current host support the GETREGS request? */
63 int have_ptrace_getregs =
64 #ifdef HAVE_PTRACE_GETREGS
71 /* Does the current host support the GETFPXREGS request? The header
72 file may or may not define it, and even if it is defined, the
73 kernel will return EIO if it's running on a pre-SSE processor.
75 My instinct is to attach this to some architecture- or
76 target-specific data structure, but really, a particular GDB
77 process can only run on top of one kernel at a time. So it's okay
78 for this to be a simple variable. */
79 int have_ptrace_getfpxregs =
80 #ifdef HAVE_PTRACE_GETFPXREGS
88 /* Accessing registers through the U area, one at a time. */
90 /* Fetch one register. */
93 fetch_register (struct regcache *regcache, int regno)
98 gdb_assert (!have_ptrace_getregs);
99 if (i386_linux_gregset_reg_offset[regno] == -1)
101 regcache_raw_supply (regcache, regno, NULL);
105 /* GNU/Linux LWP ID's are process ID's. */
106 tid = ptid_get_lwp (inferior_ptid);
108 tid = ptid_get_pid (inferior_ptid); /* Not a threaded program. */
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 (get_regcache_arch (regcache), 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 /* GNU/Linux LWP ID's are process ID's. */
134 tid = ptid_get_lwp (inferior_ptid);
136 tid = ptid_get_pid (inferior_ptid); /* Not a threaded program. */
139 regcache_raw_collect (regcache, regno, &val);
140 ptrace (PTRACE_POKEUSER, tid,
141 i386_linux_gregset_reg_offset[regno], val);
143 error (_("Couldn't write register %s (#%d): %s."),
144 gdbarch_register_name (get_regcache_arch (regcache), regno),
145 regno, safe_strerror (errno));
149 /* Transfering the general-purpose registers between GDB, inferiors
152 /* Fill GDB's register array with the general-purpose register values
156 supply_gregset (struct regcache *regcache, const elf_gregset_t *gregsetp)
158 const gdb_byte *regp = (const gdb_byte *) gregsetp;
161 for (i = 0; i < I386_NUM_GREGS; i++)
162 regcache_raw_supply (regcache, i,
163 regp + i386_linux_gregset_reg_offset[i]);
165 if (I386_LINUX_ORIG_EAX_REGNUM
166 < gdbarch_num_regs (get_regcache_arch (regcache)))
167 regcache_raw_supply (regcache, I386_LINUX_ORIG_EAX_REGNUM, regp
168 + i386_linux_gregset_reg_offset[I386_LINUX_ORIG_EAX_REGNUM]);
171 /* Fill register REGNO (if it is a general-purpose register) in
172 *GREGSETPS with the value in GDB's register array. If REGNO is -1,
173 do this for all registers. */
176 fill_gregset (const struct regcache *regcache,
177 elf_gregset_t *gregsetp, int regno)
179 gdb_byte *regp = (gdb_byte *) gregsetp;
182 for (i = 0; i < I386_NUM_GREGS; i++)
183 if (regno == -1 || regno == i)
184 regcache_raw_collect (regcache, i,
185 regp + i386_linux_gregset_reg_offset[i]);
187 if ((regno == -1 || regno == I386_LINUX_ORIG_EAX_REGNUM)
188 && I386_LINUX_ORIG_EAX_REGNUM
189 < gdbarch_num_regs (get_regcache_arch (regcache)))
190 regcache_raw_collect (regcache, I386_LINUX_ORIG_EAX_REGNUM, regp
191 + i386_linux_gregset_reg_offset[I386_LINUX_ORIG_EAX_REGNUM]);
194 #ifdef HAVE_PTRACE_GETREGS
196 /* Fetch all general-purpose registers from process/thread TID and
197 store their values in GDB's register array. */
200 fetch_regs (struct regcache *regcache, int tid)
203 elf_gregset_t *regs_p = ®s;
205 if (ptrace (PTRACE_GETREGS, tid, 0, (int) ®s) < 0)
209 /* The kernel we're running on doesn't support the GETREGS
210 request. Reset `have_ptrace_getregs'. */
211 have_ptrace_getregs = 0;
215 perror_with_name (_("Couldn't get registers"));
218 supply_gregset (regcache, (const elf_gregset_t *) regs_p);
221 /* Store all valid general-purpose registers in GDB's register array
222 into the process/thread specified by TID. */
225 store_regs (const struct regcache *regcache, int tid, int regno)
229 if (ptrace (PTRACE_GETREGS, tid, 0, (int) ®s) < 0)
230 perror_with_name (_("Couldn't get registers"));
232 fill_gregset (regcache, ®s, regno);
234 if (ptrace (PTRACE_SETREGS, tid, 0, (int) ®s) < 0)
235 perror_with_name (_("Couldn't write registers"));
240 static void fetch_regs (struct regcache *regcache, int tid) {}
241 static void store_regs (const struct regcache *regcache, int tid, int regno) {}
246 /* Transfering floating-point registers between GDB, inferiors and cores. */
248 /* Fill GDB's register array with the floating-point register values in
252 supply_fpregset (struct regcache *regcache, const elf_fpregset_t *fpregsetp)
254 i387_supply_fsave (regcache, -1, fpregsetp);
257 /* Fill register REGNO (if it is a floating-point register) in
258 *FPREGSETP with the value in GDB's register array. If REGNO is -1,
259 do this for all registers. */
262 fill_fpregset (const struct regcache *regcache,
263 elf_fpregset_t *fpregsetp, int regno)
265 i387_collect_fsave (regcache, regno, fpregsetp);
268 #ifdef HAVE_PTRACE_GETREGS
270 /* Fetch all floating-point registers from process/thread TID and store
271 thier values in GDB's register array. */
274 fetch_fpregs (struct regcache *regcache, int tid)
276 elf_fpregset_t fpregs;
278 if (ptrace (PTRACE_GETFPREGS, tid, 0, (int) &fpregs) < 0)
279 perror_with_name (_("Couldn't get floating point status"));
281 supply_fpregset (regcache, (const elf_fpregset_t *) &fpregs);
284 /* Store all valid floating-point registers in GDB's register array
285 into the process/thread specified by TID. */
288 store_fpregs (const struct regcache *regcache, int tid, int regno)
290 elf_fpregset_t fpregs;
292 if (ptrace (PTRACE_GETFPREGS, tid, 0, (int) &fpregs) < 0)
293 perror_with_name (_("Couldn't get floating point status"));
295 fill_fpregset (regcache, &fpregs, regno);
297 if (ptrace (PTRACE_SETFPREGS, tid, 0, (int) &fpregs) < 0)
298 perror_with_name (_("Couldn't write floating point status"));
304 fetch_fpregs (struct regcache *regcache, int tid)
309 store_fpregs (const struct regcache *regcache, int tid, int regno)
316 /* Transfering floating-point and SSE registers to and from GDB. */
318 /* Fetch all registers covered by the PTRACE_GETREGSET request from
319 process/thread TID and store their values in GDB's register array.
320 Return non-zero if successful, zero otherwise. */
323 fetch_xstateregs (struct regcache *regcache, int tid)
325 char xstateregs[X86_XSTATE_MAX_SIZE];
328 if (!have_ptrace_getregset)
331 iov.iov_base = xstateregs;
332 iov.iov_len = sizeof(xstateregs);
333 if (ptrace (PTRACE_GETREGSET, tid, (unsigned int) NT_X86_XSTATE,
335 perror_with_name (_("Couldn't read extended state status"));
337 i387_supply_xsave (regcache, -1, xstateregs);
341 /* Store all valid registers in GDB's register array covered by the
342 PTRACE_SETREGSET request into the process/thread specified by TID.
343 Return non-zero if successful, zero otherwise. */
346 store_xstateregs (const struct regcache *regcache, int tid, int regno)
348 char xstateregs[X86_XSTATE_MAX_SIZE];
351 if (!have_ptrace_getregset)
354 iov.iov_base = xstateregs;
355 iov.iov_len = sizeof(xstateregs);
356 if (ptrace (PTRACE_GETREGSET, tid, (unsigned int) NT_X86_XSTATE,
358 perror_with_name (_("Couldn't read extended state status"));
360 i387_collect_xsave (regcache, regno, xstateregs, 0);
362 if (ptrace (PTRACE_SETREGSET, tid, (unsigned int) NT_X86_XSTATE,
364 perror_with_name (_("Couldn't write extended state status"));
369 #ifdef HAVE_PTRACE_GETFPXREGS
371 /* Fetch all registers covered by the PTRACE_GETFPXREGS request from
372 process/thread TID and store their values in GDB's register array.
373 Return non-zero if successful, zero otherwise. */
376 fetch_fpxregs (struct regcache *regcache, int tid)
378 elf_fpxregset_t fpxregs;
380 if (! have_ptrace_getfpxregs)
383 if (ptrace (PTRACE_GETFPXREGS, tid, 0, (int) &fpxregs) < 0)
387 have_ptrace_getfpxregs = 0;
391 perror_with_name (_("Couldn't read floating-point and SSE registers"));
394 i387_supply_fxsave (regcache, -1, (const elf_fpxregset_t *) &fpxregs);
398 /* Store all valid registers in GDB's register array covered by the
399 PTRACE_SETFPXREGS request into the process/thread specified by TID.
400 Return non-zero if successful, zero otherwise. */
403 store_fpxregs (const struct regcache *regcache, int tid, int regno)
405 elf_fpxregset_t fpxregs;
407 if (! have_ptrace_getfpxregs)
410 if (ptrace (PTRACE_GETFPXREGS, tid, 0, &fpxregs) == -1)
414 have_ptrace_getfpxregs = 0;
418 perror_with_name (_("Couldn't read floating-point and SSE registers"));
421 i387_collect_fxsave (regcache, regno, &fpxregs);
423 if (ptrace (PTRACE_SETFPXREGS, tid, 0, &fpxregs) == -1)
424 perror_with_name (_("Couldn't write floating-point and SSE registers"));
432 fetch_fpxregs (struct regcache *regcache, int tid)
438 store_fpxregs (const struct regcache *regcache, int tid, int regno)
443 #endif /* HAVE_PTRACE_GETFPXREGS */
446 /* Transferring arbitrary registers between GDB and inferior. */
448 /* Fetch register REGNO from the child process. If REGNO is -1, do
449 this for all registers (including the floating point and SSE
453 i386_linux_fetch_inferior_registers (struct target_ops *ops,
454 struct regcache *regcache, int regno)
458 /* Use the old method of peeking around in `struct user' if the
459 GETREGS request isn't available. */
460 if (!have_ptrace_getregs)
464 for (i = 0; i < gdbarch_num_regs (get_regcache_arch (regcache)); i++)
465 if (regno == -1 || regno == i)
466 fetch_register (regcache, i);
471 /* GNU/Linux LWP ID's are process ID's. */
472 tid = ptid_get_lwp (inferior_ptid);
474 tid = ptid_get_pid (inferior_ptid); /* Not a threaded program. */
476 /* Use the PTRACE_GETFPXREGS request whenever possible, since it
477 transfers more registers in one system call, and we'll cache the
478 results. But remember that fetch_fpxregs can fail, and return
482 fetch_regs (regcache, tid);
484 /* The call above might reset `have_ptrace_getregs'. */
485 if (!have_ptrace_getregs)
487 i386_linux_fetch_inferior_registers (ops, regcache, regno);
491 if (fetch_xstateregs (regcache, tid))
493 if (fetch_fpxregs (regcache, tid))
495 fetch_fpregs (regcache, tid);
499 if (GETREGS_SUPPLIES (regno))
501 fetch_regs (regcache, tid);
505 if (GETXSTATEREGS_SUPPLIES (regno))
507 if (fetch_xstateregs (regcache, tid))
511 if (GETFPXREGS_SUPPLIES (regno))
513 if (fetch_fpxregs (regcache, tid))
516 /* Either our processor or our kernel doesn't support the SSE
517 registers, so read the FP registers in the traditional way,
518 and fill the SSE registers with dummy values. It would be
519 more graceful to handle differences in the register set using
520 gdbarch. Until then, this will at least make things work
522 fetch_fpregs (regcache, tid);
526 internal_error (__FILE__, __LINE__,
527 _("Got request for bad register number %d."), regno);
530 /* Store register REGNO back into the child process. If REGNO is -1,
531 do this for all registers (including the floating point and SSE
534 i386_linux_store_inferior_registers (struct target_ops *ops,
535 struct regcache *regcache, int regno)
539 /* Use the old method of poking around in `struct user' if the
540 SETREGS request isn't available. */
541 if (!have_ptrace_getregs)
545 for (i = 0; i < gdbarch_num_regs (get_regcache_arch (regcache)); i++)
546 if (regno == -1 || regno == i)
547 store_register (regcache, i);
552 /* GNU/Linux LWP ID's are process ID's. */
553 tid = ptid_get_lwp (inferior_ptid);
555 tid = ptid_get_pid (inferior_ptid); /* Not a threaded program. */
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 (const 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_resume (struct target_ops *ops,
649 ptid_t ptid, int step, enum gdb_signal signal)
651 int pid = ptid_get_pid (ptid);
655 if (catch_syscall_enabled () > 0)
656 request = PTRACE_SYSCALL;
658 request = PTRACE_CONT;
662 struct regcache *regcache = get_thread_regcache (pid_to_ptid (pid));
663 struct gdbarch *gdbarch = get_regcache_arch (regcache);
664 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
666 gdb_byte buf[LINUX_SYSCALL_LEN];
668 request = PTRACE_SINGLESTEP;
670 regcache_cooked_read_unsigned (regcache,
671 gdbarch_pc_regnum (gdbarch), &pc);
673 /* Returning from a signal trampoline is done by calling a
674 special system call (sigreturn or rt_sigreturn, see
675 i386-linux-tdep.c for more information). This system call
676 restores the registers that were saved when the signal was
677 raised, including %eflags. That means that single-stepping
678 won't work. Instead, we'll have to modify the signal context
679 that's about to be restored, and set the trace flag there. */
681 /* First check if PC is at a system call. */
682 if (target_read_memory (pc, buf, LINUX_SYSCALL_LEN) == 0
683 && memcmp (buf, linux_syscall, LINUX_SYSCALL_LEN) == 0)
686 regcache_cooked_read_unsigned (regcache,
687 LINUX_SYSCALL_REGNUM, &syscall);
689 /* Then check the system call number. */
690 if (syscall == SYS_sigreturn || syscall == SYS_rt_sigreturn)
693 unsigned long int eflags;
695 regcache_cooked_read_unsigned (regcache, I386_ESP_REGNUM, &sp);
696 if (syscall == SYS_rt_sigreturn)
697 addr = read_memory_unsigned_integer (sp + 8, 4, byte_order)
702 /* Set the trace flag in the context that's about to be
704 addr += LINUX_SIGCONTEXT_EFLAGS_OFFSET;
705 read_memory (addr, (gdb_byte *) &eflags, 4);
707 write_memory (addr, (gdb_byte *) &eflags, 4);
712 if (ptrace (request, pid, 0, gdb_signal_to_host (signal)) == -1)
713 perror_with_name (("ptrace"));
717 /* -Wmissing-prototypes */
718 extern initialize_file_ftype _initialize_i386_linux_nat;
721 _initialize_i386_linux_nat (void)
723 /* Create a generic x86 GNU/Linux target. */
724 struct target_ops *t = x86_linux_create_target ();
726 /* Override the default ptrace resume method. */
727 t->to_resume = i386_linux_resume;
729 /* Add our register access methods. */
730 t->to_fetch_registers = i386_linux_fetch_inferior_registers;
731 t->to_store_registers = i386_linux_store_inferior_registers;
733 /* Add the target. */
734 x86_linux_add_target (t);