Tue Mar 30 08:06:24 1993 Jim Kingdon (kingdon@cygnus.com)
[external/binutils.git] / gdb / breakpoint.c
1 /* Everything about breakpoints, for GDB.
2    Copyright 1986, 1987, 1989, 1990, 1991, 1992 Free Software Foundation, Inc.
3
4 This file is part of GDB.
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
19
20 #include "defs.h"
21 #include <ctype.h>
22 #include "symtab.h"
23 #include "frame.h"
24 #include "breakpoint.h"
25 #include "gdbtypes.h"
26 #include "expression.h"
27 #include "gdbcore.h"
28 #include "gdbcmd.h"
29 #include "value.h"
30 #include "ctype.h"
31 #include "command.h"
32 #include "inferior.h"
33 #include "target.h"
34 #include "language.h"
35 #include <string.h>
36 #include "demangle.h"
37
38 /* local function prototypes */
39
40 static void
41 catch_command_1 PARAMS ((char *, int, int));
42
43 static void
44 enable_delete_command PARAMS ((char *, int));
45
46 static void
47 enable_delete_breakpoint PARAMS ((struct breakpoint *));
48
49 static void
50 enable_once_command PARAMS ((char *, int));
51
52 static void
53 enable_once_breakpoint PARAMS ((struct breakpoint *));
54
55 static void
56 disable_command PARAMS ((char *, int));
57
58 static void
59 disable_breakpoint PARAMS ((struct breakpoint *));
60
61 static void
62 enable_command PARAMS ((char *, int));
63
64 static void
65 enable_breakpoint PARAMS ((struct breakpoint *));
66
67 static void
68 map_breakpoint_numbers PARAMS ((char *, void (*)(struct breakpoint *)));
69
70 static void
71 ignore_command PARAMS ((char *, int));
72
73 static int
74 breakpoint_re_set_one PARAMS ((char *));
75
76 static void
77 delete_command PARAMS ((char *, int));
78
79 static void
80 clear_command PARAMS ((char *, int));
81
82 static void
83 catch_command PARAMS ((char *, int));
84
85 static struct symtabs_and_lines
86 get_catch_sals PARAMS ((int));
87
88 static void
89 watch_command PARAMS ((char *, int));
90
91 static void
92 tbreak_command PARAMS ((char *, int));
93
94 static void
95 break_command_1 PARAMS ((char *, int, int));
96
97 static void
98 mention PARAMS ((struct breakpoint *));
99
100 static struct breakpoint *
101 set_raw_breakpoint PARAMS ((struct symtab_and_line));
102
103 static void
104 check_duplicates PARAMS ((CORE_ADDR));
105
106 static void
107 describe_other_breakpoints PARAMS ((CORE_ADDR));
108
109 static void
110 breakpoints_info PARAMS ((char *, int));
111
112 static void
113 breakpoint_1 PARAMS ((int, int));
114
115 static bpstat
116 bpstat_alloc PARAMS ((struct breakpoint *, bpstat));
117
118 static int
119 breakpoint_cond_eval PARAMS ((char *));
120
121 static void
122 cleanup_executing_breakpoints PARAMS ((int));
123
124 static void
125 commands_command PARAMS ((char *, int));
126
127 static void
128 condition_command PARAMS ((char *, int));
129
130 static int
131 get_number PARAMS ((char **));
132
133 static void
134 set_breakpoint_count PARAMS ((int));
135
136
137 extern int addressprint;                /* Print machine addresses? */
138 extern int demangle;                    /* Print de-mangled symbol names? */
139
140 /* Are we executing breakpoint commands?  */
141 static int executing_breakpoint_commands;
142
143 /* Walk the following statement or block through all breakpoints.
144    ALL_BREAKPOINTS_SAFE does so even if the statment deletes the current
145    breakpoint.  */
146
147 #define ALL_BREAKPOINTS(b)  for (b = breakpoint_chain; b; b = b->next)
148
149 #define ALL_BREAKPOINTS_SAFE(b,tmp)     \
150         for (b = breakpoint_chain;      \
151              b? (tmp=b->next, 1): 0;    \
152              b = tmp)
153
154 /* Chain of all breakpoints defined.  */
155
156 struct breakpoint *breakpoint_chain;
157
158 /* Number of last breakpoint made.  */
159
160 static int breakpoint_count;
161
162 /* Set breakpoint count to NUM.  */
163 static void
164 set_breakpoint_count (num)
165      int num;
166 {
167   breakpoint_count = num;
168   set_internalvar (lookup_internalvar ("bpnum"),
169                    value_from_longest (builtin_type_int, (LONGEST) num));
170 }
171
172 /* Default address, symtab and line to put a breakpoint at
173    for "break" command with no arg.
174    if default_breakpoint_valid is zero, the other three are
175    not valid, and "break" with no arg is an error.
176
177    This set by print_stack_frame, which calls set_default_breakpoint.  */
178
179 int default_breakpoint_valid;
180 CORE_ADDR default_breakpoint_address;
181 struct symtab *default_breakpoint_symtab;
182 int default_breakpoint_line;
183
184 /* Flag indicating extra verbosity for xgdb.  */
185 extern int xgdb_verbose;
186 \f
187 /* *PP is a string denoting a breakpoint.  Get the number of the breakpoint.
188    Advance *PP after the string and any trailing whitespace.
189
190    Currently the string can either be a number or "$" followed by the name
191    of a convenience variable.  Making it an expression wouldn't work well
192    for map_breakpoint_numbers (e.g. "4 + 5 + 6").  */
193 static int
194 get_number (pp)
195      char **pp;
196 {
197   int retval;
198   char *p = *pp;
199
200   if (p == NULL)
201     /* Empty line means refer to the last breakpoint.  */
202     return breakpoint_count;
203   else if (*p == '$')
204     {
205       /* Make a copy of the name, so we can null-terminate it
206          to pass to lookup_internalvar().  */
207       char *varname;
208       char *start = ++p;
209       value val;
210
211       while (isalnum (*p) || *p == '_')
212         p++;
213       varname = (char *) alloca (p - start + 1);
214       strncpy (varname, start, p - start);
215       varname[p - start] = '\0';
216       val = value_of_internalvar (lookup_internalvar (varname));
217       if (TYPE_CODE (VALUE_TYPE (val)) != TYPE_CODE_INT)
218         error (
219 "Convenience variables used to specify breakpoints must have integer values."
220                );
221       retval = (int) value_as_long (val);
222     }
223   else
224     {
225       if (*p == '-')
226         ++p;
227       while (*p >= '0' && *p <= '9')
228         ++p;
229       if (p == *pp)
230         /* There is no number here.  (e.g. "cond a == b").  */
231         error_no_arg ("breakpoint number");
232       retval = atoi (*pp);
233     }
234   if (!(isspace (*p) || *p == '\0'))
235     error ("breakpoint number expected");
236   while (isspace (*p))
237     p++;
238   *pp = p;
239   return retval;
240 }
241 \f
242 /* condition N EXP -- set break condition of breakpoint N to EXP.  */
243
244 static void
245 condition_command (arg, from_tty)
246      char *arg;
247      int from_tty;
248 {
249   register struct breakpoint *b;
250   char *p;
251   register int bnum;
252
253   if (arg == 0)
254     error_no_arg ("breakpoint number");
255
256   p = arg;
257   bnum = get_number (&p);
258
259   ALL_BREAKPOINTS (b)
260     if (b->number == bnum)
261       {
262         if (b->cond)
263           {
264             free ((PTR)b->cond);
265             b->cond = 0;
266           }
267         if (b->cond_string != NULL)
268           free ((PTR)b->cond_string);
269
270         if (*p == 0)
271           {
272             b->cond = 0;
273             b->cond_string = NULL;
274             if (from_tty)
275               printf_filtered ("Breakpoint %d now unconditional.\n", bnum);
276           }
277         else
278           {
279             arg = p;
280             /* I don't know if it matters whether this is the string the user
281                typed in or the decompiled expression.  */
282             b->cond_string = savestring (arg, strlen (arg));
283             b->cond = parse_exp_1 (&arg, block_for_pc (b->address), 0);
284             if (*arg)
285               error ("Junk at end of expression");
286           }
287         return;
288       }
289
290   error ("No breakpoint number %d.", bnum);
291 }
292
293 /* ARGSUSED */
294 static void
295 commands_command (arg, from_tty)
296      char *arg;
297      int from_tty;
298 {
299   register struct breakpoint *b;
300   char *p;
301   register int bnum;
302   struct command_line *l;
303
304   /* If we allowed this, we would have problems with when to
305      free the storage, if we change the commands currently
306      being read from.  */
307
308   if (executing_breakpoint_commands)
309     error ("Can't use the \"commands\" command among a breakpoint's commands.");
310
311   p = arg;
312   bnum = get_number (&p);
313   if (p && *p)
314     error ("Unexpected extra arguments following breakpoint number.");
315       
316   ALL_BREAKPOINTS (b)
317     if (b->number == bnum)
318       {
319         if (from_tty && input_from_terminal_p ())
320           printf_filtered ("Type commands for when breakpoint %d is hit, one per line.\n\
321 End with a line saying just \"end\".\n", bnum);
322         l = read_command_lines ();
323         free_command_lines (&b->commands);
324         b->commands = l;
325         return;
326       }
327   error ("No breakpoint number %d.", bnum);
328 }
329 \f
330 extern int memory_breakpoint_size; /* from mem-break.c */
331
332 /* Like target_read_memory() but if breakpoints are inserted, return
333    the shadow contents instead of the breakpoints themselves.
334
335    Read "memory data" from whatever target or inferior we have. 
336    Returns zero if successful, errno value if not.  EIO is used
337    for address out of bounds.  If breakpoints are inserted, returns
338    shadow contents, not the breakpoints themselves.  From breakpoint.c.  */
339
340 int
341 read_memory_nobpt (memaddr, myaddr, len)
342      CORE_ADDR memaddr;
343      char *myaddr;
344      unsigned len;
345 {
346   int status;
347   struct breakpoint *b;
348
349   if (memory_breakpoint_size < 0)
350     /* No breakpoints on this machine.  */
351     return target_read_memory (memaddr, myaddr, len);
352   
353   ALL_BREAKPOINTS (b)
354     {
355       if (b->type == bp_watchpoint || !b->inserted)
356         continue;
357       else if (b->address + memory_breakpoint_size <= memaddr)
358         /* The breakpoint is entirely before the chunk of memory
359            we are reading.  */
360         continue;
361       else if (b->address >= memaddr + len)
362         /* The breakpoint is entirely after the chunk of memory we
363            are reading.  */
364         continue;
365       else
366         {
367           /* Copy the breakpoint from the shadow contents, and recurse
368              for the things before and after.  */
369           
370           /* Addresses and length of the part of the breakpoint that
371              we need to copy.  */
372           CORE_ADDR membpt = b->address;
373           unsigned int bptlen = memory_breakpoint_size;
374           /* Offset within shadow_contents.  */
375           int bptoffset = 0;
376           
377           if (membpt < memaddr)
378             {
379               /* Only copy the second part of the breakpoint.  */
380               bptlen -= memaddr - membpt;
381               bptoffset = memaddr - membpt;
382               membpt = memaddr;
383             }
384
385           if (membpt + bptlen > memaddr + len)
386             {
387               /* Only copy the first part of the breakpoint.  */
388               bptlen -= (membpt + bptlen) - (memaddr + len);
389             }
390
391           memcpy (myaddr + membpt - memaddr, 
392                   b->shadow_contents + bptoffset, bptlen);
393
394           if (membpt > memaddr)
395             {
396               /* Copy the section of memory before the breakpoint.  */
397               status = read_memory_nobpt (memaddr, myaddr, membpt - memaddr);
398               if (status != 0)
399                 return status;
400             }
401
402           if (membpt + bptlen < memaddr + len)
403             {
404               /* Copy the section of memory after the breakpoint.  */
405               status = read_memory_nobpt
406                 (membpt + bptlen,
407                  myaddr + membpt + bptlen - memaddr,
408                  memaddr + len - (membpt + bptlen));
409               if (status != 0)
410                 return status;
411             }
412           return 0;
413         }
414     }
415   /* Nothing overlaps.  Just call read_memory_noerr.  */
416   return target_read_memory (memaddr, myaddr, len);
417 }
418 \f
419 /* insert_breakpoints is used when starting or continuing the program.
420    remove_breakpoints is used when the program stops.
421    Both return zero if successful,
422    or an `errno' value if could not write the inferior.  */
423
424 int
425 insert_breakpoints ()
426 {
427   register struct breakpoint *b;
428   int val = 0;
429   int disabled_breaks = 0;
430
431   ALL_BREAKPOINTS (b)
432     if (b->type != bp_watchpoint
433         && b->enable != disabled
434         && ! b->inserted
435         && ! b->duplicate)
436       {
437         val = target_insert_breakpoint(b->address, b->shadow_contents);
438         if (val)
439           {
440             /* Can't set the breakpoint.  */
441 #if defined (DISABLE_UNSETTABLE_BREAK)
442             if (DISABLE_UNSETTABLE_BREAK (b->address))
443               {
444                 val = 0;
445                 b->enable = disabled;
446                 if (!disabled_breaks)
447                   {
448                     fprintf (stderr,
449                          "Cannot insert breakpoint %d:\n", b->number);
450                     printf_filtered ("Disabling shared library breakpoints:\n");
451                   }
452                 disabled_breaks = 1;
453                 printf_filtered ("%d ", b->number);
454               }
455             else
456 #endif
457               {
458                 fprintf (stderr, "Cannot insert breakpoint %d:\n", b->number);
459 #ifdef ONE_PROCESS_WRITETEXT
460                 fprintf (stderr,
461                   "The same program may be running in another process.\n");
462 #endif
463                 memory_error (val, b->address); /* which bombs us out */
464               }
465           }
466         else
467           b->inserted = 1;
468       }
469   if (disabled_breaks)
470     printf_filtered ("\n");
471   return val;
472 }
473
474 int
475 remove_breakpoints ()
476 {
477   register struct breakpoint *b;
478   int val;
479
480 #ifdef BREAKPOINT_DEBUG
481   printf ("Removing breakpoints.\n");
482 #endif /* BREAKPOINT_DEBUG */
483
484   ALL_BREAKPOINTS (b)
485     if (b->type != bp_watchpoint && b->inserted)
486       {
487         val = target_remove_breakpoint(b->address, b->shadow_contents);
488         if (val)
489           return val;
490         b->inserted = 0;
491 #ifdef BREAKPOINT_DEBUG
492         printf ("Removed breakpoint at %s",
493                 local_hex_string(b->address));
494         printf (", shadow %s",
495                 local_hex_string(b->shadow_contents[0]));
496         printf (", %s.\n",
497                 local_hex_string(b->shadow_contents[1]));
498 #endif /* BREAKPOINT_DEBUG */
499       }
500
501   return 0;
502 }
503
504 /* Clear the "inserted" flag in all breakpoints.
505    This is done when the inferior is loaded.  */
506
507 void
508 mark_breakpoints_out ()
509 {
510   register struct breakpoint *b;
511
512   ALL_BREAKPOINTS (b)
513     b->inserted = 0;
514 }
515
516 /* breakpoint_here_p (PC) returns 1 if an enabled breakpoint exists at PC.
517    When continuing from a location with a breakpoint,
518    we actually single step once before calling insert_breakpoints.  */
519
520 int
521 breakpoint_here_p (pc)
522      CORE_ADDR pc;
523 {
524   register struct breakpoint *b;
525
526   ALL_BREAKPOINTS (b)
527     if (b->enable != disabled && b->address == pc)
528       return 1;
529
530   return 0;
531 }
532 \f
533 /* bpstat stuff.  External routines' interfaces are documented
534    in breakpoint.h.  */
535
536 /* Clear a bpstat so that it says we are not at any breakpoint.
537    Also free any storage that is part of a bpstat.  */
538
539 void
540 bpstat_clear (bsp)
541      bpstat *bsp;
542 {
543   bpstat p;
544   bpstat q;
545
546   if (bsp == 0)
547     return;
548   p = *bsp;
549   while (p != NULL)
550     {
551       q = p->next;
552       if (p->old_val != NULL)
553         value_free (p->old_val);
554       free ((PTR)p);
555       p = q;
556     }
557   *bsp = NULL;
558 }
559
560 /* Return a copy of a bpstat.  Like "bs1 = bs2" but all storage that
561    is part of the bpstat is copied as well.  */
562
563 bpstat
564 bpstat_copy (bs)
565      bpstat bs;
566 {
567   bpstat p = NULL;
568   bpstat tmp;
569   bpstat retval;
570
571   if (bs == NULL)
572     return bs;
573
574   for (; bs != NULL; bs = bs->next)
575     {
576       tmp = (bpstat) xmalloc (sizeof (*tmp));
577       memcpy (tmp, bs, sizeof (*tmp));
578       if (p == NULL)
579         /* This is the first thing in the chain.  */
580         retval = tmp;
581       else
582         p->next = tmp;
583       p = tmp;
584     }
585   p->next = NULL;
586   return retval;
587 }
588
589 /* Find the bpstat associated with this breakpoint */
590
591 bpstat
592 bpstat_find_breakpoint(bsp, breakpoint)
593      bpstat bsp;
594      struct breakpoint *breakpoint;
595 {
596   if (bsp == NULL) return NULL;
597
598   for (;bsp != NULL; bsp = bsp->next) {
599     if (bsp->breakpoint_at == breakpoint) return bsp;
600   }
601   return NULL;
602 }
603
604 /* Return the breakpoint number of the first breakpoint we are stopped
605    at.  *BSP upon return is a bpstat which points to the remaining
606    breakpoints stopped at (but which is not guaranteed to be good for
607    anything but further calls to bpstat_num).
608    Return 0 if passed a bpstat which does not indicate any breakpoints.  */
609
610 int
611 bpstat_num (bsp)
612      bpstat *bsp;
613 {
614   struct breakpoint *b;
615
616   if ((*bsp) == NULL)
617     return 0;                   /* No more breakpoint values */
618   else
619     {
620       b = (*bsp)->breakpoint_at;
621       *bsp = (*bsp)->next;
622       if (b == NULL)
623         return -1;              /* breakpoint that's been deleted since */
624       else
625         return b->number;       /* We have its number */
626     }
627 }
628
629 /* Modify BS so that the actions will not be performed.  */
630
631 void
632 bpstat_clear_actions (bs)
633      bpstat bs;
634 {
635   for (; bs != NULL; bs = bs->next)
636     {
637       bs->commands = NULL;
638       if (bs->old_val != NULL)
639         {
640           value_free (bs->old_val);
641           bs->old_val = NULL;
642         }
643     }
644 }
645
646 /* Stub for cleaning up our state if we error-out of a breakpoint command */
647 /* ARGSUSED */
648 static void
649 cleanup_executing_breakpoints (ignore)
650      int ignore;
651 {
652   executing_breakpoint_commands = 0;
653 }
654
655 /* Execute all the commands associated with all the breakpoints at this
656    location.  Any of these commands could cause the process to proceed
657    beyond this point, etc.  We look out for such changes by checking
658    the global "breakpoint_proceeded" after each command.  */
659
660 void
661 bpstat_do_actions (bsp)
662      bpstat *bsp;
663 {
664   bpstat bs;
665   struct cleanup *old_chain;
666
667   executing_breakpoint_commands = 1;
668   old_chain = make_cleanup (cleanup_executing_breakpoints, 0);
669
670 top:
671   bs = *bsp;
672
673   breakpoint_proceeded = 0;
674   for (; bs != NULL; bs = bs->next)
675     {
676       while (bs->commands)
677         {
678           char *line = bs->commands->line;
679           bs->commands = bs->commands->next;
680           execute_command (line, 0);
681           /* If the inferior is proceeded by the command, bomb out now.
682              The bpstat chain has been blown away by wait_for_inferior.
683              But since execution has stopped again, there is a new bpstat
684              to look at, so start over.  */
685           if (breakpoint_proceeded)
686             goto top;
687         }
688     }
689
690   executing_breakpoint_commands = 0;
691   discard_cleanups (old_chain);
692 }
693
694 /* This is the normal print_it function for a bpstat.  In the future,
695    much of this logic could (should?) be moved to bpstat_stop_status,
696    by having it set different print_it functions.  */
697
698 static int
699 print_it_normal (bs)
700      bpstat bs;
701 {
702   /* bs->breakpoint_at can be NULL if it was a momentary breakpoint
703      which has since been deleted.  */
704   if (bs->breakpoint_at == NULL
705       || (bs->breakpoint_at->type != bp_breakpoint
706           && bs->breakpoint_at->type != bp_watchpoint))
707     return 0;
708   
709   /* If bpstat_stop_status says don't print, OK, we won't.  An example
710      circumstance is when we single-stepped for both a watchpoint and
711      for a "stepi" instruction.  The bpstat says that the watchpoint
712      explains the stop, but we shouldn't print because the watchpoint's
713      value didn't change -- and the real reason we are stopping here
714      rather than continuing to step (as the watchpoint would've had us do)
715      is because of the "stepi".  */
716   if (!bs->print)
717     return 0;
718
719   if (bs->breakpoint_at->type == bp_breakpoint)
720     {
721       /* I think the user probably only wants to see one breakpoint
722          number, not all of them.  */
723       printf_filtered ("\nBreakpoint %d, ", bs->breakpoint_at->number);
724       return 0;
725     }
726       
727   if (bs->old_val != NULL)
728     {
729       printf_filtered ("\nWatchpoint %d, ", bs->breakpoint_at->number);
730       print_expression (bs->breakpoint_at->exp, stdout);
731       printf_filtered ("\nOld value = ");
732       value_print (bs->old_val, stdout, 0, Val_pretty_default);
733       printf_filtered ("\nNew value = ");
734       value_print (bs->breakpoint_at->val, stdout, 0,
735                    Val_pretty_default);
736       printf_filtered ("\n");
737       value_free (bs->old_val);
738       bs->old_val = NULL;
739       return 0;
740     }
741   /* We can't deal with it.  Maybe another member of the bpstat chain can.  */
742   return -1;
743 }
744
745 /* Print a message indicating what happened.  Returns nonzero to
746    say that only the source line should be printed after this (zero
747    return means print the frame as well as the source line).  */
748 /* Currently we always return zero.  */
749 int
750 bpstat_print (bs)
751      bpstat bs;
752 {
753   int val;
754   
755   if (bs == NULL)
756     return 0;
757
758   val = (*bs->print_it) (bs);
759   if (val >= 0)
760     return val;
761   
762   /* Maybe another breakpoint in the chain caused us to stop.
763      (Currently all watchpoints go on the bpstat whether hit or
764      not.  That probably could (should) be changed, provided care is taken
765      with respect to bpstat_explains_signal).  */
766   if (bs->next)
767     return bpstat_print (bs->next);
768
769   fprintf_filtered (stderr, "gdb internal error: in bpstat_print\n");
770   return 0;
771 }
772
773 /* Evaluate the expression EXP and return 1 if value is zero.
774    This is used inside a catch_errors to evaluate the breakpoint condition. 
775    The argument is a "struct expression *" that has been cast to char * to 
776    make it pass through catch_errors.  */
777
778 static int
779 breakpoint_cond_eval (exp)
780      char *exp;
781 {
782   return !value_true (evaluate_expression ((struct expression *)exp));
783 }
784
785 /* Allocate a new bpstat and chain it to the current one.  */
786
787 static bpstat
788 bpstat_alloc (b, cbs)
789      register struct breakpoint *b;
790      bpstat cbs;                        /* Current "bs" value */
791 {
792   bpstat bs;
793
794   bs = (bpstat) xmalloc (sizeof (*bs));
795   cbs->next = bs;
796   bs->breakpoint_at = b;
797   /* If the condition is false, etc., don't do the commands.  */
798   bs->commands = NULL;
799   bs->momentary = b->disposition == delete;
800   bs->old_val = NULL;
801   bs->print_it = print_it_normal;
802   return bs;
803 }
804
805 /* Possible return values for watchpoint_check (this can't be an enum
806    because of check_errors).  */
807 /* The watchpoint has been disabled.  */
808 #define WP_DISABLED 1
809 /* The value has changed.  */
810 #define WP_VALUE_CHANGED 2
811 /* The value has not changed.  */
812 #define WP_VALUE_NOT_CHANGED 3
813
814 /* Check watchpoint condition.  */
815 static int
816 watchpoint_check (p)
817      PTR p;
818 {
819   bpstat bs = (bpstat) p;
820
821   int within_current_scope;
822   if (bs->breakpoint_at->exp_valid_block != NULL)
823     within_current_scope =
824       contained_in (get_selected_block (), bs->breakpoint_at->exp_valid_block);
825   else
826     within_current_scope = 1;
827
828   if (within_current_scope)
829     {
830       /* We use value_{,free_to_}mark because it could be a
831          *long* time before we return to the command level and
832          call free_all_values.  */
833       /* But couldn't we just call free_all_values instead?  */
834
835       value mark = value_mark ();
836       value new_val = evaluate_expression (bs->breakpoint_at->exp);
837       if (!value_equal (bs->breakpoint_at->val, new_val))
838         {
839           release_value (new_val);
840           value_free_to_mark (mark);
841           bs->old_val = bs->breakpoint_at->val;
842           bs->breakpoint_at->val = new_val;
843           /* We will stop here */
844           return WP_VALUE_CHANGED;
845         }
846       else
847         {
848           /* Nothing changed, don't do anything.  */
849           value_free_to_mark (mark);
850           /* We won't stop here */
851           return WP_VALUE_NOT_CHANGED;
852         }
853     }
854   else
855     {
856       /* This seems like the only logical thing to do because
857          if we temporarily ignored the watchpoint, then when
858          we reenter the block in which it is valid it contains
859          garbage (in the case of a function, it may have two
860          garbage values, one before and one after the prologue).
861          So we can't even detect the first assignment to it and
862          watch after that (since the garbage may or may not equal
863          the first value assigned).  */
864       bs->breakpoint_at->enable = disabled;
865       printf_filtered ("\
866 Watchpoint %d disabled because the program has left the block in\n\
867 which its expression is valid.\n", bs->breakpoint_at->number);
868       return WP_DISABLED;
869     }
870 }
871
872 /* This is used when everything which needs to be printed has
873    already been printed.  But we still want to print the frame.  */
874 static int
875 print_it_noop (bs)
876      bpstat bs;
877 {
878   return 0;
879 }
880
881 /* Determine whether we stopped at a breakpoint, etc, or whether we
882    don't understand this stop.  Result is a chain of bpstat's such that:
883
884         if we don't understand the stop, the result is a null pointer.
885
886         if we understand why we stopped, the result is not null, and
887         the first element of the chain contains summary "stop" and
888         "print" flags for the whole chain.
889
890         Each element of the chain refers to a particular breakpoint or
891         watchpoint at which we have stopped.  (We may have stopped for
892         several reasons concurrently.)
893
894         Each element of the chain has valid next, breakpoint_at,
895         commands, FIXME??? fields.
896
897  */
898
899         
900 bpstat
901 bpstat_stop_status (pc, frame_address)
902      CORE_ADDR *pc;
903      FRAME_ADDR frame_address;
904 {
905   register struct breakpoint *b;
906   int stop = 0;
907   int print = 0;
908   CORE_ADDR bp_addr;
909 #if DECR_PC_AFTER_BREAK != 0 || defined (SHIFT_INST_REGS)
910   /* True if we've hit a breakpoint (as opposed to a watchpoint).  */
911   int real_breakpoint = 0;
912 #endif
913   /* Root of the chain of bpstat's */
914   struct bpstat root_bs[1];
915   /* Pointer to the last thing in the chain currently.  */
916   bpstat bs = root_bs;
917
918   /* Get the address where the breakpoint would have been.  */
919   bp_addr = *pc - DECR_PC_AFTER_BREAK;
920
921   ALL_BREAKPOINTS (b)
922     {
923       int this_bp_stop;
924       int this_bp_print;
925
926       if (b->enable == disabled)
927         continue;
928
929       if (b->type != bp_watchpoint && b->address != bp_addr)
930         continue;
931
932       /* Come here if it's a watchpoint, or if the break address matches */
933
934       bs = bpstat_alloc (b, bs);        /* Alloc a bpstat to explain stop */
935
936       this_bp_stop = 1;
937       this_bp_print = 1;
938
939       if (b->type == bp_watchpoint)
940         {
941           static char message1[] =
942             "Error evaluating expression for watchpoint %d\n";
943           char message[sizeof (message1) + 30 /* slop */];
944           sprintf (message, message1, b->number);
945           switch (catch_errors (watchpoint_check, (char *) bs, message))
946             {
947             case WP_DISABLED:
948               /* We've already printed what needs to be printed.  */
949               bs->print_it = print_it_noop;
950               /* Stop.  */
951               break;
952             case WP_VALUE_CHANGED:
953               /* Stop.  */
954               break;
955             case WP_VALUE_NOT_CHANGED:
956               /* Don't stop.  */
957               continue;
958             default:
959               /* Can't happen.  */
960               /* FALLTHROUGH */
961             case 0:
962               /* Error from catch_errors.  */
963               b->enable = disabled;
964               printf_filtered ("Watchpoint %d disabled.\n", b->number);
965               /* We've already printed what needs to be printed.  */
966               bs->print_it = print_it_noop;
967               /* Stop.  */
968               break;
969             }
970         }
971 #if DECR_PC_AFTER_BREAK != 0 || defined (SHIFT_INST_REGS)
972       else
973         real_breakpoint = 1;
974 #endif
975
976       if (b->frame && b->frame != frame_address)
977         this_bp_stop = 0;
978       else
979         {
980           int value_is_zero;
981
982           if (b->cond)
983             {
984               /* Need to select the frame, with all that implies
985                  so that the conditions will have the right context.  */
986               select_frame (get_current_frame (), 0);
987               value_is_zero
988                 = catch_errors (breakpoint_cond_eval, (char *)(b->cond),
989                                 "Error in testing breakpoint condition:\n");
990                                 /* FIXME-someday, should give breakpoint # */
991               free_all_values ();
992             }
993           if (b->cond && value_is_zero)
994             {
995               this_bp_stop = 0;
996             }
997           else if (b->ignore_count > 0)
998             {
999               b->ignore_count--;
1000               this_bp_stop = 0;
1001             }
1002           else
1003             {
1004               /* We will stop here */
1005               if (b->disposition == disable)
1006                 b->enable = disabled;
1007               bs->commands = b->commands;
1008               if (b->silent)
1009                 this_bp_print = 0;
1010               if (bs->commands && STREQ ("silent", bs->commands->line))
1011                 {
1012                   bs->commands = bs->commands->next;
1013                   this_bp_print = 0;
1014                 }
1015             }
1016         }
1017       if (this_bp_stop)
1018         stop = 1;
1019       if (this_bp_print)
1020         print = 1;
1021     }
1022
1023   bs->next = NULL;              /* Terminate the chain */
1024   bs = root_bs->next;           /* Re-grab the head of the chain */
1025   if (bs)
1026     {
1027       bs->stop = stop;
1028       bs->print = print;
1029 #if DECR_PC_AFTER_BREAK != 0 || defined (SHIFT_INST_REGS)
1030       if (real_breakpoint)
1031         {
1032           *pc = bp_addr;
1033 #if defined (SHIFT_INST_REGS)
1034           {
1035             CORE_ADDR pc = read_register (PC_REGNUM);
1036             CORE_ADDR npc = read_register (NPC_REGNUM);
1037             if (pc != npc)
1038               {
1039                 write_register (NNPC_REGNUM, npc);
1040                 write_register (NPC_REGNUM, pc);
1041               }
1042           }
1043 #else /* No SHIFT_INST_REGS.  */
1044           write_pc (bp_addr);
1045 #endif /* No SHIFT_INST_REGS.  */
1046         }
1047 #endif /* DECR_PC_AFTER_BREAK != 0.  */
1048     }
1049   return bs;
1050 }
1051
1052 /* Nonzero if we should step constantly (e.g. watchpoints on machines
1053    without hardware support).  This isn't related to a specific bpstat,
1054    just to things like whether watchpoints are set.  */
1055
1056 int 
1057 bpstat_should_step ()
1058 {
1059   struct breakpoint *b;
1060   ALL_BREAKPOINTS (b)
1061     if (b->enable == enabled && b->type == bp_watchpoint)
1062       return 1;
1063   return 0;
1064 }
1065 \f
1066 /* Print information on breakpoint number BNUM, or -1 if all.
1067    If WATCHPOINTS is zero, process only breakpoints; if WATCHPOINTS
1068    is nonzero, process only watchpoints.  */
1069
1070 static void
1071 breakpoint_1 (bnum, allflag)
1072      int bnum;
1073      int allflag;
1074 {
1075   register struct breakpoint *b;
1076   register struct command_line *l;
1077   register struct symbol *sym;
1078   CORE_ADDR last_addr = (CORE_ADDR)-1;
1079   int found_a_breakpoint = 0;
1080   static char *bptypes[] = {"breakpoint", "until", "finish", "watchpoint",
1081                               "longjmp", "longjmp resume"};
1082   static char *bpdisps[] = {"del", "dis", "keep"};
1083   static char bpenables[] = "ny";
1084
1085   if (!breakpoint_chain)
1086     {
1087       printf_filtered ("No breakpoints or watchpoints.\n");
1088       return;
1089     }
1090   
1091   ALL_BREAKPOINTS (b)
1092     if (bnum == -1
1093         || bnum == b->number)
1094       {
1095 /*  We only print out user settable breakpoints unless the allflag is set. */
1096         if (!allflag
1097             && b->type != bp_breakpoint
1098             && b->type != bp_watchpoint)
1099           continue;
1100
1101         if (!found_a_breakpoint++)
1102           printf_filtered ("Num Type           Disp Enb %sWhat\n",
1103                            addressprint ? "Address    " : "");
1104
1105         printf_filtered ("%-3d %-14s %-4s %-3c ",
1106                          b->number,
1107                          bptypes[(int)b->type],
1108                          bpdisps[(int)b->disposition],
1109                          bpenables[(int)b->enable]);
1110         switch (b->type)
1111           {
1112           case bp_watchpoint:
1113             print_expression (b->exp, stdout);
1114             break;
1115           case bp_breakpoint:
1116           case bp_until:
1117           case bp_finish:
1118           case bp_longjmp:
1119           case bp_longjmp_resume:
1120             if (addressprint)
1121               printf_filtered ("%s ", local_hex_string_custom(b->address, "08"));
1122
1123             last_addr = b->address;
1124             if (b->symtab)
1125               {
1126                 sym = find_pc_function (b->address);
1127                 if (sym)
1128                   {
1129                     fputs_filtered ("in ", stdout);
1130                     fputs_filtered (SYMBOL_SOURCE_NAME (sym), stdout);
1131                     fputs_filtered (" at ", stdout);
1132                   }
1133                 fputs_filtered (b->symtab->filename, stdout);
1134                 printf_filtered (":%d", b->line_number);
1135               }
1136             else
1137               print_address_symbolic (b->address, stdout, demangle, " ");
1138           }
1139
1140         printf_filtered ("\n");
1141
1142         if (b->frame)
1143           printf_filtered ("\tstop only in stack frame at %s\n",
1144                            local_hex_string(b->frame));
1145         if (b->cond)
1146           {
1147             printf_filtered ("\tstop only if ");
1148             print_expression (b->cond, stdout);
1149             printf_filtered ("\n");
1150           }
1151         if (b->ignore_count)
1152           printf_filtered ("\tignore next %d hits\n", b->ignore_count);
1153         if ((l = b->commands))
1154           while (l)
1155             {
1156               fputs_filtered ("\t", stdout);
1157               fputs_filtered (l->line, stdout);
1158               fputs_filtered ("\n", stdout);
1159               l = l->next;
1160             }
1161       }
1162
1163   if (!found_a_breakpoint
1164       && bnum != -1)
1165     printf_filtered ("No breakpoint or watchpoint number %d.\n", bnum);
1166   else
1167     /* Compare against (CORE_ADDR)-1 in case some compiler decides
1168        that a comparison of an unsigned with -1 is always false.  */
1169     if (last_addr != (CORE_ADDR)-1)
1170       set_next_address (last_addr);
1171 }
1172
1173 /* ARGSUSED */
1174 static void
1175 breakpoints_info (bnum_exp, from_tty)
1176      char *bnum_exp;
1177      int from_tty;
1178 {
1179   int bnum = -1;
1180
1181   if (bnum_exp)
1182     bnum = parse_and_eval_address (bnum_exp);
1183
1184   breakpoint_1 (bnum, 0);
1185 }
1186
1187 #if MAINTENANCE_CMDS
1188
1189 /* ARGSUSED */
1190 static void
1191 maintenance_info_breakpoints (bnum_exp, from_tty)
1192      char *bnum_exp;
1193      int from_tty;
1194 {
1195   int bnum = -1;
1196
1197   if (bnum_exp)
1198     bnum = parse_and_eval_address (bnum_exp);
1199
1200   breakpoint_1 (bnum, 1);
1201 }
1202
1203 #endif
1204
1205 /* Print a message describing any breakpoints set at PC.  */
1206
1207 static void
1208 describe_other_breakpoints (pc)
1209      register CORE_ADDR pc;
1210 {
1211   register int others = 0;
1212   register struct breakpoint *b;
1213
1214   ALL_BREAKPOINTS (b)
1215     if (b->address == pc)
1216       others++;
1217   if (others > 0)
1218     {
1219       printf ("Note: breakpoint%s ", (others > 1) ? "s" : "");
1220       ALL_BREAKPOINTS (b)
1221         if (b->address == pc)
1222           {
1223             others--;
1224             printf ("%d%s%s ",
1225                     b->number,
1226                     (b->enable == disabled) ? " (disabled)" : "",
1227                     (others > 1) ? "," : ((others == 1) ? " and" : ""));
1228           }
1229       printf ("also set at pc %s.\n", local_hex_string(pc));
1230     }
1231 }
1232 \f
1233 /* Set the default place to put a breakpoint
1234    for the `break' command with no arguments.  */
1235
1236 void
1237 set_default_breakpoint (valid, addr, symtab, line)
1238      int valid;
1239      CORE_ADDR addr;
1240      struct symtab *symtab;
1241      int line;
1242 {
1243   default_breakpoint_valid = valid;
1244   default_breakpoint_address = addr;
1245   default_breakpoint_symtab = symtab;
1246   default_breakpoint_line = line;
1247 }
1248
1249 /* Rescan breakpoints at address ADDRESS,
1250    marking the first one as "first" and any others as "duplicates".
1251    This is so that the bpt instruction is only inserted once.  */
1252
1253 static void
1254 check_duplicates (address)
1255      CORE_ADDR address;
1256 {
1257   register struct breakpoint *b;
1258   register int count = 0;
1259
1260   if (address == 0)             /* Watchpoints are uninteresting */
1261     return;
1262
1263   ALL_BREAKPOINTS (b)
1264     if (b->enable != disabled && b->address == address)
1265       {
1266         count++;
1267         b->duplicate = count > 1;
1268       }
1269 }
1270
1271 /* Low level routine to set a breakpoint.
1272    Takes as args the three things that every breakpoint must have.
1273    Returns the breakpoint object so caller can set other things.
1274    Does not set the breakpoint number!
1275    Does not print anything.
1276
1277    ==> This routine should not be called if there is a chance of later
1278    error(); otherwise it leaves a bogus breakpoint on the chain.  Validate
1279    your arguments BEFORE calling this routine!  */
1280
1281 static struct breakpoint *
1282 set_raw_breakpoint (sal)
1283      struct symtab_and_line sal;
1284 {
1285   register struct breakpoint *b, *b1;
1286
1287   b = (struct breakpoint *) xmalloc (sizeof (struct breakpoint));
1288   memset (b, 0, sizeof (*b));
1289   b->address = sal.pc;
1290   b->symtab = sal.symtab;
1291   b->line_number = sal.line;
1292   b->enable = enabled;
1293   b->next = 0;
1294   b->silent = 0;
1295   b->ignore_count = 0;
1296   b->commands = NULL;
1297   b->frame = 0;
1298
1299   /* Add this breakpoint to the end of the chain
1300      so that a list of breakpoints will come out in order
1301      of increasing numbers.  */
1302
1303   b1 = breakpoint_chain;
1304   if (b1 == 0)
1305     breakpoint_chain = b;
1306   else
1307     {
1308       while (b1->next)
1309         b1 = b1->next;
1310       b1->next = b;
1311     }
1312
1313   check_duplicates (sal.pc);
1314
1315   return b;
1316 }
1317
1318 static void
1319 create_longjmp_breakpoint(func_name)
1320      char *func_name;
1321 {
1322   struct symtab_and_line sal;
1323   struct breakpoint *b;
1324   static int internal_breakpoint_number = -1;
1325
1326   if (func_name != NULL)
1327     {
1328       struct minimal_symbol *m;
1329
1330       m = lookup_minimal_symbol(func_name, (struct objfile *)NULL);
1331       if (m)
1332         sal.pc = SYMBOL_VALUE_ADDRESS (m);
1333       else
1334         return;
1335     }
1336   else
1337     sal.pc = 0;
1338
1339   sal.symtab = NULL;
1340   sal.line = 0;
1341
1342   b = set_raw_breakpoint(sal);
1343   if (!b) return;
1344
1345   b->type = func_name != NULL ? bp_longjmp : bp_longjmp_resume;
1346   b->disposition = donttouch;
1347   b->enable = disabled;
1348   b->silent = 1;
1349   if (func_name)
1350     b->addr_string = strsave(func_name);
1351   b->number = internal_breakpoint_number--;
1352 }
1353
1354 /* Call this routine when stepping and nexting to enable a breakpoint if we do
1355    a longjmp().  When we hit that breakpoint, call
1356    set_longjmp_resume_breakpoint() to figure out where we are going. */
1357
1358 void
1359 enable_longjmp_breakpoint()
1360 {
1361   register struct breakpoint *b;
1362
1363   ALL_BREAKPOINTS (b)
1364     if (b->type == bp_longjmp)
1365       {
1366         b->enable = enabled;
1367         check_duplicates (b->address);
1368       }
1369 }
1370
1371 void
1372 disable_longjmp_breakpoint()
1373 {
1374   register struct breakpoint *b;
1375
1376   ALL_BREAKPOINTS (b)
1377     if (   b->type == bp_longjmp
1378         || b->type == bp_longjmp_resume)
1379       {
1380         b->enable = disabled;
1381         check_duplicates (b->address);
1382       }
1383 }
1384
1385 /* Call this after hitting the longjmp() breakpoint.  Use this to set a new
1386    breakpoint at the target of the jmp_buf.
1387
1388    FIXME - This ought to be done by setting a temporary breakpoint that gets
1389    deleted automatically...
1390 */
1391
1392 void
1393 set_longjmp_resume_breakpoint(pc, frame)
1394      CORE_ADDR pc;
1395      FRAME frame;
1396 {
1397   register struct breakpoint *b;
1398
1399   ALL_BREAKPOINTS (b)
1400     if (b->type == bp_longjmp_resume)
1401       {
1402         b->address = pc;
1403         b->enable = enabled;
1404         if (frame != NULL)
1405           b->frame = FRAME_FP(frame);
1406         else
1407           b->frame = 0;
1408         check_duplicates (b->address);
1409         return;
1410       }
1411 }
1412
1413 /* Set a breakpoint that will evaporate an end of command
1414    at address specified by SAL.
1415    Restrict it to frame FRAME if FRAME is nonzero.  */
1416
1417 struct breakpoint *
1418 set_momentary_breakpoint (sal, frame, type)
1419      struct symtab_and_line sal;
1420      FRAME frame;
1421      enum bptype type;
1422 {
1423   register struct breakpoint *b;
1424   b = set_raw_breakpoint (sal);
1425   b->type = type;
1426   b->enable = enabled;
1427   b->disposition = donttouch;
1428   b->frame = (frame ? FRAME_FP (frame) : 0);
1429   return b;
1430 }
1431
1432 #if 0
1433 void
1434 clear_momentary_breakpoints ()
1435 {
1436   register struct breakpoint *b;
1437   ALL_BREAKPOINTS (b)
1438     if (b->disposition == delete)
1439       {
1440         delete_breakpoint (b);
1441         break;
1442       }
1443 }
1444 #endif
1445 \f
1446 /* Tell the user we have just set a breakpoint B.  */
1447 static void
1448 mention (b)
1449      struct breakpoint *b;
1450 {
1451   switch (b->type)
1452     {
1453     case bp_watchpoint:
1454       printf_filtered ("Watchpoint %d: ", b->number);
1455       print_expression (b->exp, stdout);
1456       break;
1457     case bp_breakpoint:
1458       printf_filtered ("Breakpoint %d at %s", b->number,
1459                        local_hex_string(b->address));
1460       if (b->symtab)
1461         printf_filtered (": file %s, line %d.",
1462                          b->symtab->filename, b->line_number);
1463       break;
1464     case bp_until:
1465     case bp_finish:
1466     case bp_longjmp:
1467     case bp_longjmp_resume:
1468       break;
1469     }
1470   printf_filtered ("\n");
1471 }
1472
1473 #if 0
1474 /* Nobody calls this currently. */
1475 /* Set a breakpoint from a symtab and line.
1476    If TEMPFLAG is nonzero, it is a temporary breakpoint.
1477    ADDR_STRING is a malloc'd string holding the name of where we are
1478    setting the breakpoint.  This is used later to re-set it after the
1479    program is relinked and symbols are reloaded.
1480    Print the same confirmation messages that the breakpoint command prints.  */
1481
1482 void
1483 set_breakpoint (s, line, tempflag, addr_string)
1484      struct symtab *s;
1485      int line;
1486      int tempflag;
1487      char *addr_string;
1488 {
1489   register struct breakpoint *b;
1490   struct symtab_and_line sal;
1491   
1492   sal.symtab = s;
1493   sal.line = line;
1494   sal.pc = 0;
1495   resolve_sal_pc (&sal);                        /* Might error out */
1496   describe_other_breakpoints (sal.pc);
1497
1498   b = set_raw_breakpoint (sal);
1499   set_breakpoint_count (breakpoint_count + 1);
1500   b->number = breakpoint_count;
1501   b->type = bp_breakpoint;
1502   b->cond = 0;
1503   b->addr_string = addr_string;
1504   b->enable = enabled;
1505   b->disposition = tempflag ? delete : donttouch;
1506
1507   mention (b);
1508 }
1509 #endif /* 0 */
1510 \f
1511 /* Set a breakpoint according to ARG (function, linenum or *address)
1512    and make it temporary if TEMPFLAG is nonzero. */
1513
1514 static void
1515 break_command_1 (arg, tempflag, from_tty)
1516      char *arg;
1517      int tempflag, from_tty;
1518 {
1519   struct symtabs_and_lines sals;
1520   struct symtab_and_line sal;
1521   register struct expression *cond = 0;
1522   register struct breakpoint *b;
1523
1524   /* Pointers in arg to the start, and one past the end, of the condition.  */
1525   char *cond_start = NULL;
1526   char *cond_end;
1527   /* Pointers in arg to the start, and one past the end,
1528      of the address part.  */
1529   char *addr_start = NULL;
1530   char *addr_end;
1531   
1532   int i;
1533
1534   sals.sals = NULL;
1535   sals.nelts = 0;
1536
1537   sal.line = sal.pc = sal.end = 0;
1538   sal.symtab = 0;
1539
1540   /* If no arg given, or if first arg is 'if ', use the default breakpoint. */
1541
1542   if (!arg || (arg[0] == 'i' && arg[1] == 'f' 
1543                && (arg[2] == ' ' || arg[2] == '\t')))
1544     {
1545       if (default_breakpoint_valid)
1546         {
1547           sals.sals = (struct symtab_and_line *) 
1548             xmalloc (sizeof (struct symtab_and_line));
1549           sal.pc = default_breakpoint_address;
1550           sal.line = default_breakpoint_line;
1551           sal.symtab = default_breakpoint_symtab;
1552           sals.sals[0] = sal;
1553           sals.nelts = 1;
1554         }
1555       else
1556         error ("No default breakpoint address now.");
1557     }
1558   else
1559     {
1560       addr_start = arg;
1561
1562       /* Force almost all breakpoints to be in terms of the
1563          current_source_symtab (which is decode_line_1's default).  This
1564          should produce the results we want almost all of the time while
1565          leaving default_breakpoint_* alone.  */
1566       if (default_breakpoint_valid
1567           && (!current_source_symtab
1568               || (arg && (*arg == '+' || *arg == '-'))))
1569         sals = decode_line_1 (&arg, 1, default_breakpoint_symtab,
1570                               default_breakpoint_line);
1571       else
1572         sals = decode_line_1 (&arg, 1, (struct symtab *)NULL, 0);
1573
1574       addr_end = arg;
1575     }
1576   
1577   if (! sals.nelts) 
1578     return;
1579
1580   /* Resolve all line numbers to PC's, and verify that conditions
1581      can be parsed, before setting any breakpoints.  */
1582   for (i = 0; i < sals.nelts; i++)
1583     {
1584       resolve_sal_pc (&sals.sals[i]);
1585       
1586       while (arg && *arg)
1587         {
1588           if (arg[0] == 'i' && arg[1] == 'f'
1589               && (arg[2] == ' ' || arg[2] == '\t'))
1590             {
1591               arg += 2;
1592               cond_start = arg;
1593               cond = parse_exp_1 (&arg, block_for_pc (sals.sals[i].pc), 0);
1594               cond_end = arg;
1595             }
1596           else
1597             error ("Junk at end of arguments.");
1598         }
1599     }
1600
1601   /* Now set all the breakpoints.  */
1602   for (i = 0; i < sals.nelts; i++)
1603     {
1604       sal = sals.sals[i];
1605
1606       if (from_tty)
1607         describe_other_breakpoints (sal.pc);
1608
1609       b = set_raw_breakpoint (sal);
1610       set_breakpoint_count (breakpoint_count + 1);
1611       b->number = breakpoint_count;
1612       b->type = bp_breakpoint;
1613       b->cond = cond;
1614
1615       /* FIXME: We should add the filename if this is a static function
1616          and probably if it is a line number (the line numbers could
1617          have changed when we re-read symbols; possibly better to disable
1618          the breakpoint in that case).  */
1619       if (addr_start)
1620         b->addr_string = savestring (addr_start, addr_end - addr_start);
1621       if (cond_start)
1622         b->cond_string = savestring (cond_start, cond_end - cond_start);
1623                                      
1624       b->enable = enabled;
1625       b->disposition = tempflag ? delete : donttouch;
1626
1627       mention (b);
1628     }
1629
1630   if (sals.nelts > 1)
1631     {
1632       printf ("Multiple breakpoints were set.\n");
1633       printf ("Use the \"delete\" command to delete unwanted breakpoints.\n");
1634     }
1635   free ((PTR)sals.sals);
1636 }
1637
1638 /* Helper function for break_command_1 and disassemble_command.  */
1639
1640 void
1641 resolve_sal_pc (sal)
1642      struct symtab_and_line *sal;
1643 {
1644   CORE_ADDR pc;
1645
1646   if (sal->pc == 0 && sal->symtab != 0)
1647     {
1648       pc = find_line_pc (sal->symtab, sal->line);
1649       if (pc == 0)
1650         error ("No line %d in file \"%s\".",
1651                sal->line, sal->symtab->filename);
1652       sal->pc = pc;
1653     }
1654 }
1655
1656 void
1657 break_command (arg, from_tty)
1658      char *arg;
1659      int from_tty;
1660 {
1661   break_command_1 (arg, 0, from_tty);
1662 }
1663
1664 static void
1665 tbreak_command (arg, from_tty)
1666      char *arg;
1667      int from_tty;
1668 {
1669   break_command_1 (arg, 1, from_tty);
1670 }
1671
1672 /* ARGSUSED */
1673 static void
1674 watch_command (arg, from_tty)
1675      char *arg;
1676      int from_tty;
1677 {
1678   struct breakpoint *b;
1679   struct symtab_and_line sal;
1680   struct expression *exp;
1681   struct block *exp_valid_block;
1682   struct value *val;
1683
1684   sal.pc = 0;
1685   sal.symtab = NULL;
1686   sal.line = 0;
1687   
1688   /* Parse arguments.  */
1689   innermost_block = NULL;
1690   exp = parse_expression (arg);
1691   exp_valid_block = innermost_block;
1692   val = evaluate_expression (exp);
1693   release_value (val);
1694   if (VALUE_LAZY (val))
1695     value_fetch_lazy (val);
1696
1697   /* Now set up the breakpoint.  */
1698   b = set_raw_breakpoint (sal);
1699   set_breakpoint_count (breakpoint_count + 1);
1700   b->number = breakpoint_count;
1701   b->type = bp_watchpoint;
1702   b->disposition = donttouch;
1703   b->exp = exp;
1704   b->exp_valid_block = exp_valid_block;
1705   b->val = val;
1706   b->cond = 0;
1707   b->cond_string = NULL;
1708   b->exp_string = savestring (arg, strlen (arg));
1709   mention (b);
1710 }
1711 \f
1712 /*
1713  * Helper routine for the until_command routine in infcmd.c.  Here
1714  * because it uses the mechanisms of breakpoints.
1715  */
1716 /* ARGSUSED */
1717 void
1718 until_break_command (arg, from_tty)
1719      char *arg;
1720      int from_tty;
1721 {
1722   struct symtabs_and_lines sals;
1723   struct symtab_and_line sal;
1724   FRAME prev_frame = get_prev_frame (selected_frame);
1725   struct breakpoint *breakpoint;
1726   struct cleanup *old_chain;
1727
1728   clear_proceed_status ();
1729
1730   /* Set a breakpoint where the user wants it and at return from
1731      this function */
1732   
1733   if (default_breakpoint_valid)
1734     sals = decode_line_1 (&arg, 1, default_breakpoint_symtab,
1735                           default_breakpoint_line);
1736   else
1737     sals = decode_line_1 (&arg, 1, (struct symtab *)NULL, 0);
1738   
1739   if (sals.nelts != 1)
1740     error ("Couldn't get information on specified line.");
1741   
1742   sal = sals.sals[0];
1743   free ((PTR)sals.sals);                /* malloc'd, so freed */
1744   
1745   if (*arg)
1746     error ("Junk at end of arguments.");
1747   
1748   resolve_sal_pc (&sal);
1749   
1750   breakpoint = set_momentary_breakpoint (sal, selected_frame, bp_until);
1751   
1752   old_chain = make_cleanup(delete_breakpoint, breakpoint);
1753
1754   /* Keep within the current frame */
1755   
1756   if (prev_frame)
1757     {
1758       struct frame_info *fi;
1759       
1760       fi = get_frame_info (prev_frame);
1761       sal = find_pc_line (fi->pc, 0);
1762       sal.pc = fi->pc;
1763       breakpoint = set_momentary_breakpoint (sal, prev_frame, bp_until);
1764       make_cleanup(delete_breakpoint, breakpoint);
1765     }
1766   
1767   proceed (-1, -1, 0);
1768   do_cleanups(old_chain);
1769 }
1770 \f
1771 #if 0
1772 /* These aren't used; I don't konw what they were for.  */
1773 /* Set a breakpoint at the catch clause for NAME.  */
1774 static int
1775 catch_breakpoint (name)
1776      char *name;
1777 {
1778 }
1779
1780 static int
1781 disable_catch_breakpoint ()
1782 {
1783 }
1784
1785 static int
1786 delete_catch_breakpoint ()
1787 {
1788 }
1789
1790 static int
1791 enable_catch_breakpoint ()
1792 {
1793 }
1794 #endif /* 0 */
1795
1796 struct sal_chain
1797 {
1798   struct sal_chain *next;
1799   struct symtab_and_line sal;
1800 };
1801
1802 #if 0
1803 /* This isn't used; I don't know what it was for.  */
1804 /* For each catch clause identified in ARGS, run FUNCTION
1805    with that clause as an argument.  */
1806 static struct symtabs_and_lines
1807 map_catch_names (args, function)
1808      char *args;
1809      int (*function)();
1810 {
1811   register char *p = args;
1812   register char *p1;
1813   struct symtabs_and_lines sals;
1814 #if 0
1815   struct sal_chain *sal_chain = 0;
1816 #endif
1817
1818   if (p == 0)
1819     error_no_arg ("one or more catch names");
1820
1821   sals.nelts = 0;
1822   sals.sals = NULL;
1823
1824   while (*p)
1825     {
1826       p1 = p;
1827       /* Don't swallow conditional part.  */
1828       if (p1[0] == 'i' && p1[1] == 'f'
1829           && (p1[2] == ' ' || p1[2] == '\t'))
1830         break;
1831
1832       if (isalpha (*p1))
1833         {
1834           p1++;
1835           while (isalnum (*p1) || *p1 == '_' || *p1 == '$')
1836             p1++;
1837         }
1838
1839       if (*p1 && *p1 != ' ' && *p1 != '\t')
1840         error ("Arguments must be catch names.");
1841
1842       *p1 = 0;
1843 #if 0
1844       if (function (p))
1845         {
1846           struct sal_chain *next
1847             = (struct sal_chain *)alloca (sizeof (struct sal_chain));
1848           next->next = sal_chain;
1849           next->sal = get_catch_sal (p);
1850           sal_chain = next;
1851           goto win;
1852         }
1853 #endif
1854       printf ("No catch clause for exception %s.\n", p);
1855 #if 0
1856     win:
1857 #endif
1858       p = p1;
1859       while (*p == ' ' || *p == '\t') p++;
1860     }
1861 }
1862 #endif /* 0 */
1863
1864 /* This shares a lot of code with `print_frame_label_vars' from stack.c.  */
1865
1866 static struct symtabs_and_lines
1867 get_catch_sals (this_level_only)
1868      int this_level_only;
1869 {
1870   register struct blockvector *bl;
1871   register struct block *block;
1872   int index, have_default = 0;
1873   struct frame_info *fi;
1874   CORE_ADDR pc;
1875   struct symtabs_and_lines sals;
1876   struct sal_chain *sal_chain = 0;
1877   char *blocks_searched;
1878
1879   /* Not sure whether an error message is always the correct response,
1880      but it's better than a core dump.  */
1881   if (selected_frame == NULL)
1882     error ("No selected frame.");
1883   block = get_frame_block (selected_frame);
1884   fi = get_frame_info (selected_frame);
1885   pc = fi->pc;
1886
1887   sals.nelts = 0;
1888   sals.sals = NULL;
1889
1890   if (block == 0)
1891     error ("No symbol table info available.\n");
1892
1893   bl = blockvector_for_pc (BLOCK_END (block) - 4, &index);
1894   blocks_searched = (char *) alloca (BLOCKVECTOR_NBLOCKS (bl) * sizeof (char));
1895   memset (blocks_searched, 0, BLOCKVECTOR_NBLOCKS (bl) * sizeof (char));
1896
1897   while (block != 0)
1898     {
1899       CORE_ADDR end = BLOCK_END (block) - 4;
1900       int last_index;
1901
1902       if (bl != blockvector_for_pc (end, &index))
1903         error ("blockvector blotch");
1904       if (BLOCKVECTOR_BLOCK (bl, index) != block)
1905         error ("blockvector botch");
1906       last_index = BLOCKVECTOR_NBLOCKS (bl);
1907       index += 1;
1908
1909       /* Don't print out blocks that have gone by.  */
1910       while (index < last_index
1911              && BLOCK_END (BLOCKVECTOR_BLOCK (bl, index)) < pc)
1912         index++;
1913
1914       while (index < last_index
1915              && BLOCK_END (BLOCKVECTOR_BLOCK (bl, index)) < end)
1916         {
1917           if (blocks_searched[index] == 0)
1918             {
1919               struct block *b = BLOCKVECTOR_BLOCK (bl, index);
1920               int nsyms;
1921               register int i;
1922               register struct symbol *sym;
1923
1924               nsyms = BLOCK_NSYMS (b);
1925
1926               for (i = 0; i < nsyms; i++)
1927                 {
1928                   sym = BLOCK_SYM (b, i);
1929                   if (STREQ (SYMBOL_NAME (sym), "default"))
1930                     {
1931                       if (have_default)
1932                         continue;
1933                       have_default = 1;
1934                     }
1935                   if (SYMBOL_CLASS (sym) == LOC_LABEL)
1936                     {
1937                       struct sal_chain *next = (struct sal_chain *)
1938                         alloca (sizeof (struct sal_chain));
1939                       next->next = sal_chain;
1940                       next->sal = find_pc_line (SYMBOL_VALUE_ADDRESS (sym), 0);
1941                       sal_chain = next;
1942                     }
1943                 }
1944               blocks_searched[index] = 1;
1945             }
1946           index++;
1947         }
1948       if (have_default)
1949         break;
1950       if (sal_chain && this_level_only)
1951         break;
1952
1953       /* After handling the function's top-level block, stop.
1954          Don't continue to its superblock, the block of
1955          per-file symbols.  */
1956       if (BLOCK_FUNCTION (block))
1957         break;
1958       block = BLOCK_SUPERBLOCK (block);
1959     }
1960
1961   if (sal_chain)
1962     {
1963       struct sal_chain *tmp_chain;
1964
1965       /* Count the number of entries.  */
1966       for (index = 0, tmp_chain = sal_chain; tmp_chain;
1967            tmp_chain = tmp_chain->next)
1968         index++;
1969
1970       sals.nelts = index;
1971       sals.sals = (struct symtab_and_line *)
1972         xmalloc (index * sizeof (struct symtab_and_line));
1973       for (index = 0; sal_chain; sal_chain = sal_chain->next, index++)
1974         sals.sals[index] = sal_chain->sal;
1975     }
1976
1977   return sals;
1978 }
1979
1980 /* Commands to deal with catching exceptions.  */
1981
1982 static void
1983 catch_command_1 (arg, tempflag, from_tty)
1984      char *arg;
1985      int tempflag;
1986      int from_tty;
1987 {
1988   /* First, translate ARG into something we can deal with in terms
1989      of breakpoints.  */
1990
1991   struct symtabs_and_lines sals;
1992   struct symtab_and_line sal;
1993   register struct expression *cond = 0;
1994   register struct breakpoint *b;
1995   char *save_arg;
1996   int i;
1997
1998   sal.line = sal.pc = sal.end = 0;
1999   sal.symtab = 0;
2000
2001   /* If no arg given, or if first arg is 'if ', all active catch clauses
2002      are breakpointed. */
2003
2004   if (!arg || (arg[0] == 'i' && arg[1] == 'f' 
2005                && (arg[2] == ' ' || arg[2] == '\t')))
2006     {
2007       /* Grab all active catch clauses.  */
2008       sals = get_catch_sals (0);
2009     }
2010   else
2011     {
2012       /* Grab selected catch clauses.  */
2013       error ("catch NAME not implemeneted");
2014 #if 0
2015       /* This isn't used; I don't know what it was for.  */
2016       sals = map_catch_names (arg, catch_breakpoint);
2017 #endif
2018     }
2019
2020   if (! sals.nelts) 
2021     return;
2022
2023   save_arg = arg;
2024   for (i = 0; i < sals.nelts; i++)
2025     {
2026       resolve_sal_pc (&sals.sals[i]);
2027       
2028       while (arg && *arg)
2029         {
2030           if (arg[0] == 'i' && arg[1] == 'f'
2031               && (arg[2] == ' ' || arg[2] == '\t'))
2032             cond = parse_exp_1 ((arg += 2, &arg), 
2033                                 block_for_pc (sals.sals[i].pc), 0);
2034           else
2035             error ("Junk at end of arguments.");
2036         }
2037       arg = save_arg;
2038     }
2039
2040   for (i = 0; i < sals.nelts; i++)
2041     {
2042       sal = sals.sals[i];
2043
2044       if (from_tty)
2045         describe_other_breakpoints (sal.pc);
2046
2047       b = set_raw_breakpoint (sal);
2048       set_breakpoint_count (breakpoint_count + 1);
2049       b->number = breakpoint_count;
2050       b->type = bp_breakpoint;
2051       b->cond = cond;
2052       b->enable = enabled;
2053       b->disposition = tempflag ? delete : donttouch;
2054
2055       printf ("Breakpoint %d at %s", b->number, local_hex_string(b->address));
2056       if (b->symtab)
2057         printf (": file %s, line %d.", b->symtab->filename, b->line_number);
2058       printf ("\n");
2059     }
2060
2061   if (sals.nelts > 1)
2062     {
2063       printf ("Multiple breakpoints were set.\n");
2064       printf ("Use the \"delete\" command to delete unwanted breakpoints.\n");
2065     }
2066   free ((PTR)sals.sals);
2067 }
2068
2069 #if 0
2070 /* These aren't used; I don't know what they were for.  */
2071 /* Disable breakpoints on all catch clauses described in ARGS.  */
2072 static void
2073 disable_catch (args)
2074      char *args;
2075 {
2076   /* Map the disable command to catch clauses described in ARGS.  */
2077 }
2078
2079 /* Enable breakpoints on all catch clauses described in ARGS.  */
2080 static void
2081 enable_catch (args)
2082      char *args;
2083 {
2084   /* Map the disable command to catch clauses described in ARGS.  */
2085 }
2086
2087 /* Delete breakpoints on all catch clauses in the active scope.  */
2088 static void
2089 delete_catch (args)
2090      char *args;
2091 {
2092   /* Map the delete command to catch clauses described in ARGS.  */
2093 }
2094 #endif /* 0 */
2095
2096 static void
2097 catch_command (arg, from_tty)
2098      char *arg;
2099      int from_tty;
2100 {
2101   catch_command_1 (arg, 0, from_tty);
2102 }
2103 \f
2104 static void
2105 clear_command (arg, from_tty)
2106      char *arg;
2107      int from_tty;
2108 {
2109   register struct breakpoint *b, *b1;
2110   struct symtabs_and_lines sals;
2111   struct symtab_and_line sal;
2112   register struct breakpoint *found;
2113   int i;
2114
2115   if (arg)
2116     {
2117       sals = decode_line_spec (arg, 1);
2118     }
2119   else
2120     {
2121       sals.sals = (struct symtab_and_line *) xmalloc (sizeof (struct symtab_and_line));
2122       sal.line = default_breakpoint_line;
2123       sal.symtab = default_breakpoint_symtab;
2124       sal.pc = 0;
2125       if (sal.symtab == 0)
2126         error ("No source file specified.");
2127
2128       sals.sals[0] = sal;
2129       sals.nelts = 1;
2130     }
2131
2132   for (i = 0; i < sals.nelts; i++)
2133     {
2134       /* If exact pc given, clear bpts at that pc.
2135          But if sal.pc is zero, clear all bpts on specified line.  */
2136       sal = sals.sals[i];
2137       found = (struct breakpoint *) 0;
2138       while (breakpoint_chain
2139              && (sal.pc ? breakpoint_chain->address == sal.pc
2140                  : (breakpoint_chain->symtab == sal.symtab
2141                     && breakpoint_chain->line_number == sal.line)))
2142         {
2143           b1 = breakpoint_chain;
2144           breakpoint_chain = b1->next;
2145           b1->next = found;
2146           found = b1;
2147         }
2148
2149       ALL_BREAKPOINTS (b)
2150         while (b->next
2151                && b->next->type != bp_watchpoint
2152                && (sal.pc ? b->next->address == sal.pc
2153                    : (b->next->symtab == sal.symtab
2154                       && b->next->line_number == sal.line)))
2155           {
2156             b1 = b->next;
2157             b->next = b1->next;
2158             b1->next = found;
2159             found = b1;
2160           }
2161
2162       if (found == 0)
2163         {
2164           if (arg)
2165             error ("No breakpoint at %s.", arg);
2166           else
2167             error ("No breakpoint at this line.");
2168         }
2169
2170       if (found->next) from_tty = 1; /* Always report if deleted more than one */
2171       if (from_tty) printf ("Deleted breakpoint%s ", found->next ? "s" : "");
2172       while (found)
2173         {
2174           if (from_tty) printf ("%d ", found->number);
2175           b1 = found->next;
2176           delete_breakpoint (found);
2177           found = b1;
2178         }
2179       if (from_tty) putchar ('\n');
2180     }
2181   free ((PTR)sals.sals);
2182 }
2183 \f
2184 /* Delete breakpoint in BS if they are `delete' breakpoints.
2185    This is called after any breakpoint is hit, or after errors.  */
2186
2187 void
2188 breakpoint_auto_delete (bs)
2189      bpstat bs;
2190 {
2191   for (; bs; bs = bs->next)
2192     if (bs->breakpoint_at && bs->breakpoint_at->disposition == delete)
2193       delete_breakpoint (bs->breakpoint_at);
2194 }
2195
2196 /* Delete a breakpoint and clean up all traces of it in the data structures. */
2197
2198 void
2199 delete_breakpoint (bpt)
2200      struct breakpoint *bpt;
2201 {
2202   register struct breakpoint *b;
2203   register bpstat bs;
2204
2205   if (bpt->inserted)
2206       target_remove_breakpoint(bpt->address, bpt->shadow_contents);
2207
2208   if (breakpoint_chain == bpt)
2209     breakpoint_chain = bpt->next;
2210
2211   ALL_BREAKPOINTS (b)
2212     if (b->next == bpt)
2213       {
2214         b->next = bpt->next;
2215         break;
2216       }
2217
2218   check_duplicates (bpt->address);
2219
2220   free_command_lines (&bpt->commands);
2221   if (bpt->cond)
2222     free ((PTR)bpt->cond);
2223   if (bpt->cond_string != NULL)
2224     free ((PTR)bpt->cond_string);
2225   if (bpt->addr_string != NULL)
2226     free ((PTR)bpt->addr_string);
2227   if (bpt->exp_string != NULL)
2228     free ((PTR)bpt->exp_string);
2229
2230   if (xgdb_verbose && bpt->type == bp_breakpoint)
2231     printf ("breakpoint #%d deleted\n", bpt->number);
2232
2233   /* Be sure no bpstat's are pointing at it after it's been freed.  */
2234   /* FIXME, how can we find all bpstat's?  We just check stop_bpstat for now. */
2235   for (bs = stop_bpstat; bs; bs = bs->next)
2236     if (bs->breakpoint_at == bpt)
2237       bs->breakpoint_at = NULL;
2238   free ((PTR)bpt);
2239 }
2240
2241 static void
2242 delete_command (arg, from_tty)
2243      char *arg;
2244      int from_tty;
2245 {
2246
2247   if (arg == 0)
2248     {
2249       /* Ask user only if there are some breakpoints to delete.  */
2250       if (!from_tty
2251           || (breakpoint_chain && query ("Delete all breakpoints? ", 0, 0)))
2252         {
2253           /* No arg; clear all breakpoints.  */
2254           while (breakpoint_chain)
2255             delete_breakpoint (breakpoint_chain);
2256         }
2257     }
2258   else
2259     map_breakpoint_numbers (arg, delete_breakpoint);
2260 }
2261
2262 /* Reset a breakpoint given it's struct breakpoint * BINT.
2263    The value we return ends up being the return value from catch_errors.
2264    Unused in this case.  */
2265
2266 static int
2267 breakpoint_re_set_one (bint)
2268      char *bint;
2269 {
2270   struct breakpoint *b = (struct breakpoint *)bint;  /* get past catch_errs */
2271   int i;
2272   struct symtabs_and_lines sals;
2273   char *s;
2274   enum enable save_enable;
2275
2276   switch (b->type)
2277     {
2278     case bp_breakpoint:
2279       if (b->addr_string == NULL)
2280         {
2281           /* Anything without a string can't be re-set. */
2282           delete_breakpoint (b);
2283           return 0;
2284         }
2285       /* In case we have a problem, disable this breakpoint.  We'll restore
2286          its status if we succeed.  */
2287       save_enable = b->enable;
2288       b->enable = disabled;
2289
2290       s = b->addr_string;
2291       sals = decode_line_1 (&s, 1, (struct symtab *)NULL, 0);
2292       for (i = 0; i < sals.nelts; i++)
2293         {
2294           resolve_sal_pc (&sals.sals[i]);
2295           if (b->symtab != sals.sals[i].symtab
2296               || b->line_number != sals.sals[i].line
2297               || b->address != sals.sals[i].pc)
2298             {
2299               b->symtab = sals.sals[i].symtab;
2300               b->line_number = sals.sals[i].line;
2301               b->address = sals.sals[i].pc;
2302
2303               if (b->cond_string != NULL)
2304                 {
2305                   s = b->cond_string;
2306                   if (b->cond)
2307                     free ((PTR)b->cond);
2308                   b->cond = parse_exp_1 (&s, block_for_pc (sals.sals[i].pc), 0);
2309                 }
2310           
2311               check_duplicates (b->address);
2312
2313               mention (b);
2314             }
2315           b->enable = save_enable;      /* Restore it, this worked. */
2316         }
2317       free ((PTR)sals.sals);
2318       break;
2319
2320     case bp_watchpoint:
2321       innermost_block = NULL;
2322       /* The issue arises of what context to evaluate this in.  The same
2323          one as when it was set, but what does that mean when symbols have
2324          been re-read?  We could save the filename and functionname, but
2325          if the context is more local than that, the best we could do would
2326          be something like how many levels deep and which index at that
2327          particular level, but that's going to be less stable than filenames
2328          or functionnames.  */
2329       /* So for now, just use a global context.  */
2330       b->exp = parse_expression (b->exp_string);
2331       b->exp_valid_block = innermost_block;
2332       b->val = evaluate_expression (b->exp);
2333       release_value (b->val);
2334       if (VALUE_LAZY (b->val))
2335         value_fetch_lazy (b->val);
2336
2337       if (b->cond_string != NULL)
2338         {
2339           s = b->cond_string;
2340           b->cond = parse_exp_1 (&s, (struct block *)0, 0);
2341         }
2342       mention (b);
2343       break;
2344
2345     default:
2346       printf_filtered ("Deleting unknown breakpoint type %d\n", b->type);
2347       /* fall through */
2348     case bp_until:
2349     case bp_finish:
2350     case bp_longjmp:
2351     case bp_longjmp_resume:
2352       delete_breakpoint (b);
2353       break;
2354     }
2355
2356   return 0;
2357 }
2358
2359 /* Re-set all breakpoints after symbols have been re-loaded.  */
2360 void
2361 breakpoint_re_set ()
2362 {
2363   struct breakpoint *b, *temp;
2364   static char message1[] = "Error in re-setting breakpoint %d:\n";
2365   char message[sizeof (message1) + 30 /* slop */];
2366   
2367   /* If we have no current source symtab, and we have any breakpoints,
2368      go through the work of making a source context.  */
2369   if (current_source_symtab == NULL && breakpoint_chain != 0)
2370     {
2371       select_source_symtab (NULL);
2372     }
2373
2374   ALL_BREAKPOINTS_SAFE (b, temp)
2375     {
2376       sprintf (message, message1, b->number);   /* Format possible error msg */
2377       catch_errors (breakpoint_re_set_one, (char *) b, message);
2378     }
2379
2380   create_longjmp_breakpoint("longjmp");
2381   create_longjmp_breakpoint("_longjmp");
2382   create_longjmp_breakpoint("siglongjmp");
2383   create_longjmp_breakpoint(NULL);
2384
2385 #if 0
2386   /* Took this out (temporaliy at least), since it produces an extra 
2387      blank line at startup. This messes up the gdbtests. -PB */
2388   /* Blank line to finish off all those mention() messages we just printed.  */
2389   printf_filtered ("\n");
2390 #endif
2391 }
2392 \f
2393 /* Set ignore-count of breakpoint number BPTNUM to COUNT.
2394    If from_tty is nonzero, it prints a message to that effect,
2395    which ends with a period (no newline).  */
2396
2397 void
2398 set_ignore_count (bptnum, count, from_tty)
2399      int bptnum, count, from_tty;
2400 {
2401   register struct breakpoint *b;
2402
2403   if (count < 0)
2404     count = 0;
2405
2406   ALL_BREAKPOINTS (b)
2407     if (b->number == bptnum)
2408       {
2409         b->ignore_count = count;
2410         if (!from_tty)
2411           return;
2412         else if (count == 0)
2413           printf_filtered ("Will stop next time breakpoint %d is reached.",
2414                            bptnum);
2415         else if (count == 1)
2416           printf_filtered ("Will ignore next crossing of breakpoint %d.",
2417                            bptnum);
2418         else
2419           printf_filtered ("Will ignore next %d crossings of breakpoint %d.",
2420                   count, bptnum);
2421         return;
2422       }
2423
2424   error ("No breakpoint number %d.", bptnum);
2425 }
2426
2427 /* Clear the ignore counts of all breakpoints.  */
2428 void
2429 breakpoint_clear_ignore_counts ()
2430 {
2431   struct breakpoint *b;
2432
2433   ALL_BREAKPOINTS (b)
2434     b->ignore_count = 0;
2435 }
2436
2437 /* Command to set ignore-count of breakpoint N to COUNT.  */
2438
2439 static void
2440 ignore_command (args, from_tty)
2441      char *args;
2442      int from_tty;
2443 {
2444   char *p = args;
2445   register int num;
2446
2447   if (p == 0)
2448     error_no_arg ("a breakpoint number");
2449   
2450   num = get_number (&p);
2451
2452   if (*p == 0)
2453     error ("Second argument (specified ignore-count) is missing.");
2454
2455   set_ignore_count (num,
2456                     longest_to_int (value_as_long (parse_and_eval (p))),
2457                     from_tty);
2458   printf_filtered ("\n");
2459 }
2460 \f
2461 /* Call FUNCTION on each of the breakpoints
2462    whose numbers are given in ARGS.  */
2463
2464 static void
2465 map_breakpoint_numbers (args, function)
2466      char *args;
2467      void (*function) PARAMS ((struct breakpoint *));
2468 {
2469   register char *p = args;
2470   char *p1;
2471   register int num;
2472   register struct breakpoint *b;
2473
2474   if (p == 0)
2475     error_no_arg ("one or more breakpoint numbers");
2476
2477   while (*p)
2478     {
2479       p1 = p;
2480       
2481       num = get_number (&p1);
2482
2483       ALL_BREAKPOINTS (b)
2484         if (b->number == num)
2485           {
2486             function (b);
2487             goto win;
2488           }
2489       printf ("No breakpoint number %d.\n", num);
2490     win:
2491       p = p1;
2492     }
2493 }
2494
2495 static void
2496 enable_breakpoint (bpt)
2497      struct breakpoint *bpt;
2498 {
2499   bpt->enable = enabled;
2500
2501   if (xgdb_verbose && bpt->type == bp_breakpoint)
2502     printf ("breakpoint #%d enabled\n", bpt->number);
2503
2504   check_duplicates (bpt->address);
2505   if (bpt->type == bp_watchpoint)
2506     {
2507       if (bpt->exp_valid_block != NULL
2508        && !contained_in (get_selected_block (), bpt->exp_valid_block))
2509         {
2510           printf_filtered ("\
2511 Cannot enable watchpoint %d because the block in which its expression\n\
2512 is valid is not currently in scope.\n", bpt->number);
2513           bpt->enable = disabled;
2514           return;
2515         }
2516
2517       value_free (bpt->val);
2518
2519       bpt->val = evaluate_expression (bpt->exp);
2520       release_value (bpt->val);
2521       if (VALUE_LAZY (bpt->val))
2522         value_fetch_lazy (bpt->val);
2523     }
2524 }
2525
2526 /* ARGSUSED */
2527 static void
2528 enable_command (args, from_tty)
2529      char *args;
2530      int from_tty;
2531 {
2532   struct breakpoint *bpt;
2533   if (args == 0)
2534     ALL_BREAKPOINTS (bpt)
2535       switch (bpt->type)
2536         {
2537         case bp_breakpoint:
2538         case bp_watchpoint:
2539           enable_breakpoint (bpt);
2540         default:
2541           continue;
2542         }
2543   else
2544     map_breakpoint_numbers (args, enable_breakpoint);
2545 }
2546
2547 static void
2548 disable_breakpoint (bpt)
2549      struct breakpoint *bpt;
2550 {
2551   bpt->enable = disabled;
2552
2553   if (xgdb_verbose && bpt->type == bp_breakpoint)
2554     printf_filtered ("breakpoint #%d disabled\n", bpt->number);
2555
2556   check_duplicates (bpt->address);
2557 }
2558
2559 /* ARGSUSED */
2560 static void
2561 disable_command (args, from_tty)
2562      char *args;
2563      int from_tty;
2564 {
2565   register struct breakpoint *bpt;
2566   if (args == 0)
2567     ALL_BREAKPOINTS (bpt)
2568       switch (bpt->type)
2569         {
2570         case bp_breakpoint:
2571         case bp_watchpoint:
2572           disable_breakpoint (bpt);
2573         default:
2574           continue;
2575         }
2576   else
2577     map_breakpoint_numbers (args, disable_breakpoint);
2578 }
2579
2580 static void
2581 enable_once_breakpoint (bpt)
2582      struct breakpoint *bpt;
2583 {
2584   bpt->enable = enabled;
2585   bpt->disposition = disable;
2586
2587   check_duplicates (bpt->address);
2588 }
2589
2590 /* ARGSUSED */
2591 static void
2592 enable_once_command (args, from_tty)
2593      char *args;
2594      int from_tty;
2595 {
2596   map_breakpoint_numbers (args, enable_once_breakpoint);
2597 }
2598
2599 static void
2600 enable_delete_breakpoint (bpt)
2601      struct breakpoint *bpt;
2602 {
2603   bpt->enable = enabled;
2604   bpt->disposition = delete;
2605
2606   check_duplicates (bpt->address);
2607 }
2608
2609 /* ARGSUSED */
2610 static void
2611 enable_delete_command (args, from_tty)
2612      char *args;
2613      int from_tty;
2614 {
2615   map_breakpoint_numbers (args, enable_delete_breakpoint);
2616 }
2617 \f
2618 /*
2619  * Use default_breakpoint_'s, or nothing if they aren't valid.
2620  */
2621 struct symtabs_and_lines
2622 decode_line_spec_1 (string, funfirstline)
2623      char *string;
2624      int funfirstline;
2625 {
2626   struct symtabs_and_lines sals;
2627   if (string == 0)
2628     error ("Empty line specification.");
2629   if (default_breakpoint_valid)
2630     sals = decode_line_1 (&string, funfirstline,
2631                           default_breakpoint_symtab, default_breakpoint_line);
2632   else
2633     sals = decode_line_1 (&string, funfirstline, (struct symtab *)NULL, 0);
2634   if (*string)
2635     error ("Junk at end of line specification: %s", string);
2636   return sals;
2637 }
2638 \f
2639 void
2640 _initialize_breakpoint ()
2641 {
2642   breakpoint_chain = 0;
2643   /* Don't bother to call set_breakpoint_count.  $bpnum isn't useful
2644      before a breakpoint is set.  */
2645   breakpoint_count = 0;
2646
2647   add_com ("ignore", class_breakpoint, ignore_command,
2648            "Set ignore-count of breakpoint number N to COUNT.");
2649
2650   add_com ("commands", class_breakpoint, commands_command,
2651            "Set commands to be executed when a breakpoint is hit.\n\
2652 Give breakpoint number as argument after \"commands\".\n\
2653 With no argument, the targeted breakpoint is the last one set.\n\
2654 The commands themselves follow starting on the next line.\n\
2655 Type a line containing \"end\" to indicate the end of them.\n\
2656 Give \"silent\" as the first line to make the breakpoint silent;\n\
2657 then no output is printed when it is hit, except what the commands print.");
2658
2659   add_com ("condition", class_breakpoint, condition_command,
2660            "Specify breakpoint number N to break only if COND is true.\n\
2661 N is an integer; COND is an expression to be evaluated whenever\n\
2662 breakpoint N is reached.  ");
2663
2664   add_com ("tbreak", class_breakpoint, tbreak_command,
2665            "Set a temporary breakpoint.  Args like \"break\" command.\n\
2666 Like \"break\" except the breakpoint is only enabled temporarily,\n\
2667 so it will be disabled when hit.  Equivalent to \"break\" followed\n\
2668 by using \"enable once\" on the breakpoint number.");
2669
2670   add_prefix_cmd ("enable", class_breakpoint, enable_command,
2671                   "Enable some breakpoints.\n\
2672 Give breakpoint numbers (separated by spaces) as arguments.\n\
2673 With no subcommand, breakpoints are enabled until you command otherwise.\n\
2674 This is used to cancel the effect of the \"disable\" command.\n\
2675 With a subcommand you can enable temporarily.",
2676                   &enablelist, "enable ", 1, &cmdlist);
2677
2678   add_abbrev_prefix_cmd ("breakpoints", class_breakpoint, enable_command,
2679                   "Enable some breakpoints.\n\
2680 Give breakpoint numbers (separated by spaces) as arguments.\n\
2681 This is used to cancel the effect of the \"disable\" command.\n\
2682 May be abbreviated to simply \"enable\".\n",
2683                   &enablebreaklist, "enable breakpoints ", 1, &enablelist);
2684
2685   add_cmd ("once", no_class, enable_once_command,
2686            "Enable breakpoints for one hit.  Give breakpoint numbers.\n\
2687 If a breakpoint is hit while enabled in this fashion, it becomes disabled.\n\
2688 See the \"tbreak\" command which sets a breakpoint and enables it once.",
2689            &enablebreaklist);
2690
2691   add_cmd ("delete", no_class, enable_delete_command,
2692            "Enable breakpoints and delete when hit.  Give breakpoint numbers.\n\
2693 If a breakpoint is hit while enabled in this fashion, it is deleted.",
2694            &enablebreaklist);
2695
2696   add_cmd ("delete", no_class, enable_delete_command,
2697            "Enable breakpoints and delete when hit.  Give breakpoint numbers.\n\
2698 If a breakpoint is hit while enabled in this fashion, it is deleted.",
2699            &enablelist);
2700
2701   add_cmd ("once", no_class, enable_once_command,
2702            "Enable breakpoints for one hit.  Give breakpoint numbers.\n\
2703 If a breakpoint is hit while enabled in this fashion, it becomes disabled.\n\
2704 See the \"tbreak\" command which sets a breakpoint and enables it once.",
2705            &enablelist);
2706
2707   add_prefix_cmd ("disable", class_breakpoint, disable_command,
2708            "Disable some breakpoints.\n\
2709 Arguments are breakpoint numbers with spaces in between.\n\
2710 To disable all breakpoints, give no argument.\n\
2711 A disabled breakpoint is not forgotten, but has no effect until reenabled.",
2712                   &disablelist, "disable ", 1, &cmdlist);
2713   add_com_alias ("dis", "disable", class_breakpoint, 1);
2714   add_com_alias ("disa", "disable", class_breakpoint, 1);
2715
2716   add_cmd ("breakpoints", class_alias, disable_command,
2717            "Disable some breakpoints.\n\
2718 Arguments are breakpoint numbers with spaces in between.\n\
2719 To disable all breakpoints, give no argument.\n\
2720 A disabled breakpoint is not forgotten, but has no effect until reenabled.\n\
2721 This command may be abbreviated \"disable\".",
2722            &disablelist);
2723
2724   add_prefix_cmd ("delete", class_breakpoint, delete_command,
2725            "Delete some breakpoints or auto-display expressions.\n\
2726 Arguments are breakpoint numbers with spaces in between.\n\
2727 To delete all breakpoints, give no argument.\n\
2728 \n\
2729 Also a prefix command for deletion of other GDB objects.\n\
2730 The \"unset\" command is also an alias for \"delete\".",
2731                   &deletelist, "delete ", 1, &cmdlist);
2732   add_com_alias ("d", "delete", class_breakpoint, 1);
2733
2734   add_cmd ("breakpoints", class_alias, delete_command,
2735            "Delete some breakpoints or auto-display expressions.\n\
2736 Arguments are breakpoint numbers with spaces in between.\n\
2737 To delete all breakpoints, give no argument.\n\
2738 This command may be abbreviated \"delete\".",
2739            &deletelist);
2740
2741   add_com ("clear", class_breakpoint, clear_command,
2742            "Clear breakpoint at specified line or function.\n\
2743 Argument may be line number, function name, or \"*\" and an address.\n\
2744 If line number is specified, all breakpoints in that line are cleared.\n\
2745 If function is specified, breakpoints at beginning of function are cleared.\n\
2746 If an address is specified, breakpoints at that address are cleared.\n\n\
2747 With no argument, clears all breakpoints in the line that the selected frame\n\
2748 is executing in.\n\
2749 \n\
2750 See also the \"delete\" command which clears breakpoints by number.");
2751
2752   add_com ("break", class_breakpoint, break_command,
2753            "Set breakpoint at specified line or function.\n\
2754 Argument may be line number, function name, or \"*\" and an address.\n\
2755 If line number is specified, break at start of code for that line.\n\
2756 If function is specified, break at start of code for that function.\n\
2757 If an address is specified, break at that exact address.\n\
2758 With no arg, uses current execution address of selected stack frame.\n\
2759 This is useful for breaking on return to a stack frame.\n\
2760 \n\
2761 Multiple breakpoints at one place are permitted, and useful if conditional.\n\
2762 \n\
2763 Do \"help breakpoints\" for info on other commands dealing with breakpoints.");
2764   add_com_alias ("b", "break", class_run, 1);
2765   add_com_alias ("br", "break", class_run, 1);
2766   add_com_alias ("bre", "break", class_run, 1);
2767   add_com_alias ("brea", "break", class_run, 1);
2768
2769   add_info ("breakpoints", breakpoints_info,
2770             "Status of user-settable breakpoints, or breakpoint number NUMBER.\n\
2771 The \"Type\" column indicates one of:\n\
2772 \tbreakpoint     - normal breakpoint\n\
2773 \twatchpoint     - watchpoint\n\
2774 The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
2775 the disposition of the breakpoint after it gets hit.  \"dis\" means that the\n\
2776 breakpoint will be disabled.  The \"Address\" and \"What\" columns indicate the\n\
2777 address and file/line number respectively.\n\n\
2778 Convenience variable \"$_\" and default examine address for \"x\"\n\
2779 are set to the address of the last breakpoint listed.\n\n\
2780 Convenience variable \"$bpnum\" contains the number of the last\n\
2781 breakpoint set.");
2782
2783 #if MAINTENANCE_CMDS
2784
2785   add_cmd ("breakpoints", class_maintenance, maintenance_info_breakpoints,
2786             "Status of all breakpoints, or breakpoint number NUMBER.\n\
2787 The \"Type\" column indicates one of:\n\
2788 \tbreakpoint     - normal breakpoint\n\
2789 \twatchpoint     - watchpoint\n\
2790 \tlongjmp        - internal breakpoint used to step through longjmp()\n\
2791 \tlongjmp resume - internal breakpoint at the target of longjmp()\n\
2792 \tuntil          - internal breakpoint used by the \"until\" command\n\
2793 \tfinish         - internal breakpoint used by the \"finish\" command\n\
2794 The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
2795 the disposition of the breakpoint after it gets hit.  \"dis\" means that the\n\
2796 breakpoint will be disabled.  The \"Address\" and \"What\" columns indicate the\n\
2797 address and file/line number respectively.\n\n\
2798 Convenience variable \"$_\" and default examine address for \"x\"\n\
2799 are set to the address of the last breakpoint listed.\n\n\
2800 Convenience variable \"$bpnum\" contains the number of the last\n\
2801 breakpoint set.",
2802            &maintenanceinfolist);
2803
2804 #endif  /* MAINTENANCE_CMDS */
2805
2806   add_com ("catch", class_breakpoint, catch_command,
2807          "Set breakpoints to catch exceptions that are raised.\n\
2808 Argument may be a single exception to catch, multiple exceptions\n\
2809 to catch, or the default exception \"default\".  If no arguments\n\
2810 are given, breakpoints are set at all exception handlers catch clauses\n\
2811 within the current scope.\n\
2812 \n\
2813 A condition specified for the catch applies to all breakpoints set\n\
2814 with this command\n\
2815 \n\
2816 Do \"help breakpoints\" for info on other commands dealing with breakpoints.");
2817
2818   add_com ("watch", class_breakpoint, watch_command,
2819            "Set a watchpoint for an expression.\n\
2820 A watchpoint stops execution of your program whenever the value of\n\
2821 an expression changes.");
2822
2823   add_info ("watchpoints", breakpoints_info,
2824             "Synonym for ``info breakpoints''.");
2825 }
2826
2827 /* OK, when we call objfile_relocate, we need to relocate breakpoints
2828    too.  breakpoint_re_set is not a good choice--for example, if
2829    addr_string contains just a line number without a file name the
2830    breakpoint might get set in a different file.  In general, there is
2831    no need to go all the way back to the user's string (though this might
2832    work if some effort were made to canonicalize it), since symtabs and
2833    everything except addresses are still valid.
2834
2835    Probably the best way to solve this is to have each breakpoint save
2836    the objfile and the section number that was used to set it (if set
2837    by "*addr", probably it is best to use find_pc_line to get a symtab
2838    and use the objfile and block_line_section for that symtab).  Then
2839    objfile_relocate can call fixup_breakpoints with the objfile and
2840    the new_offsets, and it can relocate only the appropriate breakpoints.  */
2841
2842 #ifdef IBM6000_TARGET
2843 /* But for now, just kludge it based on the concept that before an
2844    objfile is relocated the breakpoint is below 0x10000000, and afterwards
2845    it is higher, so that way we only relocate each breakpoint once.  */
2846
2847 void
2848 fixup_breakpoints (low, high, delta)
2849   CORE_ADDR low;
2850   CORE_ADDR high;
2851   CORE_ADDR delta;
2852 {
2853   struct breakpoint *b;
2854
2855   ALL_BREAKPOINTS (b)
2856     {
2857      if (b->address >= low && b->address <= high)
2858        b->address += delta;
2859     }
2860 }
2861 #endif