1 /* Native-dependent code for Linux running on i386's, for GDB.
3 This file is part of GDB.
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA. */
24 /* For i386_linux_skip_solib_resolver. */
30 #include <sys/ptrace.h>
32 #include <sys/procfs.h>
38 /* On Linux, threads are implemented as pseudo-processes, in which
39 case we may be tracing more than one process at a time. In that
40 case, inferior_pid will contain the main process ID and the
41 individual thread (process) ID mashed together. These macros are
42 used to separate them out. These definitions should be overridden
43 if thread support is included. */
45 #if !defined (PIDGET) /* Default definition for PIDGET/TIDGET. */
46 #define PIDGET(PID) PID
51 /* The register sets used in Linux ELF core-dumps are identical to the
52 register sets in `struct user' that is used for a.out core-dumps,
53 and is also used by `ptrace'. The corresponding types are
54 `elf_gregset_t' for the general-purpose registers (with
55 `elf_greg_t' the type of a single GP register) and `elf_fpregset_t'
56 for the floating-point registers.
58 Those types used to be available under the names `gregset_t' and
59 `fpregset_t' too, and this file used those names in the past. But
60 those names are now used for the register sets used in the
61 `mcontext_t' type, and have a different size and layout. */
63 /* Mapping between the general-purpose registers in `struct user'
64 format and GDB's register array layout. */
73 /* Which ptrace request retrieves which registers?
74 These apply to the corresponding SET requests as well. */
75 #define GETREGS_SUPPLIES(regno) \
76 (0 <= (regno) && (regno) <= 15)
77 #define GETFPREGS_SUPPLIES(regno) \
78 (FP0_REGNUM <= (regno) && (regno) <= LAST_FPU_CTRL_REGNUM)
79 #define GETXFPREGS_SUPPLIES(regno) \
80 (FP0_REGNUM <= (regno) && (regno) <= MXCSR_REGNUM)
82 /* Does the current host support the GETREGS request? */
83 int have_ptrace_getregs
=
84 #ifdef HAVE_PTRACE_GETREGS
91 /* Does the current host support the GETXFPREGS request? The header
92 file may or may not define it, and even if it is defined, the
93 kernel will return EIO if it's running on a pre-SSE processor.
95 PTRACE_GETXFPREGS is a Cygnus invention, since we wrote our own
96 Linux kernel patch for SSE support. That patch may or may not
97 actually make it into the official distribution. If you find that
98 years have gone by since this stuff was added, and Linux isn't
99 using PTRACE_GETXFPREGS, that means that our patch didn't make it,
100 and you can delete this, and the related code.
102 My instinct is to attach this to some architecture- or
103 target-specific data structure, but really, a particular GDB
104 process can only run on top of one kernel at a time. So it's okay
105 for this to be a simple variable. */
106 int have_ptrace_getxfpregs
=
107 #ifdef HAVE_PTRACE_GETXFPREGS
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
124 /* Default the type of the ptrace transfer to int. */
125 #ifndef PTRACE_XFER_TYPE
126 #define PTRACE_XFER_TYPE int
129 /* Registers we shouldn't try to fetch. */
130 #if !defined (CANNOT_FETCH_REGISTER)
131 #define CANNOT_FETCH_REGISTER(regno) 0
134 /* Fetch one register. */
137 fetch_register (regno
)
140 /* This isn't really an address. But ptrace thinks of it as one. */
142 char mess
[128]; /* For messages */
144 unsigned int offset
; /* Offset of registers within the u area. */
145 char buf
[MAX_REGISTER_RAW_SIZE
];
148 if (CANNOT_FETCH_REGISTER (regno
))
150 memset (buf
, '\0', REGISTER_RAW_SIZE (regno
)); /* Supply zeroes */
151 supply_register (regno
, buf
);
155 /* Overload thread id onto process id */
156 if ((tid
= TIDGET (inferior_pid
)) == 0)
157 tid
= inferior_pid
; /* no thread id, just use process id */
159 offset
= U_REGS_OFFSET
;
161 regaddr
= register_addr (regno
, offset
);
162 for (i
= 0; i
< REGISTER_RAW_SIZE (regno
); i
+= sizeof (PTRACE_XFER_TYPE
))
165 *(PTRACE_XFER_TYPE
*) & buf
[i
] = ptrace (PT_READ_U
, tid
,
166 (PTRACE_ARG3_TYPE
) regaddr
, 0);
167 regaddr
+= sizeof (PTRACE_XFER_TYPE
);
170 sprintf (mess
, "reading register %s (#%d)",
171 REGISTER_NAME (regno
), regno
);
172 perror_with_name (mess
);
175 supply_register (regno
, buf
);
178 /* Fetch register values from the inferior.
179 If REGNO is negative, do this for all registers.
180 Otherwise, REGNO specifies which register (so we can save time). */
183 old_fetch_inferior_registers (regno
)
188 fetch_register (regno
);
192 for (regno
= 0; regno
< ARCH_NUM_REGS
; regno
++)
194 fetch_register (regno
);
199 /* Registers we shouldn't try to store. */
200 #if !defined (CANNOT_STORE_REGISTER)
201 #define CANNOT_STORE_REGISTER(regno) 0
204 /* Store one register. */
207 store_register (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 (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 (regno
)
254 store_register (regno
);
258 for (regno
= 0; regno
< ARCH_NUM_REGS
; regno
++)
260 store_register (regno
);
266 /* Transfering the general-purpose registers between GDB, inferiors
269 /* Fill GDB's register array with the genereal-purpose register values
273 supply_gregset (elf_gregset_t
*gregsetp
)
275 elf_greg_t
*regp
= (elf_greg_t
*) gregsetp
;
278 for (regi
= 0; regi
< NUM_GREGS
; regi
++)
279 supply_register (regi
, (char *) (regp
+ regmap
[regi
]));
282 /* Convert the valid general-purpose register values in GDB's register
283 array to `struct user' format and store them in *GREGSETP. The
284 array VALID indicates which register values are valid. If VALID is
285 NULL, all registers are assumed to be valid. */
288 convert_to_gregset (elf_gregset_t
*gregsetp
, signed char *valid
)
290 elf_greg_t
*regp
= (elf_greg_t
*) gregsetp
;
293 for (regi
= 0; regi
< NUM_GREGS
; regi
++)
294 if (! valid
|| valid
[regi
])
295 *(regp
+ regmap
[regi
]) = * (int *) ®isters
[REGISTER_BYTE (regi
)];
298 /* Fill register REGNO (if it is a general-purpose register) in
299 *GREGSETPS with the value in GDB's register array. If REGNO is -1,
300 do this for all registers. */
302 fill_gregset (elf_gregset_t
*gregsetp
, int regno
)
306 convert_to_gregset (gregsetp
, NULL
);
310 if (GETREGS_SUPPLIES (regno
))
312 signed char valid
[NUM_GREGS
];
314 memset (valid
, 0, sizeof (valid
));
317 convert_to_gregset (gregsetp
, valid
);
321 #ifdef HAVE_PTRACE_GETREGS
323 /* Fetch all general-purpose registers from process/thread TID and
324 store their values in GDB's register array. */
332 ret
= ptrace (PTRACE_GETREGS
, tid
, 0, (int) ®s
);
337 /* The kernel we're running on doesn't support the GETREGS
338 request. Reset `have_ptrace_getregs'. */
339 have_ptrace_getregs
= 0;
343 warning ("Couldn't get registers.");
347 supply_gregset (®s
);
350 /* Store all valid general-purpose registers in GDB's register array
351 into the process/thread specified by TID. */
359 ret
= ptrace (PTRACE_GETREGS
, tid
, 0, (int) ®s
);
362 warning ("Couldn't get registers.");
366 convert_to_gregset (®s
, register_valid
);
368 ret
= ptrace (PTRACE_SETREGS
, tid
, 0, (int) ®s
);
371 warning ("Couldn't write registers.");
378 static void fetch_regs (int tid
) {}
379 static void store_regs (int tid
) {}
384 /* Transfering floating-point registers between GDB, inferiors and cores. */
386 /* What is the address of st(N) within the floating-point register set F? */
387 #define FPREG_ADDR(f, n) ((char *) &(f)->st_space + (n) * 10)
389 /* Fill GDB's register array with the floating-point register values in
393 supply_fpregset (elf_fpregset_t
*fpregsetp
)
398 /* Supply the floating-point registers. */
399 for (reg
= 0; reg
< 8; reg
++)
400 supply_register (FP0_REGNUM
+ reg
, FPREG_ADDR (fpregsetp
, reg
));
402 /* We have to mask off the reserved bits in *FPREGSETP before
403 storing the values in GDB's register file. */
404 #define supply(REGNO, MEMBER) \
405 l = fpregsetp->MEMBER & 0xffff; \
406 supply_register (REGNO, (char *) &l)
408 supply (FCTRL_REGNUM
, cwd
);
409 supply (FSTAT_REGNUM
, swd
);
410 supply (FTAG_REGNUM
, twd
);
411 supply_register (FCOFF_REGNUM
, (char *) &fpregsetp
->fip
);
412 supply (FDS_REGNUM
, fos
);
413 supply_register (FDOFF_REGNUM
, (char *) &fpregsetp
->foo
);
417 /* Extract the code segment and opcode from the "fcs" member. */
418 l
= fpregsetp
->fcs
& 0xffff;
419 supply_register (FCS_REGNUM
, (char *) &l
);
421 l
= (fpregsetp
->fcs
>> 16) & ((1 << 11) - 1);
422 supply_register (FOP_REGNUM
, (char *) &l
);
425 /* Convert the valid floating-point register values in GDB's register
426 array to `struct user' format and store them in *FPREGSETP. The
427 array VALID indicates which register values are valid. If VALID is
428 NULL, all registers are assumed to be valid. */
431 convert_to_fpregset (elf_fpregset_t
*fpregsetp
, signed char *valid
)
435 /* Fill in the floating-point registers. */
436 for (reg
= 0; reg
< 8; reg
++)
437 if (!valid
|| valid
[reg
])
438 memcpy (FPREG_ADDR (fpregsetp
, reg
),
439 ®isters
[REGISTER_BYTE (FP0_REGNUM
+ reg
)],
440 REGISTER_RAW_SIZE(FP0_REGNUM
+ reg
));
442 /* We're not supposed to touch the reserved bits in *FPREGSETP. */
444 #define fill(MEMBER, REGNO) \
445 if (! valid || valid[(REGNO)]) \
447 = ((fpregsetp->MEMBER & ~0xffff) \
448 | (* (int *) ®isters[REGISTER_BYTE (REGNO)] & 0xffff))
450 #define fill_register(MEMBER, REGNO) \
451 if (! valid || valid[(REGNO)]) \
452 memcpy (&fpregsetp->MEMBER, ®isters[REGISTER_BYTE (REGNO)], \
453 sizeof (fpregsetp->MEMBER))
455 fill (cwd
, FCTRL_REGNUM
);
456 fill (swd
, FSTAT_REGNUM
);
457 fill (twd
, FTAG_REGNUM
);
458 fill_register (fip
, FCOFF_REGNUM
);
459 fill (foo
, FDOFF_REGNUM
);
460 fill_register (fos
, FDS_REGNUM
);
465 if (! valid
|| valid
[FCS_REGNUM
])
467 = ((fpregsetp
->fcs
& ~0xffff)
468 | (* (int *) ®isters
[REGISTER_BYTE (FCS_REGNUM
)] & 0xffff));
470 if (! valid
|| valid
[FOP_REGNUM
])
472 = ((fpregsetp
->fcs
& 0xffff)
473 | ((*(int *) ®isters
[REGISTER_BYTE (FOP_REGNUM
)] & ((1 << 11) - 1))
477 /* Fill register REGNO (if it is a floating-point register) in
478 *FPREGSETP with the value in GDB's register array. If REGNO is -1,
479 do this for all registers. */
482 fill_fpregset (elf_fpregset_t
*fpregsetp
, int regno
)
486 convert_to_fpregset (fpregsetp
, NULL
);
490 if (GETFPREGS_SUPPLIES(regno
))
492 signed char valid
[MAX_NUM_REGS
];
494 memset (valid
, 0, sizeof (valid
));
497 convert_to_fpregset (fpregsetp
, valid
);
501 #ifdef HAVE_PTRACE_GETREGS
503 /* Fetch all floating-point registers from process/thread TID and store
504 thier values in GDB's register array. */
507 fetch_fpregs (int tid
)
509 elf_fpregset_t fpregs
;
512 ret
= ptrace (PTRACE_GETFPREGS
, tid
, 0, (int) &fpregs
);
515 warning ("Couldn't get floating point status.");
519 supply_fpregset (&fpregs
);
522 /* Store all valid floating-point registers in GDB's register array
523 into the process/thread specified by TID. */
526 store_fpregs (int tid
)
528 elf_fpregset_t fpregs
;
531 ret
= ptrace (PTRACE_GETFPREGS
, tid
, 0, (int) &fpregs
);
534 warning ("Couldn't get floating point status.");
538 convert_to_fpregset (&fpregs
, register_valid
);
540 ret
= ptrace (PTRACE_SETFPREGS
, tid
, 0, (int) &fpregs
);
543 warning ("Couldn't write floating point status.");
550 static void fetch_fpregs (int tid
) {}
551 static void store_fpregs (int tid
) {}
556 /* Transfering floating-point and SSE registers to and from GDB. */
558 /* PTRACE_GETXFPREGS is a Cygnus invention, since we wrote our own
559 Linux kernel patch for SSE support. That patch may or may not
560 actually make it into the official distribution. If you find that
561 years have gone by since this code was added, and Linux isn't using
562 PTRACE_GETXFPREGS, that means that our patch didn't make it, and
563 you can delete this code. */
565 #ifdef HAVE_PTRACE_GETXFPREGS
567 /* Fill GDB's register array with the floating-point and SSE register
568 values in *XFPREGS. */
571 supply_xfpregset (struct user_xfpregs_struct
*xfpregs
)
575 /* Supply the floating-point registers. */
576 for (reg
= 0; reg
< 8; reg
++)
577 supply_register (FP0_REGNUM
+ reg
, (char *) &xfpregs
->st_space
[reg
]);
580 supply_register (FCTRL_REGNUM
, (char *) &xfpregs
->cwd
);
581 supply_register (FSTAT_REGNUM
, (char *) &xfpregs
->swd
);
582 supply_register (FTAG_REGNUM
, (char *) &xfpregs
->twd
);
583 supply_register (FCOFF_REGNUM
, (char *) &xfpregs
->fip
);
584 supply_register (FDS_REGNUM
, (char *) &xfpregs
->fos
);
585 supply_register (FDOFF_REGNUM
, (char *) &xfpregs
->foo
);
587 /* Extract the code segment and opcode from the "fcs" member. */
591 l
= xfpregs
->fcs
& 0xffff;
592 supply_register (FCS_REGNUM
, (char *) &l
);
594 l
= (xfpregs
->fcs
>> 16) & ((1 << 11) - 1);
595 supply_register (FOP_REGNUM
, (char *) &l
);
599 /* Supply the SSE registers. */
600 for (reg
= 0; reg
< 8; reg
++)
601 supply_register (XMM0_REGNUM
+ reg
, (char *) &xfpregs
->xmm_space
[reg
]);
602 supply_register (MXCSR_REGNUM
, (char *) &xfpregs
->mxcsr
);
605 /* Convert the valid floating-point and SSE registers in GDB's
606 register array to `struct user' format and store them in *XFPREGS.
607 The array VALID indicates which registers are valid. If VALID is
608 NULL, all registers are assumed to be valid. */
611 convert_to_xfpregset (struct user_xfpregs_struct
*xfpregs
,
616 /* Fill in the floating-point registers. */
617 for (reg
= 0; reg
< 8; reg
++)
618 if (!valid
|| valid
[reg
])
619 memcpy (&xfpregs
->st_space
[reg
],
620 ®isters
[REGISTER_BYTE (FP0_REGNUM
+ reg
)],
621 REGISTER_RAW_SIZE(FP0_REGNUM
+ reg
));
623 #define fill(MEMBER, REGNO) \
624 if (! valid || valid[(REGNO)]) \
625 memcpy (&xfpregs->MEMBER, ®isters[REGISTER_BYTE (REGNO)], \
626 sizeof (xfpregs->MEMBER))
628 fill (cwd
, FCTRL_REGNUM
);
629 fill (swd
, FSTAT_REGNUM
);
630 fill (twd
, FTAG_REGNUM
);
631 fill (fip
, FCOFF_REGNUM
);
632 fill (foo
, FDOFF_REGNUM
);
633 fill (fos
, FDS_REGNUM
);
637 if (! valid
|| valid
[FCS_REGNUM
])
639 = ((xfpregs
->fcs
& ~0xffff)
640 | (* (int *) ®isters
[REGISTER_BYTE (FCS_REGNUM
)] & 0xffff));
642 if (! valid
|| valid
[FOP_REGNUM
])
644 = ((xfpregs
->fcs
& 0xffff)
645 | ((*(int *) ®isters
[REGISTER_BYTE (FOP_REGNUM
)] & ((1 << 11) - 1))
648 /* Fill in the XMM registers. */
649 for (reg
= 0; reg
< 8; reg
++)
650 if (! valid
|| valid
[reg
])
651 memcpy (&xfpregs
->xmm_space
[reg
],
652 ®isters
[REGISTER_BYTE (XMM0_REGNUM
+ reg
)],
653 REGISTER_RAW_SIZE (XMM0_REGNUM
+ reg
));
656 /* Fetch all registers covered by the PTRACE_SETXFPREGS request from
657 process/thread TID and store their values in GDB's register array.
658 Return non-zero if successful, zero otherwise. */
661 fetch_xfpregs (int tid
)
663 struct user_xfpregs_struct xfpregs
;
666 if (! have_ptrace_getxfpregs
)
669 ret
= ptrace (PTRACE_GETXFPREGS
, tid
, 0, &xfpregs
);
674 have_ptrace_getxfpregs
= 0;
678 warning ("Couldn't read floating-point and SSE registers.");
682 supply_xfpregset (&xfpregs
);
686 /* Store all valid registers in GDB's register array covered by the
687 PTRACE_SETXFPREGS request into the process/thread specified by TID.
688 Return non-zero if successful, zero otherwise. */
691 store_xfpregs (int tid
)
693 struct user_xfpregs_struct xfpregs
;
696 if (! have_ptrace_getxfpregs
)
699 ret
= ptrace (PTRACE_GETXFPREGS
, tid
, 0, &xfpregs
);
704 have_ptrace_getxfpregs
= 0;
708 warning ("Couldn't read floating-point and SSE registers.");
712 convert_to_xfpregset (&xfpregs
, register_valid
);
714 if (ptrace (PTRACE_SETXFPREGS
, tid
, 0, &xfpregs
) < 0)
716 warning ("Couldn't write floating-point and SSE registers.");
723 /* Fill the XMM registers in the register array with dummy values. For
724 cases where we don't have access to the XMM registers. I think
725 this is cleaner than printing a warning. For a cleaner solution,
726 we should gdbarchify the i386 family. */
729 dummy_sse_values (void)
731 /* C doesn't have a syntax for NaN's, so write it out as an array of
733 static long dummy
[4] = { 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff };
734 static long mxcsr
= 0x1f80;
737 for (reg
= 0; reg
< 8; reg
++)
738 supply_register (XMM0_REGNUM
+ reg
, (char *) dummy
);
739 supply_register (MXCSR_REGNUM
, (char *) &mxcsr
);
744 /* Stub versions of the above routines, for systems that don't have
745 PTRACE_GETXFPREGS. */
746 static int store_xfpregs (int tid
) { return 0; }
747 static int fetch_xfpregs (int tid
) { return 0; }
748 static void dummy_sse_values (void) {}
753 /* Transferring arbitrary registers between GDB and inferior. */
755 /* Fetch register REGNO from the child process. If REGNO is -1, do
756 this for all registers (including the floating point and SSE
760 fetch_inferior_registers (int regno
)
764 /* Use the old method of peeking around in `struct user' if the
765 GETREGS request isn't available. */
766 if (! have_ptrace_getregs
)
768 old_fetch_inferior_registers (regno
);
772 /* Linux LWP ID's are process ID's. */
773 if ((tid
= TIDGET (inferior_pid
)) == 0)
774 tid
= inferior_pid
; /* Not a threaded program. */
776 /* Use the PTRACE_GETXFPREGS request whenever possible, since it
777 transfers more registers in one system call, and we'll cache the
778 results. But remember that fetch_xfpregs can fail, and return
784 /* The call above might reset `have_ptrace_getregs'. */
785 if (! have_ptrace_getregs
)
787 old_fetch_inferior_registers (-1);
791 if (fetch_xfpregs (tid
))
797 if (GETREGS_SUPPLIES (regno
))
803 if (GETXFPREGS_SUPPLIES (regno
))
805 if (fetch_xfpregs (tid
))
808 /* Either our processor or our kernel doesn't support the SSE
809 registers, so read the FP registers in the traditional way,
810 and fill the SSE registers with dummy values. It would be
811 more graceful to handle differences in the register set using
812 gdbarch. Until then, this will at least make things work
819 internal_error ("i386-linux-nat.c (fetch_inferior_registers): "
820 "got request for bad register number %d", regno
);
823 /* Store register REGNO back into the child process. If REGNO is -1,
824 do this for all registers (including the floating point and SSE
827 store_inferior_registers (int regno
)
831 /* Use the old method of poking around in `struct user' if the
832 SETREGS request isn't available. */
833 if (! have_ptrace_getregs
)
835 old_store_inferior_registers (regno
);
839 /* Linux LWP ID's are process ID's. */
840 if ((tid
= TIDGET (inferior_pid
)) == 0)
841 tid
= inferior_pid
; /* Not a threaded program. */
843 /* Use the PTRACE_SETXFPREGS requests whenever possibl, since it
844 transfers more registers in one system call. But remember that
845 store_xfpregs can fail, and return zero. */
849 if (store_xfpregs (tid
))
855 if (GETREGS_SUPPLIES (regno
))
861 if (GETXFPREGS_SUPPLIES (regno
))
863 if (store_xfpregs (tid
))
866 /* Either our processor or our kernel doesn't support the SSE
867 registers, so just write the FP registers in the traditional
873 internal_error ("Got request to store bad register number %d.", regno
);
877 /* Interpreting register set info found in core files. */
879 /* Provide registers to GDB from a core file.
881 (We can't use the generic version of this function in
882 core-regset.c, because Linux has *three* different kinds of
883 register set notes. core-regset.c would have to call
884 supply_xfpregset, which most platforms don't have.)
886 CORE_REG_SECT points to an array of bytes, which are the contents
887 of a `note' from a core file which BFD thinks might contain
888 register contents. CORE_REG_SIZE is its size.
890 WHICH says which register set corelow suspects this is:
891 0 --- the general-purpose register set, in elf_gregset_t format
892 2 --- the floating-point register set, in elf_fpregset_t format
893 3 --- the extended floating-point register set, in struct
894 user_xfpregs_struct format
896 REG_ADDR isn't used on Linux. */
899 fetch_core_registers (char *core_reg_sect
, unsigned core_reg_size
,
900 int which
, CORE_ADDR reg_addr
)
902 elf_gregset_t gregset
;
903 elf_fpregset_t fpregset
;
908 if (core_reg_size
!= sizeof (gregset
))
909 warning ("Wrong size gregset in core file.");
912 memcpy (&gregset
, core_reg_sect
, sizeof (gregset
));
913 supply_gregset (&gregset
);
918 if (core_reg_size
!= sizeof (fpregset
))
919 warning ("Wrong size fpregset in core file.");
922 memcpy (&fpregset
, core_reg_sect
, sizeof (fpregset
));
923 supply_fpregset (&fpregset
);
927 #ifdef HAVE_PTRACE_GETXFPREGS
929 struct user_xfpregs_struct xfpregset
;
932 if (core_reg_size
!= sizeof (xfpregset
))
933 warning ("Wrong size user_xfpregs_struct in core file.");
936 memcpy (&xfpregset
, core_reg_sect
, sizeof (xfpregset
));
937 supply_xfpregset (&xfpregset
);
944 /* We've covered all the kinds of registers we know about here,
945 so this must be something we wouldn't know what to do with
946 anyway. Just ignore it. */
952 /* Calling functions in shared libraries. */
953 /* FIXME: kettenis/2000-03-05: Doesn't this belong in a
954 target-dependent file? The function
955 `i386_linux_skip_solib_resolver' is mentioned in
956 `config/i386/tm-linux.h'. */
958 /* Find the minimal symbol named NAME, and return both the minsym
959 struct and its objfile. This probably ought to be in minsym.c, but
960 everything there is trying to deal with things like C++ and
961 SOFUN_ADDRESS_MAYBE_TURQUOISE, ... Since this is so simple, it may
962 be considered too special-purpose for general consumption. */
964 static struct minimal_symbol
*
965 find_minsym_and_objfile (char *name
, struct objfile
**objfile_p
)
967 struct objfile
*objfile
;
969 ALL_OBJFILES (objfile
)
971 struct minimal_symbol
*msym
;
973 ALL_OBJFILE_MSYMBOLS (objfile
, msym
)
975 if (SYMBOL_NAME (msym
)
976 && STREQ (SYMBOL_NAME (msym
), name
))
978 *objfile_p
= objfile
;
989 skip_hurd_resolver (CORE_ADDR pc
)
991 /* The HURD dynamic linker is part of the GNU C library, so many
992 GNU/Linux distributions use it. (All ELF versions, as far as I
993 know.) An unresolved PLT entry points to "_dl_runtime_resolve",
994 which calls "fixup" to patch the PLT, and then passes control to
997 We look for the symbol `_dl_runtime_resolve', and find `fixup' in
998 the same objfile. If we are at the entry point of `fixup', then
999 we set a breakpoint at the return address (at the top of the
1000 stack), and continue.
1002 It's kind of gross to do all these checks every time we're
1003 called, since they don't change once the executable has gotten
1004 started. But this is only a temporary hack --- upcoming versions
1005 of Linux will provide a portable, efficient interface for
1006 debugging programs that use shared libraries. */
1008 struct objfile
*objfile
;
1009 struct minimal_symbol
*resolver
1010 = find_minsym_and_objfile ("_dl_runtime_resolve", &objfile
);
1014 struct minimal_symbol
*fixup
1015 = lookup_minimal_symbol ("fixup", 0, objfile
);
1017 if (fixup
&& SYMBOL_VALUE_ADDRESS (fixup
) == pc
)
1018 return (SAVED_PC_AFTER_CALL (get_current_frame ()));
1024 /* See the comments for SKIP_SOLIB_RESOLVER at the top of infrun.c.
1026 1) decides whether a PLT has sent us into the linker to resolve
1027 a function reference, and
1028 2) if so, tells us where to set a temporary breakpoint that will
1029 trigger when the dynamic linker is done. */
1032 i386_linux_skip_solib_resolver (CORE_ADDR pc
)
1036 /* Plug in functions for other kinds of resolvers here. */
1037 result
= skip_hurd_resolver (pc
);
1045 /* Recognizing signal handler frames. */
1047 /* Linux has two flavors of signals. Normal signal handlers, and
1048 "realtime" (RT) signals. The RT signals can provide additional
1049 information to the signal handler if the SA_SIGINFO flag is set
1050 when establishing a signal handler using `sigaction'. It is not
1051 unlikely that future versions of Linux will support SA_SIGINFO for
1052 normal signals too. */
1054 /* When the i386 Linux kernel calls a signal handler and the
1055 SA_RESTORER flag isn't set, the return address points to a bit of
1056 code on the stack. This function returns whether the PC appears to
1057 be within this bit of code.
1059 The instruction sequence for normal signals is
1063 or 0x58 0xb8 0x77 0x00 0x00 0x00 0xcd 0x80.
1065 Checking for the code sequence should be somewhat reliable, because
1066 the effect is to call the system call sigreturn. This is unlikely
1067 to occur anywhere other than a signal trampoline.
1069 It kind of sucks that we have to read memory from the process in
1070 order to identify a signal trampoline, but there doesn't seem to be
1071 any other way. The IN_SIGTRAMP macro in tm-linux.h arranges to
1072 only call us if no function name could be identified, which should
1073 be the case since the code is on the stack.
1075 Detection of signal trampolines for handlers that set the
1076 SA_RESTORER flag is in general not possible. Unfortunately this is
1077 what the GNU C Library has been doing for quite some time now.
1078 However, as of version 2.1.2, the GNU C Library uses signal
1079 trampolines (named __restore and __restore_rt) that are identical
1080 to the ones used by the kernel. Therefore, these trampolines are
1083 #define LINUX_SIGTRAMP_INSN0 (0x58) /* pop %eax */
1084 #define LINUX_SIGTRAMP_OFFSET0 (0)
1085 #define LINUX_SIGTRAMP_INSN1 (0xb8) /* mov $NNNN,%eax */
1086 #define LINUX_SIGTRAMP_OFFSET1 (1)
1087 #define LINUX_SIGTRAMP_INSN2 (0xcd) /* int */
1088 #define LINUX_SIGTRAMP_OFFSET2 (6)
1090 static const unsigned char linux_sigtramp_code
[] =
1092 LINUX_SIGTRAMP_INSN0
, /* pop %eax */
1093 LINUX_SIGTRAMP_INSN1
, 0x77, 0x00, 0x00, 0x00, /* mov $0x77,%eax */
1094 LINUX_SIGTRAMP_INSN2
, 0x80 /* int $0x80 */
1097 #define LINUX_SIGTRAMP_LEN (sizeof linux_sigtramp_code)
1099 /* If PC is in a sigtramp routine, return the address of the start of
1100 the routine. Otherwise, return 0. */
1103 i386_linux_sigtramp_start (CORE_ADDR pc
)
1105 unsigned char buf
[LINUX_SIGTRAMP_LEN
];
1107 /* We only recognize a signal trampoline if PC is at the start of
1108 one of the three instructions. We optimize for finding the PC at
1109 the start, as will be the case when the trampoline is not the
1110 first frame on the stack. We assume that in the case where the
1111 PC is not at the start of the instruction sequence, there will be
1112 a few trailing readable bytes on the stack. */
1114 if (read_memory_nobpt (pc
, (char *) buf
, LINUX_SIGTRAMP_LEN
) != 0)
1117 if (buf
[0] != LINUX_SIGTRAMP_INSN0
)
1123 case LINUX_SIGTRAMP_INSN1
:
1124 adjust
= LINUX_SIGTRAMP_OFFSET1
;
1126 case LINUX_SIGTRAMP_INSN2
:
1127 adjust
= LINUX_SIGTRAMP_OFFSET2
;
1135 if (read_memory_nobpt (pc
, (char *) buf
, LINUX_SIGTRAMP_LEN
) != 0)
1139 if (memcmp (buf
, linux_sigtramp_code
, LINUX_SIGTRAMP_LEN
) != 0)
1145 /* This function does the same for RT signals. Here the instruction
1149 or 0xb8 0xad 0x00 0x00 0x00 0xcd 0x80.
1151 The effect is to call the system call rt_sigreturn. */
1153 #define LINUX_RT_SIGTRAMP_INSN0 (0xb8) /* mov $NNNN,%eax */
1154 #define LINUX_RT_SIGTRAMP_OFFSET0 (0)
1155 #define LINUX_RT_SIGTRAMP_INSN1 (0xcd) /* int */
1156 #define LINUX_RT_SIGTRAMP_OFFSET1 (5)
1158 static const unsigned char linux_rt_sigtramp_code
[] =
1160 LINUX_RT_SIGTRAMP_INSN0
, 0xad, 0x00, 0x00, 0x00, /* mov $0xad,%eax */
1161 LINUX_RT_SIGTRAMP_INSN1
, 0x80 /* int $0x80 */
1164 #define LINUX_RT_SIGTRAMP_LEN (sizeof linux_rt_sigtramp_code)
1166 /* If PC is in a RT sigtramp routine, return the address of the start
1167 of the routine. Otherwise, return 0. */
1170 i386_linux_rt_sigtramp_start (CORE_ADDR pc
)
1172 unsigned char buf
[LINUX_RT_SIGTRAMP_LEN
];
1174 /* We only recognize a signal trampoline if PC is at the start of
1175 one of the two instructions. We optimize for finding the PC at
1176 the start, as will be the case when the trampoline is not the
1177 first frame on the stack. We assume that in the case where the
1178 PC is not at the start of the instruction sequence, there will be
1179 a few trailing readable bytes on the stack. */
1181 if (read_memory_nobpt (pc
, (char *) buf
, LINUX_RT_SIGTRAMP_LEN
) != 0)
1184 if (buf
[0] != LINUX_RT_SIGTRAMP_INSN0
)
1186 if (buf
[0] != LINUX_RT_SIGTRAMP_INSN1
)
1189 pc
-= LINUX_RT_SIGTRAMP_OFFSET1
;
1191 if (read_memory_nobpt (pc
, (char *) buf
, LINUX_RT_SIGTRAMP_LEN
) != 0)
1195 if (memcmp (buf
, linux_rt_sigtramp_code
, LINUX_RT_SIGTRAMP_LEN
) != 0)
1201 /* Return whether PC is in a Linux sigtramp routine. */
1204 i386_linux_in_sigtramp (CORE_ADDR pc
, char *name
)
1207 return STREQ ("__restore", name
) || STREQ ("__restore_rt", name
);
1209 return (i386_linux_sigtramp_start (pc
) != 0
1210 || i386_linux_rt_sigtramp_start (pc
) != 0);
1213 /* Assuming FRAME is for a Linux sigtramp routine, return the address
1214 of the associated sigcontext structure. */
1217 i386_linux_sigcontext_addr (struct frame_info
*frame
)
1221 pc
= i386_linux_sigtramp_start (frame
->pc
);
1227 /* If this isn't the top frame, the next frame must be for the
1228 signal handler itself. The sigcontext structure lives on
1229 the stack, right after the signum argument. */
1230 return frame
->next
->frame
+ 12;
1232 /* This is the top frame. We'll have to find the address of the
1233 sigcontext structure by looking at the stack pointer. Keep
1234 in mind that the first instruction of the sigtramp code is
1235 "pop %eax". If the PC is at this instruction, adjust the
1236 returned value accordingly. */
1237 sp
= read_register (SP_REGNUM
);
1238 if (pc
== frame
->pc
)
1243 pc
= i386_linux_rt_sigtramp_start (frame
->pc
);
1247 /* If this isn't the top frame, the next frame must be for the
1248 signal handler itself. The sigcontext structure is part of
1249 the user context. A pointer to the user context is passed
1250 as the third argument to the signal handler. */
1251 return read_memory_integer (frame
->next
->frame
+ 16, 4) + 20;
1253 /* This is the top frame. Again, use the stack pointer to find
1254 the address of the sigcontext structure. */
1255 return read_memory_integer (read_register (SP_REGNUM
) + 8, 4) + 20;
1258 error ("Couldn't recognize signal trampoline.");
1262 /* Offset to saved PC in sigcontext, from <asm/sigcontext.h>. */
1263 #define LINUX_SIGCONTEXT_PC_OFFSET (56)
1265 /* Assuming FRAME is for a Linux sigtramp routine, return the saved
1269 i386_linux_sigtramp_saved_pc (struct frame_info
*frame
)
1272 addr
= i386_linux_sigcontext_addr (frame
);
1273 return read_memory_integer (addr
+ LINUX_SIGCONTEXT_PC_OFFSET
, 4);
1276 /* Offset to saved SP in sigcontext, from <asm/sigcontext.h>. */
1277 #define LINUX_SIGCONTEXT_SP_OFFSET (28)
1279 /* Assuming FRAME is for a Linux sigtramp routine, return the saved
1283 i386_linux_sigtramp_saved_sp (struct frame_info
*frame
)
1286 addr
= i386_linux_sigcontext_addr (frame
);
1287 return read_memory_integer (addr
+ LINUX_SIGCONTEXT_SP_OFFSET
, 4);
1291 /* Register that we are able to handle Linux ELF core file formats. */
1293 static struct core_fns linux_elf_core_fns
=
1295 bfd_target_elf_flavour
, /* core_flavour */
1296 default_check_format
, /* check_format */
1297 default_core_sniffer
, /* core_sniffer */
1298 fetch_core_registers
, /* core_read_registers */
1303 _initialize_i386_linux_nat ()
1305 add_core_fns (&linux_elf_core_fns
);