gdb/ppcfbsd-nat.c - gdb

Functions defined

Source code

  1. /* Native-dependent code for PowerPC's running FreeBSD, for GDB.

  2.    Copyright (C) 2013-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/procfs.h>
  20. #include <sys/ptrace.h>
  21. #include <sys/signal.h>
  22. #include <machine/frame.h>
  23. #include <machine/pcb.h>
  24. #include <machine/reg.h>

  25. #include "fbsd-nat.h"
  26. #include "gregset.h"
  27. #include "ppc-tdep.h"
  28. #include "ppcfbsd-tdep.h"
  29. #include "inf-ptrace.h"
  30. #include "bsd-kvm.h"

  31. /* Fill GDB's register array with the general-purpose register values
  32.    in *GREGSETP.  */

  33. void
  34. supply_gregset (struct regcache *regcache, const gdb_gregset_t *gregsetp)
  35. {
  36.   const struct regset *regset = ppc_fbsd_gregset (sizeof (long));

  37.   ppc_supply_gregset (regset, regcache, -1, gregsetp, sizeof (*gregsetp));
  38. }

  39. /* Fill register REGNO (if a gpr) in *GREGSETP with the value in GDB's
  40.    register array. If REGNO is -1 do it for all registers.  */

  41. void
  42. fill_gregset (const struct regcache *regcache,
  43.               gdb_gregset_t *gregsetp, int regno)
  44. {
  45.   const struct regset *regset = ppc_fbsd_gregset (sizeof (long));

  46.   if (regno == -1)
  47.     memset (gregsetp, 0, sizeof (*gregsetp));
  48.   ppc_collect_gregset (regset, regcache, regno, gregsetp, sizeof (*gregsetp));
  49. }

  50. /* Fill GDB's register array with the floating-point register values
  51.    in *FPREGSETP.  */

  52. void
  53. supply_fpregset (struct regcache *regcache, const gdb_fpregset_t * fpregsetp)
  54. {
  55.   const struct regset *regset = ppc_fbsd_fpregset ();

  56.   ppc_supply_fpregset (regset, regcache, -1,
  57.                        fpregsetp, sizeof (*fpregsetp));
  58. }

  59. /* Fill register REGNO in *FGREGSETP with the value in GDB's
  60.    register array. If REGNO is -1 do it for all registers.  */

  61. void
  62. fill_fpregset (const struct regcache *regcache,
  63.                gdb_fpregset_t *fpregsetp, int regno)
  64. {
  65.   const struct regset *regset = ppc_fbsd_fpregset ();

  66.   ppc_collect_fpregset (regset, regcache, regno,
  67.                         fpregsetp, sizeof (*fpregsetp));
  68. }

  69. /* Returns true if PT_GETFPREGS fetches this register.  */

  70. static int
  71. getfpregs_supplies (struct gdbarch *gdbarch, int regno)
  72. {
  73.   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);

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

  80.          It's not clear to me how best to update this code, so this assert
  81.          will alert the first person to encounter the NetBSD/E500
  82.          combination to the problem.  */

  83.   gdb_assert (ppc_floating_point_unit_p (gdbarch));

  84.   return ((regno >= tdep->ppc_fp0_regnum
  85.            && regno < tdep->ppc_fp0_regnum + ppc_num_fprs)
  86.           || regno == tdep->ppc_fpscr_regnum);
  87. }

  88. /* Fetch register REGNO from the child process. If REGNO is -1, do it
  89.    for all registers.  */

  90. static void
  91. ppcfbsd_fetch_inferior_registers (struct target_ops *ops,
  92.                                   struct regcache *regcache, int regno)
  93. {
  94.   gdb_gregset_t regs;

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

  98.   supply_gregset (regcache, &regs);

  99.   if (regno == -1 || getfpregs_supplies (get_regcache_arch (regcache), regno))
  100.     {
  101.       const struct regset *fpregset = ppc_fbsd_fpregset ();
  102.       gdb_fpregset_t fpregs;

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

  106.       ppc_supply_fpregset (fpregset, regcache, regno, &fpregs, sizeof fpregs);
  107.     }
  108. }

  109. /* Store register REGNO back into the child process. If REGNO is -1,
  110.    do this for all registers.  */

  111. static void
  112. ppcfbsd_store_inferior_registers (struct target_ops *ops,
  113.                                   struct regcache *regcache, int regno)
  114. {
  115.   gdb_gregset_t regs;

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

  119.   fill_gregset (regcache, &regs, regno);

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

  123.   if (regno == -1 || getfpregs_supplies (get_regcache_arch (regcache), regno))
  124.     {
  125.       gdb_fpregset_t 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 FP registers"));

  129.       fill_fpregset (regcache, &fpregs, regno);

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

  135. /* Architecture specific function that reconstructs the
  136.    register state from PCB (Process Control Block) and supplies it
  137.    to REGCACHE.  */

  138. static int
  139. ppcfbsd_supply_pcb (struct regcache *regcache, struct pcb *pcb)
  140. {
  141.   struct gdbarch *gdbarch = get_regcache_arch (regcache);
  142.   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
  143.   int i, regnum;

  144.   /* The stack pointer shouldn't be zero.  */
  145.   if (pcb->pcb_sp == 0)
  146.     return 0;

  147.   regcache_raw_supply (regcache, gdbarch_sp_regnum (gdbarch), &pcb->pcb_sp);
  148.   regcache_raw_supply (regcache, tdep->ppc_cr_regnum, &pcb->pcb_cr);
  149.   regcache_raw_supply (regcache, tdep->ppc_lr_regnum, &pcb->pcb_lr);
  150.   for (i = 0, regnum = tdep->ppc_gp0_regnum + 14; i < 20; i++, regnum++)
  151.     regcache_raw_supply (regcache, regnum, &pcb->pcb_context[i]);

  152.   return 1;
  153. }

  154. /* Provide a prototype to silence -Wmissing-prototypes.  */

  155. void _initialize_ppcfbsd_nat (void);

  156. void
  157. _initialize_ppcfbsd_nat (void)
  158. {
  159.   struct target_ops *t;

  160.   /* Add in local overrides.  */
  161.   t = inf_ptrace_target ();
  162.   t->to_fetch_registers = ppcfbsd_fetch_inferior_registers;
  163.   t->to_store_registers = ppcfbsd_store_inferior_registers;
  164.   t->to_pid_to_exec_file = fbsd_pid_to_exec_file;
  165.   t->to_find_memory_regions = fbsd_find_memory_regions;
  166.   add_target (t);

  167.   /* Support debugging kernel virtual memory images.  */
  168.   bsd_kvm_add_target (ppcfbsd_supply_pcb);
  169. }