gdb/i386bsd-nat.c - gdb

Global variables defined

Functions defined

Macros defined

Source code

  1. /* Native-dependent code for modern i386 BSD's.

  2.    Copyright (C) 2000-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 "inferior.h"
  16. #include "regcache.h"

  17. #include <signal.h>
  18. #include <sys/types.h>
  19. #include <sys/ptrace.h>
  20. #include <machine/reg.h>
  21. #include <machine/frame.h>

  22. #include "i386-tdep.h"
  23. #include "i387-tdep.h"
  24. #include "i386bsd-nat.h"
  25. #include "inf-ptrace.h"


  26. /* In older BSD versions we cannot get at some of the segment
  27.    registers.  FreeBSD for example didn't support the %fs and %gs
  28.    registers until the 3.0 release.  We have autoconf checks for their
  29.    presence, and deal gracefully with their absence.  */

  30. /* Offset in `struct reg' where MEMBER is stored.  */
  31. #define REG_OFFSET(member) offsetof (struct reg, member)

  32. /* At i386bsd_reg_offset[REGNUM] you'll find the offset in `struct
  33.    reg' where the GDB register REGNUM is stored.  Unsupported
  34.    registers are marked with `-1'.  */
  35. static int i386bsd_r_reg_offset[] =
  36. {
  37.   REG_OFFSET (r_eax),
  38.   REG_OFFSET (r_ecx),
  39.   REG_OFFSET (r_edx),
  40.   REG_OFFSET (r_ebx),
  41.   REG_OFFSET (r_esp),
  42.   REG_OFFSET (r_ebp),
  43.   REG_OFFSET (r_esi),
  44.   REG_OFFSET (r_edi),
  45.   REG_OFFSET (r_eip),
  46.   REG_OFFSET (r_eflags),
  47.   REG_OFFSET (r_cs),
  48.   REG_OFFSET (r_ss),
  49.   REG_OFFSET (r_ds),
  50.   REG_OFFSET (r_es),
  51. #ifdef HAVE_STRUCT_REG_R_FS
  52.   REG_OFFSET (r_fs),
  53. #else
  54.   -1,
  55. #endif
  56. #ifdef HAVE_STRUCT_REG_R_GS
  57.   REG_OFFSET (r_gs)
  58. #else
  59.   -1
  60. #endif
  61. };

  62. /* Macro to determine if a register is fetched with PT_GETREGS.  */
  63. #define GETREGS_SUPPLIES(regnum) \
  64.   ((0 <= (regnum) && (regnum) <= 15))

  65. #ifdef HAVE_PT_GETXMMREGS
  66. /* Set to 1 if the kernel supports PT_GETXMMREGS.  Initialized to -1
  67.    so that we try PT_GETXMMREGS the first time around.  */
  68. static int have_ptrace_xmmregs = -1;
  69. #endif


  70. /* Supply the general-purpose registers in GREGS, to REGCACHE.  */

  71. static void
  72. i386bsd_supply_gregset (struct regcache *regcache, const void *gregs)
  73. {
  74.   const char *regs = gregs;
  75.   int regnum;

  76.   for (regnum = 0; regnum < ARRAY_SIZE (i386bsd_r_reg_offset); regnum++)
  77.     {
  78.       int offset = i386bsd_r_reg_offset[regnum];

  79.       if (offset != -1)
  80.         regcache_raw_supply (regcache, regnum, regs + offset);
  81.     }
  82. }

  83. /* Collect register REGNUM from REGCACHE and store its contents in
  84.    GREGS.  If REGNUM is -1, collect and store all appropriate
  85.    registers.  */

  86. static void
  87. i386bsd_collect_gregset (const struct regcache *regcache,
  88.                          void *gregs, int regnum)
  89. {
  90.   char *regs = gregs;
  91.   int i;

  92.   for (i = 0; i < ARRAY_SIZE (i386bsd_r_reg_offset); i++)
  93.     {
  94.       if (regnum == -1 || regnum == i)
  95.         {
  96.           int offset = i386bsd_r_reg_offset[i];

  97.           if (offset != -1)
  98.             regcache_raw_collect (regcache, i, regs + offset);
  99.         }
  100.     }
  101. }

  102. /* Fetch register REGNUM from the inferior.  If REGNUM is -1, do this
  103.    for all registers (including the floating point registers).  */

  104. static void
  105. i386bsd_fetch_inferior_registers (struct target_ops *ops,
  106.                                   struct regcache *regcache, int regnum)
  107. {
  108.   if (regnum == -1 || GETREGS_SUPPLIES (regnum))
  109.     {
  110.       struct reg regs;

  111.       if (ptrace (PT_GETREGS, ptid_get_pid (inferior_ptid),
  112.                   (PTRACE_TYPE_ARG3) &regs, 0) == -1)
  113.         perror_with_name (_("Couldn't get registers"));

  114.       i386bsd_supply_gregset (regcache, &regs);
  115.       if (regnum != -1)
  116.         return;
  117.     }

  118.   if (regnum == -1 || regnum >= I386_ST0_REGNUM)
  119.     {
  120.       struct fpreg fpregs;
  121. #ifdef HAVE_PT_GETXMMREGS
  122.       char xmmregs[512];

  123.       if (have_ptrace_xmmregs != 0
  124.           && ptrace(PT_GETXMMREGS, ptid_get_pid (inferior_ptid),
  125.                     (PTRACE_TYPE_ARG3) xmmregs, 0) == 0)
  126.         {
  127.           have_ptrace_xmmregs = 1;
  128.           i387_supply_fxsave (regcache, -1, xmmregs);
  129.         }
  130.       else
  131.         {
  132.           if (ptrace (PT_GETFPREGS, ptid_get_pid (inferior_ptid),
  133.                       (PTRACE_TYPE_ARG3) &fpregs, 0) == -1)
  134.             perror_with_name (_("Couldn't get floating point status"));

  135.           i387_supply_fsave (regcache, -1, &fpregs);
  136.         }
  137. #else
  138.       if (ptrace (PT_GETFPREGS, ptid_get_pid (inferior_ptid),
  139.                   (PTRACE_TYPE_ARG3) &fpregs, 0) == -1)
  140.         perror_with_name (_("Couldn't get floating point status"));

  141.       i387_supply_fsave (regcache, -1, &fpregs);
  142. #endif
  143.     }
  144. }

  145. /* Store register REGNUM back into the inferior.  If REGNUM is -1, do
  146.    this for all registers (including the floating point registers).  */

  147. static void
  148. i386bsd_store_inferior_registers (struct target_ops *ops,
  149.                                   struct regcache *regcache, int regnum)
  150. {
  151.   if (regnum == -1 || GETREGS_SUPPLIES (regnum))
  152.     {
  153.       struct reg regs;

  154.       if (ptrace (PT_GETREGS, ptid_get_pid (inferior_ptid),
  155.                   (PTRACE_TYPE_ARG3) &regs, 0) == -1)
  156.         perror_with_name (_("Couldn't get registers"));

  157.       i386bsd_collect_gregset (regcache, &regs, regnum);

  158.       if (ptrace (PT_SETREGS, ptid_get_pid (inferior_ptid),
  159.                   (PTRACE_TYPE_ARG3) &regs, 0) == -1)
  160.         perror_with_name (_("Couldn't write registers"));

  161.       if (regnum != -1)
  162.         return;
  163.     }

  164.   if (regnum == -1 || regnum >= I386_ST0_REGNUM)
  165.     {
  166.       struct fpreg fpregs;
  167. #ifdef HAVE_PT_GETXMMREGS
  168.       char xmmregs[512];

  169.       if (have_ptrace_xmmregs != 0
  170.           && ptrace(PT_GETXMMREGS, ptid_get_pid (inferior_ptid),
  171.                     (PTRACE_TYPE_ARG3) xmmregs, 0) == 0)
  172.         {
  173.           have_ptrace_xmmregs = 1;

  174.           i387_collect_fxsave (regcache, regnum, xmmregs);

  175.           if (ptrace (PT_SETXMMREGS, ptid_get_pid (inferior_ptid),
  176.                       (PTRACE_TYPE_ARG3) xmmregs, 0) == -1)
  177.             perror_with_name (_("Couldn't write XMM registers"));
  178.         }
  179.       else
  180.         {
  181.           have_ptrace_xmmregs = 0;
  182. #endif
  183.           if (ptrace (PT_GETFPREGS, ptid_get_pid (inferior_ptid),
  184.                       (PTRACE_TYPE_ARG3) &fpregs, 0) == -1)
  185.             perror_with_name (_("Couldn't get floating point status"));

  186.           i387_collect_fsave (regcache, regnum, &fpregs);

  187.           if (ptrace (PT_SETFPREGS, ptid_get_pid (inferior_ptid),
  188.                       (PTRACE_TYPE_ARG3) &fpregs, 0) == -1)
  189.             perror_with_name (_("Couldn't write floating point status"));
  190. #ifdef HAVE_PT_GETXMMREGS
  191.         }
  192. #endif
  193.     }
  194. }

  195. /* Create a prototype *BSD/i386 target.  The client can override it
  196.    with local methods.  */

  197. struct target_ops *
  198. i386bsd_target (void)
  199. {
  200.   struct target_ops *t;

  201.   t = inf_ptrace_target ();
  202.   t->to_fetch_registers = i386bsd_fetch_inferior_registers;
  203.   t->to_store_registers = i386bsd_store_inferior_registers;
  204.   return t;
  205. }


  206. /* Support for debug registers.  */

  207. #ifdef HAVE_PT_GETDBREGS

  208. /* Not all versions of FreeBSD/i386 that support the debug registers
  209.    have this macro.  */
  210. #ifndef DBREG_DRX
  211. #define DBREG_DRX(d, x) ((&d->dr0)[x])
  212. #endif

  213. static unsigned long
  214. i386bsd_dr_get (ptid_t ptid, int regnum)
  215. {
  216.   struct dbreg dbregs;

  217.   if (ptrace (PT_GETDBREGS, ptid_get_pid (inferior_ptid),
  218.               (PTRACE_TYPE_ARG3) &dbregs, 0) == -1)
  219.     perror_with_name (_("Couldn't read debug registers"));

  220.   return DBREG_DRX ((&dbregs), regnum);
  221. }

  222. static void
  223. i386bsd_dr_set (int regnum, unsigned int value)
  224. {
  225.   struct dbreg dbregs;

  226.   if (ptrace (PT_GETDBREGS, ptid_get_pid (inferior_ptid),
  227.               (PTRACE_TYPE_ARG3) &dbregs, 0) == -1)
  228.     perror_with_name (_("Couldn't get debug registers"));

  229.   /* For some mysterious reason, some of the reserved bits in the
  230.      debug control register get set.  Mask these off, otherwise the
  231.      ptrace call below will fail.  */
  232.   DBREG_DRX ((&dbregs), 7) &= ~(0x0000fc00);

  233.   DBREG_DRX ((&dbregs), regnum) = value;

  234.   if (ptrace (PT_SETDBREGS, ptid_get_pid (inferior_ptid),
  235.               (PTRACE_TYPE_ARG3) &dbregs, 0) == -1)
  236.     perror_with_name (_("Couldn't write debug registers"));
  237. }

  238. void
  239. i386bsd_dr_set_control (unsigned long control)
  240. {
  241.   i386bsd_dr_set (7, control);
  242. }

  243. void
  244. i386bsd_dr_set_addr (int regnum, CORE_ADDR addr)
  245. {
  246.   gdb_assert (regnum >= 0 && regnum <= 4);

  247.   i386bsd_dr_set (regnum, addr);
  248. }

  249. CORE_ADDR
  250. i386bsd_dr_get_addr (int regnum)
  251. {
  252.   return i386bsd_dr_get (inferior_ptid, regnum);
  253. }

  254. unsigned long
  255. i386bsd_dr_get_status (void)
  256. {
  257.   return i386bsd_dr_get (inferior_ptid, 6);
  258. }

  259. unsigned long
  260. i386bsd_dr_get_control (void)
  261. {
  262.   return i386bsd_dr_get (inferior_ptid, 7);
  263. }

  264. #endif /* PT_GETDBREGS */


  265. /* Provide a prototype to silence -Wmissing-prototypes.  */
  266. void _initialize_i386bsd_nat (void);

  267. void
  268. _initialize_i386bsd_nat (void)
  269. {
  270.   int offset;

  271.   /* To support the recognition of signal handlers, i386bsd-tdep.c
  272.      hardcodes some constants.  Inclusion of this file means that we
  273.      are compiling a native debugger, which means that we can use the
  274.      system header files and sysctl(3) to get at the relevant
  275.      information.  */

  276. #if defined (__FreeBSD_version) && __FreeBSD_version >= 400011
  277. #define SC_REG_OFFSET i386fbsd4_sc_reg_offset
  278. #elif defined (__FreeBSD_version) && __FreeBSD_version >= 300005
  279. #define SC_REG_OFFSET i386fbsd_sc_reg_offset
  280. #elif defined (NetBSD) || defined (__NetBSD_Version__)
  281. #define SC_REG_OFFSET i386nbsd_sc_reg_offset
  282. #elif defined (OpenBSD)
  283. #define SC_REG_OFFSET i386obsd_sc_reg_offset
  284. #endif

  285. #ifdef SC_REG_OFFSET

  286.   /* We only check the program counter, stack pointer and frame
  287.      pointer since these members of `struct sigcontext' are essential
  288.      for providing backtraces.  More checks could be added, but would
  289.      involve adding configure checks for the appropriate structure
  290.      members, since older BSD's don't provide all of them.  */

  291. #define SC_PC_OFFSET SC_REG_OFFSET[I386_EIP_REGNUM]
  292. #define SC_SP_OFFSET SC_REG_OFFSET[I386_ESP_REGNUM]
  293. #define SC_FP_OFFSET SC_REG_OFFSET[I386_EBP_REGNUM]

  294.   /* Override the default value for the offset of the program counter
  295.      in the sigcontext structure.  */
  296.   offset = offsetof (struct sigcontext, sc_pc);

  297.   if (SC_PC_OFFSET != offset)
  298.     {
  299.       warning (_("\
  300. offsetof (struct sigcontext, sc_pc) yields %d instead of %d.\n\
  301. Please report this to <bug-gdb@gnu.org>."),
  302.                offset, SC_PC_OFFSET);
  303.     }

  304.   SC_PC_OFFSET = offset;

  305.   /* Likewise for the stack pointer.  */
  306.   offset = offsetof (struct sigcontext, sc_sp);

  307.   if (SC_SP_OFFSET != offset)
  308.     {
  309.       warning (_("\
  310. offsetof (struct sigcontext, sc_sp) yields %d instead of %d.\n\
  311. Please report this to <bug-gdb@gnu.org>."),
  312.                offset, SC_SP_OFFSET);
  313.     }

  314.   SC_SP_OFFSET = offset;

  315.   /* And the frame pointer.  */
  316.   offset = offsetof (struct sigcontext, sc_fp);

  317.   if (SC_FP_OFFSET != offset)
  318.     {
  319.       warning (_("\
  320. offsetof (struct sigcontext, sc_fp) yields %d instead of %d.\n\
  321. Please report this to <bug-gdb@gnu.org>."),
  322.                offset, SC_FP_OFFSET);
  323.     }

  324.   SC_FP_OFFSET = offset;

  325. #endif /* SC_REG_OFFSET */
  326. }