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