Bump GDB's version number to 15.0.91.DATE-git.
[binutils-gdb.git] / gdb / xml-tdesc.c
blob436c493d4f914eb71a80bcbc08dc2ddd38c72027
1 /* XML target description support for GDB.
3 Copyright (C) 2006-2024 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 "target.h"
23 #include "target-descriptions.h"
24 #include "xml-support.h"
25 #include "xml-tdesc.h"
26 #include "osabi.h"
27 #include "filenames.h"
28 #include <unordered_map>
29 #include <string>
31 /* Maximum sizes.
32 This is just to catch obviously wrong values. */
33 #define MAX_FIELD_SIZE 65536
34 #define MAX_FIELD_BITSIZE (MAX_FIELD_SIZE * TARGET_CHAR_BIT)
35 #define MAX_VECTOR_SIZE 65536
37 #if !defined(HAVE_LIBEXPAT)
39 /* Parse DOCUMENT into a target description. Or don't, since we don't have
40 an XML parser. */
42 static struct target_desc *
43 tdesc_parse_xml (const char *document, xml_fetch_another fetcher)
45 static int have_warned;
47 if (!have_warned)
49 have_warned = 1;
50 warning (_("Can not parse XML target description; XML support was "
51 "disabled at compile time"));
54 return NULL;
57 #else /* HAVE_LIBEXPAT */
59 /* A record of every XML description we have parsed. We never discard
60 old descriptions, because we never discard gdbarches. As long as we
61 have a gdbarch referencing this description, we want to have a copy
62 of it here, so that if we parse the same XML document again we can
63 return the same "struct target_desc *"; if they are not singletons,
64 then we will create unnecessary duplicate gdbarches. See
65 gdbarch_list_lookup_by_info. */
67 static std::unordered_map<std::string, target_desc_up> xml_cache;
69 /* Callback data for target description parsing. */
71 struct tdesc_parsing_data
73 /* The target description we are building. */
74 struct target_desc *tdesc;
76 /* The target feature we are currently parsing, or last parsed. */
77 struct tdesc_feature *current_feature;
79 /* The register number to use for the next register we see, if
80 it does not have its own. This starts at zero. */
81 int next_regnum;
83 /* The struct or union we are currently parsing, or last parsed. */
84 tdesc_type_with_fields *current_type;
86 /* The byte size of the current struct/flags type, if specified. Zero
87 if not specified. Flags values must specify a size. */
88 int current_type_size;
91 /* Handle the end of an <architecture> element and its value. */
93 static void
94 tdesc_end_arch (struct gdb_xml_parser *parser,
95 const struct gdb_xml_element *element,
96 void *user_data, const char *body_text)
98 struct tdesc_parsing_data *data = (struct tdesc_parsing_data *) user_data;
99 const struct bfd_arch_info *arch;
101 arch = bfd_scan_arch (body_text);
102 if (arch == NULL)
103 gdb_xml_error (parser, _("Target description specified unknown "
104 "architecture \"%s\""), body_text);
105 set_tdesc_architecture (data->tdesc, arch);
108 /* Handle the end of an <osabi> element and its value. */
110 static void
111 tdesc_end_osabi (struct gdb_xml_parser *parser,
112 const struct gdb_xml_element *element,
113 void *user_data, const char *body_text)
115 struct tdesc_parsing_data *data = (struct tdesc_parsing_data *) user_data;
116 enum gdb_osabi osabi;
118 osabi = osabi_from_tdesc_string (body_text);
119 if (osabi == GDB_OSABI_UNKNOWN)
120 warning (_("Target description specified unknown osabi \"%s\""),
121 body_text);
122 else
123 set_tdesc_osabi (data->tdesc, osabi);
126 /* Handle the end of a <compatible> element and its value. */
128 static void
129 tdesc_end_compatible (struct gdb_xml_parser *parser,
130 const struct gdb_xml_element *element,
131 void *user_data, const char *body_text)
133 struct tdesc_parsing_data *data = (struct tdesc_parsing_data *) user_data;
134 const struct bfd_arch_info *arch;
136 arch = bfd_scan_arch (body_text);
137 tdesc_add_compatible (data->tdesc, arch);
140 /* Handle the start of a <target> element. */
142 static void
143 tdesc_start_target (struct gdb_xml_parser *parser,
144 const struct gdb_xml_element *element,
145 void *user_data, std::vector<gdb_xml_value> &attributes)
147 char *version
148 = (char *) xml_find_attribute (attributes, "version")->value.get ();
150 if (strcmp (version, "1.0") != 0)
151 gdb_xml_error (parser,
152 _("Target description has unsupported version \"%s\""),
153 version);
156 /* Handle the start of a <feature> element. */
158 static void
159 tdesc_start_feature (struct gdb_xml_parser *parser,
160 const struct gdb_xml_element *element,
161 void *user_data, std::vector<gdb_xml_value> &attributes)
163 struct tdesc_parsing_data *data = (struct tdesc_parsing_data *) user_data;
164 char *name = (char *) xml_find_attribute (attributes, "name")->value.get ();
166 data->current_feature = tdesc_create_feature (data->tdesc, name);
169 /* Handle the start of a <reg> element. Fill in the optional
170 attributes and attach it to the containing feature. */
172 static void
173 tdesc_start_reg (struct gdb_xml_parser *parser,
174 const struct gdb_xml_element *element,
175 void *user_data, std::vector<gdb_xml_value> &attributes)
177 struct tdesc_parsing_data *data = (struct tdesc_parsing_data *) user_data;
178 int ix = 0;
179 char *name, *group;
180 const char *type;
181 int bitsize, regnum, save_restore;
183 int length = attributes.size ();
185 name = (char *) attributes[ix++].value.get ();
186 bitsize = * (ULONGEST *) attributes[ix++].value.get ();
188 if (ix < length && strcmp (attributes[ix].name, "regnum") == 0)
189 regnum = * (ULONGEST *) attributes[ix++].value.get ();
190 else
191 regnum = data->next_regnum;
193 if (ix < length && strcmp (attributes[ix].name, "type") == 0)
194 type = (char *) attributes[ix++].value.get ();
195 else
196 type = "int";
198 if (ix < length && strcmp (attributes[ix].name, "group") == 0)
199 group = (char *) attributes[ix++].value.get ();
200 else
201 group = NULL;
203 if (ix < length && strcmp (attributes[ix].name, "save-restore") == 0)
204 save_restore = * (ULONGEST *) attributes[ix++].value.get ();
205 else
206 save_restore = 1;
208 if (strcmp (type, "int") != 0
209 && strcmp (type, "float") != 0
210 && tdesc_named_type (data->current_feature, type) == NULL)
211 gdb_xml_error (parser, _("Register \"%s\" has unknown type \"%s\""),
212 name, type);
214 tdesc_create_reg (data->current_feature, name, regnum, save_restore, group,
215 bitsize, type);
217 data->next_regnum = regnum + 1;
220 /* Handle the start of a <union> element. Initialize the type and
221 record it with the current feature. */
223 static void
224 tdesc_start_union (struct gdb_xml_parser *parser,
225 const struct gdb_xml_element *element,
226 void *user_data, std::vector<gdb_xml_value> &attributes)
228 struct tdesc_parsing_data *data = (struct tdesc_parsing_data *) user_data;
229 char *id = (char *) xml_find_attribute (attributes, "id")->value.get ();
231 data->current_type = tdesc_create_union (data->current_feature, id);
232 data->current_type_size = 0;
235 /* Handle the start of a <struct> element. Initialize the type and
236 record it with the current feature. */
238 static void
239 tdesc_start_struct (struct gdb_xml_parser *parser,
240 const struct gdb_xml_element *element,
241 void *user_data, std::vector<gdb_xml_value> &attributes)
243 struct tdesc_parsing_data *data = (struct tdesc_parsing_data *) user_data;
244 char *id = (char *) xml_find_attribute (attributes, "id")->value.get ();
245 struct gdb_xml_value *attr;
247 tdesc_type_with_fields *type_with_fields
248 = tdesc_create_struct (data->current_feature, id);
249 data->current_type = type_with_fields;
250 data->current_type_size = 0;
252 attr = xml_find_attribute (attributes, "size");
253 if (attr != NULL)
255 ULONGEST size = * (ULONGEST *) attr->value.get ();
257 if (size > MAX_FIELD_SIZE)
259 gdb_xml_error (parser,
260 _("Struct size %s is larger than maximum (%d)"),
261 pulongest (size), MAX_FIELD_SIZE);
263 tdesc_set_struct_size (type_with_fields, size);
264 data->current_type_size = size;
268 static void
269 tdesc_start_flags (struct gdb_xml_parser *parser,
270 const struct gdb_xml_element *element,
271 void *user_data, std::vector<gdb_xml_value> &attributes)
273 struct tdesc_parsing_data *data = (struct tdesc_parsing_data *) user_data;
274 char *id = (char *) xml_find_attribute (attributes, "id")->value.get ();
275 ULONGEST size = * (ULONGEST *)
276 xml_find_attribute (attributes, "size")->value.get ();
278 if (size > MAX_FIELD_SIZE)
280 gdb_xml_error (parser,
281 _("Flags size %s is larger than maximum (%d)"),
282 pulongest (size), MAX_FIELD_SIZE);
285 data->current_type = tdesc_create_flags (data->current_feature, id, size);
286 data->current_type_size = size;
289 static void
290 tdesc_start_enum (struct gdb_xml_parser *parser,
291 const struct gdb_xml_element *element,
292 void *user_data, std::vector<gdb_xml_value> &attributes)
294 struct tdesc_parsing_data *data = (struct tdesc_parsing_data *) user_data;
295 char *id = (char *) xml_find_attribute (attributes, "id")->value.get ();
296 int size = * (ULONGEST *)
297 xml_find_attribute (attributes, "size")->value.get ();
299 if (size > MAX_FIELD_SIZE)
301 gdb_xml_error (parser,
302 _("Enum size %s is larger than maximum (%d)"),
303 pulongest (size), MAX_FIELD_SIZE);
306 data->current_type = tdesc_create_enum (data->current_feature, id, size);
307 data->current_type_size = 0;
310 /* Handle the start of a <field> element. Attach the field to the
311 current struct, union or flags. */
313 static void
314 tdesc_start_field (struct gdb_xml_parser *parser,
315 const struct gdb_xml_element *element,
316 void *user_data, std::vector<gdb_xml_value> &attributes)
318 struct tdesc_parsing_data *data = (struct tdesc_parsing_data *) user_data;
319 struct gdb_xml_value *attr;
320 struct tdesc_type *field_type;
321 char *field_name, *field_type_id;
322 int start, end;
324 field_name = (char *) xml_find_attribute (attributes, "name")->value.get ();
326 attr = xml_find_attribute (attributes, "type");
327 if (attr != NULL)
329 field_type_id = (char *) attr->value.get ();
330 field_type = tdesc_named_type (data->current_feature, field_type_id);
332 else
334 field_type_id = NULL;
335 field_type = NULL;
338 attr = xml_find_attribute (attributes, "start");
339 if (attr != NULL)
341 ULONGEST ul_start = * (ULONGEST *) attr->value.get ();
343 if (ul_start > MAX_FIELD_BITSIZE)
345 gdb_xml_error (parser,
346 _("Field start %s is larger than maximum (%d)"),
347 pulongest (ul_start), MAX_FIELD_BITSIZE);
349 start = ul_start;
351 else
352 start = -1;
354 attr = xml_find_attribute (attributes, "end");
355 if (attr != NULL)
357 ULONGEST ul_end = * (ULONGEST *) attr->value.get ();
359 if (ul_end > MAX_FIELD_BITSIZE)
361 gdb_xml_error (parser,
362 _("Field end %s is larger than maximum (%d)"),
363 pulongest (ul_end), MAX_FIELD_BITSIZE);
365 end = ul_end;
367 else
368 end = -1;
370 if (start != -1)
372 tdesc_type_with_fields *t = data->current_type;
374 /* Older versions of gdb can't handle elided end values.
375 Stick with that for now, to help ensure backward compatibility.
376 E.g., If a newer gdbserver is talking to an older gdb. */
377 if (end == -1)
378 gdb_xml_error (parser, _("Missing end value"));
380 if (data->current_type_size == 0)
381 gdb_xml_error (parser,
382 _("Bitfields must live in explicitly sized types"));
384 if (field_type_id != NULL
385 && strcmp (field_type_id, "bool") == 0
386 && start != end)
388 gdb_xml_error (parser,
389 _("Boolean fields must be one bit in size"));
392 if (end >= 64)
393 gdb_xml_error (parser,
394 _("Bitfield \"%s\" goes past "
395 "64 bits (unsupported)"),
396 field_name);
398 /* Assume that the bit numbering in XML is "lsb-zero". Most
399 architectures other than PowerPC use this ordering. In the
400 future, we can add an XML tag to indicate "msb-zero" numbering. */
401 if (start > end)
402 gdb_xml_error (parser, _("Bitfield \"%s\" has start after end"),
403 field_name);
404 if (end >= data->current_type_size * TARGET_CHAR_BIT)
405 gdb_xml_error (parser, _("Bitfield \"%s\" does not fit in struct"),
406 field_name);
408 if (field_type != NULL)
409 tdesc_add_typed_bitfield (t, field_name, start, end, field_type);
410 else if (start == end)
411 tdesc_add_flag (t, start, field_name);
412 else
413 tdesc_add_bitfield (t, field_name, start, end);
415 else if (start == -1 && end != -1)
416 gdb_xml_error (parser, _("End specified but not start"));
417 else if (field_type_id != NULL)
419 /* TDESC_TYPE_FLAGS values are explicitly sized, so the following test
420 catches adding non-bitfield types to flags as well. */
421 if (data->current_type_size != 0)
422 gdb_xml_error (parser,
423 _("Explicitly sized type cannot "
424 "contain non-bitfield \"%s\""),
425 field_name);
427 if (field_type == NULL)
428 gdb_xml_error (parser, _("Field \"%s\" references undefined "
429 "type \"%s\""),
430 field_name, field_type_id);
432 tdesc_add_field (data->current_type, field_name, field_type);
434 else
435 gdb_xml_error (parser, _("Field \"%s\" has neither type nor bit position"),
436 field_name);
439 /* Handle the start of an <evalue> element. Attach the value to the
440 current enum. */
442 static void
443 tdesc_start_enum_value (struct gdb_xml_parser *parser,
444 const struct gdb_xml_element *element,
445 void *user_data, std::vector<gdb_xml_value> &attributes)
447 struct tdesc_parsing_data *data = (struct tdesc_parsing_data *) user_data;
448 struct gdb_xml_value *attr;
449 char *field_name;
450 ULONGEST ul_value;
451 int value;
453 field_name = (char *) xml_find_attribute (attributes, "name")->value.get ();
455 attr = xml_find_attribute (attributes, "value");
456 ul_value = * (ULONGEST *) attr->value.get ();
457 if (ul_value > INT_MAX)
459 gdb_xml_error (parser,
460 _("Enum value %s is larger than maximum (%d)"),
461 pulongest (ul_value), INT_MAX);
463 value = ul_value;
465 tdesc_add_enum_value (data->current_type, value, field_name);
468 /* Handle the start of a <vector> element. Initialize the type and
469 record it with the current feature. */
471 static void
472 tdesc_start_vector (struct gdb_xml_parser *parser,
473 const struct gdb_xml_element *element,
474 void *user_data, std::vector<gdb_xml_value> &attributes)
476 struct tdesc_parsing_data *data = (struct tdesc_parsing_data *) user_data;
477 struct tdesc_type *field_type;
478 char *id, *field_type_id;
479 ULONGEST count;
481 id = (char *) attributes[0].value.get ();
482 field_type_id = (char *) attributes[1].value.get ();
483 count = * (ULONGEST *) attributes[2].value.get ();
485 if (count > MAX_VECTOR_SIZE)
487 gdb_xml_error (parser,
488 _("Vector size %s is larger than maximum (%d)"),
489 pulongest (count), MAX_VECTOR_SIZE);
492 field_type = tdesc_named_type (data->current_feature, field_type_id);
493 if (field_type == NULL)
494 gdb_xml_error (parser, _("Vector \"%s\" references undefined type \"%s\""),
495 id, field_type_id);
497 tdesc_create_vector (data->current_feature, id, field_type, count);
500 /* The elements and attributes of an XML target description. */
502 static const struct gdb_xml_attribute field_attributes[] = {
503 { "name", GDB_XML_AF_NONE, NULL, NULL },
504 { "type", GDB_XML_AF_OPTIONAL, NULL, NULL },
505 { "start", GDB_XML_AF_OPTIONAL, gdb_xml_parse_attr_ulongest, NULL },
506 { "end", GDB_XML_AF_OPTIONAL, gdb_xml_parse_attr_ulongest, NULL },
507 { NULL, GDB_XML_AF_NONE, NULL, NULL }
510 static const struct gdb_xml_attribute enum_value_attributes[] = {
511 { "name", GDB_XML_AF_NONE, NULL, NULL },
512 { "value", GDB_XML_AF_OPTIONAL, gdb_xml_parse_attr_ulongest, NULL },
513 { NULL, GDB_XML_AF_NONE, NULL, NULL }
516 static const struct gdb_xml_element struct_union_children[] = {
517 { "field", field_attributes, NULL, GDB_XML_EF_REPEATABLE,
518 tdesc_start_field, NULL },
519 { NULL, NULL, NULL, GDB_XML_EF_NONE, NULL, NULL }
522 static const struct gdb_xml_element enum_children[] = {
523 { "evalue", enum_value_attributes, NULL, GDB_XML_EF_REPEATABLE,
524 tdesc_start_enum_value, NULL },
525 { NULL, NULL, NULL, GDB_XML_EF_NONE, NULL, NULL }
528 static const struct gdb_xml_attribute reg_attributes[] = {
529 { "name", GDB_XML_AF_NONE, NULL, NULL },
530 { "bitsize", GDB_XML_AF_NONE, gdb_xml_parse_attr_ulongest, NULL },
531 { "regnum", GDB_XML_AF_OPTIONAL, gdb_xml_parse_attr_ulongest, NULL },
532 { "type", GDB_XML_AF_OPTIONAL, NULL, NULL },
533 { "group", GDB_XML_AF_OPTIONAL, NULL, NULL },
534 { "save-restore", GDB_XML_AF_OPTIONAL,
535 gdb_xml_parse_attr_enum, gdb_xml_enums_boolean },
536 { NULL, GDB_XML_AF_NONE, NULL, NULL }
539 static const struct gdb_xml_attribute struct_union_attributes[] = {
540 { "id", GDB_XML_AF_NONE, NULL, NULL },
541 { "size", GDB_XML_AF_OPTIONAL, gdb_xml_parse_attr_ulongest, NULL},
542 { NULL, GDB_XML_AF_NONE, NULL, NULL }
545 static const struct gdb_xml_attribute flags_attributes[] = {
546 { "id", GDB_XML_AF_NONE, NULL, NULL },
547 { "size", GDB_XML_AF_NONE, gdb_xml_parse_attr_ulongest, NULL},
548 { NULL, GDB_XML_AF_NONE, NULL, NULL }
551 static const struct gdb_xml_attribute enum_attributes[] = {
552 { "id", GDB_XML_AF_NONE, NULL, NULL },
553 { "size", GDB_XML_AF_NONE, gdb_xml_parse_attr_ulongest, NULL},
554 { NULL, GDB_XML_AF_NONE, NULL, NULL }
557 static const struct gdb_xml_attribute vector_attributes[] = {
558 { "id", GDB_XML_AF_NONE, NULL, NULL },
559 { "type", GDB_XML_AF_NONE, NULL, NULL },
560 { "count", GDB_XML_AF_NONE, gdb_xml_parse_attr_ulongest, NULL },
561 { NULL, GDB_XML_AF_NONE, NULL, NULL }
564 static const struct gdb_xml_attribute feature_attributes[] = {
565 { "name", GDB_XML_AF_NONE, NULL, NULL },
566 { NULL, GDB_XML_AF_NONE, NULL, NULL }
569 static const struct gdb_xml_element feature_children[] = {
570 { "reg", reg_attributes, NULL,
571 GDB_XML_EF_OPTIONAL | GDB_XML_EF_REPEATABLE,
572 tdesc_start_reg, NULL },
573 { "struct", struct_union_attributes, struct_union_children,
574 GDB_XML_EF_OPTIONAL | GDB_XML_EF_REPEATABLE,
575 tdesc_start_struct, NULL },
576 { "union", struct_union_attributes, struct_union_children,
577 GDB_XML_EF_OPTIONAL | GDB_XML_EF_REPEATABLE,
578 tdesc_start_union, NULL },
579 { "flags", flags_attributes, struct_union_children,
580 GDB_XML_EF_OPTIONAL | GDB_XML_EF_REPEATABLE,
581 tdesc_start_flags, NULL },
582 { "enum", enum_attributes, enum_children,
583 GDB_XML_EF_OPTIONAL | GDB_XML_EF_REPEATABLE,
584 tdesc_start_enum, NULL },
585 { "vector", vector_attributes, NULL,
586 GDB_XML_EF_OPTIONAL | GDB_XML_EF_REPEATABLE,
587 tdesc_start_vector, NULL },
588 { NULL, NULL, NULL, GDB_XML_EF_NONE, NULL, NULL }
591 static const struct gdb_xml_attribute target_attributes[] = {
592 { "version", GDB_XML_AF_NONE, NULL, NULL },
593 { NULL, GDB_XML_AF_NONE, NULL, NULL }
596 static const struct gdb_xml_element target_children[] = {
597 { "architecture", NULL, NULL, GDB_XML_EF_OPTIONAL,
598 NULL, tdesc_end_arch },
599 { "osabi", NULL, NULL, GDB_XML_EF_OPTIONAL,
600 NULL, tdesc_end_osabi },
601 { "compatible", NULL, NULL, GDB_XML_EF_OPTIONAL | GDB_XML_EF_REPEATABLE,
602 NULL, tdesc_end_compatible },
603 { "feature", feature_attributes, feature_children,
604 GDB_XML_EF_OPTIONAL | GDB_XML_EF_REPEATABLE,
605 tdesc_start_feature, NULL },
606 { NULL, NULL, NULL, GDB_XML_EF_NONE, NULL, NULL }
609 static const struct gdb_xml_element tdesc_elements[] = {
610 { "target", target_attributes, target_children, GDB_XML_EF_NONE,
611 tdesc_start_target, NULL },
612 { NULL, NULL, NULL, GDB_XML_EF_NONE, NULL, NULL }
615 /* Parse DOCUMENT into a target description and return it. */
617 static struct target_desc *
618 tdesc_parse_xml (const char *document, xml_fetch_another fetcher)
620 struct tdesc_parsing_data data;
622 /* Expand all XInclude directives. */
623 std::string expanded_text;
625 if (!xml_process_xincludes (expanded_text,
626 _("target description"),
627 document, fetcher, 0))
629 warning (_("Could not load XML target description; ignoring"));
630 return NULL;
633 /* Check for an exact match in the list of descriptions we have
634 previously parsed. */
635 const auto it = xml_cache.find (expanded_text);
636 if (it != xml_cache.end ())
637 return it->second.get ();
639 memset (&data, 0, sizeof (struct tdesc_parsing_data));
640 target_desc_up description = allocate_target_description ();
641 data.tdesc = description.get ();
643 if (gdb_xml_parse_quick (_("target description"), "gdb-target.dtd",
644 tdesc_elements, expanded_text.c_str (), &data) == 0)
646 /* Parsed successfully. */
647 xml_cache.emplace (std::move (expanded_text), std::move (description));
648 return data.tdesc;
650 else
652 warning (_("Could not load XML target description; ignoring"));
653 return NULL;
656 #endif /* HAVE_LIBEXPAT */
659 /* Read an XML target description from FILENAME. Parse it, and return
660 the parsed description. */
662 const struct target_desc *
663 file_read_description_xml (const char *filename)
665 std::optional<gdb::char_vector> tdesc_str
666 = xml_fetch_content_from_file (filename, NULL);
667 if (!tdesc_str)
669 warning (_("Could not open \"%s\""), filename);
670 return NULL;
673 const std::string dirname = ldirname (filename);
674 auto fetch_another = [&dirname] (const char *name)
676 return xml_fetch_content_from_file (name, dirname.c_str ());
679 return tdesc_parse_xml (tdesc_str->data (), fetch_another);
682 /* Read a string representation of available features from the target,
683 using TARGET_OBJECT_AVAILABLE_FEATURES. The returned string is
684 malloc allocated and NUL-terminated. NAME should be a non-NULL
685 string identifying the XML document we want; the top level document
686 is "target.xml". Other calls may be performed for the DTD or
687 for <xi:include>. */
689 static std::optional<gdb::char_vector>
690 fetch_available_features_from_target (const char *name, target_ops *ops)
692 /* Read this object as a string. This ensures that a NUL
693 terminator is added. */
694 return target_read_stralloc (ops,
695 TARGET_OBJECT_AVAILABLE_FEATURES,
696 name);
700 /* Read an XML target description using OPS. Parse it, and return the
701 parsed description. */
703 const struct target_desc *
704 target_read_description_xml (struct target_ops *ops)
706 std::optional<gdb::char_vector> tdesc_str
707 = fetch_available_features_from_target ("target.xml", ops);
708 if (!tdesc_str)
709 return NULL;
711 auto fetch_another = [ops] (const char *name)
713 return fetch_available_features_from_target (name, ops);
716 return tdesc_parse_xml (tdesc_str->data (), fetch_another);
719 /* Fetches an XML target description using OPS, processing
720 includes, but not parsing it. Used to dump whole tdesc
721 as a single XML file. */
723 std::optional<std::string>
724 target_fetch_description_xml (struct target_ops *ops)
726 #if !defined(HAVE_LIBEXPAT)
727 static int have_warned;
729 if (!have_warned)
731 have_warned = 1;
732 warning (_("Can not fetch XML target description; XML support was "
733 "disabled at compile time"));
736 return {};
737 #else
738 std::optional<gdb::char_vector>
739 tdesc_str = fetch_available_features_from_target ("target.xml", ops);
740 if (!tdesc_str)
741 return {};
743 auto fetch_another = [ops] (const char *name)
745 return fetch_available_features_from_target (name, ops);
747 std::string output;
748 if (!xml_process_xincludes (output,
749 _("target description"),
750 tdesc_str->data (), fetch_another, 0))
752 warning (_("Could not load XML target description; ignoring"));
753 return {};
755 return output;
756 #endif
759 /* See xml-tdesc.h. */
761 const struct target_desc *
762 string_read_description_xml (const char *xml)
764 return tdesc_parse_xml (xml, [] (const char *href)
766 error (_("xincludes are unsupported with this method"));
767 return std::optional<gdb::char_vector> ();