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 #define OLD_CANNOT_FETCH_REGISTER(regno) ((regno) >= NUM_GREGS)
138 /* Fetch one register. */
141 fetch_register (int regno
)
143 /* This isn't really an address. But ptrace thinks of it as one. */
145 char mess
[128]; /* For messages */
147 unsigned int offset
; /* Offset of registers within the u area. */
148 char buf
[MAX_REGISTER_RAW_SIZE
];
151 if (OLD_CANNOT_FETCH_REGISTER (regno
))
153 memset (buf
, '\0', REGISTER_RAW_SIZE (regno
)); /* Supply zeroes */
154 supply_register (regno
, buf
);
158 /* Overload thread id onto process id */
159 if ((tid
= TIDGET (inferior_pid
)) == 0)
160 tid
= inferior_pid
; /* no thread id, just use process id */
162 offset
= U_REGS_OFFSET
;
164 regaddr
= register_addr (regno
, offset
);
165 for (i
= 0; i
< REGISTER_RAW_SIZE (regno
); i
+= sizeof (PTRACE_XFER_TYPE
))
168 *(PTRACE_XFER_TYPE
*) & buf
[i
] = ptrace (PT_READ_U
, tid
,
169 (PTRACE_ARG3_TYPE
) regaddr
, 0);
170 regaddr
+= sizeof (PTRACE_XFER_TYPE
);
173 sprintf (mess
, "reading register %s (#%d)",
174 REGISTER_NAME (regno
), regno
);
175 perror_with_name (mess
);
178 supply_register (regno
, buf
);
181 /* Fetch register values from the inferior.
182 If REGNO is negative, do this for all registers.
183 Otherwise, REGNO specifies which register (so we can save time). */
186 old_fetch_inferior_registers (int regno
)
190 fetch_register (regno
);
194 for (regno
= 0; regno
< ARCH_NUM_REGS
; regno
++)
196 fetch_register (regno
);
201 /* Registers we shouldn't try to store. */
202 #define OLD_CANNOT_STORE_REGISTER(regno) ((regno) >= NUM_GREGS)
204 /* Store one register. */
207 store_register (int regno
)
209 /* This isn't really an address. But ptrace thinks of it as one. */
211 char mess
[128]; /* For messages */
213 unsigned int offset
; /* Offset of registers within the u area. */
216 if (OLD_CANNOT_STORE_REGISTER (regno
))
221 /* Overload thread id onto process id */
222 if ((tid
= TIDGET (inferior_pid
)) == 0)
223 tid
= inferior_pid
; /* no thread id, just use process id */
225 offset
= U_REGS_OFFSET
;
227 regaddr
= register_addr (regno
, offset
);
228 for (i
= 0; i
< REGISTER_RAW_SIZE (regno
); i
+= sizeof (PTRACE_XFER_TYPE
))
231 ptrace (PT_WRITE_U
, tid
, (PTRACE_ARG3_TYPE
) regaddr
,
232 *(PTRACE_XFER_TYPE
*) & registers
[REGISTER_BYTE (regno
) + i
]);
233 regaddr
+= sizeof (PTRACE_XFER_TYPE
);
236 sprintf (mess
, "writing register %s (#%d)",
237 REGISTER_NAME (regno
), regno
);
238 perror_with_name (mess
);
243 /* Store our register values back into the inferior.
244 If REGNO is negative, do this for all registers.
245 Otherwise, REGNO specifies which register (so we can save time). */
248 old_store_inferior_registers (int regno
)
252 store_register (regno
);
256 for (regno
= 0; regno
< ARCH_NUM_REGS
; regno
++)
258 store_register (regno
);
264 /* Transfering the general-purpose registers between GDB, inferiors
267 /* Fill GDB's register array with the genereal-purpose register values
271 supply_gregset (elf_gregset_t
*gregsetp
)
273 elf_greg_t
*regp
= (elf_greg_t
*) gregsetp
;
276 for (i
= 0; i
< NUM_GREGS
; i
++)
277 supply_register (i
, (char *) (regp
+ regmap
[i
]));
280 /* Fill register REGNO (if it is a general-purpose register) in
281 *GREGSETPS with the value in GDB's register array. If REGNO is -1,
282 do this for all registers. */
285 fill_gregset (elf_gregset_t
*gregsetp
, int regno
)
287 elf_greg_t
*regp
= (elf_greg_t
*) gregsetp
;
290 for (i
= 0; i
< NUM_GREGS
; i
++)
291 if ((regno
== -1 || regno
== i
))
292 *(regp
+ regmap
[i
]) = *(elf_greg_t
*) ®isters
[REGISTER_BYTE (i
)];
295 #ifdef HAVE_PTRACE_GETREGS
297 /* Fetch all general-purpose registers from process/thread TID and
298 store their values in GDB's register array. */
305 if (ptrace (PTRACE_GETREGS
, tid
, 0, (int) ®s
) < 0)
309 /* The kernel we're running on doesn't support the GETREGS
310 request. Reset `have_ptrace_getregs'. */
311 have_ptrace_getregs
= 0;
315 perror_with_name ("Couldn't get registers");
318 supply_gregset (®s
);
321 /* Store all valid general-purpose registers in GDB's register array
322 into the process/thread specified by TID. */
325 store_regs (int tid
, int regno
)
329 if (ptrace (PTRACE_GETREGS
, tid
, 0, (int) ®s
) < 0)
330 perror_with_name ("Couldn't get registers");
332 fill_gregset (®s
, regno
);
334 if (ptrace (PTRACE_SETREGS
, tid
, 0, (int) ®s
) < 0)
335 perror_with_name ("Couldn't write registers");
340 static void fetch_regs (int tid
) {}
341 static void store_regs (int tid
, int regno
) {}
346 /* Transfering floating-point registers between GDB, inferiors and cores. */
348 /* Fill GDB's register array with the floating-point register values in
352 supply_fpregset (elf_fpregset_t
*fpregsetp
)
354 i387_supply_fsave ((char *) fpregsetp
);
358 /* Fill register REGNO (if it is a floating-point register) in
359 *FPREGSETP with the value in GDB's register array. If REGNO is -1,
360 do this for all registers. */
363 fill_fpregset (elf_fpregset_t
*fpregsetp
, int regno
)
365 i387_fill_fsave ((char *) fpregsetp
, regno
);
368 #ifdef HAVE_PTRACE_GETREGS
370 /* Fetch all floating-point registers from process/thread TID and store
371 thier values in GDB's register array. */
374 fetch_fpregs (int tid
)
376 elf_fpregset_t fpregs
;
378 if (ptrace (PTRACE_GETFPREGS
, tid
, 0, (int) &fpregs
) < 0)
379 perror_with_name ("Couldn't get floating point status");
381 supply_fpregset (&fpregs
);
384 /* Store all valid floating-point registers in GDB's register array
385 into the process/thread specified by TID. */
388 store_fpregs (int tid
, int regno
)
390 elf_fpregset_t fpregs
;
392 if (ptrace (PTRACE_GETFPREGS
, tid
, 0, (int) &fpregs
) < 0)
393 perror_with_name ("Couldn't get floating point status");
395 fill_fpregset (&fpregs
, regno
);
397 if (ptrace (PTRACE_SETFPREGS
, tid
, 0, (int) &fpregs
) < 0)
398 perror_with_name ("Couldn't write floating point status");
403 static void fetch_fpregs (int tid
) {}
404 static void store_fpregs (int tid
, int regno
) {}
409 /* Transfering floating-point and SSE registers to and from GDB. */
411 #ifdef HAVE_PTRACE_GETFPXREGS
413 /* Fill GDB's register array with the floating-point and SSE register
414 values in *FPXREGSETP. */
417 supply_fpxregset (elf_fpxregset_t
*fpxregsetp
)
419 i387_supply_fxsave ((char *) fpxregsetp
);
422 /* Fill register REGNO (if it is a floating-point or SSE register) in
423 *FPXREGSETP with the value in GDB's register array. If REGNO is
424 -1, do this for all registers. */
427 fill_fpxregset (elf_fpxregset_t
*fpxregsetp
, int regno
)
429 i387_fill_fxsave ((char *) fpxregsetp
, regno
);
432 /* Fetch all registers covered by the PTRACE_GETFPXREGS request from
433 process/thread TID and store their values in GDB's register array.
434 Return non-zero if successful, zero otherwise. */
437 fetch_fpxregs (int tid
)
439 elf_fpxregset_t fpxregs
;
441 if (! have_ptrace_getfpxregs
)
444 if (ptrace (PTRACE_GETFPXREGS
, tid
, 0, (int) &fpxregs
) < 0)
448 have_ptrace_getfpxregs
= 0;
452 perror_with_name ("Couldn't read floating-point and SSE registers");
455 supply_fpxregset (&fpxregs
);
459 /* Store all valid registers in GDB's register array covered by the
460 PTRACE_SETFPXREGS request into the process/thread specified by TID.
461 Return non-zero if successful, zero otherwise. */
464 store_fpxregs (int tid
, int regno
)
466 elf_fpxregset_t fpxregs
;
468 if (! have_ptrace_getfpxregs
)
471 if (ptrace (PTRACE_GETFPXREGS
, tid
, 0, &fpxregs
) == -1)
472 perror_with_name ("Couldn't read floating-point and SSE registers");
474 fill_fpxregset (&fpxregs
, regno
);
476 if (ptrace (PTRACE_SETFPXREGS
, tid
, 0, &fpxregs
) == -1)
477 perror_with_name ("Couldn't write floating-point and SSE registers");
482 /* Fill the XMM registers in the register array with dummy values. For
483 cases where we don't have access to the XMM registers. I think
484 this is cleaner than printing a warning. For a cleaner solution,
485 we should gdbarchify the i386 family. */
488 dummy_sse_values (void)
490 /* C doesn't have a syntax for NaN's, so write it out as an array of
492 static long dummy
[4] = { 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff };
493 static long mxcsr
= 0x1f80;
496 for (reg
= 0; reg
< 8; reg
++)
497 supply_register (XMM0_REGNUM
+ reg
, (char *) dummy
);
498 supply_register (MXCSR_REGNUM
, (char *) &mxcsr
);
503 static int fetch_fpxregs (int tid
) { return 0; }
504 static int store_fpxregs (int tid
, int regno
) { return 0; }
505 static void dummy_sse_values (void) {}
507 #endif /* HAVE_PTRACE_GETFPXREGS */
510 /* Transferring arbitrary registers between GDB and inferior. */
512 /* Check if register REGNO in the child process is accessible.
513 If we are accessing registers directly via the U area, only the
514 general-purpose registers are available.
515 All registers should be accessible if we have GETREGS support. */
518 cannot_fetch_register (int regno
)
520 if (! have_ptrace_getregs
)
521 return OLD_CANNOT_FETCH_REGISTER (regno
);
525 cannot_store_register (int regno
)
527 if (! have_ptrace_getregs
)
528 return OLD_CANNOT_STORE_REGISTER (regno
);
532 /* Fetch register REGNO from the child process. If REGNO is -1, do
533 this for all registers (including the floating point and SSE
537 fetch_inferior_registers (int regno
)
541 /* Use the old method of peeking around in `struct user' if the
542 GETREGS request isn't available. */
543 if (! have_ptrace_getregs
)
545 old_fetch_inferior_registers (regno
);
549 /* Linux LWP ID's are process ID's. */
550 if ((tid
= TIDGET (inferior_pid
)) == 0)
551 tid
= inferior_pid
; /* Not a threaded program. */
553 /* Use the PTRACE_GETFPXREGS request whenever possible, since it
554 transfers more registers in one system call, and we'll cache the
555 results. But remember that fetch_fpxregs can fail, and return
561 /* The call above might reset `have_ptrace_getregs'. */
562 if (! have_ptrace_getregs
)
564 old_fetch_inferior_registers (-1);
568 if (fetch_fpxregs (tid
))
574 if (GETREGS_SUPPLIES (regno
))
580 if (GETFPXREGS_SUPPLIES (regno
))
582 if (fetch_fpxregs (tid
))
585 /* Either our processor or our kernel doesn't support the SSE
586 registers, so read the FP registers in the traditional way,
587 and fill the SSE registers with dummy values. It would be
588 more graceful to handle differences in the register set using
589 gdbarch. Until then, this will at least make things work
595 internal_error ("Got request for bad register number %d.", regno
);
598 /* Store register REGNO back into the child process. If REGNO is -1,
599 do this for all registers (including the floating point and SSE
602 store_inferior_registers (int regno
)
606 /* Use the old method of poking around in `struct user' if the
607 SETREGS request isn't available. */
608 if (! have_ptrace_getregs
)
610 old_store_inferior_registers (regno
);
614 /* Linux LWP ID's are process ID's. */
615 if ((tid
= TIDGET (inferior_pid
)) == 0)
616 tid
= inferior_pid
; /* Not a threaded program. */
618 /* Use the PTRACE_SETFPXREGS requests whenever possible, since it
619 transfers more registers in one system call. But remember that
620 store_fpxregs can fail, and return zero. */
623 store_regs (tid
, regno
);
624 if (store_fpxregs (tid
, regno
))
626 store_fpregs (tid
, regno
);
630 if (GETREGS_SUPPLIES (regno
))
632 store_regs (tid
, regno
);
636 if (GETFPXREGS_SUPPLIES (regno
))
638 if (store_fpxregs (tid
, regno
))
641 /* Either our processor or our kernel doesn't support the SSE
642 registers, so just write the FP registers in the traditional
644 store_fpregs (tid
, regno
);
648 internal_error ("Got request to store bad register number %d.", regno
);
652 /* Interpreting register set info found in core files. */
654 /* Provide registers to GDB from a core file.
656 (We can't use the generic version of this function in
657 core-regset.c, because Linux has *three* different kinds of
658 register set notes. core-regset.c would have to call
659 supply_fpxregset, which most platforms don't have.)
661 CORE_REG_SECT points to an array of bytes, which are the contents
662 of a `note' from a core file which BFD thinks might contain
663 register contents. CORE_REG_SIZE is its size.
665 WHICH says which register set corelow suspects this is:
666 0 --- the general-purpose register set, in elf_gregset_t format
667 2 --- the floating-point register set, in elf_fpregset_t format
668 3 --- the extended floating-point register set, in elf_fpxregset_t format
670 REG_ADDR isn't used on Linux. */
673 fetch_core_registers (char *core_reg_sect
, unsigned core_reg_size
,
674 int which
, CORE_ADDR reg_addr
)
676 elf_gregset_t gregset
;
677 elf_fpregset_t fpregset
;
682 if (core_reg_size
!= sizeof (gregset
))
683 warning ("Wrong size gregset in core file.");
686 memcpy (&gregset
, core_reg_sect
, sizeof (gregset
));
687 supply_gregset (&gregset
);
692 if (core_reg_size
!= sizeof (fpregset
))
693 warning ("Wrong size fpregset in core file.");
696 memcpy (&fpregset
, core_reg_sect
, sizeof (fpregset
));
697 supply_fpregset (&fpregset
);
701 #ifdef HAVE_PTRACE_GETFPXREGS
703 elf_fpxregset_t fpxregset
;
706 if (core_reg_size
!= sizeof (fpxregset
))
707 warning ("Wrong size fpxregset in core file.");
710 memcpy (&fpxregset
, core_reg_sect
, sizeof (fpxregset
));
711 supply_fpxregset (&fpxregset
);
718 /* We've covered all the kinds of registers we know about here,
719 so this must be something we wouldn't know what to do with
720 anyway. Just ignore it. */
726 /* The instruction for a Linux system call is:
730 static const unsigned char linux_syscall
[] = { 0xcd, 0x80 };
732 #define LINUX_SYSCALL_LEN (sizeof linux_syscall)
734 /* The system call number is stored in the %eax register. */
735 #define LINUX_SYSCALL_REGNUM 0 /* %eax */
737 /* We are specifically interested in the sigreturn and rt_sigreturn
740 #ifndef SYS_sigreturn
741 #define SYS_sigreturn 0x77
743 #ifndef SYS_rt_sigreturn
744 #define SYS_rt_sigreturn 0xad
747 /* Offset to saved processor flags, from <asm/sigcontext.h>. */
748 #define LINUX_SIGCONTEXT_EFLAGS_OFFSET (64)
750 /* Resume execution of the inferior process.
751 If STEP is nonzero, single-step it.
752 If SIGNAL is nonzero, give it that signal. */
755 child_resume (int pid
, int step
, enum target_signal signal
)
757 int request
= PTRACE_CONT
;
760 /* Resume all threads. */
761 /* I think this only gets used in the non-threaded case, where "resume
762 all threads" and "resume inferior_pid" are the same. */
767 CORE_ADDR pc
= read_pc_pid (pid
);
768 unsigned char buf
[LINUX_SYSCALL_LEN
];
770 request
= PTRACE_SINGLESTEP
;
772 /* Returning from a signal trampoline is done by calling a
773 special system call (sigreturn or rt_sigreturn, see
774 i386-linux-tdep.c for more information). This system call
775 restores the registers that were saved when the signal was
776 raised, including %eflags. That means that single-stepping
777 won't work. Instead, we'll have to modify the signal context
778 that's about to be restored, and set the trace flag there. */
780 /* First check if PC is at a system call. */
781 if (read_memory_nobpt (pc
, (char *) buf
, LINUX_SYSCALL_LEN
) == 0
782 && memcmp (buf
, linux_syscall
, LINUX_SYSCALL_LEN
) == 0)
784 int syscall
= read_register_pid (LINUX_SYSCALL_REGNUM
, pid
);
786 /* Then check the system call number. */
787 if (syscall
== SYS_sigreturn
|| syscall
== SYS_rt_sigreturn
)
789 CORE_ADDR sp
= read_register (SP_REGNUM
);
791 unsigned long int eflags
;
793 if (syscall
== SYS_rt_sigreturn
)
794 addr
= read_memory_integer (sp
+ 8, 4) + 20;
796 /* Set the trace flag in the context that's about to be
798 addr
+= LINUX_SIGCONTEXT_EFLAGS_OFFSET
;
799 read_memory (addr
, (char *) &eflags
, 4);
801 write_memory (addr
, (char *) &eflags
, 4);
806 if (ptrace (request
, pid
, 0, target_signal_to_host (signal
)) == -1)
807 perror_with_name ("ptrace");
811 /* Register that we are able to handle Linux ELF core file formats. */
813 static struct core_fns linux_elf_core_fns
=
815 bfd_target_elf_flavour
, /* core_flavour */
816 default_check_format
, /* check_format */
817 default_core_sniffer
, /* core_sniffer */
818 fetch_core_registers
, /* core_read_registers */
823 _initialize_i386_linux_nat (void)
825 add_core_fns (&linux_elf_core_fns
);