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 PTRACE_GETXFPREGS is a Cygnus invention, since we wrote our own
62 Linux kernel patch for SSE support. That patch may or may not
63 actually make it into the official distribution. If you find that
64 years have gone by since this stuff was added, and Linux isn't
65 using PTRACE_GETXFPREGS, that means that our patch didn't make it,
66 and you can delete this, and the related code.
68 My instinct is to attach this to some architecture- or
69 target-specific data structure, but really, a particular GDB
70 process can only run on top of one kernel at a time. So it's okay
71 for this to be a simple variable. */
72 int have_ptrace_getxfpregs
=
73 #ifdef HAVE_PTRACE_GETXFPREGS
82 /* Transfering the general registers between GDB, inferiors and core files. */
84 /* Given a pointer to a general register set in struct user format
85 (gregset_t *), unpack the register contents and supply them as
86 gdb's idea of the current register values. */
88 supply_gregset (gregsetp
)
92 register greg_t
*regp
= (greg_t
*) gregsetp
;
94 for (regi
= 0; regi
< NUM_GREGS
; regi
++)
96 supply_register (regi
, (char *) (regp
+ regmap
[regi
]));
101 /* Fill in a gregset_t object with selected data from a gdb-format
103 - GREGSETP points to the gregset_t object to be filled.
104 - GDB_REGS points to the GDB-style register file providing the data.
105 - VALID is an array indicating which registers in GDB_REGS are
106 valid; the parts of *GREGSETP that would hold registers marked
107 invalid in GDB_REGS are left unchanged. If VALID is zero, all
108 registers are assumed to be valid. */
110 convert_to_gregset (gregset_t
*gregsetp
,
115 register greg_t
*regp
= (greg_t
*) gregsetp
;
117 for (regi
= 0; regi
< NUM_GREGS
; regi
++)
118 if (! valid
|| valid
[regi
])
119 *(regp
+ regmap
[regi
]) = * (int *) ®isters
[REGISTER_BYTE (regi
)];
123 /* Store GDB's value for REGNO in *GREGSETP. If REGNO is -1, do all
126 fill_gregset (gregset_t
*gregsetp
,
130 convert_to_gregset (gregsetp
, registers
, 0);
131 else if (regno
>= 0 && regno
< NUM_GREGS
)
133 signed char valid
[NUM_GREGS
];
134 memset (valid
, 0, sizeof (valid
));
136 convert_to_gregset (gregsetp
, valid
, valid
);
141 /* Read the general registers from the process, and store them
149 ret
= ptrace (PTRACE_GETREGS
, inferior_pid
, 0, (int) &buf
);
152 warning ("Couldn't get registers");
156 supply_gregset (&buf
);
160 /* Set the inferior's general registers to the values in registers[]
161 --- but only those registers marked as valid. */
168 ret
= ptrace (PTRACE_GETREGS
, inferior_pid
, 0, (int) &buf
);
171 warning ("Couldn't get registers");
175 convert_to_gregset (&buf
, registers
, register_valid
);
177 ret
= ptrace (PTRACE_SETREGS
, inferior_pid
, 0, (int)buf
);
180 warning ("Couldn't write registers");
187 /* Transfering floating-point registers between GDB, inferiors and cores. */
189 /* What is the address of st(N) within the fpregset_t structure F? */
190 #define FPREGSET_T_FPREG_ADDR(f, n) \
191 ((char *) &(f)->st_space + (n) * 10)
193 /* Fill GDB's register file with the floating-point register values in
196 supply_fpregset (fpregset_t
*fpregsetp
)
200 /* Supply the floating-point registers. */
201 for (i
= 0; i
< 8; i
++)
202 supply_register (FP0_REGNUM
+ i
, FPREGSET_T_FPREG_ADDR (fpregsetp
, i
));
204 supply_register (FCTRL_REGNUM
, (char *) &fpregsetp
->cwd
);
205 supply_register (FSTAT_REGNUM
, (char *) &fpregsetp
->swd
);
206 supply_register (FTAG_REGNUM
, (char *) &fpregsetp
->twd
);
207 supply_register (FCOFF_REGNUM
, (char *) &fpregsetp
->fip
);
208 supply_register (FDS_REGNUM
, (char *) &fpregsetp
->fos
);
209 supply_register (FDOFF_REGNUM
, (char *) &fpregsetp
->foo
);
211 /* Extract the code segment and opcode from the "fcs" member. */
215 l
= fpregsetp
->fcs
& 0xffff;
216 supply_register (FCS_REGNUM
, (char *) &l
);
218 l
= (fpregsetp
->fcs
>> 16) & ((1 << 11) - 1);
219 supply_register (FOP_REGNUM
, (char *) &l
);
224 /* Fill in an fpregset_t structure with selected data from a
225 gdb-format register file.
226 - FPREGSETP points to the structure to be filled.
227 - GDB_REGS points to the GDB-style register file providing the data.
228 - VALID is an array indicating which registers in GDB_REGS are
229 valid; the parts of *FPREGSETP that would hold registers marked
230 invalid in GDB_REGS are left unchanged. If VALID is zero, all
231 registers are assumed to be valid. */
233 convert_to_fpregset (fpregset_t
*fpregsetp
,
239 /* Fill in the floating-point registers. */
240 for (i
= 0; i
< 8; i
++)
241 if (!valid
|| valid
[i
])
242 memcpy (FPREGSET_T_FPREG_ADDR (fpregsetp
, i
),
243 ®isters
[REGISTER_BYTE (FP0_REGNUM
+ i
)],
244 REGISTER_RAW_SIZE(FP0_REGNUM
+ i
));
246 #define fill(MEMBER, REGNO) \
247 if (! valid || valid[(REGNO)]) \
248 memcpy (&fpregsetp->MEMBER, ®isters[REGISTER_BYTE (REGNO)], \
249 sizeof (fpregsetp->MEMBER))
251 fill (cwd
, FCTRL_REGNUM
);
252 fill (swd
, FSTAT_REGNUM
);
253 fill (twd
, FTAG_REGNUM
);
254 fill (fip
, FCOFF_REGNUM
);
255 fill (foo
, FDOFF_REGNUM
);
256 fill (fos
, FDS_REGNUM
);
260 if (! valid
|| valid
[FCS_REGNUM
])
262 = ((fpregsetp
->fcs
& ~0xffff)
263 | (* (int *) ®isters
[REGISTER_BYTE (FCS_REGNUM
)] & 0xffff));
265 if (! valid
|| valid
[FOP_REGNUM
])
267 = ((fpregsetp
->fcs
& 0xffff)
268 | ((*(int *) ®isters
[REGISTER_BYTE (FOP_REGNUM
)] & ((1 << 11) - 1))
273 /* Given a pointer to a floating point register set in (fpregset_t *)
274 format, update all of the registers from gdb's idea of the current
275 floating point register set. */
278 fill_fpregset (fpregset_t
*fpregsetp
,
281 convert_to_fpregset (fpregsetp
, registers
, 0);
285 /* Get the whole floating point state of the process and store the
286 floating point stack into registers[]. */
293 ret
= ptrace (PTRACE_GETFPREGS
, inferior_pid
, 0, (int) &buf
);
296 warning ("Couldn't get floating point status");
300 /* ptrace fills an fpregset_t, so we can use the same function we do
302 supply_fpregset (&buf
);
306 /* Set the inferior's floating-point registers to the values in
307 registers[] --- but only those registers marked valid. */
314 ret
= ptrace (PTRACE_GETFPREGS
, inferior_pid
, 0, (int) &buf
);
317 warning ("Couldn't get floating point status");
321 convert_to_fpregset (&buf
, registers
, register_valid
);
323 ret
= ptrace (PTRACE_SETFPREGS
, inferior_pid
, 0, (int) &buf
);
326 warning ("Couldn't write floating point status");
332 /* Transfering floating-point and SSE registers to and from GDB. */
335 /* PTRACE_GETXFPREGS is a Cygnus invention, since we wrote our own
336 Linux kernel patch for SSE support. That patch may or may not
337 actually make it into the official distribution. If you find that
338 years have gone by since this code was added, and Linux isn't using
339 PTRACE_GETXFPREGS, that means that our patch didn't make it, and
340 you can delete this code. */
342 #ifdef HAVE_PTRACE_GETXFPREGS
344 supply_xfpregset (struct user_xfpregs_struct
*xfpregs
)
348 /* Supply the floating-point registers. */
349 for (reg
= 0; reg
< 8; reg
++)
350 supply_register (FP0_REGNUM
+ reg
, (char *) &xfpregs
->st_space
[reg
]);
353 supply_register (FCTRL_REGNUM
, (char *) &xfpregs
->cwd
);
354 supply_register (FSTAT_REGNUM
, (char *) &xfpregs
->swd
);
355 supply_register (FTAG_REGNUM
, (char *) &xfpregs
->twd
);
356 supply_register (FCOFF_REGNUM
, (char *) &xfpregs
->fip
);
357 supply_register (FDS_REGNUM
, (char *) &xfpregs
->fos
);
358 supply_register (FDOFF_REGNUM
, (char *) &xfpregs
->foo
);
360 /* Extract the code segment and opcode from the "fcs" member. */
364 l
= xfpregs
->fcs
& 0xffff;
365 supply_register (FCS_REGNUM
, (char *) &l
);
367 l
= (xfpregs
->fcs
>> 16) & ((1 << 11) - 1);
368 supply_register (FOP_REGNUM
, (char *) &l
);
372 /* Supply the SSE registers. */
373 for (reg
= 0; reg
< 8; reg
++)
374 supply_register (XMM0_REGNUM
+ reg
, (char *) &xfpregs
->xmm_space
[reg
]);
375 supply_register (MXCSR_REGNUM
, (char *) &xfpregs
->mxcsr
);
380 convert_to_xfpregset (struct user_xfpregs_struct
*xfpregs
,
386 /* Fill in the floating-point registers. */
387 for (reg
= 0; reg
< 8; reg
++)
388 if (!valid
|| valid
[reg
])
389 memcpy (&xfpregs
->st_space
[reg
],
390 ®isters
[REGISTER_BYTE (FP0_REGNUM
+ reg
)],
391 REGISTER_RAW_SIZE(FP0_REGNUM
+ reg
));
393 #define fill(MEMBER, REGNO) \
394 if (! valid || valid[(REGNO)]) \
395 memcpy (&xfpregs->MEMBER, ®isters[REGISTER_BYTE (REGNO)], \
396 sizeof (xfpregs->MEMBER))
398 fill (cwd
, FCTRL_REGNUM
);
399 fill (swd
, FSTAT_REGNUM
);
400 fill (twd
, FTAG_REGNUM
);
401 fill (fip
, FCOFF_REGNUM
);
402 fill (foo
, FDOFF_REGNUM
);
403 fill (fos
, FDS_REGNUM
);
407 if (! valid
|| valid
[FCS_REGNUM
])
409 = ((xfpregs
->fcs
& ~0xffff)
410 | (* (int *) ®isters
[REGISTER_BYTE (FCS_REGNUM
)] & 0xffff));
412 if (! valid
|| valid
[FOP_REGNUM
])
414 = ((xfpregs
->fcs
& 0xffff)
415 | ((*(int *) ®isters
[REGISTER_BYTE (FOP_REGNUM
)] & ((1 << 11) - 1))
418 /* Fill in the XMM registers. */
419 for (reg
= 0; reg
< 8; reg
++)
420 if (! valid
|| valid
[reg
])
421 memcpy (&xfpregs
->xmm_space
[reg
],
422 ®isters
[REGISTER_BYTE (XMM0_REGNUM
+ reg
)],
423 REGISTER_RAW_SIZE (XMM0_REGNUM
+ reg
));
427 /* Make a PTRACE_GETXFPREGS request, and supply all the register
428 values that yields to GDB. */
433 struct user_xfpregs_struct xfpregs
;
435 if (! have_ptrace_getxfpregs
)
438 ret
= ptrace (PTRACE_GETXFPREGS
, inferior_pid
, 0, &xfpregs
);
443 have_ptrace_getxfpregs
= 0;
447 warning ("couldn't read floating-point and SSE registers.");
451 supply_xfpregset (&xfpregs
);
456 /* Send all the valid register values in GDB's register file covered
457 by the PTRACE_SETXFPREGS request to the inferior. */
462 struct user_xfpregs_struct xfpregs
;
464 if (! have_ptrace_getxfpregs
)
467 ret
= ptrace (PTRACE_GETXFPREGS
, inferior_pid
, 0, &xfpregs
);
472 have_ptrace_getxfpregs
= 0;
476 warning ("couldn't read floating-point and SSE registers.");
480 convert_to_xfpregset (&xfpregs
, registers
, register_valid
);
482 if (ptrace (PTRACE_SETXFPREGS
, inferior_pid
, 0, &xfpregs
) < 0)
484 warning ("Couldn't write floating-point and SSE registers.");
492 /* Fill the XMM registers in the register file with dummy values. For
493 cases where we don't have access to the XMM registers. I think
494 this is cleaner than printing a warning. For a cleaner solution,
495 we should gdbarchify the i386 family. */
499 /* C doesn't have a syntax for NaN's, so write it out as an array of
501 static long dummy
[4] = { 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff };
502 static long mxcsr
= 0x1f80;
505 for (reg
= 0; reg
< 8; reg
++)
506 supply_register (XMM0_REGNUM
+ reg
, (char *) dummy
);
507 supply_register (MXCSR_REGNUM
, (char *) &mxcsr
);
512 /* Stub versions of the above routines, for systems that don't have
513 PTRACE_GETXFPREGS. */
514 static int store_xfpregs () { return 0; }
515 static int fetch_xfpregs () { return 0; }
516 static void dummy_sse_values () {}
521 /* Transferring arbitrary registers between GDB and inferior. */
523 /* Fetch registers from the child process.
524 Fetch all if regno == -1, otherwise fetch all ordinary
525 registers or all floating point registers depending
526 upon the value of regno. */
529 fetch_inferior_registers (int regno
)
531 /* Use the xfpregs requests whenever possible, since they transfer
532 more registers in one system call, and we'll cache the results.
533 But remember that fetch_xfpregs can fail, and return zero. */
537 if (fetch_xfpregs ())
543 if (GETREGS_SUPPLIES (regno
))
549 if (GETXFPREGS_SUPPLIES (regno
))
551 if (fetch_xfpregs ())
554 /* Either our processor or our kernel doesn't support the SSE
555 registers, so read the FP registers in the traditional way,
556 and fill the SSE registers with dummy values. It would be
557 more graceful to handle differences in the register set using
558 gdbarch. Until then, this will at least make things work
565 internal_error ("i386-linux-nat.c (fetch_inferior_registers): "
566 "got request for bad register number %d", regno
);
570 /* Store our register values back into the inferior.
571 If REGNO is -1, do this for all registers.
572 Otherwise, REGNO specifies which register, which
573 then determines whether we store all ordinary
574 registers or all of the floating point registers. */
577 store_inferior_registers (regno
)
580 /* Use the xfpregs requests whenever possible, since they transfer
581 more registers in one system call. But remember that
582 fetch_xfpregs can fail, and return zero. */
586 if (store_xfpregs ())
592 if (GETREGS_SUPPLIES (regno
))
598 if (GETXFPREGS_SUPPLIES (regno
))
600 if (store_xfpregs ())
603 /* Either our processor or our kernel doesn't support the SSE
604 registers, so just write the FP registers in the traditional way. */
609 internal_error ("i386-linux-nat.c (store_inferior_registers): "
610 "got request to store bad register number %d", regno
);
615 /* Interpreting register set info found in core files. */
617 /* Provide registers to GDB from a core file.
619 (We can't use the generic version of this function in
620 core-regset.c, because Linux has *three* different kinds of
621 register set notes. core-regset.c would have to call
622 supply_xfpregset, which most platforms don't have.)
624 CORE_REG_SECT points to an array of bytes, which are the contents
625 of a `note' from a core file which BFD thinks might contain
626 register contents. CORE_REG_SIZE is its size.
628 WHICH says which register set corelow suspects this is:
629 0 --- the general register set, in gregset format
630 2 --- the floating-point register set, in fpregset format
631 3 --- the extended floating-point register set, in struct
632 user_xfpregs_struct format
634 DUMMY isn't used on Linux. */
636 i386_linux_fetch_core_registers (char *core_reg_sect
,
637 unsigned core_reg_size
,
647 if (core_reg_size
!= sizeof (gregset
))
648 warning ("wrong size gregset struct in core file");
651 memcpy (&gregset
, core_reg_sect
, sizeof (gregset
));
652 supply_gregset (&gregset
);
657 if (core_reg_size
!= sizeof (fpregset
))
658 warning ("wrong size fpregset struct in core file");
661 memcpy (&fpregset
, core_reg_sect
, sizeof (fpregset
));
662 supply_fpregset (&fpregset
);
666 #ifdef HAVE_PTRACE_GETXFPREGS
668 struct user_xfpregs_struct xfpregset
;
670 if (core_reg_size
!= sizeof (struct user_xfpregs_struct
))
671 warning ("wrong size user_xfpregs_struct in core file");
674 memcpy (&xfpregset
, core_reg_sect
, sizeof (xfpregset
));
675 supply_xfpregset (&xfpregset
);
682 /* We've covered all the kinds of registers we know about here,
683 so this must be something we wouldn't know what to do with
684 anyway. Just ignore it. */
690 static struct core_fns i386_linux_nat_core_fns
=
692 bfd_target_elf_flavour
, /* core_flavour */
693 default_check_format
, /* check_format */
694 default_core_sniffer
, /* core_sniffer */
695 i386_linux_fetch_core_registers
, /* core_read_registers */
700 /* Calling functions in shared libraries. */
702 /* Find the minimal symbol named NAME, and return both the minsym
703 struct and its objfile. This probably ought to be in minsym.c, but
704 everything there is trying to deal with things like C++ and
705 SOFUN_ADDRESS_MAYBE_TURQUOISE, ... Since this is so simple, it may
706 be considered too special-purpose for general consumption. */
708 static struct minimal_symbol
*
709 find_minsym_and_objfile (char *name
, struct objfile
**objfile_p
)
711 struct objfile
*objfile
;
713 ALL_OBJFILES (objfile
)
715 struct minimal_symbol
*msym
;
717 ALL_OBJFILE_MSYMBOLS (objfile
, msym
)
719 if (SYMBOL_NAME (msym
)
720 && STREQ (SYMBOL_NAME (msym
), name
))
722 *objfile_p
= objfile
;
733 skip_hurd_resolver (CORE_ADDR pc
)
735 /* The HURD dynamic linker is part of the GNU C library, so many
736 GNU/Linux distributions use it. (All ELF versions, as far as I
737 know.) An unresolved PLT entry points to "_dl_runtime_resolve",
738 which calls "fixup" to patch the PLT, and then passes control to
741 We look for the symbol `_dl_runtime_resolve', and find `fixup' in
742 the same objfile. If we are at the entry point of `fixup', then
743 we set a breakpoint at the return address (at the top of the
744 stack), and continue.
746 It's kind of gross to do all these checks every time we're
747 called, since they don't change once the executable has gotten
748 started. But this is only a temporary hack --- upcoming versions
749 of Linux will provide a portable, efficient interface for
750 debugging programs that use shared libraries. */
752 struct objfile
*objfile
;
753 struct minimal_symbol
*resolver
754 = find_minsym_and_objfile ("_dl_runtime_resolve", &objfile
);
758 struct minimal_symbol
*fixup
759 = lookup_minimal_symbol ("fixup", 0, objfile
);
761 if (fixup
&& SYMBOL_VALUE_ADDRESS (fixup
) == pc
)
762 return (SAVED_PC_AFTER_CALL (get_current_frame ()));
769 /* See the comments for SKIP_SOLIB_RESOLVER at the top of infrun.c.
771 1) decides whether a PLT has sent us into the linker to resolve
772 a function reference, and
773 2) if so, tells us where to set a temporary breakpoint that will
774 trigger when the dynamic linker is done. */
777 i386_linux_skip_solib_resolver (CORE_ADDR pc
)
781 /* Plug in functions for other kinds of resolvers here. */
782 result
= skip_hurd_resolver (pc
);
791 /* Module initialization. */
794 _initialize_i386_linux_nat ()
796 add_core_fns (&i386_linux_nat_core_fns
);