xref: /openbsd/gnu/usr.bin/binutils/gdb/breakpoint.c (revision 07ea8d15)
1 /* Everything about breakpoints, for GDB.
2    Copyright 1986, 1987, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996
3              Free Software Foundation, Inc.
4 
5 This file is part of GDB.
6 
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
11 
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 GNU General Public License for more details.
16 
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
20 
21 #include "defs.h"
22 #include <ctype.h>
23 #include "symtab.h"
24 #include "frame.h"
25 #include "breakpoint.h"
26 #include "gdbtypes.h"
27 #include "expression.h"
28 #include "gdbcore.h"
29 #include "gdbcmd.h"
30 #include "value.h"
31 #include "command.h"
32 #include "inferior.h"
33 #include "gdbthread.h"
34 #include "target.h"
35 #include "language.h"
36 #include "gdb_string.h"
37 #include "demangle.h"
38 #include "annotate.h"
39 
40 /* local function prototypes */
41 
42 static void
43 catch_command_1 PARAMS ((char *, int, int));
44 
45 static void
46 enable_delete_command PARAMS ((char *, int));
47 
48 static void
49 enable_delete_breakpoint PARAMS ((struct breakpoint *));
50 
51 static void
52 enable_once_command PARAMS ((char *, int));
53 
54 static void
55 enable_once_breakpoint PARAMS ((struct breakpoint *));
56 
57 static void
58 disable_command PARAMS ((char *, int));
59 
60 static void
61 enable_command PARAMS ((char *, int));
62 
63 static void
64 map_breakpoint_numbers PARAMS ((char *,	void (*)(struct breakpoint *)));
65 
66 static void
67 ignore_command PARAMS ((char *, int));
68 
69 static int
70 breakpoint_re_set_one PARAMS ((char *));
71 
72 static void
73 delete_command PARAMS ((char *, int));
74 
75 static void
76 clear_command PARAMS ((char *, int));
77 
78 static void
79 catch_command PARAMS ((char *, int));
80 
81 static struct symtabs_and_lines
82 get_catch_sals PARAMS ((int));
83 
84 static void
85 watch_command PARAMS ((char *, int));
86 
87 static int
88 can_use_hardware_watchpoint PARAMS ((struct value *));
89 
90 static void
91 tbreak_command PARAMS ((char *, int));
92 
93 static void
94 break_command_1 PARAMS ((char *, int, int));
95 
96 static void
97 mention PARAMS ((struct breakpoint *));
98 
99 static struct breakpoint *
100 set_raw_breakpoint PARAMS ((struct symtab_and_line));
101 
102 static void
103 check_duplicates PARAMS ((CORE_ADDR));
104 
105 static void
106 describe_other_breakpoints PARAMS ((CORE_ADDR));
107 
108 static void
109 breakpoints_info PARAMS ((char *, int));
110 
111 static void
112 breakpoint_1 PARAMS ((int, int));
113 
114 static bpstat
115 bpstat_alloc PARAMS ((struct breakpoint *, bpstat));
116 
117 static int
118 breakpoint_cond_eval PARAMS ((char *));
119 
120 static void
121 cleanup_executing_breakpoints PARAMS ((PTR));
122 
123 static void
124 commands_command PARAMS ((char *, int));
125 
126 static void
127 condition_command PARAMS ((char *, int));
128 
129 static int
130 get_number PARAMS ((char **));
131 
132 static void
133 set_breakpoint_count PARAMS ((int));
134 
135 static int
136 remove_breakpoint PARAMS ((struct breakpoint *));
137 
138 static int
139 print_it_normal PARAMS ((bpstat));
140 
141 static int
142 watchpoint_check PARAMS ((char *));
143 
144 static int
145 print_it_done PARAMS ((bpstat));
146 
147 static int
148 print_it_noop PARAMS ((bpstat));
149 
150 static void
151 maintenance_info_breakpoints PARAMS ((char *, int));
152 
153 #ifdef GET_LONGJMP_TARGET
154 static void
155 create_longjmp_breakpoint PARAMS ((char *));
156 #endif
157 
158 static int
159 hw_breakpoint_used_count PARAMS ((void));
160 
161 static int
162 hw_watchpoint_used_count PARAMS ((enum bptype, int *));
163 
164 static void
165 hbreak_command PARAMS ((char *, int));
166 
167 static void
168 thbreak_command PARAMS ((char *, int));
169 
170 static void
171 watch_command_1 PARAMS ((char *, int, int));
172 
173 static void
174 rwatch_command PARAMS ((char *, int));
175 
176 static void
177 awatch_command PARAMS ((char *, int));
178 
179 static void
180 do_enable_breakpoint PARAMS ((struct breakpoint *, enum bpdisp));
181 
182 extern int addressprint;		/* Print machine addresses? */
183 
184 #if defined (GET_LONGJMP_TARGET) || defined (SOLIB_ADD)
185 static int internal_breakpoint_number = -1;
186 #endif
187 
188 /* Are we executing breakpoint commands?  */
189 static int executing_breakpoint_commands;
190 
191 /* Walk the following statement or block through all breakpoints.
192    ALL_BREAKPOINTS_SAFE does so even if the statment deletes the current
193    breakpoint.  */
194 
195 #define ALL_BREAKPOINTS(b)  for (b = breakpoint_chain; b; b = b->next)
196 
197 #define ALL_BREAKPOINTS_SAFE(b,tmp)	\
198 	for (b = breakpoint_chain;	\
199 	     b? (tmp=b->next, 1): 0;	\
200 	     b = tmp)
201 
202 /* True if SHIFT_INST_REGS defined, false otherwise.  */
203 
204 int must_shift_inst_regs =
205 #if defined(SHIFT_INST_REGS)
206 1
207 #else
208 0
209 #endif
210 ;
211 
212 /* True if breakpoint hit counts should be displayed in breakpoint info.  */
213 
214 int show_breakpoint_hit_counts = 1;
215 
216 /* Chain of all breakpoints defined.  */
217 
218 struct breakpoint *breakpoint_chain;
219 
220 /* Number of last breakpoint made.  */
221 
222 static int breakpoint_count;
223 
224 /* Set breakpoint count to NUM.  */
225 
226 static void
227 set_breakpoint_count (num)
228      int num;
229 {
230   breakpoint_count = num;
231   set_internalvar (lookup_internalvar ("bpnum"),
232 		   value_from_longest (builtin_type_int, (LONGEST) num));
233 }
234 
235 /* Used in run_command to zero the hit count when a new run starts. */
236 
237 void
238 clear_breakpoint_hit_counts ()
239 {
240   struct breakpoint *b;
241 
242   ALL_BREAKPOINTS (b)
243     b->hit_count = 0;
244 }
245 
246 /* Default address, symtab and line to put a breakpoint at
247    for "break" command with no arg.
248    if default_breakpoint_valid is zero, the other three are
249    not valid, and "break" with no arg is an error.
250 
251    This set by print_stack_frame, which calls set_default_breakpoint.  */
252 
253 int default_breakpoint_valid;
254 CORE_ADDR default_breakpoint_address;
255 struct symtab *default_breakpoint_symtab;
256 int default_breakpoint_line;
257 
258 /* *PP is a string denoting a breakpoint.  Get the number of the breakpoint.
259    Advance *PP after the string and any trailing whitespace.
260 
261    Currently the string can either be a number or "$" followed by the name
262    of a convenience variable.  Making it an expression wouldn't work well
263    for map_breakpoint_numbers (e.g. "4 + 5 + 6").  */
264 static int
265 get_number (pp)
266      char **pp;
267 {
268   int retval;
269   char *p = *pp;
270 
271   if (p == NULL)
272     /* Empty line means refer to the last breakpoint.  */
273     return breakpoint_count;
274   else if (*p == '$')
275     {
276       /* Make a copy of the name, so we can null-terminate it
277 	 to pass to lookup_internalvar().  */
278       char *varname;
279       char *start = ++p;
280       value_ptr val;
281 
282       while (isalnum (*p) || *p == '_')
283 	p++;
284       varname = (char *) alloca (p - start + 1);
285       strncpy (varname, start, p - start);
286       varname[p - start] = '\0';
287       val = value_of_internalvar (lookup_internalvar (varname));
288       if (TYPE_CODE (VALUE_TYPE (val)) != TYPE_CODE_INT)
289 	error (
290 "Convenience variables used to specify breakpoints must have integer values."
291 	       );
292       retval = (int) value_as_long (val);
293     }
294   else
295     {
296       if (*p == '-')
297 	++p;
298       while (*p >= '0' && *p <= '9')
299 	++p;
300       if (p == *pp)
301 	/* There is no number here.  (e.g. "cond a == b").  */
302 	error_no_arg ("breakpoint number");
303       retval = atoi (*pp);
304     }
305   if (!(isspace (*p) || *p == '\0'))
306     error ("breakpoint number expected");
307   while (isspace (*p))
308     p++;
309   *pp = p;
310   return retval;
311 }
312 
313 /* condition N EXP -- set break condition of breakpoint N to EXP.  */
314 
315 static void
316 condition_command (arg, from_tty)
317      char *arg;
318      int from_tty;
319 {
320   register struct breakpoint *b;
321   char *p;
322   register int bnum;
323 
324   if (arg == 0)
325     error_no_arg ("breakpoint number");
326 
327   p = arg;
328   bnum = get_number (&p);
329 
330   ALL_BREAKPOINTS (b)
331     if (b->number == bnum)
332       {
333 	if (b->cond)
334 	  {
335 	    free ((PTR)b->cond);
336 	    b->cond = 0;
337 	  }
338 	if (b->cond_string != NULL)
339 	  free ((PTR)b->cond_string);
340 
341 	if (*p == 0)
342 	  {
343 	    b->cond = 0;
344 	    b->cond_string = NULL;
345 	    if (from_tty)
346 	      printf_filtered ("Breakpoint %d now unconditional.\n", bnum);
347 	  }
348 	else
349 	  {
350 	    arg = p;
351 	    /* I don't know if it matters whether this is the string the user
352 	       typed in or the decompiled expression.  */
353 	    b->cond_string = savestring (arg, strlen (arg));
354 	    b->cond = parse_exp_1 (&arg, block_for_pc (b->address), 0);
355 	    if (*arg)
356 	      error ("Junk at end of expression");
357 	  }
358 	breakpoints_changed ();
359 	return;
360       }
361 
362   error ("No breakpoint number %d.", bnum);
363 }
364 
365 /* ARGSUSED */
366 static void
367 commands_command (arg, from_tty)
368      char *arg;
369      int from_tty;
370 {
371   register struct breakpoint *b;
372   char *p;
373   register int bnum;
374   struct command_line *l;
375 
376   /* If we allowed this, we would have problems with when to
377      free the storage, if we change the commands currently
378      being read from.  */
379 
380   if (executing_breakpoint_commands)
381     error ("Can't use the \"commands\" command among a breakpoint's commands.");
382 
383   p = arg;
384   bnum = get_number (&p);
385   if (p && *p)
386     error ("Unexpected extra arguments following breakpoint number.");
387 
388   ALL_BREAKPOINTS (b)
389     if (b->number == bnum)
390       {
391 	char tmpbuf[128];
392 	sprintf (tmpbuf, "Type commands for when breakpoint %d is hit, one per line.", bnum);
393 	l = read_command_lines (tmpbuf, from_tty);
394 	free_command_lines (&b->commands);
395 	b->commands = l;
396 	breakpoints_changed ();
397 	return;
398       }
399   error ("No breakpoint number %d.", bnum);
400 }
401 
402 extern int memory_breakpoint_size; /* from mem-break.c */
403 
404 /* Like target_read_memory() but if breakpoints are inserted, return
405    the shadow contents instead of the breakpoints themselves.
406 
407    Read "memory data" from whatever target or inferior we have.
408    Returns zero if successful, errno value if not.  EIO is used
409    for address out of bounds.  If breakpoints are inserted, returns
410    shadow contents, not the breakpoints themselves.  From breakpoint.c.  */
411 
412 int
413 read_memory_nobpt (memaddr, myaddr, len)
414      CORE_ADDR memaddr;
415      char *myaddr;
416      unsigned len;
417 {
418   int status;
419   struct breakpoint *b;
420 
421   if (memory_breakpoint_size < 0)
422     /* No breakpoints on this machine.  FIXME: This should be
423        dependent on the debugging target.  Probably want
424        target_insert_breakpoint to return a size, saying how many
425        bytes of the shadow contents are used, or perhaps have
426        something like target_xfer_shadow.  */
427     return target_read_memory (memaddr, myaddr, len);
428 
429   ALL_BREAKPOINTS (b)
430     {
431       if (b->type == bp_watchpoint
432 	  || b->type == bp_hardware_watchpoint
433 	  || b->type == bp_read_watchpoint
434 	  || b->type == bp_access_watchpoint
435 	  || !b->inserted)
436 	continue;
437       else if (b->address + memory_breakpoint_size <= memaddr)
438 	/* The breakpoint is entirely before the chunk of memory
439 	   we are reading.  */
440 	continue;
441       else if (b->address >= memaddr + len)
442 	/* The breakpoint is entirely after the chunk of memory we
443 	   are reading.  */
444 	continue;
445       else
446 	{
447 	  /* Copy the breakpoint from the shadow contents, and recurse
448 	     for the things before and after.  */
449 
450 	  /* Addresses and length of the part of the breakpoint that
451 	     we need to copy.  */
452 	  CORE_ADDR membpt = b->address;
453 	  unsigned int bptlen = memory_breakpoint_size;
454 	  /* Offset within shadow_contents.  */
455 	  int bptoffset = 0;
456 
457 	  if (membpt < memaddr)
458 	    {
459 	      /* Only copy the second part of the breakpoint.  */
460 	      bptlen -= memaddr - membpt;
461 	      bptoffset = memaddr - membpt;
462 	      membpt = memaddr;
463 	    }
464 
465 	  if (membpt + bptlen > memaddr + len)
466 	    {
467 	      /* Only copy the first part of the breakpoint.  */
468 	      bptlen -= (membpt + bptlen) - (memaddr + len);
469 	    }
470 
471 	  memcpy (myaddr + membpt - memaddr,
472 		  b->shadow_contents + bptoffset, bptlen);
473 
474 	  if (membpt > memaddr)
475 	    {
476 	      /* Copy the section of memory before the breakpoint.  */
477 	      status = read_memory_nobpt (memaddr, myaddr, membpt - memaddr);
478 	      if (status != 0)
479 		return status;
480 	    }
481 
482 	  if (membpt + bptlen < memaddr + len)
483 	    {
484 	      /* Copy the section of memory after the breakpoint.  */
485 	      status = read_memory_nobpt
486 		(membpt + bptlen,
487 		 myaddr + membpt + bptlen - memaddr,
488 		 memaddr + len - (membpt + bptlen));
489 	      if (status != 0)
490 		return status;
491 	    }
492 	  return 0;
493 	}
494     }
495   /* Nothing overlaps.  Just call read_memory_noerr.  */
496   return target_read_memory (memaddr, myaddr, len);
497 }
498 
499 /* insert_breakpoints is used when starting or continuing the program.
500    remove_breakpoints is used when the program stops.
501    Both return zero if successful,
502    or an `errno' value if could not write the inferior.  */
503 
504 int
505 insert_breakpoints ()
506 {
507   register struct breakpoint *b, *temp;
508   int val = 0;
509   int disabled_breaks = 0;
510 
511   ALL_BREAKPOINTS_SAFE (b, temp)
512     if (b->type != bp_watchpoint
513 	&& b->type != bp_hardware_watchpoint
514 	&& b->type != bp_read_watchpoint
515 	&& b->type != bp_access_watchpoint
516 	&& b->enable != disabled
517 	&& b->enable != shlib_disabled
518 	&& ! b->inserted
519 	&& ! b->duplicate)
520       {
521 	if (b->type == bp_hardware_breakpoint)
522 	  val = target_insert_hw_breakpoint(b->address, b->shadow_contents);
523 	else
524 	  val = target_insert_breakpoint(b->address, b->shadow_contents);
525 	if (val)
526 	  {
527 	    /* Can't set the breakpoint.  */
528 #if defined (DISABLE_UNSETTABLE_BREAK)
529 	    if (DISABLE_UNSETTABLE_BREAK (b->address))
530 	      {
531 		val = 0;
532 		b->enable = shlib_disabled;
533 		if (!disabled_breaks)
534 		  {
535 		    target_terminal_ours_for_output ();
536 		    fprintf_unfiltered (gdb_stderr,
537 			 "Cannot insert breakpoint %d:\n", b->number);
538 		    printf_filtered ("Temporarily disabling shared library breakpoints:\n");
539 		  }
540 		disabled_breaks = 1;
541 		printf_filtered ("%d ", b->number);
542 	      }
543 	    else
544 #endif
545 	      {
546 		target_terminal_ours_for_output ();
547 		fprintf_unfiltered (gdb_stderr, "Cannot insert breakpoint %d:\n", b->number);
548 #ifdef ONE_PROCESS_WRITETEXT
549 		fprintf_unfiltered (gdb_stderr,
550 		  "The same program may be running in another process.\n");
551 #endif
552 		memory_error (val, b->address);	/* which bombs us out */
553 	      }
554 	  }
555 	else
556 	  b->inserted = 1;
557       }
558     else if ((b->type == bp_hardware_watchpoint ||
559 	      b->type == bp_read_watchpoint ||
560 	      b->type == bp_access_watchpoint)
561 	     && b->enable == enabled
562 	     && ! b->inserted
563 	     && ! b->duplicate)
564       {
565 	struct frame_info *saved_frame;
566 	int saved_level, within_current_scope;
567 	value_ptr mark = value_mark ();
568 	value_ptr v;
569 
570 	/* Save the current frame and level so we can restore it after
571 	   evaluating the watchpoint expression on its own frame.  */
572 	saved_frame = selected_frame;
573 	saved_level = selected_frame_level;
574 
575 	/* Determine if the watchpoint is within scope.  */
576 	if (b->exp_valid_block == NULL)
577 	  within_current_scope = 1;
578 	else
579 	  {
580 	    struct frame_info *fi =
581 	      find_frame_addr_in_frame_chain (b->watchpoint_frame);
582 	    within_current_scope = (fi != NULL);
583 	    if (within_current_scope)
584 	      select_frame (fi, -1);
585 	  }
586 
587 	if (within_current_scope)
588 	  {
589 	    /* Evaluate the expression and cut the chain of values
590 	       produced off from the value chain.  */
591 	    v = evaluate_expression (b->exp);
592 	    value_release_to_mark (mark);
593 
594 	    b->val_chain = v;
595 	    b->inserted = 1;
596 
597 	    /* Look at each value on the value chain.  */
598 	    for ( ; v; v=v->next)
599 	      {
600 		/* If it's a memory location, then we must watch it.  */
601 		if (v->lval == lval_memory)
602 		  {
603 		    int addr, len, type;
604 
605 		    addr = VALUE_ADDRESS (v) + VALUE_OFFSET (v);
606 		    len = TYPE_LENGTH (VALUE_TYPE (v));
607 		    type = 0;
608                     if (b->type == bp_read_watchpoint)
609 		      type = 1;
610 		    else if (b->type == bp_access_watchpoint)
611 		      type = 2;
612 
613 		    val = target_insert_watchpoint (addr, len, type);
614 		    if (val == -1)
615 		      {
616 			b->inserted = 0;
617 			break;
618 		      }
619 		    val = 0;
620 		  }
621 	      }
622 	    /* Failure to insert a watchpoint on any memory value in the
623 	       value chain brings us here.  */
624 	    if (!b->inserted)
625 	      warning ("Hardware watchpoint %d: Could not insert watchpoint\n",
626 		       b->number);
627 	  }
628 	else
629 	  {
630 	    printf_filtered ("\
631 Hardware watchpoint %d deleted because the program has left the block in\n\
632 which its expression is valid.\n", b->number);
633 	    if (b->related_breakpoint)
634 	      {
635 		b->related_breakpoint->enable = disable;
636 		b->related_breakpoint->disposition = del_at_next_stop;
637 	      }
638 	    b->enable = disable;
639 	    b->disposition = del_at_next_stop;
640 	  }
641 
642 	/* Restore the frame and level.  */
643         select_frame (saved_frame, saved_level);
644       }
645   if (disabled_breaks)
646     printf_filtered ("\n");
647   return val;
648 }
649 
650 
651 int
652 remove_breakpoints ()
653 {
654   register struct breakpoint *b;
655   int val;
656 
657   ALL_BREAKPOINTS (b)
658     {
659       if (b->inserted)
660 	{
661 	  val = remove_breakpoint (b);
662 	  if (val != 0)
663 	    return val;
664 	}
665     }
666   return 0;
667 }
668 
669 
670 static int
671 remove_breakpoint (b)
672      struct breakpoint *b;
673 {
674   int val;
675 
676   if (b->type != bp_watchpoint
677       && b->type != bp_hardware_watchpoint
678       && b->type != bp_read_watchpoint
679       && b->type != bp_access_watchpoint)
680     {
681       if (b->type == bp_hardware_breakpoint)
682         val = target_remove_hw_breakpoint(b->address, b->shadow_contents);
683       else
684         val = target_remove_breakpoint(b->address, b->shadow_contents);
685       if (val)
686 	return val;
687       b->inserted = 0;
688     }
689   else if ((b->type == bp_hardware_watchpoint ||
690             b->type == bp_read_watchpoint ||
691   	    b->type == bp_access_watchpoint)
692 	   && b->enable == enabled
693 	   && ! b->duplicate)
694     {
695       value_ptr v, n;
696 
697       b->inserted = 0;
698       /* Walk down the saved value chain.  */
699       for (v = b->val_chain; v; v = v->next)
700 	{
701 	  /* For each memory reference remove the watchpoint
702 	     at that address.  */
703 	  if (v->lval == lval_memory)
704 	    {
705 	      int addr, len;
706 
707 	      addr = VALUE_ADDRESS (v) + VALUE_OFFSET (v);
708 	      len = TYPE_LENGTH (VALUE_TYPE (v));
709 	      val = target_remove_watchpoint (addr, len, b->type);
710 	      if (val == -1)
711 		b->inserted = 1;
712 	      val = 0;
713 	    }
714 	}
715       /* Failure to remove any of the hardware watchpoints comes here.  */
716       if (b->inserted)
717 	warning ("Hardware watchpoint %d: Could not remove watchpoint\n",
718 		 b->number);
719 
720       /* Free the saved value chain.  We will construct a new one
721 	 the next time the watchpoint is inserted.  */
722       for (v = b->val_chain; v; v = n)
723 	{
724 	  n = v->next;
725 	  value_free (v);
726 	}
727       b->val_chain = NULL;
728     }
729   return 0;
730 }
731 
732 /* Clear the "inserted" flag in all breakpoints.  */
733 
734 void
735 mark_breakpoints_out ()
736 {
737   register struct breakpoint *b;
738 
739   ALL_BREAKPOINTS (b)
740     b->inserted = 0;
741 }
742 
743 /* Clear the "inserted" flag in all breakpoints and delete any breakpoints
744    which should go away between runs of the program.  */
745 
746 void
747 breakpoint_init_inferior ()
748 {
749   register struct breakpoint *b, *temp;
750 
751   ALL_BREAKPOINTS_SAFE (b, temp)
752     {
753       b->inserted = 0;
754 
755       switch (b->type)
756 	{
757 	case bp_call_dummy:
758 	case bp_watchpoint_scope:
759 
760 	  /* If the call dummy breakpoint is at the entry point it will
761 	     cause problems when the inferior is rerun, so we better
762 	     get rid of it.
763 
764 	     Also get rid of scope breakpoints.  */
765 	  delete_breakpoint (b);
766 	  break;
767 
768 	case bp_watchpoint:
769 	case bp_hardware_watchpoint:
770 	case bp_read_watchpoint:
771 	case bp_access_watchpoint:
772 
773 	  /* Likewise for watchpoints on local expressions.  */
774 	  if (b->exp_valid_block != NULL)
775 	    delete_breakpoint (b);
776 	  break;
777 
778 	default:
779 	  break;
780 	}
781     }
782 }
783 
784 /* breakpoint_here_p (PC) returns 1 if an enabled breakpoint exists at PC.
785    When continuing from a location with a breakpoint,
786    we actually single step once before calling insert_breakpoints.  */
787 
788 int
789 breakpoint_here_p (pc)
790      CORE_ADDR pc;
791 {
792   register struct breakpoint *b;
793 
794   ALL_BREAKPOINTS (b)
795     if (b->enable == enabled
796 	&& b->address == pc)
797       return 1;
798 
799   return 0;
800 }
801 
802 /* breakpoint_inserted_here_p (PC) is just like breakpoint_here_p(), but it
803    only returns true if there is actually a breakpoint inserted at PC.  */
804 
805 int
806 breakpoint_inserted_here_p (pc)
807      CORE_ADDR pc;
808 {
809   register struct breakpoint *b;
810 
811   ALL_BREAKPOINTS (b)
812     if (b->inserted
813 	&& b->address == pc)
814       return 1;
815 
816   return 0;
817 }
818 
819 /* Return nonzero if FRAME is a dummy frame.  We can't use PC_IN_CALL_DUMMY
820    because figuring out the saved SP would take too much time, at least using
821    get_saved_register on the 68k.  This means that for this function to
822    work right a port must use the bp_call_dummy breakpoint.  */
823 
824 int
825 frame_in_dummy (frame)
826      struct frame_info *frame;
827 {
828   struct breakpoint *b;
829 
830 #ifdef CALL_DUMMY
831   ALL_BREAKPOINTS (b)
832     {
833       static unsigned LONGEST dummy[] = CALL_DUMMY;
834 
835       if (b->type == bp_call_dummy
836 	  && b->frame == frame->frame
837 
838 	  /* We need to check the PC as well as the frame on the sparc,
839 	     for signals.exp in the testsuite.  */
840 	  && (frame->pc
841 	      >= (b->address
842 		  - sizeof (dummy) / sizeof (LONGEST) * REGISTER_SIZE))
843 	  && frame->pc <= b->address)
844 	return 1;
845     }
846 #endif /* CALL_DUMMY */
847   return 0;
848 }
849 
850 /* breakpoint_match_thread (PC, PID) returns true if the breakpoint at PC
851    is valid for process/thread PID.  */
852 
853 int
854 breakpoint_thread_match (pc, pid)
855      CORE_ADDR pc;
856      int pid;
857 {
858   struct breakpoint *b;
859   int thread;
860 
861   thread = pid_to_thread_id (pid);
862 
863   ALL_BREAKPOINTS (b)
864     if (b->enable != disabled
865 	&& b->enable != shlib_disabled
866 	&& b->address == pc
867 	&& (b->thread == -1 || b->thread == thread))
868       return 1;
869 
870   return 0;
871 }
872 
873 
874 /* bpstat stuff.  External routines' interfaces are documented
875    in breakpoint.h.  */
876 
877 /* Clear a bpstat so that it says we are not at any breakpoint.
878    Also free any storage that is part of a bpstat.  */
879 
880 void
881 bpstat_clear (bsp)
882      bpstat *bsp;
883 {
884   bpstat p;
885   bpstat q;
886 
887   if (bsp == 0)
888     return;
889   p = *bsp;
890   while (p != NULL)
891     {
892       q = p->next;
893       if (p->old_val != NULL)
894 	value_free (p->old_val);
895       free ((PTR)p);
896       p = q;
897     }
898   *bsp = NULL;
899 }
900 
901 /* Return a copy of a bpstat.  Like "bs1 = bs2" but all storage that
902    is part of the bpstat is copied as well.  */
903 
904 bpstat
905 bpstat_copy (bs)
906      bpstat bs;
907 {
908   bpstat p = NULL;
909   bpstat tmp;
910   bpstat retval = NULL;
911 
912   if (bs == NULL)
913     return bs;
914 
915   for (; bs != NULL; bs = bs->next)
916     {
917       tmp = (bpstat) xmalloc (sizeof (*tmp));
918       memcpy (tmp, bs, sizeof (*tmp));
919       if (p == NULL)
920 	/* This is the first thing in the chain.  */
921 	retval = tmp;
922       else
923 	p->next = tmp;
924       p = tmp;
925     }
926   p->next = NULL;
927   return retval;
928 }
929 
930 /* Find the bpstat associated with this breakpoint */
931 
932 bpstat
933 bpstat_find_breakpoint(bsp, breakpoint)
934      bpstat bsp;
935      struct breakpoint *breakpoint;
936 {
937   if (bsp == NULL) return NULL;
938 
939   for (;bsp != NULL; bsp = bsp->next) {
940     if (bsp->breakpoint_at == breakpoint) return bsp;
941   }
942   return NULL;
943 }
944 
945 /* Return the breakpoint number of the first breakpoint we are stopped
946    at.  *BSP upon return is a bpstat which points to the remaining
947    breakpoints stopped at (but which is not guaranteed to be good for
948    anything but further calls to bpstat_num).
949    Return 0 if passed a bpstat which does not indicate any breakpoints.  */
950 
951 int
952 bpstat_num (bsp)
953      bpstat *bsp;
954 {
955   struct breakpoint *b;
956 
957   if ((*bsp) == NULL)
958     return 0;			/* No more breakpoint values */
959   else
960     {
961       b = (*bsp)->breakpoint_at;
962       *bsp = (*bsp)->next;
963       if (b == NULL)
964 	return -1;		/* breakpoint that's been deleted since */
965       else
966         return b->number;	/* We have its number */
967     }
968 }
969 
970 /* Modify BS so that the actions will not be performed.  */
971 
972 void
973 bpstat_clear_actions (bs)
974      bpstat bs;
975 {
976   for (; bs != NULL; bs = bs->next)
977     {
978       bs->commands = NULL;
979       if (bs->old_val != NULL)
980 	{
981 	  value_free (bs->old_val);
982 	  bs->old_val = NULL;
983 	}
984     }
985 }
986 
987 /* Stub for cleaning up our state if we error-out of a breakpoint command */
988 /* ARGSUSED */
989 static void
990 cleanup_executing_breakpoints (ignore)
991      PTR ignore;
992 {
993   executing_breakpoint_commands = 0;
994 }
995 
996 /* Execute all the commands associated with all the breakpoints at this
997    location.  Any of these commands could cause the process to proceed
998    beyond this point, etc.  We look out for such changes by checking
999    the global "breakpoint_proceeded" after each command.  */
1000 
1001 void
1002 bpstat_do_actions (bsp)
1003      bpstat *bsp;
1004 {
1005   bpstat bs;
1006   struct cleanup *old_chain;
1007   struct command_line *cmd;
1008 
1009   /* Avoid endless recursion if a `source' command is contained
1010      in bs->commands.  */
1011   if (executing_breakpoint_commands)
1012     return;
1013 
1014   executing_breakpoint_commands = 1;
1015   old_chain = make_cleanup (cleanup_executing_breakpoints, 0);
1016 
1017 top:
1018   bs = *bsp;
1019 
1020   breakpoint_proceeded = 0;
1021   for (; bs != NULL; bs = bs->next)
1022     {
1023       cmd = bs->commands;
1024       while (cmd != NULL)
1025 	{
1026 	  execute_control_command (cmd);
1027 	  cmd = cmd->next;
1028 	}
1029       if (breakpoint_proceeded)
1030 	/* The inferior is proceeded by the command; bomb out now.
1031 	   The bpstat chain has been blown away by wait_for_inferior.
1032 	   But since execution has stopped again, there is a new bpstat
1033 	   to look at, so start over.  */
1034 	goto top;
1035       else
1036 	bs->commands = NULL;
1037     }
1038 
1039   executing_breakpoint_commands = 0;
1040   discard_cleanups (old_chain);
1041 }
1042 
1043 /* This is the normal print_it function for a bpstat.  In the future,
1044    much of this logic could (should?) be moved to bpstat_stop_status,
1045    by having it set different print_it functions.  */
1046 
1047 static int
1048 print_it_normal (bs)
1049      bpstat bs;
1050 {
1051   /* bs->breakpoint_at can be NULL if it was a momentary breakpoint
1052      which has since been deleted.  */
1053   if (bs->breakpoint_at == NULL
1054       || (bs->breakpoint_at->type != bp_breakpoint
1055 	  && bs->breakpoint_at->type != bp_hardware_breakpoint
1056 	  && bs->breakpoint_at->type != bp_watchpoint
1057 	  && bs->breakpoint_at->type != bp_read_watchpoint
1058 	  && bs->breakpoint_at->type != bp_access_watchpoint
1059 	  && bs->breakpoint_at->type != bp_hardware_watchpoint))
1060     return 0;
1061 
1062   if (bs->breakpoint_at->type == bp_breakpoint ||
1063       bs->breakpoint_at->type == bp_hardware_breakpoint)
1064     {
1065       /* I think the user probably only wants to see one breakpoint
1066 	 number, not all of them.  */
1067       annotate_breakpoint (bs->breakpoint_at->number);
1068       printf_filtered ("\nBreakpoint %d, ", bs->breakpoint_at->number);
1069       return 0;
1070     }
1071   else if ((bs->old_val != NULL) &&
1072 	(bs->breakpoint_at->type == bp_watchpoint ||
1073 	 bs->breakpoint_at->type == bp_access_watchpoint ||
1074 	 bs->breakpoint_at->type == bp_hardware_watchpoint))
1075     {
1076       annotate_watchpoint (bs->breakpoint_at->number);
1077       mention (bs->breakpoint_at);
1078       printf_filtered ("\nOld value = ");
1079       value_print (bs->old_val, gdb_stdout, 0, Val_pretty_default);
1080       printf_filtered ("\nNew value = ");
1081       value_print (bs->breakpoint_at->val, gdb_stdout, 0,
1082 		   Val_pretty_default);
1083       printf_filtered ("\n");
1084       value_free (bs->old_val);
1085       bs->old_val = NULL;
1086       /* More than one watchpoint may have been triggered.  */
1087       return -1;
1088     }
1089   else if (bs->breakpoint_at->type == bp_access_watchpoint ||
1090 	   bs->breakpoint_at->type == bp_read_watchpoint)
1091     {
1092       mention (bs->breakpoint_at);
1093       printf_filtered ("\nValue = ");
1094       value_print (bs->breakpoint_at->val, gdb_stdout, 0,
1095                    Val_pretty_default);
1096       printf_filtered ("\n");
1097       return -1;
1098     }
1099   /* We can't deal with it.  Maybe another member of the bpstat chain can.  */
1100   return -1;
1101 }
1102 
1103 /* Print a message indicating what happened.  Returns nonzero to
1104    say that only the source line should be printed after this (zero
1105    return means print the frame as well as the source line).  */
1106 /* Currently we always return zero.  */
1107 int
1108 bpstat_print (bs)
1109      bpstat bs;
1110 {
1111   int val;
1112 
1113   if (bs == NULL)
1114     return 0;
1115 
1116   val = (*bs->print_it) (bs);
1117   if (val >= 0)
1118     return val;
1119 
1120   /* Maybe another breakpoint in the chain caused us to stop.
1121      (Currently all watchpoints go on the bpstat whether hit or
1122      not.  That probably could (should) be changed, provided care is taken
1123      with respect to bpstat_explains_signal).  */
1124   if (bs->next)
1125     return bpstat_print (bs->next);
1126 
1127   /* We reached the end of the chain without printing anything.  */
1128   return 0;
1129 }
1130 
1131 /* Evaluate the expression EXP and return 1 if value is zero.
1132    This is used inside a catch_errors to evaluate the breakpoint condition.
1133    The argument is a "struct expression *" that has been cast to char * to
1134    make it pass through catch_errors.  */
1135 
1136 static int
1137 breakpoint_cond_eval (exp)
1138      char *exp;
1139 {
1140   value_ptr mark = value_mark ();
1141   int i = !value_true (evaluate_expression ((struct expression *)exp));
1142   value_free_to_mark (mark);
1143   return i;
1144 }
1145 
1146 /* Allocate a new bpstat and chain it to the current one.  */
1147 
1148 static bpstat
1149 bpstat_alloc (b, cbs)
1150      register struct breakpoint *b;
1151      bpstat cbs;			/* Current "bs" value */
1152 {
1153   bpstat bs;
1154 
1155   bs = (bpstat) xmalloc (sizeof (*bs));
1156   cbs->next = bs;
1157   bs->breakpoint_at = b;
1158   /* If the condition is false, etc., don't do the commands.  */
1159   bs->commands = NULL;
1160   bs->old_val = NULL;
1161   bs->print_it = print_it_normal;
1162   return bs;
1163 }
1164 
1165 /* Possible return values for watchpoint_check (this can't be an enum
1166    because of check_errors).  */
1167 /* The watchpoint has been deleted.  */
1168 #define WP_DELETED 1
1169 /* The value has changed.  */
1170 #define WP_VALUE_CHANGED 2
1171 /* The value has not changed.  */
1172 #define WP_VALUE_NOT_CHANGED 3
1173 
1174 #define BP_TEMPFLAG 1
1175 #define BP_HARDWAREFLAG 2
1176 
1177 /* Check watchpoint condition.  */
1178 
1179 static int
1180 watchpoint_check (p)
1181      char *p;
1182 {
1183   bpstat bs = (bpstat) p;
1184   struct breakpoint *b;
1185   struct frame_info *fr;
1186   int within_current_scope;
1187 
1188   b = bs->breakpoint_at;
1189 
1190   if (b->exp_valid_block == NULL)
1191     within_current_scope = 1;
1192   else
1193     {
1194       /* There is no current frame at this moment.  If we're going to have
1195 	 any chance of handling watchpoints on local variables, we'll need
1196 	 the frame chain (so we can determine if we're in scope).  */
1197       reinit_frame_cache();
1198       fr = find_frame_addr_in_frame_chain (b->watchpoint_frame);
1199       within_current_scope = (fr != NULL);
1200       if (within_current_scope)
1201 	/* If we end up stopping, the current frame will get selected
1202 	   in normal_stop.  So this call to select_frame won't affect
1203 	   the user.  */
1204 	select_frame (fr, -1);
1205     }
1206 
1207   if (within_current_scope)
1208     {
1209       /* We use value_{,free_to_}mark because it could be a
1210          *long* time before we return to the command level and
1211 	 call free_all_values.  We can't call free_all_values because
1212 	 we might be in the middle of evaluating a function call.  */
1213 
1214       value_ptr mark = value_mark ();
1215       value_ptr new_val = evaluate_expression (bs->breakpoint_at->exp);
1216       if (!value_equal (b->val, new_val))
1217 	{
1218 	  release_value (new_val);
1219 	  value_free_to_mark (mark);
1220 	  bs->old_val = b->val;
1221 	  b->val = new_val;
1222 	  /* We will stop here */
1223 	  return WP_VALUE_CHANGED;
1224 	}
1225       else
1226 	{
1227 	  /* Nothing changed, don't do anything.  */
1228 	  value_free_to_mark (mark);
1229 	  /* We won't stop here */
1230 	  return WP_VALUE_NOT_CHANGED;
1231 	}
1232     }
1233   else
1234     {
1235       /* This seems like the only logical thing to do because
1236 	 if we temporarily ignored the watchpoint, then when
1237 	 we reenter the block in which it is valid it contains
1238 	 garbage (in the case of a function, it may have two
1239 	 garbage values, one before and one after the prologue).
1240 	 So we can't even detect the first assignment to it and
1241 	 watch after that (since the garbage may or may not equal
1242 	 the first value assigned).  */
1243       printf_filtered ("\
1244 Watchpoint %d deleted because the program has left the block in\n\
1245 which its expression is valid.\n", bs->breakpoint_at->number);
1246       if (b->related_breakpoint)
1247 	{
1248 	  b->related_breakpoint->enable = disable;
1249 	  b->related_breakpoint->disposition = del_at_next_stop;
1250 	}
1251       b->enable = disable;
1252       b->disposition = del_at_next_stop;
1253 
1254       return WP_DELETED;
1255     }
1256 }
1257 
1258 /* This is used when everything which needs to be printed has
1259    already been printed.  But we still want to print the frame.  */
1260 static int
1261 print_it_done (bs)
1262      bpstat bs;
1263 {
1264   return 0;
1265 }
1266 
1267 /* This is used when nothing should be printed for this bpstat entry.  */
1268 
1269 static int
1270 print_it_noop (bs)
1271      bpstat bs;
1272 {
1273   return -1;
1274 }
1275 
1276 /* Get a bpstat associated with having just stopped at address *PC
1277    and frame address CORE_ADDRESS.  Update *PC to point at the
1278    breakpoint (if we hit a breakpoint).  NOT_A_BREAKPOINT is nonzero
1279    if this is known to not be a real breakpoint (it could still be a
1280    watchpoint, though).  */
1281 
1282 /* Determine whether we stopped at a breakpoint, etc, or whether we
1283    don't understand this stop.  Result is a chain of bpstat's such that:
1284 
1285 	if we don't understand the stop, the result is a null pointer.
1286 
1287 	if we understand why we stopped, the result is not null.
1288 
1289 	Each element of the chain refers to a particular breakpoint or
1290 	watchpoint at which we have stopped.  (We may have stopped for
1291 	several reasons concurrently.)
1292 
1293 	Each element of the chain has valid next, breakpoint_at,
1294 	commands, FIXME??? fields.
1295 
1296  */
1297 
1298 bpstat
1299 bpstat_stop_status (pc, not_a_breakpoint)
1300      CORE_ADDR *pc;
1301      int not_a_breakpoint;
1302 {
1303   register struct breakpoint *b, *temp;
1304   CORE_ADDR bp_addr;
1305   /* True if we've hit a breakpoint (as opposed to a watchpoint).  */
1306   int real_breakpoint = 0;
1307   /* Root of the chain of bpstat's */
1308   struct bpstats root_bs[1];
1309   /* Pointer to the last thing in the chain currently.  */
1310   bpstat bs = root_bs;
1311   static char message1[] =
1312             "Error evaluating expression for watchpoint %d\n";
1313   char message[sizeof (message1) + 30 /* slop */];
1314 
1315   /* Get the address where the breakpoint would have been.  */
1316   bp_addr = *pc - DECR_PC_AFTER_BREAK;
1317 
1318   ALL_BREAKPOINTS_SAFE (b, temp)
1319     {
1320       if (b->enable == disabled
1321 	  || b->enable == shlib_disabled)
1322 	continue;
1323 
1324       if (b->type != bp_watchpoint
1325 	  && b->type != bp_hardware_watchpoint
1326           && b->type != bp_read_watchpoint
1327           && b->type != bp_access_watchpoint
1328 	  && b->type != bp_hardware_breakpoint
1329 	  && b->address != bp_addr)
1330 	continue;
1331 
1332       if (b->type == bp_hardware_breakpoint
1333 	  && b->address != (bp_addr - DECR_PC_AFTER_HW_BREAK))
1334 	continue;
1335 
1336       if (b->type != bp_watchpoint
1337 	  && b->type != bp_hardware_watchpoint
1338 	  && b->type != bp_read_watchpoint
1339 	  && b->type != bp_access_watchpoint
1340 	  && not_a_breakpoint)
1341 	continue;
1342 
1343       /* Come here if it's a watchpoint, or if the break address matches */
1344 
1345       ++(b->hit_count);
1346 
1347       bs = bpstat_alloc (b, bs);	/* Alloc a bpstat to explain stop */
1348 
1349       bs->stop = 1;
1350       bs->print = 1;
1351 
1352       sprintf (message, message1, b->number);
1353       if (b->type == bp_watchpoint || b->type == bp_hardware_watchpoint)
1354 	{
1355 	  switch (catch_errors (watchpoint_check, (char *) bs, message,
1356 				RETURN_MASK_ALL))
1357 	    {
1358 	    case WP_DELETED:
1359 	      /* We've already printed what needs to be printed.  */
1360 	      bs->print_it = print_it_done;
1361 	      /* Stop.  */
1362 	      break;
1363 	    case WP_VALUE_CHANGED:
1364 	      /* Stop.  */
1365 	      break;
1366 	    case WP_VALUE_NOT_CHANGED:
1367 	      /* Don't stop.  */
1368 	      bs->print_it = print_it_noop;
1369 	      bs->stop = 0;
1370 	      continue;
1371 	    default:
1372 	      /* Can't happen.  */
1373 	      /* FALLTHROUGH */
1374 	    case 0:
1375 	      /* Error from catch_errors.  */
1376 	      printf_filtered ("Watchpoint %d deleted.\n", b->number);
1377 	      if (b->related_breakpoint)
1378 		{
1379 		  b->related_breakpoint->enable = disable;
1380 		  b->related_breakpoint->disposition = del_at_next_stop;
1381 		}
1382 	      b->enable = disable;
1383 	      b->disposition = del_at_next_stop;
1384 	      /* We've already printed what needs to be printed.  */
1385 	      bs->print_it = print_it_done;
1386 
1387 	      /* Stop.  */
1388 	      break;
1389 	    }
1390 	}
1391       else if (b->type == bp_read_watchpoint || b->type == bp_access_watchpoint)
1392         {
1393 	  CORE_ADDR addr;
1394 	  value_ptr v;
1395           int found = 0;
1396 
1397 	  addr = target_stopped_data_address();
1398 	  if (addr == 0) continue;
1399           for (v = b->val_chain; v; v = v->next)
1400             {
1401               if (v->lval == lval_memory)
1402                 {
1403                   CORE_ADDR vaddr;
1404 
1405                   vaddr = VALUE_ADDRESS (v) + VALUE_OFFSET (v);
1406 	          if (addr == vaddr)
1407 	            found = 1;
1408                 }
1409             }
1410 	  if (found)
1411 	    switch (catch_errors (watchpoint_check, (char *) bs, message,
1412 			 RETURN_MASK_ALL))
1413    	      {
1414                 case WP_DELETED:
1415                   /* We've already printed what needs to be printed.  */
1416                   bs->print_it = print_it_done;
1417                   /* Stop.  */
1418                   break;
1419                 case WP_VALUE_CHANGED:
1420                 case WP_VALUE_NOT_CHANGED:
1421                   /* Stop.  */
1422                   break;
1423                 default:
1424                   /* Can't happen.  */
1425                 case 0:
1426                   /* Error from catch_errors.  */
1427                   printf_filtered ("Watchpoint %d deleted.\n", b->number);
1428 		  if (b->related_breakpoint)
1429 		    {
1430 		      b->related_breakpoint->enable = disable;
1431 		      b->related_breakpoint->disposition = del_at_next_stop;
1432 		    }
1433 		  b->enable = disable;
1434 		  b->disposition = del_at_next_stop;
1435                   /* We've already printed what needs to be printed.  */
1436                   bs->print_it = print_it_done;
1437                   break;
1438 	      }
1439         }
1440       else if (DECR_PC_AFTER_BREAK != 0 || must_shift_inst_regs)
1441 	real_breakpoint = 1;
1442 
1443       if (b->frame && b->frame != (get_current_frame ())->frame)
1444 	bs->stop = 0;
1445       else
1446 	{
1447 	  int value_is_zero = 0;
1448 
1449 	  if (b->cond)
1450 	    {
1451 	      /* Need to select the frame, with all that implies
1452 		 so that the conditions will have the right context.  */
1453 	      select_frame (get_current_frame (), 0);
1454 	      value_is_zero
1455 		= catch_errors (breakpoint_cond_eval, (char *)(b->cond),
1456 				"Error in testing breakpoint condition:\n",
1457 				RETURN_MASK_ALL);
1458 				/* FIXME-someday, should give breakpoint # */
1459 	      free_all_values ();
1460 	    }
1461 	  if (b->cond && value_is_zero)
1462 	    {
1463 	      bs->stop = 0;
1464 	    }
1465 	  else if (b->ignore_count > 0)
1466 	    {
1467 	      b->ignore_count--;
1468 	      bs->stop = 0;
1469 	    }
1470 	  else
1471 	    {
1472 	      /* We will stop here */
1473 	      if (b->disposition == disable)
1474 		b->enable = disabled;
1475 	      bs->commands = b->commands;
1476 	      if (b->silent)
1477 		bs->print = 0;
1478 	      if (bs->commands && STREQ ("silent", bs->commands->line))
1479 		{
1480 		  bs->commands = bs->commands->next;
1481 		  bs->print = 0;
1482 		}
1483 	    }
1484 	}
1485       /* Print nothing for this entry if we dont stop or if we dont print.  */
1486       if (bs->stop == 0 || bs->print == 0)
1487 	bs->print_it = print_it_noop;
1488     }
1489 
1490   bs->next = NULL;		/* Terminate the chain */
1491   bs = root_bs->next;		/* Re-grab the head of the chain */
1492 
1493   if ((DECR_PC_AFTER_BREAK != 0 || must_shift_inst_regs) && bs)
1494     {
1495       if (real_breakpoint)
1496 	{
1497 	  *pc = bp_addr;
1498 #if defined (SHIFT_INST_REGS)
1499 	  SHIFT_INST_REGS();
1500 #else /* No SHIFT_INST_REGS.  */
1501 	  write_pc (bp_addr);
1502 #endif /* No SHIFT_INST_REGS.  */
1503 	}
1504     }
1505 
1506   /* The value of a hardware watchpoint hasn't changed, but the
1507      intermediate memory locations we are watching may have.  */
1508   if (bs && ! bs->stop &&
1509       (bs->breakpoint_at->type == bp_hardware_watchpoint ||
1510        bs->breakpoint_at->type == bp_read_watchpoint ||
1511        bs->breakpoint_at->type == bp_access_watchpoint))
1512     {
1513       remove_breakpoints ();
1514       insert_breakpoints ();
1515     }
1516   return bs;
1517 }
1518 
1519 /* Tell what to do about this bpstat.  */
1520 struct bpstat_what
1521 bpstat_what (bs)
1522      bpstat bs;
1523 {
1524   /* Classify each bpstat as one of the following.  */
1525   enum class {
1526     /* This bpstat element has no effect on the main_action.  */
1527     no_effect = 0,
1528 
1529     /* There was a watchpoint, stop but don't print.  */
1530     wp_silent,
1531 
1532     /* There was a watchpoint, stop and print.  */
1533     wp_noisy,
1534 
1535     /* There was a breakpoint but we're not stopping.  */
1536     bp_nostop,
1537 
1538     /* There was a breakpoint, stop but don't print.  */
1539     bp_silent,
1540 
1541     /* There was a breakpoint, stop and print.  */
1542     bp_noisy,
1543 
1544     /* We hit the longjmp breakpoint.  */
1545     long_jump,
1546 
1547     /* We hit the longjmp_resume breakpoint.  */
1548     long_resume,
1549 
1550     /* We hit the step_resume breakpoint.  */
1551     step_resume,
1552 
1553     /* We hit the through_sigtramp breakpoint.  */
1554     through_sig,
1555 
1556     /* We hit the shared library event breakpoint.  */
1557     shlib_event,
1558 
1559     /* This is just used to count how many enums there are.  */
1560     class_last
1561     };
1562 
1563   /* Here is the table which drives this routine.  So that we can
1564      format it pretty, we define some abbreviations for the
1565      enum bpstat_what codes.  */
1566 #define kc BPSTAT_WHAT_KEEP_CHECKING
1567 #define ss BPSTAT_WHAT_STOP_SILENT
1568 #define sn BPSTAT_WHAT_STOP_NOISY
1569 #define sgl BPSTAT_WHAT_SINGLE
1570 #define slr BPSTAT_WHAT_SET_LONGJMP_RESUME
1571 #define clr BPSTAT_WHAT_CLEAR_LONGJMP_RESUME
1572 #define clrs BPSTAT_WHAT_CLEAR_LONGJMP_RESUME_SINGLE
1573 #define sr BPSTAT_WHAT_STEP_RESUME
1574 #define ts BPSTAT_WHAT_THROUGH_SIGTRAMP
1575 #define shl BPSTAT_WHAT_CHECK_SHLIBS
1576 
1577 /* "Can't happen."  Might want to print an error message.
1578    abort() is not out of the question, but chances are GDB is just
1579    a bit confused, not unusable.  */
1580 #define err BPSTAT_WHAT_STOP_NOISY
1581 
1582   /* Given an old action and a class, come up with a new action.  */
1583   /* One interesting property of this table is that wp_silent is the same
1584      as bp_silent and wp_noisy is the same as bp_noisy.  That is because
1585      after stopping, the check for whether to step over a breakpoint
1586      (BPSTAT_WHAT_SINGLE type stuff) is handled in proceed() without
1587      reference to how we stopped.  We retain separate wp_silent and bp_silent
1588      codes in case we want to change that someday.  */
1589 
1590   /* step_resume entries: a step resume breakpoint overrides another
1591      breakpoint of signal handling (see comment in wait_for_inferior
1592      at first IN_SIGTRAMP where we set the step_resume breakpoint).  */
1593   /* We handle the through_sigtramp_breakpoint the same way; having both
1594      one of those and a step_resume_breakpoint is probably very rare (?).  */
1595 
1596   static const enum bpstat_what_main_action
1597     table[(int)class_last][(int)BPSTAT_WHAT_LAST] =
1598       {
1599 	/*                              old action */
1600 	/*       kc   ss   sn   sgl   slr  clr   clrs  sr   ts  shl
1601 	 */
1602 /*no_effect*/	{kc,  ss,  sn,  sgl,  slr, clr,  clrs, sr,  ts, shl},
1603 /*wp_silent*/	{ss,  ss,  sn,  ss,   ss,  ss,   ss,   sr,  ts, shl},
1604 /*wp_noisy*/    {sn,  sn,  sn,  sn,   sn,  sn,   sn,   sr,  ts, shl},
1605 /*bp_nostop*/	{sgl, ss,  sn,  sgl,  slr, clrs, clrs, sr,  ts, shl},
1606 /*bp_silent*/	{ss,  ss,  sn,  ss,   ss,  ss,   ss,   sr,  ts, shl},
1607 /*bp_noisy*/    {sn,  sn,  sn,  sn,   sn,  sn,   sn,   sr,  ts, shl},
1608 /*long_jump*/	{slr, ss,  sn,  slr,  err, err,  err,  sr,  ts, shl},
1609 /*long_resume*/	{clr, ss,  sn,  clrs, err, err,  err,  sr,  ts, shl},
1610 /*step_resume*/	{sr,  sr,  sr,  sr,   sr,  sr,   sr,   sr,  ts, shl},
1611 /*through_sig*/ {ts,  ts,  ts,  ts,   ts,  ts,   ts,   ts,  ts, shl},
1612 /*shlib*/       {shl, shl, shl, shl,  shl, shl,  shl,  shl, ts, shl}
1613 	      };
1614 #undef kc
1615 #undef ss
1616 #undef sn
1617 #undef sgl
1618 #undef slr
1619 #undef clr
1620 #undef clrs
1621 #undef err
1622 #undef sr
1623 #undef ts
1624 #undef shl
1625   enum bpstat_what_main_action current_action = BPSTAT_WHAT_KEEP_CHECKING;
1626   struct bpstat_what retval;
1627 
1628   retval.call_dummy = 0;
1629   for (; bs != NULL; bs = bs->next)
1630     {
1631       enum class bs_class = no_effect;
1632       if (bs->breakpoint_at == NULL)
1633 	/* I suspect this can happen if it was a momentary breakpoint
1634 	   which has since been deleted.  */
1635 	continue;
1636       switch (bs->breakpoint_at->type)
1637 	{
1638 	case bp_breakpoint:
1639 	case bp_hardware_breakpoint:
1640 	case bp_until:
1641 	case bp_finish:
1642 	  if (bs->stop)
1643 	    {
1644 	      if (bs->print)
1645 		bs_class = bp_noisy;
1646 	      else
1647 		bs_class = bp_silent;
1648 	    }
1649 	  else
1650 	    bs_class = bp_nostop;
1651 	  break;
1652 	case bp_watchpoint:
1653 	case bp_hardware_watchpoint:
1654 	case bp_read_watchpoint:
1655 	case bp_access_watchpoint:
1656 	  if (bs->stop)
1657 	    {
1658 	      if (bs->print)
1659 		bs_class = wp_noisy;
1660 	      else
1661 		bs_class = wp_silent;
1662 	    }
1663 	  else
1664 	    /* There was a watchpoint, but we're not stopping.  This requires
1665 	       no further action.  */
1666 	    bs_class = no_effect;
1667 	  break;
1668 	case bp_longjmp:
1669 	  bs_class = long_jump;
1670 	  break;
1671 	case bp_longjmp_resume:
1672 	  bs_class = long_resume;
1673 	  break;
1674 	case bp_step_resume:
1675 	  if (bs->stop)
1676 	    {
1677 	      bs_class = step_resume;
1678 	    }
1679 	  else
1680 	    /* It is for the wrong frame.  */
1681 	    bs_class = bp_nostop;
1682 	  break;
1683 	case bp_through_sigtramp:
1684 	  bs_class = through_sig;
1685 	  break;
1686 	case bp_watchpoint_scope:
1687 	  bs_class = bp_nostop;
1688 	  break;
1689 	case bp_shlib_event:
1690 	  bs_class = shlib_event;
1691 	  break;
1692 	case bp_call_dummy:
1693 	  /* Make sure the action is stop (silent or noisy), so infrun.c
1694 	     pops the dummy frame.  */
1695 	  bs_class = bp_silent;
1696 	  retval.call_dummy = 1;
1697 	  break;
1698 	}
1699       current_action = table[(int)bs_class][(int)current_action];
1700     }
1701   retval.main_action = current_action;
1702   return retval;
1703 }
1704 
1705 /* Nonzero if we should step constantly (e.g. watchpoints on machines
1706    without hardware support).  This isn't related to a specific bpstat,
1707    just to things like whether watchpoints are set.  */
1708 
1709 int
1710 bpstat_should_step ()
1711 {
1712   struct breakpoint *b;
1713   ALL_BREAKPOINTS (b)
1714     if (b->enable == enabled && b->type == bp_watchpoint)
1715       return 1;
1716   return 0;
1717 }
1718 
1719 /* Print information on breakpoint number BNUM, or -1 if all.
1720    If WATCHPOINTS is zero, process only breakpoints; if WATCHPOINTS
1721    is nonzero, process only watchpoints.  */
1722 
1723 static void
1724 breakpoint_1 (bnum, allflag)
1725      int bnum;
1726      int allflag;
1727 {
1728   register struct breakpoint *b;
1729   register struct command_line *l;
1730   register struct symbol *sym;
1731   CORE_ADDR last_addr = (CORE_ADDR)-1;
1732   int found_a_breakpoint = 0;
1733   static char *bptypes[] = {"breakpoint", "hw breakpoint",
1734 		              "until", "finish", "watchpoint",
1735 			      "hw watchpoint", "read watchpoint",
1736 			      "acc watchpoint", "longjmp",
1737 			      "longjmp resume", "step resume",
1738 			      "sigtramp",
1739 			      "watchpoint scope", "call dummy",
1740 			      "shlib events" };
1741   static char *bpdisps[] = {"del", "dstp", "dis", "keep"};
1742   static char bpenables[] = "nyn";
1743   char wrap_indent[80];
1744 
1745   ALL_BREAKPOINTS (b)
1746     if (bnum == -1
1747 	|| bnum == b->number)
1748       {
1749 /*  We only print out user settable breakpoints unless the allflag is set. */
1750 	if (!allflag
1751 	    && b->type != bp_breakpoint
1752 	    && b->type != bp_hardware_breakpoint
1753 	    && b->type != bp_watchpoint
1754 	    && b->type != bp_read_watchpoint
1755 	    && b->type != bp_access_watchpoint
1756 	    && b->type != bp_hardware_watchpoint)
1757 	  continue;
1758 
1759 	if (!found_a_breakpoint++)
1760 	  {
1761             annotate_breakpoints_headers ();
1762 
1763             annotate_field (0);
1764             printf_filtered ("Num ");
1765             annotate_field (1);
1766             printf_filtered ("Type           ");
1767             annotate_field (2);
1768             printf_filtered ("Disp ");
1769             annotate_field (3);
1770             printf_filtered ("Enb ");
1771             if (addressprint)
1772               {
1773                 annotate_field (4);
1774                 printf_filtered ("Address    ");
1775               }
1776             annotate_field (5);
1777             printf_filtered ("What\n");
1778 
1779             annotate_breakpoints_table ();
1780           }
1781 
1782         annotate_record ();
1783         annotate_field (0);
1784         printf_filtered ("%-3d ", b->number);
1785         annotate_field (1);
1786         printf_filtered ("%-14s ", bptypes[(int)b->type]);
1787         annotate_field (2);
1788         printf_filtered ("%-4s ", bpdisps[(int)b->disposition]);
1789         annotate_field (3);
1790 	printf_filtered ("%-3c ", bpenables[(int)b->enable]);
1791 
1792 	strcpy (wrap_indent, "                           ");
1793 	if (addressprint)
1794 	  strcat (wrap_indent, "           ");
1795 	switch (b->type)
1796 	  {
1797 	  case bp_watchpoint:
1798 	  case bp_hardware_watchpoint:
1799 	  case bp_read_watchpoint:
1800 	  case bp_access_watchpoint:
1801 	    /* Field 4, the address, is omitted (which makes the columns
1802 	       not line up too nicely with the headers, but the effect
1803 	       is relatively readable).  */
1804 	    annotate_field (5);
1805 	    print_expression (b->exp, gdb_stdout);
1806 	    break;
1807 
1808 	  case bp_breakpoint:
1809 	  case bp_hardware_breakpoint:
1810 	  case bp_until:
1811 	  case bp_finish:
1812 	  case bp_longjmp:
1813 	  case bp_longjmp_resume:
1814 	  case bp_step_resume:
1815 	  case bp_through_sigtramp:
1816 	  case bp_watchpoint_scope:
1817 	  case bp_call_dummy:
1818 	  case bp_shlib_event:
1819 	    if (addressprint)
1820 	      {
1821 	        annotate_field (4);
1822 		/* FIXME-32x64: need a print_address_numeric with
1823                    field width */
1824 		printf_filtered
1825 		  ("%s ",
1826 		   local_hex_string_custom
1827 		   ((unsigned long) b->address, "08l"));
1828 	      }
1829 
1830 	    annotate_field (5);
1831 
1832 	    last_addr = b->address;
1833 	    if (b->source_file)
1834 	      {
1835 		sym = find_pc_function (b->address);
1836 		if (sym)
1837 		  {
1838 		    fputs_filtered ("in ", gdb_stdout);
1839 		    fputs_filtered (SYMBOL_SOURCE_NAME (sym), gdb_stdout);
1840 		    wrap_here (wrap_indent);
1841 		    fputs_filtered (" at ", gdb_stdout);
1842 		  }
1843 		fputs_filtered (b->source_file, gdb_stdout);
1844 		printf_filtered (":%d", b->line_number);
1845 	      }
1846 	    else
1847 	      print_address_symbolic (b->address, gdb_stdout, demangle, " ");
1848 	    break;
1849 	  }
1850 
1851 	printf_filtered ("\n");
1852 
1853 	if (b->frame)
1854 	  {
1855             annotate_field (6);
1856 
1857 	    printf_filtered ("\tstop only in stack frame at ");
1858 	    print_address_numeric (b->frame, 1, gdb_stdout);
1859 	    printf_filtered ("\n");
1860 	  }
1861 
1862 	if (b->cond)
1863 	  {
1864             annotate_field (7);
1865 
1866 	    printf_filtered ("\tstop only if ");
1867 	    print_expression (b->cond, gdb_stdout);
1868 	    printf_filtered ("\n");
1869 	  }
1870 
1871 	if (b->thread != -1)
1872 	  {
1873 	    /* FIXME should make an annotation for this */
1874 	    printf_filtered ("\tstop only in thread %d\n", b->thread);
1875 	  }
1876 
1877         if (show_breakpoint_hit_counts && b->hit_count)
1878 	  {
1879 	    /* FIXME should make an annotation for this */
1880 
1881 	    printf_filtered ("\tbreakpoint already hit %d time%s\n",
1882 			     b->hit_count, (b->hit_count == 1 ? "" : "s"));
1883 	  }
1884 
1885 	if (b->ignore_count)
1886 	  {
1887             annotate_field (8);
1888 
1889 	    printf_filtered ("\tignore next %d hits\n", b->ignore_count);
1890 	  }
1891 
1892 	if ((l = b->commands))
1893 	  {
1894             annotate_field (9);
1895 
1896 	    while (l)
1897 	      {
1898 		print_command_line (l, 4);
1899 		l = l->next;
1900 	      }
1901 	  }
1902       }
1903 
1904   if (!found_a_breakpoint)
1905     {
1906       if (bnum == -1)
1907         printf_filtered ("No breakpoints or watchpoints.\n");
1908       else
1909         printf_filtered ("No breakpoint or watchpoint number %d.\n", bnum);
1910     }
1911   else
1912     /* Compare against (CORE_ADDR)-1 in case some compiler decides
1913        that a comparison of an unsigned with -1 is always false.  */
1914     if (last_addr != (CORE_ADDR)-1)
1915       set_next_address (last_addr);
1916 
1917   annotate_breakpoints_table_end ();
1918 }
1919 
1920 /* ARGSUSED */
1921 static void
1922 breakpoints_info (bnum_exp, from_tty)
1923      char *bnum_exp;
1924      int from_tty;
1925 {
1926   int bnum = -1;
1927 
1928   if (bnum_exp)
1929     bnum = parse_and_eval_address (bnum_exp);
1930 
1931   breakpoint_1 (bnum, 0);
1932 }
1933 
1934 #if MAINTENANCE_CMDS
1935 
1936 /* ARGSUSED */
1937 static void
1938 maintenance_info_breakpoints (bnum_exp, from_tty)
1939      char *bnum_exp;
1940      int from_tty;
1941 {
1942   int bnum = -1;
1943 
1944   if (bnum_exp)
1945     bnum = parse_and_eval_address (bnum_exp);
1946 
1947   breakpoint_1 (bnum, 1);
1948 }
1949 
1950 #endif
1951 
1952 /* Print a message describing any breakpoints set at PC.  */
1953 
1954 static void
1955 describe_other_breakpoints (pc)
1956      register CORE_ADDR pc;
1957 {
1958   register int others = 0;
1959   register struct breakpoint *b;
1960 
1961   ALL_BREAKPOINTS (b)
1962     if (b->address == pc)
1963       others++;
1964   if (others > 0)
1965     {
1966       printf_filtered ("Note: breakpoint%s ", (others > 1) ? "s" : "");
1967       ALL_BREAKPOINTS (b)
1968 	if (b->address == pc)
1969 	  {
1970 	    others--;
1971 	    printf_filtered
1972 	      ("%d%s%s ",
1973 	       b->number,
1974 	       ((b->enable == disabled || b->enable == shlib_disabled)
1975 		? " (disabled)" : ""),
1976 	       (others > 1) ? "," : ((others == 1) ? " and" : ""));
1977 	  }
1978       printf_filtered ("also set at pc ");
1979       print_address_numeric (pc, 1, gdb_stdout);
1980       printf_filtered (".\n");
1981     }
1982 }
1983 
1984 /* Set the default place to put a breakpoint
1985    for the `break' command with no arguments.  */
1986 
1987 void
1988 set_default_breakpoint (valid, addr, symtab, line)
1989      int valid;
1990      CORE_ADDR addr;
1991      struct symtab *symtab;
1992      int line;
1993 {
1994   default_breakpoint_valid = valid;
1995   default_breakpoint_address = addr;
1996   default_breakpoint_symtab = symtab;
1997   default_breakpoint_line = line;
1998 }
1999 
2000 /* Rescan breakpoints at address ADDRESS,
2001    marking the first one as "first" and any others as "duplicates".
2002    This is so that the bpt instruction is only inserted once.  */
2003 
2004 static void
2005 check_duplicates (address)
2006      CORE_ADDR address;
2007 {
2008   register struct breakpoint *b;
2009   register int count = 0;
2010 
2011   if (address == 0)		/* Watchpoints are uninteresting */
2012     return;
2013 
2014   ALL_BREAKPOINTS (b)
2015     if (b->enable != disabled
2016 	&& b->enable != shlib_disabled
2017 	&& b->address == address)
2018       {
2019 	count++;
2020 	b->duplicate = count > 1;
2021       }
2022 }
2023 
2024 /* Low level routine to set a breakpoint.
2025    Takes as args the three things that every breakpoint must have.
2026    Returns the breakpoint object so caller can set other things.
2027    Does not set the breakpoint number!
2028    Does not print anything.
2029 
2030    ==> This routine should not be called if there is a chance of later
2031    error(); otherwise it leaves a bogus breakpoint on the chain.  Validate
2032    your arguments BEFORE calling this routine!  */
2033 
2034 static struct breakpoint *
2035 set_raw_breakpoint (sal)
2036      struct symtab_and_line sal;
2037 {
2038   register struct breakpoint *b, *b1;
2039 
2040   b = (struct breakpoint *) xmalloc (sizeof (struct breakpoint));
2041   memset (b, 0, sizeof (*b));
2042   b->address = sal.pc;
2043   if (sal.symtab == NULL)
2044     b->source_file = NULL;
2045   else
2046     b->source_file = savestring (sal.symtab->filename,
2047 				 strlen (sal.symtab->filename));
2048   b->language = current_language->la_language;
2049   b->input_radix = input_radix;
2050   b->thread = -1;
2051   b->line_number = sal.line;
2052   b->enable = enabled;
2053   b->next = 0;
2054   b->silent = 0;
2055   b->ignore_count = 0;
2056   b->commands = NULL;
2057   b->frame = 0;
2058 
2059   /* Add this breakpoint to the end of the chain
2060      so that a list of breakpoints will come out in order
2061      of increasing numbers.  */
2062 
2063   b1 = breakpoint_chain;
2064   if (b1 == 0)
2065     breakpoint_chain = b;
2066   else
2067     {
2068       while (b1->next)
2069 	b1 = b1->next;
2070       b1->next = b;
2071     }
2072 
2073   check_duplicates (sal.pc);
2074   breakpoints_changed ();
2075 
2076   return b;
2077 }
2078 
2079 #ifdef GET_LONGJMP_TARGET
2080 
2081 static void
2082 create_longjmp_breakpoint (func_name)
2083      char *func_name;
2084 {
2085   struct symtab_and_line sal;
2086   struct breakpoint *b;
2087 
2088   if (func_name != NULL)
2089     {
2090       struct minimal_symbol *m;
2091 
2092       m = lookup_minimal_symbol_text (func_name, NULL, (struct objfile *)NULL);
2093       if (m)
2094 	sal.pc = SYMBOL_VALUE_ADDRESS (m);
2095       else
2096 	return;
2097     }
2098   else
2099     sal.pc = 0;
2100 
2101   sal.symtab = NULL;
2102   sal.line = 0;
2103 
2104   b = set_raw_breakpoint (sal);
2105   if (!b) return;
2106 
2107   b->type = func_name != NULL ? bp_longjmp : bp_longjmp_resume;
2108   b->disposition = donttouch;
2109   b->enable = disabled;
2110   b->silent = 1;
2111   if (func_name)
2112     b->addr_string = strsave(func_name);
2113   b->number = internal_breakpoint_number--;
2114 }
2115 
2116 #endif	/* #ifdef GET_LONGJMP_TARGET */
2117 
2118 /* Call this routine when stepping and nexting to enable a breakpoint if we do
2119    a longjmp().  When we hit that breakpoint, call
2120    set_longjmp_resume_breakpoint() to figure out where we are going. */
2121 
2122 void
2123 enable_longjmp_breakpoint()
2124 {
2125   register struct breakpoint *b;
2126 
2127   ALL_BREAKPOINTS (b)
2128     if (b->type == bp_longjmp)
2129       {
2130 	b->enable = enabled;
2131 	check_duplicates (b->address);
2132       }
2133 }
2134 
2135 void
2136 disable_longjmp_breakpoint()
2137 {
2138   register struct breakpoint *b;
2139 
2140   ALL_BREAKPOINTS (b)
2141     if (   b->type == bp_longjmp
2142 	|| b->type == bp_longjmp_resume)
2143       {
2144 	b->enable = disabled;
2145 	check_duplicates (b->address);
2146       }
2147 }
2148 
2149 #ifdef SOLIB_ADD
2150 void
2151 remove_solib_event_breakpoints ()
2152 {
2153   register struct breakpoint *b, *temp;
2154 
2155   ALL_BREAKPOINTS_SAFE (b, temp)
2156     if (b->type == bp_shlib_event)
2157       delete_breakpoint (b);
2158 }
2159 
2160 void
2161 create_solib_event_breakpoint (address)
2162      CORE_ADDR address;
2163 {
2164   struct breakpoint *b;
2165   struct symtab_and_line sal;
2166 
2167   sal.pc = address;
2168   sal.symtab = NULL;
2169   sal.line = 0;
2170   b = set_raw_breakpoint (sal);
2171   b->number = internal_breakpoint_number--;
2172   b->disposition = donttouch;
2173   b->type = bp_shlib_event;
2174 }
2175 
2176 /* Try to reenable any breakpoints in shared libraries.  */
2177 void
2178 re_enable_breakpoints_in_shlibs ()
2179 {
2180   struct breakpoint *b;
2181 
2182   ALL_BREAKPOINTS (b)
2183     if (b->enable == shlib_disabled)
2184       {
2185 	char buf[1];
2186 
2187 	/* Do not reenable the breakpoint if the shared library
2188 	   is still not mapped in.  */
2189 	if (target_read_memory (b->address, buf, 1) == 0)
2190 	  b->enable = enabled;
2191       }
2192 }
2193 
2194 #endif
2195 
2196 static int
2197 hw_breakpoint_used_count()
2198 {
2199   register struct breakpoint *b;
2200   int i = 0;
2201 
2202   ALL_BREAKPOINTS (b)
2203     {
2204       if (b->type == bp_hardware_breakpoint && b->enable == enabled)
2205 	i++;
2206     }
2207 
2208   return i;
2209 }
2210 
2211 static int
2212 hw_watchpoint_used_count(type, other_type_used)
2213     enum bptype type;
2214     int *other_type_used;
2215 {
2216   register struct breakpoint *b;
2217   int i = 0;
2218 
2219   *other_type_used = 0;
2220   ALL_BREAKPOINTS (b)
2221     {
2222       if (b->enable == enabled)
2223 	{
2224           if (b->type == type) i++;
2225           else if ((b->type == bp_hardware_watchpoint ||
2226 	       b->type == bp_read_watchpoint ||
2227 	       b->type == bp_access_watchpoint)
2228 	       && b->enable == enabled)
2229 	    *other_type_used = 1;
2230         }
2231     }
2232   return i;
2233 }
2234 
2235 /* Call this after hitting the longjmp() breakpoint.  Use this to set a new
2236    breakpoint at the target of the jmp_buf.
2237 
2238    FIXME - This ought to be done by setting a temporary breakpoint that gets
2239    deleted automatically...
2240 */
2241 
2242 void
2243 set_longjmp_resume_breakpoint(pc, frame)
2244      CORE_ADDR pc;
2245      struct frame_info *frame;
2246 {
2247   register struct breakpoint *b;
2248 
2249   ALL_BREAKPOINTS (b)
2250     if (b->type == bp_longjmp_resume)
2251       {
2252 	b->address = pc;
2253 	b->enable = enabled;
2254 	if (frame != NULL)
2255 	  b->frame = frame->frame;
2256 	else
2257 	  b->frame = 0;
2258 	check_duplicates (b->address);
2259 	return;
2260       }
2261 }
2262 
2263 /* Set a breakpoint that will evaporate an end of command
2264    at address specified by SAL.
2265    Restrict it to frame FRAME if FRAME is nonzero.  */
2266 
2267 struct breakpoint *
2268 set_momentary_breakpoint (sal, frame, type)
2269      struct symtab_and_line sal;
2270      struct frame_info *frame;
2271      enum bptype type;
2272 {
2273   register struct breakpoint *b;
2274   b = set_raw_breakpoint (sal);
2275   b->type = type;
2276   b->enable = enabled;
2277   b->disposition = donttouch;
2278   b->frame = (frame ? frame->frame : 0);
2279 
2280   /* If we're debugging a multi-threaded program, then we
2281      want momentary breakpoints to be active in only a
2282      single thread of control.  */
2283   if (in_thread_list (inferior_pid))
2284     b->thread = pid_to_thread_id (inferior_pid);
2285 
2286   return b;
2287 }
2288 
2289 
2290 /* Tell the user we have just set a breakpoint B.  */
2291 
2292 static void
2293 mention (b)
2294      struct breakpoint *b;
2295 {
2296   int say_where = 0;
2297 
2298   /* FIXME: This is misplaced; mention() is called by things (like hitting a
2299      watchpoint) other than breakpoint creation.  It should be possible to
2300      clean this up and at the same time replace the random calls to
2301      breakpoint_changed with this hook, as has already been done for
2302      delete_breakpoint_hook and so on.  */
2303   if (create_breakpoint_hook)
2304     create_breakpoint_hook (b);
2305 
2306   switch (b->type)
2307     {
2308     case bp_watchpoint:
2309       printf_filtered ("Watchpoint %d: ", b->number);
2310       print_expression (b->exp, gdb_stdout);
2311       break;
2312     case bp_hardware_watchpoint:
2313       printf_filtered ("Hardware watchpoint %d: ", b->number);
2314       print_expression (b->exp, gdb_stdout);
2315       break;
2316     case bp_read_watchpoint:
2317       printf_filtered ("Hardware read watchpoint %d: ", b->number);
2318       print_expression (b->exp, gdb_stdout);
2319       break;
2320     case bp_access_watchpoint:
2321       printf_filtered ("Hardware access(read/write) watchpoint %d: ",b->number);
2322       print_expression (b->exp, gdb_stdout);
2323       break;
2324     case bp_breakpoint:
2325       printf_filtered ("Breakpoint %d", b->number);
2326       say_where = 1;
2327       break;
2328     case bp_hardware_breakpoint:
2329       printf_filtered ("Hardware assisted breakpoint %d", b->number);
2330       say_where = 1;
2331       break;
2332     case bp_until:
2333     case bp_finish:
2334     case bp_longjmp:
2335     case bp_longjmp_resume:
2336     case bp_step_resume:
2337     case bp_through_sigtramp:
2338     case bp_call_dummy:
2339     case bp_watchpoint_scope:
2340     case bp_shlib_event:
2341       break;
2342     }
2343   if (say_where)
2344     {
2345       if (addressprint || b->source_file == NULL)
2346 	{
2347 	  printf_filtered (" at ");
2348 	  print_address_numeric (b->address, 1, gdb_stdout);
2349 	}
2350       if (b->source_file)
2351 	printf_filtered (": file %s, line %d.",
2352 			 b->source_file, b->line_number);
2353     }
2354   printf_filtered ("\n");
2355 }
2356 
2357 
2358 /* Set a breakpoint according to ARG (function, linenum or *address)
2359    flag: first bit  : 0 non-temporary, 1 temporary.
2360 	 second bit : 0 normal breakpoint, 1 hardware breakpoint. */
2361 
2362 static void
2363 break_command_1 (arg, flag, from_tty)
2364      char *arg;
2365      int flag, from_tty;
2366 {
2367   int tempflag, hardwareflag;
2368   struct symtabs_and_lines sals;
2369   struct symtab_and_line sal;
2370   register struct expression *cond = 0;
2371   register struct breakpoint *b;
2372 
2373   /* Pointers in arg to the start, and one past the end, of the condition.  */
2374   char *cond_start = NULL;
2375   char *cond_end = NULL;
2376   /* Pointers in arg to the start, and one past the end,
2377      of the address part.  */
2378   char *addr_start = NULL;
2379   char *addr_end = NULL;
2380   struct cleanup *old_chain;
2381   struct cleanup *canonical_strings_chain = NULL;
2382   char **canonical = (char **)NULL;
2383   int i;
2384   int thread;
2385 
2386   hardwareflag = flag & BP_HARDWAREFLAG;
2387   tempflag = flag & BP_TEMPFLAG;
2388 
2389   sals.sals = NULL;
2390   sals.nelts = 0;
2391 
2392   sal.line = sal.pc = sal.end = 0;
2393   sal.symtab = 0;
2394 
2395   /* If no arg given, or if first arg is 'if ', use the default breakpoint. */
2396 
2397   if (!arg || (arg[0] == 'i' && arg[1] == 'f'
2398 	       && (arg[2] == ' ' || arg[2] == '\t')))
2399     {
2400       if (default_breakpoint_valid)
2401 	{
2402 	  sals.sals = (struct symtab_and_line *)
2403 	    xmalloc (sizeof (struct symtab_and_line));
2404 	  sal.pc = default_breakpoint_address;
2405 	  sal.line = default_breakpoint_line;
2406 	  sal.symtab = default_breakpoint_symtab;
2407 	  sals.sals[0] = sal;
2408 	  sals.nelts = 1;
2409 	}
2410       else
2411 	error ("No default breakpoint address now.");
2412     }
2413   else
2414     {
2415       addr_start = arg;
2416 
2417       /* Force almost all breakpoints to be in terms of the
2418 	 current_source_symtab (which is decode_line_1's default).  This
2419 	 should produce the results we want almost all of the time while
2420 	 leaving default_breakpoint_* alone.  */
2421       if (default_breakpoint_valid
2422 	  && (!current_source_symtab
2423 	      || (arg && (*arg == '+' || *arg == '-'))))
2424 	sals = decode_line_1 (&arg, 1, default_breakpoint_symtab,
2425 			      default_breakpoint_line, &canonical);
2426       else
2427 	sals = decode_line_1 (&arg, 1, (struct symtab *)NULL, 0, &canonical);
2428 
2429       addr_end = arg;
2430     }
2431 
2432   if (! sals.nelts)
2433     return;
2434 
2435   /* Make sure that all storage allocated in decode_line_1 gets freed in case
2436      the following `for' loop errors out.  */
2437   old_chain = make_cleanup (free, sals.sals);
2438   if (canonical != (char **)NULL)
2439     {
2440       make_cleanup (free, canonical);
2441       canonical_strings_chain = make_cleanup (null_cleanup, 0);
2442       for (i = 0; i < sals.nelts; i++)
2443 	{
2444 	  if (canonical[i] != NULL)
2445 	    make_cleanup (free, canonical[i]);
2446 	}
2447     }
2448 
2449   thread = -1;			/* No specific thread yet */
2450 
2451   /* Resolve all line numbers to PC's, and verify that conditions
2452      can be parsed, before setting any breakpoints.  */
2453   for (i = 0; i < sals.nelts; i++)
2454     {
2455       char *tok, *end_tok;
2456       int toklen;
2457 
2458       resolve_sal_pc (&sals.sals[i]);
2459 
2460       tok = arg;
2461 
2462       while (tok && *tok)
2463 	{
2464 	  while (*tok == ' ' || *tok == '\t')
2465 	    tok++;
2466 
2467 	  end_tok = tok;
2468 
2469 	  while (*end_tok != ' ' && *end_tok != '\t' && *end_tok != '\000')
2470 	    end_tok++;
2471 
2472 	  toklen = end_tok - tok;
2473 
2474 	  if (toklen >= 1 && strncmp (tok, "if", toklen) == 0)
2475 	    {
2476 	      tok = cond_start = end_tok + 1;
2477 	      cond = parse_exp_1 (&tok, block_for_pc (sals.sals[i].pc), 0);
2478 	      cond_end = tok;
2479 	    }
2480 	  else if (toklen >= 1 && strncmp (tok, "thread", toklen) == 0)
2481 	    {
2482 	      char *tmptok;
2483 
2484 	      tok = end_tok + 1;
2485 	      tmptok = tok;
2486 	      thread = strtol (tok, &tok, 0);
2487 	      if (tok == tmptok)
2488 		error ("Junk after thread keyword.");
2489 	      if (!valid_thread_id (thread))
2490 		error ("Unknown thread %d\n", thread);
2491 	    }
2492 	  else
2493 	    error ("Junk at end of arguments.");
2494 	}
2495     }
2496   if (hardwareflag)
2497     {
2498       int i, target_resources_ok;
2499 
2500       i = hw_breakpoint_used_count ();
2501       target_resources_ok = TARGET_CAN_USE_HARDWARE_WATCHPOINT (
2502 		bp_hardware_breakpoint, i + sals.nelts, 0);
2503       if (target_resources_ok == 0)
2504 	error ("No hardware breakpoint support in the target.");
2505       else if (target_resources_ok < 0)
2506         error ("Hardware breakpoints used exceeds limit.");
2507     }
2508 
2509   /* Remove the canonical strings from the cleanup, they are needed below.  */
2510   if (canonical != (char **)NULL)
2511     discard_cleanups (canonical_strings_chain);
2512 
2513   /* Now set all the breakpoints.  */
2514   for (i = 0; i < sals.nelts; i++)
2515     {
2516       sal = sals.sals[i];
2517 
2518       if (from_tty)
2519 	describe_other_breakpoints (sal.pc);
2520 
2521       b = set_raw_breakpoint (sal);
2522       set_breakpoint_count (breakpoint_count + 1);
2523       b->number = breakpoint_count;
2524       b->type = hardwareflag ? bp_hardware_breakpoint : bp_breakpoint;
2525       b->cond = cond;
2526       b->thread = thread;
2527 
2528       /* If a canonical line spec is needed use that instead of the
2529 	 command string.  */
2530       if (canonical != (char **)NULL && canonical[i] != NULL)
2531 	b->addr_string = canonical[i];
2532       else if (addr_start)
2533 	b->addr_string = savestring (addr_start, addr_end - addr_start);
2534       if (cond_start)
2535 	b->cond_string = savestring (cond_start, cond_end - cond_start);
2536 
2537       b->enable = enabled;
2538       b->disposition = tempflag ? del : donttouch;
2539 
2540       mention (b);
2541     }
2542 
2543   if (sals.nelts > 1)
2544     {
2545       printf_filtered ("Multiple breakpoints were set.\n");
2546       printf_filtered ("Use the \"delete\" command to delete unwanted breakpoints.\n");
2547     }
2548   do_cleanups (old_chain);
2549 }
2550 
2551 /* Helper function for break_command_1 and disassemble_command.  */
2552 
2553 void
2554 resolve_sal_pc (sal)
2555      struct symtab_and_line *sal;
2556 {
2557   CORE_ADDR pc;
2558 
2559   if (sal->pc == 0 && sal->symtab != 0)
2560     {
2561       pc = find_line_pc (sal->symtab, sal->line);
2562       if (pc == 0)
2563 	error ("No line %d in file \"%s\".",
2564 	       sal->line, sal->symtab->filename);
2565       sal->pc = pc;
2566     }
2567 }
2568 
2569 void
2570 break_command (arg, from_tty)
2571      char *arg;
2572      int from_tty;
2573 {
2574   break_command_1 (arg, 0, from_tty);
2575 }
2576 
2577 static void
2578 tbreak_command (arg, from_tty)
2579      char *arg;
2580      int from_tty;
2581 {
2582   break_command_1 (arg, BP_TEMPFLAG, from_tty);
2583 }
2584 
2585 static void
2586 hbreak_command (arg, from_tty)
2587      char *arg;
2588      int from_tty;
2589 {
2590   break_command_1 (arg, BP_HARDWAREFLAG, from_tty);
2591 }
2592 
2593 static void
2594 thbreak_command (arg, from_tty)
2595      char *arg;
2596      int from_tty;
2597 {
2598   break_command_1 (arg, (BP_TEMPFLAG | BP_HARDWAREFLAG), from_tty);
2599 }
2600 
2601 /* ARGSUSED */
2602 /* accessflag:  0: watch write, 1: watch read, 2: watch access(read or write)
2603 */
2604 static void
2605 watch_command_1 (arg, accessflag, from_tty)
2606      char *arg;
2607      int accessflag;
2608      int from_tty;
2609 {
2610   struct breakpoint *b;
2611   struct symtab_and_line sal;
2612   struct expression *exp;
2613   struct block *exp_valid_block;
2614   struct value *val, *mark;
2615   struct frame_info *frame;
2616   struct frame_info *prev_frame = NULL;
2617   char *exp_start = NULL;
2618   char *exp_end = NULL;
2619   char *tok, *end_tok;
2620   int toklen;
2621   char *cond_start = NULL;
2622   char *cond_end = NULL;
2623   struct expression *cond = NULL;
2624   int i, other_type_used, target_resources_ok;
2625   enum bptype bp_type;
2626   int mem_cnt = 0;
2627 
2628   sal.pc = 0;
2629   sal.symtab = NULL;
2630   sal.line = 0;
2631 
2632   /* Parse arguments.  */
2633   innermost_block = NULL;
2634   exp_start = arg;
2635   exp = parse_exp_1 (&arg, 0, 0);
2636   exp_end = arg;
2637   exp_valid_block = innermost_block;
2638   mark = value_mark ();
2639   val = evaluate_expression (exp);
2640   release_value (val);
2641   if (VALUE_LAZY (val))
2642     value_fetch_lazy (val);
2643 
2644   tok = arg;
2645   while (*tok == ' ' || *tok == '\t')
2646     tok++;
2647   end_tok = tok;
2648 
2649   while (*end_tok != ' ' && *end_tok != '\t' && *end_tok != '\000')
2650     end_tok++;
2651 
2652   toklen = end_tok - tok;
2653   if (toklen >= 1 && strncmp (tok, "if", toklen) == 0)
2654     {
2655       tok = cond_start = end_tok + 1;
2656       cond = parse_exp_1 (&tok, 0, 0);
2657       cond_end = tok;
2658     }
2659   if (*tok)
2660     error("Junk at end of command.");
2661 
2662   if (accessflag == 1) bp_type = bp_read_watchpoint;
2663   else if (accessflag == 2) bp_type = bp_access_watchpoint;
2664   else bp_type = bp_hardware_watchpoint;
2665 
2666   mem_cnt = can_use_hardware_watchpoint (val);
2667   if (mem_cnt == 0 && bp_type != bp_hardware_watchpoint)
2668     error ("Expression cannot be implemented with read/access watchpoint.");
2669   if (mem_cnt != 0) {
2670     i = hw_watchpoint_used_count (bp_type, &other_type_used);
2671     target_resources_ok = TARGET_CAN_USE_HARDWARE_WATCHPOINT(
2672 		bp_type, i + mem_cnt, other_type_used);
2673     if (target_resources_ok == 0 && bp_type != bp_hardware_watchpoint)
2674       error ("Target does not have this type of hardware watchpoint support.");
2675     if (target_resources_ok < 0 && bp_type != bp_hardware_watchpoint)
2676       error ("Target resources have been allocated for other types of watchpoints.");
2677   }
2678 
2679   /* Now set up the breakpoint.  */
2680   b = set_raw_breakpoint (sal);
2681   set_breakpoint_count (breakpoint_count + 1);
2682   b->number = breakpoint_count;
2683   b->disposition = donttouch;
2684   b->exp = exp;
2685   b->exp_valid_block = exp_valid_block;
2686   b->exp_string = savestring (exp_start, exp_end - exp_start);
2687   b->val = val;
2688   b->cond = cond;
2689   if (cond_start)
2690     b->cond_string = savestring (cond_start, cond_end - cond_start);
2691   else
2692     b->cond_string = 0;
2693 
2694   frame = block_innermost_frame (exp_valid_block);
2695   if (frame)
2696     {
2697       prev_frame = get_prev_frame (frame);
2698       b->watchpoint_frame = frame->frame;
2699     }
2700   else
2701     b->watchpoint_frame = (CORE_ADDR)0;
2702 
2703   if (mem_cnt && target_resources_ok > 0)
2704     b->type = bp_type;
2705   else
2706     b->type = bp_watchpoint;
2707 
2708   /* If the expression is "local", then set up a "watchpoint scope"
2709      breakpoint at the point where we've left the scope of the watchpoint
2710      expression.  */
2711   if (innermost_block)
2712     {
2713       struct breakpoint *scope_breakpoint;
2714       struct symtab_and_line scope_sal;
2715 
2716       if (prev_frame)
2717 	{
2718 	  scope_sal.pc = get_frame_pc (prev_frame);
2719 	  scope_sal.symtab = NULL;
2720 	  scope_sal.line = 0;
2721 
2722 	  scope_breakpoint = set_raw_breakpoint (scope_sal);
2723 	  set_breakpoint_count (breakpoint_count + 1);
2724 	  scope_breakpoint->number = breakpoint_count;
2725 
2726 	  scope_breakpoint->type = bp_watchpoint_scope;
2727 	  scope_breakpoint->enable = enabled;
2728 
2729 	  /* Automatically delete the breakpoint when it hits.  */
2730 	  scope_breakpoint->disposition = del;
2731 
2732 	  /* Only break in the proper frame (help with recursion).  */
2733 	  scope_breakpoint->frame = prev_frame->frame;
2734 
2735 	  /* Set the address at which we will stop.  */
2736 	  scope_breakpoint->address = get_frame_pc (prev_frame);
2737 
2738 	  /* The scope breakpoint is related to the watchpoint.  We
2739 	     will need to act on them together.  */
2740 	  b->related_breakpoint = scope_breakpoint;
2741 	}
2742     }
2743   value_free_to_mark (mark);
2744   mention (b);
2745 }
2746 
2747 /* Return count of locations need to be watched and can be handled
2748    in hardware.  If the watchpoint can not be handled
2749    in hardware return zero.  */
2750 
2751 static int
2752 can_use_hardware_watchpoint (v)
2753      struct value *v;
2754 {
2755   int found_memory_cnt = 0;
2756 
2757   /* Make sure all the intermediate values are in memory.  Also make sure
2758      we found at least one memory expression.  Guards against watch 0x12345,
2759      which is meaningless, but could cause errors if one tries to insert a
2760      hardware watchpoint for the constant expression.  */
2761   for ( ; v; v = v->next)
2762     {
2763       if (v->lval == lval_memory)
2764 	{
2765 	  if (TYPE_LENGTH (VALUE_TYPE (v)) <= REGISTER_SIZE)
2766 	    found_memory_cnt++;
2767         }
2768       else if (v->lval != not_lval && v->modifiable == 0)
2769 	return 0;
2770     }
2771 
2772   /* The expression itself looks suitable for using a hardware
2773      watchpoint, but give the target machine a chance to reject it.  */
2774   return found_memory_cnt;
2775 }
2776 
2777 static void watch_command (arg, from_tty)
2778      char *arg;
2779      int from_tty;
2780 {
2781   watch_command_1 (arg, 0, from_tty);
2782 }
2783 
2784 static void rwatch_command (arg, from_tty)
2785      char *arg;
2786      int from_tty;
2787 {
2788   watch_command_1 (arg, 1, from_tty);
2789 }
2790 
2791 static void awatch_command (arg, from_tty)
2792      char *arg;
2793      int from_tty;
2794 {
2795   watch_command_1 (arg, 2, from_tty);
2796 }
2797 
2798 
2799 /* Helper routine for the until_command routine in infcmd.c.  Here
2800    because it uses the mechanisms of breakpoints.  */
2801 
2802 /* ARGSUSED */
2803 void
2804 until_break_command (arg, from_tty)
2805      char *arg;
2806      int from_tty;
2807 {
2808   struct symtabs_and_lines sals;
2809   struct symtab_and_line sal;
2810   struct frame_info *prev_frame = get_prev_frame (selected_frame);
2811   struct breakpoint *breakpoint;
2812   struct cleanup *old_chain;
2813 
2814   clear_proceed_status ();
2815 
2816   /* Set a breakpoint where the user wants it and at return from
2817      this function */
2818 
2819   if (default_breakpoint_valid)
2820     sals = decode_line_1 (&arg, 1, default_breakpoint_symtab,
2821 			  default_breakpoint_line, (char ***)NULL);
2822   else
2823     sals = decode_line_1 (&arg, 1, (struct symtab *)NULL, 0, (char ***)NULL);
2824 
2825   if (sals.nelts != 1)
2826     error ("Couldn't get information on specified line.");
2827 
2828   sal = sals.sals[0];
2829   free ((PTR)sals.sals);		/* malloc'd, so freed */
2830 
2831   if (*arg)
2832     error ("Junk at end of arguments.");
2833 
2834   resolve_sal_pc (&sal);
2835 
2836   breakpoint = set_momentary_breakpoint (sal, selected_frame, bp_until);
2837 
2838   old_chain = make_cleanup(delete_breakpoint, breakpoint);
2839 
2840   /* Keep within the current frame */
2841 
2842   if (prev_frame)
2843     {
2844       sal = find_pc_line (prev_frame->pc, 0);
2845       sal.pc = prev_frame->pc;
2846       breakpoint = set_momentary_breakpoint (sal, prev_frame, bp_until);
2847       make_cleanup(delete_breakpoint, breakpoint);
2848     }
2849 
2850   proceed (-1, TARGET_SIGNAL_DEFAULT, 0);
2851   do_cleanups(old_chain);
2852 }
2853 
2854 #if 0
2855 /* These aren't used; I don't konw what they were for.  */
2856 /* Set a breakpoint at the catch clause for NAME.  */
2857 static int
2858 catch_breakpoint (name)
2859      char *name;
2860 {
2861 }
2862 
2863 static int
2864 disable_catch_breakpoint ()
2865 {
2866 }
2867 
2868 static int
2869 delete_catch_breakpoint ()
2870 {
2871 }
2872 
2873 static int
2874 enable_catch_breakpoint ()
2875 {
2876 }
2877 #endif /* 0 */
2878 
2879 struct sal_chain
2880 {
2881   struct sal_chain *next;
2882   struct symtab_and_line sal;
2883 };
2884 
2885 #if 0
2886 /* This isn't used; I don't know what it was for.  */
2887 /* For each catch clause identified in ARGS, run FUNCTION
2888    with that clause as an argument.  */
2889 static struct symtabs_and_lines
2890 map_catch_names (args, function)
2891      char *args;
2892      int (*function)();
2893 {
2894   register char *p = args;
2895   register char *p1;
2896   struct symtabs_and_lines sals;
2897 #if 0
2898   struct sal_chain *sal_chain = 0;
2899 #endif
2900 
2901   if (p == 0)
2902     error_no_arg ("one or more catch names");
2903 
2904   sals.nelts = 0;
2905   sals.sals = NULL;
2906 
2907   while (*p)
2908     {
2909       p1 = p;
2910       /* Don't swallow conditional part.  */
2911       if (p1[0] == 'i' && p1[1] == 'f'
2912 	  && (p1[2] == ' ' || p1[2] == '\t'))
2913 	break;
2914 
2915       if (isalpha (*p1))
2916 	{
2917 	  p1++;
2918 	  while (isalnum (*p1) || *p1 == '_' || *p1 == '$')
2919 	    p1++;
2920 	}
2921 
2922       if (*p1 && *p1 != ' ' && *p1 != '\t')
2923 	error ("Arguments must be catch names.");
2924 
2925       *p1 = 0;
2926 #if 0
2927       if (function (p))
2928 	{
2929 	  struct sal_chain *next
2930 	    = (struct sal_chain *)alloca (sizeof (struct sal_chain));
2931 	  next->next = sal_chain;
2932 	  next->sal = get_catch_sal (p);
2933 	  sal_chain = next;
2934 	  goto win;
2935 	}
2936 #endif
2937       printf_unfiltered ("No catch clause for exception %s.\n", p);
2938 #if 0
2939     win:
2940 #endif
2941       p = p1;
2942       while (*p == ' ' || *p == '\t') p++;
2943     }
2944 }
2945 #endif /* 0 */
2946 
2947 /* This shares a lot of code with `print_frame_label_vars' from stack.c.  */
2948 
2949 static struct symtabs_and_lines
2950 get_catch_sals (this_level_only)
2951      int this_level_only;
2952 {
2953   register struct blockvector *bl;
2954   register struct block *block;
2955   int index, have_default = 0;
2956   CORE_ADDR pc;
2957   struct symtabs_and_lines sals;
2958   struct sal_chain *sal_chain = 0;
2959   char *blocks_searched;
2960 
2961   /* Not sure whether an error message is always the correct response,
2962      but it's better than a core dump.  */
2963   if (selected_frame == NULL)
2964     error ("No selected frame.");
2965   block = get_frame_block (selected_frame);
2966   pc = selected_frame->pc;
2967 
2968   sals.nelts = 0;
2969   sals.sals = NULL;
2970 
2971   if (block == 0)
2972     error ("No symbol table info available.\n");
2973 
2974   bl = blockvector_for_pc (BLOCK_END (block) - 4, &index);
2975   blocks_searched = (char *) alloca (BLOCKVECTOR_NBLOCKS (bl) * sizeof (char));
2976   memset (blocks_searched, 0, BLOCKVECTOR_NBLOCKS (bl) * sizeof (char));
2977 
2978   while (block != 0)
2979     {
2980       CORE_ADDR end = BLOCK_END (block) - 4;
2981       int last_index;
2982 
2983       if (bl != blockvector_for_pc (end, &index))
2984 	error ("blockvector blotch");
2985       if (BLOCKVECTOR_BLOCK (bl, index) != block)
2986 	error ("blockvector botch");
2987       last_index = BLOCKVECTOR_NBLOCKS (bl);
2988       index += 1;
2989 
2990       /* Don't print out blocks that have gone by.  */
2991       while (index < last_index
2992 	     && BLOCK_END (BLOCKVECTOR_BLOCK (bl, index)) < pc)
2993 	index++;
2994 
2995       while (index < last_index
2996 	     && BLOCK_END (BLOCKVECTOR_BLOCK (bl, index)) < end)
2997 	{
2998 	  if (blocks_searched[index] == 0)
2999 	    {
3000 	      struct block *b = BLOCKVECTOR_BLOCK (bl, index);
3001 	      int nsyms;
3002 	      register int i;
3003 	      register struct symbol *sym;
3004 
3005 	      nsyms = BLOCK_NSYMS (b);
3006 
3007 	      for (i = 0; i < nsyms; i++)
3008 		{
3009 		  sym = BLOCK_SYM (b, i);
3010 		  if (STREQ (SYMBOL_NAME (sym), "default"))
3011 		    {
3012 		      if (have_default)
3013 			continue;
3014 		      have_default = 1;
3015 		    }
3016 		  if (SYMBOL_CLASS (sym) == LOC_LABEL)
3017 		    {
3018 		      struct sal_chain *next = (struct sal_chain *)
3019 			alloca (sizeof (struct sal_chain));
3020 		      next->next = sal_chain;
3021 		      next->sal = find_pc_line (SYMBOL_VALUE_ADDRESS (sym), 0);
3022 		      sal_chain = next;
3023 		    }
3024 		}
3025 	      blocks_searched[index] = 1;
3026 	    }
3027 	  index++;
3028 	}
3029       if (have_default)
3030 	break;
3031       if (sal_chain && this_level_only)
3032 	break;
3033 
3034       /* After handling the function's top-level block, stop.
3035 	 Don't continue to its superblock, the block of
3036 	 per-file symbols.  */
3037       if (BLOCK_FUNCTION (block))
3038 	break;
3039       block = BLOCK_SUPERBLOCK (block);
3040     }
3041 
3042   if (sal_chain)
3043     {
3044       struct sal_chain *tmp_chain;
3045 
3046       /* Count the number of entries.  */
3047       for (index = 0, tmp_chain = sal_chain; tmp_chain;
3048 	   tmp_chain = tmp_chain->next)
3049 	index++;
3050 
3051       sals.nelts = index;
3052       sals.sals = (struct symtab_and_line *)
3053 	xmalloc (index * sizeof (struct symtab_and_line));
3054       for (index = 0; sal_chain; sal_chain = sal_chain->next, index++)
3055 	sals.sals[index] = sal_chain->sal;
3056     }
3057 
3058   return sals;
3059 }
3060 
3061 /* Commands to deal with catching exceptions.  */
3062 
3063 static void
3064 catch_command_1 (arg, tempflag, from_tty)
3065      char *arg;
3066      int tempflag;
3067      int from_tty;
3068 {
3069   /* First, translate ARG into something we can deal with in terms
3070      of breakpoints.  */
3071 
3072   struct symtabs_and_lines sals;
3073   struct symtab_and_line sal;
3074   register struct expression *cond = 0;
3075   register struct breakpoint *b;
3076   char *save_arg;
3077   int i;
3078 
3079   sal.line = sal.pc = sal.end = 0;
3080   sal.symtab = 0;
3081 
3082   /* If no arg given, or if first arg is 'if ', all active catch clauses
3083      are breakpointed. */
3084 
3085   if (!arg || (arg[0] == 'i' && arg[1] == 'f'
3086 	       && (arg[2] == ' ' || arg[2] == '\t')))
3087     {
3088       /* Grab all active catch clauses.  */
3089       sals = get_catch_sals (0);
3090     }
3091   else
3092     {
3093       /* Grab selected catch clauses.  */
3094       error ("catch NAME not implemented");
3095 #if 0
3096       /* This isn't used; I don't know what it was for.  */
3097       sals = map_catch_names (arg, catch_breakpoint);
3098 #endif
3099     }
3100 
3101   if (! sals.nelts)
3102     return;
3103 
3104   save_arg = arg;
3105   for (i = 0; i < sals.nelts; i++)
3106     {
3107       resolve_sal_pc (&sals.sals[i]);
3108 
3109       while (arg && *arg)
3110 	{
3111 	  if (arg[0] == 'i' && arg[1] == 'f'
3112 	      && (arg[2] == ' ' || arg[2] == '\t'))
3113 	    cond = parse_exp_1 ((arg += 2, &arg),
3114 				block_for_pc (sals.sals[i].pc), 0);
3115 	  else
3116 	    error ("Junk at end of arguments.");
3117 	}
3118       arg = save_arg;
3119     }
3120 
3121   for (i = 0; i < sals.nelts; i++)
3122     {
3123       sal = sals.sals[i];
3124 
3125       if (from_tty)
3126 	describe_other_breakpoints (sal.pc);
3127 
3128       b = set_raw_breakpoint (sal);
3129       set_breakpoint_count (breakpoint_count + 1);
3130       b->number = breakpoint_count;
3131       b->type = bp_breakpoint;
3132       b->cond = cond;
3133       b->enable = enabled;
3134       b->disposition = tempflag ? del : donttouch;
3135 
3136       mention (b);
3137     }
3138 
3139   if (sals.nelts > 1)
3140     {
3141       printf_unfiltered ("Multiple breakpoints were set.\n");
3142       printf_unfiltered ("Use the \"delete\" command to delete unwanted breakpoints.\n");
3143     }
3144   free ((PTR)sals.sals);
3145 }
3146 
3147 /* Used by the gui, could be made a worker for other things. */
3148 
3149 struct breakpoint *
3150 set_breakpoint_sal (sal)
3151 struct symtab_and_line sal;
3152 {
3153   struct breakpoint *b;
3154   b = set_raw_breakpoint (sal);
3155   set_breakpoint_count (breakpoint_count + 1);
3156   b->number = breakpoint_count;
3157   b->type = bp_breakpoint;
3158   b->cond = 0;
3159   b->thread = -1;
3160   return b;
3161 }
3162 
3163 #if 0
3164 /* These aren't used; I don't know what they were for.  */
3165 /* Disable breakpoints on all catch clauses described in ARGS.  */
3166 static void
3167 disable_catch (args)
3168      char *args;
3169 {
3170   /* Map the disable command to catch clauses described in ARGS.  */
3171 }
3172 
3173 /* Enable breakpoints on all catch clauses described in ARGS.  */
3174 static void
3175 enable_catch (args)
3176      char *args;
3177 {
3178   /* Map the disable command to catch clauses described in ARGS.  */
3179 }
3180 
3181 /* Delete breakpoints on all catch clauses in the active scope.  */
3182 static void
3183 delete_catch (args)
3184      char *args;
3185 {
3186   /* Map the delete command to catch clauses described in ARGS.  */
3187 }
3188 #endif /* 0 */
3189 
3190 static void
3191 catch_command (arg, from_tty)
3192      char *arg;
3193      int from_tty;
3194 {
3195   catch_command_1 (arg, 0, from_tty);
3196 }
3197 
3198 static void
3199 clear_command (arg, from_tty)
3200      char *arg;
3201      int from_tty;
3202 {
3203   register struct breakpoint *b, *b1;
3204   struct symtabs_and_lines sals;
3205   struct symtab_and_line sal;
3206   register struct breakpoint *found;
3207   int i;
3208 
3209   if (arg)
3210     {
3211       sals = decode_line_spec (arg, 1);
3212     }
3213   else
3214     {
3215       sals.sals = (struct symtab_and_line *) xmalloc (sizeof (struct symtab_and_line));
3216       sal.line = default_breakpoint_line;
3217       sal.symtab = default_breakpoint_symtab;
3218       sal.pc = 0;
3219       if (sal.symtab == 0)
3220 	error ("No source file specified.");
3221 
3222       sals.sals[0] = sal;
3223       sals.nelts = 1;
3224     }
3225 
3226   for (i = 0; i < sals.nelts; i++)
3227     {
3228       /* If exact pc given, clear bpts at that pc.
3229 	 But if sal.pc is zero, clear all bpts on specified line.  */
3230       sal = sals.sals[i];
3231       found = (struct breakpoint *) 0;
3232       while (breakpoint_chain
3233 	     && (sal.pc
3234 		 ? breakpoint_chain->address == sal.pc
3235 		 : (breakpoint_chain->source_file != NULL
3236 		    && sal.symtab != NULL
3237 		    && STREQ (breakpoint_chain->source_file,
3238 			      sal.symtab->filename)
3239 		    && breakpoint_chain->line_number == sal.line)))
3240 	{
3241 	  b1 = breakpoint_chain;
3242 	  breakpoint_chain = b1->next;
3243 	  b1->next = found;
3244 	  found = b1;
3245 	}
3246 
3247       ALL_BREAKPOINTS (b)
3248 	while (b->next
3249 	       && b->next->type != bp_watchpoint
3250 	       && b->next->type != bp_hardware_watchpoint
3251 	       && b->next->type != bp_read_watchpoint
3252 	       && b->next->type != bp_access_watchpoint
3253 	       && (sal.pc
3254 		   ? b->next->address == sal.pc
3255 		   : (b->next->source_file != NULL
3256 		      && sal.symtab != NULL
3257 		      && STREQ (b->next->source_file, sal.symtab->filename)
3258 		      && b->next->line_number == sal.line)))
3259 	  {
3260 	    b1 = b->next;
3261 	    b->next = b1->next;
3262 	    b1->next = found;
3263 	    found = b1;
3264 	  }
3265 
3266       if (found == 0)
3267 	{
3268 	  if (arg)
3269 	    error ("No breakpoint at %s.", arg);
3270 	  else
3271 	    error ("No breakpoint at this line.");
3272 	}
3273 
3274       if (found->next) from_tty = 1; /* Always report if deleted more than one */
3275       if (from_tty) printf_unfiltered ("Deleted breakpoint%s ", found->next ? "s" : "");
3276       breakpoints_changed ();
3277       while (found)
3278 	{
3279 	  if (from_tty) printf_unfiltered ("%d ", found->number);
3280 	  b1 = found->next;
3281 	  delete_breakpoint (found);
3282 	  found = b1;
3283 	}
3284       if (from_tty) putchar_unfiltered ('\n');
3285     }
3286   free ((PTR)sals.sals);
3287 }
3288 
3289 /* Delete breakpoint in BS if they are `delete' breakpoints and
3290    all breakpoints that are marked for deletion, whether hit or not.
3291    This is called after any breakpoint is hit, or after errors.  */
3292 
3293 void
3294 breakpoint_auto_delete (bs)
3295      bpstat bs;
3296 {
3297   struct breakpoint *b, *temp;
3298 
3299   for (; bs; bs = bs->next)
3300     if (bs->breakpoint_at && bs->breakpoint_at->disposition == del
3301 	&& bs->stop)
3302       delete_breakpoint (bs->breakpoint_at);
3303 
3304   ALL_BREAKPOINTS_SAFE (b, temp)
3305     {
3306       if (b->disposition == del_at_next_stop)
3307 	delete_breakpoint (b);
3308     }
3309 }
3310 
3311 /* Delete a breakpoint and clean up all traces of it in the data structures. */
3312 
3313 void
3314 delete_breakpoint (bpt)
3315      struct breakpoint *bpt;
3316 {
3317   register struct breakpoint *b;
3318   register bpstat bs;
3319 
3320   if (delete_breakpoint_hook)
3321     delete_breakpoint_hook (bpt);
3322 
3323   if (bpt->inserted)
3324     remove_breakpoint (bpt);
3325 
3326   if (breakpoint_chain == bpt)
3327     breakpoint_chain = bpt->next;
3328 
3329   ALL_BREAKPOINTS (b)
3330     if (b->next == bpt)
3331       {
3332 	b->next = bpt->next;
3333 	break;
3334       }
3335 
3336   check_duplicates (bpt->address);
3337   /* If this breakpoint was inserted, and there is another breakpoint
3338      at the same address, we need to insert the other breakpoint.  */
3339   if (bpt->inserted
3340       && bpt->type != bp_hardware_watchpoint
3341       && bpt->type != bp_read_watchpoint
3342       && bpt->type != bp_access_watchpoint)
3343     {
3344       ALL_BREAKPOINTS (b)
3345 	if (b->address == bpt->address
3346 	    && !b->duplicate
3347 	    && b->enable != disabled
3348 	    && b->enable != shlib_disabled)
3349 	  {
3350 	    int val;
3351 	    val = target_insert_breakpoint (b->address, b->shadow_contents);
3352 	    if (val != 0)
3353 	      {
3354 		target_terminal_ours_for_output ();
3355 		fprintf_unfiltered (gdb_stderr, "Cannot insert breakpoint %d:\n", b->number);
3356 		memory_error (val, b->address);	/* which bombs us out */
3357 	      }
3358 	    else
3359 	      b->inserted = 1;
3360 	  }
3361     }
3362 
3363   free_command_lines (&bpt->commands);
3364   if (bpt->cond)
3365     free (bpt->cond);
3366   if (bpt->cond_string != NULL)
3367     free (bpt->cond_string);
3368   if (bpt->addr_string != NULL)
3369     free (bpt->addr_string);
3370   if (bpt->exp != NULL)
3371     free (bpt->exp);
3372   if (bpt->exp_string != NULL)
3373     free (bpt->exp_string);
3374   if (bpt->val != NULL)
3375     value_free (bpt->val);
3376   if (bpt->source_file != NULL)
3377     free (bpt->source_file);
3378 
3379   /* Be sure no bpstat's are pointing at it after it's been freed.  */
3380   /* FIXME, how can we find all bpstat's?
3381      We just check stop_bpstat for now.  */
3382   for (bs = stop_bpstat; bs; bs = bs->next)
3383     if (bs->breakpoint_at == bpt)
3384       bs->breakpoint_at = NULL;
3385   free ((PTR)bpt);
3386 }
3387 
3388 static void
3389 delete_command (arg, from_tty)
3390      char *arg;
3391      int from_tty;
3392 {
3393 
3394   if (arg == 0)
3395     {
3396       /* Ask user only if there are some breakpoints to delete.  */
3397       if (!from_tty
3398 	  || (breakpoint_chain && query ("Delete all breakpoints? ")))
3399 	{
3400 	  /* No arg; clear all breakpoints.  */
3401 	  while (breakpoint_chain)
3402 	    delete_breakpoint (breakpoint_chain);
3403 	}
3404     }
3405   else
3406     map_breakpoint_numbers (arg, delete_breakpoint);
3407 }
3408 
3409 /* Reset a breakpoint given it's struct breakpoint * BINT.
3410    The value we return ends up being the return value from catch_errors.
3411    Unused in this case.  */
3412 
3413 static int
3414 breakpoint_re_set_one (bint)
3415      char *bint;
3416 {
3417   struct breakpoint *b = (struct breakpoint *)bint;  /* get past catch_errs */
3418   struct value *mark;
3419   int i;
3420   struct symtabs_and_lines sals;
3421   char *s;
3422   enum enable save_enable;
3423 
3424   switch (b->type)
3425     {
3426     case bp_breakpoint:
3427     case bp_hardware_breakpoint:
3428       if (b->addr_string == NULL)
3429 	{
3430 	  /* Anything without a string can't be re-set. */
3431 	  delete_breakpoint (b);
3432 	  return 0;
3433 	}
3434       /* In case we have a problem, disable this breakpoint.  We'll restore
3435 	 its status if we succeed.  */
3436       save_enable = b->enable;
3437       b->enable = disabled;
3438 
3439       set_language (b->language);
3440       input_radix = b->input_radix;
3441       s = b->addr_string;
3442       sals = decode_line_1 (&s, 1, (struct symtab *)NULL, 0, (char ***)NULL);
3443       for (i = 0; i < sals.nelts; i++)
3444 	{
3445 	  resolve_sal_pc (&sals.sals[i]);
3446 
3447 	  /* Reparse conditions, they might contain references to the
3448 	     old symtab.  */
3449 	  if (b->cond_string != NULL)
3450 	    {
3451 	      s = b->cond_string;
3452 	      if (b->cond)
3453 		free ((PTR)b->cond);
3454 	      b->cond = parse_exp_1 (&s, block_for_pc (sals.sals[i].pc), 0);
3455 	    }
3456 
3457 	  /* We need to re-set the breakpoint if the address changes...*/
3458 	  if (b->address != sals.sals[i].pc
3459 	      /* ...or new and old breakpoints both have source files, and
3460 		 the source file name or the line number changes...  */
3461 	      || (b->source_file != NULL
3462 		  && sals.sals[i].symtab != NULL
3463 		  && (!STREQ (b->source_file, sals.sals[i].symtab->filename)
3464 		      || b->line_number != sals.sals[i].line)
3465 		  )
3466 	      /* ...or we switch between having a source file and not having
3467 		 one.  */
3468 	      || ((b->source_file == NULL) != (sals.sals[i].symtab == NULL))
3469 	      )
3470 	    {
3471 	      if (b->source_file != NULL)
3472 		free (b->source_file);
3473 	      if (sals.sals[i].symtab == NULL)
3474 		b->source_file = NULL;
3475 	      else
3476 		b->source_file =
3477 		  savestring (sals.sals[i].symtab->filename,
3478 			      strlen (sals.sals[i].symtab->filename));
3479 	      b->line_number = sals.sals[i].line;
3480 	      b->address = sals.sals[i].pc;
3481 
3482 	      check_duplicates (b->address);
3483 
3484 	      mention (b);
3485 
3486 	      /* Might be better to do this just once per breakpoint_re_set,
3487 		 rather than once for every breakpoint.  */
3488 	      breakpoints_changed ();
3489 	    }
3490 	  b->enable = save_enable;	/* Restore it, this worked. */
3491 	}
3492       free ((PTR)sals.sals);
3493       break;
3494 
3495     case bp_watchpoint:
3496     case bp_hardware_watchpoint:
3497     case bp_read_watchpoint:
3498     case bp_access_watchpoint:
3499       innermost_block = NULL;
3500       /* The issue arises of what context to evaluate this in.  The same
3501 	 one as when it was set, but what does that mean when symbols have
3502 	 been re-read?  We could save the filename and functionname, but
3503 	 if the context is more local than that, the best we could do would
3504 	 be something like how many levels deep and which index at that
3505 	 particular level, but that's going to be less stable than filenames
3506 	 or functionnames.  */
3507       /* So for now, just use a global context.  */
3508       if (b->exp)
3509 	free ((PTR)b->exp);
3510       b->exp = parse_expression (b->exp_string);
3511       b->exp_valid_block = innermost_block;
3512       mark = value_mark ();
3513       if (b->val)
3514 	value_free (b->val);
3515       b->val = evaluate_expression (b->exp);
3516       release_value (b->val);
3517       if (VALUE_LAZY (b->val))
3518 	value_fetch_lazy (b->val);
3519 
3520       if (b->cond_string != NULL)
3521 	{
3522 	  s = b->cond_string;
3523 	  if (b->cond)
3524 	    free ((PTR)b->cond);
3525 	  b->cond = parse_exp_1 (&s, (struct block *)0, 0);
3526 	}
3527       if (b->enable == enabled)
3528 	mention (b);
3529       value_free_to_mark (mark);
3530       break;
3531 
3532     default:
3533       printf_filtered ("Deleting unknown breakpoint type %d\n", b->type);
3534       /* fall through */
3535     /* Delete longjmp breakpoints, they will be reset later by
3536        breakpoint_re_set.  */
3537     case bp_longjmp:
3538     case bp_longjmp_resume:
3539       delete_breakpoint (b);
3540       break;
3541 
3542     /* This breakpoint is special, it's set up when the inferior
3543        starts and we really don't want to touch it.  */
3544     case bp_shlib_event:
3545 
3546     /* Keep temporary breakpoints, which can be encountered when we step
3547        over a dlopen call and SOLIB_ADD is resetting the breakpoints.
3548        Otherwise these should have been blown away via the cleanup chain
3549        or by breakpoint_init_inferior when we rerun the executable.  */
3550     case bp_until:
3551     case bp_finish:
3552     case bp_watchpoint_scope:
3553     case bp_call_dummy:
3554     case bp_step_resume:
3555       break;
3556     }
3557 
3558   return 0;
3559 }
3560 
3561 /* Re-set all breakpoints after symbols have been re-loaded.  */
3562 void
3563 breakpoint_re_set ()
3564 {
3565   struct breakpoint *b, *temp;
3566   enum language save_language;
3567   int save_input_radix;
3568   static char message1[] = "Error in re-setting breakpoint %d:\n";
3569   char message[sizeof (message1) + 30 /* slop */];
3570 
3571   save_language = current_language->la_language;
3572   save_input_radix = input_radix;
3573   ALL_BREAKPOINTS_SAFE (b, temp)
3574     {
3575       sprintf (message, message1, b->number);	/* Format possible error msg */
3576       catch_errors (breakpoint_re_set_one, (char *) b, message,
3577 		    RETURN_MASK_ALL);
3578     }
3579   set_language (save_language);
3580   input_radix = save_input_radix;
3581 
3582 #ifdef GET_LONGJMP_TARGET
3583   create_longjmp_breakpoint ("longjmp");
3584   create_longjmp_breakpoint ("_longjmp");
3585   create_longjmp_breakpoint ("siglongjmp");
3586   create_longjmp_breakpoint (NULL);
3587 #endif
3588 
3589 #if 0
3590   /* Took this out (temporarily at least), since it produces an extra
3591      blank line at startup. This messes up the gdbtests. -PB */
3592   /* Blank line to finish off all those mention() messages we just printed.  */
3593   printf_filtered ("\n");
3594 #endif
3595 }
3596 
3597 /* Set ignore-count of breakpoint number BPTNUM to COUNT.
3598    If from_tty is nonzero, it prints a message to that effect,
3599    which ends with a period (no newline).  */
3600 
3601 void
3602 set_ignore_count (bptnum, count, from_tty)
3603      int bptnum, count, from_tty;
3604 {
3605   register struct breakpoint *b;
3606 
3607   if (count < 0)
3608     count = 0;
3609 
3610   ALL_BREAKPOINTS (b)
3611     if (b->number == bptnum)
3612       {
3613 	b->ignore_count = count;
3614 	if (!from_tty)
3615 	  return;
3616 	else if (count == 0)
3617 	  printf_filtered ("Will stop next time breakpoint %d is reached.",
3618 			   bptnum);
3619 	else if (count == 1)
3620 	  printf_filtered ("Will ignore next crossing of breakpoint %d.",
3621 			   bptnum);
3622 	else
3623 	  printf_filtered ("Will ignore next %d crossings of breakpoint %d.",
3624 		  count, bptnum);
3625 	breakpoints_changed ();
3626 	return;
3627       }
3628 
3629   error ("No breakpoint number %d.", bptnum);
3630 }
3631 
3632 /* Clear the ignore counts of all breakpoints.  */
3633 void
3634 breakpoint_clear_ignore_counts ()
3635 {
3636   struct breakpoint *b;
3637 
3638   ALL_BREAKPOINTS (b)
3639     b->ignore_count = 0;
3640 }
3641 
3642 /* Command to set ignore-count of breakpoint N to COUNT.  */
3643 
3644 static void
3645 ignore_command (args, from_tty)
3646      char *args;
3647      int from_tty;
3648 {
3649   char *p = args;
3650   register int num;
3651 
3652   if (p == 0)
3653     error_no_arg ("a breakpoint number");
3654 
3655   num = get_number (&p);
3656 
3657   if (*p == 0)
3658     error ("Second argument (specified ignore-count) is missing.");
3659 
3660   set_ignore_count (num,
3661 		    longest_to_int (value_as_long (parse_and_eval (p))),
3662 		    from_tty);
3663   printf_filtered ("\n");
3664   breakpoints_changed ();
3665 }
3666 
3667 /* Call FUNCTION on each of the breakpoints
3668    whose numbers are given in ARGS.  */
3669 
3670 static void
3671 map_breakpoint_numbers (args, function)
3672      char *args;
3673      void (*function) PARAMS ((struct breakpoint *));
3674 {
3675   register char *p = args;
3676   char *p1;
3677   register int num;
3678   register struct breakpoint *b;
3679 
3680   if (p == 0)
3681     error_no_arg ("one or more breakpoint numbers");
3682 
3683   while (*p)
3684     {
3685       p1 = p;
3686 
3687       num = get_number (&p1);
3688 
3689       ALL_BREAKPOINTS (b)
3690 	if (b->number == num)
3691 	  {
3692 	    struct breakpoint *related_breakpoint = b->related_breakpoint;
3693 	    function (b);
3694 	    if (related_breakpoint)
3695 	      function (related_breakpoint);
3696 	    goto win;
3697 	  }
3698       printf_unfiltered ("No breakpoint number %d.\n", num);
3699     win:
3700       p = p1;
3701     }
3702 }
3703 
3704 void
3705 disable_breakpoint (bpt)
3706      struct breakpoint *bpt;
3707 {
3708   /* Never disable a watchpoint scope breakpoint; we want to
3709      hit them when we leave scope so we can delete both the
3710      watchpoint and its scope breakpoint at that time.  */
3711   if (bpt->type == bp_watchpoint_scope)
3712     return;
3713 
3714   bpt->enable = disabled;
3715 
3716   check_duplicates (bpt->address);
3717 
3718   if (modify_breakpoint_hook)
3719     modify_breakpoint_hook (bpt);
3720 }
3721 
3722 /* ARGSUSED */
3723 static void
3724 disable_command (args, from_tty)
3725      char *args;
3726      int from_tty;
3727 {
3728   register struct breakpoint *bpt;
3729   if (args == 0)
3730     ALL_BREAKPOINTS (bpt)
3731       switch (bpt->type)
3732 	{
3733 	case bp_breakpoint:
3734         case bp_hardware_breakpoint:
3735         case bp_watchpoint:
3736         case bp_hardware_watchpoint:
3737         case bp_read_watchpoint:
3738         case bp_access_watchpoint:
3739 	  disable_breakpoint (bpt);
3740 	default:
3741 	  continue;
3742 	}
3743   else
3744     map_breakpoint_numbers (args, disable_breakpoint);
3745 }
3746 
3747 static void
3748 do_enable_breakpoint (bpt, disposition)
3749      struct breakpoint *bpt;
3750      enum bpdisp disposition;
3751 {
3752   struct frame_info *save_selected_frame = NULL;
3753   int save_selected_frame_level = -1;
3754   int target_resources_ok, other_type_used;
3755   struct value *mark;
3756 
3757   if (bpt->type == bp_hardware_breakpoint)
3758     {
3759       int i;
3760       i = hw_breakpoint_used_count();
3761       target_resources_ok = TARGET_CAN_USE_HARDWARE_WATCHPOINT(
3762 		bp_hardware_breakpoint, i+1, 0);
3763       if (target_resources_ok == 0)
3764         error ("No hardware breakpoint support in the target.");
3765       else if (target_resources_ok < 0)
3766         error ("Hardware breakpoints used exceeds limit.");
3767     }
3768 
3769   bpt->enable = enabled;
3770   bpt->disposition = disposition;
3771   check_duplicates (bpt->address);
3772   breakpoints_changed ();
3773 
3774   if (bpt->type == bp_watchpoint || bpt->type == bp_hardware_watchpoint ||
3775       bpt->type == bp_read_watchpoint || bpt->type == bp_access_watchpoint)
3776     {
3777       if (bpt->exp_valid_block != NULL)
3778 	{
3779 	  struct frame_info *fr =
3780 	    find_frame_addr_in_frame_chain (bpt->watchpoint_frame);
3781 	  if (fr == NULL)
3782 	    {
3783 	      printf_filtered ("\
3784 Cannot enable watchpoint %d because the block in which its expression\n\
3785 is valid is not currently in scope.\n", bpt->number);
3786 	      bpt->enable = disabled;
3787 	      return;
3788 	    }
3789 
3790 	  save_selected_frame = selected_frame;
3791 	  save_selected_frame_level = selected_frame_level;
3792 	  select_frame (fr, -1);
3793 	}
3794 
3795       value_free (bpt->val);
3796       mark = value_mark ();
3797       bpt->val = evaluate_expression (bpt->exp);
3798       release_value (bpt->val);
3799       if (VALUE_LAZY (bpt->val))
3800 	value_fetch_lazy (bpt->val);
3801 
3802       if (bpt->type == bp_hardware_watchpoint ||
3803            bpt->type == bp_read_watchpoint ||
3804            bpt->type == bp_access_watchpoint)
3805       {
3806         int i = hw_watchpoint_used_count (bpt->type, &other_type_used);
3807         int mem_cnt = can_use_hardware_watchpoint (bpt->val);
3808 
3809         target_resources_ok = TARGET_CAN_USE_HARDWARE_WATCHPOINT(
3810                 bpt->type, i + mem_cnt, other_type_used);
3811         /* we can consider of type is bp_hardware_watchpoint, convert to
3812 	   bp_watchpoint in the following condition */
3813         if (target_resources_ok < 0)
3814 	  {
3815              printf_filtered("\
3816 Cannot enable watchpoint %d because target watch resources\n\
3817 have been allocated for other watchpoints.\n", bpt->number);
3818 	     bpt->enable = disabled;
3819 	     value_free_to_mark (mark);
3820 	     return;
3821           }
3822       }
3823 
3824       if (save_selected_frame_level >= 0)
3825 	select_frame (save_selected_frame, save_selected_frame_level);
3826       value_free_to_mark (mark);
3827     }
3828   if (modify_breakpoint_hook)
3829     modify_breakpoint_hook (bpt);
3830 }
3831 
3832 void
3833 enable_breakpoint (bpt)
3834      struct breakpoint *bpt;
3835 {
3836   do_enable_breakpoint (bpt, donttouch);
3837 }
3838 
3839 /* The enable command enables the specified breakpoints (or all defined
3840    breakpoints) so they once again become (or continue to be) effective
3841    in stopping the inferior. */
3842 
3843 /* ARGSUSED */
3844 static void
3845 enable_command (args, from_tty)
3846      char *args;
3847      int from_tty;
3848 {
3849   register struct breakpoint *bpt;
3850   if (args == 0)
3851     ALL_BREAKPOINTS (bpt)
3852       switch (bpt->type)
3853 	{
3854 	case bp_breakpoint:
3855 	case bp_hardware_breakpoint:
3856 	case bp_watchpoint:
3857 	case bp_hardware_watchpoint:
3858 	case bp_read_watchpoint:
3859 	case bp_access_watchpoint:
3860 	  enable_breakpoint (bpt);
3861 	default:
3862 	  continue;
3863 	}
3864   else
3865     map_breakpoint_numbers (args, enable_breakpoint);
3866 }
3867 
3868 static void
3869 enable_once_breakpoint (bpt)
3870      struct breakpoint *bpt;
3871 {
3872   do_enable_breakpoint (bpt, disable);
3873 }
3874 
3875 /* ARGSUSED */
3876 static void
3877 enable_once_command (args, from_tty)
3878      char *args;
3879      int from_tty;
3880 {
3881   map_breakpoint_numbers (args, enable_once_breakpoint);
3882 }
3883 
3884 static void
3885 enable_delete_breakpoint (bpt)
3886      struct breakpoint *bpt;
3887 {
3888   do_enable_breakpoint (bpt, del);
3889 }
3890 
3891 /* ARGSUSED */
3892 static void
3893 enable_delete_command (args, from_tty)
3894      char *args;
3895      int from_tty;
3896 {
3897   map_breakpoint_numbers (args, enable_delete_breakpoint);
3898 }
3899 
3900 /* Use default_breakpoint_'s, or nothing if they aren't valid.  */
3901 
3902 struct symtabs_and_lines
3903 decode_line_spec_1 (string, funfirstline)
3904      char *string;
3905      int funfirstline;
3906 {
3907   struct symtabs_and_lines sals;
3908   if (string == 0)
3909     error ("Empty line specification.");
3910   if (default_breakpoint_valid)
3911     sals = decode_line_1 (&string, funfirstline,
3912 			  default_breakpoint_symtab, default_breakpoint_line,
3913 			  (char ***)NULL);
3914   else
3915     sals = decode_line_1 (&string, funfirstline,
3916 			  (struct symtab *)NULL, 0, (char ***)NULL);
3917   if (*string)
3918     error ("Junk at end of line specification: %s", string);
3919   return sals;
3920 }
3921 
3922 void
3923 _initialize_breakpoint ()
3924 {
3925   breakpoint_chain = 0;
3926   /* Don't bother to call set_breakpoint_count.  $bpnum isn't useful
3927      before a breakpoint is set.  */
3928   breakpoint_count = 0;
3929 
3930   add_com ("ignore", class_breakpoint, ignore_command,
3931 	   "Set ignore-count of breakpoint number N to COUNT.\n\
3932 Usage is `ignore N COUNT'.");
3933 
3934   add_com ("commands", class_breakpoint, commands_command,
3935 	   "Set commands to be executed when a breakpoint is hit.\n\
3936 Give breakpoint number as argument after \"commands\".\n\
3937 With no argument, the targeted breakpoint is the last one set.\n\
3938 The commands themselves follow starting on the next line.\n\
3939 Type a line containing \"end\" to indicate the end of them.\n\
3940 Give \"silent\" as the first line to make the breakpoint silent;\n\
3941 then no output is printed when it is hit, except what the commands print.");
3942 
3943   add_com ("condition", class_breakpoint, condition_command,
3944 	   "Specify breakpoint number N to break only if COND is true.\n\
3945 Usage is `condition N COND', where N is an integer and COND is an\n\
3946 expression to be evaluated whenever breakpoint N is reached.  ");
3947 
3948   add_com ("tbreak", class_breakpoint, tbreak_command,
3949 	   "Set a temporary breakpoint.  Args like \"break\" command.\n\
3950 Like \"break\" except the breakpoint is only temporary,\n\
3951 so it will be deleted when hit.  Equivalent to \"break\" followed\n\
3952 by using \"enable delete\" on the breakpoint number.");
3953 
3954   add_com ("hbreak", class_breakpoint, hbreak_command,
3955 	   "Set a hardware assisted  breakpoint. Args like \"break\" command.\n\
3956 Like \"break\" except the breakpoint requires hardware support,\n\
3957 some target hardware may not have this support.");
3958 
3959   add_com ("thbreak", class_breakpoint, thbreak_command,
3960 	   "Set a temporary hardware assisted breakpoint. Args like \"break\" command.\n\
3961 Like \"hbreak\" except the breakpoint is only temporary,\n\
3962 so it will be deleted when hit.");
3963 
3964   add_prefix_cmd ("enable", class_breakpoint, enable_command,
3965 		  "Enable some breakpoints.\n\
3966 Give breakpoint numbers (separated by spaces) as arguments.\n\
3967 With no subcommand, breakpoints are enabled until you command otherwise.\n\
3968 This is used to cancel the effect of the \"disable\" command.\n\
3969 With a subcommand you can enable temporarily.",
3970 		  &enablelist, "enable ", 1, &cmdlist);
3971 
3972   add_abbrev_prefix_cmd ("breakpoints", class_breakpoint, enable_command,
3973 		  "Enable some breakpoints.\n\
3974 Give breakpoint numbers (separated by spaces) as arguments.\n\
3975 This is used to cancel the effect of the \"disable\" command.\n\
3976 May be abbreviated to simply \"enable\".\n",
3977 		  &enablebreaklist, "enable breakpoints ", 1, &enablelist);
3978 
3979   add_cmd ("once", no_class, enable_once_command,
3980 	   "Enable breakpoints for one hit.  Give breakpoint numbers.\n\
3981 If a breakpoint is hit while enabled in this fashion, it becomes disabled.",
3982 	   &enablebreaklist);
3983 
3984   add_cmd ("delete", no_class, enable_delete_command,
3985 	   "Enable breakpoints and delete when hit.  Give breakpoint numbers.\n\
3986 If a breakpoint is hit while enabled in this fashion, it is deleted.",
3987 	   &enablebreaklist);
3988 
3989   add_cmd ("delete", no_class, enable_delete_command,
3990 	   "Enable breakpoints and delete when hit.  Give breakpoint numbers.\n\
3991 If a breakpoint is hit while enabled in this fashion, it is deleted.",
3992 	   &enablelist);
3993 
3994   add_cmd ("once", no_class, enable_once_command,
3995 	   "Enable breakpoints for one hit.  Give breakpoint numbers.\n\
3996 If a breakpoint is hit while enabled in this fashion, it becomes disabled.",
3997 	   &enablelist);
3998 
3999   add_prefix_cmd ("disable", class_breakpoint, disable_command,
4000 	   "Disable some breakpoints.\n\
4001 Arguments are breakpoint numbers with spaces in between.\n\
4002 To disable all breakpoints, give no argument.\n\
4003 A disabled breakpoint is not forgotten, but has no effect until reenabled.",
4004 		  &disablelist, "disable ", 1, &cmdlist);
4005   add_com_alias ("dis", "disable", class_breakpoint, 1);
4006   add_com_alias ("disa", "disable", class_breakpoint, 1);
4007 
4008   add_cmd ("breakpoints", class_alias, disable_command,
4009 	   "Disable some breakpoints.\n\
4010 Arguments are breakpoint numbers with spaces in between.\n\
4011 To disable all breakpoints, give no argument.\n\
4012 A disabled breakpoint is not forgotten, but has no effect until reenabled.\n\
4013 This command may be abbreviated \"disable\".",
4014 	   &disablelist);
4015 
4016   add_prefix_cmd ("delete", class_breakpoint, delete_command,
4017 	   "Delete some breakpoints or auto-display expressions.\n\
4018 Arguments are breakpoint numbers with spaces in between.\n\
4019 To delete all breakpoints, give no argument.\n\
4020 \n\
4021 Also a prefix command for deletion of other GDB objects.\n\
4022 The \"unset\" command is also an alias for \"delete\".",
4023 		  &deletelist, "delete ", 1, &cmdlist);
4024   add_com_alias ("d", "delete", class_breakpoint, 1);
4025 
4026   add_cmd ("breakpoints", class_alias, delete_command,
4027 	   "Delete some breakpoints or auto-display expressions.\n\
4028 Arguments are breakpoint numbers with spaces in between.\n\
4029 To delete all breakpoints, give no argument.\n\
4030 This command may be abbreviated \"delete\".",
4031 	   &deletelist);
4032 
4033   add_com ("clear", class_breakpoint, clear_command,
4034 	   concat ("Clear breakpoint at specified line or function.\n\
4035 Argument may be line number, function name, or \"*\" and an address.\n\
4036 If line number is specified, all breakpoints in that line are cleared.\n\
4037 If function is specified, breakpoints at beginning of function are cleared.\n\
4038 If an address is specified, breakpoints at that address are cleared.\n\n",
4039 "With no argument, clears all breakpoints in the line that the selected frame\n\
4040 is executing in.\n\
4041 \n\
4042 See also the \"delete\" command which clears breakpoints by number.", NULL));
4043 
4044   add_com ("break", class_breakpoint, break_command,
4045 	   concat ("Set breakpoint at specified line or function.\n\
4046 Argument may be line number, function name, or \"*\" and an address.\n\
4047 If line number is specified, break at start of code for that line.\n\
4048 If function is specified, break at start of code for that function.\n\
4049 If an address is specified, break at that exact address.\n",
4050 "With no arg, uses current execution address of selected stack frame.\n\
4051 This is useful for breaking on return to a stack frame.\n\
4052 \n\
4053 Multiple breakpoints at one place are permitted, and useful if conditional.\n\
4054 \n\
4055 Do \"help breakpoints\" for info on other commands dealing with breakpoints.", NULL));
4056   add_com_alias ("b", "break", class_run, 1);
4057   add_com_alias ("br", "break", class_run, 1);
4058   add_com_alias ("bre", "break", class_run, 1);
4059   add_com_alias ("brea", "break", class_run, 1);
4060 
4061   add_info ("breakpoints", breakpoints_info,
4062 	    concat ("Status of user-settable breakpoints, or breakpoint number NUMBER.\n\
4063 The \"Type\" column indicates one of:\n\
4064 \tbreakpoint     - normal breakpoint\n\
4065 \twatchpoint     - watchpoint\n\
4066 The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
4067 the disposition of the breakpoint after it gets hit.  \"dis\" means that the\n\
4068 breakpoint will be disabled.  The \"Address\" and \"What\" columns indicate the\n\
4069 address and file/line number respectively.\n\n",
4070 "Convenience variable \"$_\" and default examine address for \"x\"\n\
4071 are set to the address of the last breakpoint listed.\n\n\
4072 Convenience variable \"$bpnum\" contains the number of the last\n\
4073 breakpoint set.", NULL));
4074 
4075 #if MAINTENANCE_CMDS
4076 
4077   add_cmd ("breakpoints", class_maintenance, maintenance_info_breakpoints,
4078 	    concat ("Status of all breakpoints, or breakpoint number NUMBER.\n\
4079 The \"Type\" column indicates one of:\n\
4080 \tbreakpoint     - normal breakpoint\n\
4081 \twatchpoint     - watchpoint\n\
4082 \tlongjmp        - internal breakpoint used to step through longjmp()\n\
4083 \tlongjmp resume - internal breakpoint at the target of longjmp()\n\
4084 \tuntil          - internal breakpoint used by the \"until\" command\n\
4085 \tfinish         - internal breakpoint used by the \"finish\" command\n",
4086 "The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
4087 the disposition of the breakpoint after it gets hit.  \"dis\" means that the\n\
4088 breakpoint will be disabled.  The \"Address\" and \"What\" columns indicate the\n\
4089 address and file/line number respectively.\n\n",
4090 "Convenience variable \"$_\" and default examine address for \"x\"\n\
4091 are set to the address of the last breakpoint listed.\n\n\
4092 Convenience variable \"$bpnum\" contains the number of the last\n\
4093 breakpoint set.", NULL),
4094 	   &maintenanceinfolist);
4095 
4096 #endif	/* MAINTENANCE_CMDS */
4097 
4098   add_com ("catch", class_breakpoint, catch_command,
4099          "Set breakpoints to catch exceptions that are raised.\n\
4100 Argument may be a single exception to catch, multiple exceptions\n\
4101 to catch, or the default exception \"default\".  If no arguments\n\
4102 are given, breakpoints are set at all exception handlers catch clauses\n\
4103 within the current scope.\n\
4104 \n\
4105 A condition specified for the catch applies to all breakpoints set\n\
4106 with this command\n\
4107 \n\
4108 Do \"help breakpoints\" for info on other commands dealing with breakpoints.");
4109 
4110   add_com ("watch", class_breakpoint, watch_command,
4111 	   "Set a watchpoint for an expression.\n\
4112 A watchpoint stops execution of your program whenever the value of\n\
4113 an expression changes.");
4114 
4115   add_com ("rwatch", class_breakpoint, rwatch_command,
4116 	   "Set a read watchpoint for an expression.\n\
4117 A watchpoint stops execution of your program whenever the value of\n\
4118 an expression is read.");
4119 
4120   add_com ("awatch", class_breakpoint, awatch_command,
4121 	   "Set a watchpoint for an expression.\n\
4122 A watchpoint stops execution of your program whenever the value of\n\
4123 an expression is either read or written.");
4124 
4125   add_info ("watchpoints", breakpoints_info,
4126 	    "Synonym for ``info breakpoints''.");
4127 
4128 }
4129