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