* mips-tdep.c (mips_type_needs_double_align): New function.
[deliverable/binutils-gdb.git] / gdb / i386-linux-nat.c
1 /* Native-dependent code for Linux/x86.
2 Copyright 1999, 2000, 2001 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 #include "regcache.h"
25
26 #include "gdb_assert.h"
27 #include <sys/ptrace.h>
28 #include <sys/user.h>
29 #include <sys/procfs.h>
30
31 #ifdef HAVE_SYS_REG_H
32 #include <sys/reg.h>
33 #endif
34
35 #ifdef HAVE_SYS_DEBUGREG_H
36 #include <sys/debugreg.h>
37 #endif
38
39 #ifndef DR_FIRSTADDR
40 #define DR_FIRSTADDR 0
41 #endif
42
43 #ifndef DR_LASTADDR
44 #define DR_LASTADDR 3
45 #endif
46
47 #ifndef DR_STATUS
48 #define DR_STATUS 6
49 #endif
50
51 #ifndef DR_CONTROL
52 #define DR_CONTROL 7
53 #endif
54
55 /* Prototypes for supply_gregset etc. */
56 #include "gregset.h"
57
58 /* Prototypes for i387_supply_fsave etc. */
59 #include "i387-nat.h"
60
61 /* Prototypes for local functions. */
62 static void dummy_sse_values (void);
63
64 \f
65
66 /* The register sets used in Linux ELF core-dumps are identical to the
67 register sets in `struct user' that is used for a.out core-dumps,
68 and is also used by `ptrace'. The corresponding types are
69 `elf_gregset_t' for the general-purpose registers (with
70 `elf_greg_t' the type of a single GP register) and `elf_fpregset_t'
71 for the floating-point registers.
72
73 Those types used to be available under the names `gregset_t' and
74 `fpregset_t' too, and this file used those names in the past. But
75 those names are now used for the register sets used in the
76 `mcontext_t' type, and have a different size and layout. */
77
78 /* Mapping between the general-purpose registers in `struct user'
79 format and GDB's register array layout. */
80 static int regmap[] =
81 {
82 EAX, ECX, EDX, EBX,
83 UESP, EBP, ESI, EDI,
84 EIP, EFL, CS, SS,
85 DS, ES, FS, GS
86 };
87
88 /* Which ptrace request retrieves which registers?
89 These apply to the corresponding SET requests as well. */
90 #define GETREGS_SUPPLIES(regno) \
91 (0 <= (regno) && (regno) <= 15)
92 #define GETFPREGS_SUPPLIES(regno) \
93 (FP0_REGNUM <= (regno) && (regno) <= LAST_FPU_CTRL_REGNUM)
94 #define GETFPXREGS_SUPPLIES(regno) \
95 (FP0_REGNUM <= (regno) && (regno) <= MXCSR_REGNUM)
96
97 /* Does the current host support the GETREGS request? */
98 int have_ptrace_getregs =
99 #ifdef HAVE_PTRACE_GETREGS
100 1
101 #else
102 0
103 #endif
104 ;
105
106 /* Does the current host support the GETFPXREGS request? The header
107 file may or may not define it, and even if it is defined, the
108 kernel will return EIO if it's running on a pre-SSE processor.
109
110 My instinct is to attach this to some architecture- or
111 target-specific data structure, but really, a particular GDB
112 process can only run on top of one kernel at a time. So it's okay
113 for this to be a simple variable. */
114 int have_ptrace_getfpxregs =
115 #ifdef HAVE_PTRACE_GETFPXREGS
116 1
117 #else
118 0
119 #endif
120 ;
121 \f
122
123 /* Support for the user struct. */
124
125 /* Return the address of register REGNUM. BLOCKEND is the value of
126 u.u_ar0, which should point to the registers. */
127
128 CORE_ADDR
129 register_u_addr (CORE_ADDR blockend, int regnum)
130 {
131 return (blockend + 4 * regmap[regnum]);
132 }
133
134 /* Return the size of the user struct. */
135
136 int
137 kernel_u_size (void)
138 {
139 return (sizeof (struct user));
140 }
141 \f
142
143 /* Fetching registers directly from the U area, one at a time. */
144
145 /* FIXME: kettenis/2000-03-05: This duplicates code from `inptrace.c'.
146 The problem is that we define FETCH_INFERIOR_REGISTERS since we
147 want to use our own versions of {fetch,store}_inferior_registers
148 that use the GETREGS request. This means that the code in
149 `infptrace.c' is #ifdef'd out. But we need to fall back on that
150 code when GDB is running on top of a kernel that doesn't support
151 the GETREGS request. I want to avoid changing `infptrace.c' right
152 now. */
153
154 #ifndef PT_READ_U
155 #define PT_READ_U PTRACE_PEEKUSR
156 #endif
157 #ifndef PT_WRITE_U
158 #define PT_WRITE_U PTRACE_POKEUSR
159 #endif
160
161 /* Default the type of the ptrace transfer to int. */
162 #ifndef PTRACE_XFER_TYPE
163 #define PTRACE_XFER_TYPE int
164 #endif
165
166 /* Registers we shouldn't try to fetch. */
167 #define OLD_CANNOT_FETCH_REGISTER(regno) ((regno) >= NUM_GREGS)
168
169 /* Fetch one register. */
170
171 static void
172 fetch_register (int regno)
173 {
174 /* This isn't really an address. But ptrace thinks of it as one. */
175 CORE_ADDR regaddr;
176 char mess[128]; /* For messages */
177 register int i;
178 unsigned int offset; /* Offset of registers within the u area. */
179 char buf[MAX_REGISTER_RAW_SIZE];
180 int tid;
181
182 if (OLD_CANNOT_FETCH_REGISTER (regno))
183 {
184 memset (buf, '\0', REGISTER_RAW_SIZE (regno)); /* Supply zeroes */
185 supply_register (regno, buf);
186 return;
187 }
188
189 /* Overload thread id onto process id */
190 if ((tid = TIDGET (inferior_ptid)) == 0)
191 tid = PIDGET (inferior_ptid); /* no thread id, just use process id */
192
193 offset = U_REGS_OFFSET;
194
195 regaddr = register_addr (regno, offset);
196 for (i = 0; i < REGISTER_RAW_SIZE (regno); i += sizeof (PTRACE_XFER_TYPE))
197 {
198 errno = 0;
199 *(PTRACE_XFER_TYPE *) & buf[i] = ptrace (PT_READ_U, tid,
200 (PTRACE_ARG3_TYPE) regaddr, 0);
201 regaddr += sizeof (PTRACE_XFER_TYPE);
202 if (errno != 0)
203 {
204 sprintf (mess, "reading register %s (#%d)",
205 REGISTER_NAME (regno), regno);
206 perror_with_name (mess);
207 }
208 }
209 supply_register (regno, buf);
210 }
211
212 /* Fetch register values from the inferior.
213 If REGNO is negative, do this for all registers.
214 Otherwise, REGNO specifies which register (so we can save time). */
215
216 void
217 old_fetch_inferior_registers (int regno)
218 {
219 if (regno >= 0)
220 {
221 fetch_register (regno);
222 }
223 else
224 {
225 for (regno = 0; regno < NUM_REGS; regno++)
226 {
227 fetch_register (regno);
228 }
229 }
230 }
231
232 /* Registers we shouldn't try to store. */
233 #define OLD_CANNOT_STORE_REGISTER(regno) ((regno) >= NUM_GREGS)
234
235 /* Store one register. */
236
237 static void
238 store_register (int regno)
239 {
240 /* This isn't really an address. But ptrace thinks of it as one. */
241 CORE_ADDR regaddr;
242 char mess[128]; /* For messages */
243 register int i;
244 unsigned int offset; /* Offset of registers within the u area. */
245 int tid;
246
247 if (OLD_CANNOT_STORE_REGISTER (regno))
248 {
249 return;
250 }
251
252 /* Overload thread id onto process id */
253 if ((tid = TIDGET (inferior_ptid)) == 0)
254 tid = PIDGET (inferior_ptid); /* no thread id, just use process id */
255
256 offset = U_REGS_OFFSET;
257
258 regaddr = register_addr (regno, offset);
259 for (i = 0; i < REGISTER_RAW_SIZE (regno); i += sizeof (PTRACE_XFER_TYPE))
260 {
261 errno = 0;
262 ptrace (PT_WRITE_U, tid, (PTRACE_ARG3_TYPE) regaddr,
263 *(PTRACE_XFER_TYPE *) & registers[REGISTER_BYTE (regno) + i]);
264 regaddr += sizeof (PTRACE_XFER_TYPE);
265 if (errno != 0)
266 {
267 sprintf (mess, "writing register %s (#%d)",
268 REGISTER_NAME (regno), regno);
269 perror_with_name (mess);
270 }
271 }
272 }
273
274 /* Store our register values back into the inferior.
275 If REGNO is negative, do this for all registers.
276 Otherwise, REGNO specifies which register (so we can save time). */
277
278 void
279 old_store_inferior_registers (int regno)
280 {
281 if (regno >= 0)
282 {
283 store_register (regno);
284 }
285 else
286 {
287 for (regno = 0; regno < NUM_REGS; regno++)
288 {
289 store_register (regno);
290 }
291 }
292 }
293 \f
294
295 /* Transfering the general-purpose registers between GDB, inferiors
296 and core files. */
297
298 /* Fill GDB's register array with the general-purpose register values
299 in *GREGSETP. */
300
301 void
302 supply_gregset (elf_gregset_t *gregsetp)
303 {
304 elf_greg_t *regp = (elf_greg_t *) gregsetp;
305 int i;
306
307 for (i = 0; i < NUM_GREGS; i++)
308 supply_register (i, (char *) (regp + regmap[i]));
309 }
310
311 /* Fill register REGNO (if it is a general-purpose register) in
312 *GREGSETPS with the value in GDB's register array. If REGNO is -1,
313 do this for all registers. */
314
315 void
316 fill_gregset (elf_gregset_t *gregsetp, int regno)
317 {
318 elf_greg_t *regp = (elf_greg_t *) gregsetp;
319 int i;
320
321 for (i = 0; i < NUM_GREGS; i++)
322 if ((regno == -1 || regno == i))
323 *(regp + regmap[i]) = *(elf_greg_t *) &registers[REGISTER_BYTE (i)];
324 }
325
326 #ifdef HAVE_PTRACE_GETREGS
327
328 /* Fetch all general-purpose registers from process/thread TID and
329 store their values in GDB's register array. */
330
331 static void
332 fetch_regs (int tid)
333 {
334 elf_gregset_t regs;
335
336 if (ptrace (PTRACE_GETREGS, tid, 0, (int) &regs) < 0)
337 {
338 if (errno == EIO)
339 {
340 /* The kernel we're running on doesn't support the GETREGS
341 request. Reset `have_ptrace_getregs'. */
342 have_ptrace_getregs = 0;
343 return;
344 }
345
346 perror_with_name ("Couldn't get registers");
347 }
348
349 supply_gregset (&regs);
350 }
351
352 /* Store all valid general-purpose registers in GDB's register array
353 into the process/thread specified by TID. */
354
355 static void
356 store_regs (int tid, int regno)
357 {
358 elf_gregset_t regs;
359
360 if (ptrace (PTRACE_GETREGS, tid, 0, (int) &regs) < 0)
361 perror_with_name ("Couldn't get registers");
362
363 fill_gregset (&regs, regno);
364
365 if (ptrace (PTRACE_SETREGS, tid, 0, (int) &regs) < 0)
366 perror_with_name ("Couldn't write registers");
367 }
368
369 #else
370
371 static void fetch_regs (int tid) {}
372 static void store_regs (int tid, int regno) {}
373
374 #endif
375 \f
376
377 /* Transfering floating-point registers between GDB, inferiors and cores. */
378
379 /* Fill GDB's register array with the floating-point register values in
380 *FPREGSETP. */
381
382 void
383 supply_fpregset (elf_fpregset_t *fpregsetp)
384 {
385 i387_supply_fsave ((char *) fpregsetp);
386 dummy_sse_values ();
387 }
388
389 /* Fill register REGNO (if it is a floating-point register) in
390 *FPREGSETP with the value in GDB's register array. If REGNO is -1,
391 do this for all registers. */
392
393 void
394 fill_fpregset (elf_fpregset_t *fpregsetp, int regno)
395 {
396 i387_fill_fsave ((char *) fpregsetp, regno);
397 }
398
399 #ifdef HAVE_PTRACE_GETREGS
400
401 /* Fetch all floating-point registers from process/thread TID and store
402 thier values in GDB's register array. */
403
404 static void
405 fetch_fpregs (int tid)
406 {
407 elf_fpregset_t fpregs;
408
409 if (ptrace (PTRACE_GETFPREGS, tid, 0, (int) &fpregs) < 0)
410 perror_with_name ("Couldn't get floating point status");
411
412 supply_fpregset (&fpregs);
413 }
414
415 /* Store all valid floating-point registers in GDB's register array
416 into the process/thread specified by TID. */
417
418 static void
419 store_fpregs (int tid, int regno)
420 {
421 elf_fpregset_t fpregs;
422
423 if (ptrace (PTRACE_GETFPREGS, tid, 0, (int) &fpregs) < 0)
424 perror_with_name ("Couldn't get floating point status");
425
426 fill_fpregset (&fpregs, regno);
427
428 if (ptrace (PTRACE_SETFPREGS, tid, 0, (int) &fpregs) < 0)
429 perror_with_name ("Couldn't write floating point status");
430 }
431
432 #else
433
434 static void fetch_fpregs (int tid) {}
435 static void store_fpregs (int tid, int regno) {}
436
437 #endif
438 \f
439
440 /* Transfering floating-point and SSE registers to and from GDB. */
441
442 #ifdef HAVE_PTRACE_GETFPXREGS
443
444 /* Fill GDB's register array with the floating-point and SSE register
445 values in *FPXREGSETP. */
446
447 static void
448 supply_fpxregset (elf_fpxregset_t *fpxregsetp)
449 {
450 i387_supply_fxsave ((char *) fpxregsetp);
451 }
452
453 /* Fill register REGNO (if it is a floating-point or SSE register) in
454 *FPXREGSETP with the value in GDB's register array. If REGNO is
455 -1, do this for all registers. */
456
457 static void
458 fill_fpxregset (elf_fpxregset_t *fpxregsetp, int regno)
459 {
460 i387_fill_fxsave ((char *) fpxregsetp, regno);
461 }
462
463 /* Fetch all registers covered by the PTRACE_GETFPXREGS request from
464 process/thread TID and store their values in GDB's register array.
465 Return non-zero if successful, zero otherwise. */
466
467 static int
468 fetch_fpxregs (int tid)
469 {
470 elf_fpxregset_t fpxregs;
471
472 if (! have_ptrace_getfpxregs)
473 return 0;
474
475 if (ptrace (PTRACE_GETFPXREGS, tid, 0, (int) &fpxregs) < 0)
476 {
477 if (errno == EIO)
478 {
479 have_ptrace_getfpxregs = 0;
480 return 0;
481 }
482
483 perror_with_name ("Couldn't read floating-point and SSE registers");
484 }
485
486 supply_fpxregset (&fpxregs);
487 return 1;
488 }
489
490 /* Store all valid registers in GDB's register array covered by the
491 PTRACE_SETFPXREGS request into the process/thread specified by TID.
492 Return non-zero if successful, zero otherwise. */
493
494 static int
495 store_fpxregs (int tid, int regno)
496 {
497 elf_fpxregset_t fpxregs;
498
499 if (! have_ptrace_getfpxregs)
500 return 0;
501
502 if (ptrace (PTRACE_GETFPXREGS, tid, 0, &fpxregs) == -1)
503 {
504 if (errno == EIO)
505 {
506 have_ptrace_getfpxregs = 0;
507 return 0;
508 }
509
510 perror_with_name ("Couldn't read floating-point and SSE registers");
511 }
512
513 fill_fpxregset (&fpxregs, regno);
514
515 if (ptrace (PTRACE_SETFPXREGS, tid, 0, &fpxregs) == -1)
516 perror_with_name ("Couldn't write floating-point and SSE registers");
517
518 return 1;
519 }
520
521 /* Fill the XMM registers in the register array with dummy values. For
522 cases where we don't have access to the XMM registers. I think
523 this is cleaner than printing a warning. For a cleaner solution,
524 we should gdbarchify the i386 family. */
525
526 static void
527 dummy_sse_values (void)
528 {
529 /* C doesn't have a syntax for NaN's, so write it out as an array of
530 longs. */
531 static long dummy[4] = { 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff };
532 static long mxcsr = 0x1f80;
533 int reg;
534
535 for (reg = 0; reg < 8; reg++)
536 supply_register (XMM0_REGNUM + reg, (char *) dummy);
537 supply_register (MXCSR_REGNUM, (char *) &mxcsr);
538 }
539
540 #else
541
542 static int fetch_fpxregs (int tid) { return 0; }
543 static int store_fpxregs (int tid, int regno) { return 0; }
544 static void dummy_sse_values (void) {}
545
546 #endif /* HAVE_PTRACE_GETFPXREGS */
547 \f
548
549 /* Transferring arbitrary registers between GDB and inferior. */
550
551 /* Check if register REGNO in the child process is accessible.
552 If we are accessing registers directly via the U area, only the
553 general-purpose registers are available.
554 All registers should be accessible if we have GETREGS support. */
555
556 int
557 cannot_fetch_register (int regno)
558 {
559 if (! have_ptrace_getregs)
560 return OLD_CANNOT_FETCH_REGISTER (regno);
561 return 0;
562 }
563 int
564 cannot_store_register (int regno)
565 {
566 if (! have_ptrace_getregs)
567 return OLD_CANNOT_STORE_REGISTER (regno);
568 return 0;
569 }
570
571 /* Fetch register REGNO from the child process. If REGNO is -1, do
572 this for all registers (including the floating point and SSE
573 registers). */
574
575 void
576 fetch_inferior_registers (int regno)
577 {
578 int tid;
579
580 /* Use the old method of peeking around in `struct user' if the
581 GETREGS request isn't available. */
582 if (! have_ptrace_getregs)
583 {
584 old_fetch_inferior_registers (regno);
585 return;
586 }
587
588 /* Linux LWP ID's are process ID's. */
589 if ((tid = TIDGET (inferior_ptid)) == 0)
590 tid = PIDGET (inferior_ptid); /* Not a threaded program. */
591
592 /* Use the PTRACE_GETFPXREGS request whenever possible, since it
593 transfers more registers in one system call, and we'll cache the
594 results. But remember that fetch_fpxregs can fail, and return
595 zero. */
596 if (regno == -1)
597 {
598 fetch_regs (tid);
599
600 /* The call above might reset `have_ptrace_getregs'. */
601 if (! have_ptrace_getregs)
602 {
603 old_fetch_inferior_registers (-1);
604 return;
605 }
606
607 if (fetch_fpxregs (tid))
608 return;
609 fetch_fpregs (tid);
610 return;
611 }
612
613 if (GETREGS_SUPPLIES (regno))
614 {
615 fetch_regs (tid);
616 return;
617 }
618
619 if (GETFPXREGS_SUPPLIES (regno))
620 {
621 if (fetch_fpxregs (tid))
622 return;
623
624 /* Either our processor or our kernel doesn't support the SSE
625 registers, so read the FP registers in the traditional way,
626 and fill the SSE registers with dummy values. It would be
627 more graceful to handle differences in the register set using
628 gdbarch. Until then, this will at least make things work
629 plausibly. */
630 fetch_fpregs (tid);
631 return;
632 }
633
634 internal_error (__FILE__, __LINE__,
635 "Got request for bad register number %d.", regno);
636 }
637
638 /* Store register REGNO back into the child process. If REGNO is -1,
639 do this for all registers (including the floating point and SSE
640 registers). */
641 void
642 store_inferior_registers (int regno)
643 {
644 int tid;
645
646 /* Use the old method of poking around in `struct user' if the
647 SETREGS request isn't available. */
648 if (! have_ptrace_getregs)
649 {
650 old_store_inferior_registers (regno);
651 return;
652 }
653
654 /* Linux LWP ID's are process ID's. */
655 if ((tid = TIDGET (inferior_ptid)) == 0)
656 tid = PIDGET (inferior_ptid); /* Not a threaded program. */
657
658 /* Use the PTRACE_SETFPXREGS requests whenever possible, since it
659 transfers more registers in one system call. But remember that
660 store_fpxregs can fail, and return zero. */
661 if (regno == -1)
662 {
663 store_regs (tid, regno);
664 if (store_fpxregs (tid, regno))
665 return;
666 store_fpregs (tid, regno);
667 return;
668 }
669
670 if (GETREGS_SUPPLIES (regno))
671 {
672 store_regs (tid, regno);
673 return;
674 }
675
676 if (GETFPXREGS_SUPPLIES (regno))
677 {
678 if (store_fpxregs (tid, regno))
679 return;
680
681 /* Either our processor or our kernel doesn't support the SSE
682 registers, so just write the FP registers in the traditional
683 way. */
684 store_fpregs (tid, regno);
685 return;
686 }
687
688 internal_error (__FILE__, __LINE__,
689 "Got request to store bad register number %d.", regno);
690 }
691 \f
692
693 static unsigned long
694 i386_linux_dr_get (int regnum)
695 {
696 int tid;
697 unsigned long value;
698
699 /* FIXME: kettenis/2001-01-29: It's not clear what we should do with
700 multi-threaded processes here. For now, pretend there is just
701 one thread. */
702 tid = PIDGET (inferior_ptid);
703
704 /* FIXME: kettenis/2001-03-27: Calling perror_with_name if the
705 ptrace call fails breaks debugging remote targets. The correct
706 way to fix this is to add the hardware breakpoint and watchpoint
707 stuff to the target vectore. For now, just return zero if the
708 ptrace call fails. */
709 errno = 0;
710 value = ptrace (PT_READ_U, tid,
711 offsetof (struct user, u_debugreg[regnum]), 0);
712 if (errno != 0)
713 #if 0
714 perror_with_name ("Couldn't read debug register");
715 #else
716 return 0;
717 #endif
718
719 return value;
720 }
721
722 static void
723 i386_linux_dr_set (int regnum, unsigned long value)
724 {
725 int tid;
726
727 /* FIXME: kettenis/2001-01-29: It's not clear what we should do with
728 multi-threaded processes here. For now, pretend there is just
729 one thread. */
730 tid = PIDGET (inferior_ptid);
731
732 errno = 0;
733 ptrace (PT_WRITE_U, tid,
734 offsetof (struct user, u_debugreg[regnum]), value);
735 if (errno != 0)
736 perror_with_name ("Couldn't write debug register");
737 }
738
739 void
740 i386_linux_dr_set_control (unsigned long control)
741 {
742 i386_linux_dr_set (DR_CONTROL, control);
743 }
744
745 void
746 i386_linux_dr_set_addr (int regnum, CORE_ADDR addr)
747 {
748 gdb_assert (regnum >= 0 && regnum <= DR_LASTADDR - DR_FIRSTADDR);
749
750 i386_linux_dr_set (DR_FIRSTADDR + regnum, addr);
751 }
752
753 void
754 i386_linux_dr_reset_addr (int regnum)
755 {
756 gdb_assert (regnum >= 0 && regnum <= DR_LASTADDR - DR_FIRSTADDR);
757
758 i386_linux_dr_set (DR_FIRSTADDR + regnum, 0L);
759 }
760
761 unsigned long
762 i386_linux_dr_get_status (void)
763 {
764 return i386_linux_dr_get (DR_STATUS);
765 }
766 \f
767
768 /* Interpreting register set info found in core files. */
769
770 /* Provide registers to GDB from a core file.
771
772 (We can't use the generic version of this function in
773 core-regset.c, because Linux has *three* different kinds of
774 register set notes. core-regset.c would have to call
775 supply_fpxregset, which most platforms don't have.)
776
777 CORE_REG_SECT points to an array of bytes, which are the contents
778 of a `note' from a core file which BFD thinks might contain
779 register contents. CORE_REG_SIZE is its size.
780
781 WHICH says which register set corelow suspects this is:
782 0 --- the general-purpose register set, in elf_gregset_t format
783 2 --- the floating-point register set, in elf_fpregset_t format
784 3 --- the extended floating-point register set, in elf_fpxregset_t format
785
786 REG_ADDR isn't used on Linux. */
787
788 static void
789 fetch_core_registers (char *core_reg_sect, unsigned core_reg_size,
790 int which, CORE_ADDR reg_addr)
791 {
792 elf_gregset_t gregset;
793 elf_fpregset_t fpregset;
794
795 switch (which)
796 {
797 case 0:
798 if (core_reg_size != sizeof (gregset))
799 warning ("Wrong size gregset in core file.");
800 else
801 {
802 memcpy (&gregset, core_reg_sect, sizeof (gregset));
803 supply_gregset (&gregset);
804 }
805 break;
806
807 case 2:
808 if (core_reg_size != sizeof (fpregset))
809 warning ("Wrong size fpregset in core file.");
810 else
811 {
812 memcpy (&fpregset, core_reg_sect, sizeof (fpregset));
813 supply_fpregset (&fpregset);
814 }
815 break;
816
817 #ifdef HAVE_PTRACE_GETFPXREGS
818 {
819 elf_fpxregset_t fpxregset;
820
821 case 3:
822 if (core_reg_size != sizeof (fpxregset))
823 warning ("Wrong size fpxregset in core file.");
824 else
825 {
826 memcpy (&fpxregset, core_reg_sect, sizeof (fpxregset));
827 supply_fpxregset (&fpxregset);
828 }
829 break;
830 }
831 #endif
832
833 default:
834 /* We've covered all the kinds of registers we know about here,
835 so this must be something we wouldn't know what to do with
836 anyway. Just ignore it. */
837 break;
838 }
839 }
840 \f
841
842 /* The instruction for a Linux system call is:
843 int $0x80
844 or 0xcd 0x80. */
845
846 static const unsigned char linux_syscall[] = { 0xcd, 0x80 };
847
848 #define LINUX_SYSCALL_LEN (sizeof linux_syscall)
849
850 /* The system call number is stored in the %eax register. */
851 #define LINUX_SYSCALL_REGNUM 0 /* %eax */
852
853 /* We are specifically interested in the sigreturn and rt_sigreturn
854 system calls. */
855
856 #ifndef SYS_sigreturn
857 #define SYS_sigreturn 0x77
858 #endif
859 #ifndef SYS_rt_sigreturn
860 #define SYS_rt_sigreturn 0xad
861 #endif
862
863 /* Offset to saved processor flags, from <asm/sigcontext.h>. */
864 #define LINUX_SIGCONTEXT_EFLAGS_OFFSET (64)
865
866 /* Resume execution of the inferior process.
867 If STEP is nonzero, single-step it.
868 If SIGNAL is nonzero, give it that signal. */
869
870 void
871 child_resume (ptid_t ptid, int step, enum target_signal signal)
872 {
873 int pid = PIDGET (ptid);
874
875 int request = PTRACE_CONT;
876
877 if (pid == -1)
878 /* Resume all threads. */
879 /* I think this only gets used in the non-threaded case, where "resume
880 all threads" and "resume inferior_ptid" are the same. */
881 pid = PIDGET (inferior_ptid);
882
883 if (step)
884 {
885 CORE_ADDR pc = read_pc_pid (pid_to_ptid (pid));
886 unsigned char buf[LINUX_SYSCALL_LEN];
887
888 request = PTRACE_SINGLESTEP;
889
890 /* Returning from a signal trampoline is done by calling a
891 special system call (sigreturn or rt_sigreturn, see
892 i386-linux-tdep.c for more information). This system call
893 restores the registers that were saved when the signal was
894 raised, including %eflags. That means that single-stepping
895 won't work. Instead, we'll have to modify the signal context
896 that's about to be restored, and set the trace flag there. */
897
898 /* First check if PC is at a system call. */
899 if (read_memory_nobpt (pc, (char *) buf, LINUX_SYSCALL_LEN) == 0
900 && memcmp (buf, linux_syscall, LINUX_SYSCALL_LEN) == 0)
901 {
902 int syscall = read_register_pid (LINUX_SYSCALL_REGNUM,
903 pid_to_ptid (pid));
904
905 /* Then check the system call number. */
906 if (syscall == SYS_sigreturn || syscall == SYS_rt_sigreturn)
907 {
908 CORE_ADDR sp = read_register (SP_REGNUM);
909 CORE_ADDR addr = sp;
910 unsigned long int eflags;
911
912 if (syscall == SYS_rt_sigreturn)
913 addr = read_memory_integer (sp + 8, 4) + 20;
914
915 /* Set the trace flag in the context that's about to be
916 restored. */
917 addr += LINUX_SIGCONTEXT_EFLAGS_OFFSET;
918 read_memory (addr, (char *) &eflags, 4);
919 eflags |= 0x0100;
920 write_memory (addr, (char *) &eflags, 4);
921 }
922 }
923 }
924
925 if (ptrace (request, pid, 0, target_signal_to_host (signal)) == -1)
926 perror_with_name ("ptrace");
927 }
928 \f
929
930 /* Register that we are able to handle Linux ELF core file formats. */
931
932 static struct core_fns linux_elf_core_fns =
933 {
934 bfd_target_elf_flavour, /* core_flavour */
935 default_check_format, /* check_format */
936 default_core_sniffer, /* core_sniffer */
937 fetch_core_registers, /* core_read_registers */
938 NULL /* next */
939 };
940
941 void
942 _initialize_i386_linux_nat (void)
943 {
944 add_core_fns (&linux_elf_core_fns);
945 }
This page took 0.050315 seconds and 4 git commands to generate.