1 /* Functions that provide the mechanism to parse a syscall XML file
4 Copyright (C) 2009-2019 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_syscalls_by_name (struct gdbarch
*gdbarch
, const char *syscall_name
,
66 std::vector
<int> *syscall_numbers
)
73 get_syscall_names (struct gdbarch
*gdbarch
)
80 get_syscalls_by_group (struct gdbarch
*gdbarch
, const char *group
,
81 std::vector
<int> *syscall_numbers
)
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_
, std::string alias_
)
100 : number (number_
), name (name_
), alias (alias_
)
103 /* The syscall number. */
107 /* The syscall name. */
111 /* An optional alias. */
116 typedef std::unique_ptr
<syscall_desc
> syscall_desc_up
;
118 /* Structure of a syscall group. */
119 struct syscall_group_desc
121 syscall_group_desc (const std::string
&name_
)
125 /* The group name. */
129 /* The syscalls that are part of the group. This is a non-owning
132 std::vector
<syscall_desc
*> syscalls
;
135 typedef std::unique_ptr
<syscall_group_desc
> syscall_group_desc_up
;
137 /* Structure that represents syscalls information. */
142 std::vector
<syscall_desc_up
> syscalls
;
144 /* The syscall groups. */
146 std::vector
<syscall_group_desc_up
> groups
;
148 /* Variable that will hold the last known data-directory. This is
149 useful to know whether we should re-read the XML info for the
152 std::string my_gdb_datadir
;
155 typedef std::unique_ptr
<syscalls_info
> syscalls_info_up
;
157 /* Callback data for syscall information parsing. */
158 struct syscall_parsing_data
160 /* The syscalls_info we are building. */
162 struct syscalls_info
*syscalls_info
;
165 /* Create a new syscall group. Return pointer to the
166 syscall_group_desc structure that represents the new group. */
168 static struct syscall_group_desc
*
169 syscall_group_create_syscall_group_desc (struct syscalls_info
*syscalls_info
,
172 syscall_group_desc
*groupdesc
= new syscall_group_desc (group
);
174 syscalls_info
->groups
.emplace_back (groupdesc
);
179 /* Add a syscall to the group. If group doesn't exist, create it. */
182 syscall_group_add_syscall (struct syscalls_info
*syscalls_info
,
183 struct syscall_desc
*syscall
,
186 /* Search for an existing group. */
187 std::vector
<syscall_group_desc_up
>::iterator it
188 = syscalls_info
->groups
.begin ();
190 for (; it
!= syscalls_info
->groups
.end (); it
++)
192 if ((*it
)->name
== group
)
196 syscall_group_desc
*groupdesc
;
198 if (it
!= syscalls_info
->groups
.end ())
199 groupdesc
= it
->get ();
202 /* No group was found with this name. We must create a new
204 groupdesc
= syscall_group_create_syscall_group_desc (syscalls_info
,
208 groupdesc
->syscalls
.push_back (syscall
);
212 syscall_create_syscall_desc (struct syscalls_info
*syscalls_info
,
213 const char *name
, int number
, const char *alias
,
216 syscall_desc
*sysdesc
= new syscall_desc (number
, name
,
217 alias
!= NULL
? alias
: "");
219 syscalls_info
->syscalls
.emplace_back (sysdesc
);
221 /* Add syscall to its groups. */
225 for (char *group
= strtok_r (groups
, ",", &saveptr
);
227 group
= strtok_r (NULL
, ",", &saveptr
))
228 syscall_group_add_syscall (syscalls_info
, sysdesc
, group
);
232 /* Handle the start of a <syscall> element. */
234 syscall_start_syscall (struct gdb_xml_parser
*parser
,
235 const struct gdb_xml_element
*element
,
237 std::vector
<gdb_xml_value
> &attributes
)
239 struct syscall_parsing_data
*data
= (struct syscall_parsing_data
*) user_data
;
246 for (const gdb_xml_value
&attr
: attributes
)
248 if (strcmp (attr
.name
, "name") == 0)
249 name
= (char *) attr
.value
.get ();
250 else if (strcmp (attr
.name
, "number") == 0)
251 number
= * (ULONGEST
*) attr
.value
.get ();
252 else if (strcmp (attr
.name
, "alias") == 0)
253 alias
= (char *) attr
.value
.get ();
254 else if (strcmp (attr
.name
, "groups") == 0)
255 groups
= (char *) attr
.value
.get ();
257 internal_error (__FILE__
, __LINE__
,
258 _("Unknown attribute name '%s'."), attr
.name
);
262 syscall_create_syscall_desc (data
->syscalls_info
, name
, number
, alias
,
267 /* The elements and attributes of an XML syscall document. */
268 static const struct gdb_xml_attribute syscall_attr
[] = {
269 { "number", GDB_XML_AF_NONE
, gdb_xml_parse_attr_ulongest
, NULL
},
270 { "name", GDB_XML_AF_NONE
, NULL
, NULL
},
271 { "alias", GDB_XML_AF_OPTIONAL
, NULL
, NULL
},
272 { "groups", GDB_XML_AF_OPTIONAL
, NULL
, NULL
},
273 { NULL
, GDB_XML_AF_NONE
, NULL
, NULL
}
276 static const struct gdb_xml_element syscalls_info_children
[] = {
277 { "syscall", syscall_attr
, NULL
,
278 GDB_XML_EF_OPTIONAL
| GDB_XML_EF_REPEATABLE
,
279 syscall_start_syscall
, NULL
},
280 { NULL
, NULL
, NULL
, GDB_XML_EF_NONE
, NULL
, NULL
}
283 static const struct gdb_xml_element syselements
[] = {
284 { "syscalls_info", NULL
, syscalls_info_children
,
285 GDB_XML_EF_NONE
, NULL
, NULL
},
286 { NULL
, NULL
, NULL
, GDB_XML_EF_NONE
, NULL
, NULL
}
289 static struct syscalls_info
*
290 syscall_parse_xml (const char *document
, xml_fetch_another fetcher
,
293 struct syscall_parsing_data data
;
294 syscalls_info_up
sysinfo (new syscalls_info ());
296 data
.syscalls_info
= sysinfo
.get ();
298 if (gdb_xml_parse_quick (_("syscalls info"), NULL
,
299 syselements
, document
, &data
) == 0)
301 /* Parsed successfully. */
302 return sysinfo
.release ();
306 warning (_("Could not load XML syscalls info; ignoring"));
311 /* Function responsible for initializing the information
312 about the syscalls. It reads the XML file and fills the
313 struct syscalls_info with the values.
315 Returns the struct syscalls_info if the file is valid, NULL otherwise. */
316 static struct syscalls_info
*
317 xml_init_syscalls_info (const char *filename
)
319 gdb::optional
<gdb::char_vector
> full_file
320 = xml_fetch_content_from_file (filename
,
321 const_cast<char *>(gdb_datadir
.c_str ()));
325 return syscall_parse_xml (full_file
->data (),
326 xml_fetch_content_from_file
,
327 (void *) ldirname (filename
).c_str ());
330 /* Initializes the syscalls_info structure according to the
333 init_syscalls_info (struct gdbarch
*gdbarch
)
335 struct syscalls_info
*syscalls_info
= gdbarch_syscalls_info (gdbarch
);
336 const char *xml_syscall_file
= gdbarch_xml_syscall_file (gdbarch
);
338 /* Should we re-read the XML info for this target? */
339 if (syscalls_info
!= NULL
&& !syscalls_info
->my_gdb_datadir
.empty ()
340 && filename_cmp (syscalls_info
->my_gdb_datadir
.c_str (),
341 gdb_datadir
.c_str ()) != 0)
343 /* The data-directory changed from the last time we used it.
344 It means that we have to re-read the XML info. */
345 delete syscalls_info
;
346 syscalls_info
= NULL
;
347 set_gdbarch_syscalls_info (gdbarch
, NULL
);
350 /* Did we succeed at initializing this? */
351 if (syscalls_info
!= NULL
)
354 syscalls_info
= xml_init_syscalls_info (xml_syscall_file
);
356 /* If there was some error reading the XML file, we initialize
357 gdbarch->syscalls_info anyway, in order to store information
358 about our attempt. */
359 if (syscalls_info
== NULL
)
360 syscalls_info
= new struct syscalls_info ();
362 if (syscalls_info
->syscalls
.empty ())
364 if (xml_syscall_file
!= NULL
)
365 warning (_("Could not load the syscall XML file `%s/%s'."),
366 gdb_datadir
.c_str (), xml_syscall_file
);
368 warning (_("There is no XML file to open."));
370 warning (_("GDB will not be able to display "
371 "syscall names nor to verify if\n"
372 "any provided syscall numbers are valid."));
375 /* Saving the data-directory used to read this XML info. */
376 syscalls_info
->my_gdb_datadir
.assign (gdb_datadir
);
378 set_gdbarch_syscalls_info (gdbarch
, syscalls_info
);
381 /* Search for a syscall group by its name. Return syscall_group_desc
382 structure for the group if found or NULL otherwise. */
384 static struct syscall_group_desc
*
385 syscall_group_get_group_by_name (const struct syscalls_info
*syscalls_info
,
388 if (syscalls_info
== NULL
)
394 /* Search for existing group. */
395 for (const syscall_group_desc_up
&groupdesc
: syscalls_info
->groups
)
397 if (groupdesc
->name
== group
)
398 return groupdesc
.get ();
405 xml_get_syscalls_by_name (struct gdbarch
*gdbarch
, const char *syscall_name
,
406 std::vector
<int> *syscall_numbers
)
408 struct syscalls_info
*syscalls_info
= gdbarch_syscalls_info (gdbarch
);
411 if (syscalls_info
!= NULL
&& syscall_name
!= NULL
&& syscall_numbers
!= NULL
)
412 for (const syscall_desc_up
&sysdesc
: syscalls_info
->syscalls
)
413 if (sysdesc
->name
== syscall_name
|| sysdesc
->alias
== syscall_name
)
415 syscall_numbers
->push_back (sysdesc
->number
);
423 xml_get_syscall_name (struct gdbarch
*gdbarch
,
426 struct syscalls_info
*syscalls_info
= gdbarch_syscalls_info (gdbarch
);
428 if (syscalls_info
== NULL
429 || syscall_number
< 0)
432 for (const syscall_desc_up
&sysdesc
: syscalls_info
->syscalls
)
433 if (sysdesc
->number
== syscall_number
)
434 return sysdesc
->name
.c_str ();
440 xml_list_of_syscalls (struct gdbarch
*gdbarch
)
442 struct syscalls_info
*syscalls_info
= gdbarch_syscalls_info (gdbarch
);
444 if (syscalls_info
== NULL
)
447 int nsyscalls
= syscalls_info
->syscalls
.size ();
448 const char **names
= XNEWVEC (const char *, nsyscalls
+ 1);
451 for (i
= 0; i
< syscalls_info
->syscalls
.size (); i
++)
452 names
[i
] = syscalls_info
->syscalls
[i
]->name
.c_str ();
459 /* Iterate over the syscall_group_desc element to return a list of
460 syscalls that are part of the given group. If the syscall group
461 doesn't exist, return false. */
464 xml_list_syscalls_by_group (struct gdbarch
*gdbarch
, const char *group
,
465 std::vector
<int> *syscalls
)
467 struct syscalls_info
*syscalls_info
= gdbarch_syscalls_info (gdbarch
);
468 struct syscall_group_desc
*groupdesc
;
470 if (syscalls_info
== NULL
|| syscalls
== NULL
)
473 groupdesc
= syscall_group_get_group_by_name (syscalls_info
, group
);
474 if (groupdesc
== NULL
)
477 for (const syscall_desc
*sysdesc
: groupdesc
->syscalls
)
478 syscalls
->push_back (sysdesc
->number
);
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_syscalls_by_name (struct gdbarch
*gdbarch
, const char *syscall_name
,
527 std::vector
<int> *syscall_numbers
)
529 init_syscalls_info (gdbarch
);
531 return xml_get_syscalls_by_name (gdbarch
, syscall_name
, syscall_numbers
);
535 get_syscall_names (struct gdbarch
*gdbarch
)
537 init_syscalls_info (gdbarch
);
539 return xml_list_of_syscalls (gdbarch
);
542 /* See comment in xml-syscall.h. */
545 get_syscalls_by_group (struct gdbarch
*gdbarch
, const char *group
,
546 std::vector
<int> *syscall_numbers
)
548 init_syscalls_info (gdbarch
);
550 return xml_list_syscalls_by_group (gdbarch
, group
, syscall_numbers
);
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 */