1 /* Machine-dependent code which would otherwise be in inflow.c and core.c,
2 for GDB, the GNU debugger. This code is for the HP PA-RISC cpu.
3 Copyright 1986, 1987, 1989, 1990, 1991, 1992 Free Software Foundation, Inc.
5 Contributed by the Center for Software Science at the
6 University of Utah (pa-gdb-bugs@cs.utah.edu).
8 This file is part of GDB.
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 2 of the License, or
13 (at your option) any later version.
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
29 /* For argument passing to the inferior */
33 #include <sys/types.h>
36 #include <sys/param.h>
39 #include <sys/ioctl.h>
41 #ifdef COFF_ENCAPSULATE
42 #include "a.out.encap.h"
47 #define N_SET_MAGIC(exec, val) ((exec).a_magic = (val))
50 /*#include <sys/user.h> After a.out.h */
53 #include <machine/psl.h>
63 /* Routines to extract various sized constants out of hppa
66 /* This assumes that no garbage lies outside of the lower bits of
70 sign_extend (val
, bits
)
73 return (int)(val
>> bits
- 1 ? (-1 << bits
) | val
: val
);
76 /* For many immediate values the sign bit is the low bit! */
79 low_sign_extend (val
, bits
)
82 return (int)((val
& 0x1 ? (-1 << (bits
- 1)) : 0) | val
>> 1);
84 /* extract the immediate field from a ld{bhw}s instruction */
87 get_field (val
, from
, to
)
88 unsigned val
, from
, to
;
91 return val
& ((1 << 32 - from
) - 1);
95 set_field (val
, from
, to
, new_val
)
96 unsigned *val
, from
, to
;
98 unsigned mask
= ~((1 << (to
- from
+ 1)) << (31 - from
));
99 return *val
= *val
& mask
| (new_val
<< (31 - from
));
102 /* extract a 3-bit space register number from a be, ble, mtsp or mfsp */
107 return GET_FIELD (word
, 18, 18) << 2 | GET_FIELD (word
, 16, 17);
110 extract_5_load (word
)
113 return low_sign_extend (word
>> 16 & MASK_5
, 5);
116 /* extract the immediate field from a st{bhw}s instruction */
119 extract_5_store (word
)
122 return low_sign_extend (word
& MASK_5
, 5);
125 /* extract an 11 bit immediate field */
131 return low_sign_extend (word
& MASK_11
, 11);
134 /* extract a 14 bit immediate field */
140 return low_sign_extend (word
& MASK_14
, 14);
143 /* deposit a 14 bit constant in a word */
146 deposit_14 (opnd
, word
)
150 unsigned sign
= (opnd
< 0 ? 1 : 0);
152 return word
| ((unsigned)opnd
<< 1 & MASK_14
) | sign
;
155 /* extract a 21 bit constant */
165 val
= GET_FIELD (word
, 20, 20);
167 val
|= GET_FIELD (word
, 9, 19);
169 val
|= GET_FIELD (word
, 5, 6);
171 val
|= GET_FIELD (word
, 0, 4);
173 val
|= GET_FIELD (word
, 7, 8);
174 return sign_extend (val
, 21) << 11;
177 /* deposit a 21 bit constant in a word. Although 21 bit constants are
178 usually the top 21 bits of a 32 bit constant, we assume that only
179 the low 21 bits of opnd are relevant */
182 deposit_21 (opnd
, word
)
187 val
|= GET_FIELD (opnd
, 11 + 14, 11 + 18);
189 val
|= GET_FIELD (opnd
, 11 + 12, 11 + 13);
191 val
|= GET_FIELD (opnd
, 11 + 19, 11 + 20);
193 val
|= GET_FIELD (opnd
, 11 + 1, 11 + 11);
195 val
|= GET_FIELD (opnd
, 11 + 0, 11 + 0);
199 /* extract a 12 bit constant from branch instructions */
205 return sign_extend (GET_FIELD (word
, 19, 28) |
206 GET_FIELD (word
, 29, 29) << 10 |
207 (word
& 0x1) << 11, 12) << 2;
210 /* extract a 17 bit constant from branch instructions, returning the
211 19 bit signed value. */
217 return sign_extend (GET_FIELD (word
, 19, 28) |
218 GET_FIELD (word
, 29, 29) << 10 |
219 GET_FIELD (word
, 11, 15) << 11 |
220 (word
& 0x1) << 16, 17) << 2;
223 static int use_unwind
= 0;
225 /* Lookup the unwind (stack backtrace) info for the given PC. We search all
226 of the objfiles seeking the unwind table entry for this PC. Each objfile
227 contains a sorted list of struct unwind_table_entry. Since we do a binary
228 search of the unwind tables, we depend upon them to be sorted. */
230 static struct unwind_table_entry
*
231 find_unwind_entry(pc
)
234 int first
, middle
, last
;
235 struct objfile
*objfile
;
237 ALL_OBJFILES (objfile
)
239 struct obj_unwind_info
*ui
;
241 ui
= OBJ_UNWIND_INFO (objfile
);
246 /* First, check the cache */
249 && pc
>= ui
->cache
->region_start
250 && pc
<= ui
->cache
->region_end
)
253 /* Not in the cache, do a binary search */
258 while (first
<= last
)
260 middle
= (first
+ last
) / 2;
261 if (pc
>= ui
->table
[middle
].region_start
262 && pc
<= ui
->table
[middle
].region_end
)
264 ui
->cache
= &ui
->table
[middle
];
265 return &ui
->table
[middle
];
268 if (pc
< ui
->table
[middle
].region_start
)
273 } /* ALL_OBJFILES() */
278 find_return_regnum(pc
)
281 struct unwind_table_entry
*u
;
283 u
= find_unwind_entry (pc
);
295 find_proc_framesize(pc
)
298 struct unwind_table_entry
*u
;
303 u
= find_unwind_entry (pc
);
308 return u
->Total_frame_size
<< 3;
314 struct unwind_table_entry
*u
;
316 u
= find_unwind_entry (pc
);
328 saved_pc_after_call (frame
)
333 ret_regnum
= find_return_regnum (get_frame_pc (frame
));
335 return read_register (ret_regnum
) & ~0x3;
339 frame_saved_pc (frame
)
342 CORE_ADDR pc
= get_frame_pc (frame
);
344 if (frameless_look_for_prologue (frame
))
348 ret_regnum
= find_return_regnum (pc
);
350 return read_register (ret_regnum
) & ~0x3;
352 else if (rp_saved (pc
))
353 return read_memory_integer (frame
->frame
- 20, 4) & ~0x3;
355 return read_register (RP_REGNUM
) & ~0x3;
358 /* We need to correct the PC and the FP for the outermost frame when we are
362 init_extra_frame_info (fromleaf
, frame
)
364 struct frame_info
*frame
;
369 if (frame
->next
) /* Only do this for outermost frame */
372 flags
= read_register (FLAGS_REGNUM
);
373 if (flags
& 2) /* In system call? */
374 frame
->pc
= read_register (31) & ~0x3;
376 /* The outermost frame is always derived from PC-framesize */
377 framesize
= find_proc_framesize(frame
->pc
);
379 frame
->frame
= read_register (FP_REGNUM
);
381 frame
->frame
= read_register (SP_REGNUM
) - framesize
;
383 if (!frameless_look_for_prologue (frame
)) /* Frameless? */
384 return; /* No, quit now */
386 /* For frameless functions, we need to look at the caller's frame */
387 framesize
= find_proc_framesize(FRAME_SAVED_PC(frame
));
389 frame
->frame
-= framesize
;
394 struct frame_info
*frame
;
398 framesize
= find_proc_framesize(FRAME_SAVED_PC(frame
));
401 return frame
->frame
- framesize
;
403 return read_memory_integer (frame
->frame
, 4);
406 /* To see if a frame chain is valid, see if the caller looks like it
407 was compiled with gcc. */
410 frame_chain_valid (chain
, thisframe
)
414 struct minimal_symbol
*msym
;
419 msym
= lookup_minimal_symbol_by_pc (FRAME_SAVED_PC (thisframe
));
422 && (strcmp (SYMBOL_NAME (msym
), "_start") == 0))
429 /* Some helper functions. gcc_p returns 1 if the function beginning at
430 pc appears to have been compiled with gcc. hpux_cc_p returns 1 if
431 fn was compiled with hpux cc. gcc functions look like :
433 stw rp,-0x14(sp) ; optional
436 stwm r1,framesize(sp)
438 hpux cc functions look like:
440 stw rp,-0x14(sp) ; optional.
447 if (read_memory_integer (pc
, 4) == 0x6BC23FD9)
450 if (read_memory_integer (pc
, 4) == 0x8040241
451 && read_memory_integer (pc
+ 4, 4) == 0x81E0244)
458 * These functions deal with saving and restoring register state
459 * around a function call in the inferior. They keep the stack
460 * double-word aligned; eventually, on an hp700, the stack will have
461 * to be aligned to a 64-byte boundary.
467 register CORE_ADDR sp
;
472 /* Space for "arguments"; the RP goes in here. */
473 sp
= read_register (SP_REGNUM
) + 48;
474 int_buffer
= read_register (RP_REGNUM
) | 0x3;
475 write_memory (sp
- 20, (char *)&int_buffer
, 4);
477 int_buffer
= read_register (FP_REGNUM
);
478 write_memory (sp
, (char *)&int_buffer
, 4);
480 write_register (FP_REGNUM
, sp
);
484 for (regnum
= 1; regnum
< 32; regnum
++)
485 if (regnum
!= RP_REGNUM
&& regnum
!= FP_REGNUM
)
486 sp
= push_word (sp
, read_register (regnum
));
490 for (regnum
= FP0_REGNUM
; regnum
< NUM_REGS
; regnum
++)
492 read_register_bytes (REGISTER_BYTE (regnum
), (char *)&freg_buffer
, 8);
493 sp
= push_bytes (sp
, (char *)&freg_buffer
, 8);
495 sp
= push_word (sp
, read_register (IPSW_REGNUM
));
496 sp
= push_word (sp
, read_register (SAR_REGNUM
));
497 sp
= push_word (sp
, read_register (PCOQ_HEAD_REGNUM
));
498 sp
= push_word (sp
, read_register (PCSQ_HEAD_REGNUM
));
499 sp
= push_word (sp
, read_register (PCOQ_TAIL_REGNUM
));
500 sp
= push_word (sp
, read_register (PCSQ_TAIL_REGNUM
));
501 write_register (SP_REGNUM
, sp
);
504 find_dummy_frame_regs (frame
, frame_saved_regs
)
505 struct frame_info
*frame
;
506 struct frame_saved_regs
*frame_saved_regs
;
508 CORE_ADDR fp
= frame
->frame
;
511 frame_saved_regs
->regs
[RP_REGNUM
] = fp
- 20 & ~0x3;
512 frame_saved_regs
->regs
[FP_REGNUM
] = fp
;
513 frame_saved_regs
->regs
[1] = fp
+ 8;
514 frame_saved_regs
->regs
[3] = fp
+ 12;
516 for (fp
+= 16, i
= 5; i
< 32; fp
+= 4, i
++)
517 frame_saved_regs
->regs
[i
] = fp
;
520 for (i
= FP0_REGNUM
; i
< NUM_REGS
; i
++, fp
+= 8)
521 frame_saved_regs
->regs
[i
] = fp
;
523 frame_saved_regs
->regs
[IPSW_REGNUM
] = fp
;
525 frame_saved_regs
->regs
[SAR_REGNUM
] = fp
;
527 frame_saved_regs
->regs
[PCOQ_HEAD_REGNUM
] = fp
;
529 frame_saved_regs
->regs
[PCSQ_HEAD_REGNUM
] = fp
;
531 frame_saved_regs
->regs
[PCOQ_TAIL_REGNUM
] = fp
;
533 frame_saved_regs
->regs
[PCSQ_TAIL_REGNUM
] = fp
;
539 register FRAME frame
= get_current_frame ();
540 register CORE_ADDR fp
;
542 struct frame_saved_regs fsr
;
543 struct frame_info
*fi
;
546 fi
= get_frame_info (frame
);
548 get_frame_saved_regs (fi
, &fsr
);
550 if (fsr
.regs
[IPSW_REGNUM
]) /* Restoring a call dummy frame */
551 hp_restore_pc_queue (&fsr
);
553 for (regnum
= 31; regnum
> 0; regnum
--)
554 if (fsr
.regs
[regnum
])
555 write_register (regnum
, read_memory_integer (fsr
.regs
[regnum
], 4));
557 for (regnum
= NUM_REGS
- 1; regnum
>= FP0_REGNUM
; regnum
--)
558 if (fsr
.regs
[regnum
])
560 read_memory (fsr
.regs
[regnum
], (char *)&freg_buffer
, 8);
561 write_register_bytes (REGISTER_BYTE (regnum
), (char *)&freg_buffer
, 8);
564 if (fsr
.regs
[IPSW_REGNUM
])
565 write_register (IPSW_REGNUM
,
566 read_memory_integer (fsr
.regs
[IPSW_REGNUM
], 4));
568 if (fsr
.regs
[SAR_REGNUM
])
569 write_register (SAR_REGNUM
,
570 read_memory_integer (fsr
.regs
[SAR_REGNUM
], 4));
572 if (fsr
.regs
[PCOQ_TAIL_REGNUM
])
573 write_register (PCOQ_TAIL_REGNUM
,
574 read_memory_integer (fsr
.regs
[PCOQ_TAIL_REGNUM
], 4));
576 write_register (FP_REGNUM
, read_memory_integer (fp
, 4));
578 if (fsr
.regs
[IPSW_REGNUM
]) /* call dummy */
579 write_register (SP_REGNUM
, fp
- 48);
581 write_register (SP_REGNUM
, fp
);
583 flush_cached_frames ();
584 set_current_frame (create_new_frame (read_register (FP_REGNUM
),
589 * After returning to a dummy on the stack, restore the instruction
590 * queue space registers. */
593 hp_restore_pc_queue (fsr
)
594 struct frame_saved_regs
*fsr
;
596 CORE_ADDR pc
= read_pc ();
597 CORE_ADDR new_pc
= read_memory_integer (fsr
->regs
[PCOQ_HEAD_REGNUM
], 4);
602 /* Advance past break instruction in the call dummy. */
603 write_register (PCOQ_HEAD_REGNUM
, pc
+ 4);
604 write_register (PCOQ_TAIL_REGNUM
, pc
+ 8);
607 * HPUX doesn't let us set the space registers or the space
608 * registers of the PC queue through ptrace. Boo, hiss.
609 * Conveniently, the call dummy has this sequence of instructions
614 * So, load up the registers and single step until we are in the
618 write_register (21, read_memory_integer (fsr
->regs
[PCSQ_HEAD_REGNUM
], 4));
619 write_register (22, new_pc
);
621 for (insn_count
= 0; insn_count
< 3; insn_count
++)
628 stop_signal
= WTERMSIG (w
);
629 terminal_ours_for_output ();
630 printf ("\nProgram terminated with signal %d, %s\n",
631 stop_signal
, safe_strsignal (stop_signal
));
636 fetch_inferior_registers (-1);
641 hp_push_arguments (nargs
, args
, sp
, struct_return
, struct_addr
)
646 CORE_ADDR struct_addr
;
648 /* array of arguments' offsets */
649 int *offset
= (int *)alloca(nargs
);
653 for (i
= 0; i
< nargs
; i
++)
655 cum
+= TYPE_LENGTH (VALUE_TYPE (args
[i
]));
657 /* value must go at proper alignment. Assume alignment is a
659 alignment
= hp_alignof (VALUE_TYPE (args
[i
]));
661 cum
= (cum
+ alignment
) & -alignment
;
664 sp
+= min ((cum
+ 7) & -8, 16);
666 for (i
= 0; i
< nargs
; i
++)
667 write_memory (sp
+ offset
[i
], VALUE_CONTENTS (args
[i
]),
668 TYPE_LENGTH (VALUE_TYPE (args
[i
])));
671 write_register (28, struct_addr
);
675 /* return the alignment of a type in bytes. Structures have the maximum
676 alignment required by their fields. */
682 int max_align
, align
, i
;
683 switch (TYPE_CODE (arg
))
688 return TYPE_LENGTH (arg
);
689 case TYPE_CODE_ARRAY
:
690 return hp_alignof (TYPE_FIELD_TYPE (arg
, 0));
691 case TYPE_CODE_STRUCT
:
692 case TYPE_CODE_UNION
:
694 for (i
= 0; i
< TYPE_NFIELDS (arg
); i
++)
696 /* Bit fields have no real alignment. */
697 if (!TYPE_FIELD_BITPOS (arg
, i
))
699 align
= hp_alignof (TYPE_FIELD_TYPE (arg
, i
));
700 max_align
= max (max_align
, align
);
709 /* Print the register regnum, or all registers if regnum is -1 */
711 pa_do_registers_info (regnum
, fpregs
)
715 char raw_regs
[REGISTER_BYTES
];
718 for (i
= 0; i
< NUM_REGS
; i
++)
719 read_relative_register_raw_bytes (i
, raw_regs
+ REGISTER_BYTE (i
));
721 pa_print_registers (raw_regs
, regnum
, fpregs
);
722 else if (regnum
< FP0_REGNUM
)
723 printf ("%s %x\n", reg_names
[regnum
], *(long *)(raw_regs
+
724 REGISTER_BYTE (regnum
)));
726 pa_print_fp_reg (regnum
);
729 pa_print_registers (raw_regs
, regnum
, fpregs
)
736 for (i
= 0; i
< 18; i
++)
737 printf ("%8.8s: %8x %8.8s: %8x %8.8s: %8x %8.8s: %8x\n",
739 *(int *)(raw_regs
+ REGISTER_BYTE (i
)),
741 *(int *)(raw_regs
+ REGISTER_BYTE (i
+ 18)),
743 *(int *)(raw_regs
+ REGISTER_BYTE (i
+ 36)),
745 *(int *)(raw_regs
+ REGISTER_BYTE (i
+ 54)));
748 for (i
= 72; i
< NUM_REGS
; i
++)
755 unsigned char raw_buffer
[MAX_REGISTER_RAW_SIZE
];
756 unsigned char virtual_buffer
[MAX_REGISTER_VIRTUAL_SIZE
];
759 /* Get the data in raw format, then convert also to virtual format. */
760 read_relative_register_raw_bytes (i
, raw_buffer
);
761 REGISTER_CONVERT_TO_VIRTUAL (i
, raw_buffer
, virtual_buffer
);
763 fputs_filtered (reg_names
[i
], stdout
);
764 print_spaces_filtered (15 - strlen (reg_names
[i
]), stdout
);
766 val_print (REGISTER_VIRTUAL_TYPE (i
), virtual_buffer
, 0, stdout
, 0,
767 1, 0, Val_pretty_default
);
768 printf_filtered ("\n");
771 /* Function calls that pass into a new compilation unit must pass through a
772 small piece of code that does long format (`external' in HPPA parlance)
773 jumps. We figure out where the trampoline is going to end up, and return
774 the PC of the final destination. If we aren't in a trampoline, we just
777 For computed calls, we just extract the new PC from r22. */
780 skip_trampoline_code (pc
, name
)
785 static CORE_ADDR dyncall
= 0;
786 struct minimal_symbol
*msym
;
788 /* FIXME XXX - dyncall must be initialized whenever we get a new exec file */
792 msym
= lookup_minimal_symbol ("$$dyncall", NULL
);
794 dyncall
= SYMBOL_VALUE_ADDRESS (msym
);
800 return (CORE_ADDR
)(read_register (22) & ~0x3);
802 inst0
= read_memory_integer (pc
, 4);
803 inst1
= read_memory_integer (pc
+4, 4);
805 if ( (inst0
& 0xffe00000) == 0x20200000 /* ldil xxx, r1 */
806 && (inst1
& 0xffe0e002) == 0xe0202002) /* be,n yyy(sr4, r1) */
807 pc
= extract_21 (inst0
) + extract_17 (inst1
);
809 pc
= (CORE_ADDR
)NULL
;
814 /* Advance PC across any function entry prologue instructions
815 to reach some "real" code. */
817 /* skip (stw rp, -20(0,sp)); copy 4,1; copy sp, 4; stwm 1,framesize(sp)
818 for gcc, or (stw rp, -20(0,sp); stwm 1, framesize(sp) for hcc */
827 status
= target_read_memory (pc
, (char *)&inst
, 4);
828 SWAP_TARGET_AND_HOST (&inst
, sizeof (inst
));
832 if (inst
== 0x6BC23FD9) /* stw rp,-20(sp) */
834 if (read_memory_integer (pc
+ 4, 4) == 0x8040241) /* copy r4,r1 */
836 else if ((read_memory_integer (pc
+ 4, 4) & ~MASK_14
) == 0x68810000) /* stw r1,(r4) */
839 else if (read_memory_integer (pc
, 4) == 0x8040241) /* copy r4,r1 */
841 else if ((read_memory_integer (pc
, 4) & ~MASK_14
) == 0x68810000) /* stw r1,(r4) */
848 unwind_command (exp
, from_tty
)
856 struct unwind_table_entry
*u
;
859 /* If we have an expression, evaluate it and use it as the address. */
861 if (exp
!= 0 && *exp
!= 0)
862 address
= parse_and_eval_address (exp
);
866 xxx
.u
= find_unwind_entry (address
);
870 printf ("Can't find unwind table entry for PC 0x%x\n", address
);
874 printf ("%08x\n%08X\n%08X\n%08X\n", xxx
.foo
[0], xxx
.foo
[1], xxx
.foo
[2],
879 _initialize_hppah_tdep ()
881 add_com ("unwind", class_obscure
, unwind_command
, "Print unwind info\n");
883 (add_set_cmd ("use_unwind", class_obscure
, var_boolean
,
885 "Set the usage of unwind info", &setlist
),