1 /* Native-dependent code for Linux running on i386's, for GDB.
2 Copyright (C) 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 /* For i386_linux_skip_solib_resolver. */
30 #include <sys/ptrace.h>
32 #include <sys/procfs.h>
38 /* Prototypes for supply_gregset etc. */
41 /* On Linux, threads are implemented as pseudo-processes, in which
42 case we may be tracing more than one process at a time. In that
43 case, inferior_pid will contain the main process ID and the
44 individual thread (process) ID mashed together. These macros are
45 used to separate them out. These definitions should be overridden
46 if thread support is included. */
48 #if !defined (PIDGET) /* Default definition for PIDGET/TIDGET. */
49 #define PIDGET(PID) PID
54 /* The register sets used in Linux ELF core-dumps are identical to the
55 register sets in `struct user' that is used for a.out core-dumps,
56 and is also used by `ptrace'. The corresponding types are
57 `elf_gregset_t' for the general-purpose registers (with
58 `elf_greg_t' the type of a single GP register) and `elf_fpregset_t'
59 for the floating-point registers.
61 Those types used to be available under the names `gregset_t' and
62 `fpregset_t' too, and this file used those names in the past. But
63 those names are now used for the register sets used in the
64 `mcontext_t' type, and have a different size and layout. */
66 /* Mapping between the general-purpose registers in `struct user'
67 format and GDB's register array layout. */
76 /* Which ptrace request retrieves which registers?
77 These apply to the corresponding SET requests as well. */
78 #define GETREGS_SUPPLIES(regno) \
79 (0 <= (regno) && (regno) <= 15)
80 #define GETFPREGS_SUPPLIES(regno) \
81 (FP0_REGNUM <= (regno) && (regno) <= LAST_FPU_CTRL_REGNUM)
82 #define GETXFPREGS_SUPPLIES(regno) \
83 (FP0_REGNUM <= (regno) && (regno) <= MXCSR_REGNUM)
85 /* Does the current host support the GETREGS request? */
86 int have_ptrace_getregs
=
87 #ifdef HAVE_PTRACE_GETREGS
94 /* Does the current host support the GETXFPREGS request? The header
95 file may or may not define it, and even if it is defined, the
96 kernel will return EIO if it's running on a pre-SSE processor.
98 PTRACE_GETXFPREGS is a Cygnus invention, since we wrote our own
99 Linux kernel patch for SSE support. That patch may or may not
100 actually make it into the official distribution. If you find that
101 years have gone by since this stuff was added, and Linux isn't
102 using PTRACE_GETXFPREGS, that means that our patch didn't make it,
103 and you can delete this, and the related code.
105 My instinct is to attach this to some architecture- or
106 target-specific data structure, but really, a particular GDB
107 process can only run on top of one kernel at a time. So it's okay
108 for this to be a simple variable. */
109 int have_ptrace_getxfpregs
=
110 #ifdef HAVE_PTRACE_GETXFPREGS
118 /* Fetching registers directly from the U area, one at a time. */
120 /* FIXME: kettenis/2000-03-05: This duplicates code from `inptrace.c'.
121 The problem is that we define FETCH_INFERIOR_REGISTERS since we
122 want to use our own versions of {fetch,store}_inferior_registers
123 that use the GETREGS request. This means that the code in
124 `infptrace.c' is #ifdef'd out. But we need to fall back on that
125 code when GDB is running on top of a kernel that doesn't support
126 the GETREGS request. I want to avoid changing `infptrace.c' right
130 #define PT_READ_U PTRACE_PEEKUSR
133 #define PT_WRITE_U PTRACE_POKEUSR
136 /* Default the type of the ptrace transfer to int. */
137 #ifndef PTRACE_XFER_TYPE
138 #define PTRACE_XFER_TYPE int
141 /* Registers we shouldn't try to fetch. */
142 #if !defined (CANNOT_FETCH_REGISTER)
143 #define CANNOT_FETCH_REGISTER(regno) 0
146 /* Fetch one register. */
149 fetch_register (int regno
)
151 /* This isn't really an address. But ptrace thinks of it as one. */
153 char mess
[128]; /* For messages */
155 unsigned int offset
; /* Offset of registers within the u area. */
156 char buf
[MAX_REGISTER_RAW_SIZE
];
159 if (CANNOT_FETCH_REGISTER (regno
))
161 memset (buf
, '\0', REGISTER_RAW_SIZE (regno
)); /* Supply zeroes */
162 supply_register (regno
, buf
);
166 /* Overload thread id onto process id */
167 if ((tid
= TIDGET (inferior_pid
)) == 0)
168 tid
= inferior_pid
; /* no thread id, just use process id */
170 offset
= U_REGS_OFFSET
;
172 regaddr
= register_addr (regno
, offset
);
173 for (i
= 0; i
< REGISTER_RAW_SIZE (regno
); i
+= sizeof (PTRACE_XFER_TYPE
))
176 *(PTRACE_XFER_TYPE
*) & buf
[i
] = ptrace (PT_READ_U
, tid
,
177 (PTRACE_ARG3_TYPE
) regaddr
, 0);
178 regaddr
+= sizeof (PTRACE_XFER_TYPE
);
181 sprintf (mess
, "reading register %s (#%d)",
182 REGISTER_NAME (regno
), regno
);
183 perror_with_name (mess
);
186 supply_register (regno
, buf
);
189 /* Fetch register values from the inferior.
190 If REGNO is negative, do this for all registers.
191 Otherwise, REGNO specifies which register (so we can save time). */
194 old_fetch_inferior_registers (int regno
)
198 fetch_register (regno
);
202 for (regno
= 0; regno
< ARCH_NUM_REGS
; regno
++)
204 fetch_register (regno
);
209 /* Registers we shouldn't try to store. */
210 #if !defined (CANNOT_STORE_REGISTER)
211 #define CANNOT_STORE_REGISTER(regno) 0
214 /* Store one register. */
217 store_register (int regno
)
219 /* This isn't really an address. But ptrace thinks of it as one. */
221 char mess
[128]; /* For messages */
223 unsigned int offset
; /* Offset of registers within the u area. */
226 if (CANNOT_STORE_REGISTER (regno
))
231 /* Overload thread id onto process id */
232 if ((tid
= TIDGET (inferior_pid
)) == 0)
233 tid
= inferior_pid
; /* no thread id, just use process id */
235 offset
= U_REGS_OFFSET
;
237 regaddr
= register_addr (regno
, offset
);
238 for (i
= 0; i
< REGISTER_RAW_SIZE (regno
); i
+= sizeof (PTRACE_XFER_TYPE
))
241 ptrace (PT_WRITE_U
, tid
, (PTRACE_ARG3_TYPE
) regaddr
,
242 *(PTRACE_XFER_TYPE
*) & registers
[REGISTER_BYTE (regno
) + i
]);
243 regaddr
+= sizeof (PTRACE_XFER_TYPE
);
246 sprintf (mess
, "writing register %s (#%d)",
247 REGISTER_NAME (regno
), regno
);
248 perror_with_name (mess
);
253 /* Store our register values back into the inferior.
254 If REGNO is negative, do this for all registers.
255 Otherwise, REGNO specifies which register (so we can save time). */
258 old_store_inferior_registers (int regno
)
262 store_register (regno
);
266 for (regno
= 0; regno
< ARCH_NUM_REGS
; regno
++)
268 store_register (regno
);
274 /* Transfering the general-purpose registers between GDB, inferiors
277 /* Fill GDB's register array with the genereal-purpose register values
281 supply_gregset (elf_gregset_t
*gregsetp
)
283 elf_greg_t
*regp
= (elf_greg_t
*) gregsetp
;
286 for (regi
= 0; regi
< NUM_GREGS
; regi
++)
287 supply_register (regi
, (char *) (regp
+ regmap
[regi
]));
290 /* Convert the valid general-purpose register values in GDB's register
291 array to `struct user' format and store them in *GREGSETP. The
292 array VALID indicates which register values are valid. If VALID is
293 NULL, all registers are assumed to be valid. */
296 convert_to_gregset (elf_gregset_t
*gregsetp
, signed char *valid
)
298 elf_greg_t
*regp
= (elf_greg_t
*) gregsetp
;
301 for (regi
= 0; regi
< NUM_GREGS
; regi
++)
302 if (! valid
|| valid
[regi
])
303 *(regp
+ regmap
[regi
]) = * (int *) ®isters
[REGISTER_BYTE (regi
)];
306 /* Fill register REGNO (if it is a general-purpose register) in
307 *GREGSETPS with the value in GDB's register array. If REGNO is -1,
308 do this for all registers. */
310 fill_gregset (elf_gregset_t
*gregsetp
, int regno
)
314 convert_to_gregset (gregsetp
, NULL
);
318 if (GETREGS_SUPPLIES (regno
))
320 signed char valid
[NUM_GREGS
];
322 memset (valid
, 0, sizeof (valid
));
325 convert_to_gregset (gregsetp
, valid
);
329 #ifdef HAVE_PTRACE_GETREGS
331 /* Fetch all general-purpose registers from process/thread TID and
332 store their values in GDB's register array. */
340 ret
= ptrace (PTRACE_GETREGS
, tid
, 0, (int) ®s
);
345 /* The kernel we're running on doesn't support the GETREGS
346 request. Reset `have_ptrace_getregs'. */
347 have_ptrace_getregs
= 0;
351 warning ("Couldn't get registers.");
355 supply_gregset (®s
);
358 /* Store all valid general-purpose registers in GDB's register array
359 into the process/thread specified by TID. */
367 ret
= ptrace (PTRACE_GETREGS
, tid
, 0, (int) ®s
);
370 warning ("Couldn't get registers.");
374 convert_to_gregset (®s
, register_valid
);
376 ret
= ptrace (PTRACE_SETREGS
, tid
, 0, (int) ®s
);
379 warning ("Couldn't write registers.");
386 static void fetch_regs (int tid
) {}
387 static void store_regs (int tid
) {}
392 /* Transfering floating-point registers between GDB, inferiors and cores. */
394 /* What is the address of st(N) within the floating-point register set F? */
395 #define FPREG_ADDR(f, n) ((char *) &(f)->st_space + (n) * 10)
397 /* Fill GDB's register array with the floating-point register values in
401 supply_fpregset (elf_fpregset_t
*fpregsetp
)
406 /* Supply the floating-point registers. */
407 for (reg
= 0; reg
< 8; reg
++)
408 supply_register (FP0_REGNUM
+ reg
, FPREG_ADDR (fpregsetp
, reg
));
410 /* We have to mask off the reserved bits in *FPREGSETP before
411 storing the values in GDB's register file. */
412 #define supply(REGNO, MEMBER) \
413 l = fpregsetp->MEMBER & 0xffff; \
414 supply_register (REGNO, (char *) &l)
416 supply (FCTRL_REGNUM
, cwd
);
417 supply (FSTAT_REGNUM
, swd
);
418 supply (FTAG_REGNUM
, twd
);
419 supply_register (FCOFF_REGNUM
, (char *) &fpregsetp
->fip
);
420 supply (FDS_REGNUM
, fos
);
421 supply_register (FDOFF_REGNUM
, (char *) &fpregsetp
->foo
);
425 /* Extract the code segment and opcode from the "fcs" member. */
426 l
= fpregsetp
->fcs
& 0xffff;
427 supply_register (FCS_REGNUM
, (char *) &l
);
429 l
= (fpregsetp
->fcs
>> 16) & ((1 << 11) - 1);
430 supply_register (FOP_REGNUM
, (char *) &l
);
433 /* Convert the valid floating-point register values in GDB's register
434 array to `struct user' format and store them in *FPREGSETP. The
435 array VALID indicates which register values are valid. If VALID is
436 NULL, all registers are assumed to be valid. */
439 convert_to_fpregset (elf_fpregset_t
*fpregsetp
, signed char *valid
)
443 /* Fill in the floating-point registers. */
444 for (reg
= 0; reg
< 8; reg
++)
445 if (!valid
|| valid
[reg
])
446 memcpy (FPREG_ADDR (fpregsetp
, reg
),
447 ®isters
[REGISTER_BYTE (FP0_REGNUM
+ reg
)],
448 REGISTER_RAW_SIZE(FP0_REGNUM
+ reg
));
450 /* We're not supposed to touch the reserved bits in *FPREGSETP. */
452 #define fill(MEMBER, REGNO) \
453 if (! valid || valid[(REGNO)]) \
455 = ((fpregsetp->MEMBER & ~0xffff) \
456 | (* (int *) ®isters[REGISTER_BYTE (REGNO)] & 0xffff))
458 #define fill_register(MEMBER, REGNO) \
459 if (! valid || valid[(REGNO)]) \
460 memcpy (&fpregsetp->MEMBER, ®isters[REGISTER_BYTE (REGNO)], \
461 sizeof (fpregsetp->MEMBER))
463 fill (cwd
, FCTRL_REGNUM
);
464 fill (swd
, FSTAT_REGNUM
);
465 fill (twd
, FTAG_REGNUM
);
466 fill_register (fip
, FCOFF_REGNUM
);
467 fill (foo
, FDOFF_REGNUM
);
468 fill_register (fos
, FDS_REGNUM
);
473 if (! valid
|| valid
[FCS_REGNUM
])
475 = ((fpregsetp
->fcs
& ~0xffff)
476 | (* (int *) ®isters
[REGISTER_BYTE (FCS_REGNUM
)] & 0xffff));
478 if (! valid
|| valid
[FOP_REGNUM
])
480 = ((fpregsetp
->fcs
& 0xffff)
481 | ((*(int *) ®isters
[REGISTER_BYTE (FOP_REGNUM
)] & ((1 << 11) - 1))
485 /* Fill register REGNO (if it is a floating-point register) in
486 *FPREGSETP with the value in GDB's register array. If REGNO is -1,
487 do this for all registers. */
490 fill_fpregset (elf_fpregset_t
*fpregsetp
, int regno
)
494 convert_to_fpregset (fpregsetp
, NULL
);
498 if (GETFPREGS_SUPPLIES(regno
))
500 signed char valid
[MAX_NUM_REGS
];
502 memset (valid
, 0, sizeof (valid
));
505 convert_to_fpregset (fpregsetp
, valid
);
509 #ifdef HAVE_PTRACE_GETREGS
511 /* Fetch all floating-point registers from process/thread TID and store
512 thier values in GDB's register array. */
515 fetch_fpregs (int tid
)
517 elf_fpregset_t fpregs
;
520 ret
= ptrace (PTRACE_GETFPREGS
, tid
, 0, (int) &fpregs
);
523 warning ("Couldn't get floating point status.");
527 supply_fpregset (&fpregs
);
530 /* Store all valid floating-point registers in GDB's register array
531 into the process/thread specified by TID. */
534 store_fpregs (int tid
)
536 elf_fpregset_t fpregs
;
539 ret
= ptrace (PTRACE_GETFPREGS
, tid
, 0, (int) &fpregs
);
542 warning ("Couldn't get floating point status.");
546 convert_to_fpregset (&fpregs
, register_valid
);
548 ret
= ptrace (PTRACE_SETFPREGS
, tid
, 0, (int) &fpregs
);
551 warning ("Couldn't write floating point status.");
558 static void fetch_fpregs (int tid
) {}
559 static void store_fpregs (int tid
) {}
564 /* Transfering floating-point and SSE registers to and from GDB. */
566 /* PTRACE_GETXFPREGS is a Cygnus invention, since we wrote our own
567 Linux kernel patch for SSE support. That patch may or may not
568 actually make it into the official distribution. If you find that
569 years have gone by since this code was added, and Linux isn't using
570 PTRACE_GETXFPREGS, that means that our patch didn't make it, and
571 you can delete this code. */
573 #ifdef HAVE_PTRACE_GETXFPREGS
575 /* Fill GDB's register array with the floating-point and SSE register
576 values in *XFPREGS. */
579 supply_xfpregset (struct user_xfpregs_struct
*xfpregs
)
583 /* Supply the floating-point registers. */
584 for (reg
= 0; reg
< 8; reg
++)
585 supply_register (FP0_REGNUM
+ reg
, (char *) &xfpregs
->st_space
[reg
]);
588 supply_register (FCTRL_REGNUM
, (char *) &xfpregs
->cwd
);
589 supply_register (FSTAT_REGNUM
, (char *) &xfpregs
->swd
);
590 supply_register (FTAG_REGNUM
, (char *) &xfpregs
->twd
);
591 supply_register (FCOFF_REGNUM
, (char *) &xfpregs
->fip
);
592 supply_register (FDS_REGNUM
, (char *) &xfpregs
->fos
);
593 supply_register (FDOFF_REGNUM
, (char *) &xfpregs
->foo
);
595 /* Extract the code segment and opcode from the "fcs" member. */
599 l
= xfpregs
->fcs
& 0xffff;
600 supply_register (FCS_REGNUM
, (char *) &l
);
602 l
= (xfpregs
->fcs
>> 16) & ((1 << 11) - 1);
603 supply_register (FOP_REGNUM
, (char *) &l
);
607 /* Supply the SSE registers. */
608 for (reg
= 0; reg
< 8; reg
++)
609 supply_register (XMM0_REGNUM
+ reg
, (char *) &xfpregs
->xmm_space
[reg
]);
610 supply_register (MXCSR_REGNUM
, (char *) &xfpregs
->mxcsr
);
613 /* Convert the valid floating-point and SSE registers in GDB's
614 register array to `struct user' format and store them in *XFPREGS.
615 The array VALID indicates which registers are valid. If VALID is
616 NULL, all registers are assumed to be valid. */
619 convert_to_xfpregset (struct user_xfpregs_struct
*xfpregs
,
624 /* Fill in the floating-point registers. */
625 for (reg
= 0; reg
< 8; reg
++)
626 if (!valid
|| valid
[reg
])
627 memcpy (&xfpregs
->st_space
[reg
],
628 ®isters
[REGISTER_BYTE (FP0_REGNUM
+ reg
)],
629 REGISTER_RAW_SIZE(FP0_REGNUM
+ reg
));
631 #define fill(MEMBER, REGNO) \
632 if (! valid || valid[(REGNO)]) \
633 memcpy (&xfpregs->MEMBER, ®isters[REGISTER_BYTE (REGNO)], \
634 sizeof (xfpregs->MEMBER))
636 fill (cwd
, FCTRL_REGNUM
);
637 fill (swd
, FSTAT_REGNUM
);
638 fill (twd
, FTAG_REGNUM
);
639 fill (fip
, FCOFF_REGNUM
);
640 fill (foo
, FDOFF_REGNUM
);
641 fill (fos
, FDS_REGNUM
);
645 if (! valid
|| valid
[FCS_REGNUM
])
647 = ((xfpregs
->fcs
& ~0xffff)
648 | (* (int *) ®isters
[REGISTER_BYTE (FCS_REGNUM
)] & 0xffff));
650 if (! valid
|| valid
[FOP_REGNUM
])
652 = ((xfpregs
->fcs
& 0xffff)
653 | ((*(int *) ®isters
[REGISTER_BYTE (FOP_REGNUM
)] & ((1 << 11) - 1))
656 /* Fill in the XMM registers. */
657 for (reg
= 0; reg
< 8; reg
++)
658 if (! valid
|| valid
[reg
])
659 memcpy (&xfpregs
->xmm_space
[reg
],
660 ®isters
[REGISTER_BYTE (XMM0_REGNUM
+ reg
)],
661 REGISTER_RAW_SIZE (XMM0_REGNUM
+ reg
));
664 /* Fetch all registers covered by the PTRACE_SETXFPREGS request from
665 process/thread TID and store their values in GDB's register array.
666 Return non-zero if successful, zero otherwise. */
669 fetch_xfpregs (int tid
)
671 struct user_xfpregs_struct xfpregs
;
674 if (! have_ptrace_getxfpregs
)
677 ret
= ptrace (PTRACE_GETXFPREGS
, tid
, 0, &xfpregs
);
682 have_ptrace_getxfpregs
= 0;
686 warning ("Couldn't read floating-point and SSE registers.");
690 supply_xfpregset (&xfpregs
);
694 /* Store all valid registers in GDB's register array covered by the
695 PTRACE_SETXFPREGS request into the process/thread specified by TID.
696 Return non-zero if successful, zero otherwise. */
699 store_xfpregs (int tid
)
701 struct user_xfpregs_struct xfpregs
;
704 if (! have_ptrace_getxfpregs
)
707 ret
= ptrace (PTRACE_GETXFPREGS
, tid
, 0, &xfpregs
);
712 have_ptrace_getxfpregs
= 0;
716 warning ("Couldn't read floating-point and SSE registers.");
720 convert_to_xfpregset (&xfpregs
, register_valid
);
722 if (ptrace (PTRACE_SETXFPREGS
, tid
, 0, &xfpregs
) < 0)
724 warning ("Couldn't write floating-point and SSE registers.");
731 /* Fill the XMM registers in the register array with dummy values. For
732 cases where we don't have access to the XMM registers. I think
733 this is cleaner than printing a warning. For a cleaner solution,
734 we should gdbarchify the i386 family. */
737 dummy_sse_values (void)
739 /* C doesn't have a syntax for NaN's, so write it out as an array of
741 static long dummy
[4] = { 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff };
742 static long mxcsr
= 0x1f80;
745 for (reg
= 0; reg
< 8; reg
++)
746 supply_register (XMM0_REGNUM
+ reg
, (char *) dummy
);
747 supply_register (MXCSR_REGNUM
, (char *) &mxcsr
);
752 /* Stub versions of the above routines, for systems that don't have
753 PTRACE_GETXFPREGS. */
754 static int store_xfpregs (int tid
) { return 0; }
755 static int fetch_xfpregs (int tid
) { return 0; }
756 static void dummy_sse_values (void) {}
761 /* Transferring arbitrary registers between GDB and inferior. */
763 /* Fetch register REGNO from the child process. If REGNO is -1, do
764 this for all registers (including the floating point and SSE
768 fetch_inferior_registers (int regno
)
772 /* Use the old method of peeking around in `struct user' if the
773 GETREGS request isn't available. */
774 if (! have_ptrace_getregs
)
776 old_fetch_inferior_registers (regno
);
780 /* Linux LWP ID's are process ID's. */
781 if ((tid
= TIDGET (inferior_pid
)) == 0)
782 tid
= inferior_pid
; /* Not a threaded program. */
784 /* Use the PTRACE_GETXFPREGS request whenever possible, since it
785 transfers more registers in one system call, and we'll cache the
786 results. But remember that fetch_xfpregs can fail, and return
792 /* The call above might reset `have_ptrace_getregs'. */
793 if (! have_ptrace_getregs
)
795 old_fetch_inferior_registers (-1);
799 if (fetch_xfpregs (tid
))
805 if (GETREGS_SUPPLIES (regno
))
811 if (GETXFPREGS_SUPPLIES (regno
))
813 if (fetch_xfpregs (tid
))
816 /* Either our processor or our kernel doesn't support the SSE
817 registers, so read the FP registers in the traditional way,
818 and fill the SSE registers with dummy values. It would be
819 more graceful to handle differences in the register set using
820 gdbarch. Until then, this will at least make things work
827 internal_error ("i386-linux-nat.c (fetch_inferior_registers): "
828 "got request for bad register number %d", regno
);
831 /* Store register REGNO back into the child process. If REGNO is -1,
832 do this for all registers (including the floating point and SSE
835 store_inferior_registers (int regno
)
839 /* Use the old method of poking around in `struct user' if the
840 SETREGS request isn't available. */
841 if (! have_ptrace_getregs
)
843 old_store_inferior_registers (regno
);
847 /* Linux LWP ID's are process ID's. */
848 if ((tid
= TIDGET (inferior_pid
)) == 0)
849 tid
= inferior_pid
; /* Not a threaded program. */
851 /* Use the PTRACE_SETXFPREGS requests whenever possibl, since it
852 transfers more registers in one system call. But remember that
853 store_xfpregs can fail, and return zero. */
857 if (store_xfpregs (tid
))
863 if (GETREGS_SUPPLIES (regno
))
869 if (GETXFPREGS_SUPPLIES (regno
))
871 if (store_xfpregs (tid
))
874 /* Either our processor or our kernel doesn't support the SSE
875 registers, so just write the FP registers in the traditional
881 internal_error ("Got request to store bad register number %d.", regno
);
885 /* Interpreting register set info found in core files. */
887 /* Provide registers to GDB from a core file.
889 (We can't use the generic version of this function in
890 core-regset.c, because Linux has *three* different kinds of
891 register set notes. core-regset.c would have to call
892 supply_xfpregset, which most platforms don't have.)
894 CORE_REG_SECT points to an array of bytes, which are the contents
895 of a `note' from a core file which BFD thinks might contain
896 register contents. CORE_REG_SIZE is its size.
898 WHICH says which register set corelow suspects this is:
899 0 --- the general-purpose register set, in elf_gregset_t format
900 2 --- the floating-point register set, in elf_fpregset_t format
901 3 --- the extended floating-point register set, in struct
902 user_xfpregs_struct format
904 REG_ADDR isn't used on Linux. */
907 fetch_core_registers (char *core_reg_sect
, unsigned core_reg_size
,
908 int which
, CORE_ADDR reg_addr
)
910 elf_gregset_t gregset
;
911 elf_fpregset_t fpregset
;
916 if (core_reg_size
!= sizeof (gregset
))
917 warning ("Wrong size gregset in core file.");
920 memcpy (&gregset
, core_reg_sect
, sizeof (gregset
));
921 supply_gregset (&gregset
);
926 if (core_reg_size
!= sizeof (fpregset
))
927 warning ("Wrong size fpregset in core file.");
930 memcpy (&fpregset
, core_reg_sect
, sizeof (fpregset
));
931 supply_fpregset (&fpregset
);
935 #ifdef HAVE_PTRACE_GETXFPREGS
937 struct user_xfpregs_struct xfpregset
;
940 if (core_reg_size
!= sizeof (xfpregset
))
941 warning ("Wrong size user_xfpregs_struct in core file.");
944 memcpy (&xfpregset
, core_reg_sect
, sizeof (xfpregset
));
945 supply_xfpregset (&xfpregset
);
952 /* We've covered all the kinds of registers we know about here,
953 so this must be something we wouldn't know what to do with
954 anyway. Just ignore it. */
960 /* The instruction for a Linux system call is:
964 static const unsigned char linux_syscall
[] = { 0xcd, 0x80 };
966 #define LINUX_SYSCALL_LEN (sizeof linux_syscall)
968 /* The system call number is stored in the %eax register. */
969 #define LINUX_SYSCALL_REGNUM 0 /* %eax */
971 /* We are specifically interested in the sigreturn and rt_sigreturn
974 #ifndef SYS_sigreturn
975 #define SYS_sigreturn 0x77
977 #ifndef SYS_rt_sigreturn
978 #define SYS_rt_sigreturn 0xad
981 /* Offset to saved processor flags, from <asm/sigcontext.h>. */
982 #define LINUX_SIGCONTEXT_EFLAGS_OFFSET (64)
984 /* Resume execution of the inferior process.
985 If STEP is nonzero, single-step it.
986 If SIGNAL is nonzero, give it that signal. */
989 child_resume (int pid
, int step
, enum target_signal signal
)
991 int request
= PTRACE_CONT
;
994 /* Resume all threads. */
995 /* I think this only gets used in the non-threaded case, where "resume
996 all threads" and "resume inferior_pid" are the same. */
1001 CORE_ADDR pc
= read_pc_pid (pid
);
1002 unsigned char buf
[LINUX_SYSCALL_LEN
];
1004 request
= PTRACE_SINGLESTEP
;
1006 /* Returning from a signal trampoline is done by calling a
1007 special system call (sigreturn or rt_sigreturn, see
1008 i386-linux-tdep.c for more information). This system call
1009 restores the registers that were saved when the signal was
1010 raised, including %eflags. That means that single-stepping
1011 won't work. Instead, we'll have to modify the signal context
1012 that's about to be restored, and set the trace flag there. */
1014 /* First check if PC is at a system call. */
1015 if (read_memory_nobpt (pc
, (char *) buf
, LINUX_SYSCALL_LEN
) == 0
1016 && memcmp (buf
, linux_syscall
, LINUX_SYSCALL_LEN
) == 0)
1018 int syscall
= read_register_pid (LINUX_SYSCALL_REGNUM
, pid
);
1020 /* Then check the system call number. */
1021 if (syscall
== SYS_sigreturn
|| syscall
== SYS_rt_sigreturn
)
1023 CORE_ADDR sp
= read_register (SP_REGNUM
);
1024 CORE_ADDR addr
= sp
;
1025 unsigned long int eflags
;
1027 if (syscall
== SYS_rt_sigreturn
)
1028 addr
= read_memory_integer (sp
+ 8, 4) + 20;
1030 /* Set the trace flag in the context that's about to be
1032 addr
+= LINUX_SIGCONTEXT_EFLAGS_OFFSET
;
1033 read_memory (addr
, (char *) &eflags
, 4);
1035 write_memory (addr
, (char *) &eflags
, 4);
1040 if (ptrace (request
, pid
, 0, target_signal_to_host (signal
)) == -1)
1041 perror_with_name ("ptrace");
1045 /* Calling functions in shared libraries. */
1046 /* FIXME: kettenis/2000-03-05: Doesn't this belong in a
1047 target-dependent file? The function
1048 `i386_linux_skip_solib_resolver' is mentioned in
1049 `config/i386/tm-linux.h'. */
1051 /* Find the minimal symbol named NAME, and return both the minsym
1052 struct and its objfile. This probably ought to be in minsym.c, but
1053 everything there is trying to deal with things like C++ and
1054 SOFUN_ADDRESS_MAYBE_TURQUOISE, ... Since this is so simple, it may
1055 be considered too special-purpose for general consumption. */
1057 static struct minimal_symbol
*
1058 find_minsym_and_objfile (char *name
, struct objfile
**objfile_p
)
1060 struct objfile
*objfile
;
1062 ALL_OBJFILES (objfile
)
1064 struct minimal_symbol
*msym
;
1066 ALL_OBJFILE_MSYMBOLS (objfile
, msym
)
1068 if (SYMBOL_NAME (msym
)
1069 && STREQ (SYMBOL_NAME (msym
), name
))
1071 *objfile_p
= objfile
;
1082 skip_hurd_resolver (CORE_ADDR pc
)
1084 /* The HURD dynamic linker is part of the GNU C library, so many
1085 GNU/Linux distributions use it. (All ELF versions, as far as I
1086 know.) An unresolved PLT entry points to "_dl_runtime_resolve",
1087 which calls "fixup" to patch the PLT, and then passes control to
1090 We look for the symbol `_dl_runtime_resolve', and find `fixup' in
1091 the same objfile. If we are at the entry point of `fixup', then
1092 we set a breakpoint at the return address (at the top of the
1093 stack), and continue.
1095 It's kind of gross to do all these checks every time we're
1096 called, since they don't change once the executable has gotten
1097 started. But this is only a temporary hack --- upcoming versions
1098 of Linux will provide a portable, efficient interface for
1099 debugging programs that use shared libraries. */
1101 struct objfile
*objfile
;
1102 struct minimal_symbol
*resolver
1103 = find_minsym_and_objfile ("_dl_runtime_resolve", &objfile
);
1107 struct minimal_symbol
*fixup
1108 = lookup_minimal_symbol ("fixup", 0, objfile
);
1110 if (fixup
&& SYMBOL_VALUE_ADDRESS (fixup
) == pc
)
1111 return (SAVED_PC_AFTER_CALL (get_current_frame ()));
1117 /* See the comments for SKIP_SOLIB_RESOLVER at the top of infrun.c.
1119 1) decides whether a PLT has sent us into the linker to resolve
1120 a function reference, and
1121 2) if so, tells us where to set a temporary breakpoint that will
1122 trigger when the dynamic linker is done. */
1125 i386_linux_skip_solib_resolver (CORE_ADDR pc
)
1129 /* Plug in functions for other kinds of resolvers here. */
1130 result
= skip_hurd_resolver (pc
);
1138 /* Register that we are able to handle Linux ELF core file formats. */
1140 static struct core_fns linux_elf_core_fns
=
1142 bfd_target_elf_flavour
, /* core_flavour */
1143 default_check_format
, /* check_format */
1144 default_core_sniffer
, /* core_sniffer */
1145 fetch_core_registers
, /* core_read_registers */
1150 _initialize_i386_linux_nat (void)
1152 add_core_fns (&linux_elf_core_fns
);