* defs.h: Incorporate param.h. All users changed.
[binutils-gdb.git] / gdb / pyr-xdep.c
1 /* Low level Pyramid interface to ptrace, for GDB when running under Unix.
2 Copyright (C) 1988, 1989, 1991 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., 675 Mass Ave, Cambridge, MA 02139, USA. */
19
20 #include <stdio.h>
21 #include "defs.h"
22 #include "frame.h"
23 #include "inferior.h"
24
25 #include <sys/param.h>
26 #include <sys/dir.h>
27 #include <signal.h>
28 #include <sys/ioctl.h>
29 /* #include <fcntl.h> Can we live without this? */
30
31 #include "gdbcore.h"
32 #include <sys/user.h> /* After a.out.h */
33 #include <sys/file.h>
34 #include <sys/stat.h>
35 \f
36 void
37 fetch_inferior_registers ()
38 {
39 register int regno, datum;
40 register unsigned int regaddr;
41 int reg_buf[NUM_REGS+1];
42 struct user u;
43 register int skipped_frames = 0;
44
45 registers_fetched ();
46
47 for (regno = 0; regno < 64; regno++) {
48 reg_buf[regno] = ptrace (3, inferior_pid, regno, 0);
49
50 #if defined(PYRAMID_CONTROL_FRAME_DEBUGGING)
51 printf ("Fetching %s from inferior, got %0x\n",
52 reg_names[regno],
53 reg_buf[regno]);
54 #endif /* PYRAMID_CONTROL_FRAME_DEBUGGING */
55
56 if (reg_buf[regno] == -1 && errno == EIO) {
57 printf("fetch_interior_registers: fetching %s from inferior\n",
58 reg_names[regno]);
59 errno = 0;
60 }
61 supply_register (regno, reg_buf+regno);
62 }
63 /* that leaves regs 64, 65, and 66 */
64 datum = ptrace (3, inferior_pid,
65 ((char *)&u.u_pcb.pcb_csp) -
66 ((char *)&u), 0);
67
68
69
70 /* FIXME: Find the Current Frame Pointer (CFP). CFP is a global
71 register (ie, NOT windowed), that gets saved in a frame iff
72 the code for that frame has a prologue (ie, "adsf N"). If
73 there is a prologue, the adsf insn saves the old cfp in
74 pr13, cfp is set to sp, and N bytes of locals are allocated
75 (sp is decremented by n).
76 This makes finding CFP hard. I guess the right way to do it
77 is:
78 - If this is the innermost frame, believe ptrace() or
79 the core area.
80 - Otherwise:
81 Find the first insn of the current frame.
82 - find the saved pc;
83 - find the call insn that saved it;
84 - figure out where the call is to;
85 - if the first insn is an adsf, we got a frame
86 pointer. */
87
88
89 /* Normal processors have separate stack pointers for user and
90 kernel mode. Getting the last user mode frame on such
91 machines is easy: the kernel context of the ptrace()'d
92 process is on the kernel stack, and the USP points to what
93 we want. But Pyramids only have a single cfp for both user and
94 kernel mode. And processes being ptrace()'d have some
95 kernel-context control frames on their stack.
96 To avoid tracing back into the kernel context of an inferior,
97 we skip 0 or more contiguous control frames where the pc is
98 in the kernel. */
99
100 while (1) {
101 register int inferior_saved_pc;
102 inferior_saved_pc = ptrace (1, inferior_pid, datum+((32+15)*4), 0);
103 if (inferior_saved_pc > 0) break;
104 #if defined(PYRAMID_CONTROL_FRAME_DEBUGGING)
105 printf("skipping kernel frame %08x, pc=%08x\n", datum,
106 inferior_saved_pc);
107 #endif /* PYRAMID_CONTROL_FRAME_DEBUGGING */
108 skipped_frames++;
109 datum -= CONTROL_STACK_FRAME_SIZE;
110 }
111
112 reg_buf[CSP_REGNUM] = datum;
113 supply_register(CSP_REGNUM, reg_buf+CSP_REGNUM);
114 #ifdef PYRAMID_CONTROL_FRAME_DEBUGGING
115 if (skipped_frames) {
116 fprintf (stderr,
117 "skipped %d frames from %x to %x; cfp was %x, now %x\n",
118 skipped_frames, reg_buf[CSP_REGNUM]);
119 }
120 #endif /* PYRAMID_CONTROL_FRAME_DEBUGGING */
121 }
122
123 /* Store our register values back into the inferior.
124 If REGNO is -1, do this for all registers.
125 Otherwise, REGNO specifies which register (so we can save time). */
126
127 store_inferior_registers (regno)
128 int regno;
129 {
130 register unsigned int regaddr;
131 char buf[80];
132
133 if (regno >= 0)
134 {
135 if ((0 <= regno) && (regno < 64)) {
136 /*regaddr = register_addr (regno, offset);*/
137 regaddr = regno;
138 errno = 0;
139 ptrace (6, inferior_pid, regaddr, read_register (regno));
140 if (errno != 0)
141 {
142 sprintf (buf, "writing register number %d", regno);
143 perror_with_name (buf);
144 }
145 }
146 }
147 else
148 {
149 for (regno = 0; regno < NUM_REGS; regno++)
150 {
151 /*regaddr = register_addr (regno, offset);*/
152 regaddr = regno;
153 errno = 0;
154 ptrace (6, inferior_pid, regaddr, read_register (regno));
155 if (errno != 0)
156 {
157 sprintf (buf, "writing all regs, number %d", regno);
158 perror_with_name (buf);
159 }
160 }
161 }
162 \f
163 /*** Extensions to core and dump files, for GDB. */
164
165 extern unsigned int last_frame_offset;
166
167 #ifdef PYRAMID_CORE
168
169 /* Can't make definitions here static, since core.c needs them
170 to do bounds checking on the core-file areas. O well. */
171
172 /* have two stacks: one for data, one for register windows. */
173 extern CORE_ADDR reg_stack_start;
174 extern CORE_ADDR reg_stack_end;
175
176 /* need this so we can find the global registers: they never get saved. */
177 CORE_ADDR global_reg_offset;
178 static CORE_ADDR last_frame_address;
179 CORE_ADDR last_frame_offset;
180
181
182 /* Address in core file of start of register window stack area.
183 Don't know if is this any of meaningful, useful or necessary. */
184 extern int reg_stack_offset;
185
186 #endif /* PYRAMID_CORE */
187
188 \f
189 /* Work with core dump and executable files, for GDB.
190 This code would be in core.c if it weren't machine-dependent. */
191
192 void
193 core_file_command (filename, from_tty)
194 char *filename;
195 int from_tty;
196 {
197 int val;
198 extern char registers[];
199
200 /* Discard all vestiges of any previous core file
201 and mark data and stack spaces as empty. */
202
203 if (corefile)
204 free (corefile);
205 corefile = 0;
206
207 if (corechan >= 0)
208 close (corechan);
209 corechan = -1;
210
211 data_start = 0;
212 data_end = 0;
213 stack_start = STACK_END_ADDR;
214 stack_end = STACK_END_ADDR;
215
216 #ifdef PYRAMID_CORE
217 reg_stack_start = CONTROL_STACK_ADDR;
218 reg_stack_end = CONTROL_STACK_ADDR; /* this isn't strictly true...*/
219 #endif /* PYRAMID_CORE */
220
221 /* Now, if a new core file was specified, open it and digest it. */
222
223 if (filename)
224 {
225 filename = tilde_expand (filename);
226 make_cleanup (free, filename);
227
228 if (have_inferior_p ())
229 error ("To look at a core file, you must kill the inferior with \"kill\".");
230 corechan = open (filename, O_RDONLY, 0);
231 if (corechan < 0)
232 perror_with_name (filename);
233 /* 4.2-style (and perhaps also sysV-style) core dump file. */
234 {
235 struct user u;
236
237 unsigned int reg_offset;
238
239 val = myread (corechan, &u, sizeof u);
240 if (val < 0)
241 perror_with_name ("Not a core file: reading upage");
242 if (val != sizeof u)
243 error ("Not a core file: could only read %d bytes", val);
244 data_start = exec_data_start;
245
246 data_end = data_start + NBPG * u.u_dsize;
247 data_offset = NBPG * UPAGES;
248 stack_offset = NBPG * (UPAGES + u.u_dsize);
249
250 /* find registers in core file */
251 #ifdef PYRAMID_PTRACE
252 stack_start = stack_end - NBPG * u.u_ussize;
253 reg_stack_offset = stack_offset + (NBPG *u.u_ussize);
254 reg_stack_end = reg_stack_start + NBPG * u.u_cssize;
255
256 last_frame_address = ((int) u.u_pcb.pcb_csp);
257 last_frame_offset = reg_stack_offset + last_frame_address
258 - CONTROL_STACK_ADDR ;
259 global_reg_offset = (char *)&u - (char *)&u.u_pcb.pcb_gr0 ;
260
261 /* skip any control-stack frames that were executed in the
262 kernel. */
263
264 while (1) {
265 char buf[4];
266 val = lseek (corechan, last_frame_offset+(47*4), 0);
267 if (val < 0)
268 perror_with_name (filename);
269 val = myread (corechan, buf, sizeof buf);
270 if (val < 0)
271 perror_with_name (filename);
272
273 if (*(int *)buf >= 0)
274 break;
275 printf ("skipping frame %s\n", local_hex_string (last_frame_address));
276 last_frame_offset -= CONTROL_STACK_FRAME_SIZE;
277 last_frame_address -= CONTROL_STACK_FRAME_SIZE;
278 }
279 reg_offset = last_frame_offset;
280
281 #if 1 || defined(PYRAMID_CONTROL_FRAME_DEBUGGING)
282 printf ("Control stack pointer = %s\n",
283 local_hex_string (u.u_pcb.pcb_csp));
284 printf ("offset to control stack %d outermost frame %d (%s)\n",
285 reg_stack_offset, reg_offset, local_hex_string (last_frame_address));
286 #endif /* PYRAMID_CONTROL_FRAME_DEBUGGING */
287
288 #else /* not PYRAMID_CORE */
289 stack_start = stack_end - NBPG * u.u_ssize;
290 reg_offset = (int) u.u_ar0 - KERNEL_U_ADDR;
291 #endif /* not PYRAMID_CORE */
292
293 #ifdef __not_on_pyr_yet
294 /* Some machines put an absolute address in here and some put
295 the offset in the upage of the regs. */
296 reg_offset = (int) u.u_ar0;
297 if (reg_offset > NBPG * UPAGES)
298 reg_offset -= KERNEL_U_ADDR;
299 #endif
300
301 /* I don't know where to find this info.
302 So, for now, mark it as not available. */
303 N_SET_MAGIC (core_aouthdr, 0);
304
305 /* Read the register values out of the core file and store
306 them where `read_register' will find them. */
307
308 {
309 register int regno;
310
311 for (regno = 0; regno < 64; regno++)
312 {
313 char buf[MAX_REGISTER_RAW_SIZE];
314
315 val = lseek (corechan, register_addr (regno, reg_offset), 0);
316 if (val < 0
317 || (val = myread (corechan, buf, sizeof buf)) < 0)
318 {
319 char * buffer = (char *) alloca (strlen (reg_names[regno])
320 + 30);
321 strcpy (buffer, "Reading register ");
322 strcat (buffer, reg_names[regno]);
323
324 perror_with_name (buffer);
325 }
326
327 if (val < 0)
328 perror_with_name (filename);
329 #ifdef PYRAMID_CONTROL_FRAME_DEBUGGING
330 printf ("[reg %s(%d), offset in file %s=0x%0x, addr =0x%0x, =%0x]\n",
331 reg_names[regno], regno, filename,
332 register_addr(regno, reg_offset),
333 regno * 4 + last_frame_address,
334 *((int *)buf));
335 #endif /* PYRAMID_CONTROL_FRAME_DEBUGGING */
336 supply_register (regno, buf);
337 }
338 }
339 }
340 if (filename[0] == '/')
341 corefile = savestring (filename, strlen (filename));
342 else
343 {
344 corefile = concat (current_directory, "/", filename, NULL);
345 }
346
347 #if 1 || defined(PYRAMID_CONTROL_FRAME_DEBUGGING)
348 printf ("Providing CSP (%s) as nominal address of current frame.\n",
349 local_hex_string(last_frame_address));
350 #endif PYRAMID_CONTROL_FRAME_DEBUGGING
351 /* FIXME: Which of the following is correct? */
352 #if 0
353 set_current_frame ( create_new_frame (read_register (FP_REGNUM),
354 read_pc ()));
355 #else
356 set_current_frame ( create_new_frame (last_frame_address,
357 read_pc ()));
358 #endif
359
360 select_frame (get_current_frame (), 0);
361 validate_files ();
362 }
363 else if (from_tty)
364 printf ("No core file now.\n");
365 }