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