1 /* Skipping uninteresting files and functions while stepping.
3 Copyright (C) 2011-2016 Free Software Foundation, Inc.
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 3 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>. */
26 #include "completer.h"
28 #include "cli/cli-utils.h"
29 #include "arch-utils.h"
32 #include "breakpoint.h" /* for get_sal_arch () */
34 #include "filenames.h"
36 #include "gdb_regex.h"
42 /* Non-zero if FILE is a glob-style pattern.
43 Otherewise it is the plain file name (possibly with directories). */
46 /* The name of the file or NULL.
47 The skiplist entry owns this pointer. */
50 /* Non-zero if FUNCTION is a regexp.
51 Otherwise it is a plain function name (possibly with arguments,
53 int function_is_regexp;
55 /* The name of the function or NULL.
56 The skiplist entry owns this pointer. */
59 /* If this is a function regexp, the compiled form. */
60 regex_t compiled_function_regexp;
62 /* Non-zero if the function regexp has been compiled. */
63 int compiled_function_regexp_is_valid;
67 struct skiplist_entry *next;
70 static void add_skiplist_entry (struct skiplist_entry *e);
72 static struct skiplist_entry *skiplist_entry_chain;
73 static int skiplist_entry_count;
75 #define ALL_SKIPLIST_ENTRIES(E) \
76 for (E = skiplist_entry_chain; E; E = E->next)
78 #define ALL_SKIPLIST_ENTRIES_SAFE(E,TMP) \
79 for (E = skiplist_entry_chain; \
80 E ? (TMP = E->next, 1) : 0; \
83 /* Create a skip object. */
85 static struct skiplist_entry *
86 make_skip_entry (int file_is_glob, const char *file,
87 int function_is_regexp, const char *function)
89 struct skiplist_entry *e = XCNEW (struct skiplist_entry);
91 gdb_assert (file != NULL || function != NULL);
93 gdb_assert (file != NULL);
94 if (function_is_regexp)
95 gdb_assert (function != NULL);
98 e->file = xstrdup (file);
100 e->function = xstrdup (function);
101 e->file_is_glob = file_is_glob;
102 e->function_is_regexp = function_is_regexp;
108 /* Free a skiplist entry. */
111 free_skiplist_entry (struct skiplist_entry *e)
115 if (e->function_is_regexp && e->compiled_function_regexp_is_valid)
116 regfree (&e->compiled_function_regexp);
120 /* Wrapper to free_skiplist_entry for use as a cleanup. */
123 free_skiplist_entry_cleanup (void *e)
125 free_skiplist_entry ((struct skiplist_entry *) e);
128 /* Create a cleanup to free skiplist entry E. */
130 static struct cleanup *
131 make_free_skiplist_entry_cleanup (struct skiplist_entry *e)
133 return make_cleanup (free_skiplist_entry_cleanup, e);
137 skip_file_command (char *arg, int from_tty)
139 struct symtab *symtab;
140 const char *filename = NULL;
142 /* If no argument was given, try to default to the last
143 displayed codepoint. */
146 symtab = get_last_displayed_symtab ();
148 error (_("No default file now."));
150 /* It is not a typo, symtab_to_filename_for_display woule be needlessly
152 filename = symtab_to_fullname (symtab);
157 add_skiplist_entry (make_skip_entry (0, filename, 0, NULL));
159 printf_filtered (_("File %s will be skipped when stepping.\n"), filename);
162 /* Create a skiplist entry for the given function NAME and add it to the
166 skip_function (const char *name)
168 add_skiplist_entry (make_skip_entry (0, NULL, 0, name));
170 printf_filtered (_("Function %s will be skipped when stepping.\n"), name);
174 skip_function_command (char *arg, int from_tty)
176 /* Default to the current function if no argument is given. */
179 const char *name = NULL;
182 if (!last_displayed_sal_is_valid ())
183 error (_("No default function now."));
185 pc = get_last_displayed_addr ();
186 if (!find_pc_partial_function (pc, &name, NULL, NULL))
188 error (_("No function found containing current program point %s."),
189 paddress (get_current_arch (), pc));
191 skip_function (name);
198 /* Compile the regexp in E.
199 An error is thrown if there's an error.
200 MESSAGE is used as a prefix of the error message. */
203 compile_skip_regexp (struct skiplist_entry *e, const char *message)
206 int flags = REG_NOSUB;
209 flags |= REG_EXTENDED;
212 gdb_assert (e->function_is_regexp && e->function != NULL);
214 code = regcomp (&e->compiled_function_regexp, e->function, flags);
217 char *err = get_regcomp_error (code, &e->compiled_function_regexp);
219 make_cleanup (xfree, err);
220 error (_("%s: %s"), message, err);
222 e->compiled_function_regexp_is_valid = 1;
225 /* Process "skip ..." that does not match "skip file" or "skip function". */
228 skip_command (char *arg, int from_tty)
230 const char *file = NULL;
231 const char *gfile = NULL;
232 const char *function = NULL;
233 const char *rfunction = NULL;
235 struct cleanup *cleanups;
236 struct skiplist_entry *e;
241 skip_function_command (arg, from_tty);
245 argv = buildargv (arg);
246 cleanups = make_cleanup_freeargv (argv);
248 for (i = 0; argv[i] != NULL; ++i)
250 const char *p = argv[i];
251 const char *value = argv[i + 1];
253 if (strcmp (p, "-fi") == 0
254 || strcmp (p, "-file") == 0)
257 error (_("Missing value for %s option."), p);
261 else if (strcmp (p, "-gfi") == 0
262 || strcmp (p, "-gfile") == 0)
265 error (_("Missing value for %s option."), p);
269 else if (strcmp (p, "-fu") == 0
270 || strcmp (p, "-function") == 0)
273 error (_("Missing value for %s option."), p);
277 else if (strcmp (p, "-rfu") == 0
278 || strcmp (p, "-rfunction") == 0)
281 error (_("Missing value for %s option."), p);
286 error (_("Invalid skip option: %s"), p);
289 /* Assume the user entered "skip FUNCTION-NAME".
290 FUNCTION-NAME may be `foo (int)', and therefore we pass the
291 complete original arg to skip_function command as if the user
292 typed "skip function arg". */
293 do_cleanups (cleanups);
294 skip_function_command (arg, from_tty);
298 error (_("Invalid argument: %s"), p);
301 if (file != NULL && gfile != NULL)
302 error (_("Cannot specify both -file and -gfile."));
304 if (function != NULL && rfunction != NULL)
305 error (_("Cannot specify both -function and -rfunction."));
307 /* This shouldn't happen as "skip" by itself gets punted to
308 skip_function_command. */
309 gdb_assert (file != NULL || gfile != NULL
310 || function != NULL || rfunction != NULL);
312 e = make_skip_entry (gfile != NULL, file ? file : gfile,
313 rfunction != NULL, function ? function : rfunction);
314 if (rfunction != NULL)
316 struct cleanup *rf_cleanups = make_free_skiplist_entry_cleanup (e);
318 compile_skip_regexp (e, _("regexp"));
319 discard_cleanups (rf_cleanups);
321 add_skiplist_entry (e);
323 /* I18N concerns drive some of the choices here (we can't piece together
324 the output too much). OTOH we want to keep this simple. Therefore the
325 only polish we add to the output is to append "(s)" to "File" or
326 "Function" if they're a glob/regexp. */
328 const char *file_to_print = file != NULL ? file : gfile;
329 const char *function_to_print = function != NULL ? function : rfunction;
330 const char *file_text = gfile != NULL ? _("File(s)") : _("File");
331 const char *lower_file_text = gfile != NULL ? _("file(s)") : _("file");
332 const char *function_text
333 = rfunction != NULL ? _("Function(s)") : _("Function");
335 if (function_to_print == NULL)
337 printf_filtered (_("%s %s will be skipped when stepping.\n"),
338 file_text, file_to_print);
340 else if (file_to_print == NULL)
342 printf_filtered (_("%s %s will be skipped when stepping.\n"),
343 function_text, function_to_print);
347 printf_filtered (_("%s %s in %s %s will be skipped"
348 " when stepping.\n"),
349 function_text, function_to_print,
350 lower_file_text, file_to_print);
354 do_cleanups (cleanups);
358 skip_info (char *arg, int from_tty)
360 struct skiplist_entry *e;
361 int num_printable_entries = 0;
362 struct value_print_options opts;
363 struct cleanup *tbl_chain;
365 get_user_print_options (&opts);
367 /* Count the number of rows in the table and see if we need space for a
368 64-bit address anywhere. */
369 ALL_SKIPLIST_ENTRIES (e)
370 if (arg == NULL || number_is_in_list (arg, e->number))
371 num_printable_entries++;
373 if (num_printable_entries == 0)
376 ui_out_message (current_uiout, 0, _("\
377 Not skipping any files or functions.\n"));
379 ui_out_message (current_uiout, 0,
380 _("No skiplist entries found with number %s.\n"), arg);
385 tbl_chain = make_cleanup_ui_out_table_begin_end (current_uiout, 6,
386 num_printable_entries,
389 ui_out_table_header (current_uiout, 5, ui_left, "number", "Num"); /* 1 */
390 ui_out_table_header (current_uiout, 3, ui_left, "enabled", "Enb"); /* 2 */
391 ui_out_table_header (current_uiout, 4, ui_right, "regexp", "Glob"); /* 3 */
392 ui_out_table_header (current_uiout, 20, ui_left, "file", "File"); /* 4 */
393 ui_out_table_header (current_uiout, 2, ui_right, "regexp", "RE"); /* 5 */
394 ui_out_table_header (current_uiout, 40, ui_noalign,
395 "function", "Function"); /* 6 */
396 ui_out_table_body (current_uiout);
398 ALL_SKIPLIST_ENTRIES (e)
400 struct cleanup *entry_chain;
403 if (arg != NULL && !number_is_in_list (arg, e->number))
406 entry_chain = make_cleanup_ui_out_tuple_begin_end (current_uiout,
408 ui_out_field_int (current_uiout, "number", e->number); /* 1 */
411 ui_out_field_string (current_uiout, "enabled", "y"); /* 2 */
413 ui_out_field_string (current_uiout, "enabled", "n"); /* 2 */
416 ui_out_field_string (current_uiout, "regexp", "y"); /* 3 */
418 ui_out_field_string (current_uiout, "regexp", "n"); /* 3 */
420 ui_out_field_string (current_uiout, "file",
421 e->file ? e->file : "<none>"); /* 4 */
422 if (e->function_is_regexp)
423 ui_out_field_string (current_uiout, "regexp", "y"); /* 5 */
425 ui_out_field_string (current_uiout, "regexp", "n"); /* 5 */
427 ui_out_field_string (current_uiout, "function",
428 e->function ? e->function : "<none>"); /* 6 */
430 ui_out_text (current_uiout, "\n");
431 do_cleanups (entry_chain);
434 do_cleanups (tbl_chain);
438 skip_enable_command (char *arg, int from_tty)
440 struct skiplist_entry *e;
443 ALL_SKIPLIST_ENTRIES (e)
444 if (arg == NULL || number_is_in_list (arg, e->number))
451 error (_("No skiplist entries found with number %s."), arg);
455 skip_disable_command (char *arg, int from_tty)
457 struct skiplist_entry *e;
460 ALL_SKIPLIST_ENTRIES (e)
461 if (arg == NULL || number_is_in_list (arg, e->number))
468 error (_("No skiplist entries found with number %s."), arg);
472 skip_delete_command (char *arg, int from_tty)
474 struct skiplist_entry *e, *temp, *b_prev;
478 ALL_SKIPLIST_ENTRIES_SAFE (e, temp)
479 if (arg == NULL || number_is_in_list (arg, e->number))
482 b_prev->next = e->next;
484 skiplist_entry_chain = e->next;
486 free_skiplist_entry (e);
495 error (_("No skiplist entries found with number %s."), arg);
498 /* Add the given skiplist entry to our list, and set the entry's number. */
501 add_skiplist_entry (struct skiplist_entry *e)
503 struct skiplist_entry *e1;
505 e->number = ++skiplist_entry_count;
507 /* Add to the end of the chain so that the list of
508 skiplist entries will be in numerical order. */
510 e1 = skiplist_entry_chain;
512 skiplist_entry_chain = e;
521 /* Return non-zero if we're stopped at a file to be skipped. */
524 skip_file_p (struct skiplist_entry *e,
525 const struct symtab_and_line *function_sal)
527 gdb_assert (e->file != NULL && !e->file_is_glob);
529 if (function_sal->symtab == NULL)
532 /* Check first sole SYMTAB->FILENAME. It may not be a substring of
533 symtab_to_fullname as it may contain "./" etc. */
534 if (compare_filenames_for_search (function_sal->symtab->filename, e->file))
537 /* Before we invoke realpath, which can get expensive when many
538 files are involved, do a quick comparison of the basenames. */
539 if (!basenames_may_differ
540 && filename_cmp (lbasename (function_sal->symtab->filename),
541 lbasename (e->file)) != 0)
544 /* Note: symtab_to_fullname caches its result, thus we don't have to. */
546 const char *fullname = symtab_to_fullname (function_sal->symtab);
548 if (compare_filenames_for_search (fullname, e->file))
555 /* Return non-zero if we're stopped at a globbed file to be skipped. */
558 skip_gfile_p (struct skiplist_entry *e,
559 const struct symtab_and_line *function_sal)
561 gdb_assert (e->file != NULL && e->file_is_glob);
563 if (function_sal->symtab == NULL)
566 /* Check first sole SYMTAB->FILENAME. It may not be a substring of
567 symtab_to_fullname as it may contain "./" etc. */
568 if (gdb_filename_fnmatch (e->file, function_sal->symtab->filename,
569 FNM_FILE_NAME | FNM_NOESCAPE) == 0)
572 /* Before we invoke symtab_to_fullname, which is expensive, do a quick
573 comparison of the basenames.
574 Note that we assume that lbasename works with glob-style patterns.
575 If the basename of the glob pattern is something like "*.c" then this
576 isn't much of a win. Oh well. */
577 if (!basenames_may_differ
578 && gdb_filename_fnmatch (lbasename (e->file),
579 lbasename (function_sal->symtab->filename),
580 FNM_FILE_NAME | FNM_NOESCAPE) != 0)
583 /* Note: symtab_to_fullname caches its result, thus we don't have to. */
585 const char *fullname = symtab_to_fullname (function_sal->symtab);
587 if (compare_glob_filenames_for_search (fullname, e->file))
594 /* Return non-zero if we're stopped at a function to be skipped. */
597 skip_function_p (struct skiplist_entry *e, const char *function_name)
599 gdb_assert (e->function != NULL && !e->function_is_regexp);
600 return strcmp_iw (function_name, e->function) == 0;
603 /* Return non-zero if we're stopped at a function regexp to be skipped. */
606 skip_rfunction_p (struct skiplist_entry *e, const char *function_name)
608 gdb_assert (e->function != NULL && e->function_is_regexp
609 && e->compiled_function_regexp_is_valid);
610 return (regexec (&e->compiled_function_regexp, function_name, 0, NULL, 0)
617 function_name_is_marked_for_skip (const char *function_name,
618 const struct symtab_and_line *function_sal)
620 struct skiplist_entry *e;
622 if (function_name == NULL)
625 ALL_SKIPLIST_ENTRIES (e)
627 int skip_by_file = 0;
628 int skip_by_function = 0;
637 if (skip_gfile_p (e, function_sal))
642 if (skip_file_p (e, function_sal))
646 if (e->function != NULL)
648 if (e->function_is_regexp)
650 if (skip_rfunction_p (e, function_name))
651 skip_by_function = 1;
655 if (skip_function_p (e, function_name))
656 skip_by_function = 1;
660 /* If both file and function must match, make sure we don't errantly
661 exit if only one of them match. */
662 if (e->file != NULL && e->function != NULL)
664 if (skip_by_file && skip_by_function)
667 /* Only one of file/function is specified. */
668 else if (skip_by_file || skip_by_function)
675 /* Provide a prototype to silence -Wmissing-prototypes. */
676 extern initialize_file_ftype _initialize_step_skip;
679 _initialize_step_skip (void)
681 static struct cmd_list_element *skiplist = NULL;
682 struct cmd_list_element *c;
684 skiplist_entry_chain = 0;
685 skiplist_entry_count = 0;
687 add_prefix_cmd ("skip", class_breakpoint, skip_command, _("\
688 Ignore a function while stepping.\n\
690 Usage: skip [FUNCTION-NAME]\n\
691 skip [<file-spec>] [<function-spec>]\n\
692 If no arguments are given, ignore the current function.\n\
694 <file-spec> is one of:\n\
695 -fi|-file FILE-NAME\n\
696 -gfi|-gfile GLOB-FILE-PATTERN\n\
697 <function-spec> is one of:\n\
698 -fu|-function FUNCTION-NAME\n\
699 -rfu|-rfunction FUNCTION-NAME-REGULAR-EXPRESSION"),
700 &skiplist, "skip ", 1, &cmdlist);
702 c = add_cmd ("file", class_breakpoint, skip_file_command, _("\
703 Ignore a file while stepping.\n\
704 Usage: skip file [FILE-NAME]\n\
705 If no filename is given, ignore the current file."),
707 set_cmd_completer (c, filename_completer);
709 c = add_cmd ("function", class_breakpoint, skip_function_command, _("\
710 Ignore a function while stepping.\n\
711 Usage: skip function [FUNCTION-NAME]\n\
712 If no function name is given, skip the current function."),
714 set_cmd_completer (c, location_completer);
716 add_cmd ("enable", class_breakpoint, skip_enable_command, _("\
717 Enable skip entries. You can specify numbers (e.g. \"skip enable 1 3\"), \
718 ranges (e.g. \"skip enable 4-8\"), or both (e.g. \"skip enable 1 3 4-8\").\n\n\
719 If you don't specify any numbers or ranges, we'll enable all skip entries.\n\n\
720 Usage: skip enable [NUMBERS AND/OR RANGES]"),
723 add_cmd ("disable", class_breakpoint, skip_disable_command, _("\
724 Disable skip entries. You can specify numbers (e.g. \"skip disable 1 3\"), \
725 ranges (e.g. \"skip disable 4-8\"), or both (e.g. \"skip disable 1 3 4-8\").\n\n\
726 If you don't specify any numbers or ranges, we'll disable all skip entries.\n\n\
727 Usage: skip disable [NUMBERS AND/OR RANGES]"),
730 add_cmd ("delete", class_breakpoint, skip_delete_command, _("\
731 Delete skip entries. You can specify numbers (e.g. \"skip delete 1 3\"), \
732 ranges (e.g. \"skip delete 4-8\"), or both (e.g. \"skip delete 1 3 4-8\").\n\n\
733 If you don't specify any numbers or ranges, we'll delete all skip entries.\n\n\
734 Usage: skip delete [NUMBERS AND/OR RANGES]"),
737 add_info ("skip", skip_info, _("\
738 Display the status of skips. You can specify numbers (e.g. \"skip info 1 3\"), \
739 ranges (e.g. \"skip info 4-8\"), or both (e.g. \"skip info 1 3 4-8\").\n\n\
740 If you don't specify any numbers or ranges, we'll show all skips.\n\n\
741 Usage: skip info [NUMBERS AND/OR RANGES]\n\
742 The \"Type\" column indicates one of:\n\
743 \tfile - ignored file\n\
744 \tfunction - ignored function"));