1 /* Native-dependent code for GNU/Linux x86.
3 Copyright 1999, 2000, 2001, 2002 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 2 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, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
27 #include "gdb_assert.h"
28 #include "gdb_string.h"
29 #include <sys/ptrace.h>
31 #include <sys/procfs.h>
41 #ifdef HAVE_SYS_DEBUGREG_H
42 #include <sys/debugreg.h>
46 #define DR_FIRSTADDR 0
61 /* Prototypes for supply_gregset etc. */
64 /* Prototypes for i387_supply_fsave etc. */
65 #include "i387-tdep.h"
67 /* Defines for XMM0_REGNUM etc. */
68 #include "i386-tdep.h"
70 /* Defines I386_LINUX_ORIG_EAX_REGNUM. */
71 #include "i386-linux-tdep.h"
73 /* Prototypes for local functions. */
74 static void dummy_sse_values (void);
78 /* The register sets used in GNU/Linux ELF core-dumps are identical to
79 the register sets in `struct user' that is used for a.out
80 core-dumps, and is also used by `ptrace'. The corresponding types
81 are `elf_gregset_t' for the general-purpose registers (with
82 `elf_greg_t' the type of a single GP register) and `elf_fpregset_t'
83 for the floating-point registers.
85 Those types used to be available under the names `gregset_t' and
86 `fpregset_t' too, and this file used those names in the past. But
87 those names are now used for the register sets used in the
88 `mcontext_t' type, and have a different size and layout. */
90 /* Mapping between the general-purpose registers in `struct user'
91 format and GDB's register array layout. */
98 -1, -1, -1, -1, /* st0, st1, st2, st3 */
99 -1, -1, -1, -1, /* st4, st5, st6, st7 */
100 -1, -1, -1, -1, /* fctrl, fstat, ftag, fiseg */
101 -1, -1, -1, -1, /* fioff, foseg, fooff, fop */
102 -1, -1, -1, -1, /* xmm0, xmm1, xmm2, xmm3 */
103 -1, -1, -1, -1, /* xmm4, xmm5, xmm6, xmm6 */
108 /* Which ptrace request retrieves which registers?
109 These apply to the corresponding SET requests as well. */
110 #define GETREGS_SUPPLIES(regno) \
111 ((0 <= (regno) && (regno) <= 15) || (regno) == I386_LINUX_ORIG_EAX_REGNUM)
112 #define GETFPREGS_SUPPLIES(regno) \
113 (FP0_REGNUM <= (regno) && (regno) <= LAST_FPU_CTRL_REGNUM)
114 #define GETFPXREGS_SUPPLIES(regno) \
115 (FP0_REGNUM <= (regno) && (regno) <= MXCSR_REGNUM)
117 /* Does the current host support the GETREGS request? */
118 int have_ptrace_getregs =
119 #ifdef HAVE_PTRACE_GETREGS
126 /* Does the current host support the GETFPXREGS request? The header
127 file may or may not define it, and even if it is defined, the
128 kernel will return EIO if it's running on a pre-SSE processor.
130 My instinct is to attach this to some architecture- or
131 target-specific data structure, but really, a particular GDB
132 process can only run on top of one kernel at a time. So it's okay
133 for this to be a simple variable. */
134 int have_ptrace_getfpxregs =
135 #ifdef HAVE_PTRACE_GETFPXREGS
143 /* Support for the user struct. */
145 /* Return the address of register REGNUM. BLOCKEND is the value of
146 u.u_ar0, which should point to the registers. */
149 register_u_addr (CORE_ADDR blockend, int regnum)
151 return (blockend + 4 * regmap[regnum]);
154 /* Return the size of the user struct. */
159 return (sizeof (struct user));
163 /* Accessing registers through the U area, one at a time. */
165 /* Fetch one register. */
168 fetch_register (int regno)
173 gdb_assert (!have_ptrace_getregs);
174 if (cannot_fetch_register (regno))
176 supply_register (regno, NULL);
180 /* GNU/Linux LWP ID's are process ID's. */
181 if ((tid = TIDGET (inferior_ptid)) == 0)
182 tid = PIDGET (inferior_ptid); /* Not a threaded program. */
185 val = ptrace (PTRACE_PEEKUSER, tid, register_addr (regno, 0), 0);
187 error ("Couldn't read register %s (#%d): %s.", REGISTER_NAME (regno),
188 regno, safe_strerror (errno));
190 supply_register (regno, &val);
193 /* Store one register. */
196 store_register (int regno)
201 gdb_assert (!have_ptrace_getregs);
202 if (cannot_store_register (regno))
205 /* GNU/Linux LWP ID's are process ID's. */
206 if ((tid = TIDGET (inferior_ptid)) == 0)
207 tid = PIDGET (inferior_ptid); /* Not a threaded program. */
210 regcache_collect (regno, &val);
211 ptrace (PTRACE_POKEUSER, tid, register_addr (regno, 0), val);
213 error ("Couldn't read register %s (#%d): %s.", REGISTER_NAME (regno),
214 regno, safe_strerror (errno));
218 /* Transfering the general-purpose registers between GDB, inferiors
221 /* Fill GDB's register array with the general-purpose register values
225 supply_gregset (elf_gregset_t *gregsetp)
227 elf_greg_t *regp = (elf_greg_t *) gregsetp;
230 for (i = 0; i < I386_NUM_GREGS; i++)
231 supply_register (i, (char *) (regp + regmap[i]));
233 if (I386_LINUX_ORIG_EAX_REGNUM < NUM_REGS)
234 supply_register (I386_LINUX_ORIG_EAX_REGNUM, (char *) (regp + ORIG_EAX));
237 /* Fill register REGNO (if it is a general-purpose register) in
238 *GREGSETPS with the value in GDB's register array. If REGNO is -1,
239 do this for all registers. */
242 fill_gregset (elf_gregset_t *gregsetp, int regno)
244 elf_greg_t *regp = (elf_greg_t *) gregsetp;
247 for (i = 0; i < I386_NUM_GREGS; i++)
248 if (regno == -1 || regno == i)
249 regcache_collect (i, regp + regmap[i]);
251 if ((regno == -1 || regno == I386_LINUX_ORIG_EAX_REGNUM)
252 && I386_LINUX_ORIG_EAX_REGNUM < NUM_REGS)
253 regcache_collect (I386_LINUX_ORIG_EAX_REGNUM, regp + ORIG_EAX);
256 #ifdef HAVE_PTRACE_GETREGS
258 /* Fetch all general-purpose registers from process/thread TID and
259 store their values in GDB's register array. */
266 if (ptrace (PTRACE_GETREGS, tid, 0, (int) ®s) < 0)
270 /* The kernel we're running on doesn't support the GETREGS
271 request. Reset `have_ptrace_getregs'. */
272 have_ptrace_getregs = 0;
276 perror_with_name ("Couldn't get registers");
279 supply_gregset (®s);
282 /* Store all valid general-purpose registers in GDB's register array
283 into the process/thread specified by TID. */
286 store_regs (int tid, int regno)
290 if (ptrace (PTRACE_GETREGS, tid, 0, (int) ®s) < 0)
291 perror_with_name ("Couldn't get registers");
293 fill_gregset (®s, regno);
295 if (ptrace (PTRACE_SETREGS, tid, 0, (int) ®s) < 0)
296 perror_with_name ("Couldn't write registers");
301 static void fetch_regs (int tid) {}
302 static void store_regs (int tid, int regno) {}
307 /* Transfering floating-point registers between GDB, inferiors and cores. */
309 /* Fill GDB's register array with the floating-point register values in
313 supply_fpregset (elf_fpregset_t *fpregsetp)
315 i387_supply_fsave ((char *) fpregsetp);
319 /* Fill register REGNO (if it is a floating-point register) in
320 *FPREGSETP with the value in GDB's register array. If REGNO is -1,
321 do this for all registers. */
324 fill_fpregset (elf_fpregset_t *fpregsetp, int regno)
326 i387_fill_fsave ((char *) fpregsetp, regno);
329 #ifdef HAVE_PTRACE_GETREGS
331 /* Fetch all floating-point registers from process/thread TID and store
332 thier values in GDB's register array. */
335 fetch_fpregs (int tid)
337 elf_fpregset_t fpregs;
339 if (ptrace (PTRACE_GETFPREGS, tid, 0, (int) &fpregs) < 0)
340 perror_with_name ("Couldn't get floating point status");
342 supply_fpregset (&fpregs);
345 /* Store all valid floating-point registers in GDB's register array
346 into the process/thread specified by TID. */
349 store_fpregs (int tid, int regno)
351 elf_fpregset_t fpregs;
353 if (ptrace (PTRACE_GETFPREGS, tid, 0, (int) &fpregs) < 0)
354 perror_with_name ("Couldn't get floating point status");
356 fill_fpregset (&fpregs, regno);
358 if (ptrace (PTRACE_SETFPREGS, tid, 0, (int) &fpregs) < 0)
359 perror_with_name ("Couldn't write floating point status");
364 static void fetch_fpregs (int tid) {}
365 static void store_fpregs (int tid, int regno) {}
370 /* Transfering floating-point and SSE registers to and from GDB. */
372 #ifdef HAVE_PTRACE_GETFPXREGS
374 /* Fill GDB's register array with the floating-point and SSE register
375 values in *FPXREGSETP. */
378 supply_fpxregset (elf_fpxregset_t *fpxregsetp)
380 i387_supply_fxsave ((char *) fpxregsetp);
383 /* Fill register REGNO (if it is a floating-point or SSE register) in
384 *FPXREGSETP with the value in GDB's register array. If REGNO is
385 -1, do this for all registers. */
388 fill_fpxregset (elf_fpxregset_t *fpxregsetp, int regno)
390 i387_fill_fxsave ((char *) fpxregsetp, regno);
393 /* Fetch all registers covered by the PTRACE_GETFPXREGS request from
394 process/thread TID and store their values in GDB's register array.
395 Return non-zero if successful, zero otherwise. */
398 fetch_fpxregs (int tid)
400 elf_fpxregset_t fpxregs;
402 if (! have_ptrace_getfpxregs)
405 if (ptrace (PTRACE_GETFPXREGS, tid, 0, (int) &fpxregs) < 0)
409 have_ptrace_getfpxregs = 0;
413 perror_with_name ("Couldn't read floating-point and SSE registers");
416 supply_fpxregset (&fpxregs);
420 /* Store all valid registers in GDB's register array covered by the
421 PTRACE_SETFPXREGS request into the process/thread specified by TID.
422 Return non-zero if successful, zero otherwise. */
425 store_fpxregs (int tid, int regno)
427 elf_fpxregset_t fpxregs;
429 if (! have_ptrace_getfpxregs)
432 if (ptrace (PTRACE_GETFPXREGS, tid, 0, &fpxregs) == -1)
436 have_ptrace_getfpxregs = 0;
440 perror_with_name ("Couldn't read floating-point and SSE registers");
443 fill_fpxregset (&fpxregs, regno);
445 if (ptrace (PTRACE_SETFPXREGS, tid, 0, &fpxregs) == -1)
446 perror_with_name ("Couldn't write floating-point and SSE registers");
451 /* Fill the XMM registers in the register array with dummy values. For
452 cases where we don't have access to the XMM registers. I think
453 this is cleaner than printing a warning. For a cleaner solution,
454 we should gdbarchify the i386 family. */
457 dummy_sse_values (void)
459 struct gdbarch_tdep *tdep = gdbarch_tdep (current_gdbarch);
460 /* C doesn't have a syntax for NaN's, so write it out as an array of
462 static long dummy[4] = { 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff };
463 static long mxcsr = 0x1f80;
466 for (reg = 0; reg < tdep->num_xmm_regs; reg++)
467 supply_register (XMM0_REGNUM + reg, (char *) dummy);
468 if (tdep->num_xmm_regs > 0)
469 supply_register (MXCSR_REGNUM, (char *) &mxcsr);
474 static int fetch_fpxregs (int tid) { return 0; }
475 static int store_fpxregs (int tid, int regno) { return 0; }
476 static void dummy_sse_values (void) {}
478 #endif /* HAVE_PTRACE_GETFPXREGS */
481 /* Transferring arbitrary registers between GDB and inferior. */
483 /* Check if register REGNO in the child process is accessible.
484 If we are accessing registers directly via the U area, only the
485 general-purpose registers are available.
486 All registers should be accessible if we have GETREGS support. */
489 cannot_fetch_register (int regno)
491 gdb_assert (regno >= 0 && regno < NUM_REGS);
492 return (!have_ptrace_getregs && regmap[regno] == -1);
496 cannot_store_register (int regno)
498 gdb_assert (regno >= 0 && regno < NUM_REGS);
499 return (!have_ptrace_getregs && regmap[regno] == -1);
502 /* Fetch register REGNO from the child process. If REGNO is -1, do
503 this for all registers (including the floating point and SSE
507 fetch_inferior_registers (int regno)
511 /* Use the old method of peeking around in `struct user' if the
512 GETREGS request isn't available. */
513 if (!have_ptrace_getregs)
517 for (i = 0; i < NUM_REGS; i++)
518 if (regno == -1 || regno == i)
524 /* GNU/Linux LWP ID's are process ID's. */
525 if ((tid = TIDGET (inferior_ptid)) == 0)
526 tid = PIDGET (inferior_ptid); /* Not a threaded program. */
528 /* Use the PTRACE_GETFPXREGS request whenever possible, since it
529 transfers more registers in one system call, and we'll cache the
530 results. But remember that fetch_fpxregs can fail, and return
536 /* The call above might reset `have_ptrace_getregs'. */
537 if (!have_ptrace_getregs)
539 fetch_inferior_registers (regno);
543 if (fetch_fpxregs (tid))
549 if (GETREGS_SUPPLIES (regno))
555 if (GETFPXREGS_SUPPLIES (regno))
557 if (fetch_fpxregs (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
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 store_inferior_registers (int regno)
582 /* Use the old method of poking around in `struct user' if the
583 SETREGS request isn't available. */
584 if (!have_ptrace_getregs)
588 for (i = 0; i < NUM_REGS; i++)
589 if (regno == -1 || regno == i)
595 /* GNU/Linux LWP ID's are process ID's. */
596 if ((tid = TIDGET (inferior_ptid)) == 0)
597 tid = PIDGET (inferior_ptid); /* Not a threaded program. */
599 /* Use the PTRACE_SETFPXREGS requests whenever possible, since it
600 transfers more registers in one system call. But remember that
601 store_fpxregs can fail, and return zero. */
604 store_regs (tid, regno);
605 if (store_fpxregs (tid, regno))
607 store_fpregs (tid, regno);
611 if (GETREGS_SUPPLIES (regno))
613 store_regs (tid, regno);
617 if (GETFPXREGS_SUPPLIES (regno))
619 if (store_fpxregs (tid, regno))
622 /* Either our processor or our kernel doesn't support the SSE
623 registers, so just write the FP registers in the traditional
625 store_fpregs (tid, regno);
629 internal_error (__FILE__, __LINE__,
630 "Got request to store bad register number %d.", regno);
635 i386_linux_dr_get (int regnum)
640 /* FIXME: kettenis/2001-01-29: It's not clear what we should do with
641 multi-threaded processes here. For now, pretend there is just
643 tid = PIDGET (inferior_ptid);
645 /* FIXME: kettenis/2001-03-27: Calling perror_with_name if the
646 ptrace call fails breaks debugging remote targets. The correct
647 way to fix this is to add the hardware breakpoint and watchpoint
648 stuff to the target vectore. For now, just return zero if the
649 ptrace call fails. */
651 value = ptrace (PTRACE_PEEKUSER, tid,
652 offsetof (struct user, u_debugreg[regnum]), 0);
655 perror_with_name ("Couldn't read debug register");
664 i386_linux_dr_set (int regnum, unsigned long value)
668 /* FIXME: kettenis/2001-01-29: It's not clear what we should do with
669 multi-threaded processes here. For now, pretend there is just
671 tid = PIDGET (inferior_ptid);
674 ptrace (PTRACE_POKEUSER, tid,
675 offsetof (struct user, u_debugreg[regnum]), value);
677 perror_with_name ("Couldn't write debug register");
681 i386_linux_dr_set_control (unsigned long control)
683 i386_linux_dr_set (DR_CONTROL, control);
687 i386_linux_dr_set_addr (int regnum, CORE_ADDR addr)
689 gdb_assert (regnum >= 0 && regnum <= DR_LASTADDR - DR_FIRSTADDR);
691 i386_linux_dr_set (DR_FIRSTADDR + regnum, addr);
695 i386_linux_dr_reset_addr (int regnum)
697 gdb_assert (regnum >= 0 && regnum <= DR_LASTADDR - DR_FIRSTADDR);
699 i386_linux_dr_set (DR_FIRSTADDR + regnum, 0L);
703 i386_linux_dr_get_status (void)
705 return i386_linux_dr_get (DR_STATUS);
709 /* Interpreting register set info found in core files. */
711 /* Provide registers to GDB from a core file.
713 (We can't use the generic version of this function in
714 core-regset.c, because GNU/Linux has *three* different kinds of
715 register set notes. core-regset.c would have to call
716 supply_fpxregset, which most platforms don't have.)
718 CORE_REG_SECT points to an array of bytes, which are the contents
719 of a `note' from a core file which BFD thinks might contain
720 register contents. CORE_REG_SIZE is its size.
722 WHICH says which register set corelow suspects this is:
723 0 --- the general-purpose register set, in elf_gregset_t format
724 2 --- the floating-point register set, in elf_fpregset_t format
725 3 --- the extended floating-point register set, in elf_fpxregset_t format
727 REG_ADDR isn't used on GNU/Linux. */
730 fetch_core_registers (char *core_reg_sect, unsigned core_reg_size,
731 int which, CORE_ADDR reg_addr)
733 elf_gregset_t gregset;
734 elf_fpregset_t fpregset;
739 if (core_reg_size != sizeof (gregset))
740 warning ("Wrong size gregset in core file.");
743 memcpy (&gregset, core_reg_sect, sizeof (gregset));
744 supply_gregset (&gregset);
749 if (core_reg_size != sizeof (fpregset))
750 warning ("Wrong size fpregset in core file.");
753 memcpy (&fpregset, core_reg_sect, sizeof (fpregset));
754 supply_fpregset (&fpregset);
758 #ifdef HAVE_PTRACE_GETFPXREGS
760 elf_fpxregset_t fpxregset;
763 if (core_reg_size != sizeof (fpxregset))
764 warning ("Wrong size fpxregset in core file.");
767 memcpy (&fpxregset, core_reg_sect, sizeof (fpxregset));
768 supply_fpxregset (&fpxregset);
775 /* We've covered all the kinds of registers we know about here,
776 so this must be something we wouldn't know what to do with
777 anyway. Just ignore it. */
783 /* The instruction for a GNU/Linux system call is:
787 static const unsigned char linux_syscall[] = { 0xcd, 0x80 };
789 #define LINUX_SYSCALL_LEN (sizeof linux_syscall)
791 /* The system call number is stored in the %eax register. */
792 #define LINUX_SYSCALL_REGNUM 0 /* %eax */
794 /* We are specifically interested in the sigreturn and rt_sigreturn
797 #ifndef SYS_sigreturn
798 #define SYS_sigreturn 0x77
800 #ifndef SYS_rt_sigreturn
801 #define SYS_rt_sigreturn 0xad
804 /* Offset to saved processor flags, from <asm/sigcontext.h>. */
805 #define LINUX_SIGCONTEXT_EFLAGS_OFFSET (64)
807 /* Resume execution of the inferior process.
808 If STEP is nonzero, single-step it.
809 If SIGNAL is nonzero, give it that signal. */
812 child_resume (ptid_t ptid, int step, enum target_signal signal)
814 int pid = PIDGET (ptid);
816 int request = PTRACE_CONT;
819 /* Resume all threads. */
820 /* I think this only gets used in the non-threaded case, where "resume
821 all threads" and "resume inferior_ptid" are the same. */
822 pid = PIDGET (inferior_ptid);
826 CORE_ADDR pc = read_pc_pid (pid_to_ptid (pid));
827 unsigned char buf[LINUX_SYSCALL_LEN];
829 request = PTRACE_SINGLESTEP;
831 /* Returning from a signal trampoline is done by calling a
832 special system call (sigreturn or rt_sigreturn, see
833 i386-linux-tdep.c for more information). This system call
834 restores the registers that were saved when the signal was
835 raised, including %eflags. That means that single-stepping
836 won't work. Instead, we'll have to modify the signal context
837 that's about to be restored, and set the trace flag there. */
839 /* First check if PC is at a system call. */
840 if (read_memory_nobpt (pc, (char *) buf, LINUX_SYSCALL_LEN) == 0
841 && memcmp (buf, linux_syscall, LINUX_SYSCALL_LEN) == 0)
843 int syscall = read_register_pid (LINUX_SYSCALL_REGNUM,
846 /* Then check the system call number. */
847 if (syscall == SYS_sigreturn || syscall == SYS_rt_sigreturn)
849 CORE_ADDR sp = read_register (SP_REGNUM);
851 unsigned long int eflags;
853 if (syscall == SYS_rt_sigreturn)
854 addr = read_memory_integer (sp + 8, 4) + 20;
856 /* Set the trace flag in the context that's about to be
858 addr += LINUX_SIGCONTEXT_EFLAGS_OFFSET;
859 read_memory (addr, (char *) &eflags, 4);
861 write_memory (addr, (char *) &eflags, 4);
866 if (ptrace (request, pid, 0, target_signal_to_host (signal)) == -1)
867 perror_with_name ("ptrace");
871 /* Register that we are able to handle GNU/Linux ELF core file
874 static struct core_fns linux_elf_core_fns =
876 bfd_target_elf_flavour, /* core_flavour */
877 default_check_format, /* check_format */
878 default_core_sniffer, /* core_sniffer */
879 fetch_core_registers, /* core_read_registers */
884 _initialize_i386_linux_nat (void)
886 add_core_fns (&linux_elf_core_fns);