1 /* Functions that provide the mechanism to parse a syscall XML file
4 Copyright (C) 2009-2018 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"
27 /* For the struct syscall definition. */
30 #include "filenames.h"
34 /* Dummy functions to indicate that there's no support for fetching
35 syscalls information. */
38 syscall_warn_user (void)
40 static int have_warned = 0;
44 warning (_("Can not parse XML syscalls information; XML support was "
45 "disabled at compile time."));
50 set_xml_syscall_file_name (struct gdbarch *gdbarch, const char *name)
56 get_syscall_by_number (struct gdbarch *gdbarch,
57 int syscall_number, struct syscall *s)
60 s->number = syscall_number;
65 get_syscall_by_name (struct gdbarch *gdbarch, const char *syscall_name,
69 s->number = UNKNOWN_SYSCALL;
70 s->name = syscall_name;
74 get_syscall_names (struct gdbarch *gdbarch)
81 get_syscalls_by_group (struct gdbarch *gdbarch, const char *group)
88 get_syscall_group_names (struct gdbarch *gdbarch)
94 #else /* ! HAVE_LIBEXPAT */
96 /* Structure which describes a syscall. */
99 syscall_desc (int number_, std::string name_)
100 : number (number_), name (name_)
103 /* The syscall number. */
107 /* The syscall name. */
112 typedef std::unique_ptr<syscall_desc> syscall_desc_up;
114 /* Structure of a syscall group. */
115 struct syscall_group_desc
117 syscall_group_desc (const std::string &name_)
121 /* The group name. */
125 /* The syscalls that are part of the group. This is a non-owning
128 std::vector<syscall_desc *> syscalls;
131 typedef std::unique_ptr<syscall_group_desc> syscall_group_desc_up;
133 /* Structure that represents syscalls information. */
138 std::vector<syscall_desc_up> syscalls;
140 /* The syscall groups. */
142 std::vector<syscall_group_desc_up> groups;
144 /* Variable that will hold the last known data-directory. This is
145 useful to know whether we should re-read the XML info for the
148 std::string my_gdb_datadir;
151 typedef std::unique_ptr<syscalls_info> syscalls_info_up;
153 /* Callback data for syscall information parsing. */
154 struct syscall_parsing_data
156 /* The syscalls_info we are building. */
158 struct syscalls_info *syscalls_info;
161 /* Create a new syscall group. Return pointer to the
162 syscall_group_desc structure that represents the new group. */
164 static struct syscall_group_desc *
165 syscall_group_create_syscall_group_desc (struct syscalls_info *syscalls_info,
168 syscall_group_desc *groupdesc = new syscall_group_desc (group);
170 syscalls_info->groups.emplace_back (groupdesc);
175 /* Add a syscall to the group. If group doesn't exist, create it. */
178 syscall_group_add_syscall (struct syscalls_info *syscalls_info,
179 struct syscall_desc *syscall,
182 /* Search for an existing group. */
183 std::vector<syscall_group_desc_up>::iterator it
184 = syscalls_info->groups.begin ();
186 for (; it != syscalls_info->groups.end (); it++)
188 if ((*it)->name == group)
192 syscall_group_desc *groupdesc;
194 if (it != syscalls_info->groups.end ())
195 groupdesc = it->get ();
198 /* No group was found with this name. We must create a new
200 groupdesc = syscall_group_create_syscall_group_desc (syscalls_info,
204 groupdesc->syscalls.push_back (syscall);
208 syscall_create_syscall_desc (struct syscalls_info *syscalls_info,
209 const char *name, int number,
212 syscall_desc *sysdesc = new syscall_desc (number, name);
214 syscalls_info->syscalls.emplace_back (sysdesc);
216 /* Add syscall to its groups. */
219 for (char *group = strtok (groups, ",");
221 group = strtok (NULL, ","))
222 syscall_group_add_syscall (syscalls_info, sysdesc, group);
226 /* Handle the start of a <syscall> element. */
228 syscall_start_syscall (struct gdb_xml_parser *parser,
229 const struct gdb_xml_element *element,
231 std::vector<gdb_xml_value> &attributes)
233 struct syscall_parsing_data *data = (struct syscall_parsing_data *) user_data;
239 for (const gdb_xml_value &attr : attributes)
241 if (strcmp (attr.name, "name") == 0)
242 name = (char *) attr.value.get ();
243 else if (strcmp (attr.name, "number") == 0)
244 number = * (ULONGEST *) attr.value.get ();
245 else if (strcmp (attr.name, "groups") == 0)
246 groups = (char *) attr.value.get ();
248 internal_error (__FILE__, __LINE__,
249 _("Unknown attribute name '%s'."), attr.name);
253 syscall_create_syscall_desc (data->syscalls_info, name, number, groups);
257 /* The elements and attributes of an XML syscall document. */
258 static const struct gdb_xml_attribute syscall_attr[] = {
259 { "number", GDB_XML_AF_NONE, gdb_xml_parse_attr_ulongest, NULL },
260 { "name", GDB_XML_AF_NONE, NULL, NULL },
261 { "groups", GDB_XML_AF_OPTIONAL, NULL, NULL },
262 { NULL, GDB_XML_AF_NONE, NULL, NULL }
265 static const struct gdb_xml_element syscalls_info_children[] = {
266 { "syscall", syscall_attr, NULL,
267 GDB_XML_EF_OPTIONAL | GDB_XML_EF_REPEATABLE,
268 syscall_start_syscall, NULL },
269 { NULL, NULL, NULL, GDB_XML_EF_NONE, NULL, NULL }
272 static const struct gdb_xml_element syselements[] = {
273 { "syscalls_info", NULL, syscalls_info_children,
274 GDB_XML_EF_NONE, NULL, NULL },
275 { NULL, NULL, NULL, GDB_XML_EF_NONE, NULL, NULL }
278 static struct syscalls_info *
279 syscall_parse_xml (const char *document, xml_fetch_another fetcher,
282 struct syscall_parsing_data data;
283 syscalls_info_up sysinfo (new syscalls_info ());
285 data.syscalls_info = sysinfo.get ();
287 if (gdb_xml_parse_quick (_("syscalls info"), NULL,
288 syselements, document, &data) == 0)
290 /* Parsed successfully. */
291 return sysinfo.release ();
295 warning (_("Could not load XML syscalls info; ignoring"));
300 /* Function responsible for initializing the information
301 about the syscalls. It reads the XML file and fills the
302 struct syscalls_info with the values.
304 Returns the struct syscalls_info if the file is valid, NULL otherwise. */
305 static struct syscalls_info *
306 xml_init_syscalls_info (const char *filename)
308 gdb::optional<gdb::char_vector> full_file
309 = xml_fetch_content_from_file (filename, gdb_datadir);
313 return syscall_parse_xml (full_file->data (),
314 xml_fetch_content_from_file,
315 (void *) ldirname (filename).c_str ());
318 /* Initializes the syscalls_info structure according to the
321 init_syscalls_info (struct gdbarch *gdbarch)
323 struct syscalls_info *syscalls_info = gdbarch_syscalls_info (gdbarch);
324 const char *xml_syscall_file = gdbarch_xml_syscall_file (gdbarch);
326 /* Should we re-read the XML info for this target? */
327 if (syscalls_info != NULL && !syscalls_info->my_gdb_datadir.empty ()
328 && filename_cmp (syscalls_info->my_gdb_datadir.c_str (),
331 /* The data-directory changed from the last time we used it.
332 It means that we have to re-read the XML info. */
333 delete syscalls_info;
334 syscalls_info = NULL;
335 set_gdbarch_syscalls_info (gdbarch, NULL);
338 /* Did we succeed at initializing this? */
339 if (syscalls_info != NULL)
342 syscalls_info = xml_init_syscalls_info (xml_syscall_file);
344 /* If there was some error reading the XML file, we initialize
345 gdbarch->syscalls_info anyway, in order to store information
346 about our attempt. */
347 if (syscalls_info == NULL)
348 syscalls_info = new struct syscalls_info ();
350 if (syscalls_info->syscalls.empty ())
352 if (xml_syscall_file != NULL)
353 warning (_("Could not load the syscall XML file `%s/%s'."),
354 gdb_datadir, xml_syscall_file);
356 warning (_("There is no XML file to open."));
358 warning (_("GDB will not be able to display "
359 "syscall names nor to verify if\n"
360 "any provided syscall numbers are valid."));
363 /* Saving the data-directory used to read this XML info. */
364 syscalls_info->my_gdb_datadir.assign (gdb_datadir);
366 set_gdbarch_syscalls_info (gdbarch, syscalls_info);
369 /* Search for a syscall group by its name. Return syscall_group_desc
370 structure for the group if found or NULL otherwise. */
372 static struct syscall_group_desc *
373 syscall_group_get_group_by_name (const struct syscalls_info *syscalls_info,
376 if (syscalls_info == NULL)
382 /* Search for existing group. */
383 for (const syscall_group_desc_up &groupdesc : syscalls_info->groups)
385 if (groupdesc->name == group)
386 return groupdesc.get ();
393 xml_get_syscall_number (struct gdbarch *gdbarch,
394 const char *syscall_name)
396 struct syscalls_info *syscalls_info = gdbarch_syscalls_info (gdbarch);
398 if (syscalls_info == NULL
399 || syscall_name == NULL)
400 return UNKNOWN_SYSCALL;
402 for (const syscall_desc_up &sysdesc : syscalls_info->syscalls)
403 if (sysdesc->name == syscall_name)
404 return sysdesc->number;
406 return UNKNOWN_SYSCALL;
410 xml_get_syscall_name (struct gdbarch *gdbarch,
413 struct syscalls_info *syscalls_info = gdbarch_syscalls_info (gdbarch);
415 if (syscalls_info == NULL
416 || syscall_number < 0)
419 for (const syscall_desc_up &sysdesc : syscalls_info->syscalls)
420 if (sysdesc->number == syscall_number)
421 return sysdesc->name.c_str ();
427 xml_list_of_syscalls (struct gdbarch *gdbarch)
429 struct syscalls_info *syscalls_info = gdbarch_syscalls_info (gdbarch);
431 if (syscalls_info == NULL)
434 int nsyscalls = syscalls_info->syscalls.size ();
435 const char **names = XNEWVEC (const char *, nsyscalls + 1);
438 for (i = 0; i < syscalls_info->syscalls.size (); i++)
439 names[i] = syscalls_info->syscalls[i]->name.c_str ();
446 /* Iterate over the syscall_group_desc element to return a list of
447 syscalls that are part of the given group, terminated by an empty
448 element. If the syscall group doesn't exist, return NULL. */
450 static struct syscall *
451 xml_list_syscalls_by_group (struct gdbarch *gdbarch, const char *group)
453 struct syscalls_info *syscalls_info = gdbarch_syscalls_info (gdbarch);
454 struct syscall_group_desc *groupdesc;
455 struct syscall *syscalls = NULL;
459 if (syscalls_info == NULL)
462 groupdesc = syscall_group_get_group_by_name (syscalls_info, group);
463 if (groupdesc == NULL)
466 nsyscalls = groupdesc->syscalls.size ();
467 syscalls = (struct syscall*) xmalloc ((nsyscalls + 1)
468 * sizeof (struct syscall));
470 for (i = 0; i < groupdesc->syscalls.size (); i++)
472 syscalls[i].name = groupdesc->syscalls[i]->name.c_str ();
473 syscalls[i].number = groupdesc->syscalls[i]->number;
476 /* Add final element marker. */
477 syscalls[i].name = NULL;
478 syscalls[i].number = 0;
483 /* Return a NULL terminated list of syscall groups or an empty list, if
484 no syscall group is available. Return NULL, if there is no syscall
485 information available. */
488 xml_list_of_groups (struct gdbarch *gdbarch)
490 struct syscalls_info *syscalls_info = gdbarch_syscalls_info (gdbarch);
491 const char **names = NULL;
495 if (syscalls_info == NULL)
498 ngroups = syscalls_info->groups.size ();
499 names = (const char**) xmalloc ((ngroups + 1) * sizeof (char *));
501 for (i = 0; i < syscalls_info->groups.size (); i++)
502 names[i] = syscalls_info->groups[i]->name.c_str ();
510 set_xml_syscall_file_name (struct gdbarch *gdbarch, const char *name)
512 set_gdbarch_xml_syscall_file (gdbarch, name);
516 get_syscall_by_number (struct gdbarch *gdbarch,
517 int syscall_number, struct syscall *s)
519 init_syscalls_info (gdbarch);
521 s->number = syscall_number;
522 s->name = xml_get_syscall_name (gdbarch, syscall_number);
526 get_syscall_by_name (struct gdbarch *gdbarch,
527 const char *syscall_name, struct syscall *s)
529 init_syscalls_info (gdbarch);
531 s->number = xml_get_syscall_number (gdbarch, syscall_name);
532 s->name = syscall_name;
536 get_syscall_names (struct gdbarch *gdbarch)
538 init_syscalls_info (gdbarch);
540 return xml_list_of_syscalls (gdbarch);
543 /* See comment in xml-syscall.h. */
546 get_syscalls_by_group (struct gdbarch *gdbarch, const char *group)
548 init_syscalls_info (gdbarch);
550 return xml_list_syscalls_by_group (gdbarch, group);
553 /* See comment in xml-syscall.h. */
556 get_syscall_group_names (struct gdbarch *gdbarch)
558 init_syscalls_info (gdbarch);
560 return xml_list_of_groups (gdbarch);
563 #endif /* ! HAVE_LIBEXPAT */