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 (int regno
)
150 gdb_assert (!have_ptrace_getregs
);
151 if (regmap
[regno
] == -1)
153 regcache_raw_supply (current_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."), REGISTER_NAME (regno
),
166 regno
, safe_strerror (errno
));
168 regcache_raw_supply (current_regcache
, regno
, &val
);
171 /* Store one register. */
174 store_register (int regno
)
179 gdb_assert (!have_ptrace_getregs
);
180 if (regmap
[regno
] == -1)
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 regcache_raw_collect (current_regcache
, regno
, &val
);
190 ptrace (PTRACE_POKEUSER
, tid
, 4 * regmap
[regno
], val
);
192 error (_("Couldn't write register %s (#%d): %s."), REGISTER_NAME (regno
),
193 regno
, safe_strerror (errno
));
197 /* Transfering the general-purpose registers between GDB, inferiors
200 /* Fill GDB's register array with the general-purpose register values
204 supply_gregset (elf_gregset_t
*gregsetp
)
206 elf_greg_t
*regp
= (elf_greg_t
*) gregsetp
;
209 for (i
= 0; i
< I386_NUM_GREGS
; i
++)
210 regcache_raw_supply (current_regcache
, i
, regp
+ regmap
[i
]);
212 if (I386_LINUX_ORIG_EAX_REGNUM
< NUM_REGS
)
213 regcache_raw_supply (current_regcache
, I386_LINUX_ORIG_EAX_REGNUM
,
217 /* Fill register REGNO (if it is a general-purpose register) in
218 *GREGSETPS with the value in GDB's register array. If REGNO is -1,
219 do this for all registers. */
222 fill_gregset (elf_gregset_t
*gregsetp
, int regno
)
224 elf_greg_t
*regp
= (elf_greg_t
*) gregsetp
;
227 for (i
= 0; i
< I386_NUM_GREGS
; i
++)
228 if (regno
== -1 || regno
== i
)
229 regcache_raw_collect (current_regcache
, i
, regp
+ regmap
[i
]);
231 if ((regno
== -1 || regno
== I386_LINUX_ORIG_EAX_REGNUM
)
232 && I386_LINUX_ORIG_EAX_REGNUM
< NUM_REGS
)
233 regcache_raw_collect (current_regcache
, I386_LINUX_ORIG_EAX_REGNUM
,
237 #ifdef HAVE_PTRACE_GETREGS
239 /* Fetch all general-purpose registers from process/thread TID and
240 store their values in GDB's register array. */
247 if (ptrace (PTRACE_GETREGS
, tid
, 0, (int) ®s
) < 0)
251 /* The kernel we're running on doesn't support the GETREGS
252 request. Reset `have_ptrace_getregs'. */
253 have_ptrace_getregs
= 0;
257 perror_with_name (_("Couldn't get registers"));
260 supply_gregset (®s
);
263 /* Store all valid general-purpose registers in GDB's register array
264 into the process/thread specified by TID. */
267 store_regs (int tid
, int regno
)
271 if (ptrace (PTRACE_GETREGS
, tid
, 0, (int) ®s
) < 0)
272 perror_with_name (_("Couldn't get registers"));
274 fill_gregset (®s
, regno
);
276 if (ptrace (PTRACE_SETREGS
, tid
, 0, (int) ®s
) < 0)
277 perror_with_name (_("Couldn't write registers"));
282 static void fetch_regs (int tid
) {}
283 static void store_regs (int tid
, int regno
) {}
288 /* Transfering floating-point registers between GDB, inferiors and cores. */
290 /* Fill GDB's register array with the floating-point register values in
294 supply_fpregset (elf_fpregset_t
*fpregsetp
)
296 i387_supply_fsave (current_regcache
, -1, fpregsetp
);
299 /* Fill register REGNO (if it is a floating-point register) in
300 *FPREGSETP with the value in GDB's register array. If REGNO is -1,
301 do this for all registers. */
304 fill_fpregset (elf_fpregset_t
*fpregsetp
, int regno
)
306 i387_fill_fsave ((char *) fpregsetp
, regno
);
309 #ifdef HAVE_PTRACE_GETREGS
311 /* Fetch all floating-point registers from process/thread TID and store
312 thier values in GDB's register array. */
315 fetch_fpregs (int tid
)
317 elf_fpregset_t fpregs
;
319 if (ptrace (PTRACE_GETFPREGS
, tid
, 0, (int) &fpregs
) < 0)
320 perror_with_name (_("Couldn't get floating point status"));
322 supply_fpregset (&fpregs
);
325 /* Store all valid floating-point registers in GDB's register array
326 into the process/thread specified by TID. */
329 store_fpregs (int tid
, int regno
)
331 elf_fpregset_t fpregs
;
333 if (ptrace (PTRACE_GETFPREGS
, tid
, 0, (int) &fpregs
) < 0)
334 perror_with_name (_("Couldn't get floating point status"));
336 fill_fpregset (&fpregs
, regno
);
338 if (ptrace (PTRACE_SETFPREGS
, tid
, 0, (int) &fpregs
) < 0)
339 perror_with_name (_("Couldn't write floating point status"));
344 static void fetch_fpregs (int tid
) {}
345 static void store_fpregs (int tid
, int regno
) {}
350 /* Transfering floating-point and SSE registers to and from GDB. */
352 #ifdef HAVE_PTRACE_GETFPXREGS
354 /* Fill GDB's register array with the floating-point and SSE register
355 values in *FPXREGSETP. */
358 supply_fpxregset (elf_fpxregset_t
*fpxregsetp
)
360 i387_supply_fxsave (current_regcache
, -1, fpxregsetp
);
363 /* Fill register REGNO (if it is a floating-point or SSE register) in
364 *FPXREGSETP with the value in GDB's register array. If REGNO is
365 -1, do this for all registers. */
368 fill_fpxregset (elf_fpxregset_t
*fpxregsetp
, int regno
)
370 i387_fill_fxsave ((char *) fpxregsetp
, regno
);
373 /* Fetch all registers covered by the PTRACE_GETFPXREGS request from
374 process/thread TID and store their values in GDB's register array.
375 Return non-zero if successful, zero otherwise. */
378 fetch_fpxregs (int tid
)
380 elf_fpxregset_t fpxregs
;
382 if (! have_ptrace_getfpxregs
)
385 if (ptrace (PTRACE_GETFPXREGS
, tid
, 0, (int) &fpxregs
) < 0)
389 have_ptrace_getfpxregs
= 0;
393 perror_with_name (_("Couldn't read floating-point and SSE registers"));
396 supply_fpxregset (&fpxregs
);
400 /* Store all valid registers in GDB's register array covered by the
401 PTRACE_SETFPXREGS request into the process/thread specified by TID.
402 Return non-zero if successful, zero otherwise. */
405 store_fpxregs (int tid
, int regno
)
407 elf_fpxregset_t fpxregs
;
409 if (! have_ptrace_getfpxregs
)
412 if (ptrace (PTRACE_GETFPXREGS
, tid
, 0, &fpxregs
) == -1)
416 have_ptrace_getfpxregs
= 0;
420 perror_with_name (_("Couldn't read floating-point and SSE registers"));
423 fill_fpxregset (&fpxregs
, regno
);
425 if (ptrace (PTRACE_SETFPXREGS
, tid
, 0, &fpxregs
) == -1)
426 perror_with_name (_("Couldn't write floating-point and SSE registers"));
433 static int fetch_fpxregs (int tid
) { return 0; }
434 static int store_fpxregs (int tid
, int regno
) { return 0; }
436 #endif /* HAVE_PTRACE_GETFPXREGS */
439 /* Transferring arbitrary registers between GDB and inferior. */
441 /* Fetch register REGNO from the child process. If REGNO is -1, do
442 this for all registers (including the floating point and SSE
446 i386_linux_fetch_inferior_registers (int regno
)
450 /* Use the old method of peeking around in `struct user' if the
451 GETREGS request isn't available. */
452 if (!have_ptrace_getregs
)
456 for (i
= 0; i
< NUM_REGS
; i
++)
457 if (regno
== -1 || regno
== i
)
463 /* GNU/Linux LWP ID's are process ID's. */
464 tid
= TIDGET (inferior_ptid
);
466 tid
= PIDGET (inferior_ptid
); /* Not a threaded program. */
468 /* Use the PTRACE_GETFPXREGS request whenever possible, since it
469 transfers more registers in one system call, and we'll cache the
470 results. But remember that fetch_fpxregs can fail, and return
476 /* The call above might reset `have_ptrace_getregs'. */
477 if (!have_ptrace_getregs
)
479 i386_linux_fetch_inferior_registers (regno
);
483 if (fetch_fpxregs (tid
))
489 if (GETREGS_SUPPLIES (regno
))
495 if (GETFPXREGS_SUPPLIES (regno
))
497 if (fetch_fpxregs (tid
))
500 /* Either our processor or our kernel doesn't support the SSE
501 registers, so read the FP registers in the traditional way,
502 and fill the SSE registers with dummy values. It would be
503 more graceful to handle differences in the register set using
504 gdbarch. Until then, this will at least make things work
510 internal_error (__FILE__
, __LINE__
,
511 _("Got request for bad register number %d."), regno
);
514 /* Store register REGNO back into the child process. If REGNO is -1,
515 do this for all registers (including the floating point and SSE
518 i386_linux_store_inferior_registers (int regno
)
522 /* Use the old method of poking around in `struct user' if the
523 SETREGS request isn't available. */
524 if (!have_ptrace_getregs
)
528 for (i
= 0; i
< NUM_REGS
; i
++)
529 if (regno
== -1 || regno
== i
)
535 /* GNU/Linux LWP ID's are process ID's. */
536 tid
= TIDGET (inferior_ptid
);
538 tid
= PIDGET (inferior_ptid
); /* Not a threaded program. */
540 /* Use the PTRACE_SETFPXREGS requests whenever possible, since it
541 transfers more registers in one system call. But remember that
542 store_fpxregs can fail, and return zero. */
545 store_regs (tid
, regno
);
546 if (store_fpxregs (tid
, regno
))
548 store_fpregs (tid
, regno
);
552 if (GETREGS_SUPPLIES (regno
))
554 store_regs (tid
, regno
);
558 if (GETFPXREGS_SUPPLIES (regno
))
560 if (store_fpxregs (tid
, regno
))
563 /* Either our processor or our kernel doesn't support the SSE
564 registers, so just write the FP registers in the traditional
566 store_fpregs (tid
, regno
);
570 internal_error (__FILE__
, __LINE__
,
571 _("Got request to store bad register number %d."), regno
);
575 /* Support for debug registers. */
578 i386_linux_dr_get (int regnum
)
583 /* FIXME: kettenis/2001-01-29: It's not clear what we should do with
584 multi-threaded processes here. For now, pretend there is just
586 tid
= PIDGET (inferior_ptid
);
588 /* FIXME: kettenis/2001-03-27: Calling perror_with_name if the
589 ptrace call fails breaks debugging remote targets. The correct
590 way to fix this is to add the hardware breakpoint and watchpoint
591 stuff to the target vector. For now, just return zero if the
592 ptrace call fails. */
594 value
= ptrace (PTRACE_PEEKUSER
, tid
,
595 offsetof (struct user
, u_debugreg
[regnum
]), 0);
598 perror_with_name (_("Couldn't read debug register"));
607 i386_linux_dr_set (int regnum
, unsigned long value
)
611 /* FIXME: kettenis/2001-01-29: It's not clear what we should do with
612 multi-threaded processes here. For now, pretend there is just
614 tid
= PIDGET (inferior_ptid
);
617 ptrace (PTRACE_POKEUSER
, tid
,
618 offsetof (struct user
, u_debugreg
[regnum
]), value
);
620 perror_with_name (_("Couldn't write debug register"));
624 i386_linux_dr_set_control (unsigned long control
)
626 i386_linux_dr_set (DR_CONTROL
, control
);
630 i386_linux_dr_set_addr (int regnum
, CORE_ADDR addr
)
632 gdb_assert (regnum
>= 0 && regnum
<= DR_LASTADDR
- DR_FIRSTADDR
);
634 i386_linux_dr_set (DR_FIRSTADDR
+ regnum
, addr
);
638 i386_linux_dr_reset_addr (int regnum
)
640 gdb_assert (regnum
>= 0 && regnum
<= DR_LASTADDR
- DR_FIRSTADDR
);
642 i386_linux_dr_set (DR_FIRSTADDR
+ regnum
, 0L);
646 i386_linux_dr_get_status (void)
648 return i386_linux_dr_get (DR_STATUS
);
652 /* Called by libthread_db. Returns a pointer to the thread local
653 storage (or its descriptor). */
656 ps_get_thread_area (const struct ps_prochandle
*ph
,
657 lwpid_t lwpid
, int idx
, void **base
)
659 /* NOTE: cagney/2003-08-26: The definition of this buffer is found
660 in the kernel header <asm-i386/ldt.h>. It, after padding, is 4 x
661 4 byte integers in size: `entry_number', `base_addr', `limit',
662 and a bunch of status bits.
664 The values returned by this ptrace call should be part of the
665 regcache buffer, and ps_get_thread_area should channel its
666 request through the regcache. That way remote targets could
667 provide the value using the remote protocol and not this direct
670 Is this function needed? I'm guessing that the `base' is the
671 address of a a descriptor that libthread_db uses to find the
672 thread local address base that GDB needs. Perhaps that
673 descriptor is defined by the ABI. Anyway, given that
674 libthread_db calls this function without prompting (gdb
675 requesting tls base) I guess it needs info in there anyway. */
676 unsigned int desc
[4];
677 gdb_assert (sizeof (int) == 4);
679 #ifndef PTRACE_GET_THREAD_AREA
680 #define PTRACE_GET_THREAD_AREA 25
683 if (ptrace (PTRACE_GET_THREAD_AREA
, lwpid
,
684 (void *) idx
, (unsigned long) &desc
) < 0)
687 *(int *)base
= desc
[1];
692 /* The instruction for a GNU/Linux system call is:
696 static const unsigned char linux_syscall
[] = { 0xcd, 0x80 };
698 #define LINUX_SYSCALL_LEN (sizeof linux_syscall)
700 /* The system call number is stored in the %eax register. */
701 #define LINUX_SYSCALL_REGNUM I386_EAX_REGNUM
703 /* We are specifically interested in the sigreturn and rt_sigreturn
706 #ifndef SYS_sigreturn
707 #define SYS_sigreturn 0x77
709 #ifndef SYS_rt_sigreturn
710 #define SYS_rt_sigreturn 0xad
713 /* Offset to saved processor flags, from <asm/sigcontext.h>. */
714 #define LINUX_SIGCONTEXT_EFLAGS_OFFSET (64)
716 /* Resume execution of the inferior process.
717 If STEP is nonzero, single-step it.
718 If SIGNAL is nonzero, give it that signal. */
721 i386_linux_resume (ptid_t ptid
, int step
, enum target_signal signal
)
723 int pid
= PIDGET (ptid
);
725 int request
= PTRACE_CONT
;
728 /* Resume all threads. */
729 /* I think this only gets used in the non-threaded case, where "resume
730 all threads" and "resume inferior_ptid" are the same. */
731 pid
= PIDGET (inferior_ptid
);
735 CORE_ADDR pc
= read_pc_pid (pid_to_ptid (pid
));
736 gdb_byte buf
[LINUX_SYSCALL_LEN
];
738 request
= PTRACE_SINGLESTEP
;
740 /* Returning from a signal trampoline is done by calling a
741 special system call (sigreturn or rt_sigreturn, see
742 i386-linux-tdep.c for more information). This system call
743 restores the registers that were saved when the signal was
744 raised, including %eflags. That means that single-stepping
745 won't work. Instead, we'll have to modify the signal context
746 that's about to be restored, and set the trace flag there. */
748 /* First check if PC is at a system call. */
749 if (read_memory_nobpt (pc
, buf
, LINUX_SYSCALL_LEN
) == 0
750 && memcmp (buf
, linux_syscall
, LINUX_SYSCALL_LEN
) == 0)
752 int syscall
= read_register_pid (LINUX_SYSCALL_REGNUM
,
755 /* Then check the system call number. */
756 if (syscall
== SYS_sigreturn
|| syscall
== SYS_rt_sigreturn
)
758 CORE_ADDR sp
= read_register (I386_ESP_REGNUM
);
760 unsigned long int eflags
;
762 if (syscall
== SYS_rt_sigreturn
)
763 addr
= read_memory_integer (sp
+ 8, 4) + 20;
765 /* Set the trace flag in the context that's about to be
767 addr
+= LINUX_SIGCONTEXT_EFLAGS_OFFSET
;
768 read_memory (addr
, (gdb_byte
*) &eflags
, 4);
770 write_memory (addr
, (gdb_byte
*) &eflags
, 4);
775 if (ptrace (request
, pid
, 0, target_signal_to_host (signal
)) == -1)
776 perror_with_name (("ptrace"));
779 static void (*super_post_startup_inferior
) (ptid_t ptid
);
782 i386_linux_child_post_startup_inferior (ptid_t ptid
)
784 i386_cleanup_dregs ();
785 super_post_startup_inferior (ptid
);
789 _initialize_i386_linux_nat (void)
791 struct target_ops
*t
;
793 /* Fill in the generic GNU/Linux methods. */
796 /* Override the default ptrace resume method. */
797 t
->to_resume
= i386_linux_resume
;
799 /* Override the GNU/Linux inferior startup hook. */
800 super_post_startup_inferior
= t
->to_post_startup_inferior
;
801 t
->to_post_startup_inferior
= i386_linux_child_post_startup_inferior
;
803 /* Add our register access methods. */
804 t
->to_fetch_registers
= i386_linux_fetch_inferior_registers
;
805 t
->to_store_registers
= i386_linux_store_inferior_registers
;
807 /* Register the target. */
808 linux_nat_add_target (t
);