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 GETXFPREGS request? The header
83 file may or may not define it, and even if it is defined, the
84 kernel will return EIO if it's running on a pre-SSE processor.
86 PTRACE_GETXFPREGS is a Cygnus invention, since we wrote our own
87 Linux kernel patch for SSE support. That patch may or may not
88 actually make it into the official distribution. If you find that
89 years have gone by since this stuff was added, and Linux isn't
90 using PTRACE_GETXFPREGS, that means that our patch didn't make it,
91 and you can delete this, and the related code.
93 My instinct is to attach this to some architecture- or
94 target-specific data structure, but really, a particular GDB
95 process can only run on top of one kernel at a time. So it's okay
96 for this to be a simple variable. */
97 int have_ptrace_getxfpregs =
98 #ifdef HAVE_PTRACE_GETXFPREGS
106 /* Transfering the general-purpose registers between GDB, inferiors
109 /* Fill GDB's register array with the genereal-purpose register values
113 supply_gregset (elf_gregset_t *gregsetp)
115 elf_greg_t *regp = (elf_greg_t *) gregsetp;
118 for (regi = 0; regi < NUM_GREGS; regi++)
119 supply_register (regi, (char *) (regp + regmap[regi]));
122 /* Convert the valid general-purpose register values in GDB's register
123 array to `struct user' format and store them in *GREGSETP. The
124 array VALID indicates which register values are valid. If VALID is
125 NULL, all registers are assumed to be valid. */
128 convert_to_gregset (elf_gregset_t *gregsetp, signed char *valid)
130 elf_greg_t *regp = (elf_greg_t *) gregsetp;
133 for (regi = 0; regi < NUM_GREGS; regi++)
134 if (! valid || valid[regi])
135 *(regp + regmap[regi]) = * (int *) ®isters[REGISTER_BYTE (regi)];
138 /* Fill register REGNO (if it is a general-purpose register) in
139 *GREGSETPS with the value in GDB's register array. If REGNO is -1,
140 do this for all registers. */
142 fill_gregset (elf_gregset_t *gregsetp, int regno)
146 convert_to_gregset (gregsetp, NULL);
150 if (GETREGS_SUPPLIES (regno))
152 signed char valid[NUM_GREGS];
154 memset (valid, 0, sizeof (valid));
157 convert_to_gregset (gregsetp, valid);
161 /* Fetch all general-purpose registers from process/thread TID and
162 store their values in GDB's register array. */
170 ret = ptrace (PTRACE_GETREGS, tid, 0, (int) ®s);
173 warning ("Couldn't get registers.");
177 supply_gregset (®s);
180 /* Store all valid general-purpose registers in GDB's register array
181 into the process/thread specified by TID. */
189 ret = ptrace (PTRACE_GETREGS, tid, 0, (int) ®s);
192 warning ("Couldn't get registers.");
196 convert_to_gregset (®s, register_valid);
198 ret = ptrace (PTRACE_SETREGS, tid, 0, (int) ®s);
201 warning ("Couldn't write registers.");
207 /* Transfering floating-point registers between GDB, inferiors and cores. */
209 /* What is the address of st(N) within the floating-point register set F? */
210 #define FPREG_ADDR(f, n) ((char *) &(f)->st_space + (n) * 10)
212 /* Fill GDB's register array with the floating-point register values in
216 supply_fpregset (elf_fpregset_t *fpregsetp)
220 /* Supply the floating-point registers. */
221 for (reg = 0; reg < 8; reg++)
222 supply_register (FP0_REGNUM + reg, FPREG_ADDR (fpregsetp, reg));
224 supply_register (FCTRL_REGNUM, (char *) &fpregsetp->cwd);
225 supply_register (FSTAT_REGNUM, (char *) &fpregsetp->swd);
226 supply_register (FTAG_REGNUM, (char *) &fpregsetp->twd);
227 supply_register (FCOFF_REGNUM, (char *) &fpregsetp->fip);
228 supply_register (FDS_REGNUM, (char *) &fpregsetp->fos);
229 supply_register (FDOFF_REGNUM, (char *) &fpregsetp->foo);
231 /* Extract the code segment and opcode from the "fcs" member. */
235 l = fpregsetp->fcs & 0xffff;
236 supply_register (FCS_REGNUM, (char *) &l);
238 l = (fpregsetp->fcs >> 16) & ((1 << 11) - 1);
239 supply_register (FOP_REGNUM, (char *) &l);
243 /* Convert the valid floating-point register values in GDB's register
244 array to `struct user' format and store them in *FPREGSETP. The
245 array VALID indicates which register values are valid. If VALID is
246 NULL, all registers are assumed to be valid. */
249 convert_to_fpregset (elf_fpregset_t *fpregsetp, signed char *valid)
253 /* Fill in the floating-point registers. */
254 for (reg = 0; reg < 8; reg++)
255 if (!valid || valid[reg])
256 memcpy (FPREG_ADDR (fpregsetp, reg),
257 ®isters[REGISTER_BYTE (FP0_REGNUM + reg)],
258 REGISTER_RAW_SIZE(FP0_REGNUM + reg));
260 #define fill(MEMBER, REGNO) \
261 if (! valid || valid[(REGNO)]) \
262 memcpy (&fpregsetp->MEMBER, ®isters[REGISTER_BYTE (REGNO)], \
263 sizeof (fpregsetp->MEMBER))
265 fill (cwd, FCTRL_REGNUM);
266 fill (swd, FSTAT_REGNUM);
267 fill (twd, FTAG_REGNUM);
268 fill (fip, FCOFF_REGNUM);
269 fill (foo, FDOFF_REGNUM);
270 fill (fos, FDS_REGNUM);
274 if (! valid || valid[FCS_REGNUM])
276 = ((fpregsetp->fcs & ~0xffff)
277 | (* (int *) ®isters[REGISTER_BYTE (FCS_REGNUM)] & 0xffff));
279 if (! valid || valid[FOP_REGNUM])
281 = ((fpregsetp->fcs & 0xffff)
282 | ((*(int *) ®isters[REGISTER_BYTE (FOP_REGNUM)] & ((1 << 11) - 1))
286 /* Fill register REGNO (if it is a floating-point register) in
287 *FPREGSETP with the value in GDB's register array. If REGNO is -1,
288 do this for all registers. */
291 fill_fpregset (elf_fpregset_t *fpregsetp, int regno)
295 convert_to_fpregset (fpregsetp, NULL);
299 if (GETFPREGS_SUPPLIES(regno))
301 signed char valid[MAX_NUM_REGS];
303 memset (valid, 0, sizeof (valid));
306 convert_to_fpregset (fpregsetp, valid);
310 /* Fetch all floating-point registers from process/thread TID and store
311 thier values in GDB's register array. */
314 fetch_fpregs (int tid)
316 elf_fpregset_t fpregs;
319 ret = ptrace (PTRACE_GETFPREGS, tid, 0, (int) &fpregs);
322 warning ("Couldn't get floating point status.");
326 supply_fpregset (&fpregs);
329 /* Store all valid floating-point registers in GDB's register array
330 into the process/thread specified by TID. */
333 store_fpregs (int tid)
335 elf_fpregset_t fpregs;
338 ret = ptrace (PTRACE_GETFPREGS, tid, 0, (int) &fpregs);
341 warning ("Couldn't get floating point status.");
345 convert_to_fpregset (&fpregs, register_valid);
347 ret = ptrace (PTRACE_SETFPREGS, tid, 0, (int) &fpregs);
350 warning ("Couldn't write floating point status.");
356 /* Transfering floating-point and SSE registers to and from GDB. */
358 /* PTRACE_GETXFPREGS is a Cygnus invention, since we wrote our own
359 Linux kernel patch for SSE support. That patch may or may not
360 actually make it into the official distribution. If you find that
361 years have gone by since this code was added, and Linux isn't using
362 PTRACE_GETXFPREGS, that means that our patch didn't make it, and
363 you can delete this code. */
365 #ifdef HAVE_PTRACE_GETXFPREGS
367 /* Fill GDB's register array with the floating-point and SSE register
368 values in *XFPREGS. */
371 supply_xfpregset (struct user_xfpregs_struct *xfpregs)
375 /* Supply the floating-point registers. */
376 for (reg = 0; reg < 8; reg++)
377 supply_register (FP0_REGNUM + reg, (char *) &xfpregs->st_space[reg]);
380 supply_register (FCTRL_REGNUM, (char *) &xfpregs->cwd);
381 supply_register (FSTAT_REGNUM, (char *) &xfpregs->swd);
382 supply_register (FTAG_REGNUM, (char *) &xfpregs->twd);
383 supply_register (FCOFF_REGNUM, (char *) &xfpregs->fip);
384 supply_register (FDS_REGNUM, (char *) &xfpregs->fos);
385 supply_register (FDOFF_REGNUM, (char *) &xfpregs->foo);
387 /* Extract the code segment and opcode from the "fcs" member. */
391 l = xfpregs->fcs & 0xffff;
392 supply_register (FCS_REGNUM, (char *) &l);
394 l = (xfpregs->fcs >> 16) & ((1 << 11) - 1);
395 supply_register (FOP_REGNUM, (char *) &l);
399 /* Supply the SSE registers. */
400 for (reg = 0; reg < 8; reg++)
401 supply_register (XMM0_REGNUM + reg, (char *) &xfpregs->xmm_space[reg]);
402 supply_register (MXCSR_REGNUM, (char *) &xfpregs->mxcsr);
405 /* Convert the valid floating-point and SSE registers in GDB's
406 register array to `struct user' format and store them in *XFPREGS.
407 The array VALID indicates which registers are valid. If VALID is
408 NULL, all registers are assumed to be valid. */
411 convert_to_xfpregset (struct user_xfpregs_struct *xfpregs,
416 /* Fill in the floating-point registers. */
417 for (reg = 0; reg < 8; reg++)
418 if (!valid || valid[reg])
419 memcpy (&xfpregs->st_space[reg],
420 ®isters[REGISTER_BYTE (FP0_REGNUM + reg)],
421 REGISTER_RAW_SIZE(FP0_REGNUM + reg));
423 #define fill(MEMBER, REGNO) \
424 if (! valid || valid[(REGNO)]) \
425 memcpy (&xfpregs->MEMBER, ®isters[REGISTER_BYTE (REGNO)], \
426 sizeof (xfpregs->MEMBER))
428 fill (cwd, FCTRL_REGNUM);
429 fill (swd, FSTAT_REGNUM);
430 fill (twd, FTAG_REGNUM);
431 fill (fip, FCOFF_REGNUM);
432 fill (foo, FDOFF_REGNUM);
433 fill (fos, FDS_REGNUM);
437 if (! valid || valid[FCS_REGNUM])
439 = ((xfpregs->fcs & ~0xffff)
440 | (* (int *) ®isters[REGISTER_BYTE (FCS_REGNUM)] & 0xffff));
442 if (! valid || valid[FOP_REGNUM])
444 = ((xfpregs->fcs & 0xffff)
445 | ((*(int *) ®isters[REGISTER_BYTE (FOP_REGNUM)] & ((1 << 11) - 1))
448 /* Fill in the XMM registers. */
449 for (reg = 0; reg < 8; reg++)
450 if (! valid || valid[reg])
451 memcpy (&xfpregs->xmm_space[reg],
452 ®isters[REGISTER_BYTE (XMM0_REGNUM + reg)],
453 REGISTER_RAW_SIZE (XMM0_REGNUM + reg));
456 /* Fetch all registers covered by the PTRACE_SETXFPREGS request from
457 process/thread TID and store their values in GDB's register array.
458 Return non-zero if successful, zero otherwise. */
461 fetch_xfpregs (int tid)
463 struct user_xfpregs_struct xfpregs;
466 if (! have_ptrace_getxfpregs)
469 ret = ptrace (PTRACE_GETXFPREGS, tid, 0, &xfpregs);
474 have_ptrace_getxfpregs = 0;
478 warning ("Couldn't read floating-point and SSE registers.");
482 supply_xfpregset (&xfpregs);
486 /* Store all valid registers in GDB's register array covered by the
487 PTRACE_SETXFPREGS request into the process/thread specified by TID.
488 Return non-zero if successful, zero otherwise. */
491 store_xfpregs (int tid)
493 struct user_xfpregs_struct xfpregs;
496 if (! have_ptrace_getxfpregs)
499 ret = ptrace (PTRACE_GETXFPREGS, tid, 0, &xfpregs);
504 have_ptrace_getxfpregs = 0;
508 warning ("Couldn't read floating-point and SSE registers.");
512 convert_to_xfpregset (&xfpregs, register_valid);
514 if (ptrace (PTRACE_SETXFPREGS, tid, 0, &xfpregs) < 0)
516 warning ("Couldn't write floating-point and SSE registers.");
523 /* Fill the XMM registers in the register array with dummy values. For
524 cases where we don't have access to the XMM registers. I think
525 this is cleaner than printing a warning. For a cleaner solution,
526 we should gdbarchify the i386 family. */
529 dummy_sse_values (void)
531 /* C doesn't have a syntax for NaN's, so write it out as an array of
533 static long dummy[4] = { 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff };
534 static long mxcsr = 0x1f80;
537 for (reg = 0; reg < 8; reg++)
538 supply_register (XMM0_REGNUM + reg, (char *) dummy);
539 supply_register (MXCSR_REGNUM, (char *) &mxcsr);
544 /* Stub versions of the above routines, for systems that don't have
545 PTRACE_GETXFPREGS. */
546 static int store_xfpregs (int tid) { return 0; }
547 static int fetch_xfpregs (int tid) { return 0; }
548 static void dummy_sse_values (void) {}
553 /* Transferring arbitrary registers between GDB and inferior. */
555 /* Fetch register REGNO from the child process. If REGNO is -1, do
556 this for all registers (including the floating point and SSE
560 fetch_inferior_registers (int regno)
564 /* Linux LWP ID's are process ID's. */
565 if ((tid = TIDGET (inferior_pid)) == 0)
566 tid = inferior_pid; /* Not a threaded program. */
568 /* Use the PTRACE_GETXFPREGS request whenever possible, since it
569 transfers more registers in one system call, and we'll cache the
570 results. But remember that fetch_xfpregs can fail, and return
575 if (fetch_xfpregs (tid))
581 if (GETREGS_SUPPLIES (regno))
587 if (GETXFPREGS_SUPPLIES (regno))
589 if (fetch_xfpregs (tid))
592 /* Either our processor or our kernel doesn't support the SSE
593 registers, so read the FP registers in the traditional way,
594 and fill the SSE registers with dummy values. It would be
595 more graceful to handle differences in the register set using
596 gdbarch. Until then, this will at least make things work
603 internal_error ("i386-linux-nat.c (fetch_inferior_registers): "
604 "got request for bad register number %d", regno);
607 /* Store register REGNO back into the child process. If REGNO is -1,
608 do this for all registers (including the floating point and SSE
611 store_inferior_registers (int regno)
615 /* Linux LWP ID's are process ID's. */
616 if ((tid = TIDGET (inferior_pid)) == 0)
617 tid = inferior_pid; /* Not a threaded program. */
619 /* Use the PTRACE_SETXFPREGS requests whenever possibl, since it
620 transfers more registers in one system call. But remember that
621 store_xfpregs can fail, and return zero. */
625 if (store_xfpregs (tid))
631 if (GETREGS_SUPPLIES (regno))
637 if (GETXFPREGS_SUPPLIES (regno))
639 if (store_xfpregs (tid))
642 /* Either our processor or our kernel doesn't support the SSE
643 registers, so just write the FP registers in the traditional
649 internal_error ("Got request to store bad register number %d.", regno);
653 /* Interpreting register set info found in core files. */
655 /* Provide registers to GDB from a core file.
657 (We can't use the generic version of this function in
658 core-regset.c, because Linux has *three* different kinds of
659 register set notes. core-regset.c would have to call
660 supply_xfpregset, which most platforms don't have.)
662 CORE_REG_SECT points to an array of bytes, which are the contents
663 of a `note' from a core file which BFD thinks might contain
664 register contents. CORE_REG_SIZE is its size.
666 WHICH says which register set corelow suspects this is:
667 0 --- the general-purpose register set, in elf_gregset_t format
668 2 --- the floating-point register set, in elf_fpregset_t format
669 3 --- the extended floating-point register set, in struct
670 user_xfpregs_struct format
672 REG_ADDR isn't used on Linux. */
675 fetch_core_registers (char *core_reg_sect, unsigned core_reg_size,
676 int which, CORE_ADDR reg_addr)
678 elf_gregset_t gregset;
679 elf_fpregset_t fpregset;
684 if (core_reg_size != sizeof (gregset))
685 warning ("Wrong size gregset in core file.");
688 memcpy (&gregset, core_reg_sect, sizeof (gregset));
689 supply_gregset (&gregset);
694 if (core_reg_size != sizeof (fpregset))
695 warning ("Wrong size fpregset in core file.");
698 memcpy (&fpregset, core_reg_sect, sizeof (fpregset));
699 supply_fpregset (&fpregset);
703 #ifdef HAVE_PTRACE_GETXFPREGS
705 struct user_xfpregs_struct xfpregset;
708 if (core_reg_size != sizeof (xfpregset))
709 warning ("Wrong size user_xfpregs_struct in core file.");
712 memcpy (&xfpregset, core_reg_sect, sizeof (xfpregset));
713 supply_xfpregset (&xfpregset);
720 /* We've covered all the kinds of registers we know about here,
721 so this must be something we wouldn't know what to do with
722 anyway. Just ignore it. */
728 /* Calling functions in shared libraries. */
729 /* FIXME: kettenis/2000-03-05: Doesn't this belong in a
730 target-dependent file? The function
731 `i386_linux_skip_solib_resolver' is mentioned in
732 `config/i386/tm-linux.h'. */
734 /* Find the minimal symbol named NAME, and return both the minsym
735 struct and its objfile. This probably ought to be in minsym.c, but
736 everything there is trying to deal with things like C++ and
737 SOFUN_ADDRESS_MAYBE_TURQUOISE, ... Since this is so simple, it may
738 be considered too special-purpose for general consumption. */
740 static struct minimal_symbol *
741 find_minsym_and_objfile (char *name, struct objfile **objfile_p)
743 struct objfile *objfile;
745 ALL_OBJFILES (objfile)
747 struct minimal_symbol *msym;
749 ALL_OBJFILE_MSYMBOLS (objfile, msym)
751 if (SYMBOL_NAME (msym)
752 && STREQ (SYMBOL_NAME (msym), name))
754 *objfile_p = objfile;
765 skip_hurd_resolver (CORE_ADDR pc)
767 /* The HURD dynamic linker is part of the GNU C library, so many
768 GNU/Linux distributions use it. (All ELF versions, as far as I
769 know.) An unresolved PLT entry points to "_dl_runtime_resolve",
770 which calls "fixup" to patch the PLT, and then passes control to
773 We look for the symbol `_dl_runtime_resolve', and find `fixup' in
774 the same objfile. If we are at the entry point of `fixup', then
775 we set a breakpoint at the return address (at the top of the
776 stack), and continue.
778 It's kind of gross to do all these checks every time we're
779 called, since they don't change once the executable has gotten
780 started. But this is only a temporary hack --- upcoming versions
781 of Linux will provide a portable, efficient interface for
782 debugging programs that use shared libraries. */
784 struct objfile *objfile;
785 struct minimal_symbol *resolver
786 = find_minsym_and_objfile ("_dl_runtime_resolve", &objfile);
790 struct minimal_symbol *fixup
791 = lookup_minimal_symbol ("fixup", 0, objfile);
793 if (fixup && SYMBOL_VALUE_ADDRESS (fixup) == pc)
794 return (SAVED_PC_AFTER_CALL (get_current_frame ()));
800 /* See the comments for SKIP_SOLIB_RESOLVER at the top of infrun.c.
802 1) decides whether a PLT has sent us into the linker to resolve
803 a function reference, and
804 2) if so, tells us where to set a temporary breakpoint that will
805 trigger when the dynamic linker is done. */
808 i386_linux_skip_solib_resolver (CORE_ADDR pc)
812 /* Plug in functions for other kinds of resolvers here. */
813 result = skip_hurd_resolver (pc);
821 /* Register that we are able to handle Linux ELF core file formats. */
823 static struct core_fns linux_elf_core_fns =
825 bfd_target_elf_flavour, /* core_flavour */
826 default_check_format, /* check_format */
827 default_core_sniffer, /* core_sniffer */
828 fetch_core_registers, /* core_read_registers */
833 _initialize_i386_linux_nat ()
835 add_core_fns (&linux_elf_core_fns);