* breakpoint.c (wrap_indent_at_field): New function.
[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, 2010, 2011 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 #include "jit.h"
63 #include "xml-syscall.h"
64 #include "parser-defs.h"
65 #include "cli/cli-utils.h"
66
67 /* readline include files */
68 #include "readline/readline.h"
69 #include "readline/history.h"
70
71 /* readline defines this.  */
72 #undef savestring
73
74 #include "mi/mi-common.h"
75
76 /* Arguments to pass as context to some catch command handlers.  */
77 #define CATCH_PERMANENT ((void *) (uintptr_t) 0)
78 #define CATCH_TEMPORARY ((void *) (uintptr_t) 1)
79
80 /* Prototypes for local functions.  */
81
82 static void enable_delete_command (char *, int);
83
84 static void enable_once_command (char *, int);
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                                                       void *),
92                                     void *);
93
94 static void ignore_command (char *, int);
95
96 static int breakpoint_re_set_one (void *);
97
98 static void clear_command (char *, int);
99
100 static void catch_command (char *, int);
101
102 static int can_use_hardware_watchpoint (struct value *, int);
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
109    static.  */
110 struct breakpoint *set_raw_breakpoint (struct gdbarch *gdbarch,
111                                               struct symtab_and_line,
112                                               enum bptype);
113
114 static void breakpoint_adjustment_warning (CORE_ADDR, CORE_ADDR, int, int);
115
116 static CORE_ADDR adjust_breakpoint_address (struct gdbarch *gdbarch,
117                                             CORE_ADDR bpaddr,
118                                             enum bptype bptype);
119
120 static void describe_other_breakpoints (struct gdbarch *,
121                                         struct program_space *, CORE_ADDR,
122                                         struct obj_section *, int);
123
124 static int breakpoint_address_match (struct address_space *aspace1,
125                                      CORE_ADDR addr1,
126                                      struct address_space *aspace2,
127                                      CORE_ADDR addr2);
128
129 static int watchpoint_locations_match (struct bp_location *loc1,
130                                        struct bp_location *loc2);
131
132 static void breakpoints_info (char *, int);
133
134 static void watchpoints_info (char *, int);
135
136 static int breakpoint_1 (char *, int, 
137                          int (*) (const struct breakpoint *));
138
139 static int breakpoint_cond_eval (void *);
140
141 static void cleanup_executing_breakpoints (void *);
142
143 static void commands_command (char *, int);
144
145 static void condition_command (char *, int);
146
147 typedef enum
148   {
149     mark_inserted,
150     mark_uninserted
151   }
152 insertion_state_t;
153
154 static int remove_breakpoint (struct bp_location *, insertion_state_t);
155 static int remove_breakpoint_1 (struct bp_location *, insertion_state_t);
156
157 static enum print_stop_action print_it_typical (bpstat);
158
159 static enum print_stop_action print_bp_stop_message (bpstat bs);
160
161 static int watchpoint_check (void *);
162
163 static void maintenance_info_breakpoints (char *, int);
164
165 static int hw_breakpoint_used_count (void);
166
167 static int hw_watchpoint_used_count (enum bptype, int *);
168
169 static void hbreak_command (char *, int);
170
171 static void thbreak_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 void catch_exception_command_1 (enum exception_event_kind ex_event, 
184                                        char *arg, int tempflag, int from_tty);
185
186 static void tcatch_command (char *arg, int from_tty);
187
188 static void detach_single_step_breakpoints (void);
189
190 static int single_step_breakpoint_inserted_here_p (struct address_space *,
191                                                    CORE_ADDR pc);
192
193 static void free_bp_location (struct bp_location *loc);
194 static void incref_bp_location (struct bp_location *loc);
195 static void decref_bp_location (struct bp_location **loc);
196
197 static struct bp_location *allocate_bp_location (struct breakpoint *bpt);
198
199 static void update_global_location_list (int);
200
201 static void update_global_location_list_nothrow (int);
202
203 static int is_hardware_watchpoint (const struct breakpoint *bpt);
204
205 static int is_watchpoint (const struct breakpoint *bpt);
206
207 static void insert_breakpoint_locations (void);
208
209 static int syscall_catchpoint_p (struct breakpoint *b);
210
211 static void tracepoints_info (char *, int);
212
213 static void delete_trace_command (char *, int);
214
215 static void enable_trace_command (char *, int);
216
217 static void disable_trace_command (char *, int);
218
219 static void trace_pass_command (char *, int);
220
221 /* Assuming we're creating a static tracepoint, does S look like a
222    static tracepoint marker spec ("-m MARKER_ID")?  */
223 #define is_marker_spec(s)                                               \
224   (s != NULL && strncmp (s, "-m", 2) == 0 && ((s)[2] == ' ' || (s)[2] == '\t'))
225
226 /* A reference-counted struct command_line.  This lets multiple
227    breakpoints share a single command list.  */
228 struct counted_command_line
229 {
230   /* The reference count.  */
231   int refc;
232
233   /* The command list.  */
234   struct command_line *commands;
235 };
236
237 struct command_line *
238 breakpoint_commands (struct breakpoint *b)
239 {
240   return b->commands ? b->commands->commands : NULL;
241 }
242
243 /* Flag indicating that a command has proceeded the inferior past the
244    current breakpoint.  */
245
246 static int breakpoint_proceeded;
247
248 static const char *
249 bpdisp_text (enum bpdisp disp)
250 {
251   /* NOTE: the following values are a part of MI protocol and
252      represent values of 'disp' field returned when inferior stops at
253      a breakpoint.  */
254   static const char * const bpdisps[] = {"del", "dstp", "dis", "keep"};
255
256   return bpdisps[(int) disp];
257 }
258
259 /* Prototypes for exported functions.  */
260 /* If FALSE, gdb will not use hardware support for watchpoints, even
261    if such is available.  */
262 static int can_use_hw_watchpoints;
263
264 static void
265 show_can_use_hw_watchpoints (struct ui_file *file, int from_tty,
266                              struct cmd_list_element *c,
267                              const char *value)
268 {
269   fprintf_filtered (file,
270                     _("Debugger's willingness to use "
271                       "watchpoint hardware is %s.\n"),
272                     value);
273 }
274
275 /* If AUTO_BOOLEAN_FALSE, gdb will not attempt to create pending breakpoints.
276    If AUTO_BOOLEAN_TRUE, gdb will automatically create pending breakpoints
277    for unrecognized breakpoint locations.
278    If AUTO_BOOLEAN_AUTO, gdb will query when breakpoints are unrecognized.  */
279 static enum auto_boolean pending_break_support;
280 static void
281 show_pending_break_support (struct ui_file *file, int from_tty,
282                             struct cmd_list_element *c,
283                             const char *value)
284 {
285   fprintf_filtered (file,
286                     _("Debugger's behavior regarding "
287                       "pending breakpoints is %s.\n"),
288                     value);
289 }
290
291 /* If 1, gdb will automatically use hardware breakpoints for breakpoints
292    set with "break" but falling in read-only memory.
293    If 0, gdb will warn about such breakpoints, but won't automatically
294    use hardware breakpoints.  */
295 static int automatic_hardware_breakpoints;
296 static void
297 show_automatic_hardware_breakpoints (struct ui_file *file, int from_tty,
298                                      struct cmd_list_element *c,
299                                      const char *value)
300 {
301   fprintf_filtered (file,
302                     _("Automatic usage of hardware breakpoints is %s.\n"),
303                     value);
304 }
305
306 /* If on, gdb will keep breakpoints inserted even as inferior is
307    stopped, and immediately insert any new breakpoints.  If off, gdb
308    will insert breakpoints into inferior only when resuming it, and
309    will remove breakpoints upon stop.  If auto, GDB will behave as ON
310    if in non-stop mode, and as OFF if all-stop mode.*/
311
312 static const char always_inserted_auto[] = "auto";
313 static const char always_inserted_on[] = "on";
314 static const char always_inserted_off[] = "off";
315 static const char *always_inserted_enums[] = {
316   always_inserted_auto,
317   always_inserted_off,
318   always_inserted_on,
319   NULL
320 };
321 static const char *always_inserted_mode = always_inserted_auto;
322 static void
323 show_always_inserted_mode (struct ui_file *file, int from_tty,
324                      struct cmd_list_element *c, const char *value)
325 {
326   if (always_inserted_mode == always_inserted_auto)
327     fprintf_filtered (file,
328                       _("Always inserted breakpoint "
329                         "mode is %s (currently %s).\n"),
330                       value,
331                       breakpoints_always_inserted_mode () ? "on" : "off");
332   else
333     fprintf_filtered (file, _("Always inserted breakpoint mode is %s.\n"),
334                       value);
335 }
336
337 int
338 breakpoints_always_inserted_mode (void)
339 {
340   return (always_inserted_mode == always_inserted_on
341           || (always_inserted_mode == always_inserted_auto && non_stop));
342 }
343
344 void _initialize_breakpoint (void);
345
346 /* Are we executing breakpoint commands?  */
347 static int executing_breakpoint_commands;
348
349 /* Are overlay event breakpoints enabled? */
350 static int overlay_events_enabled;
351
352 /* See description in breakpoint.h. */
353 int target_exact_watchpoints = 0;
354
355 /* Walk the following statement or block through all breakpoints.
356    ALL_BREAKPOINTS_SAFE does so even if the statment deletes the
357    current breakpoint.  */
358
359 #define ALL_BREAKPOINTS(B)  for (B = breakpoint_chain; B; B = B->next)
360
361 #define ALL_BREAKPOINTS_SAFE(B,TMP)     \
362         for (B = breakpoint_chain;      \
363              B ? (TMP=B->next, 1): 0;   \
364              B = TMP)
365
366 /* Similar iterator for the low-level breakpoints.  SAFE variant is
367    not provided so update_global_location_list must not be called
368    while executing the block of ALL_BP_LOCATIONS.  */
369
370 #define ALL_BP_LOCATIONS(B,BP_TMP)                                      \
371         for (BP_TMP = bp_location;                                      \
372              BP_TMP < bp_location + bp_location_count && (B = *BP_TMP); \
373              BP_TMP++)
374
375 /* Iterator for tracepoints only.  */
376
377 #define ALL_TRACEPOINTS(B)  \
378   for (B = breakpoint_chain; B; B = B->next)  \
379     if (is_tracepoint (B))
380
381 /* Chains of all breakpoints defined.  */
382
383 struct breakpoint *breakpoint_chain;
384
385 /* Array is sorted by bp_location_compare - primarily by the ADDRESS.  */
386
387 static struct bp_location **bp_location;
388
389 /* Number of elements of BP_LOCATION.  */
390
391 static unsigned bp_location_count;
392
393 /* Maximum alignment offset between bp_target_info.PLACED_ADDRESS and
394    ADDRESS for the current elements of BP_LOCATION which get a valid
395    result from bp_location_has_shadow.  You can use it for roughly
396    limiting the subrange of BP_LOCATION to scan for shadow bytes for
397    an address you need to read.  */
398
399 static CORE_ADDR bp_location_placed_address_before_address_max;
400
401 /* Maximum offset plus alignment between bp_target_info.PLACED_ADDRESS
402    + bp_target_info.SHADOW_LEN and ADDRESS for the current elements of
403    BP_LOCATION which get a valid result from bp_location_has_shadow.
404    You can use it for roughly limiting the subrange of BP_LOCATION to
405    scan for shadow bytes for an address you need to read.  */
406
407 static CORE_ADDR bp_location_shadow_len_after_address_max;
408
409 /* The locations that no longer correspond to any breakpoint, unlinked
410    from bp_location array, but for which a hit may still be reported
411    by a target.  */
412 VEC(bp_location_p) *moribund_locations = NULL;
413
414 /* Number of last breakpoint made.  */
415
416 static int breakpoint_count;
417
418 /* The value of `breakpoint_count' before the last command that
419    created breakpoints.  If the last (break-like) command created more
420    than one breakpoint, then the difference between BREAKPOINT_COUNT
421    and PREV_BREAKPOINT_COUNT is more than one.  */
422 static int prev_breakpoint_count;
423
424 /* Number of last tracepoint made.  */
425
426 static int tracepoint_count;
427
428 static struct cmd_list_element *breakpoint_set_cmdlist;
429 static struct cmd_list_element *breakpoint_show_cmdlist;
430 struct cmd_list_element *save_cmdlist;
431
432 /* Return whether a breakpoint is an active enabled breakpoint.  */
433 static int
434 breakpoint_enabled (struct breakpoint *b)
435 {
436   return (b->enable_state == bp_enabled);
437 }
438
439 /* Set breakpoint count to NUM.  */
440
441 static void
442 set_breakpoint_count (int num)
443 {
444   prev_breakpoint_count = breakpoint_count;
445   breakpoint_count = num;
446   set_internalvar_integer (lookup_internalvar ("bpnum"), num);
447 }
448
449 /* Used by `start_rbreak_breakpoints' below, to record the current
450    breakpoint count before "rbreak" creates any breakpoint.  */
451 static int rbreak_start_breakpoint_count;
452
453 /* Called at the start an "rbreak" command to record the first
454    breakpoint made.  */
455
456 void
457 start_rbreak_breakpoints (void)
458 {
459   rbreak_start_breakpoint_count = breakpoint_count;
460 }
461
462 /* Called at the end of an "rbreak" command to record the last
463    breakpoint made.  */
464
465 void
466 end_rbreak_breakpoints (void)
467 {
468   prev_breakpoint_count = rbreak_start_breakpoint_count;
469 }
470
471 /* Used in run_command to zero the hit count when a new run starts.  */
472
473 void
474 clear_breakpoint_hit_counts (void)
475 {
476   struct breakpoint *b;
477
478   ALL_BREAKPOINTS (b)
479     b->hit_count = 0;
480 }
481
482 /* Allocate a new counted_command_line with reference count of 1.
483    The new structure owns COMMANDS.  */
484
485 static struct counted_command_line *
486 alloc_counted_command_line (struct command_line *commands)
487 {
488   struct counted_command_line *result
489     = xmalloc (sizeof (struct counted_command_line));
490
491   result->refc = 1;
492   result->commands = commands;
493   return result;
494 }
495
496 /* Increment reference count.  This does nothing if CMD is NULL.  */
497
498 static void
499 incref_counted_command_line (struct counted_command_line *cmd)
500 {
501   if (cmd)
502     ++cmd->refc;
503 }
504
505 /* Decrement reference count.  If the reference count reaches 0,
506    destroy the counted_command_line.  Sets *CMDP to NULL.  This does
507    nothing if *CMDP is NULL.  */
508
509 static void
510 decref_counted_command_line (struct counted_command_line **cmdp)
511 {
512   if (*cmdp)
513     {
514       if (--(*cmdp)->refc == 0)
515         {
516           free_command_lines (&(*cmdp)->commands);
517           xfree (*cmdp);
518         }
519       *cmdp = NULL;
520     }
521 }
522
523 /* A cleanup function that calls decref_counted_command_line.  */
524
525 static void
526 do_cleanup_counted_command_line (void *arg)
527 {
528   decref_counted_command_line (arg);
529 }
530
531 /* Create a cleanup that calls decref_counted_command_line on the
532    argument.  */
533
534 static struct cleanup *
535 make_cleanup_decref_counted_command_line (struct counted_command_line **cmdp)
536 {
537   return make_cleanup (do_cleanup_counted_command_line, cmdp);
538 }
539
540 /* Default address, symtab and line to put a breakpoint at
541    for "break" command with no arg.
542    If default_breakpoint_valid is zero, the other three are
543    not valid, and "break" with no arg is an error.
544
545    This set by print_stack_frame, which calls set_default_breakpoint.  */
546
547 int default_breakpoint_valid;
548 CORE_ADDR default_breakpoint_address;
549 struct symtab *default_breakpoint_symtab;
550 int default_breakpoint_line;
551 struct program_space *default_breakpoint_pspace;
552
553 \f
554 /* Return the breakpoint with the specified number, or NULL
555    if the number does not refer to an existing breakpoint.  */
556
557 struct breakpoint *
558 get_breakpoint (int num)
559 {
560   struct breakpoint *b;
561
562   ALL_BREAKPOINTS (b)
563     if (b->number == num)
564       return b;
565   
566   return NULL;
567 }
568
569 \f
570
571 void
572 set_breakpoint_condition (struct breakpoint *b, char *exp,
573                           int from_tty)
574 {
575   struct bp_location *loc = b->loc;
576
577   for (; loc; loc = loc->next)
578     {
579       xfree (loc->cond);
580       loc->cond = NULL;
581     }
582   xfree (b->cond_string);
583   b->cond_string = NULL;
584   xfree (b->cond_exp);
585   b->cond_exp = NULL;
586
587   if (*exp == 0)
588     {
589       if (from_tty)
590         printf_filtered (_("Breakpoint %d now unconditional.\n"), b->number);
591     }
592   else
593     {
594       char *arg = exp;
595
596       /* I don't know if it matters whether this is the string the user
597          typed in or the decompiled expression.  */
598       b->cond_string = xstrdup (arg);
599       b->condition_not_parsed = 0;
600
601       if (is_watchpoint (b))
602         {
603           innermost_block = NULL;
604           arg = exp;
605           b->cond_exp = parse_exp_1 (&arg, 0, 0);
606           if (*arg)
607             error (_("Junk at end of expression"));
608           b->cond_exp_valid_block = innermost_block;
609         }
610       else
611         {
612           for (loc = b->loc; loc; loc = loc->next)
613             {
614               arg = exp;
615               loc->cond =
616                 parse_exp_1 (&arg, block_for_pc (loc->address), 0);
617               if (*arg)
618                 error (_("Junk at end of expression"));
619             }
620         }
621     }
622   breakpoints_changed ();
623   observer_notify_breakpoint_modified (b->number);
624 }
625
626 /* condition N EXP -- set break condition of breakpoint N to EXP.  */
627
628 static void
629 condition_command (char *arg, int from_tty)
630 {
631   struct breakpoint *b;
632   char *p;
633   int bnum;
634
635   if (arg == 0)
636     error_no_arg (_("breakpoint number"));
637
638   p = arg;
639   bnum = get_number (&p);
640   if (bnum == 0)
641     error (_("Bad breakpoint argument: '%s'"), arg);
642
643   ALL_BREAKPOINTS (b)
644     if (b->number == bnum)
645       {
646         set_breakpoint_condition (b, p, from_tty);
647         return;
648       }
649
650   error (_("No breakpoint number %d."), bnum);
651 }
652
653 /* Check that COMMAND do not contain commands that are suitable
654    only for tracepoints and not suitable for ordinary breakpoints.
655    Throw if any such commands is found.  */
656
657 static void
658 check_no_tracepoint_commands (struct command_line *commands)
659 {
660   struct command_line *c;
661
662   for (c = commands; c; c = c->next)
663     {
664       int i;
665
666       if (c->control_type == while_stepping_control)
667         error (_("The 'while-stepping' command can "
668                  "only be used for tracepoints"));
669
670       for (i = 0; i < c->body_count; ++i)
671         check_no_tracepoint_commands ((c->body_list)[i]);
672
673       /* Not that command parsing removes leading whitespace and comment
674          lines and also empty lines.  So, we only need to check for
675          command directly.  */
676       if (strstr (c->line, "collect ") == c->line)
677         error (_("The 'collect' command can only be used for tracepoints"));
678
679       if (strstr (c->line, "teval ") == c->line)
680         error (_("The 'teval' command can only be used for tracepoints"));
681     }
682 }
683
684 /* Encapsulate tests for different types of tracepoints.  */
685
686 int
687 is_tracepoint (const struct breakpoint *b)
688 {
689   return (b->type == bp_tracepoint
690           || b->type == bp_fast_tracepoint
691           || b->type == bp_static_tracepoint);
692 }
693   
694 /* A helper function that validsates that COMMANDS are valid for a
695    breakpoint.  This function will throw an exception if a problem is
696    found.  */
697
698 static void
699 validate_commands_for_breakpoint (struct breakpoint *b,
700                                   struct command_line *commands)
701 {
702   if (is_tracepoint (b))
703     {
704       /* We need to verify that each top-level element of commands is
705          valid for tracepoints, that there's at most one
706          while-stepping element, and that while-stepping's body has
707          valid tracing commands excluding nested while-stepping.  */
708       struct command_line *c;
709       struct command_line *while_stepping = 0;
710       for (c = commands; c; c = c->next)
711         {
712           if (c->control_type == while_stepping_control)
713             {
714               if (b->type == bp_fast_tracepoint)
715                 error (_("The 'while-stepping' command "
716                          "cannot be used for fast tracepoint"));
717               else if (b->type == bp_static_tracepoint)
718                 error (_("The 'while-stepping' command "
719                          "cannot be used for static tracepoint"));
720
721               if (while_stepping)
722                 error (_("The 'while-stepping' command "
723                          "can be used only once"));
724               else
725                 while_stepping = c;
726             }
727         }
728       if (while_stepping)
729         {
730           struct command_line *c2;
731
732           gdb_assert (while_stepping->body_count == 1);
733           c2 = while_stepping->body_list[0];
734           for (; c2; c2 = c2->next)
735             {
736               if (c2->control_type == while_stepping_control)
737                 error (_("The 'while-stepping' command cannot be nested"));
738             }
739         }
740     }
741   else
742     {
743       check_no_tracepoint_commands (commands);
744     }
745 }
746
747 /* Return a vector of all the static tracepoints set at ADDR.  The
748    caller is responsible for releasing the vector.  */
749
750 VEC(breakpoint_p) *
751 static_tracepoints_here (CORE_ADDR addr)
752 {
753   struct breakpoint *b;
754   VEC(breakpoint_p) *found = 0;
755   struct bp_location *loc;
756
757   ALL_BREAKPOINTS (b)
758     if (b->type == bp_static_tracepoint)
759       {
760         for (loc = b->loc; loc; loc = loc->next)
761           if (loc->address == addr)
762             VEC_safe_push(breakpoint_p, found, b);
763       }
764
765   return found;
766 }
767
768 /* Set the command list of B to COMMANDS.  If breakpoint is tracepoint,
769    validate that only allowed commands are included.  */
770
771 void
772 breakpoint_set_commands (struct breakpoint *b, 
773                          struct command_line *commands)
774 {
775   validate_commands_for_breakpoint (b, commands);
776
777   decref_counted_command_line (&b->commands);
778   b->commands = alloc_counted_command_line (commands);
779   breakpoints_changed ();
780   observer_notify_breakpoint_modified (b->number);
781 }
782
783 /* Set the internal `silent' flag on the breakpoint.  Note that this
784    is not the same as the "silent" that may appear in the breakpoint's
785    commands.  */
786
787 void
788 breakpoint_set_silent (struct breakpoint *b, int silent)
789 {
790   int old_silent = b->silent;
791
792   b->silent = silent;
793   if (old_silent != silent)
794     observer_notify_breakpoint_modified (b->number);
795 }
796
797 /* Set the thread for this breakpoint.  If THREAD is -1, make the
798    breakpoint work for any thread.  */
799
800 void
801 breakpoint_set_thread (struct breakpoint *b, int thread)
802 {
803   int old_thread = b->thread;
804
805   b->thread = thread;
806   if (old_thread != thread)
807     observer_notify_breakpoint_modified (b->number);
808 }
809
810 /* Set the task for this breakpoint.  If TASK is 0, make the
811    breakpoint work for any task.  */
812
813 void
814 breakpoint_set_task (struct breakpoint *b, int task)
815 {
816   int old_task = b->task;
817
818   b->task = task;
819   if (old_task != task)
820     observer_notify_breakpoint_modified (b->number);
821 }
822
823 void
824 check_tracepoint_command (char *line, void *closure)
825 {
826   struct breakpoint *b = closure;
827
828   validate_actionline (&line, b);
829 }
830
831 /* A structure used to pass information through
832    map_breakpoint_numbers.  */
833
834 struct commands_info
835 {
836   /* True if the command was typed at a tty.  */
837   int from_tty;
838
839   /* The breakpoint range spec.  */
840   char *arg;
841
842   /* Non-NULL if the body of the commands are being read from this
843      already-parsed command.  */
844   struct command_line *control;
845
846   /* The command lines read from the user, or NULL if they have not
847      yet been read.  */
848   struct counted_command_line *cmd;
849 };
850
851 /* A callback for map_breakpoint_numbers that sets the commands for
852    commands_command.  */
853
854 static void
855 do_map_commands_command (struct breakpoint *b, void *data)
856 {
857   struct commands_info *info = data;
858
859   if (info->cmd == NULL)
860     {
861       struct command_line *l;
862
863       if (info->control != NULL)
864         l = copy_command_lines (info->control->body_list[0]);
865       else
866         {
867           struct cleanup *old_chain;
868           char *str;
869
870           str = xstrprintf (_("Type commands for breakpoint(s) "
871                               "%s, one per line."),
872                             info->arg);
873
874           old_chain = make_cleanup (xfree, str);
875
876           l = read_command_lines (str,
877                                   info->from_tty, 1,
878                                   (is_tracepoint (b)
879                                    ? check_tracepoint_command : 0),
880                                   b);
881
882           do_cleanups (old_chain);
883         }
884
885       info->cmd = alloc_counted_command_line (l);
886     }
887
888   /* If a breakpoint was on the list more than once, we don't need to
889      do anything.  */
890   if (b->commands != info->cmd)
891     {
892       validate_commands_for_breakpoint (b, info->cmd->commands);
893       incref_counted_command_line (info->cmd);
894       decref_counted_command_line (&b->commands);
895       b->commands = info->cmd;
896       breakpoints_changed ();
897       observer_notify_breakpoint_modified (b->number);
898     }
899 }
900
901 static void
902 commands_command_1 (char *arg, int from_tty, 
903                     struct command_line *control)
904 {
905   struct cleanup *cleanups;
906   struct commands_info info;
907
908   info.from_tty = from_tty;
909   info.control = control;
910   info.cmd = NULL;
911   /* If we read command lines from the user, then `info' will hold an
912      extra reference to the commands that we must clean up.  */
913   cleanups = make_cleanup_decref_counted_command_line (&info.cmd);
914
915   if (arg == NULL || !*arg)
916     {
917       if (breakpoint_count - prev_breakpoint_count > 1)
918         arg = xstrprintf ("%d-%d", prev_breakpoint_count + 1, 
919                           breakpoint_count);
920       else if (breakpoint_count > 0)
921         arg = xstrprintf ("%d", breakpoint_count);
922       else
923         {
924           /* So that we don't try to free the incoming non-NULL
925              argument in the cleanup below.  Mapping breakpoint
926              numbers will fail in this case.  */
927           arg = NULL;
928         }
929     }
930   else
931     /* The command loop has some static state, so we need to preserve
932        our argument.  */
933     arg = xstrdup (arg);
934
935   if (arg != NULL)
936     make_cleanup (xfree, arg);
937
938   info.arg = arg;
939
940   map_breakpoint_numbers (arg, do_map_commands_command, &info);
941
942   if (info.cmd == NULL)
943     error (_("No breakpoints specified."));
944
945   do_cleanups (cleanups);
946 }
947
948 static void
949 commands_command (char *arg, int from_tty)
950 {
951   commands_command_1 (arg, from_tty, NULL);
952 }
953
954 /* Like commands_command, but instead of reading the commands from
955    input stream, takes them from an already parsed command structure.
956
957    This is used by cli-script.c to DTRT with breakpoint commands
958    that are part of if and while bodies.  */
959 enum command_control_type
960 commands_from_control_command (char *arg, struct command_line *cmd)
961 {
962   commands_command_1 (arg, 0, cmd);
963   return simple_control;
964 }
965
966 /* Return non-zero if BL->TARGET_INFO contains valid information.  */
967
968 static int
969 bp_location_has_shadow (struct bp_location *bl)
970 {
971   if (bl->loc_type != bp_loc_software_breakpoint)
972     return 0;
973   if (!bl->inserted)
974     return 0;
975   if (bl->target_info.shadow_len == 0)
976     /* bp isn't valid, or doesn't shadow memory.  */
977     return 0;
978   return 1;
979 }
980
981 /* Update BUF, which is LEN bytes read from the target address MEMADDR,
982    by replacing any memory breakpoints with their shadowed contents.
983
984    The range of shadowed area by each bp_location is:
985      bl->address - bp_location_placed_address_before_address_max
986      up to bl->address + bp_location_shadow_len_after_address_max
987    The range we were requested to resolve shadows for is:
988      memaddr ... memaddr + len
989    Thus the safe cutoff boundaries for performance optimization are
990      memaddr + len <= (bl->address
991                        - bp_location_placed_address_before_address_max)
992    and:
993      bl->address + bp_location_shadow_len_after_address_max <= memaddr  */
994
995 void
996 breakpoint_restore_shadows (gdb_byte *buf, ULONGEST memaddr, LONGEST len)
997 {
998   /* Left boundary, right boundary and median element of our binary
999      search.  */
1000   unsigned bc_l, bc_r, bc;
1001
1002   /* Find BC_L which is a leftmost element which may affect BUF
1003      content.  It is safe to report lower value but a failure to
1004      report higher one.  */
1005
1006   bc_l = 0;
1007   bc_r = bp_location_count;
1008   while (bc_l + 1 < bc_r)
1009     {
1010       struct bp_location *bl;
1011
1012       bc = (bc_l + bc_r) / 2;
1013       bl = bp_location[bc];
1014
1015       /* Check first BL->ADDRESS will not overflow due to the added
1016          constant.  Then advance the left boundary only if we are sure
1017          the BC element can in no way affect the BUF content (MEMADDR
1018          to MEMADDR + LEN range).
1019
1020          Use the BP_LOCATION_SHADOW_LEN_AFTER_ADDRESS_MAX safety
1021          offset so that we cannot miss a breakpoint with its shadow
1022          range tail still reaching MEMADDR.  */
1023
1024       if ((bl->address + bp_location_shadow_len_after_address_max
1025            >= bl->address)
1026           && (bl->address + bp_location_shadow_len_after_address_max
1027               <= memaddr))
1028         bc_l = bc;
1029       else
1030         bc_r = bc;
1031     }
1032
1033   /* Due to the binary search above, we need to make sure we pick the
1034      first location that's at BC_L's address.  E.g., if there are
1035      multiple locations at the same address, BC_L may end up pointing
1036      at a duplicate location, and miss the "master"/"inserted"
1037      location.  Say, given locations L1, L2 and L3 at addresses A and
1038      B:
1039
1040       L1@A, L2@A, L3@B, ...
1041
1042      BC_L could end up pointing at location L2, while the "master"
1043      location could be L1.  Since the `loc->inserted' flag is only set
1044      on "master" locations, we'd forget to restore the shadow of L1
1045      and L2.  */
1046   while (bc_l > 0
1047          && bp_location[bc_l]->address == bp_location[bc_l - 1]->address)
1048     bc_l--;
1049
1050   /* Now do full processing of the found relevant range of elements.  */
1051
1052   for (bc = bc_l; bc < bp_location_count; bc++)
1053   {
1054     struct bp_location *bl = bp_location[bc];
1055     CORE_ADDR bp_addr = 0;
1056     int bp_size = 0;
1057     int bptoffset = 0;
1058
1059     /* bp_location array has BL->OWNER always non-NULL.  */
1060     if (bl->owner->type == bp_none)
1061       warning (_("reading through apparently deleted breakpoint #%d?"),
1062                bl->owner->number);
1063
1064     /* Performance optimization: any futher element can no longer affect BUF
1065        content.  */
1066
1067     if (bl->address >= bp_location_placed_address_before_address_max
1068         && memaddr + len <= (bl->address
1069                              - bp_location_placed_address_before_address_max))
1070       break;
1071
1072     if (!bp_location_has_shadow (bl))
1073       continue;
1074     if (!breakpoint_address_match (bl->target_info.placed_address_space, 0,
1075                                    current_program_space->aspace, 0))
1076       continue;
1077
1078     /* Addresses and length of the part of the breakpoint that
1079        we need to copy.  */
1080     bp_addr = bl->target_info.placed_address;
1081     bp_size = bl->target_info.shadow_len;
1082
1083     if (bp_addr + bp_size <= memaddr)
1084       /* The breakpoint is entirely before the chunk of memory we
1085          are reading.  */
1086       continue;
1087
1088     if (bp_addr >= memaddr + len)
1089       /* The breakpoint is entirely after the chunk of memory we are
1090          reading.  */
1091       continue;
1092
1093     /* Offset within shadow_contents.  */
1094     if (bp_addr < memaddr)
1095       {
1096         /* Only copy the second part of the breakpoint.  */
1097         bp_size -= memaddr - bp_addr;
1098         bptoffset = memaddr - bp_addr;
1099         bp_addr = memaddr;
1100       }
1101
1102     if (bp_addr + bp_size > memaddr + len)
1103       {
1104         /* Only copy the first part of the breakpoint.  */
1105         bp_size -= (bp_addr + bp_size) - (memaddr + len);
1106       }
1107
1108     memcpy (buf + bp_addr - memaddr,
1109             bl->target_info.shadow_contents + bptoffset, bp_size);
1110   }
1111 }
1112 \f
1113
1114 /* Return true if BPT is of any hardware watchpoint kind.  */
1115
1116 static int
1117 is_hardware_watchpoint (const struct breakpoint *bpt)
1118 {
1119   return (bpt->type == bp_hardware_watchpoint
1120           || bpt->type == bp_read_watchpoint
1121           || bpt->type == bp_access_watchpoint);
1122 }
1123
1124 /* Return true if BPT is of any watchpoint kind, hardware or
1125    software.  */
1126
1127 static int
1128 is_watchpoint (const struct breakpoint *bpt)
1129 {
1130   return (is_hardware_watchpoint (bpt)
1131           || bpt->type == bp_watchpoint);
1132 }
1133
1134 /* Assuming that B is a watchpoint: returns true if the current thread
1135    and its running state are safe to evaluate or update watchpoint B.
1136    Watchpoints on local expressions need to be evaluated in the
1137    context of the thread that was current when the watchpoint was
1138    created, and, that thread needs to be stopped to be able to select
1139    the correct frame context.  Watchpoints on global expressions can
1140    be evaluated on any thread, and in any state.  It is presently left
1141    to the target allowing memory accesses when threads are
1142    running.  */
1143
1144 static int
1145 watchpoint_in_thread_scope (struct breakpoint *b)
1146 {
1147   return (ptid_equal (b->watchpoint_thread, null_ptid)
1148           || (ptid_equal (inferior_ptid, b->watchpoint_thread)
1149               && !is_executing (inferior_ptid)));
1150 }
1151
1152 /* Assuming that B is a watchpoint:
1153    - Reparse watchpoint expression, if REPARSE is non-zero
1154    - Evaluate expression and store the result in B->val
1155    - Evaluate the condition if there is one, and store the result
1156      in b->loc->cond.
1157    - Update the list of values that must be watched in B->loc.
1158
1159    If the watchpoint disposition is disp_del_at_next_stop, then do
1160    nothing.  If this is local watchpoint that is out of scope, delete
1161    it.
1162
1163    Even with `set breakpoint always-inserted on' the watchpoints are
1164    removed + inserted on each stop here.  Normal breakpoints must
1165    never be removed because they might be missed by a running thread
1166    when debugging in non-stop mode.  On the other hand, hardware
1167    watchpoints (is_hardware_watchpoint; processed here) are specific
1168    to each LWP since they are stored in each LWP's hardware debug
1169    registers.  Therefore, such LWP must be stopped first in order to
1170    be able to modify its hardware watchpoints.
1171
1172    Hardware watchpoints must be reset exactly once after being
1173    presented to the user.  It cannot be done sooner, because it would
1174    reset the data used to present the watchpoint hit to the user.  And
1175    it must not be done later because it could display the same single
1176    watchpoint hit during multiple GDB stops.  Note that the latter is
1177    relevant only to the hardware watchpoint types bp_read_watchpoint
1178    and bp_access_watchpoint.  False hit by bp_hardware_watchpoint is
1179    not user-visible - its hit is suppressed if the memory content has
1180    not changed.
1181
1182    The following constraints influence the location where we can reset
1183    hardware watchpoints:
1184
1185    * target_stopped_by_watchpoint and target_stopped_data_address are
1186      called several times when GDB stops.
1187
1188    [linux] 
1189    * Multiple hardware watchpoints can be hit at the same time,
1190      causing GDB to stop.  GDB only presents one hardware watchpoint
1191      hit at a time as the reason for stopping, and all the other hits
1192      are presented later, one after the other, each time the user
1193      requests the execution to be resumed.  Execution is not resumed
1194      for the threads still having pending hit event stored in
1195      LWP_INFO->STATUS.  While the watchpoint is already removed from
1196      the inferior on the first stop the thread hit event is kept being
1197      reported from its cached value by linux_nat_stopped_data_address
1198      until the real thread resume happens after the watchpoint gets
1199      presented and thus its LWP_INFO->STATUS gets reset.
1200
1201    Therefore the hardware watchpoint hit can get safely reset on the
1202    watchpoint removal from inferior.  */
1203
1204 static void
1205 update_watchpoint (struct breakpoint *b, int reparse)
1206 {
1207   int within_current_scope;
1208   struct frame_id saved_frame_id;
1209   int frame_saved;
1210
1211   /* If this is a local watchpoint, we only want to check if the
1212      watchpoint frame is in scope if the current thread is the thread
1213      that was used to create the watchpoint.  */
1214   if (!watchpoint_in_thread_scope (b))
1215     return;
1216
1217   if (b->disposition == disp_del_at_next_stop)
1218     return;
1219  
1220   frame_saved = 0;
1221
1222   /* Determine if the watchpoint is within scope.  */
1223   if (b->exp_valid_block == NULL)
1224     within_current_scope = 1;
1225   else
1226     {
1227       struct frame_info *fi = get_current_frame ();
1228       struct gdbarch *frame_arch = get_frame_arch (fi);
1229       CORE_ADDR frame_pc = get_frame_pc (fi);
1230
1231       /* If we're in a function epilogue, unwinding may not work
1232          properly, so do not attempt to recreate locations at this
1233          point.  See similar comments in watchpoint_check.  */
1234       if (gdbarch_in_function_epilogue_p (frame_arch, frame_pc))
1235         return;
1236
1237       /* Save the current frame's ID so we can restore it after
1238          evaluating the watchpoint expression on its own frame.  */
1239       /* FIXME drow/2003-09-09: It would be nice if evaluate_expression
1240          took a frame parameter, so that we didn't have to change the
1241          selected frame.  */
1242       frame_saved = 1;
1243       saved_frame_id = get_frame_id (get_selected_frame (NULL));
1244
1245       fi = frame_find_by_id (b->watchpoint_frame);
1246       within_current_scope = (fi != NULL);
1247       if (within_current_scope)
1248         select_frame (fi);
1249     }
1250
1251   /* We don't free locations.  They are stored in the bp_location array
1252      and update_global_location_list will eventually delete them and
1253      remove breakpoints if needed.  */
1254   b->loc = NULL;
1255
1256   if (within_current_scope && reparse)
1257     {
1258       char *s;
1259
1260       if (b->exp)
1261         {
1262           xfree (b->exp);
1263           b->exp = NULL;
1264         }
1265       s = b->exp_string_reparse ? b->exp_string_reparse : b->exp_string;
1266       b->exp = parse_exp_1 (&s, b->exp_valid_block, 0);
1267       /* If the meaning of expression itself changed, the old value is
1268          no longer relevant.  We don't want to report a watchpoint hit
1269          to the user when the old value and the new value may actually
1270          be completely different objects.  */
1271       value_free (b->val);
1272       b->val = NULL;
1273       b->val_valid = 0;
1274
1275       /* Note that unlike with breakpoints, the watchpoint's condition
1276          expression is stored in the breakpoint object, not in the
1277          locations (re)created below.  */
1278       if (b->cond_string != NULL)
1279         {
1280           if (b->cond_exp != NULL)
1281             {
1282               xfree (b->cond_exp);
1283               b->cond_exp = NULL;
1284             }
1285
1286           s = b->cond_string;
1287           b->cond_exp = parse_exp_1 (&s, b->cond_exp_valid_block, 0);
1288         }
1289     }
1290
1291   /* If we failed to parse the expression, for example because
1292      it refers to a global variable in a not-yet-loaded shared library,
1293      don't try to insert watchpoint.  We don't automatically delete
1294      such watchpoint, though, since failure to parse expression
1295      is different from out-of-scope watchpoint.  */
1296   if ( !target_has_execution)
1297     {
1298       /* Without execution, memory can't change.  No use to try and
1299          set watchpoint locations.  The watchpoint will be reset when
1300          the target gains execution, through breakpoint_re_set.  */
1301     }
1302   else if (within_current_scope && b->exp)
1303     {
1304       int pc = 0;
1305       struct value *val_chain, *v, *result, *next;
1306       struct program_space *frame_pspace;
1307
1308       fetch_subexp_value (b->exp, &pc, &v, &result, &val_chain);
1309
1310       /* Avoid setting b->val if it's already set.  The meaning of
1311          b->val is 'the last value' user saw, and we should update
1312          it only if we reported that last value to user.  As it
1313          happens, the code that reports it updates b->val directly.  */
1314       if (!b->val_valid)
1315         {
1316           b->val = v;
1317           b->val_valid = 1;
1318         }
1319
1320       frame_pspace = get_frame_program_space (get_selected_frame (NULL));
1321
1322       /* Look at each value on the value chain.  */
1323       for (v = val_chain; v; v = value_next (v))
1324         {
1325           /* If it's a memory location, and GDB actually needed
1326              its contents to evaluate the expression, then we
1327              must watch it.  If the first value returned is
1328              still lazy, that means an error occurred reading it;
1329              watch it anyway in case it becomes readable.  */
1330           if (VALUE_LVAL (v) == lval_memory
1331               && (v == val_chain || ! value_lazy (v)))
1332             {
1333               struct type *vtype = check_typedef (value_type (v));
1334
1335               /* We only watch structs and arrays if user asked
1336                  for it explicitly, never if they just happen to
1337                  appear in the middle of some value chain.  */
1338               if (v == result
1339                   || (TYPE_CODE (vtype) != TYPE_CODE_STRUCT
1340                       && TYPE_CODE (vtype) != TYPE_CODE_ARRAY))
1341                 {
1342                   CORE_ADDR addr;
1343                   int len, type;
1344                   struct bp_location *loc, **tmp;
1345
1346                   addr = value_address (v);
1347                   len = TYPE_LENGTH (value_type (v));
1348                   type = hw_write;
1349                   if (b->type == bp_read_watchpoint)
1350                     type = hw_read;
1351                   else if (b->type == bp_access_watchpoint)
1352                     type = hw_access;
1353                   
1354                   loc = allocate_bp_location (b);
1355                   for (tmp = &(b->loc); *tmp != NULL; tmp = &((*tmp)->next))
1356                     ;
1357                   *tmp = loc;
1358                   loc->gdbarch = get_type_arch (value_type (v));
1359
1360                   loc->pspace = frame_pspace;
1361                   loc->address = addr;
1362                   loc->length = len;
1363                   loc->watchpoint_type = type;
1364                 }
1365             }
1366         }
1367
1368       /* Change the type of breakpoint between hardware assisted or
1369          an ordinary watchpoint depending on the hardware support
1370          and free hardware slots.  REPARSE is set when the inferior
1371          is started.  */
1372       if ((b->type == bp_watchpoint || b->type == bp_hardware_watchpoint)
1373           && reparse)
1374         {
1375           int reg_cnt;
1376           enum bp_loc_type loc_type;
1377           struct bp_location *bl;
1378
1379           reg_cnt = can_use_hardware_watchpoint (val_chain, b->exact);
1380
1381           if (reg_cnt)
1382             {
1383               int i, target_resources_ok, other_type_used;
1384               enum enable_state orig_enable_state;
1385
1386               /* We need to determine how many resources are already
1387                  used for all other hardware watchpoints plus this one
1388                  to see if we still have enough resources to also fit
1389                  this watchpoint in as well.  To guarantee the
1390                  hw_watchpoint_used_count call below counts this
1391                  watchpoint, make sure that it is marked as a hardware
1392                  watchpoint.  */
1393               b->type = bp_hardware_watchpoint;
1394
1395               /* hw_watchpoint_used_count ignores disabled watchpoints,
1396                  and b might be disabled if we're being called from
1397                  do_enable_breakpoint.  */
1398               orig_enable_state = b->enable_state;
1399               b->enable_state = bp_enabled;
1400
1401               i = hw_watchpoint_used_count (bp_hardware_watchpoint,
1402                                             &other_type_used);
1403
1404               b->enable_state = orig_enable_state;
1405
1406               target_resources_ok = target_can_use_hardware_watchpoint
1407                     (bp_hardware_watchpoint, i, other_type_used);
1408               if (target_resources_ok <= 0)
1409                 b->type = bp_watchpoint;
1410             }
1411           else
1412             b->type = bp_watchpoint;
1413
1414           loc_type = (b->type == bp_watchpoint? bp_loc_other
1415                       : bp_loc_hardware_watchpoint);
1416           for (bl = b->loc; bl; bl = bl->next)
1417             bl->loc_type = loc_type;
1418         }
1419
1420       for (v = val_chain; v; v = next)
1421         {
1422           next = value_next (v);
1423           if (v != b->val)
1424             value_free (v);
1425         }
1426
1427       /* If a software watchpoint is not watching any memory, then the
1428          above left it without any location set up.  But,
1429          bpstat_stop_status requires a location to be able to report
1430          stops, so make sure there's at least a dummy one.  */
1431       if (b->type == bp_watchpoint && b->loc == NULL)
1432         {
1433           b->loc = allocate_bp_location (b);
1434           b->loc->pspace = frame_pspace;
1435           b->loc->address = -1;
1436           b->loc->length = -1;
1437           b->loc->watchpoint_type = -1;
1438         }
1439     }
1440   else if (!within_current_scope)
1441     {
1442       printf_filtered (_("\
1443 Watchpoint %d deleted because the program has left the block\n\
1444 in which its expression is valid.\n"),
1445                        b->number);
1446       if (b->related_breakpoint)
1447         {
1448           b->related_breakpoint->disposition = disp_del_at_next_stop;
1449           b->related_breakpoint->related_breakpoint = NULL;
1450           b->related_breakpoint= NULL;
1451         }
1452       b->disposition = disp_del_at_next_stop;
1453     }
1454
1455   /* Restore the selected frame.  */
1456   if (frame_saved)
1457     select_frame (frame_find_by_id (saved_frame_id));
1458 }
1459
1460
1461 /* Returns 1 iff breakpoint location should be
1462    inserted in the inferior.  */
1463 static int
1464 should_be_inserted (struct bp_location *bl)
1465 {
1466   if (bl->owner == NULL || !breakpoint_enabled (bl->owner))
1467     return 0;
1468
1469   if (bl->owner->disposition == disp_del_at_next_stop)
1470     return 0;
1471
1472   if (!bl->enabled || bl->shlib_disabled || bl->duplicate)
1473     return 0;
1474
1475   /* This is set for example, when we're attached to the parent of a
1476      vfork, and have detached from the child.  The child is running
1477      free, and we expect it to do an exec or exit, at which point the
1478      OS makes the parent schedulable again (and the target reports
1479      that the vfork is done).  Until the child is done with the shared
1480      memory region, do not insert breakpoints in the parent, otherwise
1481      the child could still trip on the parent's breakpoints.  Since
1482      the parent is blocked anyway, it won't miss any breakpoint.  */
1483   if (bl->pspace->breakpoints_not_allowed)
1484     return 0;
1485
1486   /* Tracepoints are inserted by the target at a time of its choosing,
1487      not by us.  */
1488   if (is_tracepoint (bl->owner))
1489     return 0;
1490
1491   return 1;
1492 }
1493
1494 /* Insert a low-level "breakpoint" of some type.  BL is the breakpoint
1495    location.  Any error messages are printed to TMP_ERROR_STREAM; and
1496    DISABLED_BREAKS, and HW_BREAKPOINT_ERROR are used to report problems.
1497
1498    NOTE drow/2003-09-09: This routine could be broken down to an
1499    object-style method for each breakpoint or catchpoint type.  */
1500 static int
1501 insert_bp_location (struct bp_location *bl,
1502                     struct ui_file *tmp_error_stream,
1503                     int *disabled_breaks,
1504                     int *hw_breakpoint_error)
1505 {
1506   int val = 0;
1507
1508   if (!should_be_inserted (bl) || bl->inserted)
1509     return 0;
1510
1511   /* Initialize the target-specific information.  */
1512   memset (&bl->target_info, 0, sizeof (bl->target_info));
1513   bl->target_info.placed_address = bl->address;
1514   bl->target_info.placed_address_space = bl->pspace->aspace;
1515
1516   if (bl->loc_type == bp_loc_software_breakpoint
1517       || bl->loc_type == bp_loc_hardware_breakpoint)
1518     {
1519       if (bl->owner->type != bp_hardware_breakpoint)
1520         {
1521           /* If the explicitly specified breakpoint type
1522              is not hardware breakpoint, check the memory map to see
1523              if the breakpoint address is in read only memory or not.
1524
1525              Two important cases are:
1526              - location type is not hardware breakpoint, memory
1527              is readonly.  We change the type of the location to
1528              hardware breakpoint.
1529              - location type is hardware breakpoint, memory is
1530              read-write.  This means we've previously made the
1531              location hardware one, but then the memory map changed,
1532              so we undo.
1533              
1534              When breakpoints are removed, remove_breakpoints will use
1535              location types we've just set here, the only possible
1536              problem is that memory map has changed during running
1537              program, but it's not going to work anyway with current
1538              gdb.  */
1539           struct mem_region *mr 
1540             = lookup_mem_region (bl->target_info.placed_address);
1541           
1542           if (mr)
1543             {
1544               if (automatic_hardware_breakpoints)
1545                 {
1546                   enum bp_loc_type new_type;
1547                   
1548                   if (mr->attrib.mode != MEM_RW)
1549                     new_type = bp_loc_hardware_breakpoint;
1550                   else 
1551                     new_type = bp_loc_software_breakpoint;
1552                   
1553                   if (new_type != bl->loc_type)
1554                     {
1555                       static int said = 0;
1556
1557                       bl->loc_type = new_type;
1558                       if (!said)
1559                         {
1560                           fprintf_filtered (gdb_stdout,
1561                                             _("Note: automatically using "
1562                                               "hardware breakpoints for "
1563                                               "read-only addresses.\n"));
1564                           said = 1;
1565                         }
1566                     }
1567                 }
1568               else if (bl->loc_type == bp_loc_software_breakpoint
1569                        && mr->attrib.mode != MEM_RW)        
1570                 warning (_("cannot set software breakpoint "
1571                            "at readonly address %s"),
1572                          paddress (bl->gdbarch, bl->address));
1573             }
1574         }
1575         
1576       /* First check to see if we have to handle an overlay.  */
1577       if (overlay_debugging == ovly_off
1578           || bl->section == NULL
1579           || !(section_is_overlay (bl->section)))
1580         {
1581           /* No overlay handling: just set the breakpoint.  */
1582
1583           if (bl->loc_type == bp_loc_hardware_breakpoint)
1584             val = target_insert_hw_breakpoint (bl->gdbarch,
1585                                                &bl->target_info);
1586           else
1587             val = target_insert_breakpoint (bl->gdbarch,
1588                                             &bl->target_info);
1589         }
1590       else
1591         {
1592           /* This breakpoint is in an overlay section.
1593              Shall we set a breakpoint at the LMA?  */
1594           if (!overlay_events_enabled)
1595             {
1596               /* Yes -- overlay event support is not active, 
1597                  so we must try to set a breakpoint at the LMA.
1598                  This will not work for a hardware breakpoint.  */
1599               if (bl->loc_type == bp_loc_hardware_breakpoint)
1600                 warning (_("hardware breakpoint %d not supported in overlay!"),
1601                          bl->owner->number);
1602               else
1603                 {
1604                   CORE_ADDR addr = overlay_unmapped_address (bl->address,
1605                                                              bl->section);
1606                   /* Set a software (trap) breakpoint at the LMA.  */
1607                   bl->overlay_target_info = bl->target_info;
1608                   bl->overlay_target_info.placed_address = addr;
1609                   val = target_insert_breakpoint (bl->gdbarch,
1610                                                   &bl->overlay_target_info);
1611                   if (val != 0)
1612                     fprintf_unfiltered (tmp_error_stream,
1613                                         "Overlay breakpoint %d "
1614                                         "failed: in ROM?\n",
1615                                         bl->owner->number);
1616                 }
1617             }
1618           /* Shall we set a breakpoint at the VMA? */
1619           if (section_is_mapped (bl->section))
1620             {
1621               /* Yes.  This overlay section is mapped into memory.  */
1622               if (bl->loc_type == bp_loc_hardware_breakpoint)
1623                 val = target_insert_hw_breakpoint (bl->gdbarch,
1624                                                    &bl->target_info);
1625               else
1626                 val = target_insert_breakpoint (bl->gdbarch,
1627                                                 &bl->target_info);
1628             }
1629           else
1630             {
1631               /* No.  This breakpoint will not be inserted.  
1632                  No error, but do not mark the bp as 'inserted'.  */
1633               return 0;
1634             }
1635         }
1636
1637       if (val)
1638         {
1639           /* Can't set the breakpoint.  */
1640           if (solib_name_from_address (bl->pspace, bl->address))
1641             {
1642               /* See also: disable_breakpoints_in_shlibs.  */
1643               val = 0;
1644               bl->shlib_disabled = 1;
1645               if (!*disabled_breaks)
1646                 {
1647                   fprintf_unfiltered (tmp_error_stream, 
1648                                       "Cannot insert breakpoint %d.\n", 
1649                                       bl->owner->number);
1650                   fprintf_unfiltered (tmp_error_stream, 
1651                                       "Temporarily disabling shared "
1652                                       "library breakpoints:\n");
1653                 }
1654               *disabled_breaks = 1;
1655               fprintf_unfiltered (tmp_error_stream,
1656                                   "breakpoint #%d\n", bl->owner->number);
1657             }
1658           else
1659             {
1660               if (bl->loc_type == bp_loc_hardware_breakpoint)
1661                 {
1662                   *hw_breakpoint_error = 1;
1663                   fprintf_unfiltered (tmp_error_stream,
1664                                       "Cannot insert hardware "
1665                                       "breakpoint %d.\n",
1666                                       bl->owner->number);
1667                 }
1668               else
1669                 {
1670                   fprintf_unfiltered (tmp_error_stream, 
1671                                       "Cannot insert breakpoint %d.\n", 
1672                                       bl->owner->number);
1673                   fprintf_filtered (tmp_error_stream, 
1674                                     "Error accessing memory address ");
1675                   fputs_filtered (paddress (bl->gdbarch, bl->address),
1676                                   tmp_error_stream);
1677                   fprintf_filtered (tmp_error_stream, ": %s.\n",
1678                                     safe_strerror (val));
1679                 }
1680
1681             }
1682         }
1683       else
1684         bl->inserted = 1;
1685
1686       return val;
1687     }
1688
1689   else if (bl->loc_type == bp_loc_hardware_watchpoint
1690            /* NOTE drow/2003-09-08: This state only exists for removing
1691               watchpoints.  It's not clear that it's necessary...  */
1692            && bl->owner->disposition != disp_del_at_next_stop)
1693     {
1694       gdb_assert (bl->owner->ops != NULL
1695                   && bl->owner->ops->insert_location != NULL);
1696
1697       val = bl->owner->ops->insert_location (bl);
1698
1699       /* If trying to set a read-watchpoint, and it turns out it's not
1700          supported, try emulating one with an access watchpoint.  */
1701       if (val == 1 && bl->watchpoint_type == hw_read)
1702         {
1703           struct bp_location *loc, **loc_temp;
1704
1705           /* But don't try to insert it, if there's already another
1706              hw_access location that would be considered a duplicate
1707              of this one.  */
1708           ALL_BP_LOCATIONS (loc, loc_temp)
1709             if (loc != bl
1710                 && loc->watchpoint_type == hw_access
1711                 && watchpoint_locations_match (bl, loc))
1712               {
1713                 bl->duplicate = 1;
1714                 bl->inserted = 1;
1715                 bl->target_info = loc->target_info;
1716                 bl->watchpoint_type = hw_access;
1717                 val = 0;
1718                 break;
1719               }
1720
1721           if (val == 1)
1722             {
1723               bl->watchpoint_type = hw_access;
1724               val = bl->owner->ops->insert_location (bl);
1725
1726               if (val)
1727                 /* Back to the original value.  */
1728                 bl->watchpoint_type = hw_read;
1729             }
1730         }
1731
1732       bl->inserted = (val == 0);
1733     }
1734
1735   else if (bl->owner->type == bp_catchpoint)
1736     {
1737       gdb_assert (bl->owner->ops != NULL
1738                   && bl->owner->ops->insert_location != NULL);
1739
1740       val = bl->owner->ops->insert_location (bl);
1741       if (val)
1742         {
1743           bl->owner->enable_state = bp_disabled;
1744
1745           if (val == 1)
1746             warning (_("\
1747 Error inserting catchpoint %d: Your system does not support this type\n\
1748 of catchpoint."), bl->owner->number);
1749           else
1750             warning (_("Error inserting catchpoint %d."), bl->owner->number);
1751         }
1752
1753       bl->inserted = (val == 0);
1754
1755       /* We've already printed an error message if there was a problem
1756          inserting this catchpoint, and we've disabled the catchpoint,
1757          so just return success.  */
1758       return 0;
1759     }
1760
1761   return 0;
1762 }
1763
1764 /* This function is called when program space PSPACE is about to be
1765    deleted.  It takes care of updating breakpoints to not reference
1766    PSPACE anymore.  */
1767
1768 void
1769 breakpoint_program_space_exit (struct program_space *pspace)
1770 {
1771   struct breakpoint *b, *b_temp;
1772   struct bp_location *loc, **loc_temp;
1773
1774   /* Remove any breakpoint that was set through this program space.  */
1775   ALL_BREAKPOINTS_SAFE (b, b_temp)
1776     {
1777       if (b->pspace == pspace)
1778         delete_breakpoint (b);
1779     }
1780
1781   /* Breakpoints set through other program spaces could have locations
1782      bound to PSPACE as well.  Remove those.  */
1783   ALL_BP_LOCATIONS (loc, loc_temp)
1784     {
1785       struct bp_location *tmp;
1786
1787       if (loc->pspace == pspace)
1788         {
1789           /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL.  */
1790           if (loc->owner->loc == loc)
1791             loc->owner->loc = loc->next;
1792           else
1793             for (tmp = loc->owner->loc; tmp->next != NULL; tmp = tmp->next)
1794               if (tmp->next == loc)
1795                 {
1796                   tmp->next = loc->next;
1797                   break;
1798                 }
1799         }
1800     }
1801
1802   /* Now update the global location list to permanently delete the
1803      removed locations above.  */
1804   update_global_location_list (0);
1805 }
1806
1807 /* Make sure all breakpoints are inserted in inferior.
1808    Throws exception on any error.
1809    A breakpoint that is already inserted won't be inserted
1810    again, so calling this function twice is safe.  */
1811 void
1812 insert_breakpoints (void)
1813 {
1814   struct breakpoint *bpt;
1815
1816   ALL_BREAKPOINTS (bpt)
1817     if (is_hardware_watchpoint (bpt))
1818       update_watchpoint (bpt, 0 /* don't reparse.  */);
1819
1820   update_global_location_list (1);
1821
1822   /* update_global_location_list does not insert breakpoints when
1823      always_inserted_mode is not enabled.  Explicitly insert them
1824      now.  */
1825   if (!breakpoints_always_inserted_mode ())
1826     insert_breakpoint_locations ();
1827 }
1828
1829 /* insert_breakpoints is used when starting or continuing the program.
1830    remove_breakpoints is used when the program stops.
1831    Both return zero if successful,
1832    or an `errno' value if could not write the inferior.  */
1833
1834 static void
1835 insert_breakpoint_locations (void)
1836 {
1837   struct breakpoint *bpt;
1838   struct bp_location *bl, **blp_tmp;
1839   int error = 0;
1840   int val = 0;
1841   int disabled_breaks = 0;
1842   int hw_breakpoint_error = 0;
1843
1844   struct ui_file *tmp_error_stream = mem_fileopen ();
1845   struct cleanup *cleanups = make_cleanup_ui_file_delete (tmp_error_stream);
1846   
1847   /* Explicitly mark the warning -- this will only be printed if
1848      there was an error.  */
1849   fprintf_unfiltered (tmp_error_stream, "Warning:\n");
1850
1851   save_current_space_and_thread ();
1852
1853   ALL_BP_LOCATIONS (bl, blp_tmp)
1854     {
1855       if (!should_be_inserted (bl) || bl->inserted)
1856         continue;
1857
1858       /* There is no point inserting thread-specific breakpoints if
1859          the thread no longer exists.  ALL_BP_LOCATIONS bp_location
1860          has BL->OWNER always non-NULL.  */
1861       if (bl->owner->thread != -1
1862           && !valid_thread_id (bl->owner->thread))
1863         continue;
1864
1865       switch_to_program_space_and_thread (bl->pspace);
1866
1867       /* For targets that support global breakpoints, there's no need
1868          to select an inferior to insert breakpoint to.  In fact, even
1869          if we aren't attached to any process yet, we should still
1870          insert breakpoints.  */
1871       if (!gdbarch_has_global_breakpoints (target_gdbarch)
1872           && ptid_equal (inferior_ptid, null_ptid))
1873         continue;
1874
1875       val = insert_bp_location (bl, tmp_error_stream, &disabled_breaks,
1876                                     &hw_breakpoint_error);
1877       if (val)
1878         error = val;
1879     }
1880
1881   /* If we failed to insert all locations of a watchpoint, remove
1882      them, as half-inserted watchpoint is of limited use.  */
1883   ALL_BREAKPOINTS (bpt)  
1884     {
1885       int some_failed = 0;
1886       struct bp_location *loc;
1887
1888       if (!is_hardware_watchpoint (bpt))
1889         continue;
1890
1891       if (!breakpoint_enabled (bpt))
1892         continue;
1893
1894       if (bpt->disposition == disp_del_at_next_stop)
1895         continue;
1896       
1897       for (loc = bpt->loc; loc; loc = loc->next)
1898         if (!loc->inserted && should_be_inserted (loc))
1899           {
1900             some_failed = 1;
1901             break;
1902           }
1903       if (some_failed)
1904         {
1905           for (loc = bpt->loc; loc; loc = loc->next)
1906             if (loc->inserted)
1907               remove_breakpoint (loc, mark_uninserted);
1908
1909           hw_breakpoint_error = 1;
1910           fprintf_unfiltered (tmp_error_stream,
1911                               "Could not insert hardware watchpoint %d.\n", 
1912                               bpt->number);
1913           error = -1;
1914         }
1915     }
1916
1917   if (error)
1918     {
1919       /* If a hardware breakpoint or watchpoint was inserted, add a
1920          message about possibly exhausted resources.  */
1921       if (hw_breakpoint_error)
1922         {
1923           fprintf_unfiltered (tmp_error_stream, 
1924                               "Could not insert hardware breakpoints:\n\
1925 You may have requested too many hardware breakpoints/watchpoints.\n");
1926         }
1927       target_terminal_ours_for_output ();
1928       error_stream (tmp_error_stream);
1929     }
1930
1931   do_cleanups (cleanups);
1932 }
1933
1934 int
1935 remove_breakpoints (void)
1936 {
1937   struct bp_location *bl, **blp_tmp;
1938   int val = 0;
1939
1940   ALL_BP_LOCATIONS (bl, blp_tmp)
1941   {
1942     if (bl->inserted)
1943       val |= remove_breakpoint (bl, mark_uninserted);
1944   }
1945   return val;
1946 }
1947
1948 /* Remove breakpoints of process PID.  */
1949
1950 int
1951 remove_breakpoints_pid (int pid)
1952 {
1953   struct bp_location *bl, **blp_tmp;
1954   int val;
1955   struct inferior *inf = find_inferior_pid (pid);
1956
1957   ALL_BP_LOCATIONS (bl, blp_tmp)
1958   {
1959     if (bl->pspace != inf->pspace)
1960       continue;
1961
1962     if (bl->inserted)
1963       {
1964         val = remove_breakpoint (bl, mark_uninserted);
1965         if (val != 0)
1966           return val;
1967       }
1968   }
1969   return 0;
1970 }
1971
1972 int
1973 remove_hw_watchpoints (void)
1974 {
1975   struct bp_location *bl, **blp_tmp;
1976   int val = 0;
1977
1978   ALL_BP_LOCATIONS (bl, blp_tmp)
1979   {
1980     if (bl->inserted && bl->loc_type == bp_loc_hardware_watchpoint)
1981       val |= remove_breakpoint (bl, mark_uninserted);
1982   }
1983   return val;
1984 }
1985
1986 int
1987 reattach_breakpoints (int pid)
1988 {
1989   struct cleanup *old_chain;
1990   struct bp_location *bl, **blp_tmp;
1991   int val;
1992   struct ui_file *tmp_error_stream;
1993   int dummy1 = 0, dummy2 = 0;
1994   struct inferior *inf;
1995   struct thread_info *tp;
1996
1997   tp = any_live_thread_of_process (pid);
1998   if (tp == NULL)
1999     return 1;
2000
2001   inf = find_inferior_pid (pid);
2002   old_chain = save_inferior_ptid ();
2003
2004   inferior_ptid = tp->ptid;
2005
2006   tmp_error_stream = mem_fileopen ();
2007   make_cleanup_ui_file_delete (tmp_error_stream);
2008
2009   ALL_BP_LOCATIONS (bl, blp_tmp)
2010   {
2011     if (bl->pspace != inf->pspace)
2012       continue;
2013
2014     if (bl->inserted)
2015       {
2016         bl->inserted = 0;
2017         val = insert_bp_location (bl, tmp_error_stream, &dummy1, &dummy2);
2018         if (val != 0)
2019           {
2020             do_cleanups (old_chain);
2021             return val;
2022           }
2023       }
2024   }
2025   do_cleanups (old_chain);
2026   return 0;
2027 }
2028
2029 static int internal_breakpoint_number = -1;
2030
2031 /* Set the breakpoint number of B, depending on the value of INTERNAL.
2032    If INTERNAL is non-zero, the breakpoint number will be populated
2033    from internal_breakpoint_number and that variable decremented.
2034    Otherwis the breakpoint number will be populated from
2035    breakpoint_count and that value incremented.  Internal breakpoints
2036    do not set the internal var bpnum.  */
2037 static void
2038 set_breakpoint_number (int internal, struct breakpoint *b)
2039 {
2040   if (internal)
2041     b->number = internal_breakpoint_number--;
2042   else
2043     {
2044       set_breakpoint_count (breakpoint_count + 1);
2045       b->number = breakpoint_count;
2046     }
2047 }
2048
2049 static struct breakpoint *
2050 create_internal_breakpoint (struct gdbarch *gdbarch,
2051                             CORE_ADDR address, enum bptype type)
2052 {
2053   struct symtab_and_line sal;
2054   struct breakpoint *b;
2055
2056   init_sal (&sal);              /* Initialize to zeroes.  */
2057
2058   sal.pc = address;
2059   sal.section = find_pc_overlay (sal.pc);
2060   sal.pspace = current_program_space;
2061
2062   b = set_raw_breakpoint (gdbarch, sal, type);
2063   b->number = internal_breakpoint_number--;
2064   b->disposition = disp_donttouch;
2065
2066   return b;
2067 }
2068
2069 static const char *const longjmp_names[] =
2070   {
2071     "longjmp", "_longjmp", "siglongjmp", "_siglongjmp"
2072   };
2073 #define NUM_LONGJMP_NAMES ARRAY_SIZE(longjmp_names)
2074
2075 /* Per-objfile data private to breakpoint.c.  */
2076 struct breakpoint_objfile_data
2077 {
2078   /* Minimal symbol for "_ovly_debug_event" (if any).  */
2079   struct minimal_symbol *overlay_msym;
2080
2081   /* Minimal symbol(s) for "longjmp", "siglongjmp", etc. (if any).  */
2082   struct minimal_symbol *longjmp_msym[NUM_LONGJMP_NAMES];
2083
2084   /* Minimal symbol for "std::terminate()" (if any).  */
2085   struct minimal_symbol *terminate_msym;
2086
2087   /* Minimal symbol for "_Unwind_DebugHook" (if any).  */
2088   struct minimal_symbol *exception_msym;
2089 };
2090
2091 static const struct objfile_data *breakpoint_objfile_key;
2092
2093 /* Minimal symbol not found sentinel.  */
2094 static struct minimal_symbol msym_not_found;
2095
2096 /* Returns TRUE if MSYM point to the "not found" sentinel.  */
2097
2098 static int
2099 msym_not_found_p (const struct minimal_symbol *msym)
2100 {
2101   return msym == &msym_not_found;
2102 }
2103
2104 /* Return per-objfile data needed by breakpoint.c.
2105    Allocate the data if necessary.  */
2106
2107 static struct breakpoint_objfile_data *
2108 get_breakpoint_objfile_data (struct objfile *objfile)
2109 {
2110   struct breakpoint_objfile_data *bp_objfile_data;
2111
2112   bp_objfile_data = objfile_data (objfile, breakpoint_objfile_key);
2113   if (bp_objfile_data == NULL)
2114     {
2115       bp_objfile_data = obstack_alloc (&objfile->objfile_obstack,
2116                                        sizeof (*bp_objfile_data));
2117
2118       memset (bp_objfile_data, 0, sizeof (*bp_objfile_data));
2119       set_objfile_data (objfile, breakpoint_objfile_key, bp_objfile_data);
2120     }
2121   return bp_objfile_data;
2122 }
2123
2124 static void
2125 create_overlay_event_breakpoint (void)
2126 {
2127   struct objfile *objfile;
2128   const char *const func_name = "_ovly_debug_event";
2129
2130   ALL_OBJFILES (objfile)
2131     {
2132       struct breakpoint *b;
2133       struct breakpoint_objfile_data *bp_objfile_data;
2134       CORE_ADDR addr;
2135
2136       bp_objfile_data = get_breakpoint_objfile_data (objfile);
2137
2138       if (msym_not_found_p (bp_objfile_data->overlay_msym))
2139         continue;
2140
2141       if (bp_objfile_data->overlay_msym == NULL)
2142         {
2143           struct minimal_symbol *m;
2144
2145           m = lookup_minimal_symbol_text (func_name, objfile);
2146           if (m == NULL)
2147             {
2148               /* Avoid future lookups in this objfile.  */
2149               bp_objfile_data->overlay_msym = &msym_not_found;
2150               continue;
2151             }
2152           bp_objfile_data->overlay_msym = m;
2153         }
2154
2155       addr = SYMBOL_VALUE_ADDRESS (bp_objfile_data->overlay_msym);
2156       b = create_internal_breakpoint (get_objfile_arch (objfile), addr,
2157                                       bp_overlay_event);
2158       b->addr_string = xstrdup (func_name);
2159
2160       if (overlay_debugging == ovly_auto)
2161         {
2162           b->enable_state = bp_enabled;
2163           overlay_events_enabled = 1;
2164         }
2165       else
2166        {
2167          b->enable_state = bp_disabled;
2168          overlay_events_enabled = 0;
2169        }
2170     }
2171   update_global_location_list (1);
2172 }
2173
2174 static void
2175 create_longjmp_master_breakpoint (void)
2176 {
2177   struct program_space *pspace;
2178   struct cleanup *old_chain;
2179
2180   old_chain = save_current_program_space ();
2181
2182   ALL_PSPACES (pspace)
2183   {
2184     struct objfile *objfile;
2185
2186     set_current_program_space (pspace);
2187
2188     ALL_OBJFILES (objfile)
2189     {
2190       int i;
2191       struct gdbarch *gdbarch;
2192       struct breakpoint_objfile_data *bp_objfile_data;
2193
2194       gdbarch = get_objfile_arch (objfile);
2195       if (!gdbarch_get_longjmp_target_p (gdbarch))
2196         continue;
2197
2198       bp_objfile_data = get_breakpoint_objfile_data (objfile);
2199
2200       for (i = 0; i < NUM_LONGJMP_NAMES; i++)
2201         {
2202           struct breakpoint *b;
2203           const char *func_name;
2204           CORE_ADDR addr;
2205
2206           if (msym_not_found_p (bp_objfile_data->longjmp_msym[i]))
2207             continue;
2208
2209           func_name = longjmp_names[i];
2210           if (bp_objfile_data->longjmp_msym[i] == NULL)
2211             {
2212               struct minimal_symbol *m;
2213
2214               m = lookup_minimal_symbol_text (func_name, objfile);
2215               if (m == NULL)
2216                 {
2217                   /* Prevent future lookups in this objfile.  */
2218                   bp_objfile_data->longjmp_msym[i] = &msym_not_found;
2219                   continue;
2220                 }
2221               bp_objfile_data->longjmp_msym[i] = m;
2222             }
2223
2224           addr = SYMBOL_VALUE_ADDRESS (bp_objfile_data->longjmp_msym[i]);
2225           b = create_internal_breakpoint (gdbarch, addr, bp_longjmp_master);
2226           b->addr_string = xstrdup (func_name);
2227           b->enable_state = bp_disabled;
2228         }
2229     }
2230   }
2231   update_global_location_list (1);
2232
2233   do_cleanups (old_chain);
2234 }
2235
2236 /* Create a master std::terminate breakpoint.  */
2237 static void
2238 create_std_terminate_master_breakpoint (void)
2239 {
2240   struct program_space *pspace;
2241   struct cleanup *old_chain;
2242   const char *const func_name = "std::terminate()";
2243
2244   old_chain = save_current_program_space ();
2245
2246   ALL_PSPACES (pspace)
2247   {
2248     struct objfile *objfile;
2249     CORE_ADDR addr;
2250
2251     set_current_program_space (pspace);
2252
2253     ALL_OBJFILES (objfile)
2254     {
2255       struct breakpoint *b;
2256       struct breakpoint_objfile_data *bp_objfile_data;
2257
2258       bp_objfile_data = get_breakpoint_objfile_data (objfile);
2259
2260       if (msym_not_found_p (bp_objfile_data->terminate_msym))
2261         continue;
2262
2263       if (bp_objfile_data->terminate_msym == NULL)
2264         {
2265           struct minimal_symbol *m;
2266
2267           m = lookup_minimal_symbol (func_name, NULL, objfile);
2268           if (m == NULL || (MSYMBOL_TYPE (m) != mst_text
2269                             && MSYMBOL_TYPE (m) != mst_file_text))
2270             {
2271               /* Prevent future lookups in this objfile.  */
2272               bp_objfile_data->terminate_msym = &msym_not_found;
2273               continue;
2274             }
2275           bp_objfile_data->terminate_msym = m;
2276         }
2277
2278       addr = SYMBOL_VALUE_ADDRESS (bp_objfile_data->terminate_msym);
2279       b = create_internal_breakpoint (get_objfile_arch (objfile), addr,
2280                                       bp_std_terminate_master);
2281       b->addr_string = xstrdup (func_name);
2282       b->enable_state = bp_disabled;
2283     }
2284   }
2285
2286   update_global_location_list (1);
2287
2288   do_cleanups (old_chain);
2289 }
2290
2291 /* Install a master breakpoint on the unwinder's debug hook.  */
2292
2293 void
2294 create_exception_master_breakpoint (void)
2295 {
2296   struct objfile *objfile;
2297   const char *const func_name = "_Unwind_DebugHook";
2298
2299   ALL_OBJFILES (objfile)
2300     {
2301       struct breakpoint *b;
2302       struct gdbarch *gdbarch;
2303       struct breakpoint_objfile_data *bp_objfile_data;
2304       CORE_ADDR addr;
2305
2306       bp_objfile_data = get_breakpoint_objfile_data (objfile);
2307
2308       if (msym_not_found_p (bp_objfile_data->exception_msym))
2309         continue;
2310
2311       gdbarch = get_objfile_arch (objfile);
2312
2313       if (bp_objfile_data->exception_msym == NULL)
2314         {
2315           struct minimal_symbol *debug_hook;
2316
2317           debug_hook = lookup_minimal_symbol (func_name, NULL, objfile);
2318           if (debug_hook == NULL)
2319             {
2320               bp_objfile_data->exception_msym = &msym_not_found;
2321               continue;
2322             }
2323
2324           bp_objfile_data->exception_msym = debug_hook;
2325         }
2326
2327       addr = SYMBOL_VALUE_ADDRESS (bp_objfile_data->exception_msym);
2328       addr = gdbarch_convert_from_func_ptr_addr (gdbarch, addr,
2329                                                  &current_target);
2330       b = create_internal_breakpoint (gdbarch, addr, bp_exception_master);
2331       b->addr_string = xstrdup (func_name);
2332       b->enable_state = bp_disabled;
2333     }
2334
2335   update_global_location_list (1);
2336 }
2337
2338 void
2339 update_breakpoints_after_exec (void)
2340 {
2341   struct breakpoint *b, *b_tmp;
2342   struct bp_location *bploc, **bplocp_tmp;
2343
2344   /* We're about to delete breakpoints from GDB's lists.  If the
2345      INSERTED flag is true, GDB will try to lift the breakpoints by
2346      writing the breakpoints' "shadow contents" back into memory.  The
2347      "shadow contents" are NOT valid after an exec, so GDB should not
2348      do that.  Instead, the target is responsible from marking
2349      breakpoints out as soon as it detects an exec.  We don't do that
2350      here instead, because there may be other attempts to delete
2351      breakpoints after detecting an exec and before reaching here.  */
2352   ALL_BP_LOCATIONS (bploc, bplocp_tmp)
2353     if (bploc->pspace == current_program_space)
2354       gdb_assert (!bploc->inserted);
2355
2356   ALL_BREAKPOINTS_SAFE (b, b_tmp)
2357   {
2358     if (b->pspace != current_program_space)
2359       continue;
2360
2361     /* Solib breakpoints must be explicitly reset after an exec().  */
2362     if (b->type == bp_shlib_event)
2363       {
2364         delete_breakpoint (b);
2365         continue;
2366       }
2367
2368     /* JIT breakpoints must be explicitly reset after an exec().  */
2369     if (b->type == bp_jit_event)
2370       {
2371         delete_breakpoint (b);
2372         continue;
2373       }
2374
2375     /* Thread event breakpoints must be set anew after an exec(),
2376        as must overlay event and longjmp master breakpoints.  */
2377     if (b->type == bp_thread_event || b->type == bp_overlay_event
2378         || b->type == bp_longjmp_master || b->type == bp_std_terminate_master
2379         || b->type == bp_exception_master)
2380       {
2381         delete_breakpoint (b);
2382         continue;
2383       }
2384
2385     /* Step-resume breakpoints are meaningless after an exec().  */
2386     if (b->type == bp_step_resume)
2387       {
2388         delete_breakpoint (b);
2389         continue;
2390       }
2391
2392     /* Longjmp and longjmp-resume breakpoints are also meaningless
2393        after an exec.  */
2394     if (b->type == bp_longjmp || b->type == bp_longjmp_resume
2395         || b->type == bp_exception || b->type == bp_exception_resume)
2396       {
2397         delete_breakpoint (b);
2398         continue;
2399       }
2400
2401     if (b->type == bp_catchpoint)
2402       {
2403         /* For now, none of the bp_catchpoint breakpoints need to
2404            do anything at this point.  In the future, if some of
2405            the catchpoints need to something, we will need to add
2406            a new method, and call this method from here.  */
2407         continue;
2408       }
2409
2410     /* bp_finish is a special case.  The only way we ought to be able
2411        to see one of these when an exec() has happened, is if the user
2412        caught a vfork, and then said "finish".  Ordinarily a finish just
2413        carries them to the call-site of the current callee, by setting
2414        a temporary bp there and resuming.  But in this case, the finish
2415        will carry them entirely through the vfork & exec.
2416
2417        We don't want to allow a bp_finish to remain inserted now.  But
2418        we can't safely delete it, 'cause finish_command has a handle to
2419        the bp on a bpstat, and will later want to delete it.  There's a
2420        chance (and I've seen it happen) that if we delete the bp_finish
2421        here, that its storage will get reused by the time finish_command
2422        gets 'round to deleting the "use to be a bp_finish" breakpoint.
2423        We really must allow finish_command to delete a bp_finish.
2424
2425        In the absense of a general solution for the "how do we know
2426        it's safe to delete something others may have handles to?"
2427        problem, what we'll do here is just uninsert the bp_finish, and
2428        let finish_command delete it.
2429
2430        (We know the bp_finish is "doomed" in the sense that it's
2431        momentary, and will be deleted as soon as finish_command sees
2432        the inferior stopped.  So it doesn't matter that the bp's
2433        address is probably bogus in the new a.out, unlike e.g., the
2434        solib breakpoints.)  */
2435
2436     if (b->type == bp_finish)
2437       {
2438         continue;
2439       }
2440
2441     /* Without a symbolic address, we have little hope of the
2442        pre-exec() address meaning the same thing in the post-exec()
2443        a.out.  */
2444     if (b->addr_string == NULL)
2445       {
2446         delete_breakpoint (b);
2447         continue;
2448       }
2449   }
2450   /* FIXME what about longjmp breakpoints?  Re-create them here?  */
2451   create_overlay_event_breakpoint ();
2452   create_longjmp_master_breakpoint ();
2453   create_std_terminate_master_breakpoint ();
2454   create_exception_master_breakpoint ();
2455 }
2456
2457 int
2458 detach_breakpoints (int pid)
2459 {
2460   struct bp_location *bl, **blp_tmp;
2461   int val = 0;
2462   struct cleanup *old_chain = save_inferior_ptid ();
2463   struct inferior *inf = current_inferior ();
2464
2465   if (pid == PIDGET (inferior_ptid))
2466     error (_("Cannot detach breakpoints of inferior_ptid"));
2467
2468   /* Set inferior_ptid; remove_breakpoint_1 uses this global.  */
2469   inferior_ptid = pid_to_ptid (pid);
2470   ALL_BP_LOCATIONS (bl, blp_tmp)
2471   {
2472     if (bl->pspace != inf->pspace)
2473       continue;
2474
2475     if (bl->inserted)
2476       val |= remove_breakpoint_1 (bl, mark_inserted);
2477   }
2478
2479   /* Detach single-step breakpoints as well.  */
2480   detach_single_step_breakpoints ();
2481
2482   do_cleanups (old_chain);
2483   return val;
2484 }
2485
2486 /* Remove the breakpoint location BL from the current address space.
2487    Note that this is used to detach breakpoints from a child fork.
2488    When we get here, the child isn't in the inferior list, and neither
2489    do we have objects to represent its address space --- we should
2490    *not* look at bl->pspace->aspace here.  */
2491
2492 static int
2493 remove_breakpoint_1 (struct bp_location *bl, insertion_state_t is)
2494 {
2495   int val;
2496
2497   /* BL is never in moribund_locations by our callers.  */
2498   gdb_assert (bl->owner != NULL);
2499
2500   if (bl->owner->enable_state == bp_permanent)
2501     /* Permanent breakpoints cannot be inserted or removed.  */
2502     return 0;
2503
2504   /* The type of none suggests that owner is actually deleted.
2505      This should not ever happen.  */
2506   gdb_assert (bl->owner->type != bp_none);
2507
2508   if (bl->loc_type == bp_loc_software_breakpoint
2509       || bl->loc_type == bp_loc_hardware_breakpoint)
2510     {
2511       /* "Normal" instruction breakpoint: either the standard
2512          trap-instruction bp (bp_breakpoint), or a
2513          bp_hardware_breakpoint.  */
2514
2515       /* First check to see if we have to handle an overlay.  */
2516       if (overlay_debugging == ovly_off
2517           || bl->section == NULL
2518           || !(section_is_overlay (bl->section)))
2519         {
2520           /* No overlay handling: just remove the breakpoint.  */
2521
2522           if (bl->loc_type == bp_loc_hardware_breakpoint)
2523             val = target_remove_hw_breakpoint (bl->gdbarch, &bl->target_info);
2524           else
2525             val = target_remove_breakpoint (bl->gdbarch, &bl->target_info);
2526         }
2527       else
2528         {
2529           /* This breakpoint is in an overlay section.
2530              Did we set a breakpoint at the LMA?  */
2531           if (!overlay_events_enabled)
2532               {
2533                 /* Yes -- overlay event support is not active, so we
2534                    should have set a breakpoint at the LMA.  Remove it.  
2535                 */
2536                 /* Ignore any failures: if the LMA is in ROM, we will
2537                    have already warned when we failed to insert it.  */
2538                 if (bl->loc_type == bp_loc_hardware_breakpoint)
2539                   target_remove_hw_breakpoint (bl->gdbarch,
2540                                                &bl->overlay_target_info);
2541                 else
2542                   target_remove_breakpoint (bl->gdbarch,
2543                                             &bl->overlay_target_info);
2544               }
2545           /* Did we set a breakpoint at the VMA? 
2546              If so, we will have marked the breakpoint 'inserted'.  */
2547           if (bl->inserted)
2548             {
2549               /* Yes -- remove it.  Previously we did not bother to
2550                  remove the breakpoint if the section had been
2551                  unmapped, but let's not rely on that being safe.  We
2552                  don't know what the overlay manager might do.  */
2553               if (bl->loc_type == bp_loc_hardware_breakpoint)
2554                 val = target_remove_hw_breakpoint (bl->gdbarch,
2555                                                    &bl->target_info);
2556
2557               /* However, we should remove *software* breakpoints only
2558                  if the section is still mapped, or else we overwrite
2559                  wrong code with the saved shadow contents.  */
2560               else if (section_is_mapped (bl->section))
2561                 val = target_remove_breakpoint (bl->gdbarch,
2562                                                 &bl->target_info);
2563               else
2564                 val = 0;
2565             }
2566           else
2567             {
2568               /* No -- not inserted, so no need to remove.  No error.  */
2569               val = 0;
2570             }
2571         }
2572
2573       /* In some cases, we might not be able to remove a breakpoint
2574          in a shared library that has already been removed, but we
2575          have not yet processed the shlib unload event.  */
2576       if (val && solib_name_from_address (bl->pspace, bl->address))
2577         val = 0;
2578
2579       if (val)
2580         return val;
2581       bl->inserted = (is == mark_inserted);
2582     }
2583   else if (bl->loc_type == bp_loc_hardware_watchpoint)
2584     {
2585       gdb_assert (bl->owner->ops != NULL
2586                   && bl->owner->ops->remove_location != NULL);
2587
2588       bl->inserted = (is == mark_inserted);
2589       bl->owner->ops->remove_location (bl);
2590
2591       /* Failure to remove any of the hardware watchpoints comes here.  */
2592       if ((is == mark_uninserted) && (bl->inserted))
2593         warning (_("Could not remove hardware watchpoint %d."),
2594                  bl->owner->number);
2595     }
2596   else if (bl->owner->type == bp_catchpoint
2597            && breakpoint_enabled (bl->owner)
2598            && !bl->duplicate)
2599     {
2600       gdb_assert (bl->owner->ops != NULL
2601                   && bl->owner->ops->remove_location != NULL);
2602
2603       val = bl->owner->ops->remove_location (bl);
2604       if (val)
2605         return val;
2606
2607       bl->inserted = (is == mark_inserted);
2608     }
2609
2610   return 0;
2611 }
2612
2613 static int
2614 remove_breakpoint (struct bp_location *bl, insertion_state_t is)
2615 {
2616   int ret;
2617   struct cleanup *old_chain;
2618
2619   /* BL is never in moribund_locations by our callers.  */
2620   gdb_assert (bl->owner != NULL);
2621
2622   if (bl->owner->enable_state == bp_permanent)
2623     /* Permanent breakpoints cannot be inserted or removed.  */
2624     return 0;
2625
2626   /* The type of none suggests that owner is actually deleted.
2627      This should not ever happen.  */
2628   gdb_assert (bl->owner->type != bp_none);
2629
2630   old_chain = save_current_space_and_thread ();
2631
2632   switch_to_program_space_and_thread (bl->pspace);
2633
2634   ret = remove_breakpoint_1 (bl, is);
2635
2636   do_cleanups (old_chain);
2637   return ret;
2638 }
2639
2640 /* Clear the "inserted" flag in all breakpoints.  */
2641
2642 void
2643 mark_breakpoints_out (void)
2644 {
2645   struct bp_location *bl, **blp_tmp;
2646
2647   ALL_BP_LOCATIONS (bl, blp_tmp)
2648     if (bl->pspace == current_program_space)
2649       bl->inserted = 0;
2650 }
2651
2652 /* Clear the "inserted" flag in all breakpoints and delete any
2653    breakpoints which should go away between runs of the program.
2654
2655    Plus other such housekeeping that has to be done for breakpoints
2656    between runs.
2657
2658    Note: this function gets called at the end of a run (by
2659    generic_mourn_inferior) and when a run begins (by
2660    init_wait_for_inferior).  */
2661
2662
2663
2664 void
2665 breakpoint_init_inferior (enum inf_context context)
2666 {
2667   struct breakpoint *b, *b_tmp;
2668   struct bp_location *bl, **blp_tmp;
2669   int ix;
2670   struct program_space *pspace = current_program_space;
2671
2672   /* If breakpoint locations are shared across processes, then there's
2673      nothing to do.  */
2674   if (gdbarch_has_global_breakpoints (target_gdbarch))
2675     return;
2676
2677   ALL_BP_LOCATIONS (bl, blp_tmp)
2678   {
2679     /* ALL_BP_LOCATIONS bp_location has BL->OWNER always non-NULL.  */
2680     if (bl->pspace == pspace
2681         && bl->owner->enable_state != bp_permanent)
2682       bl->inserted = 0;
2683   }
2684
2685   ALL_BREAKPOINTS_SAFE (b, b_tmp)
2686   {
2687     if (b->loc && b->loc->pspace != pspace)
2688       continue;
2689
2690     switch (b->type)
2691       {
2692       case bp_call_dummy:
2693
2694         /* If the call dummy breakpoint is at the entry point it will
2695            cause problems when the inferior is rerun, so we better get
2696            rid of it.  */
2697
2698       case bp_watchpoint_scope:
2699
2700         /* Also get rid of scope breakpoints.  */
2701
2702       case bp_shlib_event:
2703
2704         /* Also remove solib event breakpoints.  Their addresses may
2705            have changed since the last time we ran the program.
2706            Actually we may now be debugging against different target;
2707            and so the solib backend that installed this breakpoint may
2708            not be used in by the target.  E.g.,
2709
2710            (gdb) file prog-linux
2711            (gdb) run               # native linux target
2712            ...
2713            (gdb) kill
2714            (gdb) file prog-win.exe
2715            (gdb) tar rem :9999     # remote Windows gdbserver.
2716         */
2717
2718         delete_breakpoint (b);
2719         break;
2720
2721       case bp_watchpoint:
2722       case bp_hardware_watchpoint:
2723       case bp_read_watchpoint:
2724       case bp_access_watchpoint:
2725
2726         /* Likewise for watchpoints on local expressions.  */
2727         if (b->exp_valid_block != NULL)
2728           delete_breakpoint (b);
2729         else if (context == inf_starting) 
2730           {
2731             /* Reset val field to force reread of starting value in
2732                insert_breakpoints.  */
2733             if (b->val)
2734               value_free (b->val);
2735             b->val = NULL;
2736             b->val_valid = 0;
2737           }
2738         break;
2739       default:
2740         break;
2741       }
2742   }
2743
2744   /* Get rid of the moribund locations.  */
2745   for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, bl); ++ix)
2746     decref_bp_location (&bl);
2747   VEC_free (bp_location_p, moribund_locations);
2748 }
2749
2750 /* These functions concern about actual breakpoints inserted in the
2751    target --- to e.g. check if we need to do decr_pc adjustment or if
2752    we need to hop over the bkpt --- so we check for address space
2753    match, not program space.  */
2754
2755 /* breakpoint_here_p (PC) returns non-zero if an enabled breakpoint
2756    exists at PC.  It returns ordinary_breakpoint_here if it's an
2757    ordinary breakpoint, or permanent_breakpoint_here if it's a
2758    permanent breakpoint.
2759    - When continuing from a location with an ordinary breakpoint, we
2760      actually single step once before calling insert_breakpoints.
2761    - When continuing from a localion with a permanent breakpoint, we
2762      need to use the `SKIP_PERMANENT_BREAKPOINT' macro, provided by
2763      the target, to advance the PC past the breakpoint.  */
2764
2765 enum breakpoint_here
2766 breakpoint_here_p (struct address_space *aspace, CORE_ADDR pc)
2767 {
2768   struct bp_location *bl, **blp_tmp;
2769   int any_breakpoint_here = 0;
2770
2771   ALL_BP_LOCATIONS (bl, blp_tmp)
2772     {
2773       if (bl->loc_type != bp_loc_software_breakpoint
2774           && bl->loc_type != bp_loc_hardware_breakpoint)
2775         continue;
2776
2777       /* ALL_BP_LOCATIONS bp_location has bl->OWNER always non-NULL.  */
2778       if ((breakpoint_enabled (bl->owner)
2779            || bl->owner->enable_state == bp_permanent)
2780           && breakpoint_address_match (bl->pspace->aspace, bl->address,
2781                                        aspace, pc))
2782         {
2783           if (overlay_debugging 
2784               && section_is_overlay (bl->section)
2785               && !section_is_mapped (bl->section))
2786             continue;           /* unmapped overlay -- can't be a match */
2787           else if (bl->owner->enable_state == bp_permanent)
2788             return permanent_breakpoint_here;
2789           else
2790             any_breakpoint_here = 1;
2791         }
2792     }
2793
2794   return any_breakpoint_here ? ordinary_breakpoint_here : 0;
2795 }
2796
2797 /* Return true if there's a moribund breakpoint at PC.  */
2798
2799 int
2800 moribund_breakpoint_here_p (struct address_space *aspace, CORE_ADDR pc)
2801 {
2802   struct bp_location *loc;
2803   int ix;
2804
2805   for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, loc); ++ix)
2806     if (breakpoint_address_match (loc->pspace->aspace, loc->address,
2807                                   aspace,  pc))
2808       return 1;
2809
2810   return 0;
2811 }
2812
2813 /* Returns non-zero if there's a breakpoint inserted at PC, which is
2814    inserted using regular breakpoint_chain / bp_location array
2815    mechanism.  This does not check for single-step breakpoints, which
2816    are inserted and removed using direct target manipulation.  */
2817
2818 int
2819 regular_breakpoint_inserted_here_p (struct address_space *aspace, 
2820                                     CORE_ADDR pc)
2821 {
2822   struct bp_location *bl, **blp_tmp;
2823
2824   ALL_BP_LOCATIONS (bl, blp_tmp)
2825     {
2826       if (bl->loc_type != bp_loc_software_breakpoint
2827           && bl->loc_type != bp_loc_hardware_breakpoint)
2828         continue;
2829
2830       if (bl->inserted
2831           && breakpoint_address_match (bl->pspace->aspace, bl->address,
2832                                        aspace, pc))
2833         {
2834           if (overlay_debugging 
2835               && section_is_overlay (bl->section)
2836               && !section_is_mapped (bl->section))
2837             continue;           /* unmapped overlay -- can't be a match */
2838           else
2839             return 1;
2840         }
2841     }
2842   return 0;
2843 }
2844
2845 /* Returns non-zero iff there's either regular breakpoint
2846    or a single step breakpoint inserted at PC.  */
2847
2848 int
2849 breakpoint_inserted_here_p (struct address_space *aspace, CORE_ADDR pc)
2850 {
2851   if (regular_breakpoint_inserted_here_p (aspace, pc))
2852     return 1;
2853
2854   if (single_step_breakpoint_inserted_here_p (aspace, pc))
2855     return 1;
2856
2857   return 0;
2858 }
2859
2860 /* This function returns non-zero iff there is a software breakpoint
2861    inserted at PC.  */
2862
2863 int
2864 software_breakpoint_inserted_here_p (struct address_space *aspace,
2865                                      CORE_ADDR pc)
2866 {
2867   struct bp_location *bl, **blp_tmp;
2868
2869   ALL_BP_LOCATIONS (bl, blp_tmp)
2870     {
2871       if (bl->loc_type != bp_loc_software_breakpoint)
2872         continue;
2873
2874       if (bl->inserted
2875           && breakpoint_address_match (bl->pspace->aspace, bl->address,
2876                                        aspace, pc))
2877         {
2878           if (overlay_debugging 
2879               && section_is_overlay (bl->section)
2880               && !section_is_mapped (bl->section))
2881             continue;           /* unmapped overlay -- can't be a match */
2882           else
2883             return 1;
2884         }
2885     }
2886
2887   /* Also check for software single-step breakpoints.  */
2888   if (single_step_breakpoint_inserted_here_p (aspace, pc))
2889     return 1;
2890
2891   return 0;
2892 }
2893
2894 int
2895 hardware_watchpoint_inserted_in_range (struct address_space *aspace,
2896                                        CORE_ADDR addr, ULONGEST len)
2897 {
2898   struct breakpoint *bpt;
2899
2900   ALL_BREAKPOINTS (bpt)
2901     {
2902       struct bp_location *loc;
2903
2904       if (bpt->type != bp_hardware_watchpoint
2905           && bpt->type != bp_access_watchpoint)
2906         continue;
2907
2908       if (!breakpoint_enabled (bpt))
2909         continue;
2910
2911       for (loc = bpt->loc; loc; loc = loc->next)
2912         if (loc->pspace->aspace == aspace && loc->inserted)
2913           {
2914             CORE_ADDR l, h;
2915
2916             /* Check for intersection.  */
2917             l = max (loc->address, addr);
2918             h = min (loc->address + loc->length, addr + len);
2919             if (l < h)
2920               return 1;
2921           }
2922     }
2923   return 0;
2924 }
2925
2926 /* breakpoint_thread_match (PC, PTID) returns true if the breakpoint at
2927    PC is valid for process/thread PTID.  */
2928
2929 int
2930 breakpoint_thread_match (struct address_space *aspace, CORE_ADDR pc,
2931                          ptid_t ptid)
2932 {
2933   struct bp_location *bl, **blp_tmp;
2934   /* The thread and task IDs associated to PTID, computed lazily.  */
2935   int thread = -1;
2936   int task = 0;
2937   
2938   ALL_BP_LOCATIONS (bl, blp_tmp)
2939     {
2940       if (bl->loc_type != bp_loc_software_breakpoint
2941           && bl->loc_type != bp_loc_hardware_breakpoint)
2942         continue;
2943
2944       /* ALL_BP_LOCATIONS bp_location has bl->OWNER always non-NULL.  */
2945       if (!breakpoint_enabled (bl->owner)
2946           && bl->owner->enable_state != bp_permanent)
2947         continue;
2948
2949       if (!breakpoint_address_match (bl->pspace->aspace, bl->address,
2950                                      aspace, pc))
2951         continue;
2952
2953       if (bl->owner->thread != -1)
2954         {
2955           /* This is a thread-specific breakpoint.  Check that ptid
2956              matches that thread.  If thread hasn't been computed yet,
2957              it is now time to do so.  */
2958           if (thread == -1)
2959             thread = pid_to_thread_id (ptid);
2960           if (bl->owner->thread != thread)
2961             continue;
2962         }
2963
2964       if (bl->owner->task != 0)
2965         {
2966           /* This is a task-specific breakpoint.  Check that ptid
2967              matches that task.  If task hasn't been computed yet,
2968              it is now time to do so.  */
2969           if (task == 0)
2970             task = ada_get_task_number (ptid);
2971           if (bl->owner->task != task)
2972             continue;
2973         }
2974
2975       if (overlay_debugging 
2976           && section_is_overlay (bl->section)
2977           && !section_is_mapped (bl->section))
2978         continue;           /* unmapped overlay -- can't be a match */
2979
2980       return 1;
2981     }
2982
2983   return 0;
2984 }
2985 \f
2986
2987 /* bpstat stuff.  External routines' interfaces are documented
2988    in breakpoint.h.  */
2989
2990 int
2991 ep_is_catchpoint (struct breakpoint *ep)
2992 {
2993   return (ep->type == bp_catchpoint);
2994 }
2995
2996 /* Frees any storage that is part of a bpstat.  Does not walk the
2997    'next' chain.  */
2998
2999 static void
3000 bpstat_free (bpstat bs)
3001 {
3002   if (bs->old_val != NULL)
3003     value_free (bs->old_val);
3004   decref_counted_command_line (&bs->commands);
3005   decref_bp_location (&bs->bp_location_at);
3006   xfree (bs);
3007 }
3008
3009 /* Clear a bpstat so that it says we are not at any breakpoint.
3010    Also free any storage that is part of a bpstat.  */
3011
3012 void
3013 bpstat_clear (bpstat *bsp)
3014 {
3015   bpstat p;
3016   bpstat q;
3017
3018   if (bsp == 0)
3019     return;
3020   p = *bsp;
3021   while (p != NULL)
3022     {
3023       q = p->next;
3024       bpstat_free (p);
3025       p = q;
3026     }
3027   *bsp = NULL;
3028 }
3029
3030 /* Return a copy of a bpstat.  Like "bs1 = bs2" but all storage that
3031    is part of the bpstat is copied as well.  */
3032
3033 bpstat
3034 bpstat_copy (bpstat bs)
3035 {
3036   bpstat p = NULL;
3037   bpstat tmp;
3038   bpstat retval = NULL;
3039
3040   if (bs == NULL)
3041     return bs;
3042
3043   for (; bs != NULL; bs = bs->next)
3044     {
3045       tmp = (bpstat) xmalloc (sizeof (*tmp));
3046       memcpy (tmp, bs, sizeof (*tmp));
3047       incref_counted_command_line (tmp->commands);
3048       incref_bp_location (tmp->bp_location_at);
3049       if (bs->old_val != NULL)
3050         {
3051           tmp->old_val = value_copy (bs->old_val);
3052           release_value (tmp->old_val);
3053         }
3054
3055       if (p == NULL)
3056         /* This is the first thing in the chain.  */
3057         retval = tmp;
3058       else
3059         p->next = tmp;
3060       p = tmp;
3061     }
3062   p->next = NULL;
3063   return retval;
3064 }
3065
3066 /* Find the bpstat associated with this breakpoint.  */
3067
3068 bpstat
3069 bpstat_find_breakpoint (bpstat bsp, struct breakpoint *breakpoint)
3070 {
3071   if (bsp == NULL)
3072     return NULL;
3073
3074   for (; bsp != NULL; bsp = bsp->next)
3075     {
3076       if (bsp->breakpoint_at == breakpoint)
3077         return bsp;
3078     }
3079   return NULL;
3080 }
3081
3082 /* Put in *NUM the breakpoint number of the first breakpoint we are
3083    stopped at.  *BSP upon return is a bpstat which points to the
3084    remaining breakpoints stopped at (but which is not guaranteed to be
3085    good for anything but further calls to bpstat_num).
3086
3087    Return 0 if passed a bpstat which does not indicate any breakpoints.
3088    Return -1 if stopped at a breakpoint that has been deleted since
3089    we set it.
3090    Return 1 otherwise.  */
3091
3092 int
3093 bpstat_num (bpstat *bsp, int *num)
3094 {
3095   struct breakpoint *b;
3096
3097   if ((*bsp) == NULL)
3098     return 0;                   /* No more breakpoint values */
3099
3100   /* We assume we'll never have several bpstats that correspond to a
3101      single breakpoint -- otherwise, this function might return the
3102      same number more than once and this will look ugly.  */
3103   b = (*bsp)->breakpoint_at;
3104   *bsp = (*bsp)->next;
3105   if (b == NULL)
3106     return -1;                  /* breakpoint that's been deleted since */
3107
3108   *num = b->number;             /* We have its number */
3109   return 1;
3110 }
3111
3112 /* Modify BS so that the actions will not be performed.  */
3113
3114 void
3115 bpstat_clear_actions (bpstat bs)
3116 {
3117   for (; bs != NULL; bs = bs->next)
3118     {
3119       decref_counted_command_line (&bs->commands);
3120       bs->commands_left = NULL;
3121       if (bs->old_val != NULL)
3122         {
3123           value_free (bs->old_val);
3124           bs->old_val = NULL;
3125         }
3126     }
3127 }
3128
3129 /* Called when a command is about to proceed the inferior.  */
3130
3131 static void
3132 breakpoint_about_to_proceed (void)
3133 {
3134   if (!ptid_equal (inferior_ptid, null_ptid))
3135     {
3136       struct thread_info *tp = inferior_thread ();
3137
3138       /* Allow inferior function calls in breakpoint commands to not
3139          interrupt the command list.  When the call finishes
3140          successfully, the inferior will be standing at the same
3141          breakpoint as if nothing happened.  */
3142       if (tp->control.in_infcall)
3143         return;
3144     }
3145
3146   breakpoint_proceeded = 1;
3147 }
3148
3149 /* Stub for cleaning up our state if we error-out of a breakpoint
3150    command.  */
3151 static void
3152 cleanup_executing_breakpoints (void *ignore)
3153 {
3154   executing_breakpoint_commands = 0;
3155 }
3156
3157 /* Execute all the commands associated with all the breakpoints at
3158    this location.  Any of these commands could cause the process to
3159    proceed beyond this point, etc.  We look out for such changes by
3160    checking the global "breakpoint_proceeded" after each command.
3161
3162    Returns true if a breakpoint command resumed the inferior.  In that
3163    case, it is the caller's responsibility to recall it again with the
3164    bpstat of the current thread.  */
3165
3166 static int
3167 bpstat_do_actions_1 (bpstat *bsp)
3168 {
3169   bpstat bs;
3170   struct cleanup *old_chain;
3171   int again = 0;
3172
3173   /* Avoid endless recursion if a `source' command is contained
3174      in bs->commands.  */
3175   if (executing_breakpoint_commands)
3176     return 0;
3177
3178   executing_breakpoint_commands = 1;
3179   old_chain = make_cleanup (cleanup_executing_breakpoints, 0);
3180
3181   /* This pointer will iterate over the list of bpstat's.  */
3182   bs = *bsp;
3183
3184   breakpoint_proceeded = 0;
3185   for (; bs != NULL; bs = bs->next)
3186     {
3187       struct counted_command_line *ccmd;
3188       struct command_line *cmd;
3189       struct cleanup *this_cmd_tree_chain;
3190
3191       /* Take ownership of the BSP's command tree, if it has one.
3192
3193          The command tree could legitimately contain commands like
3194          'step' and 'next', which call clear_proceed_status, which
3195          frees stop_bpstat's command tree.  To make sure this doesn't
3196          free the tree we're executing out from under us, we need to
3197          take ownership of the tree ourselves.  Since a given bpstat's
3198          commands are only executed once, we don't need to copy it; we
3199          can clear the pointer in the bpstat, and make sure we free
3200          the tree when we're done.  */
3201       ccmd = bs->commands;
3202       bs->commands = NULL;
3203       this_cmd_tree_chain
3204         = make_cleanup_decref_counted_command_line (&ccmd);
3205       cmd = bs->commands_left;
3206       bs->commands_left = NULL;
3207
3208       while (cmd != NULL)
3209         {
3210           execute_control_command (cmd);
3211
3212           if (breakpoint_proceeded)
3213             break;
3214           else
3215             cmd = cmd->next;
3216         }
3217
3218       /* We can free this command tree now.  */
3219       do_cleanups (this_cmd_tree_chain);
3220
3221       if (breakpoint_proceeded)
3222         {
3223           if (target_can_async_p ())
3224             /* If we are in async mode, then the target might be still
3225                running, not stopped at any breakpoint, so nothing for
3226                us to do here -- just return to the event loop.  */
3227             ;
3228           else
3229             /* In sync mode, when execute_control_command returns
3230                we're already standing on the next breakpoint.
3231                Breakpoint commands for that stop were not run, since
3232                execute_command does not run breakpoint commands --
3233                only command_line_handler does, but that one is not
3234                involved in execution of breakpoint commands.  So, we
3235                can now execute breakpoint commands.  It should be
3236                noted that making execute_command do bpstat actions is
3237                not an option -- in this case we'll have recursive
3238                invocation of bpstat for each breakpoint with a
3239                command, and can easily blow up GDB stack.  Instead, we
3240                return true, which will trigger the caller to recall us
3241                with the new stop_bpstat.  */
3242             again = 1;
3243           break;
3244         }
3245     }
3246   do_cleanups (old_chain);
3247   return again;
3248 }
3249
3250 void
3251 bpstat_do_actions (void)
3252 {
3253   /* Do any commands attached to breakpoint we are stopped at.  */
3254   while (!ptid_equal (inferior_ptid, null_ptid)
3255          && target_has_execution
3256          && !is_exited (inferior_ptid)
3257          && !is_executing (inferior_ptid))
3258     /* Since in sync mode, bpstat_do_actions may resume the inferior,
3259        and only return when it is stopped at the next breakpoint, we
3260        keep doing breakpoint actions until it returns false to
3261        indicate the inferior was not resumed.  */
3262     if (!bpstat_do_actions_1 (&inferior_thread ()->control.stop_bpstat))
3263       break;
3264 }
3265
3266 /* Print out the (old or new) value associated with a watchpoint.  */
3267
3268 static void
3269 watchpoint_value_print (struct value *val, struct ui_file *stream)
3270 {
3271   if (val == NULL)
3272     fprintf_unfiltered (stream, _("<unreadable>"));
3273   else
3274     {
3275       struct value_print_options opts;
3276       get_user_print_options (&opts);
3277       value_print (val, stream, &opts);
3278     }
3279 }
3280
3281 /* This is the normal print function for a bpstat.  In the future,
3282    much of this logic could (should?) be moved to bpstat_stop_status,
3283    by having it set different print_it values.
3284
3285    Current scheme: When we stop, bpstat_print() is called.  It loops
3286    through the bpstat list of things causing this stop, calling the
3287    print_bp_stop_message function on each one.  The behavior of the
3288    print_bp_stop_message function depends on the print_it field of
3289    bpstat.  If such field so indicates, call this function here.
3290
3291    Return values from this routine (ultimately used by bpstat_print()
3292    and normal_stop() to decide what to do): 
3293    PRINT_NOTHING: Means we already printed all we needed to print,
3294    don't print anything else.
3295    PRINT_SRC_ONLY: Means we printed something, and we do *not* desire
3296    that something to be followed by a location.
3297    PRINT_SCR_AND_LOC: Means we printed something, and we *do* desire
3298    that something to be followed by a location.
3299    PRINT_UNKNOWN: Means we printed nothing or we need to do some more
3300    analysis.  */
3301
3302 static enum print_stop_action
3303 print_it_typical (bpstat bs)
3304 {
3305   struct cleanup *old_chain;
3306   struct breakpoint *b;
3307   const struct bp_location *bl;
3308   struct ui_stream *stb;
3309   int bp_temp = 0;
3310   enum print_stop_action result;
3311
3312   /* bs->breakpoint_at can be NULL if it was a momentary breakpoint
3313      which has since been deleted.  */
3314   if (bs->breakpoint_at == NULL)
3315     return PRINT_UNKNOWN;
3316
3317   gdb_assert (bs->bp_location_at != NULL);
3318
3319   bl = bs->bp_location_at;
3320   b = bs->breakpoint_at;
3321
3322   stb = ui_out_stream_new (uiout);
3323   old_chain = make_cleanup_ui_out_stream_delete (stb);
3324
3325   switch (b->type)
3326     {
3327     case bp_breakpoint:
3328     case bp_hardware_breakpoint:
3329       bp_temp = b->disposition == disp_del;
3330       if (bl->address != bl->requested_address)
3331         breakpoint_adjustment_warning (bl->requested_address,
3332                                        bl->address,
3333                                        b->number, 1);
3334       annotate_breakpoint (b->number);
3335       if (bp_temp) 
3336         ui_out_text (uiout, "\nTemporary breakpoint ");
3337       else
3338         ui_out_text (uiout, "\nBreakpoint ");
3339       if (ui_out_is_mi_like_p (uiout))
3340         {
3341           ui_out_field_string (uiout, "reason", 
3342                           async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT));
3343           ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
3344         }
3345       ui_out_field_int (uiout, "bkptno", b->number);
3346       ui_out_text (uiout, ", ");
3347       result = PRINT_SRC_AND_LOC;
3348       break;
3349
3350     case bp_shlib_event:
3351       /* Did we stop because the user set the stop_on_solib_events
3352          variable?  (If so, we report this as a generic, "Stopped due
3353          to shlib event" message.) */
3354       printf_filtered (_("Stopped due to shared library event\n"));
3355       result = PRINT_NOTHING;
3356       break;
3357
3358     case bp_thread_event:
3359       /* Not sure how we will get here.
3360          GDB should not stop for these breakpoints.  */
3361       printf_filtered (_("Thread Event Breakpoint: gdb should not stop!\n"));
3362       result = PRINT_NOTHING;
3363       break;
3364
3365     case bp_overlay_event:
3366       /* By analogy with the thread event, GDB should not stop for these.  */
3367       printf_filtered (_("Overlay Event Breakpoint: gdb should not stop!\n"));
3368       result = PRINT_NOTHING;
3369       break;
3370
3371     case bp_longjmp_master:
3372       /* These should never be enabled.  */
3373       printf_filtered (_("Longjmp Master Breakpoint: gdb should not stop!\n"));
3374       result = PRINT_NOTHING;
3375       break;
3376
3377     case bp_std_terminate_master:
3378       /* These should never be enabled.  */
3379       printf_filtered (_("std::terminate Master Breakpoint: "
3380                          "gdb should not stop!\n"));
3381       result = PRINT_NOTHING;
3382       break;
3383
3384     case bp_exception_master:
3385       /* These should never be enabled.  */
3386       printf_filtered (_("Exception Master Breakpoint: "
3387                          "gdb should not stop!\n"));
3388       result = PRINT_NOTHING;
3389       break;
3390
3391     case bp_watchpoint:
3392     case bp_hardware_watchpoint:
3393       annotate_watchpoint (b->number);
3394       if (ui_out_is_mi_like_p (uiout))
3395         ui_out_field_string
3396           (uiout, "reason",
3397            async_reason_lookup (EXEC_ASYNC_WATCHPOINT_TRIGGER));
3398       mention (b);
3399       make_cleanup_ui_out_tuple_begin_end (uiout, "value");
3400       ui_out_text (uiout, "\nOld value = ");
3401       watchpoint_value_print (bs->old_val, stb->stream);
3402       ui_out_field_stream (uiout, "old", stb);
3403       ui_out_text (uiout, "\nNew value = ");
3404       watchpoint_value_print (b->val, stb->stream);
3405       ui_out_field_stream (uiout, "new", stb);
3406       ui_out_text (uiout, "\n");
3407       /* More than one watchpoint may have been triggered.  */
3408       result = PRINT_UNKNOWN;
3409       break;
3410
3411     case bp_read_watchpoint:
3412       if (ui_out_is_mi_like_p (uiout))
3413         ui_out_field_string
3414           (uiout, "reason",
3415            async_reason_lookup (EXEC_ASYNC_READ_WATCHPOINT_TRIGGER));
3416       mention (b);
3417       make_cleanup_ui_out_tuple_begin_end (uiout, "value");
3418       ui_out_text (uiout, "\nValue = ");
3419       watchpoint_value_print (b->val, stb->stream);
3420       ui_out_field_stream (uiout, "value", stb);
3421       ui_out_text (uiout, "\n");
3422       result = PRINT_UNKNOWN;
3423       break;
3424
3425     case bp_access_watchpoint:
3426       if (bs->old_val != NULL)
3427         {
3428           annotate_watchpoint (b->number);
3429           if (ui_out_is_mi_like_p (uiout))
3430             ui_out_field_string
3431               (uiout, "reason",
3432                async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
3433           mention (b);
3434           make_cleanup_ui_out_tuple_begin_end (uiout, "value");
3435           ui_out_text (uiout, "\nOld value = ");
3436           watchpoint_value_print (bs->old_val, stb->stream);
3437           ui_out_field_stream (uiout, "old", stb);
3438           ui_out_text (uiout, "\nNew value = ");
3439         }
3440       else 
3441         {
3442           mention (b);
3443           if (ui_out_is_mi_like_p (uiout))
3444             ui_out_field_string
3445               (uiout, "reason",
3446                async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
3447           make_cleanup_ui_out_tuple_begin_end (uiout, "value");
3448           ui_out_text (uiout, "\nValue = ");
3449         }
3450       watchpoint_value_print (b->val, stb->stream);
3451       ui_out_field_stream (uiout, "new", stb);
3452       ui_out_text (uiout, "\n");
3453       result = PRINT_UNKNOWN;
3454       break;
3455
3456     /* Fall through, we don't deal with these types of breakpoints
3457        here.  */
3458
3459     case bp_finish:
3460       if (ui_out_is_mi_like_p (uiout))
3461         ui_out_field_string
3462           (uiout, "reason",
3463            async_reason_lookup (EXEC_ASYNC_FUNCTION_FINISHED));
3464       result = PRINT_UNKNOWN;
3465       break;
3466
3467     case bp_until:
3468       if (ui_out_is_mi_like_p (uiout))
3469         ui_out_field_string
3470           (uiout, "reason",
3471            async_reason_lookup (EXEC_ASYNC_LOCATION_REACHED));
3472       result = PRINT_UNKNOWN;
3473       break;
3474
3475     case bp_none:
3476     case bp_longjmp:
3477     case bp_longjmp_resume:
3478     case bp_exception:
3479     case bp_exception_resume:
3480     case bp_step_resume:
3481     case bp_watchpoint_scope:
3482     case bp_call_dummy:
3483     case bp_std_terminate:
3484     case bp_tracepoint:
3485     case bp_fast_tracepoint:
3486     case bp_jit_event:
3487     default:
3488       result = PRINT_UNKNOWN;
3489       break;
3490     }
3491
3492   do_cleanups (old_chain);
3493   return result;
3494 }
3495
3496 /* Generic routine for printing messages indicating why we
3497    stopped.  The behavior of this function depends on the value
3498    'print_it' in the bpstat structure.  Under some circumstances we
3499    may decide not to print anything here and delegate the task to
3500    normal_stop().  */
3501
3502 static enum print_stop_action
3503 print_bp_stop_message (bpstat bs)
3504 {
3505   switch (bs->print_it)
3506     {
3507     case print_it_noop:
3508       /* Nothing should be printed for this bpstat entry.  */
3509       return PRINT_UNKNOWN;
3510       break;
3511
3512     case print_it_done:
3513       /* We still want to print the frame, but we already printed the
3514          relevant messages.  */
3515       return PRINT_SRC_AND_LOC;
3516       break;
3517
3518     case print_it_normal:
3519       {
3520         struct breakpoint *b = bs->breakpoint_at;
3521
3522         /* Normal case.  Call the breakpoint's print_it method, or
3523            print_it_typical.  */
3524         /* FIXME: how breakpoint can ever be NULL here?  */
3525         if (b != NULL && b->ops != NULL && b->ops->print_it != NULL)
3526           return b->ops->print_it (b);
3527         else
3528           return print_it_typical (bs);
3529       }
3530         break;
3531
3532     default:
3533       internal_error (__FILE__, __LINE__,
3534                       _("print_bp_stop_message: unrecognized enum value"));
3535       break;
3536     }
3537 }
3538
3539 /* Print a message indicating what happened.  This is called from
3540    normal_stop().  The input to this routine is the head of the bpstat
3541    list - a list of the eventpoints that caused this stop.  This
3542    routine calls the generic print routine for printing a message
3543    about reasons for stopping.  This will print (for example) the
3544    "Breakpoint n," part of the output.  The return value of this
3545    routine is one of:
3546
3547    PRINT_UNKNOWN: Means we printed nothing.
3548    PRINT_SRC_AND_LOC: Means we printed something, and expect subsequent
3549    code to print the location.  An example is 
3550    "Breakpoint 1, " which should be followed by
3551    the location.
3552    PRINT_SRC_ONLY: Means we printed something, but there is no need
3553    to also print the location part of the message.
3554    An example is the catch/throw messages, which
3555    don't require a location appended to the end.
3556    PRINT_NOTHING: We have done some printing and we don't need any 
3557    further info to be printed.  */
3558
3559 enum print_stop_action
3560 bpstat_print (bpstat bs)
3561 {
3562   int val;
3563
3564   /* Maybe another breakpoint in the chain caused us to stop.
3565      (Currently all watchpoints go on the bpstat whether hit or not.
3566      That probably could (should) be changed, provided care is taken
3567      with respect to bpstat_explains_signal).  */
3568   for (; bs; bs = bs->next)
3569     {
3570       val = print_bp_stop_message (bs);
3571       if (val == PRINT_SRC_ONLY 
3572           || val == PRINT_SRC_AND_LOC 
3573           || val == PRINT_NOTHING)
3574         return val;
3575     }
3576
3577   /* We reached the end of the chain, or we got a null BS to start
3578      with and nothing was printed.  */
3579   return PRINT_UNKNOWN;
3580 }
3581
3582 /* Evaluate the expression EXP and return 1 if value is zero.  This is
3583    used inside a catch_errors to evaluate the breakpoint condition.
3584    The argument is a "struct expression *" that has been cast to a
3585    "char *" to make it pass through catch_errors.  */
3586
3587 static int
3588 breakpoint_cond_eval (void *exp)
3589 {
3590   struct value *mark = value_mark ();
3591   int i = !value_true (evaluate_expression ((struct expression *) exp));
3592
3593   value_free_to_mark (mark);
3594   return i;
3595 }
3596
3597 /* Allocate a new bpstat.  Link it to the FIFO list by BS_LINK_POINTER.  */
3598
3599 static bpstat
3600 bpstat_alloc (struct bp_location *bl, bpstat **bs_link_pointer)
3601 {
3602   bpstat bs;
3603
3604   bs = (bpstat) xmalloc (sizeof (*bs));
3605   bs->next = NULL;
3606   **bs_link_pointer = bs;
3607   *bs_link_pointer = &bs->next;
3608   bs->breakpoint_at = bl->owner;
3609   bs->bp_location_at = bl;
3610   incref_bp_location (bl);
3611   /* If the condition is false, etc., don't do the commands.  */
3612   bs->commands = NULL;
3613   bs->commands_left = NULL;
3614   bs->old_val = NULL;
3615   bs->print_it = print_it_normal;
3616   return bs;
3617 }
3618 \f
3619 /* The target has stopped with waitstatus WS.  Check if any hardware
3620    watchpoints have triggered, according to the target.  */
3621
3622 int
3623 watchpoints_triggered (struct target_waitstatus *ws)
3624 {
3625   int stopped_by_watchpoint = target_stopped_by_watchpoint ();
3626   CORE_ADDR addr;
3627   struct breakpoint *b;
3628
3629   if (!stopped_by_watchpoint)
3630     {
3631       /* We were not stopped by a watchpoint.  Mark all watchpoints
3632          as not triggered.  */
3633       ALL_BREAKPOINTS (b)
3634         if (is_hardware_watchpoint (b))
3635           b->watchpoint_triggered = watch_triggered_no;
3636
3637       return 0;
3638     }
3639
3640   if (!target_stopped_data_address (&current_target, &addr))
3641     {
3642       /* We were stopped by a watchpoint, but we don't know where.
3643          Mark all watchpoints as unknown.  */
3644       ALL_BREAKPOINTS (b)
3645         if (is_hardware_watchpoint (b))
3646           b->watchpoint_triggered = watch_triggered_unknown;
3647
3648       return stopped_by_watchpoint;
3649     }
3650
3651   /* The target could report the data address.  Mark watchpoints
3652      affected by this data address as triggered, and all others as not
3653      triggered.  */
3654
3655   ALL_BREAKPOINTS (b)
3656     if (is_hardware_watchpoint (b))
3657       {
3658         struct bp_location *loc;
3659
3660         b->watchpoint_triggered = watch_triggered_no;
3661         for (loc = b->loc; loc; loc = loc->next)
3662           /* Exact match not required.  Within range is
3663              sufficient.  */
3664           if (target_watchpoint_addr_within_range (&current_target,
3665                                                    addr, loc->address,
3666                                                    loc->length))
3667             {
3668               b->watchpoint_triggered = watch_triggered_yes;
3669               break;
3670             }
3671       }
3672
3673   return 1;
3674 }
3675
3676 /* Possible return values for watchpoint_check (this can't be an enum
3677    because of check_errors).  */
3678 /* The watchpoint has been deleted.  */
3679 #define WP_DELETED 1
3680 /* The value has changed.  */
3681 #define WP_VALUE_CHANGED 2
3682 /* The value has not changed.  */
3683 #define WP_VALUE_NOT_CHANGED 3
3684 /* Ignore this watchpoint, no matter if the value changed or not.  */
3685 #define WP_IGNORE 4
3686
3687 #define BP_TEMPFLAG 1
3688 #define BP_HARDWAREFLAG 2
3689
3690 /* Evaluate watchpoint condition expression and check if its value
3691    changed.
3692
3693    P should be a pointer to struct bpstat, but is defined as a void *
3694    in order for this function to be usable with catch_errors.  */
3695
3696 static int
3697 watchpoint_check (void *p)
3698 {
3699   bpstat bs = (bpstat) p;
3700   struct breakpoint *b;
3701   struct frame_info *fr;
3702   int within_current_scope;
3703
3704   /* BS is built from an existing struct breakpoint.  */
3705   gdb_assert (bs->breakpoint_at != NULL);
3706   b = bs->breakpoint_at;
3707
3708   /* If this is a local watchpoint, we only want to check if the
3709      watchpoint frame is in scope if the current thread is the thread
3710      that was used to create the watchpoint.  */
3711   if (!watchpoint_in_thread_scope (b))
3712     return WP_IGNORE;
3713
3714   if (b->exp_valid_block == NULL)
3715     within_current_scope = 1;
3716   else
3717     {
3718       struct frame_info *frame = get_current_frame ();
3719       struct gdbarch *frame_arch = get_frame_arch (frame);
3720       CORE_ADDR frame_pc = get_frame_pc (frame);
3721
3722       /* in_function_epilogue_p() returns a non-zero value if we're
3723          still in the function but the stack frame has already been
3724          invalidated.  Since we can't rely on the values of local
3725          variables after the stack has been destroyed, we are treating
3726          the watchpoint in that state as `not changed' without further
3727          checking.  Don't mark watchpoints as changed if the current
3728          frame is in an epilogue - even if they are in some other
3729          frame, our view of the stack is likely to be wrong and
3730          frame_find_by_id could error out.  */
3731       if (gdbarch_in_function_epilogue_p (frame_arch, frame_pc))
3732         return WP_IGNORE;
3733
3734       fr = frame_find_by_id (b->watchpoint_frame);
3735       within_current_scope = (fr != NULL);
3736
3737       /* If we've gotten confused in the unwinder, we might have
3738          returned a frame that can't describe this variable.  */
3739       if (within_current_scope)
3740         {
3741           struct symbol *function;
3742
3743           function = get_frame_function (fr);
3744           if (function == NULL
3745               || !contained_in (b->exp_valid_block,
3746                                 SYMBOL_BLOCK_VALUE (function)))
3747             within_current_scope = 0;
3748         }
3749
3750       if (within_current_scope)
3751         /* If we end up stopping, the current frame will get selected
3752            in normal_stop.  So this call to select_frame won't affect
3753            the user.  */
3754         select_frame (fr);
3755     }
3756
3757   if (within_current_scope)
3758     {
3759       /* We use value_{,free_to_}mark because it could be a *long*
3760          time before we return to the command level and call
3761          free_all_values.  We can't call free_all_values because we
3762          might be in the middle of evaluating a function call.  */
3763
3764       int pc = 0;
3765       struct value *mark = value_mark ();
3766       struct value *new_val;
3767
3768       fetch_subexp_value (b->exp, &pc, &new_val, NULL, NULL);
3769
3770       /* We use value_equal_contents instead of value_equal because
3771          the latter coerces an array to a pointer, thus comparing just
3772          the address of the array instead of its contents.  This is
3773          not what we want.  */
3774       if ((b->val != NULL) != (new_val != NULL)
3775           || (b->val != NULL && !value_equal_contents (b->val, new_val)))
3776         {
3777           if (new_val != NULL)
3778             {
3779               release_value (new_val);
3780               value_free_to_mark (mark);
3781             }
3782           bs->old_val = b->val;
3783           b->val = new_val;
3784           b->val_valid = 1;
3785           return WP_VALUE_CHANGED;
3786         }
3787       else
3788         {
3789           /* Nothing changed.  */
3790           value_free_to_mark (mark);
3791           return WP_VALUE_NOT_CHANGED;
3792         }
3793     }
3794   else
3795     {
3796       /* This seems like the only logical thing to do because
3797          if we temporarily ignored the watchpoint, then when
3798          we reenter the block in which it is valid it contains
3799          garbage (in the case of a function, it may have two
3800          garbage values, one before and one after the prologue).
3801          So we can't even detect the first assignment to it and
3802          watch after that (since the garbage may or may not equal
3803          the first value assigned).  */
3804       /* We print all the stop information in print_it_typical(), but
3805          in this case, by the time we call print_it_typical() this bp
3806          will be deleted already.  So we have no choice but print the
3807          information here.  */
3808       if (ui_out_is_mi_like_p (uiout))
3809         ui_out_field_string
3810           (uiout, "reason", async_reason_lookup (EXEC_ASYNC_WATCHPOINT_SCOPE));
3811       ui_out_text (uiout, "\nWatchpoint ");
3812       ui_out_field_int (uiout, "wpnum", b->number);
3813       ui_out_text (uiout,
3814                    " deleted because the program has left the block in\n\
3815 which its expression is valid.\n");     
3816
3817       if (b->related_breakpoint)
3818         {
3819           b->related_breakpoint->disposition = disp_del_at_next_stop;
3820           b->related_breakpoint->related_breakpoint = NULL;
3821           b->related_breakpoint = NULL;
3822         }
3823       b->disposition = disp_del_at_next_stop;
3824
3825       return WP_DELETED;
3826     }
3827 }
3828
3829 /* Return true if it looks like target has stopped due to hitting
3830    breakpoint location BL.  This function does not check if we
3831    should stop, only if BL explains the stop.   */
3832 static int
3833 bpstat_check_location (const struct bp_location *bl,
3834                        struct address_space *aspace, CORE_ADDR bp_addr)
3835 {
3836   struct breakpoint *b = bl->owner;
3837
3838   /* BL is from existing struct breakpoint.  */
3839   gdb_assert (b != NULL);
3840
3841   /* By definition, the inferior does not report stops at
3842      tracepoints.  */
3843   if (is_tracepoint (b))
3844     return 0;
3845
3846   if (!is_watchpoint (b)
3847       && b->type != bp_hardware_breakpoint
3848       && b->type != bp_catchpoint)      /* a non-watchpoint bp */
3849     {
3850       if (!breakpoint_address_match (bl->pspace->aspace, bl->address,
3851                                      aspace, bp_addr))
3852         return 0;
3853       if (overlay_debugging             /* unmapped overlay section */
3854           && section_is_overlay (bl->section)
3855           && !section_is_mapped (bl->section))
3856         return 0;
3857     }
3858
3859   /* Continuable hardware watchpoints are treated as non-existent if the
3860      reason we stopped wasn't a hardware watchpoint (we didn't stop on
3861      some data address).  Otherwise gdb won't stop on a break instruction
3862      in the code (not from a breakpoint) when a hardware watchpoint has
3863      been defined.  Also skip watchpoints which we know did not trigger
3864      (did not match the data address).  */
3865
3866   if (is_hardware_watchpoint (b)
3867       && b->watchpoint_triggered == watch_triggered_no)
3868     return 0;
3869   
3870   if (b->type == bp_hardware_breakpoint)
3871     {
3872       if (bl->address != bp_addr)
3873         return 0;
3874       if (overlay_debugging             /* unmapped overlay section */
3875           && section_is_overlay (bl->section)
3876           && !section_is_mapped (bl->section))
3877         return 0;
3878     }
3879
3880   if (b->type == bp_catchpoint)
3881     {
3882       gdb_assert (b->ops != NULL && b->ops->breakpoint_hit != NULL);
3883       if (!b->ops->breakpoint_hit (b))
3884         return 0;
3885     }
3886      
3887   return 1;
3888 }
3889
3890 /* If BS refers to a watchpoint, determine if the watched values
3891    has actually changed, and we should stop.  If not, set BS->stop
3892    to 0.  */
3893 static void
3894 bpstat_check_watchpoint (bpstat bs)
3895 {
3896   const struct bp_location *bl;
3897   struct breakpoint *b;
3898
3899   /* BS is built for existing struct breakpoint.  */
3900   bl = bs->bp_location_at;
3901   gdb_assert (bl != NULL);
3902   b = bs->breakpoint_at;
3903   gdb_assert (b != NULL);
3904
3905   if (is_watchpoint (b))
3906     {
3907       int must_check_value = 0;
3908       
3909       if (b->type == bp_watchpoint)
3910         /* For a software watchpoint, we must always check the
3911            watched value.  */
3912         must_check_value = 1;
3913       else if (b->watchpoint_triggered == watch_triggered_yes)
3914         /* We have a hardware watchpoint (read, write, or access)
3915            and the target earlier reported an address watched by
3916            this watchpoint.  */
3917         must_check_value = 1;
3918       else if (b->watchpoint_triggered == watch_triggered_unknown
3919                && b->type == bp_hardware_watchpoint)
3920         /* We were stopped by a hardware watchpoint, but the target could
3921            not report the data address.  We must check the watchpoint's
3922            value.  Access and read watchpoints are out of luck; without
3923            a data address, we can't figure it out.  */
3924         must_check_value = 1;
3925       
3926       if (must_check_value)
3927         {
3928           char *message
3929             = xstrprintf ("Error evaluating expression for watchpoint %d\n",
3930                           b->number);
3931           struct cleanup *cleanups = make_cleanup (xfree, message);
3932           int e = catch_errors (watchpoint_check, bs, message,
3933                                 RETURN_MASK_ALL);
3934           do_cleanups (cleanups);
3935           switch (e)
3936             {
3937             case WP_DELETED:
3938               /* We've already printed what needs to be printed.  */
3939               bs->print_it = print_it_done;
3940               /* Stop.  */
3941               break;
3942             case WP_IGNORE:
3943               bs->print_it = print_it_noop;
3944               bs->stop = 0;
3945               break;
3946             case WP_VALUE_CHANGED:
3947               if (b->type == bp_read_watchpoint)
3948                 {
3949                   /* There are two cases to consider here:
3950
3951                      1. We're watching the triggered memory for reads.
3952                      In that case, trust the target, and always report
3953                      the watchpoint hit to the user.  Even though
3954                      reads don't cause value changes, the value may
3955                      have changed since the last time it was read, and
3956                      since we're not trapping writes, we will not see
3957                      those, and as such we should ignore our notion of
3958                      old value.
3959
3960                      2. We're watching the triggered memory for both
3961                      reads and writes.  There are two ways this may
3962                      happen:
3963
3964                      2.1. This is a target that can't break on data
3965                      reads only, but can break on accesses (reads or
3966                      writes), such as e.g., x86.  We detect this case
3967                      at the time we try to insert read watchpoints.
3968
3969                      2.2. Otherwise, the target supports read
3970                      watchpoints, but, the user set an access or write
3971                      watchpoint watching the same memory as this read
3972                      watchpoint.
3973
3974                      If we're watching memory writes as well as reads,
3975                      ignore watchpoint hits when we find that the
3976                      value hasn't changed, as reads don't cause
3977                      changes.  This still gives false positives when
3978                      the program writes the same value to memory as
3979                      what there was already in memory (we will confuse
3980                      it for a read), but it's much better than
3981                      nothing.  */
3982
3983                   int other_write_watchpoint = 0;
3984
3985                   if (bl->watchpoint_type == hw_read)
3986                     {
3987                       struct breakpoint *other_b;
3988
3989                       ALL_BREAKPOINTS (other_b)
3990                         if ((other_b->type == bp_hardware_watchpoint
3991                              || other_b->type == bp_access_watchpoint)
3992                             && (other_b->watchpoint_triggered
3993                                 == watch_triggered_yes))
3994                           {
3995                             other_write_watchpoint = 1;
3996                             break;
3997                           }
3998                     }
3999
4000                   if (other_write_watchpoint
4001                       || bl->watchpoint_type == hw_access)
4002                     {
4003                       /* We're watching the same memory for writes,
4004                          and the value changed since the last time we
4005                          updated it, so this trap must be for a write.
4006                          Ignore it.  */
4007                       bs->print_it = print_it_noop;
4008                       bs->stop = 0;
4009                     }
4010                 }
4011               break;
4012             case WP_VALUE_NOT_CHANGED:
4013               if (b->type == bp_hardware_watchpoint
4014                   || b->type == bp_watchpoint)
4015                 {
4016                   /* Don't stop: write watchpoints shouldn't fire if
4017                      the value hasn't changed.  */
4018                   bs->print_it = print_it_noop;
4019                   bs->stop = 0;
4020                 }
4021               /* Stop.  */
4022               break;
4023             default:
4024               /* Can't happen.  */
4025             case 0:
4026               /* Error from catch_errors.  */
4027               printf_filtered (_("Watchpoint %d deleted.\n"), b->number);
4028               if (b->related_breakpoint)
4029                 b->related_breakpoint->disposition = disp_del_at_next_stop;
4030               b->disposition = disp_del_at_next_stop;
4031               /* We've already printed what needs to be printed.  */
4032               bs->print_it = print_it_done;
4033               break;
4034             }
4035         }
4036       else      /* must_check_value == 0 */
4037         {
4038           /* This is a case where some watchpoint(s) triggered, but
4039              not at the address of this watchpoint, or else no
4040              watchpoint triggered after all.  So don't print
4041              anything for this watchpoint.  */
4042           bs->print_it = print_it_noop;
4043           bs->stop = 0;
4044         }
4045     }
4046 }
4047
4048
4049 /* Check conditions (condition proper, frame, thread and ignore count)
4050    of breakpoint referred to by BS.  If we should not stop for this
4051    breakpoint, set BS->stop to 0.  */
4052
4053 static void
4054 bpstat_check_breakpoint_conditions (bpstat bs, ptid_t ptid)
4055 {
4056   int thread_id = pid_to_thread_id (ptid);
4057   const struct bp_location *bl;
4058   struct breakpoint *b;
4059
4060   /* BS is built for existing struct breakpoint.  */
4061   bl = bs->bp_location_at;
4062   gdb_assert (bl != NULL);
4063   b = bs->breakpoint_at;
4064   gdb_assert (b != NULL);
4065
4066   if (frame_id_p (b->frame_id)
4067       && !frame_id_eq (b->frame_id, get_stack_frame_id (get_current_frame ())))
4068     bs->stop = 0;
4069   else if (bs->stop)
4070     {
4071       int value_is_zero = 0;
4072       struct expression *cond;
4073
4074       if (is_watchpoint (b))
4075         cond = b->cond_exp;
4076       else
4077         cond = bl->cond;
4078
4079       if (cond && b->disposition != disp_del_at_next_stop)
4080         {
4081           int within_current_scope = 1;
4082
4083           /* We use value_mark and value_free_to_mark because it could
4084              be a long time before we return to the command level and
4085              call free_all_values.  We can't call free_all_values
4086              because we might be in the middle of evaluating a
4087              function call.  */
4088           struct value *mark = value_mark ();
4089
4090           /* Need to select the frame, with all that implies so that
4091              the conditions will have the right context.  Because we
4092              use the frame, we will not see an inlined function's
4093              variables when we arrive at a breakpoint at the start
4094              of the inlined function; the current frame will be the
4095              call site.  */
4096           if (!is_watchpoint (b) || b->cond_exp_valid_block == NULL)
4097             select_frame (get_current_frame ());
4098           else
4099             {
4100               struct frame_info *frame;
4101
4102               /* For local watchpoint expressions, which particular
4103                  instance of a local is being watched matters, so we
4104                  keep track of the frame to evaluate the expression
4105                  in.  To evaluate the condition however, it doesn't
4106                  really matter which instantiation of the function
4107                  where the condition makes sense triggers the
4108                  watchpoint.  This allows an expression like "watch
4109                  global if q > 10" set in `func', catch writes to
4110                  global on all threads that call `func', or catch
4111                  writes on all recursive calls of `func' by a single
4112                  thread.  We simply always evaluate the condition in
4113                  the innermost frame that's executing where it makes
4114                  sense to evaluate the condition.  It seems
4115                  intuitive.  */
4116               frame = block_innermost_frame (b->cond_exp_valid_block);
4117               if (frame != NULL)
4118                 select_frame (frame);
4119               else
4120                 within_current_scope = 0;
4121             }
4122           if (within_current_scope)
4123             value_is_zero
4124               = catch_errors (breakpoint_cond_eval, cond,
4125                               "Error in testing breakpoint condition:\n",
4126                               RETURN_MASK_ALL);
4127           else
4128             {
4129               warning (_("Watchpoint condition cannot be tested "
4130                          "in the current scope"));
4131               /* If we failed to set the right context for this
4132                  watchpoint, unconditionally report it.  */
4133               value_is_zero = 0;
4134             }
4135           /* FIXME-someday, should give breakpoint #.  */
4136           value_free_to_mark (mark);
4137         }
4138
4139       if (cond && value_is_zero)
4140         {
4141           bs->stop = 0;
4142         }
4143       else if (b->thread != -1 && b->thread != thread_id)
4144         {
4145           bs->stop = 0;
4146         }
4147       else if (b->ignore_count > 0)
4148         {
4149           b->ignore_count--;
4150           annotate_ignore_count_change ();
4151           bs->stop = 0;
4152           /* Increase the hit count even though we don't stop.  */
4153           ++(b->hit_count);
4154         }       
4155     }
4156 }
4157
4158
4159 /* Get a bpstat associated with having just stopped at address
4160    BP_ADDR in thread PTID.
4161
4162    Determine whether we stopped at a breakpoint, etc, or whether we
4163    don't understand this stop.  Result is a chain of bpstat's such
4164    that:
4165
4166    if we don't understand the stop, the result is a null pointer.
4167
4168    if we understand why we stopped, the result is not null.
4169
4170    Each element of the chain refers to a particular breakpoint or
4171    watchpoint at which we have stopped.  (We may have stopped for
4172    several reasons concurrently.)
4173
4174    Each element of the chain has valid next, breakpoint_at,
4175    commands, FIXME??? fields.  */
4176
4177 bpstat
4178 bpstat_stop_status (struct address_space *aspace,
4179                     CORE_ADDR bp_addr, ptid_t ptid)
4180 {
4181   struct breakpoint *b = NULL;
4182   struct bp_location *bl;
4183   struct bp_location *loc;
4184   /* First item of allocated bpstat's.  */
4185   bpstat bs_head = NULL, *bs_link = &bs_head;
4186   /* Pointer to the last thing in the chain currently.  */
4187   bpstat bs;
4188   int ix;
4189   int need_remove_insert;
4190   int removed_any;
4191
4192   /* First, build the bpstat chain with locations that explain a
4193      target stop, while being careful to not set the target running,
4194      as that may invalidate locations (in particular watchpoint
4195      locations are recreated).  Resuming will happen here with
4196      breakpoint conditions or watchpoint expressions that include
4197      inferior function calls.  */
4198
4199   ALL_BREAKPOINTS (b)
4200     {
4201       if (!breakpoint_enabled (b) && b->enable_state != bp_permanent)
4202         continue;
4203
4204       for (bl = b->loc; bl != NULL; bl = bl->next)
4205         {
4206           /* For hardware watchpoints, we look only at the first
4207              location.  The watchpoint_check function will work on the
4208              entire expression, not the individual locations.  For
4209              read watchpoints, the watchpoints_triggered function has
4210              checked all locations already.  */
4211           if (b->type == bp_hardware_watchpoint && bl != b->loc)
4212             break;
4213
4214           if (bl->shlib_disabled)
4215             continue;
4216
4217           if (!bpstat_check_location (bl, aspace, bp_addr))
4218             continue;
4219
4220           /* Come here if it's a watchpoint, or if the break address
4221              matches.  */
4222
4223           bs = bpstat_alloc (bl, &bs_link);     /* Alloc a bpstat to
4224                                                    explain stop.  */
4225
4226           /* Assume we stop.  Should we find a watchpoint that is not
4227              actually triggered, or if the condition of the breakpoint
4228              evaluates as false, we'll reset 'stop' to 0.  */
4229           bs->stop = 1;
4230           bs->print = 1;
4231
4232           /* If this is a scope breakpoint, mark the associated
4233              watchpoint as triggered so that we will handle the
4234              out-of-scope event.  We'll get to the watchpoint next
4235              iteration.  */
4236           if (b->type == bp_watchpoint_scope)
4237             b->related_breakpoint->watchpoint_triggered = watch_triggered_yes;
4238         }
4239     }
4240
4241   for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, loc); ++ix)
4242     {
4243       if (breakpoint_address_match (loc->pspace->aspace, loc->address,
4244                                     aspace, bp_addr))
4245         {
4246           bs = bpstat_alloc (loc, &bs_link);
4247           /* For hits of moribund locations, we should just proceed.  */
4248           bs->stop = 0;
4249           bs->print = 0;
4250           bs->print_it = print_it_noop;
4251         }
4252     }
4253
4254   /* Now go through the locations that caused the target to stop, and
4255      check whether we're interested in reporting this stop to higher
4256      layers, or whether we should resume the target transparently.  */
4257
4258   removed_any = 0;
4259
4260   for (bs = bs_head; bs != NULL; bs = bs->next)
4261     {
4262       if (!bs->stop)
4263         continue;
4264
4265       bpstat_check_watchpoint (bs);
4266       if (!bs->stop)
4267         continue;
4268
4269       b = bs->breakpoint_at;
4270
4271           if (b->type == bp_thread_event || b->type == bp_overlay_event
4272               || b->type == bp_longjmp_master
4273               || b->type == bp_std_terminate_master
4274               || b->type == bp_exception_master)
4275             /* We do not stop for these.  */
4276             bs->stop = 0;
4277           else
4278             bpstat_check_breakpoint_conditions (bs, ptid);
4279
4280           if (bs->stop)
4281             {
4282               ++(b->hit_count);
4283
4284               /* We will stop here.  */
4285               if (b->disposition == disp_disable)
4286                 {
4287                   if (b->enable_state != bp_permanent)
4288                     b->enable_state = bp_disabled;
4289                   removed_any = 1;
4290                 }
4291               if (b->silent)
4292                 bs->print = 0;
4293               bs->commands = b->commands;
4294               incref_counted_command_line (bs->commands);
4295               bs->commands_left = bs->commands ? bs->commands->commands : NULL;
4296               if (bs->commands_left
4297                   && (strcmp ("silent", bs->commands_left->line) == 0
4298                       || (xdb_commands
4299                           && strcmp ("Q",
4300                                      bs->commands_left->line) == 0)))
4301                 {
4302                   bs->commands_left = bs->commands_left->next;
4303                   bs->print = 0;
4304                 }
4305             }
4306
4307           /* Print nothing for this entry if we dont stop or dont print.  */
4308           if (bs->stop == 0 || bs->print == 0)
4309             bs->print_it = print_it_noop;
4310     }
4311
4312   /* If we aren't stopping, the value of some hardware watchpoint may
4313      not have changed, but the intermediate memory locations we are
4314      watching may have.  Don't bother if we're stopping; this will get
4315      done later.  */
4316   need_remove_insert = 0;
4317   if (! bpstat_causes_stop (bs_head))
4318     for (bs = bs_head; bs != NULL; bs = bs->next)
4319       if (!bs->stop
4320           && bs->breakpoint_at
4321           && is_hardware_watchpoint (bs->breakpoint_at))
4322         {
4323           update_watchpoint (bs->breakpoint_at, 0 /* don't reparse.  */);
4324           need_remove_insert = 1;
4325         }
4326
4327   if (need_remove_insert)
4328     update_global_location_list (1);
4329   else if (removed_any)
4330     update_global_location_list (0);
4331
4332   return bs_head;
4333 }
4334
4335 static void
4336 handle_jit_event (void)
4337 {
4338   struct frame_info *frame;
4339   struct gdbarch *gdbarch;
4340
4341   /* Switch terminal for any messages produced by
4342      breakpoint_re_set.  */
4343   target_terminal_ours_for_output ();
4344
4345   frame = get_current_frame ();
4346   gdbarch = get_frame_arch (frame);
4347
4348   jit_event_handler (gdbarch);
4349
4350   target_terminal_inferior ();
4351 }
4352
4353 /* Prepare WHAT final decision for infrun.  */
4354
4355 /* Decide what infrun needs to do with this bpstat.  */
4356
4357 struct bpstat_what
4358 bpstat_what (bpstat bs)
4359 {
4360   struct bpstat_what retval;
4361   /* We need to defer calling `solib_add', as adding new symbols
4362      resets breakpoints, which in turn deletes breakpoint locations,
4363      and hence may clear unprocessed entries in the BS chain.  */
4364   int shlib_event = 0;
4365   int jit_event = 0;
4366
4367   retval.main_action = BPSTAT_WHAT_KEEP_CHECKING;
4368   retval.call_dummy = STOP_NONE;
4369   retval.is_longjmp = 0;
4370
4371   for (; bs != NULL; bs = bs->next)
4372     {
4373       /* Extract this BS's action.  After processing each BS, we check
4374          if its action overrides all we've seem so far.  */
4375       enum bpstat_what_main_action this_action = BPSTAT_WHAT_KEEP_CHECKING;
4376       enum bptype bptype;
4377
4378       if (bs->breakpoint_at == NULL)
4379         {
4380           /* I suspect this can happen if it was a momentary
4381              breakpoint which has since been deleted.  */
4382           bptype = bp_none;
4383         }
4384       else if (bs->breakpoint_at == NULL)
4385         bptype = bp_none;
4386       else
4387         bptype = bs->breakpoint_at->type;
4388
4389       switch (bptype)
4390         {
4391         case bp_none:
4392           break;
4393         case bp_breakpoint:
4394         case bp_hardware_breakpoint:
4395         case bp_until:
4396         case bp_finish:
4397           if (bs->stop)
4398             {
4399               if (bs->print)
4400                 this_action = BPSTAT_WHAT_STOP_NOISY;
4401               else
4402                 this_action = BPSTAT_WHAT_STOP_SILENT;
4403             }
4404           else
4405             this_action = BPSTAT_WHAT_SINGLE;
4406           break;
4407         case bp_watchpoint:
4408         case bp_hardware_watchpoint:
4409         case bp_read_watchpoint:
4410         case bp_access_watchpoint:
4411           if (bs->stop)
4412             {
4413               if (bs->print)
4414                 this_action = BPSTAT_WHAT_STOP_NOISY;
4415               else
4416                 this_action = BPSTAT_WHAT_STOP_SILENT;
4417             }
4418           else
4419             {
4420               /* There was a watchpoint, but we're not stopping.
4421                  This requires no further action.  */
4422             }
4423           break;
4424         case bp_longjmp:
4425         case bp_exception:
4426           this_action = BPSTAT_WHAT_SET_LONGJMP_RESUME;
4427           retval.is_longjmp = bptype == bp_longjmp;
4428           break;
4429         case bp_longjmp_resume:
4430         case bp_exception_resume:
4431           this_action = BPSTAT_WHAT_CLEAR_LONGJMP_RESUME;
4432           retval.is_longjmp = bptype == bp_longjmp_resume;
4433           break;
4434         case bp_step_resume:
4435           if (bs->stop)
4436             this_action = BPSTAT_WHAT_STEP_RESUME;
4437           else
4438             {
4439               /* It is for the wrong frame.  */
4440               this_action = BPSTAT_WHAT_SINGLE;
4441             }
4442           break;
4443         case bp_watchpoint_scope:
4444         case bp_thread_event:
4445         case bp_overlay_event:
4446         case bp_longjmp_master:
4447         case bp_std_terminate_master:
4448         case bp_exception_master:
4449           this_action = BPSTAT_WHAT_SINGLE;
4450           break;
4451         case bp_catchpoint:
4452           if (bs->stop)
4453             {
4454               if (bs->print)
4455                 this_action = BPSTAT_WHAT_STOP_NOISY;
4456               else
4457                 this_action = BPSTAT_WHAT_STOP_SILENT;
4458             }
4459           else
4460             {
4461               /* There was a catchpoint, but we're not stopping.
4462                  This requires no further action.  */
4463             }
4464           break;
4465         case bp_shlib_event:
4466           shlib_event = 1;
4467
4468           /* If requested, stop when the dynamic linker notifies GDB
4469              of events.  This allows the user to get control and place
4470              breakpoints in initializer routines for dynamically
4471              loaded objects (among other things).  */
4472           if (stop_on_solib_events)
4473             this_action = BPSTAT_WHAT_STOP_NOISY;
4474           else
4475             this_action = BPSTAT_WHAT_SINGLE;
4476           break;
4477         case bp_jit_event:
4478           jit_event = 1;
4479           this_action = BPSTAT_WHAT_SINGLE;
4480           break;
4481         case bp_call_dummy:
4482           /* Make sure the action is stop (silent or noisy),
4483              so infrun.c pops the dummy frame.  */
4484           retval.call_dummy = STOP_STACK_DUMMY;
4485           this_action = BPSTAT_WHAT_STOP_SILENT;
4486           break;
4487         case bp_std_terminate:
4488           /* Make sure the action is stop (silent or noisy),
4489              so infrun.c pops the dummy frame.  */
4490           retval.call_dummy = STOP_STD_TERMINATE;
4491           this_action = BPSTAT_WHAT_STOP_SILENT;
4492           break;
4493         case bp_tracepoint:
4494         case bp_fast_tracepoint:
4495         case bp_static_tracepoint:
4496           /* Tracepoint hits should not be reported back to GDB, and
4497              if one got through somehow, it should have been filtered
4498              out already.  */
4499           internal_error (__FILE__, __LINE__,
4500                           _("bpstat_what: tracepoint encountered"));
4501         default:
4502           internal_error (__FILE__, __LINE__,
4503                           _("bpstat_what: unhandled bptype %d"), (int) bptype);
4504         }
4505
4506       retval.main_action = max (retval.main_action, this_action);
4507     }
4508
4509   if (shlib_event)
4510     {
4511       if (debug_infrun)
4512         fprintf_unfiltered (gdb_stdlog, "bpstat_what: bp_shlib_event\n");
4513
4514       /* Check for any newly added shared libraries if we're supposed
4515          to be adding them automatically.  */
4516
4517       /* Switch terminal for any messages produced by
4518          breakpoint_re_set.  */
4519       target_terminal_ours_for_output ();
4520
4521 #ifdef SOLIB_ADD
4522       SOLIB_ADD (NULL, 0, &current_target, auto_solib_add);
4523 #else
4524       solib_add (NULL, 0, &current_target, auto_solib_add);
4525 #endif
4526
4527       target_terminal_inferior ();
4528     }
4529
4530   if (jit_event)
4531     {
4532       if (debug_infrun)
4533         fprintf_unfiltered (gdb_stdlog, "bpstat_what: bp_jit_event\n");
4534
4535       handle_jit_event ();
4536     }
4537
4538   return retval;
4539 }
4540
4541 /* Nonzero if we should step constantly (e.g. watchpoints on machines
4542    without hardware support).  This isn't related to a specific bpstat,
4543    just to things like whether watchpoints are set.  */
4544
4545 int
4546 bpstat_should_step (void)
4547 {
4548   struct breakpoint *b;
4549
4550   ALL_BREAKPOINTS (b)
4551     if (breakpoint_enabled (b) && b->type == bp_watchpoint && b->loc != NULL)
4552       return 1;
4553   return 0;
4554 }
4555
4556 int
4557 bpstat_causes_stop (bpstat bs)
4558 {
4559   for (; bs != NULL; bs = bs->next)
4560     if (bs->stop)
4561       return 1;
4562
4563   return 0;
4564 }
4565
4566 \f
4567
4568 /* Compute a string of spaces suitable to indent the next line
4569    so it starts at the position corresponding to the table column
4570    named COL_NAME in the currently active table of UIOUT.  */
4571
4572 static char *
4573 wrap_indent_at_field (struct ui_out *uiout, const char *col_name)
4574 {
4575   static char wrap_indent[80];
4576   int i, total_width, width, align;
4577   char *text;
4578
4579   total_width = 0;
4580   for (i = 1; ui_out_query_field (uiout, i, &width, &align, &text); i++)
4581     {
4582       if (strcmp (text, col_name) == 0)
4583         {
4584           gdb_assert (total_width < sizeof wrap_indent);
4585           memset (wrap_indent, ' ', total_width);
4586           wrap_indent[total_width] = 0;
4587
4588           return wrap_indent;
4589         }
4590
4591       total_width += width + 1;
4592     }
4593
4594   return NULL;
4595 }
4596
4597 /* Print the LOC location out of the list of B->LOC locations.  */
4598
4599 static void
4600 print_breakpoint_location (struct breakpoint *b,
4601                            struct bp_location *loc)
4602 {
4603   struct cleanup *old_chain = save_current_program_space ();
4604
4605   if (loc != NULL && loc->shlib_disabled)
4606     loc = NULL;
4607
4608   if (loc != NULL)
4609     set_current_program_space (loc->pspace);
4610
4611   if (b->source_file && loc)
4612     {
4613       struct symbol *sym 
4614         = find_pc_sect_function (loc->address, loc->section);
4615       if (sym)
4616         {
4617           ui_out_text (uiout, "in ");
4618           ui_out_field_string (uiout, "func",
4619                                SYMBOL_PRINT_NAME (sym));
4620           ui_out_text (uiout, " ");
4621           ui_out_wrap_hint (uiout, wrap_indent_at_field (uiout, "what"));
4622           ui_out_text (uiout, "at ");
4623         }
4624       ui_out_field_string (uiout, "file", b->source_file);
4625       ui_out_text (uiout, ":");
4626       
4627       if (ui_out_is_mi_like_p (uiout))
4628         {
4629           struct symtab_and_line sal = find_pc_line (loc->address, 0);
4630           char *fullname = symtab_to_fullname (sal.symtab);
4631           
4632           if (fullname)
4633             ui_out_field_string (uiout, "fullname", fullname);
4634         }
4635       
4636       ui_out_field_int (uiout, "line", b->line_number);
4637     }
4638   else if (loc)
4639     {
4640       struct ui_stream *stb = ui_out_stream_new (uiout);
4641       struct cleanup *stb_chain = make_cleanup_ui_out_stream_delete (stb);
4642
4643       print_address_symbolic (loc->gdbarch, loc->address, stb->stream,
4644                               demangle, "");
4645       ui_out_field_stream (uiout, "at", stb);
4646
4647       do_cleanups (stb_chain);
4648     }
4649   else
4650     ui_out_field_string (uiout, "pending", b->addr_string);
4651
4652   do_cleanups (old_chain);
4653 }
4654
4655 static const char *
4656 bptype_string (enum bptype type)
4657 {
4658   struct ep_type_description
4659     {
4660       enum bptype type;
4661       char *description;
4662     };
4663   static struct ep_type_description bptypes[] =
4664   {
4665     {bp_none, "?deleted?"},
4666     {bp_breakpoint, "breakpoint"},
4667     {bp_hardware_breakpoint, "hw breakpoint"},
4668     {bp_until, "until"},
4669     {bp_finish, "finish"},
4670     {bp_watchpoint, "watchpoint"},
4671     {bp_hardware_watchpoint, "hw watchpoint"},
4672     {bp_read_watchpoint, "read watchpoint"},
4673     {bp_access_watchpoint, "acc watchpoint"},
4674     {bp_longjmp, "longjmp"},
4675     {bp_longjmp_resume, "longjmp resume"},
4676     {bp_exception, "exception"},
4677     {bp_exception_resume, "exception resume"},
4678     {bp_step_resume, "step resume"},
4679     {bp_watchpoint_scope, "watchpoint scope"},
4680     {bp_call_dummy, "call dummy"},
4681     {bp_std_terminate, "std::terminate"},
4682     {bp_shlib_event, "shlib events"},
4683     {bp_thread_event, "thread events"},
4684     {bp_overlay_event, "overlay events"},
4685     {bp_longjmp_master, "longjmp master"},
4686     {bp_std_terminate_master, "std::terminate master"},
4687     {bp_exception_master, "exception master"},
4688     {bp_catchpoint, "catchpoint"},
4689     {bp_tracepoint, "tracepoint"},
4690     {bp_fast_tracepoint, "fast tracepoint"},
4691     {bp_static_tracepoint, "static tracepoint"},
4692     {bp_jit_event, "jit events"},
4693   };
4694
4695   if (((int) type >= (sizeof (bptypes) / sizeof (bptypes[0])))
4696       || ((int) type != bptypes[(int) type].type))
4697     internal_error (__FILE__, __LINE__,
4698                     _("bptypes table does not describe type #%d."),
4699                     (int) type);
4700
4701   return bptypes[(int) type].description;
4702 }
4703
4704 /* Print B to gdb_stdout.  */
4705
4706 static void
4707 print_one_breakpoint_location (struct breakpoint *b,
4708                                struct bp_location *loc,
4709                                int loc_number,
4710                                struct bp_location **last_loc,
4711                                int print_address_bits,
4712                                int allflag)
4713 {
4714   struct command_line *l;
4715   static char bpenables[] = "nynny";
4716   struct cleanup *bkpt_chain;
4717
4718   int header_of_multiple = 0;
4719   int part_of_multiple = (loc != NULL);
4720   struct value_print_options opts;
4721
4722   get_user_print_options (&opts);
4723
4724   gdb_assert (!loc || loc_number != 0);
4725   /* See comment in print_one_breakpoint concerning treatment of
4726      breakpoints with single disabled location.  */
4727   if (loc == NULL 
4728       && (b->loc != NULL 
4729           && (b->loc->next != NULL || !b->loc->enabled)))
4730     header_of_multiple = 1;
4731   if (loc == NULL)
4732     loc = b->loc;
4733
4734   annotate_record ();
4735   bkpt_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "bkpt");
4736
4737   /* 1 */
4738   annotate_field (0);
4739   if (part_of_multiple)
4740     {
4741       char *formatted;
4742       formatted = xstrprintf ("%d.%d", b->number, loc_number);
4743       ui_out_field_string (uiout, "number", formatted);
4744       xfree (formatted);
4745     }
4746   else
4747     {
4748       ui_out_field_int (uiout, "number", b->number);
4749     }
4750
4751   /* 2 */
4752   annotate_field (1);
4753   if (part_of_multiple)
4754     ui_out_field_skip (uiout, "type");
4755   else
4756     ui_out_field_string (uiout, "type", bptype_string (b->type));
4757
4758   /* 3 */
4759   annotate_field (2);
4760   if (part_of_multiple)
4761     ui_out_field_skip (uiout, "disp");
4762   else
4763     ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
4764
4765
4766   /* 4 */
4767   annotate_field (3);
4768   if (part_of_multiple)
4769     ui_out_field_string (uiout, "enabled", loc->enabled ? "y" : "n");
4770   else
4771     ui_out_field_fmt (uiout, "enabled", "%c", 
4772                       bpenables[(int) b->enable_state]);
4773   ui_out_spaces (uiout, 2);
4774
4775   
4776   /* 5 and 6 */
4777   if (b->ops != NULL && b->ops->print_one != NULL)
4778     {
4779       /* Although the print_one can possibly print all locations,
4780          calling it here is not likely to get any nice result.  So,
4781          make sure there's just one location.  */
4782       gdb_assert (b->loc == NULL || b->loc->next == NULL);
4783       b->ops->print_one (b, last_loc);
4784     }
4785   else
4786     switch (b->type)
4787       {
4788       case bp_none:
4789         internal_error (__FILE__, __LINE__,
4790                         _("print_one_breakpoint: bp_none encountered\n"));
4791         break;
4792
4793       case bp_watchpoint:
4794       case bp_hardware_watchpoint:
4795       case bp_read_watchpoint:
4796       case bp_access_watchpoint:
4797         /* Field 4, the address, is omitted (which makes the columns
4798            not line up too nicely with the headers, but the effect
4799            is relatively readable).  */
4800         if (opts.addressprint)
4801           ui_out_field_skip (uiout, "addr");
4802         annotate_field (5);
4803         ui_out_field_string (uiout, "what", b->exp_string);
4804         break;
4805
4806       case bp_breakpoint:
4807       case bp_hardware_breakpoint:
4808       case bp_until:
4809       case bp_finish:
4810       case bp_longjmp:
4811       case bp_longjmp_resume:
4812       case bp_exception:
4813       case bp_exception_resume:
4814       case bp_step_resume:
4815       case bp_watchpoint_scope:
4816       case bp_call_dummy:
4817       case bp_std_terminate:
4818       case bp_shlib_event:
4819       case bp_thread_event:
4820       case bp_overlay_event:
4821       case bp_longjmp_master:
4822       case bp_std_terminate_master:
4823       case bp_exception_master:
4824       case bp_tracepoint:
4825       case bp_fast_tracepoint:
4826       case bp_static_tracepoint:
4827       case bp_jit_event:
4828         if (opts.addressprint)
4829           {
4830             annotate_field (4);
4831             if (header_of_multiple)
4832               ui_out_field_string (uiout, "addr", "<MULTIPLE>");
4833             else if (b->loc == NULL || loc->shlib_disabled)
4834               ui_out_field_string (uiout, "addr", "<PENDING>");
4835             else
4836               ui_out_field_core_addr (uiout, "addr",
4837                                       loc->gdbarch, loc->address);
4838           }
4839         annotate_field (5);
4840         if (!header_of_multiple)
4841           print_breakpoint_location (b, loc);
4842         if (b->loc)
4843           *last_loc = b->loc;
4844         break;
4845       }
4846
4847
4848   /* For backward compatibility, don't display inferiors unless there
4849      are several.  */
4850   if (loc != NULL
4851       && !header_of_multiple
4852       && (allflag
4853           || (!gdbarch_has_global_breakpoints (target_gdbarch)
4854               && (number_of_program_spaces () > 1
4855                   || number_of_inferiors () > 1)
4856               /* LOC is for existing B, it cannot be in
4857                  moribund_locations and thus having NULL OWNER.  */
4858               && loc->owner->type != bp_catchpoint)))
4859     {
4860       struct inferior *inf;
4861       int first = 1;
4862
4863       for (inf = inferior_list; inf != NULL; inf = inf->next)
4864         {
4865           if (inf->pspace == loc->pspace)
4866             {
4867               if (first)
4868                 {
4869                   first = 0;
4870                   ui_out_text (uiout, " inf ");
4871                 }
4872               else
4873                 ui_out_text (uiout, ", ");
4874               ui_out_text (uiout, plongest (inf->num));
4875             }
4876         }
4877     }
4878
4879   if (!part_of_multiple)
4880     {
4881       if (b->thread != -1)
4882         {
4883           /* FIXME: This seems to be redundant and lost here; see the
4884              "stop only in" line a little further down.  */
4885           ui_out_text (uiout, " thread ");
4886           ui_out_field_int (uiout, "thread", b->thread);
4887         }
4888       else if (b->task != 0)
4889         {
4890           ui_out_text (uiout, " task ");
4891           ui_out_field_int (uiout, "task", b->task);
4892         }
4893     }
4894   
4895   ui_out_text (uiout, "\n");
4896   
4897   if (!part_of_multiple && b->static_trace_marker_id)
4898     {
4899       gdb_assert (b->type == bp_static_tracepoint);
4900
4901       ui_out_text (uiout, "\tmarker id is ");
4902       ui_out_field_string (uiout, "static-tracepoint-marker-string-id",
4903                            b->static_trace_marker_id);
4904       ui_out_text (uiout, "\n");
4905     }
4906
4907   if (part_of_multiple && frame_id_p (b->frame_id))
4908     {
4909       annotate_field (6);
4910       ui_out_text (uiout, "\tstop only in stack frame at ");
4911       /* FIXME: cagney/2002-12-01: Shouldn't be poeking around inside
4912          the frame ID.  */
4913       ui_out_field_core_addr (uiout, "frame",
4914                               b->gdbarch, b->frame_id.stack_addr);
4915       ui_out_text (uiout, "\n");
4916     }
4917   
4918   if (!part_of_multiple && b->cond_string && !ada_exception_catchpoint_p (b))
4919     {
4920       /* We do not print the condition for Ada exception catchpoints
4921          because the condition is an internal implementation detail
4922          that we do not want to expose to the user.  */
4923       annotate_field (7);
4924       if (is_tracepoint (b))
4925         ui_out_text (uiout, "\ttrace only if ");
4926       else
4927         ui_out_text (uiout, "\tstop only if ");
4928       ui_out_field_string (uiout, "cond", b->cond_string);
4929       ui_out_text (uiout, "\n");
4930     }
4931
4932   if (!part_of_multiple && b->thread != -1)
4933     {
4934       /* FIXME should make an annotation for this.  */
4935       ui_out_text (uiout, "\tstop only in thread ");
4936       ui_out_field_int (uiout, "thread", b->thread);
4937       ui_out_text (uiout, "\n");
4938     }
4939   
4940   if (!part_of_multiple && b->hit_count)
4941     {
4942       /* FIXME should make an annotation for this.  */
4943       if (ep_is_catchpoint (b))
4944         ui_out_text (uiout, "\tcatchpoint");
4945       else
4946         ui_out_text (uiout, "\tbreakpoint");
4947       ui_out_text (uiout, " already hit ");
4948       ui_out_field_int (uiout, "times", b->hit_count);
4949       if (b->hit_count == 1)
4950         ui_out_text (uiout, " time\n");
4951       else
4952         ui_out_text (uiout, " times\n");
4953     }
4954   
4955   /* Output the count also if it is zero, but only if this is mi.
4956      FIXME: Should have a better test for this.  */
4957   if (ui_out_is_mi_like_p (uiout))
4958     if (!part_of_multiple && b->hit_count == 0)
4959       ui_out_field_int (uiout, "times", b->hit_count);
4960
4961   if (!part_of_multiple && b->ignore_count)
4962     {
4963       annotate_field (8);
4964       ui_out_text (uiout, "\tignore next ");
4965       ui_out_field_int (uiout, "ignore", b->ignore_count);
4966       ui_out_text (uiout, " hits\n");
4967     }
4968
4969   l = b->commands ? b->commands->commands : NULL;
4970   if (!part_of_multiple && l)
4971     {
4972       struct cleanup *script_chain;
4973
4974       annotate_field (9);
4975       script_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "script");
4976       print_command_lines (uiout, l, 4);
4977       do_cleanups (script_chain);
4978     }
4979
4980   if (!part_of_multiple && b->pass_count)
4981     {
4982       annotate_field (10);
4983       ui_out_text (uiout, "\tpass count ");
4984       ui_out_field_int (uiout, "pass", b->pass_count);
4985       ui_out_text (uiout, " \n");
4986     }
4987
4988   if (ui_out_is_mi_like_p (uiout) && !part_of_multiple)
4989     {
4990       if (b->addr_string)
4991         ui_out_field_string (uiout, "original-location", b->addr_string);
4992       else if (b->exp_string)
4993         ui_out_field_string (uiout, "original-location", b->exp_string);
4994     }
4995         
4996   do_cleanups (bkpt_chain);
4997 }
4998
4999 static void
5000 print_one_breakpoint (struct breakpoint *b,
5001                       struct bp_location **last_loc, 
5002                       int print_address_bits,
5003                       int allflag)
5004 {
5005   print_one_breakpoint_location (b, NULL, 0, last_loc,
5006                                  print_address_bits, allflag);
5007
5008   /* If this breakpoint has custom print function,
5009      it's already printed.  Otherwise, print individual
5010      locations, if any.  */
5011   if (b->ops == NULL || b->ops->print_one == NULL)
5012     {
5013       /* If breakpoint has a single location that is disabled, we
5014          print it as if it had several locations, since otherwise it's
5015          hard to represent "breakpoint enabled, location disabled"
5016          situation.
5017
5018          Note that while hardware watchpoints have several locations
5019          internally, that's not a property exposed to user.  */
5020       if (b->loc 
5021           && !is_hardware_watchpoint (b)
5022           && (b->loc->next || !b->loc->enabled)
5023           && !ui_out_is_mi_like_p (uiout)) 
5024         {
5025           struct bp_location *loc;
5026           int n = 1;
5027           for (loc = b->loc; loc; loc = loc->next, ++n)
5028             print_one_breakpoint_location (b, loc, n, last_loc,
5029                                            print_address_bits, allflag);
5030         }
5031     }
5032 }
5033
5034 static int
5035 breakpoint_address_bits (struct breakpoint *b)
5036 {
5037   int print_address_bits = 0;
5038   struct bp_location *loc;
5039
5040   for (loc = b->loc; loc; loc = loc->next)
5041     {
5042       int addr_bit;
5043
5044       /* Software watchpoints that aren't watching memory don't have
5045          an address to print.  */
5046       if (b->type == bp_watchpoint && loc->watchpoint_type == -1)
5047         continue;
5048
5049       addr_bit = gdbarch_addr_bit (loc->gdbarch);
5050       if (addr_bit > print_address_bits)
5051         print_address_bits = addr_bit;
5052     }
5053
5054   return print_address_bits;
5055 }
5056
5057 struct captured_breakpoint_query_args
5058   {
5059     int bnum;
5060   };
5061
5062 static int
5063 do_captured_breakpoint_query (struct ui_out *uiout, void *data)
5064 {
5065   struct captured_breakpoint_query_args *args = data;
5066   struct breakpoint *b;
5067   struct bp_location *dummy_loc = NULL;
5068
5069   ALL_BREAKPOINTS (b)
5070     {
5071       if (args->bnum == b->number)
5072         {
5073           int print_address_bits = breakpoint_address_bits (b);
5074
5075           print_one_breakpoint (b, &dummy_loc, print_address_bits, 0);
5076           return GDB_RC_OK;
5077         }
5078     }
5079   return GDB_RC_NONE;
5080 }
5081
5082 enum gdb_rc
5083 gdb_breakpoint_query (struct ui_out *uiout, int bnum, 
5084                       char **error_message)
5085 {
5086   struct captured_breakpoint_query_args args;
5087
5088   args.bnum = bnum;
5089   /* For the moment we don't trust print_one_breakpoint() to not throw
5090      an error.  */
5091   if (catch_exceptions_with_msg (uiout, do_captured_breakpoint_query, &args,
5092                                  error_message, RETURN_MASK_ALL) < 0)
5093     return GDB_RC_FAIL;
5094   else
5095     return GDB_RC_OK;
5096 }
5097
5098 /* Return non-zero if B is user settable (breakpoints, watchpoints,
5099    catchpoints, et.al.).  */
5100
5101 static int
5102 user_settable_breakpoint (const struct breakpoint *b)
5103 {
5104   return (b->type == bp_breakpoint
5105           || b->type == bp_catchpoint
5106           || b->type == bp_hardware_breakpoint
5107           || is_tracepoint (b)
5108           || is_watchpoint (b));
5109 }
5110
5111 /* Return true if this breakpoint was set by the user, false if it is
5112    internal or momentary.  */
5113
5114 int
5115 user_breakpoint_p (struct breakpoint *b)
5116 {
5117   return user_settable_breakpoint (b) && b->number > 0;
5118 }
5119
5120 /* Print information on user settable breakpoint (watchpoint, etc)
5121    number BNUM.  If BNUM is -1 print all user-settable breakpoints.
5122    If ALLFLAG is non-zero, include non-user-settable breakpoints.  If
5123    FILTER is non-NULL, call it on each breakpoint and only include the
5124    ones for which it returns non-zero.  Return the total number of
5125    breakpoints listed.  */
5126
5127 static int
5128 breakpoint_1 (char *args, int allflag, 
5129               int (*filter) (const struct breakpoint *))
5130 {
5131   struct breakpoint *b;
5132   struct bp_location *last_loc = NULL;
5133   int nr_printable_breakpoints;
5134   struct cleanup *bkpttbl_chain;
5135   struct value_print_options opts;
5136   int print_address_bits = 0;
5137   int print_type_col_width = 14;
5138
5139   get_user_print_options (&opts);
5140
5141   /* Compute the number of rows in the table, as well as the size
5142      required for address fields.  */
5143   nr_printable_breakpoints = 0;
5144   ALL_BREAKPOINTS (b)
5145     {
5146       /* If we have a filter, only list the breakpoints it accepts.  */
5147       if (filter && !filter (b))
5148         continue;
5149
5150       /* If we have an "args" string, it is a list of breakpoints to 
5151          accept.  Skip the others.  */
5152       if (args != NULL && *args != '\0')
5153         {
5154           if (allflag && parse_and_eval_long (args) != b->number)
5155             continue;
5156           if (!allflag && !number_is_in_list (args, b->number))
5157             continue;
5158         }
5159
5160       if (allflag || user_breakpoint_p (b))
5161         {
5162           int addr_bit, type_len;
5163
5164           addr_bit = breakpoint_address_bits (b);
5165           if (addr_bit > print_address_bits)
5166             print_address_bits = addr_bit;
5167
5168           type_len = strlen (bptype_string (b->type));
5169           if (type_len > print_type_col_width)
5170             print_type_col_width = type_len;
5171
5172           nr_printable_breakpoints++;
5173         }
5174     }
5175
5176   if (opts.addressprint)
5177     bkpttbl_chain 
5178       = make_cleanup_ui_out_table_begin_end (uiout, 6,
5179                                              nr_printable_breakpoints,
5180                                              "BreakpointTable");
5181   else
5182     bkpttbl_chain 
5183       = make_cleanup_ui_out_table_begin_end (uiout, 5,
5184                                              nr_printable_breakpoints,
5185                                              "BreakpointTable");
5186
5187   if (nr_printable_breakpoints > 0)
5188     annotate_breakpoints_headers ();
5189   if (nr_printable_breakpoints > 0)
5190     annotate_field (0);
5191   ui_out_table_header (uiout, 7, ui_left, "number", "Num");     /* 1 */
5192   if (nr_printable_breakpoints > 0)
5193     annotate_field (1);
5194   ui_out_table_header (uiout, print_type_col_width, ui_left,
5195                        "type", "Type");                         /* 2 */
5196   if (nr_printable_breakpoints > 0)
5197     annotate_field (2);
5198   ui_out_table_header (uiout, 4, ui_left, "disp", "Disp");      /* 3 */
5199   if (nr_printable_breakpoints > 0)
5200     annotate_field (3);
5201   ui_out_table_header (uiout, 3, ui_left, "enabled", "Enb");    /* 4 */
5202   if (opts.addressprint)
5203     {
5204       if (nr_printable_breakpoints > 0)
5205         annotate_field (4);
5206       if (print_address_bits <= 32)
5207         ui_out_table_header (uiout, 10, ui_left, 
5208                              "addr", "Address");                /* 5 */
5209       else
5210         ui_out_table_header (uiout, 18, ui_left, 
5211                              "addr", "Address");                /* 5 */
5212     }
5213   if (nr_printable_breakpoints > 0)
5214     annotate_field (5);
5215   ui_out_table_header (uiout, 40, ui_noalign, "what", "What");  /* 6 */
5216   ui_out_table_body (uiout);
5217   if (nr_printable_breakpoints > 0)
5218     annotate_breakpoints_table ();
5219
5220   ALL_BREAKPOINTS (b)
5221     {
5222       QUIT;
5223       /* If we have a filter, only list the breakpoints it accepts.  */
5224       if (filter && !filter (b))
5225         continue;
5226
5227       /* If we have an "args" string, it is a list of breakpoints to 
5228          accept.  Skip the others.  */
5229
5230       if (args != NULL && *args != '\0')
5231         {
5232           if (allflag)  /* maintenance info breakpoint */
5233             {
5234               if (parse_and_eval_long (args) != b->number)
5235                 continue;
5236             }
5237           else          /* all others */
5238             {
5239               if (!number_is_in_list (args, b->number))
5240                 continue;
5241             }
5242         }
5243       /* We only print out user settable breakpoints unless the
5244          allflag is set.  */
5245       if (allflag || user_breakpoint_p (b))
5246         print_one_breakpoint (b, &last_loc, print_address_bits, allflag);
5247     }
5248
5249   do_cleanups (bkpttbl_chain);
5250
5251   if (nr_printable_breakpoints == 0)
5252     {
5253       /* If there's a filter, let the caller decide how to report
5254          empty list.  */
5255       if (!filter)
5256         {
5257           if (args == NULL || *args == '\0')
5258             ui_out_message (uiout, 0, "No breakpoints or watchpoints.\n");
5259           else
5260             ui_out_message (uiout, 0, 
5261                             "No breakpoint or watchpoint matching '%s'.\n",
5262                             args);
5263         }
5264     }
5265   else
5266     {
5267       if (last_loc && !server_command)
5268         set_next_address (last_loc->gdbarch, last_loc->address);
5269     }
5270
5271   /* FIXME?  Should this be moved up so that it is only called when
5272      there have been breakpoints? */
5273   annotate_breakpoints_table_end ();
5274
5275   return nr_printable_breakpoints;
5276 }
5277
5278 /* Display the value of default-collect in a way that is generally
5279    compatible with the breakpoint list.  */
5280
5281 static void
5282 default_collect_info (void)
5283 {
5284   /* If it has no value (which is frequently the case), say nothing; a
5285      message like "No default-collect." gets in user's face when it's
5286      not wanted.  */
5287   if (!*default_collect)
5288     return;
5289
5290   /* The following phrase lines up nicely with per-tracepoint collect
5291      actions.  */
5292   ui_out_text (uiout, "default collect ");
5293   ui_out_field_string (uiout, "default-collect", default_collect);
5294   ui_out_text (uiout, " \n");
5295 }
5296   
5297 static void
5298 breakpoints_info (char *args, int from_tty)
5299 {
5300   breakpoint_1 (args, 0, NULL);
5301
5302   default_collect_info ();
5303 }
5304
5305 static void
5306 watchpoints_info (char *args, int from_tty)
5307 {
5308   int num_printed = breakpoint_1 (args, 0, is_watchpoint);
5309
5310   if (num_printed == 0)
5311     {
5312       if (args == NULL || *args == '\0')
5313         ui_out_message (uiout, 0, "No watchpoints.\n");
5314       else
5315         ui_out_message (uiout, 0, "No watchpoint matching '%s'.\n", args);
5316     }
5317 }
5318
5319 static void
5320 maintenance_info_breakpoints (char *args, int from_tty)
5321 {
5322   breakpoint_1 (args, 1, NULL);
5323
5324   default_collect_info ();
5325 }
5326
5327 static int
5328 breakpoint_has_pc (struct breakpoint *b,
5329                    struct program_space *pspace,
5330                    CORE_ADDR pc, struct obj_section *section)
5331 {
5332   struct bp_location *bl = b->loc;
5333
5334   for (; bl; bl = bl->next)
5335     {
5336       if (bl->pspace == pspace
5337           && bl->address == pc
5338           && (!overlay_debugging || bl->section == section))
5339         return 1;         
5340     }
5341   return 0;
5342 }
5343
5344 /* Print a message describing any breakpoints set at PC.  This
5345    concerns with logical breakpoints, so we match program spaces, not
5346    address spaces.  */
5347
5348 static void
5349 describe_other_breakpoints (struct gdbarch *gdbarch,
5350                             struct program_space *pspace, CORE_ADDR pc,
5351                             struct obj_section *section, int thread)
5352 {
5353   int others = 0;
5354   struct breakpoint *b;
5355
5356   ALL_BREAKPOINTS (b)
5357     others += breakpoint_has_pc (b, pspace, pc, section);
5358   if (others > 0)
5359     {
5360       if (others == 1)
5361         printf_filtered (_("Note: breakpoint "));
5362       else /* if (others == ???) */
5363         printf_filtered (_("Note: breakpoints "));
5364       ALL_BREAKPOINTS (b)
5365         if (breakpoint_has_pc (b, pspace, pc, section))
5366           {
5367             others--;
5368             printf_filtered ("%d", b->number);
5369             if (b->thread == -1 && thread != -1)
5370               printf_filtered (" (all threads)");
5371             else if (b->thread != -1)
5372               printf_filtered (" (thread %d)", b->thread);
5373             printf_filtered ("%s%s ",
5374                              ((b->enable_state == bp_disabled
5375                                || b->enable_state == bp_call_disabled
5376                                || b->enable_state == bp_startup_disabled)
5377                               ? " (disabled)"
5378                               : b->enable_state == bp_permanent 
5379                               ? " (permanent)"
5380                               : ""),
5381                              (others > 1) ? "," 
5382                              : ((others == 1) ? " and" : ""));
5383           }
5384       printf_filtered (_("also set at pc "));
5385       fputs_filtered (paddress (gdbarch, pc), gdb_stdout);
5386       printf_filtered (".\n");
5387     }
5388 }
5389 \f
5390 /* Set the default place to put a breakpoint
5391    for the `break' command with no arguments.  */
5392
5393 void
5394 set_default_breakpoint (int valid, struct program_space *pspace,
5395                         CORE_ADDR addr, struct symtab *symtab,
5396                         int line)
5397 {
5398   default_breakpoint_valid = valid;
5399   default_breakpoint_pspace = pspace;
5400   default_breakpoint_address = addr;
5401   default_breakpoint_symtab = symtab;
5402   default_breakpoint_line = line;
5403 }
5404
5405 /* Return true iff it is meaningful to use the address member of
5406    BPT.  For some breakpoint types, the address member is irrelevant
5407    and it makes no sense to attempt to compare it to other addresses
5408    (or use it for any other purpose either).
5409
5410    More specifically, each of the following breakpoint types will
5411    always have a zero valued address and we don't want to mark
5412    breakpoints of any of these types to be a duplicate of an actual
5413    breakpoint at address zero:
5414
5415       bp_watchpoint
5416       bp_catchpoint
5417
5418 */
5419
5420 static int
5421 breakpoint_address_is_meaningful (struct breakpoint *bpt)
5422 {
5423   enum bptype type = bpt->type;
5424
5425   return (type != bp_watchpoint && type != bp_catchpoint);
5426 }
5427
5428 /* Assuming LOC1 and LOC2's owners are hardware watchpoints, returns
5429    true if LOC1 and LOC2 represent the same watchpoint location.  */
5430
5431 static int
5432 watchpoint_locations_match (struct bp_location *loc1, 
5433                             struct bp_location *loc2)
5434 {
5435   /* Both of them must not be in moribund_locations.  */
5436   gdb_assert (loc1->owner != NULL);
5437   gdb_assert (loc2->owner != NULL);
5438
5439   /* If the target can evaluate the condition expression in hardware,
5440      then we we need to insert both watchpoints even if they are at
5441      the same place.  Otherwise the watchpoint will only trigger when
5442      the condition of whichever watchpoint was inserted evaluates to
5443      true, not giving a chance for GDB to check the condition of the
5444      other watchpoint.  */
5445   if ((loc1->owner->cond_exp
5446        && target_can_accel_watchpoint_condition (loc1->address, 
5447                                                  loc1->length,
5448                                                  loc1->watchpoint_type,
5449                                                  loc1->owner->cond_exp))
5450       || (loc2->owner->cond_exp
5451           && target_can_accel_watchpoint_condition (loc2->address, 
5452                                                     loc2->length,
5453                                                     loc2->watchpoint_type,
5454                                                     loc2->owner->cond_exp)))
5455     return 0;
5456
5457   /* Note that this checks the owner's type, not the location's.  In
5458      case the target does not support read watchpoints, but does
5459      support access watchpoints, we'll have bp_read_watchpoint
5460      watchpoints with hw_access locations.  Those should be considered
5461      duplicates of hw_read locations.  The hw_read locations will
5462      become hw_access locations later.  */
5463   return (loc1->owner->type == loc2->owner->type
5464           && loc1->pspace->aspace == loc2->pspace->aspace
5465           && loc1->address == loc2->address
5466           && loc1->length == loc2->length);
5467 }
5468
5469 /* Returns true if {ASPACE1,ADDR1} and {ASPACE2,ADDR2} represent the
5470    same breakpoint location.  In most targets, this can only be true
5471    if ASPACE1 matches ASPACE2.  On targets that have global
5472    breakpoints, the address space doesn't really matter.  */
5473
5474 static int
5475 breakpoint_address_match (struct address_space *aspace1, CORE_ADDR addr1,
5476                           struct address_space *aspace2, CORE_ADDR addr2)
5477 {
5478   return ((gdbarch_has_global_breakpoints (target_gdbarch)
5479            || aspace1 == aspace2)
5480           && addr1 == addr2);
5481 }
5482
5483 /* Assuming LOC1 and LOC2's types' have meaningful target addresses
5484    (breakpoint_address_is_meaningful), returns true if LOC1 and LOC2
5485    represent the same location.  */
5486
5487 static int
5488 breakpoint_locations_match (struct bp_location *loc1, 
5489                             struct bp_location *loc2)
5490 {
5491   int hw_point1, hw_point2;
5492
5493   /* Both of them must not be in moribund_locations.  */
5494   gdb_assert (loc1->owner != NULL);
5495   gdb_assert (loc2->owner != NULL);
5496
5497   hw_point1 = is_hardware_watchpoint (loc1->owner);
5498   hw_point2 = is_hardware_watchpoint (loc2->owner);
5499
5500   if (hw_point1 != hw_point2)
5501     return 0;
5502   else if (hw_point1)
5503     return watchpoint_locations_match (loc1, loc2);
5504   else
5505     return breakpoint_address_match (loc1->pspace->aspace, loc1->address,
5506                                      loc2->pspace->aspace, loc2->address);
5507 }
5508
5509 static void
5510 breakpoint_adjustment_warning (CORE_ADDR from_addr, CORE_ADDR to_addr,
5511                                int bnum, int have_bnum)
5512 {
5513   char astr1[40];
5514   char astr2[40];
5515
5516   strcpy (astr1, hex_string_custom ((unsigned long) from_addr, 8));
5517   strcpy (astr2, hex_string_custom ((unsigned long) to_addr, 8));
5518   if (have_bnum)
5519     warning (_("Breakpoint %d address previously adjusted from %s to %s."),
5520              bnum, astr1, astr2);
5521   else
5522     warning (_("Breakpoint address adjusted from %s to %s."), astr1, astr2);
5523 }
5524
5525 /* Adjust a breakpoint's address to account for architectural
5526    constraints on breakpoint placement.  Return the adjusted address.
5527    Note: Very few targets require this kind of adjustment.  For most
5528    targets, this function is simply the identity function.  */
5529
5530 static CORE_ADDR
5531 adjust_breakpoint_address (struct gdbarch *gdbarch,
5532                            CORE_ADDR bpaddr, enum bptype bptype)
5533 {
5534   if (!gdbarch_adjust_breakpoint_address_p (gdbarch))
5535     {
5536       /* Very few targets need any kind of breakpoint adjustment.  */
5537       return bpaddr;
5538     }
5539   else if (bptype == bp_watchpoint
5540            || bptype == bp_hardware_watchpoint
5541            || bptype == bp_read_watchpoint
5542            || bptype == bp_access_watchpoint
5543            || bptype == bp_catchpoint)
5544     {
5545       /* Watchpoints and the various bp_catch_* eventpoints should not
5546          have their addresses modified.  */
5547       return bpaddr;
5548     }
5549   else
5550     {
5551       CORE_ADDR adjusted_bpaddr;
5552
5553       /* Some targets have architectural constraints on the placement
5554          of breakpoint instructions.  Obtain the adjusted address.  */
5555       adjusted_bpaddr = gdbarch_adjust_breakpoint_address (gdbarch, bpaddr);
5556
5557       /* An adjusted breakpoint address can significantly alter
5558          a user's expectations.  Print a warning if an adjustment
5559          is required.  */
5560       if (adjusted_bpaddr != bpaddr)
5561         breakpoint_adjustment_warning (bpaddr, adjusted_bpaddr, 0, 0);
5562
5563       return adjusted_bpaddr;
5564     }
5565 }
5566
5567 /* Allocate a struct bp_location.  */
5568
5569 static struct bp_location *
5570 allocate_bp_location (struct breakpoint *bpt)
5571 {
5572   struct bp_location *loc;
5573
5574   loc = xmalloc (sizeof (struct bp_location));
5575   memset (loc, 0, sizeof (*loc));
5576
5577   loc->owner = bpt;
5578   loc->cond = NULL;
5579   loc->shlib_disabled = 0;
5580   loc->enabled = 1;
5581
5582   switch (bpt->type)
5583     {
5584     case bp_breakpoint:
5585     case bp_until:
5586     case bp_finish:
5587     case bp_longjmp:
5588     case bp_longjmp_resume:
5589     case bp_exception:
5590     case bp_exception_resume:
5591     case bp_step_resume:
5592     case bp_watchpoint_scope:
5593     case bp_call_dummy:
5594     case bp_std_terminate:
5595     case bp_shlib_event:
5596     case bp_thread_event:
5597     case bp_overlay_event:
5598     case bp_jit_event:
5599     case bp_longjmp_master:
5600     case bp_std_terminate_master:
5601     case bp_exception_master:
5602       loc->loc_type = bp_loc_software_breakpoint;
5603       break;
5604     case bp_hardware_breakpoint:
5605       loc->loc_type = bp_loc_hardware_breakpoint;
5606       break;
5607     case bp_hardware_watchpoint:
5608     case bp_read_watchpoint:
5609     case bp_access_watchpoint:
5610       loc->loc_type = bp_loc_hardware_watchpoint;
5611       break;
5612     case bp_watchpoint:
5613     case bp_catchpoint:
5614     case bp_tracepoint:
5615     case bp_fast_tracepoint:
5616     case bp_static_tracepoint:
5617       loc->loc_type = bp_loc_other;
5618       break;
5619     default:
5620       internal_error (__FILE__, __LINE__, _("unknown breakpoint type"));
5621     }
5622
5623   loc->refc = 1;
5624   return loc;
5625 }
5626
5627 static void
5628 free_bp_location (struct bp_location *loc)
5629 {
5630   if (loc->cond)
5631     xfree (loc->cond);
5632
5633   if (loc->function_name)
5634     xfree (loc->function_name);
5635
5636   xfree (loc);
5637 }
5638
5639 /* Increment reference count.  */
5640
5641 static void
5642 incref_bp_location (struct bp_location *bl)
5643 {
5644   ++bl->refc;
5645 }
5646
5647 /* Decrement reference count.  If the reference count reaches 0,
5648    destroy the bp_location.  Sets *BLP to NULL.  */
5649
5650 static void
5651 decref_bp_location (struct bp_location **blp)
5652 {
5653   gdb_assert ((*blp)->refc > 0);
5654
5655   if (--(*blp)->refc == 0)
5656     free_bp_location (*blp);
5657   *blp = NULL;
5658 }
5659
5660 /* Helper to set_raw_breakpoint below.  Creates a breakpoint that has
5661    type BPTYPE and has no locations as yet.  */
5662 /* This function is used in gdbtk sources and thus can not be made
5663    static.  */
5664
5665 static struct breakpoint *
5666 set_raw_breakpoint_without_location (struct gdbarch *gdbarch,
5667                                      enum bptype bptype)
5668 {
5669   struct breakpoint *b, *b1;
5670
5671   b = (struct breakpoint *) xmalloc (sizeof (struct breakpoint));
5672   memset (b, 0, sizeof (*b));
5673
5674   b->type = bptype;
5675   b->gdbarch = gdbarch;
5676   b->language = current_language->la_language;
5677   b->input_radix = input_radix;
5678   b->thread = -1;
5679   b->enable_state = bp_enabled;
5680   b->next = 0;
5681   b->silent = 0;
5682   b->ignore_count = 0;
5683   b->commands = NULL;
5684   b->frame_id = null_frame_id;
5685   b->forked_inferior_pid = null_ptid;
5686   b->exec_pathname = NULL;
5687   b->syscalls_to_be_caught = NULL;
5688   b->ops = NULL;
5689   b->condition_not_parsed = 0;
5690   b->py_bp_object = NULL;
5691
5692   /* Add this breakpoint to the end of the chain so that a list of
5693      breakpoints will come out in order of increasing numbers.  */
5694
5695   b1 = breakpoint_chain;
5696   if (b1 == 0)
5697     breakpoint_chain = b;
5698   else
5699     {
5700       while (b1->next)
5701         b1 = b1->next;
5702       b1->next = b;
5703     }
5704   return b;
5705 }
5706
5707 /* Initialize loc->function_name.  */
5708 static void
5709 set_breakpoint_location_function (struct bp_location *loc)
5710 {
5711   gdb_assert (loc->owner != NULL);
5712
5713   if (loc->owner->type == bp_breakpoint
5714       || loc->owner->type == bp_hardware_breakpoint
5715       || is_tracepoint (loc->owner))
5716     {
5717       find_pc_partial_function (loc->address, &(loc->function_name), 
5718                                 NULL, NULL);
5719       if (loc->function_name)
5720         loc->function_name = xstrdup (loc->function_name);
5721     }
5722 }
5723
5724 /* Attempt to determine architecture of location identified by SAL.  */
5725 static struct gdbarch *
5726 get_sal_arch (struct symtab_and_line sal)
5727 {
5728   if (sal.section)
5729     return get_objfile_arch (sal.section->objfile);
5730   if (sal.symtab)
5731     return get_objfile_arch (sal.symtab->objfile);
5732
5733   return NULL;
5734 }
5735
5736 /* set_raw_breakpoint is a low level routine for allocating and
5737    partially initializing a breakpoint of type BPTYPE.  The newly
5738    created breakpoint's address, section, source file name, and line
5739    number are provided by SAL.  The newly created and partially
5740    initialized breakpoint is added to the breakpoint chain and
5741    is also returned as the value of this function.
5742
5743    It is expected that the caller will complete the initialization of
5744    the newly created breakpoint struct as well as output any status
5745    information regarding the creation of a new breakpoint.  In
5746    particular, set_raw_breakpoint does NOT set the breakpoint
5747    number!  Care should be taken to not allow an error to occur
5748    prior to completing the initialization of the breakpoint.  If this
5749    should happen, a bogus breakpoint will be left on the chain.  */
5750
5751 struct breakpoint *
5752 set_raw_breakpoint (struct gdbarch *gdbarch,
5753                     struct symtab_and_line sal, enum bptype bptype)
5754 {
5755   struct breakpoint *b = set_raw_breakpoint_without_location (gdbarch, 
5756                                                               bptype);
5757   CORE_ADDR adjusted_address;
5758   struct gdbarch *loc_gdbarch;
5759
5760   loc_gdbarch = get_sal_arch (sal);
5761   if (!loc_gdbarch)
5762     loc_gdbarch = b->gdbarch;
5763
5764   if (bptype != bp_catchpoint)
5765     gdb_assert (sal.pspace != NULL);
5766
5767   /* Adjust the breakpoint's address prior to allocating a location.
5768      Once we call allocate_bp_location(), that mostly uninitialized
5769      location will be placed on the location chain.  Adjustment of the
5770      breakpoint may cause target_read_memory() to be called and we do
5771      not want its scan of the location chain to find a breakpoint and
5772      location that's only been partially initialized.  */
5773   adjusted_address = adjust_breakpoint_address (loc_gdbarch, 
5774                                                 sal.pc, b->type);
5775
5776   b->loc = allocate_bp_location (b);
5777   b->loc->gdbarch = loc_gdbarch;
5778   b->loc->requested_address = sal.pc;
5779   b->loc->address = adjusted_address;
5780   b->loc->pspace = sal.pspace;
5781
5782   /* Store the program space that was used to set the breakpoint, for
5783      breakpoint resetting.  */
5784   b->pspace = sal.pspace;
5785
5786   if (sal.symtab == NULL)
5787     b->source_file = NULL;
5788   else
5789     b->source_file = xstrdup (sal.symtab->filename);
5790   b->loc->section = sal.section;
5791   b->line_number = sal.line;
5792
5793   set_breakpoint_location_function (b->loc);
5794
5795   breakpoints_changed ();
5796
5797   return b;
5798 }
5799
5800
5801 /* Note that the breakpoint object B describes a permanent breakpoint
5802    instruction, hard-wired into the inferior's code.  */
5803 void
5804 make_breakpoint_permanent (struct breakpoint *b)
5805 {
5806   struct bp_location *bl;
5807
5808   b->enable_state = bp_permanent;
5809
5810   /* By definition, permanent breakpoints are already present in the
5811      code.  Mark all locations as inserted.  For now,
5812      make_breakpoint_permanent is called in just one place, so it's
5813      hard to say if it's reasonable to have permanent breakpoint with
5814      multiple locations or not, but it's easy to implmement.  */
5815   for (bl = b->loc; bl; bl = bl->next)
5816     bl->inserted = 1;
5817 }
5818
5819 /* Call this routine when stepping and nexting to enable a breakpoint
5820    if we do a longjmp() or 'throw' in TP.  FRAME is the frame which
5821    initiated the operation.  */
5822
5823 void
5824 set_longjmp_breakpoint (struct thread_info *tp, struct frame_id frame)
5825 {
5826   struct breakpoint *b, *b_tmp;
5827   int thread = tp->num;
5828
5829   /* To avoid having to rescan all objfile symbols at every step,
5830      we maintain a list of continually-inserted but always disabled
5831      longjmp "master" breakpoints.  Here, we simply create momentary
5832      clones of those and enable them for the requested thread.  */
5833   ALL_BREAKPOINTS_SAFE (b, b_tmp)
5834     if (b->pspace == current_program_space
5835         && (b->type == bp_longjmp_master
5836             || b->type == bp_exception_master))
5837       {
5838         struct breakpoint *clone = clone_momentary_breakpoint (b);
5839
5840         clone->type = b->type == bp_longjmp_master ? bp_longjmp : bp_exception;
5841         clone->thread = thread;
5842       }
5843
5844   tp->initiating_frame = frame;
5845 }
5846
5847 /* Delete all longjmp breakpoints from THREAD.  */
5848 void
5849 delete_longjmp_breakpoint (int thread)
5850 {
5851   struct breakpoint *b, *b_tmp;
5852
5853   ALL_BREAKPOINTS_SAFE (b, b_tmp)
5854     if (b->type == bp_longjmp || b->type == bp_exception)
5855       {
5856         if (b->thread == thread)
5857           delete_breakpoint (b);
5858       }
5859 }
5860
5861 void
5862 enable_overlay_breakpoints (void)
5863 {
5864   struct breakpoint *b;
5865
5866   ALL_BREAKPOINTS (b)
5867     if (b->type == bp_overlay_event)
5868     {
5869       b->enable_state = bp_enabled;
5870       update_global_location_list (1);
5871       overlay_events_enabled = 1;
5872     }
5873 }
5874
5875 void
5876 disable_overlay_breakpoints (void)
5877 {
5878   struct breakpoint *b;
5879
5880   ALL_BREAKPOINTS (b)
5881     if (b->type == bp_overlay_event)
5882     {
5883       b->enable_state = bp_disabled;
5884       update_global_location_list (0);
5885       overlay_events_enabled = 0;
5886     }
5887 }
5888
5889 /* Set an active std::terminate breakpoint for each std::terminate
5890    master breakpoint.  */
5891 void
5892 set_std_terminate_breakpoint (void)
5893 {
5894   struct breakpoint *b, *b_tmp;
5895
5896   ALL_BREAKPOINTS_SAFE (b, b_tmp)
5897     if (b->pspace == current_program_space
5898         && b->type == bp_std_terminate_master)
5899       {
5900         struct breakpoint *clone = clone_momentary_breakpoint (b);
5901         clone->type = bp_std_terminate;
5902       }
5903 }
5904
5905 /* Delete all the std::terminate breakpoints.  */
5906 void
5907 delete_std_terminate_breakpoint (void)
5908 {
5909   struct breakpoint *b, *b_tmp;
5910
5911   ALL_BREAKPOINTS_SAFE (b, b_tmp)
5912     if (b->type == bp_std_terminate)
5913       delete_breakpoint (b);
5914 }
5915
5916 struct breakpoint *
5917 create_thread_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
5918 {
5919   struct breakpoint *b;
5920
5921   b = create_internal_breakpoint (gdbarch, address, bp_thread_event);
5922   
5923   b->enable_state = bp_enabled;
5924   /* addr_string has to be used or breakpoint_re_set will delete me.  */
5925   b->addr_string
5926     = xstrprintf ("*%s", paddress (b->loc->gdbarch, b->loc->address));
5927
5928   update_global_location_list_nothrow (1);
5929
5930   return b;
5931 }
5932
5933 void
5934 remove_thread_event_breakpoints (void)
5935 {
5936   struct breakpoint *b, *b_tmp;
5937
5938   ALL_BREAKPOINTS_SAFE (b, b_tmp)
5939     if (b->type == bp_thread_event
5940         && b->loc->pspace == current_program_space)
5941       delete_breakpoint (b);
5942 }
5943
5944 struct captured_parse_breakpoint_args
5945   {
5946     char **arg_p;
5947     struct symtabs_and_lines *sals_p;
5948     char ***addr_string_p;
5949     int *not_found_ptr;
5950   };
5951
5952 struct lang_and_radix
5953   {
5954     enum language lang;
5955     int radix;
5956   };
5957
5958 /* Create a breakpoint for JIT code registration and unregistration.  */
5959
5960 struct breakpoint *
5961 create_jit_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
5962 {
5963   struct breakpoint *b;
5964
5965   b = create_internal_breakpoint (gdbarch, address, bp_jit_event);
5966   update_global_location_list_nothrow (1);
5967   return b;
5968 }
5969
5970 /* Remove JIT code registration and unregistration breakpoint(s).  */
5971
5972 void
5973 remove_jit_event_breakpoints (void)
5974 {
5975   struct breakpoint *b, *b_tmp;
5976
5977   ALL_BREAKPOINTS_SAFE (b, b_tmp)
5978     if (b->type == bp_jit_event
5979         && b->loc->pspace == current_program_space)
5980       delete_breakpoint (b);
5981 }
5982
5983 void
5984 remove_solib_event_breakpoints (void)
5985 {
5986   struct breakpoint *b, *b_tmp;
5987
5988   ALL_BREAKPOINTS_SAFE (b, b_tmp)
5989     if (b->type == bp_shlib_event
5990         && b->loc->pspace == current_program_space)
5991       delete_breakpoint (b);
5992 }
5993
5994 struct breakpoint *
5995 create_solib_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
5996 {
5997   struct breakpoint *b;
5998
5999   b = create_internal_breakpoint (gdbarch, address, bp_shlib_event);
6000   update_global_location_list_nothrow (1);
6001   return b;
6002 }
6003
6004 /* Disable any breakpoints that are on code in shared libraries.  Only
6005    apply to enabled breakpoints, disabled ones can just stay disabled.  */
6006
6007 void
6008 disable_breakpoints_in_shlibs (void)
6009 {
6010   struct bp_location *loc, **locp_tmp;
6011
6012   ALL_BP_LOCATIONS (loc, locp_tmp)
6013   {
6014     /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL.  */
6015     struct breakpoint *b = loc->owner;
6016
6017     /* We apply the check to all breakpoints, including disabled for
6018        those with loc->duplicate set.  This is so that when breakpoint
6019        becomes enabled, or the duplicate is removed, gdb will try to
6020        insert all breakpoints.  If we don't set shlib_disabled here,
6021        we'll try to insert those breakpoints and fail.  */
6022     if (((b->type == bp_breakpoint)
6023          || (b->type == bp_jit_event)
6024          || (b->type == bp_hardware_breakpoint)
6025          || (is_tracepoint (b)))
6026         && loc->pspace == current_program_space
6027         && !loc->shlib_disabled
6028 #ifdef PC_SOLIB
6029         && PC_SOLIB (loc->address)
6030 #else
6031         && solib_name_from_address (loc->pspace, loc->address)
6032 #endif
6033         )
6034       {
6035         loc->shlib_disabled = 1;
6036       }
6037   }
6038 }
6039
6040 /* Disable any breakpoints that are in in an unloaded shared library.
6041    Only apply to enabled breakpoints, disabled ones can just stay
6042    disabled.  */
6043
6044 static void
6045 disable_breakpoints_in_unloaded_shlib (struct so_list *solib)
6046 {
6047   struct bp_location *loc, **locp_tmp;
6048   int disabled_shlib_breaks = 0;
6049
6050   /* SunOS a.out shared libraries are always mapped, so do not
6051      disable breakpoints; they will only be reported as unloaded
6052      through clear_solib when GDB discards its shared library
6053      list.  See clear_solib for more information.  */
6054   if (exec_bfd != NULL
6055       && bfd_get_flavour (exec_bfd) == bfd_target_aout_flavour)
6056     return;
6057
6058   ALL_BP_LOCATIONS (loc, locp_tmp)
6059   {
6060     /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL.  */
6061     struct breakpoint *b = loc->owner;
6062
6063     if ((loc->loc_type == bp_loc_hardware_breakpoint
6064          || loc->loc_type == bp_loc_software_breakpoint)
6065         && solib->pspace == loc->pspace
6066         && !loc->shlib_disabled
6067         && (b->type == bp_breakpoint
6068             || b->type == bp_jit_event
6069             || b->type == bp_hardware_breakpoint)
6070         && solib_contains_address_p (solib, loc->address))
6071       {
6072         loc->shlib_disabled = 1;
6073         /* At this point, we cannot rely on remove_breakpoint
6074            succeeding so we must mark the breakpoint as not inserted
6075            to prevent future errors occurring in remove_breakpoints.  */
6076         loc->inserted = 0;
6077         if (!disabled_shlib_breaks)
6078           {
6079             target_terminal_ours_for_output ();
6080             warning (_("Temporarily disabling breakpoints "
6081                        "for unloaded shared library \"%s\""),
6082                      solib->so_name);
6083           }
6084         disabled_shlib_breaks = 1;
6085       }
6086   }
6087 }
6088
6089 /* FORK & VFORK catchpoints.  */
6090
6091 /* Implement the "insert" breakpoint_ops method for fork
6092    catchpoints.  */
6093
6094 static int
6095 insert_catch_fork (struct bp_location *bl)
6096 {
6097   return target_insert_fork_catchpoint (PIDGET (inferior_ptid));
6098 }
6099
6100 /* Implement the "remove" breakpoint_ops method for fork
6101    catchpoints.  */
6102
6103 static int
6104 remove_catch_fork (struct bp_location *bl)
6105 {
6106   return target_remove_fork_catchpoint (PIDGET (inferior_ptid));
6107 }
6108
6109 /* Implement the "breakpoint_hit" breakpoint_ops method for fork
6110    catchpoints.  */
6111
6112 static int
6113 breakpoint_hit_catch_fork (struct breakpoint *b)
6114 {
6115   return inferior_has_forked (inferior_ptid, &b->forked_inferior_pid);
6116 }
6117
6118 /* Implement the "print_it" breakpoint_ops method for fork
6119    catchpoints.  */
6120
6121 static enum print_stop_action
6122 print_it_catch_fork (struct breakpoint *b)
6123 {
6124   annotate_catchpoint (b->number);
6125   printf_filtered (_("\nCatchpoint %d (forked process %d), "),
6126                    b->number, ptid_get_pid (b->forked_inferior_pid));
6127   return PRINT_SRC_AND_LOC;
6128 }
6129
6130 /* Implement the "print_one" breakpoint_ops method for fork
6131    catchpoints.  */
6132
6133 static void
6134 print_one_catch_fork (struct breakpoint *b, struct bp_location **last_loc)
6135 {
6136   struct value_print_options opts;
6137
6138   get_user_print_options (&opts);
6139
6140   /* Field 4, the address, is omitted (which makes the columns not
6141      line up too nicely with the headers, but the effect is relatively
6142      readable).  */
6143   if (opts.addressprint)
6144     ui_out_field_skip (uiout, "addr");
6145   annotate_field (5);
6146   ui_out_text (uiout, "fork");
6147   if (!ptid_equal (b->forked_inferior_pid, null_ptid))
6148     {
6149       ui_out_text (uiout, ", process ");
6150       ui_out_field_int (uiout, "what",
6151                         ptid_get_pid (b->forked_inferior_pid));
6152       ui_out_spaces (uiout, 1);
6153     }
6154 }
6155
6156 /* Implement the "print_mention" breakpoint_ops method for fork
6157    catchpoints.  */
6158
6159 static void
6160 print_mention_catch_fork (struct breakpoint *b)
6161 {
6162   printf_filtered (_("Catchpoint %d (fork)"), b->number);
6163 }
6164
6165 /* Implement the "print_recreate" breakpoint_ops method for fork
6166    catchpoints.  */
6167
6168 static void
6169 print_recreate_catch_fork (struct breakpoint *b, struct ui_file *fp)
6170 {
6171   fprintf_unfiltered (fp, "catch fork");
6172 }
6173
6174 /* The breakpoint_ops structure to be used in fork catchpoints.  */
6175
6176 static struct breakpoint_ops catch_fork_breakpoint_ops =
6177 {
6178   insert_catch_fork,
6179   remove_catch_fork,
6180   breakpoint_hit_catch_fork,
6181   NULL, /* resources_needed */
6182   print_it_catch_fork,
6183   print_one_catch_fork,
6184   print_mention_catch_fork,
6185   print_recreate_catch_fork
6186 };
6187
6188 /* Implement the "insert" breakpoint_ops method for vfork
6189    catchpoints.  */
6190
6191 static int
6192 insert_catch_vfork (struct bp_location *bl)
6193 {
6194   return target_insert_vfork_catchpoint (PIDGET (inferior_ptid));
6195 }
6196
6197 /* Implement the "remove" breakpoint_ops method for vfork
6198    catchpoints.  */
6199
6200 static int
6201 remove_catch_vfork (struct bp_location *bl)
6202 {
6203   return target_remove_vfork_catchpoint (PIDGET (inferior_ptid));
6204 }
6205
6206 /* Implement the "breakpoint_hit" breakpoint_ops method for vfork
6207    catchpoints.  */
6208
6209 static int
6210 breakpoint_hit_catch_vfork (struct breakpoint *b)
6211 {
6212   return inferior_has_vforked (inferior_ptid, &b->forked_inferior_pid);
6213 }
6214
6215 /* Implement the "print_it" breakpoint_ops method for vfork
6216    catchpoints.  */
6217
6218 static enum print_stop_action
6219 print_it_catch_vfork (struct breakpoint *b)
6220 {
6221   annotate_catchpoint (b->number);
6222   printf_filtered (_("\nCatchpoint %d (vforked process %d), "),
6223                    b->number, ptid_get_pid (b->forked_inferior_pid));
6224   return PRINT_SRC_AND_LOC;
6225 }
6226
6227 /* Implement the "print_one" breakpoint_ops method for vfork
6228    catchpoints.  */
6229
6230 static void
6231 print_one_catch_vfork (struct breakpoint *b, struct bp_location **last_loc)
6232 {
6233   struct value_print_options opts;
6234
6235   get_user_print_options (&opts);
6236   /* Field 4, the address, is omitted (which makes the columns not
6237      line up too nicely with the headers, but the effect is relatively
6238      readable).  */
6239   if (opts.addressprint)
6240     ui_out_field_skip (uiout, "addr");
6241   annotate_field (5);
6242   ui_out_text (uiout, "vfork");
6243   if (!ptid_equal (b->forked_inferior_pid, null_ptid))
6244     {
6245       ui_out_text (uiout, ", process ");
6246       ui_out_field_int (uiout, "what",
6247                         ptid_get_pid (b->forked_inferior_pid));
6248       ui_out_spaces (uiout, 1);
6249     }
6250 }
6251
6252 /* Implement the "print_mention" breakpoint_ops method for vfork
6253    catchpoints.  */
6254
6255 static void
6256 print_mention_catch_vfork (struct breakpoint *b)
6257 {
6258   printf_filtered (_("Catchpoint %d (vfork)"), b->number);
6259 }
6260
6261 /* Implement the "print_recreate" breakpoint_ops method for vfork
6262    catchpoints.  */
6263
6264 static void
6265 print_recreate_catch_vfork (struct breakpoint *b, struct ui_file *fp)
6266 {
6267   fprintf_unfiltered (fp, "catch vfork");
6268 }
6269
6270 /* The breakpoint_ops structure to be used in vfork catchpoints.  */
6271
6272 static struct breakpoint_ops catch_vfork_breakpoint_ops =
6273 {
6274   insert_catch_vfork,
6275   remove_catch_vfork,
6276   breakpoint_hit_catch_vfork,
6277   NULL, /* resources_needed */
6278   print_it_catch_vfork,
6279   print_one_catch_vfork,
6280   print_mention_catch_vfork,
6281   print_recreate_catch_vfork
6282 };
6283
6284 /* Implement the "insert" breakpoint_ops method for syscall
6285    catchpoints.  */
6286
6287 static int
6288 insert_catch_syscall (struct bp_location *bl)
6289 {
6290   struct inferior *inf = current_inferior ();
6291
6292   ++inf->total_syscalls_count;
6293   if (!bl->owner->syscalls_to_be_caught)
6294     ++inf->any_syscall_count;
6295   else
6296     {
6297       int i, iter;
6298
6299       for (i = 0;
6300            VEC_iterate (int, bl->owner->syscalls_to_be_caught, i, iter);
6301            i++)
6302         {
6303           int elem;
6304
6305           if (iter >= VEC_length (int, inf->syscalls_counts))
6306             {
6307               int old_size = VEC_length (int, inf->syscalls_counts);
6308               uintptr_t vec_addr_offset
6309                 = old_size * ((uintptr_t) sizeof (int));
6310               uintptr_t vec_addr;
6311               VEC_safe_grow (int, inf->syscalls_counts, iter + 1);
6312               vec_addr = (uintptr_t) VEC_address (int, inf->syscalls_counts) +
6313                 vec_addr_offset;
6314               memset ((void *) vec_addr, 0,
6315                       (iter + 1 - old_size) * sizeof (int));
6316             }
6317           elem = VEC_index (int, inf->syscalls_counts, iter);
6318           VEC_replace (int, inf->syscalls_counts, iter, ++elem);
6319         }
6320     }
6321
6322   return target_set_syscall_catchpoint (PIDGET (inferior_ptid),
6323                                         inf->total_syscalls_count != 0,
6324                                         inf->any_syscall_count,
6325                                         VEC_length (int, inf->syscalls_counts),
6326                                         VEC_address (int, inf->syscalls_counts));
6327 }
6328
6329 /* Implement the "remove" breakpoint_ops method for syscall
6330    catchpoints.  */
6331
6332 static int
6333 remove_catch_syscall (struct bp_location *bl)
6334 {
6335   struct inferior *inf = current_inferior ();
6336
6337   --inf->total_syscalls_count;
6338   if (!bl->owner->syscalls_to_be_caught)
6339     --inf->any_syscall_count;
6340   else
6341     {
6342       int i, iter;
6343
6344       for (i = 0;
6345            VEC_iterate (int, bl->owner->syscalls_to_be_caught, i, iter);
6346            i++)
6347         {
6348           int elem;
6349           if (iter >= VEC_length (int, inf->syscalls_counts))
6350             /* Shouldn't happen.  */
6351             continue;
6352           elem = VEC_index (int, inf->syscalls_counts, iter);
6353           VEC_replace (int, inf->syscalls_counts, iter, --elem);
6354         }
6355     }
6356
6357   return target_set_syscall_catchpoint (PIDGET (inferior_ptid),
6358                                         inf->total_syscalls_count != 0,
6359                                         inf->any_syscall_count,
6360                                         VEC_length (int, inf->syscalls_counts),
6361                                         VEC_address (int,
6362                                                      inf->syscalls_counts));
6363 }
6364
6365 /* Implement the "breakpoint_hit" breakpoint_ops method for syscall
6366    catchpoints.  */
6367
6368 static int
6369 breakpoint_hit_catch_syscall (struct breakpoint *b)
6370 {
6371   /* We must check if we are catching specific syscalls in this
6372      breakpoint.  If we are, then we must guarantee that the called
6373      syscall is the same syscall we are catching.  */
6374   int syscall_number = 0;
6375
6376   if (!inferior_has_called_syscall (inferior_ptid, &syscall_number))
6377     return 0;
6378
6379   /* Now, checking if the syscall is the same.  */
6380   if (b->syscalls_to_be_caught)
6381     {
6382       int i, iter;
6383
6384       for (i = 0;
6385            VEC_iterate (int, b->syscalls_to_be_caught, i, iter);
6386            i++)
6387         if (syscall_number == iter)
6388           break;
6389       /* Not the same.  */
6390       if (!iter)
6391         return 0;
6392     }
6393
6394   return 1;
6395 }
6396
6397 /* Implement the "print_it" breakpoint_ops method for syscall
6398    catchpoints.  */
6399
6400 static enum print_stop_action
6401 print_it_catch_syscall (struct breakpoint *b)
6402 {
6403   /* These are needed because we want to know in which state a
6404      syscall is.  It can be in the TARGET_WAITKIND_SYSCALL_ENTRY
6405      or TARGET_WAITKIND_SYSCALL_RETURN, and depending on it we
6406      must print "called syscall" or "returned from syscall".  */
6407   ptid_t ptid;
6408   struct target_waitstatus last;
6409   struct syscall s;
6410   struct cleanup *old_chain;
6411   char *syscall_id;
6412
6413   get_last_target_status (&ptid, &last);
6414
6415   get_syscall_by_number (last.value.syscall_number, &s);
6416
6417   annotate_catchpoint (b->number);
6418
6419   if (s.name == NULL)
6420     syscall_id = xstrprintf ("%d", last.value.syscall_number);
6421   else
6422     syscall_id = xstrprintf ("'%s'", s.name);
6423
6424   old_chain = make_cleanup (xfree, syscall_id);
6425
6426   if (last.kind == TARGET_WAITKIND_SYSCALL_ENTRY)
6427     printf_filtered (_("\nCatchpoint %d (call to syscall %s), "),
6428                      b->number, syscall_id);
6429   else if (last.kind == TARGET_WAITKIND_SYSCALL_RETURN)
6430     printf_filtered (_("\nCatchpoint %d (returned from syscall %s), "),
6431                      b->number, syscall_id);
6432
6433   do_cleanups (old_chain);
6434
6435   return PRINT_SRC_AND_LOC;
6436 }
6437
6438 /* Implement the "print_one" breakpoint_ops method for syscall
6439    catchpoints.  */
6440
6441 static void
6442 print_one_catch_syscall (struct breakpoint *b,
6443                          struct bp_location **last_loc)
6444 {
6445   struct value_print_options opts;
6446
6447   get_user_print_options (&opts);
6448   /* Field 4, the address, is omitted (which makes the columns not
6449      line up too nicely with the headers, but the effect is relatively
6450      readable).  */
6451   if (opts.addressprint)
6452     ui_out_field_skip (uiout, "addr");
6453   annotate_field (5);
6454
6455   if (b->syscalls_to_be_caught
6456       && VEC_length (int, b->syscalls_to_be_caught) > 1)
6457     ui_out_text (uiout, "syscalls \"");
6458   else
6459     ui_out_text (uiout, "syscall \"");
6460
6461   if (b->syscalls_to_be_caught)
6462     {
6463       int i, iter;
6464       char *text = xstrprintf ("%s", "");
6465
6466       for (i = 0;
6467            VEC_iterate (int, b->syscalls_to_be_caught, i, iter);
6468            i++)
6469         {
6470           char *x = text;
6471           struct syscall s;
6472           get_syscall_by_number (iter, &s);
6473
6474           if (s.name != NULL)
6475             text = xstrprintf ("%s%s, ", text, s.name);
6476           else
6477             text = xstrprintf ("%s%d, ", text, iter);
6478
6479           /* We have to xfree the last 'text' (now stored at 'x')
6480              because xstrprintf dinamically allocates new space for it
6481              on every call.  */
6482           xfree (x);
6483         }
6484       /* Remove the last comma.  */
6485       text[strlen (text) - 2] = '\0';
6486       ui_out_field_string (uiout, "what", text);
6487     }
6488   else
6489     ui_out_field_string (uiout, "what", "<any syscall>");
6490   ui_out_text (uiout, "\" ");
6491 }
6492
6493 /* Implement the "print_mention" breakpoint_ops method for syscall
6494    catchpoints.  */
6495
6496 static void
6497 print_mention_catch_syscall (struct breakpoint *b)
6498 {
6499   if (b->syscalls_to_be_caught)
6500     {
6501       int i, iter;
6502
6503       if (VEC_length (int, b->syscalls_to_be_caught) > 1)
6504         printf_filtered (_("Catchpoint %d (syscalls"), b->number);
6505       else
6506         printf_filtered (_("Catchpoint %d (syscall"), b->number);
6507
6508       for (i = 0;
6509            VEC_iterate (int, b->syscalls_to_be_caught, i, iter);
6510            i++)
6511         {
6512           struct syscall s;
6513           get_syscall_by_number (iter, &s);
6514
6515           if (s.name)
6516             printf_filtered (" '%s' [%d]", s.name, s.number);
6517           else
6518             printf_filtered (" %d", s.number);
6519         }
6520       printf_filtered (")");
6521     }
6522   else
6523     printf_filtered (_("Catchpoint %d (any syscall)"),
6524                      b->number);
6525 }
6526
6527 /* Implement the "print_recreate" breakpoint_ops method for syscall
6528    catchpoints.  */
6529
6530 static void
6531 print_recreate_catch_syscall (struct breakpoint *b, struct ui_file *fp)
6532 {
6533   fprintf_unfiltered (fp, "catch syscall");
6534
6535   if (b->syscalls_to_be_caught)
6536     {
6537       int i, iter;
6538
6539       for (i = 0;
6540            VEC_iterate (int, b->syscalls_to_be_caught, i, iter);
6541            i++)
6542         {
6543           struct syscall s;
6544
6545           get_syscall_by_number (iter, &s);
6546           if (s.name)
6547             fprintf_unfiltered (fp, " %s", s.name);
6548           else
6549             fprintf_unfiltered (fp, " %d", s.number);
6550         }
6551     }
6552 }
6553
6554 /* The breakpoint_ops structure to be used in syscall catchpoints.  */
6555
6556 static struct breakpoint_ops catch_syscall_breakpoint_ops =
6557 {
6558   insert_catch_syscall,
6559   remove_catch_syscall,
6560   breakpoint_hit_catch_syscall,
6561   NULL, /* resources_needed */
6562   print_it_catch_syscall,
6563   print_one_catch_syscall,
6564   print_mention_catch_syscall,
6565   print_recreate_catch_syscall
6566 };
6567
6568 /* Returns non-zero if 'b' is a syscall catchpoint.  */
6569
6570 static int
6571 syscall_catchpoint_p (struct breakpoint *b)
6572 {
6573   return (b->ops == &catch_syscall_breakpoint_ops);
6574 }
6575
6576 /* Create a new breakpoint of the bp_catchpoint kind and return it,
6577    but does NOT mention it nor update the global location list.
6578    This is useful if you need to fill more fields in the
6579    struct breakpoint before calling mention.
6580  
6581    If TEMPFLAG is non-zero, then make the breakpoint temporary.
6582    If COND_STRING is not NULL, then store it in the breakpoint.
6583    OPS, if not NULL, is the breakpoint_ops structure associated
6584    to the catchpoint.  */
6585
6586 static struct breakpoint *
6587 create_catchpoint_without_mention (struct gdbarch *gdbarch, int tempflag,
6588                                    char *cond_string,
6589                                    struct breakpoint_ops *ops)
6590 {
6591   struct symtab_and_line sal;
6592   struct breakpoint *b;
6593
6594   init_sal (&sal);
6595   sal.pspace = current_program_space;
6596
6597   b = set_raw_breakpoint (gdbarch, sal, bp_catchpoint);
6598   set_breakpoint_count (breakpoint_count + 1);
6599   b->number = breakpoint_count;
6600
6601   b->cond_string = (cond_string == NULL) ? NULL : xstrdup (cond_string);
6602   b->thread = -1;
6603   b->addr_string = NULL;
6604   b->enable_state = bp_enabled;
6605   b->disposition = tempflag ? disp_del : disp_donttouch;
6606   b->ops = ops;
6607
6608   return b;
6609 }
6610
6611 /* Create a new breakpoint of the bp_catchpoint kind and return it.
6612  
6613    If TEMPFLAG is non-zero, then make the breakpoint temporary.
6614    If COND_STRING is not NULL, then store it in the breakpoint.
6615    OPS, if not NULL, is the breakpoint_ops structure associated
6616    to the catchpoint.  */
6617
6618 static struct breakpoint *
6619 create_catchpoint (struct gdbarch *gdbarch, int tempflag,
6620                    char *cond_string, struct breakpoint_ops *ops)
6621 {
6622   struct breakpoint *b =
6623     create_catchpoint_without_mention (gdbarch, tempflag, cond_string, ops);
6624
6625   mention (b);
6626   update_global_location_list (1);
6627
6628   return b;
6629 }
6630
6631 static void
6632 create_fork_vfork_event_catchpoint (struct gdbarch *gdbarch,
6633                                     int tempflag, char *cond_string,
6634                                     struct breakpoint_ops *ops)
6635 {
6636   struct breakpoint *b
6637     = create_catchpoint (gdbarch, tempflag, cond_string, ops);
6638
6639   /* FIXME: We should put this information in a breakpoint private data
6640      area.  */
6641   b->forked_inferior_pid = null_ptid;
6642 }
6643
6644 /* Exec catchpoints.  */
6645
6646 static int
6647 insert_catch_exec (struct bp_location *bl)
6648 {
6649   return target_insert_exec_catchpoint (PIDGET (inferior_ptid));
6650 }
6651
6652 static int
6653 remove_catch_exec (struct bp_location *bl)
6654 {
6655   return target_remove_exec_catchpoint (PIDGET (inferior_ptid));
6656 }
6657
6658 static int
6659 breakpoint_hit_catch_exec (struct breakpoint *b)
6660 {
6661   return inferior_has_execd (inferior_ptid, &b->exec_pathname);
6662 }
6663
6664 static enum print_stop_action
6665 print_it_catch_exec (struct breakpoint *b)
6666 {
6667   annotate_catchpoint (b->number);
6668   printf_filtered (_("\nCatchpoint %d (exec'd %s), "), b->number,
6669                    b->exec_pathname);
6670   return PRINT_SRC_AND_LOC;
6671 }
6672
6673 static void
6674 print_one_catch_exec (struct breakpoint *b, struct bp_location **last_loc)
6675 {
6676   struct value_print_options opts;
6677
6678   get_user_print_options (&opts);
6679
6680   /* Field 4, the address, is omitted (which makes the columns
6681      not line up too nicely with the headers, but the effect
6682      is relatively readable).  */
6683   if (opts.addressprint)
6684     ui_out_field_skip (uiout, "addr");
6685   annotate_field (5);
6686   ui_out_text (uiout, "exec");
6687   if (b->exec_pathname != NULL)
6688     {
6689       ui_out_text (uiout, ", program \"");
6690       ui_out_field_string (uiout, "what", b->exec_pathname);
6691       ui_out_text (uiout, "\" ");
6692     }
6693 }
6694
6695 static void
6696 print_mention_catch_exec (struct breakpoint *b)
6697 {
6698   printf_filtered (_("Catchpoint %d (exec)"), b->number);
6699 }
6700
6701 /* Implement the "print_recreate" breakpoint_ops method for exec
6702    catchpoints.  */
6703
6704 static void
6705 print_recreate_catch_exec (struct breakpoint *b, struct ui_file *fp)
6706 {
6707   fprintf_unfiltered (fp, "catch exec");
6708 }
6709
6710 static struct breakpoint_ops catch_exec_breakpoint_ops =
6711 {
6712   insert_catch_exec,
6713   remove_catch_exec,
6714   breakpoint_hit_catch_exec,
6715   NULL, /* resources_needed */
6716   print_it_catch_exec,
6717   print_one_catch_exec,
6718   print_mention_catch_exec,
6719   print_recreate_catch_exec
6720 };
6721
6722 static void
6723 create_syscall_event_catchpoint (int tempflag, VEC(int) *filter,
6724                                  struct breakpoint_ops *ops)
6725 {
6726   struct gdbarch *gdbarch = get_current_arch ();
6727   struct breakpoint *b =
6728     create_catchpoint_without_mention (gdbarch, tempflag, NULL, ops);
6729
6730   b->syscalls_to_be_caught = filter;
6731
6732   /* Now, we have to mention the breakpoint and update the global
6733      location list.  */
6734   mention (b);
6735   update_global_location_list (1);
6736 }
6737
6738 static int
6739 hw_breakpoint_used_count (void)
6740 {
6741   struct breakpoint *b;
6742   int i = 0;
6743
6744   ALL_BREAKPOINTS (b)
6745   {
6746     if (b->type == bp_hardware_breakpoint && breakpoint_enabled (b))
6747       i++;
6748   }
6749
6750   return i;
6751 }
6752
6753 static int
6754 hw_watchpoint_used_count (enum bptype type, int *other_type_used)
6755 {
6756   int i = 0;
6757   struct breakpoint *b;
6758   struct bp_location *bl;
6759
6760   *other_type_used = 0;
6761   ALL_BREAKPOINTS (b)
6762     {
6763       if (!breakpoint_enabled (b))
6764         continue;
6765
6766         if (b->type == type)
6767           for (bl = b->loc; bl; bl = bl->next)
6768             {
6769               /* Special types of hardware watchpoints may use more than
6770                  one register.  */
6771               if (b->ops && b->ops->resources_needed)
6772                 i += b->ops->resources_needed (bl);
6773               else
6774                 i++;
6775             }
6776         else if (is_hardware_watchpoint (b))
6777           *other_type_used = 1;
6778     }
6779
6780   return i;
6781 }
6782
6783 void
6784 disable_watchpoints_before_interactive_call_start (void)
6785 {
6786   struct breakpoint *b;
6787
6788   ALL_BREAKPOINTS (b)
6789   {
6790     if (is_watchpoint (b) && breakpoint_enabled (b))
6791       {
6792         b->enable_state = bp_call_disabled;
6793         update_global_location_list (0);
6794       }
6795   }
6796 }
6797
6798 void
6799 enable_watchpoints_after_interactive_call_stop (void)
6800 {
6801   struct breakpoint *b;
6802
6803   ALL_BREAKPOINTS (b)
6804   {
6805     if (is_watchpoint (b) && b->enable_state == bp_call_disabled)
6806       {
6807         b->enable_state = bp_enabled;
6808         update_global_location_list (1);
6809       }
6810   }
6811 }
6812
6813 void
6814 disable_breakpoints_before_startup (void)
6815 {
6816   struct breakpoint *b;
6817   int found = 0;
6818
6819   ALL_BREAKPOINTS (b)
6820     {
6821       if (b->pspace != current_program_space)
6822         continue;
6823
6824       if ((b->type == bp_breakpoint
6825            || b->type == bp_hardware_breakpoint)
6826           && breakpoint_enabled (b))
6827         {
6828           b->enable_state = bp_startup_disabled;
6829           found = 1;
6830         }
6831     }
6832
6833   if (found)
6834     update_global_location_list (0);
6835
6836   current_program_space->executing_startup = 1;
6837 }
6838
6839 void
6840 enable_breakpoints_after_startup (void)
6841 {
6842   struct breakpoint *b;
6843   int found = 0;
6844
6845   current_program_space->executing_startup = 0;
6846
6847   ALL_BREAKPOINTS (b)
6848     {
6849       if (b->pspace != current_program_space)
6850         continue;
6851
6852       if ((b->type == bp_breakpoint
6853            || b->type == bp_hardware_breakpoint)
6854           && b->enable_state == bp_startup_disabled)
6855         {
6856           b->enable_state = bp_enabled;
6857           found = 1;
6858         }
6859     }
6860
6861   if (found)
6862     breakpoint_re_set ();
6863 }
6864
6865
6866 /* Set a breakpoint that will evaporate an end of command
6867    at address specified by SAL.
6868    Restrict it to frame FRAME if FRAME is nonzero.  */
6869
6870 struct breakpoint *
6871 set_momentary_breakpoint (struct gdbarch *gdbarch, struct symtab_and_line sal,
6872                           struct frame_id frame_id, enum bptype type)
6873 {
6874   struct breakpoint *b;
6875
6876   /* If FRAME_ID is valid, it should be a real frame, not an inlined
6877      one.  */
6878   gdb_assert (!frame_id_inlined_p (frame_id));
6879
6880   b = set_raw_breakpoint (gdbarch, sal, type);
6881   b->enable_state = bp_enabled;
6882   b->disposition = disp_donttouch;
6883   b->frame_id = frame_id;
6884
6885   /* If we're debugging a multi-threaded program, then we want
6886      momentary breakpoints to be active in only a single thread of
6887      control.  */
6888   if (in_thread_list (inferior_ptid))
6889     b->thread = pid_to_thread_id (inferior_ptid);
6890
6891   update_global_location_list_nothrow (1);
6892
6893   return b;
6894 }
6895
6896 /* Make a deep copy of momentary breakpoint ORIG.  Returns NULL if
6897    ORIG is NULL.  */
6898
6899 struct breakpoint *
6900 clone_momentary_breakpoint (struct breakpoint *orig)
6901 {
6902   struct breakpoint *copy;
6903
6904   /* If there's nothing to clone, then return nothing.  */
6905   if (orig == NULL)
6906     return NULL;
6907
6908   copy = set_raw_breakpoint_without_location (orig->gdbarch, orig->type);
6909   copy->loc = allocate_bp_location (copy);
6910   set_breakpoint_location_function (copy->loc);
6911
6912   copy->loc->gdbarch = orig->loc->gdbarch;
6913   copy->loc->requested_address = orig->loc->requested_address;
6914   copy->loc->address = orig->loc->address;
6915   copy->loc->section = orig->loc->section;
6916   copy->loc->pspace = orig->loc->pspace;
6917
6918   if (orig->source_file == NULL)
6919     copy->source_file = NULL;
6920   else
6921     copy->source_file = xstrdup (orig->source_file);
6922
6923   copy->line_number = orig->line_number;
6924   copy->frame_id = orig->frame_id;
6925   copy->thread = orig->thread;
6926   copy->pspace = orig->pspace;
6927
6928   copy->enable_state = bp_enabled;
6929   copy->disposition = disp_donttouch;
6930   copy->number = internal_breakpoint_number--;
6931
6932   update_global_location_list_nothrow (0);
6933   return copy;
6934 }
6935
6936 struct breakpoint *
6937 set_momentary_breakpoint_at_pc (struct gdbarch *gdbarch, CORE_ADDR pc,
6938                                 enum bptype type)
6939 {
6940   struct symtab_and_line sal;
6941
6942   sal = find_pc_line (pc, 0);
6943   sal.pc = pc;
6944   sal.section = find_pc_overlay (pc);
6945   sal.explicit_pc = 1;
6946
6947   return set_momentary_breakpoint (gdbarch, sal, null_frame_id, type);
6948 }
6949 \f
6950
6951 /* Tell the user we have just set a breakpoint B.  */
6952
6953 static void
6954 mention (struct breakpoint *b)
6955 {
6956   int say_where = 0;
6957   struct cleanup *ui_out_chain;
6958   struct value_print_options opts;
6959
6960   get_user_print_options (&opts);
6961
6962   /* FIXME: This is misplaced; mention() is called by things (like
6963      hitting a watchpoint) other than breakpoint creation.  It should
6964      be possible to clean this up and at the same time replace the
6965      random calls to breakpoint_changed with this hook.  */
6966   observer_notify_breakpoint_created (b->number);
6967
6968   if (b->ops != NULL && b->ops->print_mention != NULL)
6969     b->ops->print_mention (b);
6970   else
6971     switch (b->type)
6972       {
6973       case bp_none:
6974         printf_filtered (_("(apparently deleted?) Eventpoint %d: "),
6975                          b->number);
6976         break;
6977       case bp_watchpoint:
6978         ui_out_text (uiout, "Watchpoint ");
6979         ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "wpt");
6980         ui_out_field_int (uiout, "number", b->number);
6981         ui_out_text (uiout, ": ");
6982         ui_out_field_string (uiout, "exp", b->exp_string);
6983         do_cleanups (ui_out_chain);
6984         break;
6985       case bp_hardware_watchpoint:
6986         ui_out_text (uiout, "Hardware watchpoint ");
6987         ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "wpt");
6988         ui_out_field_int (uiout, "number", b->number);
6989         ui_out_text (uiout, ": ");
6990         ui_out_field_string (uiout, "exp", b->exp_string);
6991         do_cleanups (ui_out_chain);
6992         break;
6993       case bp_read_watchpoint:
6994         ui_out_text (uiout, "Hardware read watchpoint ");
6995         ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "hw-rwpt");
6996         ui_out_field_int (uiout, "number", b->number);
6997         ui_out_text (uiout, ": ");
6998         ui_out_field_string (uiout, "exp", b->exp_string);
6999         do_cleanups (ui_out_chain);
7000         break;
7001       case bp_access_watchpoint:
7002         ui_out_text (uiout, "Hardware access (read/write) watchpoint ");
7003         ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "hw-awpt");
7004         ui_out_field_int (uiout, "number", b->number);
7005         ui_out_text (uiout, ": ");
7006         ui_out_field_string (uiout, "exp", b->exp_string);
7007         do_cleanups (ui_out_chain);
7008         break;
7009       case bp_breakpoint:
7010         if (ui_out_is_mi_like_p (uiout))
7011           {
7012             say_where = 0;
7013             break;
7014           }
7015         if (b->disposition == disp_del)
7016           printf_filtered (_("Temporary breakpoint"));
7017         else
7018           printf_filtered (_("Breakpoint"));
7019         printf_filtered (_(" %d"), b->number);
7020         say_where = 1;
7021         break;
7022       case bp_hardware_breakpoint:
7023         if (ui_out_is_mi_like_p (uiout))
7024           {
7025             say_where = 0;
7026             break;
7027           }
7028         printf_filtered (_("Hardware assisted breakpoint %d"), b->number);
7029         say_where = 1;
7030         break;
7031       case bp_tracepoint:
7032         if (ui_out_is_mi_like_p (uiout))
7033           {
7034             say_where = 0;
7035             break;
7036           }
7037         printf_filtered (_("Tracepoint"));
7038         printf_filtered (_(" %d"), b->number);
7039         say_where = 1;
7040         break;
7041       case bp_fast_tracepoint:
7042         if (ui_out_is_mi_like_p (uiout))
7043           {
7044             say_where = 0;
7045             break;
7046           }
7047         printf_filtered (_("Fast tracepoint"));
7048         printf_filtered (_(" %d"), b->number);
7049         say_where = 1;
7050         break;
7051       case bp_static_tracepoint:
7052         if (ui_out_is_mi_like_p (uiout))
7053           {
7054             say_where = 0;
7055             break;
7056           }
7057         printf_filtered (_("Static tracepoint"));
7058         printf_filtered (_(" %d"), b->number);
7059         say_where = 1;
7060         break;
7061
7062       case bp_until:
7063       case bp_finish:
7064       case bp_longjmp:
7065       case bp_longjmp_resume:
7066       case bp_exception:
7067       case bp_exception_resume:
7068       case bp_step_resume:
7069       case bp_call_dummy:
7070       case bp_std_terminate:
7071       case bp_watchpoint_scope:
7072       case bp_shlib_event:
7073       case bp_thread_event:
7074       case bp_overlay_event:
7075       case bp_jit_event:
7076       case bp_longjmp_master:
7077       case bp_std_terminate_master:
7078       case bp_exception_master:
7079         break;
7080       }
7081
7082   if (say_where)
7083     {
7084       /* i18n: cagney/2005-02-11: Below needs to be merged into a
7085          single string.  */
7086       if (b->loc == NULL)
7087         {
7088           printf_filtered (_(" (%s) pending."), b->addr_string);
7089         }
7090       else
7091         {
7092           if (opts.addressprint || b->source_file == NULL)
7093             {
7094               printf_filtered (" at ");
7095               fputs_filtered (paddress (b->loc->gdbarch, b->loc->address),
7096                               gdb_stdout);
7097             }
7098           if (b->source_file)
7099             printf_filtered (": file %s, line %d.",
7100                              b->source_file, b->line_number);
7101           
7102           if (b->loc->next)
7103             {
7104               struct bp_location *loc = b->loc;
7105               int n = 0;
7106               for (; loc; loc = loc->next)
7107                 ++n;
7108               printf_filtered (" (%d locations)", n);           
7109             }
7110
7111         }
7112     }
7113   if (ui_out_is_mi_like_p (uiout))
7114     return;
7115   printf_filtered ("\n");
7116 }
7117 \f
7118
7119 static struct bp_location *
7120 add_location_to_breakpoint (struct breakpoint *b,
7121                             const struct symtab_and_line *sal)
7122 {
7123   struct bp_location *loc, **tmp;
7124
7125   loc = allocate_bp_location (b);
7126   for (tmp = &(b->loc); *tmp != NULL; tmp = &((*tmp)->next))
7127     ;
7128   *tmp = loc;
7129   loc->gdbarch = get_sal_arch (*sal);
7130   if (!loc->gdbarch)
7131     loc->gdbarch = b->gdbarch;
7132   loc->requested_address = sal->pc;
7133   loc->address = adjust_breakpoint_address (loc->gdbarch,
7134                                             loc->requested_address, b->type);
7135   loc->pspace = sal->pspace;
7136   gdb_assert (loc->pspace != NULL);
7137   loc->section = sal->section;
7138
7139   set_breakpoint_location_function (loc);
7140   return loc;
7141 }
7142 \f
7143
7144 /* Return 1 if LOC is pointing to a permanent breakpoint, 
7145    return 0 otherwise.  */
7146
7147 static int
7148 bp_loc_is_permanent (struct bp_location *loc)
7149 {
7150   int len;
7151   CORE_ADDR addr;
7152   const gdb_byte *brk;
7153   gdb_byte *target_mem;
7154   struct cleanup *cleanup;
7155   int retval = 0;
7156
7157   gdb_assert (loc != NULL);
7158
7159   addr = loc->address;
7160   brk = gdbarch_breakpoint_from_pc (loc->gdbarch, &addr, &len);
7161
7162   /* Software breakpoints unsupported?  */
7163   if (brk == NULL)
7164     return 0;
7165
7166   target_mem = alloca (len);
7167
7168   /* Enable the automatic memory restoration from breakpoints while
7169      we read the memory.  Otherwise we could say about our temporary
7170      breakpoints they are permanent.  */
7171   cleanup = save_current_space_and_thread ();
7172
7173   switch_to_program_space_and_thread (loc->pspace);
7174   make_show_memory_breakpoints_cleanup (0);
7175
7176   if (target_read_memory (loc->address, target_mem, len) == 0
7177       && memcmp (target_mem, brk, len) == 0)
7178     retval = 1;
7179
7180   do_cleanups (cleanup);
7181
7182   return retval;
7183 }
7184
7185
7186
7187 /* Create a breakpoint with SAL as location.  Use ADDR_STRING
7188    as textual description of the location, and COND_STRING
7189    as condition expression.  */
7190
7191 static void
7192 create_breakpoint_sal (struct gdbarch *gdbarch,
7193                        struct symtabs_and_lines sals, char *addr_string,
7194                        char *cond_string,
7195                        enum bptype type, enum bpdisp disposition,
7196                        int thread, int task, int ignore_count,
7197                        struct breakpoint_ops *ops, int from_tty,
7198                        int enabled, int internal)
7199 {
7200   struct breakpoint *b = NULL;
7201   int i;
7202
7203   if (type == bp_hardware_breakpoint)
7204     {
7205       int i = hw_breakpoint_used_count ();
7206       int target_resources_ok = 
7207         target_can_use_hardware_watchpoint (bp_hardware_breakpoint, 
7208                                             i + 1, 0);
7209       if (target_resources_ok == 0)
7210         error (_("No hardware breakpoint support in the target."));
7211       else if (target_resources_ok < 0)
7212         error (_("Hardware breakpoints used exceeds limit."));
7213     }
7214
7215   gdb_assert (sals.nelts > 0);
7216
7217   for (i = 0; i < sals.nelts; ++i)
7218     {
7219       struct symtab_and_line sal = sals.sals[i];
7220       struct bp_location *loc;
7221
7222       if (from_tty)
7223         {
7224           struct gdbarch *loc_gdbarch = get_sal_arch (sal);
7225           if (!loc_gdbarch)
7226             loc_gdbarch = gdbarch;
7227
7228           describe_other_breakpoints (loc_gdbarch,
7229                                       sal.pspace, sal.pc, sal.section, thread);
7230         }
7231
7232       if (i == 0)
7233         {
7234           b = set_raw_breakpoint (gdbarch, sal, type);
7235           set_breakpoint_number (internal, b);
7236           b->thread = thread;
7237           b->task = task;
7238   
7239           b->cond_string = cond_string;
7240           b->ignore_count = ignore_count;
7241           b->enable_state = enabled ? bp_enabled : bp_disabled;
7242           b->disposition = disposition;
7243           b->pspace = sals.sals[0].pspace;
7244
7245           if (type == bp_static_tracepoint)
7246             {
7247               struct static_tracepoint_marker marker;
7248
7249               if (is_marker_spec (addr_string))
7250                 {
7251                   /* We already know the marker exists, otherwise, we
7252                      wouldn't see a sal for it.  */
7253                   char *p = &addr_string[3];
7254                   char *endp;
7255                   char *marker_str;
7256                   int i;
7257
7258                   p = skip_spaces (p);
7259
7260                   endp = skip_to_space (p);
7261
7262                   marker_str = savestring (p, endp - p);
7263                   b->static_trace_marker_id = marker_str;
7264
7265                   printf_filtered (_("Probed static tracepoint "
7266                                      "marker \"%s\"\n"),
7267                                    b->static_trace_marker_id);
7268                 }
7269               else if (target_static_tracepoint_marker_at (sal.pc, &marker))
7270                 {
7271                   b->static_trace_marker_id = xstrdup (marker.str_id);
7272                   release_static_tracepoint_marker (&marker);
7273
7274                   printf_filtered (_("Probed static tracepoint "
7275                                      "marker \"%s\"\n"),
7276                                    b->static_trace_marker_id);
7277                 }
7278               else
7279                 warning (_("Couldn't determine the static "
7280                            "tracepoint marker to probe"));
7281             }
7282
7283           if (enabled && b->pspace->executing_startup
7284               && (b->type == bp_breakpoint
7285                   || b->type == bp_hardware_breakpoint))
7286             b->enable_state = bp_startup_disabled;
7287
7288           loc = b->loc;
7289         }
7290       else
7291         {
7292           loc = add_location_to_breakpoint (b, &sal);
7293         }
7294
7295       if (bp_loc_is_permanent (loc))
7296         make_breakpoint_permanent (b);
7297
7298       if (b->cond_string)
7299         {
7300           char *arg = b->cond_string;
7301           loc->cond = parse_exp_1 (&arg, block_for_pc (loc->address), 0);
7302           if (*arg)
7303               error (_("Garbage %s follows condition"), arg);
7304         }
7305     }   
7306
7307   if (addr_string)
7308     b->addr_string = addr_string;
7309   else
7310     /* addr_string has to be used or breakpoint_re_set will delete
7311        me.  */
7312     b->addr_string
7313       = xstrprintf ("*%s", paddress (b->loc->gdbarch, b->loc->address));
7314
7315   b->ops = ops;
7316   if (internal)
7317     /* Do not mention breakpoints with a negative number, but do
7318        notify observers.  */
7319     observer_notify_breakpoint_created (b->number);
7320   else
7321     mention (b);
7322 }
7323
7324 /* Remove element at INDEX_TO_REMOVE from SAL, shifting other
7325    elements to fill the void space.  */
7326 static void
7327 remove_sal (struct symtabs_and_lines *sal, int index_to_remove)
7328 {
7329   int i = index_to_remove+1;
7330   int last_index = sal->nelts-1;
7331
7332   for (;i <= last_index; ++i)
7333     sal->sals[i-1] = sal->sals[i];
7334
7335   --(sal->nelts);
7336 }
7337
7338 /* If appropriate, obtains all sals that correspond to the same file
7339    and line as SAL, in all program spaces.  Users debugging with IDEs,
7340    will want to set a breakpoint at foo.c:line, and not really care
7341    about program spaces.  This is done only if SAL does not have
7342    explicit PC and has line and file information.  If we got just a
7343    single expanded sal, return the original.
7344
7345    Otherwise, if SAL.explicit_line is not set, filter out all sals for
7346    which the name of enclosing function is different from SAL.  This
7347    makes sure that if we have breakpoint originally set in template
7348    instantiation, say foo<int>(), we won't expand SAL to locations at
7349    the same line in all existing instantiations of 'foo'.  */
7350
7351 static struct symtabs_and_lines
7352 expand_line_sal_maybe (struct symtab_and_line sal)
7353 {
7354   struct symtabs_and_lines expanded;
7355   CORE_ADDR original_pc = sal.pc;
7356   char *original_function = NULL;
7357   int found;
7358   int i;
7359   struct cleanup *old_chain;
7360
7361   /* If we have explicit pc, don't expand.
7362      If we have no line number, we can't expand.  */
7363   if (sal.explicit_pc || sal.line == 0 || sal.symtab == NULL)
7364     {
7365       expanded.nelts = 1;
7366       expanded.sals = xmalloc (sizeof (struct symtab_and_line));
7367       expanded.sals[0] = sal;
7368       return expanded;
7369     }
7370
7371   sal.pc = 0;
7372
7373   old_chain = save_current_space_and_thread ();
7374
7375   switch_to_program_space_and_thread (sal.pspace);
7376
7377   find_pc_partial_function (original_pc, &original_function, NULL, NULL);
7378
7379   /* Note that expand_line_sal visits *all* program spaces.  */
7380   expanded = expand_line_sal (sal);
7381
7382   if (expanded.nelts == 1)
7383     {
7384       /* We had one sal, we got one sal.  Return that sal, adjusting it
7385          past the function prologue if necessary.  */
7386       xfree (expanded.sals);
7387       expanded.nelts = 1;
7388       expanded.sals = xmalloc (sizeof (struct symtab_and_line));
7389       sal.pc = original_pc;
7390       expanded.sals[0] = sal;
7391       skip_prologue_sal (&expanded.sals[0]);
7392       do_cleanups (old_chain);
7393       return expanded;      
7394     }
7395
7396   if (!sal.explicit_line)
7397     {
7398       CORE_ADDR func_addr, func_end;
7399       for (i = 0; i < expanded.nelts; ++i)
7400         {
7401           CORE_ADDR pc = expanded.sals[i].pc;
7402           char *this_function;
7403
7404           /* We need to switch threads as well since we're about to
7405              read memory.  */
7406           switch_to_program_space_and_thread (expanded.sals[i].pspace);
7407
7408           if (find_pc_partial_function (pc, &this_function, 
7409                                         &func_addr, &func_end))
7410             {
7411               if (this_function
7412                   && strcmp (this_function, original_function) != 0)
7413                 {
7414                   remove_sal (&expanded, i);
7415                   --i;
7416                 }
7417             }
7418         }
7419     }
7420
7421   /* Skip the function prologue if necessary.  */
7422   for (i = 0; i < expanded.nelts; ++i)
7423     skip_prologue_sal (&expanded.sals[i]);
7424
7425   do_cleanups (old_chain);
7426
7427   if (expanded.nelts <= 1)
7428     {
7429       /* This is un ugly workaround.  If we get zero expanded sals
7430          then something is really wrong.  Fix that by returning the
7431          original sal.  */
7432
7433       xfree (expanded.sals);
7434       expanded.nelts = 1;
7435       expanded.sals = xmalloc (sizeof (struct symtab_and_line));
7436       sal.pc = original_pc;
7437       expanded.sals[0] = sal;
7438       return expanded;      
7439     }
7440
7441   if (original_pc)
7442     {
7443       found = 0;
7444       for (i = 0; i < expanded.nelts; ++i)
7445         if (expanded.sals[i].pc == original_pc)
7446           {
7447             found = 1;
7448             break;
7449           }
7450       gdb_assert (found);
7451     }
7452
7453   return expanded;
7454 }
7455
7456 /* Add SALS.nelts breakpoints to the breakpoint table.  For each
7457    SALS.sal[i] breakpoint, include the corresponding ADDR_STRING[i]
7458    value.  COND_STRING, if not NULL, specified the condition to be
7459    used for all breakpoints.  Essentially the only case where
7460    SALS.nelts is not 1 is when we set a breakpoint on an overloaded
7461    function.  In that case, it's still not possible to specify
7462    separate conditions for different overloaded functions, so
7463    we take just a single condition string.
7464    
7465    NOTE: If the function succeeds, the caller is expected to cleanup
7466    the arrays ADDR_STRING, COND_STRING, and SALS (but not the
7467    array contents).  If the function fails (error() is called), the
7468    caller is expected to cleanups both the ADDR_STRING, COND_STRING,
7469    COND and SALS arrays and each of those arrays contents.  */
7470
7471 static void
7472 create_breakpoints_sal (struct gdbarch *gdbarch,
7473                         struct symtabs_and_lines sals, char **addr_string,
7474                         char *cond_string,
7475                         enum bptype type, enum bpdisp disposition,
7476                         int thread, int task, int ignore_count,
7477                         struct breakpoint_ops *ops, int from_tty,
7478                         int enabled, int internal)
7479 {
7480   int i;
7481
7482   for (i = 0; i < sals.nelts; ++i)
7483     {
7484       struct symtabs_and_lines expanded = 
7485         expand_line_sal_maybe (sals.sals[i]);
7486
7487       create_breakpoint_sal (gdbarch, expanded, addr_string[i],
7488                              cond_string, type, disposition,
7489                              thread, task, ignore_count, ops,
7490                              from_tty, enabled, internal);
7491     }
7492 }
7493
7494 /* Parse ADDRESS which is assumed to be a SAL specification possibly
7495    followed by conditionals.  On return, SALS contains an array of SAL
7496    addresses found.  ADDR_STRING contains a vector of (canonical)
7497    address strings.  ADDRESS points to the end of the SAL.
7498
7499    The array and the line spec strings are allocated on the heap, it is
7500    the caller's responsibility to free them.  */
7501
7502 static void
7503 parse_breakpoint_sals (char **address,
7504                        struct symtabs_and_lines *sals,
7505                        char ***addr_string,
7506                        int *not_found_ptr)
7507 {
7508   char *addr_start = *address;
7509
7510   *addr_string = NULL;
7511   /* If no arg given, or if first arg is 'if ', use the default
7512      breakpoint.  */
7513   if ((*address) == NULL
7514       || (strncmp ((*address), "if", 2) == 0 && isspace ((*address)[2])))
7515     {
7516       if (default_breakpoint_valid)
7517         {
7518           struct symtab_and_line sal;
7519
7520           init_sal (&sal);              /* Initialize to zeroes.  */
7521           sals->sals = (struct symtab_and_line *)
7522             xmalloc (sizeof (struct symtab_and_line));
7523           sal.pc = default_breakpoint_address;
7524           sal.line = default_breakpoint_line;
7525           sal.symtab = default_breakpoint_symtab;
7526           sal.pspace = default_breakpoint_pspace;
7527           sal.section = find_pc_overlay (sal.pc);
7528
7529           /* "break" without arguments is equivalent to "break *PC"
7530              where PC is the default_breakpoint_address.  So make sure
7531              to set sal.explicit_pc to prevent GDB from trying to
7532              expand the list of sals to include all other instances
7533              with the same symtab and line.  */
7534           sal.explicit_pc = 1;
7535
7536           sals->sals[0] = sal;
7537           sals->nelts = 1;
7538         }
7539       else
7540         error (_("No default breakpoint address now."));
7541     }
7542   else
7543     {
7544       /* Force almost all breakpoints to be in terms of the
7545          current_source_symtab (which is decode_line_1's default).
7546          This should produce the results we want almost all of the
7547          time while leaving default_breakpoint_* alone.
7548
7549          ObjC: However, don't match an Objective-C method name which
7550          may have a '+' or '-' succeeded by a '[' */
7551          
7552       struct symtab_and_line cursal = get_current_source_symtab_and_line ();
7553                         
7554       if (default_breakpoint_valid
7555           && (!cursal.symtab
7556               || ((strchr ("+-", (*address)[0]) != NULL)
7557                   && ((*address)[1] != '['))))
7558         *sals = decode_line_1 (address, 1, default_breakpoint_symtab,
7559                                default_breakpoint_line, addr_string, 
7560                                not_found_ptr);
7561       else
7562         *sals = decode_line_1 (address, 1, (struct symtab *) NULL, 0,
7563                                addr_string, not_found_ptr);
7564     }
7565   /* For any SAL that didn't have a canonical string, fill one in.  */
7566   if (sals->nelts > 0 && *addr_string == NULL)
7567     *addr_string = xcalloc (sals->nelts, sizeof (char **));
7568   if (addr_start != (*address))
7569     {
7570       int i;
7571
7572       for (i = 0; i < sals->nelts; i++)
7573         {
7574           /* Add the string if not present.  */
7575           if ((*addr_string)[i] == NULL)
7576             (*addr_string)[i] = savestring (addr_start, 
7577                                             (*address) - addr_start);
7578         }
7579     }
7580 }
7581
7582
7583 /* Convert each SAL into a real PC.  Verify that the PC can be
7584    inserted as a breakpoint.  If it can't throw an error.  */
7585
7586 static void
7587 breakpoint_sals_to_pc (struct symtabs_and_lines *sals)
7588 {    
7589   int i;
7590
7591   for (i = 0; i < sals->nelts; i++)
7592     resolve_sal_pc (&sals->sals[i]);
7593 }
7594
7595 /* Fast tracepoints may have restrictions on valid locations.  For
7596    instance, a fast tracepoint using a jump instead of a trap will
7597    likely have to overwrite more bytes than a trap would, and so can
7598    only be placed where the instruction is longer than the jump, or a
7599    multi-instruction sequence does not have a jump into the middle of
7600    it, etc.  */
7601
7602 static void
7603 check_fast_tracepoint_sals (struct gdbarch *gdbarch,
7604                             struct symtabs_and_lines *sals)
7605 {
7606   int i, rslt;
7607   struct symtab_and_line *sal;
7608   char *msg;
7609   struct cleanup *old_chain;
7610
7611   for (i = 0; i < sals->nelts; i++)
7612     {
7613       sal = &sals->sals[i];
7614
7615       rslt = gdbarch_fast_tracepoint_valid_at (gdbarch, sal->pc,
7616                                                NULL, &msg);
7617       old_chain = make_cleanup (xfree, msg);
7618
7619       if (!rslt)
7620         error (_("May not have a fast tracepoint at 0x%s%s"),
7621                paddress (gdbarch, sal->pc), (msg ? msg : ""));
7622
7623       do_cleanups (old_chain);
7624     }
7625 }
7626
7627 static void
7628 do_captured_parse_breakpoint (struct ui_out *ui, void *data)
7629 {
7630   struct captured_parse_breakpoint_args *args = data;
7631   
7632   parse_breakpoint_sals (args->arg_p, args->sals_p, args->addr_string_p, 
7633                          args->not_found_ptr);
7634 }
7635
7636 /* Given TOK, a string specification of condition and thread, as
7637    accepted by the 'break' command, extract the condition
7638    string and thread number and set *COND_STRING and *THREAD.
7639    PC identifies the context at which the condition should be parsed.
7640    If no condition is found, *COND_STRING is set to NULL.
7641    If no thread is found, *THREAD is set to -1.  */
7642 static void 
7643 find_condition_and_thread (char *tok, CORE_ADDR pc, 
7644                            char **cond_string, int *thread, int *task)
7645 {
7646   *cond_string = NULL;
7647   *thread = -1;
7648   while (tok && *tok)
7649     {
7650       char *end_tok;
7651       int toklen;
7652       char *cond_start = NULL;
7653       char *cond_end = NULL;
7654
7655       tok = skip_spaces (tok);
7656       
7657       end_tok = skip_to_space (tok);
7658       
7659       toklen = end_tok - tok;
7660       
7661       if (toklen >= 1 && strncmp (tok, "if", toklen) == 0)
7662         {
7663           struct expression *expr;
7664
7665           tok = cond_start = end_tok + 1;
7666           expr = parse_exp_1 (&tok, block_for_pc (pc), 0);
7667           xfree (expr);
7668           cond_end = tok;
7669           *cond_string = savestring (cond_start, 
7670                                      cond_end - cond_start);
7671         }
7672       else if (toklen >= 1 && strncmp (tok, "thread", toklen) == 0)
7673         {
7674           char *tmptok;
7675           
7676           tok = end_tok + 1;
7677           tmptok = tok;
7678           *thread = strtol (tok, &tok, 0);
7679           if (tok == tmptok)
7680             error (_("Junk after thread keyword."));
7681           if (!valid_thread_id (*thread))
7682             error (_("Unknown thread %d."), *thread);
7683         }
7684       else if (toklen >= 1 && strncmp (tok, "task", toklen) == 0)
7685         {
7686           char *tmptok;
7687
7688           tok = end_tok + 1;
7689           tmptok = tok;
7690           *task = strtol (tok, &tok, 0);
7691           if (tok == tmptok)
7692             error (_("Junk after task keyword."));
7693           if (!valid_task_id (*task))
7694             error (_("Unknown task %d."), *task);
7695         }
7696       else
7697         error (_("Junk at end of arguments."));
7698     }
7699 }
7700
7701 /* Decode a static tracepoint marker spec.  */
7702
7703 static struct symtabs_and_lines
7704 decode_static_tracepoint_spec (char **arg_p)
7705 {
7706   VEC(static_tracepoint_marker_p) *markers = NULL;
7707   struct symtabs_and_lines sals;
7708   struct symtab_and_line sal;
7709   struct symbol *sym;
7710   struct cleanup *old_chain;
7711   char *p = &(*arg_p)[3];
7712   char *endp;
7713   char *marker_str;
7714   int i;
7715
7716   p = skip_spaces (p);
7717
7718   endp = skip_to_space (p);
7719
7720   marker_str = savestring (p, endp - p);
7721   old_chain = make_cleanup (xfree, marker_str);
7722
7723   markers = target_static_tracepoint_markers_by_strid (marker_str);
7724   if (VEC_empty(static_tracepoint_marker_p, markers))
7725     error (_("No known static tracepoint marker named %s"), marker_str);
7726
7727   sals.nelts = VEC_length(static_tracepoint_marker_p, markers);
7728   sals.sals = xmalloc (sizeof *sals.sals * sals.nelts);
7729
7730   for (i = 0; i < sals.nelts; i++)
7731     {
7732       struct static_tracepoint_marker *marker;
7733
7734       marker = VEC_index (static_tracepoint_marker_p, markers, i);
7735
7736       init_sal (&sals.sals[i]);
7737
7738       sals.sals[i] = find_pc_line (marker->address, 0);
7739       sals.sals[i].pc = marker->address;
7740
7741       release_static_tracepoint_marker (marker);
7742     }
7743
7744   do_cleanups (old_chain);
7745
7746   *arg_p = endp;
7747   return sals;
7748 }
7749
7750 /* Set a breakpoint.  This function is shared between CLI and MI
7751    functions for setting a breakpoint.  This function has two major
7752    modes of operations, selected by the PARSE_CONDITION_AND_THREAD
7753    parameter.  If non-zero, the function will parse arg, extracting
7754    breakpoint location, address and thread.  Otherwise, ARG is just
7755    the location of breakpoint, with condition and thread specified by
7756    the COND_STRING and THREAD parameters.  If INTERNAL is non-zero,
7757    the breakpoint number will be allocated from the internal
7758    breakpoint count.  Returns true if any breakpoint was created;
7759    false otherwise.  */
7760
7761 int
7762 create_breakpoint (struct gdbarch *gdbarch,
7763                    char *arg, char *cond_string, int thread,
7764                    int parse_condition_and_thread,
7765                    int tempflag, enum bptype type_wanted,
7766                    int ignore_count,
7767                    enum auto_boolean pending_break_support,
7768                    struct breakpoint_ops *ops,
7769                    int from_tty, int enabled, int internal)
7770 {
7771   struct gdb_exception e;
7772   struct symtabs_and_lines sals;
7773   struct symtab_and_line pending_sal;
7774   char *copy_arg;
7775   char *addr_start = arg;
7776   char **addr_string;
7777   struct cleanup *old_chain;
7778   struct cleanup *bkpt_chain = NULL;
7779   struct captured_parse_breakpoint_args parse_args;
7780   int i;
7781   int pending = 0;
7782   int not_found = 0;
7783   int task = 0;
7784   int prev_bkpt_count = breakpoint_count;
7785
7786   sals.sals = NULL;
7787   sals.nelts = 0;
7788   addr_string = NULL;
7789
7790   parse_args.arg_p = &arg;
7791   parse_args.sals_p = &sals;
7792   parse_args.addr_string_p = &addr_string;
7793   parse_args.not_found_ptr = &not_found;
7794
7795   if (type_wanted == bp_static_tracepoint && is_marker_spec (arg))
7796     {
7797       int i;
7798
7799       sals = decode_static_tracepoint_spec (&arg);
7800
7801       copy_arg = savestring (addr_start, arg - addr_start);
7802       addr_string = xcalloc (sals.nelts, sizeof (char **));
7803       for (i = 0; i < sals.nelts; i++)
7804         addr_string[i] = xstrdup (copy_arg);
7805       goto done;
7806     }
7807
7808   e = catch_exception (uiout, do_captured_parse_breakpoint, 
7809                        &parse_args, RETURN_MASK_ALL);
7810
7811   /* If caller is interested in rc value from parse, set value.  */
7812   switch (e.reason)
7813     {
7814     case RETURN_QUIT:
7815       throw_exception (e);
7816     case RETURN_ERROR:
7817       switch (e.error)
7818         {
7819         case NOT_FOUND_ERROR:
7820
7821           /* If pending breakpoint support is turned off, throw
7822              error.  */
7823
7824           if (pending_break_support == AUTO_BOOLEAN_FALSE)
7825             throw_exception (e);
7826
7827           exception_print (gdb_stderr, e);
7828
7829           /* If pending breakpoint support is auto query and the user
7830              selects no, then simply return the error code.  */
7831           if (pending_break_support == AUTO_BOOLEAN_AUTO
7832               && !nquery (_("Make breakpoint pending on "
7833                             "future shared library load? ")))
7834             return 0;
7835
7836           /* At this point, either the user was queried about setting
7837              a pending breakpoint and selected yes, or pending
7838              breakpoint behavior is on and thus a pending breakpoint
7839              is defaulted on behalf of the user.  */
7840           copy_arg = xstrdup (addr_start);
7841           addr_string = &copy_arg;
7842           sals.nelts = 1;
7843           sals.sals = &pending_sal;
7844           pending_sal.pc = 0;
7845           pending = 1;
7846           break;
7847         default:
7848           throw_exception (e);
7849         }
7850     default:
7851       if (!sals.nelts)
7852         return 0;
7853     }
7854
7855   done:
7856
7857   /* Create a chain of things that always need to be cleaned up.  */
7858   old_chain = make_cleanup (null_cleanup, 0);
7859
7860   if (!pending)
7861     {
7862       /* Make sure that all storage allocated to SALS gets freed.  */
7863       make_cleanup (xfree, sals.sals);
7864       
7865       /* Cleanup the addr_string array but not its contents.  */
7866       make_cleanup (xfree, addr_string);
7867     }
7868
7869   /* ----------------------------- SNIP -----------------------------
7870      Anything added to the cleanup chain beyond this point is assumed
7871      to be part of a breakpoint.  If the breakpoint create succeeds
7872      then the memory is not reclaimed.  */
7873   bkpt_chain = make_cleanup (null_cleanup, 0);
7874
7875   /* Mark the contents of the addr_string for cleanup.  These go on
7876      the bkpt_chain and only occur if the breakpoint create fails.  */
7877   for (i = 0; i < sals.nelts; i++)
7878     {
7879       if (addr_string[i] != NULL)
7880         make_cleanup (xfree, addr_string[i]);
7881     }
7882
7883   /* Resolve all line numbers to PC's and verify that the addresses
7884      are ok for the target.  */
7885   if (!pending)
7886     breakpoint_sals_to_pc (&sals);
7887
7888   /* Fast tracepoints may have additional restrictions on location.  */
7889   if (type_wanted == bp_fast_tracepoint)
7890     check_fast_tracepoint_sals (gdbarch, &sals);
7891
7892   /* Verify that condition can be parsed, before setting any
7893      breakpoints.  Allocate a separate condition expression for each
7894      breakpoint.  */
7895   if (!pending)
7896     {
7897       if (parse_condition_and_thread)
7898         {
7899             /* Here we only parse 'arg' to separate condition
7900                from thread number, so parsing in context of first
7901                sal is OK.  When setting the breakpoint we'll 
7902                re-parse it in context of each sal.  */
7903             cond_string = NULL;
7904             thread = -1;
7905             find_condition_and_thread (arg, sals.sals[0].pc, &cond_string,
7906                                        &thread, &task);
7907             if (cond_string)
7908                 make_cleanup (xfree, cond_string);
7909         }
7910       else
7911         {
7912             /* Create a private copy of condition string.  */
7913             if (cond_string)
7914             {
7915                 cond_string = xstrdup (cond_string);
7916                 make_cleanup (xfree, cond_string);
7917             }
7918         }
7919
7920       /* If the user is creating a static tracepoint by marker id
7921          (strace -m MARKER_ID), then store the sals index, so that
7922          breakpoint_re_set can try to match up which of the newly
7923          found markers corresponds to this one, and, don't try to
7924          expand multiple locations for each sal, given than SALS
7925          already should contain all sals for MARKER_ID.  */
7926       if (type_wanted == bp_static_tracepoint
7927           && is_marker_spec (addr_string[0]))
7928         {
7929           int i;
7930
7931           for (i = 0; i < sals.nelts; ++i)
7932             {
7933               struct symtabs_and_lines expanded;
7934               struct breakpoint *tp;
7935               struct cleanup *old_chain;
7936
7937               expanded.nelts = 1;
7938               expanded.sals = xmalloc (sizeof (struct symtab_and_line));
7939               expanded.sals[0] = sals.sals[i];
7940               old_chain = make_cleanup (xfree, expanded.sals);
7941
7942               create_breakpoint_sal (gdbarch, expanded, addr_string[i],
7943                                      cond_string, type_wanted,
7944                                      tempflag ? disp_del : disp_donttouch,
7945                                      thread, task, ignore_count, ops,
7946                                      from_tty, enabled, internal);
7947
7948               do_cleanups (old_chain);
7949
7950               /* Get the tracepoint we just created.  */
7951               if (internal)
7952                 tp = get_breakpoint (internal_breakpoint_number);
7953               else
7954                 tp = get_breakpoint (breakpoint_count);
7955               gdb_assert (tp != NULL);
7956
7957               /* Given that its possible to have multiple markers with
7958                  the same string id, if the user is creating a static
7959                  tracepoint by marker id ("strace -m MARKER_ID"), then
7960                  store the sals index, so that breakpoint_re_set can
7961                  try to match up which of the newly found markers
7962                  corresponds to this one  */
7963               tp->static_trace_marker_id_idx = i;
7964             }
7965         }
7966       else
7967         create_breakpoints_sal (gdbarch, sals, addr_string, cond_string,
7968                                 type_wanted,
7969                                 tempflag ? disp_del : disp_donttouch,
7970                                 thread, task, ignore_count, ops, from_tty,
7971                                 enabled, internal);
7972     }
7973   else
7974     {
7975       struct breakpoint *b;
7976
7977       make_cleanup (xfree, copy_arg);
7978
7979       b = set_raw_breakpoint_without_location (gdbarch, type_wanted);
7980       set_breakpoint_number (internal, b);
7981       b->thread = -1;
7982       b->addr_string = addr_string[0];
7983       b->cond_string = NULL;
7984       b->ignore_count = ignore_count;
7985       b->disposition = tempflag ? disp_del : disp_donttouch;
7986       b->condition_not_parsed = 1;
7987       b->ops = ops;
7988       b->enable_state = enabled ? bp_enabled : bp_disabled;
7989       b->pspace = current_program_space;
7990       b->py_bp_object = NULL;
7991
7992       if (enabled && b->pspace->executing_startup
7993           && (b->type == bp_breakpoint
7994               || b->type == bp_hardware_breakpoint))
7995         b->enable_state = bp_startup_disabled;
7996
7997       if (internal)
7998         /* Do not mention breakpoints with a negative number, 
7999            but do notify observers.  */
8000         observer_notify_breakpoint_created (b->number);
8001       else
8002         mention (b);
8003     }
8004   
8005   if (sals.nelts > 1)
8006     {
8007       warning (_("Multiple breakpoints were set.\nUse the "
8008                  "\"delete\" command to delete unwanted breakpoints."));
8009       prev_breakpoint_count = prev_bkpt_count;
8010     }
8011
8012   /* That's it.  Discard the cleanups for data inserted into the
8013      breakpoint.  */
8014   discard_cleanups (bkpt_chain);
8015   /* But cleanup everything else.  */
8016   do_cleanups (old_chain);
8017
8018   /* error call may happen here - have BKPT_CHAIN already discarded.  */
8019   update_global_location_list (1);
8020
8021   return 1;
8022 }
8023
8024 /* Set a breakpoint. 
8025    ARG is a string describing breakpoint address,
8026    condition, and thread.
8027    FLAG specifies if a breakpoint is hardware on,
8028    and if breakpoint is temporary, using BP_HARDWARE_FLAG
8029    and BP_TEMPFLAG.  */
8030    
8031 static void
8032 break_command_1 (char *arg, int flag, int from_tty)
8033 {
8034   int tempflag = flag & BP_TEMPFLAG;
8035   enum bptype type_wanted = (flag & BP_HARDWAREFLAG
8036                              ? bp_hardware_breakpoint
8037                              : bp_breakpoint);
8038
8039   create_breakpoint (get_current_arch (),
8040                      arg,
8041                      NULL, 0, 1 /* parse arg */,
8042                      tempflag, type_wanted,
8043                      0 /* Ignore count */,
8044                      pending_break_support,
8045                      NULL /* breakpoint_ops */,
8046                      from_tty,
8047                      1 /* enabled */,
8048                      0 /* internal */);
8049 }
8050
8051
8052 /* Helper function for break_command_1 and disassemble_command.  */
8053
8054 void
8055 resolve_sal_pc (struct symtab_and_line *sal)
8056 {
8057   CORE_ADDR pc;
8058
8059   if (sal->pc == 0 && sal->symtab != NULL)
8060     {
8061       if (!find_line_pc (sal->symtab, sal->line, &pc))
8062         error (_("No line %d in file \"%s\"."),
8063                sal->line, sal->symtab->filename);
8064       sal->pc = pc;
8065
8066       /* If this SAL corresponds to a breakpoint inserted using a line
8067          number, then skip the function prologue if necessary.  */
8068       if (sal->explicit_line)
8069         skip_prologue_sal (sal);
8070     }
8071
8072   if (sal->section == 0 && sal->symtab != NULL)
8073     {
8074       struct blockvector *bv;
8075       struct block *b;
8076       struct symbol *sym;
8077
8078       bv = blockvector_for_pc_sect (sal->pc, 0, &b, sal->symtab);
8079       if (bv != NULL)
8080         {
8081           sym = block_linkage_function (b);
8082           if (sym != NULL)
8083             {
8084               fixup_symbol_section (sym, sal->symtab->objfile);
8085               sal->section = SYMBOL_OBJ_SECTION (sym);
8086             }
8087           else
8088             {
8089               /* It really is worthwhile to have the section, so we'll
8090                  just have to look harder. This case can be executed
8091                  if we have line numbers but no functions (as can
8092                  happen in assembly source).  */
8093
8094               struct minimal_symbol *msym;
8095               struct cleanup *old_chain = save_current_space_and_thread ();
8096
8097               switch_to_program_space_and_thread (sal->pspace);
8098
8099               msym = lookup_minimal_symbol_by_pc (sal->pc);
8100               if (msym)
8101                 sal->section = SYMBOL_OBJ_SECTION (msym);
8102
8103               do_cleanups (old_chain);
8104             }
8105         }
8106     }
8107 }
8108
8109 void
8110 break_command (char *arg, int from_tty)
8111 {
8112   break_command_1 (arg, 0, from_tty);
8113 }
8114
8115 void
8116 tbreak_command (char *arg, int from_tty)
8117 {
8118   break_command_1 (arg, BP_TEMPFLAG, from_tty);
8119 }
8120
8121 static void
8122 hbreak_command (char *arg, int from_tty)
8123 {
8124   break_command_1 (arg, BP_HARDWAREFLAG, from_tty);
8125 }
8126
8127 static void
8128 thbreak_command (char *arg, int from_tty)
8129 {
8130   break_command_1 (arg, (BP_TEMPFLAG | BP_HARDWAREFLAG), from_tty);
8131 }
8132
8133 static void
8134 stop_command (char *arg, int from_tty)
8135 {
8136   printf_filtered (_("Specify the type of breakpoint to set.\n\
8137 Usage: stop in <function | address>\n\
8138        stop at <line>\n"));
8139 }
8140
8141 static void
8142 stopin_command (char *arg, int from_tty)
8143 {
8144   int badInput = 0;
8145
8146   if (arg == (char *) NULL)
8147     badInput = 1;
8148   else if (*arg != '*')
8149     {
8150       char *argptr = arg;
8151       int hasColon = 0;
8152
8153       /* Look for a ':'.  If this is a line number specification, then
8154          say it is bad, otherwise, it should be an address or
8155          function/method name.  */
8156       while (*argptr && !hasColon)
8157         {
8158           hasColon = (*argptr == ':');
8159           argptr++;
8160         }
8161
8162       if (hasColon)
8163         badInput = (*argptr != ':');    /* Not a class::method */
8164       else
8165         badInput = isdigit (*arg);      /* a simple line number */
8166     }
8167
8168   if (badInput)
8169     printf_filtered (_("Usage: stop in <function | address>\n"));
8170   else
8171     break_command_1 (arg, 0, from_tty);
8172 }
8173
8174 static void
8175 stopat_command (char *arg, int from_tty)
8176 {
8177   int badInput = 0;
8178
8179   if (arg == (char *) NULL || *arg == '*')      /* no line number */
8180     badInput = 1;
8181   else
8182     {
8183       char *argptr = arg;
8184       int hasColon = 0;
8185
8186       /* Look for a ':'.  If there is a '::' then get out, otherwise
8187          it is probably a line number.  */
8188       while (*argptr && !hasColon)
8189         {
8190           hasColon = (*argptr == ':');
8191           argptr++;
8192         }
8193
8194       if (hasColon)
8195         badInput = (*argptr == ':');    /* we have class::method */
8196       else
8197         badInput = !isdigit (*arg);     /* not a line number */
8198     }
8199
8200   if (badInput)
8201     printf_filtered (_("Usage: stop at <line>\n"));
8202   else
8203     break_command_1 (arg, 0, from_tty);
8204 }
8205
8206 /*  Return non-zero if EXP is verified as constant.  Returned zero
8207     means EXP is variable.  Also the constant detection may fail for
8208     some constant expressions and in such case still falsely return
8209     zero.  */
8210 static int
8211 watchpoint_exp_is_const (const struct expression *exp)
8212 {
8213   int i = exp->nelts;
8214
8215   while (i > 0)
8216     {
8217       int oplenp, argsp;
8218
8219       /* We are only interested in the descriptor of each element.  */
8220       operator_length (exp, i, &oplenp, &argsp);
8221       i -= oplenp;
8222
8223       switch (exp->elts[i].opcode)
8224         {
8225         case BINOP_ADD:
8226         case BINOP_SUB:
8227         case BINOP_MUL:
8228         case BINOP_DIV:
8229         case BINOP_REM:
8230         case BINOP_MOD:
8231         case BINOP_LSH:
8232         case BINOP_RSH:
8233         case BINOP_LOGICAL_AND:
8234         case BINOP_LOGICAL_OR:
8235         case BINOP_BITWISE_AND:
8236         case BINOP_BITWISE_IOR:
8237         case BINOP_BITWISE_XOR:
8238         case BINOP_EQUAL:
8239         case BINOP_NOTEQUAL:
8240         case BINOP_LESS:
8241         case BINOP_GTR:
8242         case BINOP_LEQ:
8243         case BINOP_GEQ:
8244         case BINOP_REPEAT:
8245         case BINOP_COMMA:
8246         case BINOP_EXP:
8247         case BINOP_MIN:
8248         case BINOP_MAX:
8249         case BINOP_INTDIV:
8250         case BINOP_CONCAT:
8251         case BINOP_IN:
8252         case BINOP_RANGE:
8253         case TERNOP_COND:
8254         case TERNOP_SLICE:
8255         case TERNOP_SLICE_COUNT:
8256
8257         case OP_LONG:
8258         case OP_DOUBLE:
8259         case OP_DECFLOAT:
8260         case OP_LAST:
8261         case OP_COMPLEX:
8262         case OP_STRING:
8263         case OP_BITSTRING:
8264         case OP_ARRAY:
8265         case OP_TYPE:
8266         case OP_NAME:
8267         case OP_OBJC_NSSTRING:
8268
8269         case UNOP_NEG:
8270         case UNOP_LOGICAL_NOT:
8271         case UNOP_COMPLEMENT:
8272         case UNOP_ADDR:
8273         case UNOP_HIGH:
8274           /* Unary, binary and ternary operators: We have to check
8275              their operands.  If they are constant, then so is the
8276              result of that operation.  For instance, if A and B are
8277              determined to be constants, then so is "A + B".
8278
8279              UNOP_IND is one exception to the rule above, because the
8280              value of *ADDR is not necessarily a constant, even when
8281              ADDR is.  */
8282           break;
8283
8284         case OP_VAR_VALUE:
8285           /* Check whether the associated symbol is a constant.
8286
8287              We use SYMBOL_CLASS rather than TYPE_CONST because it's
8288              possible that a buggy compiler could mark a variable as
8289              constant even when it is not, and TYPE_CONST would return
8290              true in this case, while SYMBOL_CLASS wouldn't.
8291
8292              We also have to check for function symbols because they
8293              are always constant.  */
8294           {
8295             struct symbol *s = exp->elts[i + 2].symbol;
8296
8297             if (SYMBOL_CLASS (s) != LOC_BLOCK
8298                 && SYMBOL_CLASS (s) != LOC_CONST
8299                 && SYMBOL_CLASS (s) != LOC_CONST_BYTES)
8300               return 0;
8301             break;
8302           }
8303
8304         /* The default action is to return 0 because we are using
8305            the optimistic approach here: If we don't know something,
8306            then it is not a constant.  */
8307         default:
8308           return 0;
8309         }
8310     }
8311
8312   return 1;
8313 }
8314
8315 /* Implement the "insert" breakpoint_ops method for hardware watchpoints.  */
8316
8317 static int
8318 insert_watchpoint (struct bp_location *bl)
8319 {
8320   int length = bl->owner->exact? 1 : bl->length;
8321
8322   return target_insert_watchpoint (bl->address, length, bl->watchpoint_type,
8323                                    bl->owner->cond_exp);
8324 }
8325
8326 /* Implement the "remove" breakpoint_ops method for hardware watchpoints.  */
8327
8328 static int
8329 remove_watchpoint (struct bp_location *bl)
8330 {
8331   int length = bl->owner->exact? 1 : bl->length;
8332
8333   return target_remove_watchpoint (bl->address, length, bl->watchpoint_type,
8334                                    bl->owner->cond_exp);
8335 }
8336
8337 /* Implement the "resources_needed" breakpoint_ops method for
8338    hardware watchpoints.  */
8339
8340 static int
8341 resources_needed_watchpoint (const struct bp_location *bl)
8342 {
8343   int length = bl->owner->exact? 1 : bl->length;
8344
8345   return target_region_ok_for_hw_watchpoint (bl->address, length);
8346 }
8347
8348 /* The breakpoint_ops structure to be used in hardware watchpoints.  */
8349
8350 static struct breakpoint_ops watchpoint_breakpoint_ops =
8351 {
8352   insert_watchpoint,
8353   remove_watchpoint,
8354   NULL, /* breakpoint_hit */
8355   resources_needed_watchpoint,
8356   NULL, /* print_it */
8357   NULL, /* print_one */
8358   NULL, /* print_mention */
8359   NULL  /* print_recreate */
8360 };
8361
8362 /* accessflag:  hw_write:  watch write, 
8363                 hw_read:   watch read, 
8364                 hw_access: watch access (read or write) */
8365 static void
8366 watch_command_1 (char *arg, int accessflag, int from_tty,
8367                  int just_location, int internal)
8368 {
8369   struct breakpoint *b, *scope_breakpoint = NULL;
8370   struct expression *exp;
8371   struct block *exp_valid_block = NULL, *cond_exp_valid_block = NULL;
8372   struct value *val, *mark, *result;
8373   struct frame_info *frame;
8374   char *exp_start = NULL;
8375   char *exp_end = NULL;
8376   char *tok, *id_tok_start, *end_tok;
8377   int toklen;
8378   char *cond_start = NULL;
8379   char *cond_end = NULL;
8380   int i, other_type_used, target_resources_ok = 0;
8381   enum bptype bp_type;
8382   int reg_cnt = 0;
8383   int thread = -1;
8384   int pc = 0;
8385
8386   /* Make sure that we actually have parameters to parse.  */
8387   if (arg != NULL && arg[0] != '\0')
8388     {
8389       toklen = strlen (arg); /* Size of argument list.  */
8390
8391       /* Points tok to the end of the argument list.  */
8392       tok = arg + toklen - 1;
8393
8394       /* Go backwards in the parameters list.  Skip the last
8395          parameter.  If we're expecting a 'thread <thread_num>'
8396          parameter, this should be the thread identifier.  */
8397       while (tok > arg && (*tok == ' ' || *tok == '\t'))
8398         tok--;
8399       while (tok > arg && (*tok != ' ' && *tok != '\t'))
8400         tok--;
8401
8402       /* Points end_tok to the beginning of the last token.  */
8403       id_tok_start = tok + 1;
8404
8405       /* Go backwards in the parameters list.  Skip one more
8406          parameter.  If we're expecting a 'thread <thread_num>'
8407          parameter, we should reach a "thread" token.  */
8408       while (tok > arg && (*tok == ' ' || *tok == '\t'))
8409         tok--;
8410
8411       end_tok = tok;
8412
8413       while (tok > arg && (*tok != ' ' && *tok != '\t'))
8414         tok--;
8415
8416       /* Move the pointer forward to skip the whitespace and
8417          calculate the length of the token.  */
8418       tok++;
8419       toklen = end_tok - tok;
8420
8421       if (toklen >= 1 && strncmp (tok, "thread", toklen) == 0)
8422         {
8423           /* At this point we've found a "thread" token, which means
8424              the user is trying to set a watchpoint that triggers
8425              only in a specific thread.  */
8426           char *endp;
8427
8428           /* Extract the thread ID from the next token.  */
8429           thread = strtol (id_tok_start, &endp, 0);
8430
8431           /* Check if the user provided a valid numeric value for the
8432              thread ID.  */
8433           if (*endp != ' ' && *endp != '\t' && *endp != '\0')
8434             error (_("Invalid thread ID specification %s."), id_tok_start);
8435
8436           /* Check if the thread actually exists.  */
8437           if (!valid_thread_id (thread))
8438             error (_("Unknown thread %d."), thread);
8439
8440           /* Truncate the string and get rid of the thread <thread_num>
8441              parameter before the parameter list is parsed by the
8442              evaluate_expression() function.  */
8443           *tok = '\0';
8444         }
8445     }
8446
8447   /* Parse the rest of the arguments.  */
8448   innermost_block = NULL;
8449   exp_start = arg;
8450   exp = parse_exp_1 (&arg, 0, 0);
8451   exp_end = arg;
8452   /* Remove trailing whitespace from the expression before saving it.
8453      This makes the eventual display of the expression string a bit
8454      prettier.  */
8455   while (exp_end > exp_start && (exp_end[-1] == ' ' || exp_end[-1] == '\t'))
8456     --exp_end;
8457
8458   /* Checking if the expression is not constant.  */
8459   if (watchpoint_exp_is_const (exp))
8460     {
8461       int len;
8462
8463       len = exp_end - exp_start;
8464       while (len > 0 && isspace (exp_start[len - 1]))
8465         len--;
8466       error (_("Cannot watch constant value `%.*s'."), len, exp_start);
8467     }
8468
8469   exp_valid_block = innermost_block;
8470   mark = value_mark ();
8471   fetch_subexp_value (exp, &pc, &val, &result, NULL);
8472
8473   if (just_location)
8474     {
8475       exp_valid_block = NULL;
8476       val = value_addr (result);
8477       release_value (val);
8478       value_free_to_mark (mark);
8479     }
8480   else if (val != NULL)
8481     release_value (val);
8482
8483   tok = skip_spaces (arg);
8484   end_tok = skip_to_space (tok);
8485
8486   toklen = end_tok - tok;
8487   if (toklen >= 1 && strncmp (tok, "if", toklen) == 0)
8488     {
8489       struct expression *cond;
8490
8491       innermost_block = NULL;
8492       tok = cond_start = end_tok + 1;
8493       cond = parse_exp_1 (&tok, 0, 0);
8494
8495       /* The watchpoint expression may not be local, but the condition
8496          may still be.  E.g.: `watch global if local > 0'.  */
8497       cond_exp_valid_block = innermost_block;
8498
8499       xfree (cond);
8500       cond_end = tok;
8501     }
8502   if (*tok)
8503     error (_("Junk at end of command."));
8504
8505   if (accessflag == hw_read)
8506     bp_type = bp_read_watchpoint;
8507   else if (accessflag == hw_access)
8508     bp_type = bp_access_watchpoint;
8509   else
8510     bp_type = bp_hardware_watchpoint;
8511
8512   reg_cnt = can_use_hardware_watchpoint (val, target_exact_watchpoints);
8513   if (reg_cnt == 0 && bp_type != bp_hardware_watchpoint)
8514     error (_("Expression cannot be implemented with read/access watchpoint."));
8515   if (reg_cnt != 0)
8516     {
8517       i = hw_watchpoint_used_count (bp_type, &other_type_used);
8518       target_resources_ok = 
8519         target_can_use_hardware_watchpoint (bp_type, i + reg_cnt,
8520                                             other_type_used);
8521       if (target_resources_ok == 0 && bp_type != bp_hardware_watchpoint)
8522         error (_("Target does not support this type of hardware watchpoint."));
8523
8524       if (target_resources_ok < 0 && bp_type != bp_hardware_watchpoint)
8525         error (_("Target can only support one kind "
8526                  "of HW watchpoint at a time."));
8527     }
8528
8529   /* Change the type of breakpoint to an ordinary watchpoint if a
8530      hardware watchpoint could not be set.  */
8531   if (!reg_cnt || target_resources_ok <= 0)
8532     bp_type = bp_watchpoint;
8533
8534   frame = block_innermost_frame (exp_valid_block);
8535
8536   /* If the expression is "local", then set up a "watchpoint scope"
8537      breakpoint at the point where we've left the scope of the watchpoint
8538      expression.  Create the scope breakpoint before the watchpoint, so
8539      that we will encounter it first in bpstat_stop_status.  */
8540   if (exp_valid_block && frame)
8541     {
8542       if (frame_id_p (frame_unwind_caller_id (frame)))
8543         {
8544           scope_breakpoint
8545             = create_internal_breakpoint (frame_unwind_caller_arch (frame),
8546                                           frame_unwind_caller_pc (frame),
8547                                           bp_watchpoint_scope);
8548
8549           scope_breakpoint->enable_state = bp_enabled;
8550
8551           /* Automatically delete the breakpoint when it hits.  */
8552           scope_breakpoint->disposition = disp_del;
8553
8554           /* Only break in the proper frame (help with recursion).  */
8555           scope_breakpoint->frame_id = frame_unwind_caller_id (frame);
8556
8557           /* Set the address at which we will stop.  */
8558           scope_breakpoint->loc->gdbarch
8559             = frame_unwind_caller_arch (frame);
8560           scope_breakpoint->loc->requested_address
8561             = frame_unwind_caller_pc (frame);
8562           scope_breakpoint->loc->address
8563             = adjust_breakpoint_address (scope_breakpoint->loc->gdbarch,
8564                                          scope_breakpoint->loc->requested_address,
8565                                          scope_breakpoint->type);
8566         }
8567     }
8568
8569   /* Now set up the breakpoint.  */
8570   b = set_raw_breakpoint_without_location (NULL, bp_type);
8571   set_breakpoint_number (internal, b);
8572   b->thread = thread;
8573   b->disposition = disp_donttouch;
8574   b->exp = exp;
8575   b->exp_valid_block = exp_valid_block;
8576   b->cond_exp_valid_block = cond_exp_valid_block;
8577   if (just_location)
8578     {
8579       struct type *t = value_type (val);
8580       CORE_ADDR addr = value_as_address (val);
8581       char *name;
8582
8583       t = check_typedef (TYPE_TARGET_TYPE (check_typedef (t)));
8584       name = type_to_string (t);
8585
8586       b->exp_string_reparse = xstrprintf ("* (%s *) %s", name,
8587                                           core_addr_to_string (addr));
8588       xfree (name);
8589
8590       b->exp_string = xstrprintf ("-location: %.*s",
8591                                   (int) (exp_end - exp_start), exp_start);
8592
8593       /* The above expression is in C.  */
8594       b->language = language_c;
8595     }
8596   else
8597     b->exp_string = savestring (exp_start, exp_end - exp_start);
8598   b->val = val;
8599   b->val_valid = 1;
8600   b->ops = &watchpoint_breakpoint_ops;
8601
8602   /* Use an exact watchpoint when there's only one memory region to be
8603      watched, and only one debug register is needed to watch it.  */
8604   b->exact = target_exact_watchpoints && reg_cnt == 1;
8605
8606   if (cond_start)
8607     b->cond_string = savestring (cond_start, cond_end - cond_start);
8608   else
8609     b->cond_string = 0;
8610
8611   if (frame)
8612     {
8613       b->watchpoint_frame = get_frame_id (frame);
8614       b->watchpoint_thread = inferior_ptid;
8615     }
8616   else
8617     {
8618       b->watchpoint_frame = null_frame_id;
8619       b->watchpoint_thread = null_ptid;
8620     }
8621
8622   if (scope_breakpoint != NULL)
8623     {
8624       /* The scope breakpoint is related to the watchpoint.  We will
8625          need to act on them together.  */
8626       b->related_breakpoint = scope_breakpoint;
8627       scope_breakpoint->related_breakpoint = b;
8628     }
8629
8630   if (!just_location)
8631     value_free_to_mark (mark);
8632
8633   /* Finally update the new watchpoint.  This creates the locations
8634      that should be inserted.  */
8635   update_watchpoint (b, 1);
8636   if (internal)
8637     /* Do not mention breakpoints with a negative number, but do
8638        notify observers.  */
8639     observer_notify_breakpoint_created (b->number);
8640   else
8641     mention (b);
8642   update_global_location_list (1);
8643 }
8644
8645 /* Return count of debug registers needed to watch the given expression.
8646    If EXACT_WATCHPOINTS is 1, then consider that only the address of
8647    the start of the watched region will be monitored (i.e., all accesses
8648    will be aligned).  This uses less debug registers on some targets.
8649
8650    If the watchpoint cannot be handled in hardware return zero.  */
8651
8652 static int
8653 can_use_hardware_watchpoint (struct value *v, int exact_watchpoints)
8654 {
8655   int found_memory_cnt = 0;
8656   struct value *head = v;
8657
8658   /* Did the user specifically forbid us to use hardware watchpoints? */
8659   if (!can_use_hw_watchpoints)
8660     return 0;
8661
8662   /* Make sure that the value of the expression depends only upon
8663      memory contents, and values computed from them within GDB.  If we
8664      find any register references or function calls, we can't use a
8665      hardware watchpoint.
8666
8667      The idea here is that evaluating an expression generates a series
8668      of values, one holding the value of every subexpression.  (The
8669      expression a*b+c has five subexpressions: a, b, a*b, c, and
8670      a*b+c.)  GDB's values hold almost enough information to establish
8671      the criteria given above --- they identify memory lvalues,
8672      register lvalues, computed values, etcetera.  So we can evaluate
8673      the expression, and then scan the chain of values that leaves
8674      behind to decide whether we can detect any possible change to the
8675      expression's final value using only hardware watchpoints.
8676
8677      However, I don't think that the values returned by inferior
8678      function calls are special in any way.  So this function may not
8679      notice that an expression involving an inferior function call
8680      can't be watched with hardware watchpoints.  FIXME.  */
8681   for (; v; v = value_next (v))
8682     {
8683       if (VALUE_LVAL (v) == lval_memory)
8684         {
8685           if (v != head && value_lazy (v))
8686             /* A lazy memory lvalue in the chain is one that GDB never
8687                needed to fetch; we either just used its address (e.g.,
8688                `a' in `a.b') or we never needed it at all (e.g., `a'
8689                in `a,b').  This doesn't apply to HEAD; if that is
8690                lazy then it was not readable, but watch it anyway.  */
8691             ;
8692           else
8693             {
8694               /* Ahh, memory we actually used!  Check if we can cover
8695                  it with hardware watchpoints.  */
8696               struct type *vtype = check_typedef (value_type (v));
8697
8698               /* We only watch structs and arrays if user asked for it
8699                  explicitly, never if they just happen to appear in a
8700                  middle of some value chain.  */
8701               if (v == head
8702                   || (TYPE_CODE (vtype) != TYPE_CODE_STRUCT
8703                       && TYPE_CODE (vtype) != TYPE_CODE_ARRAY))
8704                 {
8705                   CORE_ADDR vaddr = value_address (v);
8706                   int len;
8707                   int num_regs;
8708
8709                   len = (exact_watchpoints
8710                          && is_scalar_type_recursive (vtype))?
8711                     1 : TYPE_LENGTH (value_type (v));
8712
8713                   num_regs = target_region_ok_for_hw_watchpoint (vaddr, len);
8714                   if (!num_regs)
8715                     return 0;
8716                   else
8717                     found_memory_cnt += num_regs;
8718                 }
8719             }
8720         }
8721       else if (VALUE_LVAL (v) != not_lval
8722                && deprecated_value_modifiable (v) == 0)
8723         return 0;       /* These are values from the history (e.g., $1).  */
8724       else if (VALUE_LVAL (v) == lval_register)
8725         return 0;       /* Cannot watch a register with a HW watchpoint.  */
8726     }
8727
8728   /* The expression itself looks suitable for using a hardware
8729      watchpoint, but give the target machine a chance to reject it.  */
8730   return found_memory_cnt;
8731 }
8732
8733 void
8734 watch_command_wrapper (char *arg, int from_tty, int internal)
8735 {
8736   watch_command_1 (arg, hw_write, from_tty, 0, internal);
8737 }
8738
8739 /* A helper function that looks for an argument at the start of a
8740    string.  The argument must also either be at the end of the string,
8741    or be followed by whitespace.  Returns 1 if it finds the argument,
8742    0 otherwise.  If the argument is found, it updates *STR.  */
8743
8744 static int
8745 check_for_argument (char **str, char *arg, int arg_len)
8746 {
8747   if (strncmp (*str, arg, arg_len) == 0
8748       && ((*str)[arg_len] == '\0' || isspace ((*str)[arg_len])))
8749     {
8750       *str += arg_len;
8751       return 1;
8752     }
8753   return 0;
8754 }
8755
8756 /* A helper function that looks for the "-location" argument and then
8757    calls watch_command_1.  */
8758
8759 static void
8760 watch_maybe_just_location (char *arg, int accessflag, int from_tty)
8761 {
8762   int just_location = 0;
8763
8764   if (arg
8765       && (check_for_argument (&arg, "-location", sizeof ("-location") - 1)
8766           || check_for_argument (&arg, "-l", sizeof ("-l") - 1)))
8767     {
8768       arg = skip_spaces (arg);
8769       just_location = 1;
8770     }
8771
8772   watch_command_1 (arg, accessflag, from_tty, just_location, 0);
8773 }
8774
8775 static void
8776 watch_command (char *arg, int from_tty)
8777 {
8778   watch_maybe_just_location (arg, hw_write, from_tty);
8779 }
8780
8781 void
8782 rwatch_command_wrapper (char *arg, int from_tty, int internal)
8783 {
8784   watch_command_1 (arg, hw_read, from_tty, 0, internal);
8785 }
8786
8787 static void
8788 rwatch_command (char *arg, int from_tty)
8789 {
8790   watch_maybe_just_location (arg, hw_read, from_tty);
8791 }
8792
8793 void
8794 awatch_command_wrapper (char *arg, int from_tty, int internal)
8795 {
8796   watch_command_1 (arg, hw_access, from_tty, 0, internal);
8797 }
8798
8799 static void
8800 awatch_command (char *arg, int from_tty)
8801 {
8802   watch_maybe_just_location (arg, hw_access, from_tty);
8803 }
8804 \f
8805
8806 /* Helper routines for the until_command routine in infcmd.c.  Here
8807    because it uses the mechanisms of breakpoints.  */
8808
8809 struct until_break_command_continuation_args
8810 {
8811   struct breakpoint *breakpoint;
8812   struct breakpoint *breakpoint2;
8813   int thread_num;
8814 };
8815
8816 /* This function is called by fetch_inferior_event via the
8817    cmd_continuation pointer, to complete the until command.  It takes
8818    care of cleaning up the temporary breakpoints set up by the until
8819    command.  */
8820 static void
8821 until_break_command_continuation (void *arg)
8822 {
8823   struct until_break_command_continuation_args *a = arg;
8824
8825   delete_breakpoint (a->breakpoint);
8826   if (a->breakpoint2)
8827     delete_breakpoint (a->breakpoint2);
8828   delete_longjmp_breakpoint (a->thread_num);
8829 }
8830
8831 void
8832 until_break_command (char *arg, int from_tty, int anywhere)
8833 {
8834   struct symtabs_and_lines sals;
8835   struct symtab_and_line sal;
8836   struct frame_info *frame = get_selected_frame (NULL);
8837   struct breakpoint *breakpoint;
8838   struct breakpoint *breakpoint2 = NULL;
8839   struct cleanup *old_chain;
8840   int thread;
8841   struct thread_info *tp;
8842
8843   clear_proceed_status ();
8844
8845   /* Set a breakpoint where the user wants it and at return from
8846      this function.  */
8847
8848   if (default_breakpoint_valid)
8849     sals = decode_line_1 (&arg, 1, default_breakpoint_symtab,
8850                           default_breakpoint_line, (char ***) NULL, NULL);
8851   else
8852     sals = decode_line_1 (&arg, 1, (struct symtab *) NULL, 
8853                           0, (char ***) NULL, NULL);
8854
8855   if (sals.nelts != 1)
8856     error (_("Couldn't get information on specified line."));
8857
8858   sal = sals.sals[0];
8859   xfree (sals.sals);    /* malloc'd, so freed.  */
8860
8861   if (*arg)
8862     error (_("Junk at end of arguments."));
8863
8864   resolve_sal_pc (&sal);
8865
8866   if (anywhere)
8867     /* If the user told us to continue until a specified location,
8868        we don't specify a frame at which we need to stop.  */
8869     breakpoint = set_momentary_breakpoint (get_frame_arch (frame), sal,
8870                                            null_frame_id, bp_until);
8871   else
8872     /* Otherwise, specify the selected frame, because we want to stop
8873        only at the very same frame.  */
8874     breakpoint = set_momentary_breakpoint (get_frame_arch (frame), sal,
8875                                            get_stack_frame_id (frame),
8876                                            bp_until);
8877
8878   old_chain = make_cleanup_delete_breakpoint (breakpoint);
8879
8880   tp = inferior_thread ();
8881   thread = tp->num;
8882
8883   /* Keep within the current frame, or in frames called by the current
8884      one.  */
8885
8886   if (frame_id_p (frame_unwind_caller_id (frame)))
8887     {
8888       sal = find_pc_line (frame_unwind_caller_pc (frame), 0);
8889       sal.pc = frame_unwind_caller_pc (frame);
8890       breakpoint2 = set_momentary_breakpoint (frame_unwind_caller_arch (frame),
8891                                               sal,
8892                                               frame_unwind_caller_id (frame),
8893                                               bp_until);
8894       make_cleanup_delete_breakpoint (breakpoint2);
8895
8896       set_longjmp_breakpoint (tp, frame_unwind_caller_id (frame));
8897       make_cleanup (delete_longjmp_breakpoint_cleanup, &thread);
8898     }
8899
8900   proceed (-1, TARGET_SIGNAL_DEFAULT, 0);
8901
8902   /* If we are running asynchronously, and proceed call above has
8903      actually managed to start the target, arrange for breakpoints to
8904      be deleted when the target stops.  Otherwise, we're already
8905      stopped and delete breakpoints via cleanup chain.  */
8906
8907   if (target_can_async_p () && is_running (inferior_ptid))
8908     {
8909       struct until_break_command_continuation_args *args;
8910       args = xmalloc (sizeof (*args));
8911
8912       args->breakpoint = breakpoint;
8913       args->breakpoint2 = breakpoint2;
8914       args->thread_num = thread;
8915
8916       discard_cleanups (old_chain);
8917       add_continuation (inferior_thread (),
8918                         until_break_command_continuation, args,
8919                         xfree);
8920     }
8921   else
8922     do_cleanups (old_chain);
8923 }
8924
8925 /* This function attempts to parse an optional "if <cond>" clause
8926    from the arg string.  If one is not found, it returns NULL.
8927
8928    Else, it returns a pointer to the condition string.  (It does not
8929    attempt to evaluate the string against a particular block.)  And,
8930    it updates arg to point to the first character following the parsed
8931    if clause in the arg string.  */
8932
8933 static char *
8934 ep_parse_optional_if_clause (char **arg)
8935 {
8936   char *cond_string;
8937
8938   if (((*arg)[0] != 'i') || ((*arg)[1] != 'f') || !isspace ((*arg)[2]))
8939     return NULL;
8940
8941   /* Skip the "if" keyword.  */
8942   (*arg) += 2;
8943
8944   /* Skip any extra leading whitespace, and record the start of the
8945      condition string.  */
8946   *arg = skip_spaces (*arg);
8947   cond_string = *arg;
8948
8949   /* Assume that the condition occupies the remainder of the arg
8950      string.  */
8951   (*arg) += strlen (cond_string);
8952
8953   return cond_string;
8954 }
8955
8956 /* Commands to deal with catching events, such as signals, exceptions,
8957    process start/exit, etc.  */
8958
8959 typedef enum
8960 {
8961   catch_fork_temporary, catch_vfork_temporary,
8962   catch_fork_permanent, catch_vfork_permanent
8963 }
8964 catch_fork_kind;
8965
8966 static void
8967 catch_fork_command_1 (char *arg, int from_tty, 
8968                       struct cmd_list_element *command)
8969 {
8970   struct gdbarch *gdbarch = get_current_arch ();
8971   char *cond_string = NULL;
8972   catch_fork_kind fork_kind;
8973   int tempflag;
8974
8975   fork_kind = (catch_fork_kind) (uintptr_t) get_cmd_context (command);
8976   tempflag = (fork_kind == catch_fork_temporary
8977               || fork_kind == catch_vfork_temporary);
8978
8979   if (!arg)
8980     arg = "";
8981   arg = skip_spaces (arg);
8982
8983   /* The allowed syntax is:
8984      catch [v]fork
8985      catch [v]fork if <cond>
8986
8987      First, check if there's an if clause.  */
8988   cond_string = ep_parse_optional_if_clause (&arg);
8989
8990   if ((*arg != '\0') && !isspace (*arg))
8991     error (_("Junk at end of arguments."));
8992
8993   /* If this target supports it, create a fork or vfork catchpoint
8994      and enable reporting of such events.  */
8995   switch (fork_kind)
8996     {
8997     case catch_fork_temporary:
8998     case catch_fork_permanent:
8999       create_fork_vfork_event_catchpoint (gdbarch, tempflag, cond_string,
9000                                           &catch_fork_breakpoint_ops);
9001       break;
9002     case catch_vfork_temporary:
9003     case catch_vfork_permanent:
9004       create_fork_vfork_event_catchpoint (gdbarch, tempflag, cond_string,
9005                                           &catch_vfork_breakpoint_ops);
9006       break;
9007     default:
9008       error (_("unsupported or unknown fork kind; cannot catch it"));
9009       break;
9010     }
9011 }
9012
9013 static void
9014 catch_exec_command_1 (char *arg, int from_tty, 
9015                       struct cmd_list_element *command)
9016 {
9017   struct gdbarch *gdbarch = get_current_arch ();
9018   int tempflag;
9019   char *cond_string = NULL;
9020
9021   tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
9022
9023   if (!arg)
9024     arg = "";
9025   arg = skip_spaces (arg);
9026
9027   /* The allowed syntax is:
9028      catch exec
9029      catch exec if <cond>
9030
9031      First, check if there's an if clause.  */
9032   cond_string = ep_parse_optional_if_clause (&arg);
9033
9034   if ((*arg != '\0') && !isspace (*arg))
9035     error (_("Junk at end of arguments."));
9036
9037   /* If this target supports it, create an exec catchpoint
9038      and enable reporting of such events.  */
9039   create_catchpoint (gdbarch, tempflag, cond_string,
9040                      &catch_exec_breakpoint_ops);
9041 }
9042
9043 static enum print_stop_action
9044 print_exception_catchpoint (struct breakpoint *b)
9045 {
9046   int bp_temp, bp_throw;
9047
9048   annotate_catchpoint (b->number);
9049
9050   bp_throw = strstr (b->addr_string, "throw") != NULL;
9051   if (b->loc->address != b->loc->requested_address)
9052     breakpoint_adjustment_warning (b->loc->requested_address,
9053                                    b->loc->address,
9054                                    b->number, 1);
9055   bp_temp = b->disposition == disp_del;
9056   ui_out_text (uiout, 
9057                bp_temp ? "Temporary catchpoint "
9058                        : "Catchpoint ");
9059   if (!ui_out_is_mi_like_p (uiout))
9060     ui_out_field_int (uiout, "bkptno", b->number);
9061   ui_out_text (uiout,
9062                bp_throw ? " (exception thrown), "
9063                         : " (exception caught), ");
9064   if (ui_out_is_mi_like_p (uiout))
9065     {
9066       ui_out_field_string (uiout, "reason", 
9067                            async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT));
9068       ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
9069       ui_out_field_int (uiout, "bkptno", b->number);
9070     }
9071   return PRINT_SRC_AND_LOC;
9072 }
9073
9074 static void
9075 print_one_exception_catchpoint (struct breakpoint *b, 
9076                                 struct bp_location **last_loc)
9077 {
9078   struct value_print_options opts;
9079
9080   get_user_print_options (&opts);
9081   if (opts.addressprint)
9082     {
9083       annotate_field (4);
9084       if (b->loc == NULL || b->loc->shlib_disabled)
9085         ui_out_field_string (uiout, "addr", "<PENDING>");
9086       else
9087         ui_out_field_core_addr (uiout, "addr",
9088                                 b->loc->gdbarch, b->loc->address);
9089     }
9090   annotate_field (5);
9091   if (b->loc)
9092     *last_loc = b->loc;
9093   if (strstr (b->addr_string, "throw") != NULL)
9094     ui_out_field_string (uiout, "what", "exception throw");
9095   else
9096     ui_out_field_string (uiout, "what", "exception catch");
9097 }
9098
9099 static void
9100 print_mention_exception_catchpoint (struct breakpoint *b)
9101 {
9102   int bp_temp;
9103   int bp_throw;
9104
9105   bp_temp = b->disposition == disp_del;
9106   bp_throw = strstr (b->addr_string, "throw") != NULL;
9107   ui_out_text (uiout, bp_temp ? _("Temporary catchpoint ")
9108                               : _("Catchpoint "));
9109   ui_out_field_int (uiout, "bkptno", b->number);
9110   ui_out_text (uiout, bp_throw ? _(" (throw)")
9111                                : _(" (catch)"));
9112 }
9113
9114 /* Implement the "print_recreate" breakpoint_ops method for throw and
9115    catch catchpoints.  */
9116
9117 static void
9118 print_recreate_exception_catchpoint (struct breakpoint *b, 
9119                                      struct ui_file *fp)
9120 {
9121   int bp_temp;
9122   int bp_throw;
9123
9124   bp_temp = b->disposition == disp_del;
9125   bp_throw = strstr (b->addr_string, "throw") != NULL;
9126   fprintf_unfiltered (fp, bp_temp ? "tcatch " : "catch ");
9127   fprintf_unfiltered (fp, bp_throw ? "throw" : "catch");
9128 }
9129
9130 static struct breakpoint_ops gnu_v3_exception_catchpoint_ops = {
9131   NULL, /* insert */
9132   NULL, /* remove */
9133   NULL, /* breakpoint_hit */
9134   NULL, /* resources_needed */
9135   print_exception_catchpoint,
9136   print_one_exception_catchpoint,
9137   print_mention_exception_catchpoint,
9138   print_recreate_exception_catchpoint
9139 };
9140
9141 static int
9142 handle_gnu_v3_exceptions (int tempflag, char *cond_string,
9143                           enum exception_event_kind ex_event, int from_tty)
9144 {
9145   char *trigger_func_name;
9146  
9147   if (ex_event == EX_EVENT_CATCH)
9148     trigger_func_name = "__cxa_begin_catch";
9149   else
9150     trigger_func_name = "__cxa_throw";
9151
9152   create_breakpoint (get_current_arch (),
9153                      trigger_func_name, cond_string, -1,
9154                      0 /* condition and thread are valid.  */,
9155                      tempflag, bp_breakpoint,
9156                      0,
9157                      AUTO_BOOLEAN_TRUE /* pending */,
9158                      &gnu_v3_exception_catchpoint_ops, from_tty,
9159                      1 /* enabled */,
9160                      0 /* internal */);
9161
9162   return 1;
9163 }
9164
9165 /* Deal with "catch catch" and "catch throw" commands.  */
9166
9167 static void
9168 catch_exception_command_1 (enum exception_event_kind ex_event, char *arg,
9169                            int tempflag, int from_tty)
9170 {
9171   char *cond_string = NULL;
9172
9173   if (!arg)
9174     arg = "";
9175   arg = skip_spaces (arg);
9176
9177   cond_string = ep_parse_optional_if_clause (&arg);
9178
9179   if ((*arg != '\0') && !isspace (*arg))
9180     error (_("Junk at end of arguments."));
9181
9182   if (ex_event != EX_EVENT_THROW
9183       && ex_event != EX_EVENT_CATCH)
9184     error (_("Unsupported or unknown exception event; cannot catch it"));
9185
9186   if (handle_gnu_v3_exceptions (tempflag, cond_string, ex_event, from_tty))
9187     return;
9188
9189   warning (_("Unsupported with this platform/compiler combination."));
9190 }
9191
9192 /* Implementation of "catch catch" command.  */
9193
9194 static void
9195 catch_catch_command (char *arg, int from_tty, struct cmd_list_element *command)
9196 {
9197   int tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
9198
9199   catch_exception_command_1 (EX_EVENT_CATCH, arg, tempflag, from_tty);
9200 }
9201
9202 /* Implementation of "catch throw" command.  */
9203
9204 static void
9205 catch_throw_command (char *arg, int from_tty, struct cmd_list_element *command)
9206 {
9207   int tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
9208
9209   catch_exception_command_1 (EX_EVENT_THROW, arg, tempflag, from_tty);
9210 }
9211
9212 /* Create a breakpoint struct for Ada exception catchpoints.  */
9213
9214 static void
9215 create_ada_exception_breakpoint (struct gdbarch *gdbarch,
9216                                  struct symtab_and_line sal,
9217                                  char *addr_string,
9218                                  char *exp_string,
9219                                  char *cond_string,
9220                                  struct expression *cond,
9221                                  struct breakpoint_ops *ops,
9222                                  int tempflag,
9223                                  int from_tty)
9224 {
9225   struct breakpoint *b;
9226
9227   if (from_tty)
9228     {
9229       struct gdbarch *loc_gdbarch = get_sal_arch (sal);
9230       if (!loc_gdbarch)
9231         loc_gdbarch = gdbarch;
9232
9233       describe_other_breakpoints (loc_gdbarch,
9234                                   sal.pspace, sal.pc, sal.section, -1);
9235       /* FIXME: brobecker/2006-12-28: Actually, re-implement a special
9236          version for exception catchpoints, because two catchpoints
9237          used for different exception names will use the same address.
9238          In this case, a "breakpoint ... also set at..." warning is
9239          unproductive.  Besides, the warning phrasing is also a bit
9240          inapropriate, we should use the word catchpoint, and tell
9241          the user what type of catchpoint it is.  The above is good
9242          enough for now, though.  */
9243     }
9244
9245   b = set_raw_breakpoint (gdbarch, sal, bp_breakpoint);
9246   set_breakpoint_count (breakpoint_count + 1);
9247
9248   b->enable_state = bp_enabled;
9249   b->disposition = tempflag ? disp_del : disp_donttouch;
9250   b->number = breakpoint_count;
9251   b->ignore_count = 0;
9252   b->loc->cond = cond;
9253   b->addr_string = addr_string;
9254   b->language = language_ada;
9255   b->cond_string = cond_string;
9256   b->exp_string = exp_string;
9257   b->thread = -1;
9258   b->ops = ops;
9259
9260   mention (b);
9261   update_global_location_list (1);
9262 }
9263
9264 /* Implement the "catch exception" command.  */
9265
9266 static void
9267 catch_ada_exception_command (char *arg, int from_tty,
9268                              struct cmd_list_element *command)
9269 {
9270   struct gdbarch *gdbarch = get_current_arch ();
9271   int tempflag;
9272   struct symtab_and_line sal;
9273   char *addr_string = NULL;
9274   char *exp_string = NULL;
9275   char *cond_string = NULL;
9276   struct expression *cond = NULL;
9277   struct breakpoint_ops *ops = NULL;
9278
9279   tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
9280
9281   if (!arg)
9282     arg = "";
9283   sal = ada_decode_exception_location (arg, &addr_string, &exp_string,
9284                                        &cond_string, &cond, &ops);
9285   create_ada_exception_breakpoint (gdbarch, sal, addr_string, exp_string,
9286                                    cond_string, cond, ops, tempflag,
9287                                    from_tty);
9288 }
9289
9290 /* Cleanup function for a syscall filter list.  */
9291 static void
9292 clean_up_filters (void *arg)
9293 {
9294   VEC(int) *iter = *(VEC(int) **) arg;
9295   VEC_free (int, iter);
9296 }
9297
9298 /* Splits the argument using space as delimiter.  Returns an xmalloc'd
9299    filter list, or NULL if no filtering is required.  */
9300 static VEC(int) *
9301 catch_syscall_split_args (char *arg)
9302 {
9303   VEC(int) *result = NULL;
9304   struct cleanup *cleanup = make_cleanup (clean_up_filters, &result);
9305
9306   while (*arg != '\0')
9307     {
9308       int i, syscall_number;
9309       char *endptr;
9310       char cur_name[128];
9311       struct syscall s;
9312
9313       /* Skip whitespace.  */
9314       while (isspace (*arg))
9315         arg++;
9316
9317       for (i = 0; i < 127 && arg[i] && !isspace (arg[i]); ++i)
9318         cur_name[i] = arg[i];
9319       cur_name[i] = '\0';
9320       arg += i;
9321
9322       /* Check if the user provided a syscall name or a number.  */
9323       syscall_number = (int) strtol (cur_name, &endptr, 0);
9324       if (*endptr == '\0')
9325         get_syscall_by_number (syscall_number, &s);
9326       else
9327         {
9328           /* We have a name.  Let's check if it's valid and convert it
9329              to a number.  */
9330           get_syscall_by_name (cur_name, &s);
9331
9332           if (s.number == UNKNOWN_SYSCALL)
9333             /* Here we have to issue an error instead of a warning,
9334                because GDB cannot do anything useful if there's no
9335                syscall number to be caught.  */
9336             error (_("Unknown syscall name '%s'."), cur_name);
9337         }
9338
9339       /* Ok, it's valid.  */
9340       VEC_safe_push (int, result, s.number);
9341     }
9342
9343   discard_cleanups (cleanup);
9344   return result;
9345 }
9346
9347 /* Implement the "catch syscall" command.  */
9348
9349 static void
9350 catch_syscall_command_1 (char *arg, int from_tty, 
9351                          struct cmd_list_element *command)
9352 {
9353   int tempflag;
9354   VEC(int) *filter;
9355   struct syscall s;
9356   struct gdbarch *gdbarch = get_current_arch ();
9357
9358   /* Checking if the feature if supported.  */
9359   if (gdbarch_get_syscall_number_p (gdbarch) == 0)
9360     error (_("The feature 'catch syscall' is not supported on \
9361 this architecture yet."));
9362
9363   tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
9364
9365   arg = skip_spaces (arg);
9366
9367   /* We need to do this first "dummy" translation in order
9368      to get the syscall XML file loaded or, most important,
9369      to display a warning to the user if there's no XML file
9370      for his/her architecture.  */
9371   get_syscall_by_number (0, &s);
9372
9373   /* The allowed syntax is:
9374      catch syscall
9375      catch syscall <name | number> [<name | number> ... <name | number>]
9376
9377      Let's check if there's a syscall name.  */
9378
9379   if (arg != NULL)
9380     filter = catch_syscall_split_args (arg);
9381   else
9382     filter = NULL;
9383
9384   create_syscall_event_catchpoint (tempflag, filter,
9385                                    &catch_syscall_breakpoint_ops);
9386 }
9387
9388 /* Implement the "catch assert" command.  */
9389
9390 static void
9391 catch_assert_command (char *arg, int from_tty,
9392                       struct cmd_list_element *command)
9393 {
9394   struct gdbarch *gdbarch = get_current_arch ();
9395   int tempflag;
9396   struct symtab_and_line sal;
9397   char *addr_string = NULL;
9398   struct breakpoint_ops *ops = NULL;
9399
9400   tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
9401
9402   if (!arg)
9403     arg = "";
9404   sal = ada_decode_assert_location (arg, &addr_string, &ops);
9405   create_ada_exception_breakpoint (gdbarch, sal, addr_string, NULL, NULL, NULL,
9406                                    ops, tempflag, from_tty);
9407 }
9408
9409 static void
9410 catch_command (char *arg, int from_tty)
9411 {
9412   error (_("Catch requires an event name."));
9413 }
9414 \f
9415
9416 static void
9417 tcatch_command (char *arg, int from_tty)
9418 {
9419   error (_("Catch requires an event name."));
9420 }
9421
9422 /* Delete breakpoints by address or line.  */
9423
9424 static void
9425 clear_command (char *arg, int from_tty)
9426 {
9427   struct breakpoint *b;
9428   VEC(breakpoint_p) *found = 0;
9429   int ix;
9430   int default_match;
9431   struct symtabs_and_lines sals;
9432   struct symtab_and_line sal;
9433   int i;
9434
9435   if (arg)
9436     {
9437       sals = decode_line_spec (arg, 1);
9438       default_match = 0;
9439     }
9440   else
9441     {
9442       sals.sals = (struct symtab_and_line *)
9443         xmalloc (sizeof (struct symtab_and_line));
9444       make_cleanup (xfree, sals.sals);
9445       init_sal (&sal);          /* Initialize to zeroes.  */
9446       sal.line = default_breakpoint_line;
9447       sal.symtab = default_breakpoint_symtab;
9448       sal.pc = default_breakpoint_address;
9449       sal.pspace = default_breakpoint_pspace;
9450       if (sal.symtab == 0)
9451         error (_("No source file specified."));
9452
9453       sals.sals[0] = sal;
9454       sals.nelts = 1;
9455
9456       default_match = 1;
9457     }
9458
9459   /* We don't call resolve_sal_pc here.  That's not as bad as it
9460      seems, because all existing breakpoints typically have both
9461      file/line and pc set.  So, if clear is given file/line, we can
9462      match this to existing breakpoint without obtaining pc at all.
9463
9464      We only support clearing given the address explicitly 
9465      present in breakpoint table.  Say, we've set breakpoint 
9466      at file:line.  There were several PC values for that file:line,
9467      due to optimization, all in one block.
9468
9469      We've picked one PC value.  If "clear" is issued with another
9470      PC corresponding to the same file:line, the breakpoint won't
9471      be cleared.  We probably can still clear the breakpoint, but 
9472      since the other PC value is never presented to user, user
9473      can only find it by guessing, and it does not seem important
9474      to support that.  */
9475
9476   /* For each line spec given, delete bps which correspond to it.  Do
9477      it in two passes, solely to preserve the current behavior that
9478      from_tty is forced true if we delete more than one
9479      breakpoint.  */
9480
9481   found = NULL;
9482   for (i = 0; i < sals.nelts; i++)
9483     {
9484       /* If exact pc given, clear bpts at that pc.
9485          If line given (pc == 0), clear all bpts on specified line.
9486          If defaulting, clear all bpts on default line
9487          or at default pc.
9488
9489          defaulting    sal.pc != 0    tests to do
9490
9491          0              1             pc
9492          1              1             pc _and_ line
9493          0              0             line
9494          1              0             <can't happen> */
9495
9496       sal = sals.sals[i];
9497
9498       /* Find all matching breakpoints and add them to 'found'.  */
9499       ALL_BREAKPOINTS (b)
9500         {
9501           int match = 0;
9502           /* Are we going to delete b?  */
9503           if (b->type != bp_none && !is_watchpoint (b))
9504             {
9505               struct bp_location *loc = b->loc;
9506               for (; loc; loc = loc->next)
9507                 {
9508                   int pc_match = sal.pc
9509                     && (loc->pspace == sal.pspace)
9510                     && (loc->address == sal.pc)
9511                     && (!section_is_overlay (loc->section)
9512                         || loc->section == sal.section);
9513                   int line_match = ((default_match || (0 == sal.pc))
9514                                     && b->source_file != NULL
9515                                     && sal.symtab != NULL
9516                                     && sal.pspace == loc->pspace
9517                                     && strcmp (b->source_file,
9518                                                sal.symtab->filename) == 0
9519                                     && b->line_number == sal.line);
9520                   if (pc_match || line_match)
9521                     {
9522                       match = 1;
9523                       break;
9524                     }
9525                 }
9526             }
9527
9528           if (match)
9529             VEC_safe_push(breakpoint_p, found, b);
9530         }
9531     }
9532   /* Now go thru the 'found' chain and delete them.  */
9533   if (VEC_empty(breakpoint_p, found))
9534     {
9535       if (arg)
9536         error (_("No breakpoint at %s."), arg);
9537       else
9538         error (_("No breakpoint at this line."));
9539     }
9540
9541   if (VEC_length(breakpoint_p, found) > 1)
9542     from_tty = 1;       /* Always report if deleted more than one.  */
9543   if (from_tty)
9544     {
9545       if (VEC_length(breakpoint_p, found) == 1)
9546         printf_unfiltered (_("Deleted breakpoint "));
9547       else
9548         printf_unfiltered (_("Deleted breakpoints "));
9549     }
9550   breakpoints_changed ();
9551
9552   for (ix = 0; VEC_iterate(breakpoint_p, found, ix, b); ix++)
9553     {
9554       if (from_tty)
9555         printf_unfiltered ("%d ", b->number);
9556       delete_breakpoint (b);
9557     }
9558   if (from_tty)
9559     putchar_unfiltered ('\n');
9560 }
9561 \f
9562 /* Delete breakpoint in BS if they are `delete' breakpoints and
9563    all breakpoints that are marked for deletion, whether hit or not.
9564    This is called after any breakpoint is hit, or after errors.  */
9565
9566 void
9567 breakpoint_auto_delete (bpstat bs)
9568 {
9569   struct breakpoint *b, *b_tmp;
9570
9571   for (; bs; bs = bs->next)
9572     if (bs->breakpoint_at
9573         && bs->breakpoint_at->disposition == disp_del
9574         && bs->stop)
9575       delete_breakpoint (bs->breakpoint_at);
9576
9577   ALL_BREAKPOINTS_SAFE (b, b_tmp)
9578   {
9579     if (b->disposition == disp_del_at_next_stop)
9580       delete_breakpoint (b);
9581   }
9582 }
9583
9584 /* A comparison function for bp_location AP and BP being interfaced to
9585    qsort.  Sort elements primarily by their ADDRESS (no matter what
9586    does breakpoint_address_is_meaningful say for its OWNER),
9587    secondarily by ordering first bp_permanent OWNERed elements and
9588    terciarily just ensuring the array is sorted stable way despite
9589    qsort being an instable algorithm.  */
9590
9591 static int
9592 bp_location_compare (const void *ap, const void *bp)
9593 {
9594   struct bp_location *a = *(void **) ap;
9595   struct bp_location *b = *(void **) bp;
9596   /* A and B come from existing breakpoints having non-NULL OWNER.  */
9597   int a_perm = a->owner->enable_state == bp_permanent;
9598   int b_perm = b->owner->enable_state == bp_permanent;
9599
9600   if (a->address != b->address)
9601     return (a->address > b->address) - (a->address < b->address);
9602
9603   /* Sort permanent breakpoints first.  */
9604   if (a_perm != b_perm)
9605     return (a_perm < b_perm) - (a_perm > b_perm);
9606
9607   /* Make the user-visible order stable across GDB runs.  Locations of
9608      the same breakpoint can be sorted in arbitrary order.  */
9609
9610   if (a->owner->number != b->owner->number)
9611     return (a->owner->number > b->owner->number)
9612            - (a->owner->number < b->owner->number);
9613
9614   return (a > b) - (a < b);
9615 }
9616
9617 /* Set bp_location_placed_address_before_address_max and
9618    bp_location_shadow_len_after_address_max according to the current
9619    content of the bp_location array.  */
9620
9621 static void
9622 bp_location_target_extensions_update (void)
9623 {
9624   struct bp_location *bl, **blp_tmp;
9625
9626   bp_location_placed_address_before_address_max = 0;
9627   bp_location_shadow_len_after_address_max = 0;
9628
9629   ALL_BP_LOCATIONS (bl, blp_tmp)
9630     {
9631       CORE_ADDR start, end, addr;
9632
9633       if (!bp_location_has_shadow (bl))
9634         continue;
9635
9636       start = bl->target_info.placed_address;
9637       end = start + bl->target_info.shadow_len;
9638
9639       gdb_assert (bl->address >= start);
9640       addr = bl->address - start;
9641       if (addr > bp_location_placed_address_before_address_max)
9642         bp_location_placed_address_before_address_max = addr;
9643
9644       /* Zero SHADOW_LEN would not pass bp_location_has_shadow.  */
9645
9646       gdb_assert (bl->address < end);
9647       addr = end - bl->address;
9648       if (addr > bp_location_shadow_len_after_address_max)
9649         bp_location_shadow_len_after_address_max = addr;
9650     }
9651 }
9652
9653 /* If SHOULD_INSERT is false, do not insert any breakpoint locations
9654    into the inferior, only remove already-inserted locations that no
9655    longer should be inserted.  Functions that delete a breakpoint or
9656    breakpoints should pass false, so that deleting a breakpoint
9657    doesn't have the side effect of inserting the locations of other
9658    breakpoints that are marked not-inserted, but should_be_inserted
9659    returns true on them.
9660
9661    This behaviour is useful is situations close to tear-down -- e.g.,
9662    after an exec, while the target still has execution, but breakpoint
9663    shadows of the previous executable image should *NOT* be restored
9664    to the new image; or before detaching, where the target still has
9665    execution and wants to delete breakpoints from GDB's lists, and all
9666    breakpoints had already been removed from the inferior.  */
9667
9668 static void
9669 update_global_location_list (int should_insert)
9670 {
9671   struct breakpoint *b;
9672   struct bp_location **locp, *loc;
9673   struct cleanup *cleanups;
9674
9675   /* Used in the duplicates detection below.  When iterating over all
9676      bp_locations, points to the first bp_location of a given address.
9677      Breakpoints and watchpoints of different types are never
9678      duplicates of each other.  Keep one pointer for each type of
9679      breakpoint/watchpoint, so we only need to loop over all locations
9680      once.  */
9681   struct bp_location *bp_loc_first;  /* breakpoint */
9682   struct bp_location *wp_loc_first;  /* hardware watchpoint */
9683   struct bp_location *awp_loc_first; /* access watchpoint */
9684   struct bp_location *rwp_loc_first; /* read watchpoint */
9685
9686   /* Saved former bp_location array which we compare against the newly
9687      built bp_location from the current state of ALL_BREAKPOINTS.  */
9688   struct bp_location **old_location, **old_locp;
9689   unsigned old_location_count;
9690
9691   old_location = bp_location;
9692   old_location_count = bp_location_count;
9693   bp_location = NULL;
9694   bp_location_count = 0;
9695   cleanups = make_cleanup (xfree, old_location);
9696
9697   ALL_BREAKPOINTS (b)
9698     for (loc = b->loc; loc; loc = loc->next)
9699       bp_location_count++;
9700
9701   bp_location = xmalloc (sizeof (*bp_location) * bp_location_count);
9702   locp = bp_location;
9703   ALL_BREAKPOINTS (b)
9704     for (loc = b->loc; loc; loc = loc->next)
9705       *locp++ = loc;
9706   qsort (bp_location, bp_location_count, sizeof (*bp_location),
9707          bp_location_compare);
9708
9709   bp_location_target_extensions_update ();
9710
9711   /* Identify bp_location instances that are no longer present in the
9712      new list, and therefore should be freed.  Note that it's not
9713      necessary that those locations should be removed from inferior --
9714      if there's another location at the same address (previously
9715      marked as duplicate), we don't need to remove/insert the
9716      location.
9717      
9718      LOCP is kept in sync with OLD_LOCP, each pointing to the current
9719      and former bp_location array state respectively.  */
9720
9721   locp = bp_location;
9722   for (old_locp = old_location; old_locp < old_location + old_location_count;
9723        old_locp++)
9724     {
9725       struct bp_location *old_loc = *old_locp;
9726       struct bp_location **loc2p;
9727
9728       /* Tells if 'old_loc' is found amoung the new locations.  If
9729          not, we have to free it.  */
9730       int found_object = 0;
9731       /* Tells if the location should remain inserted in the target.  */
9732       int keep_in_target = 0;
9733       int removed = 0;
9734
9735       /* Skip LOCP entries which will definitely never be needed.
9736          Stop either at or being the one matching OLD_LOC.  */
9737       while (locp < bp_location + bp_location_count
9738              && (*locp)->address < old_loc->address)
9739         locp++;
9740
9741       for (loc2p = locp;
9742            (loc2p < bp_location + bp_location_count
9743             && (*loc2p)->address == old_loc->address);
9744            loc2p++)
9745         {
9746           if (*loc2p == old_loc)
9747             {
9748               found_object = 1;
9749               break;
9750             }
9751         }
9752
9753       /* If this location is no longer present, and inserted, look if
9754          there's maybe a new location at the same address.  If so,
9755          mark that one inserted, and don't remove this one.  This is
9756          needed so that we don't have a time window where a breakpoint
9757          at certain location is not inserted.  */
9758
9759       if (old_loc->inserted)
9760         {
9761           /* If the location is inserted now, we might have to remove
9762              it.  */
9763
9764           if (found_object && should_be_inserted (old_loc))
9765             {
9766               /* The location is still present in the location list,
9767                  and still should be inserted.  Don't do anything.  */
9768               keep_in_target = 1;
9769             }
9770           else
9771             {
9772               /* The location is either no longer present, or got
9773                  disabled.  See if there's another location at the
9774                  same address, in which case we don't need to remove
9775                  this one from the target.  */
9776
9777               /* OLD_LOC comes from existing struct breakpoint.  */
9778               if (breakpoint_address_is_meaningful (old_loc->owner))
9779                 {
9780                   for (loc2p = locp;
9781                        (loc2p < bp_location + bp_location_count
9782                         && (*loc2p)->address == old_loc->address);
9783                        loc2p++)
9784                     {
9785                       struct bp_location *loc2 = *loc2p;
9786
9787                       if (breakpoint_locations_match (loc2, old_loc))
9788                         {
9789                           /* For the sake of should_be_inserted.
9790                              Duplicates check below will fix up this
9791                              later.  */
9792                           loc2->duplicate = 0;
9793
9794                           /* Read watchpoint locations are switched to
9795                              access watchpoints, if the former are not
9796                              supported, but the latter are.  */
9797                           if (is_hardware_watchpoint (old_loc->owner))
9798                             {
9799                               gdb_assert (is_hardware_watchpoint (loc2->owner));
9800                               loc2->watchpoint_type = old_loc->watchpoint_type;
9801                             }
9802
9803                           if (loc2 != old_loc && should_be_inserted (loc2))
9804                             {
9805                               loc2->inserted = 1;
9806                               loc2->target_info = old_loc->target_info;
9807                               keep_in_target = 1;
9808                               break;
9809                             }
9810                         }
9811                     }
9812                 }
9813             }
9814
9815           if (!keep_in_target)
9816             {
9817               if (remove_breakpoint (old_loc, mark_uninserted))
9818                 {
9819                   /* This is just about all we can do.  We could keep
9820                      this location on the global list, and try to
9821                      remove it next time, but there's no particular
9822                      reason why we will succeed next time.
9823                      
9824                      Note that at this point, old_loc->owner is still
9825                      valid, as delete_breakpoint frees the breakpoint
9826                      only after calling us.  */
9827                   printf_filtered (_("warning: Error removing "
9828                                      "breakpoint %d\n"), 
9829                                    old_loc->owner->number);
9830                 }
9831               removed = 1;
9832             }
9833         }
9834
9835       if (!found_object)
9836         {
9837           if (removed && non_stop
9838               && breakpoint_address_is_meaningful (old_loc->owner)
9839               && !is_hardware_watchpoint (old_loc->owner))
9840             {
9841               /* This location was removed from the target.  In
9842                  non-stop mode, a race condition is possible where
9843                  we've removed a breakpoint, but stop events for that
9844                  breakpoint are already queued and will arrive later.
9845                  We apply an heuristic to be able to distinguish such
9846                  SIGTRAPs from other random SIGTRAPs: we keep this
9847                  breakpoint location for a bit, and will retire it
9848                  after we see some number of events.  The theory here
9849                  is that reporting of events should, "on the average",
9850                  be fair, so after a while we'll see events from all
9851                  threads that have anything of interest, and no longer
9852                  need to keep this breakpoint location around.  We
9853                  don't hold locations forever so to reduce chances of
9854                  mistaking a non-breakpoint SIGTRAP for a breakpoint
9855                  SIGTRAP.
9856
9857                  The heuristic failing can be disastrous on
9858                  decr_pc_after_break targets.
9859
9860                  On decr_pc_after_break targets, like e.g., x86-linux,
9861                  if we fail to recognize a late breakpoint SIGTRAP,
9862                  because events_till_retirement has reached 0 too
9863                  soon, we'll fail to do the PC adjustment, and report
9864                  a random SIGTRAP to the user.  When the user resumes
9865                  the inferior, it will most likely immediately crash
9866                  with SIGILL/SIGBUS/SIGSEGV, or worse, get silently
9867                  corrupted, because of being resumed e.g., in the
9868                  middle of a multi-byte instruction, or skipped a
9869                  one-byte instruction.  This was actually seen happen
9870                  on native x86-linux, and should be less rare on
9871                  targets that do not support new thread events, like
9872                  remote, due to the heuristic depending on
9873                  thread_count.
9874
9875                  Mistaking a random SIGTRAP for a breakpoint trap
9876                  causes similar symptoms (PC adjustment applied when
9877                  it shouldn't), but then again, playing with SIGTRAPs
9878                  behind the debugger's back is asking for trouble.
9879
9880                  Since hardware watchpoint traps are always
9881                  distinguishable from other traps, so we don't need to
9882                  apply keep hardware watchpoint moribund locations
9883                  around.  We simply always ignore hardware watchpoint
9884                  traps we can no longer explain.  */
9885
9886               old_loc->events_till_retirement = 3 * (thread_count () + 1);
9887               old_loc->owner = NULL;
9888
9889               VEC_safe_push (bp_location_p, moribund_locations, old_loc);
9890             }
9891           else
9892             {
9893               old_loc->owner = NULL;
9894               decref_bp_location (&old_loc);
9895             }
9896         }
9897     }
9898
9899   /* Rescan breakpoints at the same address and section, marking the
9900      first one as "first" and any others as "duplicates".  This is so
9901      that the bpt instruction is only inserted once.  If we have a
9902      permanent breakpoint at the same place as BPT, make that one the
9903      official one, and the rest as duplicates.  Permanent breakpoints
9904      are sorted first for the same address.
9905
9906      Do the same for hardware watchpoints, but also considering the
9907      watchpoint's type (regular/access/read) and length.  */
9908
9909   bp_loc_first = NULL;
9910   wp_loc_first = NULL;
9911   awp_loc_first = NULL;
9912   rwp_loc_first = NULL;
9913   ALL_BP_LOCATIONS (loc, locp)
9914     {
9915       /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always
9916          non-NULL.  */
9917       struct breakpoint *b = loc->owner;
9918       struct bp_location **loc_first_p;
9919
9920       if (b->enable_state == bp_disabled
9921           || b->enable_state == bp_call_disabled
9922           || b->enable_state == bp_startup_disabled
9923           || !loc->enabled
9924           || loc->shlib_disabled
9925           || !breakpoint_address_is_meaningful (b)
9926           || is_tracepoint (b))
9927         continue;
9928
9929       /* Permanent breakpoint should always be inserted.  */
9930       if (b->enable_state == bp_permanent && ! loc->inserted)
9931         internal_error (__FILE__, __LINE__,
9932                         _("allegedly permanent breakpoint is not "
9933                         "actually inserted"));
9934
9935       if (b->type == bp_hardware_watchpoint)
9936         loc_first_p = &wp_loc_first;
9937       else if (b->type == bp_read_watchpoint)
9938         loc_first_p = &rwp_loc_first;
9939       else if (b->type == bp_access_watchpoint)
9940         loc_first_p = &awp_loc_first;
9941       else
9942         loc_first_p = &bp_loc_first;
9943
9944       if (*loc_first_p == NULL
9945           || (overlay_debugging && loc->section != (*loc_first_p)->section)
9946           || !breakpoint_locations_match (loc, *loc_first_p))
9947         {
9948           *loc_first_p = loc;
9949           loc->duplicate = 0;
9950           continue;
9951         }
9952
9953       loc->duplicate = 1;
9954
9955       if ((*loc_first_p)->owner->enable_state == bp_permanent && loc->inserted
9956           && b->enable_state != bp_permanent)
9957         internal_error (__FILE__, __LINE__,
9958                         _("another breakpoint was inserted on top of "
9959                         "a permanent breakpoint"));
9960     }
9961
9962   if (breakpoints_always_inserted_mode () && should_insert
9963       && (have_live_inferiors ()
9964           || (gdbarch_has_global_breakpoints (target_gdbarch))))
9965     insert_breakpoint_locations ();
9966
9967   do_cleanups (cleanups);
9968 }
9969
9970 void
9971 breakpoint_retire_moribund (void)
9972 {
9973   struct bp_location *loc;
9974   int ix;
9975
9976   for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, loc); ++ix)
9977     if (--(loc->events_till_retirement) == 0)
9978       {
9979         decref_bp_location (&loc);
9980         VEC_unordered_remove (bp_location_p, moribund_locations, ix);
9981         --ix;
9982       }
9983 }
9984
9985 static void
9986 update_global_location_list_nothrow (int inserting)
9987 {
9988   struct gdb_exception e;
9989
9990   TRY_CATCH (e, RETURN_MASK_ERROR)
9991     update_global_location_list (inserting);
9992 }
9993
9994 /* Clear BKP from a BPS.  */
9995
9996 static void
9997 bpstat_remove_bp_location (bpstat bps, struct breakpoint *bpt)
9998 {
9999   bpstat bs;
10000
10001   for (bs = bps; bs; bs = bs->next)
10002     if (bs->breakpoint_at == bpt)
10003       {
10004         bs->breakpoint_at = NULL;
10005         bs->old_val = NULL;
10006         /* bs->commands will be freed later.  */
10007       }
10008 }
10009
10010 /* Callback for iterate_over_threads.  */
10011 static int
10012 bpstat_remove_breakpoint_callback (struct thread_info *th, void *data)
10013 {
10014   struct breakpoint *bpt = data;
10015
10016   bpstat_remove_bp_location (th->control.stop_bpstat, bpt);
10017   return 0;
10018 }
10019
10020 /* Delete a breakpoint and clean up all traces of it in the data
10021    structures.  */
10022
10023 void
10024 delete_breakpoint (struct breakpoint *bpt)
10025 {
10026   struct breakpoint *b;
10027
10028   gdb_assert (bpt != NULL);
10029
10030   /* Has this bp already been deleted?  This can happen because
10031      multiple lists can hold pointers to bp's.  bpstat lists are
10032      especial culprits.
10033
10034      One example of this happening is a watchpoint's scope bp.  When
10035      the scope bp triggers, we notice that the watchpoint is out of
10036      scope, and delete it.  We also delete its scope bp.  But the
10037      scope bp is marked "auto-deleting", and is already on a bpstat.
10038      That bpstat is then checked for auto-deleting bp's, which are
10039      deleted.
10040
10041      A real solution to this problem might involve reference counts in
10042      bp's, and/or giving them pointers back to their referencing
10043      bpstat's, and teaching delete_breakpoint to only free a bp's
10044      storage when no more references were extent.  A cheaper bandaid
10045      was chosen.  */
10046   if (bpt->type == bp_none)
10047     return;
10048
10049   /* At least avoid this stale reference until the reference counting
10050      of breakpoints gets resolved.  */
10051   if (bpt->related_breakpoint != NULL)
10052     {
10053       gdb_assert (bpt->related_breakpoint->related_breakpoint == bpt);
10054       bpt->related_breakpoint->disposition = disp_del_at_next_stop;
10055       bpt->related_breakpoint->related_breakpoint = NULL;
10056       bpt->related_breakpoint = NULL;
10057     }
10058
10059   observer_notify_breakpoint_deleted (bpt->number);
10060
10061   if (breakpoint_chain == bpt)
10062     breakpoint_chain = bpt->next;
10063
10064   ALL_BREAKPOINTS (b)
10065     if (b->next == bpt)
10066     {
10067       b->next = bpt->next;
10068       break;
10069     }
10070
10071   decref_counted_command_line (&bpt->commands);
10072   xfree (bpt->cond_string);
10073   xfree (bpt->cond_exp);
10074   xfree (bpt->addr_string);
10075   xfree (bpt->exp);
10076   xfree (bpt->exp_string);
10077   xfree (bpt->exp_string_reparse);
10078   value_free (bpt->val);
10079   xfree (bpt->source_file);
10080   xfree (bpt->exec_pathname);
10081   clean_up_filters (&bpt->syscalls_to_be_caught);
10082
10083
10084   /* Be sure no bpstat's are pointing at the breakpoint after it's
10085      been freed.  */
10086   /* FIXME, how can we find all bpstat's?  We just check stop_bpstat
10087      in all threeds for now.  Note that we cannot just remove bpstats
10088      pointing at bpt from the stop_bpstat list entirely, as breakpoint
10089      commands are associated with the bpstat; if we remove it here,
10090      then the later call to bpstat_do_actions (&stop_bpstat); in
10091      event-top.c won't do anything, and temporary breakpoints with
10092      commands won't work.  */
10093
10094   iterate_over_threads (bpstat_remove_breakpoint_callback, bpt);
10095
10096   /* Now that breakpoint is removed from breakpoint list, update the
10097      global location list.  This will remove locations that used to
10098      belong to this breakpoint.  Do this before freeing the breakpoint
10099      itself, since remove_breakpoint looks at location's owner.  It
10100      might be better design to have location completely
10101      self-contained, but it's not the case now.  */
10102   update_global_location_list (0);
10103
10104
10105   /* On the chance that someone will soon try again to delete this
10106      same bp, we mark it as deleted before freeing its storage.  */
10107   bpt->type = bp_none;
10108
10109   xfree (bpt);
10110 }
10111
10112 static void
10113 do_delete_breakpoint_cleanup (void *b)
10114 {
10115   delete_breakpoint (b);
10116 }
10117
10118 struct cleanup *
10119 make_cleanup_delete_breakpoint (struct breakpoint *b)
10120 {
10121   return make_cleanup (do_delete_breakpoint_cleanup, b);
10122 }
10123
10124 /* A callback for map_breakpoint_numbers that calls
10125    delete_breakpoint.  */
10126
10127 static void
10128 do_delete_breakpoint (struct breakpoint *b, void *ignore)
10129 {
10130   delete_breakpoint (b);
10131 }
10132
10133 void
10134 delete_command (char *arg, int from_tty)
10135 {
10136   struct breakpoint *b, *b_tmp;
10137
10138   dont_repeat ();
10139
10140   if (arg == 0)
10141     {
10142       int breaks_to_delete = 0;
10143
10144       /* Delete all breakpoints if no argument.
10145          Do not delete internal or call-dummy breakpoints, these have
10146          to be deleted with an explicit breakpoint number argument.  */
10147       ALL_BREAKPOINTS (b)
10148       {
10149         if (b->type != bp_call_dummy
10150             && b->type != bp_std_terminate
10151             && b->type != bp_shlib_event
10152             && b->type != bp_jit_event
10153             && b->type != bp_thread_event
10154             && b->type != bp_overlay_event
10155             && b->type != bp_longjmp_master
10156             && b->type != bp_std_terminate_master
10157             && b->type != bp_exception_master
10158             && b->number >= 0)
10159           {
10160             breaks_to_delete = 1;
10161             break;
10162           }
10163       }
10164
10165       /* Ask user only if there are some breakpoints to delete.  */
10166       if (!from_tty
10167           || (breaks_to_delete && query (_("Delete all breakpoints? "))))
10168         {
10169           ALL_BREAKPOINTS_SAFE (b, b_tmp)
10170           {
10171             if (b->type != bp_call_dummy
10172                 && b->type != bp_std_terminate
10173                 && b->type != bp_shlib_event
10174                 && b->type != bp_thread_event
10175                 && b->type != bp_jit_event
10176                 && b->type != bp_overlay_event
10177                 && b->type != bp_longjmp_master
10178                 && b->type != bp_std_terminate_master
10179                 && b->type != bp_exception_master
10180                 && b->number >= 0)
10181               delete_breakpoint (b);
10182           }
10183         }
10184     }
10185   else
10186     map_breakpoint_numbers (arg, do_delete_breakpoint, NULL);
10187 }
10188
10189 static int
10190 all_locations_are_pending (struct bp_location *loc)
10191 {
10192   for (; loc; loc = loc->next)
10193     if (!loc->shlib_disabled)
10194       return 0;
10195   return 1;
10196 }
10197
10198 /* Subroutine of update_breakpoint_locations to simplify it.
10199    Return non-zero if multiple fns in list LOC have the same name.
10200    Null names are ignored.  */
10201
10202 static int
10203 ambiguous_names_p (struct bp_location *loc)
10204 {
10205   struct bp_location *l;
10206   htab_t htab = htab_create_alloc (13, htab_hash_string,
10207                                    (int (*) (const void *, 
10208                                              const void *)) streq,
10209                                    NULL, xcalloc, xfree);
10210
10211   for (l = loc; l != NULL; l = l->next)
10212     {
10213       const char **slot;
10214       const char *name = l->function_name;
10215
10216       /* Allow for some names to be NULL, ignore them.  */
10217       if (name == NULL)
10218         continue;
10219
10220       slot = (const char **) htab_find_slot (htab, (const void *) name,
10221                                              INSERT);
10222       /* NOTE: We can assume slot != NULL here because xcalloc never
10223          returns NULL.  */
10224       if (*slot != NULL)
10225         {
10226           htab_delete (htab);
10227           return 1;
10228         }
10229       *slot = name;
10230     }
10231
10232   htab_delete (htab);
10233   return 0;
10234 }
10235
10236 /* When symbols change, it probably means the sources changed as well,
10237    and it might mean the static tracepoint markers are no longer at
10238    the same address or line numbers they used to be at last we
10239    checked.  Losing your static tracepoints whenever you rebuild is
10240    undesirable.  This function tries to resync/rematch gdb static
10241    tracepoints with the markers on the target, for static tracepoints
10242    that have not been set by marker id.  Static tracepoint that have
10243    been set by marker id are reset by marker id in breakpoint_re_set.
10244    The heuristic is:
10245
10246    1) For a tracepoint set at a specific address, look for a marker at
10247    the old PC.  If one is found there, assume to be the same marker.
10248    If the name / string id of the marker found is different from the
10249    previous known name, assume that means the user renamed the marker
10250    in the sources, and output a warning.
10251
10252    2) For a tracepoint set at a given line number, look for a marker
10253    at the new address of the old line number.  If one is found there,
10254    assume to be the same marker.  If the name / string id of the
10255    marker found is different from the previous known name, assume that
10256    means the user renamed the marker in the sources, and output a
10257    warning.
10258
10259    3) If a marker is no longer found at the same address or line, it
10260    may mean the marker no longer exists.  But it may also just mean
10261    the code changed a bit.  Maybe the user added a few lines of code
10262    that made the marker move up or down (in line number terms).  Ask
10263    the target for info about the marker with the string id as we knew
10264    it.  If found, update line number and address in the matching
10265    static tracepoint.  This will get confused if there's more than one
10266    marker with the same ID (possible in UST, although unadvised
10267    precisely because it confuses tools).  */
10268
10269 static struct symtab_and_line
10270 update_static_tracepoint (struct breakpoint *b, struct symtab_and_line sal)
10271 {
10272   struct static_tracepoint_marker marker;
10273   CORE_ADDR pc;
10274   int i;
10275
10276   pc = sal.pc;
10277   if (sal.line)
10278     find_line_pc (sal.symtab, sal.line, &pc);
10279
10280   if (target_static_tracepoint_marker_at (pc, &marker))
10281     {
10282       if (strcmp (b->static_trace_marker_id, marker.str_id) != 0)
10283         warning (_("static tracepoint %d changed probed marker from %s to %s"),
10284                  b->number,
10285                  b->static_trace_marker_id, marker.str_id);
10286
10287       xfree (b->static_trace_marker_id);
10288       b->static_trace_marker_id = xstrdup (marker.str_id);
10289       release_static_tracepoint_marker (&marker);
10290
10291       return sal;
10292     }
10293
10294   /* Old marker wasn't found on target at lineno.  Try looking it up
10295      by string ID.  */
10296   if (!sal.explicit_pc
10297       && sal.line != 0
10298       && sal.symtab != NULL
10299       && b->static_trace_marker_id != NULL)
10300     {
10301       VEC(static_tracepoint_marker_p) *markers;
10302
10303       markers
10304         = target_static_tracepoint_markers_by_strid (b->static_trace_marker_id);
10305
10306       if (!VEC_empty(static_tracepoint_marker_p, markers))
10307         {
10308           struct symtab_and_line sal;
10309           struct symbol *sym;
10310           struct static_tracepoint_marker *marker;
10311
10312           marker = VEC_index (static_tracepoint_marker_p, markers, 0);
10313
10314           xfree (b->static_trace_marker_id);
10315           b->static_trace_marker_id = xstrdup (marker->str_id);
10316
10317           warning (_("marker for static tracepoint %d (%s) not "
10318                      "found at previous line number"),
10319                    b->number, b->static_trace_marker_id);
10320
10321           init_sal (&sal);
10322
10323           sal.pc = marker->address;
10324
10325           sal = find_pc_line (marker->address, 0);
10326           sym = find_pc_sect_function (marker->address, NULL);
10327           ui_out_text (uiout, "Now in ");
10328           if (sym)
10329             {
10330               ui_out_field_string (uiout, "func",
10331                                    SYMBOL_PRINT_NAME (sym));
10332               ui_out_text (uiout, " at ");
10333             }
10334           ui_out_field_string (uiout, "file", sal.symtab->filename);
10335           ui_out_text (uiout, ":");
10336
10337           if (ui_out_is_mi_like_p (uiout))
10338             {
10339               char *fullname = symtab_to_fullname (sal.symtab);
10340
10341               if (fullname)
10342                 ui_out_field_string (uiout, "fullname", fullname);
10343             }
10344
10345           ui_out_field_int (uiout, "line", sal.line);
10346           ui_out_text (uiout, "\n");
10347
10348           b->line_number = sal.line;
10349
10350           xfree (b->source_file);
10351           if (sym)
10352             b->source_file = xstrdup (sal.symtab->filename);
10353           else
10354             b->source_file = NULL;
10355
10356           xfree (b->addr_string);
10357           b->addr_string = xstrprintf ("%s:%d",
10358                                        sal.symtab->filename, b->line_number);
10359
10360           /* Might be nice to check if function changed, and warn if
10361              so.  */
10362
10363           release_static_tracepoint_marker (marker);
10364         }
10365     }
10366   return sal;
10367 }
10368
10369 static void
10370 update_breakpoint_locations (struct breakpoint *b,
10371                              struct symtabs_and_lines sals)
10372 {
10373   int i;
10374   char *s;
10375   struct bp_location *existing_locations = b->loc;
10376
10377   /* If there's no new locations, and all existing locations are
10378      pending, don't do anything.  This optimizes the common case where
10379      all locations are in the same shared library, that was unloaded.
10380      We'd like to retain the location, so that when the library is
10381      loaded again, we don't loose the enabled/disabled status of the
10382      individual locations.  */
10383   if (all_locations_are_pending (existing_locations) && sals.nelts == 0)
10384     return;
10385
10386   b->loc = NULL;
10387
10388   for (i = 0; i < sals.nelts; ++i)
10389     {
10390       struct bp_location *new_loc = 
10391         add_location_to_breakpoint (b, &(sals.sals[i]));
10392
10393       /* Reparse conditions, they might contain references to the
10394          old symtab.  */
10395       if (b->cond_string != NULL)
10396         {
10397           struct gdb_exception e;
10398
10399           s = b->cond_string;
10400           TRY_CATCH (e, RETURN_MASK_ERROR)
10401             {
10402               new_loc->cond = parse_exp_1 (&s, block_for_pc (sals.sals[i].pc), 
10403                                            0);
10404             }
10405           if (e.reason < 0)
10406             {
10407               warning (_("failed to reevaluate condition "
10408                          "for breakpoint %d: %s"), 
10409                        b->number, e.message);
10410               new_loc->enabled = 0;
10411             }
10412         }
10413
10414       if (b->source_file != NULL)
10415         xfree (b->source_file);
10416       if (sals.sals[i].symtab == NULL)
10417         b->source_file = NULL;
10418       else
10419         b->source_file = xstrdup (sals.sals[i].symtab->filename);
10420
10421       if (b->line_number == 0)
10422         b->line_number = sals.sals[i].line;
10423     }
10424
10425   /* Update locations of permanent breakpoints.  */
10426   if (b->enable_state == bp_permanent)
10427     make_breakpoint_permanent (b);
10428
10429   /* If possible, carry over 'disable' status from existing
10430      breakpoints.  */
10431   {
10432     struct bp_location *e = existing_locations;
10433     /* If there are multiple breakpoints with the same function name,
10434        e.g. for inline functions, comparing function names won't work.
10435        Instead compare pc addresses; this is just a heuristic as things
10436        may have moved, but in practice it gives the correct answer
10437        often enough until a better solution is found.  */
10438     int have_ambiguous_names = ambiguous_names_p (b->loc);
10439
10440     for (; e; e = e->next)
10441       {
10442         if (!e->enabled && e->function_name)
10443           {
10444             struct bp_location *l = b->loc;
10445             if (have_ambiguous_names)
10446               {
10447                 for (; l; l = l->next)
10448                   if (breakpoint_address_match (e->pspace->aspace, e->address,
10449                                                 l->pspace->aspace, l->address))
10450                     {
10451                       l->enabled = 0;
10452                       break;
10453                     }
10454               }
10455             else
10456               {
10457                 for (; l; l = l->next)
10458                   if (l->function_name
10459                       && strcmp (e->function_name, l->function_name) == 0)
10460                     {
10461                       l->enabled = 0;
10462                       break;
10463                     }
10464               }
10465           }
10466       }
10467   }
10468
10469   update_global_location_list (1);
10470 }
10471
10472 /* Reset a breakpoint given it's struct breakpoint * BINT.
10473    The value we return ends up being the return value from catch_errors.
10474    Unused in this case.  */
10475
10476 static int
10477 breakpoint_re_set_one (void *bint)
10478 {
10479   /* Get past catch_errs.  */
10480   struct breakpoint *b = (struct breakpoint *) bint;
10481   int not_found = 0;
10482   int *not_found_ptr = &not_found;
10483   struct symtabs_and_lines sals = {0};
10484   struct symtabs_and_lines expanded = {0};
10485   char *s;
10486   struct gdb_exception e;
10487   struct cleanup *cleanups = make_cleanup (null_cleanup, NULL);
10488   int marker_spec = 0;
10489
10490   switch (b->type)
10491     {
10492     case bp_none:
10493       warning (_("attempted to reset apparently deleted breakpoint #%d?"),
10494                b->number);
10495       return 0;
10496     case bp_breakpoint:
10497     case bp_hardware_breakpoint:
10498     case bp_tracepoint:
10499     case bp_fast_tracepoint:
10500     case bp_static_tracepoint:
10501       /* Do not attempt to re-set breakpoints disabled during startup.  */
10502       if (b->enable_state == bp_startup_disabled)
10503         return 0;
10504
10505       if (b->addr_string == NULL)
10506         {
10507           /* Anything without a string can't be re-set.  */
10508           delete_breakpoint (b);
10509           return 0;
10510         }
10511
10512       input_radix = b->input_radix;
10513       s = b->addr_string;
10514
10515       save_current_space_and_thread ();
10516       switch_to_program_space_and_thread (b->pspace);
10517
10518       marker_spec = b->type == bp_static_tracepoint && is_marker_spec (s);
10519
10520       set_language (b->language);
10521       TRY_CATCH (e, RETURN_MASK_ERROR)
10522         {
10523           if (marker_spec)
10524             {
10525               sals = decode_static_tracepoint_spec (&s);
10526               if (sals.nelts > b->static_trace_marker_id_idx)
10527                 {
10528                   sals.sals[0] = sals.sals[b->static_trace_marker_id_idx];
10529                   sals.nelts = 1;
10530                 }
10531               else
10532                 error (_("marker %s not found"), b->static_trace_marker_id);
10533             }
10534           else
10535             sals = decode_line_1 (&s, 1, (struct symtab *) NULL, 0,
10536                                   (char ***) NULL, not_found_ptr);
10537         }
10538       if (e.reason < 0)
10539         {
10540           int not_found_and_ok = 0;
10541           /* For pending breakpoints, it's expected that parsing will
10542              fail until the right shared library is loaded.  User has
10543              already told to create pending breakpoints and don't need
10544              extra messages.  If breakpoint is in bp_shlib_disabled
10545              state, then user already saw the message about that
10546              breakpoint being disabled, and don't want to see more
10547              errors.  */
10548           if (not_found 
10549               && (b->condition_not_parsed 
10550                   || (b->loc && b->loc->shlib_disabled)
10551                   || b->enable_state == bp_disabled))
10552             not_found_and_ok = 1;
10553
10554           if (!not_found_and_ok)
10555             {
10556               /* We surely don't want to warn about the same breakpoint
10557                  10 times.  One solution, implemented here, is disable
10558                  the breakpoint on error.  Another solution would be to
10559                  have separate 'warning emitted' flag.  Since this
10560                  happens only when a binary has changed, I don't know
10561                  which approach is better.  */
10562               b->enable_state = bp_disabled;
10563               throw_exception (e);
10564             }
10565         }
10566
10567       if (!not_found)
10568         {
10569           gdb_assert (sals.nelts == 1);
10570
10571           resolve_sal_pc (&sals.sals[0]);
10572           if (b->condition_not_parsed && s && s[0])
10573             {
10574               char *cond_string = 0;
10575               int thread = -1;
10576               int task = 0;
10577
10578               find_condition_and_thread (s, sals.sals[0].pc,
10579                                          &cond_string, &thread, &task);
10580               if (cond_string)
10581                 b->cond_string = cond_string;
10582               b->thread = thread;
10583               b->task = task;
10584               b->condition_not_parsed = 0;
10585             }
10586
10587           if (b->type == bp_static_tracepoint && !marker_spec)
10588             sals.sals[0] = update_static_tracepoint (b, sals.sals[0]);
10589
10590           expanded = expand_line_sal_maybe (sals.sals[0]);
10591         }
10592
10593       make_cleanup (xfree, sals.sals);
10594       update_breakpoint_locations (b, expanded);
10595       break;
10596
10597     case bp_watchpoint:
10598     case bp_hardware_watchpoint:
10599     case bp_read_watchpoint:
10600     case bp_access_watchpoint:
10601       /* Watchpoint can be either on expression using entirely global
10602          variables, or it can be on local variables.
10603
10604          Watchpoints of the first kind are never auto-deleted, and
10605          even persist across program restarts. Since they can use
10606          variables from shared libraries, we need to reparse
10607          expression as libraries are loaded and unloaded.
10608
10609          Watchpoints on local variables can also change meaning as
10610          result of solib event.  For example, if a watchpoint uses
10611          both a local and a global variables in expression, it's a
10612          local watchpoint, but unloading of a shared library will make
10613          the expression invalid.  This is not a very common use case,
10614          but we still re-evaluate expression, to avoid surprises to
10615          the user.
10616
10617          Note that for local watchpoints, we re-evaluate it only if
10618          watchpoints frame id is still valid.  If it's not, it means
10619          the watchpoint is out of scope and will be deleted soon.  In
10620          fact, I'm not sure we'll ever be called in this case.
10621
10622          If a local watchpoint's frame id is still valid, then
10623          b->exp_valid_block is likewise valid, and we can safely use it.
10624          
10625          Don't do anything about disabled watchpoints, since they will
10626          be reevaluated again when enabled.  */
10627       update_watchpoint (b, 1 /* reparse */);
10628       break;
10629       /* We needn't really do anything to reset these, since the mask
10630          that requests them is unaffected by e.g., new libraries being
10631          loaded.  */
10632     case bp_catchpoint:
10633       break;
10634
10635     default:
10636       printf_filtered (_("Deleting unknown breakpoint type %d\n"), b->type);
10637       /* fall through */
10638       /* Delete overlay event and longjmp master breakpoints; they will be
10639          reset later by breakpoint_re_set.  */
10640     case bp_overlay_event:
10641     case bp_longjmp_master:
10642     case bp_std_terminate_master:
10643     case bp_exception_master:
10644       delete_breakpoint (b);
10645       break;
10646
10647       /* This breakpoint is special, it's set up when the inferior
10648          starts and we really don't want to touch it.  */
10649     case bp_shlib_event:
10650
10651       /* Like bp_shlib_event, this breakpoint type is special.
10652          Once it is set up, we do not want to touch it.  */
10653     case bp_thread_event:
10654
10655       /* Keep temporary breakpoints, which can be encountered when we
10656          step over a dlopen call and SOLIB_ADD is resetting the
10657          breakpoints.  Otherwise these should have been blown away via
10658          the cleanup chain or by breakpoint_init_inferior when we
10659          rerun the executable.  */
10660     case bp_until:
10661     case bp_finish:
10662     case bp_watchpoint_scope:
10663     case bp_call_dummy:
10664     case bp_std_terminate:
10665     case bp_step_resume:
10666     case bp_longjmp:
10667     case bp_longjmp_resume:
10668     case bp_exception:
10669     case bp_exception_resume:
10670     case bp_jit_event:
10671       break;
10672     }
10673
10674   do_cleanups (cleanups);
10675   return 0;
10676 }
10677
10678 /* Re-set all breakpoints after symbols have been re-loaded.  */
10679 void
10680 breakpoint_re_set (void)
10681 {
10682   struct breakpoint *b, *b_tmp;
10683   enum language save_language;
10684   int save_input_radix;
10685   struct cleanup *old_chain;
10686
10687   save_language = current_language->la_language;
10688   save_input_radix = input_radix;
10689   old_chain = save_current_program_space ();
10690
10691   ALL_BREAKPOINTS_SAFE (b, b_tmp)
10692   {
10693     /* Format possible error msg.  */
10694     char *message = xstrprintf ("Error in re-setting breakpoint %d: ",
10695                                 b->number);
10696     struct cleanup *cleanups = make_cleanup (xfree, message);
10697     catch_errors (breakpoint_re_set_one, b, message, RETURN_MASK_ALL);
10698     do_cleanups (cleanups);
10699   }
10700   set_language (save_language);
10701   input_radix = save_input_radix;
10702
10703   jit_breakpoint_re_set ();
10704
10705   do_cleanups (old_chain);
10706
10707   create_overlay_event_breakpoint ();
10708   create_longjmp_master_breakpoint ();
10709   create_std_terminate_master_breakpoint ();
10710   create_exception_master_breakpoint ();
10711 }
10712 \f
10713 /* Reset the thread number of this breakpoint:
10714
10715    - If the breakpoint is for all threads, leave it as-is.
10716    - Else, reset it to the current thread for inferior_ptid.  */
10717 void
10718 breakpoint_re_set_thread (struct breakpoint *b)
10719 {
10720   if (b->thread != -1)
10721     {
10722       if (in_thread_list (inferior_ptid))
10723         b->thread = pid_to_thread_id (inferior_ptid);
10724
10725       /* We're being called after following a fork.  The new fork is
10726          selected as current, and unless this was a vfork will have a
10727          different program space from the original thread.  Reset that
10728          as well.  */
10729       b->loc->pspace = current_program_space;
10730     }
10731 }
10732
10733 /* Set ignore-count of breakpoint number BPTNUM to COUNT.
10734    If from_tty is nonzero, it prints a message to that effect,
10735    which ends with a period (no newline).  */
10736
10737 void
10738 set_ignore_count (int bptnum, int count, int from_tty)
10739 {
10740   struct breakpoint *b;
10741
10742   if (count < 0)
10743     count = 0;
10744
10745   ALL_BREAKPOINTS (b)
10746     if (b->number == bptnum)
10747     {
10748       if (is_tracepoint (b))
10749         {
10750           if (from_tty && count != 0)
10751             printf_filtered (_("Ignore count ignored for tracepoint %d."),
10752                              bptnum);
10753           return;
10754         }
10755       
10756       b->ignore_count = count;
10757       if (from_tty)
10758         {
10759           if (count == 0)
10760             printf_filtered (_("Will stop next time "
10761                                "breakpoint %d is reached."),
10762                              bptnum);
10763           else if (count == 1)
10764             printf_filtered (_("Will ignore next crossing of breakpoint %d."),
10765                              bptnum);
10766           else
10767             printf_filtered (_("Will ignore next %d "
10768                                "crossings of breakpoint %d."),
10769                              count, bptnum);
10770         }
10771       breakpoints_changed ();
10772       observer_notify_breakpoint_modified (b->number);
10773       return;
10774     }
10775
10776   error (_("No breakpoint number %d."), bptnum);
10777 }
10778
10779 /* Command to set ignore-count of breakpoint N to COUNT.  */
10780
10781 static void
10782 ignore_command (char *args, int from_tty)
10783 {
10784   char *p = args;
10785   int num;
10786
10787   if (p == 0)
10788     error_no_arg (_("a breakpoint number"));
10789
10790   num = get_number (&p);
10791   if (num == 0)
10792     error (_("bad breakpoint number: '%s'"), args);
10793   if (*p == 0)
10794     error (_("Second argument (specified ignore-count) is missing."));
10795
10796   set_ignore_count (num,
10797                     longest_to_int (value_as_long (parse_and_eval (p))),
10798                     from_tty);
10799   if (from_tty)
10800     printf_filtered ("\n");
10801 }
10802 \f
10803 /* Call FUNCTION on each of the breakpoints
10804    whose numbers are given in ARGS.  */
10805
10806 static void
10807 map_breakpoint_numbers (char *args, void (*function) (struct breakpoint *,
10808                                                       void *),
10809                         void *data)
10810 {
10811   char *p = args;
10812   char *p1;
10813   int num;
10814   struct breakpoint *b, *tmp;
10815   int match;
10816
10817   if (p == 0)
10818     error_no_arg (_("one or more breakpoint numbers"));
10819
10820   while (*p)
10821     {
10822       match = 0;
10823       p1 = p;
10824
10825       num = get_number_or_range (&p1);
10826       if (num == 0)
10827         {
10828           warning (_("bad breakpoint number at or near '%s'"), p);
10829         }
10830       else
10831         {
10832           ALL_BREAKPOINTS_SAFE (b, tmp)
10833             if (b->number == num)
10834               {
10835                 struct breakpoint *related_breakpoint = b->related_breakpoint;
10836                 match = 1;
10837                 function (b, data);
10838                 if (related_breakpoint)
10839                   function (related_breakpoint, data);
10840                 break;
10841               }
10842           if (match == 0)
10843             printf_unfiltered (_("No breakpoint number %d.\n"), num);
10844         }
10845       p = p1;
10846     }
10847 }
10848
10849 static struct bp_location *
10850 find_location_by_number (char *number)
10851 {
10852   char *dot = strchr (number, '.');
10853   char *p1;
10854   int bp_num;
10855   int loc_num;
10856   struct breakpoint *b;
10857   struct bp_location *loc;  
10858
10859   *dot = '\0';
10860
10861   p1 = number;
10862   bp_num = get_number_or_range (&p1);
10863   if (bp_num == 0)
10864     error (_("Bad breakpoint number '%s'"), number);
10865
10866   ALL_BREAKPOINTS (b)
10867     if (b->number == bp_num)
10868       {
10869         break;
10870       }
10871
10872   if (!b || b->number != bp_num)
10873     error (_("Bad breakpoint number '%s'"), number);
10874   
10875   p1 = dot+1;
10876   loc_num = get_number_or_range (&p1);
10877   if (loc_num == 0)
10878     error (_("Bad breakpoint location number '%s'"), number);
10879
10880   --loc_num;
10881   loc = b->loc;
10882   for (;loc_num && loc; --loc_num, loc = loc->next)
10883     ;
10884   if (!loc)
10885     error (_("Bad breakpoint location number '%s'"), dot+1);
10886     
10887   return loc;  
10888 }
10889
10890
10891 /* Set ignore-count of breakpoint number BPTNUM to COUNT.
10892    If from_tty is nonzero, it prints a message to that effect,
10893    which ends with a period (no newline).  */
10894
10895 void
10896 disable_breakpoint (struct breakpoint *bpt)
10897 {
10898   /* Never disable a watchpoint scope breakpoint; we want to
10899      hit them when we leave scope so we can delete both the
10900      watchpoint and its scope breakpoint at that time.  */
10901   if (bpt->type == bp_watchpoint_scope)
10902     return;
10903
10904   /* You can't disable permanent breakpoints.  */
10905   if (bpt->enable_state == bp_permanent)
10906     return;
10907
10908   bpt->enable_state = bp_disabled;
10909
10910   update_global_location_list (0);
10911
10912   observer_notify_breakpoint_modified (bpt->number);
10913 }
10914
10915 /* A callback for map_breakpoint_numbers that calls
10916    disable_breakpoint.  */
10917
10918 static void
10919 do_map_disable_breakpoint (struct breakpoint *b, void *ignore)
10920 {
10921   disable_breakpoint (b);
10922 }
10923
10924 static void
10925 disable_command (char *args, int from_tty)
10926 {
10927   struct breakpoint *bpt;
10928
10929   if (args == 0)
10930     ALL_BREAKPOINTS (bpt)
10931       switch (bpt->type)
10932       {
10933       case bp_none:
10934         warning (_("attempted to disable apparently deleted breakpoint #%d?"),
10935                  bpt->number);
10936         continue;
10937       case bp_breakpoint:
10938       case bp_tracepoint:
10939       case bp_fast_tracepoint:
10940       case bp_static_tracepoint:
10941       case bp_catchpoint:
10942       case bp_hardware_breakpoint:
10943       case bp_watchpoint:
10944       case bp_hardware_watchpoint:
10945       case bp_read_watchpoint:
10946       case bp_access_watchpoint:
10947         disable_breakpoint (bpt);
10948       default:
10949         continue;
10950       }
10951   else if (strchr (args, '.'))
10952     {
10953       struct bp_location *loc = find_location_by_number (args);
10954       if (loc)
10955         loc->enabled = 0;
10956       update_global_location_list (0);
10957     }
10958   else
10959     map_breakpoint_numbers (args, do_map_disable_breakpoint, NULL);
10960 }
10961
10962 static void
10963 do_enable_breakpoint (struct breakpoint *bpt, enum bpdisp disposition)
10964 {
10965   int target_resources_ok;
10966
10967   if (bpt->type == bp_hardware_breakpoint)
10968     {
10969       int i;
10970       i = hw_breakpoint_used_count ();
10971       target_resources_ok = 
10972         target_can_use_hardware_watchpoint (bp_hardware_breakpoint, 
10973                                             i + 1, 0);
10974       if (target_resources_ok == 0)
10975         error (_("No hardware breakpoint support in the target."));
10976       else if (target_resources_ok < 0)
10977         error (_("Hardware breakpoints used exceeds limit."));
10978     }
10979
10980   if (is_watchpoint (bpt))
10981     {
10982       struct gdb_exception e;
10983
10984       TRY_CATCH (e, RETURN_MASK_ALL)
10985         {
10986           update_watchpoint (bpt, 1 /* reparse */);
10987         }
10988       if (e.reason < 0)
10989         {
10990           exception_fprintf (gdb_stderr, e, _("Cannot enable watchpoint %d: "),
10991                              bpt->number);
10992           return;
10993         }
10994     }
10995
10996   if (bpt->enable_state != bp_permanent)
10997     bpt->enable_state = bp_enabled;
10998   bpt->disposition = disposition;
10999   update_global_location_list (1);
11000   breakpoints_changed ();
11001   
11002   observer_notify_breakpoint_modified (bpt->number);
11003 }
11004
11005
11006 void
11007 enable_breakpoint (struct breakpoint *bpt)
11008 {
11009   do_enable_breakpoint (bpt, bpt->disposition);
11010 }
11011
11012 /* A callback for map_breakpoint_numbers that calls
11013    enable_breakpoint.  */
11014
11015 static void
11016 do_map_enable_breakpoint (struct breakpoint *b, void *ignore)
11017 {
11018   enable_breakpoint (b);
11019 }
11020
11021 /* The enable command enables the specified breakpoints (or all defined
11022    breakpoints) so they once again become (or continue to be) effective
11023    in stopping the inferior.  */
11024
11025 static void
11026 enable_command (char *args, int from_tty)
11027 {
11028   struct breakpoint *bpt;
11029
11030   if (args == 0)
11031     ALL_BREAKPOINTS (bpt)
11032       switch (bpt->type)
11033       {
11034       case bp_none:
11035         warning (_("attempted to enable apparently deleted breakpoint #%d?"),
11036                  bpt->number);
11037         continue;
11038       case bp_breakpoint:
11039       case bp_tracepoint:
11040       case bp_fast_tracepoint:
11041       case bp_static_tracepoint:
11042       case bp_catchpoint:
11043       case bp_hardware_breakpoint:
11044       case bp_watchpoint:
11045       case bp_hardware_watchpoint:
11046       case bp_read_watchpoint:
11047       case bp_access_watchpoint:
11048         enable_breakpoint (bpt);
11049       default:
11050         continue;
11051       }
11052   else if (strchr (args, '.'))
11053     {
11054       struct bp_location *loc = find_location_by_number (args);
11055       if (loc)
11056         loc->enabled = 1;
11057       update_global_location_list (1);
11058     }
11059   else
11060     map_breakpoint_numbers (args, do_map_enable_breakpoint, NULL);
11061 }
11062
11063 static void
11064 enable_once_breakpoint (struct breakpoint *bpt, void *ignore)
11065 {
11066   do_enable_breakpoint (bpt, disp_disable);
11067 }
11068
11069 static void
11070 enable_once_command (char *args, int from_tty)
11071 {
11072   map_breakpoint_numbers (args, enable_once_breakpoint, NULL);
11073 }
11074
11075 static void
11076 enable_delete_breakpoint (struct breakpoint *bpt, void *ignore)
11077 {
11078   do_enable_breakpoint (bpt, disp_del);
11079 }
11080
11081 static void
11082 enable_delete_command (char *args, int from_tty)
11083 {
11084   map_breakpoint_numbers (args, enable_delete_breakpoint, NULL);
11085 }
11086 \f
11087 static void
11088 set_breakpoint_cmd (char *args, int from_tty)
11089 {
11090 }
11091
11092 static void
11093 show_breakpoint_cmd (char *args, int from_tty)
11094 {
11095 }
11096
11097 /* Invalidate last known value of any hardware watchpoint if
11098    the memory which that value represents has been written to by
11099    GDB itself.  */
11100
11101 static void
11102 invalidate_bp_value_on_memory_change (CORE_ADDR addr, int len,
11103                                       const bfd_byte *data)
11104 {
11105   struct breakpoint *bp;
11106
11107   ALL_BREAKPOINTS (bp)
11108     if (bp->enable_state == bp_enabled
11109         && bp->type == bp_hardware_watchpoint
11110         && bp->val_valid && bp->val)
11111       {
11112         struct bp_location *loc;
11113
11114         for (loc = bp->loc; loc != NULL; loc = loc->next)
11115           if (loc->loc_type == bp_loc_hardware_watchpoint
11116               && loc->address + loc->length > addr
11117               && addr + len > loc->address)
11118             {
11119               value_free (bp->val);
11120               bp->val = NULL;
11121               bp->val_valid = 0;
11122             }
11123       }
11124 }
11125
11126 /* Use default_breakpoint_'s, or nothing if they aren't valid.  */
11127
11128 struct symtabs_and_lines
11129 decode_line_spec_1 (char *string, int funfirstline)
11130 {
11131   struct symtabs_and_lines sals;
11132
11133   if (string == 0)
11134     error (_("Empty line specification."));
11135   if (default_breakpoint_valid)
11136     sals = decode_line_1 (&string, funfirstline,
11137                           default_breakpoint_symtab,
11138                           default_breakpoint_line,
11139                           (char ***) NULL, NULL);
11140   else
11141     sals = decode_line_1 (&string, funfirstline,
11142                           (struct symtab *) NULL, 0, (char ***) NULL, NULL);
11143   if (*string)
11144     error (_("Junk at end of line specification: %s"), string);
11145   return sals;
11146 }
11147
11148 /* Create and insert a raw software breakpoint at PC.  Return an
11149    identifier, which should be used to remove the breakpoint later.
11150    In general, places which call this should be using something on the
11151    breakpoint chain instead; this function should be eliminated
11152    someday.  */
11153
11154 void *
11155 deprecated_insert_raw_breakpoint (struct gdbarch *gdbarch,
11156                                   struct address_space *aspace, CORE_ADDR pc)
11157 {
11158   struct bp_target_info *bp_tgt;
11159
11160   bp_tgt = XZALLOC (struct bp_target_info);
11161
11162   bp_tgt->placed_address_space = aspace;
11163   bp_tgt->placed_address = pc;
11164
11165   if (target_insert_breakpoint (gdbarch, bp_tgt) != 0)
11166     {
11167       /* Could not insert the breakpoint.  */
11168       xfree (bp_tgt);
11169       return NULL;
11170     }
11171
11172   return bp_tgt;
11173 }
11174
11175 /* Remove a breakpoint BP inserted by
11176    deprecated_insert_raw_breakpoint.  */
11177
11178 int
11179 deprecated_remove_raw_breakpoint (struct gdbarch *gdbarch, void *bp)
11180 {
11181   struct bp_target_info *bp_tgt = bp;
11182   int ret;
11183
11184   ret = target_remove_breakpoint (gdbarch, bp_tgt);
11185   xfree (bp_tgt);
11186
11187   return ret;
11188 }
11189
11190 /* One (or perhaps two) breakpoints used for software single
11191    stepping.  */
11192
11193 static void *single_step_breakpoints[2];
11194 static struct gdbarch *single_step_gdbarch[2];
11195
11196 /* Create and insert a breakpoint for software single step.  */
11197
11198 void
11199 insert_single_step_breakpoint (struct gdbarch *gdbarch,
11200                                struct address_space *aspace, 
11201                                CORE_ADDR next_pc)
11202 {
11203   void **bpt_p;
11204
11205   if (single_step_breakpoints[0] == NULL)
11206     {
11207       bpt_p = &single_step_breakpoints[0];
11208       single_step_gdbarch[0] = gdbarch;
11209     }
11210   else
11211     {
11212       gdb_assert (single_step_breakpoints[1] == NULL);
11213       bpt_p = &single_step_breakpoints[1];
11214       single_step_gdbarch[1] = gdbarch;
11215     }
11216
11217   /* NOTE drow/2006-04-11: A future improvement to this function would
11218      be to only create the breakpoints once, and actually put them on
11219      the breakpoint chain.  That would let us use set_raw_breakpoint.
11220      We could adjust the addresses each time they were needed.  Doing
11221      this requires corresponding changes elsewhere where single step
11222      breakpoints are handled, however.  So, for now, we use this.  */
11223
11224   *bpt_p = deprecated_insert_raw_breakpoint (gdbarch, aspace, next_pc);
11225   if (*bpt_p == NULL)
11226     error (_("Could not insert single-step breakpoint at %s"),
11227              paddress (gdbarch, next_pc));
11228 }
11229
11230 /* Check if the breakpoints used for software single stepping
11231    were inserted or not.  */
11232
11233 int
11234 single_step_breakpoints_inserted (void)
11235 {
11236   return (single_step_breakpoints[0] != NULL
11237           || single_step_breakpoints[1] != NULL);
11238 }
11239
11240 /* Remove and delete any breakpoints used for software single step.  */
11241
11242 void
11243 remove_single_step_breakpoints (void)
11244 {
11245   gdb_assert (single_step_breakpoints[0] != NULL);
11246
11247   /* See insert_single_step_breakpoint for more about this deprecated
11248      call.  */
11249   deprecated_remove_raw_breakpoint (single_step_gdbarch[0],
11250                                     single_step_breakpoints[0]);
11251   single_step_gdbarch[0] = NULL;
11252   single_step_breakpoints[0] = NULL;
11253
11254   if (single_step_breakpoints[1] != NULL)
11255     {
11256       deprecated_remove_raw_breakpoint (single_step_gdbarch[1],
11257                                         single_step_breakpoints[1]);
11258       single_step_gdbarch[1] = NULL;
11259       single_step_breakpoints[1] = NULL;
11260     }
11261 }
11262
11263 /* Delete software single step breakpoints without removing them from
11264    the inferior.  This is intended to be used if the inferior's address
11265    space where they were inserted is already gone, e.g. after exit or
11266    exec.  */
11267
11268 void
11269 cancel_single_step_breakpoints (void)
11270 {
11271   int i;
11272
11273   for (i = 0; i < 2; i++)
11274     if (single_step_breakpoints[i])
11275       {
11276         xfree (single_step_breakpoints[i]);
11277         single_step_breakpoints[i] = NULL;
11278         single_step_gdbarch[i] = NULL;
11279       }
11280 }
11281
11282 /* Detach software single-step breakpoints from INFERIOR_PTID without
11283    removing them.  */
11284
11285 static void
11286 detach_single_step_breakpoints (void)
11287 {
11288   int i;
11289
11290   for (i = 0; i < 2; i++)
11291     if (single_step_breakpoints[i])
11292       target_remove_breakpoint (single_step_gdbarch[i],
11293                                 single_step_breakpoints[i]);
11294 }
11295
11296 /* Check whether a software single-step breakpoint is inserted at
11297    PC.  */
11298
11299 static int
11300 single_step_breakpoint_inserted_here_p (struct address_space *aspace, 
11301                                         CORE_ADDR pc)
11302 {
11303   int i;
11304
11305   for (i = 0; i < 2; i++)
11306     {
11307       struct bp_target_info *bp_tgt = single_step_breakpoints[i];
11308       if (bp_tgt
11309           && breakpoint_address_match (bp_tgt->placed_address_space,
11310                                        bp_tgt->placed_address,
11311                                        aspace, pc))
11312         return 1;
11313     }
11314
11315   return 0;
11316 }
11317
11318 /* Returns 0 if 'bp' is NOT a syscall catchpoint,
11319    non-zero otherwise.  */
11320 static int
11321 is_syscall_catchpoint_enabled (struct breakpoint *bp)
11322 {
11323   if (syscall_catchpoint_p (bp)
11324       && bp->enable_state != bp_disabled
11325       && bp->enable_state != bp_call_disabled)
11326     return 1;
11327   else
11328     return 0;
11329 }
11330
11331 int
11332 catch_syscall_enabled (void)
11333 {
11334   struct inferior *inf = current_inferior ();
11335
11336   return inf->total_syscalls_count != 0;
11337 }
11338
11339 int
11340 catching_syscall_number (int syscall_number)
11341 {
11342   struct breakpoint *bp;
11343
11344   ALL_BREAKPOINTS (bp)
11345     if (is_syscall_catchpoint_enabled (bp))
11346       {
11347         if (bp->syscalls_to_be_caught)
11348           {
11349             int i, iter;
11350             for (i = 0;
11351                  VEC_iterate (int, bp->syscalls_to_be_caught, i, iter);
11352                  i++)
11353               if (syscall_number == iter)
11354                 return 1;
11355           }
11356         else
11357           return 1;
11358       }
11359
11360   return 0;
11361 }
11362
11363 /* Complete syscall names.  Used by "catch syscall".  */
11364 static char **
11365 catch_syscall_completer (struct cmd_list_element *cmd,
11366                          char *text, char *word)
11367 {
11368   const char **list = get_syscall_names ();
11369
11370   return (list == NULL) ? NULL : complete_on_enum (list, text, word);
11371 }
11372
11373 /* Tracepoint-specific operations.  */
11374
11375 /* Set tracepoint count to NUM.  */
11376 static void
11377 set_tracepoint_count (int num)
11378 {
11379   tracepoint_count = num;
11380   set_internalvar_integer (lookup_internalvar ("tpnum"), num);
11381 }
11382
11383 void
11384 trace_command (char *arg, int from_tty)
11385 {
11386   if (create_breakpoint (get_current_arch (),
11387                          arg,
11388                          NULL, 0, 1 /* parse arg */,
11389                          0 /* tempflag */,
11390                          bp_tracepoint /* type_wanted */,
11391                          0 /* Ignore count */,
11392                          pending_break_support,
11393                          NULL,
11394                          from_tty,
11395                          1 /* enabled */,
11396                          0 /* internal */))
11397     set_tracepoint_count (breakpoint_count);
11398 }
11399
11400 void
11401 ftrace_command (char *arg, int from_tty)
11402 {
11403   if (create_breakpoint (get_current_arch (),
11404                          arg,
11405                          NULL, 0, 1 /* parse arg */,
11406                          0 /* tempflag */,
11407                          bp_fast_tracepoint /* type_wanted */,
11408                          0 /* Ignore count */,
11409                          pending_break_support,
11410                          NULL,
11411                          from_tty,
11412                          1 /* enabled */,
11413                          0 /* internal */))
11414     set_tracepoint_count (breakpoint_count);
11415 }
11416
11417 /* strace command implementation.  Creates a static tracepoint.  */
11418
11419 void
11420 strace_command (char *arg, int from_tty)
11421 {
11422   if (create_breakpoint (get_current_arch (),
11423                          arg,
11424                          NULL, 0, 1 /* parse arg */,
11425                          0 /* tempflag */,
11426                          bp_static_tracepoint /* type_wanted */,
11427                          0 /* Ignore count */,
11428                          pending_break_support,
11429                          NULL,
11430                          from_tty,
11431                          1 /* enabled */,
11432                          0 /* internal */))
11433     set_tracepoint_count (breakpoint_count);
11434 }
11435
11436 /* Set up a fake reader function that gets command lines from a linked
11437    list that was acquired during tracepoint uploading.  */
11438
11439 static struct uploaded_tp *this_utp;
11440 static int next_cmd;
11441
11442 static char *
11443 read_uploaded_action (void)
11444 {
11445   char *rslt;
11446
11447   VEC_iterate (char_ptr, this_utp->cmd_strings, next_cmd, rslt);
11448
11449   next_cmd++;
11450
11451   return rslt;
11452 }
11453
11454 /* Given information about a tracepoint as recorded on a target (which
11455    can be either a live system or a trace file), attempt to create an
11456    equivalent GDB tracepoint.  This is not a reliable process, since
11457    the target does not necessarily have all the information used when
11458    the tracepoint was originally defined.  */
11459   
11460 struct breakpoint *
11461 create_tracepoint_from_upload (struct uploaded_tp *utp)
11462 {
11463   char *addr_str, small_buf[100];
11464   struct breakpoint *tp;
11465
11466   if (utp->at_string)
11467     addr_str = utp->at_string;
11468   else
11469     {
11470       /* In the absence of a source location, fall back to raw
11471          address.  Since there is no way to confirm that the address
11472          means the same thing as when the trace was started, warn the
11473          user.  */
11474       warning (_("Uploaded tracepoint %d has no "
11475                  "source location, using raw address"),
11476                utp->number);
11477       sprintf (small_buf, "*%s", hex_string (utp->addr));
11478       addr_str = small_buf;
11479     }
11480
11481   /* There's not much we can do with a sequence of bytecodes.  */
11482   if (utp->cond && !utp->cond_string)
11483     warning (_("Uploaded tracepoint %d condition "
11484                "has no source form, ignoring it"),
11485              utp->number);
11486
11487   if (!create_breakpoint (get_current_arch (),
11488                           addr_str,
11489                           utp->cond_string, -1, 0 /* parse cond/thread */,
11490                           0 /* tempflag */,
11491                           utp->type /* type_wanted */,
11492                           0 /* Ignore count */,
11493                           pending_break_support,
11494                           NULL,
11495                           0 /* from_tty */,
11496                           utp->enabled /* enabled */,
11497                           0 /* internal */))
11498     return NULL;
11499
11500   set_tracepoint_count (breakpoint_count);
11501   
11502   /* Get the tracepoint we just created.  */
11503   tp = get_tracepoint (tracepoint_count);
11504   gdb_assert (tp != NULL);
11505
11506   if (utp->pass > 0)
11507     {
11508       sprintf (small_buf, "%d %d", utp->pass, tp->number);
11509
11510       trace_pass_command (small_buf, 0);
11511     }
11512
11513   /* If we have uploaded versions of the original commands, set up a
11514      special-purpose "reader" function and call the usual command line
11515      reader, then pass the result to the breakpoint command-setting
11516      function.  */
11517   if (!VEC_empty (char_ptr, utp->cmd_strings))
11518     {
11519       struct command_line *cmd_list;
11520
11521       this_utp = utp;
11522       next_cmd = 0;
11523
11524       cmd_list = read_command_lines_1 (read_uploaded_action, 1, NULL, NULL);
11525
11526       breakpoint_set_commands (tp, cmd_list);
11527     }
11528   else if (!VEC_empty (char_ptr, utp->actions)
11529            || !VEC_empty (char_ptr, utp->step_actions))
11530     warning (_("Uploaded tracepoint %d actions "
11531                "have no source form, ignoring them"),
11532              utp->number);
11533
11534   return tp;
11535   }
11536   
11537 /* Print information on tracepoint number TPNUM_EXP, or all if
11538    omitted.  */
11539
11540 static void
11541 tracepoints_info (char *args, int from_tty)
11542 {
11543   int num_printed;
11544
11545   num_printed = breakpoint_1 (args, 0, is_tracepoint);
11546
11547   if (num_printed == 0)
11548     {
11549       if (args == NULL || *args == '\0')
11550         ui_out_message (uiout, 0, "No tracepoints.\n");
11551       else
11552         ui_out_message (uiout, 0, "No tracepoint matching '%s'.\n", args);
11553     }
11554
11555   default_collect_info ();
11556 }
11557
11558 /* The 'enable trace' command enables tracepoints.
11559    Not supported by all targets.  */
11560 static void
11561 enable_trace_command (char *args, int from_tty)
11562 {
11563   enable_command (args, from_tty);
11564 }
11565
11566 /* The 'disable trace' command disables tracepoints.
11567    Not supported by all targets.  */
11568 static void
11569 disable_trace_command (char *args, int from_tty)
11570 {
11571   disable_command (args, from_tty);
11572 }
11573
11574 /* Remove a tracepoint (or all if no argument).  */
11575 static void
11576 delete_trace_command (char *arg, int from_tty)
11577 {
11578   struct breakpoint *b, *b_tmp;
11579
11580   dont_repeat ();
11581
11582   if (arg == 0)
11583     {
11584       int breaks_to_delete = 0;
11585
11586       /* Delete all breakpoints if no argument.
11587          Do not delete internal or call-dummy breakpoints, these
11588          have to be deleted with an explicit breakpoint number 
11589          argument.  */
11590       ALL_TRACEPOINTS (b)
11591       {
11592         if (b->number >= 0)
11593           {
11594             breaks_to_delete = 1;
11595             break;
11596           }
11597       }
11598
11599       /* Ask user only if there are some breakpoints to delete.  */
11600       if (!from_tty
11601           || (breaks_to_delete && query (_("Delete all tracepoints? "))))
11602         {
11603           ALL_BREAKPOINTS_SAFE (b, b_tmp)
11604           {
11605             if (is_tracepoint (b)
11606                 && b->number >= 0)
11607               delete_breakpoint (b);
11608           }
11609         }
11610     }
11611   else
11612     map_breakpoint_numbers (arg, do_delete_breakpoint, NULL);
11613 }
11614
11615 /* Set passcount for tracepoint.
11616
11617    First command argument is passcount, second is tracepoint number.
11618    If tracepoint number omitted, apply to most recently defined.
11619    Also accepts special argument "all".  */
11620
11621 static void
11622 trace_pass_command (char *args, int from_tty)
11623 {
11624   struct breakpoint *t1 = (struct breakpoint *) -1, *t2;
11625   unsigned int count;
11626   int all = 0;
11627
11628   if (args == 0 || *args == 0)
11629     error (_("passcount command requires an "
11630              "argument (count + optional TP num)"));
11631
11632   count = strtoul (args, &args, 10);    /* Count comes first, then TP num.  */
11633
11634   while (*args && isspace ((int) *args))
11635     args++;
11636
11637   if (*args && strncasecmp (args, "all", 3) == 0)
11638     {
11639       args += 3;                        /* Skip special argument "all".  */
11640       all = 1;
11641       if (*args)
11642         error (_("Junk at end of arguments."));
11643     }
11644   else
11645     t1 = get_tracepoint_by_number (&args, 1, 1);
11646
11647   do
11648     {
11649       if (t1)
11650         {
11651           ALL_TRACEPOINTS (t2)
11652             if (t1 == (struct breakpoint *) -1 || t1 == t2)
11653               {
11654                 t2->pass_count = count;
11655                 observer_notify_tracepoint_modified (t2->number);
11656                 if (from_tty)
11657                   printf_filtered (_("Setting tracepoint %d's "
11658                                      "passcount to %d\n"),
11659                                    t2->number, count);
11660               }
11661           if (! all && *args)
11662             t1 = get_tracepoint_by_number (&args, 1, 0);
11663         }
11664     }
11665   while (*args);
11666 }
11667
11668 struct breakpoint *
11669 get_tracepoint (int num)
11670 {
11671   struct breakpoint *t;
11672
11673   ALL_TRACEPOINTS (t)
11674     if (t->number == num)
11675       return t;
11676
11677   return NULL;
11678 }
11679
11680 /* Find the tracepoint with the given target-side number (which may be
11681    different from the tracepoint number after disconnecting and
11682    reconnecting).  */
11683
11684 struct breakpoint *
11685 get_tracepoint_by_number_on_target (int num)
11686 {
11687   struct breakpoint *t;
11688
11689   ALL_TRACEPOINTS (t)
11690     if (t->number_on_target == num)
11691       return t;
11692
11693   return NULL;
11694 }
11695
11696 /* Utility: parse a tracepoint number and look it up in the list.
11697    If MULTI_P is true, there might be a range of tracepoints in ARG.
11698    if OPTIONAL_P is true, then if the argument is missing, the most
11699    recent tracepoint (tracepoint_count) is returned.  */
11700 struct breakpoint *
11701 get_tracepoint_by_number (char **arg, int multi_p, int optional_p)
11702 {
11703   extern int tracepoint_count;
11704   struct breakpoint *t;
11705   int tpnum;
11706   char *instring = arg == NULL ? NULL : *arg;
11707
11708   if (arg == NULL || *arg == NULL || ! **arg)
11709     {
11710       if (optional_p)
11711         tpnum = tracepoint_count;
11712       else
11713         error_no_arg (_("tracepoint number"));
11714     }
11715   else
11716     tpnum = multi_p ? get_number_or_range (arg) : get_number (arg);
11717
11718   if (tpnum <= 0)
11719     {
11720       if (instring && *instring)
11721         printf_filtered (_("bad tracepoint number at or near '%s'\n"), 
11722                          instring);
11723       else
11724         printf_filtered (_("Tracepoint argument missing "
11725                            "and no previous tracepoint\n"));
11726       return NULL;
11727     }
11728
11729   ALL_TRACEPOINTS (t)
11730     if (t->number == tpnum)
11731     {
11732       return t;
11733     }
11734
11735   /* FIXME: if we are in the middle of a range we don't want to give
11736      a message.  The current interface to get_number_or_range doesn't
11737      allow us to discover this.  */
11738   printf_unfiltered ("No tracepoint number %d.\n", tpnum);
11739   return NULL;
11740 }
11741
11742 /* Save information on user settable breakpoints (watchpoints, etc) to
11743    a new script file named FILENAME.  If FILTER is non-NULL, call it
11744    on each breakpoint and only include the ones for which it returns
11745    non-zero.  */
11746
11747 static void
11748 save_breakpoints (char *filename, int from_tty,
11749                   int (*filter) (const struct breakpoint *))
11750 {
11751   struct breakpoint *tp;
11752   int any = 0;
11753   char *pathname;
11754   struct cleanup *cleanup;
11755   struct ui_file *fp;
11756   int extra_trace_bits = 0;
11757
11758   if (filename == 0 || *filename == 0)
11759     error (_("Argument required (file name in which to save)"));
11760
11761   /* See if we have anything to save.  */
11762   ALL_BREAKPOINTS (tp)
11763   {
11764     /* Skip internal and momentary breakpoints.  */
11765     if (!user_breakpoint_p (tp))
11766       continue;
11767
11768     /* If we have a filter, only save the breakpoints it accepts.  */
11769     if (filter && !filter (tp))
11770       continue;
11771
11772     any = 1;
11773
11774     if (is_tracepoint (tp))
11775       {
11776         extra_trace_bits = 1;
11777
11778         /* We can stop searching.  */
11779         break;
11780       }
11781   }
11782
11783   if (!any)
11784     {
11785       warning (_("Nothing to save."));
11786       return;
11787     }
11788
11789   pathname = tilde_expand (filename);
11790   cleanup = make_cleanup (xfree, pathname);
11791   fp = gdb_fopen (pathname, "w");
11792   if (!fp)
11793     error (_("Unable to open file '%s' for saving (%s)"),
11794            filename, safe_strerror (errno));
11795   make_cleanup_ui_file_delete (fp);
11796
11797   if (extra_trace_bits)
11798     save_trace_state_variables (fp);
11799
11800   ALL_BREAKPOINTS (tp)
11801   {
11802     /* Skip internal and momentary breakpoints.  */
11803     if (!user_breakpoint_p (tp))
11804       continue;
11805
11806     /* If we have a filter, only save the breakpoints it accepts.  */
11807     if (filter && !filter (tp))
11808       continue;
11809
11810     if (tp->ops != NULL)
11811       (tp->ops->print_recreate) (tp, fp);
11812     else
11813       {
11814         if (tp->type == bp_fast_tracepoint)
11815           fprintf_unfiltered (fp, "ftrace");
11816         if (tp->type == bp_static_tracepoint)
11817           fprintf_unfiltered (fp, "strace");
11818         else if (tp->type == bp_tracepoint)
11819           fprintf_unfiltered (fp, "trace");
11820         else if (tp->type == bp_breakpoint && tp->disposition == disp_del)
11821           fprintf_unfiltered (fp, "tbreak");
11822         else if (tp->type == bp_breakpoint)
11823           fprintf_unfiltered (fp, "break");
11824         else if (tp->type == bp_hardware_breakpoint
11825                  && tp->disposition == disp_del)
11826           fprintf_unfiltered (fp, "thbreak");
11827         else if (tp->type == bp_hardware_breakpoint)
11828           fprintf_unfiltered (fp, "hbreak");
11829         else if (tp->type == bp_watchpoint)
11830           fprintf_unfiltered (fp, "watch");
11831         else if (tp->type == bp_hardware_watchpoint)
11832           fprintf_unfiltered (fp, "watch");
11833         else if (tp->type == bp_read_watchpoint)
11834           fprintf_unfiltered (fp, "rwatch");
11835         else if (tp->type == bp_access_watchpoint)
11836           fprintf_unfiltered (fp, "awatch");
11837         else
11838           internal_error (__FILE__, __LINE__,
11839                           _("unhandled breakpoint type %d"), (int) tp->type);
11840
11841         if (tp->exp_string)
11842           fprintf_unfiltered (fp, " %s", tp->exp_string);
11843         else if (tp->addr_string)
11844           fprintf_unfiltered (fp, " %s", tp->addr_string);
11845         else
11846           {
11847             char tmp[40];
11848
11849             sprintf_vma (tmp, tp->loc->address);
11850             fprintf_unfiltered (fp, " *0x%s", tmp);
11851           }
11852       }
11853
11854     if (tp->thread != -1)
11855       fprintf_unfiltered (fp, " thread %d", tp->thread);
11856
11857     if (tp->task != 0)
11858       fprintf_unfiltered (fp, " task %d", tp->task);
11859
11860     fprintf_unfiltered (fp, "\n");
11861
11862     /* Note, we can't rely on tp->number for anything, as we can't
11863        assume the recreated breakpoint numbers will match.  Use $bpnum
11864        instead.  */
11865
11866     if (tp->cond_string)
11867       fprintf_unfiltered (fp, "  condition $bpnum %s\n", tp->cond_string);
11868
11869     if (tp->ignore_count)
11870       fprintf_unfiltered (fp, "  ignore $bpnum %d\n", tp->ignore_count);
11871
11872     if (tp->pass_count)
11873       fprintf_unfiltered (fp, "  passcount %d\n", tp->pass_count);
11874
11875     if (tp->commands)
11876       {
11877         volatile struct gdb_exception ex;       
11878
11879         fprintf_unfiltered (fp, "  commands\n");
11880         
11881         ui_out_redirect (uiout, fp);
11882         TRY_CATCH (ex, RETURN_MASK_ALL)
11883           {
11884             print_command_lines (uiout, tp->commands->commands, 2);
11885           }
11886         ui_out_redirect (uiout, NULL);
11887
11888         if (ex.reason < 0)
11889           throw_exception (ex);
11890
11891         fprintf_unfiltered (fp, "  end\n");
11892       }
11893
11894     if (tp->enable_state == bp_disabled)
11895       fprintf_unfiltered (fp, "disable\n");
11896
11897     /* If this is a multi-location breakpoint, check if the locations
11898        should be individually disabled.  Watchpoint locations are
11899        special, and not user visible.  */
11900     if (!is_watchpoint (tp) && tp->loc && tp->loc->next)
11901       {
11902         struct bp_location *loc;
11903         int n = 1;
11904
11905         for (loc = tp->loc; loc != NULL; loc = loc->next, n++)
11906           if (!loc->enabled)
11907             fprintf_unfiltered (fp, "disable $bpnum.%d\n", n);
11908       }
11909   }
11910
11911   if (extra_trace_bits && *default_collect)
11912     fprintf_unfiltered (fp, "set default-collect %s\n", default_collect);
11913
11914   do_cleanups (cleanup);
11915   if (from_tty)
11916     printf_filtered (_("Saved to file '%s'.\n"), filename);
11917 }
11918
11919 /* The `save breakpoints' command.  */
11920
11921 static void
11922 save_breakpoints_command (char *args, int from_tty)
11923 {
11924   save_breakpoints (args, from_tty, NULL);
11925 }
11926
11927 /* The `save tracepoints' command.  */
11928
11929 static void
11930 save_tracepoints_command (char *args, int from_tty)
11931 {
11932   save_breakpoints (args, from_tty, is_tracepoint);
11933 }
11934
11935 /* Create a vector of all tracepoints.  */
11936
11937 VEC(breakpoint_p) *
11938 all_tracepoints ()
11939 {
11940   VEC(breakpoint_p) *tp_vec = 0;
11941   struct breakpoint *tp;
11942
11943   ALL_TRACEPOINTS (tp)
11944   {
11945     VEC_safe_push (breakpoint_p, tp_vec, tp);
11946   }
11947
11948   return tp_vec;
11949 }
11950
11951 \f
11952 /* This help string is used for the break, hbreak, tbreak and thbreak
11953    commands.  It is defined as a macro to prevent duplication.
11954    COMMAND should be a string constant containing the name of the
11955    command.  */
11956 #define BREAK_ARGS_HELP(command) \
11957 command" [LOCATION] [thread THREADNUM] [if CONDITION]\n\
11958 LOCATION may be a line number, function name, or \"*\" and an address.\n\
11959 If a line number is specified, break at start of code for that line.\n\
11960 If a function is specified, break at start of code for that function.\n\
11961 If an address is specified, break at that exact address.\n\
11962 With no LOCATION, uses current execution address of the selected\n\
11963 stack frame.  This is useful for breaking on return to a stack frame.\n\
11964 \n\
11965 THREADNUM is the number from \"info threads\".\n\
11966 CONDITION is a boolean expression.\n\
11967 \n\
11968 Multiple breakpoints at one place are permitted, and useful if their\n\
11969 conditions are different.\n\
11970 \n\
11971 Do \"help breakpoints\" for info on other commands dealing with breakpoints."
11972
11973 /* List of subcommands for "catch".  */
11974 static struct cmd_list_element *catch_cmdlist;
11975
11976 /* List of subcommands for "tcatch".  */
11977 static struct cmd_list_element *tcatch_cmdlist;
11978
11979 /* Like add_cmd, but add the command to both the "catch" and "tcatch"
11980    lists, and pass some additional user data to the command function.  */
11981 static void
11982 add_catch_command (char *name, char *docstring,
11983                    void (*sfunc) (char *args, int from_tty,
11984                                   struct cmd_list_element *command),
11985                    char **(*completer) (struct cmd_list_element *cmd,
11986                                          char *text, char *word),
11987                    void *user_data_catch,
11988                    void *user_data_tcatch)
11989 {
11990   struct cmd_list_element *command;
11991
11992   command = add_cmd (name, class_breakpoint, NULL, docstring,
11993                      &catch_cmdlist);
11994   set_cmd_sfunc (command, sfunc);
11995   set_cmd_context (command, user_data_catch);
11996   set_cmd_completer (command, completer);
11997
11998   command = add_cmd (name, class_breakpoint, NULL, docstring,
11999                      &tcatch_cmdlist);
12000   set_cmd_sfunc (command, sfunc);
12001   set_cmd_context (command, user_data_tcatch);
12002   set_cmd_completer (command, completer);
12003 }
12004
12005 static void
12006 clear_syscall_counts (struct inferior *inf)
12007 {
12008   inf->total_syscalls_count = 0;
12009   inf->any_syscall_count = 0;
12010   VEC_free (int, inf->syscalls_counts);
12011 }
12012
12013 static void
12014 save_command (char *arg, int from_tty)
12015 {
12016   printf_unfiltered (_("\"save\" must be followed by "
12017                        "the name of a save subcommand.\n"));
12018   help_list (save_cmdlist, "save ", -1, gdb_stdout);
12019 }
12020
12021 struct breakpoint *
12022 iterate_over_breakpoints (int (*callback) (struct breakpoint *, void *),
12023                           void *data)
12024 {
12025   struct breakpoint *b, *b_tmp;
12026
12027   ALL_BREAKPOINTS_SAFE (b, b_tmp)
12028     {
12029       if ((*callback) (b, data))
12030         return b;
12031     }
12032
12033   return NULL;
12034 }
12035
12036 void
12037 _initialize_breakpoint (void)
12038 {
12039   struct cmd_list_element *c;
12040
12041   observer_attach_solib_unloaded (disable_breakpoints_in_unloaded_shlib);
12042   observer_attach_inferior_exit (clear_syscall_counts);
12043   observer_attach_memory_changed (invalidate_bp_value_on_memory_change);
12044
12045   breakpoint_objfile_key = register_objfile_data ();
12046
12047   breakpoint_chain = 0;
12048   /* Don't bother to call set_breakpoint_count.  $bpnum isn't useful
12049      before a breakpoint is set.  */
12050   breakpoint_count = 0;
12051
12052   tracepoint_count = 0;
12053
12054   add_com ("ignore", class_breakpoint, ignore_command, _("\
12055 Set ignore-count of breakpoint number N to COUNT.\n\
12056 Usage is `ignore N COUNT'."));
12057   if (xdb_commands)
12058     add_com_alias ("bc", "ignore", class_breakpoint, 1);
12059
12060   add_com ("commands", class_breakpoint, commands_command, _("\
12061 Set commands to be executed when a breakpoint is hit.\n\
12062 Give breakpoint number as argument after \"commands\".\n\
12063 With no argument, the targeted breakpoint is the last one set.\n\
12064 The commands themselves follow starting on the next line.\n\
12065 Type a line containing \"end\" to indicate the end of them.\n\
12066 Give \"silent\" as the first line to make the breakpoint silent;\n\
12067 then no output is printed when it is hit, except what the commands print."));
12068
12069   add_com ("condition", class_breakpoint, condition_command, _("\
12070 Specify breakpoint number N to break only if COND is true.\n\
12071 Usage is `condition N COND', where N is an integer and COND is an\n\
12072 expression to be evaluated whenever breakpoint N is reached."));
12073
12074   c = add_com ("tbreak", class_breakpoint, tbreak_command, _("\
12075 Set a temporary breakpoint.\n\
12076 Like \"break\" except the breakpoint is only temporary,\n\
12077 so it will be deleted when hit.  Equivalent to \"break\" followed\n\
12078 by using \"enable delete\" on the breakpoint number.\n\
12079 \n"
12080 BREAK_ARGS_HELP ("tbreak")));
12081   set_cmd_completer (c, location_completer);
12082
12083   c = add_com ("hbreak", class_breakpoint, hbreak_command, _("\
12084 Set a hardware assisted breakpoint.\n\
12085 Like \"break\" except the breakpoint requires hardware support,\n\
12086 some target hardware may not have this support.\n\
12087 \n"
12088 BREAK_ARGS_HELP ("hbreak")));
12089   set_cmd_completer (c, location_completer);
12090
12091   c = add_com ("thbreak", class_breakpoint, thbreak_command, _("\
12092 Set a temporary hardware assisted breakpoint.\n\
12093 Like \"hbreak\" except the breakpoint is only temporary,\n\
12094 so it will be deleted when hit.\n\
12095 \n"
12096 BREAK_ARGS_HELP ("thbreak")));
12097   set_cmd_completer (c, location_completer);
12098
12099   add_prefix_cmd ("enable", class_breakpoint, enable_command, _("\
12100 Enable some breakpoints.\n\
12101 Give breakpoint numbers (separated by spaces) as arguments.\n\
12102 With no subcommand, breakpoints are enabled until you command otherwise.\n\
12103 This is used to cancel the effect of the \"disable\" command.\n\
12104 With a subcommand you can enable temporarily."),
12105                   &enablelist, "enable ", 1, &cmdlist);
12106   if (xdb_commands)
12107     add_com ("ab", class_breakpoint, enable_command, _("\
12108 Enable some breakpoints.\n\
12109 Give breakpoint numbers (separated by spaces) as arguments.\n\
12110 With no subcommand, breakpoints are enabled until you command otherwise.\n\
12111 This is used to cancel the effect of the \"disable\" command.\n\
12112 With a subcommand you can enable temporarily."));
12113
12114   add_com_alias ("en", "enable", class_breakpoint, 1);
12115
12116   add_prefix_cmd ("breakpoints", class_breakpoint, enable_command, _("\
12117 Enable some breakpoints.\n\
12118 Give breakpoint numbers (separated by spaces) as arguments.\n\
12119 This is used to cancel the effect of the \"disable\" command.\n\
12120 May be abbreviated to simply \"enable\".\n"),
12121                    &enablebreaklist, "enable breakpoints ", 1, &enablelist);
12122
12123   add_cmd ("once", no_class, enable_once_command, _("\
12124 Enable breakpoints for one hit.  Give breakpoint numbers.\n\
12125 If a breakpoint is hit while enabled in this fashion, it becomes disabled."),
12126            &enablebreaklist);
12127
12128   add_cmd ("delete", no_class, enable_delete_command, _("\
12129 Enable breakpoints and delete when hit.  Give breakpoint numbers.\n\
12130 If a breakpoint is hit while enabled in this fashion, it is deleted."),
12131            &enablebreaklist);
12132
12133   add_cmd ("delete", no_class, enable_delete_command, _("\
12134 Enable breakpoints and delete when hit.  Give breakpoint numbers.\n\
12135 If a breakpoint is hit while enabled in this fashion, it is deleted."),
12136            &enablelist);
12137
12138   add_cmd ("once", no_class, enable_once_command, _("\
12139 Enable breakpoints for one hit.  Give breakpoint numbers.\n\
12140 If a breakpoint is hit while enabled in this fashion, it becomes disabled."),
12141            &enablelist);
12142
12143   add_prefix_cmd ("disable", class_breakpoint, disable_command, _("\
12144 Disable some breakpoints.\n\
12145 Arguments are breakpoint numbers with spaces in between.\n\
12146 To disable all breakpoints, give no argument.\n\
12147 A disabled breakpoint is not forgotten, but has no effect until reenabled."),
12148                   &disablelist, "disable ", 1, &cmdlist);
12149   add_com_alias ("dis", "disable", class_breakpoint, 1);
12150   add_com_alias ("disa", "disable", class_breakpoint, 1);
12151   if (xdb_commands)
12152     add_com ("sb", class_breakpoint, disable_command, _("\
12153 Disable some breakpoints.\n\
12154 Arguments are breakpoint numbers with spaces in between.\n\
12155 To disable all breakpoints, give no argument.\n\
12156 A disabled breakpoint is not forgotten, but has no effect until reenabled."));
12157
12158   add_cmd ("breakpoints", class_alias, disable_command, _("\
12159 Disable some breakpoints.\n\
12160 Arguments are breakpoint numbers with spaces in between.\n\
12161 To disable all breakpoints, give no argument.\n\
12162 A disabled breakpoint is not forgotten, but has no effect until reenabled.\n\
12163 This command may be abbreviated \"disable\"."),
12164            &disablelist);
12165
12166   add_prefix_cmd ("delete", class_breakpoint, delete_command, _("\
12167 Delete some breakpoints or auto-display expressions.\n\
12168 Arguments are breakpoint numbers with spaces in between.\n\
12169 To delete all breakpoints, give no argument.\n\
12170 \n\
12171 Also a prefix command for deletion of other GDB objects.\n\
12172 The \"unset\" command is also an alias for \"delete\"."),
12173                   &deletelist, "delete ", 1, &cmdlist);
12174   add_com_alias ("d", "delete", class_breakpoint, 1);
12175   add_com_alias ("del", "delete", class_breakpoint, 1);
12176   if (xdb_commands)
12177     add_com ("db", class_breakpoint, delete_command, _("\
12178 Delete some breakpoints.\n\
12179 Arguments are breakpoint numbers with spaces in between.\n\
12180 To delete all breakpoints, give no argument.\n"));
12181
12182   add_cmd ("breakpoints", class_alias, delete_command, _("\
12183 Delete some breakpoints or auto-display expressions.\n\
12184 Arguments are breakpoint numbers with spaces in between.\n\
12185 To delete all breakpoints, give no argument.\n\
12186 This command may be abbreviated \"delete\"."),
12187            &deletelist);
12188
12189   add_com ("clear", class_breakpoint, clear_command, _("\
12190 Clear breakpoint at specified line or function.\n\
12191 Argument may be line number, function name, or \"*\" and an address.\n\
12192 If line number is specified, all breakpoints in that line are cleared.\n\
12193 If function is specified, breakpoints at beginning of function are cleared.\n\
12194 If an address is specified, breakpoints at that address are cleared.\n\
12195 \n\
12196 With no argument, clears all breakpoints in the line that the selected frame\n\
12197 is executing in.\n\
12198 \n\
12199 See also the \"delete\" command which clears breakpoints by number."));
12200   add_com_alias ("cl", "clear", class_breakpoint, 1);
12201
12202   c = add_com ("break", class_breakpoint, break_command, _("\
12203 Set breakpoint at specified line or function.\n"
12204 BREAK_ARGS_HELP ("break")));
12205   set_cmd_completer (c, location_completer);
12206
12207   add_com_alias ("b", "break", class_run, 1);
12208   add_com_alias ("br", "break", class_run, 1);
12209   add_com_alias ("bre", "break", class_run, 1);
12210   add_com_alias ("brea", "break", class_run, 1);
12211
12212   if (xdb_commands)
12213    add_com_alias ("ba", "break", class_breakpoint, 1);
12214
12215   if (dbx_commands)
12216     {
12217       add_abbrev_prefix_cmd ("stop", class_breakpoint, stop_command, _("\
12218 Break in function/address or break at a line in the current file."),
12219                              &stoplist, "stop ", 1, &cmdlist);
12220       add_cmd ("in", class_breakpoint, stopin_command,
12221                _("Break in function or address."), &stoplist);
12222       add_cmd ("at", class_breakpoint, stopat_command,
12223                _("Break at a line in the current file."), &stoplist);
12224       add_com ("status", class_info, breakpoints_info, _("\
12225 Status of user-settable breakpoints, or breakpoint number NUMBER.\n\
12226 The \"Type\" column indicates one of:\n\
12227 \tbreakpoint     - normal breakpoint\n\
12228 \twatchpoint     - watchpoint\n\
12229 The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
12230 the disposition of the breakpoint after it gets hit.  \"dis\" means that the\n\
12231 breakpoint will be disabled.  The \"Address\" and \"What\" columns indicate the\n\
12232 address and file/line number respectively.\n\
12233 \n\
12234 Convenience variable \"$_\" and default examine address for \"x\"\n\
12235 are set to the address of the last breakpoint listed unless the command\n\
12236 is prefixed with \"server \".\n\n\
12237 Convenience variable \"$bpnum\" contains the number of the last\n\
12238 breakpoint set."));
12239     }
12240
12241   add_info ("breakpoints", breakpoints_info, _("\
12242 Status of specified breakpoints (all user-settable breakpoints if no argument).\n\
12243 The \"Type\" column indicates one of:\n\
12244 \tbreakpoint     - normal breakpoint\n\
12245 \twatchpoint     - watchpoint\n\
12246 The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
12247 the disposition of the breakpoint after it gets hit.  \"dis\" means that the\n\
12248 breakpoint will be disabled.  The \"Address\" and \"What\" columns indicate the\n\
12249 address and file/line number respectively.\n\
12250 \n\
12251 Convenience variable \"$_\" and default examine address for \"x\"\n\
12252 are set to the address of the last breakpoint listed unless the command\n\
12253 is prefixed with \"server \".\n\n\
12254 Convenience variable \"$bpnum\" contains the number of the last\n\
12255 breakpoint set."));
12256
12257   add_info_alias ("b", "breakpoints", 1);
12258
12259   if (xdb_commands)
12260     add_com ("lb", class_breakpoint, breakpoints_info, _("\
12261 Status of user-settable breakpoints, or breakpoint number NUMBER.\n\
12262 The \"Type\" column indicates one of:\n\
12263 \tbreakpoint     - normal breakpoint\n\
12264 \twatchpoint     - watchpoint\n\
12265 The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
12266 the disposition of the breakpoint after it gets hit.  \"dis\" means that the\n\
12267 breakpoint will be disabled.  The \"Address\" and \"What\" columns indicate the\n\
12268 address and file/line number respectively.\n\
12269 \n\
12270 Convenience variable \"$_\" and default examine address for \"x\"\n\
12271 are set to the address of the last breakpoint listed unless the command\n\
12272 is prefixed with \"server \".\n\n\
12273 Convenience variable \"$bpnum\" contains the number of the last\n\
12274 breakpoint set."));
12275
12276   add_cmd ("breakpoints", class_maintenance, maintenance_info_breakpoints, _("\
12277 Status of all breakpoints, or breakpoint number NUMBER.\n\
12278 The \"Type\" column indicates one of:\n\
12279 \tbreakpoint     - normal breakpoint\n\
12280 \twatchpoint     - watchpoint\n\
12281 \tlongjmp        - internal breakpoint used to step through longjmp()\n\
12282 \tlongjmp resume - internal breakpoint at the target of longjmp()\n\
12283 \tuntil          - internal breakpoint used by the \"until\" command\n\
12284 \tfinish         - internal breakpoint used by the \"finish\" command\n\
12285 The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
12286 the disposition of the breakpoint after it gets hit.  \"dis\" means that the\n\
12287 breakpoint will be disabled.  The \"Address\" and \"What\" columns indicate the\n\
12288 address and file/line number respectively.\n\
12289 \n\
12290 Convenience variable \"$_\" and default examine address for \"x\"\n\
12291 are set to the address of the last breakpoint listed unless the command\n\
12292 is prefixed with \"server \".\n\n\
12293 Convenience variable \"$bpnum\" contains the number of the last\n\
12294 breakpoint set."),
12295            &maintenanceinfolist);
12296
12297   add_prefix_cmd ("catch", class_breakpoint, catch_command, _("\
12298 Set catchpoints to catch events."),
12299                   &catch_cmdlist, "catch ",
12300                   0/*allow-unknown*/, &cmdlist);
12301
12302   add_prefix_cmd ("tcatch", class_breakpoint, tcatch_command, _("\
12303 Set temporary catchpoints to catch events."),
12304                   &tcatch_cmdlist, "tcatch ",
12305                   0/*allow-unknown*/, &cmdlist);
12306
12307   /* Add catch and tcatch sub-commands.  */
12308   add_catch_command ("catch", _("\
12309 Catch an exception, when caught.\n\
12310 With an argument, catch only exceptions with the given name."),
12311                      catch_catch_command,
12312                      NULL,
12313                      CATCH_PERMANENT,
12314                      CATCH_TEMPORARY);
12315   add_catch_command ("throw", _("\
12316 Catch an exception, when thrown.\n\
12317 With an argument, catch only exceptions with the given name."),
12318                      catch_throw_command,
12319                      NULL,
12320                      CATCH_PERMANENT,
12321                      CATCH_TEMPORARY);
12322   add_catch_command ("fork", _("Catch calls to fork."),
12323                      catch_fork_command_1,
12324                      NULL,
12325                      (void *) (uintptr_t) catch_fork_permanent,
12326                      (void *) (uintptr_t) catch_fork_temporary);
12327   add_catch_command ("vfork", _("Catch calls to vfork."),
12328                      catch_fork_command_1,
12329                      NULL,
12330                      (void *) (uintptr_t) catch_vfork_permanent,
12331                      (void *) (uintptr_t) catch_vfork_temporary);
12332   add_catch_command ("exec", _("Catch calls to exec."),
12333                      catch_exec_command_1,
12334                      NULL,
12335                      CATCH_PERMANENT,
12336                      CATCH_TEMPORARY);
12337   add_catch_command ("syscall", _("\
12338 Catch system calls by their names and/or numbers.\n\
12339 Arguments say which system calls to catch.  If no arguments\n\
12340 are given, every system call will be caught.\n\
12341 Arguments, if given, should be one or more system call names\n\
12342 (if your system supports that), or system call numbers."),
12343                      catch_syscall_command_1,
12344                      catch_syscall_completer,
12345                      CATCH_PERMANENT,
12346                      CATCH_TEMPORARY);
12347   add_catch_command ("exception", _("\
12348 Catch Ada exceptions, when raised.\n\
12349 With an argument, catch only exceptions with the given name."),
12350                      catch_ada_exception_command,
12351                      NULL,
12352                      CATCH_PERMANENT,
12353                      CATCH_TEMPORARY);
12354   add_catch_command ("assert", _("\
12355 Catch failed Ada assertions, when raised.\n\
12356 With an argument, catch only exceptions with the given name."),
12357                      catch_assert_command,
12358                      NULL,
12359                      CATCH_PERMANENT,
12360                      CATCH_TEMPORARY);
12361
12362   c = add_com ("watch", class_breakpoint, watch_command, _("\
12363 Set a watchpoint for an expression.\n\
12364 Usage: watch [-l|-location] EXPRESSION\n\
12365 A watchpoint stops execution of your program whenever the value of\n\
12366 an expression changes.\n\
12367 If -l or -location is given, this evaluates EXPRESSION and watches\n\
12368 the memory to which it refers."));
12369   set_cmd_completer (c, expression_completer);
12370
12371   c = add_com ("rwatch", class_breakpoint, rwatch_command, _("\
12372 Set a read watchpoint for an expression.\n\
12373 Usage: rwatch [-l|-location] EXPRESSION\n\
12374 A watchpoint stops execution of your program whenever the value of\n\
12375 an expression is read.\n\
12376 If -l or -location is given, this evaluates EXPRESSION and watches\n\
12377 the memory to which it refers."));
12378   set_cmd_completer (c, expression_completer);
12379
12380   c = add_com ("awatch", class_breakpoint, awatch_command, _("\
12381 Set a watchpoint for an expression.\n\
12382 Usage: awatch [-l|-location] EXPRESSION\n\
12383 A watchpoint stops execution of your program whenever the value of\n\
12384 an expression is either read or written.\n\
12385 If -l or -location is given, this evaluates EXPRESSION and watches\n\
12386 the memory to which it refers."));
12387   set_cmd_completer (c, expression_completer);
12388
12389   add_info ("watchpoints", watchpoints_info, _("\
12390 Status of specified watchpoints (all watchpoints if no argument)."));
12391
12392   /* XXX: cagney/2005-02-23: This should be a boolean, and should
12393      respond to changes - contrary to the description.  */
12394   add_setshow_zinteger_cmd ("can-use-hw-watchpoints", class_support,
12395                             &can_use_hw_watchpoints, _("\
12396 Set debugger's willingness to use watchpoint hardware."), _("\
12397 Show debugger's willingness to use watchpoint hardware."), _("\
12398 If zero, gdb will not use hardware for new watchpoints, even if\n\
12399 such is available.  (However, any hardware watchpoints that were\n\
12400 created before setting this to nonzero, will continue to use watchpoint\n\
12401 hardware.)"),
12402                             NULL,
12403                             show_can_use_hw_watchpoints,
12404                             &setlist, &showlist);
12405
12406   can_use_hw_watchpoints = 1;
12407
12408   /* Tracepoint manipulation commands.  */
12409
12410   c = add_com ("trace", class_breakpoint, trace_command, _("\
12411 Set a tracepoint at specified line or function.\n\
12412 \n"
12413 BREAK_ARGS_HELP ("trace") "\n\
12414 Do \"help tracepoints\" for info on other tracepoint commands."));
12415   set_cmd_completer (c, location_completer);
12416
12417   add_com_alias ("tp", "trace", class_alias, 0);
12418   add_com_alias ("tr", "trace", class_alias, 1);
12419   add_com_alias ("tra", "trace", class_alias, 1);
12420   add_com_alias ("trac", "trace", class_alias, 1);
12421
12422   c = add_com ("ftrace", class_breakpoint, ftrace_command, _("\
12423 Set a fast tracepoint at specified line or function.\n\
12424 \n"
12425 BREAK_ARGS_HELP ("ftrace") "\n\
12426 Do \"help tracepoints\" for info on other tracepoint commands."));
12427   set_cmd_completer (c, location_completer);
12428
12429   c = add_com ("strace", class_breakpoint, strace_command, _("\
12430 Set a static tracepoint at specified line, function or marker.\n\
12431 \n\
12432 strace [LOCATION] [if CONDITION]\n\
12433 LOCATION may be a line number, function name, \"*\" and an address,\n\
12434 or -m MARKER_ID.\n\
12435 If a line number is specified, probe the marker at start of code\n\
12436 for that line.  If a function is specified, probe the marker at start\n\
12437 of code for that function.  If an address is specified, probe the marker\n\
12438 at that exact address.  If a marker id is specified, probe the marker\n\
12439 with that name.  With no LOCATION, uses current execution address of\n\
12440 the selected stack frame.\n\
12441 Static tracepoints accept an extra collect action -- ``collect $_sdata''.\n\
12442 This collects arbitrary user data passed in the probe point call to the\n\
12443 tracing library.  You can inspect it when analyzing the trace buffer,\n\
12444 by printing the $_sdata variable like any other convenience variable.\n\
12445 \n\
12446 CONDITION is a boolean expression.\n\
12447 \n\
12448 Multiple tracepoints at one place are permitted, and useful if their\n\
12449 conditions are different.\n\
12450 \n\
12451 Do \"help breakpoints\" for info on other commands dealing with breakpoints.\n\
12452 Do \"help tracepoints\" for info on other tracepoint commands."));
12453   set_cmd_completer (c, location_completer);
12454
12455   add_info ("tracepoints", tracepoints_info, _("\
12456 Status of specified tracepoints (all tracepoints if no argument).\n\
12457 Convenience variable \"$tpnum\" contains the number of the\n\
12458 last tracepoint set."));
12459
12460   add_info_alias ("tp", "tracepoints", 1);
12461
12462   add_cmd ("tracepoints", class_trace, delete_trace_command, _("\
12463 Delete specified tracepoints.\n\
12464 Arguments are tracepoint numbers, separated by spaces.\n\
12465 No argument means delete all tracepoints."),
12466            &deletelist);
12467
12468   c = add_cmd ("tracepoints", class_trace, disable_trace_command, _("\
12469 Disable specified tracepoints.\n\
12470 Arguments are tracepoint numbers, separated by spaces.\n\
12471 No argument means disable all tracepoints."),
12472            &disablelist);
12473   deprecate_cmd (c, "disable");
12474
12475   c = add_cmd ("tracepoints", class_trace, enable_trace_command, _("\
12476 Enable specified tracepoints.\n\
12477 Arguments are tracepoint numbers, separated by spaces.\n\
12478 No argument means enable all tracepoints."),
12479            &enablelist);
12480   deprecate_cmd (c, "enable");
12481
12482   add_com ("passcount", class_trace, trace_pass_command, _("\
12483 Set the passcount for a tracepoint.\n\
12484 The trace will end when the tracepoint has been passed 'count' times.\n\
12485 Usage: passcount COUNT TPNUM, where TPNUM may also be \"all\";\n\
12486 if TPNUM is omitted, passcount refers to the last tracepoint defined."));
12487
12488   add_prefix_cmd ("save", class_breakpoint, save_command,
12489                   _("Save breakpoint definitions as a script."),
12490                   &save_cmdlist, "save ",
12491                   0/*allow-unknown*/, &cmdlist);
12492
12493   c = add_cmd ("breakpoints", class_breakpoint, save_breakpoints_command, _("\
12494 Save current breakpoint definitions as a script.\n\
12495 This includes all types of breakpoints (breakpoints, watchpoints,\n\
12496 catchpoints, tracepoints).  Use the 'source' command in another debug\n\
12497 session to restore them."),
12498                &save_cmdlist);
12499   set_cmd_completer (c, filename_completer);
12500
12501   c = add_cmd ("tracepoints", class_trace, save_tracepoints_command, _("\
12502 Save current tracepoint definitions as a script.\n\
12503 Use the 'source' command in another debug session to restore them."),
12504                &save_cmdlist);
12505   set_cmd_completer (c, filename_completer);
12506
12507   c = add_com_alias ("save-tracepoints", "save tracepoints", class_trace, 0);
12508   deprecate_cmd (c, "save tracepoints");
12509
12510   add_prefix_cmd ("breakpoint", class_maintenance, set_breakpoint_cmd, _("\
12511 Breakpoint specific settings\n\
12512 Configure various breakpoint-specific variables such as\n\
12513 pending breakpoint behavior"),
12514                   &breakpoint_set_cmdlist, "set breakpoint ",
12515                   0/*allow-unknown*/, &setlist);
12516   add_prefix_cmd ("breakpoint", class_maintenance, show_breakpoint_cmd, _("\
12517 Breakpoint specific settings\n\
12518 Configure various breakpoint-specific variables such as\n\
12519 pending breakpoint behavior"),
12520                   &breakpoint_show_cmdlist, "show breakpoint ",
12521                   0/*allow-unknown*/, &showlist);
12522
12523   add_setshow_auto_boolean_cmd ("pending", no_class,
12524                                 &pending_break_support, _("\
12525 Set debugger's behavior regarding pending breakpoints."), _("\
12526 Show debugger's behavior regarding pending breakpoints."), _("\
12527 If on, an unrecognized breakpoint location will cause gdb to create a\n\
12528 pending breakpoint.  If off, an unrecognized breakpoint location results in\n\
12529 an error.  If auto, an unrecognized breakpoint location results in a\n\
12530 user-query to see if a pending breakpoint should be created."),
12531                                 NULL,
12532                                 show_pending_break_support,
12533                                 &breakpoint_set_cmdlist,
12534                                 &breakpoint_show_cmdlist);
12535
12536   pending_break_support = AUTO_BOOLEAN_AUTO;
12537
12538   add_setshow_boolean_cmd ("auto-hw", no_class,
12539                            &automatic_hardware_breakpoints, _("\
12540 Set automatic usage of hardware breakpoints."), _("\
12541 Show automatic usage of hardware breakpoints."), _("\
12542 If set, the debugger will automatically use hardware breakpoints for\n\
12543 breakpoints set with \"break\" but falling in read-only memory.  If not set,\n\
12544 a warning will be emitted for such breakpoints."),
12545                            NULL,
12546                            show_automatic_hardware_breakpoints,
12547                            &breakpoint_set_cmdlist,
12548                            &breakpoint_show_cmdlist);
12549
12550   add_setshow_enum_cmd ("always-inserted", class_support,
12551                         always_inserted_enums, &always_inserted_mode, _("\
12552 Set mode for inserting breakpoints."), _("\
12553 Show mode for inserting breakpoints."), _("\
12554 When this mode is off, breakpoints are inserted in inferior when it is\n\
12555 resumed, and removed when execution stops.  When this mode is on,\n\
12556 breakpoints are inserted immediately and removed only when the user\n\
12557 deletes the breakpoint.  When this mode is auto (which is the default),\n\
12558 the behaviour depends on the non-stop setting (see help set non-stop).\n\
12559 In this case, if gdb is controlling the inferior in non-stop mode, gdb\n\
12560 behaves as if always-inserted mode is on; if gdb is controlling the\n\
12561 inferior in all-stop mode, gdb behaves as if always-inserted mode is off."),
12562                            NULL,
12563                            &show_always_inserted_mode,
12564                            &breakpoint_set_cmdlist,
12565                            &breakpoint_show_cmdlist);
12566   
12567   automatic_hardware_breakpoints = 1;
12568
12569   observer_attach_about_to_proceed (breakpoint_about_to_proceed);
12570 }