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