]>
Commit | Line | Data |
---|---|---|
23181151 DJ |
1 | /* XML target description support for GDB. |
2 | ||
618f726f | 3 | Copyright (C) 2006-2016 Free Software Foundation, Inc. |
23181151 DJ |
4 | |
5 | Contributed by CodeSourcery. | |
6 | ||
7 | This file is part of GDB. | |
8 | ||
9 | This program is free software; you can redistribute it and/or modify | |
10 | it under the terms of the GNU General Public License as published by | |
a9762ec7 | 11 | the Free Software Foundation; either version 3 of the License, or |
23181151 DJ |
12 | (at your option) any later version. |
13 | ||
14 | This program is distributed in the hope that it will be useful, | |
15 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
17 | GNU General Public License for more details. | |
18 | ||
19 | You should have received a copy of the GNU General Public License | |
a9762ec7 | 20 | along with this program. If not, see <http://www.gnu.org/licenses/>. */ |
23181151 DJ |
21 | |
22 | #include "defs.h" | |
23 | #include "target.h" | |
24 | #include "target-descriptions.h" | |
25 | #include "xml-support.h" | |
26 | #include "xml-tdesc.h" | |
08d16641 | 27 | #include "osabi.h" |
23181151 | 28 | |
108546a0 DJ |
29 | #include "filenames.h" |
30 | ||
23181151 DJ |
31 | #if !defined(HAVE_LIBEXPAT) |
32 | ||
33 | /* Parse DOCUMENT into a target description. Or don't, since we don't have | |
34 | an XML parser. */ | |
35 | ||
36 | static struct target_desc * | |
108546a0 DJ |
37 | tdesc_parse_xml (const char *document, xml_fetch_another fetcher, |
38 | void *fetcher_baton) | |
23181151 DJ |
39 | { |
40 | static int have_warned; | |
41 | ||
42 | if (!have_warned) | |
43 | { | |
44 | have_warned = 1; | |
45 | warning (_("Can not parse XML target description; XML support was " | |
46 | "disabled at compile time")); | |
47 | } | |
48 | ||
49 | return NULL; | |
50 | } | |
51 | ||
52 | #else /* HAVE_LIBEXPAT */ | |
53 | ||
fc6e0168 DJ |
54 | /* A record of every XML description we have parsed. We never discard |
55 | old descriptions, because we never discard gdbarches. As long as we | |
56 | have a gdbarch referencing this description, we want to have a copy | |
57 | of it here, so that if we parse the same XML document again we can | |
58 | return the same "struct target_desc *"; if they are not singletons, | |
59 | then we will create unnecessary duplicate gdbarches. See | |
60 | gdbarch_list_lookup_by_info. */ | |
61 | ||
62 | struct tdesc_xml_cache | |
63 | { | |
64 | const char *xml_document; | |
65 | struct target_desc *tdesc; | |
66 | }; | |
67 | typedef struct tdesc_xml_cache tdesc_xml_cache_s; | |
68 | DEF_VEC_O(tdesc_xml_cache_s); | |
69 | ||
70 | static VEC(tdesc_xml_cache_s) *xml_cache; | |
71 | ||
23181151 DJ |
72 | /* Callback data for target description parsing. */ |
73 | ||
74 | struct tdesc_parsing_data | |
75 | { | |
76 | /* The target description we are building. */ | |
77 | struct target_desc *tdesc; | |
123dc839 DJ |
78 | |
79 | /* The target feature we are currently parsing, or last parsed. */ | |
80 | struct tdesc_feature *current_feature; | |
81 | ||
82 | /* The register number to use for the next register we see, if | |
83 | it does not have its own. This starts at zero. */ | |
84 | int next_regnum; | |
85 | ||
f5dff777 DJ |
86 | /* The struct or union we are currently parsing, or last parsed. */ |
87 | struct tdesc_type *current_type; | |
88 | ||
89 | /* The byte size of the current struct type, if specified. Zero | |
90 | if not specified. */ | |
91 | int current_type_size; | |
92 | ||
93 | /* Whether the current type is a flags type. */ | |
94 | int current_type_is_flags; | |
23181151 DJ |
95 | }; |
96 | ||
97 | /* Handle the end of an <architecture> element and its value. */ | |
98 | ||
99 | static void | |
100 | tdesc_end_arch (struct gdb_xml_parser *parser, | |
101 | const struct gdb_xml_element *element, | |
102 | void *user_data, const char *body_text) | |
103 | { | |
19ba03f4 | 104 | struct tdesc_parsing_data *data = (struct tdesc_parsing_data *) user_data; |
23181151 DJ |
105 | const struct bfd_arch_info *arch; |
106 | ||
107 | arch = bfd_scan_arch (body_text); | |
108 | if (arch == NULL) | |
109 | gdb_xml_error (parser, _("Target description specified unknown " | |
110 | "architecture \"%s\""), body_text); | |
111 | set_tdesc_architecture (data->tdesc, arch); | |
112 | } | |
113 | ||
08d16641 PA |
114 | /* Handle the end of an <osabi> element and its value. */ |
115 | ||
116 | static void | |
117 | tdesc_end_osabi (struct gdb_xml_parser *parser, | |
118 | const struct gdb_xml_element *element, | |
119 | void *user_data, const char *body_text) | |
120 | { | |
19ba03f4 | 121 | struct tdesc_parsing_data *data = (struct tdesc_parsing_data *) user_data; |
08d16641 PA |
122 | enum gdb_osabi osabi; |
123 | ||
124 | osabi = osabi_from_tdesc_string (body_text); | |
125 | if (osabi == GDB_OSABI_UNKNOWN) | |
126 | warning (_("Target description specified unknown osabi \"%s\""), | |
127 | body_text); | |
128 | else | |
129 | set_tdesc_osabi (data->tdesc, osabi); | |
130 | } | |
131 | ||
e35359c5 UW |
132 | /* Handle the end of a <compatible> element and its value. */ |
133 | ||
134 | static void | |
135 | tdesc_end_compatible (struct gdb_xml_parser *parser, | |
136 | const struct gdb_xml_element *element, | |
137 | void *user_data, const char *body_text) | |
138 | { | |
19ba03f4 | 139 | struct tdesc_parsing_data *data = (struct tdesc_parsing_data *) user_data; |
e35359c5 UW |
140 | const struct bfd_arch_info *arch; |
141 | ||
142 | arch = bfd_scan_arch (body_text); | |
143 | tdesc_add_compatible (data->tdesc, arch); | |
144 | } | |
145 | ||
1780a0ed DJ |
146 | /* Handle the start of a <target> element. */ |
147 | ||
148 | static void | |
149 | tdesc_start_target (struct gdb_xml_parser *parser, | |
150 | const struct gdb_xml_element *element, | |
151 | void *user_data, VEC(gdb_xml_value_s) *attributes) | |
152 | { | |
19ba03f4 | 153 | char *version = (char *) xml_find_attribute (attributes, "version")->value; |
1780a0ed DJ |
154 | |
155 | if (strcmp (version, "1.0") != 0) | |
156 | gdb_xml_error (parser, | |
157 | _("Target description has unsupported version \"%s\""), | |
158 | version); | |
159 | } | |
160 | ||
123dc839 DJ |
161 | /* Handle the start of a <feature> element. */ |
162 | ||
163 | static void | |
164 | tdesc_start_feature (struct gdb_xml_parser *parser, | |
165 | const struct gdb_xml_element *element, | |
166 | void *user_data, VEC(gdb_xml_value_s) *attributes) | |
167 | { | |
19ba03f4 SM |
168 | struct tdesc_parsing_data *data = (struct tdesc_parsing_data *) user_data; |
169 | char *name = (char *) xml_find_attribute (attributes, "name")->value; | |
123dc839 DJ |
170 | |
171 | data->current_feature = tdesc_create_feature (data->tdesc, name); | |
172 | } | |
173 | ||
174 | /* Handle the start of a <reg> element. Fill in the optional | |
175 | attributes and attach it to the containing feature. */ | |
176 | ||
177 | static void | |
178 | tdesc_start_reg (struct gdb_xml_parser *parser, | |
179 | const struct gdb_xml_element *element, | |
180 | void *user_data, VEC(gdb_xml_value_s) *attributes) | |
181 | { | |
19ba03f4 | 182 | struct tdesc_parsing_data *data = (struct tdesc_parsing_data *) user_data; |
123dc839 DJ |
183 | struct gdb_xml_value *attrs = VEC_address (gdb_xml_value_s, attributes); |
184 | int ix = 0, length; | |
185 | char *name, *group, *type; | |
186 | int bitsize, regnum, save_restore; | |
187 | ||
188 | length = VEC_length (gdb_xml_value_s, attributes); | |
189 | ||
19ba03f4 | 190 | name = (char *) attrs[ix++].value; |
123dc839 DJ |
191 | bitsize = * (ULONGEST *) attrs[ix++].value; |
192 | ||
193 | if (ix < length && strcmp (attrs[ix].name, "regnum") == 0) | |
194 | regnum = * (ULONGEST *) attrs[ix++].value; | |
195 | else | |
196 | regnum = data->next_regnum; | |
197 | ||
198 | if (ix < length && strcmp (attrs[ix].name, "type") == 0) | |
19ba03f4 | 199 | type = (char *) attrs[ix++].value; |
123dc839 DJ |
200 | else |
201 | type = "int"; | |
202 | ||
203 | if (ix < length && strcmp (attrs[ix].name, "group") == 0) | |
19ba03f4 | 204 | group = (char *) attrs[ix++].value; |
123dc839 DJ |
205 | else |
206 | group = NULL; | |
207 | ||
208 | if (ix < length && strcmp (attrs[ix].name, "save-restore") == 0) | |
209 | save_restore = * (ULONGEST *) attrs[ix++].value; | |
210 | else | |
211 | save_restore = 1; | |
212 | ||
213 | if (strcmp (type, "int") != 0 | |
214 | && strcmp (type, "float") != 0 | |
215 | && tdesc_named_type (data->current_feature, type) == NULL) | |
216 | gdb_xml_error (parser, _("Register \"%s\" has unknown type \"%s\""), | |
217 | name, type); | |
218 | ||
219 | tdesc_create_reg (data->current_feature, name, regnum, save_restore, group, | |
220 | bitsize, type); | |
221 | ||
222 | data->next_regnum = regnum + 1; | |
223 | } | |
224 | ||
225 | /* Handle the start of a <union> element. Initialize the type and | |
226 | record it with the current feature. */ | |
227 | ||
228 | static void | |
229 | tdesc_start_union (struct gdb_xml_parser *parser, | |
230 | const struct gdb_xml_element *element, | |
231 | void *user_data, VEC(gdb_xml_value_s) *attributes) | |
232 | { | |
19ba03f4 SM |
233 | struct tdesc_parsing_data *data = (struct tdesc_parsing_data *) user_data; |
234 | char *id = (char *) xml_find_attribute (attributes, "id")->value; | |
123dc839 | 235 | |
f5dff777 DJ |
236 | data->current_type = tdesc_create_union (data->current_feature, id); |
237 | data->current_type_size = 0; | |
238 | data->current_type_is_flags = 0; | |
239 | } | |
240 | ||
241 | /* Handle the start of a <struct> element. Initialize the type and | |
242 | record it with the current feature. */ | |
243 | ||
244 | static void | |
245 | tdesc_start_struct (struct gdb_xml_parser *parser, | |
246 | const struct gdb_xml_element *element, | |
247 | void *user_data, VEC(gdb_xml_value_s) *attributes) | |
248 | { | |
19ba03f4 SM |
249 | struct tdesc_parsing_data *data = (struct tdesc_parsing_data *) user_data; |
250 | char *id = (char *) xml_find_attribute (attributes, "id")->value; | |
f5dff777 | 251 | struct tdesc_type *type; |
3d2c1d41 | 252 | struct gdb_xml_value *attr; |
f5dff777 DJ |
253 | |
254 | type = tdesc_create_struct (data->current_feature, id); | |
255 | data->current_type = type; | |
256 | data->current_type_size = 0; | |
257 | data->current_type_is_flags = 0; | |
258 | ||
3d2c1d41 PA |
259 | attr = xml_find_attribute (attributes, "size"); |
260 | if (attr != NULL) | |
f5dff777 | 261 | { |
3d2c1d41 | 262 | int size = (int) * (ULONGEST *) attr->value; |
a109c7c1 | 263 | |
f5dff777 DJ |
264 | tdesc_set_struct_size (type, size); |
265 | data->current_type_size = size; | |
266 | } | |
267 | } | |
268 | ||
269 | static void | |
270 | tdesc_start_flags (struct gdb_xml_parser *parser, | |
271 | const struct gdb_xml_element *element, | |
272 | void *user_data, VEC(gdb_xml_value_s) *attributes) | |
273 | { | |
19ba03f4 SM |
274 | struct tdesc_parsing_data *data = (struct tdesc_parsing_data *) user_data; |
275 | char *id = (char *) xml_find_attribute (attributes, "id")->value; | |
f5dff777 | 276 | int length = (int) * (ULONGEST *) |
3d2c1d41 | 277 | xml_find_attribute (attributes, "size")->value; |
f5dff777 DJ |
278 | struct tdesc_type *type; |
279 | ||
280 | type = tdesc_create_flags (data->current_feature, id, length); | |
281 | ||
282 | data->current_type = type; | |
283 | data->current_type_size = 0; | |
284 | data->current_type_is_flags = 1; | |
123dc839 DJ |
285 | } |
286 | ||
287 | /* Handle the start of a <field> element. Attach the field to the | |
f5dff777 | 288 | current struct or union. */ |
123dc839 DJ |
289 | |
290 | static void | |
291 | tdesc_start_field (struct gdb_xml_parser *parser, | |
292 | const struct gdb_xml_element *element, | |
293 | void *user_data, VEC(gdb_xml_value_s) *attributes) | |
294 | { | |
19ba03f4 | 295 | struct tdesc_parsing_data *data = (struct tdesc_parsing_data *) user_data; |
3d2c1d41 | 296 | struct gdb_xml_value *attr; |
ad068eab | 297 | struct tdesc_type *field_type; |
123dc839 | 298 | char *field_name, *field_type_id; |
f5dff777 | 299 | int start, end; |
123dc839 | 300 | |
19ba03f4 | 301 | field_name = (char *) xml_find_attribute (attributes, "name")->value; |
123dc839 | 302 | |
3d2c1d41 PA |
303 | attr = xml_find_attribute (attributes, "type"); |
304 | if (attr != NULL) | |
19ba03f4 | 305 | field_type_id = (char *) attr->value; |
f5dff777 DJ |
306 | else |
307 | field_type_id = NULL; | |
308 | ||
3d2c1d41 PA |
309 | attr = xml_find_attribute (attributes, "start"); |
310 | if (attr != NULL) | |
311 | start = * (ULONGEST *) attr->value; | |
f5dff777 DJ |
312 | else |
313 | start = -1; | |
314 | ||
3d2c1d41 PA |
315 | attr = xml_find_attribute (attributes, "end"); |
316 | if (attr != NULL) | |
317 | end = * (ULONGEST *) attr->value; | |
f5dff777 DJ |
318 | else |
319 | end = -1; | |
320 | ||
321 | if (field_type_id != NULL) | |
322 | { | |
323 | if (data->current_type_is_flags) | |
324 | gdb_xml_error (parser, _("Cannot add typed field \"%s\" to flags"), | |
325 | field_name); | |
326 | if (data->current_type_size != 0) | |
327 | gdb_xml_error (parser, | |
3e43a32a MS |
328 | _("Explicitly sized type can not " |
329 | "contain non-bitfield \"%s\""), | |
f5dff777 DJ |
330 | field_name); |
331 | ||
332 | field_type = tdesc_named_type (data->current_feature, field_type_id); | |
333 | if (field_type == NULL) | |
334 | gdb_xml_error (parser, _("Field \"%s\" references undefined " | |
335 | "type \"%s\""), | |
336 | field_name, field_type_id); | |
337 | ||
338 | tdesc_add_field (data->current_type, field_name, field_type); | |
339 | } | |
340 | else if (start != -1 && end != -1) | |
341 | { | |
342 | struct tdesc_type *t = data->current_type; | |
343 | ||
344 | if (data->current_type_is_flags) | |
345 | tdesc_add_flag (t, start, field_name); | |
346 | else | |
347 | { | |
348 | if (data->current_type_size == 0) | |
349 | gdb_xml_error (parser, | |
3e43a32a MS |
350 | _("Implicitly sized type can " |
351 | "not contain bitfield \"%s\""), | |
f5dff777 DJ |
352 | field_name); |
353 | ||
354 | if (end >= 64) | |
355 | gdb_xml_error (parser, | |
3e43a32a MS |
356 | _("Bitfield \"%s\" goes past " |
357 | "64 bits (unsupported)"), | |
f5dff777 DJ |
358 | field_name); |
359 | ||
360 | /* Assume that the bit numbering in XML is "lsb-zero". Most | |
361 | architectures other than PowerPC use this ordering. In | |
362 | the future, we can add an XML tag to indicate "msb-zero" | |
363 | numbering. */ | |
364 | if (start > end) | |
365 | gdb_xml_error (parser, _("Bitfield \"%s\" has start after end"), | |
366 | field_name); | |
367 | ||
368 | if (end >= data->current_type_size * TARGET_CHAR_BIT) | |
3e43a32a MS |
369 | gdb_xml_error (parser, |
370 | _("Bitfield \"%s\" does not fit in struct")); | |
f5dff777 DJ |
371 | |
372 | tdesc_add_bitfield (t, field_name, start, end); | |
373 | } | |
374 | } | |
375 | else | |
376 | gdb_xml_error (parser, _("Field \"%s\" has neither type nor bit position"), | |
377 | field_name); | |
123dc839 DJ |
378 | } |
379 | ||
380 | /* Handle the start of a <vector> element. Initialize the type and | |
381 | record it with the current feature. */ | |
382 | ||
383 | static void | |
384 | tdesc_start_vector (struct gdb_xml_parser *parser, | |
385 | const struct gdb_xml_element *element, | |
386 | void *user_data, VEC(gdb_xml_value_s) *attributes) | |
387 | { | |
19ba03f4 | 388 | struct tdesc_parsing_data *data = (struct tdesc_parsing_data *) user_data; |
123dc839 | 389 | struct gdb_xml_value *attrs = VEC_address (gdb_xml_value_s, attributes); |
ad068eab | 390 | struct tdesc_type *field_type; |
123dc839 DJ |
391 | char *id, *field_type_id; |
392 | int count; | |
393 | ||
19ba03f4 SM |
394 | id = (char *) attrs[0].value; |
395 | field_type_id = (char *) attrs[1].value; | |
123dc839 DJ |
396 | count = * (ULONGEST *) attrs[2].value; |
397 | ||
398 | field_type = tdesc_named_type (data->current_feature, field_type_id); | |
399 | if (field_type == NULL) | |
400 | gdb_xml_error (parser, _("Vector \"%s\" references undefined type \"%s\""), | |
401 | id, field_type_id); | |
402 | ||
ad068eab | 403 | tdesc_create_vector (data->current_feature, id, field_type, count); |
123dc839 DJ |
404 | } |
405 | ||
23181151 DJ |
406 | /* The elements and attributes of an XML target description. */ |
407 | ||
123dc839 DJ |
408 | static const struct gdb_xml_attribute field_attributes[] = { |
409 | { "name", GDB_XML_AF_NONE, NULL, NULL }, | |
f5dff777 DJ |
410 | { "type", GDB_XML_AF_OPTIONAL, NULL, NULL }, |
411 | { "start", GDB_XML_AF_OPTIONAL, gdb_xml_parse_attr_ulongest, NULL }, | |
412 | { "end", GDB_XML_AF_OPTIONAL, gdb_xml_parse_attr_ulongest, NULL }, | |
123dc839 DJ |
413 | { NULL, GDB_XML_AF_NONE, NULL, NULL } |
414 | }; | |
415 | ||
f5dff777 | 416 | static const struct gdb_xml_element struct_union_children[] = { |
123dc839 DJ |
417 | { "field", field_attributes, NULL, GDB_XML_EF_REPEATABLE, |
418 | tdesc_start_field, NULL }, | |
419 | { NULL, NULL, NULL, GDB_XML_EF_NONE, NULL, NULL } | |
420 | }; | |
421 | ||
422 | static const struct gdb_xml_attribute reg_attributes[] = { | |
423 | { "name", GDB_XML_AF_NONE, NULL, NULL }, | |
424 | { "bitsize", GDB_XML_AF_NONE, gdb_xml_parse_attr_ulongest, NULL }, | |
425 | { "regnum", GDB_XML_AF_OPTIONAL, gdb_xml_parse_attr_ulongest, NULL }, | |
426 | { "type", GDB_XML_AF_OPTIONAL, NULL, NULL }, | |
427 | { "group", GDB_XML_AF_OPTIONAL, NULL, NULL }, | |
428 | { "save-restore", GDB_XML_AF_OPTIONAL, | |
429 | gdb_xml_parse_attr_enum, gdb_xml_enums_boolean }, | |
430 | { NULL, GDB_XML_AF_NONE, NULL, NULL } | |
431 | }; | |
432 | ||
f5dff777 | 433 | static const struct gdb_xml_attribute struct_union_attributes[] = { |
123dc839 | 434 | { "id", GDB_XML_AF_NONE, NULL, NULL }, |
f5dff777 DJ |
435 | { "size", GDB_XML_AF_OPTIONAL, gdb_xml_parse_attr_ulongest, NULL}, |
436 | { NULL, GDB_XML_AF_NONE, NULL, NULL } | |
437 | }; | |
438 | ||
439 | static const struct gdb_xml_attribute flags_attributes[] = { | |
440 | { "id", GDB_XML_AF_NONE, NULL, NULL }, | |
441 | { "size", GDB_XML_AF_NONE, gdb_xml_parse_attr_ulongest, NULL}, | |
123dc839 DJ |
442 | { NULL, GDB_XML_AF_NONE, NULL, NULL } |
443 | }; | |
444 | ||
445 | static const struct gdb_xml_attribute vector_attributes[] = { | |
446 | { "id", GDB_XML_AF_NONE, NULL, NULL }, | |
447 | { "type", GDB_XML_AF_NONE, NULL, NULL }, | |
448 | { "count", GDB_XML_AF_NONE, gdb_xml_parse_attr_ulongest, NULL }, | |
449 | { NULL, GDB_XML_AF_NONE, NULL, NULL } | |
450 | }; | |
451 | ||
452 | static const struct gdb_xml_attribute feature_attributes[] = { | |
453 | { "name", GDB_XML_AF_NONE, NULL, NULL }, | |
454 | { NULL, GDB_XML_AF_NONE, NULL, NULL } | |
455 | }; | |
456 | ||
457 | static const struct gdb_xml_element feature_children[] = { | |
458 | { "reg", reg_attributes, NULL, | |
459 | GDB_XML_EF_OPTIONAL | GDB_XML_EF_REPEATABLE, | |
460 | tdesc_start_reg, NULL }, | |
f5dff777 DJ |
461 | { "struct", struct_union_attributes, struct_union_children, |
462 | GDB_XML_EF_OPTIONAL | GDB_XML_EF_REPEATABLE, | |
463 | tdesc_start_struct, NULL }, | |
464 | { "union", struct_union_attributes, struct_union_children, | |
123dc839 | 465 | GDB_XML_EF_OPTIONAL | GDB_XML_EF_REPEATABLE, |
ad068eab | 466 | tdesc_start_union, NULL }, |
f5dff777 DJ |
467 | { "flags", flags_attributes, struct_union_children, |
468 | GDB_XML_EF_OPTIONAL | GDB_XML_EF_REPEATABLE, | |
469 | tdesc_start_flags, NULL }, | |
123dc839 DJ |
470 | { "vector", vector_attributes, NULL, |
471 | GDB_XML_EF_OPTIONAL | GDB_XML_EF_REPEATABLE, | |
472 | tdesc_start_vector, NULL }, | |
473 | { NULL, NULL, NULL, GDB_XML_EF_NONE, NULL, NULL } | |
474 | }; | |
475 | ||
1780a0ed DJ |
476 | static const struct gdb_xml_attribute target_attributes[] = { |
477 | { "version", GDB_XML_AF_NONE, NULL, NULL }, | |
478 | { NULL, GDB_XML_AF_NONE, NULL, NULL } | |
479 | }; | |
480 | ||
123dc839 | 481 | static const struct gdb_xml_element target_children[] = { |
23181151 DJ |
482 | { "architecture", NULL, NULL, GDB_XML_EF_OPTIONAL, |
483 | NULL, tdesc_end_arch }, | |
08d16641 PA |
484 | { "osabi", NULL, NULL, GDB_XML_EF_OPTIONAL, |
485 | NULL, tdesc_end_osabi }, | |
e35359c5 UW |
486 | { "compatible", NULL, NULL, GDB_XML_EF_OPTIONAL | GDB_XML_EF_REPEATABLE, |
487 | NULL, tdesc_end_compatible }, | |
123dc839 DJ |
488 | { "feature", feature_attributes, feature_children, |
489 | GDB_XML_EF_OPTIONAL | GDB_XML_EF_REPEATABLE, | |
490 | tdesc_start_feature, NULL }, | |
23181151 DJ |
491 | { NULL, NULL, NULL, GDB_XML_EF_NONE, NULL, NULL } |
492 | }; | |
493 | ||
123dc839 | 494 | static const struct gdb_xml_element tdesc_elements[] = { |
1780a0ed DJ |
495 | { "target", target_attributes, target_children, GDB_XML_EF_NONE, |
496 | tdesc_start_target, NULL }, | |
23181151 DJ |
497 | { NULL, NULL, NULL, GDB_XML_EF_NONE, NULL, NULL } |
498 | }; | |
499 | ||
500 | /* Parse DOCUMENT into a target description and return it. */ | |
501 | ||
502 | static struct target_desc * | |
108546a0 DJ |
503 | tdesc_parse_xml (const char *document, xml_fetch_another fetcher, |
504 | void *fetcher_baton) | |
23181151 DJ |
505 | { |
506 | struct cleanup *back_to, *result_cleanup; | |
23181151 | 507 | struct tdesc_parsing_data data; |
fc6e0168 | 508 | struct tdesc_xml_cache *cache; |
108546a0 | 509 | char *expanded_text; |
fc6e0168 | 510 | int ix; |
23181151 | 511 | |
108546a0 DJ |
512 | /* Expand all XInclude directives. */ |
513 | expanded_text = xml_process_xincludes (_("target description"), | |
514 | document, fetcher, fetcher_baton, 0); | |
515 | if (expanded_text == NULL) | |
516 | { | |
517 | warning (_("Could not load XML target description; ignoring")); | |
518 | return NULL; | |
519 | } | |
23181151 | 520 | |
fc6e0168 DJ |
521 | /* Check for an exact match in the list of descriptions we have |
522 | previously parsed. strcmp is a slightly inefficient way to | |
523 | do this; an SHA-1 checksum would work as well. */ | |
524 | for (ix = 0; VEC_iterate (tdesc_xml_cache_s, xml_cache, ix, cache); ix++) | |
525 | if (strcmp (cache->xml_document, expanded_text) == 0) | |
526 | { | |
527 | xfree (expanded_text); | |
528 | return cache->tdesc; | |
529 | } | |
530 | ||
531 | back_to = make_cleanup (null_cleanup, NULL); | |
23181151 | 532 | |
108546a0 | 533 | memset (&data, 0, sizeof (struct tdesc_parsing_data)); |
23181151 DJ |
534 | data.tdesc = allocate_target_description (); |
535 | result_cleanup = make_cleanup_free_target_description (data.tdesc); | |
fc6e0168 | 536 | make_cleanup (xfree, expanded_text); |
23181151 | 537 | |
efc0eabd PA |
538 | if (gdb_xml_parse_quick (_("target description"), "gdb-target.dtd", |
539 | tdesc_elements, expanded_text, &data) == 0) | |
23181151 DJ |
540 | { |
541 | /* Parsed successfully. */ | |
fc6e0168 DJ |
542 | struct tdesc_xml_cache new_cache; |
543 | ||
544 | new_cache.xml_document = expanded_text; | |
545 | new_cache.tdesc = data.tdesc; | |
546 | VEC_safe_push (tdesc_xml_cache_s, xml_cache, &new_cache); | |
23181151 DJ |
547 | discard_cleanups (result_cleanup); |
548 | do_cleanups (back_to); | |
549 | return data.tdesc; | |
550 | } | |
551 | else | |
552 | { | |
553 | warning (_("Could not load XML target description; ignoring")); | |
554 | do_cleanups (back_to); | |
555 | return NULL; | |
556 | } | |
557 | } | |
23181151 DJ |
558 | #endif /* HAVE_LIBEXPAT */ |
559 | \f | |
560 | ||
23181151 DJ |
561 | /* Read an XML target description from FILENAME. Parse it, and return |
562 | the parsed description. */ | |
563 | ||
564 | const struct target_desc * | |
565 | file_read_description_xml (const char *filename) | |
566 | { | |
567 | struct target_desc *tdesc; | |
568 | char *tdesc_str; | |
569 | struct cleanup *back_to; | |
108546a0 | 570 | char *dirname; |
23181151 | 571 | |
a96d9b2e | 572 | tdesc_str = xml_fetch_content_from_file (filename, NULL); |
23181151 | 573 | if (tdesc_str == NULL) |
108546a0 DJ |
574 | { |
575 | warning (_("Could not open \"%s\""), filename); | |
576 | return NULL; | |
577 | } | |
23181151 DJ |
578 | |
579 | back_to = make_cleanup (xfree, tdesc_str); | |
108546a0 | 580 | |
e1024ff1 DJ |
581 | dirname = ldirname (filename); |
582 | if (dirname != NULL) | |
583 | make_cleanup (xfree, dirname); | |
108546a0 | 584 | |
a96d9b2e | 585 | tdesc = tdesc_parse_xml (tdesc_str, xml_fetch_content_from_file, dirname); |
23181151 DJ |
586 | do_cleanups (back_to); |
587 | ||
588 | return tdesc; | |
589 | } | |
590 | ||
108546a0 DJ |
591 | /* Read a string representation of available features from the target, |
592 | using TARGET_OBJECT_AVAILABLE_FEATURES. The returned string is | |
593 | malloc allocated and NUL-terminated. NAME should be a non-NULL | |
594 | string identifying the XML document we want; the top level document | |
595 | is "target.xml". Other calls may be performed for the DTD or | |
596 | for <xi:include>. */ | |
597 | ||
598 | static char * | |
599 | fetch_available_features_from_target (const char *name, void *baton_) | |
600 | { | |
19ba03f4 | 601 | struct target_ops *ops = (struct target_ops *) baton_; |
108546a0 DJ |
602 | |
603 | /* Read this object as a string. This ensures that a NUL | |
604 | terminator is added. */ | |
605 | return target_read_stralloc (ops, | |
606 | TARGET_OBJECT_AVAILABLE_FEATURES, | |
607 | name); | |
608 | } | |
609 | \f | |
610 | ||
23181151 DJ |
611 | /* Read an XML target description using OPS. Parse it, and return the |
612 | parsed description. */ | |
613 | ||
614 | const struct target_desc * | |
615 | target_read_description_xml (struct target_ops *ops) | |
616 | { | |
617 | struct target_desc *tdesc; | |
618 | char *tdesc_str; | |
619 | struct cleanup *back_to; | |
620 | ||
108546a0 | 621 | tdesc_str = fetch_available_features_from_target ("target.xml", ops); |
23181151 DJ |
622 | if (tdesc_str == NULL) |
623 | return NULL; | |
624 | ||
625 | back_to = make_cleanup (xfree, tdesc_str); | |
108546a0 DJ |
626 | tdesc = tdesc_parse_xml (tdesc_str, |
627 | fetch_available_features_from_target, | |
628 | ops); | |
23181151 DJ |
629 | do_cleanups (back_to); |
630 | ||
631 | return tdesc; | |
632 | } | |
18d3cec5 MK |
633 | |
634 | /* Fetches an XML target description using OPS, processing | |
635 | includes, but not parsing it. Used to dump whole tdesc | |
636 | as a single XML file. */ | |
637 | ||
638 | char * | |
639 | target_fetch_description_xml (struct target_ops *ops) | |
640 | { | |
641 | struct target_desc *tdesc; | |
642 | char *tdesc_str; | |
643 | char *expanded_text; | |
644 | struct cleanup *back_to; | |
645 | ||
646 | tdesc_str = fetch_available_features_from_target ("target.xml", ops); | |
647 | if (tdesc_str == NULL) | |
648 | return NULL; | |
649 | ||
650 | back_to = make_cleanup (xfree, tdesc_str); | |
651 | expanded_text = xml_process_xincludes (_("target description"), | |
652 | tdesc_str, | |
653 | fetch_available_features_from_target, ops, 0); | |
654 | do_cleanups (back_to); | |
655 | if (expanded_text == NULL) | |
656 | { | |
657 | warning (_("Could not load XML target description; ignoring")); | |
658 | return NULL; | |
659 | } | |
660 | ||
661 | return expanded_text; | |
662 | } |