1 /* XML target description support for GDB.
3 Copyright (C) 2006-2014 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/>. */
24 #include "target-descriptions.h"
25 #include "xml-support.h"
26 #include "xml-tdesc.h"
29 #include "filenames.h"
31 #if !defined(HAVE_LIBEXPAT)
33 /* Parse DOCUMENT into a target description. Or don't, since we don't have
36 static struct target_desc
*
37 tdesc_parse_xml (const char *document
, xml_fetch_another fetcher
,
40 static int have_warned
;
45 warning (_("Can not parse XML target description; XML support was "
46 "disabled at compile time"));
52 #else /* HAVE_LIBEXPAT */
54 /* A record of every XML description we have parsed. We never discard
55 old descriptions, because we never discard gdbarches. As long as we
56 have a gdbarch referencing this description, we want to have a copy
57 of it here, so that if we parse the same XML document again we can
58 return the same "struct target_desc *"; if they are not singletons,
59 then we will create unnecessary duplicate gdbarches. See
60 gdbarch_list_lookup_by_info. */
62 struct tdesc_xml_cache
64 const char *xml_document
;
65 struct target_desc
*tdesc
;
67 typedef struct tdesc_xml_cache tdesc_xml_cache_s
;
68 DEF_VEC_O(tdesc_xml_cache_s
);
70 static VEC(tdesc_xml_cache_s
) *xml_cache
;
72 /* Callback data for target description parsing. */
74 struct tdesc_parsing_data
76 /* The target description we are building. */
77 struct target_desc
*tdesc
;
79 /* The target feature we are currently parsing, or last parsed. */
80 struct tdesc_feature
*current_feature
;
82 /* The register number to use for the next register we see, if
83 it does not have its own. This starts at zero. */
86 /* The struct or union we are currently parsing, or last parsed. */
87 struct tdesc_type
*current_type
;
89 /* The byte size of the current struct type, if specified. Zero
91 int current_type_size
;
93 /* Whether the current type is a flags type. */
94 int current_type_is_flags
;
97 /* Handle the end of an <architecture> element and its value. */
100 tdesc_end_arch (struct gdb_xml_parser
*parser
,
101 const struct gdb_xml_element
*element
,
102 void *user_data
, const char *body_text
)
104 struct tdesc_parsing_data
*data
= user_data
;
105 const struct bfd_arch_info
*arch
;
107 arch
= bfd_scan_arch (body_text
);
109 gdb_xml_error (parser
, _("Target description specified unknown "
110 "architecture \"%s\""), body_text
);
111 set_tdesc_architecture (data
->tdesc
, arch
);
114 /* Handle the end of an <osabi> element and its value. */
117 tdesc_end_osabi (struct gdb_xml_parser
*parser
,
118 const struct gdb_xml_element
*element
,
119 void *user_data
, const char *body_text
)
121 struct tdesc_parsing_data
*data
= user_data
;
122 enum gdb_osabi osabi
;
124 osabi
= osabi_from_tdesc_string (body_text
);
125 if (osabi
== GDB_OSABI_UNKNOWN
)
126 warning (_("Target description specified unknown osabi \"%s\""),
129 set_tdesc_osabi (data
->tdesc
, osabi
);
132 /* Handle the end of a <compatible> element and its value. */
135 tdesc_end_compatible (struct gdb_xml_parser
*parser
,
136 const struct gdb_xml_element
*element
,
137 void *user_data
, const char *body_text
)
139 struct tdesc_parsing_data
*data
= user_data
;
140 const struct bfd_arch_info
*arch
;
142 arch
= bfd_scan_arch (body_text
);
143 tdesc_add_compatible (data
->tdesc
, arch
);
146 /* Handle the start of a <target> element. */
149 tdesc_start_target (struct gdb_xml_parser
*parser
,
150 const struct gdb_xml_element
*element
,
151 void *user_data
, VEC(gdb_xml_value_s
) *attributes
)
153 char *version
= xml_find_attribute (attributes
, "version")->value
;
155 if (strcmp (version
, "1.0") != 0)
156 gdb_xml_error (parser
,
157 _("Target description has unsupported version \"%s\""),
161 /* Handle the start of a <feature> element. */
164 tdesc_start_feature (struct gdb_xml_parser
*parser
,
165 const struct gdb_xml_element
*element
,
166 void *user_data
, VEC(gdb_xml_value_s
) *attributes
)
168 struct tdesc_parsing_data
*data
= user_data
;
169 char *name
= xml_find_attribute (attributes
, "name")->value
;
171 data
->current_feature
= tdesc_create_feature (data
->tdesc
, name
);
174 /* Handle the start of a <reg> element. Fill in the optional
175 attributes and attach it to the containing feature. */
178 tdesc_start_reg (struct gdb_xml_parser
*parser
,
179 const struct gdb_xml_element
*element
,
180 void *user_data
, VEC(gdb_xml_value_s
) *attributes
)
182 struct tdesc_parsing_data
*data
= user_data
;
183 struct gdb_xml_value
*attrs
= VEC_address (gdb_xml_value_s
, attributes
);
185 char *name
, *group
, *type
;
186 int bitsize
, regnum
, save_restore
;
188 length
= VEC_length (gdb_xml_value_s
, attributes
);
190 name
= attrs
[ix
++].value
;
191 bitsize
= * (ULONGEST
*) attrs
[ix
++].value
;
193 if (ix
< length
&& strcmp (attrs
[ix
].name
, "regnum") == 0)
194 regnum
= * (ULONGEST
*) attrs
[ix
++].value
;
196 regnum
= data
->next_regnum
;
198 if (ix
< length
&& strcmp (attrs
[ix
].name
, "type") == 0)
199 type
= attrs
[ix
++].value
;
203 if (ix
< length
&& strcmp (attrs
[ix
].name
, "group") == 0)
204 group
= attrs
[ix
++].value
;
208 if (ix
< length
&& strcmp (attrs
[ix
].name
, "save-restore") == 0)
209 save_restore
= * (ULONGEST
*) attrs
[ix
++].value
;
213 if (strcmp (type
, "int") != 0
214 && strcmp (type
, "float") != 0
215 && tdesc_named_type (data
->current_feature
, type
) == NULL
)
216 gdb_xml_error (parser
, _("Register \"%s\" has unknown type \"%s\""),
219 tdesc_create_reg (data
->current_feature
, name
, regnum
, save_restore
, group
,
222 data
->next_regnum
= regnum
+ 1;
225 /* Handle the start of a <union> element. Initialize the type and
226 record it with the current feature. */
229 tdesc_start_union (struct gdb_xml_parser
*parser
,
230 const struct gdb_xml_element
*element
,
231 void *user_data
, VEC(gdb_xml_value_s
) *attributes
)
233 struct tdesc_parsing_data
*data
= user_data
;
234 char *id
= xml_find_attribute (attributes
, "id")->value
;
236 data
->current_type
= tdesc_create_union (data
->current_feature
, id
);
237 data
->current_type_size
= 0;
238 data
->current_type_is_flags
= 0;
241 /* Handle the start of a <struct> element. Initialize the type and
242 record it with the current feature. */
245 tdesc_start_struct (struct gdb_xml_parser
*parser
,
246 const struct gdb_xml_element
*element
,
247 void *user_data
, VEC(gdb_xml_value_s
) *attributes
)
249 struct tdesc_parsing_data
*data
= user_data
;
250 char *id
= xml_find_attribute (attributes
, "id")->value
;
251 struct tdesc_type
*type
;
252 struct gdb_xml_value
*attr
;
254 type
= tdesc_create_struct (data
->current_feature
, id
);
255 data
->current_type
= type
;
256 data
->current_type_size
= 0;
257 data
->current_type_is_flags
= 0;
259 attr
= xml_find_attribute (attributes
, "size");
262 int size
= (int) * (ULONGEST
*) attr
->value
;
264 tdesc_set_struct_size (type
, size
);
265 data
->current_type_size
= size
;
270 tdesc_start_flags (struct gdb_xml_parser
*parser
,
271 const struct gdb_xml_element
*element
,
272 void *user_data
, VEC(gdb_xml_value_s
) *attributes
)
274 struct tdesc_parsing_data
*data
= user_data
;
275 char *id
= xml_find_attribute (attributes
, "id")->value
;
276 int length
= (int) * (ULONGEST
*)
277 xml_find_attribute (attributes
, "size")->value
;
278 struct tdesc_type
*type
;
280 type
= tdesc_create_flags (data
->current_feature
, id
, length
);
282 data
->current_type
= type
;
283 data
->current_type_size
= 0;
284 data
->current_type_is_flags
= 1;
287 /* Handle the start of a <field> element. Attach the field to the
288 current struct or union. */
291 tdesc_start_field (struct gdb_xml_parser
*parser
,
292 const struct gdb_xml_element
*element
,
293 void *user_data
, VEC(gdb_xml_value_s
) *attributes
)
295 struct tdesc_parsing_data
*data
= user_data
;
296 struct gdb_xml_value
*attr
;
297 struct tdesc_type
*field_type
;
298 char *field_name
, *field_type_id
;
301 field_name
= xml_find_attribute (attributes
, "name")->value
;
303 attr
= xml_find_attribute (attributes
, "type");
305 field_type_id
= attr
->value
;
307 field_type_id
= NULL
;
309 attr
= xml_find_attribute (attributes
, "start");
311 start
= * (ULONGEST
*) attr
->value
;
315 attr
= xml_find_attribute (attributes
, "end");
317 end
= * (ULONGEST
*) attr
->value
;
321 if (field_type_id
!= NULL
)
323 if (data
->current_type_is_flags
)
324 gdb_xml_error (parser
, _("Cannot add typed field \"%s\" to flags"),
326 if (data
->current_type_size
!= 0)
327 gdb_xml_error (parser
,
328 _("Explicitly sized type can not "
329 "contain non-bitfield \"%s\""),
332 field_type
= tdesc_named_type (data
->current_feature
, field_type_id
);
333 if (field_type
== NULL
)
334 gdb_xml_error (parser
, _("Field \"%s\" references undefined "
336 field_name
, field_type_id
);
338 tdesc_add_field (data
->current_type
, field_name
, field_type
);
340 else if (start
!= -1 && end
!= -1)
342 struct tdesc_type
*t
= data
->current_type
;
344 if (data
->current_type_is_flags
)
345 tdesc_add_flag (t
, start
, field_name
);
348 if (data
->current_type_size
== 0)
349 gdb_xml_error (parser
,
350 _("Implicitly sized type can "
351 "not contain bitfield \"%s\""),
355 gdb_xml_error (parser
,
356 _("Bitfield \"%s\" goes past "
357 "64 bits (unsupported)"),
360 /* Assume that the bit numbering in XML is "lsb-zero". Most
361 architectures other than PowerPC use this ordering. In
362 the future, we can add an XML tag to indicate "msb-zero"
365 gdb_xml_error (parser
, _("Bitfield \"%s\" has start after end"),
368 if (end
>= data
->current_type_size
* TARGET_CHAR_BIT
)
369 gdb_xml_error (parser
,
370 _("Bitfield \"%s\" does not fit in struct"));
372 tdesc_add_bitfield (t
, field_name
, start
, end
);
376 gdb_xml_error (parser
, _("Field \"%s\" has neither type nor bit position"),
380 /* Handle the start of a <vector> element. Initialize the type and
381 record it with the current feature. */
384 tdesc_start_vector (struct gdb_xml_parser
*parser
,
385 const struct gdb_xml_element
*element
,
386 void *user_data
, VEC(gdb_xml_value_s
) *attributes
)
388 struct tdesc_parsing_data
*data
= user_data
;
389 struct gdb_xml_value
*attrs
= VEC_address (gdb_xml_value_s
, attributes
);
390 struct tdesc_type
*field_type
;
391 char *id
, *field_type_id
;
395 field_type_id
= attrs
[1].value
;
396 count
= * (ULONGEST
*) attrs
[2].value
;
398 field_type
= tdesc_named_type (data
->current_feature
, field_type_id
);
399 if (field_type
== NULL
)
400 gdb_xml_error (parser
, _("Vector \"%s\" references undefined type \"%s\""),
403 tdesc_create_vector (data
->current_feature
, id
, field_type
, count
);
406 /* The elements and attributes of an XML target description. */
408 static const struct gdb_xml_attribute field_attributes
[] = {
409 { "name", GDB_XML_AF_NONE
, NULL
, NULL
},
410 { "type", GDB_XML_AF_OPTIONAL
, NULL
, NULL
},
411 { "start", GDB_XML_AF_OPTIONAL
, gdb_xml_parse_attr_ulongest
, NULL
},
412 { "end", GDB_XML_AF_OPTIONAL
, gdb_xml_parse_attr_ulongest
, NULL
},
413 { NULL
, GDB_XML_AF_NONE
, NULL
, NULL
}
416 static const struct gdb_xml_element struct_union_children
[] = {
417 { "field", field_attributes
, NULL
, GDB_XML_EF_REPEATABLE
,
418 tdesc_start_field
, NULL
},
419 { NULL
, NULL
, NULL
, GDB_XML_EF_NONE
, NULL
, NULL
}
422 static const struct gdb_xml_attribute reg_attributes
[] = {
423 { "name", GDB_XML_AF_NONE
, NULL
, NULL
},
424 { "bitsize", GDB_XML_AF_NONE
, gdb_xml_parse_attr_ulongest
, NULL
},
425 { "regnum", GDB_XML_AF_OPTIONAL
, gdb_xml_parse_attr_ulongest
, NULL
},
426 { "type", GDB_XML_AF_OPTIONAL
, NULL
, NULL
},
427 { "group", GDB_XML_AF_OPTIONAL
, NULL
, NULL
},
428 { "save-restore", GDB_XML_AF_OPTIONAL
,
429 gdb_xml_parse_attr_enum
, gdb_xml_enums_boolean
},
430 { NULL
, GDB_XML_AF_NONE
, NULL
, NULL
}
433 static const struct gdb_xml_attribute struct_union_attributes
[] = {
434 { "id", GDB_XML_AF_NONE
, NULL
, NULL
},
435 { "size", GDB_XML_AF_OPTIONAL
, gdb_xml_parse_attr_ulongest
, NULL
},
436 { NULL
, GDB_XML_AF_NONE
, NULL
, NULL
}
439 static const struct gdb_xml_attribute flags_attributes
[] = {
440 { "id", GDB_XML_AF_NONE
, NULL
, NULL
},
441 { "size", GDB_XML_AF_NONE
, gdb_xml_parse_attr_ulongest
, NULL
},
442 { NULL
, GDB_XML_AF_NONE
, NULL
, NULL
}
445 static const struct gdb_xml_attribute vector_attributes
[] = {
446 { "id", GDB_XML_AF_NONE
, NULL
, NULL
},
447 { "type", GDB_XML_AF_NONE
, NULL
, NULL
},
448 { "count", GDB_XML_AF_NONE
, gdb_xml_parse_attr_ulongest
, NULL
},
449 { NULL
, GDB_XML_AF_NONE
, NULL
, NULL
}
452 static const struct gdb_xml_attribute feature_attributes
[] = {
453 { "name", GDB_XML_AF_NONE
, NULL
, NULL
},
454 { NULL
, GDB_XML_AF_NONE
, NULL
, NULL
}
457 static const struct gdb_xml_element feature_children
[] = {
458 { "reg", reg_attributes
, NULL
,
459 GDB_XML_EF_OPTIONAL
| GDB_XML_EF_REPEATABLE
,
460 tdesc_start_reg
, NULL
},
461 { "struct", struct_union_attributes
, struct_union_children
,
462 GDB_XML_EF_OPTIONAL
| GDB_XML_EF_REPEATABLE
,
463 tdesc_start_struct
, NULL
},
464 { "union", struct_union_attributes
, struct_union_children
,
465 GDB_XML_EF_OPTIONAL
| GDB_XML_EF_REPEATABLE
,
466 tdesc_start_union
, NULL
},
467 { "flags", flags_attributes
, struct_union_children
,
468 GDB_XML_EF_OPTIONAL
| GDB_XML_EF_REPEATABLE
,
469 tdesc_start_flags
, NULL
},
470 { "vector", vector_attributes
, NULL
,
471 GDB_XML_EF_OPTIONAL
| GDB_XML_EF_REPEATABLE
,
472 tdesc_start_vector
, NULL
},
473 { NULL
, NULL
, NULL
, GDB_XML_EF_NONE
, NULL
, NULL
}
476 static const struct gdb_xml_attribute target_attributes
[] = {
477 { "version", GDB_XML_AF_NONE
, NULL
, NULL
},
478 { NULL
, GDB_XML_AF_NONE
, NULL
, NULL
}
481 static const struct gdb_xml_element target_children
[] = {
482 { "architecture", NULL
, NULL
, GDB_XML_EF_OPTIONAL
,
483 NULL
, tdesc_end_arch
},
484 { "osabi", NULL
, NULL
, GDB_XML_EF_OPTIONAL
,
485 NULL
, tdesc_end_osabi
},
486 { "compatible", NULL
, NULL
, GDB_XML_EF_OPTIONAL
| GDB_XML_EF_REPEATABLE
,
487 NULL
, tdesc_end_compatible
},
488 { "feature", feature_attributes
, feature_children
,
489 GDB_XML_EF_OPTIONAL
| GDB_XML_EF_REPEATABLE
,
490 tdesc_start_feature
, NULL
},
491 { NULL
, NULL
, NULL
, GDB_XML_EF_NONE
, NULL
, NULL
}
494 static const struct gdb_xml_element tdesc_elements
[] = {
495 { "target", target_attributes
, target_children
, GDB_XML_EF_NONE
,
496 tdesc_start_target
, NULL
},
497 { NULL
, NULL
, NULL
, GDB_XML_EF_NONE
, NULL
, NULL
}
500 /* Parse DOCUMENT into a target description and return it. */
502 static struct target_desc
*
503 tdesc_parse_xml (const char *document
, xml_fetch_another fetcher
,
506 struct cleanup
*back_to
, *result_cleanup
;
507 struct tdesc_parsing_data data
;
508 struct tdesc_xml_cache
*cache
;
512 /* Expand all XInclude directives. */
513 expanded_text
= xml_process_xincludes (_("target description"),
514 document
, fetcher
, fetcher_baton
, 0);
515 if (expanded_text
== NULL
)
517 warning (_("Could not load XML target description; ignoring"));
521 /* Check for an exact match in the list of descriptions we have
522 previously parsed. strcmp is a slightly inefficient way to
523 do this; an SHA-1 checksum would work as well. */
524 for (ix
= 0; VEC_iterate (tdesc_xml_cache_s
, xml_cache
, ix
, cache
); ix
++)
525 if (strcmp (cache
->xml_document
, expanded_text
) == 0)
527 xfree (expanded_text
);
531 back_to
= make_cleanup (null_cleanup
, NULL
);
533 memset (&data
, 0, sizeof (struct tdesc_parsing_data
));
534 data
.tdesc
= allocate_target_description ();
535 result_cleanup
= make_cleanup_free_target_description (data
.tdesc
);
536 make_cleanup (xfree
, expanded_text
);
538 if (gdb_xml_parse_quick (_("target description"), "gdb-target.dtd",
539 tdesc_elements
, expanded_text
, &data
) == 0)
541 /* Parsed successfully. */
542 struct tdesc_xml_cache new_cache
;
544 new_cache
.xml_document
= expanded_text
;
545 new_cache
.tdesc
= data
.tdesc
;
546 VEC_safe_push (tdesc_xml_cache_s
, xml_cache
, &new_cache
);
547 discard_cleanups (result_cleanup
);
548 do_cleanups (back_to
);
553 warning (_("Could not load XML target description; ignoring"));
554 do_cleanups (back_to
);
558 #endif /* HAVE_LIBEXPAT */
561 /* Read an XML target description from FILENAME. Parse it, and return
562 the parsed description. */
564 const struct target_desc
*
565 file_read_description_xml (const char *filename
)
567 struct target_desc
*tdesc
;
569 struct cleanup
*back_to
;
572 tdesc_str
= xml_fetch_content_from_file (filename
, NULL
);
573 if (tdesc_str
== NULL
)
575 warning (_("Could not open \"%s\""), filename
);
579 back_to
= make_cleanup (xfree
, tdesc_str
);
581 dirname
= ldirname (filename
);
583 make_cleanup (xfree
, dirname
);
585 tdesc
= tdesc_parse_xml (tdesc_str
, xml_fetch_content_from_file
, dirname
);
586 do_cleanups (back_to
);
591 /* Read a string representation of available features from the target,
592 using TARGET_OBJECT_AVAILABLE_FEATURES. The returned string is
593 malloc allocated and NUL-terminated. NAME should be a non-NULL
594 string identifying the XML document we want; the top level document
595 is "target.xml". Other calls may be performed for the DTD or
599 fetch_available_features_from_target (const char *name
, void *baton_
)
601 struct target_ops
*ops
= baton_
;
603 /* Read this object as a string. This ensures that a NUL
604 terminator is added. */
605 return target_read_stralloc (ops
,
606 TARGET_OBJECT_AVAILABLE_FEATURES
,
611 /* Read an XML target description using OPS. Parse it, and return the
612 parsed description. */
614 const struct target_desc
*
615 target_read_description_xml (struct target_ops
*ops
)
617 struct target_desc
*tdesc
;
619 struct cleanup
*back_to
;
621 tdesc_str
= fetch_available_features_from_target ("target.xml", ops
);
622 if (tdesc_str
== NULL
)
625 back_to
= make_cleanup (xfree
, tdesc_str
);
626 tdesc
= tdesc_parse_xml (tdesc_str
,
627 fetch_available_features_from_target
,
629 do_cleanups (back_to
);