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
[];
67 fetch_inferior_registers (regno
)
71 extern char registers
[];
73 if (regno
< 0) { /* for all registers */
75 /* read 32 general purpose registers. */
77 for (ii
=0; ii
< 32; ++ii
)
78 *(int*)®isters
[REGISTER_BYTE (ii
)] =
79 ptrace (PT_READ_GPR
, inferior_pid
, ii
, 0, 0);
81 /* read general purpose floating point registers. */
83 for (ii
=0; ii
< 32; ++ii
)
84 ptrace (PT_READ_FPR
, inferior_pid
,
85 (int*)®isters
[REGISTER_BYTE (FP0_REGNUM
+ii
)], FPR0
+ii
, 0);
87 /* read special registers. */
88 for (ii
=0; ii
<= LAST_SP_REGNUM
-FIRST_SP_REGNUM
; ++ii
)
89 *(int*)®isters
[REGISTER_BYTE (FIRST_SP_REGNUM
+ii
)] =
90 ptrace (PT_READ_GPR
, inferior_pid
, special_regs
[ii
], 0, 0);
96 /* else an individual register is addressed. */
98 else if (regno
< FP0_REGNUM
) { /* a GPR */
99 *(int*)®isters
[REGISTER_BYTE (regno
)] =
100 ptrace (PT_READ_GPR
, inferior_pid
, regno
, 0, 0);
102 else if (regno
<= FPLAST_REGNUM
) { /* a FPR */
103 ptrace (PT_READ_FPR
, inferior_pid
,
104 (int*)®isters
[REGISTER_BYTE (regno
)], (regno
-FP0_REGNUM
+FPR0
), 0);
106 else if (regno
<= LAST_SP_REGNUM
) { /* a special register */
107 *(int*)®isters
[REGISTER_BYTE (regno
)] =
108 ptrace (PT_READ_GPR
, inferior_pid
,
109 special_regs
[regno
-FIRST_SP_REGNUM
], 0, 0);
112 fprintf (stderr
, "gdb error: register no %d not implemented.\n", regno
);
114 register_valid
[regno
] = 1;
117 /* Store our register values back into the inferior.
118 If REGNO is -1, do this for all registers.
119 Otherwise, REGNO specifies which register (so we can save time). */
122 store_inferior_registers (regno
)
125 extern char registers
[];
129 if (regno
== -1) { /* for all registers.. */
132 /* execute one dummy instruction (which is a breakpoint) in inferior
133 process. So give kernel a chance to do internal house keeping.
134 Otherwise the following ptrace(2) calls will mess up user stack
135 since kernel will get confused about the bottom of the stack (%sp) */
137 exec_one_dummy_insn ();
139 /* write general purpose registers first! */
140 for ( ii
=GPR0
; ii
<=GPR31
; ++ii
) {
141 ptrace (PT_WRITE_GPR
, inferior_pid
, ii
,
142 *(int*)®isters
[REGISTER_BYTE (ii
)], 0);
144 perror ("ptrace write_gpr"); errno
= 0;
148 /* write floating point registers now. */
149 for ( ii
=0; ii
< 32; ++ii
) {
150 ptrace (PT_WRITE_FPR
, inferior_pid
,
151 (int*)®isters
[REGISTER_BYTE (FP0_REGNUM
+ii
)], FPR0
+ii
, 0);
153 perror ("ptrace write_fpr"); errno
= 0;
157 /* write special registers. */
158 for (ii
=0; ii
<= LAST_SP_REGNUM
-FIRST_SP_REGNUM
; ++ii
) {
159 ptrace (PT_WRITE_GPR
, inferior_pid
, special_regs
[ii
],
160 *(int*)®isters
[REGISTER_BYTE (FIRST_SP_REGNUM
+ii
)], 0);
162 perror ("ptrace write_gpr"); errno
= 0;
167 /* else, a specific register number is given... */
169 else if (regno
< FP0_REGNUM
) { /* a GPR */
171 ptrace (PT_WRITE_GPR
, inferior_pid
, regno
,
172 *(int*)®isters
[REGISTER_BYTE (regno
)], 0);
175 else if (regno
<= FPLAST_REGNUM
) { /* a FPR */
176 ptrace (PT_WRITE_FPR
, inferior_pid
,
177 (int*)®isters
[REGISTER_BYTE (regno
)], regno
-FP0_REGNUM
+FPR0
, 0);
180 else if (regno
<= LAST_SP_REGNUM
) { /* a special register */
182 ptrace (PT_WRITE_GPR
, inferior_pid
, special_regs
[regno
-FIRST_SP_REGNUM
],
183 *(int*)®isters
[REGISTER_BYTE (regno
)], 0);
187 fprintf (stderr
, "Gdb error: register no %d not implemented.\n", regno
);
190 perror ("ptrace write"); errno
= 0;
195 fetch_core_registers (core_reg_sect
, core_reg_size
, which
, reg_addr
)
197 unsigned core_reg_size
;
199 unsigned int reg_addr
; /* Unused in this version */
201 /* fetch GPRs and special registers from the first register section
205 /* copy GPRs first. */
206 bcopy (core_reg_sect
, registers
, 32 * 4);
208 /* gdb's internal register template and bfd's register section layout
209 should share a common include file. FIXMEmgo */
210 /* then comes special registes. They are supposed to be in the same
211 order in gdb template and bfd `.reg' section. */
212 core_reg_sect
+= (32 * 4);
213 bcopy (core_reg_sect
, ®isters
[REGISTER_BYTE (FIRST_SP_REGNUM
)],
214 (LAST_SP_REGNUM
- FIRST_SP_REGNUM
+ 1) * 4);
217 /* fetch floating point registers from register section 2 in core bfd. */
219 bcopy (core_reg_sect
, ®isters
[REGISTER_BYTE (FP0_REGNUM
)], 32 * 8);
222 fprintf (stderr
, "Gdb error: unknown parameter to fetch_core_registers().\n");
226 frameless_function_invocation (fi
)
227 struct frame_info
*fi
;
229 CORE_ADDR func_start
;
230 struct aix_framedata fdata
;
232 func_start
= get_pc_function_start (fi
->pc
) + FUNCTION_START_OFFSET
;
234 /* If we failed to find the start of the function, it is a mistake
235 to inspect the instructions. */
240 function_frame_info (func_start
, &fdata
);
241 return fdata
.frameless
;
245 /* Return the address of a frame. This is the inital %sp value when the frame
246 was first allocated. For functions calling alloca(), it might be saved in
247 an alloca register. */
250 frame_initial_stack_address (fi
)
251 struct frame_info
*fi
;
253 CORE_ADDR frame_addr
, tmpaddr
;
254 struct aix_framedata fdata
;
255 struct frame_info
*callee_fi
;
258 extern struct obstack frame_cache_obstack
;
260 /* if the initial stack pointer (frame address) of this frame is known,
264 return fi
->initial_sp
;
266 /* find out if this function is using an alloca register.. */
268 tmpaddr
= get_pc_function_start (fi
->pc
);
269 function_frame_info (tmpaddr
, &fdata
);
271 /* if saved registers of this frame are not known yet, read and cache them. */
273 if (!fi
->cache_fsr
) {
274 fi
->cache_fsr
= (struct frame_saved_regs
*)
275 obstack_alloc (&frame_cache_obstack
, sizeof (struct frame_saved_regs
));
276 bzero (fi
->cache_fsr
, sizeof (struct frame_saved_regs
));
278 if (fi
->prev
&& fi
->prev
->frame
)
279 frame_addr
= fi
->prev
->frame
;
281 frame_addr
= read_memory_integer (fi
->frame
, 4);
283 /* if != -1, fdata.saved_fpr is the smallest number of saved_fpr. All fpr's
284 from saved_fpr to fp31 are saved right underneath caller stack pointer,
285 starting from fp31 first. */
287 if (fdata
.saved_fpr
>= 0) {
288 for (ii
=31; ii
>= fdata
.saved_fpr
; --ii
)
289 fi
->cache_fsr
->regs
[FP0_REGNUM
+ ii
] = frame_addr
- ((32 - ii
) * 8);
290 frame_addr
-= (32 - fdata
.saved_fpr
) * 8;
293 /* if != -1, fdata.saved_gpr is the smallest number of saved_gpr. All gpr's
294 from saved_gpr to gpr31 are saved right under saved fprs, starting
297 if (fdata
.saved_gpr
>= 0)
298 for (ii
=31; ii
>= fdata
.saved_gpr
; --ii
)
299 fi
->cache_fsr
->regs
[ii
] = frame_addr
- ((32 - ii
) * 4);
302 /* If no alloca register used, then fi->frame is the value of the %sp for
303 this frame, and it is good enough. */
305 if (fdata
.alloca_reg
< 0) {
306 fi
->initial_sp
= fi
->frame
;
307 return fi
->initial_sp
;
310 /* This function has an alloca register. If this is the top-most frame
311 (with the lowest address), the value in alloca register is good. */
314 return fi
->initial_sp
= read_register (fdata
.alloca_reg
);
316 /* Otherwise, this is a caller frame. Callee has already saved (???) its
317 registers. Find the address in which caller's alloca register is saved. */
319 for (callee_fi
= fi
->next
; callee_fi
; callee_fi
= callee_fi
->next
) {
321 if (!callee_fi
->cache_fsr
)
322 fatal ("Callee has not saved caller's registers.");
324 /* this is the address in which alloca register is saved. */
326 tmpaddr
= callee_fi
->cache_fsr
->regs
[fdata
.alloca_reg
];
328 fi
->initial_sp
= read_memory_integer (tmpaddr
, 4);
329 return fi
->initial_sp
;
332 /* Go look into deeper levels of the frame chain to see if any one of
333 the callees has saved alloca register. */
336 /* If alloca register was not saved, by the callee (or any of its callees)
337 then the value in the register is still good. */
339 return fi
->initial_sp
= read_register (fdata
.alloca_reg
);
344 /* aixcoff_relocate_symtab - hook for symbol table relocation.
345 also reads shared libraries.. */
347 aixcoff_relocate_symtab (pid
)
350 #define MAX_LOAD_SEGS 64 /* maximum number of load segments */
355 ldi
= (void *) alloca(MAX_LOAD_SEGS
* sizeof (*ldi
));
357 /* According to my humble theory, aixcoff has some timing problems and
358 when the user stack grows, kernel doesn't update stack info in time
359 and ptrace calls step on user stack. That is why we sleep here a little,
360 and give kernel to update its internals. */
365 ptrace(PT_LDINFO
, pid
, ldi
, MAX_LOAD_SEGS
* sizeof(*ldi
), ldi
);
367 perror_with_name ("ptrace ldinfo");
374 add_text_to_loadinfo (ldi
->ldinfo_textorg
, ldi
->ldinfo_dataorg
);
375 } while (ldi
->ldinfo_next
376 && (ldi
= (void *) (ldi
->ldinfo_next
+ (char *) ldi
)));
379 /* Now that we've jumbled things around, re-sort them. */
380 sort_minimal_symbols ();
383 /* relocate the exec and core sections as well. */
388 /* Keep an array of load segment information and their TOC table addresses.
389 This info will be useful when calling a shared library function by hand. */
392 unsigned long textorg
, dataorg
, toc_offset
;
395 #define LOADINFOLEN 10
397 static LoadInfo
*loadInfo
= NULL
;
398 static int loadInfoLen
= 0;
399 static int loadInfoTocIndex
= 0;
400 int aix_loadInfoTextIndex
= 0;
403 xcoff_init_loadinfo ()
405 loadInfoTocIndex
= 0;
406 aix_loadInfoTextIndex
= 0;
408 if (loadInfoLen
== 0) {
409 loadInfo
= (void*) xmalloc (sizeof (LoadInfo
) * LOADINFOLEN
);
410 loadInfoLen
= LOADINFOLEN
;
421 loadInfoTocIndex
= 0;
422 aix_loadInfoTextIndex
= 0;
426 xcoff_add_toc_to_loadinfo (unsigned long tocaddr
)
428 while (loadInfoTocIndex
>= loadInfoLen
) {
429 loadInfoLen
+= LOADINFOLEN
;
430 loadInfo
= (void*) xrealloc (loadInfo
, sizeof(LoadInfo
) * loadInfoLen
);
432 loadInfo
[loadInfoTocIndex
++].toc_offset
= tocaddr
;
436 add_text_to_loadinfo (unsigned long textaddr
, unsigned long dataaddr
)
438 while (aix_loadInfoTextIndex
>= loadInfoLen
) {
439 loadInfoLen
+= LOADINFOLEN
;
440 loadInfo
= (void*) xrealloc (loadInfo
, sizeof(LoadInfo
) * loadInfoLen
);
442 loadInfo
[aix_loadInfoTextIndex
].textorg
= textaddr
;
443 loadInfo
[aix_loadInfoTextIndex
].dataorg
= dataaddr
;
444 ++aix_loadInfoTextIndex
;
449 find_toc_address (unsigned long pc
)
451 int ii
, toc_entry
, tocbase
= 0;
453 for (ii
=0; ii
< aix_loadInfoTextIndex
; ++ii
)
454 if (pc
> loadInfo
[ii
].textorg
&& loadInfo
[ii
].textorg
> tocbase
) {
456 tocbase
= loadInfo
[ii
].textorg
;
459 return loadInfo
[toc_entry
].dataorg
+ loadInfo
[toc_entry
].toc_offset
;
463 /* execute one dummy breakpoint instruction. This way we give kernel
464 a chance to do some housekeeping and update inferior's internal data,
467 exec_one_dummy_insn ()
469 #define DUMMY_INSN_ADDR (TEXT_SEGMENT_BASE)+0x200
471 unsigned long shadow
;
472 unsigned int status
, pid
;
474 /* We plant one dummy breakpoint into DUMMY_INSN_ADDR address. We assume that
475 this address will never be executed again by the real code. */
477 target_insert_breakpoint (DUMMY_INSN_ADDR
, &shadow
);
480 ptrace (PT_CONTINUE
, inferior_pid
, DUMMY_INSN_ADDR
, 0, 0);
482 perror ("pt_continue");
485 pid
= wait (&status
);
486 } while (pid
!= inferior_pid
);
488 target_remove_breakpoint (DUMMY_INSN_ADDR
, &shadow
);
492 /* Return the number of initial trap signals we need to ignore once the inferior
493 process starts running. This will be `2' for aix-3.1, `3' for aix-3.2 */
496 aix_starting_inferior_traps ()
498 struct utsname unamebuf
;
500 if (uname (&unamebuf
) == -1)
501 fatal ("uname(3) failed.");
503 /* Assume the future versions will behave like 3.2 and return '3' for
504 anything other than 3.1x. The extra trap in 3.2 is the "trap after the
505 program is loaded" signal. */
507 if (unamebuf
.version
[0] == '3' && unamebuf
.release
[0] == '1')