1 /* IBM RS/6000 host-dependent code for GDB, the GNU debugger.
2 Copyright (C) 1986, 1987, 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. */
27 #include <sys/param.h>
31 #include <sys/ioctl.h>
34 #include <sys/ptrace.h>
42 #include <sys/utsname.h>
45 extern int attach_flag;
47 /* Conversion from gdb-to-system special purpose register numbers.. */
49 static int special_regs[] = {
60 /* Nonzero if we just simulated a single step break. */
61 extern int one_stepped;
63 extern char register_valid[];
64 extern struct obstack frame_cache_obstack;
68 fetch_inferior_registers (regno)
72 extern char registers[];
74 if (regno < 0) { /* for all registers */
76 /* read 32 general purpose registers. */
78 for (ii=0; ii < 32; ++ii)
79 *(int*)®isters[REGISTER_BYTE (ii)] =
80 ptrace (PT_READ_GPR, inferior_pid, ii, 0, 0);
82 /* read general purpose floating point registers. */
84 for (ii=0; ii < 32; ++ii)
85 ptrace (PT_READ_FPR, inferior_pid,
86 (int*)®isters [REGISTER_BYTE (FP0_REGNUM+ii)], FPR0+ii, 0);
88 /* read special registers. */
89 for (ii=0; ii <= LAST_SP_REGNUM-FIRST_SP_REGNUM; ++ii)
90 *(int*)®isters[REGISTER_BYTE (FIRST_SP_REGNUM+ii)] =
91 ptrace (PT_READ_GPR, inferior_pid, special_regs[ii], 0, 0);
97 /* else an individual register is addressed. */
99 else if (regno < FP0_REGNUM) { /* a GPR */
100 *(int*)®isters[REGISTER_BYTE (regno)] =
101 ptrace (PT_READ_GPR, inferior_pid, regno, 0, 0);
103 else if (regno <= FPLAST_REGNUM) { /* a FPR */
104 ptrace (PT_READ_FPR, inferior_pid,
105 (int*)®isters [REGISTER_BYTE (regno)], (regno-FP0_REGNUM+FPR0), 0);
107 else if (regno <= LAST_SP_REGNUM) { /* a special register */
108 *(int*)®isters[REGISTER_BYTE (regno)] =
109 ptrace (PT_READ_GPR, inferior_pid,
110 special_regs[regno-FIRST_SP_REGNUM], 0, 0);
113 fprintf (stderr, "gdb error: register no %d not implemented.\n", regno);
115 register_valid [regno] = 1;
118 /* Store our register values back into the inferior.
119 If REGNO is -1, do this for all registers.
120 Otherwise, REGNO specifies which register (so we can save time). */
123 store_inferior_registers (regno)
126 extern char registers[];
130 if (regno == -1) { /* for all registers.. */
133 /* execute one dummy instruction (which is a breakpoint) in inferior
134 process. So give kernel a chance to do internal house keeping.
135 Otherwise the following ptrace(2) calls will mess up user stack
136 since kernel will get confused about the bottom of the stack (%sp) */
138 exec_one_dummy_insn ();
140 /* write general purpose registers first! */
141 for ( ii=GPR0; ii<=GPR31; ++ii) {
142 ptrace (PT_WRITE_GPR, inferior_pid, ii,
143 *(int*)®isters[REGISTER_BYTE (ii)], 0);
145 perror ("ptrace write_gpr"); errno = 0;
149 /* write floating point registers now. */
150 for ( ii=0; ii < 32; ++ii) {
151 ptrace (PT_WRITE_FPR, inferior_pid,
152 (int*)®isters[REGISTER_BYTE (FP0_REGNUM+ii)], FPR0+ii, 0);
154 perror ("ptrace write_fpr"); errno = 0;
158 /* write special registers. */
159 for (ii=0; ii <= LAST_SP_REGNUM-FIRST_SP_REGNUM; ++ii) {
160 ptrace (PT_WRITE_GPR, inferior_pid, special_regs[ii],
161 *(int*)®isters[REGISTER_BYTE (FIRST_SP_REGNUM+ii)], 0);
163 perror ("ptrace write_gpr"); errno = 0;
168 /* else, a specific register number is given... */
170 else if (regno < FP0_REGNUM) { /* a GPR */
172 ptrace (PT_WRITE_GPR, inferior_pid, regno,
173 *(int*)®isters[REGISTER_BYTE (regno)], 0);
176 else if (regno <= FPLAST_REGNUM) { /* a FPR */
177 ptrace (PT_WRITE_FPR, inferior_pid,
178 (int*)®isters[REGISTER_BYTE (regno)], regno-FP0_REGNUM+FPR0, 0);
181 else if (regno <= LAST_SP_REGNUM) { /* a special register */
183 ptrace (PT_WRITE_GPR, inferior_pid, special_regs [regno-FIRST_SP_REGNUM],
184 *(int*)®isters[REGISTER_BYTE (regno)], 0);
188 fprintf (stderr, "Gdb error: register no %d not implemented.\n", regno);
191 perror ("ptrace write"); errno = 0;
196 fetch_core_registers (core_reg_sect, core_reg_size, which, reg_addr)
198 unsigned core_reg_size;
200 unsigned int reg_addr; /* Unused in this version */
202 /* fetch GPRs and special registers from the first register section
206 /* copy GPRs first. */
207 bcopy (core_reg_sect, registers, 32 * 4);
209 /* gdb's internal register template and bfd's register section layout
210 should share a common include file. FIXMEmgo */
211 /* then comes special registes. They are supposed to be in the same
212 order in gdb template and bfd `.reg' section. */
213 core_reg_sect += (32 * 4);
214 bcopy (core_reg_sect, ®isters [REGISTER_BYTE (FIRST_SP_REGNUM)],
215 (LAST_SP_REGNUM - FIRST_SP_REGNUM + 1) * 4);
218 /* fetch floating point registers from register section 2 in core bfd. */
220 bcopy (core_reg_sect, ®isters [REGISTER_BYTE (FP0_REGNUM)], 32 * 8);
223 fprintf (stderr, "Gdb error: unknown parameter to fetch_core_registers().\n");
227 frameless_function_invocation (fi)
228 struct frame_info *fi;
230 CORE_ADDR func_start;
231 struct aix_framedata fdata;
233 func_start = get_pc_function_start (fi->pc) + FUNCTION_START_OFFSET;
235 /* If we failed to find the start of the function, it is a mistake
236 to inspect the instructions. */
241 function_frame_info (func_start, &fdata);
242 return fdata.frameless;
246 /* If saved registers of frame FI are not known yet, read and cache them.
247 &FDATAP contains aix_framedata; TDATAP can be NULL,
248 in which case the framedata are read.
252 frame_get_cache_fsr (fi, fdatap)
253 struct frame_info *fi;
254 struct aix_framedata *fdatap;
257 CORE_ADDR frame_addr;
258 struct aix_framedata work_fdata;
263 fdatap = &work_fdata;
264 function_frame_info (get_pc_function_start (fi->pc), fdatap);
267 fi->cache_fsr = (struct frame_saved_regs *)
268 obstack_alloc (&frame_cache_obstack, sizeof (struct frame_saved_regs));
269 bzero (fi->cache_fsr, sizeof (struct frame_saved_regs));
271 if (fi->prev && fi->prev->frame)
272 frame_addr = fi->prev->frame;
274 frame_addr = read_memory_integer (fi->frame, 4);
276 /* if != -1, fdatap->saved_fpr is the smallest number of saved_fpr.
277 All fpr's from saved_fpr to fp31 are saved right underneath caller
278 stack pointer, starting from fp31 first. */
280 if (fdatap->saved_fpr >= 0) {
281 for (ii=31; ii >= fdatap->saved_fpr; --ii)
282 fi->cache_fsr->regs [FP0_REGNUM + ii] = frame_addr - ((32 - ii) * 8);
283 frame_addr -= (32 - fdatap->saved_fpr) * 8;
286 /* if != -1, fdatap->saved_gpr is the smallest number of saved_gpr.
287 All gpr's from saved_gpr to gpr31 are saved right under saved fprs,
288 starting from r31 first. */
290 if (fdatap->saved_gpr >= 0)
291 for (ii=31; ii >= fdatap->saved_gpr; --ii)
292 fi->cache_fsr->regs [ii] = frame_addr - ((32 - ii) * 4);
295 /* Return the address of a frame. This is the inital %sp value when the frame
296 was first allocated. For functions calling alloca(), it might be saved in
297 an alloca register. */
300 frame_initial_stack_address (fi)
301 struct frame_info *fi;
304 struct aix_framedata fdata;
305 struct frame_info *callee_fi;
307 /* if the initial stack pointer (frame address) of this frame is known,
311 return fi->initial_sp;
313 /* find out if this function is using an alloca register.. */
315 function_frame_info (get_pc_function_start (fi->pc), &fdata);
317 /* if saved registers of this frame are not known yet, read and cache them. */
320 frame_get_cache_fsr (fi, &fdata);
322 /* If no alloca register used, then fi->frame is the value of the %sp for
323 this frame, and it is good enough. */
325 if (fdata.alloca_reg < 0) {
326 fi->initial_sp = fi->frame;
327 return fi->initial_sp;
330 /* This function has an alloca register. If this is the top-most frame
331 (with the lowest address), the value in alloca register is good. */
334 return fi->initial_sp = read_register (fdata.alloca_reg);
336 /* Otherwise, this is a caller frame. Callee has usually already saved
337 registers, but there are are exceptions (such as when the callee
338 has no parameters). Find the address in which caller's alloca
339 register is saved. */
341 for (callee_fi = fi->next; callee_fi; callee_fi = callee_fi->next) {
343 if (!callee_fi->cache_fsr)
344 frame_get_cache_fsr (fi, NULL);
346 /* this is the address in which alloca register is saved. */
348 tmpaddr = callee_fi->cache_fsr->regs [fdata.alloca_reg];
350 fi->initial_sp = read_memory_integer (tmpaddr, 4);
351 return fi->initial_sp;
354 /* Go look into deeper levels of the frame chain to see if any one of
355 the callees has saved alloca register. */
358 /* If alloca register was not saved, by the callee (or any of its callees)
359 then the value in the register is still good. */
361 return fi->initial_sp = read_register (fdata.alloca_reg);
366 /* aixcoff_relocate_symtab - hook for symbol table relocation.
367 also reads shared libraries.. */
369 aixcoff_relocate_symtab (pid)
372 #define MAX_LOAD_SEGS 64 /* maximum number of load segments */
377 ldi = (void *) alloca(MAX_LOAD_SEGS * sizeof (*ldi));
379 /* According to my humble theory, aixcoff has some timing problems and
380 when the user stack grows, kernel doesn't update stack info in time
381 and ptrace calls step on user stack. That is why we sleep here a little,
382 and give kernel to update its internals. */
387 ptrace(PT_LDINFO, pid, ldi, MAX_LOAD_SEGS * sizeof(*ldi), ldi);
389 perror_with_name ("ptrace ldinfo");
396 add_text_to_loadinfo (ldi->ldinfo_textorg, ldi->ldinfo_dataorg);
397 } while (ldi->ldinfo_next
398 && (ldi = (void *) (ldi->ldinfo_next + (char *) ldi)));
401 /* Now that we've jumbled things around, re-sort them. */
402 sort_minimal_symbols ();
405 /* relocate the exec and core sections as well. */
410 /* Keep an array of load segment information and their TOC table addresses.
411 This info will be useful when calling a shared library function by hand. */
414 unsigned long textorg, dataorg, toc_offset;
417 #define LOADINFOLEN 10
419 static LoadInfo *loadInfo = NULL;
420 static int loadInfoLen = 0;
421 static int loadInfoTocIndex = 0;
422 int aix_loadInfoTextIndex = 0;
425 xcoff_init_loadinfo ()
427 loadInfoTocIndex = 0;
428 aix_loadInfoTextIndex = 0;
430 if (loadInfoLen == 0) {
431 loadInfo = (void*) xmalloc (sizeof (LoadInfo) * LOADINFOLEN);
432 loadInfoLen = LOADINFOLEN;
443 loadInfoTocIndex = 0;
444 aix_loadInfoTextIndex = 0;
448 xcoff_add_toc_to_loadinfo (unsigned long tocaddr)
450 while (loadInfoTocIndex >= loadInfoLen) {
451 loadInfoLen += LOADINFOLEN;
452 loadInfo = (void*) xrealloc (loadInfo, sizeof(LoadInfo) * loadInfoLen);
454 loadInfo [loadInfoTocIndex++].toc_offset = tocaddr;
458 add_text_to_loadinfo (unsigned long textaddr, unsigned long dataaddr)
460 while (aix_loadInfoTextIndex >= loadInfoLen) {
461 loadInfoLen += LOADINFOLEN;
462 loadInfo = (void*) xrealloc (loadInfo, sizeof(LoadInfo) * loadInfoLen);
464 loadInfo [aix_loadInfoTextIndex].textorg = textaddr;
465 loadInfo [aix_loadInfoTextIndex].dataorg = dataaddr;
466 ++aix_loadInfoTextIndex;
471 find_toc_address (unsigned long pc)
473 int ii, toc_entry, tocbase = 0;
475 for (ii=0; ii < aix_loadInfoTextIndex; ++ii)
476 if (pc > loadInfo [ii].textorg && loadInfo [ii].textorg > tocbase) {
478 tocbase = loadInfo [ii].textorg;
481 return loadInfo [toc_entry].dataorg + loadInfo [toc_entry].toc_offset;
485 /* execute one dummy breakpoint instruction. This way we give kernel
486 a chance to do some housekeeping and update inferior's internal data,
489 exec_one_dummy_insn ()
491 #define DUMMY_INSN_ADDR (TEXT_SEGMENT_BASE)+0x200
493 unsigned long shadow;
494 unsigned int status, pid;
496 /* We plant one dummy breakpoint into DUMMY_INSN_ADDR address. We assume that
497 this address will never be executed again by the real code. */
499 target_insert_breakpoint (DUMMY_INSN_ADDR, &shadow);
502 ptrace (PT_CONTINUE, inferior_pid, DUMMY_INSN_ADDR, 0, 0);
504 perror ("pt_continue");
507 pid = wait (&status);
508 } while (pid != inferior_pid);
510 target_remove_breakpoint (DUMMY_INSN_ADDR, &shadow);
514 /* Return the number of initial trap signals we need to ignore once the inferior
515 process starts running. This will be `2' for aix-3.1, `3' for aix-3.2 */
518 aix_starting_inferior_traps ()
520 struct utsname unamebuf;
522 if (uname (&unamebuf) == -1)
523 fatal ("uname(3) failed.");
525 /* Assume the future versions will behave like 3.2 and return '3' for
526 anything other than 3.1x. The extra trap in 3.2 is the "trap after the
527 program is loaded" signal. */
529 if (unamebuf.version[0] == '3' && unamebuf.release[0] == '1')