Unify gdb printf functions
[binutils-gdb.git] / gdb / arch-utils.c
1 /* Dynamic architecture support for GDB, the GNU debugger.
2
3 Copyright (C) 1998-2022 Free Software Foundation, Inc.
4
5 This file is part of GDB.
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
19
20 #include "defs.h"
21
22 #include "arch-utils.h"
23 #include "gdbcmd.h"
24 #include "inferior.h" /* enum CALL_DUMMY_LOCATION et al. */
25 #include "infrun.h"
26 #include "regcache.h"
27 #include "sim-regno.h"
28 #include "gdbcore.h"
29 #include "osabi.h"
30 #include "target-descriptions.h"
31 #include "objfiles.h"
32 #include "language.h"
33 #include "symtab.h"
34 #include "dummy-frame.h"
35 #include "frame-unwind.h"
36 #include "reggroups.h"
37 #include "auxv.h"
38 #include "observable.h"
39
40 #include "gdbsupport/version.h"
41
42 #include "floatformat.h"
43
44 #include "dis-asm.h"
45
46 bool
47 default_displaced_step_hw_singlestep (struct gdbarch *gdbarch)
48 {
49 return !gdbarch_software_single_step_p (gdbarch);
50 }
51
52 CORE_ADDR
53 displaced_step_at_entry_point (struct gdbarch *gdbarch)
54 {
55 CORE_ADDR addr;
56 int bp_len;
57
58 addr = entry_point_address ();
59
60 /* Inferior calls also use the entry point as a breakpoint location.
61 We don't want displaced stepping to interfere with those
62 breakpoints, so leave space. */
63 gdbarch_breakpoint_from_pc (gdbarch, &addr, &bp_len);
64 addr += bp_len * 2;
65
66 return addr;
67 }
68
69 int
70 legacy_register_sim_regno (struct gdbarch *gdbarch, int regnum)
71 {
72 /* Only makes sense to supply raw registers. */
73 gdb_assert (regnum >= 0 && regnum < gdbarch_num_regs (gdbarch));
74 /* NOTE: cagney/2002-05-13: The old code did it this way and it is
75 suspected that some GDB/SIM combinations may rely on this
76 behaviour. The default should be one2one_register_sim_regno
77 (below). */
78 if (gdbarch_register_name (gdbarch, regnum) != NULL
79 && gdbarch_register_name (gdbarch, regnum)[0] != '\0')
80 return regnum;
81 else
82 return LEGACY_SIM_REGNO_IGNORE;
83 }
84
85
86 /* See arch-utils.h */
87
88 std::string
89 default_memtag_to_string (struct gdbarch *gdbarch, struct value *tag)
90 {
91 error (_("This architecture has no method to convert a memory tag to"
92 " a string."));
93 }
94
95 /* See arch-utils.h */
96
97 bool
98 default_tagged_address_p (struct gdbarch *gdbarch, struct value *address)
99 {
100 /* By default, assume the address is untagged. */
101 return false;
102 }
103
104 /* See arch-utils.h */
105
106 bool
107 default_memtag_matches_p (struct gdbarch *gdbarch, struct value *address)
108 {
109 /* By default, assume the tags match. */
110 return true;
111 }
112
113 /* See arch-utils.h */
114
115 bool
116 default_set_memtags (struct gdbarch *gdbarch, struct value *address,
117 size_t length, const gdb::byte_vector &tags,
118 memtag_type tag_type)
119 {
120 /* By default, return true (successful); */
121 return true;
122 }
123
124 /* See arch-utils.h */
125
126 struct value *
127 default_get_memtag (struct gdbarch *gdbarch, struct value *address,
128 memtag_type tag_type)
129 {
130 /* By default, return no tag. */
131 return nullptr;
132 }
133
134 CORE_ADDR
135 generic_skip_trampoline_code (struct frame_info *frame, CORE_ADDR pc)
136 {
137 return 0;
138 }
139
140 CORE_ADDR
141 generic_skip_solib_resolver (struct gdbarch *gdbarch, CORE_ADDR pc)
142 {
143 return 0;
144 }
145
146 int
147 generic_in_solib_return_trampoline (struct gdbarch *gdbarch,
148 CORE_ADDR pc, const char *name)
149 {
150 return 0;
151 }
152
153 int
154 generic_stack_frame_destroyed_p (struct gdbarch *gdbarch, CORE_ADDR pc)
155 {
156 return 0;
157 }
158
159 int
160 default_code_of_frame_writable (struct gdbarch *gdbarch,
161 struct frame_info *frame)
162 {
163 return 1;
164 }
165
166 /* Helper functions for gdbarch_inner_than */
167
168 int
169 core_addr_lessthan (CORE_ADDR lhs, CORE_ADDR rhs)
170 {
171 return (lhs < rhs);
172 }
173
174 int
175 core_addr_greaterthan (CORE_ADDR lhs, CORE_ADDR rhs)
176 {
177 return (lhs > rhs);
178 }
179
180 /* Misc helper functions for targets. */
181
182 CORE_ADDR
183 core_addr_identity (struct gdbarch *gdbarch, CORE_ADDR addr)
184 {
185 return addr;
186 }
187
188 CORE_ADDR
189 convert_from_func_ptr_addr_identity (struct gdbarch *gdbarch, CORE_ADDR addr,
190 struct target_ops *targ)
191 {
192 return addr;
193 }
194
195 int
196 no_op_reg_to_regnum (struct gdbarch *gdbarch, int reg)
197 {
198 return reg;
199 }
200
201 void
202 default_coff_make_msymbol_special (int val, struct minimal_symbol *msym)
203 {
204 return;
205 }
206
207 /* See arch-utils.h. */
208
209 void
210 default_make_symbol_special (struct symbol *sym, struct objfile *objfile)
211 {
212 return;
213 }
214
215 /* See arch-utils.h. */
216
217 CORE_ADDR
218 default_adjust_dwarf2_addr (CORE_ADDR pc)
219 {
220 return pc;
221 }
222
223 /* See arch-utils.h. */
224
225 CORE_ADDR
226 default_adjust_dwarf2_line (CORE_ADDR addr, int rel)
227 {
228 return addr;
229 }
230
231 /* See arch-utils.h. */
232
233 bool
234 default_execute_dwarf_cfa_vendor_op (struct gdbarch *gdbarch, gdb_byte op,
235 struct dwarf2_frame_state *fs)
236 {
237 return false;
238 }
239
240 int
241 cannot_register_not (struct gdbarch *gdbarch, int regnum)
242 {
243 return 0;
244 }
245
246 /* Legacy version of target_virtual_frame_pointer(). Assumes that
247 there is an gdbarch_deprecated_fp_regnum and that it is the same,
248 cooked or raw. */
249
250 void
251 legacy_virtual_frame_pointer (struct gdbarch *gdbarch,
252 CORE_ADDR pc,
253 int *frame_regnum,
254 LONGEST *frame_offset)
255 {
256 /* FIXME: cagney/2002-09-13: This code is used when identifying the
257 frame pointer of the current PC. It is assuming that a single
258 register and an offset can determine this. I think it should
259 instead generate a byte code expression as that would work better
260 with things like Dwarf2's CFI. */
261 if (gdbarch_deprecated_fp_regnum (gdbarch) >= 0
262 && gdbarch_deprecated_fp_regnum (gdbarch)
263 < gdbarch_num_regs (gdbarch))
264 *frame_regnum = gdbarch_deprecated_fp_regnum (gdbarch);
265 else if (gdbarch_sp_regnum (gdbarch) >= 0
266 && gdbarch_sp_regnum (gdbarch)
267 < gdbarch_num_regs (gdbarch))
268 *frame_regnum = gdbarch_sp_regnum (gdbarch);
269 else
270 /* Should this be an internal error? I guess so, it is reflecting
271 an architectural limitation in the current design. */
272 internal_error (__FILE__, __LINE__,
273 _("No virtual frame pointer available"));
274 *frame_offset = 0;
275 }
276
277 /* Return a floating-point format for a floating-point variable of
278 length LEN in bits. If non-NULL, NAME is the name of its type.
279 If no suitable type is found, return NULL. */
280
281 const struct floatformat **
282 default_floatformat_for_type (struct gdbarch *gdbarch,
283 const char *name, int len)
284 {
285 const struct floatformat **format = NULL;
286
287 /* Check if this is a bfloat16 type. It has the same size as the
288 IEEE half float type, so we use the base type name to tell them
289 apart. */
290 if (name != nullptr && strcmp (name, "__bf16") == 0
291 && len == gdbarch_bfloat16_bit (gdbarch))
292 format = gdbarch_bfloat16_format (gdbarch);
293 else if (len == gdbarch_half_bit (gdbarch))
294 format = gdbarch_half_format (gdbarch);
295 else if (len == gdbarch_float_bit (gdbarch))
296 format = gdbarch_float_format (gdbarch);
297 else if (len == gdbarch_double_bit (gdbarch))
298 format = gdbarch_double_format (gdbarch);
299 else if (len == gdbarch_long_double_bit (gdbarch))
300 format = gdbarch_long_double_format (gdbarch);
301 /* On i386 the 'long double' type takes 96 bits,
302 while the real number of used bits is only 80,
303 both in processor and in memory.
304 The code below accepts the real bit size. */
305 else if (gdbarch_long_double_format (gdbarch) != NULL
306 && len == gdbarch_long_double_format (gdbarch)[0]->totalsize)
307 format = gdbarch_long_double_format (gdbarch);
308
309 return format;
310 }
311 \f
312 int
313 generic_convert_register_p (struct gdbarch *gdbarch, int regnum,
314 struct type *type)
315 {
316 return 0;
317 }
318
319 int
320 default_stabs_argument_has_addr (struct gdbarch *gdbarch, struct type *type)
321 {
322 return 0;
323 }
324
325 int
326 generic_instruction_nullified (struct gdbarch *gdbarch,
327 struct regcache *regcache)
328 {
329 return 0;
330 }
331
332 int
333 default_remote_register_number (struct gdbarch *gdbarch,
334 int regno)
335 {
336 return regno;
337 }
338
339 /* See arch-utils.h. */
340
341 int
342 default_vsyscall_range (struct gdbarch *gdbarch, struct mem_range *range)
343 {
344 return 0;
345 }
346
347 \f
348 /* Functions to manipulate the endianness of the target. */
349
350 static enum bfd_endian target_byte_order_user = BFD_ENDIAN_UNKNOWN;
351
352 static const char endian_big[] = "big";
353 static const char endian_little[] = "little";
354 static const char endian_auto[] = "auto";
355 static const char *const endian_enum[] =
356 {
357 endian_big,
358 endian_little,
359 endian_auto,
360 NULL,
361 };
362 static const char *set_endian_string = endian_auto;
363
364 enum bfd_endian
365 selected_byte_order (void)
366 {
367 return target_byte_order_user;
368 }
369
370 /* Called by ``show endian''. */
371
372 static void
373 show_endian (struct ui_file *file, int from_tty, struct cmd_list_element *c,
374 const char *value)
375 {
376 if (target_byte_order_user == BFD_ENDIAN_UNKNOWN)
377 if (gdbarch_byte_order (get_current_arch ()) == BFD_ENDIAN_BIG)
378 gdb_printf (file, _("The target endianness is set automatically "
379 "(currently big endian).\n"));
380 else
381 gdb_printf (file, _("The target endianness is set automatically "
382 "(currently little endian).\n"));
383 else
384 if (target_byte_order_user == BFD_ENDIAN_BIG)
385 gdb_printf (file,
386 _("The target is set to big endian.\n"));
387 else
388 gdb_printf (file,
389 _("The target is set to little endian.\n"));
390 }
391
392 static void
393 set_endian (const char *ignore_args, int from_tty, struct cmd_list_element *c)
394 {
395 struct gdbarch_info info;
396
397 if (set_endian_string == endian_auto)
398 {
399 target_byte_order_user = BFD_ENDIAN_UNKNOWN;
400 if (! gdbarch_update_p (info))
401 internal_error (__FILE__, __LINE__,
402 _("set_endian: architecture update failed"));
403 }
404 else if (set_endian_string == endian_little)
405 {
406 info.byte_order = BFD_ENDIAN_LITTLE;
407 if (! gdbarch_update_p (info))
408 gdb_printf (gdb_stderr,
409 _("Little endian target not supported by GDB\n"));
410 else
411 target_byte_order_user = BFD_ENDIAN_LITTLE;
412 }
413 else if (set_endian_string == endian_big)
414 {
415 info.byte_order = BFD_ENDIAN_BIG;
416 if (! gdbarch_update_p (info))
417 gdb_printf (gdb_stderr,
418 _("Big endian target not supported by GDB\n"));
419 else
420 target_byte_order_user = BFD_ENDIAN_BIG;
421 }
422 else
423 internal_error (__FILE__, __LINE__,
424 _("set_endian: bad value"));
425
426 show_endian (gdb_stdout, from_tty, NULL, NULL);
427 }
428
429 /* Given SELECTED, a currently selected BFD architecture, and
430 TARGET_DESC, the current target description, return what
431 architecture to use.
432
433 SELECTED may be NULL, in which case we return the architecture
434 associated with TARGET_DESC. If SELECTED specifies a variant
435 of the architecture associated with TARGET_DESC, return the
436 more specific of the two.
437
438 If SELECTED is a different architecture, but it is accepted as
439 compatible by the target, we can use the target architecture.
440
441 If SELECTED is obviously incompatible, warn the user. */
442
443 static const struct bfd_arch_info *
444 choose_architecture_for_target (const struct target_desc *target_desc,
445 const struct bfd_arch_info *selected)
446 {
447 const struct bfd_arch_info *from_target = tdesc_architecture (target_desc);
448 const struct bfd_arch_info *compat1, *compat2;
449
450 if (selected == NULL)
451 return from_target;
452
453 if (from_target == NULL)
454 return selected;
455
456 /* struct bfd_arch_info objects are singletons: that is, there's
457 supposed to be exactly one instance for a given machine. So you
458 can tell whether two are equivalent by comparing pointers. */
459 if (from_target == selected)
460 return selected;
461
462 /* BFD's 'A->compatible (A, B)' functions return zero if A and B are
463 incompatible. But if they are compatible, it returns the 'more
464 featureful' of the two arches. That is, if A can run code
465 written for B, but B can't run code written for A, then it'll
466 return A.
467
468 Some targets (e.g. MIPS as of 2006-12-04) don't fully
469 implement this, instead always returning NULL or the first
470 argument. We detect that case by checking both directions. */
471
472 compat1 = selected->compatible (selected, from_target);
473 compat2 = from_target->compatible (from_target, selected);
474
475 if (compat1 == NULL && compat2 == NULL)
476 {
477 /* BFD considers the architectures incompatible. Check our
478 target description whether it accepts SELECTED as compatible
479 anyway. */
480 if (tdesc_compatible_p (target_desc, selected))
481 return from_target;
482
483 warning (_("Selected architecture %s is not compatible "
484 "with reported target architecture %s"),
485 selected->printable_name, from_target->printable_name);
486 return selected;
487 }
488
489 if (compat1 == NULL)
490 return compat2;
491 if (compat2 == NULL)
492 return compat1;
493 if (compat1 == compat2)
494 return compat1;
495
496 /* If the two didn't match, but one of them was a default
497 architecture, assume the more specific one is correct. This
498 handles the case where an executable or target description just
499 says "mips", but the other knows which MIPS variant. */
500 if (compat1->the_default)
501 return compat2;
502 if (compat2->the_default)
503 return compat1;
504
505 /* We have no idea which one is better. This is a bug, but not
506 a critical problem; warn the user. */
507 warning (_("Selected architecture %s is ambiguous with "
508 "reported target architecture %s"),
509 selected->printable_name, from_target->printable_name);
510 return selected;
511 }
512
513 /* Functions to manipulate the architecture of the target. */
514
515 enum set_arch { set_arch_auto, set_arch_manual };
516
517 static const struct bfd_arch_info *target_architecture_user;
518
519 static const char *set_architecture_string;
520
521 const char *
522 selected_architecture_name (void)
523 {
524 if (target_architecture_user == NULL)
525 return NULL;
526 else
527 return set_architecture_string;
528 }
529
530 /* Called if the user enters ``show architecture'' without an
531 argument. */
532
533 static void
534 show_architecture (struct ui_file *file, int from_tty,
535 struct cmd_list_element *c, const char *value)
536 {
537 if (target_architecture_user == NULL)
538 gdb_printf (file, _("The target architecture is set to "
539 "\"auto\" (currently \"%s\").\n"),
540 gdbarch_bfd_arch_info (get_current_arch ())->printable_name);
541 else
542 gdb_printf (file, _("The target architecture is set to \"%s\".\n"),
543 set_architecture_string);
544 }
545
546
547 /* Called if the user enters ``set architecture'' with or without an
548 argument. */
549
550 static void
551 set_architecture (const char *ignore_args,
552 int from_tty, struct cmd_list_element *c)
553 {
554 struct gdbarch_info info;
555
556 if (strcmp (set_architecture_string, "auto") == 0)
557 {
558 target_architecture_user = NULL;
559 if (!gdbarch_update_p (info))
560 internal_error (__FILE__, __LINE__,
561 _("could not select an architecture automatically"));
562 }
563 else
564 {
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_user = info.bfd_arch_info;
571 else
572 gdb_printf (gdb_stderr,
573 _("Architecture `%s' not recognized.\n"),
574 set_architecture_string);
575 }
576 show_architecture (gdb_stdout, from_tty, NULL, NULL);
577 }
578
579 /* Try to select a global architecture that matches "info". Return
580 non-zero if the attempt succeeds. */
581 int
582 gdbarch_update_p (struct gdbarch_info info)
583 {
584 struct gdbarch *new_gdbarch;
585
586 /* Check for the current file. */
587 if (info.abfd == NULL)
588 info.abfd = current_program_space->exec_bfd ();
589 if (info.abfd == NULL)
590 info.abfd = core_bfd;
591
592 /* Check for the current target description. */
593 if (info.target_desc == NULL)
594 info.target_desc = target_current_description ();
595
596 new_gdbarch = gdbarch_find_by_info (info);
597
598 /* If there no architecture by that name, reject the request. */
599 if (new_gdbarch == NULL)
600 {
601 if (gdbarch_debug)
602 gdb_printf (gdb_stdlog, "gdbarch_update_p: "
603 "Architecture not found\n");
604 return 0;
605 }
606
607 /* If it is the same old architecture, accept the request (but don't
608 swap anything). */
609 if (new_gdbarch == target_gdbarch ())
610 {
611 if (gdbarch_debug)
612 gdb_printf (gdb_stdlog, "gdbarch_update_p: "
613 "Architecture %s (%s) unchanged\n",
614 host_address_to_string (new_gdbarch),
615 gdbarch_bfd_arch_info (new_gdbarch)->printable_name);
616 return 1;
617 }
618
619 /* It's a new architecture, swap it in. */
620 if (gdbarch_debug)
621 gdb_printf (gdb_stdlog, "gdbarch_update_p: "
622 "New architecture %s (%s) selected\n",
623 host_address_to_string (new_gdbarch),
624 gdbarch_bfd_arch_info (new_gdbarch)->printable_name);
625 set_target_gdbarch (new_gdbarch);
626
627 return 1;
628 }
629
630 /* Return the architecture for ABFD. If no suitable architecture
631 could be find, return NULL. */
632
633 struct gdbarch *
634 gdbarch_from_bfd (bfd *abfd)
635 {
636 struct gdbarch_info info;
637
638 info.abfd = abfd;
639 return gdbarch_find_by_info (info);
640 }
641
642 /* Set the dynamic target-system-dependent parameters (architecture,
643 byte-order) using information found in the BFD */
644
645 void
646 set_gdbarch_from_file (bfd *abfd)
647 {
648 struct gdbarch_info info;
649 struct gdbarch *gdbarch;
650
651 info.abfd = abfd;
652 info.target_desc = target_current_description ();
653 gdbarch = gdbarch_find_by_info (info);
654
655 if (gdbarch == NULL)
656 error (_("Architecture of file not recognized."));
657 set_target_gdbarch (gdbarch);
658 }
659
660 /* Initialize the current architecture. Update the ``set
661 architecture'' command so that it specifies a list of valid
662 architectures. */
663
664 #ifdef DEFAULT_BFD_ARCH
665 extern const bfd_arch_info_type DEFAULT_BFD_ARCH;
666 static const bfd_arch_info_type *default_bfd_arch = &DEFAULT_BFD_ARCH;
667 #else
668 static const bfd_arch_info_type *default_bfd_arch;
669 #endif
670
671 #ifdef DEFAULT_BFD_VEC
672 extern const bfd_target DEFAULT_BFD_VEC;
673 static const bfd_target *default_bfd_vec = &DEFAULT_BFD_VEC;
674 #else
675 static const bfd_target *default_bfd_vec;
676 #endif
677
678 static enum bfd_endian default_byte_order = BFD_ENDIAN_UNKNOWN;
679
680 /* Printable names of architectures. Used as the enum list of the
681 "set arch" command. */
682 static std::vector<const char *> arches;
683
684 void
685 initialize_current_architecture (void)
686 {
687 arches = gdbarch_printable_names ();
688
689 /* Find a default architecture. */
690 if (default_bfd_arch == NULL)
691 {
692 /* Choose the architecture by taking the first one
693 alphabetically. */
694 const char *chosen = arches[0];
695
696 for (const char *arch : arches)
697 {
698 if (strcmp (arch, chosen) < 0)
699 chosen = arch;
700 }
701
702 if (chosen == NULL)
703 internal_error (__FILE__, __LINE__,
704 _("initialize_current_architecture: No arch"));
705
706 default_bfd_arch = bfd_scan_arch (chosen);
707 if (default_bfd_arch == NULL)
708 internal_error (__FILE__, __LINE__,
709 _("initialize_current_architecture: Arch not found"));
710 }
711
712 gdbarch_info info;
713 info.bfd_arch_info = default_bfd_arch;
714
715 /* Take several guesses at a byte order. */
716 if (default_byte_order == BFD_ENDIAN_UNKNOWN
717 && default_bfd_vec != NULL)
718 {
719 /* Extract BFD's default vector's byte order. */
720 switch (default_bfd_vec->byteorder)
721 {
722 case BFD_ENDIAN_BIG:
723 default_byte_order = BFD_ENDIAN_BIG;
724 break;
725 case BFD_ENDIAN_LITTLE:
726 default_byte_order = BFD_ENDIAN_LITTLE;
727 break;
728 default:
729 break;
730 }
731 }
732 if (default_byte_order == BFD_ENDIAN_UNKNOWN)
733 {
734 /* look for ``*el-*'' in the target name. */
735 const char *chp;
736 chp = strchr (target_name, '-');
737 if (chp != NULL
738 && chp - 2 >= target_name
739 && startswith (chp - 2, "el"))
740 default_byte_order = BFD_ENDIAN_LITTLE;
741 }
742 if (default_byte_order == BFD_ENDIAN_UNKNOWN)
743 {
744 /* Wire it to big-endian!!! */
745 default_byte_order = BFD_ENDIAN_BIG;
746 }
747
748 info.byte_order = default_byte_order;
749 info.byte_order_for_code = info.byte_order;
750
751 if (! gdbarch_update_p (info))
752 internal_error (__FILE__, __LINE__,
753 _("initialize_current_architecture: Selection of "
754 "initial architecture failed"));
755
756 /* Create the ``set architecture'' command appending ``auto'' to the
757 list of architectures. */
758 {
759 /* Append ``auto''. */
760 set_architecture_string = "auto";
761 arches.push_back (set_architecture_string);
762 arches.push_back (nullptr);
763 set_show_commands architecture_cmds
764 = add_setshow_enum_cmd ("architecture", class_support,
765 arches.data (), &set_architecture_string,
766 _("Set architecture of target."),
767 _("Show architecture of target."), NULL,
768 set_architecture, show_architecture,
769 &setlist, &showlist);
770 add_alias_cmd ("processor", architecture_cmds.set, class_support, 1,
771 &setlist);
772 }
773 }
774
775 /* Similar to init, but this time fill in the blanks. Information is
776 obtained from the global "set ..." options and explicitly
777 initialized INFO fields. */
778
779 void
780 gdbarch_info_fill (struct gdbarch_info *info)
781 {
782 /* "(gdb) set architecture ...". */
783 if (info->bfd_arch_info == NULL
784 && target_architecture_user)
785 info->bfd_arch_info = target_architecture_user;
786 /* From the file. */
787 if (info->bfd_arch_info == NULL
788 && info->abfd != NULL
789 && bfd_get_arch (info->abfd) != bfd_arch_unknown
790 && bfd_get_arch (info->abfd) != bfd_arch_obscure)
791 info->bfd_arch_info = bfd_get_arch_info (info->abfd);
792 /* From the target. */
793 if (info->target_desc != NULL)
794 info->bfd_arch_info = choose_architecture_for_target
795 (info->target_desc, info->bfd_arch_info);
796 /* From the default. */
797 if (info->bfd_arch_info == NULL)
798 info->bfd_arch_info = default_bfd_arch;
799
800 /* "(gdb) set byte-order ...". */
801 if (info->byte_order == BFD_ENDIAN_UNKNOWN
802 && target_byte_order_user != BFD_ENDIAN_UNKNOWN)
803 info->byte_order = target_byte_order_user;
804 /* From the INFO struct. */
805 if (info->byte_order == BFD_ENDIAN_UNKNOWN
806 && info->abfd != NULL)
807 info->byte_order = (bfd_big_endian (info->abfd) ? BFD_ENDIAN_BIG
808 : bfd_little_endian (info->abfd) ? BFD_ENDIAN_LITTLE
809 : BFD_ENDIAN_UNKNOWN);
810 /* From the default. */
811 if (info->byte_order == BFD_ENDIAN_UNKNOWN)
812 info->byte_order = default_byte_order;
813 info->byte_order_for_code = info->byte_order;
814 /* Wire the default to the last selected byte order. */
815 default_byte_order = info->byte_order;
816
817 /* "(gdb) set osabi ...". Handled by gdbarch_lookup_osabi. */
818 /* From the manual override, or from file. */
819 if (info->osabi == GDB_OSABI_UNKNOWN)
820 info->osabi = gdbarch_lookup_osabi (info->abfd);
821 /* From the target. */
822
823 if (info->osabi == GDB_OSABI_UNKNOWN && info->target_desc != NULL)
824 info->osabi = tdesc_osabi (info->target_desc);
825 /* From the configured default. */
826 #ifdef GDB_OSABI_DEFAULT
827 if (info->osabi == GDB_OSABI_UNKNOWN)
828 info->osabi = GDB_OSABI_DEFAULT;
829 #endif
830 /* If we still don't know which osabi to pick, pick none. */
831 if (info->osabi == GDB_OSABI_UNKNOWN)
832 info->osabi = GDB_OSABI_NONE;
833
834 /* Must have at least filled in the architecture. */
835 gdb_assert (info->bfd_arch_info != NULL);
836 }
837
838 /* Return "current" architecture. If the target is running, this is
839 the architecture of the selected frame. Otherwise, the "current"
840 architecture defaults to the target architecture.
841
842 This function should normally be called solely by the command
843 interpreter routines to determine the architecture to execute a
844 command in. */
845 struct gdbarch *
846 get_current_arch (void)
847 {
848 if (has_stack_frames ())
849 return get_frame_arch (get_selected_frame (NULL));
850 else
851 return target_gdbarch ();
852 }
853
854 int
855 default_has_shared_address_space (struct gdbarch *gdbarch)
856 {
857 /* Simply say no. In most unix-like targets each inferior/process
858 has its own address space. */
859 return 0;
860 }
861
862 int
863 default_fast_tracepoint_valid_at (struct gdbarch *gdbarch, CORE_ADDR addr,
864 std::string *msg)
865 {
866 /* We don't know if maybe the target has some way to do fast
867 tracepoints that doesn't need gdbarch, so always say yes. */
868 if (msg)
869 msg->clear ();
870 return 1;
871 }
872
873 const gdb_byte *
874 default_breakpoint_from_pc (struct gdbarch *gdbarch, CORE_ADDR *pcptr,
875 int *lenptr)
876 {
877 int kind = gdbarch_breakpoint_kind_from_pc (gdbarch, pcptr);
878
879 return gdbarch_sw_breakpoint_from_kind (gdbarch, kind, lenptr);
880 }
881 int
882 default_breakpoint_kind_from_current_state (struct gdbarch *gdbarch,
883 struct regcache *regcache,
884 CORE_ADDR *pcptr)
885 {
886 return gdbarch_breakpoint_kind_from_pc (gdbarch, pcptr);
887 }
888
889
890 void
891 default_gen_return_address (struct gdbarch *gdbarch,
892 struct agent_expr *ax, struct axs_value *value,
893 CORE_ADDR scope)
894 {
895 error (_("This architecture has no method to collect a return address."));
896 }
897
898 int
899 default_return_in_first_hidden_param_p (struct gdbarch *gdbarch,
900 struct type *type)
901 {
902 /* Usually, the return value's address is stored the in the "first hidden"
903 parameter if the return value should be passed by reference, as
904 specified in ABI. */
905 return !(language_pass_by_reference (type).trivially_copyable);
906 }
907
908 int default_insn_is_call (struct gdbarch *gdbarch, CORE_ADDR addr)
909 {
910 return 0;
911 }
912
913 int default_insn_is_ret (struct gdbarch *gdbarch, CORE_ADDR addr)
914 {
915 return 0;
916 }
917
918 int default_insn_is_jump (struct gdbarch *gdbarch, CORE_ADDR addr)
919 {
920 return 0;
921 }
922
923 /* See arch-utils.h. */
924
925 bool
926 default_program_breakpoint_here_p (struct gdbarch *gdbarch,
927 CORE_ADDR address)
928 {
929 int len;
930 const gdb_byte *bpoint = gdbarch_breakpoint_from_pc (gdbarch, &address, &len);
931
932 /* Software breakpoints unsupported? */
933 if (bpoint == nullptr)
934 return false;
935
936 gdb_byte *target_mem = (gdb_byte *) alloca (len);
937
938 /* Enable the automatic memory restoration from breakpoints while
939 we read the memory. Otherwise we may find temporary breakpoints, ones
940 inserted by GDB, and flag them as permanent breakpoints. */
941 scoped_restore restore_memory
942 = make_scoped_restore_show_memory_breakpoints (0);
943
944 if (target_read_memory (address, target_mem, len) == 0)
945 {
946 /* Check if this is a breakpoint instruction for this architecture,
947 including ones used by GDB. */
948 if (memcmp (target_mem, bpoint, len) == 0)
949 return true;
950 }
951
952 return false;
953 }
954
955 void
956 default_skip_permanent_breakpoint (struct regcache *regcache)
957 {
958 struct gdbarch *gdbarch = regcache->arch ();
959 CORE_ADDR current_pc = regcache_read_pc (regcache);
960 int bp_len;
961
962 gdbarch_breakpoint_from_pc (gdbarch, &current_pc, &bp_len);
963 current_pc += bp_len;
964 regcache_write_pc (regcache, current_pc);
965 }
966
967 CORE_ADDR
968 default_infcall_mmap (CORE_ADDR size, unsigned prot)
969 {
970 error (_("This target does not support inferior memory allocation by mmap."));
971 }
972
973 void
974 default_infcall_munmap (CORE_ADDR addr, CORE_ADDR size)
975 {
976 /* Memory reserved by inferior mmap is kept leaked. */
977 }
978
979 /* -mcmodel=large is used so that no GOT (Global Offset Table) is needed to be
980 created in inferior memory by GDB (normally it is set by ld.so). */
981
982 std::string
983 default_gcc_target_options (struct gdbarch *gdbarch)
984 {
985 return string_printf ("-m%d%s", gdbarch_ptr_bit (gdbarch),
986 (gdbarch_ptr_bit (gdbarch) == 64
987 ? " -mcmodel=large" : ""));
988 }
989
990 /* gdbarch gnu_triplet_regexp method. */
991
992 const char *
993 default_gnu_triplet_regexp (struct gdbarch *gdbarch)
994 {
995 return gdbarch_bfd_arch_info (gdbarch)->arch_name;
996 }
997
998 /* Default method for gdbarch_addressable_memory_unit_size. The default is
999 based on the bits_per_byte defined in the bfd library for the current
1000 architecture, this is usually 8-bits, and so this function will usually
1001 return 1 indicating 1 byte is 1 octet. */
1002
1003 int
1004 default_addressable_memory_unit_size (struct gdbarch *gdbarch)
1005 {
1006 return gdbarch_bfd_arch_info (gdbarch)->bits_per_byte / 8;
1007 }
1008
1009 void
1010 default_guess_tracepoint_registers (struct gdbarch *gdbarch,
1011 struct regcache *regcache,
1012 CORE_ADDR addr)
1013 {
1014 int pc_regno = gdbarch_pc_regnum (gdbarch);
1015 gdb_byte *regs;
1016
1017 /* This guessing code below only works if the PC register isn't
1018 a pseudo-register. The value of a pseudo-register isn't stored
1019 in the (non-readonly) regcache -- instead it's recomputed
1020 (probably from some other cached raw register) whenever the
1021 register is read. In this case, a custom method implementation
1022 should be used by the architecture. */
1023 if (pc_regno < 0 || pc_regno >= gdbarch_num_regs (gdbarch))
1024 return;
1025
1026 regs = (gdb_byte *) alloca (register_size (gdbarch, pc_regno));
1027 store_unsigned_integer (regs, register_size (gdbarch, pc_regno),
1028 gdbarch_byte_order (gdbarch), addr);
1029 regcache->raw_supply (pc_regno, regs);
1030 }
1031
1032 int
1033 default_print_insn (bfd_vma memaddr, disassemble_info *info)
1034 {
1035 disassembler_ftype disassemble_fn;
1036
1037 disassemble_fn = disassembler (info->arch, info->endian == BFD_ENDIAN_BIG,
1038 info->mach, current_program_space->exec_bfd ());
1039
1040 gdb_assert (disassemble_fn != NULL);
1041 return (*disassemble_fn) (memaddr, info);
1042 }
1043
1044 /* See arch-utils.h. */
1045
1046 CORE_ADDR
1047 gdbarch_skip_prologue_noexcept (gdbarch *gdbarch, CORE_ADDR pc) noexcept
1048 {
1049 CORE_ADDR new_pc = pc;
1050
1051 try
1052 {
1053 new_pc = gdbarch_skip_prologue (gdbarch, pc);
1054 }
1055 catch (const gdb_exception &ex)
1056 {}
1057
1058 return new_pc;
1059 }
1060
1061 /* See arch-utils.h. */
1062
1063 bool
1064 default_in_indirect_branch_thunk (gdbarch *gdbarch, CORE_ADDR pc)
1065 {
1066 return false;
1067 }
1068
1069 /* See arch-utils.h. */
1070
1071 ULONGEST
1072 default_type_align (struct gdbarch *gdbarch, struct type *type)
1073 {
1074 return 0;
1075 }
1076
1077 /* See arch-utils.h. */
1078
1079 std::string
1080 default_get_pc_address_flags (frame_info *frame, CORE_ADDR pc)
1081 {
1082 return "";
1083 }
1084
1085 /* See arch-utils.h. */
1086 void
1087 default_read_core_file_mappings
1088 (struct gdbarch *gdbarch,
1089 struct bfd *cbfd,
1090 read_core_file_mappings_pre_loop_ftype pre_loop_cb,
1091 read_core_file_mappings_loop_ftype loop_cb)
1092 {
1093 }
1094
1095 /* Static function declarations */
1096
1097 static void alloc_gdbarch_data (struct gdbarch *);
1098
1099 /* Non-zero if we want to trace architecture code. */
1100
1101 #ifndef GDBARCH_DEBUG
1102 #define GDBARCH_DEBUG 0
1103 #endif
1104 unsigned int gdbarch_debug = GDBARCH_DEBUG;
1105 static void
1106 show_gdbarch_debug (struct ui_file *file, int from_tty,
1107 struct cmd_list_element *c, const char *value)
1108 {
1109 gdb_printf (file, _("Architecture debugging is %s.\n"), value);
1110 }
1111
1112 static const char *
1113 pformat (const struct floatformat **format)
1114 {
1115 if (format == NULL)
1116 return "(null)";
1117 else
1118 /* Just print out one of them - this is only for diagnostics. */
1119 return format[0]->name;
1120 }
1121
1122 static const char *
1123 pstring (const char *string)
1124 {
1125 if (string == NULL)
1126 return "(null)";
1127 return string;
1128 }
1129
1130 static const char *
1131 pstring_ptr (char **string)
1132 {
1133 if (string == NULL || *string == NULL)
1134 return "(null)";
1135 return *string;
1136 }
1137
1138 /* Helper function to print a list of strings, represented as "const
1139 char *const *". The list is printed comma-separated. */
1140
1141 static const char *
1142 pstring_list (const char *const *list)
1143 {
1144 static char ret[100];
1145 const char *const *p;
1146 size_t offset = 0;
1147
1148 if (list == NULL)
1149 return "(null)";
1150
1151 ret[0] = '\0';
1152 for (p = list; *p != NULL && offset < sizeof (ret); ++p)
1153 {
1154 size_t s = xsnprintf (ret + offset, sizeof (ret) - offset, "%s, ", *p);
1155 offset += 2 + s;
1156 }
1157
1158 if (offset > 0)
1159 {
1160 gdb_assert (offset - 2 < sizeof (ret));
1161 ret[offset - 2] = '\0';
1162 }
1163
1164 return ret;
1165 }
1166
1167 #include "gdbarch.c"
1168
1169 obstack *gdbarch_obstack (gdbarch *arch)
1170 {
1171 return arch->obstack;
1172 }
1173
1174 /* See gdbarch.h. */
1175
1176 char *
1177 gdbarch_obstack_strdup (struct gdbarch *arch, const char *string)
1178 {
1179 return obstack_strdup (arch->obstack, string);
1180 }
1181
1182
1183 /* Free a gdbarch struct. This should never happen in normal
1184 operation --- once you've created a gdbarch, you keep it around.
1185 However, if an architecture's init function encounters an error
1186 building the structure, it may need to clean up a partially
1187 constructed gdbarch. */
1188
1189 void
1190 gdbarch_free (struct gdbarch *arch)
1191 {
1192 struct obstack *obstack;
1193
1194 gdb_assert (arch != NULL);
1195 gdb_assert (!arch->initialized_p);
1196 obstack = arch->obstack;
1197 obstack_free (obstack, 0); /* Includes the ARCH. */
1198 xfree (obstack);
1199 }
1200
1201 struct gdbarch_tdep *
1202 gdbarch_tdep (struct gdbarch *gdbarch)
1203 {
1204 if (gdbarch_debug >= 2)
1205 gdb_printf (gdb_stdlog, "gdbarch_tdep called\n");
1206 return gdbarch->tdep;
1207 }
1208
1209 /* Keep a registry of per-architecture data-pointers required by GDB
1210 modules. */
1211
1212 struct gdbarch_data
1213 {
1214 unsigned index;
1215 int init_p;
1216 gdbarch_data_pre_init_ftype *pre_init;
1217 gdbarch_data_post_init_ftype *post_init;
1218 };
1219
1220 struct gdbarch_data_registration
1221 {
1222 struct gdbarch_data *data;
1223 struct gdbarch_data_registration *next;
1224 };
1225
1226 struct gdbarch_data_registry
1227 {
1228 unsigned nr;
1229 struct gdbarch_data_registration *registrations;
1230 };
1231
1232 static struct gdbarch_data_registry gdbarch_data_registry =
1233 {
1234 0, NULL,
1235 };
1236
1237 static struct gdbarch_data *
1238 gdbarch_data_register (gdbarch_data_pre_init_ftype *pre_init,
1239 gdbarch_data_post_init_ftype *post_init)
1240 {
1241 struct gdbarch_data_registration **curr;
1242
1243 /* Append the new registration. */
1244 for (curr = &gdbarch_data_registry.registrations;
1245 (*curr) != NULL;
1246 curr = &(*curr)->next);
1247 (*curr) = XNEW (struct gdbarch_data_registration);
1248 (*curr)->next = NULL;
1249 (*curr)->data = XNEW (struct gdbarch_data);
1250 (*curr)->data->index = gdbarch_data_registry.nr++;
1251 (*curr)->data->pre_init = pre_init;
1252 (*curr)->data->post_init = post_init;
1253 (*curr)->data->init_p = 1;
1254 return (*curr)->data;
1255 }
1256
1257 struct gdbarch_data *
1258 gdbarch_data_register_pre_init (gdbarch_data_pre_init_ftype *pre_init)
1259 {
1260 return gdbarch_data_register (pre_init, NULL);
1261 }
1262
1263 struct gdbarch_data *
1264 gdbarch_data_register_post_init (gdbarch_data_post_init_ftype *post_init)
1265 {
1266 return gdbarch_data_register (NULL, post_init);
1267 }
1268
1269 /* Create/delete the gdbarch data vector. */
1270
1271 static void
1272 alloc_gdbarch_data (struct gdbarch *gdbarch)
1273 {
1274 gdb_assert (gdbarch->data == NULL);
1275 gdbarch->nr_data = gdbarch_data_registry.nr;
1276 gdbarch->data = GDBARCH_OBSTACK_CALLOC (gdbarch, gdbarch->nr_data, void *);
1277 }
1278
1279 /* Return the current value of the specified per-architecture
1280 data-pointer. */
1281
1282 void *
1283 gdbarch_data (struct gdbarch *gdbarch, struct gdbarch_data *data)
1284 {
1285 gdb_assert (data->index < gdbarch->nr_data);
1286 if (gdbarch->data[data->index] == NULL)
1287 {
1288 /* The data-pointer isn't initialized, call init() to get a
1289 value. */
1290 if (data->pre_init != NULL)
1291 /* Mid architecture creation: pass just the obstack, and not
1292 the entire architecture, as that way it isn't possible for
1293 pre-init code to refer to undefined architecture
1294 fields. */
1295 gdbarch->data[data->index] = data->pre_init (gdbarch->obstack);
1296 else if (gdbarch->initialized_p
1297 && data->post_init != NULL)
1298 /* Post architecture creation: pass the entire architecture
1299 (as all fields are valid), but be careful to also detect
1300 recursive references. */
1301 {
1302 gdb_assert (data->init_p);
1303 data->init_p = 0;
1304 gdbarch->data[data->index] = data->post_init (gdbarch);
1305 data->init_p = 1;
1306 }
1307 else
1308 internal_error (__FILE__, __LINE__,
1309 _("gdbarch post-init data field can only be used "
1310 "after gdbarch is fully initialised"));
1311 gdb_assert (gdbarch->data[data->index] != NULL);
1312 }
1313 return gdbarch->data[data->index];
1314 }
1315
1316
1317 /* Keep a registry of the architectures known by GDB. */
1318
1319 struct gdbarch_registration
1320 {
1321 enum bfd_architecture bfd_architecture;
1322 gdbarch_init_ftype *init;
1323 gdbarch_dump_tdep_ftype *dump_tdep;
1324 struct gdbarch_list *arches;
1325 struct gdbarch_registration *next;
1326 };
1327
1328 static struct gdbarch_registration *gdbarch_registry = NULL;
1329
1330 std::vector<const char *>
1331 gdbarch_printable_names ()
1332 {
1333 /* Accumulate a list of names based on the registed list of
1334 architectures. */
1335 std::vector<const char *> arches;
1336
1337 for (gdbarch_registration *rego = gdbarch_registry;
1338 rego != nullptr;
1339 rego = rego->next)
1340 {
1341 const struct bfd_arch_info *ap
1342 = bfd_lookup_arch (rego->bfd_architecture, 0);
1343 if (ap == nullptr)
1344 internal_error (__FILE__, __LINE__,
1345 _("gdbarch_architecture_names: multi-arch unknown"));
1346 do
1347 {
1348 arches.push_back (ap->printable_name);
1349 ap = ap->next;
1350 }
1351 while (ap != NULL);
1352 }
1353
1354 return arches;
1355 }
1356
1357
1358 void
1359 gdbarch_register (enum bfd_architecture bfd_architecture,
1360 gdbarch_init_ftype *init,
1361 gdbarch_dump_tdep_ftype *dump_tdep)
1362 {
1363 struct gdbarch_registration **curr;
1364 const struct bfd_arch_info *bfd_arch_info;
1365
1366 /* Check that BFD recognizes this architecture */
1367 bfd_arch_info = bfd_lookup_arch (bfd_architecture, 0);
1368 if (bfd_arch_info == NULL)
1369 {
1370 internal_error (__FILE__, __LINE__,
1371 _("gdbarch: Attempt to register "
1372 "unknown architecture (%d)"),
1373 bfd_architecture);
1374 }
1375 /* Check that we haven't seen this architecture before. */
1376 for (curr = &gdbarch_registry;
1377 (*curr) != NULL;
1378 curr = &(*curr)->next)
1379 {
1380 if (bfd_architecture == (*curr)->bfd_architecture)
1381 internal_error (__FILE__, __LINE__,
1382 _("gdbarch: Duplicate registration "
1383 "of architecture (%s)"),
1384 bfd_arch_info->printable_name);
1385 }
1386 /* log it */
1387 if (gdbarch_debug)
1388 gdb_printf (gdb_stdlog, "register_gdbarch_init (%s, %s)\n",
1389 bfd_arch_info->printable_name,
1390 host_address_to_string (init));
1391 /* Append it */
1392 (*curr) = XNEW (struct gdbarch_registration);
1393 (*curr)->bfd_architecture = bfd_architecture;
1394 (*curr)->init = init;
1395 (*curr)->dump_tdep = dump_tdep;
1396 (*curr)->arches = NULL;
1397 (*curr)->next = NULL;
1398 }
1399
1400 void
1401 register_gdbarch_init (enum bfd_architecture bfd_architecture,
1402 gdbarch_init_ftype *init)
1403 {
1404 gdbarch_register (bfd_architecture, init, NULL);
1405 }
1406
1407
1408 /* Look for an architecture using gdbarch_info. */
1409
1410 struct gdbarch_list *
1411 gdbarch_list_lookup_by_info (struct gdbarch_list *arches,
1412 const struct gdbarch_info *info)
1413 {
1414 for (; arches != NULL; arches = arches->next)
1415 {
1416 if (info->bfd_arch_info != arches->gdbarch->bfd_arch_info)
1417 continue;
1418 if (info->byte_order != arches->gdbarch->byte_order)
1419 continue;
1420 if (info->osabi != arches->gdbarch->osabi)
1421 continue;
1422 if (info->target_desc != arches->gdbarch->target_desc)
1423 continue;
1424 return arches;
1425 }
1426 return NULL;
1427 }
1428
1429
1430 /* Find an architecture that matches the specified INFO. Create a new
1431 architecture if needed. Return that new architecture. */
1432
1433 struct gdbarch *
1434 gdbarch_find_by_info (struct gdbarch_info info)
1435 {
1436 struct gdbarch *new_gdbarch;
1437 struct gdbarch_registration *rego;
1438
1439 /* Fill in missing parts of the INFO struct using a number of
1440 sources: "set ..."; INFOabfd supplied; and the global
1441 defaults. */
1442 gdbarch_info_fill (&info);
1443
1444 /* Must have found some sort of architecture. */
1445 gdb_assert (info.bfd_arch_info != NULL);
1446
1447 if (gdbarch_debug)
1448 {
1449 gdb_printf (gdb_stdlog,
1450 "gdbarch_find_by_info: info.bfd_arch_info %s\n",
1451 (info.bfd_arch_info != NULL
1452 ? info.bfd_arch_info->printable_name
1453 : "(null)"));
1454 gdb_printf (gdb_stdlog,
1455 "gdbarch_find_by_info: info.byte_order %d (%s)\n",
1456 info.byte_order,
1457 (info.byte_order == BFD_ENDIAN_BIG ? "big"
1458 : info.byte_order == BFD_ENDIAN_LITTLE ? "little"
1459 : "default"));
1460 gdb_printf (gdb_stdlog,
1461 "gdbarch_find_by_info: info.osabi %d (%s)\n",
1462 info.osabi, gdbarch_osabi_name (info.osabi));
1463 gdb_printf (gdb_stdlog,
1464 "gdbarch_find_by_info: info.abfd %s\n",
1465 host_address_to_string (info.abfd));
1466 }
1467
1468 /* Find the tdep code that knows about this architecture. */
1469 for (rego = gdbarch_registry;
1470 rego != NULL;
1471 rego = rego->next)
1472 if (rego->bfd_architecture == info.bfd_arch_info->arch)
1473 break;
1474 if (rego == NULL)
1475 {
1476 if (gdbarch_debug)
1477 gdb_printf (gdb_stdlog, "gdbarch_find_by_info: "
1478 "No matching architecture\n");
1479 return 0;
1480 }
1481
1482 /* Ask the tdep code for an architecture that matches "info". */
1483 new_gdbarch = rego->init (info, rego->arches);
1484
1485 /* Did the tdep code like it? No. Reject the change and revert to
1486 the old architecture. */
1487 if (new_gdbarch == NULL)
1488 {
1489 if (gdbarch_debug)
1490 gdb_printf (gdb_stdlog, "gdbarch_find_by_info: "
1491 "Target rejected architecture\n");
1492 return NULL;
1493 }
1494
1495 /* Is this a pre-existing architecture (as determined by already
1496 being initialized)? Move it to the front of the architecture
1497 list (keeping the list sorted Most Recently Used). */
1498 if (new_gdbarch->initialized_p)
1499 {
1500 struct gdbarch_list **list;
1501 struct gdbarch_list *self;
1502 if (gdbarch_debug)
1503 gdb_printf (gdb_stdlog, "gdbarch_find_by_info: "
1504 "Previous architecture %s (%s) selected\n",
1505 host_address_to_string (new_gdbarch),
1506 new_gdbarch->bfd_arch_info->printable_name);
1507 /* Find the existing arch in the list. */
1508 for (list = &rego->arches;
1509 (*list) != NULL && (*list)->gdbarch != new_gdbarch;
1510 list = &(*list)->next);
1511 /* It had better be in the list of architectures. */
1512 gdb_assert ((*list) != NULL && (*list)->gdbarch == new_gdbarch);
1513 /* Unlink SELF. */
1514 self = (*list);
1515 (*list) = self->next;
1516 /* Insert SELF at the front. */
1517 self->next = rego->arches;
1518 rego->arches = self;
1519 /* Return it. */
1520 return new_gdbarch;
1521 }
1522
1523 /* It's a new architecture. */
1524 if (gdbarch_debug)
1525 gdb_printf (gdb_stdlog, "gdbarch_find_by_info: "
1526 "New architecture %s (%s) selected\n",
1527 host_address_to_string (new_gdbarch),
1528 new_gdbarch->bfd_arch_info->printable_name);
1529
1530 /* Insert the new architecture into the front of the architecture
1531 list (keep the list sorted Most Recently Used). */
1532 {
1533 struct gdbarch_list *self = XNEW (struct gdbarch_list);
1534 self->next = rego->arches;
1535 self->gdbarch = new_gdbarch;
1536 rego->arches = self;
1537 }
1538
1539 /* Check that the newly installed architecture is valid. Plug in
1540 any post init values. */
1541 new_gdbarch->dump_tdep = rego->dump_tdep;
1542 verify_gdbarch (new_gdbarch);
1543 new_gdbarch->initialized_p = 1;
1544
1545 if (gdbarch_debug)
1546 gdbarch_dump (new_gdbarch, gdb_stdlog);
1547
1548 return new_gdbarch;
1549 }
1550
1551 /* Make the specified architecture current. */
1552
1553 void
1554 set_target_gdbarch (struct gdbarch *new_gdbarch)
1555 {
1556 gdb_assert (new_gdbarch != NULL);
1557 gdb_assert (new_gdbarch->initialized_p);
1558 current_inferior ()->gdbarch = new_gdbarch;
1559 gdb::observers::architecture_changed.notify (new_gdbarch);
1560 registers_changed ();
1561 }
1562
1563 /* Return the current inferior's arch. */
1564
1565 struct gdbarch *
1566 target_gdbarch (void)
1567 {
1568 return current_inferior ()->gdbarch;
1569 }
1570
1571 void _initialize_gdbarch_utils ();
1572 void
1573 _initialize_gdbarch_utils ()
1574 {
1575 add_setshow_enum_cmd ("endian", class_support,
1576 endian_enum, &set_endian_string,
1577 _("Set endianness of target."),
1578 _("Show endianness of target."),
1579 NULL, set_endian, show_endian,
1580 &setlist, &showlist);
1581 add_setshow_zuinteger_cmd ("arch", class_maintenance, &gdbarch_debug, _("\
1582 Set architecture debugging."), _("\
1583 Show architecture debugging."), _("\
1584 When non-zero, architecture debugging is enabled."),
1585 NULL,
1586 show_gdbarch_debug,
1587 &setdebuglist, &showdebuglist);
1588 }