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