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