2003-04-08 Elena Zannoni <ezannoni@redhat.com>
[deliverable/binutils-gdb.git] / gdb / i386-linux-nat.c
1 /* Native-dependent code for GNU/Linux x86.
2
3 Copyright 1999, 2000, 2001, 2002 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
27 #include "gdb_assert.h"
28 #include "gdb_string.h"
29 #include <sys/ptrace.h>
30 #include <sys/user.h>
31 #include <sys/procfs.h>
32
33 #ifdef HAVE_SYS_REG_H
34 #include <sys/reg.h>
35 #endif
36
37 #ifndef ORIG_EAX
38 #define ORIG_EAX -1
39 #endif
40
41 #ifdef HAVE_SYS_DEBUGREG_H
42 #include <sys/debugreg.h>
43 #endif
44
45 #ifndef DR_FIRSTADDR
46 #define DR_FIRSTADDR 0
47 #endif
48
49 #ifndef DR_LASTADDR
50 #define DR_LASTADDR 3
51 #endif
52
53 #ifndef DR_STATUS
54 #define DR_STATUS 6
55 #endif
56
57 #ifndef DR_CONTROL
58 #define DR_CONTROL 7
59 #endif
60
61 /* Prototypes for supply_gregset etc. */
62 #include "gregset.h"
63
64 /* Prototypes for i387_supply_fsave etc. */
65 #include "i387-tdep.h"
66
67 /* Defines for XMM0_REGNUM etc. */
68 #include "i386-tdep.h"
69
70 /* Defines I386_LINUX_ORIG_EAX_REGNUM. */
71 #include "i386-linux-tdep.h"
72
73 /* Prototypes for local functions. */
74 static void dummy_sse_values (void);
75
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 #define GETREGS_SUPPLIES(regno) \
111 ((0 <= (regno) && (regno) <= 15) || (regno) == I386_LINUX_ORIG_EAX_REGNUM)
112 #define GETFPREGS_SUPPLIES(regno) \
113 (FP0_REGNUM <= (regno) && (regno) <= LAST_FPU_CTRL_REGNUM)
114 #define GETFPXREGS_SUPPLIES(regno) \
115 (FP0_REGNUM <= (regno) && (regno) <= MXCSR_REGNUM)
116
117 /* Does the current host support the GETREGS request? */
118 int have_ptrace_getregs =
119 #ifdef HAVE_PTRACE_GETREGS
120 1
121 #else
122 0
123 #endif
124 ;
125
126 /* Does the current host support the GETFPXREGS request? The header
127 file may or may not define it, and even if it is defined, the
128 kernel will return EIO if it's running on a pre-SSE processor.
129
130 My instinct is to attach this to some architecture- or
131 target-specific data structure, but really, a particular GDB
132 process can only run on top of one kernel at a time. So it's okay
133 for this to be a simple variable. */
134 int have_ptrace_getfpxregs =
135 #ifdef HAVE_PTRACE_GETFPXREGS
136 1
137 #else
138 0
139 #endif
140 ;
141 \f
142
143 /* Support for the user struct. */
144
145 /* Return the address of register REGNUM. BLOCKEND is the value of
146 u.u_ar0, which should point to the registers. */
147
148 CORE_ADDR
149 register_u_addr (CORE_ADDR blockend, int regnum)
150 {
151 return (blockend + 4 * regmap[regnum]);
152 }
153
154 /* Return the size of the user struct. */
155
156 int
157 kernel_u_size (void)
158 {
159 return (sizeof (struct user));
160 }
161 \f
162
163 /* Accessing registers through the U area, one at a time. */
164
165 /* Fetch one register. */
166
167 static void
168 fetch_register (int regno)
169 {
170 int tid;
171 int val;
172
173 gdb_assert (!have_ptrace_getregs);
174 if (cannot_fetch_register (regno))
175 {
176 supply_register (regno, NULL);
177 return;
178 }
179
180 /* GNU/Linux LWP ID's are process ID's. */
181 if ((tid = TIDGET (inferior_ptid)) == 0)
182 tid = PIDGET (inferior_ptid); /* Not a threaded program. */
183
184 errno = 0;
185 val = ptrace (PTRACE_PEEKUSER, tid, register_addr (regno, 0), 0);
186 if (errno != 0)
187 error ("Couldn't read register %s (#%d): %s.", REGISTER_NAME (regno),
188 regno, safe_strerror (errno));
189
190 supply_register (regno, &val);
191 }
192
193 /* Store one register. */
194
195 static void
196 store_register (int regno)
197 {
198 int tid;
199 int val;
200
201 gdb_assert (!have_ptrace_getregs);
202 if (cannot_store_register (regno))
203 return;
204
205 /* GNU/Linux LWP ID's are process ID's. */
206 if ((tid = TIDGET (inferior_ptid)) == 0)
207 tid = PIDGET (inferior_ptid); /* Not a threaded program. */
208
209 errno = 0;
210 regcache_collect (regno, &val);
211 ptrace (PTRACE_POKEUSER, tid, register_addr (regno, 0), val);
212 if (errno != 0)
213 error ("Couldn't read register %s (#%d): %s.", REGISTER_NAME (regno),
214 regno, safe_strerror (errno));
215 }
216 \f
217
218 /* Transfering the general-purpose registers between GDB, inferiors
219 and core files. */
220
221 /* Fill GDB's register array with the general-purpose register values
222 in *GREGSETP. */
223
224 void
225 supply_gregset (elf_gregset_t *gregsetp)
226 {
227 elf_greg_t *regp = (elf_greg_t *) gregsetp;
228 int i;
229
230 for (i = 0; i < I386_NUM_GREGS; i++)
231 supply_register (i, (char *) (regp + regmap[i]));
232
233 if (I386_LINUX_ORIG_EAX_REGNUM < NUM_REGS)
234 supply_register (I386_LINUX_ORIG_EAX_REGNUM, (char *) (regp + ORIG_EAX));
235 }
236
237 /* Fill register REGNO (if it is a general-purpose register) in
238 *GREGSETPS with the value in GDB's register array. If REGNO is -1,
239 do this for all registers. */
240
241 void
242 fill_gregset (elf_gregset_t *gregsetp, int regno)
243 {
244 elf_greg_t *regp = (elf_greg_t *) gregsetp;
245 int i;
246
247 for (i = 0; i < I386_NUM_GREGS; i++)
248 if (regno == -1 || regno == i)
249 regcache_collect (i, regp + regmap[i]);
250
251 if ((regno == -1 || regno == I386_LINUX_ORIG_EAX_REGNUM)
252 && I386_LINUX_ORIG_EAX_REGNUM < NUM_REGS)
253 regcache_collect (I386_LINUX_ORIG_EAX_REGNUM, regp + ORIG_EAX);
254 }
255
256 #ifdef HAVE_PTRACE_GETREGS
257
258 /* Fetch all general-purpose registers from process/thread TID and
259 store their values in GDB's register array. */
260
261 static void
262 fetch_regs (int tid)
263 {
264 elf_gregset_t regs;
265
266 if (ptrace (PTRACE_GETREGS, tid, 0, (int) &regs) < 0)
267 {
268 if (errno == EIO)
269 {
270 /* The kernel we're running on doesn't support the GETREGS
271 request. Reset `have_ptrace_getregs'. */
272 have_ptrace_getregs = 0;
273 return;
274 }
275
276 perror_with_name ("Couldn't get registers");
277 }
278
279 supply_gregset (&regs);
280 }
281
282 /* Store all valid general-purpose registers in GDB's register array
283 into the process/thread specified by TID. */
284
285 static void
286 store_regs (int tid, int regno)
287 {
288 elf_gregset_t regs;
289
290 if (ptrace (PTRACE_GETREGS, tid, 0, (int) &regs) < 0)
291 perror_with_name ("Couldn't get registers");
292
293 fill_gregset (&regs, regno);
294
295 if (ptrace (PTRACE_SETREGS, tid, 0, (int) &regs) < 0)
296 perror_with_name ("Couldn't write registers");
297 }
298
299 #else
300
301 static void fetch_regs (int tid) {}
302 static void store_regs (int tid, int regno) {}
303
304 #endif
305 \f
306
307 /* Transfering floating-point registers between GDB, inferiors and cores. */
308
309 /* Fill GDB's register array with the floating-point register values in
310 *FPREGSETP. */
311
312 void
313 supply_fpregset (elf_fpregset_t *fpregsetp)
314 {
315 i387_supply_fsave ((char *) fpregsetp);
316 dummy_sse_values ();
317 }
318
319 /* Fill register REGNO (if it is a floating-point register) in
320 *FPREGSETP with the value in GDB's register array. If REGNO is -1,
321 do this for all registers. */
322
323 void
324 fill_fpregset (elf_fpregset_t *fpregsetp, int regno)
325 {
326 i387_fill_fsave ((char *) fpregsetp, regno);
327 }
328
329 #ifdef HAVE_PTRACE_GETREGS
330
331 /* Fetch all floating-point registers from process/thread TID and store
332 thier values in GDB's register array. */
333
334 static void
335 fetch_fpregs (int tid)
336 {
337 elf_fpregset_t fpregs;
338
339 if (ptrace (PTRACE_GETFPREGS, tid, 0, (int) &fpregs) < 0)
340 perror_with_name ("Couldn't get floating point status");
341
342 supply_fpregset (&fpregs);
343 }
344
345 /* Store all valid floating-point registers in GDB's register array
346 into the process/thread specified by TID. */
347
348 static void
349 store_fpregs (int tid, int regno)
350 {
351 elf_fpregset_t fpregs;
352
353 if (ptrace (PTRACE_GETFPREGS, tid, 0, (int) &fpregs) < 0)
354 perror_with_name ("Couldn't get floating point status");
355
356 fill_fpregset (&fpregs, regno);
357
358 if (ptrace (PTRACE_SETFPREGS, tid, 0, (int) &fpregs) < 0)
359 perror_with_name ("Couldn't write floating point status");
360 }
361
362 #else
363
364 static void fetch_fpregs (int tid) {}
365 static void store_fpregs (int tid, int regno) {}
366
367 #endif
368 \f
369
370 /* Transfering floating-point and SSE registers to and from GDB. */
371
372 #ifdef HAVE_PTRACE_GETFPXREGS
373
374 /* Fill GDB's register array with the floating-point and SSE register
375 values in *FPXREGSETP. */
376
377 void
378 supply_fpxregset (elf_fpxregset_t *fpxregsetp)
379 {
380 i387_supply_fxsave ((char *) fpxregsetp);
381 }
382
383 /* Fill register REGNO (if it is a floating-point or SSE register) in
384 *FPXREGSETP with the value in GDB's register array. If REGNO is
385 -1, do this for all registers. */
386
387 void
388 fill_fpxregset (elf_fpxregset_t *fpxregsetp, int regno)
389 {
390 i387_fill_fxsave ((char *) fpxregsetp, regno);
391 }
392
393 /* Fetch all registers covered by the PTRACE_GETFPXREGS request from
394 process/thread TID and store their values in GDB's register array.
395 Return non-zero if successful, zero otherwise. */
396
397 static int
398 fetch_fpxregs (int tid)
399 {
400 elf_fpxregset_t fpxregs;
401
402 if (! have_ptrace_getfpxregs)
403 return 0;
404
405 if (ptrace (PTRACE_GETFPXREGS, tid, 0, (int) &fpxregs) < 0)
406 {
407 if (errno == EIO)
408 {
409 have_ptrace_getfpxregs = 0;
410 return 0;
411 }
412
413 perror_with_name ("Couldn't read floating-point and SSE registers");
414 }
415
416 supply_fpxregset (&fpxregs);
417 return 1;
418 }
419
420 /* Store all valid registers in GDB's register array covered by the
421 PTRACE_SETFPXREGS request into the process/thread specified by TID.
422 Return non-zero if successful, zero otherwise. */
423
424 static int
425 store_fpxregs (int tid, int regno)
426 {
427 elf_fpxregset_t fpxregs;
428
429 if (! have_ptrace_getfpxregs)
430 return 0;
431
432 if (ptrace (PTRACE_GETFPXREGS, tid, 0, &fpxregs) == -1)
433 {
434 if (errno == EIO)
435 {
436 have_ptrace_getfpxregs = 0;
437 return 0;
438 }
439
440 perror_with_name ("Couldn't read floating-point and SSE registers");
441 }
442
443 fill_fpxregset (&fpxregs, regno);
444
445 if (ptrace (PTRACE_SETFPXREGS, tid, 0, &fpxregs) == -1)
446 perror_with_name ("Couldn't write floating-point and SSE registers");
447
448 return 1;
449 }
450
451 /* Fill the XMM registers in the register array with dummy values. For
452 cases where we don't have access to the XMM registers. I think
453 this is cleaner than printing a warning. For a cleaner solution,
454 we should gdbarchify the i386 family. */
455
456 static void
457 dummy_sse_values (void)
458 {
459 struct gdbarch_tdep *tdep = gdbarch_tdep (current_gdbarch);
460 /* C doesn't have a syntax for NaN's, so write it out as an array of
461 longs. */
462 static long dummy[4] = { 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff };
463 static long mxcsr = 0x1f80;
464 int reg;
465
466 for (reg = 0; reg < tdep->num_xmm_regs; reg++)
467 supply_register (XMM0_REGNUM + reg, (char *) dummy);
468 if (tdep->num_xmm_regs > 0)
469 supply_register (MXCSR_REGNUM, (char *) &mxcsr);
470 }
471
472 #else
473
474 static int fetch_fpxregs (int tid) { return 0; }
475 static int store_fpxregs (int tid, int regno) { return 0; }
476 static void dummy_sse_values (void) {}
477
478 #endif /* HAVE_PTRACE_GETFPXREGS */
479 \f
480
481 /* Transferring arbitrary registers between GDB and inferior. */
482
483 /* Check if register REGNO in the child process is accessible.
484 If we are accessing registers directly via the U area, only the
485 general-purpose registers are available.
486 All registers should be accessible if we have GETREGS support. */
487
488 int
489 cannot_fetch_register (int regno)
490 {
491 gdb_assert (regno >= 0 && regno < NUM_REGS);
492 return (!have_ptrace_getregs && regmap[regno] == -1);
493 }
494
495 int
496 cannot_store_register (int regno)
497 {
498 gdb_assert (regno >= 0 && regno < NUM_REGS);
499 return (!have_ptrace_getregs && regmap[regno] == -1);
500 }
501
502 /* Fetch register REGNO from the child process. If REGNO is -1, do
503 this for all registers (including the floating point and SSE
504 registers). */
505
506 void
507 fetch_inferior_registers (int regno)
508 {
509 int tid;
510
511 /* Use the old method of peeking around in `struct user' if the
512 GETREGS request isn't available. */
513 if (!have_ptrace_getregs)
514 {
515 int i;
516
517 for (i = 0; i < NUM_REGS; i++)
518 if (regno == -1 || regno == i)
519 fetch_register (i);
520
521 return;
522 }
523
524 /* GNU/Linux LWP ID's are process ID's. */
525 if ((tid = TIDGET (inferior_ptid)) == 0)
526 tid = PIDGET (inferior_ptid); /* Not a threaded program. */
527
528 /* Use the PTRACE_GETFPXREGS request whenever possible, since it
529 transfers more registers in one system call, and we'll cache the
530 results. But remember that fetch_fpxregs can fail, and return
531 zero. */
532 if (regno == -1)
533 {
534 fetch_regs (tid);
535
536 /* The call above might reset `have_ptrace_getregs'. */
537 if (!have_ptrace_getregs)
538 {
539 fetch_inferior_registers (regno);
540 return;
541 }
542
543 if (fetch_fpxregs (tid))
544 return;
545 fetch_fpregs (tid);
546 return;
547 }
548
549 if (GETREGS_SUPPLIES (regno))
550 {
551 fetch_regs (tid);
552 return;
553 }
554
555 if (GETFPXREGS_SUPPLIES (regno))
556 {
557 if (fetch_fpxregs (tid))
558 return;
559
560 /* Either our processor or our kernel doesn't support the SSE
561 registers, so read the FP registers in the traditional way,
562 and fill the SSE registers with dummy values. It would be
563 more graceful to handle differences in the register set using
564 gdbarch. Until then, this will at least make things work
565 plausibly. */
566 fetch_fpregs (tid);
567 return;
568 }
569
570 internal_error (__FILE__, __LINE__,
571 "Got request for bad register number %d.", regno);
572 }
573
574 /* Store register REGNO back into the child process. If REGNO is -1,
575 do this for all registers (including the floating point and SSE
576 registers). */
577 void
578 store_inferior_registers (int regno)
579 {
580 int tid;
581
582 /* Use the old method of poking around in `struct user' if the
583 SETREGS request isn't available. */
584 if (!have_ptrace_getregs)
585 {
586 int i;
587
588 for (i = 0; i < NUM_REGS; i++)
589 if (regno == -1 || regno == i)
590 store_register (i);
591
592 return;
593 }
594
595 /* GNU/Linux LWP ID's are process ID's. */
596 if ((tid = TIDGET (inferior_ptid)) == 0)
597 tid = PIDGET (inferior_ptid); /* Not a threaded program. */
598
599 /* Use the PTRACE_SETFPXREGS requests whenever possible, since it
600 transfers more registers in one system call. But remember that
601 store_fpxregs can fail, and return zero. */
602 if (regno == -1)
603 {
604 store_regs (tid, regno);
605 if (store_fpxregs (tid, regno))
606 return;
607 store_fpregs (tid, regno);
608 return;
609 }
610
611 if (GETREGS_SUPPLIES (regno))
612 {
613 store_regs (tid, regno);
614 return;
615 }
616
617 if (GETFPXREGS_SUPPLIES (regno))
618 {
619 if (store_fpxregs (tid, regno))
620 return;
621
622 /* Either our processor or our kernel doesn't support the SSE
623 registers, so just write the FP registers in the traditional
624 way. */
625 store_fpregs (tid, regno);
626 return;
627 }
628
629 internal_error (__FILE__, __LINE__,
630 "Got request to store bad register number %d.", regno);
631 }
632 \f
633
634 static unsigned long
635 i386_linux_dr_get (int regnum)
636 {
637 int tid;
638 unsigned long value;
639
640 /* FIXME: kettenis/2001-01-29: It's not clear what we should do with
641 multi-threaded processes here. For now, pretend there is just
642 one thread. */
643 tid = PIDGET (inferior_ptid);
644
645 /* FIXME: kettenis/2001-03-27: Calling perror_with_name if the
646 ptrace call fails breaks debugging remote targets. The correct
647 way to fix this is to add the hardware breakpoint and watchpoint
648 stuff to the target vectore. For now, just return zero if the
649 ptrace call fails. */
650 errno = 0;
651 value = ptrace (PTRACE_PEEKUSER, tid,
652 offsetof (struct user, u_debugreg[regnum]), 0);
653 if (errno != 0)
654 #if 0
655 perror_with_name ("Couldn't read debug register");
656 #else
657 return 0;
658 #endif
659
660 return value;
661 }
662
663 static void
664 i386_linux_dr_set (int regnum, unsigned long value)
665 {
666 int tid;
667
668 /* FIXME: kettenis/2001-01-29: It's not clear what we should do with
669 multi-threaded processes here. For now, pretend there is just
670 one thread. */
671 tid = PIDGET (inferior_ptid);
672
673 errno = 0;
674 ptrace (PTRACE_POKEUSER, tid,
675 offsetof (struct user, u_debugreg[regnum]), value);
676 if (errno != 0)
677 perror_with_name ("Couldn't write debug register");
678 }
679
680 void
681 i386_linux_dr_set_control (unsigned long control)
682 {
683 i386_linux_dr_set (DR_CONTROL, control);
684 }
685
686 void
687 i386_linux_dr_set_addr (int regnum, CORE_ADDR addr)
688 {
689 gdb_assert (regnum >= 0 && regnum <= DR_LASTADDR - DR_FIRSTADDR);
690
691 i386_linux_dr_set (DR_FIRSTADDR + regnum, addr);
692 }
693
694 void
695 i386_linux_dr_reset_addr (int regnum)
696 {
697 gdb_assert (regnum >= 0 && regnum <= DR_LASTADDR - DR_FIRSTADDR);
698
699 i386_linux_dr_set (DR_FIRSTADDR + regnum, 0L);
700 }
701
702 unsigned long
703 i386_linux_dr_get_status (void)
704 {
705 return i386_linux_dr_get (DR_STATUS);
706 }
707 \f
708
709 /* Interpreting register set info found in core files. */
710
711 /* Provide registers to GDB from a core file.
712
713 (We can't use the generic version of this function in
714 core-regset.c, because GNU/Linux has *three* different kinds of
715 register set notes. core-regset.c would have to call
716 supply_fpxregset, which most platforms don't have.)
717
718 CORE_REG_SECT points to an array of bytes, which are the contents
719 of a `note' from a core file which BFD thinks might contain
720 register contents. CORE_REG_SIZE is its size.
721
722 WHICH says which register set corelow suspects this is:
723 0 --- the general-purpose register set, in elf_gregset_t format
724 2 --- the floating-point register set, in elf_fpregset_t format
725 3 --- the extended floating-point register set, in elf_fpxregset_t format
726
727 REG_ADDR isn't used on GNU/Linux. */
728
729 static void
730 fetch_core_registers (char *core_reg_sect, unsigned core_reg_size,
731 int which, CORE_ADDR reg_addr)
732 {
733 elf_gregset_t gregset;
734 elf_fpregset_t fpregset;
735
736 switch (which)
737 {
738 case 0:
739 if (core_reg_size != sizeof (gregset))
740 warning ("Wrong size gregset in core file.");
741 else
742 {
743 memcpy (&gregset, core_reg_sect, sizeof (gregset));
744 supply_gregset (&gregset);
745 }
746 break;
747
748 case 2:
749 if (core_reg_size != sizeof (fpregset))
750 warning ("Wrong size fpregset in core file.");
751 else
752 {
753 memcpy (&fpregset, core_reg_sect, sizeof (fpregset));
754 supply_fpregset (&fpregset);
755 }
756 break;
757
758 #ifdef HAVE_PTRACE_GETFPXREGS
759 {
760 elf_fpxregset_t fpxregset;
761
762 case 3:
763 if (core_reg_size != sizeof (fpxregset))
764 warning ("Wrong size fpxregset in core file.");
765 else
766 {
767 memcpy (&fpxregset, core_reg_sect, sizeof (fpxregset));
768 supply_fpxregset (&fpxregset);
769 }
770 break;
771 }
772 #endif
773
774 default:
775 /* We've covered all the kinds of registers we know about here,
776 so this must be something we wouldn't know what to do with
777 anyway. Just ignore it. */
778 break;
779 }
780 }
781 \f
782
783 /* The instruction for a GNU/Linux system call is:
784 int $0x80
785 or 0xcd 0x80. */
786
787 static const unsigned char linux_syscall[] = { 0xcd, 0x80 };
788
789 #define LINUX_SYSCALL_LEN (sizeof linux_syscall)
790
791 /* The system call number is stored in the %eax register. */
792 #define LINUX_SYSCALL_REGNUM 0 /* %eax */
793
794 /* We are specifically interested in the sigreturn and rt_sigreturn
795 system calls. */
796
797 #ifndef SYS_sigreturn
798 #define SYS_sigreturn 0x77
799 #endif
800 #ifndef SYS_rt_sigreturn
801 #define SYS_rt_sigreturn 0xad
802 #endif
803
804 /* Offset to saved processor flags, from <asm/sigcontext.h>. */
805 #define LINUX_SIGCONTEXT_EFLAGS_OFFSET (64)
806
807 /* Resume execution of the inferior process.
808 If STEP is nonzero, single-step it.
809 If SIGNAL is nonzero, give it that signal. */
810
811 void
812 child_resume (ptid_t ptid, int step, enum target_signal signal)
813 {
814 int pid = PIDGET (ptid);
815
816 int request = PTRACE_CONT;
817
818 if (pid == -1)
819 /* Resume all threads. */
820 /* I think this only gets used in the non-threaded case, where "resume
821 all threads" and "resume inferior_ptid" are the same. */
822 pid = PIDGET (inferior_ptid);
823
824 if (step)
825 {
826 CORE_ADDR pc = read_pc_pid (pid_to_ptid (pid));
827 unsigned char buf[LINUX_SYSCALL_LEN];
828
829 request = PTRACE_SINGLESTEP;
830
831 /* Returning from a signal trampoline is done by calling a
832 special system call (sigreturn or rt_sigreturn, see
833 i386-linux-tdep.c for more information). This system call
834 restores the registers that were saved when the signal was
835 raised, including %eflags. That means that single-stepping
836 won't work. Instead, we'll have to modify the signal context
837 that's about to be restored, and set the trace flag there. */
838
839 /* First check if PC is at a system call. */
840 if (read_memory_nobpt (pc, (char *) buf, LINUX_SYSCALL_LEN) == 0
841 && memcmp (buf, linux_syscall, LINUX_SYSCALL_LEN) == 0)
842 {
843 int syscall = read_register_pid (LINUX_SYSCALL_REGNUM,
844 pid_to_ptid (pid));
845
846 /* Then check the system call number. */
847 if (syscall == SYS_sigreturn || syscall == SYS_rt_sigreturn)
848 {
849 CORE_ADDR sp = read_register (SP_REGNUM);
850 CORE_ADDR addr = sp;
851 unsigned long int eflags;
852
853 if (syscall == SYS_rt_sigreturn)
854 addr = read_memory_integer (sp + 8, 4) + 20;
855
856 /* Set the trace flag in the context that's about to be
857 restored. */
858 addr += LINUX_SIGCONTEXT_EFLAGS_OFFSET;
859 read_memory (addr, (char *) &eflags, 4);
860 eflags |= 0x0100;
861 write_memory (addr, (char *) &eflags, 4);
862 }
863 }
864 }
865
866 if (ptrace (request, pid, 0, target_signal_to_host (signal)) == -1)
867 perror_with_name ("ptrace");
868 }
869 \f
870
871 /* Register that we are able to handle GNU/Linux ELF core file
872 formats. */
873
874 static struct core_fns linux_elf_core_fns =
875 {
876 bfd_target_elf_flavour, /* core_flavour */
877 default_check_format, /* check_format */
878 default_core_sniffer, /* core_sniffer */
879 fetch_core_registers, /* core_read_registers */
880 NULL /* next */
881 };
882
883 void
884 _initialize_i386_linux_nat (void)
885 {
886 add_core_fns (&linux_elf_core_fns);
887 }
This page took 0.046443 seconds and 4 git commands to generate.