* remote.c (remote_pid_to_str): If printing a process id and we
[binutils-gdb.git] / gdb / i386-linux-nat.c
blob991b27fdd8ade3dfaab9fef61285734f01a8414e
1 /* Native-dependent code for GNU/Linux i386.
3 Copyright (C) 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008,
4 2009 Free Software Foundation, Inc.
6 This file is part of GDB.
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 3 of the License, or
11 (at your option) any later version.
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.
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>. */
21 #include "defs.h"
22 #include "i386-nat.h"
23 #include "inferior.h"
24 #include "gdbcore.h"
25 #include "regcache.h"
26 #include "target.h"
27 #include "linux-nat.h"
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>
35 #ifdef HAVE_SYS_REG_H
36 #include <sys/reg.h>
37 #endif
39 #ifndef ORIG_EAX
40 #define ORIG_EAX -1
41 #endif
43 #ifdef HAVE_SYS_DEBUGREG_H
44 #include <sys/debugreg.h>
45 #endif
47 #ifndef DR_FIRSTADDR
48 #define DR_FIRSTADDR 0
49 #endif
51 #ifndef DR_LASTADDR
52 #define DR_LASTADDR 3
53 #endif
55 #ifndef DR_STATUS
56 #define DR_STATUS 6
57 #endif
59 #ifndef DR_CONTROL
60 #define DR_CONTROL 7
61 #endif
63 /* Prototypes for supply_gregset etc. */
64 #include "gregset.h"
66 #include "i387-tdep.h"
67 #include "i386-tdep.h"
68 #include "i386-linux-tdep.h"
70 /* Defines ps_err_e, struct ps_prochandle. */
71 #include "gdb_proc_service.h"
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.
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. */
86 /* Mapping between the general-purpose registers in `struct user'
87 format and GDB's register array layout. */
88 static int regmap[] =
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
104 /* Which ptrace request retrieves which registers?
105 These apply to the corresponding SET requests as well. */
107 #define GETREGS_SUPPLIES(regno) \
108 ((0 <= (regno) && (regno) <= 15) || (regno) == I386_LINUX_ORIG_EAX_REGNUM)
110 #define GETFPXREGS_SUPPLIES(regno) \
111 (I386_ST0_REGNUM <= (regno) && (regno) < I386_SSE_NUM_REGS)
113 /* Does the current host support the GETREGS request? */
114 int have_ptrace_getregs =
115 #ifdef HAVE_PTRACE_GETREGS
117 #else
119 #endif
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.
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
133 #else
135 #endif
139 /* Accessing registers through the U area, one at a time. */
141 /* Fetch one register. */
143 static void
144 fetch_register (struct regcache *regcache, int regno)
146 int tid;
147 int val;
149 gdb_assert (!have_ptrace_getregs);
150 if (regmap[regno] == -1)
152 regcache_raw_supply (regcache, regno, NULL);
153 return;
156 /* GNU/Linux LWP ID's are process ID's. */
157 tid = TIDGET (inferior_ptid);
158 if (tid == 0)
159 tid = PIDGET (inferior_ptid); /* Not a threaded program. */
161 errno = 0;
162 val = ptrace (PTRACE_PEEKUSER, tid, 4 * regmap[regno], 0);
163 if (errno != 0)
164 error (_("Couldn't read register %s (#%d): %s."),
165 gdbarch_register_name (get_regcache_arch (regcache), regno),
166 regno, safe_strerror (errno));
168 regcache_raw_supply (regcache, regno, &val);
171 /* Store one register. */
173 static void
174 store_register (const struct regcache *regcache, int regno)
176 int tid;
177 int val;
179 gdb_assert (!have_ptrace_getregs);
180 if (regmap[regno] == -1)
181 return;
183 /* GNU/Linux LWP ID's are process ID's. */
184 tid = TIDGET (inferior_ptid);
185 if (tid == 0)
186 tid = PIDGET (inferior_ptid); /* Not a threaded program. */
188 errno = 0;
189 regcache_raw_collect (regcache, regno, &val);
190 ptrace (PTRACE_POKEUSER, tid, 4 * regmap[regno], val);
191 if (errno != 0)
192 error (_("Couldn't write register %s (#%d): %s."),
193 gdbarch_register_name (get_regcache_arch (regcache), regno),
194 regno, safe_strerror (errno));
198 /* Transfering the general-purpose registers between GDB, inferiors
199 and core files. */
201 /* Fill GDB's register array with the general-purpose register values
202 in *GREGSETP. */
204 void
205 supply_gregset (struct regcache *regcache, const elf_gregset_t *gregsetp)
207 const elf_greg_t *regp = (const elf_greg_t *) gregsetp;
208 int i;
210 for (i = 0; i < I386_NUM_GREGS; i++)
211 regcache_raw_supply (regcache, i, regp + regmap[i]);
213 if (I386_LINUX_ORIG_EAX_REGNUM
214 < gdbarch_num_regs (get_regcache_arch (regcache)))
215 regcache_raw_supply (regcache, I386_LINUX_ORIG_EAX_REGNUM,
216 regp + ORIG_EAX);
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. */
223 void
224 fill_gregset (const struct regcache *regcache,
225 elf_gregset_t *gregsetp, int regno)
227 elf_greg_t *regp = (elf_greg_t *) gregsetp;
228 int i;
230 for (i = 0; i < I386_NUM_GREGS; i++)
231 if (regno == -1 || regno == i)
232 regcache_raw_collect (regcache, i, regp + regmap[i]);
234 if ((regno == -1 || regno == I386_LINUX_ORIG_EAX_REGNUM)
235 && I386_LINUX_ORIG_EAX_REGNUM
236 < gdbarch_num_regs (get_regcache_arch (regcache)))
237 regcache_raw_collect (regcache, I386_LINUX_ORIG_EAX_REGNUM,
238 regp + ORIG_EAX);
241 #ifdef HAVE_PTRACE_GETREGS
243 /* Fetch all general-purpose registers from process/thread TID and
244 store their values in GDB's register array. */
246 static void
247 fetch_regs (struct regcache *regcache, int tid)
249 elf_gregset_t regs;
250 elf_gregset_t *regs_p = &regs;
252 if (ptrace (PTRACE_GETREGS, tid, 0, (int) &regs) < 0)
254 if (errno == EIO)
256 /* The kernel we're running on doesn't support the GETREGS
257 request. Reset `have_ptrace_getregs'. */
258 have_ptrace_getregs = 0;
259 return;
262 perror_with_name (_("Couldn't get registers"));
265 supply_gregset (regcache, (const elf_gregset_t *) regs_p);
268 /* Store all valid general-purpose registers in GDB's register array
269 into the process/thread specified by TID. */
271 static void
272 store_regs (const struct regcache *regcache, int tid, int regno)
274 elf_gregset_t regs;
276 if (ptrace (PTRACE_GETREGS, tid, 0, (int) &regs) < 0)
277 perror_with_name (_("Couldn't get registers"));
279 fill_gregset (regcache, &regs, regno);
281 if (ptrace (PTRACE_SETREGS, tid, 0, (int) &regs) < 0)
282 perror_with_name (_("Couldn't write registers"));
285 #else
287 static void fetch_regs (struct regcache *regcache, int tid) {}
288 static void store_regs (const struct regcache *regcache, int tid, int regno) {}
290 #endif
293 /* Transfering floating-point registers between GDB, inferiors and cores. */
295 /* Fill GDB's register array with the floating-point register values in
296 *FPREGSETP. */
298 void
299 supply_fpregset (struct regcache *regcache, const elf_fpregset_t *fpregsetp)
301 i387_supply_fsave (regcache, -1, fpregsetp);
304 /* Fill register REGNO (if it is a floating-point register) in
305 *FPREGSETP with the value in GDB's register array. If REGNO is -1,
306 do this for all registers. */
308 void
309 fill_fpregset (const struct regcache *regcache,
310 elf_fpregset_t *fpregsetp, int regno)
312 i387_collect_fsave (regcache, regno, fpregsetp);
315 #ifdef HAVE_PTRACE_GETREGS
317 /* Fetch all floating-point registers from process/thread TID and store
318 thier values in GDB's register array. */
320 static void
321 fetch_fpregs (struct regcache *regcache, int tid)
323 elf_fpregset_t fpregs;
325 if (ptrace (PTRACE_GETFPREGS, tid, 0, (int) &fpregs) < 0)
326 perror_with_name (_("Couldn't get floating point status"));
328 supply_fpregset (regcache, (const elf_fpregset_t *) &fpregs);
331 /* Store all valid floating-point registers in GDB's register array
332 into the process/thread specified by TID. */
334 static void
335 store_fpregs (const struct regcache *regcache, int tid, int regno)
337 elf_fpregset_t fpregs;
339 if (ptrace (PTRACE_GETFPREGS, tid, 0, (int) &fpregs) < 0)
340 perror_with_name (_("Couldn't get floating point status"));
342 fill_fpregset (regcache, &fpregs, regno);
344 if (ptrace (PTRACE_SETFPREGS, tid, 0, (int) &fpregs) < 0)
345 perror_with_name (_("Couldn't write floating point status"));
348 #else
350 static void fetch_fpregs (struct regcache *regcache, int tid) {}
351 static void store_fpregs (const struct regcache *regcache, int tid, int regno) {}
353 #endif
356 /* Transfering floating-point and SSE registers to and from GDB. */
358 #ifdef HAVE_PTRACE_GETFPXREGS
360 /* Fill GDB's register array with the floating-point and SSE register
361 values in *FPXREGSETP. */
363 void
364 supply_fpxregset (struct regcache *regcache,
365 const elf_fpxregset_t *fpxregsetp)
367 i387_supply_fxsave (regcache, -1, fpxregsetp);
370 /* Fill register REGNO (if it is a floating-point or SSE register) in
371 *FPXREGSETP with the value in GDB's register array. If REGNO is
372 -1, do this for all registers. */
374 void
375 fill_fpxregset (const struct regcache *regcache,
376 elf_fpxregset_t *fpxregsetp, int regno)
378 i387_collect_fxsave (regcache, regno, fpxregsetp);
381 /* Fetch all registers covered by the PTRACE_GETFPXREGS request from
382 process/thread TID and store their values in GDB's register array.
383 Return non-zero if successful, zero otherwise. */
385 static int
386 fetch_fpxregs (struct regcache *regcache, int tid)
388 elf_fpxregset_t fpxregs;
390 if (! have_ptrace_getfpxregs)
391 return 0;
393 if (ptrace (PTRACE_GETFPXREGS, tid, 0, (int) &fpxregs) < 0)
395 if (errno == EIO)
397 have_ptrace_getfpxregs = 0;
398 return 0;
401 perror_with_name (_("Couldn't read floating-point and SSE registers"));
404 supply_fpxregset (regcache, (const elf_fpxregset_t *) &fpxregs);
405 return 1;
408 /* Store all valid registers in GDB's register array covered by the
409 PTRACE_SETFPXREGS request into the process/thread specified by TID.
410 Return non-zero if successful, zero otherwise. */
412 static int
413 store_fpxregs (const struct regcache *regcache, int tid, int regno)
415 elf_fpxregset_t fpxregs;
417 if (! have_ptrace_getfpxregs)
418 return 0;
420 if (ptrace (PTRACE_GETFPXREGS, tid, 0, &fpxregs) == -1)
422 if (errno == EIO)
424 have_ptrace_getfpxregs = 0;
425 return 0;
428 perror_with_name (_("Couldn't read floating-point and SSE registers"));
431 fill_fpxregset (regcache, &fpxregs, regno);
433 if (ptrace (PTRACE_SETFPXREGS, tid, 0, &fpxregs) == -1)
434 perror_with_name (_("Couldn't write floating-point and SSE registers"));
436 return 1;
439 #else
441 static int fetch_fpxregs (struct regcache *regcache, int tid) { return 0; }
442 static int store_fpxregs (const struct regcache *regcache, int tid, int regno) { return 0; }
444 #endif /* HAVE_PTRACE_GETFPXREGS */
447 /* Transferring arbitrary registers between GDB and inferior. */
449 /* Fetch register REGNO from the child process. If REGNO is -1, do
450 this for all registers (including the floating point and SSE
451 registers). */
453 static void
454 i386_linux_fetch_inferior_registers (struct target_ops *ops,
455 struct regcache *regcache, int regno)
457 int tid;
459 /* Use the old method of peeking around in `struct user' if the
460 GETREGS request isn't available. */
461 if (!have_ptrace_getregs)
463 int i;
465 for (i = 0; i < gdbarch_num_regs (get_regcache_arch (regcache)); i++)
466 if (regno == -1 || regno == i)
467 fetch_register (regcache, i);
469 return;
472 /* GNU/Linux LWP ID's are process ID's. */
473 tid = TIDGET (inferior_ptid);
474 if (tid == 0)
475 tid = PIDGET (inferior_ptid); /* Not a threaded program. */
477 /* Use the PTRACE_GETFPXREGS request whenever possible, since it
478 transfers more registers in one system call, and we'll cache the
479 results. But remember that fetch_fpxregs can fail, and return
480 zero. */
481 if (regno == -1)
483 fetch_regs (regcache, tid);
485 /* The call above might reset `have_ptrace_getregs'. */
486 if (!have_ptrace_getregs)
488 i386_linux_fetch_inferior_registers (ops, regcache, regno);
489 return;
492 if (fetch_fpxregs (regcache, tid))
493 return;
494 fetch_fpregs (regcache, tid);
495 return;
498 if (GETREGS_SUPPLIES (regno))
500 fetch_regs (regcache, tid);
501 return;
504 if (GETFPXREGS_SUPPLIES (regno))
506 if (fetch_fpxregs (regcache, tid))
507 return;
509 /* Either our processor or our kernel doesn't support the SSE
510 registers, so read the FP registers in the traditional way,
511 and fill the SSE registers with dummy values. It would be
512 more graceful to handle differences in the register set using
513 gdbarch. Until then, this will at least make things work
514 plausibly. */
515 fetch_fpregs (regcache, tid);
516 return;
519 internal_error (__FILE__, __LINE__,
520 _("Got request for bad register number %d."), regno);
523 /* Store register REGNO back into the child process. If REGNO is -1,
524 do this for all registers (including the floating point and SSE
525 registers). */
526 static void
527 i386_linux_store_inferior_registers (struct target_ops *ops,
528 struct regcache *regcache, int regno)
530 int tid;
532 /* Use the old method of poking around in `struct user' if the
533 SETREGS request isn't available. */
534 if (!have_ptrace_getregs)
536 int i;
538 for (i = 0; i < gdbarch_num_regs (get_regcache_arch (regcache)); i++)
539 if (regno == -1 || regno == i)
540 store_register (regcache, i);
542 return;
545 /* GNU/Linux LWP ID's are process ID's. */
546 tid = TIDGET (inferior_ptid);
547 if (tid == 0)
548 tid = PIDGET (inferior_ptid); /* Not a threaded program. */
550 /* Use the PTRACE_SETFPXREGS requests whenever possible, since it
551 transfers more registers in one system call. But remember that
552 store_fpxregs can fail, and return zero. */
553 if (regno == -1)
555 store_regs (regcache, tid, regno);
556 if (store_fpxregs (regcache, tid, regno))
557 return;
558 store_fpregs (regcache, tid, regno);
559 return;
562 if (GETREGS_SUPPLIES (regno))
564 store_regs (regcache, tid, regno);
565 return;
568 if (GETFPXREGS_SUPPLIES (regno))
570 if (store_fpxregs (regcache, tid, regno))
571 return;
573 /* Either our processor or our kernel doesn't support the SSE
574 registers, so just write the FP registers in the traditional
575 way. */
576 store_fpregs (regcache, tid, regno);
577 return;
580 internal_error (__FILE__, __LINE__,
581 _("Got request to store bad register number %d."), regno);
585 /* Support for debug registers. */
587 static unsigned long i386_linux_dr[DR_CONTROL + 1];
589 static unsigned long
590 i386_linux_dr_get (ptid_t ptid, int regnum)
592 int tid;
593 unsigned long value;
595 tid = TIDGET (ptid);
596 if (tid == 0)
597 tid = PIDGET (ptid);
599 /* FIXME: kettenis/2001-03-27: Calling perror_with_name if the
600 ptrace call fails breaks debugging remote targets. The correct
601 way to fix this is to add the hardware breakpoint and watchpoint
602 stuff to the target vector. For now, just return zero if the
603 ptrace call fails. */
604 errno = 0;
605 value = ptrace (PTRACE_PEEKUSER, tid,
606 offsetof (struct user, u_debugreg[regnum]), 0);
607 if (errno != 0)
608 #if 0
609 perror_with_name (_("Couldn't read debug register"));
610 #else
611 return 0;
612 #endif
614 return value;
617 static void
618 i386_linux_dr_set (ptid_t ptid, int regnum, unsigned long value)
620 int tid;
622 tid = TIDGET (ptid);
623 if (tid == 0)
624 tid = PIDGET (ptid);
626 errno = 0;
627 ptrace (PTRACE_POKEUSER, tid,
628 offsetof (struct user, u_debugreg[regnum]), value);
629 if (errno != 0)
630 perror_with_name (_("Couldn't write debug register"));
633 static void
634 i386_linux_dr_set_control (unsigned long control)
636 struct lwp_info *lp;
637 ptid_t ptid;
639 i386_linux_dr[DR_CONTROL] = control;
640 ALL_LWPS (lp, ptid)
641 i386_linux_dr_set (ptid, DR_CONTROL, control);
644 static void
645 i386_linux_dr_set_addr (int regnum, CORE_ADDR addr)
647 struct lwp_info *lp;
648 ptid_t ptid;
650 gdb_assert (regnum >= 0 && regnum <= DR_LASTADDR - DR_FIRSTADDR);
652 i386_linux_dr[DR_FIRSTADDR + regnum] = addr;
653 ALL_LWPS (lp, ptid)
654 i386_linux_dr_set (ptid, DR_FIRSTADDR + regnum, addr);
657 static void
658 i386_linux_dr_reset_addr (int regnum)
660 i386_linux_dr_set_addr (regnum, 0);
663 static unsigned long
664 i386_linux_dr_get_status (void)
666 return i386_linux_dr_get (inferior_ptid, DR_STATUS);
669 static void
670 i386_linux_new_thread (ptid_t ptid)
672 int i;
674 for (i = DR_FIRSTADDR; i <= DR_LASTADDR; i++)
675 i386_linux_dr_set (ptid, i, i386_linux_dr[i]);
677 i386_linux_dr_set (ptid, DR_CONTROL, i386_linux_dr[DR_CONTROL]);
681 /* Called by libthread_db. Returns a pointer to the thread local
682 storage (or its descriptor). */
684 ps_err_e
685 ps_get_thread_area (const struct ps_prochandle *ph,
686 lwpid_t lwpid, int idx, void **base)
688 /* NOTE: cagney/2003-08-26: The definition of this buffer is found
689 in the kernel header <asm-i386/ldt.h>. It, after padding, is 4 x
690 4 byte integers in size: `entry_number', `base_addr', `limit',
691 and a bunch of status bits.
693 The values returned by this ptrace call should be part of the
694 regcache buffer, and ps_get_thread_area should channel its
695 request through the regcache. That way remote targets could
696 provide the value using the remote protocol and not this direct
697 call.
699 Is this function needed? I'm guessing that the `base' is the
700 address of a a descriptor that libthread_db uses to find the
701 thread local address base that GDB needs. Perhaps that
702 descriptor is defined by the ABI. Anyway, given that
703 libthread_db calls this function without prompting (gdb
704 requesting tls base) I guess it needs info in there anyway. */
705 unsigned int desc[4];
706 gdb_assert (sizeof (int) == 4);
708 #ifndef PTRACE_GET_THREAD_AREA
709 #define PTRACE_GET_THREAD_AREA 25
710 #endif
712 if (ptrace (PTRACE_GET_THREAD_AREA, lwpid,
713 (void *) idx, (unsigned long) &desc) < 0)
714 return PS_ERR;
716 *(int *)base = desc[1];
717 return PS_OK;
721 /* The instruction for a GNU/Linux system call is:
722 int $0x80
723 or 0xcd 0x80. */
725 static const unsigned char linux_syscall[] = { 0xcd, 0x80 };
727 #define LINUX_SYSCALL_LEN (sizeof linux_syscall)
729 /* The system call number is stored in the %eax register. */
730 #define LINUX_SYSCALL_REGNUM I386_EAX_REGNUM
732 /* We are specifically interested in the sigreturn and rt_sigreturn
733 system calls. */
735 #ifndef SYS_sigreturn
736 #define SYS_sigreturn 0x77
737 #endif
738 #ifndef SYS_rt_sigreturn
739 #define SYS_rt_sigreturn 0xad
740 #endif
742 /* Offset to saved processor flags, from <asm/sigcontext.h>. */
743 #define LINUX_SIGCONTEXT_EFLAGS_OFFSET (64)
745 /* Resume execution of the inferior process.
746 If STEP is nonzero, single-step it.
747 If SIGNAL is nonzero, give it that signal. */
749 static void
750 i386_linux_resume (struct target_ops *ops,
751 ptid_t ptid, int step, enum target_signal signal)
753 int pid = PIDGET (ptid);
755 int request = PTRACE_CONT;
757 if (step)
759 struct regcache *regcache = get_thread_regcache (pid_to_ptid (pid));
760 struct gdbarch *gdbarch = get_regcache_arch (regcache);
761 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
762 ULONGEST pc;
763 gdb_byte buf[LINUX_SYSCALL_LEN];
765 request = PTRACE_SINGLESTEP;
767 regcache_cooked_read_unsigned (regcache,
768 gdbarch_pc_regnum (gdbarch), &pc);
770 /* Returning from a signal trampoline is done by calling a
771 special system call (sigreturn or rt_sigreturn, see
772 i386-linux-tdep.c for more information). This system call
773 restores the registers that were saved when the signal was
774 raised, including %eflags. That means that single-stepping
775 won't work. Instead, we'll have to modify the signal context
776 that's about to be restored, and set the trace flag there. */
778 /* First check if PC is at a system call. */
779 if (target_read_memory (pc, buf, LINUX_SYSCALL_LEN) == 0
780 && memcmp (buf, linux_syscall, LINUX_SYSCALL_LEN) == 0)
782 ULONGEST syscall;
783 regcache_cooked_read_unsigned (regcache,
784 LINUX_SYSCALL_REGNUM, &syscall);
786 /* Then check the system call number. */
787 if (syscall == SYS_sigreturn || syscall == SYS_rt_sigreturn)
789 ULONGEST sp, addr;
790 unsigned long int eflags;
792 regcache_cooked_read_unsigned (regcache, I386_ESP_REGNUM, &sp);
793 if (syscall == SYS_rt_sigreturn)
794 addr = read_memory_integer (sp + 8, 4, byte_order) + 20;
795 else
796 addr = sp;
798 /* Set the trace flag in the context that's about to be
799 restored. */
800 addr += LINUX_SIGCONTEXT_EFLAGS_OFFSET;
801 read_memory (addr, (gdb_byte *) &eflags, 4);
802 eflags |= 0x0100;
803 write_memory (addr, (gdb_byte *) &eflags, 4);
808 if (ptrace (request, pid, 0, target_signal_to_host (signal)) == -1)
809 perror_with_name (("ptrace"));
812 static void (*super_post_startup_inferior) (ptid_t ptid);
814 static void
815 i386_linux_child_post_startup_inferior (ptid_t ptid)
817 i386_cleanup_dregs ();
818 super_post_startup_inferior (ptid);
821 void
822 _initialize_i386_linux_nat (void)
824 struct target_ops *t;
826 /* Fill in the generic GNU/Linux methods. */
827 t = linux_target ();
829 i386_use_watchpoints (t);
831 i386_dr_low.set_control = i386_linux_dr_set_control;
832 i386_dr_low.set_addr = i386_linux_dr_set_addr;
833 i386_dr_low.reset_addr = i386_linux_dr_reset_addr;
834 i386_dr_low.get_status = i386_linux_dr_get_status;
835 i386_set_debug_register_length (4);
837 /* Override the default ptrace resume method. */
838 t->to_resume = i386_linux_resume;
840 /* Override the GNU/Linux inferior startup hook. */
841 super_post_startup_inferior = t->to_post_startup_inferior;
842 t->to_post_startup_inferior = i386_linux_child_post_startup_inferior;
844 /* Add our register access methods. */
845 t->to_fetch_registers = i386_linux_fetch_inferior_registers;
846 t->to_store_registers = i386_linux_store_inferior_registers;
848 /* Register the target. */
849 linux_nat_add_target (t);
850 linux_nat_set_new_thread (t, i386_linux_new_thread);