]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame_incremental - gdb/xml-syscall.c
gas: introduce .errif and .warnif
[thirdparty/binutils-gdb.git] / gdb / xml-syscall.c
... / ...
CommitLineData
1/* Functions that provide the mechanism to parse a syscall XML file
2 and get its values.
3
4 Copyright (C) 2009-2025 Free Software Foundation, Inc.
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 "gdbtypes.h"
22#include "xml-support.h"
23#include "xml-syscall.h"
24#include "gdbarch.h"
25
26/* For the struct syscall definition. */
27#include "target.h"
28
29#include "filenames.h"
30
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
49set_xml_syscall_file_name (struct gdbarch *gdbarch, const char *name)
50{
51 return;
52}
53
54void
55get_syscall_by_number (struct gdbarch *gdbarch,
56 int syscall_number, struct syscall *s)
57{
58 syscall_warn_user ();
59 s->number = syscall_number;
60 s->name = NULL;
61}
62
63bool
64get_syscalls_by_name (struct gdbarch *gdbarch, const char *syscall_name,
65 std::vector<int> *syscall_numbers)
66{
67 syscall_warn_user ();
68 return false;
69}
70
71const char **
72get_syscall_names (struct gdbarch *gdbarch)
73{
74 syscall_warn_user ();
75 return NULL;
76}
77
78bool
79get_syscalls_by_group (struct gdbarch *gdbarch, const char *group,
80 std::vector<int> *syscall_numbers)
81{
82 syscall_warn_user ();
83 return false;
84}
85
86const char **
87get_syscall_group_names (struct gdbarch *gdbarch)
88{
89 syscall_warn_user ();
90 return NULL;
91}
92
93#else /* ! HAVE_LIBEXPAT */
94
95/* Structure which describes a syscall. */
96struct syscall_desc
97{
98 syscall_desc (int number_, std::string name_, std::string alias_)
99 : number (number_), name (name_), alias (alias_)
100 {}
101
102 /* The syscall number. */
103
104 int number;
105
106 /* The syscall name. */
107
108 std::string name;
109
110 /* An optional alias. */
111
112 std::string alias;
113};
114
115typedef std::unique_ptr<syscall_desc> syscall_desc_up;
116
117/* Structure of a syscall group. */
118struct syscall_group_desc
119{
120 syscall_group_desc (const std::string &name_)
121 : name (name_)
122 {}
123
124 /* The group name. */
125
126 std::string name;
127
128 /* The syscalls that are part of the group. This is a non-owning
129 reference. */
130
131 std::vector<syscall_desc *> syscalls;
132};
133
134typedef std::unique_ptr<syscall_group_desc> syscall_group_desc_up;
135
136/* Structure that represents syscalls information. */
137struct syscalls_info
138{
139 /* The syscalls. */
140
141 std::vector<syscall_desc_up> syscalls;
142
143 /* The syscall groups. */
144
145 std::vector<syscall_group_desc_up> groups;
146
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
151 std::string my_gdb_datadir;
152};
153
154typedef std::unique_ptr<syscalls_info> syscalls_info_up;
155
156/* Callback data for syscall information parsing. */
157struct syscall_parsing_data
158{
159 /* The syscalls_info we are building. */
160
161 struct syscalls_info *syscalls_info;
162};
163
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{
171 syscall_group_desc *groupdesc = new syscall_group_desc (group);
172
173 syscalls_info->groups.emplace_back (groupdesc);
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{
185 /* Search for an existing group. */
186 std::vector<syscall_group_desc_up>::iterator it
187 = syscalls_info->groups.begin ();
188
189 for (; it != syscalls_info->groups.end (); it++)
190 {
191 if ((*it)->name == group)
192 break;
193 }
194
195 syscall_group_desc *groupdesc;
196
197 if (it != syscalls_info->groups.end ())
198 groupdesc = it->get ();
199 else
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
207 groupdesc->syscalls.push_back (syscall);
208}
209
210static void
211syscall_create_syscall_desc (struct syscalls_info *syscalls_info,
212 const char *name, int number, const char *alias,
213 char *groups)
214{
215 syscall_desc *sysdesc = new syscall_desc (number, name,
216 alias != NULL ? alias : "");
217
218 syscalls_info->syscalls.emplace_back (sysdesc);
219
220 /* Add syscall to its groups. */
221 if (groups != NULL)
222 {
223 char *saveptr;
224 for (char *group = strtok_r (groups, ",", &saveptr);
225 group != NULL;
226 group = strtok_r (NULL, ",", &saveptr))
227 syscall_group_add_syscall (syscalls_info, sysdesc, group);
228 }
229}
230
231/* Handle the start of a <syscall> element. */
232static void
233syscall_start_syscall (struct gdb_xml_parser *parser,
234 const struct gdb_xml_element *element,
235 void *user_data,
236 std::vector<gdb_xml_value> &attributes)
237{
238 struct syscall_parsing_data *data = (struct syscall_parsing_data *) user_data;
239 /* syscall info. */
240 char *name = NULL;
241 int number = 0;
242 char *alias = NULL;
243 char *groups = NULL;
244
245 for (const gdb_xml_value &attr : attributes)
246 {
247 if (strcmp (attr.name, "name") == 0)
248 name = (char *) attr.value.get ();
249 else if (strcmp (attr.name, "number") == 0)
250 number = * (ULONGEST *) attr.value.get ();
251 else if (strcmp (attr.name, "alias") == 0)
252 alias = (char *) attr.value.get ();
253 else if (strcmp (attr.name, "groups") == 0)
254 groups = (char *) attr.value.get ();
255 else
256 internal_error (_("Unknown attribute name '%s'."), attr.name);
257 }
258
259 gdb_assert (name);
260 syscall_create_syscall_desc (data->syscalls_info, name, number, alias,
261 groups);
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 },
269 { "alias", GDB_XML_AF_OPTIONAL, NULL, NULL },
270 { "groups", GDB_XML_AF_OPTIONAL, NULL, NULL },
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,
283 GDB_XML_EF_NONE, NULL, NULL },
284 { NULL, NULL, NULL, GDB_XML_EF_NONE, NULL, NULL }
285};
286
287static struct syscalls_info *
288syscall_parse_xml (const char *document, xml_fetch_another fetcher)
289{
290 struct syscall_parsing_data data;
291 syscalls_info_up sysinfo (new syscalls_info ());
292
293 data.syscalls_info = sysinfo.get ();
294
295 if (gdb_xml_parse_quick (_("syscalls info"), NULL,
296 syselements, document, &data) == 0)
297 {
298 /* Parsed successfully. */
299 return sysinfo.release ();
300 }
301 else
302 {
303 warning (_("Could not load XML syscalls info; ignoring"));
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. */
313static struct syscalls_info *
314xml_init_syscalls_info (const char *filename)
315{
316 std::optional<gdb::char_vector> full_file
317 = xml_fetch_content_from_file (filename,
318 const_cast<char *>(gdb_datadir.c_str ()));
319 if (!full_file)
320 return NULL;
321
322 const std::string dirname = gdb_ldirname (filename);
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);
329}
330
331/* Initializes the syscalls_info structure according to the
332 architecture. */
333static void
334init_syscalls_info (struct gdbarch *gdbarch)
335{
336 struct syscalls_info *syscalls_info = gdbarch_syscalls_info (gdbarch);
337 const char *xml_syscall_file = gdbarch_xml_syscall_file (gdbarch);
338
339 /* Should we re-read the XML info for this target? */
340 if (syscalls_info != NULL && !syscalls_info->my_gdb_datadir.empty ()
341 && filename_cmp (syscalls_info->my_gdb_datadir.c_str (),
342 gdb_datadir.c_str ()) != 0)
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. */
346 delete syscalls_info;
347 syscalls_info = NULL;
348 set_gdbarch_syscalls_info (gdbarch, NULL);
349 }
350
351 /* Did we succeed at initializing this? */
352 if (syscalls_info != NULL)
353 return;
354
355 syscalls_info = xml_init_syscalls_info (xml_syscall_file);
356
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)
361 syscalls_info = new struct syscalls_info ();
362
363 if (syscalls_info->syscalls.empty ())
364 {
365 if (xml_syscall_file != NULL)
366 warning (_("Could not load the syscall XML file `%s/%s'."),
367 gdb_datadir.c_str (), xml_syscall_file);
368 else
369 warning (_("There is no XML file to open."));
370
371 warning (_("GDB will not be able to display "
372 "syscall names nor to verify if\n"
373 "any provided syscall numbers are valid."));
374 }
375
376 /* Saving the data-directory used to read this XML info. */
377 syscalls_info->my_gdb_datadir.assign (gdb_datadir);
378
379 set_gdbarch_syscalls_info (gdbarch, syscalls_info);
380}
381
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{
389 if (syscalls_info == NULL)
390 return NULL;
391
392 if (group == NULL)
393 return NULL;
394
395 /* Search for existing group. */
396 for (const syscall_group_desc_up &groupdesc : syscalls_info->groups)
397 {
398 if (groupdesc->name == group)
399 return groupdesc.get ();
400 }
401
402 return NULL;
403}
404
405static bool
406xml_get_syscalls_by_name (struct gdbarch *gdbarch, const char *syscall_name,
407 std::vector<int> *syscall_numbers)
408{
409 struct syscalls_info *syscalls_info = gdbarch_syscalls_info (gdbarch);
410
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 }
419
420 return found;
421}
422
423static const char *
424xml_get_syscall_name (struct gdbarch *gdbarch,
425 int syscall_number)
426{
427 struct syscalls_info *syscalls_info = gdbarch_syscalls_info (gdbarch);
428
429 if (syscalls_info == NULL
430 || syscall_number < 0)
431 return NULL;
432
433 for (const syscall_desc_up &sysdesc : syscalls_info->syscalls)
434 if (sysdesc->number == syscall_number)
435 return sysdesc->name.c_str ();
436
437 return NULL;
438}
439
440static const char **
441xml_list_of_syscalls (struct gdbarch *gdbarch)
442{
443 struct syscalls_info *syscalls_info = gdbarch_syscalls_info (gdbarch);
444
445 if (syscalls_info == NULL)
446 return NULL;
447
448 int nsyscalls = syscalls_info->syscalls.size ();
449 const char **names = XNEWVEC (const char *, nsyscalls + 1);
450
451 int i;
452 for (i = 0; i < syscalls_info->syscalls.size (); i++)
453 names[i] = syscalls_info->syscalls[i]->name.c_str ();
454
455 names[i] = NULL;
456
457 return names;
458}
459
460/* Iterate over the syscall_group_desc element to return a list of
461 syscalls that are part of the given group. If the syscall group
462 doesn't exist, return false. */
463
464static bool
465xml_list_syscalls_by_group (struct gdbarch *gdbarch, const char *group,
466 std::vector<int> *syscalls)
467{
468 struct syscalls_info *syscalls_info = gdbarch_syscalls_info (gdbarch);
469 struct syscall_group_desc *groupdesc;
470
471 if (syscalls_info == NULL || syscalls == NULL)
472 return false;
473
474 groupdesc = syscall_group_get_group_by_name (syscalls_info, group);
475 if (groupdesc == NULL)
476 return false;
477
478 for (const syscall_desc *sysdesc : groupdesc->syscalls)
479 syscalls->push_back (sysdesc->number);
480
481 return true;
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);
492 const char **names = NULL;
493 int ngroups;
494 int i;
495
496 if (syscalls_info == NULL)
497 return NULL;
498
499 ngroups = syscalls_info->groups.size ();
500 names = (const char**) xmalloc ((ngroups + 1) * sizeof (char *));
501
502 for (i = 0; i < syscalls_info->groups.size (); i++)
503 names[i] = syscalls_info->groups[i]->name.c_str ();
504
505 names[i] = NULL;
506
507 return names;
508}
509
510void
511set_xml_syscall_file_name (struct gdbarch *gdbarch, const char *name)
512{
513 set_gdbarch_xml_syscall_file (gdbarch, name);
514}
515
516void
517get_syscall_by_number (struct gdbarch *gdbarch,
518 int syscall_number, struct syscall *s)
519{
520 init_syscalls_info (gdbarch);
521
522 s->number = syscall_number;
523 s->name = xml_get_syscall_name (gdbarch, syscall_number);
524}
525
526bool
527get_syscalls_by_name (struct gdbarch *gdbarch, const char *syscall_name,
528 std::vector<int> *syscall_numbers)
529{
530 init_syscalls_info (gdbarch);
531
532 return xml_get_syscalls_by_name (gdbarch, syscall_name, syscall_numbers);
533}
534
535const char **
536get_syscall_names (struct gdbarch *gdbarch)
537{
538 init_syscalls_info (gdbarch);
539
540 return xml_list_of_syscalls (gdbarch);
541}
542
543/* See comment in xml-syscall.h. */
544
545bool
546get_syscalls_by_group (struct gdbarch *gdbarch, const char *group,
547 std::vector<int> *syscall_numbers)
548{
549 init_syscalls_info (gdbarch);
550
551 return xml_list_syscalls_by_group (gdbarch, group, syscall_numbers);
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
564#endif /* ! HAVE_LIBEXPAT */