kernel - cleanup vfs_cache debugging
[dragonfly.git] / contrib / gdb-7 / gdb / xml-syscall.c
blob751de7f42cb8ec0c08c98d5aeb85c5194b98be91
1 /* Functions that provide the mechanism to parse a syscall XML file
2 and get its values.
4 Copyright (C) 2009-2013 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 /* Variable that will hold the last known data-directory. This is useful to
84 know whether we should re-read the XML info for the target. */
85 static char *my_gdb_datadir = NULL;
87 /* Structure which describes a syscall. */
88 typedef struct syscall_desc
90 /* The syscall number. */
92 int number;
94 /* The syscall name. */
96 char *name;
97 } *syscall_desc_p;
98 DEF_VEC_P(syscall_desc_p);
100 /* Structure that represents syscalls information. */
101 struct syscalls_info
103 /* The syscalls. */
105 VEC(syscall_desc_p) *syscalls;
108 /* Callback data for syscall information parsing. */
109 struct syscall_parsing_data
111 /* The syscalls_info we are building. */
113 struct syscalls_info *sysinfo;
116 /* Structure used to store information about the available syscalls in
117 the system. */
118 static const struct syscalls_info *sysinfo = NULL;
120 /* A flag to tell if we already initialized the structure above. */
121 static int have_initialized_sysinfo = 0;
123 /* The filename of the syscall's XML. */
124 static const char *xml_syscall_file = NULL;
126 static struct syscalls_info *
127 allocate_syscalls_info (void)
129 return XZALLOC (struct syscalls_info);
132 static void
133 sysinfo_free_syscalls_desc (struct syscall_desc *sd)
135 xfree (sd->name);
138 static void
139 free_syscalls_info (void *arg)
141 struct syscalls_info *sysinfo = arg;
142 struct syscall_desc *sysdesc;
143 int i;
145 for (i = 0;
146 VEC_iterate (syscall_desc_p, sysinfo->syscalls, i, sysdesc);
147 i++)
148 sysinfo_free_syscalls_desc (sysdesc);
149 VEC_free (syscall_desc_p, sysinfo->syscalls);
151 xfree (sysinfo);
154 static struct cleanup *
155 make_cleanup_free_syscalls_info (struct syscalls_info *sysinfo)
157 return make_cleanup (free_syscalls_info, sysinfo);
160 static void
161 syscall_create_syscall_desc (struct syscalls_info *sysinfo,
162 const char *name, int number)
164 struct syscall_desc *sysdesc = XZALLOC (struct syscall_desc);
166 sysdesc->name = xstrdup (name);
167 sysdesc->number = number;
169 VEC_safe_push (syscall_desc_p, sysinfo->syscalls, sysdesc);
172 /* Handle the start of a <syscall> element. */
173 static void
174 syscall_start_syscall (struct gdb_xml_parser *parser,
175 const struct gdb_xml_element *element,
176 void *user_data, VEC(gdb_xml_value_s) *attributes)
178 struct syscall_parsing_data *data = user_data;
179 struct gdb_xml_value *attrs = VEC_address (gdb_xml_value_s, attributes);
180 int len, i;
181 /* syscall info. */
182 char *name = NULL;
183 int number = 0;
185 len = VEC_length (gdb_xml_value_s, attributes);
187 for (i = 0; i < len; i++)
189 if (strcmp (attrs[i].name, "name") == 0)
190 name = attrs[i].value;
191 else if (strcmp (attrs[i].name, "number") == 0)
192 number = * (ULONGEST *) attrs[i].value;
193 else
194 internal_error (__FILE__, __LINE__,
195 _("Unknown attribute name '%s'."), attrs[i].name);
198 gdb_assert (name);
199 syscall_create_syscall_desc (data->sysinfo, name, number);
203 /* The elements and attributes of an XML syscall document. */
204 static const struct gdb_xml_attribute syscall_attr[] = {
205 { "number", GDB_XML_AF_NONE, gdb_xml_parse_attr_ulongest, NULL },
206 { "name", GDB_XML_AF_NONE, NULL, NULL },
207 { NULL, GDB_XML_AF_NONE, NULL, NULL }
210 static const struct gdb_xml_element syscalls_info_children[] = {
211 { "syscall", syscall_attr, NULL,
212 GDB_XML_EF_OPTIONAL | GDB_XML_EF_REPEATABLE,
213 syscall_start_syscall, NULL },
214 { NULL, NULL, NULL, GDB_XML_EF_NONE, NULL, NULL }
217 static const struct gdb_xml_element syselements[] = {
218 { "syscalls_info", NULL, syscalls_info_children,
219 GDB_XML_EF_NONE, NULL, NULL },
220 { NULL, NULL, NULL, GDB_XML_EF_NONE, NULL, NULL }
223 static struct syscalls_info *
224 syscall_parse_xml (const char *document, xml_fetch_another fetcher,
225 void *fetcher_baton)
227 struct cleanup *result_cleanup;
228 struct syscall_parsing_data data;
230 data.sysinfo = allocate_syscalls_info ();
231 result_cleanup = make_cleanup_free_syscalls_info (data.sysinfo);
233 if (gdb_xml_parse_quick (_("syscalls info"), NULL,
234 syselements, document, &data) == 0)
236 /* Parsed successfully. */
237 discard_cleanups (result_cleanup);
238 return data.sysinfo;
240 else
242 warning (_("Could not load XML syscalls info; ignoring"));
243 do_cleanups (result_cleanup);
244 return NULL;
248 /* Function responsible for initializing the information
249 about the syscalls. It reads the XML file and fills the
250 struct syscalls_info with the values.
252 Returns the struct syscalls_info if the file is valid, NULL otherwise. */
253 static const struct syscalls_info *
254 xml_init_syscalls_info (const char *filename)
256 char *full_file;
257 char *dirname;
258 struct syscalls_info *sysinfo;
259 struct cleanup *back_to;
261 full_file = xml_fetch_content_from_file (filename, gdb_datadir);
262 if (full_file == NULL)
263 return NULL;
265 back_to = make_cleanup (xfree, full_file);
267 dirname = ldirname (filename);
268 if (dirname != NULL)
269 make_cleanup (xfree, dirname);
271 sysinfo = syscall_parse_xml (full_file,
272 xml_fetch_content_from_file, dirname);
273 do_cleanups (back_to);
275 return sysinfo;
278 /* Initializes the syscalls_info structure according to the
279 architecture. */
280 static void
281 init_sysinfo (void)
283 /* Should we re-read the XML info for this target? */
284 if (my_gdb_datadir && filename_cmp (my_gdb_datadir, gdb_datadir) != 0)
286 /* The data-directory changed from the last time we used it.
287 It means that we have to re-read the XML info. */
288 have_initialized_sysinfo = 0;
289 xfree (my_gdb_datadir);
290 my_gdb_datadir = NULL;
291 if (sysinfo)
292 free_syscalls_info ((void *) sysinfo);
295 /* Did we already try to initialize the structure? */
296 if (have_initialized_sysinfo)
297 return;
299 sysinfo = xml_init_syscalls_info (xml_syscall_file);
301 have_initialized_sysinfo = 1;
303 if (sysinfo == NULL)
305 if (xml_syscall_file)
306 warning (_("Could not load the syscall XML file `%s/%s'."),
307 gdb_datadir, xml_syscall_file);
308 else
309 warning (_("There is no XML file to open."));
311 warning (_("GDB will not be able to display "
312 "syscall names nor to verify if\n"
313 "any provided syscall numbers are valid."));
316 /* Saving the data-directory used to read this XML info. */
317 my_gdb_datadir = xstrdup (gdb_datadir);
320 static int
321 xml_get_syscall_number (const struct syscalls_info *sysinfo,
322 const char *syscall_name)
324 struct syscall_desc *sysdesc;
325 int i;
327 if (sysinfo == NULL
328 || syscall_name == NULL)
329 return UNKNOWN_SYSCALL;
331 for (i = 0;
332 VEC_iterate(syscall_desc_p, sysinfo->syscalls, i, sysdesc);
333 i++)
334 if (strcmp (sysdesc->name, syscall_name) == 0)
335 return sysdesc->number;
337 return UNKNOWN_SYSCALL;
340 static const char *
341 xml_get_syscall_name (const struct syscalls_info *sysinfo,
342 int syscall_number)
344 struct syscall_desc *sysdesc;
345 int i;
347 if (sysinfo == NULL
348 || syscall_number < 0)
349 return NULL;
351 for (i = 0;
352 VEC_iterate(syscall_desc_p, sysinfo->syscalls, i, sysdesc);
353 i++)
354 if (sysdesc->number == syscall_number)
355 return sysdesc->name;
357 return NULL;
360 static const char **
361 xml_list_of_syscalls (const struct syscalls_info *sysinfo)
363 struct syscall_desc *sysdesc;
364 const char **names = NULL;
365 int nsyscalls;
366 int i;
368 if (sysinfo == NULL)
369 return NULL;
371 nsyscalls = VEC_length (syscall_desc_p, sysinfo->syscalls);
372 names = xmalloc ((nsyscalls + 1) * sizeof (char *));
374 for (i = 0;
375 VEC_iterate (syscall_desc_p, sysinfo->syscalls, i, sysdesc);
376 i++)
377 names[i] = sysdesc->name;
379 names[i] = NULL;
381 return names;
384 void
385 set_xml_syscall_file_name (const char *name)
387 xml_syscall_file = name;
390 void
391 get_syscall_by_number (int syscall_number,
392 struct syscall *s)
394 init_sysinfo ();
396 s->number = syscall_number;
397 s->name = xml_get_syscall_name (sysinfo, syscall_number);
400 void
401 get_syscall_by_name (const char *syscall_name,
402 struct syscall *s)
404 init_sysinfo ();
406 s->number = xml_get_syscall_number (sysinfo, syscall_name);
407 s->name = syscall_name;
410 const char **
411 get_syscall_names (void)
413 init_sysinfo ();
415 return xml_list_of_syscalls (sysinfo);
418 #endif /* ! HAVE_LIBEXPAT */