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