1 /* Everything about breakpoints, for GDB.
2 Copyright (C) 1986, 1987 Free Software Foundation, Inc.
4 GDB is distributed in the hope that it will be useful, but WITHOUT ANY
5 WARRANTY. No author or distributor accepts responsibility to anyone
6 for the consequences of using it or for whether it serves any
7 particular purpose or works at all, unless he says so in writing.
8 Refer to the GDB General Public License for full details.
10 Everyone is granted permission to copy, modify and redistribute GDB,
11 but only under the conditions described in the GDB General Public
12 License. A copy of this license is supposed to have been given to you
13 along with GDB so you can know your rights and responsibilities. It
14 should be in a file named COPYING. Among other things, the copyright
15 notice and this notice must be preserved on all copies.
17 In other words, go ahead and share GDB, but don't try to stop
18 anyone else from sharing it farther. Help stamp out software hoarding!
22 #include "initialize.h"
29 /* This is the sequence of bytes we insert for a breakpoint. */
31 static char break_insn[] = BREAKPOINT;
33 /* States of enablement of breakpoint.
34 `temporary' means disable when hit.
35 `once' means delete when hit. */
37 enum enable { disabled, enabled, temporary, delete};
41 struct breakpoint *next;
42 /* Number assigned to distinguish breakpoints. */
44 /* Address to break at. */
46 /* Line number of this address. Redundant. */
48 /* Symtab of file of this address. Redundant. */
49 struct symtab *symtab;
50 /* Zero means disabled; remember the info but don't break here. */
52 /* Number of stops at this breakpoint that should
53 be continued automatically before really stopping. */
55 /* "Real" contents of byte where breakpoint has been inserted.
56 Valid only when breakpoints are in the program. */
57 char shadow_contents[sizeof break_insn];
58 /* Nonzero if this breakpoint is now inserted. */
60 /* Nonzero if this is not the first breakpoint in the list
61 for the given address. */
63 /* Chain of command lines to execute when this breakpoint is hit. */
64 struct command_line *commands;
65 /* Stack depth (frame). If nonzero, break only if fp equals this. */
67 /* Conditional. Break only if this expression's value is nonzero. */
68 struct expression *cond;
71 #define ALL_BREAKPOINTS(b) for (b = breakpoint_chain; b; b = b->next)
73 /* Chain of all breakpoints defined. */
75 struct breakpoint *breakpoint_chain;
77 /* Number of last breakpoint made. */
79 static int breakpoint_count;
81 /* Default address, symtab and line to put a breakpoint at
82 for "break" command with no arg.
83 if default_breakpoint_valid is zero, the other three are
84 not valid, and "break" with no arg is an error.
86 This set by print_stack_frame, which calls set_default_breakpoint. */
88 int default_breakpoint_valid;
89 CORE_ADDR default_breakpoint_address;
90 struct symtab *default_breakpoint_symtab;
91 int default_breakpoint_line;
93 /* Remaining commands (not yet executed)
94 of last breakpoint hit. */
96 struct command_line *breakpoint_commands;
100 extern char *read_line ();
102 static void delete_breakpoint ();
103 void clear_momentary_breakpoints ();
104 void breakpoint_auto_delete ();
106 /* condition N EXP -- set break condition of breakpoint N to EXP. */
109 condition_command (arg, from_tty)
113 register struct breakpoint *b;
116 register struct expression *expr;
119 error_no_arg ("breakpoint number");
122 while (*p >= '0' && *p <= '9') p++;
126 if (b->number == bnum)
134 printf ("Breakpoint %d now unconditional.\n", bnum);
138 if (*p != ' ' && *p != '\t')
139 error ("Arguments must be an integer (breakpoint number) and an expression.");
141 /* Find start of expression */
142 while (*p == ' ' || *p == '\t') p++;
145 b->cond = (struct expression *) parse_c_1 (&arg, block_for_pc (b->address));
150 error ("No breakpoint number %d.", bnum);
154 commands_command (arg)
157 register struct breakpoint *b;
158 register char *p, *p1;
160 struct command_line *l;
163 error_no_arg ("breakpoint number");
165 /* If we allowed this, we would have problems with when to
166 free the storage, if we change the commands currently
169 if (breakpoint_commands)
170 error ("Can't use the \"commands\" command among a breakpoint's commands.");
173 if (! (*p >= '0' && *p <= '9'))
174 error ("Argument must be integer (a breakpoint number).");
176 while (*p >= '0' && *p <= '9') p++;
178 error ("Unexpected extra arguments following breakpoint number.");
183 if (b->number == bnum)
185 if (input_from_terminal_p ())
186 printf ("Type commands for when breakpoint %d is hit, one per line.\n\
187 End with a line saying just \"end\".\n", bnum);
188 l = read_command_lines ();
189 free_command_lines (&b->commands);
193 error ("No breakpoint number %d.", bnum);
196 /* Called from command loop to execute the commands
197 associated with the breakpoint we just stopped at. */
200 do_breakpoint_commands ()
202 while (breakpoint_commands)
204 char *line = breakpoint_commands->line;
205 breakpoint_commands = breakpoint_commands->next;
206 execute_command (line, 0);
207 /* If command was "cont", breakpoint_commands is now 0,
208 of if we stopped at yet another breakpoint which has commands,
209 it is now the commands for the new breakpoint. */
211 clear_momentary_breakpoints ();
214 /* Used when the program is proceeded, to eliminate any remaining
215 commands attached to the previous breakpoint we stopped at. */
218 clear_breakpoint_commands ()
220 breakpoint_commands = 0;
221 breakpoint_auto_delete (0);
224 /* Functions to get and set the current list of pending
225 breakpoint commands. These are used by run_stack_dummy
226 to preserve the commands around a function call. */
228 struct command_line *
229 get_breakpoint_commands ()
231 return breakpoint_commands;
235 set_breakpoint_commands (cmds)
236 struct command_line *cmds;
238 breakpoint_commands = cmds;
241 /* insert_breakpoints is used when starting or continuing the program.
242 remove_breakpoints is used when the program stops.
243 Both return zero if successful,
244 or an `errno' value if could not write the inferior. */
247 insert_breakpoints ()
249 register struct breakpoint *b;
252 /* printf ("Inserting breakpoints.\n"); */
254 if (b->enable != disabled && ! b->inserted && ! b->duplicate)
256 read_memory (b->address, b->shadow_contents, sizeof break_insn);
257 val = write_memory (b->address, break_insn, sizeof break_insn);
260 /* printf ("Inserted breakpoint at 0x%x, shadow 0x%x, 0x%x.\n",
261 b->address, b->shadow_contents[0], b->shadow_contents[1]); */
268 remove_breakpoints ()
270 register struct breakpoint *b;
273 /* printf ("Removing breakpoints.\n"); */
277 val = write_memory (b->address, b->shadow_contents, sizeof break_insn);
281 /* printf ("Removed breakpoint at 0x%x, shadow 0x%x, 0x%x.\n",
282 b->address, b->shadow_contents[0], b->shadow_contents[1]); */
288 /* Clear the "inserted" flag in all breakpoints.
289 This is done when the inferior is loaded. */
292 mark_breakpoints_out ()
294 register struct breakpoint *b;
300 /* breakpoint_here_p (PC) returns 1 if an enabled breakpoint exists at PC.
301 When continuing from a location with a breakpoint,
302 we actually single step once before calling insert_breakpoints. */
305 breakpoint_here_p (pc)
308 register struct breakpoint *b;
311 if (b->enable != disabled && b->address == pc)
317 /* Return 0 if PC is not the address just after a breakpoint,
318 or -1 if breakpoint says do not stop now,
319 or -2 if breakpoint says it has deleted itself and don't stop,
320 or -3 if hit a breakpoint number -3 (delete when program stops),
321 or else the number of the breakpoint,
322 with 0x1000000 added for a silent breakpoint. */
325 breakpoint_stop_status (pc, frame)
329 register struct breakpoint *b;
330 register int cont = 0;
332 /* Get the address where the breakpoint would have been. */
333 pc -= DECR_PC_AFTER_BREAK;
336 if (b->enable != disabled && b->address == pc)
338 if (b->frame && b->frame != frame)
345 value_zero = value_zerop (evaluate_expression (b->cond));
348 if (b->cond && value_zero)
352 else if (b->ignore_count > 0)
359 if (b->enable == temporary)
360 b->enable = disabled;
361 breakpoint_commands = b->commands;
362 if (breakpoint_commands
363 && !strcmp ("silent", breakpoint_commands->line))
365 breakpoint_commands = breakpoint_commands->next;
366 return 0x1000000 + b->number;
380 register struct breakpoint *b;
381 register struct command_line *l;
382 register struct symbol *sym;
383 CORE_ADDR last_addr = -1;
386 if (bnum == -1 || bnum == b->number)
388 printf ("#%-3d %c 0x%08x ", b->number,
389 "nyod"[(int) b->enable],
391 last_addr = b->address;
394 sym = find_pc_function (b->address);
396 printf (" in %s (%s line %d)", SYMBOL_NAME (sym),
397 b->symtab->filename, b->line_number);
399 printf ("%s line %d", b->symtab->filename, b->line_number);
404 printf ("\tignore next %d hits\n", b->ignore_count);
406 printf ("\tstop only in stack frame at 0x%x\n", b->frame);
409 printf ("\tbreak only if ");
410 print_expression (b->cond, stdout);
416 printf ("\t%s\n", l->line);
422 set_next_address (last_addr);
426 breakpoints_info (bnum_exp)
432 bnum = parse_and_eval_address (bnum_exp);
433 else if (breakpoint_chain == 0)
434 printf ("No breakpoints.\n");
436 printf ("Breakpoints:\n\
437 Num Enb Address Where\n");
442 /* Print a message describing any breakpoints set at PC. */
445 describe_other_breakpoints (pc)
446 register CORE_ADDR pc;
448 register int others = 0;
449 register struct breakpoint *b;
452 if (b->address == pc)
456 printf ("Note: breakpoint%s ", (others > 1) ? "s" : "");
458 if (b->address == pc)
463 (b->enable == disabled) ? " (disabled)" : "",
464 (others > 1) ? "," : ((others == 1) ? " and" : ""));
466 printf (" also set at pc 0x%x\n", pc);
470 /* Set the default place to put a breakpoint
471 for the `break' command with no arguments. */
474 set_default_breakpoint (valid, addr, symtab, line)
477 struct symtab *symtab;
480 default_breakpoint_valid = valid;
481 default_breakpoint_address = addr;
482 default_breakpoint_symtab = symtab;
483 default_breakpoint_line = line;
486 /* Rescan breakpoints at address ADDRESS,
487 marking the first one as "first" and any others as "duplicates".
488 This is so that the bpt instruction is only inserted once. */
491 check_duplicates (address)
494 register struct breakpoint *b;
495 register int count = 0;
498 if (b->enable != disabled && b->address == address)
501 b->duplicate = count > 1;
505 /* Low level routine to set a breakpoint.
506 Takes as args the three things that every breakpoint must have.
507 Returns the breakpoint object so caller can set other things.
508 Does not set the breakpoint number!
509 Does not print anything. */
511 static struct breakpoint *
512 set_raw_breakpoint (sal)
513 struct symtab_and_line sal;
515 register struct breakpoint *b, *b1;
517 b = (struct breakpoint *) xmalloc (sizeof (struct breakpoint));
518 bzero (b, sizeof *b);
520 b->symtab = sal.symtab;
521 b->line_number = sal.line;
525 /* Add this breakpoint to the end of the chain
526 so that a list of breakpoints will come out in order
527 of increasing numbers. */
529 b1 = breakpoint_chain;
531 breakpoint_chain = b;
539 check_duplicates (sal.pc);
544 /* Set a breakpoint that will evaporate an end of command
545 at address specified by SAL.
546 Restrict it to frame FRAME if FRAME is nonzero. */
549 set_momentary_breakpoint (sal, frame)
550 struct symtab_and_line sal;
553 register struct breakpoint *b;
554 b = set_raw_breakpoint (sal);
561 clear_momentary_breakpoints ()
563 register struct breakpoint *b;
567 delete_breakpoint (b);
572 /* Set a breakpoint from a symtab and line.
573 If TEMPFLAG is nonzero, it is a temporary breakpoint.
574 Print the same confirmation messages that the breakpoint command prints. */
577 set_breakpoint (s, line, tempflag)
582 register struct breakpoint *b;
583 struct symtab_and_line sal;
587 sal.pc = find_line_pc (sal.symtab, sal.line);
589 error ("No line %d in file \"%s\".\n", sal.line, sal.symtab->filename);
592 describe_other_breakpoints (sal.pc);
594 b = set_raw_breakpoint (sal);
595 b->number = ++breakpoint_count;
598 b->enable = temporary;
600 printf ("Breakpoint %d at 0x%x", b->number, b->address);
602 printf (": file %s, line %d.", b->symtab->filename, b->line_number);
607 /* Set a breakpoint according to ARG (function, linenum or *address)
608 and make it temporary if TEMPFLAG is nonzero. */
611 break_command_1 (arg, tempflag, from_tty)
613 int tempflag, from_tty;
615 struct symtab_and_line sal;
616 register struct expression *cond = 0;
617 register struct breakpoint *b;
623 sal = decode_line_1 (&arg, 1, 0, 0);
625 if (sal.pc == 0 && sal.symtab != 0)
627 sal.pc = find_line_pc (sal.symtab, sal.line);
629 error ("No line %d in file \"%s\".",
630 sal.line, sal.symtab->filename);
635 if (arg[0] == 'i' && arg[1] == 'f'
636 && (arg[2] == ' ' || arg[2] == '\t'))
637 cond = (struct expression *) parse_c_1 ((arg += 2, &arg),
638 block_for_pc (sal.pc));
640 error ("Junk at end of arguments.");
643 else if (default_breakpoint_valid)
645 sal.pc = default_breakpoint_address;
646 sal.line = default_breakpoint_line;
647 sal.symtab = default_breakpoint_symtab;
650 error ("No default breakpoint address now.");
653 describe_other_breakpoints (sal.pc);
655 b = set_raw_breakpoint (sal);
656 b->number = ++breakpoint_count;
659 b->enable = temporary;
661 printf ("Breakpoint %d at 0x%x", b->number, b->address);
663 printf (": file %s, line %d.", b->symtab->filename, b->line_number);
668 break_command (arg, from_tty)
672 break_command_1 (arg, 0, from_tty);
676 tbreak_command (arg, from_tty)
680 break_command_1 (arg, 1, from_tty);
684 clear_command (arg, from_tty)
688 register struct breakpoint *b, *b1;
689 struct symtab_and_line sal;
690 register struct breakpoint *found;
693 sal = decode_line_spec (arg, 1);
696 sal.line = default_breakpoint_line;
697 sal.symtab = default_breakpoint_symtab;
700 error ("No source file specified.");
703 /* If exact pc given, clear bpts at that pc.
704 But if sal.pc is zero, clear all bpts on specified line. */
706 found = (struct breakpoint *) 0;
707 while (breakpoint_chain
708 && (sal.pc ? breakpoint_chain->address == sal.pc
709 : (breakpoint_chain->symtab == sal.symtab
710 && breakpoint_chain->line_number == sal.line)))
712 b1 = breakpoint_chain;
713 breakpoint_chain = b1->next;
720 && (sal.pc ? b->next->address == sal.pc
721 : (b->next->symtab == sal.symtab
722 && b->next->line_number == sal.line)))
731 error ("No breakpoint at %s.", arg);
733 if (found->next) from_tty = 1; /* Alwats report if deleted more than one */
734 if (from_tty) printf ("Deleted breakpoint%s ", found->next ? "s" : "");
737 if (from_tty) printf ("%d ", found->number);
739 delete_breakpoint (found);
742 if (from_tty) putchar ('\n');
745 /* Delete breakpoint number BNUM if it is a `delete' breakpoint.
746 This is called after breakpoint BNUM has been hit.
747 Also delete any breakpoint numbered -3 unless there are breakpoint
748 commands to be executed. */
751 breakpoint_auto_delete (bnum)
754 register struct breakpoint *b;
757 if (b->number == bnum)
759 if (b->enable == delete)
760 delete_breakpoint (b);
763 if (breakpoint_commands == 0)
764 clear_momentary_breakpoints ();
768 delete_breakpoint (bpt)
769 struct breakpoint *bpt;
771 register struct breakpoint *b;
774 write_memory (bpt->address, bpt->shadow_contents, sizeof break_insn);
776 if (breakpoint_chain == bpt)
777 breakpoint_chain = bpt->next;
786 check_duplicates (bpt->address);
788 free_command_lines (&bpt->commands);
794 void map_breakpoint_numbers ();
797 delete_command (arg, from_tty)
801 register struct breakpoint *b, *b1;
805 if (!from_tty || query ("Delete all breakpoints? "))
807 /* No arg; clear all breakpoints. */
808 while (breakpoint_chain)
809 delete_breakpoint (breakpoint_chain);
813 map_breakpoint_numbers (arg, delete_breakpoint);
816 /* Delete all breakpoints.
817 Done when new symtabs are loaded, since the break condition expressions
818 may become invalid, and the breakpoints are probably wrong anyway. */
823 delete_command (0, 0);
826 /* Set ignore-count of breakpoint number BPTNUM to COUNT.
827 If from_tty is nonzero, it prints a message to that effect,
828 which ends with a period (no newline). */
831 set_ignore_count (bptnum, count, from_tty)
832 int bptnum, count, from_tty;
834 register struct breakpoint *b;
840 if (b->number == bptnum)
842 b->ignore_count = count;
846 printf ("Will stop next time breakpoint %d is reached.", bptnum);
848 printf ("Will ignore next crossing of breakpoint %d.", bptnum);
850 printf ("Will ignore next %d crossings of breakpoint %d.",
855 error ("No breakpoint number %d.", bptnum);
858 /* Command to set ignore-count of breakpoint N to COUNT. */
861 ignore_command (args, from_tty)
869 error_no_arg ("a breakpoint number");
872 while (*p >= '0' && *p <= '9') p++;
873 if (*p && *p != ' ' && *p != '\t')
874 error ("First argument must be a breakpoint number.");
879 error ("Second argument (specified ignore-count) is missing.");
881 set_ignore_count (num, parse_and_eval_address (p), from_tty);
885 /* Call FUNCTION on each of the breakpoints
886 whose numbers are given in ARGS. */
889 map_breakpoint_numbers (args, function)
893 register char *p = args;
896 register struct breakpoint *b;
899 error_no_arg ("one or more breakpoint numbers");
904 while (*p1 >= '0' && *p1 <= '9') p1++;
905 if (*p1 && *p1 != ' ' && *p1 != '\t')
906 error ("Arguments must be breakpoint numbers.");
911 if (b->number == num)
916 printf ("No breakpoint number %d.\n", num);
919 while (*p == ' ' || *p == '\t') p++;
924 enable_breakpoint (bpt)
925 struct breakpoint *bpt;
927 bpt->enable = enabled;
929 check_duplicates (bpt->address);
933 enable_command (args)
936 map_breakpoint_numbers (args, enable_breakpoint);
940 disable_breakpoint (bpt)
941 struct breakpoint *bpt;
943 bpt->enable = disabled;
945 check_duplicates (bpt->address);
949 disable_command (args)
952 register struct breakpoint *bpt;
954 ALL_BREAKPOINTS (bpt)
955 disable_breakpoint (bpt);
957 map_breakpoint_numbers (args, disable_breakpoint);
961 enable_once_breakpoint (bpt)
962 struct breakpoint *bpt;
964 bpt->enable = temporary;
966 check_duplicates (bpt->address);
970 enable_once_command (args)
973 map_breakpoint_numbers (args, enable_once_breakpoint);
977 enable_delete_breakpoint (bpt)
978 struct breakpoint *bpt;
980 bpt->enable = delete;
982 check_duplicates (bpt->address);
986 enable_delete_command (args)
989 map_breakpoint_numbers (args, enable_delete_breakpoint);
993 /* Chain containing all defined enable commands. */
995 struct cmd_list_element *enablelist;
997 extern struct cmd_list_element *cmdlist;
1002 breakpoint_chain = 0;
1003 breakpoint_count = 0;
1006 add_com ("ignore", class_breakpoint, ignore_command,
1007 "Set ignore-count of breakpoint number N to COUNT.");
1009 add_com ("commands", class_breakpoint, commands_command,
1010 "Set commands to be executed when a breakpoint is hit.\n\
1011 Give breakpoint number as argument after \"commands\".\n\
1012 The commands themselves follow starting on the next line.\n\
1013 Type a line containing \"end\" to indicate the end of them.\n\
1014 Give \"silent\" as the first line to make the breakpoint silent;\n\
1015 then no output is printed when it is hit, except what the commands print.");
1017 add_com ("condition", class_breakpoint, condition_command,
1018 "Specify breakpoint number N to break only if COND is true.\n\
1019 N is an integer; COND is a C expression to be evaluated whenever\n\
1020 breakpoint N is reached. Actually break only when COND is nonzero.");
1022 add_com ("tbreak", class_breakpoint, tbreak_command,
1023 "Set a temporary breakpoint. Args like \"break\" command.\n\
1024 Like \"break\" except the breakpoint is only enabled temporarily,\n\
1025 so it will be disabled when hit. Equivalent to \"break\" followed\n\
1026 by using \"enable once\" on the breakpoint number.");
1028 add_prefix_cmd ("enable", class_breakpoint, enable_command,
1029 "Enable some breakpoints. Give breakpoint numbers as arguments.\n\
1030 With no subcommand, breakpoints are enabled until you command otherwise.\n\
1031 This is used to cancel the effect of the \"disable\" command.\n\
1032 With a subcommand you can enable temporarily.",
1033 &enablelist, "enable ", 1, &cmdlist);
1035 add_cmd ("delete", 0, enable_delete_command,
1036 "Enable breakpoints and delete when hit. Give breakpoint numbers.\n\
1037 If a breakpoint is hit while enabled in this fashion, it is deleted.",
1040 add_cmd ("once", 0, enable_once_command,
1041 "Enable breakpoints for one hit. Give breakpoint numbers.\n\
1042 If a breakpoint is hit while enabled in this fashion, it becomes disabled.\n\
1043 See the \"tbreak\" command which sets a breakpoint and enables it once.",
1046 add_com ("disable", class_breakpoint, disable_command,
1047 "Disable some breakpoints. Give breakpoint numbers as arguments.\n\
1048 With no arguments, disable all breakpoints.\n\
1049 A disabled breakpoint is not forgotten,\n\
1050 but it has no effect until enabled again.");
1051 add_com_alias ("dis", "disable", class_breakpoint, 1);
1053 add_com ("delete", class_breakpoint, delete_command,
1054 "Delete breakpoints, specifying breakpoint numbers; or all breakpoints.\n\
1055 Arguments are breakpoint numbers with spaces in between.\n\
1056 To delete all breakpoints, give no argument.");
1057 add_com_alias ("d", "delete", class_breakpoint, 1);
1059 add_com ("clear", class_breakpoint, clear_command,
1060 "Clear breakpoint at specified line or function.\n\
1061 Argument may be line number, function name, or \"*\" and an address.\n\
1062 If line number is specified, all breakpoints in that line are cleared.\n\
1063 If function is specified, breakpoints at beginning of function are cleared.\n\
1064 If an address is specified, breakpoints at that address are cleared.\n\n\
1065 With no argument, clears all breakpoints in the line that the selected frame\n\
1068 See also the \"delete\" command which clears breakpoints by number.");
1070 add_com ("break", class_breakpoint, break_command,
1071 "Set breakpoint at specified line or function.\n\
1072 Argument may be line number, function name, or \"*\" and an address.\n\
1073 If line number is specified, break at start of code for that line.\n\
1074 If function is specified, break at start of code for that function.\n\
1075 If an address is specified, break at that exact address.\n\
1076 With no arg, uses current execution address of selected stack frame.\n\
1077 This is useful for breaking on return to a stack frame.\n\
1079 Multiple breakpoints at one place are permitted, and useful if conditional.\n\
1081 Do \"help breakpoints\" for info on other commands dealing with breakpoints.");
1082 add_com_alias ("b", "break", class_run, 1);
1083 add_com_alias ("br", "break", class_run, 1);
1084 add_com_alias ("bre", "break", class_run, 1);
1085 add_com_alias ("brea", "break", class_run, 1);
1087 add_info ("breakpoints", breakpoints_info,
1088 "Status of all breakpoints, or breakpoint number NUMBER.\n\
1089 Second column is \"y\" for enabled breakpoint, \"n\" for disabled,\n\
1090 \"o\" for enabled once (disable when hit), \"d\" for enable but delete when hit.\n\
1091 Then come the address and the file/line number.\n\n\
1092 Convenience variable \"$_\" and default examine address for \"x\"\n\
1093 are set to the address of the last breakpoint listed.");