Automatic date update in version.in
[binutils-gdb.git] / gdb / xml-tdesc.c
blobfe3108cc934e957580e90df8eb87b1fbc479a5a0
1 /* XML target description support for GDB.
3 Copyright (C) 2006-2022 Free Software Foundation, Inc.
5 Contributed by CodeSourcery.
7 This file is part of GDB.
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program. If not, see <http://www.gnu.org/licenses/>. */
22 #include "defs.h"
23 #include "target.h"
24 #include "target-descriptions.h"
25 #include "xml-support.h"
26 #include "xml-tdesc.h"
27 #include "osabi.h"
28 #include "filenames.h"
29 #include <unordered_map>
30 #include <string>
32 /* Maximum sizes.
33 This is just to catch obviously wrong values. */
34 #define MAX_FIELD_SIZE 65536
35 #define MAX_FIELD_BITSIZE (MAX_FIELD_SIZE * TARGET_CHAR_BIT)
36 #define MAX_VECTOR_SIZE 65536
38 #if !defined(HAVE_LIBEXPAT)
40 /* Parse DOCUMENT into a target description. Or don't, since we don't have
41 an XML parser. */
43 static struct target_desc *
44 tdesc_parse_xml (const char *document, xml_fetch_another fetcher)
46 static int have_warned;
48 if (!have_warned)
50 have_warned = 1;
51 warning (_("Can not parse XML target description; XML support was "
52 "disabled at compile time"));
55 return NULL;
58 #else /* HAVE_LIBEXPAT */
60 /* A record of every XML description we have parsed. We never discard
61 old descriptions, because we never discard gdbarches. As long as we
62 have a gdbarch referencing this description, we want to have a copy
63 of it here, so that if we parse the same XML document again we can
64 return the same "struct target_desc *"; if they are not singletons,
65 then we will create unnecessary duplicate gdbarches. See
66 gdbarch_list_lookup_by_info. */
68 static std::unordered_map<std::string, target_desc_up> xml_cache;
70 /* Callback data for target description parsing. */
72 struct tdesc_parsing_data
74 /* The target description we are building. */
75 struct target_desc *tdesc;
77 /* The target feature we are currently parsing, or last parsed. */
78 struct tdesc_feature *current_feature;
80 /* The register number to use for the next register we see, if
81 it does not have its own. This starts at zero. */
82 int next_regnum;
84 /* The struct or union we are currently parsing, or last parsed. */
85 tdesc_type_with_fields *current_type;
87 /* The byte size of the current struct/flags type, if specified. Zero
88 if not specified. Flags values must specify a size. */
89 int current_type_size;
92 /* Handle the end of an <architecture> element and its value. */
94 static void
95 tdesc_end_arch (struct gdb_xml_parser *parser,
96 const struct gdb_xml_element *element,
97 void *user_data, const char *body_text)
99 struct tdesc_parsing_data *data = (struct tdesc_parsing_data *) user_data;
100 const struct bfd_arch_info *arch;
102 arch = bfd_scan_arch (body_text);
103 if (arch == NULL)
104 gdb_xml_error (parser, _("Target description specified unknown "
105 "architecture \"%s\""), body_text);
106 set_tdesc_architecture (data->tdesc, arch);
109 /* Handle the end of an <osabi> element and its value. */
111 static void
112 tdesc_end_osabi (struct gdb_xml_parser *parser,
113 const struct gdb_xml_element *element,
114 void *user_data, const char *body_text)
116 struct tdesc_parsing_data *data = (struct tdesc_parsing_data *) user_data;
117 enum gdb_osabi osabi;
119 osabi = osabi_from_tdesc_string (body_text);
120 if (osabi == GDB_OSABI_UNKNOWN)
121 warning (_("Target description specified unknown osabi \"%s\""),
122 body_text);
123 else
124 set_tdesc_osabi (data->tdesc, osabi);
127 /* Handle the end of a <compatible> element and its value. */
129 static void
130 tdesc_end_compatible (struct gdb_xml_parser *parser,
131 const struct gdb_xml_element *element,
132 void *user_data, const char *body_text)
134 struct tdesc_parsing_data *data = (struct tdesc_parsing_data *) user_data;
135 const struct bfd_arch_info *arch;
137 arch = bfd_scan_arch (body_text);
138 tdesc_add_compatible (data->tdesc, arch);
141 /* Handle the start of a <target> element. */
143 static void
144 tdesc_start_target (struct gdb_xml_parser *parser,
145 const struct gdb_xml_element *element,
146 void *user_data, std::vector<gdb_xml_value> &attributes)
148 char *version
149 = (char *) xml_find_attribute (attributes, "version")->value.get ();
151 if (strcmp (version, "1.0") != 0)
152 gdb_xml_error (parser,
153 _("Target description has unsupported version \"%s\""),
154 version);
157 /* Handle the start of a <feature> element. */
159 static void
160 tdesc_start_feature (struct gdb_xml_parser *parser,
161 const struct gdb_xml_element *element,
162 void *user_data, std::vector<gdb_xml_value> &attributes)
164 struct tdesc_parsing_data *data = (struct tdesc_parsing_data *) user_data;
165 char *name = (char *) xml_find_attribute (attributes, "name")->value.get ();
167 data->current_feature = tdesc_create_feature (data->tdesc, name);
170 /* Handle the start of a <reg> element. Fill in the optional
171 attributes and attach it to the containing feature. */
173 static void
174 tdesc_start_reg (struct gdb_xml_parser *parser,
175 const struct gdb_xml_element *element,
176 void *user_data, std::vector<gdb_xml_value> &attributes)
178 struct tdesc_parsing_data *data = (struct tdesc_parsing_data *) user_data;
179 int ix = 0;
180 char *name, *group;
181 const char *type;
182 int bitsize, regnum, save_restore;
184 int length = attributes.size ();
186 name = (char *) attributes[ix++].value.get ();
187 bitsize = * (ULONGEST *) attributes[ix++].value.get ();
189 if (ix < length && strcmp (attributes[ix].name, "regnum") == 0)
190 regnum = * (ULONGEST *) attributes[ix++].value.get ();
191 else
192 regnum = data->next_regnum;
194 if (ix < length && strcmp (attributes[ix].name, "type") == 0)
195 type = (char *) attributes[ix++].value.get ();
196 else
197 type = "int";
199 if (ix < length && strcmp (attributes[ix].name, "group") == 0)
200 group = (char *) attributes[ix++].value.get ();
201 else
202 group = NULL;
204 if (ix < length && strcmp (attributes[ix].name, "save-restore") == 0)
205 save_restore = * (ULONGEST *) attributes[ix++].value.get ();
206 else
207 save_restore = 1;
209 if (strcmp (type, "int") != 0
210 && strcmp (type, "float") != 0
211 && tdesc_named_type (data->current_feature, type) == NULL)
212 gdb_xml_error (parser, _("Register \"%s\" has unknown type \"%s\""),
213 name, type);
215 tdesc_create_reg (data->current_feature, name, regnum, save_restore, group,
216 bitsize, type);
218 data->next_regnum = regnum + 1;
221 /* Handle the start of a <union> element. Initialize the type and
222 record it with the current feature. */
224 static void
225 tdesc_start_union (struct gdb_xml_parser *parser,
226 const struct gdb_xml_element *element,
227 void *user_data, std::vector<gdb_xml_value> &attributes)
229 struct tdesc_parsing_data *data = (struct tdesc_parsing_data *) user_data;
230 char *id = (char *) xml_find_attribute (attributes, "id")->value.get ();
232 data->current_type = tdesc_create_union (data->current_feature, id);
233 data->current_type_size = 0;
236 /* Handle the start of a <struct> element. Initialize the type and
237 record it with the current feature. */
239 static void
240 tdesc_start_struct (struct gdb_xml_parser *parser,
241 const struct gdb_xml_element *element,
242 void *user_data, std::vector<gdb_xml_value> &attributes)
244 struct tdesc_parsing_data *data = (struct tdesc_parsing_data *) user_data;
245 char *id = (char *) xml_find_attribute (attributes, "id")->value.get ();
246 struct gdb_xml_value *attr;
248 tdesc_type_with_fields *type_with_fields
249 = tdesc_create_struct (data->current_feature, id);
250 data->current_type = type_with_fields;
251 data->current_type_size = 0;
253 attr = xml_find_attribute (attributes, "size");
254 if (attr != NULL)
256 ULONGEST size = * (ULONGEST *) attr->value.get ();
258 if (size > MAX_FIELD_SIZE)
260 gdb_xml_error (parser,
261 _("Struct size %s is larger than maximum (%d)"),
262 pulongest (size), MAX_FIELD_SIZE);
264 tdesc_set_struct_size (type_with_fields, size);
265 data->current_type_size = size;
269 static void
270 tdesc_start_flags (struct gdb_xml_parser *parser,
271 const struct gdb_xml_element *element,
272 void *user_data, std::vector<gdb_xml_value> &attributes)
274 struct tdesc_parsing_data *data = (struct tdesc_parsing_data *) user_data;
275 char *id = (char *) xml_find_attribute (attributes, "id")->value.get ();
276 ULONGEST size = * (ULONGEST *)
277 xml_find_attribute (attributes, "size")->value.get ();
279 if (size > MAX_FIELD_SIZE)
281 gdb_xml_error (parser,
282 _("Flags size %s is larger than maximum (%d)"),
283 pulongest (size), MAX_FIELD_SIZE);
286 data->current_type = tdesc_create_flags (data->current_feature, id, size);
287 data->current_type_size = size;
290 static void
291 tdesc_start_enum (struct gdb_xml_parser *parser,
292 const struct gdb_xml_element *element,
293 void *user_data, std::vector<gdb_xml_value> &attributes)
295 struct tdesc_parsing_data *data = (struct tdesc_parsing_data *) user_data;
296 char *id = (char *) xml_find_attribute (attributes, "id")->value.get ();
297 int size = * (ULONGEST *)
298 xml_find_attribute (attributes, "size")->value.get ();
300 if (size > MAX_FIELD_SIZE)
302 gdb_xml_error (parser,
303 _("Enum size %s is larger than maximum (%d)"),
304 pulongest (size), MAX_FIELD_SIZE);
307 data->current_type = tdesc_create_enum (data->current_feature, id, size);
308 data->current_type_size = 0;
311 /* Handle the start of a <field> element. Attach the field to the
312 current struct, union or flags. */
314 static void
315 tdesc_start_field (struct gdb_xml_parser *parser,
316 const struct gdb_xml_element *element,
317 void *user_data, std::vector<gdb_xml_value> &attributes)
319 struct tdesc_parsing_data *data = (struct tdesc_parsing_data *) user_data;
320 struct gdb_xml_value *attr;
321 struct tdesc_type *field_type;
322 char *field_name, *field_type_id;
323 int start, end;
325 field_name = (char *) xml_find_attribute (attributes, "name")->value.get ();
327 attr = xml_find_attribute (attributes, "type");
328 if (attr != NULL)
330 field_type_id = (char *) attr->value.get ();
331 field_type = tdesc_named_type (data->current_feature, field_type_id);
333 else
335 field_type_id = NULL;
336 field_type = NULL;
339 attr = xml_find_attribute (attributes, "start");
340 if (attr != NULL)
342 ULONGEST ul_start = * (ULONGEST *) attr->value.get ();
344 if (ul_start > MAX_FIELD_BITSIZE)
346 gdb_xml_error (parser,
347 _("Field start %s is larger than maximum (%d)"),
348 pulongest (ul_start), MAX_FIELD_BITSIZE);
350 start = ul_start;
352 else
353 start = -1;
355 attr = xml_find_attribute (attributes, "end");
356 if (attr != NULL)
358 ULONGEST ul_end = * (ULONGEST *) attr->value.get ();
360 if (ul_end > MAX_FIELD_BITSIZE)
362 gdb_xml_error (parser,
363 _("Field end %s is larger than maximum (%d)"),
364 pulongest (ul_end), MAX_FIELD_BITSIZE);
366 end = ul_end;
368 else
369 end = -1;
371 if (start != -1)
373 tdesc_type_with_fields *t = data->current_type;
375 /* Older versions of gdb can't handle elided end values.
376 Stick with that for now, to help ensure backward compatibility.
377 E.g., If a newer gdbserver is talking to an older gdb. */
378 if (end == -1)
379 gdb_xml_error (parser, _("Missing end value"));
381 if (data->current_type_size == 0)
382 gdb_xml_error (parser,
383 _("Bitfields must live in explicitly sized types"));
385 if (field_type_id != NULL
386 && strcmp (field_type_id, "bool") == 0
387 && start != end)
389 gdb_xml_error (parser,
390 _("Boolean fields must be one bit in size"));
393 if (end >= 64)
394 gdb_xml_error (parser,
395 _("Bitfield \"%s\" goes past "
396 "64 bits (unsupported)"),
397 field_name);
399 /* Assume that the bit numbering in XML is "lsb-zero". Most
400 architectures other than PowerPC use this ordering. In the
401 future, we can add an XML tag to indicate "msb-zero" numbering. */
402 if (start > end)
403 gdb_xml_error (parser, _("Bitfield \"%s\" has start after end"),
404 field_name);
405 if (end >= data->current_type_size * TARGET_CHAR_BIT)
406 gdb_xml_error (parser, _("Bitfield \"%s\" does not fit in struct"),
407 field_name);
409 if (field_type != NULL)
410 tdesc_add_typed_bitfield (t, field_name, start, end, field_type);
411 else if (start == end)
412 tdesc_add_flag (t, start, field_name);
413 else
414 tdesc_add_bitfield (t, field_name, start, end);
416 else if (start == -1 && end != -1)
417 gdb_xml_error (parser, _("End specified but not start"));
418 else if (field_type_id != NULL)
420 /* TDESC_TYPE_FLAGS values are explicitly sized, so the following test
421 catches adding non-bitfield types to flags as well. */
422 if (data->current_type_size != 0)
423 gdb_xml_error (parser,
424 _("Explicitly sized type cannot "
425 "contain non-bitfield \"%s\""),
426 field_name);
428 if (field_type == NULL)
429 gdb_xml_error (parser, _("Field \"%s\" references undefined "
430 "type \"%s\""),
431 field_name, field_type_id);
433 tdesc_add_field (data->current_type, field_name, field_type);
435 else
436 gdb_xml_error (parser, _("Field \"%s\" has neither type nor bit position"),
437 field_name);
440 /* Handle the start of an <evalue> element. Attach the value to the
441 current enum. */
443 static void
444 tdesc_start_enum_value (struct gdb_xml_parser *parser,
445 const struct gdb_xml_element *element,
446 void *user_data, std::vector<gdb_xml_value> &attributes)
448 struct tdesc_parsing_data *data = (struct tdesc_parsing_data *) user_data;
449 struct gdb_xml_value *attr;
450 char *field_name;
451 ULONGEST ul_value;
452 int value;
454 field_name = (char *) xml_find_attribute (attributes, "name")->value.get ();
456 attr = xml_find_attribute (attributes, "value");
457 ul_value = * (ULONGEST *) attr->value.get ();
458 if (ul_value > INT_MAX)
460 gdb_xml_error (parser,
461 _("Enum value %s is larger than maximum (%d)"),
462 pulongest (ul_value), INT_MAX);
464 value = ul_value;
466 tdesc_add_enum_value (data->current_type, value, field_name);
469 /* Handle the start of a <vector> element. Initialize the type and
470 record it with the current feature. */
472 static void
473 tdesc_start_vector (struct gdb_xml_parser *parser,
474 const struct gdb_xml_element *element,
475 void *user_data, std::vector<gdb_xml_value> &attributes)
477 struct tdesc_parsing_data *data = (struct tdesc_parsing_data *) user_data;
478 struct tdesc_type *field_type;
479 char *id, *field_type_id;
480 ULONGEST count;
482 id = (char *) attributes[0].value.get ();
483 field_type_id = (char *) attributes[1].value.get ();
484 count = * (ULONGEST *) attributes[2].value.get ();
486 if (count > MAX_VECTOR_SIZE)
488 gdb_xml_error (parser,
489 _("Vector size %s is larger than maximum (%d)"),
490 pulongest (count), MAX_VECTOR_SIZE);
493 field_type = tdesc_named_type (data->current_feature, field_type_id);
494 if (field_type == NULL)
495 gdb_xml_error (parser, _("Vector \"%s\" references undefined type \"%s\""),
496 id, field_type_id);
498 tdesc_create_vector (data->current_feature, id, field_type, count);
501 /* The elements and attributes of an XML target description. */
503 static const struct gdb_xml_attribute field_attributes[] = {
504 { "name", GDB_XML_AF_NONE, NULL, NULL },
505 { "type", GDB_XML_AF_OPTIONAL, NULL, NULL },
506 { "start", GDB_XML_AF_OPTIONAL, gdb_xml_parse_attr_ulongest, NULL },
507 { "end", GDB_XML_AF_OPTIONAL, gdb_xml_parse_attr_ulongest, NULL },
508 { NULL, GDB_XML_AF_NONE, NULL, NULL }
511 static const struct gdb_xml_attribute enum_value_attributes[] = {
512 { "name", GDB_XML_AF_NONE, NULL, NULL },
513 { "value", GDB_XML_AF_OPTIONAL, gdb_xml_parse_attr_ulongest, NULL },
514 { NULL, GDB_XML_AF_NONE, NULL, NULL }
517 static const struct gdb_xml_element struct_union_children[] = {
518 { "field", field_attributes, NULL, GDB_XML_EF_REPEATABLE,
519 tdesc_start_field, NULL },
520 { NULL, NULL, NULL, GDB_XML_EF_NONE, NULL, NULL }
523 static const struct gdb_xml_element enum_children[] = {
524 { "evalue", enum_value_attributes, NULL, GDB_XML_EF_REPEATABLE,
525 tdesc_start_enum_value, NULL },
526 { NULL, NULL, NULL, GDB_XML_EF_NONE, NULL, NULL }
529 static const struct gdb_xml_attribute reg_attributes[] = {
530 { "name", GDB_XML_AF_NONE, NULL, NULL },
531 { "bitsize", GDB_XML_AF_NONE, gdb_xml_parse_attr_ulongest, NULL },
532 { "regnum", GDB_XML_AF_OPTIONAL, gdb_xml_parse_attr_ulongest, NULL },
533 { "type", GDB_XML_AF_OPTIONAL, NULL, NULL },
534 { "group", GDB_XML_AF_OPTIONAL, NULL, NULL },
535 { "save-restore", GDB_XML_AF_OPTIONAL,
536 gdb_xml_parse_attr_enum, gdb_xml_enums_boolean },
537 { NULL, GDB_XML_AF_NONE, NULL, NULL }
540 static const struct gdb_xml_attribute struct_union_attributes[] = {
541 { "id", GDB_XML_AF_NONE, NULL, NULL },
542 { "size", GDB_XML_AF_OPTIONAL, gdb_xml_parse_attr_ulongest, NULL},
543 { NULL, GDB_XML_AF_NONE, NULL, NULL }
546 static const struct gdb_xml_attribute flags_attributes[] = {
547 { "id", GDB_XML_AF_NONE, NULL, NULL },
548 { "size", GDB_XML_AF_NONE, gdb_xml_parse_attr_ulongest, NULL},
549 { NULL, GDB_XML_AF_NONE, NULL, NULL }
552 static const struct gdb_xml_attribute enum_attributes[] = {
553 { "id", GDB_XML_AF_NONE, NULL, NULL },
554 { "size", GDB_XML_AF_NONE, gdb_xml_parse_attr_ulongest, NULL},
555 { NULL, GDB_XML_AF_NONE, NULL, NULL }
558 static const struct gdb_xml_attribute vector_attributes[] = {
559 { "id", GDB_XML_AF_NONE, NULL, NULL },
560 { "type", GDB_XML_AF_NONE, NULL, NULL },
561 { "count", GDB_XML_AF_NONE, gdb_xml_parse_attr_ulongest, NULL },
562 { NULL, GDB_XML_AF_NONE, NULL, NULL }
565 static const struct gdb_xml_attribute feature_attributes[] = {
566 { "name", GDB_XML_AF_NONE, NULL, NULL },
567 { NULL, GDB_XML_AF_NONE, NULL, NULL }
570 static const struct gdb_xml_element feature_children[] = {
571 { "reg", reg_attributes, NULL,
572 GDB_XML_EF_OPTIONAL | GDB_XML_EF_REPEATABLE,
573 tdesc_start_reg, NULL },
574 { "struct", struct_union_attributes, struct_union_children,
575 GDB_XML_EF_OPTIONAL | GDB_XML_EF_REPEATABLE,
576 tdesc_start_struct, NULL },
577 { "union", struct_union_attributes, struct_union_children,
578 GDB_XML_EF_OPTIONAL | GDB_XML_EF_REPEATABLE,
579 tdesc_start_union, NULL },
580 { "flags", flags_attributes, struct_union_children,
581 GDB_XML_EF_OPTIONAL | GDB_XML_EF_REPEATABLE,
582 tdesc_start_flags, NULL },
583 { "enum", enum_attributes, enum_children,
584 GDB_XML_EF_OPTIONAL | GDB_XML_EF_REPEATABLE,
585 tdesc_start_enum, NULL },
586 { "vector", vector_attributes, NULL,
587 GDB_XML_EF_OPTIONAL | GDB_XML_EF_REPEATABLE,
588 tdesc_start_vector, NULL },
589 { NULL, NULL, NULL, GDB_XML_EF_NONE, NULL, NULL }
592 static const struct gdb_xml_attribute target_attributes[] = {
593 { "version", GDB_XML_AF_NONE, NULL, NULL },
594 { NULL, GDB_XML_AF_NONE, NULL, NULL }
597 static const struct gdb_xml_element target_children[] = {
598 { "architecture", NULL, NULL, GDB_XML_EF_OPTIONAL,
599 NULL, tdesc_end_arch },
600 { "osabi", NULL, NULL, GDB_XML_EF_OPTIONAL,
601 NULL, tdesc_end_osabi },
602 { "compatible", NULL, NULL, GDB_XML_EF_OPTIONAL | GDB_XML_EF_REPEATABLE,
603 NULL, tdesc_end_compatible },
604 { "feature", feature_attributes, feature_children,
605 GDB_XML_EF_OPTIONAL | GDB_XML_EF_REPEATABLE,
606 tdesc_start_feature, NULL },
607 { NULL, NULL, NULL, GDB_XML_EF_NONE, NULL, NULL }
610 static const struct gdb_xml_element tdesc_elements[] = {
611 { "target", target_attributes, target_children, GDB_XML_EF_NONE,
612 tdesc_start_target, NULL },
613 { NULL, NULL, NULL, GDB_XML_EF_NONE, NULL, NULL }
616 /* Parse DOCUMENT into a target description and return it. */
618 static struct target_desc *
619 tdesc_parse_xml (const char *document, xml_fetch_another fetcher)
621 struct tdesc_parsing_data data;
623 /* Expand all XInclude directives. */
624 std::string expanded_text;
626 if (!xml_process_xincludes (expanded_text,
627 _("target description"),
628 document, fetcher, 0))
630 warning (_("Could not load XML target description; ignoring"));
631 return NULL;
634 /* Check for an exact match in the list of descriptions we have
635 previously parsed. */
636 const auto it = xml_cache.find (expanded_text);
637 if (it != xml_cache.end ())
638 return it->second.get ();
640 memset (&data, 0, sizeof (struct tdesc_parsing_data));
641 target_desc_up description (allocate_target_description ());
642 data.tdesc = description.get ();
644 if (gdb_xml_parse_quick (_("target description"), "gdb-target.dtd",
645 tdesc_elements, expanded_text.c_str (), &data) == 0)
647 /* Parsed successfully. */
648 xml_cache.emplace (std::move (expanded_text), std::move (description));
649 return data.tdesc;
651 else
653 warning (_("Could not load XML target description; ignoring"));
654 return NULL;
657 #endif /* HAVE_LIBEXPAT */
660 /* Read an XML target description from FILENAME. Parse it, and return
661 the parsed description. */
663 const struct target_desc *
664 file_read_description_xml (const char *filename)
666 gdb::optional<gdb::char_vector> tdesc_str
667 = xml_fetch_content_from_file (filename, NULL);
668 if (!tdesc_str)
670 warning (_("Could not open \"%s\""), filename);
671 return NULL;
674 const std::string dirname = ldirname (filename);
675 auto fetch_another = [&dirname] (const char *name)
677 return xml_fetch_content_from_file (name, dirname.c_str ());
680 return tdesc_parse_xml (tdesc_str->data (), fetch_another);
683 /* Read a string representation of available features from the target,
684 using TARGET_OBJECT_AVAILABLE_FEATURES. The returned string is
685 malloc allocated and NUL-terminated. NAME should be a non-NULL
686 string identifying the XML document we want; the top level document
687 is "target.xml". Other calls may be performed for the DTD or
688 for <xi:include>. */
690 static gdb::optional<gdb::char_vector>
691 fetch_available_features_from_target (const char *name, target_ops *ops)
693 /* Read this object as a string. This ensures that a NUL
694 terminator is added. */
695 return target_read_stralloc (ops,
696 TARGET_OBJECT_AVAILABLE_FEATURES,
697 name);
701 /* Read an XML target description using OPS. Parse it, and return the
702 parsed description. */
704 const struct target_desc *
705 target_read_description_xml (struct target_ops *ops)
707 gdb::optional<gdb::char_vector> tdesc_str
708 = fetch_available_features_from_target ("target.xml", ops);
709 if (!tdesc_str)
710 return NULL;
712 auto fetch_another = [ops] (const char *name)
714 return fetch_available_features_from_target (name, ops);
717 return tdesc_parse_xml (tdesc_str->data (), fetch_another);
720 /* Fetches an XML target description using OPS, processing
721 includes, but not parsing it. Used to dump whole tdesc
722 as a single XML file. */
724 gdb::optional<std::string>
725 target_fetch_description_xml (struct target_ops *ops)
727 #if !defined(HAVE_LIBEXPAT)
728 static int have_warned;
730 if (!have_warned)
732 have_warned = 1;
733 warning (_("Can not fetch XML target description; XML support was "
734 "disabled at compile time"));
737 return {};
738 #else
739 gdb::optional<gdb::char_vector>
740 tdesc_str = fetch_available_features_from_target ("target.xml", ops);
741 if (!tdesc_str)
742 return {};
744 auto fetch_another = [ops] (const char *name)
746 return fetch_available_features_from_target (name, ops);
748 std::string output;
749 if (!xml_process_xincludes (output,
750 _("target description"),
751 tdesc_str->data (), fetch_another, 0))
753 warning (_("Could not load XML target description; ignoring"));
754 return {};
756 return output;
757 #endif
760 /* See xml-tdesc.h. */
762 const struct target_desc *
763 string_read_description_xml (const char *xml)
765 return tdesc_parse_xml (xml, [] (const char *href)
767 error (_("xincludes are unsupported with this method"));
768 return gdb::optional<gdb::char_vector> ();