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