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 /* Prototypes for i387_supply_fsave etc. */
66 #include "i387-tdep.h"
68 /* Defines for XMM0_REGNUM etc. */
69 #include "i386-tdep.h"
71 /* Defines I386_LINUX_ORIG_EAX_REGNUM. */
72 #include "i386-linux-tdep.h"
74 /* Defines ps_err_e, struct ps_prochandle. */
75 #include "gdb_proc_service.h"
78 /* The register sets used in GNU/Linux ELF core-dumps are identical to
79 the register sets in `struct user' that is used for a.out
80 core-dumps, and is also used by `ptrace'. The corresponding types
81 are `elf_gregset_t' for the general-purpose registers (with
82 `elf_greg_t' the type of a single GP register) and `elf_fpregset_t'
83 for the floating-point registers.
85 Those types used to be available under the names `gregset_t' and
86 `fpregset_t' too, and this file used those names in the past. But
87 those names are now used for the register sets used in the
88 `mcontext_t' type, and have a different size and layout. */
90 /* Mapping between the general-purpose registers in `struct user'
91 format and GDB's register array layout. */
98 -1, -1, -1, -1, /* st0, st1, st2, st3 */
99 -1, -1, -1, -1, /* st4, st5, st6, st7 */
100 -1, -1, -1, -1, /* fctrl, fstat, ftag, fiseg */
101 -1, -1, -1, -1, /* fioff, foseg, fooff, fop */
102 -1, -1, -1, -1, /* xmm0, xmm1, xmm2, xmm3 */
103 -1, -1, -1, -1, /* xmm4, xmm5, xmm6, xmm6 */
108 /* Which ptrace request retrieves which registers?
109 These apply to the corresponding SET requests as well. */
111 #define GETREGS_SUPPLIES(regno) \
112 ((0 <= (regno) && (regno) <= 15) || (regno) == I386_LINUX_ORIG_EAX_REGNUM)
114 #define GETFPREGS_SUPPLIES(regno) \
115 (FP0_REGNUM <= (regno) && (regno) <= LAST_FPU_CTRL_REGNUM)
117 #define GETFPXREGS_SUPPLIES(regno) \
118 (FP0_REGNUM <= (regno) && (regno) <= MXCSR_REGNUM)
120 /* Does the current host support the GETREGS request? */
121 int have_ptrace_getregs
=
122 #ifdef HAVE_PTRACE_GETREGS
129 /* Does the current host support the GETFPXREGS request? The header
130 file may or may not define it, and even if it is defined, the
131 kernel will return EIO if it's running on a pre-SSE processor.
133 My instinct is to attach this to some architecture- or
134 target-specific data structure, but really, a particular GDB
135 process can only run on top of one kernel at a time. So it's okay
136 for this to be a simple variable. */
137 int have_ptrace_getfpxregs
=
138 #ifdef HAVE_PTRACE_GETFPXREGS
146 /* Support for the user struct. */
148 /* Return the address of register REGNUM. BLOCKEND is the value of
149 u.u_ar0, which should point to the registers. */
152 register_u_addr (CORE_ADDR blockend
, int regnum
)
154 return (blockend
+ 4 * regmap
[regnum
]);
157 /* Return the size of the user struct. */
162 return (sizeof (struct user
));
166 /* Accessing registers through the U area, one at a time. */
168 /* Fetch one register. */
171 fetch_register (int regno
)
176 gdb_assert (!have_ptrace_getregs
);
177 if (cannot_fetch_register (regno
))
179 regcache_raw_supply (current_regcache
, regno
, NULL
);
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 val
= ptrace (PTRACE_PEEKUSER
, tid
, register_addr (regno
, 0), 0);
191 error ("Couldn't read register %s (#%d): %s.", REGISTER_NAME (regno
),
192 regno
, safe_strerror (errno
));
194 regcache_raw_supply (current_regcache
, regno
, &val
);
197 /* Store one register. */
200 store_register (int regno
)
205 gdb_assert (!have_ptrace_getregs
);
206 if (cannot_store_register (regno
))
209 /* GNU/Linux LWP ID's are process ID's. */
210 tid
= TIDGET (inferior_ptid
);
212 tid
= PIDGET (inferior_ptid
); /* Not a threaded program. */
215 regcache_raw_collect (current_regcache
, regno
, &val
);
216 ptrace (PTRACE_POKEUSER
, tid
, register_addr (regno
, 0), val
);
218 error ("Couldn't write register %s (#%d): %s.", REGISTER_NAME (regno
),
219 regno
, safe_strerror (errno
));
223 /* Transfering the general-purpose registers between GDB, inferiors
226 /* Fill GDB's register array with the general-purpose register values
230 supply_gregset (elf_gregset_t
*gregsetp
)
232 elf_greg_t
*regp
= (elf_greg_t
*) gregsetp
;
235 for (i
= 0; i
< I386_NUM_GREGS
; i
++)
236 regcache_raw_supply (current_regcache
, i
, regp
+ regmap
[i
]);
238 if (I386_LINUX_ORIG_EAX_REGNUM
< NUM_REGS
)
239 regcache_raw_supply (current_regcache
, I386_LINUX_ORIG_EAX_REGNUM
,
243 /* Fill register REGNO (if it is a general-purpose register) in
244 *GREGSETPS with the value in GDB's register array. If REGNO is -1,
245 do this for all registers. */
248 fill_gregset (elf_gregset_t
*gregsetp
, int regno
)
250 elf_greg_t
*regp
= (elf_greg_t
*) gregsetp
;
253 for (i
= 0; i
< I386_NUM_GREGS
; i
++)
254 if (regno
== -1 || regno
== i
)
255 regcache_raw_collect (current_regcache
, i
, regp
+ regmap
[i
]);
257 if ((regno
== -1 || regno
== I386_LINUX_ORIG_EAX_REGNUM
)
258 && I386_LINUX_ORIG_EAX_REGNUM
< NUM_REGS
)
259 regcache_raw_collect (current_regcache
, I386_LINUX_ORIG_EAX_REGNUM
,
263 #ifdef HAVE_PTRACE_GETREGS
265 /* Fetch all general-purpose registers from process/thread TID and
266 store their values in GDB's register array. */
273 if (ptrace (PTRACE_GETREGS
, tid
, 0, (int) ®s
) < 0)
277 /* The kernel we're running on doesn't support the GETREGS
278 request. Reset `have_ptrace_getregs'. */
279 have_ptrace_getregs
= 0;
283 perror_with_name ("Couldn't get registers");
286 supply_gregset (®s
);
289 /* Store all valid general-purpose registers in GDB's register array
290 into the process/thread specified by TID. */
293 store_regs (int tid
, int regno
)
297 if (ptrace (PTRACE_GETREGS
, tid
, 0, (int) ®s
) < 0)
298 perror_with_name ("Couldn't get registers");
300 fill_gregset (®s
, regno
);
302 if (ptrace (PTRACE_SETREGS
, tid
, 0, (int) ®s
) < 0)
303 perror_with_name ("Couldn't write registers");
308 static void fetch_regs (int tid
) {}
309 static void store_regs (int tid
, int regno
) {}
314 /* Transfering floating-point registers between GDB, inferiors and cores. */
316 /* Fill GDB's register array with the floating-point register values in
320 supply_fpregset (elf_fpregset_t
*fpregsetp
)
322 i387_supply_fsave (current_regcache
, -1, fpregsetp
);
325 /* Fill register REGNO (if it is a floating-point register) in
326 *FPREGSETP with the value in GDB's register array. If REGNO is -1,
327 do this for all registers. */
330 fill_fpregset (elf_fpregset_t
*fpregsetp
, int regno
)
332 i387_fill_fsave ((char *) fpregsetp
, regno
);
335 #ifdef HAVE_PTRACE_GETREGS
337 /* Fetch all floating-point registers from process/thread TID and store
338 thier values in GDB's register array. */
341 fetch_fpregs (int tid
)
343 elf_fpregset_t fpregs
;
345 if (ptrace (PTRACE_GETFPREGS
, tid
, 0, (int) &fpregs
) < 0)
346 perror_with_name ("Couldn't get floating point status");
348 supply_fpregset (&fpregs
);
351 /* Store all valid floating-point registers in GDB's register array
352 into the process/thread specified by TID. */
355 store_fpregs (int tid
, int regno
)
357 elf_fpregset_t fpregs
;
359 if (ptrace (PTRACE_GETFPREGS
, tid
, 0, (int) &fpregs
) < 0)
360 perror_with_name ("Couldn't get floating point status");
362 fill_fpregset (&fpregs
, regno
);
364 if (ptrace (PTRACE_SETFPREGS
, tid
, 0, (int) &fpregs
) < 0)
365 perror_with_name ("Couldn't write floating point status");
370 static void fetch_fpregs (int tid
) {}
371 static void store_fpregs (int tid
, int regno
) {}
376 /* Transfering floating-point and SSE registers to and from GDB. */
378 #ifdef HAVE_PTRACE_GETFPXREGS
380 /* Fill GDB's register array with the floating-point and SSE register
381 values in *FPXREGSETP. */
384 supply_fpxregset (elf_fpxregset_t
*fpxregsetp
)
386 i387_supply_fxsave (current_regcache
, -1, fpxregsetp
);
389 /* Fill register REGNO (if it is a floating-point or SSE register) in
390 *FPXREGSETP with the value in GDB's register array. If REGNO is
391 -1, do this for all registers. */
394 fill_fpxregset (elf_fpxregset_t
*fpxregsetp
, int regno
)
396 i387_fill_fxsave ((char *) fpxregsetp
, regno
);
399 /* Fetch all registers covered by the PTRACE_GETFPXREGS request from
400 process/thread TID and store their values in GDB's register array.
401 Return non-zero if successful, zero otherwise. */
404 fetch_fpxregs (int tid
)
406 elf_fpxregset_t fpxregs
;
408 if (! have_ptrace_getfpxregs
)
411 if (ptrace (PTRACE_GETFPXREGS
, tid
, 0, (int) &fpxregs
) < 0)
415 have_ptrace_getfpxregs
= 0;
419 perror_with_name ("Couldn't read floating-point and SSE registers");
422 supply_fpxregset (&fpxregs
);
426 /* Store all valid registers in GDB's register array covered by the
427 PTRACE_SETFPXREGS request into the process/thread specified by TID.
428 Return non-zero if successful, zero otherwise. */
431 store_fpxregs (int tid
, int regno
)
433 elf_fpxregset_t fpxregs
;
435 if (! have_ptrace_getfpxregs
)
438 if (ptrace (PTRACE_GETFPXREGS
, tid
, 0, &fpxregs
) == -1)
442 have_ptrace_getfpxregs
= 0;
446 perror_with_name ("Couldn't read floating-point and SSE registers");
449 fill_fpxregset (&fpxregs
, regno
);
451 if (ptrace (PTRACE_SETFPXREGS
, tid
, 0, &fpxregs
) == -1)
452 perror_with_name ("Couldn't write floating-point and SSE registers");
459 static int fetch_fpxregs (int tid
) { return 0; }
460 static int store_fpxregs (int tid
, int regno
) { return 0; }
462 #endif /* HAVE_PTRACE_GETFPXREGS */
465 /* Transferring arbitrary registers between GDB and inferior. */
467 /* Check if register REGNO in the child process is accessible.
468 If we are accessing registers directly via the U area, only the
469 general-purpose registers are available.
470 All registers should be accessible if we have GETREGS support. */
473 cannot_fetch_register (int regno
)
475 gdb_assert (regno
>= 0 && regno
< NUM_REGS
);
476 return (!have_ptrace_getregs
&& regmap
[regno
] == -1);
480 cannot_store_register (int regno
)
482 gdb_assert (regno
>= 0 && regno
< NUM_REGS
);
483 return (!have_ptrace_getregs
&& regmap
[regno
] == -1);
486 /* Fetch register REGNO from the child process. If REGNO is -1, do
487 this for all registers (including the floating point and SSE
491 fetch_inferior_registers (int regno
)
495 /* Use the old method of peeking around in `struct user' if the
496 GETREGS request isn't available. */
497 if (!have_ptrace_getregs
)
501 for (i
= 0; i
< NUM_REGS
; i
++)
502 if (regno
== -1 || regno
== i
)
508 /* GNU/Linux LWP ID's are process ID's. */
509 tid
= TIDGET (inferior_ptid
);
511 tid
= PIDGET (inferior_ptid
); /* Not a threaded program. */
513 /* Use the PTRACE_GETFPXREGS request whenever possible, since it
514 transfers more registers in one system call, and we'll cache the
515 results. But remember that fetch_fpxregs can fail, and return
521 /* The call above might reset `have_ptrace_getregs'. */
522 if (!have_ptrace_getregs
)
524 fetch_inferior_registers (regno
);
528 if (fetch_fpxregs (tid
))
534 if (GETREGS_SUPPLIES (regno
))
540 if (GETFPXREGS_SUPPLIES (regno
))
542 if (fetch_fpxregs (tid
))
545 /* Either our processor or our kernel doesn't support the SSE
546 registers, so read the FP registers in the traditional way,
547 and fill the SSE registers with dummy values. It would be
548 more graceful to handle differences in the register set using
549 gdbarch. Until then, this will at least make things work
555 internal_error (__FILE__
, __LINE__
,
556 "Got request for bad register number %d.", regno
);
559 /* Store register REGNO back into the child process. If REGNO is -1,
560 do this for all registers (including the floating point and SSE
563 store_inferior_registers (int regno
)
567 /* Use the old method of poking around in `struct user' if the
568 SETREGS request isn't available. */
569 if (!have_ptrace_getregs
)
573 for (i
= 0; i
< NUM_REGS
; i
++)
574 if (regno
== -1 || regno
== i
)
580 /* GNU/Linux LWP ID's are process ID's. */
581 tid
= TIDGET (inferior_ptid
);
583 tid
= PIDGET (inferior_ptid
); /* Not a threaded program. */
585 /* Use the PTRACE_SETFPXREGS requests whenever possible, since it
586 transfers more registers in one system call. But remember that
587 store_fpxregs can fail, and return zero. */
590 store_regs (tid
, regno
);
591 if (store_fpxregs (tid
, regno
))
593 store_fpregs (tid
, regno
);
597 if (GETREGS_SUPPLIES (regno
))
599 store_regs (tid
, regno
);
603 if (GETFPXREGS_SUPPLIES (regno
))
605 if (store_fpxregs (tid
, regno
))
608 /* Either our processor or our kernel doesn't support the SSE
609 registers, so just write the FP registers in the traditional
611 store_fpregs (tid
, regno
);
615 internal_error (__FILE__
, __LINE__
,
616 "Got request to store bad register number %d.", regno
);
620 /* Support for debug registers. */
623 i386_linux_dr_get (int regnum
)
628 /* FIXME: kettenis/2001-01-29: It's not clear what we should do with
629 multi-threaded processes here. For now, pretend there is just
631 tid
= PIDGET (inferior_ptid
);
633 /* FIXME: kettenis/2001-03-27: Calling perror_with_name if the
634 ptrace call fails breaks debugging remote targets. The correct
635 way to fix this is to add the hardware breakpoint and watchpoint
636 stuff to the target vector. For now, just return zero if the
637 ptrace call fails. */
639 value
= ptrace (PTRACE_PEEKUSER
, tid
,
640 offsetof (struct user
, u_debugreg
[regnum
]), 0);
643 perror_with_name ("Couldn't read debug register");
652 i386_linux_dr_set (int regnum
, unsigned long value
)
656 /* FIXME: kettenis/2001-01-29: It's not clear what we should do with
657 multi-threaded processes here. For now, pretend there is just
659 tid
= PIDGET (inferior_ptid
);
662 ptrace (PTRACE_POKEUSER
, tid
,
663 offsetof (struct user
, u_debugreg
[regnum
]), value
);
665 perror_with_name ("Couldn't write debug register");
669 i386_linux_dr_set_control (unsigned long control
)
671 i386_linux_dr_set (DR_CONTROL
, control
);
675 i386_linux_dr_set_addr (int regnum
, CORE_ADDR addr
)
677 gdb_assert (regnum
>= 0 && regnum
<= DR_LASTADDR
- DR_FIRSTADDR
);
679 i386_linux_dr_set (DR_FIRSTADDR
+ regnum
, addr
);
683 i386_linux_dr_reset_addr (int regnum
)
685 gdb_assert (regnum
>= 0 && regnum
<= DR_LASTADDR
- DR_FIRSTADDR
);
687 i386_linux_dr_set (DR_FIRSTADDR
+ regnum
, 0L);
691 i386_linux_dr_get_status (void)
693 return i386_linux_dr_get (DR_STATUS
);
697 /* Called by libthread_db. Returns a pointer to the thread local
698 storage (or its descriptor). */
701 ps_get_thread_area (const struct ps_prochandle
*ph
,
702 lwpid_t lwpid
, int idx
, void **base
)
704 /* NOTE: cagney/2003-08-26: The definition of this buffer is found
705 in the kernel header <asm-i386/ldt.h>. It, after padding, is 4 x
706 4 byte integers in size: `entry_number', `base_addr', `limit',
707 and a bunch of status bits.
709 The values returned by this ptrace call should be part of the
710 regcache buffer, and ps_get_thread_area should channel its
711 request through the regcache. That way remote targets could
712 provide the value using the remote protocol and not this direct
715 Is this function needed? I'm guessing that the `base' is the
716 address of a a descriptor that libthread_db uses to find the
717 thread local address base that GDB needs. Perhaps that
718 descriptor is defined by the ABI. Anyway, given that
719 libthread_db calls this function without prompting (gdb
720 requesting tls base) I guess it needs info in there anyway. */
721 unsigned int desc
[4];
722 gdb_assert (sizeof (int) == 4);
724 #ifndef PTRACE_GET_THREAD_AREA
725 #define PTRACE_GET_THREAD_AREA 25
728 if (ptrace (PTRACE_GET_THREAD_AREA
, lwpid
,
729 (void *) idx
, (unsigned long) &desc
) < 0)
732 *(int *)base
= desc
[1];
737 /* The instruction for a GNU/Linux system call is:
741 static const unsigned char linux_syscall
[] = { 0xcd, 0x80 };
743 #define LINUX_SYSCALL_LEN (sizeof linux_syscall)
745 /* The system call number is stored in the %eax register. */
746 #define LINUX_SYSCALL_REGNUM I386_EAX_REGNUM
748 /* We are specifically interested in the sigreturn and rt_sigreturn
751 #ifndef SYS_sigreturn
752 #define SYS_sigreturn 0x77
754 #ifndef SYS_rt_sigreturn
755 #define SYS_rt_sigreturn 0xad
758 /* Offset to saved processor flags, from <asm/sigcontext.h>. */
759 #define LINUX_SIGCONTEXT_EFLAGS_OFFSET (64)
761 /* Resume execution of the inferior process.
762 If STEP is nonzero, single-step it.
763 If SIGNAL is nonzero, give it that signal. */
766 child_resume (ptid_t ptid
, int step
, enum target_signal signal
)
768 int pid
= PIDGET (ptid
);
770 int request
= PTRACE_CONT
;
773 /* Resume all threads. */
774 /* I think this only gets used in the non-threaded case, where "resume
775 all threads" and "resume inferior_ptid" are the same. */
776 pid
= PIDGET (inferior_ptid
);
780 CORE_ADDR pc
= read_pc_pid (pid_to_ptid (pid
));
781 unsigned char buf
[LINUX_SYSCALL_LEN
];
783 request
= PTRACE_SINGLESTEP
;
785 /* Returning from a signal trampoline is done by calling a
786 special system call (sigreturn or rt_sigreturn, see
787 i386-linux-tdep.c for more information). This system call
788 restores the registers that were saved when the signal was
789 raised, including %eflags. That means that single-stepping
790 won't work. Instead, we'll have to modify the signal context
791 that's about to be restored, and set the trace flag there. */
793 /* First check if PC is at a system call. */
794 if (deprecated_read_memory_nobpt (pc
, (char *) buf
, LINUX_SYSCALL_LEN
) == 0
795 && memcmp (buf
, linux_syscall
, LINUX_SYSCALL_LEN
) == 0)
797 int syscall
= read_register_pid (LINUX_SYSCALL_REGNUM
,
800 /* Then check the system call number. */
801 if (syscall
== SYS_sigreturn
|| syscall
== SYS_rt_sigreturn
)
803 CORE_ADDR sp
= read_register (I386_ESP_REGNUM
);
805 unsigned long int eflags
;
807 if (syscall
== SYS_rt_sigreturn
)
808 addr
= read_memory_integer (sp
+ 8, 4) + 20;
810 /* Set the trace flag in the context that's about to be
812 addr
+= LINUX_SIGCONTEXT_EFLAGS_OFFSET
;
813 read_memory (addr
, (char *) &eflags
, 4);
815 write_memory (addr
, (char *) &eflags
, 4);
820 if (ptrace (request
, pid
, 0, target_signal_to_host (signal
)) == -1)
821 perror_with_name ("ptrace");
825 child_post_startup_inferior (ptid_t ptid
)
827 i386_cleanup_dregs ();
828 linux_child_post_startup_inferior (ptid
);