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 <feature> element. */
115 tdesc_start_feature (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 *name
= VEC_index (gdb_xml_value_s
, attributes
, 0)->value
;
122 data
->current_feature
= tdesc_create_feature (data
->tdesc
, name
);
125 /* Handle the start of a <reg> element. Fill in the optional
126 attributes and attach it to the containing feature. */
129 tdesc_start_reg (struct gdb_xml_parser
*parser
,
130 const struct gdb_xml_element
*element
,
131 void *user_data
, VEC(gdb_xml_value_s
) *attributes
)
133 struct tdesc_parsing_data
*data
= user_data
;
134 struct gdb_xml_value
*attrs
= VEC_address (gdb_xml_value_s
, attributes
);
136 char *name
, *group
, *type
;
137 int bitsize
, regnum
, save_restore
;
139 length
= VEC_length (gdb_xml_value_s
, attributes
);
141 name
= attrs
[ix
++].value
;
142 bitsize
= * (ULONGEST
*) attrs
[ix
++].value
;
144 if (ix
< length
&& strcmp (attrs
[ix
].name
, "regnum") == 0)
145 regnum
= * (ULONGEST
*) attrs
[ix
++].value
;
147 regnum
= data
->next_regnum
;
149 if (ix
< length
&& strcmp (attrs
[ix
].name
, "type") == 0)
150 type
= attrs
[ix
++].value
;
154 if (ix
< length
&& strcmp (attrs
[ix
].name
, "group") == 0)
155 group
= attrs
[ix
++].value
;
159 if (ix
< length
&& strcmp (attrs
[ix
].name
, "save-restore") == 0)
160 save_restore
= * (ULONGEST
*) attrs
[ix
++].value
;
164 if (strcmp (type
, "int") != 0
165 && strcmp (type
, "float") != 0
166 && strcmp (type
, "code_ptr") != 0
167 && strcmp (type
, "data_ptr") != 0
168 && tdesc_named_type (data
->current_feature
, type
) == NULL
)
169 gdb_xml_error (parser
, _("Register \"%s\" has unknown type \"%s\""),
172 tdesc_create_reg (data
->current_feature
, name
, regnum
, save_restore
, group
,
175 data
->next_regnum
= regnum
+ 1;
178 /* Handle the start of a <union> element. Initialize the type and
179 record it with the current feature. */
182 tdesc_start_union (struct gdb_xml_parser
*parser
,
183 const struct gdb_xml_element
*element
,
184 void *user_data
, VEC(gdb_xml_value_s
) *attributes
)
186 struct tdesc_parsing_data
*data
= user_data
;
187 char *id
= VEC_index (gdb_xml_value_s
, attributes
, 0)->value
;
190 type
= init_composite_type (NULL
, TYPE_CODE_UNION
);
191 TYPE_NAME (type
) = xstrdup (id
);
192 tdesc_record_type (data
->current_feature
, type
);
193 data
->current_union
= type
;
196 /* Handle the end of a <union> element. */
199 tdesc_end_union (struct gdb_xml_parser
*parser
,
200 const struct gdb_xml_element
*element
,
201 void *user_data
, const char *body_text
)
203 struct tdesc_parsing_data
*data
= user_data
;
206 /* If any of the children of this union are vectors, flag the union
207 as a vector also. This allows e.g. a union of two vector types
208 to show up automatically in "info vector". */
209 for (i
= 0; i
< TYPE_NFIELDS (data
->current_union
); i
++)
210 if (TYPE_VECTOR (TYPE_FIELD_TYPE (data
->current_union
, i
)))
212 TYPE_FLAGS (data
->current_union
) |= TYPE_FLAG_VECTOR
;
217 /* Handle the start of a <field> element. Attach the field to the
221 tdesc_start_field (struct gdb_xml_parser
*parser
,
222 const struct gdb_xml_element
*element
,
223 void *user_data
, VEC(gdb_xml_value_s
) *attributes
)
225 struct tdesc_parsing_data
*data
= user_data
;
226 struct gdb_xml_value
*attrs
= VEC_address (gdb_xml_value_s
, attributes
);
227 struct type
*type
, *field_type
;
228 char *field_name
, *field_type_id
;
230 field_name
= attrs
[0].value
;
231 field_type_id
= attrs
[1].value
;
233 field_type
= tdesc_named_type (data
->current_feature
, field_type_id
);
234 if (field_type
== NULL
)
235 gdb_xml_error (parser
, _("Union field \"%s\" references undefined "
237 field_name
, field_type_id
);
239 append_composite_type_field (data
->current_union
, xstrdup (field_name
),
243 /* Handle the start of a <vector> element. Initialize the type and
244 record it with the current feature. */
247 tdesc_start_vector (struct gdb_xml_parser
*parser
,
248 const struct gdb_xml_element
*element
,
249 void *user_data
, VEC(gdb_xml_value_s
) *attributes
)
251 struct tdesc_parsing_data
*data
= user_data
;
252 struct gdb_xml_value
*attrs
= VEC_address (gdb_xml_value_s
, attributes
);
253 struct type
*type
, *field_type
, *range_type
;
254 char *id
, *field_type_id
;
258 field_type_id
= attrs
[1].value
;
259 count
= * (ULONGEST
*) attrs
[2].value
;
261 field_type
= tdesc_named_type (data
->current_feature
, field_type_id
);
262 if (field_type
== NULL
)
263 gdb_xml_error (parser
, _("Vector \"%s\" references undefined type \"%s\""),
266 /* A vector is just an array plus a special flag. */
267 range_type
= create_range_type (NULL
, builtin_type_int
, 0, count
- 1);
268 type
= create_array_type (NULL
, field_type
, range_type
);
269 TYPE_NAME (type
) = xstrdup (id
);
271 TYPE_FLAGS (type
) |= TYPE_FLAG_VECTOR
;
273 tdesc_record_type (data
->current_feature
, type
);
276 /* The elements and attributes of an XML target description. */
278 static const struct gdb_xml_attribute field_attributes
[] = {
279 { "name", GDB_XML_AF_NONE
, NULL
, NULL
},
280 { "type", GDB_XML_AF_NONE
, NULL
, NULL
},
281 { NULL
, GDB_XML_AF_NONE
, NULL
, NULL
}
284 static const struct gdb_xml_element union_children
[] = {
285 { "field", field_attributes
, NULL
, GDB_XML_EF_REPEATABLE
,
286 tdesc_start_field
, NULL
},
287 { NULL
, NULL
, NULL
, GDB_XML_EF_NONE
, NULL
, NULL
}
290 static const struct gdb_xml_attribute reg_attributes
[] = {
291 { "name", GDB_XML_AF_NONE
, NULL
, NULL
},
292 { "bitsize", GDB_XML_AF_NONE
, gdb_xml_parse_attr_ulongest
, NULL
},
293 { "regnum", GDB_XML_AF_OPTIONAL
, gdb_xml_parse_attr_ulongest
, NULL
},
294 { "type", GDB_XML_AF_OPTIONAL
, NULL
, NULL
},
295 { "group", GDB_XML_AF_OPTIONAL
, NULL
, NULL
},
296 { "save-restore", GDB_XML_AF_OPTIONAL
,
297 gdb_xml_parse_attr_enum
, gdb_xml_enums_boolean
},
298 { NULL
, GDB_XML_AF_NONE
, NULL
, NULL
}
301 static const struct gdb_xml_attribute union_attributes
[] = {
302 { "id", GDB_XML_AF_NONE
, NULL
, NULL
},
303 { NULL
, GDB_XML_AF_NONE
, NULL
, NULL
}
306 static const struct gdb_xml_attribute vector_attributes
[] = {
307 { "id", GDB_XML_AF_NONE
, NULL
, NULL
},
308 { "type", GDB_XML_AF_NONE
, NULL
, NULL
},
309 { "count", GDB_XML_AF_NONE
, gdb_xml_parse_attr_ulongest
, NULL
},
310 { NULL
, GDB_XML_AF_NONE
, NULL
, NULL
}
313 static const struct gdb_xml_attribute feature_attributes
[] = {
314 { "name", GDB_XML_AF_NONE
, NULL
, NULL
},
315 { NULL
, GDB_XML_AF_NONE
, NULL
, NULL
}
318 static const struct gdb_xml_element feature_children
[] = {
319 { "reg", reg_attributes
, NULL
,
320 GDB_XML_EF_OPTIONAL
| GDB_XML_EF_REPEATABLE
,
321 tdesc_start_reg
, NULL
},
322 { "union", union_attributes
, union_children
,
323 GDB_XML_EF_OPTIONAL
| GDB_XML_EF_REPEATABLE
,
324 tdesc_start_union
, tdesc_end_union
},
325 { "vector", vector_attributes
, NULL
,
326 GDB_XML_EF_OPTIONAL
| GDB_XML_EF_REPEATABLE
,
327 tdesc_start_vector
, NULL
},
328 { NULL
, NULL
, NULL
, GDB_XML_EF_NONE
, NULL
, NULL
}
331 static const struct gdb_xml_element target_children
[] = {
332 { "architecture", NULL
, NULL
, GDB_XML_EF_OPTIONAL
,
333 NULL
, tdesc_end_arch
},
334 { "feature", feature_attributes
, feature_children
,
335 GDB_XML_EF_OPTIONAL
| GDB_XML_EF_REPEATABLE
,
336 tdesc_start_feature
, NULL
},
337 { NULL
, NULL
, NULL
, GDB_XML_EF_NONE
, NULL
, NULL
}
340 static const struct gdb_xml_element tdesc_elements
[] = {
341 { "target", NULL
, target_children
, GDB_XML_EF_NONE
,
343 { NULL
, NULL
, NULL
, GDB_XML_EF_NONE
, NULL
, NULL
}
346 /* Parse DOCUMENT into a target description and return it. */
348 static struct target_desc
*
349 tdesc_parse_xml (const char *document
, xml_fetch_another fetcher
,
352 struct cleanup
*back_to
, *result_cleanup
;
353 struct gdb_xml_parser
*parser
;
354 struct tdesc_parsing_data data
;
355 struct tdesc_xml_cache
*cache
;
359 /* Expand all XInclude directives. */
360 expanded_text
= xml_process_xincludes (_("target description"),
361 document
, fetcher
, fetcher_baton
, 0);
362 if (expanded_text
== NULL
)
364 warning (_("Could not load XML target description; ignoring"));
368 /* Check for an exact match in the list of descriptions we have
369 previously parsed. strcmp is a slightly inefficient way to
370 do this; an SHA-1 checksum would work as well. */
371 for (ix
= 0; VEC_iterate (tdesc_xml_cache_s
, xml_cache
, ix
, cache
); ix
++)
372 if (strcmp (cache
->xml_document
, expanded_text
) == 0)
374 xfree (expanded_text
);
378 back_to
= make_cleanup (null_cleanup
, NULL
);
379 parser
= gdb_xml_create_parser_and_cleanup (_("target description"),
380 tdesc_elements
, &data
);
381 gdb_xml_use_dtd (parser
, "gdb-target.dtd");
383 memset (&data
, 0, sizeof (struct tdesc_parsing_data
));
384 data
.tdesc
= allocate_target_description ();
385 result_cleanup
= make_cleanup_free_target_description (data
.tdesc
);
386 make_cleanup (xfree
, expanded_text
);
388 if (gdb_xml_parse (parser
, expanded_text
) == 0)
390 /* Parsed successfully. */
391 struct tdesc_xml_cache new_cache
;
393 new_cache
.xml_document
= expanded_text
;
394 new_cache
.tdesc
= data
.tdesc
;
395 VEC_safe_push (tdesc_xml_cache_s
, xml_cache
, &new_cache
);
396 discard_cleanups (result_cleanup
);
397 do_cleanups (back_to
);
402 warning (_("Could not load XML target description; ignoring"));
403 do_cleanups (back_to
);
407 #endif /* HAVE_LIBEXPAT */
413 do_cleanup_fclose (void *file
)
418 /* Open FILENAME, read all its text into memory, close it, and return
419 the text. If something goes wrong, return NULL and warn. */
422 fetch_xml_from_file (const char *filename
, void *baton
)
424 const char *dirname
= baton
;
426 struct cleanup
*back_to
;
430 if (dirname
&& *dirname
)
432 char *fullname
= concat (dirname
, "/", filename
, NULL
);
433 if (fullname
== NULL
)
435 file
= fopen (fullname
, FOPEN_RT
);
439 file
= fopen (filename
, FOPEN_RT
);
444 back_to
= make_cleanup (do_cleanup_fclose
, file
);
446 /* Read in the whole file, one chunk at a time. */
449 text
= xmalloc (len
);
450 make_cleanup (free_current_contents
, &text
);
455 /* Continue reading where the last read left off. Leave at least
456 one byte so that we can NUL-terminate the result. */
457 bytes_read
= fread (text
+ offset
, 1, len
- offset
- 1, file
);
460 warning (_("Read error from \"%s\""), filename
);
461 do_cleanups (back_to
);
465 offset
+= bytes_read
;
471 text
= xrealloc (text
, len
);
475 discard_cleanups (back_to
);
481 /* Read an XML target description from FILENAME. Parse it, and return
482 the parsed description. */
484 const struct target_desc
*
485 file_read_description_xml (const char *filename
)
487 struct target_desc
*tdesc
;
489 struct cleanup
*back_to
;
492 tdesc_str
= fetch_xml_from_file (filename
, NULL
);
493 if (tdesc_str
== NULL
)
495 warning (_("Could not open \"%s\""), filename
);
499 back_to
= make_cleanup (xfree
, tdesc_str
);
501 dirname
= ldirname (filename
);
503 make_cleanup (xfree
, dirname
);
505 tdesc
= tdesc_parse_xml (tdesc_str
, fetch_xml_from_file
, dirname
);
506 do_cleanups (back_to
);
511 /* Read a string representation of available features from the target,
512 using TARGET_OBJECT_AVAILABLE_FEATURES. The returned string is
513 malloc allocated and NUL-terminated. NAME should be a non-NULL
514 string identifying the XML document we want; the top level document
515 is "target.xml". Other calls may be performed for the DTD or
519 fetch_available_features_from_target (const char *name
, void *baton_
)
521 struct target_ops
*ops
= baton_
;
523 /* Read this object as a string. This ensures that a NUL
524 terminator is added. */
525 return target_read_stralloc (ops
,
526 TARGET_OBJECT_AVAILABLE_FEATURES
,
531 /* Read an XML target description using OPS. Parse it, and return the
532 parsed description. */
534 const struct target_desc
*
535 target_read_description_xml (struct target_ops
*ops
)
537 struct target_desc
*tdesc
;
539 struct cleanup
*back_to
;
541 tdesc_str
= fetch_available_features_from_target ("target.xml", ops
);
542 if (tdesc_str
== NULL
)
545 back_to
= make_cleanup (xfree
, tdesc_str
);
546 tdesc
= tdesc_parse_xml (tdesc_str
,
547 fetch_available_features_from_target
,
549 do_cleanups (back_to
);