1 /* Native-dependent code for GNU/Linux i386.
3 Copyright (C) 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008,
4 2009, 2010 Free Software Foundation, Inc.
6 This file is part of GDB.
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>. */
28 #include "linux-nat.h"
30 #include "gdb_assert.h"
31 #include "gdb_string.h"
32 #include "elf/common.h"
34 #include <sys/ptrace.h>
36 #include <sys/procfs.h>
46 #ifdef HAVE_SYS_DEBUGREG_H
47 #include <sys/debugreg.h>
51 #define DR_FIRSTADDR 0
66 /* Prototypes for supply_gregset etc. */
69 #include "i387-tdep.h"
70 #include "i386-tdep.h"
71 #include "i386-linux-tdep.h"
73 /* Defines ps_err_e, struct ps_prochandle. */
74 #include "gdb_proc_service.h"
76 #include "i386-xstate.h"
78 #ifndef PTRACE_GETREGSET
79 #define PTRACE_GETREGSET 0x4204
82 #ifndef PTRACE_SETREGSET
83 #define PTRACE_SETREGSET 0x4205
86 /* Does the current host support PTRACE_GETREGSET? */
87 static int have_ptrace_getregset = -1;
90 /* The register sets used in GNU/Linux ELF core-dumps are identical to
91 the register sets in `struct user' that is used for a.out
92 core-dumps, and is also used by `ptrace'. The corresponding types
93 are `elf_gregset_t' for the general-purpose registers (with
94 `elf_greg_t' the type of a single GP register) and `elf_fpregset_t'
95 for the floating-point registers.
97 Those types used to be available under the names `gregset_t' and
98 `fpregset_t' too, and this file used those names in the past. But
99 those names are now used for the register sets used in the
100 `mcontext_t' type, and have a different size and layout. */
102 /* Mapping between the general-purpose registers in `struct user'
103 format and GDB's register array layout. */
104 static int regmap[] =
110 -1, -1, -1, -1, /* st0, st1, st2, st3 */
111 -1, -1, -1, -1, /* st4, st5, st6, st7 */
112 -1, -1, -1, -1, /* fctrl, fstat, ftag, fiseg */
113 -1, -1, -1, -1, /* fioff, foseg, fooff, fop */
114 -1, -1, -1, -1, /* xmm0, xmm1, xmm2, xmm3 */
115 -1, -1, -1, -1, /* xmm4, xmm5, xmm6, xmm6 */
117 -1, -1, -1, -1, /* ymm0h, ymm1h, ymm2h, ymm3h */
118 -1, -1, -1, -1, /* ymm4h, ymm5h, ymm6h, ymm6h */
122 /* Which ptrace request retrieves which registers?
123 These apply to the corresponding SET requests as well. */
125 #define GETREGS_SUPPLIES(regno) \
126 ((0 <= (regno) && (regno) <= 15) || (regno) == I386_LINUX_ORIG_EAX_REGNUM)
128 #define GETFPXREGS_SUPPLIES(regno) \
129 (I386_ST0_REGNUM <= (regno) && (regno) < I386_SSE_NUM_REGS)
131 #define GETXSTATEREGS_SUPPLIES(regno) \
132 (I386_ST0_REGNUM <= (regno) && (regno) < I386_AVX_NUM_REGS)
134 /* Does the current host support the GETREGS request? */
135 int have_ptrace_getregs =
136 #ifdef HAVE_PTRACE_GETREGS
143 /* Does the current host support the GETFPXREGS request? The header
144 file may or may not define it, and even if it is defined, the
145 kernel will return EIO if it's running on a pre-SSE processor.
147 My instinct is to attach this to some architecture- or
148 target-specific data structure, but really, a particular GDB
149 process can only run on top of one kernel at a time. So it's okay
150 for this to be a simple variable. */
151 int have_ptrace_getfpxregs =
152 #ifdef HAVE_PTRACE_GETFPXREGS
160 /* Accessing registers through the U area, one at a time. */
162 /* Fetch one register. */
165 fetch_register (struct regcache *regcache, int regno)
170 gdb_assert (!have_ptrace_getregs);
171 if (regmap[regno] == -1)
173 regcache_raw_supply (regcache, regno, NULL);
177 /* GNU/Linux LWP ID's are process ID's. */
178 tid = TIDGET (inferior_ptid);
180 tid = PIDGET (inferior_ptid); /* Not a threaded program. */
183 val = ptrace (PTRACE_PEEKUSER, tid, 4 * regmap[regno], 0);
185 error (_("Couldn't read register %s (#%d): %s."),
186 gdbarch_register_name (get_regcache_arch (regcache), regno),
187 regno, safe_strerror (errno));
189 regcache_raw_supply (regcache, regno, &val);
192 /* Store one register. */
195 store_register (const struct regcache *regcache, int regno)
200 gdb_assert (!have_ptrace_getregs);
201 if (regmap[regno] == -1)
204 /* GNU/Linux LWP ID's are process ID's. */
205 tid = TIDGET (inferior_ptid);
207 tid = PIDGET (inferior_ptid); /* Not a threaded program. */
210 regcache_raw_collect (regcache, regno, &val);
211 ptrace (PTRACE_POKEUSER, tid, 4 * regmap[regno], val);
213 error (_("Couldn't write register %s (#%d): %s."),
214 gdbarch_register_name (get_regcache_arch (regcache), regno),
215 regno, safe_strerror (errno));
219 /* Transfering the general-purpose registers between GDB, inferiors
222 /* Fill GDB's register array with the general-purpose register values
226 supply_gregset (struct regcache *regcache, const elf_gregset_t *gregsetp)
228 const elf_greg_t *regp = (const elf_greg_t *) gregsetp;
231 for (i = 0; i < I386_NUM_GREGS; i++)
232 regcache_raw_supply (regcache, i, regp + regmap[i]);
234 if (I386_LINUX_ORIG_EAX_REGNUM
235 < gdbarch_num_regs (get_regcache_arch (regcache)))
236 regcache_raw_supply (regcache, I386_LINUX_ORIG_EAX_REGNUM,
240 /* Fill register REGNO (if it is a general-purpose register) in
241 *GREGSETPS with the value in GDB's register array. If REGNO is -1,
242 do this for all registers. */
245 fill_gregset (const struct regcache *regcache,
246 elf_gregset_t *gregsetp, int regno)
248 elf_greg_t *regp = (elf_greg_t *) gregsetp;
251 for (i = 0; i < I386_NUM_GREGS; i++)
252 if (regno == -1 || regno == i)
253 regcache_raw_collect (regcache, i, regp + regmap[i]);
255 if ((regno == -1 || regno == I386_LINUX_ORIG_EAX_REGNUM)
256 && I386_LINUX_ORIG_EAX_REGNUM
257 < gdbarch_num_regs (get_regcache_arch (regcache)))
258 regcache_raw_collect (regcache, I386_LINUX_ORIG_EAX_REGNUM,
262 #ifdef HAVE_PTRACE_GETREGS
264 /* Fetch all general-purpose registers from process/thread TID and
265 store their values in GDB's register array. */
268 fetch_regs (struct regcache *regcache, int tid)
271 elf_gregset_t *regs_p = ®s;
273 if (ptrace (PTRACE_GETREGS, tid, 0, (int) ®s) < 0)
277 /* The kernel we're running on doesn't support the GETREGS
278 request. Reset `have_ptrace_getregs'. */
279 have_ptrace_getregs = 0;
283 perror_with_name (_("Couldn't get registers"));
286 supply_gregset (regcache, (const elf_gregset_t *) regs_p);
289 /* Store all valid general-purpose registers in GDB's register array
290 into the process/thread specified by TID. */
293 store_regs (const struct regcache *regcache, int tid, int regno)
297 if (ptrace (PTRACE_GETREGS, tid, 0, (int) ®s) < 0)
298 perror_with_name (_("Couldn't get registers"));
300 fill_gregset (regcache, ®s, regno);
302 if (ptrace (PTRACE_SETREGS, tid, 0, (int) ®s) < 0)
303 perror_with_name (_("Couldn't write registers"));
308 static void fetch_regs (struct regcache *regcache, int tid) {}
309 static void store_regs (const struct regcache *regcache, int tid, int regno) {}
314 /* Transfering floating-point registers between GDB, inferiors and cores. */
316 /* Fill GDB's register array with the floating-point register values in
320 supply_fpregset (struct regcache *regcache, const elf_fpregset_t *fpregsetp)
322 i387_supply_fsave (regcache, -1, fpregsetp);
325 /* Fill register REGNO (if it is a floating-point register) in
326 *FPREGSETP with the value in GDB's register array. If REGNO is -1,
327 do this for all registers. */
330 fill_fpregset (const struct regcache *regcache,
331 elf_fpregset_t *fpregsetp, int regno)
333 i387_collect_fsave (regcache, regno, fpregsetp);
336 #ifdef HAVE_PTRACE_GETREGS
338 /* Fetch all floating-point registers from process/thread TID and store
339 thier values in GDB's register array. */
342 fetch_fpregs (struct regcache *regcache, int tid)
344 elf_fpregset_t fpregs;
346 if (ptrace (PTRACE_GETFPREGS, tid, 0, (int) &fpregs) < 0)
347 perror_with_name (_("Couldn't get floating point status"));
349 supply_fpregset (regcache, (const elf_fpregset_t *) &fpregs);
352 /* Store all valid floating-point registers in GDB's register array
353 into the process/thread specified by TID. */
356 store_fpregs (const struct regcache *regcache, int tid, int regno)
358 elf_fpregset_t fpregs;
360 if (ptrace (PTRACE_GETFPREGS, tid, 0, (int) &fpregs) < 0)
361 perror_with_name (_("Couldn't get floating point status"));
363 fill_fpregset (regcache, &fpregs, regno);
365 if (ptrace (PTRACE_SETFPREGS, tid, 0, (int) &fpregs) < 0)
366 perror_with_name (_("Couldn't write floating point status"));
371 static void fetch_fpregs (struct regcache *regcache, int tid) {}
372 static void store_fpregs (const struct regcache *regcache, int tid, int regno) {}
377 /* Transfering floating-point and SSE registers to and from GDB. */
379 /* Fetch all registers covered by the PTRACE_GETREGSET 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_xstateregs (struct regcache *regcache, int tid)
386 char xstateregs[I386_XSTATE_MAX_SIZE];
389 if (!have_ptrace_getregset)
392 iov.iov_base = xstateregs;
393 iov.iov_len = sizeof(xstateregs);
394 if (ptrace (PTRACE_GETREGSET, tid, (unsigned int) NT_X86_XSTATE,
396 perror_with_name (_("Couldn't read extended state status"));
398 i387_supply_xsave (regcache, -1, xstateregs);
402 /* Store all valid registers in GDB's register array covered by the
403 PTRACE_SETREGSET request into the process/thread specified by TID.
404 Return non-zero if successful, zero otherwise. */
407 store_xstateregs (const struct regcache *regcache, int tid, int regno)
409 char xstateregs[I386_XSTATE_MAX_SIZE];
412 if (!have_ptrace_getregset)
415 iov.iov_base = xstateregs;
416 iov.iov_len = sizeof(xstateregs);
417 if (ptrace (PTRACE_GETREGSET, tid, (unsigned int) NT_X86_XSTATE,
419 perror_with_name (_("Couldn't read extended state status"));
421 i387_collect_xsave (regcache, regno, xstateregs, 0);
423 if (ptrace (PTRACE_SETREGSET, tid, (unsigned int) NT_X86_XSTATE,
425 perror_with_name (_("Couldn't write extended state status"));
430 #ifdef HAVE_PTRACE_GETFPXREGS
432 /* Fill GDB's register array with the floating-point and SSE register
433 values in *FPXREGSETP. */
436 supply_fpxregset (struct regcache *regcache,
437 const elf_fpxregset_t *fpxregsetp)
439 i387_supply_fxsave (regcache, -1, fpxregsetp);
442 /* Fill register REGNO (if it is a floating-point or SSE register) in
443 *FPXREGSETP with the value in GDB's register array. If REGNO is
444 -1, do this for all registers. */
447 fill_fpxregset (const struct regcache *regcache,
448 elf_fpxregset_t *fpxregsetp, int regno)
450 i387_collect_fxsave (regcache, regno, fpxregsetp);
453 /* Fetch all registers covered by the PTRACE_GETFPXREGS request from
454 process/thread TID and store their values in GDB's register array.
455 Return non-zero if successful, zero otherwise. */
458 fetch_fpxregs (struct regcache *regcache, int tid)
460 elf_fpxregset_t fpxregs;
462 if (! have_ptrace_getfpxregs)
465 if (ptrace (PTRACE_GETFPXREGS, tid, 0, (int) &fpxregs) < 0)
469 have_ptrace_getfpxregs = 0;
473 perror_with_name (_("Couldn't read floating-point and SSE registers"));
476 supply_fpxregset (regcache, (const elf_fpxregset_t *) &fpxregs);
480 /* Store all valid registers in GDB's register array covered by the
481 PTRACE_SETFPXREGS request into the process/thread specified by TID.
482 Return non-zero if successful, zero otherwise. */
485 store_fpxregs (const struct regcache *regcache, int tid, int regno)
487 elf_fpxregset_t fpxregs;
489 if (! have_ptrace_getfpxregs)
492 if (ptrace (PTRACE_GETFPXREGS, tid, 0, &fpxregs) == -1)
496 have_ptrace_getfpxregs = 0;
500 perror_with_name (_("Couldn't read floating-point and SSE registers"));
503 fill_fpxregset (regcache, &fpxregs, regno);
505 if (ptrace (PTRACE_SETFPXREGS, tid, 0, &fpxregs) == -1)
506 perror_with_name (_("Couldn't write floating-point and SSE registers"));
513 static int fetch_fpxregs (struct regcache *regcache, int tid) { return 0; }
514 static int store_fpxregs (const struct regcache *regcache, int tid, int regno) { return 0; }
516 #endif /* HAVE_PTRACE_GETFPXREGS */
519 /* Transferring arbitrary registers between GDB and inferior. */
521 /* Fetch register REGNO from the child process. If REGNO is -1, do
522 this for all registers (including the floating point and SSE
526 i386_linux_fetch_inferior_registers (struct target_ops *ops,
527 struct regcache *regcache, int regno)
531 /* Use the old method of peeking around in `struct user' if the
532 GETREGS request isn't available. */
533 if (!have_ptrace_getregs)
537 for (i = 0; i < gdbarch_num_regs (get_regcache_arch (regcache)); i++)
538 if (regno == -1 || regno == i)
539 fetch_register (regcache, i);
544 /* GNU/Linux LWP ID's are process ID's. */
545 tid = TIDGET (inferior_ptid);
547 tid = PIDGET (inferior_ptid); /* Not a threaded program. */
549 /* Use the PTRACE_GETFPXREGS request whenever possible, since it
550 transfers more registers in one system call, and we'll cache the
551 results. But remember that fetch_fpxregs can fail, and return
555 fetch_regs (regcache, tid);
557 /* The call above might reset `have_ptrace_getregs'. */
558 if (!have_ptrace_getregs)
560 i386_linux_fetch_inferior_registers (ops, regcache, regno);
564 if (fetch_xstateregs (regcache, tid))
566 if (fetch_fpxregs (regcache, tid))
568 fetch_fpregs (regcache, tid);
572 if (GETREGS_SUPPLIES (regno))
574 fetch_regs (regcache, tid);
578 if (GETXSTATEREGS_SUPPLIES (regno))
580 if (fetch_xstateregs (regcache, tid))
584 if (GETFPXREGS_SUPPLIES (regno))
586 if (fetch_fpxregs (regcache, tid))
589 /* Either our processor or our kernel doesn't support the SSE
590 registers, so read the FP registers in the traditional way,
591 and fill the SSE registers with dummy values. It would be
592 more graceful to handle differences in the register set using
593 gdbarch. Until then, this will at least make things work
595 fetch_fpregs (regcache, tid);
599 internal_error (__FILE__, __LINE__,
600 _("Got request for bad register number %d."), regno);
603 /* Store register REGNO back into the child process. If REGNO is -1,
604 do this for all registers (including the floating point and SSE
607 i386_linux_store_inferior_registers (struct target_ops *ops,
608 struct regcache *regcache, int regno)
612 /* Use the old method of poking around in `struct user' if the
613 SETREGS request isn't available. */
614 if (!have_ptrace_getregs)
618 for (i = 0; i < gdbarch_num_regs (get_regcache_arch (regcache)); i++)
619 if (regno == -1 || regno == i)
620 store_register (regcache, i);
625 /* GNU/Linux LWP ID's are process ID's. */
626 tid = TIDGET (inferior_ptid);
628 tid = PIDGET (inferior_ptid); /* Not a threaded program. */
630 /* Use the PTRACE_SETFPXREGS requests whenever possible, since it
631 transfers more registers in one system call. But remember that
632 store_fpxregs can fail, and return zero. */
635 store_regs (regcache, tid, regno);
636 if (store_xstateregs (regcache, tid, regno))
638 if (store_fpxregs (regcache, tid, regno))
640 store_fpregs (regcache, tid, regno);
644 if (GETREGS_SUPPLIES (regno))
646 store_regs (regcache, tid, regno);
650 if (GETXSTATEREGS_SUPPLIES (regno))
652 if (store_xstateregs (regcache, tid, regno))
656 if (GETFPXREGS_SUPPLIES (regno))
658 if (store_fpxregs (regcache, tid, regno))
661 /* Either our processor or our kernel doesn't support the SSE
662 registers, so just write the FP registers in the traditional
664 store_fpregs (regcache, tid, regno);
668 internal_error (__FILE__, __LINE__,
669 _("Got request to store bad register number %d."), regno);
673 /* Support for debug registers. */
675 static unsigned long i386_linux_dr[DR_CONTROL + 1];
677 /* Get debug register REGNUM value from only the one LWP of PTID. */
680 i386_linux_dr_get (ptid_t ptid, int regnum)
689 /* FIXME: kettenis/2001-03-27: Calling perror_with_name if the
690 ptrace call fails breaks debugging remote targets. The correct
691 way to fix this is to add the hardware breakpoint and watchpoint
692 stuff to the target vector. For now, just return zero if the
693 ptrace call fails. */
695 value = ptrace (PTRACE_PEEKUSER, tid,
696 offsetof (struct user, u_debugreg[regnum]), 0);
699 perror_with_name (_("Couldn't read debug register"));
707 /* Set debug register REGNUM to VALUE in only the one LWP of PTID. */
710 i386_linux_dr_set (ptid_t ptid, int regnum, unsigned long value)
719 ptrace (PTRACE_POKEUSER, tid,
720 offsetof (struct user, u_debugreg[regnum]), value);
722 perror_with_name (_("Couldn't write debug register"));
725 /* Set DR_CONTROL to ADDR in all LWPs of LWP_LIST. */
728 i386_linux_dr_set_control (unsigned long control)
733 i386_linux_dr[DR_CONTROL] = control;
735 i386_linux_dr_set (ptid, DR_CONTROL, control);
738 /* Set address REGNUM (zero based) to ADDR in all LWPs of LWP_LIST. */
741 i386_linux_dr_set_addr (int regnum, CORE_ADDR addr)
746 gdb_assert (regnum >= 0 && regnum <= DR_LASTADDR - DR_FIRSTADDR);
748 i386_linux_dr[DR_FIRSTADDR + regnum] = addr;
750 i386_linux_dr_set (ptid, DR_FIRSTADDR + regnum, addr);
753 /* Set address REGNUM (zero based) to zero in all LWPs of LWP_LIST. */
756 i386_linux_dr_reset_addr (int regnum)
758 i386_linux_dr_set_addr (regnum, 0);
761 /* Get DR_STATUS from only the one LWP of INFERIOR_PTID. */
764 i386_linux_dr_get_status (void)
766 return i386_linux_dr_get (inferior_ptid, DR_STATUS);
769 /* Unset MASK bits in DR_STATUS in all LWPs of LWP_LIST. */
772 i386_linux_dr_unset_status (unsigned long mask)
781 value = i386_linux_dr_get (ptid, DR_STATUS);
783 i386_linux_dr_set (ptid, DR_STATUS, value);
788 i386_linux_new_thread (ptid_t ptid)
792 for (i = DR_FIRSTADDR; i <= DR_LASTADDR; i++)
793 i386_linux_dr_set (ptid, i, i386_linux_dr[i]);
795 i386_linux_dr_set (ptid, DR_CONTROL, i386_linux_dr[DR_CONTROL]);
799 /* Called by libthread_db. Returns a pointer to the thread local
800 storage (or its descriptor). */
803 ps_get_thread_area (const struct ps_prochandle *ph,
804 lwpid_t lwpid, int idx, void **base)
806 /* NOTE: cagney/2003-08-26: The definition of this buffer is found
807 in the kernel header <asm-i386/ldt.h>. It, after padding, is 4 x
808 4 byte integers in size: `entry_number', `base_addr', `limit',
809 and a bunch of status bits.
811 The values returned by this ptrace call should be part of the
812 regcache buffer, and ps_get_thread_area should channel its
813 request through the regcache. That way remote targets could
814 provide the value using the remote protocol and not this direct
817 Is this function needed? I'm guessing that the `base' is the
818 address of a a descriptor that libthread_db uses to find the
819 thread local address base that GDB needs. Perhaps that
820 descriptor is defined by the ABI. Anyway, given that
821 libthread_db calls this function without prompting (gdb
822 requesting tls base) I guess it needs info in there anyway. */
823 unsigned int desc[4];
824 gdb_assert (sizeof (int) == 4);
826 #ifndef PTRACE_GET_THREAD_AREA
827 #define PTRACE_GET_THREAD_AREA 25
830 if (ptrace (PTRACE_GET_THREAD_AREA, lwpid,
831 (void *) idx, (unsigned long) &desc) < 0)
834 *(int *)base = desc[1];
839 /* The instruction for a GNU/Linux system call is:
843 static const unsigned char linux_syscall[] = { 0xcd, 0x80 };
845 #define LINUX_SYSCALL_LEN (sizeof linux_syscall)
847 /* The system call number is stored in the %eax register. */
848 #define LINUX_SYSCALL_REGNUM I386_EAX_REGNUM
850 /* We are specifically interested in the sigreturn and rt_sigreturn
853 #ifndef SYS_sigreturn
854 #define SYS_sigreturn 0x77
856 #ifndef SYS_rt_sigreturn
857 #define SYS_rt_sigreturn 0xad
860 /* Offset to saved processor flags, from <asm/sigcontext.h>. */
861 #define LINUX_SIGCONTEXT_EFLAGS_OFFSET (64)
863 /* Resume execution of the inferior process.
864 If STEP is nonzero, single-step it.
865 If SIGNAL is nonzero, give it that signal. */
868 i386_linux_resume (struct target_ops *ops,
869 ptid_t ptid, int step, enum target_signal signal)
871 int pid = PIDGET (ptid);
875 if (catch_syscall_enabled () > 0)
876 request = PTRACE_SYSCALL;
878 request = PTRACE_CONT;
882 struct regcache *regcache = get_thread_regcache (pid_to_ptid (pid));
883 struct gdbarch *gdbarch = get_regcache_arch (regcache);
884 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
886 gdb_byte buf[LINUX_SYSCALL_LEN];
888 request = PTRACE_SINGLESTEP;
890 regcache_cooked_read_unsigned (regcache,
891 gdbarch_pc_regnum (gdbarch), &pc);
893 /* Returning from a signal trampoline is done by calling a
894 special system call (sigreturn or rt_sigreturn, see
895 i386-linux-tdep.c for more information). This system call
896 restores the registers that were saved when the signal was
897 raised, including %eflags. That means that single-stepping
898 won't work. Instead, we'll have to modify the signal context
899 that's about to be restored, and set the trace flag there. */
901 /* First check if PC is at a system call. */
902 if (target_read_memory (pc, buf, LINUX_SYSCALL_LEN) == 0
903 && memcmp (buf, linux_syscall, LINUX_SYSCALL_LEN) == 0)
906 regcache_cooked_read_unsigned (regcache,
907 LINUX_SYSCALL_REGNUM, &syscall);
909 /* Then check the system call number. */
910 if (syscall == SYS_sigreturn || syscall == SYS_rt_sigreturn)
913 unsigned long int eflags;
915 regcache_cooked_read_unsigned (regcache, I386_ESP_REGNUM, &sp);
916 if (syscall == SYS_rt_sigreturn)
917 addr = read_memory_integer (sp + 8, 4, byte_order) + 20;
921 /* Set the trace flag in the context that's about to be
923 addr += LINUX_SIGCONTEXT_EFLAGS_OFFSET;
924 read_memory (addr, (gdb_byte *) &eflags, 4);
926 write_memory (addr, (gdb_byte *) &eflags, 4);
931 if (ptrace (request, pid, 0, target_signal_to_host (signal)) == -1)
932 perror_with_name (("ptrace"));
935 static void (*super_post_startup_inferior) (ptid_t ptid);
938 i386_linux_child_post_startup_inferior (ptid_t ptid)
940 i386_cleanup_dregs ();
941 super_post_startup_inferior (ptid);
944 /* Get Linux/x86 target description from running target. */
946 static const struct target_desc *
947 i386_linux_read_description (struct target_ops *ops)
950 static uint64_t xcr0;
952 /* GNU/Linux LWP ID's are process ID's. */
953 tid = TIDGET (inferior_ptid);
955 tid = PIDGET (inferior_ptid); /* Not a threaded program. */
957 #ifdef HAVE_PTRACE_GETFPXREGS
958 if (have_ptrace_getfpxregs == -1)
960 elf_fpxregset_t fpxregs;
962 if (ptrace (PTRACE_GETFPXREGS, tid, 0, (int) &fpxregs) < 0)
964 have_ptrace_getfpxregs = 0;
965 have_ptrace_getregset = 0;
966 return tdesc_i386_mmx_linux;
971 if (have_ptrace_getregset == -1)
973 uint64_t xstateregs[(I386_XSTATE_SSE_SIZE / sizeof (uint64_t))];
976 iov.iov_base = xstateregs;
977 iov.iov_len = sizeof (xstateregs);
979 /* Check if PTRACE_GETREGSET works. */
980 if (ptrace (PTRACE_GETREGSET, tid, (unsigned int) NT_X86_XSTATE,
982 have_ptrace_getregset = 0;
985 have_ptrace_getregset = 1;
987 /* Get XCR0 from XSAVE extended state. */
988 xcr0 = xstateregs[(I386_LINUX_XSAVE_XCR0_OFFSET
989 / sizeof (long long))];
993 /* Check the native XCR0 only if PTRACE_GETREGSET is available. */
994 if (have_ptrace_getregset
995 && (xcr0 & I386_XSTATE_AVX_MASK) == I386_XSTATE_AVX_MASK)
996 return tdesc_i386_avx_linux;
998 return tdesc_i386_linux;
1002 _initialize_i386_linux_nat (void)
1004 struct target_ops *t;
1006 /* Fill in the generic GNU/Linux methods. */
1007 t = linux_target ();
1009 i386_use_watchpoints (t);
1011 i386_dr_low.set_control = i386_linux_dr_set_control;
1012 i386_dr_low.set_addr = i386_linux_dr_set_addr;
1013 i386_dr_low.reset_addr = i386_linux_dr_reset_addr;
1014 i386_dr_low.get_status = i386_linux_dr_get_status;
1015 i386_dr_low.unset_status = i386_linux_dr_unset_status;
1016 i386_set_debug_register_length (4);
1018 /* Override the default ptrace resume method. */
1019 t->to_resume = i386_linux_resume;
1021 /* Override the GNU/Linux inferior startup hook. */
1022 super_post_startup_inferior = t->to_post_startup_inferior;
1023 t->to_post_startup_inferior = i386_linux_child_post_startup_inferior;
1025 /* Add our register access methods. */
1026 t->to_fetch_registers = i386_linux_fetch_inferior_registers;
1027 t->to_store_registers = i386_linux_store_inferior_registers;
1029 t->to_read_description = i386_linux_read_description;
1031 /* Register the target. */
1032 linux_nat_add_target (t);
1033 linux_nat_set_new_thread (t, i386_linux_new_thread);