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