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