1 /* Skipping uninteresting files and functions while stepping.
3 Copyright (C) 2011-2017 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"
37 #include "common/gdb_optional.h"
41 skiplist_entry (bool file_is_glob_, std::string &&file_,
42 bool function_is_regexp_, std::string &&function_)
44 file_is_glob (file_is_glob_),
45 file (std::move (file_)),
46 function_is_regexp (function_is_regexp_),
47 function (std::move (function_)),
55 /* True if FILE is a glob-style pattern.
56 Otherwise it is the plain file name (possibly with directories). */
59 /* The name of the file or empty if no name. */
62 /* True if FUNCTION is a regexp.
63 Otherwise it is a plain function name (possibly with arguments,
65 bool function_is_regexp;
67 /* The name of the function or empty if no name. */
70 /* If this is a function regexp, the compiled form. */
71 gdb::optional<compiled_regex> compiled_function_regexp;
75 struct skiplist_entry *next;
78 static void add_skiplist_entry (std::unique_ptr<skiplist_entry> &&e);
80 static struct skiplist_entry *skiplist_entry_chain;
81 static int skiplist_entry_count;
83 #define ALL_SKIPLIST_ENTRIES(E) \
84 for (E = skiplist_entry_chain; E; E = E->next)
86 #define ALL_SKIPLIST_ENTRIES_SAFE(E,TMP) \
87 for (E = skiplist_entry_chain; \
88 E ? (TMP = E->next, 1) : 0; \
91 /* Create a skip object. */
93 static std::unique_ptr<skiplist_entry>
94 make_skip_entry (bool file_is_glob, std::string &&file,
95 bool function_is_regexp, std::string &&function)
97 gdb_assert (!file.empty () || !function.empty ());
99 gdb_assert (!file.empty ());
100 if (function_is_regexp)
101 gdb_assert (!function.empty ());
103 return std::unique_ptr<skiplist_entry>
104 (new skiplist_entry (file_is_glob, std::move (file),
105 function_is_regexp, std::move (function)));
109 skip_file_command (char *arg, int from_tty)
111 struct symtab *symtab;
112 const char *filename = NULL;
114 /* If no argument was given, try to default to the last
115 displayed codepoint. */
118 symtab = get_last_displayed_symtab ();
120 error (_("No default file now."));
122 /* It is not a typo, symtab_to_filename_for_display woule be needlessly
124 filename = symtab_to_fullname (symtab);
129 add_skiplist_entry (make_skip_entry (false, std::string (filename), false,
132 printf_filtered (_("File %s will be skipped when stepping.\n"), filename);
135 /* Create a skiplist entry for the given function NAME and add it to the
139 skip_function (const char *name)
141 add_skiplist_entry (make_skip_entry (false, std::string (), false,
142 std::string (name)));
144 printf_filtered (_("Function %s will be skipped when stepping.\n"), name);
148 skip_function_command (char *arg, int from_tty)
150 /* Default to the current function if no argument is given. */
153 const char *name = NULL;
156 if (!last_displayed_sal_is_valid ())
157 error (_("No default function now."));
159 pc = get_last_displayed_addr ();
160 if (!find_pc_partial_function (pc, &name, NULL, NULL))
162 error (_("No function found containing current program point %s."),
163 paddress (get_current_arch (), pc));
165 skip_function (name);
172 /* Compile the regexp in E.
173 An error is thrown if there's an error.
174 MESSAGE is used as a prefix of the error message. */
177 compile_skip_regexp (struct skiplist_entry *e, const char *message)
179 int flags = REG_NOSUB;
182 flags |= REG_EXTENDED;
185 gdb_assert (e->function_is_regexp && !e->function.empty ());
186 e->compiled_function_regexp.emplace (e->function.c_str (), flags, message);
189 /* Process "skip ..." that does not match "skip file" or "skip function". */
192 skip_command (char *arg, int from_tty)
194 const char *file = NULL;
195 const char *gfile = NULL;
196 const char *function = NULL;
197 const char *rfunction = NULL;
202 skip_function_command (arg, from_tty);
208 for (i = 0; argv[i] != NULL; ++i)
210 const char *p = argv[i];
211 const char *value = argv[i + 1];
213 if (strcmp (p, "-fi") == 0
214 || strcmp (p, "-file") == 0)
217 error (_("Missing value for %s option."), p);
221 else if (strcmp (p, "-gfi") == 0
222 || strcmp (p, "-gfile") == 0)
225 error (_("Missing value for %s option."), p);
229 else if (strcmp (p, "-fu") == 0
230 || strcmp (p, "-function") == 0)
233 error (_("Missing value for %s option."), p);
237 else if (strcmp (p, "-rfu") == 0
238 || strcmp (p, "-rfunction") == 0)
241 error (_("Missing value for %s option."), p);
246 error (_("Invalid skip option: %s"), p);
249 /* Assume the user entered "skip FUNCTION-NAME".
250 FUNCTION-NAME may be `foo (int)', and therefore we pass the
251 complete original arg to skip_function command as if the user
252 typed "skip function arg". */
253 skip_function_command (arg, from_tty);
257 error (_("Invalid argument: %s"), p);
260 if (file != NULL && gfile != NULL)
261 error (_("Cannot specify both -file and -gfile."));
263 if (function != NULL && rfunction != NULL)
264 error (_("Cannot specify both -function and -rfunction."));
266 /* This shouldn't happen as "skip" by itself gets punted to
267 skip_function_command. */
268 gdb_assert (file != NULL || gfile != NULL
269 || function != NULL || rfunction != NULL);
271 std::string entry_file;
274 else if (gfile != NULL)
276 std::string entry_function;
277 if (function != NULL)
278 entry_function = function;
279 else if (rfunction != NULL)
280 entry_function = rfunction;
281 std::unique_ptr<skiplist_entry> e
282 = make_skip_entry (gfile != NULL, std::move (entry_file),
283 rfunction != NULL, std::move (entry_function));
284 if (rfunction != NULL)
285 compile_skip_regexp (e.get (), _("regexp"));
287 add_skiplist_entry (std::move (e));
289 /* I18N concerns drive some of the choices here (we can't piece together
290 the output too much). OTOH we want to keep this simple. Therefore the
291 only polish we add to the output is to append "(s)" to "File" or
292 "Function" if they're a glob/regexp. */
294 const char *file_to_print = file != NULL ? file : gfile;
295 const char *function_to_print = function != NULL ? function : rfunction;
296 const char *file_text = gfile != NULL ? _("File(s)") : _("File");
297 const char *lower_file_text = gfile != NULL ? _("file(s)") : _("file");
298 const char *function_text
299 = rfunction != NULL ? _("Function(s)") : _("Function");
301 if (function_to_print == NULL)
303 printf_filtered (_("%s %s will be skipped when stepping.\n"),
304 file_text, file_to_print);
306 else if (file_to_print == NULL)
308 printf_filtered (_("%s %s will be skipped when stepping.\n"),
309 function_text, function_to_print);
313 printf_filtered (_("%s %s in %s %s will be skipped"
314 " when stepping.\n"),
315 function_text, function_to_print,
316 lower_file_text, file_to_print);
322 skip_info (char *arg, int from_tty)
324 struct skiplist_entry *e;
325 int num_printable_entries = 0;
326 struct value_print_options opts;
328 get_user_print_options (&opts);
330 /* Count the number of rows in the table and see if we need space for a
331 64-bit address anywhere. */
332 ALL_SKIPLIST_ENTRIES (e)
333 if (arg == NULL || number_is_in_list (arg, e->number))
334 num_printable_entries++;
336 if (num_printable_entries == 0)
339 current_uiout->message (_("Not skipping any files or functions.\n"));
341 current_uiout->message (
342 _("No skiplist entries found with number %s.\n"), arg);
347 ui_out_emit_table table_emitter (current_uiout, 6, num_printable_entries,
350 current_uiout->table_header (5, ui_left, "number", "Num"); /* 1 */
351 current_uiout->table_header (3, ui_left, "enabled", "Enb"); /* 2 */
352 current_uiout->table_header (4, ui_right, "regexp", "Glob"); /* 3 */
353 current_uiout->table_header (20, ui_left, "file", "File"); /* 4 */
354 current_uiout->table_header (2, ui_right, "regexp", "RE"); /* 5 */
355 current_uiout->table_header (40, ui_noalign, "function", "Function"); /* 6 */
356 current_uiout->table_body ();
358 ALL_SKIPLIST_ENTRIES (e)
362 if (arg != NULL && !number_is_in_list (arg, e->number))
365 ui_out_emit_tuple tuple_emitter (current_uiout, "blklst-entry");
366 current_uiout->field_int ("number", e->number); /* 1 */
369 current_uiout->field_string ("enabled", "y"); /* 2 */
371 current_uiout->field_string ("enabled", "n"); /* 2 */
374 current_uiout->field_string ("regexp", "y"); /* 3 */
376 current_uiout->field_string ("regexp", "n"); /* 3 */
378 current_uiout->field_string ("file",
379 e->file.empty () ? "<none>"
380 : e->file.c_str ()); /* 4 */
381 if (e->function_is_regexp)
382 current_uiout->field_string ("regexp", "y"); /* 5 */
384 current_uiout->field_string ("regexp", "n"); /* 5 */
386 current_uiout->field_string ("function",
387 e->function.empty () ? "<none>"
388 : e->function.c_str ()); /* 6 */
390 current_uiout->text ("\n");
395 skip_enable_command (char *arg, int from_tty)
397 struct skiplist_entry *e;
400 ALL_SKIPLIST_ENTRIES (e)
401 if (arg == NULL || number_is_in_list (arg, e->number))
408 error (_("No skiplist entries found with number %s."), arg);
412 skip_disable_command (char *arg, int from_tty)
414 struct skiplist_entry *e;
417 ALL_SKIPLIST_ENTRIES (e)
418 if (arg == NULL || number_is_in_list (arg, e->number))
425 error (_("No skiplist entries found with number %s."), arg);
429 skip_delete_command (char *arg, int from_tty)
431 struct skiplist_entry *e, *temp, *b_prev;
435 ALL_SKIPLIST_ENTRIES_SAFE (e, temp)
436 if (arg == NULL || number_is_in_list (arg, e->number))
439 b_prev->next = e->next;
441 skiplist_entry_chain = e->next;
452 error (_("No skiplist entries found with number %s."), arg);
455 /* Add the given skiplist entry to our list, and set the entry's number. */
458 add_skiplist_entry (std::unique_ptr<skiplist_entry> &&e)
460 struct skiplist_entry *e1;
462 e->number = ++skiplist_entry_count;
464 /* Add to the end of the chain so that the list of
465 skiplist entries will be in numerical order. */
467 e1 = skiplist_entry_chain;
469 skiplist_entry_chain = e.release ();
474 e1->next = e.release ();
478 /* Return non-zero if we're stopped at a file to be skipped. */
481 skip_file_p (struct skiplist_entry *e,
482 const struct symtab_and_line *function_sal)
484 gdb_assert (!e->file.empty () && !e->file_is_glob);
486 if (function_sal->symtab == NULL)
489 /* Check first sole SYMTAB->FILENAME. It may not be a substring of
490 symtab_to_fullname as it may contain "./" etc. */
491 if (compare_filenames_for_search (function_sal->symtab->filename,
495 /* Before we invoke realpath, which can get expensive when many
496 files are involved, do a quick comparison of the basenames. */
497 if (!basenames_may_differ
498 && filename_cmp (lbasename (function_sal->symtab->filename),
499 lbasename (e->file.c_str ())) != 0)
502 /* Note: symtab_to_fullname caches its result, thus we don't have to. */
504 const char *fullname = symtab_to_fullname (function_sal->symtab);
506 if (compare_filenames_for_search (fullname, e->file.c_str ()))
513 /* Return non-zero if we're stopped at a globbed file to be skipped. */
516 skip_gfile_p (struct skiplist_entry *e,
517 const struct symtab_and_line *function_sal)
519 gdb_assert (!e->file.empty () && e->file_is_glob);
521 if (function_sal->symtab == NULL)
524 /* Check first sole SYMTAB->FILENAME. It may not be a substring of
525 symtab_to_fullname as it may contain "./" etc. */
526 if (gdb_filename_fnmatch (e->file.c_str (), function_sal->symtab->filename,
527 FNM_FILE_NAME | FNM_NOESCAPE) == 0)
530 /* Before we invoke symtab_to_fullname, which is expensive, do a quick
531 comparison of the basenames.
532 Note that we assume that lbasename works with glob-style patterns.
533 If the basename of the glob pattern is something like "*.c" then this
534 isn't much of a win. Oh well. */
535 if (!basenames_may_differ
536 && gdb_filename_fnmatch (lbasename (e->file.c_str ()),
537 lbasename (function_sal->symtab->filename),
538 FNM_FILE_NAME | FNM_NOESCAPE) != 0)
541 /* Note: symtab_to_fullname caches its result, thus we don't have to. */
543 const char *fullname = symtab_to_fullname (function_sal->symtab);
545 if (compare_glob_filenames_for_search (fullname, e->file.c_str ()))
552 /* Return non-zero if we're stopped at a function to be skipped. */
555 skip_function_p (struct skiplist_entry *e, const char *function_name)
557 gdb_assert (!e->function.empty () && !e->function_is_regexp);
558 return strcmp_iw (function_name, e->function.c_str ()) == 0;
561 /* Return non-zero if we're stopped at a function regexp to be skipped. */
564 skip_rfunction_p (struct skiplist_entry *e, const char *function_name)
566 gdb_assert (!e->function.empty () && e->function_is_regexp
567 && e->compiled_function_regexp);
568 return (e->compiled_function_regexp->exec (function_name, 0, NULL, 0)
575 function_name_is_marked_for_skip (const char *function_name,
576 const struct symtab_and_line *function_sal)
578 struct skiplist_entry *e;
580 if (function_name == NULL)
583 ALL_SKIPLIST_ENTRIES (e)
585 int skip_by_file = 0;
586 int skip_by_function = 0;
591 if (!e->file.empty ())
595 if (skip_gfile_p (e, function_sal))
600 if (skip_file_p (e, function_sal))
604 if (!e->function.empty ())
606 if (e->function_is_regexp)
608 if (skip_rfunction_p (e, function_name))
609 skip_by_function = 1;
613 if (skip_function_p (e, function_name))
614 skip_by_function = 1;
618 /* If both file and function must match, make sure we don't errantly
619 exit if only one of them match. */
620 if (!e->file.empty () && !e->function.empty ())
622 if (skip_by_file && skip_by_function)
625 /* Only one of file/function is specified. */
626 else if (skip_by_file || skip_by_function)
633 /* Provide a prototype to silence -Wmissing-prototypes. */
634 extern initialize_file_ftype _initialize_step_skip;
637 _initialize_step_skip (void)
639 static struct cmd_list_element *skiplist = NULL;
640 struct cmd_list_element *c;
642 skiplist_entry_chain = 0;
643 skiplist_entry_count = 0;
645 add_prefix_cmd ("skip", class_breakpoint, skip_command, _("\
646 Ignore a function while stepping.\n\
648 Usage: skip [FUNCTION-NAME]\n\
649 skip [<file-spec>] [<function-spec>]\n\
650 If no arguments are given, ignore the current function.\n\
652 <file-spec> is one of:\n\
653 -fi|-file FILE-NAME\n\
654 -gfi|-gfile GLOB-FILE-PATTERN\n\
655 <function-spec> is one of:\n\
656 -fu|-function FUNCTION-NAME\n\
657 -rfu|-rfunction FUNCTION-NAME-REGULAR-EXPRESSION"),
658 &skiplist, "skip ", 1, &cmdlist);
660 c = add_cmd ("file", class_breakpoint, skip_file_command, _("\
661 Ignore a file while stepping.\n\
662 Usage: skip file [FILE-NAME]\n\
663 If no filename is given, ignore the current file."),
665 set_cmd_completer (c, filename_completer);
667 c = add_cmd ("function", class_breakpoint, skip_function_command, _("\
668 Ignore a function while stepping.\n\
669 Usage: skip function [FUNCTION-NAME]\n\
670 If no function name is given, skip the current function."),
672 set_cmd_completer (c, location_completer);
674 add_cmd ("enable", class_breakpoint, skip_enable_command, _("\
675 Enable skip entries. You can specify numbers (e.g. \"skip enable 1 3\"), \
676 ranges (e.g. \"skip enable 4-8\"), or both (e.g. \"skip enable 1 3 4-8\").\n\n\
677 If you don't specify any numbers or ranges, we'll enable all skip entries.\n\n\
678 Usage: skip enable [NUMBERS AND/OR RANGES]"),
681 add_cmd ("disable", class_breakpoint, skip_disable_command, _("\
682 Disable skip entries. You can specify numbers (e.g. \"skip disable 1 3\"), \
683 ranges (e.g. \"skip disable 4-8\"), or both (e.g. \"skip disable 1 3 4-8\").\n\n\
684 If you don't specify any numbers or ranges, we'll disable all skip entries.\n\n\
685 Usage: skip disable [NUMBERS AND/OR RANGES]"),
688 add_cmd ("delete", class_breakpoint, skip_delete_command, _("\
689 Delete skip entries. You can specify numbers (e.g. \"skip delete 1 3\"), \
690 ranges (e.g. \"skip delete 4-8\"), or both (e.g. \"skip delete 1 3 4-8\").\n\n\
691 If you don't specify any numbers or ranges, we'll delete all skip entries.\n\n\
692 Usage: skip delete [NUMBERS AND/OR RANGES]"),
695 add_info ("skip", skip_info, _("\
696 Display the status of skips. You can specify numbers (e.g. \"skip info 1 3\"), \
697 ranges (e.g. \"skip info 4-8\"), or both (e.g. \"skip info 1 3 4-8\").\n\n\
698 If you don't specify any numbers or ranges, we'll show all skips.\n\n\
699 Usage: skip info [NUMBERS AND/OR RANGES]\n\
700 The \"Type\" column indicates one of:\n\
701 \tfile - ignored file\n\
702 \tfunction - ignored function"));