1 /* Dynamic architecture support for GDB, the GNU debugger.
2 Copyright 1998, 1999, 2000, 2001 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., 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
25 #include "inferior.h" /* enum CALL_DUMMY_LOCATION et.al. */
27 /* Just include everything in sight so that the every old definition
28 of macro is visible. */
29 #include "gdb_string.h"
33 #include "breakpoint.h"
41 #include "gdb_assert.h"
45 #include "floatformat.h"
47 /* Use the program counter to determine the contents and size
48 of a breakpoint instruction. If no target-dependent macro
49 BREAKPOINT_FROM_PC has been defined to implement this function,
50 assume that the breakpoint doesn't depend on the PC, and
51 use the values of the BIG_BREAKPOINT and LITTLE_BREAKPOINT macros.
52 Return a pointer to a string of bytes that encode a breakpoint
53 instruction, stores the length of the string to *lenptr,
54 and optionally adjust the pc to point to the correct memory location
55 for inserting the breakpoint. */
58 legacy_breakpoint_from_pc (CORE_ADDR
* pcptr
, int *lenptr
)
60 /* {BIG_,LITTLE_}BREAKPOINT is the sequence of bytes we insert for a
61 breakpoint. On some machines, breakpoints are handled by the
62 target environment and we don't have to worry about them here. */
64 if (TARGET_BYTE_ORDER
== BIG_ENDIAN
)
66 static unsigned char big_break_insn
[] = BIG_BREAKPOINT
;
67 *lenptr
= sizeof (big_break_insn
);
68 return big_break_insn
;
71 #ifdef LITTLE_BREAKPOINT
72 if (TARGET_BYTE_ORDER
!= BIG_ENDIAN
)
74 static unsigned char little_break_insn
[] = LITTLE_BREAKPOINT
;
75 *lenptr
= sizeof (little_break_insn
);
76 return little_break_insn
;
81 static unsigned char break_insn
[] = BREAKPOINT
;
82 *lenptr
= sizeof (break_insn
);
91 generic_frameless_function_invocation_not (struct frame_info
*fi
)
97 generic_return_value_on_stack_not (struct type
*type
)
103 generic_skip_trampoline_code (CORE_ADDR pc
)
109 generic_in_solib_call_trampoline (CORE_ADDR pc
, char *name
)
115 generic_in_function_epilogue_p (struct gdbarch
*gdbarch
, CORE_ADDR pc
)
121 legacy_register_name (int i
)
123 #ifdef REGISTER_NAMES
124 static char *names
[] = REGISTER_NAMES
;
125 if (i
< 0 || i
>= (sizeof (names
) / sizeof (*names
)))
130 internal_error (__FILE__
, __LINE__
,
131 "legacy_register_name: called.");
136 #if defined (CALL_DUMMY)
137 LONGEST legacy_call_dummy_words
[] = CALL_DUMMY
;
139 LONGEST legacy_call_dummy_words
[1];
141 int legacy_sizeof_call_dummy_words
= sizeof (legacy_call_dummy_words
);
144 generic_remote_translate_xfer_address (CORE_ADDR gdb_addr
, int gdb_len
,
145 CORE_ADDR
* rem_addr
, int *rem_len
)
147 *rem_addr
= gdb_addr
;
152 generic_prologue_frameless_p (CORE_ADDR ip
)
154 #ifdef SKIP_PROLOGUE_FRAMELESS_P
155 return ip
== SKIP_PROLOGUE_FRAMELESS_P (ip
);
157 return ip
== SKIP_PROLOGUE (ip
);
161 /* New/multi-arched targets should use the correct gdbarch field
162 instead of using this global pointer. */
164 legacy_print_insn (bfd_vma vma
, disassemble_info
*info
)
166 return (*tm_print_insn
) (vma
, info
);
169 /* Helper functions for INNER_THAN */
172 core_addr_lessthan (CORE_ADDR lhs
, CORE_ADDR rhs
)
178 core_addr_greaterthan (CORE_ADDR lhs
, CORE_ADDR rhs
)
184 /* Helper functions for TARGET_{FLOAT,DOUBLE}_FORMAT */
186 const struct floatformat
*
187 default_float_format (struct gdbarch
*gdbarch
)
190 int byte_order
= gdbarch_byte_order (gdbarch
);
192 int byte_order
= TARGET_BYTE_ORDER
;
197 return &floatformat_ieee_single_big
;
199 return &floatformat_ieee_single_little
;
201 internal_error (__FILE__
, __LINE__
,
202 "default_float_format: bad byte order");
207 const struct floatformat
*
208 default_double_format (struct gdbarch
*gdbarch
)
211 int byte_order
= gdbarch_byte_order (gdbarch
);
213 int byte_order
= TARGET_BYTE_ORDER
;
218 return &floatformat_ieee_double_big
;
220 return &floatformat_ieee_double_little
;
222 internal_error (__FILE__
, __LINE__
,
223 "default_double_format: bad byte order");
227 /* Misc helper functions for targets. */
230 frame_num_args_unknown (struct frame_info
*fi
)
237 generic_register_convertible_not (int num
)
243 /* Under some ABI's that specify the `struct convention' for returning
244 structures by value, by the time we've returned from the function,
245 the return value is sitting there in the caller's buffer, but GDB
246 has no way to find the address of that buffer.
248 On such architectures, use this function as your
249 extract_struct_value_address method. When asked to a struct
250 returned by value in this fashion, GDB will print a nice error
251 message, instead of garbage. */
253 generic_cannot_extract_struct_value_address (char *dummy
)
259 default_register_sim_regno (int num
)
266 core_addr_identity (CORE_ADDR addr
)
272 no_op_reg_to_regnum (int reg
)
277 /* For use by frame_args_address and frame_locals_address. */
279 default_frame_address (struct frame_info
*fi
)
284 /* Default prepare_to_procced(). */
286 default_prepare_to_proceed (int select_it
)
291 /* Generic prepare_to_proceed(). This one should be suitable for most
292 targets that support threads. */
294 generic_prepare_to_proceed (int select_it
)
297 struct target_waitstatus wait_status
;
299 /* Get the last target status returned by target_wait(). */
300 get_last_target_status (&wait_ptid
, &wait_status
);
302 /* Make sure we were stopped either at a breakpoint, or because
304 if (wait_status
.kind
!= TARGET_WAITKIND_STOPPED
305 || (wait_status
.value
.sig
!= TARGET_SIGNAL_TRAP
&&
306 wait_status
.value
.sig
!= TARGET_SIGNAL_INT
))
311 if (!ptid_equal (wait_ptid
, minus_one_ptid
)
312 && !ptid_equal (inferior_ptid
, wait_ptid
))
314 /* Switched over from WAIT_PID. */
315 CORE_ADDR wait_pc
= read_pc_pid (wait_ptid
);
317 if (wait_pc
!= read_pc ())
321 /* Switch back to WAIT_PID thread. */
322 inferior_ptid
= wait_ptid
;
324 /* FIXME: This stuff came from switch_to_thread() in
325 thread.c (which should probably be a public function). */
326 flush_cached_frames ();
327 registers_changed ();
329 select_frame (get_current_frame (), 0);
331 /* We return 1 to indicate that there is a breakpoint here,
332 so we need to step over it before continuing to avoid
333 hitting it straight away. */
334 if (breakpoint_here_p (wait_pc
))
345 init_frame_pc_noop (int fromleaf
, struct frame_info
*prev
)
351 init_frame_pc_default (int fromleaf
, struct frame_info
*prev
)
354 prev
->pc
= SAVED_PC_AFTER_CALL (prev
->next
);
355 else if (prev
->next
!= NULL
)
356 prev
->pc
= FRAME_SAVED_PC (prev
->next
);
358 prev
->pc
= read_pc ();
362 cannot_register_not (int regnum
)
367 /* Legacy version of target_virtual_frame_pointer(). Assumes that
368 there is an FP_REGNUM and that it is the same, cooked or raw. */
371 legacy_virtual_frame_pointer (CORE_ADDR pc
,
373 LONGEST
*frame_offset
)
375 gdb_assert (FP_REGNUM
>= 0);
376 *frame_regnum
= FP_REGNUM
;
380 /* Assume the world is flat. Every register is large enough to fit a
384 generic_register_raw_size (int regnum
)
386 gdb_assert (regnum
>= 0 && regnum
< NUM_REGS
+ NUM_PSEUDO_REGS
);
387 return TARGET_INT_BIT
/ HOST_CHAR_BIT
;
391 /* Functions to manipulate the endianness of the target. */
393 #ifdef TARGET_BYTE_ORDER_SELECTABLE
394 /* compat - Catch old targets that expect a selectable byte-order to
395 default to BIG_ENDIAN */
396 #ifndef TARGET_BYTE_ORDER_DEFAULT
397 #define TARGET_BYTE_ORDER_DEFAULT BIG_ENDIAN
400 #if !TARGET_BYTE_ORDER_SELECTABLE_P
401 #ifndef TARGET_BYTE_ORDER_DEFAULT
402 /* compat - Catch old non byte-order selectable targets that do not
403 define TARGET_BYTE_ORDER_DEFAULT and instead expect
404 TARGET_BYTE_ORDER to be used as the default. For targets that
405 defined neither TARGET_BYTE_ORDER nor TARGET_BYTE_ORDER_DEFAULT the
406 below will get a strange compiler warning. */
407 #define TARGET_BYTE_ORDER_DEFAULT TARGET_BYTE_ORDER
410 #ifndef TARGET_BYTE_ORDER_DEFAULT
411 #define TARGET_BYTE_ORDER_DEFAULT BIG_ENDIAN /* arbitrary */
413 /* ``target_byte_order'' is only used when non- multi-arch.
414 Multi-arch targets obtain the current byte order using
415 TARGET_BYTE_ORDER which is controlled by gdbarch.*. */
416 int target_byte_order
= TARGET_BYTE_ORDER_DEFAULT
;
417 int target_byte_order_auto
= 1;
419 static const char endian_big
[] = "big";
420 static const char endian_little
[] = "little";
421 static const char endian_auto
[] = "auto";
422 static const char *endian_enum
[] =
429 static const char *set_endian_string
;
431 /* Called by ``show endian''. */
434 show_endian (char *args
, int from_tty
)
436 if (TARGET_BYTE_ORDER_AUTO
)
437 printf_unfiltered ("The target endianness is set automatically (currently %s endian)\n",
438 (TARGET_BYTE_ORDER
== BIG_ENDIAN
? "big" : "little"));
440 printf_unfiltered ("The target is assumed to be %s endian\n",
441 (TARGET_BYTE_ORDER
== BIG_ENDIAN
? "big" : "little"));
445 set_endian (char *ignore_args
, int from_tty
, struct cmd_list_element
*c
)
447 if (!TARGET_BYTE_ORDER_SELECTABLE_P
)
449 printf_unfiltered ("Byte order is not selectable.");
451 else if (set_endian_string
== endian_auto
)
453 target_byte_order_auto
= 1;
455 else if (set_endian_string
== endian_little
)
457 target_byte_order_auto
= 0;
460 struct gdbarch_info info
;
461 memset (&info
, 0, sizeof info
);
462 info
.byte_order
= LITTLE_ENDIAN
;
463 if (! gdbarch_update_p (info
))
465 printf_unfiltered ("Little endian target not supported by GDB\n");
470 target_byte_order
= LITTLE_ENDIAN
;
473 else if (set_endian_string
== endian_big
)
475 target_byte_order_auto
= 0;
478 struct gdbarch_info info
;
479 memset (&info
, 0, sizeof info
);
480 info
.byte_order
= BIG_ENDIAN
;
481 if (! gdbarch_update_p (info
))
483 printf_unfiltered ("Big endian target not supported by GDB\n");
488 target_byte_order
= BIG_ENDIAN
;
492 internal_error (__FILE__
, __LINE__
,
493 "set_endian: bad value");
494 show_endian (NULL
, from_tty
);
497 /* Set the endianness from a BFD. */
500 set_endian_from_file (bfd
*abfd
)
503 internal_error (__FILE__
, __LINE__
,
504 "set_endian_from_file: not for multi-arch");
505 if (TARGET_BYTE_ORDER_SELECTABLE_P
)
509 if (bfd_big_endian (abfd
))
512 want
= LITTLE_ENDIAN
;
513 if (TARGET_BYTE_ORDER_AUTO
)
514 target_byte_order
= want
;
515 else if (TARGET_BYTE_ORDER
!= want
)
516 warning ("%s endian file does not match %s endian target.",
517 want
== BIG_ENDIAN
? "big" : "little",
518 TARGET_BYTE_ORDER
== BIG_ENDIAN
? "big" : "little");
522 if (bfd_big_endian (abfd
)
523 ? TARGET_BYTE_ORDER
!= BIG_ENDIAN
524 : TARGET_BYTE_ORDER
== BIG_ENDIAN
)
525 warning ("%s endian file does not match %s endian target.",
526 bfd_big_endian (abfd
) ? "big" : "little",
527 TARGET_BYTE_ORDER
== BIG_ENDIAN
? "big" : "little");
532 /* Functions to manipulate the architecture of the target */
534 enum set_arch
{ set_arch_auto
, set_arch_manual
};
536 int target_architecture_auto
= 1;
538 const char *set_architecture_string
;
540 /* Old way of changing the current architecture. */
542 extern const struct bfd_arch_info bfd_default_arch_struct
;
543 const struct bfd_arch_info
*target_architecture
= &bfd_default_arch_struct
;
544 int (*target_architecture_hook
) (const struct bfd_arch_info
*ap
);
547 arch_ok (const struct bfd_arch_info
*arch
)
550 internal_error (__FILE__
, __LINE__
,
551 "arch_ok: not multi-arched");
552 /* Should be performing the more basic check that the binary is
553 compatible with GDB. */
554 /* Check with the target that the architecture is valid. */
555 return (target_architecture_hook
== NULL
556 || target_architecture_hook (arch
));
560 set_arch (const struct bfd_arch_info
*arch
,
564 internal_error (__FILE__
, __LINE__
,
565 "set_arch: not multi-arched");
570 warning ("Target may not support %s architecture",
571 arch
->printable_name
);
572 target_architecture
= arch
;
574 case set_arch_manual
:
577 printf_unfiltered ("Target does not support `%s' architecture.\n",
578 arch
->printable_name
);
582 target_architecture_auto
= 0;
583 target_architecture
= arch
;
588 gdbarch_dump (current_gdbarch
, gdb_stdlog
);
591 /* Set the architecture from arch/machine (deprecated) */
594 set_architecture_from_arch_mach (enum bfd_architecture arch
,
597 const struct bfd_arch_info
*wanted
= bfd_lookup_arch (arch
, mach
);
599 internal_error (__FILE__
, __LINE__
,
600 "set_architecture_from_arch_mach: not multi-arched");
602 set_arch (wanted
, set_arch_manual
);
604 internal_error (__FILE__
, __LINE__
,
605 "gdbarch: hardwired architecture/machine not recognized");
608 /* Set the architecture from a BFD (deprecated) */
611 set_architecture_from_file (bfd
*abfd
)
613 const struct bfd_arch_info
*wanted
= bfd_get_arch_info (abfd
);
615 internal_error (__FILE__
, __LINE__
,
616 "set_architecture_from_file: not multi-arched");
617 if (target_architecture_auto
)
619 set_arch (wanted
, set_arch_auto
);
621 else if (wanted
!= target_architecture
)
623 warning ("%s architecture file may be incompatible with %s target.",
624 wanted
->printable_name
,
625 target_architecture
->printable_name
);
630 /* Called if the user enters ``show architecture'' without an
634 show_architecture (char *args
, int from_tty
)
637 arch
= TARGET_ARCHITECTURE
->printable_name
;
638 if (target_architecture_auto
)
639 printf_filtered ("The target architecture is set automatically (currently %s)\n", arch
);
641 printf_filtered ("The target architecture is assumed to be %s\n", arch
);
645 /* Called if the user enters ``set architecture'' with or without an
649 set_architecture (char *ignore_args
, int from_tty
, struct cmd_list_element
*c
)
651 if (strcmp (set_architecture_string
, "auto") == 0)
653 target_architecture_auto
= 1;
655 else if (GDB_MULTI_ARCH
)
657 struct gdbarch_info info
;
658 memset (&info
, 0, sizeof info
);
659 info
.bfd_arch_info
= bfd_scan_arch (set_architecture_string
);
660 if (info
.bfd_arch_info
== NULL
)
661 internal_error (__FILE__
, __LINE__
,
662 "set_architecture: bfd_scan_arch failed");
663 if (gdbarch_update_p (info
))
664 target_architecture_auto
= 0;
666 printf_unfiltered ("Architecture `%s' not recognized.\n",
667 set_architecture_string
);
671 const struct bfd_arch_info
*arch
672 = bfd_scan_arch (set_architecture_string
);
674 internal_error (__FILE__
, __LINE__
,
675 "set_architecture: bfd_scan_arch failed");
676 set_arch (arch
, set_arch_manual
);
678 show_architecture (NULL
, from_tty
);
681 /* Set the dynamic target-system-dependent parameters (architecture,
682 byte-order) using information found in the BFD */
685 set_gdbarch_from_file (bfd
*abfd
)
689 struct gdbarch_info info
;
690 memset (&info
, 0, sizeof info
);
692 if (! gdbarch_update_p (info
))
693 error ("Architecture of file not recognized.\n");
697 set_architecture_from_file (abfd
);
698 set_endian_from_file (abfd
);
702 /* Initialize the current architecture. Update the ``set
703 architecture'' command so that it specifies a list of valid
706 #ifdef DEFAULT_BFD_ARCH
707 extern const bfd_arch_info_type DEFAULT_BFD_ARCH
;
708 static const bfd_arch_info_type
*default_bfd_arch
= &DEFAULT_BFD_ARCH
;
710 static const bfd_arch_info_type
*default_bfd_arch
;
713 #ifdef DEFAULT_BFD_VEC
714 extern const bfd_target DEFAULT_BFD_VEC
;
715 static const bfd_target
*default_bfd_vec
= &DEFAULT_BFD_VEC
;
717 static const bfd_target
*default_bfd_vec
;
721 initialize_current_architecture (void)
723 const char **arches
= gdbarch_printable_names ();
725 /* determine a default architecture and byte order. */
726 struct gdbarch_info info
;
727 memset (&info
, 0, sizeof (info
));
729 /* Find a default architecture. */
730 if (info
.bfd_arch_info
== NULL
731 && default_bfd_arch
!= NULL
)
732 info
.bfd_arch_info
= default_bfd_arch
;
733 if (info
.bfd_arch_info
== NULL
)
735 /* Choose the architecture by taking the first one
737 const char *chosen
= arches
[0];
739 for (arch
= arches
; *arch
!= NULL
; arch
++)
741 if (strcmp (*arch
, chosen
) < 0)
745 internal_error (__FILE__
, __LINE__
,
746 "initialize_current_architecture: No arch");
747 info
.bfd_arch_info
= bfd_scan_arch (chosen
);
748 if (info
.bfd_arch_info
== NULL
)
749 internal_error (__FILE__
, __LINE__
,
750 "initialize_current_architecture: Arch not found");
753 /* take several guesses at a byte order. */
754 /* NB: can't use TARGET_BYTE_ORDER_DEFAULT as its definition is
756 if (info
.byte_order
== 0
757 && default_bfd_vec
!= NULL
)
759 /* Extract BFD's default vector's byte order. */
760 switch (default_bfd_vec
->byteorder
)
763 info
.byte_order
= BIG_ENDIAN
;
765 case BFD_ENDIAN_LITTLE
:
766 info
.byte_order
= LITTLE_ENDIAN
;
772 if (info
.byte_order
== 0)
774 /* look for ``*el-*'' in the target name. */
776 chp
= strchr (target_name
, '-');
778 && chp
- 2 >= target_name
779 && strncmp (chp
- 2, "el", 2) == 0)
780 info
.byte_order
= LITTLE_ENDIAN
;
782 if (info
.byte_order
== 0)
784 /* Wire it to big-endian!!! */
785 info
.byte_order
= BIG_ENDIAN
;
790 if (! gdbarch_update_p (info
))
792 internal_error (__FILE__
, __LINE__
,
793 "initialize_current_architecture: Selection of initial architecture failed");
797 initialize_non_multiarch ();
799 /* Create the ``set architecture'' command appending ``auto'' to the
800 list of architectures. */
802 struct cmd_list_element
*c
;
803 /* Append ``auto''. */
805 for (nr
= 0; arches
[nr
] != NULL
; nr
++);
806 arches
= xrealloc (arches
, sizeof (char*) * (nr
+ 2));
807 arches
[nr
+ 0] = "auto";
808 arches
[nr
+ 1] = NULL
;
809 /* FIXME: add_set_enum_cmd() uses an array of ``char *'' instead
810 of ``const char *''. We just happen to know that the casts are
812 c
= add_set_enum_cmd ("architecture", class_support
,
813 arches
, &set_architecture_string
,
814 "Set architecture of target.",
816 c
->function
.sfunc
= set_architecture
;
817 add_alias_cmd ("processor", "architecture", class_support
, 1, &setlist
);
818 /* Don't use set_from_show - need to print both auto/manual and
820 add_cmd ("architecture", class_support
, show_architecture
,
821 "Show the current target architecture", &showlist
);
828 extern initialize_file_ftype _initialize_gdbarch_utils
;
831 _initialize_gdbarch_utils (void)
833 struct cmd_list_element
*c
;
834 c
= add_set_enum_cmd ("endian", class_support
,
835 endian_enum
, &set_endian_string
,
836 "Set endianness of target.",
838 c
->function
.sfunc
= set_endian
;
839 /* Don't use set_from_show - need to print both auto/manual and
841 add_cmd ("endian", class_support
, show_endian
,
842 "Show the current byte-order", &showlist
);