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"
43 #include "floatformat.h"
45 /* Use the program counter to determine the contents and size
46 of a breakpoint instruction. If no target-dependent macro
47 BREAKPOINT_FROM_PC has been defined to implement this function,
48 assume that the breakpoint doesn't depend on the PC, and
49 use the values of the BIG_BREAKPOINT and LITTLE_BREAKPOINT macros.
50 Return a pointer to a string of bytes that encode a breakpoint
51 instruction, stores the length of the string to *lenptr,
52 and optionally adjust the pc to point to the correct memory location
53 for inserting the breakpoint. */
56 legacy_breakpoint_from_pc (CORE_ADDR
* pcptr
, int *lenptr
)
58 /* {BIG_,LITTLE_}BREAKPOINT is the sequence of bytes we insert for a
59 breakpoint. On some machines, breakpoints are handled by the
60 target environment and we don't have to worry about them here. */
62 if (TARGET_BYTE_ORDER
== BIG_ENDIAN
)
64 static unsigned char big_break_insn
[] = BIG_BREAKPOINT
;
65 *lenptr
= sizeof (big_break_insn
);
66 return big_break_insn
;
69 #ifdef LITTLE_BREAKPOINT
70 if (TARGET_BYTE_ORDER
!= BIG_ENDIAN
)
72 static unsigned char little_break_insn
[] = LITTLE_BREAKPOINT
;
73 *lenptr
= sizeof (little_break_insn
);
74 return little_break_insn
;
79 static unsigned char break_insn
[] = BREAKPOINT
;
80 *lenptr
= sizeof (break_insn
);
89 generic_frameless_function_invocation_not (struct frame_info
*fi
)
95 generic_return_value_on_stack_not (struct type
*type
)
101 legacy_register_name (int i
)
103 #ifdef REGISTER_NAMES
104 static char *names
[] = REGISTER_NAMES
;
105 if (i
< 0 || i
>= (sizeof (names
) / sizeof (*names
)))
110 internal_error (__FILE__
, __LINE__
,
111 "legacy_register_name: called.");
116 #if defined (CALL_DUMMY)
117 LONGEST legacy_call_dummy_words
[] = CALL_DUMMY
;
119 LONGEST legacy_call_dummy_words
[1];
121 int legacy_sizeof_call_dummy_words
= sizeof (legacy_call_dummy_words
);
124 generic_remote_translate_xfer_address (CORE_ADDR gdb_addr
, int gdb_len
,
125 CORE_ADDR
* rem_addr
, int *rem_len
)
127 *rem_addr
= gdb_addr
;
132 generic_prologue_frameless_p (CORE_ADDR ip
)
134 #ifdef SKIP_PROLOGUE_FRAMELESS_P
135 return ip
== SKIP_PROLOGUE_FRAMELESS_P (ip
);
137 return ip
== SKIP_PROLOGUE (ip
);
142 /* Helper functions for INNER_THAN */
145 core_addr_lessthan (CORE_ADDR lhs
, CORE_ADDR rhs
)
151 core_addr_greaterthan (CORE_ADDR lhs
, CORE_ADDR rhs
)
157 /* Helper functions for TARGET_{FLOAT,DOUBLE}_FORMAT */
159 const struct floatformat
*
160 default_float_format (struct gdbarch
*gdbarch
)
163 int byte_order
= gdbarch_byte_order (gdbarch
);
165 int byte_order
= TARGET_BYTE_ORDER
;
170 return &floatformat_ieee_single_big
;
172 return &floatformat_ieee_single_little
;
174 internal_error (__FILE__
, __LINE__
,
175 "default_float_format: bad byte order");
180 const struct floatformat
*
181 default_double_format (struct gdbarch
*gdbarch
)
184 int byte_order
= gdbarch_byte_order (gdbarch
);
186 int byte_order
= TARGET_BYTE_ORDER
;
191 return &floatformat_ieee_double_big
;
193 return &floatformat_ieee_double_little
;
195 internal_error (__FILE__
, __LINE__
,
196 "default_double_format: bad byte order");
200 /* Misc helper functions for targets. */
203 frame_num_args_unknown (struct frame_info
*fi
)
210 generic_register_convertible_not (int num
)
217 default_register_sim_regno (int num
)
224 default_convert_from_func_ptr_addr (CORE_ADDR addr
)
230 no_op_reg_to_regnum (int reg
)
235 /* For use by frame_args_address and frame_locals_address. */
237 default_frame_address (struct frame_info
*fi
)
242 /* Default prepare_to_procced(). */
244 default_prepare_to_proceed (int select_it
)
249 /* Generic prepare_to_proceed(). This one should be suitable for most
250 targets that support threads. */
252 generic_prepare_to_proceed (int select_it
)
255 struct target_waitstatus wait_status
;
257 /* Get the last target status returned by target_wait(). */
258 get_last_target_status (&wait_pid
, &wait_status
);
260 /* Make sure we were stopped at a breakpoint. */
261 if (wait_status
.kind
!= TARGET_WAITKIND_STOPPED
262 || wait_status
.value
.sig
!= TARGET_SIGNAL_TRAP
)
267 if (wait_pid
!= -1 && inferior_pid
!= wait_pid
)
269 /* Switched over from WAIT_PID. */
270 CORE_ADDR wait_pc
= read_pc_pid (wait_pid
);
272 /* Avoid switching where it wouldn't do any good, i.e. if both
273 threads are at the same breakpoint. */
274 if (wait_pc
!= read_pc () && breakpoint_here_p (wait_pc
))
278 /* User hasn't deleted the breakpoint. Switch back to
279 WAIT_PID and return non-zero. */
280 inferior_pid
= wait_pid
;
282 /* FIXME: This stuff came from switch_to_thread() in
283 thread.c (which should probably be a public function). */
284 flush_cached_frames ();
285 registers_changed ();
287 select_frame (get_current_frame (), 0);
297 /* Functions to manipulate the endianness of the target. */
299 #ifdef TARGET_BYTE_ORDER_SELECTABLE
300 /* compat - Catch old targets that expect a selectable byte-order to
301 default to BIG_ENDIAN */
302 #ifndef TARGET_BYTE_ORDER_DEFAULT
303 #define TARGET_BYTE_ORDER_DEFAULT BIG_ENDIAN
306 #if !TARGET_BYTE_ORDER_SELECTABLE_P
307 #ifndef TARGET_BYTE_ORDER_DEFAULT
308 /* compat - Catch old non byte-order selectable targets that do not
309 define TARGET_BYTE_ORDER_DEFAULT and instead expect
310 TARGET_BYTE_ORDER to be used as the default. For targets that
311 defined neither TARGET_BYTE_ORDER nor TARGET_BYTE_ORDER_DEFAULT the
312 below will get a strange compiler warning. */
313 #define TARGET_BYTE_ORDER_DEFAULT TARGET_BYTE_ORDER
316 #ifndef TARGET_BYTE_ORDER_DEFAULT
317 #define TARGET_BYTE_ORDER_DEFAULT BIG_ENDIAN /* arbitrary */
319 /* ``target_byte_order'' is only used when non- multi-arch.
320 Multi-arch targets obtain the current byte order using
321 TARGET_BYTE_ORDER which is controlled by gdbarch.*. */
322 int target_byte_order
= TARGET_BYTE_ORDER_DEFAULT
;
323 int target_byte_order_auto
= 1;
325 static const char endian_big
[] = "big";
326 static const char endian_little
[] = "little";
327 static const char endian_auto
[] = "auto";
328 static const char *endian_enum
[] =
335 static const char *set_endian_string
;
337 /* Called by ``show endian''. */
340 show_endian (char *args
, int from_tty
)
342 if (TARGET_BYTE_ORDER_AUTO
)
343 printf_unfiltered ("The target endianness is set automatically (currently %s endian)\n",
344 (TARGET_BYTE_ORDER
== BIG_ENDIAN
? "big" : "little"));
346 printf_unfiltered ("The target is assumed to be %s endian\n",
347 (TARGET_BYTE_ORDER
== BIG_ENDIAN
? "big" : "little"));
351 set_endian (char *ignore_args
, int from_tty
, struct cmd_list_element
*c
)
353 if (!TARGET_BYTE_ORDER_SELECTABLE_P
)
355 printf_unfiltered ("Byte order is not selectable.");
357 else if (set_endian_string
== endian_auto
)
359 target_byte_order_auto
= 1;
361 else if (set_endian_string
== endian_little
)
363 target_byte_order_auto
= 0;
366 struct gdbarch_info info
;
367 memset (&info
, 0, sizeof info
);
368 info
.byte_order
= LITTLE_ENDIAN
;
369 if (! gdbarch_update_p (info
))
371 printf_unfiltered ("Little endian target not supported by GDB\n");
376 target_byte_order
= LITTLE_ENDIAN
;
379 else if (set_endian_string
== endian_big
)
381 target_byte_order_auto
= 0;
384 struct gdbarch_info info
;
385 memset (&info
, 0, sizeof info
);
386 info
.byte_order
= BIG_ENDIAN
;
387 if (! gdbarch_update_p (info
))
389 printf_unfiltered ("Big endian target not supported by GDB\n");
394 target_byte_order
= BIG_ENDIAN
;
398 internal_error (__FILE__
, __LINE__
,
399 "set_endian: bad value");
400 show_endian (NULL
, from_tty
);
403 /* Set the endianness from a BFD. */
406 set_endian_from_file (bfd
*abfd
)
409 internal_error (__FILE__
, __LINE__
,
410 "set_endian_from_file: not for multi-arch");
411 if (TARGET_BYTE_ORDER_SELECTABLE_P
)
415 if (bfd_big_endian (abfd
))
418 want
= LITTLE_ENDIAN
;
419 if (TARGET_BYTE_ORDER_AUTO
)
420 target_byte_order
= want
;
421 else if (TARGET_BYTE_ORDER
!= want
)
422 warning ("%s endian file does not match %s endian target.",
423 want
== BIG_ENDIAN
? "big" : "little",
424 TARGET_BYTE_ORDER
== BIG_ENDIAN
? "big" : "little");
428 if (bfd_big_endian (abfd
)
429 ? TARGET_BYTE_ORDER
!= BIG_ENDIAN
430 : TARGET_BYTE_ORDER
== BIG_ENDIAN
)
431 warning ("%s endian file does not match %s endian target.",
432 bfd_big_endian (abfd
) ? "big" : "little",
433 TARGET_BYTE_ORDER
== BIG_ENDIAN
? "big" : "little");
438 /* Functions to manipulate the architecture of the target */
440 enum set_arch
{ set_arch_auto
, set_arch_manual
};
442 int target_architecture_auto
= 1;
444 const char *set_architecture_string
;
446 /* Old way of changing the current architecture. */
448 extern const struct bfd_arch_info bfd_default_arch_struct
;
449 const struct bfd_arch_info
*target_architecture
= &bfd_default_arch_struct
;
450 int (*target_architecture_hook
) (const struct bfd_arch_info
*ap
);
453 arch_ok (const struct bfd_arch_info
*arch
)
456 internal_error (__FILE__
, __LINE__
,
457 "arch_ok: not multi-arched");
458 /* Should be performing the more basic check that the binary is
459 compatible with GDB. */
460 /* Check with the target that the architecture is valid. */
461 return (target_architecture_hook
== NULL
462 || target_architecture_hook (arch
));
466 set_arch (const struct bfd_arch_info
*arch
,
470 internal_error (__FILE__
, __LINE__
,
471 "set_arch: not multi-arched");
476 warning ("Target may not support %s architecture",
477 arch
->printable_name
);
478 target_architecture
= arch
;
480 case set_arch_manual
:
483 printf_unfiltered ("Target does not support `%s' architecture.\n",
484 arch
->printable_name
);
488 target_architecture_auto
= 0;
489 target_architecture
= arch
;
494 gdbarch_dump (current_gdbarch
, gdb_stdlog
);
497 /* Set the architecture from arch/machine (deprecated) */
500 set_architecture_from_arch_mach (enum bfd_architecture arch
,
503 const struct bfd_arch_info
*wanted
= bfd_lookup_arch (arch
, mach
);
505 internal_error (__FILE__
, __LINE__
,
506 "set_architecture_from_arch_mach: not multi-arched");
508 set_arch (wanted
, set_arch_manual
);
510 internal_error (__FILE__
, __LINE__
,
511 "gdbarch: hardwired architecture/machine not recognized");
514 /* Set the architecture from a BFD (deprecated) */
517 set_architecture_from_file (bfd
*abfd
)
519 const struct bfd_arch_info
*wanted
= bfd_get_arch_info (abfd
);
521 internal_error (__FILE__
, __LINE__
,
522 "set_architecture_from_file: not multi-arched");
523 if (target_architecture_auto
)
525 set_arch (wanted
, set_arch_auto
);
527 else if (wanted
!= target_architecture
)
529 warning ("%s architecture file may be incompatible with %s target.",
530 wanted
->printable_name
,
531 target_architecture
->printable_name
);
536 /* Called if the user enters ``show architecture'' without an
540 show_architecture (char *args
, int from_tty
)
543 arch
= TARGET_ARCHITECTURE
->printable_name
;
544 if (target_architecture_auto
)
545 printf_filtered ("The target architecture is set automatically (currently %s)\n", arch
);
547 printf_filtered ("The target architecture is assumed to be %s\n", arch
);
551 /* Called if the user enters ``set architecture'' with or without an
555 set_architecture (char *ignore_args
, int from_tty
, struct cmd_list_element
*c
)
557 if (strcmp (set_architecture_string
, "auto") == 0)
559 target_architecture_auto
= 1;
561 else if (GDB_MULTI_ARCH
)
563 struct gdbarch_info info
;
564 memset (&info
, 0, sizeof info
);
565 info
.bfd_arch_info
= bfd_scan_arch (set_architecture_string
);
566 if (info
.bfd_arch_info
== NULL
)
567 internal_error (__FILE__
, __LINE__
,
568 "set_architecture: bfd_scan_arch failed");
569 if (gdbarch_update_p (info
))
570 target_architecture_auto
= 0;
572 printf_unfiltered ("Architecture `%s' not recognized.\n",
573 set_architecture_string
);
577 const struct bfd_arch_info
*arch
578 = bfd_scan_arch (set_architecture_string
);
580 internal_error (__FILE__
, __LINE__
,
581 "set_architecture: bfd_scan_arch failed");
582 set_arch (arch
, set_arch_manual
);
584 show_architecture (NULL
, from_tty
);
587 /* Called if the user enters ``info architecture'' without an argument. */
590 info_architecture (char *args
, int from_tty
)
592 printf_filtered ("Available architectures are:\n");
595 const char **arches
= gdbarch_printable_names ();
597 for (arch
= arches
; *arch
!= NULL
; arch
++)
599 printf_filtered (" %s", *arch
);
605 enum bfd_architecture a
;
606 for (a
= bfd_arch_obscure
+ 1; a
< bfd_arch_last
; a
++)
608 const struct bfd_arch_info
*ap
;
609 for (ap
= bfd_lookup_arch (a
, 0);
613 printf_filtered (" %s", ap
->printable_name
);
618 printf_filtered ("\n");
621 /* Set the dynamic target-system-dependent parameters (architecture,
622 byte-order) using information found in the BFD */
625 set_gdbarch_from_file (bfd
*abfd
)
629 struct gdbarch_info info
;
630 memset (&info
, 0, sizeof info
);
632 if (! gdbarch_update_p (info
))
633 error ("Architecture of file not recognized.\n");
637 set_architecture_from_file (abfd
);
638 set_endian_from_file (abfd
);
642 /* Initialize the current architecture. Update the ``set
643 architecture'' command so that it specifies a list of valid
646 #ifdef DEFAULT_BFD_ARCH
647 extern const bfd_arch_info_type DEFAULT_BFD_ARCH
;
648 static const bfd_arch_info_type
*default_bfd_arch
= &DEFAULT_BFD_ARCH
;
650 static const bfd_arch_info_type
*default_bfd_arch
;
653 #ifdef DEFAULT_BFD_VEC
654 extern const bfd_target DEFAULT_BFD_VEC
;
655 static const bfd_target
*default_bfd_vec
= &DEFAULT_BFD_VEC
;
657 static const bfd_target
*default_bfd_vec
;
661 initialize_current_architecture (void)
663 const char **arches
= gdbarch_printable_names ();
665 /* determine a default architecture and byte order. */
666 struct gdbarch_info info
;
667 memset (&info
, 0, sizeof (info
));
669 /* Find a default architecture. */
670 if (info
.bfd_arch_info
== NULL
671 && default_bfd_arch
!= NULL
)
672 info
.bfd_arch_info
= default_bfd_arch
;
673 if (info
.bfd_arch_info
== NULL
)
675 /* Choose the architecture by taking the first one
677 const char *chosen
= arches
[0];
679 for (arch
= arches
; *arch
!= NULL
; arch
++)
681 if (strcmp (*arch
, chosen
) < 0)
685 internal_error (__FILE__
, __LINE__
,
686 "initialize_current_architecture: No arch");
687 info
.bfd_arch_info
= bfd_scan_arch (chosen
);
688 if (info
.bfd_arch_info
== NULL
)
689 internal_error (__FILE__
, __LINE__
,
690 "initialize_current_architecture: Arch not found");
693 /* take several guesses at a byte order. */
694 /* NB: can't use TARGET_BYTE_ORDER_DEFAULT as its definition is
696 if (info
.byte_order
== 0
697 && default_bfd_vec
!= NULL
)
699 /* Extract BFD's default vector's byte order. */
700 switch (default_bfd_vec
->byteorder
)
703 info
.byte_order
= BIG_ENDIAN
;
705 case BFD_ENDIAN_LITTLE
:
706 info
.byte_order
= LITTLE_ENDIAN
;
712 if (info
.byte_order
== 0)
714 /* look for ``*el-*'' in the target name. */
716 chp
= strchr (target_name
, '-');
718 && chp
- 2 >= target_name
719 && strncmp (chp
- 2, "el", 2) == 0)
720 info
.byte_order
= LITTLE_ENDIAN
;
722 if (info
.byte_order
== 0)
724 /* Wire it to big-endian!!! */
725 info
.byte_order
= BIG_ENDIAN
;
730 if (! gdbarch_update_p (info
))
732 internal_error (__FILE__
, __LINE__
,
733 "initialize_current_architecture: Selection of initial architecture failed");
737 /* Create the ``set architecture'' command appending ``auto'' to the
738 list of architectures. */
740 struct cmd_list_element
*c
;
741 /* Append ``auto''. */
743 for (nr
= 0; arches
[nr
] != NULL
; nr
++);
744 arches
= xrealloc (arches
, sizeof (char*) * (nr
+ 2));
745 arches
[nr
+ 0] = "auto";
746 arches
[nr
+ 1] = NULL
;
747 /* FIXME: add_set_enum_cmd() uses an array of ``char *'' instead
748 of ``const char *''. We just happen to know that the casts are
750 c
= add_set_enum_cmd ("architecture", class_support
,
751 arches
, &set_architecture_string
,
752 "Set architecture of target.",
754 c
->function
.sfunc
= set_architecture
;
755 add_alias_cmd ("processor", "architecture", class_support
, 1, &setlist
);
756 /* Don't use set_from_show - need to print both auto/manual and
758 add_cmd ("architecture", class_support
, show_architecture
,
759 "Show the current target architecture", &showlist
);
760 c
= add_cmd ("architecture", class_support
, info_architecture
,
761 "List supported target architectures", &infolist
);
762 deprecate_cmd (c
, "set architecture");
769 extern initialize_file_ftype _initialize_gdbarch_utils
;
772 _initialize_gdbarch_utils (void)
774 struct cmd_list_element
*c
;
775 c
= add_set_enum_cmd ("endian", class_support
,
776 endian_enum
, &set_endian_string
,
777 "Set endianness of target.",
779 c
->function
.sfunc
= set_endian
;
780 /* Don't use set_from_show - need to print both auto/manual and
782 add_cmd ("endian", class_support
, show_endian
,
783 "Show the current byte-order", &showlist
);