* linux-low.c (regsets_fetch_inferior_registers): Fix memory leak.
[gdb/SamB.git] / gdb / i386-linux-nat.c
blob2366474657ac75a6bc9661d8da486550dc2aaff6
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 "inferior.h"
23 #include "gdbcore.h"
24 #include "regcache.h"
25 #include "target.h"
26 #include "linux-nat.h"
28 #include "gdb_assert.h"
29 #include "gdb_string.h"
30 #include <sys/ptrace.h>
31 #include <sys/user.h>
32 #include <sys/procfs.h>
34 #ifdef HAVE_SYS_REG_H
35 #include <sys/reg.h>
36 #endif
38 #ifndef ORIG_EAX
39 #define ORIG_EAX -1
40 #endif
42 #ifdef HAVE_SYS_DEBUGREG_H
43 #include <sys/debugreg.h>
44 #endif
46 #ifndef DR_FIRSTADDR
47 #define DR_FIRSTADDR 0
48 #endif
50 #ifndef DR_LASTADDR
51 #define DR_LASTADDR 3
52 #endif
54 #ifndef DR_STATUS
55 #define DR_STATUS 6
56 #endif
58 #ifndef DR_CONTROL
59 #define DR_CONTROL 7
60 #endif
62 /* Prototypes for supply_gregset etc. */
63 #include "gregset.h"
65 #include "i387-tdep.h"
66 #include "i386-tdep.h"
67 #include "i386-linux-tdep.h"
69 /* Defines ps_err_e, struct ps_prochandle. */
70 #include "gdb_proc_service.h"
73 /* The register sets used in GNU/Linux ELF core-dumps are identical to
74 the register sets in `struct user' that is used for a.out
75 core-dumps, and is also used by `ptrace'. The corresponding types
76 are `elf_gregset_t' for the general-purpose registers (with
77 `elf_greg_t' the type of a single GP register) and `elf_fpregset_t'
78 for the floating-point registers.
80 Those types used to be available under the names `gregset_t' and
81 `fpregset_t' too, and this file used those names in the past. But
82 those names are now used for the register sets used in the
83 `mcontext_t' type, and have a different size and layout. */
85 /* Mapping between the general-purpose registers in `struct user'
86 format and GDB's register array layout. */
87 static int regmap[] =
89 EAX, ECX, EDX, EBX,
90 UESP, EBP, ESI, EDI,
91 EIP, EFL, CS, SS,
92 DS, ES, FS, GS,
93 -1, -1, -1, -1, /* st0, st1, st2, st3 */
94 -1, -1, -1, -1, /* st4, st5, st6, st7 */
95 -1, -1, -1, -1, /* fctrl, fstat, ftag, fiseg */
96 -1, -1, -1, -1, /* fioff, foseg, fooff, fop */
97 -1, -1, -1, -1, /* xmm0, xmm1, xmm2, xmm3 */
98 -1, -1, -1, -1, /* xmm4, xmm5, xmm6, xmm6 */
99 -1, /* mxcsr */
100 ORIG_EAX
103 /* Which ptrace request retrieves which registers?
104 These apply to the corresponding SET requests as well. */
106 #define GETREGS_SUPPLIES(regno) \
107 ((0 <= (regno) && (regno) <= 15) || (regno) == I386_LINUX_ORIG_EAX_REGNUM)
109 #define GETFPXREGS_SUPPLIES(regno) \
110 (I386_ST0_REGNUM <= (regno) && (regno) < I386_SSE_NUM_REGS)
112 /* Does the current host support the GETREGS request? */
113 int have_ptrace_getregs =
114 #ifdef HAVE_PTRACE_GETREGS
116 #else
118 #endif
121 /* Does the current host support the GETFPXREGS request? The header
122 file may or may not define it, and even if it is defined, the
123 kernel will return EIO if it's running on a pre-SSE processor.
125 My instinct is to attach this to some architecture- or
126 target-specific data structure, but really, a particular GDB
127 process can only run on top of one kernel at a time. So it's okay
128 for this to be a simple variable. */
129 int have_ptrace_getfpxregs =
130 #ifdef HAVE_PTRACE_GETFPXREGS
132 #else
134 #endif
138 /* Accessing registers through the U area, one at a time. */
140 /* Fetch one register. */
142 static void
143 fetch_register (struct regcache *regcache, int regno)
145 int tid;
146 int val;
148 gdb_assert (!have_ptrace_getregs);
149 if (regmap[regno] == -1)
151 regcache_raw_supply (regcache, regno, NULL);
152 return;
155 /* GNU/Linux LWP ID's are process ID's. */
156 tid = TIDGET (inferior_ptid);
157 if (tid == 0)
158 tid = PIDGET (inferior_ptid); /* Not a threaded program. */
160 errno = 0;
161 val = ptrace (PTRACE_PEEKUSER, tid, 4 * regmap[regno], 0);
162 if (errno != 0)
163 error (_("Couldn't read register %s (#%d): %s."),
164 gdbarch_register_name (get_regcache_arch (regcache), regno),
165 regno, safe_strerror (errno));
167 regcache_raw_supply (regcache, regno, &val);
170 /* Store one register. */
172 static void
173 store_register (const struct regcache *regcache, int regno)
175 int tid;
176 int val;
178 gdb_assert (!have_ptrace_getregs);
179 if (regmap[regno] == -1)
180 return;
182 /* GNU/Linux LWP ID's are process ID's. */
183 tid = TIDGET (inferior_ptid);
184 if (tid == 0)
185 tid = PIDGET (inferior_ptid); /* Not a threaded program. */
187 errno = 0;
188 regcache_raw_collect (regcache, regno, &val);
189 ptrace (PTRACE_POKEUSER, tid, 4 * regmap[regno], val);
190 if (errno != 0)
191 error (_("Couldn't write register %s (#%d): %s."),
192 gdbarch_register_name (get_regcache_arch (regcache), regno),
193 regno, safe_strerror (errno));
197 /* Transfering the general-purpose registers between GDB, inferiors
198 and core files. */
200 /* Fill GDB's register array with the general-purpose register values
201 in *GREGSETP. */
203 void
204 supply_gregset (struct regcache *regcache, const elf_gregset_t *gregsetp)
206 const elf_greg_t *regp = (const elf_greg_t *) gregsetp;
207 int i;
209 for (i = 0; i < I386_NUM_GREGS; i++)
210 regcache_raw_supply (regcache, i, regp + regmap[i]);
212 if (I386_LINUX_ORIG_EAX_REGNUM
213 < gdbarch_num_regs (get_regcache_arch (regcache)))
214 regcache_raw_supply (regcache, I386_LINUX_ORIG_EAX_REGNUM,
215 regp + ORIG_EAX);
218 /* Fill register REGNO (if it is a general-purpose register) in
219 *GREGSETPS with the value in GDB's register array. If REGNO is -1,
220 do this for all registers. */
222 void
223 fill_gregset (const struct regcache *regcache,
224 elf_gregset_t *gregsetp, int regno)
226 elf_greg_t *regp = (elf_greg_t *) gregsetp;
227 int i;
229 for (i = 0; i < I386_NUM_GREGS; i++)
230 if (regno == -1 || regno == i)
231 regcache_raw_collect (regcache, i, regp + regmap[i]);
233 if ((regno == -1 || regno == I386_LINUX_ORIG_EAX_REGNUM)
234 && I386_LINUX_ORIG_EAX_REGNUM
235 < gdbarch_num_regs (get_regcache_arch (regcache)))
236 regcache_raw_collect (regcache, I386_LINUX_ORIG_EAX_REGNUM,
237 regp + ORIG_EAX);
240 #ifdef HAVE_PTRACE_GETREGS
242 /* Fetch all general-purpose registers from process/thread TID and
243 store their values in GDB's register array. */
245 static void
246 fetch_regs (struct regcache *regcache, int tid)
248 elf_gregset_t regs;
249 elf_gregset_t *regs_p = &regs;
251 if (ptrace (PTRACE_GETREGS, tid, 0, (int) &regs) < 0)
253 if (errno == EIO)
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;
261 perror_with_name (_("Couldn't get registers"));
264 supply_gregset (regcache, (const elf_gregset_t *) regs_p);
267 /* Store all valid general-purpose registers in GDB's register array
268 into the process/thread specified by TID. */
270 static void
271 store_regs (const struct regcache *regcache, int tid, int regno)
273 elf_gregset_t regs;
275 if (ptrace (PTRACE_GETREGS, tid, 0, (int) &regs) < 0)
276 perror_with_name (_("Couldn't get registers"));
278 fill_gregset (regcache, &regs, regno);
280 if (ptrace (PTRACE_SETREGS, tid, 0, (int) &regs) < 0)
281 perror_with_name (_("Couldn't write registers"));
284 #else
286 static void fetch_regs (struct regcache *regcache, int tid) {}
287 static void store_regs (const struct regcache *regcache, int tid, int regno) {}
289 #endif
292 /* Transfering floating-point registers between GDB, inferiors and cores. */
294 /* Fill GDB's register array with the floating-point register values in
295 *FPREGSETP. */
297 void
298 supply_fpregset (struct regcache *regcache, const elf_fpregset_t *fpregsetp)
300 i387_supply_fsave (regcache, -1, fpregsetp);
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. */
307 void
308 fill_fpregset (const struct regcache *regcache,
309 elf_fpregset_t *fpregsetp, int regno)
311 i387_collect_fsave (regcache, regno, fpregsetp);
314 #ifdef HAVE_PTRACE_GETREGS
316 /* Fetch all floating-point registers from process/thread TID and store
317 thier values in GDB's register array. */
319 static void
320 fetch_fpregs (struct regcache *regcache, int tid)
322 elf_fpregset_t fpregs;
324 if (ptrace (PTRACE_GETFPREGS, tid, 0, (int) &fpregs) < 0)
325 perror_with_name (_("Couldn't get floating point status"));
327 supply_fpregset (regcache, (const elf_fpregset_t *) &fpregs);
330 /* Store all valid floating-point registers in GDB's register array
331 into the process/thread specified by TID. */
333 static void
334 store_fpregs (const struct regcache *regcache, int tid, int regno)
336 elf_fpregset_t fpregs;
338 if (ptrace (PTRACE_GETFPREGS, tid, 0, (int) &fpregs) < 0)
339 perror_with_name (_("Couldn't get floating point status"));
341 fill_fpregset (regcache, &fpregs, regno);
343 if (ptrace (PTRACE_SETFPREGS, tid, 0, (int) &fpregs) < 0)
344 perror_with_name (_("Couldn't write floating point status"));
347 #else
349 static void fetch_fpregs (struct regcache *regcache, int tid) {}
350 static void store_fpregs (const struct regcache *regcache, int tid, int regno) {}
352 #endif
355 /* Transfering floating-point and SSE registers to and from GDB. */
357 #ifdef HAVE_PTRACE_GETFPXREGS
359 /* Fill GDB's register array with the floating-point and SSE register
360 values in *FPXREGSETP. */
362 void
363 supply_fpxregset (struct regcache *regcache,
364 const elf_fpxregset_t *fpxregsetp)
366 i387_supply_fxsave (regcache, -1, fpxregsetp);
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. */
373 void
374 fill_fpxregset (const struct regcache *regcache,
375 elf_fpxregset_t *fpxregsetp, int regno)
377 i387_collect_fxsave (regcache, regno, fpxregsetp);
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. */
384 static int
385 fetch_fpxregs (struct regcache *regcache, int tid)
387 elf_fpxregset_t fpxregs;
389 if (! have_ptrace_getfpxregs)
390 return 0;
392 if (ptrace (PTRACE_GETFPXREGS, tid, 0, (int) &fpxregs) < 0)
394 if (errno == EIO)
396 have_ptrace_getfpxregs = 0;
397 return 0;
400 perror_with_name (_("Couldn't read floating-point and SSE registers"));
403 supply_fpxregset (regcache, (const elf_fpxregset_t *) &fpxregs);
404 return 1;
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. */
411 static int
412 store_fpxregs (const struct regcache *regcache, int tid, int regno)
414 elf_fpxregset_t fpxregs;
416 if (! have_ptrace_getfpxregs)
417 return 0;
419 if (ptrace (PTRACE_GETFPXREGS, tid, 0, &fpxregs) == -1)
421 if (errno == EIO)
423 have_ptrace_getfpxregs = 0;
424 return 0;
427 perror_with_name (_("Couldn't read floating-point and SSE registers"));
430 fill_fpxregset (regcache, &fpxregs, regno);
432 if (ptrace (PTRACE_SETFPXREGS, tid, 0, &fpxregs) == -1)
433 perror_with_name (_("Couldn't write floating-point and SSE registers"));
435 return 1;
438 #else
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; }
443 #endif /* HAVE_PTRACE_GETFPXREGS */
446 /* Transferring arbitrary registers between GDB and inferior. */
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). */
452 static void
453 i386_linux_fetch_inferior_registers (struct target_ops *ops,
454 struct regcache *regcache, int regno)
456 int tid;
458 /* Use the old method of peeking around in `struct user' if the
459 GETREGS request isn't available. */
460 if (!have_ptrace_getregs)
462 int i;
464 for (i = 0; i < gdbarch_num_regs (get_regcache_arch (regcache)); i++)
465 if (regno == -1 || regno == i)
466 fetch_register (regcache, i);
468 return;
471 /* GNU/Linux LWP ID's are process ID's. */
472 tid = TIDGET (inferior_ptid);
473 if (tid == 0)
474 tid = PIDGET (inferior_ptid); /* Not a threaded program. */
476 /* Use the PTRACE_GETFPXREGS request whenever possible, since it
477 transfers more registers in one system call, and we'll cache the
478 results. But remember that fetch_fpxregs can fail, and return
479 zero. */
480 if (regno == -1)
482 fetch_regs (regcache, tid);
484 /* The call above might reset `have_ptrace_getregs'. */
485 if (!have_ptrace_getregs)
487 i386_linux_fetch_inferior_registers (ops, regcache, regno);
488 return;
491 if (fetch_fpxregs (regcache, tid))
492 return;
493 fetch_fpregs (regcache, tid);
494 return;
497 if (GETREGS_SUPPLIES (regno))
499 fetch_regs (regcache, tid);
500 return;
503 if (GETFPXREGS_SUPPLIES (regno))
505 if (fetch_fpxregs (regcache, tid))
506 return;
508 /* Either our processor or our kernel doesn't support the SSE
509 registers, so read the FP registers in the traditional way,
510 and fill the SSE registers with dummy values. It would be
511 more graceful to handle differences in the register set using
512 gdbarch. Until then, this will at least make things work
513 plausibly. */
514 fetch_fpregs (regcache, tid);
515 return;
518 internal_error (__FILE__, __LINE__,
519 _("Got request for bad register number %d."), regno);
522 /* Store register REGNO back into the child process. If REGNO is -1,
523 do this for all registers (including the floating point and SSE
524 registers). */
525 static void
526 i386_linux_store_inferior_registers (struct target_ops *ops,
527 struct regcache *regcache, int regno)
529 int tid;
531 /* Use the old method of poking around in `struct user' if the
532 SETREGS request isn't available. */
533 if (!have_ptrace_getregs)
535 int i;
537 for (i = 0; i < gdbarch_num_regs (get_regcache_arch (regcache)); i++)
538 if (regno == -1 || regno == i)
539 store_register (regcache, i);
541 return;
544 /* GNU/Linux LWP ID's are process ID's. */
545 tid = TIDGET (inferior_ptid);
546 if (tid == 0)
547 tid = PIDGET (inferior_ptid); /* Not a threaded program. */
549 /* Use the PTRACE_SETFPXREGS requests whenever possible, since it
550 transfers more registers in one system call. But remember that
551 store_fpxregs can fail, and return zero. */
552 if (regno == -1)
554 store_regs (regcache, tid, regno);
555 if (store_fpxregs (regcache, tid, regno))
556 return;
557 store_fpregs (regcache, tid, regno);
558 return;
561 if (GETREGS_SUPPLIES (regno))
563 store_regs (regcache, tid, regno);
564 return;
567 if (GETFPXREGS_SUPPLIES (regno))
569 if (store_fpxregs (regcache, tid, regno))
570 return;
572 /* Either our processor or our kernel doesn't support the SSE
573 registers, so just write the FP registers in the traditional
574 way. */
575 store_fpregs (regcache, tid, regno);
576 return;
579 internal_error (__FILE__, __LINE__,
580 _("Got request to store bad register number %d."), regno);
584 /* Support for debug registers. */
586 static unsigned long i386_linux_dr[DR_CONTROL + 1];
588 static unsigned long
589 i386_linux_dr_get (ptid_t ptid, int regnum)
591 int tid;
592 unsigned long value;
594 tid = TIDGET (ptid);
595 if (tid == 0)
596 tid = PIDGET (ptid);
598 /* FIXME: kettenis/2001-03-27: Calling perror_with_name if the
599 ptrace call fails breaks debugging remote targets. The correct
600 way to fix this is to add the hardware breakpoint and watchpoint
601 stuff to the target vector. For now, just return zero if the
602 ptrace call fails. */
603 errno = 0;
604 value = ptrace (PTRACE_PEEKUSER, tid,
605 offsetof (struct user, u_debugreg[regnum]), 0);
606 if (errno != 0)
607 #if 0
608 perror_with_name (_("Couldn't read debug register"));
609 #else
610 return 0;
611 #endif
613 return value;
616 static void
617 i386_linux_dr_set (ptid_t ptid, int regnum, unsigned long value)
619 int tid;
621 tid = TIDGET (ptid);
622 if (tid == 0)
623 tid = PIDGET (ptid);
625 errno = 0;
626 ptrace (PTRACE_POKEUSER, tid,
627 offsetof (struct user, u_debugreg[regnum]), value);
628 if (errno != 0)
629 perror_with_name (_("Couldn't write debug register"));
632 void
633 i386_linux_dr_set_control (unsigned long control)
635 struct lwp_info *lp;
636 ptid_t ptid;
638 i386_linux_dr[DR_CONTROL] = control;
639 ALL_LWPS (lp, ptid)
640 i386_linux_dr_set (ptid, DR_CONTROL, control);
643 void
644 i386_linux_dr_set_addr (int regnum, CORE_ADDR addr)
646 struct lwp_info *lp;
647 ptid_t ptid;
649 gdb_assert (regnum >= 0 && regnum <= DR_LASTADDR - DR_FIRSTADDR);
651 i386_linux_dr[DR_FIRSTADDR + regnum] = addr;
652 ALL_LWPS (lp, ptid)
653 i386_linux_dr_set (ptid, DR_FIRSTADDR + regnum, addr);
656 void
657 i386_linux_dr_reset_addr (int regnum)
659 i386_linux_dr_set_addr (regnum, 0);
662 unsigned long
663 i386_linux_dr_get_status (void)
665 return i386_linux_dr_get (inferior_ptid, DR_STATUS);
668 static void
669 i386_linux_new_thread (ptid_t ptid)
671 int i;
673 for (i = DR_FIRSTADDR; i <= DR_LASTADDR; i++)
674 i386_linux_dr_set (ptid, i, i386_linux_dr[i]);
676 i386_linux_dr_set (ptid, DR_CONTROL, i386_linux_dr[DR_CONTROL]);
680 /* Called by libthread_db. Returns a pointer to the thread local
681 storage (or its descriptor). */
683 ps_err_e
684 ps_get_thread_area (const struct ps_prochandle *ph,
685 lwpid_t lwpid, int idx, void **base)
687 /* NOTE: cagney/2003-08-26: The definition of this buffer is found
688 in the kernel header <asm-i386/ldt.h>. It, after padding, is 4 x
689 4 byte integers in size: `entry_number', `base_addr', `limit',
690 and a bunch of status bits.
692 The values returned by this ptrace call should be part of the
693 regcache buffer, and ps_get_thread_area should channel its
694 request through the regcache. That way remote targets could
695 provide the value using the remote protocol and not this direct
696 call.
698 Is this function needed? I'm guessing that the `base' is the
699 address of a a descriptor that libthread_db uses to find the
700 thread local address base that GDB needs. Perhaps that
701 descriptor is defined by the ABI. Anyway, given that
702 libthread_db calls this function without prompting (gdb
703 requesting tls base) I guess it needs info in there anyway. */
704 unsigned int desc[4];
705 gdb_assert (sizeof (int) == 4);
707 #ifndef PTRACE_GET_THREAD_AREA
708 #define PTRACE_GET_THREAD_AREA 25
709 #endif
711 if (ptrace (PTRACE_GET_THREAD_AREA, lwpid,
712 (void *) idx, (unsigned long) &desc) < 0)
713 return PS_ERR;
715 *(int *)base = desc[1];
716 return PS_OK;
720 /* The instruction for a GNU/Linux system call is:
721 int $0x80
722 or 0xcd 0x80. */
724 static const unsigned char linux_syscall[] = { 0xcd, 0x80 };
726 #define LINUX_SYSCALL_LEN (sizeof linux_syscall)
728 /* The system call number is stored in the %eax register. */
729 #define LINUX_SYSCALL_REGNUM I386_EAX_REGNUM
731 /* We are specifically interested in the sigreturn and rt_sigreturn
732 system calls. */
734 #ifndef SYS_sigreturn
735 #define SYS_sigreturn 0x77
736 #endif
737 #ifndef SYS_rt_sigreturn
738 #define SYS_rt_sigreturn 0xad
739 #endif
741 /* Offset to saved processor flags, from <asm/sigcontext.h>. */
742 #define LINUX_SIGCONTEXT_EFLAGS_OFFSET (64)
744 /* Resume execution of the inferior process.
745 If STEP is nonzero, single-step it.
746 If SIGNAL is nonzero, give it that signal. */
748 static void
749 i386_linux_resume (struct target_ops *ops,
750 ptid_t ptid, int step, enum target_signal signal)
752 int pid = PIDGET (ptid);
754 int request = PTRACE_CONT;
756 if (step)
758 struct regcache *regcache = get_thread_regcache (pid_to_ptid (pid));
759 ULONGEST pc;
760 gdb_byte buf[LINUX_SYSCALL_LEN];
762 request = PTRACE_SINGLESTEP;
764 regcache_cooked_read_unsigned
765 (regcache, gdbarch_pc_regnum (get_regcache_arch (regcache)), &pc);
767 /* Returning from a signal trampoline is done by calling a
768 special system call (sigreturn or rt_sigreturn, see
769 i386-linux-tdep.c for more information). This system call
770 restores the registers that were saved when the signal was
771 raised, including %eflags. That means that single-stepping
772 won't work. Instead, we'll have to modify the signal context
773 that's about to be restored, and set the trace flag there. */
775 /* First check if PC is at a system call. */
776 if (target_read_memory (pc, buf, LINUX_SYSCALL_LEN) == 0
777 && memcmp (buf, linux_syscall, LINUX_SYSCALL_LEN) == 0)
779 ULONGEST syscall;
780 regcache_cooked_read_unsigned (regcache,
781 LINUX_SYSCALL_REGNUM, &syscall);
783 /* Then check the system call number. */
784 if (syscall == SYS_sigreturn || syscall == SYS_rt_sigreturn)
786 ULONGEST sp, addr;
787 unsigned long int eflags;
789 regcache_cooked_read_unsigned (regcache, I386_ESP_REGNUM, &sp);
790 if (syscall == SYS_rt_sigreturn)
791 addr = read_memory_integer (sp + 8, 4) + 20;
792 else
793 addr = sp;
795 /* Set the trace flag in the context that's about to be
796 restored. */
797 addr += LINUX_SIGCONTEXT_EFLAGS_OFFSET;
798 read_memory (addr, (gdb_byte *) &eflags, 4);
799 eflags |= 0x0100;
800 write_memory (addr, (gdb_byte *) &eflags, 4);
805 if (ptrace (request, pid, 0, target_signal_to_host (signal)) == -1)
806 perror_with_name (("ptrace"));
809 static void (*super_post_startup_inferior) (ptid_t ptid);
811 static void
812 i386_linux_child_post_startup_inferior (ptid_t ptid)
814 i386_cleanup_dregs ();
815 super_post_startup_inferior (ptid);
818 void
819 _initialize_i386_linux_nat (void)
821 struct target_ops *t;
823 /* Fill in the generic GNU/Linux methods. */
824 t = linux_target ();
826 i386_use_watchpoints (t);
828 /* Override the default ptrace resume method. */
829 t->to_resume = i386_linux_resume;
831 /* Override the GNU/Linux inferior startup hook. */
832 super_post_startup_inferior = t->to_post_startup_inferior;
833 t->to_post_startup_inferior = i386_linux_child_post_startup_inferior;
835 /* Add our register access methods. */
836 t->to_fetch_registers = i386_linux_fetch_inferior_registers;
837 t->to_store_registers = i386_linux_store_inferior_registers;
839 /* Register the target. */
840 linux_nat_add_target (t);
841 linux_nat_set_new_thread (t, i386_linux_new_thread);