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