]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gdb/xml-tdesc.c
* configure.ac: Switch license to GPLv3.
[thirdparty/binutils-gdb.git] / gdb / xml-tdesc.c
CommitLineData
23181151
DJ
1/* XML target description support for GDB.
2
3 Copyright (C) 2006
4 Free Software Foundation, Inc.
5
6 Contributed by CodeSourcery.
7
8 This file is part of GDB.
9
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.
14
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.
19
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. */
24
25#include "defs.h"
123dc839 26#include "gdbtypes.h"
23181151
DJ
27#include "target.h"
28#include "target-descriptions.h"
29#include "xml-support.h"
30#include "xml-tdesc.h"
31
108546a0
DJ
32#include "filenames.h"
33
23181151
DJ
34#include "gdb_assert.h"
35
36#if !defined(HAVE_LIBEXPAT)
37
38/* Parse DOCUMENT into a target description. Or don't, since we don't have
39 an XML parser. */
40
41static struct target_desc *
108546a0
DJ
42tdesc_parse_xml (const char *document, xml_fetch_another fetcher,
43 void *fetcher_baton)
23181151
DJ
44{
45 static int have_warned;
46
47 if (!have_warned)
48 {
49 have_warned = 1;
50 warning (_("Can not parse XML target description; XML support was "
51 "disabled at compile time"));
52 }
53
54 return NULL;
55}
56
57#else /* HAVE_LIBEXPAT */
58
fc6e0168
DJ
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. */
66
67struct tdesc_xml_cache
68{
69 const char *xml_document;
70 struct target_desc *tdesc;
71};
72typedef struct tdesc_xml_cache tdesc_xml_cache_s;
73DEF_VEC_O(tdesc_xml_cache_s);
74
75static VEC(tdesc_xml_cache_s) *xml_cache;
76
23181151
DJ
77/* Callback data for target description parsing. */
78
79struct tdesc_parsing_data
80{
81 /* The target description we are building. */
82 struct target_desc *tdesc;
123dc839
DJ
83
84 /* The target feature we are currently parsing, or last parsed. */
85 struct tdesc_feature *current_feature;
86
87 /* The register number to use for the next register we see, if
88 it does not have its own. This starts at zero. */
89 int next_regnum;
90
91 /* The union we are currently parsing, or last parsed. */
92 struct type *current_union;
23181151
DJ
93};
94
95/* Handle the end of an <architecture> element and its value. */
96
97static void
98tdesc_end_arch (struct gdb_xml_parser *parser,
99 const struct gdb_xml_element *element,
100 void *user_data, const char *body_text)
101{
102 struct tdesc_parsing_data *data = user_data;
103 const struct bfd_arch_info *arch;
104
105 arch = bfd_scan_arch (body_text);
106 if (arch == NULL)
107 gdb_xml_error (parser, _("Target description specified unknown "
108 "architecture \"%s\""), body_text);
109 set_tdesc_architecture (data->tdesc, arch);
110}
111
1780a0ed
DJ
112/* Handle the start of a <target> element. */
113
114static void
115tdesc_start_target (struct gdb_xml_parser *parser,
116 const struct gdb_xml_element *element,
117 void *user_data, VEC(gdb_xml_value_s) *attributes)
118{
119 struct tdesc_parsing_data *data = user_data;
120 char *version = VEC_index (gdb_xml_value_s, attributes, 0)->value;
121
122 if (strcmp (version, "1.0") != 0)
123 gdb_xml_error (parser,
124 _("Target description has unsupported version \"%s\""),
125 version);
126}
127
123dc839
DJ
128/* Handle the start of a <feature> element. */
129
130static void
131tdesc_start_feature (struct gdb_xml_parser *parser,
132 const struct gdb_xml_element *element,
133 void *user_data, VEC(gdb_xml_value_s) *attributes)
134{
135 struct tdesc_parsing_data *data = user_data;
136 char *name = VEC_index (gdb_xml_value_s, attributes, 0)->value;
137
138 data->current_feature = tdesc_create_feature (data->tdesc, name);
139}
140
141/* Handle the start of a <reg> element. Fill in the optional
142 attributes and attach it to the containing feature. */
143
144static void
145tdesc_start_reg (struct gdb_xml_parser *parser,
146 const struct gdb_xml_element *element,
147 void *user_data, VEC(gdb_xml_value_s) *attributes)
148{
149 struct tdesc_parsing_data *data = user_data;
150 struct gdb_xml_value *attrs = VEC_address (gdb_xml_value_s, attributes);
151 int ix = 0, length;
152 char *name, *group, *type;
153 int bitsize, regnum, save_restore;
154
155 length = VEC_length (gdb_xml_value_s, attributes);
156
157 name = attrs[ix++].value;
158 bitsize = * (ULONGEST *) attrs[ix++].value;
159
160 if (ix < length && strcmp (attrs[ix].name, "regnum") == 0)
161 regnum = * (ULONGEST *) attrs[ix++].value;
162 else
163 regnum = data->next_regnum;
164
165 if (ix < length && strcmp (attrs[ix].name, "type") == 0)
166 type = attrs[ix++].value;
167 else
168 type = "int";
169
170 if (ix < length && strcmp (attrs[ix].name, "group") == 0)
171 group = attrs[ix++].value;
172 else
173 group = NULL;
174
175 if (ix < length && strcmp (attrs[ix].name, "save-restore") == 0)
176 save_restore = * (ULONGEST *) attrs[ix++].value;
177 else
178 save_restore = 1;
179
180 if (strcmp (type, "int") != 0
181 && strcmp (type, "float") != 0
d9cc5895
DJ
182 && strcmp (type, "code_ptr") != 0
183 && strcmp (type, "data_ptr") != 0
123dc839
DJ
184 && tdesc_named_type (data->current_feature, type) == NULL)
185 gdb_xml_error (parser, _("Register \"%s\" has unknown type \"%s\""),
186 name, type);
187
188 tdesc_create_reg (data->current_feature, name, regnum, save_restore, group,
189 bitsize, type);
190
191 data->next_regnum = regnum + 1;
192}
193
194/* Handle the start of a <union> element. Initialize the type and
195 record it with the current feature. */
196
197static void
198tdesc_start_union (struct gdb_xml_parser *parser,
199 const struct gdb_xml_element *element,
200 void *user_data, VEC(gdb_xml_value_s) *attributes)
201{
202 struct tdesc_parsing_data *data = user_data;
203 char *id = VEC_index (gdb_xml_value_s, attributes, 0)->value;
204 struct type *type;
205
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;
210}
211
212/* Handle the end of a <union> element. */
213
214static void
215tdesc_end_union (struct gdb_xml_parser *parser,
216 const struct gdb_xml_element *element,
217 void *user_data, const char *body_text)
218{
219 struct tdesc_parsing_data *data = user_data;
220 int i;
221
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)))
227 {
228 TYPE_FLAGS (data->current_union) |= TYPE_FLAG_VECTOR;
229 break;
230 }
231}
232
233/* Handle the start of a <field> element. Attach the field to the
234 current union. */
235
236static void
237tdesc_start_field (struct gdb_xml_parser *parser,
238 const struct gdb_xml_element *element,
239 void *user_data, VEC(gdb_xml_value_s) *attributes)
240{
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;
245
246 field_name = attrs[0].value;
247 field_type_id = attrs[1].value;
248
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 "
252 "type \"%s\""),
253 field_name, field_type_id);
254
255 append_composite_type_field (data->current_union, xstrdup (field_name),
256 field_type);
257}
258
259/* Handle the start of a <vector> element. Initialize the type and
260 record it with the current feature. */
261
262static void
263tdesc_start_vector (struct gdb_xml_parser *parser,
264 const struct gdb_xml_element *element,
265 void *user_data, VEC(gdb_xml_value_s) *attributes)
266{
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;
271 int count;
272
273 id = attrs[0].value;
274 field_type_id = attrs[1].value;
275 count = * (ULONGEST *) attrs[2].value;
276
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\""),
280 id, field_type_id);
281
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);
286
287 TYPE_FLAGS (type) |= TYPE_FLAG_VECTOR;
288
289 tdesc_record_type (data->current_feature, type);
290}
291
23181151
DJ
292/* The elements and attributes of an XML target description. */
293
123dc839
DJ
294static 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 }
298};
299
300static 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 }
304};
305
306static 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 }
315};
316
317static const struct gdb_xml_attribute union_attributes[] = {
318 { "id", GDB_XML_AF_NONE, NULL, NULL },
319 { NULL, GDB_XML_AF_NONE, NULL, NULL }
320};
321
322static 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 }
327};
328
329static const struct gdb_xml_attribute feature_attributes[] = {
330 { "name", GDB_XML_AF_NONE, NULL, NULL },
331 { NULL, GDB_XML_AF_NONE, NULL, NULL }
332};
333
334static 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 }
345};
346
1780a0ed
DJ
347static const struct gdb_xml_attribute target_attributes[] = {
348 { "version", GDB_XML_AF_NONE, NULL, NULL },
349 { NULL, GDB_XML_AF_NONE, NULL, NULL }
350};
351
123dc839 352static const struct gdb_xml_element target_children[] = {
23181151
DJ
353 { "architecture", NULL, NULL, GDB_XML_EF_OPTIONAL,
354 NULL, tdesc_end_arch },
123dc839
DJ
355 { "feature", feature_attributes, feature_children,
356 GDB_XML_EF_OPTIONAL | GDB_XML_EF_REPEATABLE,
357 tdesc_start_feature, NULL },
23181151
DJ
358 { NULL, NULL, NULL, GDB_XML_EF_NONE, NULL, NULL }
359};
360
123dc839 361static const struct gdb_xml_element tdesc_elements[] = {
1780a0ed
DJ
362 { "target", target_attributes, target_children, GDB_XML_EF_NONE,
363 tdesc_start_target, NULL },
23181151
DJ
364 { NULL, NULL, NULL, GDB_XML_EF_NONE, NULL, NULL }
365};
366
367/* Parse DOCUMENT into a target description and return it. */
368
369static struct target_desc *
108546a0
DJ
370tdesc_parse_xml (const char *document, xml_fetch_another fetcher,
371 void *fetcher_baton)
23181151
DJ
372{
373 struct cleanup *back_to, *result_cleanup;
374 struct gdb_xml_parser *parser;
375 struct tdesc_parsing_data data;
fc6e0168 376 struct tdesc_xml_cache *cache;
108546a0 377 char *expanded_text;
fc6e0168 378 int ix;
23181151 379
108546a0
DJ
380 /* Expand all XInclude directives. */
381 expanded_text = xml_process_xincludes (_("target description"),
382 document, fetcher, fetcher_baton, 0);
383 if (expanded_text == NULL)
384 {
385 warning (_("Could not load XML target description; ignoring"));
386 return NULL;
387 }
23181151 388
fc6e0168
DJ
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)
394 {
395 xfree (expanded_text);
396 return cache->tdesc;
397 }
398
399 back_to = make_cleanup (null_cleanup, NULL);
23181151
DJ
400 parser = gdb_xml_create_parser_and_cleanup (_("target description"),
401 tdesc_elements, &data);
108546a0 402 gdb_xml_use_dtd (parser, "gdb-target.dtd");
23181151 403
108546a0 404 memset (&data, 0, sizeof (struct tdesc_parsing_data));
23181151
DJ
405 data.tdesc = allocate_target_description ();
406 result_cleanup = make_cleanup_free_target_description (data.tdesc);
fc6e0168 407 make_cleanup (xfree, expanded_text);
23181151 408
108546a0 409 if (gdb_xml_parse (parser, expanded_text) == 0)
23181151
DJ
410 {
411 /* Parsed successfully. */
fc6e0168
DJ
412 struct tdesc_xml_cache new_cache;
413
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);
23181151
DJ
417 discard_cleanups (result_cleanup);
418 do_cleanups (back_to);
419 return data.tdesc;
420 }
421 else
422 {
423 warning (_("Could not load XML target description; ignoring"));
424 do_cleanups (back_to);
425 return NULL;
426 }
427}
23181151
DJ
428#endif /* HAVE_LIBEXPAT */
429\f
430
431/* Close FILE. */
432
433static void
434do_cleanup_fclose (void *file)
435{
436 fclose (file);
437}
438
439/* Open FILENAME, read all its text into memory, close it, and return
440 the text. If something goes wrong, return NULL and warn. */
441
442static char *
108546a0 443fetch_xml_from_file (const char *filename, void *baton)
23181151 444{
108546a0 445 const char *dirname = baton;
23181151
DJ
446 FILE *file;
447 struct cleanup *back_to;
448 char *text;
449 size_t len, offset;
450
108546a0 451 if (dirname && *dirname)
23181151 452 {
108546a0
DJ
453 char *fullname = concat (dirname, "/", filename, NULL);
454 if (fullname == NULL)
455 nomem (0);
456 file = fopen (fullname, FOPEN_RT);
457 xfree (fullname);
23181151 458 }
108546a0
DJ
459 else
460 file = fopen (filename, FOPEN_RT);
461
462 if (file == NULL)
463 return NULL;
464
23181151
DJ
465 back_to = make_cleanup (do_cleanup_fclose, file);
466
467 /* Read in the whole file, one chunk at a time. */
468 len = 4096;
469 offset = 0;
470 text = xmalloc (len);
471 make_cleanup (free_current_contents, &text);
472 while (1)
473 {
474 size_t bytes_read;
475
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);
479 if (ferror (file))
480 {
481 warning (_("Read error from \"%s\""), filename);
482 do_cleanups (back_to);
483 return NULL;
484 }
485
486 offset += bytes_read;
487
488 if (feof (file))
489 break;
490
491 len = len * 2;
492 text = xrealloc (text, len);
493 }
494
495 fclose (file);
496 discard_cleanups (back_to);
497
498 text[offset] = '\0';
499 return text;
500}
501
502/* Read an XML target description from FILENAME. Parse it, and return
503 the parsed description. */
504
505const struct target_desc *
506file_read_description_xml (const char *filename)
507{
508 struct target_desc *tdesc;
509 char *tdesc_str;
510 struct cleanup *back_to;
108546a0 511 char *dirname;
23181151 512
108546a0 513 tdesc_str = fetch_xml_from_file (filename, NULL);
23181151 514 if (tdesc_str == NULL)
108546a0
DJ
515 {
516 warning (_("Could not open \"%s\""), filename);
517 return NULL;
518 }
23181151
DJ
519
520 back_to = make_cleanup (xfree, tdesc_str);
108546a0 521
e1024ff1
DJ
522 dirname = ldirname (filename);
523 if (dirname != NULL)
524 make_cleanup (xfree, dirname);
108546a0
DJ
525
526 tdesc = tdesc_parse_xml (tdesc_str, fetch_xml_from_file, dirname);
23181151
DJ
527 do_cleanups (back_to);
528
529 return tdesc;
530}
531
108546a0
DJ
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
537 for <xi:include>. */
538
539static char *
540fetch_available_features_from_target (const char *name, void *baton_)
541{
542 struct target_ops *ops = baton_;
543
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,
548 name);
549}
550\f
551
23181151
DJ
552/* Read an XML target description using OPS. Parse it, and return the
553 parsed description. */
554
555const struct target_desc *
556target_read_description_xml (struct target_ops *ops)
557{
558 struct target_desc *tdesc;
559 char *tdesc_str;
560 struct cleanup *back_to;
561
108546a0 562 tdesc_str = fetch_available_features_from_target ("target.xml", ops);
23181151
DJ
563 if (tdesc_str == NULL)
564 return NULL;
565
566 back_to = make_cleanup (xfree, tdesc_str);
108546a0
DJ
567 tdesc = tdesc_parse_xml (tdesc_str,
568 fetch_available_features_from_target,
569 ops);
23181151
DJ
570 do_cleanups (back_to);
571
572 return tdesc;
573}