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