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