gdb/i386gnu-nat.c - gdb

Global variables defined

Data types defined

Functions defined

Macros defined

Source code

  1. /* Low level interface to i386 running the GNU Hurd.

  2.    Copyright (C) 1992-2015 Free Software Foundation, Inc.

  3.    This file is part of GDB.

  4.    This program is free software; you can redistribute it and/or modify
  5.    it under the terms of the GNU General Public License as published by
  6.    the Free Software Foundation; either version 3 of the License, or
  7.    (at your option) any later version.

  8.    This program is distributed in the hope that it will be useful,
  9.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  10.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  11.    GNU General Public License for more details.

  12.    You should have received a copy of the GNU General Public License
  13.    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */

  14. #include "defs.h"
  15. #include "x86-nat.h"
  16. #include "inferior.h"
  17. #include "floatformat.h"
  18. #include "regcache.h"

  19. #include <mach.h>
  20. #include <mach_error.h>
  21. #include <mach/message.h>
  22. #include <mach/exception.h>

  23. #include "i386-tdep.h"

  24. #include "gnu-nat.h"
  25. #include "inf-child.h"
  26. #include "i387-tdep.h"

  27. /* Offset to the thread_state_t location where REG is stored.  */
  28. #define REG_OFFSET(reg) offsetof (struct i386_thread_state, reg)

  29. /* At REG_OFFSET[N] is the offset to the thread_state_t location where
  30.    the GDB register N is stored.  */
  31. static int reg_offset[] =
  32. {
  33.   REG_OFFSET (eax), REG_OFFSET (ecx), REG_OFFSET (edx), REG_OFFSET (ebx),
  34.   REG_OFFSET (uesp), REG_OFFSET (ebp), REG_OFFSET (esi), REG_OFFSET (edi),
  35.   REG_OFFSET (eip), REG_OFFSET (efl), REG_OFFSET (cs), REG_OFFSET (ss),
  36.   REG_OFFSET (ds), REG_OFFSET (es), REG_OFFSET (fs), REG_OFFSET (gs)
  37. };

  38. #define REG_ADDR(state, regnum) ((char *)(state) + reg_offset[regnum])


  39. /* Get the whole floating-point state of THREAD and record the values
  40.    of the corresponding (pseudo) registers.  */

  41. static void
  42. fetch_fpregs (struct regcache *regcache, struct proc *thread)
  43. {
  44.   mach_msg_type_number_t count = i386_FLOAT_STATE_COUNT;
  45.   struct i386_float_state state;
  46.   error_t err;

  47.   err = thread_get_state (thread->port, i386_FLOAT_STATE,
  48.                           (thread_state_t) &state, &count);
  49.   if (err)
  50.     {
  51.       warning (_("Couldn't fetch floating-point state from %s"),
  52.                proc_string (thread));
  53.       return;
  54.     }

  55.   if (!state.initialized)
  56.     {
  57.       /* The floating-point state isn't initialized.  */
  58.       i387_supply_fsave (regcache, -1, NULL);
  59.     }
  60.   else
  61.     {
  62.       /* Supply the floating-point registers.  */
  63.       i387_supply_fsave (regcache, -1, state.hw_state);
  64.     }
  65. }

  66. /* Fetch register REGNO, or all regs if REGNO is -1.  */
  67. static void
  68. gnu_fetch_registers (struct target_ops *ops,
  69.                      struct regcache *regcache, int regno)
  70. {
  71.   struct proc *thread;

  72.   /* Make sure we know about new threads.  */
  73.   inf_update_procs (gnu_current_inf);

  74.   thread = inf_tid_to_thread (gnu_current_inf,
  75.                               ptid_get_lwp (inferior_ptid));
  76.   if (!thread)
  77.     error (_("Can't fetch registers from thread %s: No such thread"),
  78.            target_pid_to_str (inferior_ptid));

  79.   if (regno < I386_NUM_GREGS || regno == -1)
  80.     {
  81.       thread_state_t state;

  82.       /* This does the dirty work for us.  */
  83.       state = proc_get_state (thread, 0);
  84.       if (!state)
  85.         {
  86.           warning (_("Couldn't fetch registers from %s"),
  87.                    proc_string (thread));
  88.           return;
  89.         }

  90.       if (regno == -1)
  91.         {
  92.           int i;

  93.           proc_debug (thread, "fetching all register");

  94.           for (i = 0; i < I386_NUM_GREGS; i++)
  95.             regcache_raw_supply (regcache, i, REG_ADDR (state, i));
  96.           thread->fetched_regs = ~0;
  97.         }
  98.       else
  99.         {
  100.           proc_debug (thread, "fetching register %s",
  101.                       gdbarch_register_name (get_regcache_arch (regcache),
  102.                                              regno));

  103.           regcache_raw_supply (regcache, regno,
  104.                                REG_ADDR (state, regno));
  105.           thread->fetched_regs |= (1 << regno);
  106.         }
  107.     }

  108.   if (regno >= I386_NUM_GREGS || regno == -1)
  109.     {
  110.       proc_debug (thread, "fetching floating-point registers");

  111.       fetch_fpregs (regcache, thread);
  112.     }
  113. }


  114. /* Store the whole floating-point state into THREAD using information
  115.    from the corresponding (pseudo) registers.  */
  116. static void
  117. store_fpregs (const struct regcache *regcache, struct proc *thread, int regno)
  118. {
  119.   mach_msg_type_number_t count = i386_FLOAT_STATE_COUNT;
  120.   struct i386_float_state state;
  121.   error_t err;

  122.   err = thread_get_state (thread->port, i386_FLOAT_STATE,
  123.                           (thread_state_t) &state, &count);
  124.   if (err)
  125.     {
  126.       warning (_("Couldn't fetch floating-point state from %s"),
  127.                proc_string (thread));
  128.       return;
  129.     }

  130.   /* FIXME: kettenis/2001-07-15: Is this right?  Should we somehow
  131.      take into account DEPRECATED_REGISTER_VALID like the old code did?  */
  132.   i387_collect_fsave (regcache, regno, state.hw_state);

  133.   err = thread_set_state (thread->port, i386_FLOAT_STATE,
  134.                           (thread_state_t) &state, i386_FLOAT_STATE_COUNT);
  135.   if (err)
  136.     {
  137.       warning (_("Couldn't store floating-point state into %s"),
  138.                proc_string (thread));
  139.       return;
  140.     }
  141. }

  142. /* Store at least register REGNO, or all regs if REGNO == -1.  */
  143. static void
  144. gnu_store_registers (struct target_ops *ops,
  145.                      struct regcache *regcache, int regno)
  146. {
  147.   struct proc *thread;
  148.   struct gdbarch *gdbarch = get_regcache_arch (regcache);

  149.   /* Make sure we know about new threads.  */
  150.   inf_update_procs (gnu_current_inf);

  151.   thread = inf_tid_to_thread (gnu_current_inf,
  152.                               ptid_get_lwp (inferior_ptid));
  153.   if (!thread)
  154.     error (_("Couldn't store registers into thread %s: No such thread"),
  155.            target_pid_to_str (inferior_ptid));

  156.   if (regno < I386_NUM_GREGS || regno == -1)
  157.     {
  158.       thread_state_t state;
  159.       thread_state_data_t old_state;
  160.       int was_aborted = thread->aborted;
  161.       int was_valid = thread->state_valid;
  162.       int trace;

  163.       if (!was_aborted && was_valid)
  164.         memcpy (&old_state, &thread->state, sizeof (old_state));

  165.       state = proc_get_state (thread, 1);
  166.       if (!state)
  167.         {
  168.           warning (_("Couldn't store registers into %s"),
  169.                    proc_string (thread));
  170.           return;
  171.         }

  172.       /* Save the T bit.  We might try to restore the %eflags register
  173.          below, but changing the T bit would seriously confuse GDB.  */
  174.       trace = ((struct i386_thread_state *)state)->efl & 0x100;

  175.       if (!was_aborted && was_valid)
  176.         /* See which registers have changed after aborting the thread.  */
  177.         {
  178.           int check_regno;

  179.           for (check_regno = 0; check_regno < I386_NUM_GREGS; check_regno++)
  180.             if ((thread->fetched_regs & (1 << check_regno))
  181.                 && memcpy (REG_ADDR (&old_state, check_regno),
  182.                            REG_ADDR (state, check_regno),
  183.                            register_size (gdbarch, check_regno)))
  184.               /* Register CHECK_REGNO has changed!  Ack!  */
  185.               {
  186.                 warning (_("Register %s changed after the thread was aborted"),
  187.                          gdbarch_register_name (gdbarch, check_regno));
  188.                 if (regno >= 0 && regno != check_regno)
  189.                   /* Update GDB's copy of the register.  */
  190.                   regcache_raw_supply (regcache, check_regno,
  191.                                        REG_ADDR (state, check_regno));
  192.                 else
  193.                   warning (_("... also writing this register!  "
  194.                              "Suspicious..."));
  195.               }
  196.         }

  197.       if (regno == -1)
  198.         {
  199.           int i;

  200.           proc_debug (thread, "storing all registers");

  201.           for (i = 0; i < I386_NUM_GREGS; i++)
  202.             if (REG_VALID == regcache_register_status (regcache, i))
  203.               regcache_raw_collect (regcache, i, REG_ADDR (state, i));
  204.         }
  205.       else
  206.         {
  207.           proc_debug (thread, "storing register %s",
  208.                       gdbarch_register_name (gdbarch, regno));

  209.           gdb_assert (REG_VALID == regcache_register_status (regcache, regno));
  210.           regcache_raw_collect (regcache, regno, REG_ADDR (state, regno));
  211.         }

  212.       /* Restore the T bit.  */
  213.       ((struct i386_thread_state *)state)->efl &= ~0x100;
  214.       ((struct i386_thread_state *)state)->efl |= trace;
  215.     }

  216.   if (regno >= I386_NUM_GREGS || regno == -1)
  217.     {
  218.       proc_debug (thread, "storing floating-point registers");

  219.       store_fpregs (regcache, thread, regno);
  220.     }
  221. }


  222. /* Support for debug registers.  */

  223. #ifdef i386_DEBUG_STATE
  224. /* Get debug registers for thread THREAD.  */

  225. static void
  226. i386_gnu_dr_get (struct i386_debug_state *regs, struct proc *thread)
  227. {
  228.   mach_msg_type_number_t count = i386_DEBUG_STATE_COUNT;
  229.   error_t err;

  230.   err = thread_get_state (thread->port, i386_DEBUG_STATE,
  231.                           (thread_state_t) regs, &count);
  232.   if (err != 0 || count != i386_DEBUG_STATE_COUNT)
  233.     warning (_("Couldn't fetch debug state from %s"),
  234.              proc_string (thread));
  235. }

  236. /* Set debug registers for thread THREAD.  */

  237. static void
  238. i386_gnu_dr_set (const struct i386_debug_state *regs, struct proc *thread)
  239. {
  240.   error_t err;

  241.   err = thread_set_state (thread->port, i386_DEBUG_STATE,
  242.                           (thread_state_t) regs, i386_DEBUG_STATE_COUNT);
  243.   if (err != 0)
  244.     warning (_("Couldn't store debug state into %s"),
  245.              proc_string (thread));
  246. }

  247. /* Set DR_CONTROL in THREAD.  */

  248. static void
  249. i386_gnu_dr_set_control_one (struct proc *thread, void *arg)
  250. {
  251.   unsigned long *control = arg;
  252.   struct i386_debug_state regs;

  253.   i386_gnu_dr_get (&regs, thread);
  254.   regs.dr[DR_CONTROL] = *control;
  255.   i386_gnu_dr_set (&regs, thread);
  256. }

  257. /* Set DR_CONTROL to CONTROL in all threads.  */

  258. static void
  259. i386_gnu_dr_set_control (unsigned long control)
  260. {
  261.   inf_update_procs (gnu_current_inf);
  262.   inf_threads (gnu_current_inf, i386_gnu_dr_set_control_one, &control);
  263. }

  264. /* Parameters to set a debugging address.  */

  265. struct reg_addr
  266. {
  267.   int regnum;                /* Register number (zero based).  */
  268.   CORE_ADDR addr;        /* Address.  */
  269. };

  270. /* Set address REGNUM (zero based) to ADDR in THREAD.  */

  271. static void
  272. i386_gnu_dr_set_addr_one (struct proc *thread, void *arg)
  273. {
  274.   struct reg_addr *reg_addr = arg;
  275.   struct i386_debug_state regs;

  276.   i386_gnu_dr_get (&regs, thread);
  277.   regs.dr[reg_addr->regnum] = reg_addr->addr;
  278.   i386_gnu_dr_set (&regs, thread);
  279. }

  280. /* Set address REGNUM (zero based) to ADDR in all threads.  */

  281. static void
  282. i386_gnu_dr_set_addr (int regnum, CORE_ADDR addr)
  283. {
  284.   struct reg_addr reg_addr;

  285.   gdb_assert (DR_FIRSTADDR <= regnum && regnum <= DR_LASTADDR);

  286.   reg_addr.regnum = regnum;
  287.   reg_addr.addr = addr;

  288.   inf_update_procs (gnu_current_inf);
  289.   inf_threads (gnu_current_inf, i386_gnu_dr_set_addr_one, &reg_addr);
  290. }

  291. /* Get debug register REGNUM value from only the one LWP of PTID.  */

  292. static unsigned long
  293. i386_gnu_dr_get_reg (ptid_t ptid, int regnum)
  294. {
  295.   struct i386_debug_state regs;
  296.   struct proc *thread;

  297.   /* Make sure we know about new threads.  */
  298.   inf_update_procs (gnu_current_inf);

  299.   thread = inf_tid_to_thread (gnu_current_inf, ptid_get_lwp (ptid));
  300.   i386_gnu_dr_get (&regs, thread);

  301.   return regs.dr[regnum];
  302. }

  303. /* Return the inferior's debug register REGNUM.  */

  304. static CORE_ADDR
  305. i386_gnu_dr_get_addr (int regnum)
  306. {
  307.   gdb_assert (DR_FIRSTADDR <= regnum && regnum <= DR_LASTADDR);

  308.   return i386_gnu_dr_get_reg (inferior_ptid, regnum);
  309. }

  310. /* Get DR_STATUS from only the one thread of INFERIOR_PTID.  */

  311. static unsigned long
  312. i386_gnu_dr_get_status (void)
  313. {
  314.   return i386_gnu_dr_get_reg (inferior_ptid, DR_STATUS);
  315. }

  316. /* Return the inferior's DR7 debug control register.  */

  317. static unsigned long
  318. i386_gnu_dr_get_control (void)
  319. {
  320.   return i386_gnu_dr_get_reg (inferior_ptid, DR_CONTROL);
  321. }
  322. #endif /* i386_DEBUG_STATE */

  323. /* Provide a prototype to silence -Wmissing-prototypes.  */
  324. extern initialize_file_ftype _initialize_i386gnu_nat;

  325. void
  326. _initialize_i386gnu_nat (void)
  327. {
  328.   struct target_ops *t;

  329.   /* Fill in the generic GNU/Hurd methods.  */
  330.   t = gnu_target ();

  331. #ifdef i386_DEBUG_STATE
  332.   x86_use_watchpoints (t);

  333.   x86_dr_low.set_control = i386_gnu_dr_set_control;
  334.   gdb_assert (DR_FIRSTADDR == 0 && DR_LASTADDR < i386_DEBUG_STATE_COUNT);
  335.   x86_dr_low.set_addr = i386_gnu_dr_set_addr;
  336.   x86_dr_low.get_addr = i386_gnu_dr_get_addr;
  337.   x86_dr_low.get_status = i386_gnu_dr_get_status;
  338.   x86_dr_low.get_control = i386_gnu_dr_get_control;
  339.   x86_set_debug_register_length (4);
  340. #endif /* i386_DEBUG_STATE */

  341.   t->to_fetch_registers = gnu_fetch_registers;
  342.   t->to_store_registers = gnu_store_registers;

  343.   /* Register the target.  */
  344.   add_target (t);
  345. }