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