1 /* Native-dependent code for GNU/Linux i386.
3 Copyright (C) 1999-2014 Free Software Foundation, Inc.
5 This file is part of GDB.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
27 #include "linux-nat.h"
28 #include "nat/linux-btrace.h"
31 #include "gdb_assert.h"
33 #include "elf/common.h"
35 #include <sys/ptrace.h>
37 #include <sys/procfs.h>
47 #ifdef HAVE_SYS_DEBUGREG_H
48 #include <sys/debugreg.h>
51 /* Prototypes for supply_gregset etc. */
54 #include "i387-tdep.h"
55 #include "i386-tdep.h"
56 #include "i386-linux-tdep.h"
58 /* Defines ps_err_e, struct ps_prochandle. */
59 #include "gdb_proc_service.h"
61 #include "i386-xstate.h"
63 #ifndef PTRACE_GETREGSET
64 #define PTRACE_GETREGSET 0x4204
67 #ifndef PTRACE_SETREGSET
68 #define PTRACE_SETREGSET 0x4205
71 /* Per-thread arch-specific data we want to keep. */
75 /* Non-zero if our copy differs from what's recorded in the thread. */
76 int debug_registers_changed;
79 /* Does the current host support PTRACE_GETREGSET? */
80 static int have_ptrace_getregset = -1;
83 /* The register sets used in GNU/Linux ELF core-dumps are identical to
84 the register sets in `struct user' that is used for a.out
85 core-dumps, and is also used by `ptrace'. The corresponding types
86 are `elf_gregset_t' for the general-purpose registers (with
87 `elf_greg_t' the type of a single GP register) and `elf_fpregset_t'
88 for the floating-point registers.
90 Those types used to be available under the names `gregset_t' and
91 `fpregset_t' too, and this file used those names in the past. But
92 those names are now used for the register sets used in the
93 `mcontext_t' type, and have a different size and layout. */
95 /* Which ptrace request retrieves which registers?
96 These apply to the corresponding SET requests as well. */
98 #define GETREGS_SUPPLIES(regno) \
99 ((0 <= (regno) && (regno) <= 15) || (regno) == I386_LINUX_ORIG_EAX_REGNUM)
101 #define GETFPXREGS_SUPPLIES(regno) \
102 (I386_ST0_REGNUM <= (regno) && (regno) < I386_SSE_NUM_REGS)
104 #define GETXSTATEREGS_SUPPLIES(regno) \
105 (I386_ST0_REGNUM <= (regno) && (regno) < I386_AVX512_NUM_REGS)
107 /* Does the current host support the GETREGS request? */
108 int have_ptrace_getregs =
109 #ifdef HAVE_PTRACE_GETREGS
116 /* Does the current host support the GETFPXREGS request? The header
117 file may or may not define it, and even if it is defined, the
118 kernel will return EIO if it's running on a pre-SSE processor.
120 My instinct is to attach this to some architecture- or
121 target-specific data structure, but really, a particular GDB
122 process can only run on top of one kernel at a time. So it's okay
123 for this to be a simple variable. */
124 int have_ptrace_getfpxregs =
125 #ifdef HAVE_PTRACE_GETFPXREGS
133 /* Accessing registers through the U area, one at a time. */
135 /* Fetch one register. */
138 fetch_register (struct regcache *regcache, int regno)
143 gdb_assert (!have_ptrace_getregs);
144 if (i386_linux_gregset_reg_offset[regno] == -1)
146 regcache_raw_supply (regcache, regno, NULL);
150 /* GNU/Linux LWP ID's are process ID's. */
151 tid = ptid_get_lwp (inferior_ptid);
153 tid = ptid_get_pid (inferior_ptid); /* Not a threaded program. */
156 val = ptrace (PTRACE_PEEKUSER, tid,
157 i386_linux_gregset_reg_offset[regno], 0);
159 error (_("Couldn't read register %s (#%d): %s."),
160 gdbarch_register_name (get_regcache_arch (regcache), regno),
161 regno, safe_strerror (errno));
163 regcache_raw_supply (regcache, regno, &val);
166 /* Store one register. */
169 store_register (const struct regcache *regcache, int regno)
174 gdb_assert (!have_ptrace_getregs);
175 if (i386_linux_gregset_reg_offset[regno] == -1)
178 /* GNU/Linux LWP ID's are process ID's. */
179 tid = ptid_get_lwp (inferior_ptid);
181 tid = ptid_get_pid (inferior_ptid); /* Not a threaded program. */
184 regcache_raw_collect (regcache, regno, &val);
185 ptrace (PTRACE_POKEUSER, tid,
186 i386_linux_gregset_reg_offset[regno], val);
188 error (_("Couldn't write register %s (#%d): %s."),
189 gdbarch_register_name (get_regcache_arch (regcache), regno),
190 regno, safe_strerror (errno));
194 /* Transfering the general-purpose registers between GDB, inferiors
197 /* Fill GDB's register array with the general-purpose register values
201 supply_gregset (struct regcache *regcache, const elf_gregset_t *gregsetp)
203 const gdb_byte *regp = (const gdb_byte *) gregsetp;
206 for (i = 0; i < I386_NUM_GREGS; i++)
207 regcache_raw_supply (regcache, i,
208 regp + i386_linux_gregset_reg_offset[i]);
210 if (I386_LINUX_ORIG_EAX_REGNUM
211 < gdbarch_num_regs (get_regcache_arch (regcache)))
212 regcache_raw_supply (regcache, I386_LINUX_ORIG_EAX_REGNUM, regp
213 + i386_linux_gregset_reg_offset[I386_LINUX_ORIG_EAX_REGNUM]);
216 /* Fill register REGNO (if it is a general-purpose register) in
217 *GREGSETPS with the value in GDB's register array. If REGNO is -1,
218 do this for all registers. */
221 fill_gregset (const struct regcache *regcache,
222 elf_gregset_t *gregsetp, int regno)
224 gdb_byte *regp = (gdb_byte *) gregsetp;
227 for (i = 0; i < I386_NUM_GREGS; i++)
228 if (regno == -1 || regno == i)
229 regcache_raw_collect (regcache, i,
230 regp + i386_linux_gregset_reg_offset[i]);
232 if ((regno == -1 || regno == I386_LINUX_ORIG_EAX_REGNUM)
233 && I386_LINUX_ORIG_EAX_REGNUM
234 < gdbarch_num_regs (get_regcache_arch (regcache)))
235 regcache_raw_collect (regcache, I386_LINUX_ORIG_EAX_REGNUM, regp
236 + i386_linux_gregset_reg_offset[I386_LINUX_ORIG_EAX_REGNUM]);
239 #ifdef HAVE_PTRACE_GETREGS
241 /* Fetch all general-purpose registers from process/thread TID and
242 store their values in GDB's register array. */
245 fetch_regs (struct regcache *regcache, int tid)
248 elf_gregset_t *regs_p = ®s;
250 if (ptrace (PTRACE_GETREGS, tid, 0, (int) ®s) < 0)
254 /* The kernel we're running on doesn't support the GETREGS
255 request. Reset `have_ptrace_getregs'. */
256 have_ptrace_getregs = 0;
260 perror_with_name (_("Couldn't get registers"));
263 supply_gregset (regcache, (const elf_gregset_t *) regs_p);
266 /* Store all valid general-purpose registers in GDB's register array
267 into the process/thread specified by TID. */
270 store_regs (const struct regcache *regcache, int tid, int regno)
274 if (ptrace (PTRACE_GETREGS, tid, 0, (int) ®s) < 0)
275 perror_with_name (_("Couldn't get registers"));
277 fill_gregset (regcache, ®s, regno);
279 if (ptrace (PTRACE_SETREGS, tid, 0, (int) ®s) < 0)
280 perror_with_name (_("Couldn't write registers"));
285 static void fetch_regs (struct regcache *regcache, int tid) {}
286 static void store_regs (const struct regcache *regcache, int tid, int regno) {}
291 /* Transfering floating-point registers between GDB, inferiors and cores. */
293 /* Fill GDB's register array with the floating-point register values in
297 supply_fpregset (struct regcache *regcache, const elf_fpregset_t *fpregsetp)
299 i387_supply_fsave (regcache, -1, fpregsetp);
302 /* Fill register REGNO (if it is a floating-point register) in
303 *FPREGSETP with the value in GDB's register array. If REGNO is -1,
304 do this for all registers. */
307 fill_fpregset (const struct regcache *regcache,
308 elf_fpregset_t *fpregsetp, int regno)
310 i387_collect_fsave (regcache, regno, fpregsetp);
313 #ifdef HAVE_PTRACE_GETREGS
315 /* Fetch all floating-point registers from process/thread TID and store
316 thier values in GDB's register array. */
319 fetch_fpregs (struct regcache *regcache, int tid)
321 elf_fpregset_t fpregs;
323 if (ptrace (PTRACE_GETFPREGS, tid, 0, (int) &fpregs) < 0)
324 perror_with_name (_("Couldn't get floating point status"));
326 supply_fpregset (regcache, (const elf_fpregset_t *) &fpregs);
329 /* Store all valid floating-point registers in GDB's register array
330 into the process/thread specified by TID. */
333 store_fpregs (const struct regcache *regcache, int tid, int regno)
335 elf_fpregset_t fpregs;
337 if (ptrace (PTRACE_GETFPREGS, tid, 0, (int) &fpregs) < 0)
338 perror_with_name (_("Couldn't get floating point status"));
340 fill_fpregset (regcache, &fpregs, regno);
342 if (ptrace (PTRACE_SETFPREGS, tid, 0, (int) &fpregs) < 0)
343 perror_with_name (_("Couldn't write floating point status"));
349 fetch_fpregs (struct regcache *regcache, int tid)
354 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 /* Fetch all registers covered by the PTRACE_GETFPXREGS request from
417 process/thread TID and store their values in GDB's register array.
418 Return non-zero if successful, zero otherwise. */
421 fetch_fpxregs (struct regcache *regcache, int tid)
423 elf_fpxregset_t fpxregs;
425 if (! have_ptrace_getfpxregs)
428 if (ptrace (PTRACE_GETFPXREGS, tid, 0, (int) &fpxregs) < 0)
432 have_ptrace_getfpxregs = 0;
436 perror_with_name (_("Couldn't read floating-point and SSE registers"));
439 i387_supply_fxsave (regcache, -1, (const elf_fpxregset_t *) &fpxregs);
443 /* Store all valid registers in GDB's register array covered by the
444 PTRACE_SETFPXREGS request into the process/thread specified by TID.
445 Return non-zero if successful, zero otherwise. */
448 store_fpxregs (const struct regcache *regcache, int tid, int regno)
450 elf_fpxregset_t fpxregs;
452 if (! have_ptrace_getfpxregs)
455 if (ptrace (PTRACE_GETFPXREGS, tid, 0, &fpxregs) == -1)
459 have_ptrace_getfpxregs = 0;
463 perror_with_name (_("Couldn't read floating-point and SSE registers"));
466 i387_collect_fxsave (regcache, regno, &fpxregs);
468 if (ptrace (PTRACE_SETFPXREGS, tid, 0, &fpxregs) == -1)
469 perror_with_name (_("Couldn't write floating-point and SSE registers"));
477 fetch_fpxregs (struct regcache *regcache, int tid)
483 store_fpxregs (const struct regcache *regcache, int tid, int regno)
488 #endif /* HAVE_PTRACE_GETFPXREGS */
491 /* Transferring arbitrary registers between GDB and inferior. */
493 /* Fetch register REGNO from the child process. If REGNO is -1, do
494 this for all registers (including the floating point and SSE
498 i386_linux_fetch_inferior_registers (struct target_ops *ops,
499 struct regcache *regcache, int regno)
503 /* Use the old method of peeking around in `struct user' if the
504 GETREGS request isn't available. */
505 if (!have_ptrace_getregs)
509 for (i = 0; i < gdbarch_num_regs (get_regcache_arch (regcache)); i++)
510 if (regno == -1 || regno == i)
511 fetch_register (regcache, i);
516 /* GNU/Linux LWP ID's are process ID's. */
517 tid = ptid_get_lwp (inferior_ptid);
519 tid = ptid_get_pid (inferior_ptid); /* Not a threaded program. */
521 /* Use the PTRACE_GETFPXREGS request whenever possible, since it
522 transfers more registers in one system call, and we'll cache the
523 results. But remember that fetch_fpxregs can fail, and return
527 fetch_regs (regcache, tid);
529 /* The call above might reset `have_ptrace_getregs'. */
530 if (!have_ptrace_getregs)
532 i386_linux_fetch_inferior_registers (ops, regcache, regno);
536 if (fetch_xstateregs (regcache, tid))
538 if (fetch_fpxregs (regcache, tid))
540 fetch_fpregs (regcache, tid);
544 if (GETREGS_SUPPLIES (regno))
546 fetch_regs (regcache, tid);
550 if (GETXSTATEREGS_SUPPLIES (regno))
552 if (fetch_xstateregs (regcache, tid))
556 if (GETFPXREGS_SUPPLIES (regno))
558 if (fetch_fpxregs (regcache, tid))
561 /* Either our processor or our kernel doesn't support the SSE
562 registers, so read the FP registers in the traditional way,
563 and fill the SSE registers with dummy values. It would be
564 more graceful to handle differences in the register set using
565 gdbarch. Until then, this will at least make things work
567 fetch_fpregs (regcache, tid);
571 internal_error (__FILE__, __LINE__,
572 _("Got request for bad register number %d."), regno);
575 /* Store register REGNO back into the child process. If REGNO is -1,
576 do this for all registers (including the floating point and SSE
579 i386_linux_store_inferior_registers (struct target_ops *ops,
580 struct regcache *regcache, int regno)
584 /* Use the old method of poking around in `struct user' if the
585 SETREGS request isn't available. */
586 if (!have_ptrace_getregs)
590 for (i = 0; i < gdbarch_num_regs (get_regcache_arch (regcache)); i++)
591 if (regno == -1 || regno == i)
592 store_register (regcache, i);
597 /* GNU/Linux LWP ID's are process ID's. */
598 tid = ptid_get_lwp (inferior_ptid);
600 tid = ptid_get_pid (inferior_ptid); /* Not a threaded program. */
602 /* Use the PTRACE_SETFPXREGS requests whenever possible, since it
603 transfers more registers in one system call. But remember that
604 store_fpxregs can fail, and return zero. */
607 store_regs (regcache, tid, regno);
608 if (store_xstateregs (regcache, tid, regno))
610 if (store_fpxregs (regcache, tid, regno))
612 store_fpregs (regcache, tid, regno);
616 if (GETREGS_SUPPLIES (regno))
618 store_regs (regcache, tid, regno);
622 if (GETXSTATEREGS_SUPPLIES (regno))
624 if (store_xstateregs (regcache, tid, regno))
628 if (GETFPXREGS_SUPPLIES (regno))
630 if (store_fpxregs (regcache, tid, regno))
633 /* Either our processor or our kernel doesn't support the SSE
634 registers, so just write the FP registers in the traditional
636 store_fpregs (regcache, tid, regno);
640 internal_error (__FILE__, __LINE__,
641 _("Got request to store bad register number %d."), regno);
645 /* Support for debug registers. */
647 /* Get debug register REGNUM value from only the one LWP of PTID. */
650 x86_linux_dr_get (ptid_t ptid, int regnum)
655 tid = ptid_get_lwp (ptid);
657 tid = ptid_get_pid (ptid);
660 value = ptrace (PTRACE_PEEKUSER, tid,
661 offsetof (struct user, u_debugreg[regnum]), 0);
663 perror_with_name (_("Couldn't read debug register"));
668 /* Set debug register REGNUM to VALUE in only the one LWP of PTID. */
671 x86_linux_dr_set (ptid_t ptid, int regnum, unsigned long value)
675 tid = ptid_get_lwp (ptid);
677 tid = ptid_get_pid (ptid);
680 ptrace (PTRACE_POKEUSER, tid,
681 offsetof (struct user, u_debugreg[regnum]), value);
683 perror_with_name (_("Couldn't write debug register"));
686 /* Return the inferior's debug register REGNUM. */
689 x86_linux_dr_get_addr (int regnum)
691 /* DR6 and DR7 are retrieved with some other way. */
692 gdb_assert (DR_FIRSTADDR <= regnum && regnum <= DR_LASTADDR);
694 return x86_linux_dr_get (inferior_ptid, regnum);
697 /* Return the inferior's DR7 debug control register. */
700 x86_linux_dr_get_control (void)
702 return x86_linux_dr_get (inferior_ptid, DR_CONTROL);
705 /* Get DR_STATUS from only the one LWP of INFERIOR_PTID. */
708 x86_linux_dr_get_status (void)
710 return x86_linux_dr_get (inferior_ptid, DR_STATUS);
713 /* Callback for iterate_over_lwps. Update the debug registers of
717 update_debug_registers_callback (struct lwp_info *lwp, void *arg)
719 if (lwp->arch_private == NULL)
720 lwp->arch_private = XCNEW (struct arch_lwp_info);
722 /* The actual update is done later just before resuming the lwp, we
723 just mark that the registers need updating. */
724 lwp->arch_private->debug_registers_changed = 1;
726 /* If the lwp isn't stopped, force it to momentarily pause, so we
727 can update its debug registers. */
729 linux_stop_lwp (lwp);
731 /* Continue the iteration. */
735 /* Set DR_CONTROL to CONTROL in all LWPs of the current inferior. */
738 x86_linux_dr_set_control (unsigned long control)
740 ptid_t pid_ptid = pid_to_ptid (ptid_get_pid (inferior_ptid));
742 iterate_over_lwps (pid_ptid, update_debug_registers_callback, NULL);
745 /* Set address REGNUM (zero based) to ADDR in all LWPs of the current
749 x86_linux_dr_set_addr (int regnum, CORE_ADDR addr)
751 ptid_t pid_ptid = pid_to_ptid (ptid_get_pid (inferior_ptid));
753 gdb_assert (regnum >= 0 && regnum <= DR_LASTADDR - DR_FIRSTADDR);
755 iterate_over_lwps (pid_ptid, update_debug_registers_callback, NULL);
758 /* Called when resuming a thread.
759 If the debug regs have changed, update the thread's copies. */
762 x86_linux_prepare_to_resume (struct lwp_info *lwp)
764 int clear_status = 0;
766 /* NULL means this is the main thread still going through the shell,
767 or, no watchpoint has been set yet. In that case, there's
769 if (lwp->arch_private == NULL)
772 if (lwp->arch_private->debug_registers_changed)
774 struct i386_debug_reg_state *state
775 = i386_debug_reg_state (ptid_get_pid (lwp->ptid));
778 /* On Linux kernel before 2.6.33 commit
779 72f674d203cd230426437cdcf7dd6f681dad8b0d
780 if you enable a breakpoint by the DR_CONTROL bits you need to have
781 already written the corresponding DR_FIRSTADDR...DR_LASTADDR registers.
783 Ensure DR_CONTROL gets written as the very last register here. */
785 /* Clear DR_CONTROL first. In some cases, setting DR0-3 to a
786 value that doesn't match what is enabled in DR_CONTROL
787 results in EINVAL. */
788 x86_linux_dr_set (lwp->ptid, DR_CONTROL, 0);
790 for (i = DR_FIRSTADDR; i <= DR_LASTADDR; i++)
791 if (state->dr_ref_count[i] > 0)
793 x86_linux_dr_set (lwp->ptid, i, state->dr_mirror[i]);
795 /* If we're setting a watchpoint, any change the inferior
796 had done itself to the debug registers needs to be
797 discarded, otherwise, i386_stopped_data_address can get
802 /* If DR_CONTROL is supposed to be zero, we've already set it
804 if (state->dr_control_mirror != 0)
805 x86_linux_dr_set (lwp->ptid, DR_CONTROL, state->dr_control_mirror);
807 lwp->arch_private->debug_registers_changed = 0;
810 if (clear_status || lwp->stopped_by_watchpoint)
811 x86_linux_dr_set (lwp->ptid, DR_STATUS, 0);
815 x86_linux_new_thread (struct lwp_info *lp)
817 struct arch_lwp_info *info = XCNEW (struct arch_lwp_info);
819 info->debug_registers_changed = 1;
821 lp->arch_private = info;
824 /* linux_nat_new_fork hook. */
827 x86_linux_new_fork (struct lwp_info *parent, pid_t child_pid)
830 struct i386_debug_reg_state *parent_state;
831 struct i386_debug_reg_state *child_state;
833 /* NULL means no watchpoint has ever been set in the parent. In
834 that case, there's nothing to do. */
835 if (parent->arch_private == NULL)
838 /* Linux kernel before 2.6.33 commit
839 72f674d203cd230426437cdcf7dd6f681dad8b0d
840 will inherit hardware debug registers from parent
841 on fork/vfork/clone. Newer Linux kernels create such tasks with
842 zeroed debug registers.
844 GDB core assumes the child inherits the watchpoints/hw
845 breakpoints of the parent, and will remove them all from the
846 forked off process. Copy the debug registers mirrors into the
847 new process so that all breakpoints and watchpoints can be
848 removed together. The debug registers mirror will become zeroed
849 in the end before detaching the forked off process, thus making
850 this compatible with older Linux kernels too. */
852 parent_pid = ptid_get_pid (parent->ptid);
853 parent_state = i386_debug_reg_state (parent_pid);
854 child_state = i386_debug_reg_state (child_pid);
855 *child_state = *parent_state;
860 /* Helper for ps_get_thread_area. Sets BASE_ADDR to a pointer to
861 the thread local storage (or its descriptor) and returns PS_OK
862 on success. Returns PS_ERR on failure. */
865 x86_linux_get_thread_area (pid_t pid, void *addr, unsigned int *base_addr)
867 /* NOTE: cagney/2003-08-26: The definition of this buffer is found
868 in the kernel header <asm-i386/ldt.h>. It, after padding, is 4 x
869 4 byte integers in size: `entry_number', `base_addr', `limit',
870 and a bunch of status bits.
872 The values returned by this ptrace call should be part of the
873 regcache buffer, and ps_get_thread_area should channel its
874 request through the regcache. That way remote targets could
875 provide the value using the remote protocol and not this direct
878 Is this function needed? I'm guessing that the `base' is the
879 address of a descriptor that libthread_db uses to find the
880 thread local address base that GDB needs. Perhaps that
881 descriptor is defined by the ABI. Anyway, given that
882 libthread_db calls this function without prompting (gdb
883 requesting tls base) I guess it needs info in there anyway. */
884 unsigned int desc[4];
886 /* This code assumes that "int" is 32 bits and that
887 GET_THREAD_AREA returns no more than 4 int values. */
888 gdb_assert (sizeof (int) == 4);
890 #ifndef PTRACE_GET_THREAD_AREA
891 #define PTRACE_GET_THREAD_AREA 25
894 if (ptrace (PTRACE_GET_THREAD_AREA, pid, addr, &desc) < 0)
897 *base_addr = desc[1];
901 /* Called by libthread_db. Returns a pointer to the thread local
902 storage (or its descriptor). */
905 ps_get_thread_area (const struct ps_prochandle *ph,
906 lwpid_t lwpid, int idx, void **base)
908 unsigned int base_addr;
911 result = x86_linux_get_thread_area (lwpid, (void *) idx, &base_addr);
914 *(int *) base = base_addr;
920 /* The instruction for a GNU/Linux system call is:
924 static const unsigned char linux_syscall[] = { 0xcd, 0x80 };
926 #define LINUX_SYSCALL_LEN (sizeof linux_syscall)
928 /* The system call number is stored in the %eax register. */
929 #define LINUX_SYSCALL_REGNUM I386_EAX_REGNUM
931 /* We are specifically interested in the sigreturn and rt_sigreturn
934 #ifndef SYS_sigreturn
935 #define SYS_sigreturn 0x77
937 #ifndef SYS_rt_sigreturn
938 #define SYS_rt_sigreturn 0xad
941 /* Offset to saved processor flags, from <asm/sigcontext.h>. */
942 #define LINUX_SIGCONTEXT_EFLAGS_OFFSET (64)
944 /* Resume execution of the inferior process.
945 If STEP is nonzero, single-step it.
946 If SIGNAL is nonzero, give it that signal. */
949 i386_linux_resume (struct target_ops *ops,
950 ptid_t ptid, int step, enum gdb_signal signal)
952 int pid = ptid_get_pid (ptid);
956 if (catch_syscall_enabled () > 0)
957 request = PTRACE_SYSCALL;
959 request = PTRACE_CONT;
963 struct regcache *regcache = get_thread_regcache (pid_to_ptid (pid));
964 struct gdbarch *gdbarch = get_regcache_arch (regcache);
965 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
967 gdb_byte buf[LINUX_SYSCALL_LEN];
969 request = PTRACE_SINGLESTEP;
971 regcache_cooked_read_unsigned (regcache,
972 gdbarch_pc_regnum (gdbarch), &pc);
974 /* Returning from a signal trampoline is done by calling a
975 special system call (sigreturn or rt_sigreturn, see
976 i386-linux-tdep.c for more information). This system call
977 restores the registers that were saved when the signal was
978 raised, including %eflags. That means that single-stepping
979 won't work. Instead, we'll have to modify the signal context
980 that's about to be restored, and set the trace flag there. */
982 /* First check if PC is at a system call. */
983 if (target_read_memory (pc, buf, LINUX_SYSCALL_LEN) == 0
984 && memcmp (buf, linux_syscall, LINUX_SYSCALL_LEN) == 0)
987 regcache_cooked_read_unsigned (regcache,
988 LINUX_SYSCALL_REGNUM, &syscall);
990 /* Then check the system call number. */
991 if (syscall == SYS_sigreturn || syscall == SYS_rt_sigreturn)
994 unsigned long int eflags;
996 regcache_cooked_read_unsigned (regcache, I386_ESP_REGNUM, &sp);
997 if (syscall == SYS_rt_sigreturn)
998 addr = read_memory_unsigned_integer (sp + 8, 4, byte_order)
1003 /* Set the trace flag in the context that's about to be
1005 addr += LINUX_SIGCONTEXT_EFLAGS_OFFSET;
1006 read_memory (addr, (gdb_byte *) &eflags, 4);
1008 write_memory (addr, (gdb_byte *) &eflags, 4);
1013 if (ptrace (request, pid, 0, gdb_signal_to_host (signal)) == -1)
1014 perror_with_name (("ptrace"));
1017 static void (*super_post_startup_inferior) (struct target_ops *self,
1021 x86_linux_child_post_startup_inferior (struct target_ops *self, ptid_t ptid)
1023 i386_cleanup_dregs ();
1024 super_post_startup_inferior (self, ptid);
1028 /* Value of CS segment register:
1030 32bit process: 0x23 */
1031 #define AMD64_LINUX_USER64_CS 0x33
1033 /* Value of DS segment register:
1035 X32 process: 0x2b */
1036 #define AMD64_LINUX_X32_DS 0x2b
1039 /* Get Linux/x86 target description from running target. */
1041 static const struct target_desc *
1042 x86_linux_read_description (struct target_ops *ops)
1049 static uint64_t xcr0;
1050 uint64_t xcr0_features_bits;
1052 /* GNU/Linux LWP ID's are process ID's. */
1053 tid = ptid_get_lwp (inferior_ptid);
1055 tid = ptid_get_pid (inferior_ptid); /* Not a threaded program. */
1062 /* Get CS register. */
1064 cs = ptrace (PTRACE_PEEKUSER, tid,
1065 offsetof (struct user_regs_struct, cs), 0);
1067 perror_with_name (_("Couldn't get CS register"));
1069 is_64bit = cs == AMD64_LINUX_USER64_CS;
1071 /* Get DS register. */
1073 ds = ptrace (PTRACE_PEEKUSER, tid,
1074 offsetof (struct user_regs_struct, ds), 0);
1076 perror_with_name (_("Couldn't get DS register"));
1078 is_x32 = ds == AMD64_LINUX_X32_DS;
1080 if (sizeof (void *) == 4 && is_64bit && !is_x32)
1081 error (_("Can't debug 64-bit process with 32-bit GDB"));
1083 #elif HAVE_PTRACE_GETFPXREGS
1084 if (have_ptrace_getfpxregs == -1)
1086 elf_fpxregset_t fpxregs;
1088 if (ptrace (PTRACE_GETFPXREGS, tid, 0, (int) &fpxregs) < 0)
1090 have_ptrace_getfpxregs = 0;
1091 have_ptrace_getregset = 0;
1092 return tdesc_i386_mmx_linux;
1097 if (have_ptrace_getregset == -1)
1099 uint64_t xstateregs[(I386_XSTATE_SSE_SIZE / sizeof (uint64_t))];
1102 iov.iov_base = xstateregs;
1103 iov.iov_len = sizeof (xstateregs);
1105 /* Check if PTRACE_GETREGSET works. */
1106 if (ptrace (PTRACE_GETREGSET, tid,
1107 (unsigned int) NT_X86_XSTATE, &iov) < 0)
1108 have_ptrace_getregset = 0;
1111 have_ptrace_getregset = 1;
1113 /* Get XCR0 from XSAVE extended state. */
1114 xcr0 = xstateregs[(I386_LINUX_XSAVE_XCR0_OFFSET
1115 / sizeof (uint64_t))];
1119 /* Check the native XCR0 only if PTRACE_GETREGSET is available. If
1120 PTRACE_GETREGSET is not available then set xcr0_features_bits to
1121 zero so that the "no-features" descriptions are returned by the
1123 if (have_ptrace_getregset)
1124 xcr0_features_bits = xcr0 & I386_XSTATE_ALL_MASK;
1126 xcr0_features_bits = 0;
1131 switch (xcr0_features_bits)
1133 case I386_XSTATE_MPX_AVX512_MASK:
1134 case I386_XSTATE_AVX512_MASK:
1136 return tdesc_x32_avx512_linux;
1138 return tdesc_amd64_avx512_linux;
1139 case I386_XSTATE_MPX_MASK:
1141 return tdesc_x32_avx_linux; /* No MPX on x32 using AVX. */
1143 return tdesc_amd64_mpx_linux;
1144 case I386_XSTATE_AVX_MASK:
1146 return tdesc_x32_avx_linux;
1148 return tdesc_amd64_avx_linux;
1151 return tdesc_x32_linux;
1153 return tdesc_amd64_linux;
1159 switch (xcr0_features_bits)
1161 case I386_XSTATE_MPX_AVX512_MASK:
1162 case I386_XSTATE_AVX512_MASK:
1163 return tdesc_i386_avx512_linux;
1164 case I386_XSTATE_MPX_MASK:
1165 return tdesc_i386_mpx_linux;
1166 case I386_XSTATE_AVX_MASK:
1167 return tdesc_i386_avx_linux;
1169 return tdesc_i386_linux;
1173 gdb_assert_not_reached ("failed to return tdesc");
1176 /* Enable branch tracing. */
1178 static struct btrace_target_info *
1179 x86_linux_enable_btrace (struct target_ops *self, ptid_t ptid)
1181 struct btrace_target_info *tinfo;
1182 struct gdbarch *gdbarch;
1185 tinfo = linux_enable_btrace (ptid);
1188 error (_("Could not enable branch tracing for %s: %s."),
1189 target_pid_to_str (ptid), safe_strerror (errno));
1191 /* Fill in the size of a pointer in bits. */
1192 gdbarch = target_thread_architecture (ptid);
1193 tinfo->ptr_bits = gdbarch_ptr_bit (gdbarch);
1198 /* Disable branch tracing. */
1201 x86_linux_disable_btrace (struct target_ops *self,
1202 struct btrace_target_info *tinfo)
1204 enum btrace_error errcode = linux_disable_btrace (tinfo);
1206 if (errcode != BTRACE_ERR_NONE)
1207 error (_("Could not disable branch tracing."));
1210 /* Teardown branch tracing. */
1213 x86_linux_teardown_btrace (struct target_ops *self,
1214 struct btrace_target_info *tinfo)
1216 /* Ignore errors. */
1217 linux_disable_btrace (tinfo);
1220 static enum btrace_error
1221 x86_linux_read_btrace (struct target_ops *self,
1222 VEC (btrace_block_s) **data,
1223 struct btrace_target_info *btinfo,
1224 enum btrace_read_type type)
1226 return linux_read_btrace (data, btinfo, type);
1229 /* Create an x86 GNU/Linux target. */
1231 static struct target_ops *
1232 x86_linux_create_target (void)
1234 /* Fill in the generic GNU/Linux methods. */
1235 struct target_ops *t = linux_target ();
1237 /* Initialize the debug register function vectors. */
1238 i386_use_watchpoints (t);
1239 i386_dr_low.set_control = x86_linux_dr_set_control;
1240 i386_dr_low.set_addr = x86_linux_dr_set_addr;
1241 i386_dr_low.get_addr = x86_linux_dr_get_addr;
1242 i386_dr_low.get_status = x86_linux_dr_get_status;
1243 i386_dr_low.get_control = x86_linux_dr_get_control;
1244 i386_set_debug_register_length (sizeof (void *));
1246 /* Override the GNU/Linux inferior startup hook. */
1247 super_post_startup_inferior = t->to_post_startup_inferior;
1248 t->to_post_startup_inferior = x86_linux_child_post_startup_inferior;
1250 /* Add the description reader. */
1251 t->to_read_description = x86_linux_read_description;
1253 /* Add btrace methods. */
1254 t->to_supports_btrace = linux_supports_btrace;
1255 t->to_enable_btrace = x86_linux_enable_btrace;
1256 t->to_disable_btrace = x86_linux_disable_btrace;
1257 t->to_teardown_btrace = x86_linux_teardown_btrace;
1258 t->to_read_btrace = x86_linux_read_btrace;
1263 /* Add an x86 GNU/Linux target. */
1266 x86_linux_add_target (struct target_ops *t)
1268 linux_nat_add_target (t);
1269 linux_nat_set_new_thread (t, x86_linux_new_thread);
1270 linux_nat_set_new_fork (t, x86_linux_new_fork);
1271 linux_nat_set_forget_process (t, i386_forget_process);
1272 linux_nat_set_prepare_to_resume (t, x86_linux_prepare_to_resume);
1275 /* -Wmissing-prototypes */
1276 extern initialize_file_ftype _initialize_i386_linux_nat;
1279 _initialize_i386_linux_nat (void)
1281 /* Create a generic x86 GNU/Linux target. */
1282 struct target_ops *t = x86_linux_create_target ();
1284 /* Override the default ptrace resume method. */
1285 t->to_resume = i386_linux_resume;
1287 /* Add our register access methods. */
1288 t->to_fetch_registers = i386_linux_fetch_inferior_registers;
1289 t->to_store_registers = i386_linux_store_inferior_registers;
1291 /* Add the target. */
1292 x86_linux_add_target (t);