* ax-gdb.c (gen_exp_binop_rest) [BINOP_SUBSCRIPT]: Error out if
[binutils-gdb.git] / gdb / xml-syscall.c
blob3f5585c65ac6a814da0e9215961892f152a158a6
1 /* Functions that provide the mechanism to parse a syscall XML file
2 and get its values.
4 Copyright (C) 2009, 2010 Free Software Foundation, Inc.
6 This file is part of GDB.
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.
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.
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/>. */
21 #include "defs.h"
22 #include "gdbtypes.h"
23 #include "xml-support.h"
24 #include "xml-syscall.h"
26 /* For the struct syscall definition. */
27 #include "target.h"
29 #include "filenames.h"
31 #include "gdb_assert.h"
33 #ifndef HAVE_LIBEXPAT
35 /* Dummy functions to indicate that there's no support for fetching
36 syscalls information. */
38 static void
39 syscall_warn_user (void)
41 static int have_warned = 0;
42 if (!have_warned)
44 have_warned = 1;
45 warning (_("Can not parse XML syscalls information; XML support was "
46 "disabled at compile time."));
50 void
51 set_xml_syscall_file_name (const char *name)
53 return;
56 void
57 get_syscall_by_number (int syscall_number,
58 struct syscall *s)
60 syscall_warn_user ();
61 s->number = syscall_number;
62 s->name = NULL;
65 void
66 get_syscall_by_name (const char *syscall_name,
67 struct syscall *s)
69 syscall_warn_user ();
70 s->number = UNKNOWN_SYSCALL;
71 s->name = syscall_name;
74 const char **
75 get_syscall_names (void)
77 syscall_warn_user ();
78 return NULL;
81 #else /* ! HAVE_LIBEXPAT */
83 /* Structure which describes a syscall. */
84 typedef struct syscall_desc
86 /* The syscall number. */
88 int number;
90 /* The syscall name. */
92 char *name;
93 } *syscall_desc_p;
94 DEF_VEC_P(syscall_desc_p);
96 /* Structure that represents syscalls information. */
97 struct syscalls_info
99 /* The syscalls. */
101 VEC(syscall_desc_p) *syscalls;
104 /* Callback data for syscall information parsing. */
105 struct syscall_parsing_data
107 /* The syscalls_info we are building. */
109 struct syscalls_info *sysinfo;
112 /* Structure used to store information about the available syscalls in
113 the system. */
114 static const struct syscalls_info *sysinfo = NULL;
116 /* A flag to tell if we already initialized the structure above. */
117 static int have_initialized_sysinfo = 0;
119 /* The filename of the syscall's XML. */
120 static const char *xml_syscall_file = NULL;
122 static struct syscalls_info *
123 allocate_syscalls_info (void)
125 return XZALLOC (struct syscalls_info);
128 static void
129 sysinfo_free_syscalls_desc (struct syscall_desc *sd)
131 xfree (sd->name);
134 static void
135 free_syscalls_info (void *arg)
137 struct syscalls_info *sysinfo = arg;
138 struct syscall_desc *sysdesc;
139 int i;
141 for (i = 0;
142 VEC_iterate (syscall_desc_p, sysinfo->syscalls, i, sysdesc);
143 i++)
144 sysinfo_free_syscalls_desc (sysdesc);
145 VEC_free (syscall_desc_p, sysinfo->syscalls);
147 xfree (sysinfo);
150 struct cleanup *
151 make_cleanup_free_syscalls_info (struct syscalls_info *sysinfo)
153 return make_cleanup (free_syscalls_info, sysinfo);
156 static void
157 syscall_create_syscall_desc (struct syscalls_info *sysinfo,
158 const char *name, int number)
160 struct syscall_desc *sysdesc = XZALLOC (struct syscall_desc);
162 sysdesc->name = xstrdup (name);
163 sysdesc->number = number;
165 VEC_safe_push (syscall_desc_p, sysinfo->syscalls, sysdesc);
168 /* Handle the start of a <syscalls_info> element. */
169 static void
170 syscall_start_syscalls_info (struct gdb_xml_parser *parser,
171 const struct gdb_xml_element *element,
172 void *user_data,
173 VEC(gdb_xml_value_s) *attributes)
175 struct syscall_parsing_data *data = user_data;
176 struct syscalls_info *sysinfo = data->sysinfo;
179 /* Handle the start of a <syscall> element. */
180 static void
181 syscall_start_syscall (struct gdb_xml_parser *parser,
182 const struct gdb_xml_element *element,
183 void *user_data, VEC(gdb_xml_value_s) *attributes)
185 struct syscall_parsing_data *data = user_data;
186 struct gdb_xml_value *attrs = VEC_address (gdb_xml_value_s, attributes);
187 int len, i;
188 /* syscall info. */
189 char *name = NULL;
190 int number = 0;
192 len = VEC_length (gdb_xml_value_s, attributes);
194 for (i = 0; i < len; i++)
196 if (strcmp (attrs[i].name, "name") == 0)
197 name = attrs[i].value;
198 else if (strcmp (attrs[i].name, "number") == 0)
199 number = * (ULONGEST *) attrs[i].value;
200 else
201 internal_error (__FILE__, __LINE__,
202 _("Unknown attribute name '%s'."), attrs[i].name);
205 syscall_create_syscall_desc (data->sysinfo, name, number);
209 /* The elements and attributes of an XML syscall document. */
210 static const struct gdb_xml_attribute syscall_attr[] = {
211 { "number", GDB_XML_AF_NONE, gdb_xml_parse_attr_ulongest, NULL },
212 { "name", GDB_XML_AF_NONE, NULL, NULL },
213 { NULL, GDB_XML_AF_NONE, NULL, NULL }
216 static const struct gdb_xml_element syscalls_info_children[] = {
217 { "syscall", syscall_attr, NULL,
218 GDB_XML_EF_OPTIONAL | GDB_XML_EF_REPEATABLE,
219 syscall_start_syscall, NULL },
220 { NULL, NULL, NULL, GDB_XML_EF_NONE, NULL, NULL }
223 static const struct gdb_xml_element syselements[] = {
224 { "syscalls_info", NULL, syscalls_info_children,
225 GDB_XML_EF_NONE, syscall_start_syscalls_info, NULL },
226 { NULL, NULL, NULL, GDB_XML_EF_NONE, NULL, NULL }
229 static struct syscalls_info *
230 syscall_parse_xml (const char *document, xml_fetch_another fetcher,
231 void *fetcher_baton)
233 struct cleanup *result_cleanup;
234 struct gdb_xml_parser *parser;
235 struct syscall_parsing_data data;
236 char *expanded_text;
237 int i;
239 parser = gdb_xml_create_parser_and_cleanup (_("syscalls info"),
240 syselements, &data);
242 memset (&data, 0, sizeof (struct syscall_parsing_data));
243 data.sysinfo = allocate_syscalls_info ();
244 result_cleanup = make_cleanup_free_syscalls_info (data.sysinfo);
246 if (gdb_xml_parse (parser, document) == 0)
248 /* Parsed successfully. */
249 discard_cleanups (result_cleanup);
250 return data.sysinfo;
252 else
254 warning (_("Could not load XML syscalls info; ignoring"));
255 do_cleanups (result_cleanup);
256 return NULL;
260 /* Function responsible for initializing the information
261 about the syscalls. It reads the XML file and fills the
262 struct syscalls_info with the values.
264 Returns the struct syscalls_info if the file is valid, NULL otherwise. */
265 static const struct syscalls_info *
266 xml_init_syscalls_info (const char *filename)
268 char *full_file;
269 char *dirname;
270 struct syscalls_info *sysinfo;
271 struct cleanup *back_to;
273 full_file = xml_fetch_content_from_file (filename, gdb_datadir);
274 if (full_file == NULL)
275 return NULL;
277 back_to = make_cleanup (xfree, full_file);
279 dirname = ldirname (filename);
280 if (dirname != NULL)
281 make_cleanup (xfree, dirname);
283 sysinfo = syscall_parse_xml (full_file, xml_fetch_content_from_file, dirname);
284 do_cleanups (back_to);
286 return sysinfo;
289 /* Initializes the syscalls_info structure according to the
290 architecture. */
291 static void
292 init_sysinfo (void)
294 /* Did we already try to initialize the structure? */
295 if (have_initialized_sysinfo)
296 return;
298 sysinfo = xml_init_syscalls_info (xml_syscall_file);
300 have_initialized_sysinfo = 1;
302 if (sysinfo == NULL)
304 if (xml_syscall_file)
305 warning (_("\
306 Could not load the syscall XML file `%s'."), xml_syscall_file);
307 else
308 warning (_("\
309 There is no XML file to open."));
311 warning (_("\
312 GDB will not be able to display syscall names nor to verify if\n\
313 any provided syscall numbers are valid."));
317 static int
318 xml_get_syscall_number (const struct syscalls_info *sysinfo,
319 const char *syscall_name)
321 struct syscall_desc *sysdesc;
322 int i;
324 if (sysinfo == NULL
325 || syscall_name == NULL)
326 return UNKNOWN_SYSCALL;
328 for (i = 0;
329 VEC_iterate(syscall_desc_p, sysinfo->syscalls, i, sysdesc);
330 i++)
331 if (strcmp (sysdesc->name, syscall_name) == 0)
332 return sysdesc->number;
334 return UNKNOWN_SYSCALL;
337 static const char *
338 xml_get_syscall_name (const struct syscalls_info *sysinfo,
339 int syscall_number)
341 struct syscall_desc *sysdesc;
342 int i;
344 if (sysinfo == NULL
345 || syscall_number < 0)
346 return NULL;
348 for (i = 0;
349 VEC_iterate(syscall_desc_p, sysinfo->syscalls, i, sysdesc);
350 i++)
351 if (sysdesc->number == syscall_number)
352 return sysdesc->name;
354 return NULL;
357 static const char **
358 xml_list_of_syscalls (const struct syscalls_info *sysinfo)
360 struct syscall_desc *sysdesc;
361 const char **names = NULL;
362 int nsyscalls;
363 int i;
365 if (sysinfo == NULL)
366 return NULL;
368 nsyscalls = VEC_length (syscall_desc_p, sysinfo->syscalls);
369 names = xmalloc ((nsyscalls + 1) * sizeof (char *));
371 for (i = 0;
372 VEC_iterate (syscall_desc_p, sysinfo->syscalls, i, sysdesc);
373 i++)
374 names[i] = sysdesc->name;
376 names[i] = NULL;
378 return names;
381 void
382 set_xml_syscall_file_name (const char *name)
384 xml_syscall_file = name;
387 void
388 get_syscall_by_number (int syscall_number,
389 struct syscall *s)
391 init_sysinfo ();
393 s->number = syscall_number;
394 s->name = xml_get_syscall_name (sysinfo, syscall_number);
397 void
398 get_syscall_by_name (const char *syscall_name,
399 struct syscall *s)
401 init_sysinfo ();
403 s->number = xml_get_syscall_number (sysinfo, syscall_name);
404 s->name = syscall_name;
407 const char **
408 get_syscall_names (void)
410 init_sysinfo ();
412 return xml_list_of_syscalls (sysinfo);
415 #endif /* ! HAVE_LIBEXPAT */