import gdb-1999-11-01 snapshot
[deliverable/binutils-gdb.git] / gdb / i386-linux-nat.c
CommitLineData
d4f3574e
SS
1/* Native-dependent code for Linux running on i386's, for GDB.
2
3This file is part of GDB.
4
5This program is free software; you can redistribute it and/or modify
6it under the terms of the GNU General Public License as published by
7the Free Software Foundation; either version 2 of the License, or
8(at your option) any later version.
9
10This program is distributed in the hope that it will be useful,
11but WITHOUT ANY WARRANTY; without even the implied warranty of
12MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13GNU General Public License for more details.
14
15You should have received a copy of the GNU General Public License
16along with this program; if not, write to the Free Software
17Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
18
19#include "defs.h"
20#include "inferior.h"
21#include "gdbcore.h"
22
23/* For i386_linux_skip_solib_resolver */
24#include "symtab.h"
25#include "frame.h"
26#include "symfile.h"
27#include "objfiles.h"
28
29#include <sys/ptrace.h>
30#include <sys/user.h>
31#include <sys/procfs.h>
32
33#ifdef HAVE_SYS_REG_H
34#include <sys/reg.h>
35#endif
36
37/* This is a duplicate of the table in i386-xdep.c. */
38
39static int regmap[] =
40{
41 EAX, ECX, EDX, EBX,
42 UESP, EBP, ESI, EDI,
43 EIP, EFL, CS, SS,
44 DS, ES, FS, GS,
45};
46
47
5c44784c
JM
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)
56
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.
60
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. */
65int have_ptrace_getxfpregs =
66#ifdef HAVE_PTRACE_GETXFPREGS
67 1
68#else
69 0
70#endif
71;
72
73
74\f
75/* Transfering the general registers between GDB, inferiors and core files. */
76
917317f4
JM
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. */
d4f3574e
SS
80void
81supply_gregset (gregsetp)
82 gregset_t *gregsetp;
83{
84 register int regi;
85 register greg_t *regp = (greg_t *) gregsetp;
86
917317f4 87 for (regi = 0; regi < NUM_GREGS; regi++)
d4f3574e
SS
88 {
89 supply_register (regi, (char *) (regp + regmap[regi]));
90 }
91}
92
5c44784c 93
917317f4
JM
94/* Fill in a gregset_t object with selected data from a gdb-format
95 register file.
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. */
d4f3574e 102void
917317f4
JM
103convert_to_gregset (gregset_t *gregsetp,
104 char *gdb_regs,
105 signed char *valid)
d4f3574e
SS
106{
107 int regi;
108 register greg_t *regp = (greg_t *) gregsetp;
109
917317f4
JM
110 for (regi = 0; regi < NUM_GREGS; regi++)
111 if (! valid || valid[regi])
112 *(regp + regmap[regi]) = * (int *) &registers[REGISTER_BYTE (regi)];
113}
114
5c44784c
JM
115
116/* Store GDB's value for REGNO in *GREGSETP. If REGNO is -1, do all
117 of them. */
917317f4
JM
118void
119fill_gregset (gregset_t *gregsetp,
120 int regno)
121{
122 if (regno == -1)
123 convert_to_gregset (gregsetp, registers, 0);
124 else
d4f3574e 125 {
917317f4
JM
126 signed char valid[NUM_GREGS];
127 memset (valid, 0, sizeof (valid));
128 valid[regno] = 1;
129 convert_to_gregset (gregsetp, valid, valid);
d4f3574e
SS
130 }
131}
132
133
5c44784c
JM
134/* Read the general registers from the process, and store them
135 in registers[]. */
136static void
137fetch_regs ()
138{
139 int ret, regno;
140 gregset_t buf;
141
142 ret = ptrace (PTRACE_GETREGS, inferior_pid, 0, (int) &buf);
143 if (ret < 0)
144 {
145 warning ("Couldn't get registers");
146 return;
147 }
148
149 supply_gregset (&buf);
150}
151
152
153/* Set the inferior's general registers to the values in registers[]
154 --- but only those registers marked as valid. */
155static void
156store_regs ()
157{
158 int ret, regno;
159 gregset_t buf;
160
161 ret = ptrace (PTRACE_GETREGS, inferior_pid, 0, (int) &buf);
162 if (ret < 0)
163 {
164 warning ("Couldn't get registers");
165 return;
166 }
167
168 convert_to_gregset (&buf, registers, register_valid);
169
170 ret = ptrace (PTRACE_SETREGS, inferior_pid, 0, (int)buf);
171 if (ret < 0)
172 {
173 warning ("Couldn't write registers");
174 return;
175 }
176}
177
178
179\f
180/* Transfering floating-point registers between GDB, inferiors and cores. */
181
182/* What is the address of st(N) within the fpregset_t structure F? */
183#define FPREGSET_T_FPREG_ADDR(f, n) \
917317f4 184 ((char *) &(f)->st_space + (n) * 10)
d4f3574e 185
917317f4
JM
186/* Fill GDB's register file with the floating-point register values in
187 *FPREGSETP. */
d4f3574e 188void
917317f4 189supply_fpregset (fpregset_t *fpregsetp)
d4f3574e 190{
917317f4
JM
191 int i;
192
193 /* Supply the floating-point registers. */
194 for (i = 0; i < 8; i++)
5c44784c 195 supply_register (FP0_REGNUM + i, FPREGSET_T_FPREG_ADDR (fpregsetp, i));
917317f4
JM
196
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);
203
204 /* Extract the code segment and opcode from the "fcs" member. */
205 {
206 long l;
207
208 l = fpregsetp->fcs & 0xffff;
209 supply_register (FCS_REGNUM, (char *) &l);
210
211 l = (fpregsetp->fcs >> 16) & ((1 << 11) - 1);
212 supply_register (FOP_REGNUM, (char *) &l);
213 }
d4f3574e
SS
214}
215
d4f3574e 216
917317f4
JM
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. */
d4f3574e 225void
917317f4
JM
226convert_to_fpregset (fpregset_t *fpregsetp,
227 char *gdb_regs,
228 signed char *valid)
d4f3574e 229{
917317f4
JM
230 int i;
231
232 /* Fill in the floating-point registers. */
233 for (i = 0; i < 8; i++)
234 if (!valid || valid[i])
5c44784c 235 memcpy (FPREGSET_T_FPREG_ADDR (fpregsetp, i),
917317f4
JM
236 &registers[REGISTER_BYTE (FP0_REGNUM + i)],
237 REGISTER_RAW_SIZE(FP0_REGNUM + i));
238
239#define fill(MEMBER, REGNO) \
240 if (! valid || valid[(REGNO)]) \
241 memcpy (&fpregsetp->MEMBER, &registers[REGISTER_BYTE (REGNO)], \
242 sizeof (fpregsetp->MEMBER))
243
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);
250
251#undef fill
252
253 if (! valid || valid[FCS_REGNUM])
254 fpregsetp->fcs
255 = ((fpregsetp->fcs & ~0xffff)
256 | (* (int *) &registers[REGISTER_BYTE (FCS_REGNUM)] & 0xffff));
257
258 if (! valid || valid[FOP_REGNUM])
259 fpregsetp->fcs
260 = ((fpregsetp->fcs & 0xffff)
261 | ((*(int *) &registers[REGISTER_BYTE (FOP_REGNUM)] & ((1 << 11) - 1))
262 << 16));
263}
d4f3574e 264
917317f4
JM
265
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. */
269
270void
271fill_fpregset (fpregset_t *fpregsetp,
272 int regno)
273{
274 convert_to_fpregset (fpregsetp, registers, 0);
d4f3574e
SS
275}
276
917317f4
JM
277
278/* Get the whole floating point state of the process and store the
279 floating point stack into registers[]. */
d4f3574e 280static void
917317f4 281fetch_fpregs ()
d4f3574e
SS
282{
283 int ret, regno;
917317f4 284 fpregset_t buf;
d4f3574e 285
917317f4
JM
286 ret = ptrace (PTRACE_GETFPREGS, inferior_pid, 0, (int) &buf);
287 if (ret < 0)
d4f3574e
SS
288 {
289 warning ("Couldn't get floating point status");
290 return;
291 }
292
917317f4
JM
293 /* ptrace fills an fpregset_t, so we can use the same function we do
294 for core files. */
295 supply_fpregset (&buf);
d4f3574e
SS
296}
297
298
917317f4
JM
299/* Set the inferior's floating-point registers to the values in
300 registers[] --- but only those registers marked valid. */
d4f3574e 301static void
917317f4 302store_fpregs ()
d4f3574e 303{
917317f4
JM
304 int ret;
305 fpregset_t buf;
d4f3574e 306
917317f4
JM
307 ret = ptrace (PTRACE_GETFPREGS, inferior_pid, 0, (int) &buf);
308 if (ret < 0)
d4f3574e
SS
309 {
310 warning ("Couldn't get floating point status");
311 return;
312 }
313
917317f4 314 convert_to_fpregset (&buf, registers, register_valid);
d4f3574e 315
917317f4
JM
316 ret = ptrace (PTRACE_SETFPREGS, inferior_pid, 0, (int) &buf);
317 if (ret < 0)
d4f3574e
SS
318 {
319 warning ("Couldn't write floating point status");
320 return;
321 }
d4f3574e
SS
322}
323
5c44784c
JM
324\f
325/* Transfering floating-point and SSE registers to and from GDB. */
d4f3574e 326
5c44784c
JM
327
328#ifdef HAVE_PTRACE_GETXFPREGS
d4f3574e 329static void
5c44784c 330supply_xfpregset (struct user_xfpregs_struct *xfpregs)
d4f3574e 331{
5c44784c 332 int reg;
d4f3574e 333
5c44784c
JM
334 /* Supply the floating-point registers. */
335 for (reg = 0; reg < 8; reg++)
336 supply_register (FP0_REGNUM + reg, (char *) &xfpregs->st_space[reg]);
337
338 {
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);
345
346 /* Extract the code segment and opcode from the "fcs" member. */
d4f3574e 347 {
5c44784c
JM
348 long l;
349
350 l = xfpregs->fcs & 0xffff;
351 supply_register (FCS_REGNUM, (char *) &l);
352
353 l = (xfpregs->fcs >> 16) & ((1 << 11) - 1);
354 supply_register (FOP_REGNUM, (char *) &l);
d4f3574e 355 }
5c44784c 356 }
d4f3574e 357
5c44784c
JM
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);
d4f3574e
SS
362}
363
364
d4f3574e 365static void
5c44784c
JM
366convert_to_xfpregset (struct user_xfpregs_struct *xfpregs,
367 char *gdb_regs,
368 signed char *valid)
d4f3574e 369{
5c44784c 370 int reg;
d4f3574e 371
5c44784c
JM
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 &registers[REGISTER_BYTE (FP0_REGNUM + reg)],
377 REGISTER_RAW_SIZE(FP0_REGNUM + reg));
378
379#define fill(MEMBER, REGNO) \
380 if (! valid || valid[(REGNO)]) \
381 memcpy (&xfpregs->MEMBER, &registers[REGISTER_BYTE (REGNO)], \
382 sizeof (xfpregs->MEMBER))
383
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);
390
391#undef fill
392
393 if (! valid || valid[FCS_REGNUM])
394 xfpregs->fcs
395 = ((xfpregs->fcs & ~0xffff)
396 | (* (int *) &registers[REGISTER_BYTE (FCS_REGNUM)] & 0xffff));
397
398 if (! valid || valid[FOP_REGNUM])
399 xfpregs->fcs
400 = ((xfpregs->fcs & 0xffff)
401 | ((*(int *) &registers[REGISTER_BYTE (FOP_REGNUM)] & ((1 << 11) - 1))
402 << 16));
403
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 &registers[REGISTER_BYTE (XMM0_REGNUM + reg)],
409 REGISTER_RAW_SIZE (XMM0_REGNUM + reg));
410}
411
412
413/* Make a PTRACE_GETXFPREGS request, and supply all the register
414 values that yields to GDB. */
415static int
416fetch_xfpregs ()
417{
418 int ret;
419 struct user_xfpregs_struct xfpregs;
420
421 if (! have_ptrace_getxfpregs)
422 return 0;
423
424 ret = ptrace (PTRACE_GETXFPREGS, inferior_pid, 0, &xfpregs);
425 if (ret == -1)
d4f3574e 426 {
5c44784c
JM
427 if (errno == EIO)
428 {
429 have_ptrace_getxfpregs = 0;
430 return 0;
431 }
432
433 warning ("couldn't read floating-point and SSE registers.");
434 return 0;
d4f3574e
SS
435 }
436
5c44784c
JM
437 supply_xfpregset (&xfpregs);
438 return 1;
439}
d4f3574e 440
5c44784c
JM
441
442/* Send all the valid register values in GDB's register file covered
443 by the PTRACE_SETXFPREGS request to the inferior. */
444static int
445store_xfpregs ()
446{
447 int ret;
448 struct user_xfpregs_struct xfpregs;
449
450 if (! have_ptrace_getxfpregs)
451 return 0;
452
453 ret = ptrace (PTRACE_GETXFPREGS, inferior_pid, 0, &xfpregs);
454 if (ret == -1)
d4f3574e 455 {
5c44784c
JM
456 if (errno == EIO)
457 {
458 have_ptrace_getxfpregs = 0;
459 return 0;
460 }
461
462 warning ("couldn't read floating-point and SSE registers.");
463 return 0;
464 }
465
466 convert_to_xfpregset (&xfpregs, registers, register_valid);
467
468 if (ptrace (PTRACE_SETXFPREGS, inferior_pid, 0, &xfpregs) < 0)
469 {
470 warning ("Couldn't write floating-point and SSE registers.");
471 return 0;
d4f3574e 472 }
5c44784c
JM
473
474 return 1;
475}
476
477
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. */
482static void
483dummy_sse_values ()
484{
485 /* C doesn't have a syntax for NaN's, so write it out as an array of
486 longs. */
487 static long dummy[4] = { 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff };
488 static long mxcsr = 0x1f80;
489 int reg;
490
491 for (reg = 0; reg < 8; reg++)
492 supply_register (XMM0_REGNUM + reg, (char *) dummy);
493 supply_register (MXCSR_REGNUM, (char *) &mxcsr);
d4f3574e
SS
494}
495
5c44784c
JM
496#else
497
498/* Stub versions of the above routines, for systems that don't have
499 PTRACE_GETXFPREGS. */
500static int store_xfpregs () { return 0; }
501static int fetch_xfpregs () { return 0; }
502static void dummy_sse_values () {}
503
504#endif
505
506\f
507/* Transferring arbitrary registers between GDB and inferior. */
d4f3574e
SS
508
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. */
513
514void
917317f4 515fetch_inferior_registers (int regno)
d4f3574e 516{
5c44784c
JM
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. */
520 if (regno == -1)
521 {
522 fetch_regs ();
523 if (fetch_xfpregs ())
524 return;
525 fetch_fpregs ();
526 return;
527 }
d4f3574e 528
5c44784c
JM
529 if (GETREGS_SUPPLIES (regno))
530 {
531 fetch_regs ();
532 return;
533 }
534
535 if (GETXFPREGS_SUPPLIES (regno))
536 {
537 if (fetch_xfpregs ())
538 return;
539
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
545 plausibly. */
546 fetch_fpregs ();
547 dummy_sse_values ();
548 return;
549 }
550
551 internal_error ("i386-linux-nat.c (fetch_inferior_registers): "
552 "got request for bad register number %d", regno);
d4f3574e
SS
553}
554
555
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. */
561
562void
563store_inferior_registers (regno)
564 int regno;
565{
5c44784c
JM
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. */
569 if (regno == -1)
570 {
571 store_regs ();
572 if (store_xfpregs ())
573 return;
574 store_fpregs ();
575 return;
576 }
d4f3574e 577
5c44784c
JM
578 if (GETREGS_SUPPLIES (regno))
579 {
580 store_regs ();
581 return;
582 }
583
584 if (GETXFPREGS_SUPPLIES (regno))
585 {
586 if (store_xfpregs ())
587 return;
588
589 /* Either our processor or our kernel doesn't support the SSE
590 registers, so just write the FP registers in the traditional way. */
591 store_fpregs ();
592 return;
593 }
594
595 internal_error ("i386-linux-nat.c (store_inferior_registers): "
596 "got request to store bad register number %d", regno);
d4f3574e
SS
597}
598
599
5c44784c
JM
600\f
601/* Calling functions in shared libraries. */
602
d4f3574e
SS
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. */
608
609static struct minimal_symbol *
610find_minsym_and_objfile (char *name, struct objfile **objfile_p)
611{
612 struct objfile *objfile;
613
614 ALL_OBJFILES (objfile)
615 {
616 struct minimal_symbol *msym;
617
618 ALL_OBJFILE_MSYMBOLS (objfile, msym)
619 {
620 if (SYMBOL_NAME (msym)
621 && STREQ (SYMBOL_NAME (msym), name))
622 {
623 *objfile_p = objfile;
624 return msym;
625 }
626 }
627 }
628
629 return 0;
630}
631
632
633static CORE_ADDR
634skip_hurd_resolver (CORE_ADDR pc)
635{
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
640 the function.
641
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.
646
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. */
652
653 struct objfile *objfile;
654 struct minimal_symbol *resolver
655 = find_minsym_and_objfile ("_dl_runtime_resolve", &objfile);
656
657 if (resolver)
658 {
659 struct minimal_symbol *fixup
660 = lookup_minimal_symbol ("fixup", 0, objfile);
661
662 if (fixup && SYMBOL_VALUE_ADDRESS (fixup) == pc)
663 return (SAVED_PC_AFTER_CALL (get_current_frame ()));
664 }
665
666 return 0;
667}
668
669
670/* See the comments for SKIP_SOLIB_RESOLVER at the top of infrun.c.
671 This function:
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. */
676
677CORE_ADDR
678i386_linux_skip_solib_resolver (CORE_ADDR pc)
679{
680 CORE_ADDR result;
681
682 /* Plug in functions for other kinds of resolvers here. */
683 result = skip_hurd_resolver (pc);
684 if (result)
685 return result;
686
687 return 0;
688}
This page took 0.056958 seconds and 4 git commands to generate.