GDB (API)
/home/stan/gdb/src/gdb/i386-linux-nat.c
Go to the documentation of this file.
00001 /* Native-dependent code for GNU/Linux i386.
00002 
00003    Copyright (C) 1999-2013 Free Software Foundation, Inc.
00004 
00005    This file is part of GDB.
00006 
00007    This program is free software; you can redistribute it and/or modify
00008    it under the terms of the GNU General Public License as published by
00009    the Free Software Foundation; either version 3 of the License, or
00010    (at your option) any later version.
00011 
00012    This program is distributed in the hope that it will be useful,
00013    but WITHOUT ANY WARRANTY; without even the implied warranty of
00014    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015    GNU General Public License for more details.
00016 
00017    You should have received a copy of the GNU General Public License
00018    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
00019 
00020 #include "defs.h"
00021 #include "i386-nat.h"
00022 #include "inferior.h"
00023 #include "gdbcore.h"
00024 #include "regcache.h"
00025 #include "regset.h"
00026 #include "target.h"
00027 #include "linux-nat.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/user.h>
00037 #include <sys/procfs.h>
00038 
00039 #ifdef HAVE_SYS_REG_H
00040 #include <sys/reg.h>
00041 #endif
00042 
00043 #ifndef ORIG_EAX
00044 #define ORIG_EAX -1
00045 #endif
00046 
00047 #ifdef HAVE_SYS_DEBUGREG_H
00048 #include <sys/debugreg.h>
00049 #endif
00050 
00051 /* Prototypes for supply_gregset etc.  */
00052 #include "gregset.h"
00053 
00054 #include "i387-tdep.h"
00055 #include "i386-tdep.h"
00056 #include "i386-linux-tdep.h"
00057 
00058 /* Defines ps_err_e, struct ps_prochandle.  */
00059 #include "gdb_proc_service.h"
00060 
00061 #include "i386-xstate.h"
00062 
00063 #ifndef PTRACE_GETREGSET
00064 #define PTRACE_GETREGSET        0x4204
00065 #endif
00066 
00067 #ifndef PTRACE_SETREGSET
00068 #define PTRACE_SETREGSET        0x4205
00069 #endif
00070 
00071 /* Per-thread arch-specific data we want to keep.  */
00072 
00073 struct arch_lwp_info
00074 {
00075   /* Non-zero if our copy differs from what's recorded in the thread.  */
00076   int debug_registers_changed;
00077 };
00078 
00079 /* Does the current host support PTRACE_GETREGSET?  */
00080 static int have_ptrace_getregset = -1;
00081 
00082 
00083 /* The register sets used in GNU/Linux ELF core-dumps are identical to
00084    the register sets in `struct user' that is used for a.out
00085    core-dumps, and is also used by `ptrace'.  The corresponding types
00086    are `elf_gregset_t' for the general-purpose registers (with
00087    `elf_greg_t' the type of a single GP register) and `elf_fpregset_t'
00088    for the floating-point registers.
00089 
00090    Those types used to be available under the names `gregset_t' and
00091    `fpregset_t' too, and this file used those names in the past.  But
00092    those names are now used for the register sets used in the
00093    `mcontext_t' type, and have a different size and layout.  */
00094 
00095 /* Which ptrace request retrieves which registers?
00096    These apply to the corresponding SET requests as well.  */
00097 
00098 #define GETREGS_SUPPLIES(regno) \
00099   ((0 <= (regno) && (regno) <= 15) || (regno) == I386_LINUX_ORIG_EAX_REGNUM)
00100 
00101 #define GETFPXREGS_SUPPLIES(regno) \
00102   (I386_ST0_REGNUM <= (regno) && (regno) < I386_SSE_NUM_REGS)
00103 
00104 #define GETXSTATEREGS_SUPPLIES(regno) \
00105   (I386_ST0_REGNUM <= (regno) && (regno) < I386_AVX_NUM_REGS)
00106 
00107 /* Does the current host support the GETREGS request?  */
00108 int have_ptrace_getregs =
00109 #ifdef HAVE_PTRACE_GETREGS
00110   1
00111 #else
00112   0
00113 #endif
00114 ;
00115 
00116 /* Does the current host support the GETFPXREGS request?  The header
00117    file may or may not define it, and even if it is defined, the
00118    kernel will return EIO if it's running on a pre-SSE processor.
00119 
00120    My instinct is to attach this to some architecture- or
00121    target-specific data structure, but really, a particular GDB
00122    process can only run on top of one kernel at a time.  So it's okay
00123    for this to be a simple variable.  */
00124 int have_ptrace_getfpxregs =
00125 #ifdef HAVE_PTRACE_GETFPXREGS
00126   -1
00127 #else
00128   0
00129 #endif
00130 ;
00131 
00132 
00133 /* Accessing registers through the U area, one at a time.  */
00134 
00135 /* Fetch one register.  */
00136 
00137 static void
00138 fetch_register (struct regcache *regcache, int regno)
00139 {
00140   int tid;
00141   int val;
00142 
00143   gdb_assert (!have_ptrace_getregs);
00144   if (i386_linux_gregset_reg_offset[regno] == -1)
00145     {
00146       regcache_raw_supply (regcache, regno, NULL);
00147       return;
00148     }
00149 
00150   /* GNU/Linux LWP ID's are process ID's.  */
00151   tid = ptid_get_lwp (inferior_ptid);
00152   if (tid == 0)
00153     tid = ptid_get_pid (inferior_ptid); /* Not a threaded program.  */
00154 
00155   errno = 0;
00156   val = ptrace (PTRACE_PEEKUSER, tid,
00157                 i386_linux_gregset_reg_offset[regno], 0);
00158   if (errno != 0)
00159     error (_("Couldn't read register %s (#%d): %s."), 
00160            gdbarch_register_name (get_regcache_arch (regcache), regno),
00161            regno, safe_strerror (errno));
00162 
00163   regcache_raw_supply (regcache, regno, &val);
00164 }
00165 
00166 /* Store one register.  */
00167 
00168 static void
00169 store_register (const struct regcache *regcache, int regno)
00170 {
00171   int tid;
00172   int val;
00173 
00174   gdb_assert (!have_ptrace_getregs);
00175   if (i386_linux_gregset_reg_offset[regno] == -1)
00176     return;
00177 
00178   /* GNU/Linux LWP ID's are process ID's.  */
00179   tid = ptid_get_lwp (inferior_ptid);
00180   if (tid == 0)
00181     tid = ptid_get_pid (inferior_ptid); /* Not a threaded program.  */
00182 
00183   errno = 0;
00184   regcache_raw_collect (regcache, regno, &val);
00185   ptrace (PTRACE_POKEUSER, tid,
00186           i386_linux_gregset_reg_offset[regno], val);
00187   if (errno != 0)
00188     error (_("Couldn't write register %s (#%d): %s."),
00189            gdbarch_register_name (get_regcache_arch (regcache), regno),
00190            regno, safe_strerror (errno));
00191 }
00192 
00193 
00194 /* Transfering the general-purpose registers between GDB, inferiors
00195    and core files.  */
00196 
00197 /* Fill GDB's register array with the general-purpose register values
00198    in *GREGSETP.  */
00199 
00200 void
00201 supply_gregset (struct regcache *regcache, const elf_gregset_t *gregsetp)
00202 {
00203   const gdb_byte *regp = (const gdb_byte *) gregsetp;
00204   int i;
00205 
00206   for (i = 0; i < I386_NUM_GREGS; i++)
00207     regcache_raw_supply (regcache, i,
00208                          regp + i386_linux_gregset_reg_offset[i]);
00209 
00210   if (I386_LINUX_ORIG_EAX_REGNUM
00211         < gdbarch_num_regs (get_regcache_arch (regcache)))
00212     regcache_raw_supply (regcache, I386_LINUX_ORIG_EAX_REGNUM, regp
00213                          + i386_linux_gregset_reg_offset[I386_LINUX_ORIG_EAX_REGNUM]);
00214 }
00215 
00216 /* Fill register REGNO (if it is a general-purpose register) in
00217    *GREGSETPS with the value in GDB's register array.  If REGNO is -1,
00218    do this for all registers.  */
00219 
00220 void
00221 fill_gregset (const struct regcache *regcache,
00222               elf_gregset_t *gregsetp, int regno)
00223 {
00224   gdb_byte *regp = (gdb_byte *) gregsetp;
00225   int i;
00226 
00227   for (i = 0; i < I386_NUM_GREGS; i++)
00228     if (regno == -1 || regno == i)
00229       regcache_raw_collect (regcache, i,
00230                             regp + i386_linux_gregset_reg_offset[i]);
00231 
00232   if ((regno == -1 || regno == I386_LINUX_ORIG_EAX_REGNUM)
00233       && I386_LINUX_ORIG_EAX_REGNUM
00234            < gdbarch_num_regs (get_regcache_arch (regcache)))
00235     regcache_raw_collect (regcache, I386_LINUX_ORIG_EAX_REGNUM, regp
00236                           + i386_linux_gregset_reg_offset[I386_LINUX_ORIG_EAX_REGNUM]);
00237 }
00238 
00239 #ifdef HAVE_PTRACE_GETREGS
00240 
00241 /* Fetch all general-purpose registers from process/thread TID and
00242    store their values in GDB's register array.  */
00243 
00244 static void
00245 fetch_regs (struct regcache *regcache, int tid)
00246 {
00247   elf_gregset_t regs;
00248   elf_gregset_t *regs_p = &regs;
00249 
00250   if (ptrace (PTRACE_GETREGS, tid, 0, (int) &regs) < 0)
00251     {
00252       if (errno == EIO)
00253         {
00254           /* The kernel we're running on doesn't support the GETREGS
00255              request.  Reset `have_ptrace_getregs'.  */
00256           have_ptrace_getregs = 0;
00257           return;
00258         }
00259 
00260       perror_with_name (_("Couldn't get registers"));
00261     }
00262 
00263   supply_gregset (regcache, (const elf_gregset_t *) regs_p);
00264 }
00265 
00266 /* Store all valid general-purpose registers in GDB's register array
00267    into the process/thread specified by TID.  */
00268 
00269 static void
00270 store_regs (const struct regcache *regcache, int tid, int regno)
00271 {
00272   elf_gregset_t regs;
00273 
00274   if (ptrace (PTRACE_GETREGS, tid, 0, (int) &regs) < 0)
00275     perror_with_name (_("Couldn't get registers"));
00276 
00277   fill_gregset (regcache, &regs, regno);
00278   
00279   if (ptrace (PTRACE_SETREGS, tid, 0, (int) &regs) < 0)
00280     perror_with_name (_("Couldn't write registers"));
00281 }
00282 
00283 #else
00284 
00285 static void fetch_regs (struct regcache *regcache, int tid) {}
00286 static void store_regs (const struct regcache *regcache, int tid, int regno) {}
00287 
00288 #endif
00289 
00290 
00291 /* Transfering floating-point registers between GDB, inferiors and cores.  */
00292 
00293 /* Fill GDB's register array with the floating-point register values in
00294    *FPREGSETP.  */
00295 
00296 void 
00297 supply_fpregset (struct regcache *regcache, const elf_fpregset_t *fpregsetp)
00298 {
00299   i387_supply_fsave (regcache, -1, fpregsetp);
00300 }
00301 
00302 /* Fill register REGNO (if it is a floating-point register) in
00303    *FPREGSETP with the value in GDB's register array.  If REGNO is -1,
00304    do this for all registers.  */
00305 
00306 void
00307 fill_fpregset (const struct regcache *regcache,
00308                elf_fpregset_t *fpregsetp, int regno)
00309 {
00310   i387_collect_fsave (regcache, regno, fpregsetp);
00311 }
00312 
00313 #ifdef HAVE_PTRACE_GETREGS
00314 
00315 /* Fetch all floating-point registers from process/thread TID and store
00316    thier values in GDB's register array.  */
00317 
00318 static void
00319 fetch_fpregs (struct regcache *regcache, int tid)
00320 {
00321   elf_fpregset_t fpregs;
00322 
00323   if (ptrace (PTRACE_GETFPREGS, tid, 0, (int) &fpregs) < 0)
00324     perror_with_name (_("Couldn't get floating point status"));
00325 
00326   supply_fpregset (regcache, (const elf_fpregset_t *) &fpregs);
00327 }
00328 
00329 /* Store all valid floating-point registers in GDB's register array
00330    into the process/thread specified by TID.  */
00331 
00332 static void
00333 store_fpregs (const struct regcache *regcache, int tid, int regno)
00334 {
00335   elf_fpregset_t fpregs;
00336 
00337   if (ptrace (PTRACE_GETFPREGS, tid, 0, (int) &fpregs) < 0)
00338     perror_with_name (_("Couldn't get floating point status"));
00339 
00340   fill_fpregset (regcache, &fpregs, regno);
00341 
00342   if (ptrace (PTRACE_SETFPREGS, tid, 0, (int) &fpregs) < 0)
00343     perror_with_name (_("Couldn't write floating point status"));
00344 }
00345 
00346 #else
00347 
00348 static void
00349 fetch_fpregs (struct regcache *regcache, int tid)
00350 {
00351 }
00352 
00353 static void
00354 store_fpregs (const struct regcache *regcache, int tid, int regno)
00355 {
00356 }
00357 
00358 #endif
00359 
00360 
00361 /* Transfering floating-point and SSE registers to and from GDB.  */
00362 
00363 /* Fetch all registers covered by the PTRACE_GETREGSET request from
00364    process/thread TID and store their values in GDB's register array.
00365    Return non-zero if successful, zero otherwise.  */
00366 
00367 static int
00368 fetch_xstateregs (struct regcache *regcache, int tid)
00369 {
00370   char xstateregs[I386_XSTATE_MAX_SIZE];
00371   struct iovec iov;
00372 
00373   if (!have_ptrace_getregset)
00374     return 0;
00375 
00376   iov.iov_base = xstateregs;
00377   iov.iov_len = sizeof(xstateregs);
00378   if (ptrace (PTRACE_GETREGSET, tid, (unsigned int) NT_X86_XSTATE,
00379               &iov) < 0)
00380     perror_with_name (_("Couldn't read extended state status"));
00381 
00382   i387_supply_xsave (regcache, -1, xstateregs);
00383   return 1;
00384 }
00385 
00386 /* Store all valid registers in GDB's register array covered by the
00387    PTRACE_SETREGSET request into the process/thread specified by TID.
00388    Return non-zero if successful, zero otherwise.  */
00389 
00390 static int
00391 store_xstateregs (const struct regcache *regcache, int tid, int regno)
00392 {
00393   char xstateregs[I386_XSTATE_MAX_SIZE];
00394   struct iovec iov;
00395 
00396   if (!have_ptrace_getregset)
00397     return 0;
00398   
00399   iov.iov_base = xstateregs;
00400   iov.iov_len = sizeof(xstateregs);
00401   if (ptrace (PTRACE_GETREGSET, tid, (unsigned int) NT_X86_XSTATE,
00402               &iov) < 0)
00403     perror_with_name (_("Couldn't read extended state status"));
00404 
00405   i387_collect_xsave (regcache, regno, xstateregs, 0);
00406 
00407   if (ptrace (PTRACE_SETREGSET, tid, (unsigned int) NT_X86_XSTATE,
00408               (int) &iov) < 0)
00409     perror_with_name (_("Couldn't write extended state status"));
00410 
00411   return 1;
00412 }
00413 
00414 #ifdef HAVE_PTRACE_GETFPXREGS
00415 
00416 /* Fetch all registers covered by the PTRACE_GETFPXREGS request from
00417    process/thread TID and store their values in GDB's register array.
00418    Return non-zero if successful, zero otherwise.  */
00419 
00420 static int
00421 fetch_fpxregs (struct regcache *regcache, int tid)
00422 {
00423   elf_fpxregset_t fpxregs;
00424 
00425   if (! have_ptrace_getfpxregs)
00426     return 0;
00427 
00428   if (ptrace (PTRACE_GETFPXREGS, tid, 0, (int) &fpxregs) < 0)
00429     {
00430       if (errno == EIO)
00431         {
00432           have_ptrace_getfpxregs = 0;
00433           return 0;
00434         }
00435 
00436       perror_with_name (_("Couldn't read floating-point and SSE registers"));
00437     }
00438 
00439   i387_supply_fxsave (regcache, -1, (const elf_fpxregset_t *) &fpxregs);
00440   return 1;
00441 }
00442 
00443 /* Store all valid registers in GDB's register array covered by the
00444    PTRACE_SETFPXREGS request into the process/thread specified by TID.
00445    Return non-zero if successful, zero otherwise.  */
00446 
00447 static int
00448 store_fpxregs (const struct regcache *regcache, int tid, int regno)
00449 {
00450   elf_fpxregset_t fpxregs;
00451 
00452   if (! have_ptrace_getfpxregs)
00453     return 0;
00454   
00455   if (ptrace (PTRACE_GETFPXREGS, tid, 0, &fpxregs) == -1)
00456     {
00457       if (errno == EIO)
00458         {
00459           have_ptrace_getfpxregs = 0;
00460           return 0;
00461         }
00462 
00463       perror_with_name (_("Couldn't read floating-point and SSE registers"));
00464     }
00465 
00466   i387_collect_fxsave (regcache, regno, &fpxregs);
00467 
00468   if (ptrace (PTRACE_SETFPXREGS, tid, 0, &fpxregs) == -1)
00469     perror_with_name (_("Couldn't write floating-point and SSE registers"));
00470 
00471   return 1;
00472 }
00473 
00474 #else
00475 
00476 static int
00477 fetch_fpxregs (struct regcache *regcache, int tid)
00478 {
00479   return 0;
00480 }
00481 
00482 static int
00483 store_fpxregs (const struct regcache *regcache, int tid, int regno)
00484 {
00485   return 0;
00486 }
00487 
00488 #endif /* HAVE_PTRACE_GETFPXREGS */
00489 
00490 
00491 /* Transferring arbitrary registers between GDB and inferior.  */
00492 
00493 /* Fetch register REGNO from the child process.  If REGNO is -1, do
00494    this for all registers (including the floating point and SSE
00495    registers).  */
00496 
00497 static void
00498 i386_linux_fetch_inferior_registers (struct target_ops *ops,
00499                                      struct regcache *regcache, int regno)
00500 {
00501   int tid;
00502 
00503   /* Use the old method of peeking around in `struct user' if the
00504      GETREGS request isn't available.  */
00505   if (!have_ptrace_getregs)
00506     {
00507       int i;
00508 
00509       for (i = 0; i < gdbarch_num_regs (get_regcache_arch (regcache)); i++)
00510         if (regno == -1 || regno == i)
00511           fetch_register (regcache, i);
00512 
00513       return;
00514     }
00515 
00516   /* GNU/Linux LWP ID's are process ID's.  */
00517   tid = ptid_get_lwp (inferior_ptid);
00518   if (tid == 0)
00519     tid = ptid_get_pid (inferior_ptid); /* Not a threaded program.  */
00520 
00521   /* Use the PTRACE_GETFPXREGS request whenever possible, since it
00522      transfers more registers in one system call, and we'll cache the
00523      results.  But remember that fetch_fpxregs can fail, and return
00524      zero.  */
00525   if (regno == -1)
00526     {
00527       fetch_regs (regcache, tid);
00528 
00529       /* The call above might reset `have_ptrace_getregs'.  */
00530       if (!have_ptrace_getregs)
00531         {
00532           i386_linux_fetch_inferior_registers (ops, regcache, regno);
00533           return;
00534         }
00535 
00536       if (fetch_xstateregs (regcache, tid))
00537         return;
00538       if (fetch_fpxregs (regcache, tid))
00539         return;
00540       fetch_fpregs (regcache, tid);
00541       return;
00542     }
00543 
00544   if (GETREGS_SUPPLIES (regno))
00545     {
00546       fetch_regs (regcache, tid);
00547       return;
00548     }
00549 
00550   if (GETXSTATEREGS_SUPPLIES (regno))
00551     {
00552       if (fetch_xstateregs (regcache, tid))
00553         return;
00554     }
00555 
00556   if (GETFPXREGS_SUPPLIES (regno))
00557     {
00558       if (fetch_fpxregs (regcache, tid))
00559         return;
00560 
00561       /* Either our processor or our kernel doesn't support the SSE
00562          registers, so read the FP registers in the traditional way,
00563          and fill the SSE registers with dummy values.  It would be
00564          more graceful to handle differences in the register set using
00565          gdbarch.  Until then, this will at least make things work
00566          plausibly.  */
00567       fetch_fpregs (regcache, tid);
00568       return;
00569     }
00570 
00571   internal_error (__FILE__, __LINE__,
00572                   _("Got request for bad register number %d."), regno);
00573 }
00574 
00575 /* Store register REGNO back into the child process.  If REGNO is -1,
00576    do this for all registers (including the floating point and SSE
00577    registers).  */
00578 static void
00579 i386_linux_store_inferior_registers (struct target_ops *ops,
00580                                      struct regcache *regcache, int regno)
00581 {
00582   int tid;
00583 
00584   /* Use the old method of poking around in `struct user' if the
00585      SETREGS request isn't available.  */
00586   if (!have_ptrace_getregs)
00587     {
00588       int i;
00589 
00590       for (i = 0; i < gdbarch_num_regs (get_regcache_arch (regcache)); i++)
00591         if (regno == -1 || regno == i)
00592           store_register (regcache, i);
00593 
00594       return;
00595     }
00596 
00597   /* GNU/Linux LWP ID's are process ID's.  */
00598   tid = ptid_get_lwp (inferior_ptid);
00599   if (tid == 0)
00600     tid = ptid_get_pid (inferior_ptid); /* Not a threaded program.  */
00601 
00602   /* Use the PTRACE_SETFPXREGS requests whenever possible, since it
00603      transfers more registers in one system call.  But remember that
00604      store_fpxregs can fail, and return zero.  */
00605   if (regno == -1)
00606     {
00607       store_regs (regcache, tid, regno);
00608       if (store_xstateregs (regcache, tid, regno))
00609         return;
00610       if (store_fpxregs (regcache, tid, regno))
00611         return;
00612       store_fpregs (regcache, tid, regno);
00613       return;
00614     }
00615 
00616   if (GETREGS_SUPPLIES (regno))
00617     {
00618       store_regs (regcache, tid, regno);
00619       return;
00620     }
00621 
00622   if (GETXSTATEREGS_SUPPLIES (regno))
00623     {
00624       if (store_xstateregs (regcache, tid, regno))
00625         return;
00626     }
00627 
00628   if (GETFPXREGS_SUPPLIES (regno))
00629     {
00630       if (store_fpxregs (regcache, tid, regno))
00631         return;
00632 
00633       /* Either our processor or our kernel doesn't support the SSE
00634          registers, so just write the FP registers in the traditional
00635          way.  */
00636       store_fpregs (regcache, tid, regno);
00637       return;
00638     }
00639 
00640   internal_error (__FILE__, __LINE__,
00641                   _("Got request to store bad register number %d."), regno);
00642 }
00643 
00644 
00645 /* Support for debug registers.  */
00646 
00647 /* Get debug register REGNUM value from only the one LWP of PTID.  */
00648 
00649 static unsigned long
00650 i386_linux_dr_get (ptid_t ptid, int regnum)
00651 {
00652   int tid;
00653   unsigned long value;
00654 
00655   tid = ptid_get_lwp (ptid);
00656   if (tid == 0)
00657     tid = ptid_get_pid (ptid);
00658 
00659   errno = 0;
00660   value = ptrace (PTRACE_PEEKUSER, tid,
00661                   offsetof (struct user, u_debugreg[regnum]), 0);
00662   if (errno != 0)
00663     perror_with_name (_("Couldn't read debug register"));
00664 
00665   return value;
00666 }
00667 
00668 /* Set debug register REGNUM to VALUE in only the one LWP of PTID.  */
00669 
00670 static void
00671 i386_linux_dr_set (ptid_t ptid, int regnum, unsigned long value)
00672 {
00673   int tid;
00674 
00675   tid = ptid_get_lwp (ptid);
00676   if (tid == 0)
00677     tid = ptid_get_pid (ptid);
00678 
00679   errno = 0;
00680   ptrace (PTRACE_POKEUSER, tid,
00681           offsetof (struct user, u_debugreg[regnum]), value);
00682   if (errno != 0)
00683     perror_with_name (_("Couldn't write debug register"));
00684 }
00685 
00686 /* Return the inferior's debug register REGNUM.  */
00687 
00688 static CORE_ADDR
00689 i386_linux_dr_get_addr (int regnum)
00690 {
00691   /* DR6 and DR7 are retrieved with some other way.  */
00692   gdb_assert (DR_FIRSTADDR <= regnum && regnum <= DR_LASTADDR);
00693 
00694   return i386_linux_dr_get (inferior_ptid, regnum);
00695 }
00696 
00697 /* Return the inferior's DR7 debug control register.  */
00698 
00699 static unsigned long
00700 i386_linux_dr_get_control (void)
00701 {
00702   return i386_linux_dr_get (inferior_ptid, DR_CONTROL);
00703 }
00704 
00705 /* Get DR_STATUS from only the one LWP of INFERIOR_PTID.  */
00706 
00707 static unsigned long
00708 i386_linux_dr_get_status (void)
00709 {
00710   return i386_linux_dr_get (inferior_ptid, DR_STATUS);
00711 }
00712 
00713 /* Callback for iterate_over_lwps.  Update the debug registers of
00714    LWP.  */
00715 
00716 static int
00717 update_debug_registers_callback (struct lwp_info *lwp, void *arg)
00718 {
00719   if (lwp->arch_private == NULL)
00720     lwp->arch_private = XCNEW (struct arch_lwp_info);
00721 
00722   /* The actual update is done later just before resuming the lwp, we
00723      just mark that the registers need updating.  */
00724   lwp->arch_private->debug_registers_changed = 1;
00725 
00726   /* If the lwp isn't stopped, force it to momentarily pause, so we
00727      can update its debug registers.  */
00728   if (!lwp->stopped)
00729     linux_stop_lwp (lwp);
00730 
00731   /* Continue the iteration.  */
00732   return 0;
00733 }
00734 
00735 /* Set DR_CONTROL to ADDR in all LWPs of the current inferior.  */
00736 
00737 static void
00738 i386_linux_dr_set_control (unsigned long control)
00739 {
00740   ptid_t pid_ptid = pid_to_ptid (ptid_get_pid (inferior_ptid));
00741 
00742   iterate_over_lwps (pid_ptid, update_debug_registers_callback, NULL);
00743 }
00744 
00745 /* Set address REGNUM (zero based) to ADDR in all LWPs of the current
00746    inferior.  */
00747 
00748 static void
00749 i386_linux_dr_set_addr (int regnum, CORE_ADDR addr)
00750 {
00751   ptid_t pid_ptid = pid_to_ptid (ptid_get_pid (inferior_ptid));
00752 
00753   gdb_assert (regnum >= 0 && regnum <= DR_LASTADDR - DR_FIRSTADDR);
00754 
00755   iterate_over_lwps (pid_ptid, update_debug_registers_callback, NULL);
00756 }
00757 
00758 /* Called when resuming a thread.
00759    If the debug regs have changed, update the thread's copies.  */
00760 
00761 static void
00762 i386_linux_prepare_to_resume (struct lwp_info *lwp)
00763 {
00764   int clear_status = 0;
00765 
00766   /* NULL means this is the main thread still going through the shell,
00767      or, no watchpoint has been set yet.  In that case, there's
00768      nothing to do.  */
00769   if (lwp->arch_private == NULL)
00770     return;
00771 
00772   if (lwp->arch_private->debug_registers_changed)
00773     {
00774       struct i386_debug_reg_state *state
00775         = i386_debug_reg_state (ptid_get_pid (lwp->ptid));
00776       int i;
00777 
00778       /* See amd64_linux_prepare_to_resume for Linux kernel note on
00779          i386_linux_dr_set calls ordering.  */
00780 
00781       for (i = DR_FIRSTADDR; i <= DR_LASTADDR; i++)
00782         if (state->dr_ref_count[i] > 0)
00783           {
00784             i386_linux_dr_set (lwp->ptid, i, state->dr_mirror[i]);
00785 
00786             /* If we're setting a watchpoint, any change the inferior
00787                had done itself to the debug registers needs to be
00788                discarded, otherwise, i386_stopped_data_address can get
00789                confused.  */
00790             clear_status = 1;
00791           }
00792 
00793       i386_linux_dr_set (lwp->ptid, DR_CONTROL, state->dr_control_mirror);
00794 
00795       lwp->arch_private->debug_registers_changed = 0;
00796     }
00797 
00798   if (clear_status || lwp->stopped_by_watchpoint)
00799     i386_linux_dr_set (lwp->ptid, DR_STATUS, 0);
00800 }
00801 
00802 static void
00803 i386_linux_new_thread (struct lwp_info *lp)
00804 {
00805   struct arch_lwp_info *info = XCNEW (struct arch_lwp_info);
00806 
00807   info->debug_registers_changed = 1;
00808 
00809   lp->arch_private = info;
00810 }
00811 
00812 /* linux_nat_new_fork hook.   */
00813 
00814 static void
00815 i386_linux_new_fork (struct lwp_info *parent, pid_t child_pid)
00816 {
00817   pid_t parent_pid;
00818   struct i386_debug_reg_state *parent_state;
00819   struct i386_debug_reg_state *child_state;
00820 
00821   /* NULL means no watchpoint has ever been set in the parent.  In
00822      that case, there's nothing to do.  */
00823   if (parent->arch_private == NULL)
00824     return;
00825 
00826   /* Linux kernel before 2.6.33 commit
00827      72f674d203cd230426437cdcf7dd6f681dad8b0d
00828      will inherit hardware debug registers from parent
00829      on fork/vfork/clone.  Newer Linux kernels create such tasks with
00830      zeroed debug registers.
00831 
00832      GDB core assumes the child inherits the watchpoints/hw
00833      breakpoints of the parent, and will remove them all from the
00834      forked off process.  Copy the debug registers mirrors into the
00835      new process so that all breakpoints and watchpoints can be
00836      removed together.  The debug registers mirror will become zeroed
00837      in the end before detaching the forked off process, thus making
00838      this compatible with older Linux kernels too.  */
00839 
00840   parent_pid = ptid_get_pid (parent->ptid);
00841   parent_state = i386_debug_reg_state (parent_pid);
00842   child_state = i386_debug_reg_state (child_pid);
00843   *child_state = *parent_state;
00844 }
00845 
00846 
00847 
00848 /* Called by libthread_db.  Returns a pointer to the thread local
00849    storage (or its descriptor).  */
00850 
00851 ps_err_e
00852 ps_get_thread_area (const struct ps_prochandle *ph, 
00853                     lwpid_t lwpid, int idx, void **base)
00854 {
00855   /* NOTE: cagney/2003-08-26: The definition of this buffer is found
00856      in the kernel header <asm-i386/ldt.h>.  It, after padding, is 4 x
00857      4 byte integers in size: `entry_number', `base_addr', `limit',
00858      and a bunch of status bits.
00859 
00860      The values returned by this ptrace call should be part of the
00861      regcache buffer, and ps_get_thread_area should channel its
00862      request through the regcache.  That way remote targets could
00863      provide the value using the remote protocol and not this direct
00864      call.
00865 
00866      Is this function needed?  I'm guessing that the `base' is the
00867      address of a descriptor that libthread_db uses to find the
00868      thread local address base that GDB needs.  Perhaps that
00869      descriptor is defined by the ABI.  Anyway, given that
00870      libthread_db calls this function without prompting (gdb
00871      requesting tls base) I guess it needs info in there anyway.  */
00872   unsigned int desc[4];
00873   gdb_assert (sizeof (int) == 4);
00874 
00875 #ifndef PTRACE_GET_THREAD_AREA
00876 #define PTRACE_GET_THREAD_AREA 25
00877 #endif
00878 
00879   if (ptrace (PTRACE_GET_THREAD_AREA, lwpid,
00880               (void *) idx, (unsigned long) &desc) < 0)
00881     return PS_ERR;
00882 
00883   *(int *)base = desc[1];
00884   return PS_OK;
00885 }
00886 
00887 
00888 /* The instruction for a GNU/Linux system call is:
00889        int $0x80
00890    or 0xcd 0x80.  */
00891 
00892 static const unsigned char linux_syscall[] = { 0xcd, 0x80 };
00893 
00894 #define LINUX_SYSCALL_LEN (sizeof linux_syscall)
00895 
00896 /* The system call number is stored in the %eax register.  */
00897 #define LINUX_SYSCALL_REGNUM I386_EAX_REGNUM
00898 
00899 /* We are specifically interested in the sigreturn and rt_sigreturn
00900    system calls.  */
00901 
00902 #ifndef SYS_sigreturn
00903 #define SYS_sigreturn           0x77
00904 #endif
00905 #ifndef SYS_rt_sigreturn
00906 #define SYS_rt_sigreturn        0xad
00907 #endif
00908 
00909 /* Offset to saved processor flags, from <asm/sigcontext.h>.  */
00910 #define LINUX_SIGCONTEXT_EFLAGS_OFFSET (64)
00911 
00912 /* Resume execution of the inferior process.
00913    If STEP is nonzero, single-step it.
00914    If SIGNAL is nonzero, give it that signal.  */
00915 
00916 static void
00917 i386_linux_resume (struct target_ops *ops,
00918                    ptid_t ptid, int step, enum gdb_signal signal)
00919 {
00920   int pid = ptid_get_pid (ptid);
00921 
00922   int request;
00923 
00924   if (catch_syscall_enabled () > 0)
00925    request = PTRACE_SYSCALL;
00926   else
00927     request = PTRACE_CONT;
00928 
00929   if (step)
00930     {
00931       struct regcache *regcache = get_thread_regcache (pid_to_ptid (pid));
00932       struct gdbarch *gdbarch = get_regcache_arch (regcache);
00933       enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
00934       ULONGEST pc;
00935       gdb_byte buf[LINUX_SYSCALL_LEN];
00936 
00937       request = PTRACE_SINGLESTEP;
00938 
00939       regcache_cooked_read_unsigned (regcache,
00940                                      gdbarch_pc_regnum (gdbarch), &pc);
00941 
00942       /* Returning from a signal trampoline is done by calling a
00943          special system call (sigreturn or rt_sigreturn, see
00944          i386-linux-tdep.c for more information).  This system call
00945          restores the registers that were saved when the signal was
00946          raised, including %eflags.  That means that single-stepping
00947          won't work.  Instead, we'll have to modify the signal context
00948          that's about to be restored, and set the trace flag there.  */
00949 
00950       /* First check if PC is at a system call.  */
00951       if (target_read_memory (pc, buf, LINUX_SYSCALL_LEN) == 0
00952           && memcmp (buf, linux_syscall, LINUX_SYSCALL_LEN) == 0)
00953         {
00954           ULONGEST syscall;
00955           regcache_cooked_read_unsigned (regcache,
00956                                          LINUX_SYSCALL_REGNUM, &syscall);
00957 
00958           /* Then check the system call number.  */
00959           if (syscall == SYS_sigreturn || syscall == SYS_rt_sigreturn)
00960             {
00961               ULONGEST sp, addr;
00962               unsigned long int eflags;
00963 
00964               regcache_cooked_read_unsigned (regcache, I386_ESP_REGNUM, &sp);
00965               if (syscall == SYS_rt_sigreturn)
00966                 addr = read_memory_unsigned_integer (sp + 8, 4, byte_order)
00967                   + 20;
00968               else
00969                 addr = sp;
00970 
00971               /* Set the trace flag in the context that's about to be
00972                  restored.  */
00973               addr += LINUX_SIGCONTEXT_EFLAGS_OFFSET;
00974               read_memory (addr, (gdb_byte *) &eflags, 4);
00975               eflags |= 0x0100;
00976               write_memory (addr, (gdb_byte *) &eflags, 4);
00977             }
00978         }
00979     }
00980 
00981   if (ptrace (request, pid, 0, gdb_signal_to_host (signal)) == -1)
00982     perror_with_name (("ptrace"));
00983 }
00984 
00985 static void (*super_post_startup_inferior) (ptid_t ptid);
00986 
00987 static void
00988 i386_linux_child_post_startup_inferior (ptid_t ptid)
00989 {
00990   i386_cleanup_dregs ();
00991   super_post_startup_inferior (ptid);
00992 }
00993 
00994 /* Get Linux/x86 target description from running target.  */
00995 
00996 static const struct target_desc *
00997 i386_linux_read_description (struct target_ops *ops)
00998 {
00999   int tid;
01000   static uint64_t xcr0;
01001 
01002   /* GNU/Linux LWP ID's are process ID's.  */
01003   tid = ptid_get_lwp (inferior_ptid);
01004   if (tid == 0)
01005     tid = ptid_get_pid (inferior_ptid); /* Not a threaded program.  */
01006 
01007 #ifdef HAVE_PTRACE_GETFPXREGS
01008   if (have_ptrace_getfpxregs == -1)
01009     {
01010       elf_fpxregset_t fpxregs;
01011 
01012       if (ptrace (PTRACE_GETFPXREGS, tid, 0, (int) &fpxregs) < 0)
01013         {
01014           have_ptrace_getfpxregs = 0;
01015           have_ptrace_getregset = 0;
01016           return tdesc_i386_mmx_linux;
01017         }
01018     }
01019 #endif
01020 
01021   if (have_ptrace_getregset == -1)
01022     {
01023       uint64_t xstateregs[(I386_XSTATE_SSE_SIZE / sizeof (uint64_t))];
01024       struct iovec iov;
01025 
01026       iov.iov_base = xstateregs;
01027       iov.iov_len = sizeof (xstateregs);
01028 
01029       /* Check if PTRACE_GETREGSET works.  */
01030       if (ptrace (PTRACE_GETREGSET, tid, (unsigned int) NT_X86_XSTATE,
01031                   &iov) < 0)
01032         have_ptrace_getregset = 0;
01033       else
01034         {
01035           have_ptrace_getregset = 1;
01036 
01037           /* Get XCR0 from XSAVE extended state.  */
01038           xcr0 = xstateregs[(I386_LINUX_XSAVE_XCR0_OFFSET
01039                              / sizeof (long long))];
01040         }
01041     }
01042 
01043   /* Check the native XCR0 only if PTRACE_GETREGSET is available.  */
01044   if (have_ptrace_getregset
01045       && (xcr0 & I386_XSTATE_AVX_MASK) == I386_XSTATE_AVX_MASK)
01046     return tdesc_i386_avx_linux;
01047   else
01048     return tdesc_i386_linux;
01049 }
01050 
01051 /* Enable branch tracing.  */
01052 
01053 static struct btrace_target_info *
01054 i386_linux_enable_btrace (ptid_t ptid)
01055 {
01056   struct btrace_target_info *tinfo;
01057   struct gdbarch *gdbarch;
01058 
01059   errno = 0;
01060   tinfo = linux_enable_btrace (ptid);
01061 
01062   if (tinfo == NULL)
01063     error (_("Could not enable branch tracing for %s: %s."),
01064            target_pid_to_str (ptid), safe_strerror (errno));
01065 
01066   /* Fill in the size of a pointer in bits.  */
01067   gdbarch = target_thread_architecture (ptid);
01068   tinfo->ptr_bits = gdbarch_ptr_bit (gdbarch);
01069 
01070   return tinfo;
01071 }
01072 
01073 /* Disable branch tracing.  */
01074 
01075 static void
01076 i386_linux_disable_btrace (struct btrace_target_info *tinfo)
01077 {
01078   int errcode = linux_disable_btrace (tinfo);
01079 
01080   if (errcode != 0)
01081     error (_("Could not disable branch tracing: %s."), safe_strerror (errcode));
01082 }
01083 
01084 /* Teardown branch tracing.  */
01085 
01086 static void
01087 i386_linux_teardown_btrace (struct btrace_target_info *tinfo)
01088 {
01089   /* Ignore errors.  */
01090   linux_disable_btrace (tinfo);
01091 }
01092 
01093 /* -Wmissing-prototypes */
01094 extern initialize_file_ftype _initialize_i386_linux_nat;
01095 
01096 void
01097 _initialize_i386_linux_nat (void)
01098 {
01099   struct target_ops *t;
01100 
01101   /* Fill in the generic GNU/Linux methods.  */
01102   t = linux_target ();
01103 
01104   i386_use_watchpoints (t);
01105 
01106   i386_dr_low.set_control = i386_linux_dr_set_control;
01107   i386_dr_low.set_addr = i386_linux_dr_set_addr;
01108   i386_dr_low.get_addr = i386_linux_dr_get_addr;
01109   i386_dr_low.get_status = i386_linux_dr_get_status;
01110   i386_dr_low.get_control = i386_linux_dr_get_control;
01111   i386_set_debug_register_length (4);
01112 
01113   /* Override the default ptrace resume method.  */
01114   t->to_resume = i386_linux_resume;
01115 
01116   /* Override the GNU/Linux inferior startup hook.  */
01117   super_post_startup_inferior = t->to_post_startup_inferior;
01118   t->to_post_startup_inferior = i386_linux_child_post_startup_inferior;
01119 
01120   /* Add our register access methods.  */
01121   t->to_fetch_registers = i386_linux_fetch_inferior_registers;
01122   t->to_store_registers = i386_linux_store_inferior_registers;
01123 
01124   t->to_read_description = i386_linux_read_description;
01125 
01126   /* Add btrace methods.  */
01127   t->to_supports_btrace = linux_supports_btrace;
01128   t->to_enable_btrace = i386_linux_enable_btrace;
01129   t->to_disable_btrace = i386_linux_disable_btrace;
01130   t->to_teardown_btrace = i386_linux_teardown_btrace;
01131   t->to_read_btrace = linux_read_btrace;
01132 
01133   /* Register the target.  */
01134   linux_nat_add_target (t);
01135   linux_nat_set_new_thread (t, i386_linux_new_thread);
01136   linux_nat_set_new_fork (t, i386_linux_new_fork);
01137   linux_nat_set_forget_process (t, i386_forget_process);
01138   linux_nat_set_prepare_to_resume (t, i386_linux_prepare_to_resume);
01139 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Defines