import gdb-1999-09-21
[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 - DECR_PC_AFTER_BREAK;
2159
2160   ALL_BREAKPOINTS_SAFE (b, temp)
2161   {
2162     if (b->enable == disabled
2163         || b->enable == shlib_disabled
2164         || b->enable == call_disabled)
2165       continue;
2166
2167     if (b->type != bp_watchpoint
2168         && b->type != bp_hardware_watchpoint
2169         && b->type != bp_read_watchpoint
2170         && b->type != bp_access_watchpoint
2171         && b->type != bp_hardware_breakpoint
2172         && b->type != bp_catch_fork
2173         && b->type != bp_catch_vfork
2174         && b->type != bp_catch_exec
2175         && b->type != bp_catch_catch
2176         && b->type != bp_catch_throw)   /* a non-watchpoint bp */
2177       if (b->address != bp_addr ||      /* address doesn't match or */
2178           (overlay_debugging && /* overlay doesn't match */
2179            section_is_overlay (b->section) &&
2180            !section_is_mapped (b->section)))
2181         continue;
2182
2183     if (b->type == bp_hardware_breakpoint
2184         && b->address != (*pc - DECR_PC_AFTER_HW_BREAK))
2185       continue;
2186
2187     if (b->type != bp_watchpoint
2188         && b->type != bp_hardware_watchpoint
2189         && b->type != bp_read_watchpoint
2190         && b->type != bp_access_watchpoint
2191         && not_a_breakpoint)
2192       continue;
2193
2194     /* Is this a catchpoint of a load or unload?  If so, did we
2195        get a load or unload of the specified library?  If not,
2196        ignore it. */
2197     if ((b->type == bp_catch_load)
2198 #if defined(SOLIB_HAVE_LOAD_EVENT)
2199         && (!SOLIB_HAVE_LOAD_EVENT (inferior_pid)
2200             || ((b->dll_pathname != NULL)
2201                 && (strcmp (b->dll_pathname, 
2202                             SOLIB_LOADED_LIBRARY_PATHNAME (inferior_pid)) 
2203                     != 0)))
2204 #endif
2205       )
2206       continue;
2207
2208     if ((b->type == bp_catch_unload)
2209 #if defined(SOLIB_HAVE_UNLOAD_EVENT)
2210         && (!SOLIB_HAVE_UNLOAD_EVENT (inferior_pid)
2211             || ((b->dll_pathname != NULL)
2212                 && (strcmp (b->dll_pathname, 
2213                             SOLIB_UNLOADED_LIBRARY_PATHNAME (inferior_pid)) 
2214                     != 0)))
2215 #endif
2216       )
2217       continue;
2218
2219     if ((b->type == bp_catch_fork)
2220         && !target_has_forked (inferior_pid, &b->forked_inferior_pid))
2221       continue;
2222
2223     if ((b->type == bp_catch_vfork)
2224         && !target_has_vforked (inferior_pid, &b->forked_inferior_pid))
2225       continue;
2226
2227     if ((b->type == bp_catch_exec)
2228         && !target_has_execd (inferior_pid, &b->exec_pathname))
2229       continue;
2230
2231     if (ep_is_exception_catchpoint (b) &&
2232         !(current_exception_event = target_get_current_exception_event ()))
2233       continue;
2234
2235     /* Come here if it's a watchpoint, or if the break address matches */
2236
2237     bs = bpstat_alloc (b, bs);  /* Alloc a bpstat to explain stop */
2238
2239     /* Watchpoints may change this, if not found to have triggered. */
2240     bs->stop = 1;
2241     bs->print = 1;
2242
2243     sprintf (message, message1, b->number);
2244     if (b->type == bp_watchpoint ||
2245         b->type == bp_hardware_watchpoint)
2246       {
2247         switch (catch_errors (watchpoint_check, bs, message, 
2248                               RETURN_MASK_ALL))
2249           {
2250           case WP_DELETED:
2251             /* We've already printed what needs to be printed.  */
2252             bs->print_it = print_it_done;
2253             /* Stop.  */
2254             break;
2255           case WP_VALUE_CHANGED:
2256             /* Stop.  */
2257             ++(b->hit_count);
2258             break;
2259           case WP_VALUE_NOT_CHANGED:
2260             /* Don't stop.  */
2261             bs->print_it = print_it_noop;
2262             bs->stop = 0;
2263             /* Don't consider this a hit.  */
2264             --(b->hit_count);
2265             continue;
2266           default:
2267             /* Can't happen.  */
2268             /* FALLTHROUGH */
2269           case 0:
2270             /* Error from catch_errors.  */
2271             printf_filtered ("Watchpoint %d deleted.\n", b->number);
2272             if (b->related_breakpoint)
2273               b->related_breakpoint->disposition = del_at_next_stop;
2274             b->disposition = del_at_next_stop;
2275             /* We've already printed what needs to be printed.  */
2276             bs->print_it = print_it_done;
2277
2278             /* Stop.  */
2279             break;
2280           }
2281       }
2282     else if (b->type == bp_read_watchpoint || 
2283              b->type == bp_access_watchpoint)
2284       {
2285         CORE_ADDR addr;
2286         value_ptr v;
2287         int found = 0;
2288
2289         addr = target_stopped_data_address ();
2290         if (addr == 0)
2291           continue;
2292         for (v = b->val_chain; v; v = v->next)
2293           {
2294             if (v->lval == lval_memory)
2295               {
2296                 CORE_ADDR vaddr;
2297
2298                 vaddr = VALUE_ADDRESS (v) + VALUE_OFFSET (v);
2299                 /* Exact match not required.  Within range is sufficient.  
2300                  */
2301                 if (addr >= vaddr &&
2302                     addr < vaddr + TYPE_LENGTH (VALUE_TYPE (v)))
2303                   found = 1;
2304               }
2305           }
2306         if (found)
2307           switch (catch_errors (watchpoint_check, bs, message,
2308                                 RETURN_MASK_ALL))
2309             {
2310             case WP_DELETED:
2311               /* We've already printed what needs to be printed.  */
2312               bs->print_it = print_it_done;
2313               /* Stop.  */
2314               break;
2315             case WP_VALUE_CHANGED:
2316             case WP_VALUE_NOT_CHANGED:
2317               /* Stop.  */
2318               ++(b->hit_count);
2319               break;
2320             default:
2321               /* Can't happen.  */
2322             case 0:
2323               /* Error from catch_errors.  */
2324               printf_filtered ("Watchpoint %d deleted.\n", b->number);
2325               if (b->related_breakpoint)
2326                 b->related_breakpoint->disposition = del_at_next_stop;
2327               b->disposition = del_at_next_stop;
2328               /* We've already printed what needs to be printed.  */
2329               bs->print_it = print_it_done;
2330               break;
2331             }
2332         else    /* found == 0 */
2333           {
2334             /* This is a case where some watchpoint(s) triggered,
2335                but not at the address of this watchpoint (FOUND
2336                was left zero).  So don't print anything for this
2337                watchpoint.  */
2338             bs->print_it = print_it_noop;
2339             bs->stop = 0;
2340             continue;
2341           }
2342       }
2343     else
2344       {
2345         /* By definition, an encountered breakpoint is a triggered
2346            breakpoint. */
2347         ++(b->hit_count);
2348
2349         real_breakpoint = 1;
2350       }
2351
2352     if (b->frame && b->frame != (get_current_frame ())->frame &&
2353         (b->type == bp_step_resume &&
2354          (INNER_THAN (get_current_frame ()->frame, b->frame))))
2355       bs->stop = 0;
2356     else
2357       {
2358         int value_is_zero = 0;
2359
2360         if (b->cond)
2361           {
2362             /* Need to select the frame, with all that implies
2363                so that the conditions will have the right context.  */
2364             select_frame (get_current_frame (), 0);
2365             value_is_zero
2366               = catch_errors (breakpoint_cond_eval, (b->cond),
2367                               "Error in testing breakpoint condition:\n",
2368                               RETURN_MASK_ALL);
2369             /* FIXME-someday, should give breakpoint # */
2370             free_all_values ();
2371           }
2372         if (b->cond && value_is_zero)
2373           {
2374             bs->stop = 0;
2375             /* Don't consider this a hit.  */
2376             --(b->hit_count);
2377           }
2378         else if (b->ignore_count > 0)
2379           {
2380             b->ignore_count--;
2381             bs->stop = 0;
2382           }
2383         else
2384           {
2385             /* We will stop here */
2386             if (b->disposition == disable)
2387               b->enable = disabled;
2388             bs->commands = b->commands;
2389             if (b->silent)
2390               bs->print = 0;
2391             if (bs->commands &&
2392                 (STREQ ("silent", bs->commands->line) ||
2393                  (xdb_commands && STREQ ("Q", bs->commands->line))))
2394               {
2395                 bs->commands = bs->commands->next;
2396                 bs->print = 0;
2397               }
2398           }
2399       }
2400     /* Print nothing for this entry if we dont stop or if we dont print.  */
2401     if (bs->stop == 0 || bs->print == 0)
2402       bs->print_it = print_it_noop;
2403   }
2404
2405   bs->next = NULL;              /* Terminate the chain */
2406   bs = root_bs->next;           /* Re-grab the head of the chain */
2407
2408   if (real_breakpoint && bs)
2409     {
2410       if (bs->breakpoint_at->type == bp_hardware_breakpoint)
2411         {
2412           if (DECR_PC_AFTER_HW_BREAK != 0)
2413             {
2414               *pc = *pc - DECR_PC_AFTER_HW_BREAK;
2415               write_pc (*pc);
2416             }
2417         }
2418       else
2419         {
2420           if (DECR_PC_AFTER_BREAK != 0 || must_shift_inst_regs)
2421             {
2422               *pc = bp_addr;
2423 #if defined (SHIFT_INST_REGS)
2424               SHIFT_INST_REGS ();
2425 #else /* No SHIFT_INST_REGS.  */
2426               write_pc (bp_addr);
2427 #endif /* No SHIFT_INST_REGS.  */
2428             }
2429         }
2430     }
2431
2432   /* The value of a hardware watchpoint hasn't changed, but the
2433      intermediate memory locations we are watching may have.  */
2434   if (bs && !bs->stop &&
2435       (bs->breakpoint_at->type == bp_hardware_watchpoint ||
2436        bs->breakpoint_at->type == bp_read_watchpoint ||
2437        bs->breakpoint_at->type == bp_access_watchpoint))
2438     {
2439       remove_breakpoints ();
2440       insert_breakpoints ();
2441     }
2442   return bs;
2443 }
2444 \f
2445 /* Tell what to do about this bpstat.  */
2446 struct bpstat_what
2447 bpstat_what (bs)
2448      bpstat bs;
2449 {
2450   /* Classify each bpstat as one of the following.  */
2451   enum class
2452     {
2453       /* This bpstat element has no effect on the main_action.  */
2454       no_effect = 0,
2455
2456       /* There was a watchpoint, stop but don't print.  */
2457       wp_silent,
2458
2459       /* There was a watchpoint, stop and print.  */
2460       wp_noisy,
2461
2462       /* There was a breakpoint but we're not stopping.  */
2463       bp_nostop,
2464
2465       /* There was a breakpoint, stop but don't print.  */
2466       bp_silent,
2467
2468       /* There was a breakpoint, stop and print.  */
2469       bp_noisy,
2470
2471       /* We hit the longjmp breakpoint.  */
2472       long_jump,
2473
2474       /* We hit the longjmp_resume breakpoint.  */
2475       long_resume,
2476
2477       /* We hit the step_resume breakpoint.  */
2478       step_resume,
2479
2480       /* We hit the through_sigtramp breakpoint.  */
2481       through_sig,
2482
2483       /* We hit the shared library event breakpoint.  */
2484       shlib_event,
2485
2486       /* We caught a shared library event.  */
2487       catch_shlib_event,
2488
2489       /* This is just used to count how many enums there are.  */
2490       class_last
2491     };
2492
2493   /* Here is the table which drives this routine.  So that we can
2494      format it pretty, we define some abbreviations for the
2495      enum bpstat_what codes.  */
2496 #define kc BPSTAT_WHAT_KEEP_CHECKING
2497 #define ss BPSTAT_WHAT_STOP_SILENT
2498 #define sn BPSTAT_WHAT_STOP_NOISY
2499 #define sgl BPSTAT_WHAT_SINGLE
2500 #define slr BPSTAT_WHAT_SET_LONGJMP_RESUME
2501 #define clr BPSTAT_WHAT_CLEAR_LONGJMP_RESUME
2502 #define clrs BPSTAT_WHAT_CLEAR_LONGJMP_RESUME_SINGLE
2503 #define sr BPSTAT_WHAT_STEP_RESUME
2504 #define ts BPSTAT_WHAT_THROUGH_SIGTRAMP
2505 #define shl BPSTAT_WHAT_CHECK_SHLIBS
2506 #define shlr BPSTAT_WHAT_CHECK_SHLIBS_RESUME_FROM_HOOK
2507
2508 /* "Can't happen."  Might want to print an error message.
2509    abort() is not out of the question, but chances are GDB is just
2510    a bit confused, not unusable.  */
2511 #define err BPSTAT_WHAT_STOP_NOISY
2512
2513   /* Given an old action and a class, come up with a new action.  */
2514   /* One interesting property of this table is that wp_silent is the same
2515      as bp_silent and wp_noisy is the same as bp_noisy.  That is because
2516      after stopping, the check for whether to step over a breakpoint
2517      (BPSTAT_WHAT_SINGLE type stuff) is handled in proceed() without
2518      reference to how we stopped.  We retain separate wp_silent and
2519      bp_silent codes in case we want to change that someday. 
2520
2521      Another possibly interesting property of this table is that
2522      there's a partial ordering, priority-like, of the actions.  Once
2523      you've decided that some action is appropriate, you'll never go
2524      back and decide something of a lower priority is better.  The
2525      ordering is:
2526
2527      kc   < clr sgl shl slr sn sr ss ts
2528      sgl  < clrs shl shlr slr sn sr ss ts
2529      slr  < err shl shlr sn sr ss ts
2530      clr  < clrs err shl shlr sn sr ss ts
2531      clrs < err shl shlr sn sr ss ts
2532      ss   < shl shlr sn sr ts
2533      sn   < shl shlr sr ts
2534      sr   < shl shlr ts
2535      shl  < shlr
2536      ts   < 
2537      shlr <
2538
2539      What I think this means is that we don't need a damned table
2540      here.  If you just put the rows and columns in the right order,
2541      it'd look awfully regular.  We could simply walk the bpstat list
2542      and choose the highest priority action we find, with a little
2543      logic to handle the 'err' cases, and the CLEAR_LONGJMP_RESUME/
2544      CLEAR_LONGJMP_RESUME_SINGLE distinction (which breakpoint.h says
2545      is messy anyway).  */
2546
2547   /* step_resume entries: a step resume breakpoint overrides another
2548      breakpoint of signal handling (see comment in wait_for_inferior
2549      at first IN_SIGTRAMP where we set the step_resume breakpoint).  */
2550   /* We handle the through_sigtramp_breakpoint the same way; having both
2551      one of those and a step_resume_breakpoint is probably very rare (?).  */
2552
2553   static const enum bpstat_what_main_action
2554     table[(int) class_last][(int) BPSTAT_WHAT_LAST] =
2555   {
2556   /*                              old action */
2557   /*       kc    ss    sn    sgl    slr   clr    clrs   sr    ts   shl   shlr
2558    */
2559 /*no_effect */
2560     {kc, ss, sn, sgl, slr, clr, clrs, sr, ts, shl, shlr},
2561 /*wp_silent */
2562     {ss, ss, sn, ss, ss, ss, ss, sr, ts, shl, shlr},
2563 /*wp_noisy */
2564     {sn, sn, sn, sn, sn, sn, sn, sr, ts, shl, shlr},
2565 /*bp_nostop */
2566     {sgl, ss, sn, sgl, slr, clrs, clrs, sr, ts, shl, shlr},
2567 /*bp_silent */
2568     {ss, ss, sn, ss, ss, ss, ss, sr, ts, shl, shlr},
2569 /*bp_noisy */
2570     {sn, sn, sn, sn, sn, sn, sn, sr, ts, shl, shlr},
2571 /*long_jump */
2572     {slr, ss, sn, slr, err, err, err, sr, ts, shl, shlr},
2573 /*long_resume */
2574     {clr, ss, sn, clrs, err, err, err, sr, ts, shl, shlr},
2575 /*step_resume */
2576     {sr, sr, sr, sr, sr, sr, sr, sr, ts, shl, shlr},
2577 /*through_sig */
2578     {ts, ts, ts, ts, ts, ts, ts, ts, ts, shl, shlr},
2579 /*shlib */
2580     {shl, shl, shl, shl, shl, shl, shl, shl, ts, shl, shlr},
2581 /*catch_shlib */
2582     {shlr, shlr, shlr, shlr, shlr, shlr, shlr, shlr, ts, shlr, shlr}
2583   };
2584
2585 #undef kc
2586 #undef ss
2587 #undef sn
2588 #undef sgl
2589 #undef slr
2590 #undef clr
2591 #undef clrs
2592 #undef err
2593 #undef sr
2594 #undef ts
2595 #undef shl
2596 #undef shlr
2597   enum bpstat_what_main_action current_action = BPSTAT_WHAT_KEEP_CHECKING;
2598   struct bpstat_what retval;
2599
2600   retval.call_dummy = 0;
2601   for (; bs != NULL; bs = bs->next)
2602     {
2603       enum class bs_class = no_effect;
2604       if (bs->breakpoint_at == NULL)
2605         /* I suspect this can happen if it was a momentary breakpoint
2606            which has since been deleted.  */
2607         continue;
2608       switch (bs->breakpoint_at->type)
2609         {
2610         case bp_none:
2611           continue;
2612
2613         case bp_breakpoint:
2614         case bp_hardware_breakpoint:
2615         case bp_until:
2616         case bp_finish:
2617           if (bs->stop)
2618             {
2619               if (bs->print)
2620                 bs_class = bp_noisy;
2621               else
2622                 bs_class = bp_silent;
2623             }
2624           else
2625             bs_class = bp_nostop;
2626           break;
2627         case bp_watchpoint:
2628         case bp_hardware_watchpoint:
2629         case bp_read_watchpoint:
2630         case bp_access_watchpoint:
2631           if (bs->stop)
2632             {
2633               if (bs->print)
2634                 bs_class = wp_noisy;
2635               else
2636                 bs_class = wp_silent;
2637             }
2638           else
2639             /* There was a watchpoint, but we're not stopping. 
2640                This requires no further action.  */
2641             bs_class = no_effect;
2642           break;
2643         case bp_longjmp:
2644           bs_class = long_jump;
2645           break;
2646         case bp_longjmp_resume:
2647           bs_class = long_resume;
2648           break;
2649         case bp_step_resume:
2650           if (bs->stop)
2651             {
2652               bs_class = step_resume;
2653             }
2654           else
2655             /* It is for the wrong frame.  */
2656             bs_class = bp_nostop;
2657           break;
2658         case bp_through_sigtramp:
2659           bs_class = through_sig;
2660           break;
2661         case bp_watchpoint_scope:
2662           bs_class = bp_nostop;
2663           break;
2664         case bp_shlib_event:
2665           bs_class = shlib_event;
2666           break;
2667         case bp_catch_load:
2668         case bp_catch_unload:
2669           /* Only if this catchpoint triggered should we cause the
2670              step-out-of-dld behaviour.  Otherwise, we ignore this
2671              catchpoint.  */
2672           if (bs->stop)
2673             bs_class = catch_shlib_event;
2674           else
2675             bs_class = no_effect;
2676           break;
2677         case bp_catch_fork:
2678         case bp_catch_vfork:
2679         case bp_catch_exec:
2680           if (bs->stop)
2681             {
2682               if (bs->print)
2683                 bs_class = bp_noisy;
2684               else
2685                 bs_class = bp_silent;
2686             }
2687           else
2688             /* There was a catchpoint, but we're not stopping.  
2689                This requires no further action.  */
2690             bs_class = no_effect;
2691           break;
2692         case bp_catch_catch:
2693           if (!bs->stop || CURRENT_EXCEPTION_KIND != EX_EVENT_CATCH)
2694             bs_class = bp_nostop;
2695           else if (bs->stop)
2696             bs_class = bs->print ? bp_noisy : bp_silent;
2697           break;
2698         case bp_catch_throw:
2699           if (!bs->stop || CURRENT_EXCEPTION_KIND != EX_EVENT_THROW)
2700             bs_class = bp_nostop;
2701           else if (bs->stop)
2702             bs_class = bs->print ? bp_noisy : bp_silent;
2703           break;
2704         case bp_call_dummy:
2705           /* Make sure the action is stop (silent or noisy),
2706              so infrun.c pops the dummy frame.  */
2707           bs_class = bp_silent;
2708           retval.call_dummy = 1;
2709           break;
2710         }
2711       current_action = table[(int) bs_class][(int) current_action];
2712     }
2713   retval.main_action = current_action;
2714   return retval;
2715 }
2716
2717 /* Nonzero if we should step constantly (e.g. watchpoints on machines
2718    without hardware support).  This isn't related to a specific bpstat,
2719    just to things like whether watchpoints are set.  */
2720
2721 int
2722 bpstat_should_step ()
2723 {
2724   struct breakpoint *b;
2725   ALL_BREAKPOINTS (b)
2726     if (b->enable == enabled && b->type == bp_watchpoint)
2727     return 1;
2728   return 0;
2729 }
2730
2731 /* Nonzero if there are enabled hardware watchpoints. */
2732 int
2733 bpstat_have_active_hw_watchpoints ()
2734 {
2735   struct breakpoint *b;
2736   ALL_BREAKPOINTS (b)
2737     if ((b->enable == enabled) &&
2738         (b->inserted) &&
2739         ((b->type == bp_hardware_watchpoint) ||
2740          (b->type == bp_read_watchpoint) ||
2741          (b->type == bp_access_watchpoint)))
2742     return 1;
2743   return 0;
2744 }
2745 \f
2746
2747 /* Given a bpstat that records zero or more triggered eventpoints, this
2748    function returns another bpstat which contains only the catchpoints
2749    on that first list, if any. */
2750 void
2751 bpstat_get_triggered_catchpoints (ep_list, cp_list)
2752      bpstat ep_list;
2753      bpstat *cp_list;
2754 {
2755   struct bpstats root_bs[1];
2756   bpstat bs = root_bs;
2757   struct breakpoint *ep;
2758   char *dll_pathname;
2759
2760   bpstat_clear (cp_list);
2761   root_bs->next = NULL;
2762
2763   for (; ep_list != NULL; ep_list = ep_list->next)
2764     {
2765       /* Is this eventpoint a catchpoint?  If not, ignore it. */
2766       ep = ep_list->breakpoint_at;
2767       if (ep == NULL)
2768         break;
2769       if ((ep->type != bp_catch_load) &&
2770           (ep->type != bp_catch_unload) &&
2771           (ep->type != bp_catch_catch) &&
2772           (ep->type != bp_catch_throw))         
2773         /* pai: (temp) ADD fork/vfork here!!  */
2774         continue;
2775
2776       /* Yes; add it to the list. */
2777       bs = bpstat_alloc (ep, bs);
2778       *bs = *ep_list;
2779       bs->next = NULL;
2780       bs = root_bs->next;
2781
2782 #if defined(SOLIB_ADD)
2783       /* Also, for each triggered catchpoint, tag it with the name of
2784          the library that caused this trigger.  (We copy the name now,
2785          because it's only guaranteed to be available NOW, when the
2786          catchpoint triggers.  Clients who may wish to know the name
2787          later must get it from the catchpoint itself.) */
2788       if (ep->triggered_dll_pathname != NULL)
2789         free (ep->triggered_dll_pathname);
2790       if (ep->type == bp_catch_load)
2791         dll_pathname = SOLIB_LOADED_LIBRARY_PATHNAME (inferior_pid);
2792       else
2793         dll_pathname = SOLIB_UNLOADED_LIBRARY_PATHNAME (inferior_pid);
2794 #else
2795       dll_pathname = NULL;
2796 #endif
2797       if (dll_pathname)
2798         {
2799           ep->triggered_dll_pathname = (char *) 
2800             xmalloc (strlen (dll_pathname) + 1);
2801           strcpy (ep->triggered_dll_pathname, dll_pathname);
2802         }
2803       else
2804         ep->triggered_dll_pathname = NULL;
2805     }
2806
2807   *cp_list = bs;
2808 }
2809
2810 /* Print information on breakpoint number BNUM, or -1 if all.
2811    If WATCHPOINTS is zero, process only breakpoints; if WATCHPOINTS
2812    is nonzero, process only watchpoints.  */
2813
2814 typedef struct
2815 {
2816   enum bptype type;
2817   char *description;
2818 }
2819 ep_type_description_t;
2820
2821 static void
2822 breakpoint_1 (bnum, allflag)
2823      int bnum;
2824      int allflag;
2825 {
2826   register struct breakpoint *b;
2827   register struct command_line *l;
2828   register struct symbol *sym;
2829   CORE_ADDR last_addr = (CORE_ADDR) - 1;
2830   int found_a_breakpoint = 0;
2831   static ep_type_description_t bptypes[] =
2832   {
2833     {bp_none, "?deleted?"},
2834     {bp_breakpoint, "breakpoint"},
2835     {bp_hardware_breakpoint, "hw breakpoint"},
2836     {bp_until, "until"},
2837     {bp_finish, "finish"},
2838     {bp_watchpoint, "watchpoint"},
2839     {bp_hardware_watchpoint, "hw watchpoint"},
2840     {bp_read_watchpoint, "read watchpoint"},
2841     {bp_access_watchpoint, "acc watchpoint"},
2842     {bp_longjmp, "longjmp"},
2843     {bp_longjmp_resume, "longjmp resume"},
2844     {bp_step_resume, "step resume"},
2845     {bp_through_sigtramp, "sigtramp"},
2846     {bp_watchpoint_scope, "watchpoint scope"},
2847     {bp_call_dummy, "call dummy"},
2848     {bp_shlib_event, "shlib events"},
2849     {bp_catch_load, "catch load"},
2850     {bp_catch_unload, "catch unload"},
2851     {bp_catch_fork, "catch fork"},
2852     {bp_catch_vfork, "catch vfork"},
2853     {bp_catch_exec, "catch exec"},
2854     {bp_catch_catch, "catch catch"},
2855     {bp_catch_throw, "catch throw"}
2856   };
2857
2858   static char *bpdisps[] =
2859   {"del", "dstp", "dis", "keep"};
2860   static char bpenables[] = "nynny";
2861   char wrap_indent[80];
2862
2863
2864
2865   ALL_BREAKPOINTS (b)
2866     if (bnum == -1
2867         || bnum == b->number)
2868     {
2869 /*  We only print out user settable breakpoints unless the allflag is set. */
2870       if (!allflag
2871           && b->type != bp_breakpoint
2872           && b->type != bp_catch_load
2873           && b->type != bp_catch_unload
2874           && b->type != bp_catch_fork
2875           && b->type != bp_catch_vfork
2876           && b->type != bp_catch_exec
2877           && b->type != bp_catch_catch
2878           && b->type != bp_catch_throw
2879           && b->type != bp_hardware_breakpoint
2880           && b->type != bp_watchpoint
2881           && b->type != bp_read_watchpoint
2882           && b->type != bp_access_watchpoint
2883           && b->type != bp_hardware_watchpoint)
2884         continue;
2885
2886       if (!found_a_breakpoint++)
2887         {
2888           annotate_breakpoints_headers ();
2889
2890           annotate_field (0);
2891           printf_filtered ("Num ");
2892           annotate_field (1);
2893           printf_filtered ("Type           ");
2894           annotate_field (2);
2895           printf_filtered ("Disp ");
2896           annotate_field (3);
2897           printf_filtered ("Enb ");
2898           if (addressprint)
2899             {
2900               annotate_field (4);
2901               printf_filtered ("Address    ");
2902             }
2903           annotate_field (5);
2904           printf_filtered ("What\n");
2905
2906           annotate_breakpoints_table ();
2907         }
2908
2909       annotate_record ();
2910       annotate_field (0);
2911       printf_filtered ("%-3d ", b->number);
2912       annotate_field (1);
2913       if ((int) b->type > (sizeof (bptypes) / sizeof (bptypes[0])))
2914         error ("bptypes table does not describe type #%d.", (int) b->type);
2915       if ((int) b->type != bptypes[(int) b->type].type)
2916         error ("bptypes table does not describe type #%d?", (int) b->type);
2917       printf_filtered ("%-14s ", bptypes[(int) b->type].description);
2918       annotate_field (2);
2919       printf_filtered ("%-4s ", bpdisps[(int) b->disposition]);
2920       annotate_field (3);
2921       printf_filtered ("%-3c ", bpenables[(int) b->enable]);
2922
2923       strcpy (wrap_indent, "                           ");
2924       if (addressprint)
2925         strcat (wrap_indent, "           ");
2926       switch (b->type)
2927         {
2928         case bp_watchpoint:
2929         case bp_hardware_watchpoint:
2930         case bp_read_watchpoint:
2931         case bp_access_watchpoint:
2932           /* Field 4, the address, is omitted (which makes the columns
2933              not line up too nicely with the headers, but the effect
2934              is relatively readable).  */
2935           annotate_field (5);
2936           print_expression (b->exp, gdb_stdout);
2937           break;
2938
2939         case bp_catch_load:
2940         case bp_catch_unload:
2941           /* Field 4, the address, is omitted (which makes the columns
2942              not line up too nicely with the headers, but the effect
2943              is relatively readable).  */
2944           annotate_field (5);
2945           if (b->dll_pathname == NULL)
2946             printf_filtered ("<any library> ");
2947           else
2948             printf_filtered ("library \"%s\" ", b->dll_pathname);
2949           break;
2950
2951         case bp_catch_fork:
2952         case bp_catch_vfork:
2953           /* Field 4, the address, is omitted (which makes the columns
2954              not line up too nicely with the headers, but the effect
2955              is relatively readable).  */
2956           annotate_field (5);
2957           if (b->forked_inferior_pid != 0)
2958             printf_filtered ("process %d ", b->forked_inferior_pid);
2959           break;
2960
2961         case bp_catch_exec:
2962           /* Field 4, the address, is omitted (which makes the columns
2963              not line up too nicely with the headers, but the effect
2964              is relatively readable).  */
2965           annotate_field (5);
2966           if (b->exec_pathname != NULL)
2967             printf_filtered ("program \"%s\" ", b->exec_pathname);
2968           break;
2969         case bp_catch_catch:
2970           /* Field 4, the address, is omitted (which makes the columns
2971              not line up too nicely with the headers, but the effect
2972              is relatively readable).  */
2973           annotate_field (5);
2974           printf_filtered ("exception catch ");
2975           break;
2976         case bp_catch_throw:
2977           /* Field 4, the address, is omitted (which makes the columns
2978              not line up too nicely with the headers, but the effect
2979              is relatively readable).  */
2980           annotate_field (5);
2981           printf_filtered ("exception throw ");
2982           break;
2983
2984         case bp_breakpoint:
2985         case bp_hardware_breakpoint:
2986         case bp_until:
2987         case bp_finish:
2988         case bp_longjmp:
2989         case bp_longjmp_resume:
2990         case bp_step_resume:
2991         case bp_through_sigtramp:
2992         case bp_watchpoint_scope:
2993         case bp_call_dummy:
2994         case bp_shlib_event:
2995           if (addressprint)
2996             {
2997               annotate_field (4);
2998               /* FIXME-32x64: need a print_address_numeric with
2999                  field width */
3000               printf_filtered
3001                 ("%s ",
3002                  local_hex_string_custom
3003                  ((unsigned long) b->address, "08l"));
3004             }
3005
3006           annotate_field (5);
3007
3008           last_addr = b->address;
3009           if (b->source_file)
3010             {
3011               sym = find_pc_sect_function (b->address, b->section);
3012               if (sym)
3013                 {
3014                   fputs_filtered ("in ", gdb_stdout);
3015                   fputs_filtered (SYMBOL_SOURCE_NAME (sym), gdb_stdout);
3016                   wrap_here (wrap_indent);
3017                   fputs_filtered (" at ", gdb_stdout);
3018                 }
3019               fputs_filtered (b->source_file, gdb_stdout);
3020               printf_filtered (":%d", b->line_number);
3021             }
3022           else
3023             print_address_symbolic (b->address, gdb_stdout, demangle, " ");
3024           break;
3025         }
3026
3027       if (b->thread != -1)
3028         printf_filtered (" thread %d", b->thread);
3029
3030       printf_filtered ("\n");
3031
3032       if (b->frame)
3033         {
3034           annotate_field (6);
3035
3036           printf_filtered ("\tstop only in stack frame at ");
3037           print_address_numeric (b->frame, 1, gdb_stdout);
3038           printf_filtered ("\n");
3039         }
3040
3041       if (b->cond)
3042         {
3043           annotate_field (7);
3044
3045           printf_filtered ("\tstop only if ");
3046           print_expression (b->cond, gdb_stdout);
3047           printf_filtered ("\n");
3048         }
3049
3050       if (b->thread != -1)
3051         {
3052           /* FIXME should make an annotation for this */
3053           printf_filtered ("\tstop only in thread %d\n", b->thread);
3054         }
3055
3056       if (show_breakpoint_hit_counts && b->hit_count)
3057         {
3058           /* FIXME should make an annotation for this */
3059           if (ep_is_catchpoint (b))
3060             printf_filtered ("\tcatchpoint");
3061           else
3062             printf_filtered ("\tbreakpoint");
3063           printf_filtered (" already hit %d time%s\n",
3064                            b->hit_count, (b->hit_count == 1 ? "" : "s"));
3065         }
3066
3067       if (b->ignore_count)
3068         {
3069           annotate_field (8);
3070
3071           printf_filtered ("\tignore next %d hits\n", b->ignore_count);
3072         }
3073
3074       if ((l = b->commands))
3075         {
3076           annotate_field (9);
3077
3078           while (l)
3079             {
3080               print_command_line (l, 4, gdb_stdout);
3081               l = l->next;
3082             }
3083         }
3084     }
3085
3086   if (!found_a_breakpoint)
3087     {
3088       if (bnum == -1)
3089         printf_filtered ("No breakpoints or watchpoints.\n");
3090       else
3091         printf_filtered ("No breakpoint or watchpoint number %d.\n", bnum);
3092     }
3093   else
3094     /* Compare against (CORE_ADDR)-1 in case some compiler decides
3095        that a comparison of an unsigned with -1 is always false.  */
3096   if (last_addr != (CORE_ADDR) - 1)
3097     set_next_address (last_addr);
3098
3099   annotate_breakpoints_table_end ();
3100 }
3101
3102 /* ARGSUSED */
3103 static void
3104 breakpoints_info (bnum_exp, from_tty)
3105      char *bnum_exp;
3106      int from_tty;
3107 {
3108   int bnum = -1;
3109
3110   if (bnum_exp)
3111     bnum = parse_and_eval_address (bnum_exp);
3112
3113   breakpoint_1 (bnum, 0);
3114 }
3115
3116 /* ARGSUSED */
3117 static void
3118 maintenance_info_breakpoints (bnum_exp, from_tty)
3119      char *bnum_exp;
3120      int from_tty;
3121 {
3122   int bnum = -1;
3123
3124   if (bnum_exp)
3125     bnum = parse_and_eval_address (bnum_exp);
3126
3127   breakpoint_1 (bnum, 1);
3128 }
3129
3130 /* Print a message describing any breakpoints set at PC.  */
3131
3132 static void
3133 describe_other_breakpoints (pc, section)
3134      CORE_ADDR pc;
3135      asection *section;
3136 {
3137   register int others = 0;
3138   register struct breakpoint *b;
3139
3140   ALL_BREAKPOINTS (b)
3141     if (b->address == pc)
3142     if (overlay_debugging == 0 ||
3143         b->section == section)
3144       others++;
3145   if (others > 0)
3146     {
3147       printf_filtered ("Note: breakpoint%s ", (others > 1) ? "s" : "");
3148       ALL_BREAKPOINTS (b)
3149         if (b->address == pc)
3150         if (overlay_debugging == 0 ||
3151             b->section == section)
3152           {
3153             others--;
3154             printf_filtered
3155               ("%d%s%s ",
3156                b->number,
3157                ((b->enable == disabled || 
3158                  b->enable == shlib_disabled || 
3159                  b->enable == call_disabled) ? " (disabled)" 
3160                 : b->enable == permanent ? " (permanent)"
3161                 : ""),
3162                (others > 1) ? "," : ((others == 1) ? " and" : ""));
3163           }
3164       printf_filtered ("also set at pc ");
3165       print_address_numeric (pc, 1, gdb_stdout);
3166       printf_filtered (".\n");
3167     }
3168 }
3169 \f
3170 /* Set the default place to put a breakpoint
3171    for the `break' command with no arguments.  */
3172
3173 void
3174 set_default_breakpoint (valid, addr, symtab, line)
3175      int valid;
3176      CORE_ADDR addr;
3177      struct symtab *symtab;
3178      int line;
3179 {
3180   default_breakpoint_valid = valid;
3181   default_breakpoint_address = addr;
3182   default_breakpoint_symtab = symtab;
3183   default_breakpoint_line = line;
3184 }
3185
3186 /* Rescan breakpoints at address ADDRESS,
3187    marking the first one as "first" and any others as "duplicates".
3188    This is so that the bpt instruction is only inserted once.
3189    If we have a permanent breakpoint at ADDRESS, make that one
3190    the official one, and the rest as duplicates.  */
3191
3192 static void
3193 check_duplicates (address, section)
3194      CORE_ADDR address;
3195      asection *section;
3196 {
3197   register struct breakpoint *b;
3198   register int count = 0;
3199   struct breakpoint *perm_bp = 0;
3200
3201   if (address == 0)             /* Watchpoints are uninteresting */
3202     return;
3203
3204   ALL_BREAKPOINTS (b)
3205     if (b->enable != disabled
3206         && b->enable != shlib_disabled
3207         && b->enable != call_disabled
3208         && b->address == address
3209         && (overlay_debugging == 0 || b->section == section))
3210     {
3211       /* Have we found a permanent breakpoint?  */
3212       if (b->enable == permanent)
3213         {
3214           perm_bp = b;
3215           break;
3216         }
3217         
3218       count++;
3219       b->duplicate = count > 1;
3220     }
3221
3222   /* If we found a permanent breakpoint at this address, go over the
3223      list again and declare all the other breakpoints there to be the
3224      duplicates.  */
3225   if (perm_bp)
3226     {
3227       perm_bp->duplicate = 0;
3228
3229       /* Permanent breakpoint should always be inserted.  */
3230       if (! perm_bp->inserted)
3231         internal_error ("allegedly permanent breakpoint is not "
3232                         "actually inserted");
3233
3234       ALL_BREAKPOINTS (b)
3235         if (b != perm_bp)
3236           {
3237             if (b->inserted)
3238               internal_error ("another breakpoint was inserted on top of "
3239                               "a permanent breakpoint");
3240
3241             if (b->enable != disabled
3242                 && b->enable != shlib_disabled
3243                 && b->enable != call_disabled
3244                 && b->address == address
3245                 && (overlay_debugging == 0 || b->section == section))
3246               b->duplicate = 1;
3247           }
3248     }
3249 }
3250
3251 /* Low level routine to set a breakpoint.
3252    Takes as args the three things that every breakpoint must have.
3253    Returns the breakpoint object so caller can set other things.
3254    Does not set the breakpoint number!
3255    Does not print anything.
3256
3257    ==> This routine should not be called if there is a chance of later
3258    error(); otherwise it leaves a bogus breakpoint on the chain.  Validate
3259    your arguments BEFORE calling this routine!  */
3260
3261 struct breakpoint *
3262 set_raw_breakpoint (sal)
3263      struct symtab_and_line sal;
3264 {
3265   register struct breakpoint *b, *b1;
3266
3267   b = (struct breakpoint *) xmalloc (sizeof (struct breakpoint));
3268   memset (b, 0, sizeof (*b));
3269   b->address = sal.pc;
3270   if (sal.symtab == NULL)
3271     b->source_file = NULL;
3272   else
3273     b->source_file = savestring (sal.symtab->filename,
3274                                  strlen (sal.symtab->filename));
3275   b->section = sal.section;
3276   b->language = current_language->la_language;
3277   b->input_radix = input_radix;
3278   b->thread = -1;
3279   b->line_number = sal.line;
3280   b->enable = enabled;
3281   b->next = 0;
3282   b->silent = 0;
3283   b->ignore_count = 0;
3284   b->commands = NULL;
3285   b->frame = 0;
3286   b->dll_pathname = NULL;
3287   b->triggered_dll_pathname = NULL;
3288   b->forked_inferior_pid = 0;
3289   b->exec_pathname = NULL;
3290
3291   /* Add this breakpoint to the end of the chain
3292      so that a list of breakpoints will come out in order
3293      of increasing numbers.  */
3294
3295   b1 = breakpoint_chain;
3296   if (b1 == 0)
3297     breakpoint_chain = b;
3298   else
3299     {
3300       while (b1->next)
3301         b1 = b1->next;
3302       b1->next = b;
3303     }
3304
3305   check_duplicates (sal.pc, sal.section);
3306   breakpoints_changed ();
3307
3308   return b;
3309 }
3310
3311
3312 /* Note that the breakpoint object B describes a permanent breakpoint
3313    instruction, hard-wired into the inferior's code.  */
3314 void
3315 make_breakpoint_permanent (struct breakpoint *b)
3316 {
3317   b->enable = permanent;
3318
3319   /* By definition, permanent breakpoints are already present in the code.  */
3320   b->inserted = 1;
3321 }
3322
3323 #ifdef GET_LONGJMP_TARGET
3324
3325 static void
3326 create_longjmp_breakpoint (func_name)
3327      char *func_name;
3328 {
3329   struct symtab_and_line sal;
3330   struct breakpoint *b;
3331
3332   INIT_SAL (&sal);              /* initialize to zeroes */
3333   if (func_name != NULL)
3334     {
3335       struct minimal_symbol *m;
3336
3337       m = lookup_minimal_symbol_text (func_name, NULL, 
3338                                       (struct objfile *) NULL);
3339       if (m)
3340         sal.pc = SYMBOL_VALUE_ADDRESS (m);
3341       else
3342         return;
3343     }
3344   sal.section = find_pc_overlay (sal.pc);
3345   b = set_raw_breakpoint (sal);
3346   if (!b)
3347     return;
3348
3349   b->type = func_name != NULL ? bp_longjmp : bp_longjmp_resume;
3350   b->disposition = donttouch;
3351   b->enable = disabled;
3352   b->silent = 1;
3353   if (func_name)
3354     b->addr_string = strsave (func_name);
3355   b->number = internal_breakpoint_number--;
3356 }
3357
3358 #endif /* #ifdef GET_LONGJMP_TARGET */
3359
3360 /* Call this routine when stepping and nexting to enable a breakpoint
3361    if we do a longjmp().  When we hit that breakpoint, call
3362    set_longjmp_resume_breakpoint() to figure out where we are going. */
3363
3364 void
3365 enable_longjmp_breakpoint ()
3366 {
3367   register struct breakpoint *b;
3368
3369   ALL_BREAKPOINTS (b)
3370     if (b->type == bp_longjmp)
3371     {
3372       b->enable = enabled;
3373       check_duplicates (b->address, b->section);
3374     }
3375 }
3376
3377 void
3378 disable_longjmp_breakpoint ()
3379 {
3380   register struct breakpoint *b;
3381
3382   ALL_BREAKPOINTS (b)
3383     if (b->type == bp_longjmp
3384         || b->type == bp_longjmp_resume)
3385     {
3386       b->enable = disabled;
3387       check_duplicates (b->address, b->section);
3388     }
3389 }
3390
3391 #ifdef SOLIB_ADD
3392 void
3393 remove_solib_event_breakpoints ()
3394 {
3395   register struct breakpoint *b, *temp;
3396
3397   ALL_BREAKPOINTS_SAFE (b, temp)
3398     if (b->type == bp_shlib_event)
3399     delete_breakpoint (b);
3400 }
3401
3402 struct breakpoint *
3403 create_solib_event_breakpoint (address)
3404      CORE_ADDR address;
3405 {
3406   struct breakpoint *b;
3407   struct symtab_and_line sal;
3408
3409   INIT_SAL (&sal);              /* initialize to zeroes */
3410   sal.pc = address;
3411   sal.section = find_pc_overlay (sal.pc);
3412   b = set_raw_breakpoint (sal);
3413   b->number = internal_breakpoint_number--;
3414   b->disposition = donttouch;
3415   b->type = bp_shlib_event;
3416
3417   return b;
3418 }
3419
3420 /* Disable any breakpoints that are on code in shared libraries.  Only
3421    apply to enabled breakpoints, disabled ones can just stay disabled.  */
3422
3423 void
3424 disable_breakpoints_in_shlibs (silent)
3425      int silent;
3426 {
3427   struct breakpoint *b;
3428   int disabled_shlib_breaks = 0;
3429
3430   /* See also: insert_breakpoints, under DISABLE_UNSETTABLE_BREAK. */
3431   ALL_BREAKPOINTS (b)
3432   {
3433 #if defined (PC_SOLIB)
3434     if (((b->type == bp_breakpoint) ||
3435          (b->type == bp_hardware_breakpoint)) &&
3436         b->enable == enabled &&
3437         !b->duplicate &&
3438         PC_SOLIB (b->address))
3439       {
3440         b->enable = shlib_disabled;
3441         if (!silent)
3442           {
3443             if (!disabled_shlib_breaks)
3444               {
3445                 target_terminal_ours_for_output ();
3446                 warning ("Temporarily disabling shared library breakpoints:");
3447               }
3448             disabled_shlib_breaks = 1;
3449             warning ("breakpoint #%d ", b->number);
3450           }
3451       }
3452 #endif
3453   }
3454 }
3455
3456 /* Try to reenable any breakpoints in shared libraries.  */
3457 void
3458 re_enable_breakpoints_in_shlibs ()
3459 {
3460   struct breakpoint *b;
3461
3462   ALL_BREAKPOINTS (b)
3463     if (b->enable == shlib_disabled)
3464     {
3465       char buf[1];
3466
3467       /* Do not reenable the breakpoint if the shared library
3468          is still not mapped in.  */
3469       if (target_read_memory (b->address, buf, 1) == 0)
3470         b->enable = enabled;
3471     }
3472 }
3473
3474 #endif
3475
3476 static void
3477 solib_load_unload_1 (hookname, tempflag, dll_pathname, cond_string, bp_kind)
3478      char *hookname;
3479      int tempflag;
3480      char *dll_pathname;
3481      char *cond_string;
3482      enum bptype bp_kind;
3483 {
3484   struct breakpoint *b;
3485   struct symtabs_and_lines sals;
3486   struct cleanup *old_chain;
3487   struct cleanup *canonical_strings_chain = NULL;
3488   char *addr_start = hookname;
3489   char *addr_end = NULL;
3490   char **canonical = (char **) NULL;
3491   int thread = -1;              /* All threads. */
3492
3493   /* Set a breakpoint on the specified hook. */
3494   sals = decode_line_1 (&hookname, 1, (struct symtab *) NULL, 0, &canonical);
3495   addr_end = hookname;
3496
3497   if (sals.nelts == 0)
3498     {
3499       warning ("Unable to set a breakpoint on dynamic linker callback.");
3500       warning ("Suggest linking with /opt/langtools/lib/end.o.");
3501       warning ("GDB will be unable to track shl_load/shl_unload calls");
3502       return;
3503     }
3504   if (sals.nelts != 1)
3505     {
3506       warning ("Unable to set unique breakpoint on dynamic linker callback.");
3507       warning ("GDB will be unable to track shl_load/shl_unload calls");
3508       return;
3509     }
3510
3511   /* Make sure that all storage allocated in decode_line_1 gets freed
3512      in case the following errors out.  */
3513   old_chain = make_cleanup (free, sals.sals);
3514   if (canonical != (char **) NULL)
3515     {
3516       make_cleanup (free, canonical);
3517       canonical_strings_chain = make_cleanup (null_cleanup, 0);
3518       if (canonical[0] != NULL)
3519         make_cleanup (free, canonical[0]);
3520     }
3521
3522   resolve_sal_pc (&sals.sals[0]);
3523
3524   /* Remove the canonical strings from the cleanup, they are needed below.  */
3525   if (canonical != (char **) NULL)
3526     discard_cleanups (canonical_strings_chain);
3527
3528   b = set_raw_breakpoint (sals.sals[0]);
3529   set_breakpoint_count (breakpoint_count + 1);
3530   b->number = breakpoint_count;
3531   b->cond = NULL;
3532   b->cond_string = (cond_string == NULL) ? 
3533     NULL : savestring (cond_string, strlen (cond_string));
3534   b->thread = thread;
3535
3536   if (canonical != (char **) NULL && canonical[0] != NULL)
3537     b->addr_string = canonical[0];
3538   else if (addr_start)
3539     b->addr_string = savestring (addr_start, addr_end - addr_start);
3540
3541   b->enable = enabled;
3542   b->disposition = tempflag ? del : donttouch;
3543
3544   if (dll_pathname == NULL)
3545     b->dll_pathname = NULL;
3546   else
3547     {
3548       b->dll_pathname = (char *) xmalloc (strlen (dll_pathname) + 1);
3549       strcpy (b->dll_pathname, dll_pathname);
3550     }
3551   b->type = bp_kind;
3552
3553   mention (b);
3554   do_cleanups (old_chain);
3555 }
3556
3557 void
3558 create_solib_load_event_breakpoint (hookname, tempflag, 
3559                                     dll_pathname, cond_string)
3560      char *hookname;
3561      int tempflag;
3562      char *dll_pathname;
3563      char *cond_string;
3564 {
3565   solib_load_unload_1 (hookname, tempflag, dll_pathname, 
3566                        cond_string, bp_catch_load);
3567 }
3568
3569 void
3570 create_solib_unload_event_breakpoint (hookname, tempflag, 
3571                                       dll_pathname, cond_string)
3572      char *hookname;
3573      int tempflag;
3574      char *dll_pathname;
3575      char *cond_string;
3576 {
3577   solib_load_unload_1 (hookname,tempflag, dll_pathname, 
3578                        cond_string, bp_catch_unload);
3579 }
3580
3581 static void
3582 create_fork_vfork_event_catchpoint (tempflag, cond_string, bp_kind)
3583      int tempflag;
3584      char *cond_string;
3585      enum bptype bp_kind;
3586 {
3587   struct symtab_and_line sal;
3588   struct breakpoint *b;
3589   int thread = -1;              /* All threads. */
3590
3591   INIT_SAL (&sal);
3592   sal.pc = 0;
3593   sal.symtab = NULL;
3594   sal.line = 0;
3595
3596   b = set_raw_breakpoint (sal);
3597   set_breakpoint_count (breakpoint_count + 1);
3598   b->number = breakpoint_count;
3599   b->cond = NULL;
3600   b->cond_string = (cond_string == NULL) ? 
3601     NULL : savestring (cond_string, strlen (cond_string));
3602   b->thread = thread;
3603   b->addr_string = NULL;
3604   b->enable = enabled;
3605   b->disposition = tempflag ? del : donttouch;
3606   b->forked_inferior_pid = 0;
3607
3608   b->type = bp_kind;
3609
3610   mention (b);
3611 }
3612
3613 void
3614 create_fork_event_catchpoint (tempflag, cond_string)
3615      int tempflag;
3616      char *cond_string;
3617 {
3618   create_fork_vfork_event_catchpoint (tempflag, cond_string, bp_catch_fork);
3619 }
3620
3621 void
3622 create_vfork_event_catchpoint (tempflag, cond_string)
3623      int tempflag;
3624      char *cond_string;
3625 {
3626   create_fork_vfork_event_catchpoint (tempflag, cond_string, bp_catch_vfork);
3627 }
3628
3629 void
3630 create_exec_event_catchpoint (tempflag, cond_string)
3631      int tempflag;
3632      char *cond_string;
3633 {
3634   struct symtab_and_line sal;
3635   struct breakpoint *b;
3636   int thread = -1;              /* All threads. */
3637
3638   INIT_SAL (&sal);
3639   sal.pc = 0;
3640   sal.symtab = NULL;
3641   sal.line = 0;
3642
3643   b = set_raw_breakpoint (sal);
3644   set_breakpoint_count (breakpoint_count + 1);
3645   b->number = breakpoint_count;
3646   b->cond = NULL;
3647   b->cond_string = (cond_string == NULL) ?
3648     NULL : savestring (cond_string, strlen (cond_string));
3649   b->thread = thread;
3650   b->addr_string = NULL;
3651   b->enable = enabled;
3652   b->disposition = tempflag ? del : donttouch;
3653
3654   b->type = bp_catch_exec;
3655
3656   mention (b);
3657 }
3658
3659 static int
3660 hw_breakpoint_used_count ()
3661 {
3662   register struct breakpoint *b;
3663   int i = 0;
3664
3665   ALL_BREAKPOINTS (b)
3666   {
3667     if (b->type == bp_hardware_breakpoint && b->enable == enabled)
3668       i++;
3669   }
3670
3671   return i;
3672 }
3673
3674 static int
3675 hw_watchpoint_used_count (type, other_type_used)
3676      enum bptype type;
3677      int *other_type_used;
3678 {
3679   register struct breakpoint *b;
3680   int i = 0;
3681
3682   *other_type_used = 0;
3683   ALL_BREAKPOINTS (b)
3684   {
3685     if (b->enable == enabled)
3686       {
3687         if (b->type == type)
3688           i++;
3689         else if ((b->type == bp_hardware_watchpoint ||
3690                   b->type == bp_read_watchpoint ||
3691                   b->type == bp_access_watchpoint)
3692                  && b->enable == enabled)
3693           *other_type_used = 1;
3694       }
3695   }
3696   return i;
3697 }
3698
3699 /* Call this after hitting the longjmp() breakpoint.  Use this to set
3700    a new breakpoint at the target of the jmp_buf.
3701
3702    FIXME - This ought to be done by setting a temporary breakpoint
3703    that gets deleted automatically... */
3704
3705 void
3706 set_longjmp_resume_breakpoint (pc, frame)
3707      CORE_ADDR pc;
3708      struct frame_info *frame;
3709 {
3710   register struct breakpoint *b;
3711
3712   ALL_BREAKPOINTS (b)
3713     if (b->type == bp_longjmp_resume)
3714     {
3715       b->address = pc;
3716       b->enable = enabled;
3717       if (frame != NULL)
3718         b->frame = frame->frame;
3719       else
3720         b->frame = 0;
3721       check_duplicates (b->address, b->section);
3722       return;
3723     }
3724 }
3725
3726 void
3727 disable_watchpoints_before_interactive_call_start ()
3728 {
3729   struct breakpoint *b;
3730
3731   ALL_BREAKPOINTS (b)
3732   {
3733     if (((b->type == bp_watchpoint)
3734          || (b->type == bp_hardware_watchpoint)
3735          || (b->type == bp_read_watchpoint)
3736          || (b->type == bp_access_watchpoint)
3737          || ep_is_exception_catchpoint (b))
3738         && (b->enable == enabled))
3739       {
3740         b->enable = call_disabled;
3741         check_duplicates (b->address, b->section);
3742       }
3743   }
3744 }
3745
3746 void
3747 enable_watchpoints_after_interactive_call_stop ()
3748 {
3749   struct breakpoint *b;
3750
3751   ALL_BREAKPOINTS (b)
3752   {
3753     if (((b->type == bp_watchpoint)
3754          || (b->type == bp_hardware_watchpoint)
3755          || (b->type == bp_read_watchpoint)
3756          || (b->type == bp_access_watchpoint)
3757          || ep_is_exception_catchpoint (b))
3758         && (b->enable == call_disabled))
3759       {
3760         b->enable = enabled;
3761         check_duplicates (b->address, b->section);
3762       }
3763   }
3764 }
3765
3766
3767 /* Set a breakpoint that will evaporate an end of command
3768    at address specified by SAL.
3769    Restrict it to frame FRAME if FRAME is nonzero.  */
3770
3771 struct breakpoint *
3772 set_momentary_breakpoint (sal, frame, type)
3773      struct symtab_and_line sal;
3774      struct frame_info *frame;
3775      enum bptype type;
3776 {
3777   register struct breakpoint *b;
3778   b = set_raw_breakpoint (sal);
3779   b->type = type;
3780   b->enable = enabled;
3781   b->disposition = donttouch;
3782   b->frame = (frame ? frame->frame : 0);
3783
3784   /* If we're debugging a multi-threaded program, then we
3785      want momentary breakpoints to be active in only a 
3786      single thread of control.  */
3787   if (in_thread_list (inferior_pid))
3788     b->thread = pid_to_thread_id (inferior_pid);
3789
3790   return b;
3791 }
3792 \f
3793
3794 /* Tell the user we have just set a breakpoint B.  */
3795
3796 static void
3797 mention (b)
3798      struct breakpoint *b;
3799 {
3800   int say_where = 0;
3801
3802   /* FIXME: This is misplaced; mention() is called by things (like hitting a
3803      watchpoint) other than breakpoint creation.  It should be possible to
3804      clean this up and at the same time replace the random calls to
3805      breakpoint_changed with this hook, as has already been done for
3806      delete_breakpoint_hook and so on.  */
3807   if (create_breakpoint_hook)
3808     create_breakpoint_hook (b);
3809   breakpoint_create_event (b->number);
3810
3811   switch (b->type)
3812     {
3813     case bp_none:
3814       printf_filtered ("(apparently deleted?) Eventpoint %d: ", b->number);
3815       break;
3816     case bp_watchpoint:
3817       printf_filtered ("Watchpoint %d: ", b->number);
3818       print_expression (b->exp, gdb_stdout);
3819       break;
3820     case bp_hardware_watchpoint:
3821       printf_filtered ("Hardware watchpoint %d: ", b->number);
3822       print_expression (b->exp, gdb_stdout);
3823       break;
3824     case bp_read_watchpoint:
3825       printf_filtered ("Hardware read watchpoint %d: ", b->number);
3826       print_expression (b->exp, gdb_stdout);
3827       break;
3828     case bp_access_watchpoint:
3829       printf_filtered ("Hardware access (read/write) watchpoint %d: ", 
3830                        b->number);
3831       print_expression (b->exp, gdb_stdout);
3832       break;
3833     case bp_breakpoint:
3834       printf_filtered ("Breakpoint %d", b->number);
3835       say_where = 1;
3836       break;
3837     case bp_hardware_breakpoint:
3838       printf_filtered ("Hardware assisted breakpoint %d", b->number);
3839       say_where = 1;
3840       break;
3841     case bp_catch_load:
3842     case bp_catch_unload:
3843       printf_filtered ("Catchpoint %d (%s %s)",
3844                        b->number,
3845                        (b->type == bp_catch_load) ? "load" : "unload",
3846                        (b->dll_pathname != NULL) ? 
3847                        b->dll_pathname : "<any library>");
3848       break;
3849     case bp_catch_fork:
3850     case bp_catch_vfork:
3851       printf_filtered ("Catchpoint %d (%s)",
3852                        b->number,
3853                        (b->type == bp_catch_fork) ? "fork" : "vfork");
3854       break;
3855     case bp_catch_exec:
3856       printf_filtered ("Catchpoint %d (exec)",
3857                        b->number);
3858       break;
3859     case bp_catch_catch:
3860     case bp_catch_throw:
3861       printf_filtered ("Catchpoint %d (%s)",
3862                        b->number,
3863                        (b->type == bp_catch_catch) ? "catch" : "throw");
3864       break;
3865
3866     case bp_until:
3867     case bp_finish:
3868     case bp_longjmp:
3869     case bp_longjmp_resume:
3870     case bp_step_resume:
3871     case bp_through_sigtramp:
3872     case bp_call_dummy:
3873     case bp_watchpoint_scope:
3874     case bp_shlib_event:
3875       break;
3876     }
3877   if (say_where)
3878     {
3879       if (addressprint || b->source_file == NULL)
3880         {
3881           printf_filtered (" at ");
3882           print_address_numeric (b->address, 1, gdb_stdout);
3883         }
3884       if (b->source_file)
3885         printf_filtered (": file %s, line %d.",
3886                          b->source_file, b->line_number);
3887       TUIDO (((TuiOpaqueFuncPtr) tui_vAllSetHasBreakAt, b, 1));
3888       TUIDO (((TuiOpaqueFuncPtr) tuiUpdateAllExecInfos));
3889     }
3890   printf_filtered ("\n");
3891 }
3892 \f
3893
3894 /* Set a breakpoint according to ARG (function, linenum or *address)
3895    flag: first bit  : 0 non-temporary, 1 temporary.
3896    second bit : 0 normal breakpoint, 1 hardware breakpoint. */
3897
3898 static void
3899 break_command_1 (arg, flag, from_tty)
3900      char *arg;
3901      int flag, from_tty;
3902 {
3903   int tempflag, hardwareflag;
3904   struct symtabs_and_lines sals;
3905   struct symtab_and_line sal;
3906   register struct expression *cond = 0;
3907   register struct breakpoint *b;
3908
3909   /* Pointers in arg to the start, and one past the end, of the condition.  */
3910   char *cond_start = NULL;
3911   char *cond_end = NULL;
3912   /* Pointers in arg to the start, and one past the end,
3913      of the address part.  */
3914   char *addr_start = NULL;
3915   char *addr_end = NULL;
3916   struct cleanup *old_chain;
3917   struct cleanup *canonical_strings_chain = NULL;
3918   char **canonical = (char **) NULL;
3919   int i;
3920   int thread;
3921
3922   hardwareflag = flag & BP_HARDWAREFLAG;
3923   tempflag = flag & BP_TEMPFLAG;
3924
3925   sals.sals = NULL;
3926   sals.nelts = 0;
3927
3928   INIT_SAL (&sal);              /* initialize to zeroes */
3929
3930   /* If no arg given, or if first arg is 'if ', use the default breakpoint. */
3931
3932   if (!arg || (arg[0] == 'i' && arg[1] == 'f'
3933                && (arg[2] == ' ' || arg[2] == '\t')))
3934     {
3935       if (default_breakpoint_valid)
3936         {
3937           sals.sals = (struct symtab_and_line *)
3938             xmalloc (sizeof (struct symtab_and_line));
3939           sal.pc = default_breakpoint_address;
3940           sal.line = default_breakpoint_line;
3941           sal.symtab = default_breakpoint_symtab;
3942           sal.section = find_pc_overlay (sal.pc);
3943           sals.sals[0] = sal;
3944           sals.nelts = 1;
3945         }
3946       else
3947         error ("No default breakpoint address now.");
3948     }
3949   else
3950     {
3951       addr_start = arg;
3952
3953       /* Force almost all breakpoints to be in terms of the
3954          current_source_symtab (which is decode_line_1's default).  This
3955          should produce the results we want almost all of the time while
3956          leaving default_breakpoint_* alone.  */
3957       if (default_breakpoint_valid
3958           && (!current_source_symtab
3959               || (arg && (*arg == '+' || *arg == '-'))))
3960         sals = decode_line_1 (&arg, 1, default_breakpoint_symtab,
3961                               default_breakpoint_line, &canonical);
3962       else
3963         sals = decode_line_1 (&arg, 1, (struct symtab *) NULL, 0, &canonical);
3964
3965       addr_end = arg;
3966     }
3967
3968   if (!sals.nelts)
3969     return;
3970
3971   /* Make sure that all storage allocated in decode_line_1 gets freed
3972      in case the following `for' loop errors out.  */
3973   old_chain = make_cleanup (free, sals.sals);
3974   if (canonical != (char **) NULL)
3975     {
3976       make_cleanup (free, canonical);
3977       canonical_strings_chain = make_cleanup (null_cleanup, 0);
3978       for (i = 0; i < sals.nelts; i++)
3979         {
3980           if (canonical[i] != NULL)
3981             make_cleanup (free, canonical[i]);
3982         }
3983     }
3984
3985   thread = -1;                  /* No specific thread yet */
3986
3987   /* Resolve all line numbers to PC's, and verify that conditions
3988      can be parsed, before setting any breakpoints.  */
3989   for (i = 0; i < sals.nelts; i++)
3990     {
3991       char *tok, *end_tok;
3992       int toklen;
3993
3994       resolve_sal_pc (&sals.sals[i]);
3995
3996       /* It's possible for the PC to be nonzero, but still an illegal
3997          value on some targets.
3998
3999          For example, on HP-UX if you start gdb, and before running the
4000          inferior you try to set a breakpoint on a shared library function
4001          "foo" where the inferior doesn't call "foo" directly but does
4002          pass its address to another function call, then we do find a
4003          minimal symbol for the "foo", but it's address is invalid.
4004          (Appears to be an index into a table that the loader sets up
4005          when the inferior is run.)
4006
4007          Give the target a chance to bless sals.sals[i].pc before we
4008          try to make a breakpoint for it. */
4009       if (PC_REQUIRES_RUN_BEFORE_USE (sals.sals[i].pc))
4010         {
4011           error ("Cannot break on %s without a running program.", 
4012                  addr_start);
4013         }
4014
4015       tok = arg;
4016
4017       while (tok && *tok)
4018         {
4019           while (*tok == ' ' || *tok == '\t')
4020             tok++;
4021
4022           end_tok = tok;
4023
4024           while (*end_tok != ' ' && *end_tok != '\t' && *end_tok != '\000')
4025             end_tok++;
4026
4027           toklen = end_tok - tok;
4028
4029           if (toklen >= 1 && strncmp (tok, "if", toklen) == 0)
4030             {
4031               tok = cond_start = end_tok + 1;
4032               cond = parse_exp_1 (&tok, block_for_pc (sals.sals[i].pc), 0);
4033               cond_end = tok;
4034             }
4035           else if (toklen >= 1 && strncmp (tok, "thread", toklen) == 0)
4036             {
4037               char *tmptok;
4038
4039               tok = end_tok + 1;
4040               tmptok = tok;
4041               thread = strtol (tok, &tok, 0);
4042               if (tok == tmptok)
4043                 error ("Junk after thread keyword.");
4044               if (!valid_thread_id (thread))
4045                 error ("Unknown thread %d\n", thread);
4046             }
4047           else
4048             error ("Junk at end of arguments.");
4049         }
4050     }
4051   if (hardwareflag)
4052     {
4053       int i, target_resources_ok;
4054
4055       i = hw_breakpoint_used_count ();
4056       target_resources_ok = 
4057         TARGET_CAN_USE_HARDWARE_WATCHPOINT (bp_hardware_breakpoint, 
4058                                             i + sals.nelts, 0);
4059       if (target_resources_ok == 0)
4060         error ("No hardware breakpoint support in the target.");
4061       else if (target_resources_ok < 0)
4062         error ("Hardware breakpoints used exceeds limit.");
4063     }
4064
4065   /* Remove the canonical strings from the cleanup, they are needed below.  */
4066   if (canonical != (char **) NULL)
4067     discard_cleanups (canonical_strings_chain);
4068
4069   /* Now set all the breakpoints.  */
4070   for (i = 0; i < sals.nelts; i++)
4071     {
4072       sal = sals.sals[i];
4073
4074       if (from_tty)
4075         describe_other_breakpoints (sal.pc, sal.section);
4076
4077       b = set_raw_breakpoint (sal);
4078       set_breakpoint_count (breakpoint_count + 1);
4079       b->number = breakpoint_count;
4080       b->type = hardwareflag ? bp_hardware_breakpoint : bp_breakpoint;
4081       b->cond = cond;
4082       b->thread = thread;
4083
4084       /* If a canonical line spec is needed use that instead of the
4085          command string.  */
4086       if (canonical != (char **) NULL && canonical[i] != NULL)
4087         b->addr_string = canonical[i];
4088       else if (addr_start)
4089         b->addr_string = savestring (addr_start, addr_end - addr_start);
4090       if (cond_start)
4091         b->cond_string = savestring (cond_start, cond_end - cond_start);
4092
4093       b->enable = enabled;
4094       b->disposition = tempflag ? del : donttouch;
4095       mention (b);
4096     }
4097
4098   if (sals.nelts > 1)
4099     {
4100       warning ("Multiple breakpoints were set.");
4101       warning ("Use the \"delete\" command to delete unwanted breakpoints.");
4102     }
4103   do_cleanups (old_chain);
4104 }
4105
4106 static void
4107 break_at_finish_at_depth_command_1 (arg, flag, from_tty)
4108      char *arg;
4109      int flag;
4110      int from_tty;
4111 {
4112   struct frame_info *frame;
4113   CORE_ADDR low, high, selected_pc = 0;
4114   char *extra_args, *level_arg, *addr_string;
4115   int extra_args_len = 0, if_arg = 0;
4116
4117   if (!arg ||
4118       (arg[0] == 'i' && arg[1] == 'f' && (arg[2] == ' ' || arg[2] == '\t')))
4119     {
4120
4121       if (default_breakpoint_valid)
4122         {
4123           if (selected_frame)
4124             {
4125               selected_pc = selected_frame->pc;
4126               if (arg)
4127                 if_arg = 1;
4128             }
4129           else
4130             error ("No selected frame.");
4131         }
4132       else
4133         error ("No default breakpoint address now.");
4134     }
4135   else
4136     {
4137       extra_args = strchr (arg, ' ');
4138       if (extra_args)
4139         {
4140           extra_args++;
4141           extra_args_len = strlen (extra_args);
4142           level_arg = (char *) xmalloc (extra_args - arg);
4143           strncpy (level_arg, arg, extra_args - arg - 1);
4144           level_arg[extra_args - arg - 1] = '\0';
4145         }
4146       else
4147         {
4148           level_arg = (char *) xmalloc (strlen (arg) + 1);
4149           strcpy (level_arg, arg);
4150         }
4151
4152       frame = parse_frame_specification (level_arg);
4153       if (frame)
4154         selected_pc = frame->pc;
4155       else
4156         selected_pc = 0;
4157     }
4158   if (if_arg)
4159     {
4160       extra_args = arg;
4161       extra_args_len = strlen (arg);
4162     }
4163
4164   if (selected_pc)
4165     {
4166       if (find_pc_partial_function (selected_pc, (char **) NULL, &low, &high))
4167         {
4168           addr_string = (char *) xmalloc (26 + extra_args_len);
4169           if (extra_args_len)
4170             sprintf (addr_string, "*0x%s %s", paddr_nz (high), extra_args);
4171           else
4172             sprintf (addr_string, "*0x%s", paddr_nz (high));
4173           break_command_1 (addr_string, flag, from_tty);
4174           free (addr_string);
4175         }
4176       else
4177         error ("No function contains the specified address");
4178     }
4179   else
4180     error ("Unable to set breakpoint at procedure exit");
4181 }
4182
4183
4184 static void
4185 break_at_finish_command_1 (arg, flag, from_tty)
4186      char *arg;
4187      int flag;
4188      int from_tty;
4189 {
4190   char *addr_string, *break_string, *beg_addr_string;
4191   CORE_ADDR low, high;
4192   struct symtabs_and_lines sals;
4193   struct symtab_and_line sal;
4194   struct cleanup *old_chain;
4195   char *extra_args;
4196   int extra_args_len = 0;
4197   int i, if_arg = 0;
4198
4199   if (!arg ||
4200       (arg[0] == 'i' && arg[1] == 'f' && (arg[2] == ' ' || arg[2] == '\t')))
4201     {
4202       if (default_breakpoint_valid)
4203         {
4204           if (selected_frame)
4205             {
4206               addr_string = (char *) xmalloc (15);
4207               sprintf (addr_string, "*0x%s", paddr_nz (selected_frame->pc));
4208               if (arg)
4209                 if_arg = 1;
4210             }
4211           else
4212             error ("No selected frame.");
4213         }
4214       else
4215         error ("No default breakpoint address now.");
4216     }
4217   else
4218     {
4219       addr_string = (char *) xmalloc (strlen (arg) + 1);
4220       strcpy (addr_string, arg);
4221     }
4222
4223   if (if_arg)
4224     {
4225       extra_args = arg;
4226       extra_args_len = strlen (arg);
4227     }
4228   else if (arg)
4229     {
4230       /* get the stuff after the function name or address */
4231       extra_args = strchr (arg, ' ');
4232       if (extra_args)
4233         {
4234           extra_args++;
4235           extra_args_len = strlen (extra_args);
4236         }
4237     }
4238
4239   sals.sals = NULL;
4240   sals.nelts = 0;
4241
4242   beg_addr_string = addr_string;
4243   sals = decode_line_1 (&addr_string, 1, (struct symtab *) NULL, 0,
4244                         (char ***) NULL);
4245
4246   free (beg_addr_string);
4247   old_chain = make_cleanup (free, sals.sals);
4248   for (i = 0; (i < sals.nelts); i++)
4249     {
4250       sal = sals.sals[i];
4251       if (find_pc_partial_function (sal.pc, (char **) NULL, &low, &high))
4252         {
4253           break_string = (char *) xmalloc (extra_args_len + 26);
4254           if (extra_args_len)
4255             sprintf (break_string, "*0x%s %s", paddr_nz (high), extra_args);
4256           else
4257             sprintf (break_string, "*0x%s", paddr_nz (high));
4258           break_command_1 (break_string, flag, from_tty);
4259           free (break_string);
4260         }
4261       else
4262         error ("No function contains the specified address");
4263     }
4264   if (sals.nelts > 1)
4265     {
4266       warning ("Multiple breakpoints were set.\n");
4267       warning ("Use the \"delete\" command to delete unwanted breakpoints.");
4268     }
4269   do_cleanups (old_chain);
4270 }
4271
4272
4273 /* Helper function for break_command_1 and disassemble_command.  */
4274
4275 void
4276 resolve_sal_pc (sal)
4277      struct symtab_and_line *sal;
4278 {
4279   CORE_ADDR pc;
4280
4281   if (sal->pc == 0 && sal->symtab != NULL)
4282     {
4283       if (!find_line_pc (sal->symtab, sal->line, &pc))
4284         error ("No line %d in file \"%s\".",
4285                sal->line, sal->symtab->filename);
4286       sal->pc = pc;
4287     }
4288
4289   if (sal->section == 0 && sal->symtab != NULL)
4290     {
4291       struct blockvector *bv;
4292       struct block *b;
4293       struct symbol *sym;
4294       int index;
4295
4296       bv = blockvector_for_pc_sect (sal->pc, 0, &index, sal->symtab);
4297       if (bv != NULL)
4298         {
4299           b = BLOCKVECTOR_BLOCK (bv, index);
4300           sym = block_function (b);
4301           if (sym != NULL)
4302             {
4303               fixup_symbol_section (sym, sal->symtab->objfile);
4304               sal->section = SYMBOL_BFD_SECTION (sym);
4305             }
4306           else
4307             {
4308               /* It really is worthwhile to have the section, so we'll just
4309                  have to look harder. This case can be executed if we have 
4310                  line numbers but no functions (as can happen in assembly 
4311                  source).  */
4312
4313               struct minimal_symbol *msym;
4314
4315               msym = lookup_minimal_symbol_by_pc (sal->pc);
4316               if (msym)
4317                 sal->section = SYMBOL_BFD_SECTION (msym);
4318             }
4319         }
4320     }
4321 }
4322
4323 void
4324 break_command (arg, from_tty)
4325      char *arg;
4326      int from_tty;
4327 {
4328   break_command_1 (arg, 0, from_tty);
4329 }
4330
4331 static void
4332 break_at_finish_command (arg, from_tty)
4333      char *arg;
4334      int from_tty;
4335 {
4336   break_at_finish_command_1 (arg, 0, from_tty);
4337 }
4338
4339 static void
4340 break_at_finish_at_depth_command (arg, from_tty)
4341      char *arg;
4342      int from_tty;
4343 {
4344   break_at_finish_at_depth_command_1 (arg, 0, from_tty);
4345 }
4346
4347 void
4348 tbreak_command (arg, from_tty)
4349      char *arg;
4350      int from_tty;
4351 {
4352   break_command_1 (arg, BP_TEMPFLAG, from_tty);
4353 }
4354
4355 static void
4356 tbreak_at_finish_command (arg, from_tty)
4357      char *arg;
4358      int from_tty;
4359 {
4360   break_at_finish_command_1 (arg, BP_TEMPFLAG, from_tty);
4361 }
4362
4363 static void
4364 hbreak_command (arg, from_tty)
4365      char *arg;
4366      int from_tty;
4367 {
4368   break_command_1 (arg, BP_HARDWAREFLAG, from_tty);
4369 }
4370
4371 static void
4372 thbreak_command (arg, from_tty)
4373      char *arg;
4374      int from_tty;
4375 {
4376   break_command_1 (arg, (BP_TEMPFLAG | BP_HARDWAREFLAG), from_tty);
4377 }
4378
4379 static void
4380 stop_command (arg, from_tty)
4381      char *arg;
4382      int from_tty;
4383 {
4384   printf_filtered ("Specify the type of breakpoint to set.\n\
4385 Usage: stop in <function | address>\n\
4386        stop at <line>\n");
4387 }
4388
4389 static void
4390 stopin_command (arg, from_tty)
4391      char *arg;
4392      int from_tty;
4393 {
4394   int badInput = 0;
4395
4396   if (arg == (char *) NULL)
4397     badInput = 1;
4398   else if (*arg != '*')
4399     {
4400       char *argptr = arg;
4401       int hasColon = 0;
4402
4403       /* look for a ':'.  If this is a line number specification, then
4404          say it is bad, otherwise, it should be an address or
4405          function/method name */
4406       while (*argptr && !hasColon)
4407         {
4408           hasColon = (*argptr == ':');
4409           argptr++;
4410         }
4411
4412       if (hasColon)
4413         badInput = (*argptr != ':');    /* Not a class::method */
4414       else
4415         badInput = isdigit (*arg);      /* a simple line number */
4416     }
4417
4418   if (badInput)
4419     printf_filtered ("Usage: stop in <function | address>\n");
4420   else
4421     break_command_1 (arg, 0, from_tty);
4422 }
4423
4424 static void
4425 stopat_command (arg, from_tty)
4426      char *arg;
4427      int from_tty;
4428 {
4429   int badInput = 0;
4430
4431   if (arg == (char *) NULL || *arg == '*')      /* no line number */
4432     badInput = 1;
4433   else
4434     {
4435       char *argptr = arg;
4436       int hasColon = 0;
4437
4438       /* look for a ':'.  If there is a '::' then get out, otherwise
4439          it is probably a line number. */
4440       while (*argptr && !hasColon)
4441         {
4442           hasColon = (*argptr == ':');
4443           argptr++;
4444         }
4445
4446       if (hasColon)
4447         badInput = (*argptr == ':');    /* we have class::method */
4448       else
4449         badInput = !isdigit (*arg);     /* not a line number */
4450     }
4451
4452   if (badInput)
4453     printf_filtered ("Usage: stop at <line>\n");
4454   else
4455     break_command_1 (arg, 0, from_tty);
4456 }
4457
4458 /* ARGSUSED */
4459 /* accessflag:  hw_write:  watch write, 
4460                 hw_read:   watch read, 
4461                 hw_access: watch access (read or write) */
4462 static void
4463 watch_command_1 (arg, accessflag, from_tty)
4464      char *arg;
4465      int accessflag;
4466      int from_tty;
4467 {
4468   struct breakpoint *b;
4469   struct symtab_and_line sal;
4470   struct expression *exp;
4471   struct block *exp_valid_block;
4472   struct value *val, *mark;
4473   struct frame_info *frame;
4474   struct frame_info *prev_frame = NULL;
4475   char *exp_start = NULL;
4476   char *exp_end = NULL;
4477   char *tok, *end_tok;
4478   int toklen;
4479   char *cond_start = NULL;
4480   char *cond_end = NULL;
4481   struct expression *cond = NULL;
4482   int i, other_type_used, target_resources_ok = 0;
4483   enum bptype bp_type;
4484   int mem_cnt = 0;
4485
4486   INIT_SAL (&sal);              /* initialize to zeroes */
4487
4488   /* Parse arguments.  */
4489   innermost_block = NULL;
4490   exp_start = arg;
4491   exp = parse_exp_1 (&arg, 0, 0);
4492   exp_end = arg;
4493   exp_valid_block = innermost_block;
4494   mark = value_mark ();
4495   val = evaluate_expression (exp);
4496   release_value (val);
4497   if (VALUE_LAZY (val))
4498     value_fetch_lazy (val);
4499
4500   tok = arg;
4501   while (*tok == ' ' || *tok == '\t')
4502     tok++;
4503   end_tok = tok;
4504
4505   while (*end_tok != ' ' && *end_tok != '\t' && *end_tok != '\000')
4506     end_tok++;
4507
4508   toklen = end_tok - tok;
4509   if (toklen >= 1 && strncmp (tok, "if", toklen) == 0)
4510     {
4511       tok = cond_start = end_tok + 1;
4512       cond = parse_exp_1 (&tok, 0, 0);
4513       cond_end = tok;
4514     }
4515   if (*tok)
4516     error ("Junk at end of command.");
4517
4518   if (accessflag == hw_read)
4519     bp_type = bp_read_watchpoint;
4520   else if (accessflag == hw_access)
4521     bp_type = bp_access_watchpoint;
4522   else
4523     bp_type = bp_hardware_watchpoint;
4524
4525   mem_cnt = can_use_hardware_watchpoint (val);
4526   if (mem_cnt == 0 && bp_type != bp_hardware_watchpoint)
4527     error ("Expression cannot be implemented with read/access watchpoint.");
4528   if (mem_cnt != 0)
4529     {
4530       i = hw_watchpoint_used_count (bp_type, &other_type_used);
4531       target_resources_ok = 
4532         TARGET_CAN_USE_HARDWARE_WATCHPOINT (bp_type, i + mem_cnt, 
4533                                             other_type_used);
4534       if (target_resources_ok == 0 && bp_type != bp_hardware_watchpoint)
4535         error ("Target does not support this type of hardware watchpoint.");
4536
4537       if (target_resources_ok < 0 && bp_type != bp_hardware_watchpoint)
4538         error ("Target can only support one kind of HW watchpoint at a time.");
4539     }
4540
4541 #if defined(HPUXHPPA)
4542   /*  On HP-UX if you set a h/w
4543      watchpoint before the "run" command, the inferior dies with a e.g.,
4544      SIGILL once you start it.  I initially believed this was due to a
4545      bad interaction between page protection traps and the initial
4546      startup sequence by the dynamic linker.
4547
4548      However, I tried avoiding that by having HP-UX's implementation of
4549      TARGET_CAN_USE_HW_WATCHPOINT return FALSE if there was no inferior_pid
4550      yet, which forced slow watches before a "run" or "attach", and it
4551      still fails somewhere in the startup code.
4552
4553      Until I figure out what's happening, I'm disallowing watches altogether
4554      before the "run" or "attach" command.  We'll tell the user they must
4555      set watches after getting the program started. */
4556   if (!target_has_execution)
4557     {
4558       warning ("can't do that without a running program; try \"break main\", \"run\" first");
4559       return;
4560     }
4561 #endif /* HPUXHPPA */
4562
4563   /* Now set up the breakpoint.  */
4564   b = set_raw_breakpoint (sal);
4565   set_breakpoint_count (breakpoint_count + 1);
4566   b->number = breakpoint_count;
4567   b->disposition = donttouch;
4568   b->exp = exp;
4569   b->exp_valid_block = exp_valid_block;
4570   b->exp_string = savestring (exp_start, exp_end - exp_start);
4571   b->val = val;
4572   b->cond = cond;
4573   if (cond_start)
4574     b->cond_string = savestring (cond_start, cond_end - cond_start);
4575   else
4576     b->cond_string = 0;
4577
4578   frame = block_innermost_frame (exp_valid_block);
4579   if (frame)
4580     {
4581       prev_frame = get_prev_frame (frame);
4582       b->watchpoint_frame = frame->frame;
4583     }
4584   else
4585     b->watchpoint_frame = (CORE_ADDR) 0;
4586
4587   if (mem_cnt && target_resources_ok > 0)
4588     b->type = bp_type;
4589   else
4590     b->type = bp_watchpoint;
4591
4592   /* If the expression is "local", then set up a "watchpoint scope"
4593      breakpoint at the point where we've left the scope of the watchpoint
4594      expression.  */
4595   if (innermost_block)
4596     {
4597       if (prev_frame)
4598         {
4599           struct breakpoint *scope_breakpoint;
4600           struct symtab_and_line scope_sal;
4601
4602           INIT_SAL (&scope_sal);        /* initialize to zeroes */
4603           scope_sal.pc = get_frame_pc (prev_frame);
4604           scope_sal.section = find_pc_overlay (scope_sal.pc);
4605
4606           scope_breakpoint = set_raw_breakpoint (scope_sal);
4607           set_breakpoint_count (breakpoint_count + 1);
4608           scope_breakpoint->number = breakpoint_count;
4609
4610           scope_breakpoint->type = bp_watchpoint_scope;
4611           scope_breakpoint->enable = enabled;
4612
4613           /* Automatically delete the breakpoint when it hits.  */
4614           scope_breakpoint->disposition = del;
4615
4616           /* Only break in the proper frame (help with recursion).  */
4617           scope_breakpoint->frame = prev_frame->frame;
4618
4619           /* Set the address at which we will stop.  */
4620           scope_breakpoint->address = get_frame_pc (prev_frame);
4621
4622           /* The scope breakpoint is related to the watchpoint.  We
4623              will need to act on them together.  */
4624           b->related_breakpoint = scope_breakpoint;
4625         }
4626     }
4627   value_free_to_mark (mark);
4628   mention (b);
4629 }
4630
4631 /* Return count of locations need to be watched and can be handled
4632    in hardware.  If the watchpoint can not be handled
4633    in hardware return zero.  */
4634
4635 #if !defined(TARGET_REGION_SIZE_OK_FOR_HW_WATCHPOINT)
4636 #define TARGET_REGION_SIZE_OK_FOR_HW_WATCHPOINT(BYTE_SIZE) \
4637     ((BYTE_SIZE) <= (REGISTER_SIZE))
4638 #endif
4639
4640 #if !defined(TARGET_REGION_OK_FOR_HW_WATCHPOINT)
4641 #define TARGET_REGION_OK_FOR_HW_WATCHPOINT(ADDR,LEN) \
4642      TARGET_REGION_SIZE_OK_FOR_HW_WATCHPOINT(LEN)
4643 #endif
4644
4645 static int
4646 can_use_hardware_watchpoint (v)
4647      struct value *v;
4648 {
4649   int found_memory_cnt = 0;
4650
4651   /* Did the user specifically forbid us to use hardware watchpoints? */
4652   if (!can_use_hw_watchpoints)
4653     return 0;
4654
4655   /* Make sure all the intermediate values are in memory.  Also make sure
4656      we found at least one memory expression.  Guards against watch 0x12345,
4657      which is meaningless, but could cause errors if one tries to insert a 
4658      hardware watchpoint for the constant expression.  */
4659   for (; v; v = v->next)
4660     {
4661       if (v->lval == lval_memory)
4662         {
4663           CORE_ADDR vaddr = VALUE_ADDRESS (v) + VALUE_OFFSET (v);
4664           int       len   = TYPE_LENGTH (VALUE_TYPE (v));
4665
4666           if (!TARGET_REGION_OK_FOR_HW_WATCHPOINT (vaddr, len))
4667             return 0;
4668           else
4669             found_memory_cnt++;
4670         }
4671       else if (v->lval != not_lval && v->modifiable == 0)
4672         return 0;       /* ??? What does this represent? */
4673       else if (v->lval == lval_register)
4674         return 0;       /* cannot watch a register with a HW watchpoint */
4675     }
4676
4677   /* The expression itself looks suitable for using a hardware
4678      watchpoint, but give the target machine a chance to reject it.  */
4679   return found_memory_cnt;
4680 }
4681
4682 static void
4683 watch_command (arg, from_tty)
4684      char *arg;
4685      int from_tty;
4686 {
4687   watch_command_1 (arg, hw_write, from_tty);
4688 }
4689
4690 static void
4691 rwatch_command (arg, from_tty)
4692      char *arg;
4693      int from_tty;
4694 {
4695   watch_command_1 (arg, hw_read, from_tty);
4696 }
4697
4698 static void
4699 awatch_command (arg, from_tty)
4700      char *arg;
4701      int from_tty;
4702 {
4703   watch_command_1 (arg, hw_access, from_tty);
4704 }
4705 \f
4706
4707 /* Helper routines for the until_command routine in infcmd.c.  Here
4708    because it uses the mechanisms of breakpoints.  */
4709
4710 /* This function is called by fetch_inferior_event via the
4711    cmd_continuation pointer, to complete the until command. It takes
4712    care of cleaning up the temporary breakpoints set up by the until
4713    command. */
4714 static void
4715 until_break_command_continuation (struct continuation_arg *arg)
4716 {
4717   /* Do all the exec cleanups, which at this point should only be the
4718      one set up in the first part of the until_break_command
4719      function. */
4720   do_exec_cleanups (ALL_CLEANUPS);
4721 }
4722
4723 /* ARGSUSED */
4724 void
4725 until_break_command (arg, from_tty)
4726      char *arg;
4727      int from_tty;
4728 {
4729   struct symtabs_and_lines sals;
4730   struct symtab_and_line sal;
4731   struct frame_info *prev_frame = get_prev_frame (selected_frame);
4732   struct breakpoint *breakpoint;
4733   struct cleanup *old_chain;
4734
4735   clear_proceed_status ();
4736
4737   /* Set a breakpoint where the user wants it and at return from
4738      this function */
4739
4740   if (default_breakpoint_valid)
4741     sals = decode_line_1 (&arg, 1, default_breakpoint_symtab,
4742                           default_breakpoint_line, (char ***) NULL);
4743   else
4744     sals = decode_line_1 (&arg, 1, (struct symtab *) NULL, 
4745                           0, (char ***) NULL);
4746
4747   if (sals.nelts != 1)
4748     error ("Couldn't get information on specified line.");
4749
4750   sal = sals.sals[0];
4751   free ((PTR) sals.sals);       /* malloc'd, so freed */
4752
4753   if (*arg)
4754     error ("Junk at end of arguments.");
4755
4756   resolve_sal_pc (&sal);
4757
4758   breakpoint = set_momentary_breakpoint (sal, selected_frame, bp_until);
4759
4760   if (!async_p || !target_has_async)
4761     old_chain = make_cleanup ((make_cleanup_func) delete_breakpoint, 
4762                               breakpoint);
4763   else
4764     make_exec_cleanup ((make_cleanup_func) delete_breakpoint, breakpoint);
4765
4766   /* If we are running asynchronously, and the target supports async
4767      execution, we are not waiting for the target to stop, in the call
4768      tp proceed, below. This means that we cannot delete the
4769      brekpoints until the target has actually stopped. The only place
4770      where we get a chance to do that is in fetch_inferior_event, so
4771      we must set things up for that. */
4772
4773   if (async_p && target_has_async)
4774     {
4775       /* In this case we don't need args for the continuation, because
4776          all it needs to do is do the cleanups in the
4777          exec_cleanup_chain, which will be only those inserted by this
4778          function. We can get away by using ALL_CLEANUPS. */
4779       add_continuation (until_break_command_continuation, NULL);
4780     }
4781
4782   /* Keep within the current frame */
4783
4784   if (prev_frame)
4785     {
4786       sal = find_pc_line (prev_frame->pc, 0);
4787       sal.pc = prev_frame->pc;
4788       breakpoint = set_momentary_breakpoint (sal, prev_frame, bp_until);
4789       if (!async_p || !target_has_async)
4790         make_cleanup ((make_cleanup_func) delete_breakpoint, breakpoint);
4791       else
4792         make_exec_cleanup ((make_cleanup_func) delete_breakpoint, breakpoint);
4793     }
4794
4795   proceed (-1, TARGET_SIGNAL_DEFAULT, 0);
4796   /* Do the cleanups now, anly if we are not running asynchronously,
4797      of if we are, but the target is still synchronous. */
4798   if (!async_p || !target_has_async)
4799     do_cleanups (old_chain);
4800 }
4801 \f
4802 #if 0
4803 /* These aren't used; I don't konw what they were for.  */
4804 /* Set a breakpoint at the catch clause for NAME.  */
4805 static int
4806 catch_breakpoint (name)
4807      char *name;
4808 {
4809 }
4810
4811 static int
4812 disable_catch_breakpoint ()
4813 {
4814 }
4815
4816 static int
4817 delete_catch_breakpoint ()
4818 {
4819 }
4820
4821 static int
4822 enable_catch_breakpoint ()
4823 {
4824 }
4825 #endif /* 0 */
4826
4827 struct sal_chain
4828 {
4829   struct sal_chain *next;
4830   struct symtab_and_line sal;
4831 };
4832
4833 #if 0
4834 /* Not really used -- invocation in handle_gnu_4_16_catch_command
4835    had been commented out in the v.4.16 sources, and stays
4836    disabled there now because "catch NAME" syntax isn't allowed.
4837    pai/1997-07-11 */
4838 /* This isn't used; I don't know what it was for.  */
4839 /* For each catch clause identified in ARGS, run FUNCTION
4840    with that clause as an argument.  */
4841 static struct symtabs_and_lines
4842 map_catch_names (args, function)
4843      char *args;
4844      int (*function) ();
4845 {
4846   register char *p = args;
4847   register char *p1;
4848   struct symtabs_and_lines sals;
4849 #if 0
4850   struct sal_chain *sal_chain = 0;
4851 #endif
4852
4853   if (p == 0)
4854     error_no_arg ("one or more catch names");
4855
4856   sals.nelts = 0;
4857   sals.sals = NULL;
4858
4859   while (*p)
4860     {
4861       p1 = p;
4862       /* Don't swallow conditional part.  */
4863       if (p1[0] == 'i' && p1[1] == 'f'
4864           && (p1[2] == ' ' || p1[2] == '\t'))
4865         break;
4866
4867       if (isalpha (*p1))
4868         {
4869           p1++;
4870           while (isalnum (*p1) || *p1 == '_' || *p1 == '$')
4871             p1++;
4872         }
4873
4874       if (*p1 && *p1 != ' ' && *p1 != '\t')
4875         error ("Arguments must be catch names.");
4876
4877       *p1 = 0;
4878 #if 0
4879       if (function (p))
4880         {
4881           struct sal_chain *next = (struct sal_chain *)
4882           alloca (sizeof (struct sal_chain));
4883           next->next = sal_chain;
4884           next->sal = get_catch_sal (p);
4885           sal_chain = next;
4886           goto win;
4887         }
4888 #endif
4889       printf_unfiltered ("No catch clause for exception %s.\n", p);
4890 #if 0
4891     win:
4892 #endif
4893       p = p1;
4894       while (*p == ' ' || *p == '\t')
4895         p++;
4896     }
4897 }
4898 #endif
4899
4900 /* This shares a lot of code with `print_frame_label_vars' from stack.c.  */
4901
4902 static struct symtabs_and_lines
4903 get_catch_sals (this_level_only)
4904      int this_level_only;
4905 {
4906   register struct blockvector *bl;
4907   register struct block *block;
4908   int index, have_default = 0;
4909   CORE_ADDR pc;
4910   struct symtabs_and_lines sals;
4911   struct sal_chain *sal_chain = 0;
4912   char *blocks_searched;
4913
4914   /* Not sure whether an error message is always the correct response,
4915      but it's better than a core dump.  */
4916   if (selected_frame == NULL)
4917     error ("No selected frame.");
4918   block = get_frame_block (selected_frame);
4919   pc = selected_frame->pc;
4920
4921   sals.nelts = 0;
4922   sals.sals = NULL;
4923
4924   if (block == 0)
4925     error ("No symbol table info available.\n");
4926
4927   bl = blockvector_for_pc (BLOCK_END (block) - 4, &index);
4928   blocks_searched = (char *) alloca (BLOCKVECTOR_NBLOCKS (bl) * sizeof (char));
4929   memset (blocks_searched, 0, BLOCKVECTOR_NBLOCKS (bl) * sizeof (char));
4930
4931   while (block != 0)
4932     {
4933       CORE_ADDR end = BLOCK_END (block) - 4;
4934       int last_index;
4935
4936       if (bl != blockvector_for_pc (end, &index))
4937         error ("blockvector blotch");
4938       if (BLOCKVECTOR_BLOCK (bl, index) != block)
4939         error ("blockvector botch");
4940       last_index = BLOCKVECTOR_NBLOCKS (bl);
4941       index += 1;
4942
4943       /* Don't print out blocks that have gone by.  */
4944       while (index < last_index
4945              && BLOCK_END (BLOCKVECTOR_BLOCK (bl, index)) < pc)
4946         index++;
4947
4948       while (index < last_index
4949              && BLOCK_END (BLOCKVECTOR_BLOCK (bl, index)) < end)
4950         {
4951           if (blocks_searched[index] == 0)
4952             {
4953               struct block *b = BLOCKVECTOR_BLOCK (bl, index);
4954               int nsyms;
4955               register int i;
4956               register struct symbol *sym;
4957
4958               nsyms = BLOCK_NSYMS (b);
4959
4960               for (i = 0; i < nsyms; i++)
4961                 {
4962                   sym = BLOCK_SYM (b, i);
4963                   if (STREQ (SYMBOL_NAME (sym), "default"))
4964                     {
4965                       if (have_default)
4966                         continue;
4967                       have_default = 1;
4968                     }
4969                   if (SYMBOL_CLASS (sym) == LOC_LABEL)
4970                     {
4971                       struct sal_chain *next = (struct sal_chain *)
4972                       alloca (sizeof (struct sal_chain));
4973                       next->next = sal_chain;
4974                       next->sal = find_pc_line (SYMBOL_VALUE_ADDRESS (sym), 
4975                                                 0);
4976                       sal_chain = next;
4977                     }
4978                 }
4979               blocks_searched[index] = 1;
4980             }
4981           index++;
4982         }
4983       if (have_default)
4984         break;
4985       if (sal_chain && this_level_only)
4986         break;
4987
4988       /* After handling the function's top-level block, stop.
4989          Don't continue to its superblock, the block of
4990          per-file symbols.  */
4991       if (BLOCK_FUNCTION (block))
4992         break;
4993       block = BLOCK_SUPERBLOCK (block);
4994     }
4995
4996   if (sal_chain)
4997     {
4998       struct sal_chain *tmp_chain;
4999
5000       /* Count the number of entries.  */
5001       for (index = 0, tmp_chain = sal_chain; tmp_chain;
5002            tmp_chain = tmp_chain->next)
5003         index++;
5004
5005       sals.nelts = index;
5006       sals.sals = (struct symtab_and_line *)
5007         xmalloc (index * sizeof (struct symtab_and_line));
5008       for (index = 0; sal_chain; sal_chain = sal_chain->next, index++)
5009         sals.sals[index] = sal_chain->sal;
5010     }
5011
5012   return sals;
5013 }
5014
5015 static void
5016 ep_skip_leading_whitespace (s)
5017      char **s;
5018 {
5019   if ((s == NULL) || (*s == NULL))
5020     return;
5021   while (isspace (**s))
5022     *s += 1;
5023 }
5024
5025 /* This function examines a string, and attempts to find a token
5026    that might be an event name in the leading characters.  If a
5027    possible match is found, a pointer to the last character of
5028    the token is returned.  Else, NULL is returned. */
5029
5030 static char *
5031 ep_find_event_name_end (arg)
5032      char *arg;
5033 {
5034   char *s = arg;
5035   char *event_name_end = NULL;
5036
5037   /* If we could depend upon the presense of strrpbrk, we'd use that... */
5038   if (arg == NULL)
5039     return NULL;
5040
5041   /* We break out of the loop when we find a token delimiter.
5042      Basically, we're looking for alphanumerics and underscores;
5043      anything else delimites the token. */
5044   while (*s != '\0')
5045     {
5046       if (!isalnum (*s) && (*s != '_'))
5047         break;
5048       event_name_end = s;
5049       s++;
5050     }
5051
5052   return event_name_end;
5053 }
5054
5055
5056 /* This function attempts to parse an optional "if <cond>" clause
5057    from the arg string.  If one is not found, it returns NULL.
5058
5059    Else, it returns a pointer to the condition string.  (It does not
5060    attempt to evaluate the string against a particular block.)  And,
5061    it updates arg to point to the first character following the parsed
5062    if clause in the arg string. */
5063
5064 static char *
5065 ep_parse_optional_if_clause (arg)
5066      char **arg;
5067 {
5068   char *cond_string;
5069
5070   if (((*arg)[0] != 'i') || ((*arg)[1] != 'f') || !isspace ((*arg)[2]))
5071     return NULL;
5072
5073   /* Skip the "if" keyword. */
5074   (*arg) += 2;
5075
5076   /* Skip any extra leading whitespace, and record the start of the
5077      condition string. */
5078   ep_skip_leading_whitespace (arg);
5079   cond_string = *arg;
5080
5081   /* Assume that the condition occupies the remainder of the arg string. */
5082   (*arg) += strlen (cond_string);
5083
5084   return cond_string;
5085 }
5086
5087 /* This function attempts to parse an optional filename from the arg
5088    string.  If one is not found, it returns NULL.
5089
5090    Else, it returns a pointer to the parsed filename.  (This function
5091    makes no attempt to verify that a file of that name exists, or is
5092    accessible.)  And, it updates arg to point to the first character
5093    following the parsed filename in the arg string.
5094
5095    Note that clients needing to preserve the returned filename for
5096    future access should copy it to their own buffers. */
5097 static char *
5098 ep_parse_optional_filename (arg)
5099      char **arg;
5100 {
5101   static char filename[1024];
5102   char *arg_p = *arg;
5103   int i;
5104   char c;
5105
5106   if ((*arg_p == '\0') || isspace (*arg_p))
5107     return NULL;
5108
5109   for (i = 0;; i++)
5110     {
5111       c = *arg_p;
5112       if (isspace (c))
5113         c = '\0';
5114       filename[i] = c;
5115       if (c == '\0')
5116         break;
5117       arg_p++;
5118     }
5119   *arg = arg_p;
5120
5121   return filename;
5122 }
5123
5124 /* Commands to deal with catching events, such as signals, exceptions,
5125    process start/exit, etc.  */
5126
5127 typedef enum
5128 {
5129   catch_fork, catch_vfork
5130 }
5131 catch_fork_kind;
5132
5133 static void catch_fork_command_1 PARAMS ((catch_fork_kind fork_kind, 
5134                                           char *arg, 
5135                                           int tempflag, 
5136                                           int from_tty));
5137
5138 static void
5139 catch_fork_command_1 (fork_kind, arg, tempflag, from_tty)
5140      catch_fork_kind fork_kind;
5141      char *arg;
5142      int tempflag;
5143      int from_tty;
5144 {
5145   char *cond_string = NULL;
5146
5147   ep_skip_leading_whitespace (&arg);
5148
5149   /* The allowed syntax is:
5150      catch [v]fork
5151      catch [v]fork if <cond>
5152
5153      First, check if there's an if clause. */
5154   cond_string = ep_parse_optional_if_clause (&arg);
5155
5156   if ((*arg != '\0') && !isspace (*arg))
5157     error ("Junk at end of arguments.");
5158
5159   /* If this target supports it, create a fork or vfork catchpoint
5160      and enable reporting of such events. */
5161   switch (fork_kind)
5162     {
5163     case catch_fork:
5164       create_fork_event_catchpoint (tempflag, cond_string);
5165       break;
5166     case catch_vfork:
5167       create_vfork_event_catchpoint (tempflag, cond_string);
5168       break;
5169     default:
5170       error ("unsupported or unknown fork kind; cannot catch it");
5171       break;
5172     }
5173 }
5174
5175 static void
5176 catch_exec_command_1 (arg, tempflag, from_tty)
5177      char *arg;
5178      int tempflag;
5179      int from_tty;
5180 {
5181   char *cond_string = NULL;
5182
5183   ep_skip_leading_whitespace (&arg);
5184
5185   /* The allowed syntax is:
5186      catch exec
5187      catch exec if <cond>
5188
5189      First, check if there's an if clause. */
5190   cond_string = ep_parse_optional_if_clause (&arg);
5191
5192   if ((*arg != '\0') && !isspace (*arg))
5193     error ("Junk at end of arguments.");
5194
5195   /* If this target supports it, create an exec catchpoint
5196      and enable reporting of such events. */
5197   create_exec_event_catchpoint (tempflag, cond_string);
5198 }
5199
5200 #if defined(SOLIB_ADD)
5201 static void
5202 catch_load_command_1 (arg, tempflag, from_tty)
5203      char *arg;
5204      int tempflag;
5205      int from_tty;
5206 {
5207   char *dll_pathname = NULL;
5208   char *cond_string = NULL;
5209
5210   ep_skip_leading_whitespace (&arg);
5211
5212   /* The allowed syntax is:
5213      catch load
5214      catch load if <cond>
5215      catch load <filename>
5216      catch load <filename> if <cond>
5217
5218      The user is not allowed to specify the <filename> after an
5219      if clause.
5220
5221      We'll ignore the pathological case of a file named "if".
5222
5223      First, check if there's an if clause.  If so, then there
5224      cannot be a filename. */
5225   cond_string = ep_parse_optional_if_clause (&arg);
5226
5227   /* If there was an if clause, then there cannot be a filename.
5228      Else, there might be a filename and an if clause. */
5229   if (cond_string == NULL)
5230     {
5231       dll_pathname = ep_parse_optional_filename (&arg);
5232       ep_skip_leading_whitespace (&arg);
5233       cond_string = ep_parse_optional_if_clause (&arg);
5234     }
5235
5236   if ((*arg != '\0') && !isspace (*arg))
5237     error ("Junk at end of arguments.");
5238
5239   /* Create a load breakpoint that only triggers when a load of
5240      the specified dll (or any dll, if no pathname was specified)
5241      occurs. */
5242   SOLIB_CREATE_CATCH_LOAD_HOOK (inferior_pid, tempflag, 
5243                                 dll_pathname, cond_string);
5244 }
5245
5246 static void
5247 catch_unload_command_1 (arg, tempflag, from_tty)
5248      char *arg;
5249      int tempflag;
5250      int from_tty;
5251 {
5252   char *dll_pathname = NULL;
5253   char *cond_string = NULL;
5254
5255   ep_skip_leading_whitespace (&arg);
5256
5257   /* The allowed syntax is:
5258      catch unload
5259      catch unload if <cond>
5260      catch unload <filename>
5261      catch unload <filename> if <cond>
5262
5263      The user is not allowed to specify the <filename> after an
5264      if clause.
5265
5266      We'll ignore the pathological case of a file named "if".
5267
5268      First, check if there's an if clause.  If so, then there
5269      cannot be a filename. */
5270   cond_string = ep_parse_optional_if_clause (&arg);
5271
5272   /* If there was an if clause, then there cannot be a filename.
5273      Else, there might be a filename and an if clause. */
5274   if (cond_string == NULL)
5275     {
5276       dll_pathname = ep_parse_optional_filename (&arg);
5277       ep_skip_leading_whitespace (&arg);
5278       cond_string = ep_parse_optional_if_clause (&arg);
5279     }
5280
5281   if ((*arg != '\0') && !isspace (*arg))
5282     error ("Junk at end of arguments.");
5283
5284   /* Create an unload breakpoint that only triggers when an unload of
5285      the specified dll (or any dll, if no pathname was specified)
5286      occurs. */
5287   SOLIB_CREATE_CATCH_UNLOAD_HOOK (inferior_pid, tempflag, 
5288                                   dll_pathname, cond_string);
5289 }
5290 #endif /* SOLIB_ADD */
5291
5292 /* Commands to deal with catching exceptions.  */
5293
5294 /* Set a breakpoint at the specified callback routine for an
5295    exception event callback */
5296
5297 static void
5298 create_exception_catchpoint (tempflag, cond_string, ex_event, sal)
5299      int tempflag;
5300      char *cond_string;
5301      enum exception_event_kind ex_event;
5302      struct symtab_and_line *sal;
5303 {
5304   struct breakpoint *b;
5305   int thread = -1;              /* All threads. */
5306
5307   if (!sal)                     /* no exception support? */
5308     return;
5309
5310   b = set_raw_breakpoint (*sal);
5311   set_breakpoint_count (breakpoint_count + 1);
5312   b->number = breakpoint_count;
5313   b->cond = NULL;
5314   b->cond_string = (cond_string == NULL) ? 
5315     NULL : savestring (cond_string, strlen (cond_string));
5316   b->thread = thread;
5317   b->addr_string = NULL;
5318   b->enable = enabled;
5319   b->disposition = tempflag ? del : donttouch;
5320   switch (ex_event)
5321     {
5322     case EX_EVENT_THROW:
5323       b->type = bp_catch_throw;
5324       break;
5325     case EX_EVENT_CATCH:
5326       b->type = bp_catch_catch;
5327       break;
5328     default:                    /* error condition */
5329       b->type = bp_none;
5330       b->enable = disabled;
5331       error ("Internal error -- invalid catchpoint kind");
5332     }
5333   mention (b);
5334 }
5335
5336 /* Deal with "catch catch" and "catch throw" commands */
5337
5338 static void
5339 catch_exception_command_1 (ex_event, arg, tempflag, from_tty)
5340      enum exception_event_kind ex_event;
5341      char *arg;
5342      int tempflag;
5343      int from_tty;
5344 {
5345   char *cond_string = NULL;
5346   struct symtab_and_line *sal = NULL;
5347
5348   ep_skip_leading_whitespace (&arg);
5349
5350   cond_string = ep_parse_optional_if_clause (&arg);
5351
5352   if ((*arg != '\0') && !isspace (*arg))
5353     error ("Junk at end of arguments.");
5354
5355   if ((ex_event != EX_EVENT_THROW) &&
5356       (ex_event != EX_EVENT_CATCH))
5357     error ("Unsupported or unknown exception event; cannot catch it");
5358
5359   /* See if we can find a callback routine */
5360   sal = target_enable_exception_callback (ex_event, 1);
5361
5362   if (sal)
5363     {
5364       /* We have callbacks from the runtime system for exceptions.
5365          Set a breakpoint on the sal found, if no errors */
5366       if (sal != (struct symtab_and_line *) -1)
5367         create_exception_catchpoint (tempflag, cond_string, ex_event, sal);
5368       else
5369         return;         /* something went wrong with setting up callbacks */
5370     }
5371   else
5372     {
5373       /* No callbacks from runtime system for exceptions.
5374          Try GNU C++ exception breakpoints using labels in debug info. */
5375       if (ex_event == EX_EVENT_CATCH)
5376         {
5377           handle_gnu_4_16_catch_command (arg, tempflag, from_tty);
5378         }
5379       else if (ex_event == EX_EVENT_THROW)
5380         {
5381           /* Set a breakpoint on __raise_exception () */
5382
5383           warning ("Unsupported with this platform/compiler combination.");
5384           warning ("Perhaps you can achieve the effect you want by setting");
5385           warning ("a breakpoint on __raise_exception().");
5386         }
5387     }
5388 }
5389
5390 /* Cover routine to allow wrapping target_enable_exception_catchpoints
5391    inside a catch_errors */
5392
5393 static int
5394 cover_target_enable_exception_callback (arg)
5395      PTR arg;
5396 {
5397   args_for_catchpoint_enable *args = arg;
5398   struct symtab_and_line *sal;
5399   sal = target_enable_exception_callback (args->kind, args->enable);
5400   if (sal == NULL)
5401     return 0;
5402   else if (sal == (struct symtab_and_line *) -1)
5403     return -1;
5404   else
5405     return 1;                   /*is valid */
5406 }
5407
5408
5409
5410 /* This is the original v.4.16 and earlier version of the
5411    catch_command_1() function.  Now that other flavours of "catch"
5412    have been introduced, and since exception handling can be handled
5413    in other ways (through target ops) also, this is used only for the
5414    GNU C++ exception handling system.
5415    Note: Only the "catch" flavour of GDB 4.16 is handled here.  The
5416    "catch NAME" is now no longer allowed in catch_command_1().  Also,
5417    there was no code in GDB 4.16 for "catch throw". 
5418
5419    Called from catch_exception_command_1 () */
5420
5421
5422 static void
5423 handle_gnu_4_16_catch_command (arg, tempflag, from_tty)
5424      char *arg;
5425      int tempflag;
5426      int from_tty;
5427 {
5428   /* First, translate ARG into something we can deal with in terms
5429      of breakpoints.  */
5430
5431   struct symtabs_and_lines sals;
5432   struct symtab_and_line sal;
5433   register struct expression *cond = 0;
5434   register struct breakpoint *b;
5435   char *save_arg;
5436   int i;
5437
5438   INIT_SAL (&sal);              /* initialize to zeroes */
5439
5440   /* If no arg given, or if first arg is 'if ', all active catch clauses
5441      are breakpointed. */
5442
5443   if (!arg || (arg[0] == 'i' && arg[1] == 'f'
5444                && (arg[2] == ' ' || arg[2] == '\t')))
5445     {
5446       /* Grab all active catch clauses.  */
5447       sals = get_catch_sals (0);
5448     }
5449   else
5450     {
5451       /* Grab selected catch clauses.  */
5452       error ("catch NAME not implemented");
5453
5454 #if 0
5455       /* Not sure why this code has been disabled. I'm leaving
5456          it disabled.  We can never come here now anyway
5457          since we don't allow the "catch NAME" syntax.
5458          pai/1997-07-11 */
5459
5460       /* This isn't used; I don't know what it was for.  */
5461       sals = map_catch_names (arg, catch_breakpoint);
5462 #endif
5463     }
5464
5465   if (!sals.nelts)
5466     return;
5467
5468   save_arg = arg;
5469   for (i = 0; i < sals.nelts; i++)
5470     {
5471       resolve_sal_pc (&sals.sals[i]);
5472
5473       while (arg && *arg)
5474         {
5475           if (arg[0] == 'i' && arg[1] == 'f'
5476               && (arg[2] == ' ' || arg[2] == '\t'))
5477             cond = parse_exp_1 ((arg += 2, &arg),
5478                                 block_for_pc (sals.sals[i].pc), 0);
5479           else
5480             error ("Junk at end of arguments.");
5481         }
5482       arg = save_arg;
5483     }
5484
5485   for (i = 0; i < sals.nelts; i++)
5486     {
5487       sal = sals.sals[i];
5488
5489       if (from_tty)
5490         describe_other_breakpoints (sal.pc, sal.section);
5491
5492       b = set_raw_breakpoint (sal);
5493       set_breakpoint_count (breakpoint_count + 1);
5494       b->number = breakpoint_count;
5495
5496       /* Important -- this is an ordinary breakpoint.  For platforms
5497          with callback support for exceptions,
5498          create_exception_catchpoint() will create special bp types
5499          (bp_catch_catch and bp_catch_throw), and there is code in
5500          insert_breakpoints() and elsewhere that depends on that. */
5501       b->type = bp_breakpoint;  
5502
5503       b->cond = cond;
5504       b->enable = enabled;
5505       b->disposition = tempflag ? del : donttouch;
5506
5507       mention (b);
5508     }
5509
5510   if (sals.nelts > 1)
5511     {
5512       warning ("Multiple breakpoints were set.");
5513       warning ("Use the \"delete\" command to delete unwanted breakpoints.");
5514     }
5515   free ((PTR) sals.sals);
5516 }
5517
5518 #if 0
5519 /* This creates a temporary internal breakpoint
5520    just to placate infrun */
5521 static struct breakpoint *
5522 create_temp_exception_breakpoint (pc)
5523      CORE_ADDR pc;
5524 {
5525   struct symtab_and_line sal;
5526   struct breakpoint *b;
5527
5528   INIT_SAL (&sal);
5529   sal.pc = pc;
5530   sal.symtab = NULL;
5531   sal.line = 0;
5532
5533   b = set_raw_breakpoint (sal);
5534   if (!b)
5535     error ("Internal error -- couldn't set temp exception breakpoint");
5536
5537   b->type = bp_breakpoint;
5538   b->disposition = del;
5539   b->enable = enabled;
5540   b->silent = 1;
5541   b->number = internal_breakpoint_number--;
5542   return b;
5543 }
5544 #endif
5545
5546 static void
5547 catch_command_1 (arg, tempflag, from_tty)
5548      char *arg;
5549      int tempflag;
5550      int from_tty;
5551 {
5552
5553   /* The first argument may be an event name, such as "start" or "load".
5554      If so, then handle it as such.  If it doesn't match an event name,
5555      then attempt to interpret it as an exception name.  (This latter is
5556      the v4.16-and-earlier GDB meaning of the "catch" command.)
5557
5558      First, try to find the bounds of what might be an event name. */
5559   char *arg1_start = arg;
5560   char *arg1_end;
5561   int arg1_length;
5562
5563   if (arg1_start == NULL)
5564     {
5565       /* Old behaviour was to use pre-v-4.16 syntax */
5566       /* catch_throw_command_1 (arg1_start, tempflag, from_tty); */
5567       /* return; */
5568       /* Now, this is not allowed */
5569       error ("Catch requires an event name.");
5570
5571     }
5572   arg1_end = ep_find_event_name_end (arg1_start);
5573   if (arg1_end == NULL)
5574     error ("catch requires an event");
5575   arg1_length = arg1_end + 1 - arg1_start;
5576
5577   /* Try to match what we found against known event names. */
5578   if (strncmp (arg1_start, "signal", arg1_length) == 0)
5579     {
5580       error ("Catch of signal not yet implemented");
5581     }
5582   else if (strncmp (arg1_start, "catch", arg1_length) == 0)
5583     {
5584       catch_exception_command_1 (EX_EVENT_CATCH, arg1_end + 1, 
5585                                  tempflag, from_tty);
5586     }
5587   else if (strncmp (arg1_start, "throw", arg1_length) == 0)
5588     {
5589       catch_exception_command_1 (EX_EVENT_THROW, arg1_end + 1, 
5590                                  tempflag, from_tty);
5591     }
5592   else if (strncmp (arg1_start, "thread_start", arg1_length) == 0)
5593     {
5594       error ("Catch of thread_start not yet implemented");
5595     }
5596   else if (strncmp (arg1_start, "thread_exit", arg1_length) == 0)
5597     {
5598       error ("Catch of thread_exit not yet implemented");
5599     }
5600   else if (strncmp (arg1_start, "thread_join", arg1_length) == 0)
5601     {
5602       error ("Catch of thread_join not yet implemented");
5603     }
5604   else if (strncmp (arg1_start, "start", arg1_length) == 0)
5605     {
5606       error ("Catch of start not yet implemented");
5607     }
5608   else if (strncmp (arg1_start, "exit", arg1_length) == 0)
5609     {
5610       error ("Catch of exit not yet implemented");
5611     }
5612   else if (strncmp (arg1_start, "fork", arg1_length) == 0)
5613     {
5614 #if defined(CHILD_INSERT_FORK_CATCHPOINT)
5615       catch_fork_command_1 (catch_fork, arg1_end + 1, tempflag, from_tty);
5616 #else
5617       error ("Catch of fork not yet implemented");
5618 #endif
5619     }
5620   else if (strncmp (arg1_start, "vfork", arg1_length) == 0)
5621     {
5622 #if defined(CHILD_INSERT_VFORK_CATCHPOINT)
5623       catch_fork_command_1 (catch_vfork, arg1_end + 1, tempflag, from_tty);
5624 #else
5625       error ("Catch of vfork not yet implemented");
5626 #endif
5627     }
5628   else if (strncmp (arg1_start, "exec", arg1_length) == 0)
5629     {
5630 #if defined(CHILD_INSERT_EXEC_CATCHPOINT)
5631       catch_exec_command_1 (arg1_end + 1, tempflag, from_tty);
5632 #else
5633       error ("Catch of exec not yet implemented");
5634 #endif
5635     }
5636   else if (strncmp (arg1_start, "load", arg1_length) == 0)
5637     {
5638 #if defined(SOLIB_ADD)
5639       catch_load_command_1 (arg1_end + 1, tempflag, from_tty);
5640 #else
5641       error ("Catch of load not implemented");
5642 #endif
5643     }
5644   else if (strncmp (arg1_start, "unload", arg1_length) == 0)
5645     {
5646 #if defined(SOLIB_ADD)
5647       catch_unload_command_1 (arg1_end + 1, tempflag, from_tty);
5648 #else
5649       error ("Catch of load not implemented");
5650 #endif
5651     }
5652   else if (strncmp (arg1_start, "stop", arg1_length) == 0)
5653     {
5654       error ("Catch of stop not yet implemented");
5655     }
5656
5657   /* This doesn't appear to be an event name */
5658
5659   else
5660     {
5661       /* Pre-v.4.16 behaviour was to treat the argument
5662          as the name of an exception */
5663       /* catch_throw_command_1 (arg1_start, tempflag, from_tty); */
5664       /* Now this is not allowed */
5665       error ("Unknown event kind specified for catch");
5666
5667     }
5668 }
5669
5670 /* Used by the gui, could be made a worker for other things. */
5671
5672 struct breakpoint *
5673 set_breakpoint_sal (sal)
5674      struct symtab_and_line sal;
5675 {
5676   struct breakpoint *b;
5677   b = set_raw_breakpoint (sal);
5678   set_breakpoint_count (breakpoint_count + 1);
5679   b->number = breakpoint_count;
5680   b->type = bp_breakpoint;
5681   b->cond = 0;
5682   b->thread = -1;
5683   return b;
5684 }
5685
5686 #if 0
5687 /* These aren't used; I don't know what they were for.  */
5688 /* Disable breakpoints on all catch clauses described in ARGS.  */
5689 static void
5690 disable_catch (args)
5691      char *args;
5692 {
5693   /* Map the disable command to catch clauses described in ARGS.  */
5694 }
5695
5696 /* Enable breakpoints on all catch clauses described in ARGS.  */
5697 static void
5698 enable_catch (args)
5699      char *args;
5700 {
5701   /* Map the disable command to catch clauses described in ARGS.  */
5702 }
5703
5704 /* Delete breakpoints on all catch clauses in the active scope.  */
5705 static void
5706 delete_catch (args)
5707      char *args;
5708 {
5709   /* Map the delete command to catch clauses described in ARGS.  */
5710 }
5711 #endif /* 0 */
5712
5713 static void
5714 catch_command (arg, from_tty)
5715      char *arg;
5716      int from_tty;
5717 {
5718   catch_command_1 (arg, 0, from_tty);
5719 }
5720 \f
5721
5722 static void
5723 tcatch_command (arg, from_tty)
5724      char *arg;
5725      int from_tty;
5726 {
5727   catch_command_1 (arg, 1, from_tty);
5728 }
5729
5730
5731 static void
5732 clear_command (arg, from_tty)
5733      char *arg;
5734      int from_tty;
5735 {
5736   register struct breakpoint *b, *b1;
5737   int default_match;
5738   struct symtabs_and_lines sals;
5739   struct symtab_and_line sal;
5740   register struct breakpoint *found;
5741   int i;
5742
5743   if (arg)
5744     {
5745       sals = decode_line_spec (arg, 1);
5746       default_match = 0;
5747     }
5748   else
5749     {
5750       sals.sals = (struct symtab_and_line *)
5751         xmalloc (sizeof (struct symtab_and_line));
5752       INIT_SAL (&sal);          /* initialize to zeroes */
5753       sal.line = default_breakpoint_line;
5754       sal.symtab = default_breakpoint_symtab;
5755       sal.pc = default_breakpoint_address;
5756       if (sal.symtab == 0)
5757         error ("No source file specified.");
5758
5759       sals.sals[0] = sal;
5760       sals.nelts = 1;
5761
5762       default_match = 1;
5763     }
5764
5765   /* For each line spec given, delete bps which correspond
5766      to it.  We do this in two loops: the first loop looks at
5767      the initial bp(s) in the chain which should be deleted,
5768      the second goes down the rest of the chain looking ahead
5769      one so it can take those bps off the chain without messing
5770      up the chain. */
5771
5772
5773   for (i = 0; i < sals.nelts; i++)
5774     {
5775       /* If exact pc given, clear bpts at that pc.
5776          If line given (pc == 0), clear all bpts on specified line.
5777          If defaulting, clear all bpts on default line
5778          or at default pc.
5779
5780          defaulting    sal.pc != 0    tests to do
5781
5782          0              1             pc
5783          1              1             pc _and_ line
5784          0              0             line
5785          1              0             <can't happen> */
5786
5787       sal = sals.sals[i];
5788       found = (struct breakpoint *) 0;
5789
5790
5791       while (breakpoint_chain
5792       /* Why don't we check here that this is not
5793          a watchpoint, etc., as we do below?
5794          I can't make it fail, but don't know
5795          what's stopping the failure: a watchpoint
5796          of the same address as "sal.pc" should
5797          wind up being deleted. */
5798
5799              && (((sal.pc && (breakpoint_chain->address == sal.pc)) &&
5800                   (overlay_debugging == 0 ||
5801                    breakpoint_chain->section == sal.section))
5802                  || ((default_match || (0 == sal.pc))
5803                      && breakpoint_chain->source_file != NULL
5804                      && sal.symtab != NULL
5805               && STREQ (breakpoint_chain->source_file, sal.symtab->filename)
5806                      && breakpoint_chain->line_number == sal.line)))
5807
5808         {
5809           b1 = breakpoint_chain;
5810           breakpoint_chain = b1->next;
5811           b1->next = found;
5812           found = b1;
5813         }
5814
5815       ALL_BREAKPOINTS (b)
5816
5817         while (b->next
5818                && b->next->type != bp_none
5819                && b->next->type != bp_watchpoint
5820                && b->next->type != bp_hardware_watchpoint
5821                && b->next->type != bp_read_watchpoint
5822                && b->next->type != bp_access_watchpoint
5823                && (((sal.pc && (b->next->address == sal.pc)) &&
5824                     (overlay_debugging == 0 ||
5825                      b->next->section == sal.section))
5826                    || ((default_match || (0 == sal.pc))
5827                        && b->next->source_file != NULL
5828                        && sal.symtab != NULL
5829                        && STREQ (b->next->source_file, sal.symtab->filename)
5830                        && b->next->line_number == sal.line)))
5831
5832
5833         {
5834           b1 = b->next;
5835           b->next = b1->next;
5836           b1->next = found;
5837           found = b1;
5838         }
5839
5840       if (found == 0)
5841         {
5842           if (arg)
5843             error ("No breakpoint at %s.", arg);
5844           else
5845             error ("No breakpoint at this line.");
5846         }
5847
5848       if (found->next)
5849         from_tty = 1;           /* Always report if deleted more than one */
5850       if (from_tty)
5851         printf_unfiltered ("Deleted breakpoint%s ", found->next ? "s" : "");
5852       breakpoints_changed ();
5853       while (found)
5854         {
5855           if (from_tty)
5856             printf_unfiltered ("%d ", found->number);
5857           b1 = found->next;
5858           delete_breakpoint (found);
5859           found = b1;
5860         }
5861       if (from_tty)
5862         putchar_unfiltered ('\n');
5863     }
5864   free ((PTR) sals.sals);
5865 }
5866 \f
5867 /* Delete breakpoint in BS if they are `delete' breakpoints and
5868    all breakpoints that are marked for deletion, whether hit or not.
5869    This is called after any breakpoint is hit, or after errors.  */
5870
5871 void
5872 breakpoint_auto_delete (bs)
5873      bpstat bs;
5874 {
5875   struct breakpoint *b, *temp;
5876
5877   for (; bs; bs = bs->next)
5878     if (bs->breakpoint_at && bs->breakpoint_at->disposition == del
5879         && bs->stop)
5880       delete_breakpoint (bs->breakpoint_at);
5881
5882   ALL_BREAKPOINTS_SAFE (b, temp)
5883   {
5884     if (b->disposition == del_at_next_stop)
5885       delete_breakpoint (b);
5886   }
5887 }
5888
5889 /* Delete a breakpoint and clean up all traces of it in the data
5890    structures. */
5891
5892 void
5893 delete_breakpoint (bpt)
5894      struct breakpoint *bpt;
5895 {
5896   register struct breakpoint *b;
5897   register bpstat bs;
5898
5899   if (bpt == NULL)
5900     error ("Internal error (attempted to delete a NULL breakpoint)");
5901
5902
5903   /* Has this bp already been deleted?  This can happen because multiple
5904      lists can hold pointers to bp's.  bpstat lists are especial culprits.
5905
5906      One example of this happening is a watchpoint's scope bp.  When the
5907      scope bp triggers, we notice that the watchpoint is out of scope, and
5908      delete it.  We also delete its scope bp.  But the scope bp is marked
5909      "auto-deleting", and is already on a bpstat.  That bpstat is then
5910      checked for auto-deleting bp's, which are deleted.
5911
5912      A real solution to this problem might involve reference counts in bp's,
5913      and/or giving them pointers back to their referencing bpstat's, and
5914      teaching delete_breakpoint to only free a bp's storage when no more
5915      references were extent.  A cheaper bandaid was chosen. */
5916   if (bpt->type == bp_none)
5917     return;
5918
5919   if (delete_breakpoint_hook)
5920     delete_breakpoint_hook (bpt);
5921   breakpoint_delete_event (bpt->number);
5922
5923   if (bpt->inserted)
5924     remove_breakpoint (bpt, mark_uninserted);
5925
5926   if (breakpoint_chain == bpt)
5927     breakpoint_chain = bpt->next;
5928
5929   /* If we have callback-style exception catchpoints, don't go through
5930      the adjustments to the C++ runtime library etc. if the inferior
5931      isn't actually running.  target_enable_exception_callback for a
5932      null target ops vector gives an undesirable error message, so we
5933      check here and avoid it. Since currently (1997-09-17) only HP-UX aCC's
5934      exceptions are supported in this way, it's OK for now. FIXME */
5935   if (ep_is_exception_catchpoint (bpt) && target_has_execution)
5936     {
5937       static char message1[] = "Error in deleting catchpoint %d:\n";
5938       static char message[sizeof (message1) + 30];
5939       args_for_catchpoint_enable args;
5940
5941       /* Format possible error msg */
5942       sprintf (message, message1, bpt->number);
5943       args.kind = bpt->type == bp_catch_catch ? 
5944         EX_EVENT_CATCH : EX_EVENT_THROW;
5945       args.enable = 0;
5946       catch_errors (cover_target_enable_exception_callback, &args,
5947                     message, RETURN_MASK_ALL);
5948     }
5949
5950
5951   ALL_BREAKPOINTS (b)
5952     if (b->next == bpt)
5953     {
5954       b->next = bpt->next;
5955       break;
5956     }
5957
5958   /* Before turning off the visuals for the bp, check to see that
5959      there are no other bps at the same address. */
5960   if (tui_version)
5961     {
5962       int clearIt;
5963
5964       ALL_BREAKPOINTS (b)
5965       {
5966         clearIt = (b->address != bpt->address);
5967         if (!clearIt)
5968           break;
5969       }
5970
5971       if (clearIt)
5972         {
5973           TUIDO (((TuiOpaqueFuncPtr) tui_vAllSetHasBreakAt, bpt, 0));
5974           TUIDO (((TuiOpaqueFuncPtr) tuiUpdateAllExecInfos));
5975         }
5976     }
5977
5978   check_duplicates (bpt->address, bpt->section);
5979   /* If this breakpoint was inserted, and there is another breakpoint
5980      at the same address, we need to insert the other breakpoint.  */
5981   if (bpt->inserted
5982       && bpt->type != bp_hardware_watchpoint
5983       && bpt->type != bp_read_watchpoint
5984       && bpt->type != bp_access_watchpoint
5985       && bpt->type != bp_catch_fork
5986       && bpt->type != bp_catch_vfork
5987       && bpt->type != bp_catch_exec)
5988     {
5989       ALL_BREAKPOINTS (b)
5990         if (b->address == bpt->address
5991             && b->section == bpt->section
5992             && !b->duplicate
5993             && b->enable != disabled
5994             && b->enable != shlib_disabled
5995             && b->enable != call_disabled)
5996         {
5997           int val;
5998
5999           /* We should never reach this point if there is a permanent
6000              breakpoint at the same address as the one being deleted.
6001              If there is a permanent breakpoint somewhere, it should
6002              always be the only one inserted.  */
6003           if (b->enable == permanent)
6004             internal_error ("another breakpoint was inserted on top of "
6005                             "a permanent breakpoint");
6006
6007           if (b->type == bp_hardware_breakpoint)
6008             val = target_insert_hw_breakpoint (b->address, b->shadow_contents);
6009           else
6010             val = target_insert_breakpoint (b->address, b->shadow_contents);
6011
6012           if (val != 0)
6013             {
6014               target_terminal_ours_for_output ();
6015               warning ("Cannot insert breakpoint %d:", b->number);
6016               memory_error (val, b->address);   /* which bombs us out */
6017             }
6018           else
6019             b->inserted = 1;
6020         }
6021     }
6022
6023   free_command_lines (&bpt->commands);
6024   if (bpt->cond)
6025     free (bpt->cond);
6026   if (bpt->cond_string != NULL)
6027     free (bpt->cond_string);
6028   if (bpt->addr_string != NULL)
6029     free (bpt->addr_string);
6030   if (bpt->exp != NULL)
6031     free (bpt->exp);
6032   if (bpt->exp_string != NULL)
6033     free (bpt->exp_string);
6034   if (bpt->val != NULL)
6035     value_free (bpt->val);
6036   if (bpt->source_file != NULL)
6037     free (bpt->source_file);
6038   if (bpt->dll_pathname != NULL)
6039     free (bpt->dll_pathname);
6040   if (bpt->triggered_dll_pathname != NULL)
6041     free (bpt->triggered_dll_pathname);
6042   if (bpt->exec_pathname != NULL)
6043     free (bpt->exec_pathname);
6044
6045   /* Be sure no bpstat's are pointing at it after it's been freed.  */
6046   /* FIXME, how can we find all bpstat's?
6047      We just check stop_bpstat for now.  */
6048   for (bs = stop_bpstat; bs; bs = bs->next)
6049     if (bs->breakpoint_at == bpt)
6050       {
6051         bs->breakpoint_at = NULL;
6052
6053         /* we'd call bpstat_clear_actions, but that free's stuff and due
6054            to the multiple pointers pointing to one item with no
6055            reference counts found anywhere through out the bpstat's (how
6056            do you spell fragile?), we don't want to free things twice --
6057            better a memory leak than a corrupt malloc pool! */
6058         bs->commands = NULL;
6059         bs->old_val = NULL;
6060       }
6061   /* On the chance that someone will soon try again to delete this same
6062      bp, we mark it as deleted before freeing its storage. */
6063   bpt->type = bp_none;
6064
6065   free ((PTR) bpt);
6066 }
6067
6068 void
6069 delete_command (arg, from_tty)
6070      char *arg;
6071      int from_tty;
6072 {
6073   struct breakpoint *b, *temp;
6074
6075   if (arg == 0)
6076     {
6077       int breaks_to_delete = 0;
6078
6079       /* Delete all breakpoints if no argument.
6080          Do not delete internal or call-dummy breakpoints, these
6081          have to be deleted with an explicit breakpoint number argument.  */
6082       ALL_BREAKPOINTS (b)
6083       {
6084         if (b->type != bp_call_dummy &&
6085             b->type != bp_shlib_event &&
6086             b->number >= 0)
6087           breaks_to_delete = 1;
6088       }
6089
6090       /* Ask user only if there are some breakpoints to delete.  */
6091       if (!from_tty
6092           || (breaks_to_delete && query ("Delete all breakpoints? ")))
6093         {
6094           ALL_BREAKPOINTS_SAFE (b, temp)
6095           {
6096             if (b->type != bp_call_dummy &&
6097                 b->type != bp_shlib_event &&
6098                 b->number >= 0)
6099               delete_breakpoint (b);
6100           }
6101         }
6102     }
6103   else
6104     map_breakpoint_numbers (arg, delete_breakpoint);
6105 }
6106
6107 /* Reset a breakpoint given it's struct breakpoint * BINT.
6108    The value we return ends up being the return value from catch_errors.
6109    Unused in this case.  */
6110
6111 static int
6112 breakpoint_re_set_one (bint)
6113      PTR bint;
6114 {
6115   /* get past catch_errs */
6116   struct breakpoint *b = (struct breakpoint *) bint;
6117   struct value *mark;
6118   int i;
6119   struct symtabs_and_lines sals;
6120   char *s;
6121   enum enable save_enable;
6122
6123   switch (b->type)
6124     {
6125     case bp_none:
6126       warning ("attempted to reset apparently deleted breakpoint #%d?",
6127                b->number);
6128       return 0;
6129     case bp_breakpoint:
6130     case bp_hardware_breakpoint:
6131     case bp_catch_load:
6132     case bp_catch_unload:
6133       if (b->addr_string == NULL)
6134         {
6135           /* Anything without a string can't be re-set. */
6136           delete_breakpoint (b);
6137           return 0;
6138         }
6139       /* In case we have a problem, disable this breakpoint.  We'll restore
6140          its status if we succeed.  */
6141       save_enable = b->enable;
6142       b->enable = disabled;
6143
6144       set_language (b->language);
6145       input_radix = b->input_radix;
6146       s = b->addr_string;
6147       sals = decode_line_1 (&s, 1, (struct symtab *) NULL, 0, (char ***) NULL);
6148       for (i = 0; i < sals.nelts; i++)
6149         {
6150           resolve_sal_pc (&sals.sals[i]);
6151
6152           /* Reparse conditions, they might contain references to the
6153              old symtab.  */
6154           if (b->cond_string != NULL)
6155             {
6156               s = b->cond_string;
6157               if (b->cond)
6158                 free ((PTR) b->cond);
6159               b->cond = parse_exp_1 (&s, block_for_pc (sals.sals[i].pc), 0);
6160             }
6161
6162           /* We need to re-set the breakpoint if the address changes... */
6163           if (b->address != sals.sals[i].pc
6164           /* ...or new and old breakpoints both have source files, and
6165              the source file name or the line number changes...  */
6166               || (b->source_file != NULL
6167                   && sals.sals[i].symtab != NULL
6168                   && (!STREQ (b->source_file, sals.sals[i].symtab->filename)
6169                       || b->line_number != sals.sals[i].line)
6170               )
6171           /* ...or we switch between having a source file and not having
6172              one.  */
6173               || ((b->source_file == NULL) != (sals.sals[i].symtab == NULL))
6174             )
6175             {
6176               if (b->source_file != NULL)
6177                 free (b->source_file);
6178               if (sals.sals[i].symtab == NULL)
6179                 b->source_file = NULL;
6180               else
6181                 b->source_file =
6182                   savestring (sals.sals[i].symtab->filename,
6183                               strlen (sals.sals[i].symtab->filename));
6184               b->line_number = sals.sals[i].line;
6185               b->address = sals.sals[i].pc;
6186
6187               /* Used to check for duplicates here, but that can
6188                  cause trouble, as it doesn't check for disable
6189                  breakpoints. */
6190
6191               mention (b);
6192
6193               /* Might be better to do this just once per breakpoint_re_set,
6194                  rather than once for every breakpoint.  */
6195               breakpoints_changed ();
6196             }
6197           b->section = sals.sals[i].section;
6198           b->enable = save_enable;      /* Restore it, this worked. */
6199
6200
6201           /* Now that this is re-enabled, check_duplicates
6202              can be used. */
6203           check_duplicates (b->address, b->section);
6204
6205         }
6206       free ((PTR) sals.sals);
6207       break;
6208
6209     case bp_watchpoint:
6210     case bp_hardware_watchpoint:
6211     case bp_read_watchpoint:
6212     case bp_access_watchpoint:
6213       innermost_block = NULL;
6214       /* The issue arises of what context to evaluate this in.  The
6215          same one as when it was set, but what does that mean when
6216          symbols have been re-read?  We could save the filename and
6217          functionname, but if the context is more local than that, the
6218          best we could do would be something like how many levels deep
6219          and which index at that particular level, but that's going to
6220          be less stable than filenames or function names.  */
6221
6222       /* So for now, just use a global context.  */
6223       if (b->exp)
6224         free ((PTR) b->exp);
6225       b->exp = parse_expression (b->exp_string);
6226       b->exp_valid_block = innermost_block;
6227       mark = value_mark ();
6228       if (b->val)
6229         value_free (b->val);
6230       b->val = evaluate_expression (b->exp);
6231       release_value (b->val);
6232       if (VALUE_LAZY (b->val))
6233         value_fetch_lazy (b->val);
6234
6235       if (b->cond_string != NULL)
6236         {
6237           s = b->cond_string;
6238           if (b->cond)
6239             free ((PTR) b->cond);
6240           b->cond = parse_exp_1 (&s, (struct block *) 0, 0);
6241         }
6242       if (b->enable == enabled)
6243         mention (b);
6244       value_free_to_mark (mark);
6245       break;
6246     case bp_catch_catch:
6247     case bp_catch_throw:
6248       break;
6249       /* We needn't really do anything to reset these, since the mask
6250          that requests them is unaffected by e.g., new libraries being
6251          loaded. */
6252     case bp_catch_fork:
6253     case bp_catch_vfork:
6254     case bp_catch_exec:
6255       break;
6256
6257     default:
6258       printf_filtered ("Deleting unknown breakpoint type %d\n", b->type);
6259       /* fall through */
6260       /* Delete longjmp breakpoints, they will be reset later by
6261          breakpoint_re_set.  */
6262     case bp_longjmp:
6263     case bp_longjmp_resume:
6264       delete_breakpoint (b);
6265       break;
6266
6267       /* This breakpoint is special, it's set up when the inferior
6268          starts and we really don't want to touch it.  */
6269     case bp_shlib_event:
6270
6271       /* Keep temporary breakpoints, which can be encountered when we step
6272          over a dlopen call and SOLIB_ADD is resetting the breakpoints.
6273          Otherwise these should have been blown away via the cleanup chain
6274          or by breakpoint_init_inferior when we rerun the executable.  */
6275     case bp_until:
6276     case bp_finish:
6277     case bp_watchpoint_scope:
6278     case bp_call_dummy:
6279     case bp_step_resume:
6280       break;
6281     }
6282
6283   return 0;
6284 }
6285
6286 /* Re-set all breakpoints after symbols have been re-loaded.  */
6287 void
6288 breakpoint_re_set ()
6289 {
6290   struct breakpoint *b, *temp;
6291   enum language save_language;
6292   int save_input_radix;
6293   static char message1[] = "Error in re-setting breakpoint %d:\n";
6294   char message[sizeof (message1) + 30 /* slop */ ];
6295
6296   save_language = current_language->la_language;
6297   save_input_radix = input_radix;
6298   ALL_BREAKPOINTS_SAFE (b, temp)
6299   {
6300     /* Format possible error msg */
6301     sprintf (message, message1, b->number);
6302     catch_errors (breakpoint_re_set_one, b, message, RETURN_MASK_ALL);
6303   }
6304   set_language (save_language);
6305   input_radix = save_input_radix;
6306
6307 #ifdef GET_LONGJMP_TARGET
6308   create_longjmp_breakpoint ("longjmp");
6309   create_longjmp_breakpoint ("_longjmp");
6310   create_longjmp_breakpoint ("siglongjmp");
6311   create_longjmp_breakpoint ("_siglongjmp");
6312   create_longjmp_breakpoint (NULL);
6313 #endif
6314
6315 #if 0
6316   /* Took this out (temporarily at least), since it produces an extra 
6317      blank line at startup. This messes up the gdbtests. -PB */
6318   /* Blank line to finish off all those mention() messages we just printed.  */
6319   printf_filtered ("\n");
6320 #endif
6321 }
6322 \f
6323 /* Set ignore-count of breakpoint number BPTNUM to COUNT.
6324    If from_tty is nonzero, it prints a message to that effect,
6325    which ends with a period (no newline).  */
6326
6327 /* Reset the thread number of this breakpoint:
6328
6329    - If the breakpoint is for all threads, leave it as-is.
6330    - Else, reset it to the current thread for inferior_pid. */
6331 void
6332 breakpoint_re_set_thread (b)
6333      struct breakpoint *b;
6334 {
6335   if (b->thread != -1)
6336     {
6337       if (in_thread_list (inferior_pid))
6338         b->thread = pid_to_thread_id (inferior_pid);
6339     }
6340 }
6341
6342 void
6343 set_ignore_count (bptnum, count, from_tty)
6344      int bptnum, count, from_tty;
6345 {
6346   register struct breakpoint *b;
6347
6348   if (count < 0)
6349     count = 0;
6350
6351   ALL_BREAKPOINTS (b)
6352     if (b->number == bptnum)
6353     {
6354       b->ignore_count = count;
6355       if (!from_tty)
6356         return;
6357       else if (count == 0)
6358         printf_filtered ("Will stop next time breakpoint %d is reached.",
6359                          bptnum);
6360       else if (count == 1)
6361         printf_filtered ("Will ignore next crossing of breakpoint %d.",
6362                          bptnum);
6363       else
6364         printf_filtered ("Will ignore next %d crossings of breakpoint %d.",
6365                          count, bptnum);
6366       breakpoints_changed ();
6367       return;
6368     }
6369
6370   error ("No breakpoint number %d.", bptnum);
6371 }
6372
6373 /* Clear the ignore counts of all breakpoints.  */
6374 void
6375 breakpoint_clear_ignore_counts ()
6376 {
6377   struct breakpoint *b;
6378
6379   ALL_BREAKPOINTS (b)
6380     b->ignore_count = 0;
6381 }
6382
6383 /* Command to set ignore-count of breakpoint N to COUNT.  */
6384
6385 static void
6386 ignore_command (args, from_tty)
6387      char *args;
6388      int from_tty;
6389 {
6390   char *p = args;
6391   register int num;
6392
6393   if (p == 0)
6394     error_no_arg ("a breakpoint number");
6395
6396   num = get_number (&p);
6397
6398   if (*p == 0)
6399     error ("Second argument (specified ignore-count) is missing.");
6400
6401   set_ignore_count (num,
6402                     longest_to_int (value_as_long (parse_and_eval (p))),
6403                     from_tty);
6404   printf_filtered ("\n");
6405   breakpoints_changed ();
6406 }
6407 \f
6408 /* Call FUNCTION on each of the breakpoints
6409    whose numbers are given in ARGS.  */
6410
6411 static void
6412 map_breakpoint_numbers (args, function)
6413      char *args;
6414      void (*function) PARAMS ((struct breakpoint *));
6415 {
6416   register char *p = args;
6417   char *p1;
6418   register int num;
6419   register struct breakpoint *b;
6420
6421   if (p == 0)
6422     error_no_arg ("one or more breakpoint numbers");
6423
6424   while (*p)
6425     {
6426       p1 = p;
6427
6428       num = get_number (&p1);
6429
6430       ALL_BREAKPOINTS (b)
6431         if (b->number == num)
6432         {
6433           struct breakpoint *related_breakpoint = b->related_breakpoint;
6434           function (b);
6435           if (related_breakpoint)
6436             function (related_breakpoint);
6437           goto win;
6438         }
6439       printf_unfiltered ("No breakpoint number %d.\n", num);
6440     win:
6441       p = p1;
6442     }
6443 }
6444
6445 void
6446 disable_breakpoint (bpt)
6447      struct breakpoint *bpt;
6448 {
6449   /* Never disable a watchpoint scope breakpoint; we want to
6450      hit them when we leave scope so we can delete both the
6451      watchpoint and its scope breakpoint at that time.  */
6452   if (bpt->type == bp_watchpoint_scope)
6453     return;
6454
6455   /* You can't disable permanent breakpoints.  */
6456   if (bpt->enable == permanent)
6457     return;
6458
6459   bpt->enable = disabled;
6460
6461   check_duplicates (bpt->address, bpt->section);
6462
6463   if (modify_breakpoint_hook)
6464     modify_breakpoint_hook (bpt);
6465   breakpoint_modify_event (bpt->number);
6466 }
6467
6468 /* ARGSUSED */
6469 static void
6470 disable_command (args, from_tty)
6471      char *args;
6472      int from_tty;
6473 {
6474   register struct breakpoint *bpt;
6475   if (args == 0)
6476     ALL_BREAKPOINTS (bpt)
6477       switch (bpt->type)
6478       {
6479       case bp_none:
6480         warning ("attempted to disable apparently deleted breakpoint #%d?",
6481                  bpt->number);
6482         continue;
6483       case bp_breakpoint:
6484       case bp_catch_load:
6485       case bp_catch_unload:
6486       case bp_catch_fork:
6487       case bp_catch_vfork:
6488       case bp_catch_exec:
6489       case bp_catch_catch:
6490       case bp_catch_throw:
6491       case bp_hardware_breakpoint:
6492       case bp_watchpoint:
6493       case bp_hardware_watchpoint:
6494       case bp_read_watchpoint:
6495       case bp_access_watchpoint:
6496         disable_breakpoint (bpt);
6497       default:
6498         continue;
6499       }
6500   else
6501     map_breakpoint_numbers (args, disable_breakpoint);
6502 }
6503
6504 static void
6505 do_enable_breakpoint (bpt, disposition)
6506      struct breakpoint *bpt;
6507      enum bpdisp disposition;
6508 {
6509   struct frame_info *save_selected_frame = NULL;
6510   int save_selected_frame_level = -1;
6511   int target_resources_ok, other_type_used;
6512   struct value *mark;
6513
6514   if (bpt->type == bp_hardware_breakpoint)
6515     {
6516       int i;
6517       i = hw_breakpoint_used_count ();
6518       target_resources_ok = 
6519         TARGET_CAN_USE_HARDWARE_WATCHPOINT (bp_hardware_breakpoint, 
6520                                             i + 1, 0);
6521       if (target_resources_ok == 0)
6522         error ("No hardware breakpoint support in the target.");
6523       else if (target_resources_ok < 0)
6524         error ("Hardware breakpoints used exceeds limit.");
6525     }
6526
6527   if (bpt->enable != permanent)
6528     bpt->enable = enabled;
6529   bpt->disposition = disposition;
6530   check_duplicates (bpt->address, bpt->section);
6531   breakpoints_changed ();
6532
6533   if (bpt->type == bp_watchpoint || 
6534       bpt->type == bp_hardware_watchpoint ||
6535       bpt->type == bp_read_watchpoint || 
6536       bpt->type == bp_access_watchpoint)
6537     {
6538       if (bpt->exp_valid_block != NULL)
6539         {
6540           struct frame_info *fr =
6541
6542           /* Ensure that we have the current frame.  Else, this
6543              next query may pessimistically be answered as, "No,
6544              not within current scope". */
6545           get_current_frame ();
6546           fr = find_frame_addr_in_frame_chain (bpt->watchpoint_frame);
6547           if (fr == NULL)
6548             {
6549               printf_filtered ("\
6550 Cannot enable watchpoint %d because the block in which its expression\n\
6551 is valid is not currently in scope.\n", bpt->number);
6552               bpt->enable = disabled;
6553               return;
6554             }
6555
6556           save_selected_frame = selected_frame;
6557           save_selected_frame_level = selected_frame_level;
6558           select_frame (fr, -1);
6559         }
6560
6561       value_free (bpt->val);
6562       mark = value_mark ();
6563       bpt->val = evaluate_expression (bpt->exp);
6564       release_value (bpt->val);
6565       if (VALUE_LAZY (bpt->val))
6566         value_fetch_lazy (bpt->val);
6567
6568       if (bpt->type == bp_hardware_watchpoint ||
6569           bpt->type == bp_read_watchpoint ||
6570           bpt->type == bp_access_watchpoint)
6571         {
6572           int i = hw_watchpoint_used_count (bpt->type, &other_type_used);
6573           int mem_cnt = can_use_hardware_watchpoint (bpt->val);
6574
6575           /* Hack around 'unused var' error for some targets here */
6576           (void) mem_cnt, i;
6577           target_resources_ok = TARGET_CAN_USE_HARDWARE_WATCHPOINT (
6578                                    bpt->type, i + mem_cnt, other_type_used);
6579           /* we can consider of type is bp_hardware_watchpoint, convert to 
6580              bp_watchpoint in the following condition */
6581           if (target_resources_ok < 0)
6582             {
6583               printf_filtered ("\
6584 Cannot enable watchpoint %d because target watch resources\n\
6585 have been allocated for other watchpoints.\n", bpt->number);
6586               bpt->enable = disabled;
6587               value_free_to_mark (mark);
6588               return;
6589             }
6590         }
6591
6592       if (save_selected_frame_level >= 0)
6593         select_and_print_frame (save_selected_frame,
6594                                 save_selected_frame_level);
6595       value_free_to_mark (mark);
6596     }
6597   if (modify_breakpoint_hook)
6598     modify_breakpoint_hook (bpt);
6599   breakpoint_modify_event (bpt->number);
6600 }
6601
6602 void
6603 enable_breakpoint (bpt)
6604      struct breakpoint *bpt;
6605 {
6606   do_enable_breakpoint (bpt, bpt->disposition);
6607 }
6608
6609 /* The enable command enables the specified breakpoints (or all defined
6610    breakpoints) so they once again become (or continue to be) effective
6611    in stopping the inferior. */
6612
6613 /* ARGSUSED */
6614 static void
6615 enable_command (args, from_tty)
6616      char *args;
6617      int from_tty;
6618 {
6619   register struct breakpoint *bpt;
6620   if (args == 0)
6621     ALL_BREAKPOINTS (bpt)
6622       switch (bpt->type)
6623       {
6624       case bp_none:
6625         warning ("attempted to enable apparently deleted breakpoint #%d?",
6626                  bpt->number);
6627         continue;
6628       case bp_breakpoint:
6629       case bp_catch_load:
6630       case bp_catch_unload:
6631       case bp_catch_fork:
6632       case bp_catch_vfork:
6633       case bp_catch_exec:
6634       case bp_catch_catch:
6635       case bp_catch_throw:
6636       case bp_hardware_breakpoint:
6637       case bp_watchpoint:
6638       case bp_hardware_watchpoint:
6639       case bp_read_watchpoint:
6640       case bp_access_watchpoint:
6641         enable_breakpoint (bpt);
6642       default:
6643         continue;
6644       }
6645   else
6646     map_breakpoint_numbers (args, enable_breakpoint);
6647 }
6648
6649 static void
6650 enable_once_breakpoint (bpt)
6651      struct breakpoint *bpt;
6652 {
6653   do_enable_breakpoint (bpt, disable);
6654 }
6655
6656 /* ARGSUSED */
6657 static void
6658 enable_once_command (args, from_tty)
6659      char *args;
6660      int from_tty;
6661 {
6662   map_breakpoint_numbers (args, enable_once_breakpoint);
6663 }
6664
6665 static void
6666 enable_delete_breakpoint (bpt)
6667      struct breakpoint *bpt;
6668 {
6669   do_enable_breakpoint (bpt, del);
6670 }
6671
6672 /* ARGSUSED */
6673 static void
6674 enable_delete_command (args, from_tty)
6675      char *args;
6676      int from_tty;
6677 {
6678   map_breakpoint_numbers (args, enable_delete_breakpoint);
6679 }
6680 \f
6681 /* Use default_breakpoint_'s, or nothing if they aren't valid.  */
6682
6683 struct symtabs_and_lines
6684 decode_line_spec_1 (string, funfirstline)
6685      char *string;
6686      int funfirstline;
6687 {
6688   struct symtabs_and_lines sals;
6689   if (string == 0)
6690     error ("Empty line specification.");
6691   if (default_breakpoint_valid)
6692     sals = decode_line_1 (&string, funfirstline,
6693                           default_breakpoint_symtab,
6694                           default_breakpoint_line,
6695                           (char ***) NULL);
6696   else
6697     sals = decode_line_1 (&string, funfirstline,
6698                           (struct symtab *) NULL, 0, (char ***) NULL);
6699   if (*string)
6700     error ("Junk at end of line specification: %s", string);
6701   return sals;
6702 }
6703 \f
6704 void
6705 _initialize_breakpoint ()
6706 {
6707   struct cmd_list_element *c;
6708
6709   breakpoint_chain = 0;
6710   /* Don't bother to call set_breakpoint_count.  $bpnum isn't useful
6711      before a breakpoint is set.  */
6712   breakpoint_count = 0;
6713
6714   add_com ("ignore", class_breakpoint, ignore_command,
6715            "Set ignore-count of breakpoint number N to COUNT.\n\
6716 Usage is `ignore N COUNT'.");
6717   if (xdb_commands)
6718     add_com_alias ("bc", "ignore", class_breakpoint, 1);
6719
6720   add_com ("commands", class_breakpoint, commands_command,
6721            "Set commands to be executed when a breakpoint is hit.\n\
6722 Give breakpoint number as argument after \"commands\".\n\
6723 With no argument, the targeted breakpoint is the last one set.\n\
6724 The commands themselves follow starting on the next line.\n\
6725 Type a line containing \"end\" to indicate the end of them.\n\
6726 Give \"silent\" as the first line to make the breakpoint silent;\n\
6727 then no output is printed when it is hit, except what the commands print.");
6728
6729   add_com ("condition", class_breakpoint, condition_command,
6730            "Specify breakpoint number N to break only if COND is true.\n\
6731 Usage is `condition N COND', where N is an integer and COND is an\n\
6732 expression to be evaluated whenever breakpoint N is reached.  ");
6733
6734   add_com ("tbreak", class_breakpoint, tbreak_command,
6735            "Set a temporary breakpoint.  Args like \"break\" command.\n\
6736 Like \"break\" except the breakpoint is only temporary,\n\
6737 so it will be deleted when hit.  Equivalent to \"break\" followed\n\
6738 by using \"enable delete\" on the breakpoint number.");
6739   add_com ("txbreak", class_breakpoint, tbreak_at_finish_command,
6740            "Set temporary breakpoint at procedure exit.  Either there should\n\
6741 be no argument or the argument must be a depth.\n");
6742
6743   add_com ("hbreak", class_breakpoint, hbreak_command,
6744            "Set a hardware assisted  breakpoint. Args like \"break\" command.\n\
6745 Like \"break\" except the breakpoint requires hardware support,\n\
6746 some target hardware may not have this support.");
6747
6748   add_com ("thbreak", class_breakpoint, thbreak_command,
6749            "Set a temporary hardware assisted breakpoint. Args like \"break\" command.\n\
6750 Like \"hbreak\" except the breakpoint is only temporary,\n\
6751 so it will be deleted when hit.");
6752
6753   add_prefix_cmd ("enable", class_breakpoint, enable_command,
6754                   "Enable some breakpoints.\n\
6755 Give breakpoint numbers (separated by spaces) as arguments.\n\
6756 With no subcommand, breakpoints are enabled until you command otherwise.\n\
6757 This is used to cancel the effect of the \"disable\" command.\n\
6758 With a subcommand you can enable temporarily.",
6759                   &enablelist, "enable ", 1, &cmdlist);
6760   if (xdb_commands)
6761     add_com ("ab", class_breakpoint, enable_command,
6762              "Enable some breakpoints.\n\
6763 Give breakpoint numbers (separated by spaces) as arguments.\n\
6764 With no subcommand, breakpoints are enabled until you command otherwise.\n\
6765 This is used to cancel the effect of the \"disable\" command.\n\
6766 With a subcommand you can enable temporarily.");
6767
6768   add_com_alias ("en", "enable", class_breakpoint, 1);
6769
6770   add_abbrev_prefix_cmd ("breakpoints", class_breakpoint, enable_command,
6771                          "Enable some breakpoints.\n\
6772 Give breakpoint numbers (separated by spaces) as arguments.\n\
6773 This is used to cancel the effect of the \"disable\" command.\n\
6774 May be abbreviated to simply \"enable\".\n",
6775                    &enablebreaklist, "enable breakpoints ", 1, &enablelist);
6776
6777   add_cmd ("once", no_class, enable_once_command,
6778            "Enable breakpoints for one hit.  Give breakpoint numbers.\n\
6779 If a breakpoint is hit while enabled in this fashion, it becomes disabled.",
6780            &enablebreaklist);
6781
6782   add_cmd ("delete", no_class, enable_delete_command,
6783            "Enable breakpoints and delete when hit.  Give breakpoint numbers.\n\
6784 If a breakpoint is hit while enabled in this fashion, it is deleted.",
6785            &enablebreaklist);
6786
6787   add_cmd ("delete", no_class, enable_delete_command,
6788            "Enable breakpoints and delete when hit.  Give breakpoint numbers.\n\
6789 If a breakpoint is hit while enabled in this fashion, it is deleted.",
6790            &enablelist);
6791
6792   add_cmd ("once", no_class, enable_once_command,
6793            "Enable breakpoints for one hit.  Give breakpoint numbers.\n\
6794 If a breakpoint is hit while enabled in this fashion, it becomes disabled.",
6795            &enablelist);
6796
6797   add_prefix_cmd ("disable", class_breakpoint, disable_command,
6798                   "Disable some breakpoints.\n\
6799 Arguments are breakpoint numbers with spaces in between.\n\
6800 To disable all breakpoints, give no argument.\n\
6801 A disabled breakpoint is not forgotten, but has no effect until reenabled.",
6802                   &disablelist, "disable ", 1, &cmdlist);
6803   add_com_alias ("dis", "disable", class_breakpoint, 1);
6804   add_com_alias ("disa", "disable", class_breakpoint, 1);
6805   if (xdb_commands)
6806     add_com ("sb", class_breakpoint, 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.");
6811
6812   add_cmd ("breakpoints", class_alias, disable_command,
6813            "Disable some breakpoints.\n\
6814 Arguments are breakpoint numbers with spaces in between.\n\
6815 To disable all breakpoints, give no argument.\n\
6816 A disabled breakpoint is not forgotten, but has no effect until reenabled.\n\
6817 This command may be abbreviated \"disable\".",
6818            &disablelist);
6819
6820   add_prefix_cmd ("delete", class_breakpoint, delete_command,
6821                   "Delete some breakpoints or auto-display expressions.\n\
6822 Arguments are breakpoint numbers with spaces in between.\n\
6823 To delete all breakpoints, give no argument.\n\
6824 \n\
6825 Also a prefix command for deletion of other GDB objects.\n\
6826 The \"unset\" command is also an alias for \"delete\".",
6827                   &deletelist, "delete ", 1, &cmdlist);
6828   add_com_alias ("d", "delete", class_breakpoint, 1);
6829   if (xdb_commands)
6830     add_com ("db", class_breakpoint, delete_command,
6831              "Delete some breakpoints.\n\
6832 Arguments are breakpoint numbers with spaces in between.\n\
6833 To delete all breakpoints, give no argument.\n");
6834
6835   add_cmd ("breakpoints", class_alias, delete_command,
6836            "Delete some breakpoints or auto-display expressions.\n\
6837 Arguments are breakpoint numbers with spaces in between.\n\
6838 To delete all breakpoints, give no argument.\n\
6839 This command may be abbreviated \"delete\".",
6840            &deletelist);
6841
6842   add_com ("clear", class_breakpoint, clear_command,
6843            concat ("Clear breakpoint at specified line or function.\n\
6844 Argument may be line number, function name, or \"*\" and an address.\n\
6845 If line number is specified, all breakpoints in that line are cleared.\n\
6846 If function is specified, breakpoints at beginning of function are cleared.\n\
6847 If an address is specified, breakpoints at that address are cleared.\n\n",
6848                    "With no argument, clears all breakpoints in the line that the selected frame\n\
6849 is executing in.\n\
6850 \n\
6851 See also the \"delete\" command which clears breakpoints by number.", NULL));
6852
6853   add_com ("break", class_breakpoint, break_command,
6854            concat ("Set breakpoint at specified line or function.\n\
6855 Argument may be line number, function name, or \"*\" and an address.\n\
6856 If line number is specified, break at start of code for that line.\n\
6857 If function is specified, break at start of code for that function.\n\
6858 If an address is specified, break at that exact address.\n",
6859                    "With no arg, uses current execution address of selected stack frame.\n\
6860 This is useful for breaking on return to a stack frame.\n\
6861 \n\
6862 Multiple breakpoints at one place are permitted, and useful if conditional.\n\
6863 \n\
6864 Do \"help breakpoints\" for info on other commands dealing with breakpoints.", NULL));
6865   add_com_alias ("b", "break", class_run, 1);
6866   add_com_alias ("br", "break", class_run, 1);
6867   add_com_alias ("bre", "break", class_run, 1);
6868   add_com_alias ("brea", "break", class_run, 1);
6869
6870   add_com ("xbreak", class_breakpoint, break_at_finish_command,
6871            concat ("Set breakpoint at procedure exit. \n\
6872 Argument may be function name, or \"*\" and an address.\n\
6873 If function is specified, break at end of code for that function.\n\
6874 If an address is specified, break at the end of the function that contains \n\
6875 that exact address.\n",
6876                    "With no arg, uses current execution address of selected stack frame.\n\
6877 This is useful for breaking on return to a stack frame.\n\
6878 \n\
6879 Multiple breakpoints at one place are permitted, and useful if conditional.\n\
6880 \n\
6881 Do \"help breakpoints\" for info on other commands dealing with breakpoints.", NULL));
6882   add_com_alias ("xb", "xbreak", class_breakpoint, 1);
6883   add_com_alias ("xbr", "xbreak", class_breakpoint, 1);
6884   add_com_alias ("xbre", "xbreak", class_breakpoint, 1);
6885   add_com_alias ("xbrea", "xbreak", class_breakpoint, 1);
6886
6887   if (xdb_commands)
6888     {
6889       add_com_alias ("ba", "break", class_breakpoint, 1);
6890       add_com_alias ("bu", "ubreak", class_breakpoint, 1);
6891       add_com ("bx", class_breakpoint, break_at_finish_at_depth_command,
6892                "Set breakpoint at procedure exit.  Either there should\n\
6893 be no argument or the argument must be a depth.\n");
6894     }
6895
6896   if (dbx_commands)
6897     {
6898       add_abbrev_prefix_cmd ("stop", class_breakpoint, stop_command,
6899         "Break in function/address or break at a line in the current file.",
6900                              &stoplist, "stop ", 1, &cmdlist);
6901       add_cmd ("in", class_breakpoint, stopin_command,
6902                "Break in function or address.\n", &stoplist);
6903       add_cmd ("at", class_breakpoint, stopat_command,
6904                "Break at a line in the current file.\n", &stoplist);
6905       add_com ("status", class_info, breakpoints_info,
6906                concat ("Status of user-settable breakpoints, or breakpoint number NUMBER.\n\
6907 The \"Type\" column indicates one of:\n\
6908 \tbreakpoint     - normal breakpoint\n\
6909 \twatchpoint     - watchpoint\n\
6910 The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
6911 the disposition of the breakpoint after it gets hit.  \"dis\" means that the\n\
6912 breakpoint will be disabled.  The \"Address\" and \"What\" columns indicate the\n\
6913 address and file/line number respectively.\n\n",
6914                        "Convenience variable \"$_\" and default examine address for \"x\"\n\
6915 are set to the address of the last breakpoint listed.\n\n\
6916 Convenience variable \"$bpnum\" contains the number of the last\n\
6917 breakpoint set.", NULL));
6918     }
6919
6920   add_info ("breakpoints", breakpoints_info,
6921             concat ("Status of user-settable breakpoints, or breakpoint number NUMBER.\n\
6922 The \"Type\" column indicates one of:\n\
6923 \tbreakpoint     - normal breakpoint\n\
6924 \twatchpoint     - watchpoint\n\
6925 The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
6926 the disposition of the breakpoint after it gets hit.  \"dis\" means that the\n\
6927 breakpoint will be disabled.  The \"Address\" and \"What\" columns indicate the\n\
6928 address and file/line number respectively.\n\n",
6929                     "Convenience variable \"$_\" and default examine address for \"x\"\n\
6930 are set to the address of the last breakpoint listed.\n\n\
6931 Convenience variable \"$bpnum\" contains the number of the last\n\
6932 breakpoint set.", NULL));
6933
6934   if (xdb_commands)
6935     add_com ("lb", class_breakpoint, breakpoints_info,
6936              concat ("Status of user-settable breakpoints, or breakpoint number NUMBER.\n\
6937 The \"Type\" column indicates one of:\n\
6938 \tbreakpoint     - normal breakpoint\n\
6939 \twatchpoint     - watchpoint\n\
6940 The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
6941 the disposition of the breakpoint after it gets hit.  \"dis\" means that the\n\
6942 breakpoint will be disabled.  The \"Address\" and \"What\" columns indicate the\n\
6943 address and file/line number respectively.\n\n",
6944                      "Convenience variable \"$_\" and default examine address for \"x\"\n\
6945 are set to the address of the last breakpoint listed.\n\n\
6946 Convenience variable \"$bpnum\" contains the number of the last\n\
6947 breakpoint set.", NULL));
6948
6949   add_cmd ("breakpoints", class_maintenance, maintenance_info_breakpoints,
6950            concat ("Status of all breakpoints, or breakpoint number NUMBER.\n\
6951 The \"Type\" column indicates one of:\n\
6952 \tbreakpoint     - normal breakpoint\n\
6953 \twatchpoint     - watchpoint\n\
6954 \tlongjmp        - internal breakpoint used to step through longjmp()\n\
6955 \tlongjmp resume - internal breakpoint at the target of longjmp()\n\
6956 \tuntil          - internal breakpoint used by the \"until\" command\n\
6957 \tfinish         - internal breakpoint used by the \"finish\" command\n",
6958                    "The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
6959 the disposition of the breakpoint after it gets hit.  \"dis\" means that the\n\
6960 breakpoint will be disabled.  The \"Address\" and \"What\" columns indicate the\n\
6961 address and file/line number respectively.\n\n",
6962                    "Convenience variable \"$_\" and default examine address for \"x\"\n\
6963 are set to the address of the last breakpoint listed.\n\n\
6964 Convenience variable \"$bpnum\" contains the number of the last\n\
6965 breakpoint set.", NULL),
6966            &maintenanceinfolist);
6967
6968   add_com ("catch", class_breakpoint, catch_command,
6969            "Set catchpoints to catch events.\n\
6970 Raised signals may be caught:\n\
6971 \tcatch signal              - all signals\n\
6972 \tcatch signal <signame>    - a particular signal\n\
6973 Raised exceptions may be caught:\n\
6974 \tcatch throw               - all exceptions, when thrown\n\
6975 \tcatch throw <exceptname>  - a particular exception, when thrown\n\
6976 \tcatch catch               - all exceptions, when caught\n\
6977 \tcatch catch <exceptname>  - a particular exception, when caught\n\
6978 Thread or process events may be caught:\n\
6979 \tcatch thread_start        - any threads, just after creation\n\
6980 \tcatch thread_exit         - any threads, just before expiration\n\
6981 \tcatch thread_join         - any threads, just after joins\n\
6982 Process events may be caught:\n\
6983 \tcatch start               - any processes, just after creation\n\
6984 \tcatch exit                - any processes, just before expiration\n\
6985 \tcatch fork                - calls to fork()\n\
6986 \tcatch vfork               - calls to vfork()\n\
6987 \tcatch exec                - calls to exec()\n\
6988 Dynamically-linked library events may be caught:\n\
6989 \tcatch load                - loads of any library\n\
6990 \tcatch load <libname>      - loads of a particular library\n\
6991 \tcatch unload              - unloads of any library\n\
6992 \tcatch unload <libname>    - unloads of a particular library\n\
6993 The act of your program's execution stopping may also be caught:\n\
6994 \tcatch stop\n\n\
6995 C++ exceptions may be caught:\n\
6996 \tcatch throw               - all exceptions, when thrown\n\
6997 \tcatch catch               - all exceptions, when caught\n\
6998 \n\
6999 Do \"help set follow-fork-mode\" for info on debugging your program\n\
7000 after a fork or vfork is caught.\n\n\
7001 Do \"help breakpoints\" for info on other commands dealing with breakpoints.");
7002
7003   add_com ("tcatch", class_breakpoint, tcatch_command,
7004            "Set temporary catchpoints to catch events.\n\
7005 Args like \"catch\" command.\n\
7006 Like \"catch\" except the catchpoint is only temporary,\n\
7007 so it will be deleted when hit.  Equivalent to \"catch\" followed\n\
7008 by using \"enable delete\" on the catchpoint number.");
7009
7010   add_com ("watch", class_breakpoint, watch_command,
7011            "Set a watchpoint for an expression.\n\
7012 A watchpoint stops execution of your program whenever the value of\n\
7013 an expression changes.");
7014
7015   add_com ("rwatch", class_breakpoint, rwatch_command,
7016            "Set a read watchpoint for an expression.\n\
7017 A watchpoint stops execution of your program whenever the value of\n\
7018 an expression is read.");
7019
7020   add_com ("awatch", class_breakpoint, awatch_command,
7021            "Set a watchpoint for an expression.\n\
7022 A watchpoint stops execution of your program whenever the value of\n\
7023 an expression is either read or written.");
7024
7025   add_info ("watchpoints", breakpoints_info,
7026             "Synonym for ``info breakpoints''.");
7027
7028
7029   c = add_set_cmd ("can-use-hw-watchpoints", class_support, var_zinteger,
7030                    (char *) &can_use_hw_watchpoints,
7031                    "Set debugger's willingness to use watchpoint hardware.\n\
7032 If zero, gdb will not use hardware for new watchpoints, even if\n\
7033 such is available.  (However, any hardware watchpoints that were\n\
7034 created before setting this to nonzero, will continue to use watchpoint\n\
7035 hardware.)",
7036                    &setlist);
7037   add_show_from_set (c, &showlist);
7038
7039   can_use_hw_watchpoints = 1;
7040 }