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