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>
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 "x86-xstate.h"
35 #include "x86-linux-nat.h"
37 /* The register sets used in GNU/Linux ELF core-dumps are identical to
38 the register sets in `struct user' that is used for a.out
39 core-dumps, and is also used by `ptrace'. The corresponding types
40 are `elf_gregset_t' for the general-purpose registers (with
41 `elf_greg_t' the type of a single GP register) and `elf_fpregset_t'
42 for the floating-point registers.
44 Those types used to be available under the names `gregset_t' and
45 `fpregset_t' too, and this file used those names in the past. But
46 those names are now used for the register sets used in the
47 `mcontext_t' type, and have a different size and layout. */
49 /* Which ptrace request retrieves which registers?
50 These apply to the corresponding SET requests as well. */
52 #define GETREGS_SUPPLIES(regno) \
53 ((0 <= (regno) && (regno) <= 15) || (regno) == I386_LINUX_ORIG_EAX_REGNUM)
55 #define GETFPXREGS_SUPPLIES(regno) \
56 (I386_ST0_REGNUM <= (regno) && (regno) < I386_SSE_NUM_REGS)
58 #define GETXSTATEREGS_SUPPLIES(regno) \
59 (I386_ST0_REGNUM <= (regno) && (regno) < I386_AVX512_NUM_REGS)
61 /* Does the current host support the GETREGS request? */
62 int have_ptrace_getregs =
63 #ifdef HAVE_PTRACE_GETREGS
70 /* Does the current host support the GETFPXREGS request? The header
71 file may or may not define it, and even if it is defined, the
72 kernel will return EIO if it's running on a pre-SSE processor.
74 My instinct is to attach this to some architecture- or
75 target-specific data structure, but really, a particular GDB
76 process can only run on top of one kernel at a time. So it's okay
77 for this to be a simple variable. */
78 int have_ptrace_getfpxregs =
79 #ifdef HAVE_PTRACE_GETFPXREGS
87 /* Accessing registers through the U area, one at a time. */
89 /* Fetch one register. */
92 fetch_register (struct regcache *regcache, int regno)
97 gdb_assert (!have_ptrace_getregs);
98 if (i386_linux_gregset_reg_offset[regno] == -1)
100 regcache_raw_supply (regcache, regno, NULL);
104 /* GNU/Linux LWP ID's are process ID's. */
105 tid = ptid_get_lwp (inferior_ptid);
107 tid = ptid_get_pid (inferior_ptid); /* Not a threaded program. */
110 val = ptrace (PTRACE_PEEKUSER, tid,
111 i386_linux_gregset_reg_offset[regno], 0);
113 error (_("Couldn't read register %s (#%d): %s."),
114 gdbarch_register_name (get_regcache_arch (regcache), regno),
115 regno, safe_strerror (errno));
117 regcache_raw_supply (regcache, regno, &val);
120 /* Store one register. */
123 store_register (const struct regcache *regcache, int regno)
128 gdb_assert (!have_ptrace_getregs);
129 if (i386_linux_gregset_reg_offset[regno] == -1)
132 /* GNU/Linux LWP ID's are process ID's. */
133 tid = ptid_get_lwp (inferior_ptid);
135 tid = ptid_get_pid (inferior_ptid); /* Not a threaded program. */
138 regcache_raw_collect (regcache, regno, &val);
139 ptrace (PTRACE_POKEUSER, tid,
140 i386_linux_gregset_reg_offset[regno], val);
142 error (_("Couldn't write register %s (#%d): %s."),
143 gdbarch_register_name (get_regcache_arch (regcache), regno),
144 regno, safe_strerror (errno));
148 /* Transfering the general-purpose registers between GDB, inferiors
151 /* Fill GDB's register array with the general-purpose register values
155 supply_gregset (struct regcache *regcache, const elf_gregset_t *gregsetp)
157 const gdb_byte *regp = (const gdb_byte *) gregsetp;
160 for (i = 0; i < I386_NUM_GREGS; i++)
161 regcache_raw_supply (regcache, i,
162 regp + i386_linux_gregset_reg_offset[i]);
164 if (I386_LINUX_ORIG_EAX_REGNUM
165 < gdbarch_num_regs (get_regcache_arch (regcache)))
166 regcache_raw_supply (regcache, I386_LINUX_ORIG_EAX_REGNUM, regp
167 + i386_linux_gregset_reg_offset[I386_LINUX_ORIG_EAX_REGNUM]);
170 /* Fill register REGNO (if it is a general-purpose register) in
171 *GREGSETPS with the value in GDB's register array. If REGNO is -1,
172 do this for all registers. */
175 fill_gregset (const struct regcache *regcache,
176 elf_gregset_t *gregsetp, int regno)
178 gdb_byte *regp = (gdb_byte *) gregsetp;
181 for (i = 0; i < I386_NUM_GREGS; i++)
182 if (regno == -1 || regno == i)
183 regcache_raw_collect (regcache, i,
184 regp + i386_linux_gregset_reg_offset[i]);
186 if ((regno == -1 || regno == I386_LINUX_ORIG_EAX_REGNUM)
187 && I386_LINUX_ORIG_EAX_REGNUM
188 < gdbarch_num_regs (get_regcache_arch (regcache)))
189 regcache_raw_collect (regcache, I386_LINUX_ORIG_EAX_REGNUM, regp
190 + i386_linux_gregset_reg_offset[I386_LINUX_ORIG_EAX_REGNUM]);
193 #ifdef HAVE_PTRACE_GETREGS
195 /* Fetch all general-purpose registers from process/thread TID and
196 store their values in GDB's register array. */
199 fetch_regs (struct regcache *regcache, int tid)
202 elf_gregset_t *regs_p = ®s;
204 if (ptrace (PTRACE_GETREGS, tid, 0, (int) ®s) < 0)
208 /* The kernel we're running on doesn't support the GETREGS
209 request. Reset `have_ptrace_getregs'. */
210 have_ptrace_getregs = 0;
214 perror_with_name (_("Couldn't get registers"));
217 supply_gregset (regcache, (const elf_gregset_t *) regs_p);
220 /* Store all valid general-purpose registers in GDB's register array
221 into the process/thread specified by TID. */
224 store_regs (const struct regcache *regcache, int tid, int regno)
228 if (ptrace (PTRACE_GETREGS, tid, 0, (int) ®s) < 0)
229 perror_with_name (_("Couldn't get registers"));
231 fill_gregset (regcache, ®s, regno);
233 if (ptrace (PTRACE_SETREGS, tid, 0, (int) ®s) < 0)
234 perror_with_name (_("Couldn't write registers"));
239 static void fetch_regs (struct regcache *regcache, int tid) {}
240 static void store_regs (const struct regcache *regcache, int tid, int regno) {}
245 /* Transfering floating-point registers between GDB, inferiors and cores. */
247 /* Fill GDB's register array with the floating-point register values in
251 supply_fpregset (struct regcache *regcache, const elf_fpregset_t *fpregsetp)
253 i387_supply_fsave (regcache, -1, fpregsetp);
256 /* Fill register REGNO (if it is a floating-point register) in
257 *FPREGSETP with the value in GDB's register array. If REGNO is -1,
258 do this for all registers. */
261 fill_fpregset (const struct regcache *regcache,
262 elf_fpregset_t *fpregsetp, int regno)
264 i387_collect_fsave (regcache, regno, fpregsetp);
267 #ifdef HAVE_PTRACE_GETREGS
269 /* Fetch all floating-point registers from process/thread TID and store
270 thier values in GDB's register array. */
273 fetch_fpregs (struct regcache *regcache, int tid)
275 elf_fpregset_t fpregs;
277 if (ptrace (PTRACE_GETFPREGS, tid, 0, (int) &fpregs) < 0)
278 perror_with_name (_("Couldn't get floating point status"));
280 supply_fpregset (regcache, (const elf_fpregset_t *) &fpregs);
283 /* Store all valid floating-point registers in GDB's register array
284 into the process/thread specified by TID. */
287 store_fpregs (const struct regcache *regcache, int tid, int regno)
289 elf_fpregset_t fpregs;
291 if (ptrace (PTRACE_GETFPREGS, tid, 0, (int) &fpregs) < 0)
292 perror_with_name (_("Couldn't get floating point status"));
294 fill_fpregset (regcache, &fpregs, regno);
296 if (ptrace (PTRACE_SETFPREGS, tid, 0, (int) &fpregs) < 0)
297 perror_with_name (_("Couldn't write floating point status"));
303 fetch_fpregs (struct regcache *regcache, int tid)
308 store_fpregs (const struct regcache *regcache, int tid, int regno)
315 /* Transfering floating-point and SSE registers to and from GDB. */
317 /* Fetch all registers covered by the PTRACE_GETREGSET request from
318 process/thread TID and store their values in GDB's register array.
319 Return non-zero if successful, zero otherwise. */
322 fetch_xstateregs (struct regcache *regcache, int tid)
324 char xstateregs[X86_XSTATE_MAX_SIZE];
327 if (!have_ptrace_getregset)
330 iov.iov_base = xstateregs;
331 iov.iov_len = sizeof(xstateregs);
332 if (ptrace (PTRACE_GETREGSET, tid, (unsigned int) NT_X86_XSTATE,
334 perror_with_name (_("Couldn't read extended state status"));
336 i387_supply_xsave (regcache, -1, xstateregs);
340 /* Store all valid registers in GDB's register array covered by the
341 PTRACE_SETREGSET request into the process/thread specified by TID.
342 Return non-zero if successful, zero otherwise. */
345 store_xstateregs (const struct regcache *regcache, int tid, int regno)
347 char xstateregs[X86_XSTATE_MAX_SIZE];
350 if (!have_ptrace_getregset)
353 iov.iov_base = xstateregs;
354 iov.iov_len = sizeof(xstateregs);
355 if (ptrace (PTRACE_GETREGSET, tid, (unsigned int) NT_X86_XSTATE,
357 perror_with_name (_("Couldn't read extended state status"));
359 i387_collect_xsave (regcache, regno, xstateregs, 0);
361 if (ptrace (PTRACE_SETREGSET, tid, (unsigned int) NT_X86_XSTATE,
363 perror_with_name (_("Couldn't write extended state status"));
368 #ifdef HAVE_PTRACE_GETFPXREGS
370 /* Fetch all registers covered by the PTRACE_GETFPXREGS request from
371 process/thread TID and store their values in GDB's register array.
372 Return non-zero if successful, zero otherwise. */
375 fetch_fpxregs (struct regcache *regcache, int tid)
377 elf_fpxregset_t fpxregs;
379 if (! have_ptrace_getfpxregs)
382 if (ptrace (PTRACE_GETFPXREGS, tid, 0, (int) &fpxregs) < 0)
386 have_ptrace_getfpxregs = 0;
390 perror_with_name (_("Couldn't read floating-point and SSE registers"));
393 i387_supply_fxsave (regcache, -1, (const elf_fpxregset_t *) &fpxregs);
397 /* Store all valid registers in GDB's register array covered by the
398 PTRACE_SETFPXREGS request into the process/thread specified by TID.
399 Return non-zero if successful, zero otherwise. */
402 store_fpxregs (const struct regcache *regcache, int tid, int regno)
404 elf_fpxregset_t fpxregs;
406 if (! have_ptrace_getfpxregs)
409 if (ptrace (PTRACE_GETFPXREGS, tid, 0, &fpxregs) == -1)
413 have_ptrace_getfpxregs = 0;
417 perror_with_name (_("Couldn't read floating-point and SSE registers"));
420 i387_collect_fxsave (regcache, regno, &fpxregs);
422 if (ptrace (PTRACE_SETFPXREGS, tid, 0, &fpxregs) == -1)
423 perror_with_name (_("Couldn't write floating-point and SSE registers"));
431 fetch_fpxregs (struct regcache *regcache, int tid)
437 store_fpxregs (const struct regcache *regcache, int tid, int regno)
442 #endif /* HAVE_PTRACE_GETFPXREGS */
445 /* Transferring arbitrary registers between GDB and inferior. */
447 /* Fetch register REGNO from the child process. If REGNO is -1, do
448 this for all registers (including the floating point and SSE
452 i386_linux_fetch_inferior_registers (struct target_ops *ops,
453 struct regcache *regcache, int regno)
457 /* Use the old method of peeking around in `struct user' if the
458 GETREGS request isn't available. */
459 if (!have_ptrace_getregs)
463 for (i = 0; i < gdbarch_num_regs (get_regcache_arch (regcache)); i++)
464 if (regno == -1 || regno == i)
465 fetch_register (regcache, i);
470 /* GNU/Linux LWP ID's are process ID's. */
471 tid = ptid_get_lwp (inferior_ptid);
473 tid = ptid_get_pid (inferior_ptid); /* Not a threaded program. */
475 /* Use the PTRACE_GETFPXREGS request whenever possible, since it
476 transfers more registers in one system call, and we'll cache the
477 results. But remember that fetch_fpxregs can fail, and return
481 fetch_regs (regcache, tid);
483 /* The call above might reset `have_ptrace_getregs'. */
484 if (!have_ptrace_getregs)
486 i386_linux_fetch_inferior_registers (ops, regcache, regno);
490 if (fetch_xstateregs (regcache, tid))
492 if (fetch_fpxregs (regcache, tid))
494 fetch_fpregs (regcache, tid);
498 if (GETREGS_SUPPLIES (regno))
500 fetch_regs (regcache, tid);
504 if (GETXSTATEREGS_SUPPLIES (regno))
506 if (fetch_xstateregs (regcache, tid))
510 if (GETFPXREGS_SUPPLIES (regno))
512 if (fetch_fpxregs (regcache, tid))
515 /* Either our processor or our kernel doesn't support the SSE
516 registers, so read the FP registers in the traditional way,
517 and fill the SSE registers with dummy values. It would be
518 more graceful to handle differences in the register set using
519 gdbarch. Until then, this will at least make things work
521 fetch_fpregs (regcache, tid);
525 internal_error (__FILE__, __LINE__,
526 _("Got request for bad register number %d."), regno);
529 /* Store register REGNO back into the child process. If REGNO is -1,
530 do this for all registers (including the floating point and SSE
533 i386_linux_store_inferior_registers (struct target_ops *ops,
534 struct regcache *regcache, int regno)
538 /* Use the old method of poking around in `struct user' if the
539 SETREGS request isn't available. */
540 if (!have_ptrace_getregs)
544 for (i = 0; i < gdbarch_num_regs (get_regcache_arch (regcache)); i++)
545 if (regno == -1 || regno == i)
546 store_register (regcache, i);
551 /* GNU/Linux LWP ID's are process ID's. */
552 tid = ptid_get_lwp (inferior_ptid);
554 tid = ptid_get_pid (inferior_ptid); /* Not a threaded program. */
556 /* Use the PTRACE_SETFPXREGS requests whenever possible, since it
557 transfers more registers in one system call. But remember that
558 store_fpxregs can fail, and return zero. */
561 store_regs (regcache, tid, regno);
562 if (store_xstateregs (regcache, tid, regno))
564 if (store_fpxregs (regcache, tid, regno))
566 store_fpregs (regcache, tid, regno);
570 if (GETREGS_SUPPLIES (regno))
572 store_regs (regcache, tid, regno);
576 if (GETXSTATEREGS_SUPPLIES (regno))
578 if (store_xstateregs (regcache, tid, regno))
582 if (GETFPXREGS_SUPPLIES (regno))
584 if (store_fpxregs (regcache, tid, regno))
587 /* Either our processor or our kernel doesn't support the SSE
588 registers, so just write the FP registers in the traditional
590 store_fpregs (regcache, tid, regno);
594 internal_error (__FILE__, __LINE__,
595 _("Got request to store bad register number %d."), regno);
599 /* Called by libthread_db. Returns a pointer to the thread local
600 storage (or its descriptor). */
603 ps_get_thread_area (const struct ps_prochandle *ph,
604 lwpid_t lwpid, int idx, void **base)
606 unsigned int base_addr;
609 result = x86_linux_get_thread_area (lwpid, (void *) idx, &base_addr);
612 *(int *) base = base_addr;
618 /* The instruction for a GNU/Linux system call is:
622 static const unsigned char linux_syscall[] = { 0xcd, 0x80 };
624 #define LINUX_SYSCALL_LEN (sizeof linux_syscall)
626 /* The system call number is stored in the %eax register. */
627 #define LINUX_SYSCALL_REGNUM I386_EAX_REGNUM
629 /* We are specifically interested in the sigreturn and rt_sigreturn
632 #ifndef SYS_sigreturn
633 #define SYS_sigreturn 0x77
635 #ifndef SYS_rt_sigreturn
636 #define SYS_rt_sigreturn 0xad
639 /* Offset to saved processor flags, from <asm/sigcontext.h>. */
640 #define LINUX_SIGCONTEXT_EFLAGS_OFFSET (64)
642 /* Resume execution of the inferior process.
643 If STEP is nonzero, single-step it.
644 If SIGNAL is nonzero, give it that signal. */
647 i386_linux_resume (struct target_ops *ops,
648 ptid_t ptid, int step, enum gdb_signal signal)
650 int pid = ptid_get_pid (ptid);
654 if (catch_syscall_enabled () > 0)
655 request = PTRACE_SYSCALL;
657 request = PTRACE_CONT;
661 struct regcache *regcache = get_thread_regcache (pid_to_ptid (pid));
662 struct gdbarch *gdbarch = get_regcache_arch (regcache);
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"));
716 /* -Wmissing-prototypes */
717 extern initialize_file_ftype _initialize_i386_linux_nat;
720 _initialize_i386_linux_nat (void)
722 /* Create a generic x86 GNU/Linux target. */
723 struct target_ops *t = x86_linux_create_target ();
725 /* Override the default ptrace resume method. */
726 t->to_resume = i386_linux_resume;
728 /* Add our register access methods. */
729 t->to_fetch_registers = i386_linux_fetch_inferior_registers;
730 t->to_store_registers = i386_linux_store_inferior_registers;
732 /* Add the target. */
733 x86_linux_add_target (t);