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. */
27 #include "target-descriptions.h"
28 #include "xml-support.h"
29 #include "xml-tdesc.h"
31 #include "filenames.h"
33 #include "gdb_assert.h"
35 #if !defined(HAVE_LIBEXPAT)
37 /* Parse DOCUMENT into a target description. Or don't, since we don't have
40 static struct target_desc
*
41 tdesc_parse_xml (const char *document
, xml_fetch_another fetcher
,
44 static int have_warned
;
49 warning (_("Can not parse XML target description; XML support was "
50 "disabled at compile time"));
56 #else /* HAVE_LIBEXPAT */
58 /* Callback data for target description parsing. */
60 struct tdesc_parsing_data
62 /* The target description we are building. */
63 struct target_desc
*tdesc
;
66 /* Handle the end of an <architecture> element and its value. */
69 tdesc_end_arch (struct gdb_xml_parser
*parser
,
70 const struct gdb_xml_element
*element
,
71 void *user_data
, const char *body_text
)
73 struct tdesc_parsing_data
*data
= user_data
;
74 const struct bfd_arch_info
*arch
;
76 arch
= bfd_scan_arch (body_text
);
78 gdb_xml_error (parser
, _("Target description specified unknown "
79 "architecture \"%s\""), body_text
);
80 set_tdesc_architecture (data
->tdesc
, arch
);
83 /* The elements and attributes of an XML target description. */
85 const struct gdb_xml_element target_children
[] = {
86 { "architecture", NULL
, NULL
, GDB_XML_EF_OPTIONAL
,
87 NULL
, tdesc_end_arch
},
88 { NULL
, NULL
, NULL
, GDB_XML_EF_NONE
, NULL
, NULL
}
91 const struct gdb_xml_element tdesc_elements
[] = {
92 { "target", NULL
, target_children
, GDB_XML_EF_NONE
,
94 { NULL
, NULL
, NULL
, GDB_XML_EF_NONE
, NULL
, NULL
}
97 /* Parse DOCUMENT into a target description and return it. */
99 static struct target_desc
*
100 tdesc_parse_xml (const char *document
, xml_fetch_another fetcher
,
103 struct cleanup
*back_to
, *result_cleanup
;
104 struct gdb_xml_parser
*parser
;
105 struct tdesc_parsing_data data
;
108 /* Expand all XInclude directives. */
109 expanded_text
= xml_process_xincludes (_("target description"),
110 document
, fetcher
, fetcher_baton
, 0);
111 if (expanded_text
== NULL
)
113 warning (_("Could not load XML target description; ignoring"));
116 back_to
= make_cleanup (xfree
, expanded_text
);
118 parser
= gdb_xml_create_parser_and_cleanup (_("target description"),
119 tdesc_elements
, &data
);
120 gdb_xml_use_dtd (parser
, "gdb-target.dtd");
122 memset (&data
, 0, sizeof (struct tdesc_parsing_data
));
123 data
.tdesc
= allocate_target_description ();
124 result_cleanup
= make_cleanup_free_target_description (data
.tdesc
);
126 if (gdb_xml_parse (parser
, expanded_text
) == 0)
128 /* Parsed successfully. */
129 discard_cleanups (result_cleanup
);
130 do_cleanups (back_to
);
135 warning (_("Could not load XML target description; ignoring"));
136 do_cleanups (back_to
);
140 #endif /* HAVE_LIBEXPAT */
146 do_cleanup_fclose (void *file
)
151 /* Open FILENAME, read all its text into memory, close it, and return
152 the text. If something goes wrong, return NULL and warn. */
155 fetch_xml_from_file (const char *filename
, void *baton
)
157 const char *dirname
= baton
;
159 struct cleanup
*back_to
;
163 if (dirname
&& *dirname
)
165 char *fullname
= concat (dirname
, "/", filename
, NULL
);
166 if (fullname
== NULL
)
168 file
= fopen (fullname
, FOPEN_RT
);
172 file
= fopen (filename
, FOPEN_RT
);
177 back_to
= make_cleanup (do_cleanup_fclose
, file
);
179 /* Read in the whole file, one chunk at a time. */
182 text
= xmalloc (len
);
183 make_cleanup (free_current_contents
, &text
);
188 /* Continue reading where the last read left off. Leave at least
189 one byte so that we can NUL-terminate the result. */
190 bytes_read
= fread (text
+ offset
, 1, len
- offset
- 1, file
);
193 warning (_("Read error from \"%s\""), filename
);
194 do_cleanups (back_to
);
198 offset
+= bytes_read
;
204 text
= xrealloc (text
, len
);
208 discard_cleanups (back_to
);
214 /* Read an XML target description from FILENAME. Parse it, and return
215 the parsed description. */
217 const struct target_desc
*
218 file_read_description_xml (const char *filename
)
220 struct target_desc
*tdesc
;
222 struct cleanup
*back_to
;
226 tdesc_str
= fetch_xml_from_file (filename
, NULL
);
227 if (tdesc_str
== NULL
)
229 warning (_("Could not open \"%s\""), filename
);
233 back_to
= make_cleanup (xfree
, tdesc_str
);
235 /* Simple, portable version of dirname that does not modify its
237 base
= lbasename (filename
);
238 while (base
> filename
&& IS_DIR_SEPARATOR (base
[-1]))
242 dirname
= xmalloc (base
- filename
+ 2);
243 memcpy (dirname
, filename
, base
- filename
);
245 /* On DOS based file systems, convert "d:foo" to "d:.", so that
246 we create "d:./bar" later instead of the (different)
248 if (base
- filename
== 2 && IS_ABSOLUTE_PATH (base
)
249 && !IS_DIR_SEPARATOR (filename
[0]))
250 dirname
[base
++ - filename
] = '.';
252 dirname
[base
- filename
] = '\0';
253 make_cleanup (xfree
, dirname
);
258 tdesc
= tdesc_parse_xml (tdesc_str
, fetch_xml_from_file
, dirname
);
259 do_cleanups (back_to
);
264 /* Read a string representation of available features from the target,
265 using TARGET_OBJECT_AVAILABLE_FEATURES. The returned string is
266 malloc allocated and NUL-terminated. NAME should be a non-NULL
267 string identifying the XML document we want; the top level document
268 is "target.xml". Other calls may be performed for the DTD or
272 fetch_available_features_from_target (const char *name
, void *baton_
)
274 struct target_ops
*ops
= baton_
;
276 /* Read this object as a string. This ensures that a NUL
277 terminator is added. */
278 return target_read_stralloc (ops
,
279 TARGET_OBJECT_AVAILABLE_FEATURES
,
284 /* Read an XML target description using OPS. Parse it, and return the
285 parsed description. */
287 const struct target_desc
*
288 target_read_description_xml (struct target_ops
*ops
)
290 struct target_desc
*tdesc
;
292 struct cleanup
*back_to
;
294 tdesc_str
= fetch_available_features_from_target ("target.xml", ops
);
295 if (tdesc_str
== NULL
)
298 back_to
= make_cleanup (xfree
, tdesc_str
);
299 tdesc
= tdesc_parse_xml (tdesc_str
,
300 fetch_available_features_from_target
,
302 do_cleanups (back_to
);