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 /* Defines ps_err_e, struct ps_prochandle. */
74 #include "gdb_proc_service.h"
76 /* Prototypes for local functions. */
77 static void dummy_sse_values (void);
80 /* The register sets used in GNU/Linux ELF core-dumps are identical to
81 the register sets in `struct user' that is used for a.out
82 core-dumps, and is also used by `ptrace'. The corresponding types
83 are `elf_gregset_t' for the general-purpose registers (with
84 `elf_greg_t' the type of a single GP register) and `elf_fpregset_t'
85 for the floating-point registers.
87 Those types used to be available under the names `gregset_t' and
88 `fpregset_t' too, and this file used those names in the past. But
89 those names are now used for the register sets used in the
90 `mcontext_t' type, and have a different size and layout. */
92 /* Mapping between the general-purpose registers in `struct user'
93 format and GDB's register array layout. */
100 -1, -1, -1, -1, /* st0, st1, st2, st3 */
101 -1, -1, -1, -1, /* st4, st5, st6, st7 */
102 -1, -1, -1, -1, /* fctrl, fstat, ftag, fiseg */
103 -1, -1, -1, -1, /* fioff, foseg, fooff, fop */
104 -1, -1, -1, -1, /* xmm0, xmm1, xmm2, xmm3 */
105 -1, -1, -1, -1, /* xmm4, xmm5, xmm6, xmm6 */
110 /* Which ptrace request retrieves which registers?
111 These apply to the corresponding SET requests as well. */
113 #define GETREGS_SUPPLIES(regno) \
114 ((0 <= (regno) && (regno) <= 15) || (regno) == I386_LINUX_ORIG_EAX_REGNUM)
116 #define GETFPREGS_SUPPLIES(regno) \
117 (FP0_REGNUM <= (regno) && (regno) <= LAST_FPU_CTRL_REGNUM)
119 #define GETFPXREGS_SUPPLIES(regno) \
120 (FP0_REGNUM <= (regno) && (regno) <= MXCSR_REGNUM)
122 /* Does the current host support the GETREGS request? */
123 int have_ptrace_getregs =
124 #ifdef HAVE_PTRACE_GETREGS
131 /* Does the current host support the GETFPXREGS request? The header
132 file may or may not define it, and even if it is defined, the
133 kernel will return EIO if it's running on a pre-SSE processor.
135 My instinct is to attach this to some architecture- or
136 target-specific data structure, but really, a particular GDB
137 process can only run on top of one kernel at a time. So it's okay
138 for this to be a simple variable. */
139 int have_ptrace_getfpxregs =
140 #ifdef HAVE_PTRACE_GETFPXREGS
148 /* Support for the user struct. */
150 /* Return the address of register REGNUM. BLOCKEND is the value of
151 u.u_ar0, which should point to the registers. */
154 register_u_addr (CORE_ADDR blockend, int regnum)
156 return (blockend + 4 * regmap[regnum]);
159 /* Return the size of the user struct. */
164 return (sizeof (struct user));
168 /* Accessing registers through the U area, one at a time. */
170 /* Fetch one register. */
173 fetch_register (int regno)
178 gdb_assert (!have_ptrace_getregs);
179 if (cannot_fetch_register (regno))
181 supply_register (regno, NULL);
185 /* GNU/Linux LWP ID's are process ID's. */
186 tid = TIDGET (inferior_ptid);
188 tid = PIDGET (inferior_ptid); /* Not a threaded program. */
191 val = ptrace (PTRACE_PEEKUSER, tid, register_addr (regno, 0), 0);
193 error ("Couldn't read register %s (#%d): %s.", REGISTER_NAME (regno),
194 regno, safe_strerror (errno));
196 supply_register (regno, &val);
199 /* Store one register. */
202 store_register (int regno)
207 gdb_assert (!have_ptrace_getregs);
208 if (cannot_store_register (regno))
211 /* GNU/Linux LWP ID's are process ID's. */
212 tid = TIDGET (inferior_ptid);
214 tid = PIDGET (inferior_ptid); /* Not a threaded program. */
217 regcache_collect (regno, &val);
218 ptrace (PTRACE_POKEUSER, tid, register_addr (regno, 0), val);
220 error ("Couldn't write register %s (#%d): %s.", REGISTER_NAME (regno),
221 regno, safe_strerror (errno));
225 /* Transfering the general-purpose registers between GDB, inferiors
228 /* Fill GDB's register array with the general-purpose register values
232 supply_gregset (elf_gregset_t *gregsetp)
234 elf_greg_t *regp = (elf_greg_t *) gregsetp;
237 for (i = 0; i < I386_NUM_GREGS; i++)
238 supply_register (i, regp + regmap[i]);
240 if (I386_LINUX_ORIG_EAX_REGNUM < NUM_REGS)
241 supply_register (I386_LINUX_ORIG_EAX_REGNUM, regp + ORIG_EAX);
244 /* Fill register REGNO (if it is a general-purpose register) in
245 *GREGSETPS with the value in GDB's register array. If REGNO is -1,
246 do this for all registers. */
249 fill_gregset (elf_gregset_t *gregsetp, int regno)
251 elf_greg_t *regp = (elf_greg_t *) gregsetp;
254 for (i = 0; i < I386_NUM_GREGS; i++)
255 if (regno == -1 || regno == i)
256 regcache_collect (i, regp + regmap[i]);
258 if ((regno == -1 || regno == I386_LINUX_ORIG_EAX_REGNUM)
259 && I386_LINUX_ORIG_EAX_REGNUM < NUM_REGS)
260 regcache_collect (I386_LINUX_ORIG_EAX_REGNUM, regp + ORIG_EAX);
263 #ifdef HAVE_PTRACE_GETREGS
265 /* Fetch all general-purpose registers from process/thread TID and
266 store their values in GDB's register array. */
273 if (ptrace (PTRACE_GETREGS, tid, 0, (int) ®s) < 0)
277 /* The kernel we're running on doesn't support the GETREGS
278 request. Reset `have_ptrace_getregs'. */
279 have_ptrace_getregs = 0;
283 perror_with_name ("Couldn't get registers");
286 supply_gregset (®s);
289 /* Store all valid general-purpose registers in GDB's register array
290 into the process/thread specified by TID. */
293 store_regs (int tid, int regno)
297 if (ptrace (PTRACE_GETREGS, tid, 0, (int) ®s) < 0)
298 perror_with_name ("Couldn't get registers");
300 fill_gregset (®s, regno);
302 if (ptrace (PTRACE_SETREGS, tid, 0, (int) ®s) < 0)
303 perror_with_name ("Couldn't write registers");
308 static void fetch_regs (int tid) {}
309 static void store_regs (int tid, int regno) {}
314 /* Transfering floating-point registers between GDB, inferiors and cores. */
316 /* Fill GDB's register array with the floating-point register values in
320 supply_fpregset (elf_fpregset_t *fpregsetp)
322 i387_supply_fsave ((char *) fpregsetp);
326 /* Fill register REGNO (if it is a floating-point register) in
327 *FPREGSETP with the value in GDB's register array. If REGNO is -1,
328 do this for all registers. */
331 fill_fpregset (elf_fpregset_t *fpregsetp, int regno)
333 i387_fill_fsave ((char *) fpregsetp, regno);
336 #ifdef HAVE_PTRACE_GETREGS
338 /* Fetch all floating-point registers from process/thread TID and store
339 thier values in GDB's register array. */
342 fetch_fpregs (int tid)
344 elf_fpregset_t fpregs;
346 if (ptrace (PTRACE_GETFPREGS, tid, 0, (int) &fpregs) < 0)
347 perror_with_name ("Couldn't get floating point status");
349 supply_fpregset (&fpregs);
352 /* Store all valid floating-point registers in GDB's register array
353 into the process/thread specified by TID. */
356 store_fpregs (int tid, int regno)
358 elf_fpregset_t fpregs;
360 if (ptrace (PTRACE_GETFPREGS, tid, 0, (int) &fpregs) < 0)
361 perror_with_name ("Couldn't get floating point status");
363 fill_fpregset (&fpregs, regno);
365 if (ptrace (PTRACE_SETFPREGS, tid, 0, (int) &fpregs) < 0)
366 perror_with_name ("Couldn't write floating point status");
371 static void fetch_fpregs (int tid) {}
372 static void store_fpregs (int tid, int regno) {}
377 /* Transfering floating-point and SSE registers to and from GDB. */
379 #ifdef HAVE_PTRACE_GETFPXREGS
381 /* Fill GDB's register array with the floating-point and SSE register
382 values in *FPXREGSETP. */
385 supply_fpxregset (elf_fpxregset_t *fpxregsetp)
387 i387_supply_fxsave ((char *) fpxregsetp);
390 /* Fill register REGNO (if it is a floating-point or SSE register) in
391 *FPXREGSETP with the value in GDB's register array. If REGNO is
392 -1, do this for all registers. */
395 fill_fpxregset (elf_fpxregset_t *fpxregsetp, int regno)
397 i387_fill_fxsave ((char *) fpxregsetp, regno);
400 /* Fetch all registers covered by the PTRACE_GETFPXREGS request from
401 process/thread TID and store their values in GDB's register array.
402 Return non-zero if successful, zero otherwise. */
405 fetch_fpxregs (int tid)
407 elf_fpxregset_t fpxregs;
409 if (! have_ptrace_getfpxregs)
412 if (ptrace (PTRACE_GETFPXREGS, tid, 0, (int) &fpxregs) < 0)
416 have_ptrace_getfpxregs = 0;
420 perror_with_name ("Couldn't read floating-point and SSE registers");
423 supply_fpxregset (&fpxregs);
427 /* Store all valid registers in GDB's register array covered by the
428 PTRACE_SETFPXREGS request into the process/thread specified by TID.
429 Return non-zero if successful, zero otherwise. */
432 store_fpxregs (int tid, int regno)
434 elf_fpxregset_t fpxregs;
436 if (! have_ptrace_getfpxregs)
439 if (ptrace (PTRACE_GETFPXREGS, tid, 0, &fpxregs) == -1)
443 have_ptrace_getfpxregs = 0;
447 perror_with_name ("Couldn't read floating-point and SSE registers");
450 fill_fpxregset (&fpxregs, regno);
452 if (ptrace (PTRACE_SETFPXREGS, tid, 0, &fpxregs) == -1)
453 perror_with_name ("Couldn't write floating-point and SSE registers");
458 /* Fill the XMM registers in the register array with dummy values. For
459 cases where we don't have access to the XMM registers. I think
460 this is cleaner than printing a warning. For a cleaner solution,
461 we should gdbarchify the i386 family. */
464 dummy_sse_values (void)
466 struct gdbarch_tdep *tdep = gdbarch_tdep (current_gdbarch);
467 /* C doesn't have a syntax for NaN's, so write it out as an array of
469 static long dummy[4] = { 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff };
470 static long mxcsr = 0x1f80;
473 for (reg = 0; reg < tdep->num_xmm_regs; reg++)
474 supply_register (XMM0_REGNUM + reg, (char *) dummy);
475 if (tdep->num_xmm_regs > 0)
476 supply_register (MXCSR_REGNUM, (char *) &mxcsr);
481 static int fetch_fpxregs (int tid) { return 0; }
482 static int store_fpxregs (int tid, int regno) { return 0; }
483 static void dummy_sse_values (void) {}
485 #endif /* HAVE_PTRACE_GETFPXREGS */
488 /* Transferring arbitrary registers between GDB and inferior. */
490 /* Check if register REGNO in the child process is accessible.
491 If we are accessing registers directly via the U area, only the
492 general-purpose registers are available.
493 All registers should be accessible if we have GETREGS support. */
496 cannot_fetch_register (int regno)
498 gdb_assert (regno >= 0 && regno < NUM_REGS);
499 return (!have_ptrace_getregs && regmap[regno] == -1);
503 cannot_store_register (int regno)
505 gdb_assert (regno >= 0 && regno < NUM_REGS);
506 return (!have_ptrace_getregs && regmap[regno] == -1);
509 /* Fetch register REGNO from the child process. If REGNO is -1, do
510 this for all registers (including the floating point and SSE
514 fetch_inferior_registers (int regno)
518 /* Use the old method of peeking around in `struct user' if the
519 GETREGS request isn't available. */
520 if (!have_ptrace_getregs)
524 for (i = 0; i < NUM_REGS; i++)
525 if (regno == -1 || regno == i)
531 /* GNU/Linux LWP ID's are process ID's. */
532 tid = TIDGET (inferior_ptid);
534 tid = PIDGET (inferior_ptid); /* Not a threaded program. */
536 /* Use the PTRACE_GETFPXREGS request whenever possible, since it
537 transfers more registers in one system call, and we'll cache the
538 results. But remember that fetch_fpxregs can fail, and return
544 /* The call above might reset `have_ptrace_getregs'. */
545 if (!have_ptrace_getregs)
547 fetch_inferior_registers (regno);
551 if (fetch_fpxregs (tid))
557 if (GETREGS_SUPPLIES (regno))
563 if (GETFPXREGS_SUPPLIES (regno))
565 if (fetch_fpxregs (tid))
568 /* Either our processor or our kernel doesn't support the SSE
569 registers, so read the FP registers in the traditional way,
570 and fill the SSE registers with dummy values. It would be
571 more graceful to handle differences in the register set using
572 gdbarch. Until then, this will at least make things work
578 internal_error (__FILE__, __LINE__,
579 "Got request for bad register number %d.", regno);
582 /* Store register REGNO back into the child process. If REGNO is -1,
583 do this for all registers (including the floating point and SSE
586 store_inferior_registers (int regno)
590 /* Use the old method of poking around in `struct user' if the
591 SETREGS request isn't available. */
592 if (!have_ptrace_getregs)
596 for (i = 0; i < NUM_REGS; i++)
597 if (regno == -1 || regno == i)
603 /* GNU/Linux LWP ID's are process ID's. */
604 tid = TIDGET (inferior_ptid);
606 tid = PIDGET (inferior_ptid); /* Not a threaded program. */
608 /* Use the PTRACE_SETFPXREGS requests whenever possible, since it
609 transfers more registers in one system call. But remember that
610 store_fpxregs can fail, and return zero. */
613 store_regs (tid, regno);
614 if (store_fpxregs (tid, regno))
616 store_fpregs (tid, regno);
620 if (GETREGS_SUPPLIES (regno))
622 store_regs (tid, regno);
626 if (GETFPXREGS_SUPPLIES (regno))
628 if (store_fpxregs (tid, regno))
631 /* Either our processor or our kernel doesn't support the SSE
632 registers, so just write the FP registers in the traditional
634 store_fpregs (tid, regno);
638 internal_error (__FILE__, __LINE__,
639 "Got request to store bad register number %d.", regno);
644 i386_linux_dr_get (int regnum)
649 /* FIXME: kettenis/2001-01-29: It's not clear what we should do with
650 multi-threaded processes here. For now, pretend there is just
652 tid = PIDGET (inferior_ptid);
654 /* FIXME: kettenis/2001-03-27: Calling perror_with_name if the
655 ptrace call fails breaks debugging remote targets. The correct
656 way to fix this is to add the hardware breakpoint and watchpoint
657 stuff to the target vectore. For now, just return zero if the
658 ptrace call fails. */
660 value = ptrace (PTRACE_PEEKUSER, tid,
661 offsetof (struct user, u_debugreg[regnum]), 0);
664 perror_with_name ("Couldn't read debug register");
673 i386_linux_dr_set (int regnum, unsigned long value)
677 /* FIXME: kettenis/2001-01-29: It's not clear what we should do with
678 multi-threaded processes here. For now, pretend there is just
680 tid = PIDGET (inferior_ptid);
683 ptrace (PTRACE_POKEUSER, tid,
684 offsetof (struct user, u_debugreg[regnum]), value);
686 perror_with_name ("Couldn't write debug register");
690 ps_get_thread_area(const struct ps_prochandle *ph,
691 lwpid_t lwpid, int idx, void **base)
693 unsigned long int desc[3];
694 #define PTRACE_GET_THREAD_AREA 25
696 if (ptrace (PTRACE_GET_THREAD_AREA,
697 lwpid, (void *) idx, (unsigned long) &desc) < 0)
700 *(int *)base = desc[1];
705 i386_linux_dr_set_control (unsigned long control)
707 i386_linux_dr_set (DR_CONTROL, control);
711 i386_linux_dr_set_addr (int regnum, CORE_ADDR addr)
713 gdb_assert (regnum >= 0 && regnum <= DR_LASTADDR - DR_FIRSTADDR);
715 i386_linux_dr_set (DR_FIRSTADDR + regnum, addr);
719 i386_linux_dr_reset_addr (int regnum)
721 gdb_assert (regnum >= 0 && regnum <= DR_LASTADDR - DR_FIRSTADDR);
723 i386_linux_dr_set (DR_FIRSTADDR + regnum, 0L);
727 i386_linux_dr_get_status (void)
729 return i386_linux_dr_get (DR_STATUS);
733 /* Interpreting register set info found in core files. */
735 /* Provide registers to GDB from a core file.
737 (We can't use the generic version of this function in
738 core-regset.c, because GNU/Linux has *three* different kinds of
739 register set notes. core-regset.c would have to call
740 supply_fpxregset, which most platforms don't have.)
742 CORE_REG_SECT points to an array of bytes, which are the contents
743 of a `note' from a core file which BFD thinks might contain
744 register contents. CORE_REG_SIZE is its size.
746 WHICH says which register set corelow suspects this is:
747 0 --- the general-purpose register set, in elf_gregset_t format
748 2 --- the floating-point register set, in elf_fpregset_t format
749 3 --- the extended floating-point register set, in elf_fpxregset_t format
751 REG_ADDR isn't used on GNU/Linux. */
754 fetch_core_registers (char *core_reg_sect, unsigned core_reg_size,
755 int which, CORE_ADDR reg_addr)
757 elf_gregset_t gregset;
758 elf_fpregset_t fpregset;
763 if (core_reg_size != sizeof (gregset))
764 warning ("Wrong size gregset in core file.");
767 memcpy (&gregset, core_reg_sect, sizeof (gregset));
768 supply_gregset (&gregset);
773 if (core_reg_size != sizeof (fpregset))
774 warning ("Wrong size fpregset in core file.");
777 memcpy (&fpregset, core_reg_sect, sizeof (fpregset));
778 supply_fpregset (&fpregset);
782 #ifdef HAVE_PTRACE_GETFPXREGS
784 elf_fpxregset_t fpxregset;
787 if (core_reg_size != sizeof (fpxregset))
788 warning ("Wrong size fpxregset in core file.");
791 memcpy (&fpxregset, core_reg_sect, sizeof (fpxregset));
792 supply_fpxregset (&fpxregset);
799 /* We've covered all the kinds of registers we know about here,
800 so this must be something we wouldn't know what to do with
801 anyway. Just ignore it. */
807 /* The instruction for a GNU/Linux system call is:
811 static const unsigned char linux_syscall[] = { 0xcd, 0x80 };
813 #define LINUX_SYSCALL_LEN (sizeof linux_syscall)
815 /* The system call number is stored in the %eax register. */
816 #define LINUX_SYSCALL_REGNUM 0 /* %eax */
818 /* We are specifically interested in the sigreturn and rt_sigreturn
821 #ifndef SYS_sigreturn
822 #define SYS_sigreturn 0x77
824 #ifndef SYS_rt_sigreturn
825 #define SYS_rt_sigreturn 0xad
828 /* Offset to saved processor flags, from <asm/sigcontext.h>. */
829 #define LINUX_SIGCONTEXT_EFLAGS_OFFSET (64)
831 /* Resume execution of the inferior process.
832 If STEP is nonzero, single-step it.
833 If SIGNAL is nonzero, give it that signal. */
836 child_resume (ptid_t ptid, int step, enum target_signal signal)
838 int pid = PIDGET (ptid);
840 int request = PTRACE_CONT;
843 /* Resume all threads. */
844 /* I think this only gets used in the non-threaded case, where "resume
845 all threads" and "resume inferior_ptid" are the same. */
846 pid = PIDGET (inferior_ptid);
850 CORE_ADDR pc = read_pc_pid (pid_to_ptid (pid));
851 unsigned char buf[LINUX_SYSCALL_LEN];
853 request = PTRACE_SINGLESTEP;
855 /* Returning from a signal trampoline is done by calling a
856 special system call (sigreturn or rt_sigreturn, see
857 i386-linux-tdep.c for more information). This system call
858 restores the registers that were saved when the signal was
859 raised, including %eflags. That means that single-stepping
860 won't work. Instead, we'll have to modify the signal context
861 that's about to be restored, and set the trace flag there. */
863 /* First check if PC is at a system call. */
864 if (read_memory_nobpt (pc, (char *) buf, LINUX_SYSCALL_LEN) == 0
865 && memcmp (buf, linux_syscall, LINUX_SYSCALL_LEN) == 0)
867 int syscall = read_register_pid (LINUX_SYSCALL_REGNUM,
870 /* Then check the system call number. */
871 if (syscall == SYS_sigreturn || syscall == SYS_rt_sigreturn)
873 CORE_ADDR sp = read_register (I386_ESP_REGNUM);
875 unsigned long int eflags;
877 if (syscall == SYS_rt_sigreturn)
878 addr = read_memory_integer (sp + 8, 4) + 20;
880 /* Set the trace flag in the context that's about to be
882 addr += LINUX_SIGCONTEXT_EFLAGS_OFFSET;
883 read_memory (addr, (char *) &eflags, 4);
885 write_memory (addr, (char *) &eflags, 4);
890 if (ptrace (request, pid, 0, target_signal_to_host (signal)) == -1)
891 perror_with_name ("ptrace");
895 /* Register that we are able to handle GNU/Linux ELF core file
898 static struct core_fns linux_elf_core_fns =
900 bfd_target_elf_flavour, /* core_flavour */
901 default_check_format, /* check_format */
902 default_core_sniffer, /* core_sniffer */
903 fetch_core_registers, /* core_read_registers */
908 _initialize_i386_linux_nat (void)
910 add_core_fns (&linux_elf_core_fns);