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."),
166 gdbarch_register_name (current_gdbarch, regno),
167 regno, safe_strerror (errno));
169 regcache_raw_supply (regcache, regno, &val);
172 /* Store one register. */
175 store_register (const struct regcache *regcache, int regno)
180 gdb_assert (!have_ptrace_getregs);
181 if (regmap[regno] == -1)
184 /* GNU/Linux LWP ID's are process ID's. */
185 tid = TIDGET (inferior_ptid);
187 tid = PIDGET (inferior_ptid); /* Not a threaded program. */
190 regcache_raw_collect (regcache, regno, &val);
191 ptrace (PTRACE_POKEUSER, tid, 4 * regmap[regno], val);
193 error (_("Couldn't write register %s (#%d): %s."),
194 gdbarch_register_name (current_gdbarch, regno),
195 regno, safe_strerror (errno));
199 /* Transfering the general-purpose registers between GDB, inferiors
202 /* Fill GDB's register array with the general-purpose register values
206 supply_gregset (struct regcache *regcache, const elf_gregset_t *gregsetp)
208 const elf_greg_t *regp = (const elf_greg_t *) gregsetp;
211 for (i = 0; i < I386_NUM_GREGS; i++)
212 regcache_raw_supply (regcache, i, regp + regmap[i]);
214 if (I386_LINUX_ORIG_EAX_REGNUM < gdbarch_num_regs (current_gdbarch))
215 regcache_raw_supply (regcache, I386_LINUX_ORIG_EAX_REGNUM,
219 /* Fill register REGNO (if it is a general-purpose register) in
220 *GREGSETPS with the value in GDB's register array. If REGNO is -1,
221 do this for all registers. */
224 fill_gregset (const struct regcache *regcache,
225 elf_gregset_t *gregsetp, int regno)
227 elf_greg_t *regp = (elf_greg_t *) gregsetp;
230 for (i = 0; i < I386_NUM_GREGS; i++)
231 if (regno == -1 || regno == i)
232 regcache_raw_collect (regcache, i, regp + regmap[i]);
234 if ((regno == -1 || regno == I386_LINUX_ORIG_EAX_REGNUM)
235 && I386_LINUX_ORIG_EAX_REGNUM < gdbarch_num_regs (current_gdbarch))
236 regcache_raw_collect (regcache, I386_LINUX_ORIG_EAX_REGNUM,
240 #ifdef HAVE_PTRACE_GETREGS
242 /* Fetch all general-purpose registers from process/thread TID and
243 store their values in GDB's register array. */
246 fetch_regs (struct regcache *regcache, int tid)
250 if (ptrace (PTRACE_GETREGS, tid, 0, (int) ®s) < 0)
254 /* The kernel we're running on doesn't support the GETREGS
255 request. Reset `have_ptrace_getregs'. */
256 have_ptrace_getregs = 0;
260 perror_with_name (_("Couldn't get registers"));
263 supply_gregset (regcache, (const elf_gregset_t *) ®s);
266 /* Store all valid general-purpose registers in GDB's register array
267 into the process/thread specified by TID. */
270 store_regs (const struct regcache *regcache, int tid, int regno)
274 if (ptrace (PTRACE_GETREGS, tid, 0, (int) ®s) < 0)
275 perror_with_name (_("Couldn't get registers"));
277 fill_gregset (regcache, ®s, regno);
279 if (ptrace (PTRACE_SETREGS, tid, 0, (int) ®s) < 0)
280 perror_with_name (_("Couldn't write registers"));
285 static void fetch_regs (struct regcache *regcache, int tid) {}
286 static void store_regs (const struct regcache *regcache, int tid, int regno) {}
291 /* Transfering floating-point registers between GDB, inferiors and cores. */
293 /* Fill GDB's register array with the floating-point register values in
297 supply_fpregset (struct regcache *regcache, const elf_fpregset_t *fpregsetp)
299 i387_supply_fsave (regcache, -1, fpregsetp);
302 /* Fill register REGNO (if it is a floating-point register) in
303 *FPREGSETP with the value in GDB's register array. If REGNO is -1,
304 do this for all registers. */
307 fill_fpregset (const struct regcache *regcache,
308 elf_fpregset_t *fpregsetp, int regno)
310 i387_collect_fsave (regcache, regno, fpregsetp);
313 #ifdef HAVE_PTRACE_GETREGS
315 /* Fetch all floating-point registers from process/thread TID and store
316 thier values in GDB's register array. */
319 fetch_fpregs (struct regcache *regcache, int tid)
321 elf_fpregset_t fpregs;
323 if (ptrace (PTRACE_GETFPREGS, tid, 0, (int) &fpregs) < 0)
324 perror_with_name (_("Couldn't get floating point status"));
326 supply_fpregset (regcache, (const elf_fpregset_t *) &fpregs);
329 /* Store all valid floating-point registers in GDB's register array
330 into the process/thread specified by TID. */
333 store_fpregs (const struct regcache *regcache, int tid, int regno)
335 elf_fpregset_t fpregs;
337 if (ptrace (PTRACE_GETFPREGS, tid, 0, (int) &fpregs) < 0)
338 perror_with_name (_("Couldn't get floating point status"));
340 fill_fpregset (regcache, &fpregs, regno);
342 if (ptrace (PTRACE_SETFPREGS, tid, 0, (int) &fpregs) < 0)
343 perror_with_name (_("Couldn't write floating point status"));
348 static void fetch_fpregs (struct regcache *regcache, int tid) {}
349 static void store_fpregs (const struct regcache *regcache, int tid, int regno) {}
354 /* Transfering floating-point and SSE registers to and from GDB. */
356 #ifdef HAVE_PTRACE_GETFPXREGS
358 /* Fill GDB's register array with the floating-point and SSE register
359 values in *FPXREGSETP. */
362 supply_fpxregset (struct regcache *regcache,
363 const elf_fpxregset_t *fpxregsetp)
365 i387_supply_fxsave (regcache, -1, fpxregsetp);
368 /* Fill register REGNO (if it is a floating-point or SSE register) in
369 *FPXREGSETP with the value in GDB's register array. If REGNO is
370 -1, do this for all registers. */
373 fill_fpxregset (const struct regcache *regcache,
374 elf_fpxregset_t *fpxregsetp, int regno)
376 i387_collect_fxsave (regcache, regno, fpxregsetp);
379 /* Fetch all registers covered by the PTRACE_GETFPXREGS request from
380 process/thread TID and store their values in GDB's register array.
381 Return non-zero if successful, zero otherwise. */
384 fetch_fpxregs (struct regcache *regcache, int tid)
386 elf_fpxregset_t fpxregs;
388 if (! have_ptrace_getfpxregs)
391 if (ptrace (PTRACE_GETFPXREGS, tid, 0, (int) &fpxregs) < 0)
395 have_ptrace_getfpxregs = 0;
399 perror_with_name (_("Couldn't read floating-point and SSE registers"));
402 supply_fpxregset (regcache, (const elf_fpxregset_t *) &fpxregs);
406 /* Store all valid registers in GDB's register array covered by the
407 PTRACE_SETFPXREGS request into the process/thread specified by TID.
408 Return non-zero if successful, zero otherwise. */
411 store_fpxregs (const struct regcache *regcache, int tid, int regno)
413 elf_fpxregset_t fpxregs;
415 if (! have_ptrace_getfpxregs)
418 if (ptrace (PTRACE_GETFPXREGS, tid, 0, &fpxregs) == -1)
422 have_ptrace_getfpxregs = 0;
426 perror_with_name (_("Couldn't read floating-point and SSE registers"));
429 fill_fpxregset (regcache, &fpxregs, regno);
431 if (ptrace (PTRACE_SETFPXREGS, tid, 0, &fpxregs) == -1)
432 perror_with_name (_("Couldn't write floating-point and SSE registers"));
439 static int fetch_fpxregs (struct regcache *regcache, int tid) { return 0; }
440 static int store_fpxregs (const struct regcache *regcache, int tid, int regno) { return 0; }
442 #endif /* HAVE_PTRACE_GETFPXREGS */
445 /* Transferring arbitrary registers between GDB and inferior. */
447 /* Fetch register REGNO from the child process. If REGNO is -1, do
448 this for all registers (including the floating point and SSE
452 i386_linux_fetch_inferior_registers (struct regcache *regcache, int regno)
456 /* Use the old method of peeking around in `struct user' if the
457 GETREGS request isn't available. */
458 if (!have_ptrace_getregs)
462 for (i = 0; i < gdbarch_num_regs (current_gdbarch); i++)
463 if (regno == -1 || regno == i)
464 fetch_register (regcache, i);
469 /* GNU/Linux LWP ID's are process ID's. */
470 tid = TIDGET (inferior_ptid);
472 tid = PIDGET (inferior_ptid); /* Not a threaded program. */
474 /* Use the PTRACE_GETFPXREGS request whenever possible, since it
475 transfers more registers in one system call, and we'll cache the
476 results. But remember that fetch_fpxregs can fail, and return
480 fetch_regs (regcache, tid);
482 /* The call above might reset `have_ptrace_getregs'. */
483 if (!have_ptrace_getregs)
485 i386_linux_fetch_inferior_registers (regcache, regno);
489 if (fetch_fpxregs (regcache, tid))
491 fetch_fpregs (regcache, tid);
495 if (GETREGS_SUPPLIES (regno))
497 fetch_regs (regcache, tid);
501 if (GETFPXREGS_SUPPLIES (regno))
503 if (fetch_fpxregs (regcache, tid))
506 /* Either our processor or our kernel doesn't support the SSE
507 registers, so read the FP registers in the traditional way,
508 and fill the SSE registers with dummy values. It would be
509 more graceful to handle differences in the register set using
510 gdbarch. Until then, this will at least make things work
512 fetch_fpregs (regcache, tid);
516 internal_error (__FILE__, __LINE__,
517 _("Got request for bad register number %d."), regno);
520 /* Store register REGNO back into the child process. If REGNO is -1,
521 do this for all registers (including the floating point and SSE
524 i386_linux_store_inferior_registers (struct regcache *regcache, int regno)
528 /* Use the old method of poking around in `struct user' if the
529 SETREGS request isn't available. */
530 if (!have_ptrace_getregs)
534 for (i = 0; i < gdbarch_num_regs (current_gdbarch); i++)
535 if (regno == -1 || regno == i)
536 store_register (regcache, i);
541 /* GNU/Linux LWP ID's are process ID's. */
542 tid = TIDGET (inferior_ptid);
544 tid = PIDGET (inferior_ptid); /* Not a threaded program. */
546 /* Use the PTRACE_SETFPXREGS requests whenever possible, since it
547 transfers more registers in one system call. But remember that
548 store_fpxregs can fail, and return zero. */
551 store_regs (regcache, tid, regno);
552 if (store_fpxregs (regcache, tid, regno))
554 store_fpregs (regcache, tid, regno);
558 if (GETREGS_SUPPLIES (regno))
560 store_regs (regcache, tid, regno);
564 if (GETFPXREGS_SUPPLIES (regno))
566 if (store_fpxregs (regcache, tid, regno))
569 /* Either our processor or our kernel doesn't support the SSE
570 registers, so just write the FP registers in the traditional
572 store_fpregs (regcache, tid, regno);
576 internal_error (__FILE__, __LINE__,
577 _("Got request to store bad register number %d."), regno);
581 /* Support for debug registers. */
584 i386_linux_dr_get (int regnum)
589 /* FIXME: kettenis/2001-01-29: It's not clear what we should do with
590 multi-threaded processes here. For now, pretend there is just
592 tid = PIDGET (inferior_ptid);
594 /* FIXME: kettenis/2001-03-27: Calling perror_with_name if the
595 ptrace call fails breaks debugging remote targets. The correct
596 way to fix this is to add the hardware breakpoint and watchpoint
597 stuff to the target vector. For now, just return zero if the
598 ptrace call fails. */
600 value = ptrace (PTRACE_PEEKUSER, tid,
601 offsetof (struct user, u_debugreg[regnum]), 0);
604 perror_with_name (_("Couldn't read debug register"));
613 i386_linux_dr_set (int regnum, unsigned long value)
617 /* FIXME: kettenis/2001-01-29: It's not clear what we should do with
618 multi-threaded processes here. For now, pretend there is just
620 tid = PIDGET (inferior_ptid);
623 ptrace (PTRACE_POKEUSER, tid,
624 offsetof (struct user, u_debugreg[regnum]), value);
626 perror_with_name (_("Couldn't write debug register"));
630 i386_linux_dr_set_control (unsigned long control)
632 i386_linux_dr_set (DR_CONTROL, control);
636 i386_linux_dr_set_addr (int regnum, CORE_ADDR addr)
638 gdb_assert (regnum >= 0 && regnum <= DR_LASTADDR - DR_FIRSTADDR);
640 i386_linux_dr_set (DR_FIRSTADDR + regnum, addr);
644 i386_linux_dr_reset_addr (int regnum)
646 gdb_assert (regnum >= 0 && regnum <= DR_LASTADDR - DR_FIRSTADDR);
648 i386_linux_dr_set (DR_FIRSTADDR + regnum, 0L);
652 i386_linux_dr_get_status (void)
654 return i386_linux_dr_get (DR_STATUS);
658 /* Called by libthread_db. Returns a pointer to the thread local
659 storage (or its descriptor). */
662 ps_get_thread_area (const struct ps_prochandle *ph,
663 lwpid_t lwpid, int idx, void **base)
665 /* NOTE: cagney/2003-08-26: The definition of this buffer is found
666 in the kernel header <asm-i386/ldt.h>. It, after padding, is 4 x
667 4 byte integers in size: `entry_number', `base_addr', `limit',
668 and a bunch of status bits.
670 The values returned by this ptrace call should be part of the
671 regcache buffer, and ps_get_thread_area should channel its
672 request through the regcache. That way remote targets could
673 provide the value using the remote protocol and not this direct
676 Is this function needed? I'm guessing that the `base' is the
677 address of a a descriptor that libthread_db uses to find the
678 thread local address base that GDB needs. Perhaps that
679 descriptor is defined by the ABI. Anyway, given that
680 libthread_db calls this function without prompting (gdb
681 requesting tls base) I guess it needs info in there anyway. */
682 unsigned int desc[4];
683 gdb_assert (sizeof (int) == 4);
685 #ifndef PTRACE_GET_THREAD_AREA
686 #define PTRACE_GET_THREAD_AREA 25
689 if (ptrace (PTRACE_GET_THREAD_AREA, lwpid,
690 (void *) idx, (unsigned long) &desc) < 0)
693 *(int *)base = desc[1];
698 /* The instruction for a GNU/Linux system call is:
702 static const unsigned char linux_syscall[] = { 0xcd, 0x80 };
704 #define LINUX_SYSCALL_LEN (sizeof linux_syscall)
706 /* The system call number is stored in the %eax register. */
707 #define LINUX_SYSCALL_REGNUM I386_EAX_REGNUM
709 /* We are specifically interested in the sigreturn and rt_sigreturn
712 #ifndef SYS_sigreturn
713 #define SYS_sigreturn 0x77
715 #ifndef SYS_rt_sigreturn
716 #define SYS_rt_sigreturn 0xad
719 /* Offset to saved processor flags, from <asm/sigcontext.h>. */
720 #define LINUX_SIGCONTEXT_EFLAGS_OFFSET (64)
722 /* Resume execution of the inferior process.
723 If STEP is nonzero, single-step it.
724 If SIGNAL is nonzero, give it that signal. */
727 i386_linux_resume (ptid_t ptid, int step, enum target_signal signal)
729 int pid = PIDGET (ptid);
731 int request = PTRACE_CONT;
734 /* Resume all threads. */
735 /* I think this only gets used in the non-threaded case, where "resume
736 all threads" and "resume inferior_ptid" are the same. */
737 pid = PIDGET (inferior_ptid);
741 struct regcache *regcache = get_thread_regcache (pid_to_ptid (pid));
743 gdb_byte buf[LINUX_SYSCALL_LEN];
745 request = PTRACE_SINGLESTEP;
747 regcache_cooked_read_unsigned (regcache, PC_REGNUM, &pc);
749 /* Returning from a signal trampoline is done by calling a
750 special system call (sigreturn or rt_sigreturn, see
751 i386-linux-tdep.c for more information). This system call
752 restores the registers that were saved when the signal was
753 raised, including %eflags. That means that single-stepping
754 won't work. Instead, we'll have to modify the signal context
755 that's about to be restored, and set the trace flag there. */
757 /* First check if PC is at a system call. */
758 if (read_memory_nobpt (pc, buf, LINUX_SYSCALL_LEN) == 0
759 && memcmp (buf, linux_syscall, LINUX_SYSCALL_LEN) == 0)
762 regcache_cooked_read_unsigned (regcache,
763 LINUX_SYSCALL_REGNUM, &syscall);
765 /* Then check the system call number. */
766 if (syscall == SYS_sigreturn || syscall == SYS_rt_sigreturn)
769 unsigned long int eflags;
771 regcache_cooked_read_unsigned (regcache, I386_ESP_REGNUM, &sp);
772 if (syscall == SYS_rt_sigreturn)
773 addr = read_memory_integer (sp + 8, 4) + 20;
777 /* Set the trace flag in the context that's about to be
779 addr += LINUX_SIGCONTEXT_EFLAGS_OFFSET;
780 read_memory (addr, (gdb_byte *) &eflags, 4);
782 write_memory (addr, (gdb_byte *) &eflags, 4);
787 if (ptrace (request, pid, 0, target_signal_to_host (signal)) == -1)
788 perror_with_name (("ptrace"));
791 static void (*super_post_startup_inferior) (ptid_t ptid);
794 i386_linux_child_post_startup_inferior (ptid_t ptid)
796 i386_cleanup_dregs ();
797 super_post_startup_inferior (ptid);
801 _initialize_i386_linux_nat (void)
803 struct target_ops *t;
805 /* Fill in the generic GNU/Linux methods. */
808 /* Override the default ptrace resume method. */
809 t->to_resume = i386_linux_resume;
811 /* Override the GNU/Linux inferior startup hook. */
812 super_post_startup_inferior = t->to_post_startup_inferior;
813 t->to_post_startup_inferior = i386_linux_child_post_startup_inferior;
815 /* Add our register access methods. */
816 t->to_fetch_registers = i386_linux_fetch_inferior_registers;
817 t->to_store_registers = i386_linux_store_inferior_registers;
819 /* Register the target. */
820 linux_nat_add_target (t);