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"
40 /* The register sets used in GNU/Linux ELF core-dumps are identical to
41 the register sets in `struct user' that is used for a.out
42 core-dumps, and is also used by `ptrace'. The corresponding types
43 are `elf_gregset_t' for the general-purpose registers (with
44 `elf_greg_t' the type of a single GP register) and `elf_fpregset_t'
45 for the floating-point registers.
47 Those types used to be available under the names `gregset_t' and
48 `fpregset_t' too, and this file used those names in the past. But
49 those names are now used for the register sets used in the
50 `mcontext_t' type, and have a different size and layout. */
52 /* Which ptrace request retrieves which registers?
53 These apply to the corresponding SET requests as well. */
55 #define GETREGS_SUPPLIES(regno) \
56 ((0 <= (regno) && (regno) <= 15) || (regno) == I386_LINUX_ORIG_EAX_REGNUM)
58 #define GETFPXREGS_SUPPLIES(regno) \
59 (I386_ST0_REGNUM <= (regno) && (regno) < I386_SSE_NUM_REGS)
61 #define GETXSTATEREGS_SUPPLIES(regno) \
62 (I386_ST0_REGNUM <= (regno) && (regno) < I386_PKEYS_NUM_REGS)
64 /* Does the current host support the GETREGS request? */
65 int have_ptrace_getregs =
66 #ifdef HAVE_PTRACE_GETREGS
73 /* Does the current host support the GETFPXREGS request? The header
74 file may or may not define it, and even if it is defined, the
75 kernel will return EIO if it's running on a pre-SSE processor.
77 My instinct is to attach this to some architecture- or
78 target-specific data structure, but really, a particular GDB
79 process can only run on top of one kernel at a time. So it's okay
80 for this to be a simple variable. */
81 int have_ptrace_getfpxregs =
82 #ifdef HAVE_PTRACE_GETFPXREGS
90 /* Accessing registers through the U area, one at a time. */
92 /* Fetch one register. */
95 fetch_register (struct regcache *regcache, int regno)
100 gdb_assert (!have_ptrace_getregs);
101 if (i386_linux_gregset_reg_offset[regno] == -1)
103 regcache_raw_supply (regcache, regno, NULL);
107 /* GNU/Linux LWP ID's are process ID's. */
108 tid = ptid_get_lwp (inferior_ptid);
110 tid = ptid_get_pid (inferior_ptid); /* Not a threaded program. */
113 val = ptrace (PTRACE_PEEKUSER, tid,
114 i386_linux_gregset_reg_offset[regno], 0);
116 error (_("Couldn't read register %s (#%d): %s."),
117 gdbarch_register_name (get_regcache_arch (regcache), regno),
118 regno, safe_strerror (errno));
120 regcache_raw_supply (regcache, regno, &val);
123 /* Store one register. */
126 store_register (const struct regcache *regcache, int regno)
131 gdb_assert (!have_ptrace_getregs);
132 if (i386_linux_gregset_reg_offset[regno] == -1)
135 /* GNU/Linux LWP ID's are process ID's. */
136 tid = ptid_get_lwp (inferior_ptid);
138 tid = ptid_get_pid (inferior_ptid); /* Not a threaded program. */
141 regcache_raw_collect (regcache, regno, &val);
142 ptrace (PTRACE_POKEUSER, tid,
143 i386_linux_gregset_reg_offset[regno], val);
145 error (_("Couldn't write register %s (#%d): %s."),
146 gdbarch_register_name (get_regcache_arch (regcache), regno),
147 regno, safe_strerror (errno));
151 /* Transfering the general-purpose registers between GDB, inferiors
154 /* Fill GDB's register array with the general-purpose register values
158 supply_gregset (struct regcache *regcache, const elf_gregset_t *gregsetp)
160 const gdb_byte *regp = (const gdb_byte *) gregsetp;
163 for (i = 0; i < I386_NUM_GREGS; i++)
164 regcache_raw_supply (regcache, i,
165 regp + i386_linux_gregset_reg_offset[i]);
167 if (I386_LINUX_ORIG_EAX_REGNUM
168 < gdbarch_num_regs (get_regcache_arch (regcache)))
169 regcache_raw_supply (regcache, I386_LINUX_ORIG_EAX_REGNUM, regp
170 + i386_linux_gregset_reg_offset[I386_LINUX_ORIG_EAX_REGNUM]);
173 /* Fill register REGNO (if it is a general-purpose register) in
174 *GREGSETPS with the value in GDB's register array. If REGNO is -1,
175 do this for all registers. */
178 fill_gregset (const struct regcache *regcache,
179 elf_gregset_t *gregsetp, int regno)
181 gdb_byte *regp = (gdb_byte *) gregsetp;
184 for (i = 0; i < I386_NUM_GREGS; i++)
185 if (regno == -1 || regno == i)
186 regcache_raw_collect (regcache, i,
187 regp + i386_linux_gregset_reg_offset[i]);
189 if ((regno == -1 || regno == I386_LINUX_ORIG_EAX_REGNUM)
190 && I386_LINUX_ORIG_EAX_REGNUM
191 < gdbarch_num_regs (get_regcache_arch (regcache)))
192 regcache_raw_collect (regcache, I386_LINUX_ORIG_EAX_REGNUM, regp
193 + i386_linux_gregset_reg_offset[I386_LINUX_ORIG_EAX_REGNUM]);
196 #ifdef HAVE_PTRACE_GETREGS
198 /* Fetch all general-purpose registers from process/thread TID and
199 store their values in GDB's register array. */
202 fetch_regs (struct regcache *regcache, int tid)
205 elf_gregset_t *regs_p = ®s;
207 if (ptrace (PTRACE_GETREGS, tid, 0, (int) ®s) < 0)
211 /* The kernel we're running on doesn't support the GETREGS
212 request. Reset `have_ptrace_getregs'. */
213 have_ptrace_getregs = 0;
217 perror_with_name (_("Couldn't get registers"));
220 supply_gregset (regcache, (const elf_gregset_t *) regs_p);
223 /* Store all valid general-purpose registers in GDB's register array
224 into the process/thread specified by TID. */
227 store_regs (const struct regcache *regcache, int tid, int regno)
231 if (ptrace (PTRACE_GETREGS, tid, 0, (int) ®s) < 0)
232 perror_with_name (_("Couldn't get registers"));
234 fill_gregset (regcache, ®s, regno);
236 if (ptrace (PTRACE_SETREGS, tid, 0, (int) ®s) < 0)
237 perror_with_name (_("Couldn't write registers"));
242 static void fetch_regs (struct regcache *regcache, int tid) {}
243 static void store_regs (const struct regcache *regcache, int tid, int regno) {}
248 /* Transfering floating-point registers between GDB, inferiors and cores. */
250 /* Fill GDB's register array with the floating-point register values in
254 supply_fpregset (struct regcache *regcache, const elf_fpregset_t *fpregsetp)
256 i387_supply_fsave (regcache, -1, fpregsetp);
259 /* Fill register REGNO (if it is a floating-point register) in
260 *FPREGSETP with the value in GDB's register array. If REGNO is -1,
261 do this for all registers. */
264 fill_fpregset (const struct regcache *regcache,
265 elf_fpregset_t *fpregsetp, int regno)
267 i387_collect_fsave (regcache, regno, fpregsetp);
270 #ifdef HAVE_PTRACE_GETREGS
272 /* Fetch all floating-point registers from process/thread TID and store
273 thier values in GDB's register array. */
276 fetch_fpregs (struct regcache *regcache, int tid)
278 elf_fpregset_t fpregs;
280 if (ptrace (PTRACE_GETFPREGS, tid, 0, (int) &fpregs) < 0)
281 perror_with_name (_("Couldn't get floating point status"));
283 supply_fpregset (regcache, (const elf_fpregset_t *) &fpregs);
286 /* Store all valid floating-point registers in GDB's register array
287 into the process/thread specified by TID. */
290 store_fpregs (const struct regcache *regcache, int tid, int regno)
292 elf_fpregset_t fpregs;
294 if (ptrace (PTRACE_GETFPREGS, tid, 0, (int) &fpregs) < 0)
295 perror_with_name (_("Couldn't get floating point status"));
297 fill_fpregset (regcache, &fpregs, regno);
299 if (ptrace (PTRACE_SETFPREGS, tid, 0, (int) &fpregs) < 0)
300 perror_with_name (_("Couldn't write floating point status"));
306 fetch_fpregs (struct regcache *regcache, int tid)
311 store_fpregs (const struct regcache *regcache, int tid, int regno)
318 /* Transfering floating-point and SSE registers to and from GDB. */
320 /* Fetch all registers covered by the PTRACE_GETREGSET request from
321 process/thread TID and store their values in GDB's register array.
322 Return non-zero if successful, zero otherwise. */
325 fetch_xstateregs (struct regcache *regcache, int tid)
327 char xstateregs[X86_XSTATE_MAX_SIZE];
330 if (have_ptrace_getregset != TRIBOOL_TRUE)
333 iov.iov_base = xstateregs;
334 iov.iov_len = sizeof(xstateregs);
335 if (ptrace (PTRACE_GETREGSET, tid, (unsigned int) NT_X86_XSTATE,
337 perror_with_name (_("Couldn't read extended state status"));
339 i387_supply_xsave (regcache, -1, xstateregs);
343 /* Store all valid registers in GDB's register array covered by the
344 PTRACE_SETREGSET request into the process/thread specified by TID.
345 Return non-zero if successful, zero otherwise. */
348 store_xstateregs (const struct regcache *regcache, int tid, int regno)
350 char xstateregs[X86_XSTATE_MAX_SIZE];
353 if (have_ptrace_getregset != TRIBOOL_TRUE)
356 iov.iov_base = xstateregs;
357 iov.iov_len = sizeof(xstateregs);
358 if (ptrace (PTRACE_GETREGSET, tid, (unsigned int) NT_X86_XSTATE,
360 perror_with_name (_("Couldn't read extended state status"));
362 i387_collect_xsave (regcache, regno, xstateregs, 0);
364 if (ptrace (PTRACE_SETREGSET, tid, (unsigned int) NT_X86_XSTATE,
366 perror_with_name (_("Couldn't write extended state status"));
371 #ifdef HAVE_PTRACE_GETFPXREGS
373 /* Fetch all registers covered by the PTRACE_GETFPXREGS request from
374 process/thread TID and store their values in GDB's register array.
375 Return non-zero if successful, zero otherwise. */
378 fetch_fpxregs (struct regcache *regcache, int tid)
380 elf_fpxregset_t fpxregs;
382 if (! have_ptrace_getfpxregs)
385 if (ptrace (PTRACE_GETFPXREGS, tid, 0, (int) &fpxregs) < 0)
389 have_ptrace_getfpxregs = 0;
393 perror_with_name (_("Couldn't read floating-point and SSE registers"));
396 i387_supply_fxsave (regcache, -1, (const elf_fpxregset_t *) &fpxregs);
400 /* Store all valid registers in GDB's register array covered by the
401 PTRACE_SETFPXREGS request into the process/thread specified by TID.
402 Return non-zero if successful, zero otherwise. */
405 store_fpxregs (const struct regcache *regcache, int tid, int regno)
407 elf_fpxregset_t fpxregs;
409 if (! have_ptrace_getfpxregs)
412 if (ptrace (PTRACE_GETFPXREGS, tid, 0, &fpxregs) == -1)
416 have_ptrace_getfpxregs = 0;
420 perror_with_name (_("Couldn't read floating-point and SSE registers"));
423 i387_collect_fxsave (regcache, regno, &fpxregs);
425 if (ptrace (PTRACE_SETFPXREGS, tid, 0, &fpxregs) == -1)
426 perror_with_name (_("Couldn't write floating-point and SSE registers"));
434 fetch_fpxregs (struct regcache *regcache, int tid)
440 store_fpxregs (const struct regcache *regcache, int tid, int regno)
445 #endif /* HAVE_PTRACE_GETFPXREGS */
448 /* Transferring arbitrary registers between GDB and inferior. */
450 /* Fetch register REGNO from the child process. If REGNO is -1, do
451 this for all registers (including the floating point and SSE
455 i386_linux_fetch_inferior_registers (struct target_ops *ops,
456 struct regcache *regcache, int regno)
460 /* Use the old method of peeking around in `struct user' if the
461 GETREGS request isn't available. */
462 if (!have_ptrace_getregs)
466 for (i = 0; i < gdbarch_num_regs (get_regcache_arch (regcache)); i++)
467 if (regno == -1 || regno == i)
468 fetch_register (regcache, i);
473 /* GNU/Linux LWP ID's are process ID's. */
474 tid = ptid_get_lwp (inferior_ptid);
476 tid = ptid_get_pid (inferior_ptid); /* Not a threaded program. */
478 /* Use the PTRACE_GETFPXREGS request whenever possible, since it
479 transfers more registers in one system call, and we'll cache the
480 results. But remember that fetch_fpxregs can fail, and return
484 fetch_regs (regcache, tid);
486 /* The call above might reset `have_ptrace_getregs'. */
487 if (!have_ptrace_getregs)
489 i386_linux_fetch_inferior_registers (ops, regcache, regno);
493 if (fetch_xstateregs (regcache, tid))
495 if (fetch_fpxregs (regcache, tid))
497 fetch_fpregs (regcache, tid);
501 if (GETREGS_SUPPLIES (regno))
503 fetch_regs (regcache, tid);
507 if (GETXSTATEREGS_SUPPLIES (regno))
509 if (fetch_xstateregs (regcache, tid))
513 if (GETFPXREGS_SUPPLIES (regno))
515 if (fetch_fpxregs (regcache, tid))
518 /* Either our processor or our kernel doesn't support the SSE
519 registers, so read the FP registers in the traditional way,
520 and fill the SSE registers with dummy values. It would be
521 more graceful to handle differences in the register set using
522 gdbarch. Until then, this will at least make things work
524 fetch_fpregs (regcache, tid);
528 internal_error (__FILE__, __LINE__,
529 _("Got request for bad register number %d."), regno);
532 /* Store register REGNO back into the child process. If REGNO is -1,
533 do this for all registers (including the floating point and SSE
536 i386_linux_store_inferior_registers (struct target_ops *ops,
537 struct regcache *regcache, int regno)
541 /* Use the old method of poking around in `struct user' if the
542 SETREGS request isn't available. */
543 if (!have_ptrace_getregs)
547 for (i = 0; i < gdbarch_num_regs (get_regcache_arch (regcache)); i++)
548 if (regno == -1 || regno == i)
549 store_register (regcache, i);
554 /* GNU/Linux LWP ID's are process ID's. */
555 tid = ptid_get_lwp (inferior_ptid);
557 tid = ptid_get_pid (inferior_ptid); /* Not a threaded program. */
559 /* Use the PTRACE_SETFPXREGS requests whenever possible, since it
560 transfers more registers in one system call. But remember that
561 store_fpxregs can fail, and return zero. */
564 store_regs (regcache, tid, regno);
565 if (store_xstateregs (regcache, tid, regno))
567 if (store_fpxregs (regcache, tid, regno))
569 store_fpregs (regcache, tid, regno);
573 if (GETREGS_SUPPLIES (regno))
575 store_regs (regcache, tid, regno);
579 if (GETXSTATEREGS_SUPPLIES (regno))
581 if (store_xstateregs (regcache, tid, regno))
585 if (GETFPXREGS_SUPPLIES (regno))
587 if (store_fpxregs (regcache, tid, regno))
590 /* Either our processor or our kernel doesn't support the SSE
591 registers, so just write the FP registers in the traditional
593 store_fpregs (regcache, tid, regno);
597 internal_error (__FILE__, __LINE__,
598 _("Got request to store bad register number %d."), regno);
602 /* Called by libthread_db. Returns a pointer to the thread local
603 storage (or its descriptor). */
606 ps_get_thread_area (struct ps_prochandle *ph,
607 lwpid_t lwpid, int idx, void **base)
609 unsigned int base_addr;
612 result = x86_linux_get_thread_area (lwpid, (void *) idx, &base_addr);
615 *(int *) base = base_addr;
621 /* The instruction for a GNU/Linux system call is:
625 static const unsigned char linux_syscall[] = { 0xcd, 0x80 };
627 #define LINUX_SYSCALL_LEN (sizeof linux_syscall)
629 /* The system call number is stored in the %eax register. */
630 #define LINUX_SYSCALL_REGNUM I386_EAX_REGNUM
632 /* We are specifically interested in the sigreturn and rt_sigreturn
635 #ifndef SYS_sigreturn
636 #define SYS_sigreturn 0x77
638 #ifndef SYS_rt_sigreturn
639 #define SYS_rt_sigreturn 0xad
642 /* Offset to saved processor flags, from <asm/sigcontext.h>. */
643 #define LINUX_SIGCONTEXT_EFLAGS_OFFSET (64)
645 /* Resume execution of the inferior process.
646 If STEP is nonzero, single-step it.
647 If SIGNAL is nonzero, give it that signal. */
650 i386_linux_resume (struct target_ops *ops,
651 ptid_t ptid, int step, enum gdb_signal signal)
653 int pid = ptid_get_lwp (ptid);
656 if (catch_syscall_enabled () > 0)
657 request = PTRACE_SYSCALL;
659 request = PTRACE_CONT;
663 struct regcache *regcache = get_thread_regcache (ptid);
664 struct gdbarch *gdbarch = get_regcache_arch (regcache);
665 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
667 gdb_byte buf[LINUX_SYSCALL_LEN];
669 request = PTRACE_SINGLESTEP;
671 regcache_cooked_read_unsigned (regcache,
672 gdbarch_pc_regnum (gdbarch), &pc);
674 /* Returning from a signal trampoline is done by calling a
675 special system call (sigreturn or rt_sigreturn, see
676 i386-linux-tdep.c for more information). This system call
677 restores the registers that were saved when the signal was
678 raised, including %eflags. That means that single-stepping
679 won't work. Instead, we'll have to modify the signal context
680 that's about to be restored, and set the trace flag there. */
682 /* First check if PC is at a system call. */
683 if (target_read_memory (pc, buf, LINUX_SYSCALL_LEN) == 0
684 && memcmp (buf, linux_syscall, LINUX_SYSCALL_LEN) == 0)
687 regcache_cooked_read_unsigned (regcache,
688 LINUX_SYSCALL_REGNUM, &syscall);
690 /* Then check the system call number. */
691 if (syscall == SYS_sigreturn || syscall == SYS_rt_sigreturn)
694 unsigned long int eflags;
696 regcache_cooked_read_unsigned (regcache, I386_ESP_REGNUM, &sp);
697 if (syscall == SYS_rt_sigreturn)
698 addr = read_memory_unsigned_integer (sp + 8, 4, byte_order)
703 /* Set the trace flag in the context that's about to be
705 addr += LINUX_SIGCONTEXT_EFLAGS_OFFSET;
706 read_memory (addr, (gdb_byte *) &eflags, 4);
708 write_memory (addr, (gdb_byte *) &eflags, 4);
713 if (ptrace (request, pid, 0, gdb_signal_to_host (signal)) == -1)
714 perror_with_name (("ptrace"));
718 /* -Wmissing-prototypes */
719 extern initialize_file_ftype _initialize_i386_linux_nat;
722 _initialize_i386_linux_nat (void)
724 /* Create a generic x86 GNU/Linux target. */
725 struct target_ops *t = x86_linux_create_target ();
727 /* Override the default ptrace resume method. */
728 t->to_resume = i386_linux_resume;
730 /* Add our register access methods. */
731 t->to_fetch_registers = i386_linux_fetch_inferior_registers;
732 t->to_store_registers = i386_linux_store_inferior_registers;
734 /* Add the target. */
735 x86_linux_add_target (t);