*** empty log message ***
[deliverable/binutils-gdb.git] / gdb / xml-syscall.c
CommitLineData
fbbe92c5
SDJ
1/* Functions that provide the mechanism to parse a syscall XML file
2 and get its values.
3
4c38e0a4 4 Copyright (C) 2009, 2010 Free Software Foundation, Inc.
fbbe92c5
SDJ
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
26/* For the struct syscall definition. */
27#include "target.h"
28
29#include "filenames.h"
30
31#include "gdb_assert.h"
32
33#ifndef HAVE_LIBEXPAT
34
35/* Dummy functions to indicate that there's no support for fetching
36 syscalls information. */
37
38static void
39syscall_warn_user (void)
40{
41 static int have_warned = 0;
42 if (!have_warned)
43 {
44 have_warned = 1;
45 warning (_("Can not parse XML syscalls information; XML support was "
46 "disabled at compile time."));
47 }
48}
49
50void
51set_xml_syscall_file_name (const char *name)
52{
bccd0dd2 53 return;
fbbe92c5
SDJ
54}
55
56void
57get_syscall_by_number (int syscall_number,
58 struct syscall *s)
59{
60 syscall_warn_user ();
61 s->number = syscall_number;
62 s->name = NULL;
63}
64
65void
66get_syscall_by_name (const char *syscall_name,
67 struct syscall *s)
68{
69 syscall_warn_user ();
70 s->number = UNKNOWN_SYSCALL;
71 s->name = syscall_name;
72}
73
74const char **
75get_syscall_names (void)
76{
77 syscall_warn_user ();
78 return NULL;
79}
80
fbbe92c5
SDJ
81#else /* ! HAVE_LIBEXPAT */
82
626ea16d
SDJ
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. */
85static char *my_gdb_datadir = NULL;
86
fbbe92c5
SDJ
87/* Structure which describes a syscall. */
88typedef struct syscall_desc
89{
90 /* The syscall number. */
91
92 int number;
93
94 /* The syscall name. */
95
96 char *name;
97} *syscall_desc_p;
98DEF_VEC_P(syscall_desc_p);
99
100/* Structure that represents syscalls information. */
101struct syscalls_info
102{
103 /* The syscalls. */
104
105 VEC(syscall_desc_p) *syscalls;
106};
107
108/* Callback data for syscall information parsing. */
109struct syscall_parsing_data
110{
111 /* The syscalls_info we are building. */
112
113 struct syscalls_info *sysinfo;
114};
115
116/* Structure used to store information about the available syscalls in
117 the system. */
bccd0dd2 118static const struct syscalls_info *sysinfo = NULL;
fbbe92c5
SDJ
119
120/* A flag to tell if we already initialized the structure above. */
121static int have_initialized_sysinfo = 0;
122
123/* The filename of the syscall's XML. */
124static const char *xml_syscall_file = NULL;
125
126static struct syscalls_info *
127allocate_syscalls_info (void)
128{
129 return XZALLOC (struct syscalls_info);
130}
131
132static void
133sysinfo_free_syscalls_desc (struct syscall_desc *sd)
134{
135 xfree (sd->name);
136}
137
138static void
139free_syscalls_info (void *arg)
140{
141 struct syscalls_info *sysinfo = arg;
142 struct syscall_desc *sysdesc;
143 int i;
144
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);
150
151 xfree (sysinfo);
152}
153
154struct cleanup *
155make_cleanup_free_syscalls_info (struct syscalls_info *sysinfo)
156{
157 return make_cleanup (free_syscalls_info, sysinfo);
158}
159
160static void
161syscall_create_syscall_desc (struct syscalls_info *sysinfo,
162 const char *name, int number)
163{
164 struct syscall_desc *sysdesc = XZALLOC (struct syscall_desc);
165
166 sysdesc->name = xstrdup (name);
167 sysdesc->number = number;
168
169 VEC_safe_push (syscall_desc_p, sysinfo->syscalls, sysdesc);
170}
171
172/* Handle the start of a <syscalls_info> element. */
173static void
174syscall_start_syscalls_info (struct gdb_xml_parser *parser,
175 const struct gdb_xml_element *element,
176 void *user_data,
177 VEC(gdb_xml_value_s) *attributes)
178{
179 struct syscall_parsing_data *data = user_data;
180 struct syscalls_info *sysinfo = data->sysinfo;
181}
182
183/* Handle the start of a <syscall> element. */
184static void
185syscall_start_syscall (struct gdb_xml_parser *parser,
186 const struct gdb_xml_element *element,
187 void *user_data, VEC(gdb_xml_value_s) *attributes)
188{
189 struct syscall_parsing_data *data = user_data;
190 struct gdb_xml_value *attrs = VEC_address (gdb_xml_value_s, attributes);
191 int len, i;
192 /* syscall info. */
193 char *name = NULL;
194 int number = 0;
195
196 len = VEC_length (gdb_xml_value_s, attributes);
197
198 for (i = 0; i < len; i++)
199 {
200 if (strcmp (attrs[i].name, "name") == 0)
201 name = attrs[i].value;
202 else if (strcmp (attrs[i].name, "number") == 0)
203 number = * (ULONGEST *) attrs[i].value;
204 else
205 internal_error (__FILE__, __LINE__,
206 _("Unknown attribute name '%s'."), attrs[i].name);
207 }
208
209 syscall_create_syscall_desc (data->sysinfo, name, number);
210}
211
212
213/* The elements and attributes of an XML syscall document. */
214static const struct gdb_xml_attribute syscall_attr[] = {
215 { "number", GDB_XML_AF_NONE, gdb_xml_parse_attr_ulongest, NULL },
216 { "name", GDB_XML_AF_NONE, NULL, NULL },
217 { NULL, GDB_XML_AF_NONE, NULL, NULL }
218};
219
220static const struct gdb_xml_element syscalls_info_children[] = {
221 { "syscall", syscall_attr, NULL,
222 GDB_XML_EF_OPTIONAL | GDB_XML_EF_REPEATABLE,
223 syscall_start_syscall, NULL },
224 { NULL, NULL, NULL, GDB_XML_EF_NONE, NULL, NULL }
225};
226
227static const struct gdb_xml_element syselements[] = {
228 { "syscalls_info", NULL, syscalls_info_children,
229 GDB_XML_EF_NONE, syscall_start_syscalls_info, NULL },
230 { NULL, NULL, NULL, GDB_XML_EF_NONE, NULL, NULL }
231};
232
233static struct syscalls_info *
234syscall_parse_xml (const char *document, xml_fetch_another fetcher,
235 void *fetcher_baton)
236{
237 struct cleanup *result_cleanup;
238 struct gdb_xml_parser *parser;
239 struct syscall_parsing_data data;
240 char *expanded_text;
241 int i;
242
243 parser = gdb_xml_create_parser_and_cleanup (_("syscalls info"),
244 syselements, &data);
245
246 memset (&data, 0, sizeof (struct syscall_parsing_data));
247 data.sysinfo = allocate_syscalls_info ();
248 result_cleanup = make_cleanup_free_syscalls_info (data.sysinfo);
249
250 if (gdb_xml_parse (parser, document) == 0)
251 {
252 /* Parsed successfully. */
253 discard_cleanups (result_cleanup);
254 return data.sysinfo;
255 }
256 else
257 {
258 warning (_("Could not load XML syscalls info; ignoring"));
259 do_cleanups (result_cleanup);
260 return NULL;
261 }
262}
263
264/* Function responsible for initializing the information
265 about the syscalls. It reads the XML file and fills the
266 struct syscalls_info with the values.
267
268 Returns the struct syscalls_info if the file is valid, NULL otherwise. */
269static const struct syscalls_info *
270xml_init_syscalls_info (const char *filename)
271{
272 char *full_file;
273 char *dirname;
274 struct syscalls_info *sysinfo;
275 struct cleanup *back_to;
276
277 full_file = xml_fetch_content_from_file (filename, gdb_datadir);
278 if (full_file == NULL)
bccd0dd2 279 return NULL;
fbbe92c5
SDJ
280
281 back_to = make_cleanup (xfree, full_file);
282
283 dirname = ldirname (filename);
284 if (dirname != NULL)
285 make_cleanup (xfree, dirname);
286
287 sysinfo = syscall_parse_xml (full_file, xml_fetch_content_from_file, dirname);
288 do_cleanups (back_to);
289
290 return sysinfo;
291}
292
293/* Initializes the syscalls_info structure according to the
294 architecture. */
295static void
296init_sysinfo (void)
297{
626ea16d
SDJ
298 /* Should we re-read the XML info for this target? */
299 if (my_gdb_datadir && strcmp (my_gdb_datadir, gdb_datadir) != 0)
300 {
301 /* The data-directory changed from the last time we used it.
302 It means that we have to re-read the XML info. */
303 have_initialized_sysinfo = 0;
304 xfree (my_gdb_datadir);
305 my_gdb_datadir = NULL;
306 if (sysinfo)
307 free_syscalls_info ((void *) sysinfo);
308 }
309
fbbe92c5
SDJ
310 /* Did we already try to initialize the structure? */
311 if (have_initialized_sysinfo)
312 return;
fbbe92c5 313
bccd0dd2 314 sysinfo = xml_init_syscalls_info (xml_syscall_file);
fbbe92c5
SDJ
315
316 have_initialized_sysinfo = 1;
317
bccd0dd2 318 if (sysinfo == NULL)
fbbe92c5
SDJ
319 {
320 if (xml_syscall_file)
bccd0dd2 321 warning (_("\
626ea16d
SDJ
322Could not load the syscall XML file `%s/%s'."),
323 gdb_datadir, xml_syscall_file);
fbbe92c5 324 else
bccd0dd2
SDJ
325 warning (_("\
326There is no XML file to open."));
327
328 warning (_("\
329GDB will not be able to display syscall names nor to verify if\n\
330any provided syscall numbers are valid."));
fbbe92c5 331 }
626ea16d
SDJ
332
333 /* Saving the data-directory used to read this XML info. */
334 my_gdb_datadir = xstrdup (gdb_datadir);
fbbe92c5
SDJ
335}
336
337static int
338xml_get_syscall_number (const struct syscalls_info *sysinfo,
339 const char *syscall_name)
340{
341 struct syscall_desc *sysdesc;
342 int i;
343
344 if (sysinfo == NULL
345 || syscall_name == NULL)
346 return UNKNOWN_SYSCALL;
347
348 for (i = 0;
349 VEC_iterate(syscall_desc_p, sysinfo->syscalls, i, sysdesc);
350 i++)
351 if (strcmp (sysdesc->name, syscall_name) == 0)
352 return sysdesc->number;
353
354 return UNKNOWN_SYSCALL;
355}
356
357static const char *
358xml_get_syscall_name (const struct syscalls_info *sysinfo,
359 int syscall_number)
360{
361 struct syscall_desc *sysdesc;
362 int i;
363
364 if (sysinfo == NULL
365 || syscall_number < 0)
366 return NULL;
367
368 for (i = 0;
369 VEC_iterate(syscall_desc_p, sysinfo->syscalls, i, sysdesc);
370 i++)
371 if (sysdesc->number == syscall_number)
372 return sysdesc->name;
373
374 return NULL;
375}
376
fbbe92c5
SDJ
377static const char **
378xml_list_of_syscalls (const struct syscalls_info *sysinfo)
379{
380 struct syscall_desc *sysdesc;
381 const char **names = NULL;
382 int nsyscalls;
383 int i;
384
385 if (sysinfo == NULL)
386 return NULL;
387
388 nsyscalls = VEC_length (syscall_desc_p, sysinfo->syscalls);
389 names = xmalloc ((nsyscalls + 1) * sizeof (char *));
390
391 for (i = 0;
392 VEC_iterate (syscall_desc_p, sysinfo->syscalls, i, sysdesc);
393 i++)
394 names[i] = sysdesc->name;
395
396 names[i] = NULL;
397
398 return names;
399}
400
401void
402set_xml_syscall_file_name (const char *name)
403{
404 xml_syscall_file = name;
405}
406
407void
408get_syscall_by_number (int syscall_number,
409 struct syscall *s)
410{
411 init_sysinfo ();
412
413 s->number = syscall_number;
bccd0dd2 414 s->name = xml_get_syscall_name (sysinfo, syscall_number);
fbbe92c5
SDJ
415}
416
417void
418get_syscall_by_name (const char *syscall_name,
419 struct syscall *s)
420{
421 init_sysinfo ();
422
bccd0dd2 423 s->number = xml_get_syscall_number (sysinfo, syscall_name);
fbbe92c5
SDJ
424 s->name = syscall_name;
425}
426
427const char **
428get_syscall_names (void)
429{
430 init_sysinfo ();
431
bccd0dd2 432 return xml_list_of_syscalls (sysinfo);
fbbe92c5
SDJ
433}
434
435#endif /* ! HAVE_LIBEXPAT */
This page took 0.143596 seconds and 4 git commands to generate.