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