2000-05-26 Michael Snyder <msnyder@seadog.cygnus.com>
[deliverable/binutils-gdb.git] / gdb / i386-linux-nat.c
1 /* Native-dependent code for Linux running on i386's, for GDB.
2 Copyright (C) 1999, 2000 Free Software Foundation, Inc.
3
4 This file is part of GDB.
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
20
21 #include "defs.h"
22 #include "inferior.h"
23 #include "gdbcore.h"
24
25 /* For i386_linux_skip_solib_resolver. */
26 #include "symtab.h"
27 #include "symfile.h"
28 #include "objfiles.h"
29
30 #include <sys/ptrace.h>
31 #include <sys/user.h>
32 #include <sys/procfs.h>
33
34 #ifdef HAVE_SYS_REG_H
35 #include <sys/reg.h>
36 #endif
37
38 /* Prototypes for supply_gregset etc. */
39 #include "gregset.h"
40
41 /* On Linux, threads are implemented as pseudo-processes, in which
42 case we may be tracing more than one process at a time. In that
43 case, inferior_pid will contain the main process ID and the
44 individual thread (process) ID mashed together. These macros are
45 used to separate them out. These definitions should be overridden
46 if thread support is included. */
47
48 #if !defined (PIDGET) /* Default definition for PIDGET/TIDGET. */
49 #define PIDGET(PID) PID
50 #define TIDGET(PID) 0
51 #endif
52
53
54 /* The register sets used in Linux ELF core-dumps are identical to the
55 register sets in `struct user' that is used for a.out core-dumps,
56 and is also used by `ptrace'. The corresponding types are
57 `elf_gregset_t' for the general-purpose registers (with
58 `elf_greg_t' the type of a single GP register) and `elf_fpregset_t'
59 for the floating-point registers.
60
61 Those types used to be available under the names `gregset_t' and
62 `fpregset_t' too, and this file used those names in the past. But
63 those names are now used for the register sets used in the
64 `mcontext_t' type, and have a different size and layout. */
65
66 /* Mapping between the general-purpose registers in `struct user'
67 format and GDB's register array layout. */
68 static int regmap[] =
69 {
70 EAX, ECX, EDX, EBX,
71 UESP, EBP, ESI, EDI,
72 EIP, EFL, CS, SS,
73 DS, ES, FS, GS
74 };
75
76 /* Which ptrace request retrieves which registers?
77 These apply to the corresponding SET requests as well. */
78 #define GETREGS_SUPPLIES(regno) \
79 (0 <= (regno) && (regno) <= 15)
80 #define GETFPREGS_SUPPLIES(regno) \
81 (FP0_REGNUM <= (regno) && (regno) <= LAST_FPU_CTRL_REGNUM)
82 #define GETXFPREGS_SUPPLIES(regno) \
83 (FP0_REGNUM <= (regno) && (regno) <= MXCSR_REGNUM)
84
85 /* Does the current host support the GETREGS request? */
86 int have_ptrace_getregs =
87 #ifdef HAVE_PTRACE_GETREGS
88 1
89 #else
90 0
91 #endif
92 ;
93
94 /* Does the current host support the GETXFPREGS request? The header
95 file may or may not define it, and even if it is defined, the
96 kernel will return EIO if it's running on a pre-SSE processor.
97
98 PTRACE_GETXFPREGS is a Cygnus invention, since we wrote our own
99 Linux kernel patch for SSE support. That patch may or may not
100 actually make it into the official distribution. If you find that
101 years have gone by since this stuff was added, and Linux isn't
102 using PTRACE_GETXFPREGS, that means that our patch didn't make it,
103 and you can delete this, and the related code.
104
105 My instinct is to attach this to some architecture- or
106 target-specific data structure, but really, a particular GDB
107 process can only run on top of one kernel at a time. So it's okay
108 for this to be a simple variable. */
109 int have_ptrace_getxfpregs =
110 #ifdef HAVE_PTRACE_GETXFPREGS
111 1
112 #else
113 0
114 #endif
115 ;
116
117 \f
118 /* Fetching registers directly from the U area, one at a time. */
119
120 /* FIXME: kettenis/2000-03-05: This duplicates code from `inptrace.c'.
121 The problem is that we define FETCH_INFERIOR_REGISTERS since we
122 want to use our own versions of {fetch,store}_inferior_registers
123 that use the GETREGS request. This means that the code in
124 `infptrace.c' is #ifdef'd out. But we need to fall back on that
125 code when GDB is running on top of a kernel that doesn't support
126 the GETREGS request. I want to avoid changing `infptrace.c' right
127 now. */
128
129 #ifndef PT_READ_U
130 #define PT_READ_U PTRACE_PEEKUSR
131 #endif
132 #ifndef PT_WRITE_U
133 #define PT_WRITE_U PTRACE_POKEUSR
134 #endif
135
136 /* Default the type of the ptrace transfer to int. */
137 #ifndef PTRACE_XFER_TYPE
138 #define PTRACE_XFER_TYPE int
139 #endif
140
141 /* Registers we shouldn't try to fetch. */
142 #if !defined (CANNOT_FETCH_REGISTER)
143 #define CANNOT_FETCH_REGISTER(regno) 0
144 #endif
145
146 /* Fetch one register. */
147
148 static void
149 fetch_register (regno)
150 int regno;
151 {
152 /* This isn't really an address. But ptrace thinks of it as one. */
153 CORE_ADDR regaddr;
154 char mess[128]; /* For messages */
155 register int i;
156 unsigned int offset; /* Offset of registers within the u area. */
157 char buf[MAX_REGISTER_RAW_SIZE];
158 int tid;
159
160 if (CANNOT_FETCH_REGISTER (regno))
161 {
162 memset (buf, '\0', REGISTER_RAW_SIZE (regno)); /* Supply zeroes */
163 supply_register (regno, buf);
164 return;
165 }
166
167 /* Overload thread id onto process id */
168 if ((tid = TIDGET (inferior_pid)) == 0)
169 tid = inferior_pid; /* no thread id, just use process id */
170
171 offset = U_REGS_OFFSET;
172
173 regaddr = register_addr (regno, offset);
174 for (i = 0; i < REGISTER_RAW_SIZE (regno); i += sizeof (PTRACE_XFER_TYPE))
175 {
176 errno = 0;
177 *(PTRACE_XFER_TYPE *) & buf[i] = ptrace (PT_READ_U, tid,
178 (PTRACE_ARG3_TYPE) regaddr, 0);
179 regaddr += sizeof (PTRACE_XFER_TYPE);
180 if (errno != 0)
181 {
182 sprintf (mess, "reading register %s (#%d)",
183 REGISTER_NAME (regno), regno);
184 perror_with_name (mess);
185 }
186 }
187 supply_register (regno, buf);
188 }
189
190 /* Fetch register values from the inferior.
191 If REGNO is negative, do this for all registers.
192 Otherwise, REGNO specifies which register (so we can save time). */
193
194 void
195 old_fetch_inferior_registers (regno)
196 int regno;
197 {
198 if (regno >= 0)
199 {
200 fetch_register (regno);
201 }
202 else
203 {
204 for (regno = 0; regno < ARCH_NUM_REGS; regno++)
205 {
206 fetch_register (regno);
207 }
208 }
209 }
210
211 /* Registers we shouldn't try to store. */
212 #if !defined (CANNOT_STORE_REGISTER)
213 #define CANNOT_STORE_REGISTER(regno) 0
214 #endif
215
216 /* Store one register. */
217
218 static void
219 store_register (regno)
220 int regno;
221 {
222 /* This isn't really an address. But ptrace thinks of it as one. */
223 CORE_ADDR regaddr;
224 char mess[128]; /* For messages */
225 register int i;
226 unsigned int offset; /* Offset of registers within the u area. */
227 int tid;
228
229 if (CANNOT_STORE_REGISTER (regno))
230 {
231 return;
232 }
233
234 /* Overload thread id onto process id */
235 if ((tid = TIDGET (inferior_pid)) == 0)
236 tid = inferior_pid; /* no thread id, just use process id */
237
238 offset = U_REGS_OFFSET;
239
240 regaddr = register_addr (regno, offset);
241 for (i = 0; i < REGISTER_RAW_SIZE (regno); i += sizeof (PTRACE_XFER_TYPE))
242 {
243 errno = 0;
244 ptrace (PT_WRITE_U, tid, (PTRACE_ARG3_TYPE) regaddr,
245 *(PTRACE_XFER_TYPE *) & registers[REGISTER_BYTE (regno) + i]);
246 regaddr += sizeof (PTRACE_XFER_TYPE);
247 if (errno != 0)
248 {
249 sprintf (mess, "writing register %s (#%d)",
250 REGISTER_NAME (regno), regno);
251 perror_with_name (mess);
252 }
253 }
254 }
255
256 /* Store our register values back into the inferior.
257 If REGNO is negative, do this for all registers.
258 Otherwise, REGNO specifies which register (so we can save time). */
259
260 void
261 old_store_inferior_registers (regno)
262 int regno;
263 {
264 if (regno >= 0)
265 {
266 store_register (regno);
267 }
268 else
269 {
270 for (regno = 0; regno < ARCH_NUM_REGS; regno++)
271 {
272 store_register (regno);
273 }
274 }
275 }
276
277 \f
278 /* Transfering the general-purpose registers between GDB, inferiors
279 and core files. */
280
281 /* Fill GDB's register array with the genereal-purpose register values
282 in *GREGSETP. */
283
284 void
285 supply_gregset (elf_gregset_t *gregsetp)
286 {
287 elf_greg_t *regp = (elf_greg_t *) gregsetp;
288 int regi;
289
290 for (regi = 0; regi < NUM_GREGS; regi++)
291 supply_register (regi, (char *) (regp + regmap[regi]));
292 }
293
294 /* Convert the valid general-purpose register values in GDB's register
295 array to `struct user' format and store them in *GREGSETP. The
296 array VALID indicates which register values are valid. If VALID is
297 NULL, all registers are assumed to be valid. */
298
299 static void
300 convert_to_gregset (elf_gregset_t *gregsetp, signed char *valid)
301 {
302 elf_greg_t *regp = (elf_greg_t *) gregsetp;
303 int regi;
304
305 for (regi = 0; regi < NUM_GREGS; regi++)
306 if (! valid || valid[regi])
307 *(regp + regmap[regi]) = * (int *) &registers[REGISTER_BYTE (regi)];
308 }
309
310 /* Fill register REGNO (if it is a general-purpose register) in
311 *GREGSETPS with the value in GDB's register array. If REGNO is -1,
312 do this for all registers. */
313 void
314 fill_gregset (elf_gregset_t *gregsetp, int regno)
315 {
316 if (regno == -1)
317 {
318 convert_to_gregset (gregsetp, NULL);
319 return;
320 }
321
322 if (GETREGS_SUPPLIES (regno))
323 {
324 signed char valid[NUM_GREGS];
325
326 memset (valid, 0, sizeof (valid));
327 valid[regno] = 1;
328
329 convert_to_gregset (gregsetp, valid);
330 }
331 }
332
333 #ifdef HAVE_PTRACE_GETREGS
334
335 /* Fetch all general-purpose registers from process/thread TID and
336 store their values in GDB's register array. */
337
338 static void
339 fetch_regs (int tid)
340 {
341 elf_gregset_t regs;
342 int ret;
343
344 ret = ptrace (PTRACE_GETREGS, tid, 0, (int) &regs);
345 if (ret < 0)
346 {
347 if (errno == EIO)
348 {
349 /* The kernel we're running on doesn't support the GETREGS
350 request. Reset `have_ptrace_getregs'. */
351 have_ptrace_getregs = 0;
352 return;
353 }
354
355 warning ("Couldn't get registers.");
356 return;
357 }
358
359 supply_gregset (&regs);
360 }
361
362 /* Store all valid general-purpose registers in GDB's register array
363 into the process/thread specified by TID. */
364
365 static void
366 store_regs (int tid)
367 {
368 elf_gregset_t regs;
369 int ret;
370
371 ret = ptrace (PTRACE_GETREGS, tid, 0, (int) &regs);
372 if (ret < 0)
373 {
374 warning ("Couldn't get registers.");
375 return;
376 }
377
378 convert_to_gregset (&regs, register_valid);
379
380 ret = ptrace (PTRACE_SETREGS, tid, 0, (int) &regs);
381 if (ret < 0)
382 {
383 warning ("Couldn't write registers.");
384 return;
385 }
386 }
387
388 #else
389
390 static void fetch_regs (int tid) {}
391 static void store_regs (int tid) {}
392
393 #endif
394
395 \f
396 /* Transfering floating-point registers between GDB, inferiors and cores. */
397
398 /* What is the address of st(N) within the floating-point register set F? */
399 #define FPREG_ADDR(f, n) ((char *) &(f)->st_space + (n) * 10)
400
401 /* Fill GDB's register array with the floating-point register values in
402 *FPREGSETP. */
403
404 void
405 supply_fpregset (elf_fpregset_t *fpregsetp)
406 {
407 int reg;
408 long l;
409
410 /* Supply the floating-point registers. */
411 for (reg = 0; reg < 8; reg++)
412 supply_register (FP0_REGNUM + reg, FPREG_ADDR (fpregsetp, reg));
413
414 /* We have to mask off the reserved bits in *FPREGSETP before
415 storing the values in GDB's register file. */
416 #define supply(REGNO, MEMBER) \
417 l = fpregsetp->MEMBER & 0xffff; \
418 supply_register (REGNO, (char *) &l)
419
420 supply (FCTRL_REGNUM, cwd);
421 supply (FSTAT_REGNUM, swd);
422 supply (FTAG_REGNUM, twd);
423 supply_register (FCOFF_REGNUM, (char *) &fpregsetp->fip);
424 supply (FDS_REGNUM, fos);
425 supply_register (FDOFF_REGNUM, (char *) &fpregsetp->foo);
426
427 #undef supply
428
429 /* Extract the code segment and opcode from the "fcs" member. */
430 l = fpregsetp->fcs & 0xffff;
431 supply_register (FCS_REGNUM, (char *) &l);
432
433 l = (fpregsetp->fcs >> 16) & ((1 << 11) - 1);
434 supply_register (FOP_REGNUM, (char *) &l);
435 }
436
437 /* Convert the valid floating-point register values in GDB's register
438 array to `struct user' format and store them in *FPREGSETP. The
439 array VALID indicates which register values are valid. If VALID is
440 NULL, all registers are assumed to be valid. */
441
442 static void
443 convert_to_fpregset (elf_fpregset_t *fpregsetp, signed char *valid)
444 {
445 int reg;
446
447 /* Fill in the floating-point registers. */
448 for (reg = 0; reg < 8; reg++)
449 if (!valid || valid[reg])
450 memcpy (FPREG_ADDR (fpregsetp, reg),
451 &registers[REGISTER_BYTE (FP0_REGNUM + reg)],
452 REGISTER_RAW_SIZE(FP0_REGNUM + reg));
453
454 /* We're not supposed to touch the reserved bits in *FPREGSETP. */
455
456 #define fill(MEMBER, REGNO) \
457 if (! valid || valid[(REGNO)]) \
458 fpregsetp->MEMBER \
459 = ((fpregsetp->MEMBER & ~0xffff) \
460 | (* (int *) &registers[REGISTER_BYTE (REGNO)] & 0xffff))
461
462 #define fill_register(MEMBER, REGNO) \
463 if (! valid || valid[(REGNO)]) \
464 memcpy (&fpregsetp->MEMBER, &registers[REGISTER_BYTE (REGNO)], \
465 sizeof (fpregsetp->MEMBER))
466
467 fill (cwd, FCTRL_REGNUM);
468 fill (swd, FSTAT_REGNUM);
469 fill (twd, FTAG_REGNUM);
470 fill_register (fip, FCOFF_REGNUM);
471 fill (foo, FDOFF_REGNUM);
472 fill_register (fos, FDS_REGNUM);
473
474 #undef fill
475 #undef fill_register
476
477 if (! valid || valid[FCS_REGNUM])
478 fpregsetp->fcs
479 = ((fpregsetp->fcs & ~0xffff)
480 | (* (int *) &registers[REGISTER_BYTE (FCS_REGNUM)] & 0xffff));
481
482 if (! valid || valid[FOP_REGNUM])
483 fpregsetp->fcs
484 = ((fpregsetp->fcs & 0xffff)
485 | ((*(int *) &registers[REGISTER_BYTE (FOP_REGNUM)] & ((1 << 11) - 1))
486 << 16));
487 }
488
489 /* Fill register REGNO (if it is a floating-point register) in
490 *FPREGSETP with the value in GDB's register array. If REGNO is -1,
491 do this for all registers. */
492
493 void
494 fill_fpregset (elf_fpregset_t *fpregsetp, int regno)
495 {
496 if (regno == -1)
497 {
498 convert_to_fpregset (fpregsetp, NULL);
499 return;
500 }
501
502 if (GETFPREGS_SUPPLIES(regno))
503 {
504 signed char valid[MAX_NUM_REGS];
505
506 memset (valid, 0, sizeof (valid));
507 valid[regno] = 1;
508
509 convert_to_fpregset (fpregsetp, valid);
510 }
511 }
512
513 #ifdef HAVE_PTRACE_GETREGS
514
515 /* Fetch all floating-point registers from process/thread TID and store
516 thier values in GDB's register array. */
517
518 static void
519 fetch_fpregs (int tid)
520 {
521 elf_fpregset_t fpregs;
522 int ret;
523
524 ret = ptrace (PTRACE_GETFPREGS, tid, 0, (int) &fpregs);
525 if (ret < 0)
526 {
527 warning ("Couldn't get floating point status.");
528 return;
529 }
530
531 supply_fpregset (&fpregs);
532 }
533
534 /* Store all valid floating-point registers in GDB's register array
535 into the process/thread specified by TID. */
536
537 static void
538 store_fpregs (int tid)
539 {
540 elf_fpregset_t fpregs;
541 int ret;
542
543 ret = ptrace (PTRACE_GETFPREGS, tid, 0, (int) &fpregs);
544 if (ret < 0)
545 {
546 warning ("Couldn't get floating point status.");
547 return;
548 }
549
550 convert_to_fpregset (&fpregs, register_valid);
551
552 ret = ptrace (PTRACE_SETFPREGS, tid, 0, (int) &fpregs);
553 if (ret < 0)
554 {
555 warning ("Couldn't write floating point status.");
556 return;
557 }
558 }
559
560 #else
561
562 static void fetch_fpregs (int tid) {}
563 static void store_fpregs (int tid) {}
564
565 #endif
566
567 \f
568 /* Transfering floating-point and SSE registers to and from GDB. */
569
570 /* PTRACE_GETXFPREGS is a Cygnus invention, since we wrote our own
571 Linux kernel patch for SSE support. That patch may or may not
572 actually make it into the official distribution. If you find that
573 years have gone by since this code was added, and Linux isn't using
574 PTRACE_GETXFPREGS, that means that our patch didn't make it, and
575 you can delete this code. */
576
577 #ifdef HAVE_PTRACE_GETXFPREGS
578
579 /* Fill GDB's register array with the floating-point and SSE register
580 values in *XFPREGS. */
581
582 static void
583 supply_xfpregset (struct user_xfpregs_struct *xfpregs)
584 {
585 int reg;
586
587 /* Supply the floating-point registers. */
588 for (reg = 0; reg < 8; reg++)
589 supply_register (FP0_REGNUM + reg, (char *) &xfpregs->st_space[reg]);
590
591 {
592 supply_register (FCTRL_REGNUM, (char *) &xfpregs->cwd);
593 supply_register (FSTAT_REGNUM, (char *) &xfpregs->swd);
594 supply_register (FTAG_REGNUM, (char *) &xfpregs->twd);
595 supply_register (FCOFF_REGNUM, (char *) &xfpregs->fip);
596 supply_register (FDS_REGNUM, (char *) &xfpregs->fos);
597 supply_register (FDOFF_REGNUM, (char *) &xfpregs->foo);
598
599 /* Extract the code segment and opcode from the "fcs" member. */
600 {
601 long l;
602
603 l = xfpregs->fcs & 0xffff;
604 supply_register (FCS_REGNUM, (char *) &l);
605
606 l = (xfpregs->fcs >> 16) & ((1 << 11) - 1);
607 supply_register (FOP_REGNUM, (char *) &l);
608 }
609 }
610
611 /* Supply the SSE registers. */
612 for (reg = 0; reg < 8; reg++)
613 supply_register (XMM0_REGNUM + reg, (char *) &xfpregs->xmm_space[reg]);
614 supply_register (MXCSR_REGNUM, (char *) &xfpregs->mxcsr);
615 }
616
617 /* Convert the valid floating-point and SSE registers in GDB's
618 register array to `struct user' format and store them in *XFPREGS.
619 The array VALID indicates which registers are valid. If VALID is
620 NULL, all registers are assumed to be valid. */
621
622 static void
623 convert_to_xfpregset (struct user_xfpregs_struct *xfpregs,
624 signed char *valid)
625 {
626 int reg;
627
628 /* Fill in the floating-point registers. */
629 for (reg = 0; reg < 8; reg++)
630 if (!valid || valid[reg])
631 memcpy (&xfpregs->st_space[reg],
632 &registers[REGISTER_BYTE (FP0_REGNUM + reg)],
633 REGISTER_RAW_SIZE(FP0_REGNUM + reg));
634
635 #define fill(MEMBER, REGNO) \
636 if (! valid || valid[(REGNO)]) \
637 memcpy (&xfpregs->MEMBER, &registers[REGISTER_BYTE (REGNO)], \
638 sizeof (xfpregs->MEMBER))
639
640 fill (cwd, FCTRL_REGNUM);
641 fill (swd, FSTAT_REGNUM);
642 fill (twd, FTAG_REGNUM);
643 fill (fip, FCOFF_REGNUM);
644 fill (foo, FDOFF_REGNUM);
645 fill (fos, FDS_REGNUM);
646
647 #undef fill
648
649 if (! valid || valid[FCS_REGNUM])
650 xfpregs->fcs
651 = ((xfpregs->fcs & ~0xffff)
652 | (* (int *) &registers[REGISTER_BYTE (FCS_REGNUM)] & 0xffff));
653
654 if (! valid || valid[FOP_REGNUM])
655 xfpregs->fcs
656 = ((xfpregs->fcs & 0xffff)
657 | ((*(int *) &registers[REGISTER_BYTE (FOP_REGNUM)] & ((1 << 11) - 1))
658 << 16));
659
660 /* Fill in the XMM registers. */
661 for (reg = 0; reg < 8; reg++)
662 if (! valid || valid[reg])
663 memcpy (&xfpregs->xmm_space[reg],
664 &registers[REGISTER_BYTE (XMM0_REGNUM + reg)],
665 REGISTER_RAW_SIZE (XMM0_REGNUM + reg));
666 }
667
668 /* Fetch all registers covered by the PTRACE_SETXFPREGS request from
669 process/thread TID and store their values in GDB's register array.
670 Return non-zero if successful, zero otherwise. */
671
672 static int
673 fetch_xfpregs (int tid)
674 {
675 struct user_xfpregs_struct xfpregs;
676 int ret;
677
678 if (! have_ptrace_getxfpregs)
679 return 0;
680
681 ret = ptrace (PTRACE_GETXFPREGS, tid, 0, &xfpregs);
682 if (ret == -1)
683 {
684 if (errno == EIO)
685 {
686 have_ptrace_getxfpregs = 0;
687 return 0;
688 }
689
690 warning ("Couldn't read floating-point and SSE registers.");
691 return 0;
692 }
693
694 supply_xfpregset (&xfpregs);
695 return 1;
696 }
697
698 /* Store all valid registers in GDB's register array covered by the
699 PTRACE_SETXFPREGS request into the process/thread specified by TID.
700 Return non-zero if successful, zero otherwise. */
701
702 static int
703 store_xfpregs (int tid)
704 {
705 struct user_xfpregs_struct xfpregs;
706 int ret;
707
708 if (! have_ptrace_getxfpregs)
709 return 0;
710
711 ret = ptrace (PTRACE_GETXFPREGS, tid, 0, &xfpregs);
712 if (ret == -1)
713 {
714 if (errno == EIO)
715 {
716 have_ptrace_getxfpregs = 0;
717 return 0;
718 }
719
720 warning ("Couldn't read floating-point and SSE registers.");
721 return 0;
722 }
723
724 convert_to_xfpregset (&xfpregs, register_valid);
725
726 if (ptrace (PTRACE_SETXFPREGS, tid, 0, &xfpregs) < 0)
727 {
728 warning ("Couldn't write floating-point and SSE registers.");
729 return 0;
730 }
731
732 return 1;
733 }
734
735 /* Fill the XMM registers in the register array with dummy values. For
736 cases where we don't have access to the XMM registers. I think
737 this is cleaner than printing a warning. For a cleaner solution,
738 we should gdbarchify the i386 family. */
739
740 static void
741 dummy_sse_values (void)
742 {
743 /* C doesn't have a syntax for NaN's, so write it out as an array of
744 longs. */
745 static long dummy[4] = { 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff };
746 static long mxcsr = 0x1f80;
747 int reg;
748
749 for (reg = 0; reg < 8; reg++)
750 supply_register (XMM0_REGNUM + reg, (char *) dummy);
751 supply_register (MXCSR_REGNUM, (char *) &mxcsr);
752 }
753
754 #else
755
756 /* Stub versions of the above routines, for systems that don't have
757 PTRACE_GETXFPREGS. */
758 static int store_xfpregs (int tid) { return 0; }
759 static int fetch_xfpregs (int tid) { return 0; }
760 static void dummy_sse_values (void) {}
761
762 #endif
763
764 \f
765 /* Transferring arbitrary registers between GDB and inferior. */
766
767 /* Fetch register REGNO from the child process. If REGNO is -1, do
768 this for all registers (including the floating point and SSE
769 registers). */
770
771 void
772 fetch_inferior_registers (int regno)
773 {
774 int tid;
775
776 /* Use the old method of peeking around in `struct user' if the
777 GETREGS request isn't available. */
778 if (! have_ptrace_getregs)
779 {
780 old_fetch_inferior_registers (regno);
781 return;
782 }
783
784 /* Linux LWP ID's are process ID's. */
785 if ((tid = TIDGET (inferior_pid)) == 0)
786 tid = inferior_pid; /* Not a threaded program. */
787
788 /* Use the PTRACE_GETXFPREGS request whenever possible, since it
789 transfers more registers in one system call, and we'll cache the
790 results. But remember that fetch_xfpregs can fail, and return
791 zero. */
792 if (regno == -1)
793 {
794 fetch_regs (tid);
795
796 /* The call above might reset `have_ptrace_getregs'. */
797 if (! have_ptrace_getregs)
798 {
799 old_fetch_inferior_registers (-1);
800 return;
801 }
802
803 if (fetch_xfpregs (tid))
804 return;
805 fetch_fpregs (tid);
806 return;
807 }
808
809 if (GETREGS_SUPPLIES (regno))
810 {
811 fetch_regs (tid);
812 return;
813 }
814
815 if (GETXFPREGS_SUPPLIES (regno))
816 {
817 if (fetch_xfpregs (tid))
818 return;
819
820 /* Either our processor or our kernel doesn't support the SSE
821 registers, so read the FP registers in the traditional way,
822 and fill the SSE registers with dummy values. It would be
823 more graceful to handle differences in the register set using
824 gdbarch. Until then, this will at least make things work
825 plausibly. */
826 fetch_fpregs (tid);
827 dummy_sse_values ();
828 return;
829 }
830
831 internal_error ("i386-linux-nat.c (fetch_inferior_registers): "
832 "got request for bad register number %d", regno);
833 }
834
835 /* Store register REGNO back into the child process. If REGNO is -1,
836 do this for all registers (including the floating point and SSE
837 registers). */
838 void
839 store_inferior_registers (int regno)
840 {
841 int tid;
842
843 /* Use the old method of poking around in `struct user' if the
844 SETREGS request isn't available. */
845 if (! have_ptrace_getregs)
846 {
847 old_store_inferior_registers (regno);
848 return;
849 }
850
851 /* Linux LWP ID's are process ID's. */
852 if ((tid = TIDGET (inferior_pid)) == 0)
853 tid = inferior_pid; /* Not a threaded program. */
854
855 /* Use the PTRACE_SETXFPREGS requests whenever possibl, since it
856 transfers more registers in one system call. But remember that
857 store_xfpregs can fail, and return zero. */
858 if (regno == -1)
859 {
860 store_regs (tid);
861 if (store_xfpregs (tid))
862 return;
863 store_fpregs (tid);
864 return;
865 }
866
867 if (GETREGS_SUPPLIES (regno))
868 {
869 store_regs (tid);
870 return;
871 }
872
873 if (GETXFPREGS_SUPPLIES (regno))
874 {
875 if (store_xfpregs (tid))
876 return;
877
878 /* Either our processor or our kernel doesn't support the SSE
879 registers, so just write the FP registers in the traditional
880 way. */
881 store_fpregs (tid);
882 return;
883 }
884
885 internal_error ("Got request to store bad register number %d.", regno);
886 }
887
888 \f
889 /* Interpreting register set info found in core files. */
890
891 /* Provide registers to GDB from a core file.
892
893 (We can't use the generic version of this function in
894 core-regset.c, because Linux has *three* different kinds of
895 register set notes. core-regset.c would have to call
896 supply_xfpregset, which most platforms don't have.)
897
898 CORE_REG_SECT points to an array of bytes, which are the contents
899 of a `note' from a core file which BFD thinks might contain
900 register contents. CORE_REG_SIZE is its size.
901
902 WHICH says which register set corelow suspects this is:
903 0 --- the general-purpose register set, in elf_gregset_t format
904 2 --- the floating-point register set, in elf_fpregset_t format
905 3 --- the extended floating-point register set, in struct
906 user_xfpregs_struct format
907
908 REG_ADDR isn't used on Linux. */
909
910 static void
911 fetch_core_registers (char *core_reg_sect, unsigned core_reg_size,
912 int which, CORE_ADDR reg_addr)
913 {
914 elf_gregset_t gregset;
915 elf_fpregset_t fpregset;
916
917 switch (which)
918 {
919 case 0:
920 if (core_reg_size != sizeof (gregset))
921 warning ("Wrong size gregset in core file.");
922 else
923 {
924 memcpy (&gregset, core_reg_sect, sizeof (gregset));
925 supply_gregset (&gregset);
926 }
927 break;
928
929 case 2:
930 if (core_reg_size != sizeof (fpregset))
931 warning ("Wrong size fpregset in core file.");
932 else
933 {
934 memcpy (&fpregset, core_reg_sect, sizeof (fpregset));
935 supply_fpregset (&fpregset);
936 }
937 break;
938
939 #ifdef HAVE_PTRACE_GETXFPREGS
940 {
941 struct user_xfpregs_struct xfpregset;
942
943 case 3:
944 if (core_reg_size != sizeof (xfpregset))
945 warning ("Wrong size user_xfpregs_struct in core file.");
946 else
947 {
948 memcpy (&xfpregset, core_reg_sect, sizeof (xfpregset));
949 supply_xfpregset (&xfpregset);
950 }
951 break;
952 }
953 #endif
954
955 default:
956 /* We've covered all the kinds of registers we know about here,
957 so this must be something we wouldn't know what to do with
958 anyway. Just ignore it. */
959 break;
960 }
961 }
962
963 \f
964 /* The instruction for a Linux system call is:
965 int $0x80
966 or 0xcd 0x80. */
967
968 static const unsigned char linux_syscall[] = { 0xcd, 0x80 };
969
970 #define LINUX_SYSCALL_LEN (sizeof linux_syscall)
971
972 /* The system call number is stored in the %eax register. */
973 #define LINUX_SYSCALL_REGNUM 0 /* %eax */
974
975 /* We are specifically interested in the sigreturn and rt_sigreturn
976 system calls. */
977
978 #ifndef SYS_sigreturn
979 #define SYS_sigreturn 0x77
980 #endif
981 #ifndef SYS_rt_sigreturn
982 #define SYS_rt_sigreturn 0xad
983 #endif
984
985 /* Offset to saved processor flags, from <asm/sigcontext.h>. */
986 #define LINUX_SIGCONTEXT_EFLAGS_OFFSET (64)
987
988 /* Resume execution of the inferior process.
989 If STEP is nonzero, single-step it.
990 If SIGNAL is nonzero, give it that signal. */
991
992 void
993 child_resume (int pid, int step, enum target_signal signal)
994 {
995 int request = PTRACE_CONT;
996
997 if (pid == -1)
998 /* Resume all threads. */
999 /* I think this only gets used in the non-threaded case, where "resume
1000 all threads" and "resume inferior_pid" are the same. */
1001 pid = inferior_pid;
1002
1003 if (step)
1004 {
1005 CORE_ADDR pc = read_pc_pid (pid);
1006 unsigned char buf[LINUX_SYSCALL_LEN];
1007
1008 request = PTRACE_SINGLESTEP;
1009
1010 /* Returning from a signal trampoline is done by calling a
1011 special system call (sigreturn or rt_sigreturn, see
1012 i386-linux-tdep.c for more information). This system call
1013 restores the registers that were saved when the signal was
1014 raised, including %eflags. That means that single-stepping
1015 won't work. Instead, we'll have to modify the signal context
1016 that's about to be restored, and set the trace flag there. */
1017
1018 /* First check if PC is at a system call. */
1019 if (read_memory_nobpt (pc, (char *) buf, LINUX_SYSCALL_LEN) == 0
1020 && memcmp (buf, linux_syscall, LINUX_SYSCALL_LEN) == 0)
1021 {
1022 int syscall = read_register_pid (LINUX_SYSCALL_REGNUM, pid);
1023
1024 /* Then check the system call number. */
1025 if (syscall == SYS_sigreturn || syscall == SYS_rt_sigreturn)
1026 {
1027 CORE_ADDR sp = read_register (SP_REGNUM);
1028 CORE_ADDR addr = sp;
1029 unsigned long int eflags;
1030
1031 if (syscall == SYS_rt_sigreturn)
1032 addr = read_memory_integer (sp + 8, 4) + 20;
1033
1034 /* Set the trace flag in the context that's about to be
1035 restored. */
1036 addr += LINUX_SIGCONTEXT_EFLAGS_OFFSET;
1037 read_memory (addr, (char *) &eflags, 4);
1038 eflags |= 0x0100;
1039 write_memory (addr, (char *) &eflags, 4);
1040 }
1041 }
1042 }
1043
1044 if (ptrace (request, pid, 0, target_signal_to_host (signal)) == -1)
1045 perror_with_name ("ptrace");
1046 }
1047
1048 \f
1049 /* Calling functions in shared libraries. */
1050 /* FIXME: kettenis/2000-03-05: Doesn't this belong in a
1051 target-dependent file? The function
1052 `i386_linux_skip_solib_resolver' is mentioned in
1053 `config/i386/tm-linux.h'. */
1054
1055 /* Find the minimal symbol named NAME, and return both the minsym
1056 struct and its objfile. This probably ought to be in minsym.c, but
1057 everything there is trying to deal with things like C++ and
1058 SOFUN_ADDRESS_MAYBE_TURQUOISE, ... Since this is so simple, it may
1059 be considered too special-purpose for general consumption. */
1060
1061 static struct minimal_symbol *
1062 find_minsym_and_objfile (char *name, struct objfile **objfile_p)
1063 {
1064 struct objfile *objfile;
1065
1066 ALL_OBJFILES (objfile)
1067 {
1068 struct minimal_symbol *msym;
1069
1070 ALL_OBJFILE_MSYMBOLS (objfile, msym)
1071 {
1072 if (SYMBOL_NAME (msym)
1073 && STREQ (SYMBOL_NAME (msym), name))
1074 {
1075 *objfile_p = objfile;
1076 return msym;
1077 }
1078 }
1079 }
1080
1081 return 0;
1082 }
1083
1084
1085 static CORE_ADDR
1086 skip_hurd_resolver (CORE_ADDR pc)
1087 {
1088 /* The HURD dynamic linker is part of the GNU C library, so many
1089 GNU/Linux distributions use it. (All ELF versions, as far as I
1090 know.) An unresolved PLT entry points to "_dl_runtime_resolve",
1091 which calls "fixup" to patch the PLT, and then passes control to
1092 the function.
1093
1094 We look for the symbol `_dl_runtime_resolve', and find `fixup' in
1095 the same objfile. If we are at the entry point of `fixup', then
1096 we set a breakpoint at the return address (at the top of the
1097 stack), and continue.
1098
1099 It's kind of gross to do all these checks every time we're
1100 called, since they don't change once the executable has gotten
1101 started. But this is only a temporary hack --- upcoming versions
1102 of Linux will provide a portable, efficient interface for
1103 debugging programs that use shared libraries. */
1104
1105 struct objfile *objfile;
1106 struct minimal_symbol *resolver
1107 = find_minsym_and_objfile ("_dl_runtime_resolve", &objfile);
1108
1109 if (resolver)
1110 {
1111 struct minimal_symbol *fixup
1112 = lookup_minimal_symbol ("fixup", 0, objfile);
1113
1114 if (fixup && SYMBOL_VALUE_ADDRESS (fixup) == pc)
1115 return (SAVED_PC_AFTER_CALL (get_current_frame ()));
1116 }
1117
1118 return 0;
1119 }
1120
1121 /* See the comments for SKIP_SOLIB_RESOLVER at the top of infrun.c.
1122 This function:
1123 1) decides whether a PLT has sent us into the linker to resolve
1124 a function reference, and
1125 2) if so, tells us where to set a temporary breakpoint that will
1126 trigger when the dynamic linker is done. */
1127
1128 CORE_ADDR
1129 i386_linux_skip_solib_resolver (CORE_ADDR pc)
1130 {
1131 CORE_ADDR result;
1132
1133 /* Plug in functions for other kinds of resolvers here. */
1134 result = skip_hurd_resolver (pc);
1135 if (result)
1136 return result;
1137
1138 return 0;
1139 }
1140
1141 \f
1142 /* Register that we are able to handle Linux ELF core file formats. */
1143
1144 static struct core_fns linux_elf_core_fns =
1145 {
1146 bfd_target_elf_flavour, /* core_flavour */
1147 default_check_format, /* check_format */
1148 default_core_sniffer, /* core_sniffer */
1149 fetch_core_registers, /* core_read_registers */
1150 NULL /* next */
1151 };
1152
1153 void
1154 _initialize_i386_linux_nat ()
1155 {
1156 add_core_fns (&linux_elf_core_fns);
1157 }
This page took 0.053512 seconds and 4 git commands to generate.