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