xref: /netbsd-src/external/gpl3/gdb.old/dist/gdb/xml-syscall.c (revision 154bfe8e089c1a0a4e9ed8414f08d3da90949162)
1 /* Functions that provide the mechanism to parse a syscall XML file
2    and get its values.
3 
4    Copyright (C) 2009-2017 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 void
65 get_syscall_by_name (struct gdbarch *gdbarch, const char *syscall_name,
66 		     struct syscall *s)
67 {
68   syscall_warn_user ();
69   s->number = UNKNOWN_SYSCALL;
70   s->name = syscall_name;
71 }
72 
73 const char **
74 get_syscall_names (struct gdbarch *gdbarch)
75 {
76   syscall_warn_user ();
77   return NULL;
78 }
79 
80 struct syscall *
81 get_syscalls_by_group (struct gdbarch *gdbarch, const char *group)
82 {
83   syscall_warn_user ();
84   return NULL;
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 typedef struct syscall_desc
98 {
99   /* The syscall number.  */
100 
101   int number;
102 
103   /* The syscall name.  */
104 
105   char *name;
106 } *syscall_desc_p;
107 DEF_VEC_P(syscall_desc_p);
108 
109 /* Structure of a syscall group.  */
110 typedef struct syscall_group_desc
111 {
112   /* The group name.  */
113 
114   char *name;
115 
116   /* The syscalls that are part of the group.  */
117 
118   VEC(syscall_desc_p) *syscalls;
119 } *syscall_group_desc_p;
120 DEF_VEC_P(syscall_group_desc_p);
121 
122 /* Structure that represents syscalls information.  */
123 struct syscalls_info
124 {
125   /* The syscalls.  */
126 
127   VEC(syscall_desc_p) *syscalls;
128 
129   /* The syscall groups.  */
130 
131   VEC(syscall_group_desc_p) *groups;
132 
133   /* Variable that will hold the last known data-directory.  This is
134      useful to know whether we should re-read the XML info for the
135      target.  */
136 
137   char *my_gdb_datadir;
138 };
139 
140 /* Callback data for syscall information parsing.  */
141 struct syscall_parsing_data
142 {
143   /* The syscalls_info we are building.  */
144 
145   struct syscalls_info *syscalls_info;
146 };
147 
148 static struct syscalls_info *
149 allocate_syscalls_info (void)
150 {
151   return XCNEW (struct syscalls_info);
152 }
153 
154 static void
155 syscalls_info_free_syscalls_desc (struct syscall_desc *sd)
156 {
157   xfree (sd->name);
158 }
159 
160 /* Free syscall_group_desc members but not the structure itself.  */
161 
162 static void
163 syscalls_info_free_syscall_group_desc (struct syscall_group_desc *sd)
164 {
165   VEC_free (syscall_desc_p, sd->syscalls);
166   xfree (sd->name);
167 }
168 
169 static void
170 free_syscalls_info (void *arg)
171 {
172   struct syscalls_info *syscalls_info = (struct syscalls_info *) arg;
173   struct syscall_desc *sysdesc;
174   struct syscall_group_desc *groupdesc;
175   int i;
176 
177   xfree (syscalls_info->my_gdb_datadir);
178 
179   if (syscalls_info->syscalls != NULL)
180     {
181       for (i = 0;
182 	   VEC_iterate (syscall_desc_p, syscalls_info->syscalls, i, sysdesc);
183 	   i++)
184 	syscalls_info_free_syscalls_desc (sysdesc);
185       VEC_free (syscall_desc_p, syscalls_info->syscalls);
186     }
187 
188   if (syscalls_info->groups != NULL)
189     {
190       for (i = 0;
191 	   VEC_iterate (syscall_group_desc_p,
192 			syscalls_info->groups, i, groupdesc);
193 	   i++)
194 	syscalls_info_free_syscall_group_desc (groupdesc);
195 
196       VEC_free (syscall_group_desc_p, syscalls_info->groups);
197     }
198 
199   xfree (syscalls_info);
200 }
201 
202 static struct cleanup *
203 make_cleanup_free_syscalls_info (struct syscalls_info *syscalls_info)
204 {
205   return make_cleanup (free_syscalls_info, syscalls_info);
206 }
207 
208 /* Create a new syscall group.  Return pointer to the
209    syscall_group_desc structure that represents the new group.  */
210 
211 static struct syscall_group_desc *
212 syscall_group_create_syscall_group_desc (struct syscalls_info *syscalls_info,
213 					 const char *group)
214 {
215   struct syscall_group_desc *groupdesc = XCNEW (struct syscall_group_desc);
216 
217   groupdesc->name = xstrdup (group);
218 
219   VEC_safe_push (syscall_group_desc_p, syscalls_info->groups, groupdesc);
220 
221   return groupdesc;
222 }
223 
224 /* Add a syscall to the group.  If group doesn't exist, create it.  */
225 
226 static void
227 syscall_group_add_syscall (struct syscalls_info *syscalls_info,
228 			   struct syscall_desc *syscall,
229 			   const char *group)
230 {
231   struct syscall_group_desc *groupdesc = NULL;
232   int i;
233 
234   /* Search for an existing group.  */
235   for (i = 0;
236        VEC_iterate (syscall_group_desc_p, syscalls_info->groups, i, groupdesc);
237        i++)
238     {
239       if (strcmp (groupdesc->name, group) == 0)
240 	break;
241     }
242 
243   if (groupdesc == NULL)
244     {
245       /* No group was found with this name.  We must create a new
246 	 one.  */
247       groupdesc = syscall_group_create_syscall_group_desc (syscalls_info,
248 							   group);
249     }
250 
251   VEC_safe_push (syscall_desc_p, groupdesc->syscalls, syscall);
252 }
253 
254 static void
255 syscall_create_syscall_desc (struct syscalls_info *syscalls_info,
256 			     const char *name, int number,
257 			     char *groups)
258 {
259   struct syscall_desc *sysdesc = XCNEW (struct syscall_desc);
260   char *group;
261 
262   sysdesc->name = xstrdup (name);
263   sysdesc->number = number;
264 
265   VEC_safe_push (syscall_desc_p, syscalls_info->syscalls, sysdesc);
266 
267   /*  Add syscall to its groups.  */
268   if (groups != NULL)
269     {
270       for (group = strtok (groups, ",");
271 	   group != NULL;
272 	   group = strtok (NULL, ","))
273 	syscall_group_add_syscall (syscalls_info, sysdesc, group);
274     }
275 }
276 
277 /* Handle the start of a <syscall> element.  */
278 static void
279 syscall_start_syscall (struct gdb_xml_parser *parser,
280                        const struct gdb_xml_element *element,
281                        void *user_data, VEC(gdb_xml_value_s) *attributes)
282 {
283   struct syscall_parsing_data *data = (struct syscall_parsing_data *) user_data;
284   struct gdb_xml_value *attrs = VEC_address (gdb_xml_value_s, attributes);
285   int len, i;
286   /* syscall info.  */
287   char *name = NULL;
288   int number = 0;
289   char *groups = NULL;
290 
291   len = VEC_length (gdb_xml_value_s, attributes);
292 
293   for (i = 0; i < len; i++)
294     {
295       if (strcmp (attrs[i].name, "name") == 0)
296         name = (char *) attrs[i].value;
297       else if (strcmp (attrs[i].name, "number") == 0)
298         number = * (ULONGEST *) attrs[i].value;
299       else if (strcmp (attrs[i].name, "groups") == 0)
300         groups = (char *) attrs[i].value;
301       else
302         internal_error (__FILE__, __LINE__,
303                         _("Unknown attribute name '%s'."), attrs[i].name);
304     }
305 
306   gdb_assert (name);
307   syscall_create_syscall_desc (data->syscalls_info, name, number, groups);
308 }
309 
310 
311 /* The elements and attributes of an XML syscall document.  */
312 static const struct gdb_xml_attribute syscall_attr[] = {
313   { "number", GDB_XML_AF_NONE, gdb_xml_parse_attr_ulongest, NULL },
314   { "name", GDB_XML_AF_NONE, NULL, NULL },
315   { "groups", GDB_XML_AF_OPTIONAL, NULL, NULL },
316   { NULL, GDB_XML_AF_NONE, NULL, NULL }
317 };
318 
319 static const struct gdb_xml_element syscalls_info_children[] = {
320   { "syscall", syscall_attr, NULL,
321     GDB_XML_EF_OPTIONAL | GDB_XML_EF_REPEATABLE,
322     syscall_start_syscall, NULL },
323   { NULL, NULL, NULL, GDB_XML_EF_NONE, NULL, NULL }
324 };
325 
326 static const struct gdb_xml_element syselements[] = {
327   { "syscalls_info", NULL, syscalls_info_children,
328     GDB_XML_EF_NONE, NULL, NULL },
329   { NULL, NULL, NULL, GDB_XML_EF_NONE, NULL, NULL }
330 };
331 
332 static struct syscalls_info *
333 syscall_parse_xml (const char *document, xml_fetch_another fetcher,
334                    void *fetcher_baton)
335 {
336   struct cleanup *result_cleanup;
337   struct syscall_parsing_data data;
338 
339   data.syscalls_info = allocate_syscalls_info ();
340   result_cleanup = make_cleanup_free_syscalls_info (data.syscalls_info);
341 
342   if (gdb_xml_parse_quick (_("syscalls info"), NULL,
343 			   syselements, document, &data) == 0)
344     {
345       /* Parsed successfully.  */
346       discard_cleanups (result_cleanup);
347       return data.syscalls_info;
348     }
349   else
350     {
351       warning (_("Could not load XML syscalls info; ignoring"));
352       do_cleanups (result_cleanup);
353       return NULL;
354     }
355 }
356 
357 /* Function responsible for initializing the information
358    about the syscalls.  It reads the XML file and fills the
359    struct syscalls_info with the values.
360 
361    Returns the struct syscalls_info if the file is valid, NULL otherwise.  */
362 static struct syscalls_info *
363 xml_init_syscalls_info (const char *filename)
364 {
365   char *full_file;
366   struct syscalls_info *syscalls_info;
367   struct cleanup *back_to;
368 
369   full_file = xml_fetch_content_from_file (filename, gdb_datadir);
370   if (full_file == NULL)
371     return NULL;
372 
373   back_to = make_cleanup (xfree, full_file);
374 
375   syscalls_info = syscall_parse_xml (full_file,
376 				     xml_fetch_content_from_file,
377 				     (void *) ldirname (filename).c_str ());
378   do_cleanups (back_to);
379 
380   return syscalls_info;
381 }
382 
383 /* Initializes the syscalls_info structure according to the
384    architecture.  */
385 static void
386 init_syscalls_info (struct gdbarch *gdbarch)
387 {
388   struct syscalls_info *syscalls_info = gdbarch_syscalls_info (gdbarch);
389   const char *xml_syscall_file = gdbarch_xml_syscall_file (gdbarch);
390 
391   /* Should we re-read the XML info for this target?  */
392   if (syscalls_info != NULL && syscalls_info->my_gdb_datadir != NULL
393       && filename_cmp (syscalls_info->my_gdb_datadir, gdb_datadir) != 0)
394     {
395       /* The data-directory changed from the last time we used it.
396 	 It means that we have to re-read the XML info.  */
397       free_syscalls_info (syscalls_info);
398       syscalls_info = NULL;
399       set_gdbarch_syscalls_info (gdbarch, NULL);
400     }
401 
402   /* Did we succeed at initializing this?  */
403   if (syscalls_info != NULL)
404     return;
405 
406   syscalls_info = xml_init_syscalls_info (xml_syscall_file);
407 
408   /* If there was some error reading the XML file, we initialize
409      gdbarch->syscalls_info anyway, in order to store information
410      about our attempt.  */
411   if (syscalls_info == NULL)
412     syscalls_info = allocate_syscalls_info ();
413 
414   if (syscalls_info->syscalls == NULL)
415     {
416       if (xml_syscall_file != NULL)
417 	warning (_("Could not load the syscall XML file `%s/%s'."),
418 		 gdb_datadir, xml_syscall_file);
419       else
420 	warning (_("There is no XML file to open."));
421 
422       warning (_("GDB will not be able to display "
423 		 "syscall names nor to verify if\n"
424 		 "any provided syscall numbers are valid."));
425     }
426 
427   /* Saving the data-directory used to read this XML info.  */
428   syscalls_info->my_gdb_datadir = xstrdup (gdb_datadir);
429 
430   set_gdbarch_syscalls_info (gdbarch, syscalls_info);
431 }
432 
433 /* Search for a syscall group by its name.  Return syscall_group_desc
434    structure for the group if found or NULL otherwise.  */
435 
436 static struct syscall_group_desc *
437 syscall_group_get_group_by_name (const struct syscalls_info *syscalls_info,
438 				 const char *group)
439 {
440   struct syscall_group_desc *groupdesc;
441   int i;
442 
443   if (syscalls_info == NULL)
444     return NULL;
445 
446   if (group == NULL)
447     return NULL;
448 
449    /* Search for existing group.  */
450   for (i = 0;
451        VEC_iterate (syscall_group_desc_p, syscalls_info->groups, i, groupdesc);
452        i++)
453     {
454       if (strcmp (groupdesc->name, group) == 0)
455 	return groupdesc;
456     }
457 
458   return NULL;
459 }
460 
461 static int
462 xml_get_syscall_number (struct gdbarch *gdbarch,
463                         const char *syscall_name)
464 {
465   struct syscalls_info *syscalls_info = gdbarch_syscalls_info (gdbarch);
466   struct syscall_desc *sysdesc;
467   int i;
468 
469   if (syscalls_info == NULL
470       || syscall_name == NULL)
471     return UNKNOWN_SYSCALL;
472 
473   for (i = 0;
474        VEC_iterate(syscall_desc_p, syscalls_info->syscalls, i, sysdesc);
475        i++)
476     if (strcmp (sysdesc->name, syscall_name) == 0)
477       return sysdesc->number;
478 
479   return UNKNOWN_SYSCALL;
480 }
481 
482 static const char *
483 xml_get_syscall_name (struct gdbarch *gdbarch,
484                       int syscall_number)
485 {
486   struct syscalls_info *syscalls_info = gdbarch_syscalls_info (gdbarch);
487   struct syscall_desc *sysdesc;
488   int i;
489 
490   if (syscalls_info == NULL
491       || syscall_number < 0)
492     return NULL;
493 
494   for (i = 0;
495        VEC_iterate(syscall_desc_p, syscalls_info->syscalls, i, sysdesc);
496        i++)
497     if (sysdesc->number == syscall_number)
498       return sysdesc->name;
499 
500   return NULL;
501 }
502 
503 static const char **
504 xml_list_of_syscalls (struct gdbarch *gdbarch)
505 {
506   struct syscalls_info *syscalls_info = gdbarch_syscalls_info (gdbarch);
507   struct syscall_desc *sysdesc;
508   const char **names = NULL;
509   int nsyscalls;
510   int i;
511 
512   if (syscalls_info == NULL)
513     return NULL;
514 
515   nsyscalls = VEC_length (syscall_desc_p, syscalls_info->syscalls);
516   names = XNEWVEC (const char *, nsyscalls + 1);
517 
518   for (i = 0;
519        VEC_iterate (syscall_desc_p, syscalls_info->syscalls, i, sysdesc);
520        i++)
521     names[i] = sysdesc->name;
522 
523   names[i] = NULL;
524 
525   return names;
526 }
527 
528 /* Iterate over the syscall_group_desc element to return a list of
529    syscalls that are part of the given group, terminated by an empty
530    element.  If the syscall group doesn't exist, return NULL.  */
531 
532 static struct syscall *
533 xml_list_syscalls_by_group (struct gdbarch *gdbarch, const char *group)
534 {
535   struct syscalls_info *syscalls_info = gdbarch_syscalls_info (gdbarch);
536   struct syscall_group_desc *groupdesc;
537   struct syscall_desc *sysdesc;
538   struct syscall *syscalls = NULL;
539   int nsyscalls;
540   int i;
541 
542   if (syscalls_info == NULL)
543     return NULL;
544 
545   groupdesc = syscall_group_get_group_by_name (syscalls_info, group);
546   if (groupdesc == NULL)
547     return NULL;
548 
549   nsyscalls = VEC_length (syscall_desc_p, groupdesc->syscalls);
550   syscalls = (struct syscall*) xmalloc ((nsyscalls + 1)
551 					* sizeof (struct syscall));
552 
553   for (i = 0;
554        VEC_iterate (syscall_desc_p, groupdesc->syscalls, i, sysdesc);
555        i++)
556     {
557       syscalls[i].name = sysdesc->name;
558       syscalls[i].number = sysdesc->number;
559     }
560 
561   /* Add final element marker.  */
562   syscalls[i].name = NULL;
563   syscalls[i].number = 0;
564 
565   return syscalls;
566 }
567 
568 /* Return a NULL terminated list of syscall groups or an empty list, if
569    no syscall group is available.  Return NULL, if there is no syscall
570    information available.  */
571 
572 static const char **
573 xml_list_of_groups (struct gdbarch *gdbarch)
574 {
575   struct syscalls_info *syscalls_info = gdbarch_syscalls_info (gdbarch);
576   struct syscall_group_desc *groupdesc;
577   const char **names = NULL;
578   int i;
579   int ngroups;
580 
581   if (syscalls_info == NULL)
582     return NULL;
583 
584   ngroups = VEC_length (syscall_group_desc_p, syscalls_info->groups);
585   names = (const char**) xmalloc ((ngroups + 1) * sizeof (char *));
586 
587   for (i = 0;
588        VEC_iterate (syscall_group_desc_p, syscalls_info->groups, i, groupdesc);
589        i++)
590     names[i] = groupdesc->name;
591 
592   names[i] = NULL;
593 
594   return names;
595 }
596 
597 void
598 set_xml_syscall_file_name (struct gdbarch *gdbarch, const char *name)
599 {
600   set_gdbarch_xml_syscall_file (gdbarch, name);
601 }
602 
603 void
604 get_syscall_by_number (struct gdbarch *gdbarch,
605 		       int syscall_number, struct syscall *s)
606 {
607   init_syscalls_info (gdbarch);
608 
609   s->number = syscall_number;
610   s->name = xml_get_syscall_name (gdbarch, syscall_number);
611 }
612 
613 void
614 get_syscall_by_name (struct gdbarch *gdbarch,
615 		     const char *syscall_name, struct syscall *s)
616 {
617   init_syscalls_info (gdbarch);
618 
619   s->number = xml_get_syscall_number (gdbarch, syscall_name);
620   s->name = syscall_name;
621 }
622 
623 const char **
624 get_syscall_names (struct gdbarch *gdbarch)
625 {
626   init_syscalls_info (gdbarch);
627 
628   return xml_list_of_syscalls (gdbarch);
629 }
630 
631 /* See comment in xml-syscall.h.  */
632 
633 struct syscall *
634 get_syscalls_by_group (struct gdbarch *gdbarch, const char *group)
635 {
636   init_syscalls_info (gdbarch);
637 
638   return xml_list_syscalls_by_group (gdbarch, group);
639 }
640 
641 /* See comment in xml-syscall.h.  */
642 
643 const char **
644 get_syscall_group_names (struct gdbarch *gdbarch)
645 {
646   init_syscalls_info (gdbarch);
647 
648   return xml_list_of_groups (gdbarch);
649 }
650 
651 #endif /* ! HAVE_LIBEXPAT */
652