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