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 "xml-support.h"
30 #include "xml-tdesc.h"
33 #include "gdb_obstack.h"
37 #include "completer.h"
38 #include "readline/tilde.h" /* tilde_expand */
44 property (const std::string
&key_
, const std::string
&value_
)
45 : key (key_
), value (value_
)
52 /* Convert a tdesc_type to a gdb type. */
55 make_gdb_type (struct gdbarch
*gdbarch
, struct tdesc_type
*ttype
)
57 class gdb_type_creator
: public tdesc_element_visitor
60 gdb_type_creator (struct gdbarch
*gdbarch
)
69 void visit (const tdesc_type_builtin
*e
) override
73 /* Predefined types. */
75 m_type
= builtin_type (m_gdbarch
)->builtin_bool
;
78 m_type
= builtin_type (m_gdbarch
)->builtin_int8
;
80 case TDESC_TYPE_INT16
:
81 m_type
= builtin_type (m_gdbarch
)->builtin_int16
;
83 case TDESC_TYPE_INT32
:
84 m_type
= builtin_type (m_gdbarch
)->builtin_int32
;
86 case TDESC_TYPE_INT64
:
87 m_type
= builtin_type (m_gdbarch
)->builtin_int64
;
89 case TDESC_TYPE_INT128
:
90 m_type
= builtin_type (m_gdbarch
)->builtin_int128
;
92 case TDESC_TYPE_UINT8
:
93 m_type
= builtin_type (m_gdbarch
)->builtin_uint8
;
95 case TDESC_TYPE_UINT16
:
96 m_type
= builtin_type (m_gdbarch
)->builtin_uint16
;
98 case TDESC_TYPE_UINT32
:
99 m_type
= builtin_type (m_gdbarch
)->builtin_uint32
;
101 case TDESC_TYPE_UINT64
:
102 m_type
= builtin_type (m_gdbarch
)->builtin_uint64
;
104 case TDESC_TYPE_UINT128
:
105 m_type
= builtin_type (m_gdbarch
)->builtin_uint128
;
107 case TDESC_TYPE_CODE_PTR
:
108 m_type
= builtin_type (m_gdbarch
)->builtin_func_ptr
;
110 case TDESC_TYPE_DATA_PTR
:
111 m_type
= builtin_type (m_gdbarch
)->builtin_data_ptr
;
115 m_type
= tdesc_find_type (m_gdbarch
, e
->name
.c_str ());
121 case TDESC_TYPE_IEEE_HALF
:
122 m_type
= arch_float_type (m_gdbarch
, -1, "builtin_type_ieee_half",
123 floatformats_ieee_half
);
126 case TDESC_TYPE_IEEE_SINGLE
:
127 m_type
= arch_float_type (m_gdbarch
, -1, "builtin_type_ieee_single",
128 floatformats_ieee_single
);
131 case TDESC_TYPE_IEEE_DOUBLE
:
132 m_type
= arch_float_type (m_gdbarch
, -1, "builtin_type_ieee_double",
133 floatformats_ieee_double
);
135 case TDESC_TYPE_ARM_FPA_EXT
:
136 m_type
= arch_float_type (m_gdbarch
, -1, "builtin_type_arm_ext",
137 floatformats_arm_ext
);
140 case TDESC_TYPE_I387_EXT
:
141 m_type
= arch_float_type (m_gdbarch
, -1, "builtin_type_i387_ext",
142 floatformats_i387_ext
);
146 internal_error (__FILE__
, __LINE__
,
147 "Type \"%s\" has an unknown kind %d",
148 e
->name
.c_str (), e
->kind
);
151 void visit (const tdesc_type_vector
*e
) override
153 m_type
= tdesc_find_type (m_gdbarch
, e
->name
.c_str ());
157 type
*element_gdb_type
= make_gdb_type (m_gdbarch
, e
->element_type
);
158 m_type
= init_vector_type (element_gdb_type
, e
->count
);
159 TYPE_NAME (m_type
) = xstrdup (e
->name
.c_str ());
163 void visit (const tdesc_type_with_fields
*e
) override
165 m_type
= tdesc_find_type (m_gdbarch
, e
->name
.c_str ());
171 case TDESC_TYPE_STRUCT
:
172 make_gdb_type_struct (e
);
174 case TDESC_TYPE_UNION
:
175 make_gdb_type_union (e
);
177 case TDESC_TYPE_FLAGS
:
178 make_gdb_type_flags (e
);
180 case TDESC_TYPE_ENUM
:
181 make_gdb_type_enum (e
);
185 internal_error (__FILE__
, __LINE__
,
186 "Type \"%s\" has an unknown kind %d",
187 e
->name
.c_str (), e
->kind
);
192 void make_gdb_type_struct (const tdesc_type_with_fields
*e
)
194 m_type
= arch_composite_type (m_gdbarch
, NULL
, TYPE_CODE_STRUCT
);
195 TYPE_NAME (m_type
) = xstrdup (e
->name
.c_str ());
197 for (const tdesc_type_field
&f
: e
->fields
)
199 if (f
.start
!= -1 && f
.end
!= -1)
203 struct type
*field_gdb_type
;
204 int bitsize
, total_size
;
206 /* This invariant should be preserved while creating types. */
207 gdb_assert (e
->size
!= 0);
209 field_gdb_type
= make_gdb_type (m_gdbarch
, f
.type
);
210 else if (e
->size
> 4)
211 field_gdb_type
= builtin_type (m_gdbarch
)->builtin_uint64
;
213 field_gdb_type
= builtin_type (m_gdbarch
)->builtin_uint32
;
215 fld
= append_composite_type_field_raw
216 (m_type
, xstrdup (f
.name
.c_str ()), field_gdb_type
);
218 /* For little-endian, BITPOS counts from the LSB of
219 the structure and marks the LSB of the field. For
220 big-endian, BITPOS counts from the MSB of the
221 structure and marks the MSB of the field. Either
222 way, it is the number of bits to the "left" of the
223 field. To calculate this in big-endian, we need
224 the total size of the structure. */
225 bitsize
= f
.end
- f
.start
+ 1;
226 total_size
= e
->size
* TARGET_CHAR_BIT
;
227 if (gdbarch_byte_order (m_gdbarch
) == BFD_ENDIAN_BIG
)
228 SET_FIELD_BITPOS (fld
[0], total_size
- f
.start
- bitsize
);
230 SET_FIELD_BITPOS (fld
[0], f
.start
);
231 FIELD_BITSIZE (fld
[0]) = bitsize
;
235 gdb_assert (f
.start
== -1 && f
.end
== -1);
236 type
*field_gdb_type
= make_gdb_type (m_gdbarch
, f
.type
);
237 append_composite_type_field (m_type
,
238 xstrdup (f
.name
.c_str ()),
244 TYPE_LENGTH (m_type
) = e
->size
;
247 void make_gdb_type_union (const tdesc_type_with_fields
*e
)
249 m_type
= arch_composite_type (m_gdbarch
, NULL
, TYPE_CODE_UNION
);
250 TYPE_NAME (m_type
) = xstrdup (e
->name
.c_str ());
252 for (const tdesc_type_field
&f
: e
->fields
)
254 type
* field_gdb_type
= make_gdb_type (m_gdbarch
, f
.type
);
255 append_composite_type_field (m_type
, xstrdup (f
.name
.c_str ()),
258 /* If any of the children of a union are vectors, flag the
259 union as a vector also. This allows e.g. a union of two
260 vector types to show up automatically in "info vector". */
261 if (TYPE_VECTOR (field_gdb_type
))
262 TYPE_VECTOR (m_type
) = 1;
266 void make_gdb_type_flags (const tdesc_type_with_fields
*e
)
268 m_type
= arch_flags_type (m_gdbarch
, e
->name
.c_str (),
269 e
->size
* TARGET_CHAR_BIT
);
271 for (const tdesc_type_field
&f
: e
->fields
)
273 int bitsize
= f
.end
- f
.start
+ 1;
275 gdb_assert (f
.type
!= NULL
);
276 type
*field_gdb_type
= make_gdb_type (m_gdbarch
, f
.type
);
277 append_flags_type_field (m_type
, f
.start
, bitsize
,
278 field_gdb_type
, f
.name
.c_str ());
282 void make_gdb_type_enum (const tdesc_type_with_fields
*e
)
284 m_type
= arch_type (m_gdbarch
, TYPE_CODE_ENUM
, e
->size
* TARGET_CHAR_BIT
,
287 TYPE_UNSIGNED (m_type
) = 1;
288 for (const tdesc_type_field
&f
: e
->fields
)
291 = append_composite_type_field_raw (m_type
,
292 xstrdup (f
.name
.c_str ()),
295 SET_FIELD_BITPOS (fld
[0], f
.start
);
299 /* The gdbarch used. */
300 struct gdbarch
*m_gdbarch
;
302 /* The type created. */
306 gdb_type_creator
gdb_type (gdbarch
);
307 ttype
->accept (gdb_type
);
308 return gdb_type
.get_type ();
311 /* A target description. */
313 struct target_desc
: tdesc_element
318 virtual ~target_desc () = default;
320 target_desc (const target_desc
&) = delete;
321 void operator= (const target_desc
&) = delete;
323 /* The architecture reported by the target, if any. */
324 const struct bfd_arch_info
*arch
= NULL
;
326 /* The osabi reported by the target, if any; GDB_OSABI_UNKNOWN
328 enum gdb_osabi osabi
= GDB_OSABI_UNKNOWN
;
330 /* The list of compatible architectures reported by the target. */
331 std::vector
<const bfd_arch_info
*> compatible
;
333 /* Any architecture-specific properties specified by the target. */
334 std::vector
<property
> properties
;
336 /* The features associated with this target. */
337 std::vector
<tdesc_feature_up
> features
;
339 /* Used to cache the generated xml version of the target description. */
340 mutable char *xmltarget
= nullptr;
342 void accept (tdesc_element_visitor
&v
) const override
346 for (const tdesc_feature_up
&feature
: features
)
352 bool operator== (const target_desc
&other
) const
354 if (arch
!= other
.arch
)
357 if (osabi
!= other
.osabi
)
360 if (features
.size () != other
.features
.size ())
363 for (int ix
= 0; ix
< features
.size (); ix
++)
365 const tdesc_feature_up
&feature1
= features
[ix
];
366 const tdesc_feature_up
&feature2
= other
.features
[ix
];
368 if (feature1
!= feature2
&& *feature1
!= *feature2
)
375 bool operator!= (const target_desc
&other
) const
377 return !(*this == other
);
381 /* Per-architecture data associated with a target description. The
382 target description may be shared by multiple architectures, but
383 this data is private to one gdbarch. */
385 struct tdesc_arch_reg
387 tdesc_arch_reg (tdesc_reg
*reg_
, struct type
*type_
)
388 : reg (reg_
), type (type_
)
391 struct tdesc_reg
*reg
;
395 struct tdesc_arch_data
397 /* A list of register/type pairs, indexed by GDB's internal register number.
398 During initialization of the gdbarch this list is used to store
399 registers which the architecture assigns a fixed register number.
400 Registers which are NULL in this array, or off the end, are
401 treated as zero-sized and nameless (i.e. placeholders in the
403 std::vector
<tdesc_arch_reg
> arch_regs
;
405 /* Functions which report the register name, type, and reggroups for
407 gdbarch_register_name_ftype
*pseudo_register_name
= NULL
;
408 gdbarch_register_type_ftype
*pseudo_register_type
= NULL
;
409 gdbarch_register_reggroup_p_ftype
*pseudo_register_reggroup_p
= NULL
;
412 /* Info about an inferior's target description. There's one of these
413 for each inferior. */
415 struct target_desc_info
417 /* A flag indicating that a description has already been fetched
418 from the target, so it should not be queried again. */
422 /* The description fetched from the target, or NULL if the target
423 did not supply any description. Only valid when
424 target_desc_fetched is set. Only the description initialization
425 code should access this; normally, the description should be
426 accessed through the gdbarch object. */
428 const struct target_desc
*tdesc
;
430 /* The filename to read a target description from, as set by "set
431 tdesc filename ..." */
436 /* Get the inferior INF's target description info, allocating one on
437 the stop if necessary. */
439 static struct target_desc_info
*
440 get_tdesc_info (struct inferior
*inf
)
442 if (inf
->tdesc_info
== NULL
)
443 inf
->tdesc_info
= XCNEW (struct target_desc_info
);
444 return inf
->tdesc_info
;
447 /* A handle for architecture-specific data associated with the
448 target description (see struct tdesc_arch_data). */
450 static struct gdbarch_data
*tdesc_data
;
452 /* See target-descriptions.h. */
455 target_desc_info_from_user_p (struct target_desc_info
*info
)
457 return info
!= NULL
&& info
->filename
!= NULL
;
460 /* See target-descriptions.h. */
463 copy_inferior_target_desc_info (struct inferior
*destinf
, struct inferior
*srcinf
)
465 struct target_desc_info
*src
= get_tdesc_info (srcinf
);
466 struct target_desc_info
*dest
= get_tdesc_info (destinf
);
468 dest
->fetched
= src
->fetched
;
469 dest
->tdesc
= src
->tdesc
;
470 dest
->filename
= src
->filename
!= NULL
? xstrdup (src
->filename
) : NULL
;
473 /* See target-descriptions.h. */
476 target_desc_info_free (struct target_desc_info
*tdesc_info
)
478 if (tdesc_info
!= NULL
)
480 xfree (tdesc_info
->filename
);
485 /* Convenience helper macros. */
487 #define target_desc_fetched \
488 get_tdesc_info (current_inferior ())->fetched
489 #define current_target_desc \
490 get_tdesc_info (current_inferior ())->tdesc
491 #define target_description_filename \
492 get_tdesc_info (current_inferior ())->filename
494 /* The string manipulated by the "set tdesc filename ..." command. */
496 static char *tdesc_filename_cmd_string
;
498 /* Fetch the current target's description, and switch the current
499 architecture to one which incorporates that description. */
502 target_find_description (void)
504 /* If we've already fetched a description from the target, don't do
505 it again. This allows a target to fetch the description early,
506 during its to_open or to_create_inferior, if it needs extra
507 information about the target to initialize. */
508 if (target_desc_fetched
)
511 /* The current architecture should not have any target description
512 specified. It should have been cleared, e.g. when we
513 disconnected from the previous target. */
514 gdb_assert (gdbarch_target_desc (target_gdbarch ()) == NULL
);
516 /* First try to fetch an XML description from the user-specified
518 current_target_desc
= NULL
;
519 if (target_description_filename
!= NULL
520 && *target_description_filename
!= '\0')
522 = file_read_description_xml (target_description_filename
);
524 /* Next try to read the description from the current target using
526 if (current_target_desc
== NULL
)
527 current_target_desc
= target_read_description_xml (current_top_target ());
529 /* If that failed try a target-specific hook. */
530 if (current_target_desc
== NULL
)
531 current_target_desc
= target_read_description (current_top_target ());
533 /* If a non-NULL description was returned, then update the current
535 if (current_target_desc
)
537 struct gdbarch_info info
;
539 gdbarch_info_init (&info
);
540 info
.target_desc
= current_target_desc
;
541 if (!gdbarch_update_p (info
))
542 warning (_("Architecture rejected target-supplied description"));
545 struct tdesc_arch_data
*data
;
547 data
= ((struct tdesc_arch_data
*)
548 gdbarch_data (target_gdbarch (), tdesc_data
));
549 if (tdesc_has_registers (current_target_desc
)
550 && data
->arch_regs
.empty ())
551 warning (_("Target-supplied registers are not supported "
552 "by the current architecture"));
556 /* Now that we know this description is usable, record that we
558 target_desc_fetched
= 1;
561 /* Discard any description fetched from the current target, and switch
562 the current architecture to one with no target description. */
565 target_clear_description (void)
567 struct gdbarch_info info
;
569 if (!target_desc_fetched
)
572 target_desc_fetched
= 0;
573 current_target_desc
= NULL
;
575 gdbarch_info_init (&info
);
576 if (!gdbarch_update_p (info
))
577 internal_error (__FILE__
, __LINE__
,
578 _("Could not remove target-supplied description"));
581 /* Return the global current target description. This should only be
582 used by gdbarch initialization code; most access should be through
583 an existing gdbarch. */
585 const struct target_desc
*
586 target_current_description (void)
588 if (target_desc_fetched
)
589 return current_target_desc
;
594 /* Return non-zero if this target description is compatible
595 with the given BFD architecture. */
598 tdesc_compatible_p (const struct target_desc
*target_desc
,
599 const struct bfd_arch_info
*arch
)
601 for (const bfd_arch_info
*compat
: target_desc
->compatible
)
604 || arch
->compatible (arch
, compat
)
605 || compat
->compatible (compat
, arch
))
613 /* Direct accessors for target descriptions. */
615 /* Return the string value of a property named KEY, or NULL if the
616 property was not specified. */
619 tdesc_property (const struct target_desc
*target_desc
, const char *key
)
621 for (const property
&prop
: target_desc
->properties
)
623 return prop
.value
.c_str ();
628 /* Return the BFD architecture associated with this target
629 description, or NULL if no architecture was specified. */
631 const struct bfd_arch_info
*
632 tdesc_architecture (const struct target_desc
*target_desc
)
634 return target_desc
->arch
;
637 /* See gdbsupport/tdesc.h. */
640 tdesc_architecture_name (const struct target_desc
*target_desc
)
642 return target_desc
->arch
->printable_name
;
645 /* Return the OSABI associated with this target description, or
646 GDB_OSABI_UNKNOWN if no osabi was specified. */
649 tdesc_osabi (const struct target_desc
*target_desc
)
651 return target_desc
->osabi
;
654 /* See gdbsupport/tdesc.h. */
657 tdesc_osabi_name (const struct target_desc
*target_desc
)
659 enum gdb_osabi osabi
= tdesc_osabi (target_desc
);
660 if (osabi
> GDB_OSABI_UNKNOWN
&& osabi
< GDB_OSABI_INVALID
)
661 return gdbarch_osabi_name (osabi
);
665 /* Return 1 if this target description includes any registers. */
668 tdesc_has_registers (const struct target_desc
*target_desc
)
670 if (target_desc
== NULL
)
673 for (const tdesc_feature_up
&feature
: target_desc
->features
)
674 if (!feature
->registers
.empty ())
680 /* Return the feature with the given name, if present, or NULL if
681 the named feature is not found. */
683 const struct tdesc_feature
*
684 tdesc_find_feature (const struct target_desc
*target_desc
,
687 for (const tdesc_feature_up
&feature
: target_desc
->features
)
688 if (feature
->name
== name
)
689 return feature
.get ();
694 /* Return the name of FEATURE. */
697 tdesc_feature_name (const struct tdesc_feature
*feature
)
699 return feature
->name
.c_str ();
702 /* Lookup type associated with ID. */
705 tdesc_find_type (struct gdbarch
*gdbarch
, const char *id
)
707 tdesc_arch_data
*data
708 = (struct tdesc_arch_data
*) gdbarch_data (gdbarch
, tdesc_data
);
710 for (const tdesc_arch_reg
®
: data
->arch_regs
)
713 && reg
.reg
->tdesc_type
715 && reg
.reg
->tdesc_type
->name
== id
)
722 /* Support for registers from target descriptions. */
724 /* Construct the per-gdbarch data. */
727 tdesc_data_init (struct obstack
*obstack
)
729 return obstack_new
<tdesc_arch_data
> (obstack
);
732 /* Similar, but for the temporary copy used during architecture
735 struct tdesc_arch_data
*
736 tdesc_data_alloc (void)
738 return new tdesc_arch_data ();
741 /* Free something allocated by tdesc_data_alloc, if it is not going
742 to be used (for instance if it was unsuitable for the
746 tdesc_data_cleanup (void *data_untyped
)
748 struct tdesc_arch_data
*data
= (struct tdesc_arch_data
*) data_untyped
;
753 /* Search FEATURE for a register named NAME. */
755 static struct tdesc_reg
*
756 tdesc_find_register_early (const struct tdesc_feature
*feature
,
759 for (const tdesc_reg_up
®
: feature
->registers
)
760 if (strcasecmp (reg
->name
.c_str (), name
) == 0)
766 /* Search FEATURE for a register named NAME. Assign REGNO to it. */
769 tdesc_numbered_register (const struct tdesc_feature
*feature
,
770 struct tdesc_arch_data
*data
,
771 int regno
, const char *name
)
773 struct tdesc_reg
*reg
= tdesc_find_register_early (feature
, name
);
778 /* Make sure the vector includes a REGNO'th element. */
779 while (regno
>= data
->arch_regs
.size ())
780 data
->arch_regs
.emplace_back (nullptr, nullptr);
782 data
->arch_regs
[regno
] = tdesc_arch_reg (reg
, NULL
);
787 /* Search FEATURE for a register named NAME, but do not assign a fixed
788 register number to it. */
791 tdesc_unnumbered_register (const struct tdesc_feature
*feature
,
794 struct tdesc_reg
*reg
= tdesc_find_register_early (feature
, name
);
802 /* Search FEATURE for a register whose name is in NAMES and assign
806 tdesc_numbered_register_choices (const struct tdesc_feature
*feature
,
807 struct tdesc_arch_data
*data
,
808 int regno
, const char *const names
[])
812 for (i
= 0; names
[i
] != NULL
; i
++)
813 if (tdesc_numbered_register (feature
, data
, regno
, names
[i
]))
819 /* Search FEATURE for a register named NAME, and return its size in
820 bits. The register must exist. */
823 tdesc_register_bitsize (const struct tdesc_feature
*feature
, const char *name
)
825 struct tdesc_reg
*reg
= tdesc_find_register_early (feature
, name
);
827 gdb_assert (reg
!= NULL
);
831 /* Look up a register by its GDB internal register number. */
833 static struct tdesc_arch_reg
*
834 tdesc_find_arch_register (struct gdbarch
*gdbarch
, int regno
)
836 struct tdesc_arch_data
*data
;
838 data
= (struct tdesc_arch_data
*) gdbarch_data (gdbarch
, tdesc_data
);
839 if (regno
< data
->arch_regs
.size ())
840 return &data
->arch_regs
[regno
];
845 static struct tdesc_reg
*
846 tdesc_find_register (struct gdbarch
*gdbarch
, int regno
)
848 struct tdesc_arch_reg
*reg
= tdesc_find_arch_register (gdbarch
, regno
);
850 return reg
? reg
->reg
: NULL
;
853 /* Return the name of register REGNO, from the target description or
854 from an architecture-provided pseudo_register_name method. */
857 tdesc_register_name (struct gdbarch
*gdbarch
, int regno
)
859 struct tdesc_reg
*reg
= tdesc_find_register (gdbarch
, regno
);
860 int num_regs
= gdbarch_num_regs (gdbarch
);
863 return reg
->name
.c_str ();
865 if (regno
>= num_regs
&& regno
< gdbarch_num_cooked_regs (gdbarch
))
867 struct tdesc_arch_data
*data
868 = (struct tdesc_arch_data
*) gdbarch_data (gdbarch
, tdesc_data
);
870 gdb_assert (data
->pseudo_register_name
!= NULL
);
871 return data
->pseudo_register_name (gdbarch
, regno
);
878 tdesc_register_type (struct gdbarch
*gdbarch
, int regno
)
880 struct tdesc_arch_reg
*arch_reg
= tdesc_find_arch_register (gdbarch
, regno
);
881 struct tdesc_reg
*reg
= arch_reg
? arch_reg
->reg
: NULL
;
882 int num_regs
= gdbarch_num_regs (gdbarch
);
883 int num_pseudo_regs
= gdbarch_num_pseudo_regs (gdbarch
);
885 if (reg
== NULL
&& regno
>= num_regs
&& regno
< num_regs
+ num_pseudo_regs
)
887 struct tdesc_arch_data
*data
888 = (struct tdesc_arch_data
*) gdbarch_data (gdbarch
, tdesc_data
);
890 gdb_assert (data
->pseudo_register_type
!= NULL
);
891 return data
->pseudo_register_type (gdbarch
, regno
);
895 /* Return "int0_t", since "void" has a misleading size of one. */
896 return builtin_type (gdbarch
)->builtin_int0
;
898 if (arch_reg
->type
== NULL
)
900 /* First check for a predefined or target defined type. */
902 arch_reg
->type
= make_gdb_type (gdbarch
, reg
->tdesc_type
);
904 /* Next try size-sensitive type shortcuts. */
905 else if (reg
->type
== "float")
907 if (reg
->bitsize
== gdbarch_float_bit (gdbarch
))
908 arch_reg
->type
= builtin_type (gdbarch
)->builtin_float
;
909 else if (reg
->bitsize
== gdbarch_double_bit (gdbarch
))
910 arch_reg
->type
= builtin_type (gdbarch
)->builtin_double
;
911 else if (reg
->bitsize
== gdbarch_long_double_bit (gdbarch
))
912 arch_reg
->type
= builtin_type (gdbarch
)->builtin_long_double
;
915 warning (_("Register \"%s\" has an unsupported size (%d bits)"),
916 reg
->name
.c_str (), reg
->bitsize
);
917 arch_reg
->type
= builtin_type (gdbarch
)->builtin_double
;
920 else if (reg
->type
== "int")
922 if (reg
->bitsize
== gdbarch_long_bit (gdbarch
))
923 arch_reg
->type
= builtin_type (gdbarch
)->builtin_long
;
924 else if (reg
->bitsize
== TARGET_CHAR_BIT
)
925 arch_reg
->type
= builtin_type (gdbarch
)->builtin_char
;
926 else if (reg
->bitsize
== gdbarch_short_bit (gdbarch
))
927 arch_reg
->type
= builtin_type (gdbarch
)->builtin_short
;
928 else if (reg
->bitsize
== gdbarch_int_bit (gdbarch
))
929 arch_reg
->type
= builtin_type (gdbarch
)->builtin_int
;
930 else if (reg
->bitsize
== gdbarch_long_long_bit (gdbarch
))
931 arch_reg
->type
= builtin_type (gdbarch
)->builtin_long_long
;
932 else if (reg
->bitsize
== gdbarch_ptr_bit (gdbarch
))
933 /* A bit desperate by this point... */
934 arch_reg
->type
= builtin_type (gdbarch
)->builtin_data_ptr
;
937 warning (_("Register \"%s\" has an unsupported size (%d bits)"),
938 reg
->name
.c_str (), reg
->bitsize
);
939 arch_reg
->type
= builtin_type (gdbarch
)->builtin_long
;
943 if (arch_reg
->type
== NULL
)
944 internal_error (__FILE__
, __LINE__
,
945 "Register \"%s\" has an unknown type \"%s\"",
946 reg
->name
.c_str (), reg
->type
.c_str ());
949 return arch_reg
->type
;
953 tdesc_remote_register_number (struct gdbarch
*gdbarch
, int regno
)
955 struct tdesc_reg
*reg
= tdesc_find_register (gdbarch
, regno
);
958 return reg
->target_regnum
;
963 /* Check whether REGNUM is a member of REGGROUP. Registers from the
964 target description may be classified as general, float, vector or other
965 register groups registered with reggroup_add(). Unlike a gdbarch
966 register_reggroup_p method, this function will return -1 if it does not
967 know; the caller should handle registers with no specified group.
969 The names of containing features are not used. This might be extended
970 to display registers in some more useful groupings.
972 The save-restore flag is also implemented here. */
975 tdesc_register_in_reggroup_p (struct gdbarch
*gdbarch
, int regno
,
976 struct reggroup
*reggroup
)
978 struct tdesc_reg
*reg
= tdesc_find_register (gdbarch
, regno
);
980 if (reg
!= NULL
&& !reg
->group
.empty ()
981 && (reg
->group
== reggroup_name (reggroup
)))
985 && (reggroup
== save_reggroup
|| reggroup
== restore_reggroup
))
986 return reg
->save_restore
;
991 /* Check whether REGNUM is a member of REGGROUP. Registers with no
992 group specified go to the default reggroup function and are handled
996 tdesc_register_reggroup_p (struct gdbarch
*gdbarch
, int regno
,
997 struct reggroup
*reggroup
)
999 int num_regs
= gdbarch_num_regs (gdbarch
);
1000 int num_pseudo_regs
= gdbarch_num_pseudo_regs (gdbarch
);
1003 if (regno
>= num_regs
&& regno
< num_regs
+ num_pseudo_regs
)
1005 struct tdesc_arch_data
*data
1006 = (struct tdesc_arch_data
*) gdbarch_data (gdbarch
, tdesc_data
);
1008 if (data
->pseudo_register_reggroup_p
!= NULL
)
1009 return data
->pseudo_register_reggroup_p (gdbarch
, regno
, reggroup
);
1010 /* Otherwise fall through to the default reggroup_p. */
1013 ret
= tdesc_register_in_reggroup_p (gdbarch
, regno
, reggroup
);
1017 return default_register_reggroup_p (gdbarch
, regno
, reggroup
);
1020 /* Record architecture-specific functions to call for pseudo-register
1024 set_tdesc_pseudo_register_name (struct gdbarch
*gdbarch
,
1025 gdbarch_register_name_ftype
*pseudo_name
)
1027 struct tdesc_arch_data
*data
1028 = (struct tdesc_arch_data
*) gdbarch_data (gdbarch
, tdesc_data
);
1030 data
->pseudo_register_name
= pseudo_name
;
1034 set_tdesc_pseudo_register_type (struct gdbarch
*gdbarch
,
1035 gdbarch_register_type_ftype
*pseudo_type
)
1037 struct tdesc_arch_data
*data
1038 = (struct tdesc_arch_data
*) gdbarch_data (gdbarch
, tdesc_data
);
1040 data
->pseudo_register_type
= pseudo_type
;
1044 set_tdesc_pseudo_register_reggroup_p
1045 (struct gdbarch
*gdbarch
,
1046 gdbarch_register_reggroup_p_ftype
*pseudo_reggroup_p
)
1048 struct tdesc_arch_data
*data
1049 = (struct tdesc_arch_data
*) gdbarch_data (gdbarch
, tdesc_data
);
1051 data
->pseudo_register_reggroup_p
= pseudo_reggroup_p
;
1054 /* Update GDBARCH to use the target description for registers. */
1057 tdesc_use_registers (struct gdbarch
*gdbarch
,
1058 const struct target_desc
*target_desc
,
1059 struct tdesc_arch_data
*early_data
)
1061 int num_regs
= gdbarch_num_regs (gdbarch
);
1062 struct tdesc_arch_data
*data
;
1065 /* We can't use the description for registers if it doesn't describe
1066 any. This function should only be called after validating
1067 registers, so the caller should know that registers are
1069 gdb_assert (tdesc_has_registers (target_desc
));
1071 data
= (struct tdesc_arch_data
*) gdbarch_data (gdbarch
, tdesc_data
);
1072 data
->arch_regs
= early_data
->arch_regs
;
1075 /* Build up a set of all registers, so that we can assign register
1076 numbers where needed. The hash table expands as necessary, so
1077 the initial size is arbitrary. */
1078 reg_hash
= htab_create (37, htab_hash_pointer
, htab_eq_pointer
, NULL
);
1079 for (const tdesc_feature_up
&feature
: target_desc
->features
)
1080 for (const tdesc_reg_up
®
: feature
->registers
)
1082 void **slot
= htab_find_slot (reg_hash
, reg
.get (), INSERT
);
1085 /* Add reggroup if its new. */
1086 if (!reg
->group
.empty ())
1087 if (reggroup_find (gdbarch
, reg
->group
.c_str ()) == NULL
)
1088 reggroup_add (gdbarch
, reggroup_gdbarch_new (gdbarch
,
1089 reg
->group
.c_str (),
1093 /* Remove any registers which were assigned numbers by the
1095 for (const tdesc_arch_reg
&arch_reg
: data
->arch_regs
)
1096 if (arch_reg
.reg
!= NULL
)
1097 htab_remove_elt (reg_hash
, arch_reg
.reg
);
1099 /* Assign numbers to the remaining registers and add them to the
1100 list of registers. The new numbers are always above gdbarch_num_regs.
1101 Iterate over the features, not the hash table, so that the order
1102 matches that in the target description. */
1104 gdb_assert (data
->arch_regs
.size () <= num_regs
);
1105 while (data
->arch_regs
.size () < num_regs
)
1106 data
->arch_regs
.emplace_back (nullptr, nullptr);
1108 for (const tdesc_feature_up
&feature
: target_desc
->features
)
1109 for (const tdesc_reg_up
®
: feature
->registers
)
1110 if (htab_find (reg_hash
, reg
.get ()) != NULL
)
1112 data
->arch_regs
.emplace_back (reg
.get (), nullptr);
1116 htab_delete (reg_hash
);
1118 /* Update the architecture. */
1119 set_gdbarch_num_regs (gdbarch
, num_regs
);
1120 set_gdbarch_register_name (gdbarch
, tdesc_register_name
);
1121 set_gdbarch_register_type (gdbarch
, tdesc_register_type
);
1122 set_gdbarch_remote_register_number (gdbarch
,
1123 tdesc_remote_register_number
);
1124 set_gdbarch_register_reggroup_p (gdbarch
, tdesc_register_reggroup_p
);
1127 /* See gdbsupport/tdesc.h. */
1129 struct tdesc_feature
*
1130 tdesc_create_feature (struct target_desc
*tdesc
, const char *name
)
1132 struct tdesc_feature
*new_feature
= new tdesc_feature (name
);
1134 tdesc
->features
.emplace_back (new_feature
);
1139 struct target_desc
*
1140 allocate_target_description (void)
1142 return new target_desc ();
1146 target_desc_deleter::operator() (struct target_desc
*target_desc
) const
1152 tdesc_add_compatible (struct target_desc
*target_desc
,
1153 const struct bfd_arch_info
*compatible
)
1155 /* If this instance of GDB is compiled without BFD support for the
1156 compatible architecture, simply ignore it -- we would not be able
1157 to handle it anyway. */
1158 if (compatible
== NULL
)
1161 for (const bfd_arch_info
*compat
: target_desc
->compatible
)
1162 if (compat
== compatible
)
1163 internal_error (__FILE__
, __LINE__
,
1164 _("Attempted to add duplicate "
1165 "compatible architecture \"%s\""),
1166 compatible
->printable_name
);
1168 target_desc
->compatible
.push_back (compatible
);
1172 set_tdesc_property (struct target_desc
*target_desc
,
1173 const char *key
, const char *value
)
1175 gdb_assert (key
!= NULL
&& value
!= NULL
);
1177 if (tdesc_property (target_desc
, key
) != NULL
)
1178 internal_error (__FILE__
, __LINE__
,
1179 _("Attempted to add duplicate property \"%s\""), key
);
1181 target_desc
->properties
.emplace_back (key
, value
);
1184 /* See gdbsupport/tdesc.h. */
1187 set_tdesc_architecture (struct target_desc
*target_desc
,
1190 set_tdesc_architecture (target_desc
, bfd_scan_arch (name
));
1194 set_tdesc_architecture (struct target_desc
*target_desc
,
1195 const struct bfd_arch_info
*arch
)
1197 target_desc
->arch
= arch
;
1200 /* See gdbsupport/tdesc.h. */
1203 set_tdesc_osabi (struct target_desc
*target_desc
, const char *name
)
1205 set_tdesc_osabi (target_desc
, osabi_from_tdesc_string (name
));
1209 set_tdesc_osabi (struct target_desc
*target_desc
, enum gdb_osabi osabi
)
1211 target_desc
->osabi
= osabi
;
1215 static struct cmd_list_element
*tdesc_set_cmdlist
, *tdesc_show_cmdlist
;
1216 static struct cmd_list_element
*tdesc_unset_cmdlist
;
1218 /* Helper functions for the CLI commands. */
1221 set_tdesc_cmd (const char *args
, int from_tty
)
1223 help_list (tdesc_set_cmdlist
, "set tdesc ", all_commands
, gdb_stdout
);
1227 show_tdesc_cmd (const char *args
, int from_tty
)
1229 cmd_show_list (tdesc_show_cmdlist
, from_tty
, "");
1233 unset_tdesc_cmd (const char *args
, int from_tty
)
1235 help_list (tdesc_unset_cmdlist
, "unset tdesc ", all_commands
, gdb_stdout
);
1239 set_tdesc_filename_cmd (const char *args
, int from_tty
,
1240 struct cmd_list_element
*c
)
1242 xfree (target_description_filename
);
1243 target_description_filename
= xstrdup (tdesc_filename_cmd_string
);
1245 target_clear_description ();
1246 target_find_description ();
1250 show_tdesc_filename_cmd (struct ui_file
*file
, int from_tty
,
1251 struct cmd_list_element
*c
,
1254 value
= target_description_filename
;
1256 if (value
!= NULL
&& *value
!= '\0')
1257 printf_filtered (_("The target description will be read from \"%s\".\n"),
1260 printf_filtered (_("The target description will be "
1261 "read from the target.\n"));
1265 unset_tdesc_filename_cmd (const char *args
, int from_tty
)
1267 xfree (target_description_filename
);
1268 target_description_filename
= NULL
;
1269 target_clear_description ();
1270 target_find_description ();
1273 /* Print target description in C. */
1275 class print_c_tdesc
: public tdesc_element_visitor
1278 print_c_tdesc (std::string
&filename_after_features
)
1279 : m_filename_after_features (filename_after_features
)
1283 const char *filename
= lbasename (m_filename_after_features
.c_str ());
1285 m_function
= (char *) xmalloc (strlen (filename
) + 1);
1286 for (inp
= filename
, outp
= m_function
; *inp
!= '\0'; inp
++)
1289 else if (*inp
== '-')
1295 /* Standard boilerplate. */
1296 printf_unfiltered ("/* THIS FILE IS GENERATED. "
1297 "-*- buffer-read-only: t -*- vi"
1306 void visit_pre (const target_desc
*e
) override
1308 printf_unfiltered (" Original: %s */\n\n",
1309 lbasename (m_filename_after_features
.c_str ()));
1311 printf_unfiltered ("#include \"defs.h\"\n");
1312 printf_unfiltered ("#include \"osabi.h\"\n");
1313 printf_unfiltered ("#include \"target-descriptions.h\"\n");
1314 printf_unfiltered ("\n");
1316 printf_unfiltered ("struct target_desc *tdesc_%s;\n", m_function
);
1317 printf_unfiltered ("static void\n");
1318 printf_unfiltered ("initialize_tdesc_%s (void)\n", m_function
);
1319 printf_unfiltered ("{\n");
1321 (" struct target_desc *result = allocate_target_description ();\n");
1323 if (tdesc_architecture (e
) != NULL
)
1326 (" set_tdesc_architecture (result, bfd_scan_arch (\"%s\"));\n",
1327 tdesc_architecture (e
)->printable_name
);
1328 printf_unfiltered ("\n");
1330 if (tdesc_osabi (e
) > GDB_OSABI_UNKNOWN
1331 && tdesc_osabi (e
) < GDB_OSABI_INVALID
)
1334 (" set_tdesc_osabi (result, osabi_from_tdesc_string (\"%s\"));\n",
1335 gdbarch_osabi_name (tdesc_osabi (e
)));
1336 printf_unfiltered ("\n");
1339 for (const bfd_arch_info_type
*compatible
: e
->compatible
)
1341 (" tdesc_add_compatible (result, bfd_scan_arch (\"%s\"));\n",
1342 compatible
->printable_name
);
1344 if (!e
->compatible
.empty ())
1345 printf_unfiltered ("\n");
1347 for (const property
&prop
: e
->properties
)
1348 printf_unfiltered (" set_tdesc_property (result, \"%s\", \"%s\");\n",
1349 prop
.key
.c_str (), prop
.value
.c_str ());
1351 printf_unfiltered (" struct tdesc_feature *feature;\n");
1354 void visit_pre (const tdesc_feature
*e
) override
1356 printf_unfiltered ("\n feature = tdesc_create_feature (result, \"%s\");\n",
1360 void visit_post (const tdesc_feature
*e
) override
1363 void visit_post (const target_desc
*e
) override
1365 printf_unfiltered ("\n tdesc_%s = result;\n", m_function
);
1366 printf_unfiltered ("}\n");
1369 void visit (const tdesc_type_builtin
*type
) override
1371 error (_("C output is not supported type \"%s\"."), type
->name
.c_str ());
1374 void visit (const tdesc_type_vector
*type
) override
1376 if (!m_printed_element_type
)
1378 printf_unfiltered (" tdesc_type *element_type;\n");
1379 m_printed_element_type
= true;
1383 (" element_type = tdesc_named_type (feature, \"%s\");\n",
1384 type
->element_type
->name
.c_str ());
1386 (" tdesc_create_vector (feature, \"%s\", element_type, %d);\n",
1387 type
->name
.c_str (), type
->count
);
1389 printf_unfiltered ("\n");
1392 void visit (const tdesc_type_with_fields
*type
) override
1394 if (!m_printed_type_with_fields
)
1396 printf_unfiltered (" tdesc_type_with_fields *type_with_fields;\n");
1397 m_printed_type_with_fields
= true;
1402 case TDESC_TYPE_STRUCT
:
1403 case TDESC_TYPE_FLAGS
:
1404 if (type
->kind
== TDESC_TYPE_STRUCT
)
1407 (" type_with_fields = tdesc_create_struct (feature, \"%s\");\n",
1408 type
->name
.c_str ());
1409 if (type
->size
!= 0)
1411 (" tdesc_set_struct_size (type_with_fields, %d);\n", type
->size
);
1416 (" type_with_fields = tdesc_create_flags (feature, \"%s\", %d);\n",
1417 type
->name
.c_str (), type
->size
);
1419 for (const tdesc_type_field
&f
: type
->fields
)
1421 const char *type_name
;
1423 gdb_assert (f
.type
!= NULL
);
1424 type_name
= f
.type
->name
.c_str ();
1426 /* To minimize changes to generated files, don't emit type
1427 info for fields that have defaulted types. */
1430 gdb_assert (f
.end
!= -1);
1431 if (f
.type
->kind
== TDESC_TYPE_BOOL
)
1433 gdb_assert (f
.start
== f
.end
);
1435 (" tdesc_add_flag (type_with_fields, %d, \"%s\");\n",
1436 f
.start
, f
.name
.c_str ());
1438 else if ((type
->size
== 4 && f
.type
->kind
== TDESC_TYPE_UINT32
)
1440 && f
.type
->kind
== TDESC_TYPE_UINT64
))
1443 (" tdesc_add_bitfield (type_with_fields, \"%s\", %d, %d);\n",
1444 f
.name
.c_str (), f
.start
, f
.end
);
1448 printf_field_type_assignment
1449 ("tdesc_named_type (feature, \"%s\");\n",
1452 (" tdesc_add_typed_bitfield (type_with_fields, \"%s\","
1453 " %d, %d, field_type);\n",
1454 f
.name
.c_str (), f
.start
, f
.end
);
1457 else /* Not a bitfield. */
1459 gdb_assert (f
.end
== -1);
1460 gdb_assert (type
->kind
== TDESC_TYPE_STRUCT
);
1461 printf_field_type_assignment
1462 ("tdesc_named_type (feature, \"%s\");\n", type_name
);
1464 (" tdesc_add_field (type_with_fields, \"%s\", field_type);\n",
1469 case TDESC_TYPE_UNION
:
1471 (" type_with_fields = tdesc_create_union (feature, \"%s\");\n",
1472 type
->name
.c_str ());
1473 for (const tdesc_type_field
&f
: type
->fields
)
1475 printf_field_type_assignment
1476 ("tdesc_named_type (feature, \"%s\");\n", f
.type
->name
.c_str ());
1478 (" tdesc_add_field (type_with_fields, \"%s\", field_type);\n",
1482 case TDESC_TYPE_ENUM
:
1484 (" type_with_fields = tdesc_create_enum (feature, \"%s\", %d);\n",
1485 type
->name
.c_str (), type
->size
);
1486 for (const tdesc_type_field
&f
: type
->fields
)
1488 (" tdesc_add_enum_value (type_with_fields, %d, \"%s\");\n",
1489 f
.start
, f
.name
.c_str ());
1492 error (_("C output is not supported type \"%s\"."), type
->name
.c_str ());
1495 printf_unfiltered ("\n");
1498 void visit (const tdesc_reg
*reg
) override
1500 printf_unfiltered (" tdesc_create_reg (feature, \"%s\", %ld, %d, ",
1501 reg
->name
.c_str (), reg
->target_regnum
,
1503 if (!reg
->group
.empty ())
1504 printf_unfiltered ("\"%s\", ", reg
->group
.c_str ());
1506 printf_unfiltered ("NULL, ");
1507 printf_unfiltered ("%d, \"%s\");\n", reg
->bitsize
, reg
->type
.c_str ());
1511 std::string m_filename_after_features
;
1515 /* Print an assignment to the field_type variable. Print the declaration
1516 of field_type if that has not been done yet. */
1517 ATTRIBUTE_PRINTF (2, 3)
1518 void printf_field_type_assignment (const char *fmt
, ...)
1520 if (!m_printed_field_type
)
1522 printf_unfiltered (" tdesc_type *field_type;\n");
1523 m_printed_field_type
= true;
1526 printf_unfiltered (" field_type = ");
1529 va_start (args
, fmt
);
1530 vprintf_unfiltered (fmt
, args
);
1536 /* Did we print "struct tdesc_type *element_type;" yet? */
1537 bool m_printed_element_type
= false;
1539 /* Did we print "struct tdesc_type_with_fields *element_type;" yet? */
1540 bool m_printed_type_with_fields
= false;
1542 /* Did we print "struct tdesc_type *field_type;" yet? */
1543 bool m_printed_field_type
= false;
1546 /* Print target description feature in C. */
1548 class print_c_feature
: public print_c_tdesc
1551 print_c_feature (std::string
&file
)
1552 : print_c_tdesc (file
)
1555 auto const pos
= m_filename_after_features
.find_last_of ('.');
1557 m_filename_after_features
= m_filename_after_features
.substr (0, pos
);
1560 void visit_pre (const target_desc
*e
) override
1562 printf_unfiltered (" Original: %s */\n\n",
1563 lbasename (m_filename_after_features
.c_str ()));
1565 printf_unfiltered ("#include \"gdbsupport/tdesc.h\"\n");
1566 printf_unfiltered ("\n");
1569 void visit_post (const target_desc
*e
) override
1572 void visit_pre (const tdesc_feature
*e
) override
1574 std::string
name (m_filename_after_features
);
1576 auto pos
= name
.find_first_of ('.');
1578 name
= name
.substr (0, pos
);
1579 std::replace (name
.begin (), name
.end (), '/', '_');
1580 std::replace (name
.begin (), name
.end (), '-', '_');
1582 printf_unfiltered ("static int\n");
1583 printf_unfiltered ("create_feature_%s ", name
.c_str ());
1584 printf_unfiltered ("(struct target_desc *result, long regnum)\n");
1586 printf_unfiltered ("{\n");
1587 printf_unfiltered (" struct tdesc_feature *feature;\n");
1590 ("\n feature = tdesc_create_feature (result, \"%s\");\n",
1594 void visit_post (const tdesc_feature
*e
) override
1596 printf_unfiltered (" return regnum;\n");
1597 printf_unfiltered ("}\n");
1600 void visit (const tdesc_reg
*reg
) override
1602 /* Most "reg" in XML target descriptions don't have "regnum"
1603 attribute, so the register number is allocated sequentially.
1604 In case that reg has "regnum" attribute, register number
1605 should be set by that explicitly. */
1607 if (reg
->target_regnum
< m_next_regnum
)
1609 /* The integrity check, it can catch some errors on register
1610 number collision, like this,
1612 <reg name="x0" bitsize="32"/>
1613 <reg name="x1" bitsize="32"/>
1614 <reg name="x2" bitsize="32"/>
1615 <reg name="x3" bitsize="32"/>
1616 <reg name="ps" bitsize="32" regnum="3"/>
1618 but it also has false negatives. The target description
1621 <reg name="x1" bitsize="32" regnum="1"/>
1622 <reg name="x3" bitsize="32" regnum="3"/>
1623 <reg name="x2" bitsize="32" regnum="2"/>
1624 <reg name="x4" bitsize="32" regnum="4"/>
1626 but it is not a good practice, so still error on this,
1627 and also print the message so that it can be saved in the
1628 generated c file. */
1630 printf_unfiltered ("ERROR: \"regnum\" attribute %ld ",
1631 reg
->target_regnum
);
1632 printf_unfiltered ("is not the largest number (%d).\n",
1634 error (_("\"regnum\" attribute %ld is not the largest number (%d)."),
1635 reg
->target_regnum
, m_next_regnum
);
1638 if (reg
->target_regnum
> m_next_regnum
)
1640 printf_unfiltered (" regnum = %ld;\n", reg
->target_regnum
);
1641 m_next_regnum
= reg
->target_regnum
;
1644 printf_unfiltered (" tdesc_create_reg (feature, \"%s\", regnum++, %d, ",
1645 reg
->name
.c_str (), reg
->save_restore
);
1646 if (!reg
->group
.empty ())
1647 printf_unfiltered ("\"%s\", ", reg
->group
.c_str ());
1649 printf_unfiltered ("NULL, ");
1650 printf_unfiltered ("%d, \"%s\");\n", reg
->bitsize
, reg
->type
.c_str ());
1656 /* The register number to use for the next register we see. */
1657 int m_next_regnum
= 0;
1660 /* See gdbsupport/tdesc.h. */
1663 tdesc_get_features_xml (const target_desc
*tdesc
)
1665 if (tdesc
->xmltarget
== nullptr)
1667 std::string
buffer ("@");
1668 print_xml_feature
v (&buffer
);
1670 tdesc
->xmltarget
= xstrdup (buffer
.c_str ());
1672 return tdesc
->xmltarget
;
1676 maint_print_c_tdesc_cmd (const char *args
, int from_tty
)
1678 const struct target_desc
*tdesc
;
1679 const char *filename
;
1683 /* Use the global target-supplied description, not the current
1684 architecture's. This lets a GDB for one architecture generate C
1685 for another architecture's description, even though the gdbarch
1686 initialization code will reject the new description. */
1687 tdesc
= current_target_desc
;
1688 filename
= target_description_filename
;
1692 /* Use the target description from the XML file. */
1694 tdesc
= file_read_description_xml (filename
);
1698 error (_("There is no target description to print."));
1700 if (filename
== NULL
)
1701 error (_("The current target description did not come from an XML file."));
1703 std::string
filename_after_features (filename
);
1704 auto loc
= filename_after_features
.rfind ("/features/");
1706 if (loc
!= std::string::npos
)
1707 filename_after_features
= filename_after_features
.substr (loc
+ 10);
1709 /* Print c files for target features instead of target descriptions,
1710 because c files got from target features are more flexible than the
1712 if (startswith (filename_after_features
.c_str (), "i386/32bit-")
1713 || startswith (filename_after_features
.c_str (), "i386/64bit-")
1714 || startswith (filename_after_features
.c_str (), "i386/x32-core.xml")
1715 || startswith (filename_after_features
.c_str (), "riscv/")
1716 || startswith (filename_after_features
.c_str (), "tic6x-")
1717 || startswith (filename_after_features
.c_str (), "aarch64")
1718 || startswith (filename_after_features
.c_str (), "arm/"))
1720 print_c_feature
v (filename_after_features
);
1726 print_c_tdesc
v (filename_after_features
);
1732 namespace selftests
{
1734 /* A reference target description, used for testing (see record_xml_tdesc). */
1736 struct xml_test_tdesc
1738 xml_test_tdesc (const char *name
, std::unique_ptr
<const target_desc
> &&tdesc
)
1739 : name (name
), tdesc (std::move (tdesc
))
1743 std::unique_ptr
<const target_desc
> tdesc
;
1746 static std::vector
<xml_test_tdesc
> xml_tdesc
;
1750 /* See target-descriptions.h. */
1753 record_xml_tdesc (const char *xml_file
, const struct target_desc
*tdesc
)
1755 xml_tdesc
.emplace_back (xml_file
, std::unique_ptr
<const target_desc
> (tdesc
));
1761 /* Test the conversion process of a target description to/from xml: Take a target
1762 description TDESC, convert to xml, back to a description, and confirm the new
1763 tdesc is identical to the original. */
1765 maintenance_check_tdesc_xml_convert (const target_desc
*tdesc
, const char *name
)
1767 const char *xml
= tdesc_get_features_xml (tdesc
);
1769 if (xml
== nullptr || *xml
!= '@')
1771 printf_filtered (_("Could not convert description for %s to xml.\n"),
1776 const target_desc
*tdesc_trans
= string_read_description_xml (xml
+ 1);
1778 if (tdesc_trans
== nullptr)
1780 printf_filtered (_("Could not convert description for %s from xml.\n"),
1784 else if (*tdesc
!= *tdesc_trans
)
1786 printf_filtered (_("Converted description for %s does not match.\n"),
1794 /* Check that the target descriptions created dynamically by
1795 architecture-specific code equal the descriptions created from XML files
1796 found in the specified directory DIR. */
1799 maintenance_check_xml_descriptions (const char *dir
, int from_tty
)
1802 error (_("Missing dir name"));
1804 gdb::unique_xmalloc_ptr
<char> dir1 (tilde_expand (dir
));
1805 std::string
feature_dir (dir1
.get ());
1806 unsigned int failed
= 0;
1808 for (auto const &e
: selftests::xml_tdesc
)
1810 std::string tdesc_xml
= (feature_dir
+ SLASH_STRING
+ e
.name
);
1811 const target_desc
*tdesc
1812 = file_read_description_xml (tdesc_xml
.data ());
1814 if (tdesc
== NULL
|| *tdesc
!= *e
.tdesc
)
1816 printf_filtered ( _("Descriptions for %s do not match.\n"), e
.name
);
1819 else if (!maintenance_check_tdesc_xml_convert (tdesc
, e
.name
)
1820 || !maintenance_check_tdesc_xml_convert (e
.tdesc
.get (), e
.name
))
1823 printf_filtered (_("Tested %lu XML files, %d failed\n"),
1824 (long) selftests::xml_tdesc
.size (), failed
);
1828 _initialize_target_descriptions (void)
1830 tdesc_data
= gdbarch_data_register_pre_init (tdesc_data_init
);
1832 add_prefix_cmd ("tdesc", class_maintenance
, set_tdesc_cmd
, _("\
1833 Set target description specific variables."),
1834 &tdesc_set_cmdlist
, "set tdesc ",
1835 0 /* allow-unknown */, &setlist
);
1836 add_prefix_cmd ("tdesc", class_maintenance
, show_tdesc_cmd
, _("\
1837 Show target description specific variables."),
1838 &tdesc_show_cmdlist
, "show tdesc ",
1839 0 /* allow-unknown */, &showlist
);
1840 add_prefix_cmd ("tdesc", class_maintenance
, unset_tdesc_cmd
, _("\
1841 Unset target description specific variables."),
1842 &tdesc_unset_cmdlist
, "unset tdesc ",
1843 0 /* allow-unknown */, &unsetlist
);
1845 add_setshow_filename_cmd ("filename", class_obscure
,
1846 &tdesc_filename_cmd_string
,
1848 Set the file to read for an XML target description."), _("\
1849 Show the file to read for an XML target description."), _("\
1850 When set, GDB will read the target description from a local\n\
1851 file instead of querying the remote target."),
1852 set_tdesc_filename_cmd
,
1853 show_tdesc_filename_cmd
,
1854 &tdesc_set_cmdlist
, &tdesc_show_cmdlist
);
1856 add_cmd ("filename", class_obscure
, unset_tdesc_filename_cmd
, _("\
1857 Unset the file to read for an XML target description.\n\
1858 When unset, GDB will read the description from the target."),
1859 &tdesc_unset_cmdlist
);
1861 add_cmd ("c-tdesc", class_maintenance
, maint_print_c_tdesc_cmd
, _("\
1862 Print the current target description as a C source file."),
1863 &maintenanceprintlist
);
1865 cmd_list_element
*cmd
;
1867 cmd
= add_cmd ("xml-descriptions", class_maintenance
,
1868 maintenance_check_xml_descriptions
, _("\
1869 Check equality of GDB target descriptions and XML created descriptions.\n\
1870 Check the target descriptions created in GDB equal the descriptions\n\
1871 created from XML files in the directory.\n\
1872 The parameter is the directory name."),
1873 &maintenancechecklist
);
1874 set_cmd_completer (cmd
, filename_completer
);