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