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"
44 #include "floatformat.h"
46 /* Use the program counter to determine the contents and size
47 of a breakpoint instruction. If no target-dependent macro
48 BREAKPOINT_FROM_PC has been defined to implement this function,
49 assume that the breakpoint doesn't depend on the PC, and
50 use the values of the BIG_BREAKPOINT and LITTLE_BREAKPOINT macros.
51 Return a pointer to a string of bytes that encode a breakpoint
52 instruction, stores the length of the string to *lenptr,
53 and optionally adjust the pc to point to the correct memory location
54 for inserting the breakpoint. */
57 legacy_breakpoint_from_pc (CORE_ADDR
* pcptr
, int *lenptr
)
59 /* {BIG_,LITTLE_}BREAKPOINT is the sequence of bytes we insert for a
60 breakpoint. On some machines, breakpoints are handled by the
61 target environment and we don't have to worry about them here. */
63 if (TARGET_BYTE_ORDER
== BIG_ENDIAN
)
65 static unsigned char big_break_insn
[] = BIG_BREAKPOINT
;
66 *lenptr
= sizeof (big_break_insn
);
67 return big_break_insn
;
70 #ifdef LITTLE_BREAKPOINT
71 if (TARGET_BYTE_ORDER
!= BIG_ENDIAN
)
73 static unsigned char little_break_insn
[] = LITTLE_BREAKPOINT
;
74 *lenptr
= sizeof (little_break_insn
);
75 return little_break_insn
;
80 static unsigned char break_insn
[] = BREAKPOINT
;
81 *lenptr
= sizeof (break_insn
);
90 generic_frameless_function_invocation_not (struct frame_info
*fi
)
96 generic_return_value_on_stack_not (struct type
*type
)
102 legacy_register_name (int i
)
104 #ifdef REGISTER_NAMES
105 static char *names
[] = REGISTER_NAMES
;
106 if (i
< 0 || i
>= (sizeof (names
) / sizeof (*names
)))
111 internal_error (__FILE__
, __LINE__
,
112 "legacy_register_name: called.");
117 #if defined (CALL_DUMMY)
118 LONGEST legacy_call_dummy_words
[] = CALL_DUMMY
;
120 LONGEST legacy_call_dummy_words
[1];
122 int legacy_sizeof_call_dummy_words
= sizeof (legacy_call_dummy_words
);
125 generic_remote_translate_xfer_address (CORE_ADDR gdb_addr
, int gdb_len
,
126 CORE_ADDR
* rem_addr
, int *rem_len
)
128 *rem_addr
= gdb_addr
;
133 generic_prologue_frameless_p (CORE_ADDR ip
)
135 #ifdef SKIP_PROLOGUE_FRAMELESS_P
136 return ip
== SKIP_PROLOGUE_FRAMELESS_P (ip
);
138 return ip
== SKIP_PROLOGUE (ip
);
143 /* Helper functions for INNER_THAN */
146 core_addr_lessthan (CORE_ADDR lhs
, CORE_ADDR rhs
)
152 core_addr_greaterthan (CORE_ADDR lhs
, CORE_ADDR rhs
)
158 /* Helper functions for TARGET_{FLOAT,DOUBLE}_FORMAT */
160 const struct floatformat
*
161 default_float_format (struct gdbarch
*gdbarch
)
164 int byte_order
= gdbarch_byte_order (gdbarch
);
166 int byte_order
= TARGET_BYTE_ORDER
;
171 return &floatformat_ieee_single_big
;
173 return &floatformat_ieee_single_little
;
175 internal_error (__FILE__
, __LINE__
,
176 "default_float_format: bad byte order");
181 const struct floatformat
*
182 default_double_format (struct gdbarch
*gdbarch
)
185 int byte_order
= gdbarch_byte_order (gdbarch
);
187 int byte_order
= TARGET_BYTE_ORDER
;
192 return &floatformat_ieee_double_big
;
194 return &floatformat_ieee_double_little
;
196 internal_error (__FILE__
, __LINE__
,
197 "default_double_format: bad byte order");
201 /* Misc helper functions for targets. */
204 frame_num_args_unknown (struct frame_info
*fi
)
211 generic_register_convertible_not (int num
)
218 default_register_sim_regno (int num
)
225 core_addr_identity (CORE_ADDR addr
)
231 no_op_reg_to_regnum (int reg
)
236 /* For use by frame_args_address and frame_locals_address. */
238 default_frame_address (struct frame_info
*fi
)
243 /* Default prepare_to_procced(). */
245 default_prepare_to_proceed (int select_it
)
250 /* Generic prepare_to_proceed(). This one should be suitable for most
251 targets that support threads. */
253 generic_prepare_to_proceed (int select_it
)
256 struct target_waitstatus wait_status
;
258 /* Get the last target status returned by target_wait(). */
259 get_last_target_status (&wait_ptid
, &wait_status
);
261 /* Make sure we were stopped either at a breakpoint, or because
263 if (wait_status
.kind
!= TARGET_WAITKIND_STOPPED
264 || (wait_status
.value
.sig
!= TARGET_SIGNAL_TRAP
&&
265 wait_status
.value
.sig
!= TARGET_SIGNAL_INT
))
270 if (!ptid_equal (wait_ptid
, minus_one_ptid
)
271 && !ptid_equal (inferior_ptid
, wait_ptid
))
273 /* Switched over from WAIT_PID. */
274 CORE_ADDR wait_pc
= read_pc_pid (wait_ptid
);
276 if (wait_pc
!= read_pc ())
280 /* Switch back to WAIT_PID thread. */
281 inferior_ptid
= wait_ptid
;
283 /* FIXME: This stuff came from switch_to_thread() in
284 thread.c (which should probably be a public function). */
285 flush_cached_frames ();
286 registers_changed ();
288 select_frame (get_current_frame (), 0);
290 /* We return 1 to indicate that there is a breakpoint here,
291 so we need to step over it before continuing to avoid
292 hitting it straight away. */
293 if (breakpoint_here_p (wait_pc
))
304 init_frame_pc_noop (int fromleaf
, struct frame_info
*prev
)
310 init_frame_pc_default (int fromleaf
, struct frame_info
*prev
)
313 prev
->pc
= SAVED_PC_AFTER_CALL (prev
->next
);
314 else if (prev
->next
!= NULL
)
315 prev
->pc
= FRAME_SAVED_PC (prev
->next
);
317 prev
->pc
= read_pc ();
321 cannot_register_not (int regnum
)
326 /* Functions to manipulate the endianness of the target. */
328 #ifdef TARGET_BYTE_ORDER_SELECTABLE
329 /* compat - Catch old targets that expect a selectable byte-order to
330 default to BIG_ENDIAN */
331 #ifndef TARGET_BYTE_ORDER_DEFAULT
332 #define TARGET_BYTE_ORDER_DEFAULT BIG_ENDIAN
335 #if !TARGET_BYTE_ORDER_SELECTABLE_P
336 #ifndef TARGET_BYTE_ORDER_DEFAULT
337 /* compat - Catch old non byte-order selectable targets that do not
338 define TARGET_BYTE_ORDER_DEFAULT and instead expect
339 TARGET_BYTE_ORDER to be used as the default. For targets that
340 defined neither TARGET_BYTE_ORDER nor TARGET_BYTE_ORDER_DEFAULT the
341 below will get a strange compiler warning. */
342 #define TARGET_BYTE_ORDER_DEFAULT TARGET_BYTE_ORDER
345 #ifndef TARGET_BYTE_ORDER_DEFAULT
346 #define TARGET_BYTE_ORDER_DEFAULT BIG_ENDIAN /* arbitrary */
348 /* ``target_byte_order'' is only used when non- multi-arch.
349 Multi-arch targets obtain the current byte order using
350 TARGET_BYTE_ORDER which is controlled by gdbarch.*. */
351 int target_byte_order
= TARGET_BYTE_ORDER_DEFAULT
;
352 int target_byte_order_auto
= 1;
354 static const char endian_big
[] = "big";
355 static const char endian_little
[] = "little";
356 static const char endian_auto
[] = "auto";
357 static const char *endian_enum
[] =
364 static const char *set_endian_string
;
366 /* Called by ``show endian''. */
369 show_endian (char *args
, int from_tty
)
371 if (TARGET_BYTE_ORDER_AUTO
)
372 printf_unfiltered ("The target endianness is set automatically (currently %s endian)\n",
373 (TARGET_BYTE_ORDER
== BIG_ENDIAN
? "big" : "little"));
375 printf_unfiltered ("The target is assumed to be %s endian\n",
376 (TARGET_BYTE_ORDER
== BIG_ENDIAN
? "big" : "little"));
380 set_endian (char *ignore_args
, int from_tty
, struct cmd_list_element
*c
)
382 if (!TARGET_BYTE_ORDER_SELECTABLE_P
)
384 printf_unfiltered ("Byte order is not selectable.");
386 else if (set_endian_string
== endian_auto
)
388 target_byte_order_auto
= 1;
390 else if (set_endian_string
== endian_little
)
392 target_byte_order_auto
= 0;
395 struct gdbarch_info info
;
396 memset (&info
, 0, sizeof info
);
397 info
.byte_order
= LITTLE_ENDIAN
;
398 if (! gdbarch_update_p (info
))
400 printf_unfiltered ("Little endian target not supported by GDB\n");
405 target_byte_order
= LITTLE_ENDIAN
;
408 else if (set_endian_string
== endian_big
)
410 target_byte_order_auto
= 0;
413 struct gdbarch_info info
;
414 memset (&info
, 0, sizeof info
);
415 info
.byte_order
= BIG_ENDIAN
;
416 if (! gdbarch_update_p (info
))
418 printf_unfiltered ("Big endian target not supported by GDB\n");
423 target_byte_order
= BIG_ENDIAN
;
427 internal_error (__FILE__
, __LINE__
,
428 "set_endian: bad value");
429 show_endian (NULL
, from_tty
);
432 /* Set the endianness from a BFD. */
435 set_endian_from_file (bfd
*abfd
)
438 internal_error (__FILE__
, __LINE__
,
439 "set_endian_from_file: not for multi-arch");
440 if (TARGET_BYTE_ORDER_SELECTABLE_P
)
444 if (bfd_big_endian (abfd
))
447 want
= LITTLE_ENDIAN
;
448 if (TARGET_BYTE_ORDER_AUTO
)
449 target_byte_order
= want
;
450 else if (TARGET_BYTE_ORDER
!= want
)
451 warning ("%s endian file does not match %s endian target.",
452 want
== BIG_ENDIAN
? "big" : "little",
453 TARGET_BYTE_ORDER
== BIG_ENDIAN
? "big" : "little");
457 if (bfd_big_endian (abfd
)
458 ? TARGET_BYTE_ORDER
!= BIG_ENDIAN
459 : TARGET_BYTE_ORDER
== BIG_ENDIAN
)
460 warning ("%s endian file does not match %s endian target.",
461 bfd_big_endian (abfd
) ? "big" : "little",
462 TARGET_BYTE_ORDER
== BIG_ENDIAN
? "big" : "little");
467 /* Functions to manipulate the architecture of the target */
469 enum set_arch
{ set_arch_auto
, set_arch_manual
};
471 int target_architecture_auto
= 1;
473 const char *set_architecture_string
;
475 /* Old way of changing the current architecture. */
477 extern const struct bfd_arch_info bfd_default_arch_struct
;
478 const struct bfd_arch_info
*target_architecture
= &bfd_default_arch_struct
;
479 int (*target_architecture_hook
) (const struct bfd_arch_info
*ap
);
482 arch_ok (const struct bfd_arch_info
*arch
)
485 internal_error (__FILE__
, __LINE__
,
486 "arch_ok: not multi-arched");
487 /* Should be performing the more basic check that the binary is
488 compatible with GDB. */
489 /* Check with the target that the architecture is valid. */
490 return (target_architecture_hook
== NULL
491 || target_architecture_hook (arch
));
495 set_arch (const struct bfd_arch_info
*arch
,
499 internal_error (__FILE__
, __LINE__
,
500 "set_arch: not multi-arched");
505 warning ("Target may not support %s architecture",
506 arch
->printable_name
);
507 target_architecture
= arch
;
509 case set_arch_manual
:
512 printf_unfiltered ("Target does not support `%s' architecture.\n",
513 arch
->printable_name
);
517 target_architecture_auto
= 0;
518 target_architecture
= arch
;
523 gdbarch_dump (current_gdbarch
, gdb_stdlog
);
526 /* Set the architecture from arch/machine (deprecated) */
529 set_architecture_from_arch_mach (enum bfd_architecture arch
,
532 const struct bfd_arch_info
*wanted
= bfd_lookup_arch (arch
, mach
);
534 internal_error (__FILE__
, __LINE__
,
535 "set_architecture_from_arch_mach: not multi-arched");
537 set_arch (wanted
, set_arch_manual
);
539 internal_error (__FILE__
, __LINE__
,
540 "gdbarch: hardwired architecture/machine not recognized");
543 /* Set the architecture from a BFD (deprecated) */
546 set_architecture_from_file (bfd
*abfd
)
548 const struct bfd_arch_info
*wanted
= bfd_get_arch_info (abfd
);
550 internal_error (__FILE__
, __LINE__
,
551 "set_architecture_from_file: not multi-arched");
552 if (target_architecture_auto
)
554 set_arch (wanted
, set_arch_auto
);
556 else if (wanted
!= target_architecture
)
558 warning ("%s architecture file may be incompatible with %s target.",
559 wanted
->printable_name
,
560 target_architecture
->printable_name
);
565 /* Called if the user enters ``show architecture'' without an
569 show_architecture (char *args
, int from_tty
)
572 arch
= TARGET_ARCHITECTURE
->printable_name
;
573 if (target_architecture_auto
)
574 printf_filtered ("The target architecture is set automatically (currently %s)\n", arch
);
576 printf_filtered ("The target architecture is assumed to be %s\n", arch
);
580 /* Called if the user enters ``set architecture'' with or without an
584 set_architecture (char *ignore_args
, int from_tty
, struct cmd_list_element
*c
)
586 if (strcmp (set_architecture_string
, "auto") == 0)
588 target_architecture_auto
= 1;
590 else if (GDB_MULTI_ARCH
)
592 struct gdbarch_info info
;
593 memset (&info
, 0, sizeof info
);
594 info
.bfd_arch_info
= bfd_scan_arch (set_architecture_string
);
595 if (info
.bfd_arch_info
== NULL
)
596 internal_error (__FILE__
, __LINE__
,
597 "set_architecture: bfd_scan_arch failed");
598 if (gdbarch_update_p (info
))
599 target_architecture_auto
= 0;
601 printf_unfiltered ("Architecture `%s' not recognized.\n",
602 set_architecture_string
);
606 const struct bfd_arch_info
*arch
607 = bfd_scan_arch (set_architecture_string
);
609 internal_error (__FILE__
, __LINE__
,
610 "set_architecture: bfd_scan_arch failed");
611 set_arch (arch
, set_arch_manual
);
613 show_architecture (NULL
, from_tty
);
616 /* Set the dynamic target-system-dependent parameters (architecture,
617 byte-order) using information found in the BFD */
620 set_gdbarch_from_file (bfd
*abfd
)
624 struct gdbarch_info info
;
625 memset (&info
, 0, sizeof info
);
627 if (! gdbarch_update_p (info
))
628 error ("Architecture of file not recognized.\n");
632 set_architecture_from_file (abfd
);
633 set_endian_from_file (abfd
);
637 /* Initialize the current architecture. Update the ``set
638 architecture'' command so that it specifies a list of valid
641 #ifdef DEFAULT_BFD_ARCH
642 extern const bfd_arch_info_type DEFAULT_BFD_ARCH
;
643 static const bfd_arch_info_type
*default_bfd_arch
= &DEFAULT_BFD_ARCH
;
645 static const bfd_arch_info_type
*default_bfd_arch
;
648 #ifdef DEFAULT_BFD_VEC
649 extern const bfd_target DEFAULT_BFD_VEC
;
650 static const bfd_target
*default_bfd_vec
= &DEFAULT_BFD_VEC
;
652 static const bfd_target
*default_bfd_vec
;
656 initialize_current_architecture (void)
658 const char **arches
= gdbarch_printable_names ();
660 /* determine a default architecture and byte order. */
661 struct gdbarch_info info
;
662 memset (&info
, 0, sizeof (info
));
664 /* Find a default architecture. */
665 if (info
.bfd_arch_info
== NULL
666 && default_bfd_arch
!= NULL
)
667 info
.bfd_arch_info
= default_bfd_arch
;
668 if (info
.bfd_arch_info
== NULL
)
670 /* Choose the architecture by taking the first one
672 const char *chosen
= arches
[0];
674 for (arch
= arches
; *arch
!= NULL
; arch
++)
676 if (strcmp (*arch
, chosen
) < 0)
680 internal_error (__FILE__
, __LINE__
,
681 "initialize_current_architecture: No arch");
682 info
.bfd_arch_info
= bfd_scan_arch (chosen
);
683 if (info
.bfd_arch_info
== NULL
)
684 internal_error (__FILE__
, __LINE__
,
685 "initialize_current_architecture: Arch not found");
688 /* take several guesses at a byte order. */
689 /* NB: can't use TARGET_BYTE_ORDER_DEFAULT as its definition is
691 if (info
.byte_order
== 0
692 && default_bfd_vec
!= NULL
)
694 /* Extract BFD's default vector's byte order. */
695 switch (default_bfd_vec
->byteorder
)
698 info
.byte_order
= BIG_ENDIAN
;
700 case BFD_ENDIAN_LITTLE
:
701 info
.byte_order
= LITTLE_ENDIAN
;
707 if (info
.byte_order
== 0)
709 /* look for ``*el-*'' in the target name. */
711 chp
= strchr (target_name
, '-');
713 && chp
- 2 >= target_name
714 && strncmp (chp
- 2, "el", 2) == 0)
715 info
.byte_order
= LITTLE_ENDIAN
;
717 if (info
.byte_order
== 0)
719 /* Wire it to big-endian!!! */
720 info
.byte_order
= BIG_ENDIAN
;
725 if (! gdbarch_update_p (info
))
727 internal_error (__FILE__
, __LINE__
,
728 "initialize_current_architecture: Selection of initial architecture failed");
732 initialize_non_multiarch ();
734 /* Create the ``set architecture'' command appending ``auto'' to the
735 list of architectures. */
737 struct cmd_list_element
*c
;
738 /* Append ``auto''. */
740 for (nr
= 0; arches
[nr
] != NULL
; nr
++);
741 arches
= xrealloc (arches
, sizeof (char*) * (nr
+ 2));
742 arches
[nr
+ 0] = "auto";
743 arches
[nr
+ 1] = NULL
;
744 /* FIXME: add_set_enum_cmd() uses an array of ``char *'' instead
745 of ``const char *''. We just happen to know that the casts are
747 c
= add_set_enum_cmd ("architecture", class_support
,
748 arches
, &set_architecture_string
,
749 "Set architecture of target.",
751 c
->function
.sfunc
= set_architecture
;
752 add_alias_cmd ("processor", "architecture", class_support
, 1, &setlist
);
753 /* Don't use set_from_show - need to print both auto/manual and
755 add_cmd ("architecture", class_support
, show_architecture
,
756 "Show the current target architecture", &showlist
);
763 extern initialize_file_ftype _initialize_gdbarch_utils
;
766 _initialize_gdbarch_utils (void)
768 struct cmd_list_element
*c
;
769 c
= add_set_enum_cmd ("endian", class_support
,
770 endian_enum
, &set_endian_string
,
771 "Set endianness of target.",
773 c
->function
.sfunc
= set_endian
;
774 /* Don't use set_from_show - need to print both auto/manual and
776 add_cmd ("endian", class_support
, show_endian
,
777 "Show the current byte-order", &showlist
);