1 /* Native-dependent code for GNU/Linux i386.
3 Copyright 1999, 2000, 2001, 2002, 2003, 2004 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. */
26 #include "linux-nat.h"
28 #include "gdb_assert.h"
29 #include "gdb_string.h"
30 #include <sys/ptrace.h>
32 #include <sys/procfs.h>
42 #ifdef HAVE_SYS_DEBUGREG_H
43 #include <sys/debugreg.h>
47 #define DR_FIRSTADDR 0
62 /* Prototypes for supply_gregset etc. */
65 #include "i387-tdep.h"
66 #include "i386-tdep.h"
67 #include "i386-linux-tdep.h"
69 /* Defines ps_err_e, struct ps_prochandle. */
70 #include "gdb_proc_service.h"
73 /* The register sets used in GNU/Linux ELF core-dumps are identical to
74 the register sets in `struct user' that is used for a.out
75 core-dumps, and is also used by `ptrace'. The corresponding types
76 are `elf_gregset_t' for the general-purpose registers (with
77 `elf_greg_t' the type of a single GP register) and `elf_fpregset_t'
78 for the floating-point registers.
80 Those types used to be available under the names `gregset_t' and
81 `fpregset_t' too, and this file used those names in the past. But
82 those names are now used for the register sets used in the
83 `mcontext_t' type, and have a different size and layout. */
85 /* Mapping between the general-purpose registers in `struct user'
86 format and GDB's register array layout. */
93 -1, -1, -1, -1, /* st0, st1, st2, st3 */
94 -1, -1, -1, -1, /* st4, st5, st6, st7 */
95 -1, -1, -1, -1, /* fctrl, fstat, ftag, fiseg */
96 -1, -1, -1, -1, /* fioff, foseg, fooff, fop */
97 -1, -1, -1, -1, /* xmm0, xmm1, xmm2, xmm3 */
98 -1, -1, -1, -1, /* xmm4, xmm5, xmm6, xmm6 */
103 /* Which ptrace request retrieves which registers?
104 These apply to the corresponding SET requests as well. */
106 #define GETREGS_SUPPLIES(regno) \
107 ((0 <= (regno) && (regno) <= 15) || (regno) == I386_LINUX_ORIG_EAX_REGNUM)
109 #define GETFPXREGS_SUPPLIES(regno) \
110 (I386_ST0_REGNUM <= (regno) && (regno) < I386_SSE_NUM_REGS)
112 /* Does the current host support the GETREGS request? */
113 int have_ptrace_getregs =
114 #ifdef HAVE_PTRACE_GETREGS
121 /* Does the current host support the GETFPXREGS request? The header
122 file may or may not define it, and even if it is defined, the
123 kernel will return EIO if it's running on a pre-SSE processor.
125 My instinct is to attach this to some architecture- or
126 target-specific data structure, but really, a particular GDB
127 process can only run on top of one kernel at a time. So it's okay
128 for this to be a simple variable. */
129 int have_ptrace_getfpxregs =
130 #ifdef HAVE_PTRACE_GETFPXREGS
138 /* Support for the user struct. */
140 /* Return the address of register REGNUM. BLOCKEND is the value of
141 u.u_ar0, which should point to the registers. */
144 register_u_addr (CORE_ADDR blockend, int regnum)
146 return (blockend + 4 * regmap[regnum]);
149 /* Return the size of the user struct. */
154 return (sizeof (struct user));
158 /* Accessing registers through the U area, one at a time. */
160 /* Fetch one register. */
163 fetch_register (int regno)
168 gdb_assert (!have_ptrace_getregs);
169 if (cannot_fetch_register (regno))
171 regcache_raw_supply (current_regcache, regno, NULL);
175 /* GNU/Linux LWP ID's are process ID's. */
176 tid = TIDGET (inferior_ptid);
178 tid = PIDGET (inferior_ptid); /* Not a threaded program. */
181 val = ptrace (PTRACE_PEEKUSER, tid, register_addr (regno, 0), 0);
183 error (_("Couldn't read register %s (#%d): %s."), REGISTER_NAME (regno),
184 regno, safe_strerror (errno));
186 regcache_raw_supply (current_regcache, regno, (gdb_byte *) &val);
189 /* Store one register. */
192 store_register (int regno)
197 gdb_assert (!have_ptrace_getregs);
198 if (cannot_store_register (regno))
201 /* GNU/Linux LWP ID's are process ID's. */
202 tid = TIDGET (inferior_ptid);
204 tid = PIDGET (inferior_ptid); /* Not a threaded program. */
207 regcache_raw_collect (current_regcache, regno, (gdb_byte *) &val);
208 ptrace (PTRACE_POKEUSER, tid, register_addr (regno, 0), val);
210 error (_("Couldn't write register %s (#%d): %s."), REGISTER_NAME (regno),
211 regno, safe_strerror (errno));
215 /* Transfering the general-purpose registers between GDB, inferiors
218 /* Fill GDB's register array with the general-purpose register values
222 supply_gregset (elf_gregset_t *gregsetp)
224 elf_greg_t *regp = (elf_greg_t *) gregsetp;
227 for (i = 0; i < I386_NUM_GREGS; i++)
228 regcache_raw_supply (current_regcache, i, (gdb_byte *) (regp + regmap[i]));
230 if (I386_LINUX_ORIG_EAX_REGNUM < NUM_REGS)
231 regcache_raw_supply (current_regcache, I386_LINUX_ORIG_EAX_REGNUM,
232 (gdb_byte *) (regp + ORIG_EAX));
235 /* Fill register REGNO (if it is a general-purpose register) in
236 *GREGSETPS with the value in GDB's register array. If REGNO is -1,
237 do this for all registers. */
240 fill_gregset (elf_gregset_t *gregsetp, int regno)
242 elf_greg_t *regp = (elf_greg_t *) gregsetp;
245 for (i = 0; i < I386_NUM_GREGS; i++)
246 if (regno == -1 || regno == i)
247 regcache_raw_collect (current_regcache, i, (gdb_byte *) (regp + regmap[i]));
249 if ((regno == -1 || regno == I386_LINUX_ORIG_EAX_REGNUM)
250 && I386_LINUX_ORIG_EAX_REGNUM < NUM_REGS)
251 regcache_raw_collect (current_regcache, I386_LINUX_ORIG_EAX_REGNUM,
252 (gdb_byte *) (regp + ORIG_EAX));
255 #ifdef HAVE_PTRACE_GETREGS
257 /* Fetch all general-purpose registers from process/thread TID and
258 store their values in GDB's register array. */
265 if (ptrace (PTRACE_GETREGS, tid, 0, (int) ®s) < 0)
269 /* The kernel we're running on doesn't support the GETREGS
270 request. Reset `have_ptrace_getregs'. */
271 have_ptrace_getregs = 0;
275 perror_with_name (_("Couldn't get registers"));
278 supply_gregset (®s);
281 /* Store all valid general-purpose registers in GDB's register array
282 into the process/thread specified by TID. */
285 store_regs (int tid, int regno)
289 if (ptrace (PTRACE_GETREGS, tid, 0, (int) ®s) < 0)
290 perror_with_name (_("Couldn't get registers"));
292 fill_gregset (®s, regno);
294 if (ptrace (PTRACE_SETREGS, tid, 0, (int) ®s) < 0)
295 perror_with_name (_("Couldn't write registers"));
300 static void fetch_regs (int tid) {}
301 static void store_regs (int tid, int regno) {}
306 /* Transfering floating-point registers between GDB, inferiors and cores. */
308 /* Fill GDB's register array with the floating-point register values in
312 supply_fpregset (elf_fpregset_t *fpregsetp)
314 i387_supply_fsave (current_regcache, -1, fpregsetp);
317 /* Fill register REGNO (if it is a floating-point register) in
318 *FPREGSETP with the value in GDB's register array. If REGNO is -1,
319 do this for all registers. */
322 fill_fpregset (elf_fpregset_t *fpregsetp, int regno)
324 i387_fill_fsave ((char *) fpregsetp, regno);
327 #ifdef HAVE_PTRACE_GETREGS
329 /* Fetch all floating-point registers from process/thread TID and store
330 thier values in GDB's register array. */
333 fetch_fpregs (int tid)
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 supply_fpregset (&fpregs);
343 /* Store all valid floating-point registers in GDB's register array
344 into the process/thread specified by TID. */
347 store_fpregs (int tid, int regno)
349 elf_fpregset_t fpregs;
351 if (ptrace (PTRACE_GETFPREGS, tid, 0, (int) &fpregs) < 0)
352 perror_with_name (_("Couldn't get floating point status"));
354 fill_fpregset (&fpregs, regno);
356 if (ptrace (PTRACE_SETFPREGS, tid, 0, (int) &fpregs) < 0)
357 perror_with_name (_("Couldn't write floating point status"));
362 static void fetch_fpregs (int tid) {}
363 static void store_fpregs (int tid, int regno) {}
368 /* Transfering floating-point and SSE registers to and from GDB. */
370 #ifdef HAVE_PTRACE_GETFPXREGS
372 /* Fill GDB's register array with the floating-point and SSE register
373 values in *FPXREGSETP. */
376 supply_fpxregset (elf_fpxregset_t *fpxregsetp)
378 i387_supply_fxsave (current_regcache, -1, fpxregsetp);
381 /* Fill register REGNO (if it is a floating-point or SSE register) in
382 *FPXREGSETP with the value in GDB's register array. If REGNO is
383 -1, do this for all registers. */
386 fill_fpxregset (elf_fpxregset_t *fpxregsetp, int regno)
388 i387_fill_fxsave ((char *) fpxregsetp, regno);
391 /* Fetch all registers covered by the PTRACE_GETFPXREGS request from
392 process/thread TID and store their values in GDB's register array.
393 Return non-zero if successful, zero otherwise. */
396 fetch_fpxregs (int tid)
398 elf_fpxregset_t fpxregs;
400 if (! have_ptrace_getfpxregs)
403 if (ptrace (PTRACE_GETFPXREGS, tid, 0, (int) &fpxregs) < 0)
407 have_ptrace_getfpxregs = 0;
411 perror_with_name (_("Couldn't read floating-point and SSE registers"));
414 supply_fpxregset (&fpxregs);
418 /* Store all valid registers in GDB's register array covered by the
419 PTRACE_SETFPXREGS request into the process/thread specified by TID.
420 Return non-zero if successful, zero otherwise. */
423 store_fpxregs (int tid, int regno)
425 elf_fpxregset_t fpxregs;
427 if (! have_ptrace_getfpxregs)
430 if (ptrace (PTRACE_GETFPXREGS, tid, 0, &fpxregs) == -1)
434 have_ptrace_getfpxregs = 0;
438 perror_with_name (_("Couldn't read floating-point and SSE registers"));
441 fill_fpxregset (&fpxregs, regno);
443 if (ptrace (PTRACE_SETFPXREGS, tid, 0, &fpxregs) == -1)
444 perror_with_name (_("Couldn't write floating-point and SSE registers"));
451 static int fetch_fpxregs (int tid) { return 0; }
452 static int store_fpxregs (int tid, int regno) { return 0; }
454 #endif /* HAVE_PTRACE_GETFPXREGS */
457 /* Transferring arbitrary registers between GDB and inferior. */
459 /* Check if register REGNO in the child process is accessible.
460 If we are accessing registers directly via the U area, only the
461 general-purpose registers are available.
462 All registers should be accessible if we have GETREGS support. */
465 cannot_fetch_register (int regno)
467 gdb_assert (regno >= 0 && regno < NUM_REGS);
468 return (!have_ptrace_getregs && regmap[regno] == -1);
472 cannot_store_register (int regno)
474 gdb_assert (regno >= 0 && regno < NUM_REGS);
475 return (!have_ptrace_getregs && regmap[regno] == -1);
478 /* Fetch register REGNO from the child process. If REGNO is -1, do
479 this for all registers (including the floating point and SSE
483 fetch_inferior_registers (int regno)
487 /* Use the old method of peeking around in `struct user' if the
488 GETREGS request isn't available. */
489 if (!have_ptrace_getregs)
493 for (i = 0; i < NUM_REGS; i++)
494 if (regno == -1 || regno == i)
500 /* GNU/Linux LWP ID's are process ID's. */
501 tid = TIDGET (inferior_ptid);
503 tid = PIDGET (inferior_ptid); /* Not a threaded program. */
505 /* Use the PTRACE_GETFPXREGS request whenever possible, since it
506 transfers more registers in one system call, and we'll cache the
507 results. But remember that fetch_fpxregs can fail, and return
513 /* The call above might reset `have_ptrace_getregs'. */
514 if (!have_ptrace_getregs)
516 fetch_inferior_registers (regno);
520 if (fetch_fpxregs (tid))
526 if (GETREGS_SUPPLIES (regno))
532 if (GETFPXREGS_SUPPLIES (regno))
534 if (fetch_fpxregs (tid))
537 /* Either our processor or our kernel doesn't support the SSE
538 registers, so read the FP registers in the traditional way,
539 and fill the SSE registers with dummy values. It would be
540 more graceful to handle differences in the register set using
541 gdbarch. Until then, this will at least make things work
547 internal_error (__FILE__, __LINE__,
548 _("Got request for bad register number %d."), regno);
551 /* Store register REGNO back into the child process. If REGNO is -1,
552 do this for all registers (including the floating point and SSE
555 store_inferior_registers (int regno)
559 /* Use the old method of poking around in `struct user' if the
560 SETREGS request isn't available. */
561 if (!have_ptrace_getregs)
565 for (i = 0; i < NUM_REGS; i++)
566 if (regno == -1 || regno == i)
572 /* GNU/Linux LWP ID's are process ID's. */
573 tid = TIDGET (inferior_ptid);
575 tid = PIDGET (inferior_ptid); /* Not a threaded program. */
577 /* Use the PTRACE_SETFPXREGS requests whenever possible, since it
578 transfers more registers in one system call. But remember that
579 store_fpxregs can fail, and return zero. */
582 store_regs (tid, regno);
583 if (store_fpxregs (tid, regno))
585 store_fpregs (tid, regno);
589 if (GETREGS_SUPPLIES (regno))
591 store_regs (tid, regno);
595 if (GETFPXREGS_SUPPLIES (regno))
597 if (store_fpxregs (tid, regno))
600 /* Either our processor or our kernel doesn't support the SSE
601 registers, so just write the FP registers in the traditional
603 store_fpregs (tid, regno);
607 internal_error (__FILE__, __LINE__,
608 _("Got request to store bad register number %d."), regno);
612 /* Support for debug registers. */
615 i386_linux_dr_get (int regnum)
620 /* FIXME: kettenis/2001-01-29: It's not clear what we should do with
621 multi-threaded processes here. For now, pretend there is just
623 tid = PIDGET (inferior_ptid);
625 /* FIXME: kettenis/2001-03-27: Calling perror_with_name if the
626 ptrace call fails breaks debugging remote targets. The correct
627 way to fix this is to add the hardware breakpoint and watchpoint
628 stuff to the target vector. For now, just return zero if the
629 ptrace call fails. */
631 value = ptrace (PTRACE_PEEKUSER, tid,
632 offsetof (struct user, u_debugreg[regnum]), 0);
635 perror_with_name (_("Couldn't read debug register"));
644 i386_linux_dr_set (int regnum, unsigned long value)
648 /* FIXME: kettenis/2001-01-29: It's not clear what we should do with
649 multi-threaded processes here. For now, pretend there is just
651 tid = PIDGET (inferior_ptid);
654 ptrace (PTRACE_POKEUSER, tid,
655 offsetof (struct user, u_debugreg[regnum]), value);
657 perror_with_name (_("Couldn't write debug register"));
661 i386_linux_dr_set_control (unsigned long control)
663 i386_linux_dr_set (DR_CONTROL, control);
667 i386_linux_dr_set_addr (int regnum, CORE_ADDR addr)
669 gdb_assert (regnum >= 0 && regnum <= DR_LASTADDR - DR_FIRSTADDR);
671 i386_linux_dr_set (DR_FIRSTADDR + regnum, addr);
675 i386_linux_dr_reset_addr (int regnum)
677 gdb_assert (regnum >= 0 && regnum <= DR_LASTADDR - DR_FIRSTADDR);
679 i386_linux_dr_set (DR_FIRSTADDR + regnum, 0L);
683 i386_linux_dr_get_status (void)
685 return i386_linux_dr_get (DR_STATUS);
689 /* Called by libthread_db. Returns a pointer to the thread local
690 storage (or its descriptor). */
693 ps_get_thread_area (const struct ps_prochandle *ph,
694 lwpid_t lwpid, int idx, void **base)
696 /* NOTE: cagney/2003-08-26: The definition of this buffer is found
697 in the kernel header <asm-i386/ldt.h>. It, after padding, is 4 x
698 4 byte integers in size: `entry_number', `base_addr', `limit',
699 and a bunch of status bits.
701 The values returned by this ptrace call should be part of the
702 regcache buffer, and ps_get_thread_area should channel its
703 request through the regcache. That way remote targets could
704 provide the value using the remote protocol and not this direct
707 Is this function needed? I'm guessing that the `base' is the
708 address of a a descriptor that libthread_db uses to find the
709 thread local address base that GDB needs. Perhaps that
710 descriptor is defined by the ABI. Anyway, given that
711 libthread_db calls this function without prompting (gdb
712 requesting tls base) I guess it needs info in there anyway. */
713 unsigned int desc[4];
714 gdb_assert (sizeof (int) == 4);
716 #ifndef PTRACE_GET_THREAD_AREA
717 #define PTRACE_GET_THREAD_AREA 25
720 if (ptrace (PTRACE_GET_THREAD_AREA, lwpid,
721 (void *) idx, (unsigned long) &desc) < 0)
724 *(int *)base = desc[1];
729 /* The instruction for a GNU/Linux system call is:
733 static const unsigned char linux_syscall[] = { 0xcd, 0x80 };
735 #define LINUX_SYSCALL_LEN (sizeof linux_syscall)
737 /* The system call number is stored in the %eax register. */
738 #define LINUX_SYSCALL_REGNUM I386_EAX_REGNUM
740 /* We are specifically interested in the sigreturn and rt_sigreturn
743 #ifndef SYS_sigreturn
744 #define SYS_sigreturn 0x77
746 #ifndef SYS_rt_sigreturn
747 #define SYS_rt_sigreturn 0xad
750 /* Offset to saved processor flags, from <asm/sigcontext.h>. */
751 #define LINUX_SIGCONTEXT_EFLAGS_OFFSET (64)
753 /* Resume execution of the inferior process.
754 If STEP is nonzero, single-step it.
755 If SIGNAL is nonzero, give it that signal. */
758 child_resume (ptid_t ptid, int step, enum target_signal signal)
760 int pid = PIDGET (ptid);
762 int request = PTRACE_CONT;
765 /* Resume all threads. */
766 /* I think this only gets used in the non-threaded case, where "resume
767 all threads" and "resume inferior_ptid" are the same. */
768 pid = PIDGET (inferior_ptid);
772 CORE_ADDR pc = read_pc_pid (pid_to_ptid (pid));
773 unsigned char buf[LINUX_SYSCALL_LEN];
775 request = PTRACE_SINGLESTEP;
777 /* Returning from a signal trampoline is done by calling a
778 special system call (sigreturn or rt_sigreturn, see
779 i386-linux-tdep.c for more information). This system call
780 restores the registers that were saved when the signal was
781 raised, including %eflags. That means that single-stepping
782 won't work. Instead, we'll have to modify the signal context
783 that's about to be restored, and set the trace flag there. */
785 /* First check if PC is at a system call. */
786 if (deprecated_read_memory_nobpt (pc, (char *) buf, LINUX_SYSCALL_LEN) == 0
787 && memcmp (buf, linux_syscall, LINUX_SYSCALL_LEN) == 0)
789 int syscall = read_register_pid (LINUX_SYSCALL_REGNUM,
792 /* Then check the system call number. */
793 if (syscall == SYS_sigreturn || syscall == SYS_rt_sigreturn)
795 CORE_ADDR sp = read_register (I386_ESP_REGNUM);
797 unsigned long int eflags;
799 if (syscall == SYS_rt_sigreturn)
800 addr = read_memory_integer (sp + 8, 4) + 20;
802 /* Set the trace flag in the context that's about to be
804 addr += LINUX_SIGCONTEXT_EFLAGS_OFFSET;
805 read_memory (addr, (char *) &eflags, 4);
807 write_memory (addr, (char *) &eflags, 4);
812 if (ptrace (request, pid, 0, target_signal_to_host (signal)) == -1)
813 perror_with_name (("ptrace"));
817 child_post_startup_inferior (ptid_t ptid)
819 i386_cleanup_dregs ();
820 linux_child_post_startup_inferior (ptid);