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 #ifdef HAVE_PTRACE_GETXFPREGS
330 supply_xfpregset (struct user_xfpregs_struct
*xfpregs
)
334 /* Supply the floating-point registers. */
335 for (reg
= 0; reg
< 8; reg
++)
336 supply_register (FP0_REGNUM
+ reg
, (char *) &xfpregs
->st_space
[reg
]);
339 supply_register (FCTRL_REGNUM
, (char *) &xfpregs
->cwd
);
340 supply_register (FSTAT_REGNUM
, (char *) &xfpregs
->swd
);
341 supply_register (FTAG_REGNUM
, (char *) &xfpregs
->twd
);
342 supply_register (FCOFF_REGNUM
, (char *) &xfpregs
->fip
);
343 supply_register (FDS_REGNUM
, (char *) &xfpregs
->fos
);
344 supply_register (FDOFF_REGNUM
, (char *) &xfpregs
->foo
);
346 /* Extract the code segment and opcode from the "fcs" member. */
350 l
= xfpregs
->fcs
& 0xffff;
351 supply_register (FCS_REGNUM
, (char *) &l
);
353 l
= (xfpregs
->fcs
>> 16) & ((1 << 11) - 1);
354 supply_register (FOP_REGNUM
, (char *) &l
);
358 /* Supply the SSE registers. */
359 for (reg
= 0; reg
< 8; reg
++)
360 supply_register (XMM0_REGNUM
+ reg
, (char *) &xfpregs
->xmm_space
[reg
]);
361 supply_register (MXCSR_REGNUM
, (char *) &xfpregs
->mxcsr
);
366 convert_to_xfpregset (struct user_xfpregs_struct
*xfpregs
,
372 /* Fill in the floating-point registers. */
373 for (reg
= 0; reg
< 8; reg
++)
374 if (!valid
|| valid
[reg
])
375 memcpy (&xfpregs
->st_space
[reg
],
376 ®isters
[REGISTER_BYTE (FP0_REGNUM
+ reg
)],
377 REGISTER_RAW_SIZE(FP0_REGNUM
+ reg
));
379 #define fill(MEMBER, REGNO) \
380 if (! valid || valid[(REGNO)]) \
381 memcpy (&xfpregs->MEMBER, ®isters[REGISTER_BYTE (REGNO)], \
382 sizeof (xfpregs->MEMBER))
384 fill (cwd
, FCTRL_REGNUM
);
385 fill (swd
, FSTAT_REGNUM
);
386 fill (twd
, FTAG_REGNUM
);
387 fill (fip
, FCOFF_REGNUM
);
388 fill (foo
, FDOFF_REGNUM
);
389 fill (fos
, FDS_REGNUM
);
393 if (! valid
|| valid
[FCS_REGNUM
])
395 = ((xfpregs
->fcs
& ~0xffff)
396 | (* (int *) ®isters
[REGISTER_BYTE (FCS_REGNUM
)] & 0xffff));
398 if (! valid
|| valid
[FOP_REGNUM
])
400 = ((xfpregs
->fcs
& 0xffff)
401 | ((*(int *) ®isters
[REGISTER_BYTE (FOP_REGNUM
)] & ((1 << 11) - 1))
404 /* Fill in the XMM registers. */
405 for (reg
= 0; reg
< 8; reg
++)
406 if (! valid
|| valid
[reg
])
407 memcpy (&xfpregs
->xmm_space
[reg
],
408 ®isters
[REGISTER_BYTE (XMM0_REGNUM
+ reg
)],
409 REGISTER_RAW_SIZE (XMM0_REGNUM
+ reg
));
413 /* Make a PTRACE_GETXFPREGS request, and supply all the register
414 values that yields to GDB. */
419 struct user_xfpregs_struct xfpregs
;
421 if (! have_ptrace_getxfpregs
)
424 ret
= ptrace (PTRACE_GETXFPREGS
, inferior_pid
, 0, &xfpregs
);
429 have_ptrace_getxfpregs
= 0;
433 warning ("couldn't read floating-point and SSE registers.");
437 supply_xfpregset (&xfpregs
);
442 /* Send all the valid register values in GDB's register file covered
443 by the PTRACE_SETXFPREGS request to the inferior. */
448 struct user_xfpregs_struct xfpregs
;
450 if (! have_ptrace_getxfpregs
)
453 ret
= ptrace (PTRACE_GETXFPREGS
, inferior_pid
, 0, &xfpregs
);
458 have_ptrace_getxfpregs
= 0;
462 warning ("couldn't read floating-point and SSE registers.");
466 convert_to_xfpregset (&xfpregs
, registers
, register_valid
);
468 if (ptrace (PTRACE_SETXFPREGS
, inferior_pid
, 0, &xfpregs
) < 0)
470 warning ("Couldn't write floating-point and SSE registers.");
478 /* Fill the XMM registers in the register file with dummy values. For
479 cases where we don't have access to the XMM registers. I think
480 this is cleaner than printing a warning. For a cleaner solution,
481 we should gdbarchify the i386 family. */
485 /* C doesn't have a syntax for NaN's, so write it out as an array of
487 static long dummy
[4] = { 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff };
488 static long mxcsr
= 0x1f80;
491 for (reg
= 0; reg
< 8; reg
++)
492 supply_register (XMM0_REGNUM
+ reg
, (char *) dummy
);
493 supply_register (MXCSR_REGNUM
, (char *) &mxcsr
);
498 /* Stub versions of the above routines, for systems that don't have
499 PTRACE_GETXFPREGS. */
500 static int store_xfpregs () { return 0; }
501 static int fetch_xfpregs () { return 0; }
502 static void dummy_sse_values () {}
507 /* Transferring arbitrary registers between GDB and inferior. */
509 /* Fetch registers from the child process.
510 Fetch all if regno == -1, otherwise fetch all ordinary
511 registers or all floating point registers depending
512 upon the value of regno. */
515 fetch_inferior_registers (int regno
)
517 /* Use the xfpregs requests whenever possible, since they transfer
518 more registers in one system call, and we'll cache the results.
519 But remember that fetch_xfpregs can fail, and return zero. */
523 if (fetch_xfpregs ())
529 if (GETREGS_SUPPLIES (regno
))
535 if (GETXFPREGS_SUPPLIES (regno
))
537 if (fetch_xfpregs ())
540 /* Either our processor or our kernel doesn't support the SSE
541 registers, so read the FP registers in the traditional way,
542 and fill the SSE registers with dummy values. It would be
543 more graceful to handle differences in the register set using
544 gdbarch. Until then, this will at least make things work
551 internal_error ("i386-linux-nat.c (fetch_inferior_registers): "
552 "got request for bad register number %d", regno
);
556 /* Store our register values back into the inferior.
557 If REGNO is -1, do this for all registers.
558 Otherwise, REGNO specifies which register, which
559 then determines whether we store all ordinary
560 registers or all of the floating point registers. */
563 store_inferior_registers (regno
)
566 /* Use the xfpregs requests whenever possible, since they transfer
567 more registers in one system call. But remember that
568 fetch_xfpregs can fail, and return zero. */
572 if (store_xfpregs ())
578 if (GETREGS_SUPPLIES (regno
))
584 if (GETXFPREGS_SUPPLIES (regno
))
586 if (store_xfpregs ())
589 /* Either our processor or our kernel doesn't support the SSE
590 registers, so just write the FP registers in the traditional way. */
595 internal_error ("i386-linux-nat.c (store_inferior_registers): "
596 "got request to store bad register number %d", regno
);
601 /* Calling functions in shared libraries. */
603 /* Find the minimal symbol named NAME, and return both the minsym
604 struct and its objfile. This probably ought to be in minsym.c, but
605 everything there is trying to deal with things like C++ and
606 SOFUN_ADDRESS_MAYBE_TURQUOISE, ... Since this is so simple, it may
607 be considered too special-purpose for general consumption. */
609 static struct minimal_symbol
*
610 find_minsym_and_objfile (char *name
, struct objfile
**objfile_p
)
612 struct objfile
*objfile
;
614 ALL_OBJFILES (objfile
)
616 struct minimal_symbol
*msym
;
618 ALL_OBJFILE_MSYMBOLS (objfile
, msym
)
620 if (SYMBOL_NAME (msym
)
621 && STREQ (SYMBOL_NAME (msym
), name
))
623 *objfile_p
= objfile
;
634 skip_hurd_resolver (CORE_ADDR pc
)
636 /* The HURD dynamic linker is part of the GNU C library, so many
637 GNU/Linux distributions use it. (All ELF versions, as far as I
638 know.) An unresolved PLT entry points to "_dl_runtime_resolve",
639 which calls "fixup" to patch the PLT, and then passes control to
642 We look for the symbol `_dl_runtime_resolve', and find `fixup' in
643 the same objfile. If we are at the entry point of `fixup', then
644 we set a breakpoint at the return address (at the top of the
645 stack), and continue.
647 It's kind of gross to do all these checks every time we're
648 called, since they don't change once the executable has gotten
649 started. But this is only a temporary hack --- upcoming versions
650 of Linux will provide a portable, efficient interface for
651 debugging programs that use shared libraries. */
653 struct objfile
*objfile
;
654 struct minimal_symbol
*resolver
655 = find_minsym_and_objfile ("_dl_runtime_resolve", &objfile
);
659 struct minimal_symbol
*fixup
660 = lookup_minimal_symbol ("fixup", 0, objfile
);
662 if (fixup
&& SYMBOL_VALUE_ADDRESS (fixup
) == pc
)
663 return (SAVED_PC_AFTER_CALL (get_current_frame ()));
670 /* See the comments for SKIP_SOLIB_RESOLVER at the top of infrun.c.
672 1) decides whether a PLT has sent us into the linker to resolve
673 a function reference, and
674 2) if so, tells us where to set a temporary breakpoint that will
675 trigger when the dynamic linker is done. */
678 i386_linux_skip_solib_resolver (CORE_ADDR pc
)
682 /* Plug in functions for other kinds of resolvers here. */
683 result
= skip_hurd_resolver (pc
);