1 /* Low level Pyramid interface to ptrace, for GDB when running under Unix.
2 Copyright (C) 1988, 1989, 1991 Free Software Foundation, Inc.
4 This file is part of GDB.
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.
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.
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. */
25 #include <sys/param.h>
28 #include <sys/ioctl.h>
29 /* #include <fcntl.h> Can we live without this? */
32 #include <sys/user.h> /* After a.out.h */
37 fetch_inferior_registers ()
39 register int regno
, datum
;
40 register unsigned int regaddr
;
41 int reg_buf
[NUM_REGS
+1];
43 register int skipped_frames
= 0;
47 for (regno
= 0; regno
< 64; regno
++) {
48 reg_buf
[regno
] = ptrace (3, inferior_pid
, regno
, 0);
50 #if defined(PYRAMID_CONTROL_FRAME_DEBUGGING)
51 printf ("Fetching %s from inferior, got %0x\n",
54 #endif /* PYRAMID_CONTROL_FRAME_DEBUGGING */
56 if (reg_buf
[regno
] == -1 && errno
== EIO
) {
57 printf("fetch_interior_registers: fetching %s from inferior\n",
61 supply_register (regno
, reg_buf
+regno
);
63 /* that leaves regs 64, 65, and 66 */
64 datum
= ptrace (3, inferior_pid
,
65 ((char *)&u
.u_pcb
.pcb_csp
) -
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
78 - If this is the innermost frame, believe ptrace() or
81 Find the first insn of the current frame.
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
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
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
,
107 #endif /* PYRAMID_CONTROL_FRAME_DEBUGGING */
109 datum
-= CONTROL_STACK_FRAME_SIZE
;
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
) {
117 "skipped %d frames from %x to %x; cfp was %x, now %x\n",
118 skipped_frames
, reg_buf
[CSP_REGNUM
]);
120 #endif /* PYRAMID_CONTROL_FRAME_DEBUGGING */
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). */
127 store_inferior_registers (regno
)
130 register unsigned int regaddr
;
135 if ((0 <= regno
) && (regno
< 64)) {
136 /*regaddr = register_addr (regno, offset);*/
139 ptrace (6, inferior_pid
, regaddr
, read_register (regno
));
142 sprintf (buf
, "writing register number %d", regno
);
143 perror_with_name (buf
);
149 for (regno
= 0; regno
< NUM_REGS
; regno
++)
151 /*regaddr = register_addr (regno, offset);*/
154 ptrace (6, inferior_pid
, regaddr
, read_register (regno
));
157 sprintf (buf
, "writing all regs, number %d", regno
);
158 perror_with_name (buf
);
163 /*** Extensions to core and dump files, for GDB. */
165 extern unsigned int last_frame_offset
;
169 /* Can't make definitions here static, since core.c needs them
170 to do bounds checking on the core-file areas. O well. */
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
;
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
;
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
;
186 #endif /* PYRAMID_CORE */
189 /* Work with core dump and executable files, for GDB.
190 This code would be in core.c if it weren't machine-dependent. */
193 core_file_command (filename
, from_tty
)
198 extern char registers
[];
200 /* Discard all vestiges of any previous core file
201 and mark data and stack spaces as empty. */
213 stack_start
= STACK_END_ADDR
;
214 stack_end
= STACK_END_ADDR
;
217 reg_stack_start
= CONTROL_STACK_ADDR
;
218 reg_stack_end
= CONTROL_STACK_ADDR
; /* this isn't strictly true...*/
219 #endif /* PYRAMID_CORE */
221 /* Now, if a new core file was specified, open it and digest it. */
225 filename
= tilde_expand (filename
);
226 make_cleanup (free
, filename
);
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);
232 perror_with_name (filename
);
233 /* 4.2-style (and perhaps also sysV-style) core dump file. */
237 unsigned int reg_offset
;
239 val
= myread (corechan
, &u
, sizeof u
);
241 perror_with_name ("Not a core file: reading upage");
243 error ("Not a core file: could only read %d bytes", val
);
244 data_start
= exec_data_start
;
246 data_end
= data_start
+ NBPG
* u
.u_dsize
;
247 data_offset
= NBPG
* UPAGES
;
248 stack_offset
= NBPG
* (UPAGES
+ u
.u_dsize
);
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
;
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
;
261 /* skip any control-stack frames that were executed in the
266 val
= lseek (corechan
, last_frame_offset
+(47*4), 0);
268 perror_with_name (filename
);
269 val
= myread (corechan
, buf
, sizeof buf
);
271 perror_with_name (filename
);
273 if (*(int *)buf
>= 0)
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
;
279 reg_offset
= last_frame_offset
;
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 */
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 */
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
;
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);
305 /* Read the register values out of the core file and store
306 them where `read_register' will find them. */
311 for (regno
= 0; regno
< 64; regno
++)
313 char buf
[MAX_REGISTER_RAW_SIZE
];
315 val
= lseek (corechan
, register_addr (regno
, reg_offset
), 0);
317 || (val
= myread (corechan
, buf
, sizeof buf
)) < 0)
319 char * buffer
= (char *) alloca (strlen (reg_names
[regno
])
321 strcpy (buffer
, "Reading register ");
322 strcat (buffer
, reg_names
[regno
]);
324 perror_with_name (buffer
);
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
,
335 #endif /* PYRAMID_CONTROL_FRAME_DEBUGGING */
336 supply_register (regno
, buf
);
340 if (filename
[0] == '/')
341 corefile
= savestring (filename
, strlen (filename
));
344 corefile
= concat (current_directory
, "/", filename
, NULL
);
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? */
353 set_current_frame ( create_new_frame (read_register (FP_REGNUM
),
356 set_current_frame ( create_new_frame (last_frame_address
,
360 select_frame (get_current_frame (), 0);
364 printf ("No core file now.\n");