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