GDB (API)
/home/stan/gdb/src/gdb/skip.c
Go to the documentation of this file.
00001 /* Skipping uninteresting files and functions while stepping.
00002 
00003    Copyright (C) 2011-2013 Free Software Foundation, Inc.
00004 
00005    This program is free software; you can redistribute it and/or modify
00006    it under the terms of the GNU General Public License as published by
00007    the Free Software Foundation; either version 3 of the License, or
00008    (at your option) any later version.
00009 
00010    This program is distributed in the hope that it will be useful,
00011    but WITHOUT ANY WARRANTY; without even the implied warranty of
00012    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013    GNU General Public License for more details.
00014 
00015    You should have received a copy of the GNU General Public License
00016    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
00017 
00018 #include "defs.h"
00019 #include "skip.h"
00020 #include "value.h"
00021 #include "valprint.h"
00022 #include "ui-out.h"
00023 #include "gdb_string.h"
00024 #include "symtab.h"
00025 #include "gdbcmd.h"
00026 #include "command.h"
00027 #include "completer.h"
00028 #include "stack.h"
00029 #include "cli/cli-utils.h"
00030 #include "arch-utils.h"
00031 #include "linespec.h"
00032 #include "objfiles.h"
00033 #include "exceptions.h"
00034 #include "breakpoint.h" /* for get_sal_arch () */
00035 #include "source.h"
00036 #include "filenames.h"
00037 
00038 struct skiplist_entry
00039 {
00040   int number;
00041 
00042   /* NULL if this isn't a skiplist entry for an entire file.
00043      The skiplist entry owns this pointer.  */
00044   char *filename;
00045 
00046   /* The name of the marked-for-skip function, if this is a skiplist
00047      entry for a function.
00048      The skiplist entry owns this pointer.  */
00049   char *function_name;
00050 
00051   int enabled;
00052 
00053   struct skiplist_entry *next;
00054 };
00055 
00056 static void add_skiplist_entry (struct skiplist_entry *e);
00057 static void skip_function (const char *name);
00058 
00059 static struct skiplist_entry *skiplist_entry_chain;
00060 static int skiplist_entry_count;
00061 
00062 #define ALL_SKIPLIST_ENTRIES(E) \
00063   for (E = skiplist_entry_chain; E; E = E->next)
00064 
00065 #define ALL_SKIPLIST_ENTRIES_SAFE(E,TMP) \
00066   for (E = skiplist_entry_chain;         \
00067        E ? (TMP = E->next, 1) : 0;       \
00068        E = TMP)
00069 
00070 static void
00071 skip_file_command (char *arg, int from_tty)
00072 {
00073   struct skiplist_entry *e;
00074   struct symtab *symtab;
00075   const char *filename = NULL;
00076 
00077   /* If no argument was given, try to default to the last
00078      displayed codepoint.  */
00079   if (arg == NULL)
00080     {
00081       symtab = get_last_displayed_symtab ();
00082       if (symtab == NULL)
00083         error (_("No default file now."));
00084 
00085       /* It is not a typo, symtab_to_filename_for_display woule be needlessly
00086          ambiguous.  */
00087       filename = symtab_to_fullname (symtab);
00088     }
00089   else
00090     {
00091       symtab = lookup_symtab (arg);
00092       if (symtab == NULL)
00093         {
00094           fprintf_filtered (gdb_stderr, _("No source file named %s.\n"), arg);
00095           if (!nquery (_("\
00096 Ignore file pending future shared library load? ")))
00097             return;
00098         }
00099       /* Do not use SYMTAB's filename, later loaded shared libraries may match
00100          given ARG but not SYMTAB's filename.  */
00101       filename = arg;
00102     }
00103 
00104   e = XZALLOC (struct skiplist_entry);
00105   e->filename = xstrdup (filename);
00106   e->enabled = 1;
00107 
00108   add_skiplist_entry (e);
00109 
00110   printf_filtered (_("File %s will be skipped when stepping.\n"), filename);
00111 }
00112 
00113 static void
00114 skip_function_command (char *arg, int from_tty)
00115 {
00116   const char *name = NULL;
00117 
00118   /* Default to the current function if no argument is given.  */
00119   if (arg == NULL)
00120     {
00121       CORE_ADDR pc;
00122 
00123       if (!last_displayed_sal_is_valid ())
00124         error (_("No default function now."));
00125 
00126       pc = get_last_displayed_addr ();
00127       if (!find_pc_partial_function (pc, &name, NULL, NULL))
00128         {
00129           error (_("No function found containing current program point %s."),
00130                   paddress (get_current_arch (), pc));
00131         }
00132       skip_function (name);
00133     }
00134   else
00135     {
00136       if (lookup_symbol (arg, NULL, VAR_DOMAIN, NULL) == NULL)
00137         {
00138           fprintf_filtered (gdb_stderr,
00139                             _("No function found named %s.\n"), arg);
00140 
00141           if (nquery (_("\
00142 Ignore function pending future shared library load? ")))
00143             {
00144               /* Add the unverified skiplist entry.  */
00145               skip_function (arg);
00146             }
00147           return;
00148         }
00149 
00150       skip_function (arg);
00151     }
00152 }
00153 
00154 static void
00155 skip_info (char *arg, int from_tty)
00156 {
00157   struct skiplist_entry *e;
00158   int num_printable_entries = 0;
00159   struct value_print_options opts;
00160   struct cleanup *tbl_chain;
00161 
00162   get_user_print_options (&opts);
00163 
00164   /* Count the number of rows in the table and see if we need space for a
00165      64-bit address anywhere.  */
00166   ALL_SKIPLIST_ENTRIES (e)
00167     if (arg == NULL || number_is_in_list (arg, e->number))
00168       num_printable_entries++;
00169 
00170   if (num_printable_entries == 0)
00171     {
00172       if (arg == NULL)
00173         ui_out_message (current_uiout, 0, _("\
00174 Not skipping any files or functions.\n"));
00175       else
00176         ui_out_message (current_uiout, 0,
00177                         _("No skiplist entries found with number %s.\n"), arg);
00178 
00179       return;
00180     }
00181 
00182   tbl_chain = make_cleanup_ui_out_table_begin_end (current_uiout, 4,
00183                                                    num_printable_entries,
00184                                                    "SkiplistTable");
00185 
00186   ui_out_table_header (current_uiout, 7, ui_left, "number", "Num");      /* 1 */
00187   ui_out_table_header (current_uiout, 14, ui_left, "type", "Type");      /* 2 */
00188   ui_out_table_header (current_uiout, 3, ui_left, "enabled", "Enb");     /* 3 */
00189   ui_out_table_header (current_uiout, 40, ui_noalign, "what", "What");   /* 4 */
00190   ui_out_table_body (current_uiout);
00191 
00192   ALL_SKIPLIST_ENTRIES (e)
00193     {
00194       struct cleanup *entry_chain;
00195 
00196       QUIT;
00197       if (arg != NULL && !number_is_in_list (arg, e->number))
00198         continue;
00199 
00200       entry_chain = make_cleanup_ui_out_tuple_begin_end (current_uiout,
00201                                                          "blklst-entry");
00202       ui_out_field_int (current_uiout, "number", e->number);             /* 1 */
00203 
00204       if (e->function_name != NULL)
00205         ui_out_field_string (current_uiout, "type", "function");         /* 2 */
00206       else if (e->filename != NULL)
00207         ui_out_field_string (current_uiout, "type", "file");             /* 2 */
00208       else
00209         internal_error (__FILE__, __LINE__, _("\
00210 Skiplist entry should have either a filename or a function name."));
00211 
00212       if (e->enabled)
00213         ui_out_field_string (current_uiout, "enabled", "y");             /* 3 */
00214       else
00215         ui_out_field_string (current_uiout, "enabled", "n");             /* 3 */
00216 
00217       if (e->function_name != NULL)
00218         ui_out_field_string (current_uiout, "what", e->function_name);   /* 4 */
00219       else if (e->filename != NULL)
00220         ui_out_field_string (current_uiout, "what", e->filename);        /* 4 */
00221 
00222       ui_out_text (current_uiout, "\n");
00223       do_cleanups (entry_chain);
00224     }
00225 
00226   do_cleanups (tbl_chain);
00227 }
00228 
00229 static void
00230 skip_enable_command (char *arg, int from_tty)
00231 {
00232   struct skiplist_entry *e;
00233   int found = 0;
00234 
00235   ALL_SKIPLIST_ENTRIES (e)
00236     if (arg == NULL || number_is_in_list (arg, e->number))
00237       {
00238         e->enabled = 1;
00239         found = 1;
00240       }
00241 
00242   if (!found)
00243     error (_("No skiplist entries found with number %s."), arg);
00244 }
00245 
00246 static void
00247 skip_disable_command (char *arg, int from_tty)
00248 {
00249   struct skiplist_entry *e;
00250   int found = 0;
00251 
00252   ALL_SKIPLIST_ENTRIES (e)
00253     if (arg == NULL || number_is_in_list (arg, e->number))
00254       {
00255         e->enabled = 0;
00256         found = 1;
00257       }
00258 
00259   if (!found)
00260     error (_("No skiplist entries found with number %s."), arg);
00261 }
00262 
00263 static void
00264 skip_delete_command (char *arg, int from_tty)
00265 {
00266   struct skiplist_entry *e, *temp, *b_prev;
00267   int found = 0;
00268 
00269   b_prev = 0;
00270   ALL_SKIPLIST_ENTRIES_SAFE (e, temp)
00271     if (arg == NULL || number_is_in_list (arg, e->number))
00272       {
00273         if (b_prev != NULL)
00274           b_prev->next = e->next;
00275         else
00276           skiplist_entry_chain = e->next;
00277 
00278         xfree (e->function_name);
00279         xfree (e->filename);
00280         xfree (e);
00281         found = 1;
00282       }
00283     else
00284       {
00285         b_prev = e;
00286       }
00287 
00288   if (!found)
00289     error (_("No skiplist entries found with number %s."), arg);
00290 }
00291 
00292 /* Create a skiplist entry for the given function NAME and add it to the
00293    list.  */
00294 
00295 static void
00296 skip_function (const char *name)
00297 {
00298   struct skiplist_entry *e = XZALLOC (struct skiplist_entry);
00299 
00300   e->enabled = 1;
00301   e->function_name = xstrdup (name);
00302 
00303   add_skiplist_entry (e);
00304 
00305   printf_filtered (_("Function %s will be skipped when stepping.\n"), name);
00306 }
00307 
00308 /* Add the given skiplist entry to our list, and set the entry's number.  */
00309 
00310 static void
00311 add_skiplist_entry (struct skiplist_entry *e)
00312 {
00313   struct skiplist_entry *e1;
00314 
00315   e->number = ++skiplist_entry_count;
00316 
00317   /* Add to the end of the chain so that the list of
00318      skiplist entries will be in numerical order.  */
00319 
00320   e1 = skiplist_entry_chain;
00321   if (e1 == NULL)
00322     skiplist_entry_chain = e;
00323   else
00324     {
00325       while (e1->next)
00326         e1 = e1->next;
00327       e1->next = e;
00328     }
00329 }
00330 
00331 
00332 /* See skip.h.  */
00333 
00334 int
00335 function_name_is_marked_for_skip (const char *function_name,
00336                                   const struct symtab_and_line *function_sal)
00337 {
00338   int searched_for_fullname = 0;
00339   const char *fullname = NULL;
00340   struct skiplist_entry *e;
00341 
00342   if (function_name == NULL)
00343     return 0;
00344 
00345   ALL_SKIPLIST_ENTRIES (e)
00346     {
00347       if (!e->enabled)
00348         continue;
00349 
00350       /* Does the pc we're stepping into match e's stored pc? */
00351       if (e->function_name != NULL
00352           && strcmp_iw (function_name, e->function_name) == 0)
00353         return 1;
00354 
00355       if (e->filename != NULL)
00356         {
00357           /* Check first sole SYMTAB->FILENAME.  It does not need to be
00358              a substring of symtab_to_fullname as it may contain "./" etc.  */
00359           if (function_sal->symtab != NULL
00360               && compare_filenames_for_search (function_sal->symtab->filename,
00361                                                e->filename))
00362             return 1;
00363 
00364           /* Before we invoke realpath, which can get expensive when many
00365              files are involved, do a quick comparison of the basenames.  */
00366           if (!basenames_may_differ
00367               && (function_sal->symtab == NULL
00368                   || filename_cmp (lbasename (function_sal->symtab->filename),
00369                                    lbasename (e->filename)) != 0))
00370             continue;
00371 
00372           /* Get the filename corresponding to this FUNCTION_SAL, if we haven't
00373              yet.  */
00374           if (!searched_for_fullname)
00375             {
00376               if (function_sal->symtab != NULL)
00377                 fullname = symtab_to_fullname (function_sal->symtab);
00378               searched_for_fullname = 1;
00379             }
00380           if (fullname != NULL
00381               && compare_filenames_for_search (fullname, e->filename))
00382             return 1;
00383         }
00384     }
00385 
00386   return 0;
00387 }
00388 
00389 /* Provide a prototype to silence -Wmissing-prototypes.  */
00390 extern initialize_file_ftype _initialize_step_skip;
00391 
00392 void
00393 _initialize_step_skip (void)
00394 {
00395   static struct cmd_list_element *skiplist = NULL;
00396   struct cmd_list_element *c;
00397 
00398   skiplist_entry_chain = 0;
00399   skiplist_entry_count = 0;
00400 
00401   add_prefix_cmd ("skip", class_breakpoint, skip_function_command, _("\
00402 Ignore a function while stepping.\n\
00403 Usage: skip [FUNCTION NAME]\n\
00404 If no function name is given, ignore the current function."),
00405                   &skiplist, "skip ", 1, &cmdlist);
00406 
00407   c = add_cmd ("file", class_breakpoint, skip_file_command, _("\
00408 Ignore a file while stepping.\n\
00409 Usage: skip file [FILENAME]\n\
00410 If no filename is given, ignore the current file."),
00411                &skiplist);
00412   set_cmd_completer (c, filename_completer);
00413 
00414   c = add_cmd ("function", class_breakpoint, skip_function_command, _("\
00415 Ignore a function while stepping.\n\
00416 Usage: skip function [FUNCTION NAME]\n\
00417 If no function name is given, skip the current function."),
00418                &skiplist);
00419   set_cmd_completer (c, location_completer);
00420 
00421   add_cmd ("enable", class_breakpoint, skip_enable_command, _("\
00422 Enable skip entries.  You can specify numbers (e.g. \"skip enable 1 3\"), \
00423 ranges (e.g. \"skip enable 4-8\"), or both (e.g. \"skip enable 1 3 4-8\").\n\n\
00424 If you don't specify any numbers or ranges, we'll enable all skip entries.\n\n\
00425 Usage: skip enable [NUMBERS AND/OR RANGES]"),
00426            &skiplist);
00427 
00428   add_cmd ("disable", class_breakpoint, skip_disable_command, _("\
00429 Disable skip entries.  You can specify numbers (e.g. \"skip disable 1 3\"), \
00430 ranges (e.g. \"skip disable 4-8\"), or both (e.g. \"skip disable 1 3 4-8\").\n\n\
00431 If you don't specify any numbers or ranges, we'll disable all skip entries.\n\n\
00432 Usage: skip disable [NUMBERS AND/OR RANGES]"),
00433            &skiplist);
00434 
00435   add_cmd ("delete", class_breakpoint, skip_delete_command, _("\
00436 Delete skip entries.  You can specify numbers (e.g. \"skip delete 1 3\"), \
00437 ranges (e.g. \"skip delete 4-8\"), or both (e.g. \"skip delete 1 3 4-8\").\n\n\
00438 If you don't specify any numbers or ranges, we'll delete all skip entries.\n\n\
00439 Usage: skip delete [NUMBERS AND/OR RANGES]"),
00440            &skiplist);
00441 
00442   add_info ("skip", skip_info, _("\
00443 Display the status of skips.  You can specify numbers (e.g. \"skip info 1 3\"), \
00444 ranges (e.g. \"skip info 4-8\"), or both (e.g. \"skip info 1 3 4-8\").\n\n\
00445 If you don't specify any numbers or ranges, we'll show all skips.\n\n\
00446 Usage: skip info [NUMBERS AND/OR RANGES]\n\
00447 The \"Type\" column indicates one of:\n\
00448 \tfile        - ignored file\n\
00449 \tfunction    - ignored function"));
00450 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Defines