1 /* Native-dependent code for GNU/Linux i386.
3 Copyright (C) 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007
4 Free Software Foundation, Inc.
6 This file is part of GDB.
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 51 Franklin Street, Fifth Floor,
21 Boston, MA 02110-1301, USA. */
28 #include "linux-nat.h"
30 #include "gdb_assert.h"
31 #include "gdb_string.h"
32 #include <sys/ptrace.h>
34 #include <sys/procfs.h>
44 #ifdef HAVE_SYS_DEBUGREG_H
45 #include <sys/debugreg.h>
49 #define DR_FIRSTADDR 0
64 /* Prototypes for supply_gregset etc. */
67 #include "i387-tdep.h"
68 #include "i386-tdep.h"
69 #include "i386-linux-tdep.h"
71 /* Defines ps_err_e, struct ps_prochandle. */
72 #include "gdb_proc_service.h"
75 /* The register sets used in GNU/Linux ELF core-dumps are identical to
76 the register sets in `struct user' that is used for a.out
77 core-dumps, and is also used by `ptrace'. The corresponding types
78 are `elf_gregset_t' for the general-purpose registers (with
79 `elf_greg_t' the type of a single GP register) and `elf_fpregset_t'
80 for the floating-point registers.
82 Those types used to be available under the names `gregset_t' and
83 `fpregset_t' too, and this file used those names in the past. But
84 those names are now used for the register sets used in the
85 `mcontext_t' type, and have a different size and layout. */
87 /* Mapping between the general-purpose registers in `struct user'
88 format and GDB's register array layout. */
95 -1, -1, -1, -1, /* st0, st1, st2, st3 */
96 -1, -1, -1, -1, /* st4, st5, st6, st7 */
97 -1, -1, -1, -1, /* fctrl, fstat, ftag, fiseg */
98 -1, -1, -1, -1, /* fioff, foseg, fooff, fop */
99 -1, -1, -1, -1, /* xmm0, xmm1, xmm2, xmm3 */
100 -1, -1, -1, -1, /* xmm4, xmm5, xmm6, xmm6 */
105 /* Which ptrace request retrieves which registers?
106 These apply to the corresponding SET requests as well. */
108 #define GETREGS_SUPPLIES(regno) \
109 ((0 <= (regno) && (regno) <= 15) || (regno) == I386_LINUX_ORIG_EAX_REGNUM)
111 #define GETFPXREGS_SUPPLIES(regno) \
112 (I386_ST0_REGNUM <= (regno) && (regno) < I386_SSE_NUM_REGS)
114 /* Does the current host support the GETREGS request? */
115 int have_ptrace_getregs =
116 #ifdef HAVE_PTRACE_GETREGS
123 /* Does the current host support the GETFPXREGS request? The header
124 file may or may not define it, and even if it is defined, the
125 kernel will return EIO if it's running on a pre-SSE processor.
127 My instinct is to attach this to some architecture- or
128 target-specific data structure, but really, a particular GDB
129 process can only run on top of one kernel at a time. So it's okay
130 for this to be a simple variable. */
131 int have_ptrace_getfpxregs =
132 #ifdef HAVE_PTRACE_GETFPXREGS
140 /* Accessing registers through the U area, one at a time. */
142 /* Fetch one register. */
145 fetch_register (struct regcache *regcache, int regno)
150 gdb_assert (!have_ptrace_getregs);
151 if (regmap[regno] == -1)
153 regcache_raw_supply (regcache, regno, NULL);
157 /* GNU/Linux LWP ID's are process ID's. */
158 tid = TIDGET (inferior_ptid);
160 tid = PIDGET (inferior_ptid); /* Not a threaded program. */
163 val = ptrace (PTRACE_PEEKUSER, tid, 4 * regmap[regno], 0);
165 error (_("Couldn't read register %s (#%d): %s."), REGISTER_NAME (regno),
166 regno, safe_strerror (errno));
168 regcache_raw_supply (regcache, regno, &val);
171 /* Store one register. */
174 store_register (const struct regcache *regcache, int regno)
179 gdb_assert (!have_ptrace_getregs);
180 if (regmap[regno] == -1)
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 regcache_raw_collect (regcache, regno, &val);
190 ptrace (PTRACE_POKEUSER, tid, 4 * regmap[regno], val);
192 error (_("Couldn't write register %s (#%d): %s."), REGISTER_NAME (regno),
193 regno, safe_strerror (errno));
197 /* Transfering the general-purpose registers between GDB, inferiors
200 /* Fill GDB's register array with the general-purpose register values
204 supply_gregset (struct regcache *regcache, const elf_gregset_t *gregsetp)
206 const elf_greg_t *regp = (const elf_greg_t *) gregsetp;
209 for (i = 0; i < I386_NUM_GREGS; i++)
210 regcache_raw_supply (regcache, i, regp + regmap[i]);
212 if (I386_LINUX_ORIG_EAX_REGNUM < NUM_REGS)
213 regcache_raw_supply (regcache, I386_LINUX_ORIG_EAX_REGNUM,
217 /* Fill register REGNO (if it is a general-purpose register) in
218 *GREGSETPS with the value in GDB's register array. If REGNO is -1,
219 do this for all registers. */
222 fill_gregset (const struct regcache *regcache,
223 elf_gregset_t *gregsetp, int regno)
225 elf_greg_t *regp = (elf_greg_t *) gregsetp;
228 for (i = 0; i < I386_NUM_GREGS; i++)
229 if (regno == -1 || regno == i)
230 regcache_raw_collect (regcache, i, regp + regmap[i]);
232 if ((regno == -1 || regno == I386_LINUX_ORIG_EAX_REGNUM)
233 && I386_LINUX_ORIG_EAX_REGNUM < NUM_REGS)
234 regcache_raw_collect (regcache, I386_LINUX_ORIG_EAX_REGNUM,
238 #ifdef HAVE_PTRACE_GETREGS
240 /* Fetch all general-purpose registers from process/thread TID and
241 store their values in GDB's register array. */
244 fetch_regs (struct regcache *regcache, int tid)
248 if (ptrace (PTRACE_GETREGS, tid, 0, (int) ®s) < 0)
252 /* The kernel we're running on doesn't support the GETREGS
253 request. Reset `have_ptrace_getregs'. */
254 have_ptrace_getregs = 0;
258 perror_with_name (_("Couldn't get registers"));
261 supply_gregset (regcache, (const elf_gregset_t *) ®s);
264 /* Store all valid general-purpose registers in GDB's register array
265 into the process/thread specified by TID. */
268 store_regs (const struct regcache *regcache, int tid, int regno)
272 if (ptrace (PTRACE_GETREGS, tid, 0, (int) ®s) < 0)
273 perror_with_name (_("Couldn't get registers"));
275 fill_gregset (regcache, ®s, regno);
277 if (ptrace (PTRACE_SETREGS, tid, 0, (int) ®s) < 0)
278 perror_with_name (_("Couldn't write registers"));
283 static void fetch_regs (struct regcache *regcache, int tid) {}
284 static void store_regs (const struct regcache *regcache, int tid, int regno) {}
289 /* Transfering floating-point registers between GDB, inferiors and cores. */
291 /* Fill GDB's register array with the floating-point register values in
295 supply_fpregset (struct regcache *regcache, const elf_fpregset_t *fpregsetp)
297 i387_supply_fsave (regcache, -1, fpregsetp);
300 /* Fill register REGNO (if it is a floating-point register) in
301 *FPREGSETP with the value in GDB's register array. If REGNO is -1,
302 do this for all registers. */
305 fill_fpregset (const struct regcache *regcache,
306 elf_fpregset_t *fpregsetp, int regno)
308 i387_collect_fsave (regcache, regno, fpregsetp);
311 #ifdef HAVE_PTRACE_GETREGS
313 /* Fetch all floating-point registers from process/thread TID and store
314 thier values in GDB's register array. */
317 fetch_fpregs (struct regcache *regcache, int tid)
319 elf_fpregset_t fpregs;
321 if (ptrace (PTRACE_GETFPREGS, tid, 0, (int) &fpregs) < 0)
322 perror_with_name (_("Couldn't get floating point status"));
324 supply_fpregset (regcache, (const elf_fpregset_t *) &fpregs);
327 /* Store all valid floating-point registers in GDB's register array
328 into the process/thread specified by TID. */
331 store_fpregs (const struct regcache *regcache, int tid, int regno)
333 elf_fpregset_t fpregs;
335 if (ptrace (PTRACE_GETFPREGS, tid, 0, (int) &fpregs) < 0)
336 perror_with_name (_("Couldn't get floating point status"));
338 fill_fpregset (regcache, &fpregs, regno);
340 if (ptrace (PTRACE_SETFPREGS, tid, 0, (int) &fpregs) < 0)
341 perror_with_name (_("Couldn't write floating point status"));
346 static void fetch_fpregs (struct regcache *regcache, int tid) {}
347 static void store_fpregs (const struct regcache *regcache, int tid, int regno) {}
352 /* Transfering floating-point and SSE registers to and from GDB. */
354 #ifdef HAVE_PTRACE_GETFPXREGS
356 /* Fill GDB's register array with the floating-point and SSE register
357 values in *FPXREGSETP. */
360 supply_fpxregset (struct regcache *regcache,
361 const elf_fpxregset_t *fpxregsetp)
363 i387_supply_fxsave (regcache, -1, fpxregsetp);
366 /* Fill register REGNO (if it is a floating-point or SSE register) in
367 *FPXREGSETP with the value in GDB's register array. If REGNO is
368 -1, do this for all registers. */
371 fill_fpxregset (const struct regcache *regcache,
372 elf_fpxregset_t *fpxregsetp, int regno)
374 i387_collect_fxsave (regcache, regno, fpxregsetp);
377 /* Fetch all registers covered by the PTRACE_GETFPXREGS request from
378 process/thread TID and store their values in GDB's register array.
379 Return non-zero if successful, zero otherwise. */
382 fetch_fpxregs (struct regcache *regcache, int tid)
384 elf_fpxregset_t fpxregs;
386 if (! have_ptrace_getfpxregs)
389 if (ptrace (PTRACE_GETFPXREGS, tid, 0, (int) &fpxregs) < 0)
393 have_ptrace_getfpxregs = 0;
397 perror_with_name (_("Couldn't read floating-point and SSE registers"));
400 supply_fpxregset (regcache, (const elf_fpxregset_t *) &fpxregs);
404 /* Store all valid registers in GDB's register array covered by the
405 PTRACE_SETFPXREGS request into the process/thread specified by TID.
406 Return non-zero if successful, zero otherwise. */
409 store_fpxregs (const struct regcache *regcache, int tid, int regno)
411 elf_fpxregset_t fpxregs;
413 if (! have_ptrace_getfpxregs)
416 if (ptrace (PTRACE_GETFPXREGS, tid, 0, &fpxregs) == -1)
420 have_ptrace_getfpxregs = 0;
424 perror_with_name (_("Couldn't read floating-point and SSE registers"));
427 fill_fpxregset (regcache, &fpxregs, regno);
429 if (ptrace (PTRACE_SETFPXREGS, tid, 0, &fpxregs) == -1)
430 perror_with_name (_("Couldn't write floating-point and SSE registers"));
437 static int fetch_fpxregs (struct regcache *regcache, int tid) { return 0; }
438 static int store_fpxregs (const struct regcache *regcache, int tid, int regno) { return 0; }
440 #endif /* HAVE_PTRACE_GETFPXREGS */
443 /* Transferring arbitrary registers between GDB and inferior. */
445 /* Fetch register REGNO from the child process. If REGNO is -1, do
446 this for all registers (including the floating point and SSE
450 i386_linux_fetch_inferior_registers (struct regcache *regcache, int regno)
454 /* Use the old method of peeking around in `struct user' if the
455 GETREGS request isn't available. */
456 if (!have_ptrace_getregs)
460 for (i = 0; i < NUM_REGS; i++)
461 if (regno == -1 || regno == i)
462 fetch_register (regcache, i);
467 /* GNU/Linux LWP ID's are process ID's. */
468 tid = TIDGET (inferior_ptid);
470 tid = PIDGET (inferior_ptid); /* Not a threaded program. */
472 /* Use the PTRACE_GETFPXREGS request whenever possible, since it
473 transfers more registers in one system call, and we'll cache the
474 results. But remember that fetch_fpxregs can fail, and return
478 fetch_regs (regcache, tid);
480 /* The call above might reset `have_ptrace_getregs'. */
481 if (!have_ptrace_getregs)
483 i386_linux_fetch_inferior_registers (regcache, regno);
487 if (fetch_fpxregs (regcache, tid))
489 fetch_fpregs (regcache, tid);
493 if (GETREGS_SUPPLIES (regno))
495 fetch_regs (regcache, tid);
499 if (GETFPXREGS_SUPPLIES (regno))
501 if (fetch_fpxregs (regcache, tid))
504 /* Either our processor or our kernel doesn't support the SSE
505 registers, so read the FP registers in the traditional way,
506 and fill the SSE registers with dummy values. It would be
507 more graceful to handle differences in the register set using
508 gdbarch. Until then, this will at least make things work
510 fetch_fpregs (regcache, tid);
514 internal_error (__FILE__, __LINE__,
515 _("Got request for bad register number %d."), regno);
518 /* Store register REGNO back into the child process. If REGNO is -1,
519 do this for all registers (including the floating point and SSE
522 i386_linux_store_inferior_registers (struct regcache *regcache, int regno)
526 /* Use the old method of poking around in `struct user' if the
527 SETREGS request isn't available. */
528 if (!have_ptrace_getregs)
532 for (i = 0; i < NUM_REGS; i++)
533 if (regno == -1 || regno == i)
534 store_register (regcache, i);
539 /* GNU/Linux LWP ID's are process ID's. */
540 tid = TIDGET (inferior_ptid);
542 tid = PIDGET (inferior_ptid); /* Not a threaded program. */
544 /* Use the PTRACE_SETFPXREGS requests whenever possible, since it
545 transfers more registers in one system call. But remember that
546 store_fpxregs can fail, and return zero. */
549 store_regs (regcache, tid, regno);
550 if (store_fpxregs (regcache, tid, regno))
552 store_fpregs (regcache, tid, regno);
556 if (GETREGS_SUPPLIES (regno))
558 store_regs (regcache, tid, regno);
562 if (GETFPXREGS_SUPPLIES (regno))
564 if (store_fpxregs (regcache, tid, regno))
567 /* Either our processor or our kernel doesn't support the SSE
568 registers, so just write the FP registers in the traditional
570 store_fpregs (regcache, tid, regno);
574 internal_error (__FILE__, __LINE__,
575 _("Got request to store bad register number %d."), regno);
579 /* Support for debug registers. */
582 i386_linux_dr_get (int regnum)
587 /* FIXME: kettenis/2001-01-29: It's not clear what we should do with
588 multi-threaded processes here. For now, pretend there is just
590 tid = PIDGET (inferior_ptid);
592 /* FIXME: kettenis/2001-03-27: Calling perror_with_name if the
593 ptrace call fails breaks debugging remote targets. The correct
594 way to fix this is to add the hardware breakpoint and watchpoint
595 stuff to the target vector. For now, just return zero if the
596 ptrace call fails. */
598 value = ptrace (PTRACE_PEEKUSER, tid,
599 offsetof (struct user, u_debugreg[regnum]), 0);
602 perror_with_name (_("Couldn't read debug register"));
611 i386_linux_dr_set (int regnum, unsigned long value)
615 /* FIXME: kettenis/2001-01-29: It's not clear what we should do with
616 multi-threaded processes here. For now, pretend there is just
618 tid = PIDGET (inferior_ptid);
621 ptrace (PTRACE_POKEUSER, tid,
622 offsetof (struct user, u_debugreg[regnum]), value);
624 perror_with_name (_("Couldn't write debug register"));
628 i386_linux_dr_set_control (unsigned long control)
630 i386_linux_dr_set (DR_CONTROL, control);
634 i386_linux_dr_set_addr (int regnum, CORE_ADDR addr)
636 gdb_assert (regnum >= 0 && regnum <= DR_LASTADDR - DR_FIRSTADDR);
638 i386_linux_dr_set (DR_FIRSTADDR + regnum, addr);
642 i386_linux_dr_reset_addr (int regnum)
644 gdb_assert (regnum >= 0 && regnum <= DR_LASTADDR - DR_FIRSTADDR);
646 i386_linux_dr_set (DR_FIRSTADDR + regnum, 0L);
650 i386_linux_dr_get_status (void)
652 return i386_linux_dr_get (DR_STATUS);
656 /* Called by libthread_db. Returns a pointer to the thread local
657 storage (or its descriptor). */
660 ps_get_thread_area (const struct ps_prochandle *ph,
661 lwpid_t lwpid, int idx, void **base)
663 /* NOTE: cagney/2003-08-26: The definition of this buffer is found
664 in the kernel header <asm-i386/ldt.h>. It, after padding, is 4 x
665 4 byte integers in size: `entry_number', `base_addr', `limit',
666 and a bunch of status bits.
668 The values returned by this ptrace call should be part of the
669 regcache buffer, and ps_get_thread_area should channel its
670 request through the regcache. That way remote targets could
671 provide the value using the remote protocol and not this direct
674 Is this function needed? I'm guessing that the `base' is the
675 address of a a descriptor that libthread_db uses to find the
676 thread local address base that GDB needs. Perhaps that
677 descriptor is defined by the ABI. Anyway, given that
678 libthread_db calls this function without prompting (gdb
679 requesting tls base) I guess it needs info in there anyway. */
680 unsigned int desc[4];
681 gdb_assert (sizeof (int) == 4);
683 #ifndef PTRACE_GET_THREAD_AREA
684 #define PTRACE_GET_THREAD_AREA 25
687 if (ptrace (PTRACE_GET_THREAD_AREA, lwpid,
688 (void *) idx, (unsigned long) &desc) < 0)
691 *(int *)base = desc[1];
696 /* The instruction for a GNU/Linux system call is:
700 static const unsigned char linux_syscall[] = { 0xcd, 0x80 };
702 #define LINUX_SYSCALL_LEN (sizeof linux_syscall)
704 /* The system call number is stored in the %eax register. */
705 #define LINUX_SYSCALL_REGNUM I386_EAX_REGNUM
707 /* We are specifically interested in the sigreturn and rt_sigreturn
710 #ifndef SYS_sigreturn
711 #define SYS_sigreturn 0x77
713 #ifndef SYS_rt_sigreturn
714 #define SYS_rt_sigreturn 0xad
717 /* Offset to saved processor flags, from <asm/sigcontext.h>. */
718 #define LINUX_SIGCONTEXT_EFLAGS_OFFSET (64)
720 /* Resume execution of the inferior process.
721 If STEP is nonzero, single-step it.
722 If SIGNAL is nonzero, give it that signal. */
725 i386_linux_resume (ptid_t ptid, int step, enum target_signal signal)
727 int pid = PIDGET (ptid);
729 int request = PTRACE_CONT;
732 /* Resume all threads. */
733 /* I think this only gets used in the non-threaded case, where "resume
734 all threads" and "resume inferior_ptid" are the same. */
735 pid = PIDGET (inferior_ptid);
739 CORE_ADDR pc = read_pc_pid (pid_to_ptid (pid));
740 gdb_byte buf[LINUX_SYSCALL_LEN];
742 request = PTRACE_SINGLESTEP;
744 /* Returning from a signal trampoline is done by calling a
745 special system call (sigreturn or rt_sigreturn, see
746 i386-linux-tdep.c for more information). This system call
747 restores the registers that were saved when the signal was
748 raised, including %eflags. That means that single-stepping
749 won't work. Instead, we'll have to modify the signal context
750 that's about to be restored, and set the trace flag there. */
752 /* First check if PC is at a system call. */
753 if (read_memory_nobpt (pc, buf, LINUX_SYSCALL_LEN) == 0
754 && memcmp (buf, linux_syscall, LINUX_SYSCALL_LEN) == 0)
756 int syscall = read_register_pid (LINUX_SYSCALL_REGNUM,
759 /* Then check the system call number. */
760 if (syscall == SYS_sigreturn || syscall == SYS_rt_sigreturn)
762 CORE_ADDR sp = read_register (I386_ESP_REGNUM);
764 unsigned long int eflags;
766 if (syscall == SYS_rt_sigreturn)
767 addr = read_memory_integer (sp + 8, 4) + 20;
769 /* Set the trace flag in the context that's about to be
771 addr += LINUX_SIGCONTEXT_EFLAGS_OFFSET;
772 read_memory (addr, (gdb_byte *) &eflags, 4);
774 write_memory (addr, (gdb_byte *) &eflags, 4);
779 if (ptrace (request, pid, 0, target_signal_to_host (signal)) == -1)
780 perror_with_name (("ptrace"));
783 static void (*super_post_startup_inferior) (ptid_t ptid);
786 i386_linux_child_post_startup_inferior (ptid_t ptid)
788 i386_cleanup_dregs ();
789 super_post_startup_inferior (ptid);
793 _initialize_i386_linux_nat (void)
795 struct target_ops *t;
797 /* Fill in the generic GNU/Linux methods. */
800 /* Override the default ptrace resume method. */
801 t->to_resume = i386_linux_resume;
803 /* Override the GNU/Linux inferior startup hook. */
804 super_post_startup_inferior = t->to_post_startup_inferior;
805 t->to_post_startup_inferior = i386_linux_child_post_startup_inferior;
807 /* Add our register access methods. */
808 t->to_fetch_registers = i386_linux_fetch_inferior_registers;
809 t->to_store_registers = i386_linux_store_inferior_registers;
811 /* Register the target. */
812 linux_nat_add_target (t);