* Makefile.in (arm-tdep.o, eval.o, target-descriptions.o)
[deliverable/binutils-gdb.git] / gdb / xml-tdesc.c
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"
26 #include "gdbtypes.h"
27 #include "target.h"
28 #include "target-descriptions.h"
29 #include "xml-support.h"
30 #include "xml-tdesc.h"
31
32 #include "filenames.h"
33
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
41 static struct target_desc *
42 tdesc_parse_xml (const char *document, xml_fetch_another fetcher,
43 void *fetcher_baton)
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
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
67 struct tdesc_xml_cache
68 {
69 const char *xml_document;
70 struct target_desc *tdesc;
71 };
72 typedef struct tdesc_xml_cache tdesc_xml_cache_s;
73 DEF_VEC_O(tdesc_xml_cache_s);
74
75 static VEC(tdesc_xml_cache_s) *xml_cache;
76
77 /* Callback data for target description parsing. */
78
79 struct tdesc_parsing_data
80 {
81 /* The target description we are building. */
82 struct target_desc *tdesc;
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;
93 };
94
95 /* Handle the end of an <architecture> element and its value. */
96
97 static void
98 tdesc_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
112 /* Handle the start of a <feature> element. */
113
114 static void
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)
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
128 static void
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)
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
166 && tdesc_named_type (data->current_feature, type) == NULL)
167 gdb_xml_error (parser, _("Register \"%s\" has unknown type \"%s\""),
168 name, type);
169
170 tdesc_create_reg (data->current_feature, name, regnum, save_restore, group,
171 bitsize, type);
172
173 data->next_regnum = regnum + 1;
174 }
175
176 /* Handle the start of a <union> element. Initialize the type and
177 record it with the current feature. */
178
179 static void
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)
183 {
184 struct tdesc_parsing_data *data = user_data;
185 char *id = VEC_index (gdb_xml_value_s, attributes, 0)->value;
186 struct type *type;
187
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;
192 }
193
194 /* Handle the end of a <union> element. */
195
196 static void
197 tdesc_end_union (struct gdb_xml_parser *parser,
198 const struct gdb_xml_element *element,
199 void *user_data, const char *body_text)
200 {
201 struct tdesc_parsing_data *data = user_data;
202 int i;
203
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)))
209 {
210 TYPE_FLAGS (data->current_union) |= TYPE_FLAG_VECTOR;
211 break;
212 }
213 }
214
215 /* Handle the start of a <field> element. Attach the field to the
216 current union. */
217
218 static void
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)
222 {
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;
227
228 field_name = attrs[0].value;
229 field_type_id = attrs[1].value;
230
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 "
234 "type \"%s\""),
235 field_name, field_type_id);
236
237 append_composite_type_field (data->current_union, xstrdup (field_name),
238 field_type);
239 }
240
241 /* Handle the start of a <vector> element. Initialize the type and
242 record it with the current feature. */
243
244 static void
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)
248 {
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;
253 int count;
254
255 id = attrs[0].value;
256 field_type_id = attrs[1].value;
257 count = * (ULONGEST *) attrs[2].value;
258
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\""),
262 id, field_type_id);
263
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);
268
269 TYPE_FLAGS (type) |= TYPE_FLAG_VECTOR;
270
271 tdesc_record_type (data->current_feature, type);
272 }
273
274 /* The elements and attributes of an XML target description. */
275
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 }
280 };
281
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 }
286 };
287
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 }
297 };
298
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 }
302 };
303
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 }
309 };
310
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 }
314 };
315
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 }
327 };
328
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 }
336 };
337
338 static const struct gdb_xml_element tdesc_elements[] = {
339 { "target", NULL, target_children, GDB_XML_EF_NONE,
340 NULL, NULL },
341 { NULL, NULL, NULL, GDB_XML_EF_NONE, NULL, NULL }
342 };
343
344 /* Parse DOCUMENT into a target description and return it. */
345
346 static struct target_desc *
347 tdesc_parse_xml (const char *document, xml_fetch_another fetcher,
348 void *fetcher_baton)
349 {
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;
354 char *expanded_text;
355 int ix;
356
357 /* Expand all XInclude directives. */
358 expanded_text = xml_process_xincludes (_("target description"),
359 document, fetcher, fetcher_baton, 0);
360 if (expanded_text == NULL)
361 {
362 warning (_("Could not load XML target description; ignoring"));
363 return NULL;
364 }
365
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)
371 {
372 xfree (expanded_text);
373 return cache->tdesc;
374 }
375
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");
380
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);
385
386 if (gdb_xml_parse (parser, expanded_text) == 0)
387 {
388 /* Parsed successfully. */
389 struct tdesc_xml_cache new_cache;
390
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);
396 return data.tdesc;
397 }
398 else
399 {
400 warning (_("Could not load XML target description; ignoring"));
401 do_cleanups (back_to);
402 return NULL;
403 }
404 }
405 #endif /* HAVE_LIBEXPAT */
406 \f
407
408 /* Close FILE. */
409
410 static void
411 do_cleanup_fclose (void *file)
412 {
413 fclose (file);
414 }
415
416 /* Open FILENAME, read all its text into memory, close it, and return
417 the text. If something goes wrong, return NULL and warn. */
418
419 static char *
420 fetch_xml_from_file (const char *filename, void *baton)
421 {
422 const char *dirname = baton;
423 FILE *file;
424 struct cleanup *back_to;
425 char *text;
426 size_t len, offset;
427
428 if (dirname && *dirname)
429 {
430 char *fullname = concat (dirname, "/", filename, NULL);
431 if (fullname == NULL)
432 nomem (0);
433 file = fopen (fullname, FOPEN_RT);
434 xfree (fullname);
435 }
436 else
437 file = fopen (filename, FOPEN_RT);
438
439 if (file == NULL)
440 return NULL;
441
442 back_to = make_cleanup (do_cleanup_fclose, file);
443
444 /* Read in the whole file, one chunk at a time. */
445 len = 4096;
446 offset = 0;
447 text = xmalloc (len);
448 make_cleanup (free_current_contents, &text);
449 while (1)
450 {
451 size_t bytes_read;
452
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);
456 if (ferror (file))
457 {
458 warning (_("Read error from \"%s\""), filename);
459 do_cleanups (back_to);
460 return NULL;
461 }
462
463 offset += bytes_read;
464
465 if (feof (file))
466 break;
467
468 len = len * 2;
469 text = xrealloc (text, len);
470 }
471
472 fclose (file);
473 discard_cleanups (back_to);
474
475 text[offset] = '\0';
476 return text;
477 }
478
479 /* Read an XML target description from FILENAME. Parse it, and return
480 the parsed description. */
481
482 const struct target_desc *
483 file_read_description_xml (const char *filename)
484 {
485 struct target_desc *tdesc;
486 char *tdesc_str;
487 struct cleanup *back_to;
488 const char *base;
489 char *dirname;
490
491 tdesc_str = fetch_xml_from_file (filename, NULL);
492 if (tdesc_str == NULL)
493 {
494 warning (_("Could not open \"%s\""), filename);
495 return NULL;
496 }
497
498 back_to = make_cleanup (xfree, tdesc_str);
499
500 /* Simple, portable version of dirname that does not modify its
501 argument. */
502 base = lbasename (filename);
503 while (base > filename && IS_DIR_SEPARATOR (base[-1]))
504 --base;
505 if (base > filename)
506 {
507 dirname = xmalloc (base - filename + 2);
508 memcpy (dirname, filename, base - filename);
509
510 /* On DOS based file systems, convert "d:foo" to "d:.", so that
511 we create "d:./bar" later instead of the (different)
512 "d:/bar". */
513 if (base - filename == 2 && IS_ABSOLUTE_PATH (base)
514 && !IS_DIR_SEPARATOR (filename[0]))
515 dirname[base++ - filename] = '.';
516
517 dirname[base - filename] = '\0';
518 make_cleanup (xfree, dirname);
519 }
520 else
521 dirname = NULL;
522
523 tdesc = tdesc_parse_xml (tdesc_str, fetch_xml_from_file, dirname);
524 do_cleanups (back_to);
525
526 return tdesc;
527 }
528
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
534 for <xi:include>. */
535
536 static char *
537 fetch_available_features_from_target (const char *name, void *baton_)
538 {
539 struct target_ops *ops = baton_;
540
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,
545 name);
546 }
547 \f
548
549 /* Read an XML target description using OPS. Parse it, and return the
550 parsed description. */
551
552 const struct target_desc *
553 target_read_description_xml (struct target_ops *ops)
554 {
555 struct target_desc *tdesc;
556 char *tdesc_str;
557 struct cleanup *back_to;
558
559 tdesc_str = fetch_available_features_from_target ("target.xml", ops);
560 if (tdesc_str == NULL)
561 return NULL;
562
563 back_to = make_cleanup (xfree, tdesc_str);
564 tdesc = tdesc_parse_xml (tdesc_str,
565 fetch_available_features_from_target,
566 ops);
567 do_cleanups (back_to);
568
569 return tdesc;
570 }
This page took 0.05544 seconds and 5 git commands to generate.