1 /* Functions that provide the mechanism to parse a syscall XML file
4 Copyright (C) 2009 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 /* Structure which describes a syscall. */
84 typedef struct syscall_desc
86 /* The syscall number. */
90 /* The syscall name. */
94 DEF_VEC_P(syscall_desc_p);
96 /* Structure that represents syscalls information. */
101 VEC(syscall_desc_p) *syscalls;
104 /* Callback data for syscall information parsing. */
105 struct syscall_parsing_data
107 /* The syscalls_info we are building. */
109 struct syscalls_info *sysinfo;
112 /* Structure used to store information about the available syscalls in
114 static const struct syscalls_info *sysinfo = NULL;
116 /* A flag to tell if we already initialized the structure above. */
117 static int have_initialized_sysinfo = 0;
119 /* The filename of the syscall's XML. */
120 static const char *xml_syscall_file = NULL;
122 static struct syscalls_info *
123 allocate_syscalls_info (void)
125 return XZALLOC (struct syscalls_info);
129 sysinfo_free_syscalls_desc (struct syscall_desc *sd)
135 free_syscalls_info (void *arg)
137 struct syscalls_info *sysinfo = arg;
138 struct syscall_desc *sysdesc;
142 VEC_iterate (syscall_desc_p, sysinfo->syscalls, i, sysdesc);
144 sysinfo_free_syscalls_desc (sysdesc);
145 VEC_free (syscall_desc_p, sysinfo->syscalls);
151 make_cleanup_free_syscalls_info (struct syscalls_info *sysinfo)
153 return make_cleanup (free_syscalls_info, sysinfo);
157 syscall_create_syscall_desc (struct syscalls_info *sysinfo,
158 const char *name, int number)
160 struct syscall_desc *sysdesc = XZALLOC (struct syscall_desc);
162 sysdesc->name = xstrdup (name);
163 sysdesc->number = number;
165 VEC_safe_push (syscall_desc_p, sysinfo->syscalls, sysdesc);
168 /* Handle the start of a <syscalls_info> element. */
170 syscall_start_syscalls_info (struct gdb_xml_parser *parser,
171 const struct gdb_xml_element *element,
173 VEC(gdb_xml_value_s) *attributes)
175 struct syscall_parsing_data *data = user_data;
176 struct syscalls_info *sysinfo = data->sysinfo;
179 /* Handle the start of a <syscall> element. */
181 syscall_start_syscall (struct gdb_xml_parser *parser,
182 const struct gdb_xml_element *element,
183 void *user_data, VEC(gdb_xml_value_s) *attributes)
185 struct syscall_parsing_data *data = user_data;
186 struct gdb_xml_value *attrs = VEC_address (gdb_xml_value_s, attributes);
192 len = VEC_length (gdb_xml_value_s, attributes);
194 for (i = 0; i < len; i++)
196 if (strcmp (attrs[i].name, "name") == 0)
197 name = attrs[i].value;
198 else if (strcmp (attrs[i].name, "number") == 0)
199 number = * (ULONGEST *) attrs[i].value;
201 internal_error (__FILE__, __LINE__,
202 _("Unknown attribute name '%s'."), attrs[i].name);
205 syscall_create_syscall_desc (data->sysinfo, name, number);
209 /* The elements and attributes of an XML syscall document. */
210 static const struct gdb_xml_attribute syscall_attr[] = {
211 { "number", GDB_XML_AF_NONE, gdb_xml_parse_attr_ulongest, NULL },
212 { "name", GDB_XML_AF_NONE, NULL, NULL },
213 { NULL, GDB_XML_AF_NONE, NULL, NULL }
216 static const struct gdb_xml_element syscalls_info_children[] = {
217 { "syscall", syscall_attr, NULL,
218 GDB_XML_EF_OPTIONAL | GDB_XML_EF_REPEATABLE,
219 syscall_start_syscall, NULL },
220 { NULL, NULL, NULL, GDB_XML_EF_NONE, NULL, NULL }
223 static const struct gdb_xml_element syselements[] = {
224 { "syscalls_info", NULL, syscalls_info_children,
225 GDB_XML_EF_NONE, syscall_start_syscalls_info, NULL },
226 { NULL, NULL, NULL, GDB_XML_EF_NONE, NULL, NULL }
229 static struct syscalls_info *
230 syscall_parse_xml (const char *document, xml_fetch_another fetcher,
233 struct cleanup *result_cleanup;
234 struct gdb_xml_parser *parser;
235 struct syscall_parsing_data data;
239 parser = gdb_xml_create_parser_and_cleanup (_("syscalls info"),
242 memset (&data, 0, sizeof (struct syscall_parsing_data));
243 data.sysinfo = allocate_syscalls_info ();
244 result_cleanup = make_cleanup_free_syscalls_info (data.sysinfo);
246 if (gdb_xml_parse (parser, document) == 0)
248 /* Parsed successfully. */
249 discard_cleanups (result_cleanup);
254 warning (_("Could not load XML syscalls info; ignoring"));
255 do_cleanups (result_cleanup);
260 /* Function responsible for initializing the information
261 about the syscalls. It reads the XML file and fills the
262 struct syscalls_info with the values.
264 Returns the struct syscalls_info if the file is valid, NULL otherwise. */
265 static const struct syscalls_info *
266 xml_init_syscalls_info (const char *filename)
270 struct syscalls_info *sysinfo;
271 struct cleanup *back_to;
273 full_file = xml_fetch_content_from_file (filename, gdb_datadir);
274 if (full_file == NULL)
277 back_to = make_cleanup (xfree, full_file);
279 dirname = ldirname (filename);
281 make_cleanup (xfree, dirname);
283 sysinfo = syscall_parse_xml (full_file, xml_fetch_content_from_file, dirname);
284 do_cleanups (back_to);
289 /* Initializes the syscalls_info structure according to the
294 /* Did we already try to initialize the structure? */
295 if (have_initialized_sysinfo)
298 sysinfo = xml_init_syscalls_info (xml_syscall_file);
300 have_initialized_sysinfo = 1;
304 if (xml_syscall_file)
306 Could not load the syscall XML file `%s'."), xml_syscall_file);
309 There is no XML file to open."));
312 GDB will not be able to display syscall names nor to verify if\n\
313 any provided syscall numbers are valid."));
318 xml_get_syscall_number (const struct syscalls_info *sysinfo,
319 const char *syscall_name)
321 struct syscall_desc *sysdesc;
325 || syscall_name == NULL)
326 return UNKNOWN_SYSCALL;
329 VEC_iterate(syscall_desc_p, sysinfo->syscalls, i, sysdesc);
331 if (strcmp (sysdesc->name, syscall_name) == 0)
332 return sysdesc->number;
334 return UNKNOWN_SYSCALL;
338 xml_get_syscall_name (const struct syscalls_info *sysinfo,
341 struct syscall_desc *sysdesc;
345 || syscall_number < 0)
349 VEC_iterate(syscall_desc_p, sysinfo->syscalls, i, sysdesc);
351 if (sysdesc->number == syscall_number)
352 return sysdesc->name;
358 xml_number_of_syscalls (const struct syscalls_info *sysinfo)
360 return (sysinfo == NULL ? 0 : VEC_length (syscall_desc_p,
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 */