GDB (API)
/home/stan/gdb/src/gdb/amd64-linux-nat.c
Go to the documentation of this file.
00001 /* Native-dependent code for GNU/Linux x86-64.
00002 
00003    Copyright (C) 2001-2013 Free Software Foundation, Inc.
00004    Contributed by Jiri Smid, SuSE Labs.
00005 
00006    This file is part of GDB.
00007 
00008    This program is free software; you can redistribute it and/or modify
00009    it under the terms of the GNU General Public License as published by
00010    the Free Software Foundation; either version 3 of the License, or
00011    (at your option) any later version.
00012 
00013    This program is distributed in the hope that it will be useful,
00014    but WITHOUT ANY WARRANTY; without even the implied warranty of
00015    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00016    GNU General Public License for more details.
00017 
00018    You should have received a copy of the GNU General Public License
00019    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
00020 
00021 #include "defs.h"
00022 #include "inferior.h"
00023 #include "gdbcore.h"
00024 #include "regcache.h"
00025 #include "regset.h"
00026 #include "linux-nat.h"
00027 #include "amd64-linux-tdep.h"
00028 #include "linux-btrace.h"
00029 #include "btrace.h"
00030 
00031 #include "gdb_assert.h"
00032 #include "gdb_string.h"
00033 #include "elf/common.h"
00034 #include <sys/uio.h>
00035 #include <sys/ptrace.h>
00036 #include <sys/debugreg.h>
00037 #include <sys/syscall.h>
00038 #include <sys/procfs.h>
00039 #include <sys/user.h>
00040 #include <asm/prctl.h>
00041 /* FIXME ezannoni-2003-07-09: we need <sys/reg.h> to be included after
00042    <asm/ptrace.h> because the latter redefines FS and GS for no apparent
00043    reason, and those definitions don't match the ones that libpthread_db
00044    uses, which come from <sys/reg.h>.  */
00045 /* ezannoni-2003-07-09: I think this is fixed.  The extraneous defs have
00046    been removed from ptrace.h in the kernel.  However, better safe than
00047    sorry.  */
00048 #include <asm/ptrace.h>
00049 #include <sys/reg.h>
00050 #include "gdb_proc_service.h"
00051 
00052 /* Prototypes for supply_gregset etc.  */
00053 #include "gregset.h"
00054 
00055 #include "amd64-tdep.h"
00056 #include "i386-linux-tdep.h"
00057 #include "amd64-nat.h"
00058 #include "i386-nat.h"
00059 #include "i386-xstate.h"
00060 
00061 #ifndef PTRACE_GETREGSET
00062 #define PTRACE_GETREGSET        0x4204
00063 #endif
00064 
00065 #ifndef PTRACE_SETREGSET
00066 #define PTRACE_SETREGSET        0x4205
00067 #endif
00068 
00069 /* Per-thread arch-specific data we want to keep.  */
00070 
00071 struct arch_lwp_info
00072 {
00073   /* Non-zero if our copy differs from what's recorded in the thread.  */
00074   int debug_registers_changed;
00075 };
00076 
00077 /* Does the current host support PTRACE_GETREGSET?  */
00078 static int have_ptrace_getregset = -1;
00079 
00080 /* Mapping between the general-purpose registers in GNU/Linux x86-64
00081    `struct user' format and GDB's register cache layout for GNU/Linux
00082    i386.
00083 
00084    Note that most GNU/Linux x86-64 registers are 64-bit, while the
00085    GNU/Linux i386 registers are all 32-bit, but since we're
00086    little-endian we get away with that.  */
00087 
00088 /* From <sys/reg.h> on GNU/Linux i386.  */
00089 static int amd64_linux_gregset32_reg_offset[] =
00090 {
00091   RAX * 8, RCX * 8,             /* %eax, %ecx */
00092   RDX * 8, RBX * 8,             /* %edx, %ebx */
00093   RSP * 8, RBP * 8,             /* %esp, %ebp */
00094   RSI * 8, RDI * 8,             /* %esi, %edi */
00095   RIP * 8, EFLAGS * 8,          /* %eip, %eflags */
00096   CS * 8, SS * 8,               /* %cs, %ss */
00097   DS * 8, ES * 8,               /* %ds, %es */
00098   FS * 8, GS * 8,               /* %fs, %gs */
00099   -1, -1, -1, -1, -1, -1, -1, -1,
00100   -1, -1, -1, -1, -1, -1, -1, -1,
00101   -1, -1, -1, -1, -1, -1, -1, -1, -1,
00102   -1, -1, -1, -1, -1, -1, -1, -1,
00103   ORIG_RAX * 8                  /* "orig_eax" */
00104 };
00105 
00106 
00107 /* Transfering the general-purpose registers between GDB, inferiors
00108    and core files.  */
00109 
00110 /* Fill GDB's register cache with the general-purpose register values
00111    in *GREGSETP.  */
00112 
00113 void
00114 supply_gregset (struct regcache *regcache, const elf_gregset_t *gregsetp)
00115 {
00116   amd64_supply_native_gregset (regcache, gregsetp, -1);
00117 }
00118 
00119 /* Fill register REGNUM (if it is a general-purpose register) in
00120    *GREGSETP with the value in GDB's register cache.  If REGNUM is -1,
00121    do this for all registers.  */
00122 
00123 void
00124 fill_gregset (const struct regcache *regcache,
00125               elf_gregset_t *gregsetp, int regnum)
00126 {
00127   amd64_collect_native_gregset (regcache, gregsetp, regnum);
00128 }
00129 
00130 /* Transfering floating-point registers between GDB, inferiors and cores.  */
00131 
00132 /* Fill GDB's register cache with the floating-point and SSE register
00133    values in *FPREGSETP.  */
00134 
00135 void
00136 supply_fpregset (struct regcache *regcache, const elf_fpregset_t *fpregsetp)
00137 {
00138   amd64_supply_fxsave (regcache, -1, fpregsetp);
00139 }
00140 
00141 /* Fill register REGNUM (if it is a floating-point or SSE register) in
00142    *FPREGSETP with the value in GDB's register cache.  If REGNUM is
00143    -1, do this for all registers.  */
00144 
00145 void
00146 fill_fpregset (const struct regcache *regcache,
00147                elf_fpregset_t *fpregsetp, int regnum)
00148 {
00149   amd64_collect_fxsave (regcache, regnum, fpregsetp);
00150 }
00151 
00152 
00153 /* Transferring arbitrary registers between GDB and inferior.  */
00154 
00155 /* Fetch register REGNUM from the child process.  If REGNUM is -1, do
00156    this for all registers (including the floating point and SSE
00157    registers).  */
00158 
00159 static void
00160 amd64_linux_fetch_inferior_registers (struct target_ops *ops,
00161                                       struct regcache *regcache, int regnum)
00162 {
00163   struct gdbarch *gdbarch = get_regcache_arch (regcache);
00164   int tid;
00165 
00166   /* GNU/Linux LWP ID's are process ID's.  */
00167   tid = ptid_get_lwp (inferior_ptid);
00168   if (tid == 0)
00169     tid = ptid_get_pid (inferior_ptid); /* Not a threaded program.  */
00170 
00171   if (regnum == -1 || amd64_native_gregset_supplies_p (gdbarch, regnum))
00172     {
00173       elf_gregset_t regs;
00174 
00175       if (ptrace (PTRACE_GETREGS, tid, 0, (long) &regs) < 0)
00176         perror_with_name (_("Couldn't get registers"));
00177 
00178       amd64_supply_native_gregset (regcache, &regs, -1);
00179       if (regnum != -1)
00180         return;
00181     }
00182 
00183   if (regnum == -1 || !amd64_native_gregset_supplies_p (gdbarch, regnum))
00184     {
00185       elf_fpregset_t fpregs;
00186 
00187       if (have_ptrace_getregset)
00188         {
00189           char xstateregs[I386_XSTATE_MAX_SIZE];
00190           struct iovec iov;
00191 
00192           iov.iov_base = xstateregs;
00193           iov.iov_len = sizeof (xstateregs);
00194           if (ptrace (PTRACE_GETREGSET, tid,
00195                       (unsigned int) NT_X86_XSTATE, (long) &iov) < 0)
00196             perror_with_name (_("Couldn't get extended state status"));
00197 
00198           amd64_supply_xsave (regcache, -1, xstateregs);
00199         }
00200       else
00201         {
00202           if (ptrace (PTRACE_GETFPREGS, tid, 0, (long) &fpregs) < 0)
00203             perror_with_name (_("Couldn't get floating point status"));
00204 
00205           amd64_supply_fxsave (regcache, -1, &fpregs);
00206         }
00207     }
00208 }
00209 
00210 /* Store register REGNUM back into the child process.  If REGNUM is
00211    -1, do this for all registers (including the floating-point and SSE
00212    registers).  */
00213 
00214 static void
00215 amd64_linux_store_inferior_registers (struct target_ops *ops,
00216                                       struct regcache *regcache, int regnum)
00217 {
00218   struct gdbarch *gdbarch = get_regcache_arch (regcache);
00219   int tid;
00220 
00221   /* GNU/Linux LWP ID's are process ID's.  */
00222   tid = ptid_get_lwp (inferior_ptid);
00223   if (tid == 0)
00224     tid = ptid_get_pid (inferior_ptid); /* Not a threaded program.  */
00225 
00226   if (regnum == -1 || amd64_native_gregset_supplies_p (gdbarch, regnum))
00227     {
00228       elf_gregset_t regs;
00229 
00230       if (ptrace (PTRACE_GETREGS, tid, 0, (long) &regs) < 0)
00231         perror_with_name (_("Couldn't get registers"));
00232 
00233       amd64_collect_native_gregset (regcache, &regs, regnum);
00234 
00235       if (ptrace (PTRACE_SETREGS, tid, 0, (long) &regs) < 0)
00236         perror_with_name (_("Couldn't write registers"));
00237 
00238       if (regnum != -1)
00239         return;
00240     }
00241 
00242   if (regnum == -1 || !amd64_native_gregset_supplies_p (gdbarch, regnum))
00243     {
00244       elf_fpregset_t fpregs;
00245 
00246       if (have_ptrace_getregset)
00247         {
00248           char xstateregs[I386_XSTATE_MAX_SIZE];
00249           struct iovec iov;
00250 
00251           iov.iov_base = xstateregs;
00252           iov.iov_len = sizeof (xstateregs);
00253           if (ptrace (PTRACE_GETREGSET, tid,
00254                       (unsigned int) NT_X86_XSTATE, (long) &iov) < 0)
00255             perror_with_name (_("Couldn't get extended state status"));
00256 
00257           amd64_collect_xsave (regcache, regnum, xstateregs, 0);
00258 
00259           if (ptrace (PTRACE_SETREGSET, tid,
00260                       (unsigned int) NT_X86_XSTATE, (long) &iov) < 0)
00261             perror_with_name (_("Couldn't write extended state status"));
00262         }
00263       else
00264         {
00265           if (ptrace (PTRACE_GETFPREGS, tid, 0, (long) &fpregs) < 0)
00266             perror_with_name (_("Couldn't get floating point status"));
00267 
00268           amd64_collect_fxsave (regcache, regnum, &fpregs);
00269 
00270           if (ptrace (PTRACE_SETFPREGS, tid, 0, (long) &fpregs) < 0)
00271             perror_with_name (_("Couldn't write floating point status"));
00272         }
00273     }
00274 }
00275 
00276 /* Support for debug registers.  */
00277 
00278 static unsigned long
00279 amd64_linux_dr_get (ptid_t ptid, int regnum)
00280 {
00281   int tid;
00282   unsigned long value;
00283 
00284   tid = ptid_get_lwp (ptid);
00285   if (tid == 0)
00286     tid = ptid_get_pid (ptid);
00287 
00288   errno = 0;
00289   value = ptrace (PTRACE_PEEKUSER, tid,
00290                   offsetof (struct user, u_debugreg[regnum]), 0);
00291   if (errno != 0)
00292     perror_with_name (_("Couldn't read debug register"));
00293 
00294   return value;
00295 }
00296 
00297 /* Set debug register REGNUM to VALUE in only the one LWP of PTID.  */
00298 
00299 static void
00300 amd64_linux_dr_set (ptid_t ptid, int regnum, unsigned long value)
00301 {
00302   int tid;
00303 
00304   tid = ptid_get_lwp (ptid);
00305   if (tid == 0)
00306     tid = ptid_get_pid (ptid);
00307 
00308   errno = 0;
00309   ptrace (PTRACE_POKEUSER, tid,
00310           offsetof (struct user, u_debugreg[regnum]), value);
00311   if (errno != 0)
00312     perror_with_name (_("Couldn't write debug register"));
00313 }
00314 
00315 /* Return the inferior's debug register REGNUM.  */
00316 
00317 static CORE_ADDR
00318 amd64_linux_dr_get_addr (int regnum)
00319 {
00320   /* DR6 and DR7 are retrieved with some other way.  */
00321   gdb_assert (DR_FIRSTADDR <= regnum && regnum <= DR_LASTADDR);
00322 
00323   return amd64_linux_dr_get (inferior_ptid, regnum);
00324 }
00325 
00326 /* Return the inferior's DR7 debug control register.  */
00327 
00328 static unsigned long
00329 amd64_linux_dr_get_control (void)
00330 {
00331   return amd64_linux_dr_get (inferior_ptid, DR_CONTROL);
00332 }
00333 
00334 /* Get DR_STATUS from only the one LWP of INFERIOR_PTID.  */
00335 
00336 static unsigned long
00337 amd64_linux_dr_get_status (void)
00338 {
00339   return amd64_linux_dr_get (inferior_ptid, DR_STATUS);
00340 }
00341 
00342 /* Callback for iterate_over_lwps.  Update the debug registers of
00343    LWP.  */
00344 
00345 static int
00346 update_debug_registers_callback (struct lwp_info *lwp, void *arg)
00347 {
00348   if (lwp->arch_private == NULL)
00349     lwp->arch_private = XCNEW (struct arch_lwp_info);
00350 
00351   /* The actual update is done later just before resuming the lwp, we
00352      just mark that the registers need updating.  */
00353   lwp->arch_private->debug_registers_changed = 1;
00354 
00355   /* If the lwp isn't stopped, force it to momentarily pause, so we
00356      can update its debug registers.  */
00357   if (!lwp->stopped)
00358     linux_stop_lwp (lwp);
00359 
00360   /* Continue the iteration.  */
00361   return 0;
00362 }
00363 
00364 /* Set DR_CONTROL to CONTROL in all LWPs of the current inferior.  */
00365 
00366 static void
00367 amd64_linux_dr_set_control (unsigned long control)
00368 {
00369   ptid_t pid_ptid = pid_to_ptid (ptid_get_pid (inferior_ptid));
00370 
00371   iterate_over_lwps (pid_ptid, update_debug_registers_callback, NULL);
00372 }
00373 
00374 /* Set address REGNUM (zero based) to ADDR in all LWPs of the current
00375    inferior.  */
00376 
00377 static void
00378 amd64_linux_dr_set_addr (int regnum, CORE_ADDR addr)
00379 {
00380   ptid_t pid_ptid = pid_to_ptid (ptid_get_pid (inferior_ptid));
00381 
00382   gdb_assert (regnum >= 0 && regnum <= DR_LASTADDR - DR_FIRSTADDR);
00383 
00384   iterate_over_lwps (pid_ptid, update_debug_registers_callback, NULL);
00385 }
00386 
00387 /* Called when resuming a thread.
00388    If the debug regs have changed, update the thread's copies.  */
00389 
00390 static void
00391 amd64_linux_prepare_to_resume (struct lwp_info *lwp)
00392 {
00393   int clear_status = 0;
00394 
00395   /* NULL means this is the main thread still going through the shell,
00396      or, no watchpoint has been set yet.  In that case, there's
00397      nothing to do.  */
00398   if (lwp->arch_private == NULL)
00399     return;
00400 
00401   if (lwp->arch_private->debug_registers_changed)
00402     {
00403       struct i386_debug_reg_state *state
00404         = i386_debug_reg_state (ptid_get_pid (lwp->ptid));
00405       int i;
00406 
00407       /* On Linux kernel before 2.6.33 commit
00408          72f674d203cd230426437cdcf7dd6f681dad8b0d
00409          if you enable a breakpoint by the DR_CONTROL bits you need to have
00410          already written the corresponding DR_FIRSTADDR...DR_LASTADDR registers.
00411 
00412          Ensure DR_CONTROL gets written as the very last register here.  */
00413 
00414       for (i = DR_FIRSTADDR; i <= DR_LASTADDR; i++)
00415         if (state->dr_ref_count[i] > 0)
00416           {
00417             amd64_linux_dr_set (lwp->ptid, i, state->dr_mirror[i]);
00418 
00419             /* If we're setting a watchpoint, any change the inferior
00420                had done itself to the debug registers needs to be
00421                discarded, otherwise, i386_stopped_data_address can get
00422                confused.  */
00423             clear_status = 1;
00424           }
00425 
00426       amd64_linux_dr_set (lwp->ptid, DR_CONTROL, state->dr_control_mirror);
00427 
00428       lwp->arch_private->debug_registers_changed = 0;
00429     }
00430 
00431   if (clear_status || lwp->stopped_by_watchpoint)
00432     amd64_linux_dr_set (lwp->ptid, DR_STATUS, 0);
00433 }
00434 
00435 static void
00436 amd64_linux_new_thread (struct lwp_info *lp)
00437 {
00438   struct arch_lwp_info *info = XCNEW (struct arch_lwp_info);
00439 
00440   info->debug_registers_changed = 1;
00441 
00442   lp->arch_private = info;
00443 }
00444 
00445 /* linux_nat_new_fork hook.   */
00446 
00447 static void
00448 amd64_linux_new_fork (struct lwp_info *parent, pid_t child_pid)
00449 {
00450   pid_t parent_pid;
00451   struct i386_debug_reg_state *parent_state;
00452   struct i386_debug_reg_state *child_state;
00453 
00454   /* NULL means no watchpoint has ever been set in the parent.  In
00455      that case, there's nothing to do.  */
00456   if (parent->arch_private == NULL)
00457     return;
00458 
00459   /* Linux kernel before 2.6.33 commit
00460      72f674d203cd230426437cdcf7dd6f681dad8b0d
00461      will inherit hardware debug registers from parent
00462      on fork/vfork/clone.  Newer Linux kernels create such tasks with
00463      zeroed debug registers.
00464 
00465      GDB core assumes the child inherits the watchpoints/hw
00466      breakpoints of the parent, and will remove them all from the
00467      forked off process.  Copy the debug registers mirrors into the
00468      new process so that all breakpoints and watchpoints can be
00469      removed together.  The debug registers mirror will become zeroed
00470      in the end before detaching the forked off process, thus making
00471      this compatible with older Linux kernels too.  */
00472 
00473   parent_pid = ptid_get_pid (parent->ptid);
00474   parent_state = i386_debug_reg_state (parent_pid);
00475   child_state = i386_debug_reg_state (child_pid);
00476   *child_state = *parent_state;
00477 }
00478 
00479 
00480 
00481 /* This function is called by libthread_db as part of its handling of
00482    a request for a thread's local storage address.  */
00483 
00484 ps_err_e
00485 ps_get_thread_area (const struct ps_prochandle *ph,
00486                     lwpid_t lwpid, int idx, void **base)
00487 {
00488   if (gdbarch_bfd_arch_info (target_gdbarch ())->bits_per_word == 32)
00489     {
00490       /* The full structure is found in <asm-i386/ldt.h>.  The second
00491          integer is the LDT's base_address and that is used to locate
00492          the thread's local storage.  See i386-linux-nat.c more
00493          info.  */
00494       unsigned int desc[4];
00495 
00496       /* This code assumes that "int" is 32 bits and that
00497          GET_THREAD_AREA returns no more than 4 int values.  */
00498       gdb_assert (sizeof (int) == 4);   
00499 #ifndef PTRACE_GET_THREAD_AREA
00500 #define PTRACE_GET_THREAD_AREA 25
00501 #endif
00502       if  (ptrace (PTRACE_GET_THREAD_AREA, 
00503                    lwpid, (void *) (long) idx, (unsigned long) &desc) < 0)
00504         return PS_ERR;
00505       
00506       /* Extend the value to 64 bits.  Here it's assumed that a "long"
00507          and a "void *" are the same.  */
00508       (*base) = (void *) (long) desc[1];
00509       return PS_OK;
00510     }
00511   else
00512     {
00513       /* This definition comes from prctl.h, but some kernels may not
00514          have it.  */
00515 #ifndef PTRACE_ARCH_PRCTL
00516 #define PTRACE_ARCH_PRCTL      30
00517 #endif
00518       /* FIXME: ezannoni-2003-07-09 see comment above about include
00519          file order.  We could be getting bogus values for these two.  */
00520       gdb_assert (FS < ELF_NGREG);
00521       gdb_assert (GS < ELF_NGREG);
00522       switch (idx)
00523         {
00524         case FS:
00525 #ifdef HAVE_STRUCT_USER_REGS_STRUCT_FS_BASE
00526             {
00527               /* PTRACE_ARCH_PRCTL is obsolete since 2.6.25, where the
00528                  fs_base and gs_base fields of user_regs_struct can be
00529                  used directly.  */
00530               unsigned long fs;
00531               errno = 0;
00532               fs = ptrace (PTRACE_PEEKUSER, lwpid,
00533                            offsetof (struct user_regs_struct, fs_base), 0);
00534               if (errno == 0)
00535                 {
00536                   *base = (void *) fs;
00537                   return PS_OK;
00538                 }
00539             }
00540 #endif
00541           if (ptrace (PTRACE_ARCH_PRCTL, lwpid, base, ARCH_GET_FS) == 0)
00542             return PS_OK;
00543           break;
00544         case GS:
00545 #ifdef HAVE_STRUCT_USER_REGS_STRUCT_GS_BASE
00546             {
00547               unsigned long gs;
00548               errno = 0;
00549               gs = ptrace (PTRACE_PEEKUSER, lwpid,
00550                            offsetof (struct user_regs_struct, gs_base), 0);
00551               if (errno == 0)
00552                 {
00553                   *base = (void *) gs;
00554                   return PS_OK;
00555                 }
00556             }
00557 #endif
00558           if (ptrace (PTRACE_ARCH_PRCTL, lwpid, base, ARCH_GET_GS) == 0)
00559             return PS_OK;
00560           break;
00561         default:                   /* Should not happen.  */
00562           return PS_BADADDR;
00563         }
00564     }
00565   return PS_ERR;               /* ptrace failed.  */
00566 }
00567 
00568 
00569 static void (*super_post_startup_inferior) (ptid_t ptid);
00570 
00571 static void
00572 amd64_linux_child_post_startup_inferior (ptid_t ptid)
00573 {
00574   i386_cleanup_dregs ();
00575   super_post_startup_inferior (ptid);
00576 }
00577 
00578 
00579 /* When GDB is built as a 64-bit application on linux, the
00580    PTRACE_GETSIGINFO data is always presented in 64-bit layout.  Since
00581    debugging a 32-bit inferior with a 64-bit GDB should look the same
00582    as debugging it with a 32-bit GDB, we do the 32-bit <-> 64-bit
00583    conversion in-place ourselves.  */
00584 
00585 /* These types below (compat_*) define a siginfo type that is layout
00586    compatible with the siginfo type exported by the 32-bit userspace
00587    support.  */
00588 
00589 typedef int compat_int_t;
00590 typedef unsigned int compat_uptr_t;
00591 
00592 typedef int compat_time_t;
00593 typedef int compat_timer_t;
00594 typedef int compat_clock_t;
00595 
00596 struct compat_timeval
00597 {
00598   compat_time_t tv_sec;
00599   int tv_usec;
00600 };
00601 
00602 typedef union compat_sigval
00603 {
00604   compat_int_t sival_int;
00605   compat_uptr_t sival_ptr;
00606 } compat_sigval_t;
00607 
00608 typedef struct compat_siginfo
00609 {
00610   int si_signo;
00611   int si_errno;
00612   int si_code;
00613 
00614   union
00615   {
00616     int _pad[((128 / sizeof (int)) - 3)];
00617 
00618     /* kill() */
00619     struct
00620     {
00621       unsigned int _pid;
00622       unsigned int _uid;
00623     } _kill;
00624 
00625     /* POSIX.1b timers */
00626     struct
00627     {
00628       compat_timer_t _tid;
00629       int _overrun;
00630       compat_sigval_t _sigval;
00631     } _timer;
00632 
00633     /* POSIX.1b signals */
00634     struct
00635     {
00636       unsigned int _pid;
00637       unsigned int _uid;
00638       compat_sigval_t _sigval;
00639     } _rt;
00640 
00641     /* SIGCHLD */
00642     struct
00643     {
00644       unsigned int _pid;
00645       unsigned int _uid;
00646       int _status;
00647       compat_clock_t _utime;
00648       compat_clock_t _stime;
00649     } _sigchld;
00650 
00651     /* SIGILL, SIGFPE, SIGSEGV, SIGBUS */
00652     struct
00653     {
00654       unsigned int _addr;
00655     } _sigfault;
00656 
00657     /* SIGPOLL */
00658     struct
00659     {
00660       int _band;
00661       int _fd;
00662     } _sigpoll;
00663   } _sifields;
00664 } compat_siginfo_t;
00665 
00666 /* For x32, clock_t in _sigchld is 64bit aligned at 4 bytes.  */
00667 typedef struct compat_x32_clock
00668 {
00669   int lower;
00670   int upper;
00671 } compat_x32_clock_t;
00672 
00673 typedef struct compat_x32_siginfo
00674 {
00675   int si_signo;
00676   int si_errno;
00677   int si_code;
00678 
00679   union
00680   {
00681     int _pad[((128 / sizeof (int)) - 3)];
00682 
00683     /* kill() */
00684     struct
00685     {
00686       unsigned int _pid;
00687       unsigned int _uid;
00688     } _kill;
00689 
00690     /* POSIX.1b timers */
00691     struct
00692     {
00693       compat_timer_t _tid;
00694       int _overrun;
00695       compat_sigval_t _sigval;
00696     } _timer;
00697 
00698     /* POSIX.1b signals */
00699     struct
00700     {
00701       unsigned int _pid;
00702       unsigned int _uid;
00703       compat_sigval_t _sigval;
00704     } _rt;
00705 
00706     /* SIGCHLD */
00707     struct
00708     {
00709       unsigned int _pid;
00710       unsigned int _uid;
00711       int _status;
00712       compat_x32_clock_t _utime;
00713       compat_x32_clock_t _stime;
00714     } _sigchld;
00715 
00716     /* SIGILL, SIGFPE, SIGSEGV, SIGBUS */
00717     struct
00718     {
00719       unsigned int _addr;
00720     } _sigfault;
00721 
00722     /* SIGPOLL */
00723     struct
00724     {
00725       int _band;
00726       int _fd;
00727     } _sigpoll;
00728   } _sifields;
00729 } compat_x32_siginfo_t;
00730 
00731 #define cpt_si_pid _sifields._kill._pid
00732 #define cpt_si_uid _sifields._kill._uid
00733 #define cpt_si_timerid _sifields._timer._tid
00734 #define cpt_si_overrun _sifields._timer._overrun
00735 #define cpt_si_status _sifields._sigchld._status
00736 #define cpt_si_utime _sifields._sigchld._utime
00737 #define cpt_si_stime _sifields._sigchld._stime
00738 #define cpt_si_ptr _sifields._rt._sigval.sival_ptr
00739 #define cpt_si_addr _sifields._sigfault._addr
00740 #define cpt_si_band _sifields._sigpoll._band
00741 #define cpt_si_fd _sifields._sigpoll._fd
00742 
00743 /* glibc at least up to 2.3.2 doesn't have si_timerid, si_overrun.
00744    In their place is si_timer1,si_timer2.  */
00745 #ifndef si_timerid
00746 #define si_timerid si_timer1
00747 #endif
00748 #ifndef si_overrun
00749 #define si_overrun si_timer2
00750 #endif
00751 
00752 static void
00753 compat_siginfo_from_siginfo (compat_siginfo_t *to, siginfo_t *from)
00754 {
00755   memset (to, 0, sizeof (*to));
00756 
00757   to->si_signo = from->si_signo;
00758   to->si_errno = from->si_errno;
00759   to->si_code = from->si_code;
00760 
00761   if (to->si_code == SI_TIMER)
00762     {
00763       to->cpt_si_timerid = from->si_timerid;
00764       to->cpt_si_overrun = from->si_overrun;
00765       to->cpt_si_ptr = (intptr_t) from->si_ptr;
00766     }
00767   else if (to->si_code == SI_USER)
00768     {
00769       to->cpt_si_pid = from->si_pid;
00770       to->cpt_si_uid = from->si_uid;
00771     }
00772   else if (to->si_code < 0)
00773     {
00774       to->cpt_si_pid = from->si_pid;
00775       to->cpt_si_uid = from->si_uid;
00776       to->cpt_si_ptr = (intptr_t) from->si_ptr;
00777     }
00778   else
00779     {
00780       switch (to->si_signo)
00781         {
00782         case SIGCHLD:
00783           to->cpt_si_pid = from->si_pid;
00784           to->cpt_si_uid = from->si_uid;
00785           to->cpt_si_status = from->si_status;
00786           to->cpt_si_utime = from->si_utime;
00787           to->cpt_si_stime = from->si_stime;
00788           break;
00789         case SIGILL:
00790         case SIGFPE:
00791         case SIGSEGV:
00792         case SIGBUS:
00793           to->cpt_si_addr = (intptr_t) from->si_addr;
00794           break;
00795         case SIGPOLL:
00796           to->cpt_si_band = from->si_band;
00797           to->cpt_si_fd = from->si_fd;
00798           break;
00799         default:
00800           to->cpt_si_pid = from->si_pid;
00801           to->cpt_si_uid = from->si_uid;
00802           to->cpt_si_ptr = (intptr_t) from->si_ptr;
00803           break;
00804         }
00805     }
00806 }
00807 
00808 static void
00809 siginfo_from_compat_siginfo (siginfo_t *to, compat_siginfo_t *from)
00810 {
00811   memset (to, 0, sizeof (*to));
00812 
00813   to->si_signo = from->si_signo;
00814   to->si_errno = from->si_errno;
00815   to->si_code = from->si_code;
00816 
00817   if (to->si_code == SI_TIMER)
00818     {
00819       to->si_timerid = from->cpt_si_timerid;
00820       to->si_overrun = from->cpt_si_overrun;
00821       to->si_ptr = (void *) (intptr_t) from->cpt_si_ptr;
00822     }
00823   else if (to->si_code == SI_USER)
00824     {
00825       to->si_pid = from->cpt_si_pid;
00826       to->si_uid = from->cpt_si_uid;
00827     }
00828   if (to->si_code < 0)
00829     {
00830       to->si_pid = from->cpt_si_pid;
00831       to->si_uid = from->cpt_si_uid;
00832       to->si_ptr = (void *) (intptr_t) from->cpt_si_ptr;
00833     }
00834   else
00835     {
00836       switch (to->si_signo)
00837         {
00838         case SIGCHLD:
00839           to->si_pid = from->cpt_si_pid;
00840           to->si_uid = from->cpt_si_uid;
00841           to->si_status = from->cpt_si_status;
00842           to->si_utime = from->cpt_si_utime;
00843           to->si_stime = from->cpt_si_stime;
00844           break;
00845         case SIGILL:
00846         case SIGFPE:
00847         case SIGSEGV:
00848         case SIGBUS:
00849           to->si_addr = (void *) (intptr_t) from->cpt_si_addr;
00850           break;
00851         case SIGPOLL:
00852           to->si_band = from->cpt_si_band;
00853           to->si_fd = from->cpt_si_fd;
00854           break;
00855         default:
00856           to->si_pid = from->cpt_si_pid;
00857           to->si_uid = from->cpt_si_uid;
00858           to->si_ptr = (void* ) (intptr_t) from->cpt_si_ptr;
00859           break;
00860         }
00861     }
00862 }
00863 
00864 static void
00865 compat_x32_siginfo_from_siginfo (compat_x32_siginfo_t *to,
00866                                  siginfo_t *from)
00867 {
00868   memset (to, 0, sizeof (*to));
00869 
00870   to->si_signo = from->si_signo;
00871   to->si_errno = from->si_errno;
00872   to->si_code = from->si_code;
00873 
00874   if (to->si_code == SI_TIMER)
00875     {
00876       to->cpt_si_timerid = from->si_timerid;
00877       to->cpt_si_overrun = from->si_overrun;
00878       to->cpt_si_ptr = (intptr_t) from->si_ptr;
00879     }
00880   else if (to->si_code == SI_USER)
00881     {
00882       to->cpt_si_pid = from->si_pid;
00883       to->cpt_si_uid = from->si_uid;
00884     }
00885   else if (to->si_code < 0)
00886     {
00887       to->cpt_si_pid = from->si_pid;
00888       to->cpt_si_uid = from->si_uid;
00889       to->cpt_si_ptr = (intptr_t) from->si_ptr;
00890     }
00891   else
00892     {
00893       switch (to->si_signo)
00894         {
00895         case SIGCHLD:
00896           to->cpt_si_pid = from->si_pid;
00897           to->cpt_si_uid = from->si_uid;
00898           to->cpt_si_status = from->si_status;
00899           memcpy (&to->cpt_si_utime, &from->si_utime,
00900                   sizeof (to->cpt_si_utime));
00901           memcpy (&to->cpt_si_stime, &from->si_stime,
00902                   sizeof (to->cpt_si_stime));
00903           break;
00904         case SIGILL:
00905         case SIGFPE:
00906         case SIGSEGV:
00907         case SIGBUS:
00908           to->cpt_si_addr = (intptr_t) from->si_addr;
00909           break;
00910         case SIGPOLL:
00911           to->cpt_si_band = from->si_band;
00912           to->cpt_si_fd = from->si_fd;
00913           break;
00914         default:
00915           to->cpt_si_pid = from->si_pid;
00916           to->cpt_si_uid = from->si_uid;
00917           to->cpt_si_ptr = (intptr_t) from->si_ptr;
00918           break;
00919         }
00920     }
00921 }
00922 
00923 static void
00924 siginfo_from_compat_x32_siginfo (siginfo_t *to,
00925                                  compat_x32_siginfo_t *from)
00926 {
00927   memset (to, 0, sizeof (*to));
00928 
00929   to->si_signo = from->si_signo;
00930   to->si_errno = from->si_errno;
00931   to->si_code = from->si_code;
00932 
00933   if (to->si_code == SI_TIMER)
00934     {
00935       to->si_timerid = from->cpt_si_timerid;
00936       to->si_overrun = from->cpt_si_overrun;
00937       to->si_ptr = (void *) (intptr_t) from->cpt_si_ptr;
00938     }
00939   else if (to->si_code == SI_USER)
00940     {
00941       to->si_pid = from->cpt_si_pid;
00942       to->si_uid = from->cpt_si_uid;
00943     }
00944   if (to->si_code < 0)
00945     {
00946       to->si_pid = from->cpt_si_pid;
00947       to->si_uid = from->cpt_si_uid;
00948       to->si_ptr = (void *) (intptr_t) from->cpt_si_ptr;
00949     }
00950   else
00951     {
00952       switch (to->si_signo)
00953         {
00954         case SIGCHLD:
00955           to->si_pid = from->cpt_si_pid;
00956           to->si_uid = from->cpt_si_uid;
00957           to->si_status = from->cpt_si_status;
00958           memcpy (&to->si_utime, &from->cpt_si_utime,
00959                   sizeof (to->si_utime));
00960           memcpy (&to->si_stime, &from->cpt_si_stime,
00961                   sizeof (to->si_stime));
00962           break;
00963         case SIGILL:
00964         case SIGFPE:
00965         case SIGSEGV:
00966         case SIGBUS:
00967           to->si_addr = (void *) (intptr_t) from->cpt_si_addr;
00968           break;
00969         case SIGPOLL:
00970           to->si_band = from->cpt_si_band;
00971           to->si_fd = from->cpt_si_fd;
00972           break;
00973         default:
00974           to->si_pid = from->cpt_si_pid;
00975           to->si_uid = from->cpt_si_uid;
00976           to->si_ptr = (void* ) (intptr_t) from->cpt_si_ptr;
00977           break;
00978         }
00979     }
00980 }
00981 
00982 /* Convert a native/host siginfo object, into/from the siginfo in the
00983    layout of the inferiors' architecture.  Returns true if any
00984    conversion was done; false otherwise.  If DIRECTION is 1, then copy
00985    from INF to NATIVE.  If DIRECTION is 0, copy from NATIVE to
00986    INF.  */
00987 
00988 static int
00989 amd64_linux_siginfo_fixup (siginfo_t *native, gdb_byte *inf, int direction)
00990 {
00991   struct gdbarch *gdbarch = get_frame_arch (get_current_frame ());
00992 
00993   /* Is the inferior 32-bit?  If so, then do fixup the siginfo
00994      object.  */
00995   if (gdbarch_bfd_arch_info (gdbarch)->bits_per_word == 32)
00996     {
00997       gdb_assert (sizeof (siginfo_t) == sizeof (compat_siginfo_t));
00998 
00999       if (direction == 0)
01000         compat_siginfo_from_siginfo ((struct compat_siginfo *) inf, native);
01001       else
01002         siginfo_from_compat_siginfo (native, (struct compat_siginfo *) inf);
01003 
01004       return 1;
01005     }
01006   /* No fixup for native x32 GDB.  */
01007   else if (gdbarch_addr_bit (gdbarch) == 32 && sizeof (void *) == 8)
01008     {
01009       gdb_assert (sizeof (siginfo_t) == sizeof (compat_x32_siginfo_t));
01010 
01011       if (direction == 0)
01012         compat_x32_siginfo_from_siginfo ((struct compat_x32_siginfo *) inf,
01013                                          native);
01014       else
01015         siginfo_from_compat_x32_siginfo (native,
01016                                          (struct compat_x32_siginfo *) inf);
01017 
01018       return 1;
01019     }
01020   else
01021     return 0;
01022 }
01023 
01024 /* Get Linux/x86 target description from running target.
01025 
01026    Value of CS segment register:
01027      1. 64bit process: 0x33.
01028      2. 32bit process: 0x23.
01029 
01030    Value of DS segment register:
01031      1. LP64 process: 0x0.
01032      2. X32 process: 0x2b.
01033  */
01034 
01035 #define AMD64_LINUX_USER64_CS   0x33
01036 #define AMD64_LINUX_X32_DS      0x2b
01037 
01038 static const struct target_desc *
01039 amd64_linux_read_description (struct target_ops *ops)
01040 {
01041   unsigned long cs;
01042   unsigned long ds;
01043   int tid;
01044   int is_64bit;
01045   int is_x32;
01046   static uint64_t xcr0;
01047 
01048   /* GNU/Linux LWP ID's are process ID's.  */
01049   tid = ptid_get_lwp (inferior_ptid);
01050   if (tid == 0)
01051     tid = ptid_get_pid (inferior_ptid); /* Not a threaded program.  */
01052 
01053   /* Get CS register.  */
01054   errno = 0;
01055   cs = ptrace (PTRACE_PEEKUSER, tid,
01056                offsetof (struct user_regs_struct, cs), 0);
01057   if (errno != 0)
01058     perror_with_name (_("Couldn't get CS register"));
01059 
01060   is_64bit = cs == AMD64_LINUX_USER64_CS;
01061 
01062   /* Get DS register.  */
01063   errno = 0;
01064   ds = ptrace (PTRACE_PEEKUSER, tid,
01065                offsetof (struct user_regs_struct, ds), 0);
01066   if (errno != 0)
01067     perror_with_name (_("Couldn't get DS register"));
01068 
01069   is_x32 = ds == AMD64_LINUX_X32_DS;
01070 
01071   if (sizeof (void *) == 4 && is_64bit && !is_x32)
01072     error (_("Can't debug 64-bit process with 32-bit GDB"));
01073 
01074   if (have_ptrace_getregset == -1)
01075     {
01076       uint64_t xstateregs[(I386_XSTATE_SSE_SIZE / sizeof (uint64_t))];
01077       struct iovec iov;
01078 
01079       iov.iov_base = xstateregs;
01080       iov.iov_len = sizeof (xstateregs);
01081 
01082       /* Check if PTRACE_GETREGSET works.  */
01083       if (ptrace (PTRACE_GETREGSET, tid,
01084                   (unsigned int) NT_X86_XSTATE, (long) &iov) < 0)
01085         have_ptrace_getregset = 0;
01086       else
01087         {
01088           have_ptrace_getregset = 1;
01089 
01090           /* Get XCR0 from XSAVE extended state.  */
01091           xcr0 = xstateregs[(I386_LINUX_XSAVE_XCR0_OFFSET
01092                              / sizeof (uint64_t))];
01093         }
01094     }
01095 
01096   /* Check the native XCR0 only if PTRACE_GETREGSET is available.  */
01097   if (have_ptrace_getregset
01098       && (xcr0 & I386_XSTATE_AVX_MASK) == I386_XSTATE_AVX_MASK)
01099     {
01100       if (is_64bit)
01101         {
01102           if (is_x32)
01103             return tdesc_x32_avx_linux;
01104           else
01105             return tdesc_amd64_avx_linux;
01106         }
01107       else
01108         return tdesc_i386_avx_linux;
01109     }
01110   else
01111     {
01112       if (is_64bit)
01113         {
01114           if (is_x32)
01115             return tdesc_x32_linux;
01116           else
01117             return tdesc_amd64_linux;
01118         }
01119       else
01120         return tdesc_i386_linux;
01121     }
01122 }
01123 
01124 /* Enable branch tracing.  */
01125 
01126 static struct btrace_target_info *
01127 amd64_linux_enable_btrace (ptid_t ptid)
01128 {
01129   struct btrace_target_info *tinfo;
01130   struct gdbarch *gdbarch;
01131 
01132   errno = 0;
01133   tinfo = linux_enable_btrace (ptid);
01134 
01135   if (tinfo == NULL)
01136     error (_("Could not enable branch tracing for %s: %s."),
01137            target_pid_to_str (ptid), safe_strerror (errno));
01138 
01139   /* Fill in the size of a pointer in bits.  */
01140   gdbarch = target_thread_architecture (ptid);
01141   tinfo->ptr_bits = gdbarch_ptr_bit (gdbarch);
01142 
01143   return tinfo;
01144 }
01145 
01146 /* Disable branch tracing.  */
01147 
01148 static void
01149 amd64_linux_disable_btrace (struct btrace_target_info *tinfo)
01150 {
01151   int errcode = linux_disable_btrace (tinfo);
01152 
01153   if (errcode != 0)
01154     error (_("Could not disable branch tracing: %s."), safe_strerror (errcode));
01155 }
01156 
01157 /* Teardown branch tracing.  */
01158 
01159 static void
01160 amd64_linux_teardown_btrace (struct btrace_target_info *tinfo)
01161 {
01162   /* Ignore errors.  */
01163   linux_disable_btrace (tinfo);
01164 }
01165 
01166 /* Provide a prototype to silence -Wmissing-prototypes.  */
01167 void _initialize_amd64_linux_nat (void);
01168 
01169 void
01170 _initialize_amd64_linux_nat (void)
01171 {
01172   struct target_ops *t;
01173 
01174   amd64_native_gregset32_reg_offset = amd64_linux_gregset32_reg_offset;
01175   amd64_native_gregset32_num_regs = I386_LINUX_NUM_REGS;
01176   amd64_native_gregset64_reg_offset = amd64_linux_gregset_reg_offset;
01177   amd64_native_gregset64_num_regs = AMD64_LINUX_NUM_REGS;
01178 
01179   gdb_assert (ARRAY_SIZE (amd64_linux_gregset32_reg_offset)
01180               == amd64_native_gregset32_num_regs);
01181 
01182   /* Fill in the generic GNU/Linux methods.  */
01183   t = linux_target ();
01184 
01185   i386_use_watchpoints (t);
01186 
01187   i386_dr_low.set_control = amd64_linux_dr_set_control;
01188   i386_dr_low.set_addr = amd64_linux_dr_set_addr;
01189   i386_dr_low.get_addr = amd64_linux_dr_get_addr;
01190   i386_dr_low.get_status = amd64_linux_dr_get_status;
01191   i386_dr_low.get_control = amd64_linux_dr_get_control;
01192   i386_set_debug_register_length (8);
01193 
01194   /* Override the GNU/Linux inferior startup hook.  */
01195   super_post_startup_inferior = t->to_post_startup_inferior;
01196   t->to_post_startup_inferior = amd64_linux_child_post_startup_inferior;
01197 
01198   /* Add our register access methods.  */
01199   t->to_fetch_registers = amd64_linux_fetch_inferior_registers;
01200   t->to_store_registers = amd64_linux_store_inferior_registers;
01201 
01202   t->to_read_description = amd64_linux_read_description;
01203 
01204   /* Add btrace methods.  */
01205   t->to_supports_btrace = linux_supports_btrace;
01206   t->to_enable_btrace = amd64_linux_enable_btrace;
01207   t->to_disable_btrace = amd64_linux_disable_btrace;
01208   t->to_teardown_btrace = amd64_linux_teardown_btrace;
01209   t->to_read_btrace = linux_read_btrace;
01210 
01211   /* Register the target.  */
01212   linux_nat_add_target (t);
01213   linux_nat_set_new_thread (t, amd64_linux_new_thread);
01214   linux_nat_set_new_fork (t, amd64_linux_new_fork);
01215   linux_nat_set_forget_process (t, i386_forget_process);
01216   linux_nat_set_siginfo_fixup (t, amd64_linux_siginfo_fixup);
01217   linux_nat_set_prepare_to_resume (t, amd64_linux_prepare_to_resume);
01218 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Defines