1 /* Native-dependent code for GNU/Linux i386.
3 Copyright 1999, 2000, 2001, 2002, 2003, 2004, 2005
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., 59 Temple Place - Suite 330,
21 Boston, MA 02111-1307, USA. */
27 #include "linux-nat.h"
29 #include "gdb_assert.h"
30 #include "gdb_string.h"
31 #include <sys/ptrace.h>
33 #include <sys/procfs.h>
43 #ifdef HAVE_SYS_DEBUGREG_H
44 #include <sys/debugreg.h>
48 #define DR_FIRSTADDR 0
63 /* Prototypes for supply_gregset etc. */
66 #include "i387-tdep.h"
67 #include "i386-tdep.h"
68 #include "i386-linux-tdep.h"
70 /* Defines ps_err_e, struct ps_prochandle. */
71 #include "gdb_proc_service.h"
74 /* The register sets used in GNU/Linux ELF core-dumps are identical to
75 the register sets in `struct user' that is used for a.out
76 core-dumps, and is also used by `ptrace'. The corresponding types
77 are `elf_gregset_t' for the general-purpose registers (with
78 `elf_greg_t' the type of a single GP register) and `elf_fpregset_t'
79 for the floating-point registers.
81 Those types used to be available under the names `gregset_t' and
82 `fpregset_t' too, and this file used those names in the past. But
83 those names are now used for the register sets used in the
84 `mcontext_t' type, and have a different size and layout. */
86 /* Mapping between the general-purpose registers in `struct user'
87 format and GDB's register array layout. */
94 -1, -1, -1, -1, /* st0, st1, st2, st3 */
95 -1, -1, -1, -1, /* st4, st5, st6, st7 */
96 -1, -1, -1, -1, /* fctrl, fstat, ftag, fiseg */
97 -1, -1, -1, -1, /* fioff, foseg, fooff, fop */
98 -1, -1, -1, -1, /* xmm0, xmm1, xmm2, xmm3 */
99 -1, -1, -1, -1, /* xmm4, xmm5, xmm6, xmm6 */
104 /* Which ptrace request retrieves which registers?
105 These apply to the corresponding SET requests as well. */
107 #define GETREGS_SUPPLIES(regno) \
108 ((0 <= (regno) && (regno) <= 15) || (regno) == I386_LINUX_ORIG_EAX_REGNUM)
110 #define GETFPXREGS_SUPPLIES(regno) \
111 (I386_ST0_REGNUM <= (regno) && (regno) < I386_SSE_NUM_REGS)
113 /* Does the current host support the GETREGS request? */
114 int have_ptrace_getregs
=
115 #ifdef HAVE_PTRACE_GETREGS
122 /* Does the current host support the GETFPXREGS request? The header
123 file may or may not define it, and even if it is defined, the
124 kernel will return EIO if it's running on a pre-SSE processor.
126 My instinct is to attach this to some architecture- or
127 target-specific data structure, but really, a particular GDB
128 process can only run on top of one kernel at a time. So it's okay
129 for this to be a simple variable. */
130 int have_ptrace_getfpxregs
=
131 #ifdef HAVE_PTRACE_GETFPXREGS
139 /* Support for the user struct. */
141 /* Return the address of register REGNUM. BLOCKEND is the value of
142 u.u_ar0, which should point to the registers. */
145 register_u_addr (CORE_ADDR blockend
, int regnum
)
147 return (blockend
+ 4 * regmap
[regnum
]);
150 /* Return the size of the user struct. */
155 return (sizeof (struct user
));
159 /* Accessing registers through the U area, one at a time. */
161 /* Fetch one register. */
164 fetch_register (int regno
)
169 gdb_assert (!have_ptrace_getregs
);
170 if (cannot_fetch_register (regno
))
172 regcache_raw_supply (current_regcache
, regno
, NULL
);
176 /* GNU/Linux LWP ID's are process ID's. */
177 tid
= TIDGET (inferior_ptid
);
179 tid
= PIDGET (inferior_ptid
); /* Not a threaded program. */
182 val
= ptrace (PTRACE_PEEKUSER
, tid
, register_addr (regno
, 0), 0);
184 error (_("Couldn't read register %s (#%d): %s."), REGISTER_NAME (regno
),
185 regno
, safe_strerror (errno
));
187 regcache_raw_supply (current_regcache
, regno
, &val
);
190 /* Store one register. */
193 store_register (int regno
)
198 gdb_assert (!have_ptrace_getregs
);
199 if (cannot_store_register (regno
))
202 /* GNU/Linux LWP ID's are process ID's. */
203 tid
= TIDGET (inferior_ptid
);
205 tid
= PIDGET (inferior_ptid
); /* Not a threaded program. */
208 regcache_raw_collect (current_regcache
, regno
, &val
);
209 ptrace (PTRACE_POKEUSER
, tid
, register_addr (regno
, 0), val
);
211 error (_("Couldn't write register %s (#%d): %s."), REGISTER_NAME (regno
),
212 regno
, safe_strerror (errno
));
216 /* Transfering the general-purpose registers between GDB, inferiors
219 /* Fill GDB's register array with the general-purpose register values
223 supply_gregset (elf_gregset_t
*gregsetp
)
225 elf_greg_t
*regp
= (elf_greg_t
*) gregsetp
;
228 for (i
= 0; i
< I386_NUM_GREGS
; i
++)
229 regcache_raw_supply (current_regcache
, i
, regp
+ regmap
[i
]);
231 if (I386_LINUX_ORIG_EAX_REGNUM
< NUM_REGS
)
232 regcache_raw_supply (current_regcache
, I386_LINUX_ORIG_EAX_REGNUM
,
236 /* Fill register REGNO (if it is a general-purpose register) in
237 *GREGSETPS with the value in GDB's register array. If REGNO is -1,
238 do this for all registers. */
241 fill_gregset (elf_gregset_t
*gregsetp
, int regno
)
243 elf_greg_t
*regp
= (elf_greg_t
*) gregsetp
;
246 for (i
= 0; i
< I386_NUM_GREGS
; i
++)
247 if (regno
== -1 || regno
== i
)
248 regcache_raw_collect (current_regcache
, i
, regp
+ regmap
[i
]);
250 if ((regno
== -1 || regno
== I386_LINUX_ORIG_EAX_REGNUM
)
251 && I386_LINUX_ORIG_EAX_REGNUM
< NUM_REGS
)
252 regcache_raw_collect (current_regcache
, I386_LINUX_ORIG_EAX_REGNUM
,
256 #ifdef HAVE_PTRACE_GETREGS
258 /* Fetch all general-purpose registers from process/thread TID and
259 store their values in GDB's register array. */
266 if (ptrace (PTRACE_GETREGS
, tid
, 0, (int) ®s
) < 0)
270 /* The kernel we're running on doesn't support the GETREGS
271 request. Reset `have_ptrace_getregs'. */
272 have_ptrace_getregs
= 0;
276 perror_with_name (_("Couldn't get registers"));
279 supply_gregset (®s
);
282 /* Store all valid general-purpose registers in GDB's register array
283 into the process/thread specified by TID. */
286 store_regs (int tid
, int regno
)
290 if (ptrace (PTRACE_GETREGS
, tid
, 0, (int) ®s
) < 0)
291 perror_with_name (_("Couldn't get registers"));
293 fill_gregset (®s
, regno
);
295 if (ptrace (PTRACE_SETREGS
, tid
, 0, (int) ®s
) < 0)
296 perror_with_name (_("Couldn't write registers"));
301 static void fetch_regs (int tid
) {}
302 static void store_regs (int tid
, int regno
) {}
307 /* Transfering floating-point registers between GDB, inferiors and cores. */
309 /* Fill GDB's register array with the floating-point register values in
313 supply_fpregset (elf_fpregset_t
*fpregsetp
)
315 i387_supply_fsave (current_regcache
, -1, fpregsetp
);
318 /* Fill register REGNO (if it is a floating-point register) in
319 *FPREGSETP with the value in GDB's register array. If REGNO is -1,
320 do this for all registers. */
323 fill_fpregset (elf_fpregset_t
*fpregsetp
, int regno
)
325 i387_fill_fsave ((char *) fpregsetp
, regno
);
328 #ifdef HAVE_PTRACE_GETREGS
330 /* Fetch all floating-point registers from process/thread TID and store
331 thier values in GDB's register array. */
334 fetch_fpregs (int tid
)
336 elf_fpregset_t fpregs
;
338 if (ptrace (PTRACE_GETFPREGS
, tid
, 0, (int) &fpregs
) < 0)
339 perror_with_name (_("Couldn't get floating point status"));
341 supply_fpregset (&fpregs
);
344 /* Store all valid floating-point registers in GDB's register array
345 into the process/thread specified by TID. */
348 store_fpregs (int tid
, int regno
)
350 elf_fpregset_t fpregs
;
352 if (ptrace (PTRACE_GETFPREGS
, tid
, 0, (int) &fpregs
) < 0)
353 perror_with_name (_("Couldn't get floating point status"));
355 fill_fpregset (&fpregs
, regno
);
357 if (ptrace (PTRACE_SETFPREGS
, tid
, 0, (int) &fpregs
) < 0)
358 perror_with_name (_("Couldn't write floating point status"));
363 static void fetch_fpregs (int tid
) {}
364 static void store_fpregs (int tid
, int regno
) {}
369 /* Transfering floating-point and SSE registers to and from GDB. */
371 #ifdef HAVE_PTRACE_GETFPXREGS
373 /* Fill GDB's register array with the floating-point and SSE register
374 values in *FPXREGSETP. */
377 supply_fpxregset (elf_fpxregset_t
*fpxregsetp
)
379 i387_supply_fxsave (current_regcache
, -1, fpxregsetp
);
382 /* Fill register REGNO (if it is a floating-point or SSE register) in
383 *FPXREGSETP with the value in GDB's register array. If REGNO is
384 -1, do this for all registers. */
387 fill_fpxregset (elf_fpxregset_t
*fpxregsetp
, int regno
)
389 i387_fill_fxsave ((char *) fpxregsetp
, regno
);
392 /* Fetch all registers covered by the PTRACE_GETFPXREGS request from
393 process/thread TID and store their values in GDB's register array.
394 Return non-zero if successful, zero otherwise. */
397 fetch_fpxregs (int tid
)
399 elf_fpxregset_t fpxregs
;
401 if (! have_ptrace_getfpxregs
)
404 if (ptrace (PTRACE_GETFPXREGS
, tid
, 0, (int) &fpxregs
) < 0)
408 have_ptrace_getfpxregs
= 0;
412 perror_with_name (_("Couldn't read floating-point and SSE registers"));
415 supply_fpxregset (&fpxregs
);
419 /* Store all valid registers in GDB's register array covered by the
420 PTRACE_SETFPXREGS request into the process/thread specified by TID.
421 Return non-zero if successful, zero otherwise. */
424 store_fpxregs (int tid
, int regno
)
426 elf_fpxregset_t fpxregs
;
428 if (! have_ptrace_getfpxregs
)
431 if (ptrace (PTRACE_GETFPXREGS
, tid
, 0, &fpxregs
) == -1)
435 have_ptrace_getfpxregs
= 0;
439 perror_with_name (_("Couldn't read floating-point and SSE registers"));
442 fill_fpxregset (&fpxregs
, regno
);
444 if (ptrace (PTRACE_SETFPXREGS
, tid
, 0, &fpxregs
) == -1)
445 perror_with_name (_("Couldn't write floating-point and SSE registers"));
452 static int fetch_fpxregs (int tid
) { return 0; }
453 static int store_fpxregs (int tid
, int regno
) { return 0; }
455 #endif /* HAVE_PTRACE_GETFPXREGS */
458 /* Transferring arbitrary registers between GDB and inferior. */
460 /* Check if register REGNO in the child process is accessible.
461 If we are accessing registers directly via the U area, only the
462 general-purpose registers are available.
463 All registers should be accessible if we have GETREGS support. */
466 cannot_fetch_register (int regno
)
468 gdb_assert (regno
>= 0 && regno
< NUM_REGS
);
469 return (!have_ptrace_getregs
&& regmap
[regno
] == -1);
473 cannot_store_register (int regno
)
475 gdb_assert (regno
>= 0 && regno
< NUM_REGS
);
476 return (!have_ptrace_getregs
&& regmap
[regno
] == -1);
479 /* Fetch register REGNO from the child process. If REGNO is -1, do
480 this for all registers (including the floating point and SSE
484 fetch_inferior_registers (int regno
)
488 /* Use the old method of peeking around in `struct user' if the
489 GETREGS request isn't available. */
490 if (!have_ptrace_getregs
)
494 for (i
= 0; i
< NUM_REGS
; i
++)
495 if (regno
== -1 || regno
== i
)
501 /* GNU/Linux LWP ID's are process ID's. */
502 tid
= TIDGET (inferior_ptid
);
504 tid
= PIDGET (inferior_ptid
); /* Not a threaded program. */
506 /* Use the PTRACE_GETFPXREGS request whenever possible, since it
507 transfers more registers in one system call, and we'll cache the
508 results. But remember that fetch_fpxregs can fail, and return
514 /* The call above might reset `have_ptrace_getregs'. */
515 if (!have_ptrace_getregs
)
517 fetch_inferior_registers (regno
);
521 if (fetch_fpxregs (tid
))
527 if (GETREGS_SUPPLIES (regno
))
533 if (GETFPXREGS_SUPPLIES (regno
))
535 if (fetch_fpxregs (tid
))
538 /* Either our processor or our kernel doesn't support the SSE
539 registers, so read the FP registers in the traditional way,
540 and fill the SSE registers with dummy values. It would be
541 more graceful to handle differences in the register set using
542 gdbarch. Until then, this will at least make things work
548 internal_error (__FILE__
, __LINE__
,
549 _("Got request for bad register number %d."), regno
);
552 /* Store register REGNO back into the child process. If REGNO is -1,
553 do this for all registers (including the floating point and SSE
556 store_inferior_registers (int regno
)
560 /* Use the old method of poking around in `struct user' if the
561 SETREGS request isn't available. */
562 if (!have_ptrace_getregs
)
566 for (i
= 0; i
< NUM_REGS
; i
++)
567 if (regno
== -1 || regno
== i
)
573 /* GNU/Linux LWP ID's are process ID's. */
574 tid
= TIDGET (inferior_ptid
);
576 tid
= PIDGET (inferior_ptid
); /* Not a threaded program. */
578 /* Use the PTRACE_SETFPXREGS requests whenever possible, since it
579 transfers more registers in one system call. But remember that
580 store_fpxregs can fail, and return zero. */
583 store_regs (tid
, regno
);
584 if (store_fpxregs (tid
, regno
))
586 store_fpregs (tid
, regno
);
590 if (GETREGS_SUPPLIES (regno
))
592 store_regs (tid
, regno
);
596 if (GETFPXREGS_SUPPLIES (regno
))
598 if (store_fpxregs (tid
, regno
))
601 /* Either our processor or our kernel doesn't support the SSE
602 registers, so just write the FP registers in the traditional
604 store_fpregs (tid
, regno
);
608 internal_error (__FILE__
, __LINE__
,
609 _("Got request to store bad register number %d."), regno
);
613 /* Support for debug registers. */
616 i386_linux_dr_get (int regnum
)
621 /* FIXME: kettenis/2001-01-29: It's not clear what we should do with
622 multi-threaded processes here. For now, pretend there is just
624 tid
= PIDGET (inferior_ptid
);
626 /* FIXME: kettenis/2001-03-27: Calling perror_with_name if the
627 ptrace call fails breaks debugging remote targets. The correct
628 way to fix this is to add the hardware breakpoint and watchpoint
629 stuff to the target vector. For now, just return zero if the
630 ptrace call fails. */
632 value
= ptrace (PTRACE_PEEKUSER
, tid
,
633 offsetof (struct user
, u_debugreg
[regnum
]), 0);
636 perror_with_name (_("Couldn't read debug register"));
645 i386_linux_dr_set (int regnum
, unsigned long value
)
649 /* FIXME: kettenis/2001-01-29: It's not clear what we should do with
650 multi-threaded processes here. For now, pretend there is just
652 tid
= PIDGET (inferior_ptid
);
655 ptrace (PTRACE_POKEUSER
, tid
,
656 offsetof (struct user
, u_debugreg
[regnum
]), value
);
658 perror_with_name (_("Couldn't write debug register"));
662 i386_linux_dr_set_control (unsigned long control
)
664 i386_linux_dr_set (DR_CONTROL
, control
);
668 i386_linux_dr_set_addr (int regnum
, CORE_ADDR addr
)
670 gdb_assert (regnum
>= 0 && regnum
<= DR_LASTADDR
- DR_FIRSTADDR
);
672 i386_linux_dr_set (DR_FIRSTADDR
+ regnum
, addr
);
676 i386_linux_dr_reset_addr (int regnum
)
678 gdb_assert (regnum
>= 0 && regnum
<= DR_LASTADDR
- DR_FIRSTADDR
);
680 i386_linux_dr_set (DR_FIRSTADDR
+ regnum
, 0L);
684 i386_linux_dr_get_status (void)
686 return i386_linux_dr_get (DR_STATUS
);
690 /* Called by libthread_db. Returns a pointer to the thread local
691 storage (or its descriptor). */
694 ps_get_thread_area (const struct ps_prochandle
*ph
,
695 lwpid_t lwpid
, int idx
, void **base
)
697 /* NOTE: cagney/2003-08-26: The definition of this buffer is found
698 in the kernel header <asm-i386/ldt.h>. It, after padding, is 4 x
699 4 byte integers in size: `entry_number', `base_addr', `limit',
700 and a bunch of status bits.
702 The values returned by this ptrace call should be part of the
703 regcache buffer, and ps_get_thread_area should channel its
704 request through the regcache. That way remote targets could
705 provide the value using the remote protocol and not this direct
708 Is this function needed? I'm guessing that the `base' is the
709 address of a a descriptor that libthread_db uses to find the
710 thread local address base that GDB needs. Perhaps that
711 descriptor is defined by the ABI. Anyway, given that
712 libthread_db calls this function without prompting (gdb
713 requesting tls base) I guess it needs info in there anyway. */
714 unsigned int desc
[4];
715 gdb_assert (sizeof (int) == 4);
717 #ifndef PTRACE_GET_THREAD_AREA
718 #define PTRACE_GET_THREAD_AREA 25
721 if (ptrace (PTRACE_GET_THREAD_AREA
, lwpid
,
722 (void *) idx
, (unsigned long) &desc
) < 0)
725 *(int *)base
= desc
[1];
730 /* The instruction for a GNU/Linux system call is:
734 static const unsigned char linux_syscall
[] = { 0xcd, 0x80 };
736 #define LINUX_SYSCALL_LEN (sizeof linux_syscall)
738 /* The system call number is stored in the %eax register. */
739 #define LINUX_SYSCALL_REGNUM I386_EAX_REGNUM
741 /* We are specifically interested in the sigreturn and rt_sigreturn
744 #ifndef SYS_sigreturn
745 #define SYS_sigreturn 0x77
747 #ifndef SYS_rt_sigreturn
748 #define SYS_rt_sigreturn 0xad
751 /* Offset to saved processor flags, from <asm/sigcontext.h>. */
752 #define LINUX_SIGCONTEXT_EFLAGS_OFFSET (64)
754 /* Resume execution of the inferior process.
755 If STEP is nonzero, single-step it.
756 If SIGNAL is nonzero, give it that signal. */
759 child_resume (ptid_t ptid
, int step
, enum target_signal signal
)
761 int pid
= PIDGET (ptid
);
763 int request
= PTRACE_CONT
;
766 /* Resume all threads. */
767 /* I think this only gets used in the non-threaded case, where "resume
768 all threads" and "resume inferior_ptid" are the same. */
769 pid
= PIDGET (inferior_ptid
);
773 CORE_ADDR pc
= read_pc_pid (pid_to_ptid (pid
));
774 gdb_byte buf
[LINUX_SYSCALL_LEN
];
776 request
= PTRACE_SINGLESTEP
;
778 /* Returning from a signal trampoline is done by calling a
779 special system call (sigreturn or rt_sigreturn, see
780 i386-linux-tdep.c for more information). This system call
781 restores the registers that were saved when the signal was
782 raised, including %eflags. That means that single-stepping
783 won't work. Instead, we'll have to modify the signal context
784 that's about to be restored, and set the trace flag there. */
786 /* First check if PC is at a system call. */
787 if (deprecated_read_memory_nobpt (pc
, buf
, LINUX_SYSCALL_LEN
) == 0
788 && memcmp (buf
, linux_syscall
, LINUX_SYSCALL_LEN
) == 0)
790 int syscall
= read_register_pid (LINUX_SYSCALL_REGNUM
,
793 /* Then check the system call number. */
794 if (syscall
== SYS_sigreturn
|| syscall
== SYS_rt_sigreturn
)
796 CORE_ADDR sp
= read_register (I386_ESP_REGNUM
);
798 unsigned long int eflags
;
800 if (syscall
== SYS_rt_sigreturn
)
801 addr
= read_memory_integer (sp
+ 8, 4) + 20;
803 /* Set the trace flag in the context that's about to be
805 addr
+= LINUX_SIGCONTEXT_EFLAGS_OFFSET
;
806 read_memory (addr
, (gdb_byte
*) &eflags
, 4);
808 write_memory (addr
, (gdb_byte
*) &eflags
, 4);
813 if (ptrace (request
, pid
, 0, target_signal_to_host (signal
)) == -1)
814 perror_with_name (("ptrace"));
818 child_post_startup_inferior (ptid_t ptid
)
820 i386_cleanup_dregs ();
821 linux_child_post_startup_inferior (ptid
);