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