9afbdbdd58e6ad45f4d239128555b9b810624fbf
[external/binutils.git] / gdb / breakpoint.c
1 /* Everything about breakpoints, for GDB.
2
3    Copyright (C) 1986-2016 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 "infrun.h"
36 #include "gdbthread.h"
37 #include "target.h"
38 #include "language.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 "block.h"
51 #include "solib.h"
52 #include "solist.h"
53 #include "observer.h"
54 #include "memattr.h"
55 #include "ada-lang.h"
56 #include "top.h"
57 #include "valprint.h"
58 #include "jit.h"
59 #include "parser-defs.h"
60 #include "gdb_regex.h"
61 #include "probe.h"
62 #include "cli/cli-utils.h"
63 #include "continuations.h"
64 #include "stack.h"
65 #include "skip.h"
66 #include "ax-gdb.h"
67 #include "dummy-frame.h"
68 #include "interps.h"
69 #include "format.h"
70 #include "location.h"
71 #include "thread-fsm.h"
72 #include "tid-parse.h"
73
74 /* readline include files */
75 #include "readline/readline.h"
76 #include "readline/history.h"
77
78 /* readline defines this.  */
79 #undef savestring
80
81 #include "mi/mi-common.h"
82 #include "extension.h"
83 #include <algorithm>
84
85 /* Enums for exception-handling support.  */
86 enum exception_event_kind
87 {
88   EX_EVENT_THROW,
89   EX_EVENT_RETHROW,
90   EX_EVENT_CATCH
91 };
92
93 /* Prototypes for local functions.  */
94
95 static void enable_delete_command (char *, int);
96
97 static void enable_once_command (char *, int);
98
99 static void enable_count_command (char *, int);
100
101 static void disable_command (char *, int);
102
103 static void enable_command (char *, int);
104
105 static void map_breakpoint_numbers (char *, void (*) (struct breakpoint *,
106                                                       void *),
107                                     void *);
108
109 static void ignore_command (char *, int);
110
111 static int breakpoint_re_set_one (void *);
112
113 static void breakpoint_re_set_default (struct breakpoint *);
114
115 static void
116   create_sals_from_location_default (const struct event_location *location,
117                                      struct linespec_result *canonical,
118                                      enum bptype type_wanted);
119
120 static void create_breakpoints_sal_default (struct gdbarch *,
121                                             struct linespec_result *,
122                                             char *, char *, enum bptype,
123                                             enum bpdisp, int, int,
124                                             int,
125                                             const struct breakpoint_ops *,
126                                             int, int, int, unsigned);
127
128 static void decode_location_default (struct breakpoint *b,
129                                      const struct event_location *location,
130                                      struct program_space *search_pspace,
131                                      struct symtabs_and_lines *sals);
132
133 static void clear_command (char *, int);
134
135 static void catch_command (char *, int);
136
137 static int can_use_hardware_watchpoint (struct value *);
138
139 static void break_command_1 (char *, int, int);
140
141 static void mention (struct breakpoint *);
142
143 static struct breakpoint *set_raw_breakpoint_without_location (struct gdbarch *,
144                                                                enum bptype,
145                                                                const struct breakpoint_ops *);
146 static struct bp_location *add_location_to_breakpoint (struct breakpoint *,
147                                                        const struct symtab_and_line *);
148
149 /* This function is used in gdbtk sources and thus can not be made
150    static.  */
151 struct breakpoint *set_raw_breakpoint (struct gdbarch *gdbarch,
152                                        struct symtab_and_line,
153                                        enum bptype,
154                                        const struct breakpoint_ops *);
155
156 static struct breakpoint *
157   momentary_breakpoint_from_master (struct breakpoint *orig,
158                                     enum bptype type,
159                                     const struct breakpoint_ops *ops,
160                                     int loc_enabled);
161
162 static void breakpoint_adjustment_warning (CORE_ADDR, CORE_ADDR, int, int);
163
164 static CORE_ADDR adjust_breakpoint_address (struct gdbarch *gdbarch,
165                                             CORE_ADDR bpaddr,
166                                             enum bptype bptype);
167
168 static void describe_other_breakpoints (struct gdbarch *,
169                                         struct program_space *, CORE_ADDR,
170                                         struct obj_section *, int);
171
172 static int watchpoint_locations_match (struct bp_location *loc1,
173                                        struct bp_location *loc2);
174
175 static int breakpoint_location_address_match (struct bp_location *bl,
176                                               struct address_space *aspace,
177                                               CORE_ADDR addr);
178
179 static int breakpoint_location_address_range_overlap (struct bp_location *,
180                                                       struct address_space *,
181                                                       CORE_ADDR, int);
182
183 static void breakpoints_info (char *, int);
184
185 static void watchpoints_info (char *, int);
186
187 static int breakpoint_1 (char *, int, 
188                          int (*) (const struct breakpoint *));
189
190 static int breakpoint_cond_eval (void *);
191
192 static void cleanup_executing_breakpoints (void *);
193
194 static void commands_command (char *, int);
195
196 static void condition_command (char *, int);
197
198 static int remove_breakpoint (struct bp_location *);
199 static int remove_breakpoint_1 (struct bp_location *, enum remove_bp_reason);
200
201 static enum print_stop_action print_bp_stop_message (bpstat bs);
202
203 static int watchpoint_check (void *);
204
205 static void maintenance_info_breakpoints (char *, int);
206
207 static int hw_breakpoint_used_count (void);
208
209 static int hw_watchpoint_use_count (struct breakpoint *);
210
211 static int hw_watchpoint_used_count_others (struct breakpoint *except,
212                                             enum bptype type,
213                                             int *other_type_used);
214
215 static void hbreak_command (char *, int);
216
217 static void thbreak_command (char *, int);
218
219 static void enable_breakpoint_disp (struct breakpoint *, enum bpdisp,
220                                     int count);
221
222 static void stop_command (char *arg, int from_tty);
223
224 static void stopin_command (char *arg, int from_tty);
225
226 static void stopat_command (char *arg, int from_tty);
227
228 static void tcatch_command (char *arg, int from_tty);
229
230 static void free_bp_location (struct bp_location *loc);
231 static void incref_bp_location (struct bp_location *loc);
232 static void decref_bp_location (struct bp_location **loc);
233
234 static struct bp_location *allocate_bp_location (struct breakpoint *bpt);
235
236 /* update_global_location_list's modes of operation wrt to whether to
237    insert locations now.  */
238 enum ugll_insert_mode
239 {
240   /* Don't insert any breakpoint locations into the inferior, only
241      remove already-inserted locations that no longer should be
242      inserted.  Functions that delete a breakpoint or breakpoints
243      should specify this mode, so that deleting a breakpoint doesn't
244      have the side effect of inserting the locations of other
245      breakpoints that are marked not-inserted, but should_be_inserted
246      returns true on them.
247
248      This behavior is useful is situations close to tear-down -- e.g.,
249      after an exec, while the target still has execution, but
250      breakpoint shadows of the previous executable image should *NOT*
251      be restored to the new image; or before detaching, where the
252      target still has execution and wants to delete breakpoints from
253      GDB's lists, and all breakpoints had already been removed from
254      the inferior.  */
255   UGLL_DONT_INSERT,
256
257   /* May insert breakpoints iff breakpoints_should_be_inserted_now
258      claims breakpoints should be inserted now.  */
259   UGLL_MAY_INSERT,
260
261   /* Insert locations now, irrespective of
262      breakpoints_should_be_inserted_now.  E.g., say all threads are
263      stopped right now, and the user did "continue".  We need to
264      insert breakpoints _before_ resuming the target, but
265      UGLL_MAY_INSERT wouldn't insert them, because
266      breakpoints_should_be_inserted_now returns false at that point,
267      as no thread is running yet.  */
268   UGLL_INSERT
269 };
270
271 static void update_global_location_list (enum ugll_insert_mode);
272
273 static void update_global_location_list_nothrow (enum ugll_insert_mode);
274
275 static int is_hardware_watchpoint (const struct breakpoint *bpt);
276
277 static void insert_breakpoint_locations (void);
278
279 static void tracepoints_info (char *, int);
280
281 static void delete_trace_command (char *, int);
282
283 static void enable_trace_command (char *, int);
284
285 static void disable_trace_command (char *, int);
286
287 static void trace_pass_command (char *, int);
288
289 static void set_tracepoint_count (int num);
290
291 static int is_masked_watchpoint (const struct breakpoint *b);
292
293 static struct bp_location **get_first_locp_gte_addr (CORE_ADDR address);
294
295 /* Return 1 if B refers to a static tracepoint set by marker ("-m"), zero
296    otherwise.  */
297
298 static int strace_marker_p (struct breakpoint *b);
299
300 /* The breakpoint_ops structure to be inherited by all breakpoint_ops
301    that are implemented on top of software or hardware breakpoints
302    (user breakpoints, internal and momentary breakpoints, etc.).  */
303 static struct breakpoint_ops bkpt_base_breakpoint_ops;
304
305 /* Internal breakpoints class type.  */
306 static struct breakpoint_ops internal_breakpoint_ops;
307
308 /* Momentary breakpoints class type.  */
309 static struct breakpoint_ops momentary_breakpoint_ops;
310
311 /* Momentary breakpoints for bp_longjmp and bp_exception class type.  */
312 static struct breakpoint_ops longjmp_breakpoint_ops;
313
314 /* The breakpoint_ops structure to be used in regular user created
315    breakpoints.  */
316 struct breakpoint_ops bkpt_breakpoint_ops;
317
318 /* Breakpoints set on probes.  */
319 static struct breakpoint_ops bkpt_probe_breakpoint_ops;
320
321 /* Dynamic printf class type.  */
322 struct breakpoint_ops dprintf_breakpoint_ops;
323
324 /* The style in which to perform a dynamic printf.  This is a user
325    option because different output options have different tradeoffs;
326    if GDB does the printing, there is better error handling if there
327    is a problem with any of the arguments, but using an inferior
328    function lets you have special-purpose printers and sending of
329    output to the same place as compiled-in print functions.  */
330
331 static const char dprintf_style_gdb[] = "gdb";
332 static const char dprintf_style_call[] = "call";
333 static const char dprintf_style_agent[] = "agent";
334 static const char *const dprintf_style_enums[] = {
335   dprintf_style_gdb,
336   dprintf_style_call,
337   dprintf_style_agent,
338   NULL
339 };
340 static const char *dprintf_style = dprintf_style_gdb;
341
342 /* The function to use for dynamic printf if the preferred style is to
343    call into the inferior.  The value is simply a string that is
344    copied into the command, so it can be anything that GDB can
345    evaluate to a callable address, not necessarily a function name.  */
346
347 static char *dprintf_function = "";
348
349 /* The channel to use for dynamic printf if the preferred style is to
350    call into the inferior; if a nonempty string, it will be passed to
351    the call as the first argument, with the format string as the
352    second.  As with the dprintf function, this can be anything that
353    GDB knows how to evaluate, so in addition to common choices like
354    "stderr", this could be an app-specific expression like
355    "mystreams[curlogger]".  */
356
357 static char *dprintf_channel = "";
358
359 /* True if dprintf commands should continue to operate even if GDB
360    has disconnected.  */
361 static int disconnected_dprintf = 1;
362
363 /* A reference-counted struct command_line.  This lets multiple
364    breakpoints share a single command list.  */
365 struct counted_command_line
366 {
367   /* The reference count.  */
368   int refc;
369
370   /* The command list.  */
371   struct command_line *commands;
372 };
373
374 struct command_line *
375 breakpoint_commands (struct breakpoint *b)
376 {
377   return b->commands ? b->commands->commands : NULL;
378 }
379
380 /* Flag indicating that a command has proceeded the inferior past the
381    current breakpoint.  */
382
383 static int breakpoint_proceeded;
384
385 const char *
386 bpdisp_text (enum bpdisp disp)
387 {
388   /* NOTE: the following values are a part of MI protocol and
389      represent values of 'disp' field returned when inferior stops at
390      a breakpoint.  */
391   static const char * const bpdisps[] = {"del", "dstp", "dis", "keep"};
392
393   return bpdisps[(int) disp];
394 }
395
396 /* Prototypes for exported functions.  */
397 /* If FALSE, gdb will not use hardware support for watchpoints, even
398    if such is available.  */
399 static int can_use_hw_watchpoints;
400
401 static void
402 show_can_use_hw_watchpoints (struct ui_file *file, int from_tty,
403                              struct cmd_list_element *c,
404                              const char *value)
405 {
406   fprintf_filtered (file,
407                     _("Debugger's willingness to use "
408                       "watchpoint hardware is %s.\n"),
409                     value);
410 }
411
412 /* If AUTO_BOOLEAN_FALSE, gdb will not attempt to create pending breakpoints.
413    If AUTO_BOOLEAN_TRUE, gdb will automatically create pending breakpoints
414    for unrecognized breakpoint locations.
415    If AUTO_BOOLEAN_AUTO, gdb will query when breakpoints are unrecognized.  */
416 static enum auto_boolean pending_break_support;
417 static void
418 show_pending_break_support (struct ui_file *file, int from_tty,
419                             struct cmd_list_element *c,
420                             const char *value)
421 {
422   fprintf_filtered (file,
423                     _("Debugger's behavior regarding "
424                       "pending breakpoints is %s.\n"),
425                     value);
426 }
427
428 /* If 1, gdb will automatically use hardware breakpoints for breakpoints
429    set with "break" but falling in read-only memory.
430    If 0, gdb will warn about such breakpoints, but won't automatically
431    use hardware breakpoints.  */
432 static int automatic_hardware_breakpoints;
433 static void
434 show_automatic_hardware_breakpoints (struct ui_file *file, int from_tty,
435                                      struct cmd_list_element *c,
436                                      const char *value)
437 {
438   fprintf_filtered (file,
439                     _("Automatic usage of hardware breakpoints is %s.\n"),
440                     value);
441 }
442
443 /* If on, GDB keeps breakpoints inserted even if the inferior is
444    stopped, and immediately inserts any new breakpoints as soon as
445    they're created.  If off (default), GDB keeps breakpoints off of
446    the target as long as possible.  That is, it delays inserting
447    breakpoints until the next resume, and removes them again when the
448    target fully stops.  This is a bit safer in case GDB crashes while
449    processing user input.  */
450 static int always_inserted_mode = 0;
451
452 static void
453 show_always_inserted_mode (struct ui_file *file, int from_tty,
454                      struct cmd_list_element *c, const char *value)
455 {
456   fprintf_filtered (file, _("Always inserted breakpoint mode is %s.\n"),
457                     value);
458 }
459
460 /* See breakpoint.h.  */
461
462 int
463 breakpoints_should_be_inserted_now (void)
464 {
465   if (gdbarch_has_global_breakpoints (target_gdbarch ()))
466     {
467       /* If breakpoints are global, they should be inserted even if no
468          thread under gdb's control is running, or even if there are
469          no threads under GDB's control yet.  */
470       return 1;
471     }
472   else if (target_has_execution)
473     {
474       struct thread_info *tp;
475
476       if (always_inserted_mode)
477         {
478           /* The user wants breakpoints inserted even if all threads
479              are stopped.  */
480           return 1;
481         }
482
483       if (threads_are_executing ())
484         return 1;
485
486       /* Don't remove breakpoints yet if, even though all threads are
487          stopped, we still have events to process.  */
488       ALL_NON_EXITED_THREADS (tp)
489         if (tp->resumed
490             && tp->suspend.waitstatus_pending_p)
491           return 1;
492     }
493   return 0;
494 }
495
496 static const char condition_evaluation_both[] = "host or target";
497
498 /* Modes for breakpoint condition evaluation.  */
499 static const char condition_evaluation_auto[] = "auto";
500 static const char condition_evaluation_host[] = "host";
501 static const char condition_evaluation_target[] = "target";
502 static const char *const condition_evaluation_enums[] = {
503   condition_evaluation_auto,
504   condition_evaluation_host,
505   condition_evaluation_target,
506   NULL
507 };
508
509 /* Global that holds the current mode for breakpoint condition evaluation.  */
510 static const char *condition_evaluation_mode_1 = condition_evaluation_auto;
511
512 /* Global that we use to display information to the user (gets its value from
513    condition_evaluation_mode_1.  */
514 static const char *condition_evaluation_mode = condition_evaluation_auto;
515
516 /* Translate a condition evaluation mode MODE into either "host"
517    or "target".  This is used mostly to translate from "auto" to the
518    real setting that is being used.  It returns the translated
519    evaluation mode.  */
520
521 static const char *
522 translate_condition_evaluation_mode (const char *mode)
523 {
524   if (mode == condition_evaluation_auto)
525     {
526       if (target_supports_evaluation_of_breakpoint_conditions ())
527         return condition_evaluation_target;
528       else
529         return condition_evaluation_host;
530     }
531   else
532     return mode;
533 }
534
535 /* Discovers what condition_evaluation_auto translates to.  */
536
537 static const char *
538 breakpoint_condition_evaluation_mode (void)
539 {
540   return translate_condition_evaluation_mode (condition_evaluation_mode);
541 }
542
543 /* Return true if GDB should evaluate breakpoint conditions or false
544    otherwise.  */
545
546 static int
547 gdb_evaluates_breakpoint_condition_p (void)
548 {
549   const char *mode = breakpoint_condition_evaluation_mode ();
550
551   return (mode == condition_evaluation_host);
552 }
553
554 void _initialize_breakpoint (void);
555
556 /* Are we executing breakpoint commands?  */
557 static int executing_breakpoint_commands;
558
559 /* Are overlay event breakpoints enabled? */
560 static int overlay_events_enabled;
561
562 /* See description in breakpoint.h. */
563 int target_exact_watchpoints = 0;
564
565 /* Walk the following statement or block through all breakpoints.
566    ALL_BREAKPOINTS_SAFE does so even if the statement deletes the
567    current breakpoint.  */
568
569 #define ALL_BREAKPOINTS(B)  for (B = breakpoint_chain; B; B = B->next)
570
571 #define ALL_BREAKPOINTS_SAFE(B,TMP)     \
572         for (B = breakpoint_chain;      \
573              B ? (TMP=B->next, 1): 0;   \
574              B = TMP)
575
576 /* Similar iterator for the low-level breakpoints.  SAFE variant is
577    not provided so update_global_location_list must not be called
578    while executing the block of ALL_BP_LOCATIONS.  */
579
580 #define ALL_BP_LOCATIONS(B,BP_TMP)                                      \
581         for (BP_TMP = bp_location;                                      \
582              BP_TMP < bp_location + bp_location_count && (B = *BP_TMP); \
583              BP_TMP++)
584
585 /* Iterates through locations with address ADDRESS for the currently selected
586    program space.  BP_LOCP_TMP points to each object.  BP_LOCP_START points
587    to where the loop should start from.
588    If BP_LOCP_START is a NULL pointer, the macro automatically seeks the
589    appropriate location to start with.  */
590
591 #define ALL_BP_LOCATIONS_AT_ADDR(BP_LOCP_TMP, BP_LOCP_START, ADDRESS)   \
592         for (BP_LOCP_START = BP_LOCP_START == NULL ? get_first_locp_gte_addr (ADDRESS) : BP_LOCP_START, \
593              BP_LOCP_TMP = BP_LOCP_START;                               \
594              BP_LOCP_START                                              \
595              && (BP_LOCP_TMP < bp_location + bp_location_count          \
596              && (*BP_LOCP_TMP)->address == ADDRESS);                    \
597              BP_LOCP_TMP++)
598
599 /* Iterator for tracepoints only.  */
600
601 #define ALL_TRACEPOINTS(B)  \
602   for (B = breakpoint_chain; B; B = B->next)  \
603     if (is_tracepoint (B))
604
605 /* Chains of all breakpoints defined.  */
606
607 struct breakpoint *breakpoint_chain;
608
609 /* Array is sorted by bp_location_compare - primarily by the ADDRESS.  */
610
611 static struct bp_location **bp_location;
612
613 /* Number of elements of BP_LOCATION.  */
614
615 static unsigned bp_location_count;
616
617 /* Maximum alignment offset between bp_target_info.PLACED_ADDRESS and
618    ADDRESS for the current elements of BP_LOCATION which get a valid
619    result from bp_location_has_shadow.  You can use it for roughly
620    limiting the subrange of BP_LOCATION to scan for shadow bytes for
621    an address you need to read.  */
622
623 static CORE_ADDR bp_location_placed_address_before_address_max;
624
625 /* Maximum offset plus alignment between bp_target_info.PLACED_ADDRESS
626    + bp_target_info.SHADOW_LEN and ADDRESS for the current elements of
627    BP_LOCATION which get a valid result from bp_location_has_shadow.
628    You can use it for roughly limiting the subrange of BP_LOCATION to
629    scan for shadow bytes for an address you need to read.  */
630
631 static CORE_ADDR bp_location_shadow_len_after_address_max;
632
633 /* The locations that no longer correspond to any breakpoint, unlinked
634    from bp_location array, but for which a hit may still be reported
635    by a target.  */
636 VEC(bp_location_p) *moribund_locations = NULL;
637
638 /* Number of last breakpoint made.  */
639
640 static int breakpoint_count;
641
642 /* The value of `breakpoint_count' before the last command that
643    created breakpoints.  If the last (break-like) command created more
644    than one breakpoint, then the difference between BREAKPOINT_COUNT
645    and PREV_BREAKPOINT_COUNT is more than one.  */
646 static int prev_breakpoint_count;
647
648 /* Number of last tracepoint made.  */
649
650 static int tracepoint_count;
651
652 static struct cmd_list_element *breakpoint_set_cmdlist;
653 static struct cmd_list_element *breakpoint_show_cmdlist;
654 struct cmd_list_element *save_cmdlist;
655
656 /* See declaration at breakpoint.h.  */
657
658 struct breakpoint *
659 breakpoint_find_if (int (*func) (struct breakpoint *b, void *d),
660                     void *user_data)
661 {
662   struct breakpoint *b = NULL;
663
664   ALL_BREAKPOINTS (b)
665     {
666       if (func (b, user_data) != 0)
667         break;
668     }
669
670   return b;
671 }
672
673 /* Return whether a breakpoint is an active enabled breakpoint.  */
674 static int
675 breakpoint_enabled (struct breakpoint *b)
676 {
677   return (b->enable_state == bp_enabled);
678 }
679
680 /* Set breakpoint count to NUM.  */
681
682 static void
683 set_breakpoint_count (int num)
684 {
685   prev_breakpoint_count = breakpoint_count;
686   breakpoint_count = num;
687   set_internalvar_integer (lookup_internalvar ("bpnum"), num);
688 }
689
690 /* Used by `start_rbreak_breakpoints' below, to record the current
691    breakpoint count before "rbreak" creates any breakpoint.  */
692 static int rbreak_start_breakpoint_count;
693
694 /* Called at the start an "rbreak" command to record the first
695    breakpoint made.  */
696
697 void
698 start_rbreak_breakpoints (void)
699 {
700   rbreak_start_breakpoint_count = breakpoint_count;
701 }
702
703 /* Called at the end of an "rbreak" command to record the last
704    breakpoint made.  */
705
706 void
707 end_rbreak_breakpoints (void)
708 {
709   prev_breakpoint_count = rbreak_start_breakpoint_count;
710 }
711
712 /* Used in run_command to zero the hit count when a new run starts.  */
713
714 void
715 clear_breakpoint_hit_counts (void)
716 {
717   struct breakpoint *b;
718
719   ALL_BREAKPOINTS (b)
720     b->hit_count = 0;
721 }
722
723 /* Allocate a new counted_command_line with reference count of 1.
724    The new structure owns COMMANDS.  */
725
726 static struct counted_command_line *
727 alloc_counted_command_line (struct command_line *commands)
728 {
729   struct counted_command_line *result = XNEW (struct counted_command_line);
730
731   result->refc = 1;
732   result->commands = commands;
733
734   return result;
735 }
736
737 /* Increment reference count.  This does nothing if CMD is NULL.  */
738
739 static void
740 incref_counted_command_line (struct counted_command_line *cmd)
741 {
742   if (cmd)
743     ++cmd->refc;
744 }
745
746 /* Decrement reference count.  If the reference count reaches 0,
747    destroy the counted_command_line.  Sets *CMDP to NULL.  This does
748    nothing if *CMDP is NULL.  */
749
750 static void
751 decref_counted_command_line (struct counted_command_line **cmdp)
752 {
753   if (*cmdp)
754     {
755       if (--(*cmdp)->refc == 0)
756         {
757           free_command_lines (&(*cmdp)->commands);
758           xfree (*cmdp);
759         }
760       *cmdp = NULL;
761     }
762 }
763
764 /* A cleanup function that calls decref_counted_command_line.  */
765
766 static void
767 do_cleanup_counted_command_line (void *arg)
768 {
769   decref_counted_command_line ((struct counted_command_line **) arg);
770 }
771
772 /* Create a cleanup that calls decref_counted_command_line on the
773    argument.  */
774
775 static struct cleanup *
776 make_cleanup_decref_counted_command_line (struct counted_command_line **cmdp)
777 {
778   return make_cleanup (do_cleanup_counted_command_line, cmdp);
779 }
780
781 \f
782 /* Return the breakpoint with the specified number, or NULL
783    if the number does not refer to an existing breakpoint.  */
784
785 struct breakpoint *
786 get_breakpoint (int num)
787 {
788   struct breakpoint *b;
789
790   ALL_BREAKPOINTS (b)
791     if (b->number == num)
792       return b;
793   
794   return NULL;
795 }
796
797 \f
798
799 /* Mark locations as "conditions have changed" in case the target supports
800    evaluating conditions on its side.  */
801
802 static void
803 mark_breakpoint_modified (struct breakpoint *b)
804 {
805   struct bp_location *loc;
806
807   /* This is only meaningful if the target is
808      evaluating conditions and if the user has
809      opted for condition evaluation on the target's
810      side.  */
811   if (gdb_evaluates_breakpoint_condition_p ()
812       || !target_supports_evaluation_of_breakpoint_conditions ())
813     return;
814
815   if (!is_breakpoint (b))
816     return;
817
818   for (loc = b->loc; loc; loc = loc->next)
819     loc->condition_changed = condition_modified;
820 }
821
822 /* Mark location as "conditions have changed" in case the target supports
823    evaluating conditions on its side.  */
824
825 static void
826 mark_breakpoint_location_modified (struct bp_location *loc)
827 {
828   /* This is only meaningful if the target is
829      evaluating conditions and if the user has
830      opted for condition evaluation on the target's
831      side.  */
832   if (gdb_evaluates_breakpoint_condition_p ()
833       || !target_supports_evaluation_of_breakpoint_conditions ())
834
835     return;
836
837   if (!is_breakpoint (loc->owner))
838     return;
839
840   loc->condition_changed = condition_modified;
841 }
842
843 /* Sets the condition-evaluation mode using the static global
844    condition_evaluation_mode.  */
845
846 static void
847 set_condition_evaluation_mode (char *args, int from_tty,
848                                struct cmd_list_element *c)
849 {
850   const char *old_mode, *new_mode;
851
852   if ((condition_evaluation_mode_1 == condition_evaluation_target)
853       && !target_supports_evaluation_of_breakpoint_conditions ())
854     {
855       condition_evaluation_mode_1 = condition_evaluation_mode;
856       warning (_("Target does not support breakpoint condition evaluation.\n"
857                  "Using host evaluation mode instead."));
858       return;
859     }
860
861   new_mode = translate_condition_evaluation_mode (condition_evaluation_mode_1);
862   old_mode = translate_condition_evaluation_mode (condition_evaluation_mode);
863
864   /* Flip the switch.  Flip it even if OLD_MODE == NEW_MODE as one of the
865      settings was "auto".  */
866   condition_evaluation_mode = condition_evaluation_mode_1;
867
868   /* Only update the mode if the user picked a different one.  */
869   if (new_mode != old_mode)
870     {
871       struct bp_location *loc, **loc_tmp;
872       /* If the user switched to a different evaluation mode, we
873          need to synch the changes with the target as follows:
874
875          "host" -> "target": Send all (valid) conditions to the target.
876          "target" -> "host": Remove all the conditions from the target.
877       */
878
879       if (new_mode == condition_evaluation_target)
880         {
881           /* Mark everything modified and synch conditions with the
882              target.  */
883           ALL_BP_LOCATIONS (loc, loc_tmp)
884             mark_breakpoint_location_modified (loc);
885         }
886       else
887         {
888           /* Manually mark non-duplicate locations to synch conditions
889              with the target.  We do this to remove all the conditions the
890              target knows about.  */
891           ALL_BP_LOCATIONS (loc, loc_tmp)
892             if (is_breakpoint (loc->owner) && loc->inserted)
893               loc->needs_update = 1;
894         }
895
896       /* Do the update.  */
897       update_global_location_list (UGLL_MAY_INSERT);
898     }
899
900   return;
901 }
902
903 /* Shows the current mode of breakpoint condition evaluation.  Explicitly shows
904    what "auto" is translating to.  */
905
906 static void
907 show_condition_evaluation_mode (struct ui_file *file, int from_tty,
908                                 struct cmd_list_element *c, const char *value)
909 {
910   if (condition_evaluation_mode == condition_evaluation_auto)
911     fprintf_filtered (file,
912                       _("Breakpoint condition evaluation "
913                         "mode is %s (currently %s).\n"),
914                       value,
915                       breakpoint_condition_evaluation_mode ());
916   else
917     fprintf_filtered (file, _("Breakpoint condition evaluation mode is %s.\n"),
918                       value);
919 }
920
921 /* A comparison function for bp_location AP and BP that is used by
922    bsearch.  This comparison function only cares about addresses, unlike
923    the more general bp_location_compare function.  */
924
925 static int
926 bp_location_compare_addrs (const void *ap, const void *bp)
927 {
928   const struct bp_location *a = *(const struct bp_location **) ap;
929   const struct bp_location *b = *(const struct bp_location **) bp;
930
931   if (a->address == b->address)
932     return 0;
933   else
934     return ((a->address > b->address) - (a->address < b->address));
935 }
936
937 /* Helper function to skip all bp_locations with addresses
938    less than ADDRESS.  It returns the first bp_location that
939    is greater than or equal to ADDRESS.  If none is found, just
940    return NULL.  */
941
942 static struct bp_location **
943 get_first_locp_gte_addr (CORE_ADDR address)
944 {
945   struct bp_location dummy_loc;
946   struct bp_location *dummy_locp = &dummy_loc;
947   struct bp_location **locp_found = NULL;
948
949   /* Initialize the dummy location's address field.  */
950   memset (&dummy_loc, 0, sizeof (struct bp_location));
951   dummy_loc.address = address;
952
953   /* Find a close match to the first location at ADDRESS.  */
954   locp_found = ((struct bp_location **)
955                 bsearch (&dummy_locp, bp_location, bp_location_count,
956                          sizeof (struct bp_location **),
957                          bp_location_compare_addrs));
958
959   /* Nothing was found, nothing left to do.  */
960   if (locp_found == NULL)
961     return NULL;
962
963   /* We may have found a location that is at ADDRESS but is not the first in the
964      location's list.  Go backwards (if possible) and locate the first one.  */
965   while ((locp_found - 1) >= bp_location
966          && (*(locp_found - 1))->address == address)
967     locp_found--;
968
969   return locp_found;
970 }
971
972 void
973 set_breakpoint_condition (struct breakpoint *b, const char *exp,
974                           int from_tty)
975 {
976   xfree (b->cond_string);
977   b->cond_string = NULL;
978
979   if (is_watchpoint (b))
980     {
981       struct watchpoint *w = (struct watchpoint *) b;
982
983       xfree (w->cond_exp);
984       w->cond_exp = NULL;
985     }
986   else
987     {
988       struct bp_location *loc;
989
990       for (loc = b->loc; loc; loc = loc->next)
991         {
992           xfree (loc->cond);
993           loc->cond = NULL;
994
995           /* No need to free the condition agent expression
996              bytecode (if we have one).  We will handle this
997              when we go through update_global_location_list.  */
998         }
999     }
1000
1001   if (*exp == 0)
1002     {
1003       if (from_tty)
1004         printf_filtered (_("Breakpoint %d now unconditional.\n"), b->number);
1005     }
1006   else
1007     {
1008       const char *arg = exp;
1009
1010       /* I don't know if it matters whether this is the string the user
1011          typed in or the decompiled expression.  */
1012       b->cond_string = xstrdup (arg);
1013       b->condition_not_parsed = 0;
1014
1015       if (is_watchpoint (b))
1016         {
1017           struct watchpoint *w = (struct watchpoint *) b;
1018
1019           innermost_block = NULL;
1020           arg = exp;
1021           w->cond_exp = parse_exp_1 (&arg, 0, 0, 0);
1022           if (*arg)
1023             error (_("Junk at end of expression"));
1024           w->cond_exp_valid_block = innermost_block;
1025         }
1026       else
1027         {
1028           struct bp_location *loc;
1029
1030           for (loc = b->loc; loc; loc = loc->next)
1031             {
1032               arg = exp;
1033               loc->cond =
1034                 parse_exp_1 (&arg, loc->address,
1035                              block_for_pc (loc->address), 0);
1036               if (*arg)
1037                 error (_("Junk at end of expression"));
1038             }
1039         }
1040     }
1041   mark_breakpoint_modified (b);
1042
1043   observer_notify_breakpoint_modified (b);
1044 }
1045
1046 /* Completion for the "condition" command.  */
1047
1048 static VEC (char_ptr) *
1049 condition_completer (struct cmd_list_element *cmd,
1050                      const char *text, const char *word)
1051 {
1052   const char *space;
1053
1054   text = skip_spaces_const (text);
1055   space = skip_to_space_const (text);
1056   if (*space == '\0')
1057     {
1058       int len;
1059       struct breakpoint *b;
1060       VEC (char_ptr) *result = NULL;
1061
1062       if (text[0] == '$')
1063         {
1064           /* We don't support completion of history indices.  */
1065           if (isdigit (text[1]))
1066             return NULL;
1067           return complete_internalvar (&text[1]);
1068         }
1069
1070       /* We're completing the breakpoint number.  */
1071       len = strlen (text);
1072
1073       ALL_BREAKPOINTS (b)
1074         {
1075           char number[50];
1076
1077           xsnprintf (number, sizeof (number), "%d", b->number);
1078
1079           if (strncmp (number, text, len) == 0)
1080             VEC_safe_push (char_ptr, result, xstrdup (number));
1081         }
1082
1083       return result;
1084     }
1085
1086   /* We're completing the expression part.  */
1087   text = skip_spaces_const (space);
1088   return expression_completer (cmd, text, word);
1089 }
1090
1091 /* condition N EXP -- set break condition of breakpoint N to EXP.  */
1092
1093 static void
1094 condition_command (char *arg, int from_tty)
1095 {
1096   struct breakpoint *b;
1097   char *p;
1098   int bnum;
1099
1100   if (arg == 0)
1101     error_no_arg (_("breakpoint number"));
1102
1103   p = arg;
1104   bnum = get_number (&p);
1105   if (bnum == 0)
1106     error (_("Bad breakpoint argument: '%s'"), arg);
1107
1108   ALL_BREAKPOINTS (b)
1109     if (b->number == bnum)
1110       {
1111         /* Check if this breakpoint has a "stop" method implemented in an
1112            extension language.  This method and conditions entered into GDB
1113            from the CLI are mutually exclusive.  */
1114         const struct extension_language_defn *extlang
1115           = get_breakpoint_cond_ext_lang (b, EXT_LANG_NONE);
1116
1117         if (extlang != NULL)
1118           {
1119             error (_("Only one stop condition allowed.  There is currently"
1120                      " a %s stop condition defined for this breakpoint."),
1121                    ext_lang_capitalized_name (extlang));
1122           }
1123         set_breakpoint_condition (b, p, from_tty);
1124
1125         if (is_breakpoint (b))
1126           update_global_location_list (UGLL_MAY_INSERT);
1127
1128         return;
1129       }
1130
1131   error (_("No breakpoint number %d."), bnum);
1132 }
1133
1134 /* Check that COMMAND do not contain commands that are suitable
1135    only for tracepoints and not suitable for ordinary breakpoints.
1136    Throw if any such commands is found.  */
1137
1138 static void
1139 check_no_tracepoint_commands (struct command_line *commands)
1140 {
1141   struct command_line *c;
1142
1143   for (c = commands; c; c = c->next)
1144     {
1145       int i;
1146
1147       if (c->control_type == while_stepping_control)
1148         error (_("The 'while-stepping' command can "
1149                  "only be used for tracepoints"));
1150
1151       for (i = 0; i < c->body_count; ++i)
1152         check_no_tracepoint_commands ((c->body_list)[i]);
1153
1154       /* Not that command parsing removes leading whitespace and comment
1155          lines and also empty lines.  So, we only need to check for
1156          command directly.  */
1157       if (strstr (c->line, "collect ") == c->line)
1158         error (_("The 'collect' command can only be used for tracepoints"));
1159
1160       if (strstr (c->line, "teval ") == c->line)
1161         error (_("The 'teval' command can only be used for tracepoints"));
1162     }
1163 }
1164
1165 /* Encapsulate tests for different types of tracepoints.  */
1166
1167 static int
1168 is_tracepoint_type (enum bptype type)
1169 {
1170   return (type == bp_tracepoint
1171           || type == bp_fast_tracepoint
1172           || type == bp_static_tracepoint);
1173 }
1174
1175 int
1176 is_tracepoint (const struct breakpoint *b)
1177 {
1178   return is_tracepoint_type (b->type);
1179 }
1180
1181 /* A helper function that validates that COMMANDS are valid for a
1182    breakpoint.  This function will throw an exception if a problem is
1183    found.  */
1184
1185 static void
1186 validate_commands_for_breakpoint (struct breakpoint *b,
1187                                   struct command_line *commands)
1188 {
1189   if (is_tracepoint (b))
1190     {
1191       struct tracepoint *t = (struct tracepoint *) b;
1192       struct command_line *c;
1193       struct command_line *while_stepping = 0;
1194
1195       /* Reset the while-stepping step count.  The previous commands
1196          might have included a while-stepping action, while the new
1197          ones might not.  */
1198       t->step_count = 0;
1199
1200       /* We need to verify that each top-level element of commands is
1201          valid for tracepoints, that there's at most one
1202          while-stepping element, and that the while-stepping's body
1203          has valid tracing commands excluding nested while-stepping.
1204          We also need to validate the tracepoint action line in the
1205          context of the tracepoint --- validate_actionline actually
1206          has side effects, like setting the tracepoint's
1207          while-stepping STEP_COUNT, in addition to checking if the
1208          collect/teval actions parse and make sense in the
1209          tracepoint's context.  */
1210       for (c = commands; c; c = c->next)
1211         {
1212           if (c->control_type == while_stepping_control)
1213             {
1214               if (b->type == bp_fast_tracepoint)
1215                 error (_("The 'while-stepping' command "
1216                          "cannot be used for fast tracepoint"));
1217               else if (b->type == bp_static_tracepoint)
1218                 error (_("The 'while-stepping' command "
1219                          "cannot be used for static tracepoint"));
1220
1221               if (while_stepping)
1222                 error (_("The 'while-stepping' command "
1223                          "can be used only once"));
1224               else
1225                 while_stepping = c;
1226             }
1227
1228           validate_actionline (c->line, b);
1229         }
1230       if (while_stepping)
1231         {
1232           struct command_line *c2;
1233
1234           gdb_assert (while_stepping->body_count == 1);
1235           c2 = while_stepping->body_list[0];
1236           for (; c2; c2 = c2->next)
1237             {
1238               if (c2->control_type == while_stepping_control)
1239                 error (_("The 'while-stepping' command cannot be nested"));
1240             }
1241         }
1242     }
1243   else
1244     {
1245       check_no_tracepoint_commands (commands);
1246     }
1247 }
1248
1249 /* Return a vector of all the static tracepoints set at ADDR.  The
1250    caller is responsible for releasing the vector.  */
1251
1252 VEC(breakpoint_p) *
1253 static_tracepoints_here (CORE_ADDR addr)
1254 {
1255   struct breakpoint *b;
1256   VEC(breakpoint_p) *found = 0;
1257   struct bp_location *loc;
1258
1259   ALL_BREAKPOINTS (b)
1260     if (b->type == bp_static_tracepoint)
1261       {
1262         for (loc = b->loc; loc; loc = loc->next)
1263           if (loc->address == addr)
1264             VEC_safe_push(breakpoint_p, found, b);
1265       }
1266
1267   return found;
1268 }
1269
1270 /* Set the command list of B to COMMANDS.  If breakpoint is tracepoint,
1271    validate that only allowed commands are included.  */
1272
1273 void
1274 breakpoint_set_commands (struct breakpoint *b, 
1275                          struct command_line *commands)
1276 {
1277   validate_commands_for_breakpoint (b, commands);
1278
1279   decref_counted_command_line (&b->commands);
1280   b->commands = alloc_counted_command_line (commands);
1281   observer_notify_breakpoint_modified (b);
1282 }
1283
1284 /* Set the internal `silent' flag on the breakpoint.  Note that this
1285    is not the same as the "silent" that may appear in the breakpoint's
1286    commands.  */
1287
1288 void
1289 breakpoint_set_silent (struct breakpoint *b, int silent)
1290 {
1291   int old_silent = b->silent;
1292
1293   b->silent = silent;
1294   if (old_silent != silent)
1295     observer_notify_breakpoint_modified (b);
1296 }
1297
1298 /* Set the thread for this breakpoint.  If THREAD is -1, make the
1299    breakpoint work for any thread.  */
1300
1301 void
1302 breakpoint_set_thread (struct breakpoint *b, int thread)
1303 {
1304   int old_thread = b->thread;
1305
1306   b->thread = thread;
1307   if (old_thread != thread)
1308     observer_notify_breakpoint_modified (b);
1309 }
1310
1311 /* Set the task for this breakpoint.  If TASK is 0, make the
1312    breakpoint work for any task.  */
1313
1314 void
1315 breakpoint_set_task (struct breakpoint *b, int task)
1316 {
1317   int old_task = b->task;
1318
1319   b->task = task;
1320   if (old_task != task)
1321     observer_notify_breakpoint_modified (b);
1322 }
1323
1324 void
1325 check_tracepoint_command (char *line, void *closure)
1326 {
1327   struct breakpoint *b = (struct breakpoint *) closure;
1328
1329   validate_actionline (line, b);
1330 }
1331
1332 /* A structure used to pass information through
1333    map_breakpoint_numbers.  */
1334
1335 struct commands_info
1336 {
1337   /* True if the command was typed at a tty.  */
1338   int from_tty;
1339
1340   /* The breakpoint range spec.  */
1341   char *arg;
1342
1343   /* Non-NULL if the body of the commands are being read from this
1344      already-parsed command.  */
1345   struct command_line *control;
1346
1347   /* The command lines read from the user, or NULL if they have not
1348      yet been read.  */
1349   struct counted_command_line *cmd;
1350 };
1351
1352 /* A callback for map_breakpoint_numbers that sets the commands for
1353    commands_command.  */
1354
1355 static void
1356 do_map_commands_command (struct breakpoint *b, void *data)
1357 {
1358   struct commands_info *info = (struct commands_info *) data;
1359
1360   if (info->cmd == NULL)
1361     {
1362       struct command_line *l;
1363
1364       if (info->control != NULL)
1365         l = copy_command_lines (info->control->body_list[0]);
1366       else
1367         {
1368           struct cleanup *old_chain;
1369           char *str;
1370
1371           str = xstrprintf (_("Type commands for breakpoint(s) "
1372                               "%s, one per line."),
1373                             info->arg);
1374
1375           old_chain = make_cleanup (xfree, str);
1376
1377           l = read_command_lines (str,
1378                                   info->from_tty, 1,
1379                                   (is_tracepoint (b)
1380                                    ? check_tracepoint_command : 0),
1381                                   b);
1382
1383           do_cleanups (old_chain);
1384         }
1385
1386       info->cmd = alloc_counted_command_line (l);
1387     }
1388
1389   /* If a breakpoint was on the list more than once, we don't need to
1390      do anything.  */
1391   if (b->commands != info->cmd)
1392     {
1393       validate_commands_for_breakpoint (b, info->cmd->commands);
1394       incref_counted_command_line (info->cmd);
1395       decref_counted_command_line (&b->commands);
1396       b->commands = info->cmd;
1397       observer_notify_breakpoint_modified (b);
1398     }
1399 }
1400
1401 static void
1402 commands_command_1 (char *arg, int from_tty, 
1403                     struct command_line *control)
1404 {
1405   struct cleanup *cleanups;
1406   struct commands_info info;
1407
1408   info.from_tty = from_tty;
1409   info.control = control;
1410   info.cmd = NULL;
1411   /* If we read command lines from the user, then `info' will hold an
1412      extra reference to the commands that we must clean up.  */
1413   cleanups = make_cleanup_decref_counted_command_line (&info.cmd);
1414
1415   if (arg == NULL || !*arg)
1416     {
1417       if (breakpoint_count - prev_breakpoint_count > 1)
1418         arg = xstrprintf ("%d-%d", prev_breakpoint_count + 1, 
1419                           breakpoint_count);
1420       else if (breakpoint_count > 0)
1421         arg = xstrprintf ("%d", breakpoint_count);
1422       else
1423         {
1424           /* So that we don't try to free the incoming non-NULL
1425              argument in the cleanup below.  Mapping breakpoint
1426              numbers will fail in this case.  */
1427           arg = NULL;
1428         }
1429     }
1430   else
1431     /* The command loop has some static state, so we need to preserve
1432        our argument.  */
1433     arg = xstrdup (arg);
1434
1435   if (arg != NULL)
1436     make_cleanup (xfree, arg);
1437
1438   info.arg = arg;
1439
1440   map_breakpoint_numbers (arg, do_map_commands_command, &info);
1441
1442   if (info.cmd == NULL)
1443     error (_("No breakpoints specified."));
1444
1445   do_cleanups (cleanups);
1446 }
1447
1448 static void
1449 commands_command (char *arg, int from_tty)
1450 {
1451   commands_command_1 (arg, from_tty, NULL);
1452 }
1453
1454 /* Like commands_command, but instead of reading the commands from
1455    input stream, takes them from an already parsed command structure.
1456
1457    This is used by cli-script.c to DTRT with breakpoint commands
1458    that are part of if and while bodies.  */
1459 enum command_control_type
1460 commands_from_control_command (char *arg, struct command_line *cmd)
1461 {
1462   commands_command_1 (arg, 0, cmd);
1463   return simple_control;
1464 }
1465
1466 /* Return non-zero if BL->TARGET_INFO contains valid information.  */
1467
1468 static int
1469 bp_location_has_shadow (struct bp_location *bl)
1470 {
1471   if (bl->loc_type != bp_loc_software_breakpoint)
1472     return 0;
1473   if (!bl->inserted)
1474     return 0;
1475   if (bl->target_info.shadow_len == 0)
1476     /* BL isn't valid, or doesn't shadow memory.  */
1477     return 0;
1478   return 1;
1479 }
1480
1481 /* Update BUF, which is LEN bytes read from the target address
1482    MEMADDR, by replacing a memory breakpoint with its shadowed
1483    contents.
1484
1485    If READBUF is not NULL, this buffer must not overlap with the of
1486    the breakpoint location's shadow_contents buffer.  Otherwise, a
1487    failed assertion internal error will be raised.  */
1488
1489 static void
1490 one_breakpoint_xfer_memory (gdb_byte *readbuf, gdb_byte *writebuf,
1491                             const gdb_byte *writebuf_org,
1492                             ULONGEST memaddr, LONGEST len,
1493                             struct bp_target_info *target_info,
1494                             struct gdbarch *gdbarch)
1495 {
1496   /* Now do full processing of the found relevant range of elements.  */
1497   CORE_ADDR bp_addr = 0;
1498   int bp_size = 0;
1499   int bptoffset = 0;
1500
1501   if (!breakpoint_address_match (target_info->placed_address_space, 0,
1502                                  current_program_space->aspace, 0))
1503     {
1504       /* The breakpoint is inserted in a different address space.  */
1505       return;
1506     }
1507
1508   /* Addresses and length of the part of the breakpoint that
1509      we need to copy.  */
1510   bp_addr = target_info->placed_address;
1511   bp_size = target_info->shadow_len;
1512
1513   if (bp_addr + bp_size <= memaddr)
1514     {
1515       /* The breakpoint is entirely before the chunk of memory we are
1516          reading.  */
1517       return;
1518     }
1519
1520   if (bp_addr >= memaddr + len)
1521     {
1522       /* The breakpoint is entirely after the chunk of memory we are
1523          reading.  */
1524       return;
1525     }
1526
1527   /* Offset within shadow_contents.  */
1528   if (bp_addr < memaddr)
1529     {
1530       /* Only copy the second part of the breakpoint.  */
1531       bp_size -= memaddr - bp_addr;
1532       bptoffset = memaddr - bp_addr;
1533       bp_addr = memaddr;
1534     }
1535
1536   if (bp_addr + bp_size > memaddr + len)
1537     {
1538       /* Only copy the first part of the breakpoint.  */
1539       bp_size -= (bp_addr + bp_size) - (memaddr + len);
1540     }
1541
1542   if (readbuf != NULL)
1543     {
1544       /* Verify that the readbuf buffer does not overlap with the
1545          shadow_contents buffer.  */
1546       gdb_assert (target_info->shadow_contents >= readbuf + len
1547                   || readbuf >= (target_info->shadow_contents
1548                                  + target_info->shadow_len));
1549
1550       /* Update the read buffer with this inserted breakpoint's
1551          shadow.  */
1552       memcpy (readbuf + bp_addr - memaddr,
1553               target_info->shadow_contents + bptoffset, bp_size);
1554     }
1555   else
1556     {
1557       const unsigned char *bp;
1558       CORE_ADDR addr = target_info->reqstd_address;
1559       int placed_size;
1560
1561       /* Update the shadow with what we want to write to memory.  */
1562       memcpy (target_info->shadow_contents + bptoffset,
1563               writebuf_org + bp_addr - memaddr, bp_size);
1564
1565       /* Determine appropriate breakpoint contents and size for this
1566          address.  */
1567       bp = gdbarch_breakpoint_from_pc (gdbarch, &addr, &placed_size);
1568
1569       /* Update the final write buffer with this inserted
1570          breakpoint's INSN.  */
1571       memcpy (writebuf + bp_addr - memaddr, bp + bptoffset, bp_size);
1572     }
1573 }
1574
1575 /* Update BUF, which is LEN bytes read from the target address MEMADDR,
1576    by replacing any memory breakpoints with their shadowed contents.
1577
1578    If READBUF is not NULL, this buffer must not overlap with any of
1579    the breakpoint location's shadow_contents buffers.  Otherwise,
1580    a failed assertion internal error will be raised.
1581
1582    The range of shadowed area by each bp_location is:
1583      bl->address - bp_location_placed_address_before_address_max
1584      up to bl->address + bp_location_shadow_len_after_address_max
1585    The range we were requested to resolve shadows for is:
1586      memaddr ... memaddr + len
1587    Thus the safe cutoff boundaries for performance optimization are
1588      memaddr + len <= (bl->address
1589                        - bp_location_placed_address_before_address_max)
1590    and:
1591      bl->address + bp_location_shadow_len_after_address_max <= memaddr  */
1592
1593 void
1594 breakpoint_xfer_memory (gdb_byte *readbuf, gdb_byte *writebuf,
1595                         const gdb_byte *writebuf_org,
1596                         ULONGEST memaddr, LONGEST len)
1597 {
1598   /* Left boundary, right boundary and median element of our binary
1599      search.  */
1600   unsigned bc_l, bc_r, bc;
1601
1602   /* Find BC_L which is a leftmost element which may affect BUF
1603      content.  It is safe to report lower value but a failure to
1604      report higher one.  */
1605
1606   bc_l = 0;
1607   bc_r = bp_location_count;
1608   while (bc_l + 1 < bc_r)
1609     {
1610       struct bp_location *bl;
1611
1612       bc = (bc_l + bc_r) / 2;
1613       bl = bp_location[bc];
1614
1615       /* Check first BL->ADDRESS will not overflow due to the added
1616          constant.  Then advance the left boundary only if we are sure
1617          the BC element can in no way affect the BUF content (MEMADDR
1618          to MEMADDR + LEN range).
1619
1620          Use the BP_LOCATION_SHADOW_LEN_AFTER_ADDRESS_MAX safety
1621          offset so that we cannot miss a breakpoint with its shadow
1622          range tail still reaching MEMADDR.  */
1623
1624       if ((bl->address + bp_location_shadow_len_after_address_max
1625            >= bl->address)
1626           && (bl->address + bp_location_shadow_len_after_address_max
1627               <= memaddr))
1628         bc_l = bc;
1629       else
1630         bc_r = bc;
1631     }
1632
1633   /* Due to the binary search above, we need to make sure we pick the
1634      first location that's at BC_L's address.  E.g., if there are
1635      multiple locations at the same address, BC_L may end up pointing
1636      at a duplicate location, and miss the "master"/"inserted"
1637      location.  Say, given locations L1, L2 and L3 at addresses A and
1638      B:
1639
1640       L1@A, L2@A, L3@B, ...
1641
1642      BC_L could end up pointing at location L2, while the "master"
1643      location could be L1.  Since the `loc->inserted' flag is only set
1644      on "master" locations, we'd forget to restore the shadow of L1
1645      and L2.  */
1646   while (bc_l > 0
1647          && bp_location[bc_l]->address == bp_location[bc_l - 1]->address)
1648     bc_l--;
1649
1650   /* Now do full processing of the found relevant range of elements.  */
1651
1652   for (bc = bc_l; bc < bp_location_count; bc++)
1653   {
1654     struct bp_location *bl = bp_location[bc];
1655
1656     /* bp_location array has BL->OWNER always non-NULL.  */
1657     if (bl->owner->type == bp_none)
1658       warning (_("reading through apparently deleted breakpoint #%d?"),
1659                bl->owner->number);
1660
1661     /* Performance optimization: any further element can no longer affect BUF
1662        content.  */
1663
1664     if (bl->address >= bp_location_placed_address_before_address_max
1665         && memaddr + len <= (bl->address
1666                              - bp_location_placed_address_before_address_max))
1667       break;
1668
1669     if (!bp_location_has_shadow (bl))
1670       continue;
1671
1672     one_breakpoint_xfer_memory (readbuf, writebuf, writebuf_org,
1673                                 memaddr, len, &bl->target_info, bl->gdbarch);
1674   }
1675 }
1676
1677 \f
1678
1679 /* Return true if BPT is either a software breakpoint or a hardware
1680    breakpoint.  */
1681
1682 int
1683 is_breakpoint (const struct breakpoint *bpt)
1684 {
1685   return (bpt->type == bp_breakpoint
1686           || bpt->type == bp_hardware_breakpoint
1687           || bpt->type == bp_dprintf);
1688 }
1689
1690 /* Return true if BPT is of any hardware watchpoint kind.  */
1691
1692 static int
1693 is_hardware_watchpoint (const struct breakpoint *bpt)
1694 {
1695   return (bpt->type == bp_hardware_watchpoint
1696           || bpt->type == bp_read_watchpoint
1697           || bpt->type == bp_access_watchpoint);
1698 }
1699
1700 /* Return true if BPT is of any watchpoint kind, hardware or
1701    software.  */
1702
1703 int
1704 is_watchpoint (const struct breakpoint *bpt)
1705 {
1706   return (is_hardware_watchpoint (bpt)
1707           || bpt->type == bp_watchpoint);
1708 }
1709
1710 /* Returns true if the current thread and its running state are safe
1711    to evaluate or update watchpoint B.  Watchpoints on local
1712    expressions need to be evaluated in the context of the thread that
1713    was current when the watchpoint was created, and, that thread needs
1714    to be stopped to be able to select the correct frame context.
1715    Watchpoints on global expressions can be evaluated on any thread,
1716    and in any state.  It is presently left to the target allowing
1717    memory accesses when threads are running.  */
1718
1719 static int
1720 watchpoint_in_thread_scope (struct watchpoint *b)
1721 {
1722   return (b->base.pspace == current_program_space
1723           && (ptid_equal (b->watchpoint_thread, null_ptid)
1724               || (ptid_equal (inferior_ptid, b->watchpoint_thread)
1725                   && !is_executing (inferior_ptid))));
1726 }
1727
1728 /* Set watchpoint B to disp_del_at_next_stop, even including its possible
1729    associated bp_watchpoint_scope breakpoint.  */
1730
1731 static void
1732 watchpoint_del_at_next_stop (struct watchpoint *w)
1733 {
1734   struct breakpoint *b = &w->base;
1735
1736   if (b->related_breakpoint != b)
1737     {
1738       gdb_assert (b->related_breakpoint->type == bp_watchpoint_scope);
1739       gdb_assert (b->related_breakpoint->related_breakpoint == b);
1740       b->related_breakpoint->disposition = disp_del_at_next_stop;
1741       b->related_breakpoint->related_breakpoint = b->related_breakpoint;
1742       b->related_breakpoint = b;
1743     }
1744   b->disposition = disp_del_at_next_stop;
1745 }
1746
1747 /* Extract a bitfield value from value VAL using the bit parameters contained in
1748    watchpoint W.  */
1749
1750 static struct value *
1751 extract_bitfield_from_watchpoint_value (struct watchpoint *w, struct value *val)
1752 {
1753   struct value *bit_val;
1754
1755   if (val == NULL)
1756     return NULL;
1757
1758   bit_val = allocate_value (value_type (val));
1759
1760   unpack_value_bitfield (bit_val,
1761                          w->val_bitpos,
1762                          w->val_bitsize,
1763                          value_contents_for_printing (val),
1764                          value_offset (val),
1765                          val);
1766
1767   return bit_val;
1768 }
1769
1770 /* Allocate a dummy location and add it to B, which must be a software
1771    watchpoint.  This is required because even if a software watchpoint
1772    is not watching any memory, bpstat_stop_status requires a location
1773    to be able to report stops.  */
1774
1775 static void
1776 software_watchpoint_add_no_memory_location (struct breakpoint *b,
1777                                             struct program_space *pspace)
1778 {
1779   gdb_assert (b->type == bp_watchpoint && b->loc == NULL);
1780
1781   b->loc = allocate_bp_location (b);
1782   b->loc->pspace = pspace;
1783   b->loc->address = -1;
1784   b->loc->length = -1;
1785 }
1786
1787 /* Returns true if B is a software watchpoint that is not watching any
1788    memory (e.g., "watch $pc").  */
1789
1790 static int
1791 is_no_memory_software_watchpoint (struct breakpoint *b)
1792 {
1793   return (b->type == bp_watchpoint
1794           && b->loc != NULL
1795           && b->loc->next == NULL
1796           && b->loc->address == -1
1797           && b->loc->length == -1);
1798 }
1799
1800 /* Assuming that B is a watchpoint:
1801    - Reparse watchpoint expression, if REPARSE is non-zero
1802    - Evaluate expression and store the result in B->val
1803    - Evaluate the condition if there is one, and store the result
1804      in b->loc->cond.
1805    - Update the list of values that must be watched in B->loc.
1806
1807    If the watchpoint disposition is disp_del_at_next_stop, then do
1808    nothing.  If this is local watchpoint that is out of scope, delete
1809    it.
1810
1811    Even with `set breakpoint always-inserted on' the watchpoints are
1812    removed + inserted on each stop here.  Normal breakpoints must
1813    never be removed because they might be missed by a running thread
1814    when debugging in non-stop mode.  On the other hand, hardware
1815    watchpoints (is_hardware_watchpoint; processed here) are specific
1816    to each LWP since they are stored in each LWP's hardware debug
1817    registers.  Therefore, such LWP must be stopped first in order to
1818    be able to modify its hardware watchpoints.
1819
1820    Hardware watchpoints must be reset exactly once after being
1821    presented to the user.  It cannot be done sooner, because it would
1822    reset the data used to present the watchpoint hit to the user.  And
1823    it must not be done later because it could display the same single
1824    watchpoint hit during multiple GDB stops.  Note that the latter is
1825    relevant only to the hardware watchpoint types bp_read_watchpoint
1826    and bp_access_watchpoint.  False hit by bp_hardware_watchpoint is
1827    not user-visible - its hit is suppressed if the memory content has
1828    not changed.
1829
1830    The following constraints influence the location where we can reset
1831    hardware watchpoints:
1832
1833    * target_stopped_by_watchpoint and target_stopped_data_address are
1834      called several times when GDB stops.
1835
1836    [linux] 
1837    * Multiple hardware watchpoints can be hit at the same time,
1838      causing GDB to stop.  GDB only presents one hardware watchpoint
1839      hit at a time as the reason for stopping, and all the other hits
1840      are presented later, one after the other, each time the user
1841      requests the execution to be resumed.  Execution is not resumed
1842      for the threads still having pending hit event stored in
1843      LWP_INFO->STATUS.  While the watchpoint is already removed from
1844      the inferior on the first stop the thread hit event is kept being
1845      reported from its cached value by linux_nat_stopped_data_address
1846      until the real thread resume happens after the watchpoint gets
1847      presented and thus its LWP_INFO->STATUS gets reset.
1848
1849    Therefore the hardware watchpoint hit can get safely reset on the
1850    watchpoint removal from inferior.  */
1851
1852 static void
1853 update_watchpoint (struct watchpoint *b, int reparse)
1854 {
1855   int within_current_scope;
1856   struct frame_id saved_frame_id;
1857   int frame_saved;
1858
1859   /* If this is a local watchpoint, we only want to check if the
1860      watchpoint frame is in scope if the current thread is the thread
1861      that was used to create the watchpoint.  */
1862   if (!watchpoint_in_thread_scope (b))
1863     return;
1864
1865   if (b->base.disposition == disp_del_at_next_stop)
1866     return;
1867  
1868   frame_saved = 0;
1869
1870   /* Determine if the watchpoint is within scope.  */
1871   if (b->exp_valid_block == NULL)
1872     within_current_scope = 1;
1873   else
1874     {
1875       struct frame_info *fi = get_current_frame ();
1876       struct gdbarch *frame_arch = get_frame_arch (fi);
1877       CORE_ADDR frame_pc = get_frame_pc (fi);
1878
1879       /* If we're at a point where the stack has been destroyed
1880          (e.g. in a function epilogue), unwinding may not work
1881          properly. Do not attempt to recreate locations at this
1882          point.  See similar comments in watchpoint_check.  */
1883       if (gdbarch_stack_frame_destroyed_p (frame_arch, frame_pc))
1884         return;
1885
1886       /* Save the current frame's ID so we can restore it after
1887          evaluating the watchpoint expression on its own frame.  */
1888       /* FIXME drow/2003-09-09: It would be nice if evaluate_expression
1889          took a frame parameter, so that we didn't have to change the
1890          selected frame.  */
1891       frame_saved = 1;
1892       saved_frame_id = get_frame_id (get_selected_frame (NULL));
1893
1894       fi = frame_find_by_id (b->watchpoint_frame);
1895       within_current_scope = (fi != NULL);
1896       if (within_current_scope)
1897         select_frame (fi);
1898     }
1899
1900   /* We don't free locations.  They are stored in the bp_location array
1901      and update_global_location_list will eventually delete them and
1902      remove breakpoints if needed.  */
1903   b->base.loc = NULL;
1904
1905   if (within_current_scope && reparse)
1906     {
1907       const char *s;
1908
1909       if (b->exp)
1910         {
1911           xfree (b->exp);
1912           b->exp = NULL;
1913         }
1914       s = b->exp_string_reparse ? b->exp_string_reparse : b->exp_string;
1915       b->exp = parse_exp_1 (&s, 0, b->exp_valid_block, 0);
1916       /* If the meaning of expression itself changed, the old value is
1917          no longer relevant.  We don't want to report a watchpoint hit
1918          to the user when the old value and the new value may actually
1919          be completely different objects.  */
1920       value_free (b->val);
1921       b->val = NULL;
1922       b->val_valid = 0;
1923
1924       /* Note that unlike with breakpoints, the watchpoint's condition
1925          expression is stored in the breakpoint object, not in the
1926          locations (re)created below.  */
1927       if (b->base.cond_string != NULL)
1928         {
1929           if (b->cond_exp != NULL)
1930             {
1931               xfree (b->cond_exp);
1932               b->cond_exp = NULL;
1933             }
1934
1935           s = b->base.cond_string;
1936           b->cond_exp = parse_exp_1 (&s, 0, b->cond_exp_valid_block, 0);
1937         }
1938     }
1939
1940   /* If we failed to parse the expression, for example because
1941      it refers to a global variable in a not-yet-loaded shared library,
1942      don't try to insert watchpoint.  We don't automatically delete
1943      such watchpoint, though, since failure to parse expression
1944      is different from out-of-scope watchpoint.  */
1945   if (!target_has_execution)
1946     {
1947       /* Without execution, memory can't change.  No use to try and
1948          set watchpoint locations.  The watchpoint will be reset when
1949          the target gains execution, through breakpoint_re_set.  */
1950       if (!can_use_hw_watchpoints)
1951         {
1952           if (b->base.ops->works_in_software_mode (&b->base))
1953             b->base.type = bp_watchpoint;
1954           else
1955             error (_("Can't set read/access watchpoint when "
1956                      "hardware watchpoints are disabled."));
1957         }
1958     }
1959   else if (within_current_scope && b->exp)
1960     {
1961       int pc = 0;
1962       struct value *val_chain, *v, *result, *next;
1963       struct program_space *frame_pspace;
1964
1965       fetch_subexp_value (b->exp, &pc, &v, &result, &val_chain, 0);
1966
1967       /* Avoid setting b->val if it's already set.  The meaning of
1968          b->val is 'the last value' user saw, and we should update
1969          it only if we reported that last value to user.  As it
1970          happens, the code that reports it updates b->val directly.
1971          We don't keep track of the memory value for masked
1972          watchpoints.  */
1973       if (!b->val_valid && !is_masked_watchpoint (&b->base))
1974         {
1975           if (b->val_bitsize != 0)
1976             {
1977               v = extract_bitfield_from_watchpoint_value (b, v);
1978               if (v != NULL)
1979                 release_value (v);
1980             }
1981           b->val = v;
1982           b->val_valid = 1;
1983         }
1984
1985       frame_pspace = get_frame_program_space (get_selected_frame (NULL));
1986
1987       /* Look at each value on the value chain.  */
1988       for (v = val_chain; v; v = value_next (v))
1989         {
1990           /* If it's a memory location, and GDB actually needed
1991              its contents to evaluate the expression, then we
1992              must watch it.  If the first value returned is
1993              still lazy, that means an error occurred reading it;
1994              watch it anyway in case it becomes readable.  */
1995           if (VALUE_LVAL (v) == lval_memory
1996               && (v == val_chain || ! value_lazy (v)))
1997             {
1998               struct type *vtype = check_typedef (value_type (v));
1999
2000               /* We only watch structs and arrays if user asked
2001                  for it explicitly, never if they just happen to
2002                  appear in the middle of some value chain.  */
2003               if (v == result
2004                   || (TYPE_CODE (vtype) != TYPE_CODE_STRUCT
2005                       && TYPE_CODE (vtype) != TYPE_CODE_ARRAY))
2006                 {
2007                   CORE_ADDR addr;
2008                   enum target_hw_bp_type type;
2009                   struct bp_location *loc, **tmp;
2010                   int bitpos = 0, bitsize = 0;
2011
2012                   if (value_bitsize (v) != 0)
2013                     {
2014                       /* Extract the bit parameters out from the bitfield
2015                          sub-expression.  */
2016                       bitpos = value_bitpos (v);
2017                       bitsize = value_bitsize (v);
2018                     }
2019                   else if (v == result && b->val_bitsize != 0)
2020                     {
2021                      /* If VAL_BITSIZE != 0 then RESULT is actually a bitfield
2022                         lvalue whose bit parameters are saved in the fields
2023                         VAL_BITPOS and VAL_BITSIZE.  */
2024                       bitpos = b->val_bitpos;
2025                       bitsize = b->val_bitsize;
2026                     }
2027
2028                   addr = value_address (v);
2029                   if (bitsize != 0)
2030                     {
2031                       /* Skip the bytes that don't contain the bitfield.  */
2032                       addr += bitpos / 8;
2033                     }
2034
2035                   type = hw_write;
2036                   if (b->base.type == bp_read_watchpoint)
2037                     type = hw_read;
2038                   else if (b->base.type == bp_access_watchpoint)
2039                     type = hw_access;
2040
2041                   loc = allocate_bp_location (&b->base);
2042                   for (tmp = &(b->base.loc); *tmp != NULL; tmp = &((*tmp)->next))
2043                     ;
2044                   *tmp = loc;
2045                   loc->gdbarch = get_type_arch (value_type (v));
2046
2047                   loc->pspace = frame_pspace;
2048                   loc->address = addr;
2049
2050                   if (bitsize != 0)
2051                     {
2052                       /* Just cover the bytes that make up the bitfield.  */
2053                       loc->length = ((bitpos % 8) + bitsize + 7) / 8;
2054                     }
2055                   else
2056                     loc->length = TYPE_LENGTH (value_type (v));
2057
2058                   loc->watchpoint_type = type;
2059                 }
2060             }
2061         }
2062
2063       /* Change the type of breakpoint between hardware assisted or
2064          an ordinary watchpoint depending on the hardware support
2065          and free hardware slots.  REPARSE is set when the inferior
2066          is started.  */
2067       if (reparse)
2068         {
2069           int reg_cnt;
2070           enum bp_loc_type loc_type;
2071           struct bp_location *bl;
2072
2073           reg_cnt = can_use_hardware_watchpoint (val_chain);
2074
2075           if (reg_cnt)
2076             {
2077               int i, target_resources_ok, other_type_used;
2078               enum bptype type;
2079
2080               /* Use an exact watchpoint when there's only one memory region to be
2081                  watched, and only one debug register is needed to watch it.  */
2082               b->exact = target_exact_watchpoints && reg_cnt == 1;
2083
2084               /* We need to determine how many resources are already
2085                  used for all other hardware watchpoints plus this one
2086                  to see if we still have enough resources to also fit
2087                  this watchpoint in as well.  */
2088
2089               /* If this is a software watchpoint, we try to turn it
2090                  to a hardware one -- count resources as if B was of
2091                  hardware watchpoint type.  */
2092               type = b->base.type;
2093               if (type == bp_watchpoint)
2094                 type = bp_hardware_watchpoint;
2095
2096               /* This watchpoint may or may not have been placed on
2097                  the list yet at this point (it won't be in the list
2098                  if we're trying to create it for the first time,
2099                  through watch_command), so always account for it
2100                  manually.  */
2101
2102               /* Count resources used by all watchpoints except B.  */
2103               i = hw_watchpoint_used_count_others (&b->base, type, &other_type_used);
2104
2105               /* Add in the resources needed for B.  */
2106               i += hw_watchpoint_use_count (&b->base);
2107
2108               target_resources_ok
2109                 = target_can_use_hardware_watchpoint (type, i, other_type_used);
2110               if (target_resources_ok <= 0)
2111                 {
2112                   int sw_mode = b->base.ops->works_in_software_mode (&b->base);
2113
2114                   if (target_resources_ok == 0 && !sw_mode)
2115                     error (_("Target does not support this type of "
2116                              "hardware watchpoint."));
2117                   else if (target_resources_ok < 0 && !sw_mode)
2118                     error (_("There are not enough available hardware "
2119                              "resources for this watchpoint."));
2120
2121                   /* Downgrade to software watchpoint.  */
2122                   b->base.type = bp_watchpoint;
2123                 }
2124               else
2125                 {
2126                   /* If this was a software watchpoint, we've just
2127                      found we have enough resources to turn it to a
2128                      hardware watchpoint.  Otherwise, this is a
2129                      nop.  */
2130                   b->base.type = type;
2131                 }
2132             }
2133           else if (!b->base.ops->works_in_software_mode (&b->base))
2134             {
2135               if (!can_use_hw_watchpoints)
2136                 error (_("Can't set read/access watchpoint when "
2137                          "hardware watchpoints are disabled."));
2138               else
2139                 error (_("Expression cannot be implemented with "
2140                          "read/access watchpoint."));
2141             }
2142           else
2143             b->base.type = bp_watchpoint;
2144
2145           loc_type = (b->base.type == bp_watchpoint? bp_loc_other
2146                       : bp_loc_hardware_watchpoint);
2147           for (bl = b->base.loc; bl; bl = bl->next)
2148             bl->loc_type = loc_type;
2149         }
2150
2151       for (v = val_chain; v; v = next)
2152         {
2153           next = value_next (v);
2154           if (v != b->val)
2155             value_free (v);
2156         }
2157
2158       /* If a software watchpoint is not watching any memory, then the
2159          above left it without any location set up.  But,
2160          bpstat_stop_status requires a location to be able to report
2161          stops, so make sure there's at least a dummy one.  */
2162       if (b->base.type == bp_watchpoint && b->base.loc == NULL)
2163         software_watchpoint_add_no_memory_location (&b->base, frame_pspace);
2164     }
2165   else if (!within_current_scope)
2166     {
2167       printf_filtered (_("\
2168 Watchpoint %d deleted because the program has left the block\n\
2169 in which its expression is valid.\n"),
2170                        b->base.number);
2171       watchpoint_del_at_next_stop (b);
2172     }
2173
2174   /* Restore the selected frame.  */
2175   if (frame_saved)
2176     select_frame (frame_find_by_id (saved_frame_id));
2177 }
2178
2179
2180 /* Returns 1 iff breakpoint location should be
2181    inserted in the inferior.  We don't differentiate the type of BL's owner
2182    (breakpoint vs. tracepoint), although insert_location in tracepoint's
2183    breakpoint_ops is not defined, because in insert_bp_location,
2184    tracepoint's insert_location will not be called.  */
2185 static int
2186 should_be_inserted (struct bp_location *bl)
2187 {
2188   if (bl->owner == NULL || !breakpoint_enabled (bl->owner))
2189     return 0;
2190
2191   if (bl->owner->disposition == disp_del_at_next_stop)
2192     return 0;
2193
2194   if (!bl->enabled || bl->shlib_disabled || bl->duplicate)
2195     return 0;
2196
2197   if (user_breakpoint_p (bl->owner) && bl->pspace->executing_startup)
2198     return 0;
2199
2200   /* This is set for example, when we're attached to the parent of a
2201      vfork, and have detached from the child.  The child is running
2202      free, and we expect it to do an exec or exit, at which point the
2203      OS makes the parent schedulable again (and the target reports
2204      that the vfork is done).  Until the child is done with the shared
2205      memory region, do not insert breakpoints in the parent, otherwise
2206      the child could still trip on the parent's breakpoints.  Since
2207      the parent is blocked anyway, it won't miss any breakpoint.  */
2208   if (bl->pspace->breakpoints_not_allowed)
2209     return 0;
2210
2211   /* Don't insert a breakpoint if we're trying to step past its
2212      location, except if the breakpoint is a single-step breakpoint,
2213      and the breakpoint's thread is the thread which is stepping past
2214      a breakpoint.  */
2215   if ((bl->loc_type == bp_loc_software_breakpoint
2216        || bl->loc_type == bp_loc_hardware_breakpoint)
2217       && stepping_past_instruction_at (bl->pspace->aspace,
2218                                        bl->address)
2219       /* The single-step breakpoint may be inserted at the location
2220          we're trying to step if the instruction branches to itself.
2221          However, the instruction won't be executed at all and it may
2222          break the semantics of the instruction, for example, the
2223          instruction is a conditional branch or updates some flags.
2224          We can't fix it unless GDB is able to emulate the instruction
2225          or switch to displaced stepping.  */
2226       && !(bl->owner->type == bp_single_step
2227            && thread_is_stepping_over_breakpoint (bl->owner->thread)))
2228     {
2229       if (debug_infrun)
2230         {
2231           fprintf_unfiltered (gdb_stdlog,
2232                               "infrun: skipping breakpoint: "
2233                               "stepping past insn at: %s\n",
2234                               paddress (bl->gdbarch, bl->address));
2235         }
2236       return 0;
2237     }
2238
2239   /* Don't insert watchpoints if we're trying to step past the
2240      instruction that triggered one.  */
2241   if ((bl->loc_type == bp_loc_hardware_watchpoint)
2242       && stepping_past_nonsteppable_watchpoint ())
2243     {
2244       if (debug_infrun)
2245         {
2246           fprintf_unfiltered (gdb_stdlog,
2247                               "infrun: stepping past non-steppable watchpoint. "
2248                               "skipping watchpoint at %s:%d\n",
2249                               paddress (bl->gdbarch, bl->address),
2250                               bl->length);
2251         }
2252       return 0;
2253     }
2254
2255   return 1;
2256 }
2257
2258 /* Same as should_be_inserted but does the check assuming
2259    that the location is not duplicated.  */
2260
2261 static int
2262 unduplicated_should_be_inserted (struct bp_location *bl)
2263 {
2264   int result;
2265   const int save_duplicate = bl->duplicate;
2266
2267   bl->duplicate = 0;
2268   result = should_be_inserted (bl);
2269   bl->duplicate = save_duplicate;
2270   return result;
2271 }
2272
2273 /* Parses a conditional described by an expression COND into an
2274    agent expression bytecode suitable for evaluation
2275    by the bytecode interpreter.  Return NULL if there was
2276    any error during parsing.  */
2277
2278 static struct agent_expr *
2279 parse_cond_to_aexpr (CORE_ADDR scope, struct expression *cond)
2280 {
2281   struct agent_expr *aexpr = NULL;
2282
2283   if (!cond)
2284     return NULL;
2285
2286   /* We don't want to stop processing, so catch any errors
2287      that may show up.  */
2288   TRY
2289     {
2290       aexpr = gen_eval_for_expr (scope, cond);
2291     }
2292
2293   CATCH (ex, RETURN_MASK_ERROR)
2294     {
2295       /* If we got here, it means the condition could not be parsed to a valid
2296          bytecode expression and thus can't be evaluated on the target's side.
2297          It's no use iterating through the conditions.  */
2298       return NULL;
2299     }
2300   END_CATCH
2301
2302   /* We have a valid agent expression.  */
2303   return aexpr;
2304 }
2305
2306 /* Based on location BL, create a list of breakpoint conditions to be
2307    passed on to the target.  If we have duplicated locations with different
2308    conditions, we will add such conditions to the list.  The idea is that the
2309    target will evaluate the list of conditions and will only notify GDB when
2310    one of them is true.  */
2311
2312 static void
2313 build_target_condition_list (struct bp_location *bl)
2314 {
2315   struct bp_location **locp = NULL, **loc2p;
2316   int null_condition_or_parse_error = 0;
2317   int modified = bl->needs_update;
2318   struct bp_location *loc;
2319
2320   /* Release conditions left over from a previous insert.  */
2321   VEC_free (agent_expr_p, bl->target_info.conditions);
2322
2323   /* This is only meaningful if the target is
2324      evaluating conditions and if the user has
2325      opted for condition evaluation on the target's
2326      side.  */
2327   if (gdb_evaluates_breakpoint_condition_p ()
2328       || !target_supports_evaluation_of_breakpoint_conditions ())
2329     return;
2330
2331   /* Do a first pass to check for locations with no assigned
2332      conditions or conditions that fail to parse to a valid agent expression
2333      bytecode.  If any of these happen, then it's no use to send conditions
2334      to the target since this location will always trigger and generate a
2335      response back to GDB.  */
2336   ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2337     {
2338       loc = (*loc2p);
2339       if (is_breakpoint (loc->owner) && loc->pspace->num == bl->pspace->num)
2340         {
2341           if (modified)
2342             {
2343               struct agent_expr *aexpr;
2344
2345               /* Re-parse the conditions since something changed.  In that
2346                  case we already freed the condition bytecodes (see
2347                  force_breakpoint_reinsertion).  We just
2348                  need to parse the condition to bytecodes again.  */
2349               aexpr = parse_cond_to_aexpr (bl->address, loc->cond);
2350               loc->cond_bytecode = aexpr;
2351             }
2352
2353           /* If we have a NULL bytecode expression, it means something
2354              went wrong or we have a null condition expression.  */
2355           if (!loc->cond_bytecode)
2356             {
2357               null_condition_or_parse_error = 1;
2358               break;
2359             }
2360         }
2361     }
2362
2363   /* If any of these happened, it means we will have to evaluate the conditions
2364      for the location's address on gdb's side.  It is no use keeping bytecodes
2365      for all the other duplicate locations, thus we free all of them here.
2366
2367      This is so we have a finer control over which locations' conditions are
2368      being evaluated by GDB or the remote stub.  */
2369   if (null_condition_or_parse_error)
2370     {
2371       ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2372         {
2373           loc = (*loc2p);
2374           if (is_breakpoint (loc->owner) && loc->pspace->num == bl->pspace->num)
2375             {
2376               /* Only go as far as the first NULL bytecode is
2377                  located.  */
2378               if (!loc->cond_bytecode)
2379                 return;
2380
2381               free_agent_expr (loc->cond_bytecode);
2382               loc->cond_bytecode = NULL;
2383             }
2384         }
2385     }
2386
2387   /* No NULL conditions or failed bytecode generation.  Build a condition list
2388      for this location's address.  */
2389   ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2390     {
2391       loc = (*loc2p);
2392       if (loc->cond
2393           && is_breakpoint (loc->owner)
2394           && loc->pspace->num == bl->pspace->num
2395           && loc->owner->enable_state == bp_enabled
2396           && loc->enabled)
2397         /* Add the condition to the vector.  This will be used later to send the
2398            conditions to the target.  */
2399         VEC_safe_push (agent_expr_p, bl->target_info.conditions,
2400                        loc->cond_bytecode);
2401     }
2402
2403   return;
2404 }
2405
2406 /* Parses a command described by string CMD into an agent expression
2407    bytecode suitable for evaluation by the bytecode interpreter.
2408    Return NULL if there was any error during parsing.  */
2409
2410 static struct agent_expr *
2411 parse_cmd_to_aexpr (CORE_ADDR scope, char *cmd)
2412 {
2413   struct cleanup *old_cleanups = 0;
2414   struct expression *expr, **argvec;
2415   struct agent_expr *aexpr = NULL;
2416   const char *cmdrest;
2417   const char *format_start, *format_end;
2418   struct format_piece *fpieces;
2419   int nargs;
2420   struct gdbarch *gdbarch = get_current_arch ();
2421
2422   if (!cmd)
2423     return NULL;
2424
2425   cmdrest = cmd;
2426
2427   if (*cmdrest == ',')
2428     ++cmdrest;
2429   cmdrest = skip_spaces_const (cmdrest);
2430
2431   if (*cmdrest++ != '"')
2432     error (_("No format string following the location"));
2433
2434   format_start = cmdrest;
2435
2436   fpieces = parse_format_string (&cmdrest);
2437
2438   old_cleanups = make_cleanup (free_format_pieces_cleanup, &fpieces);
2439
2440   format_end = cmdrest;
2441
2442   if (*cmdrest++ != '"')
2443     error (_("Bad format string, non-terminated '\"'."));
2444   
2445   cmdrest = skip_spaces_const (cmdrest);
2446
2447   if (!(*cmdrest == ',' || *cmdrest == '\0'))
2448     error (_("Invalid argument syntax"));
2449
2450   if (*cmdrest == ',')
2451     cmdrest++;
2452   cmdrest = skip_spaces_const (cmdrest);
2453
2454   /* For each argument, make an expression.  */
2455
2456   argvec = (struct expression **) alloca (strlen (cmd)
2457                                          * sizeof (struct expression *));
2458
2459   nargs = 0;
2460   while (*cmdrest != '\0')
2461     {
2462       const char *cmd1;
2463
2464       cmd1 = cmdrest;
2465       expr = parse_exp_1 (&cmd1, scope, block_for_pc (scope), 1);
2466       argvec[nargs++] = expr;
2467       cmdrest = cmd1;
2468       if (*cmdrest == ',')
2469         ++cmdrest;
2470     }
2471
2472   /* We don't want to stop processing, so catch any errors
2473      that may show up.  */
2474   TRY
2475     {
2476       aexpr = gen_printf (scope, gdbarch, 0, 0,
2477                           format_start, format_end - format_start,
2478                           fpieces, nargs, argvec);
2479     }
2480   CATCH (ex, RETURN_MASK_ERROR)
2481     {
2482       /* If we got here, it means the command could not be parsed to a valid
2483          bytecode expression and thus can't be evaluated on the target's side.
2484          It's no use iterating through the other commands.  */
2485       aexpr = NULL;
2486     }
2487   END_CATCH
2488
2489   do_cleanups (old_cleanups);
2490
2491   /* We have a valid agent expression, return it.  */
2492   return aexpr;
2493 }
2494
2495 /* Based on location BL, create a list of breakpoint commands to be
2496    passed on to the target.  If we have duplicated locations with
2497    different commands, we will add any such to the list.  */
2498
2499 static void
2500 build_target_command_list (struct bp_location *bl)
2501 {
2502   struct bp_location **locp = NULL, **loc2p;
2503   int null_command_or_parse_error = 0;
2504   int modified = bl->needs_update;
2505   struct bp_location *loc;
2506
2507   /* Release commands left over from a previous insert.  */
2508   VEC_free (agent_expr_p, bl->target_info.tcommands);
2509
2510   if (!target_can_run_breakpoint_commands ())
2511     return;
2512
2513   /* For now, limit to agent-style dprintf breakpoints.  */
2514   if (dprintf_style != dprintf_style_agent)
2515     return;
2516
2517   /* For now, if we have any duplicate location that isn't a dprintf,
2518      don't install the target-side commands, as that would make the
2519      breakpoint not be reported to the core, and we'd lose
2520      control.  */
2521   ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2522     {
2523       loc = (*loc2p);
2524       if (is_breakpoint (loc->owner)
2525           && loc->pspace->num == bl->pspace->num
2526           && loc->owner->type != bp_dprintf)
2527         return;
2528     }
2529
2530   /* Do a first pass to check for locations with no assigned
2531      conditions or conditions that fail to parse to a valid agent expression
2532      bytecode.  If any of these happen, then it's no use to send conditions
2533      to the target since this location will always trigger and generate a
2534      response back to GDB.  */
2535   ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2536     {
2537       loc = (*loc2p);
2538       if (is_breakpoint (loc->owner) && loc->pspace->num == bl->pspace->num)
2539         {
2540           if (modified)
2541             {
2542               struct agent_expr *aexpr;
2543
2544               /* Re-parse the commands since something changed.  In that
2545                  case we already freed the command bytecodes (see
2546                  force_breakpoint_reinsertion).  We just
2547                  need to parse the command to bytecodes again.  */
2548               aexpr = parse_cmd_to_aexpr (bl->address,
2549                                           loc->owner->extra_string);
2550               loc->cmd_bytecode = aexpr;
2551             }
2552
2553           /* If we have a NULL bytecode expression, it means something
2554              went wrong or we have a null command expression.  */
2555           if (!loc->cmd_bytecode)
2556             {
2557               null_command_or_parse_error = 1;
2558               break;
2559             }
2560         }
2561     }
2562
2563   /* If anything failed, then we're not doing target-side commands,
2564      and so clean up.  */
2565   if (null_command_or_parse_error)
2566     {
2567       ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2568         {
2569           loc = (*loc2p);
2570           if (is_breakpoint (loc->owner)
2571               && loc->pspace->num == bl->pspace->num)
2572             {
2573               /* Only go as far as the first NULL bytecode is
2574                  located.  */
2575               if (loc->cmd_bytecode == NULL)
2576                 return;
2577
2578               free_agent_expr (loc->cmd_bytecode);
2579               loc->cmd_bytecode = NULL;
2580             }
2581         }
2582     }
2583
2584   /* No NULL commands or failed bytecode generation.  Build a command list
2585      for this location's address.  */
2586   ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2587     {
2588       loc = (*loc2p);
2589       if (loc->owner->extra_string
2590           && is_breakpoint (loc->owner)
2591           && loc->pspace->num == bl->pspace->num
2592           && loc->owner->enable_state == bp_enabled
2593           && loc->enabled)
2594         /* Add the command to the vector.  This will be used later
2595            to send the commands to the target.  */
2596         VEC_safe_push (agent_expr_p, bl->target_info.tcommands,
2597                        loc->cmd_bytecode);
2598     }
2599
2600   bl->target_info.persist = 0;
2601   /* Maybe flag this location as persistent.  */
2602   if (bl->owner->type == bp_dprintf && disconnected_dprintf)
2603     bl->target_info.persist = 1;
2604 }
2605
2606 /* Return the kind of breakpoint on address *ADDR.  */
2607
2608 static int
2609 breakpoint_kind (struct bp_location *bl, CORE_ADDR *addr)
2610 {
2611   return gdbarch_breakpoint_kind_from_pc (bl->gdbarch, addr);
2612 }
2613
2614 /* Insert a low-level "breakpoint" of some type.  BL is the breakpoint
2615    location.  Any error messages are printed to TMP_ERROR_STREAM; and
2616    DISABLED_BREAKS, and HW_BREAKPOINT_ERROR are used to report problems.
2617    Returns 0 for success, 1 if the bp_location type is not supported or
2618    -1 for failure.
2619
2620    NOTE drow/2003-09-09: This routine could be broken down to an
2621    object-style method for each breakpoint or catchpoint type.  */
2622 static int
2623 insert_bp_location (struct bp_location *bl,
2624                     struct ui_file *tmp_error_stream,
2625                     int *disabled_breaks,
2626                     int *hw_breakpoint_error,
2627                     int *hw_bp_error_explained_already)
2628 {
2629   enum errors bp_err = GDB_NO_ERROR;
2630   const char *bp_err_message = NULL;
2631
2632   if (!should_be_inserted (bl) || (bl->inserted && !bl->needs_update))
2633     return 0;
2634
2635   /* Note we don't initialize bl->target_info, as that wipes out
2636      the breakpoint location's shadow_contents if the breakpoint
2637      is still inserted at that location.  This in turn breaks
2638      target_read_memory which depends on these buffers when
2639      a memory read is requested at the breakpoint location:
2640      Once the target_info has been wiped, we fail to see that
2641      we have a breakpoint inserted at that address and thus
2642      read the breakpoint instead of returning the data saved in
2643      the breakpoint location's shadow contents.  */
2644   bl->target_info.reqstd_address = bl->address;
2645   bl->target_info.placed_address_space = bl->pspace->aspace;
2646   bl->target_info.length = bl->length;
2647
2648   /* When working with target-side conditions, we must pass all the conditions
2649      for the same breakpoint address down to the target since GDB will not
2650      insert those locations.  With a list of breakpoint conditions, the target
2651      can decide when to stop and notify GDB.  */
2652
2653   if (is_breakpoint (bl->owner))
2654     {
2655       build_target_condition_list (bl);
2656       build_target_command_list (bl);
2657       /* Reset the modification marker.  */
2658       bl->needs_update = 0;
2659     }
2660
2661   if (bl->loc_type == bp_loc_software_breakpoint
2662       || bl->loc_type == bp_loc_hardware_breakpoint)
2663     {
2664       if (bl->owner->type != bp_hardware_breakpoint)
2665         {
2666           /* If the explicitly specified breakpoint type
2667              is not hardware breakpoint, check the memory map to see
2668              if the breakpoint address is in read only memory or not.
2669
2670              Two important cases are:
2671              - location type is not hardware breakpoint, memory
2672              is readonly.  We change the type of the location to
2673              hardware breakpoint.
2674              - location type is hardware breakpoint, memory is
2675              read-write.  This means we've previously made the
2676              location hardware one, but then the memory map changed,
2677              so we undo.
2678              
2679              When breakpoints are removed, remove_breakpoints will use
2680              location types we've just set here, the only possible
2681              problem is that memory map has changed during running
2682              program, but it's not going to work anyway with current
2683              gdb.  */
2684           struct mem_region *mr 
2685             = lookup_mem_region (bl->target_info.reqstd_address);
2686           
2687           if (mr)
2688             {
2689               if (automatic_hardware_breakpoints)
2690                 {
2691                   enum bp_loc_type new_type;
2692                   
2693                   if (mr->attrib.mode != MEM_RW)
2694                     new_type = bp_loc_hardware_breakpoint;
2695                   else 
2696                     new_type = bp_loc_software_breakpoint;
2697                   
2698                   if (new_type != bl->loc_type)
2699                     {
2700                       static int said = 0;
2701
2702                       bl->loc_type = new_type;
2703                       if (!said)
2704                         {
2705                           fprintf_filtered (gdb_stdout,
2706                                             _("Note: automatically using "
2707                                               "hardware breakpoints for "
2708                                               "read-only addresses.\n"));
2709                           said = 1;
2710                         }
2711                     }
2712                 }
2713               else if (bl->loc_type == bp_loc_software_breakpoint
2714                        && mr->attrib.mode != MEM_RW)
2715                 {
2716                   fprintf_unfiltered (tmp_error_stream,
2717                                       _("Cannot insert breakpoint %d.\n"
2718                                         "Cannot set software breakpoint "
2719                                         "at read-only address %s\n"),
2720                                       bl->owner->number,
2721                                       paddress (bl->gdbarch, bl->address));
2722                   return 1;
2723                 }
2724             }
2725         }
2726         
2727       /* First check to see if we have to handle an overlay.  */
2728       if (overlay_debugging == ovly_off
2729           || bl->section == NULL
2730           || !(section_is_overlay (bl->section)))
2731         {
2732           /* No overlay handling: just set the breakpoint.  */
2733           TRY
2734             {
2735               int val;
2736
2737               val = bl->owner->ops->insert_location (bl);
2738               if (val)
2739                 bp_err = GENERIC_ERROR;
2740             }
2741           CATCH (e, RETURN_MASK_ALL)
2742             {
2743               bp_err = e.error;
2744               bp_err_message = e.message;
2745             }
2746           END_CATCH
2747         }
2748       else
2749         {
2750           /* This breakpoint is in an overlay section.
2751              Shall we set a breakpoint at the LMA?  */
2752           if (!overlay_events_enabled)
2753             {
2754               /* Yes -- overlay event support is not active, 
2755                  so we must try to set a breakpoint at the LMA.
2756                  This will not work for a hardware breakpoint.  */
2757               if (bl->loc_type == bp_loc_hardware_breakpoint)
2758                 warning (_("hardware breakpoint %d not supported in overlay!"),
2759                          bl->owner->number);
2760               else
2761                 {
2762                   CORE_ADDR addr = overlay_unmapped_address (bl->address,
2763                                                              bl->section);
2764                   /* Set a software (trap) breakpoint at the LMA.  */
2765                   bl->overlay_target_info = bl->target_info;
2766                   bl->overlay_target_info.reqstd_address = addr;
2767
2768                   /* No overlay handling: just set the breakpoint.  */
2769                   TRY
2770                     {
2771                       int val;
2772
2773                       bl->overlay_target_info.kind
2774                         = breakpoint_kind (bl, &addr);
2775                       bl->overlay_target_info.placed_address = addr;
2776                       val = target_insert_breakpoint (bl->gdbarch,
2777                                                       &bl->overlay_target_info);
2778                       if (val)
2779                         bp_err = GENERIC_ERROR;
2780                     }
2781                   CATCH (e, RETURN_MASK_ALL)
2782                     {
2783                       bp_err = e.error;
2784                       bp_err_message = e.message;
2785                     }
2786                   END_CATCH
2787
2788                   if (bp_err != GDB_NO_ERROR)
2789                     fprintf_unfiltered (tmp_error_stream,
2790                                         "Overlay breakpoint %d "
2791                                         "failed: in ROM?\n",
2792                                         bl->owner->number);
2793                 }
2794             }
2795           /* Shall we set a breakpoint at the VMA? */
2796           if (section_is_mapped (bl->section))
2797             {
2798               /* Yes.  This overlay section is mapped into memory.  */
2799               TRY
2800                 {
2801                   int val;
2802
2803                   val = bl->owner->ops->insert_location (bl);
2804                   if (val)
2805                     bp_err = GENERIC_ERROR;
2806                 }
2807               CATCH (e, RETURN_MASK_ALL)
2808                 {
2809                   bp_err = e.error;
2810                   bp_err_message = e.message;
2811                 }
2812               END_CATCH
2813             }
2814           else
2815             {
2816               /* No.  This breakpoint will not be inserted.  
2817                  No error, but do not mark the bp as 'inserted'.  */
2818               return 0;
2819             }
2820         }
2821
2822       if (bp_err != GDB_NO_ERROR)
2823         {
2824           /* Can't set the breakpoint.  */
2825
2826           /* In some cases, we might not be able to insert a
2827              breakpoint in a shared library that has already been
2828              removed, but we have not yet processed the shlib unload
2829              event.  Unfortunately, some targets that implement
2830              breakpoint insertion themselves can't tell why the
2831              breakpoint insertion failed (e.g., the remote target
2832              doesn't define error codes), so we must treat generic
2833              errors as memory errors.  */
2834           if ((bp_err == GENERIC_ERROR || bp_err == MEMORY_ERROR)
2835               && bl->loc_type == bp_loc_software_breakpoint
2836               && (solib_name_from_address (bl->pspace, bl->address)
2837                   || shared_objfile_contains_address_p (bl->pspace,
2838                                                         bl->address)))
2839             {
2840               /* See also: disable_breakpoints_in_shlibs.  */
2841               bl->shlib_disabled = 1;
2842               observer_notify_breakpoint_modified (bl->owner);
2843               if (!*disabled_breaks)
2844                 {
2845                   fprintf_unfiltered (tmp_error_stream, 
2846                                       "Cannot insert breakpoint %d.\n", 
2847                                       bl->owner->number);
2848                   fprintf_unfiltered (tmp_error_stream, 
2849                                       "Temporarily disabling shared "
2850                                       "library breakpoints:\n");
2851                 }
2852               *disabled_breaks = 1;
2853               fprintf_unfiltered (tmp_error_stream,
2854                                   "breakpoint #%d\n", bl->owner->number);
2855               return 0;
2856             }
2857           else
2858             {
2859               if (bl->loc_type == bp_loc_hardware_breakpoint)
2860                 {
2861                   *hw_breakpoint_error = 1;
2862                   *hw_bp_error_explained_already = bp_err_message != NULL;
2863                   fprintf_unfiltered (tmp_error_stream,
2864                                       "Cannot insert hardware breakpoint %d%s",
2865                                       bl->owner->number, bp_err_message ? ":" : ".\n");
2866                   if (bp_err_message != NULL)
2867                     fprintf_unfiltered (tmp_error_stream, "%s.\n", bp_err_message);
2868                 }
2869               else
2870                 {
2871                   if (bp_err_message == NULL)
2872                     {
2873                       char *message
2874                         = memory_error_message (TARGET_XFER_E_IO,
2875                                                 bl->gdbarch, bl->address);
2876                       struct cleanup *old_chain = make_cleanup (xfree, message);
2877
2878                       fprintf_unfiltered (tmp_error_stream,
2879                                           "Cannot insert breakpoint %d.\n"
2880                                           "%s\n",
2881                                           bl->owner->number, message);
2882                       do_cleanups (old_chain);
2883                     }
2884                   else
2885                     {
2886                       fprintf_unfiltered (tmp_error_stream,
2887                                           "Cannot insert breakpoint %d: %s\n",
2888                                           bl->owner->number,
2889                                           bp_err_message);
2890                     }
2891                 }
2892               return 1;
2893
2894             }
2895         }
2896       else
2897         bl->inserted = 1;
2898
2899       return 0;
2900     }
2901
2902   else if (bl->loc_type == bp_loc_hardware_watchpoint
2903            /* NOTE drow/2003-09-08: This state only exists for removing
2904               watchpoints.  It's not clear that it's necessary...  */
2905            && bl->owner->disposition != disp_del_at_next_stop)
2906     {
2907       int val;
2908
2909       gdb_assert (bl->owner->ops != NULL
2910                   && bl->owner->ops->insert_location != NULL);
2911
2912       val = bl->owner->ops->insert_location (bl);
2913
2914       /* If trying to set a read-watchpoint, and it turns out it's not
2915          supported, try emulating one with an access watchpoint.  */
2916       if (val == 1 && bl->watchpoint_type == hw_read)
2917         {
2918           struct bp_location *loc, **loc_temp;
2919
2920           /* But don't try to insert it, if there's already another
2921              hw_access location that would be considered a duplicate
2922              of this one.  */
2923           ALL_BP_LOCATIONS (loc, loc_temp)
2924             if (loc != bl
2925                 && loc->watchpoint_type == hw_access
2926                 && watchpoint_locations_match (bl, loc))
2927               {
2928                 bl->duplicate = 1;
2929                 bl->inserted = 1;
2930                 bl->target_info = loc->target_info;
2931                 bl->watchpoint_type = hw_access;
2932                 val = 0;
2933                 break;
2934               }
2935
2936           if (val == 1)
2937             {
2938               bl->watchpoint_type = hw_access;
2939               val = bl->owner->ops->insert_location (bl);
2940
2941               if (val)
2942                 /* Back to the original value.  */
2943                 bl->watchpoint_type = hw_read;
2944             }
2945         }
2946
2947       bl->inserted = (val == 0);
2948     }
2949
2950   else if (bl->owner->type == bp_catchpoint)
2951     {
2952       int val;
2953
2954       gdb_assert (bl->owner->ops != NULL
2955                   && bl->owner->ops->insert_location != NULL);
2956
2957       val = bl->owner->ops->insert_location (bl);
2958       if (val)
2959         {
2960           bl->owner->enable_state = bp_disabled;
2961
2962           if (val == 1)
2963             warning (_("\
2964 Error inserting catchpoint %d: Your system does not support this type\n\
2965 of catchpoint."), bl->owner->number);
2966           else
2967             warning (_("Error inserting catchpoint %d."), bl->owner->number);
2968         }
2969
2970       bl->inserted = (val == 0);
2971
2972       /* We've already printed an error message if there was a problem
2973          inserting this catchpoint, and we've disabled the catchpoint,
2974          so just return success.  */
2975       return 0;
2976     }
2977
2978   return 0;
2979 }
2980
2981 /* This function is called when program space PSPACE is about to be
2982    deleted.  It takes care of updating breakpoints to not reference
2983    PSPACE anymore.  */
2984
2985 void
2986 breakpoint_program_space_exit (struct program_space *pspace)
2987 {
2988   struct breakpoint *b, *b_temp;
2989   struct bp_location *loc, **loc_temp;
2990
2991   /* Remove any breakpoint that was set through this program space.  */
2992   ALL_BREAKPOINTS_SAFE (b, b_temp)
2993     {
2994       if (b->pspace == pspace)
2995         delete_breakpoint (b);
2996     }
2997
2998   /* Breakpoints set through other program spaces could have locations
2999      bound to PSPACE as well.  Remove those.  */
3000   ALL_BP_LOCATIONS (loc, loc_temp)
3001     {
3002       struct bp_location *tmp;
3003
3004       if (loc->pspace == pspace)
3005         {
3006           /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL.  */
3007           if (loc->owner->loc == loc)
3008             loc->owner->loc = loc->next;
3009           else
3010             for (tmp = loc->owner->loc; tmp->next != NULL; tmp = tmp->next)
3011               if (tmp->next == loc)
3012                 {
3013                   tmp->next = loc->next;
3014                   break;
3015                 }
3016         }
3017     }
3018
3019   /* Now update the global location list to permanently delete the
3020      removed locations above.  */
3021   update_global_location_list (UGLL_DONT_INSERT);
3022 }
3023
3024 /* Make sure all breakpoints are inserted in inferior.
3025    Throws exception on any error.
3026    A breakpoint that is already inserted won't be inserted
3027    again, so calling this function twice is safe.  */
3028 void
3029 insert_breakpoints (void)
3030 {
3031   struct breakpoint *bpt;
3032
3033   ALL_BREAKPOINTS (bpt)
3034     if (is_hardware_watchpoint (bpt))
3035       {
3036         struct watchpoint *w = (struct watchpoint *) bpt;
3037
3038         update_watchpoint (w, 0 /* don't reparse.  */);
3039       }
3040
3041   /* Updating watchpoints creates new locations, so update the global
3042      location list.  Explicitly tell ugll to insert locations and
3043      ignore breakpoints_always_inserted_mode.  */
3044   update_global_location_list (UGLL_INSERT);
3045 }
3046
3047 /* Invoke CALLBACK for each of bp_location.  */
3048
3049 void
3050 iterate_over_bp_locations (walk_bp_location_callback callback)
3051 {
3052   struct bp_location *loc, **loc_tmp;
3053
3054   ALL_BP_LOCATIONS (loc, loc_tmp)
3055     {
3056       callback (loc, NULL);
3057     }
3058 }
3059
3060 /* This is used when we need to synch breakpoint conditions between GDB and the
3061    target.  It is the case with deleting and disabling of breakpoints when using
3062    always-inserted mode.  */
3063
3064 static void
3065 update_inserted_breakpoint_locations (void)
3066 {
3067   struct bp_location *bl, **blp_tmp;
3068   int error_flag = 0;
3069   int val = 0;
3070   int disabled_breaks = 0;
3071   int hw_breakpoint_error = 0;
3072   int hw_bp_details_reported = 0;
3073
3074   struct ui_file *tmp_error_stream = mem_fileopen ();
3075   struct cleanup *cleanups = make_cleanup_ui_file_delete (tmp_error_stream);
3076
3077   /* Explicitly mark the warning -- this will only be printed if
3078      there was an error.  */
3079   fprintf_unfiltered (tmp_error_stream, "Warning:\n");
3080
3081   save_current_space_and_thread ();
3082
3083   ALL_BP_LOCATIONS (bl, blp_tmp)
3084     {
3085       /* We only want to update software breakpoints and hardware
3086          breakpoints.  */
3087       if (!is_breakpoint (bl->owner))
3088         continue;
3089
3090       /* We only want to update locations that are already inserted
3091          and need updating.  This is to avoid unwanted insertion during
3092          deletion of breakpoints.  */
3093       if (!bl->inserted || (bl->inserted && !bl->needs_update))
3094         continue;
3095
3096       switch_to_program_space_and_thread (bl->pspace);
3097
3098       /* For targets that support global breakpoints, there's no need
3099          to select an inferior to insert breakpoint to.  In fact, even
3100          if we aren't attached to any process yet, we should still
3101          insert breakpoints.  */
3102       if (!gdbarch_has_global_breakpoints (target_gdbarch ())
3103           && ptid_equal (inferior_ptid, null_ptid))
3104         continue;
3105
3106       val = insert_bp_location (bl, tmp_error_stream, &disabled_breaks,
3107                                     &hw_breakpoint_error, &hw_bp_details_reported);
3108       if (val)
3109         error_flag = val;
3110     }
3111
3112   if (error_flag)
3113     {
3114       target_terminal_ours_for_output ();
3115       error_stream (tmp_error_stream);
3116     }
3117
3118   do_cleanups (cleanups);
3119 }
3120
3121 /* Used when starting or continuing the program.  */
3122
3123 static void
3124 insert_breakpoint_locations (void)
3125 {
3126   struct breakpoint *bpt;
3127   struct bp_location *bl, **blp_tmp;
3128   int error_flag = 0;
3129   int val = 0;
3130   int disabled_breaks = 0;
3131   int hw_breakpoint_error = 0;
3132   int hw_bp_error_explained_already = 0;
3133
3134   struct ui_file *tmp_error_stream = mem_fileopen ();
3135   struct cleanup *cleanups = make_cleanup_ui_file_delete (tmp_error_stream);
3136   
3137   /* Explicitly mark the warning -- this will only be printed if
3138      there was an error.  */
3139   fprintf_unfiltered (tmp_error_stream, "Warning:\n");
3140
3141   save_current_space_and_thread ();
3142
3143   ALL_BP_LOCATIONS (bl, blp_tmp)
3144     {
3145       if (!should_be_inserted (bl) || (bl->inserted && !bl->needs_update))
3146         continue;
3147
3148       /* There is no point inserting thread-specific breakpoints if
3149          the thread no longer exists.  ALL_BP_LOCATIONS bp_location
3150          has BL->OWNER always non-NULL.  */
3151       if (bl->owner->thread != -1
3152           && !valid_global_thread_id (bl->owner->thread))
3153         continue;
3154
3155       switch_to_program_space_and_thread (bl->pspace);
3156
3157       /* For targets that support global breakpoints, there's no need
3158          to select an inferior to insert breakpoint to.  In fact, even
3159          if we aren't attached to any process yet, we should still
3160          insert breakpoints.  */
3161       if (!gdbarch_has_global_breakpoints (target_gdbarch ())
3162           && ptid_equal (inferior_ptid, null_ptid))
3163         continue;
3164
3165       val = insert_bp_location (bl, tmp_error_stream, &disabled_breaks,
3166                                     &hw_breakpoint_error, &hw_bp_error_explained_already);
3167       if (val)
3168         error_flag = val;
3169     }
3170
3171   /* If we failed to insert all locations of a watchpoint, remove
3172      them, as half-inserted watchpoint is of limited use.  */
3173   ALL_BREAKPOINTS (bpt)  
3174     {
3175       int some_failed = 0;
3176       struct bp_location *loc;
3177
3178       if (!is_hardware_watchpoint (bpt))
3179         continue;
3180
3181       if (!breakpoint_enabled (bpt))
3182         continue;
3183
3184       if (bpt->disposition == disp_del_at_next_stop)
3185         continue;
3186       
3187       for (loc = bpt->loc; loc; loc = loc->next)
3188         if (!loc->inserted && should_be_inserted (loc))
3189           {
3190             some_failed = 1;
3191             break;
3192           }
3193       if (some_failed)
3194         {
3195           for (loc = bpt->loc; loc; loc = loc->next)
3196             if (loc->inserted)
3197               remove_breakpoint (loc);
3198
3199           hw_breakpoint_error = 1;
3200           fprintf_unfiltered (tmp_error_stream,
3201                               "Could not insert hardware watchpoint %d.\n", 
3202                               bpt->number);
3203           error_flag = -1;
3204         }
3205     }
3206
3207   if (error_flag)
3208     {
3209       /* If a hardware breakpoint or watchpoint was inserted, add a
3210          message about possibly exhausted resources.  */
3211       if (hw_breakpoint_error && !hw_bp_error_explained_already)
3212         {
3213           fprintf_unfiltered (tmp_error_stream, 
3214                               "Could not insert hardware breakpoints:\n\
3215 You may have requested too many hardware breakpoints/watchpoints.\n");
3216         }
3217       target_terminal_ours_for_output ();
3218       error_stream (tmp_error_stream);
3219     }
3220
3221   do_cleanups (cleanups);
3222 }
3223
3224 /* Used when the program stops.
3225    Returns zero if successful, or non-zero if there was a problem
3226    removing a breakpoint location.  */
3227
3228 int
3229 remove_breakpoints (void)
3230 {
3231   struct bp_location *bl, **blp_tmp;
3232   int val = 0;
3233
3234   ALL_BP_LOCATIONS (bl, blp_tmp)
3235   {
3236     if (bl->inserted && !is_tracepoint (bl->owner))
3237       val |= remove_breakpoint (bl);
3238   }
3239   return val;
3240 }
3241
3242 /* When a thread exits, remove breakpoints that are related to
3243    that thread.  */
3244
3245 static void
3246 remove_threaded_breakpoints (struct thread_info *tp, int silent)
3247 {
3248   struct breakpoint *b, *b_tmp;
3249
3250   ALL_BREAKPOINTS_SAFE (b, b_tmp)
3251     {
3252       if (b->thread == tp->global_num && user_breakpoint_p (b))
3253         {
3254           b->disposition = disp_del_at_next_stop;
3255
3256           printf_filtered (_("\
3257 Thread-specific breakpoint %d deleted - thread %s no longer in the thread list.\n"),
3258                            b->number, print_thread_id (tp));
3259
3260           /* Hide it from the user.  */
3261           b->number = 0;
3262        }
3263     }
3264 }
3265
3266 /* Remove breakpoints of process PID.  */
3267
3268 int
3269 remove_breakpoints_pid (int pid)
3270 {
3271   struct bp_location *bl, **blp_tmp;
3272   int val;
3273   struct inferior *inf = find_inferior_pid (pid);
3274
3275   ALL_BP_LOCATIONS (bl, blp_tmp)
3276   {
3277     if (bl->pspace != inf->pspace)
3278       continue;
3279
3280     if (bl->inserted && !bl->target_info.persist)
3281       {
3282         val = remove_breakpoint (bl);
3283         if (val != 0)
3284           return val;
3285       }
3286   }
3287   return 0;
3288 }
3289
3290 int
3291 reattach_breakpoints (int pid)
3292 {
3293   struct cleanup *old_chain;
3294   struct bp_location *bl, **blp_tmp;
3295   int val;
3296   struct ui_file *tmp_error_stream;
3297   int dummy1 = 0, dummy2 = 0, dummy3 = 0;
3298   struct inferior *inf;
3299   struct thread_info *tp;
3300
3301   tp = any_live_thread_of_process (pid);
3302   if (tp == NULL)
3303     return 1;
3304
3305   inf = find_inferior_pid (pid);
3306   old_chain = save_inferior_ptid ();
3307
3308   inferior_ptid = tp->ptid;
3309
3310   tmp_error_stream = mem_fileopen ();
3311   make_cleanup_ui_file_delete (tmp_error_stream);
3312
3313   ALL_BP_LOCATIONS (bl, blp_tmp)
3314   {
3315     if (bl->pspace != inf->pspace)
3316       continue;
3317
3318     if (bl->inserted)
3319       {
3320         bl->inserted = 0;
3321         val = insert_bp_location (bl, tmp_error_stream, &dummy1, &dummy2, &dummy3);
3322         if (val != 0)
3323           {
3324             do_cleanups (old_chain);
3325             return val;
3326           }
3327       }
3328   }
3329   do_cleanups (old_chain);
3330   return 0;
3331 }
3332
3333 static int internal_breakpoint_number = -1;
3334
3335 /* Set the breakpoint number of B, depending on the value of INTERNAL.
3336    If INTERNAL is non-zero, the breakpoint number will be populated
3337    from internal_breakpoint_number and that variable decremented.
3338    Otherwise the breakpoint number will be populated from
3339    breakpoint_count and that value incremented.  Internal breakpoints
3340    do not set the internal var bpnum.  */
3341 static void
3342 set_breakpoint_number (int internal, struct breakpoint *b)
3343 {
3344   if (internal)
3345     b->number = internal_breakpoint_number--;
3346   else
3347     {
3348       set_breakpoint_count (breakpoint_count + 1);
3349       b->number = breakpoint_count;
3350     }
3351 }
3352
3353 static struct breakpoint *
3354 create_internal_breakpoint (struct gdbarch *gdbarch,
3355                             CORE_ADDR address, enum bptype type,
3356                             const struct breakpoint_ops *ops)
3357 {
3358   struct symtab_and_line sal;
3359   struct breakpoint *b;
3360
3361   init_sal (&sal);              /* Initialize to zeroes.  */
3362
3363   sal.pc = address;
3364   sal.section = find_pc_overlay (sal.pc);
3365   sal.pspace = current_program_space;
3366
3367   b = set_raw_breakpoint (gdbarch, sal, type, ops);
3368   b->number = internal_breakpoint_number--;
3369   b->disposition = disp_donttouch;
3370
3371   return b;
3372 }
3373
3374 static const char *const longjmp_names[] =
3375   {
3376     "longjmp", "_longjmp", "siglongjmp", "_siglongjmp"
3377   };
3378 #define NUM_LONGJMP_NAMES ARRAY_SIZE(longjmp_names)
3379
3380 /* Per-objfile data private to breakpoint.c.  */
3381 struct breakpoint_objfile_data
3382 {
3383   /* Minimal symbol for "_ovly_debug_event" (if any).  */
3384   struct bound_minimal_symbol overlay_msym;
3385
3386   /* Minimal symbol(s) for "longjmp", "siglongjmp", etc. (if any).  */
3387   struct bound_minimal_symbol longjmp_msym[NUM_LONGJMP_NAMES];
3388
3389   /* True if we have looked for longjmp probes.  */
3390   int longjmp_searched;
3391
3392   /* SystemTap probe points for longjmp (if any).  */
3393   VEC (probe_p) *longjmp_probes;
3394
3395   /* Minimal symbol for "std::terminate()" (if any).  */
3396   struct bound_minimal_symbol terminate_msym;
3397
3398   /* Minimal symbol for "_Unwind_DebugHook" (if any).  */
3399   struct bound_minimal_symbol exception_msym;
3400
3401   /* True if we have looked for exception probes.  */
3402   int exception_searched;
3403
3404   /* SystemTap probe points for unwinding (if any).  */
3405   VEC (probe_p) *exception_probes;
3406 };
3407
3408 static const struct objfile_data *breakpoint_objfile_key;
3409
3410 /* Minimal symbol not found sentinel.  */
3411 static struct minimal_symbol msym_not_found;
3412
3413 /* Returns TRUE if MSYM point to the "not found" sentinel.  */
3414
3415 static int
3416 msym_not_found_p (const struct minimal_symbol *msym)
3417 {
3418   return msym == &msym_not_found;
3419 }
3420
3421 /* Return per-objfile data needed by breakpoint.c.
3422    Allocate the data if necessary.  */
3423
3424 static struct breakpoint_objfile_data *
3425 get_breakpoint_objfile_data (struct objfile *objfile)
3426 {
3427   struct breakpoint_objfile_data *bp_objfile_data;
3428
3429   bp_objfile_data = ((struct breakpoint_objfile_data *)
3430                      objfile_data (objfile, breakpoint_objfile_key));
3431   if (bp_objfile_data == NULL)
3432     {
3433       bp_objfile_data =
3434         XOBNEW (&objfile->objfile_obstack, struct breakpoint_objfile_data);
3435
3436       memset (bp_objfile_data, 0, sizeof (*bp_objfile_data));
3437       set_objfile_data (objfile, breakpoint_objfile_key, bp_objfile_data);
3438     }
3439   return bp_objfile_data;
3440 }
3441
3442 static void
3443 free_breakpoint_probes (struct objfile *obj, void *data)
3444 {
3445   struct breakpoint_objfile_data *bp_objfile_data
3446     = (struct breakpoint_objfile_data *) data;
3447
3448   VEC_free (probe_p, bp_objfile_data->longjmp_probes);
3449   VEC_free (probe_p, bp_objfile_data->exception_probes);
3450 }
3451
3452 static void
3453 create_overlay_event_breakpoint (void)
3454 {
3455   struct objfile *objfile;
3456   const char *const func_name = "_ovly_debug_event";
3457
3458   ALL_OBJFILES (objfile)
3459     {
3460       struct breakpoint *b;
3461       struct breakpoint_objfile_data *bp_objfile_data;
3462       CORE_ADDR addr;
3463       struct explicit_location explicit_loc;
3464
3465       bp_objfile_data = get_breakpoint_objfile_data (objfile);
3466
3467       if (msym_not_found_p (bp_objfile_data->overlay_msym.minsym))
3468         continue;
3469
3470       if (bp_objfile_data->overlay_msym.minsym == NULL)
3471         {
3472           struct bound_minimal_symbol m;
3473
3474           m = lookup_minimal_symbol_text (func_name, objfile);
3475           if (m.minsym == NULL)
3476             {
3477               /* Avoid future lookups in this objfile.  */
3478               bp_objfile_data->overlay_msym.minsym = &msym_not_found;
3479               continue;
3480             }
3481           bp_objfile_data->overlay_msym = m;
3482         }
3483
3484       addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->overlay_msym);
3485       b = create_internal_breakpoint (get_objfile_arch (objfile), addr,
3486                                       bp_overlay_event,
3487                                       &internal_breakpoint_ops);
3488       initialize_explicit_location (&explicit_loc);
3489       explicit_loc.function_name = ASTRDUP (func_name);
3490       b->location = new_explicit_location (&explicit_loc);
3491
3492       if (overlay_debugging == ovly_auto)
3493         {
3494           b->enable_state = bp_enabled;
3495           overlay_events_enabled = 1;
3496         }
3497       else
3498        {
3499          b->enable_state = bp_disabled;
3500          overlay_events_enabled = 0;
3501        }
3502     }
3503 }
3504
3505 static void
3506 create_longjmp_master_breakpoint (void)
3507 {
3508   struct program_space *pspace;
3509   struct cleanup *old_chain;
3510
3511   old_chain = save_current_program_space ();
3512
3513   ALL_PSPACES (pspace)
3514   {
3515     struct objfile *objfile;
3516
3517     set_current_program_space (pspace);
3518
3519     ALL_OBJFILES (objfile)
3520     {
3521       int i;
3522       struct gdbarch *gdbarch;
3523       struct breakpoint_objfile_data *bp_objfile_data;
3524
3525       gdbarch = get_objfile_arch (objfile);
3526
3527       bp_objfile_data = get_breakpoint_objfile_data (objfile);
3528
3529       if (!bp_objfile_data->longjmp_searched)
3530         {
3531           VEC (probe_p) *ret;
3532
3533           ret = find_probes_in_objfile (objfile, "libc", "longjmp");
3534           if (ret != NULL)
3535             {
3536               /* We are only interested in checking one element.  */
3537               struct probe *p = VEC_index (probe_p, ret, 0);
3538
3539               if (!can_evaluate_probe_arguments (p))
3540                 {
3541                   /* We cannot use the probe interface here, because it does
3542                      not know how to evaluate arguments.  */
3543                   VEC_free (probe_p, ret);
3544                   ret = NULL;
3545                 }
3546             }
3547           bp_objfile_data->longjmp_probes = ret;
3548           bp_objfile_data->longjmp_searched = 1;
3549         }
3550
3551       if (bp_objfile_data->longjmp_probes != NULL)
3552         {
3553           int i;
3554           struct probe *probe;
3555           struct gdbarch *gdbarch = get_objfile_arch (objfile);
3556
3557           for (i = 0;
3558                VEC_iterate (probe_p,
3559                             bp_objfile_data->longjmp_probes,
3560                             i, probe);
3561                ++i)
3562             {
3563               struct breakpoint *b;
3564
3565               b = create_internal_breakpoint (gdbarch,
3566                                               get_probe_address (probe,
3567                                                                  objfile),
3568                                               bp_longjmp_master,
3569                                               &internal_breakpoint_ops);
3570               b->location
3571                 = new_probe_location ("-probe-stap libc:longjmp");
3572               b->enable_state = bp_disabled;
3573             }
3574
3575           continue;
3576         }
3577
3578       if (!gdbarch_get_longjmp_target_p (gdbarch))
3579         continue;
3580
3581       for (i = 0; i < NUM_LONGJMP_NAMES; i++)
3582         {
3583           struct breakpoint *b;
3584           const char *func_name;
3585           CORE_ADDR addr;
3586           struct explicit_location explicit_loc;
3587
3588           if (msym_not_found_p (bp_objfile_data->longjmp_msym[i].minsym))
3589             continue;
3590
3591           func_name = longjmp_names[i];
3592           if (bp_objfile_data->longjmp_msym[i].minsym == NULL)
3593             {
3594               struct bound_minimal_symbol m;
3595
3596               m = lookup_minimal_symbol_text (func_name, objfile);
3597               if (m.minsym == NULL)
3598                 {
3599                   /* Prevent future lookups in this objfile.  */
3600                   bp_objfile_data->longjmp_msym[i].minsym = &msym_not_found;
3601                   continue;
3602                 }
3603               bp_objfile_data->longjmp_msym[i] = m;
3604             }
3605
3606           addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->longjmp_msym[i]);
3607           b = create_internal_breakpoint (gdbarch, addr, bp_longjmp_master,
3608                                           &internal_breakpoint_ops);
3609           initialize_explicit_location (&explicit_loc);
3610           explicit_loc.function_name = ASTRDUP (func_name);
3611           b->location = new_explicit_location (&explicit_loc);
3612           b->enable_state = bp_disabled;
3613         }
3614     }
3615   }
3616
3617   do_cleanups (old_chain);
3618 }
3619
3620 /* Create a master std::terminate breakpoint.  */
3621 static void
3622 create_std_terminate_master_breakpoint (void)
3623 {
3624   struct program_space *pspace;
3625   struct cleanup *old_chain;
3626   const char *const func_name = "std::terminate()";
3627
3628   old_chain = save_current_program_space ();
3629
3630   ALL_PSPACES (pspace)
3631   {
3632     struct objfile *objfile;
3633     CORE_ADDR addr;
3634
3635     set_current_program_space (pspace);
3636
3637     ALL_OBJFILES (objfile)
3638     {
3639       struct breakpoint *b;
3640       struct breakpoint_objfile_data *bp_objfile_data;
3641       struct explicit_location explicit_loc;
3642
3643       bp_objfile_data = get_breakpoint_objfile_data (objfile);
3644
3645       if (msym_not_found_p (bp_objfile_data->terminate_msym.minsym))
3646         continue;
3647
3648       if (bp_objfile_data->terminate_msym.minsym == NULL)
3649         {
3650           struct bound_minimal_symbol m;
3651
3652           m = lookup_minimal_symbol (func_name, NULL, objfile);
3653           if (m.minsym == NULL || (MSYMBOL_TYPE (m.minsym) != mst_text
3654                                    && MSYMBOL_TYPE (m.minsym) != mst_file_text))
3655             {
3656               /* Prevent future lookups in this objfile.  */
3657               bp_objfile_data->terminate_msym.minsym = &msym_not_found;
3658               continue;
3659             }
3660           bp_objfile_data->terminate_msym = m;
3661         }
3662
3663       addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->terminate_msym);
3664       b = create_internal_breakpoint (get_objfile_arch (objfile), addr,
3665                                       bp_std_terminate_master,
3666                                       &internal_breakpoint_ops);
3667       initialize_explicit_location (&explicit_loc);
3668       explicit_loc.function_name = ASTRDUP (func_name);
3669       b->location = new_explicit_location (&explicit_loc);
3670       b->enable_state = bp_disabled;
3671     }
3672   }
3673
3674   do_cleanups (old_chain);
3675 }
3676
3677 /* Install a master breakpoint on the unwinder's debug hook.  */
3678
3679 static void
3680 create_exception_master_breakpoint (void)
3681 {
3682   struct objfile *objfile;
3683   const char *const func_name = "_Unwind_DebugHook";
3684
3685   ALL_OBJFILES (objfile)
3686     {
3687       struct breakpoint *b;
3688       struct gdbarch *gdbarch;
3689       struct breakpoint_objfile_data *bp_objfile_data;
3690       CORE_ADDR addr;
3691       struct explicit_location explicit_loc;
3692
3693       bp_objfile_data = get_breakpoint_objfile_data (objfile);
3694
3695       /* We prefer the SystemTap probe point if it exists.  */
3696       if (!bp_objfile_data->exception_searched)
3697         {
3698           VEC (probe_p) *ret;
3699
3700           ret = find_probes_in_objfile (objfile, "libgcc", "unwind");
3701
3702           if (ret != NULL)
3703             {
3704               /* We are only interested in checking one element.  */
3705               struct probe *p = VEC_index (probe_p, ret, 0);
3706
3707               if (!can_evaluate_probe_arguments (p))
3708                 {
3709                   /* We cannot use the probe interface here, because it does
3710                      not know how to evaluate arguments.  */
3711                   VEC_free (probe_p, ret);
3712                   ret = NULL;
3713                 }
3714             }
3715           bp_objfile_data->exception_probes = ret;
3716           bp_objfile_data->exception_searched = 1;
3717         }
3718
3719       if (bp_objfile_data->exception_probes != NULL)
3720         {
3721           struct gdbarch *gdbarch = get_objfile_arch (objfile);
3722           int i;
3723           struct probe *probe;
3724
3725           for (i = 0;
3726                VEC_iterate (probe_p,
3727                             bp_objfile_data->exception_probes,
3728                             i, probe);
3729                ++i)
3730             {
3731               struct breakpoint *b;
3732
3733               b = create_internal_breakpoint (gdbarch,
3734                                               get_probe_address (probe,
3735                                                                  objfile),
3736                                               bp_exception_master,
3737                                               &internal_breakpoint_ops);
3738               b->location
3739                 = new_probe_location ("-probe-stap libgcc:unwind");
3740               b->enable_state = bp_disabled;
3741             }
3742
3743           continue;
3744         }
3745
3746       /* Otherwise, try the hook function.  */
3747
3748       if (msym_not_found_p (bp_objfile_data->exception_msym.minsym))
3749         continue;
3750
3751       gdbarch = get_objfile_arch (objfile);
3752
3753       if (bp_objfile_data->exception_msym.minsym == NULL)
3754         {
3755           struct bound_minimal_symbol debug_hook;
3756
3757           debug_hook = lookup_minimal_symbol (func_name, NULL, objfile);
3758           if (debug_hook.minsym == NULL)
3759             {
3760               bp_objfile_data->exception_msym.minsym = &msym_not_found;
3761               continue;
3762             }
3763
3764           bp_objfile_data->exception_msym = debug_hook;
3765         }
3766
3767       addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->exception_msym);
3768       addr = gdbarch_convert_from_func_ptr_addr (gdbarch, addr,
3769                                                  &current_target);
3770       b = create_internal_breakpoint (gdbarch, addr, bp_exception_master,
3771                                       &internal_breakpoint_ops);
3772       initialize_explicit_location (&explicit_loc);
3773       explicit_loc.function_name = ASTRDUP (func_name);
3774       b->location = new_explicit_location (&explicit_loc);
3775       b->enable_state = bp_disabled;
3776     }
3777 }
3778
3779 /* Does B have a location spec?  */
3780
3781 static int
3782 breakpoint_event_location_empty_p (const struct breakpoint *b)
3783 {
3784   return b->location != NULL && event_location_empty_p (b->location);
3785 }
3786
3787 void
3788 update_breakpoints_after_exec (void)
3789 {
3790   struct breakpoint *b, *b_tmp;
3791   struct bp_location *bploc, **bplocp_tmp;
3792
3793   /* We're about to delete breakpoints from GDB's lists.  If the
3794      INSERTED flag is true, GDB will try to lift the breakpoints by
3795      writing the breakpoints' "shadow contents" back into memory.  The
3796      "shadow contents" are NOT valid after an exec, so GDB should not
3797      do that.  Instead, the target is responsible from marking
3798      breakpoints out as soon as it detects an exec.  We don't do that
3799      here instead, because there may be other attempts to delete
3800      breakpoints after detecting an exec and before reaching here.  */
3801   ALL_BP_LOCATIONS (bploc, bplocp_tmp)
3802     if (bploc->pspace == current_program_space)
3803       gdb_assert (!bploc->inserted);
3804
3805   ALL_BREAKPOINTS_SAFE (b, b_tmp)
3806   {
3807     if (b->pspace != current_program_space)
3808       continue;
3809
3810     /* Solib breakpoints must be explicitly reset after an exec().  */
3811     if (b->type == bp_shlib_event)
3812       {
3813         delete_breakpoint (b);
3814         continue;
3815       }
3816
3817     /* JIT breakpoints must be explicitly reset after an exec().  */
3818     if (b->type == bp_jit_event)
3819       {
3820         delete_breakpoint (b);
3821         continue;
3822       }
3823
3824     /* Thread event breakpoints must be set anew after an exec(),
3825        as must overlay event and longjmp master breakpoints.  */
3826     if (b->type == bp_thread_event || b->type == bp_overlay_event
3827         || b->type == bp_longjmp_master || b->type == bp_std_terminate_master
3828         || b->type == bp_exception_master)
3829       {
3830         delete_breakpoint (b);
3831         continue;
3832       }
3833
3834     /* Step-resume breakpoints are meaningless after an exec().  */
3835     if (b->type == bp_step_resume || b->type == bp_hp_step_resume)
3836       {
3837         delete_breakpoint (b);
3838         continue;
3839       }
3840
3841     /* Just like single-step breakpoints.  */
3842     if (b->type == bp_single_step)
3843       {
3844         delete_breakpoint (b);
3845         continue;
3846       }
3847
3848     /* Longjmp and longjmp-resume breakpoints are also meaningless
3849        after an exec.  */
3850     if (b->type == bp_longjmp || b->type == bp_longjmp_resume
3851         || b->type == bp_longjmp_call_dummy
3852         || b->type == bp_exception || b->type == bp_exception_resume)
3853       {
3854         delete_breakpoint (b);
3855         continue;
3856       }
3857
3858     if (b->type == bp_catchpoint)
3859       {
3860         /* For now, none of the bp_catchpoint breakpoints need to
3861            do anything at this point.  In the future, if some of
3862            the catchpoints need to something, we will need to add
3863            a new method, and call this method from here.  */
3864         continue;
3865       }
3866
3867     /* bp_finish is a special case.  The only way we ought to be able
3868        to see one of these when an exec() has happened, is if the user
3869        caught a vfork, and then said "finish".  Ordinarily a finish just
3870        carries them to the call-site of the current callee, by setting
3871        a temporary bp there and resuming.  But in this case, the finish
3872        will carry them entirely through the vfork & exec.
3873
3874        We don't want to allow a bp_finish to remain inserted now.  But
3875        we can't safely delete it, 'cause finish_command has a handle to
3876        the bp on a bpstat, and will later want to delete it.  There's a
3877        chance (and I've seen it happen) that if we delete the bp_finish
3878        here, that its storage will get reused by the time finish_command
3879        gets 'round to deleting the "use to be a bp_finish" breakpoint.
3880        We really must allow finish_command to delete a bp_finish.
3881
3882        In the absence of a general solution for the "how do we know
3883        it's safe to delete something others may have handles to?"
3884        problem, what we'll do here is just uninsert the bp_finish, and
3885        let finish_command delete it.
3886
3887        (We know the bp_finish is "doomed" in the sense that it's
3888        momentary, and will be deleted as soon as finish_command sees
3889        the inferior stopped.  So it doesn't matter that the bp's
3890        address is probably bogus in the new a.out, unlike e.g., the
3891        solib breakpoints.)  */
3892
3893     if (b->type == bp_finish)
3894       {
3895         continue;
3896       }
3897
3898     /* Without a symbolic address, we have little hope of the
3899        pre-exec() address meaning the same thing in the post-exec()
3900        a.out.  */
3901     if (breakpoint_event_location_empty_p (b))
3902       {
3903         delete_breakpoint (b);
3904         continue;
3905       }
3906   }
3907 }
3908
3909 int
3910 detach_breakpoints (ptid_t ptid)
3911 {
3912   struct bp_location *bl, **blp_tmp;
3913   int val = 0;
3914   struct cleanup *old_chain = save_inferior_ptid ();
3915   struct inferior *inf = current_inferior ();
3916
3917   if (ptid_get_pid (ptid) == ptid_get_pid (inferior_ptid))
3918     error (_("Cannot detach breakpoints of inferior_ptid"));
3919
3920   /* Set inferior_ptid; remove_breakpoint_1 uses this global.  */
3921   inferior_ptid = ptid;
3922   ALL_BP_LOCATIONS (bl, blp_tmp)
3923   {
3924     if (bl->pspace != inf->pspace)
3925       continue;
3926
3927     /* This function must physically remove breakpoints locations
3928        from the specified ptid, without modifying the breakpoint
3929        package's state.  Locations of type bp_loc_other are only
3930        maintained at GDB side.  So, there is no need to remove
3931        these bp_loc_other locations.  Moreover, removing these
3932        would modify the breakpoint package's state.  */
3933     if (bl->loc_type == bp_loc_other)
3934       continue;
3935
3936     if (bl->inserted)
3937       val |= remove_breakpoint_1 (bl, DETACH_BREAKPOINT);
3938   }
3939
3940   do_cleanups (old_chain);
3941   return val;
3942 }
3943
3944 /* Remove the breakpoint location BL from the current address space.
3945    Note that this is used to detach breakpoints from a child fork.
3946    When we get here, the child isn't in the inferior list, and neither
3947    do we have objects to represent its address space --- we should
3948    *not* look at bl->pspace->aspace here.  */
3949
3950 static int
3951 remove_breakpoint_1 (struct bp_location *bl, enum remove_bp_reason reason)
3952 {
3953   int val;
3954
3955   /* BL is never in moribund_locations by our callers.  */
3956   gdb_assert (bl->owner != NULL);
3957
3958   /* The type of none suggests that owner is actually deleted.
3959      This should not ever happen.  */
3960   gdb_assert (bl->owner->type != bp_none);
3961
3962   if (bl->loc_type == bp_loc_software_breakpoint
3963       || bl->loc_type == bp_loc_hardware_breakpoint)
3964     {
3965       /* "Normal" instruction breakpoint: either the standard
3966          trap-instruction bp (bp_breakpoint), or a
3967          bp_hardware_breakpoint.  */
3968
3969       /* First check to see if we have to handle an overlay.  */
3970       if (overlay_debugging == ovly_off
3971           || bl->section == NULL
3972           || !(section_is_overlay (bl->section)))
3973         {
3974           /* No overlay handling: just remove the breakpoint.  */
3975
3976           /* If we're trying to uninsert a memory breakpoint that we
3977              know is set in a dynamic object that is marked
3978              shlib_disabled, then either the dynamic object was
3979              removed with "remove-symbol-file" or with
3980              "nosharedlibrary".  In the former case, we don't know
3981              whether another dynamic object might have loaded over the
3982              breakpoint's address -- the user might well let us know
3983              about it next with add-symbol-file (the whole point of
3984              add-symbol-file is letting the user manually maintain a
3985              list of dynamically loaded objects).  If we have the
3986              breakpoint's shadow memory, that is, this is a software
3987              breakpoint managed by GDB, check whether the breakpoint
3988              is still inserted in memory, to avoid overwriting wrong
3989              code with stale saved shadow contents.  Note that HW
3990              breakpoints don't have shadow memory, as they're
3991              implemented using a mechanism that is not dependent on
3992              being able to modify the target's memory, and as such
3993              they should always be removed.  */
3994           if (bl->shlib_disabled
3995               && bl->target_info.shadow_len != 0
3996               && !memory_validate_breakpoint (bl->gdbarch, &bl->target_info))
3997             val = 0;
3998           else
3999             val = bl->owner->ops->remove_location (bl, reason);
4000         }
4001       else
4002         {
4003           /* This breakpoint is in an overlay section.
4004              Did we set a breakpoint at the LMA?  */
4005           if (!overlay_events_enabled)
4006               {
4007                 /* Yes -- overlay event support is not active, so we
4008                    should have set a breakpoint at the LMA.  Remove it.  
4009                 */
4010                 /* Ignore any failures: if the LMA is in ROM, we will
4011                    have already warned when we failed to insert it.  */
4012                 if (bl->loc_type == bp_loc_hardware_breakpoint)
4013                   target_remove_hw_breakpoint (bl->gdbarch,
4014                                                &bl->overlay_target_info);
4015                 else
4016                   target_remove_breakpoint (bl->gdbarch,
4017                                             &bl->overlay_target_info,
4018                                             reason);
4019               }
4020           /* Did we set a breakpoint at the VMA? 
4021              If so, we will have marked the breakpoint 'inserted'.  */
4022           if (bl->inserted)
4023             {
4024               /* Yes -- remove it.  Previously we did not bother to
4025                  remove the breakpoint if the section had been
4026                  unmapped, but let's not rely on that being safe.  We
4027                  don't know what the overlay manager might do.  */
4028
4029               /* However, we should remove *software* breakpoints only
4030                  if the section is still mapped, or else we overwrite
4031                  wrong code with the saved shadow contents.  */
4032               if (bl->loc_type == bp_loc_hardware_breakpoint
4033                   || section_is_mapped (bl->section))
4034                 val = bl->owner->ops->remove_location (bl, reason);
4035               else
4036                 val = 0;
4037             }
4038           else
4039             {
4040               /* No -- not inserted, so no need to remove.  No error.  */
4041               val = 0;
4042             }
4043         }
4044
4045       /* In some cases, we might not be able to remove a breakpoint in
4046          a shared library that has already been removed, but we have
4047          not yet processed the shlib unload event.  Similarly for an
4048          unloaded add-symbol-file object - the user might not yet have
4049          had the chance to remove-symbol-file it.  shlib_disabled will
4050          be set if the library/object has already been removed, but
4051          the breakpoint hasn't been uninserted yet, e.g., after
4052          "nosharedlibrary" or "remove-symbol-file" with breakpoints
4053          always-inserted mode.  */
4054       if (val
4055           && (bl->loc_type == bp_loc_software_breakpoint
4056               && (bl->shlib_disabled
4057                   || solib_name_from_address (bl->pspace, bl->address)
4058                   || shared_objfile_contains_address_p (bl->pspace,
4059                                                         bl->address))))
4060         val = 0;
4061
4062       if (val)
4063         return val;
4064       bl->inserted = (reason == DETACH_BREAKPOINT);
4065     }
4066   else if (bl->loc_type == bp_loc_hardware_watchpoint)
4067     {
4068       gdb_assert (bl->owner->ops != NULL
4069                   && bl->owner->ops->remove_location != NULL);
4070
4071       bl->inserted = (reason == DETACH_BREAKPOINT);
4072       bl->owner->ops->remove_location (bl, reason);
4073
4074       /* Failure to remove any of the hardware watchpoints comes here.  */
4075       if (reason == REMOVE_BREAKPOINT && bl->inserted)
4076         warning (_("Could not remove hardware watchpoint %d."),
4077                  bl->owner->number);
4078     }
4079   else if (bl->owner->type == bp_catchpoint
4080            && breakpoint_enabled (bl->owner)
4081            && !bl->duplicate)
4082     {
4083       gdb_assert (bl->owner->ops != NULL
4084                   && bl->owner->ops->remove_location != NULL);
4085
4086       val = bl->owner->ops->remove_location (bl, reason);
4087       if (val)
4088         return val;
4089
4090       bl->inserted = (reason == DETACH_BREAKPOINT);
4091     }
4092
4093   return 0;
4094 }
4095
4096 static int
4097 remove_breakpoint (struct bp_location *bl)
4098 {
4099   int ret;
4100   struct cleanup *old_chain;
4101
4102   /* BL is never in moribund_locations by our callers.  */
4103   gdb_assert (bl->owner != NULL);
4104
4105   /* The type of none suggests that owner is actually deleted.
4106      This should not ever happen.  */
4107   gdb_assert (bl->owner->type != bp_none);
4108
4109   old_chain = save_current_space_and_thread ();
4110
4111   switch_to_program_space_and_thread (bl->pspace);
4112
4113   ret = remove_breakpoint_1 (bl, REMOVE_BREAKPOINT);
4114
4115   do_cleanups (old_chain);
4116   return ret;
4117 }
4118
4119 /* Clear the "inserted" flag in all breakpoints.  */
4120
4121 void
4122 mark_breakpoints_out (void)
4123 {
4124   struct bp_location *bl, **blp_tmp;
4125
4126   ALL_BP_LOCATIONS (bl, blp_tmp)
4127     if (bl->pspace == current_program_space)
4128       bl->inserted = 0;
4129 }
4130
4131 /* Clear the "inserted" flag in all breakpoints and delete any
4132    breakpoints which should go away between runs of the program.
4133
4134    Plus other such housekeeping that has to be done for breakpoints
4135    between runs.
4136
4137    Note: this function gets called at the end of a run (by
4138    generic_mourn_inferior) and when a run begins (by
4139    init_wait_for_inferior).  */
4140
4141
4142
4143 void
4144 breakpoint_init_inferior (enum inf_context context)
4145 {
4146   struct breakpoint *b, *b_tmp;
4147   struct bp_location *bl;
4148   int ix;
4149   struct program_space *pspace = current_program_space;
4150
4151   /* If breakpoint locations are shared across processes, then there's
4152      nothing to do.  */
4153   if (gdbarch_has_global_breakpoints (target_gdbarch ()))
4154     return;
4155
4156   mark_breakpoints_out ();
4157
4158   ALL_BREAKPOINTS_SAFE (b, b_tmp)
4159   {
4160     if (b->loc && b->loc->pspace != pspace)
4161       continue;
4162
4163     switch (b->type)
4164       {
4165       case bp_call_dummy:
4166       case bp_longjmp_call_dummy:
4167
4168         /* If the call dummy breakpoint is at the entry point it will
4169            cause problems when the inferior is rerun, so we better get
4170            rid of it.  */
4171
4172       case bp_watchpoint_scope:
4173
4174         /* Also get rid of scope breakpoints.  */
4175
4176       case bp_shlib_event:
4177
4178         /* Also remove solib event breakpoints.  Their addresses may
4179            have changed since the last time we ran the program.
4180            Actually we may now be debugging against different target;
4181            and so the solib backend that installed this breakpoint may
4182            not be used in by the target.  E.g.,
4183
4184            (gdb) file prog-linux
4185            (gdb) run               # native linux target
4186            ...
4187            (gdb) kill
4188            (gdb) file prog-win.exe
4189            (gdb) tar rem :9999     # remote Windows gdbserver.
4190         */
4191
4192       case bp_step_resume:
4193
4194         /* Also remove step-resume breakpoints.  */
4195
4196       case bp_single_step:
4197
4198         /* Also remove single-step breakpoints.  */
4199
4200         delete_breakpoint (b);
4201         break;
4202
4203       case bp_watchpoint:
4204       case bp_hardware_watchpoint:
4205       case bp_read_watchpoint:
4206       case bp_access_watchpoint:
4207         {
4208           struct watchpoint *w = (struct watchpoint *) b;
4209
4210           /* Likewise for watchpoints on local expressions.  */
4211           if (w->exp_valid_block != NULL)
4212             delete_breakpoint (b);
4213           else
4214             {
4215               /* Get rid of existing locations, which are no longer
4216                  valid.  New ones will be created in
4217                  update_watchpoint, when the inferior is restarted.
4218                  The next update_global_location_list call will
4219                  garbage collect them.  */
4220               b->loc = NULL;
4221
4222               if (context == inf_starting)
4223                 {
4224                   /* Reset val field to force reread of starting value in
4225                      insert_breakpoints.  */
4226                   if (w->val)
4227                     value_free (w->val);
4228                   w->val = NULL;
4229                   w->val_valid = 0;
4230                 }
4231             }
4232         }
4233         break;
4234       default:
4235         break;
4236       }
4237   }
4238
4239   /* Get rid of the moribund locations.  */
4240   for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, bl); ++ix)
4241     decref_bp_location (&bl);
4242   VEC_free (bp_location_p, moribund_locations);
4243 }
4244
4245 /* These functions concern about actual breakpoints inserted in the
4246    target --- to e.g. check if we need to do decr_pc adjustment or if
4247    we need to hop over the bkpt --- so we check for address space
4248    match, not program space.  */
4249
4250 /* breakpoint_here_p (PC) returns non-zero if an enabled breakpoint
4251    exists at PC.  It returns ordinary_breakpoint_here if it's an
4252    ordinary breakpoint, or permanent_breakpoint_here if it's a
4253    permanent breakpoint.
4254    - When continuing from a location with an ordinary breakpoint, we
4255      actually single step once before calling insert_breakpoints.
4256    - When continuing from a location with a permanent breakpoint, we
4257      need to use the `SKIP_PERMANENT_BREAKPOINT' macro, provided by
4258      the target, to advance the PC past the breakpoint.  */
4259
4260 enum breakpoint_here
4261 breakpoint_here_p (struct address_space *aspace, CORE_ADDR pc)
4262 {
4263   struct bp_location *bl, **blp_tmp;
4264   int any_breakpoint_here = 0;
4265
4266   ALL_BP_LOCATIONS (bl, blp_tmp)
4267     {
4268       if (bl->loc_type != bp_loc_software_breakpoint
4269           && bl->loc_type != bp_loc_hardware_breakpoint)
4270         continue;
4271
4272       /* ALL_BP_LOCATIONS bp_location has BL->OWNER always non-NULL.  */
4273       if ((breakpoint_enabled (bl->owner)
4274            || bl->permanent)
4275           && breakpoint_location_address_match (bl, aspace, pc))
4276         {
4277           if (overlay_debugging 
4278               && section_is_overlay (bl->section)
4279               && !section_is_mapped (bl->section))
4280             continue;           /* unmapped overlay -- can't be a match */
4281           else if (bl->permanent)
4282             return permanent_breakpoint_here;
4283           else
4284             any_breakpoint_here = 1;
4285         }
4286     }
4287
4288   return any_breakpoint_here ? ordinary_breakpoint_here : no_breakpoint_here;
4289 }
4290
4291 /* See breakpoint.h.  */
4292
4293 int
4294 breakpoint_in_range_p (struct address_space *aspace,
4295                        CORE_ADDR addr, ULONGEST len)
4296 {
4297   struct bp_location *bl, **blp_tmp;
4298
4299   ALL_BP_LOCATIONS (bl, blp_tmp)
4300     {
4301       if (bl->loc_type != bp_loc_software_breakpoint
4302           && bl->loc_type != bp_loc_hardware_breakpoint)
4303         continue;
4304
4305       if ((breakpoint_enabled (bl->owner)
4306            || bl->permanent)
4307           && breakpoint_location_address_range_overlap (bl, aspace,
4308                                                         addr, len))
4309         {
4310           if (overlay_debugging
4311               && section_is_overlay (bl->section)
4312               && !section_is_mapped (bl->section))
4313             {
4314               /* Unmapped overlay -- can't be a match.  */
4315               continue;
4316             }
4317
4318           return 1;
4319         }
4320     }
4321
4322   return 0;
4323 }
4324
4325 /* Return true if there's a moribund breakpoint at PC.  */
4326
4327 int
4328 moribund_breakpoint_here_p (struct address_space *aspace, CORE_ADDR pc)
4329 {
4330   struct bp_location *loc;
4331   int ix;
4332
4333   for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, loc); ++ix)
4334     if (breakpoint_location_address_match (loc, aspace, pc))
4335       return 1;
4336
4337   return 0;
4338 }
4339
4340 /* Returns non-zero iff BL is inserted at PC, in address space
4341    ASPACE.  */
4342
4343 static int
4344 bp_location_inserted_here_p (struct bp_location *bl,
4345                              struct address_space *aspace, CORE_ADDR pc)
4346 {
4347   if (bl->inserted
4348       && breakpoint_address_match (bl->pspace->aspace, bl->address,
4349                                    aspace, pc))
4350     {
4351       if (overlay_debugging
4352           && section_is_overlay (bl->section)
4353           && !section_is_mapped (bl->section))
4354         return 0;               /* unmapped overlay -- can't be a match */
4355       else
4356         return 1;
4357     }
4358   return 0;
4359 }
4360
4361 /* Returns non-zero iff there's a breakpoint inserted at PC.  */
4362
4363 int
4364 breakpoint_inserted_here_p (struct address_space *aspace, CORE_ADDR pc)
4365 {
4366   struct bp_location **blp, **blp_tmp = NULL;
4367
4368   ALL_BP_LOCATIONS_AT_ADDR (blp, blp_tmp, pc)
4369     {
4370       struct bp_location *bl = *blp;
4371
4372       if (bl->loc_type != bp_loc_software_breakpoint
4373           && bl->loc_type != bp_loc_hardware_breakpoint)
4374         continue;
4375
4376       if (bp_location_inserted_here_p (bl, aspace, pc))
4377         return 1;
4378     }
4379   return 0;
4380 }
4381
4382 /* This function returns non-zero iff there is a software breakpoint
4383    inserted at PC.  */
4384
4385 int
4386 software_breakpoint_inserted_here_p (struct address_space *aspace,
4387                                      CORE_ADDR pc)
4388 {
4389   struct bp_location **blp, **blp_tmp = NULL;
4390
4391   ALL_BP_LOCATIONS_AT_ADDR (blp, blp_tmp, pc)
4392     {
4393       struct bp_location *bl = *blp;
4394
4395       if (bl->loc_type != bp_loc_software_breakpoint)
4396         continue;
4397
4398       if (bp_location_inserted_here_p (bl, aspace, pc))
4399         return 1;
4400     }
4401
4402   return 0;
4403 }
4404
4405 /* See breakpoint.h.  */
4406
4407 int
4408 hardware_breakpoint_inserted_here_p (struct address_space *aspace,
4409                                      CORE_ADDR pc)
4410 {
4411   struct bp_location **blp, **blp_tmp = NULL;
4412
4413   ALL_BP_LOCATIONS_AT_ADDR (blp, blp_tmp, pc)
4414     {
4415       struct bp_location *bl = *blp;
4416
4417       if (bl->loc_type != bp_loc_hardware_breakpoint)
4418         continue;
4419
4420       if (bp_location_inserted_here_p (bl, aspace, pc))
4421         return 1;
4422     }
4423
4424   return 0;
4425 }
4426
4427 int
4428 hardware_watchpoint_inserted_in_range (struct address_space *aspace,
4429                                        CORE_ADDR addr, ULONGEST len)
4430 {
4431   struct breakpoint *bpt;
4432
4433   ALL_BREAKPOINTS (bpt)
4434     {
4435       struct bp_location *loc;
4436
4437       if (bpt->type != bp_hardware_watchpoint
4438           && bpt->type != bp_access_watchpoint)
4439         continue;
4440
4441       if (!breakpoint_enabled (bpt))
4442         continue;
4443
4444       for (loc = bpt->loc; loc; loc = loc->next)
4445         if (loc->pspace->aspace == aspace && loc->inserted)
4446           {
4447             CORE_ADDR l, h;
4448
4449             /* Check for intersection.  */
4450             l = std::max<CORE_ADDR> (loc->address, addr);
4451             h = std::min<CORE_ADDR> (loc->address + loc->length, addr + len);
4452             if (l < h)
4453               return 1;
4454           }
4455     }
4456   return 0;
4457 }
4458 \f
4459
4460 /* bpstat stuff.  External routines' interfaces are documented
4461    in breakpoint.h.  */
4462
4463 int
4464 is_catchpoint (struct breakpoint *ep)
4465 {
4466   return (ep->type == bp_catchpoint);
4467 }
4468
4469 /* Frees any storage that is part of a bpstat.  Does not walk the
4470    'next' chain.  */
4471
4472 static void
4473 bpstat_free (bpstat bs)
4474 {
4475   if (bs->old_val != NULL)
4476     value_free (bs->old_val);
4477   decref_counted_command_line (&bs->commands);
4478   decref_bp_location (&bs->bp_location_at);
4479   xfree (bs);
4480 }
4481
4482 /* Clear a bpstat so that it says we are not at any breakpoint.
4483    Also free any storage that is part of a bpstat.  */
4484
4485 void
4486 bpstat_clear (bpstat *bsp)
4487 {
4488   bpstat p;
4489   bpstat q;
4490
4491   if (bsp == 0)
4492     return;
4493   p = *bsp;
4494   while (p != NULL)
4495     {
4496       q = p->next;
4497       bpstat_free (p);
4498       p = q;
4499     }
4500   *bsp = NULL;
4501 }
4502
4503 /* Return a copy of a bpstat.  Like "bs1 = bs2" but all storage that
4504    is part of the bpstat is copied as well.  */
4505
4506 bpstat
4507 bpstat_copy (bpstat bs)
4508 {
4509   bpstat p = NULL;
4510   bpstat tmp;
4511   bpstat retval = NULL;
4512
4513   if (bs == NULL)
4514     return bs;
4515
4516   for (; bs != NULL; bs = bs->next)
4517     {
4518       tmp = (bpstat) xmalloc (sizeof (*tmp));
4519       memcpy (tmp, bs, sizeof (*tmp));
4520       incref_counted_command_line (tmp->commands);
4521       incref_bp_location (tmp->bp_location_at);
4522       if (bs->old_val != NULL)
4523         {
4524           tmp->old_val = value_copy (bs->old_val);
4525           release_value (tmp->old_val);
4526         }
4527
4528       if (p == NULL)
4529         /* This is the first thing in the chain.  */
4530         retval = tmp;
4531       else
4532         p->next = tmp;
4533       p = tmp;
4534     }
4535   p->next = NULL;
4536   return retval;
4537 }
4538
4539 /* Find the bpstat associated with this breakpoint.  */
4540
4541 bpstat
4542 bpstat_find_breakpoint (bpstat bsp, struct breakpoint *breakpoint)
4543 {
4544   if (bsp == NULL)
4545     return NULL;
4546
4547   for (; bsp != NULL; bsp = bsp->next)
4548     {
4549       if (bsp->breakpoint_at == breakpoint)
4550         return bsp;
4551     }
4552   return NULL;
4553 }
4554
4555 /* See breakpoint.h.  */
4556
4557 int
4558 bpstat_explains_signal (bpstat bsp, enum gdb_signal sig)
4559 {
4560   for (; bsp != NULL; bsp = bsp->next)
4561     {
4562       if (bsp->breakpoint_at == NULL)
4563         {
4564           /* A moribund location can never explain a signal other than
4565              GDB_SIGNAL_TRAP.  */
4566           if (sig == GDB_SIGNAL_TRAP)
4567             return 1;
4568         }
4569       else
4570         {
4571           if (bsp->breakpoint_at->ops->explains_signal (bsp->breakpoint_at,
4572                                                         sig))
4573             return 1;
4574         }
4575     }
4576
4577   return 0;
4578 }
4579
4580 /* Put in *NUM the breakpoint number of the first breakpoint we are
4581    stopped at.  *BSP upon return is a bpstat which points to the
4582    remaining breakpoints stopped at (but which is not guaranteed to be
4583    good for anything but further calls to bpstat_num).
4584
4585    Return 0 if passed a bpstat which does not indicate any breakpoints.
4586    Return -1 if stopped at a breakpoint that has been deleted since
4587    we set it.
4588    Return 1 otherwise.  */
4589
4590 int
4591 bpstat_num (bpstat *bsp, int *num)
4592 {
4593   struct breakpoint *b;
4594
4595   if ((*bsp) == NULL)
4596     return 0;                   /* No more breakpoint values */
4597
4598   /* We assume we'll never have several bpstats that correspond to a
4599      single breakpoint -- otherwise, this function might return the
4600      same number more than once and this will look ugly.  */
4601   b = (*bsp)->breakpoint_at;
4602   *bsp = (*bsp)->next;
4603   if (b == NULL)
4604     return -1;                  /* breakpoint that's been deleted since */
4605
4606   *num = b->number;             /* We have its number */
4607   return 1;
4608 }
4609
4610 /* See breakpoint.h.  */
4611
4612 void
4613 bpstat_clear_actions (void)
4614 {
4615   struct thread_info *tp;
4616   bpstat bs;
4617
4618   if (ptid_equal (inferior_ptid, null_ptid))
4619     return;
4620
4621   tp = find_thread_ptid (inferior_ptid);
4622   if (tp == NULL)
4623     return;
4624
4625   for (bs = tp->control.stop_bpstat; bs != NULL; bs = bs->next)
4626     {
4627       decref_counted_command_line (&bs->commands);
4628
4629       if (bs->old_val != NULL)
4630         {
4631           value_free (bs->old_val);
4632           bs->old_val = NULL;
4633         }
4634     }
4635 }
4636
4637 /* Called when a command is about to proceed the inferior.  */
4638
4639 static void
4640 breakpoint_about_to_proceed (void)
4641 {
4642   if (!ptid_equal (inferior_ptid, null_ptid))
4643     {
4644       struct thread_info *tp = inferior_thread ();
4645
4646       /* Allow inferior function calls in breakpoint commands to not
4647          interrupt the command list.  When the call finishes
4648          successfully, the inferior will be standing at the same
4649          breakpoint as if nothing happened.  */
4650       if (tp->control.in_infcall)
4651         return;
4652     }
4653
4654   breakpoint_proceeded = 1;
4655 }
4656
4657 /* Stub for cleaning up our state if we error-out of a breakpoint
4658    command.  */
4659 static void
4660 cleanup_executing_breakpoints (void *ignore)
4661 {
4662   executing_breakpoint_commands = 0;
4663 }
4664
4665 /* Return non-zero iff CMD as the first line of a command sequence is `silent'
4666    or its equivalent.  */
4667
4668 static int
4669 command_line_is_silent (struct command_line *cmd)
4670 {
4671   return cmd && (strcmp ("silent", cmd->line) == 0);
4672 }
4673
4674 /* Execute all the commands associated with all the breakpoints at
4675    this location.  Any of these commands could cause the process to
4676    proceed beyond this point, etc.  We look out for such changes by
4677    checking the global "breakpoint_proceeded" after each command.
4678
4679    Returns true if a breakpoint command resumed the inferior.  In that
4680    case, it is the caller's responsibility to recall it again with the
4681    bpstat of the current thread.  */
4682
4683 static int
4684 bpstat_do_actions_1 (bpstat *bsp)
4685 {
4686   bpstat bs;
4687   struct cleanup *old_chain;
4688   int again = 0;
4689
4690   /* Avoid endless recursion if a `source' command is contained
4691      in bs->commands.  */
4692   if (executing_breakpoint_commands)
4693     return 0;
4694
4695   executing_breakpoint_commands = 1;
4696   old_chain = make_cleanup (cleanup_executing_breakpoints, 0);
4697
4698   prevent_dont_repeat ();
4699
4700   /* This pointer will iterate over the list of bpstat's.  */
4701   bs = *bsp;
4702
4703   breakpoint_proceeded = 0;
4704   for (; bs != NULL; bs = bs->next)
4705     {
4706       struct counted_command_line *ccmd;
4707       struct command_line *cmd;
4708       struct cleanup *this_cmd_tree_chain;
4709
4710       /* Take ownership of the BSP's command tree, if it has one.
4711
4712          The command tree could legitimately contain commands like
4713          'step' and 'next', which call clear_proceed_status, which
4714          frees stop_bpstat's command tree.  To make sure this doesn't
4715          free the tree we're executing out from under us, we need to
4716          take ownership of the tree ourselves.  Since a given bpstat's
4717          commands are only executed once, we don't need to copy it; we
4718          can clear the pointer in the bpstat, and make sure we free
4719          the tree when we're done.  */
4720       ccmd = bs->commands;
4721       bs->commands = NULL;
4722       this_cmd_tree_chain = make_cleanup_decref_counted_command_line (&ccmd);
4723       cmd = ccmd ? ccmd->commands : NULL;
4724       if (command_line_is_silent (cmd))
4725         {
4726           /* The action has been already done by bpstat_stop_status.  */
4727           cmd = cmd->next;
4728         }
4729
4730       while (cmd != NULL)
4731         {
4732           execute_control_command (cmd);
4733
4734           if (breakpoint_proceeded)
4735             break;
4736           else
4737             cmd = cmd->next;
4738         }
4739
4740       /* We can free this command tree now.  */
4741       do_cleanups (this_cmd_tree_chain);
4742
4743       if (breakpoint_proceeded)
4744         {
4745           if (current_ui->async)
4746             /* If we are in async mode, then the target might be still
4747                running, not stopped at any breakpoint, so nothing for
4748                us to do here -- just return to the event loop.  */
4749             ;
4750           else
4751             /* In sync mode, when execute_control_command returns
4752                we're already standing on the next breakpoint.
4753                Breakpoint commands for that stop were not run, since
4754                execute_command does not run breakpoint commands --
4755                only command_line_handler does, but that one is not
4756                involved in execution of breakpoint commands.  So, we
4757                can now execute breakpoint commands.  It should be
4758                noted that making execute_command do bpstat actions is
4759                not an option -- in this case we'll have recursive
4760                invocation of bpstat for each breakpoint with a
4761                command, and can easily blow up GDB stack.  Instead, we
4762                return true, which will trigger the caller to recall us
4763                with the new stop_bpstat.  */
4764             again = 1;
4765           break;
4766         }
4767     }
4768   do_cleanups (old_chain);
4769   return again;
4770 }
4771
4772 void
4773 bpstat_do_actions (void)
4774 {
4775   struct cleanup *cleanup_if_error = make_bpstat_clear_actions_cleanup ();
4776
4777   /* Do any commands attached to breakpoint we are stopped at.  */
4778   while (!ptid_equal (inferior_ptid, null_ptid)
4779          && target_has_execution
4780          && !is_exited (inferior_ptid)
4781          && !is_executing (inferior_ptid))
4782     /* Since in sync mode, bpstat_do_actions may resume the inferior,
4783        and only return when it is stopped at the next breakpoint, we
4784        keep doing breakpoint actions until it returns false to
4785        indicate the inferior was not resumed.  */
4786     if (!bpstat_do_actions_1 (&inferior_thread ()->control.stop_bpstat))
4787       break;
4788
4789   discard_cleanups (cleanup_if_error);
4790 }
4791
4792 /* Print out the (old or new) value associated with a watchpoint.  */
4793
4794 static void
4795 watchpoint_value_print (struct value *val, struct ui_file *stream)
4796 {
4797   if (val == NULL)
4798     fprintf_unfiltered (stream, _("<unreadable>"));
4799   else
4800     {
4801       struct value_print_options opts;
4802       get_user_print_options (&opts);
4803       value_print (val, stream, &opts);
4804     }
4805 }
4806
4807 /* Print the "Thread ID hit" part of "Thread ID hit Breakpoint N" if
4808    debugging multiple threads.  */
4809
4810 void
4811 maybe_print_thread_hit_breakpoint (struct ui_out *uiout)
4812 {
4813   if (ui_out_is_mi_like_p (uiout))
4814     return;
4815
4816   ui_out_text (uiout, "\n");
4817
4818   if (show_thread_that_caused_stop ())
4819     {
4820       const char *name;
4821       struct thread_info *thr = inferior_thread ();
4822
4823       ui_out_text (uiout, "Thread ");
4824       ui_out_field_fmt (uiout, "thread-id", "%s", print_thread_id (thr));
4825
4826       name = thr->name != NULL ? thr->name : target_thread_name (thr);
4827       if (name != NULL)
4828         {
4829           ui_out_text (uiout, " \"");
4830           ui_out_field_fmt (uiout, "name", "%s", name);
4831           ui_out_text (uiout, "\"");
4832         }
4833
4834       ui_out_text (uiout, " hit ");
4835     }
4836 }
4837
4838 /* Generic routine for printing messages indicating why we
4839    stopped.  The behavior of this function depends on the value
4840    'print_it' in the bpstat structure.  Under some circumstances we
4841    may decide not to print anything here and delegate the task to
4842    normal_stop().  */
4843
4844 static enum print_stop_action
4845 print_bp_stop_message (bpstat bs)
4846 {
4847   switch (bs->print_it)
4848     {
4849     case print_it_noop:
4850       /* Nothing should be printed for this bpstat entry.  */
4851       return PRINT_UNKNOWN;
4852       break;
4853
4854     case print_it_done:
4855       /* We still want to print the frame, but we already printed the
4856          relevant messages.  */
4857       return PRINT_SRC_AND_LOC;
4858       break;
4859
4860     case print_it_normal:
4861       {
4862         struct breakpoint *b = bs->breakpoint_at;
4863
4864         /* bs->breakpoint_at can be NULL if it was a momentary breakpoint
4865            which has since been deleted.  */
4866         if (b == NULL)
4867           return PRINT_UNKNOWN;
4868
4869         /* Normal case.  Call the breakpoint's print_it method.  */
4870         return b->ops->print_it (bs);
4871       }
4872       break;
4873
4874     default:
4875       internal_error (__FILE__, __LINE__,
4876                       _("print_bp_stop_message: unrecognized enum value"));
4877       break;
4878     }
4879 }
4880
4881 /* A helper function that prints a shared library stopped event.  */
4882
4883 static void
4884 print_solib_event (int is_catchpoint)
4885 {
4886   int any_deleted
4887     = !VEC_empty (char_ptr, current_program_space->deleted_solibs);
4888   int any_added
4889     = !VEC_empty (so_list_ptr, current_program_space->added_solibs);
4890
4891   if (!is_catchpoint)
4892     {
4893       if (any_added || any_deleted)
4894         ui_out_text (current_uiout,
4895                      _("Stopped due to shared library event:\n"));
4896       else
4897         ui_out_text (current_uiout,
4898                      _("Stopped due to shared library event (no "
4899                        "libraries added or removed)\n"));
4900     }
4901
4902   if (ui_out_is_mi_like_p (current_uiout))
4903     ui_out_field_string (current_uiout, "reason",
4904                          async_reason_lookup (EXEC_ASYNC_SOLIB_EVENT));
4905
4906   if (any_deleted)
4907     {
4908       struct cleanup *cleanup;
4909       char *name;
4910       int ix;
4911
4912       ui_out_text (current_uiout, _("  Inferior unloaded "));
4913       cleanup = make_cleanup_ui_out_list_begin_end (current_uiout,
4914                                                     "removed");
4915       for (ix = 0;
4916            VEC_iterate (char_ptr, current_program_space->deleted_solibs,
4917                         ix, name);
4918            ++ix)
4919         {
4920           if (ix > 0)
4921             ui_out_text (current_uiout, "    ");
4922           ui_out_field_string (current_uiout, "library", name);
4923           ui_out_text (current_uiout, "\n");
4924         }
4925
4926       do_cleanups (cleanup);
4927     }
4928
4929   if (any_added)
4930     {
4931       struct so_list *iter;
4932       int ix;
4933       struct cleanup *cleanup;
4934
4935       ui_out_text (current_uiout, _("  Inferior loaded "));
4936       cleanup = make_cleanup_ui_out_list_begin_end (current_uiout,
4937                                                     "added");
4938       for (ix = 0;
4939            VEC_iterate (so_list_ptr, current_program_space->added_solibs,
4940                         ix, iter);
4941            ++ix)
4942         {
4943           if (ix > 0)
4944             ui_out_text (current_uiout, "    ");
4945           ui_out_field_string (current_uiout, "library", iter->so_name);
4946           ui_out_text (current_uiout, "\n");
4947         }
4948
4949       do_cleanups (cleanup);
4950     }
4951 }
4952
4953 /* Print a message indicating what happened.  This is called from
4954    normal_stop().  The input to this routine is the head of the bpstat
4955    list - a list of the eventpoints that caused this stop.  KIND is
4956    the target_waitkind for the stopping event.  This
4957    routine calls the generic print routine for printing a message
4958    about reasons for stopping.  This will print (for example) the
4959    "Breakpoint n," part of the output.  The return value of this
4960    routine is one of:
4961
4962    PRINT_UNKNOWN: Means we printed nothing.
4963    PRINT_SRC_AND_LOC: Means we printed something, and expect subsequent
4964    code to print the location.  An example is 
4965    "Breakpoint 1, " which should be followed by
4966    the location.
4967    PRINT_SRC_ONLY: Means we printed something, but there is no need
4968    to also print the location part of the message.
4969    An example is the catch/throw messages, which
4970    don't require a location appended to the end.
4971    PRINT_NOTHING: We have done some printing and we don't need any 
4972    further info to be printed.  */
4973
4974 enum print_stop_action
4975 bpstat_print (bpstat bs, int kind)
4976 {
4977   enum print_stop_action val;
4978
4979   /* Maybe another breakpoint in the chain caused us to stop.
4980      (Currently all watchpoints go on the bpstat whether hit or not.
4981      That probably could (should) be changed, provided care is taken
4982      with respect to bpstat_explains_signal).  */
4983   for (; bs; bs = bs->next)
4984     {
4985       val = print_bp_stop_message (bs);
4986       if (val == PRINT_SRC_ONLY 
4987           || val == PRINT_SRC_AND_LOC 
4988           || val == PRINT_NOTHING)
4989         return val;
4990     }
4991
4992   /* If we had hit a shared library event breakpoint,
4993      print_bp_stop_message would print out this message.  If we hit an
4994      OS-level shared library event, do the same thing.  */
4995   if (kind == TARGET_WAITKIND_LOADED)
4996     {
4997       print_solib_event (0);
4998       return PRINT_NOTHING;
4999     }
5000
5001   /* We reached the end of the chain, or we got a null BS to start
5002      with and nothing was printed.  */
5003   return PRINT_UNKNOWN;
5004 }
5005
5006 /* Evaluate the expression EXP and return 1 if value is zero.
5007    This returns the inverse of the condition because it is called
5008    from catch_errors which returns 0 if an exception happened, and if an
5009    exception happens we want execution to stop.
5010    The argument is a "struct expression *" that has been cast to a
5011    "void *" to make it pass through catch_errors.  */
5012
5013 static int
5014 breakpoint_cond_eval (void *exp)
5015 {
5016   struct value *mark = value_mark ();
5017   int i = !value_true (evaluate_expression ((struct expression *) exp));
5018
5019   value_free_to_mark (mark);
5020   return i;
5021 }
5022
5023 /* Allocate a new bpstat.  Link it to the FIFO list by BS_LINK_POINTER.  */
5024
5025 static bpstat
5026 bpstat_alloc (struct bp_location *bl, bpstat **bs_link_pointer)
5027 {
5028   bpstat bs;
5029
5030   bs = (bpstat) xmalloc (sizeof (*bs));
5031   bs->next = NULL;
5032   **bs_link_pointer = bs;
5033   *bs_link_pointer = &bs->next;
5034   bs->breakpoint_at = bl->owner;
5035   bs->bp_location_at = bl;
5036   incref_bp_location (bl);
5037   /* If the condition is false, etc., don't do the commands.  */
5038   bs->commands = NULL;
5039   bs->old_val = NULL;
5040   bs->print_it = print_it_normal;
5041   return bs;
5042 }
5043 \f
5044 /* The target has stopped with waitstatus WS.  Check if any hardware
5045    watchpoints have triggered, according to the target.  */
5046
5047 int
5048 watchpoints_triggered (struct target_waitstatus *ws)
5049 {
5050   int stopped_by_watchpoint = target_stopped_by_watchpoint ();
5051   CORE_ADDR addr;
5052   struct breakpoint *b;
5053
5054   if (!stopped_by_watchpoint)
5055     {
5056       /* We were not stopped by a watchpoint.  Mark all watchpoints
5057          as not triggered.  */
5058       ALL_BREAKPOINTS (b)
5059         if (is_hardware_watchpoint (b))
5060           {
5061             struct watchpoint *w = (struct watchpoint *) b;
5062
5063             w->watchpoint_triggered = watch_triggered_no;
5064           }
5065
5066       return 0;
5067     }
5068
5069   if (!target_stopped_data_address (&current_target, &addr))
5070     {
5071       /* We were stopped by a watchpoint, but we don't know where.
5072          Mark all watchpoints as unknown.  */
5073       ALL_BREAKPOINTS (b)
5074         if (is_hardware_watchpoint (b))
5075           {
5076             struct watchpoint *w = (struct watchpoint *) b;
5077
5078             w->watchpoint_triggered = watch_triggered_unknown;
5079           }
5080
5081       return 1;
5082     }
5083
5084   /* The target could report the data address.  Mark watchpoints
5085      affected by this data address as triggered, and all others as not
5086      triggered.  */
5087
5088   ALL_BREAKPOINTS (b)
5089     if (is_hardware_watchpoint (b))
5090       {
5091         struct watchpoint *w = (struct watchpoint *) b;
5092         struct bp_location *loc;
5093
5094         w->watchpoint_triggered = watch_triggered_no;
5095         for (loc = b->loc; loc; loc = loc->next)
5096           {
5097             if (is_masked_watchpoint (b))
5098               {
5099                 CORE_ADDR newaddr = addr & w->hw_wp_mask;
5100                 CORE_ADDR start = loc->address & w->hw_wp_mask;
5101
5102                 if (newaddr == start)
5103                   {
5104                     w->watchpoint_triggered = watch_triggered_yes;
5105                     break;
5106                   }
5107               }
5108             /* Exact match not required.  Within range is sufficient.  */
5109             else if (target_watchpoint_addr_within_range (&current_target,
5110                                                          addr, loc->address,
5111                                                          loc->length))
5112               {
5113                 w->watchpoint_triggered = watch_triggered_yes;
5114                 break;
5115               }
5116           }
5117       }
5118
5119   return 1;
5120 }
5121
5122 /* Possible return values for watchpoint_check (this can't be an enum
5123    because of check_errors).  */
5124 /* The watchpoint has been deleted.  */
5125 #define WP_DELETED 1
5126 /* The value has changed.  */
5127 #define WP_VALUE_CHANGED 2
5128 /* The value has not changed.  */
5129 #define WP_VALUE_NOT_CHANGED 3
5130 /* Ignore this watchpoint, no matter if the value changed or not.  */
5131 #define WP_IGNORE 4
5132
5133 #define BP_TEMPFLAG 1
5134 #define BP_HARDWAREFLAG 2
5135
5136 /* Evaluate watchpoint condition expression and check if its value
5137    changed.
5138
5139    P should be a pointer to struct bpstat, but is defined as a void *
5140    in order for this function to be usable with catch_errors.  */
5141
5142 static int
5143 watchpoint_check (void *p)
5144 {
5145   bpstat bs = (bpstat) p;
5146   struct watchpoint *b;
5147   struct frame_info *fr;
5148   int within_current_scope;
5149
5150   /* BS is built from an existing struct breakpoint.  */
5151   gdb_assert (bs->breakpoint_at != NULL);
5152   b = (struct watchpoint *) bs->breakpoint_at;
5153
5154   /* If this is a local watchpoint, we only want to check if the
5155      watchpoint frame is in scope if the current thread is the thread
5156      that was used to create the watchpoint.  */
5157   if (!watchpoint_in_thread_scope (b))
5158     return WP_IGNORE;
5159
5160   if (b->exp_valid_block == NULL)
5161     within_current_scope = 1;
5162   else
5163     {
5164       struct frame_info *frame = get_current_frame ();
5165       struct gdbarch *frame_arch = get_frame_arch (frame);
5166       CORE_ADDR frame_pc = get_frame_pc (frame);
5167
5168       /* stack_frame_destroyed_p() returns a non-zero value if we're
5169          still in the function but the stack frame has already been
5170          invalidated.  Since we can't rely on the values of local
5171          variables after the stack has been destroyed, we are treating
5172          the watchpoint in that state as `not changed' without further
5173          checking.  Don't mark watchpoints as changed if the current
5174          frame is in an epilogue - even if they are in some other
5175          frame, our view of the stack is likely to be wrong and
5176          frame_find_by_id could error out.  */
5177       if (gdbarch_stack_frame_destroyed_p (frame_arch, frame_pc))
5178         return WP_IGNORE;
5179
5180       fr = frame_find_by_id (b->watchpoint_frame);
5181       within_current_scope = (fr != NULL);
5182
5183       /* If we've gotten confused in the unwinder, we might have
5184          returned a frame that can't describe this variable.  */
5185       if (within_current_scope)
5186         {
5187           struct symbol *function;
5188
5189           function = get_frame_function (fr);
5190           if (function == NULL
5191               || !contained_in (b->exp_valid_block,
5192                                 SYMBOL_BLOCK_VALUE (function)))
5193             within_current_scope = 0;
5194         }
5195
5196       if (within_current_scope)
5197         /* If we end up stopping, the current frame will get selected
5198            in normal_stop.  So this call to select_frame won't affect
5199            the user.  */
5200         select_frame (fr);
5201     }
5202
5203   if (within_current_scope)
5204     {
5205       /* We use value_{,free_to_}mark because it could be a *long*
5206          time before we return to the command level and call
5207          free_all_values.  We can't call free_all_values because we
5208          might be in the middle of evaluating a function call.  */
5209
5210       int pc = 0;
5211       struct value *mark;
5212       struct value *new_val;
5213
5214       if (is_masked_watchpoint (&b->base))
5215         /* Since we don't know the exact trigger address (from
5216            stopped_data_address), just tell the user we've triggered
5217            a mask watchpoint.  */
5218         return WP_VALUE_CHANGED;
5219
5220       mark = value_mark ();
5221       fetch_subexp_value (b->exp, &pc, &new_val, NULL, NULL, 0);
5222
5223       if (b->val_bitsize != 0)
5224         new_val = extract_bitfield_from_watchpoint_value (b, new_val);
5225
5226       /* We use value_equal_contents instead of value_equal because
5227          the latter coerces an array to a pointer, thus comparing just
5228          the address of the array instead of its contents.  This is
5229          not what we want.  */
5230       if ((b->val != NULL) != (new_val != NULL)
5231           || (b->val != NULL && !value_equal_contents (b->val, new_val)))
5232         {
5233           if (new_val != NULL)
5234             {
5235               release_value (new_val);
5236               value_free_to_mark (mark);
5237             }
5238           bs->old_val = b->val;
5239           b->val = new_val;
5240           b->val_valid = 1;
5241           return WP_VALUE_CHANGED;
5242         }
5243       else
5244         {
5245           /* Nothing changed.  */
5246           value_free_to_mark (mark);
5247           return WP_VALUE_NOT_CHANGED;
5248         }
5249     }
5250   else
5251     {
5252       /* This seems like the only logical thing to do because
5253          if we temporarily ignored the watchpoint, then when
5254          we reenter the block in which it is valid it contains
5255          garbage (in the case of a function, it may have two
5256          garbage values, one before and one after the prologue).
5257          So we can't even detect the first assignment to it and
5258          watch after that (since the garbage may or may not equal
5259          the first value assigned).  */
5260       /* We print all the stop information in
5261          breakpoint_ops->print_it, but in this case, by the time we
5262          call breakpoint_ops->print_it this bp will be deleted
5263          already.  So we have no choice but print the information
5264          here.  */
5265
5266       SWITCH_THRU_ALL_UIS ()
5267         {
5268           struct ui_out *uiout = current_uiout;
5269
5270           if (ui_out_is_mi_like_p (uiout))
5271             ui_out_field_string
5272               (uiout, "reason", async_reason_lookup (EXEC_ASYNC_WATCHPOINT_SCOPE));
5273           ui_out_text (uiout, "\nWatchpoint ");
5274           ui_out_field_int (uiout, "wpnum", b->base.number);
5275           ui_out_text (uiout,
5276                        " deleted because the program has left the block in\n"
5277                        "which its expression is valid.\n");
5278         }
5279
5280       /* Make sure the watchpoint's commands aren't executed.  */
5281       decref_counted_command_line (&b->base.commands);
5282       watchpoint_del_at_next_stop (b);
5283
5284       return WP_DELETED;
5285     }
5286 }
5287
5288 /* Return true if it looks like target has stopped due to hitting
5289    breakpoint location BL.  This function does not check if we should
5290    stop, only if BL explains the stop.  */
5291
5292 static int
5293 bpstat_check_location (const struct bp_location *bl,
5294                        struct address_space *aspace, CORE_ADDR bp_addr,
5295                        const struct target_waitstatus *ws)
5296 {
5297   struct breakpoint *b = bl->owner;
5298
5299   /* BL is from an existing breakpoint.  */
5300   gdb_assert (b != NULL);
5301
5302   return b->ops->breakpoint_hit (bl, aspace, bp_addr, ws);
5303 }
5304
5305 /* Determine if the watched values have actually changed, and we
5306    should stop.  If not, set BS->stop to 0.  */
5307
5308 static void
5309 bpstat_check_watchpoint (bpstat bs)
5310 {
5311   const struct bp_location *bl;
5312   struct watchpoint *b;
5313
5314   /* BS is built for existing struct breakpoint.  */
5315   bl = bs->bp_location_at;
5316   gdb_assert (bl != NULL);
5317   b = (struct watchpoint *) bs->breakpoint_at;
5318   gdb_assert (b != NULL);
5319
5320     {
5321       int must_check_value = 0;
5322       
5323       if (b->base.type == bp_watchpoint)
5324         /* For a software watchpoint, we must always check the
5325            watched value.  */
5326         must_check_value = 1;
5327       else if (b->watchpoint_triggered == watch_triggered_yes)
5328         /* We have a hardware watchpoint (read, write, or access)
5329            and the target earlier reported an address watched by
5330            this watchpoint.  */
5331         must_check_value = 1;
5332       else if (b->watchpoint_triggered == watch_triggered_unknown
5333                && b->base.type == bp_hardware_watchpoint)
5334         /* We were stopped by a hardware watchpoint, but the target could
5335            not report the data address.  We must check the watchpoint's
5336            value.  Access and read watchpoints are out of luck; without
5337            a data address, we can't figure it out.  */
5338         must_check_value = 1;
5339
5340       if (must_check_value)
5341         {
5342           char *message
5343             = xstrprintf ("Error evaluating expression for watchpoint %d\n",
5344                           b->base.number);
5345           struct cleanup *cleanups = make_cleanup (xfree, message);
5346           int e = catch_errors (watchpoint_check, bs, message,
5347                                 RETURN_MASK_ALL);
5348           do_cleanups (cleanups);
5349           switch (e)
5350             {
5351             case WP_DELETED:
5352               /* We've already printed what needs to be printed.  */
5353               bs->print_it = print_it_done;
5354               /* Stop.  */
5355               break;
5356             case WP_IGNORE:
5357               bs->print_it = print_it_noop;
5358               bs->stop = 0;
5359               break;
5360             case WP_VALUE_CHANGED:
5361               if (b->base.type == bp_read_watchpoint)
5362                 {
5363                   /* There are two cases to consider here:
5364
5365                      1. We're watching the triggered memory for reads.
5366                      In that case, trust the target, and always report
5367                      the watchpoint hit to the user.  Even though
5368                      reads don't cause value changes, the value may
5369                      have changed since the last time it was read, and
5370                      since we're not trapping writes, we will not see
5371                      those, and as such we should ignore our notion of
5372                      old value.
5373
5374                      2. We're watching the triggered memory for both
5375                      reads and writes.  There are two ways this may
5376                      happen:
5377
5378                      2.1. This is a target that can't break on data
5379                      reads only, but can break on accesses (reads or
5380                      writes), such as e.g., x86.  We detect this case
5381                      at the time we try to insert read watchpoints.
5382
5383                      2.2. Otherwise, the target supports read
5384                      watchpoints, but, the user set an access or write
5385                      watchpoint watching the same memory as this read
5386                      watchpoint.
5387
5388                      If we're watching memory writes as well as reads,
5389                      ignore watchpoint hits when we find that the
5390                      value hasn't changed, as reads don't cause
5391                      changes.  This still gives false positives when
5392                      the program writes the same value to memory as
5393                      what there was already in memory (we will confuse
5394                      it for a read), but it's much better than
5395                      nothing.  */
5396
5397                   int other_write_watchpoint = 0;
5398
5399                   if (bl->watchpoint_type == hw_read)
5400                     {
5401                       struct breakpoint *other_b;
5402
5403                       ALL_BREAKPOINTS (other_b)
5404                         if (other_b->type == bp_hardware_watchpoint
5405                             || other_b->type == bp_access_watchpoint)
5406                           {
5407                             struct watchpoint *other_w =
5408                               (struct watchpoint *) other_b;
5409
5410                             if (other_w->watchpoint_triggered
5411                                 == watch_triggered_yes)
5412                               {
5413                                 other_write_watchpoint = 1;
5414                                 break;
5415                               }
5416                           }
5417                     }
5418
5419                   if (other_write_watchpoint
5420                       || bl->watchpoint_type == hw_access)
5421                     {
5422                       /* We're watching the same memory for writes,
5423                          and the value changed since the last time we
5424                          updated it, so this trap must be for a write.
5425                          Ignore it.  */
5426                       bs->print_it = print_it_noop;
5427                       bs->stop = 0;
5428                     }
5429                 }
5430               break;
5431             case WP_VALUE_NOT_CHANGED:
5432               if (b->base.type == bp_hardware_watchpoint
5433                   || b->base.type == bp_watchpoint)
5434                 {
5435                   /* Don't stop: write watchpoints shouldn't fire if
5436                      the value hasn't changed.  */
5437                   bs->print_it = print_it_noop;
5438                   bs->stop = 0;
5439                 }
5440               /* Stop.  */
5441               break;
5442             default:
5443               /* Can't happen.  */
5444             case 0:
5445               /* Error from catch_errors.  */
5446               {
5447                 SWITCH_THRU_ALL_UIS ()
5448                   {
5449                     printf_filtered (_("Watchpoint %d deleted.\n"),
5450                                      b->base.number);
5451                   }
5452                 watchpoint_del_at_next_stop (b);
5453                 /* We've already printed what needs to be printed.  */
5454                 bs->print_it = print_it_done;
5455               }
5456               break;
5457             }
5458         }
5459       else      /* must_check_value == 0 */
5460         {
5461           /* This is a case where some watchpoint(s) triggered, but
5462              not at the address of this watchpoint, or else no
5463              watchpoint triggered after all.  So don't print
5464              anything for this watchpoint.  */
5465           bs->print_it = print_it_noop;
5466           bs->stop = 0;
5467         }
5468     }
5469 }
5470
5471 /* For breakpoints that are currently marked as telling gdb to stop,
5472    check conditions (condition proper, frame, thread and ignore count)
5473    of breakpoint referred to by BS.  If we should not stop for this
5474    breakpoint, set BS->stop to 0.  */
5475
5476 static void
5477 bpstat_check_breakpoint_conditions (bpstat bs, ptid_t ptid)
5478 {
5479   const struct bp_location *bl;
5480   struct breakpoint *b;
5481   int value_is_zero = 0;
5482   struct expression *cond;
5483
5484   gdb_assert (bs->stop);
5485
5486   /* BS is built for existing struct breakpoint.  */
5487   bl = bs->bp_location_at;
5488   gdb_assert (bl != NULL);
5489   b = bs->breakpoint_at;
5490   gdb_assert (b != NULL);
5491
5492   /* Even if the target evaluated the condition on its end and notified GDB, we
5493      need to do so again since GDB does not know if we stopped due to a
5494      breakpoint or a single step breakpoint.  */
5495
5496   if (frame_id_p (b->frame_id)
5497       && !frame_id_eq (b->frame_id, get_stack_frame_id (get_current_frame ())))
5498     {
5499       bs->stop = 0;
5500       return;
5501     }
5502
5503   /* If this is a thread/task-specific breakpoint, don't waste cpu
5504      evaluating the condition if this isn't the specified
5505      thread/task.  */
5506   if ((b->thread != -1 && b->thread != ptid_to_global_thread_id (ptid))
5507       || (b->task != 0 && b->task != ada_get_task_number (ptid)))
5508
5509     {
5510       bs->stop = 0;
5511       return;
5512     }
5513
5514   /* Evaluate extension language breakpoints that have a "stop" method
5515      implemented.  */
5516   bs->stop = breakpoint_ext_lang_cond_says_stop (b);
5517
5518   if (is_watchpoint (b))
5519     {
5520       struct watchpoint *w = (struct watchpoint *) b;
5521
5522       cond = w->cond_exp;
5523     }
5524   else
5525     cond = bl->cond;
5526
5527   if (cond && b->disposition != disp_del_at_next_stop)
5528     {
5529       int within_current_scope = 1;
5530       struct watchpoint * w;
5531
5532       /* We use value_mark and value_free_to_mark because it could
5533          be a long time before we return to the command level and
5534          call free_all_values.  We can't call free_all_values
5535          because we might be in the middle of evaluating a
5536          function call.  */
5537       struct value *mark = value_mark ();
5538
5539       if (is_watchpoint (b))
5540         w = (struct watchpoint *) b;
5541       else
5542         w = NULL;
5543
5544       /* Need to select the frame, with all that implies so that
5545          the conditions will have the right context.  Because we
5546          use the frame, we will not see an inlined function's
5547          variables when we arrive at a breakpoint at the start
5548          of the inlined function; the current frame will be the
5549          call site.  */
5550       if (w == NULL || w->cond_exp_valid_block == NULL)
5551         select_frame (get_current_frame ());
5552       else
5553         {
5554           struct frame_info *frame;
5555
5556           /* For local watchpoint expressions, which particular
5557              instance of a local is being watched matters, so we
5558              keep track of the frame to evaluate the expression
5559              in.  To evaluate the condition however, it doesn't
5560              really matter which instantiation of the function
5561              where the condition makes sense triggers the
5562              watchpoint.  This allows an expression like "watch
5563              global if q > 10" set in `func', catch writes to
5564              global on all threads that call `func', or catch
5565              writes on all recursive calls of `func' by a single
5566              thread.  We simply always evaluate the condition in
5567              the innermost frame that's executing where it makes
5568              sense to evaluate the condition.  It seems
5569              intuitive.  */
5570           frame = block_innermost_frame (w->cond_exp_valid_block);
5571           if (frame != NULL)
5572             select_frame (frame);
5573           else
5574             within_current_scope = 0;
5575         }
5576       if (within_current_scope)
5577         value_is_zero
5578           = catch_errors (breakpoint_cond_eval, cond,
5579                           "Error in testing breakpoint condition:\n",
5580                           RETURN_MASK_ALL);
5581       else
5582         {
5583           warning (_("Watchpoint condition cannot be tested "
5584                      "in the current scope"));
5585           /* If we failed to set the right context for this
5586              watchpoint, unconditionally report it.  */
5587           value_is_zero = 0;
5588         }
5589       /* FIXME-someday, should give breakpoint #.  */
5590       value_free_to_mark (mark);
5591     }
5592
5593   if (cond && value_is_zero)
5594     {
5595       bs->stop = 0;
5596     }
5597   else if (b->ignore_count > 0)
5598     {
5599       b->ignore_count--;
5600       bs->stop = 0;
5601       /* Increase the hit count even though we don't stop.  */
5602       ++(b->hit_count);
5603       observer_notify_breakpoint_modified (b);
5604     }   
5605 }
5606
5607 /* Returns true if we need to track moribund locations of LOC's type
5608    on the current target.  */
5609
5610 static int
5611 need_moribund_for_location_type (struct bp_location *loc)
5612 {
5613   return ((loc->loc_type == bp_loc_software_breakpoint
5614            && !target_supports_stopped_by_sw_breakpoint ())
5615           || (loc->loc_type == bp_loc_hardware_breakpoint
5616               && !target_supports_stopped_by_hw_breakpoint ()));
5617 }
5618
5619
5620 /* Get a bpstat associated with having just stopped at address
5621    BP_ADDR in thread PTID.
5622
5623    Determine whether we stopped at a breakpoint, etc, or whether we
5624    don't understand this stop.  Result is a chain of bpstat's such
5625    that:
5626
5627    if we don't understand the stop, the result is a null pointer.
5628
5629    if we understand why we stopped, the result is not null.
5630
5631    Each element of the chain refers to a particular breakpoint or
5632    watchpoint at which we have stopped.  (We may have stopped for
5633    several reasons concurrently.)
5634
5635    Each element of the chain has valid next, breakpoint_at,
5636    commands, FIXME??? fields.  */
5637
5638 bpstat
5639 bpstat_stop_status (struct address_space *aspace,
5640                     CORE_ADDR bp_addr, ptid_t ptid,
5641                     const struct target_waitstatus *ws)
5642 {
5643   struct breakpoint *b = NULL;
5644   struct bp_location *bl;
5645   struct bp_location *loc;
5646   /* First item of allocated bpstat's.  */
5647   bpstat bs_head = NULL, *bs_link = &bs_head;
5648   /* Pointer to the last thing in the chain currently.  */
5649   bpstat bs;
5650   int ix;
5651   int need_remove_insert;
5652   int removed_any;
5653
5654   /* First, build the bpstat chain with locations that explain a
5655      target stop, while being careful to not set the target running,
5656      as that may invalidate locations (in particular watchpoint
5657      locations are recreated).  Resuming will happen here with
5658      breakpoint conditions or watchpoint expressions that include
5659      inferior function calls.  */
5660
5661   ALL_BREAKPOINTS (b)
5662     {
5663       if (!breakpoint_enabled (b))
5664         continue;
5665
5666       for (bl = b->loc; bl != NULL; bl = bl->next)
5667         {
5668           /* For hardware watchpoints, we look only at the first
5669              location.  The watchpoint_check function will work on the
5670              entire expression, not the individual locations.  For
5671              read watchpoints, the watchpoints_triggered function has
5672              checked all locations already.  */
5673           if (b->type == bp_hardware_watchpoint && bl != b->loc)
5674             break;
5675
5676           if (!bl->enabled || bl->shlib_disabled)
5677             continue;
5678
5679           if (!bpstat_check_location (bl, aspace, bp_addr, ws))
5680             continue;
5681
5682           /* Come here if it's a watchpoint, or if the break address
5683              matches.  */
5684
5685           bs = bpstat_alloc (bl, &bs_link);     /* Alloc a bpstat to
5686                                                    explain stop.  */
5687
5688           /* Assume we stop.  Should we find a watchpoint that is not
5689              actually triggered, or if the condition of the breakpoint
5690              evaluates as false, we'll reset 'stop' to 0.  */
5691           bs->stop = 1;
5692           bs->print = 1;
5693
5694           /* If this is a scope breakpoint, mark the associated
5695              watchpoint as triggered so that we will handle the
5696              out-of-scope event.  We'll get to the watchpoint next
5697              iteration.  */
5698           if (b->type == bp_watchpoint_scope && b->related_breakpoint != b)
5699             {
5700               struct watchpoint *w = (struct watchpoint *) b->related_breakpoint;
5701
5702               w->watchpoint_triggered = watch_triggered_yes;
5703             }
5704         }
5705     }
5706
5707   /* Check if a moribund breakpoint explains the stop.  */
5708   if (!target_supports_stopped_by_sw_breakpoint ()
5709       || !target_supports_stopped_by_hw_breakpoint ())
5710     {
5711       for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, loc); ++ix)
5712         {
5713           if (breakpoint_location_address_match (loc, aspace, bp_addr)
5714               && need_moribund_for_location_type (loc))
5715             {
5716               bs = bpstat_alloc (loc, &bs_link);
5717               /* For hits of moribund locations, we should just proceed.  */
5718               bs->stop = 0;
5719               bs->print = 0;
5720               bs->print_it = print_it_noop;
5721             }
5722         }
5723     }
5724
5725   /* A bit of special processing for shlib breakpoints.  We need to
5726      process solib loading here, so that the lists of loaded and
5727      unloaded libraries are correct before we handle "catch load" and
5728      "catch unload".  */
5729   for (bs = bs_head; bs != NULL; bs = bs->next)
5730     {
5731       if (bs->breakpoint_at && bs->breakpoint_at->type == bp_shlib_event)
5732         {
5733           handle_solib_event ();
5734           break;
5735         }
5736     }
5737
5738   /* Now go through the locations that caused the target to stop, and
5739      check whether we're interested in reporting this stop to higher
5740      layers, or whether we should resume the target transparently.  */
5741
5742   removed_any = 0;
5743
5744   for (bs = bs_head; bs != NULL; bs = bs->next)
5745     {
5746       if (!bs->stop)
5747         continue;
5748
5749       b = bs->breakpoint_at;
5750       b->ops->check_status (bs);
5751       if (bs->stop)
5752         {
5753           bpstat_check_breakpoint_conditions (bs, ptid);
5754
5755           if (bs->stop)
5756             {
5757               ++(b->hit_count);
5758               observer_notify_breakpoint_modified (b);
5759
5760               /* We will stop here.  */
5761               if (b->disposition == disp_disable)
5762                 {
5763                   --(b->enable_count);
5764                   if (b->enable_count <= 0)
5765                     b->enable_state = bp_disabled;
5766                   removed_any = 1;
5767                 }
5768               if (b->silent)
5769                 bs->print = 0;
5770               bs->commands = b->commands;
5771               incref_counted_command_line (bs->commands);
5772               if (command_line_is_silent (bs->commands
5773                                           ? bs->commands->commands : NULL))
5774                 bs->print = 0;
5775
5776               b->ops->after_condition_true (bs);
5777             }
5778
5779         }
5780
5781       /* Print nothing for this entry if we don't stop or don't
5782          print.  */
5783       if (!bs->stop || !bs->print)
5784         bs->print_it = print_it_noop;
5785     }
5786
5787   /* If we aren't stopping, the value of some hardware watchpoint may
5788      not have changed, but the intermediate memory locations we are
5789      watching may have.  Don't bother if we're stopping; this will get
5790      done later.  */
5791   need_remove_insert = 0;
5792   if (! bpstat_causes_stop (bs_head))
5793     for (bs = bs_head; bs != NULL; bs = bs->next)
5794       if (!bs->stop
5795           && bs->breakpoint_at
5796           && is_hardware_watchpoint (bs->breakpoint_at))
5797         {
5798           struct watchpoint *w = (struct watchpoint *) bs->breakpoint_at;
5799
5800           update_watchpoint (w, 0 /* don't reparse.  */);
5801           need_remove_insert = 1;
5802         }
5803
5804   if (need_remove_insert)
5805     update_global_location_list (UGLL_MAY_INSERT);
5806   else if (removed_any)
5807     update_global_location_list (UGLL_DONT_INSERT);
5808
5809   return bs_head;
5810 }
5811
5812 static void
5813 handle_jit_event (void)
5814 {
5815   struct frame_info *frame;
5816   struct gdbarch *gdbarch;
5817
5818   if (debug_infrun)
5819     fprintf_unfiltered (gdb_stdlog, "handling bp_jit_event\n");
5820
5821   /* Switch terminal for any messages produced by
5822      breakpoint_re_set.  */
5823   target_terminal_ours_for_output ();
5824
5825   frame = get_current_frame ();
5826   gdbarch = get_frame_arch (frame);
5827
5828   jit_event_handler (gdbarch);
5829
5830   target_terminal_inferior ();
5831 }
5832
5833 /* Prepare WHAT final decision for infrun.  */
5834
5835 /* Decide what infrun needs to do with this bpstat.  */
5836
5837 struct bpstat_what
5838 bpstat_what (bpstat bs_head)
5839 {
5840   struct bpstat_what retval;
5841   bpstat bs;
5842
5843   retval.main_action = BPSTAT_WHAT_KEEP_CHECKING;
5844   retval.call_dummy = STOP_NONE;
5845   retval.is_longjmp = 0;
5846
5847   for (bs = bs_head; bs != NULL; bs = bs->next)
5848     {
5849       /* Extract this BS's action.  After processing each BS, we check
5850          if its action overrides all we've seem so far.  */
5851       enum bpstat_what_main_action this_action = BPSTAT_WHAT_KEEP_CHECKING;
5852       enum bptype bptype;
5853
5854       if (bs->breakpoint_at == NULL)
5855         {
5856           /* I suspect this can happen if it was a momentary
5857              breakpoint which has since been deleted.  */
5858           bptype = bp_none;
5859         }
5860       else
5861         bptype = bs->breakpoint_at->type;
5862
5863       switch (bptype)
5864         {
5865         case bp_none:
5866           break;
5867         case bp_breakpoint:
5868         case bp_hardware_breakpoint:
5869         case bp_single_step:
5870         case bp_until:
5871         case bp_finish:
5872         case bp_shlib_event:
5873           if (bs->stop)
5874             {
5875               if (bs->print)
5876                 this_action = BPSTAT_WHAT_STOP_NOISY;
5877               else
5878                 this_action = BPSTAT_WHAT_STOP_SILENT;
5879             }
5880           else
5881             this_action = BPSTAT_WHAT_SINGLE;
5882           break;
5883         case bp_watchpoint:
5884         case bp_hardware_watchpoint:
5885         case bp_read_watchpoint:
5886         case bp_access_watchpoint:
5887           if (bs->stop)
5888             {
5889               if (bs->print)
5890                 this_action = BPSTAT_WHAT_STOP_NOISY;
5891               else
5892                 this_action = BPSTAT_WHAT_STOP_SILENT;
5893             }
5894           else
5895             {
5896               /* There was a watchpoint, but we're not stopping.
5897                  This requires no further action.  */
5898             }
5899           break;
5900         case bp_longjmp:
5901         case bp_longjmp_call_dummy:
5902         case bp_exception:
5903           if (bs->stop)
5904             {
5905               this_action = BPSTAT_WHAT_SET_LONGJMP_RESUME;
5906               retval.is_longjmp = bptype != bp_exception;
5907             }
5908           else
5909             this_action = BPSTAT_WHAT_SINGLE;
5910           break;
5911         case bp_longjmp_resume:
5912         case bp_exception_resume:
5913           if (bs->stop)
5914             {
5915               this_action = BPSTAT_WHAT_CLEAR_LONGJMP_RESUME;
5916               retval.is_longjmp = bptype == bp_longjmp_resume;
5917             }
5918           else
5919             this_action = BPSTAT_WHAT_SINGLE;
5920           break;
5921         case bp_step_resume:
5922           if (bs->stop)
5923             this_action = BPSTAT_WHAT_STEP_RESUME;
5924           else
5925             {
5926               /* It is for the wrong frame.  */
5927               this_action = BPSTAT_WHAT_SINGLE;
5928             }
5929           break;
5930         case bp_hp_step_resume:
5931           if (bs->stop)
5932             this_action = BPSTAT_WHAT_HP_STEP_RESUME;
5933           else
5934             {
5935               /* It is for the wrong frame.  */
5936               this_action = BPSTAT_WHAT_SINGLE;
5937             }
5938           break;
5939         case bp_watchpoint_scope:
5940         case bp_thread_event:
5941         case bp_overlay_event:
5942         case bp_longjmp_master:
5943         case bp_std_terminate_master:
5944         case bp_exception_master:
5945           this_action = BPSTAT_WHAT_SINGLE;
5946           break;
5947         case bp_catchpoint:
5948           if (bs->stop)
5949             {
5950               if (bs->print)
5951                 this_action = BPSTAT_WHAT_STOP_NOISY;
5952               else
5953                 this_action = BPSTAT_WHAT_STOP_SILENT;
5954             }
5955           else
5956             {
5957               /* There was a catchpoint, but we're not stopping.
5958                  This requires no further action.  */
5959             }
5960           break;
5961         case bp_jit_event:
5962           this_action = BPSTAT_WHAT_SINGLE;
5963           break;
5964         case bp_call_dummy:
5965           /* Make sure the action is stop (silent or noisy),
5966              so infrun.c pops the dummy frame.  */
5967           retval.call_dummy = STOP_STACK_DUMMY;
5968           this_action = BPSTAT_WHAT_STOP_SILENT;
5969           break;
5970         case bp_std_terminate:
5971           /* Make sure the action is stop (silent or noisy),
5972              so infrun.c pops the dummy frame.  */
5973           retval.call_dummy = STOP_STD_TERMINATE;
5974           this_action = BPSTAT_WHAT_STOP_SILENT;
5975           break;
5976         case bp_tracepoint:
5977         case bp_fast_tracepoint:
5978         case bp_static_tracepoint:
5979           /* Tracepoint hits should not be reported back to GDB, and
5980              if one got through somehow, it should have been filtered
5981              out already.  */
5982           internal_error (__FILE__, __LINE__,
5983                           _("bpstat_what: tracepoint encountered"));
5984           break;
5985         case bp_gnu_ifunc_resolver:
5986           /* Step over it (and insert bp_gnu_ifunc_resolver_return).  */
5987           this_action = BPSTAT_WHAT_SINGLE;
5988           break;
5989         case bp_gnu_ifunc_resolver_return:
5990           /* The breakpoint will be removed, execution will restart from the
5991              PC of the former breakpoint.  */
5992           this_action = BPSTAT_WHAT_KEEP_CHECKING;
5993           break;
5994
5995         case bp_dprintf:
5996           if (bs->stop)
5997             this_action = BPSTAT_WHAT_STOP_SILENT;
5998           else
5999             this_action = BPSTAT_WHAT_SINGLE;
6000           break;
6001
6002         default:
6003           internal_error (__FILE__, __LINE__,
6004                           _("bpstat_what: unhandled bptype %d"), (int) bptype);
6005         }
6006
6007       retval.main_action = std::max (retval.main_action, this_action);
6008     }
6009
6010   return retval;
6011 }
6012
6013 void
6014 bpstat_run_callbacks (bpstat bs_head)
6015 {
6016   bpstat bs;
6017
6018   for (bs = bs_head; bs != NULL; bs = bs->next)
6019     {
6020       struct breakpoint *b = bs->breakpoint_at;
6021
6022       if (b == NULL)
6023         continue;
6024       switch (b->type)
6025         {
6026         case bp_jit_event:
6027           handle_jit_event ();
6028           break;
6029         case bp_gnu_ifunc_resolver:
6030           gnu_ifunc_resolver_stop (b);
6031           break;
6032         case bp_gnu_ifunc_resolver_return:
6033           gnu_ifunc_resolver_return_stop (b);
6034           break;
6035         }
6036     }
6037 }
6038
6039 /* Nonzero if we should step constantly (e.g. watchpoints on machines
6040    without hardware support).  This isn't related to a specific bpstat,
6041    just to things like whether watchpoints are set.  */
6042
6043 int
6044 bpstat_should_step (void)
6045 {
6046   struct breakpoint *b;
6047
6048   ALL_BREAKPOINTS (b)
6049     if (breakpoint_enabled (b) && b->type == bp_watchpoint && b->loc != NULL)
6050       return 1;
6051   return 0;
6052 }
6053
6054 int
6055 bpstat_causes_stop (bpstat bs)
6056 {
6057   for (; bs != NULL; bs = bs->next)
6058     if (bs->stop)
6059       return 1;
6060
6061   return 0;
6062 }
6063
6064 \f
6065
6066 /* Compute a string of spaces suitable to indent the next line
6067    so it starts at the position corresponding to the table column
6068    named COL_NAME in the currently active table of UIOUT.  */
6069
6070 static char *
6071 wrap_indent_at_field (struct ui_out *uiout, const char *col_name)
6072 {
6073   static char wrap_indent[80];
6074   int i, total_width, width, align;
6075   char *text;
6076
6077   total_width = 0;
6078   for (i = 1; ui_out_query_field (uiout, i, &width, &align, &text); i++)
6079     {
6080       if (strcmp (text, col_name) == 0)
6081         {
6082           gdb_assert (total_width < sizeof wrap_indent);
6083           memset (wrap_indent, ' ', total_width);
6084           wrap_indent[total_width] = 0;
6085
6086           return wrap_indent;
6087         }
6088
6089       total_width += width + 1;
6090     }
6091
6092   return NULL;
6093 }
6094
6095 /* Determine if the locations of this breakpoint will have their conditions
6096    evaluated by the target, host or a mix of both.  Returns the following:
6097
6098     "host": Host evals condition.
6099     "host or target": Host or Target evals condition.
6100     "target": Target evals condition.
6101 */
6102
6103 static const char *
6104 bp_condition_evaluator (struct breakpoint *b)
6105 {
6106   struct bp_location *bl;
6107   char host_evals = 0;
6108   char target_evals = 0;
6109
6110   if (!b)
6111     return NULL;
6112
6113   if (!is_breakpoint (b))
6114     return NULL;
6115
6116   if (gdb_evaluates_breakpoint_condition_p ()
6117       || !target_supports_evaluation_of_breakpoint_conditions ())
6118     return condition_evaluation_host;
6119
6120   for (bl = b->loc; bl; bl = bl->next)
6121     {
6122       if (bl->cond_bytecode)
6123         target_evals++;
6124       else
6125         host_evals++;
6126     }
6127
6128   if (host_evals && target_evals)
6129     return condition_evaluation_both;
6130   else if (target_evals)
6131     return condition_evaluation_target;
6132   else
6133     return condition_evaluation_host;
6134 }
6135
6136 /* Determine the breakpoint location's condition evaluator.  This is
6137    similar to bp_condition_evaluator, but for locations.  */
6138
6139 static const char *
6140 bp_location_condition_evaluator (struct bp_location *bl)
6141 {
6142   if (bl && !is_breakpoint (bl->owner))
6143     return NULL;
6144
6145   if (gdb_evaluates_breakpoint_condition_p ()
6146       || !target_supports_evaluation_of_breakpoint_conditions ())
6147     return condition_evaluation_host;
6148
6149   if (bl && bl->cond_bytecode)
6150     return condition_evaluation_target;
6151   else
6152     return condition_evaluation_host;
6153 }
6154
6155 /* Print the LOC location out of the list of B->LOC locations.  */
6156
6157 static void
6158 print_breakpoint_location (struct breakpoint *b,
6159                            struct bp_location *loc)
6160 {
6161   struct ui_out *uiout = current_uiout;
6162   struct cleanup *old_chain = save_current_program_space ();
6163
6164   if (loc != NULL && loc->shlib_disabled)
6165     loc = NULL;
6166
6167   if (loc != NULL)
6168     set_current_program_space (loc->pspace);
6169
6170   if (b->display_canonical)
6171     ui_out_field_string (uiout, "what",
6172                          event_location_to_string (b->location));
6173   else if (loc && loc->symtab)
6174     {
6175       struct symbol *sym 
6176         = find_pc_sect_function (loc->address, loc->section);
6177       if (sym)
6178         {
6179           ui_out_text (uiout, "in ");
6180           ui_out_field_string (uiout, "func",
6181                                SYMBOL_PRINT_NAME (sym));
6182           ui_out_text (uiout, " ");
6183           ui_out_wrap_hint (uiout, wrap_indent_at_field (uiout, "what"));
6184           ui_out_text (uiout, "at ");
6185         }
6186       ui_out_field_string (uiout, "file",
6187                            symtab_to_filename_for_display (loc->symtab));
6188       ui_out_text (uiout, ":");
6189
6190       if (ui_out_is_mi_like_p (uiout))
6191         ui_out_field_string (uiout, "fullname",
6192                              symtab_to_fullname (loc->symtab));
6193       
6194       ui_out_field_int (uiout, "line", loc->line_number);
6195     }
6196   else if (loc)
6197     {
6198       struct ui_file *stb = mem_fileopen ();
6199       struct cleanup *stb_chain = make_cleanup_ui_file_delete (stb);
6200
6201       print_address_symbolic (loc->gdbarch, loc->address, stb,
6202                               demangle, "");
6203       ui_out_field_stream (uiout, "at", stb);
6204
6205       do_cleanups (stb_chain);
6206     }
6207   else
6208     {
6209       ui_out_field_string (uiout, "pending",
6210                            event_location_to_string (b->location));
6211       /* If extra_string is available, it could be holding a condition
6212          or dprintf arguments.  In either case, make sure it is printed,
6213          too, but only for non-MI streams.  */
6214       if (!ui_out_is_mi_like_p (uiout) && b->extra_string != NULL)
6215         {
6216           if (b->type == bp_dprintf)
6217             ui_out_text (uiout, ",");
6218           else
6219             ui_out_text (uiout, " ");
6220           ui_out_text (uiout, b->extra_string);
6221         }
6222     }
6223
6224   if (loc && is_breakpoint (b)
6225       && breakpoint_condition_evaluation_mode () == condition_evaluation_target
6226       && bp_condition_evaluator (b) == condition_evaluation_both)
6227     {
6228       ui_out_text (uiout, " (");
6229       ui_out_field_string (uiout, "evaluated-by",
6230                            bp_location_condition_evaluator (loc));
6231       ui_out_text (uiout, ")");
6232     }
6233
6234   do_cleanups (old_chain);
6235 }
6236
6237 static const char *
6238 bptype_string (enum bptype type)
6239 {
6240   struct ep_type_description
6241     {
6242       enum bptype type;
6243       char *description;
6244     };
6245   static struct ep_type_description bptypes[] =
6246   {
6247     {bp_none, "?deleted?"},
6248     {bp_breakpoint, "breakpoint"},
6249     {bp_hardware_breakpoint, "hw breakpoint"},
6250     {bp_single_step, "sw single-step"},
6251     {bp_until, "until"},
6252     {bp_finish, "finish"},
6253     {bp_watchpoint, "watchpoint"},
6254     {bp_hardware_watchpoint, "hw watchpoint"},
6255     {bp_read_watchpoint, "read watchpoint"},
6256     {bp_access_watchpoint, "acc watchpoint"},
6257     {bp_longjmp, "longjmp"},
6258     {bp_longjmp_resume, "longjmp resume"},
6259     {bp_longjmp_call_dummy, "longjmp for call dummy"},
6260     {bp_exception, "exception"},
6261     {bp_exception_resume, "exception resume"},
6262     {bp_step_resume, "step resume"},
6263     {bp_hp_step_resume, "high-priority step resume"},
6264     {bp_watchpoint_scope, "watchpoint scope"},
6265     {bp_call_dummy, "call dummy"},
6266     {bp_std_terminate, "std::terminate"},
6267     {bp_shlib_event, "shlib events"},
6268     {bp_thread_event, "thread events"},
6269     {bp_overlay_event, "overlay events"},
6270     {bp_longjmp_master, "longjmp master"},
6271     {bp_std_terminate_master, "std::terminate master"},
6272     {bp_exception_master, "exception master"},
6273     {bp_catchpoint, "catchpoint"},
6274     {bp_tracepoint, "tracepoint"},
6275     {bp_fast_tracepoint, "fast tracepoint"},
6276     {bp_static_tracepoint, "static tracepoint"},
6277     {bp_dprintf, "dprintf"},
6278     {bp_jit_event, "jit events"},
6279     {bp_gnu_ifunc_resolver, "STT_GNU_IFUNC resolver"},
6280     {bp_gnu_ifunc_resolver_return, "STT_GNU_IFUNC resolver return"},
6281   };
6282
6283   if (((int) type >= (sizeof (bptypes) / sizeof (bptypes[0])))
6284       || ((int) type != bptypes[(int) type].type))
6285     internal_error (__FILE__, __LINE__,
6286                     _("bptypes table does not describe type #%d."),
6287                     (int) type);
6288
6289   return bptypes[(int) type].description;
6290 }
6291
6292 /* For MI, output a field named 'thread-groups' with a list as the value.
6293    For CLI, prefix the list with the string 'inf'. */
6294
6295 static void
6296 output_thread_groups (struct ui_out *uiout,
6297                       const char *field_name,
6298                       VEC(int) *inf_num,
6299                       int mi_only)
6300 {
6301   struct cleanup *back_to;
6302   int is_mi = ui_out_is_mi_like_p (uiout);
6303   int inf;
6304   int i;
6305
6306   /* For backward compatibility, don't display inferiors in CLI unless
6307      there are several.  Always display them for MI. */
6308   if (!is_mi && mi_only)
6309     return;
6310
6311   back_to = make_cleanup_ui_out_list_begin_end (uiout, field_name);
6312
6313   for (i = 0; VEC_iterate (int, inf_num, i, inf); ++i)
6314     {
6315       if (is_mi)
6316         {
6317           char mi_group[10];
6318
6319           xsnprintf (mi_group, sizeof (mi_group), "i%d", inf);
6320           ui_out_field_string (uiout, NULL, mi_group);
6321         }
6322       else
6323         {
6324           if (i == 0)
6325             ui_out_text (uiout, " inf ");
6326           else
6327             ui_out_text (uiout, ", ");
6328         
6329           ui_out_text (uiout, plongest (inf));
6330         }
6331     }
6332
6333   do_cleanups (back_to);
6334 }
6335
6336 /* Print B to gdb_stdout.  */
6337
6338 static void
6339 print_one_breakpoint_location (struct breakpoint *b,
6340                                struct bp_location *loc,
6341                                int loc_number,
6342                                struct bp_location **last_loc,
6343                                int allflag)
6344 {
6345   struct command_line *l;
6346   static char bpenables[] = "nynny";
6347
6348   struct ui_out *uiout = current_uiout;
6349   int header_of_multiple = 0;
6350   int part_of_multiple = (loc != NULL);
6351   struct value_print_options opts;
6352
6353   get_user_print_options (&opts);
6354
6355   gdb_assert (!loc || loc_number != 0);
6356   /* See comment in print_one_breakpoint concerning treatment of
6357      breakpoints with single disabled location.  */
6358   if (loc == NULL 
6359       && (b->loc != NULL 
6360           && (b->loc->next != NULL || !b->loc->enabled)))
6361     header_of_multiple = 1;
6362   if (loc == NULL)
6363     loc = b->loc;
6364
6365   annotate_record ();
6366
6367   /* 1 */
6368   annotate_field (0);
6369   if (part_of_multiple)
6370     {
6371       char *formatted;
6372       formatted = xstrprintf ("%d.%d", b->number, loc_number);
6373       ui_out_field_string (uiout, "number", formatted);
6374       xfree (formatted);
6375     }
6376   else
6377     {
6378       ui_out_field_int (uiout, "number", b->number);
6379     }
6380
6381   /* 2 */
6382   annotate_field (1);
6383   if (part_of_multiple)
6384     ui_out_field_skip (uiout, "type");
6385   else
6386     ui_out_field_string (uiout, "type", bptype_string (b->type));
6387
6388   /* 3 */
6389   annotate_field (2);
6390   if (part_of_multiple)
6391     ui_out_field_skip (uiout, "disp");
6392   else
6393     ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
6394
6395
6396   /* 4 */
6397   annotate_field (3);
6398   if (part_of_multiple)
6399     ui_out_field_string (uiout, "enabled", loc->enabled ? "y" : "n");
6400   else
6401     ui_out_field_fmt (uiout, "enabled", "%c", 
6402                       bpenables[(int) b->enable_state]);
6403   ui_out_spaces (uiout, 2);
6404
6405   
6406   /* 5 and 6 */
6407   if (b->ops != NULL && b->ops->print_one != NULL)
6408     {
6409       /* Although the print_one can possibly print all locations,
6410          calling it here is not likely to get any nice result.  So,
6411          make sure there's just one location.  */
6412       gdb_assert (b->loc == NULL || b->loc->next == NULL);
6413       b->ops->print_one (b, last_loc);
6414     }
6415   else
6416     switch (b->type)
6417       {
6418       case bp_none:
6419         internal_error (__FILE__, __LINE__,
6420                         _("print_one_breakpoint: bp_none encountered\n"));
6421         break;
6422
6423       case bp_watchpoint:
6424       case bp_hardware_watchpoint:
6425       case bp_read_watchpoint:
6426       case bp_access_watchpoint:
6427         {
6428           struct watchpoint *w = (struct watchpoint *) b;
6429
6430           /* Field 4, the address, is omitted (which makes the columns
6431              not line up too nicely with the headers, but the effect
6432              is relatively readable).  */
6433           if (opts.addressprint)
6434             ui_out_field_skip (uiout, "addr");
6435           annotate_field (5);
6436           ui_out_field_string (uiout, "what", w->exp_string);
6437         }
6438         break;
6439
6440       case bp_breakpoint:
6441       case bp_hardware_breakpoint:
6442       case bp_single_step:
6443       case bp_until:
6444       case bp_finish:
6445       case bp_longjmp:
6446       case bp_longjmp_resume:
6447       case bp_longjmp_call_dummy:
6448       case bp_exception:
6449       case bp_exception_resume:
6450       case bp_step_resume:
6451       case bp_hp_step_resume:
6452       case bp_watchpoint_scope:
6453       case bp_call_dummy:
6454       case bp_std_terminate:
6455       case bp_shlib_event:
6456       case bp_thread_event:
6457       case bp_overlay_event:
6458       case bp_longjmp_master:
6459       case bp_std_terminate_master:
6460       case bp_exception_master:
6461       case bp_tracepoint:
6462       case bp_fast_tracepoint:
6463       case bp_static_tracepoint:
6464       case bp_dprintf:
6465       case bp_jit_event:
6466       case bp_gnu_ifunc_resolver:
6467       case bp_gnu_ifunc_resolver_return:
6468         if (opts.addressprint)
6469           {
6470             annotate_field (4);
6471             if (header_of_multiple)
6472               ui_out_field_string (uiout, "addr", "<MULTIPLE>");
6473             else if (b->loc == NULL || loc->shlib_disabled)
6474               ui_out_field_string (uiout, "addr", "<PENDING>");
6475             else
6476               ui_out_field_core_addr (uiout, "addr",
6477                                       loc->gdbarch, loc->address);
6478           }
6479         annotate_field (5);
6480         if (!header_of_multiple)
6481           print_breakpoint_location (b, loc);
6482         if (b->loc)
6483           *last_loc = b->loc;
6484         break;
6485       }
6486
6487
6488   if (loc != NULL && !header_of_multiple)
6489     {
6490       struct inferior *inf;
6491       VEC(int) *inf_num = NULL;
6492       int mi_only = 1;
6493
6494       ALL_INFERIORS (inf)
6495         {
6496           if (inf->pspace == loc->pspace)
6497             VEC_safe_push (int, inf_num, inf->num);
6498         }
6499
6500         /* For backward compatibility, don't display inferiors in CLI unless
6501            there are several.  Always display for MI. */
6502         if (allflag
6503             || (!gdbarch_has_global_breakpoints (target_gdbarch ())
6504                 && (number_of_program_spaces () > 1
6505                     || number_of_inferiors () > 1)
6506                 /* LOC is for existing B, it cannot be in
6507                    moribund_locations and thus having NULL OWNER.  */
6508                 && loc->owner->type != bp_catchpoint))
6509         mi_only = 0;
6510       output_thread_groups (uiout, "thread-groups", inf_num, mi_only);
6511       VEC_free (int, inf_num);
6512     }
6513
6514   if (!part_of_multiple)
6515     {
6516       if (b->thread != -1)
6517         {
6518           /* FIXME: This seems to be redundant and lost here; see the
6519              "stop only in" line a little further down.  */
6520           ui_out_text (uiout, " thread ");
6521           ui_out_field_int (uiout, "thread", b->thread);
6522         }
6523       else if (b->task != 0)
6524         {
6525           ui_out_text (uiout, " task ");
6526           ui_out_field_int (uiout, "task", b->task);
6527         }
6528     }
6529
6530   ui_out_text (uiout, "\n");
6531
6532   if (!part_of_multiple)
6533     b->ops->print_one_detail (b, uiout);
6534
6535   if (part_of_multiple && frame_id_p (b->frame_id))
6536     {
6537       annotate_field (6);
6538       ui_out_text (uiout, "\tstop only in stack frame at ");
6539       /* FIXME: cagney/2002-12-01: Shouldn't be poking around inside
6540          the frame ID.  */
6541       ui_out_field_core_addr (uiout, "frame",
6542                               b->gdbarch, b->frame_id.stack_addr);
6543       ui_out_text (uiout, "\n");
6544     }
6545   
6546   if (!part_of_multiple && b->cond_string)
6547     {
6548       annotate_field (7);
6549       if (is_tracepoint (b))
6550         ui_out_text (uiout, "\ttrace only if ");
6551       else
6552         ui_out_text (uiout, "\tstop only if ");
6553       ui_out_field_string (uiout, "cond", b->cond_string);
6554
6555       /* Print whether the target is doing the breakpoint's condition
6556          evaluation.  If GDB is doing the evaluation, don't print anything.  */
6557       if (is_breakpoint (b)
6558           && breakpoint_condition_evaluation_mode ()
6559           == condition_evaluation_target)
6560         {
6561           ui_out_text (uiout, " (");
6562           ui_out_field_string (uiout, "evaluated-by",
6563                                bp_condition_evaluator (b));
6564           ui_out_text (uiout, " evals)");
6565         }
6566       ui_out_text (uiout, "\n");
6567     }
6568
6569   if (!part_of_multiple && b->thread != -1)
6570     {
6571       /* FIXME should make an annotation for this.  */
6572       ui_out_text (uiout, "\tstop only in thread ");
6573       if (ui_out_is_mi_like_p (uiout))
6574         ui_out_field_int (uiout, "thread", b->thread);
6575       else
6576         {
6577           struct thread_info *thr = find_thread_global_id (b->thread);
6578
6579           ui_out_field_string (uiout, "thread", print_thread_id (thr));
6580         }
6581       ui_out_text (uiout, "\n");
6582     }
6583   
6584   if (!part_of_multiple)
6585     {
6586       if (b->hit_count)
6587         {
6588           /* FIXME should make an annotation for this.  */
6589           if (is_catchpoint (b))
6590             ui_out_text (uiout, "\tcatchpoint");
6591           else if (is_tracepoint (b))
6592             ui_out_text (uiout, "\ttracepoint");
6593           else
6594             ui_out_text (uiout, "\tbreakpoint");
6595           ui_out_text (uiout, " already hit ");
6596           ui_out_field_int (uiout, "times", b->hit_count);
6597           if (b->hit_count == 1)
6598             ui_out_text (uiout, " time\n");
6599           else
6600             ui_out_text (uiout, " times\n");
6601         }
6602       else
6603         {
6604           /* Output the count also if it is zero, but only if this is mi.  */
6605           if (ui_out_is_mi_like_p (uiout))
6606             ui_out_field_int (uiout, "times", b->hit_count);
6607         }
6608     }
6609
6610   if (!part_of_multiple && b->ignore_count)
6611     {
6612       annotate_field (8);
6613       ui_out_text (uiout, "\tignore next ");
6614       ui_out_field_int (uiout, "ignore", b->ignore_count);
6615       ui_out_text (uiout, " hits\n");
6616     }
6617
6618   /* Note that an enable count of 1 corresponds to "enable once"
6619      behavior, which is reported by the combination of enablement and
6620      disposition, so we don't need to mention it here.  */
6621   if (!part_of_multiple && b->enable_count > 1)
6622     {
6623       annotate_field (8);
6624       ui_out_text (uiout, "\tdisable after ");
6625       /* Tweak the wording to clarify that ignore and enable counts
6626          are distinct, and have additive effect.  */
6627       if (b->ignore_count)
6628         ui_out_text (uiout, "additional ");
6629       else
6630         ui_out_text (uiout, "next ");
6631       ui_out_field_int (uiout, "enable", b->enable_count);
6632       ui_out_text (uiout, " hits\n");
6633     }
6634
6635   if (!part_of_multiple && is_tracepoint (b))
6636     {
6637       struct tracepoint *tp = (struct tracepoint *) b;
6638
6639       if (tp->traceframe_usage)
6640         {
6641           ui_out_text (uiout, "\ttrace buffer usage ");
6642           ui_out_field_int (uiout, "traceframe-usage", tp->traceframe_usage);
6643           ui_out_text (uiout, " bytes\n");
6644         }
6645     }
6646
6647   l = b->commands ? b->commands->commands : NULL;
6648   if (!part_of_multiple && l)
6649     {
6650       struct cleanup *script_chain;
6651
6652       annotate_field (9);
6653       script_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "script");
6654       print_command_lines (uiout, l, 4);
6655       do_cleanups (script_chain);
6656     }
6657
6658   if (is_tracepoint (b))
6659     {
6660       struct tracepoint *t = (struct tracepoint *) b;
6661
6662       if (!part_of_multiple && t->pass_count)
6663         {
6664           annotate_field (10);
6665           ui_out_text (uiout, "\tpass count ");
6666           ui_out_field_int (uiout, "pass", t->pass_count);
6667           ui_out_text (uiout, " \n");
6668         }
6669
6670       /* Don't display it when tracepoint or tracepoint location is
6671          pending.   */
6672       if (!header_of_multiple && loc != NULL && !loc->shlib_disabled)
6673         {
6674           annotate_field (11);
6675
6676           if (ui_out_is_mi_like_p (uiout))
6677             ui_out_field_string (uiout, "installed",
6678                                  loc->inserted ? "y" : "n");
6679           else
6680             {
6681               if (loc->inserted)
6682                 ui_out_text (uiout, "\t");
6683               else
6684                 ui_out_text (uiout, "\tnot ");
6685               ui_out_text (uiout, "installed on target\n");
6686             }
6687         }
6688     }
6689
6690   if (ui_out_is_mi_like_p (uiout) && !part_of_multiple)
6691     {
6692       if (is_watchpoint (b))
6693         {
6694           struct watchpoint *w = (struct watchpoint *) b;
6695
6696           ui_out_field_string (uiout, "original-location", w->exp_string);
6697         }
6698       else if (b->location != NULL
6699                && event_location_to_string (b->location) != NULL)
6700         ui_out_field_string (uiout, "original-location",
6701                              event_location_to_string (b->location));
6702     }
6703 }
6704
6705 static void
6706 print_one_breakpoint (struct breakpoint *b,
6707                       struct bp_location **last_loc, 
6708                       int allflag)
6709 {
6710   struct cleanup *bkpt_chain;
6711   struct ui_out *uiout = current_uiout;
6712
6713   bkpt_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "bkpt");
6714
6715   print_one_breakpoint_location (b, NULL, 0, last_loc, allflag);
6716   do_cleanups (bkpt_chain);
6717
6718   /* If this breakpoint has custom print function,
6719      it's already printed.  Otherwise, print individual
6720      locations, if any.  */
6721   if (b->ops == NULL || b->ops->print_one == NULL)
6722     {
6723       /* If breakpoint has a single location that is disabled, we
6724          print it as if it had several locations, since otherwise it's
6725          hard to represent "breakpoint enabled, location disabled"
6726          situation.
6727
6728          Note that while hardware watchpoints have several locations
6729          internally, that's not a property exposed to user.  */
6730       if (b->loc 
6731           && !is_hardware_watchpoint (b)
6732           && (b->loc->next || !b->loc->enabled))
6733         {
6734           struct bp_location *loc;
6735           int n = 1;
6736
6737           for (loc = b->loc; loc; loc = loc->next, ++n)
6738             {
6739               struct cleanup *inner2 =
6740                 make_cleanup_ui_out_tuple_begin_end (uiout, NULL);
6741               print_one_breakpoint_location (b, loc, n, last_loc, allflag);
6742               do_cleanups (inner2);
6743             }
6744         }
6745     }
6746 }
6747
6748 static int
6749 breakpoint_address_bits (struct breakpoint *b)
6750 {
6751   int print_address_bits = 0;
6752   struct bp_location *loc;
6753
6754   /* Software watchpoints that aren't watching memory don't have an
6755      address to print.  */
6756   if (is_no_memory_software_watchpoint (b))
6757     return 0;
6758
6759   for (loc = b->loc; loc; loc = loc->next)
6760     {
6761       int addr_bit;
6762
6763       addr_bit = gdbarch_addr_bit (loc->gdbarch);
6764       if (addr_bit > print_address_bits)
6765         print_address_bits = addr_bit;
6766     }
6767
6768   return print_address_bits;
6769 }
6770
6771 struct captured_breakpoint_query_args
6772   {
6773     int bnum;
6774   };
6775
6776 static int
6777 do_captured_breakpoint_query (struct ui_out *uiout, void *data)
6778 {
6779   struct captured_breakpoint_query_args *args
6780     = (struct captured_breakpoint_query_args *) data;
6781   struct breakpoint *b;
6782   struct bp_location *dummy_loc = NULL;
6783
6784   ALL_BREAKPOINTS (b)
6785     {
6786       if (args->bnum == b->number)
6787         {
6788           print_one_breakpoint (b, &dummy_loc, 0);
6789           return GDB_RC_OK;
6790         }
6791     }
6792   return GDB_RC_NONE;
6793 }
6794
6795 enum gdb_rc
6796 gdb_breakpoint_query (struct ui_out *uiout, int bnum, 
6797                       char **error_message)
6798 {
6799   struct captured_breakpoint_query_args args;
6800
6801   args.bnum = bnum;
6802   /* For the moment we don't trust print_one_breakpoint() to not throw
6803      an error.  */
6804   if (catch_exceptions_with_msg (uiout, do_captured_breakpoint_query, &args,
6805                                  error_message, RETURN_MASK_ALL) < 0)
6806     return GDB_RC_FAIL;
6807   else
6808     return GDB_RC_OK;
6809 }
6810
6811 /* Return true if this breakpoint was set by the user, false if it is
6812    internal or momentary.  */
6813
6814 int
6815 user_breakpoint_p (struct breakpoint *b)
6816 {
6817   return b->number > 0;
6818 }
6819
6820 /* See breakpoint.h.  */
6821
6822 int
6823 pending_breakpoint_p (struct breakpoint *b)
6824 {
6825   return b->loc == NULL;
6826 }
6827
6828 /* Print information on user settable breakpoint (watchpoint, etc)
6829    number BNUM.  If BNUM is -1 print all user-settable breakpoints.
6830    If ALLFLAG is non-zero, include non-user-settable breakpoints.  If
6831    FILTER is non-NULL, call it on each breakpoint and only include the
6832    ones for which it returns non-zero.  Return the total number of
6833    breakpoints listed.  */
6834
6835 static int
6836 breakpoint_1 (char *args, int allflag, 
6837               int (*filter) (const struct breakpoint *))
6838 {
6839   struct breakpoint *b;
6840   struct bp_location *last_loc = NULL;
6841   int nr_printable_breakpoints;
6842   struct cleanup *bkpttbl_chain;
6843   struct value_print_options opts;
6844   int print_address_bits = 0;
6845   int print_type_col_width = 14;
6846   struct ui_out *uiout = current_uiout;
6847
6848   get_user_print_options (&opts);
6849
6850   /* Compute the number of rows in the table, as well as the size
6851      required for address fields.  */
6852   nr_printable_breakpoints = 0;
6853   ALL_BREAKPOINTS (b)
6854     {
6855       /* If we have a filter, only list the breakpoints it accepts.  */
6856       if (filter && !filter (b))
6857         continue;
6858
6859       /* If we have an "args" string, it is a list of breakpoints to 
6860          accept.  Skip the others.  */
6861       if (args != NULL && *args != '\0')
6862         {
6863           if (allflag && parse_and_eval_long (args) != b->number)
6864             continue;
6865           if (!allflag && !number_is_in_list (args, b->number))
6866             continue;
6867         }
6868
6869       if (allflag || user_breakpoint_p (b))
6870         {
6871           int addr_bit, type_len;
6872
6873           addr_bit = breakpoint_address_bits (b);
6874           if (addr_bit > print_address_bits)
6875             print_address_bits = addr_bit;
6876
6877           type_len = strlen (bptype_string (b->type));
6878           if (type_len > print_type_col_width)
6879             print_type_col_width = type_len;
6880
6881           nr_printable_breakpoints++;
6882         }
6883     }
6884
6885   if (opts.addressprint)
6886     bkpttbl_chain 
6887       = make_cleanup_ui_out_table_begin_end (uiout, 6,
6888                                              nr_printable_breakpoints,
6889                                              "BreakpointTable");
6890   else
6891     bkpttbl_chain 
6892       = make_cleanup_ui_out_table_begin_end (uiout, 5,
6893                                              nr_printable_breakpoints,
6894                                              "BreakpointTable");
6895
6896   if (nr_printable_breakpoints > 0)
6897     annotate_breakpoints_headers ();
6898   if (nr_printable_breakpoints > 0)
6899     annotate_field (0);
6900   ui_out_table_header (uiout, 7, ui_left, "number", "Num");     /* 1 */
6901   if (nr_printable_breakpoints > 0)
6902     annotate_field (1);
6903   ui_out_table_header (uiout, print_type_col_width, ui_left,
6904                        "type", "Type");                         /* 2 */
6905   if (nr_printable_breakpoints > 0)
6906     annotate_field (2);
6907   ui_out_table_header (uiout, 4, ui_left, "disp", "Disp");      /* 3 */
6908   if (nr_printable_breakpoints > 0)
6909     annotate_field (3);
6910   ui_out_table_header (uiout, 3, ui_left, "enabled", "Enb");    /* 4 */
6911   if (opts.addressprint)
6912     {
6913       if (nr_printable_breakpoints > 0)
6914         annotate_field (4);
6915       if (print_address_bits <= 32)
6916         ui_out_table_header (uiout, 10, ui_left, 
6917                              "addr", "Address");                /* 5 */
6918       else
6919         ui_out_table_header (uiout, 18, ui_left, 
6920                              "addr", "Address");                /* 5 */
6921     }
6922   if (nr_printable_breakpoints > 0)
6923     annotate_field (5);
6924   ui_out_table_header (uiout, 40, ui_noalign, "what", "What");  /* 6 */
6925   ui_out_table_body (uiout);
6926   if (nr_printable_breakpoints > 0)
6927     annotate_breakpoints_table ();
6928
6929   ALL_BREAKPOINTS (b)
6930     {
6931       QUIT;
6932       /* If we have a filter, only list the breakpoints it accepts.  */
6933       if (filter && !filter (b))
6934         continue;
6935
6936       /* If we have an "args" string, it is a list of breakpoints to 
6937          accept.  Skip the others.  */
6938
6939       if (args != NULL && *args != '\0')
6940         {
6941           if (allflag)  /* maintenance info breakpoint */
6942             {
6943               if (parse_and_eval_long (args) != b->number)
6944                 continue;
6945             }
6946           else          /* all others */
6947             {
6948               if (!number_is_in_list (args, b->number))
6949                 continue;
6950             }
6951         }
6952       /* We only print out user settable breakpoints unless the
6953          allflag is set.  */
6954       if (allflag || user_breakpoint_p (b))
6955         print_one_breakpoint (b, &last_loc, allflag);
6956     }
6957
6958   do_cleanups (bkpttbl_chain);
6959
6960   if (nr_printable_breakpoints == 0)
6961     {
6962       /* If there's a filter, let the caller decide how to report
6963          empty list.  */
6964       if (!filter)
6965         {
6966           if (args == NULL || *args == '\0')
6967             ui_out_message (uiout, 0, "No breakpoints or watchpoints.\n");
6968           else
6969             ui_out_message (uiout, 0, 
6970                             "No breakpoint or watchpoint matching '%s'.\n",
6971                             args);
6972         }
6973     }
6974   else
6975     {
6976       if (last_loc && !server_command)
6977         set_next_address (last_loc->gdbarch, last_loc->address);
6978     }
6979
6980   /* FIXME?  Should this be moved up so that it is only called when
6981      there have been breakpoints? */
6982   annotate_breakpoints_table_end ();
6983
6984   return nr_printable_breakpoints;
6985 }
6986
6987 /* Display the value of default-collect in a way that is generally
6988    compatible with the breakpoint list.  */
6989
6990 static void
6991 default_collect_info (void)
6992 {
6993   struct ui_out *uiout = current_uiout;
6994
6995   /* If it has no value (which is frequently the case), say nothing; a
6996      message like "No default-collect." gets in user's face when it's
6997      not wanted.  */
6998   if (!*default_collect)
6999     return;
7000
7001   /* The following phrase lines up nicely with per-tracepoint collect
7002      actions.  */
7003   ui_out_text (uiout, "default collect ");
7004   ui_out_field_string (uiout, "default-collect", default_collect);
7005   ui_out_text (uiout, " \n");
7006 }
7007   
7008 static void
7009 breakpoints_info (char *args, int from_tty)
7010 {
7011   breakpoint_1 (args, 0, NULL);
7012
7013   default_collect_info ();
7014 }
7015
7016 static void
7017 watchpoints_info (char *args, int from_tty)
7018 {
7019   int num_printed = breakpoint_1 (args, 0, is_watchpoint);
7020   struct ui_out *uiout = current_uiout;
7021
7022   if (num_printed == 0)
7023     {
7024       if (args == NULL || *args == '\0')
7025         ui_out_message (uiout, 0, "No watchpoints.\n");
7026       else
7027         ui_out_message (uiout, 0, "No watchpoint matching '%s'.\n", args);
7028     }
7029 }
7030
7031 static void
7032 maintenance_info_breakpoints (char *args, int from_tty)
7033 {
7034   breakpoint_1 (args, 1, NULL);
7035
7036   default_collect_info ();
7037 }
7038
7039 static int
7040 breakpoint_has_pc (struct breakpoint *b,
7041                    struct program_space *pspace,
7042                    CORE_ADDR pc, struct obj_section *section)
7043 {
7044   struct bp_location *bl = b->loc;
7045
7046   for (; bl; bl = bl->next)
7047     {
7048       if (bl->pspace == pspace
7049           && bl->address == pc
7050           && (!overlay_debugging || bl->section == section))
7051         return 1;         
7052     }
7053   return 0;
7054 }
7055
7056 /* Print a message describing any user-breakpoints set at PC.  This
7057    concerns with logical breakpoints, so we match program spaces, not
7058    address spaces.  */
7059
7060 static void
7061 describe_other_breakpoints (struct gdbarch *gdbarch,
7062                             struct program_space *pspace, CORE_ADDR pc,
7063                             struct obj_section *section, int thread)
7064 {
7065   int others = 0;
7066   struct breakpoint *b;
7067
7068   ALL_BREAKPOINTS (b)
7069     others += (user_breakpoint_p (b)
7070                && breakpoint_has_pc (b, pspace, pc, section));
7071   if (others > 0)
7072     {
7073       if (others == 1)
7074         printf_filtered (_("Note: breakpoint "));
7075       else /* if (others == ???) */
7076         printf_filtered (_("Note: breakpoints "));
7077       ALL_BREAKPOINTS (b)
7078         if (user_breakpoint_p (b) && breakpoint_has_pc (b, pspace, pc, section))
7079           {
7080             others--;
7081             printf_filtered ("%d", b->number);
7082             if (b->thread == -1 && thread != -1)
7083               printf_filtered (" (all threads)");
7084             else if (b->thread != -1)
7085               printf_filtered (" (thread %d)", b->thread);
7086             printf_filtered ("%s%s ",
7087                              ((b->enable_state == bp_disabled
7088                                || b->enable_state == bp_call_disabled)
7089                               ? " (disabled)"
7090                               : ""),
7091                              (others > 1) ? "," 
7092                              : ((others == 1) ? " and" : ""));
7093           }
7094       printf_filtered (_("also set at pc "));
7095       fputs_filtered (paddress (gdbarch, pc), gdb_stdout);
7096       printf_filtered (".\n");
7097     }
7098 }
7099 \f
7100
7101 /* Return true iff it is meaningful to use the address member of
7102    BPT locations.  For some breakpoint types, the locations' address members
7103    are irrelevant and it makes no sense to attempt to compare them to other
7104    addresses (or use them for any other purpose either).
7105
7106    More specifically, each of the following breakpoint types will
7107    always have a zero valued location address and we don't want to mark
7108    breakpoints of any of these types to be a duplicate of an actual
7109    breakpoint location at address zero:
7110
7111       bp_watchpoint
7112       bp_catchpoint
7113
7114 */
7115
7116 static int
7117 breakpoint_address_is_meaningful (struct breakpoint *bpt)
7118 {
7119   enum bptype type = bpt->type;
7120
7121   return (type != bp_watchpoint && type != bp_catchpoint);
7122 }
7123
7124 /* Assuming LOC1 and LOC2's owners are hardware watchpoints, returns
7125    true if LOC1 and LOC2 represent the same watchpoint location.  */
7126
7127 static int
7128 watchpoint_locations_match (struct bp_location *loc1, 
7129                             struct bp_location *loc2)
7130 {
7131   struct watchpoint *w1 = (struct watchpoint *) loc1->owner;
7132   struct watchpoint *w2 = (struct watchpoint *) loc2->owner;
7133
7134   /* Both of them must exist.  */
7135   gdb_assert (w1 != NULL);
7136   gdb_assert (w2 != NULL);
7137
7138   /* If the target can evaluate the condition expression in hardware,
7139      then we we need to insert both watchpoints even if they are at
7140      the same place.  Otherwise the watchpoint will only trigger when
7141      the condition of whichever watchpoint was inserted evaluates to
7142      true, not giving a chance for GDB to check the condition of the
7143      other watchpoint.  */
7144   if ((w1->cond_exp
7145        && target_can_accel_watchpoint_condition (loc1->address, 
7146                                                  loc1->length,
7147                                                  loc1->watchpoint_type,
7148                                                  w1->cond_exp))
7149       || (w2->cond_exp
7150           && target_can_accel_watchpoint_condition (loc2->address, 
7151                                                     loc2->length,
7152                                                     loc2->watchpoint_type,
7153                                                     w2->cond_exp)))
7154     return 0;
7155
7156   /* Note that this checks the owner's type, not the location's.  In
7157      case the target does not support read watchpoints, but does
7158      support access watchpoints, we'll have bp_read_watchpoint
7159      watchpoints with hw_access locations.  Those should be considered
7160      duplicates of hw_read locations.  The hw_read locations will
7161      become hw_access locations later.  */
7162   return (loc1->owner->type == loc2->owner->type
7163           && loc1->pspace->aspace == loc2->pspace->aspace
7164           && loc1->address == loc2->address
7165           && loc1->length == loc2->length);
7166 }
7167
7168 /* See breakpoint.h.  */
7169
7170 int
7171 breakpoint_address_match (struct address_space *aspace1, CORE_ADDR addr1,
7172                           struct address_space *aspace2, CORE_ADDR addr2)
7173 {
7174   return ((gdbarch_has_global_breakpoints (target_gdbarch ())
7175            || aspace1 == aspace2)
7176           && addr1 == addr2);
7177 }
7178
7179 /* Returns true if {ASPACE2,ADDR2} falls within the range determined by
7180    {ASPACE1,ADDR1,LEN1}.  In most targets, this can only be true if ASPACE1
7181    matches ASPACE2.  On targets that have global breakpoints, the address
7182    space doesn't really matter.  */
7183
7184 static int
7185 breakpoint_address_match_range (struct address_space *aspace1, CORE_ADDR addr1,
7186                                 int len1, struct address_space *aspace2,
7187                                 CORE_ADDR addr2)
7188 {
7189   return ((gdbarch_has_global_breakpoints (target_gdbarch ())
7190            || aspace1 == aspace2)
7191           && addr2 >= addr1 && addr2 < addr1 + len1);
7192 }
7193
7194 /* Returns true if {ASPACE,ADDR} matches the breakpoint BL.  BL may be
7195    a ranged breakpoint.  In most targets, a match happens only if ASPACE
7196    matches the breakpoint's address space.  On targets that have global
7197    breakpoints, the address space doesn't really matter.  */
7198
7199 static int
7200 breakpoint_location_address_match (struct bp_location *bl,
7201                                    struct address_space *aspace,
7202                                    CORE_ADDR addr)
7203 {
7204   return (breakpoint_address_match (bl->pspace->aspace, bl->address,
7205                                     aspace, addr)
7206           || (bl->length
7207               && breakpoint_address_match_range (bl->pspace->aspace,
7208                                                  bl->address, bl->length,
7209                                                  aspace, addr)));
7210 }
7211
7212 /* Returns true if the [ADDR,ADDR+LEN) range in ASPACE overlaps
7213    breakpoint BL.  BL may be a ranged breakpoint.  In most targets, a
7214    match happens only if ASPACE matches the breakpoint's address
7215    space.  On targets that have global breakpoints, the address space
7216    doesn't really matter.  */
7217
7218 static int
7219 breakpoint_location_address_range_overlap (struct bp_location *bl,
7220                                            struct address_space *aspace,
7221                                            CORE_ADDR addr, int len)
7222 {
7223   if (gdbarch_has_global_breakpoints (target_gdbarch ())
7224       || bl->pspace->aspace == aspace)
7225     {
7226       int bl_len = bl->length != 0 ? bl->length : 1;
7227
7228       if (mem_ranges_overlap (addr, len, bl->address, bl_len))
7229         return 1;
7230     }
7231   return 0;
7232 }
7233
7234 /* If LOC1 and LOC2's owners are not tracepoints, returns false directly.
7235    Then, if LOC1 and LOC2 represent the same tracepoint location, returns
7236    true, otherwise returns false.  */
7237
7238 static int
7239 tracepoint_locations_match (struct bp_location *loc1,
7240                             struct bp_location *loc2)
7241 {
7242   if (is_tracepoint (loc1->owner) && is_tracepoint (loc2->owner))
7243     /* Since tracepoint locations are never duplicated with others', tracepoint
7244        locations at the same address of different tracepoints are regarded as
7245        different locations.  */
7246     return (loc1->address == loc2->address && loc1->owner == loc2->owner);
7247   else
7248     return 0;
7249 }
7250
7251 /* Assuming LOC1 and LOC2's types' have meaningful target addresses
7252    (breakpoint_address_is_meaningful), returns true if LOC1 and LOC2
7253    represent the same location.  */
7254
7255 static int
7256 breakpoint_locations_match (struct bp_location *loc1, 
7257                             struct bp_location *loc2)
7258 {
7259   int hw_point1, hw_point2;
7260
7261   /* Both of them must not be in moribund_locations.  */
7262   gdb_assert (loc1->owner != NULL);
7263   gdb_assert (loc2->owner != NULL);
7264
7265   hw_point1 = is_hardware_watchpoint (loc1->owner);
7266   hw_point2 = is_hardware_watchpoint (loc2->owner);
7267
7268   if (hw_point1 != hw_point2)
7269     return 0;
7270   else if (hw_point1)
7271     return watchpoint_locations_match (loc1, loc2);
7272   else if (is_tracepoint (loc1->owner) || is_tracepoint (loc2->owner))
7273     return tracepoint_locations_match (loc1, loc2);
7274   else
7275     /* We compare bp_location.length in order to cover ranged breakpoints.  */
7276     return (breakpoint_address_match (loc1->pspace->aspace, loc1->address,
7277                                      loc2->pspace->aspace, loc2->address)
7278             && loc1->length == loc2->length);
7279 }
7280
7281 static void
7282 breakpoint_adjustment_warning (CORE_ADDR from_addr, CORE_ADDR to_addr,
7283                                int bnum, int have_bnum)
7284 {
7285   /* The longest string possibly returned by hex_string_custom
7286      is 50 chars.  These must be at least that big for safety.  */
7287   char astr1[64];
7288   char astr2[64];
7289
7290   strcpy (astr1, hex_string_custom ((unsigned long) from_addr, 8));
7291   strcpy (astr2, hex_string_custom ((unsigned long) to_addr, 8));
7292   if (have_bnum)
7293     warning (_("Breakpoint %d address previously adjusted from %s to %s."),
7294              bnum, astr1, astr2);
7295   else
7296     warning (_("Breakpoint address adjusted from %s to %s."), astr1, astr2);
7297 }
7298
7299 /* Adjust a breakpoint's address to account for architectural
7300    constraints on breakpoint placement.  Return the adjusted address.
7301    Note: Very few targets require this kind of adjustment.  For most
7302    targets, this function is simply the identity function.  */
7303
7304 static CORE_ADDR
7305 adjust_breakpoint_address (struct gdbarch *gdbarch,
7306                            CORE_ADDR bpaddr, enum bptype bptype)
7307 {
7308   if (!gdbarch_adjust_breakpoint_address_p (gdbarch))
7309     {
7310       /* Very few targets need any kind of breakpoint adjustment.  */
7311       return bpaddr;
7312     }
7313   else if (bptype == bp_watchpoint
7314            || bptype == bp_hardware_watchpoint
7315            || bptype == bp_read_watchpoint
7316            || bptype == bp_access_watchpoint
7317            || bptype == bp_catchpoint)
7318     {
7319       /* Watchpoints and the various bp_catch_* eventpoints should not
7320          have their addresses modified.  */
7321       return bpaddr;
7322     }
7323   else if (bptype == bp_single_step)
7324     {
7325       /* Single-step breakpoints should not have their addresses
7326          modified.  If there's any architectural constrain that
7327          applies to this address, then it should have already been
7328          taken into account when the breakpoint was created in the
7329          first place.  If we didn't do this, stepping through e.g.,
7330          Thumb-2 IT blocks would break.  */
7331       return bpaddr;
7332     }
7333   else
7334     {
7335       CORE_ADDR adjusted_bpaddr;
7336
7337       /* Some targets have architectural constraints on the placement
7338          of breakpoint instructions.  Obtain the adjusted address.  */
7339       adjusted_bpaddr = gdbarch_adjust_breakpoint_address (gdbarch, bpaddr);
7340
7341       /* An adjusted breakpoint address can significantly alter
7342          a user's expectations.  Print a warning if an adjustment
7343          is required.  */
7344       if (adjusted_bpaddr != bpaddr)
7345         breakpoint_adjustment_warning (bpaddr, adjusted_bpaddr, 0, 0);
7346
7347       return adjusted_bpaddr;
7348     }
7349 }
7350
7351 void
7352 init_bp_location (struct bp_location *loc, const struct bp_location_ops *ops,
7353                   struct breakpoint *owner)
7354 {
7355   memset (loc, 0, sizeof (*loc));
7356
7357   gdb_assert (ops != NULL);
7358
7359   loc->ops = ops;
7360   loc->owner = owner;
7361   loc->cond = NULL;
7362   loc->cond_bytecode = NULL;
7363   loc->shlib_disabled = 0;
7364   loc->enabled = 1;
7365
7366   switch (owner->type)
7367     {
7368     case bp_breakpoint:
7369     case bp_single_step:
7370     case bp_until:
7371     case bp_finish:
7372     case bp_longjmp:
7373     case bp_longjmp_resume:
7374     case bp_longjmp_call_dummy:
7375     case bp_exception:
7376     case bp_exception_resume:
7377     case bp_step_resume:
7378     case bp_hp_step_resume:
7379     case bp_watchpoint_scope:
7380     case bp_call_dummy:
7381     case bp_std_terminate:
7382     case bp_shlib_event:
7383     case bp_thread_event:
7384     case bp_overlay_event:
7385     case bp_jit_event:
7386     case bp_longjmp_master:
7387     case bp_std_terminate_master:
7388     case bp_exception_master:
7389     case bp_gnu_ifunc_resolver:
7390     case bp_gnu_ifunc_resolver_return:
7391     case bp_dprintf:
7392       loc->loc_type = bp_loc_software_breakpoint;
7393       mark_breakpoint_location_modified (loc);
7394       break;
7395     case bp_hardware_breakpoint:
7396       loc->loc_type = bp_loc_hardware_breakpoint;
7397       mark_breakpoint_location_modified (loc);
7398       break;
7399     case bp_hardware_watchpoint:
7400     case bp_read_watchpoint:
7401     case bp_access_watchpoint:
7402       loc->loc_type = bp_loc_hardware_watchpoint;
7403       break;
7404     case bp_watchpoint:
7405     case bp_catchpoint:
7406     case bp_tracepoint:
7407     case bp_fast_tracepoint:
7408     case bp_static_tracepoint:
7409       loc->loc_type = bp_loc_other;
7410       break;
7411     default:
7412       internal_error (__FILE__, __LINE__, _("unknown breakpoint type"));
7413     }
7414
7415   loc->refc = 1;
7416 }
7417
7418 /* Allocate a struct bp_location.  */
7419
7420 static struct bp_location *
7421 allocate_bp_location (struct breakpoint *bpt)
7422 {
7423   return bpt->ops->allocate_location (bpt);
7424 }
7425
7426 static void
7427 free_bp_location (struct bp_location *loc)
7428 {
7429   loc->ops->dtor (loc);
7430   xfree (loc);
7431 }
7432
7433 /* Increment reference count.  */
7434
7435 static void
7436 incref_bp_location (struct bp_location *bl)
7437 {
7438   ++bl->refc;
7439 }
7440
7441 /* Decrement reference count.  If the reference count reaches 0,
7442    destroy the bp_location.  Sets *BLP to NULL.  */
7443
7444 static void
7445 decref_bp_location (struct bp_location **blp)
7446 {
7447   gdb_assert ((*blp)->refc > 0);
7448
7449   if (--(*blp)->refc == 0)
7450     free_bp_location (*blp);
7451   *blp = NULL;
7452 }
7453
7454 /* Add breakpoint B at the end of the global breakpoint chain.  */
7455
7456 static void
7457 add_to_breakpoint_chain (struct breakpoint *b)
7458 {
7459   struct breakpoint *b1;
7460
7461   /* Add this breakpoint to the end of the chain so that a list of
7462      breakpoints will come out in order of increasing numbers.  */
7463
7464   b1 = breakpoint_chain;
7465   if (b1 == 0)
7466     breakpoint_chain = b;
7467   else
7468     {
7469       while (b1->next)
7470         b1 = b1->next;
7471       b1->next = b;
7472     }
7473 }
7474
7475 /* Initializes breakpoint B with type BPTYPE and no locations yet.  */
7476
7477 static void
7478 init_raw_breakpoint_without_location (struct breakpoint *b,
7479                                       struct gdbarch *gdbarch,
7480                                       enum bptype bptype,
7481                                       const struct breakpoint_ops *ops)
7482 {
7483   memset (b, 0, sizeof (*b));
7484
7485   gdb_assert (ops != NULL);
7486
7487   b->ops = ops;
7488   b->type = bptype;
7489   b->gdbarch = gdbarch;
7490   b->language = current_language->la_language;
7491   b->input_radix = input_radix;
7492   b->thread = -1;
7493   b->enable_state = bp_enabled;
7494   b->next = 0;
7495   b->silent = 0;
7496   b->ignore_count = 0;
7497   b->commands = NULL;
7498   b->frame_id = null_frame_id;
7499   b->condition_not_parsed = 0;
7500   b->py_bp_object = NULL;
7501   b->related_breakpoint = b;
7502   b->location = NULL;
7503 }
7504
7505 /* Helper to set_raw_breakpoint below.  Creates a breakpoint
7506    that has type BPTYPE and has no locations as yet.  */
7507
7508 static struct breakpoint *
7509 set_raw_breakpoint_without_location (struct gdbarch *gdbarch,
7510                                      enum bptype bptype,
7511                                      const struct breakpoint_ops *ops)
7512 {
7513   struct breakpoint *b = XNEW (struct breakpoint);
7514
7515   init_raw_breakpoint_without_location (b, gdbarch, bptype, ops);
7516   add_to_breakpoint_chain (b);
7517   return b;
7518 }
7519
7520 /* Initialize loc->function_name.  EXPLICIT_LOC says no indirect function
7521    resolutions should be made as the user specified the location explicitly
7522    enough.  */
7523
7524 static void
7525 set_breakpoint_location_function (struct bp_location *loc, int explicit_loc)
7526 {
7527   gdb_assert (loc->owner != NULL);
7528
7529   if (loc->owner->type == bp_breakpoint
7530       || loc->owner->type == bp_hardware_breakpoint
7531       || is_tracepoint (loc->owner))
7532     {
7533       int is_gnu_ifunc;
7534       const char *function_name;
7535       CORE_ADDR func_addr;
7536
7537       find_pc_partial_function_gnu_ifunc (loc->address, &function_name,
7538                                           &func_addr, NULL, &is_gnu_ifunc);
7539
7540       if (is_gnu_ifunc && !explicit_loc)
7541         {
7542           struct breakpoint *b = loc->owner;
7543
7544           gdb_assert (loc->pspace == current_program_space);
7545           if (gnu_ifunc_resolve_name (function_name,
7546                                       &loc->requested_address))
7547             {
7548               /* Recalculate ADDRESS based on new REQUESTED_ADDRESS.  */
7549               loc->address = adjust_breakpoint_address (loc->gdbarch,
7550                                                         loc->requested_address,
7551                                                         b->type);
7552             }
7553           else if (b->type == bp_breakpoint && b->loc == loc
7554                    && loc->next == NULL && b->related_breakpoint == b)
7555             {
7556               /* Create only the whole new breakpoint of this type but do not
7557                  mess more complicated breakpoints with multiple locations.  */
7558               b->type = bp_gnu_ifunc_resolver;
7559               /* Remember the resolver's address for use by the return
7560                  breakpoint.  */
7561               loc->related_address = func_addr;
7562             }
7563         }
7564
7565       if (function_name)
7566         loc->function_name = xstrdup (function_name);
7567     }
7568 }
7569
7570 /* Attempt to determine architecture of location identified by SAL.  */
7571 struct gdbarch *
7572 get_sal_arch (struct symtab_and_line sal)
7573 {
7574   if (sal.section)
7575     return get_objfile_arch (sal.section->objfile);
7576   if (sal.symtab)
7577     return get_objfile_arch (SYMTAB_OBJFILE (sal.symtab));
7578
7579   return NULL;
7580 }
7581
7582 /* Low level routine for partially initializing a breakpoint of type
7583    BPTYPE.  The newly created breakpoint's address, section, source
7584    file name, and line number are provided by SAL.
7585
7586    It is expected that the caller will complete the initialization of
7587    the newly created breakpoint struct as well as output any status
7588    information regarding the creation of a new breakpoint.  */
7589
7590 static void
7591 init_raw_breakpoint (struct breakpoint *b, struct gdbarch *gdbarch,
7592                      struct symtab_and_line sal, enum bptype bptype,
7593                      const struct breakpoint_ops *ops)
7594 {
7595   init_raw_breakpoint_without_location (b, gdbarch, bptype, ops);
7596
7597   add_location_to_breakpoint (b, &sal);
7598
7599   if (bptype != bp_catchpoint)
7600     gdb_assert (sal.pspace != NULL);
7601
7602   /* Store the program space that was used to set the breakpoint,
7603      except for ordinary breakpoints, which are independent of the
7604      program space.  */
7605   if (bptype != bp_breakpoint && bptype != bp_hardware_breakpoint)
7606     b->pspace = sal.pspace;
7607 }
7608
7609 /* set_raw_breakpoint is a low level routine for allocating and
7610    partially initializing a breakpoint of type BPTYPE.  The newly
7611    created breakpoint's address, section, source file name, and line
7612    number are provided by SAL.  The newly created and partially
7613    initialized breakpoint is added to the breakpoint chain and
7614    is also returned as the value of this function.
7615
7616    It is expected that the caller will complete the initialization of
7617    the newly created breakpoint struct as well as output any status
7618    information regarding the creation of a new breakpoint.  In
7619    particular, set_raw_breakpoint does NOT set the breakpoint
7620    number!  Care should be taken to not allow an error to occur
7621    prior to completing the initialization of the breakpoint.  If this
7622    should happen, a bogus breakpoint will be left on the chain.  */
7623
7624 struct breakpoint *
7625 set_raw_breakpoint (struct gdbarch *gdbarch,
7626                     struct symtab_and_line sal, enum bptype bptype,
7627                     const struct breakpoint_ops *ops)
7628 {
7629   struct breakpoint *b = XNEW (struct breakpoint);
7630
7631   init_raw_breakpoint (b, gdbarch, sal, bptype, ops);
7632   add_to_breakpoint_chain (b);
7633   return b;
7634 }
7635
7636 /* Call this routine when stepping and nexting to enable a breakpoint
7637    if we do a longjmp() or 'throw' in TP.  FRAME is the frame which
7638    initiated the operation.  */
7639
7640 void
7641 set_longjmp_breakpoint (struct thread_info *tp, struct frame_id frame)
7642 {
7643   struct breakpoint *b, *b_tmp;
7644   int thread = tp->global_num;
7645
7646   /* To avoid having to rescan all objfile symbols at every step,
7647      we maintain a list of continually-inserted but always disabled
7648      longjmp "master" breakpoints.  Here, we simply create momentary
7649      clones of those and enable them for the requested thread.  */
7650   ALL_BREAKPOINTS_SAFE (b, b_tmp)
7651     if (b->pspace == current_program_space
7652         && (b->type == bp_longjmp_master
7653             || b->type == bp_exception_master))
7654       {
7655         enum bptype type = b->type == bp_longjmp_master ? bp_longjmp : bp_exception;
7656         struct breakpoint *clone;
7657
7658         /* longjmp_breakpoint_ops ensures INITIATING_FRAME is cleared again
7659            after their removal.  */
7660         clone = momentary_breakpoint_from_master (b, type,
7661                                                   &longjmp_breakpoint_ops, 1);
7662         clone->thread = thread;
7663       }
7664
7665   tp->initiating_frame = frame;
7666 }
7667
7668 /* Delete all longjmp breakpoints from THREAD.  */
7669 void
7670 delete_longjmp_breakpoint (int thread)
7671 {
7672   struct breakpoint *b, *b_tmp;
7673
7674   ALL_BREAKPOINTS_SAFE (b, b_tmp)
7675     if (b->type == bp_longjmp || b->type == bp_exception)
7676       {
7677         if (b->thread == thread)
7678           delete_breakpoint (b);
7679       }
7680 }
7681
7682 void
7683 delete_longjmp_breakpoint_at_next_stop (int thread)
7684 {
7685   struct breakpoint *b, *b_tmp;
7686
7687   ALL_BREAKPOINTS_SAFE (b, b_tmp)
7688     if (b->type == bp_longjmp || b->type == bp_exception)
7689       {
7690         if (b->thread == thread)
7691           b->disposition = disp_del_at_next_stop;
7692       }
7693 }
7694
7695 /* Place breakpoints of type bp_longjmp_call_dummy to catch longjmp for
7696    INFERIOR_PTID thread.  Chain them all by RELATED_BREAKPOINT and return
7697    pointer to any of them.  Return NULL if this system cannot place longjmp
7698    breakpoints.  */
7699
7700 struct breakpoint *
7701 set_longjmp_breakpoint_for_call_dummy (void)
7702 {
7703   struct breakpoint *b, *retval = NULL;
7704
7705   ALL_BREAKPOINTS (b)
7706     if (b->pspace == current_program_space && b->type == bp_longjmp_master)
7707       {
7708         struct breakpoint *new_b;
7709
7710         new_b = momentary_breakpoint_from_master (b, bp_longjmp_call_dummy,
7711                                                   &momentary_breakpoint_ops,
7712                                                   1);
7713         new_b->thread = ptid_to_global_thread_id (inferior_ptid);
7714
7715         /* Link NEW_B into the chain of RETVAL breakpoints.  */
7716
7717         gdb_assert (new_b->related_breakpoint == new_b);
7718         if (retval == NULL)
7719           retval = new_b;
7720         new_b->related_breakpoint = retval;
7721         while (retval->related_breakpoint != new_b->related_breakpoint)
7722           retval = retval->related_breakpoint;
7723         retval->related_breakpoint = new_b;
7724       }
7725
7726   return retval;
7727 }
7728
7729 /* Verify all existing dummy frames and their associated breakpoints for
7730    TP.  Remove those which can no longer be found in the current frame
7731    stack.
7732
7733    You should call this function only at places where it is safe to currently
7734    unwind the whole stack.  Failed stack unwind would discard live dummy
7735    frames.  */
7736
7737 void
7738 check_longjmp_breakpoint_for_call_dummy (struct thread_info *tp)
7739 {
7740   struct breakpoint *b, *b_tmp;
7741
7742   ALL_BREAKPOINTS_SAFE (b, b_tmp)
7743     if (b->type == bp_longjmp_call_dummy && b->thread == tp->global_num)
7744       {
7745         struct breakpoint *dummy_b = b->related_breakpoint;
7746
7747         while (dummy_b != b && dummy_b->type != bp_call_dummy)
7748           dummy_b = dummy_b->related_breakpoint;
7749         if (dummy_b->type != bp_call_dummy
7750             || frame_find_by_id (dummy_b->frame_id) != NULL)
7751           continue;
7752         
7753         dummy_frame_discard (dummy_b->frame_id, tp->ptid);
7754
7755         while (b->related_breakpoint != b)
7756           {
7757             if (b_tmp == b->related_breakpoint)
7758               b_tmp = b->related_breakpoint->next;
7759             delete_breakpoint (b->related_breakpoint);
7760           }
7761         delete_breakpoint (b);
7762       }
7763 }
7764
7765 void
7766 enable_overlay_breakpoints (void)
7767 {
7768   struct breakpoint *b;
7769
7770   ALL_BREAKPOINTS (b)
7771     if (b->type == bp_overlay_event)
7772     {
7773       b->enable_state = bp_enabled;
7774       update_global_location_list (UGLL_MAY_INSERT);
7775       overlay_events_enabled = 1;
7776     }
7777 }
7778
7779 void
7780 disable_overlay_breakpoints (void)
7781 {
7782   struct breakpoint *b;
7783
7784   ALL_BREAKPOINTS (b)
7785     if (b->type == bp_overlay_event)
7786     {
7787       b->enable_state = bp_disabled;
7788       update_global_location_list (UGLL_DONT_INSERT);
7789       overlay_events_enabled = 0;
7790     }
7791 }
7792
7793 /* Set an active std::terminate breakpoint for each std::terminate
7794    master breakpoint.  */
7795 void
7796 set_std_terminate_breakpoint (void)
7797 {
7798   struct breakpoint *b, *b_tmp;
7799
7800   ALL_BREAKPOINTS_SAFE (b, b_tmp)
7801     if (b->pspace == current_program_space
7802         && b->type == bp_std_terminate_master)
7803       {
7804         momentary_breakpoint_from_master (b, bp_std_terminate,
7805                                           &momentary_breakpoint_ops, 1);
7806       }
7807 }
7808
7809 /* Delete all the std::terminate breakpoints.  */
7810 void
7811 delete_std_terminate_breakpoint (void)
7812 {
7813   struct breakpoint *b, *b_tmp;
7814
7815   ALL_BREAKPOINTS_SAFE (b, b_tmp)
7816     if (b->type == bp_std_terminate)
7817       delete_breakpoint (b);
7818 }
7819
7820 struct breakpoint *
7821 create_thread_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7822 {
7823   struct breakpoint *b;
7824
7825   b = create_internal_breakpoint (gdbarch, address, bp_thread_event,
7826                                   &internal_breakpoint_ops);
7827
7828   b->enable_state = bp_enabled;
7829   /* location has to be used or breakpoint_re_set will delete me.  */
7830   b->location = new_address_location (b->loc->address, NULL, 0);
7831
7832   update_global_location_list_nothrow (UGLL_MAY_INSERT);
7833
7834   return b;
7835 }
7836
7837 struct lang_and_radix
7838   {
7839     enum language lang;
7840     int radix;
7841   };
7842
7843 /* Create a breakpoint for JIT code registration and unregistration.  */
7844
7845 struct breakpoint *
7846 create_jit_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7847 {
7848   return create_internal_breakpoint (gdbarch, address, bp_jit_event,
7849                                      &internal_breakpoint_ops);
7850 }
7851
7852 /* Remove JIT code registration and unregistration breakpoint(s).  */
7853
7854 void
7855 remove_jit_event_breakpoints (void)
7856 {
7857   struct breakpoint *b, *b_tmp;
7858
7859   ALL_BREAKPOINTS_SAFE (b, b_tmp)
7860     if (b->type == bp_jit_event
7861         && b->loc->pspace == current_program_space)
7862       delete_breakpoint (b);
7863 }
7864
7865 void
7866 remove_solib_event_breakpoints (void)
7867 {
7868   struct breakpoint *b, *b_tmp;
7869
7870   ALL_BREAKPOINTS_SAFE (b, b_tmp)
7871     if (b->type == bp_shlib_event
7872         && b->loc->pspace == current_program_space)
7873       delete_breakpoint (b);
7874 }
7875
7876 /* See breakpoint.h.  */
7877
7878 void
7879 remove_solib_event_breakpoints_at_next_stop (void)
7880 {
7881   struct breakpoint *b, *b_tmp;
7882
7883   ALL_BREAKPOINTS_SAFE (b, b_tmp)
7884     if (b->type == bp_shlib_event
7885         && b->loc->pspace == current_program_space)
7886       b->disposition = disp_del_at_next_stop;
7887 }
7888
7889 /* Helper for create_solib_event_breakpoint /
7890    create_and_insert_solib_event_breakpoint.  Allows specifying which
7891    INSERT_MODE to pass through to update_global_location_list.  */
7892
7893 static struct breakpoint *
7894 create_solib_event_breakpoint_1 (struct gdbarch *gdbarch, CORE_ADDR address,
7895                                  enum ugll_insert_mode insert_mode)
7896 {
7897   struct breakpoint *b;
7898
7899   b = create_internal_breakpoint (gdbarch, address, bp_shlib_event,
7900                                   &internal_breakpoint_ops);
7901   update_global_location_list_nothrow (insert_mode);
7902   return b;
7903 }
7904
7905 struct breakpoint *
7906 create_solib_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7907 {
7908   return create_solib_event_breakpoint_1 (gdbarch, address, UGLL_MAY_INSERT);
7909 }
7910
7911 /* See breakpoint.h.  */
7912
7913 struct breakpoint *
7914 create_and_insert_solib_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7915 {
7916   struct breakpoint *b;
7917
7918   /* Explicitly tell update_global_location_list to insert
7919      locations.  */
7920   b = create_solib_event_breakpoint_1 (gdbarch, address, UGLL_INSERT);
7921   if (!b->loc->inserted)
7922     {
7923       delete_breakpoint (b);
7924       return NULL;
7925     }
7926   return b;
7927 }
7928
7929 /* Disable any breakpoints that are on code in shared libraries.  Only
7930    apply to enabled breakpoints, disabled ones can just stay disabled.  */
7931
7932 void
7933 disable_breakpoints_in_shlibs (void)
7934 {
7935   struct bp_location *loc, **locp_tmp;
7936
7937   ALL_BP_LOCATIONS (loc, locp_tmp)
7938   {
7939     /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL.  */
7940     struct breakpoint *b = loc->owner;
7941
7942     /* We apply the check to all breakpoints, including disabled for
7943        those with loc->duplicate set.  This is so that when breakpoint
7944        becomes enabled, or the duplicate is removed, gdb will try to
7945        insert all breakpoints.  If we don't set shlib_disabled here,
7946        we'll try to insert those breakpoints and fail.  */
7947     if (((b->type == bp_breakpoint)
7948          || (b->type == bp_jit_event)
7949          || (b->type == bp_hardware_breakpoint)
7950          || (is_tracepoint (b)))
7951         && loc->pspace == current_program_space
7952         && !loc->shlib_disabled
7953         && solib_name_from_address (loc->pspace, loc->address)
7954         )
7955       {
7956         loc->shlib_disabled = 1;
7957       }
7958   }
7959 }
7960
7961 /* Disable any breakpoints and tracepoints that are in SOLIB upon
7962    notification of unloaded_shlib.  Only apply to enabled breakpoints,
7963    disabled ones can just stay disabled.  */
7964
7965 static void
7966 disable_breakpoints_in_unloaded_shlib (struct so_list *solib)
7967 {
7968   struct bp_location *loc, **locp_tmp;
7969   int disabled_shlib_breaks = 0;
7970
7971   /* SunOS a.out shared libraries are always mapped, so do not
7972      disable breakpoints; they will only be reported as unloaded
7973      through clear_solib when GDB discards its shared library
7974      list.  See clear_solib for more information.  */
7975   if (exec_bfd != NULL
7976       && bfd_get_flavour (exec_bfd) == bfd_target_aout_flavour)
7977     return;
7978
7979   ALL_BP_LOCATIONS (loc, locp_tmp)
7980   {
7981     /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL.  */
7982     struct breakpoint *b = loc->owner;
7983
7984     if (solib->pspace == loc->pspace
7985         && !loc->shlib_disabled
7986         && (((b->type == bp_breakpoint
7987               || b->type == bp_jit_event
7988               || b->type == bp_hardware_breakpoint)
7989              && (loc->loc_type == bp_loc_hardware_breakpoint
7990                  || loc->loc_type == bp_loc_software_breakpoint))
7991             || is_tracepoint (b))
7992         && solib_contains_address_p (solib, loc->address))
7993       {
7994         loc->shlib_disabled = 1;
7995         /* At this point, we cannot rely on remove_breakpoint
7996            succeeding so we must mark the breakpoint as not inserted
7997            to prevent future errors occurring in remove_breakpoints.  */
7998         loc->inserted = 0;
7999
8000         /* This may cause duplicate notifications for the same breakpoint.  */
8001         observer_notify_breakpoint_modified (b);
8002
8003         if (!disabled_shlib_breaks)
8004           {
8005             target_terminal_ours_for_output ();
8006             warning (_("Temporarily disabling breakpoints "
8007                        "for unloaded shared library \"%s\""),
8008                      solib->so_name);
8009           }
8010         disabled_shlib_breaks = 1;
8011       }
8012   }
8013 }
8014
8015 /* Disable any breakpoints and tracepoints in OBJFILE upon
8016    notification of free_objfile.  Only apply to enabled breakpoints,
8017    disabled ones can just stay disabled.  */
8018
8019 static void
8020 disable_breakpoints_in_freed_objfile (struct objfile *objfile)
8021 {
8022   struct breakpoint *b;
8023
8024   if (objfile == NULL)
8025     return;
8026
8027   /* OBJF_SHARED|OBJF_USERLOADED objfiles are dynamic modules manually
8028      managed by the user with add-symbol-file/remove-symbol-file.
8029      Similarly to how breakpoints in shared libraries are handled in
8030      response to "nosharedlibrary", mark breakpoints in such modules
8031      shlib_disabled so they end up uninserted on the next global
8032      location list update.  Shared libraries not loaded by the user
8033      aren't handled here -- they're already handled in
8034      disable_breakpoints_in_unloaded_shlib, called by solib.c's
8035      solib_unloaded observer.  We skip objfiles that are not
8036      OBJF_SHARED as those aren't considered dynamic objects (e.g. the
8037      main objfile).  */
8038   if ((objfile->flags & OBJF_SHARED) == 0
8039       || (objfile->flags & OBJF_USERLOADED) == 0)
8040     return;
8041
8042   ALL_BREAKPOINTS (b)
8043     {
8044       struct bp_location *loc;
8045       int bp_modified = 0;
8046
8047       if (!is_breakpoint (b) && !is_tracepoint (b))
8048         continue;
8049
8050       for (loc = b->loc; loc != NULL; loc = loc->next)
8051         {
8052           CORE_ADDR loc_addr = loc->address;
8053
8054           if (loc->loc_type != bp_loc_hardware_breakpoint
8055               && loc->loc_type != bp_loc_software_breakpoint)
8056             continue;
8057
8058           if (loc->shlib_disabled != 0)
8059             continue;
8060
8061           if (objfile->pspace != loc->pspace)
8062             continue;
8063
8064           if (loc->loc_type != bp_loc_hardware_breakpoint
8065               && loc->loc_type != bp_loc_software_breakpoint)
8066             continue;
8067
8068           if (is_addr_in_objfile (loc_addr, objfile))
8069             {
8070               loc->shlib_disabled = 1;
8071               /* At this point, we don't know whether the object was
8072                  unmapped from the inferior or not, so leave the
8073                  inserted flag alone.  We'll handle failure to
8074                  uninsert quietly, in case the object was indeed
8075                  unmapped.  */
8076
8077               mark_breakpoint_location_modified (loc);
8078
8079               bp_modified = 1;
8080             }
8081         }
8082
8083       if (bp_modified)
8084         observer_notify_breakpoint_modified (b);
8085     }
8086 }
8087
8088 /* FORK & VFORK catchpoints.  */
8089
8090 /* An instance of this type is used to represent a fork or vfork
8091    catchpoint.  It includes a "struct breakpoint" as a kind of base
8092    class; users downcast to "struct breakpoint *" when needed.  A
8093    breakpoint is really of this type iff its ops pointer points to
8094    CATCH_FORK_BREAKPOINT_OPS.  */
8095
8096 struct fork_catchpoint
8097 {
8098   /* The base class.  */
8099   struct breakpoint base;
8100
8101   /* Process id of a child process whose forking triggered this
8102      catchpoint.  This field is only valid immediately after this
8103      catchpoint has triggered.  */
8104   ptid_t forked_inferior_pid;
8105 };
8106
8107 /* Implement the "insert" breakpoint_ops method for fork
8108    catchpoints.  */
8109
8110 static int
8111 insert_catch_fork (struct bp_location *bl)
8112 {
8113   return target_insert_fork_catchpoint (ptid_get_pid (inferior_ptid));
8114 }
8115
8116 /* Implement the "remove" breakpoint_ops method for fork
8117    catchpoints.  */
8118
8119 static int
8120 remove_catch_fork (struct bp_location *bl, enum remove_bp_reason reason)
8121 {
8122   return target_remove_fork_catchpoint (ptid_get_pid (inferior_ptid));
8123 }
8124
8125 /* Implement the "breakpoint_hit" breakpoint_ops method for fork
8126    catchpoints.  */
8127
8128 static int
8129 breakpoint_hit_catch_fork (const struct bp_location *bl,
8130                            struct address_space *aspace, CORE_ADDR bp_addr,
8131                            const struct target_waitstatus *ws)
8132 {
8133   struct fork_catchpoint *c = (struct fork_catchpoint *) bl->owner;
8134
8135   if (ws->kind != TARGET_WAITKIND_FORKED)
8136     return 0;
8137
8138   c->forked_inferior_pid = ws->value.related_pid;
8139   return 1;
8140 }
8141
8142 /* Implement the "print_it" breakpoint_ops method for fork
8143    catchpoints.  */
8144
8145 static enum print_stop_action
8146 print_it_catch_fork (bpstat bs)
8147 {
8148   struct ui_out *uiout = current_uiout;
8149   struct breakpoint *b = bs->breakpoint_at;
8150   struct fork_catchpoint *c = (struct fork_catchpoint *) bs->breakpoint_at;
8151
8152   annotate_catchpoint (b->number);
8153   maybe_print_thread_hit_breakpoint (uiout);
8154   if (b->disposition == disp_del)
8155     ui_out_text (uiout, "Temporary catchpoint ");
8156   else
8157     ui_out_text (uiout, "Catchpoint ");
8158   if (ui_out_is_mi_like_p (uiout))
8159     {
8160       ui_out_field_string (uiout, "reason",
8161                            async_reason_lookup (EXEC_ASYNC_FORK));
8162       ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
8163     }
8164   ui_out_field_int (uiout, "bkptno", b->number);
8165   ui_out_text (uiout, " (forked process ");
8166   ui_out_field_int (uiout, "newpid", ptid_get_pid (c->forked_inferior_pid));
8167   ui_out_text (uiout, "), ");
8168   return PRINT_SRC_AND_LOC;
8169 }
8170
8171 /* Implement the "print_one" breakpoint_ops method for fork
8172    catchpoints.  */
8173
8174 static void
8175 print_one_catch_fork (struct breakpoint *b, struct bp_location **last_loc)
8176 {
8177   struct fork_catchpoint *c = (struct fork_catchpoint *) b;
8178   struct value_print_options opts;
8179   struct ui_out *uiout = current_uiout;
8180
8181   get_user_print_options (&opts);
8182
8183   /* Field 4, the address, is omitted (which makes the columns not
8184      line up too nicely with the headers, but the effect is relatively
8185      readable).  */
8186   if (opts.addressprint)
8187     ui_out_field_skip (uiout, "addr");
8188   annotate_field (5);
8189   ui_out_text (uiout, "fork");
8190   if (!ptid_equal (c->forked_inferior_pid, null_ptid))
8191     {
8192       ui_out_text (uiout, ", process ");
8193       ui_out_field_int (uiout, "what",
8194                         ptid_get_pid (c->forked_inferior_pid));
8195       ui_out_spaces (uiout, 1);
8196     }
8197
8198   if (ui_out_is_mi_like_p (uiout))
8199     ui_out_field_string (uiout, "catch-type", "fork");
8200 }
8201
8202 /* Implement the "print_mention" breakpoint_ops method for fork
8203    catchpoints.  */
8204
8205 static void
8206 print_mention_catch_fork (struct breakpoint *b)
8207 {
8208   printf_filtered (_("Catchpoint %d (fork)"), b->number);
8209 }
8210
8211 /* Implement the "print_recreate" breakpoint_ops method for fork
8212    catchpoints.  */
8213
8214 static void
8215 print_recreate_catch_fork (struct breakpoint *b, struct ui_file *fp)
8216 {
8217   fprintf_unfiltered (fp, "catch fork");
8218   print_recreate_thread (b, fp);
8219 }
8220
8221 /* The breakpoint_ops structure to be used in fork catchpoints.  */
8222
8223 static struct breakpoint_ops catch_fork_breakpoint_ops;
8224
8225 /* Implement the "insert" breakpoint_ops method for vfork
8226    catchpoints.  */
8227
8228 static int
8229 insert_catch_vfork (struct bp_location *bl)
8230 {
8231   return target_insert_vfork_catchpoint (ptid_get_pid (inferior_ptid));
8232 }
8233
8234 /* Implement the "remove" breakpoint_ops method for vfork
8235    catchpoints.  */
8236
8237 static int
8238 remove_catch_vfork (struct bp_location *bl, enum remove_bp_reason reason)
8239 {
8240   return target_remove_vfork_catchpoint (ptid_get_pid (inferior_ptid));
8241 }
8242
8243 /* Implement the "breakpoint_hit" breakpoint_ops method for vfork
8244    catchpoints.  */
8245
8246 static int
8247 breakpoint_hit_catch_vfork (const struct bp_location *bl,
8248                             struct address_space *aspace, CORE_ADDR bp_addr,
8249                             const struct target_waitstatus *ws)
8250 {
8251   struct fork_catchpoint *c = (struct fork_catchpoint *) bl->owner;
8252
8253   if (ws->kind != TARGET_WAITKIND_VFORKED)
8254     return 0;
8255
8256   c->forked_inferior_pid = ws->value.related_pid;
8257   return 1;
8258 }
8259
8260 /* Implement the "print_it" breakpoint_ops method for vfork
8261    catchpoints.  */
8262
8263 static enum print_stop_action
8264 print_it_catch_vfork (bpstat bs)
8265 {
8266   struct ui_out *uiout = current_uiout;
8267   struct breakpoint *b = bs->breakpoint_at;
8268   struct fork_catchpoint *c = (struct fork_catchpoint *) b;
8269
8270   annotate_catchpoint (b->number);
8271   maybe_print_thread_hit_breakpoint (uiout);
8272   if (b->disposition == disp_del)
8273     ui_out_text (uiout, "Temporary catchpoint ");
8274   else
8275     ui_out_text (uiout, "Catchpoint ");
8276   if (ui_out_is_mi_like_p (uiout))
8277     {
8278       ui_out_field_string (uiout, "reason",
8279                            async_reason_lookup (EXEC_ASYNC_VFORK));
8280       ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
8281     }
8282   ui_out_field_int (uiout, "bkptno", b->number);
8283   ui_out_text (uiout, " (vforked process ");
8284   ui_out_field_int (uiout, "newpid", ptid_get_pid (c->forked_inferior_pid));
8285   ui_out_text (uiout, "), ");
8286   return PRINT_SRC_AND_LOC;
8287 }
8288
8289 /* Implement the "print_one" breakpoint_ops method for vfork
8290    catchpoints.  */
8291
8292 static void
8293 print_one_catch_vfork (struct breakpoint *b, struct bp_location **last_loc)
8294 {
8295   struct fork_catchpoint *c = (struct fork_catchpoint *) b;
8296   struct value_print_options opts;
8297   struct ui_out *uiout = current_uiout;
8298
8299   get_user_print_options (&opts);
8300   /* Field 4, the address, is omitted (which makes the columns not
8301      line up too nicely with the headers, but the effect is relatively
8302      readable).  */
8303   if (opts.addressprint)
8304     ui_out_field_skip (uiout, "addr");
8305   annotate_field (5);
8306   ui_out_text (uiout, "vfork");
8307   if (!ptid_equal (c->forked_inferior_pid, null_ptid))
8308     {
8309       ui_out_text (uiout, ", process ");
8310       ui_out_field_int (uiout, "what",
8311                         ptid_get_pid (c->forked_inferior_pid));
8312       ui_out_spaces (uiout, 1);
8313     }
8314
8315   if (ui_out_is_mi_like_p (uiout))
8316     ui_out_field_string (uiout, "catch-type", "vfork");
8317 }
8318
8319 /* Implement the "print_mention" breakpoint_ops method for vfork
8320    catchpoints.  */
8321
8322 static void
8323 print_mention_catch_vfork (struct breakpoint *b)
8324 {
8325   printf_filtered (_("Catchpoint %d (vfork)"), b->number);
8326 }
8327
8328 /* Implement the "print_recreate" breakpoint_ops method for vfork
8329    catchpoints.  */
8330
8331 static void
8332 print_recreate_catch_vfork (struct breakpoint *b, struct ui_file *fp)
8333 {
8334   fprintf_unfiltered (fp, "catch vfork");
8335   print_recreate_thread (b, fp);
8336 }
8337
8338 /* The breakpoint_ops structure to be used in vfork catchpoints.  */
8339
8340 static struct breakpoint_ops catch_vfork_breakpoint_ops;
8341
8342 /* An instance of this type is used to represent an solib catchpoint.
8343    It includes a "struct breakpoint" as a kind of base class; users
8344    downcast to "struct breakpoint *" when needed.  A breakpoint is
8345    really of this type iff its ops pointer points to
8346    CATCH_SOLIB_BREAKPOINT_OPS.  */
8347
8348 struct solib_catchpoint
8349 {
8350   /* The base class.  */
8351   struct breakpoint base;
8352
8353   /* True for "catch load", false for "catch unload".  */
8354   unsigned char is_load;
8355
8356   /* Regular expression to match, if any.  COMPILED is only valid when
8357      REGEX is non-NULL.  */
8358   char *regex;
8359   regex_t compiled;
8360 };
8361
8362 static void
8363 dtor_catch_solib (struct breakpoint *b)
8364 {
8365   struct solib_catchpoint *self = (struct solib_catchpoint *) b;
8366
8367   if (self->regex)
8368     regfree (&self->compiled);
8369   xfree (self->regex);
8370
8371   base_breakpoint_ops.dtor (b);
8372 }
8373
8374 static int
8375 insert_catch_solib (struct bp_location *ignore)
8376 {
8377   return 0;
8378 }
8379
8380 static int
8381 remove_catch_solib (struct bp_location *ignore, enum remove_bp_reason reason)
8382 {
8383   return 0;
8384 }
8385
8386 static int
8387 breakpoint_hit_catch_solib (const struct bp_location *bl,
8388                             struct address_space *aspace,
8389                             CORE_ADDR bp_addr,
8390                             const struct target_waitstatus *ws)
8391 {
8392   struct solib_catchpoint *self = (struct solib_catchpoint *) bl->owner;
8393   struct breakpoint *other;
8394
8395   if (ws->kind == TARGET_WAITKIND_LOADED)
8396     return 1;
8397
8398   ALL_BREAKPOINTS (other)
8399   {
8400     struct bp_location *other_bl;
8401
8402     if (other == bl->owner)
8403       continue;
8404
8405     if (other->type != bp_shlib_event)
8406       continue;
8407
8408     if (self->base.pspace != NULL && other->pspace != self->base.pspace)
8409       continue;
8410
8411     for (other_bl = other->loc; other_bl != NULL; other_bl = other_bl->next)
8412       {
8413         if (other->ops->breakpoint_hit (other_bl, aspace, bp_addr, ws))
8414           return 1;
8415       }
8416   }
8417
8418   return 0;
8419 }
8420
8421 static void
8422 check_status_catch_solib (struct bpstats *bs)
8423 {
8424   struct solib_catchpoint *self
8425     = (struct solib_catchpoint *) bs->breakpoint_at;
8426   int ix;
8427
8428   if (self->is_load)
8429     {
8430       struct so_list *iter;
8431
8432       for (ix = 0;
8433            VEC_iterate (so_list_ptr, current_program_space->added_solibs,
8434                         ix, iter);
8435            ++ix)
8436         {
8437           if (!self->regex
8438               || regexec (&self->compiled, iter->so_name, 0, NULL, 0) == 0)
8439             return;
8440         }
8441     }
8442   else
8443     {
8444       char *iter;
8445
8446       for (ix = 0;
8447            VEC_iterate (char_ptr, current_program_space->deleted_solibs,
8448                         ix, iter);
8449            ++ix)
8450         {
8451           if (!self->regex
8452               || regexec (&self->compiled, iter, 0, NULL, 0) == 0)
8453             return;
8454         }
8455     }
8456
8457   bs->stop = 0;
8458   bs->print_it = print_it_noop;
8459 }
8460
8461 static enum print_stop_action
8462 print_it_catch_solib (bpstat bs)
8463 {
8464   struct breakpoint *b = bs->breakpoint_at;
8465   struct ui_out *uiout = current_uiout;
8466
8467   annotate_catchpoint (b->number);
8468   maybe_print_thread_hit_breakpoint (uiout);
8469   if (b->disposition == disp_del)
8470     ui_out_text (uiout, "Temporary catchpoint ");
8471   else
8472     ui_out_text (uiout, "Catchpoint ");
8473   ui_out_field_int (uiout, "bkptno", b->number);
8474   ui_out_text (uiout, "\n");
8475   if (ui_out_is_mi_like_p (uiout))
8476     ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
8477   print_solib_event (1);
8478   return PRINT_SRC_AND_LOC;
8479 }
8480
8481 static void
8482 print_one_catch_solib (struct breakpoint *b, struct bp_location **locs)
8483 {
8484   struct solib_catchpoint *self = (struct solib_catchpoint *) b;
8485   struct value_print_options opts;
8486   struct ui_out *uiout = current_uiout;
8487   char *msg;
8488
8489   get_user_print_options (&opts);
8490   /* Field 4, the address, is omitted (which makes the columns not
8491      line up too nicely with the headers, but the effect is relatively
8492      readable).  */
8493   if (opts.addressprint)
8494     {
8495       annotate_field (4);
8496       ui_out_field_skip (uiout, "addr");
8497     }
8498
8499   annotate_field (5);
8500   if (self->is_load)
8501     {
8502       if (self->regex)
8503         msg = xstrprintf (_("load of library matching %s"), self->regex);
8504       else
8505         msg = xstrdup (_("load of library"));
8506     }
8507   else
8508     {
8509       if (self->regex)
8510         msg = xstrprintf (_("unload of library matching %s"), self->regex);
8511       else
8512         msg = xstrdup (_("unload of library"));
8513     }
8514   ui_out_field_string (uiout, "what", msg);
8515   xfree (msg);
8516
8517   if (ui_out_is_mi_like_p (uiout))
8518     ui_out_field_string (uiout, "catch-type",
8519                          self->is_load ? "load" : "unload");
8520 }
8521
8522 static void
8523 print_mention_catch_solib (struct breakpoint *b)
8524 {
8525   struct solib_catchpoint *self = (struct solib_catchpoint *) b;
8526
8527   printf_filtered (_("Catchpoint %d (%s)"), b->number,
8528                    self->is_load ? "load" : "unload");
8529 }
8530
8531 static void
8532 print_recreate_catch_solib (struct breakpoint *b, struct ui_file *fp)
8533 {
8534   struct solib_catchpoint *self = (struct solib_catchpoint *) b;
8535
8536   fprintf_unfiltered (fp, "%s %s",
8537                       b->disposition == disp_del ? "tcatch" : "catch",
8538                       self->is_load ? "load" : "unload");
8539   if (self->regex)
8540     fprintf_unfiltered (fp, " %s", self->regex);
8541   fprintf_unfiltered (fp, "\n");
8542 }
8543
8544 static struct breakpoint_ops catch_solib_breakpoint_ops;
8545
8546 /* Shared helper function (MI and CLI) for creating and installing
8547    a shared object event catchpoint.  If IS_LOAD is non-zero then
8548    the events to be caught are load events, otherwise they are
8549    unload events.  If IS_TEMP is non-zero the catchpoint is a
8550    temporary one.  If ENABLED is non-zero the catchpoint is
8551    created in an enabled state.  */
8552
8553 void
8554 add_solib_catchpoint (char *arg, int is_load, int is_temp, int enabled)
8555 {
8556   struct solib_catchpoint *c;
8557   struct gdbarch *gdbarch = get_current_arch ();
8558   struct cleanup *cleanup;
8559
8560   if (!arg)
8561     arg = "";
8562   arg = skip_spaces (arg);
8563
8564   c = XCNEW (struct solib_catchpoint);
8565   cleanup = make_cleanup (xfree, c);
8566
8567   if (*arg != '\0')
8568     {
8569       int errcode;
8570
8571       errcode = regcomp (&c->compiled, arg, REG_NOSUB);
8572       if (errcode != 0)
8573         {
8574           char *err = get_regcomp_error (errcode, &c->compiled);
8575
8576           make_cleanup (xfree, err);
8577           error (_("Invalid regexp (%s): %s"), err, arg);
8578         }
8579       c->regex = xstrdup (arg);
8580     }
8581
8582   c->is_load = is_load;
8583   init_catchpoint (&c->base, gdbarch, is_temp, NULL,
8584                    &catch_solib_breakpoint_ops);
8585
8586   c->base.enable_state = enabled ? bp_enabled : bp_disabled;
8587
8588   discard_cleanups (cleanup);
8589   install_breakpoint (0, &c->base, 1);
8590 }
8591
8592 /* A helper function that does all the work for "catch load" and
8593    "catch unload".  */
8594
8595 static void
8596 catch_load_or_unload (char *arg, int from_tty, int is_load,
8597                       struct cmd_list_element *command)
8598 {
8599   int tempflag;
8600   const int enabled = 1;
8601
8602   tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
8603
8604   add_solib_catchpoint (arg, is_load, tempflag, enabled);
8605 }
8606
8607 static void
8608 catch_load_command_1 (char *arg, int from_tty,
8609                       struct cmd_list_element *command)
8610 {
8611   catch_load_or_unload (arg, from_tty, 1, command);
8612 }
8613
8614 static void
8615 catch_unload_command_1 (char *arg, int from_tty,
8616                         struct cmd_list_element *command)
8617 {
8618   catch_load_or_unload (arg, from_tty, 0, command);
8619 }
8620
8621 /* Initialize a new breakpoint of the bp_catchpoint kind.  If TEMPFLAG
8622    is non-zero, then make the breakpoint temporary.  If COND_STRING is
8623    not NULL, then store it in the breakpoint.  OPS, if not NULL, is
8624    the breakpoint_ops structure associated to the catchpoint.  */
8625
8626 void
8627 init_catchpoint (struct breakpoint *b,
8628                  struct gdbarch *gdbarch, int tempflag,
8629                  char *cond_string,
8630                  const struct breakpoint_ops *ops)
8631 {
8632   struct symtab_and_line sal;
8633
8634   init_sal (&sal);
8635   sal.pspace = current_program_space;
8636
8637   init_raw_breakpoint (b, gdbarch, sal, bp_catchpoint, ops);
8638
8639   b->cond_string = (cond_string == NULL) ? NULL : xstrdup (cond_string);
8640   b->disposition = tempflag ? disp_del : disp_donttouch;
8641 }
8642
8643 void
8644 install_breakpoint (int internal, struct breakpoint *b, int update_gll)
8645 {
8646   add_to_breakpoint_chain (b);
8647   set_breakpoint_number (internal, b);
8648   if (is_tracepoint (b))
8649     set_tracepoint_count (breakpoint_count);
8650   if (!internal)
8651     mention (b);
8652   observer_notify_breakpoint_created (b);
8653
8654   if (update_gll)
8655     update_global_location_list (UGLL_MAY_INSERT);
8656 }
8657
8658 static void
8659 create_fork_vfork_event_catchpoint (struct gdbarch *gdbarch,
8660                                     int tempflag, char *cond_string,
8661                                     const struct breakpoint_ops *ops)
8662 {
8663   struct fork_catchpoint *c = XNEW (struct fork_catchpoint);
8664
8665   init_catchpoint (&c->base, gdbarch, tempflag, cond_string, ops);
8666
8667   c->forked_inferior_pid = null_ptid;
8668
8669   install_breakpoint (0, &c->base, 1);
8670 }
8671
8672 /* Exec catchpoints.  */
8673
8674 /* An instance of this type is used to represent an exec catchpoint.
8675    It includes a "struct breakpoint" as a kind of base class; users
8676    downcast to "struct breakpoint *" when needed.  A breakpoint is
8677    really of this type iff its ops pointer points to
8678    CATCH_EXEC_BREAKPOINT_OPS.  */
8679
8680 struct exec_catchpoint
8681 {
8682   /* The base class.  */
8683   struct breakpoint base;
8684
8685   /* Filename of a program whose exec triggered this catchpoint.
8686      This field is only valid immediately after this catchpoint has
8687      triggered.  */
8688   char *exec_pathname;
8689 };
8690
8691 /* Implement the "dtor" breakpoint_ops method for exec
8692    catchpoints.  */
8693
8694 static void
8695 dtor_catch_exec (struct breakpoint *b)
8696 {
8697   struct exec_catchpoint *c = (struct exec_catchpoint *) b;
8698
8699   xfree (c->exec_pathname);
8700
8701   base_breakpoint_ops.dtor (b);
8702 }
8703
8704 static int
8705 insert_catch_exec (struct bp_location *bl)
8706 {
8707   return target_insert_exec_catchpoint (ptid_get_pid (inferior_ptid));
8708 }
8709
8710 static int
8711 remove_catch_exec (struct bp_location *bl, enum remove_bp_reason reason)
8712 {
8713   return target_remove_exec_catchpoint (ptid_get_pid (inferior_ptid));
8714 }
8715
8716 static int
8717 breakpoint_hit_catch_exec (const struct bp_location *bl,
8718                            struct address_space *aspace, CORE_ADDR bp_addr,
8719                            const struct target_waitstatus *ws)
8720 {
8721   struct exec_catchpoint *c = (struct exec_catchpoint *) bl->owner;
8722
8723   if (ws->kind != TARGET_WAITKIND_EXECD)
8724     return 0;
8725
8726   c->exec_pathname = xstrdup (ws->value.execd_pathname);
8727   return 1;
8728 }
8729
8730 static enum print_stop_action
8731 print_it_catch_exec (bpstat bs)
8732 {
8733   struct ui_out *uiout = current_uiout;
8734   struct breakpoint *b = bs->breakpoint_at;
8735   struct exec_catchpoint *c = (struct exec_catchpoint *) b;
8736
8737   annotate_catchpoint (b->number);
8738   maybe_print_thread_hit_breakpoint (uiout);
8739   if (b->disposition == disp_del)
8740     ui_out_text (uiout, "Temporary catchpoint ");
8741   else
8742     ui_out_text (uiout, "Catchpoint ");
8743   if (ui_out_is_mi_like_p (uiout))
8744     {
8745       ui_out_field_string (uiout, "reason",
8746                            async_reason_lookup (EXEC_ASYNC_EXEC));
8747       ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
8748     }
8749   ui_out_field_int (uiout, "bkptno", b->number);
8750   ui_out_text (uiout, " (exec'd ");
8751   ui_out_field_string (uiout, "new-exec", c->exec_pathname);
8752   ui_out_text (uiout, "), ");
8753
8754   return PRINT_SRC_AND_LOC;
8755 }
8756
8757 static void
8758 print_one_catch_exec (struct breakpoint *b, struct bp_location **last_loc)
8759 {
8760   struct exec_catchpoint *c = (struct exec_catchpoint *) b;
8761   struct value_print_options opts;
8762   struct ui_out *uiout = current_uiout;
8763
8764   get_user_print_options (&opts);
8765
8766   /* Field 4, the address, is omitted (which makes the columns
8767      not line up too nicely with the headers, but the effect
8768      is relatively readable).  */
8769   if (opts.addressprint)
8770     ui_out_field_skip (uiout, "addr");
8771   annotate_field (5);
8772   ui_out_text (uiout, "exec");
8773   if (c->exec_pathname != NULL)
8774     {
8775       ui_out_text (uiout, ", program \"");
8776       ui_out_field_string (uiout, "what", c->exec_pathname);
8777       ui_out_text (uiout, "\" ");
8778     }
8779
8780   if (ui_out_is_mi_like_p (uiout))
8781     ui_out_field_string (uiout, "catch-type", "exec");
8782 }
8783
8784 static void
8785 print_mention_catch_exec (struct breakpoint *b)
8786 {
8787   printf_filtered (_("Catchpoint %d (exec)"), b->number);
8788 }
8789
8790 /* Implement the "print_recreate" breakpoint_ops method for exec
8791    catchpoints.  */
8792
8793 static void
8794 print_recreate_catch_exec (struct breakpoint *b, struct ui_file *fp)
8795 {
8796   fprintf_unfiltered (fp, "catch exec");
8797   print_recreate_thread (b, fp);
8798 }
8799
8800 static struct breakpoint_ops catch_exec_breakpoint_ops;
8801
8802 static int
8803 hw_breakpoint_used_count (void)
8804 {
8805   int i = 0;
8806   struct breakpoint *b;
8807   struct bp_location *bl;
8808
8809   ALL_BREAKPOINTS (b)
8810   {
8811     if (b->type == bp_hardware_breakpoint && breakpoint_enabled (b))
8812       for (bl = b->loc; bl; bl = bl->next)
8813         {
8814           /* Special types of hardware breakpoints may use more than
8815              one register.  */
8816           i += b->ops->resources_needed (bl);
8817         }
8818   }
8819
8820   return i;
8821 }
8822
8823 /* Returns the resources B would use if it were a hardware
8824    watchpoint.  */
8825
8826 static int
8827 hw_watchpoint_use_count (struct breakpoint *b)
8828 {
8829   int i = 0;
8830   struct bp_location *bl;
8831
8832   if (!breakpoint_enabled (b))
8833     return 0;
8834
8835   for (bl = b->loc; bl; bl = bl->next)
8836     {
8837       /* Special types of hardware watchpoints may use more than
8838          one register.  */
8839       i += b->ops->resources_needed (bl);
8840     }
8841
8842   return i;
8843 }
8844
8845 /* Returns the sum the used resources of all hardware watchpoints of
8846    type TYPE in the breakpoints list.  Also returns in OTHER_TYPE_USED
8847    the sum of the used resources of all hardware watchpoints of other
8848    types _not_ TYPE.  */
8849
8850 static int
8851 hw_watchpoint_used_count_others (struct breakpoint *except,
8852                                  enum bptype type, int *other_type_used)
8853 {
8854   int i = 0;
8855   struct breakpoint *b;
8856
8857   *other_type_used = 0;
8858   ALL_BREAKPOINTS (b)
8859     {
8860       if (b == except)
8861         continue;
8862       if (!breakpoint_enabled (b))
8863         continue;
8864
8865       if (b->type == type)
8866         i += hw_watchpoint_use_count (b);
8867       else if (is_hardware_watchpoint (b))
8868         *other_type_used = 1;
8869     }
8870
8871   return i;
8872 }
8873
8874 void
8875 disable_watchpoints_before_interactive_call_start (void)
8876 {
8877   struct breakpoint *b;
8878
8879   ALL_BREAKPOINTS (b)
8880   {
8881     if (is_watchpoint (b) && breakpoint_enabled (b))
8882       {
8883         b->enable_state = bp_call_disabled;
8884         update_global_location_list (UGLL_DONT_INSERT);
8885       }
8886   }
8887 }
8888
8889 void
8890 enable_watchpoints_after_interactive_call_stop (void)
8891 {
8892   struct breakpoint *b;
8893
8894   ALL_BREAKPOINTS (b)
8895   {
8896     if (is_watchpoint (b) && b->enable_state == bp_call_disabled)
8897       {
8898         b->enable_state = bp_enabled;
8899         update_global_location_list (UGLL_MAY_INSERT);
8900       }
8901   }
8902 }
8903
8904 void
8905 disable_breakpoints_before_startup (void)
8906 {
8907   current_program_space->executing_startup = 1;
8908   update_global_location_list (UGLL_DONT_INSERT);
8909 }
8910
8911 void
8912 enable_breakpoints_after_startup (void)
8913 {
8914   current_program_space->executing_startup = 0;
8915   breakpoint_re_set ();
8916 }
8917
8918 /* Create a new single-step breakpoint for thread THREAD, with no
8919    locations.  */
8920
8921 static struct breakpoint *
8922 new_single_step_breakpoint (int thread, struct gdbarch *gdbarch)
8923 {
8924   struct breakpoint *b = XNEW (struct breakpoint);
8925
8926   init_raw_breakpoint_without_location (b, gdbarch, bp_single_step,
8927                                         &momentary_breakpoint_ops);
8928
8929   b->disposition = disp_donttouch;
8930   b->frame_id = null_frame_id;
8931
8932   b->thread = thread;
8933   gdb_assert (b->thread != 0);
8934
8935   add_to_breakpoint_chain (b);
8936
8937   return b;
8938 }
8939
8940 /* Set a momentary breakpoint of type TYPE at address specified by
8941    SAL.  If FRAME_ID is valid, the breakpoint is restricted to that
8942    frame.  */
8943
8944 struct breakpoint *
8945 set_momentary_breakpoint (struct gdbarch *gdbarch, struct symtab_and_line sal,
8946                           struct frame_id frame_id, enum bptype type)
8947 {
8948   struct breakpoint *b;
8949
8950   /* If FRAME_ID is valid, it should be a real frame, not an inlined or
8951      tail-called one.  */
8952   gdb_assert (!frame_id_artificial_p (frame_id));
8953
8954   b = set_raw_breakpoint (gdbarch, sal, type, &momentary_breakpoint_ops);
8955   b->enable_state = bp_enabled;
8956   b->disposition = disp_donttouch;
8957   b->frame_id = frame_id;
8958
8959   /* If we're debugging a multi-threaded program, then we want
8960      momentary breakpoints to be active in only a single thread of
8961      control.  */
8962   if (in_thread_list (inferior_ptid))
8963     b->thread = ptid_to_global_thread_id (inferior_ptid);
8964
8965   update_global_location_list_nothrow (UGLL_MAY_INSERT);
8966
8967   return b;
8968 }
8969
8970 /* Make a momentary breakpoint based on the master breakpoint ORIG.
8971    The new breakpoint will have type TYPE, use OPS as its
8972    breakpoint_ops, and will set enabled to LOC_ENABLED.  */
8973
8974 static struct breakpoint *
8975 momentary_breakpoint_from_master (struct breakpoint *orig,
8976                                   enum bptype type,
8977                                   const struct breakpoint_ops *ops,
8978                                   int loc_enabled)
8979 {
8980   struct breakpoint *copy;
8981
8982   copy = set_raw_breakpoint_without_location (orig->gdbarch, type, ops);
8983   copy->loc = allocate_bp_location (copy);
8984   set_breakpoint_location_function (copy->loc, 1);
8985
8986   copy->loc->gdbarch = orig->loc->gdbarch;
8987   copy->loc->requested_address = orig->loc->requested_address;
8988   copy->loc->address = orig->loc->address;
8989   copy->loc->section = orig->loc->section;
8990   copy->loc->pspace = orig->loc->pspace;
8991   copy->loc->probe = orig->loc->probe;
8992   copy->loc->line_number = orig->loc->line_number;
8993   copy->loc->symtab = orig->loc->symtab;
8994   copy->loc->enabled = loc_enabled;
8995   copy->frame_id = orig->frame_id;
8996   copy->thread = orig->thread;
8997   copy->pspace = orig->pspace;
8998
8999   copy->enable_state = bp_enabled;
9000   copy->disposition = disp_donttouch;
9001   copy->number = internal_breakpoint_number--;
9002
9003   update_global_location_list_nothrow (UGLL_DONT_INSERT);
9004   return copy;
9005 }
9006
9007 /* Make a deep copy of momentary breakpoint ORIG.  Returns NULL if
9008    ORIG is NULL.  */
9009
9010 struct breakpoint *
9011 clone_momentary_breakpoint (struct breakpoint *orig)
9012 {
9013   /* If there's nothing to clone, then return nothing.  */
9014   if (orig == NULL)
9015     return NULL;
9016
9017   return momentary_breakpoint_from_master (orig, orig->type, orig->ops, 0);
9018 }
9019
9020 struct breakpoint *
9021 set_momentary_breakpoint_at_pc (struct gdbarch *gdbarch, CORE_ADDR pc,
9022                                 enum bptype type)
9023 {
9024   struct symtab_and_line sal;
9025
9026   sal = find_pc_line (pc, 0);
9027   sal.pc = pc;
9028   sal.section = find_pc_overlay (pc);
9029   sal.explicit_pc = 1;
9030
9031   return set_momentary_breakpoint (gdbarch, sal, null_frame_id, type);
9032 }
9033 \f
9034
9035 /* Tell the user we have just set a breakpoint B.  */
9036
9037 static void
9038 mention (struct breakpoint *b)
9039 {
9040   b->ops->print_mention (b);
9041   if (ui_out_is_mi_like_p (current_uiout))
9042     return;
9043   printf_filtered ("\n");
9044 }
9045 \f
9046
9047 static int bp_loc_is_permanent (struct bp_location *loc);
9048
9049 static struct bp_location *
9050 add_location_to_breakpoint (struct breakpoint *b,
9051                             const struct symtab_and_line *sal)
9052 {
9053   struct bp_location *loc, **tmp;
9054   CORE_ADDR adjusted_address;
9055   struct gdbarch *loc_gdbarch = get_sal_arch (*sal);
9056
9057   if (loc_gdbarch == NULL)
9058     loc_gdbarch = b->gdbarch;
9059
9060   /* Adjust the breakpoint's address prior to allocating a location.
9061      Once we call allocate_bp_location(), that mostly uninitialized
9062      location will be placed on the location chain.  Adjustment of the
9063      breakpoint may cause target_read_memory() to be called and we do
9064      not want its scan of the location chain to find a breakpoint and
9065      location that's only been partially initialized.  */
9066   adjusted_address = adjust_breakpoint_address (loc_gdbarch,
9067                                                 sal->pc, b->type);
9068
9069   /* Sort the locations by their ADDRESS.  */
9070   loc = allocate_bp_location (b);
9071   for (tmp = &(b->loc); *tmp != NULL && (*tmp)->address <= adjusted_address;
9072        tmp = &((*tmp)->next))
9073     ;
9074   loc->next = *tmp;
9075   *tmp = loc;
9076
9077   loc->requested_address = sal->pc;
9078   loc->address = adjusted_address;
9079   loc->pspace = sal->pspace;
9080   loc->probe.probe = sal->probe;
9081   loc->probe.objfile = sal->objfile;
9082   gdb_assert (loc->pspace != NULL);
9083   loc->section = sal->section;
9084   loc->gdbarch = loc_gdbarch;
9085   loc->line_number = sal->line;
9086   loc->symtab = sal->symtab;
9087
9088   set_breakpoint_location_function (loc,
9089                                     sal->explicit_pc || sal->explicit_line);
9090
9091   /* While by definition, permanent breakpoints are already present in the
9092      code, we don't mark the location as inserted.  Normally one would expect
9093      that GDB could rely on that breakpoint instruction to stop the program,
9094      thus removing the need to insert its own breakpoint, except that executing
9095      the breakpoint instruction can kill the target instead of reporting a
9096      SIGTRAP.  E.g., on SPARC, when interrupts are disabled, executing the
9097      instruction resets the CPU, so QEMU 2.0.0 for SPARC correspondingly dies
9098      with "Trap 0x02 while interrupts disabled, Error state".  Letting the
9099      breakpoint be inserted normally results in QEMU knowing about the GDB
9100      breakpoint, and thus trap before the breakpoint instruction is executed.
9101      (If GDB later needs to continue execution past the permanent breakpoint,
9102      it manually increments the PC, thus avoiding executing the breakpoint
9103      instruction.)  */
9104   if (bp_loc_is_permanent (loc))
9105     loc->permanent = 1;
9106
9107   return loc;
9108 }
9109 \f
9110
9111 /* See breakpoint.h.  */
9112
9113 int
9114 program_breakpoint_here_p (struct gdbarch *gdbarch, CORE_ADDR address)
9115 {
9116   int len;
9117   CORE_ADDR addr;
9118   const gdb_byte *bpoint;
9119   gdb_byte *target_mem;
9120   struct cleanup *cleanup;
9121   int retval = 0;
9122
9123   addr = address;
9124   bpoint = gdbarch_breakpoint_from_pc (gdbarch, &addr, &len);
9125
9126   /* Software breakpoints unsupported?  */
9127   if (bpoint == NULL)
9128     return 0;
9129
9130   target_mem = (gdb_byte *) alloca (len);
9131
9132   /* Enable the automatic memory restoration from breakpoints while
9133      we read the memory.  Otherwise we could say about our temporary
9134      breakpoints they are permanent.  */
9135   cleanup = make_show_memory_breakpoints_cleanup (0);
9136
9137   if (target_read_memory (address, target_mem, len) == 0
9138       && memcmp (target_mem, bpoint, len) == 0)
9139     retval = 1;
9140
9141   do_cleanups (cleanup);
9142
9143   return retval;
9144 }
9145
9146 /* Return 1 if LOC is pointing to a permanent breakpoint,
9147    return 0 otherwise.  */
9148
9149 static int
9150 bp_loc_is_permanent (struct bp_location *loc)
9151 {
9152   struct cleanup *cleanup;
9153   int retval;
9154
9155   gdb_assert (loc != NULL);
9156
9157   /* If we have a catchpoint or a watchpoint, just return 0.  We should not
9158      attempt to read from the addresses the locations of these breakpoint types
9159      point to.  program_breakpoint_here_p, below, will attempt to read
9160      memory.  */
9161   if (!breakpoint_address_is_meaningful (loc->owner))
9162     return 0;
9163
9164   cleanup = save_current_space_and_thread ();
9165   switch_to_program_space_and_thread (loc->pspace);
9166
9167   retval = program_breakpoint_here_p (loc->gdbarch, loc->address);
9168
9169   do_cleanups (cleanup);
9170
9171   return retval;
9172 }
9173
9174 /* Build a command list for the dprintf corresponding to the current
9175    settings of the dprintf style options.  */
9176
9177 static void
9178 update_dprintf_command_list (struct breakpoint *b)
9179 {
9180   char *dprintf_args = b->extra_string;
9181   char *printf_line = NULL;
9182
9183   if (!dprintf_args)
9184     return;
9185
9186   dprintf_args = skip_spaces (dprintf_args);
9187
9188   /* Allow a comma, as it may have terminated a location, but don't
9189      insist on it.  */
9190   if (*dprintf_args == ',')
9191     ++dprintf_args;
9192   dprintf_args = skip_spaces (dprintf_args);
9193
9194   if (*dprintf_args != '"')
9195     error (_("Bad format string, missing '\"'."));
9196
9197   if (strcmp (dprintf_style, dprintf_style_gdb) == 0)
9198     printf_line = xstrprintf ("printf %s", dprintf_args);
9199   else if (strcmp (dprintf_style, dprintf_style_call) == 0)
9200     {
9201       if (!dprintf_function)
9202         error (_("No function supplied for dprintf call"));
9203
9204       if (dprintf_channel && strlen (dprintf_channel) > 0)
9205         printf_line = xstrprintf ("call (void) %s (%s,%s)",
9206                                   dprintf_function,
9207                                   dprintf_channel,
9208                                   dprintf_args);
9209       else
9210         printf_line = xstrprintf ("call (void) %s (%s)",
9211                                   dprintf_function,
9212                                   dprintf_args);
9213     }
9214   else if (strcmp (dprintf_style, dprintf_style_agent) == 0)
9215     {
9216       if (target_can_run_breakpoint_commands ())
9217         printf_line = xstrprintf ("agent-printf %s", dprintf_args);
9218       else
9219         {
9220           warning (_("Target cannot run dprintf commands, falling back to GDB printf"));
9221           printf_line = xstrprintf ("printf %s", dprintf_args);
9222         }
9223     }
9224   else
9225     internal_error (__FILE__, __LINE__,
9226                     _("Invalid dprintf style."));
9227
9228   gdb_assert (printf_line != NULL);
9229   /* Manufacture a printf sequence.  */
9230   {
9231     struct command_line *printf_cmd_line = XNEW (struct command_line);
9232
9233     printf_cmd_line->control_type = simple_control;
9234     printf_cmd_line->body_count = 0;
9235     printf_cmd_line->body_list = NULL;
9236     printf_cmd_line->next = NULL;
9237     printf_cmd_line->line = printf_line;
9238
9239     breakpoint_set_commands (b, printf_cmd_line);
9240   }
9241 }
9242
9243 /* Update all dprintf commands, making their command lists reflect
9244    current style settings.  */
9245
9246 static void
9247 update_dprintf_commands (char *args, int from_tty,
9248                          struct cmd_list_element *c)
9249 {
9250   struct breakpoint *b;
9251
9252   ALL_BREAKPOINTS (b)
9253     {
9254       if (b->type == bp_dprintf)
9255         update_dprintf_command_list (b);
9256     }
9257 }
9258
9259 /* Create a breakpoint with SAL as location.  Use LOCATION
9260    as a description of the location, and COND_STRING
9261    as condition expression.  If LOCATION is NULL then create an
9262    "address location" from the address in the SAL.  */
9263
9264 static void
9265 init_breakpoint_sal (struct breakpoint *b, struct gdbarch *gdbarch,
9266                      struct symtabs_and_lines sals,
9267                      struct event_location *location,
9268                      char *filter, char *cond_string,
9269                      char *extra_string,
9270                      enum bptype type, enum bpdisp disposition,
9271                      int thread, int task, int ignore_count,
9272                      const struct breakpoint_ops *ops, int from_tty,
9273                      int enabled, int internal, unsigned flags,
9274                      int display_canonical)
9275 {
9276   int i;
9277
9278   if (type == bp_hardware_breakpoint)
9279     {
9280       int target_resources_ok;
9281
9282       i = hw_breakpoint_used_count ();
9283       target_resources_ok =
9284         target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
9285                                             i + 1, 0);
9286       if (target_resources_ok == 0)
9287         error (_("No hardware breakpoint support in the target."));
9288       else if (target_resources_ok < 0)
9289         error (_("Hardware breakpoints used exceeds limit."));
9290     }
9291
9292   gdb_assert (sals.nelts > 0);
9293
9294   for (i = 0; i < sals.nelts; ++i)
9295     {
9296       struct symtab_and_line sal = sals.sals[i];
9297       struct bp_location *loc;
9298
9299       if (from_tty)
9300         {
9301           struct gdbarch *loc_gdbarch = get_sal_arch (sal);
9302           if (!loc_gdbarch)
9303             loc_gdbarch = gdbarch;
9304
9305           describe_other_breakpoints (loc_gdbarch,
9306                                       sal.pspace, sal.pc, sal.section, thread);
9307         }
9308
9309       if (i == 0)
9310         {
9311           init_raw_breakpoint (b, gdbarch, sal, type, ops);
9312           b->thread = thread;
9313           b->task = task;
9314
9315           b->cond_string = cond_string;
9316           b->extra_string = extra_string;
9317           b->ignore_count = ignore_count;
9318           b->enable_state = enabled ? bp_enabled : bp_disabled;
9319           b->disposition = disposition;
9320
9321           if ((flags & CREATE_BREAKPOINT_FLAGS_INSERTED) != 0)
9322             b->loc->inserted = 1;
9323
9324           if (type == bp_static_tracepoint)
9325             {
9326               struct tracepoint *t = (struct tracepoint *) b;
9327               struct static_tracepoint_marker marker;
9328
9329               if (strace_marker_p (b))
9330                 {
9331                   /* We already know the marker exists, otherwise, we
9332                      wouldn't see a sal for it.  */
9333                   const char *p = &event_location_to_string (b->location)[3];
9334                   const char *endp;
9335                   char *marker_str;
9336
9337                   p = skip_spaces_const (p);
9338
9339                   endp = skip_to_space_const (p);
9340
9341                   marker_str = savestring (p, endp - p);
9342                   t->static_trace_marker_id = marker_str;
9343
9344                   printf_filtered (_("Probed static tracepoint "
9345                                      "marker \"%s\"\n"),
9346                                    t->static_trace_marker_id);
9347                 }
9348               else if (target_static_tracepoint_marker_at (sal.pc, &marker))
9349                 {
9350                   t->static_trace_marker_id = xstrdup (marker.str_id);
9351                   release_static_tracepoint_marker (&marker);
9352
9353                   printf_filtered (_("Probed static tracepoint "
9354                                      "marker \"%s\"\n"),
9355                                    t->static_trace_marker_id);
9356                 }
9357               else
9358                 warning (_("Couldn't determine the static "
9359                            "tracepoint marker to probe"));
9360             }
9361
9362           loc = b->loc;
9363         }
9364       else
9365         {
9366           loc = add_location_to_breakpoint (b, &sal);
9367           if ((flags & CREATE_BREAKPOINT_FLAGS_INSERTED) != 0)
9368             loc->inserted = 1;
9369         }
9370
9371       if (b->cond_string)
9372         {
9373           const char *arg = b->cond_string;
9374
9375           loc->cond = parse_exp_1 (&arg, loc->address,
9376                                    block_for_pc (loc->address), 0);
9377           if (*arg)
9378               error (_("Garbage '%s' follows condition"), arg);
9379         }
9380
9381       /* Dynamic printf requires and uses additional arguments on the
9382          command line, otherwise it's an error.  */
9383       if (type == bp_dprintf)
9384         {
9385           if (b->extra_string)
9386             update_dprintf_command_list (b);
9387           else
9388             error (_("Format string required"));
9389         }
9390       else if (b->extra_string)
9391         error (_("Garbage '%s' at end of command"), b->extra_string);
9392     }
9393
9394   b->display_canonical = display_canonical;
9395   if (location != NULL)
9396     b->location = location;
9397   else
9398     {
9399       const char *addr_string = NULL;
9400       int addr_string_len = 0;
9401
9402       if (location != NULL)
9403         addr_string = event_location_to_string (location);
9404       if (addr_string != NULL)
9405         addr_string_len = strlen (addr_string);
9406
9407       b->location = new_address_location (b->loc->address,
9408                                           addr_string, addr_string_len);
9409     }
9410   b->filter = filter;
9411 }
9412
9413 static void
9414 create_breakpoint_sal (struct gdbarch *gdbarch,
9415                        struct symtabs_and_lines sals,
9416                        struct event_location *location,
9417                        char *filter, char *cond_string,
9418                        char *extra_string,
9419                        enum bptype type, enum bpdisp disposition,
9420                        int thread, int task, int ignore_count,
9421                        const struct breakpoint_ops *ops, int from_tty,
9422                        int enabled, int internal, unsigned flags,
9423                        int display_canonical)
9424 {
9425   struct breakpoint *b;
9426   struct cleanup *old_chain;
9427
9428   if (is_tracepoint_type (type))
9429     {
9430       struct tracepoint *t;
9431
9432       t = XCNEW (struct tracepoint);
9433       b = &t->base;
9434     }
9435   else
9436     b = XNEW (struct breakpoint);
9437
9438   old_chain = make_cleanup (xfree, b);
9439
9440   init_breakpoint_sal (b, gdbarch,
9441                        sals, location,
9442                        filter, cond_string, extra_string,
9443                        type, disposition,
9444                        thread, task, ignore_count,
9445                        ops, from_tty,
9446                        enabled, internal, flags,
9447                        display_canonical);
9448   discard_cleanups (old_chain);
9449
9450   install_breakpoint (internal, b, 0);
9451 }
9452
9453 /* Add SALS.nelts breakpoints to the breakpoint table.  For each
9454    SALS.sal[i] breakpoint, include the corresponding ADDR_STRING[i]
9455    value.  COND_STRING, if not NULL, specified the condition to be
9456    used for all breakpoints.  Essentially the only case where
9457    SALS.nelts is not 1 is when we set a breakpoint on an overloaded
9458    function.  In that case, it's still not possible to specify
9459    separate conditions for different overloaded functions, so
9460    we take just a single condition string.
9461    
9462    NOTE: If the function succeeds, the caller is expected to cleanup
9463    the arrays ADDR_STRING, COND_STRING, and SALS (but not the
9464    array contents).  If the function fails (error() is called), the
9465    caller is expected to cleanups both the ADDR_STRING, COND_STRING,
9466    COND and SALS arrays and each of those arrays contents.  */
9467
9468 static void
9469 create_breakpoints_sal (struct gdbarch *gdbarch,
9470                         struct linespec_result *canonical,
9471                         char *cond_string, char *extra_string,
9472                         enum bptype type, enum bpdisp disposition,
9473                         int thread, int task, int ignore_count,
9474                         const struct breakpoint_ops *ops, int from_tty,
9475                         int enabled, int internal, unsigned flags)
9476 {
9477   int i;
9478   struct linespec_sals *lsal;
9479
9480   if (canonical->pre_expanded)
9481     gdb_assert (VEC_length (linespec_sals, canonical->sals) == 1);
9482
9483   for (i = 0; VEC_iterate (linespec_sals, canonical->sals, i, lsal); ++i)
9484     {
9485       /* Note that 'location' can be NULL in the case of a plain
9486          'break', without arguments.  */
9487       struct event_location *location
9488         = (canonical->location != NULL
9489            ? copy_event_location (canonical->location) : NULL);
9490       char *filter_string = lsal->canonical ? xstrdup (lsal->canonical) : NULL;
9491       struct cleanup *inner = make_cleanup_delete_event_location (location);
9492
9493       make_cleanup (xfree, filter_string);
9494       create_breakpoint_sal (gdbarch, lsal->sals,
9495                              location,
9496                              filter_string,
9497                              cond_string, extra_string,
9498                              type, disposition,
9499                              thread, task, ignore_count, ops,
9500                              from_tty, enabled, internal, flags,
9501                              canonical->special_display);
9502       discard_cleanups (inner);
9503     }
9504 }
9505
9506 /* Parse LOCATION which is assumed to be a SAL specification possibly
9507    followed by conditionals.  On return, SALS contains an array of SAL
9508    addresses found.  LOCATION points to the end of the SAL (for
9509    linespec locations).
9510
9511    The array and the line spec strings are allocated on the heap, it is
9512    the caller's responsibility to free them.  */
9513
9514 static void
9515 parse_breakpoint_sals (const struct event_location *location,
9516                        struct linespec_result *canonical)
9517 {
9518   struct symtab_and_line cursal;
9519
9520   if (event_location_type (location) == LINESPEC_LOCATION)
9521     {
9522       const char *address = get_linespec_location (location);
9523
9524       if (address == NULL)
9525         {
9526           /* The last displayed codepoint, if it's valid, is our default
9527              breakpoint address.  */
9528           if (last_displayed_sal_is_valid ())
9529             {
9530               struct linespec_sals lsal;
9531               struct symtab_and_line sal;
9532               CORE_ADDR pc;
9533
9534               init_sal (&sal);          /* Initialize to zeroes.  */
9535               lsal.sals.sals = XNEW (struct symtab_and_line);
9536
9537               /* Set sal's pspace, pc, symtab, and line to the values
9538                  corresponding to the last call to print_frame_info.
9539                  Be sure to reinitialize LINE with NOTCURRENT == 0
9540                  as the breakpoint line number is inappropriate otherwise.
9541                  find_pc_line would adjust PC, re-set it back.  */
9542               get_last_displayed_sal (&sal);
9543               pc = sal.pc;
9544               sal = find_pc_line (pc, 0);
9545
9546               /* "break" without arguments is equivalent to "break *PC"
9547                  where PC is the last displayed codepoint's address.  So
9548                  make sure to set sal.explicit_pc to prevent GDB from
9549                  trying to expand the list of sals to include all other
9550                  instances with the same symtab and line.  */
9551               sal.pc = pc;
9552               sal.explicit_pc = 1;
9553
9554               lsal.sals.sals[0] = sal;
9555               lsal.sals.nelts = 1;
9556               lsal.canonical = NULL;
9557
9558               VEC_safe_push (linespec_sals, canonical->sals, &lsal);
9559               return;
9560             }
9561           else
9562             error (_("No default breakpoint address now."));
9563         }
9564     }
9565
9566   /* Force almost all breakpoints to be in terms of the
9567      current_source_symtab (which is decode_line_1's default).
9568      This should produce the results we want almost all of the
9569      time while leaving default_breakpoint_* alone.
9570
9571      ObjC: However, don't match an Objective-C method name which
9572      may have a '+' or '-' succeeded by a '['.  */
9573   cursal = get_current_source_symtab_and_line ();
9574   if (last_displayed_sal_is_valid ())
9575     {
9576       const char *address = NULL;
9577
9578       if (event_location_type (location) == LINESPEC_LOCATION)
9579         address = get_linespec_location (location);
9580
9581       if (!cursal.symtab
9582           || (address != NULL
9583               && strchr ("+-", address[0]) != NULL
9584               && address[1] != '['))
9585         {
9586           decode_line_full (location, DECODE_LINE_FUNFIRSTLINE, NULL,
9587                             get_last_displayed_symtab (),
9588                             get_last_displayed_line (),
9589                             canonical, NULL, NULL);
9590           return;
9591         }
9592     }
9593
9594   decode_line_full (location, DECODE_LINE_FUNFIRSTLINE, NULL,
9595                     cursal.symtab, cursal.line, canonical, NULL, NULL);
9596 }
9597
9598
9599 /* Convert each SAL into a real PC.  Verify that the PC can be
9600    inserted as a breakpoint.  If it can't throw an error.  */
9601
9602 static void
9603 breakpoint_sals_to_pc (struct symtabs_and_lines *sals)
9604 {    
9605   int i;
9606
9607   for (i = 0; i < sals->nelts; i++)
9608     resolve_sal_pc (&sals->sals[i]);
9609 }
9610
9611 /* Fast tracepoints may have restrictions on valid locations.  For
9612    instance, a fast tracepoint using a jump instead of a trap will
9613    likely have to overwrite more bytes than a trap would, and so can
9614    only be placed where the instruction is longer than the jump, or a
9615    multi-instruction sequence does not have a jump into the middle of
9616    it, etc.  */
9617
9618 static void
9619 check_fast_tracepoint_sals (struct gdbarch *gdbarch,
9620                             struct symtabs_and_lines *sals)
9621 {
9622   int i, rslt;
9623   struct symtab_and_line *sal;
9624   char *msg;
9625   struct cleanup *old_chain;
9626
9627   for (i = 0; i < sals->nelts; i++)
9628     {
9629       struct gdbarch *sarch;
9630
9631       sal = &sals->sals[i];
9632
9633       sarch = get_sal_arch (*sal);
9634       /* We fall back to GDBARCH if there is no architecture
9635          associated with SAL.  */
9636       if (sarch == NULL)
9637         sarch = gdbarch;
9638       rslt = gdbarch_fast_tracepoint_valid_at (sarch, sal->pc, &msg);
9639       old_chain = make_cleanup (xfree, msg);
9640
9641       if (!rslt)
9642         error (_("May not have a fast tracepoint at 0x%s%s"),
9643                paddress (sarch, sal->pc), (msg ? msg : ""));
9644
9645       do_cleanups (old_chain);
9646     }
9647 }
9648
9649 /* Given TOK, a string specification of condition and thread, as
9650    accepted by the 'break' command, extract the condition
9651    string and thread number and set *COND_STRING and *THREAD.
9652    PC identifies the context at which the condition should be parsed.
9653    If no condition is found, *COND_STRING is set to NULL.
9654    If no thread is found, *THREAD is set to -1.  */
9655
9656 static void
9657 find_condition_and_thread (const char *tok, CORE_ADDR pc,
9658                            char **cond_string, int *thread, int *task,
9659                            char **rest)
9660 {
9661   *cond_string = NULL;
9662   *thread = -1;
9663   *task = 0;
9664   *rest = NULL;
9665
9666   while (tok && *tok)
9667     {
9668       const char *end_tok;
9669       int toklen;
9670       const char *cond_start = NULL;
9671       const char *cond_end = NULL;
9672
9673       tok = skip_spaces_const (tok);
9674
9675       if ((*tok == '"' || *tok == ',') && rest)
9676         {
9677           *rest = savestring (tok, strlen (tok));
9678           return;
9679         }
9680
9681       end_tok = skip_to_space_const (tok);
9682
9683       toklen = end_tok - tok;
9684
9685       if (toklen >= 1 && strncmp (tok, "if", toklen) == 0)
9686         {
9687           struct expression *expr;
9688
9689           tok = cond_start = end_tok + 1;
9690           expr = parse_exp_1 (&tok, pc, block_for_pc (pc), 0);
9691           xfree (expr);
9692           cond_end = tok;
9693           *cond_string = savestring (cond_start, cond_end - cond_start);
9694         }
9695       else if (toklen >= 1 && strncmp (tok, "thread", toklen) == 0)
9696         {
9697           const char *tmptok;
9698           struct thread_info *thr;
9699
9700           tok = end_tok + 1;
9701           thr = parse_thread_id (tok, &tmptok);
9702           if (tok == tmptok)
9703             error (_("Junk after thread keyword."));
9704           *thread = thr->global_num;
9705           tok = tmptok;
9706         }
9707       else if (toklen >= 1 && strncmp (tok, "task", toklen) == 0)
9708         {
9709           char *tmptok;
9710
9711           tok = end_tok + 1;
9712           *task = strtol (tok, &tmptok, 0);
9713           if (tok == tmptok)
9714             error (_("Junk after task keyword."));
9715           if (!valid_task_id (*task))
9716             error (_("Unknown task %d."), *task);
9717           tok = tmptok;
9718         }
9719       else if (rest)
9720         {
9721           *rest = savestring (tok, strlen (tok));
9722           return;
9723         }
9724       else
9725         error (_("Junk at end of arguments."));
9726     }
9727 }
9728
9729 /* Decode a static tracepoint marker spec.  */
9730
9731 static struct symtabs_and_lines
9732 decode_static_tracepoint_spec (const char **arg_p)
9733 {
9734   VEC(static_tracepoint_marker_p) *markers = NULL;
9735   struct symtabs_and_lines sals;
9736   struct cleanup *old_chain;
9737   const char *p = &(*arg_p)[3];
9738   const char *endp;
9739   char *marker_str;
9740   int i;
9741
9742   p = skip_spaces_const (p);
9743
9744   endp = skip_to_space_const (p);
9745
9746   marker_str = savestring (p, endp - p);
9747   old_chain = make_cleanup (xfree, marker_str);
9748
9749   markers = target_static_tracepoint_markers_by_strid (marker_str);
9750   if (VEC_empty(static_tracepoint_marker_p, markers))
9751     error (_("No known static tracepoint marker named %s"), marker_str);
9752
9753   sals.nelts = VEC_length(static_tracepoint_marker_p, markers);
9754   sals.sals = XNEWVEC (struct symtab_and_line, sals.nelts);
9755
9756   for (i = 0; i < sals.nelts; i++)
9757     {
9758       struct static_tracepoint_marker *marker;
9759
9760       marker = VEC_index (static_tracepoint_marker_p, markers, i);
9761
9762       init_sal (&sals.sals[i]);
9763
9764       sals.sals[i] = find_pc_line (marker->address, 0);
9765       sals.sals[i].pc = marker->address;
9766
9767       release_static_tracepoint_marker (marker);
9768     }
9769
9770   do_cleanups (old_chain);
9771
9772   *arg_p = endp;
9773   return sals;
9774 }
9775
9776 /* See breakpoint.h.  */
9777
9778 int
9779 create_breakpoint (struct gdbarch *gdbarch,
9780                    const struct event_location *location, char *cond_string,
9781                    int thread, char *extra_string,
9782                    int parse_extra,
9783                    int tempflag, enum bptype type_wanted,
9784                    int ignore_count,
9785                    enum auto_boolean pending_break_support,
9786                    const struct breakpoint_ops *ops,
9787                    int from_tty, int enabled, int internal,
9788                    unsigned flags)
9789 {
9790   struct linespec_result canonical;
9791   struct cleanup *old_chain;
9792   struct cleanup *bkpt_chain = NULL;
9793   int pending = 0;
9794   int task = 0;
9795   int prev_bkpt_count = breakpoint_count;
9796
9797   gdb_assert (ops != NULL);
9798
9799   /* If extra_string isn't useful, set it to NULL.  */
9800   if (extra_string != NULL && *extra_string == '\0')
9801     extra_string = NULL;
9802
9803   init_linespec_result (&canonical);
9804
9805   TRY
9806     {
9807       ops->create_sals_from_location (location, &canonical, type_wanted);
9808     }
9809   CATCH (e, RETURN_MASK_ERROR)
9810     {
9811       /* If caller is interested in rc value from parse, set
9812          value.  */
9813       if (e.error == NOT_FOUND_ERROR)
9814         {
9815           /* If pending breakpoint support is turned off, throw
9816              error.  */
9817
9818           if (pending_break_support == AUTO_BOOLEAN_FALSE)
9819             throw_exception (e);
9820
9821           exception_print (gdb_stderr, e);
9822
9823           /* If pending breakpoint support is auto query and the user
9824              selects no, then simply return the error code.  */
9825           if (pending_break_support == AUTO_BOOLEAN_AUTO
9826               && !nquery (_("Make %s pending on future shared library load? "),
9827                           bptype_string (type_wanted)))
9828             return 0;
9829
9830           /* At this point, either the user was queried about setting
9831              a pending breakpoint and selected yes, or pending
9832              breakpoint behavior is on and thus a pending breakpoint
9833              is defaulted on behalf of the user.  */
9834           pending = 1;
9835         }
9836       else
9837         throw_exception (e);
9838     }
9839   END_CATCH
9840
9841   if (!pending && VEC_empty (linespec_sals, canonical.sals))
9842     return 0;
9843
9844   /* Create a chain of things that always need to be cleaned up.  */
9845   old_chain = make_cleanup_destroy_linespec_result (&canonical);
9846
9847   /* ----------------------------- SNIP -----------------------------
9848      Anything added to the cleanup chain beyond this point is assumed
9849      to be part of a breakpoint.  If the breakpoint create succeeds
9850      then the memory is not reclaimed.  */
9851   bkpt_chain = make_cleanup (null_cleanup, 0);
9852
9853   /* Resolve all line numbers to PC's and verify that the addresses
9854      are ok for the target.  */
9855   if (!pending)
9856     {
9857       int ix;
9858       struct linespec_sals *iter;
9859
9860       for (ix = 0; VEC_iterate (linespec_sals, canonical.sals, ix, iter); ++ix)
9861         breakpoint_sals_to_pc (&iter->sals);
9862     }
9863
9864   /* Fast tracepoints may have additional restrictions on location.  */
9865   if (!pending && type_wanted == bp_fast_tracepoint)
9866     {
9867       int ix;
9868       struct linespec_sals *iter;
9869
9870       for (ix = 0; VEC_iterate (linespec_sals, canonical.sals, ix, iter); ++ix)
9871         check_fast_tracepoint_sals (gdbarch, &iter->sals);
9872     }
9873
9874   /* Verify that condition can be parsed, before setting any
9875      breakpoints.  Allocate a separate condition expression for each
9876      breakpoint.  */
9877   if (!pending)
9878     {
9879       if (parse_extra)
9880         {
9881           char *rest;
9882           struct linespec_sals *lsal;
9883
9884           lsal = VEC_index (linespec_sals, canonical.sals, 0);
9885
9886           /* Here we only parse 'arg' to separate condition
9887              from thread number, so parsing in context of first
9888              sal is OK.  When setting the breakpoint we'll
9889              re-parse it in context of each sal.  */
9890
9891           find_condition_and_thread (extra_string, lsal->sals.sals[0].pc,
9892                                      &cond_string, &thread, &task, &rest);
9893           if (cond_string)
9894             make_cleanup (xfree, cond_string);
9895           if (rest)
9896             make_cleanup (xfree, rest);
9897           if (rest)
9898             extra_string = rest;
9899           else
9900             extra_string = NULL;
9901         }
9902       else
9903         {
9904           if (type_wanted != bp_dprintf
9905               && extra_string != NULL && *extra_string != '\0')
9906                 error (_("Garbage '%s' at end of location"), extra_string);
9907
9908           /* Create a private copy of condition string.  */
9909           if (cond_string)
9910             {
9911               cond_string = xstrdup (cond_string);
9912               make_cleanup (xfree, cond_string);
9913             }
9914           /* Create a private copy of any extra string.  */
9915           if (extra_string)
9916             {
9917               extra_string = xstrdup (extra_string);
9918               make_cleanup (xfree, extra_string);
9919             }
9920         }
9921
9922       ops->create_breakpoints_sal (gdbarch, &canonical,
9923                                    cond_string, extra_string, type_wanted,
9924                                    tempflag ? disp_del : disp_donttouch,
9925                                    thread, task, ignore_count, ops,
9926                                    from_tty, enabled, internal, flags);
9927     }
9928   else
9929     {
9930       struct breakpoint *b;
9931
9932       if (is_tracepoint_type (type_wanted))
9933         {
9934           struct tracepoint *t;
9935
9936           t = XCNEW (struct tracepoint);
9937           b = &t->base;
9938         }
9939       else
9940         b = XNEW (struct breakpoint);
9941
9942       init_raw_breakpoint_without_location (b, gdbarch, type_wanted, ops);
9943       b->location = copy_event_location (location);
9944
9945       if (parse_extra)
9946         b->cond_string = NULL;
9947       else
9948         {
9949           /* Create a private copy of condition string.  */
9950           if (cond_string)
9951             {
9952               cond_string = xstrdup (cond_string);
9953               make_cleanup (xfree, cond_string);
9954             }
9955           b->cond_string = cond_string;
9956           b->thread = thread;
9957         }
9958
9959       /* Create a private copy of any extra string.  */
9960       if (extra_string != NULL)
9961         {
9962           extra_string = xstrdup (extra_string);
9963           make_cleanup (xfree, extra_string);
9964         }
9965       b->extra_string = extra_string;
9966       b->ignore_count = ignore_count;
9967       b->disposition = tempflag ? disp_del : disp_donttouch;
9968       b->condition_not_parsed = 1;
9969       b->enable_state = enabled ? bp_enabled : bp_disabled;
9970       if ((type_wanted != bp_breakpoint
9971            && type_wanted != bp_hardware_breakpoint) || thread != -1)
9972         b->pspace = current_program_space;
9973
9974       install_breakpoint (internal, b, 0);
9975     }
9976   
9977   if (VEC_length (linespec_sals, canonical.sals) > 1)
9978     {
9979       warning (_("Multiple breakpoints were set.\nUse the "
9980                  "\"delete\" command to delete unwanted breakpoints."));
9981       prev_breakpoint_count = prev_bkpt_count;
9982     }
9983
9984   /* That's it.  Discard the cleanups for data inserted into the
9985      breakpoint.  */
9986   discard_cleanups (bkpt_chain);
9987   /* But cleanup everything else.  */
9988   do_cleanups (old_chain);
9989
9990   /* error call may happen here - have BKPT_CHAIN already discarded.  */
9991   update_global_location_list (UGLL_MAY_INSERT);
9992
9993   return 1;
9994 }
9995
9996 /* Set a breakpoint.
9997    ARG is a string describing breakpoint address,
9998    condition, and thread.
9999    FLAG specifies if a breakpoint is hardware on,
10000    and if breakpoint is temporary, using BP_HARDWARE_FLAG
10001    and BP_TEMPFLAG.  */
10002
10003 static void
10004 break_command_1 (char *arg, int flag, int from_tty)
10005 {
10006   int tempflag = flag & BP_TEMPFLAG;
10007   enum bptype type_wanted = (flag & BP_HARDWAREFLAG
10008                              ? bp_hardware_breakpoint
10009                              : bp_breakpoint);
10010   struct breakpoint_ops *ops;
10011   struct event_location *location;
10012   struct cleanup *cleanup;
10013
10014   location = string_to_event_location (&arg, current_language);
10015   cleanup = make_cleanup_delete_event_location (location);
10016
10017   /* Matching breakpoints on probes.  */
10018   if (location != NULL
10019       && event_location_type (location) == PROBE_LOCATION)
10020     ops = &bkpt_probe_breakpoint_ops;
10021   else
10022     ops = &bkpt_breakpoint_ops;
10023
10024   create_breakpoint (get_current_arch (),
10025                      location,
10026                      NULL, 0, arg, 1 /* parse arg */,
10027                      tempflag, type_wanted,
10028                      0 /* Ignore count */,
10029                      pending_break_support,
10030                      ops,
10031                      from_tty,
10032                      1 /* enabled */,
10033                      0 /* internal */,
10034                      0);
10035   do_cleanups (cleanup);
10036 }
10037
10038 /* Helper function for break_command_1 and disassemble_command.  */
10039
10040 void
10041 resolve_sal_pc (struct symtab_and_line *sal)
10042 {
10043   CORE_ADDR pc;
10044
10045   if (sal->pc == 0 && sal->symtab != NULL)
10046     {
10047       if (!find_line_pc (sal->symtab, sal->line, &pc))
10048         error (_("No line %d in file \"%s\"."),
10049                sal->line, symtab_to_filename_for_display (sal->symtab));
10050       sal->pc = pc;
10051
10052       /* If this SAL corresponds to a breakpoint inserted using a line
10053          number, then skip the function prologue if necessary.  */
10054       if (sal->explicit_line)
10055         skip_prologue_sal (sal);
10056     }
10057
10058   if (sal->section == 0 && sal->symtab != NULL)
10059     {
10060       const struct blockvector *bv;
10061       const struct block *b;
10062       struct symbol *sym;
10063
10064       bv = blockvector_for_pc_sect (sal->pc, 0, &b,
10065                                     SYMTAB_COMPUNIT (sal->symtab));
10066       if (bv != NULL)
10067         {
10068           sym = block_linkage_function (b);
10069           if (sym != NULL)
10070             {
10071               fixup_symbol_section (sym, SYMTAB_OBJFILE (sal->symtab));
10072               sal->section = SYMBOL_OBJ_SECTION (SYMTAB_OBJFILE (sal->symtab),
10073                                                  sym);
10074             }
10075           else
10076             {
10077               /* It really is worthwhile to have the section, so we'll
10078                  just have to look harder. This case can be executed
10079                  if we have line numbers but no functions (as can
10080                  happen in assembly source).  */
10081
10082               struct bound_minimal_symbol msym;
10083               struct cleanup *old_chain = save_current_space_and_thread ();
10084
10085               switch_to_program_space_and_thread (sal->pspace);
10086
10087               msym = lookup_minimal_symbol_by_pc (sal->pc);
10088               if (msym.minsym)
10089                 sal->section = MSYMBOL_OBJ_SECTION (msym.objfile, msym.minsym);
10090
10091               do_cleanups (old_chain);
10092             }
10093         }
10094     }
10095 }
10096
10097 void
10098 break_command (char *arg, int from_tty)
10099 {
10100   break_command_1 (arg, 0, from_tty);
10101 }
10102
10103 void
10104 tbreak_command (char *arg, int from_tty)
10105 {
10106   break_command_1 (arg, BP_TEMPFLAG, from_tty);
10107 }
10108
10109 static void
10110 hbreak_command (char *arg, int from_tty)
10111 {
10112   break_command_1 (arg, BP_HARDWAREFLAG, from_tty);
10113 }
10114
10115 static void
10116 thbreak_command (char *arg, int from_tty)
10117 {
10118   break_command_1 (arg, (BP_TEMPFLAG | BP_HARDWAREFLAG), from_tty);
10119 }
10120
10121 static void
10122 stop_command (char *arg, int from_tty)
10123 {
10124   printf_filtered (_("Specify the type of breakpoint to set.\n\
10125 Usage: stop in <function | address>\n\
10126        stop at <line>\n"));
10127 }
10128
10129 static void
10130 stopin_command (char *arg, int from_tty)
10131 {
10132   int badInput = 0;
10133
10134   if (arg == (char *) NULL)
10135     badInput = 1;
10136   else if (*arg != '*')
10137     {
10138       char *argptr = arg;
10139       int hasColon = 0;
10140
10141       /* Look for a ':'.  If this is a line number specification, then
10142          say it is bad, otherwise, it should be an address or
10143          function/method name.  */
10144       while (*argptr && !hasColon)
10145         {
10146           hasColon = (*argptr == ':');
10147           argptr++;
10148         }
10149
10150       if (hasColon)
10151         badInput = (*argptr != ':');    /* Not a class::method */
10152       else
10153         badInput = isdigit (*arg);      /* a simple line number */
10154     }
10155
10156   if (badInput)
10157     printf_filtered (_("Usage: stop in <function | address>\n"));
10158   else
10159     break_command_1 (arg, 0, from_tty);
10160 }
10161
10162 static void
10163 stopat_command (char *arg, int from_tty)
10164 {
10165   int badInput = 0;
10166
10167   if (arg == (char *) NULL || *arg == '*')      /* no line number */
10168     badInput = 1;
10169   else
10170     {
10171       char *argptr = arg;
10172       int hasColon = 0;
10173
10174       /* Look for a ':'.  If there is a '::' then get out, otherwise
10175          it is probably a line number.  */
10176       while (*argptr && !hasColon)
10177         {
10178           hasColon = (*argptr == ':');
10179           argptr++;
10180         }
10181
10182       if (hasColon)
10183         badInput = (*argptr == ':');    /* we have class::method */
10184       else
10185         badInput = !isdigit (*arg);     /* not a line number */
10186     }
10187
10188   if (badInput)
10189     printf_filtered (_("Usage: stop at <line>\n"));
10190   else
10191     break_command_1 (arg, 0, from_tty);
10192 }
10193
10194 /* The dynamic printf command is mostly like a regular breakpoint, but
10195    with a prewired command list consisting of a single output command,
10196    built from extra arguments supplied on the dprintf command
10197    line.  */
10198
10199 static void
10200 dprintf_command (char *arg, int from_tty)
10201 {
10202   struct event_location *location;
10203   struct cleanup *cleanup;
10204
10205   location = string_to_event_location (&arg, current_language);
10206   cleanup = make_cleanup_delete_event_location (location);
10207
10208   /* If non-NULL, ARG should have been advanced past the location;
10209      the next character must be ','.  */
10210   if (arg != NULL)
10211     {
10212       if (arg[0] != ',' || arg[1] == '\0')
10213         error (_("Format string required"));
10214       else
10215         {
10216           /* Skip the comma.  */
10217           ++arg;
10218         }
10219     }
10220
10221   create_breakpoint (get_current_arch (),
10222                      location,
10223                      NULL, 0, arg, 1 /* parse arg */,
10224                      0, bp_dprintf,
10225                      0 /* Ignore count */,
10226                      pending_break_support,
10227                      &dprintf_breakpoint_ops,
10228                      from_tty,
10229                      1 /* enabled */,
10230                      0 /* internal */,
10231                      0);
10232   do_cleanups (cleanup);
10233 }
10234
10235 static void
10236 agent_printf_command (char *arg, int from_tty)
10237 {
10238   error (_("May only run agent-printf on the target"));
10239 }
10240
10241 /* Implement the "breakpoint_hit" breakpoint_ops method for
10242    ranged breakpoints.  */
10243
10244 static int
10245 breakpoint_hit_ranged_breakpoint (const struct bp_location *bl,
10246                                   struct address_space *aspace,
10247                                   CORE_ADDR bp_addr,
10248                                   const struct target_waitstatus *ws)
10249 {
10250   if (ws->kind != TARGET_WAITKIND_STOPPED
10251       || ws->value.sig != GDB_SIGNAL_TRAP)
10252     return 0;
10253
10254   return breakpoint_address_match_range (bl->pspace->aspace, bl->address,
10255                                          bl->length, aspace, bp_addr);
10256 }
10257
10258 /* Implement the "resources_needed" breakpoint_ops method for
10259    ranged breakpoints.  */
10260
10261 static int
10262 resources_needed_ranged_breakpoint (const struct bp_location *bl)
10263 {
10264   return target_ranged_break_num_registers ();
10265 }
10266
10267 /* Implement the "print_it" breakpoint_ops method for
10268    ranged breakpoints.  */
10269
10270 static enum print_stop_action
10271 print_it_ranged_breakpoint (bpstat bs)
10272 {
10273   struct breakpoint *b = bs->breakpoint_at;
10274   struct bp_location *bl = b->loc;
10275   struct ui_out *uiout = current_uiout;
10276
10277   gdb_assert (b->type == bp_hardware_breakpoint);
10278
10279   /* Ranged breakpoints have only one location.  */
10280   gdb_assert (bl && bl->next == NULL);
10281
10282   annotate_breakpoint (b->number);
10283
10284   maybe_print_thread_hit_breakpoint (uiout);
10285
10286   if (b->disposition == disp_del)
10287     ui_out_text (uiout, "Temporary ranged breakpoint ");
10288   else
10289     ui_out_text (uiout, "Ranged breakpoint ");
10290   if (ui_out_is_mi_like_p (uiout))
10291     {
10292       ui_out_field_string (uiout, "reason",
10293                       async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT));
10294       ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
10295     }
10296   ui_out_field_int (uiout, "bkptno", b->number);
10297   ui_out_text (uiout, ", ");
10298
10299   return PRINT_SRC_AND_LOC;
10300 }
10301
10302 /* Implement the "print_one" breakpoint_ops method for
10303    ranged breakpoints.  */
10304
10305 static void
10306 print_one_ranged_breakpoint (struct breakpoint *b,
10307                              struct bp_location **last_loc)
10308 {
10309   struct bp_location *bl = b->loc;
10310   struct value_print_options opts;
10311   struct ui_out *uiout = current_uiout;
10312
10313   /* Ranged breakpoints have only one location.  */
10314   gdb_assert (bl && bl->next == NULL);
10315
10316   get_user_print_options (&opts);
10317
10318   if (opts.addressprint)
10319     /* We don't print the address range here, it will be printed later
10320        by print_one_detail_ranged_breakpoint.  */
10321     ui_out_field_skip (uiout, "addr");
10322   annotate_field (5);
10323   print_breakpoint_location (b, bl);
10324   *last_loc = bl;
10325 }
10326
10327 /* Implement the "print_one_detail" breakpoint_ops method for
10328    ranged breakpoints.  */
10329
10330 static void
10331 print_one_detail_ranged_breakpoint (const struct breakpoint *b,
10332                                     struct ui_out *uiout)
10333 {
10334   CORE_ADDR address_start, address_end;
10335   struct bp_location *bl = b->loc;
10336   struct ui_file *stb = mem_fileopen ();
10337   struct cleanup *cleanup = make_cleanup_ui_file_delete (stb);
10338
10339   gdb_assert (bl);
10340
10341   address_start = bl->address;
10342   address_end = address_start + bl->length - 1;
10343
10344   ui_out_text (uiout, "\taddress range: ");
10345   fprintf_unfiltered (stb, "[%s, %s]",
10346                       print_core_address (bl->gdbarch, address_start),
10347                       print_core_address (bl->gdbarch, address_end));
10348   ui_out_field_stream (uiout, "addr", stb);
10349   ui_out_text (uiout, "\n");
10350
10351   do_cleanups (cleanup);
10352 }
10353
10354 /* Implement the "print_mention" breakpoint_ops method for
10355    ranged breakpoints.  */
10356
10357 static void
10358 print_mention_ranged_breakpoint (struct breakpoint *b)
10359 {
10360   struct bp_location *bl = b->loc;
10361   struct ui_out *uiout = current_uiout;
10362
10363   gdb_assert (bl);
10364   gdb_assert (b->type == bp_hardware_breakpoint);
10365
10366   if (ui_out_is_mi_like_p (uiout))
10367     return;
10368
10369   printf_filtered (_("Hardware assisted ranged breakpoint %d from %s to %s."),
10370                    b->number, paddress (bl->gdbarch, bl->address),
10371                    paddress (bl->gdbarch, bl->address + bl->length - 1));
10372 }
10373
10374 /* Implement the "print_recreate" breakpoint_ops method for
10375    ranged breakpoints.  */
10376
10377 static void
10378 print_recreate_ranged_breakpoint (struct breakpoint *b, struct ui_file *fp)
10379 {
10380   fprintf_unfiltered (fp, "break-range %s, %s",
10381                       event_location_to_string (b->location),
10382                       event_location_to_string (b->location_range_end));
10383   print_recreate_thread (b, fp);
10384 }
10385
10386 /* The breakpoint_ops structure to be used in ranged breakpoints.  */
10387
10388 static struct breakpoint_ops ranged_breakpoint_ops;
10389
10390 /* Find the address where the end of the breakpoint range should be
10391    placed, given the SAL of the end of the range.  This is so that if
10392    the user provides a line number, the end of the range is set to the
10393    last instruction of the given line.  */
10394
10395 static CORE_ADDR
10396 find_breakpoint_range_end (struct symtab_and_line sal)
10397 {
10398   CORE_ADDR end;
10399
10400   /* If the user provided a PC value, use it.  Otherwise,
10401      find the address of the end of the given location.  */
10402   if (sal.explicit_pc)
10403     end = sal.pc;
10404   else
10405     {
10406       int ret;
10407       CORE_ADDR start;
10408
10409       ret = find_line_pc_range (sal, &start, &end);
10410       if (!ret)
10411         error (_("Could not find location of the end of the range."));
10412
10413       /* find_line_pc_range returns the start of the next line.  */
10414       end--;
10415     }
10416
10417   return end;
10418 }
10419
10420 /* Implement the "break-range" CLI command.  */
10421
10422 static void
10423 break_range_command (char *arg, int from_tty)
10424 {
10425   char *arg_start, *addr_string_start;
10426   struct linespec_result canonical_start, canonical_end;
10427   int bp_count, can_use_bp, length;
10428   CORE_ADDR end;
10429   struct breakpoint *b;
10430   struct symtab_and_line sal_start, sal_end;
10431   struct cleanup *cleanup_bkpt;
10432   struct linespec_sals *lsal_start, *lsal_end;
10433   struct event_location *start_location, *end_location;
10434
10435   /* We don't support software ranged breakpoints.  */
10436   if (target_ranged_break_num_registers () < 0)
10437     error (_("This target does not support hardware ranged breakpoints."));
10438
10439   bp_count = hw_breakpoint_used_count ();
10440   bp_count += target_ranged_break_num_registers ();
10441   can_use_bp = target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
10442                                                    bp_count, 0);
10443   if (can_use_bp < 0)
10444     error (_("Hardware breakpoints used exceeds limit."));
10445
10446   arg = skip_spaces (arg);
10447   if (arg == NULL || arg[0] == '\0')
10448     error(_("No address range specified."));
10449
10450   init_linespec_result (&canonical_start);
10451
10452   arg_start = arg;
10453   start_location = string_to_event_location (&arg, current_language);
10454   cleanup_bkpt = make_cleanup_delete_event_location (start_location);
10455   parse_breakpoint_sals (start_location, &canonical_start);
10456   make_cleanup_destroy_linespec_result (&canonical_start);
10457
10458   if (arg[0] != ',')
10459     error (_("Too few arguments."));
10460   else if (VEC_empty (linespec_sals, canonical_start.sals))
10461     error (_("Could not find location of the beginning of the range."));
10462
10463   lsal_start = VEC_index (linespec_sals, canonical_start.sals, 0);
10464
10465   if (VEC_length (linespec_sals, canonical_start.sals) > 1
10466       || lsal_start->sals.nelts != 1)
10467     error (_("Cannot create a ranged breakpoint with multiple locations."));
10468
10469   sal_start = lsal_start->sals.sals[0];
10470   addr_string_start = savestring (arg_start, arg - arg_start);
10471   make_cleanup (xfree, addr_string_start);
10472
10473   arg++;        /* Skip the comma.  */
10474   arg = skip_spaces (arg);
10475
10476   /* Parse the end location.  */
10477
10478   init_linespec_result (&canonical_end);
10479   arg_start = arg;
10480
10481   /* We call decode_line_full directly here instead of using
10482      parse_breakpoint_sals because we need to specify the start location's
10483      symtab and line as the default symtab and line for the end of the
10484      range.  This makes it possible to have ranges like "foo.c:27, +14",
10485      where +14 means 14 lines from the start location.  */
10486   end_location = string_to_event_location (&arg, current_language);
10487   make_cleanup_delete_event_location (end_location);
10488   decode_line_full (end_location, DECODE_LINE_FUNFIRSTLINE, NULL,
10489                     sal_start.symtab, sal_start.line,
10490                     &canonical_end, NULL, NULL);
10491
10492   make_cleanup_destroy_linespec_result (&canonical_end);
10493
10494   if (VEC_empty (linespec_sals, canonical_end.sals))
10495     error (_("Could not find location of the end of the range."));
10496
10497   lsal_end = VEC_index (linespec_sals, canonical_end.sals, 0);
10498   if (VEC_length (linespec_sals, canonical_end.sals) > 1
10499       || lsal_end->sals.nelts != 1)
10500     error (_("Cannot create a ranged breakpoint with multiple locations."));
10501
10502   sal_end = lsal_end->sals.sals[0];
10503
10504   end = find_breakpoint_range_end (sal_end);
10505   if (sal_start.pc > end)
10506     error (_("Invalid address range, end precedes start."));
10507
10508   length = end - sal_start.pc + 1;
10509   if (length < 0)
10510     /* Length overflowed.  */
10511     error (_("Address range too large."));
10512   else if (length == 1)
10513     {
10514       /* This range is simple enough to be handled by
10515          the `hbreak' command.  */
10516       hbreak_command (addr_string_start, 1);
10517
10518       do_cleanups (cleanup_bkpt);
10519
10520       return;
10521     }
10522
10523   /* Now set up the breakpoint.  */
10524   b = set_raw_breakpoint (get_current_arch (), sal_start,
10525                           bp_hardware_breakpoint, &ranged_breakpoint_ops);
10526   set_breakpoint_count (breakpoint_count + 1);
10527   b->number = breakpoint_count;
10528   b->disposition = disp_donttouch;
10529   b->location = copy_event_location (start_location);
10530   b->location_range_end = copy_event_location (end_location);
10531   b->loc->length = length;
10532
10533   do_cleanups (cleanup_bkpt);
10534
10535   mention (b);
10536   observer_notify_breakpoint_created (b);
10537   update_global_location_list (UGLL_MAY_INSERT);
10538 }
10539
10540 /*  Return non-zero if EXP is verified as constant.  Returned zero
10541     means EXP is variable.  Also the constant detection may fail for
10542     some constant expressions and in such case still falsely return
10543     zero.  */
10544
10545 static int
10546 watchpoint_exp_is_const (const struct expression *exp)
10547 {
10548   int i = exp->nelts;
10549
10550   while (i > 0)
10551     {
10552       int oplenp, argsp;
10553
10554       /* We are only interested in the descriptor of each element.  */
10555       operator_length (exp, i, &oplenp, &argsp);
10556       i -= oplenp;
10557
10558       switch (exp->elts[i].opcode)
10559         {
10560         case BINOP_ADD:
10561         case BINOP_SUB:
10562         case BINOP_MUL:
10563         case BINOP_DIV:
10564         case BINOP_REM:
10565         case BINOP_MOD:
10566         case BINOP_LSH:
10567         case BINOP_RSH:
10568         case BINOP_LOGICAL_AND:
10569         case BINOP_LOGICAL_OR:
10570         case BINOP_BITWISE_AND:
10571         case BINOP_BITWISE_IOR:
10572         case BINOP_BITWISE_XOR:
10573         case BINOP_EQUAL:
10574         case BINOP_NOTEQUAL:
10575         case BINOP_LESS:
10576         case BINOP_GTR:
10577         case BINOP_LEQ:
10578         case BINOP_GEQ:
10579         case BINOP_REPEAT:
10580         case BINOP_COMMA:
10581         case BINOP_EXP:
10582         case BINOP_MIN:
10583         case BINOP_MAX:
10584         case BINOP_INTDIV:
10585         case BINOP_CONCAT:
10586         case TERNOP_COND:
10587         case TERNOP_SLICE:
10588
10589         case OP_LONG:
10590         case OP_DOUBLE:
10591         case OP_DECFLOAT:
10592         case OP_LAST:
10593         case OP_COMPLEX:
10594         case OP_STRING:
10595         case OP_ARRAY:
10596         case OP_TYPE:
10597         case OP_TYPEOF:
10598         case OP_DECLTYPE:
10599         case OP_TYPEID:
10600         case OP_NAME:
10601         case OP_OBJC_NSSTRING:
10602
10603         case UNOP_NEG:
10604         case UNOP_LOGICAL_NOT:
10605         case UNOP_COMPLEMENT:
10606         case UNOP_ADDR:
10607         case UNOP_HIGH:
10608         case UNOP_CAST:
10609
10610         case UNOP_CAST_TYPE:
10611         case UNOP_REINTERPRET_CAST:
10612         case UNOP_DYNAMIC_CAST:
10613           /* Unary, binary and ternary operators: We have to check
10614              their operands.  If they are constant, then so is the
10615              result of that operation.  For instance, if A and B are
10616              determined to be constants, then so is "A + B".
10617
10618              UNOP_IND is one exception to the rule above, because the
10619              value of *ADDR is not necessarily a constant, even when
10620              ADDR is.  */
10621           break;
10622
10623         case OP_VAR_VALUE:
10624           /* Check whether the associated symbol is a constant.
10625
10626              We use SYMBOL_CLASS rather than TYPE_CONST because it's
10627              possible that a buggy compiler could mark a variable as
10628              constant even when it is not, and TYPE_CONST would return
10629              true in this case, while SYMBOL_CLASS wouldn't.
10630
10631              We also have to check for function symbols because they
10632              are always constant.  */
10633           {
10634             struct symbol *s = exp->elts[i + 2].symbol;
10635
10636             if (SYMBOL_CLASS (s) != LOC_BLOCK
10637                 && SYMBOL_CLASS (s) != LOC_CONST
10638                 && SYMBOL_CLASS (s) != LOC_CONST_BYTES)
10639               return 0;
10640             break;
10641           }
10642
10643         /* The default action is to return 0 because we are using
10644            the optimistic approach here: If we don't know something,
10645            then it is not a constant.  */
10646         default:
10647           return 0;
10648         }
10649     }
10650
10651   return 1;
10652 }
10653
10654 /* Implement the "dtor" breakpoint_ops method for watchpoints.  */
10655
10656 static void
10657 dtor_watchpoint (struct breakpoint *self)
10658 {
10659   struct watchpoint *w = (struct watchpoint *) self;
10660
10661   xfree (w->cond_exp);
10662   xfree (w->exp);
10663   xfree (w->exp_string);
10664   xfree (w->exp_string_reparse);
10665   value_free (w->val);
10666
10667   base_breakpoint_ops.dtor (self);
10668 }
10669
10670 /* Implement the "re_set" breakpoint_ops method for watchpoints.  */
10671
10672 static void
10673 re_set_watchpoint (struct breakpoint *b)
10674 {
10675   struct watchpoint *w = (struct watchpoint *) b;
10676
10677   /* Watchpoint can be either on expression using entirely global
10678      variables, or it can be on local variables.
10679
10680      Watchpoints of the first kind are never auto-deleted, and even
10681      persist across program restarts.  Since they can use variables
10682      from shared libraries, we need to reparse expression as libraries
10683      are loaded and unloaded.
10684
10685      Watchpoints on local variables can also change meaning as result
10686      of solib event.  For example, if a watchpoint uses both a local
10687      and a global variables in expression, it's a local watchpoint,
10688      but unloading of a shared library will make the expression
10689      invalid.  This is not a very common use case, but we still
10690      re-evaluate expression, to avoid surprises to the user.
10691
10692      Note that for local watchpoints, we re-evaluate it only if
10693      watchpoints frame id is still valid.  If it's not, it means the
10694      watchpoint is out of scope and will be deleted soon.  In fact,
10695      I'm not sure we'll ever be called in this case.
10696
10697      If a local watchpoint's frame id is still valid, then
10698      w->exp_valid_block is likewise valid, and we can safely use it.
10699
10700      Don't do anything about disabled watchpoints, since they will be
10701      reevaluated again when enabled.  */
10702   update_watchpoint (w, 1 /* reparse */);
10703 }
10704
10705 /* Implement the "insert" breakpoint_ops method for hardware watchpoints.  */
10706
10707 static int
10708 insert_watchpoint (struct bp_location *bl)
10709 {
10710   struct watchpoint *w = (struct watchpoint *) bl->owner;
10711   int length = w->exact ? 1 : bl->length;
10712
10713   return target_insert_watchpoint (bl->address, length, bl->watchpoint_type,
10714                                    w->cond_exp);
10715 }
10716
10717 /* Implement the "remove" breakpoint_ops method for hardware watchpoints.  */
10718
10719 static int
10720 remove_watchpoint (struct bp_location *bl, enum remove_bp_reason reason)
10721 {
10722   struct watchpoint *w = (struct watchpoint *) bl->owner;
10723   int length = w->exact ? 1 : bl->length;
10724
10725   return target_remove_watchpoint (bl->address, length, bl->watchpoint_type,
10726                                    w->cond_exp);
10727 }
10728
10729 static int
10730 breakpoint_hit_watchpoint (const struct bp_location *bl,
10731                            struct address_space *aspace, CORE_ADDR bp_addr,
10732                            const struct target_waitstatus *ws)
10733 {
10734   struct breakpoint *b = bl->owner;
10735   struct watchpoint *w = (struct watchpoint *) b;
10736
10737   /* Continuable hardware watchpoints are treated as non-existent if the
10738      reason we stopped wasn't a hardware watchpoint (we didn't stop on
10739      some data address).  Otherwise gdb won't stop on a break instruction
10740      in the code (not from a breakpoint) when a hardware watchpoint has
10741      been defined.  Also skip watchpoints which we know did not trigger
10742      (did not match the data address).  */
10743   if (is_hardware_watchpoint (b)
10744       && w->watchpoint_triggered == watch_triggered_no)
10745     return 0;
10746
10747   return 1;
10748 }
10749
10750 static void
10751 check_status_watchpoint (bpstat bs)
10752 {
10753   gdb_assert (is_watchpoint (bs->breakpoint_at));
10754
10755   bpstat_check_watchpoint (bs);
10756 }
10757
10758 /* Implement the "resources_needed" breakpoint_ops method for
10759    hardware watchpoints.  */
10760
10761 static int
10762 resources_needed_watchpoint (const struct bp_location *bl)
10763 {
10764   struct watchpoint *w = (struct watchpoint *) bl->owner;
10765   int length = w->exact? 1 : bl->length;
10766
10767   return target_region_ok_for_hw_watchpoint (bl->address, length);
10768 }
10769
10770 /* Implement the "works_in_software_mode" breakpoint_ops method for
10771    hardware watchpoints.  */
10772
10773 static int
10774 works_in_software_mode_watchpoint (const struct breakpoint *b)
10775 {
10776   /* Read and access watchpoints only work with hardware support.  */
10777   return b->type == bp_watchpoint || b->type == bp_hardware_watchpoint;
10778 }
10779
10780 static enum print_stop_action
10781 print_it_watchpoint (bpstat bs)
10782 {
10783   struct cleanup *old_chain;
10784   struct breakpoint *b;
10785   struct ui_file *stb;
10786   enum print_stop_action result;
10787   struct watchpoint *w;
10788   struct ui_out *uiout = current_uiout;
10789
10790   gdb_assert (bs->bp_location_at != NULL);
10791
10792   b = bs->breakpoint_at;
10793   w = (struct watchpoint *) b;
10794
10795   stb = mem_fileopen ();
10796   old_chain = make_cleanup_ui_file_delete (stb);
10797
10798   annotate_watchpoint (b->number);
10799   maybe_print_thread_hit_breakpoint (uiout);
10800
10801   switch (b->type)
10802     {
10803     case bp_watchpoint:
10804     case bp_hardware_watchpoint:
10805       if (ui_out_is_mi_like_p (uiout))
10806         ui_out_field_string
10807           (uiout, "reason",
10808            async_reason_lookup (EXEC_ASYNC_WATCHPOINT_TRIGGER));
10809       mention (b);
10810       make_cleanup_ui_out_tuple_begin_end (uiout, "value");
10811       ui_out_text (uiout, "\nOld value = ");
10812       watchpoint_value_print (bs->old_val, stb);
10813       ui_out_field_stream (uiout, "old", stb);
10814       ui_out_text (uiout, "\nNew value = ");
10815       watchpoint_value_print (w->val, stb);
10816       ui_out_field_stream (uiout, "new", stb);
10817       ui_out_text (uiout, "\n");
10818       /* More than one watchpoint may have been triggered.  */
10819       result = PRINT_UNKNOWN;
10820       break;
10821
10822     case bp_read_watchpoint:
10823       if (ui_out_is_mi_like_p (uiout))
10824         ui_out_field_string
10825           (uiout, "reason",
10826            async_reason_lookup (EXEC_ASYNC_READ_WATCHPOINT_TRIGGER));
10827       mention (b);
10828       make_cleanup_ui_out_tuple_begin_end (uiout, "value");
10829       ui_out_text (uiout, "\nValue = ");
10830       watchpoint_value_print (w->val, stb);
10831       ui_out_field_stream (uiout, "value", stb);
10832       ui_out_text (uiout, "\n");
10833       result = PRINT_UNKNOWN;
10834       break;
10835
10836     case bp_access_watchpoint:
10837       if (bs->old_val != NULL)
10838         {
10839           if (ui_out_is_mi_like_p (uiout))
10840             ui_out_field_string
10841               (uiout, "reason",
10842                async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
10843           mention (b);
10844           make_cleanup_ui_out_tuple_begin_end (uiout, "value");
10845           ui_out_text (uiout, "\nOld value = ");
10846           watchpoint_value_print (bs->old_val, stb);
10847           ui_out_field_stream (uiout, "old", stb);
10848           ui_out_text (uiout, "\nNew value = ");
10849         }
10850       else
10851         {
10852           mention (b);
10853           if (ui_out_is_mi_like_p (uiout))
10854             ui_out_field_string
10855               (uiout, "reason",
10856                async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
10857           make_cleanup_ui_out_tuple_begin_end (uiout, "value");
10858           ui_out_text (uiout, "\nValue = ");
10859         }
10860       watchpoint_value_print (w->val, stb);
10861       ui_out_field_stream (uiout, "new", stb);
10862       ui_out_text (uiout, "\n");
10863       result = PRINT_UNKNOWN;
10864       break;
10865     default:
10866       result = PRINT_UNKNOWN;
10867     }
10868
10869   do_cleanups (old_chain);
10870   return result;
10871 }
10872
10873 /* Implement the "print_mention" breakpoint_ops method for hardware
10874    watchpoints.  */
10875
10876 static void
10877 print_mention_watchpoint (struct breakpoint *b)
10878 {
10879   struct cleanup *ui_out_chain;
10880   struct watchpoint *w = (struct watchpoint *) b;
10881   struct ui_out *uiout = current_uiout;
10882
10883   switch (b->type)
10884     {
10885     case bp_watchpoint:
10886       ui_out_text (uiout, "Watchpoint ");
10887       ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "wpt");
10888       break;
10889     case bp_hardware_watchpoint:
10890       ui_out_text (uiout, "Hardware watchpoint ");
10891       ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "wpt");
10892       break;
10893     case bp_read_watchpoint:
10894       ui_out_text (uiout, "Hardware read watchpoint ");
10895       ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "hw-rwpt");
10896       break;
10897     case bp_access_watchpoint:
10898       ui_out_text (uiout, "Hardware access (read/write) watchpoint ");
10899       ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "hw-awpt");
10900       break;
10901     default:
10902       internal_error (__FILE__, __LINE__,
10903                       _("Invalid hardware watchpoint type."));
10904     }
10905
10906   ui_out_field_int (uiout, "number", b->number);
10907   ui_out_text (uiout, ": ");
10908   ui_out_field_string (uiout, "exp", w->exp_string);
10909   do_cleanups (ui_out_chain);
10910 }
10911
10912 /* Implement the "print_recreate" breakpoint_ops method for
10913    watchpoints.  */
10914
10915 static void
10916 print_recreate_watchpoint (struct breakpoint *b, struct ui_file *fp)
10917 {
10918   struct watchpoint *w = (struct watchpoint *) b;
10919
10920   switch (b->type)
10921     {
10922     case bp_watchpoint:
10923     case bp_hardware_watchpoint:
10924       fprintf_unfiltered (fp, "watch");
10925       break;
10926     case bp_read_watchpoint:
10927       fprintf_unfiltered (fp, "rwatch");
10928       break;
10929     case bp_access_watchpoint:
10930       fprintf_unfiltered (fp, "awatch");
10931       break;
10932     default:
10933       internal_error (__FILE__, __LINE__,
10934                       _("Invalid watchpoint type."));
10935     }
10936
10937   fprintf_unfiltered (fp, " %s", w->exp_string);
10938   print_recreate_thread (b, fp);
10939 }
10940
10941 /* Implement the "explains_signal" breakpoint_ops method for
10942    watchpoints.  */
10943
10944 static int
10945 explains_signal_watchpoint (struct breakpoint *b, enum gdb_signal sig)
10946 {
10947   /* A software watchpoint cannot cause a signal other than
10948      GDB_SIGNAL_TRAP.  */
10949   if (b->type == bp_watchpoint && sig != GDB_SIGNAL_TRAP)
10950     return 0;
10951
10952   return 1;
10953 }
10954
10955 /* The breakpoint_ops structure to be used in hardware watchpoints.  */
10956
10957 static struct breakpoint_ops watchpoint_breakpoint_ops;
10958
10959 /* Implement the "insert" breakpoint_ops method for
10960    masked hardware watchpoints.  */
10961
10962 static int
10963 insert_masked_watchpoint (struct bp_location *bl)
10964 {
10965   struct watchpoint *w = (struct watchpoint *) bl->owner;
10966
10967   return target_insert_mask_watchpoint (bl->address, w->hw_wp_mask,
10968                                         bl->watchpoint_type);
10969 }
10970
10971 /* Implement the "remove" breakpoint_ops method for
10972    masked hardware watchpoints.  */
10973
10974 static int
10975 remove_masked_watchpoint (struct bp_location *bl, enum remove_bp_reason reason)
10976 {
10977   struct watchpoint *w = (struct watchpoint *) bl->owner;
10978
10979   return target_remove_mask_watchpoint (bl->address, w->hw_wp_mask,
10980                                         bl->watchpoint_type);
10981 }
10982
10983 /* Implement the "resources_needed" breakpoint_ops method for
10984    masked hardware watchpoints.  */
10985
10986 static int
10987 resources_needed_masked_watchpoint (const struct bp_location *bl)
10988 {
10989   struct watchpoint *w = (struct watchpoint *) bl->owner;
10990
10991   return target_masked_watch_num_registers (bl->address, w->hw_wp_mask);
10992 }
10993
10994 /* Implement the "works_in_software_mode" breakpoint_ops method for
10995    masked hardware watchpoints.  */
10996
10997 static int
10998 works_in_software_mode_masked_watchpoint (const struct breakpoint *b)
10999 {
11000   return 0;
11001 }
11002
11003 /* Implement the "print_it" breakpoint_ops method for
11004    masked hardware watchpoints.  */
11005
11006 static enum print_stop_action
11007 print_it_masked_watchpoint (bpstat bs)
11008 {
11009   struct breakpoint *b = bs->breakpoint_at;
11010   struct ui_out *uiout = current_uiout;
11011
11012   /* Masked watchpoints have only one location.  */
11013   gdb_assert (b->loc && b->loc->next == NULL);
11014
11015   annotate_watchpoint (b->number);
11016   maybe_print_thread_hit_breakpoint (uiout);
11017
11018   switch (b->type)
11019     {
11020     case bp_hardware_watchpoint:
11021       if (ui_out_is_mi_like_p (uiout))
11022         ui_out_field_string
11023           (uiout, "reason",
11024            async_reason_lookup (EXEC_ASYNC_WATCHPOINT_TRIGGER));
11025       break;
11026
11027     case bp_read_watchpoint:
11028       if (ui_out_is_mi_like_p (uiout))
11029         ui_out_field_string
11030           (uiout, "reason",
11031            async_reason_lookup (EXEC_ASYNC_READ_WATCHPOINT_TRIGGER));
11032       break;
11033
11034     case bp_access_watchpoint:
11035       if (ui_out_is_mi_like_p (uiout))
11036         ui_out_field_string
11037           (uiout, "reason",
11038            async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
11039       break;
11040     default:
11041       internal_error (__FILE__, __LINE__,
11042                       _("Invalid hardware watchpoint type."));
11043     }
11044
11045   mention (b);
11046   ui_out_text (uiout, _("\n\
11047 Check the underlying instruction at PC for the memory\n\
11048 address and value which triggered this watchpoint.\n"));
11049   ui_out_text (uiout, "\n");
11050
11051   /* More than one watchpoint may have been triggered.  */
11052   return PRINT_UNKNOWN;
11053 }
11054
11055 /* Implement the "print_one_detail" breakpoint_ops method for
11056    masked hardware watchpoints.  */
11057
11058 static void
11059 print_one_detail_masked_watchpoint (const struct breakpoint *b,
11060                                     struct ui_out *uiout)
11061 {
11062   struct watchpoint *w = (struct watchpoint *) b;
11063
11064   /* Masked watchpoints have only one location.  */
11065   gdb_assert (b->loc && b->loc->next == NULL);
11066
11067   ui_out_text (uiout, "\tmask ");
11068   ui_out_field_core_addr (uiout, "mask", b->loc->gdbarch, w->hw_wp_mask);
11069   ui_out_text (uiout, "\n");
11070 }
11071
11072 /* Implement the "print_mention" breakpoint_ops method for
11073    masked hardware watchpoints.  */
11074
11075 static void
11076 print_mention_masked_watchpoint (struct breakpoint *b)
11077 {
11078   struct watchpoint *w = (struct watchpoint *) b;
11079   struct ui_out *uiout = current_uiout;
11080   struct cleanup *ui_out_chain;
11081
11082   switch (b->type)
11083     {
11084     case bp_hardware_watchpoint:
11085       ui_out_text (uiout, "Masked hardware watchpoint ");
11086       ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "wpt");
11087       break;
11088     case bp_read_watchpoint:
11089       ui_out_text (uiout, "Masked hardware read watchpoint ");
11090       ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "hw-rwpt");
11091       break;
11092     case bp_access_watchpoint:
11093       ui_out_text (uiout, "Masked hardware access (read/write) watchpoint ");
11094       ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "hw-awpt");
11095       break;
11096     default:
11097       internal_error (__FILE__, __LINE__,
11098                       _("Invalid hardware watchpoint type."));
11099     }
11100
11101   ui_out_field_int (uiout, "number", b->number);
11102   ui_out_text (uiout, ": ");
11103   ui_out_field_string (uiout, "exp", w->exp_string);
11104   do_cleanups (ui_out_chain);
11105 }
11106
11107 /* Implement the "print_recreate" breakpoint_ops method for
11108    masked hardware watchpoints.  */
11109
11110 static void
11111 print_recreate_masked_watchpoint (struct breakpoint *b, struct ui_file *fp)
11112 {
11113   struct watchpoint *w = (struct watchpoint *) b;
11114   char tmp[40];
11115
11116   switch (b->type)
11117     {
11118     case bp_hardware_watchpoint:
11119       fprintf_unfiltered (fp, "watch");
11120       break;
11121     case bp_read_watchpoint:
11122       fprintf_unfiltered (fp, "rwatch");
11123       break;
11124     case bp_access_watchpoint:
11125       fprintf_unfiltered (fp, "awatch");
11126       break;
11127     default:
11128       internal_error (__FILE__, __LINE__,
11129                       _("Invalid hardware watchpoint type."));
11130     }
11131
11132   sprintf_vma (tmp, w->hw_wp_mask);
11133   fprintf_unfiltered (fp, " %s mask 0x%s", w->exp_string, tmp);
11134   print_recreate_thread (b, fp);
11135 }
11136
11137 /* The breakpoint_ops structure to be used in masked hardware watchpoints.  */
11138
11139 static struct breakpoint_ops masked_watchpoint_breakpoint_ops;
11140
11141 /* Tell whether the given watchpoint is a masked hardware watchpoint.  */
11142
11143 static int
11144 is_masked_watchpoint (const struct breakpoint *b)
11145 {
11146   return b->ops == &masked_watchpoint_breakpoint_ops;
11147 }
11148
11149 /* accessflag:  hw_write:  watch write, 
11150                 hw_read:   watch read, 
11151                 hw_access: watch access (read or write) */
11152 static void
11153 watch_command_1 (const char *arg, int accessflag, int from_tty,
11154                  int just_location, int internal)
11155 {
11156   struct breakpoint *b, *scope_breakpoint = NULL;
11157   struct expression *exp;
11158   const struct block *exp_valid_block = NULL, *cond_exp_valid_block = NULL;
11159   struct value *val, *mark, *result;
11160   int saved_bitpos = 0, saved_bitsize = 0;
11161   struct frame_info *frame;
11162   const char *exp_start = NULL;
11163   const char *exp_end = NULL;
11164   const char *tok, *end_tok;
11165   int toklen = -1;
11166   const char *cond_start = NULL;
11167   const char *cond_end = NULL;
11168   enum bptype bp_type;
11169   int thread = -1;
11170   int pc = 0;
11171   /* Flag to indicate whether we are going to use masks for
11172      the hardware watchpoint.  */
11173   int use_mask = 0;
11174   CORE_ADDR mask = 0;
11175   struct watchpoint *w;
11176   char *expression;
11177   struct cleanup *back_to;
11178
11179   /* Make sure that we actually have parameters to parse.  */
11180   if (arg != NULL && arg[0] != '\0')
11181     {
11182       const char *value_start;
11183
11184       exp_end = arg + strlen (arg);
11185
11186       /* Look for "parameter value" pairs at the end
11187          of the arguments string.  */
11188       for (tok = exp_end - 1; tok > arg; tok--)
11189         {
11190           /* Skip whitespace at the end of the argument list.  */
11191           while (tok > arg && (*tok == ' ' || *tok == '\t'))
11192             tok--;
11193
11194           /* Find the beginning of the last token.
11195              This is the value of the parameter.  */
11196           while (tok > arg && (*tok != ' ' && *tok != '\t'))
11197             tok--;
11198           value_start = tok + 1;
11199
11200           /* Skip whitespace.  */
11201           while (tok > arg && (*tok == ' ' || *tok == '\t'))
11202             tok--;
11203
11204           end_tok = tok;
11205
11206           /* Find the beginning of the second to last token.
11207              This is the parameter itself.  */
11208           while (tok > arg && (*tok != ' ' && *tok != '\t'))
11209             tok--;
11210           tok++;
11211           toklen = end_tok - tok + 1;
11212
11213           if (toklen == 6 && startswith (tok, "thread"))
11214             {
11215               struct thread_info *thr;
11216               /* At this point we've found a "thread" token, which means
11217                  the user is trying to set a watchpoint that triggers
11218                  only in a specific thread.  */
11219               const char *endp;
11220
11221               if (thread != -1)
11222                 error(_("You can specify only one thread."));
11223
11224               /* Extract the thread ID from the next token.  */
11225               thr = parse_thread_id (value_start, &endp);
11226
11227               /* Check if the user provided a valid thread ID.  */
11228               if (*endp != ' ' && *endp != '\t' && *endp != '\0')
11229                 invalid_thread_id_error (value_start);
11230
11231               thread = thr->global_num;
11232             }
11233           else if (toklen == 4 && startswith (tok, "mask"))
11234             {
11235               /* We've found a "mask" token, which means the user wants to
11236                  create a hardware watchpoint that is going to have the mask
11237                  facility.  */
11238               struct value *mask_value, *mark;
11239
11240               if (use_mask)
11241                 error(_("You can specify only one mask."));
11242
11243               use_mask = just_location = 1;
11244
11245               mark = value_mark ();
11246               mask_value = parse_to_comma_and_eval (&value_start);
11247               mask = value_as_address (mask_value);
11248               value_free_to_mark (mark);
11249             }
11250           else
11251             /* We didn't recognize what we found.  We should stop here.  */
11252             break;
11253
11254           /* Truncate the string and get rid of the "parameter value" pair before
11255              the arguments string is parsed by the parse_exp_1 function.  */
11256           exp_end = tok;
11257         }
11258     }
11259   else
11260     exp_end = arg;
11261
11262   /* Parse the rest of the arguments.  From here on out, everything
11263      is in terms of a newly allocated string instead of the original
11264      ARG.  */
11265   innermost_block = NULL;
11266   expression = savestring (arg, exp_end - arg);
11267   back_to = make_cleanup (xfree, expression);
11268   exp_start = arg = expression;
11269   exp = parse_exp_1 (&arg, 0, 0, 0);
11270   exp_end = arg;
11271   /* Remove trailing whitespace from the expression before saving it.
11272      This makes the eventual display of the expression string a bit
11273      prettier.  */
11274   while (exp_end > exp_start && (exp_end[-1] == ' ' || exp_end[-1] == '\t'))
11275     --exp_end;
11276
11277   /* Checking if the expression is not constant.  */
11278   if (watchpoint_exp_is_const (exp))
11279     {
11280       int len;
11281
11282       len = exp_end - exp_start;
11283       while (len > 0 && isspace (exp_start[len - 1]))
11284         len--;
11285       error (_("Cannot watch constant value `%.*s'."), len, exp_start);
11286     }
11287
11288   exp_valid_block = innermost_block;
11289   mark = value_mark ();
11290   fetch_subexp_value (exp, &pc, &val, &result, NULL, just_location);
11291
11292   if (val != NULL && just_location)
11293     {
11294       saved_bitpos = value_bitpos (val);
11295       saved_bitsize = value_bitsize (val);
11296     }
11297
11298   if (just_location)
11299     {
11300       int ret;
11301
11302       exp_valid_block = NULL;
11303       val = value_addr (result);
11304       release_value (val);
11305       value_free_to_mark (mark);
11306
11307       if (use_mask)
11308         {
11309           ret = target_masked_watch_num_registers (value_as_address (val),
11310                                                    mask);
11311           if (ret == -1)
11312             error (_("This target does not support masked watchpoints."));
11313           else if (ret == -2)
11314             error (_("Invalid mask or memory region."));
11315         }
11316     }
11317   else if (val != NULL)
11318     release_value (val);
11319
11320   tok = skip_spaces_const (arg);
11321   end_tok = skip_to_space_const (tok);
11322
11323   toklen = end_tok - tok;
11324   if (toklen >= 1 && strncmp (tok, "if", toklen) == 0)
11325     {
11326       struct expression *cond;
11327
11328       innermost_block = NULL;
11329       tok = cond_start = end_tok + 1;
11330       cond = parse_exp_1 (&tok, 0, 0, 0);
11331
11332       /* The watchpoint expression may not be local, but the condition
11333          may still be.  E.g.: `watch global if local > 0'.  */
11334       cond_exp_valid_block = innermost_block;
11335
11336       xfree (cond);
11337       cond_end = tok;
11338     }
11339   if (*tok)
11340     error (_("Junk at end of command."));
11341
11342   frame = block_innermost_frame (exp_valid_block);
11343
11344   /* If the expression is "local", then set up a "watchpoint scope"
11345      breakpoint at the point where we've left the scope of the watchpoint
11346      expression.  Create the scope breakpoint before the watchpoint, so
11347      that we will encounter it first in bpstat_stop_status.  */
11348   if (exp_valid_block && frame)
11349     {
11350       if (frame_id_p (frame_unwind_caller_id (frame)))
11351         {
11352           scope_breakpoint
11353             = create_internal_breakpoint (frame_unwind_caller_arch (frame),
11354                                           frame_unwind_caller_pc (frame),
11355                                           bp_watchpoint_scope,
11356                                           &momentary_breakpoint_ops);
11357
11358           scope_breakpoint->enable_state = bp_enabled;
11359
11360           /* Automatically delete the breakpoint when it hits.  */
11361           scope_breakpoint->disposition = disp_del;
11362
11363           /* Only break in the proper frame (help with recursion).  */
11364           scope_breakpoint->frame_id = frame_unwind_caller_id (frame);
11365
11366           /* Set the address at which we will stop.  */
11367           scope_breakpoint->loc->gdbarch
11368             = frame_unwind_caller_arch (frame);
11369           scope_breakpoint->loc->requested_address
11370             = frame_unwind_caller_pc (frame);
11371           scope_breakpoint->loc->address
11372             = adjust_breakpoint_address (scope_breakpoint->loc->gdbarch,
11373                                          scope_breakpoint->loc->requested_address,
11374                                          scope_breakpoint->type);
11375         }
11376     }
11377
11378   /* Now set up the breakpoint.  We create all watchpoints as hardware
11379      watchpoints here even if hardware watchpoints are turned off, a call
11380      to update_watchpoint later in this function will cause the type to
11381      drop back to bp_watchpoint (software watchpoint) if required.  */
11382
11383   if (accessflag == hw_read)
11384     bp_type = bp_read_watchpoint;
11385   else if (accessflag == hw_access)
11386     bp_type = bp_access_watchpoint;
11387   else
11388     bp_type = bp_hardware_watchpoint;
11389
11390   w = XCNEW (struct watchpoint);
11391   b = &w->base;
11392   if (use_mask)
11393     init_raw_breakpoint_without_location (b, NULL, bp_type,
11394                                           &masked_watchpoint_breakpoint_ops);
11395   else
11396     init_raw_breakpoint_without_location (b, NULL, bp_type,
11397                                           &watchpoint_breakpoint_ops);
11398   b->thread = thread;
11399   b->disposition = disp_donttouch;
11400   b->pspace = current_program_space;
11401   w->exp = exp;
11402   w->exp_valid_block = exp_valid_block;
11403   w->cond_exp_valid_block = cond_exp_valid_block;
11404   if (just_location)
11405     {
11406       struct type *t = value_type (val);
11407       CORE_ADDR addr = value_as_address (val);
11408       char *name;
11409
11410       t = check_typedef (TYPE_TARGET_TYPE (check_typedef (t)));
11411       name = type_to_string (t);
11412
11413       w->exp_string_reparse = xstrprintf ("* (%s *) %s", name,
11414                                           core_addr_to_string (addr));
11415       xfree (name);
11416
11417       w->exp_string = xstrprintf ("-location %.*s",
11418                                   (int) (exp_end - exp_start), exp_start);
11419
11420       /* The above expression is in C.  */
11421       b->language = language_c;
11422     }
11423   else
11424     w->exp_string = savestring (exp_start, exp_end - exp_start);
11425
11426   if (use_mask)
11427     {
11428       w->hw_wp_mask = mask;
11429     }
11430   else
11431     {
11432       w->val = val;
11433       w->val_bitpos = saved_bitpos;
11434       w->val_bitsize = saved_bitsize;
11435       w->val_valid = 1;
11436     }
11437
11438   if (cond_start)
11439     b->cond_string = savestring (cond_start, cond_end - cond_start);
11440   else
11441     b->cond_string = 0;
11442
11443   if (frame)
11444     {
11445       w->watchpoint_frame = get_frame_id (frame);
11446       w->watchpoint_thread = inferior_ptid;
11447     }
11448   else
11449     {
11450       w->watchpoint_frame = null_frame_id;
11451       w->watchpoint_thread = null_ptid;
11452     }
11453
11454   if (scope_breakpoint != NULL)
11455     {
11456       /* The scope breakpoint is related to the watchpoint.  We will
11457          need to act on them together.  */
11458       b->related_breakpoint = scope_breakpoint;
11459       scope_breakpoint->related_breakpoint = b;
11460     }
11461
11462   if (!just_location)
11463     value_free_to_mark (mark);
11464
11465   TRY
11466     {
11467       /* Finally update the new watchpoint.  This creates the locations
11468          that should be inserted.  */
11469       update_watchpoint (w, 1);
11470     }
11471   CATCH (e, RETURN_MASK_ALL)
11472     {
11473       delete_breakpoint (b);
11474       throw_exception (e);
11475     }
11476   END_CATCH
11477
11478   install_breakpoint (internal, b, 1);
11479   do_cleanups (back_to);
11480 }
11481
11482 /* Return count of debug registers needed to watch the given expression.
11483    If the watchpoint cannot be handled in hardware return zero.  */
11484
11485 static int
11486 can_use_hardware_watchpoint (struct value *v)
11487 {
11488   int found_memory_cnt = 0;
11489   struct value *head = v;
11490
11491   /* Did the user specifically forbid us to use hardware watchpoints? */
11492   if (!can_use_hw_watchpoints)
11493     return 0;
11494
11495   /* Make sure that the value of the expression depends only upon
11496      memory contents, and values computed from them within GDB.  If we
11497      find any register references or function calls, we can't use a
11498      hardware watchpoint.
11499
11500      The idea here is that evaluating an expression generates a series
11501      of values, one holding the value of every subexpression.  (The
11502      expression a*b+c has five subexpressions: a, b, a*b, c, and
11503      a*b+c.)  GDB's values hold almost enough information to establish
11504      the criteria given above --- they identify memory lvalues,
11505      register lvalues, computed values, etcetera.  So we can evaluate
11506      the expression, and then scan the chain of values that leaves
11507      behind to decide whether we can detect any possible change to the
11508      expression's final value using only hardware watchpoints.
11509
11510      However, I don't think that the values returned by inferior
11511      function calls are special in any way.  So this function may not
11512      notice that an expression involving an inferior function call
11513      can't be watched with hardware watchpoints.  FIXME.  */
11514   for (; v; v = value_next (v))
11515     {
11516       if (VALUE_LVAL (v) == lval_memory)
11517         {
11518           if (v != head && value_lazy (v))
11519             /* A lazy memory lvalue in the chain is one that GDB never
11520                needed to fetch; we either just used its address (e.g.,
11521                `a' in `a.b') or we never needed it at all (e.g., `a'
11522                in `a,b').  This doesn't apply to HEAD; if that is
11523                lazy then it was not readable, but watch it anyway.  */
11524             ;
11525           else
11526             {
11527               /* Ahh, memory we actually used!  Check if we can cover
11528                  it with hardware watchpoints.  */
11529               struct type *vtype = check_typedef (value_type (v));
11530
11531               /* We only watch structs and arrays if user asked for it
11532                  explicitly, never if they just happen to appear in a
11533                  middle of some value chain.  */
11534               if (v == head
11535                   || (TYPE_CODE (vtype) != TYPE_CODE_STRUCT
11536                       && TYPE_CODE (vtype) != TYPE_CODE_ARRAY))
11537                 {
11538                   CORE_ADDR vaddr = value_address (v);
11539                   int len;
11540                   int num_regs;
11541
11542                   len = (target_exact_watchpoints
11543                          && is_scalar_type_recursive (vtype))?
11544                     1 : TYPE_LENGTH (value_type (v));
11545
11546                   num_regs = target_region_ok_for_hw_watchpoint (vaddr, len);
11547                   if (!num_regs)
11548                     return 0;
11549                   else
11550                     found_memory_cnt += num_regs;
11551                 }
11552             }
11553         }
11554       else if (VALUE_LVAL (v) != not_lval
11555                && deprecated_value_modifiable (v) == 0)
11556         return 0;       /* These are values from the history (e.g., $1).  */
11557       else if (VALUE_LVAL (v) == lval_register)
11558         return 0;       /* Cannot watch a register with a HW watchpoint.  */
11559     }
11560
11561   /* The expression itself looks suitable for using a hardware
11562      watchpoint, but give the target machine a chance to reject it.  */
11563   return found_memory_cnt;
11564 }
11565
11566 void
11567 watch_command_wrapper (char *arg, int from_tty, int internal)
11568 {
11569   watch_command_1 (arg, hw_write, from_tty, 0, internal);
11570 }
11571
11572 /* A helper function that looks for the "-location" argument and then
11573    calls watch_command_1.  */
11574
11575 static void
11576 watch_maybe_just_location (char *arg, int accessflag, int from_tty)
11577 {
11578   int just_location = 0;
11579
11580   if (arg
11581       && (check_for_argument (&arg, "-location", sizeof ("-location") - 1)
11582           || check_for_argument (&arg, "-l", sizeof ("-l") - 1)))
11583     {
11584       arg = skip_spaces (arg);
11585       just_location = 1;
11586     }
11587
11588   watch_command_1 (arg, accessflag, from_tty, just_location, 0);
11589 }
11590
11591 static void
11592 watch_command (char *arg, int from_tty)
11593 {
11594   watch_maybe_just_location (arg, hw_write, from_tty);
11595 }
11596
11597 void
11598 rwatch_command_wrapper (char *arg, int from_tty, int internal)
11599 {
11600   watch_command_1 (arg, hw_read, from_tty, 0, internal);
11601 }
11602
11603 static void
11604 rwatch_command (char *arg, int from_tty)
11605 {
11606   watch_maybe_just_location (arg, hw_read, from_tty);
11607 }
11608
11609 void
11610 awatch_command_wrapper (char *arg, int from_tty, int internal)
11611 {
11612   watch_command_1 (arg, hw_access, from_tty, 0, internal);
11613 }
11614
11615 static void
11616 awatch_command (char *arg, int from_tty)
11617 {
11618   watch_maybe_just_location (arg, hw_access, from_tty);
11619 }
11620 \f
11621
11622 /* Data for the FSM that manages the until(location)/advance commands
11623    in infcmd.c.  Here because it uses the mechanisms of
11624    breakpoints.  */
11625
11626 struct until_break_fsm
11627 {
11628   /* The base class.  */
11629   struct thread_fsm thread_fsm;
11630
11631   /* The thread that as current when the command was executed.  */
11632   int thread;
11633
11634   /* The breakpoint set at the destination location.  */
11635   struct breakpoint *location_breakpoint;
11636
11637   /* Breakpoint set at the return address in the caller frame.  May be
11638      NULL.  */
11639   struct breakpoint *caller_breakpoint;
11640 };
11641
11642 static void until_break_fsm_clean_up (struct thread_fsm *self,
11643                                       struct thread_info *thread);
11644 static int until_break_fsm_should_stop (struct thread_fsm *self,
11645                                         struct thread_info *thread);
11646 static enum async_reply_reason
11647   until_break_fsm_async_reply_reason (struct thread_fsm *self);
11648
11649 /* until_break_fsm's vtable.  */
11650
11651 static struct thread_fsm_ops until_break_fsm_ops =
11652 {
11653   NULL, /* dtor */
11654   until_break_fsm_clean_up,
11655   until_break_fsm_should_stop,
11656   NULL, /* return_value */
11657   until_break_fsm_async_reply_reason,
11658 };
11659
11660 /* Allocate a new until_break_command_fsm.  */
11661
11662 static struct until_break_fsm *
11663 new_until_break_fsm (struct interp *cmd_interp, int thread,
11664                      struct breakpoint *location_breakpoint,
11665                      struct breakpoint *caller_breakpoint)
11666 {
11667   struct until_break_fsm *sm;
11668
11669   sm = XCNEW (struct until_break_fsm);
11670   thread_fsm_ctor (&sm->thread_fsm, &until_break_fsm_ops, cmd_interp);
11671
11672   sm->thread = thread;
11673   sm->location_breakpoint = location_breakpoint;
11674   sm->caller_breakpoint = caller_breakpoint;
11675
11676   return sm;
11677 }
11678
11679 /* Implementation of the 'should_stop' FSM method for the
11680    until(location)/advance commands.  */
11681
11682 static int
11683 until_break_fsm_should_stop (struct thread_fsm *self,
11684                              struct thread_info *tp)
11685 {
11686   struct until_break_fsm *sm = (struct until_break_fsm *) self;
11687
11688   if (bpstat_find_breakpoint (tp->control.stop_bpstat,
11689                               sm->location_breakpoint) != NULL
11690       || (sm->caller_breakpoint != NULL
11691           && bpstat_find_breakpoint (tp->control.stop_bpstat,
11692                                      sm->caller_breakpoint) != NULL))
11693     thread_fsm_set_finished (self);
11694
11695   return 1;
11696 }
11697
11698 /* Implementation of the 'clean_up' FSM method for the
11699    until(location)/advance commands.  */
11700
11701 static void
11702 until_break_fsm_clean_up (struct thread_fsm *self,
11703                           struct thread_info *thread)
11704 {
11705   struct until_break_fsm *sm = (struct until_break_fsm *) self;
11706
11707   /* Clean up our temporary breakpoints.  */
11708   if (sm->location_breakpoint != NULL)
11709     {
11710       delete_breakpoint (sm->location_breakpoint);
11711       sm->location_breakpoint = NULL;
11712     }
11713   if (sm->caller_breakpoint != NULL)
11714     {
11715       delete_breakpoint (sm->caller_breakpoint);
11716       sm->caller_breakpoint = NULL;
11717     }
11718   delete_longjmp_breakpoint (sm->thread);
11719 }
11720
11721 /* Implementation of the 'async_reply_reason' FSM method for the
11722    until(location)/advance commands.  */
11723
11724 static enum async_reply_reason
11725 until_break_fsm_async_reply_reason (struct thread_fsm *self)
11726 {
11727   return EXEC_ASYNC_LOCATION_REACHED;
11728 }
11729
11730 void
11731 until_break_command (char *arg, int from_tty, int anywhere)
11732 {
11733   struct symtabs_and_lines sals;
11734   struct symtab_and_line sal;
11735   struct frame_info *frame;
11736   struct gdbarch *frame_gdbarch;
11737   struct frame_id stack_frame_id;
11738   struct frame_id caller_frame_id;
11739   struct breakpoint *location_breakpoint;
11740   struct breakpoint *caller_breakpoint = NULL;
11741   struct cleanup *old_chain, *cleanup;
11742   int thread;
11743   struct thread_info *tp;
11744   struct event_location *location;
11745   struct until_break_fsm *sm;
11746
11747   clear_proceed_status (0);
11748
11749   /* Set a breakpoint where the user wants it and at return from
11750      this function.  */
11751
11752   location = string_to_event_location (&arg, current_language);
11753   cleanup = make_cleanup_delete_event_location (location);
11754
11755   if (last_displayed_sal_is_valid ())
11756     sals = decode_line_1 (location, DECODE_LINE_FUNFIRSTLINE, NULL,
11757                           get_last_displayed_symtab (),
11758                           get_last_displayed_line ());
11759   else
11760     sals = decode_line_1 (location, DECODE_LINE_FUNFIRSTLINE,
11761                           NULL, (struct symtab *) NULL, 0);
11762
11763   if (sals.nelts != 1)
11764     error (_("Couldn't get information on specified line."));
11765
11766   sal = sals.sals[0];
11767   xfree (sals.sals);    /* malloc'd, so freed.  */
11768
11769   if (*arg)
11770     error (_("Junk at end of arguments."));
11771
11772   resolve_sal_pc (&sal);
11773
11774   tp = inferior_thread ();
11775   thread = tp->global_num;
11776
11777   old_chain = make_cleanup (null_cleanup, NULL);
11778
11779   /* Note linespec handling above invalidates the frame chain.
11780      Installing a breakpoint also invalidates the frame chain (as it
11781      may need to switch threads), so do any frame handling before
11782      that.  */
11783
11784   frame = get_selected_frame (NULL);
11785   frame_gdbarch = get_frame_arch (frame);
11786   stack_frame_id = get_stack_frame_id (frame);
11787   caller_frame_id = frame_unwind_caller_id (frame);
11788
11789   /* Keep within the current frame, or in frames called by the current
11790      one.  */
11791
11792   if (frame_id_p (caller_frame_id))
11793     {
11794       struct symtab_and_line sal2;
11795       struct gdbarch *caller_gdbarch;
11796
11797       sal2 = find_pc_line (frame_unwind_caller_pc (frame), 0);
11798       sal2.pc = frame_unwind_caller_pc (frame);
11799       caller_gdbarch = frame_unwind_caller_arch (frame);
11800       caller_breakpoint = set_momentary_breakpoint (caller_gdbarch,
11801                                                     sal2,
11802                                                     caller_frame_id,
11803                                                     bp_until);
11804       make_cleanup_delete_breakpoint (caller_breakpoint);
11805
11806       set_longjmp_breakpoint (tp, caller_frame_id);
11807       make_cleanup (delete_longjmp_breakpoint_cleanup, &thread);
11808     }
11809
11810   /* set_momentary_breakpoint could invalidate FRAME.  */
11811   frame = NULL;
11812
11813   if (anywhere)
11814     /* If the user told us to continue until a specified location,
11815        we don't specify a frame at which we need to stop.  */
11816     location_breakpoint = set_momentary_breakpoint (frame_gdbarch, sal,
11817                                                     null_frame_id, bp_until);
11818   else
11819     /* Otherwise, specify the selected frame, because we want to stop
11820        only at the very same frame.  */
11821     location_breakpoint = set_momentary_breakpoint (frame_gdbarch, sal,
11822                                                     stack_frame_id, bp_until);
11823   make_cleanup_delete_breakpoint (location_breakpoint);
11824
11825   sm = new_until_break_fsm (command_interp (), tp->global_num,
11826                             location_breakpoint, caller_breakpoint);
11827   tp->thread_fsm = &sm->thread_fsm;
11828
11829   discard_cleanups (old_chain);
11830
11831   proceed (-1, GDB_SIGNAL_DEFAULT);
11832
11833   do_cleanups (cleanup);
11834 }
11835
11836 /* This function attempts to parse an optional "if <cond>" clause
11837    from the arg string.  If one is not found, it returns NULL.
11838
11839    Else, it returns a pointer to the condition string.  (It does not
11840    attempt to evaluate the string against a particular block.)  And,
11841    it updates arg to point to the first character following the parsed
11842    if clause in the arg string.  */
11843
11844 char *
11845 ep_parse_optional_if_clause (char **arg)
11846 {
11847   char *cond_string;
11848
11849   if (((*arg)[0] != 'i') || ((*arg)[1] != 'f') || !isspace ((*arg)[2]))
11850     return NULL;
11851
11852   /* Skip the "if" keyword.  */
11853   (*arg) += 2;
11854
11855   /* Skip any extra leading whitespace, and record the start of the
11856      condition string.  */
11857   *arg = skip_spaces (*arg);
11858   cond_string = *arg;
11859
11860   /* Assume that the condition occupies the remainder of the arg
11861      string.  */
11862   (*arg) += strlen (cond_string);
11863
11864   return cond_string;
11865 }
11866
11867 /* Commands to deal with catching events, such as signals, exceptions,
11868    process start/exit, etc.  */
11869
11870 typedef enum
11871 {
11872   catch_fork_temporary, catch_vfork_temporary,
11873   catch_fork_permanent, catch_vfork_permanent
11874 }
11875 catch_fork_kind;
11876
11877 static void
11878 catch_fork_command_1 (char *arg, int from_tty, 
11879                       struct cmd_list_element *command)
11880 {
11881   struct gdbarch *gdbarch = get_current_arch ();
11882   char *cond_string = NULL;
11883   catch_fork_kind fork_kind;
11884   int tempflag;
11885
11886   fork_kind = (catch_fork_kind) (uintptr_t) get_cmd_context (command);
11887   tempflag = (fork_kind == catch_fork_temporary
11888               || fork_kind == catch_vfork_temporary);
11889
11890   if (!arg)
11891     arg = "";
11892   arg = skip_spaces (arg);
11893
11894   /* The allowed syntax is:
11895      catch [v]fork
11896      catch [v]fork if <cond>
11897
11898      First, check if there's an if clause.  */
11899   cond_string = ep_parse_optional_if_clause (&arg);
11900
11901   if ((*arg != '\0') && !isspace (*arg))
11902     error (_("Junk at end of arguments."));
11903
11904   /* If this target supports it, create a fork or vfork catchpoint
11905      and enable reporting of such events.  */
11906   switch (fork_kind)
11907     {
11908     case catch_fork_temporary:
11909     case catch_fork_permanent:
11910       create_fork_vfork_event_catchpoint (gdbarch, tempflag, cond_string,
11911                                           &catch_fork_breakpoint_ops);
11912       break;
11913     case catch_vfork_temporary:
11914     case catch_vfork_permanent:
11915       create_fork_vfork_event_catchpoint (gdbarch, tempflag, cond_string,
11916                                           &catch_vfork_breakpoint_ops);
11917       break;
11918     default:
11919       error (_("unsupported or unknown fork kind; cannot catch it"));
11920       break;
11921     }
11922 }
11923
11924 static void
11925 catch_exec_command_1 (char *arg, int from_tty, 
11926                       struct cmd_list_element *command)
11927 {
11928   struct exec_catchpoint *c;
11929   struct gdbarch *gdbarch = get_current_arch ();
11930   int tempflag;
11931   char *cond_string = NULL;
11932
11933   tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
11934
11935   if (!arg)
11936     arg = "";
11937   arg = skip_spaces (arg);
11938
11939   /* The allowed syntax is:
11940      catch exec
11941      catch exec if <cond>
11942
11943      First, check if there's an if clause.  */
11944   cond_string = ep_parse_optional_if_clause (&arg);
11945
11946   if ((*arg != '\0') && !isspace (*arg))
11947     error (_("Junk at end of arguments."));
11948
11949   c = XNEW (struct exec_catchpoint);
11950   init_catchpoint (&c->base, gdbarch, tempflag, cond_string,
11951                    &catch_exec_breakpoint_ops);
11952   c->exec_pathname = NULL;
11953
11954   install_breakpoint (0, &c->base, 1);
11955 }
11956
11957 void
11958 init_ada_exception_breakpoint (struct breakpoint *b,
11959                                struct gdbarch *gdbarch,
11960                                struct symtab_and_line sal,
11961                                char *addr_string,
11962                                const struct breakpoint_ops *ops,
11963                                int tempflag,
11964                                int enabled,
11965                                int from_tty)
11966 {
11967   if (from_tty)
11968     {
11969       struct gdbarch *loc_gdbarch = get_sal_arch (sal);
11970       if (!loc_gdbarch)
11971         loc_gdbarch = gdbarch;
11972
11973       describe_other_breakpoints (loc_gdbarch,
11974                                   sal.pspace, sal.pc, sal.section, -1);
11975       /* FIXME: brobecker/2006-12-28: Actually, re-implement a special
11976          version for exception catchpoints, because two catchpoints
11977          used for different exception names will use the same address.
11978          In this case, a "breakpoint ... also set at..." warning is
11979          unproductive.  Besides, the warning phrasing is also a bit
11980          inappropriate, we should use the word catchpoint, and tell
11981          the user what type of catchpoint it is.  The above is good
11982          enough for now, though.  */
11983     }
11984
11985   init_raw_breakpoint (b, gdbarch, sal, bp_breakpoint, ops);
11986
11987   b->enable_state = enabled ? bp_enabled : bp_disabled;
11988   b->disposition = tempflag ? disp_del : disp_donttouch;
11989   b->location = string_to_event_location (&addr_string,
11990                                           language_def (language_ada));
11991   b->language = language_ada;
11992 }
11993
11994 static void
11995 catch_command (char *arg, int from_tty)
11996 {
11997   error (_("Catch requires an event name."));
11998 }
11999 \f
12000
12001 static void
12002 tcatch_command (char *arg, int from_tty)
12003 {
12004   error (_("Catch requires an event name."));
12005 }
12006
12007 /* A qsort comparison function that sorts breakpoints in order.  */
12008
12009 static int
12010 compare_breakpoints (const void *a, const void *b)
12011 {
12012   const breakpoint_p *ba = (const breakpoint_p *) a;
12013   uintptr_t ua = (uintptr_t) *ba;
12014   const breakpoint_p *bb = (const breakpoint_p *) b;
12015   uintptr_t ub = (uintptr_t) *bb;
12016
12017   if ((*ba)->number < (*bb)->number)
12018     return -1;
12019   else if ((*ba)->number > (*bb)->number)
12020     return 1;
12021
12022   /* Now sort by address, in case we see, e..g, two breakpoints with
12023      the number 0.  */
12024   if (ua < ub)
12025     return -1;
12026   return ua > ub ? 1 : 0;
12027 }
12028
12029 /* Delete breakpoints by address or line.  */
12030
12031 static void
12032 clear_command (char *arg, int from_tty)
12033 {
12034   struct breakpoint *b, *prev;
12035   VEC(breakpoint_p) *found = 0;
12036   int ix;
12037   int default_match;
12038   struct symtabs_and_lines sals;
12039   struct symtab_and_line sal;
12040   int i;
12041   struct cleanup *cleanups = make_cleanup (null_cleanup, NULL);
12042
12043   if (arg)
12044     {
12045       sals = decode_line_with_current_source (arg,
12046                                               (DECODE_LINE_FUNFIRSTLINE
12047                                                | DECODE_LINE_LIST_MODE));
12048       make_cleanup (xfree, sals.sals);
12049       default_match = 0;
12050     }
12051   else
12052     {
12053       sals.sals = XNEW (struct symtab_and_line);
12054       make_cleanup (xfree, sals.sals);
12055       init_sal (&sal);          /* Initialize to zeroes.  */
12056
12057       /* Set sal's line, symtab, pc, and pspace to the values
12058          corresponding to the last call to print_frame_info.  If the
12059          codepoint is not valid, this will set all the fields to 0.  */
12060       get_last_displayed_sal (&sal);
12061       if (sal.symtab == 0)
12062         error (_("No source file specified."));
12063
12064       sals.sals[0] = sal;
12065       sals.nelts = 1;
12066
12067       default_match = 1;
12068     }
12069
12070   /* We don't call resolve_sal_pc here.  That's not as bad as it
12071      seems, because all existing breakpoints typically have both
12072      file/line and pc set.  So, if clear is given file/line, we can
12073      match this to existing breakpoint without obtaining pc at all.
12074
12075      We only support clearing given the address explicitly 
12076      present in breakpoint table.  Say, we've set breakpoint 
12077      at file:line.  There were several PC values for that file:line,
12078      due to optimization, all in one block.
12079
12080      We've picked one PC value.  If "clear" is issued with another
12081      PC corresponding to the same file:line, the breakpoint won't
12082      be cleared.  We probably can still clear the breakpoint, but 
12083      since the other PC value is never presented to user, user
12084      can only find it by guessing, and it does not seem important
12085      to support that.  */
12086
12087   /* For each line spec given, delete bps which correspond to it.  Do
12088      it in two passes, solely to preserve the current behavior that
12089      from_tty is forced true if we delete more than one
12090      breakpoint.  */
12091
12092   found = NULL;
12093   make_cleanup (VEC_cleanup (breakpoint_p), &found);
12094   for (i = 0; i < sals.nelts; i++)
12095     {
12096       const char *sal_fullname;
12097
12098       /* If exact pc given, clear bpts at that pc.
12099          If line given (pc == 0), clear all bpts on specified line.
12100          If defaulting, clear all bpts on default line
12101          or at default pc.
12102
12103          defaulting    sal.pc != 0    tests to do
12104
12105          0              1             pc
12106          1              1             pc _and_ line
12107          0              0             line
12108          1              0             <can't happen> */
12109
12110       sal = sals.sals[i];
12111       sal_fullname = (sal.symtab == NULL
12112                       ? NULL : symtab_to_fullname (sal.symtab));
12113
12114       /* Find all matching breakpoints and add them to 'found'.  */
12115       ALL_BREAKPOINTS (b)
12116         {
12117           int match = 0;
12118           /* Are we going to delete b?  */
12119           if (b->type != bp_none && !is_watchpoint (b))
12120             {
12121               struct bp_location *loc = b->loc;
12122               for (; loc; loc = loc->next)
12123                 {
12124                   /* If the user specified file:line, don't allow a PC
12125                      match.  This matches historical gdb behavior.  */
12126                   int pc_match = (!sal.explicit_line
12127                                   && sal.pc
12128                                   && (loc->pspace == sal.pspace)
12129                                   && (loc->address == sal.pc)
12130                                   && (!section_is_overlay (loc->section)
12131                                       || loc->section == sal.section));
12132                   int line_match = 0;
12133
12134                   if ((default_match || sal.explicit_line)
12135                       && loc->symtab != NULL
12136                       && sal_fullname != NULL
12137                       && sal.pspace == loc->pspace
12138                       && loc->line_number == sal.line
12139                       && filename_cmp (symtab_to_fullname (loc->symtab),
12140                                        sal_fullname) == 0)
12141                     line_match = 1;
12142
12143                   if (pc_match || line_match)
12144                     {
12145                       match = 1;
12146                       break;
12147                     }
12148                 }
12149             }
12150
12151           if (match)
12152             VEC_safe_push(breakpoint_p, found, b);
12153         }
12154     }
12155
12156   /* Now go thru the 'found' chain and delete them.  */
12157   if (VEC_empty(breakpoint_p, found))
12158     {
12159       if (arg)
12160         error (_("No breakpoint at %s."), arg);
12161       else
12162         error (_("No breakpoint at this line."));
12163     }
12164
12165   /* Remove duplicates from the vec.  */
12166   qsort (VEC_address (breakpoint_p, found),
12167          VEC_length (breakpoint_p, found),
12168          sizeof (breakpoint_p),
12169          compare_breakpoints);
12170   prev = VEC_index (breakpoint_p, found, 0);
12171   for (ix = 1; VEC_iterate (breakpoint_p, found, ix, b); ++ix)
12172     {
12173       if (b == prev)
12174         {
12175           VEC_ordered_remove (breakpoint_p, found, ix);
12176           --ix;
12177         }
12178     }
12179
12180   if (VEC_length(breakpoint_p, found) > 1)
12181     from_tty = 1;       /* Always report if deleted more than one.  */
12182   if (from_tty)
12183     {
12184       if (VEC_length(breakpoint_p, found) == 1)
12185         printf_unfiltered (_("Deleted breakpoint "));
12186       else
12187         printf_unfiltered (_("Deleted breakpoints "));
12188     }
12189
12190   for (ix = 0; VEC_iterate(breakpoint_p, found, ix, b); ix++)
12191     {
12192       if (from_tty)
12193         printf_unfiltered ("%d ", b->number);
12194       delete_breakpoint (b);
12195     }
12196   if (from_tty)
12197     putchar_unfiltered ('\n');
12198
12199   do_cleanups (cleanups);
12200 }
12201 \f
12202 /* Delete breakpoint in BS if they are `delete' breakpoints and
12203    all breakpoints that are marked for deletion, whether hit or not.
12204    This is called after any breakpoint is hit, or after errors.  */
12205
12206 void
12207 breakpoint_auto_delete (bpstat bs)
12208 {
12209   struct breakpoint *b, *b_tmp;
12210
12211   for (; bs; bs = bs->next)
12212     if (bs->breakpoint_at
12213         && bs->breakpoint_at->disposition == disp_del
12214         && bs->stop)
12215       delete_breakpoint (bs->breakpoint_at);
12216
12217   ALL_BREAKPOINTS_SAFE (b, b_tmp)
12218   {
12219     if (b->disposition == disp_del_at_next_stop)
12220       delete_breakpoint (b);
12221   }
12222 }
12223
12224 /* A comparison function for bp_location AP and BP being interfaced to
12225    qsort.  Sort elements primarily by their ADDRESS (no matter what
12226    does breakpoint_address_is_meaningful say for its OWNER),
12227    secondarily by ordering first permanent elements and
12228    terciarily just ensuring the array is sorted stable way despite
12229    qsort being an unstable algorithm.  */
12230
12231 static int
12232 bp_location_compare (const void *ap, const void *bp)
12233 {
12234   const struct bp_location *a = *(const struct bp_location **) ap;
12235   const struct bp_location *b = *(const struct bp_location **) bp;
12236
12237   if (a->address != b->address)
12238     return (a->address > b->address) - (a->address < b->address);
12239
12240   /* Sort locations at the same address by their pspace number, keeping
12241      locations of the same inferior (in a multi-inferior environment)
12242      grouped.  */
12243
12244   if (a->pspace->num != b->pspace->num)
12245     return ((a->pspace->num > b->pspace->num)
12246             - (a->pspace->num < b->pspace->num));
12247
12248   /* Sort permanent breakpoints first.  */
12249   if (a->permanent != b->permanent)
12250     return (a->permanent < b->permanent) - (a->permanent > b->permanent);
12251
12252   /* Make the internal GDB representation stable across GDB runs
12253      where A and B memory inside GDB can differ.  Breakpoint locations of
12254      the same type at the same address can be sorted in arbitrary order.  */
12255
12256   if (a->owner->number != b->owner->number)
12257     return ((a->owner->number > b->owner->number)
12258             - (a->owner->number < b->owner->number));
12259
12260   return (a > b) - (a < b);
12261 }
12262
12263 /* Set bp_location_placed_address_before_address_max and
12264    bp_location_shadow_len_after_address_max according to the current
12265    content of the bp_location array.  */
12266
12267 static void
12268 bp_location_target_extensions_update (void)
12269 {
12270   struct bp_location *bl, **blp_tmp;
12271
12272   bp_location_placed_address_before_address_max = 0;
12273   bp_location_shadow_len_after_address_max = 0;
12274
12275   ALL_BP_LOCATIONS (bl, blp_tmp)
12276     {
12277       CORE_ADDR start, end, addr;
12278
12279       if (!bp_location_has_shadow (bl))
12280         continue;
12281
12282       start = bl->target_info.placed_address;
12283       end = start + bl->target_info.shadow_len;
12284
12285       gdb_assert (bl->address >= start);
12286       addr = bl->address - start;
12287       if (addr > bp_location_placed_address_before_address_max)
12288         bp_location_placed_address_before_address_max = addr;
12289
12290       /* Zero SHADOW_LEN would not pass bp_location_has_shadow.  */
12291
12292       gdb_assert (bl->address < end);
12293       addr = end - bl->address;
12294       if (addr > bp_location_shadow_len_after_address_max)
12295         bp_location_shadow_len_after_address_max = addr;
12296     }
12297 }
12298
12299 /* Download tracepoint locations if they haven't been.  */
12300
12301 static void
12302 download_tracepoint_locations (void)
12303 {
12304   struct breakpoint *b;
12305   struct cleanup *old_chain;
12306   enum tribool can_download_tracepoint = TRIBOOL_UNKNOWN;
12307
12308   old_chain = save_current_space_and_thread ();
12309
12310   ALL_TRACEPOINTS (b)
12311     {
12312       struct bp_location *bl;
12313       struct tracepoint *t;
12314       int bp_location_downloaded = 0;
12315
12316       if ((b->type == bp_fast_tracepoint
12317            ? !may_insert_fast_tracepoints
12318            : !may_insert_tracepoints))
12319         continue;
12320
12321       if (can_download_tracepoint == TRIBOOL_UNKNOWN)
12322         {
12323           if (target_can_download_tracepoint ())
12324             can_download_tracepoint = TRIBOOL_TRUE;
12325           else
12326             can_download_tracepoint = TRIBOOL_FALSE;
12327         }
12328
12329       if (can_download_tracepoint == TRIBOOL_FALSE)
12330         break;
12331
12332       for (bl = b->loc; bl; bl = bl->next)
12333         {
12334           /* In tracepoint, locations are _never_ duplicated, so
12335              should_be_inserted is equivalent to
12336              unduplicated_should_be_inserted.  */
12337           if (!should_be_inserted (bl) || bl->inserted)
12338             continue;
12339
12340           switch_to_program_space_and_thread (bl->pspace);
12341
12342           target_download_tracepoint (bl);
12343
12344           bl->inserted = 1;
12345           bp_location_downloaded = 1;
12346         }
12347       t = (struct tracepoint *) b;
12348       t->number_on_target = b->number;
12349       if (bp_location_downloaded)
12350         observer_notify_breakpoint_modified (b);
12351     }
12352
12353   do_cleanups (old_chain);
12354 }
12355
12356 /* Swap the insertion/duplication state between two locations.  */
12357
12358 static void
12359 swap_insertion (struct bp_location *left, struct bp_location *right)
12360 {
12361   const int left_inserted = left->inserted;
12362   const int left_duplicate = left->duplicate;
12363   const int left_needs_update = left->needs_update;
12364   const struct bp_target_info left_target_info = left->target_info;
12365
12366   /* Locations of tracepoints can never be duplicated.  */
12367   if (is_tracepoint (left->owner))
12368     gdb_assert (!left->duplicate);
12369   if (is_tracepoint (right->owner))
12370     gdb_assert (!right->duplicate);
12371
12372   left->inserted = right->inserted;
12373   left->duplicate = right->duplicate;
12374   left->needs_update = right->needs_update;
12375   left->target_info = right->target_info;
12376   right->inserted = left_inserted;
12377   right->duplicate = left_duplicate;
12378   right->needs_update = left_needs_update;
12379   right->target_info = left_target_info;
12380 }
12381
12382 /* Force the re-insertion of the locations at ADDRESS.  This is called
12383    once a new/deleted/modified duplicate location is found and we are evaluating
12384    conditions on the target's side.  Such conditions need to be updated on
12385    the target.  */
12386
12387 static void
12388 force_breakpoint_reinsertion (struct bp_location *bl)
12389 {
12390   struct bp_location **locp = NULL, **loc2p;
12391   struct bp_location *loc;
12392   CORE_ADDR address = 0;
12393   int pspace_num;
12394
12395   address = bl->address;
12396   pspace_num = bl->pspace->num;
12397
12398   /* This is only meaningful if the target is
12399      evaluating conditions and if the user has
12400      opted for condition evaluation on the target's
12401      side.  */
12402   if (gdb_evaluates_breakpoint_condition_p ()
12403       || !target_supports_evaluation_of_breakpoint_conditions ())
12404     return;
12405
12406   /* Flag all breakpoint locations with this address and
12407      the same program space as the location
12408      as "its condition has changed".  We need to
12409      update the conditions on the target's side.  */
12410   ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, address)
12411     {
12412       loc = *loc2p;
12413
12414       if (!is_breakpoint (loc->owner)
12415           || pspace_num != loc->pspace->num)
12416         continue;
12417
12418       /* Flag the location appropriately.  We use a different state to
12419          let everyone know that we already updated the set of locations
12420          with addr bl->address and program space bl->pspace.  This is so
12421          we don't have to keep calling these functions just to mark locations
12422          that have already been marked.  */
12423       loc->condition_changed = condition_updated;
12424
12425       /* Free the agent expression bytecode as well.  We will compute
12426          it later on.  */
12427       if (loc->cond_bytecode)
12428         {
12429           free_agent_expr (loc->cond_bytecode);
12430           loc->cond_bytecode = NULL;
12431         }
12432     }
12433 }
12434 /* Called whether new breakpoints are created, or existing breakpoints
12435    deleted, to update the global location list and recompute which
12436    locations are duplicate of which.
12437
12438    The INSERT_MODE flag determines whether locations may not, may, or
12439    shall be inserted now.  See 'enum ugll_insert_mode' for more
12440    info.  */
12441
12442 static void
12443 update_global_location_list (enum ugll_insert_mode insert_mode)
12444 {
12445   struct breakpoint *b;
12446   struct bp_location **locp, *loc;
12447   struct cleanup *cleanups;
12448   /* Last breakpoint location address that was marked for update.  */
12449   CORE_ADDR last_addr = 0;
12450   /* Last breakpoint location program space that was marked for update.  */
12451   int last_pspace_num = -1;
12452
12453   /* Used in the duplicates detection below.  When iterating over all
12454      bp_locations, points to the first bp_location of a given address.
12455      Breakpoints and watchpoints of different types are never
12456      duplicates of each other.  Keep one pointer for each type of
12457      breakpoint/watchpoint, so we only need to loop over all locations
12458      once.  */
12459   struct bp_location *bp_loc_first;  /* breakpoint */
12460   struct bp_location *wp_loc_first;  /* hardware watchpoint */
12461   struct bp_location *awp_loc_first; /* access watchpoint */
12462   struct bp_location *rwp_loc_first; /* read watchpoint */
12463
12464   /* Saved former bp_location array which we compare against the newly
12465      built bp_location from the current state of ALL_BREAKPOINTS.  */
12466   struct bp_location **old_location, **old_locp;
12467   unsigned old_location_count;
12468
12469   old_location = bp_location;
12470   old_location_count = bp_location_count;
12471   bp_location = NULL;
12472   bp_location_count = 0;
12473   cleanups = make_cleanup (xfree, old_location);
12474
12475   ALL_BREAKPOINTS (b)
12476     for (loc = b->loc; loc; loc = loc->next)
12477       bp_location_count++;
12478
12479   bp_location = XNEWVEC (struct bp_location *, bp_location_count);
12480   locp = bp_location;
12481   ALL_BREAKPOINTS (b)
12482     for (loc = b->loc; loc; loc = loc->next)
12483       *locp++ = loc;
12484   qsort (bp_location, bp_location_count, sizeof (*bp_location),
12485          bp_location_compare);
12486
12487   bp_location_target_extensions_update ();
12488
12489   /* Identify bp_location instances that are no longer present in the
12490      new list, and therefore should be freed.  Note that it's not
12491      necessary that those locations should be removed from inferior --
12492      if there's another location at the same address (previously
12493      marked as duplicate), we don't need to remove/insert the
12494      location.
12495      
12496      LOCP is kept in sync with OLD_LOCP, each pointing to the current
12497      and former bp_location array state respectively.  */
12498
12499   locp = bp_location;
12500   for (old_locp = old_location; old_locp < old_location + old_location_count;
12501        old_locp++)
12502     {
12503       struct bp_location *old_loc = *old_locp;
12504       struct bp_location **loc2p;
12505
12506       /* Tells if 'old_loc' is found among the new locations.  If
12507          not, we have to free it.  */
12508       int found_object = 0;
12509       /* Tells if the location should remain inserted in the target.  */
12510       int keep_in_target = 0;
12511       int removed = 0;
12512
12513       /* Skip LOCP entries which will definitely never be needed.
12514          Stop either at or being the one matching OLD_LOC.  */
12515       while (locp < bp_location + bp_location_count
12516              && (*locp)->address < old_loc->address)
12517         locp++;
12518
12519       for (loc2p = locp;
12520            (loc2p < bp_location + bp_location_count
12521             && (*loc2p)->address == old_loc->address);
12522            loc2p++)
12523         {
12524           /* Check if this is a new/duplicated location or a duplicated
12525              location that had its condition modified.  If so, we want to send
12526              its condition to the target if evaluation of conditions is taking
12527              place there.  */
12528           if ((*loc2p)->condition_changed == condition_modified
12529               && (last_addr != old_loc->address
12530                   || last_pspace_num != old_loc->pspace->num))
12531             {
12532               force_breakpoint_reinsertion (*loc2p);
12533               last_pspace_num = old_loc->pspace->num;
12534             }
12535
12536           if (*loc2p == old_loc)
12537             found_object = 1;
12538         }
12539
12540       /* We have already handled this address, update it so that we don't
12541          have to go through updates again.  */
12542       last_addr = old_loc->address;
12543
12544       /* Target-side condition evaluation: Handle deleted locations.  */
12545       if (!found_object)
12546         force_breakpoint_reinsertion (old_loc);
12547
12548       /* If this location is no longer present, and inserted, look if
12549          there's maybe a new location at the same address.  If so,
12550          mark that one inserted, and don't remove this one.  This is
12551          needed so that we don't have a time window where a breakpoint
12552          at certain location is not inserted.  */
12553
12554       if (old_loc->inserted)
12555         {
12556           /* If the location is inserted now, we might have to remove
12557              it.  */
12558
12559           if (found_object && should_be_inserted (old_loc))
12560             {
12561               /* The location is still present in the location list,
12562                  and still should be inserted.  Don't do anything.  */
12563               keep_in_target = 1;
12564             }
12565           else
12566             {
12567               /* This location still exists, but it won't be kept in the
12568                  target since it may have been disabled.  We proceed to
12569                  remove its target-side condition.  */
12570
12571               /* The location is either no longer present, or got
12572                  disabled.  See if there's another location at the
12573                  same address, in which case we don't need to remove
12574                  this one from the target.  */
12575
12576               /* OLD_LOC comes from existing struct breakpoint.  */
12577               if (breakpoint_address_is_meaningful (old_loc->owner))
12578                 {
12579                   for (loc2p = locp;
12580                        (loc2p < bp_location + bp_location_count
12581                         && (*loc2p)->address == old_loc->address);
12582                        loc2p++)
12583                     {
12584                       struct bp_location *loc2 = *loc2p;
12585
12586                       if (breakpoint_locations_match (loc2, old_loc))
12587                         {
12588                           /* Read watchpoint locations are switched to
12589                              access watchpoints, if the former are not
12590                              supported, but the latter are.  */
12591                           if (is_hardware_watchpoint (old_loc->owner))
12592                             {
12593                               gdb_assert (is_hardware_watchpoint (loc2->owner));
12594                               loc2->watchpoint_type = old_loc->watchpoint_type;
12595                             }
12596
12597                           /* loc2 is a duplicated location. We need to check
12598                              if it should be inserted in case it will be
12599                              unduplicated.  */
12600                           if (loc2 != old_loc
12601                               && unduplicated_should_be_inserted (loc2))
12602                             {
12603                               swap_insertion (old_loc, loc2);
12604                               keep_in_target = 1;
12605                               break;
12606                             }
12607                         }
12608                     }
12609                 }
12610             }
12611
12612           if (!keep_in_target)
12613             {
12614               if (remove_breakpoint (old_loc))
12615                 {
12616                   /* This is just about all we can do.  We could keep
12617                      this location on the global list, and try to
12618                      remove it next time, but there's no particular
12619                      reason why we will succeed next time.
12620                      
12621                      Note that at this point, old_loc->owner is still
12622                      valid, as delete_breakpoint frees the breakpoint
12623                      only after calling us.  */
12624                   printf_filtered (_("warning: Error removing "
12625                                      "breakpoint %d\n"), 
12626                                    old_loc->owner->number);
12627                 }
12628               removed = 1;
12629             }
12630         }
12631
12632       if (!found_object)
12633         {
12634           if (removed && target_is_non_stop_p ()
12635               && need_moribund_for_location_type (old_loc))
12636             {
12637               /* This location was removed from the target.  In
12638                  non-stop mode, a race condition is possible where
12639                  we've removed a breakpoint, but stop events for that
12640                  breakpoint are already queued and will arrive later.
12641                  We apply an heuristic to be able to distinguish such
12642                  SIGTRAPs from other random SIGTRAPs: we keep this
12643                  breakpoint location for a bit, and will retire it
12644                  after we see some number of events.  The theory here
12645                  is that reporting of events should, "on the average",
12646                  be fair, so after a while we'll see events from all
12647                  threads that have anything of interest, and no longer
12648                  need to keep this breakpoint location around.  We
12649                  don't hold locations forever so to reduce chances of
12650                  mistaking a non-breakpoint SIGTRAP for a breakpoint
12651                  SIGTRAP.
12652
12653                  The heuristic failing can be disastrous on
12654                  decr_pc_after_break targets.
12655
12656                  On decr_pc_after_break targets, like e.g., x86-linux,
12657                  if we fail to recognize a late breakpoint SIGTRAP,
12658                  because events_till_retirement has reached 0 too
12659                  soon, we'll fail to do the PC adjustment, and report
12660                  a random SIGTRAP to the user.  When the user resumes
12661                  the inferior, it will most likely immediately crash
12662                  with SIGILL/SIGBUS/SIGSEGV, or worse, get silently
12663                  corrupted, because of being resumed e.g., in the
12664                  middle of a multi-byte instruction, or skipped a
12665                  one-byte instruction.  This was actually seen happen
12666                  on native x86-linux, and should be less rare on
12667                  targets that do not support new thread events, like
12668                  remote, due to the heuristic depending on
12669                  thread_count.
12670
12671                  Mistaking a random SIGTRAP for a breakpoint trap
12672                  causes similar symptoms (PC adjustment applied when
12673                  it shouldn't), but then again, playing with SIGTRAPs
12674                  behind the debugger's back is asking for trouble.
12675
12676                  Since hardware watchpoint traps are always
12677                  distinguishable from other traps, so we don't need to
12678                  apply keep hardware watchpoint moribund locations
12679                  around.  We simply always ignore hardware watchpoint
12680                  traps we can no longer explain.  */
12681
12682               old_loc->events_till_retirement = 3 * (thread_count () + 1);
12683               old_loc->owner = NULL;
12684
12685               VEC_safe_push (bp_location_p, moribund_locations, old_loc);
12686             }
12687           else
12688             {
12689               old_loc->owner = NULL;
12690               decref_bp_location (&old_loc);
12691             }
12692         }
12693     }
12694
12695   /* Rescan breakpoints at the same address and section, marking the
12696      first one as "first" and any others as "duplicates".  This is so
12697      that the bpt instruction is only inserted once.  If we have a
12698      permanent breakpoint at the same place as BPT, make that one the
12699      official one, and the rest as duplicates.  Permanent breakpoints
12700      are sorted first for the same address.
12701
12702      Do the same for hardware watchpoints, but also considering the
12703      watchpoint's type (regular/access/read) and length.  */
12704
12705   bp_loc_first = NULL;
12706   wp_loc_first = NULL;
12707   awp_loc_first = NULL;
12708   rwp_loc_first = NULL;
12709   ALL_BP_LOCATIONS (loc, locp)
12710     {
12711       /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always
12712          non-NULL.  */
12713       struct bp_location **loc_first_p;
12714       b = loc->owner;
12715
12716       if (!unduplicated_should_be_inserted (loc)
12717           || !breakpoint_address_is_meaningful (b)
12718           /* Don't detect duplicate for tracepoint locations because they are
12719            never duplicated.  See the comments in field `duplicate' of
12720            `struct bp_location'.  */
12721           || is_tracepoint (b))
12722         {
12723           /* Clear the condition modification flag.  */
12724           loc->condition_changed = condition_unchanged;
12725           continue;
12726         }
12727
12728       if (b->type == bp_hardware_watchpoint)
12729         loc_first_p = &wp_loc_first;
12730       else if (b->type == bp_read_watchpoint)
12731         loc_first_p = &rwp_loc_first;
12732       else if (b->type == bp_access_watchpoint)
12733         loc_first_p = &awp_loc_first;
12734       else
12735         loc_first_p = &bp_loc_first;
12736
12737       if (*loc_first_p == NULL
12738           || (overlay_debugging && loc->section != (*loc_first_p)->section)
12739           || !breakpoint_locations_match (loc, *loc_first_p))
12740         {
12741           *loc_first_p = loc;
12742           loc->duplicate = 0;
12743
12744           if (is_breakpoint (loc->owner) && loc->condition_changed)
12745             {
12746               loc->needs_update = 1;
12747               /* Clear the condition modification flag.  */
12748               loc->condition_changed = condition_unchanged;
12749             }
12750           continue;
12751         }
12752
12753
12754       /* This and the above ensure the invariant that the first location
12755          is not duplicated, and is the inserted one.
12756          All following are marked as duplicated, and are not inserted.  */
12757       if (loc->inserted)
12758         swap_insertion (loc, *loc_first_p);
12759       loc->duplicate = 1;
12760
12761       /* Clear the condition modification flag.  */
12762       loc->condition_changed = condition_unchanged;
12763     }
12764
12765   if (insert_mode == UGLL_INSERT || breakpoints_should_be_inserted_now ())
12766     {
12767       if (insert_mode != UGLL_DONT_INSERT)
12768         insert_breakpoint_locations ();
12769       else
12770         {
12771           /* Even though the caller told us to not insert new
12772              locations, we may still need to update conditions on the
12773              target's side of breakpoints that were already inserted
12774              if the target is evaluating breakpoint conditions.  We
12775              only update conditions for locations that are marked
12776              "needs_update".  */
12777           update_inserted_breakpoint_locations ();
12778         }
12779     }
12780
12781   if (insert_mode != UGLL_DONT_INSERT)
12782     download_tracepoint_locations ();
12783
12784   do_cleanups (cleanups);
12785 }
12786
12787 void
12788 breakpoint_retire_moribund (void)
12789 {
12790   struct bp_location *loc;
12791   int ix;
12792
12793   for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, loc); ++ix)
12794     if (--(loc->events_till_retirement) == 0)
12795       {
12796         decref_bp_location (&loc);
12797         VEC_unordered_remove (bp_location_p, moribund_locations, ix);
12798         --ix;
12799       }
12800 }
12801
12802 static void
12803 update_global_location_list_nothrow (enum ugll_insert_mode insert_mode)
12804 {
12805
12806   TRY
12807     {
12808       update_global_location_list (insert_mode);
12809     }
12810   CATCH (e, RETURN_MASK_ERROR)
12811     {
12812     }
12813   END_CATCH
12814 }
12815
12816 /* Clear BKP from a BPS.  */
12817
12818 static void
12819 bpstat_remove_bp_location (bpstat bps, struct breakpoint *bpt)
12820 {
12821   bpstat bs;
12822
12823   for (bs = bps; bs; bs = bs->next)
12824     if (bs->breakpoint_at == bpt)
12825       {
12826         bs->breakpoint_at = NULL;
12827         bs->old_val = NULL;
12828         /* bs->commands will be freed later.  */
12829       }
12830 }
12831
12832 /* Callback for iterate_over_threads.  */
12833 static int
12834 bpstat_remove_breakpoint_callback (struct thread_info *th, void *data)
12835 {
12836   struct breakpoint *bpt = (struct breakpoint *) data;
12837
12838   bpstat_remove_bp_location (th->control.stop_bpstat, bpt);
12839   return 0;
12840 }
12841
12842 /* Helper for breakpoint and tracepoint breakpoint_ops->mention
12843    callbacks.  */
12844
12845 static void
12846 say_where (struct breakpoint *b)
12847 {
12848   struct value_print_options opts;
12849
12850   get_user_print_options (&opts);
12851
12852   /* i18n: cagney/2005-02-11: Below needs to be merged into a
12853      single string.  */
12854   if (b->loc == NULL)
12855     {
12856       /* For pending locations, the output differs slightly based
12857          on b->extra_string.  If this is non-NULL, it contains either
12858          a condition or dprintf arguments.  */
12859       if (b->extra_string == NULL)
12860         {
12861           printf_filtered (_(" (%s) pending."),
12862                            event_location_to_string (b->location));
12863         }
12864       else if (b->type == bp_dprintf)
12865         {
12866           printf_filtered (_(" (%s,%s) pending."),
12867                            event_location_to_string (b->location),
12868                            b->extra_string);
12869         }
12870       else
12871         {
12872           printf_filtered (_(" (%s %s) pending."),
12873                            event_location_to_string (b->location),
12874                            b->extra_string);
12875         }
12876     }
12877   else
12878     {
12879       if (opts.addressprint || b->loc->symtab == NULL)
12880         {
12881           printf_filtered (" at ");
12882           fputs_filtered (paddress (b->loc->gdbarch, b->loc->address),
12883                           gdb_stdout);
12884         }
12885       if (b->loc->symtab != NULL)
12886         {
12887           /* If there is a single location, we can print the location
12888              more nicely.  */
12889           if (b->loc->next == NULL)
12890             printf_filtered (": file %s, line %d.",
12891                              symtab_to_filename_for_display (b->loc->symtab),
12892                              b->loc->line_number);
12893           else
12894             /* This is not ideal, but each location may have a
12895                different file name, and this at least reflects the
12896                real situation somewhat.  */
12897             printf_filtered (": %s.",
12898                              event_location_to_string (b->location));
12899         }
12900
12901       if (b->loc->next)
12902         {
12903           struct bp_location *loc = b->loc;
12904           int n = 0;
12905           for (; loc; loc = loc->next)
12906             ++n;
12907           printf_filtered (" (%d locations)", n);
12908         }
12909     }
12910 }
12911
12912 /* Default bp_location_ops methods.  */
12913
12914 static void
12915 bp_location_dtor (struct bp_location *self)
12916 {
12917   xfree (self->cond);
12918   if (self->cond_bytecode)
12919     free_agent_expr (self->cond_bytecode);
12920   xfree (self->function_name);
12921
12922   VEC_free (agent_expr_p, self->target_info.conditions);
12923   VEC_free (agent_expr_p, self->target_info.tcommands);
12924 }
12925
12926 static const struct bp_location_ops bp_location_ops =
12927 {
12928   bp_location_dtor
12929 };
12930
12931 /* Default breakpoint_ops methods all breakpoint_ops ultimately
12932    inherit from.  */
12933
12934 static void
12935 base_breakpoint_dtor (struct breakpoint *self)
12936 {
12937   decref_counted_command_line (&self->commands);
12938   xfree (self->cond_string);
12939   xfree (self->extra_string);
12940   xfree (self->filter);
12941   delete_event_location (self->location);
12942   delete_event_location (self->location_range_end);
12943 }
12944
12945 static struct bp_location *
12946 base_breakpoint_allocate_location (struct breakpoint *self)
12947 {
12948   struct bp_location *loc;
12949
12950   loc = XNEW (struct bp_location);
12951   init_bp_location (loc, &bp_location_ops, self);
12952   return loc;
12953 }
12954
12955 static void
12956 base_breakpoint_re_set (struct breakpoint *b)
12957 {
12958   /* Nothing to re-set. */
12959 }
12960
12961 #define internal_error_pure_virtual_called() \
12962   gdb_assert_not_reached ("pure virtual function called")
12963
12964 static int
12965 base_breakpoint_insert_location (struct bp_location *bl)
12966 {
12967   internal_error_pure_virtual_called ();
12968 }
12969
12970 static int
12971 base_breakpoint_remove_location (struct bp_location *bl,
12972                                  enum remove_bp_reason reason)
12973 {
12974   internal_error_pure_virtual_called ();
12975 }
12976
12977 static int
12978 base_breakpoint_breakpoint_hit (const struct bp_location *bl,
12979                                 struct address_space *aspace,
12980                                 CORE_ADDR bp_addr,
12981                                 const struct target_waitstatus *ws)
12982 {
12983   internal_error_pure_virtual_called ();
12984 }
12985
12986 static void
12987 base_breakpoint_check_status (bpstat bs)
12988 {
12989   /* Always stop.   */
12990 }
12991
12992 /* A "works_in_software_mode" breakpoint_ops method that just internal
12993    errors.  */
12994
12995 static int
12996 base_breakpoint_works_in_software_mode (const struct breakpoint *b)
12997 {
12998   internal_error_pure_virtual_called ();
12999 }
13000
13001 /* A "resources_needed" breakpoint_ops method that just internal
13002    errors.  */
13003
13004 static int
13005 base_breakpoint_resources_needed (const struct bp_location *bl)
13006 {
13007   internal_error_pure_virtual_called ();
13008 }
13009
13010 static enum print_stop_action
13011 base_breakpoint_print_it (bpstat bs)
13012 {
13013   internal_error_pure_virtual_called ();
13014 }
13015
13016 static void
13017 base_breakpoint_print_one_detail (const struct breakpoint *self,
13018                                   struct ui_out *uiout)
13019 {
13020   /* nothing */
13021 }
13022
13023 static void
13024 base_breakpoint_print_mention (struct breakpoint *b)
13025 {
13026   internal_error_pure_virtual_called ();
13027 }
13028
13029 static void
13030 base_breakpoint_print_recreate (struct breakpoint *b, struct ui_file *fp)
13031 {
13032   internal_error_pure_virtual_called ();
13033 }
13034
13035 static void
13036 base_breakpoint_create_sals_from_location
13037   (const struct event_location *location,
13038    struct linespec_result *canonical,
13039    enum bptype type_wanted)
13040 {
13041   internal_error_pure_virtual_called ();
13042 }
13043
13044 static void
13045 base_breakpoint_create_breakpoints_sal (struct gdbarch *gdbarch,
13046                                         struct linespec_result *c,
13047                                         char *cond_string,
13048                                         char *extra_string,
13049                                         enum bptype type_wanted,
13050                                         enum bpdisp disposition,
13051                                         int thread,
13052                                         int task, int ignore_count,
13053                                         const struct breakpoint_ops *o,
13054                                         int from_tty, int enabled,
13055                                         int internal, unsigned flags)
13056 {
13057   internal_error_pure_virtual_called ();
13058 }
13059
13060 static void
13061 base_breakpoint_decode_location (struct breakpoint *b,
13062                                  const struct event_location *location,
13063                                  struct program_space *search_pspace,
13064                                  struct symtabs_and_lines *sals)
13065 {
13066   internal_error_pure_virtual_called ();
13067 }
13068
13069 /* The default 'explains_signal' method.  */
13070
13071 static int
13072 base_breakpoint_explains_signal (struct breakpoint *b, enum gdb_signal sig)
13073 {
13074   return 1;
13075 }
13076
13077 /* The default "after_condition_true" method.  */
13078
13079 static void
13080 base_breakpoint_after_condition_true (struct bpstats *bs)
13081 {
13082   /* Nothing to do.   */
13083 }
13084
13085 struct breakpoint_ops base_breakpoint_ops =
13086 {
13087   base_breakpoint_dtor,
13088   base_breakpoint_allocate_location,
13089   base_breakpoint_re_set,
13090   base_breakpoint_insert_location,
13091   base_breakpoint_remove_location,
13092   base_breakpoint_breakpoint_hit,
13093   base_breakpoint_check_status,
13094   base_breakpoint_resources_needed,
13095   base_breakpoint_works_in_software_mode,
13096   base_breakpoint_print_it,
13097   NULL,
13098   base_breakpoint_print_one_detail,
13099   base_breakpoint_print_mention,
13100   base_breakpoint_print_recreate,
13101   base_breakpoint_create_sals_from_location,
13102   base_breakpoint_create_breakpoints_sal,
13103   base_breakpoint_decode_location,
13104   base_breakpoint_explains_signal,
13105   base_breakpoint_after_condition_true,
13106 };
13107
13108 /* Default breakpoint_ops methods.  */
13109
13110 static void
13111 bkpt_re_set (struct breakpoint *b)
13112 {
13113   /* FIXME: is this still reachable?  */
13114   if (breakpoint_event_location_empty_p (b))
13115     {
13116       /* Anything without a location can't be re-set.  */
13117       delete_breakpoint (b);
13118       return;
13119     }
13120
13121   breakpoint_re_set_default (b);
13122 }
13123
13124 static int
13125 bkpt_insert_location (struct bp_location *bl)
13126 {
13127   CORE_ADDR addr = bl->target_info.reqstd_address;
13128
13129   bl->target_info.kind = breakpoint_kind (bl, &addr);
13130   bl->target_info.placed_address = addr;
13131
13132   if (bl->loc_type == bp_loc_hardware_breakpoint)
13133     return target_insert_hw_breakpoint (bl->gdbarch, &bl->target_info);
13134   else
13135     return target_insert_breakpoint (bl->gdbarch, &bl->target_info);
13136 }
13137
13138 static int
13139 bkpt_remove_location (struct bp_location *bl, enum remove_bp_reason reason)
13140 {
13141   if (bl->loc_type == bp_loc_hardware_breakpoint)
13142     return target_remove_hw_breakpoint (bl->gdbarch, &bl->target_info);
13143   else
13144     return target_remove_breakpoint (bl->gdbarch, &bl->target_info, reason);
13145 }
13146
13147 static int
13148 bkpt_breakpoint_hit (const struct bp_location *bl,
13149                      struct address_space *aspace, CORE_ADDR bp_addr,
13150                      const struct target_waitstatus *ws)
13151 {
13152   if (ws->kind != TARGET_WAITKIND_STOPPED
13153       || ws->value.sig != GDB_SIGNAL_TRAP)
13154     return 0;
13155
13156   if (!breakpoint_address_match (bl->pspace->aspace, bl->address,
13157                                  aspace, bp_addr))
13158     return 0;
13159
13160   if (overlay_debugging         /* unmapped overlay section */
13161       && section_is_overlay (bl->section)
13162       && !section_is_mapped (bl->section))
13163     return 0;
13164
13165   return 1;
13166 }
13167
13168 static int
13169 dprintf_breakpoint_hit (const struct bp_location *bl,
13170                         struct address_space *aspace, CORE_ADDR bp_addr,
13171                         const struct target_waitstatus *ws)
13172 {
13173   if (dprintf_style == dprintf_style_agent
13174       && target_can_run_breakpoint_commands ())
13175     {
13176       /* An agent-style dprintf never causes a stop.  If we see a trap
13177          for this address it must be for a breakpoint that happens to
13178          be set at the same address.  */
13179       return 0;
13180     }
13181
13182   return bkpt_breakpoint_hit (bl, aspace, bp_addr, ws);
13183 }
13184
13185 static int
13186 bkpt_resources_needed (const struct bp_location *bl)
13187 {
13188   gdb_assert (bl->owner->type == bp_hardware_breakpoint);
13189
13190   return 1;
13191 }
13192
13193 static enum print_stop_action
13194 bkpt_print_it (bpstat bs)
13195 {
13196   struct breakpoint *b;
13197   const struct bp_location *bl;
13198   int bp_temp;
13199   struct ui_out *uiout = current_uiout;
13200
13201   gdb_assert (bs->bp_location_at != NULL);
13202
13203   bl = bs->bp_location_at;
13204   b = bs->breakpoint_at;
13205
13206   bp_temp = b->disposition == disp_del;
13207   if (bl->address != bl->requested_address)
13208     breakpoint_adjustment_warning (bl->requested_address,
13209                                    bl->address,
13210                                    b->number, 1);
13211   annotate_breakpoint (b->number);
13212   maybe_print_thread_hit_breakpoint (uiout);
13213
13214   if (bp_temp)
13215     ui_out_text (uiout, "Temporary breakpoint ");
13216   else
13217     ui_out_text (uiout, "Breakpoint ");
13218   if (ui_out_is_mi_like_p (uiout))
13219     {
13220       ui_out_field_string (uiout, "reason",
13221                            async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT));
13222       ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
13223     }
13224   ui_out_field_int (uiout, "bkptno", b->number);
13225   ui_out_text (uiout, ", ");
13226
13227   return PRINT_SRC_AND_LOC;
13228 }
13229
13230 static void
13231 bkpt_print_mention (struct breakpoint *b)
13232 {
13233   if (ui_out_is_mi_like_p (current_uiout))
13234     return;
13235
13236   switch (b->type)
13237     {
13238     case bp_breakpoint:
13239     case bp_gnu_ifunc_resolver:
13240       if (b->disposition == disp_del)
13241         printf_filtered (_("Temporary breakpoint"));
13242       else
13243         printf_filtered (_("Breakpoint"));
13244       printf_filtered (_(" %d"), b->number);
13245       if (b->type == bp_gnu_ifunc_resolver)
13246         printf_filtered (_(" at gnu-indirect-function resolver"));
13247       break;
13248     case bp_hardware_breakpoint:
13249       printf_filtered (_("Hardware assisted breakpoint %d"), b->number);
13250       break;
13251     case bp_dprintf:
13252       printf_filtered (_("Dprintf %d"), b->number);
13253       break;
13254     }
13255
13256   say_where (b);
13257 }
13258
13259 static void
13260 bkpt_print_recreate (struct breakpoint *tp, struct ui_file *fp)
13261 {
13262   if (tp->type == bp_breakpoint && tp->disposition == disp_del)
13263     fprintf_unfiltered (fp, "tbreak");
13264   else if (tp->type == bp_breakpoint)
13265     fprintf_unfiltered (fp, "break");
13266   else if (tp->type == bp_hardware_breakpoint
13267            && tp->disposition == disp_del)
13268     fprintf_unfiltered (fp, "thbreak");
13269   else if (tp->type == bp_hardware_breakpoint)
13270     fprintf_unfiltered (fp, "hbreak");
13271   else
13272     internal_error (__FILE__, __LINE__,
13273                     _("unhandled breakpoint type %d"), (int) tp->type);
13274
13275   fprintf_unfiltered (fp, " %s",
13276                       event_location_to_string (tp->location));
13277
13278   /* Print out extra_string if this breakpoint is pending.  It might
13279      contain, for example, conditions that were set by the user.  */
13280   if (tp->loc == NULL && tp->extra_string != NULL)
13281     fprintf_unfiltered (fp, " %s", tp->extra_string);
13282
13283   print_recreate_thread (tp, fp);
13284 }
13285
13286 static void
13287 bkpt_create_sals_from_location (const struct event_location *location,
13288                                 struct linespec_result *canonical,
13289                                 enum bptype type_wanted)
13290 {
13291   create_sals_from_location_default (location, canonical, type_wanted);
13292 }
13293
13294 static void
13295 bkpt_create_breakpoints_sal (struct gdbarch *gdbarch,
13296                              struct linespec_result *canonical,
13297                              char *cond_string,
13298                              char *extra_string,
13299                              enum bptype type_wanted,
13300                              enum bpdisp disposition,
13301                              int thread,
13302                              int task, int ignore_count,
13303                              const struct breakpoint_ops *ops,
13304                              int from_tty, int enabled,
13305                              int internal, unsigned flags)
13306 {
13307   create_breakpoints_sal_default (gdbarch, canonical,
13308                                   cond_string, extra_string,
13309                                   type_wanted,
13310                                   disposition, thread, task,
13311                                   ignore_count, ops, from_tty,
13312                                   enabled, internal, flags);
13313 }
13314
13315 static void
13316 bkpt_decode_location (struct breakpoint *b,
13317                       const struct event_location *location,
13318                       struct program_space *search_pspace,
13319                       struct symtabs_and_lines *sals)
13320 {
13321   decode_location_default (b, location, search_pspace, sals);
13322 }
13323
13324 /* Virtual table for internal breakpoints.  */
13325
13326 static void
13327 internal_bkpt_re_set (struct breakpoint *b)
13328 {
13329   switch (b->type)
13330     {
13331       /* Delete overlay event and longjmp master breakpoints; they
13332          will be reset later by breakpoint_re_set.  */
13333     case bp_overlay_event:
13334     case bp_longjmp_master:
13335     case bp_std_terminate_master:
13336     case bp_exception_master:
13337       delete_breakpoint (b);
13338       break;
13339
13340       /* This breakpoint is special, it's set up when the inferior
13341          starts and we really don't want to touch it.  */
13342     case bp_shlib_event:
13343
13344       /* Like bp_shlib_event, this breakpoint type is special.  Once
13345          it is set up, we do not want to touch it.  */
13346     case bp_thread_event:
13347       break;
13348     }
13349 }
13350
13351 static void
13352 internal_bkpt_check_status (bpstat bs)
13353 {
13354   if (bs->breakpoint_at->type == bp_shlib_event)
13355     {
13356       /* If requested, stop when the dynamic linker notifies GDB of
13357          events.  This allows the user to get control and place
13358          breakpoints in initializer routines for dynamically loaded
13359          objects (among other things).  */
13360       bs->stop = stop_on_solib_events;
13361       bs->print = stop_on_solib_events;
13362     }
13363   else
13364     bs->stop = 0;
13365 }
13366
13367 static enum print_stop_action
13368 internal_bkpt_print_it (bpstat bs)
13369 {
13370   struct breakpoint *b;
13371
13372   b = bs->breakpoint_at;
13373
13374   switch (b->type)
13375     {
13376     case bp_shlib_event:
13377       /* Did we stop because the user set the stop_on_solib_events
13378          variable?  (If so, we report this as a generic, "Stopped due
13379          to shlib event" message.) */
13380       print_solib_event (0);
13381       break;
13382
13383     case bp_thread_event:
13384       /* Not sure how we will get here.
13385          GDB should not stop for these breakpoints.  */
13386       printf_filtered (_("Thread Event Breakpoint: gdb should not stop!\n"));
13387       break;
13388
13389     case bp_overlay_event:
13390       /* By analogy with the thread event, GDB should not stop for these.  */
13391       printf_filtered (_("Overlay Event Breakpoint: gdb should not stop!\n"));
13392       break;
13393
13394     case bp_longjmp_master:
13395       /* These should never be enabled.  */
13396       printf_filtered (_("Longjmp Master Breakpoint: gdb should not stop!\n"));
13397       break;
13398
13399     case bp_std_terminate_master:
13400       /* These should never be enabled.  */
13401       printf_filtered (_("std::terminate Master Breakpoint: "
13402                          "gdb should not stop!\n"));
13403       break;
13404
13405     case bp_exception_master:
13406       /* These should never be enabled.  */
13407       printf_filtered (_("Exception Master Breakpoint: "
13408                          "gdb should not stop!\n"));
13409       break;
13410     }
13411
13412   return PRINT_NOTHING;
13413 }
13414
13415 static void
13416 internal_bkpt_print_mention (struct breakpoint *b)
13417 {
13418   /* Nothing to mention.  These breakpoints are internal.  */
13419 }
13420
13421 /* Virtual table for momentary breakpoints  */
13422
13423 static void
13424 momentary_bkpt_re_set (struct breakpoint *b)
13425 {
13426   /* Keep temporary breakpoints, which can be encountered when we step
13427      over a dlopen call and solib_add is resetting the breakpoints.
13428      Otherwise these should have been blown away via the cleanup chain
13429      or by breakpoint_init_inferior when we rerun the executable.  */
13430 }
13431
13432 static void
13433 momentary_bkpt_check_status (bpstat bs)
13434 {
13435   /* Nothing.  The point of these breakpoints is causing a stop.  */
13436 }
13437
13438 static enum print_stop_action
13439 momentary_bkpt_print_it (bpstat bs)
13440 {
13441   return PRINT_UNKNOWN;
13442 }
13443
13444 static void
13445 momentary_bkpt_print_mention (struct breakpoint *b)
13446 {
13447   /* Nothing to mention.  These breakpoints are internal.  */
13448 }
13449
13450 /* Ensure INITIATING_FRAME is cleared when no such breakpoint exists.
13451
13452    It gets cleared already on the removal of the first one of such placed
13453    breakpoints.  This is OK as they get all removed altogether.  */
13454
13455 static void
13456 longjmp_bkpt_dtor (struct breakpoint *self)
13457 {
13458   struct thread_info *tp = find_thread_global_id (self->thread);
13459
13460   if (tp)
13461     tp->initiating_frame = null_frame_id;
13462
13463   momentary_breakpoint_ops.dtor (self);
13464 }
13465
13466 /* Specific methods for probe breakpoints.  */
13467
13468 static int
13469 bkpt_probe_insert_location (struct bp_location *bl)
13470 {
13471   int v = bkpt_insert_location (bl);
13472
13473   if (v == 0)
13474     {
13475       /* The insertion was successful, now let's set the probe's semaphore
13476          if needed.  */
13477       if (bl->probe.probe->pops->set_semaphore != NULL)
13478         bl->probe.probe->pops->set_semaphore (bl->probe.probe,
13479                                               bl->probe.objfile,
13480                                               bl->gdbarch);
13481     }
13482
13483   return v;
13484 }
13485
13486 static int
13487 bkpt_probe_remove_location (struct bp_location *bl,
13488                             enum remove_bp_reason reason)
13489 {
13490   /* Let's clear the semaphore before removing the location.  */
13491   if (bl->probe.probe->pops->clear_semaphore != NULL)
13492     bl->probe.probe->pops->clear_semaphore (bl->probe.probe,
13493                                             bl->probe.objfile,
13494                                             bl->gdbarch);
13495
13496   return bkpt_remove_location (bl, reason);
13497 }
13498
13499 static void
13500 bkpt_probe_create_sals_from_location (const struct event_location *location,
13501                                       struct linespec_result *canonical,
13502                                       enum bptype type_wanted)
13503 {
13504   struct linespec_sals lsal;
13505
13506   lsal.sals = parse_probes (location, NULL, canonical);
13507   lsal.canonical = xstrdup (event_location_to_string (canonical->location));
13508   VEC_safe_push (linespec_sals, canonical->sals, &lsal);
13509 }
13510
13511 static void
13512 bkpt_probe_decode_location (struct breakpoint *b,
13513                             const struct event_location *location,
13514                             struct program_space *search_pspace,
13515                             struct symtabs_and_lines *sals)
13516 {
13517   *sals = parse_probes (location, search_pspace, NULL);
13518   if (!sals->sals)
13519     error (_("probe not found"));
13520 }
13521
13522 /* The breakpoint_ops structure to be used in tracepoints.  */
13523
13524 static void
13525 tracepoint_re_set (struct breakpoint *b)
13526 {
13527   breakpoint_re_set_default (b);
13528 }
13529
13530 static int
13531 tracepoint_breakpoint_hit (const struct bp_location *bl,
13532                            struct address_space *aspace, CORE_ADDR bp_addr,
13533                            const struct target_waitstatus *ws)
13534 {
13535   /* By definition, the inferior does not report stops at
13536      tracepoints.  */
13537   return 0;
13538 }
13539
13540 static void
13541 tracepoint_print_one_detail (const struct breakpoint *self,
13542                              struct ui_out *uiout)
13543 {
13544   struct tracepoint *tp = (struct tracepoint *) self;
13545   if (tp->static_trace_marker_id)
13546     {
13547       gdb_assert (self->type == bp_static_tracepoint);
13548
13549       ui_out_text (uiout, "\tmarker id is ");
13550       ui_out_field_string (uiout, "static-tracepoint-marker-string-id",
13551                            tp->static_trace_marker_id);
13552       ui_out_text (uiout, "\n");
13553     }
13554 }
13555
13556 static void
13557 tracepoint_print_mention (struct breakpoint *b)
13558 {
13559   if (ui_out_is_mi_like_p (current_uiout))
13560     return;
13561
13562   switch (b->type)
13563     {
13564     case bp_tracepoint:
13565       printf_filtered (_("Tracepoint"));
13566       printf_filtered (_(" %d"), b->number);
13567       break;
13568     case bp_fast_tracepoint:
13569       printf_filtered (_("Fast tracepoint"));
13570       printf_filtered (_(" %d"), b->number);
13571       break;
13572     case bp_static_tracepoint:
13573       printf_filtered (_("Static tracepoint"));
13574       printf_filtered (_(" %d"), b->number);
13575       break;
13576     default:
13577       internal_error (__FILE__, __LINE__,
13578                       _("unhandled tracepoint type %d"), (int) b->type);
13579     }
13580
13581   say_where (b);
13582 }
13583
13584 static void
13585 tracepoint_print_recreate (struct breakpoint *self, struct ui_file *fp)
13586 {
13587   struct tracepoint *tp = (struct tracepoint *) self;
13588
13589   if (self->type == bp_fast_tracepoint)
13590     fprintf_unfiltered (fp, "ftrace");
13591   else if (self->type == bp_static_tracepoint)
13592     fprintf_unfiltered (fp, "strace");
13593   else if (self->type == bp_tracepoint)
13594     fprintf_unfiltered (fp, "trace");
13595   else
13596     internal_error (__FILE__, __LINE__,
13597                     _("unhandled tracepoint type %d"), (int) self->type);
13598
13599   fprintf_unfiltered (fp, " %s",
13600                       event_location_to_string (self->location));
13601   print_recreate_thread (self, fp);
13602
13603   if (tp->pass_count)
13604     fprintf_unfiltered (fp, "  passcount %d\n", tp->pass_count);
13605 }
13606
13607 static void
13608 tracepoint_create_sals_from_location (const struct event_location *location,
13609                                       struct linespec_result *canonical,
13610                                       enum bptype type_wanted)
13611 {
13612   create_sals_from_location_default (location, canonical, type_wanted);
13613 }
13614
13615 static void
13616 tracepoint_create_breakpoints_sal (struct gdbarch *gdbarch,
13617                                    struct linespec_result *canonical,
13618                                    char *cond_string,
13619                                    char *extra_string,
13620                                    enum bptype type_wanted,
13621                                    enum bpdisp disposition,
13622                                    int thread,
13623                                    int task, int ignore_count,
13624                                    const struct breakpoint_ops *ops,
13625                                    int from_tty, int enabled,
13626                                    int internal, unsigned flags)
13627 {
13628   create_breakpoints_sal_default (gdbarch, canonical,
13629                                   cond_string, extra_string,
13630                                   type_wanted,
13631                                   disposition, thread, task,
13632                                   ignore_count, ops, from_tty,
13633                                   enabled, internal, flags);
13634 }
13635
13636 static void
13637 tracepoint_decode_location (struct breakpoint *b,
13638                             const struct event_location *location,
13639                             struct program_space *search_pspace,
13640                             struct symtabs_and_lines *sals)
13641 {
13642   decode_location_default (b, location, search_pspace, sals);
13643 }
13644
13645 struct breakpoint_ops tracepoint_breakpoint_ops;
13646
13647 /* The breakpoint_ops structure to be use on tracepoints placed in a
13648    static probe.  */
13649
13650 static void
13651 tracepoint_probe_create_sals_from_location
13652   (const struct event_location *location,
13653    struct linespec_result *canonical,
13654    enum bptype type_wanted)
13655 {
13656   /* We use the same method for breakpoint on probes.  */
13657   bkpt_probe_create_sals_from_location (location, canonical, type_wanted);
13658 }
13659
13660 static void
13661 tracepoint_probe_decode_location (struct breakpoint *b,
13662                                   const struct event_location *location,
13663                                   struct program_space *search_pspace,
13664                                   struct symtabs_and_lines *sals)
13665 {
13666   /* We use the same method for breakpoint on probes.  */
13667   bkpt_probe_decode_location (b, location, search_pspace, sals);
13668 }
13669
13670 static struct breakpoint_ops tracepoint_probe_breakpoint_ops;
13671
13672 /* Dprintf breakpoint_ops methods.  */
13673
13674 static void
13675 dprintf_re_set (struct breakpoint *b)
13676 {
13677   breakpoint_re_set_default (b);
13678
13679   /* extra_string should never be non-NULL for dprintf.  */
13680   gdb_assert (b->extra_string != NULL);
13681
13682   /* 1 - connect to target 1, that can run breakpoint commands.
13683      2 - create a dprintf, which resolves fine.
13684      3 - disconnect from target 1
13685      4 - connect to target 2, that can NOT run breakpoint commands.
13686
13687      After steps #3/#4, you'll want the dprintf command list to
13688      be updated, because target 1 and 2 may well return different
13689      answers for target_can_run_breakpoint_commands().
13690      Given absence of finer grained resetting, we get to do
13691      it all the time.  */
13692   if (b->extra_string != NULL)
13693     update_dprintf_command_list (b);
13694 }
13695
13696 /* Implement the "print_recreate" breakpoint_ops method for dprintf.  */
13697
13698 static void
13699 dprintf_print_recreate (struct breakpoint *tp, struct ui_file *fp)
13700 {
13701   fprintf_unfiltered (fp, "dprintf %s,%s",
13702                       event_location_to_string (tp->location),
13703                       tp->extra_string);
13704   print_recreate_thread (tp, fp);
13705 }
13706
13707 /* Implement the "after_condition_true" breakpoint_ops method for
13708    dprintf.
13709
13710    dprintf's are implemented with regular commands in their command
13711    list, but we run the commands here instead of before presenting the
13712    stop to the user, as dprintf's don't actually cause a stop.  This
13713    also makes it so that the commands of multiple dprintfs at the same
13714    address are all handled.  */
13715
13716 static void
13717 dprintf_after_condition_true (struct bpstats *bs)
13718 {
13719   struct cleanup *old_chain;
13720   struct bpstats tmp_bs = { NULL };
13721   struct bpstats *tmp_bs_p = &tmp_bs;
13722
13723   /* dprintf's never cause a stop.  This wasn't set in the
13724      check_status hook instead because that would make the dprintf's
13725      condition not be evaluated.  */
13726   bs->stop = 0;
13727
13728   /* Run the command list here.  Take ownership of it instead of
13729      copying.  We never want these commands to run later in
13730      bpstat_do_actions, if a breakpoint that causes a stop happens to
13731      be set at same address as this dprintf, or even if running the
13732      commands here throws.  */
13733   tmp_bs.commands = bs->commands;
13734   bs->commands = NULL;
13735   old_chain = make_cleanup_decref_counted_command_line (&tmp_bs.commands);
13736
13737   bpstat_do_actions_1 (&tmp_bs_p);
13738
13739   /* 'tmp_bs.commands' will usually be NULL by now, but
13740      bpstat_do_actions_1 may return early without processing the whole
13741      list.  */
13742   do_cleanups (old_chain);
13743 }
13744
13745 /* The breakpoint_ops structure to be used on static tracepoints with
13746    markers (`-m').  */
13747
13748 static void
13749 strace_marker_create_sals_from_location (const struct event_location *location,
13750                                          struct linespec_result *canonical,
13751                                          enum bptype type_wanted)
13752 {
13753   struct linespec_sals lsal;
13754   const char *arg_start, *arg;
13755   char *str;
13756   struct cleanup *cleanup;
13757
13758   arg = arg_start = get_linespec_location (location);
13759   lsal.sals = decode_static_tracepoint_spec (&arg);
13760
13761   str = savestring (arg_start, arg - arg_start);
13762   cleanup = make_cleanup (xfree, str);
13763   canonical->location = new_linespec_location (&str);
13764   do_cleanups (cleanup);
13765
13766   lsal.canonical = xstrdup (event_location_to_string (canonical->location));
13767   VEC_safe_push (linespec_sals, canonical->sals, &lsal);
13768 }
13769
13770 static void
13771 strace_marker_create_breakpoints_sal (struct gdbarch *gdbarch,
13772                                       struct linespec_result *canonical,
13773                                       char *cond_string,
13774                                       char *extra_string,
13775                                       enum bptype type_wanted,
13776                                       enum bpdisp disposition,
13777                                       int thread,
13778                                       int task, int ignore_count,
13779                                       const struct breakpoint_ops *ops,
13780                                       int from_tty, int enabled,
13781                                       int internal, unsigned flags)
13782 {
13783   int i;
13784   struct linespec_sals *lsal = VEC_index (linespec_sals,
13785                                           canonical->sals, 0);
13786
13787   /* If the user is creating a static tracepoint by marker id
13788      (strace -m MARKER_ID), then store the sals index, so that
13789      breakpoint_re_set can try to match up which of the newly
13790      found markers corresponds to this one, and, don't try to
13791      expand multiple locations for each sal, given than SALS
13792      already should contain all sals for MARKER_ID.  */
13793
13794   for (i = 0; i < lsal->sals.nelts; ++i)
13795     {
13796       struct symtabs_and_lines expanded;
13797       struct tracepoint *tp;
13798       struct cleanup *old_chain;
13799       struct event_location *location;
13800
13801       expanded.nelts = 1;
13802       expanded.sals = &lsal->sals.sals[i];
13803
13804       location = copy_event_location (canonical->location);
13805       old_chain = make_cleanup_delete_event_location (location);
13806
13807       tp = XCNEW (struct tracepoint);
13808       init_breakpoint_sal (&tp->base, gdbarch, expanded,
13809                            location, NULL,
13810                            cond_string, extra_string,
13811                            type_wanted, disposition,
13812                            thread, task, ignore_count, ops,
13813                            from_tty, enabled, internal, flags,
13814                            canonical->special_display);
13815       /* Given that its possible to have multiple markers with
13816          the same string id, if the user is creating a static
13817          tracepoint by marker id ("strace -m MARKER_ID"), then
13818          store the sals index, so that breakpoint_re_set can
13819          try to match up which of the newly found markers
13820          corresponds to this one  */
13821       tp->static_trace_marker_id_idx = i;
13822
13823       install_breakpoint (internal, &tp->base, 0);
13824
13825       discard_cleanups (old_chain);
13826     }
13827 }
13828
13829 static void
13830 strace_marker_decode_location (struct breakpoint *b,
13831                                const struct event_location *location,
13832                                struct program_space *search_pspace,
13833                                struct symtabs_and_lines *sals)
13834 {
13835   struct tracepoint *tp = (struct tracepoint *) b;
13836   const char *s = get_linespec_location (location);
13837
13838   *sals = decode_static_tracepoint_spec (&s);
13839   if (sals->nelts > tp->static_trace_marker_id_idx)
13840     {
13841       sals->sals[0] = sals->sals[tp->static_trace_marker_id_idx];
13842       sals->nelts = 1;
13843     }
13844   else
13845     error (_("marker %s not found"), tp->static_trace_marker_id);
13846 }
13847
13848 static struct breakpoint_ops strace_marker_breakpoint_ops;
13849
13850 static int
13851 strace_marker_p (struct breakpoint *b)
13852 {
13853   return b->ops == &strace_marker_breakpoint_ops;
13854 }
13855
13856 /* Delete a breakpoint and clean up all traces of it in the data
13857    structures.  */
13858
13859 void
13860 delete_breakpoint (struct breakpoint *bpt)
13861 {
13862   struct breakpoint *b;
13863
13864   gdb_assert (bpt != NULL);
13865
13866   /* Has this bp already been deleted?  This can happen because
13867      multiple lists can hold pointers to bp's.  bpstat lists are
13868      especial culprits.
13869
13870      One example of this happening is a watchpoint's scope bp.  When
13871      the scope bp triggers, we notice that the watchpoint is out of
13872      scope, and delete it.  We also delete its scope bp.  But the
13873      scope bp is marked "auto-deleting", and is already on a bpstat.
13874      That bpstat is then checked for auto-deleting bp's, which are
13875      deleted.
13876
13877      A real solution to this problem might involve reference counts in
13878      bp's, and/or giving them pointers back to their referencing
13879      bpstat's, and teaching delete_breakpoint to only free a bp's
13880      storage when no more references were extent.  A cheaper bandaid
13881      was chosen.  */
13882   if (bpt->type == bp_none)
13883     return;
13884
13885   /* At least avoid this stale reference until the reference counting
13886      of breakpoints gets resolved.  */
13887   if (bpt->related_breakpoint != bpt)
13888     {
13889       struct breakpoint *related;
13890       struct watchpoint *w;
13891
13892       if (bpt->type == bp_watchpoint_scope)
13893         w = (struct watchpoint *) bpt->related_breakpoint;
13894       else if (bpt->related_breakpoint->type == bp_watchpoint_scope)
13895         w = (struct watchpoint *) bpt;
13896       else
13897         w = NULL;
13898       if (w != NULL)
13899         watchpoint_del_at_next_stop (w);
13900
13901       /* Unlink bpt from the bpt->related_breakpoint ring.  */
13902       for (related = bpt; related->related_breakpoint != bpt;
13903            related = related->related_breakpoint);
13904       related->related_breakpoint = bpt->related_breakpoint;
13905       bpt->related_breakpoint = bpt;
13906     }
13907
13908   /* watch_command_1 creates a watchpoint but only sets its number if
13909      update_watchpoint succeeds in creating its bp_locations.  If there's
13910      a problem in that process, we'll be asked to delete the half-created
13911      watchpoint.  In that case, don't announce the deletion.  */
13912   if (bpt->number)
13913     observer_notify_breakpoint_deleted (bpt);
13914
13915   if (breakpoint_chain == bpt)
13916     breakpoint_chain = bpt->next;
13917
13918   ALL_BREAKPOINTS (b)
13919     if (b->next == bpt)
13920     {
13921       b->next = bpt->next;
13922       break;
13923     }
13924
13925   /* Be sure no bpstat's are pointing at the breakpoint after it's
13926      been freed.  */
13927   /* FIXME, how can we find all bpstat's?  We just check stop_bpstat
13928      in all threads for now.  Note that we cannot just remove bpstats
13929      pointing at bpt from the stop_bpstat list entirely, as breakpoint
13930      commands are associated with the bpstat; if we remove it here,
13931      then the later call to bpstat_do_actions (&stop_bpstat); in
13932      event-top.c won't do anything, and temporary breakpoints with
13933      commands won't work.  */
13934
13935   iterate_over_threads (bpstat_remove_breakpoint_callback, bpt);
13936
13937   /* Now that breakpoint is removed from breakpoint list, update the
13938      global location list.  This will remove locations that used to
13939      belong to this breakpoint.  Do this before freeing the breakpoint
13940      itself, since remove_breakpoint looks at location's owner.  It
13941      might be better design to have location completely
13942      self-contained, but it's not the case now.  */
13943   update_global_location_list (UGLL_DONT_INSERT);
13944
13945   bpt->ops->dtor (bpt);
13946   /* On the chance that someone will soon try again to delete this
13947      same bp, we mark it as deleted before freeing its storage.  */
13948   bpt->type = bp_none;
13949   xfree (bpt);
13950 }
13951
13952 static void
13953 do_delete_breakpoint_cleanup (void *b)
13954 {
13955   delete_breakpoint ((struct breakpoint *) b);
13956 }
13957
13958 struct cleanup *
13959 make_cleanup_delete_breakpoint (struct breakpoint *b)
13960 {
13961   return make_cleanup (do_delete_breakpoint_cleanup, b);
13962 }
13963
13964 /* Iterator function to call a user-provided callback function once
13965    for each of B and its related breakpoints.  */
13966
13967 static void
13968 iterate_over_related_breakpoints (struct breakpoint *b,
13969                                   void (*function) (struct breakpoint *,
13970                                                     void *),
13971                                   void *data)
13972 {
13973   struct breakpoint *related;
13974
13975   related = b;
13976   do
13977     {
13978       struct breakpoint *next;
13979
13980       /* FUNCTION may delete RELATED.  */
13981       next = related->related_breakpoint;
13982
13983       if (next == related)
13984         {
13985           /* RELATED is the last ring entry.  */
13986           function (related, data);
13987
13988           /* FUNCTION may have deleted it, so we'd never reach back to
13989              B.  There's nothing left to do anyway, so just break
13990              out.  */
13991           break;
13992         }
13993       else
13994         function (related, data);
13995
13996       related = next;
13997     }
13998   while (related != b);
13999 }
14000
14001 static void
14002 do_delete_breakpoint (struct breakpoint *b, void *ignore)
14003 {
14004   delete_breakpoint (b);
14005 }
14006
14007 /* A callback for map_breakpoint_numbers that calls
14008    delete_breakpoint.  */
14009
14010 static void
14011 do_map_delete_breakpoint (struct breakpoint *b, void *ignore)
14012 {
14013   iterate_over_related_breakpoints (b, do_delete_breakpoint, NULL);
14014 }
14015
14016 void
14017 delete_command (char *arg, int from_tty)
14018 {
14019   struct breakpoint *b, *b_tmp;
14020
14021   dont_repeat ();
14022
14023   if (arg == 0)
14024     {
14025       int breaks_to_delete = 0;
14026
14027       /* Delete all breakpoints if no argument.  Do not delete
14028          internal breakpoints, these have to be deleted with an
14029          explicit breakpoint number argument.  */
14030       ALL_BREAKPOINTS (b)
14031         if (user_breakpoint_p (b))
14032           {
14033             breaks_to_delete = 1;
14034             break;
14035           }
14036
14037       /* Ask user only if there are some breakpoints to delete.  */
14038       if (!from_tty
14039           || (breaks_to_delete && query (_("Delete all breakpoints? "))))
14040         {
14041           ALL_BREAKPOINTS_SAFE (b, b_tmp)
14042             if (user_breakpoint_p (b))
14043               delete_breakpoint (b);
14044         }
14045     }
14046   else
14047     map_breakpoint_numbers (arg, do_map_delete_breakpoint, NULL);
14048 }
14049
14050 /* Return true if all locations of B bound to PSPACE are pending.  If
14051    PSPACE is NULL, all locations of all program spaces are
14052    considered.  */
14053
14054 static int
14055 all_locations_are_pending (struct breakpoint *b, struct program_space *pspace)
14056 {
14057   struct bp_location *loc;
14058
14059   for (loc = b->loc; loc != NULL; loc = loc->next)
14060     if ((pspace == NULL
14061          || loc->pspace == pspace)
14062         && !loc->shlib_disabled
14063         && !loc->pspace->executing_startup)
14064       return 0;
14065   return 1;
14066 }
14067
14068 /* Subroutine of update_breakpoint_locations to simplify it.
14069    Return non-zero if multiple fns in list LOC have the same name.
14070    Null names are ignored.  */
14071
14072 static int
14073 ambiguous_names_p (struct bp_location *loc)
14074 {
14075   struct bp_location *l;
14076   htab_t htab = htab_create_alloc (13, htab_hash_string,
14077                                    (int (*) (const void *, 
14078                                              const void *)) streq,
14079                                    NULL, xcalloc, xfree);
14080
14081   for (l = loc; l != NULL; l = l->next)
14082     {
14083       const char **slot;
14084       const char *name = l->function_name;
14085
14086       /* Allow for some names to be NULL, ignore them.  */
14087       if (name == NULL)
14088         continue;
14089
14090       slot = (const char **) htab_find_slot (htab, (const void *) name,
14091                                              INSERT);
14092       /* NOTE: We can assume slot != NULL here because xcalloc never
14093          returns NULL.  */
14094       if (*slot != NULL)
14095         {
14096           htab_delete (htab);
14097           return 1;
14098         }
14099       *slot = name;
14100     }
14101
14102   htab_delete (htab);
14103   return 0;
14104 }
14105
14106 /* When symbols change, it probably means the sources changed as well,
14107    and it might mean the static tracepoint markers are no longer at
14108    the same address or line numbers they used to be at last we
14109    checked.  Losing your static tracepoints whenever you rebuild is
14110    undesirable.  This function tries to resync/rematch gdb static
14111    tracepoints with the markers on the target, for static tracepoints
14112    that have not been set by marker id.  Static tracepoint that have
14113    been set by marker id are reset by marker id in breakpoint_re_set.
14114    The heuristic is:
14115
14116    1) For a tracepoint set at a specific address, look for a marker at
14117    the old PC.  If one is found there, assume to be the same marker.
14118    If the name / string id of the marker found is different from the
14119    previous known name, assume that means the user renamed the marker
14120    in the sources, and output a warning.
14121
14122    2) For a tracepoint set at a given line number, look for a marker
14123    at the new address of the old line number.  If one is found there,
14124    assume to be the same marker.  If the name / string id of the
14125    marker found is different from the previous known name, assume that
14126    means the user renamed the marker in the sources, and output a
14127    warning.
14128
14129    3) If a marker is no longer found at the same address or line, it
14130    may mean the marker no longer exists.  But it may also just mean
14131    the code changed a bit.  Maybe the user added a few lines of code
14132    that made the marker move up or down (in line number terms).  Ask
14133    the target for info about the marker with the string id as we knew
14134    it.  If found, update line number and address in the matching
14135    static tracepoint.  This will get confused if there's more than one
14136    marker with the same ID (possible in UST, although unadvised
14137    precisely because it confuses tools).  */
14138
14139 static struct symtab_and_line
14140 update_static_tracepoint (struct breakpoint *b, struct symtab_and_line sal)
14141 {
14142   struct tracepoint *tp = (struct tracepoint *) b;
14143   struct static_tracepoint_marker marker;
14144   CORE_ADDR pc;
14145
14146   pc = sal.pc;
14147   if (sal.line)
14148     find_line_pc (sal.symtab, sal.line, &pc);
14149
14150   if (target_static_tracepoint_marker_at (pc, &marker))
14151     {
14152       if (strcmp (tp->static_trace_marker_id, marker.str_id) != 0)
14153         warning (_("static tracepoint %d changed probed marker from %s to %s"),
14154                  b->number,
14155                  tp->static_trace_marker_id, marker.str_id);
14156
14157       xfree (tp->static_trace_marker_id);
14158       tp->static_trace_marker_id = xstrdup (marker.str_id);
14159       release_static_tracepoint_marker (&marker);
14160
14161       return sal;
14162     }
14163
14164   /* Old marker wasn't found on target at lineno.  Try looking it up
14165      by string ID.  */
14166   if (!sal.explicit_pc
14167       && sal.line != 0
14168       && sal.symtab != NULL
14169       && tp->static_trace_marker_id != NULL)
14170     {
14171       VEC(static_tracepoint_marker_p) *markers;
14172
14173       markers
14174         = target_static_tracepoint_markers_by_strid (tp->static_trace_marker_id);
14175
14176       if (!VEC_empty(static_tracepoint_marker_p, markers))
14177         {
14178           struct symtab_and_line sal2;
14179           struct symbol *sym;
14180           struct static_tracepoint_marker *tpmarker;
14181           struct ui_out *uiout = current_uiout;
14182           struct explicit_location explicit_loc;
14183
14184           tpmarker = VEC_index (static_tracepoint_marker_p, markers, 0);
14185
14186           xfree (tp->static_trace_marker_id);
14187           tp->static_trace_marker_id = xstrdup (tpmarker->str_id);
14188
14189           warning (_("marker for static tracepoint %d (%s) not "
14190                      "found at previous line number"),
14191                    b->number, tp->static_trace_marker_id);
14192
14193           init_sal (&sal2);
14194
14195           sal2.pc = tpmarker->address;
14196
14197           sal2 = find_pc_line (tpmarker->address, 0);
14198           sym = find_pc_sect_function (tpmarker->address, NULL);
14199           ui_out_text (uiout, "Now in ");
14200           if (sym)
14201             {
14202               ui_out_field_string (uiout, "func",
14203                                    SYMBOL_PRINT_NAME (sym));
14204               ui_out_text (uiout, " at ");
14205             }
14206           ui_out_field_string (uiout, "file",
14207                                symtab_to_filename_for_display (sal2.symtab));
14208           ui_out_text (uiout, ":");
14209
14210           if (ui_out_is_mi_like_p (uiout))
14211             {
14212               const char *fullname = symtab_to_fullname (sal2.symtab);
14213
14214               ui_out_field_string (uiout, "fullname", fullname);
14215             }
14216
14217           ui_out_field_int (uiout, "line", sal2.line);
14218           ui_out_text (uiout, "\n");
14219
14220           b->loc->line_number = sal2.line;
14221           b->loc->symtab = sym != NULL ? sal2.symtab : NULL;
14222
14223           delete_event_location (b->location);
14224           initialize_explicit_location (&explicit_loc);
14225           explicit_loc.source_filename
14226             = ASTRDUP (symtab_to_filename_for_display (sal2.symtab));
14227           explicit_loc.line_offset.offset = b->loc->line_number;
14228           explicit_loc.line_offset.sign = LINE_OFFSET_NONE;
14229           b->location = new_explicit_location (&explicit_loc);
14230
14231           /* Might be nice to check if function changed, and warn if
14232              so.  */
14233
14234           release_static_tracepoint_marker (tpmarker);
14235         }
14236     }
14237   return sal;
14238 }
14239
14240 /* Returns 1 iff locations A and B are sufficiently same that
14241    we don't need to report breakpoint as changed.  */
14242
14243 static int
14244 locations_are_equal (struct bp_location *a, struct bp_location *b)
14245 {
14246   while (a && b)
14247     {
14248       if (a->address != b->address)
14249         return 0;
14250
14251       if (a->shlib_disabled != b->shlib_disabled)
14252         return 0;
14253
14254       if (a->enabled != b->enabled)
14255         return 0;
14256
14257       a = a->next;
14258       b = b->next;
14259     }
14260
14261   if ((a == NULL) != (b == NULL))
14262     return 0;
14263
14264   return 1;
14265 }
14266
14267 /* Split all locations of B that are bound to PSPACE out of B's
14268    location list to a separate list and return that list's head.  If
14269    PSPACE is NULL, hoist out all locations of B.  */
14270
14271 static struct bp_location *
14272 hoist_existing_locations (struct breakpoint *b, struct program_space *pspace)
14273 {
14274   struct bp_location head;
14275   struct bp_location *i = b->loc;
14276   struct bp_location **i_link = &b->loc;
14277   struct bp_location *hoisted = &head;
14278
14279   if (pspace == NULL)
14280     {
14281       i = b->loc;
14282       b->loc = NULL;
14283       return i;
14284     }
14285
14286   head.next = NULL;
14287
14288   while (i != NULL)
14289     {
14290       if (i->pspace == pspace)
14291         {
14292           *i_link = i->next;
14293           i->next = NULL;
14294           hoisted->next = i;
14295           hoisted = i;
14296         }
14297       else
14298         i_link = &i->next;
14299       i = *i_link;
14300     }
14301
14302   return head.next;
14303 }
14304
14305 /* Create new breakpoint locations for B (a hardware or software
14306    breakpoint) based on SALS and SALS_END.  If SALS_END.NELTS is not
14307    zero, then B is a ranged breakpoint.  Only recreates locations for
14308    FILTER_PSPACE.  Locations of other program spaces are left
14309    untouched.  */
14310
14311 void
14312 update_breakpoint_locations (struct breakpoint *b,
14313                              struct program_space *filter_pspace,
14314                              struct symtabs_and_lines sals,
14315                              struct symtabs_and_lines sals_end)
14316 {
14317   int i;
14318   struct bp_location *existing_locations;
14319
14320   if (sals_end.nelts != 0 && (sals.nelts != 1 || sals_end.nelts != 1))
14321     {
14322       /* Ranged breakpoints have only one start location and one end
14323          location.  */
14324       b->enable_state = bp_disabled;
14325       printf_unfiltered (_("Could not reset ranged breakpoint %d: "
14326                            "multiple locations found\n"),
14327                          b->number);
14328       return;
14329     }
14330
14331   /* If there's no new locations, and all existing locations are
14332      pending, don't do anything.  This optimizes the common case where
14333      all locations are in the same shared library, that was unloaded.
14334      We'd like to retain the location, so that when the library is
14335      loaded again, we don't loose the enabled/disabled status of the
14336      individual locations.  */
14337   if (all_locations_are_pending (b, filter_pspace) && sals.nelts == 0)
14338     return;
14339
14340   existing_locations = hoist_existing_locations (b, filter_pspace);
14341
14342   for (i = 0; i < sals.nelts; ++i)
14343     {
14344       struct bp_location *new_loc;
14345
14346       switch_to_program_space_and_thread (sals.sals[i].pspace);
14347
14348       new_loc = add_location_to_breakpoint (b, &(sals.sals[i]));
14349
14350       /* Reparse conditions, they might contain references to the
14351          old symtab.  */
14352       if (b->cond_string != NULL)
14353         {
14354           const char *s;
14355
14356           s = b->cond_string;
14357           TRY
14358             {
14359               new_loc->cond = parse_exp_1 (&s, sals.sals[i].pc,
14360                                            block_for_pc (sals.sals[i].pc), 
14361                                            0);
14362             }
14363           CATCH (e, RETURN_MASK_ERROR)
14364             {
14365               warning (_("failed to reevaluate condition "
14366                          "for breakpoint %d: %s"), 
14367                        b->number, e.message);
14368               new_loc->enabled = 0;
14369             }
14370           END_CATCH
14371         }
14372
14373       if (sals_end.nelts)
14374         {
14375           CORE_ADDR end = find_breakpoint_range_end (sals_end.sals[0]);
14376
14377           new_loc->length = end - sals.sals[0].pc + 1;
14378         }
14379     }
14380
14381   /* If possible, carry over 'disable' status from existing
14382      breakpoints.  */
14383   {
14384     struct bp_location *e = existing_locations;
14385     /* If there are multiple breakpoints with the same function name,
14386        e.g. for inline functions, comparing function names won't work.
14387        Instead compare pc addresses; this is just a heuristic as things
14388        may have moved, but in practice it gives the correct answer
14389        often enough until a better solution is found.  */
14390     int have_ambiguous_names = ambiguous_names_p (b->loc);
14391
14392     for (; e; e = e->next)
14393       {
14394         if (!e->enabled && e->function_name)
14395           {
14396             struct bp_location *l = b->loc;
14397             if (have_ambiguous_names)
14398               {
14399                 for (; l; l = l->next)
14400                   if (breakpoint_locations_match (e, l))
14401                     {
14402                       l->enabled = 0;
14403                       break;
14404                     }
14405               }
14406             else
14407               {
14408                 for (; l; l = l->next)
14409                   if (l->function_name
14410                       && strcmp (e->function_name, l->function_name) == 0)
14411                     {
14412                       l->enabled = 0;
14413                       break;
14414                     }
14415               }
14416           }
14417       }
14418   }
14419
14420   if (!locations_are_equal (existing_locations, b->loc))
14421     observer_notify_breakpoint_modified (b);
14422 }
14423
14424 /* Find the SaL locations corresponding to the given LOCATION.
14425    On return, FOUND will be 1 if any SaL was found, zero otherwise.  */
14426
14427 static struct symtabs_and_lines
14428 location_to_sals (struct breakpoint *b, struct event_location *location,
14429                   struct program_space *search_pspace, int *found)
14430 {
14431   struct symtabs_and_lines sals = {0};
14432   struct gdb_exception exception = exception_none;
14433
14434   gdb_assert (b->ops != NULL);
14435
14436   TRY
14437     {
14438       b->ops->decode_location (b, location, search_pspace, &sals);
14439     }
14440   CATCH (e, RETURN_MASK_ERROR)
14441     {
14442       int not_found_and_ok = 0;
14443
14444       exception = e;
14445
14446       /* For pending breakpoints, it's expected that parsing will
14447          fail until the right shared library is loaded.  User has
14448          already told to create pending breakpoints and don't need
14449          extra messages.  If breakpoint is in bp_shlib_disabled
14450          state, then user already saw the message about that
14451          breakpoint being disabled, and don't want to see more
14452          errors.  */
14453       if (e.error == NOT_FOUND_ERROR
14454           && (b->condition_not_parsed
14455               || (b->loc != NULL
14456                   && search_pspace != NULL
14457                   && b->loc->pspace != search_pspace)
14458               || (b->loc && b->loc->shlib_disabled)
14459               || (b->loc && b->loc->pspace->executing_startup)
14460               || b->enable_state == bp_disabled))
14461         not_found_and_ok = 1;
14462
14463       if (!not_found_and_ok)
14464         {
14465           /* We surely don't want to warn about the same breakpoint
14466              10 times.  One solution, implemented here, is disable
14467              the breakpoint on error.  Another solution would be to
14468              have separate 'warning emitted' flag.  Since this
14469              happens only when a binary has changed, I don't know
14470              which approach is better.  */
14471           b->enable_state = bp_disabled;
14472           throw_exception (e);
14473         }
14474     }
14475   END_CATCH
14476
14477   if (exception.reason == 0 || exception.error != NOT_FOUND_ERROR)
14478     {
14479       int i;
14480
14481       for (i = 0; i < sals.nelts; ++i)
14482         resolve_sal_pc (&sals.sals[i]);
14483       if (b->condition_not_parsed && b->extra_string != NULL)
14484         {
14485           char *cond_string, *extra_string;
14486           int thread, task;
14487
14488           find_condition_and_thread (b->extra_string, sals.sals[0].pc,
14489                                      &cond_string, &thread, &task,
14490                                      &extra_string);
14491           gdb_assert (b->cond_string == NULL);
14492           if (cond_string)
14493             b->cond_string = cond_string;
14494           b->thread = thread;
14495           b->task = task;
14496           if (extra_string)
14497             {
14498               xfree (b->extra_string);
14499               b->extra_string = extra_string;
14500             }
14501           b->condition_not_parsed = 0;
14502         }
14503
14504       if (b->type == bp_static_tracepoint && !strace_marker_p (b))
14505         sals.sals[0] = update_static_tracepoint (b, sals.sals[0]);
14506
14507       *found = 1;
14508     }
14509   else
14510     *found = 0;
14511
14512   return sals;
14513 }
14514
14515 /* The default re_set method, for typical hardware or software
14516    breakpoints.  Reevaluate the breakpoint and recreate its
14517    locations.  */
14518
14519 static void
14520 breakpoint_re_set_default (struct breakpoint *b)
14521 {
14522   int found;
14523   struct symtabs_and_lines sals, sals_end;
14524   struct symtabs_and_lines expanded = {0};
14525   struct symtabs_and_lines expanded_end = {0};
14526   struct program_space *filter_pspace = current_program_space;
14527
14528   sals = location_to_sals (b, b->location, filter_pspace, &found);
14529   if (found)
14530     {
14531       make_cleanup (xfree, sals.sals);
14532       expanded = sals;
14533     }
14534
14535   if (b->location_range_end != NULL)
14536     {
14537       sals_end = location_to_sals (b, b->location_range_end,
14538                                    filter_pspace, &found);
14539       if (found)
14540         {
14541           make_cleanup (xfree, sals_end.sals);
14542           expanded_end = sals_end;
14543         }
14544     }
14545
14546   update_breakpoint_locations (b, filter_pspace, expanded, expanded_end);
14547 }
14548
14549 /* Default method for creating SALs from an address string.  It basically
14550    calls parse_breakpoint_sals.  Return 1 for success, zero for failure.  */
14551
14552 static void
14553 create_sals_from_location_default (const struct event_location *location,
14554                                    struct linespec_result *canonical,
14555                                    enum bptype type_wanted)
14556 {
14557   parse_breakpoint_sals (location, canonical);
14558 }
14559
14560 /* Call create_breakpoints_sal for the given arguments.  This is the default
14561    function for the `create_breakpoints_sal' method of
14562    breakpoint_ops.  */
14563
14564 static void
14565 create_breakpoints_sal_default (struct gdbarch *gdbarch,
14566                                 struct linespec_result *canonical,
14567                                 char *cond_string,
14568                                 char *extra_string,
14569                                 enum bptype type_wanted,
14570                                 enum bpdisp disposition,
14571                                 int thread,
14572                                 int task, int ignore_count,
14573                                 const struct breakpoint_ops *ops,
14574                                 int from_tty, int enabled,
14575                                 int internal, unsigned flags)
14576 {
14577   create_breakpoints_sal (gdbarch, canonical, cond_string,
14578                           extra_string,
14579                           type_wanted, disposition,
14580                           thread, task, ignore_count, ops, from_tty,
14581                           enabled, internal, flags);
14582 }
14583
14584 /* Decode the line represented by S by calling decode_line_full.  This is the
14585    default function for the `decode_location' method of breakpoint_ops.  */
14586
14587 static void
14588 decode_location_default (struct breakpoint *b,
14589                          const struct event_location *location,
14590                          struct program_space *search_pspace,
14591                          struct symtabs_and_lines *sals)
14592 {
14593   struct linespec_result canonical;
14594
14595   init_linespec_result (&canonical);
14596   decode_line_full (location, DECODE_LINE_FUNFIRSTLINE, search_pspace,
14597                     (struct symtab *) NULL, 0,
14598                     &canonical, multiple_symbols_all,
14599                     b->filter);
14600
14601   /* We should get 0 or 1 resulting SALs.  */
14602   gdb_assert (VEC_length (linespec_sals, canonical.sals) < 2);
14603
14604   if (VEC_length (linespec_sals, canonical.sals) > 0)
14605     {
14606       struct linespec_sals *lsal;
14607
14608       lsal = VEC_index (linespec_sals, canonical.sals, 0);
14609       *sals = lsal->sals;
14610       /* Arrange it so the destructor does not free the
14611          contents.  */
14612       lsal->sals.sals = NULL;
14613     }
14614
14615   destroy_linespec_result (&canonical);
14616 }
14617
14618 /* Prepare the global context for a re-set of breakpoint B.  */
14619
14620 static struct cleanup *
14621 prepare_re_set_context (struct breakpoint *b)
14622 {
14623   input_radix = b->input_radix;
14624   set_language (b->language);
14625
14626   return make_cleanup (null_cleanup, NULL);
14627 }
14628
14629 /* Reset a breakpoint given it's struct breakpoint * BINT.
14630    The value we return ends up being the return value from catch_errors.
14631    Unused in this case.  */
14632
14633 static int
14634 breakpoint_re_set_one (void *bint)
14635 {
14636   /* Get past catch_errs.  */
14637   struct breakpoint *b = (struct breakpoint *) bint;
14638   struct cleanup *cleanups;
14639
14640   cleanups = prepare_re_set_context (b);
14641   b->ops->re_set (b);
14642   do_cleanups (cleanups);
14643   return 0;
14644 }
14645
14646 /* Re-set breakpoint locations for the current program space.
14647    Locations bound to other program spaces are left untouched.  */
14648
14649 void
14650 breakpoint_re_set (void)
14651 {
14652   struct breakpoint *b, *b_tmp;
14653   enum language save_language;
14654   int save_input_radix;
14655   struct cleanup *old_chain;
14656
14657   save_language = current_language->la_language;
14658   save_input_radix = input_radix;
14659   old_chain = save_current_space_and_thread ();
14660
14661   /* Note: we must not try to insert locations until after all
14662      breakpoints have been re-set.  Otherwise, e.g., when re-setting
14663      breakpoint 1, we'd insert the locations of breakpoint 2, which
14664      hadn't been re-set yet, and thus may have stale locations.  */
14665
14666   ALL_BREAKPOINTS_SAFE (b, b_tmp)
14667   {
14668     /* Format possible error msg.  */
14669     char *message = xstrprintf ("Error in re-setting breakpoint %d: ",
14670                                 b->number);
14671     struct cleanup *cleanups = make_cleanup (xfree, message);
14672     catch_errors (breakpoint_re_set_one, b, message, RETURN_MASK_ALL);
14673     do_cleanups (cleanups);
14674   }
14675   set_language (save_language);
14676   input_radix = save_input_radix;
14677
14678   jit_breakpoint_re_set ();
14679
14680   do_cleanups (old_chain);
14681
14682   create_overlay_event_breakpoint ();
14683   create_longjmp_master_breakpoint ();
14684   create_std_terminate_master_breakpoint ();
14685   create_exception_master_breakpoint ();
14686
14687   /* Now we can insert.  */
14688   update_global_location_list (UGLL_MAY_INSERT);
14689 }
14690 \f
14691 /* Reset the thread number of this breakpoint:
14692
14693    - If the breakpoint is for all threads, leave it as-is.
14694    - Else, reset it to the current thread for inferior_ptid.  */
14695 void
14696 breakpoint_re_set_thread (struct breakpoint *b)
14697 {
14698   if (b->thread != -1)
14699     {
14700       if (in_thread_list (inferior_ptid))
14701         b->thread = ptid_to_global_thread_id (inferior_ptid);
14702
14703       /* We're being called after following a fork.  The new fork is
14704          selected as current, and unless this was a vfork will have a
14705          different program space from the original thread.  Reset that
14706          as well.  */
14707       b->loc->pspace = current_program_space;
14708     }
14709 }
14710
14711 /* Set ignore-count of breakpoint number BPTNUM to COUNT.
14712    If from_tty is nonzero, it prints a message to that effect,
14713    which ends with a period (no newline).  */
14714
14715 void
14716 set_ignore_count (int bptnum, int count, int from_tty)
14717 {
14718   struct breakpoint *b;
14719
14720   if (count < 0)
14721     count = 0;
14722
14723   ALL_BREAKPOINTS (b)
14724     if (b->number == bptnum)
14725     {
14726       if (is_tracepoint (b))
14727         {
14728           if (from_tty && count != 0)
14729             printf_filtered (_("Ignore count ignored for tracepoint %d."),
14730                              bptnum);
14731           return;
14732         }
14733       
14734       b->ignore_count = count;
14735       if (from_tty)
14736         {
14737           if (count == 0)
14738             printf_filtered (_("Will stop next time "
14739                                "breakpoint %d is reached."),
14740                              bptnum);
14741           else if (count == 1)
14742             printf_filtered (_("Will ignore next crossing of breakpoint %d."),
14743                              bptnum);
14744           else
14745             printf_filtered (_("Will ignore next %d "
14746                                "crossings of breakpoint %d."),
14747                              count, bptnum);
14748         }
14749       observer_notify_breakpoint_modified (b);
14750       return;
14751     }
14752
14753   error (_("No breakpoint number %d."), bptnum);
14754 }
14755
14756 /* Command to set ignore-count of breakpoint N to COUNT.  */
14757
14758 static void
14759 ignore_command (char *args, int from_tty)
14760 {
14761   char *p = args;
14762   int num;
14763
14764   if (p == 0)
14765     error_no_arg (_("a breakpoint number"));
14766
14767   num = get_number (&p);
14768   if (num == 0)
14769     error (_("bad breakpoint number: '%s'"), args);
14770   if (*p == 0)
14771     error (_("Second argument (specified ignore-count) is missing."));
14772
14773   set_ignore_count (num,
14774                     longest_to_int (value_as_long (parse_and_eval (p))),
14775                     from_tty);
14776   if (from_tty)
14777     printf_filtered ("\n");
14778 }
14779 \f
14780 /* Call FUNCTION on each of the breakpoints
14781    whose numbers are given in ARGS.  */
14782
14783 static void
14784 map_breakpoint_numbers (char *args, void (*function) (struct breakpoint *,
14785                                                       void *),
14786                         void *data)
14787 {
14788   int num;
14789   struct breakpoint *b, *tmp;
14790
14791   if (args == 0 || *args == '\0')
14792     error_no_arg (_("one or more breakpoint numbers"));
14793
14794   number_or_range_parser parser (args);
14795
14796   while (!parser.finished ())
14797     {
14798       const char *p = parser.cur_tok ();
14799       bool match = false;
14800
14801       num = parser.get_number ();
14802       if (num == 0)
14803         {
14804           warning (_("bad breakpoint number at or near '%s'"), p);
14805         }
14806       else
14807         {
14808           ALL_BREAKPOINTS_SAFE (b, tmp)
14809             if (b->number == num)
14810               {
14811                 match = true;
14812                 function (b, data);
14813                 break;
14814               }
14815           if (!match)
14816             printf_unfiltered (_("No breakpoint number %d.\n"), num);
14817         }
14818     }
14819 }
14820
14821 static struct bp_location *
14822 find_location_by_number (char *number)
14823 {
14824   char *dot = strchr (number, '.');
14825   char *p1;
14826   int bp_num;
14827   int loc_num;
14828   struct breakpoint *b;
14829   struct bp_location *loc;  
14830
14831   *dot = '\0';
14832
14833   p1 = number;
14834   bp_num = get_number (&p1);
14835   if (bp_num == 0)
14836     error (_("Bad breakpoint number '%s'"), number);
14837
14838   ALL_BREAKPOINTS (b)
14839     if (b->number == bp_num)
14840       {
14841         break;
14842       }
14843
14844   if (!b || b->number != bp_num)
14845     error (_("Bad breakpoint number '%s'"), number);
14846   
14847   p1 = dot+1;
14848   loc_num = get_number (&p1);
14849   if (loc_num == 0)
14850     error (_("Bad breakpoint location number '%s'"), number);
14851
14852   --loc_num;
14853   loc = b->loc;
14854   for (;loc_num && loc; --loc_num, loc = loc->next)
14855     ;
14856   if (!loc)
14857     error (_("Bad breakpoint location number '%s'"), dot+1);
14858     
14859   return loc;  
14860 }
14861
14862
14863 /* Set ignore-count of breakpoint number BPTNUM to COUNT.
14864    If from_tty is nonzero, it prints a message to that effect,
14865    which ends with a period (no newline).  */
14866
14867 void
14868 disable_breakpoint (struct breakpoint *bpt)
14869 {
14870   /* Never disable a watchpoint scope breakpoint; we want to
14871      hit them when we leave scope so we can delete both the
14872      watchpoint and its scope breakpoint at that time.  */
14873   if (bpt->type == bp_watchpoint_scope)
14874     return;
14875
14876   bpt->enable_state = bp_disabled;
14877
14878   /* Mark breakpoint locations modified.  */
14879   mark_breakpoint_modified (bpt);
14880
14881   if (target_supports_enable_disable_tracepoint ()
14882       && current_trace_status ()->running && is_tracepoint (bpt))
14883     {
14884       struct bp_location *location;
14885      
14886       for (location = bpt->loc; location; location = location->next)
14887         target_disable_tracepoint (location);
14888     }
14889
14890   update_global_location_list (UGLL_DONT_INSERT);
14891
14892   observer_notify_breakpoint_modified (bpt);
14893 }
14894
14895 /* A callback for iterate_over_related_breakpoints.  */
14896
14897 static void
14898 do_disable_breakpoint (struct breakpoint *b, void *ignore)
14899 {
14900   disable_breakpoint (b);
14901 }
14902
14903 /* A callback for map_breakpoint_numbers that calls
14904    disable_breakpoint.  */
14905
14906 static void
14907 do_map_disable_breakpoint (struct breakpoint *b, void *ignore)
14908 {
14909   iterate_over_related_breakpoints (b, do_disable_breakpoint, NULL);
14910 }
14911
14912 static void
14913 disable_command (char *args, int from_tty)
14914 {
14915   if (args == 0)
14916     {
14917       struct breakpoint *bpt;
14918
14919       ALL_BREAKPOINTS (bpt)
14920         if (user_breakpoint_p (bpt))
14921           disable_breakpoint (bpt);
14922     }
14923   else
14924     {
14925       char *num = extract_arg (&args);
14926
14927       while (num)
14928         {
14929           if (strchr (num, '.'))
14930             {
14931               struct bp_location *loc = find_location_by_number (num);
14932
14933               if (loc)
14934                 {
14935                   if (loc->enabled)
14936                     {
14937                       loc->enabled = 0;
14938                       mark_breakpoint_location_modified (loc);
14939                     }
14940                   if (target_supports_enable_disable_tracepoint ()
14941                       && current_trace_status ()->running && loc->owner
14942                       && is_tracepoint (loc->owner))
14943                     target_disable_tracepoint (loc);
14944                 }
14945               update_global_location_list (UGLL_DONT_INSERT);
14946             }
14947           else
14948             map_breakpoint_numbers (num, do_map_disable_breakpoint, NULL);
14949           num = extract_arg (&args);
14950         }
14951     }
14952 }
14953
14954 static void
14955 enable_breakpoint_disp (struct breakpoint *bpt, enum bpdisp disposition,
14956                         int count)
14957 {
14958   int target_resources_ok;
14959
14960   if (bpt->type == bp_hardware_breakpoint)
14961     {
14962       int i;
14963       i = hw_breakpoint_used_count ();
14964       target_resources_ok = 
14965         target_can_use_hardware_watchpoint (bp_hardware_breakpoint, 
14966                                             i + 1, 0);
14967       if (target_resources_ok == 0)
14968         error (_("No hardware breakpoint support in the target."));
14969       else if (target_resources_ok < 0)
14970         error (_("Hardware breakpoints used exceeds limit."));
14971     }
14972
14973   if (is_watchpoint (bpt))
14974     {
14975       /* Initialize it just to avoid a GCC false warning.  */
14976       enum enable_state orig_enable_state = bp_disabled;
14977
14978       TRY
14979         {
14980           struct watchpoint *w = (struct watchpoint *) bpt;
14981
14982           orig_enable_state = bpt->enable_state;
14983           bpt->enable_state = bp_enabled;
14984           update_watchpoint (w, 1 /* reparse */);
14985         }
14986       CATCH (e, RETURN_MASK_ALL)
14987         {
14988           bpt->enable_state = orig_enable_state;
14989           exception_fprintf (gdb_stderr, e, _("Cannot enable watchpoint %d: "),
14990                              bpt->number);
14991           return;
14992         }
14993       END_CATCH
14994     }
14995
14996   bpt->enable_state = bp_enabled;
14997
14998   /* Mark breakpoint locations modified.  */
14999   mark_breakpoint_modified (bpt);
15000
15001   if (target_supports_enable_disable_tracepoint ()
15002       && current_trace_status ()->running && is_tracepoint (bpt))
15003     {
15004       struct bp_location *location;
15005
15006       for (location = bpt->loc; location; location = location->next)
15007         target_enable_tracepoint (location);
15008     }
15009
15010   bpt->disposition = disposition;
15011   bpt->enable_count = count;
15012   update_global_location_list (UGLL_MAY_INSERT);
15013
15014   observer_notify_breakpoint_modified (bpt);
15015 }
15016
15017
15018 void
15019 enable_breakpoint (struct breakpoint *bpt)
15020 {
15021   enable_breakpoint_disp (bpt, bpt->disposition, 0);
15022 }
15023
15024 static void
15025 do_enable_breakpoint (struct breakpoint *bpt, void *arg)
15026 {
15027   enable_breakpoint (bpt);
15028 }
15029
15030 /* A callback for map_breakpoint_numbers that calls
15031    enable_breakpoint.  */
15032
15033 static void
15034 do_map_enable_breakpoint (struct breakpoint *b, void *ignore)
15035 {
15036   iterate_over_related_breakpoints (b, do_enable_breakpoint, NULL);
15037 }
15038
15039 /* The enable command enables the specified breakpoints (or all defined
15040    breakpoints) so they once again become (or continue to be) effective
15041    in stopping the inferior.  */
15042
15043 static void
15044 enable_command (char *args, int from_tty)
15045 {
15046   if (args == 0)
15047     {
15048       struct breakpoint *bpt;
15049
15050       ALL_BREAKPOINTS (bpt)
15051         if (user_breakpoint_p (bpt))
15052           enable_breakpoint (bpt);
15053     }
15054   else
15055     {
15056       char *num = extract_arg (&args);
15057
15058       while (num)
15059         {
15060           if (strchr (num, '.'))
15061             {
15062               struct bp_location *loc = find_location_by_number (num);
15063
15064               if (loc)
15065                 {
15066                   if (!loc->enabled)
15067                     {
15068                       loc->enabled = 1;
15069                       mark_breakpoint_location_modified (loc);
15070                     }
15071                   if (target_supports_enable_disable_tracepoint ()
15072                       && current_trace_status ()->running && loc->owner
15073                       && is_tracepoint (loc->owner))
15074                     target_enable_tracepoint (loc);
15075                 }
15076               update_global_location_list (UGLL_MAY_INSERT);
15077             }
15078           else
15079             map_breakpoint_numbers (num, do_map_enable_breakpoint, NULL);
15080           num = extract_arg (&args);
15081         }
15082     }
15083 }
15084
15085 /* This struct packages up disposition data for application to multiple
15086    breakpoints.  */
15087
15088 struct disp_data
15089 {
15090   enum bpdisp disp;
15091   int count;
15092 };
15093
15094 static void
15095 do_enable_breakpoint_disp (struct breakpoint *bpt, void *arg)
15096 {
15097   struct disp_data disp_data = *(struct disp_data *) arg;
15098
15099   enable_breakpoint_disp (bpt, disp_data.disp, disp_data.count);
15100 }
15101
15102 static void
15103 do_map_enable_once_breakpoint (struct breakpoint *bpt, void *ignore)
15104 {
15105   struct disp_data disp = { disp_disable, 1 };
15106
15107   iterate_over_related_breakpoints (bpt, do_enable_breakpoint_disp, &disp);
15108 }
15109
15110 static void
15111 enable_once_command (char *args, int from_tty)
15112 {
15113   map_breakpoint_numbers (args, do_map_enable_once_breakpoint, NULL);
15114 }
15115
15116 static void
15117 do_map_enable_count_breakpoint (struct breakpoint *bpt, void *countptr)
15118 {
15119   struct disp_data disp = { disp_disable, *(int *) countptr };
15120
15121   iterate_over_related_breakpoints (bpt, do_enable_breakpoint_disp, &disp);
15122 }
15123
15124 static void
15125 enable_count_command (char *args, int from_tty)
15126 {
15127   int count;
15128
15129   if (args == NULL)
15130     error_no_arg (_("hit count"));
15131
15132   count = get_number (&args);
15133
15134   map_breakpoint_numbers (args, do_map_enable_count_breakpoint, &count);
15135 }
15136
15137 static void
15138 do_map_enable_delete_breakpoint (struct breakpoint *bpt, void *ignore)
15139 {
15140   struct disp_data disp = { disp_del, 1 };
15141
15142   iterate_over_related_breakpoints (bpt, do_enable_breakpoint_disp, &disp);
15143 }
15144
15145 static void
15146 enable_delete_command (char *args, int from_tty)
15147 {
15148   map_breakpoint_numbers (args, do_map_enable_delete_breakpoint, NULL);
15149 }
15150 \f
15151 static void
15152 set_breakpoint_cmd (char *args, int from_tty)
15153 {
15154 }
15155
15156 static void
15157 show_breakpoint_cmd (char *args, int from_tty)
15158 {
15159 }
15160
15161 /* Invalidate last known value of any hardware watchpoint if
15162    the memory which that value represents has been written to by
15163    GDB itself.  */
15164
15165 static void
15166 invalidate_bp_value_on_memory_change (struct inferior *inferior,
15167                                       CORE_ADDR addr, ssize_t len,
15168                                       const bfd_byte *data)
15169 {
15170   struct breakpoint *bp;
15171
15172   ALL_BREAKPOINTS (bp)
15173     if (bp->enable_state == bp_enabled
15174         && bp->type == bp_hardware_watchpoint)
15175       {
15176         struct watchpoint *wp = (struct watchpoint *) bp;
15177
15178         if (wp->val_valid && wp->val)
15179           {
15180             struct bp_location *loc;
15181
15182             for (loc = bp->loc; loc != NULL; loc = loc->next)
15183               if (loc->loc_type == bp_loc_hardware_watchpoint
15184                   && loc->address + loc->length > addr
15185                   && addr + len > loc->address)
15186                 {
15187                   value_free (wp->val);
15188                   wp->val = NULL;
15189                   wp->val_valid = 0;
15190                 }
15191           }
15192       }
15193 }
15194
15195 /* Create and insert a breakpoint for software single step.  */
15196
15197 void
15198 insert_single_step_breakpoint (struct gdbarch *gdbarch,
15199                                struct address_space *aspace, 
15200                                CORE_ADDR next_pc)
15201 {
15202   struct thread_info *tp = inferior_thread ();
15203   struct symtab_and_line sal;
15204   CORE_ADDR pc = next_pc;
15205
15206   if (tp->control.single_step_breakpoints == NULL)
15207     {
15208       tp->control.single_step_breakpoints
15209         = new_single_step_breakpoint (tp->global_num, gdbarch);
15210     }
15211
15212   sal = find_pc_line (pc, 0);
15213   sal.pc = pc;
15214   sal.section = find_pc_overlay (pc);
15215   sal.explicit_pc = 1;
15216   add_location_to_breakpoint (tp->control.single_step_breakpoints, &sal);
15217
15218   update_global_location_list (UGLL_INSERT);
15219 }
15220
15221 /* See breakpoint.h.  */
15222
15223 int
15224 breakpoint_has_location_inserted_here (struct breakpoint *bp,
15225                                        struct address_space *aspace,
15226                                        CORE_ADDR pc)
15227 {
15228   struct bp_location *loc;
15229
15230   for (loc = bp->loc; loc != NULL; loc = loc->next)
15231     if (loc->inserted
15232         && breakpoint_location_address_match (loc, aspace, pc))
15233       return 1;
15234
15235   return 0;
15236 }
15237
15238 /* Check whether a software single-step breakpoint is inserted at
15239    PC.  */
15240
15241 int
15242 single_step_breakpoint_inserted_here_p (struct address_space *aspace,
15243                                         CORE_ADDR pc)
15244 {
15245   struct breakpoint *bpt;
15246
15247   ALL_BREAKPOINTS (bpt)
15248     {
15249       if (bpt->type == bp_single_step
15250           && breakpoint_has_location_inserted_here (bpt, aspace, pc))
15251         return 1;
15252     }
15253   return 0;
15254 }
15255
15256 /* Tracepoint-specific operations.  */
15257
15258 /* Set tracepoint count to NUM.  */
15259 static void
15260 set_tracepoint_count (int num)
15261 {
15262   tracepoint_count = num;
15263   set_internalvar_integer (lookup_internalvar ("tpnum"), num);
15264 }
15265
15266 static void
15267 trace_command (char *arg, int from_tty)
15268 {
15269   struct breakpoint_ops *ops;
15270   struct event_location *location;
15271   struct cleanup *back_to;
15272
15273   location = string_to_event_location (&arg, current_language);
15274   back_to = make_cleanup_delete_event_location (location);
15275   if (location != NULL
15276       && event_location_type (location) == PROBE_LOCATION)
15277     ops = &tracepoint_probe_breakpoint_ops;
15278   else
15279     ops = &tracepoint_breakpoint_ops;
15280
15281   create_breakpoint (get_current_arch (),
15282                      location,
15283                      NULL, 0, arg, 1 /* parse arg */,
15284                      0 /* tempflag */,
15285                      bp_tracepoint /* type_wanted */,
15286                      0 /* Ignore count */,
15287                      pending_break_support,
15288                      ops,
15289                      from_tty,
15290                      1 /* enabled */,
15291                      0 /* internal */, 0);
15292   do_cleanups (back_to);
15293 }
15294
15295 static void
15296 ftrace_command (char *arg, int from_tty)
15297 {
15298   struct event_location *location;
15299   struct cleanup *back_to;
15300
15301   location = string_to_event_location (&arg, current_language);
15302   back_to = make_cleanup_delete_event_location (location);
15303   create_breakpoint (get_current_arch (),
15304                      location,
15305                      NULL, 0, arg, 1 /* parse arg */,
15306                      0 /* tempflag */,
15307                      bp_fast_tracepoint /* type_wanted */,
15308                      0 /* Ignore count */,
15309                      pending_break_support,
15310                      &tracepoint_breakpoint_ops,
15311                      from_tty,
15312                      1 /* enabled */,
15313                      0 /* internal */, 0);
15314   do_cleanups (back_to);
15315 }
15316
15317 /* strace command implementation.  Creates a static tracepoint.  */
15318
15319 static void
15320 strace_command (char *arg, int from_tty)
15321 {
15322   struct breakpoint_ops *ops;
15323   struct event_location *location;
15324   struct cleanup *back_to;
15325
15326   /* Decide if we are dealing with a static tracepoint marker (`-m'),
15327      or with a normal static tracepoint.  */
15328   if (arg && startswith (arg, "-m") && isspace (arg[2]))
15329     {
15330       ops = &strace_marker_breakpoint_ops;
15331       location = new_linespec_location (&arg);
15332     }
15333   else
15334     {
15335       ops = &tracepoint_breakpoint_ops;
15336       location = string_to_event_location (&arg, current_language);
15337     }
15338
15339   back_to = make_cleanup_delete_event_location (location);
15340   create_breakpoint (get_current_arch (),
15341                      location,
15342                      NULL, 0, arg, 1 /* parse arg */,
15343                      0 /* tempflag */,
15344                      bp_static_tracepoint /* type_wanted */,
15345                      0 /* Ignore count */,
15346                      pending_break_support,
15347                      ops,
15348                      from_tty,
15349                      1 /* enabled */,
15350                      0 /* internal */, 0);
15351   do_cleanups (back_to);
15352 }
15353
15354 /* Set up a fake reader function that gets command lines from a linked
15355    list that was acquired during tracepoint uploading.  */
15356
15357 static struct uploaded_tp *this_utp;
15358 static int next_cmd;
15359
15360 static char *
15361 read_uploaded_action (void)
15362 {
15363   char *rslt;
15364
15365   VEC_iterate (char_ptr, this_utp->cmd_strings, next_cmd, rslt);
15366
15367   next_cmd++;
15368
15369   return rslt;
15370 }
15371
15372 /* Given information about a tracepoint as recorded on a target (which
15373    can be either a live system or a trace file), attempt to create an
15374    equivalent GDB tracepoint.  This is not a reliable process, since
15375    the target does not necessarily have all the information used when
15376    the tracepoint was originally defined.  */
15377   
15378 struct tracepoint *
15379 create_tracepoint_from_upload (struct uploaded_tp *utp)
15380 {
15381   char *addr_str, small_buf[100];
15382   struct tracepoint *tp;
15383   struct event_location *location;
15384   struct cleanup *cleanup;
15385
15386   if (utp->at_string)
15387     addr_str = utp->at_string;
15388   else
15389     {
15390       /* In the absence of a source location, fall back to raw
15391          address.  Since there is no way to confirm that the address
15392          means the same thing as when the trace was started, warn the
15393          user.  */
15394       warning (_("Uploaded tracepoint %d has no "
15395                  "source location, using raw address"),
15396                utp->number);
15397       xsnprintf (small_buf, sizeof (small_buf), "*%s", hex_string (utp->addr));
15398       addr_str = small_buf;
15399     }
15400
15401   /* There's not much we can do with a sequence of bytecodes.  */
15402   if (utp->cond && !utp->cond_string)
15403     warning (_("Uploaded tracepoint %d condition "
15404                "has no source form, ignoring it"),
15405              utp->number);
15406
15407   location = string_to_event_location (&addr_str, current_language);
15408   cleanup = make_cleanup_delete_event_location (location);
15409   if (!create_breakpoint (get_current_arch (),
15410                           location,
15411                           utp->cond_string, -1, addr_str,
15412                           0 /* parse cond/thread */,
15413                           0 /* tempflag */,
15414                           utp->type /* type_wanted */,
15415                           0 /* Ignore count */,
15416                           pending_break_support,
15417                           &tracepoint_breakpoint_ops,
15418                           0 /* from_tty */,
15419                           utp->enabled /* enabled */,
15420                           0 /* internal */,
15421                           CREATE_BREAKPOINT_FLAGS_INSERTED))
15422     {
15423       do_cleanups (cleanup);
15424       return NULL;
15425     }
15426
15427   do_cleanups (cleanup);
15428
15429   /* Get the tracepoint we just created.  */
15430   tp = get_tracepoint (tracepoint_count);
15431   gdb_assert (tp != NULL);
15432
15433   if (utp->pass > 0)
15434     {
15435       xsnprintf (small_buf, sizeof (small_buf), "%d %d", utp->pass,
15436                  tp->base.number);
15437
15438       trace_pass_command (small_buf, 0);
15439     }
15440
15441   /* If we have uploaded versions of the original commands, set up a
15442      special-purpose "reader" function and call the usual command line
15443      reader, then pass the result to the breakpoint command-setting
15444      function.  */
15445   if (!VEC_empty (char_ptr, utp->cmd_strings))
15446     {
15447       struct command_line *cmd_list;
15448
15449       this_utp = utp;
15450       next_cmd = 0;
15451
15452       cmd_list = read_command_lines_1 (read_uploaded_action, 1, NULL, NULL);
15453
15454       breakpoint_set_commands (&tp->base, cmd_list);
15455     }
15456   else if (!VEC_empty (char_ptr, utp->actions)
15457            || !VEC_empty (char_ptr, utp->step_actions))
15458     warning (_("Uploaded tracepoint %d actions "
15459                "have no source form, ignoring them"),
15460              utp->number);
15461
15462   /* Copy any status information that might be available.  */
15463   tp->base.hit_count = utp->hit_count;
15464   tp->traceframe_usage = utp->traceframe_usage;
15465
15466   return tp;
15467 }
15468   
15469 /* Print information on tracepoint number TPNUM_EXP, or all if
15470    omitted.  */
15471
15472 static void
15473 tracepoints_info (char *args, int from_tty)
15474 {
15475   struct ui_out *uiout = current_uiout;
15476   int num_printed;
15477
15478   num_printed = breakpoint_1 (args, 0, is_tracepoint);
15479
15480   if (num_printed == 0)
15481     {
15482       if (args == NULL || *args == '\0')
15483         ui_out_message (uiout, 0, "No tracepoints.\n");
15484       else
15485         ui_out_message (uiout, 0, "No tracepoint matching '%s'.\n", args);
15486     }
15487
15488   default_collect_info ();
15489 }
15490
15491 /* The 'enable trace' command enables tracepoints.
15492    Not supported by all targets.  */
15493 static void
15494 enable_trace_command (char *args, int from_tty)
15495 {
15496   enable_command (args, from_tty);
15497 }
15498
15499 /* The 'disable trace' command disables tracepoints.
15500    Not supported by all targets.  */
15501 static void
15502 disable_trace_command (char *args, int from_tty)
15503 {
15504   disable_command (args, from_tty);
15505 }
15506
15507 /* Remove a tracepoint (or all if no argument).  */
15508 static void
15509 delete_trace_command (char *arg, int from_tty)
15510 {
15511   struct breakpoint *b, *b_tmp;
15512
15513   dont_repeat ();
15514
15515   if (arg == 0)
15516     {
15517       int breaks_to_delete = 0;
15518
15519       /* Delete all breakpoints if no argument.
15520          Do not delete internal or call-dummy breakpoints, these
15521          have to be deleted with an explicit breakpoint number 
15522          argument.  */
15523       ALL_TRACEPOINTS (b)
15524         if (is_tracepoint (b) && user_breakpoint_p (b))
15525           {
15526             breaks_to_delete = 1;
15527             break;
15528           }
15529
15530       /* Ask user only if there are some breakpoints to delete.  */
15531       if (!from_tty
15532           || (breaks_to_delete && query (_("Delete all tracepoints? "))))
15533         {
15534           ALL_BREAKPOINTS_SAFE (b, b_tmp)
15535             if (is_tracepoint (b) && user_breakpoint_p (b))
15536               delete_breakpoint (b);
15537         }
15538     }
15539   else
15540     map_breakpoint_numbers (arg, do_map_delete_breakpoint, NULL);
15541 }
15542
15543 /* Helper function for trace_pass_command.  */
15544
15545 static void
15546 trace_pass_set_count (struct tracepoint *tp, int count, int from_tty)
15547 {
15548   tp->pass_count = count;
15549   observer_notify_breakpoint_modified (&tp->base);
15550   if (from_tty)
15551     printf_filtered (_("Setting tracepoint %d's passcount to %d\n"),
15552                      tp->base.number, count);
15553 }
15554
15555 /* Set passcount for tracepoint.
15556
15557    First command argument is passcount, second is tracepoint number.
15558    If tracepoint number omitted, apply to most recently defined.
15559    Also accepts special argument "all".  */
15560
15561 static void
15562 trace_pass_command (char *args, int from_tty)
15563 {
15564   struct tracepoint *t1;
15565   unsigned int count;
15566
15567   if (args == 0 || *args == 0)
15568     error (_("passcount command requires an "
15569              "argument (count + optional TP num)"));
15570
15571   count = strtoul (args, &args, 10);    /* Count comes first, then TP num.  */
15572
15573   args = skip_spaces (args);
15574   if (*args && strncasecmp (args, "all", 3) == 0)
15575     {
15576       struct breakpoint *b;
15577
15578       args += 3;                        /* Skip special argument "all".  */
15579       if (*args)
15580         error (_("Junk at end of arguments."));
15581
15582       ALL_TRACEPOINTS (b)
15583       {
15584         t1 = (struct tracepoint *) b;
15585         trace_pass_set_count (t1, count, from_tty);
15586       }
15587     }
15588   else if (*args == '\0')
15589     {
15590       t1 = get_tracepoint_by_number (&args, NULL);
15591       if (t1)
15592         trace_pass_set_count (t1, count, from_tty);
15593     }
15594   else
15595     {
15596       number_or_range_parser parser (args);
15597       while (!parser.finished ())
15598         {
15599           t1 = get_tracepoint_by_number (&args, &parser);
15600           if (t1)
15601             trace_pass_set_count (t1, count, from_tty);
15602         }
15603     }
15604 }
15605
15606 struct tracepoint *
15607 get_tracepoint (int num)
15608 {
15609   struct breakpoint *t;
15610
15611   ALL_TRACEPOINTS (t)
15612     if (t->number == num)
15613       return (struct tracepoint *) t;
15614
15615   return NULL;
15616 }
15617
15618 /* Find the tracepoint with the given target-side number (which may be
15619    different from the tracepoint number after disconnecting and
15620    reconnecting).  */
15621
15622 struct tracepoint *
15623 get_tracepoint_by_number_on_target (int num)
15624 {
15625   struct breakpoint *b;
15626
15627   ALL_TRACEPOINTS (b)
15628     {
15629       struct tracepoint *t = (struct tracepoint *) b;
15630
15631       if (t->number_on_target == num)
15632         return t;
15633     }
15634
15635   return NULL;
15636 }
15637
15638 /* Utility: parse a tracepoint number and look it up in the list.
15639    If STATE is not NULL, use, get_number_or_range_state and ignore ARG.
15640    If the argument is missing, the most recent tracepoint
15641    (tracepoint_count) is returned.  */
15642
15643 struct tracepoint *
15644 get_tracepoint_by_number (char **arg,
15645                           number_or_range_parser *parser)
15646 {
15647   struct breakpoint *t;
15648   int tpnum;
15649   char *instring = arg == NULL ? NULL : *arg;
15650
15651   if (parser != NULL)
15652     {
15653       gdb_assert (!parser->finished ());
15654       tpnum = parser->get_number ();
15655     }
15656   else if (arg == NULL || *arg == NULL || ! **arg)
15657     tpnum = tracepoint_count;
15658   else
15659     tpnum = get_number (arg);
15660
15661   if (tpnum <= 0)
15662     {
15663       if (instring && *instring)
15664         printf_filtered (_("bad tracepoint number at or near '%s'\n"), 
15665                          instring);
15666       else
15667         printf_filtered (_("No previous tracepoint\n"));
15668       return NULL;
15669     }
15670
15671   ALL_TRACEPOINTS (t)
15672     if (t->number == tpnum)
15673     {
15674       return (struct tracepoint *) t;
15675     }
15676
15677   printf_unfiltered ("No tracepoint number %d.\n", tpnum);
15678   return NULL;
15679 }
15680
15681 void
15682 print_recreate_thread (struct breakpoint *b, struct ui_file *fp)
15683 {
15684   if (b->thread != -1)
15685     fprintf_unfiltered (fp, " thread %d", b->thread);
15686
15687   if (b->task != 0)
15688     fprintf_unfiltered (fp, " task %d", b->task);
15689
15690   fprintf_unfiltered (fp, "\n");
15691 }
15692
15693 /* Save information on user settable breakpoints (watchpoints, etc) to
15694    a new script file named FILENAME.  If FILTER is non-NULL, call it
15695    on each breakpoint and only include the ones for which it returns
15696    non-zero.  */
15697
15698 static void
15699 save_breakpoints (char *filename, int from_tty,
15700                   int (*filter) (const struct breakpoint *))
15701 {
15702   struct breakpoint *tp;
15703   int any = 0;
15704   struct cleanup *cleanup;
15705   struct ui_file *fp;
15706   int extra_trace_bits = 0;
15707
15708   if (filename == 0 || *filename == 0)
15709     error (_("Argument required (file name in which to save)"));
15710
15711   /* See if we have anything to save.  */
15712   ALL_BREAKPOINTS (tp)
15713   {
15714     /* Skip internal and momentary breakpoints.  */
15715     if (!user_breakpoint_p (tp))
15716       continue;
15717
15718     /* If we have a filter, only save the breakpoints it accepts.  */
15719     if (filter && !filter (tp))
15720       continue;
15721
15722     any = 1;
15723
15724     if (is_tracepoint (tp))
15725       {
15726         extra_trace_bits = 1;
15727
15728         /* We can stop searching.  */
15729         break;
15730       }
15731   }
15732
15733   if (!any)
15734     {
15735       warning (_("Nothing to save."));
15736       return;
15737     }
15738
15739   filename = tilde_expand (filename);
15740   cleanup = make_cleanup (xfree, filename);
15741   fp = gdb_fopen (filename, "w");
15742   if (!fp)
15743     error (_("Unable to open file '%s' for saving (%s)"),
15744            filename, safe_strerror (errno));
15745   make_cleanup_ui_file_delete (fp);
15746
15747   if (extra_trace_bits)
15748     save_trace_state_variables (fp);
15749
15750   ALL_BREAKPOINTS (tp)
15751   {
15752     /* Skip internal and momentary breakpoints.  */
15753     if (!user_breakpoint_p (tp))
15754       continue;
15755
15756     /* If we have a filter, only save the breakpoints it accepts.  */
15757     if (filter && !filter (tp))
15758       continue;
15759
15760     tp->ops->print_recreate (tp, fp);
15761
15762     /* Note, we can't rely on tp->number for anything, as we can't
15763        assume the recreated breakpoint numbers will match.  Use $bpnum
15764        instead.  */
15765
15766     if (tp->cond_string)
15767       fprintf_unfiltered (fp, "  condition $bpnum %s\n", tp->cond_string);
15768
15769     if (tp->ignore_count)
15770       fprintf_unfiltered (fp, "  ignore $bpnum %d\n", tp->ignore_count);
15771
15772     if (tp->type != bp_dprintf && tp->commands)
15773       {
15774         fprintf_unfiltered (fp, "  commands\n");
15775         
15776         ui_out_redirect (current_uiout, fp);
15777         TRY
15778           {
15779             print_command_lines (current_uiout, tp->commands->commands, 2);
15780           }
15781         CATCH (ex, RETURN_MASK_ALL)
15782           {
15783             ui_out_redirect (current_uiout, NULL);
15784             throw_exception (ex);
15785           }
15786         END_CATCH
15787
15788         ui_out_redirect (current_uiout, NULL);
15789         fprintf_unfiltered (fp, "  end\n");
15790       }
15791
15792     if (tp->enable_state == bp_disabled)
15793       fprintf_unfiltered (fp, "disable $bpnum\n");
15794
15795     /* If this is a multi-location breakpoint, check if the locations
15796        should be individually disabled.  Watchpoint locations are
15797        special, and not user visible.  */
15798     if (!is_watchpoint (tp) && tp->loc && tp->loc->next)
15799       {
15800         struct bp_location *loc;
15801         int n = 1;
15802
15803         for (loc = tp->loc; loc != NULL; loc = loc->next, n++)
15804           if (!loc->enabled)
15805             fprintf_unfiltered (fp, "disable $bpnum.%d\n", n);
15806       }
15807   }
15808
15809   if (extra_trace_bits && *default_collect)
15810     fprintf_unfiltered (fp, "set default-collect %s\n", default_collect);
15811
15812   if (from_tty)
15813     printf_filtered (_("Saved to file '%s'.\n"), filename);
15814   do_cleanups (cleanup);
15815 }
15816
15817 /* The `save breakpoints' command.  */
15818
15819 static void
15820 save_breakpoints_command (char *args, int from_tty)
15821 {
15822   save_breakpoints (args, from_tty, NULL);
15823 }
15824
15825 /* The `save tracepoints' command.  */
15826
15827 static void
15828 save_tracepoints_command (char *args, int from_tty)
15829 {
15830   save_breakpoints (args, from_tty, is_tracepoint);
15831 }
15832
15833 /* Create a vector of all tracepoints.  */
15834
15835 VEC(breakpoint_p) *
15836 all_tracepoints (void)
15837 {
15838   VEC(breakpoint_p) *tp_vec = 0;
15839   struct breakpoint *tp;
15840
15841   ALL_TRACEPOINTS (tp)
15842   {
15843     VEC_safe_push (breakpoint_p, tp_vec, tp);
15844   }
15845
15846   return tp_vec;
15847 }
15848
15849 \f
15850 /* This help string is used to consolidate all the help string for specifying
15851    locations used by several commands.  */
15852
15853 #define LOCATION_HELP_STRING \
15854 "Linespecs are colon-separated lists of location parameters, such as\n\
15855 source filename, function name, label name, and line number.\n\
15856 Example: To specify the start of a label named \"the_top\" in the\n\
15857 function \"fact\" in the file \"factorial.c\", use\n\
15858 \"factorial.c:fact:the_top\".\n\
15859 \n\
15860 Address locations begin with \"*\" and specify an exact address in the\n\
15861 program.  Example: To specify the fourth byte past the start function\n\
15862 \"main\", use \"*main + 4\".\n\
15863 \n\
15864 Explicit locations are similar to linespecs but use an option/argument\n\
15865 syntax to specify location parameters.\n\
15866 Example: To specify the start of the label named \"the_top\" in the\n\
15867 function \"fact\" in the file \"factorial.c\", use \"-source factorial.c\n\
15868 -function fact -label the_top\".\n"
15869
15870 /* This help string is used for the break, hbreak, tbreak and thbreak
15871    commands.  It is defined as a macro to prevent duplication.
15872    COMMAND should be a string constant containing the name of the
15873    command.  */
15874
15875 #define BREAK_ARGS_HELP(command) \
15876 command" [PROBE_MODIFIER] [LOCATION] [thread THREADNUM] [if CONDITION]\n\
15877 PROBE_MODIFIER shall be present if the command is to be placed in a\n\
15878 probe point.  Accepted values are `-probe' (for a generic, automatically\n\
15879 guessed probe type), `-probe-stap' (for a SystemTap probe) or \n\
15880 `-probe-dtrace' (for a DTrace probe).\n\
15881 LOCATION may be a linespec, address, or explicit location as described\n\
15882 below.\n\
15883 \n\
15884 With no LOCATION, uses current execution address of the selected\n\
15885 stack frame.  This is useful for breaking on return to a stack frame.\n\
15886 \n\
15887 THREADNUM is the number from \"info threads\".\n\
15888 CONDITION is a boolean expression.\n\
15889 \n" LOCATION_HELP_STRING "\n\
15890 Multiple breakpoints at one place are permitted, and useful if their\n\
15891 conditions are different.\n\
15892 \n\
15893 Do \"help breakpoints\" for info on other commands dealing with breakpoints."
15894
15895 /* List of subcommands for "catch".  */
15896 static struct cmd_list_element *catch_cmdlist;
15897
15898 /* List of subcommands for "tcatch".  */
15899 static struct cmd_list_element *tcatch_cmdlist;
15900
15901 void
15902 add_catch_command (char *name, char *docstring,
15903                    cmd_sfunc_ftype *sfunc,
15904                    completer_ftype *completer,
15905                    void *user_data_catch,
15906                    void *user_data_tcatch)
15907 {
15908   struct cmd_list_element *command;
15909
15910   command = add_cmd (name, class_breakpoint, NULL, docstring,
15911                      &catch_cmdlist);
15912   set_cmd_sfunc (command, sfunc);
15913   set_cmd_context (command, user_data_catch);
15914   set_cmd_completer (command, completer);
15915
15916   command = add_cmd (name, class_breakpoint, NULL, docstring,
15917                      &tcatch_cmdlist);
15918   set_cmd_sfunc (command, sfunc);
15919   set_cmd_context (command, user_data_tcatch);
15920   set_cmd_completer (command, completer);
15921 }
15922
15923 static void
15924 save_command (char *arg, int from_tty)
15925 {
15926   printf_unfiltered (_("\"save\" must be followed by "
15927                        "the name of a save subcommand.\n"));
15928   help_list (save_cmdlist, "save ", all_commands, gdb_stdout);
15929 }
15930
15931 struct breakpoint *
15932 iterate_over_breakpoints (int (*callback) (struct breakpoint *, void *),
15933                           void *data)
15934 {
15935   struct breakpoint *b, *b_tmp;
15936
15937   ALL_BREAKPOINTS_SAFE (b, b_tmp)
15938     {
15939       if ((*callback) (b, data))
15940         return b;
15941     }
15942
15943   return NULL;
15944 }
15945
15946 /* Zero if any of the breakpoint's locations could be a location where
15947    functions have been inlined, nonzero otherwise.  */
15948
15949 static int
15950 is_non_inline_function (struct breakpoint *b)
15951 {
15952   /* The shared library event breakpoint is set on the address of a
15953      non-inline function.  */
15954   if (b->type == bp_shlib_event)
15955     return 1;
15956
15957   return 0;
15958 }
15959
15960 /* Nonzero if the specified PC cannot be a location where functions
15961    have been inlined.  */
15962
15963 int
15964 pc_at_non_inline_function (struct address_space *aspace, CORE_ADDR pc,
15965                            const struct target_waitstatus *ws)
15966 {
15967   struct breakpoint *b;
15968   struct bp_location *bl;
15969
15970   ALL_BREAKPOINTS (b)
15971     {
15972       if (!is_non_inline_function (b))
15973         continue;
15974
15975       for (bl = b->loc; bl != NULL; bl = bl->next)
15976         {
15977           if (!bl->shlib_disabled
15978               && bpstat_check_location (bl, aspace, pc, ws))
15979             return 1;
15980         }
15981     }
15982
15983   return 0;
15984 }
15985
15986 /* Remove any references to OBJFILE which is going to be freed.  */
15987
15988 void
15989 breakpoint_free_objfile (struct objfile *objfile)
15990 {
15991   struct bp_location **locp, *loc;
15992
15993   ALL_BP_LOCATIONS (loc, locp)
15994     if (loc->symtab != NULL && SYMTAB_OBJFILE (loc->symtab) == objfile)
15995       loc->symtab = NULL;
15996 }
15997
15998 void
15999 initialize_breakpoint_ops (void)
16000 {
16001   static int initialized = 0;
16002
16003   struct breakpoint_ops *ops;
16004
16005   if (initialized)
16006     return;
16007   initialized = 1;
16008
16009   /* The breakpoint_ops structure to be inherit by all kinds of
16010      breakpoints (real breakpoints, i.e., user "break" breakpoints,
16011      internal and momentary breakpoints, etc.).  */
16012   ops = &bkpt_base_breakpoint_ops;
16013   *ops = base_breakpoint_ops;
16014   ops->re_set = bkpt_re_set;
16015   ops->insert_location = bkpt_insert_location;
16016   ops->remove_location = bkpt_remove_location;
16017   ops->breakpoint_hit = bkpt_breakpoint_hit;
16018   ops->create_sals_from_location = bkpt_create_sals_from_location;
16019   ops->create_breakpoints_sal = bkpt_create_breakpoints_sal;
16020   ops->decode_location = bkpt_decode_location;
16021
16022   /* The breakpoint_ops structure to be used in regular breakpoints.  */
16023   ops = &bkpt_breakpoint_ops;
16024   *ops = bkpt_base_breakpoint_ops;
16025   ops->re_set = bkpt_re_set;
16026   ops->resources_needed = bkpt_resources_needed;
16027   ops->print_it = bkpt_print_it;
16028   ops->print_mention = bkpt_print_mention;
16029   ops->print_recreate = bkpt_print_recreate;
16030
16031   /* Ranged breakpoints.  */
16032   ops = &ranged_breakpoint_ops;
16033   *ops = bkpt_breakpoint_ops;
16034   ops->breakpoint_hit = breakpoint_hit_ranged_breakpoint;
16035   ops->resources_needed = resources_needed_ranged_breakpoint;
16036   ops->print_it = print_it_ranged_breakpoint;
16037   ops->print_one = print_one_ranged_breakpoint;
16038   ops->print_one_detail = print_one_detail_ranged_breakpoint;
16039   ops->print_mention = print_mention_ranged_breakpoint;
16040   ops->print_recreate = print_recreate_ranged_breakpoint;
16041
16042   /* Internal breakpoints.  */
16043   ops = &internal_breakpoint_ops;
16044   *ops = bkpt_base_breakpoint_ops;
16045   ops->re_set = internal_bkpt_re_set;
16046   ops->check_status = internal_bkpt_check_status;
16047   ops->print_it = internal_bkpt_print_it;
16048   ops->print_mention = internal_bkpt_print_mention;
16049
16050   /* Momentary breakpoints.  */
16051   ops = &momentary_breakpoint_ops;
16052   *ops = bkpt_base_breakpoint_ops;
16053   ops->re_set = momentary_bkpt_re_set;
16054   ops->check_status = momentary_bkpt_check_status;
16055   ops->print_it = momentary_bkpt_print_it;
16056   ops->print_mention = momentary_bkpt_print_mention;
16057
16058   /* Momentary breakpoints for bp_longjmp and bp_exception.  */
16059   ops = &longjmp_breakpoint_ops;
16060   *ops = momentary_breakpoint_ops;
16061   ops->dtor = longjmp_bkpt_dtor;
16062
16063   /* Probe breakpoints.  */
16064   ops = &bkpt_probe_breakpoint_ops;
16065   *ops = bkpt_breakpoint_ops;
16066   ops->insert_location = bkpt_probe_insert_location;
16067   ops->remove_location = bkpt_probe_remove_location;
16068   ops->create_sals_from_location = bkpt_probe_create_sals_from_location;
16069   ops->decode_location = bkpt_probe_decode_location;
16070
16071   /* Watchpoints.  */
16072   ops = &watchpoint_breakpoint_ops;
16073   *ops = base_breakpoint_ops;
16074   ops->dtor = dtor_watchpoint;
16075   ops->re_set = re_set_watchpoint;
16076   ops->insert_location = insert_watchpoint;
16077   ops->remove_location = remove_watchpoint;
16078   ops->breakpoint_hit = breakpoint_hit_watchpoint;
16079   ops->check_status = check_status_watchpoint;
16080   ops->resources_needed = resources_needed_watchpoint;
16081   ops->works_in_software_mode = works_in_software_mode_watchpoint;
16082   ops->print_it = print_it_watchpoint;
16083   ops->print_mention = print_mention_watchpoint;
16084   ops->print_recreate = print_recreate_watchpoint;
16085   ops->explains_signal = explains_signal_watchpoint;
16086
16087   /* Masked watchpoints.  */
16088   ops = &masked_watchpoint_breakpoint_ops;
16089   *ops = watchpoint_breakpoint_ops;
16090   ops->insert_location = insert_masked_watchpoint;
16091   ops->remove_location = remove_masked_watchpoint;
16092   ops->resources_needed = resources_needed_masked_watchpoint;
16093   ops->works_in_software_mode = works_in_software_mode_masked_watchpoint;
16094   ops->print_it = print_it_masked_watchpoint;
16095   ops->print_one_detail = print_one_detail_masked_watchpoint;
16096   ops->print_mention = print_mention_masked_watchpoint;
16097   ops->print_recreate = print_recreate_masked_watchpoint;
16098
16099   /* Tracepoints.  */
16100   ops = &tracepoint_breakpoint_ops;
16101   *ops = base_breakpoint_ops;
16102   ops->re_set = tracepoint_re_set;
16103   ops->breakpoint_hit = tracepoint_breakpoint_hit;
16104   ops->print_one_detail = tracepoint_print_one_detail;
16105   ops->print_mention = tracepoint_print_mention;
16106   ops->print_recreate = tracepoint_print_recreate;
16107   ops->create_sals_from_location = tracepoint_create_sals_from_location;
16108   ops->create_breakpoints_sal = tracepoint_create_breakpoints_sal;
16109   ops->decode_location = tracepoint_decode_location;
16110
16111   /* Probe tracepoints.  */
16112   ops = &tracepoint_probe_breakpoint_ops;
16113   *ops = tracepoint_breakpoint_ops;
16114   ops->create_sals_from_location = tracepoint_probe_create_sals_from_location;
16115   ops->decode_location = tracepoint_probe_decode_location;
16116
16117   /* Static tracepoints with marker (`-m').  */
16118   ops = &strace_marker_breakpoint_ops;
16119   *ops = tracepoint_breakpoint_ops;
16120   ops->create_sals_from_location = strace_marker_create_sals_from_location;
16121   ops->create_breakpoints_sal = strace_marker_create_breakpoints_sal;
16122   ops->decode_location = strace_marker_decode_location;
16123
16124   /* Fork catchpoints.  */
16125   ops = &catch_fork_breakpoint_ops;
16126   *ops = base_breakpoint_ops;
16127   ops->insert_location = insert_catch_fork;
16128   ops->remove_location = remove_catch_fork;
16129   ops->breakpoint_hit = breakpoint_hit_catch_fork;
16130   ops->print_it = print_it_catch_fork;
16131   ops->print_one = print_one_catch_fork;
16132   ops->print_mention = print_mention_catch_fork;
16133   ops->print_recreate = print_recreate_catch_fork;
16134
16135   /* Vfork catchpoints.  */
16136   ops = &catch_vfork_breakpoint_ops;
16137   *ops = base_breakpoint_ops;
16138   ops->insert_location = insert_catch_vfork;
16139   ops->remove_location = remove_catch_vfork;
16140   ops->breakpoint_hit = breakpoint_hit_catch_vfork;
16141   ops->print_it = print_it_catch_vfork;
16142   ops->print_one = print_one_catch_vfork;
16143   ops->print_mention = print_mention_catch_vfork;
16144   ops->print_recreate = print_recreate_catch_vfork;
16145
16146   /* Exec catchpoints.  */
16147   ops = &catch_exec_breakpoint_ops;
16148   *ops = base_breakpoint_ops;
16149   ops->dtor = dtor_catch_exec;
16150   ops->insert_location = insert_catch_exec;
16151   ops->remove_location = remove_catch_exec;
16152   ops->breakpoint_hit = breakpoint_hit_catch_exec;
16153   ops->print_it = print_it_catch_exec;
16154   ops->print_one = print_one_catch_exec;
16155   ops->print_mention = print_mention_catch_exec;
16156   ops->print_recreate = print_recreate_catch_exec;
16157
16158   /* Solib-related catchpoints.  */
16159   ops = &catch_solib_breakpoint_ops;
16160   *ops = base_breakpoint_ops;
16161   ops->dtor = dtor_catch_solib;
16162   ops->insert_location = insert_catch_solib;
16163   ops->remove_location = remove_catch_solib;
16164   ops->breakpoint_hit = breakpoint_hit_catch_solib;
16165   ops->check_status = check_status_catch_solib;
16166   ops->print_it = print_it_catch_solib;
16167   ops->print_one = print_one_catch_solib;
16168   ops->print_mention = print_mention_catch_solib;
16169   ops->print_recreate = print_recreate_catch_solib;
16170
16171   ops = &dprintf_breakpoint_ops;
16172   *ops = bkpt_base_breakpoint_ops;
16173   ops->re_set = dprintf_re_set;
16174   ops->resources_needed = bkpt_resources_needed;
16175   ops->print_it = bkpt_print_it;
16176   ops->print_mention = bkpt_print_mention;
16177   ops->print_recreate = dprintf_print_recreate;
16178   ops->after_condition_true = dprintf_after_condition_true;
16179   ops->breakpoint_hit = dprintf_breakpoint_hit;
16180 }
16181
16182 /* Chain containing all defined "enable breakpoint" subcommands.  */
16183
16184 static struct cmd_list_element *enablebreaklist = NULL;
16185
16186 void
16187 _initialize_breakpoint (void)
16188 {
16189   struct cmd_list_element *c;
16190
16191   initialize_breakpoint_ops ();
16192
16193   observer_attach_solib_unloaded (disable_breakpoints_in_unloaded_shlib);
16194   observer_attach_free_objfile (disable_breakpoints_in_freed_objfile);
16195   observer_attach_memory_changed (invalidate_bp_value_on_memory_change);
16196
16197   breakpoint_objfile_key
16198     = register_objfile_data_with_cleanup (NULL, free_breakpoint_probes);
16199
16200   breakpoint_chain = 0;
16201   /* Don't bother to call set_breakpoint_count.  $bpnum isn't useful
16202      before a breakpoint is set.  */
16203   breakpoint_count = 0;
16204
16205   tracepoint_count = 0;
16206
16207   add_com ("ignore", class_breakpoint, ignore_command, _("\
16208 Set ignore-count of breakpoint number N to COUNT.\n\
16209 Usage is `ignore N COUNT'."));
16210
16211   add_com ("commands", class_breakpoint, commands_command, _("\
16212 Set commands to be executed when a breakpoint is hit.\n\
16213 Give breakpoint number as argument after \"commands\".\n\
16214 With no argument, the targeted breakpoint is the last one set.\n\
16215 The commands themselves follow starting on the next line.\n\
16216 Type a line containing \"end\" to indicate the end of them.\n\
16217 Give \"silent\" as the first line to make the breakpoint silent;\n\
16218 then no output is printed when it is hit, except what the commands print."));
16219
16220   c = add_com ("condition", class_breakpoint, condition_command, _("\
16221 Specify breakpoint number N to break only if COND is true.\n\
16222 Usage is `condition N COND', where N is an integer and COND is an\n\
16223 expression to be evaluated whenever breakpoint N is reached."));
16224   set_cmd_completer (c, condition_completer);
16225
16226   c = add_com ("tbreak", class_breakpoint, tbreak_command, _("\
16227 Set a temporary breakpoint.\n\
16228 Like \"break\" except the breakpoint is only temporary,\n\
16229 so it will be deleted when hit.  Equivalent to \"break\" followed\n\
16230 by using \"enable delete\" on the breakpoint number.\n\
16231 \n"
16232 BREAK_ARGS_HELP ("tbreak")));
16233   set_cmd_completer (c, location_completer);
16234
16235   c = add_com ("hbreak", class_breakpoint, hbreak_command, _("\
16236 Set a hardware assisted breakpoint.\n\
16237 Like \"break\" except the breakpoint requires hardware support,\n\
16238 some target hardware may not have this support.\n\
16239 \n"
16240 BREAK_ARGS_HELP ("hbreak")));
16241   set_cmd_completer (c, location_completer);
16242
16243   c = add_com ("thbreak", class_breakpoint, thbreak_command, _("\
16244 Set a temporary hardware assisted breakpoint.\n\
16245 Like \"hbreak\" except the breakpoint is only temporary,\n\
16246 so it will be deleted when hit.\n\
16247 \n"
16248 BREAK_ARGS_HELP ("thbreak")));
16249   set_cmd_completer (c, location_completer);
16250
16251   add_prefix_cmd ("enable", class_breakpoint, enable_command, _("\
16252 Enable some breakpoints.\n\
16253 Give breakpoint numbers (separated by spaces) as arguments.\n\
16254 With no subcommand, breakpoints are enabled until you command otherwise.\n\
16255 This is used to cancel the effect of the \"disable\" command.\n\
16256 With a subcommand you can enable temporarily."),
16257                   &enablelist, "enable ", 1, &cmdlist);
16258
16259   add_com_alias ("en", "enable", class_breakpoint, 1);
16260
16261   add_prefix_cmd ("breakpoints", class_breakpoint, enable_command, _("\
16262 Enable some breakpoints.\n\
16263 Give breakpoint numbers (separated by spaces) as arguments.\n\
16264 This is used to cancel the effect of the \"disable\" command.\n\
16265 May be abbreviated to simply \"enable\".\n"),
16266                    &enablebreaklist, "enable breakpoints ", 1, &enablelist);
16267
16268   add_cmd ("once", no_class, enable_once_command, _("\
16269 Enable breakpoints for one hit.  Give breakpoint numbers.\n\
16270 If a breakpoint is hit while enabled in this fashion, it becomes disabled."),
16271            &enablebreaklist);
16272
16273   add_cmd ("delete", no_class, enable_delete_command, _("\
16274 Enable breakpoints and delete when hit.  Give breakpoint numbers.\n\
16275 If a breakpoint is hit while enabled in this fashion, it is deleted."),
16276            &enablebreaklist);
16277
16278   add_cmd ("count", no_class, enable_count_command, _("\
16279 Enable breakpoints for COUNT hits.  Give count and then breakpoint numbers.\n\
16280 If a breakpoint is hit while enabled in this fashion,\n\
16281 the count is decremented; when it reaches zero, the breakpoint is disabled."),
16282            &enablebreaklist);
16283
16284   add_cmd ("delete", no_class, enable_delete_command, _("\
16285 Enable breakpoints and delete when hit.  Give breakpoint numbers.\n\
16286 If a breakpoint is hit while enabled in this fashion, it is deleted."),
16287            &enablelist);
16288
16289   add_cmd ("once", no_class, enable_once_command, _("\
16290 Enable breakpoints for one hit.  Give breakpoint numbers.\n\
16291 If a breakpoint is hit while enabled in this fashion, it becomes disabled."),
16292            &enablelist);
16293
16294   add_cmd ("count", no_class, enable_count_command, _("\
16295 Enable breakpoints for COUNT hits.  Give count and then breakpoint numbers.\n\
16296 If a breakpoint is hit while enabled in this fashion,\n\
16297 the count is decremented; when it reaches zero, the breakpoint is disabled."),
16298            &enablelist);
16299
16300   add_prefix_cmd ("disable", class_breakpoint, disable_command, _("\
16301 Disable some breakpoints.\n\
16302 Arguments are breakpoint numbers with spaces in between.\n\
16303 To disable all breakpoints, give no argument.\n\
16304 A disabled breakpoint is not forgotten, but has no effect until re-enabled."),
16305                   &disablelist, "disable ", 1, &cmdlist);
16306   add_com_alias ("dis", "disable", class_breakpoint, 1);
16307   add_com_alias ("disa", "disable", class_breakpoint, 1);
16308
16309   add_cmd ("breakpoints", class_alias, disable_command, _("\
16310 Disable some breakpoints.\n\
16311 Arguments are breakpoint numbers with spaces in between.\n\
16312 To disable all breakpoints, give no argument.\n\
16313 A disabled breakpoint is not forgotten, but has no effect until re-enabled.\n\
16314 This command may be abbreviated \"disable\"."),
16315            &disablelist);
16316
16317   add_prefix_cmd ("delete", class_breakpoint, delete_command, _("\
16318 Delete some breakpoints or auto-display expressions.\n\
16319 Arguments are breakpoint numbers with spaces in between.\n\
16320 To delete all breakpoints, give no argument.\n\
16321 \n\
16322 Also a prefix command for deletion of other GDB objects.\n\
16323 The \"unset\" command is also an alias for \"delete\"."),
16324                   &deletelist, "delete ", 1, &cmdlist);
16325   add_com_alias ("d", "delete", class_breakpoint, 1);
16326   add_com_alias ("del", "delete", class_breakpoint, 1);
16327
16328   add_cmd ("breakpoints", class_alias, delete_command, _("\
16329 Delete some breakpoints or auto-display expressions.\n\
16330 Arguments are breakpoint numbers with spaces in between.\n\
16331 To delete all breakpoints, give no argument.\n\
16332 This command may be abbreviated \"delete\"."),
16333            &deletelist);
16334
16335   add_com ("clear", class_breakpoint, clear_command, _("\
16336 Clear breakpoint at specified location.\n\
16337 Argument may be a linespec, explicit, or address location as described below.\n\
16338 \n\
16339 With no argument, clears all breakpoints in the line that the selected frame\n\
16340 is executing in.\n"
16341 "\n" LOCATION_HELP_STRING "\n\
16342 See also the \"delete\" command which clears breakpoints by number."));
16343   add_com_alias ("cl", "clear", class_breakpoint, 1);
16344
16345   c = add_com ("break", class_breakpoint, break_command, _("\
16346 Set breakpoint at specified location.\n"
16347 BREAK_ARGS_HELP ("break")));
16348   set_cmd_completer (c, location_completer);
16349
16350   add_com_alias ("b", "break", class_run, 1);
16351   add_com_alias ("br", "break", class_run, 1);
16352   add_com_alias ("bre", "break", class_run, 1);
16353   add_com_alias ("brea", "break", class_run, 1);
16354
16355   if (dbx_commands)
16356     {
16357       add_abbrev_prefix_cmd ("stop", class_breakpoint, stop_command, _("\
16358 Break in function/address or break at a line in the current file."),
16359                              &stoplist, "stop ", 1, &cmdlist);
16360       add_cmd ("in", class_breakpoint, stopin_command,
16361                _("Break in function or address."), &stoplist);
16362       add_cmd ("at", class_breakpoint, stopat_command,
16363                _("Break at a line in the current file."), &stoplist);
16364       add_com ("status", class_info, breakpoints_info, _("\
16365 Status of user-settable breakpoints, or breakpoint number NUMBER.\n\
16366 The \"Type\" column indicates one of:\n\
16367 \tbreakpoint     - normal breakpoint\n\
16368 \twatchpoint     - watchpoint\n\
16369 The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
16370 the disposition of the breakpoint after it gets hit.  \"dis\" means that the\n\
16371 breakpoint will be disabled.  The \"Address\" and \"What\" columns indicate the\n\
16372 address and file/line number respectively.\n\
16373 \n\
16374 Convenience variable \"$_\" and default examine address for \"x\"\n\
16375 are set to the address of the last breakpoint listed unless the command\n\
16376 is prefixed with \"server \".\n\n\
16377 Convenience variable \"$bpnum\" contains the number of the last\n\
16378 breakpoint set."));
16379     }
16380
16381   add_info ("breakpoints", breakpoints_info, _("\
16382 Status of specified breakpoints (all user-settable breakpoints if no argument).\n\
16383 The \"Type\" column indicates one of:\n\
16384 \tbreakpoint     - normal breakpoint\n\
16385 \twatchpoint     - watchpoint\n\
16386 The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
16387 the disposition of the breakpoint after it gets hit.  \"dis\" means that the\n\
16388 breakpoint will be disabled.  The \"Address\" and \"What\" columns indicate the\n\
16389 address and file/line number respectively.\n\
16390 \n\
16391 Convenience variable \"$_\" and default examine address for \"x\"\n\
16392 are set to the address of the last breakpoint listed unless the command\n\
16393 is prefixed with \"server \".\n\n\
16394 Convenience variable \"$bpnum\" contains the number of the last\n\
16395 breakpoint set."));
16396
16397   add_info_alias ("b", "breakpoints", 1);
16398
16399   add_cmd ("breakpoints", class_maintenance, maintenance_info_breakpoints, _("\
16400 Status of all breakpoints, or breakpoint number NUMBER.\n\
16401 The \"Type\" column indicates one of:\n\
16402 \tbreakpoint     - normal breakpoint\n\
16403 \twatchpoint     - watchpoint\n\
16404 \tlongjmp        - internal breakpoint used to step through longjmp()\n\
16405 \tlongjmp resume - internal breakpoint at the target of longjmp()\n\
16406 \tuntil          - internal breakpoint used by the \"until\" command\n\
16407 \tfinish         - internal breakpoint used by the \"finish\" command\n\
16408 The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
16409 the disposition of the breakpoint after it gets hit.  \"dis\" means that the\n\
16410 breakpoint will be disabled.  The \"Address\" and \"What\" columns indicate the\n\
16411 address and file/line number respectively.\n\
16412 \n\
16413 Convenience variable \"$_\" and default examine address for \"x\"\n\
16414 are set to the address of the last breakpoint listed unless the command\n\
16415 is prefixed with \"server \".\n\n\
16416 Convenience variable \"$bpnum\" contains the number of the last\n\
16417 breakpoint set."),
16418            &maintenanceinfolist);
16419
16420   add_prefix_cmd ("catch", class_breakpoint, catch_command, _("\
16421 Set catchpoints to catch events."),
16422                   &catch_cmdlist, "catch ",
16423                   0/*allow-unknown*/, &cmdlist);
16424
16425   add_prefix_cmd ("tcatch", class_breakpoint, tcatch_command, _("\
16426 Set temporary catchpoints to catch events."),
16427                   &tcatch_cmdlist, "tcatch ",
16428                   0/*allow-unknown*/, &cmdlist);
16429
16430   add_catch_command ("fork", _("Catch calls to fork."),
16431                      catch_fork_command_1,
16432                      NULL,
16433                      (void *) (uintptr_t) catch_fork_permanent,
16434                      (void *) (uintptr_t) catch_fork_temporary);
16435   add_catch_command ("vfork", _("Catch calls to vfork."),
16436                      catch_fork_command_1,
16437                      NULL,
16438                      (void *) (uintptr_t) catch_vfork_permanent,
16439                      (void *) (uintptr_t) catch_vfork_temporary);
16440   add_catch_command ("exec", _("Catch calls to exec."),
16441                      catch_exec_command_1,
16442                      NULL,
16443                      CATCH_PERMANENT,
16444                      CATCH_TEMPORARY);
16445   add_catch_command ("load", _("Catch loads of shared libraries.\n\
16446 Usage: catch load [REGEX]\n\
16447 If REGEX is given, only stop for libraries matching the regular expression."),
16448                      catch_load_command_1,
16449                      NULL,
16450                      CATCH_PERMANENT,
16451                      CATCH_TEMPORARY);
16452   add_catch_command ("unload", _("Catch unloads of shared libraries.\n\
16453 Usage: catch unload [REGEX]\n\
16454 If REGEX is given, only stop for libraries matching the regular expression."),
16455                      catch_unload_command_1,
16456                      NULL,
16457                      CATCH_PERMANENT,
16458                      CATCH_TEMPORARY);
16459
16460   c = add_com ("watch", class_breakpoint, watch_command, _("\
16461 Set a watchpoint for an expression.\n\
16462 Usage: watch [-l|-location] EXPRESSION\n\
16463 A watchpoint stops execution of your program whenever the value of\n\
16464 an expression changes.\n\
16465 If -l or -location is given, this evaluates EXPRESSION and watches\n\
16466 the memory to which it refers."));
16467   set_cmd_completer (c, expression_completer);
16468
16469   c = add_com ("rwatch", class_breakpoint, rwatch_command, _("\
16470 Set a read watchpoint for an expression.\n\
16471 Usage: rwatch [-l|-location] EXPRESSION\n\
16472 A watchpoint stops execution of your program whenever the value of\n\
16473 an expression is read.\n\
16474 If -l or -location is given, this evaluates EXPRESSION and watches\n\
16475 the memory to which it refers."));
16476   set_cmd_completer (c, expression_completer);
16477
16478   c = add_com ("awatch", class_breakpoint, awatch_command, _("\
16479 Set a watchpoint for an expression.\n\
16480 Usage: awatch [-l|-location] EXPRESSION\n\
16481 A watchpoint stops execution of your program whenever the value of\n\
16482 an expression is either read or written.\n\
16483 If -l or -location is given, this evaluates EXPRESSION and watches\n\
16484 the memory to which it refers."));
16485   set_cmd_completer (c, expression_completer);
16486
16487   add_info ("watchpoints", watchpoints_info, _("\
16488 Status of specified watchpoints (all watchpoints if no argument)."));
16489
16490   /* XXX: cagney/2005-02-23: This should be a boolean, and should
16491      respond to changes - contrary to the description.  */
16492   add_setshow_zinteger_cmd ("can-use-hw-watchpoints", class_support,
16493                             &can_use_hw_watchpoints, _("\
16494 Set debugger's willingness to use watchpoint hardware."), _("\
16495 Show debugger's willingness to use watchpoint hardware."), _("\
16496 If zero, gdb will not use hardware for new watchpoints, even if\n\
16497 such is available.  (However, any hardware watchpoints that were\n\
16498 created before setting this to nonzero, will continue to use watchpoint\n\
16499 hardware.)"),
16500                             NULL,
16501                             show_can_use_hw_watchpoints,
16502                             &setlist, &showlist);
16503
16504   can_use_hw_watchpoints = 1;
16505
16506   /* Tracepoint manipulation commands.  */
16507
16508   c = add_com ("trace", class_breakpoint, trace_command, _("\
16509 Set a tracepoint at specified location.\n\
16510 \n"
16511 BREAK_ARGS_HELP ("trace") "\n\
16512 Do \"help tracepoints\" for info on other tracepoint commands."));
16513   set_cmd_completer (c, location_completer);
16514
16515   add_com_alias ("tp", "trace", class_alias, 0);
16516   add_com_alias ("tr", "trace", class_alias, 1);
16517   add_com_alias ("tra", "trace", class_alias, 1);
16518   add_com_alias ("trac", "trace", class_alias, 1);
16519
16520   c = add_com ("ftrace", class_breakpoint, ftrace_command, _("\
16521 Set a fast tracepoint at specified location.\n\
16522 \n"
16523 BREAK_ARGS_HELP ("ftrace") "\n\
16524 Do \"help tracepoints\" for info on other tracepoint commands."));
16525   set_cmd_completer (c, location_completer);
16526
16527   c = add_com ("strace", class_breakpoint, strace_command, _("\
16528 Set a static tracepoint at location or marker.\n\
16529 \n\
16530 strace [LOCATION] [if CONDITION]\n\
16531 LOCATION may be a linespec, explicit, or address location (described below) \n\
16532 or -m MARKER_ID.\n\n\
16533 If a marker id is specified, probe the marker with that name.  With\n\
16534 no LOCATION, uses current execution address of the selected stack frame.\n\
16535 Static tracepoints accept an extra collect action -- ``collect $_sdata''.\n\
16536 This collects arbitrary user data passed in the probe point call to the\n\
16537 tracing library.  You can inspect it when analyzing the trace buffer,\n\
16538 by printing the $_sdata variable like any other convenience variable.\n\
16539 \n\
16540 CONDITION is a boolean expression.\n\
16541 \n" LOCATION_HELP_STRING "\n\
16542 Multiple tracepoints at one place are permitted, and useful if their\n\
16543 conditions are different.\n\
16544 \n\
16545 Do \"help breakpoints\" for info on other commands dealing with breakpoints.\n\
16546 Do \"help tracepoints\" for info on other tracepoint commands."));
16547   set_cmd_completer (c, location_completer);
16548
16549   add_info ("tracepoints", tracepoints_info, _("\
16550 Status of specified tracepoints (all tracepoints if no argument).\n\
16551 Convenience variable \"$tpnum\" contains the number of the\n\
16552 last tracepoint set."));
16553
16554   add_info_alias ("tp", "tracepoints", 1);
16555
16556   add_cmd ("tracepoints", class_trace, delete_trace_command, _("\
16557 Delete specified tracepoints.\n\
16558 Arguments are tracepoint numbers, separated by spaces.\n\
16559 No argument means delete all tracepoints."),
16560            &deletelist);
16561   add_alias_cmd ("tr", "tracepoints", class_trace, 1, &deletelist);
16562
16563   c = add_cmd ("tracepoints", class_trace, disable_trace_command, _("\
16564 Disable specified tracepoints.\n\
16565 Arguments are tracepoint numbers, separated by spaces.\n\
16566 No argument means disable all tracepoints."),
16567            &disablelist);
16568   deprecate_cmd (c, "disable");
16569
16570   c = add_cmd ("tracepoints", class_trace, enable_trace_command, _("\
16571 Enable specified tracepoints.\n\
16572 Arguments are tracepoint numbers, separated by spaces.\n\
16573 No argument means enable all tracepoints."),
16574            &enablelist);
16575   deprecate_cmd (c, "enable");
16576
16577   add_com ("passcount", class_trace, trace_pass_command, _("\
16578 Set the passcount for a tracepoint.\n\
16579 The trace will end when the tracepoint has been passed 'count' times.\n\
16580 Usage: passcount COUNT TPNUM, where TPNUM may also be \"all\";\n\
16581 if TPNUM is omitted, passcount refers to the last tracepoint defined."));
16582
16583   add_prefix_cmd ("save", class_breakpoint, save_command,
16584                   _("Save breakpoint definitions as a script."),
16585                   &save_cmdlist, "save ",
16586                   0/*allow-unknown*/, &cmdlist);
16587
16588   c = add_cmd ("breakpoints", class_breakpoint, save_breakpoints_command, _("\
16589 Save current breakpoint definitions as a script.\n\
16590 This includes all types of breakpoints (breakpoints, watchpoints,\n\
16591 catchpoints, tracepoints).  Use the 'source' command in another debug\n\
16592 session to restore them."),
16593                &save_cmdlist);
16594   set_cmd_completer (c, filename_completer);
16595
16596   c = add_cmd ("tracepoints", class_trace, save_tracepoints_command, _("\
16597 Save current tracepoint definitions as a script.\n\
16598 Use the 'source' command in another debug session to restore them."),
16599                &save_cmdlist);
16600   set_cmd_completer (c, filename_completer);
16601
16602   c = add_com_alias ("save-tracepoints", "save tracepoints", class_trace, 0);
16603   deprecate_cmd (c, "save tracepoints");
16604
16605   add_prefix_cmd ("breakpoint", class_maintenance, set_breakpoint_cmd, _("\
16606 Breakpoint specific settings\n\
16607 Configure various breakpoint-specific variables such as\n\
16608 pending breakpoint behavior"),
16609                   &breakpoint_set_cmdlist, "set breakpoint ",
16610                   0/*allow-unknown*/, &setlist);
16611   add_prefix_cmd ("breakpoint", class_maintenance, show_breakpoint_cmd, _("\
16612 Breakpoint specific settings\n\
16613 Configure various breakpoint-specific variables such as\n\
16614 pending breakpoint behavior"),
16615                   &breakpoint_show_cmdlist, "show breakpoint ",
16616                   0/*allow-unknown*/, &showlist);
16617
16618   add_setshow_auto_boolean_cmd ("pending", no_class,
16619                                 &pending_break_support, _("\
16620 Set debugger's behavior regarding pending breakpoints."), _("\
16621 Show debugger's behavior regarding pending breakpoints."), _("\
16622 If on, an unrecognized breakpoint location will cause gdb to create a\n\
16623 pending breakpoint.  If off, an unrecognized breakpoint location results in\n\
16624 an error.  If auto, an unrecognized breakpoint location results in a\n\
16625 user-query to see if a pending breakpoint should be created."),
16626                                 NULL,
16627                                 show_pending_break_support,
16628                                 &breakpoint_set_cmdlist,
16629                                 &breakpoint_show_cmdlist);
16630
16631   pending_break_support = AUTO_BOOLEAN_AUTO;
16632
16633   add_setshow_boolean_cmd ("auto-hw", no_class,
16634                            &automatic_hardware_breakpoints, _("\
16635 Set automatic usage of hardware breakpoints."), _("\
16636 Show automatic usage of hardware breakpoints."), _("\
16637 If set, the debugger will automatically use hardware breakpoints for\n\
16638 breakpoints set with \"break\" but falling in read-only memory.  If not set,\n\
16639 a warning will be emitted for such breakpoints."),
16640                            NULL,
16641                            show_automatic_hardware_breakpoints,
16642                            &breakpoint_set_cmdlist,
16643                            &breakpoint_show_cmdlist);
16644
16645   add_setshow_boolean_cmd ("always-inserted", class_support,
16646                            &always_inserted_mode, _("\
16647 Set mode for inserting breakpoints."), _("\
16648 Show mode for inserting breakpoints."), _("\
16649 When this mode is on, breakpoints are inserted immediately as soon as\n\
16650 they're created, kept inserted even when execution stops, and removed\n\
16651 only when the user deletes them.  When this mode is off (the default),\n\
16652 breakpoints are inserted only when execution continues, and removed\n\
16653 when execution stops."),
16654                                 NULL,
16655                                 &show_always_inserted_mode,
16656                                 &breakpoint_set_cmdlist,
16657                                 &breakpoint_show_cmdlist);
16658
16659   add_setshow_enum_cmd ("condition-evaluation", class_breakpoint,
16660                         condition_evaluation_enums,
16661                         &condition_evaluation_mode_1, _("\
16662 Set mode of breakpoint condition evaluation."), _("\
16663 Show mode of breakpoint condition evaluation."), _("\
16664 When this is set to \"host\", breakpoint conditions will be\n\
16665 evaluated on the host's side by GDB.  When it is set to \"target\",\n\
16666 breakpoint conditions will be downloaded to the target (if the target\n\
16667 supports such feature) and conditions will be evaluated on the target's side.\n\
16668 If this is set to \"auto\" (default), this will be automatically set to\n\
16669 \"target\" if it supports condition evaluation, otherwise it will\n\
16670 be set to \"gdb\""),
16671                            &set_condition_evaluation_mode,
16672                            &show_condition_evaluation_mode,
16673                            &breakpoint_set_cmdlist,
16674                            &breakpoint_show_cmdlist);
16675
16676   add_com ("break-range", class_breakpoint, break_range_command, _("\
16677 Set a breakpoint for an address range.\n\
16678 break-range START-LOCATION, END-LOCATION\n\
16679 where START-LOCATION and END-LOCATION can be one of the following:\n\
16680   LINENUM, for that line in the current file,\n\
16681   FILE:LINENUM, for that line in that file,\n\
16682   +OFFSET, for that number of lines after the current line\n\
16683            or the start of the range\n\
16684   FUNCTION, for the first line in that function,\n\
16685   FILE:FUNCTION, to distinguish among like-named static functions.\n\
16686   *ADDRESS, for the instruction at that address.\n\
16687 \n\
16688 The breakpoint will stop execution of the inferior whenever it executes\n\
16689 an instruction at any address within the [START-LOCATION, END-LOCATION]\n\
16690 range (including START-LOCATION and END-LOCATION)."));
16691
16692   c = add_com ("dprintf", class_breakpoint, dprintf_command, _("\
16693 Set a dynamic printf at specified location.\n\
16694 dprintf location,format string,arg1,arg2,...\n\
16695 location may be a linespec, explicit, or address location.\n"
16696 "\n" LOCATION_HELP_STRING));
16697   set_cmd_completer (c, location_completer);
16698
16699   add_setshow_enum_cmd ("dprintf-style", class_support,
16700                         dprintf_style_enums, &dprintf_style, _("\
16701 Set the style of usage for dynamic printf."), _("\
16702 Show the style of usage for dynamic printf."), _("\
16703 This setting chooses how GDB will do a dynamic printf.\n\
16704 If the value is \"gdb\", then the printing is done by GDB to its own\n\
16705 console, as with the \"printf\" command.\n\
16706 If the value is \"call\", the print is done by calling a function in your\n\
16707 program; by default printf(), but you can choose a different function or\n\
16708 output stream by setting dprintf-function and dprintf-channel."),
16709                         update_dprintf_commands, NULL,
16710                         &setlist, &showlist);
16711
16712   dprintf_function = xstrdup ("printf");
16713   add_setshow_string_cmd ("dprintf-function", class_support,
16714                           &dprintf_function, _("\
16715 Set the function to use for dynamic printf"), _("\
16716 Show the function to use for dynamic printf"), NULL,
16717                           update_dprintf_commands, NULL,
16718                           &setlist, &showlist);
16719
16720   dprintf_channel = xstrdup ("");
16721   add_setshow_string_cmd ("dprintf-channel", class_support,
16722                           &dprintf_channel, _("\
16723 Set the channel to use for dynamic printf"), _("\
16724 Show the channel to use for dynamic printf"), NULL,
16725                           update_dprintf_commands, NULL,
16726                           &setlist, &showlist);
16727
16728   add_setshow_boolean_cmd ("disconnected-dprintf", no_class,
16729                            &disconnected_dprintf, _("\
16730 Set whether dprintf continues after GDB disconnects."), _("\
16731 Show whether dprintf continues after GDB disconnects."), _("\
16732 Use this to let dprintf commands continue to hit and produce output\n\
16733 even if GDB disconnects or detaches from the target."),
16734                            NULL,
16735                            NULL,
16736                            &setlist, &showlist);
16737
16738   add_com ("agent-printf", class_vars, agent_printf_command, _("\
16739 agent-printf \"printf format string\", arg1, arg2, arg3, ..., argn\n\
16740 (target agent only) This is useful for formatted output in user-defined commands."));
16741
16742   automatic_hardware_breakpoints = 1;
16743
16744   observer_attach_about_to_proceed (breakpoint_about_to_proceed);
16745   observer_attach_thread_exit (remove_threaded_breakpoints);
16746 }