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