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 (regno
)
152 /* This isn't really an address. But ptrace thinks of it as one. */
154 char mess
[128]; /* For messages */
156 unsigned int offset
; /* Offset of registers within the u area. */
157 char buf
[MAX_REGISTER_RAW_SIZE
];
160 if (CANNOT_FETCH_REGISTER (regno
))
162 memset (buf
, '\0', REGISTER_RAW_SIZE (regno
)); /* Supply zeroes */
163 supply_register (regno
, buf
);
167 /* Overload thread id onto process id */
168 if ((tid
= TIDGET (inferior_pid
)) == 0)
169 tid
= inferior_pid
; /* no thread id, just use process id */
171 offset
= U_REGS_OFFSET
;
173 regaddr
= register_addr (regno
, offset
);
174 for (i
= 0; i
< REGISTER_RAW_SIZE (regno
); i
+= sizeof (PTRACE_XFER_TYPE
))
177 *(PTRACE_XFER_TYPE
*) & buf
[i
] = ptrace (PT_READ_U
, tid
,
178 (PTRACE_ARG3_TYPE
) regaddr
, 0);
179 regaddr
+= sizeof (PTRACE_XFER_TYPE
);
182 sprintf (mess
, "reading register %s (#%d)",
183 REGISTER_NAME (regno
), regno
);
184 perror_with_name (mess
);
187 supply_register (regno
, buf
);
190 /* Fetch register values from the inferior.
191 If REGNO is negative, do this for all registers.
192 Otherwise, REGNO specifies which register (so we can save time). */
195 old_fetch_inferior_registers (regno
)
200 fetch_register (regno
);
204 for (regno
= 0; regno
< ARCH_NUM_REGS
; regno
++)
206 fetch_register (regno
);
211 /* Registers we shouldn't try to store. */
212 #if !defined (CANNOT_STORE_REGISTER)
213 #define CANNOT_STORE_REGISTER(regno) 0
216 /* Store one register. */
219 store_register (regno
)
222 /* This isn't really an address. But ptrace thinks of it as one. */
224 char mess
[128]; /* For messages */
226 unsigned int offset
; /* Offset of registers within the u area. */
229 if (CANNOT_STORE_REGISTER (regno
))
234 /* Overload thread id onto process id */
235 if ((tid
= TIDGET (inferior_pid
)) == 0)
236 tid
= inferior_pid
; /* no thread id, just use process id */
238 offset
= U_REGS_OFFSET
;
240 regaddr
= register_addr (regno
, offset
);
241 for (i
= 0; i
< REGISTER_RAW_SIZE (regno
); i
+= sizeof (PTRACE_XFER_TYPE
))
244 ptrace (PT_WRITE_U
, tid
, (PTRACE_ARG3_TYPE
) regaddr
,
245 *(PTRACE_XFER_TYPE
*) & registers
[REGISTER_BYTE (regno
) + i
]);
246 regaddr
+= sizeof (PTRACE_XFER_TYPE
);
249 sprintf (mess
, "writing register %s (#%d)",
250 REGISTER_NAME (regno
), regno
);
251 perror_with_name (mess
);
256 /* Store our register values back into the inferior.
257 If REGNO is negative, do this for all registers.
258 Otherwise, REGNO specifies which register (so we can save time). */
261 old_store_inferior_registers (regno
)
266 store_register (regno
);
270 for (regno
= 0; regno
< ARCH_NUM_REGS
; regno
++)
272 store_register (regno
);
278 /* Transfering the general-purpose registers between GDB, inferiors
281 /* Fill GDB's register array with the genereal-purpose register values
285 supply_gregset (elf_gregset_t
*gregsetp
)
287 elf_greg_t
*regp
= (elf_greg_t
*) gregsetp
;
290 for (regi
= 0; regi
< NUM_GREGS
; regi
++)
291 supply_register (regi
, (char *) (regp
+ regmap
[regi
]));
294 /* Convert the valid general-purpose register values in GDB's register
295 array to `struct user' format and store them in *GREGSETP. The
296 array VALID indicates which register values are valid. If VALID is
297 NULL, all registers are assumed to be valid. */
300 convert_to_gregset (elf_gregset_t
*gregsetp
, signed char *valid
)
302 elf_greg_t
*regp
= (elf_greg_t
*) gregsetp
;
305 for (regi
= 0; regi
< NUM_GREGS
; regi
++)
306 if (! valid
|| valid
[regi
])
307 *(regp
+ regmap
[regi
]) = * (int *) ®isters
[REGISTER_BYTE (regi
)];
310 /* Fill register REGNO (if it is a general-purpose register) in
311 *GREGSETPS with the value in GDB's register array. If REGNO is -1,
312 do this for all registers. */
314 fill_gregset (elf_gregset_t
*gregsetp
, int regno
)
318 convert_to_gregset (gregsetp
, NULL
);
322 if (GETREGS_SUPPLIES (regno
))
324 signed char valid
[NUM_GREGS
];
326 memset (valid
, 0, sizeof (valid
));
329 convert_to_gregset (gregsetp
, valid
);
333 #ifdef HAVE_PTRACE_GETREGS
335 /* Fetch all general-purpose registers from process/thread TID and
336 store their values in GDB's register array. */
344 ret
= ptrace (PTRACE_GETREGS
, tid
, 0, (int) ®s
);
349 /* The kernel we're running on doesn't support the GETREGS
350 request. Reset `have_ptrace_getregs'. */
351 have_ptrace_getregs
= 0;
355 warning ("Couldn't get registers.");
359 supply_gregset (®s
);
362 /* Store all valid general-purpose registers in GDB's register array
363 into the process/thread specified by TID. */
371 ret
= ptrace (PTRACE_GETREGS
, tid
, 0, (int) ®s
);
374 warning ("Couldn't get registers.");
378 convert_to_gregset (®s
, register_valid
);
380 ret
= ptrace (PTRACE_SETREGS
, tid
, 0, (int) ®s
);
383 warning ("Couldn't write registers.");
390 static void fetch_regs (int tid
) {}
391 static void store_regs (int tid
) {}
396 /* Transfering floating-point registers between GDB, inferiors and cores. */
398 /* What is the address of st(N) within the floating-point register set F? */
399 #define FPREG_ADDR(f, n) ((char *) &(f)->st_space + (n) * 10)
401 /* Fill GDB's register array with the floating-point register values in
405 supply_fpregset (elf_fpregset_t
*fpregsetp
)
410 /* Supply the floating-point registers. */
411 for (reg
= 0; reg
< 8; reg
++)
412 supply_register (FP0_REGNUM
+ reg
, FPREG_ADDR (fpregsetp
, reg
));
414 /* We have to mask off the reserved bits in *FPREGSETP before
415 storing the values in GDB's register file. */
416 #define supply(REGNO, MEMBER) \
417 l = fpregsetp->MEMBER & 0xffff; \
418 supply_register (REGNO, (char *) &l)
420 supply (FCTRL_REGNUM
, cwd
);
421 supply (FSTAT_REGNUM
, swd
);
422 supply (FTAG_REGNUM
, twd
);
423 supply_register (FCOFF_REGNUM
, (char *) &fpregsetp
->fip
);
424 supply (FDS_REGNUM
, fos
);
425 supply_register (FDOFF_REGNUM
, (char *) &fpregsetp
->foo
);
429 /* Extract the code segment and opcode from the "fcs" member. */
430 l
= fpregsetp
->fcs
& 0xffff;
431 supply_register (FCS_REGNUM
, (char *) &l
);
433 l
= (fpregsetp
->fcs
>> 16) & ((1 << 11) - 1);
434 supply_register (FOP_REGNUM
, (char *) &l
);
437 /* Convert the valid floating-point register values in GDB's register
438 array to `struct user' format and store them in *FPREGSETP. The
439 array VALID indicates which register values are valid. If VALID is
440 NULL, all registers are assumed to be valid. */
443 convert_to_fpregset (elf_fpregset_t
*fpregsetp
, signed char *valid
)
447 /* Fill in the floating-point registers. */
448 for (reg
= 0; reg
< 8; reg
++)
449 if (!valid
|| valid
[reg
])
450 memcpy (FPREG_ADDR (fpregsetp
, reg
),
451 ®isters
[REGISTER_BYTE (FP0_REGNUM
+ reg
)],
452 REGISTER_RAW_SIZE(FP0_REGNUM
+ reg
));
454 /* We're not supposed to touch the reserved bits in *FPREGSETP. */
456 #define fill(MEMBER, REGNO) \
457 if (! valid || valid[(REGNO)]) \
459 = ((fpregsetp->MEMBER & ~0xffff) \
460 | (* (int *) ®isters[REGISTER_BYTE (REGNO)] & 0xffff))
462 #define fill_register(MEMBER, REGNO) \
463 if (! valid || valid[(REGNO)]) \
464 memcpy (&fpregsetp->MEMBER, ®isters[REGISTER_BYTE (REGNO)], \
465 sizeof (fpregsetp->MEMBER))
467 fill (cwd
, FCTRL_REGNUM
);
468 fill (swd
, FSTAT_REGNUM
);
469 fill (twd
, FTAG_REGNUM
);
470 fill_register (fip
, FCOFF_REGNUM
);
471 fill (foo
, FDOFF_REGNUM
);
472 fill_register (fos
, FDS_REGNUM
);
477 if (! valid
|| valid
[FCS_REGNUM
])
479 = ((fpregsetp
->fcs
& ~0xffff)
480 | (* (int *) ®isters
[REGISTER_BYTE (FCS_REGNUM
)] & 0xffff));
482 if (! valid
|| valid
[FOP_REGNUM
])
484 = ((fpregsetp
->fcs
& 0xffff)
485 | ((*(int *) ®isters
[REGISTER_BYTE (FOP_REGNUM
)] & ((1 << 11) - 1))
489 /* Fill register REGNO (if it is a floating-point register) in
490 *FPREGSETP with the value in GDB's register array. If REGNO is -1,
491 do this for all registers. */
494 fill_fpregset (elf_fpregset_t
*fpregsetp
, int regno
)
498 convert_to_fpregset (fpregsetp
, NULL
);
502 if (GETFPREGS_SUPPLIES(regno
))
504 signed char valid
[MAX_NUM_REGS
];
506 memset (valid
, 0, sizeof (valid
));
509 convert_to_fpregset (fpregsetp
, valid
);
513 #ifdef HAVE_PTRACE_GETREGS
515 /* Fetch all floating-point registers from process/thread TID and store
516 thier values in GDB's register array. */
519 fetch_fpregs (int tid
)
521 elf_fpregset_t fpregs
;
524 ret
= ptrace (PTRACE_GETFPREGS
, tid
, 0, (int) &fpregs
);
527 warning ("Couldn't get floating point status.");
531 supply_fpregset (&fpregs
);
534 /* Store all valid floating-point registers in GDB's register array
535 into the process/thread specified by TID. */
538 store_fpregs (int tid
)
540 elf_fpregset_t fpregs
;
543 ret
= ptrace (PTRACE_GETFPREGS
, tid
, 0, (int) &fpregs
);
546 warning ("Couldn't get floating point status.");
550 convert_to_fpregset (&fpregs
, register_valid
);
552 ret
= ptrace (PTRACE_SETFPREGS
, tid
, 0, (int) &fpregs
);
555 warning ("Couldn't write floating point status.");
562 static void fetch_fpregs (int tid
) {}
563 static void store_fpregs (int tid
) {}
568 /* Transfering floating-point and SSE registers to and from GDB. */
570 /* PTRACE_GETXFPREGS is a Cygnus invention, since we wrote our own
571 Linux kernel patch for SSE support. That patch may or may not
572 actually make it into the official distribution. If you find that
573 years have gone by since this code was added, and Linux isn't using
574 PTRACE_GETXFPREGS, that means that our patch didn't make it, and
575 you can delete this code. */
577 #ifdef HAVE_PTRACE_GETXFPREGS
579 /* Fill GDB's register array with the floating-point and SSE register
580 values in *XFPREGS. */
583 supply_xfpregset (struct user_xfpregs_struct
*xfpregs
)
587 /* Supply the floating-point registers. */
588 for (reg
= 0; reg
< 8; reg
++)
589 supply_register (FP0_REGNUM
+ reg
, (char *) &xfpregs
->st_space
[reg
]);
592 supply_register (FCTRL_REGNUM
, (char *) &xfpregs
->cwd
);
593 supply_register (FSTAT_REGNUM
, (char *) &xfpregs
->swd
);
594 supply_register (FTAG_REGNUM
, (char *) &xfpregs
->twd
);
595 supply_register (FCOFF_REGNUM
, (char *) &xfpregs
->fip
);
596 supply_register (FDS_REGNUM
, (char *) &xfpregs
->fos
);
597 supply_register (FDOFF_REGNUM
, (char *) &xfpregs
->foo
);
599 /* Extract the code segment and opcode from the "fcs" member. */
603 l
= xfpregs
->fcs
& 0xffff;
604 supply_register (FCS_REGNUM
, (char *) &l
);
606 l
= (xfpregs
->fcs
>> 16) & ((1 << 11) - 1);
607 supply_register (FOP_REGNUM
, (char *) &l
);
611 /* Supply the SSE registers. */
612 for (reg
= 0; reg
< 8; reg
++)
613 supply_register (XMM0_REGNUM
+ reg
, (char *) &xfpregs
->xmm_space
[reg
]);
614 supply_register (MXCSR_REGNUM
, (char *) &xfpregs
->mxcsr
);
617 /* Convert the valid floating-point and SSE registers in GDB's
618 register array to `struct user' format and store them in *XFPREGS.
619 The array VALID indicates which registers are valid. If VALID is
620 NULL, all registers are assumed to be valid. */
623 convert_to_xfpregset (struct user_xfpregs_struct
*xfpregs
,
628 /* Fill in the floating-point registers. */
629 for (reg
= 0; reg
< 8; reg
++)
630 if (!valid
|| valid
[reg
])
631 memcpy (&xfpregs
->st_space
[reg
],
632 ®isters
[REGISTER_BYTE (FP0_REGNUM
+ reg
)],
633 REGISTER_RAW_SIZE(FP0_REGNUM
+ reg
));
635 #define fill(MEMBER, REGNO) \
636 if (! valid || valid[(REGNO)]) \
637 memcpy (&xfpregs->MEMBER, ®isters[REGISTER_BYTE (REGNO)], \
638 sizeof (xfpregs->MEMBER))
640 fill (cwd
, FCTRL_REGNUM
);
641 fill (swd
, FSTAT_REGNUM
);
642 fill (twd
, FTAG_REGNUM
);
643 fill (fip
, FCOFF_REGNUM
);
644 fill (foo
, FDOFF_REGNUM
);
645 fill (fos
, FDS_REGNUM
);
649 if (! valid
|| valid
[FCS_REGNUM
])
651 = ((xfpregs
->fcs
& ~0xffff)
652 | (* (int *) ®isters
[REGISTER_BYTE (FCS_REGNUM
)] & 0xffff));
654 if (! valid
|| valid
[FOP_REGNUM
])
656 = ((xfpregs
->fcs
& 0xffff)
657 | ((*(int *) ®isters
[REGISTER_BYTE (FOP_REGNUM
)] & ((1 << 11) - 1))
660 /* Fill in the XMM registers. */
661 for (reg
= 0; reg
< 8; reg
++)
662 if (! valid
|| valid
[reg
])
663 memcpy (&xfpregs
->xmm_space
[reg
],
664 ®isters
[REGISTER_BYTE (XMM0_REGNUM
+ reg
)],
665 REGISTER_RAW_SIZE (XMM0_REGNUM
+ reg
));
668 /* Fetch all registers covered by the PTRACE_SETXFPREGS request from
669 process/thread TID and store their values in GDB's register array.
670 Return non-zero if successful, zero otherwise. */
673 fetch_xfpregs (int tid
)
675 struct user_xfpregs_struct xfpregs
;
678 if (! have_ptrace_getxfpregs
)
681 ret
= ptrace (PTRACE_GETXFPREGS
, tid
, 0, &xfpregs
);
686 have_ptrace_getxfpregs
= 0;
690 warning ("Couldn't read floating-point and SSE registers.");
694 supply_xfpregset (&xfpregs
);
698 /* Store all valid registers in GDB's register array covered by the
699 PTRACE_SETXFPREGS request into the process/thread specified by TID.
700 Return non-zero if successful, zero otherwise. */
703 store_xfpregs (int tid
)
705 struct user_xfpregs_struct xfpregs
;
708 if (! have_ptrace_getxfpregs
)
711 ret
= ptrace (PTRACE_GETXFPREGS
, tid
, 0, &xfpregs
);
716 have_ptrace_getxfpregs
= 0;
720 warning ("Couldn't read floating-point and SSE registers.");
724 convert_to_xfpregset (&xfpregs
, register_valid
);
726 if (ptrace (PTRACE_SETXFPREGS
, tid
, 0, &xfpregs
) < 0)
728 warning ("Couldn't write floating-point and SSE registers.");
735 /* Fill the XMM registers in the register array with dummy values. For
736 cases where we don't have access to the XMM registers. I think
737 this is cleaner than printing a warning. For a cleaner solution,
738 we should gdbarchify the i386 family. */
741 dummy_sse_values (void)
743 /* C doesn't have a syntax for NaN's, so write it out as an array of
745 static long dummy
[4] = { 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff };
746 static long mxcsr
= 0x1f80;
749 for (reg
= 0; reg
< 8; reg
++)
750 supply_register (XMM0_REGNUM
+ reg
, (char *) dummy
);
751 supply_register (MXCSR_REGNUM
, (char *) &mxcsr
);
756 /* Stub versions of the above routines, for systems that don't have
757 PTRACE_GETXFPREGS. */
758 static int store_xfpregs (int tid
) { return 0; }
759 static int fetch_xfpregs (int tid
) { return 0; }
760 static void dummy_sse_values (void) {}
765 /* Transferring arbitrary registers between GDB and inferior. */
767 /* Fetch register REGNO from the child process. If REGNO is -1, do
768 this for all registers (including the floating point and SSE
772 fetch_inferior_registers (int regno
)
776 /* Use the old method of peeking around in `struct user' if the
777 GETREGS request isn't available. */
778 if (! have_ptrace_getregs
)
780 old_fetch_inferior_registers (regno
);
784 /* Linux LWP ID's are process ID's. */
785 if ((tid
= TIDGET (inferior_pid
)) == 0)
786 tid
= inferior_pid
; /* Not a threaded program. */
788 /* Use the PTRACE_GETXFPREGS request whenever possible, since it
789 transfers more registers in one system call, and we'll cache the
790 results. But remember that fetch_xfpregs can fail, and return
796 /* The call above might reset `have_ptrace_getregs'. */
797 if (! have_ptrace_getregs
)
799 old_fetch_inferior_registers (-1);
803 if (fetch_xfpregs (tid
))
809 if (GETREGS_SUPPLIES (regno
))
815 if (GETXFPREGS_SUPPLIES (regno
))
817 if (fetch_xfpregs (tid
))
820 /* Either our processor or our kernel doesn't support the SSE
821 registers, so read the FP registers in the traditional way,
822 and fill the SSE registers with dummy values. It would be
823 more graceful to handle differences in the register set using
824 gdbarch. Until then, this will at least make things work
831 internal_error ("i386-linux-nat.c (fetch_inferior_registers): "
832 "got request for bad register number %d", regno
);
835 /* Store register REGNO back into the child process. If REGNO is -1,
836 do this for all registers (including the floating point and SSE
839 store_inferior_registers (int regno
)
843 /* Use the old method of poking around in `struct user' if the
844 SETREGS request isn't available. */
845 if (! have_ptrace_getregs
)
847 old_store_inferior_registers (regno
);
851 /* Linux LWP ID's are process ID's. */
852 if ((tid
= TIDGET (inferior_pid
)) == 0)
853 tid
= inferior_pid
; /* Not a threaded program. */
855 /* Use the PTRACE_SETXFPREGS requests whenever possibl, since it
856 transfers more registers in one system call. But remember that
857 store_xfpregs can fail, and return zero. */
861 if (store_xfpregs (tid
))
867 if (GETREGS_SUPPLIES (regno
))
873 if (GETXFPREGS_SUPPLIES (regno
))
875 if (store_xfpregs (tid
))
878 /* Either our processor or our kernel doesn't support the SSE
879 registers, so just write the FP registers in the traditional
885 internal_error ("Got request to store bad register number %d.", regno
);
889 /* Interpreting register set info found in core files. */
891 /* Provide registers to GDB from a core file.
893 (We can't use the generic version of this function in
894 core-regset.c, because Linux has *three* different kinds of
895 register set notes. core-regset.c would have to call
896 supply_xfpregset, which most platforms don't have.)
898 CORE_REG_SECT points to an array of bytes, which are the contents
899 of a `note' from a core file which BFD thinks might contain
900 register contents. CORE_REG_SIZE is its size.
902 WHICH says which register set corelow suspects this is:
903 0 --- the general-purpose register set, in elf_gregset_t format
904 2 --- the floating-point register set, in elf_fpregset_t format
905 3 --- the extended floating-point register set, in struct
906 user_xfpregs_struct format
908 REG_ADDR isn't used on Linux. */
911 fetch_core_registers (char *core_reg_sect
, unsigned core_reg_size
,
912 int which
, CORE_ADDR reg_addr
)
914 elf_gregset_t gregset
;
915 elf_fpregset_t fpregset
;
920 if (core_reg_size
!= sizeof (gregset
))
921 warning ("Wrong size gregset in core file.");
924 memcpy (&gregset
, core_reg_sect
, sizeof (gregset
));
925 supply_gregset (&gregset
);
930 if (core_reg_size
!= sizeof (fpregset
))
931 warning ("Wrong size fpregset in core file.");
934 memcpy (&fpregset
, core_reg_sect
, sizeof (fpregset
));
935 supply_fpregset (&fpregset
);
939 #ifdef HAVE_PTRACE_GETXFPREGS
941 struct user_xfpregs_struct xfpregset
;
944 if (core_reg_size
!= sizeof (xfpregset
))
945 warning ("Wrong size user_xfpregs_struct in core file.");
948 memcpy (&xfpregset
, core_reg_sect
, sizeof (xfpregset
));
949 supply_xfpregset (&xfpregset
);
956 /* We've covered all the kinds of registers we know about here,
957 so this must be something we wouldn't know what to do with
958 anyway. Just ignore it. */
964 /* The instruction for a Linux system call is:
968 static const unsigned char linux_syscall
[] = { 0xcd, 0x80 };
970 #define LINUX_SYSCALL_LEN (sizeof linux_syscall)
972 /* The system call number is stored in the %eax register. */
973 #define LINUX_SYSCALL_REGNUM 0 /* %eax */
975 /* We are specifically interested in the sigreturn and rt_sigreturn
978 #ifndef SYS_sigreturn
979 #define SYS_sigreturn 0x77
981 #ifndef SYS_rt_sigreturn
982 #define SYS_rt_sigreturn 0xad
985 /* Offset to saved processor flags, from <asm/sigcontext.h>. */
986 #define LINUX_SIGCONTEXT_EFLAGS_OFFSET (64)
988 /* Resume execution of the inferior process.
989 If STEP is nonzero, single-step it.
990 If SIGNAL is nonzero, give it that signal. */
993 child_resume (int pid
, int step
, enum target_signal signal
)
995 int request
= PTRACE_CONT
;
998 /* Resume all threads. */
999 /* I think this only gets used in the non-threaded case, where "resume
1000 all threads" and "resume inferior_pid" are the same. */
1005 CORE_ADDR pc
= read_pc_pid (pid
);
1006 unsigned char buf
[LINUX_SYSCALL_LEN
];
1008 request
= PTRACE_SINGLESTEP
;
1010 /* Returning from a signal trampoline is done by calling a
1011 special system call (sigreturn or rt_sigreturn, see
1012 i386-linux-tdep.c for more information). This system call
1013 restores the registers that were saved when the signal was
1014 raised, including %eflags. That means that single-stepping
1015 won't work. Instead, we'll have to modify the signal context
1016 that's about to be restored, and set the trace flag there. */
1018 /* First check if PC is at a system call. */
1019 if (read_memory_nobpt (pc
, (char *) buf
, LINUX_SYSCALL_LEN
) == 0
1020 && memcmp (buf
, linux_syscall
, LINUX_SYSCALL_LEN
) == 0)
1022 int syscall
= read_register_pid (LINUX_SYSCALL_REGNUM
, pid
);
1024 /* Then check the system call number. */
1025 if (syscall
== SYS_sigreturn
|| syscall
== SYS_rt_sigreturn
)
1027 CORE_ADDR sp
= read_register (SP_REGNUM
);
1028 CORE_ADDR addr
= sp
;
1029 unsigned long int eflags
;
1031 if (syscall
== SYS_rt_sigreturn
)
1032 addr
= read_memory_integer (sp
+ 8, 4) + 20;
1034 /* Set the trace flag in the context that's about to be
1036 addr
+= LINUX_SIGCONTEXT_EFLAGS_OFFSET
;
1037 read_memory (addr
, (char *) &eflags
, 4);
1039 write_memory (addr
, (char *) &eflags
, 4);
1044 if (ptrace (request
, pid
, 0, target_signal_to_host (signal
)) == -1)
1045 perror_with_name ("ptrace");
1049 /* Calling functions in shared libraries. */
1050 /* FIXME: kettenis/2000-03-05: Doesn't this belong in a
1051 target-dependent file? The function
1052 `i386_linux_skip_solib_resolver' is mentioned in
1053 `config/i386/tm-linux.h'. */
1055 /* Find the minimal symbol named NAME, and return both the minsym
1056 struct and its objfile. This probably ought to be in minsym.c, but
1057 everything there is trying to deal with things like C++ and
1058 SOFUN_ADDRESS_MAYBE_TURQUOISE, ... Since this is so simple, it may
1059 be considered too special-purpose for general consumption. */
1061 static struct minimal_symbol
*
1062 find_minsym_and_objfile (char *name
, struct objfile
**objfile_p
)
1064 struct objfile
*objfile
;
1066 ALL_OBJFILES (objfile
)
1068 struct minimal_symbol
*msym
;
1070 ALL_OBJFILE_MSYMBOLS (objfile
, msym
)
1072 if (SYMBOL_NAME (msym
)
1073 && STREQ (SYMBOL_NAME (msym
), name
))
1075 *objfile_p
= objfile
;
1086 skip_hurd_resolver (CORE_ADDR pc
)
1088 /* The HURD dynamic linker is part of the GNU C library, so many
1089 GNU/Linux distributions use it. (All ELF versions, as far as I
1090 know.) An unresolved PLT entry points to "_dl_runtime_resolve",
1091 which calls "fixup" to patch the PLT, and then passes control to
1094 We look for the symbol `_dl_runtime_resolve', and find `fixup' in
1095 the same objfile. If we are at the entry point of `fixup', then
1096 we set a breakpoint at the return address (at the top of the
1097 stack), and continue.
1099 It's kind of gross to do all these checks every time we're
1100 called, since they don't change once the executable has gotten
1101 started. But this is only a temporary hack --- upcoming versions
1102 of Linux will provide a portable, efficient interface for
1103 debugging programs that use shared libraries. */
1105 struct objfile
*objfile
;
1106 struct minimal_symbol
*resolver
1107 = find_minsym_and_objfile ("_dl_runtime_resolve", &objfile
);
1111 struct minimal_symbol
*fixup
1112 = lookup_minimal_symbol ("fixup", 0, objfile
);
1114 if (fixup
&& SYMBOL_VALUE_ADDRESS (fixup
) == pc
)
1115 return (SAVED_PC_AFTER_CALL (get_current_frame ()));
1121 /* See the comments for SKIP_SOLIB_RESOLVER at the top of infrun.c.
1123 1) decides whether a PLT has sent us into the linker to resolve
1124 a function reference, and
1125 2) if so, tells us where to set a temporary breakpoint that will
1126 trigger when the dynamic linker is done. */
1129 i386_linux_skip_solib_resolver (CORE_ADDR pc
)
1133 /* Plug in functions for other kinds of resolvers here. */
1134 result
= skip_hurd_resolver (pc
);
1142 /* Register that we are able to handle Linux ELF core file formats. */
1144 static struct core_fns linux_elf_core_fns
=
1146 bfd_target_elf_flavour
, /* core_flavour */
1147 default_check_format
, /* check_format */
1148 default_core_sniffer
, /* core_sniffer */
1149 fetch_core_registers
, /* core_read_registers */
1154 _initialize_i386_linux_nat ()
1156 add_core_fns (&linux_elf_core_fns
);