GDB (API)
/home/stan/gdb/src/gdb/i386gnu-nat.c
Go to the documentation of this file.
00001 /* Low level interface to i386 running the GNU Hurd.
00002 
00003    Copyright (C) 1992-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 "inferior.h"
00022 #include "floatformat.h"
00023 #include "regcache.h"
00024 
00025 #include "gdb_assert.h"
00026 #include <errno.h>
00027 #include <stdio.h>
00028 #include "gdb_string.h"
00029 
00030 #include <mach.h>
00031 #include <mach_error.h>
00032 #include <mach/message.h>
00033 #include <mach/exception.h>
00034 
00035 #include "i386-tdep.h"
00036 
00037 #include "gnu-nat.h"
00038 #include "i387-tdep.h"
00039 
00040 #ifdef HAVE_SYS_PROCFS_H
00041 # include <sys/procfs.h>
00042 # include "gregset.h"
00043 #endif
00044 
00045 /* Offset to the thread_state_t location where REG is stored.  */
00046 #define REG_OFFSET(reg) offsetof (struct i386_thread_state, reg)
00047 
00048 /* At REG_OFFSET[N] is the offset to the thread_state_t location where
00049    the GDB register N is stored.  */
00050 static int reg_offset[] =
00051 {
00052   REG_OFFSET (eax), REG_OFFSET (ecx), REG_OFFSET (edx), REG_OFFSET (ebx),
00053   REG_OFFSET (uesp), REG_OFFSET (ebp), REG_OFFSET (esi), REG_OFFSET (edi),
00054   REG_OFFSET (eip), REG_OFFSET (efl), REG_OFFSET (cs), REG_OFFSET (ss),
00055   REG_OFFSET (ds), REG_OFFSET (es), REG_OFFSET (fs), REG_OFFSET (gs)
00056 };
00057 
00058 /* Offset to the greg_t location where REG is stored.  */
00059 #define CREG_OFFSET(reg) (REG_##reg * 4)
00060 
00061 /* At CREG_OFFSET[N] is the offset to the greg_t location where
00062    the GDB register N is stored.  */
00063 static int creg_offset[] =
00064 {
00065   CREG_OFFSET (EAX), CREG_OFFSET (ECX), CREG_OFFSET (EDX), CREG_OFFSET (EBX),
00066   CREG_OFFSET (UESP), CREG_OFFSET (EBP), CREG_OFFSET (ESI), CREG_OFFSET (EDI),
00067   CREG_OFFSET (EIP), CREG_OFFSET (EFL), CREG_OFFSET (CS), CREG_OFFSET (SS),
00068   CREG_OFFSET (DS), CREG_OFFSET (ES), CREG_OFFSET (FS), CREG_OFFSET (GS)
00069 };
00070 
00071 #define REG_ADDR(state, regnum) ((char *)(state) + reg_offset[regnum])
00072 #define CREG_ADDR(state, regnum) ((const char *)(state) + creg_offset[regnum])
00073 
00074 
00075 /* Get the whole floating-point state of THREAD and record the values
00076    of the corresponding (pseudo) registers.  */
00077 
00078 static void
00079 fetch_fpregs (struct regcache *regcache, struct proc *thread)
00080 {
00081   mach_msg_type_number_t count = i386_FLOAT_STATE_COUNT;
00082   struct i386_float_state state;
00083   error_t err;
00084 
00085   err = thread_get_state (thread->port, i386_FLOAT_STATE,
00086                           (thread_state_t) &state, &count);
00087   if (err)
00088     {
00089       warning (_("Couldn't fetch floating-point state from %s"),
00090                proc_string (thread));
00091       return;
00092     }
00093 
00094   if (!state.initialized)
00095     {
00096       /* The floating-point state isn't initialized.  */
00097       i387_supply_fsave (regcache, -1, NULL);
00098     }
00099   else
00100     {
00101       /* Supply the floating-point registers.  */
00102       i387_supply_fsave (regcache, -1, state.hw_state);
00103     }
00104 }
00105 
00106 #ifdef HAVE_SYS_PROCFS_H
00107 /* These two calls are used by the core-regset.c code for
00108    reading ELF core files.  */
00109 void
00110 supply_gregset (struct regcache *regcache, const gdb_gregset_t *gregs)
00111 {
00112   int i;
00113   for (i = 0; i < I386_NUM_GREGS; i++)
00114     regcache_raw_supply (regcache, i, CREG_ADDR (gregs, i));
00115 }
00116 
00117 void
00118 supply_fpregset (struct regcache *regcache, const gdb_fpregset_t *fpregs)
00119 {
00120   i387_supply_fsave (regcache, -1, fpregs);
00121 }
00122 #endif
00123 
00124 /* Fetch register REGNO, or all regs if REGNO is -1.  */
00125 static void
00126 gnu_fetch_registers (struct target_ops *ops,
00127                      struct regcache *regcache, int regno)
00128 {
00129   struct proc *thread;
00130 
00131   /* Make sure we know about new threads.  */
00132   inf_update_procs (gnu_current_inf);
00133 
00134   thread = inf_tid_to_thread (gnu_current_inf,
00135                               ptid_get_lwp (inferior_ptid));
00136   if (!thread)
00137     error (_("Can't fetch registers from thread %s: No such thread"),
00138            target_pid_to_str (inferior_ptid));
00139 
00140   if (regno < I386_NUM_GREGS || regno == -1)
00141     {
00142       thread_state_t state;
00143 
00144       /* This does the dirty work for us.  */
00145       state = proc_get_state (thread, 0);
00146       if (!state)
00147         {
00148           warning (_("Couldn't fetch registers from %s"),
00149                    proc_string (thread));
00150           return;
00151         }
00152 
00153       if (regno == -1)
00154         {
00155           int i;
00156 
00157           proc_debug (thread, "fetching all register");
00158 
00159           for (i = 0; i < I386_NUM_GREGS; i++)
00160             regcache_raw_supply (regcache, i, REG_ADDR (state, i));
00161           thread->fetched_regs = ~0;
00162         }
00163       else
00164         {
00165           proc_debug (thread, "fetching register %s",
00166                       gdbarch_register_name (get_regcache_arch (regcache),
00167                                              regno));
00168 
00169           regcache_raw_supply (regcache, regno,
00170                                REG_ADDR (state, regno));
00171           thread->fetched_regs |= (1 << regno);
00172         }
00173     }
00174 
00175   if (regno >= I386_NUM_GREGS || regno == -1)
00176     {
00177       proc_debug (thread, "fetching floating-point registers");
00178 
00179       fetch_fpregs (regcache, thread);
00180     }
00181 }
00182 
00183 
00184 /* Store the whole floating-point state into THREAD using information
00185    from the corresponding (pseudo) registers.  */
00186 static void
00187 store_fpregs (const struct regcache *regcache, struct proc *thread, int regno)
00188 {
00189   mach_msg_type_number_t count = i386_FLOAT_STATE_COUNT;
00190   struct i386_float_state state;
00191   error_t err;
00192 
00193   err = thread_get_state (thread->port, i386_FLOAT_STATE,
00194                           (thread_state_t) &state, &count);
00195   if (err)
00196     {
00197       warning (_("Couldn't fetch floating-point state from %s"),
00198                proc_string (thread));
00199       return;
00200     }
00201 
00202   /* FIXME: kettenis/2001-07-15: Is this right?  Should we somehow
00203      take into account DEPRECATED_REGISTER_VALID like the old code did?  */
00204   i387_collect_fsave (regcache, regno, state.hw_state);
00205 
00206   err = thread_set_state (thread->port, i386_FLOAT_STATE,
00207                           (thread_state_t) &state, i386_FLOAT_STATE_COUNT);
00208   if (err)
00209     {
00210       warning (_("Couldn't store floating-point state into %s"),
00211                proc_string (thread));
00212       return;
00213     }
00214 }
00215 
00216 /* Store at least register REGNO, or all regs if REGNO == -1.  */
00217 static void
00218 gnu_store_registers (struct target_ops *ops,
00219                      struct regcache *regcache, int regno)
00220 {
00221   struct proc *thread;
00222   struct gdbarch *gdbarch = get_regcache_arch (regcache);
00223 
00224   /* Make sure we know about new threads.  */
00225   inf_update_procs (gnu_current_inf);
00226 
00227   thread = inf_tid_to_thread (gnu_current_inf,
00228                               ptid_get_lwp (inferior_ptid));
00229   if (!thread)
00230     error (_("Couldn't store registers into thread %s: No such thread"),
00231            target_pid_to_str (inferior_ptid));
00232 
00233   if (regno < I386_NUM_GREGS || regno == -1)
00234     {
00235       thread_state_t state;
00236       thread_state_data_t old_state;
00237       int was_aborted = thread->aborted;
00238       int was_valid = thread->state_valid;
00239       int trace;
00240 
00241       if (!was_aborted && was_valid)
00242         memcpy (&old_state, &thread->state, sizeof (old_state));
00243 
00244       state = proc_get_state (thread, 1);
00245       if (!state)
00246         {
00247           warning (_("Couldn't store registers into %s"),
00248                    proc_string (thread));
00249           return;
00250         }
00251 
00252       /* Save the T bit.  We might try to restore the %eflags register
00253          below, but changing the T bit would seriously confuse GDB.  */
00254       trace = ((struct i386_thread_state *)state)->efl & 0x100;
00255 
00256       if (!was_aborted && was_valid)
00257         /* See which registers have changed after aborting the thread.  */
00258         {
00259           int check_regno;
00260 
00261           for (check_regno = 0; check_regno < I386_NUM_GREGS; check_regno++)
00262             if ((thread->fetched_regs & (1 << check_regno))
00263                 && memcpy (REG_ADDR (&old_state, check_regno),
00264                            REG_ADDR (state, check_regno),
00265                            register_size (gdbarch, check_regno)))
00266               /* Register CHECK_REGNO has changed!  Ack!  */
00267               {
00268                 warning (_("Register %s changed after the thread was aborted"),
00269                          gdbarch_register_name (gdbarch, check_regno));
00270                 if (regno >= 0 && regno != check_regno)
00271                   /* Update GDB's copy of the register.  */
00272                   regcache_raw_supply (regcache, check_regno,
00273                                        REG_ADDR (state, check_regno));
00274                 else
00275                   warning (_("... also writing this register!  "
00276                              "Suspicious..."));
00277               }
00278         }
00279 
00280       if (regno == -1)
00281         {
00282           int i;
00283 
00284           proc_debug (thread, "storing all registers");
00285 
00286           for (i = 0; i < I386_NUM_GREGS; i++)
00287             if (REG_VALID == regcache_register_status (regcache, i))
00288               regcache_raw_collect (regcache, i, REG_ADDR (state, i));
00289         }
00290       else
00291         {
00292           proc_debug (thread, "storing register %s",
00293                       gdbarch_register_name (gdbarch, regno));
00294 
00295           gdb_assert (REG_VALID == regcache_register_status (regcache, regno));
00296           regcache_raw_collect (regcache, regno, REG_ADDR (state, regno));
00297         }
00298 
00299       /* Restore the T bit.  */
00300       ((struct i386_thread_state *)state)->efl &= ~0x100;
00301       ((struct i386_thread_state *)state)->efl |= trace;
00302     }
00303 
00304   if (regno >= I386_NUM_GREGS || regno == -1)
00305     {
00306       proc_debug (thread, "storing floating-point registers");
00307 
00308       store_fpregs (regcache, thread, regno);
00309     }
00310 }
00311 
00312 /* Provide a prototype to silence -Wmissing-prototypes.  */
00313 extern initialize_file_ftype _initialize_i386gnu_nat;
00314 
00315 void
00316 _initialize_i386gnu_nat (void)
00317 {
00318   struct target_ops *t;
00319 
00320   /* Fill in the generic GNU/Hurd methods.  */
00321   t = gnu_target ();
00322 
00323   t->to_fetch_registers = gnu_fetch_registers;
00324   t->to_store_registers = gnu_store_registers;
00325 
00326   /* Register the target.  */
00327   add_target (t);
00328 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Defines