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