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