* i386-linux-nat.c (suppy_gregset): Don't supply
[external/binutils.git] / gdb / i386-linux-nat.c
1 /* Native-dependent code for GNU/Linux x86.
2
3    Copyright 1999, 2000, 2001, 2002 Free Software Foundation, Inc.
4
5    This file is part of GDB.
6
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 2 of the License, or
10    (at your option) any later version.
11
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16
17    You should have received a copy of the GNU General Public License
18    along with this program; if not, write to the Free Software
19    Foundation, Inc., 59 Temple Place - Suite 330,
20    Boston, MA 02111-1307, USA.  */
21
22 #include "defs.h"
23 #include "inferior.h"
24 #include "gdbcore.h"
25 #include "regcache.h"
26
27 #include "gdb_assert.h"
28 #include <sys/ptrace.h>
29 #include <sys/user.h>
30 #include <sys/procfs.h>
31
32 #ifdef HAVE_SYS_REG_H
33 #include <sys/reg.h>
34 #endif
35
36 #ifdef HAVE_SYS_DEBUGREG_H
37 #include <sys/debugreg.h>
38 #endif
39
40 #ifndef DR_FIRSTADDR
41 #define DR_FIRSTADDR 0
42 #endif
43
44 #ifndef DR_LASTADDR
45 #define DR_LASTADDR 3
46 #endif
47
48 #ifndef DR_STATUS
49 #define DR_STATUS 6
50 #endif
51
52 #ifndef DR_CONTROL
53 #define DR_CONTROL 7
54 #endif
55
56 /* Prototypes for supply_gregset etc.  */
57 #include "gregset.h"
58
59 /* Prototypes for i387_supply_fsave etc.  */
60 #include "i387-tdep.h"
61
62 /* Defines for XMM0_REGNUM etc. */
63 #include "i386-tdep.h"
64
65 /* Prototypes for local functions.  */
66 static void dummy_sse_values (void);
67
68 \f
69
70 /* The register sets used in GNU/Linux ELF core-dumps are identical to
71    the register sets in `struct user' that is used for a.out
72    core-dumps, and is also used by `ptrace'.  The corresponding types
73    are `elf_gregset_t' for the general-purpose registers (with
74    `elf_greg_t' the type of a single GP register) and `elf_fpregset_t'
75    for the floating-point registers.
76
77    Those types used to be available under the names `gregset_t' and
78    `fpregset_t' too, and this file used those names in the past.  But
79    those names are now used for the register sets used in the
80    `mcontext_t' type, and have a different size and layout.  */
81
82 /* Mapping between the general-purpose registers in `struct user'
83    format and GDB's register array layout.  */
84 static int regmap[] = 
85 {
86   EAX, ECX, EDX, EBX,
87   UESP, EBP, ESI, EDI,
88   EIP, EFL, CS, SS,
89   DS, ES, FS, GS
90 };
91
92 /* Which ptrace request retrieves which registers?
93    These apply to the corresponding SET requests as well.  */
94 #define GETREGS_SUPPLIES(regno) \
95   ((0 <= (regno) && (regno) <= 15) || (regno) == I386_LINUX_ORIG_EAX_REGNUM)
96 #define GETFPREGS_SUPPLIES(regno) \
97   (FP0_REGNUM <= (regno) && (regno) <= LAST_FPU_CTRL_REGNUM)
98 #define GETFPXREGS_SUPPLIES(regno) \
99   (FP0_REGNUM <= (regno) && (regno) <= MXCSR_REGNUM)
100
101 /* Does the current host support the GETREGS request?  */
102 int have_ptrace_getregs =
103 #ifdef HAVE_PTRACE_GETREGS
104   1
105 #else
106   0
107 #endif
108 ;
109
110 /* Does the current host support the GETFPXREGS request?  The header
111    file may or may not define it, and even if it is defined, the
112    kernel will return EIO if it's running on a pre-SSE processor.
113
114    My instinct is to attach this to some architecture- or
115    target-specific data structure, but really, a particular GDB
116    process can only run on top of one kernel at a time.  So it's okay
117    for this to be a simple variable.  */
118 int have_ptrace_getfpxregs =
119 #ifdef HAVE_PTRACE_GETFPXREGS
120   1
121 #else
122   0
123 #endif
124 ;
125 \f
126
127 /* Support for the user struct.  */
128
129 /* Return the address of register REGNUM.  BLOCKEND is the value of
130    u.u_ar0, which should point to the registers.  */
131
132 CORE_ADDR
133 register_u_addr (CORE_ADDR blockend, int regnum)
134 {
135   return (blockend + 4 * regmap[regnum]);
136 }
137
138 /* Return the size of the user struct.  */
139
140 int
141 kernel_u_size (void)
142 {
143   return (sizeof (struct user));
144 }
145 \f
146
147 /* Fetching registers directly from the U area, one at a time.  */
148
149 /* FIXME: kettenis/2000-03-05: This duplicates code from `inptrace.c'.
150    The problem is that we define FETCH_INFERIOR_REGISTERS since we
151    want to use our own versions of {fetch,store}_inferior_registers
152    that use the GETREGS request.  This means that the code in
153    `infptrace.c' is #ifdef'd out.  But we need to fall back on that
154    code when GDB is running on top of a kernel that doesn't support
155    the GETREGS request.  I want to avoid changing `infptrace.c' right
156    now.  */
157
158 #ifndef PT_READ_U
159 #define PT_READ_U PTRACE_PEEKUSR
160 #endif
161 #ifndef PT_WRITE_U
162 #define PT_WRITE_U PTRACE_POKEUSR
163 #endif
164
165 /* Default the type of the ptrace transfer to int.  */
166 #ifndef PTRACE_XFER_TYPE
167 #define PTRACE_XFER_TYPE int
168 #endif
169
170 /* Registers we shouldn't try to fetch.  */
171 #define OLD_CANNOT_FETCH_REGISTER(regno) ((regno) >= NUM_GREGS)
172
173 /* Fetch one register.  */
174
175 static void
176 fetch_register (int regno)
177 {
178   /* This isn't really an address.  But ptrace thinks of it as one.  */
179   CORE_ADDR regaddr;
180   char mess[128];               /* For messages */
181   register int i;
182   unsigned int offset;          /* Offset of registers within the u area.  */
183   char buf[MAX_REGISTER_RAW_SIZE];
184   int tid;
185
186   if (OLD_CANNOT_FETCH_REGISTER (regno))
187     {
188       memset (buf, '\0', REGISTER_RAW_SIZE (regno));    /* Supply zeroes */
189       supply_register (regno, buf);
190       return;
191     }
192
193   /* Overload thread id onto process id */
194   if ((tid = TIDGET (inferior_ptid)) == 0)
195     tid = PIDGET (inferior_ptid);       /* no thread id, just use process id */
196
197   offset = U_REGS_OFFSET;
198
199   regaddr = register_addr (regno, offset);
200   for (i = 0; i < REGISTER_RAW_SIZE (regno); i += sizeof (PTRACE_XFER_TYPE))
201     {
202       errno = 0;
203       *(PTRACE_XFER_TYPE *) & buf[i] = ptrace (PT_READ_U, tid,
204                                                (PTRACE_ARG3_TYPE) regaddr, 0);
205       regaddr += sizeof (PTRACE_XFER_TYPE);
206       if (errno != 0)
207         {
208           sprintf (mess, "reading register %s (#%d)", 
209                    REGISTER_NAME (regno), regno);
210           perror_with_name (mess);
211         }
212     }
213   supply_register (regno, buf);
214 }
215
216 /* Fetch register values from the inferior.
217    If REGNO is negative, do this for all registers.
218    Otherwise, REGNO specifies which register (so we can save time). */
219
220 void
221 old_fetch_inferior_registers (int regno)
222 {
223   if (regno >= 0)
224     {
225       fetch_register (regno);
226     }
227   else
228     {
229       for (regno = 0; regno < NUM_REGS; regno++)
230         {
231           fetch_register (regno);
232         }
233     }
234 }
235
236 /* Registers we shouldn't try to store.  */
237 #define OLD_CANNOT_STORE_REGISTER(regno) ((regno) >= NUM_GREGS)
238
239 /* Store one register. */
240
241 static void
242 store_register (int regno)
243 {
244   /* This isn't really an address.  But ptrace thinks of it as one.  */
245   CORE_ADDR regaddr;
246   char mess[128];               /* For messages */
247   register int i;
248   unsigned int offset;          /* Offset of registers within the u area.  */
249   int tid;
250
251   if (OLD_CANNOT_STORE_REGISTER (regno))
252     {
253       return;
254     }
255
256   /* Overload thread id onto process id */
257   if ((tid = TIDGET (inferior_ptid)) == 0)
258     tid = PIDGET (inferior_ptid);       /* no thread id, just use process id */
259
260   offset = U_REGS_OFFSET;
261
262   regaddr = register_addr (regno, offset);
263   for (i = 0; i < REGISTER_RAW_SIZE (regno); i += sizeof (PTRACE_XFER_TYPE))
264     {
265       errno = 0;
266       ptrace (PT_WRITE_U, tid, (PTRACE_ARG3_TYPE) regaddr,
267               *(PTRACE_XFER_TYPE *) & registers[REGISTER_BYTE (regno) + i]);
268       regaddr += sizeof (PTRACE_XFER_TYPE);
269       if (errno != 0)
270         {
271           sprintf (mess, "writing register %s (#%d)", 
272                    REGISTER_NAME (regno), regno);
273           perror_with_name (mess);
274         }
275     }
276 }
277
278 /* Store our register values back into the inferior.
279    If REGNO is negative, do this for all registers.
280    Otherwise, REGNO specifies which register (so we can save time).  */
281
282 void
283 old_store_inferior_registers (int regno)
284 {
285   if (regno >= 0)
286     {
287       store_register (regno);
288     }
289   else
290     {
291       for (regno = 0; regno < NUM_REGS; regno++)
292         {
293           store_register (regno);
294         }
295     }
296 }
297 \f
298
299 /* Transfering the general-purpose registers between GDB, inferiors
300    and core files.  */
301
302 /* Fill GDB's register array with the general-purpose register values
303    in *GREGSETP.  */
304
305 void
306 supply_gregset (elf_gregset_t *gregsetp)
307 {
308   elf_greg_t *regp = (elf_greg_t *) gregsetp;
309   int i;
310
311   for (i = 0; i < NUM_GREGS; i++)
312     supply_register (i, (char *) (regp + regmap[i]));
313
314   if (I386_LINUX_ORIG_EAX_REGNUM < NUM_REGS)
315     supply_register (I386_LINUX_ORIG_EAX_REGNUM, (char *) (regp + ORIG_EAX));
316 }
317
318 /* Fill register REGNO (if it is a general-purpose register) in
319    *GREGSETPS with the value in GDB's register array.  If REGNO is -1,
320    do this for all registers.  */
321
322 void
323 fill_gregset (elf_gregset_t *gregsetp, int regno)
324 {
325   elf_greg_t *regp = (elf_greg_t *) gregsetp;
326   int i;
327
328   for (i = 0; i < NUM_GREGS; i++)
329     if ((regno == -1 || regno == i))
330       regcache_collect (i, regp + regmap[i]);
331
332   if ((regno == -1 || regno == I386_LINUX_ORIG_EAX_REGNUM)
333       && I386_LINUX_ORIG_EAX_REGNUM < NUM_REGS)
334     regcache_collect (I386_LINUX_ORIG_EAX_REGNUM, regp + ORIG_EAX);
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 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 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   /* GNU/Linux LWP ID's are process ID's.  */
600   if ((tid = TIDGET (inferior_ptid)) == 0)
601     tid = PIDGET (inferior_ptid);               /* 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   /* GNU/Linux LWP ID's are process ID's.  */
666   if ((tid = TIDGET (inferior_ptid)) == 0)
667     tid = PIDGET (inferior_ptid);       /* 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 unsigned long
705 i386_linux_dr_get (int regnum)
706 {
707   int tid;
708   unsigned 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_ptid);
714
715   /* FIXME: kettenis/2001-03-27: Calling perror_with_name if the
716      ptrace call fails breaks debugging remote targets.  The correct
717      way to fix this is to add the hardware breakpoint and watchpoint
718      stuff to the target vectore.  For now, just return zero if the
719      ptrace call fails.  */
720   errno = 0;
721   value = ptrace (PT_READ_U, tid,
722                   offsetof (struct user, u_debugreg[regnum]), 0);
723   if (errno != 0)
724 #if 0
725     perror_with_name ("Couldn't read debug register");
726 #else
727     return 0;
728 #endif
729
730   return value;
731 }
732
733 static void
734 i386_linux_dr_set (int regnum, unsigned long value)
735 {
736   int tid;
737
738   /* FIXME: kettenis/2001-01-29: It's not clear what we should do with
739      multi-threaded processes here.  For now, pretend there is just
740      one thread.  */
741   tid = PIDGET (inferior_ptid);
742
743   errno = 0;
744   ptrace (PT_WRITE_U, tid,
745           offsetof (struct user, u_debugreg[regnum]), value);
746   if (errno != 0)
747     perror_with_name ("Couldn't write debug register");
748 }
749
750 void
751 i386_linux_dr_set_control (unsigned long control)
752 {
753   i386_linux_dr_set (DR_CONTROL, control);
754 }
755
756 void
757 i386_linux_dr_set_addr (int regnum, CORE_ADDR addr)
758 {
759   gdb_assert (regnum >= 0 && regnum <= DR_LASTADDR - DR_FIRSTADDR);
760
761   i386_linux_dr_set (DR_FIRSTADDR + regnum, addr);
762 }
763
764 void
765 i386_linux_dr_reset_addr (int regnum)
766 {
767   gdb_assert (regnum >= 0 && regnum <= DR_LASTADDR - DR_FIRSTADDR);
768
769   i386_linux_dr_set (DR_FIRSTADDR + regnum, 0L);
770 }
771
772 unsigned long
773 i386_linux_dr_get_status (void)
774 {
775   return i386_linux_dr_get (DR_STATUS);
776 }
777 \f
778
779 /* Interpreting register set info found in core files.  */
780
781 /* Provide registers to GDB from a core file.
782
783    (We can't use the generic version of this function in
784    core-regset.c, because GNU/Linux has *three* different kinds of
785    register set notes.  core-regset.c would have to call
786    supply_fpxregset, which most platforms don't have.)
787
788    CORE_REG_SECT points to an array of bytes, which are the contents
789    of a `note' from a core file which BFD thinks might contain
790    register contents.  CORE_REG_SIZE is its size.
791
792    WHICH says which register set corelow suspects this is:
793      0 --- the general-purpose register set, in elf_gregset_t format
794      2 --- the floating-point register set, in elf_fpregset_t format
795      3 --- the extended floating-point register set, in elf_fpxregset_t format
796
797    REG_ADDR isn't used on GNU/Linux.  */
798
799 static void
800 fetch_core_registers (char *core_reg_sect, unsigned core_reg_size,
801                       int which, CORE_ADDR reg_addr)
802 {
803   elf_gregset_t gregset;
804   elf_fpregset_t fpregset;
805
806   switch (which)
807     {
808     case 0:
809       if (core_reg_size != sizeof (gregset))
810         warning ("Wrong size gregset in core file.");
811       else
812         {
813           memcpy (&gregset, core_reg_sect, sizeof (gregset));
814           supply_gregset (&gregset);
815         }
816       break;
817
818     case 2:
819       if (core_reg_size != sizeof (fpregset))
820         warning ("Wrong size fpregset in core file.");
821       else
822         {
823           memcpy (&fpregset, core_reg_sect, sizeof (fpregset));
824           supply_fpregset (&fpregset);
825         }
826       break;
827
828 #ifdef HAVE_PTRACE_GETFPXREGS
829       {
830         elf_fpxregset_t fpxregset;
831
832       case 3:
833         if (core_reg_size != sizeof (fpxregset))
834           warning ("Wrong size fpxregset in core file.");
835         else
836           {
837             memcpy (&fpxregset, core_reg_sect, sizeof (fpxregset));
838             supply_fpxregset (&fpxregset);
839           }
840         break;
841       }
842 #endif
843
844     default:
845       /* We've covered all the kinds of registers we know about here,
846          so this must be something we wouldn't know what to do with
847          anyway.  Just ignore it.  */
848       break;
849     }
850 }
851 \f
852
853 /* The instruction for a GNU/Linux system call is:
854        int $0x80
855    or 0xcd 0x80.  */
856
857 static const unsigned char linux_syscall[] = { 0xcd, 0x80 };
858
859 #define LINUX_SYSCALL_LEN (sizeof linux_syscall)
860
861 /* The system call number is stored in the %eax register.  */
862 #define LINUX_SYSCALL_REGNUM 0  /* %eax */
863
864 /* We are specifically interested in the sigreturn and rt_sigreturn
865    system calls.  */
866
867 #ifndef SYS_sigreturn
868 #define SYS_sigreturn           0x77
869 #endif
870 #ifndef SYS_rt_sigreturn
871 #define SYS_rt_sigreturn        0xad
872 #endif
873
874 /* Offset to saved processor flags, from <asm/sigcontext.h>.  */
875 #define LINUX_SIGCONTEXT_EFLAGS_OFFSET (64)
876
877 /* Resume execution of the inferior process.
878    If STEP is nonzero, single-step it.
879    If SIGNAL is nonzero, give it that signal.  */
880
881 void
882 child_resume (ptid_t ptid, int step, enum target_signal signal)
883 {
884   int pid = PIDGET (ptid);
885
886   int request = PTRACE_CONT;
887
888   if (pid == -1)
889     /* Resume all threads.  */
890     /* I think this only gets used in the non-threaded case, where "resume
891        all threads" and "resume inferior_ptid" are the same.  */
892     pid = PIDGET (inferior_ptid);
893
894   if (step)
895     {
896       CORE_ADDR pc = read_pc_pid (pid_to_ptid (pid));
897       unsigned char buf[LINUX_SYSCALL_LEN];
898
899       request = PTRACE_SINGLESTEP;
900
901       /* Returning from a signal trampoline is done by calling a
902          special system call (sigreturn or rt_sigreturn, see
903          i386-linux-tdep.c for more information).  This system call
904          restores the registers that were saved when the signal was
905          raised, including %eflags.  That means that single-stepping
906          won't work.  Instead, we'll have to modify the signal context
907          that's about to be restored, and set the trace flag there.  */
908
909       /* First check if PC is at a system call.  */
910       if (read_memory_nobpt (pc, (char *) buf, LINUX_SYSCALL_LEN) == 0
911           && memcmp (buf, linux_syscall, LINUX_SYSCALL_LEN) == 0)
912         {
913           int syscall = read_register_pid (LINUX_SYSCALL_REGNUM,
914                                            pid_to_ptid (pid));
915
916           /* Then check the system call number.  */
917           if (syscall == SYS_sigreturn || syscall == SYS_rt_sigreturn)
918             {
919               CORE_ADDR sp = read_register (SP_REGNUM);
920               CORE_ADDR addr = sp;
921               unsigned long int eflags;
922
923               if (syscall == SYS_rt_sigreturn)
924                 addr = read_memory_integer (sp + 8, 4) + 20;
925
926               /* Set the trace flag in the context that's about to be
927                  restored.  */
928               addr += LINUX_SIGCONTEXT_EFLAGS_OFFSET;
929               read_memory (addr, (char *) &eflags, 4);
930               eflags |= 0x0100;
931               write_memory (addr, (char *) &eflags, 4);
932             }
933         }
934     }
935
936   if (ptrace (request, pid, 0, target_signal_to_host (signal)) == -1)
937     perror_with_name ("ptrace");
938 }
939 \f
940
941 /* Register that we are able to handle GNU/Linux ELF core file
942    formats.  */
943
944 static struct core_fns linux_elf_core_fns =
945 {
946   bfd_target_elf_flavour,               /* core_flavour */
947   default_check_format,                 /* check_format */
948   default_core_sniffer,                 /* core_sniffer */
949   fetch_core_registers,                 /* core_read_registers */
950   NULL                                  /* next */
951 };
952
953 void
954 _initialize_i386_linux_nat (void)
955 {
956   add_core_fns (&linux_elf_core_fns);
957 }