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