* breakpoint.c (watch_command): Use (CORE_ADDR)0, not NULL, for
[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           printf_filtered ("Num Type           Disp Enb %sWhat\n",
1543                            addressprint ? "Address    " : "");
1544
1545         printf_filtered ("%-3d %-14s %-4s %-3c ",
1546                          b->number,
1547                          bptypes[(int)b->type],
1548                          bpdisps[(int)b->disposition],
1549                          bpenables[(int)b->enable]);
1550         strcpy (wrap_indent, "                           ");
1551         if (addressprint)
1552           strcat (wrap_indent, "           ");
1553         switch (b->type)
1554           {
1555           case bp_watchpoint:
1556           case bp_hardware_watchpoint:
1557             print_expression (b->exp, gdb_stdout);
1558             break;
1559
1560           case bp_breakpoint:
1561           case bp_until:
1562           case bp_finish:
1563           case bp_longjmp:
1564           case bp_longjmp_resume:
1565           case bp_step_resume:
1566           case bp_through_sigtramp:
1567           case bp_watchpoint_scope:
1568           case bp_call_dummy:
1569             if (addressprint)
1570               printf_filtered ("%s ", local_hex_string_custom ((unsigned long) b->address, "08l"));
1571
1572             last_addr = b->address;
1573             if (b->source_file)
1574               {
1575                 sym = find_pc_function (b->address);
1576                 if (sym)
1577                   {
1578                     fputs_filtered ("in ", gdb_stdout);
1579                     fputs_filtered (SYMBOL_SOURCE_NAME (sym), gdb_stdout);
1580                     wrap_here (wrap_indent);
1581                     fputs_filtered (" at ", gdb_stdout);
1582                   }
1583                 fputs_filtered (b->source_file, gdb_stdout);
1584                 printf_filtered (":%d", b->line_number);
1585               }
1586             else
1587               print_address_symbolic (b->address, gdb_stdout, demangle, " ");
1588             break;
1589           }
1590
1591         printf_filtered ("\n");
1592
1593         if (b->frame)
1594           {
1595             printf_filtered ("\tstop only in stack frame at ");
1596             print_address_numeric (b->frame, gdb_stdout);
1597             printf_filtered ("\n");
1598           }
1599         if (b->cond)
1600           {
1601             printf_filtered ("\tstop only if ");
1602             print_expression (b->cond, gdb_stdout);
1603             printf_filtered ("\n");
1604           }
1605         if (b->ignore_count)
1606           printf_filtered ("\tignore next %d hits\n", b->ignore_count);
1607         if ((l = b->commands))
1608           while (l)
1609             {
1610               fputs_filtered ("\t", gdb_stdout);
1611               fputs_filtered (l->line, gdb_stdout);
1612               fputs_filtered ("\n", gdb_stdout);
1613               l = l->next;
1614             }
1615       }
1616
1617   if (!found_a_breakpoint)
1618     {
1619       if (bnum == -1)
1620         printf_filtered ("No breakpoints or watchpoints.\n");
1621       else
1622         printf_filtered ("No breakpoint or watchpoint number %d.\n", bnum);
1623     }
1624   else
1625     /* Compare against (CORE_ADDR)-1 in case some compiler decides
1626        that a comparison of an unsigned with -1 is always false.  */
1627     if (last_addr != (CORE_ADDR)-1)
1628       set_next_address (last_addr);
1629 }
1630
1631 /* ARGSUSED */
1632 static void
1633 breakpoints_info (bnum_exp, from_tty)
1634      char *bnum_exp;
1635      int from_tty;
1636 {
1637   int bnum = -1;
1638
1639   if (bnum_exp)
1640     bnum = parse_and_eval_address (bnum_exp);
1641
1642   breakpoint_1 (bnum, 0);
1643 }
1644
1645 #if MAINTENANCE_CMDS
1646
1647 /* ARGSUSED */
1648 static void
1649 maintenance_info_breakpoints (bnum_exp, from_tty)
1650      char *bnum_exp;
1651      int from_tty;
1652 {
1653   int bnum = -1;
1654
1655   if (bnum_exp)
1656     bnum = parse_and_eval_address (bnum_exp);
1657
1658   breakpoint_1 (bnum, 1);
1659 }
1660
1661 #endif
1662
1663 /* Print a message describing any breakpoints set at PC.  */
1664
1665 static void
1666 describe_other_breakpoints (pc)
1667      register CORE_ADDR pc;
1668 {
1669   register int others = 0;
1670   register struct breakpoint *b;
1671
1672   ALL_BREAKPOINTS (b)
1673     if (b->address == pc)
1674       others++;
1675   if (others > 0)
1676     {
1677       printf_filtered ("Note: breakpoint%s ", (others > 1) ? "s" : "");
1678       ALL_BREAKPOINTS (b)
1679         if (b->address == pc)
1680           {
1681             others--;
1682             printf_filtered
1683               ("%d%s%s ",
1684                b->number,
1685                (b->enable == disabled) ? " (disabled)" : "",
1686                (others > 1) ? "," : ((others == 1) ? " and" : ""));
1687           }
1688       printf_filtered ("also set at pc ");
1689       print_address_numeric (pc, gdb_stdout);
1690       printf_filtered (".\n");
1691     }
1692 }
1693 \f
1694 /* Set the default place to put a breakpoint
1695    for the `break' command with no arguments.  */
1696
1697 void
1698 set_default_breakpoint (valid, addr, symtab, line)
1699      int valid;
1700      CORE_ADDR addr;
1701      struct symtab *symtab;
1702      int line;
1703 {
1704   default_breakpoint_valid = valid;
1705   default_breakpoint_address = addr;
1706   default_breakpoint_symtab = symtab;
1707   default_breakpoint_line = line;
1708 }
1709
1710 /* Rescan breakpoints at address ADDRESS,
1711    marking the first one as "first" and any others as "duplicates".
1712    This is so that the bpt instruction is only inserted once.  */
1713
1714 static void
1715 check_duplicates (address)
1716      CORE_ADDR address;
1717 {
1718   register struct breakpoint *b;
1719   register int count = 0;
1720
1721   if (address == 0)             /* Watchpoints are uninteresting */
1722     return;
1723
1724   ALL_BREAKPOINTS (b)
1725     if (b->enable != disabled && b->address == address)
1726       {
1727         count++;
1728         b->duplicate = count > 1;
1729       }
1730 }
1731
1732 /* Low level routine to set a breakpoint.
1733    Takes as args the three things that every breakpoint must have.
1734    Returns the breakpoint object so caller can set other things.
1735    Does not set the breakpoint number!
1736    Does not print anything.
1737
1738    ==> This routine should not be called if there is a chance of later
1739    error(); otherwise it leaves a bogus breakpoint on the chain.  Validate
1740    your arguments BEFORE calling this routine!  */
1741
1742 static struct breakpoint *
1743 set_raw_breakpoint (sal)
1744      struct symtab_and_line sal;
1745 {
1746   register struct breakpoint *b, *b1;
1747
1748   b = (struct breakpoint *) xmalloc (sizeof (struct breakpoint));
1749   memset (b, 0, sizeof (*b));
1750   b->address = sal.pc;
1751   if (sal.symtab == NULL)
1752     b->source_file = NULL;
1753   else
1754     b->source_file = savestring (sal.symtab->filename,
1755                                  strlen (sal.symtab->filename));
1756   b->thread = -1;
1757   b->line_number = sal.line;
1758   b->enable = enabled;
1759   b->next = 0;
1760   b->silent = 0;
1761   b->ignore_count = 0;
1762   b->commands = NULL;
1763   b->frame = 0;
1764
1765   /* Add this breakpoint to the end of the chain
1766      so that a list of breakpoints will come out in order
1767      of increasing numbers.  */
1768
1769   b1 = breakpoint_chain;
1770   if (b1 == 0)
1771     breakpoint_chain = b;
1772   else
1773     {
1774       while (b1->next)
1775         b1 = b1->next;
1776       b1->next = b;
1777     }
1778
1779   check_duplicates (sal.pc);
1780   breakpoints_changed ();
1781
1782   return b;
1783 }
1784
1785 static void
1786 create_longjmp_breakpoint(func_name)
1787      char *func_name;
1788 {
1789   struct symtab_and_line sal;
1790   struct breakpoint *b;
1791   static int internal_breakpoint_number = -1;
1792
1793   if (func_name != NULL)
1794     {
1795       struct minimal_symbol *m;
1796
1797       m = lookup_minimal_symbol(func_name, (struct objfile *)NULL);
1798       if (m)
1799         sal.pc = SYMBOL_VALUE_ADDRESS (m);
1800       else
1801         return;
1802     }
1803   else
1804     sal.pc = 0;
1805
1806   sal.symtab = NULL;
1807   sal.line = 0;
1808
1809   b = set_raw_breakpoint(sal);
1810   if (!b) return;
1811
1812   b->type = func_name != NULL ? bp_longjmp : bp_longjmp_resume;
1813   b->disposition = donttouch;
1814   b->enable = disabled;
1815   b->silent = 1;
1816   if (func_name)
1817     b->addr_string = strsave(func_name);
1818   b->number = internal_breakpoint_number--;
1819 }
1820
1821 /* Call this routine when stepping and nexting to enable a breakpoint if we do
1822    a longjmp().  When we hit that breakpoint, call
1823    set_longjmp_resume_breakpoint() to figure out where we are going. */
1824
1825 void
1826 enable_longjmp_breakpoint()
1827 {
1828   register struct breakpoint *b;
1829
1830   ALL_BREAKPOINTS (b)
1831     if (b->type == bp_longjmp)
1832       {
1833         b->enable = enabled;
1834         check_duplicates (b->address);
1835       }
1836 }
1837
1838 void
1839 disable_longjmp_breakpoint()
1840 {
1841   register struct breakpoint *b;
1842
1843   ALL_BREAKPOINTS (b)
1844     if (   b->type == bp_longjmp
1845         || b->type == bp_longjmp_resume)
1846       {
1847         b->enable = disabled;
1848         check_duplicates (b->address);
1849       }
1850 }
1851
1852 /* Call this after hitting the longjmp() breakpoint.  Use this to set a new
1853    breakpoint at the target of the jmp_buf.
1854
1855    FIXME - This ought to be done by setting a temporary breakpoint that gets
1856    deleted automatically...
1857 */
1858
1859 void
1860 set_longjmp_resume_breakpoint(pc, frame)
1861      CORE_ADDR pc;
1862      FRAME frame;
1863 {
1864   register struct breakpoint *b;
1865
1866   ALL_BREAKPOINTS (b)
1867     if (b->type == bp_longjmp_resume)
1868       {
1869         b->address = pc;
1870         b->enable = enabled;
1871         if (frame != NULL)
1872           b->frame = FRAME_FP(frame);
1873         else
1874           b->frame = 0;
1875         check_duplicates (b->address);
1876         return;
1877       }
1878 }
1879
1880 /* Set a breakpoint that will evaporate an end of command
1881    at address specified by SAL.
1882    Restrict it to frame FRAME if FRAME is nonzero.  */
1883
1884 struct breakpoint *
1885 set_momentary_breakpoint (sal, frame, type)
1886      struct symtab_and_line sal;
1887      FRAME frame;
1888      enum bptype type;
1889 {
1890   register struct breakpoint *b;
1891   b = set_raw_breakpoint (sal);
1892   b->type = type;
1893   b->enable = enabled;
1894   b->disposition = donttouch;
1895   b->frame = (frame ? FRAME_FP (frame) : 0);
1896   return b;
1897 }
1898
1899 #if 0
1900 void
1901 clear_momentary_breakpoints ()
1902 {
1903   register struct breakpoint *b;
1904   ALL_BREAKPOINTS (b)
1905     if (b->disposition == delete)
1906       {
1907         delete_breakpoint (b);
1908         break;
1909       }
1910 }
1911 #endif
1912 \f
1913 /* Tell the user we have just set a breakpoint B.  */
1914 static void
1915 mention (b)
1916      struct breakpoint *b;
1917 {
1918   switch (b->type)
1919     {
1920     case bp_watchpoint:
1921       printf_filtered ("Watchpoint %d: ", b->number);
1922       print_expression (b->exp, gdb_stdout);
1923       break;
1924     case bp_hardware_watchpoint:
1925       printf_filtered ("Hardware watchpoint %d: ", b->number);
1926       print_expression (b->exp, gdb_stdout);
1927       break;
1928     case bp_breakpoint:
1929       printf_filtered ("Breakpoint %d at ", b->number);
1930       print_address_numeric (b->address, gdb_stdout);
1931       if (b->source_file)
1932         printf_filtered (": file %s, line %d.",
1933                          b->source_file, b->line_number);
1934       break;
1935     case bp_until:
1936     case bp_finish:
1937     case bp_longjmp:
1938     case bp_longjmp_resume:
1939     case bp_step_resume:
1940     case bp_through_sigtramp:
1941     case bp_call_dummy:
1942     case bp_watchpoint_scope:
1943       break;
1944     }
1945   printf_filtered ("\n");
1946 }
1947
1948 #if 0
1949 /* Nobody calls this currently. */
1950 /* Set a breakpoint from a symtab and line.
1951    If TEMPFLAG is nonzero, it is a temporary breakpoint.
1952    ADDR_STRING is a malloc'd string holding the name of where we are
1953    setting the breakpoint.  This is used later to re-set it after the
1954    program is relinked and symbols are reloaded.
1955    Print the same confirmation messages that the breakpoint command prints.  */
1956
1957 void
1958 set_breakpoint (s, line, tempflag, addr_string)
1959      struct symtab *s;
1960      int line;
1961      int tempflag;
1962      char *addr_string;
1963 {
1964   register struct breakpoint *b;
1965   struct symtab_and_line sal;
1966   
1967   sal.symtab = s;
1968   sal.line = line;
1969   sal.pc = 0;
1970   resolve_sal_pc (&sal);                        /* Might error out */
1971   describe_other_breakpoints (sal.pc);
1972
1973   b = set_raw_breakpoint (sal);
1974   set_breakpoint_count (breakpoint_count + 1);
1975   b->number = breakpoint_count;
1976   b->type = bp_breakpoint;
1977   b->cond = 0;
1978   b->addr_string = addr_string;
1979   b->enable = enabled;
1980   b->disposition = tempflag ? delete : donttouch;
1981
1982   mention (b);
1983 }
1984 #endif /* 0 */
1985 \f
1986 /* Set a breakpoint according to ARG (function, linenum or *address)
1987    and make it temporary if TEMPFLAG is nonzero. */
1988
1989 static void
1990 break_command_1 (arg, tempflag, from_tty)
1991      char *arg;
1992      int tempflag, from_tty;
1993 {
1994   struct symtabs_and_lines sals;
1995   struct symtab_and_line sal;
1996   register struct expression *cond = 0;
1997   register struct breakpoint *b;
1998
1999   /* Pointers in arg to the start, and one past the end, of the condition.  */
2000   char *cond_start = NULL;
2001   char *cond_end = NULL;
2002   /* Pointers in arg to the start, and one past the end,
2003      of the address part.  */
2004   char *addr_start = NULL;
2005   char *addr_end = NULL;
2006   struct cleanup *old_chain;
2007   struct cleanup *canonical_strings_chain = NULL;
2008   char **canonical = (char **)NULL;
2009   int i;
2010   int thread;
2011
2012   sals.sals = NULL;
2013   sals.nelts = 0;
2014
2015   sal.line = sal.pc = sal.end = 0;
2016   sal.symtab = 0;
2017
2018   /* If no arg given, or if first arg is 'if ', use the default breakpoint. */
2019
2020   if (!arg || (arg[0] == 'i' && arg[1] == 'f' 
2021                && (arg[2] == ' ' || arg[2] == '\t')))
2022     {
2023       if (default_breakpoint_valid)
2024         {
2025           sals.sals = (struct symtab_and_line *) 
2026             xmalloc (sizeof (struct symtab_and_line));
2027           sal.pc = default_breakpoint_address;
2028           sal.line = default_breakpoint_line;
2029           sal.symtab = default_breakpoint_symtab;
2030           sals.sals[0] = sal;
2031           sals.nelts = 1;
2032         }
2033       else
2034         error ("No default breakpoint address now.");
2035     }
2036   else
2037     {
2038       addr_start = arg;
2039
2040       /* Force almost all breakpoints to be in terms of the
2041          current_source_symtab (which is decode_line_1's default).  This
2042          should produce the results we want almost all of the time while
2043          leaving default_breakpoint_* alone.  */
2044       if (default_breakpoint_valid
2045           && (!current_source_symtab
2046               || (arg && (*arg == '+' || *arg == '-'))))
2047         sals = decode_line_1 (&arg, 1, default_breakpoint_symtab,
2048                               default_breakpoint_line, &canonical);
2049       else
2050         sals = decode_line_1 (&arg, 1, (struct symtab *)NULL, 0, &canonical);
2051
2052       addr_end = arg;
2053     }
2054   
2055   if (! sals.nelts) 
2056     return;
2057
2058   /* Make sure that all storage allocated in decode_line_1 gets freed in case
2059      the following `for' loop errors out.  */
2060   old_chain = make_cleanup (free, sals.sals);
2061   if (canonical != (char **)NULL)
2062     {
2063       make_cleanup (free, canonical);
2064       canonical_strings_chain = make_cleanup (null_cleanup, 0);
2065       for (i = 0; i < sals.nelts; i++)
2066         {
2067           if (canonical[i] != NULL)
2068             make_cleanup (free, canonical[i]);
2069         }
2070     }
2071
2072   thread = -1;                  /* No specific thread yet */
2073
2074   /* Resolve all line numbers to PC's, and verify that conditions
2075      can be parsed, before setting any breakpoints.  */
2076   for (i = 0; i < sals.nelts; i++)
2077     {
2078       char *tok, *end_tok;
2079       int toklen;
2080
2081       resolve_sal_pc (&sals.sals[i]);
2082       
2083       tok = arg;
2084
2085       while (tok && *tok)
2086         {
2087           while (*tok == ' ' || *tok == '\t')
2088             tok++;
2089
2090           end_tok = tok;
2091
2092           while (*end_tok != ' ' && *end_tok != '\t' && *end_tok != '\000')
2093             end_tok++;
2094
2095           toklen = end_tok - tok;
2096
2097           if (toklen >= 1 && strncmp (tok, "if", toklen) == 0)
2098             {
2099               tok = cond_start = end_tok + 1;
2100               cond = parse_exp_1 (&tok, block_for_pc (sals.sals[i].pc), 0);
2101               cond_end = tok;
2102             }
2103           else if (toklen >= 1 && strncmp (tok, "thread", toklen) == 0)
2104             {
2105               char *tmptok;
2106
2107               tok = end_tok + 1;
2108               tmptok = tok;
2109               thread = strtol (tok, &tok, 0);
2110               if (tok == tmptok)
2111                 error ("Junk after thread keyword.");
2112               if (!valid_thread_id (thread))
2113                 error ("Unknown thread %d\n", thread);
2114             }
2115           else
2116             error ("Junk at end of arguments.");
2117         }
2118     }
2119
2120   /* Remove the canonical strings from the cleanup, they are needed below.  */
2121   if (canonical != (char **)NULL)
2122     discard_cleanups (canonical_strings_chain);
2123
2124   /* Now set all the breakpoints.  */
2125   for (i = 0; i < sals.nelts; i++)
2126     {
2127       sal = sals.sals[i];
2128
2129       if (from_tty)
2130         describe_other_breakpoints (sal.pc);
2131
2132       b = set_raw_breakpoint (sal);
2133       set_breakpoint_count (breakpoint_count + 1);
2134       b->number = breakpoint_count;
2135       b->type = bp_breakpoint;
2136       b->cond = cond;
2137       b->thread = thread;
2138
2139       /* If a canonical line spec is needed use that instead of the
2140          command string.  */
2141       if (canonical != (char **)NULL && canonical[i] != NULL)
2142         b->addr_string = canonical[i];
2143       else if (addr_start)
2144         b->addr_string = savestring (addr_start, addr_end - addr_start);
2145       if (cond_start)
2146         b->cond_string = savestring (cond_start, cond_end - cond_start);
2147                                      
2148       b->enable = enabled;
2149       b->disposition = tempflag ? delete : donttouch;
2150
2151       mention (b);
2152     }
2153
2154   if (sals.nelts > 1)
2155     {
2156       printf_filtered ("Multiple breakpoints were set.\n");
2157       printf_filtered ("Use the \"delete\" command to delete unwanted breakpoints.\n");
2158     }
2159   do_cleanups (old_chain);
2160 }
2161
2162 /* Helper function for break_command_1 and disassemble_command.  */
2163
2164 void
2165 resolve_sal_pc (sal)
2166      struct symtab_and_line *sal;
2167 {
2168   CORE_ADDR pc;
2169
2170   if (sal->pc == 0 && sal->symtab != 0)
2171     {
2172       pc = find_line_pc (sal->symtab, sal->line);
2173       if (pc == 0)
2174         error ("No line %d in file \"%s\".",
2175                sal->line, sal->symtab->filename);
2176       sal->pc = pc;
2177     }
2178 }
2179
2180 void
2181 break_command (arg, from_tty)
2182      char *arg;
2183      int from_tty;
2184 {
2185   break_command_1 (arg, 0, from_tty);
2186 }
2187
2188 static void
2189 tbreak_command (arg, from_tty)
2190      char *arg;
2191      int from_tty;
2192 {
2193   break_command_1 (arg, 1, from_tty);
2194 }
2195
2196 /* ARGSUSED */
2197 static void
2198 watch_command (arg, from_tty)
2199      char *arg;
2200      int from_tty;
2201 {
2202   struct breakpoint *b;
2203   struct symtab_and_line sal;
2204   struct expression *exp;
2205   struct block *exp_valid_block;
2206   struct value *val;
2207   FRAME frame, prev_frame;
2208
2209   sal.pc = 0;
2210   sal.symtab = NULL;
2211   sal.line = 0;
2212   
2213   /* Parse arguments.  */
2214   innermost_block = NULL;
2215   exp = parse_expression (arg);
2216   exp_valid_block = innermost_block;
2217   val = evaluate_expression (exp);
2218   release_value (val);
2219   if (VALUE_LAZY (val))
2220     value_fetch_lazy (val);
2221
2222   /* Now set up the breakpoint.  */
2223   b = set_raw_breakpoint (sal);
2224   set_breakpoint_count (breakpoint_count + 1);
2225   b->number = breakpoint_count;
2226   b->disposition = donttouch;
2227   b->exp = exp;
2228   b->exp_valid_block = exp_valid_block;
2229   b->val = val;
2230   b->cond = 0;
2231   b->cond_string = NULL;
2232   b->exp_string = savestring (arg, strlen (arg));
2233
2234   frame = block_innermost_frame (exp_valid_block);
2235   if (frame)
2236     {
2237       prev_frame = get_prev_frame (frame);
2238       b->watchpoint_frame = FRAME_FP (frame);
2239     }
2240   else
2241     b->watchpoint_frame = (CORE_ADDR)0;
2242
2243   if (can_use_hardware_watchpoint (b))
2244     b->type = bp_hardware_watchpoint;
2245   else
2246     b->type = bp_watchpoint;
2247
2248   /* If the expression is "local", then set up a "watchpoint scope"
2249      breakpoint at the point where we've left the scope of the watchpoint
2250      expression.  */
2251   if (innermost_block)
2252     {
2253       struct breakpoint *scope_breakpoint;
2254       struct symtab_and_line scope_sal;
2255
2256       if (prev_frame)
2257         {
2258           scope_sal.pc = get_frame_pc (prev_frame);
2259           scope_sal.symtab = NULL;
2260           scope_sal.line = 0;
2261           
2262           scope_breakpoint = set_raw_breakpoint (scope_sal);
2263           set_breakpoint_count (breakpoint_count + 1);
2264           scope_breakpoint->number = breakpoint_count;
2265
2266           scope_breakpoint->type = bp_watchpoint_scope;
2267           scope_breakpoint->enable = enabled;
2268
2269           /* Automatically delete the breakpoint when it hits.  */
2270           scope_breakpoint->disposition = delete;
2271
2272           /* Only break in the proper frame (help with recursion).  */
2273           scope_breakpoint->frame = prev_frame->frame;
2274
2275           /* Set the address at which we will stop.  */
2276           scope_breakpoint->address = get_frame_pc (prev_frame);
2277
2278           /* The scope breakpoint is related to the watchpoint.  We
2279              will need to act on them together.  */
2280           b->related_breakpoint = scope_breakpoint;
2281         }
2282     }
2283
2284   mention (b);
2285 }
2286
2287 /* Return nonzero if the watchpoint described by B can be handled
2288    completely in hardware.  If the watchpoint can not be handled
2289    in hardware return zero.  */
2290
2291 static int
2292 can_use_hardware_watchpoint (b)
2293      struct breakpoint *b;
2294 {
2295   value_ptr mark = value_mark ();
2296   value_ptr v = evaluate_expression (b->exp);
2297   int found_memory = 0;
2298         
2299   /* Make sure all the intermediate values are in memory.  Also make sure
2300      we found at least one memory expression.  Guards against watch 0x12345,
2301      which is meaningless, but could cause errors if one tries to insert a 
2302      hardware watchpoint for the constant expression.  */
2303   for ( ; v != mark; v = v->next)
2304     {
2305       if (!(v->lval == lval_memory)
2306           || v->lval == not_lval
2307           || (v->lval != not_lval
2308               && v->modifiable == 0))
2309         return 0;
2310       else
2311         if (v->lval == lval_memory)
2312           found_memory = 1;
2313     }
2314
2315   /* The expression itself looks suitable for using a hardware
2316      watchpoint, but give the target machine a chance to reject it.  */
2317   return found_memory && TARGET_CAN_USE_HARDWARE_WATCHPOINT (b);
2318 }
2319
2320 \f
2321 /*
2322  * Helper routine for the until_command routine in infcmd.c.  Here
2323  * because it uses the mechanisms of breakpoints.
2324  */
2325 /* ARGSUSED */
2326 void
2327 until_break_command (arg, from_tty)
2328      char *arg;
2329      int from_tty;
2330 {
2331   struct symtabs_and_lines sals;
2332   struct symtab_and_line sal;
2333   FRAME prev_frame = get_prev_frame (selected_frame);
2334   struct breakpoint *breakpoint;
2335   struct cleanup *old_chain;
2336
2337   clear_proceed_status ();
2338
2339   /* Set a breakpoint where the user wants it and at return from
2340      this function */
2341   
2342   if (default_breakpoint_valid)
2343     sals = decode_line_1 (&arg, 1, default_breakpoint_symtab,
2344                           default_breakpoint_line, (char ***)NULL);
2345   else
2346     sals = decode_line_1 (&arg, 1, (struct symtab *)NULL, 0, (char ***)NULL);
2347   
2348   if (sals.nelts != 1)
2349     error ("Couldn't get information on specified line.");
2350   
2351   sal = sals.sals[0];
2352   free ((PTR)sals.sals);                /* malloc'd, so freed */
2353   
2354   if (*arg)
2355     error ("Junk at end of arguments.");
2356   
2357   resolve_sal_pc (&sal);
2358   
2359   breakpoint = set_momentary_breakpoint (sal, selected_frame, bp_until);
2360   
2361   old_chain = make_cleanup(delete_breakpoint, breakpoint);
2362
2363   /* Keep within the current frame */
2364   
2365   if (prev_frame)
2366     {
2367       struct frame_info *fi;
2368       
2369       fi = get_frame_info (prev_frame);
2370       sal = find_pc_line (fi->pc, 0);
2371       sal.pc = fi->pc;
2372       breakpoint = set_momentary_breakpoint (sal, prev_frame, bp_until);
2373       make_cleanup(delete_breakpoint, breakpoint);
2374     }
2375   
2376   proceed (-1, TARGET_SIGNAL_DEFAULT, 0);
2377   do_cleanups(old_chain);
2378 }
2379 \f
2380 #if 0
2381 /* These aren't used; I don't konw what they were for.  */
2382 /* Set a breakpoint at the catch clause for NAME.  */
2383 static int
2384 catch_breakpoint (name)
2385      char *name;
2386 {
2387 }
2388
2389 static int
2390 disable_catch_breakpoint ()
2391 {
2392 }
2393
2394 static int
2395 delete_catch_breakpoint ()
2396 {
2397 }
2398
2399 static int
2400 enable_catch_breakpoint ()
2401 {
2402 }
2403 #endif /* 0 */
2404
2405 struct sal_chain
2406 {
2407   struct sal_chain *next;
2408   struct symtab_and_line sal;
2409 };
2410
2411 #if 0
2412 /* This isn't used; I don't know what it was for.  */
2413 /* For each catch clause identified in ARGS, run FUNCTION
2414    with that clause as an argument.  */
2415 static struct symtabs_and_lines
2416 map_catch_names (args, function)
2417      char *args;
2418      int (*function)();
2419 {
2420   register char *p = args;
2421   register char *p1;
2422   struct symtabs_and_lines sals;
2423 #if 0
2424   struct sal_chain *sal_chain = 0;
2425 #endif
2426
2427   if (p == 0)
2428     error_no_arg ("one or more catch names");
2429
2430   sals.nelts = 0;
2431   sals.sals = NULL;
2432
2433   while (*p)
2434     {
2435       p1 = p;
2436       /* Don't swallow conditional part.  */
2437       if (p1[0] == 'i' && p1[1] == 'f'
2438           && (p1[2] == ' ' || p1[2] == '\t'))
2439         break;
2440
2441       if (isalpha (*p1))
2442         {
2443           p1++;
2444           while (isalnum (*p1) || *p1 == '_' || *p1 == '$')
2445             p1++;
2446         }
2447
2448       if (*p1 && *p1 != ' ' && *p1 != '\t')
2449         error ("Arguments must be catch names.");
2450
2451       *p1 = 0;
2452 #if 0
2453       if (function (p))
2454         {
2455           struct sal_chain *next
2456             = (struct sal_chain *)alloca (sizeof (struct sal_chain));
2457           next->next = sal_chain;
2458           next->sal = get_catch_sal (p);
2459           sal_chain = next;
2460           goto win;
2461         }
2462 #endif
2463       printf_unfiltered ("No catch clause for exception %s.\n", p);
2464 #if 0
2465     win:
2466 #endif
2467       p = p1;
2468       while (*p == ' ' || *p == '\t') p++;
2469     }
2470 }
2471 #endif /* 0 */
2472
2473 /* This shares a lot of code with `print_frame_label_vars' from stack.c.  */
2474
2475 static struct symtabs_and_lines
2476 get_catch_sals (this_level_only)
2477      int this_level_only;
2478 {
2479   register struct blockvector *bl;
2480   register struct block *block;
2481   int index, have_default = 0;
2482   struct frame_info *fi;
2483   CORE_ADDR pc;
2484   struct symtabs_and_lines sals;
2485   struct sal_chain *sal_chain = 0;
2486   char *blocks_searched;
2487
2488   /* Not sure whether an error message is always the correct response,
2489      but it's better than a core dump.  */
2490   if (selected_frame == NULL)
2491     error ("No selected frame.");
2492   block = get_frame_block (selected_frame);
2493   fi = get_frame_info (selected_frame);
2494   pc = fi->pc;
2495
2496   sals.nelts = 0;
2497   sals.sals = NULL;
2498
2499   if (block == 0)
2500     error ("No symbol table info available.\n");
2501
2502   bl = blockvector_for_pc (BLOCK_END (block) - 4, &index);
2503   blocks_searched = (char *) alloca (BLOCKVECTOR_NBLOCKS (bl) * sizeof (char));
2504   memset (blocks_searched, 0, BLOCKVECTOR_NBLOCKS (bl) * sizeof (char));
2505
2506   while (block != 0)
2507     {
2508       CORE_ADDR end = BLOCK_END (block) - 4;
2509       int last_index;
2510
2511       if (bl != blockvector_for_pc (end, &index))
2512         error ("blockvector blotch");
2513       if (BLOCKVECTOR_BLOCK (bl, index) != block)
2514         error ("blockvector botch");
2515       last_index = BLOCKVECTOR_NBLOCKS (bl);
2516       index += 1;
2517
2518       /* Don't print out blocks that have gone by.  */
2519       while (index < last_index
2520              && BLOCK_END (BLOCKVECTOR_BLOCK (bl, index)) < pc)
2521         index++;
2522
2523       while (index < last_index
2524              && BLOCK_END (BLOCKVECTOR_BLOCK (bl, index)) < end)
2525         {
2526           if (blocks_searched[index] == 0)
2527             {
2528               struct block *b = BLOCKVECTOR_BLOCK (bl, index);
2529               int nsyms;
2530               register int i;
2531               register struct symbol *sym;
2532
2533               nsyms = BLOCK_NSYMS (b);
2534
2535               for (i = 0; i < nsyms; i++)
2536                 {
2537                   sym = BLOCK_SYM (b, i);
2538                   if (STREQ (SYMBOL_NAME (sym), "default"))
2539                     {
2540                       if (have_default)
2541                         continue;
2542                       have_default = 1;
2543                     }
2544                   if (SYMBOL_CLASS (sym) == LOC_LABEL)
2545                     {
2546                       struct sal_chain *next = (struct sal_chain *)
2547                         alloca (sizeof (struct sal_chain));
2548                       next->next = sal_chain;
2549                       next->sal = find_pc_line (SYMBOL_VALUE_ADDRESS (sym), 0);
2550                       sal_chain = next;
2551                     }
2552                 }
2553               blocks_searched[index] = 1;
2554             }
2555           index++;
2556         }
2557       if (have_default)
2558         break;
2559       if (sal_chain && this_level_only)
2560         break;
2561
2562       /* After handling the function's top-level block, stop.
2563          Don't continue to its superblock, the block of
2564          per-file symbols.  */
2565       if (BLOCK_FUNCTION (block))
2566         break;
2567       block = BLOCK_SUPERBLOCK (block);
2568     }
2569
2570   if (sal_chain)
2571     {
2572       struct sal_chain *tmp_chain;
2573
2574       /* Count the number of entries.  */
2575       for (index = 0, tmp_chain = sal_chain; tmp_chain;
2576            tmp_chain = tmp_chain->next)
2577         index++;
2578
2579       sals.nelts = index;
2580       sals.sals = (struct symtab_and_line *)
2581         xmalloc (index * sizeof (struct symtab_and_line));
2582       for (index = 0; sal_chain; sal_chain = sal_chain->next, index++)
2583         sals.sals[index] = sal_chain->sal;
2584     }
2585
2586   return sals;
2587 }
2588
2589 /* Commands to deal with catching exceptions.  */
2590
2591 static void
2592 catch_command_1 (arg, tempflag, from_tty)
2593      char *arg;
2594      int tempflag;
2595      int from_tty;
2596 {
2597   /* First, translate ARG into something we can deal with in terms
2598      of breakpoints.  */
2599
2600   struct symtabs_and_lines sals;
2601   struct symtab_and_line sal;
2602   register struct expression *cond = 0;
2603   register struct breakpoint *b;
2604   char *save_arg;
2605   int i;
2606
2607   sal.line = sal.pc = sal.end = 0;
2608   sal.symtab = 0;
2609
2610   /* If no arg given, or if first arg is 'if ', all active catch clauses
2611      are breakpointed. */
2612
2613   if (!arg || (arg[0] == 'i' && arg[1] == 'f' 
2614                && (arg[2] == ' ' || arg[2] == '\t')))
2615     {
2616       /* Grab all active catch clauses.  */
2617       sals = get_catch_sals (0);
2618     }
2619   else
2620     {
2621       /* Grab selected catch clauses.  */
2622       error ("catch NAME not implemented");
2623 #if 0
2624       /* This isn't used; I don't know what it was for.  */
2625       sals = map_catch_names (arg, catch_breakpoint);
2626 #endif
2627     }
2628
2629   if (! sals.nelts) 
2630     return;
2631
2632   save_arg = arg;
2633   for (i = 0; i < sals.nelts; i++)
2634     {
2635       resolve_sal_pc (&sals.sals[i]);
2636       
2637       while (arg && *arg)
2638         {
2639           if (arg[0] == 'i' && arg[1] == 'f'
2640               && (arg[2] == ' ' || arg[2] == '\t'))
2641             cond = parse_exp_1 ((arg += 2, &arg), 
2642                                 block_for_pc (sals.sals[i].pc), 0);
2643           else
2644             error ("Junk at end of arguments.");
2645         }
2646       arg = save_arg;
2647     }
2648
2649   for (i = 0; i < sals.nelts; i++)
2650     {
2651       sal = sals.sals[i];
2652
2653       if (from_tty)
2654         describe_other_breakpoints (sal.pc);
2655
2656       b = set_raw_breakpoint (sal);
2657       set_breakpoint_count (breakpoint_count + 1);
2658       b->number = breakpoint_count;
2659       b->type = bp_breakpoint;
2660       b->cond = cond;
2661       b->enable = enabled;
2662       b->disposition = tempflag ? delete : donttouch;
2663
2664       mention (b);
2665     }
2666
2667   if (sals.nelts > 1)
2668     {
2669       printf_unfiltered ("Multiple breakpoints were set.\n");
2670       printf_unfiltered ("Use the \"delete\" command to delete unwanted breakpoints.\n");
2671     }
2672   free ((PTR)sals.sals);
2673 }
2674
2675 #if 0
2676 /* These aren't used; I don't know what they were for.  */
2677 /* Disable breakpoints on all catch clauses described in ARGS.  */
2678 static void
2679 disable_catch (args)
2680      char *args;
2681 {
2682   /* Map the disable command to catch clauses described in ARGS.  */
2683 }
2684
2685 /* Enable breakpoints on all catch clauses described in ARGS.  */
2686 static void
2687 enable_catch (args)
2688      char *args;
2689 {
2690   /* Map the disable command to catch clauses described in ARGS.  */
2691 }
2692
2693 /* Delete breakpoints on all catch clauses in the active scope.  */
2694 static void
2695 delete_catch (args)
2696      char *args;
2697 {
2698   /* Map the delete command to catch clauses described in ARGS.  */
2699 }
2700 #endif /* 0 */
2701
2702 static void
2703 catch_command (arg, from_tty)
2704      char *arg;
2705      int from_tty;
2706 {
2707   catch_command_1 (arg, 0, from_tty);
2708 }
2709 \f
2710 static void
2711 clear_command (arg, from_tty)
2712      char *arg;
2713      int from_tty;
2714 {
2715   register struct breakpoint *b, *b1;
2716   struct symtabs_and_lines sals;
2717   struct symtab_and_line sal;
2718   register struct breakpoint *found;
2719   int i;
2720
2721   if (arg)
2722     {
2723       sals = decode_line_spec (arg, 1);
2724     }
2725   else
2726     {
2727       sals.sals = (struct symtab_and_line *) xmalloc (sizeof (struct symtab_and_line));
2728       sal.line = default_breakpoint_line;
2729       sal.symtab = default_breakpoint_symtab;
2730       sal.pc = 0;
2731       if (sal.symtab == 0)
2732         error ("No source file specified.");
2733
2734       sals.sals[0] = sal;
2735       sals.nelts = 1;
2736     }
2737
2738   for (i = 0; i < sals.nelts; i++)
2739     {
2740       /* If exact pc given, clear bpts at that pc.
2741          But if sal.pc is zero, clear all bpts on specified line.  */
2742       sal = sals.sals[i];
2743       found = (struct breakpoint *) 0;
2744       while (breakpoint_chain
2745              && (sal.pc
2746                  ? breakpoint_chain->address == sal.pc
2747                  : (breakpoint_chain->source_file != NULL
2748                     && sal.symtab != NULL
2749                     && STREQ (breakpoint_chain->source_file,
2750                               sal.symtab->filename)
2751                     && breakpoint_chain->line_number == sal.line)))
2752         {
2753           b1 = breakpoint_chain;
2754           breakpoint_chain = b1->next;
2755           b1->next = found;
2756           found = b1;
2757         }
2758
2759       ALL_BREAKPOINTS (b)
2760         while (b->next
2761                && b->next->type != bp_watchpoint
2762                && b->next->type != bp_hardware_watchpoint
2763                && (sal.pc
2764                    ? b->next->address == sal.pc
2765                    : (b->next->source_file != NULL
2766                       && sal.symtab != NULL
2767                       && STREQ (b->next->source_file, sal.symtab->filename)
2768                       && b->next->line_number == sal.line)))
2769           {
2770             b1 = b->next;
2771             b->next = b1->next;
2772             b1->next = found;
2773             found = b1;
2774           }
2775
2776       if (found == 0)
2777         {
2778           if (arg)
2779             error ("No breakpoint at %s.", arg);
2780           else
2781             error ("No breakpoint at this line.");
2782         }
2783
2784       if (found->next) from_tty = 1; /* Always report if deleted more than one */
2785       if (from_tty) printf_unfiltered ("Deleted breakpoint%s ", found->next ? "s" : "");
2786       breakpoints_changed ();
2787       while (found)
2788         {
2789           if (from_tty) printf_unfiltered ("%d ", found->number);
2790           b1 = found->next;
2791           delete_breakpoint (found);
2792           found = b1;
2793         }
2794       if (from_tty) putchar_unfiltered ('\n');
2795     }
2796   free ((PTR)sals.sals);
2797 }
2798 \f
2799 /* Delete breakpoint in BS if they are `delete' breakpoints.
2800    This is called after any breakpoint is hit, or after errors.  */
2801
2802 void
2803 breakpoint_auto_delete (bs)
2804      bpstat bs;
2805 {
2806   for (; bs; bs = bs->next)
2807     if (bs->breakpoint_at && bs->breakpoint_at->disposition == delete
2808         && bs->stop)
2809       delete_breakpoint (bs->breakpoint_at);
2810 }
2811
2812 /* Delete a breakpoint and clean up all traces of it in the data structures. */
2813
2814 void
2815 delete_breakpoint (bpt)
2816      struct breakpoint *bpt;
2817 {
2818   register struct breakpoint *b;
2819   register bpstat bs;
2820
2821   if (bpt->inserted)
2822     remove_breakpoint (bpt);
2823       
2824   if (breakpoint_chain == bpt)
2825     breakpoint_chain = bpt->next;
2826
2827   ALL_BREAKPOINTS (b)
2828     if (b->next == bpt)
2829       {
2830         b->next = bpt->next;
2831         break;
2832       }
2833
2834   check_duplicates (bpt->address);
2835   /* If this breakpoint was inserted, and there is another breakpoint
2836      at the same address, we need to insert the other breakpoint.  */
2837   if (bpt->inserted
2838       && bpt->type != bp_hardware_watchpoint)
2839     {
2840       ALL_BREAKPOINTS (b)
2841         if (b->address == bpt->address
2842             && !b->duplicate
2843             && b->enable != disabled)
2844           {
2845             int val;
2846             val = target_insert_breakpoint (b->address, b->shadow_contents);
2847             if (val != 0)
2848               {
2849                 target_terminal_ours_for_output ();
2850                 fprintf_unfiltered (gdb_stderr, "Cannot insert breakpoint %d:\n", b->number);
2851                 memory_error (val, b->address); /* which bombs us out */
2852               }
2853             else
2854               b->inserted = 1;
2855           }
2856     }
2857
2858   free_command_lines (&bpt->commands);
2859   if (bpt->cond)
2860     free (bpt->cond);
2861   if (bpt->cond_string != NULL)
2862     free (bpt->cond_string);
2863   if (bpt->addr_string != NULL)
2864     free (bpt->addr_string);
2865   if (bpt->exp_string != NULL)
2866     free (bpt->exp_string);
2867   if (bpt->source_file != NULL)
2868     free (bpt->source_file);
2869
2870   breakpoints_changed ();
2871
2872   /* Be sure no bpstat's are pointing at it after it's been freed.  */
2873   /* FIXME, how can we find all bpstat's?
2874      We just check stop_bpstat for now.  */
2875   for (bs = stop_bpstat; bs; bs = bs->next)
2876     if (bs->breakpoint_at == bpt)
2877       bs->breakpoint_at = NULL;
2878   free ((PTR)bpt);
2879 }
2880
2881 static void
2882 delete_command (arg, from_tty)
2883      char *arg;
2884      int from_tty;
2885 {
2886
2887   if (arg == 0)
2888     {
2889       /* Ask user only if there are some breakpoints to delete.  */
2890       if (!from_tty
2891           || (breakpoint_chain && query ("Delete all breakpoints? ", 0, 0)))
2892         {
2893           /* No arg; clear all breakpoints.  */
2894           while (breakpoint_chain)
2895             delete_breakpoint (breakpoint_chain);
2896         }
2897     }
2898   else
2899     map_breakpoint_numbers (arg, delete_breakpoint);
2900 }
2901
2902 /* Reset a breakpoint given it's struct breakpoint * BINT.
2903    The value we return ends up being the return value from catch_errors.
2904    Unused in this case.  */
2905
2906 static int
2907 breakpoint_re_set_one (bint)
2908      char *bint;
2909 {
2910   struct breakpoint *b = (struct breakpoint *)bint;  /* get past catch_errs */
2911   int i;
2912   struct symtabs_and_lines sals;
2913   char *s;
2914   enum enable save_enable;
2915
2916   switch (b->type)
2917     {
2918     case bp_breakpoint:
2919       if (b->addr_string == NULL)
2920         {
2921           /* Anything without a string can't be re-set. */
2922           delete_breakpoint (b);
2923           return 0;
2924         }
2925       /* In case we have a problem, disable this breakpoint.  We'll restore
2926          its status if we succeed.  */
2927       save_enable = b->enable;
2928       b->enable = disabled;
2929
2930       s = b->addr_string;
2931       sals = decode_line_1 (&s, 1, (struct symtab *)NULL, 0, (char ***)NULL);
2932       for (i = 0; i < sals.nelts; i++)
2933         {
2934           resolve_sal_pc (&sals.sals[i]);
2935
2936           /* Reparse conditions, they might contain references to the
2937              old symtab.  */
2938           if (b->cond_string != NULL)
2939             {
2940               s = b->cond_string;
2941               if (b->cond)
2942                 free ((PTR)b->cond);
2943               b->cond = parse_exp_1 (&s, block_for_pc (sals.sals[i].pc), 0);
2944             }
2945
2946           /* We need to re-set the breakpoint if the address changes...*/
2947           if (b->address != sals.sals[i].pc
2948               /* ...or new and old breakpoints both have source files, and
2949                  the source file name or the line number changes...  */
2950               || (b->source_file != NULL
2951                   && sals.sals[i].symtab != NULL
2952                   && (!STREQ (b->source_file, sals.sals[i].symtab->filename)
2953                       || b->line_number != sals.sals[i].line)
2954                   )
2955               /* ...or we switch between having a source file and not having
2956                  one.  */
2957               || ((b->source_file == NULL) != (sals.sals[i].symtab == NULL))
2958               )
2959             {
2960               if (b->source_file != NULL)
2961                 free (b->source_file);
2962               if (sals.sals[i].symtab == NULL)
2963                 b->source_file = NULL;
2964               else
2965                 b->source_file =
2966                   savestring (sals.sals[i].symtab->filename,
2967                               strlen (sals.sals[i].symtab->filename));
2968               b->line_number = sals.sals[i].line;
2969               b->address = sals.sals[i].pc;
2970           
2971               check_duplicates (b->address);
2972
2973               mention (b);
2974
2975               /* Might be better to do this just once per breakpoint_re_set,
2976                  rather than once for every breakpoint.  */
2977               breakpoints_changed ();
2978             }
2979           b->enable = save_enable;      /* Restore it, this worked. */
2980         }
2981       free ((PTR)sals.sals);
2982       break;
2983
2984     case bp_watchpoint:
2985     case bp_hardware_watchpoint:
2986       innermost_block = NULL;
2987       /* The issue arises of what context to evaluate this in.  The same
2988          one as when it was set, but what does that mean when symbols have
2989          been re-read?  We could save the filename and functionname, but
2990          if the context is more local than that, the best we could do would
2991          be something like how many levels deep and which index at that
2992          particular level, but that's going to be less stable than filenames
2993          or functionnames.  */
2994       /* So for now, just use a global context.  */
2995       b->exp = parse_expression (b->exp_string);
2996       b->exp_valid_block = innermost_block;
2997       b->val = evaluate_expression (b->exp);
2998       release_value (b->val);
2999       if (VALUE_LAZY (b->val))
3000         value_fetch_lazy (b->val);
3001
3002       if (b->cond_string != NULL)
3003         {
3004           s = b->cond_string;
3005           b->cond = parse_exp_1 (&s, (struct block *)0, 0);
3006         }
3007       if (b->enable == enabled)
3008         mention (b);
3009       break;
3010
3011     default:
3012       printf_filtered ("Deleting unknown breakpoint type %d\n", b->type);
3013       /* fall through */
3014     case bp_until:
3015     case bp_finish:
3016     case bp_longjmp:
3017     case bp_longjmp_resume:
3018     case bp_watchpoint_scope:
3019     case bp_call_dummy:
3020       delete_breakpoint (b);
3021       break;
3022     }
3023
3024   return 0;
3025 }
3026
3027 /* Re-set all breakpoints after symbols have been re-loaded.  */
3028 void
3029 breakpoint_re_set ()
3030 {
3031   struct breakpoint *b, *temp;
3032   static char message1[] = "Error in re-setting breakpoint %d:\n";
3033   char message[sizeof (message1) + 30 /* slop */];
3034   
3035   ALL_BREAKPOINTS_SAFE (b, temp)
3036     {
3037       sprintf (message, message1, b->number);   /* Format possible error msg */
3038       catch_errors (breakpoint_re_set_one, (char *) b, message,
3039                     RETURN_MASK_ALL);
3040     }
3041
3042   create_longjmp_breakpoint("longjmp");
3043   create_longjmp_breakpoint("_longjmp");
3044   create_longjmp_breakpoint("siglongjmp");
3045   create_longjmp_breakpoint(NULL);
3046
3047 #if 0
3048   /* Took this out (temporaliy at least), since it produces an extra 
3049      blank line at startup. This messes up the gdbtests. -PB */
3050   /* Blank line to finish off all those mention() messages we just printed.  */
3051   printf_filtered ("\n");
3052 #endif
3053 }
3054 \f
3055 /* Set ignore-count of breakpoint number BPTNUM to COUNT.
3056    If from_tty is nonzero, it prints a message to that effect,
3057    which ends with a period (no newline).  */
3058
3059 void
3060 set_ignore_count (bptnum, count, from_tty)
3061      int bptnum, count, from_tty;
3062 {
3063   register struct breakpoint *b;
3064
3065   if (count < 0)
3066     count = 0;
3067
3068   ALL_BREAKPOINTS (b)
3069     if (b->number == bptnum)
3070       {
3071         b->ignore_count = count;
3072         if (!from_tty)
3073           return;
3074         else if (count == 0)
3075           printf_filtered ("Will stop next time breakpoint %d is reached.",
3076                            bptnum);
3077         else if (count == 1)
3078           printf_filtered ("Will ignore next crossing of breakpoint %d.",
3079                            bptnum);
3080         else
3081           printf_filtered ("Will ignore next %d crossings of breakpoint %d.",
3082                   count, bptnum);
3083         breakpoints_changed ();
3084         return;
3085       }
3086
3087   error ("No breakpoint number %d.", bptnum);
3088 }
3089
3090 /* Clear the ignore counts of all breakpoints.  */
3091 void
3092 breakpoint_clear_ignore_counts ()
3093 {
3094   struct breakpoint *b;
3095
3096   ALL_BREAKPOINTS (b)
3097     b->ignore_count = 0;
3098 }
3099
3100 /* Command to set ignore-count of breakpoint N to COUNT.  */
3101
3102 static void
3103 ignore_command (args, from_tty)
3104      char *args;
3105      int from_tty;
3106 {
3107   char *p = args;
3108   register int num;
3109
3110   if (p == 0)
3111     error_no_arg ("a breakpoint number");
3112   
3113   num = get_number (&p);
3114
3115   if (*p == 0)
3116     error ("Second argument (specified ignore-count) is missing.");
3117
3118   set_ignore_count (num,
3119                     longest_to_int (value_as_long (parse_and_eval (p))),
3120                     from_tty);
3121   printf_filtered ("\n");
3122   breakpoints_changed ();
3123 }
3124 \f
3125 /* Call FUNCTION on each of the breakpoints
3126    whose numbers are given in ARGS.  */
3127
3128 static void
3129 map_breakpoint_numbers (args, function)
3130      char *args;
3131      void (*function) PARAMS ((struct breakpoint *));
3132 {
3133   register char *p = args;
3134   char *p1;
3135   register int num;
3136   register struct breakpoint *b;
3137
3138   if (p == 0)
3139     error_no_arg ("one or more breakpoint numbers");
3140
3141   while (*p)
3142     {
3143       p1 = p;
3144       
3145       num = get_number (&p1);
3146
3147       ALL_BREAKPOINTS (b)
3148         if (b->number == num)
3149           {
3150             struct breakpoint *related_breakpoint = b->related_breakpoint;
3151             function (b);
3152             if (related_breakpoint)
3153               function (related_breakpoint);
3154             goto win;
3155           }
3156       printf_unfiltered ("No breakpoint number %d.\n", num);
3157     win:
3158       p = p1;
3159     }
3160 }
3161
3162 static void
3163 enable_breakpoint (bpt)
3164      struct breakpoint *bpt;
3165 {
3166   FRAME save_selected_frame = NULL;
3167   int save_selected_frame_level = -1;
3168   
3169   bpt->enable = enabled;
3170
3171   breakpoints_changed ();
3172
3173   check_duplicates (bpt->address);
3174   if (bpt->type == bp_watchpoint || bpt->type == bp_hardware_watchpoint)
3175     {
3176       if (bpt->exp_valid_block != NULL)
3177         {
3178           FRAME fr = find_frame_addr_in_frame_chain (bpt->watchpoint_frame);
3179           if (fr == NULL)
3180             {
3181               printf_filtered ("\
3182 Cannot enable watchpoint %d because the block in which its expression\n\
3183 is valid is not currently in scope.\n", bpt->number);
3184               bpt->enable = disabled;
3185               return;
3186             }
3187
3188           save_selected_frame = selected_frame;
3189           save_selected_frame_level = selected_frame_level;
3190           select_frame (fr, -1);
3191         }
3192
3193       value_free (bpt->val);
3194
3195       bpt->val = evaluate_expression (bpt->exp);
3196       release_value (bpt->val);
3197       if (VALUE_LAZY (bpt->val))
3198         value_fetch_lazy (bpt->val);
3199
3200       if (save_selected_frame_level >= 0)
3201         select_frame (save_selected_frame, save_selected_frame_level);
3202     }
3203 }
3204
3205 /* ARGSUSED */
3206 static void
3207 enable_command (args, from_tty)
3208      char *args;
3209      int from_tty;
3210 {
3211   struct breakpoint *bpt;
3212   if (args == 0)
3213     ALL_BREAKPOINTS (bpt)
3214       switch (bpt->type)
3215         {
3216         case bp_breakpoint:
3217         case bp_watchpoint:
3218         case bp_hardware_watchpoint:
3219           enable_breakpoint (bpt);
3220         default:
3221           continue;
3222         }
3223   else
3224     map_breakpoint_numbers (args, enable_breakpoint);
3225 }
3226
3227 static void
3228 disable_breakpoint (bpt)
3229      struct breakpoint *bpt;
3230 {
3231   /* Never disable a watchpoint scope breakpoint; we want to
3232      hit them when we leave scope so we can delete both the
3233      watchpoint and its scope breakpoint at that time.  */
3234   if (bpt->type == bp_watchpoint_scope)
3235     return;
3236
3237   bpt->enable = disabled;
3238
3239   breakpoints_changed ();
3240
3241   check_duplicates (bpt->address);
3242 }
3243
3244 /* ARGSUSED */
3245 static void
3246 disable_command (args, from_tty)
3247      char *args;
3248      int from_tty;
3249 {
3250   register struct breakpoint *bpt;
3251   if (args == 0)
3252     ALL_BREAKPOINTS (bpt)
3253       switch (bpt->type)
3254         {
3255         case bp_breakpoint:
3256         case bp_watchpoint:
3257         case bp_hardware_watchpoint:
3258           disable_breakpoint (bpt);
3259         default:
3260           continue;
3261         }
3262   else
3263     map_breakpoint_numbers (args, disable_breakpoint);
3264 }
3265
3266 static void
3267 enable_once_breakpoint (bpt)
3268      struct breakpoint *bpt;
3269 {
3270   bpt->enable = enabled;
3271   bpt->disposition = disable;
3272
3273   check_duplicates (bpt->address);
3274   breakpoints_changed ();
3275 }
3276
3277 /* ARGSUSED */
3278 static void
3279 enable_once_command (args, from_tty)
3280      char *args;
3281      int from_tty;
3282 {
3283   map_breakpoint_numbers (args, enable_once_breakpoint);
3284 }
3285
3286 static void
3287 enable_delete_breakpoint (bpt)
3288      struct breakpoint *bpt;
3289 {
3290   bpt->enable = enabled;
3291   bpt->disposition = delete;
3292
3293   check_duplicates (bpt->address);
3294   breakpoints_changed ();
3295 }
3296
3297 /* ARGSUSED */
3298 static void
3299 enable_delete_command (args, from_tty)
3300      char *args;
3301      int from_tty;
3302 {
3303   map_breakpoint_numbers (args, enable_delete_breakpoint);
3304 }
3305 \f
3306 /*
3307  * Use default_breakpoint_'s, or nothing if they aren't valid.
3308  */
3309 struct symtabs_and_lines
3310 decode_line_spec_1 (string, funfirstline)
3311      char *string;
3312      int funfirstline;
3313 {
3314   struct symtabs_and_lines sals;
3315   if (string == 0)
3316     error ("Empty line specification.");
3317   if (default_breakpoint_valid)
3318     sals = decode_line_1 (&string, funfirstline,
3319                           default_breakpoint_symtab, default_breakpoint_line,
3320                           (char ***)NULL);
3321   else
3322     sals = decode_line_1 (&string, funfirstline,
3323                           (struct symtab *)NULL, 0, (char ***)NULL);
3324   if (*string)
3325     error ("Junk at end of line specification: %s", string);
3326   return sals;
3327 }
3328 \f
3329 void
3330 _initialize_breakpoint ()
3331 {
3332   breakpoint_chain = 0;
3333   /* Don't bother to call set_breakpoint_count.  $bpnum isn't useful
3334      before a breakpoint is set.  */
3335   breakpoint_count = 0;
3336
3337   add_com ("ignore", class_breakpoint, ignore_command,
3338            "Set ignore-count of breakpoint number N to COUNT.");
3339
3340   add_com ("commands", class_breakpoint, commands_command,
3341            "Set commands to be executed when a breakpoint is hit.\n\
3342 Give breakpoint number as argument after \"commands\".\n\
3343 With no argument, the targeted breakpoint is the last one set.\n\
3344 The commands themselves follow starting on the next line.\n\
3345 Type a line containing \"end\" to indicate the end of them.\n\
3346 Give \"silent\" as the first line to make the breakpoint silent;\n\
3347 then no output is printed when it is hit, except what the commands print.");
3348
3349   add_com ("condition", class_breakpoint, condition_command,
3350            "Specify breakpoint number N to break only if COND is true.\n\
3351 N is an integer; COND is an expression to be evaluated whenever\n\
3352 breakpoint N is reached.  ");
3353
3354   add_com ("tbreak", class_breakpoint, tbreak_command,
3355            "Set a temporary breakpoint.  Args like \"break\" command.\n\
3356 Like \"break\" except the breakpoint is only temporary,\n\
3357 so it will be deleted when hit.  Equivalent to \"break\" followed\n\
3358 by using \"enable delete\" on the breakpoint number.");
3359
3360   add_prefix_cmd ("enable", class_breakpoint, enable_command,
3361                   "Enable some breakpoints.\n\
3362 Give breakpoint numbers (separated by spaces) as arguments.\n\
3363 With no subcommand, breakpoints are enabled until you command otherwise.\n\
3364 This is used to cancel the effect of the \"disable\" command.\n\
3365 With a subcommand you can enable temporarily.",
3366                   &enablelist, "enable ", 1, &cmdlist);
3367
3368   add_abbrev_prefix_cmd ("breakpoints", class_breakpoint, enable_command,
3369                   "Enable some breakpoints.\n\
3370 Give breakpoint numbers (separated by spaces) as arguments.\n\
3371 This is used to cancel the effect of the \"disable\" command.\n\
3372 May be abbreviated to simply \"enable\".\n",
3373                   &enablebreaklist, "enable breakpoints ", 1, &enablelist);
3374
3375   add_cmd ("once", no_class, enable_once_command,
3376            "Enable breakpoints for one hit.  Give breakpoint numbers.\n\
3377 If a breakpoint is hit while enabled in this fashion, it becomes disabled.",
3378            &enablebreaklist);
3379
3380   add_cmd ("delete", no_class, enable_delete_command,
3381            "Enable breakpoints and delete when hit.  Give breakpoint numbers.\n\
3382 If a breakpoint is hit while enabled in this fashion, it is deleted.",
3383            &enablebreaklist);
3384
3385   add_cmd ("delete", no_class, enable_delete_command,
3386            "Enable breakpoints and delete when hit.  Give breakpoint numbers.\n\
3387 If a breakpoint is hit while enabled in this fashion, it is deleted.",
3388            &enablelist);
3389
3390   add_cmd ("once", no_class, enable_once_command,
3391            "Enable breakpoints for one hit.  Give breakpoint numbers.\n\
3392 If a breakpoint is hit while enabled in this fashion, it becomes disabled.",
3393            &enablelist);
3394
3395   add_prefix_cmd ("disable", class_breakpoint, disable_command,
3396            "Disable some breakpoints.\n\
3397 Arguments are breakpoint numbers with spaces in between.\n\
3398 To disable all breakpoints, give no argument.\n\
3399 A disabled breakpoint is not forgotten, but has no effect until reenabled.",
3400                   &disablelist, "disable ", 1, &cmdlist);
3401   add_com_alias ("dis", "disable", class_breakpoint, 1);
3402   add_com_alias ("disa", "disable", class_breakpoint, 1);
3403
3404   add_cmd ("breakpoints", class_alias, disable_command,
3405            "Disable some breakpoints.\n\
3406 Arguments are breakpoint numbers with spaces in between.\n\
3407 To disable all breakpoints, give no argument.\n\
3408 A disabled breakpoint is not forgotten, but has no effect until reenabled.\n\
3409 This command may be abbreviated \"disable\".",
3410            &disablelist);
3411
3412   add_prefix_cmd ("delete", class_breakpoint, delete_command,
3413            "Delete some breakpoints or auto-display expressions.\n\
3414 Arguments are breakpoint numbers with spaces in between.\n\
3415 To delete all breakpoints, give no argument.\n\
3416 \n\
3417 Also a prefix command for deletion of other GDB objects.\n\
3418 The \"unset\" command is also an alias for \"delete\".",
3419                   &deletelist, "delete ", 1, &cmdlist);
3420   add_com_alias ("d", "delete", class_breakpoint, 1);
3421
3422   add_cmd ("breakpoints", class_alias, delete_command,
3423            "Delete some breakpoints or auto-display expressions.\n\
3424 Arguments are breakpoint numbers with spaces in between.\n\
3425 To delete all breakpoints, give no argument.\n\
3426 This command may be abbreviated \"delete\".",
3427            &deletelist);
3428
3429   add_com ("clear", class_breakpoint, clear_command,
3430            "Clear breakpoint at specified line or function.\n\
3431 Argument may be line number, function name, or \"*\" and an address.\n\
3432 If line number is specified, all breakpoints in that line are cleared.\n\
3433 If function is specified, breakpoints at beginning of function are cleared.\n\
3434 If an address is specified, breakpoints at that address are cleared.\n\n\
3435 With no argument, clears all breakpoints in the line that the selected frame\n\
3436 is executing in.\n\
3437 \n\
3438 See also the \"delete\" command which clears breakpoints by number.");
3439
3440   add_com ("break", class_breakpoint, break_command,
3441            "Set breakpoint at specified line or function.\n\
3442 Argument may be line number, function name, or \"*\" and an address.\n\
3443 If line number is specified, break at start of code for that line.\n\
3444 If function is specified, break at start of code for that function.\n\
3445 If an address is specified, break at that exact address.\n\
3446 With no arg, uses current execution address of selected stack frame.\n\
3447 This is useful for breaking on return to a stack frame.\n\
3448 \n\
3449 Multiple breakpoints at one place are permitted, and useful if conditional.\n\
3450 \n\
3451 Do \"help breakpoints\" for info on other commands dealing with breakpoints.");
3452   add_com_alias ("b", "break", class_run, 1);
3453   add_com_alias ("br", "break", class_run, 1);
3454   add_com_alias ("bre", "break", class_run, 1);
3455   add_com_alias ("brea", "break", class_run, 1);
3456
3457   add_info ("breakpoints", breakpoints_info,
3458             "Status of user-settable breakpoints, or breakpoint number NUMBER.\n\
3459 The \"Type\" column indicates one of:\n\
3460 \tbreakpoint     - normal breakpoint\n\
3461 \twatchpoint     - watchpoint\n\
3462 The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
3463 the disposition of the breakpoint after it gets hit.  \"dis\" means that the\n\
3464 breakpoint will be disabled.  The \"Address\" and \"What\" columns indicate the\n\
3465 address and file/line number respectively.\n\n\
3466 Convenience variable \"$_\" and default examine address for \"x\"\n\
3467 are set to the address of the last breakpoint listed.\n\n\
3468 Convenience variable \"$bpnum\" contains the number of the last\n\
3469 breakpoint set.");
3470
3471 #if MAINTENANCE_CMDS
3472
3473   add_cmd ("breakpoints", class_maintenance, maintenance_info_breakpoints,
3474             "Status of all breakpoints, or breakpoint number NUMBER.\n\
3475 The \"Type\" column indicates one of:\n\
3476 \tbreakpoint     - normal breakpoint\n\
3477 \twatchpoint     - watchpoint\n\
3478 \tlongjmp        - internal breakpoint used to step through longjmp()\n\
3479 \tlongjmp resume - internal breakpoint at the target of longjmp()\n\
3480 \tuntil          - internal breakpoint used by the \"until\" command\n\
3481 \tfinish         - internal breakpoint used by the \"finish\" command\n\
3482 The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
3483 the disposition of the breakpoint after it gets hit.  \"dis\" means that the\n\
3484 breakpoint will be disabled.  The \"Address\" and \"What\" columns indicate the\n\
3485 address and file/line number respectively.\n\n\
3486 Convenience variable \"$_\" and default examine address for \"x\"\n\
3487 are set to the address of the last breakpoint listed.\n\n\
3488 Convenience variable \"$bpnum\" contains the number of the last\n\
3489 breakpoint set.",
3490            &maintenanceinfolist);
3491
3492 #endif  /* MAINTENANCE_CMDS */
3493
3494   add_com ("catch", class_breakpoint, catch_command,
3495          "Set breakpoints to catch exceptions that are raised.\n\
3496 Argument may be a single exception to catch, multiple exceptions\n\
3497 to catch, or the default exception \"default\".  If no arguments\n\
3498 are given, breakpoints are set at all exception handlers catch clauses\n\
3499 within the current scope.\n\
3500 \n\
3501 A condition specified for the catch applies to all breakpoints set\n\
3502 with this command\n\
3503 \n\
3504 Do \"help breakpoints\" for info on other commands dealing with breakpoints.");
3505
3506   add_com ("watch", class_breakpoint, watch_command,
3507            "Set a watchpoint for an expression.\n\
3508 A watchpoint stops execution of your program whenever the value of\n\
3509 an expression changes.");
3510
3511   add_info ("watchpoints", breakpoints_info,
3512             "Synonym for ``info breakpoints''.");
3513 }
3514
3515 /* OK, when we call objfile_relocate, we need to relocate breakpoints
3516    too.  breakpoint_re_set is not a good choice--for example, if
3517    addr_string contains just a line number without a file name the
3518    breakpoint might get set in a different file.  In general, there is
3519    no need to go all the way back to the user's string (though this might
3520    work if some effort were made to canonicalize it), since symtabs and
3521    everything except addresses are still valid.
3522
3523    Probably the best way to solve this is to have each breakpoint save
3524    the objfile and the section number that was used to set it (if set
3525    by "*addr", probably it is best to use find_pc_line to get a symtab
3526    and use the objfile and block_line_section for that symtab).  Then
3527    objfile_relocate can call fixup_breakpoints with the objfile and
3528    the new_offsets, and it can relocate only the appropriate breakpoints.  */
3529
3530 #ifdef IBM6000_TARGET
3531 /* But for now, just kludge it based on the concept that before an
3532    objfile is relocated the breakpoint is below 0x10000000, and afterwards
3533    it is higher, so that way we only relocate each breakpoint once.  */
3534
3535 void
3536 fixup_breakpoints (low, high, delta)
3537   CORE_ADDR low;
3538   CORE_ADDR high;
3539   CORE_ADDR delta;
3540 {
3541   struct breakpoint *b;
3542
3543   ALL_BREAKPOINTS (b)
3544     {
3545      if (b->address >= low && b->address <= high)
3546        b->address += delta;
3547     }
3548 }
3549 #endif