1 /* XML target description support for GDB.
4 Free Software Foundation, Inc.
6 Contributed by CodeSourcery.
8 This file is part of GDB.
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 2 of the License, or
13 (at your option) any later version.
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 51 Franklin Street, Fifth Floor,
23 Boston, MA 02110-1301, USA. */
28 #include "target-descriptions.h"
29 #include "xml-support.h"
30 #include "xml-tdesc.h"
32 #include "filenames.h"
34 #include "gdb_assert.h"
36 #if !defined(HAVE_LIBEXPAT)
38 /* Parse DOCUMENT into a target description. Or don't, since we don't have
41 static struct target_desc
*
42 tdesc_parse_xml (const char *document
, xml_fetch_another fetcher
,
45 static int have_warned
;
50 warning (_("Can not parse XML target description; XML support was "
51 "disabled at compile time"));
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 struct tdesc_xml_cache
69 const char *xml_document
;
70 struct target_desc
*tdesc
;
72 typedef struct tdesc_xml_cache tdesc_xml_cache_s
;
73 DEF_VEC_O(tdesc_xml_cache_s
);
75 static VEC(tdesc_xml_cache_s
) *xml_cache
;
77 /* Callback data for target description parsing. */
79 struct tdesc_parsing_data
81 /* The target description we are building. */
82 struct target_desc
*tdesc
;
84 /* The target feature we are currently parsing, or last parsed. */
85 struct tdesc_feature
*current_feature
;
87 /* The register number to use for the next register we see, if
88 it does not have its own. This starts at zero. */
91 /* The union we are currently parsing, or last parsed. */
92 struct type
*current_union
;
95 /* Handle the end of an <architecture> element and its value. */
98 tdesc_end_arch (struct gdb_xml_parser
*parser
,
99 const struct gdb_xml_element
*element
,
100 void *user_data
, const char *body_text
)
102 struct tdesc_parsing_data
*data
= user_data
;
103 const struct bfd_arch_info
*arch
;
105 arch
= bfd_scan_arch (body_text
);
107 gdb_xml_error (parser
, _("Target description specified unknown "
108 "architecture \"%s\""), body_text
);
109 set_tdesc_architecture (data
->tdesc
, arch
);
112 /* Handle the start of a <target> element. */
115 tdesc_start_target (struct gdb_xml_parser
*parser
,
116 const struct gdb_xml_element
*element
,
117 void *user_data
, VEC(gdb_xml_value_s
) *attributes
)
119 struct tdesc_parsing_data
*data
= user_data
;
120 char *version
= VEC_index (gdb_xml_value_s
, attributes
, 0)->value
;
122 if (strcmp (version
, "1.0") != 0)
123 gdb_xml_error (parser
,
124 _("Target description has unsupported version \"%s\""),
128 /* Handle the start of a <feature> element. */
131 tdesc_start_feature (struct gdb_xml_parser
*parser
,
132 const struct gdb_xml_element
*element
,
133 void *user_data
, VEC(gdb_xml_value_s
) *attributes
)
135 struct tdesc_parsing_data
*data
= user_data
;
136 char *name
= VEC_index (gdb_xml_value_s
, attributes
, 0)->value
;
138 data
->current_feature
= tdesc_create_feature (data
->tdesc
, name
);
141 /* Handle the start of a <reg> element. Fill in the optional
142 attributes and attach it to the containing feature. */
145 tdesc_start_reg (struct gdb_xml_parser
*parser
,
146 const struct gdb_xml_element
*element
,
147 void *user_data
, VEC(gdb_xml_value_s
) *attributes
)
149 struct tdesc_parsing_data
*data
= user_data
;
150 struct gdb_xml_value
*attrs
= VEC_address (gdb_xml_value_s
, attributes
);
152 char *name
, *group
, *type
;
153 int bitsize
, regnum
, save_restore
;
155 length
= VEC_length (gdb_xml_value_s
, attributes
);
157 name
= attrs
[ix
++].value
;
158 bitsize
= * (ULONGEST
*) attrs
[ix
++].value
;
160 if (ix
< length
&& strcmp (attrs
[ix
].name
, "regnum") == 0)
161 regnum
= * (ULONGEST
*) attrs
[ix
++].value
;
163 regnum
= data
->next_regnum
;
165 if (ix
< length
&& strcmp (attrs
[ix
].name
, "type") == 0)
166 type
= attrs
[ix
++].value
;
170 if (ix
< length
&& strcmp (attrs
[ix
].name
, "group") == 0)
171 group
= attrs
[ix
++].value
;
175 if (ix
< length
&& strcmp (attrs
[ix
].name
, "save-restore") == 0)
176 save_restore
= * (ULONGEST
*) attrs
[ix
++].value
;
180 if (strcmp (type
, "int") != 0
181 && strcmp (type
, "float") != 0
182 && strcmp (type
, "code_ptr") != 0
183 && strcmp (type
, "data_ptr") != 0
184 && tdesc_named_type (data
->current_feature
, type
) == NULL
)
185 gdb_xml_error (parser
, _("Register \"%s\" has unknown type \"%s\""),
188 tdesc_create_reg (data
->current_feature
, name
, regnum
, save_restore
, group
,
191 data
->next_regnum
= regnum
+ 1;
194 /* Handle the start of a <union> element. Initialize the type and
195 record it with the current feature. */
198 tdesc_start_union (struct gdb_xml_parser
*parser
,
199 const struct gdb_xml_element
*element
,
200 void *user_data
, VEC(gdb_xml_value_s
) *attributes
)
202 struct tdesc_parsing_data
*data
= user_data
;
203 char *id
= VEC_index (gdb_xml_value_s
, attributes
, 0)->value
;
206 type
= init_composite_type (NULL
, TYPE_CODE_UNION
);
207 TYPE_NAME (type
) = xstrdup (id
);
208 tdesc_record_type (data
->current_feature
, type
);
209 data
->current_union
= type
;
212 /* Handle the end of a <union> element. */
215 tdesc_end_union (struct gdb_xml_parser
*parser
,
216 const struct gdb_xml_element
*element
,
217 void *user_data
, const char *body_text
)
219 struct tdesc_parsing_data
*data
= user_data
;
222 /* If any of the children of this union are vectors, flag the union
223 as a vector also. This allows e.g. a union of two vector types
224 to show up automatically in "info vector". */
225 for (i
= 0; i
< TYPE_NFIELDS (data
->current_union
); i
++)
226 if (TYPE_VECTOR (TYPE_FIELD_TYPE (data
->current_union
, i
)))
228 TYPE_FLAGS (data
->current_union
) |= TYPE_FLAG_VECTOR
;
233 /* Handle the start of a <field> element. Attach the field to the
237 tdesc_start_field (struct gdb_xml_parser
*parser
,
238 const struct gdb_xml_element
*element
,
239 void *user_data
, VEC(gdb_xml_value_s
) *attributes
)
241 struct tdesc_parsing_data
*data
= user_data
;
242 struct gdb_xml_value
*attrs
= VEC_address (gdb_xml_value_s
, attributes
);
243 struct type
*type
, *field_type
;
244 char *field_name
, *field_type_id
;
246 field_name
= attrs
[0].value
;
247 field_type_id
= attrs
[1].value
;
249 field_type
= tdesc_named_type (data
->current_feature
, field_type_id
);
250 if (field_type
== NULL
)
251 gdb_xml_error (parser
, _("Union field \"%s\" references undefined "
253 field_name
, field_type_id
);
255 append_composite_type_field (data
->current_union
, xstrdup (field_name
),
259 /* Handle the start of a <vector> element. Initialize the type and
260 record it with the current feature. */
263 tdesc_start_vector (struct gdb_xml_parser
*parser
,
264 const struct gdb_xml_element
*element
,
265 void *user_data
, VEC(gdb_xml_value_s
) *attributes
)
267 struct tdesc_parsing_data
*data
= user_data
;
268 struct gdb_xml_value
*attrs
= VEC_address (gdb_xml_value_s
, attributes
);
269 struct type
*type
, *field_type
, *range_type
;
270 char *id
, *field_type_id
;
274 field_type_id
= attrs
[1].value
;
275 count
= * (ULONGEST
*) attrs
[2].value
;
277 field_type
= tdesc_named_type (data
->current_feature
, field_type_id
);
278 if (field_type
== NULL
)
279 gdb_xml_error (parser
, _("Vector \"%s\" references undefined type \"%s\""),
282 /* A vector is just an array plus a special flag. */
283 range_type
= create_range_type (NULL
, builtin_type_int
, 0, count
- 1);
284 type
= create_array_type (NULL
, field_type
, range_type
);
285 TYPE_NAME (type
) = xstrdup (id
);
287 TYPE_FLAGS (type
) |= TYPE_FLAG_VECTOR
;
289 tdesc_record_type (data
->current_feature
, type
);
292 /* The elements and attributes of an XML target description. */
294 static const struct gdb_xml_attribute field_attributes
[] = {
295 { "name", GDB_XML_AF_NONE
, NULL
, NULL
},
296 { "type", GDB_XML_AF_NONE
, NULL
, NULL
},
297 { NULL
, GDB_XML_AF_NONE
, NULL
, NULL
}
300 static const struct gdb_xml_element union_children
[] = {
301 { "field", field_attributes
, NULL
, GDB_XML_EF_REPEATABLE
,
302 tdesc_start_field
, NULL
},
303 { NULL
, NULL
, NULL
, GDB_XML_EF_NONE
, NULL
, NULL
}
306 static const struct gdb_xml_attribute reg_attributes
[] = {
307 { "name", GDB_XML_AF_NONE
, NULL
, NULL
},
308 { "bitsize", GDB_XML_AF_NONE
, gdb_xml_parse_attr_ulongest
, NULL
},
309 { "regnum", GDB_XML_AF_OPTIONAL
, gdb_xml_parse_attr_ulongest
, NULL
},
310 { "type", GDB_XML_AF_OPTIONAL
, NULL
, NULL
},
311 { "group", GDB_XML_AF_OPTIONAL
, NULL
, NULL
},
312 { "save-restore", GDB_XML_AF_OPTIONAL
,
313 gdb_xml_parse_attr_enum
, gdb_xml_enums_boolean
},
314 { NULL
, GDB_XML_AF_NONE
, NULL
, NULL
}
317 static const struct gdb_xml_attribute union_attributes
[] = {
318 { "id", GDB_XML_AF_NONE
, NULL
, NULL
},
319 { NULL
, GDB_XML_AF_NONE
, NULL
, NULL
}
322 static const struct gdb_xml_attribute vector_attributes
[] = {
323 { "id", GDB_XML_AF_NONE
, NULL
, NULL
},
324 { "type", GDB_XML_AF_NONE
, NULL
, NULL
},
325 { "count", GDB_XML_AF_NONE
, gdb_xml_parse_attr_ulongest
, NULL
},
326 { NULL
, GDB_XML_AF_NONE
, NULL
, NULL
}
329 static const struct gdb_xml_attribute feature_attributes
[] = {
330 { "name", GDB_XML_AF_NONE
, NULL
, NULL
},
331 { NULL
, GDB_XML_AF_NONE
, NULL
, NULL
}
334 static const struct gdb_xml_element feature_children
[] = {
335 { "reg", reg_attributes
, NULL
,
336 GDB_XML_EF_OPTIONAL
| GDB_XML_EF_REPEATABLE
,
337 tdesc_start_reg
, NULL
},
338 { "union", union_attributes
, union_children
,
339 GDB_XML_EF_OPTIONAL
| GDB_XML_EF_REPEATABLE
,
340 tdesc_start_union
, tdesc_end_union
},
341 { "vector", vector_attributes
, NULL
,
342 GDB_XML_EF_OPTIONAL
| GDB_XML_EF_REPEATABLE
,
343 tdesc_start_vector
, NULL
},
344 { NULL
, NULL
, NULL
, GDB_XML_EF_NONE
, NULL
, NULL
}
347 static const struct gdb_xml_attribute target_attributes
[] = {
348 { "version", GDB_XML_AF_NONE
, NULL
, NULL
},
349 { NULL
, GDB_XML_AF_NONE
, NULL
, NULL
}
352 static const struct gdb_xml_element target_children
[] = {
353 { "architecture", NULL
, NULL
, GDB_XML_EF_OPTIONAL
,
354 NULL
, tdesc_end_arch
},
355 { "feature", feature_attributes
, feature_children
,
356 GDB_XML_EF_OPTIONAL
| GDB_XML_EF_REPEATABLE
,
357 tdesc_start_feature
, NULL
},
358 { NULL
, NULL
, NULL
, GDB_XML_EF_NONE
, NULL
, NULL
}
361 static const struct gdb_xml_element tdesc_elements
[] = {
362 { "target", target_attributes
, target_children
, GDB_XML_EF_NONE
,
363 tdesc_start_target
, NULL
},
364 { NULL
, NULL
, NULL
, GDB_XML_EF_NONE
, NULL
, NULL
}
367 /* Parse DOCUMENT into a target description and return it. */
369 static struct target_desc
*
370 tdesc_parse_xml (const char *document
, xml_fetch_another fetcher
,
373 struct cleanup
*back_to
, *result_cleanup
;
374 struct gdb_xml_parser
*parser
;
375 struct tdesc_parsing_data data
;
376 struct tdesc_xml_cache
*cache
;
380 /* Expand all XInclude directives. */
381 expanded_text
= xml_process_xincludes (_("target description"),
382 document
, fetcher
, fetcher_baton
, 0);
383 if (expanded_text
== NULL
)
385 warning (_("Could not load XML target description; ignoring"));
389 /* Check for an exact match in the list of descriptions we have
390 previously parsed. strcmp is a slightly inefficient way to
391 do this; an SHA-1 checksum would work as well. */
392 for (ix
= 0; VEC_iterate (tdesc_xml_cache_s
, xml_cache
, ix
, cache
); ix
++)
393 if (strcmp (cache
->xml_document
, expanded_text
) == 0)
395 xfree (expanded_text
);
399 back_to
= make_cleanup (null_cleanup
, NULL
);
400 parser
= gdb_xml_create_parser_and_cleanup (_("target description"),
401 tdesc_elements
, &data
);
402 gdb_xml_use_dtd (parser
, "gdb-target.dtd");
404 memset (&data
, 0, sizeof (struct tdesc_parsing_data
));
405 data
.tdesc
= allocate_target_description ();
406 result_cleanup
= make_cleanup_free_target_description (data
.tdesc
);
407 make_cleanup (xfree
, expanded_text
);
409 if (gdb_xml_parse (parser
, expanded_text
) == 0)
411 /* Parsed successfully. */
412 struct tdesc_xml_cache new_cache
;
414 new_cache
.xml_document
= expanded_text
;
415 new_cache
.tdesc
= data
.tdesc
;
416 VEC_safe_push (tdesc_xml_cache_s
, xml_cache
, &new_cache
);
417 discard_cleanups (result_cleanup
);
418 do_cleanups (back_to
);
423 warning (_("Could not load XML target description; ignoring"));
424 do_cleanups (back_to
);
428 #endif /* HAVE_LIBEXPAT */
434 do_cleanup_fclose (void *file
)
439 /* Open FILENAME, read all its text into memory, close it, and return
440 the text. If something goes wrong, return NULL and warn. */
443 fetch_xml_from_file (const char *filename
, void *baton
)
445 const char *dirname
= baton
;
447 struct cleanup
*back_to
;
451 if (dirname
&& *dirname
)
453 char *fullname
= concat (dirname
, "/", filename
, NULL
);
454 if (fullname
== NULL
)
456 file
= fopen (fullname
, FOPEN_RT
);
460 file
= fopen (filename
, FOPEN_RT
);
465 back_to
= make_cleanup (do_cleanup_fclose
, file
);
467 /* Read in the whole file, one chunk at a time. */
470 text
= xmalloc (len
);
471 make_cleanup (free_current_contents
, &text
);
476 /* Continue reading where the last read left off. Leave at least
477 one byte so that we can NUL-terminate the result. */
478 bytes_read
= fread (text
+ offset
, 1, len
- offset
- 1, file
);
481 warning (_("Read error from \"%s\""), filename
);
482 do_cleanups (back_to
);
486 offset
+= bytes_read
;
492 text
= xrealloc (text
, len
);
496 discard_cleanups (back_to
);
502 /* Read an XML target description from FILENAME. Parse it, and return
503 the parsed description. */
505 const struct target_desc
*
506 file_read_description_xml (const char *filename
)
508 struct target_desc
*tdesc
;
510 struct cleanup
*back_to
;
513 tdesc_str
= fetch_xml_from_file (filename
, NULL
);
514 if (tdesc_str
== NULL
)
516 warning (_("Could not open \"%s\""), filename
);
520 back_to
= make_cleanup (xfree
, tdesc_str
);
522 dirname
= ldirname (filename
);
524 make_cleanup (xfree
, dirname
);
526 tdesc
= tdesc_parse_xml (tdesc_str
, fetch_xml_from_file
, dirname
);
527 do_cleanups (back_to
);
532 /* Read a string representation of available features from the target,
533 using TARGET_OBJECT_AVAILABLE_FEATURES. The returned string is
534 malloc allocated and NUL-terminated. NAME should be a non-NULL
535 string identifying the XML document we want; the top level document
536 is "target.xml". Other calls may be performed for the DTD or
540 fetch_available_features_from_target (const char *name
, void *baton_
)
542 struct target_ops
*ops
= baton_
;
544 /* Read this object as a string. This ensures that a NUL
545 terminator is added. */
546 return target_read_stralloc (ops
,
547 TARGET_OBJECT_AVAILABLE_FEATURES
,
552 /* Read an XML target description using OPS. Parse it, and return the
553 parsed description. */
555 const struct target_desc
*
556 target_read_description_xml (struct target_ops
*ops
)
558 struct target_desc
*tdesc
;
560 struct cleanup
*back_to
;
562 tdesc_str
= fetch_available_features_from_target ("target.xml", ops
);
563 if (tdesc_str
== NULL
)
566 back_to
= make_cleanup (xfree
, tdesc_str
);
567 tdesc
= tdesc_parse_xml (tdesc_str
,
568 fetch_available_features_from_target
,
570 do_cleanups (back_to
);