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 && tdesc_named_type (data
->current_feature
, type
) == NULL
)
167 gdb_xml_error (parser
, _("Register \"%s\" has unknown type \"%s\""),
170 tdesc_create_reg (data
->current_feature
, name
, regnum
, save_restore
, group
,
173 data
->next_regnum
= regnum
+ 1;
176 /* Handle the start of a <union> element. Initialize the type and
177 record it with the current feature. */
180 tdesc_start_union (struct gdb_xml_parser
*parser
,
181 const struct gdb_xml_element
*element
,
182 void *user_data
, VEC(gdb_xml_value_s
) *attributes
)
184 struct tdesc_parsing_data
*data
= user_data
;
185 char *id
= VEC_index (gdb_xml_value_s
, attributes
, 0)->value
;
188 type
= init_composite_type (NULL
, TYPE_CODE_UNION
);
189 TYPE_NAME (type
) = xstrdup (id
);
190 tdesc_record_type (data
->current_feature
, type
);
191 data
->current_union
= type
;
194 /* Handle the end of a <union> element. */
197 tdesc_end_union (struct gdb_xml_parser
*parser
,
198 const struct gdb_xml_element
*element
,
199 void *user_data
, const char *body_text
)
201 struct tdesc_parsing_data
*data
= user_data
;
204 /* If any of the children of this union are vectors, flag the union
205 as a vector also. This allows e.g. a union of two vector types
206 to show up automatically in "info vector". */
207 for (i
= 0; i
< TYPE_NFIELDS (data
->current_union
); i
++)
208 if (TYPE_VECTOR (TYPE_FIELD_TYPE (data
->current_union
, i
)))
210 TYPE_FLAGS (data
->current_union
) |= TYPE_FLAG_VECTOR
;
215 /* Handle the start of a <field> element. Attach the field to the
219 tdesc_start_field (struct gdb_xml_parser
*parser
,
220 const struct gdb_xml_element
*element
,
221 void *user_data
, VEC(gdb_xml_value_s
) *attributes
)
223 struct tdesc_parsing_data
*data
= user_data
;
224 struct gdb_xml_value
*attrs
= VEC_address (gdb_xml_value_s
, attributes
);
225 struct type
*type
, *field_type
;
226 char *field_name
, *field_type_id
;
228 field_name
= attrs
[0].value
;
229 field_type_id
= attrs
[1].value
;
231 field_type
= tdesc_named_type (data
->current_feature
, field_type_id
);
232 if (field_type
== NULL
)
233 gdb_xml_error (parser
, _("Union field \"%s\" references undefined "
235 field_name
, field_type_id
);
237 append_composite_type_field (data
->current_union
, xstrdup (field_name
),
241 /* Handle the start of a <vector> element. Initialize the type and
242 record it with the current feature. */
245 tdesc_start_vector (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 struct gdb_xml_value
*attrs
= VEC_address (gdb_xml_value_s
, attributes
);
251 struct type
*type
, *field_type
, *range_type
;
252 char *id
, *field_type_id
;
256 field_type_id
= attrs
[1].value
;
257 count
= * (ULONGEST
*) attrs
[2].value
;
259 field_type
= tdesc_named_type (data
->current_feature
, field_type_id
);
260 if (field_type
== NULL
)
261 gdb_xml_error (parser
, _("Vector \"%s\" references undefined type \"%s\""),
264 /* A vector is just an array plus a special flag. */
265 range_type
= create_range_type (NULL
, builtin_type_int
, 0, count
- 1);
266 type
= create_array_type (NULL
, field_type
, range_type
);
267 TYPE_NAME (type
) = xstrdup (id
);
269 TYPE_FLAGS (type
) |= TYPE_FLAG_VECTOR
;
271 tdesc_record_type (data
->current_feature
, type
);
274 /* The elements and attributes of an XML target description. */
276 static const struct gdb_xml_attribute field_attributes
[] = {
277 { "name", GDB_XML_AF_NONE
, NULL
, NULL
},
278 { "type", GDB_XML_AF_NONE
, NULL
, NULL
},
279 { NULL
, GDB_XML_AF_NONE
, NULL
, NULL
}
282 static const struct gdb_xml_element union_children
[] = {
283 { "field", field_attributes
, NULL
, GDB_XML_EF_REPEATABLE
,
284 tdesc_start_field
, NULL
},
285 { NULL
, NULL
, NULL
, GDB_XML_EF_NONE
, NULL
, NULL
}
288 static const struct gdb_xml_attribute reg_attributes
[] = {
289 { "name", GDB_XML_AF_NONE
, NULL
, NULL
},
290 { "bitsize", GDB_XML_AF_NONE
, gdb_xml_parse_attr_ulongest
, NULL
},
291 { "regnum", GDB_XML_AF_OPTIONAL
, gdb_xml_parse_attr_ulongest
, NULL
},
292 { "type", GDB_XML_AF_OPTIONAL
, NULL
, NULL
},
293 { "group", GDB_XML_AF_OPTIONAL
, NULL
, NULL
},
294 { "save-restore", GDB_XML_AF_OPTIONAL
,
295 gdb_xml_parse_attr_enum
, gdb_xml_enums_boolean
},
296 { NULL
, GDB_XML_AF_NONE
, NULL
, NULL
}
299 static const struct gdb_xml_attribute union_attributes
[] = {
300 { "id", GDB_XML_AF_NONE
, NULL
, NULL
},
301 { NULL
, GDB_XML_AF_NONE
, NULL
, NULL
}
304 static const struct gdb_xml_attribute vector_attributes
[] = {
305 { "id", GDB_XML_AF_NONE
, NULL
, NULL
},
306 { "type", GDB_XML_AF_NONE
, NULL
, NULL
},
307 { "count", GDB_XML_AF_NONE
, gdb_xml_parse_attr_ulongest
, NULL
},
308 { NULL
, GDB_XML_AF_NONE
, NULL
, NULL
}
311 static const struct gdb_xml_attribute feature_attributes
[] = {
312 { "name", GDB_XML_AF_NONE
, NULL
, NULL
},
313 { NULL
, GDB_XML_AF_NONE
, NULL
, NULL
}
316 static const struct gdb_xml_element feature_children
[] = {
317 { "reg", reg_attributes
, NULL
,
318 GDB_XML_EF_OPTIONAL
| GDB_XML_EF_REPEATABLE
,
319 tdesc_start_reg
, NULL
},
320 { "union", union_attributes
, union_children
,
321 GDB_XML_EF_OPTIONAL
| GDB_XML_EF_REPEATABLE
,
322 tdesc_start_union
, tdesc_end_union
},
323 { "vector", vector_attributes
, NULL
,
324 GDB_XML_EF_OPTIONAL
| GDB_XML_EF_REPEATABLE
,
325 tdesc_start_vector
, NULL
},
326 { NULL
, NULL
, NULL
, GDB_XML_EF_NONE
, NULL
, NULL
}
329 static const struct gdb_xml_element target_children
[] = {
330 { "architecture", NULL
, NULL
, GDB_XML_EF_OPTIONAL
,
331 NULL
, tdesc_end_arch
},
332 { "feature", feature_attributes
, feature_children
,
333 GDB_XML_EF_OPTIONAL
| GDB_XML_EF_REPEATABLE
,
334 tdesc_start_feature
, NULL
},
335 { NULL
, NULL
, NULL
, GDB_XML_EF_NONE
, NULL
, NULL
}
338 static const struct gdb_xml_element tdesc_elements
[] = {
339 { "target", NULL
, target_children
, GDB_XML_EF_NONE
,
341 { NULL
, NULL
, NULL
, GDB_XML_EF_NONE
, NULL
, NULL
}
344 /* Parse DOCUMENT into a target description and return it. */
346 static struct target_desc
*
347 tdesc_parse_xml (const char *document
, xml_fetch_another fetcher
,
350 struct cleanup
*back_to
, *result_cleanup
;
351 struct gdb_xml_parser
*parser
;
352 struct tdesc_parsing_data data
;
353 struct tdesc_xml_cache
*cache
;
357 /* Expand all XInclude directives. */
358 expanded_text
= xml_process_xincludes (_("target description"),
359 document
, fetcher
, fetcher_baton
, 0);
360 if (expanded_text
== NULL
)
362 warning (_("Could not load XML target description; ignoring"));
366 /* Check for an exact match in the list of descriptions we have
367 previously parsed. strcmp is a slightly inefficient way to
368 do this; an SHA-1 checksum would work as well. */
369 for (ix
= 0; VEC_iterate (tdesc_xml_cache_s
, xml_cache
, ix
, cache
); ix
++)
370 if (strcmp (cache
->xml_document
, expanded_text
) == 0)
372 xfree (expanded_text
);
376 back_to
= make_cleanup (null_cleanup
, NULL
);
377 parser
= gdb_xml_create_parser_and_cleanup (_("target description"),
378 tdesc_elements
, &data
);
379 gdb_xml_use_dtd (parser
, "gdb-target.dtd");
381 memset (&data
, 0, sizeof (struct tdesc_parsing_data
));
382 data
.tdesc
= allocate_target_description ();
383 result_cleanup
= make_cleanup_free_target_description (data
.tdesc
);
384 make_cleanup (xfree
, expanded_text
);
386 if (gdb_xml_parse (parser
, expanded_text
) == 0)
388 /* Parsed successfully. */
389 struct tdesc_xml_cache new_cache
;
391 new_cache
.xml_document
= expanded_text
;
392 new_cache
.tdesc
= data
.tdesc
;
393 VEC_safe_push (tdesc_xml_cache_s
, xml_cache
, &new_cache
);
394 discard_cleanups (result_cleanup
);
395 do_cleanups (back_to
);
400 warning (_("Could not load XML target description; ignoring"));
401 do_cleanups (back_to
);
405 #endif /* HAVE_LIBEXPAT */
411 do_cleanup_fclose (void *file
)
416 /* Open FILENAME, read all its text into memory, close it, and return
417 the text. If something goes wrong, return NULL and warn. */
420 fetch_xml_from_file (const char *filename
, void *baton
)
422 const char *dirname
= baton
;
424 struct cleanup
*back_to
;
428 if (dirname
&& *dirname
)
430 char *fullname
= concat (dirname
, "/", filename
, NULL
);
431 if (fullname
== NULL
)
433 file
= fopen (fullname
, FOPEN_RT
);
437 file
= fopen (filename
, FOPEN_RT
);
442 back_to
= make_cleanup (do_cleanup_fclose
, file
);
444 /* Read in the whole file, one chunk at a time. */
447 text
= xmalloc (len
);
448 make_cleanup (free_current_contents
, &text
);
453 /* Continue reading where the last read left off. Leave at least
454 one byte so that we can NUL-terminate the result. */
455 bytes_read
= fread (text
+ offset
, 1, len
- offset
- 1, file
);
458 warning (_("Read error from \"%s\""), filename
);
459 do_cleanups (back_to
);
463 offset
+= bytes_read
;
469 text
= xrealloc (text
, len
);
473 discard_cleanups (back_to
);
479 /* Read an XML target description from FILENAME. Parse it, and return
480 the parsed description. */
482 const struct target_desc
*
483 file_read_description_xml (const char *filename
)
485 struct target_desc
*tdesc
;
487 struct cleanup
*back_to
;
491 tdesc_str
= fetch_xml_from_file (filename
, NULL
);
492 if (tdesc_str
== NULL
)
494 warning (_("Could not open \"%s\""), filename
);
498 back_to
= make_cleanup (xfree
, tdesc_str
);
500 /* Simple, portable version of dirname that does not modify its
502 base
= lbasename (filename
);
503 while (base
> filename
&& IS_DIR_SEPARATOR (base
[-1]))
507 dirname
= xmalloc (base
- filename
+ 2);
508 memcpy (dirname
, filename
, base
- filename
);
510 /* On DOS based file systems, convert "d:foo" to "d:.", so that
511 we create "d:./bar" later instead of the (different)
513 if (base
- filename
== 2 && IS_ABSOLUTE_PATH (base
)
514 && !IS_DIR_SEPARATOR (filename
[0]))
515 dirname
[base
++ - filename
] = '.';
517 dirname
[base
- filename
] = '\0';
518 make_cleanup (xfree
, dirname
);
523 tdesc
= tdesc_parse_xml (tdesc_str
, fetch_xml_from_file
, dirname
);
524 do_cleanups (back_to
);
529 /* Read a string representation of available features from the target,
530 using TARGET_OBJECT_AVAILABLE_FEATURES. The returned string is
531 malloc allocated and NUL-terminated. NAME should be a non-NULL
532 string identifying the XML document we want; the top level document
533 is "target.xml". Other calls may be performed for the DTD or
537 fetch_available_features_from_target (const char *name
, void *baton_
)
539 struct target_ops
*ops
= baton_
;
541 /* Read this object as a string. This ensures that a NUL
542 terminator is added. */
543 return target_read_stralloc (ops
,
544 TARGET_OBJECT_AVAILABLE_FEATURES
,
549 /* Read an XML target description using OPS. Parse it, and return the
550 parsed description. */
552 const struct target_desc
*
553 target_read_description_xml (struct target_ops
*ops
)
555 struct target_desc
*tdesc
;
557 struct cleanup
*back_to
;
559 tdesc_str
= fetch_available_features_from_target ("target.xml", ops
);
560 if (tdesc_str
== NULL
)
563 back_to
= make_cleanup (xfree
, tdesc_str
);
564 tdesc
= tdesc_parse_xml (tdesc_str
,
565 fetch_available_features_from_target
,
567 do_cleanups (back_to
);