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 legacy_register_name (int i
)
105 #ifdef REGISTER_NAMES
106 static char *names
[] = REGISTER_NAMES
;
107 if (i
< 0 || i
>= (sizeof (names
) / sizeof (*names
)))
112 internal_error (__FILE__
, __LINE__
,
113 "legacy_register_name: called.");
118 #if defined (CALL_DUMMY)
119 LONGEST legacy_call_dummy_words
[] = CALL_DUMMY
;
121 LONGEST legacy_call_dummy_words
[1];
123 int legacy_sizeof_call_dummy_words
= sizeof (legacy_call_dummy_words
);
126 generic_remote_translate_xfer_address (CORE_ADDR gdb_addr
, int gdb_len
,
127 CORE_ADDR
* rem_addr
, int *rem_len
)
129 *rem_addr
= gdb_addr
;
134 generic_prologue_frameless_p (CORE_ADDR ip
)
136 #ifdef SKIP_PROLOGUE_FRAMELESS_P
137 return ip
== SKIP_PROLOGUE_FRAMELESS_P (ip
);
139 return ip
== SKIP_PROLOGUE (ip
);
144 /* Helper functions for INNER_THAN */
147 core_addr_lessthan (CORE_ADDR lhs
, CORE_ADDR rhs
)
153 core_addr_greaterthan (CORE_ADDR lhs
, CORE_ADDR rhs
)
159 /* Helper functions for TARGET_{FLOAT,DOUBLE}_FORMAT */
161 const struct floatformat
*
162 default_float_format (struct gdbarch
*gdbarch
)
165 int byte_order
= gdbarch_byte_order (gdbarch
);
167 int byte_order
= TARGET_BYTE_ORDER
;
172 return &floatformat_ieee_single_big
;
174 return &floatformat_ieee_single_little
;
176 internal_error (__FILE__
, __LINE__
,
177 "default_float_format: bad byte order");
182 const struct floatformat
*
183 default_double_format (struct gdbarch
*gdbarch
)
186 int byte_order
= gdbarch_byte_order (gdbarch
);
188 int byte_order
= TARGET_BYTE_ORDER
;
193 return &floatformat_ieee_double_big
;
195 return &floatformat_ieee_double_little
;
197 internal_error (__FILE__
, __LINE__
,
198 "default_double_format: bad byte order");
202 /* Misc helper functions for targets. */
205 frame_num_args_unknown (struct frame_info
*fi
)
212 generic_register_convertible_not (int num
)
219 default_register_sim_regno (int num
)
226 core_addr_identity (CORE_ADDR addr
)
232 no_op_reg_to_regnum (int reg
)
237 /* For use by frame_args_address and frame_locals_address. */
239 default_frame_address (struct frame_info
*fi
)
244 /* Default prepare_to_procced(). */
246 default_prepare_to_proceed (int select_it
)
251 /* Generic prepare_to_proceed(). This one should be suitable for most
252 targets that support threads. */
254 generic_prepare_to_proceed (int select_it
)
257 struct target_waitstatus wait_status
;
259 /* Get the last target status returned by target_wait(). */
260 get_last_target_status (&wait_ptid
, &wait_status
);
262 /* Make sure we were stopped either at a breakpoint, or because
264 if (wait_status
.kind
!= TARGET_WAITKIND_STOPPED
265 || (wait_status
.value
.sig
!= TARGET_SIGNAL_TRAP
&&
266 wait_status
.value
.sig
!= TARGET_SIGNAL_INT
))
271 if (!ptid_equal (wait_ptid
, minus_one_ptid
)
272 && !ptid_equal (inferior_ptid
, wait_ptid
))
274 /* Switched over from WAIT_PID. */
275 CORE_ADDR wait_pc
= read_pc_pid (wait_ptid
);
277 if (wait_pc
!= read_pc ())
281 /* Switch back to WAIT_PID thread. */
282 inferior_ptid
= wait_ptid
;
284 /* FIXME: This stuff came from switch_to_thread() in
285 thread.c (which should probably be a public function). */
286 flush_cached_frames ();
287 registers_changed ();
289 select_frame (get_current_frame (), 0);
291 /* We return 1 to indicate that there is a breakpoint here,
292 so we need to step over it before continuing to avoid
293 hitting it straight away. */
294 if (breakpoint_here_p (wait_pc
))
305 init_frame_pc_noop (int fromleaf
, struct frame_info
*prev
)
311 init_frame_pc_default (int fromleaf
, struct frame_info
*prev
)
314 prev
->pc
= SAVED_PC_AFTER_CALL (prev
->next
);
315 else if (prev
->next
!= NULL
)
316 prev
->pc
= FRAME_SAVED_PC (prev
->next
);
318 prev
->pc
= read_pc ();
322 cannot_register_not (int regnum
)
327 /* Legacy version of target_virtual_frame_pointer(). Assumes that
328 there is an FP_REGNUM and that it is the same, cooked or raw. */
331 legacy_virtual_frame_pointer (CORE_ADDR pc
,
333 LONGEST
*frame_offset
)
335 gdb_assert (FP_REGNUM
>= 0);
336 *frame_regnum
= FP_REGNUM
;
340 /* Functions to manipulate the endianness of the target. */
342 #ifdef TARGET_BYTE_ORDER_SELECTABLE
343 /* compat - Catch old targets that expect a selectable byte-order to
344 default to BIG_ENDIAN */
345 #ifndef TARGET_BYTE_ORDER_DEFAULT
346 #define TARGET_BYTE_ORDER_DEFAULT BIG_ENDIAN
349 #if !TARGET_BYTE_ORDER_SELECTABLE_P
350 #ifndef TARGET_BYTE_ORDER_DEFAULT
351 /* compat - Catch old non byte-order selectable targets that do not
352 define TARGET_BYTE_ORDER_DEFAULT and instead expect
353 TARGET_BYTE_ORDER to be used as the default. For targets that
354 defined neither TARGET_BYTE_ORDER nor TARGET_BYTE_ORDER_DEFAULT the
355 below will get a strange compiler warning. */
356 #define TARGET_BYTE_ORDER_DEFAULT TARGET_BYTE_ORDER
359 #ifndef TARGET_BYTE_ORDER_DEFAULT
360 #define TARGET_BYTE_ORDER_DEFAULT BIG_ENDIAN /* arbitrary */
362 /* ``target_byte_order'' is only used when non- multi-arch.
363 Multi-arch targets obtain the current byte order using
364 TARGET_BYTE_ORDER which is controlled by gdbarch.*. */
365 int target_byte_order
= TARGET_BYTE_ORDER_DEFAULT
;
366 int target_byte_order_auto
= 1;
368 static const char endian_big
[] = "big";
369 static const char endian_little
[] = "little";
370 static const char endian_auto
[] = "auto";
371 static const char *endian_enum
[] =
378 static const char *set_endian_string
;
380 /* Called by ``show endian''. */
383 show_endian (char *args
, int from_tty
)
385 if (TARGET_BYTE_ORDER_AUTO
)
386 printf_unfiltered ("The target endianness is set automatically (currently %s endian)\n",
387 (TARGET_BYTE_ORDER
== BIG_ENDIAN
? "big" : "little"));
389 printf_unfiltered ("The target is assumed to be %s endian\n",
390 (TARGET_BYTE_ORDER
== BIG_ENDIAN
? "big" : "little"));
394 set_endian (char *ignore_args
, int from_tty
, struct cmd_list_element
*c
)
396 if (!TARGET_BYTE_ORDER_SELECTABLE_P
)
398 printf_unfiltered ("Byte order is not selectable.");
400 else if (set_endian_string
== endian_auto
)
402 target_byte_order_auto
= 1;
404 else if (set_endian_string
== endian_little
)
406 target_byte_order_auto
= 0;
409 struct gdbarch_info info
;
410 memset (&info
, 0, sizeof info
);
411 info
.byte_order
= LITTLE_ENDIAN
;
412 if (! gdbarch_update_p (info
))
414 printf_unfiltered ("Little endian target not supported by GDB\n");
419 target_byte_order
= LITTLE_ENDIAN
;
422 else if (set_endian_string
== endian_big
)
424 target_byte_order_auto
= 0;
427 struct gdbarch_info info
;
428 memset (&info
, 0, sizeof info
);
429 info
.byte_order
= BIG_ENDIAN
;
430 if (! gdbarch_update_p (info
))
432 printf_unfiltered ("Big endian target not supported by GDB\n");
437 target_byte_order
= BIG_ENDIAN
;
441 internal_error (__FILE__
, __LINE__
,
442 "set_endian: bad value");
443 show_endian (NULL
, from_tty
);
446 /* Set the endianness from a BFD. */
449 set_endian_from_file (bfd
*abfd
)
452 internal_error (__FILE__
, __LINE__
,
453 "set_endian_from_file: not for multi-arch");
454 if (TARGET_BYTE_ORDER_SELECTABLE_P
)
458 if (bfd_big_endian (abfd
))
461 want
= LITTLE_ENDIAN
;
462 if (TARGET_BYTE_ORDER_AUTO
)
463 target_byte_order
= want
;
464 else if (TARGET_BYTE_ORDER
!= want
)
465 warning ("%s endian file does not match %s endian target.",
466 want
== BIG_ENDIAN
? "big" : "little",
467 TARGET_BYTE_ORDER
== BIG_ENDIAN
? "big" : "little");
471 if (bfd_big_endian (abfd
)
472 ? TARGET_BYTE_ORDER
!= BIG_ENDIAN
473 : TARGET_BYTE_ORDER
== BIG_ENDIAN
)
474 warning ("%s endian file does not match %s endian target.",
475 bfd_big_endian (abfd
) ? "big" : "little",
476 TARGET_BYTE_ORDER
== BIG_ENDIAN
? "big" : "little");
481 /* Functions to manipulate the architecture of the target */
483 enum set_arch
{ set_arch_auto
, set_arch_manual
};
485 int target_architecture_auto
= 1;
487 const char *set_architecture_string
;
489 /* Old way of changing the current architecture. */
491 extern const struct bfd_arch_info bfd_default_arch_struct
;
492 const struct bfd_arch_info
*target_architecture
= &bfd_default_arch_struct
;
493 int (*target_architecture_hook
) (const struct bfd_arch_info
*ap
);
496 arch_ok (const struct bfd_arch_info
*arch
)
499 internal_error (__FILE__
, __LINE__
,
500 "arch_ok: not multi-arched");
501 /* Should be performing the more basic check that the binary is
502 compatible with GDB. */
503 /* Check with the target that the architecture is valid. */
504 return (target_architecture_hook
== NULL
505 || target_architecture_hook (arch
));
509 set_arch (const struct bfd_arch_info
*arch
,
513 internal_error (__FILE__
, __LINE__
,
514 "set_arch: not multi-arched");
519 warning ("Target may not support %s architecture",
520 arch
->printable_name
);
521 target_architecture
= arch
;
523 case set_arch_manual
:
526 printf_unfiltered ("Target does not support `%s' architecture.\n",
527 arch
->printable_name
);
531 target_architecture_auto
= 0;
532 target_architecture
= arch
;
537 gdbarch_dump (current_gdbarch
, gdb_stdlog
);
540 /* Set the architecture from arch/machine (deprecated) */
543 set_architecture_from_arch_mach (enum bfd_architecture arch
,
546 const struct bfd_arch_info
*wanted
= bfd_lookup_arch (arch
, mach
);
548 internal_error (__FILE__
, __LINE__
,
549 "set_architecture_from_arch_mach: not multi-arched");
551 set_arch (wanted
, set_arch_manual
);
553 internal_error (__FILE__
, __LINE__
,
554 "gdbarch: hardwired architecture/machine not recognized");
557 /* Set the architecture from a BFD (deprecated) */
560 set_architecture_from_file (bfd
*abfd
)
562 const struct bfd_arch_info
*wanted
= bfd_get_arch_info (abfd
);
564 internal_error (__FILE__
, __LINE__
,
565 "set_architecture_from_file: not multi-arched");
566 if (target_architecture_auto
)
568 set_arch (wanted
, set_arch_auto
);
570 else if (wanted
!= target_architecture
)
572 warning ("%s architecture file may be incompatible with %s target.",
573 wanted
->printable_name
,
574 target_architecture
->printable_name
);
579 /* Called if the user enters ``show architecture'' without an
583 show_architecture (char *args
, int from_tty
)
586 arch
= TARGET_ARCHITECTURE
->printable_name
;
587 if (target_architecture_auto
)
588 printf_filtered ("The target architecture is set automatically (currently %s)\n", arch
);
590 printf_filtered ("The target architecture is assumed to be %s\n", arch
);
594 /* Called if the user enters ``set architecture'' with or without an
598 set_architecture (char *ignore_args
, int from_tty
, struct cmd_list_element
*c
)
600 if (strcmp (set_architecture_string
, "auto") == 0)
602 target_architecture_auto
= 1;
604 else if (GDB_MULTI_ARCH
)
606 struct gdbarch_info info
;
607 memset (&info
, 0, sizeof info
);
608 info
.bfd_arch_info
= bfd_scan_arch (set_architecture_string
);
609 if (info
.bfd_arch_info
== NULL
)
610 internal_error (__FILE__
, __LINE__
,
611 "set_architecture: bfd_scan_arch failed");
612 if (gdbarch_update_p (info
))
613 target_architecture_auto
= 0;
615 printf_unfiltered ("Architecture `%s' not recognized.\n",
616 set_architecture_string
);
620 const struct bfd_arch_info
*arch
621 = bfd_scan_arch (set_architecture_string
);
623 internal_error (__FILE__
, __LINE__
,
624 "set_architecture: bfd_scan_arch failed");
625 set_arch (arch
, set_arch_manual
);
627 show_architecture (NULL
, from_tty
);
630 /* Set the dynamic target-system-dependent parameters (architecture,
631 byte-order) using information found in the BFD */
634 set_gdbarch_from_file (bfd
*abfd
)
638 struct gdbarch_info info
;
639 memset (&info
, 0, sizeof info
);
641 if (! gdbarch_update_p (info
))
642 error ("Architecture of file not recognized.\n");
646 set_architecture_from_file (abfd
);
647 set_endian_from_file (abfd
);
651 /* Initialize the current architecture. Update the ``set
652 architecture'' command so that it specifies a list of valid
655 #ifdef DEFAULT_BFD_ARCH
656 extern const bfd_arch_info_type DEFAULT_BFD_ARCH
;
657 static const bfd_arch_info_type
*default_bfd_arch
= &DEFAULT_BFD_ARCH
;
659 static const bfd_arch_info_type
*default_bfd_arch
;
662 #ifdef DEFAULT_BFD_VEC
663 extern const bfd_target DEFAULT_BFD_VEC
;
664 static const bfd_target
*default_bfd_vec
= &DEFAULT_BFD_VEC
;
666 static const bfd_target
*default_bfd_vec
;
670 initialize_current_architecture (void)
672 const char **arches
= gdbarch_printable_names ();
674 /* determine a default architecture and byte order. */
675 struct gdbarch_info info
;
676 memset (&info
, 0, sizeof (info
));
678 /* Find a default architecture. */
679 if (info
.bfd_arch_info
== NULL
680 && default_bfd_arch
!= NULL
)
681 info
.bfd_arch_info
= default_bfd_arch
;
682 if (info
.bfd_arch_info
== NULL
)
684 /* Choose the architecture by taking the first one
686 const char *chosen
= arches
[0];
688 for (arch
= arches
; *arch
!= NULL
; arch
++)
690 if (strcmp (*arch
, chosen
) < 0)
694 internal_error (__FILE__
, __LINE__
,
695 "initialize_current_architecture: No arch");
696 info
.bfd_arch_info
= bfd_scan_arch (chosen
);
697 if (info
.bfd_arch_info
== NULL
)
698 internal_error (__FILE__
, __LINE__
,
699 "initialize_current_architecture: Arch not found");
702 /* take several guesses at a byte order. */
703 /* NB: can't use TARGET_BYTE_ORDER_DEFAULT as its definition is
705 if (info
.byte_order
== 0
706 && default_bfd_vec
!= NULL
)
708 /* Extract BFD's default vector's byte order. */
709 switch (default_bfd_vec
->byteorder
)
712 info
.byte_order
= BIG_ENDIAN
;
714 case BFD_ENDIAN_LITTLE
:
715 info
.byte_order
= LITTLE_ENDIAN
;
721 if (info
.byte_order
== 0)
723 /* look for ``*el-*'' in the target name. */
725 chp
= strchr (target_name
, '-');
727 && chp
- 2 >= target_name
728 && strncmp (chp
- 2, "el", 2) == 0)
729 info
.byte_order
= LITTLE_ENDIAN
;
731 if (info
.byte_order
== 0)
733 /* Wire it to big-endian!!! */
734 info
.byte_order
= BIG_ENDIAN
;
739 if (! gdbarch_update_p (info
))
741 internal_error (__FILE__
, __LINE__
,
742 "initialize_current_architecture: Selection of initial architecture failed");
746 initialize_non_multiarch ();
748 /* Create the ``set architecture'' command appending ``auto'' to the
749 list of architectures. */
751 struct cmd_list_element
*c
;
752 /* Append ``auto''. */
754 for (nr
= 0; arches
[nr
] != NULL
; nr
++);
755 arches
= xrealloc (arches
, sizeof (char*) * (nr
+ 2));
756 arches
[nr
+ 0] = "auto";
757 arches
[nr
+ 1] = NULL
;
758 /* FIXME: add_set_enum_cmd() uses an array of ``char *'' instead
759 of ``const char *''. We just happen to know that the casts are
761 c
= add_set_enum_cmd ("architecture", class_support
,
762 arches
, &set_architecture_string
,
763 "Set architecture of target.",
765 c
->function
.sfunc
= set_architecture
;
766 add_alias_cmd ("processor", "architecture", class_support
, 1, &setlist
);
767 /* Don't use set_from_show - need to print both auto/manual and
769 add_cmd ("architecture", class_support
, show_architecture
,
770 "Show the current target architecture", &showlist
);
777 extern initialize_file_ftype _initialize_gdbarch_utils
;
780 _initialize_gdbarch_utils (void)
782 struct cmd_list_element
*c
;
783 c
= add_set_enum_cmd ("endian", class_support
,
784 endian_enum
, &set_endian_string
,
785 "Set endianness of target.",
787 c
->function
.sfunc
= set_endian
;
788 /* Don't use set_from_show - need to print both auto/manual and
790 add_cmd ("endian", class_support
, show_endian
,
791 "Show the current byte-order", &showlist
);