xref: /dragonfly/contrib/gdb-7/gdb/skip.c (revision 51871435)
1 /* Skipping uninteresting files and functions while stepping.
2 
3    Copyright (C) 2011-2012 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 "gdb_string.h"
24 #include "symtab.h"
25 #include "gdbcmd.h"
26 #include "command.h"
27 #include "completer.h"
28 #include "stack.h"
29 #include "cli/cli-utils.h"
30 #include "arch-utils.h"
31 #include "linespec.h"
32 #include "objfiles.h"
33 #include "exceptions.h"
34 #include "breakpoint.h" /* for get_sal_arch () */
35 
36 struct skiplist_entry
37 {
38   int number;
39 
40   /* NULL if this isn't a skiplist entry for an entire file.
41      The skiplist entry owns this pointer.  */
42   char *filename;
43 
44   /* The name of the marked-for-skip function, if this is a skiplist
45      entry for a function.  Note that this might be non-null even if
46      the pc is 0 if the entry is pending a shared library load.
47 
48      The skiplist entry owns this pointer.  */
49   char *function_name;
50 
51   /* 0 if this is a skiplist entry for an entire file, or if this
52      entry will be on a function, pending a shared library load.  */
53   CORE_ADDR pc;
54 
55   /* Architecture we used to create the skiplist entry. May be null
56      if the entry is pending a shared library load.  */
57   struct gdbarch *gdbarch;
58 
59   int enabled;
60   int pending;
61 
62   struct skiplist_entry *next;
63 };
64 
65 static void skip_function_command (char *arg, int from_tty);
66 static void skip_file_command (char *arg, int from_tty);
67 static void skip_info (char *arg, int from_tty);
68 
69 static void add_skiplist_entry (struct skiplist_entry *e);
70 static void skip_function_pc (CORE_ADDR pc, char *name,
71 			      struct gdbarch *arch,
72 			      int pending);
73 
74 static struct skiplist_entry *skiplist_entry_chain;
75 static int skiplist_entry_count;
76 
77 #define ALL_SKIPLIST_ENTRIES(E) \
78   for (E = skiplist_entry_chain; E; E = E->next)
79 
80 #define ALL_SKIPLIST_ENTRIES_SAFE(E,TMP) \
81   for (E = skiplist_entry_chain;         \
82        E ? (TMP = E->next, 1) : 0;       \
83        E = TMP)
84 
85 static void
86 skip_file_command (char *arg, int from_tty)
87 {
88   struct skiplist_entry *e;
89   struct symtab *symtab;
90   int pending = 0;
91   char *filename = 0;
92 
93   /* If no argument was given, try to default to the last
94      displayed codepoint.  */
95   if (arg == 0)
96     {
97       symtab = get_last_displayed_symtab ();
98       if (symtab == 0)
99 	error (_("No default file now."));
100       else
101 	filename = symtab->filename;
102     }
103   else
104     {
105       symtab = lookup_symtab (arg);
106       if (symtab == 0)
107 	{
108 	  fprintf_filtered (gdb_stderr, _("No source file named %s.\n"), arg);
109 	  if (!nquery (_("\
110 Ignore file pending future shared library load? ")))
111 	    return;
112 
113 	  pending = 1;
114 	  filename = arg;
115 	}
116       else
117 	filename = symtab->filename;
118     }
119 
120   e = XZALLOC (struct skiplist_entry);
121   e->filename = xstrdup (filename);
122   e->enabled = 1;
123   e->pending = pending;
124   if (symtab != 0)
125     e->gdbarch = get_objfile_arch (symtab->objfile);
126 
127   add_skiplist_entry (e);
128 
129   printf_filtered (_("File %s will be skipped when stepping.\n"), filename);
130 }
131 
132 static void
133 skip_function_command (char *arg, int from_tty)
134 {
135   CORE_ADDR func_pc;
136   char *name = NULL;
137 
138   /* Default to the current function if no argument is given.  */
139   if (arg == 0)
140     {
141       CORE_ADDR pc;
142       if (!last_displayed_sal_is_valid ())
143 	error (_("No default function now."));
144 
145       pc = get_last_displayed_addr ();
146       if (!find_pc_partial_function (pc, &name, &func_pc, 0))
147 	{
148 	  error (_("No function found containing current program point %s."),
149 		  paddress (get_current_arch (), pc));
150 	}
151       skip_function_pc (func_pc, name, get_current_arch (), 0);
152     }
153   else
154     {
155       /* Decode arg.  We set funfirstline=1 so decode_line_1 will give us the
156 	 first line of the function specified, if it can, and so that we'll
157 	 reject variable names and the like.  */
158 
159       int i;
160       int pending = 0;
161       char *orig_arg = arg; /* decode_line_1 modifies the arg pointer.  */
162       volatile struct gdb_exception decode_exception;
163       struct symtabs_and_lines sals = { 0 };
164 
165       TRY_CATCH (decode_exception, RETURN_MASK_ERROR)
166 	{
167 	  sals = decode_line_1 (&arg, DECODE_LINE_FUNFIRSTLINE, 0, 0);
168 	}
169 
170       if (decode_exception.reason < 0)
171         {
172           if (decode_exception.error != NOT_FOUND_ERROR)
173             throw_exception (decode_exception);
174 
175 	  fprintf_filtered (gdb_stderr,
176 			    _("No function found named %s.\n"), orig_arg);
177 
178 	  if (nquery (_("\
179 Ignore function pending future shared library load? ")))
180 	    {
181 	      /* Add the pending skiplist entry.  */
182 	      skip_function_pc (0, orig_arg, 0, 1);
183 	    }
184 
185 	  return;
186 	}
187 
188       if (sals.nelts > 1)
189 	error (_("Specify just one function at a time."));
190       if (strlen (arg) != 0)
191 	error (_("Junk at end of arguments."));
192 
193       /* The pc decode_line_1 gives us is the first line of the function,
194 	 but we actually want the line before that.  The call to
195 	 find_pc_partial_function gets us the value we actually want.  */
196       {
197 	struct symtab_and_line sal = sals.sals[0];
198 	CORE_ADDR pc = sal.pc;
199 	CORE_ADDR func_start = 0;
200 	struct gdbarch *arch = get_sal_arch (sal);
201 
202 	if (!find_pc_partial_function (pc, &name, &func_start, 0))
203 	  {
204 	    error (_("No function found containing program point %s."),
205 		     paddress (arch, pc));
206 	  }
207 
208 	skip_function_pc (func_start, name, arch, 0);
209       }
210     }
211 }
212 
213 static void
214 skip_info (char *arg, int from_tty)
215 {
216   struct skiplist_entry *e;
217   int num_printable_entries = 0;
218   int address_width = 10;
219   struct value_print_options opts;
220   struct cleanup *tbl_chain;
221 
222   get_user_print_options (&opts);
223 
224   /* Count the number of rows in the table and see if we need space for a
225      64-bit address anywhere.  */
226   ALL_SKIPLIST_ENTRIES (e)
227     if (arg == 0 || number_is_in_list (arg, e->number))
228       {
229 	num_printable_entries++;
230 	if (e->gdbarch && gdbarch_addr_bit (e->gdbarch) > 32)
231 	  address_width = 18;
232       }
233 
234   if (num_printable_entries == 0)
235     {
236       if (arg == 0)
237 	ui_out_message (current_uiout, 0, _("\
238 Not skipping any files or functions.\n"));
239       else
240 	ui_out_message (current_uiout, 0,
241 			_("No skiplist entries found with number %s.\n"), arg);
242 
243       return;
244     }
245 
246   if (opts.addressprint)
247     tbl_chain = make_cleanup_ui_out_table_begin_end (current_uiout, 5,
248 						     num_printable_entries,
249 						     "SkiplistTable");
250   else
251     tbl_chain
252        = make_cleanup_ui_out_table_begin_end (current_uiout, 4,
253 					      num_printable_entries,
254 					      "SkiplistTable");
255 
256   ui_out_table_header (current_uiout, 7, ui_left, "number", "Num");      /* 1 */
257   ui_out_table_header (current_uiout, 14, ui_left, "type", "Type");      /* 2 */
258   ui_out_table_header (current_uiout, 3, ui_left, "enabled", "Enb");     /* 3 */
259   if (opts.addressprint)
260     {
261       ui_out_table_header (current_uiout, address_width, ui_left,
262 			   "addr", "Address");                           /* 4 */
263     }
264   ui_out_table_header (current_uiout, 40, ui_noalign, "what", "What");   /* 5 */
265   ui_out_table_body (current_uiout);
266 
267   ALL_SKIPLIST_ENTRIES (e)
268     {
269       struct cleanup *entry_chain;
270 
271       QUIT;
272       if (arg != 0 && !number_is_in_list (arg, e->number))
273 	continue;
274 
275       entry_chain = make_cleanup_ui_out_tuple_begin_end (current_uiout,
276 							 "blklst-entry");
277       ui_out_field_int (current_uiout, "number", e->number);             /* 1 */
278 
279       if (e->function_name != 0)
280 	ui_out_field_string (current_uiout, "type", "function");         /* 2 */
281       else if (e->filename != 0)
282 	ui_out_field_string (current_uiout, "type", "file");             /* 2 */
283       else
284 	internal_error (__FILE__, __LINE__, _("\
285 Skiplist entry should have either a filename or a function name."));
286 
287       if (e->enabled)
288 	ui_out_field_string (current_uiout, "enabled", "y");             /* 3 */
289       else
290 	ui_out_field_string (current_uiout, "enabled", "n");             /* 3 */
291 
292       if (opts.addressprint)
293 	{
294 	  if (e->pc != 0)
295 	    ui_out_field_core_addr (current_uiout, "addr",
296 				    e->gdbarch, e->pc);                  /* 4 */
297 	  else
298 	    ui_out_field_string (current_uiout, "addr", "");             /* 4 */
299 	}
300 
301       if (!e->pending && e->function_name != 0)
302 	{
303 	   struct symbol *sym;
304 
305 	   gdb_assert (e->pc != 0);
306 	   sym = find_pc_function (e->pc);
307 	   if (sym)
308 	     ui_out_field_fmt (current_uiout, "what", "%s at %s:%d",
309 			       sym->ginfo.name,
310 			       sym->symtab->filename,
311 			       sym->line);
312 	   else
313 	     ui_out_field_string (current_uiout, "what", "?");
314 	}
315       else if (e->pending && e->function_name != 0)
316 	{
317 	  ui_out_field_fmt (current_uiout, "what", "%s (PENDING)",
318 			    e->function_name);
319 	}
320       else if (!e->pending && e->filename != 0)
321 	ui_out_field_string (current_uiout, "what", e->filename);
322       else if (e->pending && e->filename != 0)
323 	ui_out_field_fmt (current_uiout, "what", "%s (PENDING)",
324 			  e->filename);
325 
326       ui_out_text (current_uiout, "\n");
327       do_cleanups (entry_chain);
328     }
329 
330   do_cleanups (tbl_chain);
331 }
332 
333 static void
334 skip_enable_command (char *arg, int from_tty)
335 {
336   struct skiplist_entry *e;
337   int found = 0;
338 
339   ALL_SKIPLIST_ENTRIES (e)
340     if (arg == 0 || number_is_in_list (arg, e->number))
341       {
342         e->enabled = 1;
343         found = 1;
344       }
345 
346   if (!found)
347     error (_("No skiplist entries found with number %s."), arg);
348 }
349 
350 static void
351 skip_disable_command (char *arg, int from_tty)
352 {
353   struct skiplist_entry *e;
354   int found = 0;
355 
356   ALL_SKIPLIST_ENTRIES (e)
357     if (arg == 0 || number_is_in_list (arg, e->number))
358       {
359 	e->enabled = 0;
360         found = 1;
361       }
362 
363   if (!found)
364     error (_("No skiplist entries found with number %s."), arg);
365 }
366 
367 static void
368 skip_delete_command (char *arg, int from_tty)
369 {
370   struct skiplist_entry *e, *temp, *b_prev;
371   int found = 0;
372 
373   b_prev = 0;
374   ALL_SKIPLIST_ENTRIES_SAFE (e, temp)
375     if (arg == 0 || number_is_in_list (arg, e->number))
376       {
377 	if (b_prev != 0)
378 	  b_prev->next = e->next;
379 	else
380 	  skiplist_entry_chain = e->next;
381 
382 	xfree (e->function_name);
383 	xfree (e->filename);
384 	xfree (e);
385         found = 1;
386       }
387     else
388       {
389 	b_prev = e;
390       }
391 
392   if (!found)
393     error (_("No skiplist entries found with number %s."), arg);
394 }
395 
396 /* Create a skiplist entry for the given pc corresponding to the given
397    function name and add it to the list.  */
398 
399 static void
400 skip_function_pc (CORE_ADDR pc, char *name, struct gdbarch *arch,
401 		  int pending)
402 {
403   struct skiplist_entry *e = XZALLOC (struct skiplist_entry);
404 
405   e->pc = pc;
406   e->gdbarch = arch;
407   e->enabled = 1;
408   e->pending = pending;
409   e->function_name = xstrdup (name);
410 
411   add_skiplist_entry (e);
412 
413   if (!pending)
414     printf_filtered (_("Function %s at %s will be skipped when stepping.\n"),
415 		     name, paddress (get_current_arch (), pc));
416   else
417     printf_filtered (_("Function %s will be skipped when stepping, "
418 		       "pending shared library load.\n"),
419 		     name);
420 }
421 
422 /* Add the given skiplist entry to our list, and set the entry's number.  */
423 
424 static void
425 add_skiplist_entry (struct skiplist_entry *e)
426 {
427   struct skiplist_entry *e1;
428 
429   e->number = ++skiplist_entry_count;
430 
431   /* Add to the end of the chain so that the list of
432      skiplist entries will be in numerical order.  */
433 
434   e1 = skiplist_entry_chain;
435   if (e1 == 0)
436     skiplist_entry_chain = e;
437   else
438     {
439       while (e1->next)
440 	e1 = e1->next;
441       e1->next = e;
442     }
443 }
444 
445 /* Does the given pc correspond to the beginning of a skipped function? */
446 
447 int
448 function_pc_is_marked_for_skip (CORE_ADDR pc)
449 {
450   int searched_for_sal = 0;
451   struct symtab_and_line sal;
452   char *filename = NULL;
453   struct skiplist_entry *e;
454 
455   ALL_SKIPLIST_ENTRIES (e)
456     {
457       if (!e->enabled || e->pending)
458 	continue;
459 
460       /* Does the pc we're stepping into match e's stored pc? */
461       if (e->pc != 0 && pc == e->pc)
462 	return 1;
463 
464       if (e->filename != 0)
465 	{
466 	  /* Get the filename corresponding to this pc, if we haven't
467 	   * yet.  */
468 	  if (!searched_for_sal)
469 	    {
470 	      sal = find_pc_line (pc, 0);
471               if (sal.symtab != 0)
472                 filename = sal.symtab->filename;
473 	      searched_for_sal = 1;
474 	    }
475 	  if (filename != 0 && strcmp (filename, e->filename) == 0)
476 	    return 1;
477 	}
478     }
479 
480   return 0;
481 }
482 
483 /* Re-set the skip list after symbols have been re-loaded.  */
484 void
485 skip_re_set (void)
486 {
487   struct skiplist_entry *e;
488 
489   ALL_SKIPLIST_ENTRIES (e)
490     {
491       if (e->filename != 0)
492 	{
493 	  /* If it's an entry telling us to skip a file, but the entry is
494 	     currently pending a solib load, let's see if we now know
495 	     about the file.  */
496 	  struct symtab *symtab = lookup_symtab (e->filename);
497 	  if (symtab != 0)
498 	    {
499 	      xfree (e->filename);
500 	      e->filename = xstrdup (symtab->filename);
501 	      e->gdbarch = get_objfile_arch (symtab->objfile);
502 	      e->pending = 0;
503 	    }
504 	  else
505 	    {
506 	      e->pending = 1;
507 	    }
508 	}
509       else if (e->function_name != 0)
510         {
511 	  char *func_name = e->function_name;
512 	  struct symtabs_and_lines sals = { 0 };
513 	  volatile struct gdb_exception decode_exception;
514 
515 	  TRY_CATCH (decode_exception, RETURN_MASK_ERROR)
516 	    {
517 	      sals = decode_line_1 (&func_name, DECODE_LINE_FUNFIRSTLINE, 0, 0);
518 	    }
519 
520 	  if (decode_exception.reason >= 0
521               && sals.nelts == 1 && strlen (func_name) == 0)
522 	    {
523 	      struct symtab_and_line sal = sals.sals[0];
524 	      CORE_ADDR pc = sal.pc;
525 	      CORE_ADDR func_start = 0;
526 	      struct gdbarch *arch = get_sal_arch (sal);
527               char *func_name;
528 
529 	      if (find_pc_partial_function (pc, &func_name, &func_start, 0))
530 		{
531 		  e->pending = 0;
532                   e->function_name = xstrdup (func_name);
533 		  e->pc = func_start;
534 		  e->gdbarch = arch;
535 		}
536 	    }
537 	  else
538 	    {
539 	      e->pending = 1;
540 	    }
541         }
542     }
543 }
544 
545 void
546 _initialize_step_skip (void)
547 {
548   struct cmd_list_element *c;
549 
550   skiplist_entry_chain = 0;
551   skiplist_entry_count = 0;
552 
553   add_prefix_cmd ("skip", class_breakpoint, skip_function_command, _("\
554 Ignore a function while stepping.\n\
555 Usage: skip [FUNCTION NAME]\n\
556 If no function name is given, ignore the current function."),
557                   &skiplist, "skip ", 1, &cmdlist);
558 
559   c = add_cmd ("file", class_breakpoint, skip_file_command, _("\
560 Ignore a file while stepping.\n\
561 Usage: skip file [FILENAME]\n\
562 If no filename is given, ignore the current file."),
563 	       &skiplist);
564   set_cmd_completer (c, filename_completer);
565 
566   c = add_cmd ("function", class_breakpoint, skip_function_command, _("\
567 Ignore a function while stepping.\n\
568 Usage: skip function [FUNCTION NAME]\n\
569 If no function name is given, skip the current function."),
570 	       &skiplist);
571   set_cmd_completer (c, location_completer);
572 
573   add_cmd ("enable", class_breakpoint, skip_enable_command, _("\
574 Enable skip entries.  You can specify numbers (e.g. \"skip enable 1 3\"), \
575 ranges (e.g. \"skip enable 4-8\"), or both (e.g. \"skip enable 1 3 4-8\").\n\n\
576 If you don't specify any numbers or ranges, we'll enable all skip entries.\n\n\
577 Usage: skip enable [NUMBERS AND/OR RANGES]"),
578 	   &skiplist);
579 
580   add_cmd ("disable", class_breakpoint, skip_disable_command, _("\
581 Disable skip entries.  You can specify numbers (e.g. \"skip disable 1 3\"), \
582 ranges (e.g. \"skip disable 4-8\"), or both (e.g. \"skip disable 1 3 4-8\").\n\n\
583 If you don't specify any numbers or ranges, we'll disable all skip entries.\n\n\
584 Usage: skip disable [NUMBERS AND/OR RANGES]"),
585 	   &skiplist);
586 
587   add_cmd ("delete", class_breakpoint, skip_delete_command, _("\
588 Delete skip entries.  You can specify numbers (e.g. \"skip delete 1 3\"), \
589 ranges (e.g. \"skip delete 4-8\"), or both (e.g. \"skip delete 1 3 4-8\").\n\n\
590 If you don't specify any numbers or ranges, we'll delete all skip entries.\n\n\
591 Usage: skip delete [NUMBERS AND/OR RANGES]"),
592            &skiplist);
593 
594   add_info ("skip", skip_info, _("\
595 Display the status of skips.  You can specify numbers (e.g. \"skip info 1 3\"), \
596 ranges (e.g. \"skip info 4-8\"), or both (e.g. \"skip info 1 3 4-8\").\n\n\
597 If you don't specify any numbers or ranges, we'll show all skips.\n\n\
598 Usage: skip info [NUMBERS AND/OR RANGES]\n\
599 The \"Type\" column indicates one of:\n\
600 \tfile        - ignored file\n\
601 \tfunction    - ignored function"));
602 }
603