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