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 <sys/ptrace.h>
28 #include <sys/procfs.h>
34 /* Prototypes for supply_gregset etc. */
37 /* Prototypes for i387_supply_fsave etc. */
40 /* Prototypes for local functions. */
41 static void dummy_sse_values (void);
43 /* On Linux, threads are implemented as pseudo-processes, in which
44 case we may be tracing more than one process at a time. In that
45 case, inferior_pid will contain the main process ID and the
46 individual thread (process) ID mashed together. These macros are
47 used to separate them out. These definitions should be overridden
48 if thread support is included. */
50 #if !defined (PIDGET) /* Default definition for PIDGET/TIDGET. */
51 #define PIDGET(PID) PID
56 /* The register sets used in Linux ELF core-dumps are identical to the
57 register sets in `struct user' that is used for a.out core-dumps,
58 and is also used by `ptrace'. The corresponding types are
59 `elf_gregset_t' for the general-purpose registers (with
60 `elf_greg_t' the type of a single GP register) and `elf_fpregset_t'
61 for the floating-point registers.
63 Those types used to be available under the names `gregset_t' and
64 `fpregset_t' too, and this file used those names in the past. But
65 those names are now used for the register sets used in the
66 `mcontext_t' type, and have a different size and layout. */
68 /* Mapping between the general-purpose registers in `struct user'
69 format and GDB's register array layout. */
78 /* Which ptrace request retrieves which registers?
79 These apply to the corresponding SET requests as well. */
80 #define GETREGS_SUPPLIES(regno) \
81 (0 <= (regno) && (regno) <= 15)
82 #define GETFPREGS_SUPPLIES(regno) \
83 (FP0_REGNUM <= (regno) && (regno) <= LAST_FPU_CTRL_REGNUM)
84 #define GETFPXREGS_SUPPLIES(regno) \
85 (FP0_REGNUM <= (regno) && (regno) <= MXCSR_REGNUM)
87 /* Does the current host support the GETREGS request? */
88 int have_ptrace_getregs
=
89 #ifdef HAVE_PTRACE_GETREGS
96 /* Does the current host support the GETFPXREGS request? The header
97 file may or may not define it, and even if it is defined, the
98 kernel will return EIO if it's running on a pre-SSE processor.
100 My instinct is to attach this to some architecture- or
101 target-specific data structure, but really, a particular GDB
102 process can only run on top of one kernel at a time. So it's okay
103 for this to be a simple variable. */
104 int have_ptrace_getfpxregs
=
105 #ifdef HAVE_PTRACE_GETFPXREGS
113 /* Fetching registers directly from the U area, one at a time. */
115 /* FIXME: kettenis/2000-03-05: This duplicates code from `inptrace.c'.
116 The problem is that we define FETCH_INFERIOR_REGISTERS since we
117 want to use our own versions of {fetch,store}_inferior_registers
118 that use the GETREGS request. This means that the code in
119 `infptrace.c' is #ifdef'd out. But we need to fall back on that
120 code when GDB is running on top of a kernel that doesn't support
121 the GETREGS request. I want to avoid changing `infptrace.c' right
125 #define PT_READ_U PTRACE_PEEKUSR
128 #define PT_WRITE_U PTRACE_POKEUSR
131 /* Default the type of the ptrace transfer to int. */
132 #ifndef PTRACE_XFER_TYPE
133 #define PTRACE_XFER_TYPE int
136 /* Registers we shouldn't try to fetch. */
137 #define OLD_CANNOT_FETCH_REGISTER(regno) ((regno) >= NUM_GREGS)
139 /* Fetch one register. */
142 fetch_register (int regno
)
144 /* This isn't really an address. But ptrace thinks of it as one. */
146 char mess
[128]; /* For messages */
148 unsigned int offset
; /* Offset of registers within the u area. */
149 char buf
[MAX_REGISTER_RAW_SIZE
];
152 if (OLD_CANNOT_FETCH_REGISTER (regno
))
154 memset (buf
, '\0', REGISTER_RAW_SIZE (regno
)); /* Supply zeroes */
155 supply_register (regno
, buf
);
159 /* Overload thread id onto process id */
160 if ((tid
= TIDGET (inferior_pid
)) == 0)
161 tid
= inferior_pid
; /* no thread id, just use process id */
163 offset
= U_REGS_OFFSET
;
165 regaddr
= register_addr (regno
, offset
);
166 for (i
= 0; i
< REGISTER_RAW_SIZE (regno
); i
+= sizeof (PTRACE_XFER_TYPE
))
169 *(PTRACE_XFER_TYPE
*) & buf
[i
] = ptrace (PT_READ_U
, tid
,
170 (PTRACE_ARG3_TYPE
) regaddr
, 0);
171 regaddr
+= sizeof (PTRACE_XFER_TYPE
);
174 sprintf (mess
, "reading register %s (#%d)",
175 REGISTER_NAME (regno
), regno
);
176 perror_with_name (mess
);
179 supply_register (regno
, buf
);
182 /* Fetch register values from the inferior.
183 If REGNO is negative, do this for all registers.
184 Otherwise, REGNO specifies which register (so we can save time). */
187 old_fetch_inferior_registers (int regno
)
191 fetch_register (regno
);
195 for (regno
= 0; regno
< NUM_REGS
; regno
++)
197 fetch_register (regno
);
202 /* Registers we shouldn't try to store. */
203 #define OLD_CANNOT_STORE_REGISTER(regno) ((regno) >= NUM_GREGS)
205 /* Store one register. */
208 store_register (int regno
)
210 /* This isn't really an address. But ptrace thinks of it as one. */
212 char mess
[128]; /* For messages */
214 unsigned int offset
; /* Offset of registers within the u area. */
217 if (OLD_CANNOT_STORE_REGISTER (regno
))
222 /* Overload thread id onto process id */
223 if ((tid
= TIDGET (inferior_pid
)) == 0)
224 tid
= inferior_pid
; /* no thread id, just use process id */
226 offset
= U_REGS_OFFSET
;
228 regaddr
= register_addr (regno
, offset
);
229 for (i
= 0; i
< REGISTER_RAW_SIZE (regno
); i
+= sizeof (PTRACE_XFER_TYPE
))
232 ptrace (PT_WRITE_U
, tid
, (PTRACE_ARG3_TYPE
) regaddr
,
233 *(PTRACE_XFER_TYPE
*) & registers
[REGISTER_BYTE (regno
) + i
]);
234 regaddr
+= sizeof (PTRACE_XFER_TYPE
);
237 sprintf (mess
, "writing register %s (#%d)",
238 REGISTER_NAME (regno
), regno
);
239 perror_with_name (mess
);
244 /* Store our register values back into the inferior.
245 If REGNO is negative, do this for all registers.
246 Otherwise, REGNO specifies which register (so we can save time). */
249 old_store_inferior_registers (int regno
)
253 store_register (regno
);
257 for (regno
= 0; regno
< NUM_REGS
; regno
++)
259 store_register (regno
);
265 /* Transfering the general-purpose registers between GDB, inferiors
268 /* Fill GDB's register array with the general-purpose register values
272 supply_gregset (elf_gregset_t
*gregsetp
)
274 elf_greg_t
*regp
= (elf_greg_t
*) gregsetp
;
277 for (i
= 0; i
< NUM_GREGS
; i
++)
278 supply_register (i
, (char *) (regp
+ regmap
[i
]));
281 /* Fill register REGNO (if it is a general-purpose register) in
282 *GREGSETPS with the value in GDB's register array. If REGNO is -1,
283 do this for all registers. */
286 fill_gregset (elf_gregset_t
*gregsetp
, int regno
)
288 elf_greg_t
*regp
= (elf_greg_t
*) gregsetp
;
291 for (i
= 0; i
< NUM_GREGS
; i
++)
292 if ((regno
== -1 || regno
== i
))
293 *(regp
+ regmap
[i
]) = *(elf_greg_t
*) ®isters
[REGISTER_BYTE (i
)];
296 #ifdef HAVE_PTRACE_GETREGS
298 /* Fetch all general-purpose registers from process/thread TID and
299 store their values in GDB's register array. */
306 if (ptrace (PTRACE_GETREGS
, tid
, 0, (int) ®s
) < 0)
310 /* The kernel we're running on doesn't support the GETREGS
311 request. Reset `have_ptrace_getregs'. */
312 have_ptrace_getregs
= 0;
316 perror_with_name ("Couldn't get registers");
319 supply_gregset (®s
);
322 /* Store all valid general-purpose registers in GDB's register array
323 into the process/thread specified by TID. */
326 store_regs (int tid
, int regno
)
330 if (ptrace (PTRACE_GETREGS
, tid
, 0, (int) ®s
) < 0)
331 perror_with_name ("Couldn't get registers");
333 fill_gregset (®s
, regno
);
335 if (ptrace (PTRACE_SETREGS
, tid
, 0, (int) ®s
) < 0)
336 perror_with_name ("Couldn't write registers");
341 static void fetch_regs (int tid
) {}
342 static void store_regs (int tid
, int regno
) {}
347 /* Transfering floating-point registers between GDB, inferiors and cores. */
349 /* Fill GDB's register array with the floating-point register values in
353 supply_fpregset (elf_fpregset_t
*fpregsetp
)
355 i387_supply_fsave ((char *) fpregsetp
);
359 /* Fill register REGNO (if it is a floating-point register) in
360 *FPREGSETP with the value in GDB's register array. If REGNO is -1,
361 do this for all registers. */
364 fill_fpregset (elf_fpregset_t
*fpregsetp
, int regno
)
366 i387_fill_fsave ((char *) fpregsetp
, regno
);
369 #ifdef HAVE_PTRACE_GETREGS
371 /* Fetch all floating-point registers from process/thread TID and store
372 thier values in GDB's register array. */
375 fetch_fpregs (int tid
)
377 elf_fpregset_t fpregs
;
379 if (ptrace (PTRACE_GETFPREGS
, tid
, 0, (int) &fpregs
) < 0)
380 perror_with_name ("Couldn't get floating point status");
382 supply_fpregset (&fpregs
);
385 /* Store all valid floating-point registers in GDB's register array
386 into the process/thread specified by TID. */
389 store_fpregs (int tid
, int regno
)
391 elf_fpregset_t fpregs
;
393 if (ptrace (PTRACE_GETFPREGS
, tid
, 0, (int) &fpregs
) < 0)
394 perror_with_name ("Couldn't get floating point status");
396 fill_fpregset (&fpregs
, regno
);
398 if (ptrace (PTRACE_SETFPREGS
, tid
, 0, (int) &fpregs
) < 0)
399 perror_with_name ("Couldn't write floating point status");
404 static void fetch_fpregs (int tid
) {}
405 static void store_fpregs (int tid
, int regno
) {}
410 /* Transfering floating-point and SSE registers to and from GDB. */
412 #ifdef HAVE_PTRACE_GETFPXREGS
414 /* Fill GDB's register array with the floating-point and SSE register
415 values in *FPXREGSETP. */
418 supply_fpxregset (elf_fpxregset_t
*fpxregsetp
)
420 i387_supply_fxsave ((char *) fpxregsetp
);
423 /* Fill register REGNO (if it is a floating-point or SSE register) in
424 *FPXREGSETP with the value in GDB's register array. If REGNO is
425 -1, do this for all registers. */
428 fill_fpxregset (elf_fpxregset_t
*fpxregsetp
, int regno
)
430 i387_fill_fxsave ((char *) fpxregsetp
, regno
);
433 /* Fetch all registers covered by the PTRACE_GETFPXREGS request from
434 process/thread TID and store their values in GDB's register array.
435 Return non-zero if successful, zero otherwise. */
438 fetch_fpxregs (int tid
)
440 elf_fpxregset_t fpxregs
;
442 if (! have_ptrace_getfpxregs
)
445 if (ptrace (PTRACE_GETFPXREGS
, tid
, 0, (int) &fpxregs
) < 0)
449 have_ptrace_getfpxregs
= 0;
453 perror_with_name ("Couldn't read floating-point and SSE registers");
456 supply_fpxregset (&fpxregs
);
460 /* Store all valid registers in GDB's register array covered by the
461 PTRACE_SETFPXREGS request into the process/thread specified by TID.
462 Return non-zero if successful, zero otherwise. */
465 store_fpxregs (int tid
, int regno
)
467 elf_fpxregset_t fpxregs
;
469 if (! have_ptrace_getfpxregs
)
472 if (ptrace (PTRACE_GETFPXREGS
, tid
, 0, &fpxregs
) == -1)
476 have_ptrace_getfpxregs
= 0;
480 perror_with_name ("Couldn't read floating-point and SSE registers");
483 fill_fpxregset (&fpxregs
, regno
);
485 if (ptrace (PTRACE_SETFPXREGS
, tid
, 0, &fpxregs
) == -1)
486 perror_with_name ("Couldn't write floating-point and SSE registers");
491 /* Fill the XMM registers in the register array with dummy values. For
492 cases where we don't have access to the XMM registers. I think
493 this is cleaner than printing a warning. For a cleaner solution,
494 we should gdbarchify the i386 family. */
497 dummy_sse_values (void)
499 /* C doesn't have a syntax for NaN's, so write it out as an array of
501 static long dummy
[4] = { 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff };
502 static long mxcsr
= 0x1f80;
505 for (reg
= 0; reg
< 8; reg
++)
506 supply_register (XMM0_REGNUM
+ reg
, (char *) dummy
);
507 supply_register (MXCSR_REGNUM
, (char *) &mxcsr
);
512 static int fetch_fpxregs (int tid
) { return 0; }
513 static int store_fpxregs (int tid
, int regno
) { return 0; }
514 static void dummy_sse_values (void) {}
516 #endif /* HAVE_PTRACE_GETFPXREGS */
519 /* Transferring arbitrary registers between GDB and inferior. */
521 /* Check if register REGNO in the child process is accessible.
522 If we are accessing registers directly via the U area, only the
523 general-purpose registers are available.
524 All registers should be accessible if we have GETREGS support. */
527 cannot_fetch_register (int regno
)
529 if (! have_ptrace_getregs
)
530 return OLD_CANNOT_FETCH_REGISTER (regno
);
534 cannot_store_register (int regno
)
536 if (! have_ptrace_getregs
)
537 return OLD_CANNOT_STORE_REGISTER (regno
);
541 /* Fetch register REGNO from the child process. If REGNO is -1, do
542 this for all registers (including the floating point and SSE
546 fetch_inferior_registers (int regno
)
550 /* Use the old method of peeking around in `struct user' if the
551 GETREGS request isn't available. */
552 if (! have_ptrace_getregs
)
554 old_fetch_inferior_registers (regno
);
558 /* Linux LWP ID's are process ID's. */
559 if ((tid
= TIDGET (inferior_pid
)) == 0)
560 tid
= inferior_pid
; /* Not a threaded program. */
562 /* Use the PTRACE_GETFPXREGS request whenever possible, since it
563 transfers more registers in one system call, and we'll cache the
564 results. But remember that fetch_fpxregs can fail, and return
570 /* The call above might reset `have_ptrace_getregs'. */
571 if (! have_ptrace_getregs
)
573 old_fetch_inferior_registers (-1);
577 if (fetch_fpxregs (tid
))
583 if (GETREGS_SUPPLIES (regno
))
589 if (GETFPXREGS_SUPPLIES (regno
))
591 if (fetch_fpxregs (tid
))
594 /* Either our processor or our kernel doesn't support the SSE
595 registers, so read the FP registers in the traditional way,
596 and fill the SSE registers with dummy values. It would be
597 more graceful to handle differences in the register set using
598 gdbarch. Until then, this will at least make things work
604 internal_error (__FILE__
, __LINE__
,
605 "Got request for bad register number %d.", regno
);
608 /* Store register REGNO back into the child process. If REGNO is -1,
609 do this for all registers (including the floating point and SSE
612 store_inferior_registers (int regno
)
616 /* Use the old method of poking around in `struct user' if the
617 SETREGS request isn't available. */
618 if (! have_ptrace_getregs
)
620 old_store_inferior_registers (regno
);
624 /* Linux LWP ID's are process ID's. */
625 if ((tid
= TIDGET (inferior_pid
)) == 0)
626 tid
= inferior_pid
; /* Not a threaded program. */
628 /* Use the PTRACE_SETFPXREGS requests whenever possible, since it
629 transfers more registers in one system call. But remember that
630 store_fpxregs can fail, and return zero. */
633 store_regs (tid
, regno
);
634 if (store_fpxregs (tid
, regno
))
636 store_fpregs (tid
, regno
);
640 if (GETREGS_SUPPLIES (regno
))
642 store_regs (tid
, regno
);
646 if (GETFPXREGS_SUPPLIES (regno
))
648 if (store_fpxregs (tid
, regno
))
651 /* Either our processor or our kernel doesn't support the SSE
652 registers, so just write the FP registers in the traditional
654 store_fpregs (tid
, regno
);
658 internal_error (__FILE__
, __LINE__
,
659 "Got request to store bad register number %d.", regno
);
663 /* Interpreting register set info found in core files. */
665 /* Provide registers to GDB from a core file.
667 (We can't use the generic version of this function in
668 core-regset.c, because Linux has *three* different kinds of
669 register set notes. core-regset.c would have to call
670 supply_fpxregset, which most platforms don't have.)
672 CORE_REG_SECT points to an array of bytes, which are the contents
673 of a `note' from a core file which BFD thinks might contain
674 register contents. CORE_REG_SIZE is its size.
676 WHICH says which register set corelow suspects this is:
677 0 --- the general-purpose register set, in elf_gregset_t format
678 2 --- the floating-point register set, in elf_fpregset_t format
679 3 --- the extended floating-point register set, in elf_fpxregset_t format
681 REG_ADDR isn't used on Linux. */
684 fetch_core_registers (char *core_reg_sect
, unsigned core_reg_size
,
685 int which
, CORE_ADDR reg_addr
)
687 elf_gregset_t gregset
;
688 elf_fpregset_t fpregset
;
693 if (core_reg_size
!= sizeof (gregset
))
694 warning ("Wrong size gregset in core file.");
697 memcpy (&gregset
, core_reg_sect
, sizeof (gregset
));
698 supply_gregset (&gregset
);
703 if (core_reg_size
!= sizeof (fpregset
))
704 warning ("Wrong size fpregset in core file.");
707 memcpy (&fpregset
, core_reg_sect
, sizeof (fpregset
));
708 supply_fpregset (&fpregset
);
712 #ifdef HAVE_PTRACE_GETFPXREGS
714 elf_fpxregset_t fpxregset
;
717 if (core_reg_size
!= sizeof (fpxregset
))
718 warning ("Wrong size fpxregset in core file.");
721 memcpy (&fpxregset
, core_reg_sect
, sizeof (fpxregset
));
722 supply_fpxregset (&fpxregset
);
729 /* We've covered all the kinds of registers we know about here,
730 so this must be something we wouldn't know what to do with
731 anyway. Just ignore it. */
737 /* The instruction for a Linux system call is:
741 static const unsigned char linux_syscall
[] = { 0xcd, 0x80 };
743 #define LINUX_SYSCALL_LEN (sizeof linux_syscall)
745 /* The system call number is stored in the %eax register. */
746 #define LINUX_SYSCALL_REGNUM 0 /* %eax */
748 /* We are specifically interested in the sigreturn and rt_sigreturn
751 #ifndef SYS_sigreturn
752 #define SYS_sigreturn 0x77
754 #ifndef SYS_rt_sigreturn
755 #define SYS_rt_sigreturn 0xad
758 /* Offset to saved processor flags, from <asm/sigcontext.h>. */
759 #define LINUX_SIGCONTEXT_EFLAGS_OFFSET (64)
761 /* Resume execution of the inferior process.
762 If STEP is nonzero, single-step it.
763 If SIGNAL is nonzero, give it that signal. */
766 child_resume (int pid
, int step
, enum target_signal signal
)
768 int request
= PTRACE_CONT
;
771 /* Resume all threads. */
772 /* I think this only gets used in the non-threaded case, where "resume
773 all threads" and "resume inferior_pid" are the same. */
778 CORE_ADDR pc
= read_pc_pid (pid
);
779 unsigned char buf
[LINUX_SYSCALL_LEN
];
781 request
= PTRACE_SINGLESTEP
;
783 /* Returning from a signal trampoline is done by calling a
784 special system call (sigreturn or rt_sigreturn, see
785 i386-linux-tdep.c for more information). This system call
786 restores the registers that were saved when the signal was
787 raised, including %eflags. That means that single-stepping
788 won't work. Instead, we'll have to modify the signal context
789 that's about to be restored, and set the trace flag there. */
791 /* First check if PC is at a system call. */
792 if (read_memory_nobpt (pc
, (char *) buf
, LINUX_SYSCALL_LEN
) == 0
793 && memcmp (buf
, linux_syscall
, LINUX_SYSCALL_LEN
) == 0)
795 int syscall
= read_register_pid (LINUX_SYSCALL_REGNUM
, pid
);
797 /* Then check the system call number. */
798 if (syscall
== SYS_sigreturn
|| syscall
== SYS_rt_sigreturn
)
800 CORE_ADDR sp
= read_register (SP_REGNUM
);
802 unsigned long int eflags
;
804 if (syscall
== SYS_rt_sigreturn
)
805 addr
= read_memory_integer (sp
+ 8, 4) + 20;
807 /* Set the trace flag in the context that's about to be
809 addr
+= LINUX_SIGCONTEXT_EFLAGS_OFFSET
;
810 read_memory (addr
, (char *) &eflags
, 4);
812 write_memory (addr
, (char *) &eflags
, 4);
817 if (ptrace (request
, pid
, 0, target_signal_to_host (signal
)) == -1)
818 perror_with_name ("ptrace");
822 /* Register that we are able to handle Linux ELF core file formats. */
824 static struct core_fns linux_elf_core_fns
=
826 bfd_target_elf_flavour
, /* core_flavour */
827 default_check_format
, /* check_format */
828 default_core_sniffer
, /* core_sniffer */
829 fetch_core_registers
, /* core_read_registers */
834 _initialize_i386_linux_nat (void)
836 add_core_fns (&linux_elf_core_fns
);