1 /* GNU/Linux/x86-64 specific low level interface, for the remote server
3 Copyright (C) 2002, 2004, 2005, 2006, 2007, 2008, 2009, 2010
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 3 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, see <http://www.gnu.org/licenses/>. */
24 #include "linux-low.h"
28 #include "gdb_proc_service.h"
30 /* Defined in auto-generated file i386-linux.c. */
31 void init_registers_i386_linux (void);
32 /* Defined in auto-generated file amd64-linux.c. */
33 void init_registers_amd64_linux (void);
36 #include <sys/procfs.h>
37 #include <sys/ptrace.h>
39 #ifndef PTRACE_GET_THREAD_AREA
40 #define PTRACE_GET_THREAD_AREA 25
43 /* This definition comes from prctl.h, but some kernels may not have it. */
44 #ifndef PTRACE_ARCH_PRCTL
45 #define PTRACE_ARCH_PRCTL 30
48 /* The following definitions come from prctl.h, but may be absent
49 for certain configurations. */
51 #define ARCH_SET_GS 0x1001
52 #define ARCH_SET_FS 0x1002
53 #define ARCH_GET_FS 0x1003
54 #define ARCH_GET_GS 0x1004
57 /* Per-process arch-specific data we want to keep. */
59 struct arch_process_info
61 struct i386_debug_reg_state debug_reg_state;
64 /* Per-thread arch-specific data we want to keep. */
68 /* Non-zero if our copy differs from what's recorded in the thread. */
69 int debug_registers_changed;
74 /* Mapping between the general-purpose registers in `struct user'
75 format and GDB's register array layout.
76 Note that the transfer layout uses 64-bit regs. */
77 static /*const*/ int i386_regmap[] =
79 RAX * 8, RCX * 8, RDX * 8, RBX * 8,
80 RSP * 8, RBP * 8, RSI * 8, RDI * 8,
81 RIP * 8, EFLAGS * 8, CS * 8, SS * 8,
82 DS * 8, ES * 8, FS * 8, GS * 8
85 #define I386_NUM_REGS (sizeof (i386_regmap) / sizeof (i386_regmap[0]))
87 /* So code below doesn't have to care, i386 or amd64. */
88 #define ORIG_EAX ORIG_RAX
90 static const int x86_64_regmap[] =
92 RAX * 8, RBX * 8, RCX * 8, RDX * 8,
93 RSI * 8, RDI * 8, RBP * 8, RSP * 8,
94 R8 * 8, R9 * 8, R10 * 8, R11 * 8,
95 R12 * 8, R13 * 8, R14 * 8, R15 * 8,
96 RIP * 8, EFLAGS * 8, CS * 8, SS * 8,
97 DS * 8, ES * 8, FS * 8, GS * 8,
98 -1, -1, -1, -1, -1, -1, -1, -1,
99 -1, -1, -1, -1, -1, -1, -1, -1,
100 -1, -1, -1, -1, -1, -1, -1, -1,
101 -1, -1, -1, -1, -1, -1, -1, -1, -1,
105 #define X86_64_NUM_REGS (sizeof (x86_64_regmap) / sizeof (x86_64_regmap[0]))
107 #else /* ! __x86_64__ */
109 /* Mapping between the general-purpose registers in `struct user'
110 format and GDB's register array layout. */
111 static /*const*/ int i386_regmap[] =
113 EAX * 4, ECX * 4, EDX * 4, EBX * 4,
114 UESP * 4, EBP * 4, ESI * 4, EDI * 4,
115 EIP * 4, EFL * 4, CS * 4, SS * 4,
116 DS * 4, ES * 4, FS * 4, GS * 4
119 #define I386_NUM_REGS (sizeof (i386_regmap) / sizeof (i386_regmap[0]))
123 /* Called by libthread_db. */
126 ps_get_thread_area (const struct ps_prochandle *ph,
127 lwpid_t lwpid, int idx, void **base)
130 int use_64bit = register_size (0) == 8;
137 if (ptrace (PTRACE_ARCH_PRCTL, lwpid, base, ARCH_GET_FS) == 0)
141 if (ptrace (PTRACE_ARCH_PRCTL, lwpid, base, ARCH_GET_GS) == 0)
152 unsigned int desc[4];
154 if (ptrace (PTRACE_GET_THREAD_AREA, lwpid,
155 (void *) (intptr_t) idx, (unsigned long) &desc) < 0)
158 *(int *)base = desc[1];
164 i386_cannot_store_register (int regno)
166 return regno >= I386_NUM_REGS;
170 i386_cannot_fetch_register (int regno)
172 return regno >= I386_NUM_REGS;
176 x86_fill_gregset (struct regcache *regcache, void *buf)
181 if (register_size (0) == 8)
183 for (i = 0; i < X86_64_NUM_REGS; i++)
184 if (x86_64_regmap[i] != -1)
185 collect_register (regcache, i, ((char *) buf) + x86_64_regmap[i]);
190 for (i = 0; i < I386_NUM_REGS; i++)
191 collect_register (regcache, i, ((char *) buf) + i386_regmap[i]);
193 collect_register_by_name (regcache, "orig_eax",
194 ((char *) buf) + ORIG_EAX * 4);
198 x86_store_gregset (struct regcache *regcache, const void *buf)
203 if (register_size (0) == 8)
205 for (i = 0; i < X86_64_NUM_REGS; i++)
206 if (x86_64_regmap[i] != -1)
207 supply_register (regcache, i, ((char *) buf) + x86_64_regmap[i]);
212 for (i = 0; i < I386_NUM_REGS; i++)
213 supply_register (regcache, i, ((char *) buf) + i386_regmap[i]);
215 supply_register_by_name (regcache, "orig_eax",
216 ((char *) buf) + ORIG_EAX * 4);
220 x86_fill_fpregset (struct regcache *regcache, void *buf)
223 i387_cache_to_fxsave (regcache, buf);
225 i387_cache_to_fsave (regcache, buf);
230 x86_store_fpregset (struct regcache *regcache, const void *buf)
233 i387_fxsave_to_cache (regcache, buf);
235 i387_fsave_to_cache (regcache, buf);
242 x86_fill_fpxregset (struct regcache *regcache, void *buf)
244 i387_cache_to_fxsave (regcache, buf);
248 x86_store_fpxregset (struct regcache *regcache, const void *buf)
250 i387_fxsave_to_cache (regcache, buf);
255 /* ??? The non-biarch i386 case stores all the i387 regs twice.
256 Once in i387_.*fsave.* and once in i387_.*fxsave.*.
257 This is, presumably, to handle the case where PTRACE_[GS]ETFPXREGS
258 doesn't work. IWBN to avoid the duplication in the case where it
259 does work. Maybe the arch_setup routine could check whether it works
260 and update target_regsets accordingly, maybe by moving target_regsets
261 to linux_target_ops and set the right one there, rather than having to
262 modify the target_regsets global. */
264 struct regset_info target_regsets[] =
266 #ifdef HAVE_PTRACE_GETREGS
267 { PTRACE_GETREGS, PTRACE_SETREGS, sizeof (elf_gregset_t),
269 x86_fill_gregset, x86_store_gregset },
271 # ifdef HAVE_PTRACE_GETFPXREGS
272 { PTRACE_GETFPXREGS, PTRACE_SETFPXREGS, sizeof (elf_fpxregset_t),
274 x86_fill_fpxregset, x86_store_fpxregset },
277 { PTRACE_GETFPREGS, PTRACE_SETFPREGS, sizeof (elf_fpregset_t),
279 x86_fill_fpregset, x86_store_fpregset },
280 #endif /* HAVE_PTRACE_GETREGS */
281 { 0, 0, -1, -1, NULL, NULL }
285 x86_get_pc (struct regcache *regcache)
287 int use_64bit = register_size (0) == 8;
292 collect_register_by_name (regcache, "rip", &pc);
293 return (CORE_ADDR) pc;
298 collect_register_by_name (regcache, "eip", &pc);
299 return (CORE_ADDR) pc;
304 x86_set_pc (struct regcache *regcache, CORE_ADDR pc)
306 int use_64bit = register_size (0) == 8;
310 unsigned long newpc = pc;
311 supply_register_by_name (regcache, "rip", &newpc);
315 unsigned int newpc = pc;
316 supply_register_by_name (regcache, "eip", &newpc);
320 static const unsigned char x86_breakpoint[] = { 0xCC };
321 #define x86_breakpoint_len 1
324 x86_breakpoint_at (CORE_ADDR pc)
328 read_inferior_memory (pc, &c, 1);
335 /* Support for debug registers. */
338 x86_linux_dr_get (ptid_t ptid, int regnum)
343 tid = ptid_get_lwp (ptid);
346 value = ptrace (PTRACE_PEEKUSER, tid,
347 offsetof (struct user, u_debugreg[regnum]), 0);
349 error ("Couldn't read debug register");
355 x86_linux_dr_set (ptid_t ptid, int regnum, unsigned long value)
359 tid = ptid_get_lwp (ptid);
362 ptrace (PTRACE_POKEUSER, tid,
363 offsetof (struct user, u_debugreg[regnum]), value);
365 error ("Couldn't write debug register");
368 /* Update the inferior's debug register REGNUM from STATE. */
371 i386_dr_low_set_addr (const struct i386_debug_reg_state *state, int regnum)
373 struct inferior_list_entry *lp;
375 /* Only need to update the threads of this process. */
376 int pid = pid_of (get_thread_lwp (current_inferior));
378 if (! (regnum >= 0 && regnum <= DR_LASTADDR - DR_FIRSTADDR))
379 fatal ("Invalid debug register %d", regnum);
381 addr = state->dr_mirror[regnum];
383 for (lp = all_lwps.head; lp; lp = lp->next)
385 struct lwp_info *lwp = (struct lwp_info *) lp;
387 /* The actual update is done later, we just mark that the register
389 if (pid_of (lwp) == pid)
390 lwp->arch_private->debug_registers_changed = 1;
394 /* Update the inferior's DR7 debug control register from STATE. */
397 i386_dr_low_set_control (const struct i386_debug_reg_state *state)
399 struct inferior_list_entry *lp;
400 /* Only need to update the threads of this process. */
401 int pid = pid_of (get_thread_lwp (current_inferior));
403 for (lp = all_lwps.head; lp; lp = lp->next)
405 struct lwp_info *lwp = (struct lwp_info *) lp;
407 /* The actual update is done later, we just mark that the register
409 if (pid_of (lwp) == pid)
410 lwp->arch_private->debug_registers_changed = 1;
414 /* Get the value of the DR6 debug status register from the inferior
415 and record it in STATE. */
418 i386_dr_low_get_status (struct i386_debug_reg_state *state)
420 struct lwp_info *lwp = get_thread_lwp (current_inferior);
421 ptid_t ptid = ptid_of (lwp);
423 state->dr_status_mirror = x86_linux_dr_get (ptid, DR_STATUS);
426 /* Watchpoint support. */
429 x86_insert_point (char type, CORE_ADDR addr, int len)
431 struct process_info *proc = current_process ();
437 return i386_low_insert_watchpoint (&proc->private->arch_private->debug_reg_state,
446 x86_remove_point (char type, CORE_ADDR addr, int len)
448 struct process_info *proc = current_process ();
454 return i386_low_remove_watchpoint (&proc->private->arch_private->debug_reg_state,
463 x86_stopped_by_watchpoint (void)
465 struct process_info *proc = current_process ();
466 return i386_low_stopped_by_watchpoint (&proc->private->arch_private->debug_reg_state);
470 x86_stopped_data_address (void)
472 struct process_info *proc = current_process ();
474 if (i386_low_stopped_data_address (&proc->private->arch_private->debug_reg_state,
480 /* Called when a new process is created. */
482 static struct arch_process_info *
483 x86_linux_new_process (void)
485 struct arch_process_info *info = xcalloc (1, sizeof (*info));
487 i386_low_init_dregs (&info->debug_reg_state);
492 /* Called when a new thread is detected. */
494 static struct arch_lwp_info *
495 x86_linux_new_thread (void)
497 struct arch_lwp_info *info = xcalloc (1, sizeof (*info));
499 info->debug_registers_changed = 1;
504 /* Called when resuming a thread.
505 If the debug regs have changed, update the thread's copies. */
508 x86_linux_prepare_to_resume (struct lwp_info *lwp)
510 if (lwp->arch_private->debug_registers_changed)
513 ptid_t ptid = ptid_of (lwp);
514 int pid = ptid_get_pid (ptid);
515 struct process_info *proc = find_process_pid (pid);
516 struct i386_debug_reg_state *state = &proc->private->arch_private->debug_reg_state;
518 for (i = DR_FIRSTADDR; i <= DR_LASTADDR; i++)
519 x86_linux_dr_set (ptid, i, state->dr_mirror[i]);
521 x86_linux_dr_set (ptid, DR_CONTROL, state->dr_control_mirror);
523 lwp->arch_private->debug_registers_changed = 0;
527 /* When GDBSERVER is built as a 64-bit application on linux, the
528 PTRACE_GETSIGINFO data is always presented in 64-bit layout. Since
529 debugging a 32-bit inferior with a 64-bit GDBSERVER should look the same
530 as debugging it with a 32-bit GDBSERVER, we do the 32-bit <-> 64-bit
531 conversion in-place ourselves. */
533 /* These types below (compat_*) define a siginfo type that is layout
534 compatible with the siginfo type exported by the 32-bit userspace
539 typedef int compat_int_t;
540 typedef unsigned int compat_uptr_t;
542 typedef int compat_time_t;
543 typedef int compat_timer_t;
544 typedef int compat_clock_t;
546 struct compat_timeval
548 compat_time_t tv_sec;
552 typedef union compat_sigval
554 compat_int_t sival_int;
555 compat_uptr_t sival_ptr;
558 typedef struct compat_siginfo
566 int _pad[((128 / sizeof (int)) - 3)];
575 /* POSIX.1b timers */
580 compat_sigval_t _sigval;
583 /* POSIX.1b signals */
588 compat_sigval_t _sigval;
597 compat_clock_t _utime;
598 compat_clock_t _stime;
601 /* SIGILL, SIGFPE, SIGSEGV, SIGBUS */
616 #define cpt_si_pid _sifields._kill._pid
617 #define cpt_si_uid _sifields._kill._uid
618 #define cpt_si_timerid _sifields._timer._tid
619 #define cpt_si_overrun _sifields._timer._overrun
620 #define cpt_si_status _sifields._sigchld._status
621 #define cpt_si_utime _sifields._sigchld._utime
622 #define cpt_si_stime _sifields._sigchld._stime
623 #define cpt_si_ptr _sifields._rt._sigval.sival_ptr
624 #define cpt_si_addr _sifields._sigfault._addr
625 #define cpt_si_band _sifields._sigpoll._band
626 #define cpt_si_fd _sifields._sigpoll._fd
628 /* glibc at least up to 2.3.2 doesn't have si_timerid, si_overrun.
629 In their place is si_timer1,si_timer2. */
631 #define si_timerid si_timer1
634 #define si_overrun si_timer2
638 compat_siginfo_from_siginfo (compat_siginfo_t *to, siginfo_t *from)
640 memset (to, 0, sizeof (*to));
642 to->si_signo = from->si_signo;
643 to->si_errno = from->si_errno;
644 to->si_code = from->si_code;
648 to->cpt_si_ptr = (intptr_t) from->si_ptr;
650 else if (to->si_code == SI_USER)
652 to->cpt_si_pid = from->si_pid;
653 to->cpt_si_uid = from->si_uid;
655 else if (to->si_code == SI_TIMER)
657 to->cpt_si_timerid = from->si_timerid;
658 to->cpt_si_overrun = from->si_overrun;
659 to->cpt_si_ptr = (intptr_t) from->si_ptr;
663 switch (to->si_signo)
666 to->cpt_si_pid = from->si_pid;
667 to->cpt_si_uid = from->si_uid;
668 to->cpt_si_status = from->si_status;
669 to->cpt_si_utime = from->si_utime;
670 to->cpt_si_stime = from->si_stime;
676 to->cpt_si_addr = (intptr_t) from->si_addr;
679 to->cpt_si_band = from->si_band;
680 to->cpt_si_fd = from->si_fd;
683 to->cpt_si_pid = from->si_pid;
684 to->cpt_si_uid = from->si_uid;
685 to->cpt_si_ptr = (intptr_t) from->si_ptr;
692 siginfo_from_compat_siginfo (siginfo_t *to, compat_siginfo_t *from)
694 memset (to, 0, sizeof (*to));
696 to->si_signo = from->si_signo;
697 to->si_errno = from->si_errno;
698 to->si_code = from->si_code;
702 to->si_ptr = (void *) (intptr_t) from->cpt_si_ptr;
704 else if (to->si_code == SI_USER)
706 to->si_pid = from->cpt_si_pid;
707 to->si_uid = from->cpt_si_uid;
709 else if (to->si_code == SI_TIMER)
711 to->si_timerid = from->cpt_si_timerid;
712 to->si_overrun = from->cpt_si_overrun;
713 to->si_ptr = (void *) (intptr_t) from->cpt_si_ptr;
717 switch (to->si_signo)
720 to->si_pid = from->cpt_si_pid;
721 to->si_uid = from->cpt_si_uid;
722 to->si_status = from->cpt_si_status;
723 to->si_utime = from->cpt_si_utime;
724 to->si_stime = from->cpt_si_stime;
730 to->si_addr = (void *) (intptr_t) from->cpt_si_addr;
733 to->si_band = from->cpt_si_band;
734 to->si_fd = from->cpt_si_fd;
737 to->si_pid = from->cpt_si_pid;
738 to->si_uid = from->cpt_si_uid;
739 to->si_ptr = (void* ) (intptr_t) from->cpt_si_ptr;
745 #endif /* __x86_64__ */
747 /* Convert a native/host siginfo object, into/from the siginfo in the
748 layout of the inferiors' architecture. Returns true if any
749 conversion was done; false otherwise. If DIRECTION is 1, then copy
750 from INF to NATIVE. If DIRECTION is 0, copy from NATIVE to
754 x86_siginfo_fixup (struct siginfo *native, void *inf, int direction)
757 /* Is the inferior 32-bit? If so, then fixup the siginfo object. */
758 if (register_size (0) == 4)
760 if (sizeof (struct siginfo) != sizeof (compat_siginfo_t))
761 fatal ("unexpected difference in siginfo");
764 compat_siginfo_from_siginfo ((struct compat_siginfo *) inf, native);
766 siginfo_from_compat_siginfo (native, (struct compat_siginfo *) inf);
775 /* Initialize gdbserver for the architecture of the inferior. */
778 x86_arch_setup (void)
781 int pid = pid_of (get_thread_lwp (current_inferior));
782 char *file = linux_child_pid_to_exec_file (pid);
783 int use_64bit = elf_64_file_p (file);
789 /* This can only happen if /proc/<pid>/exe is unreadable,
790 but "that can't happen" if we've gotten this far.
791 Fall through and assume this is a 32-bit program. */
795 init_registers_amd64_linux ();
797 /* Amd64 doesn't have HAVE_LINUX_USRREGS. */
798 the_low_target.num_regs = -1;
799 the_low_target.regmap = NULL;
800 the_low_target.cannot_fetch_register = NULL;
801 the_low_target.cannot_store_register = NULL;
803 /* Amd64 has 16 xmm regs. */
804 num_xmm_registers = 16;
810 /* Ok we have a 32-bit inferior. */
812 init_registers_i386_linux ();
814 the_low_target.num_regs = I386_NUM_REGS;
815 the_low_target.regmap = i386_regmap;
816 the_low_target.cannot_fetch_register = i386_cannot_fetch_register;
817 the_low_target.cannot_store_register = i386_cannot_store_register;
819 /* I386 has 8 xmm regs. */
820 num_xmm_registers = 8;
823 /* This is initialized assuming an amd64 target.
824 x86_arch_setup will correct it for i386 or amd64 targets. */
826 struct linux_target_ops the_low_target =
842 x86_stopped_by_watchpoint,
843 x86_stopped_data_address,
844 /* collect_ptrace_register/supply_ptrace_register are not needed in the
845 native i386 case (no registers smaller than an xfer unit), and are not
846 used in the biarch case (HAVE_LINUX_USRREGS is not defined). */
849 /* need to fix up i386 siginfo if host is amd64 */
851 x86_linux_new_process,
852 x86_linux_new_thread,
853 x86_linux_prepare_to_resume