1 /* Native-dependent code for Linux/x86.
2 Copyright 1999, 2000, 2001 Free Software Foundation, Inc.
4 This file is part of GDB.
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
26 #include "gdb_assert.h"
27 #include <sys/ptrace.h>
29 #include <sys/procfs.h>
35 #ifdef HAVE_SYS_DEBUGREG_H
36 #include <sys/debugreg.h>
40 #define DR_FIRSTADDR 0
55 /* Prototypes for supply_gregset etc. */
58 /* Prototypes for i387_supply_fsave etc. */
61 /* Defines for XMM0_REGNUM etc. */
62 #include "i386-tdep.h"
64 /* Prototypes for local functions. */
65 static void dummy_sse_values (void);
69 /* The register sets used in Linux ELF core-dumps are identical to the
70 register sets in `struct user' that is used for a.out core-dumps,
71 and is also used by `ptrace'. The corresponding types are
72 `elf_gregset_t' for the general-purpose registers (with
73 `elf_greg_t' the type of a single GP register) and `elf_fpregset_t'
74 for the floating-point registers.
76 Those types used to be available under the names `gregset_t' and
77 `fpregset_t' too, and this file used those names in the past. But
78 those names are now used for the register sets used in the
79 `mcontext_t' type, and have a different size and layout. */
81 /* Mapping between the general-purpose registers in `struct user'
82 format and GDB's register array layout. */
91 /* Which ptrace request retrieves which registers?
92 These apply to the corresponding SET requests as well. */
93 #define GETREGS_SUPPLIES(regno) \
94 ((0 <= (regno) && (regno) <= 15) || (regno) == I386_LINUX_ORIG_EAX_REGNUM)
95 #define GETFPREGS_SUPPLIES(regno) \
96 (FP0_REGNUM <= (regno) && (regno) <= LAST_FPU_CTRL_REGNUM)
97 #define GETFPXREGS_SUPPLIES(regno) \
98 (FP0_REGNUM <= (regno) && (regno) <= MXCSR_REGNUM)
100 /* Does the current host support the GETREGS request? */
101 int have_ptrace_getregs =
102 #ifdef HAVE_PTRACE_GETREGS
109 /* Does the current host support the GETFPXREGS request? The header
110 file may or may not define it, and even if it is defined, the
111 kernel will return EIO if it's running on a pre-SSE processor.
113 My instinct is to attach this to some architecture- or
114 target-specific data structure, but really, a particular GDB
115 process can only run on top of one kernel at a time. So it's okay
116 for this to be a simple variable. */
117 int have_ptrace_getfpxregs =
118 #ifdef HAVE_PTRACE_GETFPXREGS
126 /* Support for the user struct. */
128 /* Return the address of register REGNUM. BLOCKEND is the value of
129 u.u_ar0, which should point to the registers. */
132 register_u_addr (CORE_ADDR blockend, int regnum)
134 return (blockend + 4 * regmap[regnum]);
137 /* Return the size of the user struct. */
142 return (sizeof (struct user));
146 /* Fetching registers directly from the U area, one at a time. */
148 /* FIXME: kettenis/2000-03-05: This duplicates code from `inptrace.c'.
149 The problem is that we define FETCH_INFERIOR_REGISTERS since we
150 want to use our own versions of {fetch,store}_inferior_registers
151 that use the GETREGS request. This means that the code in
152 `infptrace.c' is #ifdef'd out. But we need to fall back on that
153 code when GDB is running on top of a kernel that doesn't support
154 the GETREGS request. I want to avoid changing `infptrace.c' right
158 #define PT_READ_U PTRACE_PEEKUSR
161 #define PT_WRITE_U PTRACE_POKEUSR
164 /* Default the type of the ptrace transfer to int. */
165 #ifndef PTRACE_XFER_TYPE
166 #define PTRACE_XFER_TYPE int
169 /* Registers we shouldn't try to fetch. */
170 #define OLD_CANNOT_FETCH_REGISTER(regno) ((regno) >= NUM_GREGS)
172 /* Fetch one register. */
175 fetch_register (int regno)
177 /* This isn't really an address. But ptrace thinks of it as one. */
179 char mess[128]; /* For messages */
181 unsigned int offset; /* Offset of registers within the u area. */
182 char buf[MAX_REGISTER_RAW_SIZE];
185 if (OLD_CANNOT_FETCH_REGISTER (regno))
187 memset (buf, '\0', REGISTER_RAW_SIZE (regno)); /* Supply zeroes */
188 supply_register (regno, buf);
192 /* Overload thread id onto process id */
193 if ((tid = TIDGET (inferior_ptid)) == 0)
194 tid = PIDGET (inferior_ptid); /* no thread id, just use process id */
196 offset = U_REGS_OFFSET;
198 regaddr = register_addr (regno, offset);
199 for (i = 0; i < REGISTER_RAW_SIZE (regno); i += sizeof (PTRACE_XFER_TYPE))
202 *(PTRACE_XFER_TYPE *) & buf[i] = ptrace (PT_READ_U, tid,
203 (PTRACE_ARG3_TYPE) regaddr, 0);
204 regaddr += sizeof (PTRACE_XFER_TYPE);
207 sprintf (mess, "reading register %s (#%d)",
208 REGISTER_NAME (regno), regno);
209 perror_with_name (mess);
212 supply_register (regno, buf);
215 /* Fetch register values from the inferior.
216 If REGNO is negative, do this for all registers.
217 Otherwise, REGNO specifies which register (so we can save time). */
220 old_fetch_inferior_registers (int regno)
224 fetch_register (regno);
228 for (regno = 0; regno < NUM_REGS; regno++)
230 fetch_register (regno);
235 /* Registers we shouldn't try to store. */
236 #define OLD_CANNOT_STORE_REGISTER(regno) ((regno) >= NUM_GREGS)
238 /* Store one register. */
241 store_register (int regno)
243 /* This isn't really an address. But ptrace thinks of it as one. */
245 char mess[128]; /* For messages */
247 unsigned int offset; /* Offset of registers within the u area. */
250 if (OLD_CANNOT_STORE_REGISTER (regno))
255 /* Overload thread id onto process id */
256 if ((tid = TIDGET (inferior_ptid)) == 0)
257 tid = PIDGET (inferior_ptid); /* no thread id, just use process id */
259 offset = U_REGS_OFFSET;
261 regaddr = register_addr (regno, offset);
262 for (i = 0; i < REGISTER_RAW_SIZE (regno); i += sizeof (PTRACE_XFER_TYPE))
265 ptrace (PT_WRITE_U, tid, (PTRACE_ARG3_TYPE) regaddr,
266 *(PTRACE_XFER_TYPE *) & registers[REGISTER_BYTE (regno) + i]);
267 regaddr += sizeof (PTRACE_XFER_TYPE);
270 sprintf (mess, "writing register %s (#%d)",
271 REGISTER_NAME (regno), regno);
272 perror_with_name (mess);
277 /* Store our register values back into the inferior.
278 If REGNO is negative, do this for all registers.
279 Otherwise, REGNO specifies which register (so we can save time). */
282 old_store_inferior_registers (int regno)
286 store_register (regno);
290 for (regno = 0; regno < NUM_REGS; regno++)
292 store_register (regno);
298 /* Transfering the general-purpose registers between GDB, inferiors
301 /* Fill GDB's register array with the general-purpose register values
305 supply_gregset (elf_gregset_t *gregsetp)
307 elf_greg_t *regp = (elf_greg_t *) gregsetp;
310 for (i = 0; i < NUM_GREGS; i++)
311 supply_register (i, (char *) (regp + regmap[i]));
313 supply_register (I386_LINUX_ORIG_EAX_REGNUM, (char *) (regp + ORIG_EAX));
316 /* Fill register REGNO (if it is a general-purpose register) in
317 *GREGSETPS with the value in GDB's register array. If REGNO is -1,
318 do this for all registers. */
321 fill_gregset (elf_gregset_t *gregsetp, int regno)
323 elf_greg_t *regp = (elf_greg_t *) gregsetp;
326 for (i = 0; i < NUM_GREGS; i++)
327 if ((regno == -1 || regno == i))
328 regcache_collect (i, regp + regmap[i]);
330 if (regno == -1 || regno == I386_LINUX_ORIG_EAX_REGNUM)
331 regcache_collect (I386_LINUX_ORIG_EAX_REGNUM, regp + ORIG_EAX);
334 #ifdef HAVE_PTRACE_GETREGS
336 /* Fetch all general-purpose registers from process/thread TID and
337 store their values in GDB's register array. */
344 if (ptrace (PTRACE_GETREGS, tid, 0, (int) ®s) < 0)
348 /* The kernel we're running on doesn't support the GETREGS
349 request. Reset `have_ptrace_getregs'. */
350 have_ptrace_getregs = 0;
354 perror_with_name ("Couldn't get registers");
357 supply_gregset (®s);
360 /* Store all valid general-purpose registers in GDB's register array
361 into the process/thread specified by TID. */
364 store_regs (int tid, int regno)
368 if (ptrace (PTRACE_GETREGS, tid, 0, (int) ®s) < 0)
369 perror_with_name ("Couldn't get registers");
371 fill_gregset (®s, regno);
373 if (ptrace (PTRACE_SETREGS, tid, 0, (int) ®s) < 0)
374 perror_with_name ("Couldn't write registers");
379 static void fetch_regs (int tid) {}
380 static void store_regs (int tid, int regno) {}
385 /* Transfering floating-point registers between GDB, inferiors and cores. */
387 /* Fill GDB's register array with the floating-point register values in
391 supply_fpregset (elf_fpregset_t *fpregsetp)
393 i387_supply_fsave ((char *) fpregsetp);
397 /* Fill register REGNO (if it is a floating-point register) in
398 *FPREGSETP with the value in GDB's register array. If REGNO is -1,
399 do this for all registers. */
402 fill_fpregset (elf_fpregset_t *fpregsetp, int regno)
404 i387_fill_fsave ((char *) fpregsetp, regno);
407 #ifdef HAVE_PTRACE_GETREGS
409 /* Fetch all floating-point registers from process/thread TID and store
410 thier values in GDB's register array. */
413 fetch_fpregs (int tid)
415 elf_fpregset_t fpregs;
417 if (ptrace (PTRACE_GETFPREGS, tid, 0, (int) &fpregs) < 0)
418 perror_with_name ("Couldn't get floating point status");
420 supply_fpregset (&fpregs);
423 /* Store all valid floating-point registers in GDB's register array
424 into the process/thread specified by TID. */
427 store_fpregs (int tid, int regno)
429 elf_fpregset_t fpregs;
431 if (ptrace (PTRACE_GETFPREGS, tid, 0, (int) &fpregs) < 0)
432 perror_with_name ("Couldn't get floating point status");
434 fill_fpregset (&fpregs, regno);
436 if (ptrace (PTRACE_SETFPREGS, tid, 0, (int) &fpregs) < 0)
437 perror_with_name ("Couldn't write floating point status");
442 static void fetch_fpregs (int tid) {}
443 static void store_fpregs (int tid, int regno) {}
448 /* Transfering floating-point and SSE registers to and from GDB. */
450 #ifdef HAVE_PTRACE_GETFPXREGS
452 /* Fill GDB's register array with the floating-point and SSE register
453 values in *FPXREGSETP. */
456 supply_fpxregset (elf_fpxregset_t *fpxregsetp)
458 i387_supply_fxsave ((char *) fpxregsetp);
461 /* Fill register REGNO (if it is a floating-point or SSE register) in
462 *FPXREGSETP with the value in GDB's register array. If REGNO is
463 -1, do this for all registers. */
466 fill_fpxregset (elf_fpxregset_t *fpxregsetp, int regno)
468 i387_fill_fxsave ((char *) fpxregsetp, regno);
471 /* Fetch all registers covered by the PTRACE_GETFPXREGS request from
472 process/thread TID and store their values in GDB's register array.
473 Return non-zero if successful, zero otherwise. */
476 fetch_fpxregs (int tid)
478 elf_fpxregset_t fpxregs;
480 if (! have_ptrace_getfpxregs)
483 if (ptrace (PTRACE_GETFPXREGS, tid, 0, (int) &fpxregs) < 0)
487 have_ptrace_getfpxregs = 0;
491 perror_with_name ("Couldn't read floating-point and SSE registers");
494 supply_fpxregset (&fpxregs);
498 /* Store all valid registers in GDB's register array covered by the
499 PTRACE_SETFPXREGS request into the process/thread specified by TID.
500 Return non-zero if successful, zero otherwise. */
503 store_fpxregs (int tid, int regno)
505 elf_fpxregset_t fpxregs;
507 if (! have_ptrace_getfpxregs)
510 if (ptrace (PTRACE_GETFPXREGS, tid, 0, &fpxregs) == -1)
514 have_ptrace_getfpxregs = 0;
518 perror_with_name ("Couldn't read floating-point and SSE registers");
521 fill_fpxregset (&fpxregs, regno);
523 if (ptrace (PTRACE_SETFPXREGS, tid, 0, &fpxregs) == -1)
524 perror_with_name ("Couldn't write floating-point and SSE registers");
529 /* Fill the XMM registers in the register array with dummy values. For
530 cases where we don't have access to the XMM registers. I think
531 this is cleaner than printing a warning. For a cleaner solution,
532 we should gdbarchify the i386 family. */
535 dummy_sse_values (void)
537 /* C doesn't have a syntax for NaN's, so write it out as an array of
539 static long dummy[4] = { 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff };
540 static long mxcsr = 0x1f80;
543 for (reg = 0; reg < 8; reg++)
544 supply_register (XMM0_REGNUM + reg, (char *) dummy);
545 supply_register (MXCSR_REGNUM, (char *) &mxcsr);
550 static int fetch_fpxregs (int tid) { return 0; }
551 static int store_fpxregs (int tid, int regno) { return 0; }
552 static void dummy_sse_values (void) {}
554 #endif /* HAVE_PTRACE_GETFPXREGS */
557 /* Transferring arbitrary registers between GDB and inferior. */
559 /* Check if register REGNO in the child process is accessible.
560 If we are accessing registers directly via the U area, only the
561 general-purpose registers are available.
562 All registers should be accessible if we have GETREGS support. */
565 cannot_fetch_register (int regno)
567 if (! have_ptrace_getregs)
568 return OLD_CANNOT_FETCH_REGISTER (regno);
572 cannot_store_register (int regno)
574 if (! have_ptrace_getregs)
575 return OLD_CANNOT_STORE_REGISTER (regno);
579 /* Fetch register REGNO from the child process. If REGNO is -1, do
580 this for all registers (including the floating point and SSE
584 fetch_inferior_registers (int regno)
588 /* Use the old method of peeking around in `struct user' if the
589 GETREGS request isn't available. */
590 if (! have_ptrace_getregs)
592 old_fetch_inferior_registers (regno);
596 /* Linux LWP ID's are process ID's. */
597 if ((tid = TIDGET (inferior_ptid)) == 0)
598 tid = PIDGET (inferior_ptid); /* Not a threaded program. */
600 /* Use the PTRACE_GETFPXREGS request whenever possible, since it
601 transfers more registers in one system call, and we'll cache the
602 results. But remember that fetch_fpxregs can fail, and return
608 /* The call above might reset `have_ptrace_getregs'. */
609 if (! have_ptrace_getregs)
611 old_fetch_inferior_registers (-1);
615 if (fetch_fpxregs (tid))
621 if (GETREGS_SUPPLIES (regno))
627 if (GETFPXREGS_SUPPLIES (regno))
629 if (fetch_fpxregs (tid))
632 /* Either our processor or our kernel doesn't support the SSE
633 registers, so read the FP registers in the traditional way,
634 and fill the SSE registers with dummy values. It would be
635 more graceful to handle differences in the register set using
636 gdbarch. Until then, this will at least make things work
642 internal_error (__FILE__, __LINE__,
643 "Got request for bad register number %d.", regno);
646 /* Store register REGNO back into the child process. If REGNO is -1,
647 do this for all registers (including the floating point and SSE
650 store_inferior_registers (int regno)
654 /* Use the old method of poking around in `struct user' if the
655 SETREGS request isn't available. */
656 if (! have_ptrace_getregs)
658 old_store_inferior_registers (regno);
662 /* Linux LWP ID's are process ID's. */
663 if ((tid = TIDGET (inferior_ptid)) == 0)
664 tid = PIDGET (inferior_ptid); /* Not a threaded program. */
666 /* Use the PTRACE_SETFPXREGS requests whenever possible, since it
667 transfers more registers in one system call. But remember that
668 store_fpxregs can fail, and return zero. */
671 store_regs (tid, regno);
672 if (store_fpxregs (tid, regno))
674 store_fpregs (tid, regno);
678 if (GETREGS_SUPPLIES (regno))
680 store_regs (tid, regno);
684 if (GETFPXREGS_SUPPLIES (regno))
686 if (store_fpxregs (tid, regno))
689 /* Either our processor or our kernel doesn't support the SSE
690 registers, so just write the FP registers in the traditional
692 store_fpregs (tid, regno);
696 internal_error (__FILE__, __LINE__,
697 "Got request to store bad register number %d.", regno);
702 i386_linux_dr_get (int regnum)
707 /* FIXME: kettenis/2001-01-29: It's not clear what we should do with
708 multi-threaded processes here. For now, pretend there is just
710 tid = PIDGET (inferior_ptid);
712 /* FIXME: kettenis/2001-03-27: Calling perror_with_name if the
713 ptrace call fails breaks debugging remote targets. The correct
714 way to fix this is to add the hardware breakpoint and watchpoint
715 stuff to the target vectore. For now, just return zero if the
716 ptrace call fails. */
718 value = ptrace (PT_READ_U, tid,
719 offsetof (struct user, u_debugreg[regnum]), 0);
722 perror_with_name ("Couldn't read debug register");
731 i386_linux_dr_set (int regnum, unsigned long value)
735 /* FIXME: kettenis/2001-01-29: It's not clear what we should do with
736 multi-threaded processes here. For now, pretend there is just
738 tid = PIDGET (inferior_ptid);
741 ptrace (PT_WRITE_U, tid,
742 offsetof (struct user, u_debugreg[regnum]), value);
744 perror_with_name ("Couldn't write debug register");
748 i386_linux_dr_set_control (unsigned long control)
750 i386_linux_dr_set (DR_CONTROL, control);
754 i386_linux_dr_set_addr (int regnum, CORE_ADDR addr)
756 gdb_assert (regnum >= 0 && regnum <= DR_LASTADDR - DR_FIRSTADDR);
758 i386_linux_dr_set (DR_FIRSTADDR + regnum, addr);
762 i386_linux_dr_reset_addr (int regnum)
764 gdb_assert (regnum >= 0 && regnum <= DR_LASTADDR - DR_FIRSTADDR);
766 i386_linux_dr_set (DR_FIRSTADDR + regnum, 0L);
770 i386_linux_dr_get_status (void)
772 return i386_linux_dr_get (DR_STATUS);
776 /* Interpreting register set info found in core files. */
778 /* Provide registers to GDB from a core file.
780 (We can't use the generic version of this function in
781 core-regset.c, because Linux has *three* different kinds of
782 register set notes. core-regset.c would have to call
783 supply_fpxregset, which most platforms don't have.)
785 CORE_REG_SECT points to an array of bytes, which are the contents
786 of a `note' from a core file which BFD thinks might contain
787 register contents. CORE_REG_SIZE is its size.
789 WHICH says which register set corelow suspects this is:
790 0 --- the general-purpose register set, in elf_gregset_t format
791 2 --- the floating-point register set, in elf_fpregset_t format
792 3 --- the extended floating-point register set, in elf_fpxregset_t format
794 REG_ADDR isn't used on Linux. */
797 fetch_core_registers (char *core_reg_sect, unsigned core_reg_size,
798 int which, CORE_ADDR reg_addr)
800 elf_gregset_t gregset;
801 elf_fpregset_t fpregset;
806 if (core_reg_size != sizeof (gregset))
807 warning ("Wrong size gregset in core file.");
810 memcpy (&gregset, core_reg_sect, sizeof (gregset));
811 supply_gregset (&gregset);
816 if (core_reg_size != sizeof (fpregset))
817 warning ("Wrong size fpregset in core file.");
820 memcpy (&fpregset, core_reg_sect, sizeof (fpregset));
821 supply_fpregset (&fpregset);
825 #ifdef HAVE_PTRACE_GETFPXREGS
827 elf_fpxregset_t fpxregset;
830 if (core_reg_size != sizeof (fpxregset))
831 warning ("Wrong size fpxregset in core file.");
834 memcpy (&fpxregset, core_reg_sect, sizeof (fpxregset));
835 supply_fpxregset (&fpxregset);
842 /* We've covered all the kinds of registers we know about here,
843 so this must be something we wouldn't know what to do with
844 anyway. Just ignore it. */
850 /* The instruction for a Linux system call is:
854 static const unsigned char linux_syscall[] = { 0xcd, 0x80 };
856 #define LINUX_SYSCALL_LEN (sizeof linux_syscall)
858 /* The system call number is stored in the %eax register. */
859 #define LINUX_SYSCALL_REGNUM 0 /* %eax */
861 /* We are specifically interested in the sigreturn and rt_sigreturn
864 #ifndef SYS_sigreturn
865 #define SYS_sigreturn 0x77
867 #ifndef SYS_rt_sigreturn
868 #define SYS_rt_sigreturn 0xad
871 /* Offset to saved processor flags, from <asm/sigcontext.h>. */
872 #define LINUX_SIGCONTEXT_EFLAGS_OFFSET (64)
874 /* Resume execution of the inferior process.
875 If STEP is nonzero, single-step it.
876 If SIGNAL is nonzero, give it that signal. */
879 child_resume (ptid_t ptid, int step, enum target_signal signal)
881 int pid = PIDGET (ptid);
883 int request = PTRACE_CONT;
886 /* Resume all threads. */
887 /* I think this only gets used in the non-threaded case, where "resume
888 all threads" and "resume inferior_ptid" are the same. */
889 pid = PIDGET (inferior_ptid);
893 CORE_ADDR pc = read_pc_pid (pid_to_ptid (pid));
894 unsigned char buf[LINUX_SYSCALL_LEN];
896 request = PTRACE_SINGLESTEP;
898 /* Returning from a signal trampoline is done by calling a
899 special system call (sigreturn or rt_sigreturn, see
900 i386-linux-tdep.c for more information). This system call
901 restores the registers that were saved when the signal was
902 raised, including %eflags. That means that single-stepping
903 won't work. Instead, we'll have to modify the signal context
904 that's about to be restored, and set the trace flag there. */
906 /* First check if PC is at a system call. */
907 if (read_memory_nobpt (pc, (char *) buf, LINUX_SYSCALL_LEN) == 0
908 && memcmp (buf, linux_syscall, LINUX_SYSCALL_LEN) == 0)
910 int syscall = read_register_pid (LINUX_SYSCALL_REGNUM,
913 /* Then check the system call number. */
914 if (syscall == SYS_sigreturn || syscall == SYS_rt_sigreturn)
916 CORE_ADDR sp = read_register (SP_REGNUM);
918 unsigned long int eflags;
920 if (syscall == SYS_rt_sigreturn)
921 addr = read_memory_integer (sp + 8, 4) + 20;
923 /* Set the trace flag in the context that's about to be
925 addr += LINUX_SIGCONTEXT_EFLAGS_OFFSET;
926 read_memory (addr, (char *) &eflags, 4);
928 write_memory (addr, (char *) &eflags, 4);
933 if (ptrace (request, pid, 0, target_signal_to_host (signal)) == -1)
934 perror_with_name ("ptrace");
938 /* Register that we are able to handle Linux ELF core file formats. */
940 static struct core_fns linux_elf_core_fns =
942 bfd_target_elf_flavour, /* core_flavour */
943 default_check_format, /* check_format */
944 default_core_sniffer, /* core_sniffer */
945 fetch_core_registers, /* core_read_registers */
950 _initialize_i386_linux_nat (void)
952 add_core_fns (&linux_elf_core_fns);