Commit | Line | Data |
---|---|---|
10304ef3 SDJ |
1 | /* Everything about syscall catchpoints, for GDB. |
2 | ||
42a4f53d | 3 | Copyright (C) 2009-2019 Free Software Foundation, Inc. |
10304ef3 SDJ |
4 | |
5 | This file is part of GDB. | |
6 | ||
7 | This program is free software; you can redistribute it and/or modify | |
8 | it under the terms of the GNU General Public License as published by | |
9 | the Free Software Foundation; either version 3 of the License, or | |
10 | (at your option) any later version. | |
11 | ||
12 | This program is distributed in the hope that it will be useful, | |
13 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 | GNU General Public License for more details. | |
16 | ||
17 | You should have received a copy of the GNU General Public License | |
18 | along with this program. If not, see <http://www.gnu.org/licenses/>. */ | |
19 | ||
20 | #include "defs.h" | |
d55e5aa6 TT |
21 | |
22 | /* Standard C includes. */ | |
10304ef3 | 23 | #include <ctype.h> |
d55e5aa6 TT |
24 | |
25 | /* Local non-gdb includes. */ | |
26 | #include "annotate.h" | |
27 | #include "arch-utils.h" | |
10304ef3 | 28 | #include "breakpoint.h" |
d55e5aa6 | 29 | #include "cli/cli-utils.h" |
10304ef3 SDJ |
30 | #include "gdbcmd.h" |
31 | #include "inferior.h" | |
10304ef3 | 32 | #include "mi/mi-common.h" |
76727919 | 33 | #include "observable.h" |
d55e5aa6 | 34 | #include "valprint.h" |
10304ef3 SDJ |
35 | #include "xml-syscall.h" |
36 | ||
37 | /* An instance of this type is used to represent a syscall catchpoint. | |
c1fc2657 | 38 | A breakpoint is really of this type iff its ops pointer points to |
10304ef3 SDJ |
39 | CATCH_SYSCALL_BREAKPOINT_OPS. */ |
40 | ||
c1fc2657 | 41 | struct syscall_catchpoint : public breakpoint |
10304ef3 | 42 | { |
10304ef3 | 43 | /* Syscall numbers used for the 'catch syscall' feature. If no |
e12c9b7a TT |
44 | syscall has been specified for filtering, it is empty. |
45 | Otherwise, it holds a list of all syscalls to be caught. */ | |
46 | std::vector<int> syscalls_to_be_caught; | |
10304ef3 SDJ |
47 | }; |
48 | ||
10304ef3 SDJ |
49 | static const struct inferior_data *catch_syscall_inferior_data = NULL; |
50 | ||
51 | struct catch_syscall_inferior_data | |
52 | { | |
53 | /* We keep a count of the number of times the user has requested a | |
54 | particular syscall to be tracked, and pass this information to the | |
55 | target. This lets capable targets implement filtering directly. */ | |
56 | ||
57 | /* Number of times that "any" syscall is requested. */ | |
58 | int any_syscall_count; | |
59 | ||
60 | /* Count of each system call. */ | |
b6f48cb0 | 61 | std::vector<int> syscalls_counts; |
10304ef3 SDJ |
62 | |
63 | /* This counts all syscall catch requests, so we can readily determine | |
64 | if any catching is necessary. */ | |
65 | int total_syscalls_count; | |
66 | }; | |
67 | ||
b6f48cb0 | 68 | static struct catch_syscall_inferior_data * |
10304ef3 SDJ |
69 | get_catch_syscall_inferior_data (struct inferior *inf) |
70 | { | |
71 | struct catch_syscall_inferior_data *inf_data; | |
72 | ||
9a3c8263 SM |
73 | inf_data = ((struct catch_syscall_inferior_data *) |
74 | inferior_data (inf, catch_syscall_inferior_data)); | |
10304ef3 SDJ |
75 | if (inf_data == NULL) |
76 | { | |
b6f48cb0 | 77 | inf_data = new struct catch_syscall_inferior_data (); |
10304ef3 SDJ |
78 | set_inferior_data (inf, catch_syscall_inferior_data, inf_data); |
79 | } | |
80 | ||
81 | return inf_data; | |
82 | } | |
83 | ||
84 | static void | |
85 | catch_syscall_inferior_data_cleanup (struct inferior *inf, void *arg) | |
86 | { | |
b6f48cb0 TT |
87 | struct catch_syscall_inferior_data *inf_data |
88 | = (struct catch_syscall_inferior_data *) arg; | |
89 | delete inf_data; | |
10304ef3 SDJ |
90 | } |
91 | ||
92 | ||
93 | /* Implement the "insert" breakpoint_ops method for syscall | |
94 | catchpoints. */ | |
95 | ||
96 | static int | |
97 | insert_catch_syscall (struct bp_location *bl) | |
98 | { | |
99 | struct syscall_catchpoint *c = (struct syscall_catchpoint *) bl->owner; | |
100 | struct inferior *inf = current_inferior (); | |
101 | struct catch_syscall_inferior_data *inf_data | |
102 | = get_catch_syscall_inferior_data (inf); | |
103 | ||
104 | ++inf_data->total_syscalls_count; | |
e12c9b7a | 105 | if (c->syscalls_to_be_caught.empty ()) |
10304ef3 SDJ |
106 | ++inf_data->any_syscall_count; |
107 | else | |
108 | { | |
e12c9b7a | 109 | for (int iter : c->syscalls_to_be_caught) |
10304ef3 | 110 | { |
b6f48cb0 TT |
111 | if (iter >= inf_data->syscalls_counts.size ()) |
112 | inf_data->syscalls_counts.resize (iter + 1); | |
113 | ++inf_data->syscalls_counts[iter]; | |
10304ef3 SDJ |
114 | } |
115 | } | |
116 | ||
e99b03dc | 117 | return target_set_syscall_catchpoint (inferior_ptid.pid (), |
10304ef3 SDJ |
118 | inf_data->total_syscalls_count != 0, |
119 | inf_data->any_syscall_count, | |
649a140c | 120 | inf_data->syscalls_counts); |
10304ef3 SDJ |
121 | } |
122 | ||
123 | /* Implement the "remove" breakpoint_ops method for syscall | |
124 | catchpoints. */ | |
125 | ||
126 | static int | |
73971819 | 127 | remove_catch_syscall (struct bp_location *bl, enum remove_bp_reason reason) |
10304ef3 SDJ |
128 | { |
129 | struct syscall_catchpoint *c = (struct syscall_catchpoint *) bl->owner; | |
130 | struct inferior *inf = current_inferior (); | |
131 | struct catch_syscall_inferior_data *inf_data | |
132 | = get_catch_syscall_inferior_data (inf); | |
133 | ||
134 | --inf_data->total_syscalls_count; | |
e12c9b7a | 135 | if (c->syscalls_to_be_caught.empty ()) |
10304ef3 SDJ |
136 | --inf_data->any_syscall_count; |
137 | else | |
138 | { | |
e12c9b7a | 139 | for (int iter : c->syscalls_to_be_caught) |
10304ef3 | 140 | { |
b6f48cb0 | 141 | if (iter >= inf_data->syscalls_counts.size ()) |
10304ef3 SDJ |
142 | /* Shouldn't happen. */ |
143 | continue; | |
b6f48cb0 | 144 | --inf_data->syscalls_counts[iter]; |
10304ef3 SDJ |
145 | } |
146 | } | |
147 | ||
e99b03dc | 148 | return target_set_syscall_catchpoint (inferior_ptid.pid (), |
10304ef3 SDJ |
149 | inf_data->total_syscalls_count != 0, |
150 | inf_data->any_syscall_count, | |
649a140c | 151 | inf_data->syscalls_counts); |
10304ef3 SDJ |
152 | } |
153 | ||
154 | /* Implement the "breakpoint_hit" breakpoint_ops method for syscall | |
155 | catchpoints. */ | |
156 | ||
157 | static int | |
158 | breakpoint_hit_catch_syscall (const struct bp_location *bl, | |
bd522513 | 159 | const address_space *aspace, CORE_ADDR bp_addr, |
10304ef3 SDJ |
160 | const struct target_waitstatus *ws) |
161 | { | |
162 | /* We must check if we are catching specific syscalls in this | |
163 | breakpoint. If we are, then we must guarantee that the called | |
164 | syscall is the same syscall we are catching. */ | |
165 | int syscall_number = 0; | |
166 | const struct syscall_catchpoint *c | |
167 | = (const struct syscall_catchpoint *) bl->owner; | |
168 | ||
169 | if (ws->kind != TARGET_WAITKIND_SYSCALL_ENTRY | |
170 | && ws->kind != TARGET_WAITKIND_SYSCALL_RETURN) | |
171 | return 0; | |
172 | ||
173 | syscall_number = ws->value.syscall_number; | |
174 | ||
175 | /* Now, checking if the syscall is the same. */ | |
e12c9b7a | 176 | if (!c->syscalls_to_be_caught.empty ()) |
10304ef3 | 177 | { |
e12c9b7a | 178 | for (int iter : c->syscalls_to_be_caught) |
10304ef3 SDJ |
179 | if (syscall_number == iter) |
180 | return 1; | |
181 | ||
182 | return 0; | |
183 | } | |
184 | ||
185 | return 1; | |
186 | } | |
187 | ||
188 | /* Implement the "print_it" breakpoint_ops method for syscall | |
189 | catchpoints. */ | |
190 | ||
191 | static enum print_stop_action | |
192 | print_it_catch_syscall (bpstat bs) | |
193 | { | |
194 | struct ui_out *uiout = current_uiout; | |
195 | struct breakpoint *b = bs->breakpoint_at; | |
196 | /* These are needed because we want to know in which state a | |
197 | syscall is. It can be in the TARGET_WAITKIND_SYSCALL_ENTRY | |
198 | or TARGET_WAITKIND_SYSCALL_RETURN, and depending on it we | |
199 | must print "called syscall" or "returned from syscall". */ | |
200 | ptid_t ptid; | |
201 | struct target_waitstatus last; | |
202 | struct syscall s; | |
203 | struct gdbarch *gdbarch = bs->bp_location_at->gdbarch; | |
204 | ||
205 | get_last_target_status (&ptid, &last); | |
206 | ||
207 | get_syscall_by_number (gdbarch, last.value.syscall_number, &s); | |
208 | ||
209 | annotate_catchpoint (b->number); | |
f303dbd6 | 210 | maybe_print_thread_hit_breakpoint (uiout); |
10304ef3 SDJ |
211 | |
212 | if (b->disposition == disp_del) | |
112e8700 | 213 | uiout->text ("Temporary catchpoint "); |
10304ef3 | 214 | else |
112e8700 SM |
215 | uiout->text ("Catchpoint "); |
216 | if (uiout->is_mi_like_p ()) | |
10304ef3 | 217 | { |
112e8700 | 218 | uiout->field_string ("reason", |
10304ef3 SDJ |
219 | async_reason_lookup (last.kind == TARGET_WAITKIND_SYSCALL_ENTRY |
220 | ? EXEC_ASYNC_SYSCALL_ENTRY | |
221 | : EXEC_ASYNC_SYSCALL_RETURN)); | |
112e8700 | 222 | uiout->field_string ("disp", bpdisp_text (b->disposition)); |
10304ef3 | 223 | } |
112e8700 | 224 | uiout->field_int ("bkptno", b->number); |
10304ef3 SDJ |
225 | |
226 | if (last.kind == TARGET_WAITKIND_SYSCALL_ENTRY) | |
112e8700 | 227 | uiout->text (" (call to syscall "); |
10304ef3 | 228 | else |
112e8700 | 229 | uiout->text (" (returned from syscall "); |
10304ef3 | 230 | |
112e8700 SM |
231 | if (s.name == NULL || uiout->is_mi_like_p ()) |
232 | uiout->field_int ("syscall-number", last.value.syscall_number); | |
10304ef3 | 233 | if (s.name != NULL) |
112e8700 | 234 | uiout->field_string ("syscall-name", s.name); |
10304ef3 | 235 | |
112e8700 | 236 | uiout->text ("), "); |
10304ef3 SDJ |
237 | |
238 | return PRINT_SRC_AND_LOC; | |
239 | } | |
240 | ||
241 | /* Implement the "print_one" breakpoint_ops method for syscall | |
242 | catchpoints. */ | |
243 | ||
244 | static void | |
245 | print_one_catch_syscall (struct breakpoint *b, | |
246 | struct bp_location **last_loc) | |
247 | { | |
248 | struct syscall_catchpoint *c = (struct syscall_catchpoint *) b; | |
249 | struct value_print_options opts; | |
250 | struct ui_out *uiout = current_uiout; | |
251 | struct gdbarch *gdbarch = b->loc->gdbarch; | |
252 | ||
253 | get_user_print_options (&opts); | |
254 | /* Field 4, the address, is omitted (which makes the columns not | |
255 | line up too nicely with the headers, but the effect is relatively | |
256 | readable). */ | |
257 | if (opts.addressprint) | |
112e8700 | 258 | uiout->field_skip ("addr"); |
10304ef3 SDJ |
259 | annotate_field (5); |
260 | ||
e12c9b7a | 261 | if (c->syscalls_to_be_caught.size () > 1) |
112e8700 | 262 | uiout->text ("syscalls \""); |
10304ef3 | 263 | else |
112e8700 | 264 | uiout->text ("syscall \""); |
10304ef3 | 265 | |
e12c9b7a | 266 | if (!c->syscalls_to_be_caught.empty ()) |
10304ef3 | 267 | { |
10304ef3 SDJ |
268 | char *text = xstrprintf ("%s", ""); |
269 | ||
e12c9b7a | 270 | for (int iter : c->syscalls_to_be_caught) |
10304ef3 | 271 | { |
5b38f9c1 | 272 | char *previous_text = text; |
10304ef3 SDJ |
273 | struct syscall s; |
274 | get_syscall_by_number (gdbarch, iter, &s); | |
275 | ||
276 | if (s.name != NULL) | |
277 | text = xstrprintf ("%s%s, ", text, s.name); | |
278 | else | |
279 | text = xstrprintf ("%s%d, ", text, iter); | |
280 | ||
5b38f9c1 PW |
281 | /* We have to xfree previous_text because xstrprintf dynamically |
282 | allocates new space for text on every call. */ | |
283 | xfree (previous_text); | |
10304ef3 SDJ |
284 | } |
285 | /* Remove the last comma. */ | |
286 | text[strlen (text) - 2] = '\0'; | |
112e8700 | 287 | uiout->field_string ("what", text); |
5b38f9c1 PW |
288 | /* xfree last text. */ |
289 | xfree (text); | |
10304ef3 SDJ |
290 | } |
291 | else | |
112e8700 SM |
292 | uiout->field_string ("what", "<any syscall>"); |
293 | uiout->text ("\" "); | |
10304ef3 | 294 | |
112e8700 SM |
295 | if (uiout->is_mi_like_p ()) |
296 | uiout->field_string ("catch-type", "syscall"); | |
10304ef3 SDJ |
297 | } |
298 | ||
299 | /* Implement the "print_mention" breakpoint_ops method for syscall | |
300 | catchpoints. */ | |
301 | ||
302 | static void | |
303 | print_mention_catch_syscall (struct breakpoint *b) | |
304 | { | |
305 | struct syscall_catchpoint *c = (struct syscall_catchpoint *) b; | |
306 | struct gdbarch *gdbarch = b->loc->gdbarch; | |
307 | ||
e12c9b7a | 308 | if (!c->syscalls_to_be_caught.empty ()) |
10304ef3 | 309 | { |
e12c9b7a | 310 | if (c->syscalls_to_be_caught.size () > 1) |
10304ef3 SDJ |
311 | printf_filtered (_("Catchpoint %d (syscalls"), b->number); |
312 | else | |
313 | printf_filtered (_("Catchpoint %d (syscall"), b->number); | |
314 | ||
e12c9b7a | 315 | for (int iter : c->syscalls_to_be_caught) |
10304ef3 SDJ |
316 | { |
317 | struct syscall s; | |
318 | get_syscall_by_number (gdbarch, iter, &s); | |
319 | ||
e12c9b7a | 320 | if (s.name != NULL) |
10304ef3 SDJ |
321 | printf_filtered (" '%s' [%d]", s.name, s.number); |
322 | else | |
323 | printf_filtered (" %d", s.number); | |
324 | } | |
325 | printf_filtered (")"); | |
326 | } | |
327 | else | |
328 | printf_filtered (_("Catchpoint %d (any syscall)"), | |
329 | b->number); | |
330 | } | |
331 | ||
332 | /* Implement the "print_recreate" breakpoint_ops method for syscall | |
333 | catchpoints. */ | |
334 | ||
335 | static void | |
336 | print_recreate_catch_syscall (struct breakpoint *b, struct ui_file *fp) | |
337 | { | |
338 | struct syscall_catchpoint *c = (struct syscall_catchpoint *) b; | |
339 | struct gdbarch *gdbarch = b->loc->gdbarch; | |
340 | ||
341 | fprintf_unfiltered (fp, "catch syscall"); | |
342 | ||
e12c9b7a | 343 | for (int iter : c->syscalls_to_be_caught) |
10304ef3 | 344 | { |
e12c9b7a | 345 | struct syscall s; |
10304ef3 | 346 | |
e12c9b7a TT |
347 | get_syscall_by_number (gdbarch, iter, &s); |
348 | if (s.name != NULL) | |
349 | fprintf_unfiltered (fp, " %s", s.name); | |
350 | else | |
351 | fprintf_unfiltered (fp, " %d", s.number); | |
10304ef3 | 352 | } |
e12c9b7a | 353 | |
10304ef3 SDJ |
354 | print_recreate_thread (b, fp); |
355 | } | |
356 | ||
357 | /* The breakpoint_ops structure to be used in syscall catchpoints. */ | |
358 | ||
359 | static struct breakpoint_ops catch_syscall_breakpoint_ops; | |
360 | ||
361 | /* Returns non-zero if 'b' is a syscall catchpoint. */ | |
362 | ||
363 | static int | |
364 | syscall_catchpoint_p (struct breakpoint *b) | |
365 | { | |
366 | return (b->ops == &catch_syscall_breakpoint_ops); | |
367 | } | |
368 | ||
369 | static void | |
e12c9b7a | 370 | create_syscall_event_catchpoint (int tempflag, std::vector<int> &&filter, |
10304ef3 SDJ |
371 | const struct breakpoint_ops *ops) |
372 | { | |
10304ef3 SDJ |
373 | struct gdbarch *gdbarch = get_current_arch (); |
374 | ||
b270e6f9 TT |
375 | std::unique_ptr<syscall_catchpoint> c (new syscall_catchpoint ()); |
376 | init_catchpoint (c.get (), gdbarch, tempflag, NULL, ops); | |
2f5404b3 | 377 | c->syscalls_to_be_caught = std::move (filter); |
10304ef3 | 378 | |
b270e6f9 | 379 | install_breakpoint (0, std::move (c), 1); |
10304ef3 SDJ |
380 | } |
381 | ||
e12c9b7a TT |
382 | /* Splits the argument using space as delimiter. */ |
383 | ||
384 | static std::vector<int> | |
eb4c3f4a | 385 | catch_syscall_split_args (const char *arg) |
10304ef3 | 386 | { |
e12c9b7a | 387 | std::vector<int> result; |
10304ef3 SDJ |
388 | struct gdbarch *gdbarch = target_gdbarch (); |
389 | ||
390 | while (*arg != '\0') | |
391 | { | |
392 | int i, syscall_number; | |
393 | char *endptr; | |
394 | char cur_name[128]; | |
395 | struct syscall s; | |
396 | ||
397 | /* Skip whitespace. */ | |
398 | arg = skip_spaces (arg); | |
399 | ||
400 | for (i = 0; i < 127 && arg[i] && !isspace (arg[i]); ++i) | |
401 | cur_name[i] = arg[i]; | |
402 | cur_name[i] = '\0'; | |
403 | arg += i; | |
404 | ||
e3487908 | 405 | /* Check if the user provided a syscall name, group, or a number. */ |
10304ef3 SDJ |
406 | syscall_number = (int) strtol (cur_name, &endptr, 0); |
407 | if (*endptr == '\0') | |
e3487908 GKB |
408 | { |
409 | get_syscall_by_number (gdbarch, syscall_number, &s); | |
e12c9b7a | 410 | result.push_back (s.number); |
e3487908 GKB |
411 | } |
412 | else if (startswith (cur_name, "g:") | |
413 | || startswith (cur_name, "group:")) | |
414 | { | |
415 | /* We have a syscall group. Let's expand it into a syscall | |
416 | list before inserting. */ | |
e3487908 GKB |
417 | const char *group_name; |
418 | ||
419 | /* Skip over "g:" and "group:" prefix strings. */ | |
420 | group_name = strchr (cur_name, ':') + 1; | |
421 | ||
4794efbf | 422 | if (!get_syscalls_by_group (gdbarch, group_name, &result)) |
e3487908 | 423 | error (_("Unknown syscall group '%s'."), group_name); |
e3487908 | 424 | } |
10304ef3 SDJ |
425 | else |
426 | { | |
e9076973 JB |
427 | /* We have a name. Let's check if it's valid and fetch a |
428 | list of matching numbers. */ | |
429 | if (!get_syscalls_by_name (gdbarch, cur_name, &result)) | |
10304ef3 SDJ |
430 | /* Here we have to issue an error instead of a warning, |
431 | because GDB cannot do anything useful if there's no | |
432 | syscall number to be caught. */ | |
433 | error (_("Unknown syscall name '%s'."), cur_name); | |
e3487908 | 434 | } |
10304ef3 SDJ |
435 | } |
436 | ||
10304ef3 SDJ |
437 | return result; |
438 | } | |
439 | ||
440 | /* Implement the "catch syscall" command. */ | |
441 | ||
442 | static void | |
eb4c3f4a | 443 | catch_syscall_command_1 (const char *arg, int from_tty, |
10304ef3 SDJ |
444 | struct cmd_list_element *command) |
445 | { | |
446 | int tempflag; | |
e12c9b7a | 447 | std::vector<int> filter; |
10304ef3 SDJ |
448 | struct syscall s; |
449 | struct gdbarch *gdbarch = get_current_arch (); | |
450 | ||
451 | /* Checking if the feature if supported. */ | |
452 | if (gdbarch_get_syscall_number_p (gdbarch) == 0) | |
453 | error (_("The feature 'catch syscall' is not supported on \ | |
454 | this architecture yet.")); | |
455 | ||
456 | tempflag = get_cmd_context (command) == CATCH_TEMPORARY; | |
457 | ||
458 | arg = skip_spaces (arg); | |
459 | ||
460 | /* We need to do this first "dummy" translation in order | |
461 | to get the syscall XML file loaded or, most important, | |
462 | to display a warning to the user if there's no XML file | |
463 | for his/her architecture. */ | |
464 | get_syscall_by_number (gdbarch, 0, &s); | |
465 | ||
466 | /* The allowed syntax is: | |
467 | catch syscall | |
468 | catch syscall <name | number> [<name | number> ... <name | number>] | |
469 | ||
470 | Let's check if there's a syscall name. */ | |
471 | ||
472 | if (arg != NULL) | |
473 | filter = catch_syscall_split_args (arg); | |
10304ef3 | 474 | |
e12c9b7a | 475 | create_syscall_event_catchpoint (tempflag, std::move (filter), |
10304ef3 SDJ |
476 | &catch_syscall_breakpoint_ops); |
477 | } | |
478 | ||
479 | ||
480 | /* Returns 0 if 'bp' is NOT a syscall catchpoint, | |
481 | non-zero otherwise. */ | |
482 | static int | |
483 | is_syscall_catchpoint_enabled (struct breakpoint *bp) | |
484 | { | |
485 | if (syscall_catchpoint_p (bp) | |
486 | && bp->enable_state != bp_disabled | |
487 | && bp->enable_state != bp_call_disabled) | |
488 | return 1; | |
489 | else | |
490 | return 0; | |
491 | } | |
492 | ||
493 | int | |
494 | catch_syscall_enabled (void) | |
495 | { | |
496 | struct catch_syscall_inferior_data *inf_data | |
497 | = get_catch_syscall_inferior_data (current_inferior ()); | |
498 | ||
499 | return inf_data->total_syscalls_count != 0; | |
500 | } | |
501 | ||
502 | /* Helper function for catching_syscall_number. If B is a syscall | |
503 | catchpoint for SYSCALL_NUMBER, return 1 (which will make | |
504 | 'breakpoint_find_if' return). Otherwise, return 0. */ | |
505 | ||
506 | static int | |
507 | catching_syscall_number_1 (struct breakpoint *b, | |
508 | void *data) | |
509 | { | |
510 | int syscall_number = (int) (uintptr_t) data; | |
511 | ||
512 | if (is_syscall_catchpoint_enabled (b)) | |
513 | { | |
514 | struct syscall_catchpoint *c = (struct syscall_catchpoint *) b; | |
515 | ||
e12c9b7a | 516 | if (!c->syscalls_to_be_caught.empty ()) |
10304ef3 | 517 | { |
e12c9b7a | 518 | for (int iter : c->syscalls_to_be_caught) |
10304ef3 SDJ |
519 | if (syscall_number == iter) |
520 | return 1; | |
521 | } | |
522 | else | |
523 | return 1; | |
524 | } | |
525 | ||
526 | return 0; | |
527 | } | |
528 | ||
529 | int | |
530 | catching_syscall_number (int syscall_number) | |
531 | { | |
532 | struct breakpoint *b = breakpoint_find_if (catching_syscall_number_1, | |
533 | (void *) (uintptr_t) syscall_number); | |
534 | ||
535 | return b != NULL; | |
536 | } | |
537 | ||
538 | /* Complete syscall names. Used by "catch syscall". */ | |
eb3ff9a5 PA |
539 | |
540 | static void | |
10304ef3 | 541 | catch_syscall_completer (struct cmd_list_element *cmd, |
eb3ff9a5 | 542 | completion_tracker &tracker, |
10304ef3 SDJ |
543 | const char *text, const char *word) |
544 | { | |
e3487908 | 545 | struct gdbarch *gdbarch = get_current_arch (); |
3d415c26 | 546 | gdb::unique_xmalloc_ptr<const char *> group_list; |
e3487908 | 547 | const char *prefix; |
e3487908 GKB |
548 | |
549 | /* Completion considers ':' to be a word separator, so we use this to | |
550 | verify whether the previous word was a group prefix. If so, we | |
551 | build the completion list using group names only. */ | |
552 | for (prefix = word; prefix != text && prefix[-1] != ' '; prefix--) | |
553 | ; | |
554 | ||
555 | if (startswith (prefix, "g:") || startswith (prefix, "group:")) | |
556 | { | |
557 | /* Perform completion inside 'group:' namespace only. */ | |
3d415c26 | 558 | group_list.reset (get_syscall_group_names (gdbarch)); |
eb3ff9a5 | 559 | if (group_list != NULL) |
3d415c26 | 560 | complete_on_enum (tracker, group_list.get (), word, word); |
e3487908 GKB |
561 | } |
562 | else | |
563 | { | |
564 | /* Complete with both, syscall names and groups. */ | |
3d415c26 TT |
565 | gdb::unique_xmalloc_ptr<const char *> syscall_list |
566 | (get_syscall_names (gdbarch)); | |
567 | group_list.reset (get_syscall_group_names (gdbarch)); | |
568 | ||
569 | const char **group_ptr = group_list.get (); | |
570 | ||
571 | /* Hold on to strings while we're using them. */ | |
572 | std::vector<std::string> holders; | |
e3487908 GKB |
573 | |
574 | /* Append "group:" prefix to syscall groups. */ | |
9a93831c SM |
575 | for (int i = 0; group_ptr[i] != NULL; i++) |
576 | holders.push_back (string_printf ("group:%s", group_ptr[i])); | |
e3487908 | 577 | |
9a93831c SM |
578 | for (int i = 0; group_ptr[i] != NULL; i++) |
579 | group_ptr[i] = holders[i].c_str (); | |
e3487908 | 580 | |
eb3ff9a5 | 581 | if (syscall_list != NULL) |
3d415c26 | 582 | complete_on_enum (tracker, syscall_list.get (), word, word); |
eb3ff9a5 | 583 | if (group_list != NULL) |
3d415c26 | 584 | complete_on_enum (tracker, group_ptr, word, word); |
e3487908 | 585 | } |
10304ef3 SDJ |
586 | } |
587 | ||
588 | static void | |
589 | clear_syscall_counts (struct inferior *inf) | |
590 | { | |
591 | struct catch_syscall_inferior_data *inf_data | |
592 | = get_catch_syscall_inferior_data (inf); | |
593 | ||
594 | inf_data->total_syscalls_count = 0; | |
595 | inf_data->any_syscall_count = 0; | |
b6f48cb0 | 596 | inf_data->syscalls_counts.clear (); |
10304ef3 SDJ |
597 | } |
598 | ||
599 | static void | |
600 | initialize_syscall_catchpoint_ops (void) | |
601 | { | |
602 | struct breakpoint_ops *ops; | |
603 | ||
604 | initialize_breakpoint_ops (); | |
605 | ||
606 | /* Syscall catchpoints. */ | |
607 | ops = &catch_syscall_breakpoint_ops; | |
608 | *ops = base_breakpoint_ops; | |
10304ef3 SDJ |
609 | ops->insert_location = insert_catch_syscall; |
610 | ops->remove_location = remove_catch_syscall; | |
611 | ops->breakpoint_hit = breakpoint_hit_catch_syscall; | |
612 | ops->print_it = print_it_catch_syscall; | |
613 | ops->print_one = print_one_catch_syscall; | |
614 | ops->print_mention = print_mention_catch_syscall; | |
615 | ops->print_recreate = print_recreate_catch_syscall; | |
616 | } | |
617 | ||
10304ef3 SDJ |
618 | void |
619 | _initialize_break_catch_syscall (void) | |
620 | { | |
621 | initialize_syscall_catchpoint_ops (); | |
622 | ||
76727919 | 623 | gdb::observers::inferior_exit.attach (clear_syscall_counts); |
10304ef3 SDJ |
624 | catch_syscall_inferior_data |
625 | = register_inferior_data_with_cleanup (NULL, | |
626 | catch_syscall_inferior_data_cleanup); | |
627 | ||
628 | add_catch_command ("syscall", _("\ | |
e3487908 GKB |
629 | Catch system calls by their names, groups and/or numbers.\n\ |
630 | Arguments say which system calls to catch. If no arguments are given,\n\ | |
631 | every system call will be caught. Arguments, if given, should be one\n\ | |
632 | or more system call names (if your system supports that), system call\n\ | |
633 | groups or system call numbers."), | |
10304ef3 SDJ |
634 | catch_syscall_command_1, |
635 | catch_syscall_completer, | |
636 | CATCH_PERMANENT, | |
637 | CATCH_TEMPORARY); | |
638 | } |