gdb: add target_ops::supports_displaced_step
[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
b811d2c2 4 Copyright (C) 2009-2020 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
e9076973
JB
64bool
65get_syscalls_by_name (struct gdbarch *gdbarch, const char *syscall_name,
66 std::vector<int> *syscall_numbers)
fbbe92c5
SDJ
67{
68 syscall_warn_user ();
e9076973 69 return false;
fbbe92c5
SDJ
70}
71
72const char **
7e7cbeb3 73get_syscall_names (struct gdbarch *gdbarch)
fbbe92c5
SDJ
74{
75 syscall_warn_user ();
76 return NULL;
77}
78
4794efbf
JB
79bool
80get_syscalls_by_group (struct gdbarch *gdbarch, const char *group,
81 std::vector<int> *syscall_numbers)
e3487908
GKB
82{
83 syscall_warn_user ();
4794efbf 84 return false;
e3487908
GKB
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{
e9076973
JB
99 syscall_desc (int number_, std::string name_, std::string alias_)
100 : number (number_), name (name_), alias (alias_)
5a9dcda1
SM
101 {}
102
fbbe92c5
SDJ
103 /* The syscall number. */
104
105 int number;
106
107 /* The syscall name. */
108
5a9dcda1 109 std::string name;
e9076973
JB
110
111 /* An optional alias. */
112
113 std::string alias;
5a9dcda1
SM
114};
115
116typedef std::unique_ptr<syscall_desc> syscall_desc_up;
fbbe92c5 117
e3487908 118/* Structure of a syscall group. */
5a9dcda1 119struct syscall_group_desc
e3487908 120{
5a9dcda1
SM
121 syscall_group_desc (const std::string &name_)
122 : name (name_)
123 {}
124
e3487908
GKB
125 /* The group name. */
126
5a9dcda1 127 std::string name;
e3487908 128
5a9dcda1
SM
129 /* The syscalls that are part of the group. This is a non-owning
130 reference. */
e3487908 131
5a9dcda1
SM
132 std::vector<syscall_desc *> syscalls;
133};
134
135typedef std::unique_ptr<syscall_group_desc> syscall_group_desc_up;
e3487908 136
fbbe92c5
SDJ
137/* Structure that represents syscalls information. */
138struct syscalls_info
139{
140 /* The syscalls. */
141
5a9dcda1 142 std::vector<syscall_desc_up> syscalls;
458c8db8 143
e3487908
GKB
144 /* The syscall groups. */
145
5a9dcda1 146 std::vector<syscall_group_desc_up> groups;
e3487908 147
458c8db8
SDJ
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
150 target. */
151
5a9dcda1 152 std::string my_gdb_datadir;
fbbe92c5
SDJ
153};
154
5a9dcda1
SM
155typedef std::unique_ptr<syscalls_info> syscalls_info_up;
156
fbbe92c5
SDJ
157/* Callback data for syscall information parsing. */
158struct syscall_parsing_data
159{
160 /* The syscalls_info we are building. */
161
458c8db8 162 struct syscalls_info *syscalls_info;
fbbe92c5
SDJ
163};
164
e3487908
GKB
165/* Create a new syscall group. Return pointer to the
166 syscall_group_desc structure that represents the new group. */
167
168static struct syscall_group_desc *
169syscall_group_create_syscall_group_desc (struct syscalls_info *syscalls_info,
170 const char *group)
171{
5a9dcda1 172 syscall_group_desc *groupdesc = new syscall_group_desc (group);
e3487908 173
5a9dcda1 174 syscalls_info->groups.emplace_back (groupdesc);
e3487908
GKB
175
176 return groupdesc;
177}
178
179/* Add a syscall to the group. If group doesn't exist, create it. */
180
181static void
182syscall_group_add_syscall (struct syscalls_info *syscalls_info,
183 struct syscall_desc *syscall,
184 const char *group)
185{
e3487908 186 /* Search for an existing group. */
5a9dcda1
SM
187 std::vector<syscall_group_desc_up>::iterator it
188 = syscalls_info->groups.begin ();
189
190 for (; it != syscalls_info->groups.end (); it++)
e3487908 191 {
5a9dcda1 192 if ((*it)->name == group)
e3487908
GKB
193 break;
194 }
195
5a9dcda1
SM
196 syscall_group_desc *groupdesc;
197
198 if (it != syscalls_info->groups.end ())
199 groupdesc = it->get ();
200 else
e3487908
GKB
201 {
202 /* No group was found with this name. We must create a new
203 one. */
204 groupdesc = syscall_group_create_syscall_group_desc (syscalls_info,
205 group);
206 }
207
5a9dcda1 208 groupdesc->syscalls.push_back (syscall);
e3487908
GKB
209}
210
fbbe92c5 211static void
458c8db8 212syscall_create_syscall_desc (struct syscalls_info *syscalls_info,
e9076973 213 const char *name, int number, const char *alias,
e3487908 214 char *groups)
fbbe92c5 215{
e9076973
JB
216 syscall_desc *sysdesc = new syscall_desc (number, name,
217 alias != NULL ? alias : "");
fbbe92c5 218
5a9dcda1 219 syscalls_info->syscalls.emplace_back (sysdesc);
e3487908
GKB
220
221 /* Add syscall to its groups. */
222 if (groups != NULL)
223 {
ca3a04f6
CB
224 char *saveptr;
225 for (char *group = strtok_r (groups, ",", &saveptr);
e3487908 226 group != NULL;
ca3a04f6 227 group = strtok_r (NULL, ",", &saveptr))
e3487908
GKB
228 syscall_group_add_syscall (syscalls_info, sysdesc, group);
229 }
fbbe92c5
SDJ
230}
231
fbbe92c5
SDJ
232/* Handle the start of a <syscall> element. */
233static void
234syscall_start_syscall (struct gdb_xml_parser *parser,
235 const struct gdb_xml_element *element,
4d0fdd9b
SM
236 void *user_data,
237 std::vector<gdb_xml_value> &attributes)
fbbe92c5 238{
19ba03f4 239 struct syscall_parsing_data *data = (struct syscall_parsing_data *) user_data;
fbbe92c5
SDJ
240 /* syscall info. */
241 char *name = NULL;
242 int number = 0;
e9076973 243 char *alias = NULL;
e3487908 244 char *groups = NULL;
fbbe92c5 245
4d0fdd9b 246 for (const gdb_xml_value &attr : attributes)
fbbe92c5 247 {
4d0fdd9b
SM
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 ();
e9076973
JB
252 else if (strcmp (attr.name, "alias") == 0)
253 alias = (char *) attr.value.get ();
4d0fdd9b
SM
254 else if (strcmp (attr.name, "groups") == 0)
255 groups = (char *) attr.value.get ();
fbbe92c5
SDJ
256 else
257 internal_error (__FILE__, __LINE__,
4d0fdd9b 258 _("Unknown attribute name '%s'."), attr.name);
fbbe92c5
SDJ
259 }
260
154f592e 261 gdb_assert (name);
e9076973
JB
262 syscall_create_syscall_desc (data->syscalls_info, name, number, alias,
263 groups);
fbbe92c5
SDJ
264}
265
266
267/* The elements and attributes of an XML syscall document. */
268static 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 },
e9076973 271 { "alias", GDB_XML_AF_OPTIONAL, NULL, NULL },
e3487908 272 { "groups", GDB_XML_AF_OPTIONAL, NULL, NULL },
fbbe92c5
SDJ
273 { NULL, GDB_XML_AF_NONE, NULL, NULL }
274};
275
276static 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 }
281};
282
283static const struct gdb_xml_element syselements[] = {
284 { "syscalls_info", NULL, syscalls_info_children,
f8e50cfe 285 GDB_XML_EF_NONE, NULL, NULL },
fbbe92c5
SDJ
286 { NULL, NULL, NULL, GDB_XML_EF_NONE, NULL, NULL }
287};
288
289static struct syscalls_info *
290syscall_parse_xml (const char *document, xml_fetch_another fetcher,
291 void *fetcher_baton)
292{
fbbe92c5 293 struct syscall_parsing_data data;
5a9dcda1 294 syscalls_info_up sysinfo (new syscalls_info ());
fbbe92c5 295
5a9dcda1 296 data.syscalls_info = sysinfo.get ();
fbbe92c5 297
efc0eabd
PA
298 if (gdb_xml_parse_quick (_("syscalls info"), NULL,
299 syselements, document, &data) == 0)
fbbe92c5
SDJ
300 {
301 /* Parsed successfully. */
5a9dcda1 302 return sysinfo.release ();
fbbe92c5
SDJ
303 }
304 else
305 {
306 warning (_("Could not load XML syscalls info; ignoring"));
fbbe92c5
SDJ
307 return NULL;
308 }
309}
310
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.
314
315 Returns the struct syscalls_info if the file is valid, NULL otherwise. */
458c8db8 316static struct syscalls_info *
fbbe92c5
SDJ
317xml_init_syscalls_info (const char *filename)
318{
9018be22 319 gdb::optional<gdb::char_vector> full_file
f2aec7f6
CB
320 = xml_fetch_content_from_file (filename,
321 const_cast<char *>(gdb_datadir.c_str ()));
9018be22 322 if (!full_file)
bccd0dd2 323 return NULL;
fbbe92c5 324
9018be22 325 return syscall_parse_xml (full_file->data (),
b7b030ad
TT
326 xml_fetch_content_from_file,
327 (void *) ldirname (filename).c_str ());
fbbe92c5
SDJ
328}
329
330/* Initializes the syscalls_info structure according to the
331 architecture. */
332static void
458c8db8 333init_syscalls_info (struct gdbarch *gdbarch)
fbbe92c5 334{
458c8db8
SDJ
335 struct syscalls_info *syscalls_info = gdbarch_syscalls_info (gdbarch);
336 const char *xml_syscall_file = gdbarch_xml_syscall_file (gdbarch);
337
626ea16d 338 /* Should we re-read the XML info for this target? */
5a9dcda1
SM
339 if (syscalls_info != NULL && !syscalls_info->my_gdb_datadir.empty ()
340 && filename_cmp (syscalls_info->my_gdb_datadir.c_str (),
f2aec7f6 341 gdb_datadir.c_str ()) != 0)
626ea16d
SDJ
342 {
343 /* The data-directory changed from the last time we used it.
344 It means that we have to re-read the XML info. */
5a9dcda1 345 delete syscalls_info;
458c8db8
SDJ
346 syscalls_info = NULL;
347 set_gdbarch_syscalls_info (gdbarch, NULL);
626ea16d
SDJ
348 }
349
458c8db8
SDJ
350 /* Did we succeed at initializing this? */
351 if (syscalls_info != NULL)
fbbe92c5 352 return;
fbbe92c5 353
458c8db8 354 syscalls_info = xml_init_syscalls_info (xml_syscall_file);
fbbe92c5 355
458c8db8
SDJ
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)
5a9dcda1 360 syscalls_info = new struct syscalls_info ();
fbbe92c5 361
5a9dcda1 362 if (syscalls_info->syscalls.empty ())
fbbe92c5 363 {
458c8db8 364 if (xml_syscall_file != NULL)
3e43a32a 365 warning (_("Could not load the syscall XML file `%s/%s'."),
f2aec7f6 366 gdb_datadir.c_str (), xml_syscall_file);
fbbe92c5 367 else
3e43a32a 368 warning (_("There is no XML file to open."));
bccd0dd2 369
3e43a32a
MS
370 warning (_("GDB will not be able to display "
371 "syscall names nor to verify if\n"
372 "any provided syscall numbers are valid."));
fbbe92c5 373 }
626ea16d
SDJ
374
375 /* Saving the data-directory used to read this XML info. */
5a9dcda1 376 syscalls_info->my_gdb_datadir.assign (gdb_datadir);
458c8db8
SDJ
377
378 set_gdbarch_syscalls_info (gdbarch, syscalls_info);
fbbe92c5
SDJ
379}
380
e3487908
GKB
381/* Search for a syscall group by its name. Return syscall_group_desc
382 structure for the group if found or NULL otherwise. */
383
384static struct syscall_group_desc *
385syscall_group_get_group_by_name (const struct syscalls_info *syscalls_info,
386 const char *group)
387{
e3487908
GKB
388 if (syscalls_info == NULL)
389 return NULL;
390
391 if (group == NULL)
392 return NULL;
393
5a9dcda1
SM
394 /* Search for existing group. */
395 for (const syscall_group_desc_up &groupdesc : syscalls_info->groups)
e3487908 396 {
5a9dcda1
SM
397 if (groupdesc->name == group)
398 return groupdesc.get ();
e3487908
GKB
399 }
400
401 return NULL;
402}
403
e9076973
JB
404static bool
405xml_get_syscalls_by_name (struct gdbarch *gdbarch, const char *syscall_name,
406 std::vector<int> *syscall_numbers)
fbbe92c5 407{
458c8db8 408 struct syscalls_info *syscalls_info = gdbarch_syscalls_info (gdbarch);
fbbe92c5 409
e9076973
JB
410 bool found = false;
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)
414 {
415 syscall_numbers->push_back (sysdesc->number);
416 found = true;
417 }
fbbe92c5 418
e9076973 419 return found;
fbbe92c5
SDJ
420}
421
422static const char *
458c8db8 423xml_get_syscall_name (struct gdbarch *gdbarch,
fbbe92c5
SDJ
424 int syscall_number)
425{
458c8db8 426 struct syscalls_info *syscalls_info = gdbarch_syscalls_info (gdbarch);
fbbe92c5 427
458c8db8 428 if (syscalls_info == NULL
fbbe92c5
SDJ
429 || syscall_number < 0)
430 return NULL;
431
5a9dcda1 432 for (const syscall_desc_up &sysdesc : syscalls_info->syscalls)
fbbe92c5 433 if (sysdesc->number == syscall_number)
5a9dcda1 434 return sysdesc->name.c_str ();
fbbe92c5
SDJ
435
436 return NULL;
437}
438
fbbe92c5 439static const char **
458c8db8 440xml_list_of_syscalls (struct gdbarch *gdbarch)
fbbe92c5 441{
458c8db8 442 struct syscalls_info *syscalls_info = gdbarch_syscalls_info (gdbarch);
fbbe92c5 443
458c8db8 444 if (syscalls_info == NULL)
fbbe92c5
SDJ
445 return NULL;
446
5a9dcda1
SM
447 int nsyscalls = syscalls_info->syscalls.size ();
448 const char **names = XNEWVEC (const char *, nsyscalls + 1);
fbbe92c5 449
5a9dcda1
SM
450 int i;
451 for (i = 0; i < syscalls_info->syscalls.size (); i++)
452 names[i] = syscalls_info->syscalls[i]->name.c_str ();
fbbe92c5
SDJ
453
454 names[i] = NULL;
455
456 return names;
457}
458
e3487908 459/* Iterate over the syscall_group_desc element to return a list of
4794efbf
JB
460 syscalls that are part of the given group. If the syscall group
461 doesn't exist, return false. */
e3487908 462
4794efbf
JB
463static bool
464xml_list_syscalls_by_group (struct gdbarch *gdbarch, const char *group,
465 std::vector<int> *syscalls)
e3487908
GKB
466{
467 struct syscalls_info *syscalls_info = gdbarch_syscalls_info (gdbarch);
468 struct syscall_group_desc *groupdesc;
e3487908 469
4794efbf
JB
470 if (syscalls_info == NULL || syscalls == NULL)
471 return false;
e3487908
GKB
472
473 groupdesc = syscall_group_get_group_by_name (syscalls_info, group);
474 if (groupdesc == NULL)
4794efbf 475 return false;
e3487908 476
a6a4b2c6 477 for (const syscall_desc *sysdesc : groupdesc->syscalls)
4794efbf 478 syscalls->push_back (sysdesc->number);
e3487908 479
4794efbf 480 return true;
e3487908
GKB
481}
482
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. */
486
487static const char **
488xml_list_of_groups (struct gdbarch *gdbarch)
489{
490 struct syscalls_info *syscalls_info = gdbarch_syscalls_info (gdbarch);
e3487908 491 const char **names = NULL;
e3487908 492 int ngroups;
5a9dcda1 493 int i;
e3487908
GKB
494
495 if (syscalls_info == NULL)
496 return NULL;
497
5a9dcda1 498 ngroups = syscalls_info->groups.size ();
e3487908
GKB
499 names = (const char**) xmalloc ((ngroups + 1) * sizeof (char *));
500
5a9dcda1
SM
501 for (i = 0; i < syscalls_info->groups.size (); i++)
502 names[i] = syscalls_info->groups[i]->name.c_str ();
e3487908
GKB
503
504 names[i] = NULL;
505
506 return names;
507}
508
fbbe92c5 509void
458c8db8 510set_xml_syscall_file_name (struct gdbarch *gdbarch, const char *name)
fbbe92c5 511{
458c8db8 512 set_gdbarch_xml_syscall_file (gdbarch, name);
fbbe92c5
SDJ
513}
514
515void
458c8db8
SDJ
516get_syscall_by_number (struct gdbarch *gdbarch,
517 int syscall_number, struct syscall *s)
fbbe92c5 518{
458c8db8 519 init_syscalls_info (gdbarch);
fbbe92c5
SDJ
520
521 s->number = syscall_number;
458c8db8 522 s->name = xml_get_syscall_name (gdbarch, syscall_number);
fbbe92c5
SDJ
523}
524
e9076973
JB
525bool
526get_syscalls_by_name (struct gdbarch *gdbarch, const char *syscall_name,
527 std::vector<int> *syscall_numbers)
fbbe92c5 528{
458c8db8 529 init_syscalls_info (gdbarch);
fbbe92c5 530
e9076973 531 return xml_get_syscalls_by_name (gdbarch, syscall_name, syscall_numbers);
fbbe92c5
SDJ
532}
533
534const char **
458c8db8 535get_syscall_names (struct gdbarch *gdbarch)
fbbe92c5 536{
458c8db8 537 init_syscalls_info (gdbarch);
fbbe92c5 538
458c8db8 539 return xml_list_of_syscalls (gdbarch);
fbbe92c5
SDJ
540}
541
e3487908
GKB
542/* See comment in xml-syscall.h. */
543
4794efbf
JB
544bool
545get_syscalls_by_group (struct gdbarch *gdbarch, const char *group,
546 std::vector<int> *syscall_numbers)
e3487908
GKB
547{
548 init_syscalls_info (gdbarch);
549
4794efbf 550 return xml_list_syscalls_by_group (gdbarch, group, syscall_numbers);
e3487908
GKB
551}
552
553/* See comment in xml-syscall.h. */
554
555const char **
556get_syscall_group_names (struct gdbarch *gdbarch)
557{
558 init_syscalls_info (gdbarch);
559
560 return xml_list_of_groups (gdbarch);
561}
562
fbbe92c5 563#endif /* ! HAVE_LIBEXPAT */
This page took 1.336132 seconds and 4 git commands to generate.