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