gdb/
[external/binutils.git] / gdb / breakpoint.c
1 /* Everything about breakpoints, for GDB.
2
3    Copyright (C) 1986-2012 Free Software Foundation, Inc.
4
5    This file is part of GDB.
6
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 3 of the License, or
10    (at your option) any later version.
11
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16
17    You should have received a copy of the GNU General Public License
18    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
19
20 #include "defs.h"
21 #include "arch-utils.h"
22 #include <ctype.h>
23 #include "hashtab.h"
24 #include "symtab.h"
25 #include "frame.h"
26 #include "breakpoint.h"
27 #include "tracepoint.h"
28 #include "gdbtypes.h"
29 #include "expression.h"
30 #include "gdbcore.h"
31 #include "gdbcmd.h"
32 #include "value.h"
33 #include "command.h"
34 #include "inferior.h"
35 #include "gdbthread.h"
36 #include "target.h"
37 #include "language.h"
38 #include "gdb_string.h"
39 #include "gdb-demangle.h"
40 #include "filenames.h"
41 #include "annotate.h"
42 #include "symfile.h"
43 #include "objfiles.h"
44 #include "source.h"
45 #include "linespec.h"
46 #include "completer.h"
47 #include "gdb.h"
48 #include "ui-out.h"
49 #include "cli/cli-script.h"
50 #include "gdb_assert.h"
51 #include "block.h"
52 #include "solib.h"
53 #include "solist.h"
54 #include "observer.h"
55 #include "exceptions.h"
56 #include "memattr.h"
57 #include "ada-lang.h"
58 #include "top.h"
59 #include "valprint.h"
60 #include "jit.h"
61 #include "xml-syscall.h"
62 #include "parser-defs.h"
63 #include "gdb_regex.h"
64 #include "probe.h"
65 #include "cli/cli-utils.h"
66 #include "continuations.h"
67 #include "stack.h"
68 #include "skip.h"
69 #include "gdb_regex.h"
70 #include "ax-gdb.h"
71 #include "dummy-frame.h"
72
73 /* readline include files */
74 #include "readline/readline.h"
75 #include "readline/history.h"
76
77 /* readline defines this.  */
78 #undef savestring
79
80 #include "mi/mi-common.h"
81 #include "python/python.h"
82
83 /* Prototypes for local functions.  */
84
85 static void enable_delete_command (char *, int);
86
87 static void enable_once_command (char *, int);
88
89 static void enable_count_command (char *, int);
90
91 static void disable_command (char *, int);
92
93 static void enable_command (char *, int);
94
95 static void map_breakpoint_numbers (char *, void (*) (struct breakpoint *,
96                                                       void *),
97                                     void *);
98
99 static void ignore_command (char *, int);
100
101 static int breakpoint_re_set_one (void *);
102
103 static void breakpoint_re_set_default (struct breakpoint *);
104
105 static void create_sals_from_address_default (char **,
106                                               struct linespec_result *,
107                                               enum bptype, char *,
108                                               char **);
109
110 static void create_breakpoints_sal_default (struct gdbarch *,
111                                             struct linespec_result *,
112                                             struct linespec_sals *,
113                                             char *, char *, enum bptype,
114                                             enum bpdisp, int, int,
115                                             int,
116                                             const struct breakpoint_ops *,
117                                             int, int, int, unsigned);
118
119 static void decode_linespec_default (struct breakpoint *, char **,
120                                      struct symtabs_and_lines *);
121
122 static void clear_command (char *, int);
123
124 static void catch_command (char *, int);
125
126 static int can_use_hardware_watchpoint (struct value *);
127
128 static void break_command_1 (char *, int, int);
129
130 static void mention (struct breakpoint *);
131
132 static struct breakpoint *set_raw_breakpoint_without_location (struct gdbarch *,
133                                                                enum bptype,
134                                                                const struct breakpoint_ops *);
135 static struct bp_location *add_location_to_breakpoint (struct breakpoint *,
136                                                        const struct symtab_and_line *);
137
138 /* This function is used in gdbtk sources and thus can not be made
139    static.  */
140 struct breakpoint *set_raw_breakpoint (struct gdbarch *gdbarch,
141                                        struct symtab_and_line,
142                                        enum bptype,
143                                        const struct breakpoint_ops *);
144
145 static struct breakpoint *
146   momentary_breakpoint_from_master (struct breakpoint *orig,
147                                     enum bptype type,
148                                     const struct breakpoint_ops *ops);
149
150 static void breakpoint_adjustment_warning (CORE_ADDR, CORE_ADDR, int, int);
151
152 static CORE_ADDR adjust_breakpoint_address (struct gdbarch *gdbarch,
153                                             CORE_ADDR bpaddr,
154                                             enum bptype bptype);
155
156 static void describe_other_breakpoints (struct gdbarch *,
157                                         struct program_space *, CORE_ADDR,
158                                         struct obj_section *, int);
159
160 static int breakpoint_address_match (struct address_space *aspace1,
161                                      CORE_ADDR addr1,
162                                      struct address_space *aspace2,
163                                      CORE_ADDR addr2);
164
165 static int watchpoint_locations_match (struct bp_location *loc1,
166                                        struct bp_location *loc2);
167
168 static int breakpoint_location_address_match (struct bp_location *bl,
169                                               struct address_space *aspace,
170                                               CORE_ADDR addr);
171
172 static void breakpoints_info (char *, int);
173
174 static void watchpoints_info (char *, int);
175
176 static int breakpoint_1 (char *, int, 
177                          int (*) (const struct breakpoint *));
178
179 static int breakpoint_cond_eval (void *);
180
181 static void cleanup_executing_breakpoints (void *);
182
183 static void commands_command (char *, int);
184
185 static void condition_command (char *, int);
186
187 typedef enum
188   {
189     mark_inserted,
190     mark_uninserted
191   }
192 insertion_state_t;
193
194 static int remove_breakpoint (struct bp_location *, insertion_state_t);
195 static int remove_breakpoint_1 (struct bp_location *, insertion_state_t);
196
197 static enum print_stop_action print_bp_stop_message (bpstat bs);
198
199 static int watchpoint_check (void *);
200
201 static void maintenance_info_breakpoints (char *, int);
202
203 static int hw_breakpoint_used_count (void);
204
205 static int hw_watchpoint_use_count (struct breakpoint *);
206
207 static int hw_watchpoint_used_count_others (struct breakpoint *except,
208                                             enum bptype type,
209                                             int *other_type_used);
210
211 static void hbreak_command (char *, int);
212
213 static void thbreak_command (char *, int);
214
215 static void enable_breakpoint_disp (struct breakpoint *, enum bpdisp,
216                                     int count);
217
218 static void stop_command (char *arg, int from_tty);
219
220 static void stopin_command (char *arg, int from_tty);
221
222 static void stopat_command (char *arg, int from_tty);
223
224 static char *ep_parse_optional_if_clause (char **arg);
225
226 static void catch_exception_command_1 (enum exception_event_kind ex_event, 
227                                        char *arg, int tempflag, int from_tty);
228
229 static void tcatch_command (char *arg, int from_tty);
230
231 static void detach_single_step_breakpoints (void);
232
233 static int single_step_breakpoint_inserted_here_p (struct address_space *,
234                                                    CORE_ADDR pc);
235
236 static void free_bp_location (struct bp_location *loc);
237 static void incref_bp_location (struct bp_location *loc);
238 static void decref_bp_location (struct bp_location **loc);
239
240 static struct bp_location *allocate_bp_location (struct breakpoint *bpt);
241
242 static void update_global_location_list (int);
243
244 static void update_global_location_list_nothrow (int);
245
246 static int is_hardware_watchpoint (const struct breakpoint *bpt);
247
248 static void insert_breakpoint_locations (void);
249
250 static int syscall_catchpoint_p (struct breakpoint *b);
251
252 static void tracepoints_info (char *, int);
253
254 static void delete_trace_command (char *, int);
255
256 static void enable_trace_command (char *, int);
257
258 static void disable_trace_command (char *, int);
259
260 static void trace_pass_command (char *, int);
261
262 static int is_masked_watchpoint (const struct breakpoint *b);
263
264 static struct bp_location **get_first_locp_gte_addr (CORE_ADDR address);
265
266 /* Return 1 if B refers to a static tracepoint set by marker ("-m"), zero
267    otherwise.  */
268
269 static int strace_marker_p (struct breakpoint *b);
270
271 static void init_catchpoint (struct breakpoint *b,
272                              struct gdbarch *gdbarch, int tempflag,
273                              char *cond_string,
274                              const struct breakpoint_ops *ops);
275
276 /* The abstract base class all breakpoint_ops structures inherit
277    from.  */
278 static struct breakpoint_ops base_breakpoint_ops;
279
280 /* The breakpoint_ops structure to be inherited by all breakpoint_ops
281    that are implemented on top of software or hardware breakpoints
282    (user breakpoints, internal and momentary breakpoints, etc.).  */
283 static struct breakpoint_ops bkpt_base_breakpoint_ops;
284
285 /* Internal breakpoints class type.  */
286 static struct breakpoint_ops internal_breakpoint_ops;
287
288 /* Momentary breakpoints class type.  */
289 static struct breakpoint_ops momentary_breakpoint_ops;
290
291 /* Momentary breakpoints for bp_longjmp and bp_exception class type.  */
292 static struct breakpoint_ops longjmp_breakpoint_ops;
293
294 /* The breakpoint_ops structure to be used in regular user created
295    breakpoints.  */
296 struct breakpoint_ops bkpt_breakpoint_ops;
297
298 /* Breakpoints set on probes.  */
299 static struct breakpoint_ops bkpt_probe_breakpoint_ops;
300
301 /* Dynamic printf class type.  */
302 static struct breakpoint_ops dprintf_breakpoint_ops;
303
304 /* A reference-counted struct command_line.  This lets multiple
305    breakpoints share a single command list.  */
306 struct counted_command_line
307 {
308   /* The reference count.  */
309   int refc;
310
311   /* The command list.  */
312   struct command_line *commands;
313 };
314
315 struct command_line *
316 breakpoint_commands (struct breakpoint *b)
317 {
318   return b->commands ? b->commands->commands : NULL;
319 }
320
321 /* Flag indicating that a command has proceeded the inferior past the
322    current breakpoint.  */
323
324 static int breakpoint_proceeded;
325
326 const char *
327 bpdisp_text (enum bpdisp disp)
328 {
329   /* NOTE: the following values are a part of MI protocol and
330      represent values of 'disp' field returned when inferior stops at
331      a breakpoint.  */
332   static const char * const bpdisps[] = {"del", "dstp", "dis", "keep"};
333
334   return bpdisps[(int) disp];
335 }
336
337 /* Prototypes for exported functions.  */
338 /* If FALSE, gdb will not use hardware support for watchpoints, even
339    if such is available.  */
340 static int can_use_hw_watchpoints;
341
342 static void
343 show_can_use_hw_watchpoints (struct ui_file *file, int from_tty,
344                              struct cmd_list_element *c,
345                              const char *value)
346 {
347   fprintf_filtered (file,
348                     _("Debugger's willingness to use "
349                       "watchpoint hardware is %s.\n"),
350                     value);
351 }
352
353 /* If AUTO_BOOLEAN_FALSE, gdb will not attempt to create pending breakpoints.
354    If AUTO_BOOLEAN_TRUE, gdb will automatically create pending breakpoints
355    for unrecognized breakpoint locations.
356    If AUTO_BOOLEAN_AUTO, gdb will query when breakpoints are unrecognized.  */
357 static enum auto_boolean pending_break_support;
358 static void
359 show_pending_break_support (struct ui_file *file, int from_tty,
360                             struct cmd_list_element *c,
361                             const char *value)
362 {
363   fprintf_filtered (file,
364                     _("Debugger's behavior regarding "
365                       "pending breakpoints is %s.\n"),
366                     value);
367 }
368
369 /* If 1, gdb will automatically use hardware breakpoints for breakpoints
370    set with "break" but falling in read-only memory.
371    If 0, gdb will warn about such breakpoints, but won't automatically
372    use hardware breakpoints.  */
373 static int automatic_hardware_breakpoints;
374 static void
375 show_automatic_hardware_breakpoints (struct ui_file *file, int from_tty,
376                                      struct cmd_list_element *c,
377                                      const char *value)
378 {
379   fprintf_filtered (file,
380                     _("Automatic usage of hardware breakpoints is %s.\n"),
381                     value);
382 }
383
384 /* If on, gdb will keep breakpoints inserted even as inferior is
385    stopped, and immediately insert any new breakpoints.  If off, gdb
386    will insert breakpoints into inferior only when resuming it, and
387    will remove breakpoints upon stop.  If auto, GDB will behave as ON
388    if in non-stop mode, and as OFF if all-stop mode.*/
389
390 static const char always_inserted_auto[] = "auto";
391 static const char always_inserted_on[] = "on";
392 static const char always_inserted_off[] = "off";
393 static const char *const always_inserted_enums[] = {
394   always_inserted_auto,
395   always_inserted_off,
396   always_inserted_on,
397   NULL
398 };
399 static const char *always_inserted_mode = always_inserted_auto;
400 static void
401 show_always_inserted_mode (struct ui_file *file, int from_tty,
402                      struct cmd_list_element *c, const char *value)
403 {
404   if (always_inserted_mode == always_inserted_auto)
405     fprintf_filtered (file,
406                       _("Always inserted breakpoint "
407                         "mode is %s (currently %s).\n"),
408                       value,
409                       breakpoints_always_inserted_mode () ? "on" : "off");
410   else
411     fprintf_filtered (file, _("Always inserted breakpoint mode is %s.\n"),
412                       value);
413 }
414
415 int
416 breakpoints_always_inserted_mode (void)
417 {
418   return (always_inserted_mode == always_inserted_on
419           || (always_inserted_mode == always_inserted_auto && non_stop));
420 }
421
422 static const char condition_evaluation_both[] = "host or target";
423
424 /* Modes for breakpoint condition evaluation.  */
425 static const char condition_evaluation_auto[] = "auto";
426 static const char condition_evaluation_host[] = "host";
427 static const char condition_evaluation_target[] = "target";
428 static const char *const condition_evaluation_enums[] = {
429   condition_evaluation_auto,
430   condition_evaluation_host,
431   condition_evaluation_target,
432   NULL
433 };
434
435 /* Global that holds the current mode for breakpoint condition evaluation.  */
436 static const char *condition_evaluation_mode_1 = condition_evaluation_auto;
437
438 /* Global that we use to display information to the user (gets its value from
439    condition_evaluation_mode_1.  */
440 static const char *condition_evaluation_mode = condition_evaluation_auto;
441
442 /* Translate a condition evaluation mode MODE into either "host"
443    or "target".  This is used mostly to translate from "auto" to the
444    real setting that is being used.  It returns the translated
445    evaluation mode.  */
446
447 static const char *
448 translate_condition_evaluation_mode (const char *mode)
449 {
450   if (mode == condition_evaluation_auto)
451     {
452       if (target_supports_evaluation_of_breakpoint_conditions ())
453         return condition_evaluation_target;
454       else
455         return condition_evaluation_host;
456     }
457   else
458     return mode;
459 }
460
461 /* Discovers what condition_evaluation_auto translates to.  */
462
463 static const char *
464 breakpoint_condition_evaluation_mode (void)
465 {
466   return translate_condition_evaluation_mode (condition_evaluation_mode);
467 }
468
469 /* Return true if GDB should evaluate breakpoint conditions or false
470    otherwise.  */
471
472 static int
473 gdb_evaluates_breakpoint_condition_p (void)
474 {
475   const char *mode = breakpoint_condition_evaluation_mode ();
476
477   return (mode == condition_evaluation_host);
478 }
479
480 void _initialize_breakpoint (void);
481
482 /* Are we executing breakpoint commands?  */
483 static int executing_breakpoint_commands;
484
485 /* Are overlay event breakpoints enabled? */
486 static int overlay_events_enabled;
487
488 /* See description in breakpoint.h. */
489 int target_exact_watchpoints = 0;
490
491 /* Walk the following statement or block through all breakpoints.
492    ALL_BREAKPOINTS_SAFE does so even if the statement deletes the
493    current breakpoint.  */
494
495 #define ALL_BREAKPOINTS(B)  for (B = breakpoint_chain; B; B = B->next)
496
497 #define ALL_BREAKPOINTS_SAFE(B,TMP)     \
498         for (B = breakpoint_chain;      \
499              B ? (TMP=B->next, 1): 0;   \
500              B = TMP)
501
502 /* Similar iterator for the low-level breakpoints.  SAFE variant is
503    not provided so update_global_location_list must not be called
504    while executing the block of ALL_BP_LOCATIONS.  */
505
506 #define ALL_BP_LOCATIONS(B,BP_TMP)                                      \
507         for (BP_TMP = bp_location;                                      \
508              BP_TMP < bp_location + bp_location_count && (B = *BP_TMP); \
509              BP_TMP++)
510
511 /* Iterates through locations with address ADDRESS for the currently selected
512    program space.  BP_LOCP_TMP points to each object.  BP_LOCP_START points
513    to where the loop should start from.
514    If BP_LOCP_START is a NULL pointer, the macro automatically seeks the
515    appropriate location to start with.  */
516
517 #define ALL_BP_LOCATIONS_AT_ADDR(BP_LOCP_TMP, BP_LOCP_START, ADDRESS)   \
518         for (BP_LOCP_START = BP_LOCP_START == NULL ? get_first_locp_gte_addr (ADDRESS) : BP_LOCP_START, \
519              BP_LOCP_TMP = BP_LOCP_START;                               \
520              BP_LOCP_START                                              \
521              && (BP_LOCP_TMP < bp_location + bp_location_count          \
522              && (*BP_LOCP_TMP)->address == ADDRESS);                    \
523              BP_LOCP_TMP++)
524
525 /* Iterator for tracepoints only.  */
526
527 #define ALL_TRACEPOINTS(B)  \
528   for (B = breakpoint_chain; B; B = B->next)  \
529     if (is_tracepoint (B))
530
531 /* Chains of all breakpoints defined.  */
532
533 struct breakpoint *breakpoint_chain;
534
535 /* Array is sorted by bp_location_compare - primarily by the ADDRESS.  */
536
537 static struct bp_location **bp_location;
538
539 /* Number of elements of BP_LOCATION.  */
540
541 static unsigned bp_location_count;
542
543 /* Maximum alignment offset between bp_target_info.PLACED_ADDRESS and
544    ADDRESS for the current elements of BP_LOCATION which get a valid
545    result from bp_location_has_shadow.  You can use it for roughly
546    limiting the subrange of BP_LOCATION to scan for shadow bytes for
547    an address you need to read.  */
548
549 static CORE_ADDR bp_location_placed_address_before_address_max;
550
551 /* Maximum offset plus alignment between bp_target_info.PLACED_ADDRESS
552    + bp_target_info.SHADOW_LEN and ADDRESS for the current elements of
553    BP_LOCATION which get a valid result from bp_location_has_shadow.
554    You can use it for roughly limiting the subrange of BP_LOCATION to
555    scan for shadow bytes for an address you need to read.  */
556
557 static CORE_ADDR bp_location_shadow_len_after_address_max;
558
559 /* The locations that no longer correspond to any breakpoint, unlinked
560    from bp_location array, but for which a hit may still be reported
561    by a target.  */
562 VEC(bp_location_p) *moribund_locations = NULL;
563
564 /* Number of last breakpoint made.  */
565
566 static int breakpoint_count;
567
568 /* The value of `breakpoint_count' before the last command that
569    created breakpoints.  If the last (break-like) command created more
570    than one breakpoint, then the difference between BREAKPOINT_COUNT
571    and PREV_BREAKPOINT_COUNT is more than one.  */
572 static int prev_breakpoint_count;
573
574 /* Number of last tracepoint made.  */
575
576 static int tracepoint_count;
577
578 static struct cmd_list_element *breakpoint_set_cmdlist;
579 static struct cmd_list_element *breakpoint_show_cmdlist;
580 struct cmd_list_element *save_cmdlist;
581
582 /* Return whether a breakpoint is an active enabled breakpoint.  */
583 static int
584 breakpoint_enabled (struct breakpoint *b)
585 {
586   return (b->enable_state == bp_enabled);
587 }
588
589 /* Set breakpoint count to NUM.  */
590
591 static void
592 set_breakpoint_count (int num)
593 {
594   prev_breakpoint_count = breakpoint_count;
595   breakpoint_count = num;
596   set_internalvar_integer (lookup_internalvar ("bpnum"), num);
597 }
598
599 /* Used by `start_rbreak_breakpoints' below, to record the current
600    breakpoint count before "rbreak" creates any breakpoint.  */
601 static int rbreak_start_breakpoint_count;
602
603 /* Called at the start an "rbreak" command to record the first
604    breakpoint made.  */
605
606 void
607 start_rbreak_breakpoints (void)
608 {
609   rbreak_start_breakpoint_count = breakpoint_count;
610 }
611
612 /* Called at the end of an "rbreak" command to record the last
613    breakpoint made.  */
614
615 void
616 end_rbreak_breakpoints (void)
617 {
618   prev_breakpoint_count = rbreak_start_breakpoint_count;
619 }
620
621 /* Used in run_command to zero the hit count when a new run starts.  */
622
623 void
624 clear_breakpoint_hit_counts (void)
625 {
626   struct breakpoint *b;
627
628   ALL_BREAKPOINTS (b)
629     b->hit_count = 0;
630 }
631
632 /* Allocate a new counted_command_line with reference count of 1.
633    The new structure owns COMMANDS.  */
634
635 static struct counted_command_line *
636 alloc_counted_command_line (struct command_line *commands)
637 {
638   struct counted_command_line *result
639     = xmalloc (sizeof (struct counted_command_line));
640
641   result->refc = 1;
642   result->commands = commands;
643   return result;
644 }
645
646 /* Increment reference count.  This does nothing if CMD is NULL.  */
647
648 static void
649 incref_counted_command_line (struct counted_command_line *cmd)
650 {
651   if (cmd)
652     ++cmd->refc;
653 }
654
655 /* Decrement reference count.  If the reference count reaches 0,
656    destroy the counted_command_line.  Sets *CMDP to NULL.  This does
657    nothing if *CMDP is NULL.  */
658
659 static void
660 decref_counted_command_line (struct counted_command_line **cmdp)
661 {
662   if (*cmdp)
663     {
664       if (--(*cmdp)->refc == 0)
665         {
666           free_command_lines (&(*cmdp)->commands);
667           xfree (*cmdp);
668         }
669       *cmdp = NULL;
670     }
671 }
672
673 /* A cleanup function that calls decref_counted_command_line.  */
674
675 static void
676 do_cleanup_counted_command_line (void *arg)
677 {
678   decref_counted_command_line (arg);
679 }
680
681 /* Create a cleanup that calls decref_counted_command_line on the
682    argument.  */
683
684 static struct cleanup *
685 make_cleanup_decref_counted_command_line (struct counted_command_line **cmdp)
686 {
687   return make_cleanup (do_cleanup_counted_command_line, cmdp);
688 }
689
690 \f
691 /* Return the breakpoint with the specified number, or NULL
692    if the number does not refer to an existing breakpoint.  */
693
694 struct breakpoint *
695 get_breakpoint (int num)
696 {
697   struct breakpoint *b;
698
699   ALL_BREAKPOINTS (b)
700     if (b->number == num)
701       return b;
702   
703   return NULL;
704 }
705
706 \f
707
708 /* Mark locations as "conditions have changed" in case the target supports
709    evaluating conditions on its side.  */
710
711 static void
712 mark_breakpoint_modified (struct breakpoint *b)
713 {
714   struct bp_location *loc;
715
716   /* This is only meaningful if the target is
717      evaluating conditions and if the user has
718      opted for condition evaluation on the target's
719      side.  */
720   if (gdb_evaluates_breakpoint_condition_p ()
721       || !target_supports_evaluation_of_breakpoint_conditions ())
722     return;
723
724   if (!is_breakpoint (b))
725     return;
726
727   for (loc = b->loc; loc; loc = loc->next)
728     loc->condition_changed = condition_modified;
729 }
730
731 /* Mark location as "conditions have changed" in case the target supports
732    evaluating conditions on its side.  */
733
734 static void
735 mark_breakpoint_location_modified (struct bp_location *loc)
736 {
737   /* This is only meaningful if the target is
738      evaluating conditions and if the user has
739      opted for condition evaluation on the target's
740      side.  */
741   if (gdb_evaluates_breakpoint_condition_p ()
742       || !target_supports_evaluation_of_breakpoint_conditions ())
743
744     return;
745
746   if (!is_breakpoint (loc->owner))
747     return;
748
749   loc->condition_changed = condition_modified;
750 }
751
752 /* Sets the condition-evaluation mode using the static global
753    condition_evaluation_mode.  */
754
755 static void
756 set_condition_evaluation_mode (char *args, int from_tty,
757                                struct cmd_list_element *c)
758 {
759   const char *old_mode, *new_mode;
760
761   if ((condition_evaluation_mode_1 == condition_evaluation_target)
762       && !target_supports_evaluation_of_breakpoint_conditions ())
763     {
764       condition_evaluation_mode_1 = condition_evaluation_mode;
765       warning (_("Target does not support breakpoint condition evaluation.\n"
766                  "Using host evaluation mode instead."));
767       return;
768     }
769
770   new_mode = translate_condition_evaluation_mode (condition_evaluation_mode_1);
771   old_mode = translate_condition_evaluation_mode (condition_evaluation_mode);
772
773   /* Flip the switch.  Flip it even if OLD_MODE == NEW_MODE as one of the
774      settings was "auto".  */
775   condition_evaluation_mode = condition_evaluation_mode_1;
776
777   /* Only update the mode if the user picked a different one.  */
778   if (new_mode != old_mode)
779     {
780       struct bp_location *loc, **loc_tmp;
781       /* If the user switched to a different evaluation mode, we
782          need to synch the changes with the target as follows:
783
784          "host" -> "target": Send all (valid) conditions to the target.
785          "target" -> "host": Remove all the conditions from the target.
786       */
787
788       if (new_mode == condition_evaluation_target)
789         {
790           /* Mark everything modified and synch conditions with the
791              target.  */
792           ALL_BP_LOCATIONS (loc, loc_tmp)
793             mark_breakpoint_location_modified (loc);
794         }
795       else
796         {
797           /* Manually mark non-duplicate locations to synch conditions
798              with the target.  We do this to remove all the conditions the
799              target knows about.  */
800           ALL_BP_LOCATIONS (loc, loc_tmp)
801             if (is_breakpoint (loc->owner) && loc->inserted)
802               loc->needs_update = 1;
803         }
804
805       /* Do the update.  */
806       update_global_location_list (1);
807     }
808
809   return;
810 }
811
812 /* Shows the current mode of breakpoint condition evaluation.  Explicitly shows
813    what "auto" is translating to.  */
814
815 static void
816 show_condition_evaluation_mode (struct ui_file *file, int from_tty,
817                                 struct cmd_list_element *c, const char *value)
818 {
819   if (condition_evaluation_mode == condition_evaluation_auto)
820     fprintf_filtered (file,
821                       _("Breakpoint condition evaluation "
822                         "mode is %s (currently %s).\n"),
823                       value,
824                       breakpoint_condition_evaluation_mode ());
825   else
826     fprintf_filtered (file, _("Breakpoint condition evaluation mode is %s.\n"),
827                       value);
828 }
829
830 /* A comparison function for bp_location AP and BP that is used by
831    bsearch.  This comparison function only cares about addresses, unlike
832    the more general bp_location_compare function.  */
833
834 static int
835 bp_location_compare_addrs (const void *ap, const void *bp)
836 {
837   struct bp_location *a = *(void **) ap;
838   struct bp_location *b = *(void **) bp;
839
840   if (a->address == b->address)
841     return 0;
842   else
843     return ((a->address > b->address) - (a->address < b->address));
844 }
845
846 /* Helper function to skip all bp_locations with addresses
847    less than ADDRESS.  It returns the first bp_location that
848    is greater than or equal to ADDRESS.  If none is found, just
849    return NULL.  */
850
851 static struct bp_location **
852 get_first_locp_gte_addr (CORE_ADDR address)
853 {
854   struct bp_location dummy_loc;
855   struct bp_location *dummy_locp = &dummy_loc;
856   struct bp_location **locp_found = NULL;
857
858   /* Initialize the dummy location's address field.  */
859   memset (&dummy_loc, 0, sizeof (struct bp_location));
860   dummy_loc.address = address;
861
862   /* Find a close match to the first location at ADDRESS.  */
863   locp_found = bsearch (&dummy_locp, bp_location, bp_location_count,
864                         sizeof (struct bp_location **),
865                         bp_location_compare_addrs);
866
867   /* Nothing was found, nothing left to do.  */
868   if (locp_found == NULL)
869     return NULL;
870
871   /* We may have found a location that is at ADDRESS but is not the first in the
872      location's list.  Go backwards (if possible) and locate the first one.  */
873   while ((locp_found - 1) >= bp_location
874          && (*(locp_found - 1))->address == address)
875     locp_found--;
876
877   return locp_found;
878 }
879
880 void
881 set_breakpoint_condition (struct breakpoint *b, char *exp,
882                           int from_tty)
883 {
884   xfree (b->cond_string);
885   b->cond_string = NULL;
886
887   if (is_watchpoint (b))
888     {
889       struct watchpoint *w = (struct watchpoint *) b;
890
891       xfree (w->cond_exp);
892       w->cond_exp = NULL;
893     }
894   else
895     {
896       struct bp_location *loc;
897
898       for (loc = b->loc; loc; loc = loc->next)
899         {
900           xfree (loc->cond);
901           loc->cond = NULL;
902
903           /* No need to free the condition agent expression
904              bytecode (if we have one).  We will handle this
905              when we go through update_global_location_list.  */
906         }
907     }
908
909   if (*exp == 0)
910     {
911       if (from_tty)
912         printf_filtered (_("Breakpoint %d now unconditional.\n"), b->number);
913     }
914   else
915     {
916       char *arg = exp;
917
918       /* I don't know if it matters whether this is the string the user
919          typed in or the decompiled expression.  */
920       b->cond_string = xstrdup (arg);
921       b->condition_not_parsed = 0;
922
923       if (is_watchpoint (b))
924         {
925           struct watchpoint *w = (struct watchpoint *) b;
926
927           innermost_block = NULL;
928           arg = exp;
929           w->cond_exp = parse_exp_1 (&arg, 0, 0);
930           if (*arg)
931             error (_("Junk at end of expression"));
932           w->cond_exp_valid_block = innermost_block;
933         }
934       else
935         {
936           struct bp_location *loc;
937
938           for (loc = b->loc; loc; loc = loc->next)
939             {
940               arg = exp;
941               loc->cond =
942                 parse_exp_1 (&arg, block_for_pc (loc->address), 0);
943               if (*arg)
944                 error (_("Junk at end of expression"));
945             }
946         }
947     }
948   mark_breakpoint_modified (b);
949
950   breakpoints_changed ();
951   observer_notify_breakpoint_modified (b);
952 }
953
954 /* Completion for the "condition" command.  */
955
956 static VEC (char_ptr) *
957 condition_completer (struct cmd_list_element *cmd, char *text, char *word)
958 {
959   char *space;
960
961   text = skip_spaces (text);
962   space = skip_to_space (text);
963   if (*space == '\0')
964     {
965       int len;
966       struct breakpoint *b;
967       VEC (char_ptr) *result = NULL;
968
969       if (text[0] == '$')
970         {
971           /* We don't support completion of history indices.  */
972           if (isdigit (text[1]))
973             return NULL;
974           return complete_internalvar (&text[1]);
975         }
976
977       /* We're completing the breakpoint number.  */
978       len = strlen (text);
979
980       ALL_BREAKPOINTS (b)
981       {
982         int single = b->loc->next == NULL;
983         struct bp_location *loc;
984         int count = 1;
985
986         for (loc = b->loc; loc; loc = loc->next)
987           {
988             char location[50];
989
990             if (single)
991               sprintf (location, "%d", b->number);
992             else
993               sprintf (location, "%d.%d", b->number, count);
994
995             if (strncmp (location, text, len) == 0)
996               VEC_safe_push (char_ptr, result, xstrdup (location));
997
998             ++count;
999           }
1000       }
1001
1002       return result;
1003     }
1004
1005   /* We're completing the expression part.  */
1006   text = skip_spaces (space);
1007   return expression_completer (cmd, text, word);
1008 }
1009
1010 /* condition N EXP -- set break condition of breakpoint N to EXP.  */
1011
1012 static void
1013 condition_command (char *arg, int from_tty)
1014 {
1015   struct breakpoint *b;
1016   char *p;
1017   int bnum;
1018
1019   if (arg == 0)
1020     error_no_arg (_("breakpoint number"));
1021
1022   p = arg;
1023   bnum = get_number (&p);
1024   if (bnum == 0)
1025     error (_("Bad breakpoint argument: '%s'"), arg);
1026
1027   ALL_BREAKPOINTS (b)
1028     if (b->number == bnum)
1029       {
1030         /* Check if this breakpoint has a Python object assigned to
1031            it, and if it has a definition of the "stop"
1032            method.  This method and conditions entered into GDB from
1033            the CLI are mutually exclusive.  */
1034         if (b->py_bp_object
1035             && gdbpy_breakpoint_has_py_cond (b->py_bp_object))
1036           error (_("Cannot set a condition where a Python 'stop' "
1037                    "method has been defined in the breakpoint."));
1038         set_breakpoint_condition (b, p, from_tty);
1039
1040         if (is_breakpoint (b))
1041           update_global_location_list (1);
1042
1043         return;
1044       }
1045
1046   error (_("No breakpoint number %d."), bnum);
1047 }
1048
1049 /* Check that COMMAND do not contain commands that are suitable
1050    only for tracepoints and not suitable for ordinary breakpoints.
1051    Throw if any such commands is found.  */
1052
1053 static void
1054 check_no_tracepoint_commands (struct command_line *commands)
1055 {
1056   struct command_line *c;
1057
1058   for (c = commands; c; c = c->next)
1059     {
1060       int i;
1061
1062       if (c->control_type == while_stepping_control)
1063         error (_("The 'while-stepping' command can "
1064                  "only be used for tracepoints"));
1065
1066       for (i = 0; i < c->body_count; ++i)
1067         check_no_tracepoint_commands ((c->body_list)[i]);
1068
1069       /* Not that command parsing removes leading whitespace and comment
1070          lines and also empty lines.  So, we only need to check for
1071          command directly.  */
1072       if (strstr (c->line, "collect ") == c->line)
1073         error (_("The 'collect' command can only be used for tracepoints"));
1074
1075       if (strstr (c->line, "teval ") == c->line)
1076         error (_("The 'teval' command can only be used for tracepoints"));
1077     }
1078 }
1079
1080 /* Encapsulate tests for different types of tracepoints.  */
1081
1082 static int
1083 is_tracepoint_type (enum bptype type)
1084 {
1085   return (type == bp_tracepoint
1086           || type == bp_fast_tracepoint
1087           || type == bp_static_tracepoint);
1088 }
1089
1090 int
1091 is_tracepoint (const struct breakpoint *b)
1092 {
1093   return is_tracepoint_type (b->type);
1094 }
1095
1096 /* A helper function that validates that COMMANDS are valid for a
1097    breakpoint.  This function will throw an exception if a problem is
1098    found.  */
1099
1100 static void
1101 validate_commands_for_breakpoint (struct breakpoint *b,
1102                                   struct command_line *commands)
1103 {
1104   if (is_tracepoint (b))
1105     {
1106       /* We need to verify that each top-level element of commands is
1107          valid for tracepoints, that there's at most one
1108          while-stepping element, and that while-stepping's body has
1109          valid tracing commands excluding nested while-stepping.  */
1110       struct command_line *c;
1111       struct command_line *while_stepping = 0;
1112       for (c = commands; c; c = c->next)
1113         {
1114           if (c->control_type == while_stepping_control)
1115             {
1116               if (b->type == bp_fast_tracepoint)
1117                 error (_("The 'while-stepping' command "
1118                          "cannot be used for fast tracepoint"));
1119               else if (b->type == bp_static_tracepoint)
1120                 error (_("The 'while-stepping' command "
1121                          "cannot be used for static tracepoint"));
1122
1123               if (while_stepping)
1124                 error (_("The 'while-stepping' command "
1125                          "can be used only once"));
1126               else
1127                 while_stepping = c;
1128             }
1129         }
1130       if (while_stepping)
1131         {
1132           struct command_line *c2;
1133
1134           gdb_assert (while_stepping->body_count == 1);
1135           c2 = while_stepping->body_list[0];
1136           for (; c2; c2 = c2->next)
1137             {
1138               if (c2->control_type == while_stepping_control)
1139                 error (_("The 'while-stepping' command cannot be nested"));
1140             }
1141         }
1142     }
1143   else
1144     {
1145       check_no_tracepoint_commands (commands);
1146     }
1147 }
1148
1149 /* Return a vector of all the static tracepoints set at ADDR.  The
1150    caller is responsible for releasing the vector.  */
1151
1152 VEC(breakpoint_p) *
1153 static_tracepoints_here (CORE_ADDR addr)
1154 {
1155   struct breakpoint *b;
1156   VEC(breakpoint_p) *found = 0;
1157   struct bp_location *loc;
1158
1159   ALL_BREAKPOINTS (b)
1160     if (b->type == bp_static_tracepoint)
1161       {
1162         for (loc = b->loc; loc; loc = loc->next)
1163           if (loc->address == addr)
1164             VEC_safe_push(breakpoint_p, found, b);
1165       }
1166
1167   return found;
1168 }
1169
1170 /* Set the command list of B to COMMANDS.  If breakpoint is tracepoint,
1171    validate that only allowed commands are included.  */
1172
1173 void
1174 breakpoint_set_commands (struct breakpoint *b, 
1175                          struct command_line *commands)
1176 {
1177   validate_commands_for_breakpoint (b, commands);
1178
1179   decref_counted_command_line (&b->commands);
1180   b->commands = alloc_counted_command_line (commands);
1181   breakpoints_changed ();
1182   observer_notify_breakpoint_modified (b);
1183 }
1184
1185 /* Set the internal `silent' flag on the breakpoint.  Note that this
1186    is not the same as the "silent" that may appear in the breakpoint's
1187    commands.  */
1188
1189 void
1190 breakpoint_set_silent (struct breakpoint *b, int silent)
1191 {
1192   int old_silent = b->silent;
1193
1194   b->silent = silent;
1195   if (old_silent != silent)
1196     observer_notify_breakpoint_modified (b);
1197 }
1198
1199 /* Set the thread for this breakpoint.  If THREAD is -1, make the
1200    breakpoint work for any thread.  */
1201
1202 void
1203 breakpoint_set_thread (struct breakpoint *b, int thread)
1204 {
1205   int old_thread = b->thread;
1206
1207   b->thread = thread;
1208   if (old_thread != thread)
1209     observer_notify_breakpoint_modified (b);
1210 }
1211
1212 /* Set the task for this breakpoint.  If TASK is 0, make the
1213    breakpoint work for any task.  */
1214
1215 void
1216 breakpoint_set_task (struct breakpoint *b, int task)
1217 {
1218   int old_task = b->task;
1219
1220   b->task = task;
1221   if (old_task != task)
1222     observer_notify_breakpoint_modified (b);
1223 }
1224
1225 void
1226 check_tracepoint_command (char *line, void *closure)
1227 {
1228   struct breakpoint *b = closure;
1229
1230   validate_actionline (&line, b);
1231 }
1232
1233 /* A structure used to pass information through
1234    map_breakpoint_numbers.  */
1235
1236 struct commands_info
1237 {
1238   /* True if the command was typed at a tty.  */
1239   int from_tty;
1240
1241   /* The breakpoint range spec.  */
1242   char *arg;
1243
1244   /* Non-NULL if the body of the commands are being read from this
1245      already-parsed command.  */
1246   struct command_line *control;
1247
1248   /* The command lines read from the user, or NULL if they have not
1249      yet been read.  */
1250   struct counted_command_line *cmd;
1251 };
1252
1253 /* A callback for map_breakpoint_numbers that sets the commands for
1254    commands_command.  */
1255
1256 static void
1257 do_map_commands_command (struct breakpoint *b, void *data)
1258 {
1259   struct commands_info *info = data;
1260
1261   if (info->cmd == NULL)
1262     {
1263       struct command_line *l;
1264
1265       if (info->control != NULL)
1266         l = copy_command_lines (info->control->body_list[0]);
1267       else
1268         {
1269           struct cleanup *old_chain;
1270           char *str;
1271
1272           str = xstrprintf (_("Type commands for breakpoint(s) "
1273                               "%s, one per line."),
1274                             info->arg);
1275
1276           old_chain = make_cleanup (xfree, str);
1277
1278           l = read_command_lines (str,
1279                                   info->from_tty, 1,
1280                                   (is_tracepoint (b)
1281                                    ? check_tracepoint_command : 0),
1282                                   b);
1283
1284           do_cleanups (old_chain);
1285         }
1286
1287       info->cmd = alloc_counted_command_line (l);
1288     }
1289
1290   /* If a breakpoint was on the list more than once, we don't need to
1291      do anything.  */
1292   if (b->commands != info->cmd)
1293     {
1294       validate_commands_for_breakpoint (b, info->cmd->commands);
1295       incref_counted_command_line (info->cmd);
1296       decref_counted_command_line (&b->commands);
1297       b->commands = info->cmd;
1298       breakpoints_changed ();
1299       observer_notify_breakpoint_modified (b);
1300     }
1301 }
1302
1303 static void
1304 commands_command_1 (char *arg, int from_tty, 
1305                     struct command_line *control)
1306 {
1307   struct cleanup *cleanups;
1308   struct commands_info info;
1309
1310   info.from_tty = from_tty;
1311   info.control = control;
1312   info.cmd = NULL;
1313   /* If we read command lines from the user, then `info' will hold an
1314      extra reference to the commands that we must clean up.  */
1315   cleanups = make_cleanup_decref_counted_command_line (&info.cmd);
1316
1317   if (arg == NULL || !*arg)
1318     {
1319       if (breakpoint_count - prev_breakpoint_count > 1)
1320         arg = xstrprintf ("%d-%d", prev_breakpoint_count + 1, 
1321                           breakpoint_count);
1322       else if (breakpoint_count > 0)
1323         arg = xstrprintf ("%d", breakpoint_count);
1324       else
1325         {
1326           /* So that we don't try to free the incoming non-NULL
1327              argument in the cleanup below.  Mapping breakpoint
1328              numbers will fail in this case.  */
1329           arg = NULL;
1330         }
1331     }
1332   else
1333     /* The command loop has some static state, so we need to preserve
1334        our argument.  */
1335     arg = xstrdup (arg);
1336
1337   if (arg != NULL)
1338     make_cleanup (xfree, arg);
1339
1340   info.arg = arg;
1341
1342   map_breakpoint_numbers (arg, do_map_commands_command, &info);
1343
1344   if (info.cmd == NULL)
1345     error (_("No breakpoints specified."));
1346
1347   do_cleanups (cleanups);
1348 }
1349
1350 static void
1351 commands_command (char *arg, int from_tty)
1352 {
1353   commands_command_1 (arg, from_tty, NULL);
1354 }
1355
1356 /* Like commands_command, but instead of reading the commands from
1357    input stream, takes them from an already parsed command structure.
1358
1359    This is used by cli-script.c to DTRT with breakpoint commands
1360    that are part of if and while bodies.  */
1361 enum command_control_type
1362 commands_from_control_command (char *arg, struct command_line *cmd)
1363 {
1364   commands_command_1 (arg, 0, cmd);
1365   return simple_control;
1366 }
1367
1368 /* Return non-zero if BL->TARGET_INFO contains valid information.  */
1369
1370 static int
1371 bp_location_has_shadow (struct bp_location *bl)
1372 {
1373   if (bl->loc_type != bp_loc_software_breakpoint)
1374     return 0;
1375   if (!bl->inserted)
1376     return 0;
1377   if (bl->target_info.shadow_len == 0)
1378     /* BL isn't valid, or doesn't shadow memory.  */
1379     return 0;
1380   return 1;
1381 }
1382
1383 /* Update BUF, which is LEN bytes read from the target address MEMADDR,
1384    by replacing any memory breakpoints with their shadowed contents.
1385
1386    If READBUF is not NULL, this buffer must not overlap with any of
1387    the breakpoint location's shadow_contents buffers.  Otherwise,
1388    a failed assertion internal error will be raised.
1389
1390    The range of shadowed area by each bp_location is:
1391      bl->address - bp_location_placed_address_before_address_max
1392      up to bl->address + bp_location_shadow_len_after_address_max
1393    The range we were requested to resolve shadows for is:
1394      memaddr ... memaddr + len
1395    Thus the safe cutoff boundaries for performance optimization are
1396      memaddr + len <= (bl->address
1397                        - bp_location_placed_address_before_address_max)
1398    and:
1399      bl->address + bp_location_shadow_len_after_address_max <= memaddr  */
1400
1401 void
1402 breakpoint_xfer_memory (gdb_byte *readbuf, gdb_byte *writebuf,
1403                         const gdb_byte *writebuf_org,
1404                         ULONGEST memaddr, LONGEST len)
1405 {
1406   /* Left boundary, right boundary and median element of our binary
1407      search.  */
1408   unsigned bc_l, bc_r, bc;
1409
1410   /* Find BC_L which is a leftmost element which may affect BUF
1411      content.  It is safe to report lower value but a failure to
1412      report higher one.  */
1413
1414   bc_l = 0;
1415   bc_r = bp_location_count;
1416   while (bc_l + 1 < bc_r)
1417     {
1418       struct bp_location *bl;
1419
1420       bc = (bc_l + bc_r) / 2;
1421       bl = bp_location[bc];
1422
1423       /* Check first BL->ADDRESS will not overflow due to the added
1424          constant.  Then advance the left boundary only if we are sure
1425          the BC element can in no way affect the BUF content (MEMADDR
1426          to MEMADDR + LEN range).
1427
1428          Use the BP_LOCATION_SHADOW_LEN_AFTER_ADDRESS_MAX safety
1429          offset so that we cannot miss a breakpoint with its shadow
1430          range tail still reaching MEMADDR.  */
1431
1432       if ((bl->address + bp_location_shadow_len_after_address_max
1433            >= bl->address)
1434           && (bl->address + bp_location_shadow_len_after_address_max
1435               <= memaddr))
1436         bc_l = bc;
1437       else
1438         bc_r = bc;
1439     }
1440
1441   /* Due to the binary search above, we need to make sure we pick the
1442      first location that's at BC_L's address.  E.g., if there are
1443      multiple locations at the same address, BC_L may end up pointing
1444      at a duplicate location, and miss the "master"/"inserted"
1445      location.  Say, given locations L1, L2 and L3 at addresses A and
1446      B:
1447
1448       L1@A, L2@A, L3@B, ...
1449
1450      BC_L could end up pointing at location L2, while the "master"
1451      location could be L1.  Since the `loc->inserted' flag is only set
1452      on "master" locations, we'd forget to restore the shadow of L1
1453      and L2.  */
1454   while (bc_l > 0
1455          && bp_location[bc_l]->address == bp_location[bc_l - 1]->address)
1456     bc_l--;
1457
1458   /* Now do full processing of the found relevant range of elements.  */
1459
1460   for (bc = bc_l; bc < bp_location_count; bc++)
1461   {
1462     struct bp_location *bl = bp_location[bc];
1463     CORE_ADDR bp_addr = 0;
1464     int bp_size = 0;
1465     int bptoffset = 0;
1466
1467     /* bp_location array has BL->OWNER always non-NULL.  */
1468     if (bl->owner->type == bp_none)
1469       warning (_("reading through apparently deleted breakpoint #%d?"),
1470                bl->owner->number);
1471
1472     /* Performance optimization: any further element can no longer affect BUF
1473        content.  */
1474
1475     if (bl->address >= bp_location_placed_address_before_address_max
1476         && memaddr + len <= (bl->address
1477                              - bp_location_placed_address_before_address_max))
1478       break;
1479
1480     if (!bp_location_has_shadow (bl))
1481       continue;
1482     if (!breakpoint_address_match (bl->target_info.placed_address_space, 0,
1483                                    current_program_space->aspace, 0))
1484       continue;
1485
1486     /* Addresses and length of the part of the breakpoint that
1487        we need to copy.  */
1488     bp_addr = bl->target_info.placed_address;
1489     bp_size = bl->target_info.shadow_len;
1490
1491     if (bp_addr + bp_size <= memaddr)
1492       /* The breakpoint is entirely before the chunk of memory we
1493          are reading.  */
1494       continue;
1495
1496     if (bp_addr >= memaddr + len)
1497       /* The breakpoint is entirely after the chunk of memory we are
1498          reading.  */
1499       continue;
1500
1501     /* Offset within shadow_contents.  */
1502     if (bp_addr < memaddr)
1503       {
1504         /* Only copy the second part of the breakpoint.  */
1505         bp_size -= memaddr - bp_addr;
1506         bptoffset = memaddr - bp_addr;
1507         bp_addr = memaddr;
1508       }
1509
1510     if (bp_addr + bp_size > memaddr + len)
1511       {
1512         /* Only copy the first part of the breakpoint.  */
1513         bp_size -= (bp_addr + bp_size) - (memaddr + len);
1514       }
1515
1516     if (readbuf != NULL)
1517       {
1518         /* Verify that the readbuf buffer does not overlap with
1519            the shadow_contents buffer.  */
1520         gdb_assert (bl->target_info.shadow_contents >= readbuf + len
1521                     || readbuf >= (bl->target_info.shadow_contents
1522                                    + bl->target_info.shadow_len));
1523
1524         /* Update the read buffer with this inserted breakpoint's
1525            shadow.  */
1526         memcpy (readbuf + bp_addr - memaddr,
1527                 bl->target_info.shadow_contents + bptoffset, bp_size);
1528       }
1529     else
1530       {
1531         struct gdbarch *gdbarch = bl->gdbarch;
1532         const unsigned char *bp;
1533         CORE_ADDR placed_address = bl->target_info.placed_address;
1534         unsigned placed_size = bl->target_info.placed_size;
1535
1536         /* Update the shadow with what we want to write to memory.  */
1537         memcpy (bl->target_info.shadow_contents + bptoffset,
1538                 writebuf_org + bp_addr - memaddr, bp_size);
1539
1540         /* Determine appropriate breakpoint contents and size for this
1541            address.  */
1542         bp = gdbarch_breakpoint_from_pc (gdbarch, &placed_address, &placed_size);
1543
1544         /* Update the final write buffer with this inserted
1545            breakpoint's INSN.  */
1546         memcpy (writebuf + bp_addr - memaddr, bp + bptoffset, bp_size);
1547       }
1548   }
1549 }
1550 \f
1551
1552 /* Return true if BPT is either a software breakpoint or a hardware
1553    breakpoint.  */
1554
1555 int
1556 is_breakpoint (const struct breakpoint *bpt)
1557 {
1558   return (bpt->type == bp_breakpoint
1559           || bpt->type == bp_hardware_breakpoint
1560           || bpt->type == bp_dprintf);
1561 }
1562
1563 /* Return true if BPT is of any hardware watchpoint kind.  */
1564
1565 static int
1566 is_hardware_watchpoint (const struct breakpoint *bpt)
1567 {
1568   return (bpt->type == bp_hardware_watchpoint
1569           || bpt->type == bp_read_watchpoint
1570           || bpt->type == bp_access_watchpoint);
1571 }
1572
1573 /* Return true if BPT is of any watchpoint kind, hardware or
1574    software.  */
1575
1576 int
1577 is_watchpoint (const struct breakpoint *bpt)
1578 {
1579   return (is_hardware_watchpoint (bpt)
1580           || bpt->type == bp_watchpoint);
1581 }
1582
1583 /* Returns true if the current thread and its running state are safe
1584    to evaluate or update watchpoint B.  Watchpoints on local
1585    expressions need to be evaluated in the context of the thread that
1586    was current when the watchpoint was created, and, that thread needs
1587    to be stopped to be able to select the correct frame context.
1588    Watchpoints on global expressions can be evaluated on any thread,
1589    and in any state.  It is presently left to the target allowing
1590    memory accesses when threads are running.  */
1591
1592 static int
1593 watchpoint_in_thread_scope (struct watchpoint *b)
1594 {
1595   return (b->base.pspace == current_program_space
1596           && (ptid_equal (b->watchpoint_thread, null_ptid)
1597               || (ptid_equal (inferior_ptid, b->watchpoint_thread)
1598                   && !is_executing (inferior_ptid))));
1599 }
1600
1601 /* Set watchpoint B to disp_del_at_next_stop, even including its possible
1602    associated bp_watchpoint_scope breakpoint.  */
1603
1604 static void
1605 watchpoint_del_at_next_stop (struct watchpoint *w)
1606 {
1607   struct breakpoint *b = &w->base;
1608
1609   if (b->related_breakpoint != b)
1610     {
1611       gdb_assert (b->related_breakpoint->type == bp_watchpoint_scope);
1612       gdb_assert (b->related_breakpoint->related_breakpoint == b);
1613       b->related_breakpoint->disposition = disp_del_at_next_stop;
1614       b->related_breakpoint->related_breakpoint = b->related_breakpoint;
1615       b->related_breakpoint = b;
1616     }
1617   b->disposition = disp_del_at_next_stop;
1618 }
1619
1620 /* Assuming that B is a watchpoint:
1621    - Reparse watchpoint expression, if REPARSE is non-zero
1622    - Evaluate expression and store the result in B->val
1623    - Evaluate the condition if there is one, and store the result
1624      in b->loc->cond.
1625    - Update the list of values that must be watched in B->loc.
1626
1627    If the watchpoint disposition is disp_del_at_next_stop, then do
1628    nothing.  If this is local watchpoint that is out of scope, delete
1629    it.
1630
1631    Even with `set breakpoint always-inserted on' the watchpoints are
1632    removed + inserted on each stop here.  Normal breakpoints must
1633    never be removed because they might be missed by a running thread
1634    when debugging in non-stop mode.  On the other hand, hardware
1635    watchpoints (is_hardware_watchpoint; processed here) are specific
1636    to each LWP since they are stored in each LWP's hardware debug
1637    registers.  Therefore, such LWP must be stopped first in order to
1638    be able to modify its hardware watchpoints.
1639
1640    Hardware watchpoints must be reset exactly once after being
1641    presented to the user.  It cannot be done sooner, because it would
1642    reset the data used to present the watchpoint hit to the user.  And
1643    it must not be done later because it could display the same single
1644    watchpoint hit during multiple GDB stops.  Note that the latter is
1645    relevant only to the hardware watchpoint types bp_read_watchpoint
1646    and bp_access_watchpoint.  False hit by bp_hardware_watchpoint is
1647    not user-visible - its hit is suppressed if the memory content has
1648    not changed.
1649
1650    The following constraints influence the location where we can reset
1651    hardware watchpoints:
1652
1653    * target_stopped_by_watchpoint and target_stopped_data_address are
1654      called several times when GDB stops.
1655
1656    [linux] 
1657    * Multiple hardware watchpoints can be hit at the same time,
1658      causing GDB to stop.  GDB only presents one hardware watchpoint
1659      hit at a time as the reason for stopping, and all the other hits
1660      are presented later, one after the other, each time the user
1661      requests the execution to be resumed.  Execution is not resumed
1662      for the threads still having pending hit event stored in
1663      LWP_INFO->STATUS.  While the watchpoint is already removed from
1664      the inferior on the first stop the thread hit event is kept being
1665      reported from its cached value by linux_nat_stopped_data_address
1666      until the real thread resume happens after the watchpoint gets
1667      presented and thus its LWP_INFO->STATUS gets reset.
1668
1669    Therefore the hardware watchpoint hit can get safely reset on the
1670    watchpoint removal from inferior.  */
1671
1672 static void
1673 update_watchpoint (struct watchpoint *b, int reparse)
1674 {
1675   int within_current_scope;
1676   struct frame_id saved_frame_id;
1677   int frame_saved;
1678
1679   /* If this is a local watchpoint, we only want to check if the
1680      watchpoint frame is in scope if the current thread is the thread
1681      that was used to create the watchpoint.  */
1682   if (!watchpoint_in_thread_scope (b))
1683     return;
1684
1685   if (b->base.disposition == disp_del_at_next_stop)
1686     return;
1687  
1688   frame_saved = 0;
1689
1690   /* Determine if the watchpoint is within scope.  */
1691   if (b->exp_valid_block == NULL)
1692     within_current_scope = 1;
1693   else
1694     {
1695       struct frame_info *fi = get_current_frame ();
1696       struct gdbarch *frame_arch = get_frame_arch (fi);
1697       CORE_ADDR frame_pc = get_frame_pc (fi);
1698
1699       /* If we're in a function epilogue, unwinding may not work
1700          properly, so do not attempt to recreate locations at this
1701          point.  See similar comments in watchpoint_check.  */
1702       if (gdbarch_in_function_epilogue_p (frame_arch, frame_pc))
1703         return;
1704
1705       /* Save the current frame's ID so we can restore it after
1706          evaluating the watchpoint expression on its own frame.  */
1707       /* FIXME drow/2003-09-09: It would be nice if evaluate_expression
1708          took a frame parameter, so that we didn't have to change the
1709          selected frame.  */
1710       frame_saved = 1;
1711       saved_frame_id = get_frame_id (get_selected_frame (NULL));
1712
1713       fi = frame_find_by_id (b->watchpoint_frame);
1714       within_current_scope = (fi != NULL);
1715       if (within_current_scope)
1716         select_frame (fi);
1717     }
1718
1719   /* We don't free locations.  They are stored in the bp_location array
1720      and update_global_location_list will eventually delete them and
1721      remove breakpoints if needed.  */
1722   b->base.loc = NULL;
1723
1724   if (within_current_scope && reparse)
1725     {
1726       char *s;
1727
1728       if (b->exp)
1729         {
1730           xfree (b->exp);
1731           b->exp = NULL;
1732         }
1733       s = b->exp_string_reparse ? b->exp_string_reparse : b->exp_string;
1734       b->exp = parse_exp_1 (&s, b->exp_valid_block, 0);
1735       /* If the meaning of expression itself changed, the old value is
1736          no longer relevant.  We don't want to report a watchpoint hit
1737          to the user when the old value and the new value may actually
1738          be completely different objects.  */
1739       value_free (b->val);
1740       b->val = NULL;
1741       b->val_valid = 0;
1742
1743       /* Note that unlike with breakpoints, the watchpoint's condition
1744          expression is stored in the breakpoint object, not in the
1745          locations (re)created below.  */
1746       if (b->base.cond_string != NULL)
1747         {
1748           if (b->cond_exp != NULL)
1749             {
1750               xfree (b->cond_exp);
1751               b->cond_exp = NULL;
1752             }
1753
1754           s = b->base.cond_string;
1755           b->cond_exp = parse_exp_1 (&s, b->cond_exp_valid_block, 0);
1756         }
1757     }
1758
1759   /* If we failed to parse the expression, for example because
1760      it refers to a global variable in a not-yet-loaded shared library,
1761      don't try to insert watchpoint.  We don't automatically delete
1762      such watchpoint, though, since failure to parse expression
1763      is different from out-of-scope watchpoint.  */
1764   if ( !target_has_execution)
1765     {
1766       /* Without execution, memory can't change.  No use to try and
1767          set watchpoint locations.  The watchpoint will be reset when
1768          the target gains execution, through breakpoint_re_set.  */
1769     }
1770   else if (within_current_scope && b->exp)
1771     {
1772       int pc = 0;
1773       struct value *val_chain, *v, *result, *next;
1774       struct program_space *frame_pspace;
1775
1776       fetch_subexp_value (b->exp, &pc, &v, &result, &val_chain);
1777
1778       /* Avoid setting b->val if it's already set.  The meaning of
1779          b->val is 'the last value' user saw, and we should update
1780          it only if we reported that last value to user.  As it
1781          happens, the code that reports it updates b->val directly.
1782          We don't keep track of the memory value for masked
1783          watchpoints.  */
1784       if (!b->val_valid && !is_masked_watchpoint (&b->base))
1785         {
1786           b->val = v;
1787           b->val_valid = 1;
1788         }
1789
1790       frame_pspace = get_frame_program_space (get_selected_frame (NULL));
1791
1792       /* Look at each value on the value chain.  */
1793       for (v = val_chain; v; v = value_next (v))
1794         {
1795           /* If it's a memory location, and GDB actually needed
1796              its contents to evaluate the expression, then we
1797              must watch it.  If the first value returned is
1798              still lazy, that means an error occurred reading it;
1799              watch it anyway in case it becomes readable.  */
1800           if (VALUE_LVAL (v) == lval_memory
1801               && (v == val_chain || ! value_lazy (v)))
1802             {
1803               struct type *vtype = check_typedef (value_type (v));
1804
1805               /* We only watch structs and arrays if user asked
1806                  for it explicitly, never if they just happen to
1807                  appear in the middle of some value chain.  */
1808               if (v == result
1809                   || (TYPE_CODE (vtype) != TYPE_CODE_STRUCT
1810                       && TYPE_CODE (vtype) != TYPE_CODE_ARRAY))
1811                 {
1812                   CORE_ADDR addr;
1813                   int len, type;
1814                   struct bp_location *loc, **tmp;
1815
1816                   addr = value_address (v);
1817                   len = TYPE_LENGTH (value_type (v));
1818                   type = hw_write;
1819                   if (b->base.type == bp_read_watchpoint)
1820                     type = hw_read;
1821                   else if (b->base.type == bp_access_watchpoint)
1822                     type = hw_access;
1823
1824                   loc = allocate_bp_location (&b->base);
1825                   for (tmp = &(b->base.loc); *tmp != NULL; tmp = &((*tmp)->next))
1826                     ;
1827                   *tmp = loc;
1828                   loc->gdbarch = get_type_arch (value_type (v));
1829
1830                   loc->pspace = frame_pspace;
1831                   loc->address = addr;
1832                   loc->length = len;
1833                   loc->watchpoint_type = type;
1834                 }
1835             }
1836         }
1837
1838       /* Change the type of breakpoint between hardware assisted or
1839          an ordinary watchpoint depending on the hardware support
1840          and free hardware slots.  REPARSE is set when the inferior
1841          is started.  */
1842       if (reparse)
1843         {
1844           int reg_cnt;
1845           enum bp_loc_type loc_type;
1846           struct bp_location *bl;
1847
1848           reg_cnt = can_use_hardware_watchpoint (val_chain);
1849
1850           if (reg_cnt)
1851             {
1852               int i, target_resources_ok, other_type_used;
1853               enum bptype type;
1854
1855               /* Use an exact watchpoint when there's only one memory region to be
1856                  watched, and only one debug register is needed to watch it.  */
1857               b->exact = target_exact_watchpoints && reg_cnt == 1;
1858
1859               /* We need to determine how many resources are already
1860                  used for all other hardware watchpoints plus this one
1861                  to see if we still have enough resources to also fit
1862                  this watchpoint in as well.  */
1863
1864               /* If this is a software watchpoint, we try to turn it
1865                  to a hardware one -- count resources as if B was of
1866                  hardware watchpoint type.  */
1867               type = b->base.type;
1868               if (type == bp_watchpoint)
1869                 type = bp_hardware_watchpoint;
1870
1871               /* This watchpoint may or may not have been placed on
1872                  the list yet at this point (it won't be in the list
1873                  if we're trying to create it for the first time,
1874                  through watch_command), so always account for it
1875                  manually.  */
1876
1877               /* Count resources used by all watchpoints except B.  */
1878               i = hw_watchpoint_used_count_others (&b->base, type, &other_type_used);
1879
1880               /* Add in the resources needed for B.  */
1881               i += hw_watchpoint_use_count (&b->base);
1882
1883               target_resources_ok
1884                 = target_can_use_hardware_watchpoint (type, i, other_type_used);
1885               if (target_resources_ok <= 0)
1886                 {
1887                   int sw_mode = b->base.ops->works_in_software_mode (&b->base);
1888
1889                   if (target_resources_ok == 0 && !sw_mode)
1890                     error (_("Target does not support this type of "
1891                              "hardware watchpoint."));
1892                   else if (target_resources_ok < 0 && !sw_mode)
1893                     error (_("There are not enough available hardware "
1894                              "resources for this watchpoint."));
1895
1896                   /* Downgrade to software watchpoint.  */
1897                   b->base.type = bp_watchpoint;
1898                 }
1899               else
1900                 {
1901                   /* If this was a software watchpoint, we've just
1902                      found we have enough resources to turn it to a
1903                      hardware watchpoint.  Otherwise, this is a
1904                      nop.  */
1905                   b->base.type = type;
1906                 }
1907             }
1908           else if (!b->base.ops->works_in_software_mode (&b->base))
1909             error (_("Expression cannot be implemented with "
1910                      "read/access watchpoint."));
1911           else
1912             b->base.type = bp_watchpoint;
1913
1914           loc_type = (b->base.type == bp_watchpoint? bp_loc_other
1915                       : bp_loc_hardware_watchpoint);
1916           for (bl = b->base.loc; bl; bl = bl->next)
1917             bl->loc_type = loc_type;
1918         }
1919
1920       for (v = val_chain; v; v = next)
1921         {
1922           next = value_next (v);
1923           if (v != b->val)
1924             value_free (v);
1925         }
1926
1927       /* If a software watchpoint is not watching any memory, then the
1928          above left it without any location set up.  But,
1929          bpstat_stop_status requires a location to be able to report
1930          stops, so make sure there's at least a dummy one.  */
1931       if (b->base.type == bp_watchpoint && b->base.loc == NULL)
1932         {
1933           struct breakpoint *base = &b->base;
1934           base->loc = allocate_bp_location (base);
1935           base->loc->pspace = frame_pspace;
1936           base->loc->address = -1;
1937           base->loc->length = -1;
1938           base->loc->watchpoint_type = -1;
1939         }
1940     }
1941   else if (!within_current_scope)
1942     {
1943       printf_filtered (_("\
1944 Watchpoint %d deleted because the program has left the block\n\
1945 in which its expression is valid.\n"),
1946                        b->base.number);
1947       watchpoint_del_at_next_stop (b);
1948     }
1949
1950   /* Restore the selected frame.  */
1951   if (frame_saved)
1952     select_frame (frame_find_by_id (saved_frame_id));
1953 }
1954
1955
1956 /* Returns 1 iff breakpoint location should be
1957    inserted in the inferior.  We don't differentiate the type of BL's owner
1958    (breakpoint vs. tracepoint), although insert_location in tracepoint's
1959    breakpoint_ops is not defined, because in insert_bp_location,
1960    tracepoint's insert_location will not be called.  */
1961 static int
1962 should_be_inserted (struct bp_location *bl)
1963 {
1964   if (bl->owner == NULL || !breakpoint_enabled (bl->owner))
1965     return 0;
1966
1967   if (bl->owner->disposition == disp_del_at_next_stop)
1968     return 0;
1969
1970   if (!bl->enabled || bl->shlib_disabled || bl->duplicate)
1971     return 0;
1972
1973   if (user_breakpoint_p (bl->owner) && bl->pspace->executing_startup)
1974     return 0;
1975
1976   /* This is set for example, when we're attached to the parent of a
1977      vfork, and have detached from the child.  The child is running
1978      free, and we expect it to do an exec or exit, at which point the
1979      OS makes the parent schedulable again (and the target reports
1980      that the vfork is done).  Until the child is done with the shared
1981      memory region, do not insert breakpoints in the parent, otherwise
1982      the child could still trip on the parent's breakpoints.  Since
1983      the parent is blocked anyway, it won't miss any breakpoint.  */
1984   if (bl->pspace->breakpoints_not_allowed)
1985     return 0;
1986
1987   return 1;
1988 }
1989
1990 /* Same as should_be_inserted but does the check assuming
1991    that the location is not duplicated.  */
1992
1993 static int
1994 unduplicated_should_be_inserted (struct bp_location *bl)
1995 {
1996   int result;
1997   const int save_duplicate = bl->duplicate;
1998
1999   bl->duplicate = 0;
2000   result = should_be_inserted (bl);
2001   bl->duplicate = save_duplicate;
2002   return result;
2003 }
2004
2005 /* Parses a conditional described by an expression COND into an
2006    agent expression bytecode suitable for evaluation
2007    by the bytecode interpreter.  Return NULL if there was
2008    any error during parsing.  */
2009
2010 static struct agent_expr *
2011 parse_cond_to_aexpr (CORE_ADDR scope, struct expression *cond)
2012 {
2013   struct agent_expr *aexpr = NULL;
2014   struct cleanup *old_chain = NULL;
2015   volatile struct gdb_exception ex;
2016
2017   if (!cond)
2018     return NULL;
2019
2020   /* We don't want to stop processing, so catch any errors
2021      that may show up.  */
2022   TRY_CATCH (ex, RETURN_MASK_ERROR)
2023     {
2024       aexpr = gen_eval_for_expr (scope, cond);
2025     }
2026
2027   if (ex.reason < 0)
2028     {
2029       /* If we got here, it means the condition could not be parsed to a valid
2030          bytecode expression and thus can't be evaluated on the target's side.
2031          It's no use iterating through the conditions.  */
2032       return NULL;
2033     }
2034
2035   /* We have a valid agent expression.  */
2036   return aexpr;
2037 }
2038
2039 /* Based on location BL, create a list of breakpoint conditions to be
2040    passed on to the target.  If we have duplicated locations with different
2041    conditions, we will add such conditions to the list.  The idea is that the
2042    target will evaluate the list of conditions and will only notify GDB when
2043    one of them is true.  */
2044
2045 static void
2046 build_target_condition_list (struct bp_location *bl)
2047 {
2048   struct bp_location **locp = NULL, **loc2p;
2049   int null_condition_or_parse_error = 0;
2050   int modified = bl->needs_update;
2051   struct bp_location *loc;
2052
2053   /* This is only meaningful if the target is
2054      evaluating conditions and if the user has
2055      opted for condition evaluation on the target's
2056      side.  */
2057   if (gdb_evaluates_breakpoint_condition_p ()
2058       || !target_supports_evaluation_of_breakpoint_conditions ())
2059     return;
2060
2061   /* Do a first pass to check for locations with no assigned
2062      conditions or conditions that fail to parse to a valid agent expression
2063      bytecode.  If any of these happen, then it's no use to send conditions
2064      to the target since this location will always trigger and generate a
2065      response back to GDB.  */
2066   ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2067     {
2068       loc = (*loc2p);
2069       if (is_breakpoint (loc->owner) && loc->pspace->num == bl->pspace->num)
2070         {
2071           if (modified)
2072             {
2073               struct agent_expr *aexpr;
2074
2075               /* Re-parse the conditions since something changed.  In that
2076                  case we already freed the condition bytecodes (see
2077                  force_breakpoint_reinsertion).  We just
2078                  need to parse the condition to bytecodes again.  */
2079               aexpr = parse_cond_to_aexpr (bl->address, loc->cond);
2080               loc->cond_bytecode = aexpr;
2081
2082               /* Check if we managed to parse the conditional expression
2083                  correctly.  If not, we will not send this condition
2084                  to the target.  */
2085               if (aexpr)
2086                 continue;
2087             }
2088
2089           /* If we have a NULL bytecode expression, it means something
2090              went wrong or we have a null condition expression.  */
2091           if (!loc->cond_bytecode)
2092             {
2093               null_condition_or_parse_error = 1;
2094               break;
2095             }
2096         }
2097     }
2098
2099   /* If any of these happened, it means we will have to evaluate the conditions
2100      for the location's address on gdb's side.  It is no use keeping bytecodes
2101      for all the other duplicate locations, thus we free all of them here.
2102
2103      This is so we have a finer control over which locations' conditions are
2104      being evaluated by GDB or the remote stub.  */
2105   if (null_condition_or_parse_error)
2106     {
2107       ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2108         {
2109           loc = (*loc2p);
2110           if (is_breakpoint (loc->owner) && loc->pspace->num == bl->pspace->num)
2111             {
2112               /* Only go as far as the first NULL bytecode is
2113                  located.  */
2114               if (!loc->cond_bytecode)
2115                 return;
2116
2117               free_agent_expr (loc->cond_bytecode);
2118               loc->cond_bytecode = NULL;
2119             }
2120         }
2121     }
2122
2123   /* No NULL conditions or failed bytecode generation.  Build a condition list
2124      for this location's address.  */
2125   ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2126     {
2127       loc = (*loc2p);
2128       if (loc->cond
2129           && is_breakpoint (loc->owner)
2130           && loc->pspace->num == bl->pspace->num
2131           && loc->owner->enable_state == bp_enabled
2132           && loc->enabled)
2133         /* Add the condition to the vector.  This will be used later to send the
2134            conditions to the target.  */
2135         VEC_safe_push (agent_expr_p, bl->target_info.conditions,
2136                        loc->cond_bytecode);
2137     }
2138
2139   return;
2140 }
2141
2142 /* Insert a low-level "breakpoint" of some type.  BL is the breakpoint
2143    location.  Any error messages are printed to TMP_ERROR_STREAM; and
2144    DISABLED_BREAKS, and HW_BREAKPOINT_ERROR are used to report problems.
2145    Returns 0 for success, 1 if the bp_location type is not supported or
2146    -1 for failure.
2147
2148    NOTE drow/2003-09-09: This routine could be broken down to an
2149    object-style method for each breakpoint or catchpoint type.  */
2150 static int
2151 insert_bp_location (struct bp_location *bl,
2152                     struct ui_file *tmp_error_stream,
2153                     int *disabled_breaks,
2154                     int *hw_breakpoint_error)
2155 {
2156   int val = 0;
2157
2158   if (!should_be_inserted (bl) || (bl->inserted && !bl->needs_update))
2159     return 0;
2160
2161   /* Note we don't initialize bl->target_info, as that wipes out
2162      the breakpoint location's shadow_contents if the breakpoint
2163      is still inserted at that location.  This in turn breaks
2164      target_read_memory which depends on these buffers when
2165      a memory read is requested at the breakpoint location:
2166      Once the target_info has been wiped, we fail to see that
2167      we have a breakpoint inserted at that address and thus
2168      read the breakpoint instead of returning the data saved in
2169      the breakpoint location's shadow contents.  */
2170   bl->target_info.placed_address = bl->address;
2171   bl->target_info.placed_address_space = bl->pspace->aspace;
2172   bl->target_info.length = bl->length;
2173
2174   /* When working with target-side conditions, we must pass all the conditions
2175      for the same breakpoint address down to the target since GDB will not
2176      insert those locations.  With a list of breakpoint conditions, the target
2177      can decide when to stop and notify GDB.  */
2178
2179   if (is_breakpoint (bl->owner))
2180     {
2181       build_target_condition_list (bl);
2182       /* Reset the condition modification marker.  */
2183       bl->needs_update = 0;
2184     }
2185
2186   if (bl->loc_type == bp_loc_software_breakpoint
2187       || bl->loc_type == bp_loc_hardware_breakpoint)
2188     {
2189       if (bl->owner->type != bp_hardware_breakpoint)
2190         {
2191           /* If the explicitly specified breakpoint type
2192              is not hardware breakpoint, check the memory map to see
2193              if the breakpoint address is in read only memory or not.
2194
2195              Two important cases are:
2196              - location type is not hardware breakpoint, memory
2197              is readonly.  We change the type of the location to
2198              hardware breakpoint.
2199              - location type is hardware breakpoint, memory is
2200              read-write.  This means we've previously made the
2201              location hardware one, but then the memory map changed,
2202              so we undo.
2203              
2204              When breakpoints are removed, remove_breakpoints will use
2205              location types we've just set here, the only possible
2206              problem is that memory map has changed during running
2207              program, but it's not going to work anyway with current
2208              gdb.  */
2209           struct mem_region *mr 
2210             = lookup_mem_region (bl->target_info.placed_address);
2211           
2212           if (mr)
2213             {
2214               if (automatic_hardware_breakpoints)
2215                 {
2216                   enum bp_loc_type new_type;
2217                   
2218                   if (mr->attrib.mode != MEM_RW)
2219                     new_type = bp_loc_hardware_breakpoint;
2220                   else 
2221                     new_type = bp_loc_software_breakpoint;
2222                   
2223                   if (new_type != bl->loc_type)
2224                     {
2225                       static int said = 0;
2226
2227                       bl->loc_type = new_type;
2228                       if (!said)
2229                         {
2230                           fprintf_filtered (gdb_stdout,
2231                                             _("Note: automatically using "
2232                                               "hardware breakpoints for "
2233                                               "read-only addresses.\n"));
2234                           said = 1;
2235                         }
2236                     }
2237                 }
2238               else if (bl->loc_type == bp_loc_software_breakpoint
2239                        && mr->attrib.mode != MEM_RW)        
2240                 warning (_("cannot set software breakpoint "
2241                            "at readonly address %s"),
2242                          paddress (bl->gdbarch, bl->address));
2243             }
2244         }
2245         
2246       /* First check to see if we have to handle an overlay.  */
2247       if (overlay_debugging == ovly_off
2248           || bl->section == NULL
2249           || !(section_is_overlay (bl->section)))
2250         {
2251           /* No overlay handling: just set the breakpoint.  */
2252
2253           val = bl->owner->ops->insert_location (bl);
2254         }
2255       else
2256         {
2257           /* This breakpoint is in an overlay section.
2258              Shall we set a breakpoint at the LMA?  */
2259           if (!overlay_events_enabled)
2260             {
2261               /* Yes -- overlay event support is not active, 
2262                  so we must try to set a breakpoint at the LMA.
2263                  This will not work for a hardware breakpoint.  */
2264               if (bl->loc_type == bp_loc_hardware_breakpoint)
2265                 warning (_("hardware breakpoint %d not supported in overlay!"),
2266                          bl->owner->number);
2267               else
2268                 {
2269                   CORE_ADDR addr = overlay_unmapped_address (bl->address,
2270                                                              bl->section);
2271                   /* Set a software (trap) breakpoint at the LMA.  */
2272                   bl->overlay_target_info = bl->target_info;
2273                   bl->overlay_target_info.placed_address = addr;
2274                   val = target_insert_breakpoint (bl->gdbarch,
2275                                                   &bl->overlay_target_info);
2276                   if (val != 0)
2277                     fprintf_unfiltered (tmp_error_stream,
2278                                         "Overlay breakpoint %d "
2279                                         "failed: in ROM?\n",
2280                                         bl->owner->number);
2281                 }
2282             }
2283           /* Shall we set a breakpoint at the VMA? */
2284           if (section_is_mapped (bl->section))
2285             {
2286               /* Yes.  This overlay section is mapped into memory.  */
2287               val = bl->owner->ops->insert_location (bl);
2288             }
2289           else
2290             {
2291               /* No.  This breakpoint will not be inserted.  
2292                  No error, but do not mark the bp as 'inserted'.  */
2293               return 0;
2294             }
2295         }
2296
2297       if (val)
2298         {
2299           /* Can't set the breakpoint.  */
2300           if (solib_name_from_address (bl->pspace, bl->address))
2301             {
2302               /* See also: disable_breakpoints_in_shlibs.  */
2303               val = 0;
2304               bl->shlib_disabled = 1;
2305               observer_notify_breakpoint_modified (bl->owner);
2306               if (!*disabled_breaks)
2307                 {
2308                   fprintf_unfiltered (tmp_error_stream, 
2309                                       "Cannot insert breakpoint %d.\n", 
2310                                       bl->owner->number);
2311                   fprintf_unfiltered (tmp_error_stream, 
2312                                       "Temporarily disabling shared "
2313                                       "library breakpoints:\n");
2314                 }
2315               *disabled_breaks = 1;
2316               fprintf_unfiltered (tmp_error_stream,
2317                                   "breakpoint #%d\n", bl->owner->number);
2318             }
2319           else
2320             {
2321               if (bl->loc_type == bp_loc_hardware_breakpoint)
2322                 {
2323                   *hw_breakpoint_error = 1;
2324                   fprintf_unfiltered (tmp_error_stream,
2325                                       "Cannot insert hardware "
2326                                       "breakpoint %d.\n",
2327                                       bl->owner->number);
2328                 }
2329               else
2330                 {
2331                   fprintf_unfiltered (tmp_error_stream, 
2332                                       "Cannot insert breakpoint %d.\n", 
2333                                       bl->owner->number);
2334                   fprintf_filtered (tmp_error_stream, 
2335                                     "Error accessing memory address ");
2336                   fputs_filtered (paddress (bl->gdbarch, bl->address),
2337                                   tmp_error_stream);
2338                   fprintf_filtered (tmp_error_stream, ": %s.\n",
2339                                     safe_strerror (val));
2340                 }
2341
2342             }
2343         }
2344       else
2345         bl->inserted = 1;
2346
2347       return val;
2348     }
2349
2350   else if (bl->loc_type == bp_loc_hardware_watchpoint
2351            /* NOTE drow/2003-09-08: This state only exists for removing
2352               watchpoints.  It's not clear that it's necessary...  */
2353            && bl->owner->disposition != disp_del_at_next_stop)
2354     {
2355       gdb_assert (bl->owner->ops != NULL
2356                   && bl->owner->ops->insert_location != NULL);
2357
2358       val = bl->owner->ops->insert_location (bl);
2359
2360       /* If trying to set a read-watchpoint, and it turns out it's not
2361          supported, try emulating one with an access watchpoint.  */
2362       if (val == 1 && bl->watchpoint_type == hw_read)
2363         {
2364           struct bp_location *loc, **loc_temp;
2365
2366           /* But don't try to insert it, if there's already another
2367              hw_access location that would be considered a duplicate
2368              of this one.  */
2369           ALL_BP_LOCATIONS (loc, loc_temp)
2370             if (loc != bl
2371                 && loc->watchpoint_type == hw_access
2372                 && watchpoint_locations_match (bl, loc))
2373               {
2374                 bl->duplicate = 1;
2375                 bl->inserted = 1;
2376                 bl->target_info = loc->target_info;
2377                 bl->watchpoint_type = hw_access;
2378                 val = 0;
2379                 break;
2380               }
2381
2382           if (val == 1)
2383             {
2384               bl->watchpoint_type = hw_access;
2385               val = bl->owner->ops->insert_location (bl);
2386
2387               if (val)
2388                 /* Back to the original value.  */
2389                 bl->watchpoint_type = hw_read;
2390             }
2391         }
2392
2393       bl->inserted = (val == 0);
2394     }
2395
2396   else if (bl->owner->type == bp_catchpoint)
2397     {
2398       gdb_assert (bl->owner->ops != NULL
2399                   && bl->owner->ops->insert_location != NULL);
2400
2401       val = bl->owner->ops->insert_location (bl);
2402       if (val)
2403         {
2404           bl->owner->enable_state = bp_disabled;
2405
2406           if (val == 1)
2407             warning (_("\
2408 Error inserting catchpoint %d: Your system does not support this type\n\
2409 of catchpoint."), bl->owner->number);
2410           else
2411             warning (_("Error inserting catchpoint %d."), bl->owner->number);
2412         }
2413
2414       bl->inserted = (val == 0);
2415
2416       /* We've already printed an error message if there was a problem
2417          inserting this catchpoint, and we've disabled the catchpoint,
2418          so just return success.  */
2419       return 0;
2420     }
2421
2422   return 0;
2423 }
2424
2425 /* This function is called when program space PSPACE is about to be
2426    deleted.  It takes care of updating breakpoints to not reference
2427    PSPACE anymore.  */
2428
2429 void
2430 breakpoint_program_space_exit (struct program_space *pspace)
2431 {
2432   struct breakpoint *b, *b_temp;
2433   struct bp_location *loc, **loc_temp;
2434
2435   /* Remove any breakpoint that was set through this program space.  */
2436   ALL_BREAKPOINTS_SAFE (b, b_temp)
2437     {
2438       if (b->pspace == pspace)
2439         delete_breakpoint (b);
2440     }
2441
2442   /* Breakpoints set through other program spaces could have locations
2443      bound to PSPACE as well.  Remove those.  */
2444   ALL_BP_LOCATIONS (loc, loc_temp)
2445     {
2446       struct bp_location *tmp;
2447
2448       if (loc->pspace == pspace)
2449         {
2450           /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL.  */
2451           if (loc->owner->loc == loc)
2452             loc->owner->loc = loc->next;
2453           else
2454             for (tmp = loc->owner->loc; tmp->next != NULL; tmp = tmp->next)
2455               if (tmp->next == loc)
2456                 {
2457                   tmp->next = loc->next;
2458                   break;
2459                 }
2460         }
2461     }
2462
2463   /* Now update the global location list to permanently delete the
2464      removed locations above.  */
2465   update_global_location_list (0);
2466 }
2467
2468 /* Make sure all breakpoints are inserted in inferior.
2469    Throws exception on any error.
2470    A breakpoint that is already inserted won't be inserted
2471    again, so calling this function twice is safe.  */
2472 void
2473 insert_breakpoints (void)
2474 {
2475   struct breakpoint *bpt;
2476
2477   ALL_BREAKPOINTS (bpt)
2478     if (is_hardware_watchpoint (bpt))
2479       {
2480         struct watchpoint *w = (struct watchpoint *) bpt;
2481
2482         update_watchpoint (w, 0 /* don't reparse.  */);
2483       }
2484
2485   update_global_location_list (1);
2486
2487   /* update_global_location_list does not insert breakpoints when
2488      always_inserted_mode is not enabled.  Explicitly insert them
2489      now.  */
2490   if (!breakpoints_always_inserted_mode ())
2491     insert_breakpoint_locations ();
2492 }
2493
2494 /* Invoke CALLBACK for each of bp_location.  */
2495
2496 void
2497 iterate_over_bp_locations (walk_bp_location_callback callback)
2498 {
2499   struct bp_location *loc, **loc_tmp;
2500
2501   ALL_BP_LOCATIONS (loc, loc_tmp)
2502     {
2503       callback (loc, NULL);
2504     }
2505 }
2506
2507 /* This is used when we need to synch breakpoint conditions between GDB and the
2508    target.  It is the case with deleting and disabling of breakpoints when using
2509    always-inserted mode.  */
2510
2511 static void
2512 update_inserted_breakpoint_locations (void)
2513 {
2514   struct bp_location *bl, **blp_tmp;
2515   int error_flag = 0;
2516   int val = 0;
2517   int disabled_breaks = 0;
2518   int hw_breakpoint_error = 0;
2519
2520   struct ui_file *tmp_error_stream = mem_fileopen ();
2521   struct cleanup *cleanups = make_cleanup_ui_file_delete (tmp_error_stream);
2522
2523   /* Explicitly mark the warning -- this will only be printed if
2524      there was an error.  */
2525   fprintf_unfiltered (tmp_error_stream, "Warning:\n");
2526
2527   save_current_space_and_thread ();
2528
2529   ALL_BP_LOCATIONS (bl, blp_tmp)
2530     {
2531       /* We only want to update software breakpoints and hardware
2532          breakpoints.  */
2533       if (!is_breakpoint (bl->owner))
2534         continue;
2535
2536       /* We only want to update locations that are already inserted
2537          and need updating.  This is to avoid unwanted insertion during
2538          deletion of breakpoints.  */
2539       if (!bl->inserted || (bl->inserted && !bl->needs_update))
2540         continue;
2541
2542       switch_to_program_space_and_thread (bl->pspace);
2543
2544       /* For targets that support global breakpoints, there's no need
2545          to select an inferior to insert breakpoint to.  In fact, even
2546          if we aren't attached to any process yet, we should still
2547          insert breakpoints.  */
2548       if (!gdbarch_has_global_breakpoints (target_gdbarch)
2549           && ptid_equal (inferior_ptid, null_ptid))
2550         continue;
2551
2552       val = insert_bp_location (bl, tmp_error_stream, &disabled_breaks,
2553                                     &hw_breakpoint_error);
2554       if (val)
2555         error_flag = val;
2556     }
2557
2558   if (error_flag)
2559     {
2560       target_terminal_ours_for_output ();
2561       error_stream (tmp_error_stream);
2562     }
2563
2564   do_cleanups (cleanups);
2565 }
2566
2567 /* Used when starting or continuing the program.  */
2568
2569 static void
2570 insert_breakpoint_locations (void)
2571 {
2572   struct breakpoint *bpt;
2573   struct bp_location *bl, **blp_tmp;
2574   int error_flag = 0;
2575   int val = 0;
2576   int disabled_breaks = 0;
2577   int hw_breakpoint_error = 0;
2578
2579   struct ui_file *tmp_error_stream = mem_fileopen ();
2580   struct cleanup *cleanups = make_cleanup_ui_file_delete (tmp_error_stream);
2581   
2582   /* Explicitly mark the warning -- this will only be printed if
2583      there was an error.  */
2584   fprintf_unfiltered (tmp_error_stream, "Warning:\n");
2585
2586   save_current_space_and_thread ();
2587
2588   ALL_BP_LOCATIONS (bl, blp_tmp)
2589     {
2590       if (!should_be_inserted (bl) || (bl->inserted && !bl->needs_update))
2591         continue;
2592
2593       /* There is no point inserting thread-specific breakpoints if
2594          the thread no longer exists.  ALL_BP_LOCATIONS bp_location
2595          has BL->OWNER always non-NULL.  */
2596       if (bl->owner->thread != -1
2597           && !valid_thread_id (bl->owner->thread))
2598         continue;
2599
2600       switch_to_program_space_and_thread (bl->pspace);
2601
2602       /* For targets that support global breakpoints, there's no need
2603          to select an inferior to insert breakpoint to.  In fact, even
2604          if we aren't attached to any process yet, we should still
2605          insert breakpoints.  */
2606       if (!gdbarch_has_global_breakpoints (target_gdbarch)
2607           && ptid_equal (inferior_ptid, null_ptid))
2608         continue;
2609
2610       val = insert_bp_location (bl, tmp_error_stream, &disabled_breaks,
2611                                     &hw_breakpoint_error);
2612       if (val)
2613         error_flag = val;
2614     }
2615
2616   /* If we failed to insert all locations of a watchpoint, remove
2617      them, as half-inserted watchpoint is of limited use.  */
2618   ALL_BREAKPOINTS (bpt)  
2619     {
2620       int some_failed = 0;
2621       struct bp_location *loc;
2622
2623       if (!is_hardware_watchpoint (bpt))
2624         continue;
2625
2626       if (!breakpoint_enabled (bpt))
2627         continue;
2628
2629       if (bpt->disposition == disp_del_at_next_stop)
2630         continue;
2631       
2632       for (loc = bpt->loc; loc; loc = loc->next)
2633         if (!loc->inserted && should_be_inserted (loc))
2634           {
2635             some_failed = 1;
2636             break;
2637           }
2638       if (some_failed)
2639         {
2640           for (loc = bpt->loc; loc; loc = loc->next)
2641             if (loc->inserted)
2642               remove_breakpoint (loc, mark_uninserted);
2643
2644           hw_breakpoint_error = 1;
2645           fprintf_unfiltered (tmp_error_stream,
2646                               "Could not insert hardware watchpoint %d.\n", 
2647                               bpt->number);
2648           error_flag = -1;
2649         }
2650     }
2651
2652   if (error_flag)
2653     {
2654       /* If a hardware breakpoint or watchpoint was inserted, add a
2655          message about possibly exhausted resources.  */
2656       if (hw_breakpoint_error)
2657         {
2658           fprintf_unfiltered (tmp_error_stream, 
2659                               "Could not insert hardware breakpoints:\n\
2660 You may have requested too many hardware breakpoints/watchpoints.\n");
2661         }
2662       target_terminal_ours_for_output ();
2663       error_stream (tmp_error_stream);
2664     }
2665
2666   do_cleanups (cleanups);
2667 }
2668
2669 /* Used when the program stops.
2670    Returns zero if successful, or non-zero if there was a problem
2671    removing a breakpoint location.  */
2672
2673 int
2674 remove_breakpoints (void)
2675 {
2676   struct bp_location *bl, **blp_tmp;
2677   int val = 0;
2678
2679   ALL_BP_LOCATIONS (bl, blp_tmp)
2680   {
2681     if (bl->inserted && !is_tracepoint (bl->owner))
2682       val |= remove_breakpoint (bl, mark_uninserted);
2683   }
2684   return val;
2685 }
2686
2687 /* Remove breakpoints of process PID.  */
2688
2689 int
2690 remove_breakpoints_pid (int pid)
2691 {
2692   struct bp_location *bl, **blp_tmp;
2693   int val;
2694   struct inferior *inf = find_inferior_pid (pid);
2695
2696   ALL_BP_LOCATIONS (bl, blp_tmp)
2697   {
2698     if (bl->pspace != inf->pspace)
2699       continue;
2700
2701     if (bl->inserted)
2702       {
2703         val = remove_breakpoint (bl, mark_uninserted);
2704         if (val != 0)
2705           return val;
2706       }
2707   }
2708   return 0;
2709 }
2710
2711 int
2712 reattach_breakpoints (int pid)
2713 {
2714   struct cleanup *old_chain;
2715   struct bp_location *bl, **blp_tmp;
2716   int val;
2717   struct ui_file *tmp_error_stream;
2718   int dummy1 = 0, dummy2 = 0;
2719   struct inferior *inf;
2720   struct thread_info *tp;
2721
2722   tp = any_live_thread_of_process (pid);
2723   if (tp == NULL)
2724     return 1;
2725
2726   inf = find_inferior_pid (pid);
2727   old_chain = save_inferior_ptid ();
2728
2729   inferior_ptid = tp->ptid;
2730
2731   tmp_error_stream = mem_fileopen ();
2732   make_cleanup_ui_file_delete (tmp_error_stream);
2733
2734   ALL_BP_LOCATIONS (bl, blp_tmp)
2735   {
2736     if (bl->pspace != inf->pspace)
2737       continue;
2738
2739     if (bl->inserted)
2740       {
2741         bl->inserted = 0;
2742         val = insert_bp_location (bl, tmp_error_stream, &dummy1, &dummy2);
2743         if (val != 0)
2744           {
2745             do_cleanups (old_chain);
2746             return val;
2747           }
2748       }
2749   }
2750   do_cleanups (old_chain);
2751   return 0;
2752 }
2753
2754 static int internal_breakpoint_number = -1;
2755
2756 /* Set the breakpoint number of B, depending on the value of INTERNAL.
2757    If INTERNAL is non-zero, the breakpoint number will be populated
2758    from internal_breakpoint_number and that variable decremented.
2759    Otherwise the breakpoint number will be populated from
2760    breakpoint_count and that value incremented.  Internal breakpoints
2761    do not set the internal var bpnum.  */
2762 static void
2763 set_breakpoint_number (int internal, struct breakpoint *b)
2764 {
2765   if (internal)
2766     b->number = internal_breakpoint_number--;
2767   else
2768     {
2769       set_breakpoint_count (breakpoint_count + 1);
2770       b->number = breakpoint_count;
2771     }
2772 }
2773
2774 static struct breakpoint *
2775 create_internal_breakpoint (struct gdbarch *gdbarch,
2776                             CORE_ADDR address, enum bptype type,
2777                             const struct breakpoint_ops *ops)
2778 {
2779   struct symtab_and_line sal;
2780   struct breakpoint *b;
2781
2782   init_sal (&sal);              /* Initialize to zeroes.  */
2783
2784   sal.pc = address;
2785   sal.section = find_pc_overlay (sal.pc);
2786   sal.pspace = current_program_space;
2787
2788   b = set_raw_breakpoint (gdbarch, sal, type, ops);
2789   b->number = internal_breakpoint_number--;
2790   b->disposition = disp_donttouch;
2791
2792   return b;
2793 }
2794
2795 static const char *const longjmp_names[] =
2796   {
2797     "longjmp", "_longjmp", "siglongjmp", "_siglongjmp"
2798   };
2799 #define NUM_LONGJMP_NAMES ARRAY_SIZE(longjmp_names)
2800
2801 /* Per-objfile data private to breakpoint.c.  */
2802 struct breakpoint_objfile_data
2803 {
2804   /* Minimal symbol for "_ovly_debug_event" (if any).  */
2805   struct minimal_symbol *overlay_msym;
2806
2807   /* Minimal symbol(s) for "longjmp", "siglongjmp", etc. (if any).  */
2808   struct minimal_symbol *longjmp_msym[NUM_LONGJMP_NAMES];
2809
2810   /* True if we have looked for longjmp probes.  */
2811   int longjmp_searched;
2812
2813   /* SystemTap probe points for longjmp (if any).  */
2814   VEC (probe_p) *longjmp_probes;
2815
2816   /* Minimal symbol for "std::terminate()" (if any).  */
2817   struct minimal_symbol *terminate_msym;
2818
2819   /* Minimal symbol for "_Unwind_DebugHook" (if any).  */
2820   struct minimal_symbol *exception_msym;
2821
2822   /* True if we have looked for exception probes.  */
2823   int exception_searched;
2824
2825   /* SystemTap probe points for unwinding (if any).  */
2826   VEC (probe_p) *exception_probes;
2827 };
2828
2829 static const struct objfile_data *breakpoint_objfile_key;
2830
2831 /* Minimal symbol not found sentinel.  */
2832 static struct minimal_symbol msym_not_found;
2833
2834 /* Returns TRUE if MSYM point to the "not found" sentinel.  */
2835
2836 static int
2837 msym_not_found_p (const struct minimal_symbol *msym)
2838 {
2839   return msym == &msym_not_found;
2840 }
2841
2842 /* Return per-objfile data needed by breakpoint.c.
2843    Allocate the data if necessary.  */
2844
2845 static struct breakpoint_objfile_data *
2846 get_breakpoint_objfile_data (struct objfile *objfile)
2847 {
2848   struct breakpoint_objfile_data *bp_objfile_data;
2849
2850   bp_objfile_data = objfile_data (objfile, breakpoint_objfile_key);
2851   if (bp_objfile_data == NULL)
2852     {
2853       bp_objfile_data = obstack_alloc (&objfile->objfile_obstack,
2854                                        sizeof (*bp_objfile_data));
2855
2856       memset (bp_objfile_data, 0, sizeof (*bp_objfile_data));
2857       set_objfile_data (objfile, breakpoint_objfile_key, bp_objfile_data);
2858     }
2859   return bp_objfile_data;
2860 }
2861
2862 static void
2863 free_breakpoint_probes (struct objfile *obj, void *data)
2864 {
2865   struct breakpoint_objfile_data *bp_objfile_data = data;
2866
2867   VEC_free (probe_p, bp_objfile_data->longjmp_probes);
2868   VEC_free (probe_p, bp_objfile_data->exception_probes);
2869 }
2870
2871 static void
2872 create_overlay_event_breakpoint (void)
2873 {
2874   struct objfile *objfile;
2875   const char *const func_name = "_ovly_debug_event";
2876
2877   ALL_OBJFILES (objfile)
2878     {
2879       struct breakpoint *b;
2880       struct breakpoint_objfile_data *bp_objfile_data;
2881       CORE_ADDR addr;
2882
2883       bp_objfile_data = get_breakpoint_objfile_data (objfile);
2884
2885       if (msym_not_found_p (bp_objfile_data->overlay_msym))
2886         continue;
2887
2888       if (bp_objfile_data->overlay_msym == NULL)
2889         {
2890           struct minimal_symbol *m;
2891
2892           m = lookup_minimal_symbol_text (func_name, objfile);
2893           if (m == NULL)
2894             {
2895               /* Avoid future lookups in this objfile.  */
2896               bp_objfile_data->overlay_msym = &msym_not_found;
2897               continue;
2898             }
2899           bp_objfile_data->overlay_msym = m;
2900         }
2901
2902       addr = SYMBOL_VALUE_ADDRESS (bp_objfile_data->overlay_msym);
2903       b = create_internal_breakpoint (get_objfile_arch (objfile), addr,
2904                                       bp_overlay_event,
2905                                       &internal_breakpoint_ops);
2906       b->addr_string = xstrdup (func_name);
2907
2908       if (overlay_debugging == ovly_auto)
2909         {
2910           b->enable_state = bp_enabled;
2911           overlay_events_enabled = 1;
2912         }
2913       else
2914        {
2915          b->enable_state = bp_disabled;
2916          overlay_events_enabled = 0;
2917        }
2918     }
2919   update_global_location_list (1);
2920 }
2921
2922 static void
2923 create_longjmp_master_breakpoint (void)
2924 {
2925   struct program_space *pspace;
2926   struct cleanup *old_chain;
2927
2928   old_chain = save_current_program_space ();
2929
2930   ALL_PSPACES (pspace)
2931   {
2932     struct objfile *objfile;
2933
2934     set_current_program_space (pspace);
2935
2936     ALL_OBJFILES (objfile)
2937     {
2938       int i;
2939       struct gdbarch *gdbarch;
2940       struct breakpoint_objfile_data *bp_objfile_data;
2941
2942       gdbarch = get_objfile_arch (objfile);
2943       if (!gdbarch_get_longjmp_target_p (gdbarch))
2944         continue;
2945
2946       bp_objfile_data = get_breakpoint_objfile_data (objfile);
2947
2948       if (!bp_objfile_data->longjmp_searched)
2949         {
2950           bp_objfile_data->longjmp_probes
2951             = find_probes_in_objfile (objfile, "libc", "longjmp");
2952           bp_objfile_data->longjmp_searched = 1;
2953         }
2954
2955       if (bp_objfile_data->longjmp_probes != NULL)
2956         {
2957           int i;
2958           struct probe *probe;
2959           struct gdbarch *gdbarch = get_objfile_arch (objfile);
2960
2961           for (i = 0;
2962                VEC_iterate (probe_p,
2963                             bp_objfile_data->longjmp_probes,
2964                             i, probe);
2965                ++i)
2966             {
2967               struct breakpoint *b;
2968
2969               b = create_internal_breakpoint (gdbarch, probe->address,
2970                                               bp_longjmp_master,
2971                                               &internal_breakpoint_ops);
2972               b->addr_string = xstrdup ("-probe-stap libc:longjmp");
2973               b->enable_state = bp_disabled;
2974             }
2975
2976           continue;
2977         }
2978
2979       for (i = 0; i < NUM_LONGJMP_NAMES; i++)
2980         {
2981           struct breakpoint *b;
2982           const char *func_name;
2983           CORE_ADDR addr;
2984
2985           if (msym_not_found_p (bp_objfile_data->longjmp_msym[i]))
2986             continue;
2987
2988           func_name = longjmp_names[i];
2989           if (bp_objfile_data->longjmp_msym[i] == NULL)
2990             {
2991               struct minimal_symbol *m;
2992
2993               m = lookup_minimal_symbol_text (func_name, objfile);
2994               if (m == NULL)
2995                 {
2996                   /* Prevent future lookups in this objfile.  */
2997                   bp_objfile_data->longjmp_msym[i] = &msym_not_found;
2998                   continue;
2999                 }
3000               bp_objfile_data->longjmp_msym[i] = m;
3001             }
3002
3003           addr = SYMBOL_VALUE_ADDRESS (bp_objfile_data->longjmp_msym[i]);
3004           b = create_internal_breakpoint (gdbarch, addr, bp_longjmp_master,
3005                                           &internal_breakpoint_ops);
3006           b->addr_string = xstrdup (func_name);
3007           b->enable_state = bp_disabled;
3008         }
3009     }
3010   }
3011   update_global_location_list (1);
3012
3013   do_cleanups (old_chain);
3014 }
3015
3016 /* Create a master std::terminate breakpoint.  */
3017 static void
3018 create_std_terminate_master_breakpoint (void)
3019 {
3020   struct program_space *pspace;
3021   struct cleanup *old_chain;
3022   const char *const func_name = "std::terminate()";
3023
3024   old_chain = save_current_program_space ();
3025
3026   ALL_PSPACES (pspace)
3027   {
3028     struct objfile *objfile;
3029     CORE_ADDR addr;
3030
3031     set_current_program_space (pspace);
3032
3033     ALL_OBJFILES (objfile)
3034     {
3035       struct breakpoint *b;
3036       struct breakpoint_objfile_data *bp_objfile_data;
3037
3038       bp_objfile_data = get_breakpoint_objfile_data (objfile);
3039
3040       if (msym_not_found_p (bp_objfile_data->terminate_msym))
3041         continue;
3042
3043       if (bp_objfile_data->terminate_msym == NULL)
3044         {
3045           struct minimal_symbol *m;
3046
3047           m = lookup_minimal_symbol (func_name, NULL, objfile);
3048           if (m == NULL || (MSYMBOL_TYPE (m) != mst_text
3049                             && MSYMBOL_TYPE (m) != mst_file_text))
3050             {
3051               /* Prevent future lookups in this objfile.  */
3052               bp_objfile_data->terminate_msym = &msym_not_found;
3053               continue;
3054             }
3055           bp_objfile_data->terminate_msym = m;
3056         }
3057
3058       addr = SYMBOL_VALUE_ADDRESS (bp_objfile_data->terminate_msym);
3059       b = create_internal_breakpoint (get_objfile_arch (objfile), addr,
3060                                       bp_std_terminate_master,
3061                                       &internal_breakpoint_ops);
3062       b->addr_string = xstrdup (func_name);
3063       b->enable_state = bp_disabled;
3064     }
3065   }
3066
3067   update_global_location_list (1);
3068
3069   do_cleanups (old_chain);
3070 }
3071
3072 /* Install a master breakpoint on the unwinder's debug hook.  */
3073
3074 static void
3075 create_exception_master_breakpoint (void)
3076 {
3077   struct objfile *objfile;
3078   const char *const func_name = "_Unwind_DebugHook";
3079
3080   ALL_OBJFILES (objfile)
3081     {
3082       struct breakpoint *b;
3083       struct gdbarch *gdbarch;
3084       struct breakpoint_objfile_data *bp_objfile_data;
3085       CORE_ADDR addr;
3086
3087       bp_objfile_data = get_breakpoint_objfile_data (objfile);
3088
3089       /* We prefer the SystemTap probe point if it exists.  */
3090       if (!bp_objfile_data->exception_searched)
3091         {
3092           bp_objfile_data->exception_probes
3093             = find_probes_in_objfile (objfile, "libgcc", "unwind");
3094           bp_objfile_data->exception_searched = 1;
3095         }
3096
3097       if (bp_objfile_data->exception_probes != NULL)
3098         {
3099           struct gdbarch *gdbarch = get_objfile_arch (objfile);
3100           int i;
3101           struct probe *probe;
3102
3103           for (i = 0;
3104                VEC_iterate (probe_p,
3105                             bp_objfile_data->exception_probes,
3106                             i, probe);
3107                ++i)
3108             {
3109               struct breakpoint *b;
3110
3111               b = create_internal_breakpoint (gdbarch, probe->address,
3112                                               bp_exception_master,
3113                                               &internal_breakpoint_ops);
3114               b->addr_string = xstrdup ("-probe-stap libgcc:unwind");
3115               b->enable_state = bp_disabled;
3116             }
3117
3118           continue;
3119         }
3120
3121       /* Otherwise, try the hook function.  */
3122
3123       if (msym_not_found_p (bp_objfile_data->exception_msym))
3124         continue;
3125
3126       gdbarch = get_objfile_arch (objfile);
3127
3128       if (bp_objfile_data->exception_msym == NULL)
3129         {
3130           struct minimal_symbol *debug_hook;
3131
3132           debug_hook = lookup_minimal_symbol (func_name, NULL, objfile);
3133           if (debug_hook == NULL)
3134             {
3135               bp_objfile_data->exception_msym = &msym_not_found;
3136               continue;
3137             }
3138
3139           bp_objfile_data->exception_msym = debug_hook;
3140         }
3141
3142       addr = SYMBOL_VALUE_ADDRESS (bp_objfile_data->exception_msym);
3143       addr = gdbarch_convert_from_func_ptr_addr (gdbarch, addr,
3144                                                  &current_target);
3145       b = create_internal_breakpoint (gdbarch, addr, bp_exception_master,
3146                                       &internal_breakpoint_ops);
3147       b->addr_string = xstrdup (func_name);
3148       b->enable_state = bp_disabled;
3149     }
3150
3151   update_global_location_list (1);
3152 }
3153
3154 void
3155 update_breakpoints_after_exec (void)
3156 {
3157   struct breakpoint *b, *b_tmp;
3158   struct bp_location *bploc, **bplocp_tmp;
3159
3160   /* We're about to delete breakpoints from GDB's lists.  If the
3161      INSERTED flag is true, GDB will try to lift the breakpoints by
3162      writing the breakpoints' "shadow contents" back into memory.  The
3163      "shadow contents" are NOT valid after an exec, so GDB should not
3164      do that.  Instead, the target is responsible from marking
3165      breakpoints out as soon as it detects an exec.  We don't do that
3166      here instead, because there may be other attempts to delete
3167      breakpoints after detecting an exec and before reaching here.  */
3168   ALL_BP_LOCATIONS (bploc, bplocp_tmp)
3169     if (bploc->pspace == current_program_space)
3170       gdb_assert (!bploc->inserted);
3171
3172   ALL_BREAKPOINTS_SAFE (b, b_tmp)
3173   {
3174     if (b->pspace != current_program_space)
3175       continue;
3176
3177     /* Solib breakpoints must be explicitly reset after an exec().  */
3178     if (b->type == bp_shlib_event)
3179       {
3180         delete_breakpoint (b);
3181         continue;
3182       }
3183
3184     /* JIT breakpoints must be explicitly reset after an exec().  */
3185     if (b->type == bp_jit_event)
3186       {
3187         delete_breakpoint (b);
3188         continue;
3189       }
3190
3191     /* Thread event breakpoints must be set anew after an exec(),
3192        as must overlay event and longjmp master breakpoints.  */
3193     if (b->type == bp_thread_event || b->type == bp_overlay_event
3194         || b->type == bp_longjmp_master || b->type == bp_std_terminate_master
3195         || b->type == bp_exception_master)
3196       {
3197         delete_breakpoint (b);
3198         continue;
3199       }
3200
3201     /* Step-resume breakpoints are meaningless after an exec().  */
3202     if (b->type == bp_step_resume || b->type == bp_hp_step_resume)
3203       {
3204         delete_breakpoint (b);
3205         continue;
3206       }
3207
3208     /* Longjmp and longjmp-resume breakpoints are also meaningless
3209        after an exec.  */
3210     if (b->type == bp_longjmp || b->type == bp_longjmp_resume
3211         || b->type == bp_longjmp_call_dummy
3212         || b->type == bp_exception || b->type == bp_exception_resume)
3213       {
3214         delete_breakpoint (b);
3215         continue;
3216       }
3217
3218     if (b->type == bp_catchpoint)
3219       {
3220         /* For now, none of the bp_catchpoint breakpoints need to
3221            do anything at this point.  In the future, if some of
3222            the catchpoints need to something, we will need to add
3223            a new method, and call this method from here.  */
3224         continue;
3225       }
3226
3227     /* bp_finish is a special case.  The only way we ought to be able
3228        to see one of these when an exec() has happened, is if the user
3229        caught a vfork, and then said "finish".  Ordinarily a finish just
3230        carries them to the call-site of the current callee, by setting
3231        a temporary bp there and resuming.  But in this case, the finish
3232        will carry them entirely through the vfork & exec.
3233
3234        We don't want to allow a bp_finish to remain inserted now.  But
3235        we can't safely delete it, 'cause finish_command has a handle to
3236        the bp on a bpstat, and will later want to delete it.  There's a
3237        chance (and I've seen it happen) that if we delete the bp_finish
3238        here, that its storage will get reused by the time finish_command
3239        gets 'round to deleting the "use to be a bp_finish" breakpoint.
3240        We really must allow finish_command to delete a bp_finish.
3241
3242        In the absence of a general solution for the "how do we know
3243        it's safe to delete something others may have handles to?"
3244        problem, what we'll do here is just uninsert the bp_finish, and
3245        let finish_command delete it.
3246
3247        (We know the bp_finish is "doomed" in the sense that it's
3248        momentary, and will be deleted as soon as finish_command sees
3249        the inferior stopped.  So it doesn't matter that the bp's
3250        address is probably bogus in the new a.out, unlike e.g., the
3251        solib breakpoints.)  */
3252
3253     if (b->type == bp_finish)
3254       {
3255         continue;
3256       }
3257
3258     /* Without a symbolic address, we have little hope of the
3259        pre-exec() address meaning the same thing in the post-exec()
3260        a.out.  */
3261     if (b->addr_string == NULL)
3262       {
3263         delete_breakpoint (b);
3264         continue;
3265       }
3266   }
3267   /* FIXME what about longjmp breakpoints?  Re-create them here?  */
3268   create_overlay_event_breakpoint ();
3269   create_longjmp_master_breakpoint ();
3270   create_std_terminate_master_breakpoint ();
3271   create_exception_master_breakpoint ();
3272 }
3273
3274 int
3275 detach_breakpoints (int pid)
3276 {
3277   struct bp_location *bl, **blp_tmp;
3278   int val = 0;
3279   struct cleanup *old_chain = save_inferior_ptid ();
3280   struct inferior *inf = current_inferior ();
3281
3282   if (pid == PIDGET (inferior_ptid))
3283     error (_("Cannot detach breakpoints of inferior_ptid"));
3284
3285   /* Set inferior_ptid; remove_breakpoint_1 uses this global.  */
3286   inferior_ptid = pid_to_ptid (pid);
3287   ALL_BP_LOCATIONS (bl, blp_tmp)
3288   {
3289     if (bl->pspace != inf->pspace)
3290       continue;
3291
3292     if (bl->inserted)
3293       val |= remove_breakpoint_1 (bl, mark_inserted);
3294   }
3295
3296   /* Detach single-step breakpoints as well.  */
3297   detach_single_step_breakpoints ();
3298
3299   do_cleanups (old_chain);
3300   return val;
3301 }
3302
3303 /* Remove the breakpoint location BL from the current address space.
3304    Note that this is used to detach breakpoints from a child fork.
3305    When we get here, the child isn't in the inferior list, and neither
3306    do we have objects to represent its address space --- we should
3307    *not* look at bl->pspace->aspace here.  */
3308
3309 static int
3310 remove_breakpoint_1 (struct bp_location *bl, insertion_state_t is)
3311 {
3312   int val;
3313
3314   /* BL is never in moribund_locations by our callers.  */
3315   gdb_assert (bl->owner != NULL);
3316
3317   if (bl->owner->enable_state == bp_permanent)
3318     /* Permanent breakpoints cannot be inserted or removed.  */
3319     return 0;
3320
3321   /* The type of none suggests that owner is actually deleted.
3322      This should not ever happen.  */
3323   gdb_assert (bl->owner->type != bp_none);
3324
3325   if (bl->loc_type == bp_loc_software_breakpoint
3326       || bl->loc_type == bp_loc_hardware_breakpoint)
3327     {
3328       /* "Normal" instruction breakpoint: either the standard
3329          trap-instruction bp (bp_breakpoint), or a
3330          bp_hardware_breakpoint.  */
3331
3332       /* First check to see if we have to handle an overlay.  */
3333       if (overlay_debugging == ovly_off
3334           || bl->section == NULL
3335           || !(section_is_overlay (bl->section)))
3336         {
3337           /* No overlay handling: just remove the breakpoint.  */
3338           val = bl->owner->ops->remove_location (bl);
3339         }
3340       else
3341         {
3342           /* This breakpoint is in an overlay section.
3343              Did we set a breakpoint at the LMA?  */
3344           if (!overlay_events_enabled)
3345               {
3346                 /* Yes -- overlay event support is not active, so we
3347                    should have set a breakpoint at the LMA.  Remove it.  
3348                 */
3349                 /* Ignore any failures: if the LMA is in ROM, we will
3350                    have already warned when we failed to insert it.  */
3351                 if (bl->loc_type == bp_loc_hardware_breakpoint)
3352                   target_remove_hw_breakpoint (bl->gdbarch,
3353                                                &bl->overlay_target_info);
3354                 else
3355                   target_remove_breakpoint (bl->gdbarch,
3356                                             &bl->overlay_target_info);
3357               }
3358           /* Did we set a breakpoint at the VMA? 
3359              If so, we will have marked the breakpoint 'inserted'.  */
3360           if (bl->inserted)
3361             {
3362               /* Yes -- remove it.  Previously we did not bother to
3363                  remove the breakpoint if the section had been
3364                  unmapped, but let's not rely on that being safe.  We
3365                  don't know what the overlay manager might do.  */
3366
3367               /* However, we should remove *software* breakpoints only
3368                  if the section is still mapped, or else we overwrite
3369                  wrong code with the saved shadow contents.  */
3370               if (bl->loc_type == bp_loc_hardware_breakpoint
3371                   || section_is_mapped (bl->section))
3372                 val = bl->owner->ops->remove_location (bl);
3373               else
3374                 val = 0;
3375             }
3376           else
3377             {
3378               /* No -- not inserted, so no need to remove.  No error.  */
3379               val = 0;
3380             }
3381         }
3382
3383       /* In some cases, we might not be able to remove a breakpoint
3384          in a shared library that has already been removed, but we
3385          have not yet processed the shlib unload event.  */
3386       if (val && solib_name_from_address (bl->pspace, bl->address))
3387         val = 0;
3388
3389       if (val)
3390         return val;
3391       bl->inserted = (is == mark_inserted);
3392     }
3393   else if (bl->loc_type == bp_loc_hardware_watchpoint)
3394     {
3395       gdb_assert (bl->owner->ops != NULL
3396                   && bl->owner->ops->remove_location != NULL);
3397
3398       bl->inserted = (is == mark_inserted);
3399       bl->owner->ops->remove_location (bl);
3400
3401       /* Failure to remove any of the hardware watchpoints comes here.  */
3402       if ((is == mark_uninserted) && (bl->inserted))
3403         warning (_("Could not remove hardware watchpoint %d."),
3404                  bl->owner->number);
3405     }
3406   else if (bl->owner->type == bp_catchpoint
3407            && breakpoint_enabled (bl->owner)
3408            && !bl->duplicate)
3409     {
3410       gdb_assert (bl->owner->ops != NULL
3411                   && bl->owner->ops->remove_location != NULL);
3412
3413       val = bl->owner->ops->remove_location (bl);
3414       if (val)
3415         return val;
3416
3417       bl->inserted = (is == mark_inserted);
3418     }
3419
3420   return 0;
3421 }
3422
3423 static int
3424 remove_breakpoint (struct bp_location *bl, insertion_state_t is)
3425 {
3426   int ret;
3427   struct cleanup *old_chain;
3428
3429   /* BL is never in moribund_locations by our callers.  */
3430   gdb_assert (bl->owner != NULL);
3431
3432   if (bl->owner->enable_state == bp_permanent)
3433     /* Permanent breakpoints cannot be inserted or removed.  */
3434     return 0;
3435
3436   /* The type of none suggests that owner is actually deleted.
3437      This should not ever happen.  */
3438   gdb_assert (bl->owner->type != bp_none);
3439
3440   old_chain = save_current_space_and_thread ();
3441
3442   switch_to_program_space_and_thread (bl->pspace);
3443
3444   ret = remove_breakpoint_1 (bl, is);
3445
3446   do_cleanups (old_chain);
3447   return ret;
3448 }
3449
3450 /* Clear the "inserted" flag in all breakpoints.  */
3451
3452 void
3453 mark_breakpoints_out (void)
3454 {
3455   struct bp_location *bl, **blp_tmp;
3456
3457   ALL_BP_LOCATIONS (bl, blp_tmp)
3458     if (bl->pspace == current_program_space)
3459       bl->inserted = 0;
3460 }
3461
3462 /* Clear the "inserted" flag in all breakpoints and delete any
3463    breakpoints which should go away between runs of the program.
3464
3465    Plus other such housekeeping that has to be done for breakpoints
3466    between runs.
3467
3468    Note: this function gets called at the end of a run (by
3469    generic_mourn_inferior) and when a run begins (by
3470    init_wait_for_inferior).  */
3471
3472
3473
3474 void
3475 breakpoint_init_inferior (enum inf_context context)
3476 {
3477   struct breakpoint *b, *b_tmp;
3478   struct bp_location *bl, **blp_tmp;
3479   int ix;
3480   struct program_space *pspace = current_program_space;
3481
3482   /* If breakpoint locations are shared across processes, then there's
3483      nothing to do.  */
3484   if (gdbarch_has_global_breakpoints (target_gdbarch))
3485     return;
3486
3487   ALL_BP_LOCATIONS (bl, blp_tmp)
3488   {
3489     /* ALL_BP_LOCATIONS bp_location has BL->OWNER always non-NULL.  */
3490     if (bl->pspace == pspace
3491         && bl->owner->enable_state != bp_permanent)
3492       bl->inserted = 0;
3493   }
3494
3495   ALL_BREAKPOINTS_SAFE (b, b_tmp)
3496   {
3497     if (b->loc && b->loc->pspace != pspace)
3498       continue;
3499
3500     switch (b->type)
3501       {
3502       case bp_call_dummy:
3503       case bp_longjmp_call_dummy:
3504
3505         /* If the call dummy breakpoint is at the entry point it will
3506            cause problems when the inferior is rerun, so we better get
3507            rid of it.  */
3508
3509       case bp_watchpoint_scope:
3510
3511         /* Also get rid of scope breakpoints.  */
3512
3513       case bp_shlib_event:
3514
3515         /* Also remove solib event breakpoints.  Their addresses may
3516            have changed since the last time we ran the program.
3517            Actually we may now be debugging against different target;
3518            and so the solib backend that installed this breakpoint may
3519            not be used in by the target.  E.g.,
3520
3521            (gdb) file prog-linux
3522            (gdb) run               # native linux target
3523            ...
3524            (gdb) kill
3525            (gdb) file prog-win.exe
3526            (gdb) tar rem :9999     # remote Windows gdbserver.
3527         */
3528
3529       case bp_step_resume:
3530
3531         /* Also remove step-resume breakpoints.  */
3532
3533         delete_breakpoint (b);
3534         break;
3535
3536       case bp_watchpoint:
3537       case bp_hardware_watchpoint:
3538       case bp_read_watchpoint:
3539       case bp_access_watchpoint:
3540         {
3541           struct watchpoint *w = (struct watchpoint *) b;
3542
3543           /* Likewise for watchpoints on local expressions.  */
3544           if (w->exp_valid_block != NULL)
3545             delete_breakpoint (b);
3546           else if (context == inf_starting)
3547             {
3548               /* Reset val field to force reread of starting value in
3549                  insert_breakpoints.  */
3550               if (w->val)
3551                 value_free (w->val);
3552               w->val = NULL;
3553               w->val_valid = 0;
3554           }
3555         }
3556         break;
3557       default:
3558         break;
3559       }
3560   }
3561
3562   /* Get rid of the moribund locations.  */
3563   for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, bl); ++ix)
3564     decref_bp_location (&bl);
3565   VEC_free (bp_location_p, moribund_locations);
3566 }
3567
3568 /* These functions concern about actual breakpoints inserted in the
3569    target --- to e.g. check if we need to do decr_pc adjustment or if
3570    we need to hop over the bkpt --- so we check for address space
3571    match, not program space.  */
3572
3573 /* breakpoint_here_p (PC) returns non-zero if an enabled breakpoint
3574    exists at PC.  It returns ordinary_breakpoint_here if it's an
3575    ordinary breakpoint, or permanent_breakpoint_here if it's a
3576    permanent breakpoint.
3577    - When continuing from a location with an ordinary breakpoint, we
3578      actually single step once before calling insert_breakpoints.
3579    - When continuing from a location with a permanent breakpoint, we
3580      need to use the `SKIP_PERMANENT_BREAKPOINT' macro, provided by
3581      the target, to advance the PC past the breakpoint.  */
3582
3583 enum breakpoint_here
3584 breakpoint_here_p (struct address_space *aspace, CORE_ADDR pc)
3585 {
3586   struct bp_location *bl, **blp_tmp;
3587   int any_breakpoint_here = 0;
3588
3589   ALL_BP_LOCATIONS (bl, blp_tmp)
3590     {
3591       if (bl->loc_type != bp_loc_software_breakpoint
3592           && bl->loc_type != bp_loc_hardware_breakpoint)
3593         continue;
3594
3595       /* ALL_BP_LOCATIONS bp_location has BL->OWNER always non-NULL.  */
3596       if ((breakpoint_enabled (bl->owner)
3597            || bl->owner->enable_state == bp_permanent)
3598           && breakpoint_location_address_match (bl, aspace, pc))
3599         {
3600           if (overlay_debugging 
3601               && section_is_overlay (bl->section)
3602               && !section_is_mapped (bl->section))
3603             continue;           /* unmapped overlay -- can't be a match */
3604           else if (bl->owner->enable_state == bp_permanent)
3605             return permanent_breakpoint_here;
3606           else
3607             any_breakpoint_here = 1;
3608         }
3609     }
3610
3611   return any_breakpoint_here ? ordinary_breakpoint_here : 0;
3612 }
3613
3614 /* Return true if there's a moribund breakpoint at PC.  */
3615
3616 int
3617 moribund_breakpoint_here_p (struct address_space *aspace, CORE_ADDR pc)
3618 {
3619   struct bp_location *loc;
3620   int ix;
3621
3622   for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, loc); ++ix)
3623     if (breakpoint_location_address_match (loc, aspace, pc))
3624       return 1;
3625
3626   return 0;
3627 }
3628
3629 /* Returns non-zero if there's a breakpoint inserted at PC, which is
3630    inserted using regular breakpoint_chain / bp_location array
3631    mechanism.  This does not check for single-step breakpoints, which
3632    are inserted and removed using direct target manipulation.  */
3633
3634 int
3635 regular_breakpoint_inserted_here_p (struct address_space *aspace, 
3636                                     CORE_ADDR pc)
3637 {
3638   struct bp_location *bl, **blp_tmp;
3639
3640   ALL_BP_LOCATIONS (bl, blp_tmp)
3641     {
3642       if (bl->loc_type != bp_loc_software_breakpoint
3643           && bl->loc_type != bp_loc_hardware_breakpoint)
3644         continue;
3645
3646       if (bl->inserted
3647           && breakpoint_location_address_match (bl, aspace, pc))
3648         {
3649           if (overlay_debugging 
3650               && section_is_overlay (bl->section)
3651               && !section_is_mapped (bl->section))
3652             continue;           /* unmapped overlay -- can't be a match */
3653           else
3654             return 1;
3655         }
3656     }
3657   return 0;
3658 }
3659
3660 /* Returns non-zero iff there's either regular breakpoint
3661    or a single step breakpoint inserted at PC.  */
3662
3663 int
3664 breakpoint_inserted_here_p (struct address_space *aspace, CORE_ADDR pc)
3665 {
3666   if (regular_breakpoint_inserted_here_p (aspace, pc))
3667     return 1;
3668
3669   if (single_step_breakpoint_inserted_here_p (aspace, pc))
3670     return 1;
3671
3672   return 0;
3673 }
3674
3675 /* This function returns non-zero iff there is a software breakpoint
3676    inserted at PC.  */
3677
3678 int
3679 software_breakpoint_inserted_here_p (struct address_space *aspace,
3680                                      CORE_ADDR pc)
3681 {
3682   struct bp_location *bl, **blp_tmp;
3683
3684   ALL_BP_LOCATIONS (bl, blp_tmp)
3685     {
3686       if (bl->loc_type != bp_loc_software_breakpoint)
3687         continue;
3688
3689       if (bl->inserted
3690           && breakpoint_address_match (bl->pspace->aspace, bl->address,
3691                                        aspace, pc))
3692         {
3693           if (overlay_debugging 
3694               && section_is_overlay (bl->section)
3695               && !section_is_mapped (bl->section))
3696             continue;           /* unmapped overlay -- can't be a match */
3697           else
3698             return 1;
3699         }
3700     }
3701
3702   /* Also check for software single-step breakpoints.  */
3703   if (single_step_breakpoint_inserted_here_p (aspace, pc))
3704     return 1;
3705
3706   return 0;
3707 }
3708
3709 int
3710 hardware_watchpoint_inserted_in_range (struct address_space *aspace,
3711                                        CORE_ADDR addr, ULONGEST len)
3712 {
3713   struct breakpoint *bpt;
3714
3715   ALL_BREAKPOINTS (bpt)
3716     {
3717       struct bp_location *loc;
3718
3719       if (bpt->type != bp_hardware_watchpoint
3720           && bpt->type != bp_access_watchpoint)
3721         continue;
3722
3723       if (!breakpoint_enabled (bpt))
3724         continue;
3725
3726       for (loc = bpt->loc; loc; loc = loc->next)
3727         if (loc->pspace->aspace == aspace && loc->inserted)
3728           {
3729             CORE_ADDR l, h;
3730
3731             /* Check for intersection.  */
3732             l = max (loc->address, addr);
3733             h = min (loc->address + loc->length, addr + len);
3734             if (l < h)
3735               return 1;
3736           }
3737     }
3738   return 0;
3739 }
3740
3741 /* breakpoint_thread_match (PC, PTID) returns true if the breakpoint at
3742    PC is valid for process/thread PTID.  */
3743
3744 int
3745 breakpoint_thread_match (struct address_space *aspace, CORE_ADDR pc,
3746                          ptid_t ptid)
3747 {
3748   struct bp_location *bl, **blp_tmp;
3749   /* The thread and task IDs associated to PTID, computed lazily.  */
3750   int thread = -1;
3751   int task = 0;
3752   
3753   ALL_BP_LOCATIONS (bl, blp_tmp)
3754     {
3755       if (bl->loc_type != bp_loc_software_breakpoint
3756           && bl->loc_type != bp_loc_hardware_breakpoint)
3757         continue;
3758
3759       /* ALL_BP_LOCATIONS bp_location has bl->OWNER always non-NULL.  */
3760       if (!breakpoint_enabled (bl->owner)
3761           && bl->owner->enable_state != bp_permanent)
3762         continue;
3763
3764       if (!breakpoint_location_address_match (bl, aspace, pc))
3765         continue;
3766
3767       if (bl->owner->thread != -1)
3768         {
3769           /* This is a thread-specific breakpoint.  Check that ptid
3770              matches that thread.  If thread hasn't been computed yet,
3771              it is now time to do so.  */
3772           if (thread == -1)
3773             thread = pid_to_thread_id (ptid);
3774           if (bl->owner->thread != thread)
3775             continue;
3776         }
3777
3778       if (bl->owner->task != 0)
3779         {
3780           /* This is a task-specific breakpoint.  Check that ptid
3781              matches that task.  If task hasn't been computed yet,
3782              it is now time to do so.  */
3783           if (task == 0)
3784             task = ada_get_task_number (ptid);
3785           if (bl->owner->task != task)
3786             continue;
3787         }
3788
3789       if (overlay_debugging 
3790           && section_is_overlay (bl->section)
3791           && !section_is_mapped (bl->section))
3792         continue;           /* unmapped overlay -- can't be a match */
3793
3794       return 1;
3795     }
3796
3797   return 0;
3798 }
3799 \f
3800
3801 /* bpstat stuff.  External routines' interfaces are documented
3802    in breakpoint.h.  */
3803
3804 int
3805 is_catchpoint (struct breakpoint *ep)
3806 {
3807   return (ep->type == bp_catchpoint);
3808 }
3809
3810 /* Frees any storage that is part of a bpstat.  Does not walk the
3811    'next' chain.  */
3812
3813 static void
3814 bpstat_free (bpstat bs)
3815 {
3816   if (bs->old_val != NULL)
3817     value_free (bs->old_val);
3818   decref_counted_command_line (&bs->commands);
3819   decref_bp_location (&bs->bp_location_at);
3820   xfree (bs);
3821 }
3822
3823 /* Clear a bpstat so that it says we are not at any breakpoint.
3824    Also free any storage that is part of a bpstat.  */
3825
3826 void
3827 bpstat_clear (bpstat *bsp)
3828 {
3829   bpstat p;
3830   bpstat q;
3831
3832   if (bsp == 0)
3833     return;
3834   p = *bsp;
3835   while (p != NULL)
3836     {
3837       q = p->next;
3838       bpstat_free (p);
3839       p = q;
3840     }
3841   *bsp = NULL;
3842 }
3843
3844 /* Return a copy of a bpstat.  Like "bs1 = bs2" but all storage that
3845    is part of the bpstat is copied as well.  */
3846
3847 bpstat
3848 bpstat_copy (bpstat bs)
3849 {
3850   bpstat p = NULL;
3851   bpstat tmp;
3852   bpstat retval = NULL;
3853
3854   if (bs == NULL)
3855     return bs;
3856
3857   for (; bs != NULL; bs = bs->next)
3858     {
3859       tmp = (bpstat) xmalloc (sizeof (*tmp));
3860       memcpy (tmp, bs, sizeof (*tmp));
3861       incref_counted_command_line (tmp->commands);
3862       incref_bp_location (tmp->bp_location_at);
3863       if (bs->old_val != NULL)
3864         {
3865           tmp->old_val = value_copy (bs->old_val);
3866           release_value (tmp->old_val);
3867         }
3868
3869       if (p == NULL)
3870         /* This is the first thing in the chain.  */
3871         retval = tmp;
3872       else
3873         p->next = tmp;
3874       p = tmp;
3875     }
3876   p->next = NULL;
3877   return retval;
3878 }
3879
3880 /* Find the bpstat associated with this breakpoint.  */
3881
3882 bpstat
3883 bpstat_find_breakpoint (bpstat bsp, struct breakpoint *breakpoint)
3884 {
3885   if (bsp == NULL)
3886     return NULL;
3887
3888   for (; bsp != NULL; bsp = bsp->next)
3889     {
3890       if (bsp->breakpoint_at == breakpoint)
3891         return bsp;
3892     }
3893   return NULL;
3894 }
3895
3896 /* Put in *NUM the breakpoint number of the first breakpoint we are
3897    stopped at.  *BSP upon return is a bpstat which points to the
3898    remaining breakpoints stopped at (but which is not guaranteed to be
3899    good for anything but further calls to bpstat_num).
3900
3901    Return 0 if passed a bpstat which does not indicate any breakpoints.
3902    Return -1 if stopped at a breakpoint that has been deleted since
3903    we set it.
3904    Return 1 otherwise.  */
3905
3906 int
3907 bpstat_num (bpstat *bsp, int *num)
3908 {
3909   struct breakpoint *b;
3910
3911   if ((*bsp) == NULL)
3912     return 0;                   /* No more breakpoint values */
3913
3914   /* We assume we'll never have several bpstats that correspond to a
3915      single breakpoint -- otherwise, this function might return the
3916      same number more than once and this will look ugly.  */
3917   b = (*bsp)->breakpoint_at;
3918   *bsp = (*bsp)->next;
3919   if (b == NULL)
3920     return -1;                  /* breakpoint that's been deleted since */
3921
3922   *num = b->number;             /* We have its number */
3923   return 1;
3924 }
3925
3926 /* See breakpoint.h.  */
3927
3928 void
3929 bpstat_clear_actions (void)
3930 {
3931   struct thread_info *tp;
3932   bpstat bs;
3933
3934   if (ptid_equal (inferior_ptid, null_ptid))
3935     return;
3936
3937   tp = find_thread_ptid (inferior_ptid);
3938   if (tp == NULL)
3939     return;
3940
3941   for (bs = tp->control.stop_bpstat; bs != NULL; bs = bs->next)
3942     {
3943       decref_counted_command_line (&bs->commands);
3944
3945       if (bs->old_val != NULL)
3946         {
3947           value_free (bs->old_val);
3948           bs->old_val = NULL;
3949         }
3950     }
3951 }
3952
3953 /* Called when a command is about to proceed the inferior.  */
3954
3955 static void
3956 breakpoint_about_to_proceed (void)
3957 {
3958   if (!ptid_equal (inferior_ptid, null_ptid))
3959     {
3960       struct thread_info *tp = inferior_thread ();
3961
3962       /* Allow inferior function calls in breakpoint commands to not
3963          interrupt the command list.  When the call finishes
3964          successfully, the inferior will be standing at the same
3965          breakpoint as if nothing happened.  */
3966       if (tp->control.in_infcall)
3967         return;
3968     }
3969
3970   breakpoint_proceeded = 1;
3971 }
3972
3973 /* Stub for cleaning up our state if we error-out of a breakpoint
3974    command.  */
3975 static void
3976 cleanup_executing_breakpoints (void *ignore)
3977 {
3978   executing_breakpoint_commands = 0;
3979 }
3980
3981 /* Return non-zero iff CMD as the first line of a command sequence is `silent'
3982    or its equivalent.  */
3983
3984 static int
3985 command_line_is_silent (struct command_line *cmd)
3986 {
3987   return cmd && (strcmp ("silent", cmd->line) == 0
3988                  || (xdb_commands && strcmp ("Q", cmd->line) == 0));
3989 }
3990
3991 /* Execute all the commands associated with all the breakpoints at
3992    this location.  Any of these commands could cause the process to
3993    proceed beyond this point, etc.  We look out for such changes by
3994    checking the global "breakpoint_proceeded" after each command.
3995
3996    Returns true if a breakpoint command resumed the inferior.  In that
3997    case, it is the caller's responsibility to recall it again with the
3998    bpstat of the current thread.  */
3999
4000 static int
4001 bpstat_do_actions_1 (bpstat *bsp)
4002 {
4003   bpstat bs;
4004   struct cleanup *old_chain;
4005   int again = 0;
4006
4007   /* Avoid endless recursion if a `source' command is contained
4008      in bs->commands.  */
4009   if (executing_breakpoint_commands)
4010     return 0;
4011
4012   executing_breakpoint_commands = 1;
4013   old_chain = make_cleanup (cleanup_executing_breakpoints, 0);
4014
4015   prevent_dont_repeat ();
4016
4017   /* This pointer will iterate over the list of bpstat's.  */
4018   bs = *bsp;
4019
4020   breakpoint_proceeded = 0;
4021   for (; bs != NULL; bs = bs->next)
4022     {
4023       struct counted_command_line *ccmd;
4024       struct command_line *cmd;
4025       struct cleanup *this_cmd_tree_chain;
4026
4027       /* Take ownership of the BSP's command tree, if it has one.
4028
4029          The command tree could legitimately contain commands like
4030          'step' and 'next', which call clear_proceed_status, which
4031          frees stop_bpstat's command tree.  To make sure this doesn't
4032          free the tree we're executing out from under us, we need to
4033          take ownership of the tree ourselves.  Since a given bpstat's
4034          commands are only executed once, we don't need to copy it; we
4035          can clear the pointer in the bpstat, and make sure we free
4036          the tree when we're done.  */
4037       ccmd = bs->commands;
4038       bs->commands = NULL;
4039       this_cmd_tree_chain = make_cleanup_decref_counted_command_line (&ccmd);
4040       cmd = ccmd ? ccmd->commands : NULL;
4041       if (command_line_is_silent (cmd))
4042         {
4043           /* The action has been already done by bpstat_stop_status.  */
4044           cmd = cmd->next;
4045         }
4046
4047       while (cmd != NULL)
4048         {
4049           execute_control_command (cmd);
4050
4051           if (breakpoint_proceeded)
4052             break;
4053           else
4054             cmd = cmd->next;
4055         }
4056
4057       /* We can free this command tree now.  */
4058       do_cleanups (this_cmd_tree_chain);
4059
4060       if (breakpoint_proceeded)
4061         {
4062           if (target_can_async_p ())
4063             /* If we are in async mode, then the target might be still
4064                running, not stopped at any breakpoint, so nothing for
4065                us to do here -- just return to the event loop.  */
4066             ;
4067           else
4068             /* In sync mode, when execute_control_command returns
4069                we're already standing on the next breakpoint.
4070                Breakpoint commands for that stop were not run, since
4071                execute_command does not run breakpoint commands --
4072                only command_line_handler does, but that one is not
4073                involved in execution of breakpoint commands.  So, we
4074                can now execute breakpoint commands.  It should be
4075                noted that making execute_command do bpstat actions is
4076                not an option -- in this case we'll have recursive
4077                invocation of bpstat for each breakpoint with a
4078                command, and can easily blow up GDB stack.  Instead, we
4079                return true, which will trigger the caller to recall us
4080                with the new stop_bpstat.  */
4081             again = 1;
4082           break;
4083         }
4084     }
4085   do_cleanups (old_chain);
4086   return again;
4087 }
4088
4089 void
4090 bpstat_do_actions (void)
4091 {
4092   struct cleanup *cleanup_if_error = make_bpstat_clear_actions_cleanup ();
4093
4094   /* Do any commands attached to breakpoint we are stopped at.  */
4095   while (!ptid_equal (inferior_ptid, null_ptid)
4096          && target_has_execution
4097          && !is_exited (inferior_ptid)
4098          && !is_executing (inferior_ptid))
4099     /* Since in sync mode, bpstat_do_actions may resume the inferior,
4100        and only return when it is stopped at the next breakpoint, we
4101        keep doing breakpoint actions until it returns false to
4102        indicate the inferior was not resumed.  */
4103     if (!bpstat_do_actions_1 (&inferior_thread ()->control.stop_bpstat))
4104       break;
4105
4106   discard_cleanups (cleanup_if_error);
4107 }
4108
4109 /* Print out the (old or new) value associated with a watchpoint.  */
4110
4111 static void
4112 watchpoint_value_print (struct value *val, struct ui_file *stream)
4113 {
4114   if (val == NULL)
4115     fprintf_unfiltered (stream, _("<unreadable>"));
4116   else
4117     {
4118       struct value_print_options opts;
4119       get_user_print_options (&opts);
4120       value_print (val, stream, &opts);
4121     }
4122 }
4123
4124 /* Generic routine for printing messages indicating why we
4125    stopped.  The behavior of this function depends on the value
4126    'print_it' in the bpstat structure.  Under some circumstances we
4127    may decide not to print anything here and delegate the task to
4128    normal_stop().  */
4129
4130 static enum print_stop_action
4131 print_bp_stop_message (bpstat bs)
4132 {
4133   switch (bs->print_it)
4134     {
4135     case print_it_noop:
4136       /* Nothing should be printed for this bpstat entry.  */
4137       return PRINT_UNKNOWN;
4138       break;
4139
4140     case print_it_done:
4141       /* We still want to print the frame, but we already printed the
4142          relevant messages.  */
4143       return PRINT_SRC_AND_LOC;
4144       break;
4145
4146     case print_it_normal:
4147       {
4148         struct breakpoint *b = bs->breakpoint_at;
4149
4150         /* bs->breakpoint_at can be NULL if it was a momentary breakpoint
4151            which has since been deleted.  */
4152         if (b == NULL)
4153           return PRINT_UNKNOWN;
4154
4155         /* Normal case.  Call the breakpoint's print_it method.  */
4156         return b->ops->print_it (bs);
4157       }
4158       break;
4159
4160     default:
4161       internal_error (__FILE__, __LINE__,
4162                       _("print_bp_stop_message: unrecognized enum value"));
4163       break;
4164     }
4165 }
4166
4167 /* A helper function that prints a shared library stopped event.  */
4168
4169 static void
4170 print_solib_event (int is_catchpoint)
4171 {
4172   int any_deleted
4173     = !VEC_empty (char_ptr, current_program_space->deleted_solibs);
4174   int any_added
4175     = !VEC_empty (so_list_ptr, current_program_space->added_solibs);
4176
4177   if (!is_catchpoint)
4178     {
4179       if (any_added || any_deleted)
4180         ui_out_text (current_uiout,
4181                      _("Stopped due to shared library event:\n"));
4182       else
4183         ui_out_text (current_uiout,
4184                      _("Stopped due to shared library event (no "
4185                        "libraries added or removed)\n"));
4186     }
4187
4188   if (ui_out_is_mi_like_p (current_uiout))
4189     ui_out_field_string (current_uiout, "reason",
4190                          async_reason_lookup (EXEC_ASYNC_SOLIB_EVENT));
4191
4192   if (any_deleted)
4193     {
4194       struct cleanup *cleanup;
4195       char *name;
4196       int ix;
4197
4198       ui_out_text (current_uiout, _("  Inferior unloaded "));
4199       cleanup = make_cleanup_ui_out_list_begin_end (current_uiout,
4200                                                     "removed");
4201       for (ix = 0;
4202            VEC_iterate (char_ptr, current_program_space->deleted_solibs,
4203                         ix, name);
4204            ++ix)
4205         {
4206           if (ix > 0)
4207             ui_out_text (current_uiout, "    ");
4208           ui_out_field_string (current_uiout, "library", name);
4209           ui_out_text (current_uiout, "\n");
4210         }
4211
4212       do_cleanups (cleanup);
4213     }
4214
4215   if (any_added)
4216     {
4217       struct so_list *iter;
4218       int ix;
4219       struct cleanup *cleanup;
4220
4221       ui_out_text (current_uiout, _("  Inferior loaded "));
4222       cleanup = make_cleanup_ui_out_list_begin_end (current_uiout,
4223                                                     "added");
4224       for (ix = 0;
4225            VEC_iterate (so_list_ptr, current_program_space->added_solibs,
4226                         ix, iter);
4227            ++ix)
4228         {
4229           if (ix > 0)
4230             ui_out_text (current_uiout, "    ");
4231           ui_out_field_string (current_uiout, "library", iter->so_name);
4232           ui_out_text (current_uiout, "\n");
4233         }
4234
4235       do_cleanups (cleanup);
4236     }
4237 }
4238
4239 /* Print a message indicating what happened.  This is called from
4240    normal_stop().  The input to this routine is the head of the bpstat
4241    list - a list of the eventpoints that caused this stop.  KIND is
4242    the target_waitkind for the stopping event.  This
4243    routine calls the generic print routine for printing a message
4244    about reasons for stopping.  This will print (for example) the
4245    "Breakpoint n," part of the output.  The return value of this
4246    routine is one of:
4247
4248    PRINT_UNKNOWN: Means we printed nothing.
4249    PRINT_SRC_AND_LOC: Means we printed something, and expect subsequent
4250    code to print the location.  An example is 
4251    "Breakpoint 1, " which should be followed by
4252    the location.
4253    PRINT_SRC_ONLY: Means we printed something, but there is no need
4254    to also print the location part of the message.
4255    An example is the catch/throw messages, which
4256    don't require a location appended to the end.
4257    PRINT_NOTHING: We have done some printing and we don't need any 
4258    further info to be printed.  */
4259
4260 enum print_stop_action
4261 bpstat_print (bpstat bs, int kind)
4262 {
4263   int val;
4264
4265   /* Maybe another breakpoint in the chain caused us to stop.
4266      (Currently all watchpoints go on the bpstat whether hit or not.
4267      That probably could (should) be changed, provided care is taken
4268      with respect to bpstat_explains_signal).  */
4269   for (; bs; bs = bs->next)
4270     {
4271       val = print_bp_stop_message (bs);
4272       if (val == PRINT_SRC_ONLY 
4273           || val == PRINT_SRC_AND_LOC 
4274           || val == PRINT_NOTHING)
4275         return val;
4276     }
4277
4278   /* If we had hit a shared library event breakpoint,
4279      print_bp_stop_message would print out this message.  If we hit an
4280      OS-level shared library event, do the same thing.  */
4281   if (kind == TARGET_WAITKIND_LOADED)
4282     {
4283       print_solib_event (0);
4284       return PRINT_NOTHING;
4285     }
4286
4287   /* We reached the end of the chain, or we got a null BS to start
4288      with and nothing was printed.  */
4289   return PRINT_UNKNOWN;
4290 }
4291
4292 /* Evaluate the expression EXP and return 1 if value is zero.  This is
4293    used inside a catch_errors to evaluate the breakpoint condition.
4294    The argument is a "struct expression *" that has been cast to a
4295    "char *" to make it pass through catch_errors.  */
4296
4297 static int
4298 breakpoint_cond_eval (void *exp)
4299 {
4300   struct value *mark = value_mark ();
4301   int i = !value_true (evaluate_expression ((struct expression *) exp));
4302
4303   value_free_to_mark (mark);
4304   return i;
4305 }
4306
4307 /* Allocate a new bpstat.  Link it to the FIFO list by BS_LINK_POINTER.  */
4308
4309 static bpstat
4310 bpstat_alloc (struct bp_location *bl, bpstat **bs_link_pointer)
4311 {
4312   bpstat bs;
4313
4314   bs = (bpstat) xmalloc (sizeof (*bs));
4315   bs->next = NULL;
4316   **bs_link_pointer = bs;
4317   *bs_link_pointer = &bs->next;
4318   bs->breakpoint_at = bl->owner;
4319   bs->bp_location_at = bl;
4320   incref_bp_location (bl);
4321   /* If the condition is false, etc., don't do the commands.  */
4322   bs->commands = NULL;
4323   bs->old_val = NULL;
4324   bs->print_it = print_it_normal;
4325   return bs;
4326 }
4327 \f
4328 /* The target has stopped with waitstatus WS.  Check if any hardware
4329    watchpoints have triggered, according to the target.  */
4330
4331 int
4332 watchpoints_triggered (struct target_waitstatus *ws)
4333 {
4334   int stopped_by_watchpoint = target_stopped_by_watchpoint ();
4335   CORE_ADDR addr;
4336   struct breakpoint *b;
4337
4338   if (!stopped_by_watchpoint)
4339     {
4340       /* We were not stopped by a watchpoint.  Mark all watchpoints
4341          as not triggered.  */
4342       ALL_BREAKPOINTS (b)
4343         if (is_hardware_watchpoint (b))
4344           {
4345             struct watchpoint *w = (struct watchpoint *) b;
4346
4347             w->watchpoint_triggered = watch_triggered_no;
4348           }
4349
4350       return 0;
4351     }
4352
4353   if (!target_stopped_data_address (&current_target, &addr))
4354     {
4355       /* We were stopped by a watchpoint, but we don't know where.
4356          Mark all watchpoints as unknown.  */
4357       ALL_BREAKPOINTS (b)
4358         if (is_hardware_watchpoint (b))
4359           {
4360             struct watchpoint *w = (struct watchpoint *) b;
4361
4362             w->watchpoint_triggered = watch_triggered_unknown;
4363           }
4364
4365       return stopped_by_watchpoint;
4366     }
4367
4368   /* The target could report the data address.  Mark watchpoints
4369      affected by this data address as triggered, and all others as not
4370      triggered.  */
4371
4372   ALL_BREAKPOINTS (b)
4373     if (is_hardware_watchpoint (b))
4374       {
4375         struct watchpoint *w = (struct watchpoint *) b;
4376         struct bp_location *loc;
4377
4378         w->watchpoint_triggered = watch_triggered_no;
4379         for (loc = b->loc; loc; loc = loc->next)
4380           {
4381             if (is_masked_watchpoint (b))
4382               {
4383                 CORE_ADDR newaddr = addr & w->hw_wp_mask;
4384                 CORE_ADDR start = loc->address & w->hw_wp_mask;
4385
4386                 if (newaddr == start)
4387                   {
4388                     w->watchpoint_triggered = watch_triggered_yes;
4389                     break;
4390                   }
4391               }
4392             /* Exact match not required.  Within range is sufficient.  */
4393             else if (target_watchpoint_addr_within_range (&current_target,
4394                                                          addr, loc->address,
4395                                                          loc->length))
4396               {
4397                 w->watchpoint_triggered = watch_triggered_yes;
4398                 break;
4399               }
4400           }
4401       }
4402
4403   return 1;
4404 }
4405
4406 /* Possible return values for watchpoint_check (this can't be an enum
4407    because of check_errors).  */
4408 /* The watchpoint has been deleted.  */
4409 #define WP_DELETED 1
4410 /* The value has changed.  */
4411 #define WP_VALUE_CHANGED 2
4412 /* The value has not changed.  */
4413 #define WP_VALUE_NOT_CHANGED 3
4414 /* Ignore this watchpoint, no matter if the value changed or not.  */
4415 #define WP_IGNORE 4
4416
4417 #define BP_TEMPFLAG 1
4418 #define BP_HARDWAREFLAG 2
4419
4420 /* Evaluate watchpoint condition expression and check if its value
4421    changed.
4422
4423    P should be a pointer to struct bpstat, but is defined as a void *
4424    in order for this function to be usable with catch_errors.  */
4425
4426 static int
4427 watchpoint_check (void *p)
4428 {
4429   bpstat bs = (bpstat) p;
4430   struct watchpoint *b;
4431   struct frame_info *fr;
4432   int within_current_scope;
4433
4434   /* BS is built from an existing struct breakpoint.  */
4435   gdb_assert (bs->breakpoint_at != NULL);
4436   b = (struct watchpoint *) bs->breakpoint_at;
4437
4438   /* If this is a local watchpoint, we only want to check if the
4439      watchpoint frame is in scope if the current thread is the thread
4440      that was used to create the watchpoint.  */
4441   if (!watchpoint_in_thread_scope (b))
4442     return WP_IGNORE;
4443
4444   if (b->exp_valid_block == NULL)
4445     within_current_scope = 1;
4446   else
4447     {
4448       struct frame_info *frame = get_current_frame ();
4449       struct gdbarch *frame_arch = get_frame_arch (frame);
4450       CORE_ADDR frame_pc = get_frame_pc (frame);
4451
4452       /* in_function_epilogue_p() returns a non-zero value if we're
4453          still in the function but the stack frame has already been
4454          invalidated.  Since we can't rely on the values of local
4455          variables after the stack has been destroyed, we are treating
4456          the watchpoint in that state as `not changed' without further
4457          checking.  Don't mark watchpoints as changed if the current
4458          frame is in an epilogue - even if they are in some other
4459          frame, our view of the stack is likely to be wrong and
4460          frame_find_by_id could error out.  */
4461       if (gdbarch_in_function_epilogue_p (frame_arch, frame_pc))
4462         return WP_IGNORE;
4463
4464       fr = frame_find_by_id (b->watchpoint_frame);
4465       within_current_scope = (fr != NULL);
4466
4467       /* If we've gotten confused in the unwinder, we might have
4468          returned a frame that can't describe this variable.  */
4469       if (within_current_scope)
4470         {
4471           struct symbol *function;
4472
4473           function = get_frame_function (fr);
4474           if (function == NULL
4475               || !contained_in (b->exp_valid_block,
4476                                 SYMBOL_BLOCK_VALUE (function)))
4477             within_current_scope = 0;
4478         }
4479
4480       if (within_current_scope)
4481         /* If we end up stopping, the current frame will get selected
4482            in normal_stop.  So this call to select_frame won't affect
4483            the user.  */
4484         select_frame (fr);
4485     }
4486
4487   if (within_current_scope)
4488     {
4489       /* We use value_{,free_to_}mark because it could be a *long*
4490          time before we return to the command level and call
4491          free_all_values.  We can't call free_all_values because we
4492          might be in the middle of evaluating a function call.  */
4493
4494       int pc = 0;
4495       struct value *mark;
4496       struct value *new_val;
4497
4498       if (is_masked_watchpoint (&b->base))
4499         /* Since we don't know the exact trigger address (from
4500            stopped_data_address), just tell the user we've triggered
4501            a mask watchpoint.  */
4502         return WP_VALUE_CHANGED;
4503
4504       mark = value_mark ();
4505       fetch_subexp_value (b->exp, &pc, &new_val, NULL, NULL);
4506
4507       /* We use value_equal_contents instead of value_equal because
4508          the latter coerces an array to a pointer, thus comparing just
4509          the address of the array instead of its contents.  This is
4510          not what we want.  */
4511       if ((b->val != NULL) != (new_val != NULL)
4512           || (b->val != NULL && !value_equal_contents (b->val, new_val)))
4513         {
4514           if (new_val != NULL)
4515             {
4516               release_value (new_val);
4517               value_free_to_mark (mark);
4518             }
4519           bs->old_val = b->val;
4520           b->val = new_val;
4521           b->val_valid = 1;
4522           return WP_VALUE_CHANGED;
4523         }
4524       else
4525         {
4526           /* Nothing changed.  */
4527           value_free_to_mark (mark);
4528           return WP_VALUE_NOT_CHANGED;
4529         }
4530     }
4531   else
4532     {
4533       struct ui_out *uiout = current_uiout;
4534
4535       /* This seems like the only logical thing to do because
4536          if we temporarily ignored the watchpoint, then when
4537          we reenter the block in which it is valid it contains
4538          garbage (in the case of a function, it may have two
4539          garbage values, one before and one after the prologue).
4540          So we can't even detect the first assignment to it and
4541          watch after that (since the garbage may or may not equal
4542          the first value assigned).  */
4543       /* We print all the stop information in
4544          breakpoint_ops->print_it, but in this case, by the time we
4545          call breakpoint_ops->print_it this bp will be deleted
4546          already.  So we have no choice but print the information
4547          here.  */
4548       if (ui_out_is_mi_like_p (uiout))
4549         ui_out_field_string
4550           (uiout, "reason", async_reason_lookup (EXEC_ASYNC_WATCHPOINT_SCOPE));
4551       ui_out_text (uiout, "\nWatchpoint ");
4552       ui_out_field_int (uiout, "wpnum", b->base.number);
4553       ui_out_text (uiout,
4554                    " deleted because the program has left the block in\n\
4555 which its expression is valid.\n");     
4556
4557       /* Make sure the watchpoint's commands aren't executed.  */
4558       decref_counted_command_line (&b->base.commands);
4559       watchpoint_del_at_next_stop (b);
4560
4561       return WP_DELETED;
4562     }
4563 }
4564
4565 /* Return true if it looks like target has stopped due to hitting
4566    breakpoint location BL.  This function does not check if we should
4567    stop, only if BL explains the stop.  */
4568
4569 static int
4570 bpstat_check_location (const struct bp_location *bl,
4571                        struct address_space *aspace, CORE_ADDR bp_addr,
4572                        const struct target_waitstatus *ws)
4573 {
4574   struct breakpoint *b = bl->owner;
4575
4576   /* BL is from an existing breakpoint.  */
4577   gdb_assert (b != NULL);
4578
4579   return b->ops->breakpoint_hit (bl, aspace, bp_addr, ws);
4580 }
4581
4582 /* Determine if the watched values have actually changed, and we
4583    should stop.  If not, set BS->stop to 0.  */
4584
4585 static void
4586 bpstat_check_watchpoint (bpstat bs)
4587 {
4588   const struct bp_location *bl;
4589   struct watchpoint *b;
4590
4591   /* BS is built for existing struct breakpoint.  */
4592   bl = bs->bp_location_at;
4593   gdb_assert (bl != NULL);
4594   b = (struct watchpoint *) bs->breakpoint_at;
4595   gdb_assert (b != NULL);
4596
4597     {
4598       int must_check_value = 0;
4599       
4600       if (b->base.type == bp_watchpoint)
4601         /* For a software watchpoint, we must always check the
4602            watched value.  */
4603         must_check_value = 1;
4604       else if (b->watchpoint_triggered == watch_triggered_yes)
4605         /* We have a hardware watchpoint (read, write, or access)
4606            and the target earlier reported an address watched by
4607            this watchpoint.  */
4608         must_check_value = 1;
4609       else if (b->watchpoint_triggered == watch_triggered_unknown
4610                && b->base.type == bp_hardware_watchpoint)
4611         /* We were stopped by a hardware watchpoint, but the target could
4612            not report the data address.  We must check the watchpoint's
4613            value.  Access and read watchpoints are out of luck; without
4614            a data address, we can't figure it out.  */
4615         must_check_value = 1;
4616
4617       if (must_check_value)
4618         {
4619           char *message
4620             = xstrprintf ("Error evaluating expression for watchpoint %d\n",
4621                           b->base.number);
4622           struct cleanup *cleanups = make_cleanup (xfree, message);
4623           int e = catch_errors (watchpoint_check, bs, message,
4624                                 RETURN_MASK_ALL);
4625           do_cleanups (cleanups);
4626           switch (e)
4627             {
4628             case WP_DELETED:
4629               /* We've already printed what needs to be printed.  */
4630               bs->print_it = print_it_done;
4631               /* Stop.  */
4632               break;
4633             case WP_IGNORE:
4634               bs->print_it = print_it_noop;
4635               bs->stop = 0;
4636               break;
4637             case WP_VALUE_CHANGED:
4638               if (b->base.type == bp_read_watchpoint)
4639                 {
4640                   /* There are two cases to consider here:
4641
4642                      1. We're watching the triggered memory for reads.
4643                      In that case, trust the target, and always report
4644                      the watchpoint hit to the user.  Even though
4645                      reads don't cause value changes, the value may
4646                      have changed since the last time it was read, and
4647                      since we're not trapping writes, we will not see
4648                      those, and as such we should ignore our notion of
4649                      old value.
4650
4651                      2. We're watching the triggered memory for both
4652                      reads and writes.  There are two ways this may
4653                      happen:
4654
4655                      2.1. This is a target that can't break on data
4656                      reads only, but can break on accesses (reads or
4657                      writes), such as e.g., x86.  We detect this case
4658                      at the time we try to insert read watchpoints.
4659
4660                      2.2. Otherwise, the target supports read
4661                      watchpoints, but, the user set an access or write
4662                      watchpoint watching the same memory as this read
4663                      watchpoint.
4664
4665                      If we're watching memory writes as well as reads,
4666                      ignore watchpoint hits when we find that the
4667                      value hasn't changed, as reads don't cause
4668                      changes.  This still gives false positives when
4669                      the program writes the same value to memory as
4670                      what there was already in memory (we will confuse
4671                      it for a read), but it's much better than
4672                      nothing.  */
4673
4674                   int other_write_watchpoint = 0;
4675
4676                   if (bl->watchpoint_type == hw_read)
4677                     {
4678                       struct breakpoint *other_b;
4679
4680                       ALL_BREAKPOINTS (other_b)
4681                         if (other_b->type == bp_hardware_watchpoint
4682                             || other_b->type == bp_access_watchpoint)
4683                           {
4684                             struct watchpoint *other_w =
4685                               (struct watchpoint *) other_b;
4686
4687                             if (other_w->watchpoint_triggered
4688                                 == watch_triggered_yes)
4689                               {
4690                                 other_write_watchpoint = 1;
4691                                 break;
4692                               }
4693                           }
4694                     }
4695
4696                   if (other_write_watchpoint
4697                       || bl->watchpoint_type == hw_access)
4698                     {
4699                       /* We're watching the same memory for writes,
4700                          and the value changed since the last time we
4701                          updated it, so this trap must be for a write.
4702                          Ignore it.  */
4703                       bs->print_it = print_it_noop;
4704                       bs->stop = 0;
4705                     }
4706                 }
4707               break;
4708             case WP_VALUE_NOT_CHANGED:
4709               if (b->base.type == bp_hardware_watchpoint
4710                   || b->base.type == bp_watchpoint)
4711                 {
4712                   /* Don't stop: write watchpoints shouldn't fire if
4713                      the value hasn't changed.  */
4714                   bs->print_it = print_it_noop;
4715                   bs->stop = 0;
4716                 }
4717               /* Stop.  */
4718               break;
4719             default:
4720               /* Can't happen.  */
4721             case 0:
4722               /* Error from catch_errors.  */
4723               printf_filtered (_("Watchpoint %d deleted.\n"), b->base.number);
4724               watchpoint_del_at_next_stop (b);
4725               /* We've already printed what needs to be printed.  */
4726               bs->print_it = print_it_done;
4727               break;
4728             }
4729         }
4730       else      /* must_check_value == 0 */
4731         {
4732           /* This is a case where some watchpoint(s) triggered, but
4733              not at the address of this watchpoint, or else no
4734              watchpoint triggered after all.  So don't print
4735              anything for this watchpoint.  */
4736           bs->print_it = print_it_noop;
4737           bs->stop = 0;
4738         }
4739     }
4740 }
4741
4742
4743 /* Check conditions (condition proper, frame, thread and ignore count)
4744    of breakpoint referred to by BS.  If we should not stop for this
4745    breakpoint, set BS->stop to 0.  */
4746
4747 static void
4748 bpstat_check_breakpoint_conditions (bpstat bs, ptid_t ptid)
4749 {
4750   int thread_id = pid_to_thread_id (ptid);
4751   const struct bp_location *bl;
4752   struct breakpoint *b;
4753
4754   /* BS is built for existing struct breakpoint.  */
4755   bl = bs->bp_location_at;
4756   gdb_assert (bl != NULL);
4757   b = bs->breakpoint_at;
4758   gdb_assert (b != NULL);
4759
4760   /* Even if the target evaluated the condition on its end and notified GDB, we
4761      need to do so again since GDB does not know if we stopped due to a
4762      breakpoint or a single step breakpoint.  */
4763
4764   if (frame_id_p (b->frame_id)
4765       && !frame_id_eq (b->frame_id, get_stack_frame_id (get_current_frame ())))
4766     bs->stop = 0;
4767   else if (bs->stop)
4768     {
4769       int value_is_zero = 0;
4770       struct expression *cond;
4771
4772       /* Evaluate Python breakpoints that have a "stop"
4773          method implemented.  */
4774       if (b->py_bp_object)
4775         bs->stop = gdbpy_should_stop (b->py_bp_object);
4776
4777       if (is_watchpoint (b))
4778         {
4779           struct watchpoint *w = (struct watchpoint *) b;
4780
4781           cond = w->cond_exp;
4782         }
4783       else
4784         cond = bl->cond;
4785
4786       if (cond && b->disposition != disp_del_at_next_stop)
4787         {
4788           int within_current_scope = 1;
4789           struct watchpoint * w;
4790
4791           /* We use value_mark and value_free_to_mark because it could
4792              be a long time before we return to the command level and
4793              call free_all_values.  We can't call free_all_values
4794              because we might be in the middle of evaluating a
4795              function call.  */
4796           struct value *mark = value_mark ();
4797
4798           if (is_watchpoint (b))
4799             w = (struct watchpoint *) b;
4800           else
4801             w = NULL;
4802
4803           /* Need to select the frame, with all that implies so that
4804              the conditions will have the right context.  Because we
4805              use the frame, we will not see an inlined function's
4806              variables when we arrive at a breakpoint at the start
4807              of the inlined function; the current frame will be the
4808              call site.  */
4809           if (w == NULL || w->cond_exp_valid_block == NULL)
4810             select_frame (get_current_frame ());
4811           else
4812             {
4813               struct frame_info *frame;
4814
4815               /* For local watchpoint expressions, which particular
4816                  instance of a local is being watched matters, so we
4817                  keep track of the frame to evaluate the expression
4818                  in.  To evaluate the condition however, it doesn't
4819                  really matter which instantiation of the function
4820                  where the condition makes sense triggers the
4821                  watchpoint.  This allows an expression like "watch
4822                  global if q > 10" set in `func', catch writes to
4823                  global on all threads that call `func', or catch
4824                  writes on all recursive calls of `func' by a single
4825                  thread.  We simply always evaluate the condition in
4826                  the innermost frame that's executing where it makes
4827                  sense to evaluate the condition.  It seems
4828                  intuitive.  */
4829               frame = block_innermost_frame (w->cond_exp_valid_block);
4830               if (frame != NULL)
4831                 select_frame (frame);
4832               else
4833                 within_current_scope = 0;
4834             }
4835           if (within_current_scope)
4836             value_is_zero
4837               = catch_errors (breakpoint_cond_eval, cond,
4838                               "Error in testing breakpoint condition:\n",
4839                               RETURN_MASK_ALL);
4840           else
4841             {
4842               warning (_("Watchpoint condition cannot be tested "
4843                          "in the current scope"));
4844               /* If we failed to set the right context for this
4845                  watchpoint, unconditionally report it.  */
4846               value_is_zero = 0;
4847             }
4848           /* FIXME-someday, should give breakpoint #.  */
4849           value_free_to_mark (mark);
4850         }
4851
4852       if (cond && value_is_zero)
4853         {
4854           bs->stop = 0;
4855         }
4856       else if (b->thread != -1 && b->thread != thread_id)
4857         {
4858           bs->stop = 0;
4859         }
4860       else if (b->ignore_count > 0)
4861         {
4862           b->ignore_count--;
4863           annotate_ignore_count_change ();
4864           bs->stop = 0;
4865           /* Increase the hit count even though we don't stop.  */
4866           ++(b->hit_count);
4867           observer_notify_breakpoint_modified (b);
4868         }       
4869     }
4870 }
4871
4872
4873 /* Get a bpstat associated with having just stopped at address
4874    BP_ADDR in thread PTID.
4875
4876    Determine whether we stopped at a breakpoint, etc, or whether we
4877    don't understand this stop.  Result is a chain of bpstat's such
4878    that:
4879
4880    if we don't understand the stop, the result is a null pointer.
4881
4882    if we understand why we stopped, the result is not null.
4883
4884    Each element of the chain refers to a particular breakpoint or
4885    watchpoint at which we have stopped.  (We may have stopped for
4886    several reasons concurrently.)
4887
4888    Each element of the chain has valid next, breakpoint_at,
4889    commands, FIXME??? fields.  */
4890
4891 bpstat
4892 bpstat_stop_status (struct address_space *aspace,
4893                     CORE_ADDR bp_addr, ptid_t ptid,
4894                     const struct target_waitstatus *ws)
4895 {
4896   struct breakpoint *b = NULL;
4897   struct bp_location *bl;
4898   struct bp_location *loc;
4899   /* First item of allocated bpstat's.  */
4900   bpstat bs_head = NULL, *bs_link = &bs_head;
4901   /* Pointer to the last thing in the chain currently.  */
4902   bpstat bs;
4903   int ix;
4904   int need_remove_insert;
4905   int removed_any;
4906
4907   /* First, build the bpstat chain with locations that explain a
4908      target stop, while being careful to not set the target running,
4909      as that may invalidate locations (in particular watchpoint
4910      locations are recreated).  Resuming will happen here with
4911      breakpoint conditions or watchpoint expressions that include
4912      inferior function calls.  */
4913
4914   ALL_BREAKPOINTS (b)
4915     {
4916       if (!breakpoint_enabled (b) && b->enable_state != bp_permanent)
4917         continue;
4918
4919       for (bl = b->loc; bl != NULL; bl = bl->next)
4920         {
4921           /* For hardware watchpoints, we look only at the first
4922              location.  The watchpoint_check function will work on the
4923              entire expression, not the individual locations.  For
4924              read watchpoints, the watchpoints_triggered function has
4925              checked all locations already.  */
4926           if (b->type == bp_hardware_watchpoint && bl != b->loc)
4927             break;
4928
4929           if (bl->shlib_disabled)
4930             continue;
4931
4932           if (!bpstat_check_location (bl, aspace, bp_addr, ws))
4933             continue;
4934
4935           /* Come here if it's a watchpoint, or if the break address
4936              matches.  */
4937
4938           bs = bpstat_alloc (bl, &bs_link);     /* Alloc a bpstat to
4939                                                    explain stop.  */
4940
4941           /* Assume we stop.  Should we find a watchpoint that is not
4942              actually triggered, or if the condition of the breakpoint
4943              evaluates as false, we'll reset 'stop' to 0.  */
4944           bs->stop = 1;
4945           bs->print = 1;
4946
4947           /* If this is a scope breakpoint, mark the associated
4948              watchpoint as triggered so that we will handle the
4949              out-of-scope event.  We'll get to the watchpoint next
4950              iteration.  */
4951           if (b->type == bp_watchpoint_scope && b->related_breakpoint != b)
4952             {
4953               struct watchpoint *w = (struct watchpoint *) b->related_breakpoint;
4954
4955               w->watchpoint_triggered = watch_triggered_yes;
4956             }
4957         }
4958     }
4959
4960   for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, loc); ++ix)
4961     {
4962       if (breakpoint_location_address_match (loc, aspace, bp_addr))
4963         {
4964           bs = bpstat_alloc (loc, &bs_link);
4965           /* For hits of moribund locations, we should just proceed.  */
4966           bs->stop = 0;
4967           bs->print = 0;
4968           bs->print_it = print_it_noop;
4969         }
4970     }
4971
4972   /* A bit of special processing for shlib breakpoints.  We need to
4973      process solib loading here, so that the lists of loaded and
4974      unloaded libraries are correct before we handle "catch load" and
4975      "catch unload".  */
4976   for (bs = bs_head; bs != NULL; bs = bs->next)
4977     {
4978       if (bs->breakpoint_at && bs->breakpoint_at->type == bp_shlib_event)
4979         {
4980           handle_solib_event ();
4981           break;
4982         }
4983     }
4984
4985   /* Now go through the locations that caused the target to stop, and
4986      check whether we're interested in reporting this stop to higher
4987      layers, or whether we should resume the target transparently.  */
4988
4989   removed_any = 0;
4990
4991   for (bs = bs_head; bs != NULL; bs = bs->next)
4992     {
4993       if (!bs->stop)
4994         continue;
4995
4996       b = bs->breakpoint_at;
4997       b->ops->check_status (bs);
4998       if (bs->stop)
4999         {
5000           bpstat_check_breakpoint_conditions (bs, ptid);
5001
5002           if (bs->stop)
5003             {
5004               ++(b->hit_count);
5005               observer_notify_breakpoint_modified (b);
5006
5007               /* We will stop here.  */
5008               if (b->disposition == disp_disable)
5009                 {
5010                   --(b->enable_count);
5011                   if (b->enable_count <= 0
5012                       && b->enable_state != bp_permanent)
5013                     b->enable_state = bp_disabled;
5014                   removed_any = 1;
5015                 }
5016               if (b->silent)
5017                 bs->print = 0;
5018               bs->commands = b->commands;
5019               incref_counted_command_line (bs->commands);
5020               if (command_line_is_silent (bs->commands
5021                                           ? bs->commands->commands : NULL))
5022                 bs->print = 0;
5023             }
5024
5025         }
5026
5027       /* Print nothing for this entry if we don't stop or don't
5028          print.  */
5029       if (!bs->stop || !bs->print)
5030         bs->print_it = print_it_noop;
5031     }
5032
5033   /* If we aren't stopping, the value of some hardware watchpoint may
5034      not have changed, but the intermediate memory locations we are
5035      watching may have.  Don't bother if we're stopping; this will get
5036      done later.  */
5037   need_remove_insert = 0;
5038   if (! bpstat_causes_stop (bs_head))
5039     for (bs = bs_head; bs != NULL; bs = bs->next)
5040       if (!bs->stop
5041           && bs->breakpoint_at
5042           && is_hardware_watchpoint (bs->breakpoint_at))
5043         {
5044           struct watchpoint *w = (struct watchpoint *) bs->breakpoint_at;
5045
5046           update_watchpoint (w, 0 /* don't reparse.  */);
5047           need_remove_insert = 1;
5048         }
5049
5050   if (need_remove_insert)
5051     update_global_location_list (1);
5052   else if (removed_any)
5053     update_global_location_list (0);
5054
5055   return bs_head;
5056 }
5057
5058 static void
5059 handle_jit_event (void)
5060 {
5061   struct frame_info *frame;
5062   struct gdbarch *gdbarch;
5063
5064   /* Switch terminal for any messages produced by
5065      breakpoint_re_set.  */
5066   target_terminal_ours_for_output ();
5067
5068   frame = get_current_frame ();
5069   gdbarch = get_frame_arch (frame);
5070
5071   jit_event_handler (gdbarch);
5072
5073   target_terminal_inferior ();
5074 }
5075
5076 /* Handle an solib event by calling solib_add.  */
5077
5078 void
5079 handle_solib_event (void)
5080 {
5081   clear_program_space_solib_cache (current_inferior ()->pspace);
5082
5083   /* Check for any newly added shared libraries if we're supposed to
5084      be adding them automatically.  Switch terminal for any messages
5085      produced by breakpoint_re_set.  */
5086   target_terminal_ours_for_output ();
5087 #ifdef SOLIB_ADD
5088   SOLIB_ADD (NULL, 0, &current_target, auto_solib_add);
5089 #else
5090   solib_add (NULL, 0, &current_target, auto_solib_add);
5091 #endif
5092   target_terminal_inferior ();
5093 }
5094
5095 /* Prepare WHAT final decision for infrun.  */
5096
5097 /* Decide what infrun needs to do with this bpstat.  */
5098
5099 struct bpstat_what
5100 bpstat_what (bpstat bs_head)
5101 {
5102   struct bpstat_what retval;
5103   int jit_event = 0;
5104   bpstat bs;
5105
5106   retval.main_action = BPSTAT_WHAT_KEEP_CHECKING;
5107   retval.call_dummy = STOP_NONE;
5108   retval.is_longjmp = 0;
5109
5110   for (bs = bs_head; bs != NULL; bs = bs->next)
5111     {
5112       /* Extract this BS's action.  After processing each BS, we check
5113          if its action overrides all we've seem so far.  */
5114       enum bpstat_what_main_action this_action = BPSTAT_WHAT_KEEP_CHECKING;
5115       enum bptype bptype;
5116
5117       if (bs->breakpoint_at == NULL)
5118         {
5119           /* I suspect this can happen if it was a momentary
5120              breakpoint which has since been deleted.  */
5121           bptype = bp_none;
5122         }
5123       else
5124         bptype = bs->breakpoint_at->type;
5125
5126       switch (bptype)
5127         {
5128         case bp_none:
5129           break;
5130         case bp_breakpoint:
5131         case bp_hardware_breakpoint:
5132         case bp_until:
5133         case bp_finish:
5134         case bp_shlib_event:
5135           if (bs->stop)
5136             {
5137               if (bs->print)
5138                 this_action = BPSTAT_WHAT_STOP_NOISY;
5139               else
5140                 this_action = BPSTAT_WHAT_STOP_SILENT;
5141             }
5142           else
5143             this_action = BPSTAT_WHAT_SINGLE;
5144           break;
5145         case bp_watchpoint:
5146         case bp_hardware_watchpoint:
5147         case bp_read_watchpoint:
5148         case bp_access_watchpoint:
5149           if (bs->stop)
5150             {
5151               if (bs->print)
5152                 this_action = BPSTAT_WHAT_STOP_NOISY;
5153               else
5154                 this_action = BPSTAT_WHAT_STOP_SILENT;
5155             }
5156           else
5157             {
5158               /* There was a watchpoint, but we're not stopping.
5159                  This requires no further action.  */
5160             }
5161           break;
5162         case bp_longjmp:
5163         case bp_longjmp_call_dummy:
5164         case bp_exception:
5165           this_action = BPSTAT_WHAT_SET_LONGJMP_RESUME;
5166           retval.is_longjmp = bptype != bp_exception;
5167           break;
5168         case bp_longjmp_resume:
5169         case bp_exception_resume:
5170           this_action = BPSTAT_WHAT_CLEAR_LONGJMP_RESUME;
5171           retval.is_longjmp = bptype == bp_longjmp_resume;
5172           break;
5173         case bp_step_resume:
5174           if (bs->stop)
5175             this_action = BPSTAT_WHAT_STEP_RESUME;
5176           else
5177             {
5178               /* It is for the wrong frame.  */
5179               this_action = BPSTAT_WHAT_SINGLE;
5180             }
5181           break;
5182         case bp_hp_step_resume:
5183           if (bs->stop)
5184             this_action = BPSTAT_WHAT_HP_STEP_RESUME;
5185           else
5186             {
5187               /* It is for the wrong frame.  */
5188               this_action = BPSTAT_WHAT_SINGLE;
5189             }
5190           break;
5191         case bp_watchpoint_scope:
5192         case bp_thread_event:
5193         case bp_overlay_event:
5194         case bp_longjmp_master:
5195         case bp_std_terminate_master:
5196         case bp_exception_master:
5197           this_action = BPSTAT_WHAT_SINGLE;
5198           break;
5199         case bp_catchpoint:
5200           if (bs->stop)
5201             {
5202               if (bs->print)
5203                 this_action = BPSTAT_WHAT_STOP_NOISY;
5204               else
5205                 this_action = BPSTAT_WHAT_STOP_SILENT;
5206             }
5207           else
5208             {
5209               /* There was a catchpoint, but we're not stopping.
5210                  This requires no further action.  */
5211             }
5212           break;
5213         case bp_jit_event:
5214           jit_event = 1;
5215           this_action = BPSTAT_WHAT_SINGLE;
5216           break;
5217         case bp_call_dummy:
5218           /* Make sure the action is stop (silent or noisy),
5219              so infrun.c pops the dummy frame.  */
5220           retval.call_dummy = STOP_STACK_DUMMY;
5221           this_action = BPSTAT_WHAT_STOP_SILENT;
5222           break;
5223         case bp_std_terminate:
5224           /* Make sure the action is stop (silent or noisy),
5225              so infrun.c pops the dummy frame.  */
5226           retval.call_dummy = STOP_STD_TERMINATE;
5227           this_action = BPSTAT_WHAT_STOP_SILENT;
5228           break;
5229         case bp_tracepoint:
5230         case bp_fast_tracepoint:
5231         case bp_static_tracepoint:
5232           /* Tracepoint hits should not be reported back to GDB, and
5233              if one got through somehow, it should have been filtered
5234              out already.  */
5235           internal_error (__FILE__, __LINE__,
5236                           _("bpstat_what: tracepoint encountered"));
5237           break;
5238         case bp_gnu_ifunc_resolver:
5239           /* Step over it (and insert bp_gnu_ifunc_resolver_return).  */
5240           this_action = BPSTAT_WHAT_SINGLE;
5241           break;
5242         case bp_gnu_ifunc_resolver_return:
5243           /* The breakpoint will be removed, execution will restart from the
5244              PC of the former breakpoint.  */
5245           this_action = BPSTAT_WHAT_KEEP_CHECKING;
5246           break;
5247
5248         case bp_dprintf:
5249           this_action = BPSTAT_WHAT_STOP_SILENT;
5250           break;
5251
5252         default:
5253           internal_error (__FILE__, __LINE__,
5254                           _("bpstat_what: unhandled bptype %d"), (int) bptype);
5255         }
5256
5257       retval.main_action = max (retval.main_action, this_action);
5258     }
5259
5260   /* These operations may affect the bs->breakpoint_at state so they are
5261      delayed after MAIN_ACTION is decided above.  */
5262
5263   if (jit_event)
5264     {
5265       if (debug_infrun)
5266         fprintf_unfiltered (gdb_stdlog, "bpstat_what: bp_jit_event\n");
5267
5268       handle_jit_event ();
5269     }
5270
5271   for (bs = bs_head; bs != NULL; bs = bs->next)
5272     {
5273       struct breakpoint *b = bs->breakpoint_at;
5274
5275       if (b == NULL)
5276         continue;
5277       switch (b->type)
5278         {
5279         case bp_gnu_ifunc_resolver:
5280           gnu_ifunc_resolver_stop (b);
5281           break;
5282         case bp_gnu_ifunc_resolver_return:
5283           gnu_ifunc_resolver_return_stop (b);
5284           break;
5285         }
5286     }
5287
5288   return retval;
5289 }
5290
5291 /* Nonzero if we should step constantly (e.g. watchpoints on machines
5292    without hardware support).  This isn't related to a specific bpstat,
5293    just to things like whether watchpoints are set.  */
5294
5295 int
5296 bpstat_should_step (void)
5297 {
5298   struct breakpoint *b;
5299
5300   ALL_BREAKPOINTS (b)
5301     if (breakpoint_enabled (b) && b->type == bp_watchpoint && b->loc != NULL)
5302       return 1;
5303   return 0;
5304 }
5305
5306 int
5307 bpstat_causes_stop (bpstat bs)
5308 {
5309   for (; bs != NULL; bs = bs->next)
5310     if (bs->stop)
5311       return 1;
5312
5313   return 0;
5314 }
5315
5316 \f
5317
5318 /* Compute a string of spaces suitable to indent the next line
5319    so it starts at the position corresponding to the table column
5320    named COL_NAME in the currently active table of UIOUT.  */
5321
5322 static char *
5323 wrap_indent_at_field (struct ui_out *uiout, const char *col_name)
5324 {
5325   static char wrap_indent[80];
5326   int i, total_width, width, align;
5327   char *text;
5328
5329   total_width = 0;
5330   for (i = 1; ui_out_query_field (uiout, i, &width, &align, &text); i++)
5331     {
5332       if (strcmp (text, col_name) == 0)
5333         {
5334           gdb_assert (total_width < sizeof wrap_indent);
5335           memset (wrap_indent, ' ', total_width);
5336           wrap_indent[total_width] = 0;
5337
5338           return wrap_indent;
5339         }
5340
5341       total_width += width + 1;
5342     }
5343
5344   return NULL;
5345 }
5346
5347 /* Determine if the locations of this breakpoint will have their conditions
5348    evaluated by the target, host or a mix of both.  Returns the following:
5349
5350     "host": Host evals condition.
5351     "host or target": Host or Target evals condition.
5352     "target": Target evals condition.
5353 */
5354
5355 static const char *
5356 bp_condition_evaluator (struct breakpoint *b)
5357 {
5358   struct bp_location *bl;
5359   char host_evals = 0;
5360   char target_evals = 0;
5361
5362   if (!b)
5363     return NULL;
5364
5365   if (!is_breakpoint (b))
5366     return NULL;
5367
5368   if (gdb_evaluates_breakpoint_condition_p ()
5369       || !target_supports_evaluation_of_breakpoint_conditions ())
5370     return condition_evaluation_host;
5371
5372   for (bl = b->loc; bl; bl = bl->next)
5373     {
5374       if (bl->cond_bytecode)
5375         target_evals++;
5376       else
5377         host_evals++;
5378     }
5379
5380   if (host_evals && target_evals)
5381     return condition_evaluation_both;
5382   else if (target_evals)
5383     return condition_evaluation_target;
5384   else
5385     return condition_evaluation_host;
5386 }
5387
5388 /* Determine the breakpoint location's condition evaluator.  This is
5389    similar to bp_condition_evaluator, but for locations.  */
5390
5391 static const char *
5392 bp_location_condition_evaluator (struct bp_location *bl)
5393 {
5394   if (bl && !is_breakpoint (bl->owner))
5395     return NULL;
5396
5397   if (gdb_evaluates_breakpoint_condition_p ()
5398       || !target_supports_evaluation_of_breakpoint_conditions ())
5399     return condition_evaluation_host;
5400
5401   if (bl && bl->cond_bytecode)
5402     return condition_evaluation_target;
5403   else
5404     return condition_evaluation_host;
5405 }
5406
5407 /* Print the LOC location out of the list of B->LOC locations.  */
5408
5409 static void
5410 print_breakpoint_location (struct breakpoint *b,
5411                            struct bp_location *loc)
5412 {
5413   struct ui_out *uiout = current_uiout;
5414   struct cleanup *old_chain = save_current_program_space ();
5415
5416   if (loc != NULL && loc->shlib_disabled)
5417     loc = NULL;
5418
5419   if (loc != NULL)
5420     set_current_program_space (loc->pspace);
5421
5422   if (b->display_canonical)
5423     ui_out_field_string (uiout, "what", b->addr_string);
5424   else if (loc && loc->source_file)
5425     {
5426       struct symbol *sym 
5427         = find_pc_sect_function (loc->address, loc->section);
5428       if (sym)
5429         {
5430           ui_out_text (uiout, "in ");
5431           ui_out_field_string (uiout, "func",
5432                                SYMBOL_PRINT_NAME (sym));
5433           ui_out_text (uiout, " ");
5434           ui_out_wrap_hint (uiout, wrap_indent_at_field (uiout, "what"));
5435           ui_out_text (uiout, "at ");
5436         }
5437       ui_out_field_string (uiout, "file", loc->source_file);
5438       ui_out_text (uiout, ":");
5439       
5440       if (ui_out_is_mi_like_p (uiout))
5441         {
5442           struct symtab_and_line sal = find_pc_line (loc->address, 0);
5443           char *fullname = symtab_to_fullname (sal.symtab);
5444           
5445           if (fullname)
5446             ui_out_field_string (uiout, "fullname", fullname);
5447         }
5448       
5449       ui_out_field_int (uiout, "line", loc->line_number);
5450     }
5451   else if (loc)
5452     {
5453       struct ui_file *stb = mem_fileopen ();
5454       struct cleanup *stb_chain = make_cleanup_ui_file_delete (stb);
5455
5456       print_address_symbolic (loc->gdbarch, loc->address, stb,
5457                               demangle, "");
5458       ui_out_field_stream (uiout, "at", stb);
5459
5460       do_cleanups (stb_chain);
5461     }
5462   else
5463     ui_out_field_string (uiout, "pending", b->addr_string);
5464
5465   if (loc && is_breakpoint (b)
5466       && breakpoint_condition_evaluation_mode () == condition_evaluation_target
5467       && bp_condition_evaluator (b) == condition_evaluation_both)
5468     {
5469       ui_out_text (uiout, " (");
5470       ui_out_field_string (uiout, "evaluated-by",
5471                            bp_location_condition_evaluator (loc));
5472       ui_out_text (uiout, ")");
5473     }
5474
5475   do_cleanups (old_chain);
5476 }
5477
5478 static const char *
5479 bptype_string (enum bptype type)
5480 {
5481   struct ep_type_description
5482     {
5483       enum bptype type;
5484       char *description;
5485     };
5486   static struct ep_type_description bptypes[] =
5487   {
5488     {bp_none, "?deleted?"},
5489     {bp_breakpoint, "breakpoint"},
5490     {bp_hardware_breakpoint, "hw breakpoint"},
5491     {bp_until, "until"},
5492     {bp_finish, "finish"},
5493     {bp_watchpoint, "watchpoint"},
5494     {bp_hardware_watchpoint, "hw watchpoint"},
5495     {bp_read_watchpoint, "read watchpoint"},
5496     {bp_access_watchpoint, "acc watchpoint"},
5497     {bp_longjmp, "longjmp"},
5498     {bp_longjmp_resume, "longjmp resume"},
5499     {bp_longjmp_call_dummy, "longjmp for call dummy"},
5500     {bp_exception, "exception"},
5501     {bp_exception_resume, "exception resume"},
5502     {bp_step_resume, "step resume"},
5503     {bp_hp_step_resume, "high-priority step resume"},
5504     {bp_watchpoint_scope, "watchpoint scope"},
5505     {bp_call_dummy, "call dummy"},
5506     {bp_std_terminate, "std::terminate"},
5507     {bp_shlib_event, "shlib events"},
5508     {bp_thread_event, "thread events"},
5509     {bp_overlay_event, "overlay events"},
5510     {bp_longjmp_master, "longjmp master"},
5511     {bp_std_terminate_master, "std::terminate master"},
5512     {bp_exception_master, "exception master"},
5513     {bp_catchpoint, "catchpoint"},
5514     {bp_tracepoint, "tracepoint"},
5515     {bp_fast_tracepoint, "fast tracepoint"},
5516     {bp_static_tracepoint, "static tracepoint"},
5517     {bp_dprintf, "dprintf"},
5518     {bp_jit_event, "jit events"},
5519     {bp_gnu_ifunc_resolver, "STT_GNU_IFUNC resolver"},
5520     {bp_gnu_ifunc_resolver_return, "STT_GNU_IFUNC resolver return"},
5521   };
5522
5523   if (((int) type >= (sizeof (bptypes) / sizeof (bptypes[0])))
5524       || ((int) type != bptypes[(int) type].type))
5525     internal_error (__FILE__, __LINE__,
5526                     _("bptypes table does not describe type #%d."),
5527                     (int) type);
5528
5529   return bptypes[(int) type].description;
5530 }
5531
5532 /* Print B to gdb_stdout.  */
5533
5534 static void
5535 print_one_breakpoint_location (struct breakpoint *b,
5536                                struct bp_location *loc,
5537                                int loc_number,
5538                                struct bp_location **last_loc,
5539                                int allflag)
5540 {
5541   struct command_line *l;
5542   static char bpenables[] = "nynny";
5543
5544   struct ui_out *uiout = current_uiout;
5545   int header_of_multiple = 0;
5546   int part_of_multiple = (loc != NULL);
5547   struct value_print_options opts;
5548
5549   get_user_print_options (&opts);
5550
5551   gdb_assert (!loc || loc_number != 0);
5552   /* See comment in print_one_breakpoint concerning treatment of
5553      breakpoints with single disabled location.  */
5554   if (loc == NULL 
5555       && (b->loc != NULL 
5556           && (b->loc->next != NULL || !b->loc->enabled)))
5557     header_of_multiple = 1;
5558   if (loc == NULL)
5559     loc = b->loc;
5560
5561   annotate_record ();
5562
5563   /* 1 */
5564   annotate_field (0);
5565   if (part_of_multiple)
5566     {
5567       char *formatted;
5568       formatted = xstrprintf ("%d.%d", b->number, loc_number);
5569       ui_out_field_string (uiout, "number", formatted);
5570       xfree (formatted);
5571     }
5572   else
5573     {
5574       ui_out_field_int (uiout, "number", b->number);
5575     }
5576
5577   /* 2 */
5578   annotate_field (1);
5579   if (part_of_multiple)
5580     ui_out_field_skip (uiout, "type");
5581   else
5582     ui_out_field_string (uiout, "type", bptype_string (b->type));
5583
5584   /* 3 */
5585   annotate_field (2);
5586   if (part_of_multiple)
5587     ui_out_field_skip (uiout, "disp");
5588   else
5589     ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
5590
5591
5592   /* 4 */
5593   annotate_field (3);
5594   if (part_of_multiple)
5595     ui_out_field_string (uiout, "enabled", loc->enabled ? "y" : "n");
5596   else
5597     ui_out_field_fmt (uiout, "enabled", "%c", 
5598                       bpenables[(int) b->enable_state]);
5599   ui_out_spaces (uiout, 2);
5600
5601   
5602   /* 5 and 6 */
5603   if (b->ops != NULL && b->ops->print_one != NULL)
5604     {
5605       /* Although the print_one can possibly print all locations,
5606          calling it here is not likely to get any nice result.  So,
5607          make sure there's just one location.  */
5608       gdb_assert (b->loc == NULL || b->loc->next == NULL);
5609       b->ops->print_one (b, last_loc);
5610     }
5611   else
5612     switch (b->type)
5613       {
5614       case bp_none:
5615         internal_error (__FILE__, __LINE__,
5616                         _("print_one_breakpoint: bp_none encountered\n"));
5617         break;
5618
5619       case bp_watchpoint:
5620       case bp_hardware_watchpoint:
5621       case bp_read_watchpoint:
5622       case bp_access_watchpoint:
5623         {
5624           struct watchpoint *w = (struct watchpoint *) b;
5625
5626           /* Field 4, the address, is omitted (which makes the columns
5627              not line up too nicely with the headers, but the effect
5628              is relatively readable).  */
5629           if (opts.addressprint)
5630             ui_out_field_skip (uiout, "addr");
5631           annotate_field (5);
5632           ui_out_field_string (uiout, "what", w->exp_string);
5633         }
5634         break;
5635
5636       case bp_breakpoint:
5637       case bp_hardware_breakpoint:
5638       case bp_until:
5639       case bp_finish:
5640       case bp_longjmp:
5641       case bp_longjmp_resume:
5642       case bp_longjmp_call_dummy:
5643       case bp_exception:
5644       case bp_exception_resume:
5645       case bp_step_resume:
5646       case bp_hp_step_resume:
5647       case bp_watchpoint_scope:
5648       case bp_call_dummy:
5649       case bp_std_terminate:
5650       case bp_shlib_event:
5651       case bp_thread_event:
5652       case bp_overlay_event:
5653       case bp_longjmp_master:
5654       case bp_std_terminate_master:
5655       case bp_exception_master:
5656       case bp_tracepoint:
5657       case bp_fast_tracepoint:
5658       case bp_static_tracepoint:
5659       case bp_dprintf:
5660       case bp_jit_event:
5661       case bp_gnu_ifunc_resolver:
5662       case bp_gnu_ifunc_resolver_return:
5663         if (opts.addressprint)
5664           {
5665             annotate_field (4);
5666             if (header_of_multiple)
5667               ui_out_field_string (uiout, "addr", "<MULTIPLE>");
5668             else if (b->loc == NULL || loc->shlib_disabled)
5669               ui_out_field_string (uiout, "addr", "<PENDING>");
5670             else
5671               ui_out_field_core_addr (uiout, "addr",
5672                                       loc->gdbarch, loc->address);
5673           }
5674         annotate_field (5);
5675         if (!header_of_multiple)
5676           print_breakpoint_location (b, loc);
5677         if (b->loc)
5678           *last_loc = b->loc;
5679         break;
5680       }
5681
5682
5683   /* For backward compatibility, don't display inferiors unless there
5684      are several.  */
5685   if (loc != NULL
5686       && !header_of_multiple
5687       && (allflag
5688           || (!gdbarch_has_global_breakpoints (target_gdbarch)
5689               && (number_of_program_spaces () > 1
5690                   || number_of_inferiors () > 1)
5691               /* LOC is for existing B, it cannot be in
5692                  moribund_locations and thus having NULL OWNER.  */
5693               && loc->owner->type != bp_catchpoint)))
5694     {
5695       struct inferior *inf;
5696       int first = 1;
5697
5698       for (inf = inferior_list; inf != NULL; inf = inf->next)
5699         {
5700           if (inf->pspace == loc->pspace)
5701             {
5702               if (first)
5703                 {
5704                   first = 0;
5705                   ui_out_text (uiout, " inf ");
5706                 }
5707               else
5708                 ui_out_text (uiout, ", ");
5709               ui_out_text (uiout, plongest (inf->num));
5710             }
5711         }
5712     }
5713
5714   if (!part_of_multiple)
5715     {
5716       if (b->thread != -1)
5717         {
5718           /* FIXME: This seems to be redundant and lost here; see the
5719              "stop only in" line a little further down.  */
5720           ui_out_text (uiout, " thread ");
5721           ui_out_field_int (uiout, "thread", b->thread);
5722         }
5723       else if (b->task != 0)
5724         {
5725           ui_out_text (uiout, " task ");
5726           ui_out_field_int (uiout, "task", b->task);
5727         }
5728     }
5729
5730   ui_out_text (uiout, "\n");
5731
5732   if (!part_of_multiple)
5733     b->ops->print_one_detail (b, uiout);
5734
5735   if (part_of_multiple && frame_id_p (b->frame_id))
5736     {
5737       annotate_field (6);
5738       ui_out_text (uiout, "\tstop only in stack frame at ");
5739       /* FIXME: cagney/2002-12-01: Shouldn't be poking around inside
5740          the frame ID.  */
5741       ui_out_field_core_addr (uiout, "frame",
5742                               b->gdbarch, b->frame_id.stack_addr);
5743       ui_out_text (uiout, "\n");
5744     }
5745   
5746   if (!part_of_multiple && b->cond_string)
5747     {
5748       annotate_field (7);
5749       if (is_tracepoint (b))
5750         ui_out_text (uiout, "\ttrace only if ");
5751       else
5752         ui_out_text (uiout, "\tstop only if ");
5753       ui_out_field_string (uiout, "cond", b->cond_string);
5754
5755       /* Print whether the target is doing the breakpoint's condition
5756          evaluation.  If GDB is doing the evaluation, don't print anything.  */
5757       if (is_breakpoint (b)
5758           && breakpoint_condition_evaluation_mode ()
5759           == condition_evaluation_target)
5760         {
5761           ui_out_text (uiout, " (");
5762           ui_out_field_string (uiout, "evaluated-by",
5763                                bp_condition_evaluator (b));
5764           ui_out_text (uiout, " evals)");
5765         }
5766       ui_out_text (uiout, "\n");
5767     }
5768
5769   if (!part_of_multiple && b->thread != -1)
5770     {
5771       /* FIXME should make an annotation for this.  */
5772       ui_out_text (uiout, "\tstop only in thread ");
5773       ui_out_field_int (uiout, "thread", b->thread);
5774       ui_out_text (uiout, "\n");
5775     }
5776   
5777   if (!part_of_multiple && b->hit_count)
5778     {
5779       /* FIXME should make an annotation for this.  */
5780       if (is_catchpoint (b))
5781         ui_out_text (uiout, "\tcatchpoint");
5782       else if (is_tracepoint (b))
5783         ui_out_text (uiout, "\ttracepoint");
5784       else
5785         ui_out_text (uiout, "\tbreakpoint");
5786       ui_out_text (uiout, " already hit ");
5787       ui_out_field_int (uiout, "times", b->hit_count);
5788       if (b->hit_count == 1)
5789         ui_out_text (uiout, " time\n");
5790       else
5791         ui_out_text (uiout, " times\n");
5792     }
5793   
5794   /* Output the count also if it is zero, but only if this is mi.
5795      FIXME: Should have a better test for this.  */
5796   if (ui_out_is_mi_like_p (uiout))
5797     if (!part_of_multiple && b->hit_count == 0)
5798       ui_out_field_int (uiout, "times", b->hit_count);
5799
5800   if (!part_of_multiple && b->ignore_count)
5801     {
5802       annotate_field (8);
5803       ui_out_text (uiout, "\tignore next ");
5804       ui_out_field_int (uiout, "ignore", b->ignore_count);
5805       ui_out_text (uiout, " hits\n");
5806     }
5807
5808   /* Note that an enable count of 1 corresponds to "enable once"
5809      behavior, which is reported by the combination of enablement and
5810      disposition, so we don't need to mention it here.  */
5811   if (!part_of_multiple && b->enable_count > 1)
5812     {
5813       annotate_field (8);
5814       ui_out_text (uiout, "\tdisable after ");
5815       /* Tweak the wording to clarify that ignore and enable counts
5816          are distinct, and have additive effect.  */
5817       if (b->ignore_count)
5818         ui_out_text (uiout, "additional ");
5819       else
5820         ui_out_text (uiout, "next ");
5821       ui_out_field_int (uiout, "enable", b->enable_count);
5822       ui_out_text (uiout, " hits\n");
5823     }
5824
5825   if (!part_of_multiple && is_tracepoint (b))
5826     {
5827       struct tracepoint *tp = (struct tracepoint *) b;
5828
5829       if (tp->traceframe_usage)
5830         {
5831           ui_out_text (uiout, "\ttrace buffer usage ");
5832           ui_out_field_int (uiout, "traceframe-usage", tp->traceframe_usage);
5833           ui_out_text (uiout, " bytes\n");
5834         }
5835     }
5836   
5837   l = b->commands ? b->commands->commands : NULL;
5838   if (!part_of_multiple && l)
5839     {
5840       struct cleanup *script_chain;
5841
5842       annotate_field (9);
5843       script_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "script");
5844       print_command_lines (uiout, l, 4);
5845       do_cleanups (script_chain);
5846     }
5847
5848   if (is_tracepoint (b))
5849     {
5850       struct tracepoint *t = (struct tracepoint *) b;
5851
5852       if (!part_of_multiple && t->pass_count)
5853         {
5854           annotate_field (10);
5855           ui_out_text (uiout, "\tpass count ");
5856           ui_out_field_int (uiout, "pass", t->pass_count);
5857           ui_out_text (uiout, " \n");
5858         }
5859     }
5860
5861   if (ui_out_is_mi_like_p (uiout) && !part_of_multiple)
5862     {
5863       if (is_watchpoint (b))
5864         {
5865           struct watchpoint *w = (struct watchpoint *) b;
5866
5867           ui_out_field_string (uiout, "original-location", w->exp_string);
5868         }
5869       else if (b->addr_string)
5870         ui_out_field_string (uiout, "original-location", b->addr_string);
5871     }
5872 }
5873
5874 static void
5875 print_one_breakpoint (struct breakpoint *b,
5876                       struct bp_location **last_loc, 
5877                       int allflag)
5878 {
5879   struct cleanup *bkpt_chain;
5880   struct ui_out *uiout = current_uiout;
5881
5882   bkpt_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "bkpt");
5883
5884   print_one_breakpoint_location (b, NULL, 0, last_loc, allflag);
5885   do_cleanups (bkpt_chain);
5886
5887   /* If this breakpoint has custom print function,
5888      it's already printed.  Otherwise, print individual
5889      locations, if any.  */
5890   if (b->ops == NULL || b->ops->print_one == NULL)
5891     {
5892       /* If breakpoint has a single location that is disabled, we
5893          print it as if it had several locations, since otherwise it's
5894          hard to represent "breakpoint enabled, location disabled"
5895          situation.
5896
5897          Note that while hardware watchpoints have several locations
5898          internally, that's not a property exposed to user.  */
5899       if (b->loc 
5900           && !is_hardware_watchpoint (b)
5901           && (b->loc->next || !b->loc->enabled))
5902         {
5903           struct bp_location *loc;
5904           int n = 1;
5905
5906           for (loc = b->loc; loc; loc = loc->next, ++n)
5907             {
5908               struct cleanup *inner2 =
5909                 make_cleanup_ui_out_tuple_begin_end (uiout, NULL);
5910               print_one_breakpoint_location (b, loc, n, last_loc, allflag);
5911               do_cleanups (inner2);
5912             }
5913         }
5914     }
5915 }
5916
5917 static int
5918 breakpoint_address_bits (struct breakpoint *b)
5919 {
5920   int print_address_bits = 0;
5921   struct bp_location *loc;
5922
5923   for (loc = b->loc; loc; loc = loc->next)
5924     {
5925       int addr_bit;
5926
5927       /* Software watchpoints that aren't watching memory don't have
5928          an address to print.  */
5929       if (b->type == bp_watchpoint && loc->watchpoint_type == -1)
5930         continue;
5931
5932       addr_bit = gdbarch_addr_bit (loc->gdbarch);
5933       if (addr_bit > print_address_bits)
5934         print_address_bits = addr_bit;
5935     }
5936
5937   return print_address_bits;
5938 }
5939
5940 struct captured_breakpoint_query_args
5941   {
5942     int bnum;
5943   };
5944
5945 static int
5946 do_captured_breakpoint_query (struct ui_out *uiout, void *data)
5947 {
5948   struct captured_breakpoint_query_args *args = data;
5949   struct breakpoint *b;
5950   struct bp_location *dummy_loc = NULL;
5951
5952   ALL_BREAKPOINTS (b)
5953     {
5954       if (args->bnum == b->number)
5955         {
5956           print_one_breakpoint (b, &dummy_loc, 0);
5957           return GDB_RC_OK;
5958         }
5959     }
5960   return GDB_RC_NONE;
5961 }
5962
5963 enum gdb_rc
5964 gdb_breakpoint_query (struct ui_out *uiout, int bnum, 
5965                       char **error_message)
5966 {
5967   struct captured_breakpoint_query_args args;
5968
5969   args.bnum = bnum;
5970   /* For the moment we don't trust print_one_breakpoint() to not throw
5971      an error.  */
5972   if (catch_exceptions_with_msg (uiout, do_captured_breakpoint_query, &args,
5973                                  error_message, RETURN_MASK_ALL) < 0)
5974     return GDB_RC_FAIL;
5975   else
5976     return GDB_RC_OK;
5977 }
5978
5979 /* Return true if this breakpoint was set by the user, false if it is
5980    internal or momentary.  */
5981
5982 int
5983 user_breakpoint_p (struct breakpoint *b)
5984 {
5985   return b->number > 0;
5986 }
5987
5988 /* Print information on user settable breakpoint (watchpoint, etc)
5989    number BNUM.  If BNUM is -1 print all user-settable breakpoints.
5990    If ALLFLAG is non-zero, include non-user-settable breakpoints.  If
5991    FILTER is non-NULL, call it on each breakpoint and only include the
5992    ones for which it returns non-zero.  Return the total number of
5993    breakpoints listed.  */
5994
5995 static int
5996 breakpoint_1 (char *args, int allflag, 
5997               int (*filter) (const struct breakpoint *))
5998 {
5999   struct breakpoint *b;
6000   struct bp_location *last_loc = NULL;
6001   int nr_printable_breakpoints;
6002   struct cleanup *bkpttbl_chain;
6003   struct value_print_options opts;
6004   int print_address_bits = 0;
6005   int print_type_col_width = 14;
6006   struct ui_out *uiout = current_uiout;
6007
6008   get_user_print_options (&opts);
6009
6010   /* Compute the number of rows in the table, as well as the size
6011      required for address fields.  */
6012   nr_printable_breakpoints = 0;
6013   ALL_BREAKPOINTS (b)
6014     {
6015       /* If we have a filter, only list the breakpoints it accepts.  */
6016       if (filter && !filter (b))
6017         continue;
6018
6019       /* If we have an "args" string, it is a list of breakpoints to 
6020          accept.  Skip the others.  */
6021       if (args != NULL && *args != '\0')
6022         {
6023           if (allflag && parse_and_eval_long (args) != b->number)
6024             continue;
6025           if (!allflag && !number_is_in_list (args, b->number))
6026             continue;
6027         }
6028
6029       if (allflag || user_breakpoint_p (b))
6030         {
6031           int addr_bit, type_len;
6032
6033           addr_bit = breakpoint_address_bits (b);
6034           if (addr_bit > print_address_bits)
6035             print_address_bits = addr_bit;
6036
6037           type_len = strlen (bptype_string (b->type));
6038           if (type_len > print_type_col_width)
6039             print_type_col_width = type_len;
6040
6041           nr_printable_breakpoints++;
6042         }
6043     }
6044
6045   if (opts.addressprint)
6046     bkpttbl_chain 
6047       = make_cleanup_ui_out_table_begin_end (uiout, 6,
6048                                              nr_printable_breakpoints,
6049                                              "BreakpointTable");
6050   else
6051     bkpttbl_chain 
6052       = make_cleanup_ui_out_table_begin_end (uiout, 5,
6053                                              nr_printable_breakpoints,
6054                                              "BreakpointTable");
6055
6056   if (nr_printable_breakpoints > 0)
6057     annotate_breakpoints_headers ();
6058   if (nr_printable_breakpoints > 0)
6059     annotate_field (0);
6060   ui_out_table_header (uiout, 7, ui_left, "number", "Num");     /* 1 */
6061   if (nr_printable_breakpoints > 0)
6062     annotate_field (1);
6063   ui_out_table_header (uiout, print_type_col_width, ui_left,
6064                        "type", "Type");                         /* 2 */
6065   if (nr_printable_breakpoints > 0)
6066     annotate_field (2);
6067   ui_out_table_header (uiout, 4, ui_left, "disp", "Disp");      /* 3 */
6068   if (nr_printable_breakpoints > 0)
6069     annotate_field (3);
6070   ui_out_table_header (uiout, 3, ui_left, "enabled", "Enb");    /* 4 */
6071   if (opts.addressprint)
6072     {
6073       if (nr_printable_breakpoints > 0)
6074         annotate_field (4);
6075       if (print_address_bits <= 32)
6076         ui_out_table_header (uiout, 10, ui_left, 
6077                              "addr", "Address");                /* 5 */
6078       else
6079         ui_out_table_header (uiout, 18, ui_left, 
6080                              "addr", "Address");                /* 5 */
6081     }
6082   if (nr_printable_breakpoints > 0)
6083     annotate_field (5);
6084   ui_out_table_header (uiout, 40, ui_noalign, "what", "What");  /* 6 */
6085   ui_out_table_body (uiout);
6086   if (nr_printable_breakpoints > 0)
6087     annotate_breakpoints_table ();
6088
6089   ALL_BREAKPOINTS (b)
6090     {
6091       QUIT;
6092       /* If we have a filter, only list the breakpoints it accepts.  */
6093       if (filter && !filter (b))
6094         continue;
6095
6096       /* If we have an "args" string, it is a list of breakpoints to 
6097          accept.  Skip the others.  */
6098
6099       if (args != NULL && *args != '\0')
6100         {
6101           if (allflag)  /* maintenance info breakpoint */
6102             {
6103               if (parse_and_eval_long (args) != b->number)
6104                 continue;
6105             }
6106           else          /* all others */
6107             {
6108               if (!number_is_in_list (args, b->number))
6109                 continue;
6110             }
6111         }
6112       /* We only print out user settable breakpoints unless the
6113          allflag is set.  */
6114       if (allflag || user_breakpoint_p (b))
6115         print_one_breakpoint (b, &last_loc, allflag);
6116     }
6117
6118   do_cleanups (bkpttbl_chain);
6119
6120   if (nr_printable_breakpoints == 0)
6121     {
6122       /* If there's a filter, let the caller decide how to report
6123          empty list.  */
6124       if (!filter)
6125         {
6126           if (args == NULL || *args == '\0')
6127             ui_out_message (uiout, 0, "No breakpoints or watchpoints.\n");
6128           else
6129             ui_out_message (uiout, 0, 
6130                             "No breakpoint or watchpoint matching '%s'.\n",
6131                             args);
6132         }
6133     }
6134   else
6135     {
6136       if (last_loc && !server_command)
6137         set_next_address (last_loc->gdbarch, last_loc->address);
6138     }
6139
6140   /* FIXME?  Should this be moved up so that it is only called when
6141      there have been breakpoints? */
6142   annotate_breakpoints_table_end ();
6143
6144   return nr_printable_breakpoints;
6145 }
6146
6147 /* Display the value of default-collect in a way that is generally
6148    compatible with the breakpoint list.  */
6149
6150 static void
6151 default_collect_info (void)
6152 {
6153   struct ui_out *uiout = current_uiout;
6154
6155   /* If it has no value (which is frequently the case), say nothing; a
6156      message like "No default-collect." gets in user's face when it's
6157      not wanted.  */
6158   if (!*default_collect)
6159     return;
6160
6161   /* The following phrase lines up nicely with per-tracepoint collect
6162      actions.  */
6163   ui_out_text (uiout, "default collect ");
6164   ui_out_field_string (uiout, "default-collect", default_collect);
6165   ui_out_text (uiout, " \n");
6166 }
6167   
6168 static void
6169 breakpoints_info (char *args, int from_tty)
6170 {
6171   breakpoint_1 (args, 0, NULL);
6172
6173   default_collect_info ();
6174 }
6175
6176 static void
6177 watchpoints_info (char *args, int from_tty)
6178 {
6179   int num_printed = breakpoint_1 (args, 0, is_watchpoint);
6180   struct ui_out *uiout = current_uiout;
6181
6182   if (num_printed == 0)
6183     {
6184       if (args == NULL || *args == '\0')
6185         ui_out_message (uiout, 0, "No watchpoints.\n");
6186       else
6187         ui_out_message (uiout, 0, "No watchpoint matching '%s'.\n", args);
6188     }
6189 }
6190
6191 static void
6192 maintenance_info_breakpoints (char *args, int from_tty)
6193 {
6194   breakpoint_1 (args, 1, NULL);
6195
6196   default_collect_info ();
6197 }
6198
6199 static int
6200 breakpoint_has_pc (struct breakpoint *b,
6201                    struct program_space *pspace,
6202                    CORE_ADDR pc, struct obj_section *section)
6203 {
6204   struct bp_location *bl = b->loc;
6205
6206   for (; bl; bl = bl->next)
6207     {
6208       if (bl->pspace == pspace
6209           && bl->address == pc
6210           && (!overlay_debugging || bl->section == section))
6211         return 1;         
6212     }
6213   return 0;
6214 }
6215
6216 /* Print a message describing any user-breakpoints set at PC.  This
6217    concerns with logical breakpoints, so we match program spaces, not
6218    address spaces.  */
6219
6220 static void
6221 describe_other_breakpoints (struct gdbarch *gdbarch,
6222                             struct program_space *pspace, CORE_ADDR pc,
6223                             struct obj_section *section, int thread)
6224 {
6225   int others = 0;
6226   struct breakpoint *b;
6227
6228   ALL_BREAKPOINTS (b)
6229     others += (user_breakpoint_p (b)
6230                && breakpoint_has_pc (b, pspace, pc, section));
6231   if (others > 0)
6232     {
6233       if (others == 1)
6234         printf_filtered (_("Note: breakpoint "));
6235       else /* if (others == ???) */
6236         printf_filtered (_("Note: breakpoints "));
6237       ALL_BREAKPOINTS (b)
6238         if (user_breakpoint_p (b) && breakpoint_has_pc (b, pspace, pc, section))
6239           {
6240             others--;
6241             printf_filtered ("%d", b->number);
6242             if (b->thread == -1 && thread != -1)
6243               printf_filtered (" (all threads)");
6244             else if (b->thread != -1)
6245               printf_filtered (" (thread %d)", b->thread);
6246             printf_filtered ("%s%s ",
6247                              ((b->enable_state == bp_disabled
6248                                || b->enable_state == bp_call_disabled)
6249                               ? " (disabled)"
6250                               : b->enable_state == bp_permanent 
6251                               ? " (permanent)"
6252                               : ""),
6253                              (others > 1) ? "," 
6254                              : ((others == 1) ? " and" : ""));
6255           }
6256       printf_filtered (_("also set at pc "));
6257       fputs_filtered (paddress (gdbarch, pc), gdb_stdout);
6258       printf_filtered (".\n");
6259     }
6260 }
6261 \f
6262
6263 /* Return true iff it is meaningful to use the address member of
6264    BPT.  For some breakpoint types, the address member is irrelevant
6265    and it makes no sense to attempt to compare it to other addresses
6266    (or use it for any other purpose either).
6267
6268    More specifically, each of the following breakpoint types will
6269    always have a zero valued address and we don't want to mark
6270    breakpoints of any of these types to be a duplicate of an actual
6271    breakpoint at address zero:
6272
6273       bp_watchpoint
6274       bp_catchpoint
6275
6276 */
6277
6278 static int
6279 breakpoint_address_is_meaningful (struct breakpoint *bpt)
6280 {
6281   enum bptype type = bpt->type;
6282
6283   return (type != bp_watchpoint && type != bp_catchpoint);
6284 }
6285
6286 /* Assuming LOC1 and LOC2's owners are hardware watchpoints, returns
6287    true if LOC1 and LOC2 represent the same watchpoint location.  */
6288
6289 static int
6290 watchpoint_locations_match (struct bp_location *loc1, 
6291                             struct bp_location *loc2)
6292 {
6293   struct watchpoint *w1 = (struct watchpoint *) loc1->owner;
6294   struct watchpoint *w2 = (struct watchpoint *) loc2->owner;
6295
6296   /* Both of them must exist.  */
6297   gdb_assert (w1 != NULL);
6298   gdb_assert (w2 != NULL);
6299
6300   /* If the target can evaluate the condition expression in hardware,
6301      then we we need to insert both watchpoints even if they are at
6302      the same place.  Otherwise the watchpoint will only trigger when
6303      the condition of whichever watchpoint was inserted evaluates to
6304      true, not giving a chance for GDB to check the condition of the
6305      other watchpoint.  */
6306   if ((w1->cond_exp
6307        && target_can_accel_watchpoint_condition (loc1->address, 
6308                                                  loc1->length,
6309                                                  loc1->watchpoint_type,
6310                                                  w1->cond_exp))
6311       || (w2->cond_exp
6312           && target_can_accel_watchpoint_condition (loc2->address, 
6313                                                     loc2->length,
6314                                                     loc2->watchpoint_type,
6315                                                     w2->cond_exp)))
6316     return 0;
6317
6318   /* Note that this checks the owner's type, not the location's.  In
6319      case the target does not support read watchpoints, but does
6320      support access watchpoints, we'll have bp_read_watchpoint
6321      watchpoints with hw_access locations.  Those should be considered
6322      duplicates of hw_read locations.  The hw_read locations will
6323      become hw_access locations later.  */
6324   return (loc1->owner->type == loc2->owner->type
6325           && loc1->pspace->aspace == loc2->pspace->aspace
6326           && loc1->address == loc2->address
6327           && loc1->length == loc2->length);
6328 }
6329
6330 /* Returns true if {ASPACE1,ADDR1} and {ASPACE2,ADDR2} represent the
6331    same breakpoint location.  In most targets, this can only be true
6332    if ASPACE1 matches ASPACE2.  On targets that have global
6333    breakpoints, the address space doesn't really matter.  */
6334
6335 static int
6336 breakpoint_address_match (struct address_space *aspace1, CORE_ADDR addr1,
6337                           struct address_space *aspace2, CORE_ADDR addr2)
6338 {
6339   return ((gdbarch_has_global_breakpoints (target_gdbarch)
6340            || aspace1 == aspace2)
6341           && addr1 == addr2);
6342 }
6343
6344 /* Returns true if {ASPACE2,ADDR2} falls within the range determined by
6345    {ASPACE1,ADDR1,LEN1}.  In most targets, this can only be true if ASPACE1
6346    matches ASPACE2.  On targets that have global breakpoints, the address
6347    space doesn't really matter.  */
6348
6349 static int
6350 breakpoint_address_match_range (struct address_space *aspace1, CORE_ADDR addr1,
6351                                 int len1, struct address_space *aspace2,
6352                                 CORE_ADDR addr2)
6353 {
6354   return ((gdbarch_has_global_breakpoints (target_gdbarch)
6355            || aspace1 == aspace2)
6356           && addr2 >= addr1 && addr2 < addr1 + len1);
6357 }
6358
6359 /* Returns true if {ASPACE,ADDR} matches the breakpoint BL.  BL may be
6360    a ranged breakpoint.  In most targets, a match happens only if ASPACE
6361    matches the breakpoint's address space.  On targets that have global
6362    breakpoints, the address space doesn't really matter.  */
6363
6364 static int
6365 breakpoint_location_address_match (struct bp_location *bl,
6366                                    struct address_space *aspace,
6367                                    CORE_ADDR addr)
6368 {
6369   return (breakpoint_address_match (bl->pspace->aspace, bl->address,
6370                                     aspace, addr)
6371           || (bl->length
6372               && breakpoint_address_match_range (bl->pspace->aspace,
6373                                                  bl->address, bl->length,
6374                                                  aspace, addr)));
6375 }
6376
6377 /* If LOC1 and LOC2's owners are not tracepoints, returns false directly.
6378    Then, if LOC1 and LOC2 represent the same tracepoint location, returns
6379    true, otherwise returns false.  */
6380
6381 static int
6382 tracepoint_locations_match (struct bp_location *loc1,
6383                             struct bp_location *loc2)
6384 {
6385   if (is_tracepoint (loc1->owner) && is_tracepoint (loc2->owner))
6386     /* Since tracepoint locations are never duplicated with others', tracepoint
6387        locations at the same address of different tracepoints are regarded as
6388        different locations.  */
6389     return (loc1->address == loc2->address && loc1->owner == loc2->owner);
6390   else
6391     return 0;
6392 }
6393
6394 /* Assuming LOC1 and LOC2's types' have meaningful target addresses
6395    (breakpoint_address_is_meaningful), returns true if LOC1 and LOC2
6396    represent the same location.  */
6397
6398 static int
6399 breakpoint_locations_match (struct bp_location *loc1, 
6400                             struct bp_location *loc2)
6401 {
6402   int hw_point1, hw_point2;
6403
6404   /* Both of them must not be in moribund_locations.  */
6405   gdb_assert (loc1->owner != NULL);
6406   gdb_assert (loc2->owner != NULL);
6407
6408   hw_point1 = is_hardware_watchpoint (loc1->owner);
6409   hw_point2 = is_hardware_watchpoint (loc2->owner);
6410
6411   if (hw_point1 != hw_point2)
6412     return 0;
6413   else if (hw_point1)
6414     return watchpoint_locations_match (loc1, loc2);
6415   else if (is_tracepoint (loc1->owner) || is_tracepoint (loc2->owner))
6416     return tracepoint_locations_match (loc1, loc2);
6417   else
6418     /* We compare bp_location.length in order to cover ranged breakpoints.  */
6419     return (breakpoint_address_match (loc1->pspace->aspace, loc1->address,
6420                                      loc2->pspace->aspace, loc2->address)
6421             && loc1->length == loc2->length);
6422 }
6423
6424 static void
6425 breakpoint_adjustment_warning (CORE_ADDR from_addr, CORE_ADDR to_addr,
6426                                int bnum, int have_bnum)
6427 {
6428   /* The longest string possibly returned by hex_string_custom
6429      is 50 chars.  These must be at least that big for safety.  */
6430   char astr1[64];
6431   char astr2[64];
6432
6433   strcpy (astr1, hex_string_custom ((unsigned long) from_addr, 8));
6434   strcpy (astr2, hex_string_custom ((unsigned long) to_addr, 8));
6435   if (have_bnum)
6436     warning (_("Breakpoint %d address previously adjusted from %s to %s."),
6437              bnum, astr1, astr2);
6438   else
6439     warning (_("Breakpoint address adjusted from %s to %s."), astr1, astr2);
6440 }
6441
6442 /* Adjust a breakpoint's address to account for architectural
6443    constraints on breakpoint placement.  Return the adjusted address.
6444    Note: Very few targets require this kind of adjustment.  For most
6445    targets, this function is simply the identity function.  */
6446
6447 static CORE_ADDR
6448 adjust_breakpoint_address (struct gdbarch *gdbarch,
6449                            CORE_ADDR bpaddr, enum bptype bptype)
6450 {
6451   if (!gdbarch_adjust_breakpoint_address_p (gdbarch))
6452     {
6453       /* Very few targets need any kind of breakpoint adjustment.  */
6454       return bpaddr;
6455     }
6456   else if (bptype == bp_watchpoint
6457            || bptype == bp_hardware_watchpoint
6458            || bptype == bp_read_watchpoint
6459            || bptype == bp_access_watchpoint
6460            || bptype == bp_catchpoint)
6461     {
6462       /* Watchpoints and the various bp_catch_* eventpoints should not
6463          have their addresses modified.  */
6464       return bpaddr;
6465     }
6466   else
6467     {
6468       CORE_ADDR adjusted_bpaddr;
6469
6470       /* Some targets have architectural constraints on the placement
6471          of breakpoint instructions.  Obtain the adjusted address.  */
6472       adjusted_bpaddr = gdbarch_adjust_breakpoint_address (gdbarch, bpaddr);
6473
6474       /* An adjusted breakpoint address can significantly alter
6475          a user's expectations.  Print a warning if an adjustment
6476          is required.  */
6477       if (adjusted_bpaddr != bpaddr)
6478         breakpoint_adjustment_warning (bpaddr, adjusted_bpaddr, 0, 0);
6479
6480       return adjusted_bpaddr;
6481     }
6482 }
6483
6484 void
6485 init_bp_location (struct bp_location *loc, const struct bp_location_ops *ops,
6486                   struct breakpoint *owner)
6487 {
6488   memset (loc, 0, sizeof (*loc));
6489
6490   gdb_assert (ops != NULL);
6491
6492   loc->ops = ops;
6493   loc->owner = owner;
6494   loc->cond = NULL;
6495   loc->cond_bytecode = NULL;
6496   loc->shlib_disabled = 0;
6497   loc->enabled = 1;
6498
6499   switch (owner->type)
6500     {
6501     case bp_breakpoint:
6502     case bp_until:
6503     case bp_finish:
6504     case bp_longjmp:
6505     case bp_longjmp_resume:
6506     case bp_longjmp_call_dummy:
6507     case bp_exception:
6508     case bp_exception_resume:
6509     case bp_step_resume:
6510     case bp_hp_step_resume:
6511     case bp_watchpoint_scope:
6512     case bp_call_dummy:
6513     case bp_std_terminate:
6514     case bp_shlib_event:
6515     case bp_thread_event:
6516     case bp_overlay_event:
6517     case bp_jit_event:
6518     case bp_longjmp_master:
6519     case bp_std_terminate_master:
6520     case bp_exception_master:
6521     case bp_gnu_ifunc_resolver:
6522     case bp_gnu_ifunc_resolver_return:
6523     case bp_dprintf:
6524       loc->loc_type = bp_loc_software_breakpoint;
6525       mark_breakpoint_location_modified (loc);
6526       break;
6527     case bp_hardware_breakpoint:
6528       loc->loc_type = bp_loc_hardware_breakpoint;
6529       mark_breakpoint_location_modified (loc);
6530       break;
6531     case bp_hardware_watchpoint:
6532     case bp_read_watchpoint:
6533     case bp_access_watchpoint:
6534       loc->loc_type = bp_loc_hardware_watchpoint;
6535       break;
6536     case bp_watchpoint:
6537     case bp_catchpoint:
6538     case bp_tracepoint:
6539     case bp_fast_tracepoint:
6540     case bp_static_tracepoint:
6541       loc->loc_type = bp_loc_other;
6542       break;
6543     default:
6544       internal_error (__FILE__, __LINE__, _("unknown breakpoint type"));
6545     }
6546
6547   loc->refc = 1;
6548 }
6549
6550 /* Allocate a struct bp_location.  */
6551
6552 static struct bp_location *
6553 allocate_bp_location (struct breakpoint *bpt)
6554 {
6555   return bpt->ops->allocate_location (bpt);
6556 }
6557
6558 static void
6559 free_bp_location (struct bp_location *loc)
6560 {
6561   loc->ops->dtor (loc);
6562   xfree (loc);
6563 }
6564
6565 /* Increment reference count.  */
6566
6567 static void
6568 incref_bp_location (struct bp_location *bl)
6569 {
6570   ++bl->refc;
6571 }
6572
6573 /* Decrement reference count.  If the reference count reaches 0,
6574    destroy the bp_location.  Sets *BLP to NULL.  */
6575
6576 static void
6577 decref_bp_location (struct bp_location **blp)
6578 {
6579   gdb_assert ((*blp)->refc > 0);
6580
6581   if (--(*blp)->refc == 0)
6582     free_bp_location (*blp);
6583   *blp = NULL;
6584 }
6585
6586 /* Add breakpoint B at the end of the global breakpoint chain.  */
6587
6588 static void
6589 add_to_breakpoint_chain (struct breakpoint *b)
6590 {
6591   struct breakpoint *b1;
6592
6593   /* Add this breakpoint to the end of the chain so that a list of
6594      breakpoints will come out in order of increasing numbers.  */
6595
6596   b1 = breakpoint_chain;
6597   if (b1 == 0)
6598     breakpoint_chain = b;
6599   else
6600     {
6601       while (b1->next)
6602         b1 = b1->next;
6603       b1->next = b;
6604     }
6605 }
6606
6607 /* Initializes breakpoint B with type BPTYPE and no locations yet.  */
6608
6609 static void
6610 init_raw_breakpoint_without_location (struct breakpoint *b,
6611                                       struct gdbarch *gdbarch,
6612                                       enum bptype bptype,
6613                                       const struct breakpoint_ops *ops)
6614 {
6615   memset (b, 0, sizeof (*b));
6616
6617   gdb_assert (ops != NULL);
6618
6619   b->ops = ops;
6620   b->type = bptype;
6621   b->gdbarch = gdbarch;
6622   b->language = current_language->la_language;
6623   b->input_radix = input_radix;
6624   b->thread = -1;
6625   b->enable_state = bp_enabled;
6626   b->next = 0;
6627   b->silent = 0;
6628   b->ignore_count = 0;
6629   b->commands = NULL;
6630   b->frame_id = null_frame_id;
6631   b->condition_not_parsed = 0;
6632   b->py_bp_object = NULL;
6633   b->related_breakpoint = b;
6634 }
6635
6636 /* Helper to set_raw_breakpoint below.  Creates a breakpoint
6637    that has type BPTYPE and has no locations as yet.  */
6638
6639 static struct breakpoint *
6640 set_raw_breakpoint_without_location (struct gdbarch *gdbarch,
6641                                      enum bptype bptype,
6642                                      const struct breakpoint_ops *ops)
6643 {
6644   struct breakpoint *b = XNEW (struct breakpoint);
6645
6646   init_raw_breakpoint_without_location (b, gdbarch, bptype, ops);
6647   add_to_breakpoint_chain (b);
6648   return b;
6649 }
6650
6651 /* Initialize loc->function_name.  EXPLICIT_LOC says no indirect function
6652    resolutions should be made as the user specified the location explicitly
6653    enough.  */
6654
6655 static void
6656 set_breakpoint_location_function (struct bp_location *loc, int explicit_loc)
6657 {
6658   gdb_assert (loc->owner != NULL);
6659
6660   if (loc->owner->type == bp_breakpoint
6661       || loc->owner->type == bp_hardware_breakpoint
6662       || is_tracepoint (loc->owner))
6663     {
6664       int is_gnu_ifunc;
6665       const char *function_name;
6666       CORE_ADDR func_addr;
6667
6668       find_pc_partial_function_gnu_ifunc (loc->address, &function_name,
6669                                           &func_addr, NULL, &is_gnu_ifunc);
6670
6671       if (is_gnu_ifunc && !explicit_loc)
6672         {
6673           struct breakpoint *b = loc->owner;
6674
6675           gdb_assert (loc->pspace == current_program_space);
6676           if (gnu_ifunc_resolve_name (function_name,
6677                                       &loc->requested_address))
6678             {
6679               /* Recalculate ADDRESS based on new REQUESTED_ADDRESS.  */
6680               loc->address = adjust_breakpoint_address (loc->gdbarch,
6681                                                         loc->requested_address,
6682                                                         b->type);
6683             }
6684           else if (b->type == bp_breakpoint && b->loc == loc
6685                    && loc->next == NULL && b->related_breakpoint == b)
6686             {
6687               /* Create only the whole new breakpoint of this type but do not
6688                  mess more complicated breakpoints with multiple locations.  */
6689               b->type = bp_gnu_ifunc_resolver;
6690               /* Remember the resolver's address for use by the return
6691                  breakpoint.  */
6692               loc->related_address = func_addr;
6693             }
6694         }
6695
6696       if (function_name)
6697         loc->function_name = xstrdup (function_name);
6698     }
6699 }
6700
6701 /* Attempt to determine architecture of location identified by SAL.  */
6702 struct gdbarch *
6703 get_sal_arch (struct symtab_and_line sal)
6704 {
6705   if (sal.section)
6706     return get_objfile_arch (sal.section->objfile);
6707   if (sal.symtab)
6708     return get_objfile_arch (sal.symtab->objfile);
6709
6710   return NULL;
6711 }
6712
6713 /* Low level routine for partially initializing a breakpoint of type
6714    BPTYPE.  The newly created breakpoint's address, section, source
6715    file name, and line number are provided by SAL.
6716
6717    It is expected that the caller will complete the initialization of
6718    the newly created breakpoint struct as well as output any status
6719    information regarding the creation of a new breakpoint.  */
6720
6721 static void
6722 init_raw_breakpoint (struct breakpoint *b, struct gdbarch *gdbarch,
6723                      struct symtab_and_line sal, enum bptype bptype,
6724                      const struct breakpoint_ops *ops)
6725 {
6726   init_raw_breakpoint_without_location (b, gdbarch, bptype, ops);
6727
6728   add_location_to_breakpoint (b, &sal);
6729
6730   if (bptype != bp_catchpoint)
6731     gdb_assert (sal.pspace != NULL);
6732
6733   /* Store the program space that was used to set the breakpoint,
6734      except for ordinary breakpoints, which are independent of the
6735      program space.  */
6736   if (bptype != bp_breakpoint && bptype != bp_hardware_breakpoint)
6737     b->pspace = sal.pspace;
6738
6739   breakpoints_changed ();
6740 }
6741
6742 /* set_raw_breakpoint is a low level routine for allocating and
6743    partially initializing a breakpoint of type BPTYPE.  The newly
6744    created breakpoint's address, section, source file name, and line
6745    number are provided by SAL.  The newly created and partially
6746    initialized breakpoint is added to the breakpoint chain and
6747    is also returned as the value of this function.
6748
6749    It is expected that the caller will complete the initialization of
6750    the newly created breakpoint struct as well as output any status
6751    information regarding the creation of a new breakpoint.  In
6752    particular, set_raw_breakpoint does NOT set the breakpoint
6753    number!  Care should be taken to not allow an error to occur
6754    prior to completing the initialization of the breakpoint.  If this
6755    should happen, a bogus breakpoint will be left on the chain.  */
6756
6757 struct breakpoint *
6758 set_raw_breakpoint (struct gdbarch *gdbarch,
6759                     struct symtab_and_line sal, enum bptype bptype,
6760                     const struct breakpoint_ops *ops)
6761 {
6762   struct breakpoint *b = XNEW (struct breakpoint);
6763
6764   init_raw_breakpoint (b, gdbarch, sal, bptype, ops);
6765   add_to_breakpoint_chain (b);
6766   return b;
6767 }
6768
6769
6770 /* Note that the breakpoint object B describes a permanent breakpoint
6771    instruction, hard-wired into the inferior's code.  */
6772 void
6773 make_breakpoint_permanent (struct breakpoint *b)
6774 {
6775   struct bp_location *bl;
6776
6777   b->enable_state = bp_permanent;
6778
6779   /* By definition, permanent breakpoints are already present in the
6780      code.  Mark all locations as inserted.  For now,
6781      make_breakpoint_permanent is called in just one place, so it's
6782      hard to say if it's reasonable to have permanent breakpoint with
6783      multiple locations or not, but it's easy to implement.  */
6784   for (bl = b->loc; bl; bl = bl->next)
6785     bl->inserted = 1;
6786 }
6787
6788 /* Call this routine when stepping and nexting to enable a breakpoint
6789    if we do a longjmp() or 'throw' in TP.  FRAME is the frame which
6790    initiated the operation.  */
6791
6792 void
6793 set_longjmp_breakpoint (struct thread_info *tp, struct frame_id frame)
6794 {
6795   struct breakpoint *b, *b_tmp;
6796   int thread = tp->num;
6797
6798   /* To avoid having to rescan all objfile symbols at every step,
6799      we maintain a list of continually-inserted but always disabled
6800      longjmp "master" breakpoints.  Here, we simply create momentary
6801      clones of those and enable them for the requested thread.  */
6802   ALL_BREAKPOINTS_SAFE (b, b_tmp)
6803     if (b->pspace == current_program_space
6804         && (b->type == bp_longjmp_master
6805             || b->type == bp_exception_master))
6806       {
6807         enum bptype type = b->type == bp_longjmp_master ? bp_longjmp : bp_exception;
6808         struct breakpoint *clone;
6809
6810         /* longjmp_breakpoint_ops ensures INITIATING_FRAME is cleared again
6811            after their removal.  */
6812         clone = momentary_breakpoint_from_master (b, type,
6813                                                   &longjmp_breakpoint_ops);
6814         clone->thread = thread;
6815       }
6816
6817   tp->initiating_frame = frame;
6818 }
6819
6820 /* Delete all longjmp breakpoints from THREAD.  */
6821 void
6822 delete_longjmp_breakpoint (int thread)
6823 {
6824   struct breakpoint *b, *b_tmp;
6825
6826   ALL_BREAKPOINTS_SAFE (b, b_tmp)
6827     if (b->type == bp_longjmp || b->type == bp_exception)
6828       {
6829         if (b->thread == thread)
6830           delete_breakpoint (b);
6831       }
6832 }
6833
6834 void
6835 delete_longjmp_breakpoint_at_next_stop (int thread)
6836 {
6837   struct breakpoint *b, *b_tmp;
6838
6839   ALL_BREAKPOINTS_SAFE (b, b_tmp)
6840     if (b->type == bp_longjmp || b->type == bp_exception)
6841       {
6842         if (b->thread == thread)
6843           b->disposition = disp_del_at_next_stop;
6844       }
6845 }
6846
6847 /* Place breakpoints of type bp_longjmp_call_dummy to catch longjmp for
6848    INFERIOR_PTID thread.  Chain them all by RELATED_BREAKPOINT and return
6849    pointer to any of them.  Return NULL if this system cannot place longjmp
6850    breakpoints.  */
6851
6852 struct breakpoint *
6853 set_longjmp_breakpoint_for_call_dummy (void)
6854 {
6855   struct breakpoint *b, *retval = NULL;
6856
6857   ALL_BREAKPOINTS (b)
6858     if (b->pspace == current_program_space && b->type == bp_longjmp_master)
6859       {
6860         struct breakpoint *new_b;
6861
6862         new_b = momentary_breakpoint_from_master (b, bp_longjmp_call_dummy,
6863                                                   &momentary_breakpoint_ops);
6864         new_b->thread = pid_to_thread_id (inferior_ptid);
6865
6866         /* Link NEW_B into the chain of RETVAL breakpoints.  */
6867
6868         gdb_assert (new_b->related_breakpoint == new_b);
6869         if (retval == NULL)
6870           retval = new_b;
6871         new_b->related_breakpoint = retval;
6872         while (retval->related_breakpoint != new_b->related_breakpoint)
6873           retval = retval->related_breakpoint;
6874         retval->related_breakpoint = new_b;
6875       }
6876
6877   return retval;
6878 }
6879
6880 /* Verify all existing dummy frames and their associated breakpoints for
6881    THREAD.  Remove those which can no longer be found in the current frame
6882    stack.
6883
6884    You should call this function only at places where it is safe to currently
6885    unwind the whole stack.  Failed stack unwind would discard live dummy
6886    frames.  */
6887
6888 void
6889 check_longjmp_breakpoint_for_call_dummy (int thread)
6890 {
6891   struct breakpoint *b, *b_tmp;
6892
6893   ALL_BREAKPOINTS_SAFE (b, b_tmp)
6894     if (b->type == bp_longjmp_call_dummy && b->thread == thread)
6895       {
6896         struct breakpoint *dummy_b = b->related_breakpoint;
6897
6898         while (dummy_b != b && dummy_b->type != bp_call_dummy)
6899           dummy_b = dummy_b->related_breakpoint;
6900         if (dummy_b->type != bp_call_dummy
6901             || frame_find_by_id (dummy_b->frame_id) != NULL)
6902           continue;
6903         
6904         dummy_frame_discard (dummy_b->frame_id);
6905
6906         while (b->related_breakpoint != b)
6907           {
6908             if (b_tmp == b->related_breakpoint)
6909               b_tmp = b->related_breakpoint->next;
6910             delete_breakpoint (b->related_breakpoint);
6911           }
6912         delete_breakpoint (b);
6913       }
6914 }
6915
6916 void
6917 enable_overlay_breakpoints (void)
6918 {
6919   struct breakpoint *b;
6920
6921   ALL_BREAKPOINTS (b)
6922     if (b->type == bp_overlay_event)
6923     {
6924       b->enable_state = bp_enabled;
6925       update_global_location_list (1);
6926       overlay_events_enabled = 1;
6927     }
6928 }
6929
6930 void
6931 disable_overlay_breakpoints (void)
6932 {
6933   struct breakpoint *b;
6934
6935   ALL_BREAKPOINTS (b)
6936     if (b->type == bp_overlay_event)
6937     {
6938       b->enable_state = bp_disabled;
6939       update_global_location_list (0);
6940       overlay_events_enabled = 0;
6941     }
6942 }
6943
6944 /* Set an active std::terminate breakpoint for each std::terminate
6945    master breakpoint.  */
6946 void
6947 set_std_terminate_breakpoint (void)
6948 {
6949   struct breakpoint *b, *b_tmp;
6950
6951   ALL_BREAKPOINTS_SAFE (b, b_tmp)
6952     if (b->pspace == current_program_space
6953         && b->type == bp_std_terminate_master)
6954       {
6955         momentary_breakpoint_from_master (b, bp_std_terminate,
6956                                           &momentary_breakpoint_ops);
6957       }
6958 }
6959
6960 /* Delete all the std::terminate breakpoints.  */
6961 void
6962 delete_std_terminate_breakpoint (void)
6963 {
6964   struct breakpoint *b, *b_tmp;
6965
6966   ALL_BREAKPOINTS_SAFE (b, b_tmp)
6967     if (b->type == bp_std_terminate)
6968       delete_breakpoint (b);
6969 }
6970
6971 struct breakpoint *
6972 create_thread_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
6973 {
6974   struct breakpoint *b;
6975
6976   b = create_internal_breakpoint (gdbarch, address, bp_thread_event,
6977                                   &internal_breakpoint_ops);
6978
6979   b->enable_state = bp_enabled;
6980   /* addr_string has to be used or breakpoint_re_set will delete me.  */
6981   b->addr_string
6982     = xstrprintf ("*%s", paddress (b->loc->gdbarch, b->loc->address));
6983
6984   update_global_location_list_nothrow (1);
6985
6986   return b;
6987 }
6988
6989 void
6990 remove_thread_event_breakpoints (void)
6991 {
6992   struct breakpoint *b, *b_tmp;
6993
6994   ALL_BREAKPOINTS_SAFE (b, b_tmp)
6995     if (b->type == bp_thread_event
6996         && b->loc->pspace == current_program_space)
6997       delete_breakpoint (b);
6998 }
6999
7000 struct lang_and_radix
7001   {
7002     enum language lang;
7003     int radix;
7004   };
7005
7006 /* Create a breakpoint for JIT code registration and unregistration.  */
7007
7008 struct breakpoint *
7009 create_jit_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7010 {
7011   struct breakpoint *b;
7012
7013   b = create_internal_breakpoint (gdbarch, address, bp_jit_event,
7014                                   &internal_breakpoint_ops);
7015   update_global_location_list_nothrow (1);
7016   return b;
7017 }
7018
7019 /* Remove JIT code registration and unregistration breakpoint(s).  */
7020
7021 void
7022 remove_jit_event_breakpoints (void)
7023 {
7024   struct breakpoint *b, *b_tmp;
7025
7026   ALL_BREAKPOINTS_SAFE (b, b_tmp)
7027     if (b->type == bp_jit_event
7028         && b->loc->pspace == current_program_space)
7029       delete_breakpoint (b);
7030 }
7031
7032 void
7033 remove_solib_event_breakpoints (void)
7034 {
7035   struct breakpoint *b, *b_tmp;
7036
7037   ALL_BREAKPOINTS_SAFE (b, b_tmp)
7038     if (b->type == bp_shlib_event
7039         && b->loc->pspace == current_program_space)
7040       delete_breakpoint (b);
7041 }
7042
7043 struct breakpoint *
7044 create_solib_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7045 {
7046   struct breakpoint *b;
7047
7048   b = create_internal_breakpoint (gdbarch, address, bp_shlib_event,
7049                                   &internal_breakpoint_ops);
7050   update_global_location_list_nothrow (1);
7051   return b;
7052 }
7053
7054 /* Disable any breakpoints that are on code in shared libraries.  Only
7055    apply to enabled breakpoints, disabled ones can just stay disabled.  */
7056
7057 void
7058 disable_breakpoints_in_shlibs (void)
7059 {
7060   struct bp_location *loc, **locp_tmp;
7061
7062   ALL_BP_LOCATIONS (loc, locp_tmp)
7063   {
7064     /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL.  */
7065     struct breakpoint *b = loc->owner;
7066
7067     /* We apply the check to all breakpoints, including disabled for
7068        those with loc->duplicate set.  This is so that when breakpoint
7069        becomes enabled, or the duplicate is removed, gdb will try to
7070        insert all breakpoints.  If we don't set shlib_disabled here,
7071        we'll try to insert those breakpoints and fail.  */
7072     if (((b->type == bp_breakpoint)
7073          || (b->type == bp_jit_event)
7074          || (b->type == bp_hardware_breakpoint)
7075          || (is_tracepoint (b)))
7076         && loc->pspace == current_program_space
7077         && !loc->shlib_disabled
7078 #ifdef PC_SOLIB
7079         && PC_SOLIB (loc->address)
7080 #else
7081         && solib_name_from_address (loc->pspace, loc->address)
7082 #endif
7083         )
7084       {
7085         loc->shlib_disabled = 1;
7086       }
7087   }
7088 }
7089
7090 /* Disable any breakpoints and tracepoints that are in an unloaded shared
7091    library.  Only apply to enabled breakpoints, disabled ones can just stay
7092    disabled.  */
7093
7094 static void
7095 disable_breakpoints_in_unloaded_shlib (struct so_list *solib)
7096 {
7097   struct bp_location *loc, **locp_tmp;
7098   int disabled_shlib_breaks = 0;
7099
7100   /* SunOS a.out shared libraries are always mapped, so do not
7101      disable breakpoints; they will only be reported as unloaded
7102      through clear_solib when GDB discards its shared library
7103      list.  See clear_solib for more information.  */
7104   if (exec_bfd != NULL
7105       && bfd_get_flavour (exec_bfd) == bfd_target_aout_flavour)
7106     return;
7107
7108   ALL_BP_LOCATIONS (loc, locp_tmp)
7109   {
7110     /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL.  */
7111     struct breakpoint *b = loc->owner;
7112
7113     if (solib->pspace == loc->pspace
7114         && !loc->shlib_disabled
7115         && (((b->type == bp_breakpoint
7116               || b->type == bp_jit_event
7117               || b->type == bp_hardware_breakpoint)
7118              && (loc->loc_type == bp_loc_hardware_breakpoint
7119                  || loc->loc_type == bp_loc_software_breakpoint))
7120             || is_tracepoint (b))
7121         && solib_contains_address_p (solib, loc->address))
7122       {
7123         loc->shlib_disabled = 1;
7124         /* At this point, we cannot rely on remove_breakpoint
7125            succeeding so we must mark the breakpoint as not inserted
7126            to prevent future errors occurring in remove_breakpoints.  */
7127         loc->inserted = 0;
7128
7129         /* This may cause duplicate notifications for the same breakpoint.  */
7130         observer_notify_breakpoint_modified (b);
7131
7132         if (!disabled_shlib_breaks)
7133           {
7134             target_terminal_ours_for_output ();
7135             warning (_("Temporarily disabling breakpoints "
7136                        "for unloaded shared library \"%s\""),
7137                      solib->so_name);
7138           }
7139         disabled_shlib_breaks = 1;
7140       }
7141   }
7142 }
7143
7144 /* FORK & VFORK catchpoints.  */
7145
7146 /* An instance of this type is used to represent a fork or vfork
7147    catchpoint.  It includes a "struct breakpoint" as a kind of base
7148    class; users downcast to "struct breakpoint *" when needed.  A
7149    breakpoint is really of this type iff its ops pointer points to
7150    CATCH_FORK_BREAKPOINT_OPS.  */
7151
7152 struct fork_catchpoint
7153 {
7154   /* The base class.  */
7155   struct breakpoint base;
7156
7157   /* Process id of a child process whose forking triggered this
7158      catchpoint.  This field is only valid immediately after this
7159      catchpoint has triggered.  */
7160   ptid_t forked_inferior_pid;
7161 };
7162
7163 /* Implement the "insert" breakpoint_ops method for fork
7164    catchpoints.  */
7165
7166 static int
7167 insert_catch_fork (struct bp_location *bl)
7168 {
7169   return target_insert_fork_catchpoint (PIDGET (inferior_ptid));
7170 }
7171
7172 /* Implement the "remove" breakpoint_ops method for fork
7173    catchpoints.  */
7174
7175 static int
7176 remove_catch_fork (struct bp_location *bl)
7177 {
7178   return target_remove_fork_catchpoint (PIDGET (inferior_ptid));
7179 }
7180
7181 /* Implement the "breakpoint_hit" breakpoint_ops method for fork
7182    catchpoints.  */
7183
7184 static int
7185 breakpoint_hit_catch_fork (const struct bp_location *bl,
7186                            struct address_space *aspace, CORE_ADDR bp_addr,
7187                            const struct target_waitstatus *ws)
7188 {
7189   struct fork_catchpoint *c = (struct fork_catchpoint *) bl->owner;
7190
7191   if (ws->kind != TARGET_WAITKIND_FORKED)
7192     return 0;
7193
7194   c->forked_inferior_pid = ws->value.related_pid;
7195   return 1;
7196 }
7197
7198 /* Implement the "print_it" breakpoint_ops method for fork
7199    catchpoints.  */
7200
7201 static enum print_stop_action
7202 print_it_catch_fork (bpstat bs)
7203 {
7204   struct ui_out *uiout = current_uiout;
7205   struct breakpoint *b = bs->breakpoint_at;
7206   struct fork_catchpoint *c = (struct fork_catchpoint *) bs->breakpoint_at;
7207
7208   annotate_catchpoint (b->number);
7209   if (b->disposition == disp_del)
7210     ui_out_text (uiout, "\nTemporary catchpoint ");
7211   else
7212     ui_out_text (uiout, "\nCatchpoint ");
7213   if (ui_out_is_mi_like_p (uiout))
7214     {
7215       ui_out_field_string (uiout, "reason",
7216                            async_reason_lookup (EXEC_ASYNC_FORK));
7217       ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
7218     }
7219   ui_out_field_int (uiout, "bkptno", b->number);
7220   ui_out_text (uiout, " (forked process ");
7221   ui_out_field_int (uiout, "newpid", ptid_get_pid (c->forked_inferior_pid));
7222   ui_out_text (uiout, "), ");
7223   return PRINT_SRC_AND_LOC;
7224 }
7225
7226 /* Implement the "print_one" breakpoint_ops method for fork
7227    catchpoints.  */
7228
7229 static void
7230 print_one_catch_fork (struct breakpoint *b, struct bp_location **last_loc)
7231 {
7232   struct fork_catchpoint *c = (struct fork_catchpoint *) b;
7233   struct value_print_options opts;
7234   struct ui_out *uiout = current_uiout;
7235
7236   get_user_print_options (&opts);
7237
7238   /* Field 4, the address, is omitted (which makes the columns not
7239      line up too nicely with the headers, but the effect is relatively
7240      readable).  */
7241   if (opts.addressprint)
7242     ui_out_field_skip (uiout, "addr");
7243   annotate_field (5);
7244   ui_out_text (uiout, "fork");
7245   if (!ptid_equal (c->forked_inferior_pid, null_ptid))
7246     {
7247       ui_out_text (uiout, ", process ");
7248       ui_out_field_int (uiout, "what",
7249                         ptid_get_pid (c->forked_inferior_pid));
7250       ui_out_spaces (uiout, 1);
7251     }
7252 }
7253
7254 /* Implement the "print_mention" breakpoint_ops method for fork
7255    catchpoints.  */
7256
7257 static void
7258 print_mention_catch_fork (struct breakpoint *b)
7259 {
7260   printf_filtered (_("Catchpoint %d (fork)"), b->number);
7261 }
7262
7263 /* Implement the "print_recreate" breakpoint_ops method for fork
7264    catchpoints.  */
7265
7266 static void
7267 print_recreate_catch_fork (struct breakpoint *b, struct ui_file *fp)
7268 {
7269   fprintf_unfiltered (fp, "catch fork");
7270   print_recreate_thread (b, fp);
7271 }
7272
7273 /* The breakpoint_ops structure to be used in fork catchpoints.  */
7274
7275 static struct breakpoint_ops catch_fork_breakpoint_ops;
7276
7277 /* Implement the "insert" breakpoint_ops method for vfork
7278    catchpoints.  */
7279
7280 static int
7281 insert_catch_vfork (struct bp_location *bl)
7282 {
7283   return target_insert_vfork_catchpoint (PIDGET (inferior_ptid));
7284 }
7285
7286 /* Implement the "remove" breakpoint_ops method for vfork
7287    catchpoints.  */
7288
7289 static int
7290 remove_catch_vfork (struct bp_location *bl)
7291 {
7292   return target_remove_vfork_catchpoint (PIDGET (inferior_ptid));
7293 }
7294
7295 /* Implement the "breakpoint_hit" breakpoint_ops method for vfork
7296    catchpoints.  */
7297
7298 static int
7299 breakpoint_hit_catch_vfork (const struct bp_location *bl,
7300                             struct address_space *aspace, CORE_ADDR bp_addr,
7301                             const struct target_waitstatus *ws)
7302 {
7303   struct fork_catchpoint *c = (struct fork_catchpoint *) bl->owner;
7304
7305   if (ws->kind != TARGET_WAITKIND_VFORKED)
7306     return 0;
7307
7308   c->forked_inferior_pid = ws->value.related_pid;
7309   return 1;
7310 }
7311
7312 /* Implement the "print_it" breakpoint_ops method for vfork
7313    catchpoints.  */
7314
7315 static enum print_stop_action
7316 print_it_catch_vfork (bpstat bs)
7317 {
7318   struct ui_out *uiout = current_uiout;
7319   struct breakpoint *b = bs->breakpoint_at;
7320   struct fork_catchpoint *c = (struct fork_catchpoint *) b;
7321
7322   annotate_catchpoint (b->number);
7323   if (b->disposition == disp_del)
7324     ui_out_text (uiout, "\nTemporary catchpoint ");
7325   else
7326     ui_out_text (uiout, "\nCatchpoint ");
7327   if (ui_out_is_mi_like_p (uiout))
7328     {
7329       ui_out_field_string (uiout, "reason",
7330                            async_reason_lookup (EXEC_ASYNC_VFORK));
7331       ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
7332     }
7333   ui_out_field_int (uiout, "bkptno", b->number);
7334   ui_out_text (uiout, " (vforked process ");
7335   ui_out_field_int (uiout, "newpid", ptid_get_pid (c->forked_inferior_pid));
7336   ui_out_text (uiout, "), ");
7337   return PRINT_SRC_AND_LOC;
7338 }
7339
7340 /* Implement the "print_one" breakpoint_ops method for vfork
7341    catchpoints.  */
7342
7343 static void
7344 print_one_catch_vfork (struct breakpoint *b, struct bp_location **last_loc)
7345 {
7346   struct fork_catchpoint *c = (struct fork_catchpoint *) b;
7347   struct value_print_options opts;
7348   struct ui_out *uiout = current_uiout;
7349
7350   get_user_print_options (&opts);
7351   /* Field 4, the address, is omitted (which makes the columns not
7352      line up too nicely with the headers, but the effect is relatively
7353      readable).  */
7354   if (opts.addressprint)
7355     ui_out_field_skip (uiout, "addr");
7356   annotate_field (5);
7357   ui_out_text (uiout, "vfork");
7358   if (!ptid_equal (c->forked_inferior_pid, null_ptid))
7359     {
7360       ui_out_text (uiout, ", process ");
7361       ui_out_field_int (uiout, "what",
7362                         ptid_get_pid (c->forked_inferior_pid));
7363       ui_out_spaces (uiout, 1);
7364     }
7365 }
7366
7367 /* Implement the "print_mention" breakpoint_ops method for vfork
7368    catchpoints.  */
7369
7370 static void
7371 print_mention_catch_vfork (struct breakpoint *b)
7372 {
7373   printf_filtered (_("Catchpoint %d (vfork)"), b->number);
7374 }
7375
7376 /* Implement the "print_recreate" breakpoint_ops method for vfork
7377    catchpoints.  */
7378
7379 static void
7380 print_recreate_catch_vfork (struct breakpoint *b, struct ui_file *fp)
7381 {
7382   fprintf_unfiltered (fp, "catch vfork");
7383   print_recreate_thread (b, fp);
7384 }
7385
7386 /* The breakpoint_ops structure to be used in vfork catchpoints.  */
7387
7388 static struct breakpoint_ops catch_vfork_breakpoint_ops;
7389
7390 /* An instance of this type is used to represent an solib catchpoint.
7391    It includes a "struct breakpoint" as a kind of base class; users
7392    downcast to "struct breakpoint *" when needed.  A breakpoint is
7393    really of this type iff its ops pointer points to
7394    CATCH_SOLIB_BREAKPOINT_OPS.  */
7395
7396 struct solib_catchpoint
7397 {
7398   /* The base class.  */
7399   struct breakpoint base;
7400
7401   /* True for "catch load", false for "catch unload".  */
7402   unsigned char is_load;
7403
7404   /* Regular expression to match, if any.  COMPILED is only valid when
7405      REGEX is non-NULL.  */
7406   char *regex;
7407   regex_t compiled;
7408 };
7409
7410 static void
7411 dtor_catch_solib (struct breakpoint *b)
7412 {
7413   struct solib_catchpoint *self = (struct solib_catchpoint *) b;
7414
7415   if (self->regex)
7416     regfree (&self->compiled);
7417   xfree (self->regex);
7418
7419   base_breakpoint_ops.dtor (b);
7420 }
7421
7422 static int
7423 insert_catch_solib (struct bp_location *ignore)
7424 {
7425   return 0;
7426 }
7427
7428 static int
7429 remove_catch_solib (struct bp_location *ignore)
7430 {
7431   return 0;
7432 }
7433
7434 static int
7435 breakpoint_hit_catch_solib (const struct bp_location *bl,
7436                             struct address_space *aspace,
7437                             CORE_ADDR bp_addr,
7438                             const struct target_waitstatus *ws)
7439 {
7440   struct solib_catchpoint *self = (struct solib_catchpoint *) bl->owner;
7441   struct breakpoint *other;
7442
7443   if (ws->kind == TARGET_WAITKIND_LOADED)
7444     return 1;
7445
7446   ALL_BREAKPOINTS (other)
7447   {
7448     struct bp_location *other_bl;
7449
7450     if (other == bl->owner)
7451       continue;
7452
7453     if (other->type != bp_shlib_event)
7454       continue;
7455
7456     if (self->base.pspace != NULL && other->pspace != self->base.pspace)
7457       continue;
7458
7459     for (other_bl = other->loc; other_bl != NULL; other_bl = other_bl->next)
7460       {
7461         if (other->ops->breakpoint_hit (other_bl, aspace, bp_addr, ws))
7462           return 1;
7463       }
7464   }
7465
7466   return 0;
7467 }
7468
7469 static void
7470 check_status_catch_solib (struct bpstats *bs)
7471 {
7472   struct solib_catchpoint *self
7473     = (struct solib_catchpoint *) bs->breakpoint_at;
7474   int ix;
7475
7476   if (self->is_load)
7477     {
7478       struct so_list *iter;
7479
7480       for (ix = 0;
7481            VEC_iterate (so_list_ptr, current_program_space->added_solibs,
7482                         ix, iter);
7483            ++ix)
7484         {
7485           if (!self->regex
7486               || regexec (&self->compiled, iter->so_name, 0, NULL, 0) == 0)
7487             return;
7488         }
7489     }
7490   else
7491     {
7492       char *iter;
7493
7494       for (ix = 0;
7495            VEC_iterate (char_ptr, current_program_space->deleted_solibs,
7496                         ix, iter);
7497            ++ix)
7498         {
7499           if (!self->regex
7500               || regexec (&self->compiled, iter, 0, NULL, 0) == 0)
7501             return;
7502         }
7503     }
7504
7505   bs->stop = 0;
7506   bs->print_it = print_it_noop;
7507 }
7508
7509 static enum print_stop_action
7510 print_it_catch_solib (bpstat bs)
7511 {
7512   struct breakpoint *b = bs->breakpoint_at;
7513   struct ui_out *uiout = current_uiout;
7514
7515   annotate_catchpoint (b->number);
7516   if (b->disposition == disp_del)
7517     ui_out_text (uiout, "\nTemporary catchpoint ");
7518   else
7519     ui_out_text (uiout, "\nCatchpoint ");
7520   ui_out_field_int (uiout, "bkptno", b->number);
7521   ui_out_text (uiout, "\n");
7522   if (ui_out_is_mi_like_p (uiout))
7523     ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
7524   print_solib_event (1);
7525   return PRINT_SRC_AND_LOC;
7526 }
7527
7528 static void
7529 print_one_catch_solib (struct breakpoint *b, struct bp_location **locs)
7530 {
7531   struct solib_catchpoint *self = (struct solib_catchpoint *) b;
7532   struct value_print_options opts;
7533   struct ui_out *uiout = current_uiout;
7534   char *msg;
7535
7536   get_user_print_options (&opts);
7537   /* Field 4, the address, is omitted (which makes the columns not
7538      line up too nicely with the headers, but the effect is relatively
7539      readable).  */
7540   if (opts.addressprint)
7541     {
7542       annotate_field (4);
7543       ui_out_field_skip (uiout, "addr");
7544     }
7545
7546   annotate_field (5);
7547   if (self->is_load)
7548     {
7549       if (self->regex)
7550         msg = xstrprintf (_("load of library matching %s"), self->regex);
7551       else
7552         msg = xstrdup (_("load of library"));
7553     }
7554   else
7555     {
7556       if (self->regex)
7557         msg = xstrprintf (_("unload of library matching %s"), self->regex);
7558       else
7559         msg = xstrdup (_("unload of library"));
7560     }
7561   ui_out_field_string (uiout, "what", msg);
7562   xfree (msg);
7563 }
7564
7565 static void
7566 print_mention_catch_solib (struct breakpoint *b)
7567 {
7568   struct solib_catchpoint *self = (struct solib_catchpoint *) b;
7569
7570   printf_filtered (_("Catchpoint %d (%s)"), b->number,
7571                    self->is_load ? "load" : "unload");
7572 }
7573
7574 static void
7575 print_recreate_catch_solib (struct breakpoint *b, struct ui_file *fp)
7576 {
7577   struct solib_catchpoint *self = (struct solib_catchpoint *) b;
7578
7579   fprintf_unfiltered (fp, "%s %s",
7580                       b->disposition == disp_del ? "tcatch" : "catch",
7581                       self->is_load ? "load" : "unload");
7582   if (self->regex)
7583     fprintf_unfiltered (fp, " %s", self->regex);
7584   fprintf_unfiltered (fp, "\n");
7585 }
7586
7587 static struct breakpoint_ops catch_solib_breakpoint_ops;
7588
7589 /* A helper function that does all the work for "catch load" and
7590    "catch unload".  */
7591
7592 static void
7593 catch_load_or_unload (char *arg, int from_tty, int is_load,
7594                       struct cmd_list_element *command)
7595 {
7596   struct solib_catchpoint *c;
7597   struct gdbarch *gdbarch = get_current_arch ();
7598   int tempflag;
7599   struct cleanup *cleanup;
7600
7601   tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
7602
7603   if (!arg)
7604     arg = "";
7605   arg = skip_spaces (arg);
7606
7607   c = XCNEW (struct solib_catchpoint);
7608   cleanup = make_cleanup (xfree, c);
7609
7610   if (*arg != '\0')
7611     {
7612       int errcode;
7613
7614       errcode = regcomp (&c->compiled, arg, REG_NOSUB);
7615       if (errcode != 0)
7616         {
7617           char *err = get_regcomp_error (errcode, &c->compiled);
7618
7619           make_cleanup (xfree, err);
7620           error (_("Invalid regexp (%s): %s"), err, arg);
7621         }
7622       c->regex = xstrdup (arg);
7623     }
7624
7625   c->is_load = is_load;
7626   init_catchpoint (&c->base, gdbarch, tempflag, NULL,
7627                    &catch_solib_breakpoint_ops);
7628
7629   discard_cleanups (cleanup);
7630   install_breakpoint (0, &c->base, 1);
7631 }
7632
7633 static void
7634 catch_load_command_1 (char *arg, int from_tty,
7635                       struct cmd_list_element *command)
7636 {
7637   catch_load_or_unload (arg, from_tty, 1, command);
7638 }
7639
7640 static void
7641 catch_unload_command_1 (char *arg, int from_tty,
7642                         struct cmd_list_element *command)
7643 {
7644   catch_load_or_unload (arg, from_tty, 0, command);
7645 }
7646
7647 DEF_VEC_I(int);
7648
7649 /* An instance of this type is used to represent a syscall catchpoint.
7650    It includes a "struct breakpoint" as a kind of base class; users
7651    downcast to "struct breakpoint *" when needed.  A breakpoint is
7652    really of this type iff its ops pointer points to
7653    CATCH_SYSCALL_BREAKPOINT_OPS.  */
7654
7655 struct syscall_catchpoint
7656 {
7657   /* The base class.  */
7658   struct breakpoint base;
7659
7660   /* Syscall numbers used for the 'catch syscall' feature.  If no
7661      syscall has been specified for filtering, its value is NULL.
7662      Otherwise, it holds a list of all syscalls to be caught.  The
7663      list elements are allocated with xmalloc.  */
7664   VEC(int) *syscalls_to_be_caught;
7665 };
7666
7667 /* Implement the "dtor" breakpoint_ops method for syscall
7668    catchpoints.  */
7669
7670 static void
7671 dtor_catch_syscall (struct breakpoint *b)
7672 {
7673   struct syscall_catchpoint *c = (struct syscall_catchpoint *) b;
7674
7675   VEC_free (int, c->syscalls_to_be_caught);
7676
7677   base_breakpoint_ops.dtor (b);
7678 }
7679
7680 static const struct inferior_data *catch_syscall_inferior_data = NULL;
7681
7682 struct catch_syscall_inferior_data
7683 {
7684   /* We keep a count of the number of times the user has requested a
7685      particular syscall to be tracked, and pass this information to the
7686      target.  This lets capable targets implement filtering directly.  */
7687
7688   /* Number of times that "any" syscall is requested.  */
7689   int any_syscall_count;
7690
7691   /* Count of each system call.  */
7692   VEC(int) *syscalls_counts;
7693
7694   /* This counts all syscall catch requests, so we can readily determine
7695      if any catching is necessary.  */
7696   int total_syscalls_count;
7697 };
7698
7699 static struct catch_syscall_inferior_data*
7700 get_catch_syscall_inferior_data (struct inferior *inf)
7701 {
7702   struct catch_syscall_inferior_data *inf_data;
7703
7704   inf_data = inferior_data (inf, catch_syscall_inferior_data);
7705   if (inf_data == NULL)
7706     {
7707       inf_data = XZALLOC (struct catch_syscall_inferior_data);
7708       set_inferior_data (inf, catch_syscall_inferior_data, inf_data);
7709     }
7710
7711   return inf_data;
7712 }
7713
7714 static void
7715 catch_syscall_inferior_data_cleanup (struct inferior *inf, void *arg)
7716 {
7717   xfree (arg);
7718 }
7719
7720
7721 /* Implement the "insert" breakpoint_ops method for syscall
7722    catchpoints.  */
7723
7724 static int
7725 insert_catch_syscall (struct bp_location *bl)
7726 {
7727   struct syscall_catchpoint *c = (struct syscall_catchpoint *) bl->owner;
7728   struct inferior *inf = current_inferior ();
7729   struct catch_syscall_inferior_data *inf_data
7730     = get_catch_syscall_inferior_data (inf);
7731
7732   ++inf_data->total_syscalls_count;
7733   if (!c->syscalls_to_be_caught)
7734     ++inf_data->any_syscall_count;
7735   else
7736     {
7737       int i, iter;
7738
7739       for (i = 0;
7740            VEC_iterate (int, c->syscalls_to_be_caught, i, iter);
7741            i++)
7742         {
7743           int elem;
7744
7745           if (iter >= VEC_length (int, inf_data->syscalls_counts))
7746             {
7747               int old_size = VEC_length (int, inf_data->syscalls_counts);
7748               uintptr_t vec_addr_offset
7749                 = old_size * ((uintptr_t) sizeof (int));
7750               uintptr_t vec_addr;
7751               VEC_safe_grow (int, inf_data->syscalls_counts, iter + 1);
7752               vec_addr = ((uintptr_t) VEC_address (int,
7753                                                   inf_data->syscalls_counts)
7754                           + vec_addr_offset);
7755               memset ((void *) vec_addr, 0,
7756                       (iter + 1 - old_size) * sizeof (int));
7757             }
7758           elem = VEC_index (int, inf_data->syscalls_counts, iter);
7759           VEC_replace (int, inf_data->syscalls_counts, iter, ++elem);
7760         }
7761     }
7762
7763   return target_set_syscall_catchpoint (PIDGET (inferior_ptid),
7764                                         inf_data->total_syscalls_count != 0,
7765                                         inf_data->any_syscall_count,
7766                                         VEC_length (int,
7767                                                     inf_data->syscalls_counts),
7768                                         VEC_address (int,
7769                                                      inf_data->syscalls_counts));
7770 }
7771
7772 /* Implement the "remove" breakpoint_ops method for syscall
7773    catchpoints.  */
7774
7775 static int
7776 remove_catch_syscall (struct bp_location *bl)
7777 {
7778   struct syscall_catchpoint *c = (struct syscall_catchpoint *) bl->owner;
7779   struct inferior *inf = current_inferior ();
7780   struct catch_syscall_inferior_data *inf_data
7781     = get_catch_syscall_inferior_data (inf);
7782
7783   --inf_data->total_syscalls_count;
7784   if (!c->syscalls_to_be_caught)
7785     --inf_data->any_syscall_count;
7786   else
7787     {
7788       int i, iter;
7789
7790       for (i = 0;
7791            VEC_iterate (int, c->syscalls_to_be_caught, i, iter);
7792            i++)
7793         {
7794           int elem;
7795           if (iter >= VEC_length (int, inf_data->syscalls_counts))
7796             /* Shouldn't happen.  */
7797             continue;
7798           elem = VEC_index (int, inf_data->syscalls_counts, iter);
7799           VEC_replace (int, inf_data->syscalls_counts, iter, --elem);
7800         }
7801     }
7802
7803   return target_set_syscall_catchpoint (PIDGET (inferior_ptid),
7804                                         inf_data->total_syscalls_count != 0,
7805                                         inf_data->any_syscall_count,
7806                                         VEC_length (int,
7807                                                     inf_data->syscalls_counts),
7808                                         VEC_address (int,
7809                                                      inf_data->syscalls_counts));
7810 }
7811
7812 /* Implement the "breakpoint_hit" breakpoint_ops method for syscall
7813    catchpoints.  */
7814
7815 static int
7816 breakpoint_hit_catch_syscall (const struct bp_location *bl,
7817                               struct address_space *aspace, CORE_ADDR bp_addr,
7818                               const struct target_waitstatus *ws)
7819 {
7820   /* We must check if we are catching specific syscalls in this
7821      breakpoint.  If we are, then we must guarantee that the called
7822      syscall is the same syscall we are catching.  */
7823   int syscall_number = 0;
7824   const struct syscall_catchpoint *c
7825     = (const struct syscall_catchpoint *) bl->owner;
7826
7827   if (ws->kind != TARGET_WAITKIND_SYSCALL_ENTRY
7828       && ws->kind != TARGET_WAITKIND_SYSCALL_RETURN)
7829     return 0;
7830
7831   syscall_number = ws->value.syscall_number;
7832
7833   /* Now, checking if the syscall is the same.  */
7834   if (c->syscalls_to_be_caught)
7835     {
7836       int i, iter;
7837
7838       for (i = 0;
7839            VEC_iterate (int, c->syscalls_to_be_caught, i, iter);
7840            i++)
7841         if (syscall_number == iter)
7842           break;
7843       /* Not the same.  */
7844       if (!iter)
7845         return 0;
7846     }
7847
7848   return 1;
7849 }
7850
7851 /* Implement the "print_it" breakpoint_ops method for syscall
7852    catchpoints.  */
7853
7854 static enum print_stop_action
7855 print_it_catch_syscall (bpstat bs)
7856 {
7857   struct ui_out *uiout = current_uiout;
7858   struct breakpoint *b = bs->breakpoint_at;
7859   /* These are needed because we want to know in which state a
7860      syscall is.  It can be in the TARGET_WAITKIND_SYSCALL_ENTRY
7861      or TARGET_WAITKIND_SYSCALL_RETURN, and depending on it we
7862      must print "called syscall" or "returned from syscall".  */
7863   ptid_t ptid;
7864   struct target_waitstatus last;
7865   struct syscall s;
7866
7867   get_last_target_status (&ptid, &last);
7868
7869   get_syscall_by_number (last.value.syscall_number, &s);
7870
7871   annotate_catchpoint (b->number);
7872
7873   if (b->disposition == disp_del)
7874     ui_out_text (uiout, "\nTemporary catchpoint ");
7875   else
7876     ui_out_text (uiout, "\nCatchpoint ");
7877   if (ui_out_is_mi_like_p (uiout))
7878     {
7879       ui_out_field_string (uiout, "reason",
7880                            async_reason_lookup (last.kind == TARGET_WAITKIND_SYSCALL_ENTRY
7881                                                 ? EXEC_ASYNC_SYSCALL_ENTRY
7882                                                 : EXEC_ASYNC_SYSCALL_RETURN));
7883       ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
7884     }
7885   ui_out_field_int (uiout, "bkptno", b->number);
7886
7887   if (last.kind == TARGET_WAITKIND_SYSCALL_ENTRY)
7888     ui_out_text (uiout, " (call to syscall ");
7889   else
7890     ui_out_text (uiout, " (returned from syscall ");
7891
7892   if (s.name == NULL || ui_out_is_mi_like_p (uiout))
7893     ui_out_field_int (uiout, "syscall-number", last.value.syscall_number);
7894   if (s.name != NULL)
7895     ui_out_field_string (uiout, "syscall-name", s.name);
7896
7897   ui_out_text (uiout, "), ");
7898
7899   return PRINT_SRC_AND_LOC;
7900 }
7901
7902 /* Implement the "print_one" breakpoint_ops method for syscall
7903    catchpoints.  */
7904
7905 static void
7906 print_one_catch_syscall (struct breakpoint *b,
7907                          struct bp_location **last_loc)
7908 {
7909   struct syscall_catchpoint *c = (struct syscall_catchpoint *) b;
7910   struct value_print_options opts;
7911   struct ui_out *uiout = current_uiout;
7912
7913   get_user_print_options (&opts);
7914   /* Field 4, the address, is omitted (which makes the columns not
7915      line up too nicely with the headers, but the effect is relatively
7916      readable).  */
7917   if (opts.addressprint)
7918     ui_out_field_skip (uiout, "addr");
7919   annotate_field (5);
7920
7921   if (c->syscalls_to_be_caught
7922       && VEC_length (int, c->syscalls_to_be_caught) > 1)
7923     ui_out_text (uiout, "syscalls \"");
7924   else
7925     ui_out_text (uiout, "syscall \"");
7926
7927   if (c->syscalls_to_be_caught)
7928     {
7929       int i, iter;
7930       char *text = xstrprintf ("%s", "");
7931
7932       for (i = 0;
7933            VEC_iterate (int, c->syscalls_to_be_caught, i, iter);
7934            i++)
7935         {
7936           char *x = text;
7937           struct syscall s;
7938           get_syscall_by_number (iter, &s);
7939
7940           if (s.name != NULL)
7941             text = xstrprintf ("%s%s, ", text, s.name);
7942           else
7943             text = xstrprintf ("%s%d, ", text, iter);
7944
7945           /* We have to xfree the last 'text' (now stored at 'x')
7946              because xstrprintf dynamically allocates new space for it
7947              on every call.  */
7948           xfree (x);
7949         }
7950       /* Remove the last comma.  */
7951       text[strlen (text) - 2] = '\0';
7952       ui_out_field_string (uiout, "what", text);
7953     }
7954   else
7955     ui_out_field_string (uiout, "what", "<any syscall>");
7956   ui_out_text (uiout, "\" ");
7957 }
7958
7959 /* Implement the "print_mention" breakpoint_ops method for syscall
7960    catchpoints.  */
7961
7962 static void
7963 print_mention_catch_syscall (struct breakpoint *b)
7964 {
7965   struct syscall_catchpoint *c = (struct syscall_catchpoint *) b;
7966
7967   if (c->syscalls_to_be_caught)
7968     {
7969       int i, iter;
7970
7971       if (VEC_length (int, c->syscalls_to_be_caught) > 1)
7972         printf_filtered (_("Catchpoint %d (syscalls"), b->number);
7973       else
7974         printf_filtered (_("Catchpoint %d (syscall"), b->number);
7975
7976       for (i = 0;
7977            VEC_iterate (int, c->syscalls_to_be_caught, i, iter);
7978            i++)
7979         {
7980           struct syscall s;
7981           get_syscall_by_number (iter, &s);
7982
7983           if (s.name)
7984             printf_filtered (" '%s' [%d]", s.name, s.number);
7985           else
7986             printf_filtered (" %d", s.number);
7987         }
7988       printf_filtered (")");
7989     }
7990   else
7991     printf_filtered (_("Catchpoint %d (any syscall)"),
7992                      b->number);
7993 }
7994
7995 /* Implement the "print_recreate" breakpoint_ops method for syscall
7996    catchpoints.  */
7997
7998 static void
7999 print_recreate_catch_syscall (struct breakpoint *b, struct ui_file *fp)
8000 {
8001   struct syscall_catchpoint *c = (struct syscall_catchpoint *) b;
8002
8003   fprintf_unfiltered (fp, "catch syscall");
8004
8005   if (c->syscalls_to_be_caught)
8006     {
8007       int i, iter;
8008
8009       for (i = 0;
8010            VEC_iterate (int, c->syscalls_to_be_caught, i, iter);
8011            i++)
8012         {
8013           struct syscall s;
8014
8015           get_syscall_by_number (iter, &s);
8016           if (s.name)
8017             fprintf_unfiltered (fp, " %s", s.name);
8018           else
8019             fprintf_unfiltered (fp, " %d", s.number);
8020         }
8021     }
8022   print_recreate_thread (b, fp);
8023 }
8024
8025 /* The breakpoint_ops structure to be used in syscall catchpoints.  */
8026
8027 static struct breakpoint_ops catch_syscall_breakpoint_ops;
8028
8029 /* Returns non-zero if 'b' is a syscall catchpoint.  */
8030
8031 static int
8032 syscall_catchpoint_p (struct breakpoint *b)
8033 {
8034   return (b->ops == &catch_syscall_breakpoint_ops);
8035 }
8036
8037 /* Initialize a new breakpoint of the bp_catchpoint kind.  If TEMPFLAG
8038    is non-zero, then make the breakpoint temporary.  If COND_STRING is
8039    not NULL, then store it in the breakpoint.  OPS, if not NULL, is
8040    the breakpoint_ops structure associated to the catchpoint.  */
8041
8042 static void
8043 init_catchpoint (struct breakpoint *b,
8044                  struct gdbarch *gdbarch, int tempflag,
8045                  char *cond_string,
8046                  const struct breakpoint_ops *ops)
8047 {
8048   struct symtab_and_line sal;
8049
8050   init_sal (&sal);
8051   sal.pspace = current_program_space;
8052
8053   init_raw_breakpoint (b, gdbarch, sal, bp_catchpoint, ops);
8054
8055   b->cond_string = (cond_string == NULL) ? NULL : xstrdup (cond_string);
8056   b->disposition = tempflag ? disp_del : disp_donttouch;
8057 }
8058
8059 void
8060 install_breakpoint (int internal, struct breakpoint *b, int update_gll)
8061 {
8062   add_to_breakpoint_chain (b);
8063   set_breakpoint_number (internal, b);
8064   if (!internal)
8065     mention (b);
8066   observer_notify_breakpoint_created (b);
8067
8068   if (update_gll)
8069     update_global_location_list (1);
8070 }
8071
8072 static void
8073 create_fork_vfork_event_catchpoint (struct gdbarch *gdbarch,
8074                                     int tempflag, char *cond_string,
8075                                     const struct breakpoint_ops *ops)
8076 {
8077   struct fork_catchpoint *c = XNEW (struct fork_catchpoint);
8078
8079   init_catchpoint (&c->base, gdbarch, tempflag, cond_string, ops);
8080
8081   c->forked_inferior_pid = null_ptid;
8082
8083   install_breakpoint (0, &c->base, 1);
8084 }
8085
8086 /* Exec catchpoints.  */
8087
8088 /* An instance of this type is used to represent an exec catchpoint.
8089    It includes a "struct breakpoint" as a kind of base class; users
8090    downcast to "struct breakpoint *" when needed.  A breakpoint is
8091    really of this type iff its ops pointer points to
8092    CATCH_EXEC_BREAKPOINT_OPS.  */
8093
8094 struct exec_catchpoint
8095 {
8096   /* The base class.  */
8097   struct breakpoint base;
8098
8099   /* Filename of a program whose exec triggered this catchpoint.
8100      This field is only valid immediately after this catchpoint has
8101      triggered.  */
8102   char *exec_pathname;
8103 };
8104
8105 /* Implement the "dtor" breakpoint_ops method for exec
8106    catchpoints.  */
8107
8108 static void
8109 dtor_catch_exec (struct breakpoint *b)
8110 {
8111   struct exec_catchpoint *c = (struct exec_catchpoint *) b;
8112
8113   xfree (c->exec_pathname);
8114
8115   base_breakpoint_ops.dtor (b);
8116 }
8117
8118 static int
8119 insert_catch_exec (struct bp_location *bl)
8120 {
8121   return target_insert_exec_catchpoint (PIDGET (inferior_ptid));
8122 }
8123
8124 static int
8125 remove_catch_exec (struct bp_location *bl)
8126 {
8127   return target_remove_exec_catchpoint (PIDGET (inferior_ptid));
8128 }
8129
8130 static int
8131 breakpoint_hit_catch_exec (const struct bp_location *bl,
8132                            struct address_space *aspace, CORE_ADDR bp_addr,
8133                            const struct target_waitstatus *ws)
8134 {
8135   struct exec_catchpoint *c = (struct exec_catchpoint *) bl->owner;
8136
8137   if (ws->kind != TARGET_WAITKIND_EXECD)
8138     return 0;
8139
8140   c->exec_pathname = xstrdup (ws->value.execd_pathname);
8141   return 1;
8142 }
8143
8144 static enum print_stop_action
8145 print_it_catch_exec (bpstat bs)
8146 {
8147   struct ui_out *uiout = current_uiout;
8148   struct breakpoint *b = bs->breakpoint_at;
8149   struct exec_catchpoint *c = (struct exec_catchpoint *) b;
8150
8151   annotate_catchpoint (b->number);
8152   if (b->disposition == disp_del)
8153     ui_out_text (uiout, "\nTemporary catchpoint ");
8154   else
8155     ui_out_text (uiout, "\nCatchpoint ");
8156   if (ui_out_is_mi_like_p (uiout))
8157     {
8158       ui_out_field_string (uiout, "reason",
8159                            async_reason_lookup (EXEC_ASYNC_EXEC));
8160       ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
8161     }
8162   ui_out_field_int (uiout, "bkptno", b->number);
8163   ui_out_text (uiout, " (exec'd ");
8164   ui_out_field_string (uiout, "new-exec", c->exec_pathname);
8165   ui_out_text (uiout, "), ");
8166
8167   return PRINT_SRC_AND_LOC;
8168 }
8169
8170 static void
8171 print_one_catch_exec (struct breakpoint *b, struct bp_location **last_loc)
8172 {
8173   struct exec_catchpoint *c = (struct exec_catchpoint *) b;
8174   struct value_print_options opts;
8175   struct ui_out *uiout = current_uiout;
8176
8177   get_user_print_options (&opts);
8178
8179   /* Field 4, the address, is omitted (which makes the columns
8180      not line up too nicely with the headers, but the effect
8181      is relatively readable).  */
8182   if (opts.addressprint)
8183     ui_out_field_skip (uiout, "addr");
8184   annotate_field (5);
8185   ui_out_text (uiout, "exec");
8186   if (c->exec_pathname != NULL)
8187     {
8188       ui_out_text (uiout, ", program \"");
8189       ui_out_field_string (uiout, "what", c->exec_pathname);
8190       ui_out_text (uiout, "\" ");
8191     }
8192 }
8193
8194 static void
8195 print_mention_catch_exec (struct breakpoint *b)
8196 {
8197   printf_filtered (_("Catchpoint %d (exec)"), b->number);
8198 }
8199
8200 /* Implement the "print_recreate" breakpoint_ops method for exec
8201    catchpoints.  */
8202
8203 static void
8204 print_recreate_catch_exec (struct breakpoint *b, struct ui_file *fp)
8205 {
8206   fprintf_unfiltered (fp, "catch exec");
8207   print_recreate_thread (b, fp);
8208 }
8209
8210 static struct breakpoint_ops catch_exec_breakpoint_ops;
8211
8212 static void
8213 create_syscall_event_catchpoint (int tempflag, VEC(int) *filter,
8214                                  const struct breakpoint_ops *ops)
8215 {
8216   struct syscall_catchpoint *c;
8217   struct gdbarch *gdbarch = get_current_arch ();
8218
8219   c = XNEW (struct syscall_catchpoint);
8220   init_catchpoint (&c->base, gdbarch, tempflag, NULL, ops);
8221   c->syscalls_to_be_caught = filter;
8222
8223   install_breakpoint (0, &c->base, 1);
8224 }
8225
8226 static int
8227 hw_breakpoint_used_count (void)
8228 {
8229   int i = 0;
8230   struct breakpoint *b;
8231   struct bp_location *bl;
8232
8233   ALL_BREAKPOINTS (b)
8234   {
8235     if (b->type == bp_hardware_breakpoint && breakpoint_enabled (b))
8236       for (bl = b->loc; bl; bl = bl->next)
8237         {
8238           /* Special types of hardware breakpoints may use more than
8239              one register.  */
8240           i += b->ops->resources_needed (bl);
8241         }
8242   }
8243
8244   return i;
8245 }
8246
8247 /* Returns the resources B would use if it were a hardware
8248    watchpoint.  */
8249
8250 static int
8251 hw_watchpoint_use_count (struct breakpoint *b)
8252 {
8253   int i = 0;
8254   struct bp_location *bl;
8255
8256   if (!breakpoint_enabled (b))
8257     return 0;
8258
8259   for (bl = b->loc; bl; bl = bl->next)
8260     {
8261       /* Special types of hardware watchpoints may use more than
8262          one register.  */
8263       i += b->ops->resources_needed (bl);
8264     }
8265
8266   return i;
8267 }
8268
8269 /* Returns the sum the used resources of all hardware watchpoints of
8270    type TYPE in the breakpoints list.  Also returns in OTHER_TYPE_USED
8271    the sum of the used resources of all hardware watchpoints of other
8272    types _not_ TYPE.  */
8273
8274 static int
8275 hw_watchpoint_used_count_others (struct breakpoint *except,
8276                                  enum bptype type, int *other_type_used)
8277 {
8278   int i = 0;
8279   struct breakpoint *b;
8280
8281   *other_type_used = 0;
8282   ALL_BREAKPOINTS (b)
8283     {
8284       if (b == except)
8285         continue;
8286       if (!breakpoint_enabled (b))
8287         continue;
8288
8289       if (b->type == type)
8290         i += hw_watchpoint_use_count (b);
8291       else if (is_hardware_watchpoint (b))
8292         *other_type_used = 1;
8293     }
8294
8295   return i;
8296 }
8297
8298 void
8299 disable_watchpoints_before_interactive_call_start (void)
8300 {
8301   struct breakpoint *b;
8302
8303   ALL_BREAKPOINTS (b)
8304   {
8305     if (is_watchpoint (b) && breakpoint_enabled (b))
8306       {
8307         b->enable_state = bp_call_disabled;
8308         update_global_location_list (0);
8309       }
8310   }
8311 }
8312
8313 void
8314 enable_watchpoints_after_interactive_call_stop (void)
8315 {
8316   struct breakpoint *b;
8317
8318   ALL_BREAKPOINTS (b)
8319   {
8320     if (is_watchpoint (b) && b->enable_state == bp_call_disabled)
8321       {
8322         b->enable_state = bp_enabled;
8323         update_global_location_list (1);
8324       }
8325   }
8326 }
8327
8328 void
8329 disable_breakpoints_before_startup (void)
8330 {
8331   current_program_space->executing_startup = 1;
8332   update_global_location_list (0);
8333 }
8334
8335 void
8336 enable_breakpoints_after_startup (void)
8337 {
8338   current_program_space->executing_startup = 0;
8339   breakpoint_re_set ();
8340 }
8341
8342
8343 /* Set a breakpoint that will evaporate an end of command
8344    at address specified by SAL.
8345    Restrict it to frame FRAME if FRAME is nonzero.  */
8346
8347 struct breakpoint *
8348 set_momentary_breakpoint (struct gdbarch *gdbarch, struct symtab_and_line sal,
8349                           struct frame_id frame_id, enum bptype type)
8350 {
8351   struct breakpoint *b;
8352
8353   /* If FRAME_ID is valid, it should be a real frame, not an inlined
8354      one.  */
8355   gdb_assert (!frame_id_inlined_p (frame_id));
8356
8357   b = set_raw_breakpoint (gdbarch, sal, type, &momentary_breakpoint_ops);
8358   b->enable_state = bp_enabled;
8359   b->disposition = disp_donttouch;
8360   b->frame_id = frame_id;
8361
8362   /* If we're debugging a multi-threaded program, then we want
8363      momentary breakpoints to be active in only a single thread of
8364      control.  */
8365   if (in_thread_list (inferior_ptid))
8366     b->thread = pid_to_thread_id (inferior_ptid);
8367
8368   update_global_location_list_nothrow (1);
8369
8370   return b;
8371 }
8372
8373 /* Make a momentary breakpoint based on the master breakpoint ORIG.
8374    The new breakpoint will have type TYPE, and use OPS as it
8375    breakpoint_ops.  */
8376
8377 static struct breakpoint *
8378 momentary_breakpoint_from_master (struct breakpoint *orig,
8379                                   enum bptype type,
8380                                   const struct breakpoint_ops *ops)
8381 {
8382   struct breakpoint *copy;
8383
8384   copy = set_raw_breakpoint_without_location (orig->gdbarch, type, ops);
8385   copy->loc = allocate_bp_location (copy);
8386   set_breakpoint_location_function (copy->loc, 1);
8387
8388   copy->loc->gdbarch = orig->loc->gdbarch;
8389   copy->loc->requested_address = orig->loc->requested_address;
8390   copy->loc->address = orig->loc->address;
8391   copy->loc->section = orig->loc->section;
8392   copy->loc->pspace = orig->loc->pspace;
8393   copy->loc->probe = orig->loc->probe;
8394
8395   if (orig->loc->source_file != NULL)
8396     copy->loc->source_file = xstrdup (orig->loc->source_file);
8397
8398   copy->loc->line_number = orig->loc->line_number;
8399   copy->frame_id = orig->frame_id;
8400   copy->thread = orig->thread;
8401   copy->pspace = orig->pspace;
8402
8403   copy->enable_state = bp_enabled;
8404   copy->disposition = disp_donttouch;
8405   copy->number = internal_breakpoint_number--;
8406
8407   update_global_location_list_nothrow (0);
8408   return copy;
8409 }
8410
8411 /* Make a deep copy of momentary breakpoint ORIG.  Returns NULL if
8412    ORIG is NULL.  */
8413
8414 struct breakpoint *
8415 clone_momentary_breakpoint (struct breakpoint *orig)
8416 {
8417   /* If there's nothing to clone, then return nothing.  */
8418   if (orig == NULL)
8419     return NULL;
8420
8421   return momentary_breakpoint_from_master (orig, orig->type, orig->ops);
8422 }
8423
8424 struct breakpoint *
8425 set_momentary_breakpoint_at_pc (struct gdbarch *gdbarch, CORE_ADDR pc,
8426                                 enum bptype type)
8427 {
8428   struct symtab_and_line sal;
8429
8430   sal = find_pc_line (pc, 0);
8431   sal.pc = pc;
8432   sal.section = find_pc_overlay (pc);
8433   sal.explicit_pc = 1;
8434
8435   return set_momentary_breakpoint (gdbarch, sal, null_frame_id, type);
8436 }
8437 \f
8438
8439 /* Tell the user we have just set a breakpoint B.  */
8440
8441 static void
8442 mention (struct breakpoint *b)
8443 {
8444   b->ops->print_mention (b);
8445   if (ui_out_is_mi_like_p (current_uiout))
8446     return;
8447   printf_filtered ("\n");
8448 }
8449 \f
8450
8451 static struct bp_location *
8452 add_location_to_breakpoint (struct breakpoint *b,
8453                             const struct symtab_and_line *sal)
8454 {
8455   struct bp_location *loc, **tmp;
8456   CORE_ADDR adjusted_address;
8457   struct gdbarch *loc_gdbarch = get_sal_arch (*sal);
8458
8459   if (loc_gdbarch == NULL)
8460     loc_gdbarch = b->gdbarch;
8461
8462   /* Adjust the breakpoint's address prior to allocating a location.
8463      Once we call allocate_bp_location(), that mostly uninitialized
8464      location will be placed on the location chain.  Adjustment of the
8465      breakpoint may cause target_read_memory() to be called and we do
8466      not want its scan of the location chain to find a breakpoint and
8467      location that's only been partially initialized.  */
8468   adjusted_address = adjust_breakpoint_address (loc_gdbarch,
8469                                                 sal->pc, b->type);
8470
8471   loc = allocate_bp_location (b);
8472   for (tmp = &(b->loc); *tmp != NULL; tmp = &((*tmp)->next))
8473     ;
8474   *tmp = loc;
8475
8476   loc->requested_address = sal->pc;
8477   loc->address = adjusted_address;
8478   loc->pspace = sal->pspace;
8479   loc->probe = sal->probe;
8480   gdb_assert (loc->pspace != NULL);
8481   loc->section = sal->section;
8482   loc->gdbarch = loc_gdbarch;
8483
8484   if (sal->symtab != NULL)
8485     loc->source_file = xstrdup (sal->symtab->filename);
8486   loc->line_number = sal->line;
8487
8488   set_breakpoint_location_function (loc,
8489                                     sal->explicit_pc || sal->explicit_line);
8490   return loc;
8491 }
8492 \f
8493
8494 /* Return 1 if LOC is pointing to a permanent breakpoint, 
8495    return 0 otherwise.  */
8496
8497 static int
8498 bp_loc_is_permanent (struct bp_location *loc)
8499 {
8500   int len;
8501   CORE_ADDR addr;
8502   const gdb_byte *bpoint;
8503   gdb_byte *target_mem;
8504   struct cleanup *cleanup;
8505   int retval = 0;
8506
8507   gdb_assert (loc != NULL);
8508
8509   addr = loc->address;
8510   bpoint = gdbarch_breakpoint_from_pc (loc->gdbarch, &addr, &len);
8511
8512   /* Software breakpoints unsupported?  */
8513   if (bpoint == NULL)
8514     return 0;
8515
8516   target_mem = alloca (len);
8517
8518   /* Enable the automatic memory restoration from breakpoints while
8519      we read the memory.  Otherwise we could say about our temporary
8520      breakpoints they are permanent.  */
8521   cleanup = save_current_space_and_thread ();
8522
8523   switch_to_program_space_and_thread (loc->pspace);
8524   make_show_memory_breakpoints_cleanup (0);
8525
8526   if (target_read_memory (loc->address, target_mem, len) == 0
8527       && memcmp (target_mem, bpoint, len) == 0)
8528     retval = 1;
8529
8530   do_cleanups (cleanup);
8531
8532   return retval;
8533 }
8534
8535 /* The style in which to perform a dynamic printf.  This is a user
8536    option because different output options have different tradeoffs;
8537    if GDB does the printing, there is better error handling if there
8538    is a problem with any of the arguments, but using an inferior
8539    function lets you have special-purpose printers and sending of
8540    output to the same place as compiled-in print functions.  (Future
8541    styles may include the ability to do a target-side printf.)  */
8542
8543 static const char dprintf_style_gdb[] = "gdb";
8544 static const char dprintf_style_call[] = "call";
8545 static const char *const dprintf_style_enums[] = {
8546   dprintf_style_gdb,
8547   dprintf_style_call,
8548   NULL
8549 };
8550 static const char *dprintf_style = dprintf_style_gdb;
8551
8552 /* The function to use for dynamic printf if the preferred style is to
8553    call into the inferior.  The value is simply a string that is
8554    copied into the command, so it can be anything that GDB can
8555    evaluate to a callable address, not necessarily a function name.  */
8556
8557 static char *dprintf_function = "";
8558
8559 /* The channel to use for dynamic printf if the preferred style is to
8560    call into the inferior; if a nonempty string, it will be passed to
8561    the call as the first argument, with the format string as the
8562    second.  As with the dprintf function, this can be anything that
8563    GDB knows how to evaluate, so in addition to common choices like
8564    "stderr", this could be an app-specific expression like
8565    "mystreams[curlogger]".  */
8566
8567 static char *dprintf_channel = "";
8568
8569 /* Build a command list for the dprintf corresponding to the current
8570    settings of the dprintf style options.  */
8571
8572 static void
8573 update_dprintf_command_list (struct breakpoint *b)
8574 {
8575   char *dprintf_args = b->extra_string;
8576   char *printf_line = NULL;
8577
8578   if (!dprintf_args)
8579     return;
8580
8581   dprintf_args = skip_spaces (dprintf_args);
8582
8583   /* Allow a comma, as it may have terminated a location, but don't
8584      insist on it.  */
8585   if (*dprintf_args == ',')
8586     ++dprintf_args;
8587   dprintf_args = skip_spaces (dprintf_args);
8588
8589   if (*dprintf_args != '"')
8590     error (_("Bad format string, missing '\"'."));
8591
8592   if (strcmp (dprintf_style, "gdb") == 0)
8593     printf_line = xstrprintf ("printf %s", dprintf_args);
8594   else if (strcmp (dprintf_style, "call") == 0)
8595     {
8596       if (!dprintf_function)
8597         error (_("No function supplied for dprintf call"));
8598
8599       if (dprintf_channel && strlen (dprintf_channel) > 0)
8600         printf_line = xstrprintf ("call (void) %s (%s,%s)",
8601                                   dprintf_function,
8602                                   dprintf_channel,
8603                                   dprintf_args);
8604       else
8605         printf_line = xstrprintf ("call (void) %s (%s)",
8606                                   dprintf_function,
8607                                   dprintf_args);
8608     }
8609   else
8610     internal_error (__FILE__, __LINE__,
8611                     _("Invalid dprintf style."));
8612
8613   /* Manufacture a printf/continue sequence.  */
8614   if (printf_line)
8615     {
8616       struct command_line *printf_cmd_line, *cont_cmd_line = NULL;
8617
8618       cont_cmd_line = xmalloc (sizeof (struct command_line));
8619       cont_cmd_line->control_type = simple_control;
8620       cont_cmd_line->body_count = 0;
8621       cont_cmd_line->body_list = NULL;
8622       cont_cmd_line->next = NULL;
8623       cont_cmd_line->line = xstrdup ("continue");
8624
8625       printf_cmd_line = xmalloc (sizeof (struct command_line));
8626       printf_cmd_line->control_type = simple_control;
8627       printf_cmd_line->body_count = 0;
8628       printf_cmd_line->body_list = NULL;
8629       printf_cmd_line->next = cont_cmd_line;
8630       printf_cmd_line->line = printf_line;
8631
8632       breakpoint_set_commands (b, printf_cmd_line);
8633     }
8634 }
8635
8636 /* Update all dprintf commands, making their command lists reflect
8637    current style settings.  */
8638
8639 static void
8640 update_dprintf_commands (char *args, int from_tty,
8641                          struct cmd_list_element *c)
8642 {
8643   struct breakpoint *b;
8644
8645   ALL_BREAKPOINTS (b)
8646     {
8647       if (b->type == bp_dprintf)
8648         update_dprintf_command_list (b);
8649     }
8650 }
8651
8652 /* Create a breakpoint with SAL as location.  Use ADDR_STRING
8653    as textual description of the location, and COND_STRING
8654    as condition expression.  */
8655
8656 static void
8657 init_breakpoint_sal (struct breakpoint *b, struct gdbarch *gdbarch,
8658                      struct symtabs_and_lines sals, char *addr_string,
8659                      char *filter, char *cond_string,
8660                      char *extra_string,
8661                      enum bptype type, enum bpdisp disposition,
8662                      int thread, int task, int ignore_count,
8663                      const struct breakpoint_ops *ops, int from_tty,
8664                      int enabled, int internal, unsigned flags,
8665                      int display_canonical)
8666 {
8667   int i;
8668
8669   if (type == bp_hardware_breakpoint)
8670     {
8671       int target_resources_ok;
8672
8673       i = hw_breakpoint_used_count ();
8674       target_resources_ok =
8675         target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
8676                                             i + 1, 0);
8677       if (target_resources_ok == 0)
8678         error (_("No hardware breakpoint support in the target."));
8679       else if (target_resources_ok < 0)
8680         error (_("Hardware breakpoints used exceeds limit."));
8681     }
8682
8683   gdb_assert (sals.nelts > 0);
8684
8685   for (i = 0; i < sals.nelts; ++i)
8686     {
8687       struct symtab_and_line sal = sals.sals[i];
8688       struct bp_location *loc;
8689
8690       if (from_tty)
8691         {
8692           struct gdbarch *loc_gdbarch = get_sal_arch (sal);
8693           if (!loc_gdbarch)
8694             loc_gdbarch = gdbarch;
8695
8696           describe_other_breakpoints (loc_gdbarch,
8697                                       sal.pspace, sal.pc, sal.section, thread);
8698         }
8699
8700       if (i == 0)
8701         {
8702           init_raw_breakpoint (b, gdbarch, sal, type, ops);
8703           b->thread = thread;
8704           b->task = task;
8705
8706           b->cond_string = cond_string;
8707           b->extra_string = extra_string;
8708           b->ignore_count = ignore_count;
8709           b->enable_state = enabled ? bp_enabled : bp_disabled;
8710           b->disposition = disposition;
8711
8712           if ((flags & CREATE_BREAKPOINT_FLAGS_INSERTED) != 0)
8713             b->loc->inserted = 1;
8714
8715           if (type == bp_static_tracepoint)
8716             {
8717               struct tracepoint *t = (struct tracepoint *) b;
8718               struct static_tracepoint_marker marker;
8719
8720               if (strace_marker_p (b))
8721                 {
8722                   /* We already know the marker exists, otherwise, we
8723                      wouldn't see a sal for it.  */
8724                   char *p = &addr_string[3];
8725                   char *endp;
8726                   char *marker_str;
8727
8728                   p = skip_spaces (p);
8729
8730                   endp = skip_to_space (p);
8731
8732                   marker_str = savestring (p, endp - p);
8733                   t->static_trace_marker_id = marker_str;
8734
8735                   printf_filtered (_("Probed static tracepoint "
8736                                      "marker \"%s\"\n"),
8737                                    t->static_trace_marker_id);
8738                 }
8739               else if (target_static_tracepoint_marker_at (sal.pc, &marker))
8740                 {
8741                   t->static_trace_marker_id = xstrdup (marker.str_id);
8742                   release_static_tracepoint_marker (&marker);
8743
8744                   printf_filtered (_("Probed static tracepoint "
8745                                      "marker \"%s\"\n"),
8746                                    t->static_trace_marker_id);
8747                 }
8748               else
8749                 warning (_("Couldn't determine the static "
8750                            "tracepoint marker to probe"));
8751             }
8752
8753           loc = b->loc;
8754         }
8755       else
8756         {
8757           loc = add_location_to_breakpoint (b, &sal);
8758           if ((flags & CREATE_BREAKPOINT_FLAGS_INSERTED) != 0)
8759             loc->inserted = 1;
8760         }
8761
8762       if (bp_loc_is_permanent (loc))
8763         make_breakpoint_permanent (b);
8764
8765       if (b->cond_string)
8766         {
8767           char *arg = b->cond_string;
8768           loc->cond = parse_exp_1 (&arg, block_for_pc (loc->address), 0);
8769           if (*arg)
8770               error (_("Garbage '%s' follows condition"), arg);
8771         }
8772
8773       /* Dynamic printf requires and uses additional arguments on the
8774          command line, otherwise it's an error.  */
8775       if (type == bp_dprintf)
8776         {
8777           if (b->extra_string)
8778             update_dprintf_command_list (b);
8779           else
8780             error (_("Format string required"));
8781         }
8782       else if (b->extra_string)
8783         error (_("Garbage '%s' at end of command"), b->extra_string);
8784     }
8785
8786   b->display_canonical = display_canonical;
8787   if (addr_string)
8788     b->addr_string = addr_string;
8789   else
8790     /* addr_string has to be used or breakpoint_re_set will delete
8791        me.  */
8792     b->addr_string
8793       = xstrprintf ("*%s", paddress (b->loc->gdbarch, b->loc->address));
8794   b->filter = filter;
8795 }
8796
8797 static void
8798 create_breakpoint_sal (struct gdbarch *gdbarch,
8799                        struct symtabs_and_lines sals, char *addr_string,
8800                        char *filter, char *cond_string,
8801                        char *extra_string,
8802                        enum bptype type, enum bpdisp disposition,
8803                        int thread, int task, int ignore_count,
8804                        const struct breakpoint_ops *ops, int from_tty,
8805                        int enabled, int internal, unsigned flags,
8806                        int display_canonical)
8807 {
8808   struct breakpoint *b;
8809   struct cleanup *old_chain;
8810
8811   if (is_tracepoint_type (type))
8812     {
8813       struct tracepoint *t;
8814
8815       t = XCNEW (struct tracepoint);
8816       b = &t->base;
8817     }
8818   else
8819     b = XNEW (struct breakpoint);
8820
8821   old_chain = make_cleanup (xfree, b);
8822
8823   init_breakpoint_sal (b, gdbarch,
8824                        sals, addr_string,
8825                        filter, cond_string, extra_string,
8826                        type, disposition,
8827                        thread, task, ignore_count,
8828                        ops, from_tty,
8829                        enabled, internal, flags,
8830                        display_canonical);
8831   discard_cleanups (old_chain);
8832
8833   install_breakpoint (internal, b, 0);
8834 }
8835
8836 /* Add SALS.nelts breakpoints to the breakpoint table.  For each
8837    SALS.sal[i] breakpoint, include the corresponding ADDR_STRING[i]
8838    value.  COND_STRING, if not NULL, specified the condition to be
8839    used for all breakpoints.  Essentially the only case where
8840    SALS.nelts is not 1 is when we set a breakpoint on an overloaded
8841    function.  In that case, it's still not possible to specify
8842    separate conditions for different overloaded functions, so
8843    we take just a single condition string.
8844    
8845    NOTE: If the function succeeds, the caller is expected to cleanup
8846    the arrays ADDR_STRING, COND_STRING, and SALS (but not the
8847    array contents).  If the function fails (error() is called), the
8848    caller is expected to cleanups both the ADDR_STRING, COND_STRING,
8849    COND and SALS arrays and each of those arrays contents.  */
8850
8851 static void
8852 create_breakpoints_sal (struct gdbarch *gdbarch,
8853                         struct linespec_result *canonical,
8854                         char *cond_string, char *extra_string,
8855                         enum bptype type, enum bpdisp disposition,
8856                         int thread, int task, int ignore_count,
8857                         const struct breakpoint_ops *ops, int from_tty,
8858                         int enabled, int internal, unsigned flags)
8859 {
8860   int i;
8861   struct linespec_sals *lsal;
8862
8863   if (canonical->pre_expanded)
8864     gdb_assert (VEC_length (linespec_sals, canonical->sals) == 1);
8865
8866   for (i = 0; VEC_iterate (linespec_sals, canonical->sals, i, lsal); ++i)
8867     {
8868       /* Note that 'addr_string' can be NULL in the case of a plain
8869          'break', without arguments.  */
8870       char *addr_string = (canonical->addr_string
8871                            ? xstrdup (canonical->addr_string)
8872                            : NULL);
8873       char *filter_string = lsal->canonical ? xstrdup (lsal->canonical) : NULL;
8874       struct cleanup *inner = make_cleanup (xfree, addr_string);
8875
8876       make_cleanup (xfree, filter_string);
8877       create_breakpoint_sal (gdbarch, lsal->sals,
8878                              addr_string,
8879                              filter_string,
8880                              cond_string, extra_string,
8881                              type, disposition,
8882                              thread, task, ignore_count, ops,
8883                              from_tty, enabled, internal, flags,
8884                              canonical->special_display);
8885       discard_cleanups (inner);
8886     }
8887 }
8888
8889 /* Parse ADDRESS which is assumed to be a SAL specification possibly
8890    followed by conditionals.  On return, SALS contains an array of SAL
8891    addresses found.  ADDR_STRING contains a vector of (canonical)
8892    address strings.  ADDRESS points to the end of the SAL.
8893
8894    The array and the line spec strings are allocated on the heap, it is
8895    the caller's responsibility to free them.  */
8896
8897 static void
8898 parse_breakpoint_sals (char **address,
8899                        struct linespec_result *canonical)
8900 {
8901   char *addr_start = *address;
8902
8903   /* If no arg given, or if first arg is 'if ', use the default
8904      breakpoint.  */
8905   if ((*address) == NULL
8906       || (strncmp ((*address), "if", 2) == 0 && isspace ((*address)[2])))
8907     {
8908       /* The last displayed codepoint, if it's valid, is our default breakpoint
8909          address.  */
8910       if (last_displayed_sal_is_valid ())
8911         {
8912           struct linespec_sals lsal;
8913           struct symtab_and_line sal;
8914
8915           init_sal (&sal);              /* Initialize to zeroes.  */
8916           lsal.sals.sals = (struct symtab_and_line *)
8917             xmalloc (sizeof (struct symtab_and_line));
8918
8919           /* Set sal's pspace, pc, symtab, and line to the values
8920              corresponding to the last call to print_frame_info.  */
8921           get_last_displayed_sal (&sal);
8922           sal.section = find_pc_overlay (sal.pc);
8923
8924           /* "break" without arguments is equivalent to "break *PC"
8925              where PC is the last displayed codepoint's address.  So
8926              make sure to set sal.explicit_pc to prevent GDB from
8927              trying to expand the list of sals to include all other
8928              instances with the same symtab and line.  */
8929           sal.explicit_pc = 1;
8930
8931           lsal.sals.sals[0] = sal;
8932           lsal.sals.nelts = 1;
8933           lsal.canonical = NULL;
8934
8935           VEC_safe_push (linespec_sals, canonical->sals, &lsal);
8936         }
8937       else
8938         error (_("No default breakpoint address now."));
8939     }
8940   else
8941     {
8942       struct symtab_and_line cursal = get_current_source_symtab_and_line ();
8943
8944       /* Force almost all breakpoints to be in terms of the
8945          current_source_symtab (which is decode_line_1's default).
8946          This should produce the results we want almost all of the
8947          time while leaving default_breakpoint_* alone.
8948
8949          ObjC: However, don't match an Objective-C method name which
8950          may have a '+' or '-' succeeded by a '['.  */
8951       if (last_displayed_sal_is_valid ()
8952           && (!cursal.symtab
8953               || ((strchr ("+-", (*address)[0]) != NULL)
8954                   && ((*address)[1] != '['))))
8955         decode_line_full (address, DECODE_LINE_FUNFIRSTLINE,
8956                           get_last_displayed_symtab (),
8957                           get_last_displayed_line (),
8958                           canonical, NULL, NULL);
8959       else
8960         decode_line_full (address, DECODE_LINE_FUNFIRSTLINE,
8961                           cursal.symtab, cursal.line, canonical, NULL, NULL);
8962     }
8963 }
8964
8965
8966 /* Convert each SAL into a real PC.  Verify that the PC can be
8967    inserted as a breakpoint.  If it can't throw an error.  */
8968
8969 static void
8970 breakpoint_sals_to_pc (struct symtabs_and_lines *sals)
8971 {    
8972   int i;
8973
8974   for (i = 0; i < sals->nelts; i++)
8975     resolve_sal_pc (&sals->sals[i]);
8976 }
8977
8978 /* Fast tracepoints may have restrictions on valid locations.  For
8979    instance, a fast tracepoint using a jump instead of a trap will
8980    likely have to overwrite more bytes than a trap would, and so can
8981    only be placed where the instruction is longer than the jump, or a
8982    multi-instruction sequence does not have a jump into the middle of
8983    it, etc.  */
8984
8985 static void
8986 check_fast_tracepoint_sals (struct gdbarch *gdbarch,
8987                             struct symtabs_and_lines *sals)
8988 {
8989   int i, rslt;
8990   struct symtab_and_line *sal;
8991   char *msg;
8992   struct cleanup *old_chain;
8993
8994   for (i = 0; i < sals->nelts; i++)
8995     {
8996       struct gdbarch *sarch;
8997
8998       sal = &sals->sals[i];
8999
9000       sarch = get_sal_arch (*sal);
9001       /* We fall back to GDBARCH if there is no architecture
9002          associated with SAL.  */
9003       if (sarch == NULL)
9004         sarch = gdbarch;
9005       rslt = gdbarch_fast_tracepoint_valid_at (sarch, sal->pc,
9006                                                NULL, &msg);
9007       old_chain = make_cleanup (xfree, msg);
9008
9009       if (!rslt)
9010         error (_("May not have a fast tracepoint at 0x%s%s"),
9011                paddress (sarch, sal->pc), (msg ? msg : ""));
9012
9013       do_cleanups (old_chain);
9014     }
9015 }
9016
9017 /* Given TOK, a string specification of condition and thread, as
9018    accepted by the 'break' command, extract the condition
9019    string and thread number and set *COND_STRING and *THREAD.
9020    PC identifies the context at which the condition should be parsed.
9021    If no condition is found, *COND_STRING is set to NULL.
9022    If no thread is found, *THREAD is set to -1.  */
9023
9024 static void
9025 find_condition_and_thread (char *tok, CORE_ADDR pc,
9026                            char **cond_string, int *thread, int *task,
9027                            char **rest)
9028 {
9029   *cond_string = NULL;
9030   *thread = -1;
9031   while (tok && *tok)
9032     {
9033       char *end_tok;
9034       int toklen;
9035       char *cond_start = NULL;
9036       char *cond_end = NULL;
9037
9038       tok = skip_spaces (tok);
9039
9040       if ((*tok == '"' || *tok == ',') && rest)
9041         {
9042           *rest = savestring (tok, strlen (tok));
9043           return;
9044         }
9045
9046       end_tok = skip_to_space (tok);
9047
9048       toklen = end_tok - tok;
9049
9050       if (toklen >= 1 && strncmp (tok, "if", toklen) == 0)
9051         {
9052           struct expression *expr;
9053
9054           tok = cond_start = end_tok + 1;
9055           expr = parse_exp_1 (&tok, block_for_pc (pc), 0);
9056           xfree (expr);
9057           cond_end = tok;
9058           *cond_string = savestring (cond_start, cond_end - cond_start);
9059         }
9060       else if (toklen >= 1 && strncmp (tok, "thread", toklen) == 0)
9061         {
9062           char *tmptok;
9063
9064           tok = end_tok + 1;
9065           tmptok = tok;
9066           *thread = strtol (tok, &tok, 0);
9067           if (tok == tmptok)
9068             error (_("Junk after thread keyword."));
9069           if (!valid_thread_id (*thread))
9070             error (_("Unknown thread %d."), *thread);
9071         }
9072       else if (toklen >= 1 && strncmp (tok, "task", toklen) == 0)
9073         {
9074           char *tmptok;
9075
9076           tok = end_tok + 1;
9077           tmptok = tok;
9078           *task = strtol (tok, &tok, 0);
9079           if (tok == tmptok)
9080             error (_("Junk after task keyword."));
9081           if (!valid_task_id (*task))
9082             error (_("Unknown task %d."), *task);
9083         }
9084       else if (rest)
9085         {
9086           *rest = savestring (tok, strlen (tok));
9087           return;
9088         }
9089       else
9090         error (_("Junk at end of arguments."));
9091     }
9092 }
9093
9094 /* Decode a static tracepoint marker spec.  */
9095
9096 static struct symtabs_and_lines
9097 decode_static_tracepoint_spec (char **arg_p)
9098 {
9099   VEC(static_tracepoint_marker_p) *markers = NULL;
9100   struct symtabs_and_lines sals;
9101   struct cleanup *old_chain;
9102   char *p = &(*arg_p)[3];
9103   char *endp;
9104   char *marker_str;
9105   int i;
9106
9107   p = skip_spaces (p);
9108
9109   endp = skip_to_space (p);
9110
9111   marker_str = savestring (p, endp - p);
9112   old_chain = make_cleanup (xfree, marker_str);
9113
9114   markers = target_static_tracepoint_markers_by_strid (marker_str);
9115   if (VEC_empty(static_tracepoint_marker_p, markers))
9116     error (_("No known static tracepoint marker named %s"), marker_str);
9117
9118   sals.nelts = VEC_length(static_tracepoint_marker_p, markers);
9119   sals.sals = xmalloc (sizeof *sals.sals * sals.nelts);
9120
9121   for (i = 0; i < sals.nelts; i++)
9122     {
9123       struct static_tracepoint_marker *marker;
9124
9125       marker = VEC_index (static_tracepoint_marker_p, markers, i);
9126
9127       init_sal (&sals.sals[i]);
9128
9129       sals.sals[i] = find_pc_line (marker->address, 0);
9130       sals.sals[i].pc = marker->address;
9131
9132       release_static_tracepoint_marker (marker);
9133     }
9134
9135   do_cleanups (old_chain);
9136
9137   *arg_p = endp;
9138   return sals;
9139 }
9140
9141 /* Set a breakpoint.  This function is shared between CLI and MI
9142    functions for setting a breakpoint.  This function has two major
9143    modes of operations, selected by the PARSE_CONDITION_AND_THREAD
9144    parameter.  If non-zero, the function will parse arg, extracting
9145    breakpoint location, address and thread.  Otherwise, ARG is just
9146    the location of breakpoint, with condition and thread specified by
9147    the COND_STRING and THREAD parameters.  If INTERNAL is non-zero,
9148    the breakpoint number will be allocated from the internal
9149    breakpoint count.  Returns true if any breakpoint was created;
9150    false otherwise.  */
9151
9152 int
9153 create_breakpoint (struct gdbarch *gdbarch,
9154                    char *arg, char *cond_string,
9155                    int thread, char *extra_string,
9156                    int parse_condition_and_thread,
9157                    int tempflag, enum bptype type_wanted,
9158                    int ignore_count,
9159                    enum auto_boolean pending_break_support,
9160                    const struct breakpoint_ops *ops,
9161                    int from_tty, int enabled, int internal,
9162                    unsigned flags)
9163 {
9164   volatile struct gdb_exception e;
9165   char *copy_arg = NULL;
9166   char *addr_start = arg;
9167   struct linespec_result canonical;
9168   struct cleanup *old_chain;
9169   struct cleanup *bkpt_chain = NULL;
9170   int pending = 0;
9171   int task = 0;
9172   int prev_bkpt_count = breakpoint_count;
9173
9174   gdb_assert (ops != NULL);
9175
9176   init_linespec_result (&canonical);
9177
9178   TRY_CATCH (e, RETURN_MASK_ALL)
9179     {
9180       ops->create_sals_from_address (&arg, &canonical, type_wanted,
9181                                      addr_start, &copy_arg);
9182     }
9183
9184   /* If caller is interested in rc value from parse, set value.  */
9185   switch (e.reason)
9186     {
9187     case GDB_NO_ERROR:
9188       if (VEC_empty (linespec_sals, canonical.sals))
9189         return 0;
9190       break;
9191     case RETURN_ERROR:
9192       switch (e.error)
9193         {
9194         case NOT_FOUND_ERROR:
9195
9196           /* If pending breakpoint support is turned off, throw
9197              error.  */
9198
9199           if (pending_break_support == AUTO_BOOLEAN_FALSE)
9200             throw_exception (e);
9201
9202           exception_print (gdb_stderr, e);
9203
9204           /* If pending breakpoint support is auto query and the user
9205              selects no, then simply return the error code.  */
9206           if (pending_break_support == AUTO_BOOLEAN_AUTO
9207               && !nquery (_("Make %s pending on future shared library load? "),
9208                           bptype_string (type_wanted)))
9209             return 0;
9210
9211           /* At this point, either the user was queried about setting
9212              a pending breakpoint and selected yes, or pending
9213              breakpoint behavior is on and thus a pending breakpoint
9214              is defaulted on behalf of the user.  */
9215           {
9216             struct linespec_sals lsal;
9217
9218             copy_arg = xstrdup (addr_start);
9219             lsal.canonical = xstrdup (copy_arg);
9220             lsal.sals.nelts = 1;
9221             lsal.sals.sals = XNEW (struct symtab_and_line);
9222             init_sal (&lsal.sals.sals[0]);
9223             pending = 1;
9224             VEC_safe_push (linespec_sals, canonical.sals, &lsal);
9225           }
9226           break;
9227         default:
9228           throw_exception (e);
9229         }
9230       break;
9231     default:
9232       throw_exception (e);
9233     }
9234
9235   /* Create a chain of things that always need to be cleaned up.  */
9236   old_chain = make_cleanup_destroy_linespec_result (&canonical);
9237
9238   /* ----------------------------- SNIP -----------------------------
9239      Anything added to the cleanup chain beyond this point is assumed
9240      to be part of a breakpoint.  If the breakpoint create succeeds
9241      then the memory is not reclaimed.  */
9242   bkpt_chain = make_cleanup (null_cleanup, 0);
9243
9244   /* Resolve all line numbers to PC's and verify that the addresses
9245      are ok for the target.  */
9246   if (!pending)
9247     {
9248       int ix;
9249       struct linespec_sals *iter;
9250
9251       for (ix = 0; VEC_iterate (linespec_sals, canonical.sals, ix, iter); ++ix)
9252         breakpoint_sals_to_pc (&iter->sals);
9253     }
9254
9255   /* Fast tracepoints may have additional restrictions on location.  */
9256   if (!pending && type_wanted == bp_fast_tracepoint)
9257     {
9258       int ix;
9259       struct linespec_sals *iter;
9260
9261       for (ix = 0; VEC_iterate (linespec_sals, canonical.sals, ix, iter); ++ix)
9262         check_fast_tracepoint_sals (gdbarch, &iter->sals);
9263     }
9264
9265   /* Verify that condition can be parsed, before setting any
9266      breakpoints.  Allocate a separate condition expression for each
9267      breakpoint.  */
9268   if (!pending)
9269     {
9270       struct linespec_sals *lsal;
9271
9272       lsal = VEC_index (linespec_sals, canonical.sals, 0);
9273
9274       if (parse_condition_and_thread)
9275         {
9276             char *rest;
9277             /* Here we only parse 'arg' to separate condition
9278                from thread number, so parsing in context of first
9279                sal is OK.  When setting the breakpoint we'll 
9280                re-parse it in context of each sal.  */
9281             cond_string = NULL;
9282             thread = -1;
9283             rest = NULL;
9284             find_condition_and_thread (arg, lsal->sals.sals[0].pc, &cond_string,
9285                                        &thread, &task, &rest);
9286             if (cond_string)
9287                 make_cleanup (xfree, cond_string);
9288             if (rest)
9289               make_cleanup (xfree, rest);
9290             if (rest)
9291               extra_string = rest;
9292         }
9293       else
9294         {
9295             /* Create a private copy of condition string.  */
9296             if (cond_string)
9297             {
9298                 cond_string = xstrdup (cond_string);
9299                 make_cleanup (xfree, cond_string);
9300             }
9301             /* Create a private copy of any extra string.  */
9302             if (extra_string)
9303               {
9304                 extra_string = xstrdup (extra_string);
9305                 make_cleanup (xfree, extra_string);
9306               }
9307         }
9308
9309       ops->create_breakpoints_sal (gdbarch, &canonical, lsal,
9310                                    cond_string, extra_string, type_wanted,
9311                                    tempflag ? disp_del : disp_donttouch,
9312                                    thread, task, ignore_count, ops,
9313                                    from_tty, enabled, internal, flags);
9314     }
9315   else
9316     {
9317       struct breakpoint *b;
9318
9319       make_cleanup (xfree, copy_arg);
9320
9321       if (is_tracepoint_type (type_wanted))
9322         {
9323           struct tracepoint *t;
9324
9325           t = XCNEW (struct tracepoint);
9326           b = &t->base;
9327         }
9328       else
9329         b = XNEW (struct breakpoint);
9330
9331       init_raw_breakpoint_without_location (b, gdbarch, type_wanted, ops);
9332
9333       b->addr_string = copy_arg;
9334       b->cond_string = NULL;
9335       b->extra_string = NULL;
9336       b->ignore_count = ignore_count;
9337       b->disposition = tempflag ? disp_del : disp_donttouch;
9338       b->condition_not_parsed = 1;
9339       b->enable_state = enabled ? bp_enabled : bp_disabled;
9340       if ((type_wanted != bp_breakpoint
9341            && type_wanted != bp_hardware_breakpoint) || thread != -1)
9342         b->pspace = current_program_space;
9343
9344       install_breakpoint (internal, b, 0);
9345     }
9346   
9347   if (VEC_length (linespec_sals, canonical.sals) > 1)
9348     {
9349       warning (_("Multiple breakpoints were set.\nUse the "
9350                  "\"delete\" command to delete unwanted breakpoints."));
9351       prev_breakpoint_count = prev_bkpt_count;
9352     }
9353
9354   /* That's it.  Discard the cleanups for data inserted into the
9355      breakpoint.  */
9356   discard_cleanups (bkpt_chain);
9357   /* But cleanup everything else.  */
9358   do_cleanups (old_chain);
9359
9360   /* error call may happen here - have BKPT_CHAIN already discarded.  */
9361   update_global_location_list (1);
9362
9363   return 1;
9364 }
9365
9366 /* Set a breakpoint.
9367    ARG is a string describing breakpoint address,
9368    condition, and thread.
9369    FLAG specifies if a breakpoint is hardware on,
9370    and if breakpoint is temporary, using BP_HARDWARE_FLAG
9371    and BP_TEMPFLAG.  */
9372
9373 static void
9374 break_command_1 (char *arg, int flag, int from_tty)
9375 {
9376   int tempflag = flag & BP_TEMPFLAG;
9377   enum bptype type_wanted = (flag & BP_HARDWAREFLAG
9378                              ? bp_hardware_breakpoint
9379                              : bp_breakpoint);
9380   struct breakpoint_ops *ops;
9381   const char *arg_cp = arg;
9382
9383   /* Matching breakpoints on probes.  */
9384   if (arg && probe_linespec_to_ops (&arg_cp) != NULL)
9385     ops = &bkpt_probe_breakpoint_ops;
9386   else
9387     ops = &bkpt_breakpoint_ops;
9388
9389   create_breakpoint (get_current_arch (),
9390                      arg,
9391                      NULL, 0, NULL, 1 /* parse arg */,
9392                      tempflag, type_wanted,
9393                      0 /* Ignore count */,
9394                      pending_break_support,
9395                      ops,
9396                      from_tty,
9397                      1 /* enabled */,
9398                      0 /* internal */,
9399                      0);
9400 }
9401
9402 /* Helper function for break_command_1 and disassemble_command.  */
9403
9404 void
9405 resolve_sal_pc (struct symtab_and_line *sal)
9406 {
9407   CORE_ADDR pc;
9408
9409   if (sal->pc == 0 && sal->symtab != NULL)
9410     {
9411       if (!find_line_pc (sal->symtab, sal->line, &pc))
9412         error (_("No line %d in file \"%s\"."),
9413                sal->line, sal->symtab->filename);
9414       sal->pc = pc;
9415
9416       /* If this SAL corresponds to a breakpoint inserted using a line
9417          number, then skip the function prologue if necessary.  */
9418       if (sal->explicit_line)
9419         skip_prologue_sal (sal);
9420     }
9421
9422   if (sal->section == 0 && sal->symtab != NULL)
9423     {
9424       struct blockvector *bv;
9425       struct block *b;
9426       struct symbol *sym;
9427
9428       bv = blockvector_for_pc_sect (sal->pc, 0, &b, sal->symtab);
9429       if (bv != NULL)
9430         {
9431           sym = block_linkage_function (b);
9432           if (sym != NULL)
9433             {
9434               fixup_symbol_section (sym, sal->symtab->objfile);
9435               sal->section = SYMBOL_OBJ_SECTION (sym);
9436             }
9437           else
9438             {
9439               /* It really is worthwhile to have the section, so we'll
9440                  just have to look harder. This case can be executed
9441                  if we have line numbers but no functions (as can
9442                  happen in assembly source).  */
9443
9444               struct minimal_symbol *msym;
9445               struct cleanup *old_chain = save_current_space_and_thread ();
9446
9447               switch_to_program_space_and_thread (sal->pspace);
9448
9449               msym = lookup_minimal_symbol_by_pc (sal->pc);
9450               if (msym)
9451                 sal->section = SYMBOL_OBJ_SECTION (msym);
9452
9453               do_cleanups (old_chain);
9454             }
9455         }
9456     }
9457 }
9458
9459 void
9460 break_command (char *arg, int from_tty)
9461 {
9462   break_command_1 (arg, 0, from_tty);
9463 }
9464
9465 void
9466 tbreak_command (char *arg, int from_tty)
9467 {
9468   break_command_1 (arg, BP_TEMPFLAG, from_tty);
9469 }
9470
9471 static void
9472 hbreak_command (char *arg, int from_tty)
9473 {
9474   break_command_1 (arg, BP_HARDWAREFLAG, from_tty);
9475 }
9476
9477 static void
9478 thbreak_command (char *arg, int from_tty)
9479 {
9480   break_command_1 (arg, (BP_TEMPFLAG | BP_HARDWAREFLAG), from_tty);
9481 }
9482
9483 static void
9484 stop_command (char *arg, int from_tty)
9485 {
9486   printf_filtered (_("Specify the type of breakpoint to set.\n\
9487 Usage: stop in <function | address>\n\
9488        stop at <line>\n"));
9489 }
9490
9491 static void
9492 stopin_command (char *arg, int from_tty)
9493 {
9494   int badInput = 0;
9495
9496   if (arg == (char *) NULL)
9497     badInput = 1;
9498   else if (*arg != '*')
9499     {
9500       char *argptr = arg;
9501       int hasColon = 0;
9502
9503       /* Look for a ':'.  If this is a line number specification, then
9504          say it is bad, otherwise, it should be an address or
9505          function/method name.  */
9506       while (*argptr && !hasColon)
9507         {
9508           hasColon = (*argptr == ':');
9509           argptr++;
9510         }
9511
9512       if (hasColon)
9513         badInput = (*argptr != ':');    /* Not a class::method */
9514       else
9515         badInput = isdigit (*arg);      /* a simple line number */
9516     }
9517
9518   if (badInput)
9519     printf_filtered (_("Usage: stop in <function | address>\n"));
9520   else
9521     break_command_1 (arg, 0, from_tty);
9522 }
9523
9524 static void
9525 stopat_command (char *arg, int from_tty)
9526 {
9527   int badInput = 0;
9528
9529   if (arg == (char *) NULL || *arg == '*')      /* no line number */
9530     badInput = 1;
9531   else
9532     {
9533       char *argptr = arg;
9534       int hasColon = 0;
9535
9536       /* Look for a ':'.  If there is a '::' then get out, otherwise
9537          it is probably a line number.  */
9538       while (*argptr && !hasColon)
9539         {
9540           hasColon = (*argptr == ':');
9541           argptr++;
9542         }
9543
9544       if (hasColon)
9545         badInput = (*argptr == ':');    /* we have class::method */
9546       else
9547         badInput = !isdigit (*arg);     /* not a line number */
9548     }
9549
9550   if (badInput)
9551     printf_filtered (_("Usage: stop at <line>\n"));
9552   else
9553     break_command_1 (arg, 0, from_tty);
9554 }
9555
9556 void dprintf_command (char *arg, int from_tty);
9557
9558 /* The dynamic printf command is mostly like a regular breakpoint, but
9559    with a prewired command list consisting of a single output command,
9560    built from extra arguments supplied on the dprintf command
9561    line.  */
9562
9563 void
9564 dprintf_command (char *arg, int from_tty)
9565 {
9566   create_breakpoint (get_current_arch (),
9567                      arg,
9568                      NULL, 0, NULL, 1 /* parse arg */,
9569                      0, bp_dprintf,
9570                      0 /* Ignore count */,
9571                      pending_break_support,
9572                      &dprintf_breakpoint_ops,
9573                      from_tty,
9574                      1 /* enabled */,
9575                      0 /* internal */,
9576                      0);
9577 }
9578
9579 /* Implement the "breakpoint_hit" breakpoint_ops method for
9580    ranged breakpoints.  */
9581
9582 static int
9583 breakpoint_hit_ranged_breakpoint (const struct bp_location *bl,
9584                                   struct address_space *aspace,
9585                                   CORE_ADDR bp_addr,
9586                                   const struct target_waitstatus *ws)
9587 {
9588   if (ws->kind != TARGET_WAITKIND_STOPPED
9589       || ws->value.sig != GDB_SIGNAL_TRAP)
9590     return 0;
9591
9592   return breakpoint_address_match_range (bl->pspace->aspace, bl->address,
9593                                          bl->length, aspace, bp_addr);
9594 }
9595
9596 /* Implement the "resources_needed" breakpoint_ops method for
9597    ranged breakpoints.  */
9598
9599 static int
9600 resources_needed_ranged_breakpoint (const struct bp_location *bl)
9601 {
9602   return target_ranged_break_num_registers ();
9603 }
9604
9605 /* Implement the "print_it" breakpoint_ops method for
9606    ranged breakpoints.  */
9607
9608 static enum print_stop_action
9609 print_it_ranged_breakpoint (bpstat bs)
9610 {
9611   struct breakpoint *b = bs->breakpoint_at;
9612   struct bp_location *bl = b->loc;
9613   struct ui_out *uiout = current_uiout;
9614
9615   gdb_assert (b->type == bp_hardware_breakpoint);
9616
9617   /* Ranged breakpoints have only one location.  */
9618   gdb_assert (bl && bl->next == NULL);
9619
9620   annotate_breakpoint (b->number);
9621   if (b->disposition == disp_del)
9622     ui_out_text (uiout, "\nTemporary ranged breakpoint ");
9623   else
9624     ui_out_text (uiout, "\nRanged breakpoint ");
9625   if (ui_out_is_mi_like_p (uiout))
9626     {
9627       ui_out_field_string (uiout, "reason",
9628                       async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT));
9629       ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
9630     }
9631   ui_out_field_int (uiout, "bkptno", b->number);
9632   ui_out_text (uiout, ", ");
9633
9634   return PRINT_SRC_AND_LOC;
9635 }
9636
9637 /* Implement the "print_one" breakpoint_ops method for
9638    ranged breakpoints.  */
9639
9640 static void
9641 print_one_ranged_breakpoint (struct breakpoint *b,
9642                              struct bp_location **last_loc)
9643 {
9644   struct bp_location *bl = b->loc;
9645   struct value_print_options opts;
9646   struct ui_out *uiout = current_uiout;
9647
9648   /* Ranged breakpoints have only one location.  */
9649   gdb_assert (bl && bl->next == NULL);
9650
9651   get_user_print_options (&opts);
9652
9653   if (opts.addressprint)
9654     /* We don't print the address range here, it will be printed later
9655        by print_one_detail_ranged_breakpoint.  */
9656     ui_out_field_skip (uiout, "addr");
9657   annotate_field (5);
9658   print_breakpoint_location (b, bl);
9659   *last_loc = bl;
9660 }
9661
9662 /* Implement the "print_one_detail" breakpoint_ops method for
9663    ranged breakpoints.  */
9664
9665 static void
9666 print_one_detail_ranged_breakpoint (const struct breakpoint *b,
9667                                     struct ui_out *uiout)
9668 {
9669   CORE_ADDR address_start, address_end;
9670   struct bp_location *bl = b->loc;
9671   struct ui_file *stb = mem_fileopen ();
9672   struct cleanup *cleanup = make_cleanup_ui_file_delete (stb);
9673
9674   gdb_assert (bl);
9675
9676   address_start = bl->address;
9677   address_end = address_start + bl->length - 1;
9678
9679   ui_out_text (uiout, "\taddress range: ");
9680   fprintf_unfiltered (stb, "[%s, %s]",
9681                       print_core_address (bl->gdbarch, address_start),
9682                       print_core_address (bl->gdbarch, address_end));
9683   ui_out_field_stream (uiout, "addr", stb);
9684   ui_out_text (uiout, "\n");
9685
9686   do_cleanups (cleanup);
9687 }
9688
9689 /* Implement the "print_mention" breakpoint_ops method for
9690    ranged breakpoints.  */
9691
9692 static void
9693 print_mention_ranged_breakpoint (struct breakpoint *b)
9694 {
9695   struct bp_location *bl = b->loc;
9696   struct ui_out *uiout = current_uiout;
9697
9698   gdb_assert (bl);
9699   gdb_assert (b->type == bp_hardware_breakpoint);
9700
9701   if (ui_out_is_mi_like_p (uiout))
9702     return;
9703
9704   printf_filtered (_("Hardware assisted ranged breakpoint %d from %s to %s."),
9705                    b->number, paddress (bl->gdbarch, bl->address),
9706                    paddress (bl->gdbarch, bl->address + bl->length - 1));
9707 }
9708
9709 /* Implement the "print_recreate" breakpoint_ops method for
9710    ranged breakpoints.  */
9711
9712 static void
9713 print_recreate_ranged_breakpoint (struct breakpoint *b, struct ui_file *fp)
9714 {
9715   fprintf_unfiltered (fp, "break-range %s, %s", b->addr_string,
9716                       b->addr_string_range_end);
9717   print_recreate_thread (b, fp);
9718 }
9719
9720 /* The breakpoint_ops structure to be used in ranged breakpoints.  */
9721
9722 static struct breakpoint_ops ranged_breakpoint_ops;
9723
9724 /* Find the address where the end of the breakpoint range should be
9725    placed, given the SAL of the end of the range.  This is so that if
9726    the user provides a line number, the end of the range is set to the
9727    last instruction of the given line.  */
9728
9729 static CORE_ADDR
9730 find_breakpoint_range_end (struct symtab_and_line sal)
9731 {
9732   CORE_ADDR end;
9733
9734   /* If the user provided a PC value, use it.  Otherwise,
9735      find the address of the end of the given location.  */
9736   if (sal.explicit_pc)
9737     end = sal.pc;
9738   else
9739     {
9740       int ret;
9741       CORE_ADDR start;
9742
9743       ret = find_line_pc_range (sal, &start, &end);
9744       if (!ret)
9745         error (_("Could not find location of the end of the range."));
9746
9747       /* find_line_pc_range returns the start of the next line.  */
9748       end--;
9749     }
9750
9751   return end;
9752 }
9753
9754 /* Implement the "break-range" CLI command.  */
9755
9756 static void
9757 break_range_command (char *arg, int from_tty)
9758 {
9759   char *arg_start, *addr_string_start, *addr_string_end;
9760   struct linespec_result canonical_start, canonical_end;
9761   int bp_count, can_use_bp, length;
9762   CORE_ADDR end;
9763   struct breakpoint *b;
9764   struct symtab_and_line sal_start, sal_end;
9765   struct cleanup *cleanup_bkpt;
9766   struct linespec_sals *lsal_start, *lsal_end;
9767
9768   /* We don't support software ranged breakpoints.  */
9769   if (target_ranged_break_num_registers () < 0)
9770     error (_("This target does not support hardware ranged breakpoints."));
9771
9772   bp_count = hw_breakpoint_used_count ();
9773   bp_count += target_ranged_break_num_registers ();
9774   can_use_bp = target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
9775                                                    bp_count, 0);
9776   if (can_use_bp < 0)
9777     error (_("Hardware breakpoints used exceeds limit."));
9778
9779   arg = skip_spaces (arg);
9780   if (arg == NULL || arg[0] == '\0')
9781     error(_("No address range specified."));
9782
9783   init_linespec_result (&canonical_start);
9784
9785   arg_start = arg;
9786   parse_breakpoint_sals (&arg, &canonical_start);
9787
9788   cleanup_bkpt = make_cleanup_destroy_linespec_result (&canonical_start);
9789
9790   if (arg[0] != ',')
9791     error (_("Too few arguments."));
9792   else if (VEC_empty (linespec_sals, canonical_start.sals))
9793     error (_("Could not find location of the beginning of the range."));
9794
9795   lsal_start = VEC_index (linespec_sals, canonical_start.sals, 0);
9796
9797   if (VEC_length (linespec_sals, canonical_start.sals) > 1
9798       || lsal_start->sals.nelts != 1)
9799     error (_("Cannot create a ranged breakpoint with multiple locations."));
9800
9801   sal_start = lsal_start->sals.sals[0];
9802   addr_string_start = savestring (arg_start, arg - arg_start);
9803   make_cleanup (xfree, addr_string_start);
9804
9805   arg++;        /* Skip the comma.  */
9806   arg = skip_spaces (arg);
9807
9808   /* Parse the end location.  */
9809
9810   init_linespec_result (&canonical_end);
9811   arg_start = arg;
9812
9813   /* We call decode_line_full directly here instead of using
9814      parse_breakpoint_sals because we need to specify the start location's
9815      symtab and line as the default symtab and line for the end of the
9816      range.  This makes it possible to have ranges like "foo.c:27, +14",
9817      where +14 means 14 lines from the start location.  */
9818   decode_line_full (&arg, DECODE_LINE_FUNFIRSTLINE,
9819                     sal_start.symtab, sal_start.line,
9820                     &canonical_end, NULL, NULL);
9821
9822   make_cleanup_destroy_linespec_result (&canonical_end);
9823
9824   if (VEC_empty (linespec_sals, canonical_end.sals))
9825     error (_("Could not find location of the end of the range."));
9826
9827   lsal_end = VEC_index (linespec_sals, canonical_end.sals, 0);
9828   if (VEC_length (linespec_sals, canonical_end.sals) > 1
9829       || lsal_end->sals.nelts != 1)
9830     error (_("Cannot create a ranged breakpoint with multiple locations."));
9831
9832   sal_end = lsal_end->sals.sals[0];
9833   addr_string_end = savestring (arg_start, arg - arg_start);
9834   make_cleanup (xfree, addr_string_end);
9835
9836   end = find_breakpoint_range_end (sal_end);
9837   if (sal_start.pc > end)
9838     error (_("Invalid address range, end precedes start."));
9839
9840   length = end - sal_start.pc + 1;
9841   if (length < 0)
9842     /* Length overflowed.  */
9843     error (_("Address range too large."));
9844   else if (length == 1)
9845     {
9846       /* This range is simple enough to be handled by
9847          the `hbreak' command.  */
9848       hbreak_command (addr_string_start, 1);
9849
9850       do_cleanups (cleanup_bkpt);
9851
9852       return;
9853     }
9854
9855   /* Now set up the breakpoint.  */
9856   b = set_raw_breakpoint (get_current_arch (), sal_start,
9857                           bp_hardware_breakpoint, &ranged_breakpoint_ops);
9858   set_breakpoint_count (breakpoint_count + 1);
9859   b->number = breakpoint_count;
9860   b->disposition = disp_donttouch;
9861   b->addr_string = xstrdup (addr_string_start);
9862   b->addr_string_range_end = xstrdup (addr_string_end);
9863   b->loc->length = length;
9864
9865   do_cleanups (cleanup_bkpt);
9866
9867   mention (b);
9868   observer_notify_breakpoint_created (b);
9869   update_global_location_list (1);
9870 }
9871
9872 /*  Return non-zero if EXP is verified as constant.  Returned zero
9873     means EXP is variable.  Also the constant detection may fail for
9874     some constant expressions and in such case still falsely return
9875     zero.  */
9876
9877 static int
9878 watchpoint_exp_is_const (const struct expression *exp)
9879 {
9880   int i = exp->nelts;
9881
9882   while (i > 0)
9883     {
9884       int oplenp, argsp;
9885
9886       /* We are only interested in the descriptor of each element.  */
9887       operator_length (exp, i, &oplenp, &argsp);
9888       i -= oplenp;
9889
9890       switch (exp->elts[i].opcode)
9891         {
9892         case BINOP_ADD:
9893         case BINOP_SUB:
9894         case BINOP_MUL:
9895         case BINOP_DIV:
9896         case BINOP_REM:
9897         case BINOP_MOD:
9898         case BINOP_LSH:
9899         case BINOP_RSH:
9900         case BINOP_LOGICAL_AND:
9901         case BINOP_LOGICAL_OR:
9902         case BINOP_BITWISE_AND:
9903         case BINOP_BITWISE_IOR:
9904         case BINOP_BITWISE_XOR:
9905         case BINOP_EQUAL:
9906         case BINOP_NOTEQUAL:
9907         case BINOP_LESS:
9908         case BINOP_GTR:
9909         case BINOP_LEQ:
9910         case BINOP_GEQ:
9911         case BINOP_REPEAT:
9912         case BINOP_COMMA:
9913         case BINOP_EXP:
9914         case BINOP_MIN:
9915         case BINOP_MAX:
9916         case BINOP_INTDIV:
9917         case BINOP_CONCAT:
9918         case BINOP_IN:
9919         case BINOP_RANGE:
9920         case TERNOP_COND:
9921         case TERNOP_SLICE:
9922         case TERNOP_SLICE_COUNT:
9923
9924         case OP_LONG:
9925         case OP_DOUBLE:
9926         case OP_DECFLOAT:
9927         case OP_LAST:
9928         case OP_COMPLEX:
9929         case OP_STRING:
9930         case OP_BITSTRING:
9931         case OP_ARRAY:
9932         case OP_TYPE:
9933         case OP_NAME:
9934         case OP_OBJC_NSSTRING:
9935
9936         case UNOP_NEG:
9937         case UNOP_LOGICAL_NOT:
9938         case UNOP_COMPLEMENT:
9939         case UNOP_ADDR:
9940         case UNOP_HIGH:
9941         case UNOP_CAST:
9942           /* Unary, binary and ternary operators: We have to check
9943              their operands.  If they are constant, then so is the
9944              result of that operation.  For instance, if A and B are
9945              determined to be constants, then so is "A + B".
9946
9947              UNOP_IND is one exception to the rule above, because the
9948              value of *ADDR is not necessarily a constant, even when
9949              ADDR is.  */
9950           break;
9951
9952         case OP_VAR_VALUE:
9953           /* Check whether the associated symbol is a constant.
9954
9955              We use SYMBOL_CLASS rather than TYPE_CONST because it's
9956              possible that a buggy compiler could mark a variable as
9957              constant even when it is not, and TYPE_CONST would return
9958              true in this case, while SYMBOL_CLASS wouldn't.
9959
9960              We also have to check for function symbols because they
9961              are always constant.  */
9962           {
9963             struct symbol *s = exp->elts[i + 2].symbol;
9964
9965             if (SYMBOL_CLASS (s) != LOC_BLOCK
9966                 && SYMBOL_CLASS (s) != LOC_CONST
9967                 && SYMBOL_CLASS (s) != LOC_CONST_BYTES)
9968               return 0;
9969             break;
9970           }
9971
9972         /* The default action is to return 0 because we are using
9973            the optimistic approach here: If we don't know something,
9974            then it is not a constant.  */
9975         default:
9976           return 0;
9977         }
9978     }
9979
9980   return 1;
9981 }
9982
9983 /* Implement the "dtor" breakpoint_ops method for watchpoints.  */
9984
9985 static void
9986 dtor_watchpoint (struct breakpoint *self)
9987 {
9988   struct watchpoint *w = (struct watchpoint *) self;
9989
9990   xfree (w->cond_exp);
9991   xfree (w->exp);
9992   xfree (w->exp_string);
9993   xfree (w->exp_string_reparse);
9994   value_free (w->val);
9995
9996   base_breakpoint_ops.dtor (self);
9997 }
9998
9999 /* Implement the "re_set" breakpoint_ops method for watchpoints.  */
10000
10001 static void
10002 re_set_watchpoint (struct breakpoint *b)
10003 {
10004   struct watchpoint *w = (struct watchpoint *) b;
10005
10006   /* Watchpoint can be either on expression using entirely global
10007      variables, or it can be on local variables.
10008
10009      Watchpoints of the first kind are never auto-deleted, and even
10010      persist across program restarts.  Since they can use variables
10011      from shared libraries, we need to reparse expression as libraries
10012      are loaded and unloaded.
10013
10014      Watchpoints on local variables can also change meaning as result
10015      of solib event.  For example, if a watchpoint uses both a local
10016      and a global variables in expression, it's a local watchpoint,
10017      but unloading of a shared library will make the expression
10018      invalid.  This is not a very common use case, but we still
10019      re-evaluate expression, to avoid surprises to the user.
10020
10021      Note that for local watchpoints, we re-evaluate it only if
10022      watchpoints frame id is still valid.  If it's not, it means the
10023      watchpoint is out of scope and will be deleted soon.  In fact,
10024      I'm not sure we'll ever be called in this case.
10025
10026      If a local watchpoint's frame id is still valid, then
10027      w->exp_valid_block is likewise valid, and we can safely use it.
10028
10029      Don't do anything about disabled watchpoints, since they will be
10030      reevaluated again when enabled.  */
10031   update_watchpoint (w, 1 /* reparse */);
10032 }
10033
10034 /* Implement the "insert" breakpoint_ops method for hardware watchpoints.  */
10035
10036 static int
10037 insert_watchpoint (struct bp_location *bl)
10038 {
10039   struct watchpoint *w = (struct watchpoint *) bl->owner;
10040   int length = w->exact ? 1 : bl->length;
10041
10042   return target_insert_watchpoint (bl->address, length, bl->watchpoint_type,
10043                                    w->cond_exp);
10044 }
10045
10046 /* Implement the "remove" breakpoint_ops method for hardware watchpoints.  */
10047
10048 static int
10049 remove_watchpoint (struct bp_location *bl)
10050 {
10051   struct watchpoint *w = (struct watchpoint *) bl->owner;
10052   int length = w->exact ? 1 : bl->length;
10053
10054   return target_remove_watchpoint (bl->address, length, bl->watchpoint_type,
10055                                    w->cond_exp);
10056 }
10057
10058 static int
10059 breakpoint_hit_watchpoint (const struct bp_location *bl,
10060                            struct address_space *aspace, CORE_ADDR bp_addr,
10061                            const struct target_waitstatus *ws)
10062 {
10063   struct breakpoint *b = bl->owner;
10064   struct watchpoint *w = (struct watchpoint *) b;
10065
10066   /* Continuable hardware watchpoints are treated as non-existent if the
10067      reason we stopped wasn't a hardware watchpoint (we didn't stop on
10068      some data address).  Otherwise gdb won't stop on a break instruction
10069      in the code (not from a breakpoint) when a hardware watchpoint has
10070      been defined.  Also skip watchpoints which we know did not trigger
10071      (did not match the data address).  */
10072   if (is_hardware_watchpoint (b)
10073       && w->watchpoint_triggered == watch_triggered_no)
10074     return 0;
10075
10076   return 1;
10077 }
10078
10079 static void
10080 check_status_watchpoint (bpstat bs)
10081 {
10082   gdb_assert (is_watchpoint (bs->breakpoint_at));
10083
10084   bpstat_check_watchpoint (bs);
10085 }
10086
10087 /* Implement the "resources_needed" breakpoint_ops method for
10088    hardware watchpoints.  */
10089
10090 static int
10091 resources_needed_watchpoint (const struct bp_location *bl)
10092 {
10093   struct watchpoint *w = (struct watchpoint *) bl->owner;
10094   int length = w->exact? 1 : bl->length;
10095
10096   return target_region_ok_for_hw_watchpoint (bl->address, length);
10097 }
10098
10099 /* Implement the "works_in_software_mode" breakpoint_ops method for
10100    hardware watchpoints.  */
10101
10102 static int
10103 works_in_software_mode_watchpoint (const struct breakpoint *b)
10104 {
10105   /* Read and access watchpoints only work with hardware support.  */
10106   return b->type == bp_watchpoint || b->type == bp_hardware_watchpoint;
10107 }
10108
10109 static enum print_stop_action
10110 print_it_watchpoint (bpstat bs)
10111 {
10112   struct cleanup *old_chain;
10113   struct breakpoint *b;
10114   const struct bp_location *bl;
10115   struct ui_file *stb;
10116   enum print_stop_action result;
10117   struct watchpoint *w;
10118   struct ui_out *uiout = current_uiout;
10119
10120   gdb_assert (bs->bp_location_at != NULL);
10121
10122   bl = bs->bp_location_at;
10123   b = bs->breakpoint_at;
10124   w = (struct watchpoint *) b;
10125
10126   stb = mem_fileopen ();
10127   old_chain = make_cleanup_ui_file_delete (stb);
10128
10129   switch (b->type)
10130     {
10131     case bp_watchpoint:
10132     case bp_hardware_watchpoint:
10133       annotate_watchpoint (b->number);
10134       if (ui_out_is_mi_like_p (uiout))
10135         ui_out_field_string
10136           (uiout, "reason",
10137            async_reason_lookup (EXEC_ASYNC_WATCHPOINT_TRIGGER));
10138       mention (b);
10139       make_cleanup_ui_out_tuple_begin_end (uiout, "value");
10140       ui_out_text (uiout, "\nOld value = ");
10141       watchpoint_value_print (bs->old_val, stb);
10142       ui_out_field_stream (uiout, "old", stb);
10143       ui_out_text (uiout, "\nNew value = ");
10144       watchpoint_value_print (w->val, stb);
10145       ui_out_field_stream (uiout, "new", stb);
10146       ui_out_text (uiout, "\n");
10147       /* More than one watchpoint may have been triggered.  */
10148       result = PRINT_UNKNOWN;
10149       break;
10150
10151     case bp_read_watchpoint:
10152       if (ui_out_is_mi_like_p (uiout))
10153         ui_out_field_string
10154           (uiout, "reason",
10155            async_reason_lookup (EXEC_ASYNC_READ_WATCHPOINT_TRIGGER));
10156       mention (b);
10157       make_cleanup_ui_out_tuple_begin_end (uiout, "value");
10158       ui_out_text (uiout, "\nValue = ");
10159       watchpoint_value_print (w->val, stb);
10160       ui_out_field_stream (uiout, "value", stb);
10161       ui_out_text (uiout, "\n");
10162       result = PRINT_UNKNOWN;
10163       break;
10164
10165     case bp_access_watchpoint:
10166       if (bs->old_val != NULL)
10167         {
10168           annotate_watchpoint (b->number);
10169           if (ui_out_is_mi_like_p (uiout))
10170             ui_out_field_string
10171               (uiout, "reason",
10172                async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
10173           mention (b);
10174           make_cleanup_ui_out_tuple_begin_end (uiout, "value");
10175           ui_out_text (uiout, "\nOld value = ");
10176           watchpoint_value_print (bs->old_val, stb);
10177           ui_out_field_stream (uiout, "old", stb);
10178           ui_out_text (uiout, "\nNew value = ");
10179         }
10180       else
10181         {
10182           mention (b);
10183           if (ui_out_is_mi_like_p (uiout))
10184             ui_out_field_string
10185               (uiout, "reason",
10186                async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
10187           make_cleanup_ui_out_tuple_begin_end (uiout, "value");
10188           ui_out_text (uiout, "\nValue = ");
10189         }
10190       watchpoint_value_print (w->val, stb);
10191       ui_out_field_stream (uiout, "new", stb);
10192       ui_out_text (uiout, "\n");
10193       result = PRINT_UNKNOWN;
10194       break;
10195     default:
10196       result = PRINT_UNKNOWN;
10197     }
10198
10199   do_cleanups (old_chain);
10200   return result;
10201 }
10202
10203 /* Implement the "print_mention" breakpoint_ops method for hardware
10204    watchpoints.  */
10205
10206 static void
10207 print_mention_watchpoint (struct breakpoint *b)
10208 {
10209   struct cleanup *ui_out_chain;
10210   struct watchpoint *w = (struct watchpoint *) b;
10211   struct ui_out *uiout = current_uiout;
10212
10213   switch (b->type)
10214     {
10215     case bp_watchpoint:
10216       ui_out_text (uiout, "Watchpoint ");
10217       ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "wpt");
10218       break;
10219     case bp_hardware_watchpoint:
10220       ui_out_text (uiout, "Hardware watchpoint ");
10221       ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "wpt");
10222       break;
10223     case bp_read_watchpoint:
10224       ui_out_text (uiout, "Hardware read watchpoint ");
10225       ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "hw-rwpt");
10226       break;
10227     case bp_access_watchpoint:
10228       ui_out_text (uiout, "Hardware access (read/write) watchpoint ");
10229       ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "hw-awpt");
10230       break;
10231     default:
10232       internal_error (__FILE__, __LINE__,
10233                       _("Invalid hardware watchpoint type."));
10234     }
10235
10236   ui_out_field_int (uiout, "number", b->number);
10237   ui_out_text (uiout, ": ");
10238   ui_out_field_string (uiout, "exp", w->exp_string);
10239   do_cleanups (ui_out_chain);
10240 }
10241
10242 /* Implement the "print_recreate" breakpoint_ops method for
10243    watchpoints.  */
10244
10245 static void
10246 print_recreate_watchpoint (struct breakpoint *b, struct ui_file *fp)
10247 {
10248   struct watchpoint *w = (struct watchpoint *) b;
10249
10250   switch (b->type)
10251     {
10252     case bp_watchpoint:
10253     case bp_hardware_watchpoint:
10254       fprintf_unfiltered (fp, "watch");
10255       break;
10256     case bp_read_watchpoint:
10257       fprintf_unfiltered (fp, "rwatch");
10258       break;
10259     case bp_access_watchpoint:
10260       fprintf_unfiltered (fp, "awatch");
10261       break;
10262     default:
10263       internal_error (__FILE__, __LINE__,
10264                       _("Invalid watchpoint type."));
10265     }
10266
10267   fprintf_unfiltered (fp, " %s", w->exp_string);
10268   print_recreate_thread (b, fp);
10269 }
10270
10271 /* The breakpoint_ops structure to be used in hardware watchpoints.  */
10272
10273 static struct breakpoint_ops watchpoint_breakpoint_ops;
10274
10275 /* Implement the "insert" breakpoint_ops method for
10276    masked hardware watchpoints.  */
10277
10278 static int
10279 insert_masked_watchpoint (struct bp_location *bl)
10280 {
10281   struct watchpoint *w = (struct watchpoint *) bl->owner;
10282
10283   return target_insert_mask_watchpoint (bl->address, w->hw_wp_mask,
10284                                         bl->watchpoint_type);
10285 }
10286
10287 /* Implement the "remove" breakpoint_ops method for
10288    masked hardware watchpoints.  */
10289
10290 static int
10291 remove_masked_watchpoint (struct bp_location *bl)
10292 {
10293   struct watchpoint *w = (struct watchpoint *) bl->owner;
10294
10295   return target_remove_mask_watchpoint (bl->address, w->hw_wp_mask,
10296                                         bl->watchpoint_type);
10297 }
10298
10299 /* Implement the "resources_needed" breakpoint_ops method for
10300    masked hardware watchpoints.  */
10301
10302 static int
10303 resources_needed_masked_watchpoint (const struct bp_location *bl)
10304 {
10305   struct watchpoint *w = (struct watchpoint *) bl->owner;
10306
10307   return target_masked_watch_num_registers (bl->address, w->hw_wp_mask);
10308 }
10309
10310 /* Implement the "works_in_software_mode" breakpoint_ops method for
10311    masked hardware watchpoints.  */
10312
10313 static int
10314 works_in_software_mode_masked_watchpoint (const struct breakpoint *b)
10315 {
10316   return 0;
10317 }
10318
10319 /* Implement the "print_it" breakpoint_ops method for
10320    masked hardware watchpoints.  */
10321
10322 static enum print_stop_action
10323 print_it_masked_watchpoint (bpstat bs)
10324 {
10325   struct breakpoint *b = bs->breakpoint_at;
10326   struct ui_out *uiout = current_uiout;
10327
10328   /* Masked watchpoints have only one location.  */
10329   gdb_assert (b->loc && b->loc->next == NULL);
10330
10331   switch (b->type)
10332     {
10333     case bp_hardware_watchpoint:
10334       annotate_watchpoint (b->number);
10335       if (ui_out_is_mi_like_p (uiout))
10336         ui_out_field_string
10337           (uiout, "reason",
10338            async_reason_lookup (EXEC_ASYNC_WATCHPOINT_TRIGGER));
10339       break;
10340
10341     case bp_read_watchpoint:
10342       if (ui_out_is_mi_like_p (uiout))
10343         ui_out_field_string
10344           (uiout, "reason",
10345            async_reason_lookup (EXEC_ASYNC_READ_WATCHPOINT_TRIGGER));
10346       break;
10347
10348     case bp_access_watchpoint:
10349       if (ui_out_is_mi_like_p (uiout))
10350         ui_out_field_string
10351           (uiout, "reason",
10352            async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
10353       break;
10354     default:
10355       internal_error (__FILE__, __LINE__,
10356                       _("Invalid hardware watchpoint type."));
10357     }
10358
10359   mention (b);
10360   ui_out_text (uiout, _("\n\
10361 Check the underlying instruction at PC for the memory\n\
10362 address and value which triggered this watchpoint.\n"));
10363   ui_out_text (uiout, "\n");
10364
10365   /* More than one watchpoint may have been triggered.  */
10366   return PRINT_UNKNOWN;
10367 }
10368
10369 /* Implement the "print_one_detail" breakpoint_ops method for
10370    masked hardware watchpoints.  */
10371
10372 static void
10373 print_one_detail_masked_watchpoint (const struct breakpoint *b,
10374                                     struct ui_out *uiout)
10375 {
10376   struct watchpoint *w = (struct watchpoint *) b;
10377
10378   /* Masked watchpoints have only one location.  */
10379   gdb_assert (b->loc && b->loc->next == NULL);
10380
10381   ui_out_text (uiout, "\tmask ");
10382   ui_out_field_core_addr (uiout, "mask", b->loc->gdbarch, w->hw_wp_mask);
10383   ui_out_text (uiout, "\n");
10384 }
10385
10386 /* Implement the "print_mention" breakpoint_ops method for
10387    masked hardware watchpoints.  */
10388
10389 static void
10390 print_mention_masked_watchpoint (struct breakpoint *b)
10391 {
10392   struct watchpoint *w = (struct watchpoint *) b;
10393   struct ui_out *uiout = current_uiout;
10394   struct cleanup *ui_out_chain;
10395
10396   switch (b->type)
10397     {
10398     case bp_hardware_watchpoint:
10399       ui_out_text (uiout, "Masked hardware watchpoint ");
10400       ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "wpt");
10401       break;
10402     case bp_read_watchpoint:
10403       ui_out_text (uiout, "Masked hardware read watchpoint ");
10404       ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "hw-rwpt");
10405       break;
10406     case bp_access_watchpoint:
10407       ui_out_text (uiout, "Masked hardware access (read/write) watchpoint ");
10408       ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "hw-awpt");
10409       break;
10410     default:
10411       internal_error (__FILE__, __LINE__,
10412                       _("Invalid hardware watchpoint type."));
10413     }
10414
10415   ui_out_field_int (uiout, "number", b->number);
10416   ui_out_text (uiout, ": ");
10417   ui_out_field_string (uiout, "exp", w->exp_string);
10418   do_cleanups (ui_out_chain);
10419 }
10420
10421 /* Implement the "print_recreate" breakpoint_ops method for
10422    masked hardware watchpoints.  */
10423
10424 static void
10425 print_recreate_masked_watchpoint (struct breakpoint *b, struct ui_file *fp)
10426 {
10427   struct watchpoint *w = (struct watchpoint *) b;
10428   char tmp[40];
10429
10430   switch (b->type)
10431     {
10432     case bp_hardware_watchpoint:
10433       fprintf_unfiltered (fp, "watch");
10434       break;
10435     case bp_read_watchpoint:
10436       fprintf_unfiltered (fp, "rwatch");
10437       break;
10438     case bp_access_watchpoint:
10439       fprintf_unfiltered (fp, "awatch");
10440       break;
10441     default:
10442       internal_error (__FILE__, __LINE__,
10443                       _("Invalid hardware watchpoint type."));
10444     }
10445
10446   sprintf_vma (tmp, w->hw_wp_mask);
10447   fprintf_unfiltered (fp, " %s mask 0x%s", w->exp_string, tmp);
10448   print_recreate_thread (b, fp);
10449 }
10450
10451 /* The breakpoint_ops structure to be used in masked hardware watchpoints.  */
10452
10453 static struct breakpoint_ops masked_watchpoint_breakpoint_ops;
10454
10455 /* Tell whether the given watchpoint is a masked hardware watchpoint.  */
10456
10457 static int
10458 is_masked_watchpoint (const struct breakpoint *b)
10459 {
10460   return b->ops == &masked_watchpoint_breakpoint_ops;
10461 }
10462
10463 /* accessflag:  hw_write:  watch write, 
10464                 hw_read:   watch read, 
10465                 hw_access: watch access (read or write) */
10466 static void
10467 watch_command_1 (char *arg, int accessflag, int from_tty,
10468                  int just_location, int internal)
10469 {
10470   volatile struct gdb_exception e;
10471   struct breakpoint *b, *scope_breakpoint = NULL;
10472   struct expression *exp;
10473   struct block *exp_valid_block = NULL, *cond_exp_valid_block = NULL;
10474   struct value *val, *mark, *result;
10475   struct frame_info *frame;
10476   char *exp_start = NULL;
10477   char *exp_end = NULL;
10478   char *tok, *end_tok;
10479   int toklen = -1;
10480   char *cond_start = NULL;
10481   char *cond_end = NULL;
10482   enum bptype bp_type;
10483   int thread = -1;
10484   int pc = 0;
10485   /* Flag to indicate whether we are going to use masks for
10486      the hardware watchpoint.  */
10487   int use_mask = 0;
10488   CORE_ADDR mask = 0;
10489   struct watchpoint *w;
10490
10491   /* Make sure that we actually have parameters to parse.  */
10492   if (arg != NULL && arg[0] != '\0')
10493     {
10494       char *value_start;
10495
10496       /* Look for "parameter value" pairs at the end
10497          of the arguments string.  */
10498       for (tok = arg + strlen (arg) - 1; tok > arg; tok--)
10499         {
10500           /* Skip whitespace at the end of the argument list.  */
10501           while (tok > arg && (*tok == ' ' || *tok == '\t'))
10502             tok--;
10503
10504           /* Find the beginning of the last token.
10505              This is the value of the parameter.  */
10506           while (tok > arg && (*tok != ' ' && *tok != '\t'))
10507             tok--;
10508           value_start = tok + 1;
10509
10510           /* Skip whitespace.  */
10511           while (tok > arg && (*tok == ' ' || *tok == '\t'))
10512             tok--;
10513
10514           end_tok = tok;
10515
10516           /* Find the beginning of the second to last token.
10517              This is the parameter itself.  */
10518           while (tok > arg && (*tok != ' ' && *tok != '\t'))
10519             tok--;
10520           tok++;
10521           toklen = end_tok - tok + 1;
10522
10523           if (toklen == 6 && !strncmp (tok, "thread", 6))
10524             {
10525               /* At this point we've found a "thread" token, which means
10526                  the user is trying to set a watchpoint that triggers
10527                  only in a specific thread.  */
10528               char *endp;
10529
10530               if (thread != -1)
10531                 error(_("You can specify only one thread."));
10532
10533               /* Extract the thread ID from the next token.  */
10534               thread = strtol (value_start, &endp, 0);
10535
10536               /* Check if the user provided a valid numeric value for the
10537                  thread ID.  */
10538               if (*endp != ' ' && *endp != '\t' && *endp != '\0')
10539                 error (_("Invalid thread ID specification %s."), value_start);
10540
10541               /* Check if the thread actually exists.  */
10542               if (!valid_thread_id (thread))
10543                 error (_("Unknown thread %d."), thread);
10544             }
10545           else if (toklen == 4 && !strncmp (tok, "mask", 4))
10546             {
10547               /* We've found a "mask" token, which means the user wants to
10548                  create a hardware watchpoint that is going to have the mask
10549                  facility.  */
10550               struct value *mask_value, *mark;
10551
10552               if (use_mask)
10553                 error(_("You can specify only one mask."));
10554
10555               use_mask = just_location = 1;
10556
10557               mark = value_mark ();
10558               mask_value = parse_to_comma_and_eval (&value_start);
10559               mask = value_as_address (mask_value);
10560               value_free_to_mark (mark);
10561             }
10562           else
10563             /* We didn't recognize what we found.  We should stop here.  */
10564             break;
10565
10566           /* Truncate the string and get rid of the "parameter value" pair before
10567              the arguments string is parsed by the parse_exp_1 function.  */
10568           *tok = '\0';
10569         }
10570     }
10571
10572   /* Parse the rest of the arguments.  */
10573   innermost_block = NULL;
10574   exp_start = arg;
10575   exp = parse_exp_1 (&arg, 0, 0);
10576   exp_end = arg;
10577   /* Remove trailing whitespace from the expression before saving it.
10578      This makes the eventual display of the expression string a bit
10579      prettier.  */
10580   while (exp_end > exp_start && (exp_end[-1] == ' ' || exp_end[-1] == '\t'))
10581     --exp_end;
10582
10583   /* Checking if the expression is not constant.  */
10584   if (watchpoint_exp_is_const (exp))
10585     {
10586       int len;
10587
10588       len = exp_end - exp_start;
10589       while (len > 0 && isspace (exp_start[len - 1]))
10590         len--;
10591       error (_("Cannot watch constant value `%.*s'."), len, exp_start);
10592     }
10593
10594   exp_valid_block = innermost_block;
10595   mark = value_mark ();
10596   fetch_subexp_value (exp, &pc, &val, &result, NULL);
10597
10598   if (just_location)
10599     {
10600       int ret;
10601
10602       exp_valid_block = NULL;
10603       val = value_addr (result);
10604       release_value (val);
10605       value_free_to_mark (mark);
10606
10607       if (use_mask)
10608         {
10609           ret = target_masked_watch_num_registers (value_as_address (val),
10610                                                    mask);
10611           if (ret == -1)
10612             error (_("This target does not support masked watchpoints."));
10613           else if (ret == -2)
10614             error (_("Invalid mask or memory region."));
10615         }
10616     }
10617   else if (val != NULL)
10618     release_value (val);
10619
10620   tok = skip_spaces (arg);
10621   end_tok = skip_to_space (tok);
10622
10623   toklen = end_tok - tok;
10624   if (toklen >= 1 && strncmp (tok, "if", toklen) == 0)
10625     {
10626       struct expression *cond;
10627
10628       innermost_block = NULL;
10629       tok = cond_start = end_tok + 1;
10630       cond = parse_exp_1 (&tok, 0, 0);
10631
10632       /* The watchpoint expression may not be local, but the condition
10633          may still be.  E.g.: `watch global if local > 0'.  */
10634       cond_exp_valid_block = innermost_block;
10635
10636       xfree (cond);
10637       cond_end = tok;
10638     }
10639   if (*tok)
10640     error (_("Junk at end of command."));
10641
10642   if (accessflag == hw_read)
10643     bp_type = bp_read_watchpoint;
10644   else if (accessflag == hw_access)
10645     bp_type = bp_access_watchpoint;
10646   else
10647     bp_type = bp_hardware_watchpoint;
10648
10649   frame = block_innermost_frame (exp_valid_block);
10650
10651   /* If the expression is "local", then set up a "watchpoint scope"
10652      breakpoint at the point where we've left the scope of the watchpoint
10653      expression.  Create the scope breakpoint before the watchpoint, so
10654      that we will encounter it first in bpstat_stop_status.  */
10655   if (exp_valid_block && frame)
10656     {
10657       if (frame_id_p (frame_unwind_caller_id (frame)))
10658         {
10659           scope_breakpoint
10660             = create_internal_breakpoint (frame_unwind_caller_arch (frame),
10661                                           frame_unwind_caller_pc (frame),
10662                                           bp_watchpoint_scope,
10663                                           &momentary_breakpoint_ops);
10664
10665           scope_breakpoint->enable_state = bp_enabled;
10666
10667           /* Automatically delete the breakpoint when it hits.  */
10668           scope_breakpoint->disposition = disp_del;
10669
10670           /* Only break in the proper frame (help with recursion).  */
10671           scope_breakpoint->frame_id = frame_unwind_caller_id (frame);
10672
10673           /* Set the address at which we will stop.  */
10674           scope_breakpoint->loc->gdbarch
10675             = frame_unwind_caller_arch (frame);
10676           scope_breakpoint->loc->requested_address
10677             = frame_unwind_caller_pc (frame);
10678           scope_breakpoint->loc->address
10679             = adjust_breakpoint_address (scope_breakpoint->loc->gdbarch,
10680                                          scope_breakpoint->loc->requested_address,
10681                                          scope_breakpoint->type);
10682         }
10683     }
10684
10685   /* Now set up the breakpoint.  */
10686
10687   w = XCNEW (struct watchpoint);
10688   b = &w->base;
10689   if (use_mask)
10690     init_raw_breakpoint_without_location (b, NULL, bp_type,
10691                                           &masked_watchpoint_breakpoint_ops);
10692   else
10693     init_raw_breakpoint_without_location (b, NULL, bp_type,
10694                                           &watchpoint_breakpoint_ops);
10695   b->thread = thread;
10696   b->disposition = disp_donttouch;
10697   b->pspace = current_program_space;
10698   w->exp = exp;
10699   w->exp_valid_block = exp_valid_block;
10700   w->cond_exp_valid_block = cond_exp_valid_block;
10701   if (just_location)
10702     {
10703       struct type *t = value_type (val);
10704       CORE_ADDR addr = value_as_address (val);
10705       char *name;
10706
10707       t = check_typedef (TYPE_TARGET_TYPE (check_typedef (t)));
10708       name = type_to_string (t);
10709
10710       w->exp_string_reparse = xstrprintf ("* (%s *) %s", name,
10711                                           core_addr_to_string (addr));
10712       xfree (name);
10713
10714       w->exp_string = xstrprintf ("-location %.*s",
10715                                   (int) (exp_end - exp_start), exp_start);
10716
10717       /* The above expression is in C.  */
10718       b->language = language_c;
10719     }
10720   else
10721     w->exp_string = savestring (exp_start, exp_end - exp_start);
10722
10723   if (use_mask)
10724     {
10725       w->hw_wp_mask = mask;
10726     }
10727   else
10728     {
10729       w->val = val;
10730       w->val_valid = 1;
10731     }
10732
10733   if (cond_start)
10734     b->cond_string = savestring (cond_start, cond_end - cond_start);
10735   else
10736     b->cond_string = 0;
10737
10738   if (frame)
10739     {
10740       w->watchpoint_frame = get_frame_id (frame);
10741       w->watchpoint_thread = inferior_ptid;
10742     }
10743   else
10744     {
10745       w->watchpoint_frame = null_frame_id;
10746       w->watchpoint_thread = null_ptid;
10747     }
10748
10749   if (scope_breakpoint != NULL)
10750     {
10751       /* The scope breakpoint is related to the watchpoint.  We will
10752          need to act on them together.  */
10753       b->related_breakpoint = scope_breakpoint;
10754       scope_breakpoint->related_breakpoint = b;
10755     }
10756
10757   if (!just_location)
10758     value_free_to_mark (mark);
10759
10760   TRY_CATCH (e, RETURN_MASK_ALL)
10761     {
10762       /* Finally update the new watchpoint.  This creates the locations
10763          that should be inserted.  */
10764       update_watchpoint (w, 1);
10765     }
10766   if (e.reason < 0)
10767     {
10768       delete_breakpoint (b);
10769       throw_exception (e);
10770     }
10771
10772   install_breakpoint (internal, b, 1);
10773 }
10774
10775 /* Return count of debug registers needed to watch the given expression.
10776    If the watchpoint cannot be handled in hardware return zero.  */
10777
10778 static int
10779 can_use_hardware_watchpoint (struct value *v)
10780 {
10781   int found_memory_cnt = 0;
10782   struct value *head = v;
10783
10784   /* Did the user specifically forbid us to use hardware watchpoints? */
10785   if (!can_use_hw_watchpoints)
10786     return 0;
10787
10788   /* Make sure that the value of the expression depends only upon
10789      memory contents, and values computed from them within GDB.  If we
10790      find any register references or function calls, we can't use a
10791      hardware watchpoint.
10792
10793      The idea here is that evaluating an expression generates a series
10794      of values, one holding the value of every subexpression.  (The
10795      expression a*b+c has five subexpressions: a, b, a*b, c, and
10796      a*b+c.)  GDB's values hold almost enough information to establish
10797      the criteria given above --- they identify memory lvalues,
10798      register lvalues, computed values, etcetera.  So we can evaluate
10799      the expression, and then scan the chain of values that leaves
10800      behind to decide whether we can detect any possible change to the
10801      expression's final value using only hardware watchpoints.
10802
10803      However, I don't think that the values returned by inferior
10804      function calls are special in any way.  So this function may not
10805      notice that an expression involving an inferior function call
10806      can't be watched with hardware watchpoints.  FIXME.  */
10807   for (; v; v = value_next (v))
10808     {
10809       if (VALUE_LVAL (v) == lval_memory)
10810         {
10811           if (v != head && value_lazy (v))
10812             /* A lazy memory lvalue in the chain is one that GDB never
10813                needed to fetch; we either just used its address (e.g.,
10814                `a' in `a.b') or we never needed it at all (e.g., `a'
10815                in `a,b').  This doesn't apply to HEAD; if that is
10816                lazy then it was not readable, but watch it anyway.  */
10817             ;
10818           else
10819             {
10820               /* Ahh, memory we actually used!  Check if we can cover
10821                  it with hardware watchpoints.  */
10822               struct type *vtype = check_typedef (value_type (v));
10823
10824               /* We only watch structs and arrays if user asked for it
10825                  explicitly, never if they just happen to appear in a
10826                  middle of some value chain.  */
10827               if (v == head
10828                   || (TYPE_CODE (vtype) != TYPE_CODE_STRUCT
10829                       && TYPE_CODE (vtype) != TYPE_CODE_ARRAY))
10830                 {
10831                   CORE_ADDR vaddr = value_address (v);
10832                   int len;
10833                   int num_regs;
10834
10835                   len = (target_exact_watchpoints
10836                          && is_scalar_type_recursive (vtype))?
10837                     1 : TYPE_LENGTH (value_type (v));
10838
10839                   num_regs = target_region_ok_for_hw_watchpoint (vaddr, len);
10840                   if (!num_regs)
10841                     return 0;
10842                   else
10843                     found_memory_cnt += num_regs;
10844                 }
10845             }
10846         }
10847       else if (VALUE_LVAL (v) != not_lval
10848                && deprecated_value_modifiable (v) == 0)
10849         return 0;       /* These are values from the history (e.g., $1).  */
10850       else if (VALUE_LVAL (v) == lval_register)
10851         return 0;       /* Cannot watch a register with a HW watchpoint.  */
10852     }
10853
10854   /* The expression itself looks suitable for using a hardware
10855      watchpoint, but give the target machine a chance to reject it.  */
10856   return found_memory_cnt;
10857 }
10858
10859 void
10860 watch_command_wrapper (char *arg, int from_tty, int internal)
10861 {
10862   watch_command_1 (arg, hw_write, from_tty, 0, internal);
10863 }
10864
10865 /* A helper function that looks for an argument at the start of a
10866    string.  The argument must also either be at the end of the string,
10867    or be followed by whitespace.  Returns 1 if it finds the argument,
10868    0 otherwise.  If the argument is found, it updates *STR.  */
10869
10870 static int
10871 check_for_argument (char **str, char *arg, int arg_len)
10872 {
10873   if (strncmp (*str, arg, arg_len) == 0
10874       && ((*str)[arg_len] == '\0' || isspace ((*str)[arg_len])))
10875     {
10876       *str += arg_len;
10877       return 1;
10878     }
10879   return 0;
10880 }
10881
10882 /* A helper function that looks for the "-location" argument and then
10883    calls watch_command_1.  */
10884
10885 static void
10886 watch_maybe_just_location (char *arg, int accessflag, int from_tty)
10887 {
10888   int just_location = 0;
10889
10890   if (arg
10891       && (check_for_argument (&arg, "-location", sizeof ("-location") - 1)
10892           || check_for_argument (&arg, "-l", sizeof ("-l") - 1)))
10893     {
10894       arg = skip_spaces (arg);
10895       just_location = 1;
10896     }
10897
10898   watch_command_1 (arg, accessflag, from_tty, just_location, 0);
10899 }
10900
10901 static void
10902 watch_command (char *arg, int from_tty)
10903 {
10904   watch_maybe_just_location (arg, hw_write, from_tty);
10905 }
10906
10907 void
10908 rwatch_command_wrapper (char *arg, int from_tty, int internal)
10909 {
10910   watch_command_1 (arg, hw_read, from_tty, 0, internal);
10911 }
10912
10913 static void
10914 rwatch_command (char *arg, int from_tty)
10915 {
10916   watch_maybe_just_location (arg, hw_read, from_tty);
10917 }
10918
10919 void
10920 awatch_command_wrapper (char *arg, int from_tty, int internal)
10921 {
10922   watch_command_1 (arg, hw_access, from_tty, 0, internal);
10923 }
10924
10925 static void
10926 awatch_command (char *arg, int from_tty)
10927 {
10928   watch_maybe_just_location (arg, hw_access, from_tty);
10929 }
10930 \f
10931
10932 /* Helper routines for the until_command routine in infcmd.c.  Here
10933    because it uses the mechanisms of breakpoints.  */
10934
10935 struct until_break_command_continuation_args
10936 {
10937   struct breakpoint *breakpoint;
10938   struct breakpoint *breakpoint2;
10939   int thread_num;
10940 };
10941
10942 /* This function is called by fetch_inferior_event via the
10943    cmd_continuation pointer, to complete the until command.  It takes
10944    care of cleaning up the temporary breakpoints set up by the until
10945    command.  */
10946 static void
10947 until_break_command_continuation (void *arg, int err)
10948 {
10949   struct until_break_command_continuation_args *a = arg;
10950
10951   delete_breakpoint (a->breakpoint);
10952   if (a->breakpoint2)
10953     delete_breakpoint (a->breakpoint2);
10954   delete_longjmp_breakpoint (a->thread_num);
10955 }
10956
10957 void
10958 until_break_command (char *arg, int from_tty, int anywhere)
10959 {
10960   struct symtabs_and_lines sals;
10961   struct symtab_and_line sal;
10962   struct frame_info *frame;
10963   struct gdbarch *frame_gdbarch;
10964   struct frame_id stack_frame_id;
10965   struct frame_id caller_frame_id;
10966   struct breakpoint *breakpoint;
10967   struct breakpoint *breakpoint2 = NULL;
10968   struct cleanup *old_chain;
10969   int thread;
10970   struct thread_info *tp;
10971
10972   clear_proceed_status ();
10973
10974   /* Set a breakpoint where the user wants it and at return from
10975      this function.  */
10976
10977   if (last_displayed_sal_is_valid ())
10978     sals = decode_line_1 (&arg, DECODE_LINE_FUNFIRSTLINE,
10979                           get_last_displayed_symtab (),
10980                           get_last_displayed_line ());
10981   else
10982     sals = decode_line_1 (&arg, DECODE_LINE_FUNFIRSTLINE,
10983                           (struct symtab *) NULL, 0);
10984
10985   if (sals.nelts != 1)
10986     error (_("Couldn't get information on specified line."));
10987
10988   sal = sals.sals[0];
10989   xfree (sals.sals);    /* malloc'd, so freed.  */
10990
10991   if (*arg)
10992     error (_("Junk at end of arguments."));
10993
10994   resolve_sal_pc (&sal);
10995
10996   tp = inferior_thread ();
10997   thread = tp->num;
10998
10999   old_chain = make_cleanup (null_cleanup, NULL);
11000
11001   /* Note linespec handling above invalidates the frame chain.
11002      Installing a breakpoint also invalidates the frame chain (as it
11003      may need to switch threads), so do any frame handling before
11004      that.  */
11005
11006   frame = get_selected_frame (NULL);
11007   frame_gdbarch = get_frame_arch (frame);
11008   stack_frame_id = get_stack_frame_id (frame);
11009   caller_frame_id = frame_unwind_caller_id (frame);
11010
11011   /* Keep within the current frame, or in frames called by the current
11012      one.  */
11013
11014   if (frame_id_p (caller_frame_id))
11015     {
11016       struct symtab_and_line sal2;
11017
11018       sal2 = find_pc_line (frame_unwind_caller_pc (frame), 0);
11019       sal2.pc = frame_unwind_caller_pc (frame);
11020       breakpoint2 = set_momentary_breakpoint (frame_unwind_caller_arch (frame),
11021                                               sal2,
11022                                               caller_frame_id,
11023                                               bp_until);
11024       make_cleanup_delete_breakpoint (breakpoint2);
11025
11026       set_longjmp_breakpoint (tp, caller_frame_id);
11027       make_cleanup (delete_longjmp_breakpoint_cleanup, &thread);
11028     }
11029
11030   /* set_momentary_breakpoint could invalidate FRAME.  */
11031   frame = NULL;
11032
11033   if (anywhere)
11034     /* If the user told us to continue until a specified location,
11035        we don't specify a frame at which we need to stop.  */
11036     breakpoint = set_momentary_breakpoint (frame_gdbarch, sal,
11037                                            null_frame_id, bp_until);
11038   else
11039     /* Otherwise, specify the selected frame, because we want to stop
11040        only at the very same frame.  */
11041     breakpoint = set_momentary_breakpoint (frame_gdbarch, sal,
11042                                            stack_frame_id, bp_until);
11043   make_cleanup_delete_breakpoint (breakpoint);
11044
11045   proceed (-1, GDB_SIGNAL_DEFAULT, 0);
11046
11047   /* If we are running asynchronously, and proceed call above has
11048      actually managed to start the target, arrange for breakpoints to
11049      be deleted when the target stops.  Otherwise, we're already
11050      stopped and delete breakpoints via cleanup chain.  */
11051
11052   if (target_can_async_p () && is_running (inferior_ptid))
11053     {
11054       struct until_break_command_continuation_args *args;
11055       args = xmalloc (sizeof (*args));
11056
11057       args->breakpoint = breakpoint;
11058       args->breakpoint2 = breakpoint2;
11059       args->thread_num = thread;
11060
11061       discard_cleanups (old_chain);
11062       add_continuation (inferior_thread (),
11063                         until_break_command_continuation, args,
11064                         xfree);
11065     }
11066   else
11067     do_cleanups (old_chain);
11068 }
11069
11070 /* This function attempts to parse an optional "if <cond>" clause
11071    from the arg string.  If one is not found, it returns NULL.
11072
11073    Else, it returns a pointer to the condition string.  (It does not
11074    attempt to evaluate the string against a particular block.)  And,
11075    it updates arg to point to the first character following the parsed
11076    if clause in the arg string.  */
11077
11078 static char *
11079 ep_parse_optional_if_clause (char **arg)
11080 {
11081   char *cond_string;
11082
11083   if (((*arg)[0] != 'i') || ((*arg)[1] != 'f') || !isspace ((*arg)[2]))
11084     return NULL;
11085
11086   /* Skip the "if" keyword.  */
11087   (*arg) += 2;
11088
11089   /* Skip any extra leading whitespace, and record the start of the
11090      condition string.  */
11091   *arg = skip_spaces (*arg);
11092   cond_string = *arg;
11093
11094   /* Assume that the condition occupies the remainder of the arg
11095      string.  */
11096   (*arg) += strlen (cond_string);
11097
11098   return cond_string;
11099 }
11100
11101 /* Commands to deal with catching events, such as signals, exceptions,
11102    process start/exit, etc.  */
11103
11104 typedef enum
11105 {
11106   catch_fork_temporary, catch_vfork_temporary,
11107   catch_fork_permanent, catch_vfork_permanent
11108 }
11109 catch_fork_kind;
11110
11111 static void
11112 catch_fork_command_1 (char *arg, int from_tty, 
11113                       struct cmd_list_element *command)
11114 {
11115   struct gdbarch *gdbarch = get_current_arch ();
11116   char *cond_string = NULL;
11117   catch_fork_kind fork_kind;
11118   int tempflag;
11119
11120   fork_kind = (catch_fork_kind) (uintptr_t) get_cmd_context (command);
11121   tempflag = (fork_kind == catch_fork_temporary
11122               || fork_kind == catch_vfork_temporary);
11123
11124   if (!arg)
11125     arg = "";
11126   arg = skip_spaces (arg);
11127
11128   /* The allowed syntax is:
11129      catch [v]fork
11130      catch [v]fork if <cond>
11131
11132      First, check if there's an if clause.  */
11133   cond_string = ep_parse_optional_if_clause (&arg);
11134
11135   if ((*arg != '\0') && !isspace (*arg))
11136     error (_("Junk at end of arguments."));
11137
11138   /* If this target supports it, create a fork or vfork catchpoint
11139      and enable reporting of such events.  */
11140   switch (fork_kind)
11141     {
11142     case catch_fork_temporary:
11143     case catch_fork_permanent:
11144       create_fork_vfork_event_catchpoint (gdbarch, tempflag, cond_string,
11145                                           &catch_fork_breakpoint_ops);
11146       break;
11147     case catch_vfork_temporary:
11148     case catch_vfork_permanent:
11149       create_fork_vfork_event_catchpoint (gdbarch, tempflag, cond_string,
11150                                           &catch_vfork_breakpoint_ops);
11151       break;
11152     default:
11153       error (_("unsupported or unknown fork kind; cannot catch it"));
11154       break;
11155     }
11156 }
11157
11158 static void
11159 catch_exec_command_1 (char *arg, int from_tty, 
11160                       struct cmd_list_element *command)
11161 {
11162   struct exec_catchpoint *c;
11163   struct gdbarch *gdbarch = get_current_arch ();
11164   int tempflag;
11165   char *cond_string = NULL;
11166
11167   tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
11168
11169   if (!arg)
11170     arg = "";
11171   arg = skip_spaces (arg);
11172
11173   /* The allowed syntax is:
11174      catch exec
11175      catch exec if <cond>
11176
11177      First, check if there's an if clause.  */
11178   cond_string = ep_parse_optional_if_clause (&arg);
11179
11180   if ((*arg != '\0') && !isspace (*arg))
11181     error (_("Junk at end of arguments."));
11182
11183   c = XNEW (struct exec_catchpoint);
11184   init_catchpoint (&c->base, gdbarch, tempflag, cond_string,
11185                    &catch_exec_breakpoint_ops);
11186   c->exec_pathname = NULL;
11187
11188   install_breakpoint (0, &c->base, 1);
11189 }
11190
11191 static enum print_stop_action
11192 print_it_exception_catchpoint (bpstat bs)
11193 {
11194   struct ui_out *uiout = current_uiout;
11195   struct breakpoint *b = bs->breakpoint_at;
11196   int bp_temp, bp_throw;
11197
11198   annotate_catchpoint (b->number);
11199
11200   bp_throw = strstr (b->addr_string, "throw") != NULL;
11201   if (b->loc->address != b->loc->requested_address)
11202     breakpoint_adjustment_warning (b->loc->requested_address,
11203                                    b->loc->address,
11204                                    b->number, 1);
11205   bp_temp = b->disposition == disp_del;
11206   ui_out_text (uiout, 
11207                bp_temp ? "Temporary catchpoint "
11208                        : "Catchpoint ");
11209   if (!ui_out_is_mi_like_p (uiout))
11210     ui_out_field_int (uiout, "bkptno", b->number);
11211   ui_out_text (uiout,
11212                bp_throw ? " (exception thrown), "
11213                         : " (exception caught), ");
11214   if (ui_out_is_mi_like_p (uiout))
11215     {
11216       ui_out_field_string (uiout, "reason", 
11217                            async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT));
11218       ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
11219       ui_out_field_int (uiout, "bkptno", b->number);
11220     }
11221   return PRINT_SRC_AND_LOC;
11222 }
11223
11224 static void
11225 print_one_exception_catchpoint (struct breakpoint *b, 
11226                                 struct bp_location **last_loc)
11227 {
11228   struct value_print_options opts;
11229   struct ui_out *uiout = current_uiout;
11230
11231   get_user_print_options (&opts);
11232   if (opts.addressprint)
11233     {
11234       annotate_field (4);
11235       if (b->loc == NULL || b->loc->shlib_disabled)
11236         ui_out_field_string (uiout, "addr", "<PENDING>");
11237       else
11238         ui_out_field_core_addr (uiout, "addr",
11239                                 b->loc->gdbarch, b->loc->address);
11240     }
11241   annotate_field (5);
11242   if (b->loc)
11243     *last_loc = b->loc;
11244   if (strstr (b->addr_string, "throw") != NULL)
11245     ui_out_field_string (uiout, "what", "exception throw");
11246   else
11247     ui_out_field_string (uiout, "what", "exception catch");
11248 }
11249
11250 static void
11251 print_mention_exception_catchpoint (struct breakpoint *b)
11252 {
11253   struct ui_out *uiout = current_uiout;
11254   int bp_temp;
11255   int bp_throw;
11256
11257   bp_temp = b->disposition == disp_del;
11258   bp_throw = strstr (b->addr_string, "throw") != NULL;
11259   ui_out_text (uiout, bp_temp ? _("Temporary catchpoint ")
11260                               : _("Catchpoint "));
11261   ui_out_field_int (uiout, "bkptno", b->number);
11262   ui_out_text (uiout, bp_throw ? _(" (throw)")
11263                                : _(" (catch)"));
11264 }
11265
11266 /* Implement the "print_recreate" breakpoint_ops method for throw and
11267    catch catchpoints.  */
11268
11269 static void
11270 print_recreate_exception_catchpoint (struct breakpoint *b, 
11271                                      struct ui_file *fp)
11272 {
11273   int bp_temp;
11274   int bp_throw;
11275
11276   bp_temp = b->disposition == disp_del;
11277   bp_throw = strstr (b->addr_string, "throw") != NULL;
11278   fprintf_unfiltered (fp, bp_temp ? "tcatch " : "catch ");
11279   fprintf_unfiltered (fp, bp_throw ? "throw" : "catch");
11280   print_recreate_thread (b, fp);
11281 }
11282
11283 static struct breakpoint_ops gnu_v3_exception_catchpoint_ops;
11284
11285 static int
11286 handle_gnu_v3_exceptions (int tempflag, char *cond_string,
11287                           enum exception_event_kind ex_event, int from_tty)
11288 {
11289   char *trigger_func_name;
11290  
11291   if (ex_event == EX_EVENT_CATCH)
11292     trigger_func_name = "__cxa_begin_catch";
11293   else
11294     trigger_func_name = "__cxa_throw";
11295
11296   create_breakpoint (get_current_arch (),
11297                      trigger_func_name, cond_string, -1, NULL,
11298                      0 /* condition and thread are valid.  */,
11299                      tempflag, bp_breakpoint,
11300                      0,
11301                      AUTO_BOOLEAN_TRUE /* pending */,
11302                      &gnu_v3_exception_catchpoint_ops, from_tty,
11303                      1 /* enabled */,
11304                      0 /* internal */,
11305                      0);
11306
11307   return 1;
11308 }
11309
11310 /* Deal with "catch catch" and "catch throw" commands.  */
11311
11312 static void
11313 catch_exception_command_1 (enum exception_event_kind ex_event, char *arg,
11314                            int tempflag, int from_tty)
11315 {
11316   char *cond_string = NULL;
11317
11318   if (!arg)
11319     arg = "";
11320   arg = skip_spaces (arg);
11321
11322   cond_string = ep_parse_optional_if_clause (&arg);
11323
11324   if ((*arg != '\0') && !isspace (*arg))
11325     error (_("Junk at end of arguments."));
11326
11327   if (ex_event != EX_EVENT_THROW
11328       && ex_event != EX_EVENT_CATCH)
11329     error (_("Unsupported or unknown exception event; cannot catch it"));
11330
11331   if (handle_gnu_v3_exceptions (tempflag, cond_string, ex_event, from_tty))
11332     return;
11333
11334   warning (_("Unsupported with this platform/compiler combination."));
11335 }
11336
11337 /* Implementation of "catch catch" command.  */
11338
11339 static void
11340 catch_catch_command (char *arg, int from_tty, struct cmd_list_element *command)
11341 {
11342   int tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
11343
11344   catch_exception_command_1 (EX_EVENT_CATCH, arg, tempflag, from_tty);
11345 }
11346
11347 /* Implementation of "catch throw" command.  */
11348
11349 static void
11350 catch_throw_command (char *arg, int from_tty, struct cmd_list_element *command)
11351 {
11352   int tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
11353
11354   catch_exception_command_1 (EX_EVENT_THROW, arg, tempflag, from_tty);
11355 }
11356
11357 void
11358 init_ada_exception_breakpoint (struct breakpoint *b,
11359                                struct gdbarch *gdbarch,
11360                                struct symtab_and_line sal,
11361                                char *addr_string,
11362                                const struct breakpoint_ops *ops,
11363                                int tempflag,
11364                                int from_tty)
11365 {
11366   if (from_tty)
11367     {
11368       struct gdbarch *loc_gdbarch = get_sal_arch (sal);
11369       if (!loc_gdbarch)
11370         loc_gdbarch = gdbarch;
11371
11372       describe_other_breakpoints (loc_gdbarch,
11373                                   sal.pspace, sal.pc, sal.section, -1);
11374       /* FIXME: brobecker/2006-12-28: Actually, re-implement a special
11375          version for exception catchpoints, because two catchpoints
11376          used for different exception names will use the same address.
11377          In this case, a "breakpoint ... also set at..." warning is
11378          unproductive.  Besides, the warning phrasing is also a bit
11379          inappropriate, we should use the word catchpoint, and tell
11380          the user what type of catchpoint it is.  The above is good
11381          enough for now, though.  */
11382     }
11383
11384   init_raw_breakpoint (b, gdbarch, sal, bp_breakpoint, ops);
11385
11386   b->enable_state = bp_enabled;
11387   b->disposition = tempflag ? disp_del : disp_donttouch;
11388   b->addr_string = addr_string;
11389   b->language = language_ada;
11390 }
11391
11392 /* Splits the argument using space as delimiter.  Returns an xmalloc'd
11393    filter list, or NULL if no filtering is required.  */
11394 static VEC(int) *
11395 catch_syscall_split_args (char *arg)
11396 {
11397   VEC(int) *result = NULL;
11398   struct cleanup *cleanup = make_cleanup (VEC_cleanup (int), &result);
11399
11400   while (*arg != '\0')
11401     {
11402       int i, syscall_number;
11403       char *endptr;
11404       char cur_name[128];
11405       struct syscall s;
11406
11407       /* Skip whitespace.  */
11408       while (isspace (*arg))
11409         arg++;
11410
11411       for (i = 0; i < 127 && arg[i] && !isspace (arg[i]); ++i)
11412         cur_name[i] = arg[i];
11413       cur_name[i] = '\0';
11414       arg += i;
11415
11416       /* Check if the user provided a syscall name or a number.  */
11417       syscall_number = (int) strtol (cur_name, &endptr, 0);
11418       if (*endptr == '\0')
11419         get_syscall_by_number (syscall_number, &s);
11420       else
11421         {
11422           /* We have a name.  Let's check if it's valid and convert it
11423              to a number.  */
11424           get_syscall_by_name (cur_name, &s);
11425
11426           if (s.number == UNKNOWN_SYSCALL)
11427             /* Here we have to issue an error instead of a warning,
11428                because GDB cannot do anything useful if there's no
11429                syscall number to be caught.  */
11430             error (_("Unknown syscall name '%s'."), cur_name);
11431         }
11432
11433       /* Ok, it's valid.  */
11434       VEC_safe_push (int, result, s.number);
11435     }
11436
11437   discard_cleanups (cleanup);
11438   return result;
11439 }
11440
11441 /* Implement the "catch syscall" command.  */
11442
11443 static void
11444 catch_syscall_command_1 (char *arg, int from_tty, 
11445                          struct cmd_list_element *command)
11446 {
11447   int tempflag;
11448   VEC(int) *filter;
11449   struct syscall s;
11450   struct gdbarch *gdbarch = get_current_arch ();
11451
11452   /* Checking if the feature if supported.  */
11453   if (gdbarch_get_syscall_number_p (gdbarch) == 0)
11454     error (_("The feature 'catch syscall' is not supported on \
11455 this architecture yet."));
11456
11457   tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
11458
11459   arg = skip_spaces (arg);
11460
11461   /* We need to do this first "dummy" translation in order
11462      to get the syscall XML file loaded or, most important,
11463      to display a warning to the user if there's no XML file
11464      for his/her architecture.  */
11465   get_syscall_by_number (0, &s);
11466
11467   /* The allowed syntax is:
11468      catch syscall
11469      catch syscall <name | number> [<name | number> ... <name | number>]
11470
11471      Let's check if there's a syscall name.  */
11472
11473   if (arg != NULL)
11474     filter = catch_syscall_split_args (arg);
11475   else
11476     filter = NULL;
11477
11478   create_syscall_event_catchpoint (tempflag, filter,
11479                                    &catch_syscall_breakpoint_ops);
11480 }
11481
11482 static void
11483 catch_command (char *arg, int from_tty)
11484 {
11485   error (_("Catch requires an event name."));
11486 }
11487 \f
11488
11489 static void
11490 tcatch_command (char *arg, int from_tty)
11491 {
11492   error (_("Catch requires an event name."));
11493 }
11494
11495 /* A qsort comparison function that sorts breakpoints in order.  */
11496
11497 static int
11498 compare_breakpoints (const void *a, const void *b)
11499 {
11500   const breakpoint_p *ba = a;
11501   uintptr_t ua = (uintptr_t) *ba;
11502   const breakpoint_p *bb = b;
11503   uintptr_t ub = (uintptr_t) *bb;
11504
11505   if ((*ba)->number < (*bb)->number)
11506     return -1;
11507   else if ((*ba)->number > (*bb)->number)
11508     return 1;
11509
11510   /* Now sort by address, in case we see, e..g, two breakpoints with
11511      the number 0.  */
11512   if (ua < ub)
11513     return -1;
11514   return ub > ub ? 1 : 0;
11515 }
11516
11517 /* Delete breakpoints by address or line.  */
11518
11519 static void
11520 clear_command (char *arg, int from_tty)
11521 {
11522   struct breakpoint *b, *prev;
11523   VEC(breakpoint_p) *found = 0;
11524   int ix;
11525   int default_match;
11526   struct symtabs_and_lines sals;
11527   struct symtab_and_line sal;
11528   int i;
11529   struct cleanup *cleanups = make_cleanup (null_cleanup, NULL);
11530
11531   if (arg)
11532     {
11533       sals = decode_line_spec (arg, (DECODE_LINE_FUNFIRSTLINE
11534                                      | DECODE_LINE_LIST_MODE));
11535       default_match = 0;
11536     }
11537   else
11538     {
11539       sals.sals = (struct symtab_and_line *)
11540         xmalloc (sizeof (struct symtab_and_line));
11541       make_cleanup (xfree, sals.sals);
11542       init_sal (&sal);          /* Initialize to zeroes.  */
11543
11544       /* Set sal's line, symtab, pc, and pspace to the values
11545          corresponding to the last call to print_frame_info.  If the
11546          codepoint is not valid, this will set all the fields to 0.  */
11547       get_last_displayed_sal (&sal);
11548       if (sal.symtab == 0)
11549         error (_("No source file specified."));
11550
11551       sals.sals[0] = sal;
11552       sals.nelts = 1;
11553
11554       default_match = 1;
11555     }
11556
11557   /* We don't call resolve_sal_pc here.  That's not as bad as it
11558      seems, because all existing breakpoints typically have both
11559      file/line and pc set.  So, if clear is given file/line, we can
11560      match this to existing breakpoint without obtaining pc at all.
11561
11562      We only support clearing given the address explicitly 
11563      present in breakpoint table.  Say, we've set breakpoint 
11564      at file:line.  There were several PC values for that file:line,
11565      due to optimization, all in one block.
11566
11567      We've picked one PC value.  If "clear" is issued with another
11568      PC corresponding to the same file:line, the breakpoint won't
11569      be cleared.  We probably can still clear the breakpoint, but 
11570      since the other PC value is never presented to user, user
11571      can only find it by guessing, and it does not seem important
11572      to support that.  */
11573
11574   /* For each line spec given, delete bps which correspond to it.  Do
11575      it in two passes, solely to preserve the current behavior that
11576      from_tty is forced true if we delete more than one
11577      breakpoint.  */
11578
11579   found = NULL;
11580   make_cleanup (VEC_cleanup (breakpoint_p), &found);
11581   for (i = 0; i < sals.nelts; i++)
11582     {
11583       int is_abs, sal_name_len;
11584
11585       /* If exact pc given, clear bpts at that pc.
11586          If line given (pc == 0), clear all bpts on specified line.
11587          If defaulting, clear all bpts on default line
11588          or at default pc.
11589
11590          defaulting    sal.pc != 0    tests to do
11591
11592          0              1             pc
11593          1              1             pc _and_ line
11594          0              0             line
11595          1              0             <can't happen> */
11596
11597       sal = sals.sals[i];
11598       is_abs = sal.symtab == NULL ? 1 : IS_ABSOLUTE_PATH (sal.symtab->filename);
11599       sal_name_len = is_abs ? 0 : strlen (sal.symtab->filename);
11600
11601       /* Find all matching breakpoints and add them to 'found'.  */
11602       ALL_BREAKPOINTS (b)
11603         {
11604           int match = 0;
11605           /* Are we going to delete b?  */
11606           if (b->type != bp_none && !is_watchpoint (b))
11607             {
11608               struct bp_location *loc = b->loc;
11609               for (; loc; loc = loc->next)
11610                 {
11611                   /* If the user specified file:line, don't allow a PC
11612                      match.  This matches historical gdb behavior.  */
11613                   int pc_match = (!sal.explicit_line
11614                                   && sal.pc
11615                                   && (loc->pspace == sal.pspace)
11616                                   && (loc->address == sal.pc)
11617                                   && (!section_is_overlay (loc->section)
11618                                       || loc->section == sal.section));
11619                   int line_match = 0;
11620
11621                   if ((default_match || sal.explicit_line)
11622                       && loc->source_file != NULL
11623                       && sal.symtab != NULL
11624                       && sal.pspace == loc->pspace
11625                       && loc->line_number == sal.line)
11626                     {
11627                       if (filename_cmp (loc->source_file,
11628                                         sal.symtab->filename) == 0)
11629                         line_match = 1;
11630                       else if (!IS_ABSOLUTE_PATH (sal.symtab->filename)
11631                                && compare_filenames_for_search (loc->source_file,
11632                                                                 sal.symtab->filename,
11633                                                                 sal_name_len))
11634                         line_match = 1;
11635                     }
11636
11637                   if (pc_match || line_match)
11638                     {
11639                       match = 1;
11640                       break;
11641                     }
11642                 }
11643             }
11644
11645           if (match)
11646             VEC_safe_push(breakpoint_p, found, b);
11647         }
11648     }
11649
11650   /* Now go thru the 'found' chain and delete them.  */
11651   if (VEC_empty(breakpoint_p, found))
11652     {
11653       if (arg)
11654         error (_("No breakpoint at %s."), arg);
11655       else
11656         error (_("No breakpoint at this line."));
11657     }
11658
11659   /* Remove duplicates from the vec.  */
11660   qsort (VEC_address (breakpoint_p, found),
11661          VEC_length (breakpoint_p, found),
11662          sizeof (breakpoint_p),
11663          compare_breakpoints);
11664   prev = VEC_index (breakpoint_p, found, 0);
11665   for (ix = 1; VEC_iterate (breakpoint_p, found, ix, b); ++ix)
11666     {
11667       if (b == prev)
11668         {
11669           VEC_ordered_remove (breakpoint_p, found, ix);
11670           --ix;
11671         }
11672     }
11673
11674   if (VEC_length(breakpoint_p, found) > 1)
11675     from_tty = 1;       /* Always report if deleted more than one.  */
11676   if (from_tty)
11677     {
11678       if (VEC_length(breakpoint_p, found) == 1)
11679         printf_unfiltered (_("Deleted breakpoint "));
11680       else
11681         printf_unfiltered (_("Deleted breakpoints "));
11682     }
11683   breakpoints_changed ();
11684
11685   for (ix = 0; VEC_iterate(breakpoint_p, found, ix, b); ix++)
11686     {
11687       if (from_tty)
11688         printf_unfiltered ("%d ", b->number);
11689       delete_breakpoint (b);
11690     }
11691   if (from_tty)
11692     putchar_unfiltered ('\n');
11693
11694   do_cleanups (cleanups);
11695 }
11696 \f
11697 /* Delete breakpoint in BS if they are `delete' breakpoints and
11698    all breakpoints that are marked for deletion, whether hit or not.
11699    This is called after any breakpoint is hit, or after errors.  */
11700
11701 void
11702 breakpoint_auto_delete (bpstat bs)
11703 {
11704   struct breakpoint *b, *b_tmp;
11705
11706   for (; bs; bs = bs->next)
11707     if (bs->breakpoint_at
11708         && bs->breakpoint_at->disposition == disp_del
11709         && bs->stop)
11710       delete_breakpoint (bs->breakpoint_at);
11711
11712   ALL_BREAKPOINTS_SAFE (b, b_tmp)
11713   {
11714     if (b->disposition == disp_del_at_next_stop)
11715       delete_breakpoint (b);
11716   }
11717 }
11718
11719 /* A comparison function for bp_location AP and BP being interfaced to
11720    qsort.  Sort elements primarily by their ADDRESS (no matter what
11721    does breakpoint_address_is_meaningful say for its OWNER),
11722    secondarily by ordering first bp_permanent OWNERed elements and
11723    terciarily just ensuring the array is sorted stable way despite
11724    qsort being an unstable algorithm.  */
11725
11726 static int
11727 bp_location_compare (const void *ap, const void *bp)
11728 {
11729   struct bp_location *a = *(void **) ap;
11730   struct bp_location *b = *(void **) bp;
11731   /* A and B come from existing breakpoints having non-NULL OWNER.  */
11732   int a_perm = a->owner->enable_state == bp_permanent;
11733   int b_perm = b->owner->enable_state == bp_permanent;
11734
11735   if (a->address != b->address)
11736     return (a->address > b->address) - (a->address < b->address);
11737
11738   /* Sort locations at the same address by their pspace number, keeping
11739      locations of the same inferior (in a multi-inferior environment)
11740      grouped.  */
11741
11742   if (a->pspace->num != b->pspace->num)
11743     return ((a->pspace->num > b->pspace->num)
11744             - (a->pspace->num < b->pspace->num));
11745
11746   /* Sort permanent breakpoints first.  */
11747   if (a_perm != b_perm)
11748     return (a_perm < b_perm) - (a_perm > b_perm);
11749
11750   /* Make the internal GDB representation stable across GDB runs
11751      where A and B memory inside GDB can differ.  Breakpoint locations of
11752      the same type at the same address can be sorted in arbitrary order.  */
11753
11754   if (a->owner->number != b->owner->number)
11755     return ((a->owner->number > b->owner->number)
11756             - (a->owner->number < b->owner->number));
11757
11758   return (a > b) - (a < b);
11759 }
11760
11761 /* Set bp_location_placed_address_before_address_max and
11762    bp_location_shadow_len_after_address_max according to the current
11763    content of the bp_location array.  */
11764
11765 static void
11766 bp_location_target_extensions_update (void)
11767 {
11768   struct bp_location *bl, **blp_tmp;
11769
11770   bp_location_placed_address_before_address_max = 0;
11771   bp_location_shadow_len_after_address_max = 0;
11772
11773   ALL_BP_LOCATIONS (bl, blp_tmp)
11774     {
11775       CORE_ADDR start, end, addr;
11776
11777       if (!bp_location_has_shadow (bl))
11778         continue;
11779
11780       start = bl->target_info.placed_address;
11781       end = start + bl->target_info.shadow_len;
11782
11783       gdb_assert (bl->address >= start);
11784       addr = bl->address - start;
11785       if (addr > bp_location_placed_address_before_address_max)
11786         bp_location_placed_address_before_address_max = addr;
11787
11788       /* Zero SHADOW_LEN would not pass bp_location_has_shadow.  */
11789
11790       gdb_assert (bl->address < end);
11791       addr = end - bl->address;
11792       if (addr > bp_location_shadow_len_after_address_max)
11793         bp_location_shadow_len_after_address_max = addr;
11794     }
11795 }
11796
11797 /* Download tracepoint locations if they haven't been.  */
11798
11799 static void
11800 download_tracepoint_locations (void)
11801 {
11802   struct bp_location *bl, **blp_tmp;
11803   struct cleanup *old_chain;
11804
11805   if (!target_can_download_tracepoint ())
11806     return;
11807
11808   old_chain = save_current_space_and_thread ();
11809
11810   ALL_BP_LOCATIONS (bl, blp_tmp)
11811     {
11812       struct tracepoint *t;
11813
11814       if (!is_tracepoint (bl->owner))
11815         continue;
11816
11817       if ((bl->owner->type == bp_fast_tracepoint
11818            ? !may_insert_fast_tracepoints
11819            : !may_insert_tracepoints))
11820         continue;
11821
11822       /* In tracepoint, locations are _never_ duplicated, so
11823          should_be_inserted is equivalent to
11824          unduplicated_should_be_inserted.  */
11825       if (!should_be_inserted (bl) || bl->inserted)
11826         continue;
11827
11828       switch_to_program_space_and_thread (bl->pspace);
11829
11830       target_download_tracepoint (bl);
11831
11832       bl->inserted = 1;
11833       t = (struct tracepoint *) bl->owner;
11834       t->number_on_target = bl->owner->number;
11835     }
11836
11837   do_cleanups (old_chain);
11838 }
11839
11840 /* Swap the insertion/duplication state between two locations.  */
11841
11842 static void
11843 swap_insertion (struct bp_location *left, struct bp_location *right)
11844 {
11845   const int left_inserted = left->inserted;
11846   const int left_duplicate = left->duplicate;
11847   const int left_needs_update = left->needs_update;
11848   const struct bp_target_info left_target_info = left->target_info;
11849
11850   /* Locations of tracepoints can never be duplicated.  */
11851   if (is_tracepoint (left->owner))
11852     gdb_assert (!left->duplicate);
11853   if (is_tracepoint (right->owner))
11854     gdb_assert (!right->duplicate);
11855
11856   left->inserted = right->inserted;
11857   left->duplicate = right->duplicate;
11858   left->needs_update = right->needs_update;
11859   left->target_info = right->target_info;
11860   right->inserted = left_inserted;
11861   right->duplicate = left_duplicate;
11862   right->needs_update = left_needs_update;
11863   right->target_info = left_target_info;
11864 }
11865
11866 /* Force the re-insertion of the locations at ADDRESS.  This is called
11867    once a new/deleted/modified duplicate location is found and we are evaluating
11868    conditions on the target's side.  Such conditions need to be updated on
11869    the target.  */
11870
11871 static void
11872 force_breakpoint_reinsertion (struct bp_location *bl)
11873 {
11874   struct bp_location **locp = NULL, **loc2p;
11875   struct bp_location *loc;
11876   CORE_ADDR address = 0;
11877   int pspace_num;
11878
11879   address = bl->address;
11880   pspace_num = bl->pspace->num;
11881
11882   /* This is only meaningful if the target is
11883      evaluating conditions and if the user has
11884      opted for condition evaluation on the target's
11885      side.  */
11886   if (gdb_evaluates_breakpoint_condition_p ()
11887       || !target_supports_evaluation_of_breakpoint_conditions ())
11888     return;
11889
11890   /* Flag all breakpoint locations with this address and
11891      the same program space as the location
11892      as "its condition has changed".  We need to
11893      update the conditions on the target's side.  */
11894   ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, address)
11895     {
11896       loc = *loc2p;
11897
11898       if (!is_breakpoint (loc->owner)
11899           || pspace_num != loc->pspace->num)
11900         continue;
11901
11902       /* Flag the location appropriately.  We use a different state to
11903          let everyone know that we already updated the set of locations
11904          with addr bl->address and program space bl->pspace.  This is so
11905          we don't have to keep calling these functions just to mark locations
11906          that have already been marked.  */
11907       loc->condition_changed = condition_updated;
11908
11909       /* Free the agent expression bytecode as well.  We will compute
11910          it later on.  */
11911       if (loc->cond_bytecode)
11912         {
11913           free_agent_expr (loc->cond_bytecode);
11914           loc->cond_bytecode = NULL;
11915         }
11916     }
11917 }
11918
11919 /* If SHOULD_INSERT is false, do not insert any breakpoint locations
11920    into the inferior, only remove already-inserted locations that no
11921    longer should be inserted.  Functions that delete a breakpoint or
11922    breakpoints should pass false, so that deleting a breakpoint
11923    doesn't have the side effect of inserting the locations of other
11924    breakpoints that are marked not-inserted, but should_be_inserted
11925    returns true on them.
11926
11927    This behaviour is useful is situations close to tear-down -- e.g.,
11928    after an exec, while the target still has execution, but breakpoint
11929    shadows of the previous executable image should *NOT* be restored
11930    to the new image; or before detaching, where the target still has
11931    execution and wants to delete breakpoints from GDB's lists, and all
11932    breakpoints had already been removed from the inferior.  */
11933
11934 static void
11935 update_global_location_list (int should_insert)
11936 {
11937   struct breakpoint *b;
11938   struct bp_location **locp, *loc;
11939   struct cleanup *cleanups;
11940   /* Last breakpoint location address that was marked for update.  */
11941   CORE_ADDR last_addr = 0;
11942   /* Last breakpoint location program space that was marked for update.  */
11943   int last_pspace_num = -1;
11944
11945   /* Used in the duplicates detection below.  When iterating over all
11946      bp_locations, points to the first bp_location of a given address.
11947      Breakpoints and watchpoints of different types are never
11948      duplicates of each other.  Keep one pointer for each type of
11949      breakpoint/watchpoint, so we only need to loop over all locations
11950      once.  */
11951   struct bp_location *bp_loc_first;  /* breakpoint */
11952   struct bp_location *wp_loc_first;  /* hardware watchpoint */
11953   struct bp_location *awp_loc_first; /* access watchpoint */
11954   struct bp_location *rwp_loc_first; /* read watchpoint */
11955
11956   /* Saved former bp_location array which we compare against the newly
11957      built bp_location from the current state of ALL_BREAKPOINTS.  */
11958   struct bp_location **old_location, **old_locp;
11959   unsigned old_location_count;
11960
11961   old_location = bp_location;
11962   old_location_count = bp_location_count;
11963   bp_location = NULL;
11964   bp_location_count = 0;
11965   cleanups = make_cleanup (xfree, old_location);
11966
11967   ALL_BREAKPOINTS (b)
11968     for (loc = b->loc; loc; loc = loc->next)
11969       bp_location_count++;
11970
11971   bp_location = xmalloc (sizeof (*bp_location) * bp_location_count);
11972   locp = bp_location;
11973   ALL_BREAKPOINTS (b)
11974     for (loc = b->loc; loc; loc = loc->next)
11975       *locp++ = loc;
11976   qsort (bp_location, bp_location_count, sizeof (*bp_location),
11977          bp_location_compare);
11978
11979   bp_location_target_extensions_update ();
11980
11981   /* Identify bp_location instances that are no longer present in the
11982      new list, and therefore should be freed.  Note that it's not
11983      necessary that those locations should be removed from inferior --
11984      if there's another location at the same address (previously
11985      marked as duplicate), we don't need to remove/insert the
11986      location.
11987      
11988      LOCP is kept in sync with OLD_LOCP, each pointing to the current
11989      and former bp_location array state respectively.  */
11990
11991   locp = bp_location;
11992   for (old_locp = old_location; old_locp < old_location + old_location_count;
11993        old_locp++)
11994     {
11995       struct bp_location *old_loc = *old_locp;
11996       struct bp_location **loc2p;
11997
11998       /* Tells if 'old_loc' is found among the new locations.  If
11999          not, we have to free it.  */
12000       int found_object = 0;
12001       /* Tells if the location should remain inserted in the target.  */
12002       int keep_in_target = 0;
12003       int removed = 0;
12004
12005       /* Skip LOCP entries which will definitely never be needed.
12006          Stop either at or being the one matching OLD_LOC.  */
12007       while (locp < bp_location + bp_location_count
12008              && (*locp)->address < old_loc->address)
12009         locp++;
12010
12011       for (loc2p = locp;
12012            (loc2p < bp_location + bp_location_count
12013             && (*loc2p)->address == old_loc->address);
12014            loc2p++)
12015         {
12016           /* Check if this is a new/duplicated location or a duplicated
12017              location that had its condition modified.  If so, we want to send
12018              its condition to the target if evaluation of conditions is taking
12019              place there.  */
12020           if ((*loc2p)->condition_changed == condition_modified
12021               && (last_addr != old_loc->address
12022                   || last_pspace_num != old_loc->pspace->num))
12023             {
12024               force_breakpoint_reinsertion (*loc2p);
12025               last_pspace_num = old_loc->pspace->num;
12026             }
12027
12028           if (*loc2p == old_loc)
12029             found_object = 1;
12030         }
12031
12032       /* We have already handled this address, update it so that we don't
12033          have to go through updates again.  */
12034       last_addr = old_loc->address;
12035
12036       /* Target-side condition evaluation: Handle deleted locations.  */
12037       if (!found_object)
12038         force_breakpoint_reinsertion (old_loc);
12039
12040       /* If this location is no longer present, and inserted, look if
12041          there's maybe a new location at the same address.  If so,
12042          mark that one inserted, and don't remove this one.  This is
12043          needed so that we don't have a time window where a breakpoint
12044          at certain location is not inserted.  */
12045
12046       if (old_loc->inserted)
12047         {
12048           /* If the location is inserted now, we might have to remove
12049              it.  */
12050
12051           if (found_object && should_be_inserted (old_loc))
12052             {
12053               /* The location is still present in the location list,
12054                  and still should be inserted.  Don't do anything.  */
12055               keep_in_target = 1;
12056             }
12057           else
12058             {
12059               /* This location still exists, but it won't be kept in the
12060                  target since it may have been disabled.  We proceed to
12061                  remove its target-side condition.  */
12062
12063               /* The location is either no longer present, or got
12064                  disabled.  See if there's another location at the
12065                  same address, in which case we don't need to remove
12066                  this one from the target.  */
12067
12068               /* OLD_LOC comes from existing struct breakpoint.  */
12069               if (breakpoint_address_is_meaningful (old_loc->owner))
12070                 {
12071                   for (loc2p = locp;
12072                        (loc2p < bp_location + bp_location_count
12073                         && (*loc2p)->address == old_loc->address);
12074                        loc2p++)
12075                     {
12076                       struct bp_location *loc2 = *loc2p;
12077
12078                       if (breakpoint_locations_match (loc2, old_loc))
12079                         {
12080                           /* Read watchpoint locations are switched to
12081                              access watchpoints, if the former are not
12082                              supported, but the latter are.  */
12083                           if (is_hardware_watchpoint (old_loc->owner))
12084                             {
12085                               gdb_assert (is_hardware_watchpoint (loc2->owner));
12086                               loc2->watchpoint_type = old_loc->watchpoint_type;
12087                             }
12088
12089                           /* loc2 is a duplicated location. We need to check
12090                              if it should be inserted in case it will be
12091                              unduplicated.  */
12092                           if (loc2 != old_loc
12093                               && unduplicated_should_be_inserted (loc2))
12094                             {
12095                               swap_insertion (old_loc, loc2);
12096                               keep_in_target = 1;
12097                               break;
12098                             }
12099                         }
12100                     }
12101                 }
12102             }
12103
12104           if (!keep_in_target)
12105             {
12106               if (remove_breakpoint (old_loc, mark_uninserted))
12107                 {
12108                   /* This is just about all we can do.  We could keep
12109                      this location on the global list, and try to
12110                      remove it next time, but there's no particular
12111                      reason why we will succeed next time.
12112                      
12113                      Note that at this point, old_loc->owner is still
12114                      valid, as delete_breakpoint frees the breakpoint
12115                      only after calling us.  */
12116                   printf_filtered (_("warning: Error removing "
12117                                      "breakpoint %d\n"), 
12118                                    old_loc->owner->number);
12119                 }
12120               removed = 1;
12121             }
12122         }
12123
12124       if (!found_object)
12125         {
12126           if (removed && non_stop
12127               && breakpoint_address_is_meaningful (old_loc->owner)
12128               && !is_hardware_watchpoint (old_loc->owner))
12129             {
12130               /* This location was removed from the target.  In
12131                  non-stop mode, a race condition is possible where
12132                  we've removed a breakpoint, but stop events for that
12133                  breakpoint are already queued and will arrive later.
12134                  We apply an heuristic to be able to distinguish such
12135                  SIGTRAPs from other random SIGTRAPs: we keep this
12136                  breakpoint location for a bit, and will retire it
12137                  after we see some number of events.  The theory here
12138                  is that reporting of events should, "on the average",
12139                  be fair, so after a while we'll see events from all
12140                  threads that have anything of interest, and no longer
12141                  need to keep this breakpoint location around.  We
12142                  don't hold locations forever so to reduce chances of
12143                  mistaking a non-breakpoint SIGTRAP for a breakpoint
12144                  SIGTRAP.
12145
12146                  The heuristic failing can be disastrous on
12147                  decr_pc_after_break targets.
12148
12149                  On decr_pc_after_break targets, like e.g., x86-linux,
12150                  if we fail to recognize a late breakpoint SIGTRAP,
12151                  because events_till_retirement has reached 0 too
12152                  soon, we'll fail to do the PC adjustment, and report
12153                  a random SIGTRAP to the user.  When the user resumes
12154                  the inferior, it will most likely immediately crash
12155                  with SIGILL/SIGBUS/SIGSEGV, or worse, get silently
12156                  corrupted, because of being resumed e.g., in the
12157                  middle of a multi-byte instruction, or skipped a
12158                  one-byte instruction.  This was actually seen happen
12159                  on native x86-linux, and should be less rare on
12160                  targets that do not support new thread events, like
12161                  remote, due to the heuristic depending on
12162                  thread_count.
12163
12164                  Mistaking a random SIGTRAP for a breakpoint trap
12165                  causes similar symptoms (PC adjustment applied when
12166                  it shouldn't), but then again, playing with SIGTRAPs
12167                  behind the debugger's back is asking for trouble.
12168
12169                  Since hardware watchpoint traps are always
12170                  distinguishable from other traps, so we don't need to
12171                  apply keep hardware watchpoint moribund locations
12172                  around.  We simply always ignore hardware watchpoint
12173                  traps we can no longer explain.  */
12174
12175               old_loc->events_till_retirement = 3 * (thread_count () + 1);
12176               old_loc->owner = NULL;
12177
12178               VEC_safe_push (bp_location_p, moribund_locations, old_loc);
12179             }
12180           else
12181             {
12182               old_loc->owner = NULL;
12183               decref_bp_location (&old_loc);
12184             }
12185         }
12186     }
12187
12188   /* Rescan breakpoints at the same address and section, marking the
12189      first one as "first" and any others as "duplicates".  This is so
12190      that the bpt instruction is only inserted once.  If we have a
12191      permanent breakpoint at the same place as BPT, make that one the
12192      official one, and the rest as duplicates.  Permanent breakpoints
12193      are sorted first for the same address.
12194
12195      Do the same for hardware watchpoints, but also considering the
12196      watchpoint's type (regular/access/read) and length.  */
12197
12198   bp_loc_first = NULL;
12199   wp_loc_first = NULL;
12200   awp_loc_first = NULL;
12201   rwp_loc_first = NULL;
12202   ALL_BP_LOCATIONS (loc, locp)
12203     {
12204       /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always
12205          non-NULL.  */
12206       struct bp_location **loc_first_p;
12207       b = loc->owner;
12208
12209       if (!should_be_inserted (loc)
12210           || !breakpoint_address_is_meaningful (b)
12211           /* Don't detect duplicate for tracepoint locations because they are
12212            never duplicated.  See the comments in field `duplicate' of
12213            `struct bp_location'.  */
12214           || is_tracepoint (b))
12215         {
12216           /* Clear the condition modification flag.  */
12217           loc->condition_changed = condition_unchanged;
12218           continue;
12219         }
12220
12221       /* Permanent breakpoint should always be inserted.  */
12222       if (b->enable_state == bp_permanent && ! loc->inserted)
12223         internal_error (__FILE__, __LINE__,
12224                         _("allegedly permanent breakpoint is not "
12225                         "actually inserted"));
12226
12227       if (b->type == bp_hardware_watchpoint)
12228         loc_first_p = &wp_loc_first;
12229       else if (b->type == bp_read_watchpoint)
12230         loc_first_p = &rwp_loc_first;
12231       else if (b->type == bp_access_watchpoint)
12232         loc_first_p = &awp_loc_first;
12233       else
12234         loc_first_p = &bp_loc_first;
12235
12236       if (*loc_first_p == NULL
12237           || (overlay_debugging && loc->section != (*loc_first_p)->section)
12238           || !breakpoint_locations_match (loc, *loc_first_p))
12239         {
12240           *loc_first_p = loc;
12241           loc->duplicate = 0;
12242
12243           if (is_breakpoint (loc->owner) && loc->condition_changed)
12244             {
12245               loc->needs_update = 1;
12246               /* Clear the condition modification flag.  */
12247               loc->condition_changed = condition_unchanged;
12248             }
12249           continue;
12250         }
12251
12252
12253       /* This and the above ensure the invariant that the first location
12254          is not duplicated, and is the inserted one.
12255          All following are marked as duplicated, and are not inserted.  */
12256       if (loc->inserted)
12257         swap_insertion (loc, *loc_first_p);
12258       loc->duplicate = 1;
12259
12260       /* Clear the condition modification flag.  */
12261       loc->condition_changed = condition_unchanged;
12262
12263       if ((*loc_first_p)->owner->enable_state == bp_permanent && loc->inserted
12264           && b->enable_state != bp_permanent)
12265         internal_error (__FILE__, __LINE__,
12266                         _("another breakpoint was inserted on top of "
12267                         "a permanent breakpoint"));
12268     }
12269
12270   if (breakpoints_always_inserted_mode ()
12271       && (have_live_inferiors ()
12272           || (gdbarch_has_global_breakpoints (target_gdbarch))))
12273     {
12274       if (should_insert)
12275         insert_breakpoint_locations ();
12276       else
12277         {
12278           /* Though should_insert is false, we may need to update conditions
12279              on the target's side if it is evaluating such conditions.  We
12280              only update conditions for locations that are marked
12281              "needs_update".  */
12282           update_inserted_breakpoint_locations ();
12283         }
12284     }
12285
12286   if (should_insert)
12287     download_tracepoint_locations ();
12288
12289   do_cleanups (cleanups);
12290 }
12291
12292 void
12293 breakpoint_retire_moribund (void)
12294 {
12295   struct bp_location *loc;
12296   int ix;
12297
12298   for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, loc); ++ix)
12299     if (--(loc->events_till_retirement) == 0)
12300       {
12301         decref_bp_location (&loc);
12302         VEC_unordered_remove (bp_location_p, moribund_locations, ix);
12303         --ix;
12304       }
12305 }
12306
12307 static void
12308 update_global_location_list_nothrow (int inserting)
12309 {
12310   volatile struct gdb_exception e;
12311
12312   TRY_CATCH (e, RETURN_MASK_ERROR)
12313     update_global_location_list (inserting);
12314 }
12315
12316 /* Clear BKP from a BPS.  */
12317
12318 static void
12319 bpstat_remove_bp_location (bpstat bps, struct breakpoint *bpt)
12320 {
12321   bpstat bs;
12322
12323   for (bs = bps; bs; bs = bs->next)
12324     if (bs->breakpoint_at == bpt)
12325       {
12326         bs->breakpoint_at = NULL;
12327         bs->old_val = NULL;
12328         /* bs->commands will be freed later.  */
12329       }
12330 }
12331
12332 /* Callback for iterate_over_threads.  */
12333 static int
12334 bpstat_remove_breakpoint_callback (struct thread_info *th, void *data)
12335 {
12336   struct breakpoint *bpt = data;
12337
12338   bpstat_remove_bp_location (th->control.stop_bpstat, bpt);
12339   return 0;
12340 }
12341
12342 /* Helper for breakpoint and tracepoint breakpoint_ops->mention
12343    callbacks.  */
12344
12345 static void
12346 say_where (struct breakpoint *b)
12347 {
12348   struct ui_out *uiout = current_uiout;
12349   struct value_print_options opts;
12350
12351   get_user_print_options (&opts);
12352
12353   /* i18n: cagney/2005-02-11: Below needs to be merged into a
12354      single string.  */
12355   if (b->loc == NULL)
12356     {
12357       printf_filtered (_(" (%s) pending."), b->addr_string);
12358     }
12359   else
12360     {
12361       if (opts.addressprint || b->loc->source_file == NULL)
12362         {
12363           printf_filtered (" at ");
12364           fputs_filtered (paddress (b->loc->gdbarch, b->loc->address),
12365                           gdb_stdout);
12366         }
12367       if (b->loc->source_file)
12368         {
12369           /* If there is a single location, we can print the location
12370              more nicely.  */
12371           if (b->loc->next == NULL)
12372             printf_filtered (": file %s, line %d.",
12373                              b->loc->source_file, b->loc->line_number);
12374           else
12375             /* This is not ideal, but each location may have a
12376                different file name, and this at least reflects the
12377                real situation somewhat.  */
12378             printf_filtered (": %s.", b->addr_string);
12379         }
12380
12381       if (b->loc->next)
12382         {
12383           struct bp_location *loc = b->loc;
12384           int n = 0;
12385           for (; loc; loc = loc->next)
12386             ++n;
12387           printf_filtered (" (%d locations)", n);
12388         }
12389     }
12390 }
12391
12392 /* Default bp_location_ops methods.  */
12393
12394 static void
12395 bp_location_dtor (struct bp_location *self)
12396 {
12397   xfree (self->cond);
12398   if (self->cond_bytecode)
12399     free_agent_expr (self->cond_bytecode);
12400   xfree (self->function_name);
12401   xfree (self->source_file);
12402 }
12403
12404 static const struct bp_location_ops bp_location_ops =
12405 {
12406   bp_location_dtor
12407 };
12408
12409 /* Default breakpoint_ops methods all breakpoint_ops ultimately
12410    inherit from.  */
12411
12412 static void
12413 base_breakpoint_dtor (struct breakpoint *self)
12414 {
12415   decref_counted_command_line (&self->commands);
12416   xfree (self->cond_string);
12417   xfree (self->addr_string);
12418   xfree (self->filter);
12419   xfree (self->addr_string_range_end);
12420 }
12421
12422 static struct bp_location *
12423 base_breakpoint_allocate_location (struct breakpoint *self)
12424 {
12425   struct bp_location *loc;
12426
12427   loc = XNEW (struct bp_location);
12428   init_bp_location (loc, &bp_location_ops, self);
12429   return loc;
12430 }
12431
12432 static void
12433 base_breakpoint_re_set (struct breakpoint *b)
12434 {
12435   /* Nothing to re-set. */
12436 }
12437
12438 #define internal_error_pure_virtual_called() \
12439   gdb_assert_not_reached ("pure virtual function called")
12440
12441 static int
12442 base_breakpoint_insert_location (struct bp_location *bl)
12443 {
12444   internal_error_pure_virtual_called ();
12445 }
12446
12447 static int
12448 base_breakpoint_remove_location (struct bp_location *bl)
12449 {
12450   internal_error_pure_virtual_called ();
12451 }
12452
12453 static int
12454 base_breakpoint_breakpoint_hit (const struct bp_location *bl,
12455                                 struct address_space *aspace,
12456                                 CORE_ADDR bp_addr,
12457                                 const struct target_waitstatus *ws)
12458 {
12459   internal_error_pure_virtual_called ();
12460 }
12461
12462 static void
12463 base_breakpoint_check_status (bpstat bs)
12464 {
12465   /* Always stop.   */
12466 }
12467
12468 /* A "works_in_software_mode" breakpoint_ops method that just internal
12469    errors.  */
12470
12471 static int
12472 base_breakpoint_works_in_software_mode (const struct breakpoint *b)
12473 {
12474   internal_error_pure_virtual_called ();
12475 }
12476
12477 /* A "resources_needed" breakpoint_ops method that just internal
12478    errors.  */
12479
12480 static int
12481 base_breakpoint_resources_needed (const struct bp_location *bl)
12482 {
12483   internal_error_pure_virtual_called ();
12484 }
12485
12486 static enum print_stop_action
12487 base_breakpoint_print_it (bpstat bs)
12488 {
12489   internal_error_pure_virtual_called ();
12490 }
12491
12492 static void
12493 base_breakpoint_print_one_detail (const struct breakpoint *self,
12494                                   struct ui_out *uiout)
12495 {
12496   /* nothing */
12497 }
12498
12499 static void
12500 base_breakpoint_print_mention (struct breakpoint *b)
12501 {
12502   internal_error_pure_virtual_called ();
12503 }
12504
12505 static void
12506 base_breakpoint_print_recreate (struct breakpoint *b, struct ui_file *fp)
12507 {
12508   internal_error_pure_virtual_called ();
12509 }
12510
12511 static void
12512 base_breakpoint_create_sals_from_address (char **arg,
12513                                           struct linespec_result *canonical,
12514                                           enum bptype type_wanted,
12515                                           char *addr_start,
12516                                           char **copy_arg)
12517 {
12518   internal_error_pure_virtual_called ();
12519 }
12520
12521 static void
12522 base_breakpoint_create_breakpoints_sal (struct gdbarch *gdbarch,
12523                                         struct linespec_result *c,
12524                                         struct linespec_sals *lsal,
12525                                         char *cond_string,
12526                                         char *extra_string,
12527                                         enum bptype type_wanted,
12528                                         enum bpdisp disposition,
12529                                         int thread,
12530                                         int task, int ignore_count,
12531                                         const struct breakpoint_ops *o,
12532                                         int from_tty, int enabled,
12533                                         int internal, unsigned flags)
12534 {
12535   internal_error_pure_virtual_called ();
12536 }
12537
12538 static void
12539 base_breakpoint_decode_linespec (struct breakpoint *b, char **s,
12540                                  struct symtabs_and_lines *sals)
12541 {
12542   internal_error_pure_virtual_called ();
12543 }
12544
12545 static struct breakpoint_ops base_breakpoint_ops =
12546 {
12547   base_breakpoint_dtor,
12548   base_breakpoint_allocate_location,
12549   base_breakpoint_re_set,
12550   base_breakpoint_insert_location,
12551   base_breakpoint_remove_location,
12552   base_breakpoint_breakpoint_hit,
12553   base_breakpoint_check_status,
12554   base_breakpoint_resources_needed,
12555   base_breakpoint_works_in_software_mode,
12556   base_breakpoint_print_it,
12557   NULL,
12558   base_breakpoint_print_one_detail,
12559   base_breakpoint_print_mention,
12560   base_breakpoint_print_recreate,
12561   base_breakpoint_create_sals_from_address,
12562   base_breakpoint_create_breakpoints_sal,
12563   base_breakpoint_decode_linespec,
12564 };
12565
12566 /* Default breakpoint_ops methods.  */
12567
12568 static void
12569 bkpt_re_set (struct breakpoint *b)
12570 {
12571   /* FIXME: is this still reachable?  */
12572   if (b->addr_string == NULL)
12573     {
12574       /* Anything without a string can't be re-set.  */
12575       delete_breakpoint (b);
12576       return;
12577     }
12578
12579   breakpoint_re_set_default (b);
12580 }
12581
12582 static int
12583 bkpt_insert_location (struct bp_location *bl)
12584 {
12585   if (bl->loc_type == bp_loc_hardware_breakpoint)
12586     return target_insert_hw_breakpoint (bl->gdbarch,
12587                                         &bl->target_info);
12588   else
12589     return target_insert_breakpoint (bl->gdbarch,
12590                                      &bl->target_info);
12591 }
12592
12593 static int
12594 bkpt_remove_location (struct bp_location *bl)
12595 {
12596   if (bl->loc_type == bp_loc_hardware_breakpoint)
12597     return target_remove_hw_breakpoint (bl->gdbarch, &bl->target_info);
12598   else
12599     return target_remove_breakpoint (bl->gdbarch, &bl->target_info);
12600 }
12601
12602 static int
12603 bkpt_breakpoint_hit (const struct bp_location *bl,
12604                      struct address_space *aspace, CORE_ADDR bp_addr,
12605                      const struct target_waitstatus *ws)
12606 {
12607   struct breakpoint *b = bl->owner;
12608
12609   if (ws->kind != TARGET_WAITKIND_STOPPED
12610       || ws->value.sig != GDB_SIGNAL_TRAP)
12611     return 0;
12612
12613   if (!breakpoint_address_match (bl->pspace->aspace, bl->address,
12614                                  aspace, bp_addr))
12615     return 0;
12616
12617   if (overlay_debugging         /* unmapped overlay section */
12618       && section_is_overlay (bl->section)
12619       && !section_is_mapped (bl->section))
12620     return 0;
12621
12622   return 1;
12623 }
12624
12625 static int
12626 bkpt_resources_needed (const struct bp_location *bl)
12627 {
12628   gdb_assert (bl->owner->type == bp_hardware_breakpoint);
12629
12630   return 1;
12631 }
12632
12633 static enum print_stop_action
12634 bkpt_print_it (bpstat bs)
12635 {
12636   struct breakpoint *b;
12637   const struct bp_location *bl;
12638   int bp_temp;
12639   struct ui_out *uiout = current_uiout;
12640
12641   gdb_assert (bs->bp_location_at != NULL);
12642
12643   bl = bs->bp_location_at;
12644   b = bs->breakpoint_at;
12645
12646   bp_temp = b->disposition == disp_del;
12647   if (bl->address != bl->requested_address)
12648     breakpoint_adjustment_warning (bl->requested_address,
12649                                    bl->address,
12650                                    b->number, 1);
12651   annotate_breakpoint (b->number);
12652   if (bp_temp)
12653     ui_out_text (uiout, "\nTemporary breakpoint ");
12654   else
12655     ui_out_text (uiout, "\nBreakpoint ");
12656   if (ui_out_is_mi_like_p (uiout))
12657     {
12658       ui_out_field_string (uiout, "reason",
12659                            async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT));
12660       ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
12661     }
12662   ui_out_field_int (uiout, "bkptno", b->number);
12663   ui_out_text (uiout, ", ");
12664
12665   return PRINT_SRC_AND_LOC;
12666 }
12667
12668 static void
12669 bkpt_print_mention (struct breakpoint *b)
12670 {
12671   if (ui_out_is_mi_like_p (current_uiout))
12672     return;
12673
12674   switch (b->type)
12675     {
12676     case bp_breakpoint:
12677     case bp_gnu_ifunc_resolver:
12678       if (b->disposition == disp_del)
12679         printf_filtered (_("Temporary breakpoint"));
12680       else
12681         printf_filtered (_("Breakpoint"));
12682       printf_filtered (_(" %d"), b->number);
12683       if (b->type == bp_gnu_ifunc_resolver)
12684         printf_filtered (_(" at gnu-indirect-function resolver"));
12685       break;
12686     case bp_hardware_breakpoint:
12687       printf_filtered (_("Hardware assisted breakpoint %d"), b->number);
12688       break;
12689     case bp_dprintf:
12690       printf_filtered (_("Dprintf %d"), b->number);
12691       break;
12692     }
12693
12694   say_where (b);
12695 }
12696
12697 static void
12698 bkpt_print_recreate (struct breakpoint *tp, struct ui_file *fp)
12699 {
12700   if (tp->type == bp_breakpoint && tp->disposition == disp_del)
12701     fprintf_unfiltered (fp, "tbreak");
12702   else if (tp->type == bp_breakpoint)
12703     fprintf_unfiltered (fp, "break");
12704   else if (tp->type == bp_hardware_breakpoint
12705            && tp->disposition == disp_del)
12706     fprintf_unfiltered (fp, "thbreak");
12707   else if (tp->type == bp_hardware_breakpoint)
12708     fprintf_unfiltered (fp, "hbreak");
12709   else
12710     internal_error (__FILE__, __LINE__,
12711                     _("unhandled breakpoint type %d"), (int) tp->type);
12712
12713   fprintf_unfiltered (fp, " %s", tp->addr_string);
12714   print_recreate_thread (tp, fp);
12715 }
12716
12717 static void
12718 bkpt_create_sals_from_address (char **arg,
12719                                struct linespec_result *canonical,
12720                                enum bptype type_wanted,
12721                                char *addr_start, char **copy_arg)
12722 {
12723   create_sals_from_address_default (arg, canonical, type_wanted,
12724                                     addr_start, copy_arg);
12725 }
12726
12727 static void
12728 bkpt_create_breakpoints_sal (struct gdbarch *gdbarch,
12729                              struct linespec_result *canonical,
12730                              struct linespec_sals *lsal,
12731                              char *cond_string,
12732                              char *extra_string,
12733                              enum bptype type_wanted,
12734                              enum bpdisp disposition,
12735                              int thread,
12736                              int task, int ignore_count,
12737                              const struct breakpoint_ops *ops,
12738                              int from_tty, int enabled,
12739                              int internal, unsigned flags)
12740 {
12741   create_breakpoints_sal_default (gdbarch, canonical, lsal,
12742                                   cond_string, extra_string,
12743                                   type_wanted,
12744                                   disposition, thread, task,
12745                                   ignore_count, ops, from_tty,
12746                                   enabled, internal, flags);
12747 }
12748
12749 static void
12750 bkpt_decode_linespec (struct breakpoint *b, char **s,
12751                       struct symtabs_and_lines *sals)
12752 {
12753   decode_linespec_default (b, s, sals);
12754 }
12755
12756 /* Virtual table for internal breakpoints.  */
12757
12758 static void
12759 internal_bkpt_re_set (struct breakpoint *b)
12760 {
12761   switch (b->type)
12762     {
12763       /* Delete overlay event and longjmp master breakpoints; they
12764          will be reset later by breakpoint_re_set.  */
12765     case bp_overlay_event:
12766     case bp_longjmp_master:
12767     case bp_std_terminate_master:
12768     case bp_exception_master:
12769       delete_breakpoint (b);
12770       break;
12771
12772       /* This breakpoint is special, it's set up when the inferior
12773          starts and we really don't want to touch it.  */
12774     case bp_shlib_event:
12775
12776       /* Like bp_shlib_event, this breakpoint type is special.  Once
12777          it is set up, we do not want to touch it.  */
12778     case bp_thread_event:
12779       break;
12780     }
12781 }
12782
12783 static void
12784 internal_bkpt_check_status (bpstat bs)
12785 {
12786   if (bs->breakpoint_at->type == bp_shlib_event)
12787     {
12788       /* If requested, stop when the dynamic linker notifies GDB of
12789          events.  This allows the user to get control and place
12790          breakpoints in initializer routines for dynamically loaded
12791          objects (among other things).  */
12792       bs->stop = stop_on_solib_events;
12793       bs->print = stop_on_solib_events;
12794     }
12795   else
12796     bs->stop = 0;
12797 }
12798
12799 static enum print_stop_action
12800 internal_bkpt_print_it (bpstat bs)
12801 {
12802   struct ui_out *uiout = current_uiout;
12803   struct breakpoint *b;
12804
12805   b = bs->breakpoint_at;
12806
12807   switch (b->type)
12808     {
12809     case bp_shlib_event:
12810       /* Did we stop because the user set the stop_on_solib_events
12811          variable?  (If so, we report this as a generic, "Stopped due
12812          to shlib event" message.) */
12813       print_solib_event (0);
12814       break;
12815
12816     case bp_thread_event:
12817       /* Not sure how we will get here.
12818          GDB should not stop for these breakpoints.  */
12819       printf_filtered (_("Thread Event Breakpoint: gdb should not stop!\n"));
12820       break;
12821
12822     case bp_overlay_event:
12823       /* By analogy with the thread event, GDB should not stop for these.  */
12824       printf_filtered (_("Overlay Event Breakpoint: gdb should not stop!\n"));
12825       break;
12826
12827     case bp_longjmp_master:
12828       /* These should never be enabled.  */
12829       printf_filtered (_("Longjmp Master Breakpoint: gdb should not stop!\n"));
12830       break;
12831
12832     case bp_std_terminate_master:
12833       /* These should never be enabled.  */
12834       printf_filtered (_("std::terminate Master Breakpoint: "
12835                          "gdb should not stop!\n"));
12836       break;
12837
12838     case bp_exception_master:
12839       /* These should never be enabled.  */
12840       printf_filtered (_("Exception Master Breakpoint: "
12841                          "gdb should not stop!\n"));
12842       break;
12843     }
12844
12845   return PRINT_NOTHING;
12846 }
12847
12848 static void
12849 internal_bkpt_print_mention (struct breakpoint *b)
12850 {
12851   /* Nothing to mention.  These breakpoints are internal.  */
12852 }
12853
12854 /* Virtual table for momentary breakpoints  */
12855
12856 static void
12857 momentary_bkpt_re_set (struct breakpoint *b)
12858 {
12859   /* Keep temporary breakpoints, which can be encountered when we step
12860      over a dlopen call and SOLIB_ADD is resetting the breakpoints.
12861      Otherwise these should have been blown away via the cleanup chain
12862      or by breakpoint_init_inferior when we rerun the executable.  */
12863 }
12864
12865 static void
12866 momentary_bkpt_check_status (bpstat bs)
12867 {
12868   /* Nothing.  The point of these breakpoints is causing a stop.  */
12869 }
12870
12871 static enum print_stop_action
12872 momentary_bkpt_print_it (bpstat bs)
12873 {
12874   struct ui_out *uiout = current_uiout;
12875
12876   if (ui_out_is_mi_like_p (uiout))
12877     {
12878       struct breakpoint *b = bs->breakpoint_at;
12879
12880       switch (b->type)
12881         {
12882         case bp_finish:
12883           ui_out_field_string
12884             (uiout, "reason",
12885              async_reason_lookup (EXEC_ASYNC_FUNCTION_FINISHED));
12886           break;
12887
12888         case bp_until:
12889           ui_out_field_string
12890             (uiout, "reason",
12891              async_reason_lookup (EXEC_ASYNC_LOCATION_REACHED));
12892           break;
12893         }
12894     }
12895
12896   return PRINT_UNKNOWN;
12897 }
12898
12899 static void
12900 momentary_bkpt_print_mention (struct breakpoint *b)
12901 {
12902   /* Nothing to mention.  These breakpoints are internal.  */
12903 }
12904
12905 /* Ensure INITIATING_FRAME is cleared when no such breakpoint exists.
12906
12907    It gets cleared already on the removal of the first one of such placed
12908    breakpoints.  This is OK as they get all removed altogether.  */
12909
12910 static void
12911 longjmp_bkpt_dtor (struct breakpoint *self)
12912 {
12913   struct thread_info *tp = find_thread_id (self->thread);
12914
12915   if (tp)
12916     tp->initiating_frame = null_frame_id;
12917
12918   momentary_breakpoint_ops.dtor (self);
12919 }
12920
12921 /* Specific methods for probe breakpoints.  */
12922
12923 static int
12924 bkpt_probe_insert_location (struct bp_location *bl)
12925 {
12926   int v = bkpt_insert_location (bl);
12927
12928   if (v == 0)
12929     {
12930       /* The insertion was successful, now let's set the probe's semaphore
12931          if needed.  */
12932       bl->probe->pops->set_semaphore (bl->probe, bl->gdbarch);
12933     }
12934
12935   return v;
12936 }
12937
12938 static int
12939 bkpt_probe_remove_location (struct bp_location *bl)
12940 {
12941   /* Let's clear the semaphore before removing the location.  */
12942   bl->probe->pops->clear_semaphore (bl->probe, bl->gdbarch);
12943
12944   return bkpt_remove_location (bl);
12945 }
12946
12947 static void
12948 bkpt_probe_create_sals_from_address (char **arg,
12949                                      struct linespec_result *canonical,
12950                                      enum bptype type_wanted,
12951                                      char *addr_start, char **copy_arg)
12952 {
12953   struct linespec_sals lsal;
12954
12955   lsal.sals = parse_probes (arg, canonical);
12956
12957   *copy_arg = xstrdup (canonical->addr_string);
12958   lsal.canonical = xstrdup (*copy_arg);
12959
12960   VEC_safe_push (linespec_sals, canonical->sals, &lsal);
12961 }
12962
12963 static void
12964 bkpt_probe_decode_linespec (struct breakpoint *b, char **s,
12965                             struct symtabs_and_lines *sals)
12966 {
12967   *sals = parse_probes (s, NULL);
12968   if (!sals->sals)
12969     error (_("probe not found"));
12970 }
12971
12972 /* The breakpoint_ops structure to be used in tracepoints.  */
12973
12974 static void
12975 tracepoint_re_set (struct breakpoint *b)
12976 {
12977   breakpoint_re_set_default (b);
12978 }
12979
12980 static int
12981 tracepoint_breakpoint_hit (const struct bp_location *bl,
12982                            struct address_space *aspace, CORE_ADDR bp_addr,
12983                            const struct target_waitstatus *ws)
12984 {
12985   /* By definition, the inferior does not report stops at
12986      tracepoints.  */
12987   return 0;
12988 }
12989
12990 static void
12991 tracepoint_print_one_detail (const struct breakpoint *self,
12992                              struct ui_out *uiout)
12993 {
12994   struct tracepoint *tp = (struct tracepoint *) self;
12995   if (tp->static_trace_marker_id)
12996     {
12997       gdb_assert (self->type == bp_static_tracepoint);
12998
12999       ui_out_text (uiout, "\tmarker id is ");
13000       ui_out_field_string (uiout, "static-tracepoint-marker-string-id",
13001                            tp->static_trace_marker_id);
13002       ui_out_text (uiout, "\n");
13003     }
13004 }
13005
13006 static void
13007 tracepoint_print_mention (struct breakpoint *b)
13008 {
13009   if (ui_out_is_mi_like_p (current_uiout))
13010     return;
13011
13012   switch (b->type)
13013     {
13014     case bp_tracepoint:
13015       printf_filtered (_("Tracepoint"));
13016       printf_filtered (_(" %d"), b->number);
13017       break;
13018     case bp_fast_tracepoint:
13019       printf_filtered (_("Fast tracepoint"));
13020       printf_filtered (_(" %d"), b->number);
13021       break;
13022     case bp_static_tracepoint:
13023       printf_filtered (_("Static tracepoint"));
13024       printf_filtered (_(" %d"), b->number);
13025       break;
13026     default:
13027       internal_error (__FILE__, __LINE__,
13028                       _("unhandled tracepoint type %d"), (int) b->type);
13029     }
13030
13031   say_where (b);
13032 }
13033
13034 static void
13035 tracepoint_print_recreate (struct breakpoint *self, struct ui_file *fp)
13036 {
13037   struct tracepoint *tp = (struct tracepoint *) self;
13038
13039   if (self->type == bp_fast_tracepoint)
13040     fprintf_unfiltered (fp, "ftrace");
13041   if (self->type == bp_static_tracepoint)
13042     fprintf_unfiltered (fp, "strace");
13043   else if (self->type == bp_tracepoint)
13044     fprintf_unfiltered (fp, "trace");
13045   else
13046     internal_error (__FILE__, __LINE__,
13047                     _("unhandled tracepoint type %d"), (int) self->type);
13048
13049   fprintf_unfiltered (fp, " %s", self->addr_string);
13050   print_recreate_thread (self, fp);
13051
13052   if (tp->pass_count)
13053     fprintf_unfiltered (fp, "  passcount %d\n", tp->pass_count);
13054 }
13055
13056 static void
13057 tracepoint_create_sals_from_address (char **arg,
13058                                      struct linespec_result *canonical,
13059                                      enum bptype type_wanted,
13060                                      char *addr_start, char **copy_arg)
13061 {
13062   create_sals_from_address_default (arg, canonical, type_wanted,
13063                                     addr_start, copy_arg);
13064 }
13065
13066 static void
13067 tracepoint_create_breakpoints_sal (struct gdbarch *gdbarch,
13068                                    struct linespec_result *canonical,
13069                                    struct linespec_sals *lsal,
13070                                    char *cond_string,
13071                                    char *extra_string,
13072                                    enum bptype type_wanted,
13073                                    enum bpdisp disposition,
13074                                    int thread,
13075                                    int task, int ignore_count,
13076                                    const struct breakpoint_ops *ops,
13077                                    int from_tty, int enabled,
13078                                    int internal, unsigned flags)
13079 {
13080   create_breakpoints_sal_default (gdbarch, canonical, lsal,
13081                                   cond_string, extra_string,
13082                                   type_wanted,
13083                                   disposition, thread, task,
13084                                   ignore_count, ops, from_tty,
13085                                   enabled, internal, flags);
13086 }
13087
13088 static void
13089 tracepoint_decode_linespec (struct breakpoint *b, char **s,
13090                             struct symtabs_and_lines *sals)
13091 {
13092   decode_linespec_default (b, s, sals);
13093 }
13094
13095 struct breakpoint_ops tracepoint_breakpoint_ops;
13096
13097 /* The breakpoint_ops structure to be use on tracepoints placed in a
13098    static probe.  */
13099
13100 static void
13101 tracepoint_probe_create_sals_from_address (char **arg,
13102                                            struct linespec_result *canonical,
13103                                            enum bptype type_wanted,
13104                                            char *addr_start, char **copy_arg)
13105 {
13106   /* We use the same method for breakpoint on probes.  */
13107   bkpt_probe_create_sals_from_address (arg, canonical, type_wanted,
13108                                        addr_start, copy_arg);
13109 }
13110
13111 static void
13112 tracepoint_probe_decode_linespec (struct breakpoint *b, char **s,
13113                                   struct symtabs_and_lines *sals)
13114 {
13115   /* We use the same method for breakpoint on probes.  */
13116   bkpt_probe_decode_linespec (b, s, sals);
13117 }
13118
13119 static struct breakpoint_ops tracepoint_probe_breakpoint_ops;
13120
13121 /* The breakpoint_ops structure to be used on static tracepoints with
13122    markers (`-m').  */
13123
13124 static void
13125 strace_marker_create_sals_from_address (char **arg,
13126                                         struct linespec_result *canonical,
13127                                         enum bptype type_wanted,
13128                                         char *addr_start, char **copy_arg)
13129 {
13130   struct linespec_sals lsal;
13131
13132   lsal.sals = decode_static_tracepoint_spec (arg);
13133
13134   *copy_arg = savestring (addr_start, *arg - addr_start);
13135
13136   canonical->addr_string = xstrdup (*copy_arg);
13137   lsal.canonical = xstrdup (*copy_arg);
13138   VEC_safe_push (linespec_sals, canonical->sals, &lsal);
13139 }
13140
13141 static void
13142 strace_marker_create_breakpoints_sal (struct gdbarch *gdbarch,
13143                                       struct linespec_result *canonical,
13144                                       struct linespec_sals *lsal,
13145                                       char *cond_string,
13146                                       char *extra_string,
13147                                       enum bptype type_wanted,
13148                                       enum bpdisp disposition,
13149                                       int thread,
13150                                       int task, int ignore_count,
13151                                       const struct breakpoint_ops *ops,
13152                                       int from_tty, int enabled,
13153                                       int internal, unsigned flags)
13154 {
13155   int i;
13156
13157   /* If the user is creating a static tracepoint by marker id
13158      (strace -m MARKER_ID), then store the sals index, so that
13159      breakpoint_re_set can try to match up which of the newly
13160      found markers corresponds to this one, and, don't try to
13161      expand multiple locations for each sal, given than SALS
13162      already should contain all sals for MARKER_ID.  */
13163
13164   for (i = 0; i < lsal->sals.nelts; ++i)
13165     {
13166       struct symtabs_and_lines expanded;
13167       struct tracepoint *tp;
13168       struct cleanup *old_chain;
13169       char *addr_string;
13170
13171       expanded.nelts = 1;
13172       expanded.sals = &lsal->sals.sals[i];
13173
13174       addr_string = xstrdup (canonical->addr_string);
13175       old_chain = make_cleanup (xfree, addr_string);
13176
13177       tp = XCNEW (struct tracepoint);
13178       init_breakpoint_sal (&tp->base, gdbarch, expanded,
13179                            addr_string, NULL,
13180                            cond_string, extra_string,
13181                            type_wanted, disposition,
13182                            thread, task, ignore_count, ops,
13183                            from_tty, enabled, internal, flags,
13184                            canonical->special_display);
13185       /* Given that its possible to have multiple markers with
13186          the same string id, if the user is creating a static
13187          tracepoint by marker id ("strace -m MARKER_ID"), then
13188          store the sals index, so that breakpoint_re_set can
13189          try to match up which of the newly found markers
13190          corresponds to this one  */
13191       tp->static_trace_marker_id_idx = i;
13192
13193       install_breakpoint (internal, &tp->base, 0);
13194
13195       discard_cleanups (old_chain);
13196     }
13197 }
13198
13199 static void
13200 strace_marker_decode_linespec (struct breakpoint *b, char **s,
13201                                struct symtabs_and_lines *sals)
13202 {
13203   struct tracepoint *tp = (struct tracepoint *) b;
13204
13205   *sals = decode_static_tracepoint_spec (s);
13206   if (sals->nelts > tp->static_trace_marker_id_idx)
13207     {
13208       sals->sals[0] = sals->sals[tp->static_trace_marker_id_idx];
13209       sals->nelts = 1;
13210     }
13211   else
13212     error (_("marker %s not found"), tp->static_trace_marker_id);
13213 }
13214
13215 static struct breakpoint_ops strace_marker_breakpoint_ops;
13216
13217 static int
13218 strace_marker_p (struct breakpoint *b)
13219 {
13220   return b->ops == &strace_marker_breakpoint_ops;
13221 }
13222
13223 /* Delete a breakpoint and clean up all traces of it in the data
13224    structures.  */
13225
13226 void
13227 delete_breakpoint (struct breakpoint *bpt)
13228 {
13229   struct breakpoint *b;
13230
13231   gdb_assert (bpt != NULL);
13232
13233   /* Has this bp already been deleted?  This can happen because
13234      multiple lists can hold pointers to bp's.  bpstat lists are
13235      especial culprits.
13236
13237      One example of this happening is a watchpoint's scope bp.  When
13238      the scope bp triggers, we notice that the watchpoint is out of
13239      scope, and delete it.  We also delete its scope bp.  But the
13240      scope bp is marked "auto-deleting", and is already on a bpstat.
13241      That bpstat is then checked for auto-deleting bp's, which are
13242      deleted.
13243
13244      A real solution to this problem might involve reference counts in
13245      bp's, and/or giving them pointers back to their referencing
13246      bpstat's, and teaching delete_breakpoint to only free a bp's
13247      storage when no more references were extent.  A cheaper bandaid
13248      was chosen.  */
13249   if (bpt->type == bp_none)
13250     return;
13251
13252   /* At least avoid this stale reference until the reference counting
13253      of breakpoints gets resolved.  */
13254   if (bpt->related_breakpoint != bpt)
13255     {
13256       struct breakpoint *related;
13257       struct watchpoint *w;
13258
13259       if (bpt->type == bp_watchpoint_scope)
13260         w = (struct watchpoint *) bpt->related_breakpoint;
13261       else if (bpt->related_breakpoint->type == bp_watchpoint_scope)
13262         w = (struct watchpoint *) bpt;
13263       else
13264         w = NULL;
13265       if (w != NULL)
13266         watchpoint_del_at_next_stop (w);
13267
13268       /* Unlink bpt from the bpt->related_breakpoint ring.  */
13269       for (related = bpt; related->related_breakpoint != bpt;
13270            related = related->related_breakpoint);
13271       related->related_breakpoint = bpt->related_breakpoint;
13272       bpt->related_breakpoint = bpt;
13273     }
13274
13275   /* watch_command_1 creates a watchpoint but only sets its number if
13276      update_watchpoint succeeds in creating its bp_locations.  If there's
13277      a problem in that process, we'll be asked to delete the half-created
13278      watchpoint.  In that case, don't announce the deletion.  */
13279   if (bpt->number)
13280     observer_notify_breakpoint_deleted (bpt);
13281
13282   if (breakpoint_chain == bpt)
13283     breakpoint_chain = bpt->next;
13284
13285   ALL_BREAKPOINTS (b)
13286     if (b->next == bpt)
13287     {
13288       b->next = bpt->next;
13289       break;
13290     }
13291
13292   /* Be sure no bpstat's are pointing at the breakpoint after it's
13293      been freed.  */
13294   /* FIXME, how can we find all bpstat's?  We just check stop_bpstat
13295      in all threads for now.  Note that we cannot just remove bpstats
13296      pointing at bpt from the stop_bpstat list entirely, as breakpoint
13297      commands are associated with the bpstat; if we remove it here,
13298      then the later call to bpstat_do_actions (&stop_bpstat); in
13299      event-top.c won't do anything, and temporary breakpoints with
13300      commands won't work.  */
13301
13302   iterate_over_threads (bpstat_remove_breakpoint_callback, bpt);
13303
13304   /* Now that breakpoint is removed from breakpoint list, update the
13305      global location list.  This will remove locations that used to
13306      belong to this breakpoint.  Do this before freeing the breakpoint
13307      itself, since remove_breakpoint looks at location's owner.  It
13308      might be better design to have location completely
13309      self-contained, but it's not the case now.  */
13310   update_global_location_list (0);
13311
13312   bpt->ops->dtor (bpt);
13313   /* On the chance that someone will soon try again to delete this
13314      same bp, we mark it as deleted before freeing its storage.  */
13315   bpt->type = bp_none;
13316   xfree (bpt);
13317 }
13318
13319 static void
13320 do_delete_breakpoint_cleanup (void *b)
13321 {
13322   delete_breakpoint (b);
13323 }
13324
13325 struct cleanup *
13326 make_cleanup_delete_breakpoint (struct breakpoint *b)
13327 {
13328   return make_cleanup (do_delete_breakpoint_cleanup, b);
13329 }
13330
13331 /* Iterator function to call a user-provided callback function once
13332    for each of B and its related breakpoints.  */
13333
13334 static void
13335 iterate_over_related_breakpoints (struct breakpoint *b,
13336                                   void (*function) (struct breakpoint *,
13337                                                     void *),
13338                                   void *data)
13339 {
13340   struct breakpoint *related;
13341
13342   related = b;
13343   do
13344     {
13345       struct breakpoint *next;
13346
13347       /* FUNCTION may delete RELATED.  */
13348       next = related->related_breakpoint;
13349
13350       if (next == related)
13351         {
13352           /* RELATED is the last ring entry.  */
13353           function (related, data);
13354
13355           /* FUNCTION may have deleted it, so we'd never reach back to
13356              B.  There's nothing left to do anyway, so just break
13357              out.  */
13358           break;
13359         }
13360       else
13361         function (related, data);
13362
13363       related = next;
13364     }
13365   while (related != b);
13366 }
13367
13368 static void
13369 do_delete_breakpoint (struct breakpoint *b, void *ignore)
13370 {
13371   delete_breakpoint (b);
13372 }
13373
13374 /* A callback for map_breakpoint_numbers that calls
13375    delete_breakpoint.  */
13376
13377 static void
13378 do_map_delete_breakpoint (struct breakpoint *b, void *ignore)
13379 {
13380   iterate_over_related_breakpoints (b, do_delete_breakpoint, NULL);
13381 }
13382
13383 void
13384 delete_command (char *arg, int from_tty)
13385 {
13386   struct breakpoint *b, *b_tmp;
13387
13388   dont_repeat ();
13389
13390   if (arg == 0)
13391     {
13392       int breaks_to_delete = 0;
13393
13394       /* Delete all breakpoints if no argument.  Do not delete
13395          internal breakpoints, these have to be deleted with an
13396          explicit breakpoint number argument.  */
13397       ALL_BREAKPOINTS (b)
13398         if (user_breakpoint_p (b))
13399           {
13400             breaks_to_delete = 1;
13401             break;
13402           }
13403
13404       /* Ask user only if there are some breakpoints to delete.  */
13405       if (!from_tty
13406           || (breaks_to_delete && query (_("Delete all breakpoints? "))))
13407         {
13408           ALL_BREAKPOINTS_SAFE (b, b_tmp)
13409             if (user_breakpoint_p (b))
13410               delete_breakpoint (b);
13411         }
13412     }
13413   else
13414     map_breakpoint_numbers (arg, do_map_delete_breakpoint, NULL);
13415 }
13416
13417 static int
13418 all_locations_are_pending (struct bp_location *loc)
13419 {
13420   for (; loc; loc = loc->next)
13421     if (!loc->shlib_disabled
13422         && !loc->pspace->executing_startup)
13423       return 0;
13424   return 1;
13425 }
13426
13427 /* Subroutine of update_breakpoint_locations to simplify it.
13428    Return non-zero if multiple fns in list LOC have the same name.
13429    Null names are ignored.  */
13430
13431 static int
13432 ambiguous_names_p (struct bp_location *loc)
13433 {
13434   struct bp_location *l;
13435   htab_t htab = htab_create_alloc (13, htab_hash_string,
13436                                    (int (*) (const void *, 
13437                                              const void *)) streq,
13438                                    NULL, xcalloc, xfree);
13439
13440   for (l = loc; l != NULL; l = l->next)
13441     {
13442       const char **slot;
13443       const char *name = l->function_name;
13444
13445       /* Allow for some names to be NULL, ignore them.  */
13446       if (name == NULL)
13447         continue;
13448
13449       slot = (const char **) htab_find_slot (htab, (const void *) name,
13450                                              INSERT);
13451       /* NOTE: We can assume slot != NULL here because xcalloc never
13452          returns NULL.  */
13453       if (*slot != NULL)
13454         {
13455           htab_delete (htab);
13456           return 1;
13457         }
13458       *slot = name;
13459     }
13460
13461   htab_delete (htab);
13462   return 0;
13463 }
13464
13465 /* When symbols change, it probably means the sources changed as well,
13466    and it might mean the static tracepoint markers are no longer at
13467    the same address or line numbers they used to be at last we
13468    checked.  Losing your static tracepoints whenever you rebuild is
13469    undesirable.  This function tries to resync/rematch gdb static
13470    tracepoints with the markers on the target, for static tracepoints
13471    that have not been set by marker id.  Static tracepoint that have
13472    been set by marker id are reset by marker id in breakpoint_re_set.
13473    The heuristic is:
13474
13475    1) For a tracepoint set at a specific address, look for a marker at
13476    the old PC.  If one is found there, assume to be the same marker.
13477    If the name / string id of the marker found is different from the
13478    previous known name, assume that means the user renamed the marker
13479    in the sources, and output a warning.
13480
13481    2) For a tracepoint set at a given line number, look for a marker
13482    at the new address of the old line number.  If one is found there,
13483    assume to be the same marker.  If the name / string id of the
13484    marker found is different from the previous known name, assume that
13485    means the user renamed the marker in the sources, and output a
13486    warning.
13487
13488    3) If a marker is no longer found at the same address or line, it
13489    may mean the marker no longer exists.  But it may also just mean
13490    the code changed a bit.  Maybe the user added a few lines of code
13491    that made the marker move up or down (in line number terms).  Ask
13492    the target for info about the marker with the string id as we knew
13493    it.  If found, update line number and address in the matching
13494    static tracepoint.  This will get confused if there's more than one
13495    marker with the same ID (possible in UST, although unadvised
13496    precisely because it confuses tools).  */
13497
13498 static struct symtab_and_line
13499 update_static_tracepoint (struct breakpoint *b, struct symtab_and_line sal)
13500 {
13501   struct tracepoint *tp = (struct tracepoint *) b;
13502   struct static_tracepoint_marker marker;
13503   CORE_ADDR pc;
13504
13505   pc = sal.pc;
13506   if (sal.line)
13507     find_line_pc (sal.symtab, sal.line, &pc);
13508
13509   if (target_static_tracepoint_marker_at (pc, &marker))
13510     {
13511       if (strcmp (tp->static_trace_marker_id, marker.str_id) != 0)
13512         warning (_("static tracepoint %d changed probed marker from %s to %s"),
13513                  b->number,
13514                  tp->static_trace_marker_id, marker.str_id);
13515
13516       xfree (tp->static_trace_marker_id);
13517       tp->static_trace_marker_id = xstrdup (marker.str_id);
13518       release_static_tracepoint_marker (&marker);
13519
13520       return sal;
13521     }
13522
13523   /* Old marker wasn't found on target at lineno.  Try looking it up
13524      by string ID.  */
13525   if (!sal.explicit_pc
13526       && sal.line != 0
13527       && sal.symtab != NULL
13528       && tp->static_trace_marker_id != NULL)
13529     {
13530       VEC(static_tracepoint_marker_p) *markers;
13531
13532       markers
13533         = target_static_tracepoint_markers_by_strid (tp->static_trace_marker_id);
13534
13535       if (!VEC_empty(static_tracepoint_marker_p, markers))
13536         {
13537           struct symtab_and_line sal2;
13538           struct symbol *sym;
13539           struct static_tracepoint_marker *tpmarker;
13540           struct ui_out *uiout = current_uiout;
13541
13542           tpmarker = VEC_index (static_tracepoint_marker_p, markers, 0);
13543
13544           xfree (tp->static_trace_marker_id);
13545           tp->static_trace_marker_id = xstrdup (tpmarker->str_id);
13546
13547           warning (_("marker for static tracepoint %d (%s) not "
13548                      "found at previous line number"),
13549                    b->number, tp->static_trace_marker_id);
13550
13551           init_sal (&sal2);
13552
13553           sal2.pc = tpmarker->address;
13554
13555           sal2 = find_pc_line (tpmarker->address, 0);
13556           sym = find_pc_sect_function (tpmarker->address, NULL);
13557           ui_out_text (uiout, "Now in ");
13558           if (sym)
13559             {
13560               ui_out_field_string (uiout, "func",
13561                                    SYMBOL_PRINT_NAME (sym));
13562               ui_out_text (uiout, " at ");
13563             }
13564           ui_out_field_string (uiout, "file", sal2.symtab->filename);
13565           ui_out_text (uiout, ":");
13566
13567           if (ui_out_is_mi_like_p (uiout))
13568             {
13569               char *fullname = symtab_to_fullname (sal2.symtab);
13570
13571               if (fullname)
13572                 ui_out_field_string (uiout, "fullname", fullname);
13573             }
13574
13575           ui_out_field_int (uiout, "line", sal2.line);
13576           ui_out_text (uiout, "\n");
13577
13578           b->loc->line_number = sal2.line;
13579
13580           xfree (b->loc->source_file);
13581           if (sym)
13582             b->loc->source_file = xstrdup (sal2.symtab->filename);
13583           else
13584             b->loc->source_file = NULL;
13585
13586           xfree (b->addr_string);
13587           b->addr_string = xstrprintf ("%s:%d",
13588                                        sal2.symtab->filename,
13589                                        b->loc->line_number);
13590
13591           /* Might be nice to check if function changed, and warn if
13592              so.  */
13593
13594           release_static_tracepoint_marker (tpmarker);
13595         }
13596     }
13597   return sal;
13598 }
13599
13600 /* Returns 1 iff locations A and B are sufficiently same that
13601    we don't need to report breakpoint as changed.  */
13602
13603 static int
13604 locations_are_equal (struct bp_location *a, struct bp_location *b)
13605 {
13606   while (a && b)
13607     {
13608       if (a->address != b->address)
13609         return 0;
13610
13611       if (a->shlib_disabled != b->shlib_disabled)
13612         return 0;
13613
13614       if (a->enabled != b->enabled)
13615         return 0;
13616
13617       a = a->next;
13618       b = b->next;
13619     }
13620
13621   if ((a == NULL) != (b == NULL))
13622     return 0;
13623
13624   return 1;
13625 }
13626
13627 /* Create new breakpoint locations for B (a hardware or software breakpoint)
13628    based on SALS and SALS_END.  If SALS_END.NELTS is not zero, then B is
13629    a ranged breakpoint.  */
13630
13631 void
13632 update_breakpoint_locations (struct breakpoint *b,
13633                              struct symtabs_and_lines sals,
13634                              struct symtabs_and_lines sals_end)
13635 {
13636   int i;
13637   struct bp_location *existing_locations = b->loc;
13638
13639   if (sals_end.nelts != 0 && (sals.nelts != 1 || sals_end.nelts != 1))
13640     {
13641       /* Ranged breakpoints have only one start location and one end
13642          location.  */
13643       b->enable_state = bp_disabled;
13644       update_global_location_list (1);
13645       printf_unfiltered (_("Could not reset ranged breakpoint %d: "
13646                            "multiple locations found\n"),
13647                          b->number);
13648       return;
13649     }
13650
13651   /* If there's no new locations, and all existing locations are
13652      pending, don't do anything.  This optimizes the common case where
13653      all locations are in the same shared library, that was unloaded.
13654      We'd like to retain the location, so that when the library is
13655      loaded again, we don't loose the enabled/disabled status of the
13656      individual locations.  */
13657   if (all_locations_are_pending (existing_locations) && sals.nelts == 0)
13658     return;
13659
13660   b->loc = NULL;
13661
13662   for (i = 0; i < sals.nelts; ++i)
13663     {
13664       struct bp_location *new_loc;
13665
13666       switch_to_program_space_and_thread (sals.sals[i].pspace);
13667
13668       new_loc = add_location_to_breakpoint (b, &(sals.sals[i]));
13669
13670       /* Reparse conditions, they might contain references to the
13671          old symtab.  */
13672       if (b->cond_string != NULL)
13673         {
13674           char *s;
13675           volatile struct gdb_exception e;
13676
13677           s = b->cond_string;
13678           TRY_CATCH (e, RETURN_MASK_ERROR)
13679             {
13680               new_loc->cond = parse_exp_1 (&s, block_for_pc (sals.sals[i].pc), 
13681                                            0);
13682             }
13683           if (e.reason < 0)
13684             {
13685               warning (_("failed to reevaluate condition "
13686                          "for breakpoint %d: %s"), 
13687                        b->number, e.message);
13688               new_loc->enabled = 0;
13689             }
13690         }
13691
13692       if (sals_end.nelts)
13693         {
13694           CORE_ADDR end = find_breakpoint_range_end (sals_end.sals[0]);
13695
13696           new_loc->length = end - sals.sals[0].pc + 1;
13697         }
13698     }
13699
13700   /* Update locations of permanent breakpoints.  */
13701   if (b->enable_state == bp_permanent)
13702     make_breakpoint_permanent (b);
13703
13704   /* If possible, carry over 'disable' status from existing
13705      breakpoints.  */
13706   {
13707     struct bp_location *e = existing_locations;
13708     /* If there are multiple breakpoints with the same function name,
13709        e.g. for inline functions, comparing function names won't work.
13710        Instead compare pc addresses; this is just a heuristic as things
13711        may have moved, but in practice it gives the correct answer
13712        often enough until a better solution is found.  */
13713     int have_ambiguous_names = ambiguous_names_p (b->loc);
13714
13715     for (; e; e = e->next)
13716       {
13717         if (!e->enabled && e->function_name)
13718           {
13719             struct bp_location *l = b->loc;
13720             if (have_ambiguous_names)
13721               {
13722                 for (; l; l = l->next)
13723                   if (breakpoint_locations_match (e, l))
13724                     {
13725                       l->enabled = 0;
13726                       break;
13727                     }
13728               }
13729             else
13730               {
13731                 for (; l; l = l->next)
13732                   if (l->function_name
13733                       && strcmp (e->function_name, l->function_name) == 0)
13734                     {
13735                       l->enabled = 0;
13736                       break;
13737                     }
13738               }
13739           }
13740       }
13741   }
13742
13743   if (!locations_are_equal (existing_locations, b->loc))
13744     observer_notify_breakpoint_modified (b);
13745
13746   update_global_location_list (1);
13747 }
13748
13749 /* Find the SaL locations corresponding to the given ADDR_STRING.
13750    On return, FOUND will be 1 if any SaL was found, zero otherwise.  */
13751
13752 static struct symtabs_and_lines
13753 addr_string_to_sals (struct breakpoint *b, char *addr_string, int *found)
13754 {
13755   char *s;
13756   struct symtabs_and_lines sals = {0};
13757   volatile struct gdb_exception e;
13758
13759   gdb_assert (b->ops != NULL);
13760   s = addr_string;
13761
13762   TRY_CATCH (e, RETURN_MASK_ERROR)
13763     {
13764       b->ops->decode_linespec (b, &s, &sals);
13765     }
13766   if (e.reason < 0)
13767     {
13768       int not_found_and_ok = 0;
13769       /* For pending breakpoints, it's expected that parsing will
13770          fail until the right shared library is loaded.  User has
13771          already told to create pending breakpoints and don't need
13772          extra messages.  If breakpoint is in bp_shlib_disabled
13773          state, then user already saw the message about that
13774          breakpoint being disabled, and don't want to see more
13775          errors.  */
13776       if (e.error == NOT_FOUND_ERROR
13777           && (b->condition_not_parsed 
13778               || (b->loc && b->loc->shlib_disabled)
13779               || (b->loc && b->loc->pspace->executing_startup)
13780               || b->enable_state == bp_disabled))
13781         not_found_and_ok = 1;
13782
13783       if (!not_found_and_ok)
13784         {
13785           /* We surely don't want to warn about the same breakpoint
13786              10 times.  One solution, implemented here, is disable
13787              the breakpoint on error.  Another solution would be to
13788              have separate 'warning emitted' flag.  Since this
13789              happens only when a binary has changed, I don't know
13790              which approach is better.  */
13791           b->enable_state = bp_disabled;
13792           throw_exception (e);
13793         }
13794     }
13795
13796   if (e.reason == 0 || e.error != NOT_FOUND_ERROR)
13797     {
13798       int i;
13799
13800       for (i = 0; i < sals.nelts; ++i)
13801         resolve_sal_pc (&sals.sals[i]);
13802       if (b->condition_not_parsed && s && s[0])
13803         {
13804           char *cond_string = 0;
13805           int thread = -1;
13806           int task = 0;
13807           char *extra_string = NULL;
13808
13809           find_condition_and_thread (s, sals.sals[0].pc,
13810                                      &cond_string, &thread, &task,
13811                                      &extra_string);
13812           if (cond_string)
13813             b->cond_string = cond_string;
13814           b->thread = thread;
13815           b->task = task;
13816           if (extra_string)
13817             b->extra_string = extra_string;
13818           b->condition_not_parsed = 0;
13819         }
13820
13821       if (b->type == bp_static_tracepoint && !strace_marker_p (b))
13822         sals.sals[0] = update_static_tracepoint (b, sals.sals[0]);
13823
13824       *found = 1;
13825     }
13826   else
13827     *found = 0;
13828
13829   return sals;
13830 }
13831
13832 /* The default re_set method, for typical hardware or software
13833    breakpoints.  Reevaluate the breakpoint and recreate its
13834    locations.  */
13835
13836 static void
13837 breakpoint_re_set_default (struct breakpoint *b)
13838 {
13839   int found;
13840   struct symtabs_and_lines sals, sals_end;
13841   struct symtabs_and_lines expanded = {0};
13842   struct symtabs_and_lines expanded_end = {0};
13843
13844   sals = addr_string_to_sals (b, b->addr_string, &found);
13845   if (found)
13846     {
13847       make_cleanup (xfree, sals.sals);
13848       expanded = sals;
13849     }
13850
13851   if (b->addr_string_range_end)
13852     {
13853       sals_end = addr_string_to_sals (b, b->addr_string_range_end, &found);
13854       if (found)
13855         {
13856           make_cleanup (xfree, sals_end.sals);
13857           expanded_end = sals_end;
13858         }
13859     }
13860
13861   update_breakpoint_locations (b, expanded, expanded_end);
13862 }
13863
13864 /* Default method for creating SALs from an address string.  It basically
13865    calls parse_breakpoint_sals.  Return 1 for success, zero for failure.  */
13866
13867 static void
13868 create_sals_from_address_default (char **arg,
13869                                   struct linespec_result *canonical,
13870                                   enum bptype type_wanted,
13871                                   char *addr_start, char **copy_arg)
13872 {
13873   parse_breakpoint_sals (arg, canonical);
13874 }
13875
13876 /* Call create_breakpoints_sal for the given arguments.  This is the default
13877    function for the `create_breakpoints_sal' method of
13878    breakpoint_ops.  */
13879
13880 static void
13881 create_breakpoints_sal_default (struct gdbarch *gdbarch,
13882                                 struct linespec_result *canonical,
13883                                 struct linespec_sals *lsal,
13884                                 char *cond_string,
13885                                 char *extra_string,
13886                                 enum bptype type_wanted,
13887                                 enum bpdisp disposition,
13888                                 int thread,
13889                                 int task, int ignore_count,
13890                                 const struct breakpoint_ops *ops,
13891                                 int from_tty, int enabled,
13892                                 int internal, unsigned flags)
13893 {
13894   create_breakpoints_sal (gdbarch, canonical, cond_string,
13895                           extra_string,
13896                           type_wanted, disposition,
13897                           thread, task, ignore_count, ops, from_tty,
13898                           enabled, internal, flags);
13899 }
13900
13901 /* Decode the line represented by S by calling decode_line_full.  This is the
13902    default function for the `decode_linespec' method of breakpoint_ops.  */
13903
13904 static void
13905 decode_linespec_default (struct breakpoint *b, char **s,
13906                          struct symtabs_and_lines *sals)
13907 {
13908   struct linespec_result canonical;
13909
13910   init_linespec_result (&canonical);
13911   decode_line_full (s, DECODE_LINE_FUNFIRSTLINE,
13912                     (struct symtab *) NULL, 0,
13913                     &canonical, multiple_symbols_all,
13914                     b->filter);
13915
13916   /* We should get 0 or 1 resulting SALs.  */
13917   gdb_assert (VEC_length (linespec_sals, canonical.sals) < 2);
13918
13919   if (VEC_length (linespec_sals, canonical.sals) > 0)
13920     {
13921       struct linespec_sals *lsal;
13922
13923       lsal = VEC_index (linespec_sals, canonical.sals, 0);
13924       *sals = lsal->sals;
13925       /* Arrange it so the destructor does not free the
13926          contents.  */
13927       lsal->sals.sals = NULL;
13928     }
13929
13930   destroy_linespec_result (&canonical);
13931 }
13932
13933 /* Prepare the global context for a re-set of breakpoint B.  */
13934
13935 static struct cleanup *
13936 prepare_re_set_context (struct breakpoint *b)
13937 {
13938   struct cleanup *cleanups;
13939
13940   input_radix = b->input_radix;
13941   cleanups = save_current_space_and_thread ();
13942   if (b->pspace != NULL)
13943     switch_to_program_space_and_thread (b->pspace);
13944   set_language (b->language);
13945
13946   return cleanups;
13947 }
13948
13949 /* Reset a breakpoint given it's struct breakpoint * BINT.
13950    The value we return ends up being the return value from catch_errors.
13951    Unused in this case.  */
13952
13953 static int
13954 breakpoint_re_set_one (void *bint)
13955 {
13956   /* Get past catch_errs.  */
13957   struct breakpoint *b = (struct breakpoint *) bint;
13958   struct cleanup *cleanups;
13959
13960   cleanups = prepare_re_set_context (b);
13961   b->ops->re_set (b);
13962   do_cleanups (cleanups);
13963   return 0;
13964 }
13965
13966 /* Re-set all breakpoints after symbols have been re-loaded.  */
13967 void
13968 breakpoint_re_set (void)
13969 {
13970   struct breakpoint *b, *b_tmp;
13971   enum language save_language;
13972   int save_input_radix;
13973   struct cleanup *old_chain;
13974
13975   save_language = current_language->la_language;
13976   save_input_radix = input_radix;
13977   old_chain = save_current_program_space ();
13978
13979   ALL_BREAKPOINTS_SAFE (b, b_tmp)
13980   {
13981     /* Format possible error msg.  */
13982     char *message = xstrprintf ("Error in re-setting breakpoint %d: ",
13983                                 b->number);
13984     struct cleanup *cleanups = make_cleanup (xfree, message);
13985     catch_errors (breakpoint_re_set_one, b, message, RETURN_MASK_ALL);
13986     do_cleanups (cleanups);
13987   }
13988   set_language (save_language);
13989   input_radix = save_input_radix;
13990
13991   jit_breakpoint_re_set ();
13992
13993   do_cleanups (old_chain);
13994
13995   create_overlay_event_breakpoint ();
13996   create_longjmp_master_breakpoint ();
13997   create_std_terminate_master_breakpoint ();
13998   create_exception_master_breakpoint ();
13999
14000   /* While we're at it, reset the skip list too.  */
14001   skip_re_set ();
14002 }
14003 \f
14004 /* Reset the thread number of this breakpoint:
14005
14006    - If the breakpoint is for all threads, leave it as-is.
14007    - Else, reset it to the current thread for inferior_ptid.  */
14008 void
14009 breakpoint_re_set_thread (struct breakpoint *b)
14010 {
14011   if (b->thread != -1)
14012     {
14013       if (in_thread_list (inferior_ptid))
14014         b->thread = pid_to_thread_id (inferior_ptid);
14015
14016       /* We're being called after following a fork.  The new fork is
14017          selected as current, and unless this was a vfork will have a
14018          different program space from the original thread.  Reset that
14019          as well.  */
14020       b->loc->pspace = current_program_space;
14021     }
14022 }
14023
14024 /* Set ignore-count of breakpoint number BPTNUM to COUNT.
14025    If from_tty is nonzero, it prints a message to that effect,
14026    which ends with a period (no newline).  */
14027
14028 void
14029 set_ignore_count (int bptnum, int count, int from_tty)
14030 {
14031   struct breakpoint *b;
14032
14033   if (count < 0)
14034     count = 0;
14035
14036   ALL_BREAKPOINTS (b)
14037     if (b->number == bptnum)
14038     {
14039       if (is_tracepoint (b))
14040         {
14041           if (from_tty && count != 0)
14042             printf_filtered (_("Ignore count ignored for tracepoint %d."),
14043                              bptnum);
14044           return;
14045         }
14046       
14047       b->ignore_count = count;
14048       if (from_tty)
14049         {
14050           if (count == 0)
14051             printf_filtered (_("Will stop next time "
14052                                "breakpoint %d is reached."),
14053                              bptnum);
14054           else if (count == 1)
14055             printf_filtered (_("Will ignore next crossing of breakpoint %d."),
14056                              bptnum);
14057           else
14058             printf_filtered (_("Will ignore next %d "
14059                                "crossings of breakpoint %d."),
14060                              count, bptnum);
14061         }
14062       breakpoints_changed ();
14063       observer_notify_breakpoint_modified (b);
14064       return;
14065     }
14066
14067   error (_("No breakpoint number %d."), bptnum);
14068 }
14069
14070 /* Command to set ignore-count of breakpoint N to COUNT.  */
14071
14072 static void
14073 ignore_command (char *args, int from_tty)
14074 {
14075   char *p = args;
14076   int num;
14077
14078   if (p == 0)
14079     error_no_arg (_("a breakpoint number"));
14080
14081   num = get_number (&p);
14082   if (num == 0)
14083     error (_("bad breakpoint number: '%s'"), args);
14084   if (*p == 0)
14085     error (_("Second argument (specified ignore-count) is missing."));
14086
14087   set_ignore_count (num,
14088                     longest_to_int (value_as_long (parse_and_eval (p))),
14089                     from_tty);
14090   if (from_tty)
14091     printf_filtered ("\n");
14092 }
14093 \f
14094 /* Call FUNCTION on each of the breakpoints
14095    whose numbers are given in ARGS.  */
14096
14097 static void
14098 map_breakpoint_numbers (char *args, void (*function) (struct breakpoint *,
14099                                                       void *),
14100                         void *data)
14101 {
14102   int num;
14103   struct breakpoint *b, *tmp;
14104   int match;
14105   struct get_number_or_range_state state;
14106
14107   if (args == 0)
14108     error_no_arg (_("one or more breakpoint numbers"));
14109
14110   init_number_or_range (&state, args);
14111
14112   while (!state.finished)
14113     {
14114       char *p = state.string;
14115
14116       match = 0;
14117
14118       num = get_number_or_range (&state);
14119       if (num == 0)
14120         {
14121           warning (_("bad breakpoint number at or near '%s'"), p);
14122         }
14123       else
14124         {
14125           ALL_BREAKPOINTS_SAFE (b, tmp)
14126             if (b->number == num)
14127               {
14128                 match = 1;
14129                 function (b, data);
14130                 break;
14131               }
14132           if (match == 0)
14133             printf_unfiltered (_("No breakpoint number %d.\n"), num);
14134         }
14135     }
14136 }
14137
14138 static struct bp_location *
14139 find_location_by_number (char *number)
14140 {
14141   char *dot = strchr (number, '.');
14142   char *p1;
14143   int bp_num;
14144   int loc_num;
14145   struct breakpoint *b;
14146   struct bp_location *loc;  
14147
14148   *dot = '\0';
14149
14150   p1 = number;
14151   bp_num = get_number (&p1);
14152   if (bp_num == 0)
14153     error (_("Bad breakpoint number '%s'"), number);
14154
14155   ALL_BREAKPOINTS (b)
14156     if (b->number == bp_num)
14157       {
14158         break;
14159       }
14160
14161   if (!b || b->number != bp_num)
14162     error (_("Bad breakpoint number '%s'"), number);
14163   
14164   p1 = dot+1;
14165   loc_num = get_number (&p1);
14166   if (loc_num == 0)
14167     error (_("Bad breakpoint location number '%s'"), number);
14168
14169   --loc_num;
14170   loc = b->loc;
14171   for (;loc_num && loc; --loc_num, loc = loc->next)
14172     ;
14173   if (!loc)
14174     error (_("Bad breakpoint location number '%s'"), dot+1);
14175     
14176   return loc;  
14177 }
14178
14179
14180 /* Set ignore-count of breakpoint number BPTNUM to COUNT.
14181    If from_tty is nonzero, it prints a message to that effect,
14182    which ends with a period (no newline).  */
14183
14184 void
14185 disable_breakpoint (struct breakpoint *bpt)
14186 {
14187   /* Never disable a watchpoint scope breakpoint; we want to
14188      hit them when we leave scope so we can delete both the
14189      watchpoint and its scope breakpoint at that time.  */
14190   if (bpt->type == bp_watchpoint_scope)
14191     return;
14192
14193   /* You can't disable permanent breakpoints.  */
14194   if (bpt->enable_state == bp_permanent)
14195     return;
14196
14197   bpt->enable_state = bp_disabled;
14198
14199   /* Mark breakpoint locations modified.  */
14200   mark_breakpoint_modified (bpt);
14201
14202   if (target_supports_enable_disable_tracepoint ()
14203       && current_trace_status ()->running && is_tracepoint (bpt))
14204     {
14205       struct bp_location *location;
14206      
14207       for (location = bpt->loc; location; location = location->next)
14208         target_disable_tracepoint (location);
14209     }
14210
14211   update_global_location_list (0);
14212
14213   observer_notify_breakpoint_modified (bpt);
14214 }
14215
14216 /* A callback for iterate_over_related_breakpoints.  */
14217
14218 static void
14219 do_disable_breakpoint (struct breakpoint *b, void *ignore)
14220 {
14221   disable_breakpoint (b);
14222 }
14223
14224 /* A callback for map_breakpoint_numbers that calls
14225    disable_breakpoint.  */
14226
14227 static void
14228 do_map_disable_breakpoint (struct breakpoint *b, void *ignore)
14229 {
14230   iterate_over_related_breakpoints (b, do_disable_breakpoint, NULL);
14231 }
14232
14233 static void
14234 disable_command (char *args, int from_tty)
14235 {
14236   if (args == 0)
14237     {
14238       struct breakpoint *bpt;
14239
14240       ALL_BREAKPOINTS (bpt)
14241         if (user_breakpoint_p (bpt))
14242           disable_breakpoint (bpt);
14243     }
14244   else if (strchr (args, '.'))
14245     {
14246       struct bp_location *loc = find_location_by_number (args);
14247       if (loc)
14248         {
14249           if (loc->enabled)
14250             {
14251               loc->enabled = 0;
14252               mark_breakpoint_location_modified (loc);
14253             }
14254           if (target_supports_enable_disable_tracepoint ()
14255               && current_trace_status ()->running && loc->owner
14256               && is_tracepoint (loc->owner))
14257             target_disable_tracepoint (loc);
14258         }
14259       update_global_location_list (0);
14260     }
14261   else
14262     map_breakpoint_numbers (args, do_map_disable_breakpoint, NULL);
14263 }
14264
14265 static void
14266 enable_breakpoint_disp (struct breakpoint *bpt, enum bpdisp disposition,
14267                         int count)
14268 {
14269   int target_resources_ok;
14270
14271   if (bpt->type == bp_hardware_breakpoint)
14272     {
14273       int i;
14274       i = hw_breakpoint_used_count ();
14275       target_resources_ok = 
14276         target_can_use_hardware_watchpoint (bp_hardware_breakpoint, 
14277                                             i + 1, 0);
14278       if (target_resources_ok == 0)
14279         error (_("No hardware breakpoint support in the target."));
14280       else if (target_resources_ok < 0)
14281         error (_("Hardware breakpoints used exceeds limit."));
14282     }
14283
14284   if (is_watchpoint (bpt))
14285     {
14286       /* Initialize it just to avoid a GCC false warning.  */
14287       enum enable_state orig_enable_state = 0;
14288       volatile struct gdb_exception e;
14289
14290       TRY_CATCH (e, RETURN_MASK_ALL)
14291         {
14292           struct watchpoint *w = (struct watchpoint *) bpt;
14293
14294           orig_enable_state = bpt->enable_state;
14295           bpt->enable_state = bp_enabled;
14296           update_watchpoint (w, 1 /* reparse */);
14297         }
14298       if (e.reason < 0)
14299         {
14300           bpt->enable_state = orig_enable_state;
14301           exception_fprintf (gdb_stderr, e, _("Cannot enable watchpoint %d: "),
14302                              bpt->number);
14303           return;
14304         }
14305     }
14306
14307   if (bpt->enable_state != bp_permanent)
14308     bpt->enable_state = bp_enabled;
14309
14310   bpt->enable_state = bp_enabled;
14311
14312   /* Mark breakpoint locations modified.  */
14313   mark_breakpoint_modified (bpt);
14314
14315   if (target_supports_enable_disable_tracepoint ()
14316       && current_trace_status ()->running && is_tracepoint (bpt))
14317     {
14318       struct bp_location *location;
14319
14320       for (location = bpt->loc; location; location = location->next)
14321         target_enable_tracepoint (location);
14322     }
14323
14324   bpt->disposition = disposition;
14325   bpt->enable_count = count;
14326   update_global_location_list (1);
14327   breakpoints_changed ();
14328   
14329   observer_notify_breakpoint_modified (bpt);
14330 }
14331
14332
14333 void
14334 enable_breakpoint (struct breakpoint *bpt)
14335 {
14336   enable_breakpoint_disp (bpt, bpt->disposition, 0);
14337 }
14338
14339 static void
14340 do_enable_breakpoint (struct breakpoint *bpt, void *arg)
14341 {
14342   enable_breakpoint (bpt);
14343 }
14344
14345 /* A callback for map_breakpoint_numbers that calls
14346    enable_breakpoint.  */
14347
14348 static void
14349 do_map_enable_breakpoint (struct breakpoint *b, void *ignore)
14350 {
14351   iterate_over_related_breakpoints (b, do_enable_breakpoint, NULL);
14352 }
14353
14354 /* The enable command enables the specified breakpoints (or all defined
14355    breakpoints) so they once again become (or continue to be) effective
14356    in stopping the inferior.  */
14357
14358 static void
14359 enable_command (char *args, int from_tty)
14360 {
14361   if (args == 0)
14362     {
14363       struct breakpoint *bpt;
14364
14365       ALL_BREAKPOINTS (bpt)
14366         if (user_breakpoint_p (bpt))
14367           enable_breakpoint (bpt);
14368     }
14369   else if (strchr (args, '.'))
14370     {
14371       struct bp_location *loc = find_location_by_number (args);
14372       if (loc)
14373         {
14374           if (!loc->enabled)
14375             {
14376               loc->enabled = 1;
14377               mark_breakpoint_location_modified (loc);
14378             }
14379           if (target_supports_enable_disable_tracepoint ()
14380               && current_trace_status ()->running && loc->owner
14381               && is_tracepoint (loc->owner))
14382             target_enable_tracepoint (loc);
14383         }
14384       update_global_location_list (1);
14385     }
14386   else
14387     map_breakpoint_numbers (args, do_map_enable_breakpoint, NULL);
14388 }
14389
14390 /* This struct packages up disposition data for application to multiple
14391    breakpoints.  */
14392
14393 struct disp_data
14394 {
14395   enum bpdisp disp;
14396   int count;
14397 };
14398
14399 static void
14400 do_enable_breakpoint_disp (struct breakpoint *bpt, void *arg)
14401 {
14402   struct disp_data disp_data = *(struct disp_data *) arg;
14403
14404   enable_breakpoint_disp (bpt, disp_data.disp, disp_data.count);
14405 }
14406
14407 static void
14408 do_map_enable_once_breakpoint (struct breakpoint *bpt, void *ignore)
14409 {
14410   struct disp_data disp = { disp_disable, 1 };
14411
14412   iterate_over_related_breakpoints (bpt, do_enable_breakpoint_disp, &disp);
14413 }
14414
14415 static void
14416 enable_once_command (char *args, int from_tty)
14417 {
14418   map_breakpoint_numbers (args, do_map_enable_once_breakpoint, NULL);
14419 }
14420
14421 static void
14422 do_map_enable_count_breakpoint (struct breakpoint *bpt, void *countptr)
14423 {
14424   struct disp_data disp = { disp_disable, *(int *) countptr };
14425
14426   iterate_over_related_breakpoints (bpt, do_enable_breakpoint_disp, &disp);
14427 }
14428
14429 static void
14430 enable_count_command (char *args, int from_tty)
14431 {
14432   int count = get_number (&args);
14433
14434   map_breakpoint_numbers (args, do_map_enable_count_breakpoint, &count);
14435 }
14436
14437 static void
14438 do_map_enable_delete_breakpoint (struct breakpoint *bpt, void *ignore)
14439 {
14440   struct disp_data disp = { disp_del, 1 };
14441
14442   iterate_over_related_breakpoints (bpt, do_enable_breakpoint_disp, &disp);
14443 }
14444
14445 static void
14446 enable_delete_command (char *args, int from_tty)
14447 {
14448   map_breakpoint_numbers (args, do_map_enable_delete_breakpoint, NULL);
14449 }
14450 \f
14451 static void
14452 set_breakpoint_cmd (char *args, int from_tty)
14453 {
14454 }
14455
14456 static void
14457 show_breakpoint_cmd (char *args, int from_tty)
14458 {
14459 }
14460
14461 /* Invalidate last known value of any hardware watchpoint if
14462    the memory which that value represents has been written to by
14463    GDB itself.  */
14464
14465 static void
14466 invalidate_bp_value_on_memory_change (CORE_ADDR addr, int len,
14467                                       const bfd_byte *data)
14468 {
14469   struct breakpoint *bp;
14470
14471   ALL_BREAKPOINTS (bp)
14472     if (bp->enable_state == bp_enabled
14473         && bp->type == bp_hardware_watchpoint)
14474       {
14475         struct watchpoint *wp = (struct watchpoint *) bp;
14476
14477         if (wp->val_valid && wp->val)
14478           {
14479             struct bp_location *loc;
14480
14481             for (loc = bp->loc; loc != NULL; loc = loc->next)
14482               if (loc->loc_type == bp_loc_hardware_watchpoint
14483                   && loc->address + loc->length > addr
14484                   && addr + len > loc->address)
14485                 {
14486                   value_free (wp->val);
14487                   wp->val = NULL;
14488                   wp->val_valid = 0;
14489                 }
14490           }
14491       }
14492 }
14493
14494 /* Use the last displayed codepoint's values, or nothing
14495    if they aren't valid.  */
14496
14497 struct symtabs_and_lines
14498 decode_line_spec_1 (char *string, int flags)
14499 {
14500   struct symtabs_and_lines sals;
14501
14502   if (string == 0)
14503     error (_("Empty line specification."));
14504   if (last_displayed_sal_is_valid ())
14505     sals = decode_line_1 (&string, flags,
14506                           get_last_displayed_symtab (),
14507                           get_last_displayed_line ());
14508   else
14509     sals = decode_line_1 (&string, flags, (struct symtab *) NULL, 0);
14510   if (*string)
14511     error (_("Junk at end of line specification: %s"), string);
14512   return sals;
14513 }
14514
14515 /* Create and insert a raw software breakpoint at PC.  Return an
14516    identifier, which should be used to remove the breakpoint later.
14517    In general, places which call this should be using something on the
14518    breakpoint chain instead; this function should be eliminated
14519    someday.  */
14520
14521 void *
14522 deprecated_insert_raw_breakpoint (struct gdbarch *gdbarch,
14523                                   struct address_space *aspace, CORE_ADDR pc)
14524 {
14525   struct bp_target_info *bp_tgt;
14526
14527   bp_tgt = XZALLOC (struct bp_target_info);
14528
14529   bp_tgt->placed_address_space = aspace;
14530   bp_tgt->placed_address = pc;
14531
14532   if (target_insert_breakpoint (gdbarch, bp_tgt) != 0)
14533     {
14534       /* Could not insert the breakpoint.  */
14535       xfree (bp_tgt);
14536       return NULL;
14537     }
14538
14539   return bp_tgt;
14540 }
14541
14542 /* Remove a breakpoint BP inserted by
14543    deprecated_insert_raw_breakpoint.  */
14544
14545 int
14546 deprecated_remove_raw_breakpoint (struct gdbarch *gdbarch, void *bp)
14547 {
14548   struct bp_target_info *bp_tgt = bp;
14549   int ret;
14550
14551   ret = target_remove_breakpoint (gdbarch, bp_tgt);
14552   xfree (bp_tgt);
14553
14554   return ret;
14555 }
14556
14557 /* One (or perhaps two) breakpoints used for software single
14558    stepping.  */
14559
14560 static void *single_step_breakpoints[2];
14561 static struct gdbarch *single_step_gdbarch[2];
14562
14563 /* Create and insert a breakpoint for software single step.  */
14564
14565 void
14566 insert_single_step_breakpoint (struct gdbarch *gdbarch,
14567                                struct address_space *aspace, 
14568                                CORE_ADDR next_pc)
14569 {
14570   void **bpt_p;
14571
14572   if (single_step_breakpoints[0] == NULL)
14573     {
14574       bpt_p = &single_step_breakpoints[0];
14575       single_step_gdbarch[0] = gdbarch;
14576     }
14577   else
14578     {
14579       gdb_assert (single_step_breakpoints[1] == NULL);
14580       bpt_p = &single_step_breakpoints[1];
14581       single_step_gdbarch[1] = gdbarch;
14582     }
14583
14584   /* NOTE drow/2006-04-11: A future improvement to this function would
14585      be to only create the breakpoints once, and actually put them on
14586      the breakpoint chain.  That would let us use set_raw_breakpoint.
14587      We could adjust the addresses each time they were needed.  Doing
14588      this requires corresponding changes elsewhere where single step
14589      breakpoints are handled, however.  So, for now, we use this.  */
14590
14591   *bpt_p = deprecated_insert_raw_breakpoint (gdbarch, aspace, next_pc);
14592   if (*bpt_p == NULL)
14593     error (_("Could not insert single-step breakpoint at %s"),
14594              paddress (gdbarch, next_pc));
14595 }
14596
14597 /* Check if the breakpoints used for software single stepping
14598    were inserted or not.  */
14599
14600 int
14601 single_step_breakpoints_inserted (void)
14602 {
14603   return (single_step_breakpoints[0] != NULL
14604           || single_step_breakpoints[1] != NULL);
14605 }
14606
14607 /* Remove and delete any breakpoints used for software single step.  */
14608
14609 void
14610 remove_single_step_breakpoints (void)
14611 {
14612   gdb_assert (single_step_breakpoints[0] != NULL);
14613
14614   /* See insert_single_step_breakpoint for more about this deprecated
14615      call.  */
14616   deprecated_remove_raw_breakpoint (single_step_gdbarch[0],
14617                                     single_step_breakpoints[0]);
14618   single_step_gdbarch[0] = NULL;
14619   single_step_breakpoints[0] = NULL;
14620
14621   if (single_step_breakpoints[1] != NULL)
14622     {
14623       deprecated_remove_raw_breakpoint (single_step_gdbarch[1],
14624                                         single_step_breakpoints[1]);
14625       single_step_gdbarch[1] = NULL;
14626       single_step_breakpoints[1] = NULL;
14627     }
14628 }
14629
14630 /* Delete software single step breakpoints without removing them from
14631    the inferior.  This is intended to be used if the inferior's address
14632    space where they were inserted is already gone, e.g. after exit or
14633    exec.  */
14634
14635 void
14636 cancel_single_step_breakpoints (void)
14637 {
14638   int i;
14639
14640   for (i = 0; i < 2; i++)
14641     if (single_step_breakpoints[i])
14642       {
14643         xfree (single_step_breakpoints[i]);
14644         single_step_breakpoints[i] = NULL;
14645         single_step_gdbarch[i] = NULL;
14646       }
14647 }
14648
14649 /* Detach software single-step breakpoints from INFERIOR_PTID without
14650    removing them.  */
14651
14652 static void
14653 detach_single_step_breakpoints (void)
14654 {
14655   int i;
14656
14657   for (i = 0; i < 2; i++)
14658     if (single_step_breakpoints[i])
14659       target_remove_breakpoint (single_step_gdbarch[i],
14660                                 single_step_breakpoints[i]);
14661 }
14662
14663 /* Check whether a software single-step breakpoint is inserted at
14664    PC.  */
14665
14666 static int
14667 single_step_breakpoint_inserted_here_p (struct address_space *aspace, 
14668                                         CORE_ADDR pc)
14669 {
14670   int i;
14671
14672   for (i = 0; i < 2; i++)
14673     {
14674       struct bp_target_info *bp_tgt = single_step_breakpoints[i];
14675       if (bp_tgt
14676           && breakpoint_address_match (bp_tgt->placed_address_space,
14677                                        bp_tgt->placed_address,
14678                                        aspace, pc))
14679         return 1;
14680     }
14681
14682   return 0;
14683 }
14684
14685 /* Returns 0 if 'bp' is NOT a syscall catchpoint,
14686    non-zero otherwise.  */
14687 static int
14688 is_syscall_catchpoint_enabled (struct breakpoint *bp)
14689 {
14690   if (syscall_catchpoint_p (bp)
14691       && bp->enable_state != bp_disabled
14692       && bp->enable_state != bp_call_disabled)
14693     return 1;
14694   else
14695     return 0;
14696 }
14697
14698 int
14699 catch_syscall_enabled (void)
14700 {
14701   struct catch_syscall_inferior_data *inf_data
14702     = get_catch_syscall_inferior_data (current_inferior ());
14703
14704   return inf_data->total_syscalls_count != 0;
14705 }
14706
14707 int
14708 catching_syscall_number (int syscall_number)
14709 {
14710   struct breakpoint *bp;
14711
14712   ALL_BREAKPOINTS (bp)
14713     if (is_syscall_catchpoint_enabled (bp))
14714       {
14715         struct syscall_catchpoint *c = (struct syscall_catchpoint *) bp;
14716
14717         if (c->syscalls_to_be_caught)
14718           {
14719             int i, iter;
14720             for (i = 0;
14721                  VEC_iterate (int, c->syscalls_to_be_caught, i, iter);
14722                  i++)
14723               if (syscall_number == iter)
14724                 return 1;
14725           }
14726         else
14727           return 1;
14728       }
14729
14730   return 0;
14731 }
14732
14733 /* Complete syscall names.  Used by "catch syscall".  */
14734 static VEC (char_ptr) *
14735 catch_syscall_completer (struct cmd_list_element *cmd,
14736                          char *text, char *word)
14737 {
14738   const char **list = get_syscall_names ();
14739   VEC (char_ptr) *retlist
14740     = (list == NULL) ? NULL : complete_on_enum (list, text, word);
14741
14742   xfree (list);
14743   return retlist;
14744 }
14745
14746 /* Tracepoint-specific operations.  */
14747
14748 /* Set tracepoint count to NUM.  */
14749 static void
14750 set_tracepoint_count (int num)
14751 {
14752   tracepoint_count = num;
14753   set_internalvar_integer (lookup_internalvar ("tpnum"), num);
14754 }
14755
14756 static void
14757 trace_command (char *arg, int from_tty)
14758 {
14759   struct breakpoint_ops *ops;
14760   const char *arg_cp = arg;
14761
14762   if (arg && probe_linespec_to_ops (&arg_cp))
14763     ops = &tracepoint_probe_breakpoint_ops;
14764   else
14765     ops = &tracepoint_breakpoint_ops;
14766
14767   if (create_breakpoint (get_current_arch (),
14768                          arg,
14769                          NULL, 0, NULL, 1 /* parse arg */,
14770                          0 /* tempflag */,
14771                          bp_tracepoint /* type_wanted */,
14772                          0 /* Ignore count */,
14773                          pending_break_support,
14774                          ops,
14775                          from_tty,
14776                          1 /* enabled */,
14777                          0 /* internal */, 0))
14778     set_tracepoint_count (breakpoint_count);
14779 }
14780
14781 static void
14782 ftrace_command (char *arg, int from_tty)
14783 {
14784   if (create_breakpoint (get_current_arch (),
14785                          arg,
14786                          NULL, 0, NULL, 1 /* parse arg */,
14787                          0 /* tempflag */,
14788                          bp_fast_tracepoint /* type_wanted */,
14789                          0 /* Ignore count */,
14790                          pending_break_support,
14791                          &tracepoint_breakpoint_ops,
14792                          from_tty,
14793                          1 /* enabled */,
14794                          0 /* internal */, 0))
14795     set_tracepoint_count (breakpoint_count);
14796 }
14797
14798 /* strace command implementation.  Creates a static tracepoint.  */
14799
14800 static void
14801 strace_command (char *arg, int from_tty)
14802 {
14803   struct breakpoint_ops *ops;
14804
14805   /* Decide if we are dealing with a static tracepoint marker (`-m'),
14806      or with a normal static tracepoint.  */
14807   if (arg && strncmp (arg, "-m", 2) == 0 && isspace (arg[2]))
14808     ops = &strace_marker_breakpoint_ops;
14809   else
14810     ops = &tracepoint_breakpoint_ops;
14811
14812   if (create_breakpoint (get_current_arch (),
14813                          arg,
14814                          NULL, 0, NULL, 1 /* parse arg */,
14815                          0 /* tempflag */,
14816                          bp_static_tracepoint /* type_wanted */,
14817                          0 /* Ignore count */,
14818                          pending_break_support,
14819                          ops,
14820                          from_tty,
14821                          1 /* enabled */,
14822                          0 /* internal */, 0))
14823     set_tracepoint_count (breakpoint_count);
14824 }
14825
14826 /* Set up a fake reader function that gets command lines from a linked
14827    list that was acquired during tracepoint uploading.  */
14828
14829 static struct uploaded_tp *this_utp;
14830 static int next_cmd;
14831
14832 static char *
14833 read_uploaded_action (void)
14834 {
14835   char *rslt;
14836
14837   VEC_iterate (char_ptr, this_utp->cmd_strings, next_cmd, rslt);
14838
14839   next_cmd++;
14840
14841   return rslt;
14842 }
14843
14844 /* Given information about a tracepoint as recorded on a target (which
14845    can be either a live system or a trace file), attempt to create an
14846    equivalent GDB tracepoint.  This is not a reliable process, since
14847    the target does not necessarily have all the information used when
14848    the tracepoint was originally defined.  */
14849   
14850 struct tracepoint *
14851 create_tracepoint_from_upload (struct uploaded_tp *utp)
14852 {
14853   char *addr_str, small_buf[100];
14854   struct tracepoint *tp;
14855
14856   if (utp->at_string)
14857     addr_str = utp->at_string;
14858   else
14859     {
14860       /* In the absence of a source location, fall back to raw
14861          address.  Since there is no way to confirm that the address
14862          means the same thing as when the trace was started, warn the
14863          user.  */
14864       warning (_("Uploaded tracepoint %d has no "
14865                  "source location, using raw address"),
14866                utp->number);
14867       sprintf (small_buf, "*%s", hex_string (utp->addr));
14868       addr_str = small_buf;
14869     }
14870
14871   /* There's not much we can do with a sequence of bytecodes.  */
14872   if (utp->cond && !utp->cond_string)
14873     warning (_("Uploaded tracepoint %d condition "
14874                "has no source form, ignoring it"),
14875              utp->number);
14876
14877   if (!create_breakpoint (get_current_arch (),
14878                           addr_str,
14879                           utp->cond_string, -1, NULL,
14880                           0 /* parse cond/thread */,
14881                           0 /* tempflag */,
14882                           utp->type /* type_wanted */,
14883                           0 /* Ignore count */,
14884                           pending_break_support,
14885                           &tracepoint_breakpoint_ops,
14886                           0 /* from_tty */,
14887                           utp->enabled /* enabled */,
14888                           0 /* internal */,
14889                           CREATE_BREAKPOINT_FLAGS_INSERTED))
14890     return NULL;
14891
14892   set_tracepoint_count (breakpoint_count);
14893   
14894   /* Get the tracepoint we just created.  */
14895   tp = get_tracepoint (tracepoint_count);
14896   gdb_assert (tp != NULL);
14897
14898   if (utp->pass > 0)
14899     {
14900       sprintf (small_buf, "%d %d", utp->pass, tp->base.number);
14901
14902       trace_pass_command (small_buf, 0);
14903     }
14904
14905   /* If we have uploaded versions of the original commands, set up a
14906      special-purpose "reader" function and call the usual command line
14907      reader, then pass the result to the breakpoint command-setting
14908      function.  */
14909   if (!VEC_empty (char_ptr, utp->cmd_strings))
14910     {
14911       struct command_line *cmd_list;
14912
14913       this_utp = utp;
14914       next_cmd = 0;
14915
14916       cmd_list = read_command_lines_1 (read_uploaded_action, 1, NULL, NULL);
14917
14918       breakpoint_set_commands (&tp->base, cmd_list);
14919     }
14920   else if (!VEC_empty (char_ptr, utp->actions)
14921            || !VEC_empty (char_ptr, utp->step_actions))
14922     warning (_("Uploaded tracepoint %d actions "
14923                "have no source form, ignoring them"),
14924              utp->number);
14925
14926   /* Copy any status information that might be available.  */
14927   tp->base.hit_count = utp->hit_count;
14928   tp->traceframe_usage = utp->traceframe_usage;
14929
14930   return tp;
14931 }
14932   
14933 /* Print information on tracepoint number TPNUM_EXP, or all if
14934    omitted.  */
14935
14936 static void
14937 tracepoints_info (char *args, int from_tty)
14938 {
14939   struct ui_out *uiout = current_uiout;
14940   int num_printed;
14941
14942   num_printed = breakpoint_1 (args, 0, is_tracepoint);
14943
14944   if (num_printed == 0)
14945     {
14946       if (args == NULL || *args == '\0')
14947         ui_out_message (uiout, 0, "No tracepoints.\n");
14948       else
14949         ui_out_message (uiout, 0, "No tracepoint matching '%s'.\n", args);
14950     }
14951
14952   default_collect_info ();
14953 }
14954
14955 /* The 'enable trace' command enables tracepoints.
14956    Not supported by all targets.  */
14957 static void
14958 enable_trace_command (char *args, int from_tty)
14959 {
14960   enable_command (args, from_tty);
14961 }
14962
14963 /* The 'disable trace' command disables tracepoints.
14964    Not supported by all targets.  */
14965 static void
14966 disable_trace_command (char *args, int from_tty)
14967 {
14968   disable_command (args, from_tty);
14969 }
14970
14971 /* Remove a tracepoint (or all if no argument).  */
14972 static void
14973 delete_trace_command (char *arg, int from_tty)
14974 {
14975   struct breakpoint *b, *b_tmp;
14976
14977   dont_repeat ();
14978
14979   if (arg == 0)
14980     {
14981       int breaks_to_delete = 0;
14982
14983       /* Delete all breakpoints if no argument.
14984          Do not delete internal or call-dummy breakpoints, these
14985          have to be deleted with an explicit breakpoint number 
14986          argument.  */
14987       ALL_TRACEPOINTS (b)
14988         if (is_tracepoint (b) && user_breakpoint_p (b))
14989           {
14990             breaks_to_delete = 1;
14991             break;
14992           }
14993
14994       /* Ask user only if there are some breakpoints to delete.  */
14995       if (!from_tty
14996           || (breaks_to_delete && query (_("Delete all tracepoints? "))))
14997         {
14998           ALL_BREAKPOINTS_SAFE (b, b_tmp)
14999             if (is_tracepoint (b) && user_breakpoint_p (b))
15000               delete_breakpoint (b);
15001         }
15002     }
15003   else
15004     map_breakpoint_numbers (arg, do_map_delete_breakpoint, NULL);
15005 }
15006
15007 /* Helper function for trace_pass_command.  */
15008
15009 static void
15010 trace_pass_set_count (struct tracepoint *tp, int count, int from_tty)
15011 {
15012   tp->pass_count = count;
15013   observer_notify_tracepoint_modified (tp->base.number);
15014   if (from_tty)
15015     printf_filtered (_("Setting tracepoint %d's passcount to %d\n"),
15016                      tp->base.number, count);
15017 }
15018
15019 /* Set passcount for tracepoint.
15020
15021    First command argument is passcount, second is tracepoint number.
15022    If tracepoint number omitted, apply to most recently defined.
15023    Also accepts special argument "all".  */
15024
15025 static void
15026 trace_pass_command (char *args, int from_tty)
15027 {
15028   struct tracepoint *t1;
15029   unsigned int count;
15030
15031   if (args == 0 || *args == 0)
15032     error (_("passcount command requires an "
15033              "argument (count + optional TP num)"));
15034
15035   count = strtoul (args, &args, 10);    /* Count comes first, then TP num.  */
15036
15037   while (*args && isspace ((int) *args))
15038     args++;
15039
15040   if (*args && strncasecmp (args, "all", 3) == 0)
15041     {
15042       struct breakpoint *b;
15043
15044       args += 3;                        /* Skip special argument "all".  */
15045       if (*args)
15046         error (_("Junk at end of arguments."));
15047
15048       ALL_TRACEPOINTS (b)
15049       {
15050         t1 = (struct tracepoint *) b;
15051         trace_pass_set_count (t1, count, from_tty);
15052       }
15053     }
15054   else if (*args == '\0')
15055     {
15056       t1 = get_tracepoint_by_number (&args, NULL, 1);
15057       if (t1)
15058         trace_pass_set_count (t1, count, from_tty);
15059     }
15060   else
15061     {
15062       struct get_number_or_range_state state;
15063
15064       init_number_or_range (&state, args);
15065       while (!state.finished)
15066         {
15067           t1 = get_tracepoint_by_number (&args, &state, 1);
15068           if (t1)
15069             trace_pass_set_count (t1, count, from_tty);
15070         }
15071     }
15072 }
15073
15074 struct tracepoint *
15075 get_tracepoint (int num)
15076 {
15077   struct breakpoint *t;
15078
15079   ALL_TRACEPOINTS (t)
15080     if (t->number == num)
15081       return (struct tracepoint *) t;
15082
15083   return NULL;
15084 }
15085
15086 /* Find the tracepoint with the given target-side number (which may be
15087    different from the tracepoint number after disconnecting and
15088    reconnecting).  */
15089
15090 struct tracepoint *
15091 get_tracepoint_by_number_on_target (int num)
15092 {
15093   struct breakpoint *b;
15094
15095   ALL_TRACEPOINTS (b)
15096     {
15097       struct tracepoint *t = (struct tracepoint *) b;
15098
15099       if (t->number_on_target == num)
15100         return t;
15101     }
15102
15103   return NULL;
15104 }
15105
15106 /* Utility: parse a tracepoint number and look it up in the list.
15107    If STATE is not NULL, use, get_number_or_range_state and ignore ARG.
15108    If OPTIONAL_P is true, then if the argument is missing, the most
15109    recent tracepoint (tracepoint_count) is returned.  */
15110 struct tracepoint *
15111 get_tracepoint_by_number (char **arg,
15112                           struct get_number_or_range_state *state,
15113                           int optional_p)
15114 {
15115   extern int tracepoint_count;
15116   struct breakpoint *t;
15117   int tpnum;
15118   char *instring = arg == NULL ? NULL : *arg;
15119
15120   if (state)
15121     {
15122       gdb_assert (!state->finished);
15123       tpnum = get_number_or_range (state);
15124     }
15125   else if (arg == NULL || *arg == NULL || ! **arg)
15126     {
15127       if (optional_p)
15128         tpnum = tracepoint_count;
15129       else
15130         error_no_arg (_("tracepoint number"));
15131     }
15132   else
15133     tpnum = get_number (arg);
15134
15135   if (tpnum <= 0)
15136     {
15137       if (instring && *instring)
15138         printf_filtered (_("bad tracepoint number at or near '%s'\n"), 
15139                          instring);
15140       else
15141         printf_filtered (_("Tracepoint argument missing "
15142                            "and no previous tracepoint\n"));
15143       return NULL;
15144     }
15145
15146   ALL_TRACEPOINTS (t)
15147     if (t->number == tpnum)
15148     {
15149       return (struct tracepoint *) t;
15150     }
15151
15152   printf_unfiltered ("No tracepoint number %d.\n", tpnum);
15153   return NULL;
15154 }
15155
15156 void
15157 print_recreate_thread (struct breakpoint *b, struct ui_file *fp)
15158 {
15159   if (b->thread != -1)
15160     fprintf_unfiltered (fp, " thread %d", b->thread);
15161
15162   if (b->task != 0)
15163     fprintf_unfiltered (fp, " task %d", b->task);
15164
15165   fprintf_unfiltered (fp, "\n");
15166 }
15167
15168 /* Save information on user settable breakpoints (watchpoints, etc) to
15169    a new script file named FILENAME.  If FILTER is non-NULL, call it
15170    on each breakpoint and only include the ones for which it returns
15171    non-zero.  */
15172
15173 static void
15174 save_breakpoints (char *filename, int from_tty,
15175                   int (*filter) (const struct breakpoint *))
15176 {
15177   struct breakpoint *tp;
15178   int any = 0;
15179   char *pathname;
15180   struct cleanup *cleanup;
15181   struct ui_file *fp;
15182   int extra_trace_bits = 0;
15183
15184   if (filename == 0 || *filename == 0)
15185     error (_("Argument required (file name in which to save)"));
15186
15187   /* See if we have anything to save.  */
15188   ALL_BREAKPOINTS (tp)
15189   {
15190     /* Skip internal and momentary breakpoints.  */
15191     if (!user_breakpoint_p (tp))
15192       continue;
15193
15194     /* If we have a filter, only save the breakpoints it accepts.  */
15195     if (filter && !filter (tp))
15196       continue;
15197
15198     any = 1;
15199
15200     if (is_tracepoint (tp))
15201       {
15202         extra_trace_bits = 1;
15203
15204         /* We can stop searching.  */
15205         break;
15206       }
15207   }
15208
15209   if (!any)
15210     {
15211       warning (_("Nothing to save."));
15212       return;
15213     }
15214
15215   pathname = tilde_expand (filename);
15216   cleanup = make_cleanup (xfree, pathname);
15217   fp = gdb_fopen (pathname, "w");
15218   if (!fp)
15219     error (_("Unable to open file '%s' for saving (%s)"),
15220            filename, safe_strerror (errno));
15221   make_cleanup_ui_file_delete (fp);
15222
15223   if (extra_trace_bits)
15224     save_trace_state_variables (fp);
15225
15226   ALL_BREAKPOINTS (tp)
15227   {
15228     /* Skip internal and momentary breakpoints.  */
15229     if (!user_breakpoint_p (tp))
15230       continue;
15231
15232     /* If we have a filter, only save the breakpoints it accepts.  */
15233     if (filter && !filter (tp))
15234       continue;
15235
15236     tp->ops->print_recreate (tp, fp);
15237
15238     /* Note, we can't rely on tp->number for anything, as we can't
15239        assume the recreated breakpoint numbers will match.  Use $bpnum
15240        instead.  */
15241
15242     if (tp->cond_string)
15243       fprintf_unfiltered (fp, "  condition $bpnum %s\n", tp->cond_string);
15244
15245     if (tp->ignore_count)
15246       fprintf_unfiltered (fp, "  ignore $bpnum %d\n", tp->ignore_count);
15247
15248     if (tp->commands)
15249       {
15250         volatile struct gdb_exception ex;       
15251
15252         fprintf_unfiltered (fp, "  commands\n");
15253         
15254         ui_out_redirect (current_uiout, fp);
15255         TRY_CATCH (ex, RETURN_MASK_ALL)
15256           {
15257             print_command_lines (current_uiout, tp->commands->commands, 2);
15258           }
15259         ui_out_redirect (current_uiout, NULL);
15260
15261         if (ex.reason < 0)
15262           throw_exception (ex);
15263
15264         fprintf_unfiltered (fp, "  end\n");
15265       }
15266
15267     if (tp->enable_state == bp_disabled)
15268       fprintf_unfiltered (fp, "disable\n");
15269
15270     /* If this is a multi-location breakpoint, check if the locations
15271        should be individually disabled.  Watchpoint locations are
15272        special, and not user visible.  */
15273     if (!is_watchpoint (tp) && tp->loc && tp->loc->next)
15274       {
15275         struct bp_location *loc;
15276         int n = 1;
15277
15278         for (loc = tp->loc; loc != NULL; loc = loc->next, n++)
15279           if (!loc->enabled)
15280             fprintf_unfiltered (fp, "disable $bpnum.%d\n", n);
15281       }
15282   }
15283
15284   if (extra_trace_bits && *default_collect)
15285     fprintf_unfiltered (fp, "set default-collect %s\n", default_collect);
15286
15287   do_cleanups (cleanup);
15288   if (from_tty)
15289     printf_filtered (_("Saved to file '%s'.\n"), filename);
15290 }
15291
15292 /* The `save breakpoints' command.  */
15293
15294 static void
15295 save_breakpoints_command (char *args, int from_tty)
15296 {
15297   save_breakpoints (args, from_tty, NULL);
15298 }
15299
15300 /* The `save tracepoints' command.  */
15301
15302 static void
15303 save_tracepoints_command (char *args, int from_tty)
15304 {
15305   save_breakpoints (args, from_tty, is_tracepoint);
15306 }
15307
15308 /* Create a vector of all tracepoints.  */
15309
15310 VEC(breakpoint_p) *
15311 all_tracepoints (void)
15312 {
15313   VEC(breakpoint_p) *tp_vec = 0;
15314   struct breakpoint *tp;
15315
15316   ALL_TRACEPOINTS (tp)
15317   {
15318     VEC_safe_push (breakpoint_p, tp_vec, tp);
15319   }
15320
15321   return tp_vec;
15322 }
15323
15324 \f
15325 /* This help string is used for the break, hbreak, tbreak and thbreak
15326    commands.  It is defined as a macro to prevent duplication.
15327    COMMAND should be a string constant containing the name of the
15328    command.  */
15329 #define BREAK_ARGS_HELP(command) \
15330 command" [LOCATION] [thread THREADNUM] [if CONDITION]\n\
15331 LOCATION may be a line number, function name, or \"*\" and an address.\n\
15332 If a line number is specified, break at start of code for that line.\n\
15333 If a function is specified, break at start of code for that function.\n\
15334 If an address is specified, break at that exact address.\n\
15335 With no LOCATION, uses current execution address of the selected\n\
15336 stack frame.  This is useful for breaking on return to a stack frame.\n\
15337 \n\
15338 THREADNUM is the number from \"info threads\".\n\
15339 CONDITION is a boolean expression.\n\
15340 \n\
15341 Multiple breakpoints at one place are permitted, and useful if their\n\
15342 conditions are different.\n\
15343 \n\
15344 Do \"help breakpoints\" for info on other commands dealing with breakpoints."
15345
15346 /* List of subcommands for "catch".  */
15347 static struct cmd_list_element *catch_cmdlist;
15348
15349 /* List of subcommands for "tcatch".  */
15350 static struct cmd_list_element *tcatch_cmdlist;
15351
15352 void
15353 add_catch_command (char *name, char *docstring,
15354                    void (*sfunc) (char *args, int from_tty,
15355                                   struct cmd_list_element *command),
15356                    completer_ftype *completer,
15357                    void *user_data_catch,
15358                    void *user_data_tcatch)
15359 {
15360   struct cmd_list_element *command;
15361
15362   command = add_cmd (name, class_breakpoint, NULL, docstring,
15363                      &catch_cmdlist);
15364   set_cmd_sfunc (command, sfunc);
15365   set_cmd_context (command, user_data_catch);
15366   set_cmd_completer (command, completer);
15367
15368   command = add_cmd (name, class_breakpoint, NULL, docstring,
15369                      &tcatch_cmdlist);
15370   set_cmd_sfunc (command, sfunc);
15371   set_cmd_context (command, user_data_tcatch);
15372   set_cmd_completer (command, completer);
15373 }
15374
15375 static void
15376 clear_syscall_counts (struct inferior *inf)
15377 {
15378   struct catch_syscall_inferior_data *inf_data
15379     = get_catch_syscall_inferior_data (inf);
15380
15381   inf_data->total_syscalls_count = 0;
15382   inf_data->any_syscall_count = 0;
15383   VEC_free (int, inf_data->syscalls_counts);
15384 }
15385
15386 static void
15387 save_command (char *arg, int from_tty)
15388 {
15389   printf_unfiltered (_("\"save\" must be followed by "
15390                        "the name of a save subcommand.\n"));
15391   help_list (save_cmdlist, "save ", -1, gdb_stdout);
15392 }
15393
15394 struct breakpoint *
15395 iterate_over_breakpoints (int (*callback) (struct breakpoint *, void *),
15396                           void *data)
15397 {
15398   struct breakpoint *b, *b_tmp;
15399
15400   ALL_BREAKPOINTS_SAFE (b, b_tmp)
15401     {
15402       if ((*callback) (b, data))
15403         return b;
15404     }
15405
15406   return NULL;
15407 }
15408
15409 /* Zero if any of the breakpoint's locations could be a location where
15410    functions have been inlined, nonzero otherwise.  */
15411
15412 static int
15413 is_non_inline_function (struct breakpoint *b)
15414 {
15415   /* The shared library event breakpoint is set on the address of a
15416      non-inline function.  */
15417   if (b->type == bp_shlib_event)
15418     return 1;
15419
15420   return 0;
15421 }
15422
15423 /* Nonzero if the specified PC cannot be a location where functions
15424    have been inlined.  */
15425
15426 int
15427 pc_at_non_inline_function (struct address_space *aspace, CORE_ADDR pc,
15428                            const struct target_waitstatus *ws)
15429 {
15430   struct breakpoint *b;
15431   struct bp_location *bl;
15432
15433   ALL_BREAKPOINTS (b)
15434     {
15435       if (!is_non_inline_function (b))
15436         continue;
15437
15438       for (bl = b->loc; bl != NULL; bl = bl->next)
15439         {
15440           if (!bl->shlib_disabled
15441               && bpstat_check_location (bl, aspace, pc, ws))
15442             return 1;
15443         }
15444     }
15445
15446   return 0;
15447 }
15448
15449 void
15450 initialize_breakpoint_ops (void)
15451 {
15452   static int initialized = 0;
15453
15454   struct breakpoint_ops *ops;
15455
15456   if (initialized)
15457     return;
15458   initialized = 1;
15459
15460   /* The breakpoint_ops structure to be inherit by all kinds of
15461      breakpoints (real breakpoints, i.e., user "break" breakpoints,
15462      internal and momentary breakpoints, etc.).  */
15463   ops = &bkpt_base_breakpoint_ops;
15464   *ops = base_breakpoint_ops;
15465   ops->re_set = bkpt_re_set;
15466   ops->insert_location = bkpt_insert_location;
15467   ops->remove_location = bkpt_remove_location;
15468   ops->breakpoint_hit = bkpt_breakpoint_hit;
15469   ops->create_sals_from_address = bkpt_create_sals_from_address;
15470   ops->create_breakpoints_sal = bkpt_create_breakpoints_sal;
15471   ops->decode_linespec = bkpt_decode_linespec;
15472
15473   /* The breakpoint_ops structure to be used in regular breakpoints.  */
15474   ops = &bkpt_breakpoint_ops;
15475   *ops = bkpt_base_breakpoint_ops;
15476   ops->re_set = bkpt_re_set;
15477   ops->resources_needed = bkpt_resources_needed;
15478   ops->print_it = bkpt_print_it;
15479   ops->print_mention = bkpt_print_mention;
15480   ops->print_recreate = bkpt_print_recreate;
15481
15482   /* Ranged breakpoints.  */
15483   ops = &ranged_breakpoint_ops;
15484   *ops = bkpt_breakpoint_ops;
15485   ops->breakpoint_hit = breakpoint_hit_ranged_breakpoint;
15486   ops->resources_needed = resources_needed_ranged_breakpoint;
15487   ops->print_it = print_it_ranged_breakpoint;
15488   ops->print_one = print_one_ranged_breakpoint;
15489   ops->print_one_detail = print_one_detail_ranged_breakpoint;
15490   ops->print_mention = print_mention_ranged_breakpoint;
15491   ops->print_recreate = print_recreate_ranged_breakpoint;
15492
15493   /* Internal breakpoints.  */
15494   ops = &internal_breakpoint_ops;
15495   *ops = bkpt_base_breakpoint_ops;
15496   ops->re_set = internal_bkpt_re_set;
15497   ops->check_status = internal_bkpt_check_status;
15498   ops->print_it = internal_bkpt_print_it;
15499   ops->print_mention = internal_bkpt_print_mention;
15500
15501   /* Momentary breakpoints.  */
15502   ops = &momentary_breakpoint_ops;
15503   *ops = bkpt_base_breakpoint_ops;
15504   ops->re_set = momentary_bkpt_re_set;
15505   ops->check_status = momentary_bkpt_check_status;
15506   ops->print_it = momentary_bkpt_print_it;
15507   ops->print_mention = momentary_bkpt_print_mention;
15508
15509   /* Momentary breakpoints for bp_longjmp and bp_exception.  */
15510   ops = &longjmp_breakpoint_ops;
15511   *ops = momentary_breakpoint_ops;
15512   ops->dtor = longjmp_bkpt_dtor;
15513
15514   /* Probe breakpoints.  */
15515   ops = &bkpt_probe_breakpoint_ops;
15516   *ops = bkpt_breakpoint_ops;
15517   ops->insert_location = bkpt_probe_insert_location;
15518   ops->remove_location = bkpt_probe_remove_location;
15519   ops->create_sals_from_address = bkpt_probe_create_sals_from_address;
15520   ops->decode_linespec = bkpt_probe_decode_linespec;
15521
15522   /* GNU v3 exception catchpoints.  */
15523   ops = &gnu_v3_exception_catchpoint_ops;
15524   *ops = bkpt_breakpoint_ops;
15525   ops->print_it = print_it_exception_catchpoint;
15526   ops->print_one = print_one_exception_catchpoint;
15527   ops->print_mention = print_mention_exception_catchpoint;
15528   ops->print_recreate = print_recreate_exception_catchpoint;
15529
15530   /* Watchpoints.  */
15531   ops = &watchpoint_breakpoint_ops;
15532   *ops = base_breakpoint_ops;
15533   ops->dtor = dtor_watchpoint;
15534   ops->re_set = re_set_watchpoint;
15535   ops->insert_location = insert_watchpoint;
15536   ops->remove_location = remove_watchpoint;
15537   ops->breakpoint_hit = breakpoint_hit_watchpoint;
15538   ops->check_status = check_status_watchpoint;
15539   ops->resources_needed = resources_needed_watchpoint;
15540   ops->works_in_software_mode = works_in_software_mode_watchpoint;
15541   ops->print_it = print_it_watchpoint;
15542   ops->print_mention = print_mention_watchpoint;
15543   ops->print_recreate = print_recreate_watchpoint;
15544
15545   /* Masked watchpoints.  */
15546   ops = &masked_watchpoint_breakpoint_ops;
15547   *ops = watchpoint_breakpoint_ops;
15548   ops->insert_location = insert_masked_watchpoint;
15549   ops->remove_location = remove_masked_watchpoint;
15550   ops->resources_needed = resources_needed_masked_watchpoint;
15551   ops->works_in_software_mode = works_in_software_mode_masked_watchpoint;
15552   ops->print_it = print_it_masked_watchpoint;
15553   ops->print_one_detail = print_one_detail_masked_watchpoint;
15554   ops->print_mention = print_mention_masked_watchpoint;
15555   ops->print_recreate = print_recreate_masked_watchpoint;
15556
15557   /* Tracepoints.  */
15558   ops = &tracepoint_breakpoint_ops;
15559   *ops = base_breakpoint_ops;
15560   ops->re_set = tracepoint_re_set;
15561   ops->breakpoint_hit = tracepoint_breakpoint_hit;
15562   ops->print_one_detail = tracepoint_print_one_detail;
15563   ops->print_mention = tracepoint_print_mention;
15564   ops->print_recreate = tracepoint_print_recreate;
15565   ops->create_sals_from_address = tracepoint_create_sals_from_address;
15566   ops->create_breakpoints_sal = tracepoint_create_breakpoints_sal;
15567   ops->decode_linespec = tracepoint_decode_linespec;
15568
15569   /* Probe tracepoints.  */
15570   ops = &tracepoint_probe_breakpoint_ops;
15571   *ops = tracepoint_breakpoint_ops;
15572   ops->create_sals_from_address = tracepoint_probe_create_sals_from_address;
15573   ops->decode_linespec = tracepoint_probe_decode_linespec;
15574
15575   /* Static tracepoints with marker (`-m').  */
15576   ops = &strace_marker_breakpoint_ops;
15577   *ops = tracepoint_breakpoint_ops;
15578   ops->create_sals_from_address = strace_marker_create_sals_from_address;
15579   ops->create_breakpoints_sal = strace_marker_create_breakpoints_sal;
15580   ops->decode_linespec = strace_marker_decode_linespec;
15581
15582   /* Fork catchpoints.  */
15583   ops = &catch_fork_breakpoint_ops;
15584   *ops = base_breakpoint_ops;
15585   ops->insert_location = insert_catch_fork;
15586   ops->remove_location = remove_catch_fork;
15587   ops->breakpoint_hit = breakpoint_hit_catch_fork;
15588   ops->print_it = print_it_catch_fork;
15589   ops->print_one = print_one_catch_fork;
15590   ops->print_mention = print_mention_catch_fork;
15591   ops->print_recreate = print_recreate_catch_fork;
15592
15593   /* Vfork catchpoints.  */
15594   ops = &catch_vfork_breakpoint_ops;
15595   *ops = base_breakpoint_ops;
15596   ops->insert_location = insert_catch_vfork;
15597   ops->remove_location = remove_catch_vfork;
15598   ops->breakpoint_hit = breakpoint_hit_catch_vfork;
15599   ops->print_it = print_it_catch_vfork;
15600   ops->print_one = print_one_catch_vfork;
15601   ops->print_mention = print_mention_catch_vfork;
15602   ops->print_recreate = print_recreate_catch_vfork;
15603
15604   /* Exec catchpoints.  */
15605   ops = &catch_exec_breakpoint_ops;
15606   *ops = base_breakpoint_ops;
15607   ops->dtor = dtor_catch_exec;
15608   ops->insert_location = insert_catch_exec;
15609   ops->remove_location = remove_catch_exec;
15610   ops->breakpoint_hit = breakpoint_hit_catch_exec;
15611   ops->print_it = print_it_catch_exec;
15612   ops->print_one = print_one_catch_exec;
15613   ops->print_mention = print_mention_catch_exec;
15614   ops->print_recreate = print_recreate_catch_exec;
15615
15616   /* Syscall catchpoints.  */
15617   ops = &catch_syscall_breakpoint_ops;
15618   *ops = base_breakpoint_ops;
15619   ops->dtor = dtor_catch_syscall;
15620   ops->insert_location = insert_catch_syscall;
15621   ops->remove_location = remove_catch_syscall;
15622   ops->breakpoint_hit = breakpoint_hit_catch_syscall;
15623   ops->print_it = print_it_catch_syscall;
15624   ops->print_one = print_one_catch_syscall;
15625   ops->print_mention = print_mention_catch_syscall;
15626   ops->print_recreate = print_recreate_catch_syscall;
15627
15628   /* Solib-related catchpoints.  */
15629   ops = &catch_solib_breakpoint_ops;
15630   *ops = base_breakpoint_ops;
15631   ops->dtor = dtor_catch_solib;
15632   ops->insert_location = insert_catch_solib;
15633   ops->remove_location = remove_catch_solib;
15634   ops->breakpoint_hit = breakpoint_hit_catch_solib;
15635   ops->check_status = check_status_catch_solib;
15636   ops->print_it = print_it_catch_solib;
15637   ops->print_one = print_one_catch_solib;
15638   ops->print_mention = print_mention_catch_solib;
15639   ops->print_recreate = print_recreate_catch_solib;
15640
15641   ops = &dprintf_breakpoint_ops;
15642   *ops = bkpt_base_breakpoint_ops;
15643   ops->re_set = bkpt_re_set;
15644   ops->resources_needed = bkpt_resources_needed;
15645   ops->print_it = bkpt_print_it;
15646   ops->print_mention = bkpt_print_mention;
15647   ops->print_recreate = bkpt_print_recreate;
15648 }
15649
15650 void
15651 _initialize_breakpoint (void)
15652 {
15653   struct cmd_list_element *c;
15654
15655   initialize_breakpoint_ops ();
15656
15657   observer_attach_solib_unloaded (disable_breakpoints_in_unloaded_shlib);
15658   observer_attach_inferior_exit (clear_syscall_counts);
15659   observer_attach_memory_changed (invalidate_bp_value_on_memory_change);
15660
15661   breakpoint_objfile_key
15662     = register_objfile_data_with_cleanup (NULL, free_breakpoint_probes);
15663
15664   catch_syscall_inferior_data
15665     = register_inferior_data_with_cleanup (catch_syscall_inferior_data_cleanup);
15666
15667   breakpoint_chain = 0;
15668   /* Don't bother to call set_breakpoint_count.  $bpnum isn't useful
15669      before a breakpoint is set.  */
15670   breakpoint_count = 0;
15671
15672   tracepoint_count = 0;
15673
15674   add_com ("ignore", class_breakpoint, ignore_command, _("\
15675 Set ignore-count of breakpoint number N to COUNT.\n\
15676 Usage is `ignore N COUNT'."));
15677   if (xdb_commands)
15678     add_com_alias ("bc", "ignore", class_breakpoint, 1);
15679
15680   add_com ("commands", class_breakpoint, commands_command, _("\
15681 Set commands to be executed when a breakpoint is hit.\n\
15682 Give breakpoint number as argument after \"commands\".\n\
15683 With no argument, the targeted breakpoint is the last one set.\n\
15684 The commands themselves follow starting on the next line.\n\
15685 Type a line containing \"end\" to indicate the end of them.\n\
15686 Give \"silent\" as the first line to make the breakpoint silent;\n\
15687 then no output is printed when it is hit, except what the commands print."));
15688
15689   c = add_com ("condition", class_breakpoint, condition_command, _("\
15690 Specify breakpoint number N to break only if COND is true.\n\
15691 Usage is `condition N COND', where N is an integer and COND is an\n\
15692 expression to be evaluated whenever breakpoint N is reached."));
15693   set_cmd_completer (c, condition_completer);
15694
15695   c = add_com ("tbreak", class_breakpoint, tbreak_command, _("\
15696 Set a temporary breakpoint.\n\
15697 Like \"break\" except the breakpoint is only temporary,\n\
15698 so it will be deleted when hit.  Equivalent to \"break\" followed\n\
15699 by using \"enable delete\" on the breakpoint number.\n\
15700 \n"
15701 BREAK_ARGS_HELP ("tbreak")));
15702   set_cmd_completer (c, location_completer);
15703
15704   c = add_com ("hbreak", class_breakpoint, hbreak_command, _("\
15705 Set a hardware assisted breakpoint.\n\
15706 Like \"break\" except the breakpoint requires hardware support,\n\
15707 some target hardware may not have this support.\n\
15708 \n"
15709 BREAK_ARGS_HELP ("hbreak")));
15710   set_cmd_completer (c, location_completer);
15711
15712   c = add_com ("thbreak", class_breakpoint, thbreak_command, _("\
15713 Set a temporary hardware assisted breakpoint.\n\
15714 Like \"hbreak\" except the breakpoint is only temporary,\n\
15715 so it will be deleted when hit.\n\
15716 \n"
15717 BREAK_ARGS_HELP ("thbreak")));
15718   set_cmd_completer (c, location_completer);
15719
15720   add_prefix_cmd ("enable", class_breakpoint, enable_command, _("\
15721 Enable some breakpoints.\n\
15722 Give breakpoint numbers (separated by spaces) as arguments.\n\
15723 With no subcommand, breakpoints are enabled until you command otherwise.\n\
15724 This is used to cancel the effect of the \"disable\" command.\n\
15725 With a subcommand you can enable temporarily."),
15726                   &enablelist, "enable ", 1, &cmdlist);
15727   if (xdb_commands)
15728     add_com ("ab", class_breakpoint, enable_command, _("\
15729 Enable some breakpoints.\n\
15730 Give breakpoint numbers (separated by spaces) as arguments.\n\
15731 With no subcommand, breakpoints are enabled until you command otherwise.\n\
15732 This is used to cancel the effect of the \"disable\" command.\n\
15733 With a subcommand you can enable temporarily."));
15734
15735   add_com_alias ("en", "enable", class_breakpoint, 1);
15736
15737   add_prefix_cmd ("breakpoints", class_breakpoint, enable_command, _("\
15738 Enable some breakpoints.\n\
15739 Give breakpoint numbers (separated by spaces) as arguments.\n\
15740 This is used to cancel the effect of the \"disable\" command.\n\
15741 May be abbreviated to simply \"enable\".\n"),
15742                    &enablebreaklist, "enable breakpoints ", 1, &enablelist);
15743
15744   add_cmd ("once", no_class, enable_once_command, _("\
15745 Enable breakpoints for one hit.  Give breakpoint numbers.\n\
15746 If a breakpoint is hit while enabled in this fashion, it becomes disabled."),
15747            &enablebreaklist);
15748
15749   add_cmd ("delete", no_class, enable_delete_command, _("\
15750 Enable breakpoints and delete when hit.  Give breakpoint numbers.\n\
15751 If a breakpoint is hit while enabled in this fashion, it is deleted."),
15752            &enablebreaklist);
15753
15754   add_cmd ("count", no_class, enable_count_command, _("\
15755 Enable breakpoints for COUNT hits.  Give count and then breakpoint numbers.\n\
15756 If a breakpoint is hit while enabled in this fashion,\n\
15757 the count is decremented; when it reaches zero, the breakpoint is disabled."),
15758            &enablebreaklist);
15759
15760   add_cmd ("delete", no_class, enable_delete_command, _("\
15761 Enable breakpoints and delete when hit.  Give breakpoint numbers.\n\
15762 If a breakpoint is hit while enabled in this fashion, it is deleted."),
15763            &enablelist);
15764
15765   add_cmd ("once", no_class, enable_once_command, _("\
15766 Enable breakpoints for one hit.  Give breakpoint numbers.\n\
15767 If a breakpoint is hit while enabled in this fashion, it becomes disabled."),
15768            &enablelist);
15769
15770   add_cmd ("count", no_class, enable_count_command, _("\
15771 Enable breakpoints for COUNT hits.  Give count and then breakpoint numbers.\n\
15772 If a breakpoint is hit while enabled in this fashion,\n\
15773 the count is decremented; when it reaches zero, the breakpoint is disabled."),
15774            &enablelist);
15775
15776   add_prefix_cmd ("disable", class_breakpoint, disable_command, _("\
15777 Disable some breakpoints.\n\
15778 Arguments are breakpoint numbers with spaces in between.\n\
15779 To disable all breakpoints, give no argument.\n\
15780 A disabled breakpoint is not forgotten, but has no effect until re-enabled."),
15781                   &disablelist, "disable ", 1, &cmdlist);
15782   add_com_alias ("dis", "disable", class_breakpoint, 1);
15783   add_com_alias ("disa", "disable", class_breakpoint, 1);
15784   if (xdb_commands)
15785     add_com ("sb", class_breakpoint, disable_command, _("\
15786 Disable some breakpoints.\n\
15787 Arguments are breakpoint numbers with spaces in between.\n\
15788 To disable all breakpoints, give no argument.\n\
15789 A disabled breakpoint is not forgotten, but has no effect until re-enabled."));
15790
15791   add_cmd ("breakpoints", class_alias, disable_command, _("\
15792 Disable some breakpoints.\n\
15793 Arguments are breakpoint numbers with spaces in between.\n\
15794 To disable all breakpoints, give no argument.\n\
15795 A disabled breakpoint is not forgotten, but has no effect until re-enabled.\n\
15796 This command may be abbreviated \"disable\"."),
15797            &disablelist);
15798
15799   add_prefix_cmd ("delete", class_breakpoint, delete_command, _("\
15800 Delete some breakpoints or auto-display expressions.\n\
15801 Arguments are breakpoint numbers with spaces in between.\n\
15802 To delete all breakpoints, give no argument.\n\
15803 \n\
15804 Also a prefix command for deletion of other GDB objects.\n\
15805 The \"unset\" command is also an alias for \"delete\"."),
15806                   &deletelist, "delete ", 1, &cmdlist);
15807   add_com_alias ("d", "delete", class_breakpoint, 1);
15808   add_com_alias ("del", "delete", class_breakpoint, 1);
15809   if (xdb_commands)
15810     add_com ("db", class_breakpoint, delete_command, _("\
15811 Delete some breakpoints.\n\
15812 Arguments are breakpoint numbers with spaces in between.\n\
15813 To delete all breakpoints, give no argument.\n"));
15814
15815   add_cmd ("breakpoints", class_alias, delete_command, _("\
15816 Delete some breakpoints or auto-display expressions.\n\
15817 Arguments are breakpoint numbers with spaces in between.\n\
15818 To delete all breakpoints, give no argument.\n\
15819 This command may be abbreviated \"delete\"."),
15820            &deletelist);
15821
15822   add_com ("clear", class_breakpoint, clear_command, _("\
15823 Clear breakpoint at specified line or function.\n\
15824 Argument may be line number, function name, or \"*\" and an address.\n\
15825 If line number is specified, all breakpoints in that line are cleared.\n\
15826 If function is specified, breakpoints at beginning of function are cleared.\n\
15827 If an address is specified, breakpoints at that address are cleared.\n\
15828 \n\
15829 With no argument, clears all breakpoints in the line that the selected frame\n\
15830 is executing in.\n\
15831 \n\
15832 See also the \"delete\" command which clears breakpoints by number."));
15833   add_com_alias ("cl", "clear", class_breakpoint, 1);
15834
15835   c = add_com ("break", class_breakpoint, break_command, _("\
15836 Set breakpoint at specified line or function.\n"
15837 BREAK_ARGS_HELP ("break")));
15838   set_cmd_completer (c, location_completer);
15839
15840   add_com_alias ("b", "break", class_run, 1);
15841   add_com_alias ("br", "break", class_run, 1);
15842   add_com_alias ("bre", "break", class_run, 1);
15843   add_com_alias ("brea", "break", class_run, 1);
15844
15845   if (xdb_commands)
15846    add_com_alias ("ba", "break", class_breakpoint, 1);
15847
15848   if (dbx_commands)
15849     {
15850       add_abbrev_prefix_cmd ("stop", class_breakpoint, stop_command, _("\
15851 Break in function/address or break at a line in the current file."),
15852                              &stoplist, "stop ", 1, &cmdlist);
15853       add_cmd ("in", class_breakpoint, stopin_command,
15854                _("Break in function or address."), &stoplist);
15855       add_cmd ("at", class_breakpoint, stopat_command,
15856                _("Break at a line in the current file."), &stoplist);
15857       add_com ("status", class_info, breakpoints_info, _("\
15858 Status of user-settable breakpoints, or breakpoint number NUMBER.\n\
15859 The \"Type\" column indicates one of:\n\
15860 \tbreakpoint     - normal breakpoint\n\
15861 \twatchpoint     - watchpoint\n\
15862 The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
15863 the disposition of the breakpoint after it gets hit.  \"dis\" means that the\n\
15864 breakpoint will be disabled.  The \"Address\" and \"What\" columns indicate the\n\
15865 address and file/line number respectively.\n\
15866 \n\
15867 Convenience variable \"$_\" and default examine address for \"x\"\n\
15868 are set to the address of the last breakpoint listed unless the command\n\
15869 is prefixed with \"server \".\n\n\
15870 Convenience variable \"$bpnum\" contains the number of the last\n\
15871 breakpoint set."));
15872     }
15873
15874   add_info ("breakpoints", breakpoints_info, _("\
15875 Status of specified breakpoints (all user-settable breakpoints if no argument).\n\
15876 The \"Type\" column indicates one of:\n\
15877 \tbreakpoint     - normal breakpoint\n\
15878 \twatchpoint     - watchpoint\n\
15879 The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
15880 the disposition of the breakpoint after it gets hit.  \"dis\" means that the\n\
15881 breakpoint will be disabled.  The \"Address\" and \"What\" columns indicate the\n\
15882 address and file/line number respectively.\n\
15883 \n\
15884 Convenience variable \"$_\" and default examine address for \"x\"\n\
15885 are set to the address of the last breakpoint listed unless the command\n\
15886 is prefixed with \"server \".\n\n\
15887 Convenience variable \"$bpnum\" contains the number of the last\n\
15888 breakpoint set."));
15889
15890   add_info_alias ("b", "breakpoints", 1);
15891
15892   if (xdb_commands)
15893     add_com ("lb", class_breakpoint, breakpoints_info, _("\
15894 Status of user-settable breakpoints, or breakpoint number NUMBER.\n\
15895 The \"Type\" column indicates one of:\n\
15896 \tbreakpoint     - normal breakpoint\n\
15897 \twatchpoint     - watchpoint\n\
15898 The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
15899 the disposition of the breakpoint after it gets hit.  \"dis\" means that the\n\
15900 breakpoint will be disabled.  The \"Address\" and \"What\" columns indicate the\n\
15901 address and file/line number respectively.\n\
15902 \n\
15903 Convenience variable \"$_\" and default examine address for \"x\"\n\
15904 are set to the address of the last breakpoint listed unless the command\n\
15905 is prefixed with \"server \".\n\n\
15906 Convenience variable \"$bpnum\" contains the number of the last\n\
15907 breakpoint set."));
15908
15909   add_cmd ("breakpoints", class_maintenance, maintenance_info_breakpoints, _("\
15910 Status of all breakpoints, or breakpoint number NUMBER.\n\
15911 The \"Type\" column indicates one of:\n\
15912 \tbreakpoint     - normal breakpoint\n\
15913 \twatchpoint     - watchpoint\n\
15914 \tlongjmp        - internal breakpoint used to step through longjmp()\n\
15915 \tlongjmp resume - internal breakpoint at the target of longjmp()\n\
15916 \tuntil          - internal breakpoint used by the \"until\" command\n\
15917 \tfinish         - internal breakpoint used by the \"finish\" command\n\
15918 The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
15919 the disposition of the breakpoint after it gets hit.  \"dis\" means that the\n\
15920 breakpoint will be disabled.  The \"Address\" and \"What\" columns indicate the\n\
15921 address and file/line number respectively.\n\
15922 \n\
15923 Convenience variable \"$_\" and default examine address for \"x\"\n\
15924 are set to the address of the last breakpoint listed unless the command\n\
15925 is prefixed with \"server \".\n\n\
15926 Convenience variable \"$bpnum\" contains the number of the last\n\
15927 breakpoint set."),
15928            &maintenanceinfolist);
15929
15930   add_prefix_cmd ("catch", class_breakpoint, catch_command, _("\
15931 Set catchpoints to catch events."),
15932                   &catch_cmdlist, "catch ",
15933                   0/*allow-unknown*/, &cmdlist);
15934
15935   add_prefix_cmd ("tcatch", class_breakpoint, tcatch_command, _("\
15936 Set temporary catchpoints to catch events."),
15937                   &tcatch_cmdlist, "tcatch ",
15938                   0/*allow-unknown*/, &cmdlist);
15939
15940   /* Add catch and tcatch sub-commands.  */
15941   add_catch_command ("catch", _("\
15942 Catch an exception, when caught."),
15943                      catch_catch_command,
15944                      NULL,
15945                      CATCH_PERMANENT,
15946                      CATCH_TEMPORARY);
15947   add_catch_command ("throw", _("\
15948 Catch an exception, when thrown."),
15949                      catch_throw_command,
15950                      NULL,
15951                      CATCH_PERMANENT,
15952                      CATCH_TEMPORARY);
15953   add_catch_command ("fork", _("Catch calls to fork."),
15954                      catch_fork_command_1,
15955                      NULL,
15956                      (void *) (uintptr_t) catch_fork_permanent,
15957                      (void *) (uintptr_t) catch_fork_temporary);
15958   add_catch_command ("vfork", _("Catch calls to vfork."),
15959                      catch_fork_command_1,
15960                      NULL,
15961                      (void *) (uintptr_t) catch_vfork_permanent,
15962                      (void *) (uintptr_t) catch_vfork_temporary);
15963   add_catch_command ("exec", _("Catch calls to exec."),
15964                      catch_exec_command_1,
15965                      NULL,
15966                      CATCH_PERMANENT,
15967                      CATCH_TEMPORARY);
15968   add_catch_command ("load", _("Catch loads of shared libraries.\n\
15969 Usage: catch load [REGEX]\n\
15970 If REGEX is given, only stop for libraries matching the regular expression."),
15971                      catch_load_command_1,
15972                      NULL,
15973                      CATCH_PERMANENT,
15974                      CATCH_TEMPORARY);
15975   add_catch_command ("unload", _("Catch unloads of shared libraries.\n\
15976 Usage: catch unload [REGEX]\n\
15977 If REGEX is given, only stop for libraries matching the regular expression."),
15978                      catch_unload_command_1,
15979                      NULL,
15980                      CATCH_PERMANENT,
15981                      CATCH_TEMPORARY);
15982   add_catch_command ("syscall", _("\
15983 Catch system calls by their names and/or numbers.\n\
15984 Arguments say which system calls to catch.  If no arguments\n\
15985 are given, every system call will be caught.\n\
15986 Arguments, if given, should be one or more system call names\n\
15987 (if your system supports that), or system call numbers."),
15988                      catch_syscall_command_1,
15989                      catch_syscall_completer,
15990                      CATCH_PERMANENT,
15991                      CATCH_TEMPORARY);
15992
15993   c = add_com ("watch", class_breakpoint, watch_command, _("\
15994 Set a watchpoint for an expression.\n\
15995 Usage: watch [-l|-location] EXPRESSION\n\
15996 A watchpoint stops execution of your program whenever the value of\n\
15997 an expression changes.\n\
15998 If -l or -location is given, this evaluates EXPRESSION and watches\n\
15999 the memory to which it refers."));
16000   set_cmd_completer (c, expression_completer);
16001
16002   c = add_com ("rwatch", class_breakpoint, rwatch_command, _("\
16003 Set a read watchpoint for an expression.\n\
16004 Usage: rwatch [-l|-location] EXPRESSION\n\
16005 A watchpoint stops execution of your program whenever the value of\n\
16006 an expression is read.\n\
16007 If -l or -location is given, this evaluates EXPRESSION and watches\n\
16008 the memory to which it refers."));
16009   set_cmd_completer (c, expression_completer);
16010
16011   c = add_com ("awatch", class_breakpoint, awatch_command, _("\
16012 Set a watchpoint for an expression.\n\
16013 Usage: awatch [-l|-location] EXPRESSION\n\
16014 A watchpoint stops execution of your program whenever the value of\n\
16015 an expression is either read or written.\n\
16016 If -l or -location is given, this evaluates EXPRESSION and watches\n\
16017 the memory to which it refers."));
16018   set_cmd_completer (c, expression_completer);
16019
16020   add_info ("watchpoints", watchpoints_info, _("\
16021 Status of specified watchpoints (all watchpoints if no argument)."));
16022
16023   /* XXX: cagney/2005-02-23: This should be a boolean, and should
16024      respond to changes - contrary to the description.  */
16025   add_setshow_zinteger_cmd ("can-use-hw-watchpoints", class_support,
16026                             &can_use_hw_watchpoints, _("\
16027 Set debugger's willingness to use watchpoint hardware."), _("\
16028 Show debugger's willingness to use watchpoint hardware."), _("\
16029 If zero, gdb will not use hardware for new watchpoints, even if\n\
16030 such is available.  (However, any hardware watchpoints that were\n\
16031 created before setting this to nonzero, will continue to use watchpoint\n\
16032 hardware.)"),
16033                             NULL,
16034                             show_can_use_hw_watchpoints,
16035                             &setlist, &showlist);
16036
16037   can_use_hw_watchpoints = 1;
16038
16039   /* Tracepoint manipulation commands.  */
16040
16041   c = add_com ("trace", class_breakpoint, trace_command, _("\
16042 Set a tracepoint at specified line or function.\n\
16043 \n"
16044 BREAK_ARGS_HELP ("trace") "\n\
16045 Do \"help tracepoints\" for info on other tracepoint commands."));
16046   set_cmd_completer (c, location_completer);
16047
16048   add_com_alias ("tp", "trace", class_alias, 0);
16049   add_com_alias ("tr", "trace", class_alias, 1);
16050   add_com_alias ("tra", "trace", class_alias, 1);
16051   add_com_alias ("trac", "trace", class_alias, 1);
16052
16053   c = add_com ("ftrace", class_breakpoint, ftrace_command, _("\
16054 Set a fast tracepoint at specified line or function.\n\
16055 \n"
16056 BREAK_ARGS_HELP ("ftrace") "\n\
16057 Do \"help tracepoints\" for info on other tracepoint commands."));
16058   set_cmd_completer (c, location_completer);
16059
16060   c = add_com ("strace", class_breakpoint, strace_command, _("\
16061 Set a static tracepoint at specified line, function or marker.\n\
16062 \n\
16063 strace [LOCATION] [if CONDITION]\n\
16064 LOCATION may be a line number, function name, \"*\" and an address,\n\
16065 or -m MARKER_ID.\n\
16066 If a line number is specified, probe the marker at start of code\n\
16067 for that line.  If a function is specified, probe the marker at start\n\
16068 of code for that function.  If an address is specified, probe the marker\n\
16069 at that exact address.  If a marker id is specified, probe the marker\n\
16070 with that name.  With no LOCATION, uses current execution address of\n\
16071 the selected stack frame.\n\
16072 Static tracepoints accept an extra collect action -- ``collect $_sdata''.\n\
16073 This collects arbitrary user data passed in the probe point call to the\n\
16074 tracing library.  You can inspect it when analyzing the trace buffer,\n\
16075 by printing the $_sdata variable like any other convenience variable.\n\
16076 \n\
16077 CONDITION is a boolean expression.\n\
16078 \n\
16079 Multiple tracepoints at one place are permitted, and useful if their\n\
16080 conditions are different.\n\
16081 \n\
16082 Do \"help breakpoints\" for info on other commands dealing with breakpoints.\n\
16083 Do \"help tracepoints\" for info on other tracepoint commands."));
16084   set_cmd_completer (c, location_completer);
16085
16086   add_info ("tracepoints", tracepoints_info, _("\
16087 Status of specified tracepoints (all tracepoints if no argument).\n\
16088 Convenience variable \"$tpnum\" contains the number of the\n\
16089 last tracepoint set."));
16090
16091   add_info_alias ("tp", "tracepoints", 1);
16092
16093   add_cmd ("tracepoints", class_trace, delete_trace_command, _("\
16094 Delete specified tracepoints.\n\
16095 Arguments are tracepoint numbers, separated by spaces.\n\
16096 No argument means delete all tracepoints."),
16097            &deletelist);
16098
16099   c = add_cmd ("tracepoints", class_trace, disable_trace_command, _("\
16100 Disable specified tracepoints.\n\
16101 Arguments are tracepoint numbers, separated by spaces.\n\
16102 No argument means disable all tracepoints."),
16103            &disablelist);
16104   deprecate_cmd (c, "disable");
16105
16106   c = add_cmd ("tracepoints", class_trace, enable_trace_command, _("\
16107 Enable specified tracepoints.\n\
16108 Arguments are tracepoint numbers, separated by spaces.\n\
16109 No argument means enable all tracepoints."),
16110            &enablelist);
16111   deprecate_cmd (c, "enable");
16112
16113   add_com ("passcount", class_trace, trace_pass_command, _("\
16114 Set the passcount for a tracepoint.\n\
16115 The trace will end when the tracepoint has been passed 'count' times.\n\
16116 Usage: passcount COUNT TPNUM, where TPNUM may also be \"all\";\n\
16117 if TPNUM is omitted, passcount refers to the last tracepoint defined."));
16118
16119   add_prefix_cmd ("save", class_breakpoint, save_command,
16120                   _("Save breakpoint definitions as a script."),
16121                   &save_cmdlist, "save ",
16122                   0/*allow-unknown*/, &cmdlist);
16123
16124   c = add_cmd ("breakpoints", class_breakpoint, save_breakpoints_command, _("\
16125 Save current breakpoint definitions as a script.\n\
16126 This includes all types of breakpoints (breakpoints, watchpoints,\n\
16127 catchpoints, tracepoints).  Use the 'source' command in another debug\n\
16128 session to restore them."),
16129                &save_cmdlist);
16130   set_cmd_completer (c, filename_completer);
16131
16132   c = add_cmd ("tracepoints", class_trace, save_tracepoints_command, _("\
16133 Save current tracepoint definitions as a script.\n\
16134 Use the 'source' command in another debug session to restore them."),
16135                &save_cmdlist);
16136   set_cmd_completer (c, filename_completer);
16137
16138   c = add_com_alias ("save-tracepoints", "save tracepoints", class_trace, 0);
16139   deprecate_cmd (c, "save tracepoints");
16140
16141   add_prefix_cmd ("breakpoint", class_maintenance, set_breakpoint_cmd, _("\
16142 Breakpoint specific settings\n\
16143 Configure various breakpoint-specific variables such as\n\
16144 pending breakpoint behavior"),
16145                   &breakpoint_set_cmdlist, "set breakpoint ",
16146                   0/*allow-unknown*/, &setlist);
16147   add_prefix_cmd ("breakpoint", class_maintenance, show_breakpoint_cmd, _("\
16148 Breakpoint specific settings\n\
16149 Configure various breakpoint-specific variables such as\n\
16150 pending breakpoint behavior"),
16151                   &breakpoint_show_cmdlist, "show breakpoint ",
16152                   0/*allow-unknown*/, &showlist);
16153
16154   add_setshow_auto_boolean_cmd ("pending", no_class,
16155                                 &pending_break_support, _("\
16156 Set debugger's behavior regarding pending breakpoints."), _("\
16157 Show debugger's behavior regarding pending breakpoints."), _("\
16158 If on, an unrecognized breakpoint location will cause gdb to create a\n\
16159 pending breakpoint.  If off, an unrecognized breakpoint location results in\n\
16160 an error.  If auto, an unrecognized breakpoint location results in a\n\
16161 user-query to see if a pending breakpoint should be created."),
16162                                 NULL,
16163                                 show_pending_break_support,
16164                                 &breakpoint_set_cmdlist,
16165                                 &breakpoint_show_cmdlist);
16166
16167   pending_break_support = AUTO_BOOLEAN_AUTO;
16168
16169   add_setshow_boolean_cmd ("auto-hw", no_class,
16170                            &automatic_hardware_breakpoints, _("\
16171 Set automatic usage of hardware breakpoints."), _("\
16172 Show automatic usage of hardware breakpoints."), _("\
16173 If set, the debugger will automatically use hardware breakpoints for\n\
16174 breakpoints set with \"break\" but falling in read-only memory.  If not set,\n\
16175 a warning will be emitted for such breakpoints."),
16176                            NULL,
16177                            show_automatic_hardware_breakpoints,
16178                            &breakpoint_set_cmdlist,
16179                            &breakpoint_show_cmdlist);
16180
16181   add_setshow_enum_cmd ("always-inserted", class_support,
16182                         always_inserted_enums, &always_inserted_mode, _("\
16183 Set mode for inserting breakpoints."), _("\
16184 Show mode for inserting breakpoints."), _("\
16185 When this mode is off, breakpoints are inserted in inferior when it is\n\
16186 resumed, and removed when execution stops.  When this mode is on,\n\
16187 breakpoints are inserted immediately and removed only when the user\n\
16188 deletes the breakpoint.  When this mode is auto (which is the default),\n\
16189 the behaviour depends on the non-stop setting (see help set non-stop).\n\
16190 In this case, if gdb is controlling the inferior in non-stop mode, gdb\n\
16191 behaves as if always-inserted mode is on; if gdb is controlling the\n\
16192 inferior in all-stop mode, gdb behaves as if always-inserted mode is off."),
16193                            NULL,
16194                            &show_always_inserted_mode,
16195                            &breakpoint_set_cmdlist,
16196                            &breakpoint_show_cmdlist);
16197
16198   add_setshow_enum_cmd ("condition-evaluation", class_breakpoint,
16199                         condition_evaluation_enums,
16200                         &condition_evaluation_mode_1, _("\
16201 Set mode of breakpoint condition evaluation."), _("\
16202 Show mode of breakpoint condition evaluation."), _("\
16203 When this is set to \"host\", breakpoint conditions will be\n\
16204 evaluated on the host's side by GDB.  When it is set to \"target\",\n\
16205 breakpoint conditions will be downloaded to the target (if the target\n\
16206 supports such feature) and conditions will be evaluated on the target's side.\n\
16207 If this is set to \"auto\" (default), this will be automatically set to\n\
16208 \"target\" if it supports condition evaluation, otherwise it will\n\
16209 be set to \"gdb\""),
16210                            &set_condition_evaluation_mode,
16211                            &show_condition_evaluation_mode,
16212                            &breakpoint_set_cmdlist,
16213                            &breakpoint_show_cmdlist);
16214
16215   add_com ("break-range", class_breakpoint, break_range_command, _("\
16216 Set a breakpoint for an address range.\n\
16217 break-range START-LOCATION, END-LOCATION\n\
16218 where START-LOCATION and END-LOCATION can be one of the following:\n\
16219   LINENUM, for that line in the current file,\n\
16220   FILE:LINENUM, for that line in that file,\n\
16221   +OFFSET, for that number of lines after the current line\n\
16222            or the start of the range\n\
16223   FUNCTION, for the first line in that function,\n\
16224   FILE:FUNCTION, to distinguish among like-named static functions.\n\
16225   *ADDRESS, for the instruction at that address.\n\
16226 \n\
16227 The breakpoint will stop execution of the inferior whenever it executes\n\
16228 an instruction at any address within the [START-LOCATION, END-LOCATION]\n\
16229 range (including START-LOCATION and END-LOCATION)."));
16230
16231   c = add_com ("dprintf", class_breakpoint, dprintf_command, _("\
16232 Set a dynamic printf at specified line or function.\n\
16233 dprintf location,format string,arg1,arg2,...\n\
16234 location may be a line number, function name, or \"*\" and an address.\n\
16235 If a line number is specified, break at start of code for that line.\n\
16236 If a function is specified, break at start of code for that function.\n\
16237 "));
16238   set_cmd_completer (c, location_completer);
16239
16240   add_setshow_enum_cmd ("dprintf-style", class_support,
16241                         dprintf_style_enums, &dprintf_style, _("\
16242 Set the style of usage for dynamic printf."), _("\
16243 Show the style of usage for dynamic printf."), _("\
16244 This setting chooses how GDB will do a dynamic printf.\n\
16245 If the value is \"gdb\", then the printing is done by GDB to its own\n\
16246 console, as with the \"printf\" command.\n\
16247 If the value is \"call\", the print is done by calling a function in your\n\
16248 program; by default printf(), but you can choose a different function or\n\
16249 output stream by setting dprintf-function and dprintf-channel."),
16250                         update_dprintf_commands, NULL,
16251                         &setlist, &showlist);
16252
16253   dprintf_function = xstrdup ("printf");
16254   add_setshow_string_cmd ("dprintf-function", class_support,
16255                           &dprintf_function, _("\
16256 Set the function to use for dynamic printf"), _("\
16257 Show the function to use for dynamic printf"), NULL,
16258                           update_dprintf_commands, NULL,
16259                           &setlist, &showlist);
16260
16261   dprintf_channel = xstrdup ("");
16262   add_setshow_string_cmd ("dprintf-channel", class_support,
16263                           &dprintf_channel, _("\
16264 Set the channel to use for dynamic printf"), _("\
16265 Show the channel to use for dynamic printf"), NULL,
16266                           update_dprintf_commands, NULL,
16267                           &setlist, &showlist);
16268
16269   automatic_hardware_breakpoints = 1;
16270
16271   observer_attach_about_to_proceed (breakpoint_about_to_proceed);
16272 }