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