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"
77 /* Prototypes for local functions. */
78 static void dummy_sse_values (void);
81 /* The register sets used in GNU/Linux ELF core-dumps are identical to
82 the register sets in `struct user' that is used for a.out
83 core-dumps, and is also used by `ptrace'. The corresponding types
84 are `elf_gregset_t' for the general-purpose registers (with
85 `elf_greg_t' the type of a single GP register) and `elf_fpregset_t'
86 for the floating-point registers.
88 Those types used to be available under the names `gregset_t' and
89 `fpregset_t' too, and this file used those names in the past. But
90 those names are now used for the register sets used in the
91 `mcontext_t' type, and have a different size and layout. */
93 /* Mapping between the general-purpose registers in `struct user'
94 format and GDB's register array layout. */
101 -1, -1, -1, -1, /* st0, st1, st2, st3 */
102 -1, -1, -1, -1, /* st4, st5, st6, st7 */
103 -1, -1, -1, -1, /* fctrl, fstat, ftag, fiseg */
104 -1, -1, -1, -1, /* fioff, foseg, fooff, fop */
105 -1, -1, -1, -1, /* xmm0, xmm1, xmm2, xmm3 */
106 -1, -1, -1, -1, /* xmm4, xmm5, xmm6, xmm6 */
111 /* Which ptrace request retrieves which registers?
112 These apply to the corresponding SET requests as well. */
114 #define GETREGS_SUPPLIES(regno) \
115 ((0 <= (regno) && (regno) <= 15) || (regno) == I386_LINUX_ORIG_EAX_REGNUM)
117 #define GETFPREGS_SUPPLIES(regno) \
118 (FP0_REGNUM <= (regno) && (regno) <= LAST_FPU_CTRL_REGNUM)
120 #define GETFPXREGS_SUPPLIES(regno) \
121 (FP0_REGNUM <= (regno) && (regno) <= MXCSR_REGNUM)
123 /* Does the current host support the GETREGS request? */
124 int have_ptrace_getregs
=
125 #ifdef HAVE_PTRACE_GETREGS
132 /* Does the current host support the GETFPXREGS request? The header
133 file may or may not define it, and even if it is defined, the
134 kernel will return EIO if it's running on a pre-SSE processor.
136 My instinct is to attach this to some architecture- or
137 target-specific data structure, but really, a particular GDB
138 process can only run on top of one kernel at a time. So it's okay
139 for this to be a simple variable. */
140 int have_ptrace_getfpxregs
=
141 #ifdef HAVE_PTRACE_GETFPXREGS
149 /* Support for the user struct. */
151 /* Return the address of register REGNUM. BLOCKEND is the value of
152 u.u_ar0, which should point to the registers. */
155 register_u_addr (CORE_ADDR blockend
, int regnum
)
157 return (blockend
+ 4 * regmap
[regnum
]);
160 /* Return the size of the user struct. */
165 return (sizeof (struct user
));
169 /* Accessing registers through the U area, one at a time. */
171 /* Fetch one register. */
174 fetch_register (int regno
)
179 gdb_assert (!have_ptrace_getregs
);
180 if (cannot_fetch_register (regno
))
182 regcache_raw_supply (current_regcache
, regno
, NULL
);
186 /* GNU/Linux LWP ID's are process ID's. */
187 tid
= TIDGET (inferior_ptid
);
189 tid
= PIDGET (inferior_ptid
); /* Not a threaded program. */
192 val
= ptrace (PTRACE_PEEKUSER
, tid
, register_addr (regno
, 0), 0);
194 error ("Couldn't read register %s (#%d): %s.", REGISTER_NAME (regno
),
195 regno
, safe_strerror (errno
));
197 regcache_raw_supply (current_regcache
, regno
, &val
);
200 /* Store one register. */
203 store_register (int regno
)
208 gdb_assert (!have_ptrace_getregs
);
209 if (cannot_store_register (regno
))
212 /* GNU/Linux LWP ID's are process ID's. */
213 tid
= TIDGET (inferior_ptid
);
215 tid
= PIDGET (inferior_ptid
); /* Not a threaded program. */
218 regcache_collect (regno
, &val
);
219 ptrace (PTRACE_POKEUSER
, tid
, register_addr (regno
, 0), val
);
221 error ("Couldn't write register %s (#%d): %s.", REGISTER_NAME (regno
),
222 regno
, safe_strerror (errno
));
226 /* Transfering the general-purpose registers between GDB, inferiors
229 /* Fill GDB's register array with the general-purpose register values
233 supply_gregset (elf_gregset_t
*gregsetp
)
235 elf_greg_t
*regp
= (elf_greg_t
*) gregsetp
;
238 for (i
= 0; i
< I386_NUM_GREGS
; i
++)
239 regcache_raw_supply (current_regcache
, i
, regp
+ regmap
[i
]);
241 if (I386_LINUX_ORIG_EAX_REGNUM
< NUM_REGS
)
242 regcache_raw_supply (current_regcache
, I386_LINUX_ORIG_EAX_REGNUM
,
246 /* Fill register REGNO (if it is a general-purpose register) in
247 *GREGSETPS with the value in GDB's register array. If REGNO is -1,
248 do this for all registers. */
251 fill_gregset (elf_gregset_t
*gregsetp
, int regno
)
253 elf_greg_t
*regp
= (elf_greg_t
*) gregsetp
;
256 for (i
= 0; i
< I386_NUM_GREGS
; i
++)
257 if (regno
== -1 || regno
== i
)
258 regcache_collect (i
, regp
+ regmap
[i
]);
260 if ((regno
== -1 || regno
== I386_LINUX_ORIG_EAX_REGNUM
)
261 && I386_LINUX_ORIG_EAX_REGNUM
< NUM_REGS
)
262 regcache_collect (I386_LINUX_ORIG_EAX_REGNUM
, regp
+ ORIG_EAX
);
265 #ifdef HAVE_PTRACE_GETREGS
267 /* Fetch all general-purpose registers from process/thread TID and
268 store their values in GDB's register array. */
275 if (ptrace (PTRACE_GETREGS
, tid
, 0, (int) ®s
) < 0)
279 /* The kernel we're running on doesn't support the GETREGS
280 request. Reset `have_ptrace_getregs'. */
281 have_ptrace_getregs
= 0;
285 perror_with_name ("Couldn't get registers");
288 supply_gregset (®s
);
291 /* Store all valid general-purpose registers in GDB's register array
292 into the process/thread specified by TID. */
295 store_regs (int tid
, int regno
)
299 if (ptrace (PTRACE_GETREGS
, tid
, 0, (int) ®s
) < 0)
300 perror_with_name ("Couldn't get registers");
302 fill_gregset (®s
, regno
);
304 if (ptrace (PTRACE_SETREGS
, tid
, 0, (int) ®s
) < 0)
305 perror_with_name ("Couldn't write registers");
310 static void fetch_regs (int tid
) {}
311 static void store_regs (int tid
, int regno
) {}
316 /* Transfering floating-point registers between GDB, inferiors and cores. */
318 /* Fill GDB's register array with the floating-point register values in
322 supply_fpregset (elf_fpregset_t
*fpregsetp
)
324 i387_supply_fsave (current_regcache
, -1, fpregsetp
);
328 /* Fill register REGNO (if it is a floating-point register) in
329 *FPREGSETP with the value in GDB's register array. If REGNO is -1,
330 do this for all registers. */
333 fill_fpregset (elf_fpregset_t
*fpregsetp
, int regno
)
335 i387_fill_fsave ((char *) fpregsetp
, regno
);
338 #ifdef HAVE_PTRACE_GETREGS
340 /* Fetch all floating-point registers from process/thread TID and store
341 thier values in GDB's register array. */
344 fetch_fpregs (int tid
)
346 elf_fpregset_t fpregs
;
348 if (ptrace (PTRACE_GETFPREGS
, tid
, 0, (int) &fpregs
) < 0)
349 perror_with_name ("Couldn't get floating point status");
351 supply_fpregset (&fpregs
);
354 /* Store all valid floating-point registers in GDB's register array
355 into the process/thread specified by TID. */
358 store_fpregs (int tid
, int regno
)
360 elf_fpregset_t fpregs
;
362 if (ptrace (PTRACE_GETFPREGS
, tid
, 0, (int) &fpregs
) < 0)
363 perror_with_name ("Couldn't get floating point status");
365 fill_fpregset (&fpregs
, regno
);
367 if (ptrace (PTRACE_SETFPREGS
, tid
, 0, (int) &fpregs
) < 0)
368 perror_with_name ("Couldn't write floating point status");
373 static void fetch_fpregs (int tid
) {}
374 static void store_fpregs (int tid
, int regno
) {}
379 /* Transfering floating-point and SSE registers to and from GDB. */
381 #ifdef HAVE_PTRACE_GETFPXREGS
383 /* Fill GDB's register array with the floating-point and SSE register
384 values in *FPXREGSETP. */
387 supply_fpxregset (elf_fpxregset_t
*fpxregsetp
)
389 i387_supply_fxsave (current_regcache
, -1, fpxregsetp
);
392 /* Fill register REGNO (if it is a floating-point or SSE register) in
393 *FPXREGSETP with the value in GDB's register array. If REGNO is
394 -1, do this for all registers. */
397 fill_fpxregset (elf_fpxregset_t
*fpxregsetp
, int regno
)
399 i387_fill_fxsave ((char *) fpxregsetp
, regno
);
402 /* Fetch all registers covered by the PTRACE_GETFPXREGS request from
403 process/thread TID and store their values in GDB's register array.
404 Return non-zero if successful, zero otherwise. */
407 fetch_fpxregs (int tid
)
409 elf_fpxregset_t fpxregs
;
411 if (! have_ptrace_getfpxregs
)
414 if (ptrace (PTRACE_GETFPXREGS
, tid
, 0, (int) &fpxregs
) < 0)
418 have_ptrace_getfpxregs
= 0;
422 perror_with_name ("Couldn't read floating-point and SSE registers");
425 supply_fpxregset (&fpxregs
);
429 /* Store all valid registers in GDB's register array covered by the
430 PTRACE_SETFPXREGS request into the process/thread specified by TID.
431 Return non-zero if successful, zero otherwise. */
434 store_fpxregs (int tid
, int regno
)
436 elf_fpxregset_t fpxregs
;
438 if (! have_ptrace_getfpxregs
)
441 if (ptrace (PTRACE_GETFPXREGS
, tid
, 0, &fpxregs
) == -1)
445 have_ptrace_getfpxregs
= 0;
449 perror_with_name ("Couldn't read floating-point and SSE registers");
452 fill_fpxregset (&fpxregs
, regno
);
454 if (ptrace (PTRACE_SETFPXREGS
, tid
, 0, &fpxregs
) == -1)
455 perror_with_name ("Couldn't write floating-point and SSE registers");
460 /* Fill the XMM registers in the register array with dummy values. For
461 cases where we don't have access to the XMM registers. I think
462 this is cleaner than printing a warning. For a cleaner solution,
463 we should gdbarchify the i386 family. */
466 dummy_sse_values (void)
468 struct gdbarch_tdep
*tdep
= gdbarch_tdep (current_gdbarch
);
469 /* C doesn't have a syntax for NaN's, so write it out as an array of
471 static long dummy
[4] = { 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff };
472 static long mxcsr
= 0x1f80;
475 for (reg
= 0; reg
< tdep
->num_xmm_regs
; reg
++)
476 regcache_raw_supply (current_regcache
, XMM0_REGNUM
+ reg
, (char *) dummy
);
477 if (tdep
->num_xmm_regs
> 0)
478 regcache_raw_supply (current_regcache
, MXCSR_REGNUM
, (char *) &mxcsr
);
483 static int fetch_fpxregs (int tid
) { return 0; }
484 static int store_fpxregs (int tid
, int regno
) { return 0; }
485 static void dummy_sse_values (void) {}
487 #endif /* HAVE_PTRACE_GETFPXREGS */
490 /* Transferring arbitrary registers between GDB and inferior. */
492 /* Check if register REGNO in the child process is accessible.
493 If we are accessing registers directly via the U area, only the
494 general-purpose registers are available.
495 All registers should be accessible if we have GETREGS support. */
498 cannot_fetch_register (int regno
)
500 gdb_assert (regno
>= 0 && regno
< NUM_REGS
);
501 return (!have_ptrace_getregs
&& regmap
[regno
] == -1);
505 cannot_store_register (int regno
)
507 gdb_assert (regno
>= 0 && regno
< NUM_REGS
);
508 return (!have_ptrace_getregs
&& regmap
[regno
] == -1);
511 /* Fetch register REGNO from the child process. If REGNO is -1, do
512 this for all registers (including the floating point and SSE
516 fetch_inferior_registers (int regno
)
520 /* Use the old method of peeking around in `struct user' if the
521 GETREGS request isn't available. */
522 if (!have_ptrace_getregs
)
526 for (i
= 0; i
< NUM_REGS
; i
++)
527 if (regno
== -1 || regno
== i
)
533 /* GNU/Linux LWP ID's are process ID's. */
534 tid
= TIDGET (inferior_ptid
);
536 tid
= PIDGET (inferior_ptid
); /* Not a threaded program. */
538 /* Use the PTRACE_GETFPXREGS request whenever possible, since it
539 transfers more registers in one system call, and we'll cache the
540 results. But remember that fetch_fpxregs can fail, and return
546 /* The call above might reset `have_ptrace_getregs'. */
547 if (!have_ptrace_getregs
)
549 fetch_inferior_registers (regno
);
553 if (fetch_fpxregs (tid
))
559 if (GETREGS_SUPPLIES (regno
))
565 if (GETFPXREGS_SUPPLIES (regno
))
567 if (fetch_fpxregs (tid
))
570 /* Either our processor or our kernel doesn't support the SSE
571 registers, so read the FP registers in the traditional way,
572 and fill the SSE registers with dummy values. It would be
573 more graceful to handle differences in the register set using
574 gdbarch. Until then, this will at least make things work
580 internal_error (__FILE__
, __LINE__
,
581 "Got request for bad register number %d.", regno
);
584 /* Store register REGNO back into the child process. If REGNO is -1,
585 do this for all registers (including the floating point and SSE
588 store_inferior_registers (int regno
)
592 /* Use the old method of poking around in `struct user' if the
593 SETREGS request isn't available. */
594 if (!have_ptrace_getregs
)
598 for (i
= 0; i
< NUM_REGS
; i
++)
599 if (regno
== -1 || regno
== i
)
605 /* GNU/Linux LWP ID's are process ID's. */
606 tid
= TIDGET (inferior_ptid
);
608 tid
= PIDGET (inferior_ptid
); /* Not a threaded program. */
610 /* Use the PTRACE_SETFPXREGS requests whenever possible, since it
611 transfers more registers in one system call. But remember that
612 store_fpxregs can fail, and return zero. */
615 store_regs (tid
, regno
);
616 if (store_fpxregs (tid
, regno
))
618 store_fpregs (tid
, regno
);
622 if (GETREGS_SUPPLIES (regno
))
624 store_regs (tid
, regno
);
628 if (GETFPXREGS_SUPPLIES (regno
))
630 if (store_fpxregs (tid
, regno
))
633 /* Either our processor or our kernel doesn't support the SSE
634 registers, so just write the FP registers in the traditional
636 store_fpregs (tid
, regno
);
640 internal_error (__FILE__
, __LINE__
,
641 "Got request to store bad register number %d.", regno
);
645 /* Support for debug registers. */
648 i386_linux_dr_get (int regnum
)
653 /* FIXME: kettenis/2001-01-29: It's not clear what we should do with
654 multi-threaded processes here. For now, pretend there is just
656 tid
= PIDGET (inferior_ptid
);
658 /* FIXME: kettenis/2001-03-27: Calling perror_with_name if the
659 ptrace call fails breaks debugging remote targets. The correct
660 way to fix this is to add the hardware breakpoint and watchpoint
661 stuff to the target vectore. For now, just return zero if the
662 ptrace call fails. */
664 value
= ptrace (PTRACE_PEEKUSER
, tid
,
665 offsetof (struct user
, u_debugreg
[regnum
]), 0);
668 perror_with_name ("Couldn't read debug register");
677 i386_linux_dr_set (int regnum
, unsigned long value
)
681 /* FIXME: kettenis/2001-01-29: It's not clear what we should do with
682 multi-threaded processes here. For now, pretend there is just
684 tid
= PIDGET (inferior_ptid
);
687 ptrace (PTRACE_POKEUSER
, tid
,
688 offsetof (struct user
, u_debugreg
[regnum
]), value
);
690 perror_with_name ("Couldn't write debug register");
694 i386_linux_dr_set_control (unsigned long control
)
696 i386_linux_dr_set (DR_CONTROL
, control
);
700 i386_linux_dr_set_addr (int regnum
, CORE_ADDR addr
)
702 gdb_assert (regnum
>= 0 && regnum
<= DR_LASTADDR
- DR_FIRSTADDR
);
704 i386_linux_dr_set (DR_FIRSTADDR
+ regnum
, addr
);
708 i386_linux_dr_reset_addr (int regnum
)
710 gdb_assert (regnum
>= 0 && regnum
<= DR_LASTADDR
- DR_FIRSTADDR
);
712 i386_linux_dr_set (DR_FIRSTADDR
+ regnum
, 0L);
716 i386_linux_dr_get_status (void)
718 return i386_linux_dr_get (DR_STATUS
);
722 /* Called by libthread_db. Returns a pointer to the thread local
723 storage (or its descriptor). */
726 ps_get_thread_area (const struct ps_prochandle
*ph
,
727 lwpid_t lwpid
, int idx
, void **base
)
729 /* NOTE: cagney/2003-08-26: The definition of this buffer is found
730 in the kernel header <asm-i386/ldt.h>. It, after padding, is 4 x
731 4 byte integers in size: `entry_number', `base_addr', `limit',
732 and a bunch of status bits.
734 The values returned by this ptrace call should be part of the
735 regcache buffer, and ps_get_thread_area should channel its
736 request through the regcache. That way remote targets could
737 provide the value using the remote protocol and not this direct
740 Is this function needed? I'm guessing that the `base' is the
741 address of a a descriptor that libthread_db uses to find the
742 thread local address base that GDB needs. Perhaps that
743 descriptor is defined by the ABI. Anyway, given that
744 libthread_db calls this function without prompting (gdb
745 requesting tls base) I guess it needs info in there anyway. */
746 unsigned int desc
[4];
747 gdb_assert (sizeof (int) == 4);
749 #ifndef PTRACE_GET_THREAD_AREA
750 #define PTRACE_GET_THREAD_AREA 25
753 if (ptrace (PTRACE_GET_THREAD_AREA
, lwpid
,
754 (void *) idx
, (unsigned long) &desc
) < 0)
757 *(int *)base
= desc
[1];
762 /* The instruction for a GNU/Linux system call is:
766 static const unsigned char linux_syscall
[] = { 0xcd, 0x80 };
768 #define LINUX_SYSCALL_LEN (sizeof linux_syscall)
770 /* The system call number is stored in the %eax register. */
771 #define LINUX_SYSCALL_REGNUM 0 /* %eax */
773 /* We are specifically interested in the sigreturn and rt_sigreturn
776 #ifndef SYS_sigreturn
777 #define SYS_sigreturn 0x77
779 #ifndef SYS_rt_sigreturn
780 #define SYS_rt_sigreturn 0xad
783 /* Offset to saved processor flags, from <asm/sigcontext.h>. */
784 #define LINUX_SIGCONTEXT_EFLAGS_OFFSET (64)
786 /* Resume execution of the inferior process.
787 If STEP is nonzero, single-step it.
788 If SIGNAL is nonzero, give it that signal. */
791 child_resume (ptid_t ptid
, int step
, enum target_signal signal
)
793 int pid
= PIDGET (ptid
);
795 int request
= PTRACE_CONT
;
798 /* Resume all threads. */
799 /* I think this only gets used in the non-threaded case, where "resume
800 all threads" and "resume inferior_ptid" are the same. */
801 pid
= PIDGET (inferior_ptid
);
805 CORE_ADDR pc
= read_pc_pid (pid_to_ptid (pid
));
806 unsigned char buf
[LINUX_SYSCALL_LEN
];
808 request
= PTRACE_SINGLESTEP
;
810 /* Returning from a signal trampoline is done by calling a
811 special system call (sigreturn or rt_sigreturn, see
812 i386-linux-tdep.c for more information). This system call
813 restores the registers that were saved when the signal was
814 raised, including %eflags. That means that single-stepping
815 won't work. Instead, we'll have to modify the signal context
816 that's about to be restored, and set the trace flag there. */
818 /* First check if PC is at a system call. */
819 if (deprecated_read_memory_nobpt (pc
, (char *) buf
, LINUX_SYSCALL_LEN
) == 0
820 && memcmp (buf
, linux_syscall
, LINUX_SYSCALL_LEN
) == 0)
822 int syscall
= read_register_pid (LINUX_SYSCALL_REGNUM
,
825 /* Then check the system call number. */
826 if (syscall
== SYS_sigreturn
|| syscall
== SYS_rt_sigreturn
)
828 CORE_ADDR sp
= read_register (I386_ESP_REGNUM
);
830 unsigned long int eflags
;
832 if (syscall
== SYS_rt_sigreturn
)
833 addr
= read_memory_integer (sp
+ 8, 4) + 20;
835 /* Set the trace flag in the context that's about to be
837 addr
+= LINUX_SIGCONTEXT_EFLAGS_OFFSET
;
838 read_memory (addr
, (char *) &eflags
, 4);
840 write_memory (addr
, (char *) &eflags
, 4);
845 if (ptrace (request
, pid
, 0, target_signal_to_host (signal
)) == -1)
846 perror_with_name ("ptrace");
850 child_post_startup_inferior (ptid_t ptid
)
852 i386_cleanup_dregs ();
853 linux_child_post_startup_inferior (ptid
);