1 /* Native-dependent code for Linux/x86.
2 Copyright 1999, 2000 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. */
25 #include <sys/ptrace.h>
27 #include <sys/procfs.h>
33 /* Prototypes for supply_gregset etc. */
36 /* Prototypes for i387_supply_fsave etc. */
39 /* Prototypes for local functions. */
40 static void dummy_sse_values (void);
42 /* On Linux, threads are implemented as pseudo-processes, in which
43 case we may be tracing more than one process at a time. In that
44 case, inferior_pid will contain the main process ID and the
45 individual thread (process) ID mashed together. These macros are
46 used to separate them out. These definitions should be overridden
47 if thread support is included. */
49 #if !defined (PIDGET) /* Default definition for PIDGET/TIDGET. */
50 #define PIDGET(PID) PID
55 /* The register sets used in Linux ELF core-dumps are identical to the
56 register sets in `struct user' that is used for a.out core-dumps,
57 and is also used by `ptrace'. The corresponding types are
58 `elf_gregset_t' for the general-purpose registers (with
59 `elf_greg_t' the type of a single GP register) and `elf_fpregset_t'
60 for the floating-point registers.
62 Those types used to be available under the names `gregset_t' and
63 `fpregset_t' too, and this file used those names in the past. But
64 those names are now used for the register sets used in the
65 `mcontext_t' type, and have a different size and layout. */
67 /* Mapping between the general-purpose registers in `struct user'
68 format and GDB's register array layout. */
77 /* Which ptrace request retrieves which registers?
78 These apply to the corresponding SET requests as well. */
79 #define GETREGS_SUPPLIES(regno) \
80 (0 <= (regno) && (regno) <= 15)
81 #define GETFPREGS_SUPPLIES(regno) \
82 (FP0_REGNUM <= (regno) && (regno) <= LAST_FPU_CTRL_REGNUM)
83 #define GETFPXREGS_SUPPLIES(regno) \
84 (FP0_REGNUM <= (regno) && (regno) <= MXCSR_REGNUM)
86 /* Does the current host support the GETREGS request? */
87 int have_ptrace_getregs
=
88 #ifdef HAVE_PTRACE_GETREGS
95 /* Does the current host support the GETFPXREGS request? The header
96 file may or may not define it, and even if it is defined, the
97 kernel will return EIO if it's running on a pre-SSE processor.
99 My instinct is to attach this to some architecture- or
100 target-specific data structure, but really, a particular GDB
101 process can only run on top of one kernel at a time. So it's okay
102 for this to be a simple variable. */
103 int have_ptrace_getfpxregs
=
104 #ifdef HAVE_PTRACE_GETFPXREGS
112 /* Fetching registers directly from the U area, one at a time. */
114 /* FIXME: kettenis/2000-03-05: This duplicates code from `inptrace.c'.
115 The problem is that we define FETCH_INFERIOR_REGISTERS since we
116 want to use our own versions of {fetch,store}_inferior_registers
117 that use the GETREGS request. This means that the code in
118 `infptrace.c' is #ifdef'd out. But we need to fall back on that
119 code when GDB is running on top of a kernel that doesn't support
120 the GETREGS request. I want to avoid changing `infptrace.c' right
124 #define PT_READ_U PTRACE_PEEKUSR
127 #define PT_WRITE_U PTRACE_POKEUSR
130 /* Default the type of the ptrace transfer to int. */
131 #ifndef PTRACE_XFER_TYPE
132 #define PTRACE_XFER_TYPE int
135 /* Registers we shouldn't try to fetch. */
136 #if !defined (CANNOT_FETCH_REGISTER)
137 #define CANNOT_FETCH_REGISTER(regno) 0
140 /* Fetch one register. */
143 fetch_register (int regno
)
145 /* This isn't really an address. But ptrace thinks of it as one. */
147 char mess
[128]; /* For messages */
149 unsigned int offset
; /* Offset of registers within the u area. */
150 char buf
[MAX_REGISTER_RAW_SIZE
];
153 if (CANNOT_FETCH_REGISTER (regno
))
155 memset (buf
, '\0', REGISTER_RAW_SIZE (regno
)); /* Supply zeroes */
156 supply_register (regno
, buf
);
160 /* Overload thread id onto process id */
161 if ((tid
= TIDGET (inferior_pid
)) == 0)
162 tid
= inferior_pid
; /* no thread id, just use process id */
164 offset
= U_REGS_OFFSET
;
166 regaddr
= register_addr (regno
, offset
);
167 for (i
= 0; i
< REGISTER_RAW_SIZE (regno
); i
+= sizeof (PTRACE_XFER_TYPE
))
170 *(PTRACE_XFER_TYPE
*) & buf
[i
] = ptrace (PT_READ_U
, tid
,
171 (PTRACE_ARG3_TYPE
) regaddr
, 0);
172 regaddr
+= sizeof (PTRACE_XFER_TYPE
);
175 sprintf (mess
, "reading register %s (#%d)",
176 REGISTER_NAME (regno
), regno
);
177 perror_with_name (mess
);
180 supply_register (regno
, buf
);
183 /* Fetch register values from the inferior.
184 If REGNO is negative, do this for all registers.
185 Otherwise, REGNO specifies which register (so we can save time). */
188 old_fetch_inferior_registers (int regno
)
192 fetch_register (regno
);
196 for (regno
= 0; regno
< ARCH_NUM_REGS
; regno
++)
198 fetch_register (regno
);
203 /* Registers we shouldn't try to store. */
204 #if !defined (CANNOT_STORE_REGISTER)
205 #define CANNOT_STORE_REGISTER(regno) 0
208 /* Store one register. */
211 store_register (int regno
)
213 /* This isn't really an address. But ptrace thinks of it as one. */
215 char mess
[128]; /* For messages */
217 unsigned int offset
; /* Offset of registers within the u area. */
220 if (CANNOT_STORE_REGISTER (regno
))
225 /* Overload thread id onto process id */
226 if ((tid
= TIDGET (inferior_pid
)) == 0)
227 tid
= inferior_pid
; /* no thread id, just use process id */
229 offset
= U_REGS_OFFSET
;
231 regaddr
= register_addr (regno
, offset
);
232 for (i
= 0; i
< REGISTER_RAW_SIZE (regno
); i
+= sizeof (PTRACE_XFER_TYPE
))
235 ptrace (PT_WRITE_U
, tid
, (PTRACE_ARG3_TYPE
) regaddr
,
236 *(PTRACE_XFER_TYPE
*) & registers
[REGISTER_BYTE (regno
) + i
]);
237 regaddr
+= sizeof (PTRACE_XFER_TYPE
);
240 sprintf (mess
, "writing register %s (#%d)",
241 REGISTER_NAME (regno
), regno
);
242 perror_with_name (mess
);
247 /* Store our register values back into the inferior.
248 If REGNO is negative, do this for all registers.
249 Otherwise, REGNO specifies which register (so we can save time). */
252 old_store_inferior_registers (int regno
)
256 store_register (regno
);
260 for (regno
= 0; regno
< ARCH_NUM_REGS
; regno
++)
262 store_register (regno
);
268 /* Transfering the general-purpose registers between GDB, inferiors
271 /* Fill GDB's register array with the genereal-purpose register values
275 supply_gregset (elf_gregset_t
*gregsetp
)
277 elf_greg_t
*regp
= (elf_greg_t
*) gregsetp
;
280 for (i
= 0; i
< NUM_GREGS
; i
++)
281 supply_register (i
, (char *) (regp
+ regmap
[i
]));
284 /* Fill register REGNO (if it is a general-purpose register) in
285 *GREGSETPS with the value in GDB's register array. If REGNO is -1,
286 do this for all registers. */
289 fill_gregset (elf_gregset_t
*gregsetp
, int regno
)
291 elf_greg_t
*regp
= (elf_greg_t
*) gregsetp
;
294 for (i
= 0; i
< NUM_GREGS
; i
++)
295 if ((regno
== -1 || regno
== i
))
296 *(regp
+ regmap
[i
]) = *(elf_greg_t
*) ®isters
[REGISTER_BYTE (i
)];
299 #ifdef HAVE_PTRACE_GETREGS
301 /* Fetch all general-purpose registers from process/thread TID and
302 store their values in GDB's register array. */
309 if (ptrace (PTRACE_GETREGS
, tid
, 0, (int) ®s
) < 0)
313 /* The kernel we're running on doesn't support the GETREGS
314 request. Reset `have_ptrace_getregs'. */
315 have_ptrace_getregs
= 0;
319 perror_with_name ("Couldn't get registers");
322 supply_gregset (®s
);
325 /* Store all valid general-purpose registers in GDB's register array
326 into the process/thread specified by TID. */
329 store_regs (int tid
, int regno
)
333 if (ptrace (PTRACE_GETREGS
, tid
, 0, (int) ®s
) < 0)
334 perror_with_name ("Couldn't get registers");
336 fill_gregset (®s
, regno
);
338 if (ptrace (PTRACE_SETREGS
, tid
, 0, (int) ®s
) < 0)
339 perror_with_name ("Couldn't write registers");
344 static void fetch_regs (int tid
) {}
345 static void store_regs (int tid
, int regno
) {}
350 /* Transfering floating-point registers between GDB, inferiors and cores. */
352 /* Fill GDB's register array with the floating-point register values in
356 supply_fpregset (elf_fpregset_t
*fpregsetp
)
358 i387_supply_fsave ((char *) fpregsetp
);
362 /* Fill register REGNO (if it is a floating-point register) in
363 *FPREGSETP with the value in GDB's register array. If REGNO is -1,
364 do this for all registers. */
367 fill_fpregset (elf_fpregset_t
*fpregsetp
, int regno
)
369 i387_fill_fsave ((char *) fpregsetp
, regno
);
372 #ifdef HAVE_PTRACE_GETREGS
374 /* Fetch all floating-point registers from process/thread TID and store
375 thier values in GDB's register array. */
378 fetch_fpregs (int tid
)
380 elf_fpregset_t fpregs
;
382 if (ptrace (PTRACE_GETFPREGS
, tid
, 0, (int) &fpregs
) < 0)
383 perror_with_name ("Couldn't get floating point status");
385 supply_fpregset (&fpregs
);
388 /* Store all valid floating-point registers in GDB's register array
389 into the process/thread specified by TID. */
392 store_fpregs (int tid
, int regno
)
394 elf_fpregset_t fpregs
;
396 if (ptrace (PTRACE_GETFPREGS
, tid
, 0, (int) &fpregs
) < 0)
397 perror_with_name ("Couldn't get floating point status");
399 fill_fpregset (&fpregs
, regno
);
401 if (ptrace (PTRACE_SETFPREGS
, tid
, 0, (int) &fpregs
) < 0)
402 perror_with_name ("Couldn't write floating point status");
407 static void fetch_fpregs (int tid
) {}
408 static void store_fpregs (int tid
, int regno
) {}
413 /* Transfering floating-point and SSE registers to and from GDB. */
415 #ifdef HAVE_PTRACE_GETFPXREGS
417 /* Fill GDB's register array with the floating-point and SSE register
418 values in *FPXREGSETP. */
421 supply_fpxregset (elf_fpxregset_t
*fpxregsetp
)
423 i387_supply_fxsave ((char *) fpxregsetp
);
426 /* Fill register REGNO (if it is a floating-point or SSE register) in
427 *FPXREGSETP with the value in GDB's register array. If REGNO is
428 -1, do this for all registers. */
431 fill_fpxregset (elf_fpxregset_t
*fpxregsetp
, int regno
)
433 i387_fill_fxsave ((char *) fpxregsetp
, regno
);
436 /* Fetch all registers covered by the PTRACE_GETFPXREGS request from
437 process/thread TID and store their values in GDB's register array.
438 Return non-zero if successful, zero otherwise. */
441 fetch_fpxregs (int tid
)
443 elf_fpxregset_t fpxregs
;
445 if (! have_ptrace_getfpxregs
)
448 if (ptrace (PTRACE_GETFPXREGS
, tid
, 0, (int) &fpxregs
) < 0)
452 have_ptrace_getfpxregs
= 0;
456 perror_with_name ("Couldn't read floating-point and SSE registers");
459 supply_fpxregset (&fpxregs
);
463 /* Store all valid registers in GDB's register array covered by the
464 PTRACE_SETFPXREGS request into the process/thread specified by TID.
465 Return non-zero if successful, zero otherwise. */
468 store_fpxregs (int tid
, int regno
)
470 elf_fpxregset_t fpxregs
;
472 if (! have_ptrace_getfpxregs
)
475 if (ptrace (PTRACE_GETFPXREGS
, tid
, 0, &fpxregs
) == -1)
476 perror_with_name ("Couldn't read floating-point and SSE registers");
478 fill_fpxregset (&fpxregs
, regno
);
480 if (ptrace (PTRACE_SETFPXREGS
, tid
, 0, &fpxregs
) == -1)
481 perror_with_name ("Couldn't write floating-point and SSE registers");
486 /* Fill the XMM registers in the register array with dummy values. For
487 cases where we don't have access to the XMM registers. I think
488 this is cleaner than printing a warning. For a cleaner solution,
489 we should gdbarchify the i386 family. */
492 dummy_sse_values (void)
494 /* C doesn't have a syntax for NaN's, so write it out as an array of
496 static long dummy
[4] = { 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff };
497 static long mxcsr
= 0x1f80;
500 for (reg
= 0; reg
< 8; reg
++)
501 supply_register (XMM0_REGNUM
+ reg
, (char *) dummy
);
502 supply_register (MXCSR_REGNUM
, (char *) &mxcsr
);
507 static int fetch_fpxregs (int tid
) { return 0; }
508 static int store_fpxregs (int tid
, int regno
) { return 0; }
509 static void dummy_sse_values (void) {}
511 #endif /* HAVE_PTRACE_GETFPXREGS */
514 /* Transferring arbitrary registers between GDB and inferior. */
516 /* Fetch register REGNO from the child process. If REGNO is -1, do
517 this for all registers (including the floating point and SSE
521 fetch_inferior_registers (int regno
)
525 /* Use the old method of peeking around in `struct user' if the
526 GETREGS request isn't available. */
527 if (! have_ptrace_getregs
)
529 old_fetch_inferior_registers (regno
);
533 /* Linux LWP ID's are process ID's. */
534 if ((tid
= TIDGET (inferior_pid
)) == 0)
535 tid
= inferior_pid
; /* 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 old_fetch_inferior_registers (-1);
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 ("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
)
594 old_store_inferior_registers (regno
);
598 /* Linux LWP ID's are process ID's. */
599 if ((tid
= TIDGET (inferior_pid
)) == 0)
600 tid
= inferior_pid
; /* Not a threaded program. */
602 /* Use the PTRACE_SETFPXREGS requests whenever possible, since it
603 transfers more registers in one system call. But remember that
604 store_fpxregs can fail, and return zero. */
607 store_regs (tid
, regno
);
608 if (store_fpxregs (tid
, regno
))
610 store_fpregs (tid
, regno
);
614 if (GETREGS_SUPPLIES (regno
))
616 store_regs (tid
, regno
);
620 if (GETFPXREGS_SUPPLIES (regno
))
622 if (store_fpxregs (tid
, regno
))
625 /* Either our processor or our kernel doesn't support the SSE
626 registers, so just write the FP registers in the traditional
628 store_fpregs (tid
, regno
);
632 internal_error ("Got request to store bad register number %d.", regno
);
636 /* Interpreting register set info found in core files. */
638 /* Provide registers to GDB from a core file.
640 (We can't use the generic version of this function in
641 core-regset.c, because Linux has *three* different kinds of
642 register set notes. core-regset.c would have to call
643 supply_fpxregset, which most platforms don't have.)
645 CORE_REG_SECT points to an array of bytes, which are the contents
646 of a `note' from a core file which BFD thinks might contain
647 register contents. CORE_REG_SIZE is its size.
649 WHICH says which register set corelow suspects this is:
650 0 --- the general-purpose register set, in elf_gregset_t format
651 2 --- the floating-point register set, in elf_fpregset_t format
652 3 --- the extended floating-point register set, in elf_fpxregset_t format
654 REG_ADDR isn't used on Linux. */
657 fetch_core_registers (char *core_reg_sect
, unsigned core_reg_size
,
658 int which
, CORE_ADDR reg_addr
)
660 elf_gregset_t gregset
;
661 elf_fpregset_t fpregset
;
666 if (core_reg_size
!= sizeof (gregset
))
667 warning ("Wrong size gregset in core file.");
670 memcpy (&gregset
, core_reg_sect
, sizeof (gregset
));
671 supply_gregset (&gregset
);
676 if (core_reg_size
!= sizeof (fpregset
))
677 warning ("Wrong size fpregset in core file.");
680 memcpy (&fpregset
, core_reg_sect
, sizeof (fpregset
));
681 supply_fpregset (&fpregset
);
685 #ifdef HAVE_PTRACE_GETFPXREGS
687 elf_fpxregset_t fpxregset
;
690 if (core_reg_size
!= sizeof (fpxregset
))
691 warning ("Wrong size fpxregset in core file.");
694 memcpy (&fpxregset
, core_reg_sect
, sizeof (fpxregset
));
695 supply_fpxregset (&fpxregset
);
702 /* We've covered all the kinds of registers we know about here,
703 so this must be something we wouldn't know what to do with
704 anyway. Just ignore it. */
710 /* The instruction for a Linux system call is:
714 static const unsigned char linux_syscall
[] = { 0xcd, 0x80 };
716 #define LINUX_SYSCALL_LEN (sizeof linux_syscall)
718 /* The system call number is stored in the %eax register. */
719 #define LINUX_SYSCALL_REGNUM 0 /* %eax */
721 /* We are specifically interested in the sigreturn and rt_sigreturn
724 #ifndef SYS_sigreturn
725 #define SYS_sigreturn 0x77
727 #ifndef SYS_rt_sigreturn
728 #define SYS_rt_sigreturn 0xad
731 /* Offset to saved processor flags, from <asm/sigcontext.h>. */
732 #define LINUX_SIGCONTEXT_EFLAGS_OFFSET (64)
734 /* Resume execution of the inferior process.
735 If STEP is nonzero, single-step it.
736 If SIGNAL is nonzero, give it that signal. */
739 child_resume (int pid
, int step
, enum target_signal signal
)
741 int request
= PTRACE_CONT
;
744 /* Resume all threads. */
745 /* I think this only gets used in the non-threaded case, where "resume
746 all threads" and "resume inferior_pid" are the same. */
751 CORE_ADDR pc
= read_pc_pid (pid
);
752 unsigned char buf
[LINUX_SYSCALL_LEN
];
754 request
= PTRACE_SINGLESTEP
;
756 /* Returning from a signal trampoline is done by calling a
757 special system call (sigreturn or rt_sigreturn, see
758 i386-linux-tdep.c for more information). This system call
759 restores the registers that were saved when the signal was
760 raised, including %eflags. That means that single-stepping
761 won't work. Instead, we'll have to modify the signal context
762 that's about to be restored, and set the trace flag there. */
764 /* First check if PC is at a system call. */
765 if (read_memory_nobpt (pc
, (char *) buf
, LINUX_SYSCALL_LEN
) == 0
766 && memcmp (buf
, linux_syscall
, LINUX_SYSCALL_LEN
) == 0)
768 int syscall
= read_register_pid (LINUX_SYSCALL_REGNUM
, pid
);
770 /* Then check the system call number. */
771 if (syscall
== SYS_sigreturn
|| syscall
== SYS_rt_sigreturn
)
773 CORE_ADDR sp
= read_register (SP_REGNUM
);
775 unsigned long int eflags
;
777 if (syscall
== SYS_rt_sigreturn
)
778 addr
= read_memory_integer (sp
+ 8, 4) + 20;
780 /* Set the trace flag in the context that's about to be
782 addr
+= LINUX_SIGCONTEXT_EFLAGS_OFFSET
;
783 read_memory (addr
, (char *) &eflags
, 4);
785 write_memory (addr
, (char *) &eflags
, 4);
790 if (ptrace (request
, pid
, 0, target_signal_to_host (signal
)) == -1)
791 perror_with_name ("ptrace");
795 /* Register that we are able to handle Linux ELF core file formats. */
797 static struct core_fns linux_elf_core_fns
=
799 bfd_target_elf_flavour
, /* core_flavour */
800 default_check_format
, /* check_format */
801 default_core_sniffer
, /* core_sniffer */
802 fetch_core_registers
, /* core_read_registers */
807 _initialize_i386_linux_nat (void)
809 add_core_fns (&linux_elf_core_fns
);