1 /* Target description support for GDB.
3 Copyright (C) 2006-2019 Free Software Foundation, Inc.
5 Contributed by CodeSourcery.
7 This file is part of GDB.
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program. If not, see <http://www.gnu.org/licenses/>. */
23 #include "arch-utils.h"
26 #include "reggroups.h"
28 #include "target-descriptions.h"
29 #include "gdbsupport/vec.h"
30 #include "xml-support.h"
31 #include "xml-tdesc.h"
34 #include "gdb_obstack.h"
38 #include "completer.h"
39 #include "readline/tilde.h" /* tilde_expand */
45 property (const std::string &key_, const std::string &value_)
46 : key (key_), value (value_)
53 /* Convert a tdesc_type to a gdb type. */
56 make_gdb_type (struct gdbarch *gdbarch, struct tdesc_type *ttype)
58 class gdb_type_creator : public tdesc_element_visitor
61 gdb_type_creator (struct gdbarch *gdbarch)
70 void visit (const tdesc_type_builtin *e) override
74 /* Predefined types. */
76 m_type = builtin_type (m_gdbarch)->builtin_bool;
79 m_type = builtin_type (m_gdbarch)->builtin_int8;
81 case TDESC_TYPE_INT16:
82 m_type = builtin_type (m_gdbarch)->builtin_int16;
84 case TDESC_TYPE_INT32:
85 m_type = builtin_type (m_gdbarch)->builtin_int32;
87 case TDESC_TYPE_INT64:
88 m_type = builtin_type (m_gdbarch)->builtin_int64;
90 case TDESC_TYPE_INT128:
91 m_type = builtin_type (m_gdbarch)->builtin_int128;
93 case TDESC_TYPE_UINT8:
94 m_type = builtin_type (m_gdbarch)->builtin_uint8;
96 case TDESC_TYPE_UINT16:
97 m_type = builtin_type (m_gdbarch)->builtin_uint16;
99 case TDESC_TYPE_UINT32:
100 m_type = builtin_type (m_gdbarch)->builtin_uint32;
102 case TDESC_TYPE_UINT64:
103 m_type = builtin_type (m_gdbarch)->builtin_uint64;
105 case TDESC_TYPE_UINT128:
106 m_type = builtin_type (m_gdbarch)->builtin_uint128;
108 case TDESC_TYPE_CODE_PTR:
109 m_type = builtin_type (m_gdbarch)->builtin_func_ptr;
111 case TDESC_TYPE_DATA_PTR:
112 m_type = builtin_type (m_gdbarch)->builtin_data_ptr;
116 m_type = tdesc_find_type (m_gdbarch, e->name.c_str ());
122 case TDESC_TYPE_IEEE_HALF:
123 m_type = arch_float_type (m_gdbarch, -1, "builtin_type_ieee_half",
124 floatformats_ieee_half);
127 case TDESC_TYPE_IEEE_SINGLE:
128 m_type = arch_float_type (m_gdbarch, -1, "builtin_type_ieee_single",
129 floatformats_ieee_single);
132 case TDESC_TYPE_IEEE_DOUBLE:
133 m_type = arch_float_type (m_gdbarch, -1, "builtin_type_ieee_double",
134 floatformats_ieee_double);
136 case TDESC_TYPE_ARM_FPA_EXT:
137 m_type = arch_float_type (m_gdbarch, -1, "builtin_type_arm_ext",
138 floatformats_arm_ext);
141 case TDESC_TYPE_I387_EXT:
142 m_type = arch_float_type (m_gdbarch, -1, "builtin_type_i387_ext",
143 floatformats_i387_ext);
147 internal_error (__FILE__, __LINE__,
148 "Type \"%s\" has an unknown kind %d",
149 e->name.c_str (), e->kind);
152 void visit (const tdesc_type_vector *e) override
154 m_type = tdesc_find_type (m_gdbarch, e->name.c_str ());
158 type *element_gdb_type = make_gdb_type (m_gdbarch, e->element_type);
159 m_type = init_vector_type (element_gdb_type, e->count);
160 TYPE_NAME (m_type) = xstrdup (e->name.c_str ());
164 void visit (const tdesc_type_with_fields *e) override
166 m_type = tdesc_find_type (m_gdbarch, e->name.c_str ());
172 case TDESC_TYPE_STRUCT:
173 make_gdb_type_struct (e);
175 case TDESC_TYPE_UNION:
176 make_gdb_type_union (e);
178 case TDESC_TYPE_FLAGS:
179 make_gdb_type_flags (e);
181 case TDESC_TYPE_ENUM:
182 make_gdb_type_enum (e);
186 internal_error (__FILE__, __LINE__,
187 "Type \"%s\" has an unknown kind %d",
188 e->name.c_str (), e->kind);
193 void make_gdb_type_struct (const tdesc_type_with_fields *e)
195 m_type = arch_composite_type (m_gdbarch, NULL, TYPE_CODE_STRUCT);
196 TYPE_NAME (m_type) = xstrdup (e->name.c_str ());
198 for (const tdesc_type_field &f : e->fields)
200 if (f.start != -1 && f.end != -1)
204 struct type *field_gdb_type;
205 int bitsize, total_size;
207 /* This invariant should be preserved while creating types. */
208 gdb_assert (e->size != 0);
210 field_gdb_type = make_gdb_type (m_gdbarch, f.type);
211 else if (e->size > 4)
212 field_gdb_type = builtin_type (m_gdbarch)->builtin_uint64;
214 field_gdb_type = builtin_type (m_gdbarch)->builtin_uint32;
216 fld = append_composite_type_field_raw
217 (m_type, xstrdup (f.name.c_str ()), field_gdb_type);
219 /* For little-endian, BITPOS counts from the LSB of
220 the structure and marks the LSB of the field. For
221 big-endian, BITPOS counts from the MSB of the
222 structure and marks the MSB of the field. Either
223 way, it is the number of bits to the "left" of the
224 field. To calculate this in big-endian, we need
225 the total size of the structure. */
226 bitsize = f.end - f.start + 1;
227 total_size = e->size * TARGET_CHAR_BIT;
228 if (gdbarch_bits_big_endian (m_gdbarch))
229 SET_FIELD_BITPOS (fld[0], total_size - f.start - bitsize);
231 SET_FIELD_BITPOS (fld[0], f.start);
232 FIELD_BITSIZE (fld[0]) = bitsize;
236 gdb_assert (f.start == -1 && f.end == -1);
237 type *field_gdb_type = make_gdb_type (m_gdbarch, f.type);
238 append_composite_type_field (m_type,
239 xstrdup (f.name.c_str ()),
245 TYPE_LENGTH (m_type) = e->size;
248 void make_gdb_type_union (const tdesc_type_with_fields *e)
250 m_type = arch_composite_type (m_gdbarch, NULL, TYPE_CODE_UNION);
251 TYPE_NAME (m_type) = xstrdup (e->name.c_str ());
253 for (const tdesc_type_field &f : e->fields)
255 type* field_gdb_type = make_gdb_type (m_gdbarch, f.type);
256 append_composite_type_field (m_type, xstrdup (f.name.c_str ()),
259 /* If any of the children of a union are vectors, flag the
260 union as a vector also. This allows e.g. a union of two
261 vector types to show up automatically in "info vector". */
262 if (TYPE_VECTOR (field_gdb_type))
263 TYPE_VECTOR (m_type) = 1;
267 void make_gdb_type_flags (const tdesc_type_with_fields *e)
269 m_type = arch_flags_type (m_gdbarch, e->name.c_str (),
270 e->size * TARGET_CHAR_BIT);
272 for (const tdesc_type_field &f : e->fields)
274 int bitsize = f.end - f.start + 1;
276 gdb_assert (f.type != NULL);
277 type *field_gdb_type = make_gdb_type (m_gdbarch, f.type);
278 append_flags_type_field (m_type, f.start, bitsize,
279 field_gdb_type, f.name.c_str ());
283 void make_gdb_type_enum (const tdesc_type_with_fields *e)
285 m_type = arch_type (m_gdbarch, TYPE_CODE_ENUM, e->size * TARGET_CHAR_BIT,
288 TYPE_UNSIGNED (m_type) = 1;
289 for (const tdesc_type_field &f : e->fields)
292 = append_composite_type_field_raw (m_type,
293 xstrdup (f.name.c_str ()),
296 SET_FIELD_BITPOS (fld[0], f.start);
300 /* The gdbarch used. */
301 struct gdbarch *m_gdbarch;
303 /* The type created. */
307 gdb_type_creator gdb_type (gdbarch);
308 ttype->accept (gdb_type);
309 return gdb_type.get_type ();
312 /* A target description. */
314 struct target_desc : tdesc_element
319 virtual ~target_desc () = default;
321 target_desc (const target_desc &) = delete;
322 void operator= (const target_desc &) = delete;
324 /* The architecture reported by the target, if any. */
325 const struct bfd_arch_info *arch = NULL;
327 /* The osabi reported by the target, if any; GDB_OSABI_UNKNOWN
329 enum gdb_osabi osabi = GDB_OSABI_UNKNOWN;
331 /* The list of compatible architectures reported by the target. */
332 std::vector<const bfd_arch_info *> compatible;
334 /* Any architecture-specific properties specified by the target. */
335 std::vector<property> properties;
337 /* The features associated with this target. */
338 std::vector<tdesc_feature_up> features;
340 /* Used to cache the generated xml version of the target description. */
341 mutable char *xmltarget = nullptr;
343 void accept (tdesc_element_visitor &v) const override
347 for (const tdesc_feature_up &feature : features)
353 bool operator== (const target_desc &other) const
355 if (arch != other.arch)
358 if (osabi != other.osabi)
361 if (features.size () != other.features.size ())
364 for (int ix = 0; ix < features.size (); ix++)
366 const tdesc_feature_up &feature1 = features[ix];
367 const tdesc_feature_up &feature2 = other.features[ix];
369 if (feature1 != feature2 && *feature1 != *feature2)
376 bool operator!= (const target_desc &other) const
378 return !(*this == other);
382 /* Per-architecture data associated with a target description. The
383 target description may be shared by multiple architectures, but
384 this data is private to one gdbarch. */
386 struct tdesc_arch_reg
388 tdesc_arch_reg (tdesc_reg *reg_, struct type *type_)
389 : reg (reg_), type (type_)
392 struct tdesc_reg *reg;
396 struct tdesc_arch_data
398 /* A list of register/type pairs, indexed by GDB's internal register number.
399 During initialization of the gdbarch this list is used to store
400 registers which the architecture assigns a fixed register number.
401 Registers which are NULL in this array, or off the end, are
402 treated as zero-sized and nameless (i.e. placeholders in the
404 std::vector<tdesc_arch_reg> arch_regs;
406 /* Functions which report the register name, type, and reggroups for
408 gdbarch_register_name_ftype *pseudo_register_name = NULL;
409 gdbarch_register_type_ftype *pseudo_register_type = NULL;
410 gdbarch_register_reggroup_p_ftype *pseudo_register_reggroup_p = NULL;
413 /* Info about an inferior's target description. There's one of these
414 for each inferior. */
416 struct target_desc_info
418 /* A flag indicating that a description has already been fetched
419 from the target, so it should not be queried again. */
423 /* The description fetched from the target, or NULL if the target
424 did not supply any description. Only valid when
425 target_desc_fetched is set. Only the description initialization
426 code should access this; normally, the description should be
427 accessed through the gdbarch object. */
429 const struct target_desc *tdesc;
431 /* The filename to read a target description from, as set by "set
432 tdesc filename ..." */
437 /* Get the inferior INF's target description info, allocating one on
438 the stop if necessary. */
440 static struct target_desc_info *
441 get_tdesc_info (struct inferior *inf)
443 if (inf->tdesc_info == NULL)
444 inf->tdesc_info = XCNEW (struct target_desc_info);
445 return inf->tdesc_info;
448 /* A handle for architecture-specific data associated with the
449 target description (see struct tdesc_arch_data). */
451 static struct gdbarch_data *tdesc_data;
453 /* See target-descriptions.h. */
456 target_desc_info_from_user_p (struct target_desc_info *info)
458 return info != NULL && info->filename != NULL;
461 /* See target-descriptions.h. */
464 copy_inferior_target_desc_info (struct inferior *destinf, struct inferior *srcinf)
466 struct target_desc_info *src = get_tdesc_info (srcinf);
467 struct target_desc_info *dest = get_tdesc_info (destinf);
469 dest->fetched = src->fetched;
470 dest->tdesc = src->tdesc;
471 dest->filename = src->filename != NULL ? xstrdup (src->filename) : NULL;
474 /* See target-descriptions.h. */
477 target_desc_info_free (struct target_desc_info *tdesc_info)
479 if (tdesc_info != NULL)
481 xfree (tdesc_info->filename);
486 /* Convenience helper macros. */
488 #define target_desc_fetched \
489 get_tdesc_info (current_inferior ())->fetched
490 #define current_target_desc \
491 get_tdesc_info (current_inferior ())->tdesc
492 #define target_description_filename \
493 get_tdesc_info (current_inferior ())->filename
495 /* The string manipulated by the "set tdesc filename ..." command. */
497 static char *tdesc_filename_cmd_string;
499 /* Fetch the current target's description, and switch the current
500 architecture to one which incorporates that description. */
503 target_find_description (void)
505 /* If we've already fetched a description from the target, don't do
506 it again. This allows a target to fetch the description early,
507 during its to_open or to_create_inferior, if it needs extra
508 information about the target to initialize. */
509 if (target_desc_fetched)
512 /* The current architecture should not have any target description
513 specified. It should have been cleared, e.g. when we
514 disconnected from the previous target. */
515 gdb_assert (gdbarch_target_desc (target_gdbarch ()) == NULL);
517 /* First try to fetch an XML description from the user-specified
519 current_target_desc = NULL;
520 if (target_description_filename != NULL
521 && *target_description_filename != '\0')
523 = file_read_description_xml (target_description_filename);
525 /* Next try to read the description from the current target using
527 if (current_target_desc == NULL)
528 current_target_desc = target_read_description_xml (current_top_target ());
530 /* If that failed try a target-specific hook. */
531 if (current_target_desc == NULL)
532 current_target_desc = target_read_description (current_top_target ());
534 /* If a non-NULL description was returned, then update the current
536 if (current_target_desc)
538 struct gdbarch_info info;
540 gdbarch_info_init (&info);
541 info.target_desc = current_target_desc;
542 if (!gdbarch_update_p (info))
543 warning (_("Architecture rejected target-supplied description"));
546 struct tdesc_arch_data *data;
548 data = ((struct tdesc_arch_data *)
549 gdbarch_data (target_gdbarch (), tdesc_data));
550 if (tdesc_has_registers (current_target_desc)
551 && data->arch_regs.empty ())
552 warning (_("Target-supplied registers are not supported "
553 "by the current architecture"));
557 /* Now that we know this description is usable, record that we
559 target_desc_fetched = 1;
562 /* Discard any description fetched from the current target, and switch
563 the current architecture to one with no target description. */
566 target_clear_description (void)
568 struct gdbarch_info info;
570 if (!target_desc_fetched)
573 target_desc_fetched = 0;
574 current_target_desc = NULL;
576 gdbarch_info_init (&info);
577 if (!gdbarch_update_p (info))
578 internal_error (__FILE__, __LINE__,
579 _("Could not remove target-supplied description"));
582 /* Return the global current target description. This should only be
583 used by gdbarch initialization code; most access should be through
584 an existing gdbarch. */
586 const struct target_desc *
587 target_current_description (void)
589 if (target_desc_fetched)
590 return current_target_desc;
595 /* Return non-zero if this target description is compatible
596 with the given BFD architecture. */
599 tdesc_compatible_p (const struct target_desc *target_desc,
600 const struct bfd_arch_info *arch)
602 for (const bfd_arch_info *compat : target_desc->compatible)
605 || arch->compatible (arch, compat)
606 || compat->compatible (compat, arch))
614 /* Direct accessors for target descriptions. */
616 /* Return the string value of a property named KEY, or NULL if the
617 property was not specified. */
620 tdesc_property (const struct target_desc *target_desc, const char *key)
622 for (const property &prop : target_desc->properties)
624 return prop.value.c_str ();
629 /* Return the BFD architecture associated with this target
630 description, or NULL if no architecture was specified. */
632 const struct bfd_arch_info *
633 tdesc_architecture (const struct target_desc *target_desc)
635 return target_desc->arch;
638 /* See gdbsupport/tdesc.h. */
641 tdesc_architecture_name (const struct target_desc *target_desc)
643 return target_desc->arch->printable_name;
646 /* Return the OSABI associated with this target description, or
647 GDB_OSABI_UNKNOWN if no osabi was specified. */
650 tdesc_osabi (const struct target_desc *target_desc)
652 return target_desc->osabi;
655 /* See gdbsupport/tdesc.h. */
658 tdesc_osabi_name (const struct target_desc *target_desc)
660 enum gdb_osabi osabi = tdesc_osabi (target_desc);
661 if (osabi > GDB_OSABI_UNKNOWN && osabi < GDB_OSABI_INVALID)
662 return gdbarch_osabi_name (osabi);
666 /* Return 1 if this target description includes any registers. */
669 tdesc_has_registers (const struct target_desc *target_desc)
671 if (target_desc == NULL)
674 for (const tdesc_feature_up &feature : target_desc->features)
675 if (!feature->registers.empty ())
681 /* Return the feature with the given name, if present, or NULL if
682 the named feature is not found. */
684 const struct tdesc_feature *
685 tdesc_find_feature (const struct target_desc *target_desc,
688 for (const tdesc_feature_up &feature : target_desc->features)
689 if (feature->name == name)
690 return feature.get ();
695 /* Return the name of FEATURE. */
698 tdesc_feature_name (const struct tdesc_feature *feature)
700 return feature->name.c_str ();
703 /* Lookup type associated with ID. */
706 tdesc_find_type (struct gdbarch *gdbarch, const char *id)
708 tdesc_arch_data *data
709 = (struct tdesc_arch_data *) gdbarch_data (gdbarch, tdesc_data);
711 for (const tdesc_arch_reg ® : data->arch_regs)
714 && reg.reg->tdesc_type
716 && reg.reg->tdesc_type->name == id)
723 /* Support for registers from target descriptions. */
725 /* Construct the per-gdbarch data. */
728 tdesc_data_init (struct obstack *obstack)
730 return obstack_new<tdesc_arch_data> (obstack);
733 /* Similar, but for the temporary copy used during architecture
736 struct tdesc_arch_data *
737 tdesc_data_alloc (void)
739 return new tdesc_arch_data ();
742 /* Free something allocated by tdesc_data_alloc, if it is not going
743 to be used (for instance if it was unsuitable for the
747 tdesc_data_cleanup (void *data_untyped)
749 struct tdesc_arch_data *data = (struct tdesc_arch_data *) data_untyped;
754 /* Search FEATURE for a register named NAME. */
756 static struct tdesc_reg *
757 tdesc_find_register_early (const struct tdesc_feature *feature,
760 for (const tdesc_reg_up ® : feature->registers)
761 if (strcasecmp (reg->name.c_str (), name) == 0)
767 /* Search FEATURE for a register named NAME. Assign REGNO to it. */
770 tdesc_numbered_register (const struct tdesc_feature *feature,
771 struct tdesc_arch_data *data,
772 int regno, const char *name)
774 struct tdesc_reg *reg = tdesc_find_register_early (feature, name);
779 /* Make sure the vector includes a REGNO'th element. */
780 while (regno >= data->arch_regs.size ())
781 data->arch_regs.emplace_back (nullptr, nullptr);
783 data->arch_regs[regno] = tdesc_arch_reg (reg, NULL);
788 /* Search FEATURE for a register named NAME, but do not assign a fixed
789 register number to it. */
792 tdesc_unnumbered_register (const struct tdesc_feature *feature,
795 struct tdesc_reg *reg = tdesc_find_register_early (feature, name);
803 /* Search FEATURE for a register whose name is in NAMES and assign
807 tdesc_numbered_register_choices (const struct tdesc_feature *feature,
808 struct tdesc_arch_data *data,
809 int regno, const char *const names[])
813 for (i = 0; names[i] != NULL; i++)
814 if (tdesc_numbered_register (feature, data, regno, names[i]))
820 /* Search FEATURE for a register named NAME, and return its size in
821 bits. The register must exist. */
824 tdesc_register_bitsize (const struct tdesc_feature *feature, const char *name)
826 struct tdesc_reg *reg = tdesc_find_register_early (feature, name);
828 gdb_assert (reg != NULL);
832 /* Look up a register by its GDB internal register number. */
834 static struct tdesc_arch_reg *
835 tdesc_find_arch_register (struct gdbarch *gdbarch, int regno)
837 struct tdesc_arch_data *data;
839 data = (struct tdesc_arch_data *) gdbarch_data (gdbarch, tdesc_data);
840 if (regno < data->arch_regs.size ())
841 return &data->arch_regs[regno];
846 static struct tdesc_reg *
847 tdesc_find_register (struct gdbarch *gdbarch, int regno)
849 struct tdesc_arch_reg *reg = tdesc_find_arch_register (gdbarch, regno);
851 return reg? reg->reg : NULL;
854 /* Return the name of register REGNO, from the target description or
855 from an architecture-provided pseudo_register_name method. */
858 tdesc_register_name (struct gdbarch *gdbarch, int regno)
860 struct tdesc_reg *reg = tdesc_find_register (gdbarch, regno);
861 int num_regs = gdbarch_num_regs (gdbarch);
864 return reg->name.c_str ();
866 if (regno >= num_regs && regno < gdbarch_num_cooked_regs (gdbarch))
868 struct tdesc_arch_data *data
869 = (struct tdesc_arch_data *) gdbarch_data (gdbarch, tdesc_data);
871 gdb_assert (data->pseudo_register_name != NULL);
872 return data->pseudo_register_name (gdbarch, regno);
879 tdesc_register_type (struct gdbarch *gdbarch, int regno)
881 struct tdesc_arch_reg *arch_reg = tdesc_find_arch_register (gdbarch, regno);
882 struct tdesc_reg *reg = arch_reg? arch_reg->reg : NULL;
883 int num_regs = gdbarch_num_regs (gdbarch);
884 int num_pseudo_regs = gdbarch_num_pseudo_regs (gdbarch);
886 if (reg == NULL && regno >= num_regs && regno < num_regs + num_pseudo_regs)
888 struct tdesc_arch_data *data
889 = (struct tdesc_arch_data *) gdbarch_data (gdbarch, tdesc_data);
891 gdb_assert (data->pseudo_register_type != NULL);
892 return data->pseudo_register_type (gdbarch, regno);
896 /* Return "int0_t", since "void" has a misleading size of one. */
897 return builtin_type (gdbarch)->builtin_int0;
899 if (arch_reg->type == NULL)
901 /* First check for a predefined or target defined type. */
903 arch_reg->type = make_gdb_type (gdbarch, reg->tdesc_type);
905 /* Next try size-sensitive type shortcuts. */
906 else if (reg->type == "float")
908 if (reg->bitsize == gdbarch_float_bit (gdbarch))
909 arch_reg->type = builtin_type (gdbarch)->builtin_float;
910 else if (reg->bitsize == gdbarch_double_bit (gdbarch))
911 arch_reg->type = builtin_type (gdbarch)->builtin_double;
912 else if (reg->bitsize == gdbarch_long_double_bit (gdbarch))
913 arch_reg->type = builtin_type (gdbarch)->builtin_long_double;
916 warning (_("Register \"%s\" has an unsupported size (%d bits)"),
917 reg->name.c_str (), reg->bitsize);
918 arch_reg->type = builtin_type (gdbarch)->builtin_double;
921 else if (reg->type == "int")
923 if (reg->bitsize == gdbarch_long_bit (gdbarch))
924 arch_reg->type = builtin_type (gdbarch)->builtin_long;
925 else if (reg->bitsize == TARGET_CHAR_BIT)
926 arch_reg->type = builtin_type (gdbarch)->builtin_char;
927 else if (reg->bitsize == gdbarch_short_bit (gdbarch))
928 arch_reg->type = builtin_type (gdbarch)->builtin_short;
929 else if (reg->bitsize == gdbarch_int_bit (gdbarch))
930 arch_reg->type = builtin_type (gdbarch)->builtin_int;
931 else if (reg->bitsize == gdbarch_long_long_bit (gdbarch))
932 arch_reg->type = builtin_type (gdbarch)->builtin_long_long;
933 else if (reg->bitsize == gdbarch_ptr_bit (gdbarch))
934 /* A bit desperate by this point... */
935 arch_reg->type = builtin_type (gdbarch)->builtin_data_ptr;
938 warning (_("Register \"%s\" has an unsupported size (%d bits)"),
939 reg->name.c_str (), reg->bitsize);
940 arch_reg->type = builtin_type (gdbarch)->builtin_long;
944 if (arch_reg->type == NULL)
945 internal_error (__FILE__, __LINE__,
946 "Register \"%s\" has an unknown type \"%s\"",
947 reg->name.c_str (), reg->type.c_str ());
950 return arch_reg->type;
954 tdesc_remote_register_number (struct gdbarch *gdbarch, int regno)
956 struct tdesc_reg *reg = tdesc_find_register (gdbarch, regno);
959 return reg->target_regnum;
964 /* Check whether REGNUM is a member of REGGROUP. Registers from the
965 target description may be classified as general, float, vector or other
966 register groups registered with reggroup_add(). Unlike a gdbarch
967 register_reggroup_p method, this function will return -1 if it does not
968 know; the caller should handle registers with no specified group.
970 The names of containing features are not used. This might be extended
971 to display registers in some more useful groupings.
973 The save-restore flag is also implemented here. */
976 tdesc_register_in_reggroup_p (struct gdbarch *gdbarch, int regno,
977 struct reggroup *reggroup)
979 struct tdesc_reg *reg = tdesc_find_register (gdbarch, regno);
981 if (reg != NULL && !reg->group.empty ()
982 && (reg->group == reggroup_name (reggroup)))
986 && (reggroup == save_reggroup || reggroup == restore_reggroup))
987 return reg->save_restore;
992 /* Check whether REGNUM is a member of REGGROUP. Registers with no
993 group specified go to the default reggroup function and are handled
997 tdesc_register_reggroup_p (struct gdbarch *gdbarch, int regno,
998 struct reggroup *reggroup)
1000 int num_regs = gdbarch_num_regs (gdbarch);
1001 int num_pseudo_regs = gdbarch_num_pseudo_regs (gdbarch);
1004 if (regno >= num_regs && regno < num_regs + num_pseudo_regs)
1006 struct tdesc_arch_data *data
1007 = (struct tdesc_arch_data *) gdbarch_data (gdbarch, tdesc_data);
1009 if (data->pseudo_register_reggroup_p != NULL)
1010 return data->pseudo_register_reggroup_p (gdbarch, regno, reggroup);
1011 /* Otherwise fall through to the default reggroup_p. */
1014 ret = tdesc_register_in_reggroup_p (gdbarch, regno, reggroup);
1018 return default_register_reggroup_p (gdbarch, regno, reggroup);
1021 /* Record architecture-specific functions to call for pseudo-register
1025 set_tdesc_pseudo_register_name (struct gdbarch *gdbarch,
1026 gdbarch_register_name_ftype *pseudo_name)
1028 struct tdesc_arch_data *data
1029 = (struct tdesc_arch_data *) gdbarch_data (gdbarch, tdesc_data);
1031 data->pseudo_register_name = pseudo_name;
1035 set_tdesc_pseudo_register_type (struct gdbarch *gdbarch,
1036 gdbarch_register_type_ftype *pseudo_type)
1038 struct tdesc_arch_data *data
1039 = (struct tdesc_arch_data *) gdbarch_data (gdbarch, tdesc_data);
1041 data->pseudo_register_type = pseudo_type;
1045 set_tdesc_pseudo_register_reggroup_p
1046 (struct gdbarch *gdbarch,
1047 gdbarch_register_reggroup_p_ftype *pseudo_reggroup_p)
1049 struct tdesc_arch_data *data
1050 = (struct tdesc_arch_data *) gdbarch_data (gdbarch, tdesc_data);
1052 data->pseudo_register_reggroup_p = pseudo_reggroup_p;
1055 /* Update GDBARCH to use the target description for registers. */
1058 tdesc_use_registers (struct gdbarch *gdbarch,
1059 const struct target_desc *target_desc,
1060 struct tdesc_arch_data *early_data)
1062 int num_regs = gdbarch_num_regs (gdbarch);
1063 struct tdesc_arch_data *data;
1066 /* We can't use the description for registers if it doesn't describe
1067 any. This function should only be called after validating
1068 registers, so the caller should know that registers are
1070 gdb_assert (tdesc_has_registers (target_desc));
1072 data = (struct tdesc_arch_data *) gdbarch_data (gdbarch, tdesc_data);
1073 data->arch_regs = early_data->arch_regs;
1076 /* Build up a set of all registers, so that we can assign register
1077 numbers where needed. The hash table expands as necessary, so
1078 the initial size is arbitrary. */
1079 reg_hash = htab_create (37, htab_hash_pointer, htab_eq_pointer, NULL);
1080 for (const tdesc_feature_up &feature : target_desc->features)
1081 for (const tdesc_reg_up ® : feature->registers)
1083 void **slot = htab_find_slot (reg_hash, reg.get (), INSERT);
1086 /* Add reggroup if its new. */
1087 if (!reg->group.empty ())
1088 if (reggroup_find (gdbarch, reg->group.c_str ()) == NULL)
1089 reggroup_add (gdbarch, reggroup_gdbarch_new (gdbarch,
1090 reg->group.c_str (),
1094 /* Remove any registers which were assigned numbers by the
1096 for (const tdesc_arch_reg &arch_reg : data->arch_regs)
1097 if (arch_reg.reg != NULL)
1098 htab_remove_elt (reg_hash, arch_reg.reg);
1100 /* Assign numbers to the remaining registers and add them to the
1101 list of registers. The new numbers are always above gdbarch_num_regs.
1102 Iterate over the features, not the hash table, so that the order
1103 matches that in the target description. */
1105 gdb_assert (data->arch_regs.size () <= num_regs);
1106 while (data->arch_regs.size () < num_regs)
1107 data->arch_regs.emplace_back (nullptr, nullptr);
1109 for (const tdesc_feature_up &feature : target_desc->features)
1110 for (const tdesc_reg_up ® : feature->registers)
1111 if (htab_find (reg_hash, reg.get ()) != NULL)
1113 data->arch_regs.emplace_back (reg.get (), nullptr);
1117 htab_delete (reg_hash);
1119 /* Update the architecture. */
1120 set_gdbarch_num_regs (gdbarch, num_regs);
1121 set_gdbarch_register_name (gdbarch, tdesc_register_name);
1122 set_gdbarch_register_type (gdbarch, tdesc_register_type);
1123 set_gdbarch_remote_register_number (gdbarch,
1124 tdesc_remote_register_number);
1125 set_gdbarch_register_reggroup_p (gdbarch, tdesc_register_reggroup_p);
1128 /* See gdbsupport/tdesc.h. */
1130 struct tdesc_feature *
1131 tdesc_create_feature (struct target_desc *tdesc, const char *name)
1133 struct tdesc_feature *new_feature = new tdesc_feature (name);
1135 tdesc->features.emplace_back (new_feature);
1140 struct target_desc *
1141 allocate_target_description (void)
1143 return new target_desc ();
1147 target_desc_deleter::operator() (struct target_desc *target_desc) const
1153 tdesc_add_compatible (struct target_desc *target_desc,
1154 const struct bfd_arch_info *compatible)
1156 /* If this instance of GDB is compiled without BFD support for the
1157 compatible architecture, simply ignore it -- we would not be able
1158 to handle it anyway. */
1159 if (compatible == NULL)
1162 for (const bfd_arch_info *compat : target_desc->compatible)
1163 if (compat == compatible)
1164 internal_error (__FILE__, __LINE__,
1165 _("Attempted to add duplicate "
1166 "compatible architecture \"%s\""),
1167 compatible->printable_name);
1169 target_desc->compatible.push_back (compatible);
1173 set_tdesc_property (struct target_desc *target_desc,
1174 const char *key, const char *value)
1176 gdb_assert (key != NULL && value != NULL);
1178 if (tdesc_property (target_desc, key) != NULL)
1179 internal_error (__FILE__, __LINE__,
1180 _("Attempted to add duplicate property \"%s\""), key);
1182 target_desc->properties.emplace_back (key, value);
1185 /* See gdbsupport/tdesc.h. */
1188 set_tdesc_architecture (struct target_desc *target_desc,
1191 set_tdesc_architecture (target_desc, bfd_scan_arch (name));
1195 set_tdesc_architecture (struct target_desc *target_desc,
1196 const struct bfd_arch_info *arch)
1198 target_desc->arch = arch;
1201 /* See gdbsupport/tdesc.h. */
1204 set_tdesc_osabi (struct target_desc *target_desc, const char *name)
1206 set_tdesc_osabi (target_desc, osabi_from_tdesc_string (name));
1210 set_tdesc_osabi (struct target_desc *target_desc, enum gdb_osabi osabi)
1212 target_desc->osabi = osabi;
1216 static struct cmd_list_element *tdesc_set_cmdlist, *tdesc_show_cmdlist;
1217 static struct cmd_list_element *tdesc_unset_cmdlist;
1219 /* Helper functions for the CLI commands. */
1222 set_tdesc_cmd (const char *args, int from_tty)
1224 help_list (tdesc_set_cmdlist, "set tdesc ", all_commands, gdb_stdout);
1228 show_tdesc_cmd (const char *args, int from_tty)
1230 cmd_show_list (tdesc_show_cmdlist, from_tty, "");
1234 unset_tdesc_cmd (const char *args, int from_tty)
1236 help_list (tdesc_unset_cmdlist, "unset tdesc ", all_commands, gdb_stdout);
1240 set_tdesc_filename_cmd (const char *args, int from_tty,
1241 struct cmd_list_element *c)
1243 xfree (target_description_filename);
1244 target_description_filename = xstrdup (tdesc_filename_cmd_string);
1246 target_clear_description ();
1247 target_find_description ();
1251 show_tdesc_filename_cmd (struct ui_file *file, int from_tty,
1252 struct cmd_list_element *c,
1255 value = target_description_filename;
1257 if (value != NULL && *value != '\0')
1258 printf_filtered (_("The target description will be read from \"%s\".\n"),
1261 printf_filtered (_("The target description will be "
1262 "read from the target.\n"));
1266 unset_tdesc_filename_cmd (const char *args, int from_tty)
1268 xfree (target_description_filename);
1269 target_description_filename = NULL;
1270 target_clear_description ();
1271 target_find_description ();
1274 /* Print target description in C. */
1276 class print_c_tdesc : public tdesc_element_visitor
1279 print_c_tdesc (std::string &filename_after_features)
1280 : m_filename_after_features (filename_after_features)
1284 const char *filename = lbasename (m_filename_after_features.c_str ());
1286 m_function = (char *) xmalloc (strlen (filename) + 1);
1287 for (inp = filename, outp = m_function; *inp != '\0'; inp++)
1290 else if (*inp == '-')
1296 /* Standard boilerplate. */
1297 printf_unfiltered ("/* THIS FILE IS GENERATED. "
1298 "-*- buffer-read-only: t -*- vi"
1307 void visit_pre (const target_desc *e) override
1309 printf_unfiltered (" Original: %s */\n\n",
1310 lbasename (m_filename_after_features.c_str ()));
1312 printf_unfiltered ("#include \"defs.h\"\n");
1313 printf_unfiltered ("#include \"osabi.h\"\n");
1314 printf_unfiltered ("#include \"target-descriptions.h\"\n");
1315 printf_unfiltered ("\n");
1317 printf_unfiltered ("struct target_desc *tdesc_%s;\n", m_function);
1318 printf_unfiltered ("static void\n");
1319 printf_unfiltered ("initialize_tdesc_%s (void)\n", m_function);
1320 printf_unfiltered ("{\n");
1322 (" struct target_desc *result = allocate_target_description ();\n");
1324 if (tdesc_architecture (e) != NULL)
1327 (" set_tdesc_architecture (result, bfd_scan_arch (\"%s\"));\n",
1328 tdesc_architecture (e)->printable_name);
1329 printf_unfiltered ("\n");
1331 if (tdesc_osabi (e) > GDB_OSABI_UNKNOWN
1332 && tdesc_osabi (e) < GDB_OSABI_INVALID)
1335 (" set_tdesc_osabi (result, osabi_from_tdesc_string (\"%s\"));\n",
1336 gdbarch_osabi_name (tdesc_osabi (e)));
1337 printf_unfiltered ("\n");
1340 for (const bfd_arch_info_type *compatible : e->compatible)
1342 (" tdesc_add_compatible (result, bfd_scan_arch (\"%s\"));\n",
1343 compatible->printable_name);
1345 if (!e->compatible.empty ())
1346 printf_unfiltered ("\n");
1348 for (const property &prop : e->properties)
1349 printf_unfiltered (" set_tdesc_property (result, \"%s\", \"%s\");\n",
1350 prop.key.c_str (), prop.value.c_str ());
1352 printf_unfiltered (" struct tdesc_feature *feature;\n");
1355 void visit_pre (const tdesc_feature *e) override
1357 printf_unfiltered ("\n feature = tdesc_create_feature (result, \"%s\");\n",
1361 void visit_post (const tdesc_feature *e) override
1364 void visit_post (const target_desc *e) override
1366 printf_unfiltered ("\n tdesc_%s = result;\n", m_function);
1367 printf_unfiltered ("}\n");
1370 void visit (const tdesc_type_builtin *type) override
1372 error (_("C output is not supported type \"%s\"."), type->name.c_str ());
1375 void visit (const tdesc_type_vector *type) override
1377 if (!m_printed_element_type)
1379 printf_unfiltered (" tdesc_type *element_type;\n");
1380 m_printed_element_type = true;
1384 (" element_type = tdesc_named_type (feature, \"%s\");\n",
1385 type->element_type->name.c_str ());
1387 (" tdesc_create_vector (feature, \"%s\", element_type, %d);\n",
1388 type->name.c_str (), type->count);
1390 printf_unfiltered ("\n");
1393 void visit (const tdesc_type_with_fields *type) override
1395 if (!m_printed_type_with_fields)
1397 printf_unfiltered (" tdesc_type_with_fields *type_with_fields;\n");
1398 m_printed_type_with_fields = true;
1403 case TDESC_TYPE_STRUCT:
1404 case TDESC_TYPE_FLAGS:
1405 if (type->kind == TDESC_TYPE_STRUCT)
1408 (" type_with_fields = tdesc_create_struct (feature, \"%s\");\n",
1409 type->name.c_str ());
1410 if (type->size != 0)
1412 (" tdesc_set_struct_size (type_with_fields, %d);\n", type->size);
1417 (" type_with_fields = tdesc_create_flags (feature, \"%s\", %d);\n",
1418 type->name.c_str (), type->size);
1420 for (const tdesc_type_field &f : type->fields)
1422 const char *type_name;
1424 gdb_assert (f.type != NULL);
1425 type_name = f.type->name.c_str ();
1427 /* To minimize changes to generated files, don't emit type
1428 info for fields that have defaulted types. */
1431 gdb_assert (f.end != -1);
1432 if (f.type->kind == TDESC_TYPE_BOOL)
1434 gdb_assert (f.start == f.end);
1436 (" tdesc_add_flag (type_with_fields, %d, \"%s\");\n",
1437 f.start, f.name.c_str ());
1439 else if ((type->size == 4 && f.type->kind == TDESC_TYPE_UINT32)
1441 && f.type->kind == TDESC_TYPE_UINT64))
1444 (" tdesc_add_bitfield (type_with_fields, \"%s\", %d, %d);\n",
1445 f.name.c_str (), f.start, f.end);
1449 printf_field_type_assignment
1450 ("tdesc_named_type (feature, \"%s\");\n",
1453 (" tdesc_add_typed_bitfield (type_with_fields, \"%s\","
1454 " %d, %d, field_type);\n",
1455 f.name.c_str (), f.start, f.end);
1458 else /* Not a bitfield. */
1460 gdb_assert (f.end == -1);
1461 gdb_assert (type->kind == TDESC_TYPE_STRUCT);
1462 printf_field_type_assignment
1463 ("tdesc_named_type (feature, \"%s\");\n", type_name);
1465 (" tdesc_add_field (type_with_fields, \"%s\", field_type);\n",
1470 case TDESC_TYPE_UNION:
1472 (" type_with_fields = tdesc_create_union (feature, \"%s\");\n",
1473 type->name.c_str ());
1474 for (const tdesc_type_field &f : type->fields)
1476 printf_field_type_assignment
1477 ("tdesc_named_type (feature, \"%s\");\n", f.type->name.c_str ());
1479 (" tdesc_add_field (type_with_fields, \"%s\", field_type);\n",
1483 case TDESC_TYPE_ENUM:
1485 (" type_with_fields = tdesc_create_enum (feature, \"%s\", %d);\n",
1486 type->name.c_str (), type->size);
1487 for (const tdesc_type_field &f : type->fields)
1489 (" tdesc_add_enum_value (type_with_fields, %d, \"%s\");\n",
1490 f.start, f.name.c_str ());
1493 error (_("C output is not supported type \"%s\"."), type->name.c_str ());
1496 printf_unfiltered ("\n");
1499 void visit (const tdesc_reg *reg) override
1501 printf_unfiltered (" tdesc_create_reg (feature, \"%s\", %ld, %d, ",
1502 reg->name.c_str (), reg->target_regnum,
1504 if (!reg->group.empty ())
1505 printf_unfiltered ("\"%s\", ", reg->group.c_str ());
1507 printf_unfiltered ("NULL, ");
1508 printf_unfiltered ("%d, \"%s\");\n", reg->bitsize, reg->type.c_str ());
1512 std::string m_filename_after_features;
1516 /* Print an assignment to the field_type variable. Print the declaration
1517 of field_type if that has not been done yet. */
1518 ATTRIBUTE_PRINTF (2, 3)
1519 void printf_field_type_assignment (const char *fmt, ...)
1521 if (!m_printed_field_type)
1523 printf_unfiltered (" tdesc_type *field_type;\n");
1524 m_printed_field_type = true;
1527 printf_unfiltered (" field_type = ");
1530 va_start (args, fmt);
1531 vprintf_unfiltered (fmt, args);
1537 /* Did we print "struct tdesc_type *element_type;" yet? */
1538 bool m_printed_element_type = false;
1540 /* Did we print "struct tdesc_type_with_fields *element_type;" yet? */
1541 bool m_printed_type_with_fields = false;
1543 /* Did we print "struct tdesc_type *field_type;" yet? */
1544 bool m_printed_field_type = false;
1547 /* Print target description feature in C. */
1549 class print_c_feature : public print_c_tdesc
1552 print_c_feature (std::string &file)
1553 : print_c_tdesc (file)
1556 auto const pos = m_filename_after_features.find_last_of ('.');
1558 m_filename_after_features = m_filename_after_features.substr (0, pos);
1561 void visit_pre (const target_desc *e) override
1563 printf_unfiltered (" Original: %s */\n\n",
1564 lbasename (m_filename_after_features.c_str ()));
1566 printf_unfiltered ("#include \"gdbsupport/tdesc.h\"\n");
1567 printf_unfiltered ("\n");
1570 void visit_post (const target_desc *e) override
1573 void visit_pre (const tdesc_feature *e) override
1575 std::string name (m_filename_after_features);
1577 auto pos = name.find_first_of ('.');
1579 name = name.substr (0, pos);
1580 std::replace (name.begin (), name.end (), '/', '_');
1581 std::replace (name.begin (), name.end (), '-', '_');
1583 printf_unfiltered ("static int\n");
1584 printf_unfiltered ("create_feature_%s ", name.c_str ());
1585 printf_unfiltered ("(struct target_desc *result, long regnum)\n");
1587 printf_unfiltered ("{\n");
1588 printf_unfiltered (" struct tdesc_feature *feature;\n");
1591 ("\n feature = tdesc_create_feature (result, \"%s\");\n",
1595 void visit_post (const tdesc_feature *e) override
1597 printf_unfiltered (" return regnum;\n");
1598 printf_unfiltered ("}\n");
1601 void visit (const tdesc_reg *reg) override
1603 /* Most "reg" in XML target descriptions don't have "regnum"
1604 attribute, so the register number is allocated sequentially.
1605 In case that reg has "regnum" attribute, register number
1606 should be set by that explicitly. */
1608 if (reg->target_regnum < m_next_regnum)
1610 /* The integrity check, it can catch some errors on register
1611 number collision, like this,
1613 <reg name="x0" bitsize="32"/>
1614 <reg name="x1" bitsize="32"/>
1615 <reg name="x2" bitsize="32"/>
1616 <reg name="x3" bitsize="32"/>
1617 <reg name="ps" bitsize="32" regnum="3"/>
1619 but it also has false negatives. The target description
1622 <reg name="x1" bitsize="32" regnum="1"/>
1623 <reg name="x3" bitsize="32" regnum="3"/>
1624 <reg name="x2" bitsize="32" regnum="2"/>
1625 <reg name="x4" bitsize="32" regnum="4"/>
1627 but it is not a good practice, so still error on this,
1628 and also print the message so that it can be saved in the
1629 generated c file. */
1631 printf_unfiltered ("ERROR: \"regnum\" attribute %ld ",
1632 reg->target_regnum);
1633 printf_unfiltered ("is not the largest number (%d).\n",
1635 error (_("\"regnum\" attribute %ld is not the largest number (%d)."),
1636 reg->target_regnum, m_next_regnum);
1639 if (reg->target_regnum > m_next_regnum)
1641 printf_unfiltered (" regnum = %ld;\n", reg->target_regnum);
1642 m_next_regnum = reg->target_regnum;
1645 printf_unfiltered (" tdesc_create_reg (feature, \"%s\", regnum++, %d, ",
1646 reg->name.c_str (), reg->save_restore);
1647 if (!reg->group.empty ())
1648 printf_unfiltered ("\"%s\", ", reg->group.c_str ());
1650 printf_unfiltered ("NULL, ");
1651 printf_unfiltered ("%d, \"%s\");\n", reg->bitsize, reg->type.c_str ());
1657 /* The register number to use for the next register we see. */
1658 int m_next_regnum = 0;
1661 /* See gdbsupport/tdesc.h. */
1664 tdesc_get_features_xml (const target_desc *tdesc)
1666 if (tdesc->xmltarget == nullptr)
1668 std::string buffer ("@");
1669 print_xml_feature v (&buffer);
1671 tdesc->xmltarget = xstrdup (buffer.c_str ());
1673 return tdesc->xmltarget;
1677 maint_print_c_tdesc_cmd (const char *args, int from_tty)
1679 const struct target_desc *tdesc;
1680 const char *filename;
1684 /* Use the global target-supplied description, not the current
1685 architecture's. This lets a GDB for one architecture generate C
1686 for another architecture's description, even though the gdbarch
1687 initialization code will reject the new description. */
1688 tdesc = current_target_desc;
1689 filename = target_description_filename;
1693 /* Use the target description from the XML file. */
1695 tdesc = file_read_description_xml (filename);
1699 error (_("There is no target description to print."));
1701 if (filename == NULL)
1702 error (_("The current target description did not come from an XML file."));
1704 std::string filename_after_features (filename);
1705 auto loc = filename_after_features.rfind ("/features/");
1707 if (loc != std::string::npos)
1708 filename_after_features = filename_after_features.substr (loc + 10);
1710 /* Print c files for target features instead of target descriptions,
1711 because c files got from target features are more flexible than the
1713 if (startswith (filename_after_features.c_str (), "i386/32bit-")
1714 || startswith (filename_after_features.c_str (), "i386/64bit-")
1715 || startswith (filename_after_features.c_str (), "i386/x32-core.xml")
1716 || startswith (filename_after_features.c_str (), "riscv/")
1717 || startswith (filename_after_features.c_str (), "tic6x-")
1718 || startswith (filename_after_features.c_str (), "aarch64")
1719 || startswith (filename_after_features.c_str (), "arm/"))
1721 print_c_feature v (filename_after_features);
1727 print_c_tdesc v (filename_after_features);
1733 namespace selftests {
1735 /* A reference target description, used for testing (see record_xml_tdesc). */
1737 struct xml_test_tdesc
1739 xml_test_tdesc (const char *name, std::unique_ptr<const target_desc> &&tdesc)
1740 : name (name), tdesc (std::move (tdesc))
1744 std::unique_ptr<const target_desc> tdesc;
1747 static std::vector<xml_test_tdesc> xml_tdesc;
1751 /* See target-descritpions.h. */
1754 record_xml_tdesc (const char *xml_file, const struct target_desc *tdesc)
1756 xml_tdesc.emplace_back (xml_file, std::unique_ptr<const target_desc> (tdesc));
1762 /* Test the convesion process of a target description to/from xml: Take a target
1763 description TDESC, convert to xml, back to a description, and confirm the new
1764 tdesc is identical to the original. */
1766 maintenance_check_tdesc_xml_convert (const target_desc *tdesc, const char *name)
1768 const char *xml = tdesc_get_features_xml (tdesc);
1770 if (xml == nullptr || *xml != '@')
1772 printf_filtered (_("Could not convert description for %s to xml.\n"),
1777 const target_desc *tdesc_trans = string_read_description_xml (xml + 1);
1779 if (tdesc_trans == nullptr)
1781 printf_filtered (_("Could not convert description for %s from xml.\n"),
1785 else if (*tdesc != *tdesc_trans)
1787 printf_filtered (_("Converted description for %s does not match.\n"),
1795 /* Check that the target descriptions created dynamically by
1796 architecture-specific code equal the descriptions created from XML files
1797 found in the specified directory DIR. */
1800 maintenance_check_xml_descriptions (const char *dir, int from_tty)
1803 error (_("Missing dir name"));
1805 gdb::unique_xmalloc_ptr<char> dir1 (tilde_expand (dir));
1806 std::string feature_dir (dir1.get ());
1807 unsigned int failed = 0;
1809 for (auto const &e : selftests::xml_tdesc)
1811 std::string tdesc_xml = (feature_dir + SLASH_STRING + e.name);
1812 const target_desc *tdesc
1813 = file_read_description_xml (tdesc_xml.data ());
1815 if (tdesc == NULL || *tdesc != *e.tdesc)
1817 printf_filtered ( _("Descriptions for %s do not match.\n"), e.name);
1820 else if (!maintenance_check_tdesc_xml_convert (tdesc, e.name)
1821 || !maintenance_check_tdesc_xml_convert (e.tdesc.get (), e.name))
1824 printf_filtered (_("Tested %lu XML files, %d failed\n"),
1825 (long) selftests::xml_tdesc.size (), failed);
1829 _initialize_target_descriptions (void)
1831 tdesc_data = gdbarch_data_register_pre_init (tdesc_data_init);
1833 add_prefix_cmd ("tdesc", class_maintenance, set_tdesc_cmd, _("\
1834 Set target description specific variables."),
1835 &tdesc_set_cmdlist, "set tdesc ",
1836 0 /* allow-unknown */, &setlist);
1837 add_prefix_cmd ("tdesc", class_maintenance, show_tdesc_cmd, _("\
1838 Show target description specific variables."),
1839 &tdesc_show_cmdlist, "show tdesc ",
1840 0 /* allow-unknown */, &showlist);
1841 add_prefix_cmd ("tdesc", class_maintenance, unset_tdesc_cmd, _("\
1842 Unset target description specific variables."),
1843 &tdesc_unset_cmdlist, "unset tdesc ",
1844 0 /* allow-unknown */, &unsetlist);
1846 add_setshow_filename_cmd ("filename", class_obscure,
1847 &tdesc_filename_cmd_string,
1849 Set the file to read for an XML target description."), _("\
1850 Show the file to read for an XML target description."), _("\
1851 When set, GDB will read the target description from a local\n\
1852 file instead of querying the remote target."),
1853 set_tdesc_filename_cmd,
1854 show_tdesc_filename_cmd,
1855 &tdesc_set_cmdlist, &tdesc_show_cmdlist);
1857 add_cmd ("filename", class_obscure, unset_tdesc_filename_cmd, _("\
1858 Unset the file to read for an XML target description.\n\
1859 When unset, GDB will read the description from the target."),
1860 &tdesc_unset_cmdlist);
1862 add_cmd ("c-tdesc", class_maintenance, maint_print_c_tdesc_cmd, _("\
1863 Print the current target description as a C source file."),
1864 &maintenanceprintlist);
1866 cmd_list_element *cmd;
1868 cmd = add_cmd ("xml-descriptions", class_maintenance,
1869 maintenance_check_xml_descriptions, _("\
1870 Check equality of GDB target descriptions and XML created descriptions.\n\
1871 Check the target descriptions created in GDB equal the descriptions\n\
1872 created from XML files in the directory.\n\
1873 The parameter is the directory name."),
1874 &maintenancechecklist);
1875 set_cmd_completer (cmd, filename_completer);