GDB (API)
/home/stan/gdb/src/gdb/m68kbsd-nat.c
Go to the documentation of this file.
00001 /* Native-dependent code for Motorola 68000 BSD's.
00002 
00003    Copyright (C) 2004-2013 Free Software Foundation, Inc.
00004 
00005    This file is part of GDB.
00006 
00007    This program is free software; you can redistribute it and/or modify
00008    it under the terms of the GNU General Public License as published by
00009    the Free Software Foundation; either version 3 of the License, or
00010    (at your option) any later version.
00011 
00012    This program is distributed in the hope that it will be useful,
00013    but WITHOUT ANY WARRANTY; without even the implied warranty of
00014    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015    GNU General Public License for more details.
00016 
00017    You should have received a copy of the GNU General Public License
00018    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
00019 
00020 #include "defs.h"
00021 #include "gdbcore.h"
00022 #include "inferior.h"
00023 #include "regcache.h"
00024 
00025 #include "gdb_assert.h"
00026 #include <sys/types.h>
00027 #include <sys/ptrace.h>
00028 #include <machine/reg.h>
00029 
00030 #include "m68k-tdep.h"
00031 #include "inf-ptrace.h"
00032 
00033 static int
00034 m68kbsd_gregset_supplies_p (int regnum)
00035 {
00036   return (regnum >= M68K_D0_REGNUM && regnum <= M68K_PC_REGNUM);
00037 }
00038 
00039 static int
00040 m68kbsd_fpregset_supplies_p (int regnum)
00041 {
00042   return (regnum >= M68K_FP0_REGNUM && regnum <= M68K_FPI_REGNUM);
00043 }
00044 
00045 /* Supply the general-purpose registers stored in GREGS to REGCACHE.  */
00046 
00047 static void
00048 m68kbsd_supply_gregset (struct regcache *regcache, const void *gregs)
00049 {
00050   const char *regs = gregs;
00051   int regnum;
00052 
00053   for (regnum = M68K_D0_REGNUM; regnum <= M68K_PC_REGNUM; regnum++)
00054     regcache_raw_supply (regcache, regnum, regs + regnum * 4);
00055 }
00056 
00057 /* Supply the floating-point registers stored in FPREGS to REGCACHE.  */
00058 
00059 static void
00060 m68kbsd_supply_fpregset (struct regcache *regcache, const void *fpregs)
00061 {
00062   struct gdbarch *gdbarch = get_regcache_arch (regcache);
00063   const char *regs = fpregs;
00064   int regnum;
00065 
00066   for (regnum = M68K_FP0_REGNUM; regnum <= M68K_FPI_REGNUM; regnum++)
00067     regcache_raw_supply (regcache, regnum,
00068                          regs + m68kbsd_fpreg_offset (gdbarch, regnum));
00069 }
00070 
00071 /* Collect the general-purpose registers from REGCACHE and store them
00072    in GREGS.  */
00073 
00074 static void
00075 m68kbsd_collect_gregset (const struct regcache *regcache,
00076                          void *gregs, int regnum)
00077 {
00078   char *regs = gregs;
00079   int i;
00080 
00081   for (i = M68K_D0_REGNUM; i <= M68K_PC_REGNUM; i++)
00082     {
00083       if (regnum == -1 || regnum == i)
00084         regcache_raw_collect (regcache, i, regs + i * 4);
00085     }
00086 }
00087 
00088 /* Collect the floating-point registers from REGCACHE and store them
00089    in FPREGS.  */
00090 
00091 static void
00092 m68kbsd_collect_fpregset (struct regcache *regcache,
00093                           void *fpregs, int regnum)
00094 {
00095   struct gdbarch *gdbarch = get_regcache_arch (regcache);
00096   char *regs = fpregs;
00097   int i;
00098 
00099   for (i = M68K_FP0_REGNUM; i <= M68K_FPI_REGNUM; i++)
00100     {
00101       if (regnum == -1 || regnum == i)
00102         regcache_raw_collect (regcache, i,
00103                               regs + m68kbsd_fpreg_offset (gdbarch, i));
00104     }
00105 }
00106 
00107 
00108 /* Fetch register REGNUM from the inferior.  If REGNUM is -1, do this
00109    for all registers (including the floating-point registers).  */
00110 
00111 static void
00112 m68kbsd_fetch_inferior_registers (struct target_ops *ops,
00113                                   struct regcache *regcache, int regnum)
00114 {
00115   if (regnum == -1 || m68kbsd_gregset_supplies_p (regnum))
00116     {
00117       struct reg regs;
00118 
00119       if (ptrace (PT_GETREGS, ptid_get_pid (inferior_ptid),
00120                   (PTRACE_TYPE_ARG3) &regs, 0) == -1)
00121         perror_with_name (_("Couldn't get registers"));
00122 
00123       m68kbsd_supply_gregset (regcache, &regs);
00124     }
00125 
00126   if (regnum == -1 || m68kbsd_fpregset_supplies_p (regnum))
00127     {
00128       struct fpreg fpregs;
00129 
00130       if (ptrace (PT_GETFPREGS, ptid_get_pid (inferior_ptid),
00131                   (PTRACE_TYPE_ARG3) &fpregs, 0) == -1)
00132         perror_with_name (_("Couldn't get floating point status"));
00133 
00134       m68kbsd_supply_fpregset (regcache, &fpregs);
00135     }
00136 }
00137 
00138 /* Store register REGNUM back into the inferior.  If REGNUM is -1, do
00139    this for all registers (including the floating-point registers).  */
00140 
00141 static void
00142 m68kbsd_store_inferior_registers (struct target_ops *ops,
00143                                   struct regcache *regcache, int regnum)
00144 {
00145   if (regnum == -1 || m68kbsd_gregset_supplies_p (regnum))
00146     {
00147       struct reg regs;
00148 
00149       if (ptrace (PT_GETREGS, ptid_get_pid (inferior_ptid),
00150                   (PTRACE_TYPE_ARG3) &regs, 0) == -1)
00151         perror_with_name (_("Couldn't get registers"));
00152 
00153       m68kbsd_collect_gregset (regcache, &regs, regnum);
00154 
00155       if (ptrace (PT_SETREGS, ptid_get_pid (inferior_ptid),
00156                   (PTRACE_TYPE_ARG3) &regs, 0) == -1)
00157         perror_with_name (_("Couldn't write registers"));
00158     }
00159 
00160   if (regnum == -1 || m68kbsd_fpregset_supplies_p (regnum))
00161     {
00162       struct fpreg fpregs;
00163 
00164       if (ptrace (PT_GETFPREGS, ptid_get_pid (inferior_ptid),
00165                   (PTRACE_TYPE_ARG3) &fpregs, 0) == -1)
00166         perror_with_name (_("Couldn't get floating point status"));
00167 
00168       m68kbsd_collect_fpregset (regcache, &fpregs, regnum);
00169 
00170       if (ptrace (PT_SETFPREGS, ptid_get_pid (inferior_ptid),
00171                   (PTRACE_TYPE_ARG3) &fpregs, 0) == -1)
00172         perror_with_name (_("Couldn't write floating point status"));
00173     }
00174 }
00175 
00176 
00177 /* Support for debugging kernel virtual memory images.  */
00178 
00179 #include <sys/types.h>
00180 #include <machine/pcb.h>
00181 
00182 #include "bsd-kvm.h"
00183 
00184 /* OpenBSD doesn't have these.  */
00185 #ifndef PCB_REGS_FP
00186 #define PCB_REGS_FP 10
00187 #endif
00188 #ifndef PCB_REGS_SP
00189 #define PCB_REGS_SP 11
00190 #endif
00191 
00192 static int
00193 m68kbsd_supply_pcb (struct regcache *regcache, struct pcb *pcb)
00194 {
00195   int regnum, tmp;
00196   int i = 0;
00197 
00198   /* The following is true for NetBSD 1.6.2:
00199 
00200      The pcb contains %d2...%d7, %a2...%a7 and %ps.  This accounts for
00201      all callee-saved registers.  From this information we reconstruct
00202      the register state as it would look when we just returned from
00203      cpu_switch().  */
00204 
00205   /* The stack pointer shouldn't be zero.  */
00206   if (pcb->pcb_regs[PCB_REGS_SP] == 0)
00207     return 0;
00208 
00209   for (regnum = M68K_D2_REGNUM; regnum <= M68K_D7_REGNUM; regnum++)
00210     regcache_raw_supply (regcache, regnum, &pcb->pcb_regs[i++]);
00211   for (regnum = M68K_A2_REGNUM; regnum <= M68K_SP_REGNUM; regnum++)
00212     regcache_raw_supply (regcache, regnum, &pcb->pcb_regs[i++]);
00213 
00214   tmp = pcb->pcb_ps & 0xffff;
00215   regcache_raw_supply (regcache, M68K_PS_REGNUM, &tmp);
00216 
00217   read_memory (pcb->pcb_regs[PCB_REGS_FP] + 4, (char *) &tmp, sizeof tmp);
00218   regcache_raw_supply (regcache, M68K_PC_REGNUM, &tmp);
00219 
00220   return 1;
00221 }
00222 
00223 
00224 /* Provide a prototype to silence -Wmissing-prototypes.  */
00225 void _initialize_m68kbsd_nat (void);
00226 
00227 void
00228 _initialize_m68kbsd_nat (void)
00229 {
00230   struct target_ops *t;
00231 
00232   t = inf_ptrace_target ();
00233   t->to_fetch_registers = m68kbsd_fetch_inferior_registers;
00234   t->to_store_registers = m68kbsd_store_inferior_registers;
00235   add_target (t);
00236 
00237   /* Support debugging kernel virtual memory images.  */
00238   bsd_kvm_add_target (m68kbsd_supply_pcb);
00239 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Defines