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