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