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