1 /* Native-dependent code for GNU/Linux x86.
3 Copyright 1999, 2000, 2001, 2002 Free Software Foundation, Inc.
5 This file is part of GDB.
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.
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.
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. */
27 #include "gdb_assert.h"
28 #include "gdb_string.h"
29 #include <sys/ptrace.h>
31 #include <sys/procfs.h>
41 #ifdef HAVE_SYS_DEBUGREG_H
42 #include <sys/debugreg.h>
46 #define DR_FIRSTADDR 0
61 /* Prototypes for supply_gregset etc. */
64 /* Prototypes for i387_supply_fsave etc. */
65 #include "i387-tdep.h"
67 /* Defines for XMM0_REGNUM etc. */
68 #include "i386-tdep.h"
70 /* Defines I386_LINUX_ORIG_EAX_REGNUM. */
71 #include "i386-linux-tdep.h"
73 /* Prototypes for local functions. */
74 static void dummy_sse_values (void);
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.
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. */
90 /* Mapping between the general-purpose registers in `struct user'
91 format and GDB's register array layout. */
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 */
108 /* Which ptrace request retrieves which registers?
109 These apply to the corresponding SET requests as well. */
111 #define GETREGS_SUPPLIES(regno) \
112 ((0 <= (regno) && (regno) <= 15) || (regno) == I386_LINUX_ORIG_EAX_REGNUM)
114 #define GETFPREGS_SUPPLIES(regno) \
115 (FP0_REGNUM <= (regno) && (regno) <= LAST_FPU_CTRL_REGNUM)
117 #define GETFPXREGS_SUPPLIES(regno) \
118 (FP0_REGNUM <= (regno) && (regno) <= MXCSR_REGNUM)
120 /* Does the current host support the GETREGS request? */
121 int have_ptrace_getregs
=
122 #ifdef HAVE_PTRACE_GETREGS
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.
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
146 /* Support for the user struct. */
148 /* Return the address of register REGNUM. BLOCKEND is the value of
149 u.u_ar0, which should point to the registers. */
152 register_u_addr (CORE_ADDR blockend
, int regnum
)
154 return (blockend
+ 4 * regmap
[regnum
]);
157 /* Return the size of the user struct. */
162 return (sizeof (struct user
));
166 /* Accessing registers through the U area, one at a time. */
168 /* Fetch one register. */
171 fetch_register (int regno
)
176 gdb_assert (!have_ptrace_getregs
);
177 if (cannot_fetch_register (regno
))
179 supply_register (regno
, NULL
);
183 /* GNU/Linux LWP ID's are process ID's. */
184 tid
= TIDGET (inferior_ptid
);
186 tid
= PIDGET (inferior_ptid
); /* Not a threaded program. */
189 val
= ptrace (PTRACE_PEEKUSER
, tid
, register_addr (regno
, 0), 0);
191 error ("Couldn't read register %s (#%d): %s.", REGISTER_NAME (regno
),
192 regno
, safe_strerror (errno
));
194 supply_register (regno
, &val
);
197 /* Store one register. */
200 store_register (int regno
)
205 gdb_assert (!have_ptrace_getregs
);
206 if (cannot_store_register (regno
))
209 /* GNU/Linux LWP ID's are process ID's. */
210 tid
= TIDGET (inferior_ptid
);
212 tid
= PIDGET (inferior_ptid
); /* Not a threaded program. */
215 regcache_collect (regno
, &val
);
216 ptrace (PTRACE_POKEUSER
, tid
, register_addr (regno
, 0), val
);
218 error ("Couldn't write register %s (#%d): %s.", REGISTER_NAME (regno
),
219 regno
, safe_strerror (errno
));
223 /* Transfering the general-purpose registers between GDB, inferiors
226 /* Fill GDB's register array with the general-purpose register values
230 supply_gregset (elf_gregset_t
*gregsetp
)
232 elf_greg_t
*regp
= (elf_greg_t
*) gregsetp
;
235 for (i
= 0; i
< I386_NUM_GREGS
; i
++)
236 supply_register (i
, (char *) (regp
+ regmap
[i
]));
238 if (I386_LINUX_ORIG_EAX_REGNUM
< NUM_REGS
)
239 supply_register (I386_LINUX_ORIG_EAX_REGNUM
, (char *) (regp
+ ORIG_EAX
));
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. */
247 fill_gregset (elf_gregset_t
*gregsetp
, int regno
)
249 elf_greg_t
*regp
= (elf_greg_t
*) gregsetp
;
252 for (i
= 0; i
< I386_NUM_GREGS
; i
++)
253 if (regno
== -1 || regno
== i
)
254 regcache_collect (i
, regp
+ regmap
[i
]);
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
);
261 #ifdef HAVE_PTRACE_GETREGS
263 /* Fetch all general-purpose registers from process/thread TID and
264 store their values in GDB's register array. */
271 if (ptrace (PTRACE_GETREGS
, tid
, 0, (int) ®s
) < 0)
275 /* The kernel we're running on doesn't support the GETREGS
276 request. Reset `have_ptrace_getregs'. */
277 have_ptrace_getregs
= 0;
281 perror_with_name ("Couldn't get registers");
284 supply_gregset (®s
);
287 /* Store all valid general-purpose registers in GDB's register array
288 into the process/thread specified by TID. */
291 store_regs (int tid
, int regno
)
295 if (ptrace (PTRACE_GETREGS
, tid
, 0, (int) ®s
) < 0)
296 perror_with_name ("Couldn't get registers");
298 fill_gregset (®s
, regno
);
300 if (ptrace (PTRACE_SETREGS
, tid
, 0, (int) ®s
) < 0)
301 perror_with_name ("Couldn't write registers");
306 static void fetch_regs (int tid
) {}
307 static void store_regs (int tid
, int regno
) {}
312 /* Transfering floating-point registers between GDB, inferiors and cores. */
314 /* Fill GDB's register array with the floating-point register values in
318 supply_fpregset (elf_fpregset_t
*fpregsetp
)
320 i387_supply_fsave ((char *) fpregsetp
);
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. */
329 fill_fpregset (elf_fpregset_t
*fpregsetp
, int regno
)
331 i387_fill_fsave ((char *) fpregsetp
, regno
);
334 #ifdef HAVE_PTRACE_GETREGS
336 /* Fetch all floating-point registers from process/thread TID and store
337 thier values in GDB's register array. */
340 fetch_fpregs (int tid
)
342 elf_fpregset_t fpregs
;
344 if (ptrace (PTRACE_GETFPREGS
, tid
, 0, (int) &fpregs
) < 0)
345 perror_with_name ("Couldn't get floating point status");
347 supply_fpregset (&fpregs
);
350 /* Store all valid floating-point registers in GDB's register array
351 into the process/thread specified by TID. */
354 store_fpregs (int tid
, int regno
)
356 elf_fpregset_t fpregs
;
358 if (ptrace (PTRACE_GETFPREGS
, tid
, 0, (int) &fpregs
) < 0)
359 perror_with_name ("Couldn't get floating point status");
361 fill_fpregset (&fpregs
, regno
);
363 if (ptrace (PTRACE_SETFPREGS
, tid
, 0, (int) &fpregs
) < 0)
364 perror_with_name ("Couldn't write floating point status");
369 static void fetch_fpregs (int tid
) {}
370 static void store_fpregs (int tid
, int regno
) {}
375 /* Transfering floating-point and SSE registers to and from GDB. */
377 #ifdef HAVE_PTRACE_GETFPXREGS
379 /* Fill GDB's register array with the floating-point and SSE register
380 values in *FPXREGSETP. */
383 supply_fpxregset (elf_fpxregset_t
*fpxregsetp
)
385 i387_supply_fxsave ((char *) fpxregsetp
);
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. */
393 fill_fpxregset (elf_fpxregset_t
*fpxregsetp
, int regno
)
395 i387_fill_fxsave ((char *) fpxregsetp
, regno
);
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. */
403 fetch_fpxregs (int tid
)
405 elf_fpxregset_t fpxregs
;
407 if (! have_ptrace_getfpxregs
)
410 if (ptrace (PTRACE_GETFPXREGS
, tid
, 0, (int) &fpxregs
) < 0)
414 have_ptrace_getfpxregs
= 0;
418 perror_with_name ("Couldn't read floating-point and SSE registers");
421 supply_fpxregset (&fpxregs
);
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. */
430 store_fpxregs (int tid
, int regno
)
432 elf_fpxregset_t fpxregs
;
434 if (! have_ptrace_getfpxregs
)
437 if (ptrace (PTRACE_GETFPXREGS
, tid
, 0, &fpxregs
) == -1)
441 have_ptrace_getfpxregs
= 0;
445 perror_with_name ("Couldn't read floating-point and SSE registers");
448 fill_fpxregset (&fpxregs
, regno
);
450 if (ptrace (PTRACE_SETFPXREGS
, tid
, 0, &fpxregs
) == -1)
451 perror_with_name ("Couldn't write floating-point and SSE registers");
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. */
462 dummy_sse_values (void)
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
467 static long dummy
[4] = { 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff };
468 static long mxcsr
= 0x1f80;
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
);
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) {}
483 #endif /* HAVE_PTRACE_GETFPXREGS */
486 /* Transferring arbitrary registers between GDB and inferior. */
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. */
494 cannot_fetch_register (int regno
)
496 gdb_assert (regno
>= 0 && regno
< NUM_REGS
);
497 return (!have_ptrace_getregs
&& regmap
[regno
] == -1);
501 cannot_store_register (int regno
)
503 gdb_assert (regno
>= 0 && regno
< NUM_REGS
);
504 return (!have_ptrace_getregs
&& regmap
[regno
] == -1);
507 /* Fetch register REGNO from the child process. If REGNO is -1, do
508 this for all registers (including the floating point and SSE
512 fetch_inferior_registers (int regno
)
516 /* Use the old method of peeking around in `struct user' if the
517 GETREGS request isn't available. */
518 if (!have_ptrace_getregs
)
522 for (i
= 0; i
< NUM_REGS
; i
++)
523 if (regno
== -1 || regno
== i
)
529 /* GNU/Linux LWP ID's are process ID's. */
530 tid
= TIDGET (inferior_ptid
);
532 tid
= PIDGET (inferior_ptid
); /* Not a threaded program. */
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
542 /* The call above might reset `have_ptrace_getregs'. */
543 if (!have_ptrace_getregs
)
545 fetch_inferior_registers (regno
);
549 if (fetch_fpxregs (tid
))
555 if (GETREGS_SUPPLIES (regno
))
561 if (GETFPXREGS_SUPPLIES (regno
))
563 if (fetch_fpxregs (tid
))
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
576 internal_error (__FILE__
, __LINE__
,
577 "Got request for bad register number %d.", regno
);
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
584 store_inferior_registers (int regno
)
588 /* Use the old method of poking around in `struct user' if the
589 SETREGS request isn't available. */
590 if (!have_ptrace_getregs
)
594 for (i
= 0; i
< NUM_REGS
; i
++)
595 if (regno
== -1 || regno
== i
)
601 /* GNU/Linux LWP ID's are process ID's. */
602 tid
= TIDGET (inferior_ptid
);
604 tid
= PIDGET (inferior_ptid
); /* Not a threaded program. */
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. */
611 store_regs (tid
, regno
);
612 if (store_fpxregs (tid
, regno
))
614 store_fpregs (tid
, regno
);
618 if (GETREGS_SUPPLIES (regno
))
620 store_regs (tid
, regno
);
624 if (GETFPXREGS_SUPPLIES (regno
))
626 if (store_fpxregs (tid
, regno
))
629 /* Either our processor or our kernel doesn't support the SSE
630 registers, so just write the FP registers in the traditional
632 store_fpregs (tid
, regno
);
636 internal_error (__FILE__
, __LINE__
,
637 "Got request to store bad register number %d.", regno
);
642 i386_linux_dr_get (int regnum
)
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
650 tid
= PIDGET (inferior_ptid
);
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. */
658 value
= ptrace (PTRACE_PEEKUSER
, tid
,
659 offsetof (struct user
, u_debugreg
[regnum
]), 0);
662 perror_with_name ("Couldn't read debug register");
671 i386_linux_dr_set (int regnum
, unsigned long value
)
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
678 tid
= PIDGET (inferior_ptid
);
681 ptrace (PTRACE_POKEUSER
, tid
,
682 offsetof (struct user
, u_debugreg
[regnum
]), value
);
684 perror_with_name ("Couldn't write debug register");
688 i386_linux_dr_set_control (unsigned long control
)
690 i386_linux_dr_set (DR_CONTROL
, control
);
694 i386_linux_dr_set_addr (int regnum
, CORE_ADDR addr
)
696 gdb_assert (regnum
>= 0 && regnum
<= DR_LASTADDR
- DR_FIRSTADDR
);
698 i386_linux_dr_set (DR_FIRSTADDR
+ regnum
, addr
);
702 i386_linux_dr_reset_addr (int regnum
)
704 gdb_assert (regnum
>= 0 && regnum
<= DR_LASTADDR
- DR_FIRSTADDR
);
706 i386_linux_dr_set (DR_FIRSTADDR
+ regnum
, 0L);
710 i386_linux_dr_get_status (void)
712 return i386_linux_dr_get (DR_STATUS
);
716 /* Interpreting register set info found in core files. */
718 /* Provide registers to GDB from a core file.
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.)
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.
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
734 REG_ADDR isn't used on GNU/Linux. */
737 fetch_core_registers (char *core_reg_sect
, unsigned core_reg_size
,
738 int which
, CORE_ADDR reg_addr
)
740 elf_gregset_t gregset
;
741 elf_fpregset_t fpregset
;
746 if (core_reg_size
!= sizeof (gregset
))
747 warning ("Wrong size gregset in core file.");
750 memcpy (&gregset
, core_reg_sect
, sizeof (gregset
));
751 supply_gregset (&gregset
);
756 if (core_reg_size
!= sizeof (fpregset
))
757 warning ("Wrong size fpregset in core file.");
760 memcpy (&fpregset
, core_reg_sect
, sizeof (fpregset
));
761 supply_fpregset (&fpregset
);
765 #ifdef HAVE_PTRACE_GETFPXREGS
767 elf_fpxregset_t fpxregset
;
770 if (core_reg_size
!= sizeof (fpxregset
))
771 warning ("Wrong size fpxregset in core file.");
774 memcpy (&fpxregset
, core_reg_sect
, sizeof (fpxregset
));
775 supply_fpxregset (&fpxregset
);
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. */
790 /* The instruction for a GNU/Linux system call is:
794 static const unsigned char linux_syscall
[] = { 0xcd, 0x80 };
796 #define LINUX_SYSCALL_LEN (sizeof linux_syscall)
798 /* The system call number is stored in the %eax register. */
799 #define LINUX_SYSCALL_REGNUM 0 /* %eax */
801 /* We are specifically interested in the sigreturn and rt_sigreturn
804 #ifndef SYS_sigreturn
805 #define SYS_sigreturn 0x77
807 #ifndef SYS_rt_sigreturn
808 #define SYS_rt_sigreturn 0xad
811 /* Offset to saved processor flags, from <asm/sigcontext.h>. */
812 #define LINUX_SIGCONTEXT_EFLAGS_OFFSET (64)
814 /* Resume execution of the inferior process.
815 If STEP is nonzero, single-step it.
816 If SIGNAL is nonzero, give it that signal. */
819 child_resume (ptid_t ptid
, int step
, enum target_signal signal
)
821 int pid
= PIDGET (ptid
);
823 int request
= PTRACE_CONT
;
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
);
833 CORE_ADDR pc
= read_pc_pid (pid_to_ptid (pid
));
834 unsigned char buf
[LINUX_SYSCALL_LEN
];
836 request
= PTRACE_SINGLESTEP
;
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. */
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)
850 int syscall
= read_register_pid (LINUX_SYSCALL_REGNUM
,
853 /* Then check the system call number. */
854 if (syscall
== SYS_sigreturn
|| syscall
== SYS_rt_sigreturn
)
856 CORE_ADDR sp
= read_register (SP_REGNUM
);
858 unsigned long int eflags
;
860 if (syscall
== SYS_rt_sigreturn
)
861 addr
= read_memory_integer (sp
+ 8, 4) + 20;
863 /* Set the trace flag in the context that's about to be
865 addr
+= LINUX_SIGCONTEXT_EFLAGS_OFFSET
;
866 read_memory (addr
, (char *) &eflags
, 4);
868 write_memory (addr
, (char *) &eflags
, 4);
873 if (ptrace (request
, pid
, 0, target_signal_to_host (signal
)) == -1)
874 perror_with_name ("ptrace");
878 /* Register that we are able to handle GNU/Linux ELF core file
881 static struct core_fns linux_elf_core_fns
=
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 */
891 _initialize_i386_linux_nat (void)
893 add_core_fns (&linux_elf_core_fns
);