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