1 /* Target-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. */
28 #include <sys/param.h>
32 #include <sys/ioctl.h>
35 #include <sys/ptrace.h>
44 extern int attach_flag
;
46 /* Nonzero if we just simulated a single step break. */
49 /* Breakpoint shadows for the single step instructions will be kept here. */
51 static struct sstep_breaks
{
58 * Calculate the destination of a branch/jump. Return -1 if not a branch.
61 branch_dest (opcode
, instr
, pc
, safety
)
62 int opcode
, instr
, pc
, safety
;
70 absolute
= (int) ((instr
>> 1) & 1);
74 immediate
= ((instr
& ~3) << 6) >> 6; /* br unconditionl */
77 if (opcode
!= 18) /* br conditional */
78 immediate
= ((instr
& ~3) << 16) >> 16;
82 dest
= pc
+ immediate
;
86 ext_op
= (instr
>>1) & 0x3ff;
88 if (ext_op
== 16) /* br conditional register */
89 dest
= read_register (LR_REGNUM
) & ~3;
91 else if (ext_op
== 528) /* br cond to count reg */
92 dest
= read_register (CTR_REGNUM
) & ~3;
99 return (dest
< TEXT_SEGMENT_BASE
) ? safety
: dest
;
104 /* AIX does not support PT_STEP. Simulate it. */
110 #define INSNLEN(OPCODE) 4
112 static char breakp
[] = BREAKPOINT
;
113 int ii
, insn
, ret
, loc
;
114 int breaks
[2], opcode
;
117 extern CORE_ADDR text_start
;
120 ret
= read_memory (loc
, &insn
, sizeof (int));
122 printf ("Error in single_step()!!\n");
124 breaks
[0] = loc
+ INSNLEN(insn
);
126 breaks
[1] = branch_dest (opcode
, insn
, loc
, breaks
[0]);
128 /* Don't put two breakpoints on the same address. */
129 if (breaks
[1] == breaks
[0])
132 stepBreaks
[1].address
= -1;
134 for (ii
=0; ii
< 2; ++ii
) {
136 /* ignore invalid breakpoint. */
137 if ( breaks
[ii
] == -1)
140 read_memory (breaks
[ii
], &(stepBreaks
[ii
].data
), sizeof(int));
142 ret
= write_memory (breaks
[ii
], breakp
, sizeof(int));
143 stepBreaks
[ii
].address
= breaks
[ii
];
147 ptrace (PT_CONTINUE
, inferior_pid
, 1, signal
, 0);
151 /* remove step breakpoints. */
152 for (ii
=0; ii
< 2; ++ii
)
153 if (stepBreaks
[ii
].address
!= -1)
155 (stepBreaks
[ii
].address
, &(stepBreaks
[ii
].data
), sizeof(int));
164 /* return pc value after skipping a function prologue. */
172 if (target_read_memory (pc
, (char *)&op
, sizeof (op
)))
173 return pc
; /* Can't access it -- assume no prologue. */
174 SWAP_TARGET_AND_HOST (&op
, sizeof (op
));
176 /* Assume that subsequent fetches can fail with low probability. */
178 if (op
== 0x7c0802a6) { /* mflr r0 */
180 op
= read_memory_integer (pc
, 4);
183 if ((op
& 0xfc00003e) == 0x7c000026) { /* mfcr Rx */
185 op
= read_memory_integer (pc
, 4);
188 if ((op
& 0xfc000000) == 0x48000000) { /* bl foo, to save fprs??? */
190 op
= read_memory_integer (pc
, 4);
194 if ((op
& 0xfc1f0000) == 0xd8010000) { /* stfd Rx,NUM(r1) */
195 pc
+= 4; /* store floating register double */
196 op
= read_memory_integer (pc
, 4);
200 if ((op
& 0xfc1f0000) == 0xbc010000) { /* stm Rx, NUM(r1) */
202 op
= read_memory_integer (pc
, 4);
205 while (((tmp
= op
>> 16) == 0x9001) || /* st r0, NUM(r1) */
206 (tmp
== 0x9421) || /* stu r1, NUM(r1) */
207 (op
== 0x93e1fffc)) /* st r31,-4(r1) */
210 op
= read_memory_integer (pc
, 4);
213 while ((tmp
= (op
>> 22)) == 0x20f) { /* l r31, ... or */
214 pc
+= 4; /* l r30, ... */
215 op
= read_memory_integer (pc
, 4);
218 /* store parameters into stack */
220 (op
& 0xfc1f0000) == 0xd8010000 || /* stfd Rx,NUM(r1) */
221 (op
& 0xfc1f0000) == 0x90010000 || /* st r?, NUM(r1) */
222 (op
& 0xfc000000) == 0xfc000000 || /* frsp, fp?, .. */
223 (op
& 0xd0000000) == 0xd0000000) /* stfs, fp?, .. */
225 pc
+= 4; /* store fpr double */
226 op
= read_memory_integer (pc
, 4);
229 if (op
== 0x603f0000) { /* oril r31, r1, 0x0 */
230 pc
+= 4; /* this happens if r31 is used as */
231 op
= read_memory_integer (pc
, 4); /* frame ptr. (gcc does that) */
234 while ((op
>> 16) == (0x907f + tmp
)) { /* st r3, NUM(r31) */
235 pc
+= 4; /* st r4, NUM(r31), ... */
236 op
= read_memory_integer (pc
, 4);
244 /* text start and end addresses in virtual memory. */
246 CORE_ADDR text_start
;
250 /*************************************************************************
251 Support for creating pushind a dummy frame into the stack, and popping
253 *************************************************************************/
255 /* The total size of dummy frame is 436, which is;
260 and 24 extra bytes for the callee's link area. The last 24 bytes
261 for the link area might not be necessary, since it will be taken
262 care of by push_arguments(). */
264 #define DUMMY_FRAME_SIZE 436
266 #define DUMMY_FRAME_ADDR_SIZE 10
268 /* Make sure you initialize these in somewhere, in case gdb gives up what it
269 was debugging and starts debugging something else. FIXMEibm */
271 static int dummy_frame_count
= 0;
272 static int dummy_frame_size
= 0;
273 static CORE_ADDR
*dummy_frame_addr
= 0;
275 extern int stop_stack_dummy
;
277 /* push a dummy frame into stack, save all register. Currently we are saving
278 only gpr's and fpr's, which is not good enough! FIXMEmgo */
282 int sp
, pc
; /* stack pointer and link register */
285 if (dummy_frame_count
>= dummy_frame_size
) {
286 dummy_frame_size
+= DUMMY_FRAME_ADDR_SIZE
;
287 if (dummy_frame_addr
)
288 dummy_frame_addr
= (CORE_ADDR
*) xrealloc
289 (dummy_frame_addr
, sizeof(CORE_ADDR
) * (dummy_frame_size
));
291 dummy_frame_addr
= (CORE_ADDR
*)
292 xmalloc (sizeof(CORE_ADDR
) * (dummy_frame_size
));
295 sp
= read_register(SP_REGNUM
);
296 pc
= read_register(PC_REGNUM
);
298 dummy_frame_addr
[dummy_frame_count
++] = sp
;
300 /* Be careful! If the stack pointer is not decremented first, then kernel
301 thinks he is free to use the sapce underneath it. And kernel actually
302 uses that area for IPC purposes when executing ptrace(2) calls. So
303 before writing register values into the new frame, decrement and update
304 %sp first in order to secure your frame. */
306 write_register (SP_REGNUM
, sp
-DUMMY_FRAME_SIZE
);
308 /* gdb relies on the state of current_frame. We'd better update it,
309 otherwise things like do_registers_info() wouldn't work properly! */
311 flush_cached_frames ();
312 set_current_frame (create_new_frame (sp
-DUMMY_FRAME_SIZE
, pc
));
314 /* save program counter in link register's space. */
315 write_memory (sp
+8, &pc
, 4);
317 /* save full floating point registers here. They will be from F14..F31
318 for know. I am not sure if we need to save everything here! */
321 for (ii
= 0; ii
< 32; ++ii
)
322 write_memory (sp
-8-(ii
*8), ®isters
[REGISTER_BYTE (31-ii
+FP0_REGNUM
)], 8);
325 for (ii
=1; ii
<=32; ++ii
)
326 write_memory (sp
-256-(ii
*4), ®isters
[REGISTER_BYTE (32-ii
)], 4);
328 /* so far, 32*2 + 32 words = 384 bytes have been written.
329 7 extra registers in our register set: pc, ps, cnd, lr, cnt, xer, mq */
331 for (ii
=1; ii
<= (LAST_SP_REGNUM
-FIRST_SP_REGNUM
+1); ++ii
) {
332 write_memory (sp
-384-(ii
*4),
333 ®isters
[REGISTER_BYTE (FPLAST_REGNUM
+ ii
)], 4);
336 /* Save sp or so called back chain right here. */
337 write_memory (sp
-DUMMY_FRAME_SIZE
, &sp
, 4);
338 sp
-= DUMMY_FRAME_SIZE
;
340 /* And finally, this is the back chain. */
341 write_memory (sp
+8, &pc
, 4);
345 /* Pop a dummy frame.
347 In rs6000 when we push a dummy frame, we save all of the registers. This
348 is usually done before user calls a function explicitly.
350 After a dummy frame is pushed, some instructions are copied into stack,
351 and stack pointer is decremented even more. Since we don't have a frame
352 pointer to get back to the parent frame of the dummy, we start having
353 trouble poping it. Therefore, we keep a dummy frame stack, keeping
354 addresses of dummy frames as such. When poping happens and when we
355 detect that was a dummy frame, we pop it back to its parent by using
356 dummy frame stack (`dummy_frame_addr' array).
363 sp
= dummy_frame_addr
[--dummy_frame_count
];
365 /* restore all fpr's. */
366 for (ii
= 1; ii
<= 32; ++ii
)
367 read_memory (sp
-(ii
*8), ®isters
[REGISTER_BYTE (32-ii
+FP0_REGNUM
)], 8);
369 /* restore all gpr's */
370 for (ii
=1; ii
<= 32; ++ii
) {
371 read_memory (sp
-256-(ii
*4), ®isters
[REGISTER_BYTE (32-ii
)], 4);
374 /* restore the rest of the registers. */
375 for (ii
=1; ii
<=(LAST_SP_REGNUM
-FIRST_SP_REGNUM
+1); ++ii
)
376 read_memory (sp
-384-(ii
*4),
377 ®isters
[REGISTER_BYTE (FPLAST_REGNUM
+ ii
)], 4);
379 read_memory (sp
-(DUMMY_FRAME_SIZE
-8),
380 ®isters
[REGISTER_BYTE(PC_REGNUM
)], 4);
382 /* when a dummy frame was being pushed, we had to decrement %sp first, in
383 order to secure astack space. Thus, saved %sp (or %r1) value, is not the
384 one we should restore. Change it with the one we need. */
386 *(int*)®isters
[REGISTER_BYTE(FP_REGNUM
)] = sp
;
388 /* Now we can restore all registers. */
390 store_inferior_registers (-1);
392 flush_cached_frames ();
393 set_current_frame (create_new_frame (sp
, pc
));
397 /* pop the innermost frame, go back to the caller. */
401 int pc
, lr
, sp
, prev_sp
; /* %pc, %lr, %sp */
402 FRAME fr
= get_current_frame ();
404 int frameless
= 0; /* TRUE if function is frameless */
406 int saved_gpr
, saved_fpr
; /* # of saved gpr's and fpr's */
411 if (stop_stack_dummy
&& dummy_frame_count
) {
416 /* figure out previous %pc value. If the function is frameless, it is
417 still in the link register, otherwise walk the frames and retrieve the
418 saved %pc value in the previous frame. */
420 addr
= get_pc_function_start (fr
->pc
) + FUNCTION_START_OFFSET
;
421 function_frame_info (addr
, &frameless
, &offset
, &saved_gpr
, &saved_fpr
);
423 read_memory (sp
, &prev_sp
, 4);
425 lr
= read_register (LR_REGNUM
);
427 read_memory (prev_sp
+8, &lr
, 4);
429 /* reset %pc value. */
430 write_register (PC_REGNUM
, lr
);
432 /* reset register values if any was saved earlier. */
433 addr
= prev_sp
- offset
;
436 for (ii
=saved_gpr
; ii
<= 31; ++ii
) {
437 read_memory (addr
, ®isters
[REGISTER_BYTE (ii
)], 4);
438 addr
+= sizeof (int);
442 for (ii
=saved_fpr
; ii
<= 31; ++ii
) {
443 read_memory (addr
, ®isters
[REGISTER_BYTE (ii
+FP0_REGNUM
)], 8);
447 write_register (SP_REGNUM
, prev_sp
);
448 store_inferior_registers (-1);
449 flush_cached_frames ();
450 set_current_frame (create_new_frame (prev_sp
, lr
));
454 /* fixup the call sequence of a dummy function, with the real function address.
455 its argumets will be passed by gdb. */
457 fix_call_dummy(dummyname
, pc
, fun
, nargs
, type
)
461 int nargs
; /* not used */
462 int type
; /* not used */
465 #define TOC_ADDR_OFFSET 20
466 #define TARGET_ADDR_OFFSET 28
469 unsigned long target_addr
;
470 unsigned long tocvalue
;
473 tocvalue
= find_toc_address (target_addr
);
475 ii
= *(int*)((char*)dummyname
+ TOC_ADDR_OFFSET
);
476 ii
= (ii
& 0xffff0000) | (tocvalue
>> 16);
477 *(int*)((char*)dummyname
+ TOC_ADDR_OFFSET
) = ii
;
479 ii
= *(int*)((char*)dummyname
+ TOC_ADDR_OFFSET
+4);
480 ii
= (ii
& 0xffff0000) | (tocvalue
& 0x0000ffff);
481 *(int*)((char*)dummyname
+ TOC_ADDR_OFFSET
+4) = ii
;
483 ii
= *(int*)((char*)dummyname
+ TARGET_ADDR_OFFSET
);
484 ii
= (ii
& 0xffff0000) | (target_addr
>> 16);
485 *(int*)((char*)dummyname
+ TARGET_ADDR_OFFSET
) = ii
;
487 ii
= *(int*)((char*)dummyname
+ TARGET_ADDR_OFFSET
+4);
488 ii
= (ii
& 0xffff0000) | (target_addr
& 0x0000ffff);
489 *(int*)((char*)dummyname
+ TARGET_ADDR_OFFSET
+4) = ii
;
494 /* return information about a function frame.
495 - frameless is TRUE, if function does not save %pc value in its frame.
496 - offset is the number of bytes used in the frame to save registers.
497 - saved_gpr is the number of the first saved gpr.
498 - saved_fpr is the number of the first saved fpr.
500 function_frame_info (pc
, frameless
, offset
, saved_gpr
, saved_fpr
)
502 int *frameless
, *offset
, *saved_gpr
, *saved_fpr
;
505 register unsigned int op
;
508 *saved_gpr
= *saved_fpr
= -1;
510 op
= read_memory_integer (pc
, 4);
511 if (op
== 0x7c0802a6) { /* mflr r0 */
513 op
= read_memory_integer (pc
, 4);
516 else /* else, this is a frameless invocation */
520 if ((op
& 0xfc00003e) == 0x7c000026) { /* mfcr Rx */
522 op
= read_memory_integer (pc
, 4);
525 if ((op
& 0xfc000000) == 0x48000000) { /* bl foo, to save fprs??? */
527 op
= read_memory_integer (pc
, 4);
530 if ((op
& 0xfc1f0000) == 0xd8010000) { /* stfd Rx,NUM(r1) */
531 pc
+= 4; /* store floating register double */
532 op
= read_memory_integer (pc
, 4);
535 if ((op
& 0xfc1f0000) == 0xbc010000) { /* stm Rx, NUM(r1) */
537 *saved_gpr
= (op
>> 21) & 0x1f;
540 tmp2
= 0xffff0000 | tmp2
;
544 *saved_fpr
= (tmp2
- ((32 - *saved_gpr
) * 4)) / 8;
546 *saved_fpr
= 32 - *saved_fpr
;
555 /* Pass the arguments in either registers, or in the stack. In RS6000, the first
556 eight words of the argument list (that might be less than eight parameters if
557 some parameters occupy more than one word) are passed in r3..r11 registers.
558 float and double parameters are passed in fpr's, in addition to that. Rest of
559 the parameters if any are passed in user stack. There might be cases in which
560 half of the parameter is copied into registers, the other half is pushed into
563 If the function is returning a structure, then the return address is passed
564 in r3, then the first 7 words of the parametes can be passed in registers,
568 push_arguments (nargs
, args
, sp
, struct_return
, struct_addr
)
573 CORE_ADDR struct_addr
;
576 int argno
; /* current argument number */
577 int argbytes
; /* current argument byte */
578 char tmp_buffer
[50];
580 int f_argno
= 0; /* current floating point argno */
582 CORE_ADDR saved_sp
, pc
;
584 if ( dummy_frame_count
<= 0)
585 printf ("FATAL ERROR -push_arguments()! frame not found!!\n");
587 /* The first eight words of ther arguments are passed in registers. Copy
590 If the function is returning a `struct', then the first word (which
591 will be passed in r3) is used for struct return address. In that
592 case we should advance one word and start from r4 register to copy
595 ii
= struct_return
? 1 : 0;
597 for (argno
=0, argbytes
=0; argno
< nargs
&& ii
<8; ++ii
) {
599 arg
= value_arg_coerce (args
[argno
]);
600 len
= TYPE_LENGTH (VALUE_TYPE (arg
));
602 if (TYPE_CODE (VALUE_TYPE (arg
)) == TYPE_CODE_FLT
) {
604 /* floating point arguments are passed in fpr's, as well as gpr's.
605 There are 13 fpr's reserved for passing parameters. At this point
606 there is no way we would run out of them. */
610 "Fatal Error: a floating point parameter #%d with a size > 8 is found!\n", argno
);
612 bcopy (VALUE_CONTENTS (arg
),
613 ®isters
[REGISTER_BYTE(FP0_REGNUM
+ 1 + f_argno
)], len
);
619 /* Argument takes more than one register. */
620 while (argbytes
< len
) {
622 *(int*)®isters
[REGISTER_BYTE(ii
+3)] = 0;
623 bcopy ( ((char*)VALUE_CONTENTS (arg
))+argbytes
,
624 ®isters
[REGISTER_BYTE(ii
+3)],
625 (len
- argbytes
) > 4 ? 4 : len
- argbytes
);
629 goto ran_out_of_registers_for_arguments
;
634 else { /* Argument can fit in one register. No problem. */
635 *(int*)®isters
[REGISTER_BYTE(ii
+3)] = 0;
636 bcopy (VALUE_CONTENTS (arg
), ®isters
[REGISTER_BYTE(ii
+3)], len
);
641 ran_out_of_registers_for_arguments
:
643 /* location for 8 parameters are always reserved. */
646 /* another six words for back chain, TOC register, link register, etc. */
649 /* if there are more arguments, allocate space for them in
650 the stack, then push them starting from the ninth one. */
652 if ((argno
< nargs
) || argbytes
) {
657 space
+= ((len
- argbytes
+ 3) & -4);
663 for (; jj
< nargs
; ++jj
) {
664 val
= value_arg_coerce (args
[jj
]);
665 space
+= ((TYPE_LENGTH (VALUE_TYPE (val
))) + 3) & -4;
668 /* add location required for the rest of the parameters */
669 space
= (space
+ 7) & -8;
672 /* This is another instance we need to be concerned about securing our
673 stack space. If we write anything underneath %sp (r1), we might conflict
674 with the kernel who thinks he is free to use this area. So, update %sp
675 first before doing anything else. */
677 write_register (SP_REGNUM
, sp
);
681 flush_cached_frames ();
682 set_current_frame (create_new_frame (sp
, pc
));
685 /* if the last argument copied into the registers didn't fit there
686 completely, push the rest of it into stack. */
690 sp
+24+(ii
*4), ((char*)VALUE_CONTENTS (arg
))+argbytes
, len
- argbytes
);
692 ii
+= ((len
- argbytes
+ 3) & -4) / 4;
695 /* push the rest of the arguments into stack. */
696 for (; argno
< nargs
; ++argno
) {
698 arg
= value_arg_coerce (args
[argno
]);
699 len
= TYPE_LENGTH (VALUE_TYPE (arg
));
702 /* float types should be passed in fpr's, as well as in the stack. */
703 if (TYPE_CODE (VALUE_TYPE (arg
)) == TYPE_CODE_FLT
&& f_argno
< 13) {
707 "Fatal Error: a floating point parameter #%d with a size > 8 is found!\n", argno
);
709 bcopy (VALUE_CONTENTS (arg
),
710 ®isters
[REGISTER_BYTE(FP0_REGNUM
+ 1 + f_argno
)], len
);
714 write_memory (sp
+24+(ii
*4), VALUE_CONTENTS (arg
), len
);
715 ii
+= ((len
+ 3) & -4) / 4;
720 /* Secure stack areas first, before doing anything else. */
721 write_register (SP_REGNUM
, sp
);
725 flush_cached_frames ();
726 set_current_frame (create_new_frame (sp
, pc
));
730 saved_sp
= dummy_frame_addr
[dummy_frame_count
- 1];
731 read_memory (saved_sp
, tmp_buffer
, 24);
732 write_memory (sp
, tmp_buffer
, 24);
734 write_memory (sp
, &saved_sp
, 4); /* set back chain properly */
736 store_inferior_registers (-1);
740 /* a given return value in `regbuf' with a type `valtype', extract and copy its
741 value into `valbuf' */
743 extract_return_value (valtype
, regbuf
, valbuf
)
744 struct type
*valtype
;
745 char regbuf
[REGISTER_BYTES
];
749 if (TYPE_CODE (valtype
) == TYPE_CODE_FLT
) {
752 /* floats and doubles are returned in fpr1. fpr's have a size of 8 bytes.
753 We need to truncate the return value into float size (4 byte) if
756 if (TYPE_LENGTH (valtype
) > 4) /* this is a double */
757 bcopy (®buf
[REGISTER_BYTE (FP0_REGNUM
+ 1)], valbuf
,
758 TYPE_LENGTH (valtype
));
760 bcopy (®buf
[REGISTER_BYTE (FP0_REGNUM
+ 1)], &dd
, 8);
762 bcopy (&ff
, valbuf
, sizeof(float));
766 /* return value is copied starting from r3. */
767 bcopy (®buf
[REGISTER_BYTE (3)], valbuf
, TYPE_LENGTH (valtype
));
771 /* keep keep structure return address in this variable. */
773 CORE_ADDR rs6000_struct_return_address
;
776 /* Throw away this debugging code. FIXMEmgo. */
781 for (ii
=0; ii
<40; ++ii
) {
784 val
= read_memory_integer (fram
+ ii
* 4, 4);
785 printf ("0x%08x\t", val
);
792 /* Indirect function calls use a piece of trampoline code do co context switching,
793 i.e. to set the new TOC table. Skip such code if exists. */
795 skip_trampoline_code (pc
)
798 register unsigned int ii
, op
;
800 static unsigned trampoline_code
[] = {
801 0x800b0000, /* l r0,0x0(r11) */
802 0x90410014, /* st r2,0x14(r1) */
803 0x7c0903a6, /* mtctr r0 */
804 0x804b0004, /* l r2,0x4(r11) */
805 0x816b0008, /* l r11,0x8(r11) */
806 0x4e800420, /* bctr */
811 for (ii
=0; trampoline_code
[ii
]; ++ii
) {
812 op
= read_memory_integer (pc
+ (ii
*4), 4);
813 if (op
!= trampoline_code
[ii
])
816 ii
= read_register (11); /* r11 holds destination addr */
817 pc
= read_memory_integer (ii
, 4); /* (r11) value */