1 /* Native-dependent code for Linux/x86.
2 Copyright 1999, 2000 Free Software Foundation, Inc.
4 This file is part of GDB.
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
25 /* For i386_linux_skip_solib_resolver. */
30 #include <sys/ptrace.h>
32 #include <sys/procfs.h>
38 /* Prototypes for supply_gregset etc. */
41 /* Prototypes for i387_supply_fsave etc. */
44 /* On Linux, threads are implemented as pseudo-processes, in which
45 case we may be tracing more than one process at a time. In that
46 case, inferior_pid will contain the main process ID and the
47 individual thread (process) ID mashed together. These macros are
48 used to separate them out. These definitions should be overridden
49 if thread support is included. */
51 #if !defined (PIDGET) /* Default definition for PIDGET/TIDGET. */
52 #define PIDGET(PID) PID
57 /* The register sets used in Linux ELF core-dumps are identical to the
58 register sets in `struct user' that is used for a.out core-dumps,
59 and is also used by `ptrace'. The corresponding types are
60 `elf_gregset_t' for the general-purpose registers (with
61 `elf_greg_t' the type of a single GP register) and `elf_fpregset_t'
62 for the floating-point registers.
64 Those types used to be available under the names `gregset_t' and
65 `fpregset_t' too, and this file used those names in the past. But
66 those names are now used for the register sets used in the
67 `mcontext_t' type, and have a different size and layout. */
69 /* Mapping between the general-purpose registers in `struct user'
70 format and GDB's register array layout. */
79 /* Which ptrace request retrieves which registers?
80 These apply to the corresponding SET requests as well. */
81 #define GETREGS_SUPPLIES(regno) \
82 (0 <= (regno) && (regno) <= 15)
83 #define GETFPREGS_SUPPLIES(regno) \
84 (FP0_REGNUM <= (regno) && (regno) <= LAST_FPU_CTRL_REGNUM)
85 #define GETFPXREGS_SUPPLIES(regno) \
86 (FP0_REGNUM <= (regno) && (regno) <= MXCSR_REGNUM)
88 /* Does the current host support the GETREGS request? */
89 int have_ptrace_getregs =
90 #ifdef HAVE_PTRACE_GETREGS
97 /* Does the current host support the GETFPXREGS request? The header
98 file may or may not define it, and even if it is defined, the
99 kernel will return EIO if it's running on a pre-SSE processor.
101 My instinct is to attach this to some architecture- or
102 target-specific data structure, but really, a particular GDB
103 process can only run on top of one kernel at a time. So it's okay
104 for this to be a simple variable. */
105 int have_ptrace_getfpxregs =
106 #ifdef HAVE_PTRACE_GETFPXREGS
114 /* Fetching registers directly from the U area, one at a time. */
116 /* FIXME: kettenis/2000-03-05: This duplicates code from `inptrace.c'.
117 The problem is that we define FETCH_INFERIOR_REGISTERS since we
118 want to use our own versions of {fetch,store}_inferior_registers
119 that use the GETREGS request. This means that the code in
120 `infptrace.c' is #ifdef'd out. But we need to fall back on that
121 code when GDB is running on top of a kernel that doesn't support
122 the GETREGS request. I want to avoid changing `infptrace.c' right
126 #define PT_READ_U PTRACE_PEEKUSR
129 #define PT_WRITE_U PTRACE_POKEUSR
132 /* Default the type of the ptrace transfer to int. */
133 #ifndef PTRACE_XFER_TYPE
134 #define PTRACE_XFER_TYPE int
137 /* Registers we shouldn't try to fetch. */
138 #if !defined (CANNOT_FETCH_REGISTER)
139 #define CANNOT_FETCH_REGISTER(regno) 0
142 /* Fetch one register. */
145 fetch_register (int regno)
147 /* This isn't really an address. But ptrace thinks of it as one. */
149 char mess[128]; /* For messages */
151 unsigned int offset; /* Offset of registers within the u area. */
152 char buf[MAX_REGISTER_RAW_SIZE];
155 if (CANNOT_FETCH_REGISTER (regno))
157 memset (buf, '\0', REGISTER_RAW_SIZE (regno)); /* Supply zeroes */
158 supply_register (regno, buf);
162 /* Overload thread id onto process id */
163 if ((tid = TIDGET (inferior_pid)) == 0)
164 tid = inferior_pid; /* no thread id, just use process id */
166 offset = U_REGS_OFFSET;
168 regaddr = register_addr (regno, offset);
169 for (i = 0; i < REGISTER_RAW_SIZE (regno); i += sizeof (PTRACE_XFER_TYPE))
172 *(PTRACE_XFER_TYPE *) & buf[i] = ptrace (PT_READ_U, tid,
173 (PTRACE_ARG3_TYPE) regaddr, 0);
174 regaddr += sizeof (PTRACE_XFER_TYPE);
177 sprintf (mess, "reading register %s (#%d)",
178 REGISTER_NAME (regno), regno);
179 perror_with_name (mess);
182 supply_register (regno, buf);
185 /* Fetch register values from the inferior.
186 If REGNO is negative, do this for all registers.
187 Otherwise, REGNO specifies which register (so we can save time). */
190 old_fetch_inferior_registers (int regno)
194 fetch_register (regno);
198 for (regno = 0; regno < ARCH_NUM_REGS; regno++)
200 fetch_register (regno);
205 /* Registers we shouldn't try to store. */
206 #if !defined (CANNOT_STORE_REGISTER)
207 #define CANNOT_STORE_REGISTER(regno) 0
210 /* Store one register. */
213 store_register (int regno)
215 /* This isn't really an address. But ptrace thinks of it as one. */
217 char mess[128]; /* For messages */
219 unsigned int offset; /* Offset of registers within the u area. */
222 if (CANNOT_STORE_REGISTER (regno))
227 /* Overload thread id onto process id */
228 if ((tid = TIDGET (inferior_pid)) == 0)
229 tid = inferior_pid; /* no thread id, just use process id */
231 offset = U_REGS_OFFSET;
233 regaddr = register_addr (regno, offset);
234 for (i = 0; i < REGISTER_RAW_SIZE (regno); i += sizeof (PTRACE_XFER_TYPE))
237 ptrace (PT_WRITE_U, tid, (PTRACE_ARG3_TYPE) regaddr,
238 *(PTRACE_XFER_TYPE *) & registers[REGISTER_BYTE (regno) + i]);
239 regaddr += sizeof (PTRACE_XFER_TYPE);
242 sprintf (mess, "writing register %s (#%d)",
243 REGISTER_NAME (regno), regno);
244 perror_with_name (mess);
249 /* Store our register values back into the inferior.
250 If REGNO is negative, do this for all registers.
251 Otherwise, REGNO specifies which register (so we can save time). */
254 old_store_inferior_registers (int regno)
258 store_register (regno);
262 for (regno = 0; regno < ARCH_NUM_REGS; regno++)
264 store_register (regno);
270 /* Transfering the general-purpose registers between GDB, inferiors
273 /* Fill GDB's register array with the genereal-purpose register values
277 supply_gregset (elf_gregset_t *gregsetp)
279 elf_greg_t *regp = (elf_greg_t *) gregsetp;
282 for (i = 0; i < NUM_GREGS; i++)
283 supply_register (i, (char *) (regp + regmap[i]));
286 /* Fill register REGNO (if it is a general-purpose register) in
287 *GREGSETPS with the value in GDB's register array. If REGNO is -1,
288 do this for all registers. */
291 fill_gregset (elf_gregset_t *gregsetp, int regno)
293 elf_greg_t *regp = (elf_greg_t *) gregsetp;
296 for (i = 0; i < NUM_GREGS; i++)
297 if ((regno == -1 || regno == i))
298 *(regp + regmap[i]) = *(elf_greg_t *) ®isters[REGISTER_BYTE (i)];
301 #ifdef HAVE_PTRACE_GETREGS
303 /* Fetch all general-purpose registers from process/thread TID and
304 store their values in GDB's register array. */
311 if (ptrace (PTRACE_GETREGS, tid, 0, (int) ®s) < 0)
315 /* The kernel we're running on doesn't support the GETREGS
316 request. Reset `have_ptrace_getregs'. */
317 have_ptrace_getregs = 0;
321 perror_with_name ("Couldn't get registers");
324 supply_gregset (®s);
327 /* Store all valid general-purpose registers in GDB's register array
328 into the process/thread specified by TID. */
331 store_regs (int tid, int regno)
335 if (ptrace (PTRACE_GETREGS, tid, 0, (int) ®s) < 0)
336 perror_with_name ("Couldn't get registers");
338 fill_gregset (®s, regno);
340 if (ptrace (PTRACE_SETREGS, tid, 0, (int) ®s) < 0)
341 perror_with_name ("Couldn't write registers");
346 static void fetch_regs (int tid) {}
347 static void store_regs (int tid, int regno) {}
352 /* Transfering floating-point registers between GDB, inferiors and cores. */
354 /* Fill GDB's register array with the floating-point register values in
358 supply_fpregset (elf_fpregset_t *fpregsetp)
360 i387_supply_fsave ((char *) fpregsetp);
363 /* Fill register REGNO (if it is a floating-point register) in
364 *FPREGSETP with the value in GDB's register array. If REGNO is -1,
365 do this for all registers. */
368 fill_fpregset (elf_fpregset_t *fpregsetp, int regno)
370 i387_fill_fsave ((char *) fpregsetp, regno);
373 #ifdef HAVE_PTRACE_GETREGS
375 /* Fetch all floating-point registers from process/thread TID and store
376 thier values in GDB's register array. */
379 fetch_fpregs (int tid)
381 elf_fpregset_t fpregs;
383 if (ptrace (PTRACE_GETFPREGS, tid, 0, (int) &fpregs) < 0)
384 perror_with_name ("Couldn't get floating point status");
386 supply_fpregset (&fpregs);
389 /* Store all valid floating-point registers in GDB's register array
390 into the process/thread specified by TID. */
393 store_fpregs (int tid, int regno)
395 elf_fpregset_t fpregs;
397 if (ptrace (PTRACE_GETFPREGS, tid, 0, (int) &fpregs) < 0)
398 perror_with_name ("Couldn't get floating point status");
400 fill_fpregset (&fpregs, regno);
402 if (ptrace (PTRACE_SETFPREGS, tid, 0, (int) &fpregs) < 0)
403 perror_with_name ("Couldn't write floating point status");
408 static void fetch_fpregs (int tid) {}
409 static void store_fpregs (int tid, int regno) {}
414 /* Transfering floating-point and SSE registers to and from GDB. */
416 #ifdef HAVE_PTRACE_GETFPXREGS
418 /* Fill GDB's register array with the floating-point and SSE register
419 values in *FPXREGSETP. */
422 supply_fpxregset (elf_fpxregset_t *fpxregsetp)
424 i387_supply_fxsave ((char *) fpxregsetp);
427 /* Fill register REGNO (if it is a floating-point or SSE register) in
428 *FPXREGSETP with the value in GDB's register array. If REGNO is
429 -1, do this for all registers. */
432 fill_fpxregset (elf_fpxregset_t *fpxregsetp, int regno)
434 i387_fill_fxsave ((char *) fpxregsetp, regno);
437 /* Fetch all registers covered by the PTRACE_GETFPXREGS request from
438 process/thread TID and store their values in GDB's register array.
439 Return non-zero if successful, zero otherwise. */
442 fetch_fpxregs (int tid)
444 elf_fpxregset_t fpxregs;
446 if (! have_ptrace_getfpxregs)
449 if (ptrace (PTRACE_GETFPXREGS, tid, 0, (int) &fpxregs) < 0)
453 have_ptrace_getfpxregs = 0;
457 perror_with_name ("Couldn't read floating-point and SSE registers");
460 supply_fpxregset (&fpxregs);
464 /* Store all valid registers in GDB's register array covered by the
465 PTRACE_SETFPXREGS request into the process/thread specified by TID.
466 Return non-zero if successful, zero otherwise. */
469 store_fpxregs (int tid, int regno)
471 elf_fpxregset_t fpxregs;
473 if (! have_ptrace_getfpxregs)
476 if (ptrace (PTRACE_GETFPXREGS, tid, 0, &fpxregs) == -1)
477 perror_with_name ("Couldn't read floating-point and SSE registers");
479 fill_fpxregset (&fpxregs, regno);
481 if (ptrace (PTRACE_SETFPXREGS, tid, 0, &fpxregs) == -1)
482 perror_with_name ("Couldn't write floating-point and SSE registers");
487 /* Fill the XMM registers in the register array with dummy values. For
488 cases where we don't have access to the XMM registers. I think
489 this is cleaner than printing a warning. For a cleaner solution,
490 we should gdbarchify the i386 family. */
493 dummy_sse_values (void)
495 /* C doesn't have a syntax for NaN's, so write it out as an array of
497 static long dummy[4] = { 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff };
498 static long mxcsr = 0x1f80;
501 for (reg = 0; reg < 8; reg++)
502 supply_register (XMM0_REGNUM + reg, (char *) dummy);
503 supply_register (MXCSR_REGNUM, (char *) &mxcsr);
508 static int fetch_fpxregs (int tid) { return 0; }
509 static int store_fpxregs (int tid, int regno) { return 0; }
510 static void dummy_sse_values (void) {}
512 #endif /* HAVE_PTRACE_GETFPXREGS */
515 /* Transferring arbitrary registers between GDB and inferior. */
517 /* Fetch register REGNO from the child process. If REGNO is -1, do
518 this for all registers (including the floating point and SSE
522 fetch_inferior_registers (int regno)
526 /* Use the old method of peeking around in `struct user' if the
527 GETREGS request isn't available. */
528 if (! have_ptrace_getregs)
530 old_fetch_inferior_registers (regno);
534 /* Linux LWP ID's are process ID's. */
535 if ((tid = TIDGET (inferior_pid)) == 0)
536 tid = inferior_pid; /* Not a threaded program. */
538 /* Use the PTRACE_GETFPXREGS request whenever possible, since it
539 transfers more registers in one system call, and we'll cache the
540 results. But remember that fetch_fpxregs can fail, and return
546 /* The call above might reset `have_ptrace_getregs'. */
547 if (! have_ptrace_getregs)
549 old_fetch_inferior_registers (-1);
553 if (fetch_fpxregs (tid))
559 if (GETREGS_SUPPLIES (regno))
565 if (GETFPXREGS_SUPPLIES (regno))
567 if (fetch_fpxregs (tid))
570 /* Either our processor or our kernel doesn't support the SSE
571 registers, so read the FP registers in the traditional way,
572 and fill the SSE registers with dummy values. It would be
573 more graceful to handle differences in the register set using
574 gdbarch. Until then, this will at least make things work
581 internal_error ("Got request for bad register number %d.", regno);
584 /* Store register REGNO back into the child process. If REGNO is -1,
585 do this for all registers (including the floating point and SSE
588 store_inferior_registers (int regno)
592 /* Use the old method of poking around in `struct user' if the
593 SETREGS request isn't available. */
594 if (! have_ptrace_getregs)
596 old_store_inferior_registers (regno);
600 /* Linux LWP ID's are process ID's. */
601 if ((tid = TIDGET (inferior_pid)) == 0)
602 tid = inferior_pid; /* Not a threaded program. */
604 /* Use the PTRACE_SETFPXREGS requests whenever possible, since it
605 transfers more registers in one system call. But remember that
606 store_fpxregs can fail, and return zero. */
609 store_regs (tid, regno);
610 if (store_fpxregs (tid, regno))
612 store_fpregs (tid, regno);
616 if (GETREGS_SUPPLIES (regno))
618 store_regs (tid, regno);
622 if (GETFPXREGS_SUPPLIES (regno))
624 if (store_fpxregs (tid, regno))
627 /* Either our processor or our kernel doesn't support the SSE
628 registers, so just write the FP registers in the traditional
630 store_fpregs (tid, regno);
634 internal_error ("Got request to store bad register number %d.", regno);
638 /* Interpreting register set info found in core files. */
640 /* Provide registers to GDB from a core file.
642 (We can't use the generic version of this function in
643 core-regset.c, because Linux has *three* different kinds of
644 register set notes. core-regset.c would have to call
645 supply_fpxregset, which most platforms don't have.)
647 CORE_REG_SECT points to an array of bytes, which are the contents
648 of a `note' from a core file which BFD thinks might contain
649 register contents. CORE_REG_SIZE is its size.
651 WHICH says which register set corelow suspects this is:
652 0 --- the general-purpose register set, in elf_gregset_t format
653 2 --- the floating-point register set, in elf_fpregset_t format
654 3 --- the extended floating-point register set, in elf_fpxregset_t format
656 REG_ADDR isn't used on Linux. */
659 fetch_core_registers (char *core_reg_sect, unsigned core_reg_size,
660 int which, CORE_ADDR reg_addr)
662 elf_gregset_t gregset;
663 elf_fpregset_t fpregset;
668 if (core_reg_size != sizeof (gregset))
669 warning ("Wrong size gregset in core file.");
672 memcpy (&gregset, core_reg_sect, sizeof (gregset));
673 supply_gregset (&gregset);
678 if (core_reg_size != sizeof (fpregset))
679 warning ("Wrong size fpregset in core file.");
682 memcpy (&fpregset, core_reg_sect, sizeof (fpregset));
683 supply_fpregset (&fpregset);
687 #ifdef HAVE_PTRACE_GETFPXREGS
689 elf_fpxregset_t fpxregset;
692 if (core_reg_size != sizeof (fpxregset))
693 warning ("Wrong size fpxregset in core file.");
696 memcpy (&fpxregset, core_reg_sect, sizeof (fpxregset));
697 supply_fpxregset (&fpxregset);
704 /* We've covered all the kinds of registers we know about here,
705 so this must be something we wouldn't know what to do with
706 anyway. Just ignore it. */
712 /* The instruction for a Linux system call is:
716 static const unsigned char linux_syscall[] = { 0xcd, 0x80 };
718 #define LINUX_SYSCALL_LEN (sizeof linux_syscall)
720 /* The system call number is stored in the %eax register. */
721 #define LINUX_SYSCALL_REGNUM 0 /* %eax */
723 /* We are specifically interested in the sigreturn and rt_sigreturn
726 #ifndef SYS_sigreturn
727 #define SYS_sigreturn 0x77
729 #ifndef SYS_rt_sigreturn
730 #define SYS_rt_sigreturn 0xad
733 /* Offset to saved processor flags, from <asm/sigcontext.h>. */
734 #define LINUX_SIGCONTEXT_EFLAGS_OFFSET (64)
736 /* Resume execution of the inferior process.
737 If STEP is nonzero, single-step it.
738 If SIGNAL is nonzero, give it that signal. */
741 child_resume (int pid, int step, enum target_signal signal)
743 int request = PTRACE_CONT;
746 /* Resume all threads. */
747 /* I think this only gets used in the non-threaded case, where "resume
748 all threads" and "resume inferior_pid" are the same. */
753 CORE_ADDR pc = read_pc_pid (pid);
754 unsigned char buf[LINUX_SYSCALL_LEN];
756 request = PTRACE_SINGLESTEP;
758 /* Returning from a signal trampoline is done by calling a
759 special system call (sigreturn or rt_sigreturn, see
760 i386-linux-tdep.c for more information). This system call
761 restores the registers that were saved when the signal was
762 raised, including %eflags. That means that single-stepping
763 won't work. Instead, we'll have to modify the signal context
764 that's about to be restored, and set the trace flag there. */
766 /* First check if PC is at a system call. */
767 if (read_memory_nobpt (pc, (char *) buf, LINUX_SYSCALL_LEN) == 0
768 && memcmp (buf, linux_syscall, LINUX_SYSCALL_LEN) == 0)
770 int syscall = read_register_pid (LINUX_SYSCALL_REGNUM, pid);
772 /* Then check the system call number. */
773 if (syscall == SYS_sigreturn || syscall == SYS_rt_sigreturn)
775 CORE_ADDR sp = read_register (SP_REGNUM);
777 unsigned long int eflags;
779 if (syscall == SYS_rt_sigreturn)
780 addr = read_memory_integer (sp + 8, 4) + 20;
782 /* Set the trace flag in the context that's about to be
784 addr += LINUX_SIGCONTEXT_EFLAGS_OFFSET;
785 read_memory (addr, (char *) &eflags, 4);
787 write_memory (addr, (char *) &eflags, 4);
792 if (ptrace (request, pid, 0, target_signal_to_host (signal)) == -1)
793 perror_with_name ("ptrace");
797 /* Calling functions in shared libraries. */
798 /* FIXME: kettenis/2000-03-05: Doesn't this belong in a
799 target-dependent file? The function
800 `i386_linux_skip_solib_resolver' is mentioned in
801 `config/i386/tm-linux.h'. */
803 /* Find the minimal symbol named NAME, and return both the minsym
804 struct and its objfile. This probably ought to be in minsym.c, but
805 everything there is trying to deal with things like C++ and
806 SOFUN_ADDRESS_MAYBE_TURQUOISE, ... Since this is so simple, it may
807 be considered too special-purpose for general consumption. */
809 static struct minimal_symbol *
810 find_minsym_and_objfile (char *name, struct objfile **objfile_p)
812 struct objfile *objfile;
814 ALL_OBJFILES (objfile)
816 struct minimal_symbol *msym;
818 ALL_OBJFILE_MSYMBOLS (objfile, msym)
820 if (SYMBOL_NAME (msym)
821 && STREQ (SYMBOL_NAME (msym), name))
823 *objfile_p = objfile;
833 skip_hurd_resolver (CORE_ADDR pc)
835 /* The HURD dynamic linker is part of the GNU C library, so many
836 GNU/Linux distributions use it. (All ELF versions, as far as I
837 know.) An unresolved PLT entry points to "_dl_runtime_resolve",
838 which calls "fixup" to patch the PLT, and then passes control to
841 We look for the symbol `_dl_runtime_resolve', and find `fixup' in
842 the same objfile. If we are at the entry point of `fixup', then
843 we set a breakpoint at the return address (at the top of the
844 stack), and continue.
846 It's kind of gross to do all these checks every time we're
847 called, since they don't change once the executable has gotten
848 started. But this is only a temporary hack --- upcoming versions
849 of Linux will provide a portable, efficient interface for
850 debugging programs that use shared libraries. */
852 struct objfile *objfile;
853 struct minimal_symbol *resolver
854 = find_minsym_and_objfile ("_dl_runtime_resolve", &objfile);
858 struct minimal_symbol *fixup
859 = lookup_minimal_symbol ("fixup", 0, objfile);
861 if (fixup && SYMBOL_VALUE_ADDRESS (fixup) == pc)
862 return (SAVED_PC_AFTER_CALL (get_current_frame ()));
868 /* See the comments for SKIP_SOLIB_RESOLVER at the top of infrun.c.
870 1) decides whether a PLT has sent us into the linker to resolve
871 a function reference, and
872 2) if so, tells us where to set a temporary breakpoint that will
873 trigger when the dynamic linker is done. */
876 i386_linux_skip_solib_resolver (CORE_ADDR pc)
880 /* Plug in functions for other kinds of resolvers here. */
881 result = skip_hurd_resolver (pc);
889 /* Register that we are able to handle Linux ELF core file formats. */
891 static struct core_fns linux_elf_core_fns =
893 bfd_target_elf_flavour, /* core_flavour */
894 default_check_format, /* check_format */
895 default_core_sniffer, /* core_sniffer */
896 fetch_core_registers, /* core_read_registers */
901 _initialize_i386_linux_nat (void)
903 add_core_fns (&linux_elf_core_fns);