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