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 /* Which ptrace request retrieves which registers?
103 These apply to the corresponding SET requests as well. */
105 #define GETREGS_SUPPLIES(regno) \
106 ((0 <= (regno) && (regno) <= 15) || (regno) == I386_LINUX_ORIG_EAX_REGNUM)
108 #define GETFPXREGS_SUPPLIES(regno) \
109 (I386_ST0_REGNUM <= (regno) && (regno) < I386_SSE_NUM_REGS)
111 #define GETXSTATEREGS_SUPPLIES(regno) \
112 (I386_ST0_REGNUM <= (regno) && (regno) < I386_AVX_NUM_REGS)
114 /* Does the current host support the GETREGS request? */
115 int have_ptrace_getregs
=
116 #ifdef HAVE_PTRACE_GETREGS
123 /* Does the current host support the GETFPXREGS request? The header
124 file may or may not define it, and even if it is defined, the
125 kernel will return EIO if it's running on a pre-SSE processor.
127 My instinct is to attach this to some architecture- or
128 target-specific data structure, but really, a particular GDB
129 process can only run on top of one kernel at a time. So it's okay
130 for this to be a simple variable. */
131 int have_ptrace_getfpxregs
=
132 #ifdef HAVE_PTRACE_GETFPXREGS
140 /* Accessing registers through the U area, one at a time. */
142 /* Fetch one register. */
145 fetch_register (struct regcache
*regcache
, int regno
)
150 gdb_assert (!have_ptrace_getregs
);
151 if (i386_linux_gregset_reg_offset
[regno
] == -1)
153 regcache_raw_supply (regcache
, regno
, NULL
);
157 /* GNU/Linux LWP ID's are process ID's. */
158 tid
= TIDGET (inferior_ptid
);
160 tid
= PIDGET (inferior_ptid
); /* Not a threaded program. */
163 val
= ptrace (PTRACE_PEEKUSER
, tid
,
164 i386_linux_gregset_reg_offset
[regno
], 0);
166 error (_("Couldn't read register %s (#%d): %s."),
167 gdbarch_register_name (get_regcache_arch (regcache
), regno
),
168 regno
, safe_strerror (errno
));
170 regcache_raw_supply (regcache
, regno
, &val
);
173 /* Store one register. */
176 store_register (const struct regcache
*regcache
, int regno
)
181 gdb_assert (!have_ptrace_getregs
);
182 if (i386_linux_gregset_reg_offset
[regno
] == -1)
185 /* GNU/Linux LWP ID's are process ID's. */
186 tid
= TIDGET (inferior_ptid
);
188 tid
= PIDGET (inferior_ptid
); /* Not a threaded program. */
191 regcache_raw_collect (regcache
, regno
, &val
);
192 ptrace (PTRACE_POKEUSER
, tid
,
193 i386_linux_gregset_reg_offset
[regno
], val
);
195 error (_("Couldn't write register %s (#%d): %s."),
196 gdbarch_register_name (get_regcache_arch (regcache
), regno
),
197 regno
, safe_strerror (errno
));
201 /* Transfering the general-purpose registers between GDB, inferiors
204 /* Fill GDB's register array with the general-purpose register values
208 supply_gregset (struct regcache
*regcache
, const elf_gregset_t
*gregsetp
)
210 const gdb_byte
*regp
= (const gdb_byte
*) gregsetp
;
213 for (i
= 0; i
< I386_NUM_GREGS
; i
++)
214 regcache_raw_supply (regcache
, i
,
215 regp
+ i386_linux_gregset_reg_offset
[i
]);
217 if (I386_LINUX_ORIG_EAX_REGNUM
218 < gdbarch_num_regs (get_regcache_arch (regcache
)))
219 regcache_raw_supply (regcache
, I386_LINUX_ORIG_EAX_REGNUM
,
220 regp
+ i386_linux_gregset_reg_offset
[I386_LINUX_ORIG_EAX_REGNUM
]);
223 /* Fill register REGNO (if it is a general-purpose register) in
224 *GREGSETPS with the value in GDB's register array. If REGNO is -1,
225 do this for all registers. */
228 fill_gregset (const struct regcache
*regcache
,
229 elf_gregset_t
*gregsetp
, int regno
)
231 gdb_byte
*regp
= (gdb_byte
*) gregsetp
;
234 for (i
= 0; i
< I386_NUM_GREGS
; i
++)
235 if (regno
== -1 || regno
== i
)
236 regcache_raw_collect (regcache
, i
,
237 regp
+ i386_linux_gregset_reg_offset
[i
]);
239 if ((regno
== -1 || regno
== I386_LINUX_ORIG_EAX_REGNUM
)
240 && I386_LINUX_ORIG_EAX_REGNUM
241 < gdbarch_num_regs (get_regcache_arch (regcache
)))
242 regcache_raw_collect (regcache
, I386_LINUX_ORIG_EAX_REGNUM
,
243 regp
+ i386_linux_gregset_reg_offset
[I386_LINUX_ORIG_EAX_REGNUM
]);
246 #ifdef HAVE_PTRACE_GETREGS
248 /* Fetch all general-purpose registers from process/thread TID and
249 store their values in GDB's register array. */
252 fetch_regs (struct regcache
*regcache
, int tid
)
255 elf_gregset_t
*regs_p
= ®s
;
257 if (ptrace (PTRACE_GETREGS
, tid
, 0, (int) ®s
) < 0)
261 /* The kernel we're running on doesn't support the GETREGS
262 request. Reset `have_ptrace_getregs'. */
263 have_ptrace_getregs
= 0;
267 perror_with_name (_("Couldn't get registers"));
270 supply_gregset (regcache
, (const elf_gregset_t
*) regs_p
);
273 /* Store all valid general-purpose registers in GDB's register array
274 into the process/thread specified by TID. */
277 store_regs (const struct regcache
*regcache
, int tid
, int regno
)
281 if (ptrace (PTRACE_GETREGS
, tid
, 0, (int) ®s
) < 0)
282 perror_with_name (_("Couldn't get registers"));
284 fill_gregset (regcache
, ®s
, regno
);
286 if (ptrace (PTRACE_SETREGS
, tid
, 0, (int) ®s
) < 0)
287 perror_with_name (_("Couldn't write registers"));
292 static void fetch_regs (struct regcache
*regcache
, int tid
) {}
293 static void store_regs (const struct regcache
*regcache
, int tid
, int regno
) {}
298 /* Transfering floating-point registers between GDB, inferiors and cores. */
300 /* Fill GDB's register array with the floating-point register values in
304 supply_fpregset (struct regcache
*regcache
, const elf_fpregset_t
*fpregsetp
)
306 i387_supply_fsave (regcache
, -1, fpregsetp
);
309 /* Fill register REGNO (if it is a floating-point register) in
310 *FPREGSETP with the value in GDB's register array. If REGNO is -1,
311 do this for all registers. */
314 fill_fpregset (const struct regcache
*regcache
,
315 elf_fpregset_t
*fpregsetp
, int regno
)
317 i387_collect_fsave (regcache
, regno
, fpregsetp
);
320 #ifdef HAVE_PTRACE_GETREGS
322 /* Fetch all floating-point registers from process/thread TID and store
323 thier values in GDB's register array. */
326 fetch_fpregs (struct regcache
*regcache
, int tid
)
328 elf_fpregset_t fpregs
;
330 if (ptrace (PTRACE_GETFPREGS
, tid
, 0, (int) &fpregs
) < 0)
331 perror_with_name (_("Couldn't get floating point status"));
333 supply_fpregset (regcache
, (const elf_fpregset_t
*) &fpregs
);
336 /* Store all valid floating-point registers in GDB's register array
337 into the process/thread specified by TID. */
340 store_fpregs (const struct regcache
*regcache
, int tid
, int regno
)
342 elf_fpregset_t fpregs
;
344 if (ptrace (PTRACE_GETFPREGS
, tid
, 0, (int) &fpregs
) < 0)
345 perror_with_name (_("Couldn't get floating point status"));
347 fill_fpregset (regcache
, &fpregs
, regno
);
349 if (ptrace (PTRACE_SETFPREGS
, tid
, 0, (int) &fpregs
) < 0)
350 perror_with_name (_("Couldn't write floating point status"));
355 static void fetch_fpregs (struct regcache
*regcache
, int tid
) {}
356 static void store_fpregs (const struct regcache
*regcache
, int tid
, int regno
) {}
361 /* Transfering floating-point and SSE registers to and from GDB. */
363 /* Fetch all registers covered by the PTRACE_GETREGSET request from
364 process/thread TID and store their values in GDB's register array.
365 Return non-zero if successful, zero otherwise. */
368 fetch_xstateregs (struct regcache
*regcache
, int tid
)
370 char xstateregs
[I386_XSTATE_MAX_SIZE
];
373 if (!have_ptrace_getregset
)
376 iov
.iov_base
= xstateregs
;
377 iov
.iov_len
= sizeof(xstateregs
);
378 if (ptrace (PTRACE_GETREGSET
, tid
, (unsigned int) NT_X86_XSTATE
,
380 perror_with_name (_("Couldn't read extended state status"));
382 i387_supply_xsave (regcache
, -1, xstateregs
);
386 /* Store all valid registers in GDB's register array covered by the
387 PTRACE_SETREGSET request into the process/thread specified by TID.
388 Return non-zero if successful, zero otherwise. */
391 store_xstateregs (const struct regcache
*regcache
, int tid
, int regno
)
393 char xstateregs
[I386_XSTATE_MAX_SIZE
];
396 if (!have_ptrace_getregset
)
399 iov
.iov_base
= xstateregs
;
400 iov
.iov_len
= sizeof(xstateregs
);
401 if (ptrace (PTRACE_GETREGSET
, tid
, (unsigned int) NT_X86_XSTATE
,
403 perror_with_name (_("Couldn't read extended state status"));
405 i387_collect_xsave (regcache
, regno
, xstateregs
, 0);
407 if (ptrace (PTRACE_SETREGSET
, tid
, (unsigned int) NT_X86_XSTATE
,
409 perror_with_name (_("Couldn't write extended state status"));
414 #ifdef HAVE_PTRACE_GETFPXREGS
416 /* Fill GDB's register array with the floating-point and SSE register
417 values in *FPXREGSETP. */
420 supply_fpxregset (struct regcache
*regcache
,
421 const elf_fpxregset_t
*fpxregsetp
)
423 i387_supply_fxsave (regcache
, -1, fpxregsetp
);
426 /* Fill register REGNO (if it is a floating-point or SSE register) in
427 *FPXREGSETP with the value in GDB's register array. If REGNO is
428 -1, do this for all registers. */
431 fill_fpxregset (const struct regcache
*regcache
,
432 elf_fpxregset_t
*fpxregsetp
, int regno
)
434 i387_collect_fxsave (regcache
, regno
, fpxregsetp
);
437 /* Fetch all registers covered by the PTRACE_GETFPXREGS request from
438 process/thread TID and store their values in GDB's register array.
439 Return non-zero if successful, zero otherwise. */
442 fetch_fpxregs (struct regcache
*regcache
, int tid
)
444 elf_fpxregset_t fpxregs
;
446 if (! have_ptrace_getfpxregs
)
449 if (ptrace (PTRACE_GETFPXREGS
, tid
, 0, (int) &fpxregs
) < 0)
453 have_ptrace_getfpxregs
= 0;
457 perror_with_name (_("Couldn't read floating-point and SSE registers"));
460 supply_fpxregset (regcache
, (const elf_fpxregset_t
*) &fpxregs
);
464 /* Store all valid registers in GDB's register array covered by the
465 PTRACE_SETFPXREGS request into the process/thread specified by TID.
466 Return non-zero if successful, zero otherwise. */
469 store_fpxregs (const struct regcache
*regcache
, int tid
, int regno
)
471 elf_fpxregset_t fpxregs
;
473 if (! have_ptrace_getfpxregs
)
476 if (ptrace (PTRACE_GETFPXREGS
, tid
, 0, &fpxregs
) == -1)
480 have_ptrace_getfpxregs
= 0;
484 perror_with_name (_("Couldn't read floating-point and SSE registers"));
487 fill_fpxregset (regcache
, &fpxregs
, regno
);
489 if (ptrace (PTRACE_SETFPXREGS
, tid
, 0, &fpxregs
) == -1)
490 perror_with_name (_("Couldn't write floating-point and SSE registers"));
497 static int fetch_fpxregs (struct regcache
*regcache
, int tid
) { return 0; }
498 static int store_fpxregs (const struct regcache
*regcache
, int tid
, int regno
) { return 0; }
500 #endif /* HAVE_PTRACE_GETFPXREGS */
503 /* Transferring arbitrary registers between GDB and inferior. */
505 /* Fetch register REGNO from the child process. If REGNO is -1, do
506 this for all registers (including the floating point and SSE
510 i386_linux_fetch_inferior_registers (struct target_ops
*ops
,
511 struct regcache
*regcache
, int regno
)
515 /* Use the old method of peeking around in `struct user' if the
516 GETREGS request isn't available. */
517 if (!have_ptrace_getregs
)
521 for (i
= 0; i
< gdbarch_num_regs (get_regcache_arch (regcache
)); i
++)
522 if (regno
== -1 || regno
== i
)
523 fetch_register (regcache
, i
);
528 /* GNU/Linux LWP ID's are process ID's. */
529 tid
= TIDGET (inferior_ptid
);
531 tid
= PIDGET (inferior_ptid
); /* Not a threaded program. */
533 /* Use the PTRACE_GETFPXREGS request whenever possible, since it
534 transfers more registers in one system call, and we'll cache the
535 results. But remember that fetch_fpxregs can fail, and return
539 fetch_regs (regcache
, tid
);
541 /* The call above might reset `have_ptrace_getregs'. */
542 if (!have_ptrace_getregs
)
544 i386_linux_fetch_inferior_registers (ops
, regcache
, regno
);
548 if (fetch_xstateregs (regcache
, tid
))
550 if (fetch_fpxregs (regcache
, tid
))
552 fetch_fpregs (regcache
, tid
);
556 if (GETREGS_SUPPLIES (regno
))
558 fetch_regs (regcache
, tid
);
562 if (GETXSTATEREGS_SUPPLIES (regno
))
564 if (fetch_xstateregs (regcache
, tid
))
568 if (GETFPXREGS_SUPPLIES (regno
))
570 if (fetch_fpxregs (regcache
, tid
))
573 /* Either our processor or our kernel doesn't support the SSE
574 registers, so read the FP registers in the traditional way,
575 and fill the SSE registers with dummy values. It would be
576 more graceful to handle differences in the register set using
577 gdbarch. Until then, this will at least make things work
579 fetch_fpregs (regcache
, tid
);
583 internal_error (__FILE__
, __LINE__
,
584 _("Got request for bad register number %d."), regno
);
587 /* Store register REGNO back into the child process. If REGNO is -1,
588 do this for all registers (including the floating point and SSE
591 i386_linux_store_inferior_registers (struct target_ops
*ops
,
592 struct regcache
*regcache
, int regno
)
596 /* Use the old method of poking around in `struct user' if the
597 SETREGS request isn't available. */
598 if (!have_ptrace_getregs
)
602 for (i
= 0; i
< gdbarch_num_regs (get_regcache_arch (regcache
)); i
++)
603 if (regno
== -1 || regno
== i
)
604 store_register (regcache
, i
);
609 /* GNU/Linux LWP ID's are process ID's. */
610 tid
= TIDGET (inferior_ptid
);
612 tid
= PIDGET (inferior_ptid
); /* Not a threaded program. */
614 /* Use the PTRACE_SETFPXREGS requests whenever possible, since it
615 transfers more registers in one system call. But remember that
616 store_fpxregs can fail, and return zero. */
619 store_regs (regcache
, tid
, regno
);
620 if (store_xstateregs (regcache
, tid
, regno
))
622 if (store_fpxregs (regcache
, tid
, regno
))
624 store_fpregs (regcache
, tid
, regno
);
628 if (GETREGS_SUPPLIES (regno
))
630 store_regs (regcache
, tid
, regno
);
634 if (GETXSTATEREGS_SUPPLIES (regno
))
636 if (store_xstateregs (regcache
, tid
, regno
))
640 if (GETFPXREGS_SUPPLIES (regno
))
642 if (store_fpxregs (regcache
, tid
, regno
))
645 /* Either our processor or our kernel doesn't support the SSE
646 registers, so just write the FP registers in the traditional
648 store_fpregs (regcache
, tid
, regno
);
652 internal_error (__FILE__
, __LINE__
,
653 _("Got request to store bad register number %d."), regno
);
657 /* Support for debug registers. */
659 static unsigned long i386_linux_dr
[DR_CONTROL
+ 1];
661 /* Get debug register REGNUM value from only the one LWP of PTID. */
664 i386_linux_dr_get (ptid_t ptid
, int regnum
)
673 /* FIXME: kettenis/2001-03-27: Calling perror_with_name if the
674 ptrace call fails breaks debugging remote targets. The correct
675 way to fix this is to add the hardware breakpoint and watchpoint
676 stuff to the target vector. For now, just return zero if the
677 ptrace call fails. */
679 value
= ptrace (PTRACE_PEEKUSER
, tid
,
680 offsetof (struct user
, u_debugreg
[regnum
]), 0);
683 perror_with_name (_("Couldn't read debug register"));
691 /* Set debug register REGNUM to VALUE in only the one LWP of PTID. */
694 i386_linux_dr_set (ptid_t ptid
, int regnum
, unsigned long value
)
703 ptrace (PTRACE_POKEUSER
, tid
,
704 offsetof (struct user
, u_debugreg
[regnum
]), value
);
706 perror_with_name (_("Couldn't write debug register"));
709 /* Set DR_CONTROL to ADDR in all LWPs of LWP_LIST. */
712 i386_linux_dr_set_control (unsigned long control
)
717 i386_linux_dr
[DR_CONTROL
] = control
;
719 i386_linux_dr_set (ptid
, DR_CONTROL
, control
);
722 /* Set address REGNUM (zero based) to ADDR in all LWPs of LWP_LIST. */
725 i386_linux_dr_set_addr (int regnum
, CORE_ADDR addr
)
730 gdb_assert (regnum
>= 0 && regnum
<= DR_LASTADDR
- DR_FIRSTADDR
);
732 i386_linux_dr
[DR_FIRSTADDR
+ regnum
] = addr
;
734 i386_linux_dr_set (ptid
, DR_FIRSTADDR
+ regnum
, addr
);
737 /* Set address REGNUM (zero based) to zero in all LWPs of LWP_LIST. */
740 i386_linux_dr_reset_addr (int regnum
)
742 i386_linux_dr_set_addr (regnum
, 0);
745 /* Get DR_STATUS from only the one LWP of INFERIOR_PTID. */
748 i386_linux_dr_get_status (void)
750 return i386_linux_dr_get (inferior_ptid
, DR_STATUS
);
753 /* Unset MASK bits in DR_STATUS in all LWPs of LWP_LIST. */
756 i386_linux_dr_unset_status (unsigned long mask
)
765 value
= i386_linux_dr_get (ptid
, DR_STATUS
);
767 i386_linux_dr_set (ptid
, DR_STATUS
, value
);
772 i386_linux_new_thread (ptid_t ptid
)
776 for (i
= DR_FIRSTADDR
; i
<= DR_LASTADDR
; i
++)
777 i386_linux_dr_set (ptid
, i
, i386_linux_dr
[i
]);
779 i386_linux_dr_set (ptid
, DR_CONTROL
, i386_linux_dr
[DR_CONTROL
]);
783 /* Called by libthread_db. Returns a pointer to the thread local
784 storage (or its descriptor). */
787 ps_get_thread_area (const struct ps_prochandle
*ph
,
788 lwpid_t lwpid
, int idx
, void **base
)
790 /* NOTE: cagney/2003-08-26: The definition of this buffer is found
791 in the kernel header <asm-i386/ldt.h>. It, after padding, is 4 x
792 4 byte integers in size: `entry_number', `base_addr', `limit',
793 and a bunch of status bits.
795 The values returned by this ptrace call should be part of the
796 regcache buffer, and ps_get_thread_area should channel its
797 request through the regcache. That way remote targets could
798 provide the value using the remote protocol and not this direct
801 Is this function needed? I'm guessing that the `base' is the
802 address of a a descriptor that libthread_db uses to find the
803 thread local address base that GDB needs. Perhaps that
804 descriptor is defined by the ABI. Anyway, given that
805 libthread_db calls this function without prompting (gdb
806 requesting tls base) I guess it needs info in there anyway. */
807 unsigned int desc
[4];
808 gdb_assert (sizeof (int) == 4);
810 #ifndef PTRACE_GET_THREAD_AREA
811 #define PTRACE_GET_THREAD_AREA 25
814 if (ptrace (PTRACE_GET_THREAD_AREA
, lwpid
,
815 (void *) idx
, (unsigned long) &desc
) < 0)
818 *(int *)base
= desc
[1];
823 /* The instruction for a GNU/Linux system call is:
827 static const unsigned char linux_syscall
[] = { 0xcd, 0x80 };
829 #define LINUX_SYSCALL_LEN (sizeof linux_syscall)
831 /* The system call number is stored in the %eax register. */
832 #define LINUX_SYSCALL_REGNUM I386_EAX_REGNUM
834 /* We are specifically interested in the sigreturn and rt_sigreturn
837 #ifndef SYS_sigreturn
838 #define SYS_sigreturn 0x77
840 #ifndef SYS_rt_sigreturn
841 #define SYS_rt_sigreturn 0xad
844 /* Offset to saved processor flags, from <asm/sigcontext.h>. */
845 #define LINUX_SIGCONTEXT_EFLAGS_OFFSET (64)
847 /* Resume execution of the inferior process.
848 If STEP is nonzero, single-step it.
849 If SIGNAL is nonzero, give it that signal. */
852 i386_linux_resume (struct target_ops
*ops
,
853 ptid_t ptid
, int step
, enum target_signal signal
)
855 int pid
= PIDGET (ptid
);
859 if (catch_syscall_enabled () > 0)
860 request
= PTRACE_SYSCALL
;
862 request
= PTRACE_CONT
;
866 struct regcache
*regcache
= get_thread_regcache (pid_to_ptid (pid
));
867 struct gdbarch
*gdbarch
= get_regcache_arch (regcache
);
868 enum bfd_endian byte_order
= gdbarch_byte_order (gdbarch
);
870 gdb_byte buf
[LINUX_SYSCALL_LEN
];
872 request
= PTRACE_SINGLESTEP
;
874 regcache_cooked_read_unsigned (regcache
,
875 gdbarch_pc_regnum (gdbarch
), &pc
);
877 /* Returning from a signal trampoline is done by calling a
878 special system call (sigreturn or rt_sigreturn, see
879 i386-linux-tdep.c for more information). This system call
880 restores the registers that were saved when the signal was
881 raised, including %eflags. That means that single-stepping
882 won't work. Instead, we'll have to modify the signal context
883 that's about to be restored, and set the trace flag there. */
885 /* First check if PC is at a system call. */
886 if (target_read_memory (pc
, buf
, LINUX_SYSCALL_LEN
) == 0
887 && memcmp (buf
, linux_syscall
, LINUX_SYSCALL_LEN
) == 0)
890 regcache_cooked_read_unsigned (regcache
,
891 LINUX_SYSCALL_REGNUM
, &syscall
);
893 /* Then check the system call number. */
894 if (syscall
== SYS_sigreturn
|| syscall
== SYS_rt_sigreturn
)
897 unsigned long int eflags
;
899 regcache_cooked_read_unsigned (regcache
, I386_ESP_REGNUM
, &sp
);
900 if (syscall
== SYS_rt_sigreturn
)
901 addr
= read_memory_integer (sp
+ 8, 4, byte_order
) + 20;
905 /* Set the trace flag in the context that's about to be
907 addr
+= LINUX_SIGCONTEXT_EFLAGS_OFFSET
;
908 read_memory (addr
, (gdb_byte
*) &eflags
, 4);
910 write_memory (addr
, (gdb_byte
*) &eflags
, 4);
915 if (ptrace (request
, pid
, 0, target_signal_to_host (signal
)) == -1)
916 perror_with_name (("ptrace"));
919 static void (*super_post_startup_inferior
) (ptid_t ptid
);
922 i386_linux_child_post_startup_inferior (ptid_t ptid
)
924 i386_cleanup_dregs ();
925 super_post_startup_inferior (ptid
);
928 /* Get Linux/x86 target description from running target. */
930 static const struct target_desc
*
931 i386_linux_read_description (struct target_ops
*ops
)
934 static uint64_t xcr0
;
936 /* GNU/Linux LWP ID's are process ID's. */
937 tid
= TIDGET (inferior_ptid
);
939 tid
= PIDGET (inferior_ptid
); /* Not a threaded program. */
941 #ifdef HAVE_PTRACE_GETFPXREGS
942 if (have_ptrace_getfpxregs
== -1)
944 elf_fpxregset_t fpxregs
;
946 if (ptrace (PTRACE_GETFPXREGS
, tid
, 0, (int) &fpxregs
) < 0)
948 have_ptrace_getfpxregs
= 0;
949 have_ptrace_getregset
= 0;
950 return tdesc_i386_mmx_linux
;
955 if (have_ptrace_getregset
== -1)
957 uint64_t xstateregs
[(I386_XSTATE_SSE_SIZE
/ sizeof (uint64_t))];
960 iov
.iov_base
= xstateregs
;
961 iov
.iov_len
= sizeof (xstateregs
);
963 /* Check if PTRACE_GETREGSET works. */
964 if (ptrace (PTRACE_GETREGSET
, tid
, (unsigned int) NT_X86_XSTATE
,
966 have_ptrace_getregset
= 0;
969 have_ptrace_getregset
= 1;
971 /* Get XCR0 from XSAVE extended state. */
972 xcr0
= xstateregs
[(I386_LINUX_XSAVE_XCR0_OFFSET
973 / sizeof (long long))];
977 /* Check the native XCR0 only if PTRACE_GETREGSET is available. */
978 if (have_ptrace_getregset
979 && (xcr0
& I386_XSTATE_AVX_MASK
) == I386_XSTATE_AVX_MASK
)
980 return tdesc_i386_avx_linux
;
982 return tdesc_i386_linux
;
986 _initialize_i386_linux_nat (void)
988 struct target_ops
*t
;
990 /* Fill in the generic GNU/Linux methods. */
993 i386_use_watchpoints (t
);
995 i386_dr_low
.set_control
= i386_linux_dr_set_control
;
996 i386_dr_low
.set_addr
= i386_linux_dr_set_addr
;
997 i386_dr_low
.reset_addr
= i386_linux_dr_reset_addr
;
998 i386_dr_low
.get_status
= i386_linux_dr_get_status
;
999 i386_dr_low
.unset_status
= i386_linux_dr_unset_status
;
1000 i386_set_debug_register_length (4);
1002 /* Override the default ptrace resume method. */
1003 t
->to_resume
= i386_linux_resume
;
1005 /* Override the GNU/Linux inferior startup hook. */
1006 super_post_startup_inferior
= t
->to_post_startup_inferior
;
1007 t
->to_post_startup_inferior
= i386_linux_child_post_startup_inferior
;
1009 /* Add our register access methods. */
1010 t
->to_fetch_registers
= i386_linux_fetch_inferior_registers
;
1011 t
->to_store_registers
= i386_linux_store_inferior_registers
;
1013 t
->to_read_description
= i386_linux_read_description
;
1015 /* Register the target. */
1016 linux_nat_add_target (t
);
1017 linux_nat_set_new_thread (t
, i386_linux_new_thread
);