1 /* Everything about syscall catchpoints, for GDB.
3 Copyright (C) 2009-2015 Free Software Foundation, Inc.
5 This file is part of GDB.
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.
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.
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/>. */
22 #include "breakpoint.h"
25 #include "cli/cli-utils.h"
27 #include "mi/mi-common.h"
29 #include "arch-utils.h"
31 #include "xml-syscall.h"
33 /* An instance of this type is used to represent a syscall catchpoint.
34 It includes a "struct breakpoint" as a kind of base class; users
35 downcast to "struct breakpoint *" when needed. A breakpoint is
36 really of this type iff its ops pointer points to
37 CATCH_SYSCALL_BREAKPOINT_OPS. */
39 struct syscall_catchpoint
42 struct breakpoint base
;
44 /* Syscall numbers used for the 'catch syscall' feature. If no
45 syscall has been specified for filtering, its value is NULL.
46 Otherwise, it holds a list of all syscalls to be caught. The
47 list elements are allocated with xmalloc. */
48 VEC(int) *syscalls_to_be_caught
;
51 /* Implement the "dtor" breakpoint_ops method for syscall
55 dtor_catch_syscall (struct breakpoint
*b
)
57 struct syscall_catchpoint
*c
= (struct syscall_catchpoint
*) b
;
59 VEC_free (int, c
->syscalls_to_be_caught
);
61 base_breakpoint_ops
.dtor (b
);
64 static const struct inferior_data
*catch_syscall_inferior_data
= NULL
;
66 struct catch_syscall_inferior_data
68 /* We keep a count of the number of times the user has requested a
69 particular syscall to be tracked, and pass this information to the
70 target. This lets capable targets implement filtering directly. */
72 /* Number of times that "any" syscall is requested. */
73 int any_syscall_count
;
75 /* Count of each system call. */
76 VEC(int) *syscalls_counts
;
78 /* This counts all syscall catch requests, so we can readily determine
79 if any catching is necessary. */
80 int total_syscalls_count
;
83 static struct catch_syscall_inferior_data
*
84 get_catch_syscall_inferior_data (struct inferior
*inf
)
86 struct catch_syscall_inferior_data
*inf_data
;
88 inf_data
= inferior_data (inf
, catch_syscall_inferior_data
);
91 inf_data
= XCNEW (struct catch_syscall_inferior_data
);
92 set_inferior_data (inf
, catch_syscall_inferior_data
, inf_data
);
99 catch_syscall_inferior_data_cleanup (struct inferior
*inf
, void *arg
)
105 /* Implement the "insert" breakpoint_ops method for syscall
109 insert_catch_syscall (struct bp_location
*bl
)
111 struct syscall_catchpoint
*c
= (struct syscall_catchpoint
*) bl
->owner
;
112 struct inferior
*inf
= current_inferior ();
113 struct catch_syscall_inferior_data
*inf_data
114 = get_catch_syscall_inferior_data (inf
);
116 ++inf_data
->total_syscalls_count
;
117 if (!c
->syscalls_to_be_caught
)
118 ++inf_data
->any_syscall_count
;
124 VEC_iterate (int, c
->syscalls_to_be_caught
, i
, iter
);
129 if (iter
>= VEC_length (int, inf_data
->syscalls_counts
))
131 int old_size
= VEC_length (int, inf_data
->syscalls_counts
);
132 uintptr_t vec_addr_offset
133 = old_size
* ((uintptr_t) sizeof (int));
135 VEC_safe_grow (int, inf_data
->syscalls_counts
, iter
+ 1);
136 vec_addr
= ((uintptr_t) VEC_address (int,
137 inf_data
->syscalls_counts
)
139 memset ((void *) vec_addr
, 0,
140 (iter
+ 1 - old_size
) * sizeof (int));
142 elem
= VEC_index (int, inf_data
->syscalls_counts
, iter
);
143 VEC_replace (int, inf_data
->syscalls_counts
, iter
, ++elem
);
147 return target_set_syscall_catchpoint (ptid_get_pid (inferior_ptid
),
148 inf_data
->total_syscalls_count
!= 0,
149 inf_data
->any_syscall_count
,
151 inf_data
->syscalls_counts
),
153 inf_data
->syscalls_counts
));
156 /* Implement the "remove" breakpoint_ops method for syscall
160 remove_catch_syscall (struct bp_location
*bl
)
162 struct syscall_catchpoint
*c
= (struct syscall_catchpoint
*) bl
->owner
;
163 struct inferior
*inf
= current_inferior ();
164 struct catch_syscall_inferior_data
*inf_data
165 = get_catch_syscall_inferior_data (inf
);
167 --inf_data
->total_syscalls_count
;
168 if (!c
->syscalls_to_be_caught
)
169 --inf_data
->any_syscall_count
;
175 VEC_iterate (int, c
->syscalls_to_be_caught
, i
, iter
);
179 if (iter
>= VEC_length (int, inf_data
->syscalls_counts
))
180 /* Shouldn't happen. */
182 elem
= VEC_index (int, inf_data
->syscalls_counts
, iter
);
183 VEC_replace (int, inf_data
->syscalls_counts
, iter
, --elem
);
187 return target_set_syscall_catchpoint (ptid_get_pid (inferior_ptid
),
188 inf_data
->total_syscalls_count
!= 0,
189 inf_data
->any_syscall_count
,
191 inf_data
->syscalls_counts
),
193 inf_data
->syscalls_counts
));
196 /* Implement the "breakpoint_hit" breakpoint_ops method for syscall
200 breakpoint_hit_catch_syscall (const struct bp_location
*bl
,
201 struct address_space
*aspace
, CORE_ADDR bp_addr
,
202 const struct target_waitstatus
*ws
)
204 /* We must check if we are catching specific syscalls in this
205 breakpoint. If we are, then we must guarantee that the called
206 syscall is the same syscall we are catching. */
207 int syscall_number
= 0;
208 const struct syscall_catchpoint
*c
209 = (const struct syscall_catchpoint
*) bl
->owner
;
211 if (ws
->kind
!= TARGET_WAITKIND_SYSCALL_ENTRY
212 && ws
->kind
!= TARGET_WAITKIND_SYSCALL_RETURN
)
215 syscall_number
= ws
->value
.syscall_number
;
217 /* Now, checking if the syscall is the same. */
218 if (c
->syscalls_to_be_caught
)
223 VEC_iterate (int, c
->syscalls_to_be_caught
, i
, iter
);
225 if (syscall_number
== iter
)
234 /* Implement the "print_it" breakpoint_ops method for syscall
237 static enum print_stop_action
238 print_it_catch_syscall (bpstat bs
)
240 struct ui_out
*uiout
= current_uiout
;
241 struct breakpoint
*b
= bs
->breakpoint_at
;
242 /* These are needed because we want to know in which state a
243 syscall is. It can be in the TARGET_WAITKIND_SYSCALL_ENTRY
244 or TARGET_WAITKIND_SYSCALL_RETURN, and depending on it we
245 must print "called syscall" or "returned from syscall". */
247 struct target_waitstatus last
;
249 struct gdbarch
*gdbarch
= bs
->bp_location_at
->gdbarch
;
251 get_last_target_status (&ptid
, &last
);
253 get_syscall_by_number (gdbarch
, last
.value
.syscall_number
, &s
);
255 annotate_catchpoint (b
->number
);
257 if (b
->disposition
== disp_del
)
258 ui_out_text (uiout
, "\nTemporary catchpoint ");
260 ui_out_text (uiout
, "\nCatchpoint ");
261 if (ui_out_is_mi_like_p (uiout
))
263 ui_out_field_string (uiout
, "reason",
264 async_reason_lookup (last
.kind
== TARGET_WAITKIND_SYSCALL_ENTRY
265 ? EXEC_ASYNC_SYSCALL_ENTRY
266 : EXEC_ASYNC_SYSCALL_RETURN
));
267 ui_out_field_string (uiout
, "disp", bpdisp_text (b
->disposition
));
269 ui_out_field_int (uiout
, "bkptno", b
->number
);
271 if (last
.kind
== TARGET_WAITKIND_SYSCALL_ENTRY
)
272 ui_out_text (uiout
, " (call to syscall ");
274 ui_out_text (uiout
, " (returned from syscall ");
276 if (s
.name
== NULL
|| ui_out_is_mi_like_p (uiout
))
277 ui_out_field_int (uiout
, "syscall-number", last
.value
.syscall_number
);
279 ui_out_field_string (uiout
, "syscall-name", s
.name
);
281 ui_out_text (uiout
, "), ");
283 return PRINT_SRC_AND_LOC
;
286 /* Implement the "print_one" breakpoint_ops method for syscall
290 print_one_catch_syscall (struct breakpoint
*b
,
291 struct bp_location
**last_loc
)
293 struct syscall_catchpoint
*c
= (struct syscall_catchpoint
*) b
;
294 struct value_print_options opts
;
295 struct ui_out
*uiout
= current_uiout
;
296 struct gdbarch
*gdbarch
= b
->loc
->gdbarch
;
298 get_user_print_options (&opts
);
299 /* Field 4, the address, is omitted (which makes the columns not
300 line up too nicely with the headers, but the effect is relatively
302 if (opts
.addressprint
)
303 ui_out_field_skip (uiout
, "addr");
306 if (c
->syscalls_to_be_caught
307 && VEC_length (int, c
->syscalls_to_be_caught
) > 1)
308 ui_out_text (uiout
, "syscalls \"");
310 ui_out_text (uiout
, "syscall \"");
312 if (c
->syscalls_to_be_caught
)
315 char *text
= xstrprintf ("%s", "");
318 VEC_iterate (int, c
->syscalls_to_be_caught
, i
, iter
);
323 get_syscall_by_number (gdbarch
, iter
, &s
);
326 text
= xstrprintf ("%s%s, ", text
, s
.name
);
328 text
= xstrprintf ("%s%d, ", text
, iter
);
330 /* We have to xfree the last 'text' (now stored at 'x')
331 because xstrprintf dynamically allocates new space for it
335 /* Remove the last comma. */
336 text
[strlen (text
) - 2] = '\0';
337 ui_out_field_string (uiout
, "what", text
);
340 ui_out_field_string (uiout
, "what", "<any syscall>");
341 ui_out_text (uiout
, "\" ");
343 if (ui_out_is_mi_like_p (uiout
))
344 ui_out_field_string (uiout
, "catch-type", "syscall");
347 /* Implement the "print_mention" breakpoint_ops method for syscall
351 print_mention_catch_syscall (struct breakpoint
*b
)
353 struct syscall_catchpoint
*c
= (struct syscall_catchpoint
*) b
;
354 struct gdbarch
*gdbarch
= b
->loc
->gdbarch
;
356 if (c
->syscalls_to_be_caught
)
360 if (VEC_length (int, c
->syscalls_to_be_caught
) > 1)
361 printf_filtered (_("Catchpoint %d (syscalls"), b
->number
);
363 printf_filtered (_("Catchpoint %d (syscall"), b
->number
);
366 VEC_iterate (int, c
->syscalls_to_be_caught
, i
, iter
);
370 get_syscall_by_number (gdbarch
, iter
, &s
);
373 printf_filtered (" '%s' [%d]", s
.name
, s
.number
);
375 printf_filtered (" %d", s
.number
);
377 printf_filtered (")");
380 printf_filtered (_("Catchpoint %d (any syscall)"),
384 /* Implement the "print_recreate" breakpoint_ops method for syscall
388 print_recreate_catch_syscall (struct breakpoint
*b
, struct ui_file
*fp
)
390 struct syscall_catchpoint
*c
= (struct syscall_catchpoint
*) b
;
391 struct gdbarch
*gdbarch
= b
->loc
->gdbarch
;
393 fprintf_unfiltered (fp
, "catch syscall");
395 if (c
->syscalls_to_be_caught
)
400 VEC_iterate (int, c
->syscalls_to_be_caught
, i
, iter
);
405 get_syscall_by_number (gdbarch
, iter
, &s
);
407 fprintf_unfiltered (fp
, " %s", s
.name
);
409 fprintf_unfiltered (fp
, " %d", s
.number
);
412 print_recreate_thread (b
, fp
);
415 /* The breakpoint_ops structure to be used in syscall catchpoints. */
417 static struct breakpoint_ops catch_syscall_breakpoint_ops
;
419 /* Returns non-zero if 'b' is a syscall catchpoint. */
422 syscall_catchpoint_p (struct breakpoint
*b
)
424 return (b
->ops
== &catch_syscall_breakpoint_ops
);
428 create_syscall_event_catchpoint (int tempflag
, VEC(int) *filter
,
429 const struct breakpoint_ops
*ops
)
431 struct syscall_catchpoint
*c
;
432 struct gdbarch
*gdbarch
= get_current_arch ();
434 c
= XNEW (struct syscall_catchpoint
);
435 init_catchpoint (&c
->base
, gdbarch
, tempflag
, NULL
, ops
);
436 c
->syscalls_to_be_caught
= filter
;
438 install_breakpoint (0, &c
->base
, 1);
441 /* Splits the argument using space as delimiter. Returns an xmalloc'd
442 filter list, or NULL if no filtering is required. */
444 catch_syscall_split_args (char *arg
)
446 VEC(int) *result
= NULL
;
447 struct cleanup
*cleanup
= make_cleanup (VEC_cleanup (int), &result
);
448 struct gdbarch
*gdbarch
= target_gdbarch ();
452 int i
, syscall_number
;
457 /* Skip whitespace. */
458 arg
= skip_spaces (arg
);
460 for (i
= 0; i
< 127 && arg
[i
] && !isspace (arg
[i
]); ++i
)
461 cur_name
[i
] = arg
[i
];
465 /* Check if the user provided a syscall name or a number. */
466 syscall_number
= (int) strtol (cur_name
, &endptr
, 0);
468 get_syscall_by_number (gdbarch
, syscall_number
, &s
);
471 /* We have a name. Let's check if it's valid and convert it
473 get_syscall_by_name (gdbarch
, cur_name
, &s
);
475 if (s
.number
== UNKNOWN_SYSCALL
)
476 /* Here we have to issue an error instead of a warning,
477 because GDB cannot do anything useful if there's no
478 syscall number to be caught. */
479 error (_("Unknown syscall name '%s'."), cur_name
);
482 /* Ok, it's valid. */
483 VEC_safe_push (int, result
, s
.number
);
486 discard_cleanups (cleanup
);
490 /* Implement the "catch syscall" command. */
493 catch_syscall_command_1 (char *arg
, int from_tty
,
494 struct cmd_list_element
*command
)
499 struct gdbarch
*gdbarch
= get_current_arch ();
501 /* Checking if the feature if supported. */
502 if (gdbarch_get_syscall_number_p (gdbarch
) == 0)
503 error (_("The feature 'catch syscall' is not supported on \
504 this architecture yet."));
506 tempflag
= get_cmd_context (command
) == CATCH_TEMPORARY
;
508 arg
= skip_spaces (arg
);
510 /* We need to do this first "dummy" translation in order
511 to get the syscall XML file loaded or, most important,
512 to display a warning to the user if there's no XML file
513 for his/her architecture. */
514 get_syscall_by_number (gdbarch
, 0, &s
);
516 /* The allowed syntax is:
518 catch syscall <name | number> [<name | number> ... <name | number>]
520 Let's check if there's a syscall name. */
523 filter
= catch_syscall_split_args (arg
);
527 create_syscall_event_catchpoint (tempflag
, filter
,
528 &catch_syscall_breakpoint_ops
);
532 /* Returns 0 if 'bp' is NOT a syscall catchpoint,
533 non-zero otherwise. */
535 is_syscall_catchpoint_enabled (struct breakpoint
*bp
)
537 if (syscall_catchpoint_p (bp
)
538 && bp
->enable_state
!= bp_disabled
539 && bp
->enable_state
!= bp_call_disabled
)
546 catch_syscall_enabled (void)
548 struct catch_syscall_inferior_data
*inf_data
549 = get_catch_syscall_inferior_data (current_inferior ());
551 return inf_data
->total_syscalls_count
!= 0;
554 /* Helper function for catching_syscall_number. If B is a syscall
555 catchpoint for SYSCALL_NUMBER, return 1 (which will make
556 'breakpoint_find_if' return). Otherwise, return 0. */
559 catching_syscall_number_1 (struct breakpoint
*b
,
562 int syscall_number
= (int) (uintptr_t) data
;
564 if (is_syscall_catchpoint_enabled (b
))
566 struct syscall_catchpoint
*c
= (struct syscall_catchpoint
*) b
;
568 if (c
->syscalls_to_be_caught
)
572 VEC_iterate (int, c
->syscalls_to_be_caught
, i
, iter
);
574 if (syscall_number
== iter
)
585 catching_syscall_number (int syscall_number
)
587 struct breakpoint
*b
= breakpoint_find_if (catching_syscall_number_1
,
588 (void *) (uintptr_t) syscall_number
);
593 /* Complete syscall names. Used by "catch syscall". */
594 static VEC (char_ptr
) *
595 catch_syscall_completer (struct cmd_list_element
*cmd
,
596 const char *text
, const char *word
)
598 const char **list
= get_syscall_names (get_current_arch ());
599 VEC (char_ptr
) *retlist
600 = (list
== NULL
) ? NULL
: complete_on_enum (list
, word
, word
);
607 clear_syscall_counts (struct inferior
*inf
)
609 struct catch_syscall_inferior_data
*inf_data
610 = get_catch_syscall_inferior_data (inf
);
612 inf_data
->total_syscalls_count
= 0;
613 inf_data
->any_syscall_count
= 0;
614 VEC_free (int, inf_data
->syscalls_counts
);
618 initialize_syscall_catchpoint_ops (void)
620 struct breakpoint_ops
*ops
;
622 initialize_breakpoint_ops ();
624 /* Syscall catchpoints. */
625 ops
= &catch_syscall_breakpoint_ops
;
626 *ops
= base_breakpoint_ops
;
627 ops
->dtor
= dtor_catch_syscall
;
628 ops
->insert_location
= insert_catch_syscall
;
629 ops
->remove_location
= remove_catch_syscall
;
630 ops
->breakpoint_hit
= breakpoint_hit_catch_syscall
;
631 ops
->print_it
= print_it_catch_syscall
;
632 ops
->print_one
= print_one_catch_syscall
;
633 ops
->print_mention
= print_mention_catch_syscall
;
634 ops
->print_recreate
= print_recreate_catch_syscall
;
637 initialize_file_ftype _initialize_break_catch_syscall
;
640 _initialize_break_catch_syscall (void)
642 initialize_syscall_catchpoint_ops ();
644 observer_attach_inferior_exit (clear_syscall_counts
);
645 catch_syscall_inferior_data
646 = register_inferior_data_with_cleanup (NULL
,
647 catch_syscall_inferior_data_cleanup
);
649 add_catch_command ("syscall", _("\
650 Catch system calls by their names and/or numbers.\n\
651 Arguments say which system calls to catch. If no arguments\n\
652 are given, every system call will be caught.\n\
653 Arguments, if given, should be one or more system call names\n\
654 (if your system supports that), or system call numbers."),
655 catch_syscall_command_1
,
656 catch_syscall_completer
,