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);
77 /* The register sets used in GNU/Linux ELF core-dumps are identical to
78 the register sets in `struct user' that is used for a.out
79 core-dumps, and is also used by `ptrace'. The corresponding types
80 are `elf_gregset_t' for the general-purpose registers (with
81 `elf_greg_t' the type of a single GP register) and `elf_fpregset_t'
82 for the floating-point registers.
84 Those types used to be available under the names `gregset_t' and
85 `fpregset_t' too, and this file used those names in the past. But
86 those names are now used for the register sets used in the
87 `mcontext_t' type, and have a different size and layout. */
89 /* Mapping between the general-purpose registers in `struct user'
90 format and GDB's register array layout. */
97 -1, -1, -1, -1, /* st0, st1, st2, st3 */
98 -1, -1, -1, -1, /* st4, st5, st6, st7 */
99 -1, -1, -1, -1, /* fctrl, fstat, ftag, fiseg */
100 -1, -1, -1, -1, /* fioff, foseg, fooff, fop */
101 -1, -1, -1, -1, /* xmm0, xmm1, xmm2, xmm3 */
102 -1, -1, -1, -1, /* xmm4, xmm5, xmm6, xmm6 */
107 /* Which ptrace request retrieves which registers?
108 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)
113 #define GETFPREGS_SUPPLIES(regno) \
114 (FP0_REGNUM <= (regno) && (regno) <= LAST_FPU_CTRL_REGNUM)
116 #define GETFPXREGS_SUPPLIES(regno) \
117 (FP0_REGNUM <= (regno) && (regno) <= MXCSR_REGNUM)
119 /* Does the current host support the GETREGS request? */
120 int have_ptrace_getregs =
121 #ifdef HAVE_PTRACE_GETREGS
128 /* Does the current host support the GETFPXREGS request? The header
129 file may or may not define it, and even if it is defined, the
130 kernel will return EIO if it's running on a pre-SSE processor.
132 My instinct is to attach this to some architecture- or
133 target-specific data structure, but really, a particular GDB
134 process can only run on top of one kernel at a time. So it's okay
135 for this to be a simple variable. */
136 int have_ptrace_getfpxregs =
137 #ifdef HAVE_PTRACE_GETFPXREGS
145 /* Support for the user struct. */
147 /* Return the address of register REGNUM. BLOCKEND is the value of
148 u.u_ar0, which should point to the registers. */
151 register_u_addr (CORE_ADDR blockend, int regnum)
153 return (blockend + 4 * regmap[regnum]);
156 /* Return the size of the user struct. */
161 return (sizeof (struct user));
165 /* Accessing registers through the U area, one at a time. */
167 /* Fetch one register. */
170 fetch_register (int regno)
175 gdb_assert (!have_ptrace_getregs);
176 if (cannot_fetch_register (regno))
178 supply_register (regno, NULL);
182 /* GNU/Linux LWP ID's are process ID's. */
183 tid = TIDGET (inferior_ptid);
185 tid = PIDGET (inferior_ptid); /* Not a threaded program. */
188 val = ptrace (PTRACE_PEEKUSER, tid, register_addr (regno, 0), 0);
190 error ("Couldn't read register %s (#%d): %s.", REGISTER_NAME (regno),
191 regno, safe_strerror (errno));
193 supply_register (regno, &val);
196 /* Store one register. */
199 store_register (int regno)
204 gdb_assert (!have_ptrace_getregs);
205 if (cannot_store_register (regno))
208 /* GNU/Linux LWP ID's are process ID's. */
209 tid = TIDGET (inferior_ptid);
211 tid = PIDGET (inferior_ptid); /* Not a threaded program. */
214 regcache_collect (regno, &val);
215 ptrace (PTRACE_POKEUSER, tid, register_addr (regno, 0), val);
217 error ("Couldn't write register %s (#%d): %s.", REGISTER_NAME (regno),
218 regno, safe_strerror (errno));
222 /* Transfering the general-purpose registers between GDB, inferiors
225 /* Fill GDB's register array with the general-purpose register values
229 supply_gregset (elf_gregset_t *gregsetp)
231 elf_greg_t *regp = (elf_greg_t *) gregsetp;
234 for (i = 0; i < I386_NUM_GREGS; i++)
235 supply_register (i, regp + regmap[i]);
237 if (I386_LINUX_ORIG_EAX_REGNUM < NUM_REGS)
238 supply_register (I386_LINUX_ORIG_EAX_REGNUM, regp + ORIG_EAX);
241 /* Fill register REGNO (if it is a general-purpose register) in
242 *GREGSETPS with the value in GDB's register array. If REGNO is -1,
243 do this for all registers. */
246 fill_gregset (elf_gregset_t *gregsetp, int regno)
248 elf_greg_t *regp = (elf_greg_t *) gregsetp;
251 for (i = 0; i < I386_NUM_GREGS; i++)
252 if (regno == -1 || regno == i)
253 regcache_collect (i, regp + regmap[i]);
255 if ((regno == -1 || regno == I386_LINUX_ORIG_EAX_REGNUM)
256 && I386_LINUX_ORIG_EAX_REGNUM < NUM_REGS)
257 regcache_collect (I386_LINUX_ORIG_EAX_REGNUM, regp + ORIG_EAX);
260 #ifdef HAVE_PTRACE_GETREGS
262 /* Fetch all general-purpose registers from process/thread TID and
263 store their values in GDB's register array. */
270 if (ptrace (PTRACE_GETREGS, tid, 0, (int) ®s) < 0)
274 /* The kernel we're running on doesn't support the GETREGS
275 request. Reset `have_ptrace_getregs'. */
276 have_ptrace_getregs = 0;
280 perror_with_name ("Couldn't get registers");
283 supply_gregset (®s);
286 /* Store all valid general-purpose registers in GDB's register array
287 into the process/thread specified by TID. */
290 store_regs (int tid, int regno)
294 if (ptrace (PTRACE_GETREGS, tid, 0, (int) ®s) < 0)
295 perror_with_name ("Couldn't get registers");
297 fill_gregset (®s, regno);
299 if (ptrace (PTRACE_SETREGS, tid, 0, (int) ®s) < 0)
300 perror_with_name ("Couldn't write registers");
305 static void fetch_regs (int tid) {}
306 static void store_regs (int tid, int regno) {}
311 /* Transfering floating-point registers between GDB, inferiors and cores. */
313 /* Fill GDB's register array with the floating-point register values in
317 supply_fpregset (elf_fpregset_t *fpregsetp)
319 i387_supply_fsave ((char *) fpregsetp);
323 /* Fill register REGNO (if it is a floating-point register) in
324 *FPREGSETP with the value in GDB's register array. If REGNO is -1,
325 do this for all registers. */
328 fill_fpregset (elf_fpregset_t *fpregsetp, int regno)
330 i387_fill_fsave ((char *) fpregsetp, regno);
333 #ifdef HAVE_PTRACE_GETREGS
335 /* Fetch all floating-point registers from process/thread TID and store
336 thier values in GDB's register array. */
339 fetch_fpregs (int tid)
341 elf_fpregset_t fpregs;
343 if (ptrace (PTRACE_GETFPREGS, tid, 0, (int) &fpregs) < 0)
344 perror_with_name ("Couldn't get floating point status");
346 supply_fpregset (&fpregs);
349 /* Store all valid floating-point registers in GDB's register array
350 into the process/thread specified by TID. */
353 store_fpregs (int tid, int regno)
355 elf_fpregset_t fpregs;
357 if (ptrace (PTRACE_GETFPREGS, tid, 0, (int) &fpregs) < 0)
358 perror_with_name ("Couldn't get floating point status");
360 fill_fpregset (&fpregs, regno);
362 if (ptrace (PTRACE_SETFPREGS, tid, 0, (int) &fpregs) < 0)
363 perror_with_name ("Couldn't write floating point status");
368 static void fetch_fpregs (int tid) {}
369 static void store_fpregs (int tid, int regno) {}
374 /* Transfering floating-point and SSE registers to and from GDB. */
376 #ifdef HAVE_PTRACE_GETFPXREGS
378 /* Fill GDB's register array with the floating-point and SSE register
379 values in *FPXREGSETP. */
382 supply_fpxregset (elf_fpxregset_t *fpxregsetp)
384 i387_supply_fxsave ((char *) fpxregsetp);
387 /* Fill register REGNO (if it is a floating-point or SSE register) in
388 *FPXREGSETP with the value in GDB's register array. If REGNO is
389 -1, do this for all registers. */
392 fill_fpxregset (elf_fpxregset_t *fpxregsetp, int regno)
394 i387_fill_fxsave ((char *) fpxregsetp, regno);
397 /* Fetch all registers covered by the PTRACE_GETFPXREGS request from
398 process/thread TID and store their values in GDB's register array.
399 Return non-zero if successful, zero otherwise. */
402 fetch_fpxregs (int tid)
404 elf_fpxregset_t fpxregs;
406 if (! have_ptrace_getfpxregs)
409 if (ptrace (PTRACE_GETFPXREGS, tid, 0, (int) &fpxregs) < 0)
413 have_ptrace_getfpxregs = 0;
417 perror_with_name ("Couldn't read floating-point and SSE registers");
420 supply_fpxregset (&fpxregs);
424 /* Store all valid registers in GDB's register array covered by the
425 PTRACE_SETFPXREGS request into the process/thread specified by TID.
426 Return non-zero if successful, zero otherwise. */
429 store_fpxregs (int tid, int regno)
431 elf_fpxregset_t fpxregs;
433 if (! have_ptrace_getfpxregs)
436 if (ptrace (PTRACE_GETFPXREGS, tid, 0, &fpxregs) == -1)
440 have_ptrace_getfpxregs = 0;
444 perror_with_name ("Couldn't read floating-point and SSE registers");
447 fill_fpxregset (&fpxregs, regno);
449 if (ptrace (PTRACE_SETFPXREGS, tid, 0, &fpxregs) == -1)
450 perror_with_name ("Couldn't write floating-point and SSE registers");
455 /* Fill the XMM registers in the register array with dummy values. For
456 cases where we don't have access to the XMM registers. I think
457 this is cleaner than printing a warning. For a cleaner solution,
458 we should gdbarchify the i386 family. */
461 dummy_sse_values (void)
463 struct gdbarch_tdep *tdep = gdbarch_tdep (current_gdbarch);
464 /* C doesn't have a syntax for NaN's, so write it out as an array of
466 static long dummy[4] = { 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff };
467 static long mxcsr = 0x1f80;
470 for (reg = 0; reg < tdep->num_xmm_regs; reg++)
471 supply_register (XMM0_REGNUM + reg, (char *) dummy);
472 if (tdep->num_xmm_regs > 0)
473 supply_register (MXCSR_REGNUM, (char *) &mxcsr);
478 static int fetch_fpxregs (int tid) { return 0; }
479 static int store_fpxregs (int tid, int regno) { return 0; }
480 static void dummy_sse_values (void) {}
482 #endif /* HAVE_PTRACE_GETFPXREGS */
485 /* Transferring arbitrary registers between GDB and inferior. */
487 /* Check if register REGNO in the child process is accessible.
488 If we are accessing registers directly via the U area, only the
489 general-purpose registers are available.
490 All registers should be accessible if we have GETREGS support. */
493 cannot_fetch_register (int regno)
495 gdb_assert (regno >= 0 && regno < NUM_REGS);
496 return (!have_ptrace_getregs && regmap[regno] == -1);
500 cannot_store_register (int regno)
502 gdb_assert (regno >= 0 && regno < NUM_REGS);
503 return (!have_ptrace_getregs && regmap[regno] == -1);
506 /* Fetch register REGNO from the child process. If REGNO is -1, do
507 this for all registers (including the floating point and SSE
511 fetch_inferior_registers (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 < NUM_REGS; i++)
522 if (regno == -1 || regno == 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
541 /* The call above might reset `have_ptrace_getregs'. */
542 if (!have_ptrace_getregs)
544 fetch_inferior_registers (regno);
548 if (fetch_fpxregs (tid))
554 if (GETREGS_SUPPLIES (regno))
560 if (GETFPXREGS_SUPPLIES (regno))
562 if (fetch_fpxregs (tid))
565 /* Either our processor or our kernel doesn't support the SSE
566 registers, so read the FP registers in the traditional way,
567 and fill the SSE registers with dummy values. It would be
568 more graceful to handle differences in the register set using
569 gdbarch. Until then, this will at least make things work
575 internal_error (__FILE__, __LINE__,
576 "Got request for bad register number %d.", regno);
579 /* Store register REGNO back into the child process. If REGNO is -1,
580 do this for all registers (including the floating point and SSE
583 store_inferior_registers (int regno)
587 /* Use the old method of poking around in `struct user' if the
588 SETREGS request isn't available. */
589 if (!have_ptrace_getregs)
593 for (i = 0; i < NUM_REGS; i++)
594 if (regno == -1 || regno == i)
600 /* GNU/Linux LWP ID's are process ID's. */
601 tid = TIDGET (inferior_ptid);
603 tid = PIDGET (inferior_ptid); /* Not a threaded program. */
605 /* Use the PTRACE_SETFPXREGS requests whenever possible, since it
606 transfers more registers in one system call. But remember that
607 store_fpxregs can fail, and return zero. */
610 store_regs (tid, regno);
611 if (store_fpxregs (tid, regno))
613 store_fpregs (tid, regno);
617 if (GETREGS_SUPPLIES (regno))
619 store_regs (tid, regno);
623 if (GETFPXREGS_SUPPLIES (regno))
625 if (store_fpxregs (tid, regno))
628 /* Either our processor or our kernel doesn't support the SSE
629 registers, so just write the FP registers in the traditional
631 store_fpregs (tid, regno);
635 internal_error (__FILE__, __LINE__,
636 "Got request to store bad register number %d.", regno);
641 i386_linux_dr_get (int regnum)
646 /* FIXME: kettenis/2001-01-29: It's not clear what we should do with
647 multi-threaded processes here. For now, pretend there is just
649 tid = PIDGET (inferior_ptid);
651 /* FIXME: kettenis/2001-03-27: Calling perror_with_name if the
652 ptrace call fails breaks debugging remote targets. The correct
653 way to fix this is to add the hardware breakpoint and watchpoint
654 stuff to the target vectore. For now, just return zero if the
655 ptrace call fails. */
657 value = ptrace (PTRACE_PEEKUSER, tid,
658 offsetof (struct user, u_debugreg[regnum]), 0);
661 perror_with_name ("Couldn't read debug register");
670 i386_linux_dr_set (int regnum, unsigned long value)
674 /* FIXME: kettenis/2001-01-29: It's not clear what we should do with
675 multi-threaded processes here. For now, pretend there is just
677 tid = PIDGET (inferior_ptid);
680 ptrace (PTRACE_POKEUSER, tid,
681 offsetof (struct user, u_debugreg[regnum]), value);
683 perror_with_name ("Couldn't write debug register");
687 i386_linux_dr_set_control (unsigned long control)
689 i386_linux_dr_set (DR_CONTROL, control);
693 i386_linux_dr_set_addr (int regnum, CORE_ADDR addr)
695 gdb_assert (regnum >= 0 && regnum <= DR_LASTADDR - DR_FIRSTADDR);
697 i386_linux_dr_set (DR_FIRSTADDR + regnum, addr);
701 i386_linux_dr_reset_addr (int regnum)
703 gdb_assert (regnum >= 0 && regnum <= DR_LASTADDR - DR_FIRSTADDR);
705 i386_linux_dr_set (DR_FIRSTADDR + regnum, 0L);
709 i386_linux_dr_get_status (void)
711 return i386_linux_dr_get (DR_STATUS);
715 /* Interpreting register set info found in core files. */
717 /* Provide registers to GDB from a core file.
719 (We can't use the generic version of this function in
720 core-regset.c, because GNU/Linux has *three* different kinds of
721 register set notes. core-regset.c would have to call
722 supply_fpxregset, which most platforms don't have.)
724 CORE_REG_SECT points to an array of bytes, which are the contents
725 of a `note' from a core file which BFD thinks might contain
726 register contents. CORE_REG_SIZE is its size.
728 WHICH says which register set corelow suspects this is:
729 0 --- the general-purpose register set, in elf_gregset_t format
730 2 --- the floating-point register set, in elf_fpregset_t format
731 3 --- the extended floating-point register set, in elf_fpxregset_t format
733 REG_ADDR isn't used on GNU/Linux. */
736 fetch_core_registers (char *core_reg_sect, unsigned core_reg_size,
737 int which, CORE_ADDR reg_addr)
739 elf_gregset_t gregset;
740 elf_fpregset_t fpregset;
745 if (core_reg_size != sizeof (gregset))
746 warning ("Wrong size gregset in core file.");
749 memcpy (&gregset, core_reg_sect, sizeof (gregset));
750 supply_gregset (&gregset);
755 if (core_reg_size != sizeof (fpregset))
756 warning ("Wrong size fpregset in core file.");
759 memcpy (&fpregset, core_reg_sect, sizeof (fpregset));
760 supply_fpregset (&fpregset);
764 #ifdef HAVE_PTRACE_GETFPXREGS
766 elf_fpxregset_t fpxregset;
769 if (core_reg_size != sizeof (fpxregset))
770 warning ("Wrong size fpxregset in core file.");
773 memcpy (&fpxregset, core_reg_sect, sizeof (fpxregset));
774 supply_fpxregset (&fpxregset);
781 /* We've covered all the kinds of registers we know about here,
782 so this must be something we wouldn't know what to do with
783 anyway. Just ignore it. */
789 /* The instruction for a GNU/Linux system call is:
793 static const unsigned char linux_syscall[] = { 0xcd, 0x80 };
795 #define LINUX_SYSCALL_LEN (sizeof linux_syscall)
797 /* The system call number is stored in the %eax register. */
798 #define LINUX_SYSCALL_REGNUM 0 /* %eax */
800 /* We are specifically interested in the sigreturn and rt_sigreturn
803 #ifndef SYS_sigreturn
804 #define SYS_sigreturn 0x77
806 #ifndef SYS_rt_sigreturn
807 #define SYS_rt_sigreturn 0xad
810 /* Offset to saved processor flags, from <asm/sigcontext.h>. */
811 #define LINUX_SIGCONTEXT_EFLAGS_OFFSET (64)
813 /* Resume execution of the inferior process.
814 If STEP is nonzero, single-step it.
815 If SIGNAL is nonzero, give it that signal. */
818 child_resume (ptid_t ptid, int step, enum target_signal signal)
820 int pid = PIDGET (ptid);
822 int request = PTRACE_CONT;
825 /* Resume all threads. */
826 /* I think this only gets used in the non-threaded case, where "resume
827 all threads" and "resume inferior_ptid" are the same. */
828 pid = PIDGET (inferior_ptid);
832 CORE_ADDR pc = read_pc_pid (pid_to_ptid (pid));
833 unsigned char buf[LINUX_SYSCALL_LEN];
835 request = PTRACE_SINGLESTEP;
837 /* Returning from a signal trampoline is done by calling a
838 special system call (sigreturn or rt_sigreturn, see
839 i386-linux-tdep.c for more information). This system call
840 restores the registers that were saved when the signal was
841 raised, including %eflags. That means that single-stepping
842 won't work. Instead, we'll have to modify the signal context
843 that's about to be restored, and set the trace flag there. */
845 /* First check if PC is at a system call. */
846 if (read_memory_nobpt (pc, (char *) buf, LINUX_SYSCALL_LEN) == 0
847 && memcmp (buf, linux_syscall, LINUX_SYSCALL_LEN) == 0)
849 int syscall = read_register_pid (LINUX_SYSCALL_REGNUM,
852 /* Then check the system call number. */
853 if (syscall == SYS_sigreturn || syscall == SYS_rt_sigreturn)
855 CORE_ADDR sp = read_register (SP_REGNUM);
857 unsigned long int eflags;
859 if (syscall == SYS_rt_sigreturn)
860 addr = read_memory_integer (sp + 8, 4) + 20;
862 /* Set the trace flag in the context that's about to be
864 addr += LINUX_SIGCONTEXT_EFLAGS_OFFSET;
865 read_memory (addr, (char *) &eflags, 4);
867 write_memory (addr, (char *) &eflags, 4);
872 if (ptrace (request, pid, 0, target_signal_to_host (signal)) == -1)
873 perror_with_name ("ptrace");
877 /* Register that we are able to handle GNU/Linux ELF core file
880 static struct core_fns linux_elf_core_fns =
882 bfd_target_elf_flavour, /* core_flavour */
883 default_check_format, /* check_format */
884 default_core_sniffer, /* core_sniffer */
885 fetch_core_registers, /* core_read_registers */
890 _initialize_i386_linux_nat (void)
892 add_core_fns (&linux_elf_core_fns);