* defs.h (ldirname): New prototype.
[deliverable/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
123dc839
DJ
112/* Handle the start of a <feature> element. */
113
114static void
115tdesc_start_feature (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 *name = VEC_index (gdb_xml_value_s, attributes, 0)->value;
121
122 data->current_feature = tdesc_create_feature (data->tdesc, name);
123}
124
125/* Handle the start of a <reg> element. Fill in the optional
126 attributes and attach it to the containing feature. */
127
128static void
129tdesc_start_reg (struct gdb_xml_parser *parser,
130 const struct gdb_xml_element *element,
131 void *user_data, VEC(gdb_xml_value_s) *attributes)
132{
133 struct tdesc_parsing_data *data = user_data;
134 struct gdb_xml_value *attrs = VEC_address (gdb_xml_value_s, attributes);
135 int ix = 0, length;
136 char *name, *group, *type;
137 int bitsize, regnum, save_restore;
138
139 length = VEC_length (gdb_xml_value_s, attributes);
140
141 name = attrs[ix++].value;
142 bitsize = * (ULONGEST *) attrs[ix++].value;
143
144 if (ix < length && strcmp (attrs[ix].name, "regnum") == 0)
145 regnum = * (ULONGEST *) attrs[ix++].value;
146 else
147 regnum = data->next_regnum;
148
149 if (ix < length && strcmp (attrs[ix].name, "type") == 0)
150 type = attrs[ix++].value;
151 else
152 type = "int";
153
154 if (ix < length && strcmp (attrs[ix].name, "group") == 0)
155 group = attrs[ix++].value;
156 else
157 group = NULL;
158
159 if (ix < length && strcmp (attrs[ix].name, "save-restore") == 0)
160 save_restore = * (ULONGEST *) attrs[ix++].value;
161 else
162 save_restore = 1;
163
164 if (strcmp (type, "int") != 0
165 && strcmp (type, "float") != 0
d9cc5895
DJ
166 && strcmp (type, "code_ptr") != 0
167 && strcmp (type, "data_ptr") != 0
123dc839
DJ
168 && tdesc_named_type (data->current_feature, type) == NULL)
169 gdb_xml_error (parser, _("Register \"%s\" has unknown type \"%s\""),
170 name, type);
171
172 tdesc_create_reg (data->current_feature, name, regnum, save_restore, group,
173 bitsize, type);
174
175 data->next_regnum = regnum + 1;
176}
177
178/* Handle the start of a <union> element. Initialize the type and
179 record it with the current feature. */
180
181static void
182tdesc_start_union (struct gdb_xml_parser *parser,
183 const struct gdb_xml_element *element,
184 void *user_data, VEC(gdb_xml_value_s) *attributes)
185{
186 struct tdesc_parsing_data *data = user_data;
187 char *id = VEC_index (gdb_xml_value_s, attributes, 0)->value;
188 struct type *type;
189
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;
194}
195
196/* Handle the end of a <union> element. */
197
198static void
199tdesc_end_union (struct gdb_xml_parser *parser,
200 const struct gdb_xml_element *element,
201 void *user_data, const char *body_text)
202{
203 struct tdesc_parsing_data *data = user_data;
204 int i;
205
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)))
211 {
212 TYPE_FLAGS (data->current_union) |= TYPE_FLAG_VECTOR;
213 break;
214 }
215}
216
217/* Handle the start of a <field> element. Attach the field to the
218 current union. */
219
220static void
221tdesc_start_field (struct gdb_xml_parser *parser,
222 const struct gdb_xml_element *element,
223 void *user_data, VEC(gdb_xml_value_s) *attributes)
224{
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;
229
230 field_name = attrs[0].value;
231 field_type_id = attrs[1].value;
232
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 "
236 "type \"%s\""),
237 field_name, field_type_id);
238
239 append_composite_type_field (data->current_union, xstrdup (field_name),
240 field_type);
241}
242
243/* Handle the start of a <vector> element. Initialize the type and
244 record it with the current feature. */
245
246static void
247tdesc_start_vector (struct gdb_xml_parser *parser,
248 const struct gdb_xml_element *element,
249 void *user_data, VEC(gdb_xml_value_s) *attributes)
250{
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;
255 int count;
256
257 id = attrs[0].value;
258 field_type_id = attrs[1].value;
259 count = * (ULONGEST *) attrs[2].value;
260
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\""),
264 id, field_type_id);
265
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);
270
271 TYPE_FLAGS (type) |= TYPE_FLAG_VECTOR;
272
273 tdesc_record_type (data->current_feature, type);
274}
275
23181151
DJ
276/* The elements and attributes of an XML target description. */
277
123dc839
DJ
278static 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 }
282};
283
284static 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 }
288};
289
290static 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 }
299};
300
301static const struct gdb_xml_attribute union_attributes[] = {
302 { "id", GDB_XML_AF_NONE, NULL, NULL },
303 { NULL, GDB_XML_AF_NONE, NULL, NULL }
304};
305
306static 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 }
311};
312
313static const struct gdb_xml_attribute feature_attributes[] = {
314 { "name", GDB_XML_AF_NONE, NULL, NULL },
315 { NULL, GDB_XML_AF_NONE, NULL, NULL }
316};
317
318static 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 }
329};
330
331static const struct gdb_xml_element target_children[] = {
23181151
DJ
332 { "architecture", NULL, NULL, GDB_XML_EF_OPTIONAL,
333 NULL, tdesc_end_arch },
123dc839
DJ
334 { "feature", feature_attributes, feature_children,
335 GDB_XML_EF_OPTIONAL | GDB_XML_EF_REPEATABLE,
336 tdesc_start_feature, NULL },
23181151
DJ
337 { NULL, NULL, NULL, GDB_XML_EF_NONE, NULL, NULL }
338};
339
123dc839 340static const struct gdb_xml_element tdesc_elements[] = {
23181151
DJ
341 { "target", NULL, target_children, GDB_XML_EF_NONE,
342 NULL, NULL },
343 { NULL, NULL, NULL, GDB_XML_EF_NONE, NULL, NULL }
344};
345
346/* Parse DOCUMENT into a target description and return it. */
347
348static struct target_desc *
108546a0
DJ
349tdesc_parse_xml (const char *document, xml_fetch_another fetcher,
350 void *fetcher_baton)
23181151
DJ
351{
352 struct cleanup *back_to, *result_cleanup;
353 struct gdb_xml_parser *parser;
354 struct tdesc_parsing_data data;
fc6e0168 355 struct tdesc_xml_cache *cache;
108546a0 356 char *expanded_text;
fc6e0168 357 int ix;
23181151 358
108546a0
DJ
359 /* Expand all XInclude directives. */
360 expanded_text = xml_process_xincludes (_("target description"),
361 document, fetcher, fetcher_baton, 0);
362 if (expanded_text == NULL)
363 {
364 warning (_("Could not load XML target description; ignoring"));
365 return NULL;
366 }
23181151 367
fc6e0168
DJ
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)
373 {
374 xfree (expanded_text);
375 return cache->tdesc;
376 }
377
378 back_to = make_cleanup (null_cleanup, NULL);
23181151
DJ
379 parser = gdb_xml_create_parser_and_cleanup (_("target description"),
380 tdesc_elements, &data);
108546a0 381 gdb_xml_use_dtd (parser, "gdb-target.dtd");
23181151 382
108546a0 383 memset (&data, 0, sizeof (struct tdesc_parsing_data));
23181151
DJ
384 data.tdesc = allocate_target_description ();
385 result_cleanup = make_cleanup_free_target_description (data.tdesc);
fc6e0168 386 make_cleanup (xfree, expanded_text);
23181151 387
108546a0 388 if (gdb_xml_parse (parser, expanded_text) == 0)
23181151
DJ
389 {
390 /* Parsed successfully. */
fc6e0168
DJ
391 struct tdesc_xml_cache new_cache;
392
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);
23181151
DJ
396 discard_cleanups (result_cleanup);
397 do_cleanups (back_to);
398 return data.tdesc;
399 }
400 else
401 {
402 warning (_("Could not load XML target description; ignoring"));
403 do_cleanups (back_to);
404 return NULL;
405 }
406}
23181151
DJ
407#endif /* HAVE_LIBEXPAT */
408\f
409
410/* Close FILE. */
411
412static void
413do_cleanup_fclose (void *file)
414{
415 fclose (file);
416}
417
418/* Open FILENAME, read all its text into memory, close it, and return
419 the text. If something goes wrong, return NULL and warn. */
420
421static char *
108546a0 422fetch_xml_from_file (const char *filename, void *baton)
23181151 423{
108546a0 424 const char *dirname = baton;
23181151
DJ
425 FILE *file;
426 struct cleanup *back_to;
427 char *text;
428 size_t len, offset;
429
108546a0 430 if (dirname && *dirname)
23181151 431 {
108546a0
DJ
432 char *fullname = concat (dirname, "/", filename, NULL);
433 if (fullname == NULL)
434 nomem (0);
435 file = fopen (fullname, FOPEN_RT);
436 xfree (fullname);
23181151 437 }
108546a0
DJ
438 else
439 file = fopen (filename, FOPEN_RT);
440
441 if (file == NULL)
442 return NULL;
443
23181151
DJ
444 back_to = make_cleanup (do_cleanup_fclose, file);
445
446 /* Read in the whole file, one chunk at a time. */
447 len = 4096;
448 offset = 0;
449 text = xmalloc (len);
450 make_cleanup (free_current_contents, &text);
451 while (1)
452 {
453 size_t bytes_read;
454
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);
458 if (ferror (file))
459 {
460 warning (_("Read error from \"%s\""), filename);
461 do_cleanups (back_to);
462 return NULL;
463 }
464
465 offset += bytes_read;
466
467 if (feof (file))
468 break;
469
470 len = len * 2;
471 text = xrealloc (text, len);
472 }
473
474 fclose (file);
475 discard_cleanups (back_to);
476
477 text[offset] = '\0';
478 return text;
479}
480
481/* Read an XML target description from FILENAME. Parse it, and return
482 the parsed description. */
483
484const struct target_desc *
485file_read_description_xml (const char *filename)
486{
487 struct target_desc *tdesc;
488 char *tdesc_str;
489 struct cleanup *back_to;
108546a0 490 char *dirname;
23181151 491
108546a0 492 tdesc_str = fetch_xml_from_file (filename, NULL);
23181151 493 if (tdesc_str == NULL)
108546a0
DJ
494 {
495 warning (_("Could not open \"%s\""), filename);
496 return NULL;
497 }
23181151
DJ
498
499 back_to = make_cleanup (xfree, tdesc_str);
108546a0 500
e1024ff1
DJ
501 dirname = ldirname (filename);
502 if (dirname != NULL)
503 make_cleanup (xfree, dirname);
108546a0
DJ
504
505 tdesc = tdesc_parse_xml (tdesc_str, fetch_xml_from_file, dirname);
23181151
DJ
506 do_cleanups (back_to);
507
508 return tdesc;
509}
510
108546a0
DJ
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
516 for <xi:include>. */
517
518static char *
519fetch_available_features_from_target (const char *name, void *baton_)
520{
521 struct target_ops *ops = baton_;
522
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,
527 name);
528}
529\f
530
23181151
DJ
531/* Read an XML target description using OPS. Parse it, and return the
532 parsed description. */
533
534const struct target_desc *
535target_read_description_xml (struct target_ops *ops)
536{
537 struct target_desc *tdesc;
538 char *tdesc_str;
539 struct cleanup *back_to;
540
108546a0 541 tdesc_str = fetch_available_features_from_target ("target.xml", ops);
23181151
DJ
542 if (tdesc_str == NULL)
543 return NULL;
544
545 back_to = make_cleanup (xfree, tdesc_str);
108546a0
DJ
546 tdesc = tdesc_parse_xml (tdesc_str,
547 fetch_available_features_from_target,
548 ops);
23181151
DJ
549 do_cleanups (back_to);
550
551 return tdesc;
552}
This page took 0.080592 seconds and 4 git commands to generate.