1 /* Functions that provide the mechanism to parse a syscall XML file
4 Copyright (C) 2009-2014 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"
33 /* Dummy functions to indicate that there's no support for fetching
34 syscalls information. */
37 syscall_warn_user (void)
39 static int have_warned = 0;
43 warning (_("Can not parse XML syscalls information; XML support was "
44 "disabled at compile time."));
49 set_xml_syscall_file_name (const char *name)
55 get_syscall_by_number (int syscall_number,
59 s->number = syscall_number;
64 get_syscall_by_name (const char *syscall_name,
68 s->number = UNKNOWN_SYSCALL;
69 s->name = syscall_name;
73 get_syscall_names (void)
79 #else /* ! HAVE_LIBEXPAT */
81 /* Variable that will hold the last known data-directory. This is useful to
82 know whether we should re-read the XML info for the target. */
83 static char *my_gdb_datadir = NULL;
85 /* Structure which describes a syscall. */
86 typedef struct syscall_desc
88 /* The syscall number. */
92 /* The syscall name. */
96 DEF_VEC_P(syscall_desc_p);
98 /* Structure that represents syscalls information. */
103 VEC(syscall_desc_p) *syscalls;
106 /* Callback data for syscall information parsing. */
107 struct syscall_parsing_data
109 /* The syscalls_info we are building. */
111 struct syscalls_info *sysinfo;
114 /* Structure used to store information about the available syscalls in
116 static const struct syscalls_info *sysinfo = NULL;
118 /* A flag to tell if we already initialized the structure above. */
119 static int have_initialized_sysinfo = 0;
121 /* The filename of the syscall's XML. */
122 static const char *xml_syscall_file = NULL;
124 static struct syscalls_info *
125 allocate_syscalls_info (void)
127 return XCNEW (struct syscalls_info);
131 sysinfo_free_syscalls_desc (struct syscall_desc *sd)
137 free_syscalls_info (void *arg)
139 struct syscalls_info *sysinfo = arg;
140 struct syscall_desc *sysdesc;
144 VEC_iterate (syscall_desc_p, sysinfo->syscalls, i, sysdesc);
146 sysinfo_free_syscalls_desc (sysdesc);
147 VEC_free (syscall_desc_p, sysinfo->syscalls);
152 static struct cleanup *
153 make_cleanup_free_syscalls_info (struct syscalls_info *sysinfo)
155 return make_cleanup (free_syscalls_info, sysinfo);
159 syscall_create_syscall_desc (struct syscalls_info *sysinfo,
160 const char *name, int number)
162 struct syscall_desc *sysdesc = XCNEW (struct syscall_desc);
164 sysdesc->name = xstrdup (name);
165 sysdesc->number = number;
167 VEC_safe_push (syscall_desc_p, sysinfo->syscalls, sysdesc);
170 /* Handle the start of a <syscall> element. */
172 syscall_start_syscall (struct gdb_xml_parser *parser,
173 const struct gdb_xml_element *element,
174 void *user_data, VEC(gdb_xml_value_s) *attributes)
176 struct syscall_parsing_data *data = user_data;
177 struct gdb_xml_value *attrs = VEC_address (gdb_xml_value_s, attributes);
183 len = VEC_length (gdb_xml_value_s, attributes);
185 for (i = 0; i < len; i++)
187 if (strcmp (attrs[i].name, "name") == 0)
188 name = attrs[i].value;
189 else if (strcmp (attrs[i].name, "number") == 0)
190 number = * (ULONGEST *) attrs[i].value;
192 internal_error (__FILE__, __LINE__,
193 _("Unknown attribute name '%s'."), attrs[i].name);
197 syscall_create_syscall_desc (data->sysinfo, name, number);
201 /* The elements and attributes of an XML syscall document. */
202 static const struct gdb_xml_attribute syscall_attr[] = {
203 { "number", GDB_XML_AF_NONE, gdb_xml_parse_attr_ulongest, NULL },
204 { "name", GDB_XML_AF_NONE, NULL, NULL },
205 { NULL, GDB_XML_AF_NONE, NULL, NULL }
208 static const struct gdb_xml_element syscalls_info_children[] = {
209 { "syscall", syscall_attr, NULL,
210 GDB_XML_EF_OPTIONAL | GDB_XML_EF_REPEATABLE,
211 syscall_start_syscall, NULL },
212 { NULL, NULL, NULL, GDB_XML_EF_NONE, NULL, NULL }
215 static const struct gdb_xml_element syselements[] = {
216 { "syscalls_info", NULL, syscalls_info_children,
217 GDB_XML_EF_NONE, NULL, NULL },
218 { NULL, NULL, NULL, GDB_XML_EF_NONE, NULL, NULL }
221 static struct syscalls_info *
222 syscall_parse_xml (const char *document, xml_fetch_another fetcher,
225 struct cleanup *result_cleanup;
226 struct syscall_parsing_data data;
228 data.sysinfo = allocate_syscalls_info ();
229 result_cleanup = make_cleanup_free_syscalls_info (data.sysinfo);
231 if (gdb_xml_parse_quick (_("syscalls info"), NULL,
232 syselements, document, &data) == 0)
234 /* Parsed successfully. */
235 discard_cleanups (result_cleanup);
240 warning (_("Could not load XML syscalls info; ignoring"));
241 do_cleanups (result_cleanup);
246 /* Function responsible for initializing the information
247 about the syscalls. It reads the XML file and fills the
248 struct syscalls_info with the values.
250 Returns the struct syscalls_info if the file is valid, NULL otherwise. */
251 static const struct syscalls_info *
252 xml_init_syscalls_info (const char *filename)
256 struct syscalls_info *sysinfo;
257 struct cleanup *back_to;
259 full_file = xml_fetch_content_from_file (filename, gdb_datadir);
260 if (full_file == NULL)
263 back_to = make_cleanup (xfree, full_file);
265 dirname = ldirname (filename);
267 make_cleanup (xfree, dirname);
269 sysinfo = syscall_parse_xml (full_file,
270 xml_fetch_content_from_file, dirname);
271 do_cleanups (back_to);
276 /* Initializes the syscalls_info structure according to the
281 /* Should we re-read the XML info for this target? */
282 if (my_gdb_datadir && filename_cmp (my_gdb_datadir, gdb_datadir) != 0)
284 /* The data-directory changed from the last time we used it.
285 It means that we have to re-read the XML info. */
286 have_initialized_sysinfo = 0;
287 xfree (my_gdb_datadir);
288 my_gdb_datadir = NULL;
290 free_syscalls_info ((void *) sysinfo);
293 /* Did we already try to initialize the structure? */
294 if (have_initialized_sysinfo)
297 sysinfo = xml_init_syscalls_info (xml_syscall_file);
299 have_initialized_sysinfo = 1;
303 if (xml_syscall_file)
304 warning (_("Could not load the syscall XML file `%s/%s'."),
305 gdb_datadir, xml_syscall_file);
307 warning (_("There is no XML file to open."));
309 warning (_("GDB will not be able to display "
310 "syscall names nor to verify if\n"
311 "any provided syscall numbers are valid."));
314 /* Saving the data-directory used to read this XML info. */
315 my_gdb_datadir = xstrdup (gdb_datadir);
319 xml_get_syscall_number (const struct syscalls_info *sysinfo,
320 const char *syscall_name)
322 struct syscall_desc *sysdesc;
326 || syscall_name == NULL)
327 return UNKNOWN_SYSCALL;
330 VEC_iterate(syscall_desc_p, sysinfo->syscalls, i, sysdesc);
332 if (strcmp (sysdesc->name, syscall_name) == 0)
333 return sysdesc->number;
335 return UNKNOWN_SYSCALL;
339 xml_get_syscall_name (const struct syscalls_info *sysinfo,
342 struct syscall_desc *sysdesc;
346 || syscall_number < 0)
350 VEC_iterate(syscall_desc_p, sysinfo->syscalls, i, sysdesc);
352 if (sysdesc->number == syscall_number)
353 return sysdesc->name;
359 xml_list_of_syscalls (const struct syscalls_info *sysinfo)
361 struct syscall_desc *sysdesc;
362 const char **names = NULL;
369 nsyscalls = VEC_length (syscall_desc_p, sysinfo->syscalls);
370 names = xmalloc ((nsyscalls + 1) * sizeof (char *));
373 VEC_iterate (syscall_desc_p, sysinfo->syscalls, i, sysdesc);
375 names[i] = sysdesc->name;
383 set_xml_syscall_file_name (const char *name)
385 xml_syscall_file = name;
389 get_syscall_by_number (int syscall_number,
394 s->number = syscall_number;
395 s->name = xml_get_syscall_name (sysinfo, syscall_number);
399 get_syscall_by_name (const char *syscall_name,
404 s->number = xml_get_syscall_number (sysinfo, syscall_name);
405 s->name = syscall_name;
409 get_syscall_names (void)
413 return xml_list_of_syscalls (sysinfo);
416 #endif /* ! HAVE_LIBEXPAT */