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, Boston, MA 02111-1307, USA. */
23 /* For i386_linux_skip_solib_resolver */
29 #include <sys/ptrace.h>
31 #include <sys/procfs.h>
38 * Some systems (Linux) may have threads implemented as pseudo-processes,
39 * in which case we may be tracing more than one process at a time.
40 * In that 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. The definitions may be overridden in tm.h
45 #if !defined (PIDGET) /* Default definition for PIDGET/TIDGET. */
46 #define PIDGET(PID) PID
50 /* This is a duplicate of the table in i386-xdep.c. */
61 /* Which ptrace request retrieves which registers?
62 These apply to the corresponding SET requests as well. */
63 #define GETREGS_SUPPLIES(regno) \
64 (0 <= (regno) && (regno) <= 15)
65 #define GETFPREGS_SUPPLIES(regno) \
66 (FP0_REGNUM <= (regno) && (regno) <= LAST_FPU_CTRL_REGNUM)
67 #define GETXFPREGS_SUPPLIES(regno) \
68 (FP0_REGNUM <= (regno) && (regno) <= MXCSR_REGNUM)
70 /* Does the current host support the GETXFPREGS request? The header
71 file may or may not define it, and even if it is defined, the
72 kernel will return EIO if it's running on a pre-SSE processor.
74 PTRACE_GETXFPREGS is a Cygnus invention, since we wrote our own
75 Linux kernel patch for SSE support. That patch may or may not
76 actually make it into the official distribution. If you find that
77 years have gone by since this stuff was added, and Linux isn't
78 using PTRACE_GETXFPREGS, that means that our patch didn't make it,
79 and you can delete this, and the related code.
81 My instinct is to attach this to some architecture- or
82 target-specific data structure, but really, a particular GDB
83 process can only run on top of one kernel at a time. So it's okay
84 for this to be a simple variable. */
85 int have_ptrace_getxfpregs =
86 #ifdef HAVE_PTRACE_GETXFPREGS
95 /* Transfering the general registers between GDB, inferiors and core files. */
97 /* Given a pointer to a general register set in struct user format
98 (gregset_t *), unpack the register contents and supply them as
99 gdb's idea of the current register values. */
101 supply_gregset (gregsetp)
105 register greg_t *regp = (greg_t *) gregsetp;
107 for (regi = 0; regi < NUM_GREGS; regi++)
109 supply_register (regi, (char *) (regp + regmap[regi]));
114 /* Fill in a gregset_t object with selected data from a gdb-format
116 - GREGSETP points to the gregset_t object to be filled.
117 - GDB_REGS points to the GDB-style register file providing the data.
118 - VALID is an array indicating which registers in GDB_REGS are
119 valid; the parts of *GREGSETP that would hold registers marked
120 invalid in GDB_REGS are left unchanged. If VALID is zero, all
121 registers are assumed to be valid. */
123 convert_to_gregset (gregset_t *gregsetp,
128 register greg_t *regp = (greg_t *) gregsetp;
130 for (regi = 0; regi < NUM_GREGS; regi++)
131 if (! valid || valid[regi])
132 *(regp + regmap[regi]) = * (int *) ®isters[REGISTER_BYTE (regi)];
136 /* Store GDB's value for REGNO in *GREGSETP. If REGNO is -1, do all
139 fill_gregset (gregset_t *gregsetp,
143 convert_to_gregset (gregsetp, registers, 0);
144 else if (regno >= 0 && regno < NUM_GREGS)
146 signed char valid[NUM_GREGS];
147 memset (valid, 0, sizeof (valid));
149 convert_to_gregset (gregsetp, registers, valid);
154 /* Read the general registers from the process, and store them
162 ret = ptrace (PTRACE_GETREGS, tid, 0, (int) &buf);
165 warning ("Couldn't get registers");
169 supply_gregset (&buf);
173 /* Set the inferior's general registers to the values in registers[]
174 --- but only those registers marked as valid. */
181 ret = ptrace (PTRACE_GETREGS, tid, 0, (int) &buf);
184 warning ("Couldn't get registers");
188 convert_to_gregset (&buf, registers, register_valid);
190 ret = ptrace (PTRACE_SETREGS, tid, 0, (int)buf);
193 warning ("Couldn't write registers");
200 /* Transfering floating-point registers between GDB, inferiors and cores. */
202 /* What is the address of st(N) within the fpregset_t structure F? */
203 #define FPREGSET_T_FPREG_ADDR(f, n) \
204 ((char *) &(f)->st_space + (n) * 10)
206 /* Fill GDB's register file with the floating-point register values in
209 supply_fpregset (fpregset_t *fpregsetp)
213 /* Supply the floating-point registers. */
214 for (i = 0; i < 8; i++)
215 supply_register (FP0_REGNUM + i, FPREGSET_T_FPREG_ADDR (fpregsetp, i));
217 supply_register (FCTRL_REGNUM, (char *) &fpregsetp->cwd);
218 supply_register (FSTAT_REGNUM, (char *) &fpregsetp->swd);
219 supply_register (FTAG_REGNUM, (char *) &fpregsetp->twd);
220 supply_register (FCOFF_REGNUM, (char *) &fpregsetp->fip);
221 supply_register (FDS_REGNUM, (char *) &fpregsetp->fos);
222 supply_register (FDOFF_REGNUM, (char *) &fpregsetp->foo);
224 /* Extract the code segment and opcode from the "fcs" member. */
228 l = fpregsetp->fcs & 0xffff;
229 supply_register (FCS_REGNUM, (char *) &l);
231 l = (fpregsetp->fcs >> 16) & ((1 << 11) - 1);
232 supply_register (FOP_REGNUM, (char *) &l);
237 /* Fill in an fpregset_t structure with selected data from a
238 gdb-format register file.
239 - FPREGSETP points to the structure to be filled.
240 - GDB_REGS points to the GDB-style register file providing the data.
241 - VALID is an array indicating which registers in GDB_REGS are
242 valid; the parts of *FPREGSETP that would hold registers marked
243 invalid in GDB_REGS are left unchanged. If VALID is zero, all
244 registers are assumed to be valid. */
246 convert_to_fpregset (fpregset_t *fpregsetp,
252 /* Fill in the floating-point registers. */
253 for (i = 0; i < 8; i++)
254 if (!valid || valid[i])
255 memcpy (FPREGSET_T_FPREG_ADDR (fpregsetp, i),
256 ®isters[REGISTER_BYTE (FP0_REGNUM + i)],
257 REGISTER_RAW_SIZE(FP0_REGNUM + i));
259 #define fill(MEMBER, REGNO) \
260 if (! valid || valid[(REGNO)]) \
261 memcpy (&fpregsetp->MEMBER, ®isters[REGISTER_BYTE (REGNO)], \
262 sizeof (fpregsetp->MEMBER))
264 fill (cwd, FCTRL_REGNUM);
265 fill (swd, FSTAT_REGNUM);
266 fill (twd, FTAG_REGNUM);
267 fill (fip, FCOFF_REGNUM);
268 fill (foo, FDOFF_REGNUM);
269 fill (fos, FDS_REGNUM);
273 if (! valid || valid[FCS_REGNUM])
275 = ((fpregsetp->fcs & ~0xffff)
276 | (* (int *) ®isters[REGISTER_BYTE (FCS_REGNUM)] & 0xffff));
278 if (! valid || valid[FOP_REGNUM])
280 = ((fpregsetp->fcs & 0xffff)
281 | ((*(int *) ®isters[REGISTER_BYTE (FOP_REGNUM)] & ((1 << 11) - 1))
286 /* Given a pointer to a floating point register set in (fpregset_t *)
287 format, update all of the registers from gdb's idea of the current
288 floating point register set. */
291 fill_fpregset (fpregset_t *fpregsetp,
294 convert_to_fpregset (fpregsetp, registers, 0);
298 /* Get the whole floating point state of the process and store the
299 floating point stack into registers[]. */
301 fetch_fpregs (int tid)
306 ret = ptrace (PTRACE_GETFPREGS, tid, 0, (int) &buf);
309 warning ("Couldn't get floating point status");
313 /* ptrace fills an fpregset_t, so we can use the same function we do
315 supply_fpregset (&buf);
319 /* Set the inferior's floating-point registers to the values in
320 registers[] --- but only those registers marked valid. */
322 store_fpregs (int tid)
327 ret = ptrace (PTRACE_GETFPREGS, tid, 0, (int) &buf);
330 warning ("Couldn't get floating point status");
334 convert_to_fpregset (&buf, registers, register_valid);
336 ret = ptrace (PTRACE_SETFPREGS, tid, 0, (int) &buf);
339 warning ("Couldn't write floating point status");
345 /* Transfering floating-point and SSE registers to and from GDB. */
348 /* PTRACE_GETXFPREGS is a Cygnus invention, since we wrote our own
349 Linux kernel patch for SSE support. That patch may or may not
350 actually make it into the official distribution. If you find that
351 years have gone by since this code was added, and Linux isn't using
352 PTRACE_GETXFPREGS, that means that our patch didn't make it, and
353 you can delete this code. */
355 #ifdef HAVE_PTRACE_GETXFPREGS
357 supply_xfpregset (struct user_xfpregs_struct *xfpregs)
361 /* Supply the floating-point registers. */
362 for (reg = 0; reg < 8; reg++)
363 supply_register (FP0_REGNUM + reg, (char *) &xfpregs->st_space[reg]);
366 supply_register (FCTRL_REGNUM, (char *) &xfpregs->cwd);
367 supply_register (FSTAT_REGNUM, (char *) &xfpregs->swd);
368 supply_register (FTAG_REGNUM, (char *) &xfpregs->twd);
369 supply_register (FCOFF_REGNUM, (char *) &xfpregs->fip);
370 supply_register (FDS_REGNUM, (char *) &xfpregs->fos);
371 supply_register (FDOFF_REGNUM, (char *) &xfpregs->foo);
373 /* Extract the code segment and opcode from the "fcs" member. */
377 l = xfpregs->fcs & 0xffff;
378 supply_register (FCS_REGNUM, (char *) &l);
380 l = (xfpregs->fcs >> 16) & ((1 << 11) - 1);
381 supply_register (FOP_REGNUM, (char *) &l);
385 /* Supply the SSE registers. */
386 for (reg = 0; reg < 8; reg++)
387 supply_register (XMM0_REGNUM + reg, (char *) &xfpregs->xmm_space[reg]);
388 supply_register (MXCSR_REGNUM, (char *) &xfpregs->mxcsr);
393 convert_to_xfpregset (struct user_xfpregs_struct *xfpregs,
399 /* Fill in the floating-point registers. */
400 for (reg = 0; reg < 8; reg++)
401 if (!valid || valid[reg])
402 memcpy (&xfpregs->st_space[reg],
403 ®isters[REGISTER_BYTE (FP0_REGNUM + reg)],
404 REGISTER_RAW_SIZE(FP0_REGNUM + reg));
406 #define fill(MEMBER, REGNO) \
407 if (! valid || valid[(REGNO)]) \
408 memcpy (&xfpregs->MEMBER, ®isters[REGISTER_BYTE (REGNO)], \
409 sizeof (xfpregs->MEMBER))
411 fill (cwd, FCTRL_REGNUM);
412 fill (swd, FSTAT_REGNUM);
413 fill (twd, FTAG_REGNUM);
414 fill (fip, FCOFF_REGNUM);
415 fill (foo, FDOFF_REGNUM);
416 fill (fos, FDS_REGNUM);
420 if (! valid || valid[FCS_REGNUM])
422 = ((xfpregs->fcs & ~0xffff)
423 | (* (int *) ®isters[REGISTER_BYTE (FCS_REGNUM)] & 0xffff));
425 if (! valid || valid[FOP_REGNUM])
427 = ((xfpregs->fcs & 0xffff)
428 | ((*(int *) ®isters[REGISTER_BYTE (FOP_REGNUM)] & ((1 << 11) - 1))
431 /* Fill in the XMM registers. */
432 for (reg = 0; reg < 8; reg++)
433 if (! valid || valid[reg])
434 memcpy (&xfpregs->xmm_space[reg],
435 ®isters[REGISTER_BYTE (XMM0_REGNUM + reg)],
436 REGISTER_RAW_SIZE (XMM0_REGNUM + reg));
440 /* Make a PTRACE_GETXFPREGS request, and supply all the register
441 values that yields to GDB. */
443 fetch_xfpregs (int tid)
446 struct user_xfpregs_struct xfpregs;
448 if (! have_ptrace_getxfpregs)
451 ret = ptrace (PTRACE_GETXFPREGS, tid, 0, &xfpregs);
456 have_ptrace_getxfpregs = 0;
460 warning ("couldn't read floating-point and SSE registers.");
464 supply_xfpregset (&xfpregs);
469 /* Send all the valid register values in GDB's register file covered
470 by the PTRACE_SETXFPREGS request to the inferior. */
472 store_xfpregs (int tid)
475 struct user_xfpregs_struct xfpregs;
477 if (! have_ptrace_getxfpregs)
480 ret = ptrace (PTRACE_GETXFPREGS, tid, 0, &xfpregs);
485 have_ptrace_getxfpregs = 0;
489 warning ("couldn't read floating-point and SSE registers.");
493 convert_to_xfpregset (&xfpregs, registers, register_valid);
495 if (ptrace (PTRACE_SETXFPREGS, tid, 0, &xfpregs) < 0)
497 warning ("Couldn't write floating-point and SSE registers.");
505 /* Fill the XMM registers in the register file with dummy values. For
506 cases where we don't have access to the XMM registers. I think
507 this is cleaner than printing a warning. For a cleaner solution,
508 we should gdbarchify the i386 family. */
512 /* C doesn't have a syntax for NaN's, so write it out as an array of
514 static long dummy[4] = { 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff };
515 static long mxcsr = 0x1f80;
518 for (reg = 0; reg < 8; reg++)
519 supply_register (XMM0_REGNUM + reg, (char *) dummy);
520 supply_register (MXCSR_REGNUM, (char *) &mxcsr);
525 /* Stub versions of the above routines, for systems that don't have
526 PTRACE_GETXFPREGS. */
527 static int store_xfpregs (int tid) { return 0; }
528 static int fetch_xfpregs (int tid) { return 0; }
529 static void dummy_sse_values () {}
534 /* Transferring arbitrary registers between GDB and inferior. */
536 /* Fetch registers from the child process.
537 Fetch all if regno == -1, otherwise fetch all ordinary
538 registers or all floating point registers depending
539 upon the value of regno. */
542 fetch_inferior_registers (int regno)
544 /* linux lwp id's are process id's */
547 if ((tid = TIDGET (inferior_pid)) == 0)
548 tid = inferior_pid; /* not a threaded program */
550 /* Use the xfpregs requests whenever possible, since they transfer
551 more registers in one system call, and we'll cache the results.
552 But remember that fetch_xfpregs can fail, and return zero. */
556 if (fetch_xfpregs (tid))
562 if (GETREGS_SUPPLIES (regno))
568 if (GETXFPREGS_SUPPLIES (regno))
570 if (fetch_xfpregs (tid))
573 /* Either our processor or our kernel doesn't support the SSE
574 registers, so read the FP registers in the traditional way,
575 and fill the SSE registers with dummy values. It would be
576 more graceful to handle differences in the register set using
577 gdbarch. Until then, this will at least make things work
584 internal_error ("i386-linux-nat.c (fetch_inferior_registers): "
585 "got request for bad register number %d", regno);
589 /* Store our register values back into the inferior.
590 If REGNO is -1, do this for all registers.
591 Otherwise, REGNO specifies which register, which
592 then determines whether we store all ordinary
593 registers or all of the floating point registers. */
596 store_inferior_registers (regno)
599 /* linux lwp id's are process id's */
602 if ((tid = TIDGET (inferior_pid)) == 0)
603 tid = inferior_pid; /* not a threaded program */
605 /* Use the xfpregs requests whenever possible, since they transfer
606 more registers in one system call. But remember that
607 store_xfpregs can fail, and return zero. */
611 if (store_xfpregs (tid))
617 if (GETREGS_SUPPLIES (regno))
623 if (GETXFPREGS_SUPPLIES (regno))
625 if (store_xfpregs (tid))
628 /* Either our processor or our kernel doesn't support the SSE
629 registers, so just write the FP registers in the traditional way. */
634 internal_error ("i386-linux-nat.c (store_inferior_registers): "
635 "got request to store bad register number %d", regno);
640 /* Interpreting register set info found in core files. */
642 /* Provide registers to GDB from a core file.
644 (We can't use the generic version of this function in
645 core-regset.c, because Linux has *three* different kinds of
646 register set notes. core-regset.c would have to call
647 supply_xfpregset, which most platforms don't have.)
649 CORE_REG_SECT points to an array of bytes, which are the contents
650 of a `note' from a core file which BFD thinks might contain
651 register contents. CORE_REG_SIZE is its size.
653 WHICH says which register set corelow suspects this is:
654 0 --- the general register set, in gregset format
655 2 --- the floating-point register set, in fpregset format
656 3 --- the extended floating-point register set, in struct
657 user_xfpregs_struct format
659 DUMMY isn't used on Linux. */
661 i386_linux_fetch_core_registers (char *core_reg_sect,
662 unsigned core_reg_size,
672 if (core_reg_size != sizeof (gregset))
673 warning ("wrong size gregset struct in core file");
676 memcpy (&gregset, core_reg_sect, sizeof (gregset));
677 supply_gregset (&gregset);
682 if (core_reg_size != sizeof (fpregset))
683 warning ("wrong size fpregset struct in core file");
686 memcpy (&fpregset, core_reg_sect, sizeof (fpregset));
687 supply_fpregset (&fpregset);
691 #ifdef HAVE_PTRACE_GETXFPREGS
693 struct user_xfpregs_struct xfpregset;
695 if (core_reg_size != sizeof (struct user_xfpregs_struct))
696 warning ("wrong size user_xfpregs_struct in core file");
699 memcpy (&xfpregset, core_reg_sect, sizeof (xfpregset));
700 supply_xfpregset (&xfpregset);
707 /* We've covered all the kinds of registers we know about here,
708 so this must be something we wouldn't know what to do with
709 anyway. Just ignore it. */
715 static struct core_fns i386_linux_nat_core_fns =
717 bfd_target_elf_flavour, /* core_flavour */
718 default_check_format, /* check_format */
719 default_core_sniffer, /* core_sniffer */
720 i386_linux_fetch_core_registers, /* core_read_registers */
725 /* Calling functions in shared libraries. */
727 /* Find the minimal symbol named NAME, and return both the minsym
728 struct and its objfile. This probably ought to be in minsym.c, but
729 everything there is trying to deal with things like C++ and
730 SOFUN_ADDRESS_MAYBE_TURQUOISE, ... Since this is so simple, it may
731 be considered too special-purpose for general consumption. */
733 static struct minimal_symbol *
734 find_minsym_and_objfile (char *name, struct objfile **objfile_p)
736 struct objfile *objfile;
738 ALL_OBJFILES (objfile)
740 struct minimal_symbol *msym;
742 ALL_OBJFILE_MSYMBOLS (objfile, msym)
744 if (SYMBOL_NAME (msym)
745 && STREQ (SYMBOL_NAME (msym), name))
747 *objfile_p = objfile;
758 skip_hurd_resolver (CORE_ADDR pc)
760 /* The HURD dynamic linker is part of the GNU C library, so many
761 GNU/Linux distributions use it. (All ELF versions, as far as I
762 know.) An unresolved PLT entry points to "_dl_runtime_resolve",
763 which calls "fixup" to patch the PLT, and then passes control to
766 We look for the symbol `_dl_runtime_resolve', and find `fixup' in
767 the same objfile. If we are at the entry point of `fixup', then
768 we set a breakpoint at the return address (at the top of the
769 stack), and continue.
771 It's kind of gross to do all these checks every time we're
772 called, since they don't change once the executable has gotten
773 started. But this is only a temporary hack --- upcoming versions
774 of Linux will provide a portable, efficient interface for
775 debugging programs that use shared libraries. */
777 struct objfile *objfile;
778 struct minimal_symbol *resolver
779 = find_minsym_and_objfile ("_dl_runtime_resolve", &objfile);
783 struct minimal_symbol *fixup
784 = lookup_minimal_symbol ("fixup", 0, objfile);
786 if (fixup && SYMBOL_VALUE_ADDRESS (fixup) == pc)
787 return (SAVED_PC_AFTER_CALL (get_current_frame ()));
794 /* See the comments for SKIP_SOLIB_RESOLVER at the top of infrun.c.
796 1) decides whether a PLT has sent us into the linker to resolve
797 a function reference, and
798 2) if so, tells us where to set a temporary breakpoint that will
799 trigger when the dynamic linker is done. */
802 i386_linux_skip_solib_resolver (CORE_ADDR pc)
806 /* Plug in functions for other kinds of resolvers here. */
807 result = skip_hurd_resolver (pc);
816 /* Module initialization. */
819 _initialize_i386_linux_nat ()
821 add_core_fns (&i386_linux_nat_core_fns);