1 /* Functions that provide the mechanism to parse a syscall XML file
4 Copyright (C) 2009, 2010 Free Software Foundation, Inc.
6 This file is part of GDB.
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>. */
23 #include "xml-support.h"
24 #include "xml-syscall.h"
26 /* For the struct syscall definition. */
29 #include "filenames.h"
31 #include "gdb_assert.h"
35 /* Dummy functions to indicate that there's no support for fetching
36 syscalls information. */
39 syscall_warn_user (void)
41 static int have_warned = 0;
45 warning (_("Can not parse XML syscalls information; XML support was "
46 "disabled at compile time."));
51 set_xml_syscall_file_name (const char *name)
57 get_syscall_by_number (int syscall_number,
61 s->number = syscall_number;
66 get_syscall_by_name (const char *syscall_name,
70 s->number = UNKNOWN_SYSCALL;
71 s->name = syscall_name;
75 get_syscall_names (void)
81 #else /* ! HAVE_LIBEXPAT */
83 /* Variable that will hold the last known data-directory. This is useful to
84 know whether we should re-read the XML info for the target. */
85 static char *my_gdb_datadir = NULL;
87 /* Structure which describes a syscall. */
88 typedef struct syscall_desc
90 /* The syscall number. */
94 /* The syscall name. */
98 DEF_VEC_P(syscall_desc_p);
100 /* Structure that represents syscalls information. */
105 VEC(syscall_desc_p) *syscalls;
108 /* Callback data for syscall information parsing. */
109 struct syscall_parsing_data
111 /* The syscalls_info we are building. */
113 struct syscalls_info *sysinfo;
116 /* Structure used to store information about the available syscalls in
118 static const struct syscalls_info *sysinfo = NULL;
120 /* A flag to tell if we already initialized the structure above. */
121 static int have_initialized_sysinfo = 0;
123 /* The filename of the syscall's XML. */
124 static const char *xml_syscall_file = NULL;
126 static struct syscalls_info *
127 allocate_syscalls_info (void)
129 return XZALLOC (struct syscalls_info);
133 sysinfo_free_syscalls_desc (struct syscall_desc *sd)
139 free_syscalls_info (void *arg)
141 struct syscalls_info *sysinfo = arg;
142 struct syscall_desc *sysdesc;
146 VEC_iterate (syscall_desc_p, sysinfo->syscalls, i, sysdesc);
148 sysinfo_free_syscalls_desc (sysdesc);
149 VEC_free (syscall_desc_p, sysinfo->syscalls);
155 make_cleanup_free_syscalls_info (struct syscalls_info *sysinfo)
157 return make_cleanup (free_syscalls_info, sysinfo);
161 syscall_create_syscall_desc (struct syscalls_info *sysinfo,
162 const char *name, int number)
164 struct syscall_desc *sysdesc = XZALLOC (struct syscall_desc);
166 sysdesc->name = xstrdup (name);
167 sysdesc->number = number;
169 VEC_safe_push (syscall_desc_p, sysinfo->syscalls, sysdesc);
172 /* Handle the start of a <syscall> element. */
174 syscall_start_syscall (struct gdb_xml_parser *parser,
175 const struct gdb_xml_element *element,
176 void *user_data, VEC(gdb_xml_value_s) *attributes)
178 struct syscall_parsing_data *data = user_data;
179 struct gdb_xml_value *attrs = VEC_address (gdb_xml_value_s, attributes);
185 len = VEC_length (gdb_xml_value_s, attributes);
187 for (i = 0; i < len; i++)
189 if (strcmp (attrs[i].name, "name") == 0)
190 name = attrs[i].value;
191 else if (strcmp (attrs[i].name, "number") == 0)
192 number = * (ULONGEST *) attrs[i].value;
194 internal_error (__FILE__, __LINE__,
195 _("Unknown attribute name '%s'."), attrs[i].name);
198 syscall_create_syscall_desc (data->sysinfo, name, number);
202 /* The elements and attributes of an XML syscall document. */
203 static const struct gdb_xml_attribute syscall_attr[] = {
204 { "number", GDB_XML_AF_NONE, gdb_xml_parse_attr_ulongest, NULL },
205 { "name", GDB_XML_AF_NONE, NULL, NULL },
206 { NULL, GDB_XML_AF_NONE, NULL, NULL }
209 static const struct gdb_xml_element syscalls_info_children[] = {
210 { "syscall", syscall_attr, NULL,
211 GDB_XML_EF_OPTIONAL | GDB_XML_EF_REPEATABLE,
212 syscall_start_syscall, NULL },
213 { NULL, NULL, NULL, GDB_XML_EF_NONE, NULL, NULL }
216 static const struct gdb_xml_element syselements[] = {
217 { "syscalls_info", NULL, syscalls_info_children,
218 GDB_XML_EF_NONE, NULL, NULL },
219 { NULL, NULL, NULL, GDB_XML_EF_NONE, NULL, NULL }
222 static struct syscalls_info *
223 syscall_parse_xml (const char *document, xml_fetch_another fetcher,
226 struct cleanup *result_cleanup;
227 struct gdb_xml_parser *parser;
228 struct syscall_parsing_data data;
230 parser = gdb_xml_create_parser_and_cleanup (_("syscalls info"),
233 memset (&data, 0, sizeof (struct syscall_parsing_data));
234 data.sysinfo = allocate_syscalls_info ();
235 result_cleanup = make_cleanup_free_syscalls_info (data.sysinfo);
237 if (gdb_xml_parse (parser, document) == 0)
239 /* Parsed successfully. */
240 discard_cleanups (result_cleanup);
245 warning (_("Could not load XML syscalls info; ignoring"));
246 do_cleanups (result_cleanup);
251 /* Function responsible for initializing the information
252 about the syscalls. It reads the XML file and fills the
253 struct syscalls_info with the values.
255 Returns the struct syscalls_info if the file is valid, NULL otherwise. */
256 static const struct syscalls_info *
257 xml_init_syscalls_info (const char *filename)
261 struct syscalls_info *sysinfo;
262 struct cleanup *back_to;
264 full_file = xml_fetch_content_from_file (filename, gdb_datadir);
265 if (full_file == NULL)
268 back_to = make_cleanup (xfree, full_file);
270 dirname = ldirname (filename);
272 make_cleanup (xfree, dirname);
274 sysinfo = syscall_parse_xml (full_file, xml_fetch_content_from_file, dirname);
275 do_cleanups (back_to);
280 /* Initializes the syscalls_info structure according to the
285 /* Should we re-read the XML info for this target? */
286 if (my_gdb_datadir && strcmp (my_gdb_datadir, gdb_datadir) != 0)
288 /* The data-directory changed from the last time we used it.
289 It means that we have to re-read the XML info. */
290 have_initialized_sysinfo = 0;
291 xfree (my_gdb_datadir);
292 my_gdb_datadir = NULL;
294 free_syscalls_info ((void *) sysinfo);
297 /* Did we already try to initialize the structure? */
298 if (have_initialized_sysinfo)
301 sysinfo = xml_init_syscalls_info (xml_syscall_file);
303 have_initialized_sysinfo = 1;
307 if (xml_syscall_file)
309 Could not load the syscall XML file `%s/%s'."),
310 gdb_datadir, xml_syscall_file);
313 There is no XML file to open."));
316 GDB will not be able to display syscall names nor to verify if\n\
317 any provided syscall numbers are valid."));
320 /* Saving the data-directory used to read this XML info. */
321 my_gdb_datadir = xstrdup (gdb_datadir);
325 xml_get_syscall_number (const struct syscalls_info *sysinfo,
326 const char *syscall_name)
328 struct syscall_desc *sysdesc;
332 || syscall_name == NULL)
333 return UNKNOWN_SYSCALL;
336 VEC_iterate(syscall_desc_p, sysinfo->syscalls, i, sysdesc);
338 if (strcmp (sysdesc->name, syscall_name) == 0)
339 return sysdesc->number;
341 return UNKNOWN_SYSCALL;
345 xml_get_syscall_name (const struct syscalls_info *sysinfo,
348 struct syscall_desc *sysdesc;
352 || syscall_number < 0)
356 VEC_iterate(syscall_desc_p, sysinfo->syscalls, i, sysdesc);
358 if (sysdesc->number == syscall_number)
359 return sysdesc->name;
365 xml_list_of_syscalls (const struct syscalls_info *sysinfo)
367 struct syscall_desc *sysdesc;
368 const char **names = NULL;
375 nsyscalls = VEC_length (syscall_desc_p, sysinfo->syscalls);
376 names = xmalloc ((nsyscalls + 1) * sizeof (char *));
379 VEC_iterate (syscall_desc_p, sysinfo->syscalls, i, sysdesc);
381 names[i] = sysdesc->name;
389 set_xml_syscall_file_name (const char *name)
391 xml_syscall_file = name;
395 get_syscall_by_number (int syscall_number,
400 s->number = syscall_number;
401 s->name = xml_get_syscall_name (sysinfo, syscall_number);
405 get_syscall_by_name (const char *syscall_name,
410 s->number = xml_get_syscall_number (sysinfo, syscall_name);
411 s->name = syscall_name;
415 get_syscall_names (void)
419 return xml_list_of_syscalls (sysinfo);
422 #endif /* ! HAVE_LIBEXPAT */