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