Home | History | Annotate | Line # | Download | only in gdb
target-descriptions.c revision 1.1.1.8
      1 /* Target description support for GDB.
      2 
      3    Copyright (C) 2006-2023 Free Software Foundation, Inc.
      4 
      5    Contributed by CodeSourcery.
      6 
      7    This file is part of GDB.
      8 
      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.
     13 
     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.
     18 
     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/>.  */
     21 
     22 #include "defs.h"
     23 #include "arch-utils.h"
     24 #include "gdbcmd.h"
     25 #include "gdbtypes.h"
     26 #include "reggroups.h"
     27 #include "target.h"
     28 #include "target-descriptions.h"
     29 #include "xml-support.h"
     30 #include "xml-tdesc.h"
     31 #include "osabi.h"
     32 
     33 #include "gdbsupport/gdb_obstack.h"
     34 #include "hashtab.h"
     35 #include "inferior.h"
     36 #include <algorithm>
     37 #include "completer.h"
     38 #include "readline/tilde.h" /* tilde_expand */
     39 
     40 /* Types.  */
     41 
     42 struct property
     43 {
     44   property (const std::string &key_, const std::string &value_)
     45   : key (key_), value (value_)
     46   {}
     47 
     48   std::string key;
     49   std::string value;
     50 };
     51 
     52 /* Convert a tdesc_type to a gdb type.  */
     53 
     54 static type *
     55 make_gdb_type (struct gdbarch *gdbarch, struct tdesc_type *ttype)
     56 {
     57   class gdb_type_creator : public tdesc_element_visitor
     58   {
     59   public:
     60     gdb_type_creator (struct gdbarch *gdbarch)
     61       : m_gdbarch (gdbarch)
     62     {}
     63 
     64     type *get_type ()
     65     {
     66       return m_type;
     67     }
     68 
     69     void visit (const tdesc_type_builtin *e) override
     70     {
     71       switch (e->kind)
     72 	{
     73 	  /* Predefined types.  */
     74 	case TDESC_TYPE_BOOL:
     75 	  m_type = builtin_type (m_gdbarch)->builtin_bool;
     76 	  return;
     77 	case TDESC_TYPE_INT8:
     78 	  m_type = builtin_type (m_gdbarch)->builtin_int8;
     79 	  return;
     80 	case TDESC_TYPE_INT16:
     81 	  m_type = builtin_type (m_gdbarch)->builtin_int16;
     82 	  return;
     83 	case TDESC_TYPE_INT32:
     84 	  m_type = builtin_type (m_gdbarch)->builtin_int32;
     85 	  return;
     86 	case TDESC_TYPE_INT64:
     87 	  m_type = builtin_type (m_gdbarch)->builtin_int64;
     88 	  return;
     89 	case TDESC_TYPE_INT128:
     90 	  m_type = builtin_type (m_gdbarch)->builtin_int128;
     91 	  return;
     92 	case TDESC_TYPE_UINT8:
     93 	  m_type = builtin_type (m_gdbarch)->builtin_uint8;
     94 	  return;
     95 	case TDESC_TYPE_UINT16:
     96 	  m_type = builtin_type (m_gdbarch)->builtin_uint16;
     97 	  return;
     98 	case TDESC_TYPE_UINT32:
     99 	  m_type = builtin_type (m_gdbarch)->builtin_uint32;
    100 	  return;
    101 	case TDESC_TYPE_UINT64:
    102 	  m_type = builtin_type (m_gdbarch)->builtin_uint64;
    103 	  return;
    104 	case TDESC_TYPE_UINT128:
    105 	  m_type = builtin_type (m_gdbarch)->builtin_uint128;
    106 	  return;
    107 	case TDESC_TYPE_CODE_PTR:
    108 	  m_type = builtin_type (m_gdbarch)->builtin_func_ptr;
    109 	  return;
    110 	case TDESC_TYPE_DATA_PTR:
    111 	  m_type = builtin_type (m_gdbarch)->builtin_data_ptr;
    112 	  return;
    113 	}
    114 
    115       m_type = tdesc_find_type (m_gdbarch, e->name.c_str ());
    116       if (m_type != NULL)
    117 	return;
    118 
    119       switch (e->kind)
    120 	{
    121 	case TDESC_TYPE_IEEE_HALF:
    122 	  m_type = arch_float_type (m_gdbarch, -1, "builtin_type_ieee_half",
    123 				    floatformats_ieee_half);
    124 	  return;
    125 
    126 	case TDESC_TYPE_IEEE_SINGLE:
    127 	  m_type = arch_float_type (m_gdbarch, -1, "builtin_type_ieee_single",
    128 				    floatformats_ieee_single);
    129 	  return;
    130 
    131 	case TDESC_TYPE_IEEE_DOUBLE:
    132 	  m_type = arch_float_type (m_gdbarch, -1, "builtin_type_ieee_double",
    133 				    floatformats_ieee_double);
    134 	  return;
    135 	case TDESC_TYPE_ARM_FPA_EXT:
    136 	  m_type = arch_float_type (m_gdbarch, -1, "builtin_type_arm_ext",
    137 				    floatformats_arm_ext);
    138 	  return;
    139 
    140 	case TDESC_TYPE_I387_EXT:
    141 	  m_type = arch_float_type (m_gdbarch, -1, "builtin_type_i387_ext",
    142 				    floatformats_i387_ext);
    143 	  return;
    144 
    145 	case TDESC_TYPE_BFLOAT16:
    146 	  m_type = arch_float_type (m_gdbarch, -1, "builtin_type_bfloat16",
    147 				    floatformats_bfloat16);
    148 	  return;
    149 	}
    150 
    151       internal_error ("Type \"%s\" has an unknown kind %d",
    152 		      e->name.c_str (), e->kind);
    153     }
    154 
    155     void visit (const tdesc_type_vector *e) override
    156     {
    157       m_type = tdesc_find_type (m_gdbarch, e->name.c_str ());
    158       if (m_type != NULL)
    159 	return;
    160 
    161       type *element_gdb_type = make_gdb_type (m_gdbarch, e->element_type);
    162       m_type = init_vector_type (element_gdb_type, e->count);
    163       m_type->set_name (xstrdup (e->name.c_str ()));
    164       return;
    165     }
    166 
    167     void visit (const tdesc_type_with_fields *e) override
    168     {
    169       m_type = tdesc_find_type (m_gdbarch, e->name.c_str ());
    170       if (m_type != NULL)
    171 	return;
    172 
    173       switch (e->kind)
    174 	{
    175 	case TDESC_TYPE_STRUCT:
    176 	  make_gdb_type_struct (e);
    177 	  return;
    178 	case TDESC_TYPE_UNION:
    179 	  make_gdb_type_union (e);
    180 	  return;
    181 	case TDESC_TYPE_FLAGS:
    182 	  make_gdb_type_flags (e);
    183 	  return;
    184 	case TDESC_TYPE_ENUM:
    185 	  make_gdb_type_enum (e);
    186 	  return;
    187 	}
    188 
    189       internal_error ("Type \"%s\" has an unknown kind %d",
    190 		      e->name.c_str (), e->kind);
    191     }
    192 
    193   private:
    194 
    195     void make_gdb_type_struct (const tdesc_type_with_fields *e)
    196     {
    197       m_type = arch_composite_type (m_gdbarch, NULL, TYPE_CODE_STRUCT);
    198       m_type->set_name (xstrdup (e->name.c_str ()));
    199 
    200       for (const tdesc_type_field &f : e->fields)
    201 	{
    202 	  if (f.start != -1 && f.end != -1)
    203 	    {
    204 	      /* Bitfield.  */
    205 	      struct field *fld;
    206 	      struct type *field_gdb_type;
    207 	      int bitsize, total_size;
    208 
    209 	      /* This invariant should be preserved while creating types.  */
    210 	      gdb_assert (e->size != 0);
    211 	      if (f.type != NULL)
    212 		field_gdb_type = make_gdb_type (m_gdbarch, f.type);
    213 	      else if (e->size > 4)
    214 		field_gdb_type = builtin_type (m_gdbarch)->builtin_uint64;
    215 	      else
    216 		field_gdb_type = builtin_type (m_gdbarch)->builtin_uint32;
    217 
    218 	      fld = append_composite_type_field_raw
    219 		      (m_type, xstrdup (f.name.c_str ()), field_gdb_type);
    220 
    221 	      /* For little-endian, BITPOS counts from the LSB of
    222 		 the structure and marks the LSB of the field.  For
    223 		 big-endian, BITPOS counts from the MSB of the
    224 		 structure and marks the MSB of the field.  Either
    225 		 way, it is the number of bits to the "left" of the
    226 		 field.  To calculate this in big-endian, we need
    227 		 the total size of the structure.  */
    228 	      bitsize = f.end - f.start + 1;
    229 	      total_size = e->size * TARGET_CHAR_BIT;
    230 	      if (gdbarch_byte_order (m_gdbarch) == BFD_ENDIAN_BIG)
    231 		fld->set_loc_bitpos (total_size - f.start - bitsize);
    232 	      else
    233 		fld->set_loc_bitpos (f.start);
    234 	      FIELD_BITSIZE (fld[0]) = bitsize;
    235 	    }
    236 	  else
    237 	    {
    238 	      gdb_assert (f.start == -1 && f.end == -1);
    239 	      type *field_gdb_type = make_gdb_type (m_gdbarch, f.type);
    240 	      append_composite_type_field (m_type,
    241 					   xstrdup (f.name.c_str ()),
    242 					   field_gdb_type);
    243 	    }
    244 	}
    245 
    246       if (e->size != 0)
    247 	m_type->set_length (e->size);
    248     }
    249 
    250     void make_gdb_type_union (const tdesc_type_with_fields *e)
    251     {
    252       m_type = arch_composite_type (m_gdbarch, NULL, TYPE_CODE_UNION);
    253       m_type->set_name (xstrdup (e->name.c_str ()));
    254 
    255       for (const tdesc_type_field &f : e->fields)
    256 	{
    257 	  type* field_gdb_type = make_gdb_type (m_gdbarch, f.type);
    258 	  append_composite_type_field (m_type, xstrdup (f.name.c_str ()),
    259 				       field_gdb_type);
    260 
    261 	  /* If any of the children of a union are vectors, flag the
    262 	     union as a vector also.  This allows e.g. a union of two
    263 	     vector types to show up automatically in "info vector".  */
    264 	  if (field_gdb_type->is_vector ())
    265 	    m_type->set_is_vector (true);
    266 	}
    267     }
    268 
    269     void make_gdb_type_flags (const tdesc_type_with_fields *e)
    270     {
    271       m_type = arch_flags_type (m_gdbarch, e->name.c_str (),
    272 				e->size * TARGET_CHAR_BIT);
    273 
    274       for (const tdesc_type_field &f : e->fields)
    275 	{
    276 	  int bitsize = f.end - f.start + 1;
    277 
    278 	  gdb_assert (f.type != NULL);
    279 	  type *field_gdb_type = make_gdb_type (m_gdbarch, f.type);
    280 	  append_flags_type_field (m_type, f.start, bitsize,
    281 				   field_gdb_type, f.name.c_str ());
    282 	}
    283     }
    284 
    285     void make_gdb_type_enum (const tdesc_type_with_fields *e)
    286     {
    287       m_type = arch_type (m_gdbarch, TYPE_CODE_ENUM, e->size * TARGET_CHAR_BIT,
    288 			  e->name.c_str ());
    289 
    290       m_type->set_is_unsigned (true);
    291 
    292       for (const tdesc_type_field &f : e->fields)
    293 	{
    294 	  struct field *fld
    295 	    = append_composite_type_field_raw (m_type,
    296 					       xstrdup (f.name.c_str ()),
    297 					       NULL);
    298 
    299 	  fld->set_loc_enumval (f.start);
    300 	}
    301     }
    302 
    303     /* The gdbarch used.  */
    304     struct gdbarch *m_gdbarch;
    305 
    306     /* The type created.  */
    307     type *m_type;
    308   };
    309 
    310   gdb_type_creator gdb_type (gdbarch);
    311   ttype->accept (gdb_type);
    312   return gdb_type.get_type ();
    313 }
    314 
    315 /* Wrapper around bfd_arch_info_type.  A class with this name is used in
    316    the API that is shared between gdb and gdbserver code, but gdbserver
    317    doesn't use compatibility information, so its version of this class is
    318    empty.  */
    319 
    320 class tdesc_compatible_info
    321 {
    322 public:
    323   /* Constructor.  */
    324   explicit tdesc_compatible_info (const bfd_arch_info_type *arch)
    325     : m_arch (arch)
    326   { /* Nothing.  */ }
    327 
    328   /* Access the contained pointer.  */
    329   const bfd_arch_info_type *arch () const
    330   { return m_arch; }
    331 
    332 private:
    333   /* Architecture information looked up from the <compatible> entity within
    334      a target description.  */
    335   const bfd_arch_info_type *m_arch;
    336 };
    337 
    338 /* A target description.  */
    339 
    340 struct target_desc : tdesc_element
    341 {
    342   target_desc ()
    343   {}
    344 
    345   virtual ~target_desc () = default;
    346 
    347   target_desc (const target_desc &) = delete;
    348   void operator= (const target_desc &) = delete;
    349 
    350   /* The architecture reported by the target, if any.  */
    351   const struct bfd_arch_info *arch = NULL;
    352 
    353   /* The osabi reported by the target, if any; GDB_OSABI_UNKNOWN
    354      otherwise.  */
    355   enum gdb_osabi osabi = GDB_OSABI_UNKNOWN;
    356 
    357   /* The list of compatible architectures reported by the target.  */
    358   std::vector<tdesc_compatible_info_up> compatible;
    359 
    360   /* Any architecture-specific properties specified by the target.  */
    361   std::vector<property> properties;
    362 
    363   /* The features associated with this target.  */
    364   std::vector<tdesc_feature_up> features;
    365 
    366   /* Used to cache the generated xml version of the target description.  */
    367   mutable char *xmltarget = nullptr;
    368 
    369   void accept (tdesc_element_visitor &v) const override
    370   {
    371     v.visit_pre (this);
    372 
    373     for (const tdesc_feature_up &feature : features)
    374       feature->accept (v);
    375 
    376     v.visit_post (this);
    377   }
    378 
    379   bool operator== (const target_desc &other) const
    380   {
    381     if (arch != other.arch)
    382       return false;
    383 
    384     if (osabi != other.osabi)
    385       return false;
    386 
    387     if (features.size () != other.features.size ())
    388       return false;
    389 
    390     for (int ix = 0; ix < features.size (); ix++)
    391       {
    392 	const tdesc_feature_up &feature1 = features[ix];
    393 	const tdesc_feature_up &feature2 = other.features[ix];
    394 
    395 	if (feature1 != feature2 && *feature1 != *feature2)
    396 	  return false;
    397       }
    398 
    399     return true;
    400   }
    401 
    402   bool operator!= (const target_desc &other) const
    403   {
    404     return !(*this == other);
    405   }
    406 };
    407 
    408 /* Per-architecture data associated with a target description.  The
    409    target description may be shared by multiple architectures, but
    410    this data is private to one gdbarch.  */
    411 
    412 struct tdesc_arch_reg
    413 {
    414   tdesc_arch_reg (tdesc_reg *reg_, struct type *type_)
    415   : reg (reg_), type (type_)
    416   {}
    417 
    418   struct tdesc_reg *reg;
    419   struct type *type;
    420 };
    421 
    422 struct tdesc_arch_data
    423 {
    424   /* A list of register/type pairs, indexed by GDB's internal register number.
    425      During initialization of the gdbarch this list is used to store
    426      registers which the architecture assigns a fixed register number.
    427      Registers which are NULL in this array, or off the end, are
    428      treated as zero-sized and nameless (i.e. placeholders in the
    429      numbering).  */
    430   std::vector<tdesc_arch_reg> arch_regs;
    431 
    432   /* Functions which report the register name, type, and reggroups for
    433      pseudo-registers.  */
    434   gdbarch_register_name_ftype *pseudo_register_name = NULL;
    435   gdbarch_register_type_ftype *pseudo_register_type = NULL;
    436   gdbarch_register_reggroup_p_ftype *pseudo_register_reggroup_p = NULL;
    437 };
    438 
    439 /* Info about an inferior's target description.  There's one of these
    440    for each inferior.  */
    441 
    442 struct target_desc_info
    443 {
    444   /* A flag indicating that a description has already been fetched
    445      from the target, so it should not be queried again.  */
    446 
    447   bool fetched = false;
    448 
    449   /* The description fetched from the target, or NULL if the target
    450      did not supply any description.  Only valid when
    451      FETCHED is set.  Only the description initialization
    452      code should access this; normally, the description should be
    453      accessed through the gdbarch object.  */
    454 
    455   const struct target_desc *tdesc = nullptr;
    456 
    457   /* If not empty, the filename to read a target description from, as set by
    458      "set tdesc filename ...".
    459 
    460      If empty, there is not filename specified by the user.  */
    461 
    462   std::string filename;
    463 };
    464 
    465 /* Get the inferior INF's target description info, allocating one on
    466    the stop if necessary.  */
    467 
    468 static struct target_desc_info *
    469 get_tdesc_info (struct inferior *inf)
    470 {
    471   if (inf->tdesc_info == NULL)
    472     inf->tdesc_info = new target_desc_info;
    473 
    474   return inf->tdesc_info;
    475 }
    476 
    477 /* A handle for architecture-specific data associated with the
    478    target description (see struct tdesc_arch_data).  */
    479 
    480 static const registry<gdbarch>::key<tdesc_arch_data> tdesc_data;
    481 
    482 /* Get or create the tdesc_data.  */
    483 static tdesc_arch_data *
    484 get_arch_data (struct gdbarch *gdbarch)
    485 {
    486   tdesc_arch_data *result = tdesc_data.get (gdbarch);
    487   if (result == nullptr)
    488     result = tdesc_data.emplace (gdbarch);
    489   return result;
    490 }
    491 
    492 /* See target-descriptions.h.  */
    493 
    494 int
    495 target_desc_info_from_user_p (struct target_desc_info *info)
    496 {
    497   return info != nullptr && !info->filename.empty ();
    498 }
    499 
    500 /* See target-descriptions.h.  */
    501 
    502 void
    503 copy_inferior_target_desc_info (struct inferior *destinf, struct inferior *srcinf)
    504 {
    505   struct target_desc_info *src = get_tdesc_info (srcinf);
    506   struct target_desc_info *dest = get_tdesc_info (destinf);
    507 
    508   *dest = *src;
    509 }
    510 
    511 /* See target-descriptions.h.  */
    512 
    513 void
    514 target_desc_info_free (struct target_desc_info *tdesc_info)
    515 {
    516   delete tdesc_info;
    517 }
    518 
    519 /* The string manipulated by the "set tdesc filename ..." command.  */
    520 
    521 static std::string tdesc_filename_cmd_string;
    522 
    523 /* Fetch the current target's description, and switch the current
    524    architecture to one which incorporates that description.  */
    525 
    526 void
    527 target_find_description (void)
    528 {
    529   target_desc_info *tdesc_info = get_tdesc_info (current_inferior ());
    530 
    531   /* If we've already fetched a description from the target, don't do
    532      it again.  This allows a target to fetch the description early,
    533      during its to_open or to_create_inferior, if it needs extra
    534      information about the target to initialize.  */
    535   if (tdesc_info->fetched)
    536     return;
    537 
    538   /* The current architecture should not have any target description
    539      specified.  It should have been cleared, e.g. when we
    540      disconnected from the previous target.  */
    541   gdb_assert (gdbarch_target_desc (target_gdbarch ()) == NULL);
    542 
    543   /* First try to fetch an XML description from the user-specified
    544      file.  */
    545   tdesc_info->tdesc = nullptr;
    546   if (!tdesc_info->filename.empty ())
    547     tdesc_info->tdesc = file_read_description_xml (tdesc_info->filename.data ());
    548 
    549   /* Next try to read the description from the current target using
    550      target objects.  */
    551   if (tdesc_info->tdesc == nullptr)
    552     tdesc_info->tdesc = target_read_description_xml
    553       (current_inferior ()->top_target ());
    554 
    555   /* If that failed try a target-specific hook.  */
    556   if (tdesc_info->tdesc == nullptr)
    557     tdesc_info->tdesc = target_read_description
    558       (current_inferior ()->top_target ());
    559 
    560   /* If a non-NULL description was returned, then update the current
    561      architecture.  */
    562   if (tdesc_info->tdesc != nullptr)
    563     {
    564       struct gdbarch_info info;
    565 
    566       info.target_desc = tdesc_info->tdesc;
    567       if (!gdbarch_update_p (info))
    568 	warning (_("Architecture rejected target-supplied description"));
    569       else
    570 	{
    571 	  struct tdesc_arch_data *data;
    572 
    573 	  data = get_arch_data (target_gdbarch ());
    574 	  if (tdesc_has_registers (tdesc_info->tdesc)
    575 	      && data->arch_regs.empty ())
    576 	    warning (_("Target-supplied registers are not supported "
    577 		       "by the current architecture"));
    578 	}
    579     }
    580 
    581   /* Now that we know this description is usable, record that we
    582      fetched it.  */
    583   tdesc_info->fetched = true;
    584 }
    585 
    586 /* Discard any description fetched from the current target, and switch
    587    the current architecture to one with no target description.  */
    588 
    589 void
    590 target_clear_description (void)
    591 {
    592   target_desc_info *tdesc_info = get_tdesc_info (current_inferior ());
    593 
    594   if (!tdesc_info->fetched)
    595     return;
    596 
    597   tdesc_info->fetched = false;
    598   tdesc_info->tdesc = nullptr;
    599 
    600   gdbarch_info info;
    601   if (!gdbarch_update_p (info))
    602     internal_error (_("Could not remove target-supplied description"));
    603 }
    604 
    605 /* Return the global current target description.  This should only be
    606    used by gdbarch initialization code; most access should be through
    607    an existing gdbarch.  */
    608 
    609 const struct target_desc *
    610 target_current_description (void)
    611 {
    612   target_desc_info *tdesc_info = get_tdesc_info (current_inferior ());
    613 
    614   if (tdesc_info->fetched)
    615     return tdesc_info->tdesc;
    616 
    617   return NULL;
    618 }
    619 
    620 /* Return non-zero if this target description is compatible
    621    with the given BFD architecture.  */
    622 
    623 int
    624 tdesc_compatible_p (const struct target_desc *target_desc,
    625 		    const struct bfd_arch_info *arch)
    626 {
    627   for (const tdesc_compatible_info_up &compat : target_desc->compatible)
    628     {
    629       if (compat->arch () == arch
    630 	  || arch->compatible (arch, compat->arch ())
    631 	  || compat->arch ()->compatible (compat->arch (), arch))
    632 	return 1;
    633     }
    634 
    635   return 0;
    636 }
    637 
    638 
    640 /* Direct accessors for target descriptions.  */
    641 
    642 /* Return the string value of a property named KEY, or NULL if the
    643    property was not specified.  */
    644 
    645 const char *
    646 tdesc_property (const struct target_desc *target_desc, const char *key)
    647 {
    648   for (const property &prop : target_desc->properties)
    649     if (prop.key == key)
    650       return prop.value.c_str ();
    651 
    652   return NULL;
    653 }
    654 
    655 /* Return the BFD architecture associated with this target
    656    description, or NULL if no architecture was specified.  */
    657 
    658 const struct bfd_arch_info *
    659 tdesc_architecture (const struct target_desc *target_desc)
    660 {
    661   return target_desc->arch;
    662 }
    663 
    664 /* See gdbsupport/tdesc.h.  */
    665 
    666 const char *
    667 tdesc_architecture_name (const struct target_desc *target_desc)
    668 {
    669   if (target_desc->arch != NULL)
    670     return target_desc->arch->printable_name;
    671   return NULL;
    672 }
    673 
    674 /* See gdbsupport/tdesc.h.  */
    675 
    676 const std::vector<tdesc_compatible_info_up> &
    677 tdesc_compatible_info_list (const target_desc *target_desc)
    678 {
    679   return target_desc->compatible;
    680 }
    681 
    682 /* See gdbsupport/tdesc.h.  */
    683 
    684 const char *
    685 tdesc_compatible_info_arch_name (const tdesc_compatible_info_up &compatible)
    686 {
    687   return compatible->arch ()->printable_name;
    688 }
    689 
    690 /* Return the OSABI associated with this target description, or
    691    GDB_OSABI_UNKNOWN if no osabi was specified.  */
    692 
    693 enum gdb_osabi
    694 tdesc_osabi (const struct target_desc *target_desc)
    695 {
    696   return target_desc->osabi;
    697 }
    698 
    699 /* See gdbsupport/tdesc.h.  */
    700 
    701 const char *
    702 tdesc_osabi_name (const struct target_desc *target_desc)
    703 {
    704   enum gdb_osabi osabi = tdesc_osabi (target_desc);
    705   if (osabi > GDB_OSABI_UNKNOWN && osabi < GDB_OSABI_INVALID)
    706     return gdbarch_osabi_name (osabi);
    707   return nullptr;
    708 }
    709 
    710 /* Return 1 if this target description includes any registers.  */
    711 
    712 int
    713 tdesc_has_registers (const struct target_desc *target_desc)
    714 {
    715   if (target_desc == NULL)
    716     return 0;
    717 
    718   for (const tdesc_feature_up &feature : target_desc->features)
    719     if (!feature->registers.empty ())
    720       return 1;
    721 
    722   return 0;
    723 }
    724 
    725 /* Return the feature with the given name, if present, or NULL if
    726    the named feature is not found.  */
    727 
    728 const struct tdesc_feature *
    729 tdesc_find_feature (const struct target_desc *target_desc,
    730 		    const char *name)
    731 {
    732   for (const tdesc_feature_up &feature : target_desc->features)
    733     if (feature->name == name)
    734       return feature.get ();
    735 
    736   return NULL;
    737 }
    738 
    739 /* Return the name of FEATURE.  */
    740 
    741 const char *
    742 tdesc_feature_name (const struct tdesc_feature *feature)
    743 {
    744   return feature->name.c_str ();
    745 }
    746 
    747 /* Lookup type associated with ID.  */
    748 
    749 struct type *
    750 tdesc_find_type (struct gdbarch *gdbarch, const char *id)
    751 {
    752   tdesc_arch_data *data = get_arch_data (gdbarch);
    753 
    754   for (const tdesc_arch_reg &reg : data->arch_regs)
    755     {
    756       if (reg.reg
    757 	  && reg.reg->tdesc_type
    758 	  && reg.type
    759 	  && reg.reg->tdesc_type->name == id)
    760 	return reg.type;
    761     }
    762 
    763   return NULL;
    764 }
    765 
    766 /* Support for registers from target descriptions.  */
    767 
    768 /* Construct the per-gdbarch data.  */
    769 
    770 tdesc_arch_data_up
    771 tdesc_data_alloc (void)
    772 {
    773   return tdesc_arch_data_up (new tdesc_arch_data ());
    774 }
    775 
    776 /* See target-descriptions.h.  */
    777 
    778 void
    779 tdesc_arch_data_deleter::operator() (struct tdesc_arch_data *data) const
    780 {
    781   delete data;
    782 }
    783 
    784 /* Search FEATURE for a register named NAME.  */
    785 
    786 static struct tdesc_reg *
    787 tdesc_find_register_early (const struct tdesc_feature *feature,
    788 			   const char *name)
    789 {
    790   for (const tdesc_reg_up &reg : feature->registers)
    791     if (strcasecmp (reg->name.c_str (), name) == 0)
    792       return reg.get ();
    793 
    794   return NULL;
    795 }
    796 
    797 /* Search FEATURE for a register named NAME.  Assign REGNO to it.  */
    798 
    799 int
    800 tdesc_numbered_register (const struct tdesc_feature *feature,
    801 			 struct tdesc_arch_data *data,
    802 			 int regno, const char *name)
    803 {
    804   struct tdesc_reg *reg = tdesc_find_register_early (feature, name);
    805 
    806   if (reg == NULL)
    807     return 0;
    808 
    809   /* Make sure the vector includes a REGNO'th element.  */
    810   while (regno >= data->arch_regs.size ())
    811     data->arch_regs.emplace_back (nullptr, nullptr);
    812 
    813   data->arch_regs[regno] = tdesc_arch_reg (reg, NULL);
    814 
    815   return 1;
    816 }
    817 
    818 /* Search FEATURE for a register named NAME, but do not assign a fixed
    819    register number to it.  */
    820 
    821 int
    822 tdesc_unnumbered_register (const struct tdesc_feature *feature,
    823 			   const char *name)
    824 {
    825   struct tdesc_reg *reg = tdesc_find_register_early (feature, name);
    826 
    827   if (reg == NULL)
    828     return 0;
    829 
    830   return 1;
    831 }
    832 
    833 /* Search FEATURE for a register whose name is in NAMES and assign
    834    REGNO to it.  */
    835 
    836 int
    837 tdesc_numbered_register_choices (const struct tdesc_feature *feature,
    838 				 struct tdesc_arch_data *data,
    839 				 int regno, const char *const names[])
    840 {
    841   int i;
    842 
    843   for (i = 0; names[i] != NULL; i++)
    844     if (tdesc_numbered_register (feature, data, regno, names[i]))
    845       return 1;
    846 
    847   return 0;
    848 }
    849 
    850 /* See target-descriptions.h.  */
    851 
    852 bool
    853 tdesc_found_register (struct tdesc_arch_data *data, int regno)
    854 {
    855   gdb_assert (regno >= 0);
    856 
    857   return (regno < data->arch_regs.size ()
    858 	  && data->arch_regs[regno].reg != nullptr);
    859 }
    860 
    861 /* Search FEATURE for a register named NAME, and return its size in
    862    bits.  The register must exist.  */
    863 
    864 int
    865 tdesc_register_bitsize (const struct tdesc_feature *feature, const char *name)
    866 {
    867   struct tdesc_reg *reg = tdesc_find_register_early (feature, name);
    868 
    869   gdb_assert (reg != NULL);
    870   return reg->bitsize;
    871 }
    872 
    873 /* Look up a register by its GDB internal register number.  */
    874 
    875 static struct tdesc_arch_reg *
    876 tdesc_find_arch_register (struct gdbarch *gdbarch, int regno)
    877 {
    878   struct tdesc_arch_data *data = get_arch_data (gdbarch);
    879 
    880   if (regno < data->arch_regs.size ())
    881     return &data->arch_regs[regno];
    882   else
    883     return NULL;
    884 }
    885 
    886 static struct tdesc_reg *
    887 tdesc_find_register (struct gdbarch *gdbarch, int regno)
    888 {
    889   struct tdesc_arch_reg *reg = tdesc_find_arch_register (gdbarch, regno);
    890 
    891   return reg? reg->reg : NULL;
    892 }
    893 
    894 /* Return the name of register REGNO, from the target description or
    895    from an architecture-provided pseudo_register_name method.  */
    896 
    897 const char *
    898 tdesc_register_name (struct gdbarch *gdbarch, int regno)
    899 {
    900   struct tdesc_reg *reg = tdesc_find_register (gdbarch, regno);
    901   int num_regs = gdbarch_num_regs (gdbarch);
    902 
    903   if (reg != NULL)
    904     return reg->name.c_str ();
    905 
    906   if (regno >= num_regs && regno < gdbarch_num_cooked_regs (gdbarch))
    907     {
    908       struct tdesc_arch_data *data = get_arch_data (gdbarch);
    909 
    910       gdb_assert (data->pseudo_register_name != NULL);
    911       return data->pseudo_register_name (gdbarch, regno);
    912     }
    913 
    914   return "";
    915 }
    916 
    917 struct type *
    918 tdesc_register_type (struct gdbarch *gdbarch, int regno)
    919 {
    920   struct tdesc_arch_reg *arch_reg = tdesc_find_arch_register (gdbarch, regno);
    921   struct tdesc_reg *reg = arch_reg? arch_reg->reg : NULL;
    922   int num_regs = gdbarch_num_regs (gdbarch);
    923   int num_pseudo_regs = gdbarch_num_pseudo_regs (gdbarch);
    924 
    925   if (reg == NULL && regno >= num_regs && regno < num_regs + num_pseudo_regs)
    926     {
    927       struct tdesc_arch_data *data = get_arch_data (gdbarch);
    928 
    929       gdb_assert (data->pseudo_register_type != NULL);
    930       return data->pseudo_register_type (gdbarch, regno);
    931     }
    932 
    933   if (reg == NULL)
    934     /* Return "int0_t", since "void" has a misleading size of one.  */
    935     return builtin_type (gdbarch)->builtin_int0;
    936 
    937   if (arch_reg->type == NULL)
    938     {
    939       /* First check for a predefined or target defined type.  */
    940       if (reg->tdesc_type)
    941 	arch_reg->type = make_gdb_type (gdbarch, reg->tdesc_type);
    942 
    943       /* Next try size-sensitive type shortcuts.  */
    944       else if (reg->type == "float")
    945 	{
    946 	  if (reg->bitsize == gdbarch_float_bit (gdbarch))
    947 	    arch_reg->type = builtin_type (gdbarch)->builtin_float;
    948 	  else if (reg->bitsize == gdbarch_double_bit (gdbarch))
    949 	    arch_reg->type = builtin_type (gdbarch)->builtin_double;
    950 	  else if (reg->bitsize == gdbarch_long_double_bit (gdbarch))
    951 	    arch_reg->type = builtin_type (gdbarch)->builtin_long_double;
    952 	  else
    953 	    {
    954 	      warning (_("Register \"%s\" has an unsupported size (%d bits)"),
    955 		       reg->name.c_str (), reg->bitsize);
    956 	      arch_reg->type = builtin_type (gdbarch)->builtin_double;
    957 	    }
    958 	}
    959       else if (reg->type == "int")
    960 	{
    961 	  if (reg->bitsize == gdbarch_long_bit (gdbarch))
    962 	    arch_reg->type = builtin_type (gdbarch)->builtin_long;
    963 	  else if (reg->bitsize == TARGET_CHAR_BIT)
    964 	    arch_reg->type = builtin_type (gdbarch)->builtin_char;
    965 	  else if (reg->bitsize == gdbarch_short_bit (gdbarch))
    966 	    arch_reg->type = builtin_type (gdbarch)->builtin_short;
    967 	  else if (reg->bitsize == gdbarch_int_bit (gdbarch))
    968 	    arch_reg->type = builtin_type (gdbarch)->builtin_int;
    969 	  else if (reg->bitsize == gdbarch_long_long_bit (gdbarch))
    970 	    arch_reg->type = builtin_type (gdbarch)->builtin_long_long;
    971 	  else if (reg->bitsize == gdbarch_ptr_bit (gdbarch))
    972 	  /* A bit desperate by this point...  */
    973 	    arch_reg->type = builtin_type (gdbarch)->builtin_data_ptr;
    974 	  else
    975 	    {
    976 	      warning (_("Register \"%s\" has an unsupported size (%d bits)"),
    977 		       reg->name.c_str (), reg->bitsize);
    978 	      arch_reg->type = builtin_type (gdbarch)->builtin_long;
    979 	    }
    980 	}
    981 
    982       if (arch_reg->type == NULL)
    983 	internal_error ("Register \"%s\" has an unknown type \"%s\"",
    984 			reg->name.c_str (), reg->type.c_str ());
    985     }
    986 
    987   return arch_reg->type;
    988 }
    989 
    990 static int
    991 tdesc_remote_register_number (struct gdbarch *gdbarch, int regno)
    992 {
    993   struct tdesc_reg *reg = tdesc_find_register (gdbarch, regno);
    994 
    995   if (reg != NULL)
    996     return reg->target_regnum;
    997   else
    998     return -1;
    999 }
   1000 
   1001 /* Check whether REGNUM is a member of REGGROUP.  Registers from the
   1002    target description may be classified as general, float, vector or other
   1003    register groups registered with reggroup_add().  Unlike a gdbarch
   1004    register_reggroup_p method, this function will return -1 if it does not
   1005    know; the caller should handle registers with no specified group.
   1006 
   1007    The names of containing features are not used.  This might be extended
   1008    to display registers in some more useful groupings.
   1009 
   1010    The save-restore flag is also implemented here.  */
   1011 
   1012 int
   1013 tdesc_register_in_reggroup_p (struct gdbarch *gdbarch, int regno,
   1014 			      const struct reggroup *reggroup)
   1015 {
   1016   struct tdesc_reg *reg = tdesc_find_register (gdbarch, regno);
   1017 
   1018   if (reg != NULL && !reg->group.empty ()
   1019       && (reg->group == reggroup->name ()))
   1020 	return 1;
   1021 
   1022   if (reg != NULL
   1023       && (reggroup == save_reggroup || reggroup == restore_reggroup))
   1024     return reg->save_restore;
   1025 
   1026   return -1;
   1027 }
   1028 
   1029 /* Check whether REGNUM is a member of REGGROUP.  Registers with no
   1030    group specified go to the default reggroup function and are handled
   1031    by type.  */
   1032 
   1033 static int
   1034 tdesc_register_reggroup_p (struct gdbarch *gdbarch, int regno,
   1035 			   const struct reggroup *reggroup)
   1036 {
   1037   int num_regs = gdbarch_num_regs (gdbarch);
   1038   int num_pseudo_regs = gdbarch_num_pseudo_regs (gdbarch);
   1039   int ret;
   1040 
   1041   if (regno >= num_regs && regno < num_regs + num_pseudo_regs)
   1042     {
   1043       struct tdesc_arch_data *data = get_arch_data (gdbarch);
   1044 
   1045       if (data->pseudo_register_reggroup_p != NULL)
   1046 	return data->pseudo_register_reggroup_p (gdbarch, regno, reggroup);
   1047       /* Otherwise fall through to the default reggroup_p.  */
   1048     }
   1049 
   1050   ret = tdesc_register_in_reggroup_p (gdbarch, regno, reggroup);
   1051   if (ret != -1)
   1052     return ret;
   1053 
   1054   return default_register_reggroup_p (gdbarch, regno, reggroup);
   1055 }
   1056 
   1057 /* Record architecture-specific functions to call for pseudo-register
   1058    support.  */
   1059 
   1060 void
   1061 set_tdesc_pseudo_register_name (struct gdbarch *gdbarch,
   1062 				gdbarch_register_name_ftype *pseudo_name)
   1063 {
   1064   struct tdesc_arch_data *data = get_arch_data (gdbarch);
   1065 
   1066   data->pseudo_register_name = pseudo_name;
   1067 }
   1068 
   1069 void
   1070 set_tdesc_pseudo_register_type (struct gdbarch *gdbarch,
   1071 				gdbarch_register_type_ftype *pseudo_type)
   1072 {
   1073   struct tdesc_arch_data *data = get_arch_data (gdbarch);
   1074 
   1075   data->pseudo_register_type = pseudo_type;
   1076 }
   1077 
   1078 void
   1079 set_tdesc_pseudo_register_reggroup_p
   1080   (struct gdbarch *gdbarch,
   1081    gdbarch_register_reggroup_p_ftype *pseudo_reggroup_p)
   1082 {
   1083   struct tdesc_arch_data *data = get_arch_data (gdbarch);
   1084 
   1085   data->pseudo_register_reggroup_p = pseudo_reggroup_p;
   1086 }
   1087 
   1088 /* Update GDBARCH to use the target description for registers.  */
   1089 
   1090 void
   1091 tdesc_use_registers (struct gdbarch *gdbarch,
   1092 		     const struct target_desc *target_desc,
   1093 		     tdesc_arch_data_up &&early_data,
   1094 		     tdesc_unknown_register_ftype unk_reg_cb)
   1095 {
   1096   int num_regs = gdbarch_num_regs (gdbarch);
   1097   struct tdesc_arch_data *data;
   1098 
   1099   /* We can't use the description for registers if it doesn't describe
   1100      any.  This function should only be called after validating
   1101      registers, so the caller should know that registers are
   1102      included.  */
   1103   gdb_assert (tdesc_has_registers (target_desc));
   1104 
   1105   data = get_arch_data (gdbarch);
   1106   data->arch_regs = std::move (early_data->arch_regs);
   1107 
   1108   /* Build up a set of all registers, so that we can assign register
   1109      numbers where needed.  The hash table expands as necessary, so
   1110      the initial size is arbitrary.  */
   1111   htab_up reg_hash (htab_create (37, htab_hash_pointer, htab_eq_pointer,
   1112 				 NULL));
   1113   for (const tdesc_feature_up &feature : target_desc->features)
   1114     for (const tdesc_reg_up &reg : feature->registers)
   1115       {
   1116 	void **slot = htab_find_slot (reg_hash.get (), reg.get (), INSERT);
   1117 
   1118 	*slot = reg.get ();
   1119 	/* Add reggroup if its new.  */
   1120 	if (!reg->group.empty ())
   1121 	  if (reggroup_find (gdbarch, reg->group.c_str ()) == NULL)
   1122 	    reggroup_add (gdbarch, reggroup_gdbarch_new (gdbarch,
   1123 							 reg->group.c_str (),
   1124 							 USER_REGGROUP));
   1125       }
   1126 
   1127   /* Remove any registers which were assigned numbers by the
   1128      architecture.  */
   1129   for (const tdesc_arch_reg &arch_reg : data->arch_regs)
   1130     if (arch_reg.reg != NULL)
   1131       htab_remove_elt (reg_hash.get (), arch_reg.reg);
   1132 
   1133   /* Assign numbers to the remaining registers and add them to the
   1134      list of registers.  The new numbers are always above gdbarch_num_regs.
   1135      Iterate over the features, not the hash table, so that the order
   1136      matches that in the target description.  */
   1137 
   1138   gdb_assert (data->arch_regs.size () <= num_regs);
   1139   while (data->arch_regs.size () < num_regs)
   1140     data->arch_regs.emplace_back (nullptr, nullptr);
   1141 
   1142   /* First we give the target a chance to number previously unknown
   1143      registers.  This allows targets to record the numbers assigned based
   1144      on which feature the register was from.  */
   1145   if (unk_reg_cb != NULL)
   1146     {
   1147       for (const tdesc_feature_up &feature : target_desc->features)
   1148 	for (const tdesc_reg_up &reg : feature->registers)
   1149 	  if (htab_find (reg_hash.get (), reg.get ()) != NULL)
   1150 	    {
   1151 	      int regno = unk_reg_cb (gdbarch, feature.get (),
   1152 				      reg->name.c_str (), num_regs);
   1153 	      gdb_assert (regno == -1 || regno >= num_regs);
   1154 	      if (regno != -1)
   1155 		{
   1156 		  while (regno >= data->arch_regs.size ())
   1157 		    data->arch_regs.emplace_back (nullptr, nullptr);
   1158 		  data->arch_regs[regno] = tdesc_arch_reg (reg.get (), NULL);
   1159 		  num_regs = regno + 1;
   1160 		  htab_remove_elt (reg_hash.get (), reg.get ());
   1161 		}
   1162 	    }
   1163     }
   1164 
   1165   /* Ensure the array was sized correctly above.  */
   1166   gdb_assert (data->arch_regs.size () == num_regs);
   1167 
   1168   /* Now in a final pass we assign register numbers to any remaining
   1169      unnumbered registers.  */
   1170   for (const tdesc_feature_up &feature : target_desc->features)
   1171     for (const tdesc_reg_up &reg : feature->registers)
   1172       if (htab_find (reg_hash.get (), reg.get ()) != NULL)
   1173 	{
   1174 	  data->arch_regs.emplace_back (reg.get (), nullptr);
   1175 	  num_regs++;
   1176 	}
   1177 
   1178   /* Update the architecture.  */
   1179   set_gdbarch_num_regs (gdbarch, num_regs);
   1180   set_gdbarch_register_name (gdbarch, tdesc_register_name);
   1181   set_gdbarch_register_type (gdbarch, tdesc_register_type);
   1182   set_gdbarch_remote_register_number (gdbarch,
   1183 				      tdesc_remote_register_number);
   1184   set_gdbarch_register_reggroup_p (gdbarch, tdesc_register_reggroup_p);
   1185 }
   1186 
   1187 /* See gdbsupport/tdesc.h.  */
   1188 
   1189 struct tdesc_feature *
   1190 tdesc_create_feature (struct target_desc *tdesc, const char *name)
   1191 {
   1192   struct tdesc_feature *new_feature = new tdesc_feature (name);
   1193 
   1194   tdesc->features.emplace_back (new_feature);
   1195 
   1196   return new_feature;
   1197 }
   1198 
   1199 /* See gdbsupport/tdesc.h.  */
   1200 
   1201 target_desc_up
   1202 allocate_target_description (void)
   1203 {
   1204   return target_desc_up (new target_desc ());
   1205 }
   1206 
   1207 /* See gdbsupport/tdesc.h.  */
   1208 
   1209 void
   1210 target_desc_deleter::operator() (struct target_desc *target_desc) const
   1211 {
   1212   delete target_desc;
   1213 }
   1214 
   1215 void
   1216 tdesc_add_compatible (struct target_desc *target_desc,
   1217 		      const struct bfd_arch_info *compatible)
   1218 {
   1219   /* If this instance of GDB is compiled without BFD support for the
   1220      compatible architecture, simply ignore it -- we would not be able
   1221      to handle it anyway.  */
   1222   if (compatible == NULL)
   1223     return;
   1224 
   1225   for (const tdesc_compatible_info_up &compat : target_desc->compatible)
   1226     if (compat->arch () == compatible)
   1227       internal_error (_("Attempted to add duplicate "
   1228 			"compatible architecture \"%s\""),
   1229 		      compatible->printable_name);
   1230 
   1231   target_desc->compatible.push_back
   1232     (std::unique_ptr<tdesc_compatible_info>
   1233      (new tdesc_compatible_info (compatible)));
   1234 }
   1235 
   1236 void
   1237 set_tdesc_property (struct target_desc *target_desc,
   1238 		    const char *key, const char *value)
   1239 {
   1240   gdb_assert (key != NULL && value != NULL);
   1241 
   1242   if (tdesc_property (target_desc, key) != NULL)
   1243     internal_error (_("Attempted to add duplicate property \"%s\""), key);
   1244 
   1245   target_desc->properties.emplace_back (key, value);
   1246 }
   1247 
   1248 /* See gdbsupport/tdesc.h.  */
   1249 
   1250 void
   1251 set_tdesc_architecture (struct target_desc *target_desc,
   1252 			const char *name)
   1253 {
   1254   set_tdesc_architecture (target_desc, bfd_scan_arch (name));
   1255 }
   1256 
   1257 void
   1258 set_tdesc_architecture (struct target_desc *target_desc,
   1259 			const struct bfd_arch_info *arch)
   1260 {
   1261   target_desc->arch = arch;
   1262 }
   1263 
   1264 /* See gdbsupport/tdesc.h.  */
   1265 
   1266 void
   1267 set_tdesc_osabi (struct target_desc *target_desc, const char *name)
   1268 {
   1269   set_tdesc_osabi (target_desc, osabi_from_tdesc_string (name));
   1270 }
   1271 
   1272 void
   1273 set_tdesc_osabi (struct target_desc *target_desc, enum gdb_osabi osabi)
   1274 {
   1275   target_desc->osabi = osabi;
   1276 }
   1277 
   1278 
   1280 static struct cmd_list_element *tdesc_set_cmdlist, *tdesc_show_cmdlist;
   1281 static struct cmd_list_element *tdesc_unset_cmdlist;
   1282 
   1283 /* Helper functions for the CLI commands.  */
   1284 
   1285 static void
   1286 set_tdesc_filename_cmd (const char *args, int from_tty,
   1287 			struct cmd_list_element *c)
   1288 {
   1289   target_desc_info *tdesc_info = get_tdesc_info (current_inferior ());
   1290 
   1291   tdesc_info->filename = tdesc_filename_cmd_string;
   1292 
   1293   target_clear_description ();
   1294   target_find_description ();
   1295 }
   1296 
   1297 static void
   1298 show_tdesc_filename_cmd (struct ui_file *file, int from_tty,
   1299 			 struct cmd_list_element *c,
   1300 			 const char *value)
   1301 {
   1302   value = get_tdesc_info (current_inferior ())->filename.data ();
   1303 
   1304   if (value != NULL && *value != '\0')
   1305     gdb_printf (file,
   1306 		_("The target description will be read from \"%s\".\n"),
   1307 		value);
   1308   else
   1309     gdb_printf (file,
   1310 		_("The target description will be "
   1311 		  "read from the target.\n"));
   1312 }
   1313 
   1314 static void
   1315 unset_tdesc_filename_cmd (const char *args, int from_tty)
   1316 {
   1317   target_desc_info *tdesc_info = get_tdesc_info (current_inferior ());
   1318 
   1319   tdesc_info->filename.clear ();
   1320   target_clear_description ();
   1321   target_find_description ();
   1322 }
   1323 
   1324 /* Print target description in C.  */
   1325 
   1326 class print_c_tdesc : public tdesc_element_visitor
   1327 {
   1328 public:
   1329   print_c_tdesc (std::string &filename_after_features)
   1330     : m_filename_after_features (filename_after_features)
   1331   {
   1332     const char *inp;
   1333     char *outp;
   1334     const char *filename = lbasename (m_filename_after_features.c_str ());
   1335 
   1336     m_function = (char *) xmalloc (strlen (filename) + 1);
   1337     for (inp = filename, outp = m_function; *inp != '\0'; inp++)
   1338       if (*inp == '.')
   1339 	break;
   1340       else if (*inp == '-')
   1341 	*outp++ = '_';
   1342       else if (*inp == ' ')
   1343 	*outp++ = '_';
   1344       else
   1345 	*outp++ = *inp;
   1346     *outp = '\0';
   1347 
   1348     /* Standard boilerplate.  */
   1349     gdb_printf ("/* THIS FILE IS GENERATED.  "
   1350 		"-*- buffer-read-only: t -*- vi"
   1351 		":set ro:\n");
   1352   }
   1353 
   1354   ~print_c_tdesc ()
   1355   {
   1356     xfree (m_function);
   1357   }
   1358 
   1359   void visit_pre (const target_desc *e) override
   1360   {
   1361     gdb_printf ("  Original: %s */\n\n",
   1362 		lbasename (m_filename_after_features.c_str ()));
   1363 
   1364     gdb_printf ("#include \"defs.h\"\n");
   1365     gdb_printf ("#include \"osabi.h\"\n");
   1366     gdb_printf ("#include \"target-descriptions.h\"\n");
   1367     gdb_printf ("\n");
   1368 
   1369     gdb_printf ("const struct target_desc *tdesc_%s;\n", m_function);
   1370     gdb_printf ("static void\n");
   1371     gdb_printf ("initialize_tdesc_%s (void)\n", m_function);
   1372     gdb_printf ("{\n");
   1373     gdb_printf
   1374       ("  target_desc_up result = allocate_target_description ();\n");
   1375 
   1376     if (tdesc_architecture (e) != NULL)
   1377       {
   1378 	gdb_printf
   1379 	  ("  set_tdesc_architecture (result.get (), bfd_scan_arch (\"%s\"));\n",
   1380 	   tdesc_architecture (e)->printable_name);
   1381 	gdb_printf ("\n");
   1382       }
   1383     if (tdesc_osabi (e) > GDB_OSABI_UNKNOWN
   1384 	&& tdesc_osabi (e) < GDB_OSABI_INVALID)
   1385       {
   1386 	gdb_printf
   1387 	  ("  set_tdesc_osabi (result.get (), osabi_from_tdesc_string (\"%s\"));\n",
   1388 	   gdbarch_osabi_name (tdesc_osabi (e)));
   1389 	gdb_printf ("\n");
   1390       }
   1391 
   1392     for (const tdesc_compatible_info_up &compatible : e->compatible)
   1393       gdb_printf
   1394 	("  tdesc_add_compatible (result.get (), bfd_scan_arch (\"%s\"));\n",
   1395 	 compatible->arch ()->printable_name);
   1396 
   1397     if (!e->compatible.empty ())
   1398       gdb_printf ("\n");
   1399 
   1400     for (const property &prop : e->properties)
   1401       gdb_printf ("  set_tdesc_property (result.get (), \"%s\", \"%s\");\n",
   1402 		  prop.key.c_str (), prop.value.c_str ());
   1403 
   1404     gdb_printf ("  struct tdesc_feature *feature;\n");
   1405   }
   1406 
   1407   void visit_pre (const tdesc_feature *e) override
   1408   {
   1409     gdb_printf ("\n  feature = tdesc_create_feature (result.get (), \"%s\");\n",
   1410 		e->name.c_str ());
   1411   }
   1412 
   1413   void visit_post (const tdesc_feature *e) override
   1414   {}
   1415 
   1416   void visit_post (const target_desc *e) override
   1417   {
   1418     gdb_printf ("\n  tdesc_%s = result.release ();\n", m_function);
   1419     gdb_printf ("}\n");
   1420   }
   1421 
   1422   void visit (const tdesc_type_builtin *type) override
   1423   {
   1424     error (_("C output is not supported type \"%s\"."), type->name.c_str ());
   1425   }
   1426 
   1427   void visit (const tdesc_type_vector *type) override
   1428   {
   1429     if (!m_printed_element_type)
   1430       {
   1431 	gdb_printf ("  tdesc_type *element_type;\n");
   1432 	m_printed_element_type = true;
   1433       }
   1434 
   1435     gdb_printf
   1436       ("  element_type = tdesc_named_type (feature, \"%s\");\n",
   1437        type->element_type->name.c_str ());
   1438     gdb_printf
   1439       ("  tdesc_create_vector (feature, \"%s\", element_type, %d);\n",
   1440        type->name.c_str (), type->count);
   1441 
   1442     gdb_printf ("\n");
   1443   }
   1444 
   1445   void visit (const tdesc_type_with_fields *type) override
   1446   {
   1447     if (!m_printed_type_with_fields)
   1448       {
   1449 	gdb_printf ("  tdesc_type_with_fields *type_with_fields;\n");
   1450 	m_printed_type_with_fields = true;
   1451       }
   1452 
   1453     switch (type->kind)
   1454       {
   1455       case TDESC_TYPE_STRUCT:
   1456       case TDESC_TYPE_FLAGS:
   1457 	if (type->kind == TDESC_TYPE_STRUCT)
   1458 	  {
   1459 	    gdb_printf
   1460 	      ("  type_with_fields = tdesc_create_struct (feature, \"%s\");\n",
   1461 	       type->name.c_str ());
   1462 	    if (type->size != 0)
   1463 	      gdb_printf
   1464 		("  tdesc_set_struct_size (type_with_fields, %d);\n", type->size);
   1465 	  }
   1466 	else
   1467 	  {
   1468 	    gdb_printf
   1469 	      ("  type_with_fields = tdesc_create_flags (feature, \"%s\", %d);\n",
   1470 	       type->name.c_str (), type->size);
   1471 	  }
   1472 	for (const tdesc_type_field &f : type->fields)
   1473 	  {
   1474 	    const char *type_name;
   1475 
   1476 	    gdb_assert (f.type != NULL);
   1477 	    type_name = f.type->name.c_str ();
   1478 
   1479 	    /* To minimize changes to generated files, don't emit type
   1480 	       info for fields that have defaulted types.  */
   1481 	    if (f.start != -1)
   1482 	      {
   1483 		gdb_assert (f.end != -1);
   1484 		if (f.type->kind == TDESC_TYPE_BOOL)
   1485 		  {
   1486 		    gdb_assert (f.start == f.end);
   1487 		    gdb_printf
   1488 		      ("  tdesc_add_flag (type_with_fields, %d, \"%s\");\n",
   1489 		       f.start, f.name.c_str ());
   1490 		  }
   1491 		else if ((type->size == 4 && f.type->kind == TDESC_TYPE_UINT32)
   1492 			 || (type->size == 8
   1493 			     && f.type->kind == TDESC_TYPE_UINT64))
   1494 		  {
   1495 		    gdb_printf
   1496 		      ("  tdesc_add_bitfield (type_with_fields, \"%s\", %d, %d);\n",
   1497 		       f.name.c_str (), f.start, f.end);
   1498 		  }
   1499 		else
   1500 		  {
   1501 		    printf_field_type_assignment
   1502 		      ("tdesc_named_type (feature, \"%s\");\n",
   1503 		       type_name);
   1504 		    gdb_printf
   1505 		      ("  tdesc_add_typed_bitfield (type_with_fields, \"%s\","
   1506 		       " %d, %d, field_type);\n",
   1507 		       f.name.c_str (), f.start, f.end);
   1508 		  }
   1509 	      }
   1510 	    else /* Not a bitfield.  */
   1511 	      {
   1512 		gdb_assert (f.end == -1);
   1513 		gdb_assert (type->kind == TDESC_TYPE_STRUCT);
   1514 		printf_field_type_assignment
   1515 		  ("tdesc_named_type (feature, \"%s\");\n", type_name);
   1516 		gdb_printf
   1517 		  ("  tdesc_add_field (type_with_fields, \"%s\", field_type);\n",
   1518 		   f.name.c_str ());
   1519 	      }
   1520 	  }
   1521 	break;
   1522       case TDESC_TYPE_UNION:
   1523 	gdb_printf
   1524 	  ("  type_with_fields = tdesc_create_union (feature, \"%s\");\n",
   1525 	   type->name.c_str ());
   1526 	for (const tdesc_type_field &f : type->fields)
   1527 	  {
   1528 	    printf_field_type_assignment
   1529 	      ("tdesc_named_type (feature, \"%s\");\n", f.type->name.c_str ());
   1530 	    gdb_printf
   1531 	      ("  tdesc_add_field (type_with_fields, \"%s\", field_type);\n",
   1532 	       f.name.c_str ());
   1533 	  }
   1534 	break;
   1535       case TDESC_TYPE_ENUM:
   1536 	gdb_printf
   1537 	  ("  type_with_fields = tdesc_create_enum (feature, \"%s\", %d);\n",
   1538 	   type->name.c_str (), type->size);
   1539 	for (const tdesc_type_field &f : type->fields)
   1540 	  gdb_printf
   1541 	    ("  tdesc_add_enum_value (type_with_fields, %d, \"%s\");\n",
   1542 	     f.start, f.name.c_str ());
   1543 	break;
   1544       default:
   1545 	error (_("C output is not supported type \"%s\"."), type->name.c_str ());
   1546       }
   1547 
   1548     gdb_printf ("\n");
   1549   }
   1550 
   1551   void visit (const tdesc_reg *reg) override
   1552   {
   1553     gdb_printf ("  tdesc_create_reg (feature, \"%s\", %ld, %d, ",
   1554 		reg->name.c_str (), reg->target_regnum,
   1555 		reg->save_restore);
   1556     if (!reg->group.empty ())
   1557       gdb_printf ("\"%s\", ", reg->group.c_str ());
   1558     else
   1559       gdb_printf ("NULL, ");
   1560     gdb_printf ("%d, \"%s\");\n", reg->bitsize, reg->type.c_str ());
   1561   }
   1562 
   1563 protected:
   1564   std::string m_filename_after_features;
   1565 
   1566 private:
   1567 
   1568   /* Print an assignment to the field_type variable.  Print the declaration
   1569      of field_type if that has not been done yet.  */
   1570   ATTRIBUTE_PRINTF (2, 3)
   1571   void printf_field_type_assignment (const char *fmt, ...)
   1572   {
   1573     if (!m_printed_field_type)
   1574       {
   1575 	gdb_printf ("  tdesc_type *field_type;\n");
   1576 	m_printed_field_type = true;
   1577       }
   1578 
   1579     gdb_printf ("  field_type = ");
   1580 
   1581     va_list args;
   1582     va_start (args, fmt);
   1583     gdb_vprintf (fmt, args);
   1584     va_end (args);
   1585   }
   1586 
   1587   char *m_function;
   1588 
   1589   /* Did we print "struct tdesc_type *element_type;" yet?  */
   1590   bool m_printed_element_type = false;
   1591 
   1592   /* Did we print "struct tdesc_type_with_fields *element_type;" yet?  */
   1593   bool m_printed_type_with_fields = false;
   1594 
   1595   /* Did we print "struct tdesc_type *field_type;" yet?  */
   1596   bool m_printed_field_type = false;
   1597 };
   1598 
   1599 /* Print target description feature in C.  */
   1600 
   1601 class print_c_feature : public print_c_tdesc
   1602 {
   1603 public:
   1604   print_c_feature (std::string &file)
   1605     : print_c_tdesc (file)
   1606   {
   1607     /* Trim ".tmp".  */
   1608     auto const pos = m_filename_after_features.find_last_of ('.');
   1609 
   1610     m_filename_after_features = m_filename_after_features.substr (0, pos);
   1611   }
   1612 
   1613   void visit_pre (const target_desc *e) override
   1614   {
   1615     gdb_printf ("  Original: %s */\n\n",
   1616 		lbasename (m_filename_after_features.c_str ()));
   1617 
   1618     gdb_printf ("#include \"gdbsupport/tdesc.h\"\n");
   1619     gdb_printf ("\n");
   1620   }
   1621 
   1622   void visit_post (const target_desc *e) override
   1623   {}
   1624 
   1625   void visit_pre (const tdesc_feature *e) override
   1626   {
   1627     std::string name (m_filename_after_features);
   1628 
   1629     auto pos = name.find_first_of ('.');
   1630 
   1631     name = name.substr (0, pos);
   1632     std::replace (name.begin (), name.end (), '/', '_');
   1633     std::replace (name.begin (), name.end (), '-', '_');
   1634 
   1635     gdb_printf ("static int\n");
   1636     gdb_printf ("create_feature_%s ", name.c_str ());
   1637     gdb_printf ("(struct target_desc *result, long regnum)\n");
   1638 
   1639     gdb_printf ("{\n");
   1640     gdb_printf ("  struct tdesc_feature *feature;\n");
   1641 
   1642     gdb_printf
   1643       ("\n  feature = tdesc_create_feature (result, \"%s\");\n",
   1644        e->name.c_str ());
   1645   }
   1646 
   1647   void visit_post (const tdesc_feature *e) override
   1648   {
   1649     gdb_printf ("  return regnum;\n");
   1650     gdb_printf ("}\n");
   1651   }
   1652 
   1653   void visit (const tdesc_reg *reg) override
   1654   {
   1655     /* Most "reg" in XML target descriptions don't have "regnum"
   1656        attribute, so the register number is allocated sequentially.
   1657        In case that reg has "regnum" attribute, register number
   1658        should be set by that explicitly.  */
   1659 
   1660     if (reg->target_regnum < m_next_regnum)
   1661       {
   1662 	/* The integrity check, it can catch some errors on register
   1663 	   number collision, like this,
   1664 
   1665 	  <reg name="x0" bitsize="32"/>
   1666 	  <reg name="x1" bitsize="32"/>
   1667 	  <reg name="x2" bitsize="32"/>
   1668 	  <reg name="x3" bitsize="32"/>
   1669 	  <reg name="ps" bitsize="32" regnum="3"/>
   1670 
   1671 	  but it also has false negatives.  The target description
   1672 	  below is correct,
   1673 
   1674 	  <reg name="x1" bitsize="32" regnum="1"/>
   1675 	  <reg name="x3" bitsize="32" regnum="3"/>
   1676 	  <reg name="x2" bitsize="32" regnum="2"/>
   1677 	  <reg name="x4" bitsize="32" regnum="4"/>
   1678 
   1679 	  but it is not a good practice, so still error on this,
   1680 	  and also print the message so that it can be saved in the
   1681 	  generated c file.  */
   1682 
   1683 	gdb_printf ("ERROR: \"regnum\" attribute %ld ",
   1684 		    reg->target_regnum);
   1685 	gdb_printf ("is not the largest number (%d).\n",
   1686 		    m_next_regnum);
   1687 	error (_("\"regnum\" attribute %ld is not the largest number (%d)."),
   1688 	       reg->target_regnum, m_next_regnum);
   1689       }
   1690 
   1691     if (reg->target_regnum > m_next_regnum)
   1692       {
   1693 	gdb_printf ("  regnum = %ld;\n", reg->target_regnum);
   1694 	m_next_regnum = reg->target_regnum;
   1695       }
   1696 
   1697     gdb_printf ("  tdesc_create_reg (feature, \"%s\", regnum++, %d, ",
   1698 		reg->name.c_str (), reg->save_restore);
   1699     if (!reg->group.empty ())
   1700       gdb_printf ("\"%s\", ", reg->group.c_str ());
   1701     else
   1702       gdb_printf ("NULL, ");
   1703     gdb_printf ("%d, \"%s\");\n", reg->bitsize, reg->type.c_str ());
   1704 
   1705     m_next_regnum++;
   1706   }
   1707 
   1708 private:
   1709   /* The register number to use for the next register we see.  */
   1710   int m_next_regnum = 0;
   1711 };
   1712 
   1713 /* See gdbsupport/tdesc.h.  */
   1714 
   1715 const char *
   1716 tdesc_get_features_xml (const target_desc *tdesc)
   1717 {
   1718   if (tdesc->xmltarget == nullptr)
   1719     {
   1720       std::string buffer ("@");
   1721       print_xml_feature v (&buffer);
   1722       tdesc->accept (v);
   1723       tdesc->xmltarget = xstrdup (buffer.c_str ());
   1724     }
   1725   return tdesc->xmltarget;
   1726 }
   1727 
   1728 /* Data structures and functions to setup the option flags for 'maintenance
   1729    print c-tdesc command.  */
   1730 
   1731 struct maint_print_c_tdesc_options
   1732 {
   1733   /* True when the '-single-feature' flag was passed.  */
   1734   bool single_feature = false;
   1735 };
   1736 
   1737 using maint_print_c_tdesc_opt_def
   1738   = gdb::option::flag_option_def<maint_print_c_tdesc_options>;
   1739 
   1740 static const gdb::option::option_def maint_print_c_tdesc_opt_defs[] = {
   1741   maint_print_c_tdesc_opt_def {
   1742     "single-feature",
   1743     [] (maint_print_c_tdesc_options *opt) { return &opt->single_feature; },
   1744     N_("Print C description of just a single feature.")
   1745   },
   1746 };
   1747 
   1748 static inline gdb::option::option_def_group
   1749 make_maint_print_c_tdesc_options_def_group (maint_print_c_tdesc_options *opts)
   1750 {
   1751   return {{maint_print_c_tdesc_opt_defs}, opts};
   1752 }
   1753 
   1754 /* Implement 'maintenance print c-tdesc' command.  */
   1755 
   1756 static void
   1757 maint_print_c_tdesc_cmd (const char *args, int from_tty)
   1758 {
   1759   const struct target_desc *tdesc;
   1760   const char *filename;
   1761 
   1762   maint_print_c_tdesc_options opts;
   1763   auto grp = make_maint_print_c_tdesc_options_def_group (&opts);
   1764   gdb::option::process_options
   1765     (&args, gdb::option::PROCESS_OPTIONS_UNKNOWN_IS_ERROR, grp);
   1766 
   1767   if (args == NULL)
   1768     {
   1769       /* Use the global target-supplied description, not the current
   1770 	 architecture's.  This lets a GDB for one architecture generate C
   1771 	 for another architecture's description, even though the gdbarch
   1772 	 initialization code will reject the new description.  */
   1773       target_desc_info *tdesc_info = get_tdesc_info (current_inferior ());
   1774       tdesc = tdesc_info->tdesc;
   1775       filename = tdesc_info->filename.data ();
   1776     }
   1777   else
   1778     {
   1779       /* Use the target description from the XML file.  */
   1780       filename = args;
   1781       tdesc = file_read_description_xml (filename);
   1782     }
   1783 
   1784   if (tdesc == NULL)
   1785     error (_("There is no target description to print."));
   1786 
   1787   if (filename == NULL)
   1788     filename = "fetched from target";
   1789 
   1790   std::string filename_after_features (filename);
   1791   auto loc = filename_after_features.rfind ("/features/");
   1792 
   1793   if (loc != std::string::npos)
   1794     filename_after_features = filename_after_features.substr (loc + 10);
   1795 
   1796   /* Print c files for target features instead of target descriptions,
   1797      because c files got from target features are more flexible than the
   1798      counterparts.  */
   1799   if (opts.single_feature)
   1800     {
   1801       if (tdesc->features.size () != 1)
   1802 	error (_("only target descriptions with 1 feature can be used "
   1803 		 "with -single-feature option"));
   1804 
   1805       print_c_feature v (filename_after_features);
   1806 
   1807       tdesc->accept (v);
   1808     }
   1809   else
   1810     {
   1811       print_c_tdesc v (filename_after_features);
   1812 
   1813       tdesc->accept (v);
   1814     }
   1815 }
   1816 
   1817 /* Completer for the "backtrace" command.  */
   1818 
   1819 static void
   1820 maint_print_c_tdesc_cmd_completer (struct cmd_list_element *ignore,
   1821 				   completion_tracker &tracker,
   1822 				   const char *text, const char *word)
   1823 {
   1824   auto grp = make_maint_print_c_tdesc_options_def_group (nullptr);
   1825   if (gdb::option::complete_options
   1826       (tracker, &text, gdb::option::PROCESS_OPTIONS_UNKNOWN_IS_ERROR, grp))
   1827     return;
   1828 
   1829   word = advance_to_filename_complete_word_point (tracker, text);
   1830   filename_completer (ignore, tracker, text, word);
   1831 }
   1832 
   1833 /* Implement the maintenance print xml-tdesc command.  */
   1834 
   1835 static void
   1836 maint_print_xml_tdesc_cmd (const char *args, int from_tty)
   1837 {
   1838   const struct target_desc *tdesc;
   1839 
   1840   if (args == NULL)
   1841     {
   1842       /* Use the global target-supplied description, not the current
   1843 	 architecture's.  This lets a GDB for one architecture generate XML
   1844 	 for another architecture's description, even though the gdbarch
   1845 	 initialization code will reject the new description.  */
   1846       tdesc = get_tdesc_info (current_inferior ())->tdesc;
   1847     }
   1848   else
   1849     {
   1850       /* Use the target description from the XML file.  */
   1851       tdesc = file_read_description_xml (args);
   1852     }
   1853 
   1854   if (tdesc == NULL)
   1855     error (_("There is no target description to print."));
   1856 
   1857   std::string buf;
   1858   print_xml_feature v (&buf);
   1859   tdesc->accept (v);
   1860   gdb_puts (buf.c_str ());
   1861 }
   1862 
   1863 namespace selftests {
   1864 
   1865 /* A reference target description, used for testing (see record_xml_tdesc).  */
   1866 
   1867 struct xml_test_tdesc
   1868 {
   1869   xml_test_tdesc (const char *name, std::unique_ptr<const target_desc> &&tdesc)
   1870     : name (name), tdesc (std::move (tdesc))
   1871   {}
   1872 
   1873   const char *name;
   1874   std::unique_ptr<const target_desc> tdesc;
   1875 };
   1876 
   1877 static std::vector<xml_test_tdesc> xml_tdesc;
   1878 
   1879 #if GDB_SELF_TEST
   1880 
   1881 /* See target-descriptions.h.  */
   1882 
   1883 void
   1884 record_xml_tdesc (const char *xml_file, const struct target_desc *tdesc)
   1885 {
   1886   xml_tdesc.emplace_back (xml_file, std::unique_ptr<const target_desc> (tdesc));
   1887 }
   1888 #endif
   1889 
   1890 }
   1891 
   1892 /* Test the conversion process of a target description to/from xml: Take a target
   1893    description TDESC, convert to xml, back to a description, and confirm the new
   1894    tdesc is identical to the original.  */
   1895 static bool
   1896 maintenance_check_tdesc_xml_convert (const target_desc *tdesc, const char *name)
   1897 {
   1898   const char *xml = tdesc_get_features_xml (tdesc);
   1899 
   1900   if (xml == nullptr || *xml != '@')
   1901     {
   1902       gdb_printf (_("Could not convert description for %s to xml.\n"),
   1903 		  name);
   1904       return false;
   1905     }
   1906 
   1907   const target_desc *tdesc_trans = string_read_description_xml (xml + 1);
   1908 
   1909   if (tdesc_trans == nullptr)
   1910     {
   1911       gdb_printf (_("Could not convert description for %s from xml.\n"),
   1912 		  name);
   1913       return false;
   1914     }
   1915   else if (*tdesc != *tdesc_trans)
   1916     {
   1917       gdb_printf (_("Converted description for %s does not match.\n"),
   1918 		  name);
   1919       return false;
   1920     }
   1921   return true;
   1922 }
   1923 
   1924 
   1925 /* Check that the target descriptions created dynamically by
   1926    architecture-specific code equal the descriptions created from XML files
   1927    found in the specified directory DIR.  */
   1928 
   1929 static void
   1930 maintenance_check_xml_descriptions (const char *dir, int from_tty)
   1931 {
   1932   if (dir == NULL)
   1933     error (_("Missing dir name"));
   1934 
   1935   gdb::unique_xmalloc_ptr<char> dir1 (tilde_expand (dir));
   1936   std::string feature_dir (dir1.get ());
   1937   unsigned int failed = 0;
   1938 
   1939   for (auto const &e : selftests::xml_tdesc)
   1940     {
   1941       std::string tdesc_xml = (feature_dir + SLASH_STRING + e.name);
   1942       const target_desc *tdesc
   1943 	= file_read_description_xml (tdesc_xml.data ());
   1944 
   1945       if (tdesc == NULL || *tdesc != *e.tdesc)
   1946 	{
   1947 	  gdb_printf ( _("Descriptions for %s do not match.\n"), e.name);
   1948 	  failed++;
   1949 	}
   1950       else if (!maintenance_check_tdesc_xml_convert (tdesc, e.name)
   1951 	       || !maintenance_check_tdesc_xml_convert (e.tdesc.get (), e.name))
   1952 	failed++;
   1953     }
   1954   gdb_printf (_("Tested %lu XML files, %d failed\n"),
   1955 	      (long) selftests::xml_tdesc.size (), failed);
   1956 }
   1957 
   1958 void _initialize_target_descriptions ();
   1959 void
   1960 _initialize_target_descriptions ()
   1961 {
   1962   cmd_list_element *cmd;
   1963 
   1964   add_setshow_prefix_cmd ("tdesc", class_maintenance,
   1965 			  _("Set target description specific variables."),
   1966 			  _("Show target description specific variables."),
   1967 			  &tdesc_set_cmdlist, &tdesc_show_cmdlist,
   1968 			  &setlist, &showlist);
   1969 
   1970   add_basic_prefix_cmd ("tdesc", class_maintenance, _("\
   1971 Unset target description specific variables."),
   1972 			&tdesc_unset_cmdlist,
   1973 			0 /* allow-unknown */, &unsetlist);
   1974 
   1975   add_setshow_filename_cmd ("filename", class_obscure,
   1976 			    &tdesc_filename_cmd_string,
   1977 			    _("\
   1978 Set the file to read for an XML target description."), _("\
   1979 Show the file to read for an XML target description."), _("\
   1980 When set, GDB will read the target description from a local\n\
   1981 file instead of querying the remote target."),
   1982 			    set_tdesc_filename_cmd,
   1983 			    show_tdesc_filename_cmd,
   1984 			    &tdesc_set_cmdlist, &tdesc_show_cmdlist);
   1985 
   1986   add_cmd ("filename", class_obscure, unset_tdesc_filename_cmd, _("\
   1987 Unset the file to read for an XML target description.\n\
   1988 When unset, GDB will read the description from the target."),
   1989 	   &tdesc_unset_cmdlist);
   1990 
   1991   auto grp = make_maint_print_c_tdesc_options_def_group (nullptr);
   1992   static std::string help_text
   1993     = gdb::option::build_help (_("\
   1994 Print the current target description as a C source file.\n\
   1995 Usage: maintenance print c-tdesc [OPTION] [FILENAME]\n\
   1996 \n\
   1997 Options:\n\
   1998 %OPTIONS%\n\
   1999 \n\
   2000 When FILENAME is not provided then print the current target\n\
   2001 description, otherwise an XML target description is read from\n\
   2002 FILENAME and printed as a C function.\n\
   2003 \n\
   2004 When '-single-feature' is used then the target description should\n\
   2005 contain a single feature and the generated C code will only create\n\
   2006 that feature within an already existing target_desc object."), grp);
   2007   cmd = add_cmd ("c-tdesc", class_maintenance, maint_print_c_tdesc_cmd,
   2008 		 help_text.c_str (), &maintenanceprintlist);
   2009   set_cmd_completer_handle_brkchars (cmd, maint_print_c_tdesc_cmd_completer);
   2010 
   2011   cmd = add_cmd ("xml-tdesc", class_maintenance, maint_print_xml_tdesc_cmd, _("\
   2012 Print the current target description as an XML file."),
   2013 		 &maintenanceprintlist);
   2014   set_cmd_completer (cmd, filename_completer);
   2015 
   2016   cmd = add_cmd ("xml-descriptions", class_maintenance,
   2017 		 maintenance_check_xml_descriptions, _("\
   2018 Check equality of GDB target descriptions and XML created descriptions.\n\
   2019 Check the target descriptions created in GDB equal the descriptions\n\
   2020 created from XML files in the directory.\n\
   2021 The parameter is the directory name."),
   2022 		 &maintenancechecklist);
   2023   set_cmd_completer (cmd, filename_completer);
   2024 }
   2025