xref: /netbsd-src/external/gpl3/gdb.old/dist/gdb/vax-bsd-nat.c (revision 7f2ac4106a41b75cd7b67d794305c00f598853fb)
1 /* Native-dependent code for modern VAX BSD's.
2 
3    Copyright (C) 2004-2019 Free Software Foundation, Inc.
4 
5    This file is part of GDB.
6 
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 3 of the License, or
10    (at your option) any later version.
11 
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16 
17    You should have received a copy of the GNU General Public License
18    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
19 
20 #ifndef _KERNTYPES
21 #define _KERNTYPES
22 #endif
23 #include "defs.h"
24 #include "inferior.h"
25 #include "regcache.h"
26 #include "target.h"
27 
28 #include <sys/types.h>
29 #include <sys/ptrace.h>
30 #include <machine/reg.h>
31 
32 #include "vax-tdep.h"
33 #include "inf-ptrace.h"
34 
35 #ifdef __NetBSD__
36 #include "nbsd-nat.h"
37 struct vax_bsd_nat_target final : public nbsd_nat_target
38 #else
39 struct vax_bsd_nat_target final : public inf_ptrace_target
40 #endif
41 
42 {
43   void fetch_registers (struct regcache *, int) override;
44   void store_registers (struct regcache *, int) override;
45 };
46 
47 static vax_bsd_nat_target the_vax_bsd_nat_target;
48 
49 /* Supply the general-purpose registers stored in GREGS to REGCACHE.  */
50 
51 static void
52 vaxbsd_supply_gregset (struct regcache *regcache, const void *gregs)
53 {
54   const gdb_byte *regs = (const gdb_byte *)gregs;
55   int regnum;
56 
57   for (regnum = 0; regnum < VAX_NUM_REGS; regnum++)
58     regcache->raw_supply (regnum, regs + regnum * 4);
59 }
60 
61 /* Collect the general-purpose registers from REGCACHE and store them
62    in GREGS.  */
63 
64 static void
65 vaxbsd_collect_gregset (const struct regcache *regcache,
66 			void *gregs, int regnum)
67 {
68   gdb_byte *regs = (gdb_byte *)gregs;
69   int i;
70 
71   for (i = 0; i <= VAX_NUM_REGS; i++)
72     {
73       if (regnum == -1 || regnum == i)
74 	regcache->raw_collect (i, regs + i * 4);
75     }
76 }
77 
78 
79 /* Fetch register REGNUM from the inferior.  If REGNUM is -1, do this
80    for all registers.  */
81 
82 void
83 vax_bsd_nat_target::fetch_registers (struct regcache *regcache, int regnum)
84 {
85   struct reg regs;
86   ptid_t ptid = regcache->ptid ();
87   pid_t pid = ptid.pid ();
88   int lwp = ptid.lwp ();
89 
90   if (ptrace (PT_GETREGS, pid, (PTRACE_TYPE_ARG3) &regs,  lwp) == -1)
91     perror_with_name (_("Couldn't get registers"));
92 
93   vaxbsd_supply_gregset (regcache, &regs);
94 }
95 
96 /* Store register REGNUM back into the inferior.  If REGNUM is -1, do
97    this for all registers.  */
98 
99 void
100 vax_bsd_nat_target::store_registers (struct regcache *regcache, int regnum)
101 {
102   struct reg regs;
103   ptid_t ptid = regcache->ptid ();
104   pid_t pid = ptid.pid ();
105   int lwp = ptid.lwp ();
106 
107   if (ptrace (PT_GETREGS, pid, (PTRACE_TYPE_ARG3) &regs, lwp) == -1)
108     perror_with_name (_("Couldn't get registers"));
109 
110   vaxbsd_collect_gregset (regcache, &regs, regnum);
111 
112   if (ptrace (PT_SETREGS, pid, (PTRACE_TYPE_ARG3) &regs,  lwp) == -1)
113     perror_with_name (_("Couldn't write registers"));
114 }
115 
116 
117 /* Support for debugging kernel virtual memory images.  */
118 
119 #include <machine/pcb.h>
120 
121 #include "bsd-kvm.h"
122 
123 static int
124 vaxbsd_supply_pcb (struct regcache *regcache, struct pcb *pcb)
125 {
126   int regnum;
127 
128   /* The following is true for OpenBSD 3.5:
129 
130      The pcb contains the register state at the context switch inside
131      cpu_switch().  */
132 
133   /* The stack pointer shouldn't be zero.  */
134   if (pcb->KSP == 0)
135     return 0;
136 
137   for (regnum = VAX_R0_REGNUM; regnum < VAX_AP_REGNUM; regnum++)
138     regcache->raw_supply (regnum, &pcb->R[regnum - VAX_R0_REGNUM]);
139   regcache->raw_supply (VAX_AP_REGNUM, &pcb->AP);
140   regcache->raw_supply (VAX_FP_REGNUM, &pcb->FP);
141   regcache->raw_supply (VAX_SP_REGNUM, &pcb->KSP);
142   regcache->raw_supply (VAX_PC_REGNUM, &pcb->PC);
143   regcache->raw_supply (VAX_PS_REGNUM, &pcb->PSL);
144 
145   return 1;
146 }
147 
148 void
149 _initialize_vaxbsd_nat (void)
150 {
151   add_inf_child_target (&the_vax_bsd_nat_target);
152 
153   /* Support debugging kernel virtual memory images.  */
154   bsd_kvm_add_target (vaxbsd_supply_pcb);
155 }
156