gdb/m68kbsd-nat.c - gdb

Functions defined

Macros defined

Source code

  1. /* Native-dependent code for Motorola 68000 BSD's.

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

  18. #include <sys/types.h>
  19. #include <sys/ptrace.h>
  20. #include <machine/reg.h>

  21. #include "m68k-tdep.h"
  22. #include "inf-ptrace.h"

  23. static int
  24. m68kbsd_gregset_supplies_p (int regnum)
  25. {
  26.   return (regnum >= M68K_D0_REGNUM && regnum <= M68K_PC_REGNUM);
  27. }

  28. static int
  29. m68kbsd_fpregset_supplies_p (int regnum)
  30. {
  31.   return (regnum >= M68K_FP0_REGNUM && regnum <= M68K_FPI_REGNUM);
  32. }

  33. /* Supply the general-purpose registers stored in GREGS to REGCACHE.  */

  34. static void
  35. m68kbsd_supply_gregset (struct regcache *regcache, const void *gregs)
  36. {
  37.   const char *regs = gregs;
  38.   int regnum;

  39.   for (regnum = M68K_D0_REGNUM; regnum <= M68K_PC_REGNUM; regnum++)
  40.     regcache_raw_supply (regcache, regnum, regs + regnum * 4);
  41. }

  42. /* Supply the floating-point registers stored in FPREGS to REGCACHE.  */

  43. static void
  44. m68kbsd_supply_fpregset (struct regcache *regcache, const void *fpregs)
  45. {
  46.   struct gdbarch *gdbarch = get_regcache_arch (regcache);
  47.   const char *regs = fpregs;
  48.   int regnum;

  49.   for (regnum = M68K_FP0_REGNUM; regnum <= M68K_FPI_REGNUM; regnum++)
  50.     regcache_raw_supply (regcache, regnum,
  51.                          regs + m68kbsd_fpreg_offset (gdbarch, regnum));
  52. }

  53. /* Collect the general-purpose registers from REGCACHE and store them
  54.    in GREGS.  */

  55. static void
  56. m68kbsd_collect_gregset (const struct regcache *regcache,
  57.                          void *gregs, int regnum)
  58. {
  59.   char *regs = gregs;
  60.   int i;

  61.   for (i = M68K_D0_REGNUM; i <= M68K_PC_REGNUM; i++)
  62.     {
  63.       if (regnum == -1 || regnum == i)
  64.         regcache_raw_collect (regcache, i, regs + i * 4);
  65.     }
  66. }

  67. /* Collect the floating-point registers from REGCACHE and store them
  68.    in FPREGS.  */

  69. static void
  70. m68kbsd_collect_fpregset (struct regcache *regcache,
  71.                           void *fpregs, int regnum)
  72. {
  73.   struct gdbarch *gdbarch = get_regcache_arch (regcache);
  74.   char *regs = fpregs;
  75.   int i;

  76.   for (i = M68K_FP0_REGNUM; i <= M68K_FPI_REGNUM; i++)
  77.     {
  78.       if (regnum == -1 || regnum == i)
  79.         regcache_raw_collect (regcache, i,
  80.                               regs + m68kbsd_fpreg_offset (gdbarch, i));
  81.     }
  82. }


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

  85. static void
  86. m68kbsd_fetch_inferior_registers (struct target_ops *ops,
  87.                                   struct regcache *regcache, int regnum)
  88. {
  89.   if (regnum == -1 || m68kbsd_gregset_supplies_p (regnum))
  90.     {
  91.       struct reg regs;

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

  95.       m68kbsd_supply_gregset (regcache, &regs);
  96.     }

  97.   if (regnum == -1 || m68kbsd_fpregset_supplies_p (regnum))
  98.     {
  99.       struct fpreg fpregs;

  100.       if (ptrace (PT_GETFPREGS, ptid_get_pid (inferior_ptid),
  101.                   (PTRACE_TYPE_ARG3) &fpregs, 0) == -1)
  102.         perror_with_name (_("Couldn't get floating point status"));

  103.       m68kbsd_supply_fpregset (regcache, &fpregs);
  104.     }
  105. }

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

  108. static void
  109. m68kbsd_store_inferior_registers (struct target_ops *ops,
  110.                                   struct regcache *regcache, int regnum)
  111. {
  112.   if (regnum == -1 || m68kbsd_gregset_supplies_p (regnum))
  113.     {
  114.       struct reg regs;

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

  118.       m68kbsd_collect_gregset (regcache, &regs, regnum);

  119.       if (ptrace (PT_SETREGS, ptid_get_pid (inferior_ptid),
  120.                   (PTRACE_TYPE_ARG3) &regs, 0) == -1)
  121.         perror_with_name (_("Couldn't write registers"));
  122.     }

  123.   if (regnum == -1 || m68kbsd_fpregset_supplies_p (regnum))
  124.     {
  125.       struct fpreg fpregs;

  126.       if (ptrace (PT_GETFPREGS, ptid_get_pid (inferior_ptid),
  127.                   (PTRACE_TYPE_ARG3) &fpregs, 0) == -1)
  128.         perror_with_name (_("Couldn't get floating point status"));

  129.       m68kbsd_collect_fpregset (regcache, &fpregs, regnum);

  130.       if (ptrace (PT_SETFPREGS, ptid_get_pid (inferior_ptid),
  131.                   (PTRACE_TYPE_ARG3) &fpregs, 0) == -1)
  132.         perror_with_name (_("Couldn't write floating point status"));
  133.     }
  134. }


  135. /* Support for debugging kernel virtual memory images.  */

  136. #include <machine/pcb.h>

  137. #include "bsd-kvm.h"

  138. /* OpenBSD doesn't have these.  */
  139. #ifndef PCB_REGS_FP
  140. #define PCB_REGS_FP 10
  141. #endif
  142. #ifndef PCB_REGS_SP
  143. #define PCB_REGS_SP 11
  144. #endif

  145. static int
  146. m68kbsd_supply_pcb (struct regcache *regcache, struct pcb *pcb)
  147. {
  148.   int regnum, tmp;
  149.   int i = 0;

  150.   /* The following is true for NetBSD 1.6.2:

  151.      The pcb contains %d2...%d7, %a2...%a7 and %ps.  This accounts for
  152.      all callee-saved registers.  From this information we reconstruct
  153.      the register state as it would look when we just returned from
  154.      cpu_switch().  */

  155.   /* The stack pointer shouldn't be zero.  */
  156.   if (pcb->pcb_regs[PCB_REGS_SP] == 0)
  157.     return 0;

  158.   for (regnum = M68K_D2_REGNUM; regnum <= M68K_D7_REGNUM; regnum++)
  159.     regcache_raw_supply (regcache, regnum, &pcb->pcb_regs[i++]);
  160.   for (regnum = M68K_A2_REGNUM; regnum <= M68K_SP_REGNUM; regnum++)
  161.     regcache_raw_supply (regcache, regnum, &pcb->pcb_regs[i++]);

  162.   tmp = pcb->pcb_ps & 0xffff;
  163.   regcache_raw_supply (regcache, M68K_PS_REGNUM, &tmp);

  164.   read_memory (pcb->pcb_regs[PCB_REGS_FP] + 4, (char *) &tmp, sizeof tmp);
  165.   regcache_raw_supply (regcache, M68K_PC_REGNUM, &tmp);

  166.   return 1;
  167. }


  168. /* Provide a prototype to silence -Wmissing-prototypes.  */
  169. void _initialize_m68kbsd_nat (void);

  170. void
  171. _initialize_m68kbsd_nat (void)
  172. {
  173.   struct target_ops *t;

  174.   t = inf_ptrace_target ();
  175.   t->to_fetch_registers = m68kbsd_fetch_inferior_registers;
  176.   t->to_store_registers = m68kbsd_store_inferior_registers;
  177.   add_target (t);

  178.   /* Support debugging kernel virtual memory images.  */
  179.   bsd_kvm_add_target (m68kbsd_supply_pcb);
  180. }