Commit | Line | Data |
---|---|---|
2268d619 | 1 | /* Low level DECstation interface to ptrace, for GDB when running native. |
a70dc898 RP |
2 | Copyright 1988, 1989, 1991, 1992 Free Software Foundation, Inc. |
3 | Contributed by Alessandro Forin(af@cs.cmu.edu) at CMU | |
4 | and by Per Bothner(bothner@cs.wisc.edu) at U.Wisconsin. | |
5 | ||
6 | This file is part of GDB. | |
7 | ||
8 | This program is free software; you can redistribute it and/or modify | |
9 | it under the terms of the GNU General Public License as published by | |
10 | the Free Software Foundation; either version 2 of the License, or | |
11 | (at your option) any later version. | |
12 | ||
13 | This program is distributed in the hope that it will be useful, | |
14 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
16 | GNU General Public License for more details. | |
17 | ||
18 | You should have received a copy of the GNU General Public License | |
19 | along with this program; if not, write to the Free Software | |
20 | Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */ | |
21 | ||
22 | #include "defs.h" | |
23 | #include "inferior.h" | |
24 | #include "gdbcore.h" | |
2268d619 JG |
25 | #include <sys/ptrace.h> |
26 | #include <setjmp.h> /* For JB_XXX. */ | |
a70dc898 | 27 | |
56fc16c7 SG |
28 | #include <sys/types.h> |
29 | #include <sys/param.h> | |
30 | #include "gdbcore.h" | |
31 | ||
32 | /* These are needed on various systems to expand REGISTER_U_ADDR. */ | |
33 | #include <sys/dir.h> | |
34 | #include <sys/file.h> | |
35 | #include <sys/stat.h> | |
36 | #include <sys/user.h> | |
37 | #include <sys/ptrace.h> | |
38 | ||
2268d619 | 39 | /* Size of elements in jmpbuf */ |
a70dc898 | 40 | |
2268d619 | 41 | #define JB_ELEMENT_SIZE 4 |
a70dc898 RP |
42 | |
43 | /* Map gdb internal register number to ptrace ``address''. | |
44 | These ``addresses'' are defined in DECstation <sys/ptrace.h> */ | |
45 | ||
46 | #define REGISTER_PTRACE_ADDR(regno) \ | |
47 | (regno < 32 ? GPR_BASE + regno \ | |
48 | : regno == PC_REGNUM ? PC \ | |
49 | : regno == CAUSE_REGNUM ? CAUSE \ | |
50 | : regno == HI_REGNUM ? MMHI \ | |
51 | : regno == LO_REGNUM ? MMLO \ | |
52 | : regno == FCRCS_REGNUM ? FPC_CSR \ | |
53 | : regno == FCRIR_REGNUM ? FPC_EIR \ | |
54 | : regno >= FP0_REGNUM ? FPR_BASE + (regno - FP0_REGNUM) \ | |
55 | : 0) | |
56 | ||
57 | static const char zerobuf[MAX_REGISTER_RAW_SIZE]; | |
58 | ||
59 | /* Get all registers from the inferior */ | |
60 | ||
61 | void | |
62 | fetch_inferior_registers (regno) | |
63 | int regno; | |
64 | { | |
65 | register unsigned int regaddr; | |
66 | char buf[MAX_REGISTER_RAW_SIZE]; | |
67 | register int i; | |
68 | ||
69 | registers_fetched (); | |
70 | ||
71 | for (regno = 1; regno < NUM_REGS; regno++) | |
72 | { | |
73 | regaddr = REGISTER_PTRACE_ADDR (regno); | |
74 | for (i = 0; i < REGISTER_RAW_SIZE (regno); i += sizeof (int)) | |
75 | { | |
76 | *(int *) &buf[i] = ptrace (PT_READ_U, inferior_pid, | |
77 | (PTRACE_ARG3_TYPE) regaddr, 0); | |
78 | regaddr += sizeof (int); | |
79 | } | |
80 | supply_register (regno, buf); | |
81 | } | |
82 | ||
83 | supply_register (ZERO_REGNUM, zerobuf); | |
84 | /* Frame ptr reg must appear to be 0; it is faked by stack handling code. */ | |
85 | supply_register (FP_REGNUM, zerobuf); | |
86 | } | |
87 | ||
88 | /* Store our register values back into the inferior. | |
89 | If REGNO is -1, do this for all registers. | |
90 | Otherwise, REGNO specifies which register (so we can save time). */ | |
91 | ||
92 | void | |
93 | store_inferior_registers (regno) | |
94 | int regno; | |
95 | { | |
96 | register unsigned int regaddr; | |
97 | char buf[80]; | |
98 | ||
99 | if (regno == 0) | |
100 | return; | |
101 | ||
102 | if (regno > 0) | |
103 | { | |
104 | regaddr = REGISTER_PTRACE_ADDR (regno); | |
105 | errno = 0; | |
106 | ptrace (PT_WRITE_U, inferior_pid, (PTRACE_ARG3_TYPE) regaddr, | |
107 | read_register (regno)); | |
108 | if (errno != 0) | |
109 | { | |
110 | sprintf (buf, "writing register number %d", regno); | |
111 | perror_with_name (buf); | |
112 | } | |
113 | } | |
114 | else | |
115 | { | |
116 | for (regno = 0; regno < NUM_REGS; regno++) | |
117 | { | |
118 | if (regno == ZERO_REGNUM || regno == PS_REGNUM | |
119 | || regno == BADVADDR_REGNUM || regno == CAUSE_REGNUM | |
120 | || regno == FCRIR_REGNUM || regno == FP_REGNUM | |
121 | || (regno >= FIRST_EMBED_REGNUM && regno <= LAST_EMBED_REGNUM)) | |
122 | continue; | |
123 | regaddr = register_addr (regno, 1); | |
124 | errno = 0; | |
125 | ptrace (6, inferior_pid, (PTRACE_ARG3_TYPE) regaddr, | |
126 | read_register (regno)); | |
127 | if (errno != 0) | |
128 | { | |
129 | sprintf (buf, "writing all regs, number %d", regno); | |
130 | perror_with_name (buf); | |
131 | } | |
132 | } | |
133 | } | |
134 | } | |
135 | ||
a70dc898 | 136 | |
2268d619 JG |
137 | /* Figure out where the longjmp will land. |
138 | We expect the first arg to be a pointer to the jmp_buf structure from which | |
139 | we extract the pc (JB_PC) that we will land at. The pc is copied into PC. | |
140 | This routine returns true on success. */ | |
141 | ||
142 | int | |
143 | get_longjmp_target(pc) | |
144 | CORE_ADDR *pc; | |
145 | { | |
146 | CORE_ADDR jb_addr; | |
147 | ||
148 | jb_addr = read_register(A0_REGNUM); | |
a70dc898 | 149 | |
2268d619 JG |
150 | if (target_read_memory(jb_addr + JB_PC * JB_ELEMENT_SIZE, pc, |
151 | sizeof(CORE_ADDR))) | |
152 | return 0; | |
153 | ||
154 | SWAP_TARGET_AND_HOST(pc, sizeof(CORE_ADDR)); | |
155 | ||
156 | return 1; | |
157 | } | |
56fc16c7 SG |
158 | |
159 | /* Extract the register values out of the core file and store | |
160 | them where `read_register' will find them. | |
161 | ||
162 | CORE_REG_SECT points to the register values themselves, read into memory. | |
163 | CORE_REG_SIZE is the size of that area. | |
164 | WHICH says which set of registers we are handling (0 = int, 2 = float | |
165 | on machines where they are discontiguous). | |
166 | REG_ADDR is the offset from u.u_ar0 to the register values relative to | |
167 | core_reg_sect. This is used with old-fashioned core files to | |
168 | locate the registers in a large upage-plus-stack ".reg" section. | |
169 | Original upage address X is at location core_reg_sect+x+reg_addr. | |
170 | */ | |
171 | ||
172 | void | |
173 | fetch_core_registers (core_reg_sect, core_reg_size, which, reg_addr) | |
174 | char *core_reg_sect; | |
175 | unsigned core_reg_size; | |
176 | int which; | |
177 | unsigned reg_addr; | |
178 | { | |
179 | register int regno; | |
180 | register unsigned int addr; | |
181 | int bad_reg = -1; | |
182 | register reg_ptr = -reg_addr; /* Original u.u_ar0 is -reg_addr. */ | |
183 | ||
184 | /* If u.u_ar0 was an absolute address in the core file, relativize it now, | |
185 | so we can use it as an offset into core_reg_sect. When we're done, | |
186 | "register 0" will be at core_reg_sect+reg_ptr, and we can use | |
187 | register_addr to offset to the other registers. If this is a modern | |
188 | core file without a upage, reg_ptr will be zero and this is all a big | |
189 | NOP. */ | |
190 | if (reg_ptr > core_reg_size) | |
191 | reg_ptr -= KERNEL_U_ADDR; | |
192 | ||
193 | for (regno = 0; regno < NUM_REGS; regno++) | |
194 | { | |
195 | addr = register_addr (regno, reg_ptr); | |
196 | if (addr >= core_reg_size) { | |
197 | if (bad_reg < 0) | |
198 | bad_reg = regno; | |
199 | } else { | |
200 | supply_register (regno, core_reg_sect + addr); | |
201 | } | |
202 | } | |
203 | if (bad_reg >= 0) | |
204 | { | |
205 | error ("Register %s not found in core file.", reg_names[bad_reg]); | |
206 | } | |
207 | supply_register (ZERO_REGNUM, zerobuf); | |
208 | /* Frame ptr reg must appear to be 0; it is faked by stack handling code. */ | |
209 | supply_register (FP_REGNUM, zerobuf); | |
210 | } | |
211 | ||
212 | /* Return the address in the core dump or inferior of register REGNO. | |
213 | BLOCKEND is the address of the end of the user structure. */ | |
214 | ||
215 | unsigned int | |
216 | register_addr (regno, blockend) | |
217 | int regno; | |
218 | int blockend; | |
219 | { | |
220 | int addr; | |
221 | ||
222 | if (regno < 0 || regno >= NUM_REGS) | |
223 | error ("Invalid register number %d.", regno); | |
224 | ||
225 | REGISTER_U_ADDR (addr, blockend, regno); | |
226 | ||
227 | return addr; | |
228 | } |