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