fix typos in ada-lang.c comment
[deliverable/binutils-gdb.git] / gdb / xml-syscall.c
CommitLineData
fbbe92c5
SDJ
1/* Functions that provide the mechanism to parse a syscall XML file
2 and get its values.
3
61baf725 4 Copyright (C) 2009-2017 Free Software Foundation, Inc.
fbbe92c5
SDJ
5
6 This file is part of GDB.
7
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.
12
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.
17
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/>. */
20
21#include "defs.h"
22#include "gdbtypes.h"
23#include "xml-support.h"
24#include "xml-syscall.h"
458c8db8 25#include "gdbarch.h"
fbbe92c5
SDJ
26
27/* For the struct syscall definition. */
28#include "target.h"
29
30#include "filenames.h"
31
fbbe92c5
SDJ
32#ifndef HAVE_LIBEXPAT
33
34/* Dummy functions to indicate that there's no support for fetching
35 syscalls information. */
36
37static void
38syscall_warn_user (void)
39{
40 static int have_warned = 0;
41 if (!have_warned)
42 {
43 have_warned = 1;
44 warning (_("Can not parse XML syscalls information; XML support was "
45 "disabled at compile time."));
46 }
47}
48
49void
7e7cbeb3 50set_xml_syscall_file_name (struct gdbarch *gdbarch, const char *name)
fbbe92c5 51{
bccd0dd2 52 return;
fbbe92c5
SDJ
53}
54
55void
7e7cbeb3 56get_syscall_by_number (struct gdbarch *gdbarch,
458c8db8 57 int syscall_number, struct syscall *s)
fbbe92c5
SDJ
58{
59 syscall_warn_user ();
60 s->number = syscall_number;
61 s->name = NULL;
62}
63
64void
7e7cbeb3
SDJ
65get_syscall_by_name (struct gdbarch *gdbarch, const char *syscall_name,
66 struct syscall *s)
fbbe92c5
SDJ
67{
68 syscall_warn_user ();
69 s->number = UNKNOWN_SYSCALL;
70 s->name = syscall_name;
71}
72
73const char **
7e7cbeb3 74get_syscall_names (struct gdbarch *gdbarch)
fbbe92c5
SDJ
75{
76 syscall_warn_user ();
77 return NULL;
78}
79
e3487908
GKB
80struct syscall *
81get_syscalls_by_group (struct gdbarch *gdbarch, const char *group)
82{
83 syscall_warn_user ();
84 return NULL;
85}
86
87const char **
88get_syscall_group_names (struct gdbarch *gdbarch)
89{
90 syscall_warn_user ();
91 return NULL;
92}
93
fbbe92c5
SDJ
94#else /* ! HAVE_LIBEXPAT */
95
96/* Structure which describes a syscall. */
5a9dcda1 97struct syscall_desc
fbbe92c5 98{
5a9dcda1
SM
99 syscall_desc (int number_, std::string name_)
100 : number (number_), name (name_)
101 {}
102
fbbe92c5
SDJ
103 /* The syscall number. */
104
105 int number;
106
107 /* The syscall name. */
108
5a9dcda1
SM
109 std::string name;
110};
111
112typedef std::unique_ptr<syscall_desc> syscall_desc_up;
fbbe92c5 113
e3487908 114/* Structure of a syscall group. */
5a9dcda1 115struct syscall_group_desc
e3487908 116{
5a9dcda1
SM
117 syscall_group_desc (const std::string &name_)
118 : name (name_)
119 {}
120
e3487908
GKB
121 /* The group name. */
122
5a9dcda1 123 std::string name;
e3487908 124
5a9dcda1
SM
125 /* The syscalls that are part of the group. This is a non-owning
126 reference. */
e3487908 127
5a9dcda1
SM
128 std::vector<syscall_desc *> syscalls;
129};
130
131typedef std::unique_ptr<syscall_group_desc> syscall_group_desc_up;
e3487908 132
fbbe92c5
SDJ
133/* Structure that represents syscalls information. */
134struct syscalls_info
135{
136 /* The syscalls. */
137
5a9dcda1 138 std::vector<syscall_desc_up> syscalls;
458c8db8 139
e3487908
GKB
140 /* The syscall groups. */
141
5a9dcda1 142 std::vector<syscall_group_desc_up> groups;
e3487908 143
458c8db8
SDJ
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
146 target. */
147
5a9dcda1 148 std::string my_gdb_datadir;
fbbe92c5
SDJ
149};
150
5a9dcda1
SM
151typedef std::unique_ptr<syscalls_info> syscalls_info_up;
152
fbbe92c5
SDJ
153/* Callback data for syscall information parsing. */
154struct syscall_parsing_data
155{
156 /* The syscalls_info we are building. */
157
458c8db8 158 struct syscalls_info *syscalls_info;
fbbe92c5
SDJ
159};
160
e3487908
GKB
161/* Create a new syscall group. Return pointer to the
162 syscall_group_desc structure that represents the new group. */
163
164static struct syscall_group_desc *
165syscall_group_create_syscall_group_desc (struct syscalls_info *syscalls_info,
166 const char *group)
167{
5a9dcda1 168 syscall_group_desc *groupdesc = new syscall_group_desc (group);
e3487908 169
5a9dcda1 170 syscalls_info->groups.emplace_back (groupdesc);
e3487908
GKB
171
172 return groupdesc;
173}
174
175/* Add a syscall to the group. If group doesn't exist, create it. */
176
177static void
178syscall_group_add_syscall (struct syscalls_info *syscalls_info,
179 struct syscall_desc *syscall,
180 const char *group)
181{
e3487908 182 /* Search for an existing group. */
5a9dcda1
SM
183 std::vector<syscall_group_desc_up>::iterator it
184 = syscalls_info->groups.begin ();
185
186 for (; it != syscalls_info->groups.end (); it++)
e3487908 187 {
5a9dcda1 188 if ((*it)->name == group)
e3487908
GKB
189 break;
190 }
191
5a9dcda1
SM
192 syscall_group_desc *groupdesc;
193
194 if (it != syscalls_info->groups.end ())
195 groupdesc = it->get ();
196 else
e3487908
GKB
197 {
198 /* No group was found with this name. We must create a new
199 one. */
200 groupdesc = syscall_group_create_syscall_group_desc (syscalls_info,
201 group);
202 }
203
5a9dcda1 204 groupdesc->syscalls.push_back (syscall);
e3487908
GKB
205}
206
fbbe92c5 207static void
458c8db8 208syscall_create_syscall_desc (struct syscalls_info *syscalls_info,
e3487908
GKB
209 const char *name, int number,
210 char *groups)
fbbe92c5 211{
5a9dcda1 212 syscall_desc *sysdesc = new syscall_desc (number, name);
fbbe92c5 213
5a9dcda1 214 syscalls_info->syscalls.emplace_back (sysdesc);
e3487908
GKB
215
216 /* Add syscall to its groups. */
217 if (groups != NULL)
218 {
5a9dcda1 219 for (char *group = strtok (groups, ",");
e3487908
GKB
220 group != NULL;
221 group = strtok (NULL, ","))
222 syscall_group_add_syscall (syscalls_info, sysdesc, group);
223 }
fbbe92c5
SDJ
224}
225
fbbe92c5
SDJ
226/* Handle the start of a <syscall> element. */
227static void
228syscall_start_syscall (struct gdb_xml_parser *parser,
229 const struct gdb_xml_element *element,
230 void *user_data, VEC(gdb_xml_value_s) *attributes)
231{
19ba03f4 232 struct syscall_parsing_data *data = (struct syscall_parsing_data *) user_data;
fbbe92c5
SDJ
233 struct gdb_xml_value *attrs = VEC_address (gdb_xml_value_s, attributes);
234 int len, i;
235 /* syscall info. */
236 char *name = NULL;
237 int number = 0;
e3487908 238 char *groups = NULL;
fbbe92c5
SDJ
239
240 len = VEC_length (gdb_xml_value_s, attributes);
241
242 for (i = 0; i < len; i++)
243 {
244 if (strcmp (attrs[i].name, "name") == 0)
19ba03f4 245 name = (char *) attrs[i].value;
fbbe92c5
SDJ
246 else if (strcmp (attrs[i].name, "number") == 0)
247 number = * (ULONGEST *) attrs[i].value;
e3487908
GKB
248 else if (strcmp (attrs[i].name, "groups") == 0)
249 groups = (char *) attrs[i].value;
fbbe92c5
SDJ
250 else
251 internal_error (__FILE__, __LINE__,
252 _("Unknown attribute name '%s'."), attrs[i].name);
253 }
254
154f592e 255 gdb_assert (name);
e3487908 256 syscall_create_syscall_desc (data->syscalls_info, name, number, groups);
fbbe92c5
SDJ
257}
258
259
260/* The elements and attributes of an XML syscall document. */
261static const struct gdb_xml_attribute syscall_attr[] = {
262 { "number", GDB_XML_AF_NONE, gdb_xml_parse_attr_ulongest, NULL },
263 { "name", GDB_XML_AF_NONE, NULL, NULL },
e3487908 264 { "groups", GDB_XML_AF_OPTIONAL, NULL, NULL },
fbbe92c5
SDJ
265 { NULL, GDB_XML_AF_NONE, NULL, NULL }
266};
267
268static const struct gdb_xml_element syscalls_info_children[] = {
269 { "syscall", syscall_attr, NULL,
270 GDB_XML_EF_OPTIONAL | GDB_XML_EF_REPEATABLE,
271 syscall_start_syscall, NULL },
272 { NULL, NULL, NULL, GDB_XML_EF_NONE, NULL, NULL }
273};
274
275static const struct gdb_xml_element syselements[] = {
276 { "syscalls_info", NULL, syscalls_info_children,
f8e50cfe 277 GDB_XML_EF_NONE, NULL, NULL },
fbbe92c5
SDJ
278 { NULL, NULL, NULL, GDB_XML_EF_NONE, NULL, NULL }
279};
280
281static struct syscalls_info *
282syscall_parse_xml (const char *document, xml_fetch_another fetcher,
283 void *fetcher_baton)
284{
fbbe92c5 285 struct syscall_parsing_data data;
5a9dcda1 286 syscalls_info_up sysinfo (new syscalls_info ());
fbbe92c5 287
5a9dcda1 288 data.syscalls_info = sysinfo.get ();
fbbe92c5 289
efc0eabd
PA
290 if (gdb_xml_parse_quick (_("syscalls info"), NULL,
291 syselements, document, &data) == 0)
fbbe92c5
SDJ
292 {
293 /* Parsed successfully. */
5a9dcda1 294 return sysinfo.release ();
fbbe92c5
SDJ
295 }
296 else
297 {
298 warning (_("Could not load XML syscalls info; ignoring"));
fbbe92c5
SDJ
299 return NULL;
300 }
301}
302
303/* Function responsible for initializing the information
304 about the syscalls. It reads the XML file and fills the
305 struct syscalls_info with the values.
306
307 Returns the struct syscalls_info if the file is valid, NULL otherwise. */
458c8db8 308static struct syscalls_info *
fbbe92c5
SDJ
309xml_init_syscalls_info (const char *filename)
310{
b7b030ad
TT
311 gdb::unique_xmalloc_ptr<char> full_file
312 = xml_fetch_content_from_file (filename, gdb_datadir);
fbbe92c5 313 if (full_file == NULL)
bccd0dd2 314 return NULL;
fbbe92c5 315
b7b030ad
TT
316 return syscall_parse_xml (full_file.get (),
317 xml_fetch_content_from_file,
318 (void *) ldirname (filename).c_str ());
fbbe92c5
SDJ
319}
320
321/* Initializes the syscalls_info structure according to the
322 architecture. */
323static void
458c8db8 324init_syscalls_info (struct gdbarch *gdbarch)
fbbe92c5 325{
458c8db8
SDJ
326 struct syscalls_info *syscalls_info = gdbarch_syscalls_info (gdbarch);
327 const char *xml_syscall_file = gdbarch_xml_syscall_file (gdbarch);
328
626ea16d 329 /* Should we re-read the XML info for this target? */
5a9dcda1
SM
330 if (syscalls_info != NULL && !syscalls_info->my_gdb_datadir.empty ()
331 && filename_cmp (syscalls_info->my_gdb_datadir.c_str (),
332 gdb_datadir) != 0)
626ea16d
SDJ
333 {
334 /* The data-directory changed from the last time we used it.
335 It means that we have to re-read the XML info. */
5a9dcda1 336 delete syscalls_info;
458c8db8
SDJ
337 syscalls_info = NULL;
338 set_gdbarch_syscalls_info (gdbarch, NULL);
626ea16d
SDJ
339 }
340
458c8db8
SDJ
341 /* Did we succeed at initializing this? */
342 if (syscalls_info != NULL)
fbbe92c5 343 return;
fbbe92c5 344
458c8db8 345 syscalls_info = xml_init_syscalls_info (xml_syscall_file);
fbbe92c5 346
458c8db8
SDJ
347 /* If there was some error reading the XML file, we initialize
348 gdbarch->syscalls_info anyway, in order to store information
349 about our attempt. */
350 if (syscalls_info == NULL)
5a9dcda1 351 syscalls_info = new struct syscalls_info ();
fbbe92c5 352
5a9dcda1 353 if (syscalls_info->syscalls.empty ())
fbbe92c5 354 {
458c8db8 355 if (xml_syscall_file != NULL)
3e43a32a 356 warning (_("Could not load the syscall XML file `%s/%s'."),
626ea16d 357 gdb_datadir, xml_syscall_file);
fbbe92c5 358 else
3e43a32a 359 warning (_("There is no XML file to open."));
bccd0dd2 360
3e43a32a
MS
361 warning (_("GDB will not be able to display "
362 "syscall names nor to verify if\n"
363 "any provided syscall numbers are valid."));
fbbe92c5 364 }
626ea16d
SDJ
365
366 /* Saving the data-directory used to read this XML info. */
5a9dcda1 367 syscalls_info->my_gdb_datadir.assign (gdb_datadir);
458c8db8
SDJ
368
369 set_gdbarch_syscalls_info (gdbarch, syscalls_info);
fbbe92c5
SDJ
370}
371
e3487908
GKB
372/* Search for a syscall group by its name. Return syscall_group_desc
373 structure for the group if found or NULL otherwise. */
374
375static struct syscall_group_desc *
376syscall_group_get_group_by_name (const struct syscalls_info *syscalls_info,
377 const char *group)
378{
e3487908
GKB
379 if (syscalls_info == NULL)
380 return NULL;
381
382 if (group == NULL)
383 return NULL;
384
5a9dcda1
SM
385 /* Search for existing group. */
386 for (const syscall_group_desc_up &groupdesc : syscalls_info->groups)
e3487908 387 {
5a9dcda1
SM
388 if (groupdesc->name == group)
389 return groupdesc.get ();
e3487908
GKB
390 }
391
392 return NULL;
393}
394
fbbe92c5 395static int
458c8db8 396xml_get_syscall_number (struct gdbarch *gdbarch,
fbbe92c5
SDJ
397 const char *syscall_name)
398{
458c8db8 399 struct syscalls_info *syscalls_info = gdbarch_syscalls_info (gdbarch);
fbbe92c5 400
458c8db8 401 if (syscalls_info == NULL
fbbe92c5
SDJ
402 || syscall_name == NULL)
403 return UNKNOWN_SYSCALL;
404
5a9dcda1
SM
405 for (const syscall_desc_up &sysdesc : syscalls_info->syscalls)
406 if (sysdesc->name == syscall_name)
fbbe92c5
SDJ
407 return sysdesc->number;
408
409 return UNKNOWN_SYSCALL;
410}
411
412static const char *
458c8db8 413xml_get_syscall_name (struct gdbarch *gdbarch,
fbbe92c5
SDJ
414 int syscall_number)
415{
458c8db8 416 struct syscalls_info *syscalls_info = gdbarch_syscalls_info (gdbarch);
fbbe92c5 417
458c8db8 418 if (syscalls_info == NULL
fbbe92c5
SDJ
419 || syscall_number < 0)
420 return NULL;
421
5a9dcda1 422 for (const syscall_desc_up &sysdesc : syscalls_info->syscalls)
fbbe92c5 423 if (sysdesc->number == syscall_number)
5a9dcda1 424 return sysdesc->name.c_str ();
fbbe92c5
SDJ
425
426 return NULL;
427}
428
fbbe92c5 429static const char **
458c8db8 430xml_list_of_syscalls (struct gdbarch *gdbarch)
fbbe92c5 431{
458c8db8 432 struct syscalls_info *syscalls_info = gdbarch_syscalls_info (gdbarch);
fbbe92c5 433
458c8db8 434 if (syscalls_info == NULL)
fbbe92c5
SDJ
435 return NULL;
436
5a9dcda1
SM
437 int nsyscalls = syscalls_info->syscalls.size ();
438 const char **names = XNEWVEC (const char *, nsyscalls + 1);
fbbe92c5 439
5a9dcda1
SM
440 int i;
441 for (i = 0; i < syscalls_info->syscalls.size (); i++)
442 names[i] = syscalls_info->syscalls[i]->name.c_str ();
fbbe92c5
SDJ
443
444 names[i] = NULL;
445
446 return names;
447}
448
e3487908
GKB
449/* Iterate over the syscall_group_desc element to return a list of
450 syscalls that are part of the given group, terminated by an empty
451 element. If the syscall group doesn't exist, return NULL. */
452
453static struct syscall *
454xml_list_syscalls_by_group (struct gdbarch *gdbarch, const char *group)
455{
456 struct syscalls_info *syscalls_info = gdbarch_syscalls_info (gdbarch);
457 struct syscall_group_desc *groupdesc;
458 struct syscall_desc *sysdesc;
459 struct syscall *syscalls = NULL;
460 int nsyscalls;
461 int i;
462
463 if (syscalls_info == NULL)
464 return NULL;
465
466 groupdesc = syscall_group_get_group_by_name (syscalls_info, group);
467 if (groupdesc == NULL)
468 return NULL;
469
5a9dcda1 470 nsyscalls = groupdesc->syscalls.size ();
e3487908
GKB
471 syscalls = (struct syscall*) xmalloc ((nsyscalls + 1)
472 * sizeof (struct syscall));
473
5a9dcda1 474 for (i = 0; i < groupdesc->syscalls.size (); i++)
e3487908 475 {
5a9dcda1
SM
476 syscalls[i].name = groupdesc->syscalls[i]->name.c_str ();
477 syscalls[i].number = groupdesc->syscalls[i]->number;
e3487908
GKB
478 }
479
480 /* Add final element marker. */
481 syscalls[i].name = NULL;
482 syscalls[i].number = 0;
483
484 return syscalls;
485}
486
487/* Return a NULL terminated list of syscall groups or an empty list, if
488 no syscall group is available. Return NULL, if there is no syscall
489 information available. */
490
491static const char **
492xml_list_of_groups (struct gdbarch *gdbarch)
493{
494 struct syscalls_info *syscalls_info = gdbarch_syscalls_info (gdbarch);
e3487908 495 const char **names = NULL;
e3487908 496 int ngroups;
5a9dcda1 497 int i;
e3487908
GKB
498
499 if (syscalls_info == NULL)
500 return NULL;
501
5a9dcda1 502 ngroups = syscalls_info->groups.size ();
e3487908
GKB
503 names = (const char**) xmalloc ((ngroups + 1) * sizeof (char *));
504
5a9dcda1
SM
505 for (i = 0; i < syscalls_info->groups.size (); i++)
506 names[i] = syscalls_info->groups[i]->name.c_str ();
e3487908
GKB
507
508 names[i] = NULL;
509
510 return names;
511}
512
fbbe92c5 513void
458c8db8 514set_xml_syscall_file_name (struct gdbarch *gdbarch, const char *name)
fbbe92c5 515{
458c8db8 516 set_gdbarch_xml_syscall_file (gdbarch, name);
fbbe92c5
SDJ
517}
518
519void
458c8db8
SDJ
520get_syscall_by_number (struct gdbarch *gdbarch,
521 int syscall_number, struct syscall *s)
fbbe92c5 522{
458c8db8 523 init_syscalls_info (gdbarch);
fbbe92c5
SDJ
524
525 s->number = syscall_number;
458c8db8 526 s->name = xml_get_syscall_name (gdbarch, syscall_number);
fbbe92c5
SDJ
527}
528
529void
458c8db8
SDJ
530get_syscall_by_name (struct gdbarch *gdbarch,
531 const char *syscall_name, struct syscall *s)
fbbe92c5 532{
458c8db8 533 init_syscalls_info (gdbarch);
fbbe92c5 534
458c8db8 535 s->number = xml_get_syscall_number (gdbarch, syscall_name);
fbbe92c5
SDJ
536 s->name = syscall_name;
537}
538
539const char **
458c8db8 540get_syscall_names (struct gdbarch *gdbarch)
fbbe92c5 541{
458c8db8 542 init_syscalls_info (gdbarch);
fbbe92c5 543
458c8db8 544 return xml_list_of_syscalls (gdbarch);
fbbe92c5
SDJ
545}
546
e3487908
GKB
547/* See comment in xml-syscall.h. */
548
549struct syscall *
550get_syscalls_by_group (struct gdbarch *gdbarch, const char *group)
551{
552 init_syscalls_info (gdbarch);
553
554 return xml_list_syscalls_by_group (gdbarch, group);
555}
556
557/* See comment in xml-syscall.h. */
558
559const char **
560get_syscall_group_names (struct gdbarch *gdbarch)
561{
562 init_syscalls_info (gdbarch);
563
564 return xml_list_of_groups (gdbarch);
565}
566
fbbe92c5 567#endif /* ! HAVE_LIBEXPAT */
This page took 1.057971 seconds and 4 git commands to generate.