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