GDB (xrefs)

/tmp/gdb-8.1/gdb/xml-tdesc.c
Go to the documentation of this file.
1 /* XML target description support for GDB.
2
3 Copyright (C) 2006-2018 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 "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>
31
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
37
38 #if !defined(HAVE_LIBEXPAT)
39
40 /* Parse DOCUMENT into a target description. Or don't, since we don't have
41 an XML parser. */
42
43 static struct target_desc *
44 tdesc_parse_xml (const char *document, xml_fetch_another fetcher,
45 void *fetcher_baton)
46 {
47 static int have_warned;
48
49 if (!have_warned)
50 {
51 have_warned = 1;
52 warning (_("Can not parse XML target description; XML support was "
53 "disabled at compile time"));
54 }
55
56 return NULL;
57 }
58
59 #else /* HAVE_LIBEXPAT */
60
61 /* A record of every XML description we have parsed. We never discard
62 old descriptions, because we never discard gdbarches. As long as we
63 have a gdbarch referencing this description, we want to have a copy
64 of it here, so that if we parse the same XML document again we can
65 return the same "struct target_desc *"; if they are not singletons,
66 then we will create unnecessary duplicate gdbarches. See
67 gdbarch_list_lookup_by_info. */
68
69 static std::unordered_map<std::string, target_desc *> xml_cache;
70
71 /* Callback data for target description parsing. */
72
73 struct tdesc_parsing_data
74 {
75 /* The target description we are building. */
76 struct target_desc *tdesc;
77
78 /* The target feature we are currently parsing, or last parsed. */
79 struct tdesc_feature *current_feature;
80
81 /* The register number to use for the next register we see, if
82 it does not have its own. This starts at zero. */
83 int next_regnum;
84
85 /* The struct or union we are currently parsing, or last parsed. */
86 tdesc_type_with_fields *current_type;
87
88 /* The byte size of the current struct/flags type, if specified. Zero
89 if not specified. Flags values must specify a size. */
90 int current_type_size;
91 };
92
93 /* Handle the end of an <architecture> element and its value. */
94
95 static void
96 tdesc_end_arch (struct gdb_xml_parser *parser,
97 const struct gdb_xml_element *element,
98 void *user_data, const char *body_text)
99 {
100 struct tdesc_parsing_data *data = (struct tdesc_parsing_data *) user_data;
101 const struct bfd_arch_info *arch;
102
103 arch = bfd_scan_arch (body_text);
104 if (arch == NULL)
105 gdb_xml_error (parser, _("Target description specified unknown "
106 "architecture \"%s\""), body_text);
107 set_tdesc_architecture (data->tdesc, arch);
108 }
109
110 /* Handle the end of an <osabi> element and its value. */
111
112 static void
113 tdesc_end_osabi (struct gdb_xml_parser *parser,
114 const struct gdb_xml_element *element,
115 void *user_data, const char *body_text)
116 {
117 struct tdesc_parsing_data *data = (struct tdesc_parsing_data *) user_data;
118 enum gdb_osabi osabi;
119
120 osabi = osabi_from_tdesc_string (body_text);
121 if (osabi == GDB_OSABI_UNKNOWN)
122 warning (_("Target description specified unknown osabi \"%s\""),
123 body_text);
124 else
125 set_tdesc_osabi (data->tdesc, osabi);
126 }
127
128 /* Handle the end of a <compatible> element and its value. */
129
130 static void
131 tdesc_end_compatible (struct gdb_xml_parser *parser,
132 const struct gdb_xml_element *element,
133 void *user_data, const char *body_text)
134 {
135 struct tdesc_parsing_data *data = (struct tdesc_parsing_data *) user_data;
136 const struct bfd_arch_info *arch;
137
138 arch = bfd_scan_arch (body_text);
139 tdesc_add_compatible (data->tdesc, arch);
140 }
141
142 /* Handle the start of a <target> element. */
143
144 static void
145 tdesc_start_target (struct gdb_xml_parser *parser,
146 const struct gdb_xml_element *element,
147 void *user_data, VEC(gdb_xml_value_s) *attributes)
148 {
149 char *version = (char *) xml_find_attribute (attributes, "version")->value;
150
151 if (strcmp (version, "1.0") != 0)
152 gdb_xml_error (parser,
153 _("Target description has unsupported version \"%s\""),
154 version);
155 }
156
157 /* Handle the start of a <feature> element. */
158
159 static void
160 tdesc_start_feature (struct gdb_xml_parser *parser,
161 const struct gdb_xml_element *element,
162 void *user_data, VEC(gdb_xml_value_s) *attributes)
163 {
164 struct tdesc_parsing_data *data = (struct tdesc_parsing_data *) user_data;
165 char *name = (char *) xml_find_attribute (attributes, "name")->value;
166
167 data->current_feature = tdesc_create_feature (data->tdesc, name);
168 }
169
170 /* Handle the start of a <reg> element. Fill in the optional
171 attributes and attach it to the containing feature. */
172
173 static void
174 tdesc_start_reg (struct gdb_xml_parser *parser,
175 const struct gdb_xml_element *element,
176 void *user_data, VEC(gdb_xml_value_s) *attributes)
177 {
178 struct tdesc_parsing_data *data = (struct tdesc_parsing_data *) user_data;
179 struct gdb_xml_value *attrs = VEC_address (gdb_xml_value_s, attributes);
180 int ix = 0, length;
181 char *name, *group;
182 const char *type;
183 int bitsize, regnum, save_restore;
184
185 length = VEC_length (gdb_xml_value_s, attributes);
186
187 name = (char *) attrs[ix++].value;
188 bitsize = * (ULONGEST *) attrs[ix++].value;
189
190 if (ix < length && strcmp (attrs[ix].name, "regnum") == 0)
191 regnum = * (ULONGEST *) attrs[ix++].value;
192 else
193 regnum = data->next_regnum;
194
195 if (ix < length && strcmp (attrs[ix].name, "type") == 0)
196 type = (char *) attrs[ix++].value;
197 else
198 type = "int";
199
200 if (ix < length && strcmp (attrs[ix].name, "group") == 0)
201 group = (char *) attrs[ix++].value;
202 else
203 group = NULL;
204
205 if (ix < length && strcmp (attrs[ix].name, "save-restore") == 0)
206 save_restore = * (ULONGEST *) attrs[ix++].value;
207 else
208 save_restore = 1;
209
210 if (strcmp (type, "int") != 0
211 && strcmp (type, "float") != 0
212 && tdesc_named_type (data->current_feature, type) == NULL)
213 gdb_xml_error (parser, _("Register \"%s\" has unknown type \"%s\""),
214 name, type);
215
216 tdesc_create_reg (data->current_feature, name, regnum, save_restore, group,
217 bitsize, type);
218
219 data->next_regnum = regnum + 1;
220 }
221
222 /* Handle the start of a <union> element. Initialize the type and
223 record it with the current feature. */
224
225 static void
226 tdesc_start_union (struct gdb_xml_parser *parser,
227 const struct gdb_xml_element *element,
228 void *user_data, VEC(gdb_xml_value_s) *attributes)
229 {
230 struct tdesc_parsing_data *data = (struct tdesc_parsing_data *) user_data;
231 char *id = (char *) xml_find_attribute (attributes, "id")->value;
232
233 data->current_type = tdesc_create_union (data->current_feature, id);
234 data->current_type_size = 0;
235 }
236
237 /* Handle the start of a <struct> element. Initialize the type and
238 record it with the current feature. */
239
240 static void
241 tdesc_start_struct (struct gdb_xml_parser *parser,
242 const struct gdb_xml_element *element,
243 void *user_data, VEC(gdb_xml_value_s) *attributes)
244 {
245 struct tdesc_parsing_data *data = (struct tdesc_parsing_data *) user_data;
246 char *id = (char *) xml_find_attribute (attributes, "id")->value;
247 struct gdb_xml_value *attr;
248
249 tdesc_type_with_fields *type_with_fields
250 = tdesc_create_struct (data->current_feature, id);
251 data->current_type = type_with_fields;
252 data->current_type_size = 0;
253
254 attr = xml_find_attribute (attributes, "size");
255 if (attr != NULL)
256 {
257 ULONGEST size = * (ULONGEST *) attr->value;
258
259 if (size > MAX_FIELD_SIZE)
260 {
261 gdb_xml_error (parser,
262 _("Struct size %s is larger than maximum (%d)"),
263 pulongest (size), MAX_FIELD_SIZE);
264 }
265 tdesc_set_struct_size (type_with_fields, size);
266 data->current_type_size = size;
267 }
268 }
269
270 static void
271 tdesc_start_flags (struct gdb_xml_parser *parser,
272 const struct gdb_xml_element *element,
273 void *user_data, VEC(gdb_xml_value_s) *attributes)
274 {
275 struct tdesc_parsing_data *data = (struct tdesc_parsing_data *) user_data;
276 char *id = (char *) xml_find_attribute (attributes, "id")->value;
277 ULONGEST size = * (ULONGEST *)
278 xml_find_attribute (attributes, "size")->value;
279
280 if (size > MAX_FIELD_SIZE)
281 {
282 gdb_xml_error (parser,
283 _("Flags size %s is larger than maximum (%d)"),
284 pulongest (size), MAX_FIELD_SIZE);
285 }
286
287 data->current_type = tdesc_create_flags (data->current_feature, id, size);
288 data->current_type_size = size;
289 }
290
291 static void
292 tdesc_start_enum (struct gdb_xml_parser *parser,
293 const struct gdb_xml_element *element,
294 void *user_data, VEC(gdb_xml_value_s) *attributes)
295 {
296 struct tdesc_parsing_data *data = (struct tdesc_parsing_data *) user_data;
297 char *id = (char *) xml_find_attribute (attributes, "id")->value;
298 int size = * (ULONGEST *)
299 xml_find_attribute (attributes, "size")->value;
300
301 if (size > MAX_FIELD_SIZE)
302 {
303 gdb_xml_error (parser,
304 _("Enum size %s is larger than maximum (%d)"),
305 pulongest (size), MAX_FIELD_SIZE);
306 }
307
308 data->current_type = tdesc_create_enum (data->current_feature, id, size);
309 data->current_type_size = 0;
310 }
311
312 /* Handle the start of a <field> element. Attach the field to the
313 current struct, union or flags. */
314
315 static void
316 tdesc_start_field (struct gdb_xml_parser *parser,
317 const struct gdb_xml_element *element,
318 void *user_data, VEC(gdb_xml_value_s) *attributes)
319 {
320 struct tdesc_parsing_data *data = (struct tdesc_parsing_data *) user_data;
321 struct gdb_xml_value *attr;
322 struct tdesc_type *field_type;
323 char *field_name, *field_type_id;
324 int start, end;
325
326 field_name = (char *) xml_find_attribute (attributes, "name")->value;
327
328 attr = xml_find_attribute (attributes, "type");
329 if (attr != NULL)
330 {
331 field_type_id = (char *) attr->value;
332 field_type = tdesc_named_type (data->current_feature, field_type_id);
333 }
334 else
335 {
336 field_type_id = NULL;
337 field_type = NULL;
338 }
339
340 attr = xml_find_attribute (attributes, "start");
341 if (attr != NULL)
342 {
343 ULONGEST ul_start = * (ULONGEST *) attr->value;
344
345 if (ul_start > MAX_FIELD_BITSIZE)
346 {
347 gdb_xml_error (parser,
348 _("Field start %s is larger than maximum (%d)"),
349 pulongest (ul_start), MAX_FIELD_BITSIZE);
350 }
351 start = ul_start;
352 }
353 else
354 start = -1;
355
356 attr = xml_find_attribute (attributes, "end");
357 if (attr != NULL)
358 {
359 ULONGEST ul_end = * (ULONGEST *) attr->value;
360
361 if (ul_end > MAX_FIELD_BITSIZE)
362 {
363 gdb_xml_error (parser,
364 _("Field end %s is larger than maximum (%d)"),
365 pulongest (ul_end), MAX_FIELD_BITSIZE);
366 }
367 end = ul_end;
368 }
369 else
370 end = -1;
371
372 if (start != -1)
373 {
374 tdesc_type_with_fields *t = data->current_type;
375
376 /* Older versions of gdb can't handle elided end values.
377 Stick with that for now, to help ensure backward compatibility.
378 E.g., If a newer gdbserver is talking to an older gdb. */
379 if (end == -1)
380 gdb_xml_error (parser, _("Missing end value"));
381
382 if (data->current_type_size == 0)
383 gdb_xml_error (parser,
384 _("Bitfields must live in explicitly sized types"));
385
386 if (field_type_id != NULL
387 && strcmp (field_type_id, "bool") == 0
388 && start != end)
389 {
390 gdb_xml_error (parser,
391 _("Boolean fields must be one bit in size"));
392 }
393
394 if (end >= 64)
395 gdb_xml_error (parser,
396 _("Bitfield \"%s\" goes past "
397 "64 bits (unsupported)"),
398 field_name);
399
400 /* Assume that the bit numbering in XML is "lsb-zero". Most
401 architectures other than PowerPC use this ordering. In the
402 future, we can add an XML tag to indicate "msb-zero" numbering. */
403 if (start > end)
404 gdb_xml_error (parser, _("Bitfield \"%s\" has start after end"),
405 field_name);
406 if (end >= data->current_type_size * TARGET_CHAR_BIT)
407 gdb_xml_error (parser, _("Bitfield \"%s\" does not fit in struct"),
408 field_name);
409
410 if (field_type != NULL)
411 tdesc_add_typed_bitfield (t, field_name, start, end, field_type);
412 else if (start == end)
413 tdesc_add_flag (t, start, field_name);
414 else
415 tdesc_add_bitfield (t, field_name, start, end);
416 }
417 else if (start == -1 && end != -1)
418 gdb_xml_error (parser, _("End specified but not start"));
419 else if (field_type_id != NULL)
420 {
421 /* TDESC_TYPE_FLAGS values are explicitly sized, so the following test
422 catches adding non-bitfield types to flags as well. */
423 if (data->current_type_size != 0)
424 gdb_xml_error (parser,
425 _("Explicitly sized type cannot "
426 "contain non-bitfield \"%s\""),
427 field_name);
428
429 if (field_type == NULL)
430 gdb_xml_error (parser, _("Field \"%s\" references undefined "
431 "type \"%s\""),
432 field_name, field_type_id);
433
434 tdesc_add_field (data->current_type, field_name, field_type);
435 }
436 else
437 gdb_xml_error (parser, _("Field \"%s\" has neither type nor bit position"),
438 field_name);
439 }
440
441 /* Handle the start of an <evalue> element. Attach the value to the
442 current enum. */
443
444 static void
445 tdesc_start_enum_value (struct gdb_xml_parser *parser,
446 const struct gdb_xml_element *element,
447 void *user_data, VEC(gdb_xml_value_s) *attributes)
448 {
449 struct tdesc_parsing_data *data = (struct tdesc_parsing_data *) user_data;
450 struct gdb_xml_value *attr;
451 char *field_name;
452 ULONGEST ul_value;
453 int value;
454
455 field_name = (char *) xml_find_attribute (attributes, "name")->value;
456
457 attr = xml_find_attribute (attributes, "value");
458 ul_value = * (ULONGEST *) attr->value;
459 if (ul_value > INT_MAX)
460 {
461 gdb_xml_error (parser,
462 _("Enum value %s is larger than maximum (%d)"),
463 pulongest (ul_value), INT_MAX);
464 }
465 value = ul_value;
466
467 tdesc_add_enum_value (data->current_type, value, field_name);
468 }
469
470 /* Handle the start of a <vector> element. Initialize the type and
471 record it with the current feature. */
472
473 static void
474 tdesc_start_vector (struct gdb_xml_parser *parser,
475 const struct gdb_xml_element *element,
476 void *user_data, VEC(gdb_xml_value_s) *attributes)
477 {
478 struct tdesc_parsing_data *data = (struct tdesc_parsing_data *) user_data;
479 struct gdb_xml_value *attrs = VEC_address (gdb_xml_value_s, attributes);
480 struct tdesc_type *field_type;
481 char *id, *field_type_id;
482 ULONGEST count;
483
484 id = (char *) attrs[0].value;
485 field_type_id = (char *) attrs[1].value;
486 count = * (ULONGEST *) attrs[2].value;
487
488 if (count > MAX_VECTOR_SIZE)
489 {
490 gdb_xml_error (parser,
491 _("Vector size %s is larger than maximum (%d)"),
492 pulongest (count), MAX_VECTOR_SIZE);
493 }
494
495 field_type = tdesc_named_type (data->current_feature, field_type_id);
496 if (field_type == NULL)
497 gdb_xml_error (parser, _("Vector \"%s\" references undefined type \"%s\""),
498 id, field_type_id);
499
500 tdesc_create_vector (data->current_feature, id, field_type, count);
501 }
502
503 /* The elements and attributes of an XML target description. */
504
505 static const struct gdb_xml_attribute field_attributes[] = {
506 { "name", GDB_XML_AF_NONE, NULL, NULL },
507 { "type", GDB_XML_AF_OPTIONAL, NULL, NULL },
508 { "start", GDB_XML_AF_OPTIONAL, gdb_xml_parse_attr_ulongest, NULL },
509 { "end", GDB_XML_AF_OPTIONAL, gdb_xml_parse_attr_ulongest, NULL },
510 { NULL, GDB_XML_AF_NONE, NULL, NULL }
511 };
512
513 static const struct gdb_xml_attribute enum_value_attributes[] = {
514 { "name", GDB_XML_AF_NONE, NULL, NULL },
515 { "value", GDB_XML_AF_OPTIONAL, gdb_xml_parse_attr_ulongest, NULL },
516 { NULL, GDB_XML_AF_NONE, NULL, NULL }
517 };
518
519 static const struct gdb_xml_element struct_union_children[] = {
520 { "field", field_attributes, NULL, GDB_XML_EF_REPEATABLE,
521 tdesc_start_field, NULL },
522 { NULL, NULL, NULL, GDB_XML_EF_NONE, NULL, NULL }
523 };
524
525 static const struct gdb_xml_element enum_children[] = {
526 { "evalue", enum_value_attributes, NULL, GDB_XML_EF_REPEATABLE,
527 tdesc_start_enum_value, NULL },
528 { NULL, NULL, NULL, GDB_XML_EF_NONE, NULL, NULL }
529 };
530
531 static const struct gdb_xml_attribute reg_attributes[] = {
532 { "name", GDB_XML_AF_NONE, NULL, NULL },
533 { "bitsize", GDB_XML_AF_NONE, gdb_xml_parse_attr_ulongest, NULL },
534 { "regnum", GDB_XML_AF_OPTIONAL, gdb_xml_parse_attr_ulongest, NULL },
535 { "type", GDB_XML_AF_OPTIONAL, NULL, NULL },
536 { "group", GDB_XML_AF_OPTIONAL, NULL, NULL },
537 { "save-restore", GDB_XML_AF_OPTIONAL,
538 gdb_xml_parse_attr_enum, gdb_xml_enums_boolean },
539 { NULL, GDB_XML_AF_NONE, NULL, NULL }
540 };
541
542 static const struct gdb_xml_attribute struct_union_attributes[] = {
543 { "id", GDB_XML_AF_NONE, NULL, NULL },
544 { "size", GDB_XML_AF_OPTIONAL, gdb_xml_parse_attr_ulongest, NULL},
545 { NULL, GDB_XML_AF_NONE, NULL, NULL }
546 };
547
548 static const struct gdb_xml_attribute flags_attributes[] = {
549 { "id", GDB_XML_AF_NONE, NULL, NULL },
550 { "size", GDB_XML_AF_NONE, gdb_xml_parse_attr_ulongest, NULL},
551 { NULL, GDB_XML_AF_NONE, NULL, NULL }
552 };
553
554 static const struct gdb_xml_attribute enum_attributes[] = {
555 { "id", GDB_XML_AF_NONE, NULL, NULL },
556 { "size", GDB_XML_AF_NONE, gdb_xml_parse_attr_ulongest, NULL},
557 { NULL, GDB_XML_AF_NONE, NULL, NULL }
558 };
559
560 static const struct gdb_xml_attribute vector_attributes[] = {
561 { "id", GDB_XML_AF_NONE, NULL, NULL },
562 { "type", GDB_XML_AF_NONE, NULL, NULL },
563 { "count", GDB_XML_AF_NONE, gdb_xml_parse_attr_ulongest, NULL },
564 { NULL, GDB_XML_AF_NONE, NULL, NULL }
565 };
566
567 static const struct gdb_xml_attribute feature_attributes[] = {
568 { "name", GDB_XML_AF_NONE, NULL, NULL },
569 { NULL, GDB_XML_AF_NONE, NULL, NULL }
570 };
571
572 static const struct gdb_xml_element feature_children[] = {
573 { "reg", reg_attributes, NULL,
574 GDB_XML_EF_OPTIONAL | GDB_XML_EF_REPEATABLE,
575 tdesc_start_reg, NULL },
576 { "struct", struct_union_attributes, struct_union_children,
577 GDB_XML_EF_OPTIONAL | GDB_XML_EF_REPEATABLE,
578 tdesc_start_struct, NULL },
579 { "union", struct_union_attributes, struct_union_children,
580 GDB_XML_EF_OPTIONAL | GDB_XML_EF_REPEATABLE,
581 tdesc_start_union, NULL },
582 { "flags", flags_attributes, struct_union_children,
583 GDB_XML_EF_OPTIONAL | GDB_XML_EF_REPEATABLE,
584 tdesc_start_flags, NULL },
585 { "enum", enum_attributes, enum_children,
586 GDB_XML_EF_OPTIONAL | GDB_XML_EF_REPEATABLE,
587 tdesc_start_enum, NULL },
588 { "vector", vector_attributes, NULL,
589 GDB_XML_EF_OPTIONAL | GDB_XML_EF_REPEATABLE,
590 tdesc_start_vector, NULL },
591 { NULL, NULL, NULL, GDB_XML_EF_NONE, NULL, NULL }
592 };
593
594 static const struct gdb_xml_attribute target_attributes[] = {
595 { "version", GDB_XML_AF_NONE, NULL, NULL },
596 { NULL, GDB_XML_AF_NONE, NULL, NULL }
597 };
598
599 static const struct gdb_xml_element target_children[] = {
600 { "architecture", NULL, NULL, GDB_XML_EF_OPTIONAL,
601 NULL, tdesc_end_arch },
602 { "osabi", NULL, NULL, GDB_XML_EF_OPTIONAL,
603 NULL, tdesc_end_osabi },
604 { "compatible", NULL, NULL, GDB_XML_EF_OPTIONAL | GDB_XML_EF_REPEATABLE,
605 NULL, tdesc_end_compatible },
606 { "feature", feature_attributes, feature_children,
607 GDB_XML_EF_OPTIONAL | GDB_XML_EF_REPEATABLE,
608 tdesc_start_feature, NULL },
609 { NULL, NULL, NULL, GDB_XML_EF_NONE, NULL, NULL }
610 };
611
612 static const struct gdb_xml_element tdesc_elements[] = {
613 { "target", target_attributes, target_children, GDB_XML_EF_NONE,
614 tdesc_start_target, NULL },
615 { NULL, NULL, NULL, GDB_XML_EF_NONE, NULL, NULL }
616 };
617
618 /* Parse DOCUMENT into a target description and return it. */
619
620 static struct target_desc *
621 tdesc_parse_xml (const char *document, xml_fetch_another fetcher,
622 void *fetcher_baton)
623 {
624 struct tdesc_parsing_data data;
625
626 /* Expand all XInclude directives. */
627 std::string expanded_text;
628
629 if (!xml_process_xincludes (expanded_text,
630 _("target description"),
631 document, fetcher, fetcher_baton, 0))
632 {
633 warning (_("Could not load XML target description; ignoring"));
634 return NULL;
635 }
636
637 /* Check for an exact match in the list of descriptions we have
638 previously parsed. */
639 const auto it = xml_cache.find (expanded_text);
640 if (it != xml_cache.end ())
641 return it->second;
642
643 memset (&data, 0, sizeof (struct tdesc_parsing_data));
644 data.tdesc = allocate_target_description ();
645 struct cleanup *result_cleanup
646 = make_cleanup_free_target_description (data.tdesc);
647
648 if (gdb_xml_parse_quick (_("target description"), "gdb-target.dtd",
649 tdesc_elements, expanded_text.c_str (), &data) == 0)
650 {
651 /* Parsed successfully. */
652 xml_cache.emplace (std::move (expanded_text), data.tdesc);
653 discard_cleanups (result_cleanup);
654 return data.tdesc;
655 }
656 else
657 {
658 warning (_("Could not load XML target description; ignoring"));
659 do_cleanups (result_cleanup);
660 return NULL;
661 }
662 }
663 #endif /* HAVE_LIBEXPAT */
664
665
666 /* Read an XML target description from FILENAME. Parse it, and return
667 the parsed description. */
668
669 const struct target_desc *
670 file_read_description_xml (const char *filename)
671 {
672 gdb::unique_xmalloc_ptr<char> tdesc_str
673 = xml_fetch_content_from_file (filename, NULL);
674 if (tdesc_str == NULL)
675 {
676 warning (_("Could not open \"%s\""), filename);
677 return NULL;
678 }
679
680 return tdesc_parse_xml (tdesc_str.get (), xml_fetch_content_from_file,
681 (void *) ldirname (filename).c_str ());
682 }
683
684 /* Read a string representation of available features from the target,
685 using TARGET_OBJECT_AVAILABLE_FEATURES. The returned string is
686 malloc allocated and NUL-terminated. NAME should be a non-NULL
687 string identifying the XML document we want; the top level document
688 is "target.xml". Other calls may be performed for the DTD or
689 for <xi:include>. */
690
691 static gdb::unique_xmalloc_ptr<char>
692 fetch_available_features_from_target (const char *name, void *baton_)
693 {
694 struct target_ops *ops = (struct target_ops *) baton_;
695
696 /* Read this object as a string. This ensures that a NUL
697 terminator is added. */
698 return target_read_stralloc (ops,
699 TARGET_OBJECT_AVAILABLE_FEATURES,
700 name);
701 }
702
703
704 /* Read an XML target description using OPS. Parse it, and return the
705 parsed description. */
706
707 const struct target_desc *
708 target_read_description_xml (struct target_ops *ops)
709 {
710 gdb::unique_xmalloc_ptr<char> tdesc_str
711 = fetch_available_features_from_target ("target.xml", ops);
712 if (tdesc_str == NULL)
713 return NULL;
714
715 return tdesc_parse_xml (tdesc_str.get (),
716 fetch_available_features_from_target,
717 ops);
718 }
719
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. */
723
724 gdb::optional<std::string>
725 target_fetch_description_xml (struct target_ops *ops)
726 {
727 #if !defined(HAVE_LIBEXPAT)
728 static int have_warned;
729
730 if (!have_warned)
731 {
732 have_warned = 1;
733 warning (_("Can not fetch XML target description; XML support was "
734 "disabled at compile time"));
735 }
736
737 return {};
738 #else
739 gdb::unique_xmalloc_ptr<char>
740 tdesc_str = fetch_available_features_from_target ("target.xml", ops);
741 if (tdesc_str == NULL)
742 return {};
743
744 std::string output;
745 if (!xml_process_xincludes (output,
746 _("target description"),
747 tdesc_str.get (),
748 fetch_available_features_from_target, ops, 0))
749 {
750 warning (_("Could not load XML target description; ignoring"));
751 return {};
752 }
753 return output;
754 #endif
755 }
tdesc_add_typed_bitfield
void tdesc_add_typed_bitfield(tdesc_type_with_fields *type, const char *field_name, int start, int end, struct tdesc_type *field_type)
Definition: target-descriptions.c:1547
string
const char * string
Definition: signals.c:50
tdesc_create_enum
tdesc_type_with_fields * tdesc_create_enum(struct tdesc_feature *feature, const char *name, int size)
Definition: target-descriptions.c:1520
tdesc_type_with_fields
Definition: target-descriptions.c:355
vector_attributes
static const struct gdb_xml_attribute vector_attributes[]
Definition: xml-tdesc.c:560
gdb_xml_value::value
void * value
Definition: xml-support.h:78
tdesc_parsing_data::current_feature
struct tdesc_feature * current_feature
Definition: xml-tdesc.c:79
tdesc_end_osabi
static void tdesc_end_osabi(struct gdb_xml_parser *parser, const struct gdb_xml_element *element, void *user_data, const char *body_text)
Definition: xml-tdesc.c:113
tdesc_add_flag
void tdesc_add_flag(tdesc_type_with_fields *type, int start, const char *flag_name)
Definition: target-descriptions.c:1578
set_tdesc_osabi
void set_tdesc_osabi(target_desc *target_desc, const char *name)
Definition: target-descriptions.c:1684
if
if(!(yy_init))
Definition: ada-lex.c:1075
INT_MAX
#define INT_MAX
Definition: defs.h:477
warning
void warning(const char *fmt,...)
Definition: errors.c:26
gdb_xml_parse_attr_enum
gdb_xml_attribute_handler gdb_xml_parse_attr_enum
gdb_xml_parser
Definition: xml-support.c:68
reg_attributes
static const struct gdb_xml_attribute reg_attributes[]
Definition: xml-tdesc.c:531
gdb_xml_value
Definition: xml-support.h:75
memset
void * memset(T *s, int c, size_t n)=delete
feature_attributes
static const struct gdb_xml_attribute feature_attributes[]
Definition: xml-tdesc.c:567
set_tdesc_architecture
void set_tdesc_architecture(target_desc *target_desc, const char *name)
Definition: target-descriptions.c:1668
tdesc_elements
static const struct gdb_xml_element tdesc_elements[]
Definition: xml-tdesc.c:612
xml_fetch_another
gdb::unique_xmalloc_ptr< char >(* xml_fetch_another)(const char *href, void *baton)
Definition: xml-support.h:55
tdesc_start_feature
static void tdesc_start_feature(struct gdb_xml_parser *parser, const struct gdb_xml_element *element, void *user_data, VEC(gdb_xml_value_s) *attributes)
Definition: xml-tdesc.c:160
VEC
#define VEC(T)
Definition: vec.h:414
cleanup
Definition: cleanups.c:38
_
#define _(String)
Definition: gdb_locale.h:35
xml_cache
static std::unordered_map< std::string, target_desc * > xml_cache
Definition: xml-tdesc.c:69
defs.h
tdesc_create_reg
void tdesc_create_reg(struct tdesc_feature *feature, const char *name, int regnum, int save_restore, const char *group, int bitsize, const char *type)
Definition: target-descriptions.c:1448
GDB_OSABI_UNKNOWN
Definition: defs.h:512
GDB_XML_AF_NONE
Definition: xml-support.h:106
target_read_stralloc
gdb::unique_xmalloc_ptr< char > target_read_stralloc(struct target_ops *ops, enum target_object object, const char *annex)
Definition: target.c:1927
TARGET_OBJECT_AVAILABLE_FEATURES
Definition: target.h:166
osabi_from_tdesc_string
enum gdb_osabi osabi_from_tdesc_string(const char *name)
Definition: osabi.c:110
tdesc_start_union
static void tdesc_start_union(struct gdb_xml_parser *parser, const struct gdb_xml_element *element, void *user_data, VEC(gdb_xml_value_s) *attributes)
Definition: xml-tdesc.c:226
name
const char *const name
Definition: aarch64-tdep.c:76
make_cleanup_free_target_description
struct cleanup * make_cleanup_free_target_description(struct target_desc *target_desc)
Definition: target-descriptions.c:1627
tdesc_type
Definition: target-descriptions.c:201
GDB_XML_AF_OPTIONAL
Definition: xml-support.h:107
ldirname
std::string ldirname(const char *filename)
Definition: utils.c:3040
GDB_XML_EF_NONE
Definition: xml-support.h:127
MAX_FIELD_SIZE
#define MAX_FIELD_SIZE
Definition: xml-tdesc.c:34
gdb::unique_xmalloc_ptr
std::unique_ptr< T, xfree_deleter< T > > unique_xmalloc_ptr
Definition: gdb_unique_ptr.h:48
xml-tdesc.h
gdb_xml_parse_quick
int gdb_xml_parse_quick(const char *name, const char *dtd_name, const struct gdb_xml_element *elements, const char *document, void *user_data)
Definition: xml-support.c:647
xml_find_attribute
struct gdb_xml_value * xml_find_attribute(VEC(gdb_xml_value_s) *attributes, const char *name)
Definition: xml-support.c:231
target_ops
Definition: target.h:412
MAX_VECTOR_SIZE
#define MAX_VECTOR_SIZE
Definition: xml-tdesc.c:36
tdesc_add_enum_value
void tdesc_add_enum_value(tdesc_type_with_fields *type, int value, const char *name)
Definition: target-descriptions.c:1590
osabi.h
xml_process_xincludes
bool xml_process_xincludes(std::string &result, const char *name, const char *text, xml_fetch_another fetcher, void *fetcher_baton, int depth)
Definition: xml-support.c:905
VEC_length
#define VEC_length(T, V)
Definition: vec.h:140
pulongest
char * pulongest(ULONGEST u)
Definition: print-utils.c:129
TARGET_CHAR_BIT
#define TARGET_CHAR_BIT
Definition: host-defs.h:29
version
const char version[]
Definition: version.c:2
type
Definition: gdbtypes.h:749
tdesc_add_bitfield
void tdesc_add_bitfield(tdesc_type_with_fields *type, const char *field_name, int start, int end)
Definition: target-descriptions.c:1560
xml-support.h
type
static const char * type
Definition: language.c:113
enum_attributes
static const struct gdb_xml_attribute enum_attributes[]
Definition: xml-tdesc.c:554
tdesc_start_flags
static void tdesc_start_flags(struct gdb_xml_parser *parser, const struct gdb_xml_element *element, void *user_data, VEC(gdb_xml_value_s) *attributes)
Definition: xml-tdesc.c:271
GDB_XML_EF_REPEATABLE
Definition: xml-support.h:129
field_attributes
static const struct gdb_xml_attribute field_attributes[]
Definition: xml-tdesc.c:505
flags_attributes
static const struct gdb_xml_attribute flags_attributes[]
Definition: xml-tdesc.c:548
tdesc_feature
Definition: target-descriptions.c:516
regnum
int regnum
Definition: aarch64-tdep.c:77
tdesc_parse_xml
static struct target_desc * tdesc_parse_xml(const char *document, xml_fetch_another fetcher, void *fetcher_baton)
Definition: xml-tdesc.c:621
target_attributes
static const struct gdb_xml_attribute target_attributes[]
Definition: xml-tdesc.c:594
tdesc_start_reg
static void tdesc_start_reg(struct gdb_xml_parser *parser, const struct gdb_xml_element *element, void *user_data, VEC(gdb_xml_value_s) *attributes)
Definition: xml-tdesc.c:174
gdb_xml_element
Definition: xml-support.h:165
tdesc_start_enum_value
static void tdesc_start_enum_value(struct gdb_xml_parser *parser, const struct gdb_xml_element *element, void *user_data, VEC(gdb_xml_value_s) *attributes)
Definition: xml-tdesc.c:445
struct_union_attributes
static const struct gdb_xml_attribute struct_union_attributes[]
Definition: xml-tdesc.c:542
gdb_xml_error
void gdb_xml_error(struct gdb_xml_parser *parser, const char *format,...)
Definition: xml-support.c:219
target_fetch_description_xml
gdb::optional< std::string > target_fetch_description_xml(struct target_ops *ops)
Definition: xml-tdesc.c:725
tdesc_start_enum
static void tdesc_start_enum(struct gdb_xml_parser *parser, const struct gdb_xml_element *element, void *user_data, VEC(gdb_xml_value_s) *attributes)
Definition: xml-tdesc.c:292
value
Definition: value.c:169
gdb_xml_enums_boolean
const struct gdb_xml_enum gdb_xml_enums_boolean[]
Definition: xml-support.c:715
enum_children
static const struct gdb_xml_element enum_children[]
Definition: xml-tdesc.c:525
tdesc_start_struct
static void tdesc_start_struct(struct gdb_xml_parser *parser, const struct gdb_xml_element *element, void *user_data, VEC(gdb_xml_value_s) *attributes)
Definition: xml-tdesc.c:241
file_read_description_xml
const struct target_desc * file_read_description_xml(const char *filename)
Definition: xml-tdesc.c:670
tdesc_create_union
tdesc_type_with_fields * tdesc_create_union(struct tdesc_feature *feature, const char *name)
Definition: target-descriptions.c:1495
tdesc_start_target
static void tdesc_start_target(struct gdb_xml_parser *parser, const struct gdb_xml_element *element, void *user_data, VEC(gdb_xml_value_s) *attributes)
Definition: xml-tdesc.c:145
MAX_FIELD_BITSIZE
#define MAX_FIELD_BITSIZE
Definition: xml-tdesc.c:35
discard_cleanups
void discard_cleanups(struct cleanup *old_chain)
Definition: cleanups.c:212
target_read_description_xml
const struct target_desc * target_read_description_xml(struct target_ops *ops)
Definition: xml-tdesc.c:708
gdb::optional
Definition: gdb_optional.h:51
target_desc
Definition: target-descriptions.c:591
xml_fetch_content_from_file
gdb::unique_xmalloc_ptr< char > xml_fetch_content_from_file(const char *filename, void *baton)
Definition: xml-support.c:997
enum_value_attributes
static const struct gdb_xml_attribute enum_value_attributes[]
Definition: xml-tdesc.c:513
tdesc_create_feature
struct tdesc_feature * tdesc_create_feature(struct target_desc *tdesc, const char *name, const char *xml=nullptr)
Definition: target-descriptions.c:1602
target-descriptions.h
tdesc_create_struct
tdesc_type_with_fields * tdesc_create_struct(struct tdesc_feature *feature, const char *name)
Definition: target-descriptions.c:1473
feature_children
static const struct gdb_xml_element feature_children[]
Definition: xml-tdesc.c:572
VEC_address
#define VEC_address(T, V)
Definition: vec.h:385
target.h
gdb_xml_parse_attr_ulongest
gdb_xml_attribute_handler gdb_xml_parse_attr_ulongest
allocate_target_description
target_desc * allocate_target_description(void)
Definition: target-descriptions.c:1613
ULONGEST
unsigned long long ULONGEST
Definition: common-types.h:53
field_name
static PyObject * field_name(struct type *type, int field)
Definition: py-type.c:267
tdesc_end_arch
static void tdesc_end_arch(struct gdb_xml_parser *parser, const struct gdb_xml_element *element, void *user_data, const char *body_text)
Definition: xml-tdesc.c:96
gdb_xml_attribute
Definition: xml-support.h:114
tdesc_create_vector
struct tdesc_type * tdesc_create_vector(struct tdesc_feature *feature, const char *name, struct tdesc_type *field_type, int count)
Definition: target-descriptions.c:1461
tdesc_parsing_data::current_type_size
int current_type_size
Definition: xml-tdesc.c:90
GDB_XML_EF_OPTIONAL
Definition: xml-support.h:128
tdesc_named_type
struct tdesc_type * tdesc_named_type(const struct tdesc_feature *feature, const char *id)
Definition: target-descriptions.c:997
tdesc_parsing_data::tdesc
struct target_desc * tdesc
Definition: xml-tdesc.c:76
tdesc_parsing_data::next_regnum
int next_regnum
Definition: xml-tdesc.c:83
tdesc_end_compatible
static void tdesc_end_compatible(struct gdb_xml_parser *parser, const struct gdb_xml_element *element, void *user_data, const char *body_text)
Definition: xml-tdesc.c:131
tdesc_add_field
void tdesc_add_field(tdesc_type_with_fields *type, const char *field_name, struct tdesc_type *field_type)
Definition: target-descriptions.c:1535
tdesc_add_compatible
void tdesc_add_compatible(struct target_desc *target_desc, const struct bfd_arch_info *compatible)
Definition: target-descriptions.c:1633
tdesc_parsing_data::current_type
tdesc_type_with_fields * current_type
Definition: xml-tdesc.c:86
target_children
static const struct gdb_xml_element target_children[]
Definition: xml-tdesc.c:599
tdesc_start_field
static void tdesc_start_field(struct gdb_xml_parser *parser, const struct gdb_xml_element *element, void *user_data, VEC(gdb_xml_value_s) *attributes)
Definition: xml-tdesc.c:316
tdesc_start_vector
static void tdesc_start_vector(struct gdb_xml_parser *parser, const struct gdb_xml_element *element, void *user_data, VEC(gdb_xml_value_s) *attributes)
Definition: xml-tdesc.c:474
struct_union_children
static const struct gdb_xml_element struct_union_children[]
Definition: xml-tdesc.c:519
size
size_t size
Definition: go32-nat.c:242
tdesc_parsing_data
Definition: xml-tdesc.c:73
tdesc_create_flags
tdesc_type_with_fields * tdesc_create_flags(struct tdesc_feature *feature, const char *name, int size)
Definition: target-descriptions.c:1507
do_cleanups
void do_cleanups(struct cleanup *old_chain)
Definition: cleanups.c:174
gdb_osabi
gdb_osabi
Definition: defs.h:508
tdesc_set_struct_size
void tdesc_set_struct_size(tdesc_type_with_fields *type, int size)
Definition: target-descriptions.c:1485
fetch_available_features_from_target
static gdb::unique_xmalloc_ptr< char > fetch_available_features_from_target(const char *name, void *baton_)
Definition: xml-tdesc.c:692