gdb/ppcnbsd-nat.c - gdb

Functions defined

Source code

  1. /* Native-dependent code for NetBSD/powerpc.

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

  3.    Contributed by Wasabi Systems, Inc.

  4.    This file is part of GDB.

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

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

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

  15. #include "defs.h"

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

  21. #include "gdbcore.h"
  22. #include "inferior.h"
  23. #include "regcache.h"

  24. #include "ppc-tdep.h"
  25. #include "ppcnbsd-tdep.h"
  26. #include "bsd-kvm.h"
  27. #include "inf-ptrace.h"

  28. /* Returns true if PT_GETREGS fetches this register.  */

  29. static int
  30. getregs_supplies (struct gdbarch *gdbarch, int regnum)
  31. {
  32.   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);

  33.   return ((regnum >= tdep->ppc_gp0_regnum
  34.            && regnum < tdep->ppc_gp0_regnum + ppc_num_gprs)
  35.           || regnum == tdep->ppc_lr_regnum
  36.           || regnum == tdep->ppc_cr_regnum
  37.           || regnum == tdep->ppc_xer_regnum
  38.           || regnum == tdep->ppc_ctr_regnum
  39.           || regnum == gdbarch_pc_regnum (gdbarch));
  40. }

  41. /* Like above, but for PT_GETFPREGS.  */

  42. static int
  43. getfpregs_supplies (struct gdbarch *gdbarch, int regnum)
  44. {
  45.   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);

  46.   /* FIXME: jimb/2004-05-05: Some PPC variants don't have floating
  47.      point registers.  Traditionally, GDB's register set has still
  48.      listed the floating point registers for such machines, so this
  49.      code is harmless.  However, the new E500 port actually omits the
  50.      floating point registers entirely from the register set --- they
  51.      don't even have register numbers assigned to them.

  52.      It's not clear to me how best to update this code, so this assert
  53.      will alert the first person to encounter the NetBSD/E500
  54.      combination to the problem.  */
  55.   gdb_assert (ppc_floating_point_unit_p (gdbarch));

  56.   return ((regnum >= tdep->ppc_fp0_regnum
  57.            && regnum < tdep->ppc_fp0_regnum + ppc_num_fprs)
  58.           || regnum == tdep->ppc_fpscr_regnum);
  59. }

  60. static void
  61. ppcnbsd_fetch_inferior_registers (struct target_ops *ops,
  62.                                   struct regcache *regcache, int regnum)
  63. {
  64.   struct gdbarch *gdbarch = get_regcache_arch (regcache);

  65.   if (regnum == -1 || getregs_supplies (gdbarch, regnum))
  66.     {
  67.       struct reg regs;

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

  71.       ppc_supply_gregset (&ppcnbsd_gregset, regcache,
  72.                           regnum, &regs, sizeof regs);
  73.     }

  74.   if (regnum == -1 || getfpregs_supplies (gdbarch, regnum))
  75.     {
  76.       struct fpreg fpregs;

  77.       if (ptrace (PT_GETFPREGS, ptid_get_pid (inferior_ptid),
  78.                   (PTRACE_TYPE_ARG3) &fpregs, 0) == -1)
  79.         perror_with_name (_("Couldn't get FP registers"));

  80.       ppc_supply_fpregset (&ppcnbsd_fpregset, regcache,
  81.                            regnum, &fpregs, sizeof fpregs);
  82.     }
  83. }

  84. static void
  85. ppcnbsd_store_inferior_registers (struct target_ops *ops,
  86.                                   struct regcache *regcache, int regnum)
  87. {
  88.   struct gdbarch *gdbarch = get_regcache_arch (regcache);

  89.   if (regnum == -1 || getregs_supplies (gdbarch, 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.       ppc_collect_gregset (&ppcnbsd_gregset, regcache,
  96.                            regnum, &regs, sizeof regs);

  97.       if (ptrace (PT_SETREGS, ptid_get_pid (inferior_ptid),
  98.                   (PTRACE_TYPE_ARG3) &regs, 0) == -1)
  99.         perror_with_name (_("Couldn't write registers"));
  100.     }

  101.   if (regnum == -1 || getfpregs_supplies (gdbarch, regnum))
  102.     {
  103.       struct fpreg fpregs;

  104.       if (ptrace (PT_GETFPREGS, ptid_get_pid (inferior_ptid),
  105.                   (PTRACE_TYPE_ARG3) &fpregs, 0) == -1)
  106.         perror_with_name (_("Couldn't get FP registers"));

  107.       ppc_collect_fpregset (&ppcnbsd_fpregset, regcache,
  108.                             regnum, &fpregs, sizeof fpregs);

  109.       if (ptrace (PT_SETFPREGS, ptid_get_pid (inferior_ptid),
  110.                   (PTRACE_TYPE_ARG3) &fpregs, 0) == -1)
  111.         perror_with_name (_("Couldn't set FP registers"));
  112.     }
  113. }

  114. static int
  115. ppcnbsd_supply_pcb (struct regcache *regcache, struct pcb *pcb)
  116. {
  117.   struct switchframe sf;
  118.   struct callframe cf;
  119.   struct gdbarch *gdbarch = get_regcache_arch (regcache);
  120.   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
  121.   int i;

  122.   /* The stack pointer shouldn't be zero.  */
  123.   if (pcb->pcb_sp == 0)
  124.     return 0;

  125.   read_memory (pcb->pcb_sp, (gdb_byte *)&sf, sizeof sf);
  126.   regcache_raw_supply (regcache, tdep->ppc_cr_regnum, &sf.cr);
  127.   regcache_raw_supply (regcache, tdep->ppc_gp0_regnum + 2, &sf.fixreg2);
  128.   for (i = 0 ; i < 19 ; i++)
  129.     regcache_raw_supply (regcache, tdep->ppc_gp0_regnum + 13 + i,
  130.                          &sf.fixreg[i]);

  131.   read_memory(sf.sp, (gdb_byte *)&cf, sizeof(cf));
  132.   regcache_raw_supply (regcache, tdep->ppc_gp0_regnum + 30, &cf.r30);
  133.   regcache_raw_supply (regcache, tdep->ppc_gp0_regnum + 31, &cf.r31);
  134.   regcache_raw_supply (regcache, tdep->ppc_gp0_regnum + 1, &cf.sp);

  135.   read_memory(cf.sp, (gdb_byte *)&cf, sizeof(cf));
  136.   regcache_raw_supply (regcache, tdep->ppc_lr_regnum, &cf.lr);
  137.   regcache_raw_supply (regcache, gdbarch_pc_regnum (gdbarch), &cf.lr);

  138.   return 1;
  139. }

  140. /* Provide a prototype to silence -Wmissing-prototypes.  */
  141. void _initialize_ppcnbsd_nat (void);

  142. void
  143. _initialize_ppcnbsd_nat (void)
  144. {
  145.   struct target_ops *t;

  146.   /* Support debugging kernel virtual memory images.  */
  147.   bsd_kvm_add_target (ppcnbsd_supply_pcb);

  148.   /* Add in local overrides.  */
  149.   t = inf_ptrace_target ();
  150.   t->to_fetch_registers = ppcnbsd_fetch_inferior_registers;
  151.   t->to_store_registers = ppcnbsd_store_inferior_registers;
  152.   add_target (t);
  153. }