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