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