Initial import from FreeBSD RELENG_4:
[dragonfly.git] / gnu / usr.bin / binutils / gdb / alpha / freebsd-nat.c
1 /* Native-dependent code for BSD Unix running on i386's, for GDB.
2    Copyright 1988, 1989, 1991, 1992, 1994, 1996 Free Software Foundation, Inc.
3
4 This file is part of GDB.
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
19
20 #include "defs.h"
21
22 #include <sys/types.h>
23 #include <sys/ptrace.h>
24 #include <machine/reg.h>
25 #include <machine/frame.h>
26 #include <sys/param.h>
27 #include <sys/user.h>
28 #include <string.h>
29 #include "gdbcore.h"
30 #include "value.h"
31 #include "inferior.h"
32
33 #if defined(HAVE_GREGSET_T)
34 #include <sys/procfs.h>
35 #endif
36
37 int kernel_debugging = 0;
38
39 /* Size of elements in jmpbuf */
40
41 #define JB_ELEMENT_SIZE 8
42
43 /* The definition for JB_PC in machine/reg.h is wrong.
44    And we can't get at the correct definition in setjmp.h as it is
45    not always available (eg. if _POSIX_SOURCE is defined which is the
46    default). As the defintion is unlikely to change (see comment
47    in <setjmp.h>, define the correct value here.  */
48
49 #undef JB_PC
50 #define JB_PC 2
51
52 /* Figure out where the longjmp will land.
53    We expect the first arg to be a pointer to the jmp_buf structure from which
54    we extract the pc (JB_PC) that we will land at.  The pc is copied into PC.
55    This routine returns true on success. */
56
57 int
58 get_longjmp_target (pc)
59      CORE_ADDR *pc;
60 {
61   CORE_ADDR jb_addr;
62   char raw_buffer[MAX_REGISTER_RAW_SIZE];
63
64   jb_addr = read_register(A0_REGNUM);
65
66   if (target_read_memory(jb_addr + JB_PC * JB_ELEMENT_SIZE, raw_buffer,
67                          sizeof(CORE_ADDR)))
68     return 0;
69
70   *pc = extract_address (raw_buffer, sizeof(CORE_ADDR));
71   return 1;
72 }
73
74 void
75 fetch_inferior_registers (regno)
76      int regno;
77 {
78   struct reg regs;      /* ptrace order, not gcc/gdb order */
79   struct fpreg fpregs;
80   int r;
81
82   ptrace (PT_GETREGS, inferior_pid, (PTRACE_ARG3_TYPE) &regs, 0);
83   ptrace (PT_GETFPREGS, inferior_pid, (PTRACE_ARG3_TYPE) &fpregs, 0);
84
85   for (r = 0; r < 31; r++)
86     memcpy (&registers[REGISTER_BYTE (r)],
87             &regs.r_regs[r], sizeof(u_int64_t));
88   for (r = 0; r < 32; r++)
89     memcpy (&registers[REGISTER_BYTE (r + FP0_REGNUM)],
90             &fpregs.fpr_regs[r], sizeof(u_int64_t));
91   memcpy (&registers[REGISTER_BYTE (PC_REGNUM)],
92           &regs.r_regs[31], sizeof(u_int64_t));
93
94   memset (&registers[REGISTER_BYTE (ZERO_REGNUM)], 0, sizeof(u_int64_t));
95   memset (&registers[REGISTER_BYTE (FP_REGNUM)], 0, sizeof(u_int64_t));
96
97   registers_fetched ();
98 }
99
100 void
101 store_inferior_registers (regno)
102      int regno;
103 {
104   struct reg regs;      /* ptrace order, not gcc/gdb order */
105   struct fpreg fpregs;
106   int r;
107
108   for (r = 0; r < 31; r++)
109     memcpy (&regs.r_regs[r],
110             &registers[REGISTER_BYTE (r)], sizeof(u_int64_t));
111   for (r = 0; r < 32; r++)
112     memcpy (&fpregs.fpr_regs[r],
113             &registers[REGISTER_BYTE (r + FP0_REGNUM)], sizeof(u_int64_t));
114   memcpy (&regs.r_regs[31],
115           &registers[REGISTER_BYTE (PC_REGNUM)], sizeof(u_int64_t));
116
117   ptrace (PT_SETREGS, inferior_pid, (PTRACE_ARG3_TYPE) &regs, 0);
118   ptrace (PT_SETFPREGS, inferior_pid, (PTRACE_ARG3_TYPE) &fpregs, 0);
119 }
120
121 #ifdef HAVE_GREGSET_T
122 void
123 supply_gregset (gp)
124   gregset_t *gp;
125 {
126   int regno = 0;
127
128   /* These must be ordered the same as REGISTER_NAMES in
129      config/alpha/tm-alpha.h. */
130   for (regno = 0; regno < 31; regno++)
131     supply_register (regno, (char *)&gp->r_regs[regno]);
132   supply_register (PC_REGNUM, (char *)&gp->r_regs[regno]);
133 }
134 #endif  /* HAVE_GREGSET_T */
135
136 #ifdef HAVE_FPREGSET_T
137 void
138 supply_fpregset (fp)
139   fpregset_t *fp;
140 {
141   int regno = 0;
142
143   for (regno = 0; regno < 32; regno++)
144     supply_register (regno + 32, (char *)&fp->fpr_regs[regno]);
145 }
146 #endif  /* HAVE_FPREGSET_T */
147
148 /*
149  * Get registers from a kernel crash dump or live kernel.
150  * Called by kvm-fbsd.c:get_kcore_registers().
151  */
152 fetch_kcore_registers (pcbp)
153   struct pcb *pcbp;
154 {
155
156   /* First clear out any garbage. */
157   memset(registers, '\0', REGISTER_BYTES);
158
159   /* SP */
160   *(long *) &registers[REGISTER_BYTE (SP_REGNUM)] =
161     pcbp->pcb_hw.apcb_ksp;
162
163   /* S0 through S6 */
164   memcpy (&registers[REGISTER_BYTE (S0_REGNUM)],
165           &pcbp->pcb_context[0], 7 * sizeof(long));
166
167   /* PC */
168   *(long *) &registers[REGISTER_BYTE (PC_REGNUM)] =
169     pcbp->pcb_context[7];
170
171   registers_fetched ();
172 }
173