C++-ify skip.c
[external/binutils.git] / gdb / skip.c
1 /* Skipping uninteresting files and functions while stepping.
2
3    Copyright (C) 2011-2017 Free Software Foundation, Inc.
4
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.
9
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.
14
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/>.  */
17
18 #include "defs.h"
19 #include "skip.h"
20 #include "value.h"
21 #include "valprint.h"
22 #include "ui-out.h"
23 #include "symtab.h"
24 #include "gdbcmd.h"
25 #include "command.h"
26 #include "completer.h"
27 #include "stack.h"
28 #include "cli/cli-utils.h"
29 #include "arch-utils.h"
30 #include "linespec.h"
31 #include "objfiles.h"
32 #include "breakpoint.h" /* for get_sal_arch () */
33 #include "source.h"
34 #include "filenames.h"
35 #include "fnmatch.h"
36 #include "gdb_regex.h"
37 #include "common/gdb_optional.h"
38
39 struct skiplist_entry
40 {
41   skiplist_entry (bool file_is_glob_, std::string &&file_,
42                   bool function_is_regexp_, std::string &&function_)
43     : number (-1),
44       file_is_glob (file_is_glob_),
45       file (std::move (file_)),
46       function_is_regexp (function_is_regexp_),
47       function (std::move (function_)),
48       enabled (true),
49       next (NULL)
50   {
51   }
52
53   int number;
54
55   /* True if FILE is a glob-style pattern.
56      Otherwise it is the plain file name (possibly with directories).  */
57   bool file_is_glob;
58
59   /* The name of the file or empty if no name.  */
60   std::string file;
61
62   /* True if FUNCTION is a regexp.
63      Otherwise it is a plain function name (possibly with arguments,
64      for C++).  */
65   bool function_is_regexp;
66
67   /* The name of the function or empty if no name.  */
68   std::string function;
69
70   /* If this is a function regexp, the compiled form.  */
71   gdb::optional<compiled_regex> compiled_function_regexp;
72
73   bool enabled;
74
75   struct skiplist_entry *next;
76 };
77
78 static void add_skiplist_entry (std::unique_ptr<skiplist_entry> &&e);
79
80 static struct skiplist_entry *skiplist_entry_chain;
81 static int skiplist_entry_count;
82
83 #define ALL_SKIPLIST_ENTRIES(E) \
84   for (E = skiplist_entry_chain; E; E = E->next)
85
86 #define ALL_SKIPLIST_ENTRIES_SAFE(E,TMP) \
87   for (E = skiplist_entry_chain;         \
88        E ? (TMP = E->next, 1) : 0;       \
89        E = TMP)
90
91 /* Create a skip object.  */
92
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)
96 {
97   gdb_assert (!file.empty () || !function.empty ());
98   if (file_is_glob)
99     gdb_assert (!file.empty ());
100   if (function_is_regexp)
101     gdb_assert (!function.empty ());
102
103   return std::unique_ptr<skiplist_entry>
104     (new skiplist_entry (file_is_glob, std::move (file),
105                          function_is_regexp, std::move (function)));
106 }
107
108 static void
109 skip_file_command (char *arg, int from_tty)
110 {
111   struct symtab *symtab;
112   const char *filename = NULL;
113
114   /* If no argument was given, try to default to the last
115      displayed codepoint.  */
116   if (arg == NULL)
117     {
118       symtab = get_last_displayed_symtab ();
119       if (symtab == NULL)
120         error (_("No default file now."));
121
122       /* It is not a typo, symtab_to_filename_for_display woule be needlessly
123          ambiguous.  */
124       filename = symtab_to_fullname (symtab);
125     }
126   else
127     filename = arg;
128
129   add_skiplist_entry (make_skip_entry (false, std::string (filename), false,
130                                        std::string ()));
131
132   printf_filtered (_("File %s will be skipped when stepping.\n"), filename);
133 }
134
135 /* Create a skiplist entry for the given function NAME and add it to the
136    list.  */
137
138 static void
139 skip_function (const char *name)
140 {
141   add_skiplist_entry (make_skip_entry (false, std::string (), false,
142                                        std::string (name)));
143
144   printf_filtered (_("Function %s will be skipped when stepping.\n"), name);
145 }
146
147 static void
148 skip_function_command (char *arg, int from_tty)
149 {
150   /* Default to the current function if no argument is given.  */
151   if (arg == NULL)
152     {
153       const char *name = NULL;
154       CORE_ADDR pc;
155
156       if (!last_displayed_sal_is_valid ())
157         error (_("No default function now."));
158
159       pc = get_last_displayed_addr ();
160       if (!find_pc_partial_function (pc, &name, NULL, NULL))
161         {
162           error (_("No function found containing current program point %s."),
163                   paddress (get_current_arch (), pc));
164         }
165       skip_function (name);
166       return;
167     }
168
169   skip_function (arg);
170 }
171
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.  */
175
176 static void
177 compile_skip_regexp (struct skiplist_entry *e, const char *message)
178 {
179   int flags = REG_NOSUB;
180
181 #ifdef REG_EXTENDED
182   flags |= REG_EXTENDED;
183 #endif
184
185   gdb_assert (e->function_is_regexp && !e->function.empty ());
186   e->compiled_function_regexp.emplace (e->function.c_str (), flags, message);
187 }
188
189 /* Process "skip ..." that does not match "skip file" or "skip function".  */
190
191 static void
192 skip_command (char *arg, int from_tty)
193 {
194   const char *file = NULL;
195   const char *gfile = NULL;
196   const char *function = NULL;
197   const char *rfunction = NULL;
198   int i;
199
200   if (arg == NULL)
201     {
202       skip_function_command (arg, from_tty);
203       return;
204     }
205
206   gdb_argv argv (arg);
207
208   for (i = 0; argv[i] != NULL; ++i)
209     {
210       const char *p = argv[i];
211       const char *value = argv[i + 1];
212
213       if (strcmp (p, "-fi") == 0
214           || strcmp (p, "-file") == 0)
215         {
216           if (value == NULL)
217             error (_("Missing value for %s option."), p);
218           file = value;
219           ++i;
220         }
221       else if (strcmp (p, "-gfi") == 0
222                || strcmp (p, "-gfile") == 0)
223         {
224           if (value == NULL)
225             error (_("Missing value for %s option."), p);
226           gfile = value;
227           ++i;
228         }
229       else if (strcmp (p, "-fu") == 0
230                || strcmp (p, "-function") == 0)
231         {
232           if (value == NULL)
233             error (_("Missing value for %s option."), p);
234           function = value;
235           ++i;
236         }
237       else if (strcmp (p, "-rfu") == 0
238                || strcmp (p, "-rfunction") == 0)
239         {
240           if (value == NULL)
241             error (_("Missing value for %s option."), p);
242           rfunction = value;
243           ++i;
244         }
245       else if (*p == '-')
246         error (_("Invalid skip option: %s"), p);
247       else if (i == 0)
248         {
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);
254           return;
255         }
256       else
257         error (_("Invalid argument: %s"), p);
258     }
259
260   if (file != NULL && gfile != NULL)
261     error (_("Cannot specify both -file and -gfile."));
262
263   if (function != NULL && rfunction != NULL)
264     error (_("Cannot specify both -function and -rfunction."));
265
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);
270
271   std::string entry_file;
272   if (file != NULL)
273     entry_file = file;
274   else if (gfile != NULL)
275     entry_file = gfile;
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"));
286
287   add_skiplist_entry (std::move (e));
288
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.  */
293   {
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");
300
301     if (function_to_print == NULL)
302       {
303         printf_filtered (_("%s %s will be skipped when stepping.\n"),
304                          file_text, file_to_print);
305       }
306     else if (file_to_print == NULL)
307       {
308         printf_filtered (_("%s %s will be skipped when stepping.\n"),
309                          function_text, function_to_print);
310       }
311     else
312       {
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);
317       }
318   }
319 }
320
321 static void
322 skip_info (char *arg, int from_tty)
323 {
324   struct skiplist_entry *e;
325   int num_printable_entries = 0;
326   struct value_print_options opts;
327
328   get_user_print_options (&opts);
329
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++;
335
336   if (num_printable_entries == 0)
337     {
338       if (arg == NULL)
339         current_uiout->message (_("Not skipping any files or functions.\n"));
340       else
341         current_uiout->message (
342           _("No skiplist entries found with number %s.\n"), arg);
343
344       return;
345     }
346
347   ui_out_emit_table table_emitter (current_uiout, 6, num_printable_entries,
348                                    "SkiplistTable");
349
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 ();
357
358   ALL_SKIPLIST_ENTRIES (e)
359     {
360
361       QUIT;
362       if (arg != NULL && !number_is_in_list (arg, e->number))
363         continue;
364
365       ui_out_emit_tuple tuple_emitter (current_uiout, "blklst-entry");
366       current_uiout->field_int ("number", e->number); /* 1 */
367
368       if (e->enabled)
369         current_uiout->field_string ("enabled", "y"); /* 2 */
370       else
371         current_uiout->field_string ("enabled", "n"); /* 2 */
372
373       if (e->file_is_glob)
374         current_uiout->field_string ("regexp", "y"); /* 3 */
375       else
376         current_uiout->field_string ("regexp", "n"); /* 3 */
377
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 */
383       else
384         current_uiout->field_string ("regexp", "n"); /* 5 */
385
386       current_uiout->field_string ("function",
387                                    e->function.empty () ? "<none>"
388                                    : e->function.c_str ()); /* 6 */
389
390       current_uiout->text ("\n");
391     }
392 }
393
394 static void
395 skip_enable_command (char *arg, int from_tty)
396 {
397   struct skiplist_entry *e;
398   int found = 0;
399
400   ALL_SKIPLIST_ENTRIES (e)
401     if (arg == NULL || number_is_in_list (arg, e->number))
402       {
403         e->enabled = true;
404         found = 1;
405       }
406
407   if (!found)
408     error (_("No skiplist entries found with number %s."), arg);
409 }
410
411 static void
412 skip_disable_command (char *arg, int from_tty)
413 {
414   struct skiplist_entry *e;
415   int found = 0;
416
417   ALL_SKIPLIST_ENTRIES (e)
418     if (arg == NULL || number_is_in_list (arg, e->number))
419       {
420         e->enabled = false;
421         found = 1;
422       }
423
424   if (!found)
425     error (_("No skiplist entries found with number %s."), arg);
426 }
427
428 static void
429 skip_delete_command (char *arg, int from_tty)
430 {
431   struct skiplist_entry *e, *temp, *b_prev;
432   int found = 0;
433
434   b_prev = 0;
435   ALL_SKIPLIST_ENTRIES_SAFE (e, temp)
436     if (arg == NULL || number_is_in_list (arg, e->number))
437       {
438         if (b_prev != NULL)
439           b_prev->next = e->next;
440         else
441           skiplist_entry_chain = e->next;
442
443         delete e;
444         found = 1;
445       }
446     else
447       {
448         b_prev = e;
449       }
450
451   if (!found)
452     error (_("No skiplist entries found with number %s."), arg);
453 }
454
455 /* Add the given skiplist entry to our list, and set the entry's number.  */
456
457 static void
458 add_skiplist_entry (std::unique_ptr<skiplist_entry> &&e)
459 {
460   struct skiplist_entry *e1;
461
462   e->number = ++skiplist_entry_count;
463
464   /* Add to the end of the chain so that the list of
465      skiplist entries will be in numerical order.  */
466
467   e1 = skiplist_entry_chain;
468   if (e1 == NULL)
469     skiplist_entry_chain = e.release ();
470   else
471     {
472       while (e1->next)
473         e1 = e1->next;
474       e1->next = e.release ();
475     }
476 }
477
478 /* Return non-zero if we're stopped at a file to be skipped.  */
479
480 static int
481 skip_file_p (struct skiplist_entry *e,
482              const struct symtab_and_line *function_sal)
483 {
484   gdb_assert (!e->file.empty () && !e->file_is_glob);
485
486   if (function_sal->symtab == NULL)
487     return 0;
488
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,
492                                     e->file.c_str ()))
493     return 1;
494
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)
500     return 0;
501
502   /* Note: symtab_to_fullname caches its result, thus we don't have to.  */
503   {
504     const char *fullname = symtab_to_fullname (function_sal->symtab);
505
506     if (compare_filenames_for_search (fullname, e->file.c_str ()))
507       return 1;
508   }
509
510   return 0;
511 }
512
513 /* Return non-zero if we're stopped at a globbed file to be skipped.  */
514
515 static int
516 skip_gfile_p (struct skiplist_entry *e,
517               const struct symtab_and_line *function_sal)
518 {
519   gdb_assert (!e->file.empty () && e->file_is_glob);
520
521   if (function_sal->symtab == NULL)
522     return 0;
523
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)
528     return 1;
529
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)
539     return 0;
540
541   /* Note: symtab_to_fullname caches its result, thus we don't have to.  */
542   {
543     const char *fullname = symtab_to_fullname (function_sal->symtab);
544
545     if (compare_glob_filenames_for_search (fullname, e->file.c_str ()))
546       return 1;
547   }
548
549   return 0;
550 }
551
552 /* Return non-zero if we're stopped at a function to be skipped.  */
553
554 static int
555 skip_function_p (struct skiplist_entry *e, const char *function_name)
556 {
557   gdb_assert (!e->function.empty () && !e->function_is_regexp);
558   return strcmp_iw (function_name, e->function.c_str ()) == 0;
559 }
560
561 /* Return non-zero if we're stopped at a function regexp to be skipped.  */
562
563 static int
564 skip_rfunction_p (struct skiplist_entry *e, const char *function_name)
565 {
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)
569           == 0);
570 }
571
572 /* See skip.h.  */
573
574 int
575 function_name_is_marked_for_skip (const char *function_name,
576                                   const struct symtab_and_line *function_sal)
577 {
578   struct skiplist_entry *e;
579
580   if (function_name == NULL)
581     return 0;
582
583   ALL_SKIPLIST_ENTRIES (e)
584     {
585       int skip_by_file = 0;
586       int skip_by_function = 0;
587
588       if (!e->enabled)
589         continue;
590
591       if (!e->file.empty ())
592         {
593           if (e->file_is_glob)
594             {
595               if (skip_gfile_p (e, function_sal))
596                 skip_by_file = 1;
597             }
598           else
599             {
600               if (skip_file_p (e, function_sal))
601                 skip_by_file = 1;
602             }
603         }
604       if (!e->function.empty ())
605         {
606           if (e->function_is_regexp)
607             {
608               if (skip_rfunction_p (e, function_name))
609                 skip_by_function = 1;
610             }
611           else
612             {
613               if (skip_function_p (e, function_name))
614                 skip_by_function = 1;
615             }
616         }
617
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 ())
621         {
622           if (skip_by_file && skip_by_function)
623             return 1;
624         }
625       /* Only one of file/function is specified.  */
626       else if (skip_by_file || skip_by_function)
627         return 1;
628     }
629
630   return 0;
631 }
632
633 /* Provide a prototype to silence -Wmissing-prototypes.  */
634 extern initialize_file_ftype _initialize_step_skip;
635
636 void
637 _initialize_step_skip (void)
638 {
639   static struct cmd_list_element *skiplist = NULL;
640   struct cmd_list_element *c;
641
642   skiplist_entry_chain = 0;
643   skiplist_entry_count = 0;
644
645   add_prefix_cmd ("skip", class_breakpoint, skip_command, _("\
646 Ignore a function while stepping.\n\
647 \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\
651 \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);
659
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."),
664                &skiplist);
665   set_cmd_completer (c, filename_completer);
666
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."),
671                &skiplist);
672   set_cmd_completer (c, location_completer);
673
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]"),
679            &skiplist);
680
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]"),
686            &skiplist);
687
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]"),
693            &skiplist);
694
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"));
703 }