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