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. */
26 #include "linux-nat.h"
28 #include "gdb_assert.h"
29 #include "gdb_string.h"
30 #include <sys/ptrace.h>
32 #include <sys/procfs.h>
42 #ifdef HAVE_SYS_DEBUGREG_H
43 #include <sys/debugreg.h>
47 #define DR_FIRSTADDR 0
62 /* Prototypes for supply_gregset etc. */
65 /* Prototypes for i387_supply_fsave etc. */
66 #include "i387-tdep.h"
68 /* Defines for XMM0_REGNUM etc. */
69 #include "i386-tdep.h"
71 /* Defines I386_LINUX_ORIG_EAX_REGNUM. */
72 #include "i386-linux-tdep.h"
74 /* Defines ps_err_e, struct ps_prochandle. */
75 #include "gdb_proc_service.h"
77 /* Prototypes for local functions. */
78 static void dummy_sse_values (void);
81 /* The register sets used in GNU/Linux ELF core-dumps are identical to
82 the register sets in `struct user' that is used for a.out
83 core-dumps, and is also used by `ptrace'. The corresponding types
84 are `elf_gregset_t' for the general-purpose registers (with
85 `elf_greg_t' the type of a single GP register) and `elf_fpregset_t'
86 for the floating-point registers.
88 Those types used to be available under the names `gregset_t' and
89 `fpregset_t' too, and this file used those names in the past. But
90 those names are now used for the register sets used in the
91 `mcontext_t' type, and have a different size and layout. */
93 /* Mapping between the general-purpose registers in `struct user'
94 format and GDB's register array layout. */
101 -1, -1, -1, -1, /* st0, st1, st2, st3 */
102 -1, -1, -1, -1, /* st4, st5, st6, st7 */
103 -1, -1, -1, -1, /* fctrl, fstat, ftag, fiseg */
104 -1, -1, -1, -1, /* fioff, foseg, fooff, fop */
105 -1, -1, -1, -1, /* xmm0, xmm1, xmm2, xmm3 */
106 -1, -1, -1, -1, /* xmm4, xmm5, xmm6, xmm6 */
111 /* Which ptrace request retrieves which registers?
112 These apply to the corresponding SET requests as well. */
114 #define GETREGS_SUPPLIES(regno) \
115 ((0 <= (regno) && (regno) <= 15) || (regno) == I386_LINUX_ORIG_EAX_REGNUM)
117 #define GETFPREGS_SUPPLIES(regno) \
118 (FP0_REGNUM <= (regno) && (regno) <= LAST_FPU_CTRL_REGNUM)
120 #define GETFPXREGS_SUPPLIES(regno) \
121 (FP0_REGNUM <= (regno) && (regno) <= MXCSR_REGNUM)
123 /* Does the current host support the GETREGS request? */
124 int have_ptrace_getregs =
125 #ifdef HAVE_PTRACE_GETREGS
132 /* Does the current host support the GETFPXREGS request? The header
133 file may or may not define it, and even if it is defined, the
134 kernel will return EIO if it's running on a pre-SSE processor.
136 My instinct is to attach this to some architecture- or
137 target-specific data structure, but really, a particular GDB
138 process can only run on top of one kernel at a time. So it's okay
139 for this to be a simple variable. */
140 int have_ptrace_getfpxregs =
141 #ifdef HAVE_PTRACE_GETFPXREGS
149 /* Support for the user struct. */
151 /* Return the address of register REGNUM. BLOCKEND is the value of
152 u.u_ar0, which should point to the registers. */
155 register_u_addr (CORE_ADDR blockend, int regnum)
157 return (blockend + 4 * regmap[regnum]);
160 /* Return the size of the user struct. */
165 return (sizeof (struct user));
169 /* Accessing registers through the U area, one at a time. */
171 /* Fetch one register. */
174 fetch_register (int regno)
179 gdb_assert (!have_ptrace_getregs);
180 if (cannot_fetch_register (regno))
182 supply_register (regno, NULL);
186 /* GNU/Linux LWP ID's are process ID's. */
187 tid = TIDGET (inferior_ptid);
189 tid = PIDGET (inferior_ptid); /* Not a threaded program. */
192 val = ptrace (PTRACE_PEEKUSER, tid, register_addr (regno, 0), 0);
194 error ("Couldn't read register %s (#%d): %s.", REGISTER_NAME (regno),
195 regno, safe_strerror (errno));
197 supply_register (regno, &val);
200 /* Store one register. */
203 store_register (int regno)
208 gdb_assert (!have_ptrace_getregs);
209 if (cannot_store_register (regno))
212 /* GNU/Linux LWP ID's are process ID's. */
213 tid = TIDGET (inferior_ptid);
215 tid = PIDGET (inferior_ptid); /* Not a threaded program. */
218 regcache_collect (regno, &val);
219 ptrace (PTRACE_POKEUSER, tid, register_addr (regno, 0), val);
221 error ("Couldn't write register %s (#%d): %s.", REGISTER_NAME (regno),
222 regno, safe_strerror (errno));
226 /* Transfering the general-purpose registers between GDB, inferiors
229 /* Fill GDB's register array with the general-purpose register values
233 supply_gregset (elf_gregset_t *gregsetp)
235 elf_greg_t *regp = (elf_greg_t *) gregsetp;
238 for (i = 0; i < I386_NUM_GREGS; i++)
239 supply_register (i, regp + regmap[i]);
241 if (I386_LINUX_ORIG_EAX_REGNUM < NUM_REGS)
242 supply_register (I386_LINUX_ORIG_EAX_REGNUM, regp + ORIG_EAX);
245 /* Fill register REGNO (if it is a general-purpose register) in
246 *GREGSETPS with the value in GDB's register array. If REGNO is -1,
247 do this for all registers. */
250 fill_gregset (elf_gregset_t *gregsetp, int regno)
252 elf_greg_t *regp = (elf_greg_t *) gregsetp;
255 for (i = 0; i < I386_NUM_GREGS; i++)
256 if (regno == -1 || regno == i)
257 regcache_collect (i, regp + regmap[i]);
259 if ((regno == -1 || regno == I386_LINUX_ORIG_EAX_REGNUM)
260 && I386_LINUX_ORIG_EAX_REGNUM < NUM_REGS)
261 regcache_collect (I386_LINUX_ORIG_EAX_REGNUM, regp + ORIG_EAX);
264 #ifdef HAVE_PTRACE_GETREGS
266 /* Fetch all general-purpose registers from process/thread TID and
267 store their values in GDB's register array. */
274 if (ptrace (PTRACE_GETREGS, tid, 0, (int) ®s) < 0)
278 /* The kernel we're running on doesn't support the GETREGS
279 request. Reset `have_ptrace_getregs'. */
280 have_ptrace_getregs = 0;
284 perror_with_name ("Couldn't get registers");
287 supply_gregset (®s);
290 /* Store all valid general-purpose registers in GDB's register array
291 into the process/thread specified by TID. */
294 store_regs (int tid, int regno)
298 if (ptrace (PTRACE_GETREGS, tid, 0, (int) ®s) < 0)
299 perror_with_name ("Couldn't get registers");
301 fill_gregset (®s, regno);
303 if (ptrace (PTRACE_SETREGS, tid, 0, (int) ®s) < 0)
304 perror_with_name ("Couldn't write registers");
309 static void fetch_regs (int tid) {}
310 static void store_regs (int tid, int regno) {}
315 /* Transfering floating-point registers between GDB, inferiors and cores. */
317 /* Fill GDB's register array with the floating-point register values in
321 supply_fpregset (elf_fpregset_t *fpregsetp)
323 i387_supply_fsave ((char *) fpregsetp);
327 /* Fill register REGNO (if it is a floating-point register) in
328 *FPREGSETP with the value in GDB's register array. If REGNO is -1,
329 do this for all registers. */
332 fill_fpregset (elf_fpregset_t *fpregsetp, int regno)
334 i387_fill_fsave ((char *) fpregsetp, regno);
337 #ifdef HAVE_PTRACE_GETREGS
339 /* Fetch all floating-point registers from process/thread TID and store
340 thier values in GDB's register array. */
343 fetch_fpregs (int tid)
345 elf_fpregset_t fpregs;
347 if (ptrace (PTRACE_GETFPREGS, tid, 0, (int) &fpregs) < 0)
348 perror_with_name ("Couldn't get floating point status");
350 supply_fpregset (&fpregs);
353 /* Store all valid floating-point registers in GDB's register array
354 into the process/thread specified by TID. */
357 store_fpregs (int tid, int regno)
359 elf_fpregset_t fpregs;
361 if (ptrace (PTRACE_GETFPREGS, tid, 0, (int) &fpregs) < 0)
362 perror_with_name ("Couldn't get floating point status");
364 fill_fpregset (&fpregs, regno);
366 if (ptrace (PTRACE_SETFPREGS, tid, 0, (int) &fpregs) < 0)
367 perror_with_name ("Couldn't write floating point status");
372 static void fetch_fpregs (int tid) {}
373 static void store_fpregs (int tid, int regno) {}
378 /* Transfering floating-point and SSE registers to and from GDB. */
380 #ifdef HAVE_PTRACE_GETFPXREGS
382 /* Fill GDB's register array with the floating-point and SSE register
383 values in *FPXREGSETP. */
386 supply_fpxregset (elf_fpxregset_t *fpxregsetp)
388 i387_supply_fxsave ((char *) fpxregsetp);
391 /* Fill register REGNO (if it is a floating-point or SSE register) in
392 *FPXREGSETP with the value in GDB's register array. If REGNO is
393 -1, do this for all registers. */
396 fill_fpxregset (elf_fpxregset_t *fpxregsetp, int regno)
398 i387_fill_fxsave ((char *) fpxregsetp, regno);
401 /* Fetch all registers covered by the PTRACE_GETFPXREGS request from
402 process/thread TID and store their values in GDB's register array.
403 Return non-zero if successful, zero otherwise. */
406 fetch_fpxregs (int tid)
408 elf_fpxregset_t fpxregs;
410 if (! have_ptrace_getfpxregs)
413 if (ptrace (PTRACE_GETFPXREGS, tid, 0, (int) &fpxregs) < 0)
417 have_ptrace_getfpxregs = 0;
421 perror_with_name ("Couldn't read floating-point and SSE registers");
424 supply_fpxregset (&fpxregs);
428 /* Store all valid registers in GDB's register array covered by the
429 PTRACE_SETFPXREGS request into the process/thread specified by TID.
430 Return non-zero if successful, zero otherwise. */
433 store_fpxregs (int tid, int regno)
435 elf_fpxregset_t fpxregs;
437 if (! have_ptrace_getfpxregs)
440 if (ptrace (PTRACE_GETFPXREGS, tid, 0, &fpxregs) == -1)
444 have_ptrace_getfpxregs = 0;
448 perror_with_name ("Couldn't read floating-point and SSE registers");
451 fill_fpxregset (&fpxregs, regno);
453 if (ptrace (PTRACE_SETFPXREGS, tid, 0, &fpxregs) == -1)
454 perror_with_name ("Couldn't write floating-point and SSE registers");
459 /* Fill the XMM registers in the register array with dummy values. For
460 cases where we don't have access to the XMM registers. I think
461 this is cleaner than printing a warning. For a cleaner solution,
462 we should gdbarchify the i386 family. */
465 dummy_sse_values (void)
467 struct gdbarch_tdep *tdep = gdbarch_tdep (current_gdbarch);
468 /* C doesn't have a syntax for NaN's, so write it out as an array of
470 static long dummy[4] = { 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff };
471 static long mxcsr = 0x1f80;
474 for (reg = 0; reg < tdep->num_xmm_regs; reg++)
475 supply_register (XMM0_REGNUM + reg, (char *) dummy);
476 if (tdep->num_xmm_regs > 0)
477 supply_register (MXCSR_REGNUM, (char *) &mxcsr);
482 static int fetch_fpxregs (int tid) { return 0; }
483 static int store_fpxregs (int tid, int regno) { return 0; }
484 static void dummy_sse_values (void) {}
486 #endif /* HAVE_PTRACE_GETFPXREGS */
489 /* Transferring arbitrary registers between GDB and inferior. */
491 /* Check if register REGNO in the child process is accessible.
492 If we are accessing registers directly via the U area, only the
493 general-purpose registers are available.
494 All registers should be accessible if we have GETREGS support. */
497 cannot_fetch_register (int regno)
499 gdb_assert (regno >= 0 && regno < NUM_REGS);
500 return (!have_ptrace_getregs && regmap[regno] == -1);
504 cannot_store_register (int regno)
506 gdb_assert (regno >= 0 && regno < NUM_REGS);
507 return (!have_ptrace_getregs && regmap[regno] == -1);
510 /* Fetch register REGNO from the child process. If REGNO is -1, do
511 this for all registers (including the floating point and SSE
515 fetch_inferior_registers (int regno)
519 /* Use the old method of peeking around in `struct user' if the
520 GETREGS request isn't available. */
521 if (!have_ptrace_getregs)
525 for (i = 0; i < NUM_REGS; i++)
526 if (regno == -1 || regno == i)
532 /* GNU/Linux LWP ID's are process ID's. */
533 tid = TIDGET (inferior_ptid);
535 tid = PIDGET (inferior_ptid); /* Not a threaded program. */
537 /* Use the PTRACE_GETFPXREGS request whenever possible, since it
538 transfers more registers in one system call, and we'll cache the
539 results. But remember that fetch_fpxregs can fail, and return
545 /* The call above might reset `have_ptrace_getregs'. */
546 if (!have_ptrace_getregs)
548 fetch_inferior_registers (regno);
552 if (fetch_fpxregs (tid))
558 if (GETREGS_SUPPLIES (regno))
564 if (GETFPXREGS_SUPPLIES (regno))
566 if (fetch_fpxregs (tid))
569 /* Either our processor or our kernel doesn't support the SSE
570 registers, so read the FP registers in the traditional way,
571 and fill the SSE registers with dummy values. It would be
572 more graceful to handle differences in the register set using
573 gdbarch. Until then, this will at least make things work
579 internal_error (__FILE__, __LINE__,
580 "Got request for bad register number %d.", regno);
583 /* Store register REGNO back into the child process. If REGNO is -1,
584 do this for all registers (including the floating point and SSE
587 store_inferior_registers (int regno)
591 /* Use the old method of poking around in `struct user' if the
592 SETREGS request isn't available. */
593 if (!have_ptrace_getregs)
597 for (i = 0; i < NUM_REGS; i++)
598 if (regno == -1 || regno == i)
604 /* GNU/Linux LWP ID's are process ID's. */
605 tid = TIDGET (inferior_ptid);
607 tid = PIDGET (inferior_ptid); /* Not a threaded program. */
609 /* Use the PTRACE_SETFPXREGS requests whenever possible, since it
610 transfers more registers in one system call. But remember that
611 store_fpxregs can fail, and return zero. */
614 store_regs (tid, regno);
615 if (store_fpxregs (tid, regno))
617 store_fpregs (tid, regno);
621 if (GETREGS_SUPPLIES (regno))
623 store_regs (tid, regno);
627 if (GETFPXREGS_SUPPLIES (regno))
629 if (store_fpxregs (tid, regno))
632 /* Either our processor or our kernel doesn't support the SSE
633 registers, so just write the FP registers in the traditional
635 store_fpregs (tid, regno);
639 internal_error (__FILE__, __LINE__,
640 "Got request to store bad register number %d.", regno);
645 i386_linux_dr_get (int regnum)
650 /* FIXME: kettenis/2001-01-29: It's not clear what we should do with
651 multi-threaded processes here. For now, pretend there is just
653 tid = PIDGET (inferior_ptid);
655 /* FIXME: kettenis/2001-03-27: Calling perror_with_name if the
656 ptrace call fails breaks debugging remote targets. The correct
657 way to fix this is to add the hardware breakpoint and watchpoint
658 stuff to the target vectore. For now, just return zero if the
659 ptrace call fails. */
661 value = ptrace (PTRACE_PEEKUSER, tid,
662 offsetof (struct user, u_debugreg[regnum]), 0);
665 perror_with_name ("Couldn't read debug register");
674 i386_linux_dr_set (int regnum, unsigned long value)
678 /* FIXME: kettenis/2001-01-29: It's not clear what we should do with
679 multi-threaded processes here. For now, pretend there is just
681 tid = PIDGET (inferior_ptid);
684 ptrace (PTRACE_POKEUSER, tid,
685 offsetof (struct user, u_debugreg[regnum]), value);
687 perror_with_name ("Couldn't write debug register");
691 ps_get_thread_area(const struct ps_prochandle *ph,
692 lwpid_t lwpid, int idx, void **base)
694 unsigned long int desc[3];
695 #define PTRACE_GET_THREAD_AREA 25
697 if (ptrace (PTRACE_GET_THREAD_AREA,
698 lwpid, (void *) idx, (unsigned long) &desc) < 0)
701 *(int *)base = desc[1];
706 i386_linux_dr_set_control (unsigned long control)
708 i386_linux_dr_set (DR_CONTROL, control);
712 i386_linux_dr_set_addr (int regnum, CORE_ADDR addr)
714 gdb_assert (regnum >= 0 && regnum <= DR_LASTADDR - DR_FIRSTADDR);
716 i386_linux_dr_set (DR_FIRSTADDR + regnum, addr);
720 i386_linux_dr_reset_addr (int regnum)
722 gdb_assert (regnum >= 0 && regnum <= DR_LASTADDR - DR_FIRSTADDR);
724 i386_linux_dr_set (DR_FIRSTADDR + regnum, 0L);
728 i386_linux_dr_get_status (void)
730 return i386_linux_dr_get (DR_STATUS);
734 /* Interpreting register set info found in core files. */
736 /* Provide registers to GDB from a core file.
738 (We can't use the generic version of this function in
739 core-regset.c, because GNU/Linux has *three* different kinds of
740 register set notes. core-regset.c would have to call
741 supply_fpxregset, which most platforms don't have.)
743 CORE_REG_SECT points to an array of bytes, which are the contents
744 of a `note' from a core file which BFD thinks might contain
745 register contents. CORE_REG_SIZE is its size.
747 WHICH says which register set corelow suspects this is:
748 0 --- the general-purpose register set, in elf_gregset_t format
749 2 --- the floating-point register set, in elf_fpregset_t format
750 3 --- the extended floating-point register set, in elf_fpxregset_t format
752 REG_ADDR isn't used on GNU/Linux. */
755 fetch_core_registers (char *core_reg_sect, unsigned core_reg_size,
756 int which, CORE_ADDR reg_addr)
758 elf_gregset_t gregset;
759 elf_fpregset_t fpregset;
764 if (core_reg_size != sizeof (gregset))
765 warning ("Wrong size gregset in core file.");
768 memcpy (&gregset, core_reg_sect, sizeof (gregset));
769 supply_gregset (&gregset);
774 if (core_reg_size != sizeof (fpregset))
775 warning ("Wrong size fpregset in core file.");
778 memcpy (&fpregset, core_reg_sect, sizeof (fpregset));
779 supply_fpregset (&fpregset);
783 #ifdef HAVE_PTRACE_GETFPXREGS
785 elf_fpxregset_t fpxregset;
788 if (core_reg_size != sizeof (fpxregset))
789 warning ("Wrong size fpxregset in core file.");
792 memcpy (&fpxregset, core_reg_sect, sizeof (fpxregset));
793 supply_fpxregset (&fpxregset);
800 /* We've covered all the kinds of registers we know about here,
801 so this must be something we wouldn't know what to do with
802 anyway. Just ignore it. */
808 /* The instruction for a GNU/Linux system call is:
812 static const unsigned char linux_syscall[] = { 0xcd, 0x80 };
814 #define LINUX_SYSCALL_LEN (sizeof linux_syscall)
816 /* The system call number is stored in the %eax register. */
817 #define LINUX_SYSCALL_REGNUM 0 /* %eax */
819 /* We are specifically interested in the sigreturn and rt_sigreturn
822 #ifndef SYS_sigreturn
823 #define SYS_sigreturn 0x77
825 #ifndef SYS_rt_sigreturn
826 #define SYS_rt_sigreturn 0xad
829 /* Offset to saved processor flags, from <asm/sigcontext.h>. */
830 #define LINUX_SIGCONTEXT_EFLAGS_OFFSET (64)
832 /* Resume execution of the inferior process.
833 If STEP is nonzero, single-step it.
834 If SIGNAL is nonzero, give it that signal. */
837 child_resume (ptid_t ptid, int step, enum target_signal signal)
839 int pid = PIDGET (ptid);
841 int request = PTRACE_CONT;
844 /* Resume all threads. */
845 /* I think this only gets used in the non-threaded case, where "resume
846 all threads" and "resume inferior_ptid" are the same. */
847 pid = PIDGET (inferior_ptid);
851 CORE_ADDR pc = read_pc_pid (pid_to_ptid (pid));
852 unsigned char buf[LINUX_SYSCALL_LEN];
854 request = PTRACE_SINGLESTEP;
856 /* Returning from a signal trampoline is done by calling a
857 special system call (sigreturn or rt_sigreturn, see
858 i386-linux-tdep.c for more information). This system call
859 restores the registers that were saved when the signal was
860 raised, including %eflags. That means that single-stepping
861 won't work. Instead, we'll have to modify the signal context
862 that's about to be restored, and set the trace flag there. */
864 /* First check if PC is at a system call. */
865 if (read_memory_nobpt (pc, (char *) buf, LINUX_SYSCALL_LEN) == 0
866 && memcmp (buf, linux_syscall, LINUX_SYSCALL_LEN) == 0)
868 int syscall = read_register_pid (LINUX_SYSCALL_REGNUM,
871 /* Then check the system call number. */
872 if (syscall == SYS_sigreturn || syscall == SYS_rt_sigreturn)
874 CORE_ADDR sp = read_register (I386_ESP_REGNUM);
876 unsigned long int eflags;
878 if (syscall == SYS_rt_sigreturn)
879 addr = read_memory_integer (sp + 8, 4) + 20;
881 /* Set the trace flag in the context that's about to be
883 addr += LINUX_SIGCONTEXT_EFLAGS_OFFSET;
884 read_memory (addr, (char *) &eflags, 4);
886 write_memory (addr, (char *) &eflags, 4);
891 if (ptrace (request, pid, 0, target_signal_to_host (signal)) == -1)
892 perror_with_name ("ptrace");
896 child_post_startup_inferior (ptid_t ptid)
898 i386_cleanup_dregs ();
899 linux_child_post_startup_inferior (ptid);
903 /* Register that we are able to handle GNU/Linux ELF core file
906 static struct core_fns linux_elf_core_fns =
908 bfd_target_elf_flavour, /* core_flavour */
909 default_check_format, /* check_format */
910 default_core_sniffer, /* core_sniffer */
911 fetch_core_registers, /* core_read_registers */
916 _initialize_i386_linux_nat (void)
918 add_core_fns (&linux_elf_core_fns);