xref: /386bsd/usr/src/usr.bin/gdb/breakpoint.c (revision a2142627)
1 /*-
2  * This code is derived from software copyrighted by the Free Software
3  * Foundation.
4  *
5  * Modified 1991 by Donn Seeley at UUNET Technologies, Inc.
6  * Modified 1990 by Van Jacobson at Lawrence Berkeley Laboratory.
7  */
8 
9 #ifndef lint
10 static char sccsid[] = "@(#)breakpoint.c	6.3 (Berkeley) 5/8/91";
11 #endif /* not lint */
12 
13 /* Everything about breakpoints, for GDB.
14    Copyright (C) 1986, 1987, 1989 Free Software Foundation, Inc.
15 
16 This file is part of GDB.
17 
18 GDB is free software; you can redistribute it and/or modify
19 it under the terms of the GNU General Public License as published by
20 the Free Software Foundation; either version 1, or (at your option)
21 any later version.
22 
23 GDB is distributed in the hope that it will be useful,
24 but WITHOUT ANY WARRANTY; without even the implied warranty of
25 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
26 GNU General Public License for more details.
27 
28 You should have received a copy of the GNU General Public License
29 along with GDB; see the file COPYING.  If not, write to
30 the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
31 
32 #include <stdio.h>
33 #include "defs.h"
34 #include "param.h"
35 #include "symtab.h"
36 #include "frame.h"
37 
38 /* This is the sequence of bytes we insert for a breakpoint.  */
39 
40 static char break_insn[] = BREAKPOINT;
41 
42 /* States of enablement of breakpoint.
43    `temporary' means disable when hit.
44    `delete' means delete when hit.  */
45 
46 enum enable { disabled, enabled, temporary, delete};
47 
48 /* Not that the ->silent field is not currently used by any commands
49    (though the code is in there if it was to be and set_raw_breakpoint
50    does set it to 0).  I implemented it because I thought it would be
51    useful for a hack I had to put in; I'm going to leave it in because
52    I can see how there might be times when it would indeed be useful */
53 
54 struct breakpoint
55 {
56   struct breakpoint *next;
57   /* Number assigned to distinguish breakpoints.  */
58   int number;
59   /* Address to break at.  */
60   CORE_ADDR address;
61   /* Line number of this address.  Redundant.  */
62   int line_number;
63   /* Symtab of file of this address.  Redundant.  */
64   struct symtab *symtab;
65   /* Zero means disabled; remember the info but don't break here.  */
66   enum enable enable;
67   /* Non-zero means a silent breakpoint (don't print frame info
68      if we stop here). */
69   unsigned char silent;
70   /* Number of stops at this breakpoint that should
71      be continued automatically before really stopping.  */
72   int ignore_count;
73   /* "Real" contents of byte where breakpoint has been inserted.
74      Valid only when breakpoints are in the program.  */
75   char shadow_contents[sizeof break_insn];
76   /* Nonzero if this breakpoint is now inserted.  */
77   char inserted;
78   /* Nonzero if this is not the first breakpoint in the list
79      for the given address.  */
80   char duplicate;
81   /* Chain of command lines to execute when this breakpoint is hit.  */
82   struct command_line *commands;
83   /* Stack depth (address of frame).  If nonzero, break only if fp
84      equals this.  */
85   FRAME_ADDR frame;
86   /* Conditional.  Break only if this expression's value is nonzero.  */
87   struct expression *cond;
88 };
89 
90 #define ALL_BREAKPOINTS(b)  for (b = breakpoint_chain; b; b = b->next)
91 
92 /* Chain of all breakpoints defined.  */
93 
94 struct breakpoint *breakpoint_chain;
95 
96 /* Number of last breakpoint made.  */
97 
98 static int breakpoint_count;
99 
100 /* Default address, symtab and line to put a breakpoint at
101    for "break" command with no arg.
102    if default_breakpoint_valid is zero, the other three are
103    not valid, and "break" with no arg is an error.
104 
105    This set by print_stack_frame, which calls set_default_breakpoint.  */
106 
107 int default_breakpoint_valid;
108 CORE_ADDR default_breakpoint_address;
109 struct symtab *default_breakpoint_symtab;
110 int default_breakpoint_line;
111 
112 /* Remaining commands (not yet executed)
113    of last breakpoint hit.  */
114 
115 struct command_line *breakpoint_commands;
116 
117 static void delete_breakpoint ();
118 void clear_momentary_breakpoints ();
119 void breakpoint_auto_delete ();
120 
121 /* Flag indicating extra verbosity for xgdb.  */
122 extern int xgdb_verbose;
123 
124 /* condition N EXP -- set break condition of breakpoint N to EXP.  */
125 
126 static void
condition_command(arg,from_tty)127 condition_command (arg, from_tty)
128      char *arg;
129      int from_tty;
130 {
131   register struct breakpoint *b;
132   register char *p;
133   register int bnum;
134   register struct expression *expr;
135 
136   if (arg == 0)
137     error_no_arg ("breakpoint number");
138 
139   p = arg;
140   while (*p >= '0' && *p <= '9') p++;
141   if (p == arg)
142     /* There is no number here.  (e.g. "cond a == b").  */
143     error_no_arg ("breakpoint number");
144   bnum = atoi (arg);
145 
146   ALL_BREAKPOINTS (b)
147     if (b->number == bnum)
148       {
149 	if (b->cond)
150 	  {
151 	    free (b->cond);
152 	    b->cond = 0;  /* parse_c_1 can leave this unchanged. */
153 	  }
154 	if (*p == 0)
155 	  {
156 	    b->cond = 0;
157 	    if (from_tty)
158 	      printf ("Breakpoint %d now unconditional.\n", bnum);
159 	  }
160 	else
161 	  {
162 	    if (*p != ' ' && *p != '\t')
163 	      error ("Arguments must be an integer (breakpoint number) and an expression.");
164 
165 	    /* Find start of expression */
166 	    while (*p == ' ' || *p == '\t') p++;
167 
168 	    arg = p;
169 	    b->cond = (struct expression *) parse_c_1 (&arg, block_for_pc (b->address), 0);
170 	    if (*arg)
171 	      error ("Junk at end of expression");
172 	  }
173 	return;
174       }
175 
176   error ("No breakpoint number %d.", bnum);
177 }
178 
179 static void
commands_command(arg,from_tty)180 commands_command (arg, from_tty)
181      char *arg;
182      int from_tty;
183 {
184   register struct breakpoint *b;
185   register char *p, *p1;
186   register int bnum;
187   struct command_line *l;
188 
189   /* If we allowed this, we would have problems with when to
190      free the storage, if we change the commands currently
191      being read from.  */
192 
193   if (breakpoint_commands)
194     error ("Can't use the \"commands\" command among a breakpoint's commands.");
195 
196   /* Allow commands by itself to refer to the last breakpoint.  */
197   if (arg == 0)
198     bnum = breakpoint_count;
199   else
200     {
201       p = arg;
202       if (! (*p >= '0' && *p <= '9'))
203 	error ("Argument must be integer (a breakpoint number).");
204 
205       while (*p >= '0' && *p <= '9') p++;
206       if (*p)
207 	error ("Unexpected extra arguments following breakpoint number.");
208 
209       bnum = atoi (arg);
210     }
211 
212   ALL_BREAKPOINTS (b)
213     if (b->number == bnum)
214       {
215 	if (from_tty && input_from_terminal_p ())
216 	  {
217 	    printf ("Type commands for when breakpoint %d is hit, one per line.\n\
218 End with a line saying just \"end\".\n", bnum);
219 	    fflush (stdout);
220 	  }
221 	l = read_command_lines (from_tty);
222 	free_command_lines (b->commands);
223 	b->commands = l;
224 	return;
225       }
226   error ("No breakpoint number %d.", bnum);
227 }
228 
229 /* Called from command loop to execute the commands
230    associated with the breakpoint we just stopped at.  */
231 
232 void
do_breakpoint_commands()233 do_breakpoint_commands ()
234 {
235   struct command_line *cmd;
236 
237   while (cmd = breakpoint_commands)
238     {
239       breakpoint_commands = 0;
240       execute_command_lines(cmd);
241       /* If command was "cont", breakpoint_commands is now 0,
242 	 of if we stopped at yet another breakpoint which has commands,
243 	 it is now the commands for the new breakpoint.  */
244     }
245   clear_momentary_breakpoints ();
246 }
247 
248 /* Used when the program is proceeded, to eliminate any remaining
249    commands attached to the previous breakpoint we stopped at.  */
250 
251 void
clear_breakpoint_commands()252 clear_breakpoint_commands ()
253 {
254   breakpoint_commands = 0;
255   breakpoint_auto_delete (0);
256 }
257 
258 /* Functions to get and set the current list of pending
259    breakpoint commands.  These are used by run_stack_dummy
260    to preserve the commands around a function call.  */
261 
262 struct command_line *
get_breakpoint_commands()263 get_breakpoint_commands ()
264 {
265   return breakpoint_commands;
266 }
267 
268 void
set_breakpoint_commands(cmds)269 set_breakpoint_commands (cmds)
270      struct command_line *cmds;
271 {
272   breakpoint_commands = cmds;
273 }
274 
275 /* insert_breakpoints is used when starting or continuing the program.
276    remove_breakpoints is used when the program stops.
277    Both return zero if successful,
278    or an `errno' value if could not write the inferior.  */
279 
280 int
insert_breakpoints()281 insert_breakpoints ()
282 {
283   register struct breakpoint *b;
284   int val;
285 
286 #ifdef BREAKPOINT_DEBUG
287   printf ("Inserting breakpoints.\n");
288 #endif /* BREAKPOINT_DEBUG */
289 
290   ALL_BREAKPOINTS (b)
291     if (b->enable != disabled && ! b->inserted && ! b->duplicate)
292       {
293 	read_memory (b->address, b->shadow_contents, sizeof break_insn);
294 	val = write_memory (b->address, break_insn, sizeof break_insn);
295 	if (val)
296 	  return val;
297 #ifdef BREAKPOINT_DEBUG
298 	printf ("Inserted breakpoint at 0x%x, shadow 0x%x, 0x%x.\n",
299 		b->address, b->shadow_contents[0], b->shadow_contents[1]);
300 #endif /* BREAKPOINT_DEBUG */
301 	b->inserted = 1;
302       }
303   return 0;
304 }
305 
306 int
remove_breakpoints()307 remove_breakpoints ()
308 {
309   register struct breakpoint *b;
310   int val;
311 
312 #ifdef BREAKPOINT_DEBUG
313   printf ("Removing breakpoints.\n");
314 #endif /* BREAKPOINT_DEBUG */
315 
316   ALL_BREAKPOINTS (b)
317     if (b->inserted)
318       {
319 	val = write_memory (b->address, b->shadow_contents, sizeof break_insn);
320 	if (val)
321 	  return val;
322 	b->inserted = 0;
323 #ifdef BREAKPOINT_DEBUG
324 	printf ("Removed breakpoint at 0x%x, shadow 0x%x, 0x%x.\n",
325 		b->address, b->shadow_contents[0], b->shadow_contents[1]);
326 #endif /* BREAKPOINT_DEBUG */
327       }
328 
329   return 0;
330 }
331 
332 /* Clear the "inserted" flag in all breakpoints.
333    This is done when the inferior is loaded.  */
334 
335 void
mark_breakpoints_out()336 mark_breakpoints_out ()
337 {
338   register struct breakpoint *b;
339 
340   ALL_BREAKPOINTS (b)
341     b->inserted = 0;
342 }
343 
344 /* breakpoint_here_p (PC) returns 1 if an enabled breakpoint exists at PC.
345    When continuing from a location with a breakpoint,
346    we actually single step once before calling insert_breakpoints.  */
347 
348 int
breakpoint_here_p(pc)349 breakpoint_here_p (pc)
350      CORE_ADDR pc;
351 {
352   register struct breakpoint *b;
353 
354   ALL_BREAKPOINTS (b)
355     if (b->enable != disabled && b->address == pc)
356       return 1;
357 
358   return 0;
359 }
360 
361 /* Evaluate the expression EXP and return 1 if value is zero.
362    This is used inside a catch_errors to evaluate the breakpoint condition.  */
363 
364 int
breakpoint_cond_eval(exp)365 breakpoint_cond_eval (exp)
366      struct expression *exp;
367 {
368   return value_zerop (evaluate_expression (exp));
369 }
370 
371 /* Return 0 if PC is not the address just after a breakpoint,
372    or -1 if breakpoint says do not stop now,
373    or -2 if breakpoint says it has deleted itself and don't stop,
374    or -3 if hit a breakpoint number -3 (delete when program stops),
375    or else the number of the breakpoint,
376    with 0x1000000 added (or subtracted, for a negative return value) for
377    a silent breakpoint.  */
378 
379 int
breakpoint_stop_status(pc,frame_address)380 breakpoint_stop_status (pc, frame_address)
381      CORE_ADDR pc;
382      FRAME_ADDR frame_address;
383 {
384   register struct breakpoint *b;
385   register int cont = 0;
386 
387   /* Get the address where the breakpoint would have been.  */
388   pc -= DECR_PC_AFTER_BREAK;
389 
390   ALL_BREAKPOINTS (b)
391     if (b->enable != disabled && b->address == pc)
392       {
393 	if (b->frame && b->frame != frame_address)
394 	  cont = -1;
395 	else
396 	  {
397 	    int value_zero;
398 	    if (b->cond)
399 	      {
400 		/* Need to select the frame, with all that implies
401 		   so that the conditions will have the right context.  */
402 		select_frame (get_current_frame (), 0);
403 		value_zero
404 		  = catch_errors (breakpoint_cond_eval, b->cond,
405 				  "Error occurred in testing breakpoint condition.");
406 		free_all_values ();
407 	      }
408 	    if (b->cond && value_zero)
409 	      {
410 		cont = -1;
411 	      }
412 	    else if (b->ignore_count > 0)
413 	      {
414 		b->ignore_count--;
415 		cont = -1;
416 	      }
417 	    else
418 	      {
419 		if (b->enable == temporary)
420 		  b->enable = disabled;
421 		breakpoint_commands = b->commands;
422 		if (b->silent
423 		    || (breakpoint_commands
424 			&& !strcmp ("silent", breakpoint_commands->line)))
425 		  {
426 		    if (breakpoint_commands)
427 		      breakpoint_commands = breakpoint_commands->next;
428 		    return (b->number > 0 ?
429 			    0x1000000 + b->number :
430 			    b->number - 0x1000000);
431 		  }
432 		return b->number;
433 	      }
434 	  }
435       }
436 
437   return cont;
438 }
439 
440 static void
breakpoint_1(bnum)441 breakpoint_1 (bnum)
442      int bnum;
443 {
444   register struct breakpoint *b;
445   register struct command_line *l;
446   register struct symbol *sym;
447   CORE_ADDR last_addr = (CORE_ADDR)-1;
448 
449   ALL_BREAKPOINTS (b)
450     if (bnum == -1 || bnum == b->number)
451       {
452 	printf_filtered ("#%-3d %c  0x%08x", b->number,
453 		"nyod"[(int) b->enable],
454 		b->address);
455 	last_addr = b->address;
456 	if (b->symtab)
457 	  {
458 	    sym = find_pc_function (b->address);
459 	    if (sym)
460 	      {
461 		fputs_filtered (" in ", stdout);
462 		fputs_demangled (SYMBOL_NAME (sym), stdout, 1);
463 		fputs_filtered (" (", stdout);
464 	      }
465 	    fputs_filtered (b->symtab->filename, stdout);
466 	    printf_filtered (" line %d", b->line_number);
467 	    if (sym) fputs_filtered(")", stdout);
468 	  }
469 	else
470 	      print_address_symbolic (b->address, stdout);
471 
472 	printf_filtered ("\n");
473 
474 	if (b->ignore_count)
475 	  printf_filtered ("\tignore next %d hits\n", b->ignore_count);
476 	if (b->frame)
477 	  printf_filtered ("\tstop only in stack frame at 0x%x\n", b->frame);
478 	if (b->cond)
479 	  {
480 	    printf_filtered ("\tbreak only if ");
481 	    print_expression (b->cond, stdout);
482 	    printf_filtered ("\n");
483 	  }
484 	if (l = b->commands)
485 	  while (l)
486 	    {
487 	      printf_filtered ("\t%s\n", l->line);
488 	      l = l->next;
489 	    }
490       }
491 
492   /* Compare against (CORE_ADDR)-1 in case some compiler decides
493      that a comparison of an unsigned with -1 is always false.  */
494   if (last_addr != (CORE_ADDR)-1)
495     set_next_address (last_addr);
496 }
497 
498 static void
breakpoints_info(bnum_exp)499 breakpoints_info (bnum_exp)
500      char *bnum_exp;
501 {
502   int bnum = -1;
503 
504   if (bnum_exp)
505     bnum = parse_and_eval_address (bnum_exp);
506   else if (breakpoint_chain == 0)
507     printf_filtered ("No breakpoints.\n");
508   else
509     printf_filtered ("Breakpoints:\n\
510 Num Enb   Address    Where\n");
511 
512   breakpoint_1 (bnum);
513 }
514 
515 /* Print a message describing any breakpoints set at PC.  */
516 
517 static void
describe_other_breakpoints(pc)518 describe_other_breakpoints (pc)
519      register CORE_ADDR pc;
520 {
521   register int others = 0;
522   register struct breakpoint *b;
523 
524   ALL_BREAKPOINTS (b)
525     if (b->address == pc)
526       others++;
527   if (others > 0)
528     {
529       printf ("Note: breakpoint%s ", (others > 1) ? "s" : "");
530       ALL_BREAKPOINTS (b)
531 	if (b->address == pc)
532 	  {
533 	    others--;
534 	    printf ("%d%s%s ",
535 		    b->number,
536 		    (b->enable == disabled) ? " (disabled)" : "",
537 		    (others > 1) ? "," : ((others == 1) ? " and" : ""));
538 	  }
539       printf ("also set at pc 0x%x.\n", pc);
540     }
541 }
542 
543 /* Set the default place to put a breakpoint
544    for the `break' command with no arguments.  */
545 
546 void
set_default_breakpoint(valid,addr,symtab,line)547 set_default_breakpoint (valid, addr, symtab, line)
548      int valid;
549      CORE_ADDR addr;
550      struct symtab *symtab;
551      int line;
552 {
553   default_breakpoint_valid = valid;
554   default_breakpoint_address = addr;
555   default_breakpoint_symtab = symtab;
556   default_breakpoint_line = line;
557 }
558 
559 /* Rescan breakpoints at address ADDRESS,
560    marking the first one as "first" and any others as "duplicates".
561    This is so that the bpt instruction is only inserted once.  */
562 
563 static void
check_duplicates(address)564 check_duplicates (address)
565      CORE_ADDR address;
566 {
567   register struct breakpoint *b;
568   register int count = 0;
569 
570   ALL_BREAKPOINTS (b)
571     if (b->enable != disabled && b->address == address)
572       {
573 	count++;
574 	b->duplicate = count > 1;
575       }
576 }
577 
578 /* Low level routine to set a breakpoint.
579    Takes as args the three things that every breakpoint must have.
580    Returns the breakpoint object so caller can set other things.
581    Does not set the breakpoint number!
582    Does not print anything.  */
583 
584 static struct breakpoint *
set_raw_breakpoint(sal)585 set_raw_breakpoint (sal)
586      struct symtab_and_line sal;
587 {
588   register struct breakpoint *b, *b1;
589 
590   b = (struct breakpoint *) xmalloc (sizeof (struct breakpoint));
591   bzero (b, sizeof *b);
592   b->address = sal.pc;
593   b->symtab = sal.symtab;
594   b->line_number = sal.line;
595   b->enable = enabled;
596   b->next = 0;
597   b->silent = 0;
598 
599   /* Add this breakpoint to the end of the chain
600      so that a list of breakpoints will come out in order
601      of increasing numbers.  */
602 
603   b1 = breakpoint_chain;
604   if (b1 == 0)
605     breakpoint_chain = b;
606   else
607     {
608       while (b1->next)
609 	b1 = b1->next;
610       b1->next = b;
611     }
612 
613   check_duplicates (sal.pc);
614 
615   return b;
616 }
617 
618 /* Set a breakpoint that will evaporate an end of command
619    at address specified by SAL.
620    Restrict it to frame FRAME if FRAME is nonzero.  */
621 
622 void
set_momentary_breakpoint(sal,frame)623 set_momentary_breakpoint (sal, frame)
624      struct symtab_and_line sal;
625      FRAME frame;
626 {
627   register struct breakpoint *b;
628   b = set_raw_breakpoint (sal);
629   b->number = -3;
630   b->enable = delete;
631   b->frame = (frame ? FRAME_FP (frame) : 0);
632 }
633 
634 void
clear_momentary_breakpoints()635 clear_momentary_breakpoints ()
636 {
637   register struct breakpoint *b;
638   ALL_BREAKPOINTS (b)
639     if (b->number == -3)
640       {
641 	delete_breakpoint (b);
642 	break;
643       }
644 }
645 
646 /* Set a breakpoint from a symtab and line.
647    If TEMPFLAG is nonzero, it is a temporary breakpoint.
648    Print the same confirmation messages that the breakpoint command prints.  */
649 
650 void
set_breakpoint(s,line,tempflag)651 set_breakpoint (s, line, tempflag)
652      struct symtab *s;
653      int line;
654      int tempflag;
655 {
656   register struct breakpoint *b;
657   struct symtab_and_line sal;
658 
659   sal.symtab = s;
660   sal.line = line;
661   sal.pc = find_line_pc (sal.symtab, sal.line);
662   if (sal.pc == 0)
663     error ("No line %d in file \"%s\".\n", sal.line, sal.symtab->filename);
664   else
665     {
666       describe_other_breakpoints (sal.pc);
667 
668       b = set_raw_breakpoint (sal);
669       b->number = ++breakpoint_count;
670       b->cond = 0;
671       if (tempflag)
672 	b->enable = temporary;
673 
674       printf ("Breakpoint %d at 0x%x", b->number, b->address);
675       if (b->symtab)
676 	printf (": file %s, line %d.", b->symtab->filename, b->line_number);
677       printf ("\n");
678     }
679 }
680 
681 /* Set a breakpoint according to ARG (function, linenum or *address)
682    and make it temporary if TEMPFLAG is nonzero. */
683 
684 static void
break_command_1(arg,tempflag,from_tty)685 break_command_1 (arg, tempflag, from_tty)
686      char *arg;
687      int tempflag, from_tty;
688 {
689   struct symtabs_and_lines sals;
690   struct symtab_and_line sal;
691   register struct expression *cond = 0;
692   register struct breakpoint *b;
693   char *save_arg;
694   int i;
695   CORE_ADDR pc;
696 
697   sals.sals = NULL;
698   sals.nelts = 0;
699 
700   sal.line = sal.pc = sal.end = 0;
701   sal.symtab = 0;
702 
703   /* If no arg given, or if first arg is 'if ', use the default breakpoint. */
704 
705   if (!arg || (arg[0] == 'i' && arg[1] == 'f'
706 	       && (arg[2] == ' ' || arg[2] == '\t')))
707     {
708       if (default_breakpoint_valid)
709 	{
710 	  sals.sals = (struct symtab_and_line *)
711 	    malloc (sizeof (struct symtab_and_line));
712 	  sal.pc = default_breakpoint_address;
713 	  sal.line = default_breakpoint_line;
714 	  sal.symtab = default_breakpoint_symtab;
715 	  sals.sals[0] = sal;
716 	  sals.nelts = 1;
717 	}
718       else
719 	error ("No default breakpoint address now.");
720     }
721   else
722     /* Force almost all breakpoints to be in terms of the
723        current_source_symtab (which is decode_line_1's default).  This
724        should produce the results we want almost all of the time while
725        leaving default_breakpoint_* alone.  */
726     if (default_breakpoint_valid
727 	&& (!current_source_symtab
728 	    || (arg && (*arg == '+' || *arg == '-'))))
729       sals = decode_line_1 (&arg, 1, default_breakpoint_symtab,
730 			    default_breakpoint_line);
731     else
732       sals = decode_line_1 (&arg, 1, 0, 0);
733 
734   if (! sals.nelts)
735     return;
736 
737   save_arg = arg;
738   for (i = 0; i < sals.nelts; i++)
739     {
740       sal = sals.sals[i];
741       if (sal.pc == 0 && sal.symtab != 0)
742 	{
743 	  pc = find_line_pc (sal.symtab, sal.line);
744 	  if (pc == 0)
745 	    error ("No line %d in file \"%s\".",
746 		   sal.line, sal.symtab->filename);
747 	}
748       else
749 	pc = sal.pc;
750 
751       while (arg && *arg)
752 	{
753 	  if (arg[0] == 'i' && arg[1] == 'f'
754 	      && (arg[2] == ' ' || arg[2] == '\t'))
755 	    cond = (struct expression *) parse_c_1 ((arg += 2, &arg),
756 						    block_for_pc (pc), 0);
757 	  else
758 	    error ("Junk at end of arguments.");
759 	}
760       arg = save_arg;
761       sals.sals[i].pc = pc;
762     }
763 
764   for (i = 0; i < sals.nelts; i++)
765     {
766       sal = sals.sals[i];
767 
768       if (from_tty)
769 	describe_other_breakpoints (sal.pc);
770 
771       b = set_raw_breakpoint (sal);
772       b->number = ++breakpoint_count;
773       b->cond = cond;
774       if (tempflag)
775 	b->enable = temporary;
776 
777       printf ("Breakpoint %d at 0x%x", b->number, b->address);
778       if (b->symtab)
779 	printf (": file %s, line %d.", b->symtab->filename, b->line_number);
780       printf ("\n");
781     }
782 
783   if (sals.nelts > 1)
784     {
785       printf ("Multiple breakpoints were set.\n");
786       printf ("Use the \"delete\" command to delete unwanted breakpoints.\n");
787     }
788   free (sals.sals);
789 }
790 
791 static void
break_command(arg,from_tty)792 break_command (arg, from_tty)
793      char *arg;
794      int from_tty;
795 {
796   break_command_1 (arg, 0, from_tty);
797 }
798 
799 static void
tbreak_command(arg,from_tty)800 tbreak_command (arg, from_tty)
801      char *arg;
802      int from_tty;
803 {
804   break_command_1 (arg, 1, from_tty);
805 }
806 
807 /*
808  * Helper routine for the until_command routine in infcmd.c.  Here
809  * because it uses the mechanisms of breakpoints.
810  */
811 void
until_break_command(arg,from_tty)812 until_break_command (arg, from_tty)
813      char *arg;
814      int from_tty;
815 {
816   struct symtabs_and_lines sals;
817   struct symtab_and_line sal;
818   FRAME prev_frame = get_prev_frame (selected_frame);
819 
820   clear_proceed_status ();
821 
822   /* Set a breakpoint where the user wants it and at return from
823      this function */
824 
825   if (default_breakpoint_valid)
826     sals = decode_line_1 (&arg, 1, default_breakpoint_symtab,
827 			  default_breakpoint_line);
828   else
829     sals = decode_line_1 (&arg, 1, 0, 0);
830 
831   if (sals.nelts != 1)
832     error ("Couldn't get information on specified line.");
833 
834   sal = sals.sals[0];
835   free (sals.sals);		/* malloc'd, so freed */
836 
837   if (*arg)
838     error ("Junk at end of arguments.");
839 
840   if (sal.pc == 0 && sal.symtab != 0)
841     sal.pc = find_line_pc (sal.symtab, sal.line);
842 
843   if (sal.pc == 0)
844     error ("No line %d in file \"%s\".", sal.line, sal.symtab->filename);
845 
846   set_momentary_breakpoint (sal, selected_frame);
847 
848   /* Keep within the current frame */
849 
850   if (prev_frame)
851     {
852       struct frame_info *fi;
853 
854       fi = get_frame_info (prev_frame);
855       sal = find_pc_line (fi->pc, 0);
856       sal.pc = fi->pc;
857       set_momentary_breakpoint (sal, prev_frame);
858     }
859 
860   proceed (-1, -1, 0);
861 }
862 
863 static void
clear_command(arg,from_tty)864 clear_command (arg, from_tty)
865      char *arg;
866      int from_tty;
867 {
868   register struct breakpoint *b, *b1;
869   struct symtabs_and_lines sals;
870   struct symtab_and_line sal;
871   register struct breakpoint *found;
872   int i;
873 
874   if (arg)
875     {
876       sals = decode_line_spec (arg, 1);
877     }
878   else
879     {
880       sals.sals = (struct symtab_and_line *) malloc (sizeof (struct symtab_and_line));
881       sal.line = default_breakpoint_line;
882       sal.symtab = default_breakpoint_symtab;
883       sal.pc = 0;
884       if (sal.symtab == 0)
885 	error ("No source file specified.");
886 
887       sals.sals[0] = sal;
888       sals.nelts = 1;
889     }
890 
891   for (i = 0; i < sals.nelts; i++)
892     {
893       /* If exact pc given, clear bpts at that pc.
894 	 But if sal.pc is zero, clear all bpts on specified line.  */
895       sal = sals.sals[i];
896       found = (struct breakpoint *) 0;
897       while (breakpoint_chain
898 	     && (sal.pc ? breakpoint_chain->address == sal.pc
899 		 : (breakpoint_chain->symtab == sal.symtab
900 		    && breakpoint_chain->line_number == sal.line)))
901 	{
902 	  b1 = breakpoint_chain;
903 	  breakpoint_chain = b1->next;
904 	  b1->next = found;
905 	  found = b1;
906 	}
907 
908       ALL_BREAKPOINTS (b)
909 	while (b->next
910 	       && (sal.pc ? b->next->address == sal.pc
911 		   : (b->next->symtab == sal.symtab
912 		      && b->next->line_number == sal.line)))
913 	  {
914 	    b1 = b->next;
915 	    b->next = b1->next;
916 	    b1->next = found;
917 	    found = b1;
918 	  }
919 
920       if (found == 0)
921 	error ("No breakpoint at %s.", arg);
922 
923       if (found->next) from_tty = 1; /* Always report if deleted more than one */
924       if (from_tty) printf ("Deleted breakpoint%s ", found->next ? "s" : "");
925       while (found)
926 	{
927 	  if (from_tty) printf ("%d ", found->number);
928 	  b1 = found->next;
929 	  delete_breakpoint (found);
930 	  found = b1;
931 	}
932       if (from_tty) putchar ('\n');
933     }
934   free (sals.sals);
935 }
936 
937 /* Delete breakpoint number BNUM if it is a `delete' breakpoint.
938    This is called after breakpoint BNUM has been hit.
939    Also delete any breakpoint numbered -3 unless there are breakpoint
940    commands to be executed.  */
941 
942 void
breakpoint_auto_delete(bnum)943 breakpoint_auto_delete (bnum)
944      int bnum;
945 {
946   register struct breakpoint *b;
947   if (bnum != 0)
948     ALL_BREAKPOINTS (b)
949       if (b->number == bnum)
950 	{
951 	  if (b->enable == delete)
952 	    delete_breakpoint (b);
953 	  break;
954 	}
955   if (breakpoint_commands == 0)
956     clear_momentary_breakpoints ();
957 }
958 
959 static void
delete_breakpoint(bpt)960 delete_breakpoint (bpt)
961      struct breakpoint *bpt;
962 {
963   register struct breakpoint *b;
964 
965   if (bpt->inserted)
966     write_memory (bpt->address, bpt->shadow_contents, sizeof break_insn);
967 
968   if (breakpoint_chain == bpt)
969     breakpoint_chain = bpt->next;
970 
971   ALL_BREAKPOINTS (b)
972     if (b->next == bpt)
973       {
974 	b->next = bpt->next;
975 	break;
976       }
977 
978   check_duplicates (bpt->address);
979 
980   free_command_lines (bpt->commands);
981   if (bpt->cond)
982     free (bpt->cond);
983 
984   if (xgdb_verbose && bpt->number >=0)
985     printf ("breakpoint #%d deleted\n", bpt->number);
986 
987   free (bpt);
988 }
989 
990 static void map_breakpoint_numbers ();
991 
992 static void
delete_command(arg,from_tty)993 delete_command (arg, from_tty)
994      char *arg;
995      int from_tty;
996 {
997   register struct breakpoint *b, *b1;
998 
999   if (arg == 0)
1000     {
1001       /* Ask user only if there are some breakpoints to delete.  */
1002       if (!from_tty
1003 	  || breakpoint_chain && query ("Delete all breakpoints? "))
1004 	{
1005 	  /* No arg; clear all breakpoints.  */
1006 	  while (breakpoint_chain)
1007 	    delete_breakpoint (breakpoint_chain);
1008 	}
1009     }
1010   else
1011     map_breakpoint_numbers (arg, delete_breakpoint);
1012 }
1013 
1014 /* Delete all breakpoints.
1015    Done when new symtabs are loaded, since the break condition expressions
1016    may become invalid, and the breakpoints are probably wrong anyway.  */
1017 
1018 void
clear_breakpoints()1019 clear_breakpoints ()
1020 {
1021   delete_command (0, 0);
1022 }
1023 
1024 /* Set ignore-count of breakpoint number BPTNUM to COUNT.
1025    If from_tty is nonzero, it prints a message to that effect,
1026    which ends with a period (no newline).  */
1027 
1028 void
set_ignore_count(bptnum,count,from_tty)1029 set_ignore_count (bptnum, count, from_tty)
1030      int bptnum, count, from_tty;
1031 {
1032   register struct breakpoint *b;
1033 
1034   if (count < 0)
1035     count = 0;
1036 
1037   ALL_BREAKPOINTS (b)
1038     if (b->number == bptnum)
1039       {
1040 	b->ignore_count = count;
1041 	if (!from_tty)
1042 	  return;
1043 	else if (count == 0)
1044 	  printf ("Will stop next time breakpoint %d is reached.", bptnum);
1045 	else if (count == 1)
1046 	  printf ("Will ignore next crossing of breakpoint %d.", bptnum);
1047 	else
1048 	  printf ("Will ignore next %d crossings of breakpoint %d.",
1049 		  count, bptnum);
1050 	return;
1051       }
1052 
1053   error ("No breakpoint number %d.", bptnum);
1054 }
1055 
1056 /* Clear the ignore counts of all breakpoints.  */
1057 void
breakpoint_clear_ignore_counts()1058 breakpoint_clear_ignore_counts ()
1059 {
1060   struct breakpoint *b;
1061 
1062   ALL_BREAKPOINTS (b)
1063     b->ignore_count = 0;
1064 }
1065 
1066 /* Command to set ignore-count of breakpoint N to COUNT.  */
1067 
1068 static void
ignore_command(args,from_tty)1069 ignore_command (args, from_tty)
1070      char *args;
1071      int from_tty;
1072 {
1073   register char *p = args;
1074   register int num;
1075 
1076   if (p == 0)
1077     error_no_arg ("a breakpoint number");
1078 
1079   while (*p >= '0' && *p <= '9') p++;
1080   if (*p && *p != ' ' && *p != '\t')
1081     error ("First argument must be a breakpoint number.");
1082 
1083   num = atoi (args);
1084 
1085   if (*p == 0)
1086     error ("Second argument (specified ignore-count) is missing.");
1087 
1088   set_ignore_count (num, parse_and_eval_address (p), from_tty);
1089   printf ("\n");
1090 }
1091 
1092 /* Call FUNCTION on each of the breakpoints
1093    whose numbers are given in ARGS.  */
1094 
1095 static void
map_breakpoint_numbers(args,function)1096 map_breakpoint_numbers (args, function)
1097      char *args;
1098      void (*function) ();
1099 {
1100   register char *p = args;
1101   register char *p1;
1102   register int num;
1103   register struct breakpoint *b;
1104 
1105   if (p == 0)
1106     error_no_arg ("one or more breakpoint numbers");
1107 
1108   while (*p)
1109     {
1110       p1 = p;
1111       while (*p1 >= '0' && *p1 <= '9') p1++;
1112       if (*p1 && *p1 != ' ' && *p1 != '\t')
1113 	error ("Arguments must be breakpoint numbers.");
1114 
1115       num = atoi (p);
1116 
1117       ALL_BREAKPOINTS (b)
1118 	if (b->number == num)
1119 	  {
1120 	    function (b);
1121 	    goto win;
1122 	  }
1123       printf ("No breakpoint number %d.\n", num);
1124     win:
1125       p = p1;
1126       while (*p == ' ' || *p == '\t') p++;
1127     }
1128 }
1129 
1130 static void
enable_breakpoint(bpt)1131 enable_breakpoint (bpt)
1132      struct breakpoint *bpt;
1133 {
1134   bpt->enable = enabled;
1135 
1136   if (xgdb_verbose && bpt->number >= 0)
1137     printf ("breakpoint #%d enabled\n", bpt->number);
1138 
1139   check_duplicates (bpt->address);
1140 }
1141 
1142 static void
enable_command(args)1143 enable_command (args)
1144      char *args;
1145 {
1146   struct breakpoint *bpt;
1147   if (args == 0)
1148     ALL_BREAKPOINTS (bpt)
1149       enable_breakpoint (bpt);
1150   else
1151     map_breakpoint_numbers (args, enable_breakpoint);
1152 }
1153 
1154 static void
disable_breakpoint(bpt)1155 disable_breakpoint (bpt)
1156      struct breakpoint *bpt;
1157 {
1158   bpt->enable = disabled;
1159 
1160   if (xgdb_verbose && bpt->number >= 0)
1161     printf ("breakpoint #%d disabled\n", bpt->number);
1162 
1163   check_duplicates (bpt->address);
1164 }
1165 
1166 static void
disable_command(args)1167 disable_command (args)
1168      char *args;
1169 {
1170   register struct breakpoint *bpt;
1171   if (args == 0)
1172     ALL_BREAKPOINTS (bpt)
1173       disable_breakpoint (bpt);
1174   else
1175     map_breakpoint_numbers (args, disable_breakpoint);
1176 }
1177 
1178 static void
enable_once_breakpoint(bpt)1179 enable_once_breakpoint (bpt)
1180      struct breakpoint *bpt;
1181 {
1182   bpt->enable = temporary;
1183 
1184   check_duplicates (bpt->address);
1185 }
1186 
1187 static void
enable_once_command(args)1188 enable_once_command (args)
1189      char *args;
1190 {
1191   map_breakpoint_numbers (args, enable_once_breakpoint);
1192 }
1193 
1194 static void
enable_delete_breakpoint(bpt)1195 enable_delete_breakpoint (bpt)
1196      struct breakpoint *bpt;
1197 {
1198   bpt->enable = delete;
1199 
1200   check_duplicates (bpt->address);
1201 }
1202 
1203 static void
enable_delete_command(args)1204 enable_delete_command (args)
1205      char *args;
1206 {
1207   map_breakpoint_numbers (args, enable_delete_breakpoint);
1208 }
1209 
1210 /*
1211  * Use default_breakpoint_'s, or nothing if they aren't valid.
1212  */
1213 struct symtabs_and_lines
decode_line_spec_1(string,funfirstline)1214 decode_line_spec_1 (string, funfirstline)
1215      char *string;
1216      int funfirstline;
1217 {
1218   struct symtabs_and_lines sals;
1219   if (string == 0)
1220     error ("Empty line specification.");
1221   if (default_breakpoint_valid)
1222     sals = decode_line_1 (&string, funfirstline,
1223 			  default_breakpoint_symtab, default_breakpoint_line);
1224   else
1225     sals = decode_line_1 (&string, funfirstline, 0, 0);
1226   if (*string)
1227     error ("Junk at end of line specification: %s", string);
1228   return sals;
1229 }
1230 
1231 
1232 /* Chain containing all defined enable commands.  */
1233 
1234 extern struct cmd_list_element
1235   *enablelist, *disablelist,
1236   *deletelist, *enablebreaklist;
1237 
1238 extern struct cmd_list_element *cmdlist;
1239 
1240 void
_initialize_breakpoint()1241 _initialize_breakpoint ()
1242 {
1243   breakpoint_chain = 0;
1244   breakpoint_count = 0;
1245 
1246   add_com ("ignore", class_breakpoint, ignore_command,
1247 	   "Set ignore-count of breakpoint number N to COUNT.");
1248 
1249   add_com ("commands", class_breakpoint, commands_command,
1250 	   "Set commands to be executed when a breakpoint is hit.\n\
1251 Give breakpoint number as argument after \"commands\".\n\
1252 With no argument, the targeted breakpoint is the last one set.\n\
1253 The commands themselves follow starting on the next line.\n\
1254 Type a line containing \"end\" to indicate the end of them.\n\
1255 Give \"silent\" as the first line to make the breakpoint silent;\n\
1256 then no output is printed when it is hit, except what the commands print.");
1257 
1258   add_com ("condition", class_breakpoint, condition_command,
1259 	   "Specify breakpoint number N to break only if COND is true.\n\
1260 N is an integer; COND is a C expression to be evaluated whenever\n\
1261 breakpoint N is reached.  Actually break only when COND is nonzero.");
1262 
1263   add_com ("tbreak", class_breakpoint, tbreak_command,
1264 	   "Set a temporary breakpoint.  Args like \"break\" command.\n\
1265 Like \"break\" except the breakpoint is only enabled temporarily,\n\
1266 so it will be disabled when hit.  Equivalent to \"break\" followed\n\
1267 by using \"enable once\" on the breakpoint number.");
1268 
1269   add_prefix_cmd ("enable", class_breakpoint, enable_command,
1270 		  "Enable some breakpoints or auto-display expressions.\n\
1271 Give breakpoint numbers (separated by spaces) as arguments.\n\
1272 With no subcommand, breakpoints are enabled until you command otherwise.\n\
1273 This is used to cancel the effect of the \"disable\" command.\n\
1274 With a subcommand you can enable temporarily.\n\
1275 \n\
1276 The \"display\" subcommand applies to auto-displays instead of breakpoints.",
1277 		  &enablelist, "enable ", 1, &cmdlist);
1278 
1279   add_abbrev_prefix_cmd ("breakpoints", class_breakpoint, enable_command,
1280 		  "Enable some breakpoints or auto-display expressions.\n\
1281 Give breakpoint numbers (separated by spaces) as arguments.\n\
1282 With no subcommand, breakpoints are enabled until you command otherwise.\n\
1283 This is used to cancel the effect of the \"disable\" command.\n\
1284 May be abbreviates to simply \"enable\".\n\
1285 With a subcommand you can enable temporarily.",
1286 		  &enablebreaklist, "enable breakpoints ", 1, &enablelist);
1287 
1288   add_cmd ("once", no_class, enable_once_command,
1289 	   "Enable breakpoints for one hit.  Give breakpoint numbers.\n\
1290 If a breakpoint is hit while enabled in this fashion, it becomes disabled.\n\
1291 See the \"tbreak\" command which sets a breakpoint and enables it once.",
1292 	   &enablebreaklist);
1293 
1294   add_cmd ("delete", no_class, enable_delete_command,
1295 	   "Enable breakpoints and delete when hit.  Give breakpoint numbers.\n\
1296 If a breakpoint is hit while enabled in this fashion, it is deleted.",
1297 	   &enablebreaklist);
1298 
1299   add_cmd ("delete", no_class, enable_delete_command,
1300 	   "Enable breakpoints and delete when hit.  Give breakpoint numbers.\n\
1301 If a breakpoint is hit while enabled in this fashion, it is deleted.",
1302 	   &enablelist);
1303 
1304   add_cmd ("once", no_class, enable_once_command,
1305 	   "Enable breakpoints for one hit.  Give breakpoint numbers.\n\
1306 If a breakpoint is hit while enabled in this fashion, it becomes disabled.\n\
1307 See the \"tbreak\" command which sets a breakpoint and enables it once.",
1308 	   &enablelist);
1309 
1310   add_prefix_cmd ("disable", class_breakpoint, disable_command,
1311 	   "Disable some breakpoints or auto-display expressions.\n\
1312 Arguments are breakpoint numbers with spaces in between.\n\
1313 To disable all breakpoints, give no argument.\n\
1314 A disabled breakpoint is not forgotten, but has no effect until reenabled.\n\
1315 \n\
1316 The \"display\" subcommand applies to auto-displays instead of breakpoints.",
1317 		  &disablelist, "disable ", 1, &cmdlist);
1318   add_com_alias ("dis", "disable", class_breakpoint, 1);
1319   add_com_alias ("disa", "disable", class_breakpoint, 1);
1320 
1321   add_abbrev_cmd ("breakpoints", class_breakpoint, disable_command,
1322 	   "Disable some breakpoints or auto-display expressions.\n\
1323 Arguments are breakpoint numbers with spaces in between.\n\
1324 To disable all breakpoints, give no argument.\n\
1325 A disabled breakpoint is not forgotten, but has no effect until reenabled.\n\
1326 This command may be abbreviated \"disable\".",
1327 	   &disablelist);
1328 
1329   add_prefix_cmd ("delete", class_breakpoint, delete_command,
1330 	   "Delete some breakpoints or auto-display expressions.\n\
1331 Arguments are breakpoint numbers with spaces in between.\n\
1332 To delete all breakpoints, give no argument.\n\
1333 \n\
1334 Also a prefix command for deletion of other GDB objects.\n\
1335 The \"unset\" command is also an alias for \"delete\".",
1336 		  &deletelist, "delete ", 1, &cmdlist);
1337   add_com_alias ("d", "delete", class_breakpoint, 1);
1338   add_com_alias ("unset", "delete", class_alias, 1);
1339 
1340   add_cmd ("breakpoints", class_alias, delete_command,
1341 	   "Delete some breakpoints or auto-display expressions.\n\
1342 Arguments are breakpoint numbers with spaces in between.\n\
1343 To delete all breakpoints, give no argument.\n\
1344 This command may be abbreviated \"delete\".",
1345 	   &deletelist);
1346 
1347   add_com ("clear", class_breakpoint, clear_command,
1348 	   "Clear breakpoint at specified line or function.\n\
1349 Argument may be line number, function name, or \"*\" and an address.\n\
1350 If line number is specified, all breakpoints in that line are cleared.\n\
1351 If function is specified, breakpoints at beginning of function are cleared.\n\
1352 If an address is specified, breakpoints at that address are cleared.\n\n\
1353 With no argument, clears all breakpoints in the line that the selected frame\n\
1354 is executing in.\n\
1355 \n\
1356 See also the \"delete\" command which clears breakpoints by number.");
1357 
1358   add_com ("break", class_breakpoint, break_command,
1359 	   "Set breakpoint at specified line or function.\n\
1360 Argument may be line number, function name, or \"*\" and an address.\n\
1361 If line number is specified, break at start of code for that line.\n\
1362 If function is specified, break at start of code for that function.\n\
1363 If an address is specified, break at that exact address.\n\
1364 With no arg, uses current execution address of selected stack frame.\n\
1365 This is useful for breaking on return to a stack frame.\n\
1366 \n\
1367 Multiple breakpoints at one place are permitted, and useful if conditional.\n\
1368 \n\
1369 Do \"help breakpoints\" for info on other commands dealing with breakpoints.");
1370   add_com_alias ("b", "break", class_run, 1);
1371   add_com_alias ("br", "break", class_run, 1);
1372   add_com_alias ("bre", "break", class_run, 1);
1373   add_com_alias ("brea", "break", class_run, 1);
1374 
1375   add_info ("breakpoints", breakpoints_info,
1376 	    "Status of all breakpoints, or breakpoint number NUMBER.\n\
1377 Second column is \"y\" for enabled breakpoint, \"n\" for disabled,\n\
1378 \"o\" for enabled once (disable when hit), \"d\" for enable but delete when hit.\n\
1379 Then come the address and the file/line number.\n\n\
1380 Convenience variable \"$_\" and default examine address for \"x\"\n\
1381 are set to the address of the last breakpoint listed.");
1382 }
1383 
1384