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>
37 /* This is a duplicate of the table in i386-xdep.c. */
48 /* Which ptrace request retrieves which registers?
49 These apply to the corresponding SET requests as well. */
50 #define GETREGS_SUPPLIES(regno) \
51 (0 <= (regno) && (regno) <= 15)
52 #define GETFPREGS_SUPPLIES(regno) \
53 (FP0_REGNUM <= (regno) && (regno) <= LAST_FPU_CTRL_REGNUM)
54 #define GETXFPREGS_SUPPLIES(regno) \
55 (FP0_REGNUM <= (regno) && (regno) <= MXCSR_REGNUM)
57 /* Does the current host support the GETXFPREGS request? The header
58 file may or may not define it, and even if it is defined, the
59 kernel will return EIO if it's running on a pre-SSE processor.
61 My instinct is to attach this to some architecture- or
62 target-specific data structure, but really, a particular GDB
63 process can only run on top of one kernel at a time. So it's okay
64 for this to be a simple variable. */
65 int have_ptrace_getxfpregs =
66 #ifdef HAVE_PTRACE_GETXFPREGS
75 /* Transfering the general registers between GDB, inferiors and core files. */
77 /* Given a pointer to a general register set in struct user format
78 (gregset_t *), unpack the register contents and supply them as
79 gdb's idea of the current register values. */
81 supply_gregset (gregsetp)
85 register greg_t *regp = (greg_t *) gregsetp;
87 for (regi = 0; regi < NUM_GREGS; regi++)
89 supply_register (regi, (char *) (regp + regmap[regi]));
94 /* Fill in a gregset_t object with selected data from a gdb-format
96 - GREGSETP points to the gregset_t object to be filled.
97 - GDB_REGS points to the GDB-style register file providing the data.
98 - VALID is an array indicating which registers in GDB_REGS are
99 valid; the parts of *GREGSETP that would hold registers marked
100 invalid in GDB_REGS are left unchanged. If VALID is zero, all
101 registers are assumed to be valid. */
103 convert_to_gregset (gregset_t *gregsetp,
108 register greg_t *regp = (greg_t *) gregsetp;
110 for (regi = 0; regi < NUM_GREGS; regi++)
111 if (! valid || valid[regi])
112 *(regp + regmap[regi]) = * (int *) ®isters[REGISTER_BYTE (regi)];
116 /* Store GDB's value for REGNO in *GREGSETP. If REGNO is -1, do all
119 fill_gregset (gregset_t *gregsetp,
123 convert_to_gregset (gregsetp, registers, 0);
126 signed char valid[NUM_GREGS];
127 memset (valid, 0, sizeof (valid));
129 convert_to_gregset (gregsetp, valid, valid);
134 /* Read the general registers from the process, and store them
142 ret = ptrace (PTRACE_GETREGS, inferior_pid, 0, (int) &buf);
145 warning ("Couldn't get registers");
149 supply_gregset (&buf);
153 /* Set the inferior's general registers to the values in registers[]
154 --- but only those registers marked as valid. */
161 ret = ptrace (PTRACE_GETREGS, inferior_pid, 0, (int) &buf);
164 warning ("Couldn't get registers");
168 convert_to_gregset (&buf, registers, register_valid);
170 ret = ptrace (PTRACE_SETREGS, inferior_pid, 0, (int)buf);
173 warning ("Couldn't write registers");
180 /* Transfering floating-point registers between GDB, inferiors and cores. */
182 /* What is the address of st(N) within the fpregset_t structure F? */
183 #define FPREGSET_T_FPREG_ADDR(f, n) \
184 ((char *) &(f)->st_space + (n) * 10)
186 /* Fill GDB's register file with the floating-point register values in
189 supply_fpregset (fpregset_t *fpregsetp)
193 /* Supply the floating-point registers. */
194 for (i = 0; i < 8; i++)
195 supply_register (FP0_REGNUM + i, FPREGSET_T_FPREG_ADDR (fpregsetp, i));
197 supply_register (FCTRL_REGNUM, (char *) &fpregsetp->cwd);
198 supply_register (FSTAT_REGNUM, (char *) &fpregsetp->swd);
199 supply_register (FTAG_REGNUM, (char *) &fpregsetp->twd);
200 supply_register (FCOFF_REGNUM, (char *) &fpregsetp->fip);
201 supply_register (FDS_REGNUM, (char *) &fpregsetp->fos);
202 supply_register (FDOFF_REGNUM, (char *) &fpregsetp->foo);
204 /* Extract the code segment and opcode from the "fcs" member. */
208 l = fpregsetp->fcs & 0xffff;
209 supply_register (FCS_REGNUM, (char *) &l);
211 l = (fpregsetp->fcs >> 16) & ((1 << 11) - 1);
212 supply_register (FOP_REGNUM, (char *) &l);
217 /* Fill in an fpregset_t structure with selected data from a
218 gdb-format register file.
219 - FPREGSETP points to the structure to be filled.
220 - GDB_REGS points to the GDB-style register file providing the data.
221 - VALID is an array indicating which registers in GDB_REGS are
222 valid; the parts of *FPREGSETP that would hold registers marked
223 invalid in GDB_REGS are left unchanged. If VALID is zero, all
224 registers are assumed to be valid. */
226 convert_to_fpregset (fpregset_t *fpregsetp,
232 /* Fill in the floating-point registers. */
233 for (i = 0; i < 8; i++)
234 if (!valid || valid[i])
235 memcpy (FPREGSET_T_FPREG_ADDR (fpregsetp, i),
236 ®isters[REGISTER_BYTE (FP0_REGNUM + i)],
237 REGISTER_RAW_SIZE(FP0_REGNUM + i));
239 #define fill(MEMBER, REGNO) \
240 if (! valid || valid[(REGNO)]) \
241 memcpy (&fpregsetp->MEMBER, ®isters[REGISTER_BYTE (REGNO)], \
242 sizeof (fpregsetp->MEMBER))
244 fill (cwd, FCTRL_REGNUM);
245 fill (swd, FSTAT_REGNUM);
246 fill (twd, FTAG_REGNUM);
247 fill (fip, FCOFF_REGNUM);
248 fill (foo, FDOFF_REGNUM);
249 fill (fos, FDS_REGNUM);
253 if (! valid || valid[FCS_REGNUM])
255 = ((fpregsetp->fcs & ~0xffff)
256 | (* (int *) ®isters[REGISTER_BYTE (FCS_REGNUM)] & 0xffff));
258 if (! valid || valid[FOP_REGNUM])
260 = ((fpregsetp->fcs & 0xffff)
261 | ((*(int *) ®isters[REGISTER_BYTE (FOP_REGNUM)] & ((1 << 11) - 1))
266 /* Given a pointer to a floating point register set in (fpregset_t *)
267 format, update all of the registers from gdb's idea of the current
268 floating point register set. */
271 fill_fpregset (fpregset_t *fpregsetp,
274 convert_to_fpregset (fpregsetp, registers, 0);
278 /* Get the whole floating point state of the process and store the
279 floating point stack into registers[]. */
286 ret = ptrace (PTRACE_GETFPREGS, inferior_pid, 0, (int) &buf);
289 warning ("Couldn't get floating point status");
293 /* ptrace fills an fpregset_t, so we can use the same function we do
295 supply_fpregset (&buf);
299 /* Set the inferior's floating-point registers to the values in
300 registers[] --- but only those registers marked valid. */
307 ret = ptrace (PTRACE_GETFPREGS, inferior_pid, 0, (int) &buf);
310 warning ("Couldn't get floating point status");
314 convert_to_fpregset (&buf, registers, register_valid);
316 ret = ptrace (PTRACE_SETFPREGS, inferior_pid, 0, (int) &buf);
319 warning ("Couldn't write floating point status");
325 /* Transfering floating-point and SSE registers to and from GDB. */
328 /* PTRACE_GETXFPREGS is a Cygnus invention, since we wrote our own
329 Linux kernel patch for SSE support. That patch may or may not
330 actually make it into the official distribution. If you find that
331 years have gone by since this code was added, and Linux isn't using
332 PTRACE_GETXFPREGS, that means that our patch didn't make it, and
333 you can delete this code. */
335 #ifdef HAVE_PTRACE_GETXFPREGS
337 supply_xfpregset (struct user_xfpregs_struct *xfpregs)
341 /* Supply the floating-point registers. */
342 for (reg = 0; reg < 8; reg++)
343 supply_register (FP0_REGNUM + reg, (char *) &xfpregs->st_space[reg]);
346 supply_register (FCTRL_REGNUM, (char *) &xfpregs->cwd);
347 supply_register (FSTAT_REGNUM, (char *) &xfpregs->swd);
348 supply_register (FTAG_REGNUM, (char *) &xfpregs->twd);
349 supply_register (FCOFF_REGNUM, (char *) &xfpregs->fip);
350 supply_register (FDS_REGNUM, (char *) &xfpregs->fos);
351 supply_register (FDOFF_REGNUM, (char *) &xfpregs->foo);
353 /* Extract the code segment and opcode from the "fcs" member. */
357 l = xfpregs->fcs & 0xffff;
358 supply_register (FCS_REGNUM, (char *) &l);
360 l = (xfpregs->fcs >> 16) & ((1 << 11) - 1);
361 supply_register (FOP_REGNUM, (char *) &l);
365 /* Supply the SSE registers. */
366 for (reg = 0; reg < 8; reg++)
367 supply_register (XMM0_REGNUM + reg, (char *) &xfpregs->xmm_space[reg]);
368 supply_register (MXCSR_REGNUM, (char *) &xfpregs->mxcsr);
373 convert_to_xfpregset (struct user_xfpregs_struct *xfpregs,
379 /* Fill in the floating-point registers. */
380 for (reg = 0; reg < 8; reg++)
381 if (!valid || valid[reg])
382 memcpy (&xfpregs->st_space[reg],
383 ®isters[REGISTER_BYTE (FP0_REGNUM + reg)],
384 REGISTER_RAW_SIZE(FP0_REGNUM + reg));
386 #define fill(MEMBER, REGNO) \
387 if (! valid || valid[(REGNO)]) \
388 memcpy (&xfpregs->MEMBER, ®isters[REGISTER_BYTE (REGNO)], \
389 sizeof (xfpregs->MEMBER))
391 fill (cwd, FCTRL_REGNUM);
392 fill (swd, FSTAT_REGNUM);
393 fill (twd, FTAG_REGNUM);
394 fill (fip, FCOFF_REGNUM);
395 fill (foo, FDOFF_REGNUM);
396 fill (fos, FDS_REGNUM);
400 if (! valid || valid[FCS_REGNUM])
402 = ((xfpregs->fcs & ~0xffff)
403 | (* (int *) ®isters[REGISTER_BYTE (FCS_REGNUM)] & 0xffff));
405 if (! valid || valid[FOP_REGNUM])
407 = ((xfpregs->fcs & 0xffff)
408 | ((*(int *) ®isters[REGISTER_BYTE (FOP_REGNUM)] & ((1 << 11) - 1))
411 /* Fill in the XMM registers. */
412 for (reg = 0; reg < 8; reg++)
413 if (! valid || valid[reg])
414 memcpy (&xfpregs->xmm_space[reg],
415 ®isters[REGISTER_BYTE (XMM0_REGNUM + reg)],
416 REGISTER_RAW_SIZE (XMM0_REGNUM + reg));
420 /* Make a PTRACE_GETXFPREGS request, and supply all the register
421 values that yields to GDB. */
426 struct user_xfpregs_struct xfpregs;
428 if (! have_ptrace_getxfpregs)
431 ret = ptrace (PTRACE_GETXFPREGS, inferior_pid, 0, &xfpregs);
436 have_ptrace_getxfpregs = 0;
440 warning ("couldn't read floating-point and SSE registers.");
444 supply_xfpregset (&xfpregs);
449 /* Send all the valid register values in GDB's register file covered
450 by the PTRACE_SETXFPREGS request to the inferior. */
455 struct user_xfpregs_struct xfpregs;
457 if (! have_ptrace_getxfpregs)
460 ret = ptrace (PTRACE_GETXFPREGS, inferior_pid, 0, &xfpregs);
465 have_ptrace_getxfpregs = 0;
469 warning ("couldn't read floating-point and SSE registers.");
473 convert_to_xfpregset (&xfpregs, registers, register_valid);
475 if (ptrace (PTRACE_SETXFPREGS, inferior_pid, 0, &xfpregs) < 0)
477 warning ("Couldn't write floating-point and SSE registers.");
485 /* Fill the XMM registers in the register file with dummy values. For
486 cases where we don't have access to the XMM registers. I think
487 this is cleaner than printing a warning. For a cleaner solution,
488 we should gdbarchify the i386 family. */
492 /* C doesn't have a syntax for NaN's, so write it out as an array of
494 static long dummy[4] = { 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff };
495 static long mxcsr = 0x1f80;
498 for (reg = 0; reg < 8; reg++)
499 supply_register (XMM0_REGNUM + reg, (char *) dummy);
500 supply_register (MXCSR_REGNUM, (char *) &mxcsr);
505 /* Stub versions of the above routines, for systems that don't have
506 PTRACE_GETXFPREGS. */
507 static int store_xfpregs () { return 0; }
508 static int fetch_xfpregs () { return 0; }
509 static void dummy_sse_values () {}
514 /* Transferring arbitrary registers between GDB and inferior. */
516 /* Fetch registers from the child process.
517 Fetch all if regno == -1, otherwise fetch all ordinary
518 registers or all floating point registers depending
519 upon the value of regno. */
522 fetch_inferior_registers (int regno)
524 /* Use the xfpregs requests whenever possible, since they transfer
525 more registers in one system call, and we'll cache the results.
526 But remember that fetch_xfpregs can fail, and return zero. */
530 if (fetch_xfpregs ())
536 if (GETREGS_SUPPLIES (regno))
542 if (GETXFPREGS_SUPPLIES (regno))
544 if (fetch_xfpregs ())
547 /* Either our processor or our kernel doesn't support the SSE
548 registers, so read the FP registers in the traditional way,
549 and fill the SSE registers with dummy values. It would be
550 more graceful to handle differences in the register set using
551 gdbarch. Until then, this will at least make things work
558 internal_error ("i386-linux-nat.c (fetch_inferior_registers): "
559 "got request for bad register number %d", regno);
563 /* Store our register values back into the inferior.
564 If REGNO is -1, do this for all registers.
565 Otherwise, REGNO specifies which register, which
566 then determines whether we store all ordinary
567 registers or all of the floating point registers. */
570 store_inferior_registers (regno)
573 /* Use the xfpregs requests whenever possible, since they transfer
574 more registers in one system call. But remember that
575 fetch_xfpregs can fail, and return zero. */
579 if (store_xfpregs ())
585 if (GETREGS_SUPPLIES (regno))
591 if (GETXFPREGS_SUPPLIES (regno))
593 if (store_xfpregs ())
596 /* Either our processor or our kernel doesn't support the SSE
597 registers, so just write the FP registers in the traditional way. */
602 internal_error ("i386-linux-nat.c (store_inferior_registers): "
603 "got request to store bad register number %d", regno);
608 /* Calling functions in shared libraries. */
610 /* Find the minimal symbol named NAME, and return both the minsym
611 struct and its objfile. This probably ought to be in minsym.c, but
612 everything there is trying to deal with things like C++ and
613 SOFUN_ADDRESS_MAYBE_TURQUOISE, ... Since this is so simple, it may
614 be considered too special-purpose for general consumption. */
616 static struct minimal_symbol *
617 find_minsym_and_objfile (char *name, struct objfile **objfile_p)
619 struct objfile *objfile;
621 ALL_OBJFILES (objfile)
623 struct minimal_symbol *msym;
625 ALL_OBJFILE_MSYMBOLS (objfile, msym)
627 if (SYMBOL_NAME (msym)
628 && STREQ (SYMBOL_NAME (msym), name))
630 *objfile_p = objfile;
641 skip_hurd_resolver (CORE_ADDR pc)
643 /* The HURD dynamic linker is part of the GNU C library, so many
644 GNU/Linux distributions use it. (All ELF versions, as far as I
645 know.) An unresolved PLT entry points to "_dl_runtime_resolve",
646 which calls "fixup" to patch the PLT, and then passes control to
649 We look for the symbol `_dl_runtime_resolve', and find `fixup' in
650 the same objfile. If we are at the entry point of `fixup', then
651 we set a breakpoint at the return address (at the top of the
652 stack), and continue.
654 It's kind of gross to do all these checks every time we're
655 called, since they don't change once the executable has gotten
656 started. But this is only a temporary hack --- upcoming versions
657 of Linux will provide a portable, efficient interface for
658 debugging programs that use shared libraries. */
660 struct objfile *objfile;
661 struct minimal_symbol *resolver
662 = find_minsym_and_objfile ("_dl_runtime_resolve", &objfile);
666 struct minimal_symbol *fixup
667 = lookup_minimal_symbol ("fixup", 0, objfile);
669 if (fixup && SYMBOL_VALUE_ADDRESS (fixup) == pc)
670 return (SAVED_PC_AFTER_CALL (get_current_frame ()));
677 /* See the comments for SKIP_SOLIB_RESOLVER at the top of infrun.c.
679 1) decides whether a PLT has sent us into the linker to resolve
680 a function reference, and
681 2) if so, tells us where to set a temporary breakpoint that will
682 trigger when the dynamic linker is done. */
685 i386_linux_skip_solib_resolver (CORE_ADDR pc)
689 /* Plug in functions for other kinds of resolvers here. */
690 result = skip_hurd_resolver (pc);