import gdb-1999-09-08 snapshot
[platform/upstream/binutils.git] / gdb / breakpoint.c
1 /* Everything about breakpoints, for GDB.
2    Copyright 1986, 87, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 1999
3    Free Software Foundation, Inc.
4
5    This file is part of GDB.
6
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 2 of the License, or
10    (at your option) any later version.
11
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16
17    You should have received a copy of the GNU General Public License
18    along with this program; if not, write to the Free Software
19    Foundation, Inc., 59 Temple Place - Suite 330,
20    Boston, MA 02111-1307, USA.  */
21
22 #include "defs.h"
23 #include <ctype.h>
24 #include "symtab.h"
25 #include "frame.h"
26 #include "breakpoint.h"
27 #include "gdbtypes.h"
28 #include "expression.h"
29 #include "gdbcore.h"
30 #include "gdbcmd.h"
31 #include "value.h"
32 #include "command.h"
33 #include "inferior.h"
34 #include "gdbthread.h"
35 #include "target.h"
36 #include "language.h"
37 #include "gdb_string.h"
38 #include "demangle.h"
39 #include "annotate.h"
40 #include "symfile.h"
41 #include "objfiles.h"
42
43 #include "gdb-events.h"
44
45 /* Prototypes for local functions. */
46
47 static void
48 catch_command_1 PARAMS ((char *, int, int));
49
50 static void
51 enable_delete_command PARAMS ((char *, int));
52
53 static void
54 enable_delete_breakpoint PARAMS ((struct breakpoint *));
55
56 static void
57 enable_once_command PARAMS ((char *, int));
58
59 static void
60 enable_once_breakpoint PARAMS ((struct breakpoint *));
61
62 static void
63 disable_command PARAMS ((char *, int));
64
65 static void
66 enable_command PARAMS ((char *, int));
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 breakpoint_re_set_one PARAMS ((PTR));
75
76 static void
77 clear_command PARAMS ((char *, int));
78
79 static void
80 catch_command PARAMS ((char *, int));
81
82 static void
83 handle_gnu_4_16_catch_command PARAMS ((char *, int, int));
84
85 static struct symtabs_and_lines
86 get_catch_sals PARAMS ((int));
87
88 static void
89 watch_command PARAMS ((char *, int));
90
91 static int
92 can_use_hardware_watchpoint PARAMS ((struct value *));
93
94 static void break_at_finish_command PARAMS ((char *, int));
95 static void break_at_finish_at_depth_command PARAMS ((char *, int));
96
97 void
98 tbreak_command PARAMS ((char *, int));
99
100 static void tbreak_at_finish_command PARAMS ((char *, int));
101
102 static void
103 break_command_1 PARAMS ((char *, int, int));
104
105 static void
106 mention PARAMS ((struct breakpoint *));
107
108 struct breakpoint *
109   set_raw_breakpoint PARAMS ((struct symtab_and_line));
110
111 static void
112 check_duplicates PARAMS ((CORE_ADDR, asection *));
113
114 static void
115 describe_other_breakpoints PARAMS ((CORE_ADDR, asection *));
116
117 static void
118 breakpoints_info PARAMS ((char *, int));
119
120 static void
121 breakpoint_1 PARAMS ((int, int));
122
123 static bpstat
124   bpstat_alloc PARAMS ((struct breakpoint *, bpstat));
125
126 static int breakpoint_cond_eval PARAMS ((PTR));
127
128 static void
129 cleanup_executing_breakpoints PARAMS ((PTR));
130
131 static void
132 commands_command PARAMS ((char *, int));
133
134 static void
135 condition_command PARAMS ((char *, int));
136
137 static int
138 get_number PARAMS ((char **));
139
140 void
141 set_breakpoint_count PARAMS ((int));
142
143 #if 0
144 static struct breakpoint *
145   create_temp_exception_breakpoint PARAMS ((CORE_ADDR));
146 #endif
147
148 typedef enum
149   {
150     mark_inserted,
151     mark_uninserted
152   }
153 insertion_state_t;
154
155 static int
156 remove_breakpoint PARAMS ((struct breakpoint *, insertion_state_t));
157
158 static int print_it_normal PARAMS ((bpstat));
159
160 typedef struct
161   {
162     enum exception_event_kind kind;
163     int enable;
164   }
165 args_for_catchpoint_enable;
166
167 static int watchpoint_check PARAMS ((PTR));
168
169 static int cover_target_enable_exception_callback PARAMS ((PTR));
170
171 static int print_it_done PARAMS ((bpstat));
172
173 static int print_it_noop PARAMS ((bpstat));
174
175 static void maintenance_info_breakpoints PARAMS ((char *, int));
176
177 #ifdef GET_LONGJMP_TARGET
178 static void create_longjmp_breakpoint PARAMS ((char *));
179 #endif
180
181 static int hw_breakpoint_used_count PARAMS ((void));
182
183 static int hw_watchpoint_used_count PARAMS ((enum bptype, int *));
184
185 static void hbreak_command PARAMS ((char *, int));
186
187 static void thbreak_command PARAMS ((char *, int));
188
189 static void watch_command_1 PARAMS ((char *, int, int));
190
191 static void rwatch_command PARAMS ((char *, int));
192
193 static void awatch_command PARAMS ((char *, int));
194
195 static void do_enable_breakpoint PARAMS ((struct breakpoint *, enum bpdisp));
196
197 static void solib_load_unload_1 PARAMS ((char *hookname,
198                                          int tempflag,
199                                          char *dll_pathname,
200                                          char *cond_string,
201                                          enum bptype bp_kind));
202
203 static void create_fork_vfork_event_catchpoint PARAMS ((int tempflag, 
204                                                         char *cond_string,
205                                                         enum bptype bp_kind));
206
207 static void break_at_finish_at_depth_command_1 PARAMS ((char *arg, 
208                                                         int flag, 
209                                                         int from_tty));
210
211 static void break_at_finish_command_1 PARAMS ((char *arg, 
212                                                int flag, 
213                                                int from_tty));
214
215 static void stop_command PARAMS ((char *arg, int from_tty));
216
217 static void stopin_command PARAMS ((char *arg, int from_tty));
218
219 static void stopat_command PARAMS ((char *arg, int from_tty));
220
221 static char *ep_find_event_name_end PARAMS ((char *arg));
222
223 static char *ep_parse_optional_if_clause PARAMS ((char **arg));
224
225 static char *ep_parse_optional_filename PARAMS ((char **arg));
226
227 static void catch_exec_command_1 PARAMS ((char *arg, int tempflag, 
228                                           int from_tty));
229
230 static void create_exception_catchpoint 
231   PARAMS ((int tempflag, char *cond_string,
232            enum exception_event_kind ex_event,
233            struct symtab_and_line * sal));
234
235 static void catch_exception_command_1 
236   PARAMS ((enum exception_event_kind ex_event,
237            char *arg, int tempflag, int from_tty));
238
239 static void tcatch_command PARAMS ((char *arg, int from_tty));
240
241 static void ep_skip_leading_whitespace PARAMS ((char **s));
242
243 /* Prototypes for exported functions. */
244
245 static void
246 awatch_command PARAMS ((char *, int));
247
248 static void
249 do_enable_breakpoint PARAMS ((struct breakpoint *, enum bpdisp));
250
251 /* If FALSE, gdb will not use hardware support for watchpoints, even
252    if such is available. */
253 static int can_use_hw_watchpoints;
254
255 void delete_command PARAMS ((char *, int));
256
257 void _initialize_breakpoint PARAMS ((void));
258
259 void set_breakpoint_count PARAMS ((int));
260
261 extern int addressprint;        /* Print machine addresses? */
262
263 #if defined (GET_LONGJMP_TARGET) || defined (SOLIB_ADD)
264 static int internal_breakpoint_number = -1;
265 #endif
266
267 /* Are we executing breakpoint commands?  */
268 static int executing_breakpoint_commands;
269
270 /* Walk the following statement or block through all breakpoints.
271    ALL_BREAKPOINTS_SAFE does so even if the statment deletes the current
272    breakpoint.  */
273
274 #define ALL_BREAKPOINTS(b)  for (b = breakpoint_chain; b; b = b->next)
275
276 #define ALL_BREAKPOINTS_SAFE(b,tmp)     \
277         for (b = breakpoint_chain;      \
278              b? (tmp=b->next, 1): 0;    \
279              b = tmp)
280
281 /* True if SHIFT_INST_REGS defined, false otherwise.  */
282
283 int must_shift_inst_regs =
284 #if defined(SHIFT_INST_REGS)
285 1
286 #else
287 0
288 #endif
289  ;
290
291 /* True if breakpoint hit counts should be displayed in breakpoint info.  */
292
293 int show_breakpoint_hit_counts = 1;
294
295 /* Chain of all breakpoints defined.  */
296
297 struct breakpoint *breakpoint_chain;
298
299 /* Number of last breakpoint made.  */
300
301 int breakpoint_count;
302
303 /* Pointer to current exception event record */
304 static struct exception_event_record *current_exception_event;
305
306 /* Indicator of whether exception catchpoints should be nuked
307    between runs of a program */
308 int exception_catchpoints_are_fragile = 0;
309
310 /* Indicator of when exception catchpoints set-up should be
311    reinitialized -- e.g. when program is re-run */
312 int exception_support_initialized = 0;
313
314 /* This function returns a pointer to the string representation of the
315    pathname of the dynamically-linked library that has just been
316    loaded.
317
318    This function must be used only when SOLIB_HAVE_LOAD_EVENT is TRUE,
319    or undefined results are guaranteed.
320
321    This string's contents are only valid immediately after the
322    inferior has stopped in the dynamic linker hook, and becomes
323    invalid as soon as the inferior is continued.  Clients should make
324    a copy of this string if they wish to continue the inferior and
325    then access the string.  */
326
327 #ifndef SOLIB_LOADED_LIBRARY_PATHNAME
328 #define SOLIB_LOADED_LIBRARY_PATHNAME(pid) ""
329 #endif
330
331 /* This function returns a pointer to the string representation of the
332    pathname of the dynamically-linked library that has just been
333    unloaded.
334
335    This function must be used only when SOLIB_HAVE_UNLOAD_EVENT is
336    TRUE, or undefined results are guaranteed.
337
338    This string's contents are only valid immediately after the
339    inferior has stopped in the dynamic linker hook, and becomes
340    invalid as soon as the inferior is continued.  Clients should make
341    a copy of this string if they wish to continue the inferior and
342    then access the string.  */
343
344 #ifndef SOLIB_UNLOADED_LIBRARY_PATHNAME
345 #define SOLIB_UNLOADED_LIBRARY_PATHNAME(pid) ""
346 #endif
347
348 /* This function is called by the "catch load" command.  It allows the
349    debugger to be notified by the dynamic linker when a specified
350    library file (or any library file, if filename is NULL) is loaded.  */
351
352 #ifndef SOLIB_CREATE_CATCH_LOAD_HOOK
353 #define SOLIB_CREATE_CATCH_LOAD_HOOK(pid,tempflag,filename,cond_string) \
354    error ("catch of library loads not yet implemented on this platform")
355 #endif
356
357 /* This function is called by the "catch unload" command.  It allows
358    the debugger to be notified by the dynamic linker when a specified
359    library file (or any library file, if filename is NULL) is
360    unloaded.  */
361
362 #ifndef SOLIB_CREATE_CATCH_UNLOAD_HOOK
363 #define SOLIB_CREATE_CATCH_UNLOAD_HOOK(pid,tempflag,filename,cond_string) \
364    error ("catch of library unloads not yet implemented on this platform")
365 #endif
366
367 /* Set breakpoint count to NUM.  */
368
369 void
370 set_breakpoint_count (num)
371      int num;
372 {
373   breakpoint_count = num;
374   set_internalvar (lookup_internalvar ("bpnum"),
375                    value_from_longest (builtin_type_int, (LONGEST) num));
376 }
377
378 /* Used in run_command to zero the hit count when a new run starts. */
379
380 void
381 clear_breakpoint_hit_counts ()
382 {
383   struct breakpoint *b;
384
385   ALL_BREAKPOINTS (b)
386     b->hit_count = 0;
387 }
388
389 /* Default address, symtab and line to put a breakpoint at
390    for "break" command with no arg.
391    if default_breakpoint_valid is zero, the other three are
392    not valid, and "break" with no arg is an error.
393
394    This set by print_stack_frame, which calls set_default_breakpoint.  */
395
396 int default_breakpoint_valid;
397 CORE_ADDR default_breakpoint_address;
398 struct symtab *default_breakpoint_symtab;
399 int default_breakpoint_line;
400 \f
401 /* *PP is a string denoting a breakpoint.  Get the number of the breakpoint.
402    Advance *PP after the string and any trailing whitespace.
403
404    Currently the string can either be a number or "$" followed by the name
405    of a convenience variable.  Making it an expression wouldn't work well
406    for map_breakpoint_numbers (e.g. "4 + 5 + 6").  */
407 static int
408 get_number (pp)
409      char **pp;
410 {
411   int retval;
412   char *p = *pp;
413
414   if (p == NULL)
415     /* Empty line means refer to the last breakpoint.  */
416     return breakpoint_count;
417   else if (*p == '$')
418     {
419       /* Make a copy of the name, so we can null-terminate it
420          to pass to lookup_internalvar().  */
421       char *varname;
422       char *start = ++p;
423       value_ptr val;
424
425       while (isalnum (*p) || *p == '_')
426         p++;
427       varname = (char *) alloca (p - start + 1);
428       strncpy (varname, start, p - start);
429       varname[p - start] = '\0';
430       val = value_of_internalvar (lookup_internalvar (varname));
431       if (TYPE_CODE (VALUE_TYPE (val)) != TYPE_CODE_INT)
432         error (
433  "Convenience variables used to specify breakpoints must have integer values."
434           );
435       retval = (int) value_as_long (val);
436     }
437   else
438     {
439       if (*p == '-')
440         ++p;
441       while (*p >= '0' && *p <= '9')
442         ++p;
443       if (p == *pp)
444         /* There is no number here.  (e.g. "cond a == b").  */
445         error_no_arg ("breakpoint number");
446       retval = atoi (*pp);
447     }
448   if (!(isspace (*p) || *p == '\0'))
449     error ("breakpoint number expected");
450   while (isspace (*p))
451     p++;
452   *pp = p;
453   return retval;
454 }
455 \f
456 /* condition N EXP -- set break condition of breakpoint N to EXP.  */
457
458 static void
459 condition_command (arg, from_tty)
460      char *arg;
461      int from_tty;
462 {
463   register struct breakpoint *b;
464   char *p;
465   register int bnum;
466
467   if (arg == 0)
468     error_no_arg ("breakpoint number");
469
470   p = arg;
471   bnum = get_number (&p);
472
473   ALL_BREAKPOINTS (b)
474     if (b->number == bnum)
475     {
476       if (b->cond)
477         {
478           free ((PTR) b->cond);
479           b->cond = 0;
480         }
481       if (b->cond_string != NULL)
482         free ((PTR) b->cond_string);
483
484       if (*p == 0)
485         {
486           b->cond = 0;
487           b->cond_string = NULL;
488           if (from_tty)
489             printf_filtered ("Breakpoint %d now unconditional.\n", bnum);
490         }
491       else
492         {
493           arg = p;
494           /* I don't know if it matters whether this is the string the user
495              typed in or the decompiled expression.  */
496           b->cond_string = savestring (arg, strlen (arg));
497           b->cond = parse_exp_1 (&arg, block_for_pc (b->address), 0);
498           if (*arg)
499             error ("Junk at end of expression");
500         }
501       breakpoints_changed ();
502       return;
503     }
504
505   error ("No breakpoint number %d.", bnum);
506 }
507
508 /* ARGSUSED */
509 static void
510 commands_command (arg, from_tty)
511      char *arg;
512      int from_tty;
513 {
514   register struct breakpoint *b;
515   char *p;
516   register int bnum;
517   struct command_line *l;
518
519   /* If we allowed this, we would have problems with when to
520      free the storage, if we change the commands currently
521      being read from.  */
522
523   if (executing_breakpoint_commands)
524     error ("Can't use the \"commands\" command among a breakpoint's commands.");
525
526   p = arg;
527   bnum = get_number (&p);
528   if (p && *p)
529     error ("Unexpected extra arguments following breakpoint number.");
530
531   ALL_BREAKPOINTS (b)
532     if (b->number == bnum)
533     {
534       char tmpbuf[128];
535       sprintf (tmpbuf, 
536                "Type commands for when breakpoint %d is hit, one per line.", 
537                bnum);
538       l = read_command_lines (tmpbuf, from_tty);
539       free_command_lines (&b->commands);
540       b->commands = l;
541       breakpoints_changed ();
542       return;
543     }
544   error ("No breakpoint number %d.", bnum);
545 }
546 \f
547 /* Like target_read_memory() but if breakpoints are inserted, return
548    the shadow contents instead of the breakpoints themselves.
549
550    Read "memory data" from whatever target or inferior we have. 
551    Returns zero if successful, errno value if not.  EIO is used
552    for address out of bounds.  If breakpoints are inserted, returns
553    shadow contents, not the breakpoints themselves.  From breakpoint.c.  */
554
555 int
556 read_memory_nobpt (memaddr, myaddr, len)
557      CORE_ADDR memaddr;
558      char *myaddr;
559      unsigned len;
560 {
561   int status;
562   struct breakpoint *b;
563   CORE_ADDR bp_addr = 0;
564   int bp_size = 0;
565
566   if (BREAKPOINT_FROM_PC (&bp_addr, &bp_size) == NULL)
567     /* No breakpoints on this machine. */
568     return target_read_memory (memaddr, myaddr, len);
569
570   ALL_BREAKPOINTS (b)
571   {
572     if (b->type == bp_none)
573       warning ("reading through apparently deleted breakpoint #%d?", 
574                b->number);
575
576     /* memory breakpoint? */
577     if (b->type == bp_watchpoint
578         || b->type == bp_hardware_watchpoint
579         || b->type == bp_read_watchpoint
580         || b->type == bp_access_watchpoint)
581       continue;
582     /* bp in memory? */
583     if (!b->inserted)
584       continue;
585     /* Addresses and length of the part of the breakpoint that
586        we need to copy.  */
587     /* XXXX The m68k, sh and h8300 have different local and remote
588        breakpoint values.  BREAKPOINT_FROM_PC still manages to
589        correctly determine the breakpoints memory address and size
590        for these targets. */
591     bp_addr = b->address;
592     bp_size = 0;
593     if (BREAKPOINT_FROM_PC (&bp_addr, &bp_size) == NULL)
594       continue;
595     if (bp_size == 0)
596       /* bp isn't valid */
597       continue;
598     if (bp_addr + bp_size <= memaddr)
599       /* The breakpoint is entirely before the chunk of memory we
600          are reading.  */
601       continue;
602     if (bp_addr >= memaddr + len)
603       /* The breakpoint is entirely after the chunk of memory we are
604          reading. */
605       continue;
606     /* Copy the breakpoint from the shadow contents, and recurse for
607        the things before and after.  */
608     {
609       /* Offset within shadow_contents.  */
610       int bptoffset = 0;
611
612       if (bp_addr < memaddr)
613         {
614           /* Only copy the second part of the breakpoint.  */
615           bp_size -= memaddr - bp_addr;
616           bptoffset = memaddr - bp_addr;
617           bp_addr = memaddr;
618         }
619
620       if (bp_addr + bp_size > memaddr + len)
621         {
622           /* Only copy the first part of the breakpoint.  */
623           bp_size -= (bp_addr + bp_size) - (memaddr + len);
624         }
625
626       memcpy (myaddr + bp_addr - memaddr,
627               b->shadow_contents + bptoffset, bp_size);
628
629       if (bp_addr > memaddr)
630         {
631           /* Copy the section of memory before the breakpoint.  */
632           status = read_memory_nobpt (memaddr, myaddr, bp_addr - memaddr);
633           if (status != 0)
634             return status;
635         }
636
637       if (bp_addr + bp_size < memaddr + len)
638         {
639           /* Copy the section of memory after the breakpoint.  */
640           status = read_memory_nobpt
641             (bp_addr + bp_size,
642              myaddr + bp_addr + bp_size - memaddr,
643              memaddr + len - (bp_addr + bp_size));
644           if (status != 0)
645             return status;
646         }
647       return 0;
648     }
649   }
650   /* Nothing overlaps.  Just call read_memory_noerr.  */
651   return target_read_memory (memaddr, myaddr, len);
652 }
653 \f
654
655 /* insert_breakpoints is used when starting or continuing the program.
656    remove_breakpoints is used when the program stops.
657    Both return zero if successful,
658    or an `errno' value if could not write the inferior.  */
659
660 int
661 insert_breakpoints ()
662 {
663   register struct breakpoint *b, *temp;
664   int return_val = 0;   /* return success code. */
665   int val = 0;
666   int disabled_breaks = 0;
667
668   static char message1[] = "Error inserting catchpoint %d:\n";
669   static char message[sizeof (message1) + 30];
670
671
672   ALL_BREAKPOINTS_SAFE (b, temp)
673   {
674     if (b->type != bp_watchpoint
675         && b->type != bp_hardware_watchpoint
676         && b->type != bp_read_watchpoint
677         && b->type != bp_access_watchpoint
678         && b->type != bp_catch_fork
679         && b->type != bp_catch_vfork
680         && b->type != bp_catch_exec
681         && b->type != bp_catch_throw
682         && b->type != bp_catch_catch
683         && b->enable != disabled
684         && b->enable != shlib_disabled
685         && b->enable != call_disabled
686         && !b->inserted
687         && !b->duplicate)
688       {
689         if (b->type == bp_hardware_breakpoint)
690           val = target_insert_hw_breakpoint (b->address, b->shadow_contents);
691         else
692           {
693             /* Check to see if breakpoint is in an overlay section;
694                if so, we should set the breakpoint at the LMA address.
695                Only if the section is currently mapped should we ALSO
696                set a break at the VMA address. */
697             if (overlay_debugging && b->section &&
698                 section_is_overlay (b->section))
699               {
700                 CORE_ADDR addr;
701
702                 addr = overlay_unmapped_address (b->address, b->section);
703                 val = target_insert_breakpoint (addr, b->shadow_contents);
704                 /* This would be the time to check val, to see if the
705                    breakpoint write to the load address succeeded.  
706                    However, this might be an ordinary occurrance, eg. if 
707                    the unmapped overlay is in ROM.  */
708                 val = 0;        /* in case unmapped address failed */
709                 if (section_is_mapped (b->section))
710                   val = target_insert_breakpoint (b->address,
711                                                   b->shadow_contents);
712               }
713             else                /* ordinary (non-overlay) address */
714               val = target_insert_breakpoint (b->address, b->shadow_contents);
715           }
716         if (val)
717           {
718             /* Can't set the breakpoint.  */
719 #if defined (DISABLE_UNSETTABLE_BREAK)
720             if (DISABLE_UNSETTABLE_BREAK (b->address))
721               {
722                 /* See also: disable_breakpoints_in_shlibs. */
723                 val = 0;
724                 b->enable = shlib_disabled;
725                 if (!disabled_breaks)
726                   {
727                     target_terminal_ours_for_output ();
728                     warning ("Cannot insert breakpoint %d:", b->number);
729                     warning ("Temporarily disabling shared library breakpoints:");
730                   }
731                 disabled_breaks = 1;
732                 warning ("breakpoint #%d ", b->number);
733               }
734             else
735 #endif
736               {
737                 target_terminal_ours_for_output ();
738                 warning ("Cannot insert breakpoint %d:", b->number);
739 #ifdef ONE_PROCESS_WRITETEXT
740                 warning ("The same program may be running in another process.");
741 #endif
742                 memory_error (val, b->address);    /* which bombs us out */
743               }
744           }
745         else
746           b->inserted = 1;
747
748         if (val)
749           return_val = val;     /* remember failure */
750       }
751     else if (ep_is_exception_catchpoint (b)
752              && b->enable != disabled
753              && b->enable != shlib_disabled
754              && b->enable != call_disabled
755              && !b->inserted
756              && !b->duplicate)
757
758       {
759         /* If we get here, we must have a callback mechanism for exception
760            events -- with g++ style embedded label support, we insert
761            ordinary breakpoints and not catchpoints. */
762         /* Format possible error message */
763         sprintf (message, message1, b->number);
764
765         val = target_insert_breakpoint (b->address, b->shadow_contents);
766         if (val)
767           {
768             /* Couldn't set breakpoint for some reason */
769             target_terminal_ours_for_output ();
770             warning ("Cannot insert catchpoint %d; disabling it.",
771                      b->number);
772             b->enable = disabled;
773           }
774         else
775           {
776             /* Bp set, now make sure callbacks are enabled */
777             int val;
778             args_for_catchpoint_enable args;
779             args.kind = b->type == bp_catch_catch ? 
780               EX_EVENT_CATCH : EX_EVENT_THROW;
781             args.enable = 1;
782             val = catch_errors (cover_target_enable_exception_callback,
783                                 &args,
784                                 message, RETURN_MASK_ALL);
785             if (val != 0 && val != -1)
786               {
787                 b->inserted = 1;
788               }
789             /* Check if something went wrong; val == 0 can be ignored */
790             if (val == -1)
791               {
792                 /* something went wrong */
793                 target_terminal_ours_for_output ();
794                 warning ("Cannot insert catchpoint %d; disabling it.",
795                          b->number);
796                 b->enable = disabled;
797               }
798           }
799
800         if (val)
801           return_val = val;     /* remember failure */
802       }
803
804     else if ((b->type == bp_hardware_watchpoint ||
805               b->type == bp_read_watchpoint ||
806               b->type == bp_access_watchpoint)
807              && b->enable == enabled
808              && !b->inserted
809              && !b->duplicate)
810       {
811         struct frame_info *saved_frame;
812         int saved_level, within_current_scope;
813         value_ptr mark = value_mark ();
814         value_ptr v;
815
816         /* Save the current frame and level so we can restore it after
817            evaluating the watchpoint expression on its own frame.  */
818         saved_frame = selected_frame;
819         saved_level = selected_frame_level;
820
821         /* Determine if the watchpoint is within scope.  */
822         if (b->exp_valid_block == NULL)
823           within_current_scope = 1;
824         else
825           {
826             struct frame_info *fi;
827
828             /* There might be no current frame at this moment if we are
829                resuming from a step over a breakpoint.
830                Set up current frame before trying to find the watchpoint
831                frame.  */
832             get_current_frame ();
833             fi = find_frame_addr_in_frame_chain (b->watchpoint_frame);
834             within_current_scope = (fi != NULL);
835             if (within_current_scope)
836               select_frame (fi, -1);
837           }
838
839         if (within_current_scope)
840           {
841             /* Evaluate the expression and cut the chain of values
842                produced off from the value chain.  */
843             v = evaluate_expression (b->exp);
844             value_release_to_mark (mark);
845
846             b->val_chain = v;
847             b->inserted = 1;
848
849             /* Look at each value on the value chain.  */
850             for (; v; v = v->next)
851               {
852                 /* If it's a memory location, then we must watch it.  */
853                 if (v->lval == lval_memory)
854                   {
855                     int addr, len, type;
856
857                     addr = VALUE_ADDRESS (v) + VALUE_OFFSET (v);
858                     len = TYPE_LENGTH (VALUE_TYPE (v));
859                     type   = hw_write;
860                     if (b->type == bp_read_watchpoint)
861                       type = hw_read;
862                     else if (b->type == bp_access_watchpoint)
863                       type = hw_access;
864
865                     val = target_insert_watchpoint (addr, len, type);
866                     if (val == -1)
867                       {
868                         b->inserted = 0;
869                         break;
870                       }
871                     val = 0;
872                   }
873               }
874             /* Failure to insert a watchpoint on any memory value in the
875                value chain brings us here.  */
876             if (!b->inserted)
877               {
878                 remove_breakpoint (b, mark_uninserted);
879                 warning ("Could not insert hardware watchpoint %d.",
880                          b->number);
881                 val = -1;
882               }               
883           }
884         else
885           {
886             printf_filtered ("Hardware watchpoint %d deleted", b->number);
887             printf_filtered ("because the program has left the block \n");
888             printf_filtered ("in which its expression is valid.\n");
889             if (b->related_breakpoint)
890               b->related_breakpoint->disposition = del_at_next_stop;
891             b->disposition = del_at_next_stop;
892           }
893
894         /* Restore the frame and level.  */
895         if ((saved_frame != selected_frame) ||
896             (saved_level != selected_frame_level))
897           select_and_print_frame (saved_frame, saved_level);
898
899         if (val)
900           return_val = val;     /* remember failure */
901       }
902     else if ((b->type == bp_catch_fork
903               || b->type == bp_catch_vfork
904               || b->type == bp_catch_exec)
905              && b->enable == enabled
906              && !b->inserted
907              && !b->duplicate)
908       {
909         val = -1;
910         switch (b->type)
911           {
912           case bp_catch_fork:
913             val = target_insert_fork_catchpoint (inferior_pid);
914             break;
915           case bp_catch_vfork:
916             val = target_insert_vfork_catchpoint (inferior_pid);
917             break;
918           case bp_catch_exec:
919             val = target_insert_exec_catchpoint (inferior_pid);
920             break;
921           default:
922             warning ("Internal error, %s line %d.", __FILE__, __LINE__);
923             break;
924           }
925         if (val < 0)
926           {
927             target_terminal_ours_for_output ();
928             warning ("Cannot insert catchpoint %d.", b->number);
929           }
930         else
931           b->inserted = 1;
932
933         if (val)
934           return_val = val;     /* remember failure */
935       }
936   }
937
938   return return_val;
939 }
940
941
942 int
943 remove_breakpoints ()
944 {
945   register struct breakpoint *b;
946   int val;
947
948   ALL_BREAKPOINTS (b)
949   {
950     if (b->inserted)
951       {
952         val = remove_breakpoint (b, mark_uninserted);
953         if (val != 0)
954           return val;
955       }
956   }
957   return 0;
958 }
959
960 int
961 reattach_breakpoints (pid)
962      int pid;
963 {
964   register struct breakpoint *b;
965   int val;
966   int saved_inferior_pid = inferior_pid;
967
968   /* FIXME: use a cleanup, to insure that inferior_pid gets replaced! */
969   inferior_pid = pid;   /* Because remove_breakpoint will use this global. */
970   ALL_BREAKPOINTS (b)
971   {
972     if (b->inserted)
973       {
974         remove_breakpoint (b, mark_inserted);
975         if (b->type == bp_hardware_breakpoint)
976           val = target_insert_hw_breakpoint (b->address, b->shadow_contents);
977         else
978           val = target_insert_breakpoint (b->address, b->shadow_contents);
979         if (val != 0)
980           {
981             inferior_pid = saved_inferior_pid;
982             return val;
983           }
984       }
985   }
986   inferior_pid = saved_inferior_pid;
987   return 0;
988 }
989
990 void
991 update_breakpoints_after_exec ()
992 {
993   struct breakpoint *b;
994   struct breakpoint *temp;
995
996   /* Doing this first prevents the badness of having delete_breakpoint()
997      write a breakpoint's current "shadow contents" to lift the bp.  That
998      shadow is NOT valid after an exec()! */
999   mark_breakpoints_out ();
1000
1001   ALL_BREAKPOINTS_SAFE (b, temp)
1002   {
1003     /* Solib breakpoints must be explicitly reset after an exec(). */
1004     if (b->type == bp_shlib_event)
1005       {
1006         delete_breakpoint (b);
1007         continue;
1008       }
1009
1010     /* Step-resume breakpoints are meaningless after an exec(). */
1011     if (b->type == bp_step_resume)
1012       {
1013         delete_breakpoint (b);
1014         continue;
1015       }
1016
1017     /* Ditto the sigtramp handler breakpoints. */
1018     if (b->type == bp_through_sigtramp)
1019       {
1020         delete_breakpoint (b);
1021         continue;
1022       }
1023
1024     /* Ditto the exception-handling catchpoints. */
1025     if ((b->type == bp_catch_catch) || (b->type == bp_catch_throw))
1026       {
1027         delete_breakpoint (b);
1028         continue;
1029       }
1030
1031     /* Don't delete an exec catchpoint, because else the inferior
1032        won't stop when it ought!
1033
1034        Similarly, we probably ought to keep vfork catchpoints, 'cause
1035        on this target, we may not be able to stop when the vfork is
1036        seen, but only when the subsequent exec is seen.  (And because
1037        deleting fork catchpoints here but not vfork catchpoints will
1038        seem mysterious to users, keep those too.)
1039
1040        ??rehrauer: Let's hope that merely clearing out this catchpoint's
1041        target address field, if any, is sufficient to have it be reset
1042        automagically.  Certainly on HP-UX that's true. */
1043     if ((b->type == bp_catch_exec) ||
1044         (b->type == bp_catch_vfork) ||
1045         (b->type == bp_catch_fork))
1046       {
1047         b->address = (CORE_ADDR) NULL;
1048         continue;
1049       }
1050
1051     /* bp_finish is a special case.  The only way we ought to be able
1052        to see one of these when an exec() has happened, is if the user
1053        caught a vfork, and then said "finish".  Ordinarily a finish just
1054        carries them to the call-site of the current callee, by setting
1055        a temporary bp there and resuming.  But in this case, the finish
1056        will carry them entirely through the vfork & exec.
1057
1058        We don't want to allow a bp_finish to remain inserted now.  But
1059        we can't safely delete it, 'cause finish_command has a handle to
1060        the bp on a bpstat, and will later want to delete it.  There's a
1061        chance (and I've seen it happen) that if we delete the bp_finish
1062        here, that its storage will get reused by the time finish_command
1063        gets 'round to deleting the "use to be a bp_finish" breakpoint.
1064        We really must allow finish_command to delete a bp_finish.
1065
1066        In the absense of a general solution for the "how do we know
1067        it's safe to delete something others may have handles to?"
1068        problem, what we'll do here is just uninsert the bp_finish, and
1069        let finish_command delete it.
1070
1071        (We know the bp_finish is "doomed" in the sense that it's
1072        momentary, and will be deleted as soon as finish_command sees
1073        the inferior stopped.  So it doesn't matter that the bp's
1074        address is probably bogus in the new a.out, unlike e.g., the
1075        solib breakpoints.)  */
1076
1077     if (b->type == bp_finish)
1078       {
1079         continue;
1080       }
1081
1082     /* Without a symbolic address, we have little hope of the
1083        pre-exec() address meaning the same thing in the post-exec()
1084        a.out. */
1085     if (b->addr_string == NULL)
1086       {
1087         delete_breakpoint (b);
1088         continue;
1089       }
1090
1091     /* If this breakpoint has survived the above battery of checks, then
1092        it must have a symbolic address.  Be sure that it gets reevaluated
1093        to a target address, rather than reusing the old evaluation.  */
1094     b->address = (CORE_ADDR) NULL;
1095   }
1096 }
1097
1098 int
1099 detach_breakpoints (pid)
1100      int pid;
1101 {
1102   register struct breakpoint *b;
1103   int val;
1104   int saved_inferior_pid = inferior_pid;
1105
1106   if (pid == inferior_pid)
1107     error ("Cannot detach breakpoints of inferior_pid");
1108
1109   /* FIXME: use a cleanup, to insure that inferior_pid gets replaced! */
1110   inferior_pid = pid;   /* Because remove_breakpoint will use this global. */
1111   ALL_BREAKPOINTS (b)
1112   {
1113     if (b->inserted)
1114       {
1115         val = remove_breakpoint (b, mark_inserted);
1116         if (val != 0)
1117           {
1118             inferior_pid = saved_inferior_pid;
1119             return val;
1120           }
1121       }
1122   }
1123   inferior_pid = saved_inferior_pid;
1124   return 0;
1125 }
1126
1127 static int
1128 remove_breakpoint (b, is)
1129      struct breakpoint *b;
1130      insertion_state_t is;
1131 {
1132   int val;
1133
1134   if (b->type == bp_none)
1135     warning ("attempted to remove apparently deleted breakpoint #%d?", 
1136              b->number);
1137
1138   if (b->type != bp_watchpoint
1139       && b->type != bp_hardware_watchpoint
1140       && b->type != bp_read_watchpoint
1141       && b->type != bp_access_watchpoint
1142       && b->type != bp_catch_fork
1143       && b->type != bp_catch_vfork
1144       && b->type != bp_catch_exec
1145       && b->type != bp_catch_catch
1146       && b->type != bp_catch_throw)
1147
1148     {
1149       if (b->type == bp_hardware_breakpoint)
1150         val = target_remove_hw_breakpoint (b->address, b->shadow_contents);
1151       else
1152         {
1153           /* Check to see if breakpoint is in an overlay section;
1154              if so, we should remove the breakpoint at the LMA address.
1155              If that is not equal to the raw address, then we should 
1156              presumable remove the breakpoint there as well.  */
1157           if (overlay_debugging && b->section &&
1158               section_is_overlay (b->section))
1159             {
1160               CORE_ADDR addr;
1161
1162               addr = overlay_unmapped_address (b->address, b->section);
1163               val = target_remove_breakpoint (addr, b->shadow_contents);
1164               /* This would be the time to check val, to see if the
1165                  shadow breakpoint write to the load address succeeded.  
1166                  However, this might be an ordinary occurrance, eg. if 
1167                  the unmapped overlay is in ROM.  */
1168               val = 0;          /* in case unmapped address failed */
1169               if (section_is_mapped (b->section))
1170                 val = target_remove_breakpoint (b->address,
1171                                                 b->shadow_contents);
1172             }
1173           else                  /* ordinary (non-overlay) address */
1174             val = target_remove_breakpoint (b->address, b->shadow_contents);
1175         }
1176       if (val)
1177         return val;
1178       b->inserted = (is == mark_inserted);
1179     }
1180   else if ((b->type == bp_hardware_watchpoint ||
1181             b->type == bp_read_watchpoint ||
1182             b->type == bp_access_watchpoint)
1183            && b->enable == enabled
1184            && !b->duplicate)
1185     {
1186       value_ptr v, n;
1187
1188       b->inserted = (is == mark_inserted);
1189       /* Walk down the saved value chain.  */
1190       for (v = b->val_chain; v; v = v->next)
1191         {
1192           /* For each memory reference remove the watchpoint
1193              at that address.  */
1194           if (v->lval == lval_memory)
1195             {
1196               int addr, len, type;
1197
1198               addr = VALUE_ADDRESS (v) + VALUE_OFFSET (v);
1199               len = TYPE_LENGTH (VALUE_TYPE (v));
1200               type   = hw_write;
1201               if (b->type == bp_read_watchpoint)
1202                 type = hw_read;
1203               else if (b->type == bp_access_watchpoint)
1204                 type = hw_access;
1205
1206               val = target_remove_watchpoint (addr, len, type);
1207               if (val == -1)
1208                 b->inserted = 1;
1209               val = 0;
1210             }
1211         }
1212       /* Failure to remove any of the hardware watchpoints comes here.  */
1213       if ((is == mark_uninserted) && (b->inserted))
1214         warning ("Could not remove hardware watchpoint %d.",
1215                  b->number);
1216
1217       /* Free the saved value chain.  We will construct a new one
1218          the next time the watchpoint is inserted.  */
1219       for (v = b->val_chain; v; v = n)
1220         {
1221           n = v->next;
1222           value_free (v);
1223         }
1224       b->val_chain = NULL;
1225     }
1226   else if ((b->type == bp_catch_fork ||
1227             b->type == bp_catch_vfork ||
1228             b->type == bp_catch_exec)
1229            && b->enable == enabled
1230            && !b->duplicate)
1231     {
1232       val = -1;
1233       switch (b->type)
1234         {
1235         case bp_catch_fork:
1236           val = target_remove_fork_catchpoint (inferior_pid);
1237           break;
1238         case bp_catch_vfork:
1239           val = target_remove_vfork_catchpoint (inferior_pid);
1240           break;
1241         case bp_catch_exec:
1242           val = target_remove_exec_catchpoint (inferior_pid);
1243           break;
1244         default:
1245           warning ("Internal error, %s line %d.", __FILE__, __LINE__);
1246           break;
1247         }
1248       if (val)
1249         return val;
1250       b->inserted = (is == mark_inserted);
1251     }
1252   else if ((b->type == bp_catch_catch ||
1253             b->type == bp_catch_throw)
1254            && b->enable == enabled
1255            && !b->duplicate)
1256     {
1257
1258       val = target_remove_breakpoint (b->address, b->shadow_contents);
1259       if (val)
1260         return val;
1261       b->inserted = (is == mark_inserted);
1262     }
1263   else if (ep_is_exception_catchpoint (b)
1264            && b->inserted       /* sometimes previous insert doesn't happen */
1265            && b->enable == enabled
1266            && !b->duplicate)
1267     {
1268
1269       val = target_remove_breakpoint (b->address, b->shadow_contents);
1270       if (val)
1271         return val;
1272
1273       b->inserted = (is == mark_inserted);
1274     }
1275
1276   return 0;
1277 }
1278
1279 /* Clear the "inserted" flag in all breakpoints.  */
1280
1281 void
1282 mark_breakpoints_out ()
1283 {
1284   register struct breakpoint *b;
1285
1286   ALL_BREAKPOINTS (b)
1287     b->inserted = 0;
1288 }
1289
1290 /* Clear the "inserted" flag in all breakpoints and delete any
1291    breakpoints which should go away between runs of the program.
1292
1293    Plus other such housekeeping that has to be done for breakpoints
1294    between runs.
1295
1296    Note: this function gets called at the end of a run (by
1297    generic_mourn_inferior) and when a run begins (by
1298    init_wait_for_inferior). */
1299
1300
1301
1302 void
1303 breakpoint_init_inferior (context)
1304      enum inf_context context;
1305 {
1306   register struct breakpoint *b, *temp;
1307   static int warning_needed = 0;
1308
1309   ALL_BREAKPOINTS_SAFE (b, temp)
1310   {
1311     b->inserted = 0;
1312
1313     switch (b->type)
1314       {
1315       case bp_call_dummy:
1316       case bp_watchpoint_scope:
1317
1318         /* If the call dummy breakpoint is at the entry point it will
1319            cause problems when the inferior is rerun, so we better
1320            get rid of it. 
1321
1322            Also get rid of scope breakpoints.  */
1323         delete_breakpoint (b);
1324         break;
1325
1326       case bp_watchpoint:
1327       case bp_hardware_watchpoint:
1328       case bp_read_watchpoint:
1329       case bp_access_watchpoint:
1330
1331         /* Likewise for watchpoints on local expressions.  */
1332         if (b->exp_valid_block != NULL)
1333           delete_breakpoint (b);
1334         break;
1335       default:
1336         /* Likewise for exception catchpoints in dynamic-linked
1337            executables where required */
1338         if (ep_is_exception_catchpoint (b) &&
1339             exception_catchpoints_are_fragile)
1340           {
1341             warning_needed = 1;
1342             delete_breakpoint (b);
1343           }
1344         break;
1345       }
1346   }
1347
1348   if (exception_catchpoints_are_fragile)
1349     exception_support_initialized = 0;
1350
1351   /* Don't issue the warning unless it's really needed... */
1352   if (warning_needed && (context != inf_exited))
1353     {
1354       warning ("Exception catchpoints from last run were deleted.");
1355       warning ("You must reinsert them explicitly.");
1356       warning_needed = 0;
1357     }
1358 }
1359
1360 /* breakpoint_here_p (PC) returns 1 if an enabled breakpoint exists at
1361    PC.  When continuing from a location with a breakpoint, we actually
1362    single step once before calling insert_breakpoints.  */
1363
1364 int
1365 breakpoint_here_p (pc)
1366      CORE_ADDR pc;
1367 {
1368   register struct breakpoint *b;
1369
1370   ALL_BREAKPOINTS (b)
1371     if (b->enable == enabled
1372         && b->enable != shlib_disabled
1373         && b->enable != call_disabled
1374         && b->address == pc)    /* bp is enabled and matches pc */
1375     {
1376       if (overlay_debugging &&
1377           section_is_overlay (b->section) &&
1378           !section_is_mapped (b->section))
1379         continue;               /* unmapped overlay -- can't be a match */
1380       else
1381         return 1;
1382     }
1383
1384   return 0;
1385 }
1386
1387 /* breakpoint_inserted_here_p (PC) is just like breakpoint_here_p(),
1388    but it only returns true if there is actually a breakpoint inserted
1389    at PC.  */
1390
1391 int
1392 breakpoint_inserted_here_p (pc)
1393      CORE_ADDR pc;
1394 {
1395   register struct breakpoint *b;
1396
1397   ALL_BREAKPOINTS (b)
1398     if (b->inserted
1399         && b->address == pc)    /* bp is inserted and matches pc */
1400     {
1401       if (overlay_debugging &&
1402           section_is_overlay (b->section) &&
1403           !section_is_mapped (b->section))
1404         continue;               /* unmapped overlay -- can't be a match */
1405       else
1406         return 1;
1407     }
1408
1409   return 0;
1410 }
1411
1412 /* Return nonzero if FRAME is a dummy frame.  We can't use
1413    PC_IN_CALL_DUMMY because figuring out the saved SP would take too
1414    much time, at least using get_saved_register on the 68k.  This
1415    means that for this function to work right a port must use the
1416    bp_call_dummy breakpoint.  */
1417
1418 int
1419 frame_in_dummy (frame)
1420      struct frame_info *frame;
1421 {
1422   struct breakpoint *b;
1423
1424   if (!CALL_DUMMY_P)
1425     return 0;
1426
1427   if (USE_GENERIC_DUMMY_FRAMES)
1428     return generic_pc_in_call_dummy (frame->pc, frame->frame, frame->frame);
1429
1430   ALL_BREAKPOINTS (b)
1431   {
1432     if (b->type == bp_call_dummy
1433         && b->frame == frame->frame
1434     /* We need to check the PC as well as the frame on the sparc,
1435        for signals.exp in the testsuite.  */
1436         && (frame->pc
1437             >= (b->address
1438               - SIZEOF_CALL_DUMMY_WORDS / sizeof (LONGEST) * REGISTER_SIZE))
1439         && frame->pc <= b->address)
1440       return 1;
1441   }
1442   return 0;
1443 }
1444
1445 /* breakpoint_match_thread (PC, PID) returns true if the breakpoint at PC
1446    is valid for process/thread PID.  */
1447
1448 int
1449 breakpoint_thread_match (pc, pid)
1450      CORE_ADDR pc;
1451      int pid;
1452 {
1453   struct breakpoint *b;
1454   int thread;
1455
1456   thread = pid_to_thread_id (pid);
1457
1458   ALL_BREAKPOINTS (b)
1459     if (b->enable != disabled
1460         && b->enable != shlib_disabled
1461         && b->enable != call_disabled
1462         && b->address == pc
1463         && (b->thread == -1 || b->thread == thread))
1464     {
1465       if (overlay_debugging &&
1466           section_is_overlay (b->section) &&
1467           !section_is_mapped (b->section))
1468         continue;               /* unmapped overlay -- can't be a match */
1469       else
1470         return 1;
1471     }
1472
1473   return 0;
1474 }
1475 \f
1476
1477 /* bpstat stuff.  External routines' interfaces are documented
1478    in breakpoint.h.  */
1479
1480 int
1481 ep_is_catchpoint (ep)
1482      struct breakpoint *ep;
1483 {
1484   return
1485     (ep->type == bp_catch_load)
1486     || (ep->type == bp_catch_unload)
1487     || (ep->type == bp_catch_fork)
1488     || (ep->type == bp_catch_vfork)
1489     || (ep->type == bp_catch_exec)
1490     || (ep->type == bp_catch_catch)
1491     || (ep->type == bp_catch_throw)
1492
1493
1494   /* ??rehrauer: Add more kinds here, as are implemented... */
1495     ;
1496 }
1497
1498 int
1499 ep_is_shlib_catchpoint (ep)
1500      struct breakpoint *ep;
1501 {
1502   return
1503     (ep->type == bp_catch_load)
1504     || (ep->type == bp_catch_unload)
1505     ;
1506 }
1507
1508 int
1509 ep_is_exception_catchpoint (ep)
1510      struct breakpoint *ep;
1511 {
1512   return
1513     (ep->type == bp_catch_catch)
1514     || (ep->type == bp_catch_throw)
1515     ;
1516 }
1517
1518 /* Clear a bpstat so that it says we are not at any breakpoint.
1519    Also free any storage that is part of a bpstat.  */
1520
1521 void
1522 bpstat_clear (bsp)
1523      bpstat *bsp;
1524 {
1525   bpstat p;
1526   bpstat q;
1527
1528   if (bsp == 0)
1529     return;
1530   p = *bsp;
1531   while (p != NULL)
1532     {
1533       q = p->next;
1534       if (p->old_val != NULL)
1535         value_free (p->old_val);
1536       free ((PTR) p);
1537       p = q;
1538     }
1539   *bsp = NULL;
1540 }
1541
1542 /* Return a copy of a bpstat.  Like "bs1 = bs2" but all storage that
1543    is part of the bpstat is copied as well.  */
1544
1545 bpstat
1546 bpstat_copy (bs)
1547      bpstat bs;
1548 {
1549   bpstat p = NULL;
1550   bpstat tmp;
1551   bpstat retval = NULL;
1552
1553   if (bs == NULL)
1554     return bs;
1555
1556   for (; bs != NULL; bs = bs->next)
1557     {
1558       tmp = (bpstat) xmalloc (sizeof (*tmp));
1559       memcpy (tmp, bs, sizeof (*tmp));
1560       if (p == NULL)
1561         /* This is the first thing in the chain.  */
1562         retval = tmp;
1563       else
1564         p->next = tmp;
1565       p = tmp;
1566     }
1567   p->next = NULL;
1568   return retval;
1569 }
1570
1571 /* Find the bpstat associated with this breakpoint */
1572
1573 bpstat
1574 bpstat_find_breakpoint (bsp, breakpoint)
1575      bpstat bsp;
1576      struct breakpoint *breakpoint;
1577 {
1578   if (bsp == NULL)
1579     return NULL;
1580
1581   for (; bsp != NULL; bsp = bsp->next)
1582     {
1583       if (bsp->breakpoint_at == breakpoint)
1584         return bsp;
1585     }
1586   return NULL;
1587 }
1588
1589 /* Find a step_resume breakpoint associated with this bpstat.
1590    (If there are multiple step_resume bp's on the list, this function
1591    will arbitrarily pick one.)
1592
1593    It is an error to use this function if BPSTAT doesn't contain a
1594    step_resume breakpoint.
1595
1596    See wait_for_inferior's use of this function.  */
1597 struct breakpoint *
1598 bpstat_find_step_resume_breakpoint (bsp)
1599      bpstat bsp;
1600 {
1601   if (bsp == NULL)
1602     error ("Internal error (bpstat_find_step_resume_breakpoint)");
1603
1604   for (; bsp != NULL; bsp = bsp->next)
1605     {
1606       if ((bsp->breakpoint_at != NULL) &&
1607           (bsp->breakpoint_at->type == bp_step_resume))
1608         return bsp->breakpoint_at;
1609     }
1610
1611   error ("Internal error (no step_resume breakpoint found)");
1612 }
1613
1614
1615 /* Return the breakpoint number of the first breakpoint we are stopped
1616    at.  *BSP upon return is a bpstat which points to the remaining
1617    breakpoints stopped at (but which is not guaranteed to be good for
1618    anything but further calls to bpstat_num).
1619    Return 0 if passed a bpstat which does not indicate any breakpoints.  */
1620
1621 int
1622 bpstat_num (bsp)
1623      bpstat *bsp;
1624 {
1625   struct breakpoint *b;
1626
1627   if ((*bsp) == NULL)
1628     return 0;                   /* No more breakpoint values */
1629   else
1630     {
1631       b = (*bsp)->breakpoint_at;
1632       *bsp = (*bsp)->next;
1633       if (b == NULL)
1634         return -1;              /* breakpoint that's been deleted since */
1635       else
1636         return b->number;       /* We have its number */
1637     }
1638 }
1639
1640 /* Modify BS so that the actions will not be performed.  */
1641
1642 void
1643 bpstat_clear_actions (bs)
1644      bpstat bs;
1645 {
1646   for (; bs != NULL; bs = bs->next)
1647     {
1648       bs->commands = NULL;
1649       if (bs->old_val != NULL)
1650         {
1651           value_free (bs->old_val);
1652           bs->old_val = NULL;
1653         }
1654     }
1655 }
1656
1657 /* Stub for cleaning up our state if we error-out of a breakpoint command */
1658 /* ARGSUSED */
1659 static void
1660 cleanup_executing_breakpoints (ignore)
1661      PTR ignore;
1662 {
1663   executing_breakpoint_commands = 0;
1664 }
1665
1666 /* Execute all the commands associated with all the breakpoints at this
1667    location.  Any of these commands could cause the process to proceed
1668    beyond this point, etc.  We look out for such changes by checking
1669    the global "breakpoint_proceeded" after each command.  */
1670
1671 void
1672 bpstat_do_actions (bsp)
1673      bpstat *bsp;
1674 {
1675   bpstat bs;
1676   struct cleanup *old_chain;
1677   struct command_line *cmd;
1678
1679   /* Avoid endless recursion if a `source' command is contained
1680      in bs->commands.  */
1681   if (executing_breakpoint_commands)
1682     return;
1683
1684   executing_breakpoint_commands = 1;
1685   old_chain = make_cleanup (cleanup_executing_breakpoints, 0);
1686
1687 top:
1688   /* Note that (as of this writing), our callers all appear to
1689      be passing us the address of global stop_bpstat.  And, if
1690      our calls to execute_control_command cause the inferior to
1691      proceed, that global (and hence, *bsp) will change.
1692
1693      We must be careful to not touch *bsp unless the inferior
1694      has not proceeded. */
1695
1696   /* This pointer will iterate over the list of bpstat's. */
1697   bs = *bsp;
1698
1699   breakpoint_proceeded = 0;
1700   for (; bs != NULL; bs = bs->next)
1701     {
1702       cmd = bs->commands;
1703       while (cmd != NULL)
1704         {
1705           execute_control_command (cmd);
1706
1707           if (breakpoint_proceeded)
1708             break;
1709           else
1710             cmd = cmd->next;
1711         }
1712       if (breakpoint_proceeded)
1713         /* The inferior is proceeded by the command; bomb out now.
1714            The bpstat chain has been blown away by wait_for_inferior.
1715            But since execution has stopped again, there is a new bpstat
1716            to look at, so start over.  */
1717         goto top;
1718       else
1719         bs->commands = NULL;
1720     }
1721
1722   executing_breakpoint_commands = 0;
1723   discard_cleanups (old_chain);
1724 }
1725
1726 /* This is the normal print_it function for a bpstat.  In the future,
1727    much of this logic could (should?) be moved to bpstat_stop_status,
1728    by having it set different print_it functions.
1729
1730    Current scheme: When we stop, bpstat_print() is called.
1731    It loops through the bpstat list of things causing this stop,
1732    calling the print_it function for each one. The default
1733    print_it function, used for breakpoints, is print_it_normal().
1734    (Also see print_it_noop() and print_it_done()).
1735
1736    Return values from this routine (used by bpstat_print() to
1737    decide what to do):
1738    1: Means we printed something, and we do *not* desire that
1739    something to be followed by a location.
1740    0: Means we printed something, and we *do*  desire that
1741    something to be followed by a location.
1742    -1: Means we printed nothing.  */
1743
1744 static int
1745 print_it_normal (bs)
1746      bpstat bs;
1747 {
1748   /* bs->breakpoint_at can be NULL if it was a momentary breakpoint
1749      which has since been deleted.  */
1750   if (bs->breakpoint_at == NULL
1751       || (bs->breakpoint_at->type != bp_breakpoint
1752           && bs->breakpoint_at->type != bp_catch_load
1753           && bs->breakpoint_at->type != bp_catch_unload
1754           && bs->breakpoint_at->type != bp_catch_fork
1755           && bs->breakpoint_at->type != bp_catch_vfork
1756           && bs->breakpoint_at->type != bp_catch_exec
1757           && bs->breakpoint_at->type != bp_catch_catch
1758           && bs->breakpoint_at->type != bp_catch_throw
1759           && bs->breakpoint_at->type != bp_hardware_breakpoint
1760           && bs->breakpoint_at->type != bp_watchpoint
1761           && bs->breakpoint_at->type != bp_read_watchpoint
1762           && bs->breakpoint_at->type != bp_access_watchpoint
1763           && bs->breakpoint_at->type != bp_hardware_watchpoint))
1764     return -1;
1765
1766   if (ep_is_shlib_catchpoint (bs->breakpoint_at))
1767     {
1768       annotate_catchpoint (bs->breakpoint_at->number);
1769       printf_filtered ("\nCatchpoint %d (", bs->breakpoint_at->number);
1770       if (bs->breakpoint_at->type == bp_catch_load)
1771         printf_filtered ("loaded");
1772       else if (bs->breakpoint_at->type == bp_catch_unload)
1773         printf_filtered ("unloaded");
1774       printf_filtered (" %s), ", bs->breakpoint_at->triggered_dll_pathname);
1775       return 0;
1776     }
1777   else if (bs->breakpoint_at->type == bp_catch_fork ||
1778            bs->breakpoint_at->type == bp_catch_vfork)
1779     {
1780       annotate_catchpoint (bs->breakpoint_at->number);
1781       printf_filtered ("\nCatchpoint %d (", bs->breakpoint_at->number);
1782       if (bs->breakpoint_at->type == bp_catch_fork)
1783         printf_filtered ("forked");
1784       else if (bs->breakpoint_at->type == bp_catch_vfork)
1785         printf_filtered ("vforked");
1786       printf_filtered (" process %d), ", 
1787                        bs->breakpoint_at->forked_inferior_pid);
1788       return 0;
1789     }
1790   else if (bs->breakpoint_at->type == bp_catch_exec)
1791     {
1792       annotate_catchpoint (bs->breakpoint_at->number);
1793       printf_filtered ("\nCatchpoint %d (exec'd %s), ",
1794                        bs->breakpoint_at->number,
1795                        bs->breakpoint_at->exec_pathname);
1796       return 0;
1797     }
1798   else if (bs->breakpoint_at->type == bp_catch_catch)
1799     {
1800       if (current_exception_event && 
1801           (CURRENT_EXCEPTION_KIND == EX_EVENT_CATCH))
1802         {
1803           annotate_catchpoint (bs->breakpoint_at->number);
1804           printf_filtered ("\nCatchpoint %d (exception caught), ", 
1805                            bs->breakpoint_at->number);
1806           printf_filtered ("throw location ");
1807           if (CURRENT_EXCEPTION_THROW_PC && CURRENT_EXCEPTION_THROW_LINE)
1808             printf_filtered ("%s:%d",
1809                              CURRENT_EXCEPTION_THROW_FILE,
1810                              CURRENT_EXCEPTION_THROW_LINE);
1811           else
1812             printf_filtered ("unknown");
1813
1814           printf_filtered (", catch location ");
1815           if (CURRENT_EXCEPTION_CATCH_PC && CURRENT_EXCEPTION_CATCH_LINE)
1816             printf_filtered ("%s:%d",
1817                              CURRENT_EXCEPTION_CATCH_FILE,
1818                              CURRENT_EXCEPTION_CATCH_LINE);
1819           else
1820             printf_filtered ("unknown");
1821
1822           printf_filtered ("\n");
1823           return 1;     /* don't bother to print location frame info */
1824         }
1825       else
1826         {
1827           return -1;    /* really throw, some other bpstat will handle it */
1828         }
1829     }
1830   else if (bs->breakpoint_at->type == bp_catch_throw)
1831     {
1832       if (current_exception_event && 
1833           (CURRENT_EXCEPTION_KIND == EX_EVENT_THROW))
1834         {
1835           annotate_catchpoint (bs->breakpoint_at->number);
1836           printf_filtered ("\nCatchpoint %d (exception thrown), ",
1837                            bs->breakpoint_at->number);
1838           printf_filtered ("throw location ");
1839           if (CURRENT_EXCEPTION_THROW_PC && CURRENT_EXCEPTION_THROW_LINE)
1840             printf_filtered ("%s:%d",
1841                              CURRENT_EXCEPTION_THROW_FILE,
1842                              CURRENT_EXCEPTION_THROW_LINE);
1843           else
1844             printf_filtered ("unknown");
1845
1846           printf_filtered (", catch location ");
1847           if (CURRENT_EXCEPTION_CATCH_PC && CURRENT_EXCEPTION_CATCH_LINE)
1848             printf_filtered ("%s:%d",
1849                              CURRENT_EXCEPTION_CATCH_FILE,
1850                              CURRENT_EXCEPTION_CATCH_LINE);
1851           else
1852             printf_filtered ("unknown");
1853
1854           printf_filtered ("\n");
1855           return 1;     /* don't bother to print location frame info */
1856         }
1857       else
1858         {
1859           return -1;    /* really catch, some other bpstat willhandle it */
1860         }
1861     }
1862
1863   else if (bs->breakpoint_at->type == bp_breakpoint ||
1864            bs->breakpoint_at->type == bp_hardware_breakpoint)
1865     {
1866       /* I think the user probably only wants to see one breakpoint
1867          number, not all of them.  */
1868       annotate_breakpoint (bs->breakpoint_at->number);
1869       printf_filtered ("\nBreakpoint %d, ", bs->breakpoint_at->number);
1870       return 0;
1871     }
1872   else if ((bs->old_val != NULL) &&
1873            (bs->breakpoint_at->type == bp_watchpoint ||
1874             bs->breakpoint_at->type == bp_access_watchpoint ||
1875             bs->breakpoint_at->type == bp_hardware_watchpoint))
1876     {
1877       annotate_watchpoint (bs->breakpoint_at->number);
1878       mention (bs->breakpoint_at);
1879       printf_filtered ("\nOld value = ");
1880       value_print (bs->old_val, gdb_stdout, 0, Val_pretty_default);
1881       printf_filtered ("\nNew value = ");
1882       value_print (bs->breakpoint_at->val, gdb_stdout, 0,
1883                    Val_pretty_default);
1884       printf_filtered ("\n");
1885       value_free (bs->old_val);
1886       bs->old_val = NULL;
1887       /* More than one watchpoint may have been triggered.  */
1888       return -1;
1889     }
1890   else if (bs->breakpoint_at->type == bp_access_watchpoint ||
1891            bs->breakpoint_at->type == bp_read_watchpoint)
1892     {
1893       mention (bs->breakpoint_at);
1894       printf_filtered ("\nValue = ");
1895       value_print (bs->breakpoint_at->val, gdb_stdout, 0,
1896                    Val_pretty_default);
1897       printf_filtered ("\n");
1898       return -1;
1899     }
1900   /* We can't deal with it.  
1901      Maybe another member of the bpstat chain can.  */
1902   return -1;
1903 }
1904
1905 /* Print a message indicating what happened.
1906    This is called from normal_stop().
1907    The input to this routine is the head of the bpstat list - a list
1908    of the eventpoints that caused this stop.
1909    This routine calls the "print_it" routine(s) associated
1910    with these eventpoints. This will print (for example)
1911    the "Breakpoint n," part of the output.
1912    The return value of this routine is one of:
1913
1914    -1: Means we printed nothing
1915    0: Means we printed something, and expect subsequent
1916    code to print the location. An example is 
1917    "Breakpoint 1, " which should be followed by
1918    the location.
1919    1 : Means we printed something, but there is no need
1920    to also print the location part of the message.
1921    An example is the catch/throw messages, which
1922    don't require a location appended to the end.  */
1923
1924 int
1925 bpstat_print (bs)
1926      bpstat bs;
1927 {
1928   int val;
1929
1930   if (bs == NULL)
1931     return -1;
1932
1933   val = (*bs->print_it) (bs);
1934   if (val >= 0)
1935     return val;
1936
1937   /* Maybe another breakpoint in the chain caused us to stop.
1938      (Currently all watchpoints go on the bpstat whether hit or not.
1939      That probably could (should) be changed, provided care is taken
1940      with respect to bpstat_explains_signal).  */
1941   if (bs->next)
1942     return bpstat_print (bs->next);
1943
1944   /* We reached the end of the chain without printing anything.  */
1945   return -1;
1946 }
1947
1948 /* Evaluate the expression EXP and return 1 if value is zero.
1949    This is used inside a catch_errors to evaluate the breakpoint condition. 
1950    The argument is a "struct expression *" that has been cast to char * to 
1951    make it pass through catch_errors.  */
1952
1953 static int
1954 breakpoint_cond_eval (exp)
1955      PTR exp;
1956 {
1957   value_ptr mark = value_mark ();
1958   int i = !value_true (evaluate_expression ((struct expression *) exp));
1959   value_free_to_mark (mark);
1960   return i;
1961 }
1962
1963 /* Allocate a new bpstat and chain it to the current one.  */
1964
1965 static bpstat
1966 bpstat_alloc (b, cbs)
1967      register struct breakpoint *b;
1968      bpstat cbs;                /* Current "bs" value */
1969 {
1970   bpstat bs;
1971
1972   bs = (bpstat) xmalloc (sizeof (*bs));
1973   cbs->next = bs;
1974   bs->breakpoint_at = b;
1975   /* If the condition is false, etc., don't do the commands.  */
1976   bs->commands = NULL;
1977   bs->old_val = NULL;
1978   bs->print_it = print_it_normal;
1979   return bs;
1980 }
1981 \f
1982 /* Possible return values for watchpoint_check (this can't be an enum
1983    because of check_errors).  */
1984 /* The watchpoint has been deleted.  */
1985 #define WP_DELETED 1
1986 /* The value has changed.  */
1987 #define WP_VALUE_CHANGED 2
1988 /* The value has not changed.  */
1989 #define WP_VALUE_NOT_CHANGED 3
1990
1991 #define BP_TEMPFLAG 1
1992 #define BP_HARDWAREFLAG 2
1993
1994 /* Check watchpoint condition.  */
1995
1996 static int
1997 watchpoint_check (p)
1998      PTR p;
1999 {
2000   bpstat bs = (bpstat) p;
2001   struct breakpoint *b;
2002   struct frame_info *fr;
2003   int within_current_scope;
2004
2005   b = bs->breakpoint_at;
2006
2007   if (b->exp_valid_block == NULL)
2008     within_current_scope = 1;
2009   else
2010     {
2011       /* There is no current frame at this moment.  If we're going to have
2012          any chance of handling watchpoints on local variables, we'll need
2013          the frame chain (so we can determine if we're in scope).  */
2014       reinit_frame_cache ();
2015       fr = find_frame_addr_in_frame_chain (b->watchpoint_frame);
2016       within_current_scope = (fr != NULL);
2017       if (within_current_scope)
2018         /* If we end up stopping, the current frame will get selected
2019            in normal_stop.  So this call to select_frame won't affect
2020            the user.  */
2021         select_frame (fr, -1);
2022     }
2023
2024   if (within_current_scope)
2025     {
2026       /* We use value_{,free_to_}mark because it could be a
2027          *long* time before we return to the command level and
2028          call free_all_values.  We can't call free_all_values because
2029          we might be in the middle of evaluating a function call.  */
2030
2031       value_ptr mark = value_mark ();
2032       value_ptr new_val = evaluate_expression (bs->breakpoint_at->exp);
2033       if (!value_equal (b->val, new_val))
2034         {
2035           release_value (new_val);
2036           value_free_to_mark (mark);
2037           bs->old_val = b->val;
2038           b->val = new_val;
2039           /* We will stop here */
2040           return WP_VALUE_CHANGED;
2041         }
2042       else
2043         {
2044           /* Nothing changed, don't do anything.  */
2045           value_free_to_mark (mark);
2046           /* We won't stop here */
2047           return WP_VALUE_NOT_CHANGED;
2048         }
2049     }
2050   else
2051     {
2052       /* This seems like the only logical thing to do because
2053          if we temporarily ignored the watchpoint, then when
2054          we reenter the block in which it is valid it contains
2055          garbage (in the case of a function, it may have two
2056          garbage values, one before and one after the prologue).
2057          So we can't even detect the first assignment to it and
2058          watch after that (since the garbage may or may not equal
2059          the first value assigned).  */
2060       printf_filtered ("\
2061 Watchpoint %d deleted because the program has left the block in\n\
2062 which its expression is valid.\n", bs->breakpoint_at->number);
2063       if (b->related_breakpoint)
2064         b->related_breakpoint->disposition = del_at_next_stop;
2065       b->disposition = del_at_next_stop;
2066
2067       return WP_DELETED;
2068     }
2069 }
2070
2071 /* This is used when everything which needs to be printed has
2072    already been printed.  But we still want to print the frame.  */
2073
2074 /* Background: When we stop, bpstat_print() is called.
2075    It loops through the bpstat list of things causing this stop,
2076    calling the print_it function for each one. The default
2077    print_it function, used for breakpoints, is print_it_normal().
2078    Also see print_it_noop() and print_it_done() are the other 
2079    two possibilities. See comments in bpstat_print() and
2080    in header of print_it_normal() for more detail.  */
2081
2082 static int
2083 print_it_done (bs)
2084      bpstat bs;
2085 {
2086   return 0;
2087 }
2088
2089 /* This is used when nothing should be printed for this bpstat entry.  */
2090 /* Background: When we stop, bpstat_print() is called.
2091    It loops through the bpstat list of things causing this stop,
2092    calling the print_it function for each one. The default
2093    print_it function, used for breakpoints, is print_it_normal().
2094    Also see print_it_noop() and print_it_done() are the other 
2095    two possibilities. See comments in bpstat_print() and
2096    in header of print_it_normal() for more detail.  */
2097
2098 static int
2099 print_it_noop (bs)
2100      bpstat bs;
2101 {
2102   return -1;
2103 }
2104
2105 /* Get a bpstat associated with having just stopped at address *PC
2106    and frame address CORE_ADDRESS.  Update *PC to point at the
2107    breakpoint (if we hit a breakpoint).  NOT_A_BREAKPOINT is nonzero
2108    if this is known to not be a real breakpoint (it could still be a
2109    watchpoint, though).  */
2110
2111 /* Determine whether we stopped at a breakpoint, etc, or whether we
2112    don't understand this stop.  Result is a chain of bpstat's such that:
2113
2114    if we don't understand the stop, the result is a null pointer.
2115
2116    if we understand why we stopped, the result is not null.
2117
2118    Each element of the chain refers to a particular breakpoint or
2119    watchpoint at which we have stopped.  (We may have stopped for
2120    several reasons concurrently.)
2121
2122    Each element of the chain has valid next, breakpoint_at,
2123    commands, FIXME??? fields.  */
2124
2125 bpstat
2126 bpstat_stop_status (pc, not_a_breakpoint)
2127      CORE_ADDR *pc;
2128      int not_a_breakpoint;
2129 {
2130   register struct breakpoint *b, *temp;
2131   CORE_ADDR bp_addr;
2132   /* True if we've hit a breakpoint (as opposed to a watchpoint).  */
2133   int real_breakpoint = 0;
2134   /* Root of the chain of bpstat's */
2135   struct bpstats root_bs[1];
2136   /* Pointer to the last thing in the chain currently.  */
2137   bpstat bs = root_bs;
2138   static char message1[] =
2139   "Error evaluating expression for watchpoint %d\n";
2140   char message[sizeof (message1) + 30 /* slop */ ];
2141
2142   /* Get the address where the breakpoint would have been.  */
2143   bp_addr = *pc - DECR_PC_AFTER_BREAK;
2144
2145   ALL_BREAKPOINTS_SAFE (b, temp)
2146   {
2147     if (b->enable == disabled
2148         || b->enable == shlib_disabled
2149         || b->enable == call_disabled)
2150       continue;
2151
2152     if (b->type != bp_watchpoint
2153         && b->type != bp_hardware_watchpoint
2154         && b->type != bp_read_watchpoint
2155         && b->type != bp_access_watchpoint
2156         && b->type != bp_hardware_breakpoint
2157         && b->type != bp_catch_fork
2158         && b->type != bp_catch_vfork
2159         && b->type != bp_catch_exec
2160         && b->type != bp_catch_catch
2161         && b->type != bp_catch_throw)   /* a non-watchpoint bp */
2162       if (b->address != bp_addr ||      /* address doesn't match or */
2163           (overlay_debugging && /* overlay doesn't match */
2164            section_is_overlay (b->section) &&
2165            !section_is_mapped (b->section)))
2166         continue;
2167
2168     if (b->type == bp_hardware_breakpoint
2169         && b->address != (*pc - DECR_PC_AFTER_HW_BREAK))
2170       continue;
2171
2172     if (b->type != bp_watchpoint
2173         && b->type != bp_hardware_watchpoint
2174         && b->type != bp_read_watchpoint
2175         && b->type != bp_access_watchpoint
2176         && not_a_breakpoint)
2177       continue;
2178
2179     /* Is this a catchpoint of a load or unload?  If so, did we
2180        get a load or unload of the specified library?  If not,
2181        ignore it. */
2182     if ((b->type == bp_catch_load)
2183 #if defined(SOLIB_HAVE_LOAD_EVENT)
2184         && (!SOLIB_HAVE_LOAD_EVENT (inferior_pid)
2185             || ((b->dll_pathname != NULL)
2186                 && (strcmp (b->dll_pathname, 
2187                             SOLIB_LOADED_LIBRARY_PATHNAME (inferior_pid)) 
2188                     != 0)))
2189 #endif
2190       )
2191       continue;
2192
2193     if ((b->type == bp_catch_unload)
2194 #if defined(SOLIB_HAVE_UNLOAD_EVENT)
2195         && (!SOLIB_HAVE_UNLOAD_EVENT (inferior_pid)
2196             || ((b->dll_pathname != NULL)
2197                 && (strcmp (b->dll_pathname, 
2198                             SOLIB_UNLOADED_LIBRARY_PATHNAME (inferior_pid)) 
2199                     != 0)))
2200 #endif
2201       )
2202       continue;
2203
2204     if ((b->type == bp_catch_fork)
2205         && !target_has_forked (inferior_pid, &b->forked_inferior_pid))
2206       continue;
2207
2208     if ((b->type == bp_catch_vfork)
2209         && !target_has_vforked (inferior_pid, &b->forked_inferior_pid))
2210       continue;
2211
2212     if ((b->type == bp_catch_exec)
2213         && !target_has_execd (inferior_pid, &b->exec_pathname))
2214       continue;
2215
2216     if (ep_is_exception_catchpoint (b) &&
2217         !(current_exception_event = target_get_current_exception_event ()))
2218       continue;
2219
2220     /* Come here if it's a watchpoint, or if the break address matches */
2221
2222     bs = bpstat_alloc (b, bs);  /* Alloc a bpstat to explain stop */
2223
2224     /* Watchpoints may change this, if not found to have triggered. */
2225     bs->stop = 1;
2226     bs->print = 1;
2227
2228     sprintf (message, message1, b->number);
2229     if (b->type == bp_watchpoint ||
2230         b->type == bp_hardware_watchpoint)
2231       {
2232         switch (catch_errors (watchpoint_check, bs, message, 
2233                               RETURN_MASK_ALL))
2234           {
2235           case WP_DELETED:
2236             /* We've already printed what needs to be printed.  */
2237             bs->print_it = print_it_done;
2238             /* Stop.  */
2239             break;
2240           case WP_VALUE_CHANGED:
2241             /* Stop.  */
2242             ++(b->hit_count);
2243             break;
2244           case WP_VALUE_NOT_CHANGED:
2245             /* Don't stop.  */
2246             bs->print_it = print_it_noop;
2247             bs->stop = 0;
2248             /* Don't consider this a hit.  */
2249             --(b->hit_count);
2250             continue;
2251           default:
2252             /* Can't happen.  */
2253             /* FALLTHROUGH */
2254           case 0:
2255             /* Error from catch_errors.  */
2256             printf_filtered ("Watchpoint %d deleted.\n", b->number);
2257             if (b->related_breakpoint)
2258               b->related_breakpoint->disposition = del_at_next_stop;
2259             b->disposition = del_at_next_stop;
2260             /* We've already printed what needs to be printed.  */
2261             bs->print_it = print_it_done;
2262
2263             /* Stop.  */
2264             break;
2265           }
2266       }
2267     else if (b->type == bp_read_watchpoint || 
2268              b->type == bp_access_watchpoint)
2269       {
2270         CORE_ADDR addr;
2271         value_ptr v;
2272         int found = 0;
2273
2274         addr = target_stopped_data_address ();
2275         if (addr == 0)
2276           continue;
2277         for (v = b->val_chain; v; v = v->next)
2278           {
2279             if (v->lval == lval_memory)
2280               {
2281                 CORE_ADDR vaddr;
2282
2283                 vaddr = VALUE_ADDRESS (v) + VALUE_OFFSET (v);
2284                 /* Exact match not required.  Within range is sufficient.  
2285                  */
2286                 if (addr >= vaddr &&
2287                     addr < vaddr + TYPE_LENGTH (VALUE_TYPE (v)))
2288                   found = 1;
2289               }
2290           }
2291         if (found)
2292           switch (catch_errors (watchpoint_check, bs, message,
2293                                 RETURN_MASK_ALL))
2294             {
2295             case WP_DELETED:
2296               /* We've already printed what needs to be printed.  */
2297               bs->print_it = print_it_done;
2298               /* Stop.  */
2299               break;
2300             case WP_VALUE_CHANGED:
2301             case WP_VALUE_NOT_CHANGED:
2302               /* Stop.  */
2303               ++(b->hit_count);
2304               break;
2305             default:
2306               /* Can't happen.  */
2307             case 0:
2308               /* Error from catch_errors.  */
2309               printf_filtered ("Watchpoint %d deleted.\n", b->number);
2310               if (b->related_breakpoint)
2311                 b->related_breakpoint->disposition = del_at_next_stop;
2312               b->disposition = del_at_next_stop;
2313               /* We've already printed what needs to be printed.  */
2314               bs->print_it = print_it_done;
2315               break;
2316             }
2317         else    /* found == 0 */
2318           {
2319             /* This is a case where some watchpoint(s) triggered,
2320                but not at the address of this watchpoint (FOUND
2321                was left zero).  So don't print anything for this
2322                watchpoint.  */
2323             bs->print_it = print_it_noop;
2324             bs->stop = 0;
2325             continue;
2326           }
2327       }
2328     else
2329       {
2330         /* By definition, an encountered breakpoint is a triggered
2331            breakpoint. */
2332         ++(b->hit_count);
2333
2334         real_breakpoint = 1;
2335       }
2336
2337     if (b->frame && b->frame != (get_current_frame ())->frame &&
2338         (b->type == bp_step_resume &&
2339          (INNER_THAN (get_current_frame ()->frame, b->frame))))
2340       bs->stop = 0;
2341     else
2342       {
2343         int value_is_zero = 0;
2344
2345         if (b->cond)
2346           {
2347             /* Need to select the frame, with all that implies
2348                so that the conditions will have the right context.  */
2349             select_frame (get_current_frame (), 0);
2350             value_is_zero
2351               = catch_errors (breakpoint_cond_eval, (b->cond),
2352                               "Error in testing breakpoint condition:\n",
2353                               RETURN_MASK_ALL);
2354             /* FIXME-someday, should give breakpoint # */
2355             free_all_values ();
2356           }
2357         if (b->cond && value_is_zero)
2358           {
2359             bs->stop = 0;
2360             /* Don't consider this a hit.  */
2361             --(b->hit_count);
2362           }
2363         else if (b->ignore_count > 0)
2364           {
2365             b->ignore_count--;
2366             bs->stop = 0;
2367           }
2368         else
2369           {
2370             /* We will stop here */
2371             if (b->disposition == disable)
2372               b->enable = disabled;
2373             bs->commands = b->commands;
2374             if (b->silent)
2375               bs->print = 0;
2376             if (bs->commands &&
2377                 (STREQ ("silent", bs->commands->line) ||
2378                  (xdb_commands && STREQ ("Q", bs->commands->line))))
2379               {
2380                 bs->commands = bs->commands->next;
2381                 bs->print = 0;
2382               }
2383           }
2384       }
2385     /* Print nothing for this entry if we dont stop or if we dont print.  */
2386     if (bs->stop == 0 || bs->print == 0)
2387       bs->print_it = print_it_noop;
2388   }
2389
2390   bs->next = NULL;              /* Terminate the chain */
2391   bs = root_bs->next;           /* Re-grab the head of the chain */
2392
2393   if (real_breakpoint && bs)
2394     {
2395       if (bs->breakpoint_at->type == bp_hardware_breakpoint)
2396         {
2397           if (DECR_PC_AFTER_HW_BREAK != 0)
2398             {
2399               *pc = *pc - DECR_PC_AFTER_HW_BREAK;
2400               write_pc (*pc);
2401             }
2402         }
2403       else
2404         {
2405           if (DECR_PC_AFTER_BREAK != 0 || must_shift_inst_regs)
2406             {
2407               *pc = bp_addr;
2408 #if defined (SHIFT_INST_REGS)
2409               SHIFT_INST_REGS ();
2410 #else /* No SHIFT_INST_REGS.  */
2411               write_pc (bp_addr);
2412 #endif /* No SHIFT_INST_REGS.  */
2413             }
2414         }
2415     }
2416
2417   /* The value of a hardware watchpoint hasn't changed, but the
2418      intermediate memory locations we are watching may have.  */
2419   if (bs && !bs->stop &&
2420       (bs->breakpoint_at->type == bp_hardware_watchpoint ||
2421        bs->breakpoint_at->type == bp_read_watchpoint ||
2422        bs->breakpoint_at->type == bp_access_watchpoint))
2423     {
2424       remove_breakpoints ();
2425       insert_breakpoints ();
2426     }
2427   return bs;
2428 }
2429 \f
2430 /* Tell what to do about this bpstat.  */
2431 struct bpstat_what
2432 bpstat_what (bs)
2433      bpstat bs;
2434 {
2435   /* Classify each bpstat as one of the following.  */
2436   enum class
2437     {
2438       /* This bpstat element has no effect on the main_action.  */
2439       no_effect = 0,
2440
2441       /* There was a watchpoint, stop but don't print.  */
2442       wp_silent,
2443
2444       /* There was a watchpoint, stop and print.  */
2445       wp_noisy,
2446
2447       /* There was a breakpoint but we're not stopping.  */
2448       bp_nostop,
2449
2450       /* There was a breakpoint, stop but don't print.  */
2451       bp_silent,
2452
2453       /* There was a breakpoint, stop and print.  */
2454       bp_noisy,
2455
2456       /* We hit the longjmp breakpoint.  */
2457       long_jump,
2458
2459       /* We hit the longjmp_resume breakpoint.  */
2460       long_resume,
2461
2462       /* We hit the step_resume breakpoint.  */
2463       step_resume,
2464
2465       /* We hit the through_sigtramp breakpoint.  */
2466       through_sig,
2467
2468       /* We hit the shared library event breakpoint.  */
2469       shlib_event,
2470
2471       /* We caught a shared library event.  */
2472       catch_shlib_event,
2473
2474       /* This is just used to count how many enums there are.  */
2475       class_last
2476     };
2477
2478   /* Here is the table which drives this routine.  So that we can
2479      format it pretty, we define some abbreviations for the
2480      enum bpstat_what codes.  */
2481 #define kc BPSTAT_WHAT_KEEP_CHECKING
2482 #define ss BPSTAT_WHAT_STOP_SILENT
2483 #define sn BPSTAT_WHAT_STOP_NOISY
2484 #define sgl BPSTAT_WHAT_SINGLE
2485 #define slr BPSTAT_WHAT_SET_LONGJMP_RESUME
2486 #define clr BPSTAT_WHAT_CLEAR_LONGJMP_RESUME
2487 #define clrs BPSTAT_WHAT_CLEAR_LONGJMP_RESUME_SINGLE
2488 #define sr BPSTAT_WHAT_STEP_RESUME
2489 #define ts BPSTAT_WHAT_THROUGH_SIGTRAMP
2490 #define shl BPSTAT_WHAT_CHECK_SHLIBS
2491 #define shlr BPSTAT_WHAT_CHECK_SHLIBS_RESUME_FROM_HOOK
2492
2493 /* "Can't happen."  Might want to print an error message.
2494    abort() is not out of the question, but chances are GDB is just
2495    a bit confused, not unusable.  */
2496 #define err BPSTAT_WHAT_STOP_NOISY
2497
2498   /* Given an old action and a class, come up with a new action.  */
2499   /* One interesting property of this table is that wp_silent is the same
2500      as bp_silent and wp_noisy is the same as bp_noisy.  That is because
2501      after stopping, the check for whether to step over a breakpoint
2502      (BPSTAT_WHAT_SINGLE type stuff) is handled in proceed() without
2503      reference to how we stopped.  We retain separate wp_silent and
2504      bp_silent codes in case we want to change that someday. 
2505
2506      Another possibly interesting property of this table is that
2507      there's a partial ordering, priority-like, of the actions.  Once
2508      you've decided that some action is appropriate, you'll never go
2509      back and decide something of a lower priority is better.  The
2510      ordering is:
2511
2512      kc   < clr sgl shl slr sn sr ss ts
2513      sgl  < clrs shl shlr slr sn sr ss ts
2514      slr  < err shl shlr sn sr ss ts
2515      clr  < clrs err shl shlr sn sr ss ts
2516      clrs < err shl shlr sn sr ss ts
2517      ss   < shl shlr sn sr ts
2518      sn   < shl shlr sr ts
2519      sr   < shl shlr ts
2520      shl  < shlr
2521      ts   < 
2522      shlr <
2523
2524      What I think this means is that we don't need a damned table
2525      here.  If you just put the rows and columns in the right order,
2526      it'd look awfully regular.  We could simply walk the bpstat list
2527      and choose the highest priority action we find, with a little
2528      logic to handle the 'err' cases, and the CLEAR_LONGJMP_RESUME/
2529      CLEAR_LONGJMP_RESUME_SINGLE distinction (which breakpoint.h says
2530      is messy anyway).  */
2531
2532   /* step_resume entries: a step resume breakpoint overrides another
2533      breakpoint of signal handling (see comment in wait_for_inferior
2534      at first IN_SIGTRAMP where we set the step_resume breakpoint).  */
2535   /* We handle the through_sigtramp_breakpoint the same way; having both
2536      one of those and a step_resume_breakpoint is probably very rare (?).  */
2537
2538   static const enum bpstat_what_main_action
2539     table[(int) class_last][(int) BPSTAT_WHAT_LAST] =
2540   {
2541   /*                              old action */
2542   /*       kc    ss    sn    sgl    slr   clr    clrs   sr    ts   shl   shlr
2543    */
2544 /*no_effect */
2545     {kc, ss, sn, sgl, slr, clr, clrs, sr, ts, shl, shlr},
2546 /*wp_silent */
2547     {ss, ss, sn, ss, ss, ss, ss, sr, ts, shl, shlr},
2548 /*wp_noisy */
2549     {sn, sn, sn, sn, sn, sn, sn, sr, ts, shl, shlr},
2550 /*bp_nostop */
2551     {sgl, ss, sn, sgl, slr, clrs, clrs, sr, ts, shl, shlr},
2552 /*bp_silent */
2553     {ss, ss, sn, ss, ss, ss, ss, sr, ts, shl, shlr},
2554 /*bp_noisy */
2555     {sn, sn, sn, sn, sn, sn, sn, sr, ts, shl, shlr},
2556 /*long_jump */
2557     {slr, ss, sn, slr, err, err, err, sr, ts, shl, shlr},
2558 /*long_resume */
2559     {clr, ss, sn, clrs, err, err, err, sr, ts, shl, shlr},
2560 /*step_resume */
2561     {sr, sr, sr, sr, sr, sr, sr, sr, ts, shl, shlr},
2562 /*through_sig */
2563     {ts, ts, ts, ts, ts, ts, ts, ts, ts, shl, shlr},
2564 /*shlib */
2565     {shl, shl, shl, shl, shl, shl, shl, shl, ts, shl, shlr},
2566 /*catch_shlib */
2567     {shlr, shlr, shlr, shlr, shlr, shlr, shlr, shlr, ts, shlr, shlr}
2568   };
2569
2570 #undef kc
2571 #undef ss
2572 #undef sn
2573 #undef sgl
2574 #undef slr
2575 #undef clr
2576 #undef clrs
2577 #undef err
2578 #undef sr
2579 #undef ts
2580 #undef shl
2581 #undef shlr
2582   enum bpstat_what_main_action current_action = BPSTAT_WHAT_KEEP_CHECKING;
2583   struct bpstat_what retval;
2584
2585   retval.call_dummy = 0;
2586   for (; bs != NULL; bs = bs->next)
2587     {
2588       enum class bs_class = no_effect;
2589       if (bs->breakpoint_at == NULL)
2590         /* I suspect this can happen if it was a momentary breakpoint
2591            which has since been deleted.  */
2592         continue;
2593       switch (bs->breakpoint_at->type)
2594         {
2595         case bp_none:
2596           continue;
2597
2598         case bp_breakpoint:
2599         case bp_hardware_breakpoint:
2600         case bp_until:
2601         case bp_finish:
2602           if (bs->stop)
2603             {
2604               if (bs->print)
2605                 bs_class = bp_noisy;
2606               else
2607                 bs_class = bp_silent;
2608             }
2609           else
2610             bs_class = bp_nostop;
2611           break;
2612         case bp_watchpoint:
2613         case bp_hardware_watchpoint:
2614         case bp_read_watchpoint:
2615         case bp_access_watchpoint:
2616           if (bs->stop)
2617             {
2618               if (bs->print)
2619                 bs_class = wp_noisy;
2620               else
2621                 bs_class = wp_silent;
2622             }
2623           else
2624             /* There was a watchpoint, but we're not stopping. 
2625                This requires no further action.  */
2626             bs_class = no_effect;
2627           break;
2628         case bp_longjmp:
2629           bs_class = long_jump;
2630           break;
2631         case bp_longjmp_resume:
2632           bs_class = long_resume;
2633           break;
2634         case bp_step_resume:
2635           if (bs->stop)
2636             {
2637               bs_class = step_resume;
2638             }
2639           else
2640             /* It is for the wrong frame.  */
2641             bs_class = bp_nostop;
2642           break;
2643         case bp_through_sigtramp:
2644           bs_class = through_sig;
2645           break;
2646         case bp_watchpoint_scope:
2647           bs_class = bp_nostop;
2648           break;
2649         case bp_shlib_event:
2650           bs_class = shlib_event;
2651           break;
2652         case bp_catch_load:
2653         case bp_catch_unload:
2654           /* Only if this catchpoint triggered should we cause the
2655              step-out-of-dld behaviour.  Otherwise, we ignore this
2656              catchpoint.  */
2657           if (bs->stop)
2658             bs_class = catch_shlib_event;
2659           else
2660             bs_class = no_effect;
2661           break;
2662         case bp_catch_fork:
2663         case bp_catch_vfork:
2664         case bp_catch_exec:
2665           if (bs->stop)
2666             {
2667               if (bs->print)
2668                 bs_class = bp_noisy;
2669               else
2670                 bs_class = bp_silent;
2671             }
2672           else
2673             /* There was a catchpoint, but we're not stopping.  
2674                This requires no further action.  */
2675             bs_class = no_effect;
2676           break;
2677         case bp_catch_catch:
2678           if (!bs->stop || CURRENT_EXCEPTION_KIND != EX_EVENT_CATCH)
2679             bs_class = bp_nostop;
2680           else if (bs->stop)
2681             bs_class = bs->print ? bp_noisy : bp_silent;
2682           break;
2683         case bp_catch_throw:
2684           if (!bs->stop || CURRENT_EXCEPTION_KIND != EX_EVENT_THROW)
2685             bs_class = bp_nostop;
2686           else if (bs->stop)
2687             bs_class = bs->print ? bp_noisy : bp_silent;
2688           break;
2689         case bp_call_dummy:
2690           /* Make sure the action is stop (silent or noisy),
2691              so infrun.c pops the dummy frame.  */
2692           bs_class = bp_silent;
2693           retval.call_dummy = 1;
2694           break;
2695         }
2696       current_action = table[(int) bs_class][(int) current_action];
2697     }
2698   retval.main_action = current_action;
2699   return retval;
2700 }
2701
2702 /* Nonzero if we should step constantly (e.g. watchpoints on machines
2703    without hardware support).  This isn't related to a specific bpstat,
2704    just to things like whether watchpoints are set.  */
2705
2706 int
2707 bpstat_should_step ()
2708 {
2709   struct breakpoint *b;
2710   ALL_BREAKPOINTS (b)
2711     if (b->enable == enabled && b->type == bp_watchpoint)
2712     return 1;
2713   return 0;
2714 }
2715
2716 /* Nonzero if there are enabled hardware watchpoints. */
2717 int
2718 bpstat_have_active_hw_watchpoints ()
2719 {
2720   struct breakpoint *b;
2721   ALL_BREAKPOINTS (b)
2722     if ((b->enable == enabled) &&
2723         (b->inserted) &&
2724         ((b->type == bp_hardware_watchpoint) ||
2725          (b->type == bp_read_watchpoint) ||
2726          (b->type == bp_access_watchpoint)))
2727     return 1;
2728   return 0;
2729 }
2730 \f
2731
2732 /* Given a bpstat that records zero or more triggered eventpoints, this
2733    function returns another bpstat which contains only the catchpoints
2734    on that first list, if any. */
2735 void
2736 bpstat_get_triggered_catchpoints (ep_list, cp_list)
2737      bpstat ep_list;
2738      bpstat *cp_list;
2739 {
2740   struct bpstats root_bs[1];
2741   bpstat bs = root_bs;
2742   struct breakpoint *ep;
2743   char *dll_pathname;
2744
2745   bpstat_clear (cp_list);
2746   root_bs->next = NULL;
2747
2748   for (; ep_list != NULL; ep_list = ep_list->next)
2749     {
2750       /* Is this eventpoint a catchpoint?  If not, ignore it. */
2751       ep = ep_list->breakpoint_at;
2752       if (ep == NULL)
2753         break;
2754       if ((ep->type != bp_catch_load) &&
2755           (ep->type != bp_catch_unload) &&
2756           (ep->type != bp_catch_catch) &&
2757           (ep->type != bp_catch_throw))         
2758         /* pai: (temp) ADD fork/vfork here!!  */
2759         continue;
2760
2761       /* Yes; add it to the list. */
2762       bs = bpstat_alloc (ep, bs);
2763       *bs = *ep_list;
2764       bs->next = NULL;
2765       bs = root_bs->next;
2766
2767 #if defined(SOLIB_ADD)
2768       /* Also, for each triggered catchpoint, tag it with the name of
2769          the library that caused this trigger.  (We copy the name now,
2770          because it's only guaranteed to be available NOW, when the
2771          catchpoint triggers.  Clients who may wish to know the name
2772          later must get it from the catchpoint itself.) */
2773       if (ep->triggered_dll_pathname != NULL)
2774         free (ep->triggered_dll_pathname);
2775       if (ep->type == bp_catch_load)
2776         dll_pathname = SOLIB_LOADED_LIBRARY_PATHNAME (inferior_pid);
2777       else
2778         dll_pathname = SOLIB_UNLOADED_LIBRARY_PATHNAME (inferior_pid);
2779 #else
2780       dll_pathname = NULL;
2781 #endif
2782       if (dll_pathname)
2783         {
2784           ep->triggered_dll_pathname = (char *) 
2785             xmalloc (strlen (dll_pathname) + 1);
2786           strcpy (ep->triggered_dll_pathname, dll_pathname);
2787         }
2788       else
2789         ep->triggered_dll_pathname = NULL;
2790     }
2791
2792   *cp_list = bs;
2793 }
2794
2795 /* Print information on breakpoint number BNUM, or -1 if all.
2796    If WATCHPOINTS is zero, process only breakpoints; if WATCHPOINTS
2797    is nonzero, process only watchpoints.  */
2798
2799 typedef struct
2800 {
2801   enum bptype type;
2802   char *description;
2803 }
2804 ep_type_description_t;
2805
2806 static void
2807 breakpoint_1 (bnum, allflag)
2808      int bnum;
2809      int allflag;
2810 {
2811   register struct breakpoint *b;
2812   register struct command_line *l;
2813   register struct symbol *sym;
2814   CORE_ADDR last_addr = (CORE_ADDR) - 1;
2815   int found_a_breakpoint = 0;
2816   static ep_type_description_t bptypes[] =
2817   {
2818     {bp_none, "?deleted?"},
2819     {bp_breakpoint, "breakpoint"},
2820     {bp_hardware_breakpoint, "hw breakpoint"},
2821     {bp_until, "until"},
2822     {bp_finish, "finish"},
2823     {bp_watchpoint, "watchpoint"},
2824     {bp_hardware_watchpoint, "hw watchpoint"},
2825     {bp_read_watchpoint, "read watchpoint"},
2826     {bp_access_watchpoint, "acc watchpoint"},
2827     {bp_longjmp, "longjmp"},
2828     {bp_longjmp_resume, "longjmp resume"},
2829     {bp_step_resume, "step resume"},
2830     {bp_through_sigtramp, "sigtramp"},
2831     {bp_watchpoint_scope, "watchpoint scope"},
2832     {bp_call_dummy, "call dummy"},
2833     {bp_shlib_event, "shlib events"},
2834     {bp_catch_load, "catch load"},
2835     {bp_catch_unload, "catch unload"},
2836     {bp_catch_fork, "catch fork"},
2837     {bp_catch_vfork, "catch vfork"},
2838     {bp_catch_exec, "catch exec"},
2839     {bp_catch_catch, "catch catch"},
2840     {bp_catch_throw, "catch throw"}
2841   };
2842
2843   static char *bpdisps[] =
2844   {"del", "dstp", "dis", "keep"};
2845   static char bpenables[] = "nyn";
2846   char wrap_indent[80];
2847
2848
2849
2850   ALL_BREAKPOINTS (b)
2851     if (bnum == -1
2852         || bnum == b->number)
2853     {
2854 /*  We only print out user settable breakpoints unless the allflag is set. */
2855       if (!allflag
2856           && b->type != bp_breakpoint
2857           && b->type != bp_catch_load
2858           && b->type != bp_catch_unload
2859           && b->type != bp_catch_fork
2860           && b->type != bp_catch_vfork
2861           && b->type != bp_catch_exec
2862           && b->type != bp_catch_catch
2863           && b->type != bp_catch_throw
2864           && b->type != bp_hardware_breakpoint
2865           && b->type != bp_watchpoint
2866           && b->type != bp_read_watchpoint
2867           && b->type != bp_access_watchpoint
2868           && b->type != bp_hardware_watchpoint)
2869         continue;
2870
2871       if (!found_a_breakpoint++)
2872         {
2873           annotate_breakpoints_headers ();
2874
2875           annotate_field (0);
2876           printf_filtered ("Num ");
2877           annotate_field (1);
2878           printf_filtered ("Type           ");
2879           annotate_field (2);
2880           printf_filtered ("Disp ");
2881           annotate_field (3);
2882           printf_filtered ("Enb ");
2883           if (addressprint)
2884             {
2885               annotate_field (4);
2886               printf_filtered ("Address    ");
2887             }
2888           annotate_field (5);
2889           printf_filtered ("What\n");
2890
2891           annotate_breakpoints_table ();
2892         }
2893
2894       annotate_record ();
2895       annotate_field (0);
2896       printf_filtered ("%-3d ", b->number);
2897       annotate_field (1);
2898       if ((int) b->type > (sizeof (bptypes) / sizeof (bptypes[0])))
2899         error ("bptypes table does not describe type #%d.", (int) b->type);
2900       if ((int) b->type != bptypes[(int) b->type].type)
2901         error ("bptypes table does not describe type #%d?", (int) b->type);
2902       printf_filtered ("%-14s ", bptypes[(int) b->type].description);
2903       annotate_field (2);
2904       printf_filtered ("%-4s ", bpdisps[(int) b->disposition]);
2905       annotate_field (3);
2906       printf_filtered ("%-3c ", bpenables[(int) b->enable]);
2907
2908       strcpy (wrap_indent, "                           ");
2909       if (addressprint)
2910         strcat (wrap_indent, "           ");
2911       switch (b->type)
2912         {
2913         case bp_watchpoint:
2914         case bp_hardware_watchpoint:
2915         case bp_read_watchpoint:
2916         case bp_access_watchpoint:
2917           /* Field 4, the address, is omitted (which makes the columns
2918              not line up too nicely with the headers, but the effect
2919              is relatively readable).  */
2920           annotate_field (5);
2921           print_expression (b->exp, gdb_stdout);
2922           break;
2923
2924         case bp_catch_load:
2925         case bp_catch_unload:
2926           /* Field 4, the address, is omitted (which makes the columns
2927              not line up too nicely with the headers, but the effect
2928              is relatively readable).  */
2929           annotate_field (5);
2930           if (b->dll_pathname == NULL)
2931             printf_filtered ("<any library> ");
2932           else
2933             printf_filtered ("library \"%s\" ", b->dll_pathname);
2934           break;
2935
2936         case bp_catch_fork:
2937         case bp_catch_vfork:
2938           /* Field 4, the address, is omitted (which makes the columns
2939              not line up too nicely with the headers, but the effect
2940              is relatively readable).  */
2941           annotate_field (5);
2942           if (b->forked_inferior_pid != 0)
2943             printf_filtered ("process %d ", b->forked_inferior_pid);
2944           break;
2945
2946         case bp_catch_exec:
2947           /* Field 4, the address, is omitted (which makes the columns
2948              not line up too nicely with the headers, but the effect
2949              is relatively readable).  */
2950           annotate_field (5);
2951           if (b->exec_pathname != NULL)
2952             printf_filtered ("program \"%s\" ", b->exec_pathname);
2953           break;
2954         case bp_catch_catch:
2955           /* Field 4, the address, is omitted (which makes the columns
2956              not line up too nicely with the headers, but the effect
2957              is relatively readable).  */
2958           annotate_field (5);
2959           printf_filtered ("exception catch ");
2960           break;
2961         case bp_catch_throw:
2962           /* Field 4, the address, is omitted (which makes the columns
2963              not line up too nicely with the headers, but the effect
2964              is relatively readable).  */
2965           annotate_field (5);
2966           printf_filtered ("exception throw ");
2967           break;
2968
2969         case bp_breakpoint:
2970         case bp_hardware_breakpoint:
2971         case bp_until:
2972         case bp_finish:
2973         case bp_longjmp:
2974         case bp_longjmp_resume:
2975         case bp_step_resume:
2976         case bp_through_sigtramp:
2977         case bp_watchpoint_scope:
2978         case bp_call_dummy:
2979         case bp_shlib_event:
2980           if (addressprint)
2981             {
2982               annotate_field (4);
2983               /* FIXME-32x64: need a print_address_numeric with
2984                  field width */
2985               printf_filtered
2986                 ("%s ",
2987                  local_hex_string_custom
2988                  ((unsigned long) b->address, "08l"));
2989             }
2990
2991           annotate_field (5);
2992
2993           last_addr = b->address;
2994           if (b->source_file)
2995             {
2996               sym = find_pc_sect_function (b->address, b->section);
2997               if (sym)
2998                 {
2999                   fputs_filtered ("in ", gdb_stdout);
3000                   fputs_filtered (SYMBOL_SOURCE_NAME (sym), gdb_stdout);
3001                   wrap_here (wrap_indent);
3002                   fputs_filtered (" at ", gdb_stdout);
3003                 }
3004               fputs_filtered (b->source_file, gdb_stdout);
3005               printf_filtered (":%d", b->line_number);
3006             }
3007           else
3008             print_address_symbolic (b->address, gdb_stdout, demangle, " ");
3009           break;
3010         }
3011
3012       if (b->thread != -1)
3013         printf_filtered (" thread %d", b->thread);
3014
3015       printf_filtered ("\n");
3016
3017       if (b->frame)
3018         {
3019           annotate_field (6);
3020
3021           printf_filtered ("\tstop only in stack frame at ");
3022           print_address_numeric (b->frame, 1, gdb_stdout);
3023           printf_filtered ("\n");
3024         }
3025
3026       if (b->cond)
3027         {
3028           annotate_field (7);
3029
3030           printf_filtered ("\tstop only if ");
3031           print_expression (b->cond, gdb_stdout);
3032           printf_filtered ("\n");
3033         }
3034
3035       if (b->thread != -1)
3036         {
3037           /* FIXME should make an annotation for this */
3038           printf_filtered ("\tstop only in thread %d\n", b->thread);
3039         }
3040
3041       if (show_breakpoint_hit_counts && b->hit_count)
3042         {
3043           /* FIXME should make an annotation for this */
3044           if (ep_is_catchpoint (b))
3045             printf_filtered ("\tcatchpoint");
3046           else
3047             printf_filtered ("\tbreakpoint");
3048           printf_filtered (" already hit %d time%s\n",
3049                            b->hit_count, (b->hit_count == 1 ? "" : "s"));
3050         }
3051
3052       if (b->ignore_count)
3053         {
3054           annotate_field (8);
3055
3056           printf_filtered ("\tignore next %d hits\n", b->ignore_count);
3057         }
3058
3059       if ((l = b->commands))
3060         {
3061           annotate_field (9);
3062
3063           while (l)
3064             {
3065               print_command_line (l, 4, gdb_stdout);
3066               l = l->next;
3067             }
3068         }
3069     }
3070
3071   if (!found_a_breakpoint)
3072     {
3073       if (bnum == -1)
3074         printf_filtered ("No breakpoints or watchpoints.\n");
3075       else
3076         printf_filtered ("No breakpoint or watchpoint number %d.\n", bnum);
3077     }
3078   else
3079     /* Compare against (CORE_ADDR)-1 in case some compiler decides
3080        that a comparison of an unsigned with -1 is always false.  */
3081   if (last_addr != (CORE_ADDR) - 1)
3082     set_next_address (last_addr);
3083
3084   annotate_breakpoints_table_end ();
3085 }
3086
3087 /* ARGSUSED */
3088 static void
3089 breakpoints_info (bnum_exp, from_tty)
3090      char *bnum_exp;
3091      int from_tty;
3092 {
3093   int bnum = -1;
3094
3095   if (bnum_exp)
3096     bnum = parse_and_eval_address (bnum_exp);
3097
3098   breakpoint_1 (bnum, 0);
3099 }
3100
3101 /* ARGSUSED */
3102 static void
3103 maintenance_info_breakpoints (bnum_exp, from_tty)
3104      char *bnum_exp;
3105      int from_tty;
3106 {
3107   int bnum = -1;
3108
3109   if (bnum_exp)
3110     bnum = parse_and_eval_address (bnum_exp);
3111
3112   breakpoint_1 (bnum, 1);
3113 }
3114
3115 /* Print a message describing any breakpoints set at PC.  */
3116
3117 static void
3118 describe_other_breakpoints (pc, section)
3119      CORE_ADDR pc;
3120      asection *section;
3121 {
3122   register int others = 0;
3123   register struct breakpoint *b;
3124
3125   ALL_BREAKPOINTS (b)
3126     if (b->address == pc)
3127     if (overlay_debugging == 0 ||
3128         b->section == section)
3129       others++;
3130   if (others > 0)
3131     {
3132       printf_filtered ("Note: breakpoint%s ", (others > 1) ? "s" : "");
3133       ALL_BREAKPOINTS (b)
3134         if (b->address == pc)
3135         if (overlay_debugging == 0 ||
3136             b->section == section)
3137           {
3138             others--;
3139             printf_filtered
3140               ("%d%s%s ",
3141                b->number,
3142                ((b->enable == disabled || 
3143                  b->enable == shlib_disabled || 
3144                  b->enable == call_disabled)
3145                 ? " (disabled)" : ""),
3146                (others > 1) ? "," : ((others == 1) ? " and" : ""));
3147           }
3148       printf_filtered ("also set at pc ");
3149       print_address_numeric (pc, 1, gdb_stdout);
3150       printf_filtered (".\n");
3151     }
3152 }
3153 \f
3154 /* Set the default place to put a breakpoint
3155    for the `break' command with no arguments.  */
3156
3157 void
3158 set_default_breakpoint (valid, addr, symtab, line)
3159      int valid;
3160      CORE_ADDR addr;
3161      struct symtab *symtab;
3162      int line;
3163 {
3164   default_breakpoint_valid = valid;
3165   default_breakpoint_address = addr;
3166   default_breakpoint_symtab = symtab;
3167   default_breakpoint_line = line;
3168 }
3169
3170 /* Rescan breakpoints at address ADDRESS,
3171    marking the first one as "first" and any others as "duplicates".
3172    This is so that the bpt instruction is only inserted once.  */
3173
3174 static void
3175 check_duplicates (address, section)
3176      CORE_ADDR address;
3177      asection *section;
3178 {
3179   register struct breakpoint *b;
3180   register int count = 0;
3181
3182   if (address == 0)             /* Watchpoints are uninteresting */
3183     return;
3184
3185   ALL_BREAKPOINTS (b)
3186     if (b->enable != disabled
3187         && b->enable != shlib_disabled
3188         && b->enable != call_disabled
3189         && b->address == address
3190         && (overlay_debugging == 0 || b->section == section))
3191     {
3192       count++;
3193       b->duplicate = count > 1;
3194     }
3195 }
3196
3197 /* Low level routine to set a breakpoint.
3198    Takes as args the three things that every breakpoint must have.
3199    Returns the breakpoint object so caller can set other things.
3200    Does not set the breakpoint number!
3201    Does not print anything.
3202
3203    ==> This routine should not be called if there is a chance of later
3204    error(); otherwise it leaves a bogus breakpoint on the chain.  Validate
3205    your arguments BEFORE calling this routine!  */
3206
3207 struct breakpoint *
3208 set_raw_breakpoint (sal)
3209      struct symtab_and_line sal;
3210 {
3211   register struct breakpoint *b, *b1;
3212
3213   b = (struct breakpoint *) xmalloc (sizeof (struct breakpoint));
3214   memset (b, 0, sizeof (*b));
3215   b->address = sal.pc;
3216   if (sal.symtab == NULL)
3217     b->source_file = NULL;
3218   else
3219     b->source_file = savestring (sal.symtab->filename,
3220                                  strlen (sal.symtab->filename));
3221   b->section = sal.section;
3222   b->language = current_language->la_language;
3223   b->input_radix = input_radix;
3224   b->thread = -1;
3225   b->line_number = sal.line;
3226   b->enable = enabled;
3227   b->next = 0;
3228   b->silent = 0;
3229   b->ignore_count = 0;
3230   b->commands = NULL;
3231   b->frame = 0;
3232   b->dll_pathname = NULL;
3233   b->triggered_dll_pathname = NULL;
3234   b->forked_inferior_pid = 0;
3235   b->exec_pathname = NULL;
3236
3237   /* Add this breakpoint to the end of the chain
3238      so that a list of breakpoints will come out in order
3239      of increasing numbers.  */
3240
3241   b1 = breakpoint_chain;
3242   if (b1 == 0)
3243     breakpoint_chain = b;
3244   else
3245     {
3246       while (b1->next)
3247         b1 = b1->next;
3248       b1->next = b;
3249     }
3250
3251   check_duplicates (sal.pc, sal.section);
3252   breakpoints_changed ();
3253
3254   return b;
3255 }
3256
3257 #ifdef GET_LONGJMP_TARGET
3258
3259 static void
3260 create_longjmp_breakpoint (func_name)
3261      char *func_name;
3262 {
3263   struct symtab_and_line sal;
3264   struct breakpoint *b;
3265
3266   INIT_SAL (&sal);              /* initialize to zeroes */
3267   if (func_name != NULL)
3268     {
3269       struct minimal_symbol *m;
3270
3271       m = lookup_minimal_symbol_text (func_name, NULL, 
3272                                       (struct objfile *) NULL);
3273       if (m)
3274         sal.pc = SYMBOL_VALUE_ADDRESS (m);
3275       else
3276         return;
3277     }
3278   sal.section = find_pc_overlay (sal.pc);
3279   b = set_raw_breakpoint (sal);
3280   if (!b)
3281     return;
3282
3283   b->type = func_name != NULL ? bp_longjmp : bp_longjmp_resume;
3284   b->disposition = donttouch;
3285   b->enable = disabled;
3286   b->silent = 1;
3287   if (func_name)
3288     b->addr_string = strsave (func_name);
3289   b->number = internal_breakpoint_number--;
3290 }
3291
3292 #endif /* #ifdef GET_LONGJMP_TARGET */
3293
3294 /* Call this routine when stepping and nexting to enable a breakpoint
3295    if we do a longjmp().  When we hit that breakpoint, call
3296    set_longjmp_resume_breakpoint() to figure out where we are going. */
3297
3298 void
3299 enable_longjmp_breakpoint ()
3300 {
3301   register struct breakpoint *b;
3302
3303   ALL_BREAKPOINTS (b)
3304     if (b->type == bp_longjmp)
3305     {
3306       b->enable = enabled;
3307       check_duplicates (b->address, b->section);
3308     }
3309 }
3310
3311 void
3312 disable_longjmp_breakpoint ()
3313 {
3314   register struct breakpoint *b;
3315
3316   ALL_BREAKPOINTS (b)
3317     if (b->type == bp_longjmp
3318         || b->type == bp_longjmp_resume)
3319     {
3320       b->enable = disabled;
3321       check_duplicates (b->address, b->section);
3322     }
3323 }
3324
3325 #ifdef SOLIB_ADD
3326 void
3327 remove_solib_event_breakpoints ()
3328 {
3329   register struct breakpoint *b, *temp;
3330
3331   ALL_BREAKPOINTS_SAFE (b, temp)
3332     if (b->type == bp_shlib_event)
3333     delete_breakpoint (b);
3334 }
3335
3336 void
3337 create_solib_event_breakpoint (address)
3338      CORE_ADDR address;
3339 {
3340   struct breakpoint *b;
3341   struct symtab_and_line sal;
3342
3343   INIT_SAL (&sal);              /* initialize to zeroes */
3344   sal.pc = address;
3345   sal.section = find_pc_overlay (sal.pc);
3346   b = set_raw_breakpoint (sal);
3347   b->number = internal_breakpoint_number--;
3348   b->disposition = donttouch;
3349   b->type = bp_shlib_event;
3350 }
3351
3352 /* Disable any breakpoints that are on code in shared libraries.  Only
3353    apply to enabled breakpoints, disabled ones can just stay disabled.  */
3354
3355 void
3356 disable_breakpoints_in_shlibs (silent)
3357      int silent;
3358 {
3359   struct breakpoint *b;
3360   int disabled_shlib_breaks = 0;
3361
3362   /* See also: insert_breakpoints, under DISABLE_UNSETTABLE_BREAK. */
3363   ALL_BREAKPOINTS (b)
3364   {
3365 #if defined (PC_SOLIB)
3366     if (((b->type == bp_breakpoint) ||
3367          (b->type == bp_hardware_breakpoint)) &&
3368         b->enable == enabled &&
3369         !b->duplicate &&
3370         PC_SOLIB (b->address))
3371       {
3372         b->enable = shlib_disabled;
3373         if (!silent)
3374           {
3375             if (!disabled_shlib_breaks)
3376               {
3377                 target_terminal_ours_for_output ();
3378                 warning ("Temporarily disabling shared library breakpoints:");
3379               }
3380             disabled_shlib_breaks = 1;
3381             warning ("breakpoint #%d ", b->number);
3382           }
3383       }
3384 #endif
3385   }
3386 }
3387
3388 /* Try to reenable any breakpoints in shared libraries.  */
3389 void
3390 re_enable_breakpoints_in_shlibs ()
3391 {
3392   struct breakpoint *b;
3393
3394   ALL_BREAKPOINTS (b)
3395     if (b->enable == shlib_disabled)
3396     {
3397       char buf[1];
3398
3399       /* Do not reenable the breakpoint if the shared library
3400          is still not mapped in.  */
3401       if (target_read_memory (b->address, buf, 1) == 0)
3402         b->enable = enabled;
3403     }
3404 }
3405
3406 #endif
3407
3408 static void
3409 solib_load_unload_1 (hookname, tempflag, dll_pathname, cond_string, bp_kind)
3410      char *hookname;
3411      int tempflag;
3412      char *dll_pathname;
3413      char *cond_string;
3414      enum bptype bp_kind;
3415 {
3416   struct breakpoint *b;
3417   struct symtabs_and_lines sals;
3418   struct cleanup *old_chain;
3419   struct cleanup *canonical_strings_chain = NULL;
3420   char *addr_start = hookname;
3421   char *addr_end = NULL;
3422   char **canonical = (char **) NULL;
3423   int thread = -1;              /* All threads. */
3424
3425   /* Set a breakpoint on the specified hook. */
3426   sals = decode_line_1 (&hookname, 1, (struct symtab *) NULL, 0, &canonical);
3427   addr_end = hookname;
3428
3429   if (sals.nelts == 0)
3430     {
3431       warning ("Unable to set a breakpoint on dynamic linker callback.");
3432       warning ("Suggest linking with /opt/langtools/lib/end.o.");
3433       warning ("GDB will be unable to track shl_load/shl_unload calls");
3434       return;
3435     }
3436   if (sals.nelts != 1)
3437     {
3438       warning ("Unable to set unique breakpoint on dynamic linker callback.");
3439       warning ("GDB will be unable to track shl_load/shl_unload calls");
3440       return;
3441     }
3442
3443   /* Make sure that all storage allocated in decode_line_1 gets freed
3444      in case the following errors out.  */
3445   old_chain = make_cleanup (free, sals.sals);
3446   if (canonical != (char **) NULL)
3447     {
3448       make_cleanup (free, canonical);
3449       canonical_strings_chain = make_cleanup (null_cleanup, 0);
3450       if (canonical[0] != NULL)
3451         make_cleanup (free, canonical[0]);
3452     }
3453
3454   resolve_sal_pc (&sals.sals[0]);
3455
3456   /* Remove the canonical strings from the cleanup, they are needed below.  */
3457   if (canonical != (char **) NULL)
3458     discard_cleanups (canonical_strings_chain);
3459
3460   b = set_raw_breakpoint (sals.sals[0]);
3461   set_breakpoint_count (breakpoint_count + 1);
3462   b->number = breakpoint_count;
3463   b->cond = NULL;
3464   b->cond_string = (cond_string == NULL) ? 
3465     NULL : savestring (cond_string, strlen (cond_string));
3466   b->thread = thread;
3467
3468   if (canonical != (char **) NULL && canonical[0] != NULL)
3469     b->addr_string = canonical[0];
3470   else if (addr_start)
3471     b->addr_string = savestring (addr_start, addr_end - addr_start);
3472
3473   b->enable = enabled;
3474   b->disposition = tempflag ? del : donttouch;
3475
3476   if (dll_pathname == NULL)
3477     b->dll_pathname = NULL;
3478   else
3479     {
3480       b->dll_pathname = (char *) xmalloc (strlen (dll_pathname) + 1);
3481       strcpy (b->dll_pathname, dll_pathname);
3482     }
3483   b->type = bp_kind;
3484
3485   mention (b);
3486   do_cleanups (old_chain);
3487 }
3488
3489 void
3490 create_solib_load_event_breakpoint (hookname, tempflag, 
3491                                     dll_pathname, cond_string)
3492      char *hookname;
3493      int tempflag;
3494      char *dll_pathname;
3495      char *cond_string;
3496 {
3497   solib_load_unload_1 (hookname, tempflag, dll_pathname, 
3498                        cond_string, bp_catch_load);
3499 }
3500
3501 void
3502 create_solib_unload_event_breakpoint (hookname, tempflag, 
3503                                       dll_pathname, cond_string)
3504      char *hookname;
3505      int tempflag;
3506      char *dll_pathname;
3507      char *cond_string;
3508 {
3509   solib_load_unload_1 (hookname,tempflag, dll_pathname, 
3510                        cond_string, bp_catch_unload);
3511 }
3512
3513 static void
3514 create_fork_vfork_event_catchpoint (tempflag, cond_string, bp_kind)
3515      int tempflag;
3516      char *cond_string;
3517      enum bptype bp_kind;
3518 {
3519   struct symtab_and_line sal;
3520   struct breakpoint *b;
3521   int thread = -1;              /* All threads. */
3522
3523   INIT_SAL (&sal);
3524   sal.pc = 0;
3525   sal.symtab = NULL;
3526   sal.line = 0;
3527
3528   b = set_raw_breakpoint (sal);
3529   set_breakpoint_count (breakpoint_count + 1);
3530   b->number = breakpoint_count;
3531   b->cond = NULL;
3532   b->cond_string = (cond_string == NULL) ? 
3533     NULL : savestring (cond_string, strlen (cond_string));
3534   b->thread = thread;
3535   b->addr_string = NULL;
3536   b->enable = enabled;
3537   b->disposition = tempflag ? del : donttouch;
3538   b->forked_inferior_pid = 0;
3539
3540   b->type = bp_kind;
3541
3542   mention (b);
3543 }
3544
3545 void
3546 create_fork_event_catchpoint (tempflag, cond_string)
3547      int tempflag;
3548      char *cond_string;
3549 {
3550   create_fork_vfork_event_catchpoint (tempflag, cond_string, bp_catch_fork);
3551 }
3552
3553 void
3554 create_vfork_event_catchpoint (tempflag, cond_string)
3555      int tempflag;
3556      char *cond_string;
3557 {
3558   create_fork_vfork_event_catchpoint (tempflag, cond_string, bp_catch_vfork);
3559 }
3560
3561 void
3562 create_exec_event_catchpoint (tempflag, cond_string)
3563      int tempflag;
3564      char *cond_string;
3565 {
3566   struct symtab_and_line sal;
3567   struct breakpoint *b;
3568   int thread = -1;              /* All threads. */
3569
3570   INIT_SAL (&sal);
3571   sal.pc = 0;
3572   sal.symtab = NULL;
3573   sal.line = 0;
3574
3575   b = set_raw_breakpoint (sal);
3576   set_breakpoint_count (breakpoint_count + 1);
3577   b->number = breakpoint_count;
3578   b->cond = NULL;
3579   b->cond_string = (cond_string == NULL) ?
3580     NULL : savestring (cond_string, strlen (cond_string));
3581   b->thread = thread;
3582   b->addr_string = NULL;
3583   b->enable = enabled;
3584   b->disposition = tempflag ? del : donttouch;
3585
3586   b->type = bp_catch_exec;
3587
3588   mention (b);
3589 }
3590
3591 static int
3592 hw_breakpoint_used_count ()
3593 {
3594   register struct breakpoint *b;
3595   int i = 0;
3596
3597   ALL_BREAKPOINTS (b)
3598   {
3599     if (b->type == bp_hardware_breakpoint && b->enable == enabled)
3600       i++;
3601   }
3602
3603   return i;
3604 }
3605
3606 static int
3607 hw_watchpoint_used_count (type, other_type_used)
3608      enum bptype type;
3609      int *other_type_used;
3610 {
3611   register struct breakpoint *b;
3612   int i = 0;
3613
3614   *other_type_used = 0;
3615   ALL_BREAKPOINTS (b)
3616   {
3617     if (b->enable == enabled)
3618       {
3619         if (b->type == type)
3620           i++;
3621         else if ((b->type == bp_hardware_watchpoint ||
3622                   b->type == bp_read_watchpoint ||
3623                   b->type == bp_access_watchpoint)
3624                  && b->enable == enabled)
3625           *other_type_used = 1;
3626       }
3627   }
3628   return i;
3629 }
3630
3631 /* Call this after hitting the longjmp() breakpoint.  Use this to set
3632    a new breakpoint at the target of the jmp_buf.
3633
3634    FIXME - This ought to be done by setting a temporary breakpoint
3635    that gets deleted automatically... */
3636
3637 void
3638 set_longjmp_resume_breakpoint (pc, frame)
3639      CORE_ADDR pc;
3640      struct frame_info *frame;
3641 {
3642   register struct breakpoint *b;
3643
3644   ALL_BREAKPOINTS (b)
3645     if (b->type == bp_longjmp_resume)
3646     {
3647       b->address = pc;
3648       b->enable = enabled;
3649       if (frame != NULL)
3650         b->frame = frame->frame;
3651       else
3652         b->frame = 0;
3653       check_duplicates (b->address, b->section);
3654       return;
3655     }
3656 }
3657
3658 void
3659 disable_watchpoints_before_interactive_call_start ()
3660 {
3661   struct breakpoint *b;
3662
3663   ALL_BREAKPOINTS (b)
3664   {
3665     if (((b->type == bp_watchpoint)
3666          || (b->type == bp_hardware_watchpoint)
3667          || (b->type == bp_read_watchpoint)
3668          || (b->type == bp_access_watchpoint)
3669          || ep_is_exception_catchpoint (b))
3670         && (b->enable == enabled))
3671       {
3672         b->enable = call_disabled;
3673         check_duplicates (b->address, b->section);
3674       }
3675   }
3676 }
3677
3678 void
3679 enable_watchpoints_after_interactive_call_stop ()
3680 {
3681   struct breakpoint *b;
3682
3683   ALL_BREAKPOINTS (b)
3684   {
3685     if (((b->type == bp_watchpoint)
3686          || (b->type == bp_hardware_watchpoint)
3687          || (b->type == bp_read_watchpoint)
3688          || (b->type == bp_access_watchpoint)
3689          || ep_is_exception_catchpoint (b))
3690         && (b->enable == call_disabled))
3691       {
3692         b->enable = enabled;
3693         check_duplicates (b->address, b->section);
3694       }
3695   }
3696 }
3697
3698
3699 /* Set a breakpoint that will evaporate an end of command
3700    at address specified by SAL.
3701    Restrict it to frame FRAME if FRAME is nonzero.  */
3702
3703 struct breakpoint *
3704 set_momentary_breakpoint (sal, frame, type)
3705      struct symtab_and_line sal;
3706      struct frame_info *frame;
3707      enum bptype type;
3708 {
3709   register struct breakpoint *b;
3710   b = set_raw_breakpoint (sal);
3711   b->type = type;
3712   b->enable = enabled;
3713   b->disposition = donttouch;
3714   b->frame = (frame ? frame->frame : 0);
3715
3716   /* If we're debugging a multi-threaded program, then we
3717      want momentary breakpoints to be active in only a 
3718      single thread of control.  */
3719   if (in_thread_list (inferior_pid))
3720     b->thread = pid_to_thread_id (inferior_pid);
3721
3722   return b;
3723 }
3724 \f
3725
3726 /* Tell the user we have just set a breakpoint B.  */
3727
3728 static void
3729 mention (b)
3730      struct breakpoint *b;
3731 {
3732   int say_where = 0;
3733
3734   /* FIXME: This is misplaced; mention() is called by things (like hitting a
3735      watchpoint) other than breakpoint creation.  It should be possible to
3736      clean this up and at the same time replace the random calls to
3737      breakpoint_changed with this hook, as has already been done for
3738      delete_breakpoint_hook and so on.  */
3739   if (create_breakpoint_hook)
3740     create_breakpoint_hook (b);
3741   breakpoint_create_event (b->number);
3742
3743   switch (b->type)
3744     {
3745     case bp_none:
3746       printf_filtered ("(apparently deleted?) Eventpoint %d: ", b->number);
3747       break;
3748     case bp_watchpoint:
3749       printf_filtered ("Watchpoint %d: ", b->number);
3750       print_expression (b->exp, gdb_stdout);
3751       break;
3752     case bp_hardware_watchpoint:
3753       printf_filtered ("Hardware watchpoint %d: ", b->number);
3754       print_expression (b->exp, gdb_stdout);
3755       break;
3756     case bp_read_watchpoint:
3757       printf_filtered ("Hardware read watchpoint %d: ", b->number);
3758       print_expression (b->exp, gdb_stdout);
3759       break;
3760     case bp_access_watchpoint:
3761       printf_filtered ("Hardware access (read/write) watchpoint %d: ", 
3762                        b->number);
3763       print_expression (b->exp, gdb_stdout);
3764       break;
3765     case bp_breakpoint:
3766       printf_filtered ("Breakpoint %d", b->number);
3767       say_where = 1;
3768       break;
3769     case bp_hardware_breakpoint:
3770       printf_filtered ("Hardware assisted breakpoint %d", b->number);
3771       say_where = 1;
3772       break;
3773     case bp_catch_load:
3774     case bp_catch_unload:
3775       printf_filtered ("Catchpoint %d (%s %s)",
3776                        b->number,
3777                        (b->type == bp_catch_load) ? "load" : "unload",
3778                        (b->dll_pathname != NULL) ? 
3779                        b->dll_pathname : "<any library>");
3780       break;
3781     case bp_catch_fork:
3782     case bp_catch_vfork:
3783       printf_filtered ("Catchpoint %d (%s)",
3784                        b->number,
3785                        (b->type == bp_catch_fork) ? "fork" : "vfork");
3786       break;
3787     case bp_catch_exec:
3788       printf_filtered ("Catchpoint %d (exec)",
3789                        b->number);
3790       break;
3791     case bp_catch_catch:
3792     case bp_catch_throw:
3793       printf_filtered ("Catchpoint %d (%s)",
3794                        b->number,
3795                        (b->type == bp_catch_catch) ? "catch" : "throw");
3796       break;
3797
3798     case bp_until:
3799     case bp_finish:
3800     case bp_longjmp:
3801     case bp_longjmp_resume:
3802     case bp_step_resume:
3803     case bp_through_sigtramp:
3804     case bp_call_dummy:
3805     case bp_watchpoint_scope:
3806     case bp_shlib_event:
3807       break;
3808     }
3809   if (say_where)
3810     {
3811       if (addressprint || b->source_file == NULL)
3812         {
3813           printf_filtered (" at ");
3814           print_address_numeric (b->address, 1, gdb_stdout);
3815         }
3816       if (b->source_file)
3817         printf_filtered (": file %s, line %d.",
3818                          b->source_file, b->line_number);
3819       TUIDO (((TuiOpaqueFuncPtr) tui_vAllSetHasBreakAt, b, 1));
3820       TUIDO (((TuiOpaqueFuncPtr) tuiUpdateAllExecInfos));
3821     }
3822   printf_filtered ("\n");
3823 }
3824 \f
3825
3826 /* Set a breakpoint according to ARG (function, linenum or *address)
3827    flag: first bit  : 0 non-temporary, 1 temporary.
3828    second bit : 0 normal breakpoint, 1 hardware breakpoint. */
3829
3830 static void
3831 break_command_1 (arg, flag, from_tty)
3832      char *arg;
3833      int flag, from_tty;
3834 {
3835   int tempflag, hardwareflag;
3836   struct symtabs_and_lines sals;
3837   struct symtab_and_line sal;
3838   register struct expression *cond = 0;
3839   register struct breakpoint *b;
3840
3841   /* Pointers in arg to the start, and one past the end, of the condition.  */
3842   char *cond_start = NULL;
3843   char *cond_end = NULL;
3844   /* Pointers in arg to the start, and one past the end,
3845      of the address part.  */
3846   char *addr_start = NULL;
3847   char *addr_end = NULL;
3848   struct cleanup *old_chain;
3849   struct cleanup *canonical_strings_chain = NULL;
3850   char **canonical = (char **) NULL;
3851   int i;
3852   int thread;
3853
3854   hardwareflag = flag & BP_HARDWAREFLAG;
3855   tempflag = flag & BP_TEMPFLAG;
3856
3857   sals.sals = NULL;
3858   sals.nelts = 0;
3859
3860   INIT_SAL (&sal);              /* initialize to zeroes */
3861
3862   /* If no arg given, or if first arg is 'if ', use the default breakpoint. */
3863
3864   if (!arg || (arg[0] == 'i' && arg[1] == 'f'
3865                && (arg[2] == ' ' || arg[2] == '\t')))
3866     {
3867       if (default_breakpoint_valid)
3868         {
3869           sals.sals = (struct symtab_and_line *)
3870             xmalloc (sizeof (struct symtab_and_line));
3871           sal.pc = default_breakpoint_address;
3872           sal.line = default_breakpoint_line;
3873           sal.symtab = default_breakpoint_symtab;
3874           sal.section = find_pc_overlay (sal.pc);
3875           sals.sals[0] = sal;
3876           sals.nelts = 1;
3877         }
3878       else
3879         error ("No default breakpoint address now.");
3880     }
3881   else
3882     {
3883       addr_start = arg;
3884
3885       /* Force almost all breakpoints to be in terms of the
3886          current_source_symtab (which is decode_line_1's default).  This
3887          should produce the results we want almost all of the time while
3888          leaving default_breakpoint_* alone.  */
3889       if (default_breakpoint_valid
3890           && (!current_source_symtab
3891               || (arg && (*arg == '+' || *arg == '-'))))
3892         sals = decode_line_1 (&arg, 1, default_breakpoint_symtab,
3893                               default_breakpoint_line, &canonical);
3894       else
3895         sals = decode_line_1 (&arg, 1, (struct symtab *) NULL, 0, &canonical);
3896
3897       addr_end = arg;
3898     }
3899
3900   if (!sals.nelts)
3901     return;
3902
3903   /* Make sure that all storage allocated in decode_line_1 gets freed
3904      in case the following `for' loop errors out.  */
3905   old_chain = make_cleanup (free, sals.sals);
3906   if (canonical != (char **) NULL)
3907     {
3908       make_cleanup (free, canonical);
3909       canonical_strings_chain = make_cleanup (null_cleanup, 0);
3910       for (i = 0; i < sals.nelts; i++)
3911         {
3912           if (canonical[i] != NULL)
3913             make_cleanup (free, canonical[i]);
3914         }
3915     }
3916
3917   thread = -1;                  /* No specific thread yet */
3918
3919   /* Resolve all line numbers to PC's, and verify that conditions
3920      can be parsed, before setting any breakpoints.  */
3921   for (i = 0; i < sals.nelts; i++)
3922     {
3923       char *tok, *end_tok;
3924       int toklen;
3925
3926       resolve_sal_pc (&sals.sals[i]);
3927
3928       /* It's possible for the PC to be nonzero, but still an illegal
3929          value on some targets.
3930
3931          For example, on HP-UX if you start gdb, and before running the
3932          inferior you try to set a breakpoint on a shared library function
3933          "foo" where the inferior doesn't call "foo" directly but does
3934          pass its address to another function call, then we do find a
3935          minimal symbol for the "foo", but it's address is invalid.
3936          (Appears to be an index into a table that the loader sets up
3937          when the inferior is run.)
3938
3939          Give the target a chance to bless sals.sals[i].pc before we
3940          try to make a breakpoint for it. */
3941       if (PC_REQUIRES_RUN_BEFORE_USE (sals.sals[i].pc))
3942         {
3943           error ("Cannot break on %s without a running program.", 
3944                  addr_start);
3945         }
3946
3947       tok = arg;
3948
3949       while (tok && *tok)
3950         {
3951           while (*tok == ' ' || *tok == '\t')
3952             tok++;
3953
3954           end_tok = tok;
3955
3956           while (*end_tok != ' ' && *end_tok != '\t' && *end_tok != '\000')
3957             end_tok++;
3958
3959           toklen = end_tok - tok;
3960
3961           if (toklen >= 1 && strncmp (tok, "if", toklen) == 0)
3962             {
3963               tok = cond_start = end_tok + 1;
3964               cond = parse_exp_1 (&tok, block_for_pc (sals.sals[i].pc), 0);
3965               cond_end = tok;
3966             }
3967           else if (toklen >= 1 && strncmp (tok, "thread", toklen) == 0)
3968             {
3969               char *tmptok;
3970
3971               tok = end_tok + 1;
3972               tmptok = tok;
3973               thread = strtol (tok, &tok, 0);
3974               if (tok == tmptok)
3975                 error ("Junk after thread keyword.");
3976               if (!valid_thread_id (thread))
3977                 error ("Unknown thread %d\n", thread);
3978             }
3979           else
3980             error ("Junk at end of arguments.");
3981         }
3982     }
3983   if (hardwareflag)
3984     {
3985       int i, target_resources_ok;
3986
3987       i = hw_breakpoint_used_count ();
3988       target_resources_ok = 
3989         TARGET_CAN_USE_HARDWARE_WATCHPOINT (bp_hardware_breakpoint, 
3990                                             i + sals.nelts, 0);
3991       if (target_resources_ok == 0)
3992         error ("No hardware breakpoint support in the target.");
3993       else if (target_resources_ok < 0)
3994         error ("Hardware breakpoints used exceeds limit.");
3995     }
3996
3997   /* Remove the canonical strings from the cleanup, they are needed below.  */
3998   if (canonical != (char **) NULL)
3999     discard_cleanups (canonical_strings_chain);
4000
4001   /* Now set all the breakpoints.  */
4002   for (i = 0; i < sals.nelts; i++)
4003     {
4004       sal = sals.sals[i];
4005
4006       if (from_tty)
4007         describe_other_breakpoints (sal.pc, sal.section);
4008
4009       b = set_raw_breakpoint (sal);
4010       set_breakpoint_count (breakpoint_count + 1);
4011       b->number = breakpoint_count;
4012       b->type = hardwareflag ? bp_hardware_breakpoint : bp_breakpoint;
4013       b->cond = cond;
4014       b->thread = thread;
4015
4016       /* If a canonical line spec is needed use that instead of the
4017          command string.  */
4018       if (canonical != (char **) NULL && canonical[i] != NULL)
4019         b->addr_string = canonical[i];
4020       else if (addr_start)
4021         b->addr_string = savestring (addr_start, addr_end - addr_start);
4022       if (cond_start)
4023         b->cond_string = savestring (cond_start, cond_end - cond_start);
4024
4025       b->enable = enabled;
4026       b->disposition = tempflag ? del : donttouch;
4027       mention (b);
4028     }
4029
4030   if (sals.nelts > 1)
4031     {
4032       warning ("Multiple breakpoints were set.");
4033       warning ("Use the \"delete\" command to delete unwanted breakpoints.");
4034     }
4035   do_cleanups (old_chain);
4036 }
4037
4038 static void
4039 break_at_finish_at_depth_command_1 (arg, flag, from_tty)
4040      char *arg;
4041      int flag;
4042      int from_tty;
4043 {
4044   struct frame_info *frame;
4045   CORE_ADDR low, high, selected_pc = 0;
4046   char *extra_args, *level_arg, *addr_string;
4047   int extra_args_len = 0, if_arg = 0;
4048
4049   if (!arg ||
4050       (arg[0] == 'i' && arg[1] == 'f' && (arg[2] == ' ' || arg[2] == '\t')))
4051     {
4052
4053       if (default_breakpoint_valid)
4054         {
4055           if (selected_frame)
4056             {
4057               selected_pc = selected_frame->pc;
4058               if (arg)
4059                 if_arg = 1;
4060             }
4061           else
4062             error ("No selected frame.");
4063         }
4064       else
4065         error ("No default breakpoint address now.");
4066     }
4067   else
4068     {
4069       extra_args = strchr (arg, ' ');
4070       if (extra_args)
4071         {
4072           extra_args++;
4073           extra_args_len = strlen (extra_args);
4074           level_arg = (char *) xmalloc (extra_args - arg);
4075           strncpy (level_arg, arg, extra_args - arg - 1);
4076           level_arg[extra_args - arg - 1] = '\0';
4077         }
4078       else
4079         {
4080           level_arg = (char *) xmalloc (strlen (arg) + 1);
4081           strcpy (level_arg, arg);
4082         }
4083
4084       frame = parse_frame_specification (level_arg);
4085       if (frame)
4086         selected_pc = frame->pc;
4087       else
4088         selected_pc = 0;
4089     }
4090   if (if_arg)
4091     {
4092       extra_args = arg;
4093       extra_args_len = strlen (arg);
4094     }
4095
4096   if (selected_pc)
4097     {
4098       if (find_pc_partial_function (selected_pc, (char **) NULL, &low, &high))
4099         {
4100           addr_string = (char *) xmalloc (26 + extra_args_len);
4101           if (extra_args_len)
4102             sprintf (addr_string, "*0x%s %s", paddr_nz (high), extra_args);
4103           else
4104             sprintf (addr_string, "*0x%s", paddr_nz (high));
4105           break_command_1 (addr_string, flag, from_tty);
4106           free (addr_string);
4107         }
4108       else
4109         error ("No function contains the specified address");
4110     }
4111   else
4112     error ("Unable to set breakpoint at procedure exit");
4113 }
4114
4115
4116 static void
4117 break_at_finish_command_1 (arg, flag, from_tty)
4118      char *arg;
4119      int flag;
4120      int from_tty;
4121 {
4122   char *addr_string, *break_string, *beg_addr_string;
4123   CORE_ADDR low, high;
4124   struct symtabs_and_lines sals;
4125   struct symtab_and_line sal;
4126   struct cleanup *old_chain;
4127   char *extra_args;
4128   int extra_args_len = 0;
4129   int i, if_arg = 0;
4130
4131   if (!arg ||
4132       (arg[0] == 'i' && arg[1] == 'f' && (arg[2] == ' ' || arg[2] == '\t')))
4133     {
4134       if (default_breakpoint_valid)
4135         {
4136           if (selected_frame)
4137             {
4138               addr_string = (char *) xmalloc (15);
4139               sprintf (addr_string, "*0x%s", paddr_nz (selected_frame->pc));
4140               if (arg)
4141                 if_arg = 1;
4142             }
4143           else
4144             error ("No selected frame.");
4145         }
4146       else
4147         error ("No default breakpoint address now.");
4148     }
4149   else
4150     {
4151       addr_string = (char *) xmalloc (strlen (arg) + 1);
4152       strcpy (addr_string, arg);
4153     }
4154
4155   if (if_arg)
4156     {
4157       extra_args = arg;
4158       extra_args_len = strlen (arg);
4159     }
4160   else if (arg)
4161     {
4162       /* get the stuff after the function name or address */
4163       extra_args = strchr (arg, ' ');
4164       if (extra_args)
4165         {
4166           extra_args++;
4167           extra_args_len = strlen (extra_args);
4168         }
4169     }
4170
4171   sals.sals = NULL;
4172   sals.nelts = 0;
4173
4174   beg_addr_string = addr_string;
4175   sals = decode_line_1 (&addr_string, 1, (struct symtab *) NULL, 0,
4176                         (char ***) NULL);
4177
4178   free (beg_addr_string);
4179   old_chain = make_cleanup (free, sals.sals);
4180   for (i = 0; (i < sals.nelts); i++)
4181     {
4182       sal = sals.sals[i];
4183       if (find_pc_partial_function (sal.pc, (char **) NULL, &low, &high))
4184         {
4185           break_string = (char *) xmalloc (extra_args_len + 26);
4186           if (extra_args_len)
4187             sprintf (break_string, "*0x%s %s", paddr_nz (high), extra_args);
4188           else
4189             sprintf (break_string, "*0x%s", paddr_nz (high));
4190           break_command_1 (break_string, flag, from_tty);
4191           free (break_string);
4192         }
4193       else
4194         error ("No function contains the specified address");
4195     }
4196   if (sals.nelts > 1)
4197     {
4198       warning ("Multiple breakpoints were set.\n");
4199       warning ("Use the \"delete\" command to delete unwanted breakpoints.");
4200     }
4201   do_cleanups (old_chain);
4202 }
4203
4204
4205 /* Helper function for break_command_1 and disassemble_command.  */
4206
4207 void
4208 resolve_sal_pc (sal)
4209      struct symtab_and_line *sal;
4210 {
4211   CORE_ADDR pc;
4212
4213   if (sal->pc == 0 && sal->symtab != NULL)
4214     {
4215       if (!find_line_pc (sal->symtab, sal->line, &pc))
4216         error ("No line %d in file \"%s\".",
4217                sal->line, sal->symtab->filename);
4218       sal->pc = pc;
4219     }
4220
4221   if (sal->section == 0 && sal->symtab != NULL)
4222     {
4223       struct blockvector *bv;
4224       struct block *b;
4225       struct symbol *sym;
4226       int index;
4227
4228       bv = blockvector_for_pc_sect (sal->pc, 0, &index, sal->symtab);
4229       if (bv != NULL)
4230         {
4231           b = BLOCKVECTOR_BLOCK (bv, index);
4232           sym = block_function (b);
4233           if (sym != NULL)
4234             {
4235               fixup_symbol_section (sym, sal->symtab->objfile);
4236               sal->section = SYMBOL_BFD_SECTION (sym);
4237             }
4238           else
4239             {
4240               /* It really is worthwhile to have the section, so we'll just
4241                  have to look harder. This case can be executed if we have 
4242                  line numbers but no functions (as can happen in assembly 
4243                  source).  */
4244
4245               struct minimal_symbol *msym;
4246
4247               msym = lookup_minimal_symbol_by_pc (sal->pc);
4248               if (msym)
4249                 sal->section = SYMBOL_BFD_SECTION (msym);
4250             }
4251         }
4252     }
4253 }
4254
4255 void
4256 break_command (arg, from_tty)
4257      char *arg;
4258      int from_tty;
4259 {
4260   break_command_1 (arg, 0, from_tty);
4261 }
4262
4263 static void
4264 break_at_finish_command (arg, from_tty)
4265      char *arg;
4266      int from_tty;
4267 {
4268   break_at_finish_command_1 (arg, 0, from_tty);
4269 }
4270
4271 static void
4272 break_at_finish_at_depth_command (arg, from_tty)
4273      char *arg;
4274      int from_tty;
4275 {
4276   break_at_finish_at_depth_command_1 (arg, 0, from_tty);
4277 }
4278
4279 void
4280 tbreak_command (arg, from_tty)
4281      char *arg;
4282      int from_tty;
4283 {
4284   break_command_1 (arg, BP_TEMPFLAG, from_tty);
4285 }
4286
4287 static void
4288 tbreak_at_finish_command (arg, from_tty)
4289      char *arg;
4290      int from_tty;
4291 {
4292   break_at_finish_command_1 (arg, BP_TEMPFLAG, from_tty);
4293 }
4294
4295 static void
4296 hbreak_command (arg, from_tty)
4297      char *arg;
4298      int from_tty;
4299 {
4300   break_command_1 (arg, BP_HARDWAREFLAG, from_tty);
4301 }
4302
4303 static void
4304 thbreak_command (arg, from_tty)
4305      char *arg;
4306      int from_tty;
4307 {
4308   break_command_1 (arg, (BP_TEMPFLAG | BP_HARDWAREFLAG), from_tty);
4309 }
4310
4311 static void
4312 stop_command (arg, from_tty)
4313      char *arg;
4314      int from_tty;
4315 {
4316   printf_filtered ("Specify the type of breakpoint to set.\n\
4317 Usage: stop in <function | address>\n\
4318        stop at <line>\n");
4319 }
4320
4321 static void
4322 stopin_command (arg, from_tty)
4323      char *arg;
4324      int from_tty;
4325 {
4326   int badInput = 0;
4327
4328   if (arg == (char *) NULL)
4329     badInput = 1;
4330   else if (*arg != '*')
4331     {
4332       char *argptr = arg;
4333       int hasColon = 0;
4334
4335       /* look for a ':'.  If this is a line number specification, then
4336          say it is bad, otherwise, it should be an address or
4337          function/method name */
4338       while (*argptr && !hasColon)
4339         {
4340           hasColon = (*argptr == ':');
4341           argptr++;
4342         }
4343
4344       if (hasColon)
4345         badInput = (*argptr != ':');    /* Not a class::method */
4346       else
4347         badInput = isdigit (*arg);      /* a simple line number */
4348     }
4349
4350   if (badInput)
4351     printf_filtered ("Usage: stop in <function | address>\n");
4352   else
4353     break_command_1 (arg, 0, from_tty);
4354 }
4355
4356 static void
4357 stopat_command (arg, from_tty)
4358      char *arg;
4359      int from_tty;
4360 {
4361   int badInput = 0;
4362
4363   if (arg == (char *) NULL || *arg == '*')      /* no line number */
4364     badInput = 1;
4365   else
4366     {
4367       char *argptr = arg;
4368       int hasColon = 0;
4369
4370       /* look for a ':'.  If there is a '::' then get out, otherwise
4371          it is probably a line number. */
4372       while (*argptr && !hasColon)
4373         {
4374           hasColon = (*argptr == ':');
4375           argptr++;
4376         }
4377
4378       if (hasColon)
4379         badInput = (*argptr == ':');    /* we have class::method */
4380       else
4381         badInput = !isdigit (*arg);     /* not a line number */
4382     }
4383
4384   if (badInput)
4385     printf_filtered ("Usage: stop at <line>\n");
4386   else
4387     break_command_1 (arg, 0, from_tty);
4388 }
4389
4390 /* ARGSUSED */
4391 /* accessflag:  hw_write:  watch write, 
4392                 hw_read:   watch read, 
4393                 hw_access: watch access (read or write) */
4394 static void
4395 watch_command_1 (arg, accessflag, from_tty)
4396      char *arg;
4397      int accessflag;
4398      int from_tty;
4399 {
4400   struct breakpoint *b;
4401   struct symtab_and_line sal;
4402   struct expression *exp;
4403   struct block *exp_valid_block;
4404   struct value *val, *mark;
4405   struct frame_info *frame;
4406   struct frame_info *prev_frame = NULL;
4407   char *exp_start = NULL;
4408   char *exp_end = NULL;
4409   char *tok, *end_tok;
4410   int toklen;
4411   char *cond_start = NULL;
4412   char *cond_end = NULL;
4413   struct expression *cond = NULL;
4414   int i, other_type_used, target_resources_ok = 0;
4415   enum bptype bp_type;
4416   int mem_cnt = 0;
4417
4418   INIT_SAL (&sal);              /* initialize to zeroes */
4419
4420   /* Parse arguments.  */
4421   innermost_block = NULL;
4422   exp_start = arg;
4423   exp = parse_exp_1 (&arg, 0, 0);
4424   exp_end = arg;
4425   exp_valid_block = innermost_block;
4426   mark = value_mark ();
4427   val = evaluate_expression (exp);
4428   release_value (val);
4429   if (VALUE_LAZY (val))
4430     value_fetch_lazy (val);
4431
4432   tok = arg;
4433   while (*tok == ' ' || *tok == '\t')
4434     tok++;
4435   end_tok = tok;
4436
4437   while (*end_tok != ' ' && *end_tok != '\t' && *end_tok != '\000')
4438     end_tok++;
4439
4440   toklen = end_tok - tok;
4441   if (toklen >= 1 && strncmp (tok, "if", toklen) == 0)
4442     {
4443       tok = cond_start = end_tok + 1;
4444       cond = parse_exp_1 (&tok, 0, 0);
4445       cond_end = tok;
4446     }
4447   if (*tok)
4448     error ("Junk at end of command.");
4449
4450   if (accessflag == hw_read)
4451     bp_type = bp_read_watchpoint;
4452   else if (accessflag == hw_access)
4453     bp_type = bp_access_watchpoint;
4454   else
4455     bp_type = bp_hardware_watchpoint;
4456
4457   mem_cnt = can_use_hardware_watchpoint (val);
4458   if (mem_cnt == 0 && bp_type != bp_hardware_watchpoint)
4459     error ("Expression cannot be implemented with read/access watchpoint.");
4460   if (mem_cnt != 0)
4461     {
4462       i = hw_watchpoint_used_count (bp_type, &other_type_used);
4463       target_resources_ok = 
4464         TARGET_CAN_USE_HARDWARE_WATCHPOINT (bp_type, i + mem_cnt, 
4465                                             other_type_used);
4466       if (target_resources_ok == 0 && bp_type != bp_hardware_watchpoint)
4467         error ("Target does not support this type of hardware watchpoint.");
4468
4469       if (target_resources_ok < 0 && bp_type != bp_hardware_watchpoint)
4470         error ("Target can only support one kind of HW watchpoint at a time.");
4471     }
4472
4473 #if defined(HPUXHPPA)
4474   /*  On HP-UX if you set a h/w
4475      watchpoint before the "run" command, the inferior dies with a e.g.,
4476      SIGILL once you start it.  I initially believed this was due to a
4477      bad interaction between page protection traps and the initial
4478      startup sequence by the dynamic linker.
4479
4480      However, I tried avoiding that by having HP-UX's implementation of
4481      TARGET_CAN_USE_HW_WATCHPOINT return FALSE if there was no inferior_pid
4482      yet, which forced slow watches before a "run" or "attach", and it
4483      still fails somewhere in the startup code.
4484
4485      Until I figure out what's happening, I'm disallowing watches altogether
4486      before the "run" or "attach" command.  We'll tell the user they must
4487      set watches after getting the program started. */
4488   if (!target_has_execution)
4489     {
4490       warning ("can't do that without a running program; try \"break main\", \"run\" first");
4491       return;
4492     }
4493 #endif /* HPUXHPPA */
4494
4495   /* Now set up the breakpoint.  */
4496   b = set_raw_breakpoint (sal);
4497   set_breakpoint_count (breakpoint_count + 1);
4498   b->number = breakpoint_count;
4499   b->disposition = donttouch;
4500   b->exp = exp;
4501   b->exp_valid_block = exp_valid_block;
4502   b->exp_string = savestring (exp_start, exp_end - exp_start);
4503   b->val = val;
4504   b->cond = cond;
4505   if (cond_start)
4506     b->cond_string = savestring (cond_start, cond_end - cond_start);
4507   else
4508     b->cond_string = 0;
4509
4510   frame = block_innermost_frame (exp_valid_block);
4511   if (frame)
4512     {
4513       prev_frame = get_prev_frame (frame);
4514       b->watchpoint_frame = frame->frame;
4515     }
4516   else
4517     b->watchpoint_frame = (CORE_ADDR) 0;
4518
4519   if (mem_cnt && target_resources_ok > 0)
4520     b->type = bp_type;
4521   else
4522     b->type = bp_watchpoint;
4523
4524   /* If the expression is "local", then set up a "watchpoint scope"
4525      breakpoint at the point where we've left the scope of the watchpoint
4526      expression.  */
4527   if (innermost_block)
4528     {
4529       if (prev_frame)
4530         {
4531           struct breakpoint *scope_breakpoint;
4532           struct symtab_and_line scope_sal;
4533
4534           INIT_SAL (&scope_sal);        /* initialize to zeroes */
4535           scope_sal.pc = get_frame_pc (prev_frame);
4536           scope_sal.section = find_pc_overlay (scope_sal.pc);
4537
4538           scope_breakpoint = set_raw_breakpoint (scope_sal);
4539           set_breakpoint_count (breakpoint_count + 1);
4540           scope_breakpoint->number = breakpoint_count;
4541
4542           scope_breakpoint->type = bp_watchpoint_scope;
4543           scope_breakpoint->enable = enabled;
4544
4545           /* Automatically delete the breakpoint when it hits.  */
4546           scope_breakpoint->disposition = del;
4547
4548           /* Only break in the proper frame (help with recursion).  */
4549           scope_breakpoint->frame = prev_frame->frame;
4550
4551           /* Set the address at which we will stop.  */
4552           scope_breakpoint->address = get_frame_pc (prev_frame);
4553
4554           /* The scope breakpoint is related to the watchpoint.  We
4555              will need to act on them together.  */
4556           b->related_breakpoint = scope_breakpoint;
4557         }
4558     }
4559   value_free_to_mark (mark);
4560   mention (b);
4561 }
4562
4563 /* Return count of locations need to be watched and can be handled
4564    in hardware.  If the watchpoint can not be handled
4565    in hardware return zero.  */
4566
4567 #if !defined(TARGET_REGION_SIZE_OK_FOR_HW_WATCHPOINT)
4568 #define TARGET_REGION_SIZE_OK_FOR_HW_WATCHPOINT(BYTE_SIZE) \
4569     ((BYTE_SIZE) <= (REGISTER_SIZE))
4570 #endif
4571
4572 #if !defined(TARGET_REGION_OK_FOR_HW_WATCHPOINT)
4573 #define TARGET_REGION_OK_FOR_HW_WATCHPOINT(ADDR,LEN) \
4574      TARGET_REGION_SIZE_OK_FOR_HW_WATCHPOINT(LEN)
4575 #endif
4576
4577 static int
4578 can_use_hardware_watchpoint (v)
4579      struct value *v;
4580 {
4581   int found_memory_cnt = 0;
4582
4583   /* Did the user specifically forbid us to use hardware watchpoints? */
4584   if (!can_use_hw_watchpoints)
4585     return 0;
4586
4587   /* Make sure all the intermediate values are in memory.  Also make sure
4588      we found at least one memory expression.  Guards against watch 0x12345,
4589      which is meaningless, but could cause errors if one tries to insert a 
4590      hardware watchpoint for the constant expression.  */
4591   for (; v; v = v->next)
4592     {
4593       if (v->lval == lval_memory)
4594         {
4595           CORE_ADDR vaddr = VALUE_ADDRESS (v) + VALUE_OFFSET (v);
4596           int       len   = TYPE_LENGTH (VALUE_TYPE (v));
4597
4598           if (!TARGET_REGION_OK_FOR_HW_WATCHPOINT (vaddr, len))
4599             return 0;
4600           else
4601             found_memory_cnt++;
4602         }
4603       else if (v->lval != not_lval && v->modifiable == 0)
4604         return 0;       /* ??? What does this represent? */
4605       else if (v->lval == lval_register)
4606         return 0;       /* cannot watch a register with a HW watchpoint */
4607     }
4608
4609   /* The expression itself looks suitable for using a hardware
4610      watchpoint, but give the target machine a chance to reject it.  */
4611   return found_memory_cnt;
4612 }
4613
4614 static void
4615 watch_command (arg, from_tty)
4616      char *arg;
4617      int from_tty;
4618 {
4619   watch_command_1 (arg, hw_write, from_tty);
4620 }
4621
4622 static void
4623 rwatch_command (arg, from_tty)
4624      char *arg;
4625      int from_tty;
4626 {
4627   watch_command_1 (arg, hw_read, from_tty);
4628 }
4629
4630 static void
4631 awatch_command (arg, from_tty)
4632      char *arg;
4633      int from_tty;
4634 {
4635   watch_command_1 (arg, hw_access, from_tty);
4636 }
4637 \f
4638
4639 /* Helper routines for the until_command routine in infcmd.c.  Here
4640    because it uses the mechanisms of breakpoints.  */
4641
4642 /* This function is called by fetch_inferior_event via the
4643    cmd_continuation pointer, to complete the until command. It takes
4644    care of cleaning up the temporary breakpoints set up by the until
4645    command. */
4646 void
4647 until_break_command_continuation (arg)
4648      struct continuation_arg *arg;
4649 {
4650   /* Do all the exec cleanups, which at this point should only be the
4651      one set up in the first part of the until_break_command
4652      function. */
4653   do_exec_cleanups (ALL_CLEANUPS);
4654 }
4655
4656 /* ARGSUSED */
4657 void
4658 until_break_command (arg, from_tty)
4659      char *arg;
4660      int from_tty;
4661 {
4662   struct symtabs_and_lines sals;
4663   struct symtab_and_line sal;
4664   struct frame_info *prev_frame = get_prev_frame (selected_frame);
4665   struct breakpoint *breakpoint;
4666   struct cleanup *old_chain;
4667
4668   clear_proceed_status ();
4669
4670   /* Set a breakpoint where the user wants it and at return from
4671      this function */
4672
4673   if (default_breakpoint_valid)
4674     sals = decode_line_1 (&arg, 1, default_breakpoint_symtab,
4675                           default_breakpoint_line, (char ***) NULL);
4676   else
4677     sals = decode_line_1 (&arg, 1, (struct symtab *) NULL, 
4678                           0, (char ***) NULL);
4679
4680   if (sals.nelts != 1)
4681     error ("Couldn't get information on specified line.");
4682
4683   sal = sals.sals[0];
4684   free ((PTR) sals.sals);       /* malloc'd, so freed */
4685
4686   if (*arg)
4687     error ("Junk at end of arguments.");
4688
4689   resolve_sal_pc (&sal);
4690
4691   breakpoint = set_momentary_breakpoint (sal, selected_frame, bp_until);
4692
4693   if (!async_p || !target_has_async)
4694     old_chain = make_cleanup ((make_cleanup_func) delete_breakpoint, 
4695                               breakpoint);
4696   else
4697     make_exec_cleanup ((make_cleanup_func) delete_breakpoint, breakpoint);
4698
4699   /* If we are running asynchronously, and the target supports async
4700      execution, we are not waiting for the target to stop, in the call
4701      tp proceed, below. This means that we cannot delete the
4702      brekpoints until the target has actually stopped. The only place
4703      where we get a chance to do that is in fetch_inferior_event, so
4704      we must set things up for that. */
4705
4706   if (async_p && target_has_async)
4707     {
4708       /* In this case we don't need args for the continuation, because
4709          all it needs to do is do the cleanups in the
4710          exec_cleanup_chain, which will be only those inserted by this
4711          function. We can get away by using ALL_CLEANUPS. */
4712       add_continuation (until_break_command_continuation, NULL);
4713     }
4714
4715   /* Keep within the current frame */
4716
4717   if (prev_frame)
4718     {
4719       sal = find_pc_line (prev_frame->pc, 0);
4720       sal.pc = prev_frame->pc;
4721       breakpoint = set_momentary_breakpoint (sal, prev_frame, bp_until);
4722       if (!async_p || !target_has_async)
4723         make_cleanup ((make_cleanup_func) delete_breakpoint, breakpoint);
4724       else
4725         make_exec_cleanup ((make_cleanup_func) delete_breakpoint, breakpoint);
4726     }
4727
4728   proceed (-1, TARGET_SIGNAL_DEFAULT, 0);
4729   /* Do the cleanups now, anly if we are not running asynchronously,
4730      of if we are, but the target is still synchronous. */
4731   if (!async_p || !target_has_async)
4732     do_cleanups (old_chain);
4733 }
4734 \f
4735 #if 0
4736 /* These aren't used; I don't konw what they were for.  */
4737 /* Set a breakpoint at the catch clause for NAME.  */
4738 static int
4739 catch_breakpoint (name)
4740      char *name;
4741 {
4742 }
4743
4744 static int
4745 disable_catch_breakpoint ()
4746 {
4747 }
4748
4749 static int
4750 delete_catch_breakpoint ()
4751 {
4752 }
4753
4754 static int
4755 enable_catch_breakpoint ()
4756 {
4757 }
4758 #endif /* 0 */
4759
4760 struct sal_chain
4761 {
4762   struct sal_chain *next;
4763   struct symtab_and_line sal;
4764 };
4765
4766 #if 0
4767 /* Not really used -- invocation in handle_gnu_4_16_catch_command
4768    had been commented out in the v.4.16 sources, and stays
4769    disabled there now because "catch NAME" syntax isn't allowed.
4770    pai/1997-07-11 */
4771 /* This isn't used; I don't know what it was for.  */
4772 /* For each catch clause identified in ARGS, run FUNCTION
4773    with that clause as an argument.  */
4774 static struct symtabs_and_lines
4775 map_catch_names (args, function)
4776      char *args;
4777      int (*function) ();
4778 {
4779   register char *p = args;
4780   register char *p1;
4781   struct symtabs_and_lines sals;
4782 #if 0
4783   struct sal_chain *sal_chain = 0;
4784 #endif
4785
4786   if (p == 0)
4787     error_no_arg ("one or more catch names");
4788
4789   sals.nelts = 0;
4790   sals.sals = NULL;
4791
4792   while (*p)
4793     {
4794       p1 = p;
4795       /* Don't swallow conditional part.  */
4796       if (p1[0] == 'i' && p1[1] == 'f'
4797           && (p1[2] == ' ' || p1[2] == '\t'))
4798         break;
4799
4800       if (isalpha (*p1))
4801         {
4802           p1++;
4803           while (isalnum (*p1) || *p1 == '_' || *p1 == '$')
4804             p1++;
4805         }
4806
4807       if (*p1 && *p1 != ' ' && *p1 != '\t')
4808         error ("Arguments must be catch names.");
4809
4810       *p1 = 0;
4811 #if 0
4812       if (function (p))
4813         {
4814           struct sal_chain *next = (struct sal_chain *)
4815           alloca (sizeof (struct sal_chain));
4816           next->next = sal_chain;
4817           next->sal = get_catch_sal (p);
4818           sal_chain = next;
4819           goto win;
4820         }
4821 #endif
4822       printf_unfiltered ("No catch clause for exception %s.\n", p);
4823 #if 0
4824     win:
4825 #endif
4826       p = p1;
4827       while (*p == ' ' || *p == '\t')
4828         p++;
4829     }
4830 }
4831 #endif
4832
4833 /* This shares a lot of code with `print_frame_label_vars' from stack.c.  */
4834
4835 static struct symtabs_and_lines
4836 get_catch_sals (this_level_only)
4837      int this_level_only;
4838 {
4839   register struct blockvector *bl;
4840   register struct block *block;
4841   int index, have_default = 0;
4842   CORE_ADDR pc;
4843   struct symtabs_and_lines sals;
4844   struct sal_chain *sal_chain = 0;
4845   char *blocks_searched;
4846
4847   /* Not sure whether an error message is always the correct response,
4848      but it's better than a core dump.  */
4849   if (selected_frame == NULL)
4850     error ("No selected frame.");
4851   block = get_frame_block (selected_frame);
4852   pc = selected_frame->pc;
4853
4854   sals.nelts = 0;
4855   sals.sals = NULL;
4856
4857   if (block == 0)
4858     error ("No symbol table info available.\n");
4859
4860   bl = blockvector_for_pc (BLOCK_END (block) - 4, &index);
4861   blocks_searched = (char *) alloca (BLOCKVECTOR_NBLOCKS (bl) * sizeof (char));
4862   memset (blocks_searched, 0, BLOCKVECTOR_NBLOCKS (bl) * sizeof (char));
4863
4864   while (block != 0)
4865     {
4866       CORE_ADDR end = BLOCK_END (block) - 4;
4867       int last_index;
4868
4869       if (bl != blockvector_for_pc (end, &index))
4870         error ("blockvector blotch");
4871       if (BLOCKVECTOR_BLOCK (bl, index) != block)
4872         error ("blockvector botch");
4873       last_index = BLOCKVECTOR_NBLOCKS (bl);
4874       index += 1;
4875
4876       /* Don't print out blocks that have gone by.  */
4877       while (index < last_index
4878              && BLOCK_END (BLOCKVECTOR_BLOCK (bl, index)) < pc)
4879         index++;
4880
4881       while (index < last_index
4882              && BLOCK_END (BLOCKVECTOR_BLOCK (bl, index)) < end)
4883         {
4884           if (blocks_searched[index] == 0)
4885             {
4886               struct block *b = BLOCKVECTOR_BLOCK (bl, index);
4887               int nsyms;
4888               register int i;
4889               register struct symbol *sym;
4890
4891               nsyms = BLOCK_NSYMS (b);
4892
4893               for (i = 0; i < nsyms; i++)
4894                 {
4895                   sym = BLOCK_SYM (b, i);
4896                   if (STREQ (SYMBOL_NAME (sym), "default"))
4897                     {
4898                       if (have_default)
4899                         continue;
4900                       have_default = 1;
4901                     }
4902                   if (SYMBOL_CLASS (sym) == LOC_LABEL)
4903                     {
4904                       struct sal_chain *next = (struct sal_chain *)
4905                       alloca (sizeof (struct sal_chain));
4906                       next->next = sal_chain;
4907                       next->sal = find_pc_line (SYMBOL_VALUE_ADDRESS (sym), 
4908                                                 0);
4909                       sal_chain = next;
4910                     }
4911                 }
4912               blocks_searched[index] = 1;
4913             }
4914           index++;
4915         }
4916       if (have_default)
4917         break;
4918       if (sal_chain && this_level_only)
4919         break;
4920
4921       /* After handling the function's top-level block, stop.
4922          Don't continue to its superblock, the block of
4923          per-file symbols.  */
4924       if (BLOCK_FUNCTION (block))
4925         break;
4926       block = BLOCK_SUPERBLOCK (block);
4927     }
4928
4929   if (sal_chain)
4930     {
4931       struct sal_chain *tmp_chain;
4932
4933       /* Count the number of entries.  */
4934       for (index = 0, tmp_chain = sal_chain; tmp_chain;
4935            tmp_chain = tmp_chain->next)
4936         index++;
4937
4938       sals.nelts = index;
4939       sals.sals = (struct symtab_and_line *)
4940         xmalloc (index * sizeof (struct symtab_and_line));
4941       for (index = 0; sal_chain; sal_chain = sal_chain->next, index++)
4942         sals.sals[index] = sal_chain->sal;
4943     }
4944
4945   return sals;
4946 }
4947
4948 static void
4949 ep_skip_leading_whitespace (s)
4950      char **s;
4951 {
4952   if ((s == NULL) || (*s == NULL))
4953     return;
4954   while (isspace (**s))
4955     *s += 1;
4956 }
4957
4958 /* This function examines a string, and attempts to find a token
4959    that might be an event name in the leading characters.  If a
4960    possible match is found, a pointer to the last character of
4961    the token is returned.  Else, NULL is returned. */
4962
4963 static char *
4964 ep_find_event_name_end (arg)
4965      char *arg;
4966 {
4967   char *s = arg;
4968   char *event_name_end = NULL;
4969
4970   /* If we could depend upon the presense of strrpbrk, we'd use that... */
4971   if (arg == NULL)
4972     return NULL;
4973
4974   /* We break out of the loop when we find a token delimiter.
4975      Basically, we're looking for alphanumerics and underscores;
4976      anything else delimites the token. */
4977   while (*s != '\0')
4978     {
4979       if (!isalnum (*s) && (*s != '_'))
4980         break;
4981       event_name_end = s;
4982       s++;
4983     }
4984
4985   return event_name_end;
4986 }
4987
4988
4989 /* This function attempts to parse an optional "if <cond>" clause
4990    from the arg string.  If one is not found, it returns NULL.
4991
4992    Else, it returns a pointer to the condition string.  (It does not
4993    attempt to evaluate the string against a particular block.)  And,
4994    it updates arg to point to the first character following the parsed
4995    if clause in the arg string. */
4996
4997 static char *
4998 ep_parse_optional_if_clause (arg)
4999      char **arg;
5000 {
5001   char *cond_string;
5002
5003   if (((*arg)[0] != 'i') || ((*arg)[1] != 'f') || !isspace ((*arg)[2]))
5004     return NULL;
5005
5006   /* Skip the "if" keyword. */
5007   (*arg) += 2;
5008
5009   /* Skip any extra leading whitespace, and record the start of the
5010      condition string. */
5011   ep_skip_leading_whitespace (arg);
5012   cond_string = *arg;
5013
5014   /* Assume that the condition occupies the remainder of the arg string. */
5015   (*arg) += strlen (cond_string);
5016
5017   return cond_string;
5018 }
5019
5020 /* This function attempts to parse an optional filename from the arg
5021    string.  If one is not found, it returns NULL.
5022
5023    Else, it returns a pointer to the parsed filename.  (This function
5024    makes no attempt to verify that a file of that name exists, or is
5025    accessible.)  And, it updates arg to point to the first character
5026    following the parsed filename in the arg string.
5027
5028    Note that clients needing to preserve the returned filename for
5029    future access should copy it to their own buffers. */
5030 static char *
5031 ep_parse_optional_filename (arg)
5032      char **arg;
5033 {
5034   static char filename[1024];
5035   char *arg_p = *arg;
5036   int i;
5037   char c;
5038
5039   if ((*arg_p == '\0') || isspace (*arg_p))
5040     return NULL;
5041
5042   for (i = 0;; i++)
5043     {
5044       c = *arg_p;
5045       if (isspace (c))
5046         c = '\0';
5047       filename[i] = c;
5048       if (c == '\0')
5049         break;
5050       arg_p++;
5051     }
5052   *arg = arg_p;
5053
5054   return filename;
5055 }
5056
5057 /* Commands to deal with catching events, such as signals, exceptions,
5058    process start/exit, etc.  */
5059
5060 typedef enum
5061 {
5062   catch_fork, catch_vfork
5063 }
5064 catch_fork_kind;
5065
5066 static void catch_fork_command_1 PARAMS ((catch_fork_kind fork_kind, 
5067                                           char *arg, 
5068                                           int tempflag, 
5069                                           int from_tty));
5070
5071 static void
5072 catch_fork_command_1 (fork_kind, arg, tempflag, from_tty)
5073      catch_fork_kind fork_kind;
5074      char *arg;
5075      int tempflag;
5076      int from_tty;
5077 {
5078   char *cond_string = NULL;
5079
5080   ep_skip_leading_whitespace (&arg);
5081
5082   /* The allowed syntax is:
5083      catch [v]fork
5084      catch [v]fork if <cond>
5085
5086      First, check if there's an if clause. */
5087   cond_string = ep_parse_optional_if_clause (&arg);
5088
5089   if ((*arg != '\0') && !isspace (*arg))
5090     error ("Junk at end of arguments.");
5091
5092   /* If this target supports it, create a fork or vfork catchpoint
5093      and enable reporting of such events. */
5094   switch (fork_kind)
5095     {
5096     case catch_fork:
5097       create_fork_event_catchpoint (tempflag, cond_string);
5098       break;
5099     case catch_vfork:
5100       create_vfork_event_catchpoint (tempflag, cond_string);
5101       break;
5102     default:
5103       error ("unsupported or unknown fork kind; cannot catch it");
5104       break;
5105     }
5106 }
5107
5108 static void
5109 catch_exec_command_1 (arg, tempflag, from_tty)
5110      char *arg;
5111      int tempflag;
5112      int from_tty;
5113 {
5114   char *cond_string = NULL;
5115
5116   ep_skip_leading_whitespace (&arg);
5117
5118   /* The allowed syntax is:
5119      catch exec
5120      catch exec if <cond>
5121
5122      First, check if there's an if clause. */
5123   cond_string = ep_parse_optional_if_clause (&arg);
5124
5125   if ((*arg != '\0') && !isspace (*arg))
5126     error ("Junk at end of arguments.");
5127
5128   /* If this target supports it, create an exec catchpoint
5129      and enable reporting of such events. */
5130   create_exec_event_catchpoint (tempflag, cond_string);
5131 }
5132
5133 #if defined(SOLIB_ADD)
5134 static void
5135 catch_load_command_1 (arg, tempflag, from_tty)
5136      char *arg;
5137      int tempflag;
5138      int from_tty;
5139 {
5140   char *dll_pathname = NULL;
5141   char *cond_string = NULL;
5142
5143   ep_skip_leading_whitespace (&arg);
5144
5145   /* The allowed syntax is:
5146      catch load
5147      catch load if <cond>
5148      catch load <filename>
5149      catch load <filename> if <cond>
5150
5151      The user is not allowed to specify the <filename> after an
5152      if clause.
5153
5154      We'll ignore the pathological case of a file named "if".
5155
5156      First, check if there's an if clause.  If so, then there
5157      cannot be a filename. */
5158   cond_string = ep_parse_optional_if_clause (&arg);
5159
5160   /* If there was an if clause, then there cannot be a filename.
5161      Else, there might be a filename and an if clause. */
5162   if (cond_string == NULL)
5163     {
5164       dll_pathname = ep_parse_optional_filename (&arg);
5165       ep_skip_leading_whitespace (&arg);
5166       cond_string = ep_parse_optional_if_clause (&arg);
5167     }
5168
5169   if ((*arg != '\0') && !isspace (*arg))
5170     error ("Junk at end of arguments.");
5171
5172   /* Create a load breakpoint that only triggers when a load of
5173      the specified dll (or any dll, if no pathname was specified)
5174      occurs. */
5175   SOLIB_CREATE_CATCH_LOAD_HOOK (inferior_pid, tempflag, 
5176                                 dll_pathname, cond_string);
5177 }
5178
5179 static void
5180 catch_unload_command_1 (arg, tempflag, from_tty)
5181      char *arg;
5182      int tempflag;
5183      int from_tty;
5184 {
5185   char *dll_pathname = NULL;
5186   char *cond_string = NULL;
5187
5188   ep_skip_leading_whitespace (&arg);
5189
5190   /* The allowed syntax is:
5191      catch unload
5192      catch unload if <cond>
5193      catch unload <filename>
5194      catch unload <filename> if <cond>
5195
5196      The user is not allowed to specify the <filename> after an
5197      if clause.
5198
5199      We'll ignore the pathological case of a file named "if".
5200
5201      First, check if there's an if clause.  If so, then there
5202      cannot be a filename. */
5203   cond_string = ep_parse_optional_if_clause (&arg);
5204
5205   /* If there was an if clause, then there cannot be a filename.
5206      Else, there might be a filename and an if clause. */
5207   if (cond_string == NULL)
5208     {
5209       dll_pathname = ep_parse_optional_filename (&arg);
5210       ep_skip_leading_whitespace (&arg);
5211       cond_string = ep_parse_optional_if_clause (&arg);
5212     }
5213
5214   if ((*arg != '\0') && !isspace (*arg))
5215     error ("Junk at end of arguments.");
5216
5217   /* Create an unload breakpoint that only triggers when an unload of
5218      the specified dll (or any dll, if no pathname was specified)
5219      occurs. */
5220   SOLIB_CREATE_CATCH_UNLOAD_HOOK (inferior_pid, tempflag, 
5221                                   dll_pathname, cond_string);
5222 }
5223 #endif /* SOLIB_ADD */
5224
5225 /* Commands to deal with catching exceptions.  */
5226
5227 /* Set a breakpoint at the specified callback routine for an
5228    exception event callback */
5229
5230 static void
5231 create_exception_catchpoint (tempflag, cond_string, ex_event, sal)
5232      int tempflag;
5233      char *cond_string;
5234      enum exception_event_kind ex_event;
5235      struct symtab_and_line *sal;
5236 {
5237   struct breakpoint *b;
5238   int thread = -1;              /* All threads. */
5239
5240   if (!sal)                     /* no exception support? */
5241     return;
5242
5243   b = set_raw_breakpoint (*sal);
5244   set_breakpoint_count (breakpoint_count + 1);
5245   b->number = breakpoint_count;
5246   b->cond = NULL;
5247   b->cond_string = (cond_string == NULL) ? 
5248     NULL : savestring (cond_string, strlen (cond_string));
5249   b->thread = thread;
5250   b->addr_string = NULL;
5251   b->enable = enabled;
5252   b->disposition = tempflag ? del : donttouch;
5253   switch (ex_event)
5254     {
5255     case EX_EVENT_THROW:
5256       b->type = bp_catch_throw;
5257       break;
5258     case EX_EVENT_CATCH:
5259       b->type = bp_catch_catch;
5260       break;
5261     default:                    /* error condition */
5262       b->type = bp_none;
5263       b->enable = disabled;
5264       error ("Internal error -- invalid catchpoint kind");
5265     }
5266   mention (b);
5267 }
5268
5269 /* Deal with "catch catch" and "catch throw" commands */
5270
5271 static void
5272 catch_exception_command_1 (ex_event, arg, tempflag, from_tty)
5273      enum exception_event_kind ex_event;
5274      char *arg;
5275      int tempflag;
5276      int from_tty;
5277 {
5278   char *cond_string = NULL;
5279   struct symtab_and_line *sal = NULL;
5280
5281   ep_skip_leading_whitespace (&arg);
5282
5283   cond_string = ep_parse_optional_if_clause (&arg);
5284
5285   if ((*arg != '\0') && !isspace (*arg))
5286     error ("Junk at end of arguments.");
5287
5288   if ((ex_event != EX_EVENT_THROW) &&
5289       (ex_event != EX_EVENT_CATCH))
5290     error ("Unsupported or unknown exception event; cannot catch it");
5291
5292   /* See if we can find a callback routine */
5293   sal = target_enable_exception_callback (ex_event, 1);
5294
5295   if (sal)
5296     {
5297       /* We have callbacks from the runtime system for exceptions.
5298          Set a breakpoint on the sal found, if no errors */
5299       if (sal != (struct symtab_and_line *) -1)
5300         create_exception_catchpoint (tempflag, cond_string, ex_event, sal);
5301       else
5302         return;         /* something went wrong with setting up callbacks */
5303     }
5304   else
5305     {
5306       /* No callbacks from runtime system for exceptions.
5307          Try GNU C++ exception breakpoints using labels in debug info. */
5308       if (ex_event == EX_EVENT_CATCH)
5309         {
5310           handle_gnu_4_16_catch_command (arg, tempflag, from_tty);
5311         }
5312       else if (ex_event == EX_EVENT_THROW)
5313         {
5314           /* Set a breakpoint on __raise_exception () */
5315
5316           warning ("Unsupported with this platform/compiler combination.");
5317           warning ("Perhaps you can achieve the effect you want by setting");
5318           warning ("a breakpoint on __raise_exception().");
5319         }
5320     }
5321 }
5322
5323 /* Cover routine to allow wrapping target_enable_exception_catchpoints
5324    inside a catch_errors */
5325
5326 static int
5327 cover_target_enable_exception_callback (arg)
5328      PTR arg;
5329 {
5330   args_for_catchpoint_enable *args = arg;
5331   struct symtab_and_line *sal;
5332   sal = target_enable_exception_callback (args->kind, args->enable);
5333   if (sal == NULL)
5334     return 0;
5335   else if (sal == (struct symtab_and_line *) -1)
5336     return -1;
5337   else
5338     return 1;                   /*is valid */
5339 }
5340
5341
5342
5343 /* This is the original v.4.16 and earlier version of the
5344    catch_command_1() function.  Now that other flavours of "catch"
5345    have been introduced, and since exception handling can be handled
5346    in other ways (through target ops) also, this is used only for the
5347    GNU C++ exception handling system.
5348    Note: Only the "catch" flavour of GDB 4.16 is handled here.  The
5349    "catch NAME" is now no longer allowed in catch_command_1().  Also,
5350    there was no code in GDB 4.16 for "catch throw". 
5351
5352    Called from catch_exception_command_1 () */
5353
5354
5355 static void
5356 handle_gnu_4_16_catch_command (arg, tempflag, from_tty)
5357      char *arg;
5358      int tempflag;
5359      int from_tty;
5360 {
5361   /* First, translate ARG into something we can deal with in terms
5362      of breakpoints.  */
5363
5364   struct symtabs_and_lines sals;
5365   struct symtab_and_line sal;
5366   register struct expression *cond = 0;
5367   register struct breakpoint *b;
5368   char *save_arg;
5369   int i;
5370
5371   INIT_SAL (&sal);              /* initialize to zeroes */
5372
5373   /* If no arg given, or if first arg is 'if ', all active catch clauses
5374      are breakpointed. */
5375
5376   if (!arg || (arg[0] == 'i' && arg[1] == 'f'
5377                && (arg[2] == ' ' || arg[2] == '\t')))
5378     {
5379       /* Grab all active catch clauses.  */
5380       sals = get_catch_sals (0);
5381     }
5382   else
5383     {
5384       /* Grab selected catch clauses.  */
5385       error ("catch NAME not implemented");
5386
5387 #if 0
5388       /* Not sure why this code has been disabled. I'm leaving
5389          it disabled.  We can never come here now anyway
5390          since we don't allow the "catch NAME" syntax.
5391          pai/1997-07-11 */
5392
5393       /* This isn't used; I don't know what it was for.  */
5394       sals = map_catch_names (arg, catch_breakpoint);
5395 #endif
5396     }
5397
5398   if (!sals.nelts)
5399     return;
5400
5401   save_arg = arg;
5402   for (i = 0; i < sals.nelts; i++)
5403     {
5404       resolve_sal_pc (&sals.sals[i]);
5405
5406       while (arg && *arg)
5407         {
5408           if (arg[0] == 'i' && arg[1] == 'f'
5409               && (arg[2] == ' ' || arg[2] == '\t'))
5410             cond = parse_exp_1 ((arg += 2, &arg),
5411                                 block_for_pc (sals.sals[i].pc), 0);
5412           else
5413             error ("Junk at end of arguments.");
5414         }
5415       arg = save_arg;
5416     }
5417
5418   for (i = 0; i < sals.nelts; i++)
5419     {
5420       sal = sals.sals[i];
5421
5422       if (from_tty)
5423         describe_other_breakpoints (sal.pc, sal.section);
5424
5425       b = set_raw_breakpoint (sal);
5426       set_breakpoint_count (breakpoint_count + 1);
5427       b->number = breakpoint_count;
5428
5429       /* Important -- this is an ordinary breakpoint.  For platforms
5430          with callback support for exceptions,
5431          create_exception_catchpoint() will create special bp types
5432          (bp_catch_catch and bp_catch_throw), and there is code in
5433          insert_breakpoints() and elsewhere that depends on that. */
5434       b->type = bp_breakpoint;  
5435
5436       b->cond = cond;
5437       b->enable = enabled;
5438       b->disposition = tempflag ? del : donttouch;
5439
5440       mention (b);
5441     }
5442
5443   if (sals.nelts > 1)
5444     {
5445       warning ("Multiple breakpoints were set.");
5446       warning ("Use the \"delete\" command to delete unwanted breakpoints.");
5447     }
5448   free ((PTR) sals.sals);
5449 }
5450
5451 #if 0
5452 /* This creates a temporary internal breakpoint
5453    just to placate infrun */
5454 static struct breakpoint *
5455 create_temp_exception_breakpoint (pc)
5456      CORE_ADDR pc;
5457 {
5458   struct symtab_and_line sal;
5459   struct breakpoint *b;
5460
5461   INIT_SAL (&sal);
5462   sal.pc = pc;
5463   sal.symtab = NULL;
5464   sal.line = 0;
5465
5466   b = set_raw_breakpoint (sal);
5467   if (!b)
5468     error ("Internal error -- couldn't set temp exception breakpoint");
5469
5470   b->type = bp_breakpoint;
5471   b->disposition = del;
5472   b->enable = enabled;
5473   b->silent = 1;
5474   b->number = internal_breakpoint_number--;
5475   return b;
5476 }
5477 #endif
5478
5479 static void
5480 catch_command_1 (arg, tempflag, from_tty)
5481      char *arg;
5482      int tempflag;
5483      int from_tty;
5484 {
5485
5486   /* The first argument may be an event name, such as "start" or "load".
5487      If so, then handle it as such.  If it doesn't match an event name,
5488      then attempt to interpret it as an exception name.  (This latter is
5489      the v4.16-and-earlier GDB meaning of the "catch" command.)
5490
5491      First, try to find the bounds of what might be an event name. */
5492   char *arg1_start = arg;
5493   char *arg1_end;
5494   int arg1_length;
5495
5496   if (arg1_start == NULL)
5497     {
5498       /* Old behaviour was to use pre-v-4.16 syntax */
5499       /* catch_throw_command_1 (arg1_start, tempflag, from_tty); */
5500       /* return; */
5501       /* Now, this is not allowed */
5502       error ("Catch requires an event name.");
5503
5504     }
5505   arg1_end = ep_find_event_name_end (arg1_start);
5506   if (arg1_end == NULL)
5507     error ("catch requires an event");
5508   arg1_length = arg1_end + 1 - arg1_start;
5509
5510   /* Try to match what we found against known event names. */
5511   if (strncmp (arg1_start, "signal", arg1_length) == 0)
5512     {
5513       error ("Catch of signal not yet implemented");
5514     }
5515   else if (strncmp (arg1_start, "catch", arg1_length) == 0)
5516     {
5517       catch_exception_command_1 (EX_EVENT_CATCH, arg1_end + 1, 
5518                                  tempflag, from_tty);
5519     }
5520   else if (strncmp (arg1_start, "throw", arg1_length) == 0)
5521     {
5522       catch_exception_command_1 (EX_EVENT_THROW, arg1_end + 1, 
5523                                  tempflag, from_tty);
5524     }
5525   else if (strncmp (arg1_start, "thread_start", arg1_length) == 0)
5526     {
5527       error ("Catch of thread_start not yet implemented");
5528     }
5529   else if (strncmp (arg1_start, "thread_exit", arg1_length) == 0)
5530     {
5531       error ("Catch of thread_exit not yet implemented");
5532     }
5533   else if (strncmp (arg1_start, "thread_join", arg1_length) == 0)
5534     {
5535       error ("Catch of thread_join not yet implemented");
5536     }
5537   else if (strncmp (arg1_start, "start", arg1_length) == 0)
5538     {
5539       error ("Catch of start not yet implemented");
5540     }
5541   else if (strncmp (arg1_start, "exit", arg1_length) == 0)
5542     {
5543       error ("Catch of exit not yet implemented");
5544     }
5545   else if (strncmp (arg1_start, "fork", arg1_length) == 0)
5546     {
5547 #if defined(CHILD_INSERT_FORK_CATCHPOINT)
5548       catch_fork_command_1 (catch_fork, arg1_end + 1, tempflag, from_tty);
5549 #else
5550       error ("Catch of fork not yet implemented");
5551 #endif
5552     }
5553   else if (strncmp (arg1_start, "vfork", arg1_length) == 0)
5554     {
5555 #if defined(CHILD_INSERT_VFORK_CATCHPOINT)
5556       catch_fork_command_1 (catch_vfork, arg1_end + 1, tempflag, from_tty);
5557 #else
5558       error ("Catch of vfork not yet implemented");
5559 #endif
5560     }
5561   else if (strncmp (arg1_start, "exec", arg1_length) == 0)
5562     {
5563 #if defined(CHILD_INSERT_EXEC_CATCHPOINT)
5564       catch_exec_command_1 (arg1_end + 1, tempflag, from_tty);
5565 #else
5566       error ("Catch of exec not yet implemented");
5567 #endif
5568     }
5569   else if (strncmp (arg1_start, "load", arg1_length) == 0)
5570     {
5571 #if defined(SOLIB_ADD)
5572       catch_load_command_1 (arg1_end + 1, tempflag, from_tty);
5573 #else
5574       error ("Catch of load not implemented");
5575 #endif
5576     }
5577   else if (strncmp (arg1_start, "unload", arg1_length) == 0)
5578     {
5579 #if defined(SOLIB_ADD)
5580       catch_unload_command_1 (arg1_end + 1, tempflag, from_tty);
5581 #else
5582       error ("Catch of load not implemented");
5583 #endif
5584     }
5585   else if (strncmp (arg1_start, "stop", arg1_length) == 0)
5586     {
5587       error ("Catch of stop not yet implemented");
5588     }
5589
5590   /* This doesn't appear to be an event name */
5591
5592   else
5593     {
5594       /* Pre-v.4.16 behaviour was to treat the argument
5595          as the name of an exception */
5596       /* catch_throw_command_1 (arg1_start, tempflag, from_tty); */
5597       /* Now this is not allowed */
5598       error ("Unknown event kind specified for catch");
5599
5600     }
5601 }
5602
5603 /* Used by the gui, could be made a worker for other things. */
5604
5605 struct breakpoint *
5606 set_breakpoint_sal (sal)
5607      struct symtab_and_line sal;
5608 {
5609   struct breakpoint *b;
5610   b = set_raw_breakpoint (sal);
5611   set_breakpoint_count (breakpoint_count + 1);
5612   b->number = breakpoint_count;
5613   b->type = bp_breakpoint;
5614   b->cond = 0;
5615   b->thread = -1;
5616   return b;
5617 }
5618
5619 #if 0
5620 /* These aren't used; I don't know what they were for.  */
5621 /* Disable breakpoints on all catch clauses described in ARGS.  */
5622 static void
5623 disable_catch (args)
5624      char *args;
5625 {
5626   /* Map the disable command to catch clauses described in ARGS.  */
5627 }
5628
5629 /* Enable breakpoints on all catch clauses described in ARGS.  */
5630 static void
5631 enable_catch (args)
5632      char *args;
5633 {
5634   /* Map the disable command to catch clauses described in ARGS.  */
5635 }
5636
5637 /* Delete breakpoints on all catch clauses in the active scope.  */
5638 static void
5639 delete_catch (args)
5640      char *args;
5641 {
5642   /* Map the delete command to catch clauses described in ARGS.  */
5643 }
5644 #endif /* 0 */
5645
5646 static void
5647 catch_command (arg, from_tty)
5648      char *arg;
5649      int from_tty;
5650 {
5651   catch_command_1 (arg, 0, from_tty);
5652 }
5653 \f
5654
5655 static void
5656 tcatch_command (arg, from_tty)
5657      char *arg;
5658      int from_tty;
5659 {
5660   catch_command_1 (arg, 1, from_tty);
5661 }
5662
5663
5664 static void
5665 clear_command (arg, from_tty)
5666      char *arg;
5667      int from_tty;
5668 {
5669   register struct breakpoint *b, *b1;
5670   int default_match;
5671   struct symtabs_and_lines sals;
5672   struct symtab_and_line sal;
5673   register struct breakpoint *found;
5674   int i;
5675
5676   if (arg)
5677     {
5678       sals = decode_line_spec (arg, 1);
5679       default_match = 0;
5680     }
5681   else
5682     {
5683       sals.sals = (struct symtab_and_line *)
5684         xmalloc (sizeof (struct symtab_and_line));
5685       INIT_SAL (&sal);          /* initialize to zeroes */
5686       sal.line = default_breakpoint_line;
5687       sal.symtab = default_breakpoint_symtab;
5688       sal.pc = default_breakpoint_address;
5689       if (sal.symtab == 0)
5690         error ("No source file specified.");
5691
5692       sals.sals[0] = sal;
5693       sals.nelts = 1;
5694
5695       default_match = 1;
5696     }
5697
5698   /* For each line spec given, delete bps which correspond
5699      to it.  We do this in two loops: the first loop looks at
5700      the initial bp(s) in the chain which should be deleted,
5701      the second goes down the rest of the chain looking ahead
5702      one so it can take those bps off the chain without messing
5703      up the chain. */
5704
5705
5706   for (i = 0; i < sals.nelts; i++)
5707     {
5708       /* If exact pc given, clear bpts at that pc.
5709          If line given (pc == 0), clear all bpts on specified line.
5710          If defaulting, clear all bpts on default line
5711          or at default pc.
5712
5713          defaulting    sal.pc != 0    tests to do
5714
5715          0              1             pc
5716          1              1             pc _and_ line
5717          0              0             line
5718          1              0             <can't happen> */
5719
5720       sal = sals.sals[i];
5721       found = (struct breakpoint *) 0;
5722
5723
5724       while (breakpoint_chain
5725       /* Why don't we check here that this is not
5726          a watchpoint, etc., as we do below?
5727          I can't make it fail, but don't know
5728          what's stopping the failure: a watchpoint
5729          of the same address as "sal.pc" should
5730          wind up being deleted. */
5731
5732              && (((sal.pc && (breakpoint_chain->address == sal.pc)) &&
5733                   (overlay_debugging == 0 ||
5734                    breakpoint_chain->section == sal.section))
5735                  || ((default_match || (0 == sal.pc))
5736                      && breakpoint_chain->source_file != NULL
5737                      && sal.symtab != NULL
5738               && STREQ (breakpoint_chain->source_file, sal.symtab->filename)
5739                      && breakpoint_chain->line_number == sal.line)))
5740
5741         {
5742           b1 = breakpoint_chain;
5743           breakpoint_chain = b1->next;
5744           b1->next = found;
5745           found = b1;
5746         }
5747
5748       ALL_BREAKPOINTS (b)
5749
5750         while (b->next
5751                && b->next->type != bp_none
5752                && b->next->type != bp_watchpoint
5753                && b->next->type != bp_hardware_watchpoint
5754                && b->next->type != bp_read_watchpoint
5755                && b->next->type != bp_access_watchpoint
5756                && (((sal.pc && (b->next->address == sal.pc)) &&
5757                     (overlay_debugging == 0 ||
5758                      b->next->section == sal.section))
5759                    || ((default_match || (0 == sal.pc))
5760                        && b->next->source_file != NULL
5761                        && sal.symtab != NULL
5762                        && STREQ (b->next->source_file, sal.symtab->filename)
5763                        && b->next->line_number == sal.line)))
5764
5765
5766         {
5767           b1 = b->next;
5768           b->next = b1->next;
5769           b1->next = found;
5770           found = b1;
5771         }
5772
5773       if (found == 0)
5774         {
5775           if (arg)
5776             error ("No breakpoint at %s.", arg);
5777           else
5778             error ("No breakpoint at this line.");
5779         }
5780
5781       if (found->next)
5782         from_tty = 1;           /* Always report if deleted more than one */
5783       if (from_tty)
5784         printf_unfiltered ("Deleted breakpoint%s ", found->next ? "s" : "");
5785       breakpoints_changed ();
5786       while (found)
5787         {
5788           if (from_tty)
5789             printf_unfiltered ("%d ", found->number);
5790           b1 = found->next;
5791           delete_breakpoint (found);
5792           found = b1;
5793         }
5794       if (from_tty)
5795         putchar_unfiltered ('\n');
5796     }
5797   free ((PTR) sals.sals);
5798 }
5799 \f
5800 /* Delete breakpoint in BS if they are `delete' breakpoints and
5801    all breakpoints that are marked for deletion, whether hit or not.
5802    This is called after any breakpoint is hit, or after errors.  */
5803
5804 void
5805 breakpoint_auto_delete (bs)
5806      bpstat bs;
5807 {
5808   struct breakpoint *b, *temp;
5809
5810   for (; bs; bs = bs->next)
5811     if (bs->breakpoint_at && bs->breakpoint_at->disposition == del
5812         && bs->stop)
5813       delete_breakpoint (bs->breakpoint_at);
5814
5815   ALL_BREAKPOINTS_SAFE (b, temp)
5816   {
5817     if (b->disposition == del_at_next_stop)
5818       delete_breakpoint (b);
5819   }
5820 }
5821
5822 /* Delete a breakpoint and clean up all traces of it in the data
5823    structures. */
5824
5825 void
5826 delete_breakpoint (bpt)
5827      struct breakpoint *bpt;
5828 {
5829   register struct breakpoint *b;
5830   register bpstat bs;
5831
5832   if (bpt == NULL)
5833     error ("Internal error (attempted to delete a NULL breakpoint)");
5834
5835
5836   /* Has this bp already been deleted?  This can happen because multiple
5837      lists can hold pointers to bp's.  bpstat lists are especial culprits.
5838
5839      One example of this happening is a watchpoint's scope bp.  When the
5840      scope bp triggers, we notice that the watchpoint is out of scope, and
5841      delete it.  We also delete its scope bp.  But the scope bp is marked
5842      "auto-deleting", and is already on a bpstat.  That bpstat is then
5843      checked for auto-deleting bp's, which are deleted.
5844
5845      A real solution to this problem might involve reference counts in bp's,
5846      and/or giving them pointers back to their referencing bpstat's, and
5847      teaching delete_breakpoint to only free a bp's storage when no more
5848      references were extent.  A cheaper bandaid was chosen. */
5849   if (bpt->type == bp_none)
5850     return;
5851
5852   if (delete_breakpoint_hook)
5853     delete_breakpoint_hook (bpt);
5854   breakpoint_delete_event (bpt->number);
5855
5856   if (bpt->inserted)
5857     remove_breakpoint (bpt, mark_uninserted);
5858
5859   if (breakpoint_chain == bpt)
5860     breakpoint_chain = bpt->next;
5861
5862   /* If we have callback-style exception catchpoints, don't go through
5863      the adjustments to the C++ runtime library etc. if the inferior
5864      isn't actually running.  target_enable_exception_callback for a
5865      null target ops vector gives an undesirable error message, so we
5866      check here and avoid it. Since currently (1997-09-17) only HP-UX aCC's
5867      exceptions are supported in this way, it's OK for now. FIXME */
5868   if (ep_is_exception_catchpoint (bpt) && target_has_execution)
5869     {
5870       static char message1[] = "Error in deleting catchpoint %d:\n";
5871       static char message[sizeof (message1) + 30];
5872       args_for_catchpoint_enable args;
5873
5874       /* Format possible error msg */
5875       sprintf (message, message1, bpt->number);
5876       args.kind = bpt->type == bp_catch_catch ? 
5877         EX_EVENT_CATCH : EX_EVENT_THROW;
5878       args.enable = 0;
5879       catch_errors (cover_target_enable_exception_callback, &args,
5880                     message, RETURN_MASK_ALL);
5881     }
5882
5883
5884   ALL_BREAKPOINTS (b)
5885     if (b->next == bpt)
5886     {
5887       b->next = bpt->next;
5888       break;
5889     }
5890
5891   /* Before turning off the visuals for the bp, check to see that
5892      there are no other bps at the same address. */
5893   if (tui_version)
5894     {
5895       int clearIt;
5896
5897       ALL_BREAKPOINTS (b)
5898       {
5899         clearIt = (b->address != bpt->address);
5900         if (!clearIt)
5901           break;
5902       }
5903
5904       if (clearIt)
5905         {
5906           TUIDO (((TuiOpaqueFuncPtr) tui_vAllSetHasBreakAt, bpt, 0));
5907           TUIDO (((TuiOpaqueFuncPtr) tuiUpdateAllExecInfos));
5908         }
5909     }
5910
5911   check_duplicates (bpt->address, bpt->section);
5912   /* If this breakpoint was inserted, and there is another breakpoint
5913      at the same address, we need to insert the other breakpoint.  */
5914   if (bpt->inserted
5915       && bpt->type != bp_hardware_watchpoint
5916       && bpt->type != bp_read_watchpoint
5917       && bpt->type != bp_access_watchpoint
5918       && bpt->type != bp_catch_fork
5919       && bpt->type != bp_catch_vfork
5920       && bpt->type != bp_catch_exec)
5921     {
5922       ALL_BREAKPOINTS (b)
5923         if (b->address == bpt->address
5924             && b->section == bpt->section
5925             && !b->duplicate
5926             && b->enable != disabled
5927             && b->enable != shlib_disabled
5928             && b->enable != call_disabled)
5929         {
5930           int val;
5931
5932           if (b->type == bp_hardware_breakpoint)
5933             val = target_insert_hw_breakpoint (b->address, b->shadow_contents);
5934           else
5935             val = target_insert_breakpoint (b->address, b->shadow_contents);
5936
5937           if (val != 0)
5938             {
5939               target_terminal_ours_for_output ();
5940               warning ("Cannot insert breakpoint %d:", b->number);
5941               memory_error (val, b->address);   /* which bombs us out */
5942             }
5943           else
5944             b->inserted = 1;
5945         }
5946     }
5947
5948   free_command_lines (&bpt->commands);
5949   if (bpt->cond)
5950     free (bpt->cond);
5951   if (bpt->cond_string != NULL)
5952     free (bpt->cond_string);
5953   if (bpt->addr_string != NULL)
5954     free (bpt->addr_string);
5955   if (bpt->exp != NULL)
5956     free (bpt->exp);
5957   if (bpt->exp_string != NULL)
5958     free (bpt->exp_string);
5959   if (bpt->val != NULL)
5960     value_free (bpt->val);
5961   if (bpt->source_file != NULL)
5962     free (bpt->source_file);
5963   if (bpt->dll_pathname != NULL)
5964     free (bpt->dll_pathname);
5965   if (bpt->triggered_dll_pathname != NULL)
5966     free (bpt->triggered_dll_pathname);
5967   if (bpt->exec_pathname != NULL)
5968     free (bpt->exec_pathname);
5969
5970   /* Be sure no bpstat's are pointing at it after it's been freed.  */
5971   /* FIXME, how can we find all bpstat's?
5972      We just check stop_bpstat for now.  */
5973   for (bs = stop_bpstat; bs; bs = bs->next)
5974     if (bs->breakpoint_at == bpt)
5975       {
5976         bs->breakpoint_at = NULL;
5977
5978         /* we'd call bpstat_clear_actions, but that free's stuff and due
5979            to the multiple pointers pointing to one item with no
5980            reference counts found anywhere through out the bpstat's (how
5981            do you spell fragile?), we don't want to free things twice --
5982            better a memory leak than a corrupt malloc pool! */
5983         bs->commands = NULL;
5984         bs->old_val = NULL;
5985       }
5986   /* On the chance that someone will soon try again to delete this same
5987      bp, we mark it as deleted before freeing its storage. */
5988   bpt->type = bp_none;
5989
5990   free ((PTR) bpt);
5991 }
5992
5993 void
5994 delete_command (arg, from_tty)
5995      char *arg;
5996      int from_tty;
5997 {
5998   struct breakpoint *b, *temp;
5999
6000   if (arg == 0)
6001     {
6002       int breaks_to_delete = 0;
6003
6004       /* Delete all breakpoints if no argument.
6005          Do not delete internal or call-dummy breakpoints, these
6006          have to be deleted with an explicit breakpoint number argument.  */
6007       ALL_BREAKPOINTS (b)
6008       {
6009         if (b->type != bp_call_dummy &&
6010             b->type != bp_shlib_event &&
6011             b->number >= 0)
6012           breaks_to_delete = 1;
6013       }
6014
6015       /* Ask user only if there are some breakpoints to delete.  */
6016       if (!from_tty
6017           || (breaks_to_delete && query ("Delete all breakpoints? ")))
6018         {
6019           ALL_BREAKPOINTS_SAFE (b, temp)
6020           {
6021             if (b->type != bp_call_dummy &&
6022                 b->type != bp_shlib_event &&
6023                 b->number >= 0)
6024               delete_breakpoint (b);
6025           }
6026         }
6027     }
6028   else
6029     map_breakpoint_numbers (arg, delete_breakpoint);
6030 }
6031
6032 /* Reset a breakpoint given it's struct breakpoint * BINT.
6033    The value we return ends up being the return value from catch_errors.
6034    Unused in this case.  */
6035
6036 static int
6037 breakpoint_re_set_one (bint)
6038      PTR bint;
6039 {
6040   /* get past catch_errs */
6041   struct breakpoint *b = (struct breakpoint *) bint;
6042   struct value *mark;
6043   int i;
6044   struct symtabs_and_lines sals;
6045   char *s;
6046   enum enable save_enable;
6047
6048   switch (b->type)
6049     {
6050     case bp_none:
6051       warning ("attempted to reset apparently deleted breakpoint #%d?",
6052                b->number);
6053       return 0;
6054     case bp_breakpoint:
6055     case bp_hardware_breakpoint:
6056     case bp_catch_load:
6057     case bp_catch_unload:
6058       if (b->addr_string == NULL)
6059         {
6060           /* Anything without a string can't be re-set. */
6061           delete_breakpoint (b);
6062           return 0;
6063         }
6064       /* In case we have a problem, disable this breakpoint.  We'll restore
6065          its status if we succeed.  */
6066       save_enable = b->enable;
6067       b->enable = disabled;
6068
6069       set_language (b->language);
6070       input_radix = b->input_radix;
6071       s = b->addr_string;
6072       sals = decode_line_1 (&s, 1, (struct symtab *) NULL, 0, (char ***) NULL);
6073       for (i = 0; i < sals.nelts; i++)
6074         {
6075           resolve_sal_pc (&sals.sals[i]);
6076
6077           /* Reparse conditions, they might contain references to the
6078              old symtab.  */
6079           if (b->cond_string != NULL)
6080             {
6081               s = b->cond_string;
6082               if (b->cond)
6083                 free ((PTR) b->cond);
6084               b->cond = parse_exp_1 (&s, block_for_pc (sals.sals[i].pc), 0);
6085             }
6086
6087           /* We need to re-set the breakpoint if the address changes... */
6088           if (b->address != sals.sals[i].pc
6089           /* ...or new and old breakpoints both have source files, and
6090              the source file name or the line number changes...  */
6091               || (b->source_file != NULL
6092                   && sals.sals[i].symtab != NULL
6093                   && (!STREQ (b->source_file, sals.sals[i].symtab->filename)
6094                       || b->line_number != sals.sals[i].line)
6095               )
6096           /* ...or we switch between having a source file and not having
6097              one.  */
6098               || ((b->source_file == NULL) != (sals.sals[i].symtab == NULL))
6099             )
6100             {
6101               if (b->source_file != NULL)
6102                 free (b->source_file);
6103               if (sals.sals[i].symtab == NULL)
6104                 b->source_file = NULL;
6105               else
6106                 b->source_file =
6107                   savestring (sals.sals[i].symtab->filename,
6108                               strlen (sals.sals[i].symtab->filename));
6109               b->line_number = sals.sals[i].line;
6110               b->address = sals.sals[i].pc;
6111
6112               /* Used to check for duplicates here, but that can
6113                  cause trouble, as it doesn't check for disable
6114                  breakpoints. */
6115
6116               mention (b);
6117
6118               /* Might be better to do this just once per breakpoint_re_set,
6119                  rather than once for every breakpoint.  */
6120               breakpoints_changed ();
6121             }
6122           b->section = sals.sals[i].section;
6123           b->enable = save_enable;      /* Restore it, this worked. */
6124
6125
6126           /* Now that this is re-enabled, check_duplicates
6127              can be used. */
6128           check_duplicates (b->address, b->section);
6129
6130         }
6131       free ((PTR) sals.sals);
6132       break;
6133
6134     case bp_watchpoint:
6135     case bp_hardware_watchpoint:
6136     case bp_read_watchpoint:
6137     case bp_access_watchpoint:
6138       innermost_block = NULL;
6139       /* The issue arises of what context to evaluate this in.  The
6140          same one as when it was set, but what does that mean when
6141          symbols have been re-read?  We could save the filename and
6142          functionname, but if the context is more local than that, the
6143          best we could do would be something like how many levels deep
6144          and which index at that particular level, but that's going to
6145          be less stable than filenames or function names.  */
6146
6147       /* So for now, just use a global context.  */
6148       if (b->exp)
6149         free ((PTR) b->exp);
6150       b->exp = parse_expression (b->exp_string);
6151       b->exp_valid_block = innermost_block;
6152       mark = value_mark ();
6153       if (b->val)
6154         value_free (b->val);
6155       b->val = evaluate_expression (b->exp);
6156       release_value (b->val);
6157       if (VALUE_LAZY (b->val))
6158         value_fetch_lazy (b->val);
6159
6160       if (b->cond_string != NULL)
6161         {
6162           s = b->cond_string;
6163           if (b->cond)
6164             free ((PTR) b->cond);
6165           b->cond = parse_exp_1 (&s, (struct block *) 0, 0);
6166         }
6167       if (b->enable == enabled)
6168         mention (b);
6169       value_free_to_mark (mark);
6170       break;
6171     case bp_catch_catch:
6172     case bp_catch_throw:
6173       break;
6174       /* We needn't really do anything to reset these, since the mask
6175          that requests them is unaffected by e.g., new libraries being
6176          loaded. */
6177     case bp_catch_fork:
6178     case bp_catch_vfork:
6179     case bp_catch_exec:
6180       break;
6181
6182     default:
6183       printf_filtered ("Deleting unknown breakpoint type %d\n", b->type);
6184       /* fall through */
6185       /* Delete longjmp breakpoints, they will be reset later by
6186          breakpoint_re_set.  */
6187     case bp_longjmp:
6188     case bp_longjmp_resume:
6189       delete_breakpoint (b);
6190       break;
6191
6192       /* This breakpoint is special, it's set up when the inferior
6193          starts and we really don't want to touch it.  */
6194     case bp_shlib_event:
6195
6196       /* Keep temporary breakpoints, which can be encountered when we step
6197          over a dlopen call and SOLIB_ADD is resetting the breakpoints.
6198          Otherwise these should have been blown away via the cleanup chain
6199          or by breakpoint_init_inferior when we rerun the executable.  */
6200     case bp_until:
6201     case bp_finish:
6202     case bp_watchpoint_scope:
6203     case bp_call_dummy:
6204     case bp_step_resume:
6205       break;
6206     }
6207
6208   return 0;
6209 }
6210
6211 /* Re-set all breakpoints after symbols have been re-loaded.  */
6212 void
6213 breakpoint_re_set ()
6214 {
6215   struct breakpoint *b, *temp;
6216   enum language save_language;
6217   int save_input_radix;
6218   static char message1[] = "Error in re-setting breakpoint %d:\n";
6219   char message[sizeof (message1) + 30 /* slop */ ];
6220
6221   save_language = current_language->la_language;
6222   save_input_radix = input_radix;
6223   ALL_BREAKPOINTS_SAFE (b, temp)
6224   {
6225     /* Format possible error msg */
6226     sprintf (message, message1, b->number);
6227     catch_errors (breakpoint_re_set_one, b, message, RETURN_MASK_ALL);
6228   }
6229   set_language (save_language);
6230   input_radix = save_input_radix;
6231
6232 #ifdef GET_LONGJMP_TARGET
6233   create_longjmp_breakpoint ("longjmp");
6234   create_longjmp_breakpoint ("_longjmp");
6235   create_longjmp_breakpoint ("siglongjmp");
6236   create_longjmp_breakpoint ("_siglongjmp");
6237   create_longjmp_breakpoint (NULL);
6238 #endif
6239
6240 #if 0
6241   /* Took this out (temporarily at least), since it produces an extra 
6242      blank line at startup. This messes up the gdbtests. -PB */
6243   /* Blank line to finish off all those mention() messages we just printed.  */
6244   printf_filtered ("\n");
6245 #endif
6246 }
6247 \f
6248 /* Set ignore-count of breakpoint number BPTNUM to COUNT.
6249    If from_tty is nonzero, it prints a message to that effect,
6250    which ends with a period (no newline).  */
6251
6252 /* Reset the thread number of this breakpoint:
6253
6254    - If the breakpoint is for all threads, leave it as-is.
6255    - Else, reset it to the current thread for inferior_pid. */
6256 void
6257 breakpoint_re_set_thread (b)
6258      struct breakpoint *b;
6259 {
6260   if (b->thread != -1)
6261     {
6262       if (in_thread_list (inferior_pid))
6263         b->thread = pid_to_thread_id (inferior_pid);
6264     }
6265 }
6266
6267 void
6268 set_ignore_count (bptnum, count, from_tty)
6269      int bptnum, count, from_tty;
6270 {
6271   register struct breakpoint *b;
6272
6273   if (count < 0)
6274     count = 0;
6275
6276   ALL_BREAKPOINTS (b)
6277     if (b->number == bptnum)
6278     {
6279       b->ignore_count = count;
6280       if (!from_tty)
6281         return;
6282       else if (count == 0)
6283         printf_filtered ("Will stop next time breakpoint %d is reached.",
6284                          bptnum);
6285       else if (count == 1)
6286         printf_filtered ("Will ignore next crossing of breakpoint %d.",
6287                          bptnum);
6288       else
6289         printf_filtered ("Will ignore next %d crossings of breakpoint %d.",
6290                          count, bptnum);
6291       breakpoints_changed ();
6292       return;
6293     }
6294
6295   error ("No breakpoint number %d.", bptnum);
6296 }
6297
6298 /* Clear the ignore counts of all breakpoints.  */
6299 void
6300 breakpoint_clear_ignore_counts ()
6301 {
6302   struct breakpoint *b;
6303
6304   ALL_BREAKPOINTS (b)
6305     b->ignore_count = 0;
6306 }
6307
6308 /* Command to set ignore-count of breakpoint N to COUNT.  */
6309
6310 static void
6311 ignore_command (args, from_tty)
6312      char *args;
6313      int from_tty;
6314 {
6315   char *p = args;
6316   register int num;
6317
6318   if (p == 0)
6319     error_no_arg ("a breakpoint number");
6320
6321   num = get_number (&p);
6322
6323   if (*p == 0)
6324     error ("Second argument (specified ignore-count) is missing.");
6325
6326   set_ignore_count (num,
6327                     longest_to_int (value_as_long (parse_and_eval (p))),
6328                     from_tty);
6329   printf_filtered ("\n");
6330   breakpoints_changed ();
6331 }
6332 \f
6333 /* Call FUNCTION on each of the breakpoints
6334    whose numbers are given in ARGS.  */
6335
6336 static void
6337 map_breakpoint_numbers (args, function)
6338      char *args;
6339      void (*function) PARAMS ((struct breakpoint *));
6340 {
6341   register char *p = args;
6342   char *p1;
6343   register int num;
6344   register struct breakpoint *b;
6345
6346   if (p == 0)
6347     error_no_arg ("one or more breakpoint numbers");
6348
6349   while (*p)
6350     {
6351       p1 = p;
6352
6353       num = get_number (&p1);
6354
6355       ALL_BREAKPOINTS (b)
6356         if (b->number == num)
6357         {
6358           struct breakpoint *related_breakpoint = b->related_breakpoint;
6359           function (b);
6360           if (related_breakpoint)
6361             function (related_breakpoint);
6362           goto win;
6363         }
6364       printf_unfiltered ("No breakpoint number %d.\n", num);
6365     win:
6366       p = p1;
6367     }
6368 }
6369
6370 void
6371 disable_breakpoint (bpt)
6372      struct breakpoint *bpt;
6373 {
6374   /* Never disable a watchpoint scope breakpoint; we want to
6375      hit them when we leave scope so we can delete both the
6376      watchpoint and its scope breakpoint at that time.  */
6377   if (bpt->type == bp_watchpoint_scope)
6378     return;
6379
6380   bpt->enable = disabled;
6381
6382   check_duplicates (bpt->address, bpt->section);
6383
6384   if (modify_breakpoint_hook)
6385     modify_breakpoint_hook (bpt);
6386   breakpoint_modify_event (bpt->number);
6387 }
6388
6389 /* ARGSUSED */
6390 static void
6391 disable_command (args, from_tty)
6392      char *args;
6393      int from_tty;
6394 {
6395   register struct breakpoint *bpt;
6396   if (args == 0)
6397     ALL_BREAKPOINTS (bpt)
6398       switch (bpt->type)
6399       {
6400       case bp_none:
6401         warning ("attempted to disable apparently deleted breakpoint #%d?",
6402                  bpt->number);
6403         continue;
6404       case bp_breakpoint:
6405       case bp_catch_load:
6406       case bp_catch_unload:
6407       case bp_catch_fork:
6408       case bp_catch_vfork:
6409       case bp_catch_exec:
6410       case bp_catch_catch:
6411       case bp_catch_throw:
6412       case bp_hardware_breakpoint:
6413       case bp_watchpoint:
6414       case bp_hardware_watchpoint:
6415       case bp_read_watchpoint:
6416       case bp_access_watchpoint:
6417         disable_breakpoint (bpt);
6418       default:
6419         continue;
6420       }
6421   else
6422     map_breakpoint_numbers (args, disable_breakpoint);
6423 }
6424
6425 static void
6426 do_enable_breakpoint (bpt, disposition)
6427      struct breakpoint *bpt;
6428      enum bpdisp disposition;
6429 {
6430   struct frame_info *save_selected_frame = NULL;
6431   int save_selected_frame_level = -1;
6432   int target_resources_ok, other_type_used;
6433   struct value *mark;
6434
6435   if (bpt->type == bp_hardware_breakpoint)
6436     {
6437       int i;
6438       i = hw_breakpoint_used_count ();
6439       target_resources_ok = 
6440         TARGET_CAN_USE_HARDWARE_WATCHPOINT (bp_hardware_breakpoint, 
6441                                             i + 1, 0);
6442       if (target_resources_ok == 0)
6443         error ("No hardware breakpoint support in the target.");
6444       else if (target_resources_ok < 0)
6445         error ("Hardware breakpoints used exceeds limit.");
6446     }
6447
6448   bpt->enable = enabled;
6449   bpt->disposition = disposition;
6450   check_duplicates (bpt->address, bpt->section);
6451   breakpoints_changed ();
6452
6453   if (bpt->type == bp_watchpoint || 
6454       bpt->type == bp_hardware_watchpoint ||
6455       bpt->type == bp_read_watchpoint || 
6456       bpt->type == bp_access_watchpoint)
6457     {
6458       if (bpt->exp_valid_block != NULL)
6459         {
6460           struct frame_info *fr =
6461
6462           /* Ensure that we have the current frame.  Else, this
6463              next query may pessimistically be answered as, "No,
6464              not within current scope". */
6465           get_current_frame ();
6466           fr = find_frame_addr_in_frame_chain (bpt->watchpoint_frame);
6467           if (fr == NULL)
6468             {
6469               printf_filtered ("\
6470 Cannot enable watchpoint %d because the block in which its expression\n\
6471 is valid is not currently in scope.\n", bpt->number);
6472               bpt->enable = disabled;
6473               return;
6474             }
6475
6476           save_selected_frame = selected_frame;
6477           save_selected_frame_level = selected_frame_level;
6478           select_frame (fr, -1);
6479         }
6480
6481       value_free (bpt->val);
6482       mark = value_mark ();
6483       bpt->val = evaluate_expression (bpt->exp);
6484       release_value (bpt->val);
6485       if (VALUE_LAZY (bpt->val))
6486         value_fetch_lazy (bpt->val);
6487
6488       if (bpt->type == bp_hardware_watchpoint ||
6489           bpt->type == bp_read_watchpoint ||
6490           bpt->type == bp_access_watchpoint)
6491         {
6492           int i = hw_watchpoint_used_count (bpt->type, &other_type_used);
6493           int mem_cnt = can_use_hardware_watchpoint (bpt->val);
6494
6495           /* Hack around 'unused var' error for some targets here */
6496           (void) mem_cnt, i;
6497           target_resources_ok = TARGET_CAN_USE_HARDWARE_WATCHPOINT (
6498                                    bpt->type, i + mem_cnt, other_type_used);
6499           /* we can consider of type is bp_hardware_watchpoint, convert to 
6500              bp_watchpoint in the following condition */
6501           if (target_resources_ok < 0)
6502             {
6503               printf_filtered ("\
6504 Cannot enable watchpoint %d because target watch resources\n\
6505 have been allocated for other watchpoints.\n", bpt->number);
6506               bpt->enable = disabled;
6507               value_free_to_mark (mark);
6508               return;
6509             }
6510         }
6511
6512       if (save_selected_frame_level >= 0)
6513         select_and_print_frame (save_selected_frame,
6514                                 save_selected_frame_level);
6515       value_free_to_mark (mark);
6516     }
6517   if (modify_breakpoint_hook)
6518     modify_breakpoint_hook (bpt);
6519   breakpoint_modify_event (bpt->number);
6520 }
6521
6522 void
6523 enable_breakpoint (bpt)
6524      struct breakpoint *bpt;
6525 {
6526   do_enable_breakpoint (bpt, bpt->disposition);
6527 }
6528
6529 /* The enable command enables the specified breakpoints (or all defined
6530    breakpoints) so they once again become (or continue to be) effective
6531    in stopping the inferior. */
6532
6533 /* ARGSUSED */
6534 static void
6535 enable_command (args, from_tty)
6536      char *args;
6537      int from_tty;
6538 {
6539   register struct breakpoint *bpt;
6540   if (args == 0)
6541     ALL_BREAKPOINTS (bpt)
6542       switch (bpt->type)
6543       {
6544       case bp_none:
6545         warning ("attempted to enable apparently deleted breakpoint #%d?",
6546                  bpt->number);
6547         continue;
6548       case bp_breakpoint:
6549       case bp_catch_load:
6550       case bp_catch_unload:
6551       case bp_catch_fork:
6552       case bp_catch_vfork:
6553       case bp_catch_exec:
6554       case bp_catch_catch:
6555       case bp_catch_throw:
6556       case bp_hardware_breakpoint:
6557       case bp_watchpoint:
6558       case bp_hardware_watchpoint:
6559       case bp_read_watchpoint:
6560       case bp_access_watchpoint:
6561         enable_breakpoint (bpt);
6562       default:
6563         continue;
6564       }
6565   else
6566     map_breakpoint_numbers (args, enable_breakpoint);
6567 }
6568
6569 static void
6570 enable_once_breakpoint (bpt)
6571      struct breakpoint *bpt;
6572 {
6573   do_enable_breakpoint (bpt, disable);
6574 }
6575
6576 /* ARGSUSED */
6577 static void
6578 enable_once_command (args, from_tty)
6579      char *args;
6580      int from_tty;
6581 {
6582   map_breakpoint_numbers (args, enable_once_breakpoint);
6583 }
6584
6585 static void
6586 enable_delete_breakpoint (bpt)
6587      struct breakpoint *bpt;
6588 {
6589   do_enable_breakpoint (bpt, del);
6590 }
6591
6592 /* ARGSUSED */
6593 static void
6594 enable_delete_command (args, from_tty)
6595      char *args;
6596      int from_tty;
6597 {
6598   map_breakpoint_numbers (args, enable_delete_breakpoint);
6599 }
6600 \f
6601 /* Use default_breakpoint_'s, or nothing if they aren't valid.  */
6602
6603 struct symtabs_and_lines
6604 decode_line_spec_1 (string, funfirstline)
6605      char *string;
6606      int funfirstline;
6607 {
6608   struct symtabs_and_lines sals;
6609   if (string == 0)
6610     error ("Empty line specification.");
6611   if (default_breakpoint_valid)
6612     sals = decode_line_1 (&string, funfirstline,
6613                           default_breakpoint_symtab,
6614                           default_breakpoint_line,
6615                           (char ***) NULL);
6616   else
6617     sals = decode_line_1 (&string, funfirstline,
6618                           (struct symtab *) NULL, 0, (char ***) NULL);
6619   if (*string)
6620     error ("Junk at end of line specification: %s", string);
6621   return sals;
6622 }
6623 \f
6624 void
6625 _initialize_breakpoint ()
6626 {
6627   struct cmd_list_element *c;
6628
6629   breakpoint_chain = 0;
6630   /* Don't bother to call set_breakpoint_count.  $bpnum isn't useful
6631      before a breakpoint is set.  */
6632   breakpoint_count = 0;
6633
6634   add_com ("ignore", class_breakpoint, ignore_command,
6635            "Set ignore-count of breakpoint number N to COUNT.\n\
6636 Usage is `ignore N COUNT'.");
6637   if (xdb_commands)
6638     add_com_alias ("bc", "ignore", class_breakpoint, 1);
6639
6640   add_com ("commands", class_breakpoint, commands_command,
6641            "Set commands to be executed when a breakpoint is hit.\n\
6642 Give breakpoint number as argument after \"commands\".\n\
6643 With no argument, the targeted breakpoint is the last one set.\n\
6644 The commands themselves follow starting on the next line.\n\
6645 Type a line containing \"end\" to indicate the end of them.\n\
6646 Give \"silent\" as the first line to make the breakpoint silent;\n\
6647 then no output is printed when it is hit, except what the commands print.");
6648
6649   add_com ("condition", class_breakpoint, condition_command,
6650            "Specify breakpoint number N to break only if COND is true.\n\
6651 Usage is `condition N COND', where N is an integer and COND is an\n\
6652 expression to be evaluated whenever breakpoint N is reached.  ");
6653
6654   add_com ("tbreak", class_breakpoint, tbreak_command,
6655            "Set a temporary breakpoint.  Args like \"break\" command.\n\
6656 Like \"break\" except the breakpoint is only temporary,\n\
6657 so it will be deleted when hit.  Equivalent to \"break\" followed\n\
6658 by using \"enable delete\" on the breakpoint number.");
6659   add_com ("txbreak", class_breakpoint, tbreak_at_finish_command,
6660            "Set temporary breakpoint at procedure exit.  Either there should\n\
6661 be no argument or the argument must be a depth.\n");
6662
6663   add_com ("hbreak", class_breakpoint, hbreak_command,
6664            "Set a hardware assisted  breakpoint. Args like \"break\" command.\n\
6665 Like \"break\" except the breakpoint requires hardware support,\n\
6666 some target hardware may not have this support.");
6667
6668   add_com ("thbreak", class_breakpoint, thbreak_command,
6669            "Set a temporary hardware assisted breakpoint. Args like \"break\" command.\n\
6670 Like \"hbreak\" except the breakpoint is only temporary,\n\
6671 so it will be deleted when hit.");
6672
6673   add_prefix_cmd ("enable", class_breakpoint, enable_command,
6674                   "Enable some breakpoints.\n\
6675 Give breakpoint numbers (separated by spaces) as arguments.\n\
6676 With no subcommand, breakpoints are enabled until you command otherwise.\n\
6677 This is used to cancel the effect of the \"disable\" command.\n\
6678 With a subcommand you can enable temporarily.",
6679                   &enablelist, "enable ", 1, &cmdlist);
6680   if (xdb_commands)
6681     add_com ("ab", class_breakpoint, enable_command,
6682              "Enable some breakpoints.\n\
6683 Give breakpoint numbers (separated by spaces) as arguments.\n\
6684 With no subcommand, breakpoints are enabled until you command otherwise.\n\
6685 This is used to cancel the effect of the \"disable\" command.\n\
6686 With a subcommand you can enable temporarily.");
6687
6688   add_com_alias ("en", "enable", class_breakpoint, 1);
6689
6690   add_abbrev_prefix_cmd ("breakpoints", class_breakpoint, enable_command,
6691                          "Enable some breakpoints.\n\
6692 Give breakpoint numbers (separated by spaces) as arguments.\n\
6693 This is used to cancel the effect of the \"disable\" command.\n\
6694 May be abbreviated to simply \"enable\".\n",
6695                    &enablebreaklist, "enable breakpoints ", 1, &enablelist);
6696
6697   add_cmd ("once", no_class, enable_once_command,
6698            "Enable breakpoints for one hit.  Give breakpoint numbers.\n\
6699 If a breakpoint is hit while enabled in this fashion, it becomes disabled.",
6700            &enablebreaklist);
6701
6702   add_cmd ("delete", no_class, enable_delete_command,
6703            "Enable breakpoints and delete when hit.  Give breakpoint numbers.\n\
6704 If a breakpoint is hit while enabled in this fashion, it is deleted.",
6705            &enablebreaklist);
6706
6707   add_cmd ("delete", no_class, enable_delete_command,
6708            "Enable breakpoints and delete when hit.  Give breakpoint numbers.\n\
6709 If a breakpoint is hit while enabled in this fashion, it is deleted.",
6710            &enablelist);
6711
6712   add_cmd ("once", no_class, enable_once_command,
6713            "Enable breakpoints for one hit.  Give breakpoint numbers.\n\
6714 If a breakpoint is hit while enabled in this fashion, it becomes disabled.",
6715            &enablelist);
6716
6717   add_prefix_cmd ("disable", class_breakpoint, disable_command,
6718                   "Disable some breakpoints.\n\
6719 Arguments are breakpoint numbers with spaces in between.\n\
6720 To disable all breakpoints, give no argument.\n\
6721 A disabled breakpoint is not forgotten, but has no effect until reenabled.",
6722                   &disablelist, "disable ", 1, &cmdlist);
6723   add_com_alias ("dis", "disable", class_breakpoint, 1);
6724   add_com_alias ("disa", "disable", class_breakpoint, 1);
6725   if (xdb_commands)
6726     add_com ("sb", class_breakpoint, disable_command,
6727              "Disable some breakpoints.\n\
6728 Arguments are breakpoint numbers with spaces in between.\n\
6729 To disable all breakpoints, give no argument.\n\
6730 A disabled breakpoint is not forgotten, but has no effect until reenabled.");
6731
6732   add_cmd ("breakpoints", class_alias, disable_command,
6733            "Disable some breakpoints.\n\
6734 Arguments are breakpoint numbers with spaces in between.\n\
6735 To disable all breakpoints, give no argument.\n\
6736 A disabled breakpoint is not forgotten, but has no effect until reenabled.\n\
6737 This command may be abbreviated \"disable\".",
6738            &disablelist);
6739
6740   add_prefix_cmd ("delete", class_breakpoint, delete_command,
6741                   "Delete some breakpoints or auto-display expressions.\n\
6742 Arguments are breakpoint numbers with spaces in between.\n\
6743 To delete all breakpoints, give no argument.\n\
6744 \n\
6745 Also a prefix command for deletion of other GDB objects.\n\
6746 The \"unset\" command is also an alias for \"delete\".",
6747                   &deletelist, "delete ", 1, &cmdlist);
6748   add_com_alias ("d", "delete", class_breakpoint, 1);
6749   if (xdb_commands)
6750     add_com ("db", class_breakpoint, delete_command,
6751              "Delete some breakpoints.\n\
6752 Arguments are breakpoint numbers with spaces in between.\n\
6753 To delete all breakpoints, give no argument.\n");
6754
6755   add_cmd ("breakpoints", class_alias, delete_command,
6756            "Delete some breakpoints or auto-display expressions.\n\
6757 Arguments are breakpoint numbers with spaces in between.\n\
6758 To delete all breakpoints, give no argument.\n\
6759 This command may be abbreviated \"delete\".",
6760            &deletelist);
6761
6762   add_com ("clear", class_breakpoint, clear_command,
6763            concat ("Clear breakpoint at specified line or function.\n\
6764 Argument may be line number, function name, or \"*\" and an address.\n\
6765 If line number is specified, all breakpoints in that line are cleared.\n\
6766 If function is specified, breakpoints at beginning of function are cleared.\n\
6767 If an address is specified, breakpoints at that address are cleared.\n\n",
6768                    "With no argument, clears all breakpoints in the line that the selected frame\n\
6769 is executing in.\n\
6770 \n\
6771 See also the \"delete\" command which clears breakpoints by number.", NULL));
6772
6773   add_com ("break", class_breakpoint, break_command,
6774            concat ("Set breakpoint at specified line or function.\n\
6775 Argument may be line number, function name, or \"*\" and an address.\n\
6776 If line number is specified, break at start of code for that line.\n\
6777 If function is specified, break at start of code for that function.\n\
6778 If an address is specified, break at that exact address.\n",
6779                    "With no arg, uses current execution address of selected stack frame.\n\
6780 This is useful for breaking on return to a stack frame.\n\
6781 \n\
6782 Multiple breakpoints at one place are permitted, and useful if conditional.\n\
6783 \n\
6784 Do \"help breakpoints\" for info on other commands dealing with breakpoints.", NULL));
6785   add_com_alias ("b", "break", class_run, 1);
6786   add_com_alias ("br", "break", class_run, 1);
6787   add_com_alias ("bre", "break", class_run, 1);
6788   add_com_alias ("brea", "break", class_run, 1);
6789
6790   add_com ("xbreak", class_breakpoint, break_at_finish_command,
6791            concat ("Set breakpoint at procedure exit. \n\
6792 Argument may be function name, or \"*\" and an address.\n\
6793 If function is specified, break at end of code for that function.\n\
6794 If an address is specified, break at the end of the function that contains \n\
6795 that exact address.\n",
6796                    "With no arg, uses current execution address of selected stack frame.\n\
6797 This is useful for breaking on return to a stack frame.\n\
6798 \n\
6799 Multiple breakpoints at one place are permitted, and useful if conditional.\n\
6800 \n\
6801 Do \"help breakpoints\" for info on other commands dealing with breakpoints.", NULL));
6802   add_com_alias ("xb", "xbreak", class_breakpoint, 1);
6803   add_com_alias ("xbr", "xbreak", class_breakpoint, 1);
6804   add_com_alias ("xbre", "xbreak", class_breakpoint, 1);
6805   add_com_alias ("xbrea", "xbreak", class_breakpoint, 1);
6806
6807   if (xdb_commands)
6808     {
6809       add_com_alias ("ba", "break", class_breakpoint, 1);
6810       add_com_alias ("bu", "ubreak", class_breakpoint, 1);
6811       add_com ("bx", class_breakpoint, break_at_finish_at_depth_command,
6812                "Set breakpoint at procedure exit.  Either there should\n\
6813 be no argument or the argument must be a depth.\n");
6814     }
6815
6816   if (dbx_commands)
6817     {
6818       add_abbrev_prefix_cmd ("stop", class_breakpoint, stop_command,
6819         "Break in function/address or break at a line in the current file.",
6820                              &stoplist, "stop ", 1, &cmdlist);
6821       add_cmd ("in", class_breakpoint, stopin_command,
6822                "Break in function or address.\n", &stoplist);
6823       add_cmd ("at", class_breakpoint, stopat_command,
6824                "Break at a line in the current file.\n", &stoplist);
6825       add_com ("status", class_info, breakpoints_info,
6826                concat ("Status of user-settable breakpoints, or breakpoint number NUMBER.\n\
6827 The \"Type\" column indicates one of:\n\
6828 \tbreakpoint     - normal breakpoint\n\
6829 \twatchpoint     - watchpoint\n\
6830 The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
6831 the disposition of the breakpoint after it gets hit.  \"dis\" means that the\n\
6832 breakpoint will be disabled.  The \"Address\" and \"What\" columns indicate the\n\
6833 address and file/line number respectively.\n\n",
6834                        "Convenience variable \"$_\" and default examine address for \"x\"\n\
6835 are set to the address of the last breakpoint listed.\n\n\
6836 Convenience variable \"$bpnum\" contains the number of the last\n\
6837 breakpoint set.", NULL));
6838     }
6839
6840   add_info ("breakpoints", breakpoints_info,
6841             concat ("Status of user-settable breakpoints, or breakpoint number NUMBER.\n\
6842 The \"Type\" column indicates one of:\n\
6843 \tbreakpoint     - normal breakpoint\n\
6844 \twatchpoint     - watchpoint\n\
6845 The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
6846 the disposition of the breakpoint after it gets hit.  \"dis\" means that the\n\
6847 breakpoint will be disabled.  The \"Address\" and \"What\" columns indicate the\n\
6848 address and file/line number respectively.\n\n",
6849                     "Convenience variable \"$_\" and default examine address for \"x\"\n\
6850 are set to the address of the last breakpoint listed.\n\n\
6851 Convenience variable \"$bpnum\" contains the number of the last\n\
6852 breakpoint set.", NULL));
6853
6854   if (xdb_commands)
6855     add_com ("lb", class_breakpoint, breakpoints_info,
6856              concat ("Status of user-settable breakpoints, or breakpoint number NUMBER.\n\
6857 The \"Type\" column indicates one of:\n\
6858 \tbreakpoint     - normal breakpoint\n\
6859 \twatchpoint     - watchpoint\n\
6860 The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
6861 the disposition of the breakpoint after it gets hit.  \"dis\" means that the\n\
6862 breakpoint will be disabled.  The \"Address\" and \"What\" columns indicate the\n\
6863 address and file/line number respectively.\n\n",
6864                      "Convenience variable \"$_\" and default examine address for \"x\"\n\
6865 are set to the address of the last breakpoint listed.\n\n\
6866 Convenience variable \"$bpnum\" contains the number of the last\n\
6867 breakpoint set.", NULL));
6868
6869   add_cmd ("breakpoints", class_maintenance, maintenance_info_breakpoints,
6870            concat ("Status of all breakpoints, or breakpoint number NUMBER.\n\
6871 The \"Type\" column indicates one of:\n\
6872 \tbreakpoint     - normal breakpoint\n\
6873 \twatchpoint     - watchpoint\n\
6874 \tlongjmp        - internal breakpoint used to step through longjmp()\n\
6875 \tlongjmp resume - internal breakpoint at the target of longjmp()\n\
6876 \tuntil          - internal breakpoint used by the \"until\" command\n\
6877 \tfinish         - internal breakpoint used by the \"finish\" command\n",
6878                    "The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
6879 the disposition of the breakpoint after it gets hit.  \"dis\" means that the\n\
6880 breakpoint will be disabled.  The \"Address\" and \"What\" columns indicate the\n\
6881 address and file/line number respectively.\n\n",
6882                    "Convenience variable \"$_\" and default examine address for \"x\"\n\
6883 are set to the address of the last breakpoint listed.\n\n\
6884 Convenience variable \"$bpnum\" contains the number of the last\n\
6885 breakpoint set.", NULL),
6886            &maintenanceinfolist);
6887
6888   add_com ("catch", class_breakpoint, catch_command,
6889            "Set catchpoints to catch events.\n\
6890 Raised signals may be caught:\n\
6891 \tcatch signal              - all signals\n\
6892 \tcatch signal <signame>    - a particular signal\n\
6893 Raised exceptions may be caught:\n\
6894 \tcatch throw               - all exceptions, when thrown\n\
6895 \tcatch throw <exceptname>  - a particular exception, when thrown\n\
6896 \tcatch catch               - all exceptions, when caught\n\
6897 \tcatch catch <exceptname>  - a particular exception, when caught\n\
6898 Thread or process events may be caught:\n\
6899 \tcatch thread_start        - any threads, just after creation\n\
6900 \tcatch thread_exit         - any threads, just before expiration\n\
6901 \tcatch thread_join         - any threads, just after joins\n\
6902 Process events may be caught:\n\
6903 \tcatch start               - any processes, just after creation\n\
6904 \tcatch exit                - any processes, just before expiration\n\
6905 \tcatch fork                - calls to fork()\n\
6906 \tcatch vfork               - calls to vfork()\n\
6907 \tcatch exec                - calls to exec()\n\
6908 Dynamically-linked library events may be caught:\n\
6909 \tcatch load                - loads of any library\n\
6910 \tcatch load <libname>      - loads of a particular library\n\
6911 \tcatch unload              - unloads of any library\n\
6912 \tcatch unload <libname>    - unloads of a particular library\n\
6913 The act of your program's execution stopping may also be caught:\n\
6914 \tcatch stop\n\n\
6915 C++ exceptions may be caught:\n\
6916 \tcatch throw               - all exceptions, when thrown\n\
6917 \tcatch catch               - all exceptions, when caught\n\
6918 \n\
6919 Do \"help set follow-fork-mode\" for info on debugging your program\n\
6920 after a fork or vfork is caught.\n\n\
6921 Do \"help breakpoints\" for info on other commands dealing with breakpoints.");
6922
6923   add_com ("tcatch", class_breakpoint, tcatch_command,
6924            "Set temporary catchpoints to catch events.\n\
6925 Args like \"catch\" command.\n\
6926 Like \"catch\" except the catchpoint is only temporary,\n\
6927 so it will be deleted when hit.  Equivalent to \"catch\" followed\n\
6928 by using \"enable delete\" on the catchpoint number.");
6929
6930   add_com ("watch", class_breakpoint, watch_command,
6931            "Set a watchpoint for an expression.\n\
6932 A watchpoint stops execution of your program whenever the value of\n\
6933 an expression changes.");
6934
6935   add_com ("rwatch", class_breakpoint, rwatch_command,
6936            "Set a read watchpoint for an expression.\n\
6937 A watchpoint stops execution of your program whenever the value of\n\
6938 an expression is read.");
6939
6940   add_com ("awatch", class_breakpoint, awatch_command,
6941            "Set a watchpoint for an expression.\n\
6942 A watchpoint stops execution of your program whenever the value of\n\
6943 an expression is either read or written.");
6944
6945   add_info ("watchpoints", breakpoints_info,
6946             "Synonym for ``info breakpoints''.");
6947
6948
6949   c = add_set_cmd ("can-use-hw-watchpoints", class_support, var_zinteger,
6950                    (char *) &can_use_hw_watchpoints,
6951                    "Set debugger's willingness to use watchpoint hardware.\n\
6952 If zero, gdb will not use hardware for new watchpoints, even if\n\
6953 such is available.  (However, any hardware watchpoints that were\n\
6954 created before setting this to nonzero, will continue to use watchpoint\n\
6955 hardware.)",
6956                    &setlist);
6957   add_show_from_set (c, &showlist);
6958
6959   can_use_hw_watchpoints = 1;
6960 }