1 /* Native-dependent code for GNU/Linux i386.
3 Copyright (C) 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008,
4 2009, 2010, 2011 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>
50 /* Prototypes for supply_gregset etc. */
53 #include "i387-tdep.h"
54 #include "i386-tdep.h"
55 #include "i386-linux-tdep.h"
57 /* Defines ps_err_e, struct ps_prochandle. */
58 #include "gdb_proc_service.h"
60 #include "i386-xstate.h"
62 #ifndef PTRACE_GETREGSET
63 #define PTRACE_GETREGSET 0x4204
66 #ifndef PTRACE_SETREGSET
67 #define PTRACE_SETREGSET 0x4205
70 /* Per-thread arch-specific data we want to keep. */
74 /* Non-zero if our copy differs from what's recorded in the thread. */
75 int debug_registers_changed
;
78 /* Does the current host support PTRACE_GETREGSET? */
79 static int have_ptrace_getregset
= -1;
82 /* The register sets used in GNU/Linux ELF core-dumps are identical to
83 the register sets in `struct user' that is used for a.out
84 core-dumps, and is also used by `ptrace'. The corresponding types
85 are `elf_gregset_t' for the general-purpose registers (with
86 `elf_greg_t' the type of a single GP register) and `elf_fpregset_t'
87 for the floating-point registers.
89 Those types used to be available under the names `gregset_t' and
90 `fpregset_t' too, and this file used those names in the past. But
91 those names are now used for the register sets used in the
92 `mcontext_t' type, and have a different size and layout. */
94 /* Which ptrace request retrieves which registers?
95 These apply to the corresponding SET requests as well. */
97 #define GETREGS_SUPPLIES(regno) \
98 ((0 <= (regno) && (regno) <= 15) || (regno) == I386_LINUX_ORIG_EAX_REGNUM)
100 #define GETFPXREGS_SUPPLIES(regno) \
101 (I386_ST0_REGNUM <= (regno) && (regno) < I386_SSE_NUM_REGS)
103 #define GETXSTATEREGS_SUPPLIES(regno) \
104 (I386_ST0_REGNUM <= (regno) && (regno) < I386_AVX_NUM_REGS)
106 /* Does the current host support the GETREGS request? */
107 int have_ptrace_getregs
=
108 #ifdef HAVE_PTRACE_GETREGS
115 /* Does the current host support the GETFPXREGS request? The header
116 file may or may not define it, and even if it is defined, the
117 kernel will return EIO if it's running on a pre-SSE processor.
119 My instinct is to attach this to some architecture- or
120 target-specific data structure, but really, a particular GDB
121 process can only run on top of one kernel at a time. So it's okay
122 for this to be a simple variable. */
123 int have_ptrace_getfpxregs
=
124 #ifdef HAVE_PTRACE_GETFPXREGS
132 /* Accessing registers through the U area, one at a time. */
134 /* Fetch one register. */
137 fetch_register (struct regcache
*regcache
, int regno
)
142 gdb_assert (!have_ptrace_getregs
);
143 if (i386_linux_gregset_reg_offset
[regno
] == -1)
145 regcache_raw_supply (regcache
, regno
, NULL
);
149 /* GNU/Linux LWP ID's are process ID's. */
150 tid
= TIDGET (inferior_ptid
);
152 tid
= PIDGET (inferior_ptid
); /* Not a threaded program. */
155 val
= ptrace (PTRACE_PEEKUSER
, tid
,
156 i386_linux_gregset_reg_offset
[regno
], 0);
158 error (_("Couldn't read register %s (#%d): %s."),
159 gdbarch_register_name (get_regcache_arch (regcache
), regno
),
160 regno
, safe_strerror (errno
));
162 regcache_raw_supply (regcache
, regno
, &val
);
165 /* Store one register. */
168 store_register (const struct regcache
*regcache
, int regno
)
173 gdb_assert (!have_ptrace_getregs
);
174 if (i386_linux_gregset_reg_offset
[regno
] == -1)
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 regcache_raw_collect (regcache
, regno
, &val
);
184 ptrace (PTRACE_POKEUSER
, tid
,
185 i386_linux_gregset_reg_offset
[regno
], val
);
187 error (_("Couldn't write register %s (#%d): %s."),
188 gdbarch_register_name (get_regcache_arch (regcache
), regno
),
189 regno
, safe_strerror (errno
));
193 /* Transfering the general-purpose registers between GDB, inferiors
196 /* Fill GDB's register array with the general-purpose register values
200 supply_gregset (struct regcache
*regcache
, const elf_gregset_t
*gregsetp
)
202 const gdb_byte
*regp
= (const gdb_byte
*) gregsetp
;
205 for (i
= 0; i
< I386_NUM_GREGS
; i
++)
206 regcache_raw_supply (regcache
, i
,
207 regp
+ i386_linux_gregset_reg_offset
[i
]);
209 if (I386_LINUX_ORIG_EAX_REGNUM
210 < gdbarch_num_regs (get_regcache_arch (regcache
)))
211 regcache_raw_supply (regcache
, I386_LINUX_ORIG_EAX_REGNUM
, regp
212 + i386_linux_gregset_reg_offset
[I386_LINUX_ORIG_EAX_REGNUM
]);
215 /* Fill register REGNO (if it is a general-purpose register) in
216 *GREGSETPS with the value in GDB's register array. If REGNO is -1,
217 do this for all registers. */
220 fill_gregset (const struct regcache
*regcache
,
221 elf_gregset_t
*gregsetp
, int regno
)
223 gdb_byte
*regp
= (gdb_byte
*) gregsetp
;
226 for (i
= 0; i
< I386_NUM_GREGS
; i
++)
227 if (regno
== -1 || regno
== i
)
228 regcache_raw_collect (regcache
, i
,
229 regp
+ i386_linux_gregset_reg_offset
[i
]);
231 if ((regno
== -1 || regno
== I386_LINUX_ORIG_EAX_REGNUM
)
232 && I386_LINUX_ORIG_EAX_REGNUM
233 < gdbarch_num_regs (get_regcache_arch (regcache
)))
234 regcache_raw_collect (regcache
, I386_LINUX_ORIG_EAX_REGNUM
, regp
235 + i386_linux_gregset_reg_offset
[I386_LINUX_ORIG_EAX_REGNUM
]);
238 #ifdef HAVE_PTRACE_GETREGS
240 /* Fetch all general-purpose registers from process/thread TID and
241 store their values in GDB's register array. */
244 fetch_regs (struct regcache
*regcache
, int tid
)
247 elf_gregset_t
*regs_p
= ®s
;
249 if (ptrace (PTRACE_GETREGS
, tid
, 0, (int) ®s
) < 0)
253 /* The kernel we're running on doesn't support the GETREGS
254 request. Reset `have_ptrace_getregs'. */
255 have_ptrace_getregs
= 0;
259 perror_with_name (_("Couldn't get registers"));
262 supply_gregset (regcache
, (const elf_gregset_t
*) regs_p
);
265 /* Store all valid general-purpose registers in GDB's register array
266 into the process/thread specified by TID. */
269 store_regs (const struct regcache
*regcache
, int tid
, int regno
)
273 if (ptrace (PTRACE_GETREGS
, tid
, 0, (int) ®s
) < 0)
274 perror_with_name (_("Couldn't get registers"));
276 fill_gregset (regcache
, ®s
, regno
);
278 if (ptrace (PTRACE_SETREGS
, tid
, 0, (int) ®s
) < 0)
279 perror_with_name (_("Couldn't write registers"));
284 static void fetch_regs (struct regcache
*regcache
, int tid
) {}
285 static void store_regs (const struct regcache
*regcache
, int tid
, int regno
) {}
290 /* Transfering floating-point registers between GDB, inferiors and cores. */
292 /* Fill GDB's register array with the floating-point register values in
296 supply_fpregset (struct regcache
*regcache
, const elf_fpregset_t
*fpregsetp
)
298 i387_supply_fsave (regcache
, -1, fpregsetp
);
301 /* Fill register REGNO (if it is a floating-point register) in
302 *FPREGSETP with the value in GDB's register array. If REGNO is -1,
303 do this for all registers. */
306 fill_fpregset (const struct regcache
*regcache
,
307 elf_fpregset_t
*fpregsetp
, int regno
)
309 i387_collect_fsave (regcache
, regno
, fpregsetp
);
312 #ifdef HAVE_PTRACE_GETREGS
314 /* Fetch all floating-point registers from process/thread TID and store
315 thier values in GDB's register array. */
318 fetch_fpregs (struct regcache
*regcache
, int tid
)
320 elf_fpregset_t fpregs
;
322 if (ptrace (PTRACE_GETFPREGS
, tid
, 0, (int) &fpregs
) < 0)
323 perror_with_name (_("Couldn't get floating point status"));
325 supply_fpregset (regcache
, (const elf_fpregset_t
*) &fpregs
);
328 /* Store all valid floating-point registers in GDB's register array
329 into the process/thread specified by TID. */
332 store_fpregs (const struct regcache
*regcache
, int tid
, int regno
)
334 elf_fpregset_t fpregs
;
336 if (ptrace (PTRACE_GETFPREGS
, tid
, 0, (int) &fpregs
) < 0)
337 perror_with_name (_("Couldn't get floating point status"));
339 fill_fpregset (regcache
, &fpregs
, regno
);
341 if (ptrace (PTRACE_SETFPREGS
, tid
, 0, (int) &fpregs
) < 0)
342 perror_with_name (_("Couldn't write floating point status"));
348 fetch_fpregs (struct regcache
*regcache
, int tid
)
353 store_fpregs (const struct regcache
*regcache
, int tid
, int regno
)
360 /* Transfering floating-point and SSE registers to and from GDB. */
362 /* Fetch all registers covered by the PTRACE_GETREGSET request from
363 process/thread TID and store their values in GDB's register array.
364 Return non-zero if successful, zero otherwise. */
367 fetch_xstateregs (struct regcache
*regcache
, int tid
)
369 char xstateregs
[I386_XSTATE_MAX_SIZE
];
372 if (!have_ptrace_getregset
)
375 iov
.iov_base
= xstateregs
;
376 iov
.iov_len
= sizeof(xstateregs
);
377 if (ptrace (PTRACE_GETREGSET
, tid
, (unsigned int) NT_X86_XSTATE
,
379 perror_with_name (_("Couldn't read extended state status"));
381 i387_supply_xsave (regcache
, -1, xstateregs
);
385 /* Store all valid registers in GDB's register array covered by the
386 PTRACE_SETREGSET request into the process/thread specified by TID.
387 Return non-zero if successful, zero otherwise. */
390 store_xstateregs (const struct regcache
*regcache
, int tid
, int regno
)
392 char xstateregs
[I386_XSTATE_MAX_SIZE
];
395 if (!have_ptrace_getregset
)
398 iov
.iov_base
= xstateregs
;
399 iov
.iov_len
= sizeof(xstateregs
);
400 if (ptrace (PTRACE_GETREGSET
, tid
, (unsigned int) NT_X86_XSTATE
,
402 perror_with_name (_("Couldn't read extended state status"));
404 i387_collect_xsave (regcache
, regno
, xstateregs
, 0);
406 if (ptrace (PTRACE_SETREGSET
, tid
, (unsigned int) NT_X86_XSTATE
,
408 perror_with_name (_("Couldn't write extended state status"));
413 #ifdef HAVE_PTRACE_GETFPXREGS
415 /* Fetch all registers covered by the PTRACE_GETFPXREGS request from
416 process/thread TID and store their values in GDB's register array.
417 Return non-zero if successful, zero otherwise. */
420 fetch_fpxregs (struct regcache
*regcache
, int tid
)
422 elf_fpxregset_t fpxregs
;
424 if (! have_ptrace_getfpxregs
)
427 if (ptrace (PTRACE_GETFPXREGS
, tid
, 0, (int) &fpxregs
) < 0)
431 have_ptrace_getfpxregs
= 0;
435 perror_with_name (_("Couldn't read floating-point and SSE registers"));
438 i387_supply_fxsave (regcache
, -1, (const elf_fpxregset_t
*) &fpxregs
);
442 /* Store all valid registers in GDB's register array covered by the
443 PTRACE_SETFPXREGS request into the process/thread specified by TID.
444 Return non-zero if successful, zero otherwise. */
447 store_fpxregs (const struct regcache
*regcache
, int tid
, int regno
)
449 elf_fpxregset_t fpxregs
;
451 if (! have_ptrace_getfpxregs
)
454 if (ptrace (PTRACE_GETFPXREGS
, tid
, 0, &fpxregs
) == -1)
458 have_ptrace_getfpxregs
= 0;
462 perror_with_name (_("Couldn't read floating-point and SSE registers"));
465 i387_collect_fxsave (regcache
, regno
, &fpxregs
);
467 if (ptrace (PTRACE_SETFPXREGS
, tid
, 0, &fpxregs
) == -1)
468 perror_with_name (_("Couldn't write floating-point and SSE registers"));
476 fetch_fpxregs (struct regcache
*regcache
, int tid
)
482 store_fpxregs (const struct regcache
*regcache
, int tid
, int regno
)
487 #endif /* HAVE_PTRACE_GETFPXREGS */
490 /* Transferring arbitrary registers between GDB and inferior. */
492 /* Fetch register REGNO from the child process. If REGNO is -1, do
493 this for all registers (including the floating point and SSE
497 i386_linux_fetch_inferior_registers (struct target_ops
*ops
,
498 struct regcache
*regcache
, int regno
)
502 /* Use the old method of peeking around in `struct user' if the
503 GETREGS request isn't available. */
504 if (!have_ptrace_getregs
)
508 for (i
= 0; i
< gdbarch_num_regs (get_regcache_arch (regcache
)); i
++)
509 if (regno
== -1 || regno
== i
)
510 fetch_register (regcache
, i
);
515 /* GNU/Linux LWP ID's are process ID's. */
516 tid
= TIDGET (inferior_ptid
);
518 tid
= PIDGET (inferior_ptid
); /* Not a threaded program. */
520 /* Use the PTRACE_GETFPXREGS request whenever possible, since it
521 transfers more registers in one system call, and we'll cache the
522 results. But remember that fetch_fpxregs can fail, and return
526 fetch_regs (regcache
, tid
);
528 /* The call above might reset `have_ptrace_getregs'. */
529 if (!have_ptrace_getregs
)
531 i386_linux_fetch_inferior_registers (ops
, regcache
, regno
);
535 if (fetch_xstateregs (regcache
, tid
))
537 if (fetch_fpxregs (regcache
, tid
))
539 fetch_fpregs (regcache
, tid
);
543 if (GETREGS_SUPPLIES (regno
))
545 fetch_regs (regcache
, tid
);
549 if (GETXSTATEREGS_SUPPLIES (regno
))
551 if (fetch_xstateregs (regcache
, tid
))
555 if (GETFPXREGS_SUPPLIES (regno
))
557 if (fetch_fpxregs (regcache
, tid
))
560 /* Either our processor or our kernel doesn't support the SSE
561 registers, so read the FP registers in the traditional way,
562 and fill the SSE registers with dummy values. It would be
563 more graceful to handle differences in the register set using
564 gdbarch. Until then, this will at least make things work
566 fetch_fpregs (regcache
, tid
);
570 internal_error (__FILE__
, __LINE__
,
571 _("Got request for bad register number %d."), regno
);
574 /* Store register REGNO back into the child process. If REGNO is -1,
575 do this for all registers (including the floating point and SSE
578 i386_linux_store_inferior_registers (struct target_ops
*ops
,
579 struct regcache
*regcache
, int regno
)
583 /* Use the old method of poking around in `struct user' if the
584 SETREGS request isn't available. */
585 if (!have_ptrace_getregs
)
589 for (i
= 0; i
< gdbarch_num_regs (get_regcache_arch (regcache
)); i
++)
590 if (regno
== -1 || regno
== i
)
591 store_register (regcache
, i
);
596 /* GNU/Linux LWP ID's are process ID's. */
597 tid
= TIDGET (inferior_ptid
);
599 tid
= PIDGET (inferior_ptid
); /* Not a threaded program. */
601 /* Use the PTRACE_SETFPXREGS requests whenever possible, since it
602 transfers more registers in one system call. But remember that
603 store_fpxregs can fail, and return zero. */
606 store_regs (regcache
, tid
, regno
);
607 if (store_xstateregs (regcache
, tid
, regno
))
609 if (store_fpxregs (regcache
, tid
, regno
))
611 store_fpregs (regcache
, tid
, regno
);
615 if (GETREGS_SUPPLIES (regno
))
617 store_regs (regcache
, tid
, regno
);
621 if (GETXSTATEREGS_SUPPLIES (regno
))
623 if (store_xstateregs (regcache
, tid
, regno
))
627 if (GETFPXREGS_SUPPLIES (regno
))
629 if (store_fpxregs (regcache
, tid
, regno
))
632 /* Either our processor or our kernel doesn't support the SSE
633 registers, so just write the FP registers in the traditional
635 store_fpregs (regcache
, tid
, regno
);
639 internal_error (__FILE__
, __LINE__
,
640 _("Got request to store bad register number %d."), regno
);
644 /* Support for debug registers. */
646 /* Get debug register REGNUM value from only the one LWP of PTID. */
649 i386_linux_dr_get (ptid_t ptid
, int regnum
)
659 value
= ptrace (PTRACE_PEEKUSER
, tid
,
660 offsetof (struct user
, u_debugreg
[regnum
]), 0);
662 perror_with_name (_("Couldn't read debug register"));
667 /* Set debug register REGNUM to VALUE in only the one LWP of PTID. */
670 i386_linux_dr_set (ptid_t ptid
, int regnum
, unsigned long value
)
679 ptrace (PTRACE_POKEUSER
, tid
,
680 offsetof (struct user
, u_debugreg
[regnum
]), value
);
682 perror_with_name (_("Couldn't write debug register"));
685 /* Return the inferior's debug register REGNUM. */
688 i386_linux_dr_get_addr (int regnum
)
690 /* DR6 and DR7 are retrieved with some other way. */
691 gdb_assert (DR_FIRSTADDR
<= regnum
&& regnum
<= DR_LASTADDR
);
693 return i386_linux_dr_get (inferior_ptid
, regnum
);
696 /* Return the inferior's DR7 debug control register. */
699 i386_linux_dr_get_control (void)
701 return i386_linux_dr_get (inferior_ptid
, DR_CONTROL
);
704 /* Get DR_STATUS from only the one LWP of INFERIOR_PTID. */
707 i386_linux_dr_get_status (void)
709 return i386_linux_dr_get (inferior_ptid
, DR_STATUS
);
712 /* Callback for iterate_over_lwps. Update the debug registers of
716 update_debug_registers_callback (struct lwp_info
*lwp
, void *arg
)
718 if (lwp
->arch_private
== NULL
)
719 lwp
->arch_private
= XCNEW (struct arch_lwp_info
);
721 /* The actual update is done later just before resuming the lwp, we
722 just mark that the registers need updating. */
723 lwp
->arch_private
->debug_registers_changed
= 1;
725 /* If the lwp isn't stopped, force it to momentarily pause, so we
726 can update its debug registers. */
728 linux_stop_lwp (lwp
);
733 /* Set DR_CONTROL to ADDR in all LWPs of the current inferior. */
736 i386_linux_dr_set_control (unsigned long control
)
738 ptid_t pid_ptid
= pid_to_ptid (ptid_get_pid (inferior_ptid
));
740 iterate_over_lwps (pid_ptid
, update_debug_registers_callback
, NULL
);
743 /* Set address REGNUM (zero based) to ADDR in all LWPs of the current
747 i386_linux_dr_set_addr (int regnum
, CORE_ADDR addr
)
749 ptid_t pid_ptid
= pid_to_ptid (ptid_get_pid (inferior_ptid
));
751 gdb_assert (regnum
>= 0 && regnum
<= DR_LASTADDR
- DR_FIRSTADDR
);
753 iterate_over_lwps (pid_ptid
, update_debug_registers_callback
, NULL
);
756 /* Called when resuming a thread.
757 If the debug regs have changed, update the thread's copies. */
760 i386_linux_prepare_to_resume (struct lwp_info
*lwp
)
762 int clear_status
= 0;
764 /* NULL means this is the main thread still going through the shell,
765 or, no watchpoint has been set yet. In that case, there's
767 if (lwp
->arch_private
== NULL
)
770 if (lwp
->arch_private
->debug_registers_changed
)
772 struct i386_debug_reg_state
*state
= i386_debug_reg_state ();
775 for (i
= DR_FIRSTADDR
; i
<= DR_LASTADDR
; i
++)
776 if (state
->dr_ref_count
[i
] > 0)
778 i386_linux_dr_set (lwp
->ptid
, i
, state
->dr_mirror
[i
]);
780 /* If we're setting a watchpoint, any change the inferior
781 had done itself to the debug registers needs to be
782 discarded, otherwise, i386_stopped_data_address can get
787 i386_linux_dr_set (lwp
->ptid
, DR_CONTROL
, state
->dr_control_mirror
);
789 lwp
->arch_private
->debug_registers_changed
= 0;
792 if (clear_status
|| lwp
->stopped_by_watchpoint
)
793 i386_linux_dr_set (lwp
->ptid
, DR_STATUS
, 0);
797 i386_linux_new_thread (struct lwp_info
*lp
)
799 struct arch_lwp_info
*info
= XCNEW (struct arch_lwp_info
);
801 info
->debug_registers_changed
= 1;
803 lp
->arch_private
= info
;
807 /* Called by libthread_db. Returns a pointer to the thread local
808 storage (or its descriptor). */
811 ps_get_thread_area (const struct ps_prochandle
*ph
,
812 lwpid_t lwpid
, int idx
, void **base
)
814 /* NOTE: cagney/2003-08-26: The definition of this buffer is found
815 in the kernel header <asm-i386/ldt.h>. It, after padding, is 4 x
816 4 byte integers in size: `entry_number', `base_addr', `limit',
817 and a bunch of status bits.
819 The values returned by this ptrace call should be part of the
820 regcache buffer, and ps_get_thread_area should channel its
821 request through the regcache. That way remote targets could
822 provide the value using the remote protocol and not this direct
825 Is this function needed? I'm guessing that the `base' is the
826 address of a descriptor that libthread_db uses to find the
827 thread local address base that GDB needs. Perhaps that
828 descriptor is defined by the ABI. Anyway, given that
829 libthread_db calls this function without prompting (gdb
830 requesting tls base) I guess it needs info in there anyway. */
831 unsigned int desc
[4];
832 gdb_assert (sizeof (int) == 4);
834 #ifndef PTRACE_GET_THREAD_AREA
835 #define PTRACE_GET_THREAD_AREA 25
838 if (ptrace (PTRACE_GET_THREAD_AREA
, lwpid
,
839 (void *) idx
, (unsigned long) &desc
) < 0)
842 *(int *)base
= desc
[1];
847 /* The instruction for a GNU/Linux system call is:
851 static const unsigned char linux_syscall
[] = { 0xcd, 0x80 };
853 #define LINUX_SYSCALL_LEN (sizeof linux_syscall)
855 /* The system call number is stored in the %eax register. */
856 #define LINUX_SYSCALL_REGNUM I386_EAX_REGNUM
858 /* We are specifically interested in the sigreturn and rt_sigreturn
861 #ifndef SYS_sigreturn
862 #define SYS_sigreturn 0x77
864 #ifndef SYS_rt_sigreturn
865 #define SYS_rt_sigreturn 0xad
868 /* Offset to saved processor flags, from <asm/sigcontext.h>. */
869 #define LINUX_SIGCONTEXT_EFLAGS_OFFSET (64)
871 /* Resume execution of the inferior process.
872 If STEP is nonzero, single-step it.
873 If SIGNAL is nonzero, give it that signal. */
876 i386_linux_resume (struct target_ops
*ops
,
877 ptid_t ptid
, int step
, enum target_signal signal
)
879 int pid
= PIDGET (ptid
);
883 if (catch_syscall_enabled () > 0)
884 request
= PTRACE_SYSCALL
;
886 request
= PTRACE_CONT
;
890 struct regcache
*regcache
= get_thread_regcache (pid_to_ptid (pid
));
891 struct gdbarch
*gdbarch
= get_regcache_arch (regcache
);
892 enum bfd_endian byte_order
= gdbarch_byte_order (gdbarch
);
894 gdb_byte buf
[LINUX_SYSCALL_LEN
];
896 request
= PTRACE_SINGLESTEP
;
898 regcache_cooked_read_unsigned (regcache
,
899 gdbarch_pc_regnum (gdbarch
), &pc
);
901 /* Returning from a signal trampoline is done by calling a
902 special system call (sigreturn or rt_sigreturn, see
903 i386-linux-tdep.c for more information). This system call
904 restores the registers that were saved when the signal was
905 raised, including %eflags. That means that single-stepping
906 won't work. Instead, we'll have to modify the signal context
907 that's about to be restored, and set the trace flag there. */
909 /* First check if PC is at a system call. */
910 if (target_read_memory (pc
, buf
, LINUX_SYSCALL_LEN
) == 0
911 && memcmp (buf
, linux_syscall
, LINUX_SYSCALL_LEN
) == 0)
914 regcache_cooked_read_unsigned (regcache
,
915 LINUX_SYSCALL_REGNUM
, &syscall
);
917 /* Then check the system call number. */
918 if (syscall
== SYS_sigreturn
|| syscall
== SYS_rt_sigreturn
)
921 unsigned long int eflags
;
923 regcache_cooked_read_unsigned (regcache
, I386_ESP_REGNUM
, &sp
);
924 if (syscall
== SYS_rt_sigreturn
)
925 addr
= read_memory_unsigned_integer (sp
+ 8, 4, byte_order
)
930 /* Set the trace flag in the context that's about to be
932 addr
+= LINUX_SIGCONTEXT_EFLAGS_OFFSET
;
933 read_memory (addr
, (gdb_byte
*) &eflags
, 4);
935 write_memory (addr
, (gdb_byte
*) &eflags
, 4);
940 if (ptrace (request
, pid
, 0, target_signal_to_host (signal
)) == -1)
941 perror_with_name (("ptrace"));
944 static void (*super_post_startup_inferior
) (ptid_t ptid
);
947 i386_linux_child_post_startup_inferior (ptid_t ptid
)
949 i386_cleanup_dregs ();
950 super_post_startup_inferior (ptid
);
953 /* Get Linux/x86 target description from running target. */
955 static const struct target_desc
*
956 i386_linux_read_description (struct target_ops
*ops
)
959 static uint64_t xcr0
;
961 /* GNU/Linux LWP ID's are process ID's. */
962 tid
= TIDGET (inferior_ptid
);
964 tid
= PIDGET (inferior_ptid
); /* Not a threaded program. */
966 #ifdef HAVE_PTRACE_GETFPXREGS
967 if (have_ptrace_getfpxregs
== -1)
969 elf_fpxregset_t fpxregs
;
971 if (ptrace (PTRACE_GETFPXREGS
, tid
, 0, (int) &fpxregs
) < 0)
973 have_ptrace_getfpxregs
= 0;
974 have_ptrace_getregset
= 0;
975 return tdesc_i386_mmx_linux
;
980 if (have_ptrace_getregset
== -1)
982 uint64_t xstateregs
[(I386_XSTATE_SSE_SIZE
/ sizeof (uint64_t))];
985 iov
.iov_base
= xstateregs
;
986 iov
.iov_len
= sizeof (xstateregs
);
988 /* Check if PTRACE_GETREGSET works. */
989 if (ptrace (PTRACE_GETREGSET
, tid
, (unsigned int) NT_X86_XSTATE
,
991 have_ptrace_getregset
= 0;
994 have_ptrace_getregset
= 1;
996 /* Get XCR0 from XSAVE extended state. */
997 xcr0
= xstateregs
[(I386_LINUX_XSAVE_XCR0_OFFSET
998 / sizeof (long long))];
1002 /* Check the native XCR0 only if PTRACE_GETREGSET is available. */
1003 if (have_ptrace_getregset
1004 && (xcr0
& I386_XSTATE_AVX_MASK
) == I386_XSTATE_AVX_MASK
)
1005 return tdesc_i386_avx_linux
;
1007 return tdesc_i386_linux
;
1011 _initialize_i386_linux_nat (void)
1013 struct target_ops
*t
;
1015 /* Fill in the generic GNU/Linux methods. */
1016 t
= linux_target ();
1018 i386_use_watchpoints (t
);
1020 i386_dr_low
.set_control
= i386_linux_dr_set_control
;
1021 i386_dr_low
.set_addr
= i386_linux_dr_set_addr
;
1022 i386_dr_low
.get_addr
= i386_linux_dr_get_addr
;
1023 i386_dr_low
.get_status
= i386_linux_dr_get_status
;
1024 i386_dr_low
.get_control
= i386_linux_dr_get_control
;
1025 i386_set_debug_register_length (4);
1027 /* Override the default ptrace resume method. */
1028 t
->to_resume
= i386_linux_resume
;
1030 /* Override the GNU/Linux inferior startup hook. */
1031 super_post_startup_inferior
= t
->to_post_startup_inferior
;
1032 t
->to_post_startup_inferior
= i386_linux_child_post_startup_inferior
;
1034 /* Add our register access methods. */
1035 t
->to_fetch_registers
= i386_linux_fetch_inferior_registers
;
1036 t
->to_store_registers
= i386_linux_store_inferior_registers
;
1038 t
->to_read_description
= i386_linux_read_description
;
1040 /* Register the target. */
1041 linux_nat_add_target (t
);
1042 linux_nat_set_new_thread (t
, i386_linux_new_thread
);
1043 linux_nat_set_prepare_to_resume (t
, i386_linux_prepare_to_resume
);