start change to progspace independence
[platform/upstream/binutils.git] / gdb / breakpoint.c
1 /* Everything about breakpoints, for GDB.
2
3    Copyright (C) 1986-2014 Free Software Foundation, Inc.
4
5    This file is part of GDB.
6
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 3 of the License, or
10    (at your option) any later version.
11
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16
17    You should have received a copy of the GNU General Public License
18    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
19
20 #include "defs.h"
21 #include "arch-utils.h"
22 #include <ctype.h>
23 #include "hashtab.h"
24 #include "symtab.h"
25 #include "frame.h"
26 #include "breakpoint.h"
27 #include "tracepoint.h"
28 #include "gdbtypes.h"
29 #include "expression.h"
30 #include "gdbcore.h"
31 #include "gdbcmd.h"
32 #include "value.h"
33 #include "command.h"
34 #include "inferior.h"
35 #include "gdbthread.h"
36 #include "target.h"
37 #include "language.h"
38 #include <string.h>
39 #include "gdb-demangle.h"
40 #include "filenames.h"
41 #include "annotate.h"
42 #include "symfile.h"
43 #include "objfiles.h"
44 #include "source.h"
45 #include "linespec.h"
46 #include "completer.h"
47 #include "gdb.h"
48 #include "ui-out.h"
49 #include "cli/cli-script.h"
50 #include "gdb_assert.h"
51 #include "block.h"
52 #include "solib.h"
53 #include "solist.h"
54 #include "observer.h"
55 #include "exceptions.h"
56 #include "memattr.h"
57 #include "ada-lang.h"
58 #include "top.h"
59 #include "valprint.h"
60 #include "jit.h"
61 #include "xml-syscall.h"
62 #include "parser-defs.h"
63 #include "gdb_regex.h"
64 #include "probe.h"
65 #include "cli/cli-utils.h"
66 #include "continuations.h"
67 #include "stack.h"
68 #include "skip.h"
69 #include "ax-gdb.h"
70 #include "dummy-frame.h"
71
72 #include "format.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
84 /* Enums for exception-handling support.  */
85 enum exception_event_kind
86 {
87   EX_EVENT_THROW,
88   EX_EVENT_RETHROW,
89   EX_EVENT_CATCH
90 };
91
92 /* Prototypes for local functions.  */
93
94 static void enable_delete_command (char *, int);
95
96 static void enable_once_command (char *, int);
97
98 static void enable_count_command (char *, int);
99
100 static void disable_command (char *, int);
101
102 static void enable_command (char *, int);
103
104 static void map_breakpoint_numbers (char *, void (*) (struct breakpoint *,
105                                                       void *),
106                                     void *);
107
108 static void ignore_command (char *, int);
109
110 static int breakpoint_re_set_one (void *);
111
112 static void breakpoint_re_set_default (struct breakpoint *);
113
114 static void create_sals_from_address_default (char **,
115                                               struct linespec_result *,
116                                               enum bptype, char *,
117                                               char **);
118
119 static void create_breakpoints_sal_default (struct gdbarch *,
120                                             struct linespec_result *,
121                                             char *, char *, enum bptype,
122                                             enum bpdisp, int, int,
123                                             int,
124                                             const struct breakpoint_ops *,
125                                             int, int, int, unsigned);
126
127 static void decode_linespec_default (struct breakpoint *, char **,
128                                      struct symtabs_and_lines *);
129
130 static void clear_command (char *, int);
131
132 static void catch_command (char *, int);
133
134 static int can_use_hardware_watchpoint (struct value *);
135
136 static void break_command_1 (char *, int, int);
137
138 static void mention (struct breakpoint *);
139
140 static struct breakpoint *set_raw_breakpoint_without_location (struct gdbarch *,
141                                                                enum bptype,
142                                                                const struct breakpoint_ops *);
143 static struct bp_location *add_location_to_breakpoint (struct breakpoint *,
144                                                        const struct symtab_and_line *);
145
146 /* This function is used in gdbtk sources and thus can not be made
147    static.  */
148 struct breakpoint *set_raw_breakpoint (struct gdbarch *gdbarch,
149                                        struct symtab_and_line,
150                                        enum bptype,
151                                        const struct breakpoint_ops *);
152
153 static struct breakpoint *
154   momentary_breakpoint_from_master (struct breakpoint *orig,
155                                     enum bptype type,
156                                     const struct breakpoint_ops *ops);
157
158 static void breakpoint_adjustment_warning (CORE_ADDR, CORE_ADDR, int, int);
159
160 static CORE_ADDR adjust_breakpoint_address (struct gdbarch *gdbarch,
161                                             CORE_ADDR bpaddr,
162                                             enum bptype bptype);
163
164 static void describe_other_breakpoints (struct gdbarch *,
165                                         struct program_space *, CORE_ADDR,
166                                         struct obj_section *, int);
167
168 static int breakpoint_address_match (struct address_space *aspace1,
169                                      CORE_ADDR addr1,
170                                      struct address_space *aspace2,
171                                      CORE_ADDR addr2);
172
173 static int watchpoint_locations_match (struct bp_location *loc1,
174                                        struct bp_location *loc2);
175
176 static int breakpoint_location_address_match (struct bp_location *bl,
177                                               struct address_space *aspace,
178                                               CORE_ADDR addr);
179
180 static void breakpoints_info (char *, int);
181
182 static void watchpoints_info (char *, int);
183
184 static int breakpoint_1 (char *, int, 
185                          int (*) (const struct breakpoint *));
186
187 static int breakpoint_cond_eval (void *);
188
189 static void cleanup_executing_breakpoints (void *);
190
191 static void commands_command (char *, int);
192
193 static void condition_command (char *, int);
194
195 typedef enum
196   {
197     mark_inserted,
198     mark_uninserted
199   }
200 insertion_state_t;
201
202 static int remove_breakpoint (struct bp_location *, insertion_state_t);
203 static int remove_breakpoint_1 (struct bp_location *, insertion_state_t);
204
205 static enum print_stop_action print_bp_stop_message (bpstat bs);
206
207 static int watchpoint_check (void *);
208
209 static void maintenance_info_breakpoints (char *, int);
210
211 static int hw_breakpoint_used_count (void);
212
213 static int hw_watchpoint_use_count (struct breakpoint *);
214
215 static int hw_watchpoint_used_count_others (struct breakpoint *except,
216                                             enum bptype type,
217                                             int *other_type_used);
218
219 static void hbreak_command (char *, int);
220
221 static void thbreak_command (char *, int);
222
223 static void enable_breakpoint_disp (struct breakpoint *, enum bpdisp,
224                                     int count);
225
226 static void stop_command (char *arg, int from_tty);
227
228 static void stopin_command (char *arg, int from_tty);
229
230 static void stopat_command (char *arg, int from_tty);
231
232 static void tcatch_command (char *arg, int from_tty);
233
234 static void detach_single_step_breakpoints (void);
235
236 static int single_step_breakpoint_inserted_here_p (struct address_space *,
237                                                    CORE_ADDR pc);
238
239 static void free_bp_location (struct bp_location *loc);
240 static void incref_bp_location (struct bp_location *loc);
241 static void decref_bp_location (struct bp_location **loc);
242
243 static struct bp_location *allocate_bp_location (struct breakpoint *bpt);
244
245 static void update_global_location_list (int);
246
247 static void update_global_location_list_nothrow (int);
248
249 static int is_hardware_watchpoint (const struct breakpoint *bpt);
250
251 static void insert_breakpoint_locations (void);
252
253 static int syscall_catchpoint_p (struct breakpoint *b);
254
255 static void tracepoints_info (char *, int);
256
257 static void delete_trace_command (char *, int);
258
259 static void enable_trace_command (char *, int);
260
261 static void disable_trace_command (char *, int);
262
263 static void trace_pass_command (char *, int);
264
265 static void set_tracepoint_count (int num);
266
267 static int is_masked_watchpoint (const struct breakpoint *b);
268
269 static struct bp_location **get_first_locp_gte_addr (CORE_ADDR address);
270
271 /* Return 1 if B refers to a static tracepoint set by marker ("-m"), zero
272    otherwise.  */
273
274 static int strace_marker_p (struct breakpoint *b);
275
276 /* The abstract base class all breakpoint_ops structures inherit
277    from.  */
278 struct breakpoint_ops base_breakpoint_ops;
279
280 /* The breakpoint_ops structure to be inherited by all breakpoint_ops
281    that are implemented on top of software or hardware breakpoints
282    (user breakpoints, internal and momentary breakpoints, etc.).  */
283 static struct breakpoint_ops bkpt_base_breakpoint_ops;
284
285 /* Internal breakpoints class type.  */
286 static struct breakpoint_ops internal_breakpoint_ops;
287
288 /* Momentary breakpoints class type.  */
289 static struct breakpoint_ops momentary_breakpoint_ops;
290
291 /* Momentary breakpoints for bp_longjmp and bp_exception class type.  */
292 static struct breakpoint_ops longjmp_breakpoint_ops;
293
294 /* The breakpoint_ops structure to be used in regular user created
295    breakpoints.  */
296 struct breakpoint_ops bkpt_breakpoint_ops;
297
298 /* Breakpoints set on probes.  */
299 static struct breakpoint_ops bkpt_probe_breakpoint_ops;
300
301 /* Dynamic printf class type.  */
302 struct breakpoint_ops dprintf_breakpoint_ops;
303
304 /* The style in which to perform a dynamic printf.  This is a user
305    option because different output options have different tradeoffs;
306    if GDB does the printing, there is better error handling if there
307    is a problem with any of the arguments, but using an inferior
308    function lets you have special-purpose printers and sending of
309    output to the same place as compiled-in print functions.  */
310
311 static const char dprintf_style_gdb[] = "gdb";
312 static const char dprintf_style_call[] = "call";
313 static const char dprintf_style_agent[] = "agent";
314 static const char *const dprintf_style_enums[] = {
315   dprintf_style_gdb,
316   dprintf_style_call,
317   dprintf_style_agent,
318   NULL
319 };
320 static const char *dprintf_style = dprintf_style_gdb;
321
322 /* The function to use for dynamic printf if the preferred style is to
323    call into the inferior.  The value is simply a string that is
324    copied into the command, so it can be anything that GDB can
325    evaluate to a callable address, not necessarily a function name.  */
326
327 static char *dprintf_function = "";
328
329 /* The channel to use for dynamic printf if the preferred style is to
330    call into the inferior; if a nonempty string, it will be passed to
331    the call as the first argument, with the format string as the
332    second.  As with the dprintf function, this can be anything that
333    GDB knows how to evaluate, so in addition to common choices like
334    "stderr", this could be an app-specific expression like
335    "mystreams[curlogger]".  */
336
337 static char *dprintf_channel = "";
338
339 /* True if dprintf commands should continue to operate even if GDB
340    has disconnected.  */
341 static int disconnected_dprintf = 1;
342
343 /* A reference-counted struct command_line.  This lets multiple
344    breakpoints share a single command list.  */
345 struct counted_command_line
346 {
347   /* The reference count.  */
348   int refc;
349
350   /* The command list.  */
351   struct command_line *commands;
352 };
353
354 struct command_line *
355 breakpoint_commands (struct breakpoint *b)
356 {
357   return b->commands ? b->commands->commands : NULL;
358 }
359
360 /* Flag indicating that a command has proceeded the inferior past the
361    current breakpoint.  */
362
363 static int breakpoint_proceeded;
364
365 const char *
366 bpdisp_text (enum bpdisp disp)
367 {
368   /* NOTE: the following values are a part of MI protocol and
369      represent values of 'disp' field returned when inferior stops at
370      a breakpoint.  */
371   static const char * const bpdisps[] = {"del", "dstp", "dis", "keep"};
372
373   return bpdisps[(int) disp];
374 }
375
376 /* Prototypes for exported functions.  */
377 /* If FALSE, gdb will not use hardware support for watchpoints, even
378    if such is available.  */
379 static int can_use_hw_watchpoints;
380
381 static void
382 show_can_use_hw_watchpoints (struct ui_file *file, int from_tty,
383                              struct cmd_list_element *c,
384                              const char *value)
385 {
386   fprintf_filtered (file,
387                     _("Debugger's willingness to use "
388                       "watchpoint hardware is %s.\n"),
389                     value);
390 }
391
392 /* If AUTO_BOOLEAN_FALSE, gdb will not attempt to create pending breakpoints.
393    If AUTO_BOOLEAN_TRUE, gdb will automatically create pending breakpoints
394    for unrecognized breakpoint locations.
395    If AUTO_BOOLEAN_AUTO, gdb will query when breakpoints are unrecognized.  */
396 static enum auto_boolean pending_break_support;
397 static void
398 show_pending_break_support (struct ui_file *file, int from_tty,
399                             struct cmd_list_element *c,
400                             const char *value)
401 {
402   fprintf_filtered (file,
403                     _("Debugger's behavior regarding "
404                       "pending breakpoints is %s.\n"),
405                     value);
406 }
407
408 /* If 1, gdb will automatically use hardware breakpoints for breakpoints
409    set with "break" but falling in read-only memory.
410    If 0, gdb will warn about such breakpoints, but won't automatically
411    use hardware breakpoints.  */
412 static int automatic_hardware_breakpoints;
413 static void
414 show_automatic_hardware_breakpoints (struct ui_file *file, int from_tty,
415                                      struct cmd_list_element *c,
416                                      const char *value)
417 {
418   fprintf_filtered (file,
419                     _("Automatic usage of hardware breakpoints is %s.\n"),
420                     value);
421 }
422
423 /* If on, gdb will keep breakpoints inserted even as inferior is
424    stopped, and immediately insert any new breakpoints.  If off, gdb
425    will insert breakpoints into inferior only when resuming it, and
426    will remove breakpoints upon stop.  If auto, GDB will behave as ON
427    if in non-stop mode, and as OFF if all-stop mode.*/
428
429 static enum auto_boolean always_inserted_mode = AUTO_BOOLEAN_AUTO;
430
431 static void
432 show_always_inserted_mode (struct ui_file *file, int from_tty,
433                      struct cmd_list_element *c, const char *value)
434 {
435   if (always_inserted_mode == AUTO_BOOLEAN_AUTO)
436     fprintf_filtered (file,
437                       _("Always inserted breakpoint "
438                         "mode is %s (currently %s).\n"),
439                       value,
440                       breakpoints_always_inserted_mode () ? "on" : "off");
441   else
442     fprintf_filtered (file, _("Always inserted breakpoint mode is %s.\n"),
443                       value);
444 }
445
446 int
447 breakpoints_always_inserted_mode (void)
448 {
449   return (always_inserted_mode == AUTO_BOOLEAN_TRUE
450           || (always_inserted_mode == AUTO_BOOLEAN_AUTO && non_stop));
451 }
452
453 static const char condition_evaluation_both[] = "host or target";
454
455 /* Modes for breakpoint condition evaluation.  */
456 static const char condition_evaluation_auto[] = "auto";
457 static const char condition_evaluation_host[] = "host";
458 static const char condition_evaluation_target[] = "target";
459 static const char *const condition_evaluation_enums[] = {
460   condition_evaluation_auto,
461   condition_evaluation_host,
462   condition_evaluation_target,
463   NULL
464 };
465
466 /* Global that holds the current mode for breakpoint condition evaluation.  */
467 static const char *condition_evaluation_mode_1 = condition_evaluation_auto;
468
469 /* Global that we use to display information to the user (gets its value from
470    condition_evaluation_mode_1.  */
471 static const char *condition_evaluation_mode = condition_evaluation_auto;
472
473 /* Translate a condition evaluation mode MODE into either "host"
474    or "target".  This is used mostly to translate from "auto" to the
475    real setting that is being used.  It returns the translated
476    evaluation mode.  */
477
478 static const char *
479 translate_condition_evaluation_mode (const char *mode)
480 {
481   if (mode == condition_evaluation_auto)
482     {
483       if (target_supports_evaluation_of_breakpoint_conditions ())
484         return condition_evaluation_target;
485       else
486         return condition_evaluation_host;
487     }
488   else
489     return mode;
490 }
491
492 /* Discovers what condition_evaluation_auto translates to.  */
493
494 static const char *
495 breakpoint_condition_evaluation_mode (void)
496 {
497   return translate_condition_evaluation_mode (condition_evaluation_mode);
498 }
499
500 /* Return true if GDB should evaluate breakpoint conditions or false
501    otherwise.  */
502
503 static int
504 gdb_evaluates_breakpoint_condition_p (void)
505 {
506   const char *mode = breakpoint_condition_evaluation_mode ();
507
508   return (mode == condition_evaluation_host);
509 }
510
511 void _initialize_breakpoint (void);
512
513 /* Are we executing breakpoint commands?  */
514 static int executing_breakpoint_commands;
515
516 /* Are overlay event breakpoints enabled? */
517 static int overlay_events_enabled;
518
519 /* See description in breakpoint.h. */
520 int target_exact_watchpoints = 0;
521
522 /* Walk the following statement or block through all breakpoints.
523    ALL_BREAKPOINTS_SAFE does so even if the statement deletes the
524    current breakpoint.  */
525
526 #define ALL_BREAKPOINTS(B)  for (B = breakpoint_chain; B; B = B->next)
527
528 #define ALL_BREAKPOINTS_SAFE(B,TMP)     \
529         for (B = breakpoint_chain;      \
530              B ? (TMP=B->next, 1): 0;   \
531              B = TMP)
532
533 /* Similar iterator for the low-level breakpoints.  SAFE variant is
534    not provided so update_global_location_list must not be called
535    while executing the block of ALL_BP_LOCATIONS.  */
536
537 #define ALL_BP_LOCATIONS(B,BP_TMP)                                      \
538         for (BP_TMP = bp_location;                                      \
539              BP_TMP < bp_location + bp_location_count && (B = *BP_TMP); \
540              BP_TMP++)
541
542 /* Iterates through locations with address ADDRESS for the currently selected
543    program space.  BP_LOCP_TMP points to each object.  BP_LOCP_START points
544    to where the loop should start from.
545    If BP_LOCP_START is a NULL pointer, the macro automatically seeks the
546    appropriate location to start with.  */
547
548 #define ALL_BP_LOCATIONS_AT_ADDR(BP_LOCP_TMP, BP_LOCP_START, ADDRESS)   \
549         for (BP_LOCP_START = BP_LOCP_START == NULL ? get_first_locp_gte_addr (ADDRESS) : BP_LOCP_START, \
550              BP_LOCP_TMP = BP_LOCP_START;                               \
551              BP_LOCP_START                                              \
552              && (BP_LOCP_TMP < bp_location + bp_location_count          \
553              && (*BP_LOCP_TMP)->address == ADDRESS);                    \
554              BP_LOCP_TMP++)
555
556 /* Iterator for tracepoints only.  */
557
558 #define ALL_TRACEPOINTS(B)  \
559   for (B = breakpoint_chain; B; B = B->next)  \
560     if (is_tracepoint (B))
561
562 /* Chains of all breakpoints defined.  */
563
564 struct breakpoint *breakpoint_chain;
565
566 /* Array is sorted by bp_location_compare - primarily by the ADDRESS.  */
567
568 static struct bp_location **bp_location;
569
570 /* Number of elements of BP_LOCATION.  */
571
572 static unsigned bp_location_count;
573
574 /* Maximum alignment offset between bp_target_info.PLACED_ADDRESS and
575    ADDRESS for the current elements of BP_LOCATION which get a valid
576    result from bp_location_has_shadow.  You can use it for roughly
577    limiting the subrange of BP_LOCATION to scan for shadow bytes for
578    an address you need to read.  */
579
580 static CORE_ADDR bp_location_placed_address_before_address_max;
581
582 /* Maximum offset plus alignment between bp_target_info.PLACED_ADDRESS
583    + bp_target_info.SHADOW_LEN and ADDRESS for the current elements of
584    BP_LOCATION which get a valid result from bp_location_has_shadow.
585    You can use it for roughly limiting the subrange of BP_LOCATION to
586    scan for shadow bytes for an address you need to read.  */
587
588 static CORE_ADDR bp_location_shadow_len_after_address_max;
589
590 /* The locations that no longer correspond to any breakpoint, unlinked
591    from bp_location array, but for which a hit may still be reported
592    by a target.  */
593 VEC(bp_location_p) *moribund_locations = NULL;
594
595 /* Number of last breakpoint made.  */
596
597 static int breakpoint_count;
598
599 /* The value of `breakpoint_count' before the last command that
600    created breakpoints.  If the last (break-like) command created more
601    than one breakpoint, then the difference between BREAKPOINT_COUNT
602    and PREV_BREAKPOINT_COUNT is more than one.  */
603 static int prev_breakpoint_count;
604
605 /* Number of last tracepoint made.  */
606
607 static int tracepoint_count;
608
609 static struct cmd_list_element *breakpoint_set_cmdlist;
610 static struct cmd_list_element *breakpoint_show_cmdlist;
611 struct cmd_list_element *save_cmdlist;
612
613 /* Return whether a breakpoint is an active enabled breakpoint.  */
614 static int
615 breakpoint_enabled (struct breakpoint *b)
616 {
617   return (b->enable_state == bp_enabled);
618 }
619
620 /* Set breakpoint count to NUM.  */
621
622 static void
623 set_breakpoint_count (int num)
624 {
625   prev_breakpoint_count = breakpoint_count;
626   breakpoint_count = num;
627   set_internalvar_integer (lookup_internalvar ("bpnum"), num);
628 }
629
630 /* Used by `start_rbreak_breakpoints' below, to record the current
631    breakpoint count before "rbreak" creates any breakpoint.  */
632 static int rbreak_start_breakpoint_count;
633
634 /* Called at the start an "rbreak" command to record the first
635    breakpoint made.  */
636
637 void
638 start_rbreak_breakpoints (void)
639 {
640   rbreak_start_breakpoint_count = breakpoint_count;
641 }
642
643 /* Called at the end of an "rbreak" command to record the last
644    breakpoint made.  */
645
646 void
647 end_rbreak_breakpoints (void)
648 {
649   prev_breakpoint_count = rbreak_start_breakpoint_count;
650 }
651
652 /* Used in run_command to zero the hit count when a new run starts.  */
653
654 void
655 clear_breakpoint_hit_counts (void)
656 {
657   struct breakpoint *b;
658
659   ALL_BREAKPOINTS (b)
660     b->hit_count = 0;
661 }
662
663 /* Allocate a new counted_command_line with reference count of 1.
664    The new structure owns COMMANDS.  */
665
666 static struct counted_command_line *
667 alloc_counted_command_line (struct command_line *commands)
668 {
669   struct counted_command_line *result
670     = xmalloc (sizeof (struct counted_command_line));
671
672   result->refc = 1;
673   result->commands = commands;
674   return result;
675 }
676
677 /* Increment reference count.  This does nothing if CMD is NULL.  */
678
679 static void
680 incref_counted_command_line (struct counted_command_line *cmd)
681 {
682   if (cmd)
683     ++cmd->refc;
684 }
685
686 /* Decrement reference count.  If the reference count reaches 0,
687    destroy the counted_command_line.  Sets *CMDP to NULL.  This does
688    nothing if *CMDP is NULL.  */
689
690 static void
691 decref_counted_command_line (struct counted_command_line **cmdp)
692 {
693   if (*cmdp)
694     {
695       if (--(*cmdp)->refc == 0)
696         {
697           free_command_lines (&(*cmdp)->commands);
698           xfree (*cmdp);
699         }
700       *cmdp = NULL;
701     }
702 }
703
704 /* A cleanup function that calls decref_counted_command_line.  */
705
706 static void
707 do_cleanup_counted_command_line (void *arg)
708 {
709   decref_counted_command_line (arg);
710 }
711
712 /* Create a cleanup that calls decref_counted_command_line on the
713    argument.  */
714
715 static struct cleanup *
716 make_cleanup_decref_counted_command_line (struct counted_command_line **cmdp)
717 {
718   return make_cleanup (do_cleanup_counted_command_line, cmdp);
719 }
720
721 \f
722 /* Return the breakpoint with the specified number, or NULL
723    if the number does not refer to an existing breakpoint.  */
724
725 struct breakpoint *
726 get_breakpoint (int num)
727 {
728   struct breakpoint *b;
729
730   ALL_BREAKPOINTS (b)
731     if (b->number == num)
732       return b;
733   
734   return NULL;
735 }
736
737 \f
738
739 /* Mark locations as "conditions have changed" in case the target supports
740    evaluating conditions on its side.  */
741
742 static void
743 mark_breakpoint_modified (struct breakpoint *b)
744 {
745   struct bp_location *loc;
746
747   /* This is only meaningful if the target is
748      evaluating conditions and if the user has
749      opted for condition evaluation on the target's
750      side.  */
751   if (gdb_evaluates_breakpoint_condition_p ()
752       || !target_supports_evaluation_of_breakpoint_conditions ())
753     return;
754
755   if (!is_breakpoint (b))
756     return;
757
758   for (loc = b->loc; loc; loc = loc->next)
759     loc->condition_changed = condition_modified;
760 }
761
762 /* Mark location as "conditions have changed" in case the target supports
763    evaluating conditions on its side.  */
764
765 static void
766 mark_breakpoint_location_modified (struct bp_location *loc)
767 {
768   /* This is only meaningful if the target is
769      evaluating conditions and if the user has
770      opted for condition evaluation on the target's
771      side.  */
772   if (gdb_evaluates_breakpoint_condition_p ()
773       || !target_supports_evaluation_of_breakpoint_conditions ())
774
775     return;
776
777   if (!is_breakpoint (loc->owner))
778     return;
779
780   loc->condition_changed = condition_modified;
781 }
782
783 /* Sets the condition-evaluation mode using the static global
784    condition_evaluation_mode.  */
785
786 static void
787 set_condition_evaluation_mode (char *args, int from_tty,
788                                struct cmd_list_element *c)
789 {
790   const char *old_mode, *new_mode;
791
792   if ((condition_evaluation_mode_1 == condition_evaluation_target)
793       && !target_supports_evaluation_of_breakpoint_conditions ())
794     {
795       condition_evaluation_mode_1 = condition_evaluation_mode;
796       warning (_("Target does not support breakpoint condition evaluation.\n"
797                  "Using host evaluation mode instead."));
798       return;
799     }
800
801   new_mode = translate_condition_evaluation_mode (condition_evaluation_mode_1);
802   old_mode = translate_condition_evaluation_mode (condition_evaluation_mode);
803
804   /* Flip the switch.  Flip it even if OLD_MODE == NEW_MODE as one of the
805      settings was "auto".  */
806   condition_evaluation_mode = condition_evaluation_mode_1;
807
808   /* Only update the mode if the user picked a different one.  */
809   if (new_mode != old_mode)
810     {
811       struct bp_location *loc, **loc_tmp;
812       /* If the user switched to a different evaluation mode, we
813          need to synch the changes with the target as follows:
814
815          "host" -> "target": Send all (valid) conditions to the target.
816          "target" -> "host": Remove all the conditions from the target.
817       */
818
819       if (new_mode == condition_evaluation_target)
820         {
821           /* Mark everything modified and synch conditions with the
822              target.  */
823           ALL_BP_LOCATIONS (loc, loc_tmp)
824             mark_breakpoint_location_modified (loc);
825         }
826       else
827         {
828           /* Manually mark non-duplicate locations to synch conditions
829              with the target.  We do this to remove all the conditions the
830              target knows about.  */
831           ALL_BP_LOCATIONS (loc, loc_tmp)
832             if (is_breakpoint (loc->owner) && loc->inserted)
833               loc->needs_update = 1;
834         }
835
836       /* Do the update.  */
837       update_global_location_list (1);
838     }
839
840   return;
841 }
842
843 /* Shows the current mode of breakpoint condition evaluation.  Explicitly shows
844    what "auto" is translating to.  */
845
846 static void
847 show_condition_evaluation_mode (struct ui_file *file, int from_tty,
848                                 struct cmd_list_element *c, const char *value)
849 {
850   if (condition_evaluation_mode == condition_evaluation_auto)
851     fprintf_filtered (file,
852                       _("Breakpoint condition evaluation "
853                         "mode is %s (currently %s).\n"),
854                       value,
855                       breakpoint_condition_evaluation_mode ());
856   else
857     fprintf_filtered (file, _("Breakpoint condition evaluation mode is %s.\n"),
858                       value);
859 }
860
861 /* A comparison function for bp_location AP and BP that is used by
862    bsearch.  This comparison function only cares about addresses, unlike
863    the more general bp_location_compare function.  */
864
865 static int
866 bp_location_compare_addrs (const void *ap, const void *bp)
867 {
868   struct bp_location *a = *(void **) ap;
869   struct bp_location *b = *(void **) bp;
870
871   if (a->address == b->address)
872     return 0;
873   else
874     return ((a->address > b->address) - (a->address < b->address));
875 }
876
877 /* Helper function to skip all bp_locations with addresses
878    less than ADDRESS.  It returns the first bp_location that
879    is greater than or equal to ADDRESS.  If none is found, just
880    return NULL.  */
881
882 static struct bp_location **
883 get_first_locp_gte_addr (CORE_ADDR address)
884 {
885   struct bp_location dummy_loc;
886   struct bp_location *dummy_locp = &dummy_loc;
887   struct bp_location **locp_found = NULL;
888
889   /* Initialize the dummy location's address field.  */
890   memset (&dummy_loc, 0, sizeof (struct bp_location));
891   dummy_loc.address = address;
892
893   /* Find a close match to the first location at ADDRESS.  */
894   locp_found = bsearch (&dummy_locp, bp_location, bp_location_count,
895                         sizeof (struct bp_location **),
896                         bp_location_compare_addrs);
897
898   /* Nothing was found, nothing left to do.  */
899   if (locp_found == NULL)
900     return NULL;
901
902   /* We may have found a location that is at ADDRESS but is not the first in the
903      location's list.  Go backwards (if possible) and locate the first one.  */
904   while ((locp_found - 1) >= bp_location
905          && (*(locp_found - 1))->address == address)
906     locp_found--;
907
908   return locp_found;
909 }
910
911 void
912 set_breakpoint_condition (struct breakpoint *b, char *exp,
913                           int from_tty)
914 {
915   xfree (b->cond_string);
916   b->cond_string = NULL;
917
918   if (is_watchpoint (b))
919     {
920       struct watchpoint *w = (struct watchpoint *) b;
921
922       xfree (w->cond_exp);
923       w->cond_exp = NULL;
924     }
925   else
926     {
927       struct bp_location *loc;
928
929       for (loc = b->loc; loc; loc = loc->next)
930         {
931           xfree (loc->cond);
932           loc->cond = NULL;
933
934           /* No need to free the condition agent expression
935              bytecode (if we have one).  We will handle this
936              when we go through update_global_location_list.  */
937         }
938     }
939
940   if (*exp == 0)
941     {
942       if (from_tty)
943         printf_filtered (_("Breakpoint %d now unconditional.\n"), b->number);
944     }
945   else
946     {
947       const char *arg = exp;
948
949       /* I don't know if it matters whether this is the string the user
950          typed in or the decompiled expression.  */
951       b->cond_string = xstrdup (arg);
952       b->condition_not_parsed = 0;
953
954       if (is_watchpoint (b))
955         {
956           struct watchpoint *w = (struct watchpoint *) b;
957
958           innermost_block = NULL;
959           arg = exp;
960           w->cond_exp = parse_exp_1 (&arg, 0, 0, 0);
961           if (*arg)
962             error (_("Junk at end of expression"));
963           w->cond_exp_valid_block = innermost_block;
964         }
965       else
966         {
967           struct bp_location *loc;
968
969           for (loc = b->loc; loc; loc = loc->next)
970             {
971               arg = exp;
972               loc->cond =
973                 parse_exp_1 (&arg, loc->address,
974                              block_for_pc (loc->address), 0);
975               if (*arg)
976                 error (_("Junk at end of expression"));
977             }
978         }
979     }
980   mark_breakpoint_modified (b);
981
982   observer_notify_breakpoint_modified (b);
983 }
984
985 /* Completion for the "condition" command.  */
986
987 static VEC (char_ptr) *
988 condition_completer (struct cmd_list_element *cmd,
989                      const char *text, const char *word)
990 {
991   const char *space;
992
993   text = skip_spaces_const (text);
994   space = skip_to_space_const (text);
995   if (*space == '\0')
996     {
997       int len;
998       struct breakpoint *b;
999       VEC (char_ptr) *result = NULL;
1000
1001       if (text[0] == '$')
1002         {
1003           /* We don't support completion of history indices.  */
1004           if (isdigit (text[1]))
1005             return NULL;
1006           return complete_internalvar (&text[1]);
1007         }
1008
1009       /* We're completing the breakpoint number.  */
1010       len = strlen (text);
1011
1012       ALL_BREAKPOINTS (b)
1013         {
1014           char number[50];
1015
1016           xsnprintf (number, sizeof (number), "%d", b->number);
1017
1018           if (strncmp (number, text, len) == 0)
1019             VEC_safe_push (char_ptr, result, xstrdup (number));
1020         }
1021
1022       return result;
1023     }
1024
1025   /* We're completing the expression part.  */
1026   text = skip_spaces_const (space);
1027   return expression_completer (cmd, text, word);
1028 }
1029
1030 /* condition N EXP -- set break condition of breakpoint N to EXP.  */
1031
1032 static void
1033 condition_command (char *arg, int from_tty)
1034 {
1035   struct breakpoint *b;
1036   char *p;
1037   int bnum;
1038
1039   if (arg == 0)
1040     error_no_arg (_("breakpoint number"));
1041
1042   p = arg;
1043   bnum = get_number (&p);
1044   if (bnum == 0)
1045     error (_("Bad breakpoint argument: '%s'"), arg);
1046
1047   ALL_BREAKPOINTS (b)
1048     if (b->number == bnum)
1049       {
1050         /* Check if this breakpoint has a "stop" method implemented in an
1051            extension language.  This method and conditions entered into GDB
1052            from the CLI are mutually exclusive.  */
1053         const struct extension_language_defn *extlang
1054           = get_breakpoint_cond_ext_lang (b, EXT_LANG_NONE);
1055
1056         if (extlang != NULL)
1057           {
1058             error (_("Only one stop condition allowed.  There is currently"
1059                      " a %s stop condition defined for this breakpoint."),
1060                    ext_lang_capitalized_name (extlang));
1061           }
1062         set_breakpoint_condition (b, p, from_tty);
1063
1064         if (is_breakpoint (b))
1065           update_global_location_list (1);
1066
1067         return;
1068       }
1069
1070   error (_("No breakpoint number %d."), bnum);
1071 }
1072
1073 /* Check that COMMAND do not contain commands that are suitable
1074    only for tracepoints and not suitable for ordinary breakpoints.
1075    Throw if any such commands is found.  */
1076
1077 static void
1078 check_no_tracepoint_commands (struct command_line *commands)
1079 {
1080   struct command_line *c;
1081
1082   for (c = commands; c; c = c->next)
1083     {
1084       int i;
1085
1086       if (c->control_type == while_stepping_control)
1087         error (_("The 'while-stepping' command can "
1088                  "only be used for tracepoints"));
1089
1090       for (i = 0; i < c->body_count; ++i)
1091         check_no_tracepoint_commands ((c->body_list)[i]);
1092
1093       /* Not that command parsing removes leading whitespace and comment
1094          lines and also empty lines.  So, we only need to check for
1095          command directly.  */
1096       if (strstr (c->line, "collect ") == c->line)
1097         error (_("The 'collect' command can only be used for tracepoints"));
1098
1099       if (strstr (c->line, "teval ") == c->line)
1100         error (_("The 'teval' command can only be used for tracepoints"));
1101     }
1102 }
1103
1104 /* Encapsulate tests for different types of tracepoints.  */
1105
1106 static int
1107 is_tracepoint_type (enum bptype type)
1108 {
1109   return (type == bp_tracepoint
1110           || type == bp_fast_tracepoint
1111           || type == bp_static_tracepoint);
1112 }
1113
1114 int
1115 is_tracepoint (const struct breakpoint *b)
1116 {
1117   return is_tracepoint_type (b->type);
1118 }
1119
1120 /* A helper function that validates that COMMANDS are valid for a
1121    breakpoint.  This function will throw an exception if a problem is
1122    found.  */
1123
1124 static void
1125 validate_commands_for_breakpoint (struct breakpoint *b,
1126                                   struct command_line *commands)
1127 {
1128   if (is_tracepoint (b))
1129     {
1130       struct tracepoint *t = (struct tracepoint *) b;
1131       struct command_line *c;
1132       struct command_line *while_stepping = 0;
1133
1134       /* Reset the while-stepping step count.  The previous commands
1135          might have included a while-stepping action, while the new
1136          ones might not.  */
1137       t->step_count = 0;
1138
1139       /* We need to verify that each top-level element of commands is
1140          valid for tracepoints, that there's at most one
1141          while-stepping element, and that the while-stepping's body
1142          has valid tracing commands excluding nested while-stepping.
1143          We also need to validate the tracepoint action line in the
1144          context of the tracepoint --- validate_actionline actually
1145          has side effects, like setting the tracepoint's
1146          while-stepping STEP_COUNT, in addition to checking if the
1147          collect/teval actions parse and make sense in the
1148          tracepoint's context.  */
1149       for (c = commands; c; c = c->next)
1150         {
1151           if (c->control_type == while_stepping_control)
1152             {
1153               if (b->type == bp_fast_tracepoint)
1154                 error (_("The 'while-stepping' command "
1155                          "cannot be used for fast tracepoint"));
1156               else if (b->type == bp_static_tracepoint)
1157                 error (_("The 'while-stepping' command "
1158                          "cannot be used for static tracepoint"));
1159
1160               if (while_stepping)
1161                 error (_("The 'while-stepping' command "
1162                          "can be used only once"));
1163               else
1164                 while_stepping = c;
1165             }
1166
1167           validate_actionline (c->line, b);
1168         }
1169       if (while_stepping)
1170         {
1171           struct command_line *c2;
1172
1173           gdb_assert (while_stepping->body_count == 1);
1174           c2 = while_stepping->body_list[0];
1175           for (; c2; c2 = c2->next)
1176             {
1177               if (c2->control_type == while_stepping_control)
1178                 error (_("The 'while-stepping' command cannot be nested"));
1179             }
1180         }
1181     }
1182   else
1183     {
1184       check_no_tracepoint_commands (commands);
1185     }
1186 }
1187
1188 /* Return a vector of all the static tracepoints set at ADDR.  The
1189    caller is responsible for releasing the vector.  */
1190
1191 VEC(breakpoint_p) *
1192 static_tracepoints_here (CORE_ADDR addr)
1193 {
1194   struct breakpoint *b;
1195   VEC(breakpoint_p) *found = 0;
1196   struct bp_location *loc;
1197
1198   ALL_BREAKPOINTS (b)
1199     if (b->type == bp_static_tracepoint)
1200       {
1201         for (loc = b->loc; loc; loc = loc->next)
1202           if (loc->address == addr)
1203             VEC_safe_push(breakpoint_p, found, b);
1204       }
1205
1206   return found;
1207 }
1208
1209 /* Set the command list of B to COMMANDS.  If breakpoint is tracepoint,
1210    validate that only allowed commands are included.  */
1211
1212 void
1213 breakpoint_set_commands (struct breakpoint *b, 
1214                          struct command_line *commands)
1215 {
1216   validate_commands_for_breakpoint (b, commands);
1217
1218   decref_counted_command_line (&b->commands);
1219   b->commands = alloc_counted_command_line (commands);
1220   observer_notify_breakpoint_modified (b);
1221 }
1222
1223 /* Set the internal `silent' flag on the breakpoint.  Note that this
1224    is not the same as the "silent" that may appear in the breakpoint's
1225    commands.  */
1226
1227 void
1228 breakpoint_set_silent (struct breakpoint *b, int silent)
1229 {
1230   int old_silent = b->silent;
1231
1232   b->silent = silent;
1233   if (old_silent != silent)
1234     observer_notify_breakpoint_modified (b);
1235 }
1236
1237 /* Set the thread for this breakpoint.  If THREAD is -1, make the
1238    breakpoint work for any thread.  */
1239
1240 void
1241 breakpoint_set_thread (struct breakpoint *b, int thread)
1242 {
1243   int old_thread = b->thread;
1244
1245   b->thread = thread;
1246   if (old_thread != thread)
1247     observer_notify_breakpoint_modified (b);
1248 }
1249
1250 /* Set the task for this breakpoint.  If TASK is 0, make the
1251    breakpoint work for any task.  */
1252
1253 void
1254 breakpoint_set_task (struct breakpoint *b, int task)
1255 {
1256   int old_task = b->task;
1257
1258   b->task = task;
1259   if (old_task != task)
1260     observer_notify_breakpoint_modified (b);
1261 }
1262
1263 void
1264 check_tracepoint_command (char *line, void *closure)
1265 {
1266   struct breakpoint *b = closure;
1267
1268   validate_actionline (line, b);
1269 }
1270
1271 /* A structure used to pass information through
1272    map_breakpoint_numbers.  */
1273
1274 struct commands_info
1275 {
1276   /* True if the command was typed at a tty.  */
1277   int from_tty;
1278
1279   /* The breakpoint range spec.  */
1280   char *arg;
1281
1282   /* Non-NULL if the body of the commands are being read from this
1283      already-parsed command.  */
1284   struct command_line *control;
1285
1286   /* The command lines read from the user, or NULL if they have not
1287      yet been read.  */
1288   struct counted_command_line *cmd;
1289 };
1290
1291 /* A callback for map_breakpoint_numbers that sets the commands for
1292    commands_command.  */
1293
1294 static void
1295 do_map_commands_command (struct breakpoint *b, void *data)
1296 {
1297   struct commands_info *info = data;
1298
1299   if (info->cmd == NULL)
1300     {
1301       struct command_line *l;
1302
1303       if (info->control != NULL)
1304         l = copy_command_lines (info->control->body_list[0]);
1305       else
1306         {
1307           struct cleanup *old_chain;
1308           char *str;
1309
1310           str = xstrprintf (_("Type commands for breakpoint(s) "
1311                               "%s, one per line."),
1312                             info->arg);
1313
1314           old_chain = make_cleanup (xfree, str);
1315
1316           l = read_command_lines (str,
1317                                   info->from_tty, 1,
1318                                   (is_tracepoint (b)
1319                                    ? check_tracepoint_command : 0),
1320                                   b);
1321
1322           do_cleanups (old_chain);
1323         }
1324
1325       info->cmd = alloc_counted_command_line (l);
1326     }
1327
1328   /* If a breakpoint was on the list more than once, we don't need to
1329      do anything.  */
1330   if (b->commands != info->cmd)
1331     {
1332       validate_commands_for_breakpoint (b, info->cmd->commands);
1333       incref_counted_command_line (info->cmd);
1334       decref_counted_command_line (&b->commands);
1335       b->commands = info->cmd;
1336       observer_notify_breakpoint_modified (b);
1337     }
1338 }
1339
1340 static void
1341 commands_command_1 (char *arg, int from_tty, 
1342                     struct command_line *control)
1343 {
1344   struct cleanup *cleanups;
1345   struct commands_info info;
1346
1347   info.from_tty = from_tty;
1348   info.control = control;
1349   info.cmd = NULL;
1350   /* If we read command lines from the user, then `info' will hold an
1351      extra reference to the commands that we must clean up.  */
1352   cleanups = make_cleanup_decref_counted_command_line (&info.cmd);
1353
1354   if (arg == NULL || !*arg)
1355     {
1356       if (breakpoint_count - prev_breakpoint_count > 1)
1357         arg = xstrprintf ("%d-%d", prev_breakpoint_count + 1, 
1358                           breakpoint_count);
1359       else if (breakpoint_count > 0)
1360         arg = xstrprintf ("%d", breakpoint_count);
1361       else
1362         {
1363           /* So that we don't try to free the incoming non-NULL
1364              argument in the cleanup below.  Mapping breakpoint
1365              numbers will fail in this case.  */
1366           arg = NULL;
1367         }
1368     }
1369   else
1370     /* The command loop has some static state, so we need to preserve
1371        our argument.  */
1372     arg = xstrdup (arg);
1373
1374   if (arg != NULL)
1375     make_cleanup (xfree, arg);
1376
1377   info.arg = arg;
1378
1379   map_breakpoint_numbers (arg, do_map_commands_command, &info);
1380
1381   if (info.cmd == NULL)
1382     error (_("No breakpoints specified."));
1383
1384   do_cleanups (cleanups);
1385 }
1386
1387 static void
1388 commands_command (char *arg, int from_tty)
1389 {
1390   commands_command_1 (arg, from_tty, NULL);
1391 }
1392
1393 /* Like commands_command, but instead of reading the commands from
1394    input stream, takes them from an already parsed command structure.
1395
1396    This is used by cli-script.c to DTRT with breakpoint commands
1397    that are part of if and while bodies.  */
1398 enum command_control_type
1399 commands_from_control_command (char *arg, struct command_line *cmd)
1400 {
1401   commands_command_1 (arg, 0, cmd);
1402   return simple_control;
1403 }
1404
1405 /* Return non-zero if BL->TARGET_INFO contains valid information.  */
1406
1407 static int
1408 bp_location_has_shadow (struct bp_location *bl)
1409 {
1410   if (bl->loc_type != bp_loc_software_breakpoint)
1411     return 0;
1412   if (!bl->inserted)
1413     return 0;
1414   if (bl->target_info.shadow_len == 0)
1415     /* BL isn't valid, or doesn't shadow memory.  */
1416     return 0;
1417   return 1;
1418 }
1419
1420 /* Update BUF, which is LEN bytes read from the target address MEMADDR,
1421    by replacing any memory breakpoints with their shadowed contents.
1422
1423    If READBUF is not NULL, this buffer must not overlap with any of
1424    the breakpoint location's shadow_contents buffers.  Otherwise,
1425    a failed assertion internal error will be raised.
1426
1427    The range of shadowed area by each bp_location is:
1428      bl->address - bp_location_placed_address_before_address_max
1429      up to bl->address + bp_location_shadow_len_after_address_max
1430    The range we were requested to resolve shadows for is:
1431      memaddr ... memaddr + len
1432    Thus the safe cutoff boundaries for performance optimization are
1433      memaddr + len <= (bl->address
1434                        - bp_location_placed_address_before_address_max)
1435    and:
1436      bl->address + bp_location_shadow_len_after_address_max <= memaddr  */
1437
1438 void
1439 breakpoint_xfer_memory (gdb_byte *readbuf, gdb_byte *writebuf,
1440                         const gdb_byte *writebuf_org,
1441                         ULONGEST memaddr, LONGEST len)
1442 {
1443   /* Left boundary, right boundary and median element of our binary
1444      search.  */
1445   unsigned bc_l, bc_r, bc;
1446
1447   /* Find BC_L which is a leftmost element which may affect BUF
1448      content.  It is safe to report lower value but a failure to
1449      report higher one.  */
1450
1451   bc_l = 0;
1452   bc_r = bp_location_count;
1453   while (bc_l + 1 < bc_r)
1454     {
1455       struct bp_location *bl;
1456
1457       bc = (bc_l + bc_r) / 2;
1458       bl = bp_location[bc];
1459
1460       /* Check first BL->ADDRESS will not overflow due to the added
1461          constant.  Then advance the left boundary only if we are sure
1462          the BC element can in no way affect the BUF content (MEMADDR
1463          to MEMADDR + LEN range).
1464
1465          Use the BP_LOCATION_SHADOW_LEN_AFTER_ADDRESS_MAX safety
1466          offset so that we cannot miss a breakpoint with its shadow
1467          range tail still reaching MEMADDR.  */
1468
1469       if ((bl->address + bp_location_shadow_len_after_address_max
1470            >= bl->address)
1471           && (bl->address + bp_location_shadow_len_after_address_max
1472               <= memaddr))
1473         bc_l = bc;
1474       else
1475         bc_r = bc;
1476     }
1477
1478   /* Due to the binary search above, we need to make sure we pick the
1479      first location that's at BC_L's address.  E.g., if there are
1480      multiple locations at the same address, BC_L may end up pointing
1481      at a duplicate location, and miss the "master"/"inserted"
1482      location.  Say, given locations L1, L2 and L3 at addresses A and
1483      B:
1484
1485       L1@A, L2@A, L3@B, ...
1486
1487      BC_L could end up pointing at location L2, while the "master"
1488      location could be L1.  Since the `loc->inserted' flag is only set
1489      on "master" locations, we'd forget to restore the shadow of L1
1490      and L2.  */
1491   while (bc_l > 0
1492          && bp_location[bc_l]->address == bp_location[bc_l - 1]->address)
1493     bc_l--;
1494
1495   /* Now do full processing of the found relevant range of elements.  */
1496
1497   for (bc = bc_l; bc < bp_location_count; bc++)
1498   {
1499     struct bp_location *bl = bp_location[bc];
1500     CORE_ADDR bp_addr = 0;
1501     int bp_size = 0;
1502     int bptoffset = 0;
1503
1504     /* bp_location array has BL->OWNER always non-NULL.  */
1505     if (bl->owner->type == bp_none)
1506       warning (_("reading through apparently deleted breakpoint #%d?"),
1507                bl->owner->number);
1508
1509     /* Performance optimization: any further element can no longer affect BUF
1510        content.  */
1511
1512     if (bl->address >= bp_location_placed_address_before_address_max
1513         && memaddr + len <= (bl->address
1514                              - bp_location_placed_address_before_address_max))
1515       break;
1516
1517     if (!bp_location_has_shadow (bl))
1518       continue;
1519     if (!breakpoint_address_match (bl->target_info.placed_address_space, 0,
1520                                    current_program_space->aspace, 0))
1521       continue;
1522
1523     /* Addresses and length of the part of the breakpoint that
1524        we need to copy.  */
1525     bp_addr = bl->target_info.placed_address;
1526     bp_size = bl->target_info.shadow_len;
1527
1528     if (bp_addr + bp_size <= memaddr)
1529       /* The breakpoint is entirely before the chunk of memory we
1530          are reading.  */
1531       continue;
1532
1533     if (bp_addr >= memaddr + len)
1534       /* The breakpoint is entirely after the chunk of memory we are
1535          reading.  */
1536       continue;
1537
1538     /* Offset within shadow_contents.  */
1539     if (bp_addr < memaddr)
1540       {
1541         /* Only copy the second part of the breakpoint.  */
1542         bp_size -= memaddr - bp_addr;
1543         bptoffset = memaddr - bp_addr;
1544         bp_addr = memaddr;
1545       }
1546
1547     if (bp_addr + bp_size > memaddr + len)
1548       {
1549         /* Only copy the first part of the breakpoint.  */
1550         bp_size -= (bp_addr + bp_size) - (memaddr + len);
1551       }
1552
1553     if (readbuf != NULL)
1554       {
1555         /* Verify that the readbuf buffer does not overlap with
1556            the shadow_contents buffer.  */
1557         gdb_assert (bl->target_info.shadow_contents >= readbuf + len
1558                     || readbuf >= (bl->target_info.shadow_contents
1559                                    + bl->target_info.shadow_len));
1560
1561         /* Update the read buffer with this inserted breakpoint's
1562            shadow.  */
1563         memcpy (readbuf + bp_addr - memaddr,
1564                 bl->target_info.shadow_contents + bptoffset, bp_size);
1565       }
1566     else
1567       {
1568         struct gdbarch *gdbarch = bl->gdbarch;
1569         const unsigned char *bp;
1570         CORE_ADDR placed_address = bl->target_info.placed_address;
1571         int placed_size = bl->target_info.placed_size;
1572
1573         /* Update the shadow with what we want to write to memory.  */
1574         memcpy (bl->target_info.shadow_contents + bptoffset,
1575                 writebuf_org + bp_addr - memaddr, bp_size);
1576
1577         /* Determine appropriate breakpoint contents and size for this
1578            address.  */
1579         bp = gdbarch_breakpoint_from_pc (gdbarch, &placed_address, &placed_size);
1580
1581         /* Update the final write buffer with this inserted
1582            breakpoint's INSN.  */
1583         memcpy (writebuf + bp_addr - memaddr, bp + bptoffset, bp_size);
1584       }
1585   }
1586 }
1587 \f
1588
1589 /* Return true if BPT is either a software breakpoint or a hardware
1590    breakpoint.  */
1591
1592 int
1593 is_breakpoint (const struct breakpoint *bpt)
1594 {
1595   return (bpt->type == bp_breakpoint
1596           || bpt->type == bp_hardware_breakpoint
1597           || bpt->type == bp_dprintf);
1598 }
1599
1600 /* Return true if BPT is of any hardware watchpoint kind.  */
1601
1602 static int
1603 is_hardware_watchpoint (const struct breakpoint *bpt)
1604 {
1605   return (bpt->type == bp_hardware_watchpoint
1606           || bpt->type == bp_read_watchpoint
1607           || bpt->type == bp_access_watchpoint);
1608 }
1609
1610 /* Return true if BPT is of any watchpoint kind, hardware or
1611    software.  */
1612
1613 int
1614 is_watchpoint (const struct breakpoint *bpt)
1615 {
1616   return (is_hardware_watchpoint (bpt)
1617           || bpt->type == bp_watchpoint);
1618 }
1619
1620 /* Returns true if the current thread and its running state are safe
1621    to evaluate or update watchpoint B.  Watchpoints on local
1622    expressions need to be evaluated in the context of the thread that
1623    was current when the watchpoint was created, and, that thread needs
1624    to be stopped to be able to select the correct frame context.
1625    Watchpoints on global expressions can be evaluated on any thread,
1626    and in any state.  It is presently left to the target allowing
1627    memory accesses when threads are running.  */
1628
1629 static int
1630 watchpoint_in_thread_scope (struct watchpoint *b)
1631 {
1632   return (b->base.pspace == current_program_space
1633           && (ptid_equal (b->watchpoint_thread, null_ptid)
1634               || (ptid_equal (inferior_ptid, b->watchpoint_thread)
1635                   && !is_executing (inferior_ptid))));
1636 }
1637
1638 /* Set watchpoint B to disp_del_at_next_stop, even including its possible
1639    associated bp_watchpoint_scope breakpoint.  */
1640
1641 static void
1642 watchpoint_del_at_next_stop (struct watchpoint *w)
1643 {
1644   struct breakpoint *b = &w->base;
1645
1646   if (b->related_breakpoint != b)
1647     {
1648       gdb_assert (b->related_breakpoint->type == bp_watchpoint_scope);
1649       gdb_assert (b->related_breakpoint->related_breakpoint == b);
1650       b->related_breakpoint->disposition = disp_del_at_next_stop;
1651       b->related_breakpoint->related_breakpoint = b->related_breakpoint;
1652       b->related_breakpoint = b;
1653     }
1654   b->disposition = disp_del_at_next_stop;
1655 }
1656
1657 /* Assuming that B is a watchpoint:
1658    - Reparse watchpoint expression, if REPARSE is non-zero
1659    - Evaluate expression and store the result in B->val
1660    - Evaluate the condition if there is one, and store the result
1661      in b->loc->cond.
1662    - Update the list of values that must be watched in B->loc.
1663
1664    If the watchpoint disposition is disp_del_at_next_stop, then do
1665    nothing.  If this is local watchpoint that is out of scope, delete
1666    it.
1667
1668    Even with `set breakpoint always-inserted on' the watchpoints are
1669    removed + inserted on each stop here.  Normal breakpoints must
1670    never be removed because they might be missed by a running thread
1671    when debugging in non-stop mode.  On the other hand, hardware
1672    watchpoints (is_hardware_watchpoint; processed here) are specific
1673    to each LWP since they are stored in each LWP's hardware debug
1674    registers.  Therefore, such LWP must be stopped first in order to
1675    be able to modify its hardware watchpoints.
1676
1677    Hardware watchpoints must be reset exactly once after being
1678    presented to the user.  It cannot be done sooner, because it would
1679    reset the data used to present the watchpoint hit to the user.  And
1680    it must not be done later because it could display the same single
1681    watchpoint hit during multiple GDB stops.  Note that the latter is
1682    relevant only to the hardware watchpoint types bp_read_watchpoint
1683    and bp_access_watchpoint.  False hit by bp_hardware_watchpoint is
1684    not user-visible - its hit is suppressed if the memory content has
1685    not changed.
1686
1687    The following constraints influence the location where we can reset
1688    hardware watchpoints:
1689
1690    * target_stopped_by_watchpoint and target_stopped_data_address are
1691      called several times when GDB stops.
1692
1693    [linux] 
1694    * Multiple hardware watchpoints can be hit at the same time,
1695      causing GDB to stop.  GDB only presents one hardware watchpoint
1696      hit at a time as the reason for stopping, and all the other hits
1697      are presented later, one after the other, each time the user
1698      requests the execution to be resumed.  Execution is not resumed
1699      for the threads still having pending hit event stored in
1700      LWP_INFO->STATUS.  While the watchpoint is already removed from
1701      the inferior on the first stop the thread hit event is kept being
1702      reported from its cached value by linux_nat_stopped_data_address
1703      until the real thread resume happens after the watchpoint gets
1704      presented and thus its LWP_INFO->STATUS gets reset.
1705
1706    Therefore the hardware watchpoint hit can get safely reset on the
1707    watchpoint removal from inferior.  */
1708
1709 static void
1710 update_watchpoint (struct watchpoint *b, int reparse)
1711 {
1712   int within_current_scope;
1713   struct frame_id saved_frame_id;
1714   int frame_saved;
1715
1716   /* If this is a local watchpoint, we only want to check if the
1717      watchpoint frame is in scope if the current thread is the thread
1718      that was used to create the watchpoint.  */
1719   if (!watchpoint_in_thread_scope (b))
1720     return;
1721
1722   if (b->base.disposition == disp_del_at_next_stop)
1723     return;
1724  
1725   frame_saved = 0;
1726
1727   /* Determine if the watchpoint is within scope.  */
1728   if (b->exp_valid_block == NULL)
1729     within_current_scope = 1;
1730   else
1731     {
1732       struct frame_info *fi = get_current_frame ();
1733       struct gdbarch *frame_arch = get_frame_arch (fi);
1734       CORE_ADDR frame_pc = get_frame_pc (fi);
1735
1736       /* If we're in a function epilogue, unwinding may not work
1737          properly, so do not attempt to recreate locations at this
1738          point.  See similar comments in watchpoint_check.  */
1739       if (gdbarch_in_function_epilogue_p (frame_arch, frame_pc))
1740         return;
1741
1742       /* Save the current frame's ID so we can restore it after
1743          evaluating the watchpoint expression on its own frame.  */
1744       /* FIXME drow/2003-09-09: It would be nice if evaluate_expression
1745          took a frame parameter, so that we didn't have to change the
1746          selected frame.  */
1747       frame_saved = 1;
1748       saved_frame_id = get_frame_id (get_selected_frame (NULL));
1749
1750       fi = frame_find_by_id (b->watchpoint_frame);
1751       within_current_scope = (fi != NULL);
1752       if (within_current_scope)
1753         select_frame (fi);
1754     }
1755
1756   /* We don't free locations.  They are stored in the bp_location array
1757      and update_global_location_list will eventually delete them and
1758      remove breakpoints if needed.  */
1759   b->base.loc = NULL;
1760
1761   if (within_current_scope && reparse)
1762     {
1763       const char *s;
1764
1765       if (b->exp)
1766         {
1767           xfree (b->exp);
1768           b->exp = NULL;
1769         }
1770       s = b->exp_string_reparse ? b->exp_string_reparse : b->exp_string;
1771       b->exp = parse_exp_1 (&s, 0, b->exp_valid_block, 0);
1772       /* If the meaning of expression itself changed, the old value is
1773          no longer relevant.  We don't want to report a watchpoint hit
1774          to the user when the old value and the new value may actually
1775          be completely different objects.  */
1776       value_free (b->val);
1777       b->val = NULL;
1778       b->val_valid = 0;
1779
1780       /* Note that unlike with breakpoints, the watchpoint's condition
1781          expression is stored in the breakpoint object, not in the
1782          locations (re)created below.  */
1783       if (b->base.cond_string != NULL)
1784         {
1785           if (b->cond_exp != NULL)
1786             {
1787               xfree (b->cond_exp);
1788               b->cond_exp = NULL;
1789             }
1790
1791           s = b->base.cond_string;
1792           b->cond_exp = parse_exp_1 (&s, 0, b->cond_exp_valid_block, 0);
1793         }
1794     }
1795
1796   /* If we failed to parse the expression, for example because
1797      it refers to a global variable in a not-yet-loaded shared library,
1798      don't try to insert watchpoint.  We don't automatically delete
1799      such watchpoint, though, since failure to parse expression
1800      is different from out-of-scope watchpoint.  */
1801   if (!target_has_execution)
1802     {
1803       /* Without execution, memory can't change.  No use to try and
1804          set watchpoint locations.  The watchpoint will be reset when
1805          the target gains execution, through breakpoint_re_set.  */
1806       if (!can_use_hw_watchpoints)
1807         {
1808           if (b->base.ops->works_in_software_mode (&b->base))
1809             b->base.type = bp_watchpoint;
1810           else
1811             error (_("Can't set read/access watchpoint when "
1812                      "hardware watchpoints are disabled."));
1813         }
1814     }
1815   else if (within_current_scope && b->exp)
1816     {
1817       int pc = 0;
1818       struct value *val_chain, *v, *result, *next;
1819       struct program_space *frame_pspace;
1820
1821       fetch_subexp_value (b->exp, &pc, &v, &result, &val_chain, 0);
1822
1823       /* Avoid setting b->val if it's already set.  The meaning of
1824          b->val is 'the last value' user saw, and we should update
1825          it only if we reported that last value to user.  As it
1826          happens, the code that reports it updates b->val directly.
1827          We don't keep track of the memory value for masked
1828          watchpoints.  */
1829       if (!b->val_valid && !is_masked_watchpoint (&b->base))
1830         {
1831           b->val = v;
1832           b->val_valid = 1;
1833         }
1834
1835       frame_pspace = get_frame_program_space (get_selected_frame (NULL));
1836
1837       /* Look at each value on the value chain.  */
1838       for (v = val_chain; v; v = value_next (v))
1839         {
1840           /* If it's a memory location, and GDB actually needed
1841              its contents to evaluate the expression, then we
1842              must watch it.  If the first value returned is
1843              still lazy, that means an error occurred reading it;
1844              watch it anyway in case it becomes readable.  */
1845           if (VALUE_LVAL (v) == lval_memory
1846               && (v == val_chain || ! value_lazy (v)))
1847             {
1848               struct type *vtype = check_typedef (value_type (v));
1849
1850               /* We only watch structs and arrays if user asked
1851                  for it explicitly, never if they just happen to
1852                  appear in the middle of some value chain.  */
1853               if (v == result
1854                   || (TYPE_CODE (vtype) != TYPE_CODE_STRUCT
1855                       && TYPE_CODE (vtype) != TYPE_CODE_ARRAY))
1856                 {
1857                   CORE_ADDR addr;
1858                   int type;
1859                   struct bp_location *loc, **tmp;
1860
1861                   addr = value_address (v);
1862                   type = hw_write;
1863                   if (b->base.type == bp_read_watchpoint)
1864                     type = hw_read;
1865                   else if (b->base.type == bp_access_watchpoint)
1866                     type = hw_access;
1867
1868                   loc = allocate_bp_location (&b->base);
1869                   for (tmp = &(b->base.loc); *tmp != NULL; tmp = &((*tmp)->next))
1870                     ;
1871                   *tmp = loc;
1872                   loc->gdbarch = get_type_arch (value_type (v));
1873
1874                   loc->pspace = frame_pspace;
1875                   loc->address = addr;
1876                   loc->length = TYPE_LENGTH (value_type (v));
1877                   loc->watchpoint_type = type;
1878                 }
1879             }
1880         }
1881
1882       /* Change the type of breakpoint between hardware assisted or
1883          an ordinary watchpoint depending on the hardware support
1884          and free hardware slots.  REPARSE is set when the inferior
1885          is started.  */
1886       if (reparse)
1887         {
1888           int reg_cnt;
1889           enum bp_loc_type loc_type;
1890           struct bp_location *bl;
1891
1892           reg_cnt = can_use_hardware_watchpoint (val_chain);
1893
1894           if (reg_cnt)
1895             {
1896               int i, target_resources_ok, other_type_used;
1897               enum bptype type;
1898
1899               /* Use an exact watchpoint when there's only one memory region to be
1900                  watched, and only one debug register is needed to watch it.  */
1901               b->exact = target_exact_watchpoints && reg_cnt == 1;
1902
1903               /* We need to determine how many resources are already
1904                  used for all other hardware watchpoints plus this one
1905                  to see if we still have enough resources to also fit
1906                  this watchpoint in as well.  */
1907
1908               /* If this is a software watchpoint, we try to turn it
1909                  to a hardware one -- count resources as if B was of
1910                  hardware watchpoint type.  */
1911               type = b->base.type;
1912               if (type == bp_watchpoint)
1913                 type = bp_hardware_watchpoint;
1914
1915               /* This watchpoint may or may not have been placed on
1916                  the list yet at this point (it won't be in the list
1917                  if we're trying to create it for the first time,
1918                  through watch_command), so always account for it
1919                  manually.  */
1920
1921               /* Count resources used by all watchpoints except B.  */
1922               i = hw_watchpoint_used_count_others (&b->base, type, &other_type_used);
1923
1924               /* Add in the resources needed for B.  */
1925               i += hw_watchpoint_use_count (&b->base);
1926
1927               target_resources_ok
1928                 = target_can_use_hardware_watchpoint (type, i, other_type_used);
1929               if (target_resources_ok <= 0)
1930                 {
1931                   int sw_mode = b->base.ops->works_in_software_mode (&b->base);
1932
1933                   if (target_resources_ok == 0 && !sw_mode)
1934                     error (_("Target does not support this type of "
1935                              "hardware watchpoint."));
1936                   else if (target_resources_ok < 0 && !sw_mode)
1937                     error (_("There are not enough available hardware "
1938                              "resources for this watchpoint."));
1939
1940                   /* Downgrade to software watchpoint.  */
1941                   b->base.type = bp_watchpoint;
1942                 }
1943               else
1944                 {
1945                   /* If this was a software watchpoint, we've just
1946                      found we have enough resources to turn it to a
1947                      hardware watchpoint.  Otherwise, this is a
1948                      nop.  */
1949                   b->base.type = type;
1950                 }
1951             }
1952           else if (!b->base.ops->works_in_software_mode (&b->base))
1953             {
1954               if (!can_use_hw_watchpoints)
1955                 error (_("Can't set read/access watchpoint when "
1956                          "hardware watchpoints are disabled."));
1957               else
1958                 error (_("Expression cannot be implemented with "
1959                          "read/access watchpoint."));
1960             }
1961           else
1962             b->base.type = bp_watchpoint;
1963
1964           loc_type = (b->base.type == bp_watchpoint? bp_loc_other
1965                       : bp_loc_hardware_watchpoint);
1966           for (bl = b->base.loc; bl; bl = bl->next)
1967             bl->loc_type = loc_type;
1968         }
1969
1970       for (v = val_chain; v; v = next)
1971         {
1972           next = value_next (v);
1973           if (v != b->val)
1974             value_free (v);
1975         }
1976
1977       /* If a software watchpoint is not watching any memory, then the
1978          above left it without any location set up.  But,
1979          bpstat_stop_status requires a location to be able to report
1980          stops, so make sure there's at least a dummy one.  */
1981       if (b->base.type == bp_watchpoint && b->base.loc == NULL)
1982         {
1983           struct breakpoint *base = &b->base;
1984           base->loc = allocate_bp_location (base);
1985           base->loc->pspace = frame_pspace;
1986           base->loc->address = -1;
1987           base->loc->length = -1;
1988           base->loc->watchpoint_type = -1;
1989         }
1990     }
1991   else if (!within_current_scope)
1992     {
1993       printf_filtered (_("\
1994 Watchpoint %d deleted because the program has left the block\n\
1995 in which its expression is valid.\n"),
1996                        b->base.number);
1997       watchpoint_del_at_next_stop (b);
1998     }
1999
2000   /* Restore the selected frame.  */
2001   if (frame_saved)
2002     select_frame (frame_find_by_id (saved_frame_id));
2003 }
2004
2005
2006 /* Returns 1 iff breakpoint location should be
2007    inserted in the inferior.  We don't differentiate the type of BL's owner
2008    (breakpoint vs. tracepoint), although insert_location in tracepoint's
2009    breakpoint_ops is not defined, because in insert_bp_location,
2010    tracepoint's insert_location will not be called.  */
2011 static int
2012 should_be_inserted (struct bp_location *bl)
2013 {
2014   if (bl->owner == NULL || !breakpoint_enabled (bl->owner))
2015     return 0;
2016
2017   if (bl->owner->disposition == disp_del_at_next_stop)
2018     return 0;
2019
2020   if (!bl->enabled || bl->shlib_disabled || bl->duplicate)
2021     return 0;
2022
2023   if (user_breakpoint_p (bl->owner) && bl->pspace->executing_startup)
2024     return 0;
2025
2026   /* This is set for example, when we're attached to the parent of a
2027      vfork, and have detached from the child.  The child is running
2028      free, and we expect it to do an exec or exit, at which point the
2029      OS makes the parent schedulable again (and the target reports
2030      that the vfork is done).  Until the child is done with the shared
2031      memory region, do not insert breakpoints in the parent, otherwise
2032      the child could still trip on the parent's breakpoints.  Since
2033      the parent is blocked anyway, it won't miss any breakpoint.  */
2034   if (bl->pspace->breakpoints_not_allowed)
2035     return 0;
2036
2037   return 1;
2038 }
2039
2040 /* Same as should_be_inserted but does the check assuming
2041    that the location is not duplicated.  */
2042
2043 static int
2044 unduplicated_should_be_inserted (struct bp_location *bl)
2045 {
2046   int result;
2047   const int save_duplicate = bl->duplicate;
2048
2049   bl->duplicate = 0;
2050   result = should_be_inserted (bl);
2051   bl->duplicate = save_duplicate;
2052   return result;
2053 }
2054
2055 /* Parses a conditional described by an expression COND into an
2056    agent expression bytecode suitable for evaluation
2057    by the bytecode interpreter.  Return NULL if there was
2058    any error during parsing.  */
2059
2060 static struct agent_expr *
2061 parse_cond_to_aexpr (CORE_ADDR scope, struct expression *cond)
2062 {
2063   struct agent_expr *aexpr = NULL;
2064   volatile struct gdb_exception ex;
2065
2066   if (!cond)
2067     return NULL;
2068
2069   /* We don't want to stop processing, so catch any errors
2070      that may show up.  */
2071   TRY_CATCH (ex, RETURN_MASK_ERROR)
2072     {
2073       aexpr = gen_eval_for_expr (scope, cond);
2074     }
2075
2076   if (ex.reason < 0)
2077     {
2078       /* If we got here, it means the condition could not be parsed to a valid
2079          bytecode expression and thus can't be evaluated on the target's side.
2080          It's no use iterating through the conditions.  */
2081       return NULL;
2082     }
2083
2084   /* We have a valid agent expression.  */
2085   return aexpr;
2086 }
2087
2088 /* Based on location BL, create a list of breakpoint conditions to be
2089    passed on to the target.  If we have duplicated locations with different
2090    conditions, we will add such conditions to the list.  The idea is that the
2091    target will evaluate the list of conditions and will only notify GDB when
2092    one of them is true.  */
2093
2094 static void
2095 build_target_condition_list (struct bp_location *bl)
2096 {
2097   struct bp_location **locp = NULL, **loc2p;
2098   int null_condition_or_parse_error = 0;
2099   int modified = bl->needs_update;
2100   struct bp_location *loc;
2101
2102   /* Release conditions left over from a previous insert.  */
2103   VEC_free (agent_expr_p, bl->target_info.conditions);
2104
2105   /* This is only meaningful if the target is
2106      evaluating conditions and if the user has
2107      opted for condition evaluation on the target's
2108      side.  */
2109   if (gdb_evaluates_breakpoint_condition_p ()
2110       || !target_supports_evaluation_of_breakpoint_conditions ())
2111     return;
2112
2113   /* Do a first pass to check for locations with no assigned
2114      conditions or conditions that fail to parse to a valid agent expression
2115      bytecode.  If any of these happen, then it's no use to send conditions
2116      to the target since this location will always trigger and generate a
2117      response back to GDB.  */
2118   ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2119     {
2120       loc = (*loc2p);
2121       if (is_breakpoint (loc->owner) && loc->pspace->num == bl->pspace->num)
2122         {
2123           if (modified)
2124             {
2125               struct agent_expr *aexpr;
2126
2127               /* Re-parse the conditions since something changed.  In that
2128                  case we already freed the condition bytecodes (see
2129                  force_breakpoint_reinsertion).  We just
2130                  need to parse the condition to bytecodes again.  */
2131               aexpr = parse_cond_to_aexpr (bl->address, loc->cond);
2132               loc->cond_bytecode = aexpr;
2133
2134               /* Check if we managed to parse the conditional expression
2135                  correctly.  If not, we will not send this condition
2136                  to the target.  */
2137               if (aexpr)
2138                 continue;
2139             }
2140
2141           /* If we have a NULL bytecode expression, it means something
2142              went wrong or we have a null condition expression.  */
2143           if (!loc->cond_bytecode)
2144             {
2145               null_condition_or_parse_error = 1;
2146               break;
2147             }
2148         }
2149     }
2150
2151   /* If any of these happened, it means we will have to evaluate the conditions
2152      for the location's address on gdb's side.  It is no use keeping bytecodes
2153      for all the other duplicate locations, thus we free all of them here.
2154
2155      This is so we have a finer control over which locations' conditions are
2156      being evaluated by GDB or the remote stub.  */
2157   if (null_condition_or_parse_error)
2158     {
2159       ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2160         {
2161           loc = (*loc2p);
2162           if (is_breakpoint (loc->owner) && loc->pspace->num == bl->pspace->num)
2163             {
2164               /* Only go as far as the first NULL bytecode is
2165                  located.  */
2166               if (!loc->cond_bytecode)
2167                 return;
2168
2169               free_agent_expr (loc->cond_bytecode);
2170               loc->cond_bytecode = NULL;
2171             }
2172         }
2173     }
2174
2175   /* No NULL conditions or failed bytecode generation.  Build a condition list
2176      for this location's address.  */
2177   ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2178     {
2179       loc = (*loc2p);
2180       if (loc->cond
2181           && is_breakpoint (loc->owner)
2182           && loc->pspace->num == bl->pspace->num
2183           && loc->owner->enable_state == bp_enabled
2184           && loc->enabled)
2185         /* Add the condition to the vector.  This will be used later to send the
2186            conditions to the target.  */
2187         VEC_safe_push (agent_expr_p, bl->target_info.conditions,
2188                        loc->cond_bytecode);
2189     }
2190
2191   return;
2192 }
2193
2194 /* Parses a command described by string CMD into an agent expression
2195    bytecode suitable for evaluation by the bytecode interpreter.
2196    Return NULL if there was any error during parsing.  */
2197
2198 static struct agent_expr *
2199 parse_cmd_to_aexpr (CORE_ADDR scope, char *cmd)
2200 {
2201   struct cleanup *old_cleanups = 0;
2202   struct expression *expr, **argvec;
2203   struct agent_expr *aexpr = NULL;
2204   volatile struct gdb_exception ex;
2205   const char *cmdrest;
2206   const char *format_start, *format_end;
2207   struct format_piece *fpieces;
2208   int nargs;
2209   struct gdbarch *gdbarch = get_current_arch ();
2210
2211   if (!cmd)
2212     return NULL;
2213
2214   cmdrest = cmd;
2215
2216   if (*cmdrest == ',')
2217     ++cmdrest;
2218   cmdrest = skip_spaces_const (cmdrest);
2219
2220   if (*cmdrest++ != '"')
2221     error (_("No format string following the location"));
2222
2223   format_start = cmdrest;
2224
2225   fpieces = parse_format_string (&cmdrest);
2226
2227   old_cleanups = make_cleanup (free_format_pieces_cleanup, &fpieces);
2228
2229   format_end = cmdrest;
2230
2231   if (*cmdrest++ != '"')
2232     error (_("Bad format string, non-terminated '\"'."));
2233   
2234   cmdrest = skip_spaces_const (cmdrest);
2235
2236   if (!(*cmdrest == ',' || *cmdrest == '\0'))
2237     error (_("Invalid argument syntax"));
2238
2239   if (*cmdrest == ',')
2240     cmdrest++;
2241   cmdrest = skip_spaces_const (cmdrest);
2242
2243   /* For each argument, make an expression.  */
2244
2245   argvec = (struct expression **) alloca (strlen (cmd)
2246                                          * sizeof (struct expression *));
2247
2248   nargs = 0;
2249   while (*cmdrest != '\0')
2250     {
2251       const char *cmd1;
2252
2253       cmd1 = cmdrest;
2254       expr = parse_exp_1 (&cmd1, scope, block_for_pc (scope), 1);
2255       argvec[nargs++] = expr;
2256       cmdrest = cmd1;
2257       if (*cmdrest == ',')
2258         ++cmdrest;
2259     }
2260
2261   /* We don't want to stop processing, so catch any errors
2262      that may show up.  */
2263   TRY_CATCH (ex, RETURN_MASK_ERROR)
2264     {
2265       aexpr = gen_printf (scope, gdbarch, 0, 0,
2266                           format_start, format_end - format_start,
2267                           fpieces, nargs, argvec);
2268     }
2269
2270   do_cleanups (old_cleanups);
2271
2272   if (ex.reason < 0)
2273     {
2274       /* If we got here, it means the command could not be parsed to a valid
2275          bytecode expression and thus can't be evaluated on the target's side.
2276          It's no use iterating through the other commands.  */
2277       return NULL;
2278     }
2279
2280   /* We have a valid agent expression, return it.  */
2281   return aexpr;
2282 }
2283
2284 /* Based on location BL, create a list of breakpoint commands to be
2285    passed on to the target.  If we have duplicated locations with
2286    different commands, we will add any such to the list.  */
2287
2288 static void
2289 build_target_command_list (struct bp_location *bl)
2290 {
2291   struct bp_location **locp = NULL, **loc2p;
2292   int null_command_or_parse_error = 0;
2293   int modified = bl->needs_update;
2294   struct bp_location *loc;
2295
2296   /* Release commands left over from a previous insert.  */
2297   VEC_free (agent_expr_p, bl->target_info.tcommands);
2298
2299   /* For now, limit to agent-style dprintf breakpoints.  */
2300   if (bl->owner->type != bp_dprintf
2301       || strcmp (dprintf_style, dprintf_style_agent) != 0)
2302     return;
2303
2304   if (!target_can_run_breakpoint_commands ())
2305     return;
2306
2307   /* Do a first pass to check for locations with no assigned
2308      conditions or conditions that fail to parse to a valid agent expression
2309      bytecode.  If any of these happen, then it's no use to send conditions
2310      to the target since this location will always trigger and generate a
2311      response back to GDB.  */
2312   ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2313     {
2314       loc = (*loc2p);
2315       if (is_breakpoint (loc->owner) && loc->pspace->num == bl->pspace->num)
2316         {
2317           if (modified)
2318             {
2319               struct agent_expr *aexpr;
2320
2321               /* Re-parse the commands since something changed.  In that
2322                  case we already freed the command bytecodes (see
2323                  force_breakpoint_reinsertion).  We just
2324                  need to parse the command to bytecodes again.  */
2325               aexpr = parse_cmd_to_aexpr (bl->address,
2326                                           loc->owner->extra_string);
2327               loc->cmd_bytecode = aexpr;
2328
2329               if (!aexpr)
2330                 continue;
2331             }
2332
2333           /* If we have a NULL bytecode expression, it means something
2334              went wrong or we have a null command expression.  */
2335           if (!loc->cmd_bytecode)
2336             {
2337               null_command_or_parse_error = 1;
2338               break;
2339             }
2340         }
2341     }
2342
2343   /* If anything failed, then we're not doing target-side commands,
2344      and so clean up.  */
2345   if (null_command_or_parse_error)
2346     {
2347       ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2348         {
2349           loc = (*loc2p);
2350           if (is_breakpoint (loc->owner)
2351               && loc->pspace->num == bl->pspace->num)
2352             {
2353               /* Only go as far as the first NULL bytecode is
2354                  located.  */
2355               if (loc->cmd_bytecode == NULL)
2356                 return;
2357
2358               free_agent_expr (loc->cmd_bytecode);
2359               loc->cmd_bytecode = NULL;
2360             }
2361         }
2362     }
2363
2364   /* No NULL commands or failed bytecode generation.  Build a command list
2365      for this location's address.  */
2366   ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2367     {
2368       loc = (*loc2p);
2369       if (loc->owner->extra_string
2370           && is_breakpoint (loc->owner)
2371           && loc->pspace->num == bl->pspace->num
2372           && loc->owner->enable_state == bp_enabled
2373           && loc->enabled)
2374         /* Add the command to the vector.  This will be used later
2375            to send the commands to the target.  */
2376         VEC_safe_push (agent_expr_p, bl->target_info.tcommands,
2377                        loc->cmd_bytecode);
2378     }
2379
2380   bl->target_info.persist = 0;
2381   /* Maybe flag this location as persistent.  */
2382   if (bl->owner->type == bp_dprintf && disconnected_dprintf)
2383     bl->target_info.persist = 1;
2384 }
2385
2386 /* Insert a low-level "breakpoint" of some type.  BL is the breakpoint
2387    location.  Any error messages are printed to TMP_ERROR_STREAM; and
2388    DISABLED_BREAKS, and HW_BREAKPOINT_ERROR are used to report problems.
2389    Returns 0 for success, 1 if the bp_location type is not supported or
2390    -1 for failure.
2391
2392    NOTE drow/2003-09-09: This routine could be broken down to an
2393    object-style method for each breakpoint or catchpoint type.  */
2394 static int
2395 insert_bp_location (struct bp_location *bl,
2396                     struct ui_file *tmp_error_stream,
2397                     int *disabled_breaks,
2398                     int *hw_breakpoint_error,
2399                     int *hw_bp_error_explained_already)
2400 {
2401   enum errors bp_err = GDB_NO_ERROR;
2402   const char *bp_err_message = NULL;
2403   volatile struct gdb_exception e;
2404
2405   if (!should_be_inserted (bl) || (bl->inserted && !bl->needs_update))
2406     return 0;
2407
2408   /* Note we don't initialize bl->target_info, as that wipes out
2409      the breakpoint location's shadow_contents if the breakpoint
2410      is still inserted at that location.  This in turn breaks
2411      target_read_memory which depends on these buffers when
2412      a memory read is requested at the breakpoint location:
2413      Once the target_info has been wiped, we fail to see that
2414      we have a breakpoint inserted at that address and thus
2415      read the breakpoint instead of returning the data saved in
2416      the breakpoint location's shadow contents.  */
2417   bl->target_info.placed_address = bl->address;
2418   bl->target_info.placed_address_space = bl->pspace->aspace;
2419   bl->target_info.length = bl->length;
2420
2421   /* When working with target-side conditions, we must pass all the conditions
2422      for the same breakpoint address down to the target since GDB will not
2423      insert those locations.  With a list of breakpoint conditions, the target
2424      can decide when to stop and notify GDB.  */
2425
2426   if (is_breakpoint (bl->owner))
2427     {
2428       build_target_condition_list (bl);
2429       build_target_command_list (bl);
2430       /* Reset the modification marker.  */
2431       bl->needs_update = 0;
2432     }
2433
2434   if (bl->loc_type == bp_loc_software_breakpoint
2435       || bl->loc_type == bp_loc_hardware_breakpoint)
2436     {
2437       if (bl->owner->type != bp_hardware_breakpoint)
2438         {
2439           /* If the explicitly specified breakpoint type
2440              is not hardware breakpoint, check the memory map to see
2441              if the breakpoint address is in read only memory or not.
2442
2443              Two important cases are:
2444              - location type is not hardware breakpoint, memory
2445              is readonly.  We change the type of the location to
2446              hardware breakpoint.
2447              - location type is hardware breakpoint, memory is
2448              read-write.  This means we've previously made the
2449              location hardware one, but then the memory map changed,
2450              so we undo.
2451              
2452              When breakpoints are removed, remove_breakpoints will use
2453              location types we've just set here, the only possible
2454              problem is that memory map has changed during running
2455              program, but it's not going to work anyway with current
2456              gdb.  */
2457           struct mem_region *mr 
2458             = lookup_mem_region (bl->target_info.placed_address);
2459           
2460           if (mr)
2461             {
2462               if (automatic_hardware_breakpoints)
2463                 {
2464                   enum bp_loc_type new_type;
2465                   
2466                   if (mr->attrib.mode != MEM_RW)
2467                     new_type = bp_loc_hardware_breakpoint;
2468                   else 
2469                     new_type = bp_loc_software_breakpoint;
2470                   
2471                   if (new_type != bl->loc_type)
2472                     {
2473                       static int said = 0;
2474
2475                       bl->loc_type = new_type;
2476                       if (!said)
2477                         {
2478                           fprintf_filtered (gdb_stdout,
2479                                             _("Note: automatically using "
2480                                               "hardware breakpoints for "
2481                                               "read-only addresses.\n"));
2482                           said = 1;
2483                         }
2484                     }
2485                 }
2486               else if (bl->loc_type == bp_loc_software_breakpoint
2487                        && mr->attrib.mode != MEM_RW)        
2488                 warning (_("cannot set software breakpoint "
2489                            "at readonly address %s"),
2490                          paddress (bl->gdbarch, bl->address));
2491             }
2492         }
2493         
2494       /* First check to see if we have to handle an overlay.  */
2495       if (overlay_debugging == ovly_off
2496           || bl->section == NULL
2497           || !(section_is_overlay (bl->section)))
2498         {
2499           /* No overlay handling: just set the breakpoint.  */
2500           TRY_CATCH (e, RETURN_MASK_ALL)
2501             {
2502               int val;
2503
2504               val = bl->owner->ops->insert_location (bl);
2505               if (val)
2506                 bp_err = GENERIC_ERROR;
2507             }
2508           if (e.reason < 0)
2509             {
2510               bp_err = e.error;
2511               bp_err_message = e.message;
2512             }
2513         }
2514       else
2515         {
2516           /* This breakpoint is in an overlay section.
2517              Shall we set a breakpoint at the LMA?  */
2518           if (!overlay_events_enabled)
2519             {
2520               /* Yes -- overlay event support is not active, 
2521                  so we must try to set a breakpoint at the LMA.
2522                  This will not work for a hardware breakpoint.  */
2523               if (bl->loc_type == bp_loc_hardware_breakpoint)
2524                 warning (_("hardware breakpoint %d not supported in overlay!"),
2525                          bl->owner->number);
2526               else
2527                 {
2528                   CORE_ADDR addr = overlay_unmapped_address (bl->address,
2529                                                              bl->section);
2530                   /* Set a software (trap) breakpoint at the LMA.  */
2531                   bl->overlay_target_info = bl->target_info;
2532                   bl->overlay_target_info.placed_address = addr;
2533
2534                   /* No overlay handling: just set the breakpoint.  */
2535                   TRY_CATCH (e, RETURN_MASK_ALL)
2536                     {
2537                       int val;
2538
2539                       val = target_insert_breakpoint (bl->gdbarch,
2540                                                       &bl->overlay_target_info);
2541                       if (val)
2542                         bp_err = GENERIC_ERROR;
2543                     }
2544                   if (e.reason < 0)
2545                     {
2546                       bp_err = e.error;
2547                       bp_err_message = e.message;
2548                     }
2549
2550                   if (bp_err != GDB_NO_ERROR)
2551                     fprintf_unfiltered (tmp_error_stream,
2552                                         "Overlay breakpoint %d "
2553                                         "failed: in ROM?\n",
2554                                         bl->owner->number);
2555                 }
2556             }
2557           /* Shall we set a breakpoint at the VMA? */
2558           if (section_is_mapped (bl->section))
2559             {
2560               /* Yes.  This overlay section is mapped into memory.  */
2561               TRY_CATCH (e, RETURN_MASK_ALL)
2562                 {
2563                   int val;
2564
2565                   val = bl->owner->ops->insert_location (bl);
2566                   if (val)
2567                     bp_err = GENERIC_ERROR;
2568                 }
2569               if (e.reason < 0)
2570                 {
2571                   bp_err = e.error;
2572                   bp_err_message = e.message;
2573                 }
2574             }
2575           else
2576             {
2577               /* No.  This breakpoint will not be inserted.  
2578                  No error, but do not mark the bp as 'inserted'.  */
2579               return 0;
2580             }
2581         }
2582
2583       if (bp_err != GDB_NO_ERROR)
2584         {
2585           /* Can't set the breakpoint.  */
2586
2587           /* In some cases, we might not be able to insert a
2588              breakpoint in a shared library that has already been
2589              removed, but we have not yet processed the shlib unload
2590              event.  Unfortunately, some targets that implement
2591              breakpoint insertion themselves (necessary if this is a
2592              HW breakpoint, but SW breakpoints likewise) can't tell
2593              why the breakpoint insertion failed (e.g., the remote
2594              target doesn't define error codes), so we must treat
2595              generic errors as memory errors.  */
2596           if ((bp_err == GENERIC_ERROR || bp_err == MEMORY_ERROR)
2597               && solib_name_from_address (bl->pspace, bl->address))
2598             {
2599               /* See also: disable_breakpoints_in_shlibs.  */
2600               bl->shlib_disabled = 1;
2601               observer_notify_breakpoint_modified (bl->owner);
2602               if (!*disabled_breaks)
2603                 {
2604                   fprintf_unfiltered (tmp_error_stream, 
2605                                       "Cannot insert breakpoint %d.\n", 
2606                                       bl->owner->number);
2607                   fprintf_unfiltered (tmp_error_stream, 
2608                                       "Temporarily disabling shared "
2609                                       "library breakpoints:\n");
2610                 }
2611               *disabled_breaks = 1;
2612               fprintf_unfiltered (tmp_error_stream,
2613                                   "breakpoint #%d\n", bl->owner->number);
2614               return 0;
2615             }
2616           else
2617             {
2618               if (bl->loc_type == bp_loc_hardware_breakpoint)
2619                 {
2620                   *hw_breakpoint_error = 1;
2621                   *hw_bp_error_explained_already = bp_err_message != NULL;
2622                   fprintf_unfiltered (tmp_error_stream,
2623                                       "Cannot insert hardware breakpoint %d%s",
2624                                       bl->owner->number, bp_err_message ? ":" : ".\n");
2625                   if (bp_err_message != NULL)
2626                     fprintf_unfiltered (tmp_error_stream, "%s.\n", bp_err_message);
2627                 }
2628               else
2629                 {
2630                   if (bp_err_message == NULL)
2631                     {
2632                       char *message
2633                         = memory_error_message (TARGET_XFER_E_IO,
2634                                                 bl->gdbarch, bl->address);
2635                       struct cleanup *old_chain = make_cleanup (xfree, message);
2636
2637                       fprintf_unfiltered (tmp_error_stream,
2638                                           "Cannot insert breakpoint %d.\n"
2639                                           "%s\n",
2640                                           bl->owner->number, message);
2641                       do_cleanups (old_chain);
2642                     }
2643                   else
2644                     {
2645                       fprintf_unfiltered (tmp_error_stream,
2646                                           "Cannot insert breakpoint %d: %s\n",
2647                                           bl->owner->number,
2648                                           bp_err_message);
2649                     }
2650                 }
2651               return 1;
2652
2653             }
2654         }
2655       else
2656         bl->inserted = 1;
2657
2658       return 0;
2659     }
2660
2661   else if (bl->loc_type == bp_loc_hardware_watchpoint
2662            /* NOTE drow/2003-09-08: This state only exists for removing
2663               watchpoints.  It's not clear that it's necessary...  */
2664            && bl->owner->disposition != disp_del_at_next_stop)
2665     {
2666       int val;
2667
2668       gdb_assert (bl->owner->ops != NULL
2669                   && bl->owner->ops->insert_location != NULL);
2670
2671       val = bl->owner->ops->insert_location (bl);
2672
2673       /* If trying to set a read-watchpoint, and it turns out it's not
2674          supported, try emulating one with an access watchpoint.  */
2675       if (val == 1 && bl->watchpoint_type == hw_read)
2676         {
2677           struct bp_location *loc, **loc_temp;
2678
2679           /* But don't try to insert it, if there's already another
2680              hw_access location that would be considered a duplicate
2681              of this one.  */
2682           ALL_BP_LOCATIONS (loc, loc_temp)
2683             if (loc != bl
2684                 && loc->watchpoint_type == hw_access
2685                 && watchpoint_locations_match (bl, loc))
2686               {
2687                 bl->duplicate = 1;
2688                 bl->inserted = 1;
2689                 bl->target_info = loc->target_info;
2690                 bl->watchpoint_type = hw_access;
2691                 val = 0;
2692                 break;
2693               }
2694
2695           if (val == 1)
2696             {
2697               bl->watchpoint_type = hw_access;
2698               val = bl->owner->ops->insert_location (bl);
2699
2700               if (val)
2701                 /* Back to the original value.  */
2702                 bl->watchpoint_type = hw_read;
2703             }
2704         }
2705
2706       bl->inserted = (val == 0);
2707     }
2708
2709   else if (bl->owner->type == bp_catchpoint)
2710     {
2711       int val;
2712
2713       gdb_assert (bl->owner->ops != NULL
2714                   && bl->owner->ops->insert_location != NULL);
2715
2716       val = bl->owner->ops->insert_location (bl);
2717       if (val)
2718         {
2719           bl->owner->enable_state = bp_disabled;
2720
2721           if (val == 1)
2722             warning (_("\
2723 Error inserting catchpoint %d: Your system does not support this type\n\
2724 of catchpoint."), bl->owner->number);
2725           else
2726             warning (_("Error inserting catchpoint %d."), bl->owner->number);
2727         }
2728
2729       bl->inserted = (val == 0);
2730
2731       /* We've already printed an error message if there was a problem
2732          inserting this catchpoint, and we've disabled the catchpoint,
2733          so just return success.  */
2734       return 0;
2735     }
2736
2737   return 0;
2738 }
2739
2740 /* This function is called when program space PSPACE is about to be
2741    deleted.  It takes care of updating breakpoints to not reference
2742    PSPACE anymore.  */
2743
2744 void
2745 breakpoint_program_space_exit (struct program_space *pspace)
2746 {
2747   struct breakpoint *b, *b_temp;
2748   struct bp_location *loc, **loc_temp;
2749
2750   /* Remove any breakpoint that was set through this program space.  */
2751   ALL_BREAKPOINTS_SAFE (b, b_temp)
2752     {
2753       if (b->pspace == pspace)
2754         delete_breakpoint (b);
2755     }
2756
2757   /* Breakpoints set through other program spaces could have locations
2758      bound to PSPACE as well.  Remove those.  */
2759   ALL_BP_LOCATIONS (loc, loc_temp)
2760     {
2761       struct bp_location *tmp;
2762
2763       if (loc->pspace == pspace)
2764         {
2765           /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL.  */
2766           if (loc->owner->loc == loc)
2767             loc->owner->loc = loc->next;
2768           else
2769             for (tmp = loc->owner->loc; tmp->next != NULL; tmp = tmp->next)
2770               if (tmp->next == loc)
2771                 {
2772                   tmp->next = loc->next;
2773                   break;
2774                 }
2775         }
2776     }
2777
2778   /* Now update the global location list to permanently delete the
2779      removed locations above.  */
2780   update_global_location_list (0);
2781 }
2782
2783 /* Make sure all breakpoints are inserted in inferior.
2784    Throws exception on any error.
2785    A breakpoint that is already inserted won't be inserted
2786    again, so calling this function twice is safe.  */
2787 void
2788 insert_breakpoints (void)
2789 {
2790   struct breakpoint *bpt;
2791
2792   ALL_BREAKPOINTS (bpt)
2793     if (is_hardware_watchpoint (bpt))
2794       {
2795         struct watchpoint *w = (struct watchpoint *) bpt;
2796
2797         update_watchpoint (w, 0 /* don't reparse.  */);
2798       }
2799
2800   update_global_location_list (1);
2801
2802   /* update_global_location_list does not insert breakpoints when
2803      always_inserted_mode is not enabled.  Explicitly insert them
2804      now.  */
2805   if (!breakpoints_always_inserted_mode ())
2806     insert_breakpoint_locations ();
2807 }
2808
2809 /* Invoke CALLBACK for each of bp_location.  */
2810
2811 void
2812 iterate_over_bp_locations (walk_bp_location_callback callback)
2813 {
2814   struct bp_location *loc, **loc_tmp;
2815
2816   ALL_BP_LOCATIONS (loc, loc_tmp)
2817     {
2818       callback (loc, NULL);
2819     }
2820 }
2821
2822 /* This is used when we need to synch breakpoint conditions between GDB and the
2823    target.  It is the case with deleting and disabling of breakpoints when using
2824    always-inserted mode.  */
2825
2826 static void
2827 update_inserted_breakpoint_locations (void)
2828 {
2829   struct bp_location *bl, **blp_tmp;
2830   int error_flag = 0;
2831   int val = 0;
2832   int disabled_breaks = 0;
2833   int hw_breakpoint_error = 0;
2834   int hw_bp_details_reported = 0;
2835
2836   struct ui_file *tmp_error_stream = mem_fileopen ();
2837   struct cleanup *cleanups = make_cleanup_ui_file_delete (tmp_error_stream);
2838
2839   /* Explicitly mark the warning -- this will only be printed if
2840      there was an error.  */
2841   fprintf_unfiltered (tmp_error_stream, "Warning:\n");
2842
2843   save_current_space_and_thread ();
2844
2845   ALL_BP_LOCATIONS (bl, blp_tmp)
2846     {
2847       /* We only want to update software breakpoints and hardware
2848          breakpoints.  */
2849       if (!is_breakpoint (bl->owner))
2850         continue;
2851
2852       /* We only want to update locations that are already inserted
2853          and need updating.  This is to avoid unwanted insertion during
2854          deletion of breakpoints.  */
2855       if (!bl->inserted || (bl->inserted && !bl->needs_update))
2856         continue;
2857
2858       switch_to_program_space_and_thread (bl->pspace);
2859
2860       /* For targets that support global breakpoints, there's no need
2861          to select an inferior to insert breakpoint to.  In fact, even
2862          if we aren't attached to any process yet, we should still
2863          insert breakpoints.  */
2864       if (!gdbarch_has_global_breakpoints (target_gdbarch ())
2865           && ptid_equal (inferior_ptid, null_ptid))
2866         continue;
2867
2868       val = insert_bp_location (bl, tmp_error_stream, &disabled_breaks,
2869                                     &hw_breakpoint_error, &hw_bp_details_reported);
2870       if (val)
2871         error_flag = val;
2872     }
2873
2874   if (error_flag)
2875     {
2876       target_terminal_ours_for_output ();
2877       error_stream (tmp_error_stream);
2878     }
2879
2880   do_cleanups (cleanups);
2881 }
2882
2883 /* Used when starting or continuing the program.  */
2884
2885 static void
2886 insert_breakpoint_locations (void)
2887 {
2888   struct breakpoint *bpt;
2889   struct bp_location *bl, **blp_tmp;
2890   int error_flag = 0;
2891   int val = 0;
2892   int disabled_breaks = 0;
2893   int hw_breakpoint_error = 0;
2894   int hw_bp_error_explained_already = 0;
2895
2896   struct ui_file *tmp_error_stream = mem_fileopen ();
2897   struct cleanup *cleanups = make_cleanup_ui_file_delete (tmp_error_stream);
2898   
2899   /* Explicitly mark the warning -- this will only be printed if
2900      there was an error.  */
2901   fprintf_unfiltered (tmp_error_stream, "Warning:\n");
2902
2903   save_current_space_and_thread ();
2904
2905   ALL_BP_LOCATIONS (bl, blp_tmp)
2906     {
2907       if (!should_be_inserted (bl) || (bl->inserted && !bl->needs_update))
2908         continue;
2909
2910       /* There is no point inserting thread-specific breakpoints if
2911          the thread no longer exists.  ALL_BP_LOCATIONS bp_location
2912          has BL->OWNER always non-NULL.  */
2913       if (bl->owner->thread != -1
2914           && !valid_thread_id (bl->owner->thread))
2915         continue;
2916
2917       switch_to_program_space_and_thread (bl->pspace);
2918
2919       /* For targets that support global breakpoints, there's no need
2920          to select an inferior to insert breakpoint to.  In fact, even
2921          if we aren't attached to any process yet, we should still
2922          insert breakpoints.  */
2923       if (!gdbarch_has_global_breakpoints (target_gdbarch ())
2924           && ptid_equal (inferior_ptid, null_ptid))
2925         continue;
2926
2927       val = insert_bp_location (bl, tmp_error_stream, &disabled_breaks,
2928                                     &hw_breakpoint_error, &hw_bp_error_explained_already);
2929       if (val)
2930         error_flag = val;
2931     }
2932
2933   /* If we failed to insert all locations of a watchpoint, remove
2934      them, as half-inserted watchpoint is of limited use.  */
2935   ALL_BREAKPOINTS (bpt)  
2936     {
2937       int some_failed = 0;
2938       struct bp_location *loc;
2939
2940       if (!is_hardware_watchpoint (bpt))
2941         continue;
2942
2943       if (!breakpoint_enabled (bpt))
2944         continue;
2945
2946       if (bpt->disposition == disp_del_at_next_stop)
2947         continue;
2948       
2949       for (loc = bpt->loc; loc; loc = loc->next)
2950         if (!loc->inserted && should_be_inserted (loc))
2951           {
2952             some_failed = 1;
2953             break;
2954           }
2955       if (some_failed)
2956         {
2957           for (loc = bpt->loc; loc; loc = loc->next)
2958             if (loc->inserted)
2959               remove_breakpoint (loc, mark_uninserted);
2960
2961           hw_breakpoint_error = 1;
2962           fprintf_unfiltered (tmp_error_stream,
2963                               "Could not insert hardware watchpoint %d.\n", 
2964                               bpt->number);
2965           error_flag = -1;
2966         }
2967     }
2968
2969   if (error_flag)
2970     {
2971       /* If a hardware breakpoint or watchpoint was inserted, add a
2972          message about possibly exhausted resources.  */
2973       if (hw_breakpoint_error && !hw_bp_error_explained_already)
2974         {
2975           fprintf_unfiltered (tmp_error_stream, 
2976                               "Could not insert hardware breakpoints:\n\
2977 You may have requested too many hardware breakpoints/watchpoints.\n");
2978         }
2979       target_terminal_ours_for_output ();
2980       error_stream (tmp_error_stream);
2981     }
2982
2983   do_cleanups (cleanups);
2984 }
2985
2986 /* Used when the program stops.
2987    Returns zero if successful, or non-zero if there was a problem
2988    removing a breakpoint location.  */
2989
2990 int
2991 remove_breakpoints (void)
2992 {
2993   struct bp_location *bl, **blp_tmp;
2994   int val = 0;
2995
2996   ALL_BP_LOCATIONS (bl, blp_tmp)
2997   {
2998     if (bl->inserted && !is_tracepoint (bl->owner))
2999       val |= remove_breakpoint (bl, mark_uninserted);
3000   }
3001   return val;
3002 }
3003
3004 /* When a thread exits, remove breakpoints that are related to
3005    that thread.  */
3006
3007 static void
3008 remove_threaded_breakpoints (struct thread_info *tp, int silent)
3009 {
3010   struct breakpoint *b, *b_tmp;
3011
3012   ALL_BREAKPOINTS_SAFE (b, b_tmp)
3013     {
3014       if (b->thread == tp->num && user_breakpoint_p (b))
3015         {
3016           b->disposition = disp_del_at_next_stop;
3017
3018           printf_filtered (_("\
3019 Thread-specific breakpoint %d deleted - thread %d no longer in the thread list.\n"),
3020                           b->number, tp->num);
3021
3022           /* Hide it from the user.  */
3023           b->number = 0;
3024        }
3025     }
3026 }
3027
3028 /* Remove breakpoints of process PID.  */
3029
3030 int
3031 remove_breakpoints_pid (int pid)
3032 {
3033   struct bp_location *bl, **blp_tmp;
3034   int val;
3035   struct inferior *inf = find_inferior_pid (pid);
3036
3037   ALL_BP_LOCATIONS (bl, blp_tmp)
3038   {
3039     if (bl->pspace != inf->pspace)
3040       continue;
3041
3042     if (bl->owner->type == bp_dprintf)
3043       continue;
3044
3045     if (bl->inserted)
3046       {
3047         val = remove_breakpoint (bl, mark_uninserted);
3048         if (val != 0)
3049           return val;
3050       }
3051   }
3052   return 0;
3053 }
3054
3055 int
3056 reattach_breakpoints (int pid)
3057 {
3058   struct cleanup *old_chain;
3059   struct bp_location *bl, **blp_tmp;
3060   int val;
3061   struct ui_file *tmp_error_stream;
3062   int dummy1 = 0, dummy2 = 0, dummy3 = 0;
3063   struct inferior *inf;
3064   struct thread_info *tp;
3065
3066   tp = any_live_thread_of_process (pid);
3067   if (tp == NULL)
3068     return 1;
3069
3070   inf = find_inferior_pid (pid);
3071   old_chain = save_inferior_ptid ();
3072
3073   inferior_ptid = tp->ptid;
3074
3075   tmp_error_stream = mem_fileopen ();
3076   make_cleanup_ui_file_delete (tmp_error_stream);
3077
3078   ALL_BP_LOCATIONS (bl, blp_tmp)
3079   {
3080     if (bl->pspace != inf->pspace)
3081       continue;
3082
3083     if (bl->inserted)
3084       {
3085         bl->inserted = 0;
3086         val = insert_bp_location (bl, tmp_error_stream, &dummy1, &dummy2, &dummy3);
3087         if (val != 0)
3088           {
3089             do_cleanups (old_chain);
3090             return val;
3091           }
3092       }
3093   }
3094   do_cleanups (old_chain);
3095   return 0;
3096 }
3097
3098 static int internal_breakpoint_number = -1;
3099
3100 /* Set the breakpoint number of B, depending on the value of INTERNAL.
3101    If INTERNAL is non-zero, the breakpoint number will be populated
3102    from internal_breakpoint_number and that variable decremented.
3103    Otherwise the breakpoint number will be populated from
3104    breakpoint_count and that value incremented.  Internal breakpoints
3105    do not set the internal var bpnum.  */
3106 static void
3107 set_breakpoint_number (int internal, struct breakpoint *b)
3108 {
3109   if (internal)
3110     b->number = internal_breakpoint_number--;
3111   else
3112     {
3113       set_breakpoint_count (breakpoint_count + 1);
3114       b->number = breakpoint_count;
3115     }
3116 }
3117
3118 static struct breakpoint *
3119 create_internal_breakpoint (struct gdbarch *gdbarch,
3120                             CORE_ADDR address, enum bptype type,
3121                             const struct breakpoint_ops *ops)
3122 {
3123   struct symtab_and_line sal;
3124   struct breakpoint *b;
3125
3126   init_sal (&sal);              /* Initialize to zeroes.  */
3127
3128   sal.pc = address;
3129   sal.section = find_pc_overlay (sal.pc);
3130   sal.pspace = current_program_space;
3131
3132   b = set_raw_breakpoint (gdbarch, sal, type, ops);
3133   b->number = internal_breakpoint_number--;
3134   b->disposition = disp_donttouch;
3135
3136   return b;
3137 }
3138
3139 static const char *const longjmp_names[] =
3140   {
3141     "longjmp", "_longjmp", "siglongjmp", "_siglongjmp"
3142   };
3143 #define NUM_LONGJMP_NAMES ARRAY_SIZE(longjmp_names)
3144
3145 /* Per-objfile data private to breakpoint.c.  */
3146 struct breakpoint_objfile_data
3147 {
3148   /* Minimal symbol for "_ovly_debug_event" (if any).  */
3149   struct bound_minimal_symbol overlay_msym;
3150
3151   /* Minimal symbol(s) for "longjmp", "siglongjmp", etc. (if any).  */
3152   struct bound_minimal_symbol longjmp_msym[NUM_LONGJMP_NAMES];
3153
3154   /* True if we have looked for longjmp probes.  */
3155   int longjmp_searched;
3156
3157   /* SystemTap probe points for longjmp (if any).  */
3158   VEC (probe_p) *longjmp_probes;
3159
3160   /* Minimal symbol for "std::terminate()" (if any).  */
3161   struct bound_minimal_symbol terminate_msym;
3162
3163   /* Minimal symbol for "_Unwind_DebugHook" (if any).  */
3164   struct bound_minimal_symbol exception_msym;
3165
3166   /* True if we have looked for exception probes.  */
3167   int exception_searched;
3168
3169   /* SystemTap probe points for unwinding (if any).  */
3170   VEC (probe_p) *exception_probes;
3171 };
3172
3173 static const struct objfile_data *breakpoint_objfile_key;
3174
3175 /* Minimal symbol not found sentinel.  */
3176 static struct minimal_symbol msym_not_found;
3177
3178 /* Returns TRUE if MSYM point to the "not found" sentinel.  */
3179
3180 static int
3181 msym_not_found_p (const struct minimal_symbol *msym)
3182 {
3183   return msym == &msym_not_found;
3184 }
3185
3186 /* Return per-objfile data needed by breakpoint.c.
3187    Allocate the data if necessary.  */
3188
3189 static struct breakpoint_objfile_data *
3190 get_breakpoint_objfile_data (struct objfile *objfile)
3191 {
3192   struct breakpoint_objfile_data *bp_objfile_data;
3193
3194   bp_objfile_data = objfile_data (objfile, breakpoint_objfile_key);
3195   if (bp_objfile_data == NULL)
3196     {
3197       bp_objfile_data = obstack_alloc (&objfile->objfile_obstack,
3198                                        sizeof (*bp_objfile_data));
3199
3200       memset (bp_objfile_data, 0, sizeof (*bp_objfile_data));
3201       set_objfile_data (objfile, breakpoint_objfile_key, bp_objfile_data);
3202     }
3203   return bp_objfile_data;
3204 }
3205
3206 static void
3207 free_breakpoint_probes (struct objfile *obj, void *data)
3208 {
3209   struct breakpoint_objfile_data *bp_objfile_data = data;
3210
3211   VEC_free (probe_p, bp_objfile_data->longjmp_probes);
3212   VEC_free (probe_p, bp_objfile_data->exception_probes);
3213 }
3214
3215 static void
3216 create_overlay_event_breakpoint (void)
3217 {
3218   struct objfile *objfile;
3219   const char *const func_name = "_ovly_debug_event";
3220
3221   ALL_OBJFILES (objfile)
3222     {
3223       struct breakpoint *b;
3224       struct breakpoint_objfile_data *bp_objfile_data;
3225       CORE_ADDR addr;
3226
3227       bp_objfile_data = get_breakpoint_objfile_data (objfile);
3228
3229       if (msym_not_found_p (bp_objfile_data->overlay_msym.minsym))
3230         continue;
3231
3232       if (bp_objfile_data->overlay_msym.minsym == NULL)
3233         {
3234           struct bound_minimal_symbol m;
3235
3236           m = lookup_minimal_symbol_text (func_name, objfile);
3237           if (m.minsym == NULL)
3238             {
3239               /* Avoid future lookups in this objfile.  */
3240               bp_objfile_data->overlay_msym.minsym = &msym_not_found;
3241               continue;
3242             }
3243           bp_objfile_data->overlay_msym = m;
3244         }
3245
3246       addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->overlay_msym);
3247       b = create_internal_breakpoint (get_objfile_arch (objfile), addr,
3248                                       bp_overlay_event,
3249                                       &internal_breakpoint_ops);
3250       b->addr_string = xstrdup (func_name);
3251
3252       if (overlay_debugging == ovly_auto)
3253         {
3254           b->enable_state = bp_enabled;
3255           overlay_events_enabled = 1;
3256         }
3257       else
3258        {
3259          b->enable_state = bp_disabled;
3260          overlay_events_enabled = 0;
3261        }
3262     }
3263   update_global_location_list (1);
3264 }
3265
3266 static void
3267 create_longjmp_master_breakpoint (void)
3268 {
3269   struct program_space *pspace;
3270   struct cleanup *old_chain;
3271
3272   old_chain = save_current_program_space ();
3273
3274   ALL_PSPACES (pspace)
3275   {
3276     struct objfile *objfile;
3277
3278     set_current_program_space (pspace);
3279
3280     ALL_OBJFILES (objfile)
3281     {
3282       int i;
3283       struct gdbarch *gdbarch;
3284       struct breakpoint_objfile_data *bp_objfile_data;
3285
3286       gdbarch = get_objfile_arch (objfile);
3287
3288       bp_objfile_data = get_breakpoint_objfile_data (objfile);
3289
3290       if (!bp_objfile_data->longjmp_searched)
3291         {
3292           VEC (probe_p) *ret;
3293
3294           ret = find_probes_in_objfile (objfile, "libc", "longjmp");
3295           if (ret != NULL)
3296             {
3297               /* We are only interested in checking one element.  */
3298               struct probe *p = VEC_index (probe_p, ret, 0);
3299
3300               if (!can_evaluate_probe_arguments (p))
3301                 {
3302                   /* We cannot use the probe interface here, because it does
3303                      not know how to evaluate arguments.  */
3304                   VEC_free (probe_p, ret);
3305                   ret = NULL;
3306                 }
3307             }
3308           bp_objfile_data->longjmp_probes = ret;
3309           bp_objfile_data->longjmp_searched = 1;
3310         }
3311
3312       if (bp_objfile_data->longjmp_probes != NULL)
3313         {
3314           int i;
3315           struct probe *probe;
3316           struct gdbarch *gdbarch = get_objfile_arch (objfile);
3317
3318           for (i = 0;
3319                VEC_iterate (probe_p,
3320                             bp_objfile_data->longjmp_probes,
3321                             i, probe);
3322                ++i)
3323             {
3324               struct breakpoint *b;
3325
3326               b = create_internal_breakpoint (gdbarch, probe->address,
3327                                               bp_longjmp_master,
3328                                               &internal_breakpoint_ops);
3329               b->addr_string = xstrdup ("-probe-stap libc:longjmp");
3330               b->enable_state = bp_disabled;
3331             }
3332
3333           continue;
3334         }
3335
3336       if (!gdbarch_get_longjmp_target_p (gdbarch))
3337         continue;
3338
3339       for (i = 0; i < NUM_LONGJMP_NAMES; i++)
3340         {
3341           struct breakpoint *b;
3342           const char *func_name;
3343           CORE_ADDR addr;
3344
3345           if (msym_not_found_p (bp_objfile_data->longjmp_msym[i].minsym))
3346             continue;
3347
3348           func_name = longjmp_names[i];
3349           if (bp_objfile_data->longjmp_msym[i].minsym == NULL)
3350             {
3351               struct bound_minimal_symbol m;
3352
3353               m = lookup_minimal_symbol_text (func_name, objfile);
3354               if (m.minsym == NULL)
3355                 {
3356                   /* Prevent future lookups in this objfile.  */
3357                   bp_objfile_data->longjmp_msym[i].minsym = &msym_not_found;
3358                   continue;
3359                 }
3360               bp_objfile_data->longjmp_msym[i] = m;
3361             }
3362
3363           addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->longjmp_msym[i]);
3364           b = create_internal_breakpoint (gdbarch, addr, bp_longjmp_master,
3365                                           &internal_breakpoint_ops);
3366           b->addr_string = xstrdup (func_name);
3367           b->enable_state = bp_disabled;
3368         }
3369     }
3370   }
3371   update_global_location_list (1);
3372
3373   do_cleanups (old_chain);
3374 }
3375
3376 /* Create a master std::terminate breakpoint.  */
3377 static void
3378 create_std_terminate_master_breakpoint (void)
3379 {
3380   struct program_space *pspace;
3381   struct cleanup *old_chain;
3382   const char *const func_name = "std::terminate()";
3383
3384   old_chain = save_current_program_space ();
3385
3386   ALL_PSPACES (pspace)
3387   {
3388     struct objfile *objfile;
3389     CORE_ADDR addr;
3390
3391     set_current_program_space (pspace);
3392
3393     ALL_OBJFILES (objfile)
3394     {
3395       struct breakpoint *b;
3396       struct breakpoint_objfile_data *bp_objfile_data;
3397
3398       bp_objfile_data = get_breakpoint_objfile_data (objfile);
3399
3400       if (msym_not_found_p (bp_objfile_data->terminate_msym.minsym))
3401         continue;
3402
3403       if (bp_objfile_data->terminate_msym.minsym == NULL)
3404         {
3405           struct bound_minimal_symbol m;
3406
3407           m = lookup_minimal_symbol (func_name, NULL, objfile);
3408           if (m.minsym == NULL || (MSYMBOL_TYPE (m.minsym) != mst_text
3409                                    && MSYMBOL_TYPE (m.minsym) != mst_file_text))
3410             {
3411               /* Prevent future lookups in this objfile.  */
3412               bp_objfile_data->terminate_msym.minsym = &msym_not_found;
3413               continue;
3414             }
3415           bp_objfile_data->terminate_msym = m;
3416         }
3417
3418       addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->terminate_msym);
3419       b = create_internal_breakpoint (get_objfile_arch (objfile), addr,
3420                                       bp_std_terminate_master,
3421                                       &internal_breakpoint_ops);
3422       b->addr_string = xstrdup (func_name);
3423       b->enable_state = bp_disabled;
3424     }
3425   }
3426
3427   update_global_location_list (1);
3428
3429   do_cleanups (old_chain);
3430 }
3431
3432 /* Install a master breakpoint on the unwinder's debug hook.  */
3433
3434 static void
3435 create_exception_master_breakpoint (void)
3436 {
3437   struct objfile *objfile;
3438   const char *const func_name = "_Unwind_DebugHook";
3439
3440   ALL_OBJFILES (objfile)
3441     {
3442       struct breakpoint *b;
3443       struct gdbarch *gdbarch;
3444       struct breakpoint_objfile_data *bp_objfile_data;
3445       CORE_ADDR addr;
3446
3447       bp_objfile_data = get_breakpoint_objfile_data (objfile);
3448
3449       /* We prefer the SystemTap probe point if it exists.  */
3450       if (!bp_objfile_data->exception_searched)
3451         {
3452           VEC (probe_p) *ret;
3453
3454           ret = find_probes_in_objfile (objfile, "libgcc", "unwind");
3455
3456           if (ret != NULL)
3457             {
3458               /* We are only interested in checking one element.  */
3459               struct probe *p = VEC_index (probe_p, ret, 0);
3460
3461               if (!can_evaluate_probe_arguments (p))
3462                 {
3463                   /* We cannot use the probe interface here, because it does
3464                      not know how to evaluate arguments.  */
3465                   VEC_free (probe_p, ret);
3466                   ret = NULL;
3467                 }
3468             }
3469           bp_objfile_data->exception_probes = ret;
3470           bp_objfile_data->exception_searched = 1;
3471         }
3472
3473       if (bp_objfile_data->exception_probes != NULL)
3474         {
3475           struct gdbarch *gdbarch = get_objfile_arch (objfile);
3476           int i;
3477           struct probe *probe;
3478
3479           for (i = 0;
3480                VEC_iterate (probe_p,
3481                             bp_objfile_data->exception_probes,
3482                             i, probe);
3483                ++i)
3484             {
3485               struct breakpoint *b;
3486
3487               b = create_internal_breakpoint (gdbarch, probe->address,
3488                                               bp_exception_master,
3489                                               &internal_breakpoint_ops);
3490               b->addr_string = xstrdup ("-probe-stap libgcc:unwind");
3491               b->enable_state = bp_disabled;
3492             }
3493
3494           continue;
3495         }
3496
3497       /* Otherwise, try the hook function.  */
3498
3499       if (msym_not_found_p (bp_objfile_data->exception_msym.minsym))
3500         continue;
3501
3502       gdbarch = get_objfile_arch (objfile);
3503
3504       if (bp_objfile_data->exception_msym.minsym == NULL)
3505         {
3506           struct bound_minimal_symbol debug_hook;
3507
3508           debug_hook = lookup_minimal_symbol (func_name, NULL, objfile);
3509           if (debug_hook.minsym == NULL)
3510             {
3511               bp_objfile_data->exception_msym.minsym = &msym_not_found;
3512               continue;
3513             }
3514
3515           bp_objfile_data->exception_msym = debug_hook;
3516         }
3517
3518       addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->exception_msym);
3519       addr = gdbarch_convert_from_func_ptr_addr (gdbarch, addr,
3520                                                  &current_target);
3521       b = create_internal_breakpoint (gdbarch, addr, bp_exception_master,
3522                                       &internal_breakpoint_ops);
3523       b->addr_string = xstrdup (func_name);
3524       b->enable_state = bp_disabled;
3525     }
3526
3527   update_global_location_list (1);
3528 }
3529
3530 void
3531 update_breakpoints_after_exec (void)
3532 {
3533   struct breakpoint *b, *b_tmp;
3534   struct bp_location *bploc, **bplocp_tmp;
3535
3536   /* We're about to delete breakpoints from GDB's lists.  If the
3537      INSERTED flag is true, GDB will try to lift the breakpoints by
3538      writing the breakpoints' "shadow contents" back into memory.  The
3539      "shadow contents" are NOT valid after an exec, so GDB should not
3540      do that.  Instead, the target is responsible from marking
3541      breakpoints out as soon as it detects an exec.  We don't do that
3542      here instead, because there may be other attempts to delete
3543      breakpoints after detecting an exec and before reaching here.  */
3544   ALL_BP_LOCATIONS (bploc, bplocp_tmp)
3545     if (bploc->pspace == current_program_space)
3546       gdb_assert (!bploc->inserted);
3547
3548   ALL_BREAKPOINTS_SAFE (b, b_tmp)
3549   {
3550     if (b->pspace != current_program_space)
3551       continue;
3552
3553     /* Solib breakpoints must be explicitly reset after an exec().  */
3554     if (b->type == bp_shlib_event)
3555       {
3556         delete_breakpoint (b);
3557         continue;
3558       }
3559
3560     /* JIT breakpoints must be explicitly reset after an exec().  */
3561     if (b->type == bp_jit_event)
3562       {
3563         delete_breakpoint (b);
3564         continue;
3565       }
3566
3567     /* Thread event breakpoints must be set anew after an exec(),
3568        as must overlay event and longjmp master breakpoints.  */
3569     if (b->type == bp_thread_event || b->type == bp_overlay_event
3570         || b->type == bp_longjmp_master || b->type == bp_std_terminate_master
3571         || b->type == bp_exception_master)
3572       {
3573         delete_breakpoint (b);
3574         continue;
3575       }
3576
3577     /* Step-resume breakpoints are meaningless after an exec().  */
3578     if (b->type == bp_step_resume || b->type == bp_hp_step_resume)
3579       {
3580         delete_breakpoint (b);
3581         continue;
3582       }
3583
3584     /* Longjmp and longjmp-resume breakpoints are also meaningless
3585        after an exec.  */
3586     if (b->type == bp_longjmp || b->type == bp_longjmp_resume
3587         || b->type == bp_longjmp_call_dummy
3588         || b->type == bp_exception || b->type == bp_exception_resume)
3589       {
3590         delete_breakpoint (b);
3591         continue;
3592       }
3593
3594     if (b->type == bp_catchpoint)
3595       {
3596         /* For now, none of the bp_catchpoint breakpoints need to
3597            do anything at this point.  In the future, if some of
3598            the catchpoints need to something, we will need to add
3599            a new method, and call this method from here.  */
3600         continue;
3601       }
3602
3603     /* bp_finish is a special case.  The only way we ought to be able
3604        to see one of these when an exec() has happened, is if the user
3605        caught a vfork, and then said "finish".  Ordinarily a finish just
3606        carries them to the call-site of the current callee, by setting
3607        a temporary bp there and resuming.  But in this case, the finish
3608        will carry them entirely through the vfork & exec.
3609
3610        We don't want to allow a bp_finish to remain inserted now.  But
3611        we can't safely delete it, 'cause finish_command has a handle to
3612        the bp on a bpstat, and will later want to delete it.  There's a
3613        chance (and I've seen it happen) that if we delete the bp_finish
3614        here, that its storage will get reused by the time finish_command
3615        gets 'round to deleting the "use to be a bp_finish" breakpoint.
3616        We really must allow finish_command to delete a bp_finish.
3617
3618        In the absence of a general solution for the "how do we know
3619        it's safe to delete something others may have handles to?"
3620        problem, what we'll do here is just uninsert the bp_finish, and
3621        let finish_command delete it.
3622
3623        (We know the bp_finish is "doomed" in the sense that it's
3624        momentary, and will be deleted as soon as finish_command sees
3625        the inferior stopped.  So it doesn't matter that the bp's
3626        address is probably bogus in the new a.out, unlike e.g., the
3627        solib breakpoints.)  */
3628
3629     if (b->type == bp_finish)
3630       {
3631         continue;
3632       }
3633
3634     /* Without a symbolic address, we have little hope of the
3635        pre-exec() address meaning the same thing in the post-exec()
3636        a.out.  */
3637     if (b->addr_string == NULL)
3638       {
3639         delete_breakpoint (b);
3640         continue;
3641       }
3642   }
3643   /* FIXME what about longjmp breakpoints?  Re-create them here?  */
3644   create_overlay_event_breakpoint ();
3645   create_longjmp_master_breakpoint ();
3646   create_std_terminate_master_breakpoint ();
3647   create_exception_master_breakpoint ();
3648 }
3649
3650 int
3651 detach_breakpoints (ptid_t ptid)
3652 {
3653   struct bp_location *bl, **blp_tmp;
3654   int val = 0;
3655   struct cleanup *old_chain = save_inferior_ptid ();
3656   struct inferior *inf = current_inferior ();
3657
3658   if (ptid_get_pid (ptid) == ptid_get_pid (inferior_ptid))
3659     error (_("Cannot detach breakpoints of inferior_ptid"));
3660
3661   /* Set inferior_ptid; remove_breakpoint_1 uses this global.  */
3662   inferior_ptid = ptid;
3663   ALL_BP_LOCATIONS (bl, blp_tmp)
3664   {
3665     if (bl->pspace != inf->pspace)
3666       continue;
3667
3668     /* This function must physically remove breakpoints locations
3669        from the specified ptid, without modifying the breakpoint
3670        package's state.  Locations of type bp_loc_other are only
3671        maintained at GDB side.  So, there is no need to remove
3672        these bp_loc_other locations.  Moreover, removing these
3673        would modify the breakpoint package's state.  */
3674     if (bl->loc_type == bp_loc_other)
3675       continue;
3676
3677     if (bl->inserted)
3678       val |= remove_breakpoint_1 (bl, mark_inserted);
3679   }
3680
3681   /* Detach single-step breakpoints as well.  */
3682   detach_single_step_breakpoints ();
3683
3684   do_cleanups (old_chain);
3685   return val;
3686 }
3687
3688 /* Remove the breakpoint location BL from the current address space.
3689    Note that this is used to detach breakpoints from a child fork.
3690    When we get here, the child isn't in the inferior list, and neither
3691    do we have objects to represent its address space --- we should
3692    *not* look at bl->pspace->aspace here.  */
3693
3694 static int
3695 remove_breakpoint_1 (struct bp_location *bl, insertion_state_t is)
3696 {
3697   int val;
3698
3699   /* BL is never in moribund_locations by our callers.  */
3700   gdb_assert (bl->owner != NULL);
3701
3702   if (bl->owner->enable_state == bp_permanent)
3703     /* Permanent breakpoints cannot be inserted or removed.  */
3704     return 0;
3705
3706   /* The type of none suggests that owner is actually deleted.
3707      This should not ever happen.  */
3708   gdb_assert (bl->owner->type != bp_none);
3709
3710   if (bl->loc_type == bp_loc_software_breakpoint
3711       || bl->loc_type == bp_loc_hardware_breakpoint)
3712     {
3713       /* "Normal" instruction breakpoint: either the standard
3714          trap-instruction bp (bp_breakpoint), or a
3715          bp_hardware_breakpoint.  */
3716
3717       /* First check to see if we have to handle an overlay.  */
3718       if (overlay_debugging == ovly_off
3719           || bl->section == NULL
3720           || !(section_is_overlay (bl->section)))
3721         {
3722           /* No overlay handling: just remove the breakpoint.  */
3723           val = bl->owner->ops->remove_location (bl);
3724         }
3725       else
3726         {
3727           /* This breakpoint is in an overlay section.
3728              Did we set a breakpoint at the LMA?  */
3729           if (!overlay_events_enabled)
3730               {
3731                 /* Yes -- overlay event support is not active, so we
3732                    should have set a breakpoint at the LMA.  Remove it.  
3733                 */
3734                 /* Ignore any failures: if the LMA is in ROM, we will
3735                    have already warned when we failed to insert it.  */
3736                 if (bl->loc_type == bp_loc_hardware_breakpoint)
3737                   target_remove_hw_breakpoint (bl->gdbarch,
3738                                                &bl->overlay_target_info);
3739                 else
3740                   target_remove_breakpoint (bl->gdbarch,
3741                                             &bl->overlay_target_info);
3742               }
3743           /* Did we set a breakpoint at the VMA? 
3744              If so, we will have marked the breakpoint 'inserted'.  */
3745           if (bl->inserted)
3746             {
3747               /* Yes -- remove it.  Previously we did not bother to
3748                  remove the breakpoint if the section had been
3749                  unmapped, but let's not rely on that being safe.  We
3750                  don't know what the overlay manager might do.  */
3751
3752               /* However, we should remove *software* breakpoints only
3753                  if the section is still mapped, or else we overwrite
3754                  wrong code with the saved shadow contents.  */
3755               if (bl->loc_type == bp_loc_hardware_breakpoint
3756                   || section_is_mapped (bl->section))
3757                 val = bl->owner->ops->remove_location (bl);
3758               else
3759                 val = 0;
3760             }
3761           else
3762             {
3763               /* No -- not inserted, so no need to remove.  No error.  */
3764               val = 0;
3765             }
3766         }
3767
3768       /* In some cases, we might not be able to remove a breakpoint
3769          in a shared library that has already been removed, but we
3770          have not yet processed the shlib unload event.  */
3771       if (val && solib_name_from_address (bl->pspace, bl->address))
3772         val = 0;
3773
3774       if (val)
3775         return val;
3776       bl->inserted = (is == mark_inserted);
3777     }
3778   else if (bl->loc_type == bp_loc_hardware_watchpoint)
3779     {
3780       gdb_assert (bl->owner->ops != NULL
3781                   && bl->owner->ops->remove_location != NULL);
3782
3783       bl->inserted = (is == mark_inserted);
3784       bl->owner->ops->remove_location (bl);
3785
3786       /* Failure to remove any of the hardware watchpoints comes here.  */
3787       if ((is == mark_uninserted) && (bl->inserted))
3788         warning (_("Could not remove hardware watchpoint %d."),
3789                  bl->owner->number);
3790     }
3791   else if (bl->owner->type == bp_catchpoint
3792            && breakpoint_enabled (bl->owner)
3793            && !bl->duplicate)
3794     {
3795       gdb_assert (bl->owner->ops != NULL
3796                   && bl->owner->ops->remove_location != NULL);
3797
3798       val = bl->owner->ops->remove_location (bl);
3799       if (val)
3800         return val;
3801
3802       bl->inserted = (is == mark_inserted);
3803     }
3804
3805   return 0;
3806 }
3807
3808 static int
3809 remove_breakpoint (struct bp_location *bl, insertion_state_t is)
3810 {
3811   int ret;
3812   struct cleanup *old_chain;
3813
3814   /* BL is never in moribund_locations by our callers.  */
3815   gdb_assert (bl->owner != NULL);
3816
3817   if (bl->owner->enable_state == bp_permanent)
3818     /* Permanent breakpoints cannot be inserted or removed.  */
3819     return 0;
3820
3821   /* The type of none suggests that owner is actually deleted.
3822      This should not ever happen.  */
3823   gdb_assert (bl->owner->type != bp_none);
3824
3825   old_chain = save_current_space_and_thread ();
3826
3827   switch_to_program_space_and_thread (bl->pspace);
3828
3829   ret = remove_breakpoint_1 (bl, is);
3830
3831   do_cleanups (old_chain);
3832   return ret;
3833 }
3834
3835 /* Clear the "inserted" flag in all breakpoints.  */
3836
3837 void
3838 mark_breakpoints_out (void)
3839 {
3840   struct bp_location *bl, **blp_tmp;
3841
3842   ALL_BP_LOCATIONS (bl, blp_tmp)
3843     if (bl->pspace == current_program_space)
3844       bl->inserted = 0;
3845 }
3846
3847 /* Clear the "inserted" flag in all breakpoints and delete any
3848    breakpoints which should go away between runs of the program.
3849
3850    Plus other such housekeeping that has to be done for breakpoints
3851    between runs.
3852
3853    Note: this function gets called at the end of a run (by
3854    generic_mourn_inferior) and when a run begins (by
3855    init_wait_for_inferior).  */
3856
3857
3858
3859 void
3860 breakpoint_init_inferior (enum inf_context context)
3861 {
3862   struct breakpoint *b, *b_tmp;
3863   struct bp_location *bl, **blp_tmp;
3864   int ix;
3865   struct program_space *pspace = current_program_space;
3866
3867   /* If breakpoint locations are shared across processes, then there's
3868      nothing to do.  */
3869   if (gdbarch_has_global_breakpoints (target_gdbarch ()))
3870     return;
3871
3872   ALL_BP_LOCATIONS (bl, blp_tmp)
3873   {
3874     /* ALL_BP_LOCATIONS bp_location has BL->OWNER always non-NULL.  */
3875     if (bl->pspace == pspace
3876         && bl->owner->enable_state != bp_permanent)
3877       bl->inserted = 0;
3878   }
3879
3880   ALL_BREAKPOINTS_SAFE (b, b_tmp)
3881   {
3882     if (b->loc && b->loc->pspace != pspace)
3883       continue;
3884
3885     switch (b->type)
3886       {
3887       case bp_call_dummy:
3888       case bp_longjmp_call_dummy:
3889
3890         /* If the call dummy breakpoint is at the entry point it will
3891            cause problems when the inferior is rerun, so we better get
3892            rid of it.  */
3893
3894       case bp_watchpoint_scope:
3895
3896         /* Also get rid of scope breakpoints.  */
3897
3898       case bp_shlib_event:
3899
3900         /* Also remove solib event breakpoints.  Their addresses may
3901            have changed since the last time we ran the program.
3902            Actually we may now be debugging against different target;
3903            and so the solib backend that installed this breakpoint may
3904            not be used in by the target.  E.g.,
3905
3906            (gdb) file prog-linux
3907            (gdb) run               # native linux target
3908            ...
3909            (gdb) kill
3910            (gdb) file prog-win.exe
3911            (gdb) tar rem :9999     # remote Windows gdbserver.
3912         */
3913
3914       case bp_step_resume:
3915
3916         /* Also remove step-resume breakpoints.  */
3917
3918         delete_breakpoint (b);
3919         break;
3920
3921       case bp_watchpoint:
3922       case bp_hardware_watchpoint:
3923       case bp_read_watchpoint:
3924       case bp_access_watchpoint:
3925         {
3926           struct watchpoint *w = (struct watchpoint *) b;
3927
3928           /* Likewise for watchpoints on local expressions.  */
3929           if (w->exp_valid_block != NULL)
3930             delete_breakpoint (b);
3931           else if (context == inf_starting)
3932             {
3933               /* Reset val field to force reread of starting value in
3934                  insert_breakpoints.  */
3935               if (w->val)
3936                 value_free (w->val);
3937               w->val = NULL;
3938               w->val_valid = 0;
3939           }
3940         }
3941         break;
3942       default:
3943         break;
3944       }
3945   }
3946
3947   /* Get rid of the moribund locations.  */
3948   for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, bl); ++ix)
3949     decref_bp_location (&bl);
3950   VEC_free (bp_location_p, moribund_locations);
3951 }
3952
3953 /* These functions concern about actual breakpoints inserted in the
3954    target --- to e.g. check if we need to do decr_pc adjustment or if
3955    we need to hop over the bkpt --- so we check for address space
3956    match, not program space.  */
3957
3958 /* breakpoint_here_p (PC) returns non-zero if an enabled breakpoint
3959    exists at PC.  It returns ordinary_breakpoint_here if it's an
3960    ordinary breakpoint, or permanent_breakpoint_here if it's a
3961    permanent breakpoint.
3962    - When continuing from a location with an ordinary breakpoint, we
3963      actually single step once before calling insert_breakpoints.
3964    - When continuing from a location with a permanent breakpoint, we
3965      need to use the `SKIP_PERMANENT_BREAKPOINT' macro, provided by
3966      the target, to advance the PC past the breakpoint.  */
3967
3968 enum breakpoint_here
3969 breakpoint_here_p (struct address_space *aspace, CORE_ADDR pc)
3970 {
3971   struct bp_location *bl, **blp_tmp;
3972   int any_breakpoint_here = 0;
3973
3974   ALL_BP_LOCATIONS (bl, blp_tmp)
3975     {
3976       if (bl->loc_type != bp_loc_software_breakpoint
3977           && bl->loc_type != bp_loc_hardware_breakpoint)
3978         continue;
3979
3980       /* ALL_BP_LOCATIONS bp_location has BL->OWNER always non-NULL.  */
3981       if ((breakpoint_enabled (bl->owner)
3982            || bl->owner->enable_state == bp_permanent)
3983           && breakpoint_location_address_match (bl, aspace, pc))
3984         {
3985           if (overlay_debugging 
3986               && section_is_overlay (bl->section)
3987               && !section_is_mapped (bl->section))
3988             continue;           /* unmapped overlay -- can't be a match */
3989           else if (bl->owner->enable_state == bp_permanent)
3990             return permanent_breakpoint_here;
3991           else
3992             any_breakpoint_here = 1;
3993         }
3994     }
3995
3996   return any_breakpoint_here ? ordinary_breakpoint_here : 0;
3997 }
3998
3999 /* Return true if there's a moribund breakpoint at PC.  */
4000
4001 int
4002 moribund_breakpoint_here_p (struct address_space *aspace, CORE_ADDR pc)
4003 {
4004   struct bp_location *loc;
4005   int ix;
4006
4007   for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, loc); ++ix)
4008     if (breakpoint_location_address_match (loc, aspace, pc))
4009       return 1;
4010
4011   return 0;
4012 }
4013
4014 /* Returns non-zero if there's a breakpoint inserted at PC, which is
4015    inserted using regular breakpoint_chain / bp_location array
4016    mechanism.  This does not check for single-step breakpoints, which
4017    are inserted and removed using direct target manipulation.  */
4018
4019 int
4020 regular_breakpoint_inserted_here_p (struct address_space *aspace, 
4021                                     CORE_ADDR pc)
4022 {
4023   struct bp_location *bl, **blp_tmp;
4024
4025   ALL_BP_LOCATIONS (bl, blp_tmp)
4026     {
4027       if (bl->loc_type != bp_loc_software_breakpoint
4028           && bl->loc_type != bp_loc_hardware_breakpoint)
4029         continue;
4030
4031       if (bl->inserted
4032           && breakpoint_location_address_match (bl, aspace, pc))
4033         {
4034           if (overlay_debugging 
4035               && section_is_overlay (bl->section)
4036               && !section_is_mapped (bl->section))
4037             continue;           /* unmapped overlay -- can't be a match */
4038           else
4039             return 1;
4040         }
4041     }
4042   return 0;
4043 }
4044
4045 /* Returns non-zero iff there's either regular breakpoint
4046    or a single step breakpoint inserted at PC.  */
4047
4048 int
4049 breakpoint_inserted_here_p (struct address_space *aspace, CORE_ADDR pc)
4050 {
4051   if (regular_breakpoint_inserted_here_p (aspace, pc))
4052     return 1;
4053
4054   if (single_step_breakpoint_inserted_here_p (aspace, pc))
4055     return 1;
4056
4057   return 0;
4058 }
4059
4060 /* This function returns non-zero iff there is a software breakpoint
4061    inserted at PC.  */
4062
4063 int
4064 software_breakpoint_inserted_here_p (struct address_space *aspace,
4065                                      CORE_ADDR pc)
4066 {
4067   struct bp_location *bl, **blp_tmp;
4068
4069   ALL_BP_LOCATIONS (bl, blp_tmp)
4070     {
4071       if (bl->loc_type != bp_loc_software_breakpoint)
4072         continue;
4073
4074       if (bl->inserted
4075           && breakpoint_address_match (bl->pspace->aspace, bl->address,
4076                                        aspace, pc))
4077         {
4078           if (overlay_debugging 
4079               && section_is_overlay (bl->section)
4080               && !section_is_mapped (bl->section))
4081             continue;           /* unmapped overlay -- can't be a match */
4082           else
4083             return 1;
4084         }
4085     }
4086
4087   /* Also check for software single-step breakpoints.  */
4088   if (single_step_breakpoint_inserted_here_p (aspace, pc))
4089     return 1;
4090
4091   return 0;
4092 }
4093
4094 int
4095 hardware_watchpoint_inserted_in_range (struct address_space *aspace,
4096                                        CORE_ADDR addr, ULONGEST len)
4097 {
4098   struct breakpoint *bpt;
4099
4100   ALL_BREAKPOINTS (bpt)
4101     {
4102       struct bp_location *loc;
4103
4104       if (bpt->type != bp_hardware_watchpoint
4105           && bpt->type != bp_access_watchpoint)
4106         continue;
4107
4108       if (!breakpoint_enabled (bpt))
4109         continue;
4110
4111       for (loc = bpt->loc; loc; loc = loc->next)
4112         if (loc->pspace->aspace == aspace && loc->inserted)
4113           {
4114             CORE_ADDR l, h;
4115
4116             /* Check for intersection.  */
4117             l = max (loc->address, addr);
4118             h = min (loc->address + loc->length, addr + len);
4119             if (l < h)
4120               return 1;
4121           }
4122     }
4123   return 0;
4124 }
4125
4126 /* breakpoint_thread_match (PC, PTID) returns true if the breakpoint at
4127    PC is valid for process/thread PTID.  */
4128
4129 int
4130 breakpoint_thread_match (struct address_space *aspace, CORE_ADDR pc,
4131                          ptid_t ptid)
4132 {
4133   struct bp_location *bl, **blp_tmp;
4134   /* The thread and task IDs associated to PTID, computed lazily.  */
4135   int thread = -1;
4136   int task = 0;
4137   
4138   ALL_BP_LOCATIONS (bl, blp_tmp)
4139     {
4140       if (bl->loc_type != bp_loc_software_breakpoint
4141           && bl->loc_type != bp_loc_hardware_breakpoint)
4142         continue;
4143
4144       /* ALL_BP_LOCATIONS bp_location has bl->OWNER always non-NULL.  */
4145       if (!breakpoint_enabled (bl->owner)
4146           && bl->owner->enable_state != bp_permanent)
4147         continue;
4148
4149       if (!breakpoint_location_address_match (bl, aspace, pc))
4150         continue;
4151
4152       if (bl->owner->thread != -1)
4153         {
4154           /* This is a thread-specific breakpoint.  Check that ptid
4155              matches that thread.  If thread hasn't been computed yet,
4156              it is now time to do so.  */
4157           if (thread == -1)
4158             thread = pid_to_thread_id (ptid);
4159           if (bl->owner->thread != thread)
4160             continue;
4161         }
4162
4163       if (bl->owner->task != 0)
4164         {
4165           /* This is a task-specific breakpoint.  Check that ptid
4166              matches that task.  If task hasn't been computed yet,
4167              it is now time to do so.  */
4168           if (task == 0)
4169             task = ada_get_task_number (ptid);
4170           if (bl->owner->task != task)
4171             continue;
4172         }
4173
4174       if (overlay_debugging 
4175           && section_is_overlay (bl->section)
4176           && !section_is_mapped (bl->section))
4177         continue;           /* unmapped overlay -- can't be a match */
4178
4179       return 1;
4180     }
4181
4182   return 0;
4183 }
4184 \f
4185
4186 /* bpstat stuff.  External routines' interfaces are documented
4187    in breakpoint.h.  */
4188
4189 int
4190 is_catchpoint (struct breakpoint *ep)
4191 {
4192   return (ep->type == bp_catchpoint);
4193 }
4194
4195 /* Frees any storage that is part of a bpstat.  Does not walk the
4196    'next' chain.  */
4197
4198 static void
4199 bpstat_free (bpstat bs)
4200 {
4201   if (bs->old_val != NULL)
4202     value_free (bs->old_val);
4203   decref_counted_command_line (&bs->commands);
4204   decref_bp_location (&bs->bp_location_at);
4205   xfree (bs);
4206 }
4207
4208 /* Clear a bpstat so that it says we are not at any breakpoint.
4209    Also free any storage that is part of a bpstat.  */
4210
4211 void
4212 bpstat_clear (bpstat *bsp)
4213 {
4214   bpstat p;
4215   bpstat q;
4216
4217   if (bsp == 0)
4218     return;
4219   p = *bsp;
4220   while (p != NULL)
4221     {
4222       q = p->next;
4223       bpstat_free (p);
4224       p = q;
4225     }
4226   *bsp = NULL;
4227 }
4228
4229 /* Return a copy of a bpstat.  Like "bs1 = bs2" but all storage that
4230    is part of the bpstat is copied as well.  */
4231
4232 bpstat
4233 bpstat_copy (bpstat bs)
4234 {
4235   bpstat p = NULL;
4236   bpstat tmp;
4237   bpstat retval = NULL;
4238
4239   if (bs == NULL)
4240     return bs;
4241
4242   for (; bs != NULL; bs = bs->next)
4243     {
4244       tmp = (bpstat) xmalloc (sizeof (*tmp));
4245       memcpy (tmp, bs, sizeof (*tmp));
4246       incref_counted_command_line (tmp->commands);
4247       incref_bp_location (tmp->bp_location_at);
4248       if (bs->old_val != NULL)
4249         {
4250           tmp->old_val = value_copy (bs->old_val);
4251           release_value (tmp->old_val);
4252         }
4253
4254       if (p == NULL)
4255         /* This is the first thing in the chain.  */
4256         retval = tmp;
4257       else
4258         p->next = tmp;
4259       p = tmp;
4260     }
4261   p->next = NULL;
4262   return retval;
4263 }
4264
4265 /* Find the bpstat associated with this breakpoint.  */
4266
4267 bpstat
4268 bpstat_find_breakpoint (bpstat bsp, struct breakpoint *breakpoint)
4269 {
4270   if (bsp == NULL)
4271     return NULL;
4272
4273   for (; bsp != NULL; bsp = bsp->next)
4274     {
4275       if (bsp->breakpoint_at == breakpoint)
4276         return bsp;
4277     }
4278   return NULL;
4279 }
4280
4281 /* See breakpoint.h.  */
4282
4283 int
4284 bpstat_explains_signal (bpstat bsp, enum gdb_signal sig)
4285 {
4286   for (; bsp != NULL; bsp = bsp->next)
4287     {
4288       if (bsp->breakpoint_at == NULL)
4289         {
4290           /* A moribund location can never explain a signal other than
4291              GDB_SIGNAL_TRAP.  */
4292           if (sig == GDB_SIGNAL_TRAP)
4293             return 1;
4294         }
4295       else
4296         {
4297           if (bsp->breakpoint_at->ops->explains_signal (bsp->breakpoint_at,
4298                                                         sig))
4299             return 1;
4300         }
4301     }
4302
4303   return 0;
4304 }
4305
4306 /* Put in *NUM the breakpoint number of the first breakpoint we are
4307    stopped at.  *BSP upon return is a bpstat which points to the
4308    remaining breakpoints stopped at (but which is not guaranteed to be
4309    good for anything but further calls to bpstat_num).
4310
4311    Return 0 if passed a bpstat which does not indicate any breakpoints.
4312    Return -1 if stopped at a breakpoint that has been deleted since
4313    we set it.
4314    Return 1 otherwise.  */
4315
4316 int
4317 bpstat_num (bpstat *bsp, int *num)
4318 {
4319   struct breakpoint *b;
4320
4321   if ((*bsp) == NULL)
4322     return 0;                   /* No more breakpoint values */
4323
4324   /* We assume we'll never have several bpstats that correspond to a
4325      single breakpoint -- otherwise, this function might return the
4326      same number more than once and this will look ugly.  */
4327   b = (*bsp)->breakpoint_at;
4328   *bsp = (*bsp)->next;
4329   if (b == NULL)
4330     return -1;                  /* breakpoint that's been deleted since */
4331
4332   *num = b->number;             /* We have its number */
4333   return 1;
4334 }
4335
4336 /* See breakpoint.h.  */
4337
4338 void
4339 bpstat_clear_actions (void)
4340 {
4341   struct thread_info *tp;
4342   bpstat bs;
4343
4344   if (ptid_equal (inferior_ptid, null_ptid))
4345     return;
4346
4347   tp = find_thread_ptid (inferior_ptid);
4348   if (tp == NULL)
4349     return;
4350
4351   for (bs = tp->control.stop_bpstat; bs != NULL; bs = bs->next)
4352     {
4353       decref_counted_command_line (&bs->commands);
4354
4355       if (bs->old_val != NULL)
4356         {
4357           value_free (bs->old_val);
4358           bs->old_val = NULL;
4359         }
4360     }
4361 }
4362
4363 /* Called when a command is about to proceed the inferior.  */
4364
4365 static void
4366 breakpoint_about_to_proceed (void)
4367 {
4368   if (!ptid_equal (inferior_ptid, null_ptid))
4369     {
4370       struct thread_info *tp = inferior_thread ();
4371
4372       /* Allow inferior function calls in breakpoint commands to not
4373          interrupt the command list.  When the call finishes
4374          successfully, the inferior will be standing at the same
4375          breakpoint as if nothing happened.  */
4376       if (tp->control.in_infcall)
4377         return;
4378     }
4379
4380   breakpoint_proceeded = 1;
4381 }
4382
4383 /* Stub for cleaning up our state if we error-out of a breakpoint
4384    command.  */
4385 static void
4386 cleanup_executing_breakpoints (void *ignore)
4387 {
4388   executing_breakpoint_commands = 0;
4389 }
4390
4391 /* Return non-zero iff CMD as the first line of a command sequence is `silent'
4392    or its equivalent.  */
4393
4394 static int
4395 command_line_is_silent (struct command_line *cmd)
4396 {
4397   return cmd && (strcmp ("silent", cmd->line) == 0
4398                  || (xdb_commands && strcmp ("Q", cmd->line) == 0));
4399 }
4400
4401 /* Execute all the commands associated with all the breakpoints at
4402    this location.  Any of these commands could cause the process to
4403    proceed beyond this point, etc.  We look out for such changes by
4404    checking the global "breakpoint_proceeded" after each command.
4405
4406    Returns true if a breakpoint command resumed the inferior.  In that
4407    case, it is the caller's responsibility to recall it again with the
4408    bpstat of the current thread.  */
4409
4410 static int
4411 bpstat_do_actions_1 (bpstat *bsp)
4412 {
4413   bpstat bs;
4414   struct cleanup *old_chain;
4415   int again = 0;
4416
4417   /* Avoid endless recursion if a `source' command is contained
4418      in bs->commands.  */
4419   if (executing_breakpoint_commands)
4420     return 0;
4421
4422   executing_breakpoint_commands = 1;
4423   old_chain = make_cleanup (cleanup_executing_breakpoints, 0);
4424
4425   prevent_dont_repeat ();
4426
4427   /* This pointer will iterate over the list of bpstat's.  */
4428   bs = *bsp;
4429
4430   breakpoint_proceeded = 0;
4431   for (; bs != NULL; bs = bs->next)
4432     {
4433       struct counted_command_line *ccmd;
4434       struct command_line *cmd;
4435       struct cleanup *this_cmd_tree_chain;
4436
4437       /* Take ownership of the BSP's command tree, if it has one.
4438
4439          The command tree could legitimately contain commands like
4440          'step' and 'next', which call clear_proceed_status, which
4441          frees stop_bpstat's command tree.  To make sure this doesn't
4442          free the tree we're executing out from under us, we need to
4443          take ownership of the tree ourselves.  Since a given bpstat's
4444          commands are only executed once, we don't need to copy it; we
4445          can clear the pointer in the bpstat, and make sure we free
4446          the tree when we're done.  */
4447       ccmd = bs->commands;
4448       bs->commands = NULL;
4449       this_cmd_tree_chain = make_cleanup_decref_counted_command_line (&ccmd);
4450       cmd = ccmd ? ccmd->commands : NULL;
4451       if (command_line_is_silent (cmd))
4452         {
4453           /* The action has been already done by bpstat_stop_status.  */
4454           cmd = cmd->next;
4455         }
4456
4457       while (cmd != NULL)
4458         {
4459           execute_control_command (cmd);
4460
4461           if (breakpoint_proceeded)
4462             break;
4463           else
4464             cmd = cmd->next;
4465         }
4466
4467       /* We can free this command tree now.  */
4468       do_cleanups (this_cmd_tree_chain);
4469
4470       if (breakpoint_proceeded)
4471         {
4472           if (target_can_async_p ())
4473             /* If we are in async mode, then the target might be still
4474                running, not stopped at any breakpoint, so nothing for
4475                us to do here -- just return to the event loop.  */
4476             ;
4477           else
4478             /* In sync mode, when execute_control_command returns
4479                we're already standing on the next breakpoint.
4480                Breakpoint commands for that stop were not run, since
4481                execute_command does not run breakpoint commands --
4482                only command_line_handler does, but that one is not
4483                involved in execution of breakpoint commands.  So, we
4484                can now execute breakpoint commands.  It should be
4485                noted that making execute_command do bpstat actions is
4486                not an option -- in this case we'll have recursive
4487                invocation of bpstat for each breakpoint with a
4488                command, and can easily blow up GDB stack.  Instead, we
4489                return true, which will trigger the caller to recall us
4490                with the new stop_bpstat.  */
4491             again = 1;
4492           break;
4493         }
4494     }
4495   do_cleanups (old_chain);
4496   return again;
4497 }
4498
4499 void
4500 bpstat_do_actions (void)
4501 {
4502   struct cleanup *cleanup_if_error = make_bpstat_clear_actions_cleanup ();
4503
4504   /* Do any commands attached to breakpoint we are stopped at.  */
4505   while (!ptid_equal (inferior_ptid, null_ptid)
4506          && target_has_execution
4507          && !is_exited (inferior_ptid)
4508          && !is_executing (inferior_ptid))
4509     /* Since in sync mode, bpstat_do_actions may resume the inferior,
4510        and only return when it is stopped at the next breakpoint, we
4511        keep doing breakpoint actions until it returns false to
4512        indicate the inferior was not resumed.  */
4513     if (!bpstat_do_actions_1 (&inferior_thread ()->control.stop_bpstat))
4514       break;
4515
4516   discard_cleanups (cleanup_if_error);
4517 }
4518
4519 /* Print out the (old or new) value associated with a watchpoint.  */
4520
4521 static void
4522 watchpoint_value_print (struct value *val, struct ui_file *stream)
4523 {
4524   if (val == NULL)
4525     fprintf_unfiltered (stream, _("<unreadable>"));
4526   else
4527     {
4528       struct value_print_options opts;
4529       get_user_print_options (&opts);
4530       value_print (val, stream, &opts);
4531     }
4532 }
4533
4534 /* Generic routine for printing messages indicating why we
4535    stopped.  The behavior of this function depends on the value
4536    'print_it' in the bpstat structure.  Under some circumstances we
4537    may decide not to print anything here and delegate the task to
4538    normal_stop().  */
4539
4540 static enum print_stop_action
4541 print_bp_stop_message (bpstat bs)
4542 {
4543   switch (bs->print_it)
4544     {
4545     case print_it_noop:
4546       /* Nothing should be printed for this bpstat entry.  */
4547       return PRINT_UNKNOWN;
4548       break;
4549
4550     case print_it_done:
4551       /* We still want to print the frame, but we already printed the
4552          relevant messages.  */
4553       return PRINT_SRC_AND_LOC;
4554       break;
4555
4556     case print_it_normal:
4557       {
4558         struct breakpoint *b = bs->breakpoint_at;
4559
4560         /* bs->breakpoint_at can be NULL if it was a momentary breakpoint
4561            which has since been deleted.  */
4562         if (b == NULL)
4563           return PRINT_UNKNOWN;
4564
4565         /* Normal case.  Call the breakpoint's print_it method.  */
4566         return b->ops->print_it (bs);
4567       }
4568       break;
4569
4570     default:
4571       internal_error (__FILE__, __LINE__,
4572                       _("print_bp_stop_message: unrecognized enum value"));
4573       break;
4574     }
4575 }
4576
4577 /* A helper function that prints a shared library stopped event.  */
4578
4579 static void
4580 print_solib_event (int is_catchpoint)
4581 {
4582   int any_deleted
4583     = !VEC_empty (char_ptr, current_program_space->deleted_solibs);
4584   int any_added
4585     = !VEC_empty (so_list_ptr, current_program_space->added_solibs);
4586
4587   if (!is_catchpoint)
4588     {
4589       if (any_added || any_deleted)
4590         ui_out_text (current_uiout,
4591                      _("Stopped due to shared library event:\n"));
4592       else
4593         ui_out_text (current_uiout,
4594                      _("Stopped due to shared library event (no "
4595                        "libraries added or removed)\n"));
4596     }
4597
4598   if (ui_out_is_mi_like_p (current_uiout))
4599     ui_out_field_string (current_uiout, "reason",
4600                          async_reason_lookup (EXEC_ASYNC_SOLIB_EVENT));
4601
4602   if (any_deleted)
4603     {
4604       struct cleanup *cleanup;
4605       char *name;
4606       int ix;
4607
4608       ui_out_text (current_uiout, _("  Inferior unloaded "));
4609       cleanup = make_cleanup_ui_out_list_begin_end (current_uiout,
4610                                                     "removed");
4611       for (ix = 0;
4612            VEC_iterate (char_ptr, current_program_space->deleted_solibs,
4613                         ix, name);
4614            ++ix)
4615         {
4616           if (ix > 0)
4617             ui_out_text (current_uiout, "    ");
4618           ui_out_field_string (current_uiout, "library", name);
4619           ui_out_text (current_uiout, "\n");
4620         }
4621
4622       do_cleanups (cleanup);
4623     }
4624
4625   if (any_added)
4626     {
4627       struct so_list *iter;
4628       int ix;
4629       struct cleanup *cleanup;
4630
4631       ui_out_text (current_uiout, _("  Inferior loaded "));
4632       cleanup = make_cleanup_ui_out_list_begin_end (current_uiout,
4633                                                     "added");
4634       for (ix = 0;
4635            VEC_iterate (so_list_ptr, current_program_space->added_solibs,
4636                         ix, iter);
4637            ++ix)
4638         {
4639           if (ix > 0)
4640             ui_out_text (current_uiout, "    ");
4641           ui_out_field_string (current_uiout, "library", iter->so_name);
4642           ui_out_text (current_uiout, "\n");
4643         }
4644
4645       do_cleanups (cleanup);
4646     }
4647 }
4648
4649 /* Print a message indicating what happened.  This is called from
4650    normal_stop().  The input to this routine is the head of the bpstat
4651    list - a list of the eventpoints that caused this stop.  KIND is
4652    the target_waitkind for the stopping event.  This
4653    routine calls the generic print routine for printing a message
4654    about reasons for stopping.  This will print (for example) the
4655    "Breakpoint n," part of the output.  The return value of this
4656    routine is one of:
4657
4658    PRINT_UNKNOWN: Means we printed nothing.
4659    PRINT_SRC_AND_LOC: Means we printed something, and expect subsequent
4660    code to print the location.  An example is 
4661    "Breakpoint 1, " which should be followed by
4662    the location.
4663    PRINT_SRC_ONLY: Means we printed something, but there is no need
4664    to also print the location part of the message.
4665    An example is the catch/throw messages, which
4666    don't require a location appended to the end.
4667    PRINT_NOTHING: We have done some printing and we don't need any 
4668    further info to be printed.  */
4669
4670 enum print_stop_action
4671 bpstat_print (bpstat bs, int kind)
4672 {
4673   int val;
4674
4675   /* Maybe another breakpoint in the chain caused us to stop.
4676      (Currently all watchpoints go on the bpstat whether hit or not.
4677      That probably could (should) be changed, provided care is taken
4678      with respect to bpstat_explains_signal).  */
4679   for (; bs; bs = bs->next)
4680     {
4681       val = print_bp_stop_message (bs);
4682       if (val == PRINT_SRC_ONLY 
4683           || val == PRINT_SRC_AND_LOC 
4684           || val == PRINT_NOTHING)
4685         return val;
4686     }
4687
4688   /* If we had hit a shared library event breakpoint,
4689      print_bp_stop_message would print out this message.  If we hit an
4690      OS-level shared library event, do the same thing.  */
4691   if (kind == TARGET_WAITKIND_LOADED)
4692     {
4693       print_solib_event (0);
4694       return PRINT_NOTHING;
4695     }
4696
4697   /* We reached the end of the chain, or we got a null BS to start
4698      with and nothing was printed.  */
4699   return PRINT_UNKNOWN;
4700 }
4701
4702 /* Evaluate the expression EXP and return 1 if value is zero.
4703    This returns the inverse of the condition because it is called
4704    from catch_errors which returns 0 if an exception happened, and if an
4705    exception happens we want execution to stop.
4706    The argument is a "struct expression *" that has been cast to a
4707    "void *" to make it pass through catch_errors.  */
4708
4709 static int
4710 breakpoint_cond_eval (void *exp)
4711 {
4712   struct value *mark = value_mark ();
4713   int i = !value_true (evaluate_expression ((struct expression *) exp));
4714
4715   value_free_to_mark (mark);
4716   return i;
4717 }
4718
4719 /* Allocate a new bpstat.  Link it to the FIFO list by BS_LINK_POINTER.  */
4720
4721 static bpstat
4722 bpstat_alloc (struct bp_location *bl, bpstat **bs_link_pointer)
4723 {
4724   bpstat bs;
4725
4726   bs = (bpstat) xmalloc (sizeof (*bs));
4727   bs->next = NULL;
4728   **bs_link_pointer = bs;
4729   *bs_link_pointer = &bs->next;
4730   bs->breakpoint_at = bl->owner;
4731   bs->bp_location_at = bl;
4732   incref_bp_location (bl);
4733   /* If the condition is false, etc., don't do the commands.  */
4734   bs->commands = NULL;
4735   bs->old_val = NULL;
4736   bs->print_it = print_it_normal;
4737   return bs;
4738 }
4739 \f
4740 /* The target has stopped with waitstatus WS.  Check if any hardware
4741    watchpoints have triggered, according to the target.  */
4742
4743 int
4744 watchpoints_triggered (struct target_waitstatus *ws)
4745 {
4746   int stopped_by_watchpoint = target_stopped_by_watchpoint ();
4747   CORE_ADDR addr;
4748   struct breakpoint *b;
4749
4750   if (!stopped_by_watchpoint)
4751     {
4752       /* We were not stopped by a watchpoint.  Mark all watchpoints
4753          as not triggered.  */
4754       ALL_BREAKPOINTS (b)
4755         if (is_hardware_watchpoint (b))
4756           {
4757             struct watchpoint *w = (struct watchpoint *) b;
4758
4759             w->watchpoint_triggered = watch_triggered_no;
4760           }
4761
4762       return 0;
4763     }
4764
4765   if (!target_stopped_data_address (&current_target, &addr))
4766     {
4767       /* We were stopped by a watchpoint, but we don't know where.
4768          Mark all watchpoints as unknown.  */
4769       ALL_BREAKPOINTS (b)
4770         if (is_hardware_watchpoint (b))
4771           {
4772             struct watchpoint *w = (struct watchpoint *) b;
4773
4774             w->watchpoint_triggered = watch_triggered_unknown;
4775           }
4776
4777       return 1;
4778     }
4779
4780   /* The target could report the data address.  Mark watchpoints
4781      affected by this data address as triggered, and all others as not
4782      triggered.  */
4783
4784   ALL_BREAKPOINTS (b)
4785     if (is_hardware_watchpoint (b))
4786       {
4787         struct watchpoint *w = (struct watchpoint *) b;
4788         struct bp_location *loc;
4789
4790         w->watchpoint_triggered = watch_triggered_no;
4791         for (loc = b->loc; loc; loc = loc->next)
4792           {
4793             if (is_masked_watchpoint (b))
4794               {
4795                 CORE_ADDR newaddr = addr & w->hw_wp_mask;
4796                 CORE_ADDR start = loc->address & w->hw_wp_mask;
4797
4798                 if (newaddr == start)
4799                   {
4800                     w->watchpoint_triggered = watch_triggered_yes;
4801                     break;
4802                   }
4803               }
4804             /* Exact match not required.  Within range is sufficient.  */
4805             else if (target_watchpoint_addr_within_range (&current_target,
4806                                                          addr, loc->address,
4807                                                          loc->length))
4808               {
4809                 w->watchpoint_triggered = watch_triggered_yes;
4810                 break;
4811               }
4812           }
4813       }
4814
4815   return 1;
4816 }
4817
4818 /* Possible return values for watchpoint_check (this can't be an enum
4819    because of check_errors).  */
4820 /* The watchpoint has been deleted.  */
4821 #define WP_DELETED 1
4822 /* The value has changed.  */
4823 #define WP_VALUE_CHANGED 2
4824 /* The value has not changed.  */
4825 #define WP_VALUE_NOT_CHANGED 3
4826 /* Ignore this watchpoint, no matter if the value changed or not.  */
4827 #define WP_IGNORE 4
4828
4829 #define BP_TEMPFLAG 1
4830 #define BP_HARDWAREFLAG 2
4831
4832 /* Evaluate watchpoint condition expression and check if its value
4833    changed.
4834
4835    P should be a pointer to struct bpstat, but is defined as a void *
4836    in order for this function to be usable with catch_errors.  */
4837
4838 static int
4839 watchpoint_check (void *p)
4840 {
4841   bpstat bs = (bpstat) p;
4842   struct watchpoint *b;
4843   struct frame_info *fr;
4844   int within_current_scope;
4845
4846   /* BS is built from an existing struct breakpoint.  */
4847   gdb_assert (bs->breakpoint_at != NULL);
4848   b = (struct watchpoint *) bs->breakpoint_at;
4849
4850   /* If this is a local watchpoint, we only want to check if the
4851      watchpoint frame is in scope if the current thread is the thread
4852      that was used to create the watchpoint.  */
4853   if (!watchpoint_in_thread_scope (b))
4854     return WP_IGNORE;
4855
4856   if (b->exp_valid_block == NULL)
4857     within_current_scope = 1;
4858   else
4859     {
4860       struct frame_info *frame = get_current_frame ();
4861       struct gdbarch *frame_arch = get_frame_arch (frame);
4862       CORE_ADDR frame_pc = get_frame_pc (frame);
4863
4864       /* in_function_epilogue_p() returns a non-zero value if we're
4865          still in the function but the stack frame has already been
4866          invalidated.  Since we can't rely on the values of local
4867          variables after the stack has been destroyed, we are treating
4868          the watchpoint in that state as `not changed' without further
4869          checking.  Don't mark watchpoints as changed if the current
4870          frame is in an epilogue - even if they are in some other
4871          frame, our view of the stack is likely to be wrong and
4872          frame_find_by_id could error out.  */
4873       if (gdbarch_in_function_epilogue_p (frame_arch, frame_pc))
4874         return WP_IGNORE;
4875
4876       fr = frame_find_by_id (b->watchpoint_frame);
4877       within_current_scope = (fr != NULL);
4878
4879       /* If we've gotten confused in the unwinder, we might have
4880          returned a frame that can't describe this variable.  */
4881       if (within_current_scope)
4882         {
4883           struct symbol *function;
4884
4885           function = get_frame_function (fr);
4886           if (function == NULL
4887               || !contained_in (b->exp_valid_block,
4888                                 SYMBOL_BLOCK_VALUE (function)))
4889             within_current_scope = 0;
4890         }
4891
4892       if (within_current_scope)
4893         /* If we end up stopping, the current frame will get selected
4894            in normal_stop.  So this call to select_frame won't affect
4895            the user.  */
4896         select_frame (fr);
4897     }
4898
4899   if (within_current_scope)
4900     {
4901       /* We use value_{,free_to_}mark because it could be a *long*
4902          time before we return to the command level and call
4903          free_all_values.  We can't call free_all_values because we
4904          might be in the middle of evaluating a function call.  */
4905
4906       int pc = 0;
4907       struct value *mark;
4908       struct value *new_val;
4909
4910       if (is_masked_watchpoint (&b->base))
4911         /* Since we don't know the exact trigger address (from
4912            stopped_data_address), just tell the user we've triggered
4913            a mask watchpoint.  */
4914         return WP_VALUE_CHANGED;
4915
4916       mark = value_mark ();
4917       fetch_subexp_value (b->exp, &pc, &new_val, NULL, NULL, 0);
4918
4919       /* We use value_equal_contents instead of value_equal because
4920          the latter coerces an array to a pointer, thus comparing just
4921          the address of the array instead of its contents.  This is
4922          not what we want.  */
4923       if ((b->val != NULL) != (new_val != NULL)
4924           || (b->val != NULL && !value_equal_contents (b->val, new_val)))
4925         {
4926           if (new_val != NULL)
4927             {
4928               release_value (new_val);
4929               value_free_to_mark (mark);
4930             }
4931           bs->old_val = b->val;
4932           b->val = new_val;
4933           b->val_valid = 1;
4934           return WP_VALUE_CHANGED;
4935         }
4936       else
4937         {
4938           /* Nothing changed.  */
4939           value_free_to_mark (mark);
4940           return WP_VALUE_NOT_CHANGED;
4941         }
4942     }
4943   else
4944     {
4945       struct ui_out *uiout = current_uiout;
4946
4947       /* This seems like the only logical thing to do because
4948          if we temporarily ignored the watchpoint, then when
4949          we reenter the block in which it is valid it contains
4950          garbage (in the case of a function, it may have two
4951          garbage values, one before and one after the prologue).
4952          So we can't even detect the first assignment to it and
4953          watch after that (since the garbage may or may not equal
4954          the first value assigned).  */
4955       /* We print all the stop information in
4956          breakpoint_ops->print_it, but in this case, by the time we
4957          call breakpoint_ops->print_it this bp will be deleted
4958          already.  So we have no choice but print the information
4959          here.  */
4960       if (ui_out_is_mi_like_p (uiout))
4961         ui_out_field_string
4962           (uiout, "reason", async_reason_lookup (EXEC_ASYNC_WATCHPOINT_SCOPE));
4963       ui_out_text (uiout, "\nWatchpoint ");
4964       ui_out_field_int (uiout, "wpnum", b->base.number);
4965       ui_out_text (uiout,
4966                    " deleted because the program has left the block in\n\
4967 which its expression is valid.\n");     
4968
4969       /* Make sure the watchpoint's commands aren't executed.  */
4970       decref_counted_command_line (&b->base.commands);
4971       watchpoint_del_at_next_stop (b);
4972
4973       return WP_DELETED;
4974     }
4975 }
4976
4977 /* Return true if it looks like target has stopped due to hitting
4978    breakpoint location BL.  This function does not check if we should
4979    stop, only if BL explains the stop.  */
4980
4981 static int
4982 bpstat_check_location (const struct bp_location *bl,
4983                        struct address_space *aspace, CORE_ADDR bp_addr,
4984                        const struct target_waitstatus *ws)
4985 {
4986   struct breakpoint *b = bl->owner;
4987
4988   /* BL is from an existing breakpoint.  */
4989   gdb_assert (b != NULL);
4990
4991   return b->ops->breakpoint_hit (bl, aspace, bp_addr, ws);
4992 }
4993
4994 /* Determine if the watched values have actually changed, and we
4995    should stop.  If not, set BS->stop to 0.  */
4996
4997 static void
4998 bpstat_check_watchpoint (bpstat bs)
4999 {
5000   const struct bp_location *bl;
5001   struct watchpoint *b;
5002
5003   /* BS is built for existing struct breakpoint.  */
5004   bl = bs->bp_location_at;
5005   gdb_assert (bl != NULL);
5006   b = (struct watchpoint *) bs->breakpoint_at;
5007   gdb_assert (b != NULL);
5008
5009     {
5010       int must_check_value = 0;
5011       
5012       if (b->base.type == bp_watchpoint)
5013         /* For a software watchpoint, we must always check the
5014            watched value.  */
5015         must_check_value = 1;
5016       else if (b->watchpoint_triggered == watch_triggered_yes)
5017         /* We have a hardware watchpoint (read, write, or access)
5018            and the target earlier reported an address watched by
5019            this watchpoint.  */
5020         must_check_value = 1;
5021       else if (b->watchpoint_triggered == watch_triggered_unknown
5022                && b->base.type == bp_hardware_watchpoint)
5023         /* We were stopped by a hardware watchpoint, but the target could
5024            not report the data address.  We must check the watchpoint's
5025            value.  Access and read watchpoints are out of luck; without
5026            a data address, we can't figure it out.  */
5027         must_check_value = 1;
5028
5029       if (must_check_value)
5030         {
5031           char *message
5032             = xstrprintf ("Error evaluating expression for watchpoint %d\n",
5033                           b->base.number);
5034           struct cleanup *cleanups = make_cleanup (xfree, message);
5035           int e = catch_errors (watchpoint_check, bs, message,
5036                                 RETURN_MASK_ALL);
5037           do_cleanups (cleanups);
5038           switch (e)
5039             {
5040             case WP_DELETED:
5041               /* We've already printed what needs to be printed.  */
5042               bs->print_it = print_it_done;
5043               /* Stop.  */
5044               break;
5045             case WP_IGNORE:
5046               bs->print_it = print_it_noop;
5047               bs->stop = 0;
5048               break;
5049             case WP_VALUE_CHANGED:
5050               if (b->base.type == bp_read_watchpoint)
5051                 {
5052                   /* There are two cases to consider here:
5053
5054                      1. We're watching the triggered memory for reads.
5055                      In that case, trust the target, and always report
5056                      the watchpoint hit to the user.  Even though
5057                      reads don't cause value changes, the value may
5058                      have changed since the last time it was read, and
5059                      since we're not trapping writes, we will not see
5060                      those, and as such we should ignore our notion of
5061                      old value.
5062
5063                      2. We're watching the triggered memory for both
5064                      reads and writes.  There are two ways this may
5065                      happen:
5066
5067                      2.1. This is a target that can't break on data
5068                      reads only, but can break on accesses (reads or
5069                      writes), such as e.g., x86.  We detect this case
5070                      at the time we try to insert read watchpoints.
5071
5072                      2.2. Otherwise, the target supports read
5073                      watchpoints, but, the user set an access or write
5074                      watchpoint watching the same memory as this read
5075                      watchpoint.
5076
5077                      If we're watching memory writes as well as reads,
5078                      ignore watchpoint hits when we find that the
5079                      value hasn't changed, as reads don't cause
5080                      changes.  This still gives false positives when
5081                      the program writes the same value to memory as
5082                      what there was already in memory (we will confuse
5083                      it for a read), but it's much better than
5084                      nothing.  */
5085
5086                   int other_write_watchpoint = 0;
5087
5088                   if (bl->watchpoint_type == hw_read)
5089                     {
5090                       struct breakpoint *other_b;
5091
5092                       ALL_BREAKPOINTS (other_b)
5093                         if (other_b->type == bp_hardware_watchpoint
5094                             || other_b->type == bp_access_watchpoint)
5095                           {
5096                             struct watchpoint *other_w =
5097                               (struct watchpoint *) other_b;
5098
5099                             if (other_w->watchpoint_triggered
5100                                 == watch_triggered_yes)
5101                               {
5102                                 other_write_watchpoint = 1;
5103                                 break;
5104                               }
5105                           }
5106                     }
5107
5108                   if (other_write_watchpoint
5109                       || bl->watchpoint_type == hw_access)
5110                     {
5111                       /* We're watching the same memory for writes,
5112                          and the value changed since the last time we
5113                          updated it, so this trap must be for a write.
5114                          Ignore it.  */
5115                       bs->print_it = print_it_noop;
5116                       bs->stop = 0;
5117                     }
5118                 }
5119               break;
5120             case WP_VALUE_NOT_CHANGED:
5121               if (b->base.type == bp_hardware_watchpoint
5122                   || b->base.type == bp_watchpoint)
5123                 {
5124                   /* Don't stop: write watchpoints shouldn't fire if
5125                      the value hasn't changed.  */
5126                   bs->print_it = print_it_noop;
5127                   bs->stop = 0;
5128                 }
5129               /* Stop.  */
5130               break;
5131             default:
5132               /* Can't happen.  */
5133             case 0:
5134               /* Error from catch_errors.  */
5135               printf_filtered (_("Watchpoint %d deleted.\n"), b->base.number);
5136               watchpoint_del_at_next_stop (b);
5137               /* We've already printed what needs to be printed.  */
5138               bs->print_it = print_it_done;
5139               break;
5140             }
5141         }
5142       else      /* must_check_value == 0 */
5143         {
5144           /* This is a case where some watchpoint(s) triggered, but
5145              not at the address of this watchpoint, or else no
5146              watchpoint triggered after all.  So don't print
5147              anything for this watchpoint.  */
5148           bs->print_it = print_it_noop;
5149           bs->stop = 0;
5150         }
5151     }
5152 }
5153
5154 /* For breakpoints that are currently marked as telling gdb to stop,
5155    check conditions (condition proper, frame, thread and ignore count)
5156    of breakpoint referred to by BS.  If we should not stop for this
5157    breakpoint, set BS->stop to 0.  */
5158
5159 static void
5160 bpstat_check_breakpoint_conditions (bpstat bs, ptid_t ptid)
5161 {
5162   const struct bp_location *bl;
5163   struct breakpoint *b;
5164   int value_is_zero = 0;
5165   struct expression *cond;
5166
5167   gdb_assert (bs->stop);
5168
5169   /* BS is built for existing struct breakpoint.  */
5170   bl = bs->bp_location_at;
5171   gdb_assert (bl != NULL);
5172   b = bs->breakpoint_at;
5173   gdb_assert (b != NULL);
5174
5175   /* Even if the target evaluated the condition on its end and notified GDB, we
5176      need to do so again since GDB does not know if we stopped due to a
5177      breakpoint or a single step breakpoint.  */
5178
5179   if (frame_id_p (b->frame_id)
5180       && !frame_id_eq (b->frame_id, get_stack_frame_id (get_current_frame ())))
5181     {
5182       bs->stop = 0;
5183       return;
5184     }
5185
5186   /* If this is a thread/task-specific breakpoint, don't waste cpu
5187      evaluating the condition if this isn't the specified
5188      thread/task.  */
5189   if ((b->thread != -1 && b->thread != pid_to_thread_id (ptid))
5190       || (b->task != 0 && b->task != ada_get_task_number (ptid)))
5191
5192     {
5193       bs->stop = 0;
5194       return;
5195     }
5196
5197   /* Evaluate extension language breakpoints that have a "stop" method
5198      implemented.  */
5199   bs->stop = breakpoint_ext_lang_cond_says_stop (b);
5200
5201   if (is_watchpoint (b))
5202     {
5203       struct watchpoint *w = (struct watchpoint *) b;
5204
5205       cond = w->cond_exp;
5206     }
5207   else
5208     cond = bl->cond;
5209
5210   if (cond && b->disposition != disp_del_at_next_stop)
5211     {
5212       int within_current_scope = 1;
5213       struct watchpoint * w;
5214
5215       /* We use value_mark and value_free_to_mark because it could
5216          be a long time before we return to the command level and
5217          call free_all_values.  We can't call free_all_values
5218          because we might be in the middle of evaluating a
5219          function call.  */
5220       struct value *mark = value_mark ();
5221
5222       if (is_watchpoint (b))
5223         w = (struct watchpoint *) b;
5224       else
5225         w = NULL;
5226
5227       /* Need to select the frame, with all that implies so that
5228          the conditions will have the right context.  Because we
5229          use the frame, we will not see an inlined function's
5230          variables when we arrive at a breakpoint at the start
5231          of the inlined function; the current frame will be the
5232          call site.  */
5233       if (w == NULL || w->cond_exp_valid_block == NULL)
5234         select_frame (get_current_frame ());
5235       else
5236         {
5237           struct frame_info *frame;
5238
5239           /* For local watchpoint expressions, which particular
5240              instance of a local is being watched matters, so we
5241              keep track of the frame to evaluate the expression
5242              in.  To evaluate the condition however, it doesn't
5243              really matter which instantiation of the function
5244              where the condition makes sense triggers the
5245              watchpoint.  This allows an expression like "watch
5246              global if q > 10" set in `func', catch writes to
5247              global on all threads that call `func', or catch
5248              writes on all recursive calls of `func' by a single
5249              thread.  We simply always evaluate the condition in
5250              the innermost frame that's executing where it makes
5251              sense to evaluate the condition.  It seems
5252              intuitive.  */
5253           frame = block_innermost_frame (w->cond_exp_valid_block);
5254           if (frame != NULL)
5255             select_frame (frame);
5256           else
5257             within_current_scope = 0;
5258         }
5259       if (within_current_scope)
5260         value_is_zero
5261           = catch_errors (breakpoint_cond_eval, cond,
5262                           "Error in testing breakpoint condition:\n",
5263                           RETURN_MASK_ALL);
5264       else
5265         {
5266           warning (_("Watchpoint condition cannot be tested "
5267                      "in the current scope"));
5268           /* If we failed to set the right context for this
5269              watchpoint, unconditionally report it.  */
5270           value_is_zero = 0;
5271         }
5272       /* FIXME-someday, should give breakpoint #.  */
5273       value_free_to_mark (mark);
5274     }
5275
5276   if (cond && value_is_zero)
5277     {
5278       bs->stop = 0;
5279     }
5280   else if (b->ignore_count > 0)
5281     {
5282       b->ignore_count--;
5283       bs->stop = 0;
5284       /* Increase the hit count even though we don't stop.  */
5285       ++(b->hit_count);
5286       observer_notify_breakpoint_modified (b);
5287     }   
5288 }
5289
5290
5291 /* Get a bpstat associated with having just stopped at address
5292    BP_ADDR in thread PTID.
5293
5294    Determine whether we stopped at a breakpoint, etc, or whether we
5295    don't understand this stop.  Result is a chain of bpstat's such
5296    that:
5297
5298    if we don't understand the stop, the result is a null pointer.
5299
5300    if we understand why we stopped, the result is not null.
5301
5302    Each element of the chain refers to a particular breakpoint or
5303    watchpoint at which we have stopped.  (We may have stopped for
5304    several reasons concurrently.)
5305
5306    Each element of the chain has valid next, breakpoint_at,
5307    commands, FIXME??? fields.  */
5308
5309 bpstat
5310 bpstat_stop_status (struct address_space *aspace,
5311                     CORE_ADDR bp_addr, ptid_t ptid,
5312                     const struct target_waitstatus *ws)
5313 {
5314   struct breakpoint *b = NULL;
5315   struct bp_location *bl;
5316   struct bp_location *loc;
5317   /* First item of allocated bpstat's.  */
5318   bpstat bs_head = NULL, *bs_link = &bs_head;
5319   /* Pointer to the last thing in the chain currently.  */
5320   bpstat bs;
5321   int ix;
5322   int need_remove_insert;
5323   int removed_any;
5324
5325   /* First, build the bpstat chain with locations that explain a
5326      target stop, while being careful to not set the target running,
5327      as that may invalidate locations (in particular watchpoint
5328      locations are recreated).  Resuming will happen here with
5329      breakpoint conditions or watchpoint expressions that include
5330      inferior function calls.  */
5331
5332   ALL_BREAKPOINTS (b)
5333     {
5334       if (!breakpoint_enabled (b) && b->enable_state != bp_permanent)
5335         continue;
5336
5337       for (bl = b->loc; bl != NULL; bl = bl->next)
5338         {
5339           /* For hardware watchpoints, we look only at the first
5340              location.  The watchpoint_check function will work on the
5341              entire expression, not the individual locations.  For
5342              read watchpoints, the watchpoints_triggered function has
5343              checked all locations already.  */
5344           if (b->type == bp_hardware_watchpoint && bl != b->loc)
5345             break;
5346
5347           if (!bl->enabled || bl->shlib_disabled)
5348             continue;
5349
5350           if (!bpstat_check_location (bl, aspace, bp_addr, ws))
5351             continue;
5352
5353           /* Come here if it's a watchpoint, or if the break address
5354              matches.  */
5355
5356           bs = bpstat_alloc (bl, &bs_link);     /* Alloc a bpstat to
5357                                                    explain stop.  */
5358
5359           /* Assume we stop.  Should we find a watchpoint that is not
5360              actually triggered, or if the condition of the breakpoint
5361              evaluates as false, we'll reset 'stop' to 0.  */
5362           bs->stop = 1;
5363           bs->print = 1;
5364
5365           /* If this is a scope breakpoint, mark the associated
5366              watchpoint as triggered so that we will handle the
5367              out-of-scope event.  We'll get to the watchpoint next
5368              iteration.  */
5369           if (b->type == bp_watchpoint_scope && b->related_breakpoint != b)
5370             {
5371               struct watchpoint *w = (struct watchpoint *) b->related_breakpoint;
5372
5373               w->watchpoint_triggered = watch_triggered_yes;
5374             }
5375         }
5376     }
5377
5378   for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, loc); ++ix)
5379     {
5380       if (breakpoint_location_address_match (loc, aspace, bp_addr))
5381         {
5382           bs = bpstat_alloc (loc, &bs_link);
5383           /* For hits of moribund locations, we should just proceed.  */
5384           bs->stop = 0;
5385           bs->print = 0;
5386           bs->print_it = print_it_noop;
5387         }
5388     }
5389
5390   /* A bit of special processing for shlib breakpoints.  We need to
5391      process solib loading here, so that the lists of loaded and
5392      unloaded libraries are correct before we handle "catch load" and
5393      "catch unload".  */
5394   for (bs = bs_head; bs != NULL; bs = bs->next)
5395     {
5396       if (bs->breakpoint_at && bs->breakpoint_at->type == bp_shlib_event)
5397         {
5398           handle_solib_event ();
5399           break;
5400         }
5401     }
5402
5403   /* Now go through the locations that caused the target to stop, and
5404      check whether we're interested in reporting this stop to higher
5405      layers, or whether we should resume the target transparently.  */
5406
5407   removed_any = 0;
5408
5409   for (bs = bs_head; bs != NULL; bs = bs->next)
5410     {
5411       if (!bs->stop)
5412         continue;
5413
5414       b = bs->breakpoint_at;
5415       b->ops->check_status (bs);
5416       if (bs->stop)
5417         {
5418           bpstat_check_breakpoint_conditions (bs, ptid);
5419
5420           if (bs->stop)
5421             {
5422               ++(b->hit_count);
5423               observer_notify_breakpoint_modified (b);
5424
5425               /* We will stop here.  */
5426               if (b->disposition == disp_disable)
5427                 {
5428                   --(b->enable_count);
5429                   if (b->enable_count <= 0
5430                       && b->enable_state != bp_permanent)
5431                     b->enable_state = bp_disabled;
5432                   removed_any = 1;
5433                 }
5434               if (b->silent)
5435                 bs->print = 0;
5436               bs->commands = b->commands;
5437               incref_counted_command_line (bs->commands);
5438               if (command_line_is_silent (bs->commands
5439                                           ? bs->commands->commands : NULL))
5440                 bs->print = 0;
5441
5442               b->ops->after_condition_true (bs);
5443             }
5444
5445         }
5446
5447       /* Print nothing for this entry if we don't stop or don't
5448          print.  */
5449       if (!bs->stop || !bs->print)
5450         bs->print_it = print_it_noop;
5451     }
5452
5453   /* If we aren't stopping, the value of some hardware watchpoint may
5454      not have changed, but the intermediate memory locations we are
5455      watching may have.  Don't bother if we're stopping; this will get
5456      done later.  */
5457   need_remove_insert = 0;
5458   if (! bpstat_causes_stop (bs_head))
5459     for (bs = bs_head; bs != NULL; bs = bs->next)
5460       if (!bs->stop
5461           && bs->breakpoint_at
5462           && is_hardware_watchpoint (bs->breakpoint_at))
5463         {
5464           struct watchpoint *w = (struct watchpoint *) bs->breakpoint_at;
5465
5466           update_watchpoint (w, 0 /* don't reparse.  */);
5467           need_remove_insert = 1;
5468         }
5469
5470   if (need_remove_insert)
5471     update_global_location_list (1);
5472   else if (removed_any)
5473     update_global_location_list (0);
5474
5475   return bs_head;
5476 }
5477
5478 static void
5479 handle_jit_event (void)
5480 {
5481   struct frame_info *frame;
5482   struct gdbarch *gdbarch;
5483
5484   /* Switch terminal for any messages produced by
5485      breakpoint_re_set.  */
5486   target_terminal_ours_for_output ();
5487
5488   frame = get_current_frame ();
5489   gdbarch = get_frame_arch (frame);
5490
5491   jit_event_handler (gdbarch);
5492
5493   target_terminal_inferior ();
5494 }
5495
5496 /* Prepare WHAT final decision for infrun.  */
5497
5498 /* Decide what infrun needs to do with this bpstat.  */
5499
5500 struct bpstat_what
5501 bpstat_what (bpstat bs_head)
5502 {
5503   struct bpstat_what retval;
5504   int jit_event = 0;
5505   bpstat bs;
5506
5507   retval.main_action = BPSTAT_WHAT_KEEP_CHECKING;
5508   retval.call_dummy = STOP_NONE;
5509   retval.is_longjmp = 0;
5510
5511   for (bs = bs_head; bs != NULL; bs = bs->next)
5512     {
5513       /* Extract this BS's action.  After processing each BS, we check
5514          if its action overrides all we've seem so far.  */
5515       enum bpstat_what_main_action this_action = BPSTAT_WHAT_KEEP_CHECKING;
5516       enum bptype bptype;
5517
5518       if (bs->breakpoint_at == NULL)
5519         {
5520           /* I suspect this can happen if it was a momentary
5521              breakpoint which has since been deleted.  */
5522           bptype = bp_none;
5523         }
5524       else
5525         bptype = bs->breakpoint_at->type;
5526
5527       switch (bptype)
5528         {
5529         case bp_none:
5530           break;
5531         case bp_breakpoint:
5532         case bp_hardware_breakpoint:
5533         case bp_until:
5534         case bp_finish:
5535         case bp_shlib_event:
5536           if (bs->stop)
5537             {
5538               if (bs->print)
5539                 this_action = BPSTAT_WHAT_STOP_NOISY;
5540               else
5541                 this_action = BPSTAT_WHAT_STOP_SILENT;
5542             }
5543           else
5544             this_action = BPSTAT_WHAT_SINGLE;
5545           break;
5546         case bp_watchpoint:
5547         case bp_hardware_watchpoint:
5548         case bp_read_watchpoint:
5549         case bp_access_watchpoint:
5550           if (bs->stop)
5551             {
5552               if (bs->print)
5553                 this_action = BPSTAT_WHAT_STOP_NOISY;
5554               else
5555                 this_action = BPSTAT_WHAT_STOP_SILENT;
5556             }
5557           else
5558             {
5559               /* There was a watchpoint, but we're not stopping.
5560                  This requires no further action.  */
5561             }
5562           break;
5563         case bp_longjmp:
5564         case bp_longjmp_call_dummy:
5565         case bp_exception:
5566           this_action = BPSTAT_WHAT_SET_LONGJMP_RESUME;
5567           retval.is_longjmp = bptype != bp_exception;
5568           break;
5569         case bp_longjmp_resume:
5570         case bp_exception_resume:
5571           this_action = BPSTAT_WHAT_CLEAR_LONGJMP_RESUME;
5572           retval.is_longjmp = bptype == bp_longjmp_resume;
5573           break;
5574         case bp_step_resume:
5575           if (bs->stop)
5576             this_action = BPSTAT_WHAT_STEP_RESUME;
5577           else
5578             {
5579               /* It is for the wrong frame.  */
5580               this_action = BPSTAT_WHAT_SINGLE;
5581             }
5582           break;
5583         case bp_hp_step_resume:
5584           if (bs->stop)
5585             this_action = BPSTAT_WHAT_HP_STEP_RESUME;
5586           else
5587             {
5588               /* It is for the wrong frame.  */
5589               this_action = BPSTAT_WHAT_SINGLE;
5590             }
5591           break;
5592         case bp_watchpoint_scope:
5593         case bp_thread_event:
5594         case bp_overlay_event:
5595         case bp_longjmp_master:
5596         case bp_std_terminate_master:
5597         case bp_exception_master:
5598           this_action = BPSTAT_WHAT_SINGLE;
5599           break;
5600         case bp_catchpoint:
5601           if (bs->stop)
5602             {
5603               if (bs->print)
5604                 this_action = BPSTAT_WHAT_STOP_NOISY;
5605               else
5606                 this_action = BPSTAT_WHAT_STOP_SILENT;
5607             }
5608           else
5609             {
5610               /* There was a catchpoint, but we're not stopping.
5611                  This requires no further action.  */
5612             }
5613           break;
5614         case bp_jit_event:
5615           jit_event = 1;
5616           this_action = BPSTAT_WHAT_SINGLE;
5617           break;
5618         case bp_call_dummy:
5619           /* Make sure the action is stop (silent or noisy),
5620              so infrun.c pops the dummy frame.  */
5621           retval.call_dummy = STOP_STACK_DUMMY;
5622           this_action = BPSTAT_WHAT_STOP_SILENT;
5623           break;
5624         case bp_std_terminate:
5625           /* Make sure the action is stop (silent or noisy),
5626              so infrun.c pops the dummy frame.  */
5627           retval.call_dummy = STOP_STD_TERMINATE;
5628           this_action = BPSTAT_WHAT_STOP_SILENT;
5629           break;
5630         case bp_tracepoint:
5631         case bp_fast_tracepoint:
5632         case bp_static_tracepoint:
5633           /* Tracepoint hits should not be reported back to GDB, and
5634              if one got through somehow, it should have been filtered
5635              out already.  */
5636           internal_error (__FILE__, __LINE__,
5637                           _("bpstat_what: tracepoint encountered"));
5638           break;
5639         case bp_gnu_ifunc_resolver:
5640           /* Step over it (and insert bp_gnu_ifunc_resolver_return).  */
5641           this_action = BPSTAT_WHAT_SINGLE;
5642           break;
5643         case bp_gnu_ifunc_resolver_return:
5644           /* The breakpoint will be removed, execution will restart from the
5645              PC of the former breakpoint.  */
5646           this_action = BPSTAT_WHAT_KEEP_CHECKING;
5647           break;
5648
5649         case bp_dprintf:
5650           if (bs->stop)
5651             this_action = BPSTAT_WHAT_STOP_SILENT;
5652           else
5653             this_action = BPSTAT_WHAT_SINGLE;
5654           break;
5655
5656         default:
5657           internal_error (__FILE__, __LINE__,
5658                           _("bpstat_what: unhandled bptype %d"), (int) bptype);
5659         }
5660
5661       retval.main_action = max (retval.main_action, this_action);
5662     }
5663
5664   /* These operations may affect the bs->breakpoint_at state so they are
5665      delayed after MAIN_ACTION is decided above.  */
5666
5667   if (jit_event)
5668     {
5669       if (debug_infrun)
5670         fprintf_unfiltered (gdb_stdlog, "bpstat_what: bp_jit_event\n");
5671
5672       handle_jit_event ();
5673     }
5674
5675   for (bs = bs_head; bs != NULL; bs = bs->next)
5676     {
5677       struct breakpoint *b = bs->breakpoint_at;
5678
5679       if (b == NULL)
5680         continue;
5681       switch (b->type)
5682         {
5683         case bp_gnu_ifunc_resolver:
5684           gnu_ifunc_resolver_stop (b);
5685           break;
5686         case bp_gnu_ifunc_resolver_return:
5687           gnu_ifunc_resolver_return_stop (b);
5688           break;
5689         }
5690     }
5691
5692   return retval;
5693 }
5694
5695 /* Nonzero if we should step constantly (e.g. watchpoints on machines
5696    without hardware support).  This isn't related to a specific bpstat,
5697    just to things like whether watchpoints are set.  */
5698
5699 int
5700 bpstat_should_step (void)
5701 {
5702   struct breakpoint *b;
5703
5704   ALL_BREAKPOINTS (b)
5705     if (breakpoint_enabled (b) && b->type == bp_watchpoint && b->loc != NULL)
5706       return 1;
5707   return 0;
5708 }
5709
5710 int
5711 bpstat_causes_stop (bpstat bs)
5712 {
5713   for (; bs != NULL; bs = bs->next)
5714     if (bs->stop)
5715       return 1;
5716
5717   return 0;
5718 }
5719
5720 \f
5721
5722 /* Compute a string of spaces suitable to indent the next line
5723    so it starts at the position corresponding to the table column
5724    named COL_NAME in the currently active table of UIOUT.  */
5725
5726 static char *
5727 wrap_indent_at_field (struct ui_out *uiout, const char *col_name)
5728 {
5729   static char wrap_indent[80];
5730   int i, total_width, width, align;
5731   char *text;
5732
5733   total_width = 0;
5734   for (i = 1; ui_out_query_field (uiout, i, &width, &align, &text); i++)
5735     {
5736       if (strcmp (text, col_name) == 0)
5737         {
5738           gdb_assert (total_width < sizeof wrap_indent);
5739           memset (wrap_indent, ' ', total_width);
5740           wrap_indent[total_width] = 0;
5741
5742           return wrap_indent;
5743         }
5744
5745       total_width += width + 1;
5746     }
5747
5748   return NULL;
5749 }
5750
5751 /* Determine if the locations of this breakpoint will have their conditions
5752    evaluated by the target, host or a mix of both.  Returns the following:
5753
5754     "host": Host evals condition.
5755     "host or target": Host or Target evals condition.
5756     "target": Target evals condition.
5757 */
5758
5759 static const char *
5760 bp_condition_evaluator (struct breakpoint *b)
5761 {
5762   struct bp_location *bl;
5763   char host_evals = 0;
5764   char target_evals = 0;
5765
5766   if (!b)
5767     return NULL;
5768
5769   if (!is_breakpoint (b))
5770     return NULL;
5771
5772   if (gdb_evaluates_breakpoint_condition_p ()
5773       || !target_supports_evaluation_of_breakpoint_conditions ())
5774     return condition_evaluation_host;
5775
5776   for (bl = b->loc; bl; bl = bl->next)
5777     {
5778       if (bl->cond_bytecode)
5779         target_evals++;
5780       else
5781         host_evals++;
5782     }
5783
5784   if (host_evals && target_evals)
5785     return condition_evaluation_both;
5786   else if (target_evals)
5787     return condition_evaluation_target;
5788   else
5789     return condition_evaluation_host;
5790 }
5791
5792 /* Determine the breakpoint location's condition evaluator.  This is
5793    similar to bp_condition_evaluator, but for locations.  */
5794
5795 static const char *
5796 bp_location_condition_evaluator (struct bp_location *bl)
5797 {
5798   if (bl && !is_breakpoint (bl->owner))
5799     return NULL;
5800
5801   if (gdb_evaluates_breakpoint_condition_p ()
5802       || !target_supports_evaluation_of_breakpoint_conditions ())
5803     return condition_evaluation_host;
5804
5805   if (bl && bl->cond_bytecode)
5806     return condition_evaluation_target;
5807   else
5808     return condition_evaluation_host;
5809 }
5810
5811 /* Print the LOC location out of the list of B->LOC locations.  */
5812
5813 static void
5814 print_breakpoint_location (struct breakpoint *b,
5815                            struct bp_location *loc)
5816 {
5817   struct ui_out *uiout = current_uiout;
5818   struct cleanup *old_chain = save_current_program_space ();
5819
5820   if (loc != NULL && loc->shlib_disabled)
5821     loc = NULL;
5822
5823   if (loc != NULL)
5824     set_current_program_space (loc->pspace);
5825
5826   if (b->display_canonical)
5827     ui_out_field_string (uiout, "what", b->addr_string);
5828   else if (loc && loc->symtab)
5829     {
5830       struct symbol *sym 
5831         = find_pc_sect_function (loc->address, loc->section);
5832       if (sym)
5833         {
5834           ui_out_text (uiout, "in ");
5835           ui_out_field_string (uiout, "func",
5836                                SYMBOL_PRINT_NAME (sym));
5837           ui_out_text (uiout, " ");
5838           ui_out_wrap_hint (uiout, wrap_indent_at_field (uiout, "what"));
5839           ui_out_text (uiout, "at ");
5840         }
5841       ui_out_field_string (uiout, "file",
5842                            symtab_to_filename_for_display (loc->symtab));
5843       ui_out_text (uiout, ":");
5844
5845       if (ui_out_is_mi_like_p (uiout))
5846         ui_out_field_string (uiout, "fullname",
5847                              symtab_to_fullname (loc->symtab));
5848       
5849       ui_out_field_int (uiout, "line", loc->line_number);
5850     }
5851   else if (loc)
5852     {
5853       struct ui_file *stb = mem_fileopen ();
5854       struct cleanup *stb_chain = make_cleanup_ui_file_delete (stb);
5855
5856       print_address_symbolic (loc->gdbarch, loc->address, stb,
5857                               demangle, "");
5858       ui_out_field_stream (uiout, "at", stb);
5859
5860       do_cleanups (stb_chain);
5861     }
5862   else
5863     ui_out_field_string (uiout, "pending", b->addr_string);
5864
5865   if (loc && is_breakpoint (b)
5866       && breakpoint_condition_evaluation_mode () == condition_evaluation_target
5867       && bp_condition_evaluator (b) == condition_evaluation_both)
5868     {
5869       ui_out_text (uiout, " (");
5870       ui_out_field_string (uiout, "evaluated-by",
5871                            bp_location_condition_evaluator (loc));
5872       ui_out_text (uiout, ")");
5873     }
5874
5875   do_cleanups (old_chain);
5876 }
5877
5878 static const char *
5879 bptype_string (enum bptype type)
5880 {
5881   struct ep_type_description
5882     {
5883       enum bptype type;
5884       char *description;
5885     };
5886   static struct ep_type_description bptypes[] =
5887   {
5888     {bp_none, "?deleted?"},
5889     {bp_breakpoint, "breakpoint"},
5890     {bp_hardware_breakpoint, "hw breakpoint"},
5891     {bp_until, "until"},
5892     {bp_finish, "finish"},
5893     {bp_watchpoint, "watchpoint"},
5894     {bp_hardware_watchpoint, "hw watchpoint"},
5895     {bp_read_watchpoint, "read watchpoint"},
5896     {bp_access_watchpoint, "acc watchpoint"},
5897     {bp_longjmp, "longjmp"},
5898     {bp_longjmp_resume, "longjmp resume"},
5899     {bp_longjmp_call_dummy, "longjmp for call dummy"},
5900     {bp_exception, "exception"},
5901     {bp_exception_resume, "exception resume"},
5902     {bp_step_resume, "step resume"},
5903     {bp_hp_step_resume, "high-priority step resume"},
5904     {bp_watchpoint_scope, "watchpoint scope"},
5905     {bp_call_dummy, "call dummy"},
5906     {bp_std_terminate, "std::terminate"},
5907     {bp_shlib_event, "shlib events"},
5908     {bp_thread_event, "thread events"},
5909     {bp_overlay_event, "overlay events"},
5910     {bp_longjmp_master, "longjmp master"},
5911     {bp_std_terminate_master, "std::terminate master"},
5912     {bp_exception_master, "exception master"},
5913     {bp_catchpoint, "catchpoint"},
5914     {bp_tracepoint, "tracepoint"},
5915     {bp_fast_tracepoint, "fast tracepoint"},
5916     {bp_static_tracepoint, "static tracepoint"},
5917     {bp_dprintf, "dprintf"},
5918     {bp_jit_event, "jit events"},
5919     {bp_gnu_ifunc_resolver, "STT_GNU_IFUNC resolver"},
5920     {bp_gnu_ifunc_resolver_return, "STT_GNU_IFUNC resolver return"},
5921   };
5922
5923   if (((int) type >= (sizeof (bptypes) / sizeof (bptypes[0])))
5924       || ((int) type != bptypes[(int) type].type))
5925     internal_error (__FILE__, __LINE__,
5926                     _("bptypes table does not describe type #%d."),
5927                     (int) type);
5928
5929   return bptypes[(int) type].description;
5930 }
5931
5932 /* For MI, output a field named 'thread-groups' with a list as the value.
5933    For CLI, prefix the list with the string 'inf'. */
5934
5935 static void
5936 output_thread_groups (struct ui_out *uiout,
5937                       const char *field_name,
5938                       VEC(int) *inf_num,
5939                       int mi_only)
5940 {
5941   struct cleanup *back_to;
5942   int is_mi = ui_out_is_mi_like_p (uiout);
5943   int inf;
5944   int i;
5945
5946   /* For backward compatibility, don't display inferiors in CLI unless
5947      there are several.  Always display them for MI. */
5948   if (!is_mi && mi_only)
5949     return;
5950
5951   back_to = make_cleanup_ui_out_list_begin_end (uiout, field_name);
5952
5953   for (i = 0; VEC_iterate (int, inf_num, i, inf); ++i)
5954     {
5955       if (is_mi)
5956         {
5957           char mi_group[10];
5958
5959           xsnprintf (mi_group, sizeof (mi_group), "i%d", inf);
5960           ui_out_field_string (uiout, NULL, mi_group);
5961         }
5962       else
5963         {
5964           if (i == 0)
5965             ui_out_text (uiout, " inf ");
5966           else
5967             ui_out_text (uiout, ", ");
5968         
5969           ui_out_text (uiout, plongest (inf));
5970         }
5971     }
5972
5973   do_cleanups (back_to);
5974 }
5975
5976 /* Print B to gdb_stdout.  */
5977
5978 static void
5979 print_one_breakpoint_location (struct breakpoint *b,
5980                                struct bp_location *loc,
5981                                int loc_number,
5982                                struct bp_location **last_loc,
5983                                int allflag)
5984 {
5985   struct command_line *l;
5986   static char bpenables[] = "nynny";
5987
5988   struct ui_out *uiout = current_uiout;
5989   int header_of_multiple = 0;
5990   int part_of_multiple = (loc != NULL);
5991   struct value_print_options opts;
5992
5993   get_user_print_options (&opts);
5994
5995   gdb_assert (!loc || loc_number != 0);
5996   /* See comment in print_one_breakpoint concerning treatment of
5997      breakpoints with single disabled location.  */
5998   if (loc == NULL 
5999       && (b->loc != NULL 
6000           && (b->loc->next != NULL || !b->loc->enabled)))
6001     header_of_multiple = 1;
6002   if (loc == NULL)
6003     loc = b->loc;
6004
6005   annotate_record ();
6006
6007   /* 1 */
6008   annotate_field (0);
6009   if (part_of_multiple)
6010     {
6011       char *formatted;
6012       formatted = xstrprintf ("%d.%d", b->number, loc_number);
6013       ui_out_field_string (uiout, "number", formatted);
6014       xfree (formatted);
6015     }
6016   else
6017     {
6018       ui_out_field_int (uiout, "number", b->number);
6019     }
6020
6021   /* 2 */
6022   annotate_field (1);
6023   if (part_of_multiple)
6024     ui_out_field_skip (uiout, "type");
6025   else
6026     ui_out_field_string (uiout, "type", bptype_string (b->type));
6027
6028   /* 3 */
6029   annotate_field (2);
6030   if (part_of_multiple)
6031     ui_out_field_skip (uiout, "disp");
6032   else
6033     ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
6034
6035
6036   /* 4 */
6037   annotate_field (3);
6038   if (part_of_multiple)
6039     ui_out_field_string (uiout, "enabled", loc->enabled ? "y" : "n");
6040   else
6041     ui_out_field_fmt (uiout, "enabled", "%c", 
6042                       bpenables[(int) b->enable_state]);
6043   ui_out_spaces (uiout, 2);
6044
6045   
6046   /* 5 and 6 */
6047   if (b->ops != NULL && b->ops->print_one != NULL)
6048     {
6049       /* Although the print_one can possibly print all locations,
6050          calling it here is not likely to get any nice result.  So,
6051          make sure there's just one location.  */
6052       gdb_assert (b->loc == NULL || b->loc->next == NULL);
6053       b->ops->print_one (b, last_loc);
6054     }
6055   else
6056     switch (b->type)
6057       {
6058       case bp_none:
6059         internal_error (__FILE__, __LINE__,
6060                         _("print_one_breakpoint: bp_none encountered\n"));
6061         break;
6062
6063       case bp_watchpoint:
6064       case bp_hardware_watchpoint:
6065       case bp_read_watchpoint:
6066       case bp_access_watchpoint:
6067         {
6068           struct watchpoint *w = (struct watchpoint *) b;
6069
6070           /* Field 4, the address, is omitted (which makes the columns
6071              not line up too nicely with the headers, but the effect
6072              is relatively readable).  */
6073           if (opts.addressprint)
6074             ui_out_field_skip (uiout, "addr");
6075           annotate_field (5);
6076           ui_out_field_string (uiout, "what", w->exp_string);
6077         }
6078         break;
6079
6080       case bp_breakpoint:
6081       case bp_hardware_breakpoint:
6082       case bp_until:
6083       case bp_finish:
6084       case bp_longjmp:
6085       case bp_longjmp_resume:
6086       case bp_longjmp_call_dummy:
6087       case bp_exception:
6088       case bp_exception_resume:
6089       case bp_step_resume:
6090       case bp_hp_step_resume:
6091       case bp_watchpoint_scope:
6092       case bp_call_dummy:
6093       case bp_std_terminate:
6094       case bp_shlib_event:
6095       case bp_thread_event:
6096       case bp_overlay_event:
6097       case bp_longjmp_master:
6098       case bp_std_terminate_master:
6099       case bp_exception_master:
6100       case bp_tracepoint:
6101       case bp_fast_tracepoint:
6102       case bp_static_tracepoint:
6103       case bp_dprintf:
6104       case bp_jit_event:
6105       case bp_gnu_ifunc_resolver:
6106       case bp_gnu_ifunc_resolver_return:
6107         if (opts.addressprint)
6108           {
6109             annotate_field (4);
6110             if (header_of_multiple)
6111               ui_out_field_string (uiout, "addr", "<MULTIPLE>");
6112             else if (b->loc == NULL || loc->shlib_disabled)
6113               ui_out_field_string (uiout, "addr", "<PENDING>");
6114             else
6115               ui_out_field_core_addr (uiout, "addr",
6116                                       loc->gdbarch, loc->address);
6117           }
6118         annotate_field (5);
6119         if (!header_of_multiple)
6120           print_breakpoint_location (b, loc);
6121         if (b->loc)
6122           *last_loc = b->loc;
6123         break;
6124       }
6125
6126
6127   if (loc != NULL && !header_of_multiple)
6128     {
6129       struct inferior *inf;
6130       VEC(int) *inf_num = NULL;
6131       int mi_only = 1;
6132
6133       ALL_INFERIORS (inf)
6134         {
6135           if (inf->pspace == loc->pspace)
6136             VEC_safe_push (int, inf_num, inf->num);
6137         }
6138
6139         /* For backward compatibility, don't display inferiors in CLI unless
6140            there are several.  Always display for MI. */
6141         if (allflag
6142             || (!gdbarch_has_global_breakpoints (target_gdbarch ())
6143                 && (number_of_program_spaces () > 1
6144                     || number_of_inferiors () > 1)
6145                 /* LOC is for existing B, it cannot be in
6146                    moribund_locations and thus having NULL OWNER.  */
6147                 && loc->owner->type != bp_catchpoint))
6148         mi_only = 0;
6149       output_thread_groups (uiout, "thread-groups", inf_num, mi_only);
6150       VEC_free (int, inf_num);
6151     }
6152
6153   if (!part_of_multiple)
6154     {
6155       if (b->thread != -1)
6156         {
6157           /* FIXME: This seems to be redundant and lost here; see the
6158              "stop only in" line a little further down.  */
6159           ui_out_text (uiout, " thread ");
6160           ui_out_field_int (uiout, "thread", b->thread);
6161         }
6162       else if (b->task != 0)
6163         {
6164           ui_out_text (uiout, " task ");
6165           ui_out_field_int (uiout, "task", b->task);
6166         }
6167     }
6168
6169   ui_out_text (uiout, "\n");
6170
6171   if (!part_of_multiple)
6172     b->ops->print_one_detail (b, uiout);
6173
6174   if (part_of_multiple && frame_id_p (b->frame_id))
6175     {
6176       annotate_field (6);
6177       ui_out_text (uiout, "\tstop only in stack frame at ");
6178       /* FIXME: cagney/2002-12-01: Shouldn't be poking around inside
6179          the frame ID.  */
6180       ui_out_field_core_addr (uiout, "frame",
6181                               b->gdbarch, b->frame_id.stack_addr);
6182       ui_out_text (uiout, "\n");
6183     }
6184   
6185   if (!part_of_multiple && b->cond_string)
6186     {
6187       annotate_field (7);
6188       if (is_tracepoint (b))
6189         ui_out_text (uiout, "\ttrace only if ");
6190       else
6191         ui_out_text (uiout, "\tstop only if ");
6192       ui_out_field_string (uiout, "cond", b->cond_string);
6193
6194       /* Print whether the target is doing the breakpoint's condition
6195          evaluation.  If GDB is doing the evaluation, don't print anything.  */
6196       if (is_breakpoint (b)
6197           && breakpoint_condition_evaluation_mode ()
6198           == condition_evaluation_target)
6199         {
6200           ui_out_text (uiout, " (");
6201           ui_out_field_string (uiout, "evaluated-by",
6202                                bp_condition_evaluator (b));
6203           ui_out_text (uiout, " evals)");
6204         }
6205       ui_out_text (uiout, "\n");
6206     }
6207
6208   if (!part_of_multiple && b->thread != -1)
6209     {
6210       /* FIXME should make an annotation for this.  */
6211       ui_out_text (uiout, "\tstop only in thread ");
6212       ui_out_field_int (uiout, "thread", b->thread);
6213       ui_out_text (uiout, "\n");
6214     }
6215   
6216   if (!part_of_multiple)
6217     {
6218       if (b->hit_count)
6219         {
6220           /* FIXME should make an annotation for this.  */
6221           if (is_catchpoint (b))
6222             ui_out_text (uiout, "\tcatchpoint");
6223           else if (is_tracepoint (b))
6224             ui_out_text (uiout, "\ttracepoint");
6225           else
6226             ui_out_text (uiout, "\tbreakpoint");
6227           ui_out_text (uiout, " already hit ");
6228           ui_out_field_int (uiout, "times", b->hit_count);
6229           if (b->hit_count == 1)
6230             ui_out_text (uiout, " time\n");
6231           else
6232             ui_out_text (uiout, " times\n");
6233         }
6234       else
6235         {
6236           /* Output the count also if it is zero, but only if this is mi.  */
6237           if (ui_out_is_mi_like_p (uiout))
6238             ui_out_field_int (uiout, "times", b->hit_count);
6239         }
6240     }
6241
6242   if (!part_of_multiple && b->ignore_count)
6243     {
6244       annotate_field (8);
6245       ui_out_text (uiout, "\tignore next ");
6246       ui_out_field_int (uiout, "ignore", b->ignore_count);
6247       ui_out_text (uiout, " hits\n");
6248     }
6249
6250   /* Note that an enable count of 1 corresponds to "enable once"
6251      behavior, which is reported by the combination of enablement and
6252      disposition, so we don't need to mention it here.  */
6253   if (!part_of_multiple && b->enable_count > 1)
6254     {
6255       annotate_field (8);
6256       ui_out_text (uiout, "\tdisable after ");
6257       /* Tweak the wording to clarify that ignore and enable counts
6258          are distinct, and have additive effect.  */
6259       if (b->ignore_count)
6260         ui_out_text (uiout, "additional ");
6261       else
6262         ui_out_text (uiout, "next ");
6263       ui_out_field_int (uiout, "enable", b->enable_count);
6264       ui_out_text (uiout, " hits\n");
6265     }
6266
6267   if (!part_of_multiple && is_tracepoint (b))
6268     {
6269       struct tracepoint *tp = (struct tracepoint *) b;
6270
6271       if (tp->traceframe_usage)
6272         {
6273           ui_out_text (uiout, "\ttrace buffer usage ");
6274           ui_out_field_int (uiout, "traceframe-usage", tp->traceframe_usage);
6275           ui_out_text (uiout, " bytes\n");
6276         }
6277     }
6278
6279   l = b->commands ? b->commands->commands : NULL;
6280   if (!part_of_multiple && l)
6281     {
6282       struct cleanup *script_chain;
6283
6284       annotate_field (9);
6285       script_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "script");
6286       print_command_lines (uiout, l, 4);
6287       do_cleanups (script_chain);
6288     }
6289
6290   if (is_tracepoint (b))
6291     {
6292       struct tracepoint *t = (struct tracepoint *) b;
6293
6294       if (!part_of_multiple && t->pass_count)
6295         {
6296           annotate_field (10);
6297           ui_out_text (uiout, "\tpass count ");
6298           ui_out_field_int (uiout, "pass", t->pass_count);
6299           ui_out_text (uiout, " \n");
6300         }
6301
6302       /* Don't display it when tracepoint or tracepoint location is
6303          pending.   */
6304       if (!header_of_multiple && loc != NULL && !loc->shlib_disabled)
6305         {
6306           annotate_field (11);
6307
6308           if (ui_out_is_mi_like_p (uiout))
6309             ui_out_field_string (uiout, "installed",
6310                                  loc->inserted ? "y" : "n");
6311           else
6312             {
6313               if (loc->inserted)
6314                 ui_out_text (uiout, "\t");
6315               else
6316                 ui_out_text (uiout, "\tnot ");
6317               ui_out_text (uiout, "installed on target\n");
6318             }
6319         }
6320     }
6321
6322   if (ui_out_is_mi_like_p (uiout) && !part_of_multiple)
6323     {
6324       if (is_watchpoint (b))
6325         {
6326           struct watchpoint *w = (struct watchpoint *) b;
6327
6328           ui_out_field_string (uiout, "original-location", w->exp_string);
6329         }
6330       else if (b->addr_string)
6331         ui_out_field_string (uiout, "original-location", b->addr_string);
6332     }
6333 }
6334
6335 static void
6336 print_one_breakpoint (struct breakpoint *b,
6337                       struct bp_location **last_loc, 
6338                       int allflag)
6339 {
6340   struct cleanup *bkpt_chain;
6341   struct ui_out *uiout = current_uiout;
6342
6343   bkpt_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "bkpt");
6344
6345   print_one_breakpoint_location (b, NULL, 0, last_loc, allflag);
6346   do_cleanups (bkpt_chain);
6347
6348   /* If this breakpoint has custom print function,
6349      it's already printed.  Otherwise, print individual
6350      locations, if any.  */
6351   if (b->ops == NULL || b->ops->print_one == NULL)
6352     {
6353       /* If breakpoint has a single location that is disabled, we
6354          print it as if it had several locations, since otherwise it's
6355          hard to represent "breakpoint enabled, location disabled"
6356          situation.
6357
6358          Note that while hardware watchpoints have several locations
6359          internally, that's not a property exposed to user.  */
6360       if (b->loc 
6361           && !is_hardware_watchpoint (b)
6362           && (b->loc->next || !b->loc->enabled))
6363         {
6364           struct bp_location *loc;
6365           int n = 1;
6366
6367           for (loc = b->loc; loc; loc = loc->next, ++n)
6368             {
6369               struct cleanup *inner2 =
6370                 make_cleanup_ui_out_tuple_begin_end (uiout, NULL);
6371               print_one_breakpoint_location (b, loc, n, last_loc, allflag);
6372               do_cleanups (inner2);
6373             }
6374         }
6375     }
6376 }
6377
6378 static int
6379 breakpoint_address_bits (struct breakpoint *b)
6380 {
6381   int print_address_bits = 0;
6382   struct bp_location *loc;
6383
6384   for (loc = b->loc; loc; loc = loc->next)
6385     {
6386       int addr_bit;
6387
6388       /* Software watchpoints that aren't watching memory don't have
6389          an address to print.  */
6390       if (b->type == bp_watchpoint && loc->watchpoint_type == -1)
6391         continue;
6392
6393       addr_bit = gdbarch_addr_bit (loc->gdbarch);
6394       if (addr_bit > print_address_bits)
6395         print_address_bits = addr_bit;
6396     }
6397
6398   return print_address_bits;
6399 }
6400
6401 struct captured_breakpoint_query_args
6402   {
6403     int bnum;
6404   };
6405
6406 static int
6407 do_captured_breakpoint_query (struct ui_out *uiout, void *data)
6408 {
6409   struct captured_breakpoint_query_args *args = data;
6410   struct breakpoint *b;
6411   struct bp_location *dummy_loc = NULL;
6412
6413   ALL_BREAKPOINTS (b)
6414     {
6415       if (args->bnum == b->number)
6416         {
6417           print_one_breakpoint (b, &dummy_loc, 0);
6418           return GDB_RC_OK;
6419         }
6420     }
6421   return GDB_RC_NONE;
6422 }
6423
6424 enum gdb_rc
6425 gdb_breakpoint_query (struct ui_out *uiout, int bnum, 
6426                       char **error_message)
6427 {
6428   struct captured_breakpoint_query_args args;
6429
6430   args.bnum = bnum;
6431   /* For the moment we don't trust print_one_breakpoint() to not throw
6432      an error.  */
6433   if (catch_exceptions_with_msg (uiout, do_captured_breakpoint_query, &args,
6434                                  error_message, RETURN_MASK_ALL) < 0)
6435     return GDB_RC_FAIL;
6436   else
6437     return GDB_RC_OK;
6438 }
6439
6440 /* Return true if this breakpoint was set by the user, false if it is
6441    internal or momentary.  */
6442
6443 int
6444 user_breakpoint_p (struct breakpoint *b)
6445 {
6446   return b->number > 0;
6447 }
6448
6449 /* Print information on user settable breakpoint (watchpoint, etc)
6450    number BNUM.  If BNUM is -1 print all user-settable breakpoints.
6451    If ALLFLAG is non-zero, include non-user-settable breakpoints.  If
6452    FILTER is non-NULL, call it on each breakpoint and only include the
6453    ones for which it returns non-zero.  Return the total number of
6454    breakpoints listed.  */
6455
6456 static int
6457 breakpoint_1 (char *args, int allflag, 
6458               int (*filter) (const struct breakpoint *))
6459 {
6460   struct breakpoint *b;
6461   struct bp_location *last_loc = NULL;
6462   int nr_printable_breakpoints;
6463   struct cleanup *bkpttbl_chain;
6464   struct value_print_options opts;
6465   int print_address_bits = 0;
6466   int print_type_col_width = 14;
6467   struct ui_out *uiout = current_uiout;
6468
6469   get_user_print_options (&opts);
6470
6471   /* Compute the number of rows in the table, as well as the size
6472      required for address fields.  */
6473   nr_printable_breakpoints = 0;
6474   ALL_BREAKPOINTS (b)
6475     {
6476       /* If we have a filter, only list the breakpoints it accepts.  */
6477       if (filter && !filter (b))
6478         continue;
6479
6480       /* If we have an "args" string, it is a list of breakpoints to 
6481          accept.  Skip the others.  */
6482       if (args != NULL && *args != '\0')
6483         {
6484           if (allflag && parse_and_eval_long (args) != b->number)
6485             continue;
6486           if (!allflag && !number_is_in_list (args, b->number))
6487             continue;
6488         }
6489
6490       if (allflag || user_breakpoint_p (b))
6491         {
6492           int addr_bit, type_len;
6493
6494           addr_bit = breakpoint_address_bits (b);
6495           if (addr_bit > print_address_bits)
6496             print_address_bits = addr_bit;
6497
6498           type_len = strlen (bptype_string (b->type));
6499           if (type_len > print_type_col_width)
6500             print_type_col_width = type_len;
6501
6502           nr_printable_breakpoints++;
6503         }
6504     }
6505
6506   if (opts.addressprint)
6507     bkpttbl_chain 
6508       = make_cleanup_ui_out_table_begin_end (uiout, 6,
6509                                              nr_printable_breakpoints,
6510                                              "BreakpointTable");
6511   else
6512     bkpttbl_chain 
6513       = make_cleanup_ui_out_table_begin_end (uiout, 5,
6514                                              nr_printable_breakpoints,
6515                                              "BreakpointTable");
6516
6517   if (nr_printable_breakpoints > 0)
6518     annotate_breakpoints_headers ();
6519   if (nr_printable_breakpoints > 0)
6520     annotate_field (0);
6521   ui_out_table_header (uiout, 7, ui_left, "number", "Num");     /* 1 */
6522   if (nr_printable_breakpoints > 0)
6523     annotate_field (1);
6524   ui_out_table_header (uiout, print_type_col_width, ui_left,
6525                        "type", "Type");                         /* 2 */
6526   if (nr_printable_breakpoints > 0)
6527     annotate_field (2);
6528   ui_out_table_header (uiout, 4, ui_left, "disp", "Disp");      /* 3 */
6529   if (nr_printable_breakpoints > 0)
6530     annotate_field (3);
6531   ui_out_table_header (uiout, 3, ui_left, "enabled", "Enb");    /* 4 */
6532   if (opts.addressprint)
6533     {
6534       if (nr_printable_breakpoints > 0)
6535         annotate_field (4);
6536       if (print_address_bits <= 32)
6537         ui_out_table_header (uiout, 10, ui_left, 
6538                              "addr", "Address");                /* 5 */
6539       else
6540         ui_out_table_header (uiout, 18, ui_left, 
6541                              "addr", "Address");                /* 5 */
6542     }
6543   if (nr_printable_breakpoints > 0)
6544     annotate_field (5);
6545   ui_out_table_header (uiout, 40, ui_noalign, "what", "What");  /* 6 */
6546   ui_out_table_body (uiout);
6547   if (nr_printable_breakpoints > 0)
6548     annotate_breakpoints_table ();
6549
6550   ALL_BREAKPOINTS (b)
6551     {
6552       QUIT;
6553       /* If we have a filter, only list the breakpoints it accepts.  */
6554       if (filter && !filter (b))
6555         continue;
6556
6557       /* If we have an "args" string, it is a list of breakpoints to 
6558          accept.  Skip the others.  */
6559
6560       if (args != NULL && *args != '\0')
6561         {
6562           if (allflag)  /* maintenance info breakpoint */
6563             {
6564               if (parse_and_eval_long (args) != b->number)
6565                 continue;
6566             }
6567           else          /* all others */
6568             {
6569               if (!number_is_in_list (args, b->number))
6570                 continue;
6571             }
6572         }
6573       /* We only print out user settable breakpoints unless the
6574          allflag is set.  */
6575       if (allflag || user_breakpoint_p (b))
6576         print_one_breakpoint (b, &last_loc, allflag);
6577     }
6578
6579   do_cleanups (bkpttbl_chain);
6580
6581   if (nr_printable_breakpoints == 0)
6582     {
6583       /* If there's a filter, let the caller decide how to report
6584          empty list.  */
6585       if (!filter)
6586         {
6587           if (args == NULL || *args == '\0')
6588             ui_out_message (uiout, 0, "No breakpoints or watchpoints.\n");
6589           else
6590             ui_out_message (uiout, 0, 
6591                             "No breakpoint or watchpoint matching '%s'.\n",
6592                             args);
6593         }
6594     }
6595   else
6596     {
6597       if (last_loc && !server_command)
6598         set_next_address (last_loc->gdbarch, last_loc->address);
6599     }
6600
6601   /* FIXME?  Should this be moved up so that it is only called when
6602      there have been breakpoints? */
6603   annotate_breakpoints_table_end ();
6604
6605   return nr_printable_breakpoints;
6606 }
6607
6608 /* Display the value of default-collect in a way that is generally
6609    compatible with the breakpoint list.  */
6610
6611 static void
6612 default_collect_info (void)
6613 {
6614   struct ui_out *uiout = current_uiout;
6615
6616   /* If it has no value (which is frequently the case), say nothing; a
6617      message like "No default-collect." gets in user's face when it's
6618      not wanted.  */
6619   if (!*default_collect)
6620     return;
6621
6622   /* The following phrase lines up nicely with per-tracepoint collect
6623      actions.  */
6624   ui_out_text (uiout, "default collect ");
6625   ui_out_field_string (uiout, "default-collect", default_collect);
6626   ui_out_text (uiout, " \n");
6627 }
6628   
6629 static void
6630 breakpoints_info (char *args, int from_tty)
6631 {
6632   breakpoint_1 (args, 0, NULL);
6633
6634   default_collect_info ();
6635 }
6636
6637 static void
6638 watchpoints_info (char *args, int from_tty)
6639 {
6640   int num_printed = breakpoint_1 (args, 0, is_watchpoint);
6641   struct ui_out *uiout = current_uiout;
6642
6643   if (num_printed == 0)
6644     {
6645       if (args == NULL || *args == '\0')
6646         ui_out_message (uiout, 0, "No watchpoints.\n");
6647       else
6648         ui_out_message (uiout, 0, "No watchpoint matching '%s'.\n", args);
6649     }
6650 }
6651
6652 static void
6653 maintenance_info_breakpoints (char *args, int from_tty)
6654 {
6655   breakpoint_1 (args, 1, NULL);
6656
6657   default_collect_info ();
6658 }
6659
6660 static int
6661 breakpoint_has_pc (struct breakpoint *b,
6662                    struct program_space *pspace,
6663                    CORE_ADDR pc, struct obj_section *section)
6664 {
6665   struct bp_location *bl = b->loc;
6666
6667   for (; bl; bl = bl->next)
6668     {
6669       if (bl->pspace == pspace
6670           && bl->address == pc
6671           && (!overlay_debugging || bl->section == section))
6672         return 1;         
6673     }
6674   return 0;
6675 }
6676
6677 /* Print a message describing any user-breakpoints set at PC.  This
6678    concerns with logical breakpoints, so we match program spaces, not
6679    address spaces.  */
6680
6681 static void
6682 describe_other_breakpoints (struct gdbarch *gdbarch,
6683                             struct program_space *pspace, CORE_ADDR pc,
6684                             struct obj_section *section, int thread)
6685 {
6686   int others = 0;
6687   struct breakpoint *b;
6688
6689   ALL_BREAKPOINTS (b)
6690     others += (user_breakpoint_p (b)
6691                && breakpoint_has_pc (b, pspace, pc, section));
6692   if (others > 0)
6693     {
6694       if (others == 1)
6695         printf_filtered (_("Note: breakpoint "));
6696       else /* if (others == ???) */
6697         printf_filtered (_("Note: breakpoints "));
6698       ALL_BREAKPOINTS (b)
6699         if (user_breakpoint_p (b) && breakpoint_has_pc (b, pspace, pc, section))
6700           {
6701             others--;
6702             printf_filtered ("%d", b->number);
6703             if (b->thread == -1 && thread != -1)
6704               printf_filtered (" (all threads)");
6705             else if (b->thread != -1)
6706               printf_filtered (" (thread %d)", b->thread);
6707             printf_filtered ("%s%s ",
6708                              ((b->enable_state == bp_disabled
6709                                || b->enable_state == bp_call_disabled)
6710                               ? " (disabled)"
6711                               : b->enable_state == bp_permanent 
6712                               ? " (permanent)"
6713                               : ""),
6714                              (others > 1) ? "," 
6715                              : ((others == 1) ? " and" : ""));
6716           }
6717       printf_filtered (_("also set at pc "));
6718       fputs_filtered (paddress (gdbarch, pc), gdb_stdout);
6719       printf_filtered (".\n");
6720     }
6721 }
6722 \f
6723
6724 /* Return true iff it is meaningful to use the address member of
6725    BPT.  For some breakpoint types, the address member is irrelevant
6726    and it makes no sense to attempt to compare it to other addresses
6727    (or use it for any other purpose either).
6728
6729    More specifically, each of the following breakpoint types will
6730    always have a zero valued address and we don't want to mark
6731    breakpoints of any of these types to be a duplicate of an actual
6732    breakpoint at address zero:
6733
6734       bp_watchpoint
6735       bp_catchpoint
6736
6737 */
6738
6739 static int
6740 breakpoint_address_is_meaningful (struct breakpoint *bpt)
6741 {
6742   enum bptype type = bpt->type;
6743
6744   return (type != bp_watchpoint && type != bp_catchpoint);
6745 }
6746
6747 /* Assuming LOC1 and LOC2's owners are hardware watchpoints, returns
6748    true if LOC1 and LOC2 represent the same watchpoint location.  */
6749
6750 static int
6751 watchpoint_locations_match (struct bp_location *loc1, 
6752                             struct bp_location *loc2)
6753 {
6754   struct watchpoint *w1 = (struct watchpoint *) loc1->owner;
6755   struct watchpoint *w2 = (struct watchpoint *) loc2->owner;
6756
6757   /* Both of them must exist.  */
6758   gdb_assert (w1 != NULL);
6759   gdb_assert (w2 != NULL);
6760
6761   /* If the target can evaluate the condition expression in hardware,
6762      then we we need to insert both watchpoints even if they are at
6763      the same place.  Otherwise the watchpoint will only trigger when
6764      the condition of whichever watchpoint was inserted evaluates to
6765      true, not giving a chance for GDB to check the condition of the
6766      other watchpoint.  */
6767   if ((w1->cond_exp
6768        && target_can_accel_watchpoint_condition (loc1->address, 
6769                                                  loc1->length,
6770                                                  loc1->watchpoint_type,
6771                                                  w1->cond_exp))
6772       || (w2->cond_exp
6773           && target_can_accel_watchpoint_condition (loc2->address, 
6774                                                     loc2->length,
6775                                                     loc2->watchpoint_type,
6776                                                     w2->cond_exp)))
6777     return 0;
6778
6779   /* Note that this checks the owner's type, not the location's.  In
6780      case the target does not support read watchpoints, but does
6781      support access watchpoints, we'll have bp_read_watchpoint
6782      watchpoints with hw_access locations.  Those should be considered
6783      duplicates of hw_read locations.  The hw_read locations will
6784      become hw_access locations later.  */
6785   return (loc1->owner->type == loc2->owner->type
6786           && loc1->pspace->aspace == loc2->pspace->aspace
6787           && loc1->address == loc2->address
6788           && loc1->length == loc2->length);
6789 }
6790
6791 /* Returns true if {ASPACE1,ADDR1} and {ASPACE2,ADDR2} represent the
6792    same breakpoint location.  In most targets, this can only be true
6793    if ASPACE1 matches ASPACE2.  On targets that have global
6794    breakpoints, the address space doesn't really matter.  */
6795
6796 static int
6797 breakpoint_address_match (struct address_space *aspace1, CORE_ADDR addr1,
6798                           struct address_space *aspace2, CORE_ADDR addr2)
6799 {
6800   return ((gdbarch_has_global_breakpoints (target_gdbarch ())
6801            || aspace1 == aspace2)
6802           && addr1 == addr2);
6803 }
6804
6805 /* Returns true if {ASPACE2,ADDR2} falls within the range determined by
6806    {ASPACE1,ADDR1,LEN1}.  In most targets, this can only be true if ASPACE1
6807    matches ASPACE2.  On targets that have global breakpoints, the address
6808    space doesn't really matter.  */
6809
6810 static int
6811 breakpoint_address_match_range (struct address_space *aspace1, CORE_ADDR addr1,
6812                                 int len1, struct address_space *aspace2,
6813                                 CORE_ADDR addr2)
6814 {
6815   return ((gdbarch_has_global_breakpoints (target_gdbarch ())
6816            || aspace1 == aspace2)
6817           && addr2 >= addr1 && addr2 < addr1 + len1);
6818 }
6819
6820 /* Returns true if {ASPACE,ADDR} matches the breakpoint BL.  BL may be
6821    a ranged breakpoint.  In most targets, a match happens only if ASPACE
6822    matches the breakpoint's address space.  On targets that have global
6823    breakpoints, the address space doesn't really matter.  */
6824
6825 static int
6826 breakpoint_location_address_match (struct bp_location *bl,
6827                                    struct address_space *aspace,
6828                                    CORE_ADDR addr)
6829 {
6830   return (breakpoint_address_match (bl->pspace->aspace, bl->address,
6831                                     aspace, addr)
6832           || (bl->length
6833               && breakpoint_address_match_range (bl->pspace->aspace,
6834                                                  bl->address, bl->length,
6835                                                  aspace, addr)));
6836 }
6837
6838 /* If LOC1 and LOC2's owners are not tracepoints, returns false directly.
6839    Then, if LOC1 and LOC2 represent the same tracepoint location, returns
6840    true, otherwise returns false.  */
6841
6842 static int
6843 tracepoint_locations_match (struct bp_location *loc1,
6844                             struct bp_location *loc2)
6845 {
6846   if (is_tracepoint (loc1->owner) && is_tracepoint (loc2->owner))
6847     /* Since tracepoint locations are never duplicated with others', tracepoint
6848        locations at the same address of different tracepoints are regarded as
6849        different locations.  */
6850     return (loc1->address == loc2->address && loc1->owner == loc2->owner);
6851   else
6852     return 0;
6853 }
6854
6855 /* Assuming LOC1 and LOC2's types' have meaningful target addresses
6856    (breakpoint_address_is_meaningful), returns true if LOC1 and LOC2
6857    represent the same location.  */
6858
6859 static int
6860 breakpoint_locations_match (struct bp_location *loc1, 
6861                             struct bp_location *loc2)
6862 {
6863   int hw_point1, hw_point2;
6864
6865   /* Both of them must not be in moribund_locations.  */
6866   gdb_assert (loc1->owner != NULL);
6867   gdb_assert (loc2->owner != NULL);
6868
6869   hw_point1 = is_hardware_watchpoint (loc1->owner);
6870   hw_point2 = is_hardware_watchpoint (loc2->owner);
6871
6872   if (hw_point1 != hw_point2)
6873     return 0;
6874   else if (hw_point1)
6875     return watchpoint_locations_match (loc1, loc2);
6876   else if (is_tracepoint (loc1->owner) || is_tracepoint (loc2->owner))
6877     return tracepoint_locations_match (loc1, loc2);
6878   else
6879     /* We compare bp_location.length in order to cover ranged breakpoints.  */
6880     return (breakpoint_address_match (loc1->pspace->aspace, loc1->address,
6881                                      loc2->pspace->aspace, loc2->address)
6882             && loc1->length == loc2->length);
6883 }
6884
6885 static void
6886 breakpoint_adjustment_warning (CORE_ADDR from_addr, CORE_ADDR to_addr,
6887                                int bnum, int have_bnum)
6888 {
6889   /* The longest string possibly returned by hex_string_custom
6890      is 50 chars.  These must be at least that big for safety.  */
6891   char astr1[64];
6892   char astr2[64];
6893
6894   strcpy (astr1, hex_string_custom ((unsigned long) from_addr, 8));
6895   strcpy (astr2, hex_string_custom ((unsigned long) to_addr, 8));
6896   if (have_bnum)
6897     warning (_("Breakpoint %d address previously adjusted from %s to %s."),
6898              bnum, astr1, astr2);
6899   else
6900     warning (_("Breakpoint address adjusted from %s to %s."), astr1, astr2);
6901 }
6902
6903 /* Adjust a breakpoint's address to account for architectural
6904    constraints on breakpoint placement.  Return the adjusted address.
6905    Note: Very few targets require this kind of adjustment.  For most
6906    targets, this function is simply the identity function.  */
6907
6908 static CORE_ADDR
6909 adjust_breakpoint_address (struct gdbarch *gdbarch,
6910                            CORE_ADDR bpaddr, enum bptype bptype)
6911 {
6912   if (!gdbarch_adjust_breakpoint_address_p (gdbarch))
6913     {
6914       /* Very few targets need any kind of breakpoint adjustment.  */
6915       return bpaddr;
6916     }
6917   else if (bptype == bp_watchpoint
6918            || bptype == bp_hardware_watchpoint
6919            || bptype == bp_read_watchpoint
6920            || bptype == bp_access_watchpoint
6921            || bptype == bp_catchpoint)
6922     {
6923       /* Watchpoints and the various bp_catch_* eventpoints should not
6924          have their addresses modified.  */
6925       return bpaddr;
6926     }
6927   else
6928     {
6929       CORE_ADDR adjusted_bpaddr;
6930
6931       /* Some targets have architectural constraints on the placement
6932          of breakpoint instructions.  Obtain the adjusted address.  */
6933       adjusted_bpaddr = gdbarch_adjust_breakpoint_address (gdbarch, bpaddr);
6934
6935       /* An adjusted breakpoint address can significantly alter
6936          a user's expectations.  Print a warning if an adjustment
6937          is required.  */
6938       if (adjusted_bpaddr != bpaddr)
6939         breakpoint_adjustment_warning (bpaddr, adjusted_bpaddr, 0, 0);
6940
6941       return adjusted_bpaddr;
6942     }
6943 }
6944
6945 void
6946 init_bp_location (struct bp_location *loc, const struct bp_location_ops *ops,
6947                   struct breakpoint *owner)
6948 {
6949   memset (loc, 0, sizeof (*loc));
6950
6951   gdb_assert (ops != NULL);
6952
6953   loc->ops = ops;
6954   loc->owner = owner;
6955   loc->cond = NULL;
6956   loc->cond_bytecode = NULL;
6957   loc->shlib_disabled = 0;
6958   loc->enabled = 1;
6959
6960   switch (owner->type)
6961     {
6962     case bp_breakpoint:
6963     case bp_until:
6964     case bp_finish:
6965     case bp_longjmp:
6966     case bp_longjmp_resume:
6967     case bp_longjmp_call_dummy:
6968     case bp_exception:
6969     case bp_exception_resume:
6970     case bp_step_resume:
6971     case bp_hp_step_resume:
6972     case bp_watchpoint_scope:
6973     case bp_call_dummy:
6974     case bp_std_terminate:
6975     case bp_shlib_event:
6976     case bp_thread_event:
6977     case bp_overlay_event:
6978     case bp_jit_event:
6979     case bp_longjmp_master:
6980     case bp_std_terminate_master:
6981     case bp_exception_master:
6982     case bp_gnu_ifunc_resolver:
6983     case bp_gnu_ifunc_resolver_return:
6984     case bp_dprintf:
6985       loc->loc_type = bp_loc_software_breakpoint;
6986       mark_breakpoint_location_modified (loc);
6987       break;
6988     case bp_hardware_breakpoint:
6989       loc->loc_type = bp_loc_hardware_breakpoint;
6990       mark_breakpoint_location_modified (loc);
6991       break;
6992     case bp_hardware_watchpoint:
6993     case bp_read_watchpoint:
6994     case bp_access_watchpoint:
6995       loc->loc_type = bp_loc_hardware_watchpoint;
6996       break;
6997     case bp_watchpoint:
6998     case bp_catchpoint:
6999     case bp_tracepoint:
7000     case bp_fast_tracepoint:
7001     case bp_static_tracepoint:
7002       loc->loc_type = bp_loc_other;
7003       break;
7004     default:
7005       internal_error (__FILE__, __LINE__, _("unknown breakpoint type"));
7006     }
7007
7008   loc->refc = 1;
7009 }
7010
7011 /* Allocate a struct bp_location.  */
7012
7013 static struct bp_location *
7014 allocate_bp_location (struct breakpoint *bpt)
7015 {
7016   return bpt->ops->allocate_location (bpt);
7017 }
7018
7019 static void
7020 free_bp_location (struct bp_location *loc)
7021 {
7022   loc->ops->dtor (loc);
7023   xfree (loc);
7024 }
7025
7026 /* Increment reference count.  */
7027
7028 static void
7029 incref_bp_location (struct bp_location *bl)
7030 {
7031   ++bl->refc;
7032 }
7033
7034 /* Decrement reference count.  If the reference count reaches 0,
7035    destroy the bp_location.  Sets *BLP to NULL.  */
7036
7037 static void
7038 decref_bp_location (struct bp_location **blp)
7039 {
7040   gdb_assert ((*blp)->refc > 0);
7041
7042   if (--(*blp)->refc == 0)
7043     free_bp_location (*blp);
7044   *blp = NULL;
7045 }
7046
7047 /* Add breakpoint B at the end of the global breakpoint chain.  */
7048
7049 static void
7050 add_to_breakpoint_chain (struct breakpoint *b)
7051 {
7052   struct breakpoint *b1;
7053
7054   /* Add this breakpoint to the end of the chain so that a list of
7055      breakpoints will come out in order of increasing numbers.  */
7056
7057   b1 = breakpoint_chain;
7058   if (b1 == 0)
7059     breakpoint_chain = b;
7060   else
7061     {
7062       while (b1->next)
7063         b1 = b1->next;
7064       b1->next = b;
7065     }
7066 }
7067
7068 /* Initializes breakpoint B with type BPTYPE and no locations yet.  */
7069
7070 static void
7071 init_raw_breakpoint_without_location (struct breakpoint *b,
7072                                       struct gdbarch *gdbarch,
7073                                       enum bptype bptype,
7074                                       const struct breakpoint_ops *ops)
7075 {
7076   memset (b, 0, sizeof (*b));
7077
7078   gdb_assert (ops != NULL);
7079
7080   b->ops = ops;
7081   b->type = bptype;
7082   b->gdbarch = gdbarch;
7083   b->language = current_language->la_language;
7084   b->input_radix = input_radix;
7085   b->thread = -1;
7086   b->enable_state = bp_enabled;
7087   b->next = 0;
7088   b->silent = 0;
7089   b->ignore_count = 0;
7090   b->commands = NULL;
7091   b->frame_id = null_frame_id;
7092   b->condition_not_parsed = 0;
7093   b->py_bp_object = NULL;
7094   b->related_breakpoint = b;
7095 }
7096
7097 /* Helper to set_raw_breakpoint below.  Creates a breakpoint
7098    that has type BPTYPE and has no locations as yet.  */
7099
7100 static struct breakpoint *
7101 set_raw_breakpoint_without_location (struct gdbarch *gdbarch,
7102                                      enum bptype bptype,
7103                                      const struct breakpoint_ops *ops)
7104 {
7105   struct breakpoint *b = XNEW (struct breakpoint);
7106
7107   init_raw_breakpoint_without_location (b, gdbarch, bptype, ops);
7108   add_to_breakpoint_chain (b);
7109   return b;
7110 }
7111
7112 /* Initialize loc->function_name.  EXPLICIT_LOC says no indirect function
7113    resolutions should be made as the user specified the location explicitly
7114    enough.  */
7115
7116 static void
7117 set_breakpoint_location_function (struct bp_location *loc, int explicit_loc)
7118 {
7119   gdb_assert (loc->owner != NULL);
7120
7121   if (loc->owner->type == bp_breakpoint
7122       || loc->owner->type == bp_hardware_breakpoint
7123       || is_tracepoint (loc->owner))
7124     {
7125       int is_gnu_ifunc;
7126       const char *function_name;
7127       CORE_ADDR func_addr;
7128
7129       find_pc_partial_function_gnu_ifunc (loc->address, &function_name,
7130                                           &func_addr, NULL, &is_gnu_ifunc);
7131
7132       if (is_gnu_ifunc && !explicit_loc)
7133         {
7134           struct breakpoint *b = loc->owner;
7135
7136           gdb_assert (loc->pspace == current_program_space);
7137           if (gnu_ifunc_resolve_name (function_name,
7138                                       &loc->requested_address))
7139             {
7140               /* Recalculate ADDRESS based on new REQUESTED_ADDRESS.  */
7141               loc->address = adjust_breakpoint_address (loc->gdbarch,
7142                                                         loc->requested_address,
7143                                                         b->type);
7144             }
7145           else if (b->type == bp_breakpoint && b->loc == loc
7146                    && loc->next == NULL && b->related_breakpoint == b)
7147             {
7148               /* Create only the whole new breakpoint of this type but do not
7149                  mess more complicated breakpoints with multiple locations.  */
7150               b->type = bp_gnu_ifunc_resolver;
7151               /* Remember the resolver's address for use by the return
7152                  breakpoint.  */
7153               loc->related_address = func_addr;
7154             }
7155         }
7156
7157       if (function_name)
7158         loc->function_name = xstrdup (function_name);
7159     }
7160 }
7161
7162 /* Attempt to determine architecture of location identified by SAL.  */
7163 struct gdbarch *
7164 get_sal_arch (struct symtab_and_line sal)
7165 {
7166   if (sal.section)
7167     return get_objfile_arch (sal.section->objfile);
7168   if (sal.symtab)
7169     return get_objfile_arch (sal.symtab->objfile);
7170
7171   return NULL;
7172 }
7173
7174 /* Low level routine for partially initializing a breakpoint of type
7175    BPTYPE.  The newly created breakpoint's address, section, source
7176    file name, and line number are provided by SAL.
7177
7178    It is expected that the caller will complete the initialization of
7179    the newly created breakpoint struct as well as output any status
7180    information regarding the creation of a new breakpoint.  */
7181
7182 static void
7183 init_raw_breakpoint (struct breakpoint *b, struct gdbarch *gdbarch,
7184                      struct symtab_and_line sal, enum bptype bptype,
7185                      const struct breakpoint_ops *ops)
7186 {
7187   init_raw_breakpoint_without_location (b, gdbarch, bptype, ops);
7188
7189   add_location_to_breakpoint (b, &sal);
7190
7191   if (bptype != bp_catchpoint)
7192     gdb_assert (sal.pspace != NULL);
7193
7194   /* Store the program space that was used to set the breakpoint,
7195      except for ordinary breakpoints, which are independent of the
7196      program space.  */
7197   if (bptype != bp_breakpoint && bptype != bp_hardware_breakpoint)
7198     b->pspace = sal.pspace;
7199 }
7200
7201 /* set_raw_breakpoint is a low level routine for allocating and
7202    partially initializing a breakpoint of type BPTYPE.  The newly
7203    created breakpoint's address, section, source file name, and line
7204    number are provided by SAL.  The newly created and partially
7205    initialized breakpoint is added to the breakpoint chain and
7206    is also returned as the value of this function.
7207
7208    It is expected that the caller will complete the initialization of
7209    the newly created breakpoint struct as well as output any status
7210    information regarding the creation of a new breakpoint.  In
7211    particular, set_raw_breakpoint does NOT set the breakpoint
7212    number!  Care should be taken to not allow an error to occur
7213    prior to completing the initialization of the breakpoint.  If this
7214    should happen, a bogus breakpoint will be left on the chain.  */
7215
7216 struct breakpoint *
7217 set_raw_breakpoint (struct gdbarch *gdbarch,
7218                     struct symtab_and_line sal, enum bptype bptype,
7219                     const struct breakpoint_ops *ops)
7220 {
7221   struct breakpoint *b = XNEW (struct breakpoint);
7222
7223   init_raw_breakpoint (b, gdbarch, sal, bptype, ops);
7224   add_to_breakpoint_chain (b);
7225   return b;
7226 }
7227
7228
7229 /* Note that the breakpoint object B describes a permanent breakpoint
7230    instruction, hard-wired into the inferior's code.  */
7231 void
7232 make_breakpoint_permanent (struct breakpoint *b)
7233 {
7234   struct bp_location *bl;
7235
7236   b->enable_state = bp_permanent;
7237
7238   /* By definition, permanent breakpoints are already present in the
7239      code.  Mark all locations as inserted.  For now,
7240      make_breakpoint_permanent is called in just one place, so it's
7241      hard to say if it's reasonable to have permanent breakpoint with
7242      multiple locations or not, but it's easy to implement.  */
7243   for (bl = b->loc; bl; bl = bl->next)
7244     bl->inserted = 1;
7245 }
7246
7247 /* Call this routine when stepping and nexting to enable a breakpoint
7248    if we do a longjmp() or 'throw' in TP.  FRAME is the frame which
7249    initiated the operation.  */
7250
7251 void
7252 set_longjmp_breakpoint (struct thread_info *tp, struct frame_id frame)
7253 {
7254   struct breakpoint *b, *b_tmp;
7255   int thread = tp->num;
7256
7257   /* To avoid having to rescan all objfile symbols at every step,
7258      we maintain a list of continually-inserted but always disabled
7259      longjmp "master" breakpoints.  Here, we simply create momentary
7260      clones of those and enable them for the requested thread.  */
7261   ALL_BREAKPOINTS_SAFE (b, b_tmp)
7262     if (b->pspace == current_program_space
7263         && (b->type == bp_longjmp_master
7264             || b->type == bp_exception_master))
7265       {
7266         enum bptype type = b->type == bp_longjmp_master ? bp_longjmp : bp_exception;
7267         struct breakpoint *clone;
7268
7269         /* longjmp_breakpoint_ops ensures INITIATING_FRAME is cleared again
7270            after their removal.  */
7271         clone = momentary_breakpoint_from_master (b, type,
7272                                                   &longjmp_breakpoint_ops);
7273         clone->thread = thread;
7274       }
7275
7276   tp->initiating_frame = frame;
7277 }
7278
7279 /* Delete all longjmp breakpoints from THREAD.  */
7280 void
7281 delete_longjmp_breakpoint (int thread)
7282 {
7283   struct breakpoint *b, *b_tmp;
7284
7285   ALL_BREAKPOINTS_SAFE (b, b_tmp)
7286     if (b->type == bp_longjmp || b->type == bp_exception)
7287       {
7288         if (b->thread == thread)
7289           delete_breakpoint (b);
7290       }
7291 }
7292
7293 void
7294 delete_longjmp_breakpoint_at_next_stop (int thread)
7295 {
7296   struct breakpoint *b, *b_tmp;
7297
7298   ALL_BREAKPOINTS_SAFE (b, b_tmp)
7299     if (b->type == bp_longjmp || b->type == bp_exception)
7300       {
7301         if (b->thread == thread)
7302           b->disposition = disp_del_at_next_stop;
7303       }
7304 }
7305
7306 /* Place breakpoints of type bp_longjmp_call_dummy to catch longjmp for
7307    INFERIOR_PTID thread.  Chain them all by RELATED_BREAKPOINT and return
7308    pointer to any of them.  Return NULL if this system cannot place longjmp
7309    breakpoints.  */
7310
7311 struct breakpoint *
7312 set_longjmp_breakpoint_for_call_dummy (void)
7313 {
7314   struct breakpoint *b, *retval = NULL;
7315
7316   ALL_BREAKPOINTS (b)
7317     if (b->pspace == current_program_space && b->type == bp_longjmp_master)
7318       {
7319         struct breakpoint *new_b;
7320
7321         new_b = momentary_breakpoint_from_master (b, bp_longjmp_call_dummy,
7322                                                   &momentary_breakpoint_ops);
7323         new_b->thread = pid_to_thread_id (inferior_ptid);
7324
7325         /* Link NEW_B into the chain of RETVAL breakpoints.  */
7326
7327         gdb_assert (new_b->related_breakpoint == new_b);
7328         if (retval == NULL)
7329           retval = new_b;
7330         new_b->related_breakpoint = retval;
7331         while (retval->related_breakpoint != new_b->related_breakpoint)
7332           retval = retval->related_breakpoint;
7333         retval->related_breakpoint = new_b;
7334       }
7335
7336   return retval;
7337 }
7338
7339 /* Verify all existing dummy frames and their associated breakpoints for
7340    THREAD.  Remove those which can no longer be found in the current frame
7341    stack.
7342
7343    You should call this function only at places where it is safe to currently
7344    unwind the whole stack.  Failed stack unwind would discard live dummy
7345    frames.  */
7346
7347 void
7348 check_longjmp_breakpoint_for_call_dummy (int thread)
7349 {
7350   struct breakpoint *b, *b_tmp;
7351
7352   ALL_BREAKPOINTS_SAFE (b, b_tmp)
7353     if (b->type == bp_longjmp_call_dummy && b->thread == thread)
7354       {
7355         struct breakpoint *dummy_b = b->related_breakpoint;
7356
7357         while (dummy_b != b && dummy_b->type != bp_call_dummy)
7358           dummy_b = dummy_b->related_breakpoint;
7359         if (dummy_b->type != bp_call_dummy
7360             || frame_find_by_id (dummy_b->frame_id) != NULL)
7361           continue;
7362         
7363         dummy_frame_discard (dummy_b->frame_id);
7364
7365         while (b->related_breakpoint != b)
7366           {
7367             if (b_tmp == b->related_breakpoint)
7368               b_tmp = b->related_breakpoint->next;
7369             delete_breakpoint (b->related_breakpoint);
7370           }
7371         delete_breakpoint (b);
7372       }
7373 }
7374
7375 void
7376 enable_overlay_breakpoints (void)
7377 {
7378   struct breakpoint *b;
7379
7380   ALL_BREAKPOINTS (b)
7381     if (b->type == bp_overlay_event)
7382     {
7383       b->enable_state = bp_enabled;
7384       update_global_location_list (1);
7385       overlay_events_enabled = 1;
7386     }
7387 }
7388
7389 void
7390 disable_overlay_breakpoints (void)
7391 {
7392   struct breakpoint *b;
7393
7394   ALL_BREAKPOINTS (b)
7395     if (b->type == bp_overlay_event)
7396     {
7397       b->enable_state = bp_disabled;
7398       update_global_location_list (0);
7399       overlay_events_enabled = 0;
7400     }
7401 }
7402
7403 /* Set an active std::terminate breakpoint for each std::terminate
7404    master breakpoint.  */
7405 void
7406 set_std_terminate_breakpoint (void)
7407 {
7408   struct breakpoint *b, *b_tmp;
7409
7410   ALL_BREAKPOINTS_SAFE (b, b_tmp)
7411     if (b->pspace == current_program_space
7412         && b->type == bp_std_terminate_master)
7413       {
7414         momentary_breakpoint_from_master (b, bp_std_terminate,
7415                                           &momentary_breakpoint_ops);
7416       }
7417 }
7418
7419 /* Delete all the std::terminate breakpoints.  */
7420 void
7421 delete_std_terminate_breakpoint (void)
7422 {
7423   struct breakpoint *b, *b_tmp;
7424
7425   ALL_BREAKPOINTS_SAFE (b, b_tmp)
7426     if (b->type == bp_std_terminate)
7427       delete_breakpoint (b);
7428 }
7429
7430 struct breakpoint *
7431 create_thread_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7432 {
7433   struct breakpoint *b;
7434
7435   b = create_internal_breakpoint (gdbarch, address, bp_thread_event,
7436                                   &internal_breakpoint_ops);
7437
7438   b->enable_state = bp_enabled;
7439   /* addr_string has to be used or breakpoint_re_set will delete me.  */
7440   b->addr_string
7441     = xstrprintf ("*%s", paddress (b->loc->gdbarch, b->loc->address));
7442
7443   update_global_location_list_nothrow (1);
7444
7445   return b;
7446 }
7447
7448 void
7449 remove_thread_event_breakpoints (void)
7450 {
7451   struct breakpoint *b, *b_tmp;
7452
7453   ALL_BREAKPOINTS_SAFE (b, b_tmp)
7454     if (b->type == bp_thread_event
7455         && b->loc->pspace == current_program_space)
7456       delete_breakpoint (b);
7457 }
7458
7459 struct lang_and_radix
7460   {
7461     enum language lang;
7462     int radix;
7463   };
7464
7465 /* Create a breakpoint for JIT code registration and unregistration.  */
7466
7467 struct breakpoint *
7468 create_jit_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7469 {
7470   struct breakpoint *b;
7471
7472   b = create_internal_breakpoint (gdbarch, address, bp_jit_event,
7473                                   &internal_breakpoint_ops);
7474   update_global_location_list_nothrow (1);
7475   return b;
7476 }
7477
7478 /* Remove JIT code registration and unregistration breakpoint(s).  */
7479
7480 void
7481 remove_jit_event_breakpoints (void)
7482 {
7483   struct breakpoint *b, *b_tmp;
7484
7485   ALL_BREAKPOINTS_SAFE (b, b_tmp)
7486     if (b->type == bp_jit_event
7487         && b->loc->pspace == current_program_space)
7488       delete_breakpoint (b);
7489 }
7490
7491 void
7492 remove_solib_event_breakpoints (void)
7493 {
7494   struct breakpoint *b, *b_tmp;
7495
7496   ALL_BREAKPOINTS_SAFE (b, b_tmp)
7497     if (b->type == bp_shlib_event
7498         && b->loc->pspace == current_program_space)
7499       delete_breakpoint (b);
7500 }
7501
7502 struct breakpoint *
7503 create_solib_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7504 {
7505   struct breakpoint *b;
7506
7507   b = create_internal_breakpoint (gdbarch, address, bp_shlib_event,
7508                                   &internal_breakpoint_ops);
7509   update_global_location_list_nothrow (1);
7510   return b;
7511 }
7512
7513 /* Disable any breakpoints that are on code in shared libraries.  Only
7514    apply to enabled breakpoints, disabled ones can just stay disabled.  */
7515
7516 void
7517 disable_breakpoints_in_shlibs (void)
7518 {
7519   struct bp_location *loc, **locp_tmp;
7520
7521   ALL_BP_LOCATIONS (loc, locp_tmp)
7522   {
7523     /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL.  */
7524     struct breakpoint *b = loc->owner;
7525
7526     /* We apply the check to all breakpoints, including disabled for
7527        those with loc->duplicate set.  This is so that when breakpoint
7528        becomes enabled, or the duplicate is removed, gdb will try to
7529        insert all breakpoints.  If we don't set shlib_disabled here,
7530        we'll try to insert those breakpoints and fail.  */
7531     if (((b->type == bp_breakpoint)
7532          || (b->type == bp_jit_event)
7533          || (b->type == bp_hardware_breakpoint)
7534          || (is_tracepoint (b)))
7535         && loc->pspace == current_program_space
7536         && !loc->shlib_disabled
7537         && solib_name_from_address (loc->pspace, loc->address)
7538         )
7539       {
7540         loc->shlib_disabled = 1;
7541       }
7542   }
7543 }
7544
7545 /* Disable any breakpoints and tracepoints that are in SOLIB upon
7546    notification of unloaded_shlib.  Only apply to enabled breakpoints,
7547    disabled ones can just stay disabled.  */
7548
7549 static void
7550 disable_breakpoints_in_unloaded_shlib (struct so_list *solib)
7551 {
7552   struct bp_location *loc, **locp_tmp;
7553   int disabled_shlib_breaks = 0;
7554
7555   /* SunOS a.out shared libraries are always mapped, so do not
7556      disable breakpoints; they will only be reported as unloaded
7557      through clear_solib when GDB discards its shared library
7558      list.  See clear_solib for more information.  */
7559   if (exec_bfd != NULL
7560       && bfd_get_flavour (exec_bfd) == bfd_target_aout_flavour)
7561     return;
7562
7563   ALL_BP_LOCATIONS (loc, locp_tmp)
7564   {
7565     /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL.  */
7566     struct breakpoint *b = loc->owner;
7567
7568     if (solib->pspace == loc->pspace
7569         && !loc->shlib_disabled
7570         && (((b->type == bp_breakpoint
7571               || b->type == bp_jit_event
7572               || b->type == bp_hardware_breakpoint)
7573              && (loc->loc_type == bp_loc_hardware_breakpoint
7574                  || loc->loc_type == bp_loc_software_breakpoint))
7575             || is_tracepoint (b))
7576         && solib_contains_address_p (solib, loc->address))
7577       {
7578         loc->shlib_disabled = 1;
7579         /* At this point, we cannot rely on remove_breakpoint
7580            succeeding so we must mark the breakpoint as not inserted
7581            to prevent future errors occurring in remove_breakpoints.  */
7582         loc->inserted = 0;
7583
7584         /* This may cause duplicate notifications for the same breakpoint.  */
7585         observer_notify_breakpoint_modified (b);
7586
7587         if (!disabled_shlib_breaks)
7588           {
7589             target_terminal_ours_for_output ();
7590             warning (_("Temporarily disabling breakpoints "
7591                        "for unloaded shared library \"%s\""),
7592                      solib->so_name);
7593           }
7594         disabled_shlib_breaks = 1;
7595       }
7596   }
7597 }
7598
7599 /* Disable any breakpoints and tracepoints in OBJFILE upon
7600    notification of free_objfile.  Only apply to enabled breakpoints,
7601    disabled ones can just stay disabled.  */
7602
7603 static void
7604 disable_breakpoints_in_freed_objfile (struct objfile *objfile)
7605 {
7606   struct breakpoint *b;
7607
7608   if (objfile == NULL)
7609     return;
7610
7611   /* If the file is a shared library not loaded by the user then
7612      solib_unloaded was notified and disable_breakpoints_in_unloaded_shlib
7613      was called.  In that case there is no need to take action again.  */
7614   if ((objfile->flags & OBJF_SHARED) && !(objfile->flags & OBJF_USERLOADED))
7615     return;
7616
7617   ALL_BREAKPOINTS (b)
7618     {
7619       struct bp_location *loc;
7620       int bp_modified = 0;
7621
7622       if (!is_breakpoint (b) && !is_tracepoint (b))
7623         continue;
7624
7625       for (loc = b->loc; loc != NULL; loc = loc->next)
7626         {
7627           CORE_ADDR loc_addr = loc->address;
7628
7629           if (loc->loc_type != bp_loc_hardware_breakpoint
7630               && loc->loc_type != bp_loc_software_breakpoint)
7631             continue;
7632
7633           if (loc->shlib_disabled != 0)
7634             continue;
7635
7636           if (objfile->pspace != loc->pspace)
7637             continue;
7638
7639           if (loc->loc_type != bp_loc_hardware_breakpoint
7640               && loc->loc_type != bp_loc_software_breakpoint)
7641             continue;
7642
7643           if (is_addr_in_objfile (loc_addr, objfile))
7644             {
7645               loc->shlib_disabled = 1;
7646               loc->inserted = 0;
7647
7648               mark_breakpoint_location_modified (loc);
7649
7650               bp_modified = 1;
7651             }
7652         }
7653
7654       if (bp_modified)
7655         observer_notify_breakpoint_modified (b);
7656     }
7657 }
7658
7659 /* FORK & VFORK catchpoints.  */
7660
7661 /* An instance of this type is used to represent a fork or vfork
7662    catchpoint.  It includes a "struct breakpoint" as a kind of base
7663    class; users downcast to "struct breakpoint *" when needed.  A
7664    breakpoint is really of this type iff its ops pointer points to
7665    CATCH_FORK_BREAKPOINT_OPS.  */
7666
7667 struct fork_catchpoint
7668 {
7669   /* The base class.  */
7670   struct breakpoint base;
7671
7672   /* Process id of a child process whose forking triggered this
7673      catchpoint.  This field is only valid immediately after this
7674      catchpoint has triggered.  */
7675   ptid_t forked_inferior_pid;
7676 };
7677
7678 /* Implement the "insert" breakpoint_ops method for fork
7679    catchpoints.  */
7680
7681 static int
7682 insert_catch_fork (struct bp_location *bl)
7683 {
7684   return target_insert_fork_catchpoint (ptid_get_pid (inferior_ptid));
7685 }
7686
7687 /* Implement the "remove" breakpoint_ops method for fork
7688    catchpoints.  */
7689
7690 static int
7691 remove_catch_fork (struct bp_location *bl)
7692 {
7693   return target_remove_fork_catchpoint (ptid_get_pid (inferior_ptid));
7694 }
7695
7696 /* Implement the "breakpoint_hit" breakpoint_ops method for fork
7697    catchpoints.  */
7698
7699 static int
7700 breakpoint_hit_catch_fork (const struct bp_location *bl,
7701                            struct address_space *aspace, CORE_ADDR bp_addr,
7702                            const struct target_waitstatus *ws)
7703 {
7704   struct fork_catchpoint *c = (struct fork_catchpoint *) bl->owner;
7705
7706   if (ws->kind != TARGET_WAITKIND_FORKED)
7707     return 0;
7708
7709   c->forked_inferior_pid = ws->value.related_pid;
7710   return 1;
7711 }
7712
7713 /* Implement the "print_it" breakpoint_ops method for fork
7714    catchpoints.  */
7715
7716 static enum print_stop_action
7717 print_it_catch_fork (bpstat bs)
7718 {
7719   struct ui_out *uiout = current_uiout;
7720   struct breakpoint *b = bs->breakpoint_at;
7721   struct fork_catchpoint *c = (struct fork_catchpoint *) bs->breakpoint_at;
7722
7723   annotate_catchpoint (b->number);
7724   if (b->disposition == disp_del)
7725     ui_out_text (uiout, "\nTemporary catchpoint ");
7726   else
7727     ui_out_text (uiout, "\nCatchpoint ");
7728   if (ui_out_is_mi_like_p (uiout))
7729     {
7730       ui_out_field_string (uiout, "reason",
7731                            async_reason_lookup (EXEC_ASYNC_FORK));
7732       ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
7733     }
7734   ui_out_field_int (uiout, "bkptno", b->number);
7735   ui_out_text (uiout, " (forked process ");
7736   ui_out_field_int (uiout, "newpid", ptid_get_pid (c->forked_inferior_pid));
7737   ui_out_text (uiout, "), ");
7738   return PRINT_SRC_AND_LOC;
7739 }
7740
7741 /* Implement the "print_one" breakpoint_ops method for fork
7742    catchpoints.  */
7743
7744 static void
7745 print_one_catch_fork (struct breakpoint *b, struct bp_location **last_loc)
7746 {
7747   struct fork_catchpoint *c = (struct fork_catchpoint *) b;
7748   struct value_print_options opts;
7749   struct ui_out *uiout = current_uiout;
7750
7751   get_user_print_options (&opts);
7752
7753   /* Field 4, the address, is omitted (which makes the columns not
7754      line up too nicely with the headers, but the effect is relatively
7755      readable).  */
7756   if (opts.addressprint)
7757     ui_out_field_skip (uiout, "addr");
7758   annotate_field (5);
7759   ui_out_text (uiout, "fork");
7760   if (!ptid_equal (c->forked_inferior_pid, null_ptid))
7761     {
7762       ui_out_text (uiout, ", process ");
7763       ui_out_field_int (uiout, "what",
7764                         ptid_get_pid (c->forked_inferior_pid));
7765       ui_out_spaces (uiout, 1);
7766     }
7767
7768   if (ui_out_is_mi_like_p (uiout))
7769     ui_out_field_string (uiout, "catch-type", "fork");
7770 }
7771
7772 /* Implement the "print_mention" breakpoint_ops method for fork
7773    catchpoints.  */
7774
7775 static void
7776 print_mention_catch_fork (struct breakpoint *b)
7777 {
7778   printf_filtered (_("Catchpoint %d (fork)"), b->number);
7779 }
7780
7781 /* Implement the "print_recreate" breakpoint_ops method for fork
7782    catchpoints.  */
7783
7784 static void
7785 print_recreate_catch_fork (struct breakpoint *b, struct ui_file *fp)
7786 {
7787   fprintf_unfiltered (fp, "catch fork");
7788   print_recreate_thread (b, fp);
7789 }
7790
7791 /* The breakpoint_ops structure to be used in fork catchpoints.  */
7792
7793 static struct breakpoint_ops catch_fork_breakpoint_ops;
7794
7795 /* Implement the "insert" breakpoint_ops method for vfork
7796    catchpoints.  */
7797
7798 static int
7799 insert_catch_vfork (struct bp_location *bl)
7800 {
7801   return target_insert_vfork_catchpoint (ptid_get_pid (inferior_ptid));
7802 }
7803
7804 /* Implement the "remove" breakpoint_ops method for vfork
7805    catchpoints.  */
7806
7807 static int
7808 remove_catch_vfork (struct bp_location *bl)
7809 {
7810   return target_remove_vfork_catchpoint (ptid_get_pid (inferior_ptid));
7811 }
7812
7813 /* Implement the "breakpoint_hit" breakpoint_ops method for vfork
7814    catchpoints.  */
7815
7816 static int
7817 breakpoint_hit_catch_vfork (const struct bp_location *bl,
7818                             struct address_space *aspace, CORE_ADDR bp_addr,
7819                             const struct target_waitstatus *ws)
7820 {
7821   struct fork_catchpoint *c = (struct fork_catchpoint *) bl->owner;
7822
7823   if (ws->kind != TARGET_WAITKIND_VFORKED)
7824     return 0;
7825
7826   c->forked_inferior_pid = ws->value.related_pid;
7827   return 1;
7828 }
7829
7830 /* Implement the "print_it" breakpoint_ops method for vfork
7831    catchpoints.  */
7832
7833 static enum print_stop_action
7834 print_it_catch_vfork (bpstat bs)
7835 {
7836   struct ui_out *uiout = current_uiout;
7837   struct breakpoint *b = bs->breakpoint_at;
7838   struct fork_catchpoint *c = (struct fork_catchpoint *) b;
7839
7840   annotate_catchpoint (b->number);
7841   if (b->disposition == disp_del)
7842     ui_out_text (uiout, "\nTemporary catchpoint ");
7843   else
7844     ui_out_text (uiout, "\nCatchpoint ");
7845   if (ui_out_is_mi_like_p (uiout))
7846     {
7847       ui_out_field_string (uiout, "reason",
7848                            async_reason_lookup (EXEC_ASYNC_VFORK));
7849       ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
7850     }
7851   ui_out_field_int (uiout, "bkptno", b->number);
7852   ui_out_text (uiout, " (vforked process ");
7853   ui_out_field_int (uiout, "newpid", ptid_get_pid (c->forked_inferior_pid));
7854   ui_out_text (uiout, "), ");
7855   return PRINT_SRC_AND_LOC;
7856 }
7857
7858 /* Implement the "print_one" breakpoint_ops method for vfork
7859    catchpoints.  */
7860
7861 static void
7862 print_one_catch_vfork (struct breakpoint *b, struct bp_location **last_loc)
7863 {
7864   struct fork_catchpoint *c = (struct fork_catchpoint *) b;
7865   struct value_print_options opts;
7866   struct ui_out *uiout = current_uiout;
7867
7868   get_user_print_options (&opts);
7869   /* Field 4, the address, is omitted (which makes the columns not
7870      line up too nicely with the headers, but the effect is relatively
7871      readable).  */
7872   if (opts.addressprint)
7873     ui_out_field_skip (uiout, "addr");
7874   annotate_field (5);
7875   ui_out_text (uiout, "vfork");
7876   if (!ptid_equal (c->forked_inferior_pid, null_ptid))
7877     {
7878       ui_out_text (uiout, ", process ");
7879       ui_out_field_int (uiout, "what",
7880                         ptid_get_pid (c->forked_inferior_pid));
7881       ui_out_spaces (uiout, 1);
7882     }
7883
7884   if (ui_out_is_mi_like_p (uiout))
7885     ui_out_field_string (uiout, "catch-type", "vfork");
7886 }
7887
7888 /* Implement the "print_mention" breakpoint_ops method for vfork
7889    catchpoints.  */
7890
7891 static void
7892 print_mention_catch_vfork (struct breakpoint *b)
7893 {
7894   printf_filtered (_("Catchpoint %d (vfork)"), b->number);
7895 }
7896
7897 /* Implement the "print_recreate" breakpoint_ops method for vfork
7898    catchpoints.  */
7899
7900 static void
7901 print_recreate_catch_vfork (struct breakpoint *b, struct ui_file *fp)
7902 {
7903   fprintf_unfiltered (fp, "catch vfork");
7904   print_recreate_thread (b, fp);
7905 }
7906
7907 /* The breakpoint_ops structure to be used in vfork catchpoints.  */
7908
7909 static struct breakpoint_ops catch_vfork_breakpoint_ops;
7910
7911 /* An instance of this type is used to represent an solib catchpoint.
7912    It includes a "struct breakpoint" as a kind of base class; users
7913    downcast to "struct breakpoint *" when needed.  A breakpoint is
7914    really of this type iff its ops pointer points to
7915    CATCH_SOLIB_BREAKPOINT_OPS.  */
7916
7917 struct solib_catchpoint
7918 {
7919   /* The base class.  */
7920   struct breakpoint base;
7921
7922   /* True for "catch load", false for "catch unload".  */
7923   unsigned char is_load;
7924
7925   /* Regular expression to match, if any.  COMPILED is only valid when
7926      REGEX is non-NULL.  */
7927   char *regex;
7928   regex_t compiled;
7929 };
7930
7931 static void
7932 dtor_catch_solib (struct breakpoint *b)
7933 {
7934   struct solib_catchpoint *self = (struct solib_catchpoint *) b;
7935
7936   if (self->regex)
7937     regfree (&self->compiled);
7938   xfree (self->regex);
7939
7940   base_breakpoint_ops.dtor (b);
7941 }
7942
7943 static int
7944 insert_catch_solib (struct bp_location *ignore)
7945 {
7946   return 0;
7947 }
7948
7949 static int
7950 remove_catch_solib (struct bp_location *ignore)
7951 {
7952   return 0;
7953 }
7954
7955 static int
7956 breakpoint_hit_catch_solib (const struct bp_location *bl,
7957                             struct address_space *aspace,
7958                             CORE_ADDR bp_addr,
7959                             const struct target_waitstatus *ws)
7960 {
7961   struct solib_catchpoint *self = (struct solib_catchpoint *) bl->owner;
7962   struct breakpoint *other;
7963
7964   if (ws->kind == TARGET_WAITKIND_LOADED)
7965     return 1;
7966
7967   ALL_BREAKPOINTS (other)
7968   {
7969     struct bp_location *other_bl;
7970
7971     if (other == bl->owner)
7972       continue;
7973
7974     if (other->type != bp_shlib_event)
7975       continue;
7976
7977     if (self->base.pspace != NULL && other->pspace != self->base.pspace)
7978       continue;
7979
7980     for (other_bl = other->loc; other_bl != NULL; other_bl = other_bl->next)
7981       {
7982         if (other->ops->breakpoint_hit (other_bl, aspace, bp_addr, ws))
7983           return 1;
7984       }
7985   }
7986
7987   return 0;
7988 }
7989
7990 static void
7991 check_status_catch_solib (struct bpstats *bs)
7992 {
7993   struct solib_catchpoint *self
7994     = (struct solib_catchpoint *) bs->breakpoint_at;
7995   int ix;
7996
7997   if (self->is_load)
7998     {
7999       struct so_list *iter;
8000
8001       for (ix = 0;
8002            VEC_iterate (so_list_ptr, current_program_space->added_solibs,
8003                         ix, iter);
8004            ++ix)
8005         {
8006           if (!self->regex
8007               || regexec (&self->compiled, iter->so_name, 0, NULL, 0) == 0)
8008             return;
8009         }
8010     }
8011   else
8012     {
8013       char *iter;
8014
8015       for (ix = 0;
8016            VEC_iterate (char_ptr, current_program_space->deleted_solibs,
8017                         ix, iter);
8018            ++ix)
8019         {
8020           if (!self->regex
8021               || regexec (&self->compiled, iter, 0, NULL, 0) == 0)
8022             return;
8023         }
8024     }
8025
8026   bs->stop = 0;
8027   bs->print_it = print_it_noop;
8028 }
8029
8030 static enum print_stop_action
8031 print_it_catch_solib (bpstat bs)
8032 {
8033   struct breakpoint *b = bs->breakpoint_at;
8034   struct ui_out *uiout = current_uiout;
8035
8036   annotate_catchpoint (b->number);
8037   if (b->disposition == disp_del)
8038     ui_out_text (uiout, "\nTemporary catchpoint ");
8039   else
8040     ui_out_text (uiout, "\nCatchpoint ");
8041   ui_out_field_int (uiout, "bkptno", b->number);
8042   ui_out_text (uiout, "\n");
8043   if (ui_out_is_mi_like_p (uiout))
8044     ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
8045   print_solib_event (1);
8046   return PRINT_SRC_AND_LOC;
8047 }
8048
8049 static void
8050 print_one_catch_solib (struct breakpoint *b, struct bp_location **locs)
8051 {
8052   struct solib_catchpoint *self = (struct solib_catchpoint *) b;
8053   struct value_print_options opts;
8054   struct ui_out *uiout = current_uiout;
8055   char *msg;
8056
8057   get_user_print_options (&opts);
8058   /* Field 4, the address, is omitted (which makes the columns not
8059      line up too nicely with the headers, but the effect is relatively
8060      readable).  */
8061   if (opts.addressprint)
8062     {
8063       annotate_field (4);
8064       ui_out_field_skip (uiout, "addr");
8065     }
8066
8067   annotate_field (5);
8068   if (self->is_load)
8069     {
8070       if (self->regex)
8071         msg = xstrprintf (_("load of library matching %s"), self->regex);
8072       else
8073         msg = xstrdup (_("load of library"));
8074     }
8075   else
8076     {
8077       if (self->regex)
8078         msg = xstrprintf (_("unload of library matching %s"), self->regex);
8079       else
8080         msg = xstrdup (_("unload of library"));
8081     }
8082   ui_out_field_string (uiout, "what", msg);
8083   xfree (msg);
8084
8085   if (ui_out_is_mi_like_p (uiout))
8086     ui_out_field_string (uiout, "catch-type",
8087                          self->is_load ? "load" : "unload");
8088 }
8089
8090 static void
8091 print_mention_catch_solib (struct breakpoint *b)
8092 {
8093   struct solib_catchpoint *self = (struct solib_catchpoint *) b;
8094
8095   printf_filtered (_("Catchpoint %d (%s)"), b->number,
8096                    self->is_load ? "load" : "unload");
8097 }
8098
8099 static void
8100 print_recreate_catch_solib (struct breakpoint *b, struct ui_file *fp)
8101 {
8102   struct solib_catchpoint *self = (struct solib_catchpoint *) b;
8103
8104   fprintf_unfiltered (fp, "%s %s",
8105                       b->disposition == disp_del ? "tcatch" : "catch",
8106                       self->is_load ? "load" : "unload");
8107   if (self->regex)
8108     fprintf_unfiltered (fp, " %s", self->regex);
8109   fprintf_unfiltered (fp, "\n");
8110 }
8111
8112 static struct breakpoint_ops catch_solib_breakpoint_ops;
8113
8114 /* Shared helper function (MI and CLI) for creating and installing
8115    a shared object event catchpoint.  If IS_LOAD is non-zero then
8116    the events to be caught are load events, otherwise they are
8117    unload events.  If IS_TEMP is non-zero the catchpoint is a
8118    temporary one.  If ENABLED is non-zero the catchpoint is
8119    created in an enabled state.  */
8120
8121 void
8122 add_solib_catchpoint (char *arg, int is_load, int is_temp, int enabled)
8123 {
8124   struct solib_catchpoint *c;
8125   struct gdbarch *gdbarch = get_current_arch ();
8126   struct cleanup *cleanup;
8127
8128   if (!arg)
8129     arg = "";
8130   arg = skip_spaces (arg);
8131
8132   c = XCNEW (struct solib_catchpoint);
8133   cleanup = make_cleanup (xfree, c);
8134
8135   if (*arg != '\0')
8136     {
8137       int errcode;
8138
8139       errcode = regcomp (&c->compiled, arg, REG_NOSUB);
8140       if (errcode != 0)
8141         {
8142           char *err = get_regcomp_error (errcode, &c->compiled);
8143
8144           make_cleanup (xfree, err);
8145           error (_("Invalid regexp (%s): %s"), err, arg);
8146         }
8147       c->regex = xstrdup (arg);
8148     }
8149
8150   c->is_load = is_load;
8151   init_catchpoint (&c->base, gdbarch, is_temp, NULL,
8152                    &catch_solib_breakpoint_ops);
8153
8154   c->base.enable_state = enabled ? bp_enabled : bp_disabled;
8155
8156   discard_cleanups (cleanup);
8157   install_breakpoint (0, &c->base, 1);
8158 }
8159
8160 /* A helper function that does all the work for "catch load" and
8161    "catch unload".  */
8162
8163 static void
8164 catch_load_or_unload (char *arg, int from_tty, int is_load,
8165                       struct cmd_list_element *command)
8166 {
8167   int tempflag;
8168   const int enabled = 1;
8169
8170   tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
8171
8172   add_solib_catchpoint (arg, is_load, tempflag, enabled);
8173 }
8174
8175 static void
8176 catch_load_command_1 (char *arg, int from_tty,
8177                       struct cmd_list_element *command)
8178 {
8179   catch_load_or_unload (arg, from_tty, 1, command);
8180 }
8181
8182 static void
8183 catch_unload_command_1 (char *arg, int from_tty,
8184                         struct cmd_list_element *command)
8185 {
8186   catch_load_or_unload (arg, from_tty, 0, command);
8187 }
8188
8189 /* An instance of this type is used to represent a syscall catchpoint.
8190    It includes a "struct breakpoint" as a kind of base class; users
8191    downcast to "struct breakpoint *" when needed.  A breakpoint is
8192    really of this type iff its ops pointer points to
8193    CATCH_SYSCALL_BREAKPOINT_OPS.  */
8194
8195 struct syscall_catchpoint
8196 {
8197   /* The base class.  */
8198   struct breakpoint base;
8199
8200   /* Syscall numbers used for the 'catch syscall' feature.  If no
8201      syscall has been specified for filtering, its value is NULL.
8202      Otherwise, it holds a list of all syscalls to be caught.  The
8203      list elements are allocated with xmalloc.  */
8204   VEC(int) *syscalls_to_be_caught;
8205 };
8206
8207 /* Implement the "dtor" breakpoint_ops method for syscall
8208    catchpoints.  */
8209
8210 static void
8211 dtor_catch_syscall (struct breakpoint *b)
8212 {
8213   struct syscall_catchpoint *c = (struct syscall_catchpoint *) b;
8214
8215   VEC_free (int, c->syscalls_to_be_caught);
8216
8217   base_breakpoint_ops.dtor (b);
8218 }
8219
8220 static const struct inferior_data *catch_syscall_inferior_data = NULL;
8221
8222 struct catch_syscall_inferior_data
8223 {
8224   /* We keep a count of the number of times the user has requested a
8225      particular syscall to be tracked, and pass this information to the
8226      target.  This lets capable targets implement filtering directly.  */
8227
8228   /* Number of times that "any" syscall is requested.  */
8229   int any_syscall_count;
8230
8231   /* Count of each system call.  */
8232   VEC(int) *syscalls_counts;
8233
8234   /* This counts all syscall catch requests, so we can readily determine
8235      if any catching is necessary.  */
8236   int total_syscalls_count;
8237 };
8238
8239 static struct catch_syscall_inferior_data*
8240 get_catch_syscall_inferior_data (struct inferior *inf)
8241 {
8242   struct catch_syscall_inferior_data *inf_data;
8243
8244   inf_data = inferior_data (inf, catch_syscall_inferior_data);
8245   if (inf_data == NULL)
8246     {
8247       inf_data = XCNEW (struct catch_syscall_inferior_data);
8248       set_inferior_data (inf, catch_syscall_inferior_data, inf_data);
8249     }
8250
8251   return inf_data;
8252 }
8253
8254 static void
8255 catch_syscall_inferior_data_cleanup (struct inferior *inf, void *arg)
8256 {
8257   xfree (arg);
8258 }
8259
8260
8261 /* Implement the "insert" breakpoint_ops method for syscall
8262    catchpoints.  */
8263
8264 static int
8265 insert_catch_syscall (struct bp_location *bl)
8266 {
8267   struct syscall_catchpoint *c = (struct syscall_catchpoint *) bl->owner;
8268   struct inferior *inf = current_inferior ();
8269   struct catch_syscall_inferior_data *inf_data
8270     = get_catch_syscall_inferior_data (inf);
8271
8272   ++inf_data->total_syscalls_count;
8273   if (!c->syscalls_to_be_caught)
8274     ++inf_data->any_syscall_count;
8275   else
8276     {
8277       int i, iter;
8278
8279       for (i = 0;
8280            VEC_iterate (int, c->syscalls_to_be_caught, i, iter);
8281            i++)
8282         {
8283           int elem;
8284
8285           if (iter >= VEC_length (int, inf_data->syscalls_counts))
8286             {
8287               int old_size = VEC_length (int, inf_data->syscalls_counts);
8288               uintptr_t vec_addr_offset
8289                 = old_size * ((uintptr_t) sizeof (int));
8290               uintptr_t vec_addr;
8291               VEC_safe_grow (int, inf_data->syscalls_counts, iter + 1);
8292               vec_addr = ((uintptr_t) VEC_address (int,
8293                                                   inf_data->syscalls_counts)
8294                           + vec_addr_offset);
8295               memset ((void *) vec_addr, 0,
8296                       (iter + 1 - old_size) * sizeof (int));
8297             }
8298           elem = VEC_index (int, inf_data->syscalls_counts, iter);
8299           VEC_replace (int, inf_data->syscalls_counts, iter, ++elem);
8300         }
8301     }
8302
8303   return target_set_syscall_catchpoint (ptid_get_pid (inferior_ptid),
8304                                         inf_data->total_syscalls_count != 0,
8305                                         inf_data->any_syscall_count,
8306                                         VEC_length (int,
8307                                                     inf_data->syscalls_counts),
8308                                         VEC_address (int,
8309                                                      inf_data->syscalls_counts));
8310 }
8311
8312 /* Implement the "remove" breakpoint_ops method for syscall
8313    catchpoints.  */
8314
8315 static int
8316 remove_catch_syscall (struct bp_location *bl)
8317 {
8318   struct syscall_catchpoint *c = (struct syscall_catchpoint *) bl->owner;
8319   struct inferior *inf = current_inferior ();
8320   struct catch_syscall_inferior_data *inf_data
8321     = get_catch_syscall_inferior_data (inf);
8322
8323   --inf_data->total_syscalls_count;
8324   if (!c->syscalls_to_be_caught)
8325     --inf_data->any_syscall_count;
8326   else
8327     {
8328       int i, iter;
8329
8330       for (i = 0;
8331            VEC_iterate (int, c->syscalls_to_be_caught, i, iter);
8332            i++)
8333         {
8334           int elem;
8335           if (iter >= VEC_length (int, inf_data->syscalls_counts))
8336             /* Shouldn't happen.  */
8337             continue;
8338           elem = VEC_index (int, inf_data->syscalls_counts, iter);
8339           VEC_replace (int, inf_data->syscalls_counts, iter, --elem);
8340         }
8341     }
8342
8343   return target_set_syscall_catchpoint (ptid_get_pid (inferior_ptid),
8344                                         inf_data->total_syscalls_count != 0,
8345                                         inf_data->any_syscall_count,
8346                                         VEC_length (int,
8347                                                     inf_data->syscalls_counts),
8348                                         VEC_address (int,
8349                                                      inf_data->syscalls_counts));
8350 }
8351
8352 /* Implement the "breakpoint_hit" breakpoint_ops method for syscall
8353    catchpoints.  */
8354
8355 static int
8356 breakpoint_hit_catch_syscall (const struct bp_location *bl,
8357                               struct address_space *aspace, CORE_ADDR bp_addr,
8358                               const struct target_waitstatus *ws)
8359 {
8360   /* We must check if we are catching specific syscalls in this
8361      breakpoint.  If we are, then we must guarantee that the called
8362      syscall is the same syscall we are catching.  */
8363   int syscall_number = 0;
8364   const struct syscall_catchpoint *c
8365     = (const struct syscall_catchpoint *) bl->owner;
8366
8367   if (ws->kind != TARGET_WAITKIND_SYSCALL_ENTRY
8368       && ws->kind != TARGET_WAITKIND_SYSCALL_RETURN)
8369     return 0;
8370
8371   syscall_number = ws->value.syscall_number;
8372
8373   /* Now, checking if the syscall is the same.  */
8374   if (c->syscalls_to_be_caught)
8375     {
8376       int i, iter;
8377
8378       for (i = 0;
8379            VEC_iterate (int, c->syscalls_to_be_caught, i, iter);
8380            i++)
8381         if (syscall_number == iter)
8382           return 1;
8383
8384       return 0;
8385     }
8386
8387   return 1;
8388 }
8389
8390 /* Implement the "print_it" breakpoint_ops method for syscall
8391    catchpoints.  */
8392
8393 static enum print_stop_action
8394 print_it_catch_syscall (bpstat bs)
8395 {
8396   struct ui_out *uiout = current_uiout;
8397   struct breakpoint *b = bs->breakpoint_at;
8398   /* These are needed because we want to know in which state a
8399      syscall is.  It can be in the TARGET_WAITKIND_SYSCALL_ENTRY
8400      or TARGET_WAITKIND_SYSCALL_RETURN, and depending on it we
8401      must print "called syscall" or "returned from syscall".  */
8402   ptid_t ptid;
8403   struct target_waitstatus last;
8404   struct syscall s;
8405
8406   get_last_target_status (&ptid, &last);
8407
8408   get_syscall_by_number (last.value.syscall_number, &s);
8409
8410   annotate_catchpoint (b->number);
8411
8412   if (b->disposition == disp_del)
8413     ui_out_text (uiout, "\nTemporary catchpoint ");
8414   else
8415     ui_out_text (uiout, "\nCatchpoint ");
8416   if (ui_out_is_mi_like_p (uiout))
8417     {
8418       ui_out_field_string (uiout, "reason",
8419                            async_reason_lookup (last.kind == TARGET_WAITKIND_SYSCALL_ENTRY
8420                                                 ? EXEC_ASYNC_SYSCALL_ENTRY
8421                                                 : EXEC_ASYNC_SYSCALL_RETURN));
8422       ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
8423     }
8424   ui_out_field_int (uiout, "bkptno", b->number);
8425
8426   if (last.kind == TARGET_WAITKIND_SYSCALL_ENTRY)
8427     ui_out_text (uiout, " (call to syscall ");
8428   else
8429     ui_out_text (uiout, " (returned from syscall ");
8430
8431   if (s.name == NULL || ui_out_is_mi_like_p (uiout))
8432     ui_out_field_int (uiout, "syscall-number", last.value.syscall_number);
8433   if (s.name != NULL)
8434     ui_out_field_string (uiout, "syscall-name", s.name);
8435
8436   ui_out_text (uiout, "), ");
8437
8438   return PRINT_SRC_AND_LOC;
8439 }
8440
8441 /* Implement the "print_one" breakpoint_ops method for syscall
8442    catchpoints.  */
8443
8444 static void
8445 print_one_catch_syscall (struct breakpoint *b,
8446                          struct bp_location **last_loc)
8447 {
8448   struct syscall_catchpoint *c = (struct syscall_catchpoint *) b;
8449   struct value_print_options opts;
8450   struct ui_out *uiout = current_uiout;
8451
8452   get_user_print_options (&opts);
8453   /* Field 4, the address, is omitted (which makes the columns not
8454      line up too nicely with the headers, but the effect is relatively
8455      readable).  */
8456   if (opts.addressprint)
8457     ui_out_field_skip (uiout, "addr");
8458   annotate_field (5);
8459
8460   if (c->syscalls_to_be_caught
8461       && VEC_length (int, c->syscalls_to_be_caught) > 1)
8462     ui_out_text (uiout, "syscalls \"");
8463   else
8464     ui_out_text (uiout, "syscall \"");
8465
8466   if (c->syscalls_to_be_caught)
8467     {
8468       int i, iter;
8469       char *text = xstrprintf ("%s", "");
8470
8471       for (i = 0;
8472            VEC_iterate (int, c->syscalls_to_be_caught, i, iter);
8473            i++)
8474         {
8475           char *x = text;
8476           struct syscall s;
8477           get_syscall_by_number (iter, &s);
8478
8479           if (s.name != NULL)
8480             text = xstrprintf ("%s%s, ", text, s.name);
8481           else
8482             text = xstrprintf ("%s%d, ", text, iter);
8483
8484           /* We have to xfree the last 'text' (now stored at 'x')
8485              because xstrprintf dynamically allocates new space for it
8486              on every call.  */
8487           xfree (x);
8488         }
8489       /* Remove the last comma.  */
8490       text[strlen (text) - 2] = '\0';
8491       ui_out_field_string (uiout, "what", text);
8492     }
8493   else
8494     ui_out_field_string (uiout, "what", "<any syscall>");
8495   ui_out_text (uiout, "\" ");
8496
8497   if (ui_out_is_mi_like_p (uiout))
8498     ui_out_field_string (uiout, "catch-type", "syscall");
8499 }
8500
8501 /* Implement the "print_mention" breakpoint_ops method for syscall
8502    catchpoints.  */
8503
8504 static void
8505 print_mention_catch_syscall (struct breakpoint *b)
8506 {
8507   struct syscall_catchpoint *c = (struct syscall_catchpoint *) b;
8508
8509   if (c->syscalls_to_be_caught)
8510     {
8511       int i, iter;
8512
8513       if (VEC_length (int, c->syscalls_to_be_caught) > 1)
8514         printf_filtered (_("Catchpoint %d (syscalls"), b->number);
8515       else
8516         printf_filtered (_("Catchpoint %d (syscall"), b->number);
8517
8518       for (i = 0;
8519            VEC_iterate (int, c->syscalls_to_be_caught, i, iter);
8520            i++)
8521         {
8522           struct syscall s;
8523           get_syscall_by_number (iter, &s);
8524
8525           if (s.name)
8526             printf_filtered (" '%s' [%d]", s.name, s.number);
8527           else
8528             printf_filtered (" %d", s.number);
8529         }
8530       printf_filtered (")");
8531     }
8532   else
8533     printf_filtered (_("Catchpoint %d (any syscall)"),
8534                      b->number);
8535 }
8536
8537 /* Implement the "print_recreate" breakpoint_ops method for syscall
8538    catchpoints.  */
8539
8540 static void
8541 print_recreate_catch_syscall (struct breakpoint *b, struct ui_file *fp)
8542 {
8543   struct syscall_catchpoint *c = (struct syscall_catchpoint *) b;
8544
8545   fprintf_unfiltered (fp, "catch syscall");
8546
8547   if (c->syscalls_to_be_caught)
8548     {
8549       int i, iter;
8550
8551       for (i = 0;
8552            VEC_iterate (int, c->syscalls_to_be_caught, i, iter);
8553            i++)
8554         {
8555           struct syscall s;
8556
8557           get_syscall_by_number (iter, &s);
8558           if (s.name)
8559             fprintf_unfiltered (fp, " %s", s.name);
8560           else
8561             fprintf_unfiltered (fp, " %d", s.number);
8562         }
8563     }
8564   print_recreate_thread (b, fp);
8565 }
8566
8567 /* The breakpoint_ops structure to be used in syscall catchpoints.  */
8568
8569 static struct breakpoint_ops catch_syscall_breakpoint_ops;
8570
8571 /* Returns non-zero if 'b' is a syscall catchpoint.  */
8572
8573 static int
8574 syscall_catchpoint_p (struct breakpoint *b)
8575 {
8576   return (b->ops == &catch_syscall_breakpoint_ops);
8577 }
8578
8579 /* Initialize a new breakpoint of the bp_catchpoint kind.  If TEMPFLAG
8580    is non-zero, then make the breakpoint temporary.  If COND_STRING is
8581    not NULL, then store it in the breakpoint.  OPS, if not NULL, is
8582    the breakpoint_ops structure associated to the catchpoint.  */
8583
8584 void
8585 init_catchpoint (struct breakpoint *b,
8586                  struct gdbarch *gdbarch, int tempflag,
8587                  char *cond_string,
8588                  const struct breakpoint_ops *ops)
8589 {
8590   struct symtab_and_line sal;
8591
8592   init_sal (&sal);
8593   sal.pspace = current_program_space;
8594
8595   init_raw_breakpoint (b, gdbarch, sal, bp_catchpoint, ops);
8596
8597   b->cond_string = (cond_string == NULL) ? NULL : xstrdup (cond_string);
8598   b->disposition = tempflag ? disp_del : disp_donttouch;
8599 }
8600
8601 void
8602 install_breakpoint (int internal, struct breakpoint *b, int update_gll)
8603 {
8604   add_to_breakpoint_chain (b);
8605   set_breakpoint_number (internal, b);
8606   if (is_tracepoint (b))
8607     set_tracepoint_count (breakpoint_count);
8608   if (!internal)
8609     mention (b);
8610   observer_notify_breakpoint_created (b);
8611
8612   if (update_gll)
8613     update_global_location_list (1);
8614 }
8615
8616 static void
8617 create_fork_vfork_event_catchpoint (struct gdbarch *gdbarch,
8618                                     int tempflag, char *cond_string,
8619                                     const struct breakpoint_ops *ops)
8620 {
8621   struct fork_catchpoint *c = XNEW (struct fork_catchpoint);
8622
8623   init_catchpoint (&c->base, gdbarch, tempflag, cond_string, ops);
8624
8625   c->forked_inferior_pid = null_ptid;
8626
8627   install_breakpoint (0, &c->base, 1);
8628 }
8629
8630 /* Exec catchpoints.  */
8631
8632 /* An instance of this type is used to represent an exec catchpoint.
8633    It includes a "struct breakpoint" as a kind of base class; users
8634    downcast to "struct breakpoint *" when needed.  A breakpoint is
8635    really of this type iff its ops pointer points to
8636    CATCH_EXEC_BREAKPOINT_OPS.  */
8637
8638 struct exec_catchpoint
8639 {
8640   /* The base class.  */
8641   struct breakpoint base;
8642
8643   /* Filename of a program whose exec triggered this catchpoint.
8644      This field is only valid immediately after this catchpoint has
8645      triggered.  */
8646   char *exec_pathname;
8647 };
8648
8649 /* Implement the "dtor" breakpoint_ops method for exec
8650    catchpoints.  */
8651
8652 static void
8653 dtor_catch_exec (struct breakpoint *b)
8654 {
8655   struct exec_catchpoint *c = (struct exec_catchpoint *) b;
8656
8657   xfree (c->exec_pathname);
8658
8659   base_breakpoint_ops.dtor (b);
8660 }
8661
8662 static int
8663 insert_catch_exec (struct bp_location *bl)
8664 {
8665   return target_insert_exec_catchpoint (ptid_get_pid (inferior_ptid));
8666 }
8667
8668 static int
8669 remove_catch_exec (struct bp_location *bl)
8670 {
8671   return target_remove_exec_catchpoint (ptid_get_pid (inferior_ptid));
8672 }
8673
8674 static int
8675 breakpoint_hit_catch_exec (const struct bp_location *bl,
8676                            struct address_space *aspace, CORE_ADDR bp_addr,
8677                            const struct target_waitstatus *ws)
8678 {
8679   struct exec_catchpoint *c = (struct exec_catchpoint *) bl->owner;
8680
8681   if (ws->kind != TARGET_WAITKIND_EXECD)
8682     return 0;
8683
8684   c->exec_pathname = xstrdup (ws->value.execd_pathname);
8685   return 1;
8686 }
8687
8688 static enum print_stop_action
8689 print_it_catch_exec (bpstat bs)
8690 {
8691   struct ui_out *uiout = current_uiout;
8692   struct breakpoint *b = bs->breakpoint_at;
8693   struct exec_catchpoint *c = (struct exec_catchpoint *) b;
8694
8695   annotate_catchpoint (b->number);
8696   if (b->disposition == disp_del)
8697     ui_out_text (uiout, "\nTemporary catchpoint ");
8698   else
8699     ui_out_text (uiout, "\nCatchpoint ");
8700   if (ui_out_is_mi_like_p (uiout))
8701     {
8702       ui_out_field_string (uiout, "reason",
8703                            async_reason_lookup (EXEC_ASYNC_EXEC));
8704       ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
8705     }
8706   ui_out_field_int (uiout, "bkptno", b->number);
8707   ui_out_text (uiout, " (exec'd ");
8708   ui_out_field_string (uiout, "new-exec", c->exec_pathname);
8709   ui_out_text (uiout, "), ");
8710
8711   return PRINT_SRC_AND_LOC;
8712 }
8713
8714 static void
8715 print_one_catch_exec (struct breakpoint *b, struct bp_location **last_loc)
8716 {
8717   struct exec_catchpoint *c = (struct exec_catchpoint *) b;
8718   struct value_print_options opts;
8719   struct ui_out *uiout = current_uiout;
8720
8721   get_user_print_options (&opts);
8722
8723   /* Field 4, the address, is omitted (which makes the columns
8724      not line up too nicely with the headers, but the effect
8725      is relatively readable).  */
8726   if (opts.addressprint)
8727     ui_out_field_skip (uiout, "addr");
8728   annotate_field (5);
8729   ui_out_text (uiout, "exec");
8730   if (c->exec_pathname != NULL)
8731     {
8732       ui_out_text (uiout, ", program \"");
8733       ui_out_field_string (uiout, "what", c->exec_pathname);
8734       ui_out_text (uiout, "\" ");
8735     }
8736
8737   if (ui_out_is_mi_like_p (uiout))
8738     ui_out_field_string (uiout, "catch-type", "exec");
8739 }
8740
8741 static void
8742 print_mention_catch_exec (struct breakpoint *b)
8743 {
8744   printf_filtered (_("Catchpoint %d (exec)"), b->number);
8745 }
8746
8747 /* Implement the "print_recreate" breakpoint_ops method for exec
8748    catchpoints.  */
8749
8750 static void
8751 print_recreate_catch_exec (struct breakpoint *b, struct ui_file *fp)
8752 {
8753   fprintf_unfiltered (fp, "catch exec");
8754   print_recreate_thread (b, fp);
8755 }
8756
8757 static struct breakpoint_ops catch_exec_breakpoint_ops;
8758
8759 static void
8760 create_syscall_event_catchpoint (int tempflag, VEC(int) *filter,
8761                                  const struct breakpoint_ops *ops)
8762 {
8763   struct syscall_catchpoint *c;
8764   struct gdbarch *gdbarch = get_current_arch ();
8765
8766   c = XNEW (struct syscall_catchpoint);
8767   init_catchpoint (&c->base, gdbarch, tempflag, NULL, ops);
8768   c->syscalls_to_be_caught = filter;
8769
8770   install_breakpoint (0, &c->base, 1);
8771 }
8772
8773 static int
8774 hw_breakpoint_used_count (void)
8775 {
8776   int i = 0;
8777   struct breakpoint *b;
8778   struct bp_location *bl;
8779
8780   ALL_BREAKPOINTS (b)
8781   {
8782     if (b->type == bp_hardware_breakpoint && breakpoint_enabled (b))
8783       for (bl = b->loc; bl; bl = bl->next)
8784         {
8785           /* Special types of hardware breakpoints may use more than
8786              one register.  */
8787           i += b->ops->resources_needed (bl);
8788         }
8789   }
8790
8791   return i;
8792 }
8793
8794 /* Returns the resources B would use if it were a hardware
8795    watchpoint.  */
8796
8797 static int
8798 hw_watchpoint_use_count (struct breakpoint *b)
8799 {
8800   int i = 0;
8801   struct bp_location *bl;
8802
8803   if (!breakpoint_enabled (b))
8804     return 0;
8805
8806   for (bl = b->loc; bl; bl = bl->next)
8807     {
8808       /* Special types of hardware watchpoints may use more than
8809          one register.  */
8810       i += b->ops->resources_needed (bl);
8811     }
8812
8813   return i;
8814 }
8815
8816 /* Returns the sum the used resources of all hardware watchpoints of
8817    type TYPE in the breakpoints list.  Also returns in OTHER_TYPE_USED
8818    the sum of the used resources of all hardware watchpoints of other
8819    types _not_ TYPE.  */
8820
8821 static int
8822 hw_watchpoint_used_count_others (struct breakpoint *except,
8823                                  enum bptype type, int *other_type_used)
8824 {
8825   int i = 0;
8826   struct breakpoint *b;
8827
8828   *other_type_used = 0;
8829   ALL_BREAKPOINTS (b)
8830     {
8831       if (b == except)
8832         continue;
8833       if (!breakpoint_enabled (b))
8834         continue;
8835
8836       if (b->type == type)
8837         i += hw_watchpoint_use_count (b);
8838       else if (is_hardware_watchpoint (b))
8839         *other_type_used = 1;
8840     }
8841
8842   return i;
8843 }
8844
8845 void
8846 disable_watchpoints_before_interactive_call_start (void)
8847 {
8848   struct breakpoint *b;
8849
8850   ALL_BREAKPOINTS (b)
8851   {
8852     if (is_watchpoint (b) && breakpoint_enabled (b))
8853       {
8854         b->enable_state = bp_call_disabled;
8855         update_global_location_list (0);
8856       }
8857   }
8858 }
8859
8860 void
8861 enable_watchpoints_after_interactive_call_stop (void)
8862 {
8863   struct breakpoint *b;
8864
8865   ALL_BREAKPOINTS (b)
8866   {
8867     if (is_watchpoint (b) && b->enable_state == bp_call_disabled)
8868       {
8869         b->enable_state = bp_enabled;
8870         update_global_location_list (1);
8871       }
8872   }
8873 }
8874
8875 void
8876 disable_breakpoints_before_startup (void)
8877 {
8878   current_program_space->executing_startup = 1;
8879   update_global_location_list (0);
8880 }
8881
8882 void
8883 enable_breakpoints_after_startup (void)
8884 {
8885   current_program_space->executing_startup = 0;
8886   breakpoint_re_set ();
8887 }
8888
8889
8890 /* Set a breakpoint that will evaporate an end of command
8891    at address specified by SAL.
8892    Restrict it to frame FRAME if FRAME is nonzero.  */
8893
8894 struct breakpoint *
8895 set_momentary_breakpoint (struct gdbarch *gdbarch, struct symtab_and_line sal,
8896                           struct frame_id frame_id, enum bptype type)
8897 {
8898   struct breakpoint *b;
8899
8900   /* If FRAME_ID is valid, it should be a real frame, not an inlined or
8901      tail-called one.  */
8902   gdb_assert (!frame_id_artificial_p (frame_id));
8903
8904   b = set_raw_breakpoint (gdbarch, sal, type, &momentary_breakpoint_ops);
8905   b->enable_state = bp_enabled;
8906   b->disposition = disp_donttouch;
8907   b->frame_id = frame_id;
8908
8909   /* If we're debugging a multi-threaded program, then we want
8910      momentary breakpoints to be active in only a single thread of
8911      control.  */
8912   if (in_thread_list (inferior_ptid))
8913     b->thread = pid_to_thread_id (inferior_ptid);
8914
8915   update_global_location_list_nothrow (1);
8916
8917   return b;
8918 }
8919
8920 /* Make a momentary breakpoint based on the master breakpoint ORIG.
8921    The new breakpoint will have type TYPE, and use OPS as it
8922    breakpoint_ops.  */
8923
8924 static struct breakpoint *
8925 momentary_breakpoint_from_master (struct breakpoint *orig,
8926                                   enum bptype type,
8927                                   const struct breakpoint_ops *ops)
8928 {
8929   struct breakpoint *copy;
8930
8931   copy = set_raw_breakpoint_without_location (orig->gdbarch, type, ops);
8932   copy->loc = allocate_bp_location (copy);
8933   set_breakpoint_location_function (copy->loc, 1);
8934
8935   copy->loc->gdbarch = orig->loc->gdbarch;
8936   copy->loc->requested_address = orig->loc->requested_address;
8937   copy->loc->address = orig->loc->address;
8938   copy->loc->section = orig->loc->section;
8939   copy->loc->pspace = orig->loc->pspace;
8940   copy->loc->probe = orig->loc->probe;
8941   copy->loc->line_number = orig->loc->line_number;
8942   copy->loc->symtab = orig->loc->symtab;
8943   copy->frame_id = orig->frame_id;
8944   copy->thread = orig->thread;
8945   copy->pspace = orig->pspace;
8946
8947   copy->enable_state = bp_enabled;
8948   copy->disposition = disp_donttouch;
8949   copy->number = internal_breakpoint_number--;
8950
8951   update_global_location_list_nothrow (0);
8952   return copy;
8953 }
8954
8955 /* Make a deep copy of momentary breakpoint ORIG.  Returns NULL if
8956    ORIG is NULL.  */
8957
8958 struct breakpoint *
8959 clone_momentary_breakpoint (struct breakpoint *orig)
8960 {
8961   /* If there's nothing to clone, then return nothing.  */
8962   if (orig == NULL)
8963     return NULL;
8964
8965   return momentary_breakpoint_from_master (orig, orig->type, orig->ops);
8966 }
8967
8968 struct breakpoint *
8969 set_momentary_breakpoint_at_pc (struct gdbarch *gdbarch, CORE_ADDR pc,
8970                                 enum bptype type)
8971 {
8972   struct symtab_and_line sal;
8973
8974   sal = find_pc_line (pc, 0);
8975   sal.pc = pc;
8976   sal.section = find_pc_overlay (pc);
8977   sal.explicit_pc = 1;
8978
8979   return set_momentary_breakpoint (gdbarch, sal, null_frame_id, type);
8980 }
8981 \f
8982
8983 /* Tell the user we have just set a breakpoint B.  */
8984
8985 static void
8986 mention (struct breakpoint *b)
8987 {
8988   b->ops->print_mention (b);
8989   if (ui_out_is_mi_like_p (current_uiout))
8990     return;
8991   printf_filtered ("\n");
8992 }
8993 \f
8994
8995 static struct bp_location *
8996 add_location_to_breakpoint (struct breakpoint *b,
8997                             const struct symtab_and_line *sal)
8998 {
8999   struct bp_location *loc, **tmp;
9000   CORE_ADDR adjusted_address;
9001   struct gdbarch *loc_gdbarch = get_sal_arch (*sal);
9002
9003   if (loc_gdbarch == NULL)
9004     loc_gdbarch = b->gdbarch;
9005
9006   /* Adjust the breakpoint's address prior to allocating a location.
9007      Once we call allocate_bp_location(), that mostly uninitialized
9008      location will be placed on the location chain.  Adjustment of the
9009      breakpoint may cause target_read_memory() to be called and we do
9010      not want its scan of the location chain to find a breakpoint and
9011      location that's only been partially initialized.  */
9012   adjusted_address = adjust_breakpoint_address (loc_gdbarch,
9013                                                 sal->pc, b->type);
9014
9015   /* Sort the locations by their ADDRESS.  */
9016   loc = allocate_bp_location (b);
9017   for (tmp = &(b->loc); *tmp != NULL && (*tmp)->address <= adjusted_address;
9018        tmp = &((*tmp)->next))
9019     ;
9020   loc->next = *tmp;
9021   *tmp = loc;
9022
9023   loc->requested_address = sal->pc;
9024   loc->address = adjusted_address;
9025   loc->pspace = sal->pspace;
9026   loc->probe = sal->probe;
9027   gdb_assert (loc->pspace != NULL);
9028   loc->section = sal->section;
9029   loc->gdbarch = loc_gdbarch;
9030   loc->line_number = sal->line;
9031   loc->symtab = sal->symtab;
9032
9033   set_breakpoint_location_function (loc,
9034                                     sal->explicit_pc || sal->explicit_line);
9035   return loc;
9036 }
9037 \f
9038
9039 /* Return 1 if LOC is pointing to a permanent breakpoint, 
9040    return 0 otherwise.  */
9041
9042 static int
9043 bp_loc_is_permanent (struct bp_location *loc)
9044 {
9045   int len;
9046   CORE_ADDR addr;
9047   const gdb_byte *bpoint;
9048   gdb_byte *target_mem;
9049   struct cleanup *cleanup;
9050   int retval = 0;
9051
9052   gdb_assert (loc != NULL);
9053
9054   addr = loc->address;
9055   bpoint = gdbarch_breakpoint_from_pc (loc->gdbarch, &addr, &len);
9056
9057   /* Software breakpoints unsupported?  */
9058   if (bpoint == NULL)
9059     return 0;
9060
9061   target_mem = alloca (len);
9062
9063   /* Enable the automatic memory restoration from breakpoints while
9064      we read the memory.  Otherwise we could say about our temporary
9065      breakpoints they are permanent.  */
9066   cleanup = save_current_space_and_thread ();
9067
9068   switch_to_program_space_and_thread (loc->pspace);
9069   make_show_memory_breakpoints_cleanup (0);
9070
9071   if (target_read_memory (loc->address, target_mem, len) == 0
9072       && memcmp (target_mem, bpoint, len) == 0)
9073     retval = 1;
9074
9075   do_cleanups (cleanup);
9076
9077   return retval;
9078 }
9079
9080 /* Build a command list for the dprintf corresponding to the current
9081    settings of the dprintf style options.  */
9082
9083 static void
9084 update_dprintf_command_list (struct breakpoint *b)
9085 {
9086   char *dprintf_args = b->extra_string;
9087   char *printf_line = NULL;
9088
9089   if (!dprintf_args)
9090     return;
9091
9092   dprintf_args = skip_spaces (dprintf_args);
9093
9094   /* Allow a comma, as it may have terminated a location, but don't
9095      insist on it.  */
9096   if (*dprintf_args == ',')
9097     ++dprintf_args;
9098   dprintf_args = skip_spaces (dprintf_args);
9099
9100   if (*dprintf_args != '"')
9101     error (_("Bad format string, missing '\"'."));
9102
9103   if (strcmp (dprintf_style, dprintf_style_gdb) == 0)
9104     printf_line = xstrprintf ("printf %s", dprintf_args);
9105   else if (strcmp (dprintf_style, dprintf_style_call) == 0)
9106     {
9107       if (!dprintf_function)
9108         error (_("No function supplied for dprintf call"));
9109
9110       if (dprintf_channel && strlen (dprintf_channel) > 0)
9111         printf_line = xstrprintf ("call (void) %s (%s,%s)",
9112                                   dprintf_function,
9113                                   dprintf_channel,
9114                                   dprintf_args);
9115       else
9116         printf_line = xstrprintf ("call (void) %s (%s)",
9117                                   dprintf_function,
9118                                   dprintf_args);
9119     }
9120   else if (strcmp (dprintf_style, dprintf_style_agent) == 0)
9121     {
9122       if (target_can_run_breakpoint_commands ())
9123         printf_line = xstrprintf ("agent-printf %s", dprintf_args);
9124       else
9125         {
9126           warning (_("Target cannot run dprintf commands, falling back to GDB printf"));
9127           printf_line = xstrprintf ("printf %s", dprintf_args);
9128         }
9129     }
9130   else
9131     internal_error (__FILE__, __LINE__,
9132                     _("Invalid dprintf style."));
9133
9134   gdb_assert (printf_line != NULL);
9135   /* Manufacture a printf sequence.  */
9136   {
9137     struct command_line *printf_cmd_line
9138       = xmalloc (sizeof (struct command_line));
9139
9140     printf_cmd_line = xmalloc (sizeof (struct command_line));
9141     printf_cmd_line->control_type = simple_control;
9142     printf_cmd_line->body_count = 0;
9143     printf_cmd_line->body_list = NULL;
9144     printf_cmd_line->next = NULL;
9145     printf_cmd_line->line = printf_line;
9146
9147     breakpoint_set_commands (b, printf_cmd_line);
9148   }
9149 }
9150
9151 /* Update all dprintf commands, making their command lists reflect
9152    current style settings.  */
9153
9154 static void
9155 update_dprintf_commands (char *args, int from_tty,
9156                          struct cmd_list_element *c)
9157 {
9158   struct breakpoint *b;
9159
9160   ALL_BREAKPOINTS (b)
9161     {
9162       if (b->type == bp_dprintf)
9163         update_dprintf_command_list (b);
9164     }
9165 }
9166
9167 /* Create a breakpoint with SAL as location.  Use ADDR_STRING
9168    as textual description of the location, and COND_STRING
9169    as condition expression.  */
9170
9171 static void
9172 init_breakpoint_sal (struct breakpoint *b, struct gdbarch *gdbarch,
9173                      struct symtabs_and_lines sals, char *addr_string,
9174                      char *filter, char *cond_string,
9175                      char *extra_string,
9176                      enum bptype type, enum bpdisp disposition,
9177                      int thread, int task, int ignore_count,
9178                      const struct breakpoint_ops *ops, int from_tty,
9179                      int enabled, int internal, unsigned flags,
9180                      int display_canonical)
9181 {
9182   int i;
9183
9184   if (type == bp_hardware_breakpoint)
9185     {
9186       int target_resources_ok;
9187
9188       i = hw_breakpoint_used_count ();
9189       target_resources_ok =
9190         target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
9191                                             i + 1, 0);
9192       if (target_resources_ok == 0)
9193         error (_("No hardware breakpoint support in the target."));
9194       else if (target_resources_ok < 0)
9195         error (_("Hardware breakpoints used exceeds limit."));
9196     }
9197
9198   gdb_assert (sals.nelts > 0);
9199
9200   for (i = 0; i < sals.nelts; ++i)
9201     {
9202       struct symtab_and_line sal = sals.sals[i];
9203       struct bp_location *loc;
9204
9205       if (from_tty)
9206         {
9207           struct gdbarch *loc_gdbarch = get_sal_arch (sal);
9208           if (!loc_gdbarch)
9209             loc_gdbarch = gdbarch;
9210
9211           describe_other_breakpoints (loc_gdbarch,
9212                                       sal.pspace, sal.pc, sal.section, thread);
9213         }
9214
9215       if (i == 0)
9216         {
9217           init_raw_breakpoint (b, gdbarch, sal, type, ops);
9218           b->thread = thread;
9219           b->task = task;
9220
9221           b->cond_string = cond_string;
9222           b->extra_string = extra_string;
9223           b->ignore_count = ignore_count;
9224           b->enable_state = enabled ? bp_enabled : bp_disabled;
9225           b->disposition = disposition;
9226
9227           if ((flags & CREATE_BREAKPOINT_FLAGS_INSERTED) != 0)
9228             b->loc->inserted = 1;
9229
9230           if (type == bp_static_tracepoint)
9231             {
9232               struct tracepoint *t = (struct tracepoint *) b;
9233               struct static_tracepoint_marker marker;
9234
9235               if (strace_marker_p (b))
9236                 {
9237                   /* We already know the marker exists, otherwise, we
9238                      wouldn't see a sal for it.  */
9239                   char *p = &addr_string[3];
9240                   char *endp;
9241                   char *marker_str;
9242
9243                   p = skip_spaces (p);
9244
9245                   endp = skip_to_space (p);
9246
9247                   marker_str = savestring (p, endp - p);
9248                   t->static_trace_marker_id = marker_str;
9249
9250                   printf_filtered (_("Probed static tracepoint "
9251                                      "marker \"%s\"\n"),
9252                                    t->static_trace_marker_id);
9253                 }
9254               else if (target_static_tracepoint_marker_at (sal.pc, &marker))
9255                 {
9256                   t->static_trace_marker_id = xstrdup (marker.str_id);
9257                   release_static_tracepoint_marker (&marker);
9258
9259                   printf_filtered (_("Probed static tracepoint "
9260                                      "marker \"%s\"\n"),
9261                                    t->static_trace_marker_id);
9262                 }
9263               else
9264                 warning (_("Couldn't determine the static "
9265                            "tracepoint marker to probe"));
9266             }
9267
9268           loc = b->loc;
9269         }
9270       else
9271         {
9272           loc = add_location_to_breakpoint (b, &sal);
9273           if ((flags & CREATE_BREAKPOINT_FLAGS_INSERTED) != 0)
9274             loc->inserted = 1;
9275         }
9276
9277       if (bp_loc_is_permanent (loc))
9278         make_breakpoint_permanent (b);
9279
9280       if (b->cond_string)
9281         {
9282           const char *arg = b->cond_string;
9283
9284           loc->cond = parse_exp_1 (&arg, loc->address,
9285                                    block_for_pc (loc->address), 0);
9286           if (*arg)
9287               error (_("Garbage '%s' follows condition"), arg);
9288         }
9289
9290       /* Dynamic printf requires and uses additional arguments on the
9291          command line, otherwise it's an error.  */
9292       if (type == bp_dprintf)
9293         {
9294           if (b->extra_string)
9295             update_dprintf_command_list (b);
9296           else
9297             error (_("Format string required"));
9298         }
9299       else if (b->extra_string)
9300         error (_("Garbage '%s' at end of command"), b->extra_string);
9301     }
9302
9303   b->display_canonical = display_canonical;
9304   if (addr_string)
9305     b->addr_string = addr_string;
9306   else
9307     /* addr_string has to be used or breakpoint_re_set will delete
9308        me.  */
9309     b->addr_string
9310       = xstrprintf ("*%s", paddress (b->loc->gdbarch, b->loc->address));
9311   b->filter = filter;
9312 }
9313
9314 static void
9315 create_breakpoint_sal (struct gdbarch *gdbarch,
9316                        struct symtabs_and_lines sals, char *addr_string,
9317                        char *filter, char *cond_string,
9318                        char *extra_string,
9319                        enum bptype type, enum bpdisp disposition,
9320                        int thread, int task, int ignore_count,
9321                        const struct breakpoint_ops *ops, int from_tty,
9322                        int enabled, int internal, unsigned flags,
9323                        int display_canonical)
9324 {
9325   struct breakpoint *b;
9326   struct cleanup *old_chain;
9327
9328   if (is_tracepoint_type (type))
9329     {
9330       struct tracepoint *t;
9331
9332       t = XCNEW (struct tracepoint);
9333       b = &t->base;
9334     }
9335   else
9336     b = XNEW (struct breakpoint);
9337
9338   old_chain = make_cleanup (xfree, b);
9339
9340   init_breakpoint_sal (b, gdbarch,
9341                        sals, addr_string,
9342                        filter, cond_string, extra_string,
9343                        type, disposition,
9344                        thread, task, ignore_count,
9345                        ops, from_tty,
9346                        enabled, internal, flags,
9347                        display_canonical);
9348   discard_cleanups (old_chain);
9349
9350   install_breakpoint (internal, b, 0);
9351 }
9352
9353 /* Add SALS.nelts breakpoints to the breakpoint table.  For each
9354    SALS.sal[i] breakpoint, include the corresponding ADDR_STRING[i]
9355    value.  COND_STRING, if not NULL, specified the condition to be
9356    used for all breakpoints.  Essentially the only case where
9357    SALS.nelts is not 1 is when we set a breakpoint on an overloaded
9358    function.  In that case, it's still not possible to specify
9359    separate conditions for different overloaded functions, so
9360    we take just a single condition string.
9361    
9362    NOTE: If the function succeeds, the caller is expected to cleanup
9363    the arrays ADDR_STRING, COND_STRING, and SALS (but not the
9364    array contents).  If the function fails (error() is called), the
9365    caller is expected to cleanups both the ADDR_STRING, COND_STRING,
9366    COND and SALS arrays and each of those arrays contents.  */
9367
9368 static void
9369 create_breakpoints_sal (struct gdbarch *gdbarch,
9370                         struct linespec_result *canonical,
9371                         char *cond_string, char *extra_string,
9372                         enum bptype type, enum bpdisp disposition,
9373                         int thread, int task, int ignore_count,
9374                         const struct breakpoint_ops *ops, int from_tty,
9375                         int enabled, int internal, unsigned flags)
9376 {
9377   int i;
9378   struct linespec_sals *lsal;
9379
9380   if (canonical->pre_expanded)
9381     gdb_assert (VEC_length (linespec_sals, canonical->sals) == 1);
9382
9383   for (i = 0; VEC_iterate (linespec_sals, canonical->sals, i, lsal); ++i)
9384     {
9385       /* Note that 'addr_string' can be NULL in the case of a plain
9386          'break', without arguments.  */
9387       char *addr_string = (canonical->addr_string
9388                            ? xstrdup (canonical->addr_string)
9389                            : NULL);
9390       char *filter_string = lsal->canonical ? xstrdup (lsal->canonical) : NULL;
9391       struct cleanup *inner = make_cleanup (xfree, addr_string);
9392
9393       make_cleanup (xfree, filter_string);
9394       create_breakpoint_sal (gdbarch, lsal->sals,
9395                              addr_string,
9396                              filter_string,
9397                              cond_string, extra_string,
9398                              type, disposition,
9399                              thread, task, ignore_count, ops,
9400                              from_tty, enabled, internal, flags,
9401                              canonical->special_display);
9402       discard_cleanups (inner);
9403     }
9404 }
9405
9406 /* Parse ADDRESS which is assumed to be a SAL specification possibly
9407    followed by conditionals.  On return, SALS contains an array of SAL
9408    addresses found.  ADDR_STRING contains a vector of (canonical)
9409    address strings.  ADDRESS points to the end of the SAL.
9410
9411    The array and the line spec strings are allocated on the heap, it is
9412    the caller's responsibility to free them.  */
9413
9414 static void
9415 parse_breakpoint_sals (char **address,
9416                        struct linespec_result *canonical)
9417 {
9418   /* If no arg given, or if first arg is 'if ', use the default
9419      breakpoint.  */
9420   if ((*address) == NULL
9421       || (strncmp ((*address), "if", 2) == 0 && isspace ((*address)[2])))
9422     {
9423       /* The last displayed codepoint, if it's valid, is our default breakpoint
9424          address.  */
9425       if (last_displayed_sal_is_valid ())
9426         {
9427           struct linespec_sals lsal;
9428           struct symtab_and_line sal;
9429           CORE_ADDR pc;
9430
9431           init_sal (&sal);              /* Initialize to zeroes.  */
9432           lsal.sals.sals = (struct symtab_and_line *)
9433             xmalloc (sizeof (struct symtab_and_line));
9434
9435           /* Set sal's pspace, pc, symtab, and line to the values
9436              corresponding to the last call to print_frame_info.
9437              Be sure to reinitialize LINE with NOTCURRENT == 0
9438              as the breakpoint line number is inappropriate otherwise.
9439              find_pc_line would adjust PC, re-set it back.  */
9440           get_last_displayed_sal (&sal);
9441           pc = sal.pc;
9442           sal = find_pc_line (pc, 0);
9443
9444           /* "break" without arguments is equivalent to "break *PC"
9445              where PC is the last displayed codepoint's address.  So
9446              make sure to set sal.explicit_pc to prevent GDB from
9447              trying to expand the list of sals to include all other
9448              instances with the same symtab and line.  */
9449           sal.pc = pc;
9450           sal.explicit_pc = 1;
9451
9452           lsal.sals.sals[0] = sal;
9453           lsal.sals.nelts = 1;
9454           lsal.canonical = NULL;
9455
9456           VEC_safe_push (linespec_sals, canonical->sals, &lsal);
9457         }
9458       else
9459         error (_("No default breakpoint address now."));
9460     }
9461   else
9462     {
9463       struct symtab_and_line cursal = get_current_source_symtab_and_line ();
9464
9465       /* Force almost all breakpoints to be in terms of the
9466          current_source_symtab (which is decode_line_1's default).
9467          This should produce the results we want almost all of the
9468          time while leaving default_breakpoint_* alone.
9469
9470          ObjC: However, don't match an Objective-C method name which
9471          may have a '+' or '-' succeeded by a '['.  */
9472       if (last_displayed_sal_is_valid ()
9473           && (!cursal.symtab
9474               || ((strchr ("+-", (*address)[0]) != NULL)
9475                   && ((*address)[1] != '['))))
9476         decode_line_full (address, DECODE_LINE_FUNFIRSTLINE,
9477                           get_last_displayed_symtab (),
9478                           get_last_displayed_line (),
9479                           canonical, NULL, NULL);
9480       else
9481         decode_line_full (address, DECODE_LINE_FUNFIRSTLINE,
9482                           cursal.symtab, cursal.line, canonical, NULL, NULL);
9483     }
9484 }
9485
9486
9487 /* Convert each SAL into a real PC.  Verify that the PC can be
9488    inserted as a breakpoint.  If it can't throw an error.  */
9489
9490 static void
9491 breakpoint_sals_to_pc (struct symtabs_and_lines *sals)
9492 {    
9493   int i;
9494
9495   for (i = 0; i < sals->nelts; i++)
9496     resolve_sal_pc (&sals->sals[i]);
9497 }
9498
9499 /* Fast tracepoints may have restrictions on valid locations.  For
9500    instance, a fast tracepoint using a jump instead of a trap will
9501    likely have to overwrite more bytes than a trap would, and so can
9502    only be placed where the instruction is longer than the jump, or a
9503    multi-instruction sequence does not have a jump into the middle of
9504    it, etc.  */
9505
9506 static void
9507 check_fast_tracepoint_sals (struct gdbarch *gdbarch,
9508                             struct symtabs_and_lines *sals)
9509 {
9510   int i, rslt;
9511   struct symtab_and_line *sal;
9512   char *msg;
9513   struct cleanup *old_chain;
9514
9515   for (i = 0; i < sals->nelts; i++)
9516     {
9517       struct gdbarch *sarch;
9518
9519       sal = &sals->sals[i];
9520
9521       sarch = get_sal_arch (*sal);
9522       /* We fall back to GDBARCH if there is no architecture
9523          associated with SAL.  */
9524       if (sarch == NULL)
9525         sarch = gdbarch;
9526       rslt = gdbarch_fast_tracepoint_valid_at (sarch, sal->pc,
9527                                                NULL, &msg);
9528       old_chain = make_cleanup (xfree, msg);
9529
9530       if (!rslt)
9531         error (_("May not have a fast tracepoint at 0x%s%s"),
9532                paddress (sarch, sal->pc), (msg ? msg : ""));
9533
9534       do_cleanups (old_chain);
9535     }
9536 }
9537
9538 /* Issue an invalid thread ID error.  */
9539
9540 static void ATTRIBUTE_NORETURN
9541 invalid_thread_id_error (int id)
9542 {
9543   error (_("Unknown thread %d."), id);
9544 }
9545
9546 /* Given TOK, a string specification of condition and thread, as
9547    accepted by the 'break' command, extract the condition
9548    string and thread number and set *COND_STRING and *THREAD.
9549    PC identifies the context at which the condition should be parsed.
9550    If no condition is found, *COND_STRING is set to NULL.
9551    If no thread is found, *THREAD is set to -1.  */
9552
9553 static void
9554 find_condition_and_thread (const char *tok, CORE_ADDR pc,
9555                            char **cond_string, int *thread, int *task,
9556                            char **rest)
9557 {
9558   *cond_string = NULL;
9559   *thread = -1;
9560   *task = 0;
9561   *rest = NULL;
9562
9563   while (tok && *tok)
9564     {
9565       const char *end_tok;
9566       int toklen;
9567       const char *cond_start = NULL;
9568       const char *cond_end = NULL;
9569
9570       tok = skip_spaces_const (tok);
9571
9572       if ((*tok == '"' || *tok == ',') && rest)
9573         {
9574           *rest = savestring (tok, strlen (tok));
9575           return;
9576         }
9577
9578       end_tok = skip_to_space_const (tok);
9579
9580       toklen = end_tok - tok;
9581
9582       if (toklen >= 1 && strncmp (tok, "if", toklen) == 0)
9583         {
9584           struct expression *expr;
9585
9586           tok = cond_start = end_tok + 1;
9587           expr = parse_exp_1 (&tok, pc, block_for_pc (pc), 0);
9588           xfree (expr);
9589           cond_end = tok;
9590           *cond_string = savestring (cond_start, cond_end - cond_start);
9591         }
9592       else if (toklen >= 1 && strncmp (tok, "thread", toklen) == 0)
9593         {
9594           char *tmptok;
9595
9596           tok = end_tok + 1;
9597           *thread = strtol (tok, &tmptok, 0);
9598           if (tok == tmptok)
9599             error (_("Junk after thread keyword."));
9600           if (!valid_thread_id (*thread))
9601             invalid_thread_id_error (*thread);
9602           tok = tmptok;
9603         }
9604       else if (toklen >= 1 && strncmp (tok, "task", toklen) == 0)
9605         {
9606           char *tmptok;
9607
9608           tok = end_tok + 1;
9609           *task = strtol (tok, &tmptok, 0);
9610           if (tok == tmptok)
9611             error (_("Junk after task keyword."));
9612           if (!valid_task_id (*task))
9613             error (_("Unknown task %d."), *task);
9614           tok = tmptok;
9615         }
9616       else if (rest)
9617         {
9618           *rest = savestring (tok, strlen (tok));
9619           return;
9620         }
9621       else
9622         error (_("Junk at end of arguments."));
9623     }
9624 }
9625
9626 /* Decode a static tracepoint marker spec.  */
9627
9628 static struct symtabs_and_lines
9629 decode_static_tracepoint_spec (char **arg_p)
9630 {
9631   VEC(static_tracepoint_marker_p) *markers = NULL;
9632   struct symtabs_and_lines sals;
9633   struct cleanup *old_chain;
9634   char *p = &(*arg_p)[3];
9635   char *endp;
9636   char *marker_str;
9637   int i;
9638
9639   p = skip_spaces (p);
9640
9641   endp = skip_to_space (p);
9642
9643   marker_str = savestring (p, endp - p);
9644   old_chain = make_cleanup (xfree, marker_str);
9645
9646   markers = target_static_tracepoint_markers_by_strid (marker_str);
9647   if (VEC_empty(static_tracepoint_marker_p, markers))
9648     error (_("No known static tracepoint marker named %s"), marker_str);
9649
9650   sals.nelts = VEC_length(static_tracepoint_marker_p, markers);
9651   sals.sals = xmalloc (sizeof *sals.sals * sals.nelts);
9652
9653   for (i = 0; i < sals.nelts; i++)
9654     {
9655       struct static_tracepoint_marker *marker;
9656
9657       marker = VEC_index (static_tracepoint_marker_p, markers, i);
9658
9659       init_sal (&sals.sals[i]);
9660
9661       sals.sals[i] = find_pc_line (marker->address, 0);
9662       sals.sals[i].pc = marker->address;
9663
9664       release_static_tracepoint_marker (marker);
9665     }
9666
9667   do_cleanups (old_chain);
9668
9669   *arg_p = endp;
9670   return sals;
9671 }
9672
9673 /* Set a breakpoint.  This function is shared between CLI and MI
9674    functions for setting a breakpoint.  This function has two major
9675    modes of operations, selected by the PARSE_ARG parameter.  If
9676    non-zero, the function will parse ARG, extracting location,
9677    condition, thread and extra string.  Otherwise, ARG is just the
9678    breakpoint's location, with condition, thread, and extra string
9679    specified by the COND_STRING, THREAD and EXTRA_STRING parameters.
9680    If INTERNAL is non-zero, the breakpoint number will be allocated
9681    from the internal breakpoint count.  Returns true if any breakpoint
9682    was created; false otherwise.  */
9683
9684 int
9685 create_breakpoint (struct gdbarch *gdbarch,
9686                    char *arg, char *cond_string,
9687                    int thread, char *extra_string,
9688                    int parse_arg,
9689                    int tempflag, enum bptype type_wanted,
9690                    int ignore_count,
9691                    enum auto_boolean pending_break_support,
9692                    const struct breakpoint_ops *ops,
9693                    int from_tty, int enabled, int internal,
9694                    unsigned flags)
9695 {
9696   volatile struct gdb_exception e;
9697   char *copy_arg = NULL;
9698   char *addr_start = arg;
9699   struct linespec_result canonical;
9700   struct cleanup *old_chain;
9701   struct cleanup *bkpt_chain = NULL;
9702   int pending = 0;
9703   int task = 0;
9704   int prev_bkpt_count = breakpoint_count;
9705
9706   gdb_assert (ops != NULL);
9707
9708   init_linespec_result (&canonical);
9709
9710   TRY_CATCH (e, RETURN_MASK_ALL)
9711     {
9712       ops->create_sals_from_address (&arg, &canonical, type_wanted,
9713                                      addr_start, &copy_arg);
9714     }
9715
9716   /* If caller is interested in rc value from parse, set value.  */
9717   switch (e.reason)
9718     {
9719     case GDB_NO_ERROR:
9720       if (VEC_empty (linespec_sals, canonical.sals))
9721         return 0;
9722       break;
9723     case RETURN_ERROR:
9724       switch (e.error)
9725         {
9726         case NOT_FOUND_ERROR:
9727
9728           /* If pending breakpoint support is turned off, throw
9729              error.  */
9730
9731           if (pending_break_support == AUTO_BOOLEAN_FALSE)
9732             throw_exception (e);
9733
9734           exception_print (gdb_stderr, e);
9735
9736           /* If pending breakpoint support is auto query and the user
9737              selects no, then simply return the error code.  */
9738           if (pending_break_support == AUTO_BOOLEAN_AUTO
9739               && !nquery (_("Make %s pending on future shared library load? "),
9740                           bptype_string (type_wanted)))
9741             return 0;
9742
9743           /* At this point, either the user was queried about setting
9744              a pending breakpoint and selected yes, or pending
9745              breakpoint behavior is on and thus a pending breakpoint
9746              is defaulted on behalf of the user.  */
9747           {
9748             struct linespec_sals lsal;
9749
9750             copy_arg = xstrdup (addr_start);
9751             lsal.canonical = xstrdup (copy_arg);
9752             lsal.sals.nelts = 1;
9753             lsal.sals.sals = XNEW (struct symtab_and_line);
9754             init_sal (&lsal.sals.sals[0]);
9755             pending = 1;
9756             VEC_safe_push (linespec_sals, canonical.sals, &lsal);
9757           }
9758           break;
9759         default:
9760           throw_exception (e);
9761         }
9762       break;
9763     default:
9764       throw_exception (e);
9765     }
9766
9767   /* Create a chain of things that always need to be cleaned up.  */
9768   old_chain = make_cleanup_destroy_linespec_result (&canonical);
9769
9770   /* ----------------------------- SNIP -----------------------------
9771      Anything added to the cleanup chain beyond this point is assumed
9772      to be part of a breakpoint.  If the breakpoint create succeeds
9773      then the memory is not reclaimed.  */
9774   bkpt_chain = make_cleanup (null_cleanup, 0);
9775
9776   /* Resolve all line numbers to PC's and verify that the addresses
9777      are ok for the target.  */
9778   if (!pending)
9779     {
9780       int ix;
9781       struct linespec_sals *iter;
9782
9783       for (ix = 0; VEC_iterate (linespec_sals, canonical.sals, ix, iter); ++ix)
9784         breakpoint_sals_to_pc (&iter->sals);
9785     }
9786
9787   /* Fast tracepoints may have additional restrictions on location.  */
9788   if (!pending && type_wanted == bp_fast_tracepoint)
9789     {
9790       int ix;
9791       struct linespec_sals *iter;
9792
9793       for (ix = 0; VEC_iterate (linespec_sals, canonical.sals, ix, iter); ++ix)
9794         check_fast_tracepoint_sals (gdbarch, &iter->sals);
9795     }
9796
9797   /* Verify that condition can be parsed, before setting any
9798      breakpoints.  Allocate a separate condition expression for each
9799      breakpoint.  */
9800   if (!pending)
9801     {
9802       if (parse_arg)
9803         {
9804           char *rest;
9805           struct linespec_sals *lsal;
9806
9807           lsal = VEC_index (linespec_sals, canonical.sals, 0);
9808
9809           /* Here we only parse 'arg' to separate condition
9810              from thread number, so parsing in context of first
9811              sal is OK.  When setting the breakpoint we'll
9812              re-parse it in context of each sal.  */
9813
9814           find_condition_and_thread (arg, lsal->sals.sals[0].pc, &cond_string,
9815                                      &thread, &task, &rest);
9816           if (cond_string)
9817             make_cleanup (xfree, cond_string);
9818           if (rest)
9819             make_cleanup (xfree, rest);
9820           if (rest)
9821             extra_string = rest;
9822         }
9823       else
9824         {
9825           if (*arg != '\0')
9826             error (_("Garbage '%s' at end of location"), arg);
9827
9828           /* Create a private copy of condition string.  */
9829           if (cond_string)
9830             {
9831               cond_string = xstrdup (cond_string);
9832               make_cleanup (xfree, cond_string);
9833             }
9834           /* Create a private copy of any extra string.  */
9835           if (extra_string)
9836             {
9837               extra_string = xstrdup (extra_string);
9838               make_cleanup (xfree, extra_string);
9839             }
9840         }
9841
9842       ops->create_breakpoints_sal (gdbarch, &canonical,
9843                                    cond_string, extra_string, type_wanted,
9844                                    tempflag ? disp_del : disp_donttouch,
9845                                    thread, task, ignore_count, ops,
9846                                    from_tty, enabled, internal, flags);
9847     }
9848   else
9849     {
9850       struct breakpoint *b;
9851
9852       make_cleanup (xfree, copy_arg);
9853
9854       if (is_tracepoint_type (type_wanted))
9855         {
9856           struct tracepoint *t;
9857
9858           t = XCNEW (struct tracepoint);
9859           b = &t->base;
9860         }
9861       else
9862         b = XNEW (struct breakpoint);
9863
9864       init_raw_breakpoint_without_location (b, gdbarch, type_wanted, ops);
9865
9866       b->addr_string = copy_arg;
9867       if (parse_arg)
9868         b->cond_string = NULL;
9869       else
9870         {
9871           /* Create a private copy of condition string.  */
9872           if (cond_string)
9873             {
9874               cond_string = xstrdup (cond_string);
9875               make_cleanup (xfree, cond_string);
9876             }
9877           b->cond_string = cond_string;
9878         }
9879       b->extra_string = NULL;
9880       b->ignore_count = ignore_count;
9881       b->disposition = tempflag ? disp_del : disp_donttouch;
9882       b->condition_not_parsed = 1;
9883       b->enable_state = enabled ? bp_enabled : bp_disabled;
9884       if ((type_wanted != bp_breakpoint
9885            && type_wanted != bp_hardware_breakpoint) || thread != -1)
9886         b->pspace = current_program_space;
9887
9888       install_breakpoint (internal, b, 0);
9889     }
9890   
9891   if (VEC_length (linespec_sals, canonical.sals) > 1)
9892     {
9893       warning (_("Multiple breakpoints were set.\nUse the "
9894                  "\"delete\" command to delete unwanted breakpoints."));
9895       prev_breakpoint_count = prev_bkpt_count;
9896     }
9897
9898   /* That's it.  Discard the cleanups for data inserted into the
9899      breakpoint.  */
9900   discard_cleanups (bkpt_chain);
9901   /* But cleanup everything else.  */
9902   do_cleanups (old_chain);
9903
9904   /* error call may happen here - have BKPT_CHAIN already discarded.  */
9905   update_global_location_list (1);
9906
9907   return 1;
9908 }
9909
9910 /* Set a breakpoint.
9911    ARG is a string describing breakpoint address,
9912    condition, and thread.
9913    FLAG specifies if a breakpoint is hardware on,
9914    and if breakpoint is temporary, using BP_HARDWARE_FLAG
9915    and BP_TEMPFLAG.  */
9916
9917 static void
9918 break_command_1 (char *arg, int flag, int from_tty)
9919 {
9920   int tempflag = flag & BP_TEMPFLAG;
9921   enum bptype type_wanted = (flag & BP_HARDWAREFLAG
9922                              ? bp_hardware_breakpoint
9923                              : bp_breakpoint);
9924   struct breakpoint_ops *ops;
9925   const char *arg_cp = arg;
9926
9927   /* Matching breakpoints on probes.  */
9928   if (arg && probe_linespec_to_ops (&arg_cp) != NULL)
9929     ops = &bkpt_probe_breakpoint_ops;
9930   else
9931     ops = &bkpt_breakpoint_ops;
9932
9933   create_breakpoint (get_current_arch (),
9934                      arg,
9935                      NULL, 0, NULL, 1 /* parse arg */,
9936                      tempflag, type_wanted,
9937                      0 /* Ignore count */,
9938                      pending_break_support,
9939                      ops,
9940                      from_tty,
9941                      1 /* enabled */,
9942                      0 /* internal */,
9943                      0);
9944 }
9945
9946 /* Helper function for break_command_1 and disassemble_command.  */
9947
9948 void
9949 resolve_sal_pc (struct symtab_and_line *sal)
9950 {
9951   CORE_ADDR pc;
9952
9953   if (sal->pc == 0 && sal->symtab != NULL)
9954     {
9955       if (!find_line_pc (sal->symtab, sal->line, &pc))
9956         error (_("No line %d in file \"%s\"."),
9957                sal->line, symtab_to_filename_for_display (sal->symtab));
9958       sal->pc = pc;
9959
9960       /* If this SAL corresponds to a breakpoint inserted using a line
9961          number, then skip the function prologue if necessary.  */
9962       if (sal->explicit_line)
9963         skip_prologue_sal (sal);
9964     }
9965
9966   if (sal->section == 0 && sal->symtab != NULL)
9967     {
9968       struct blockvector *bv;
9969       struct block *b;
9970       struct symbol *sym;
9971
9972       bv = blockvector_for_pc_sect (sal->pc, 0, &b, sal->symtab);
9973       if (bv != NULL)
9974         {
9975           sym = block_linkage_function (b);
9976           if (sym != NULL)
9977             {
9978               fixup_symbol_section (sym, sal->symtab->objfile);
9979               sal->section = SYMBOL_OBJ_SECTION (sal->symtab->objfile, sym);
9980             }
9981           else
9982             {
9983               /* It really is worthwhile to have the section, so we'll
9984                  just have to look harder. This case can be executed
9985                  if we have line numbers but no functions (as can
9986                  happen in assembly source).  */
9987
9988               struct bound_minimal_symbol msym;
9989               struct cleanup *old_chain = save_current_space_and_thread ();
9990
9991               switch_to_program_space_and_thread (sal->pspace);
9992
9993               msym = lookup_minimal_symbol_by_pc (sal->pc);
9994               if (msym.minsym)
9995                 sal->section = MSYMBOL_OBJ_SECTION (msym.objfile, msym.minsym);
9996
9997               do_cleanups (old_chain);
9998             }
9999         }
10000     }
10001 }
10002
10003 void
10004 break_command (char *arg, int from_tty)
10005 {
10006   break_command_1 (arg, 0, from_tty);
10007 }
10008
10009 void
10010 tbreak_command (char *arg, int from_tty)
10011 {
10012   break_command_1 (arg, BP_TEMPFLAG, from_tty);
10013 }
10014
10015 static void
10016 hbreak_command (char *arg, int from_tty)
10017 {
10018   break_command_1 (arg, BP_HARDWAREFLAG, from_tty);
10019 }
10020
10021 static void
10022 thbreak_command (char *arg, int from_tty)
10023 {
10024   break_command_1 (arg, (BP_TEMPFLAG | BP_HARDWAREFLAG), from_tty);
10025 }
10026
10027 static void
10028 stop_command (char *arg, int from_tty)
10029 {
10030   printf_filtered (_("Specify the type of breakpoint to set.\n\
10031 Usage: stop in <function | address>\n\
10032        stop at <line>\n"));
10033 }
10034
10035 static void
10036 stopin_command (char *arg, int from_tty)
10037 {
10038   int badInput = 0;
10039
10040   if (arg == (char *) NULL)
10041     badInput = 1;
10042   else if (*arg != '*')
10043     {
10044       char *argptr = arg;
10045       int hasColon = 0;
10046
10047       /* Look for a ':'.  If this is a line number specification, then
10048          say it is bad, otherwise, it should be an address or
10049          function/method name.  */
10050       while (*argptr && !hasColon)
10051         {
10052           hasColon = (*argptr == ':');
10053           argptr++;
10054         }
10055
10056       if (hasColon)
10057         badInput = (*argptr != ':');    /* Not a class::method */
10058       else
10059         badInput = isdigit (*arg);      /* a simple line number */
10060     }
10061
10062   if (badInput)
10063     printf_filtered (_("Usage: stop in <function | address>\n"));
10064   else
10065     break_command_1 (arg, 0, from_tty);
10066 }
10067
10068 static void
10069 stopat_command (char *arg, int from_tty)
10070 {
10071   int badInput = 0;
10072
10073   if (arg == (char *) NULL || *arg == '*')      /* no line number */
10074     badInput = 1;
10075   else
10076     {
10077       char *argptr = arg;
10078       int hasColon = 0;
10079
10080       /* Look for a ':'.  If there is a '::' then get out, otherwise
10081          it is probably a line number.  */
10082       while (*argptr && !hasColon)
10083         {
10084           hasColon = (*argptr == ':');
10085           argptr++;
10086         }
10087
10088       if (hasColon)
10089         badInput = (*argptr == ':');    /* we have class::method */
10090       else
10091         badInput = !isdigit (*arg);     /* not a line number */
10092     }
10093
10094   if (badInput)
10095     printf_filtered (_("Usage: stop at <line>\n"));
10096   else
10097     break_command_1 (arg, 0, from_tty);
10098 }
10099
10100 /* The dynamic printf command is mostly like a regular breakpoint, but
10101    with a prewired command list consisting of a single output command,
10102    built from extra arguments supplied on the dprintf command
10103    line.  */
10104
10105 static void
10106 dprintf_command (char *arg, int from_tty)
10107 {
10108   create_breakpoint (get_current_arch (),
10109                      arg,
10110                      NULL, 0, NULL, 1 /* parse arg */,
10111                      0, bp_dprintf,
10112                      0 /* Ignore count */,
10113                      pending_break_support,
10114                      &dprintf_breakpoint_ops,
10115                      from_tty,
10116                      1 /* enabled */,
10117                      0 /* internal */,
10118                      0);
10119 }
10120
10121 static void
10122 agent_printf_command (char *arg, int from_tty)
10123 {
10124   error (_("May only run agent-printf on the target"));
10125 }
10126
10127 /* Implement the "breakpoint_hit" breakpoint_ops method for
10128    ranged breakpoints.  */
10129
10130 static int
10131 breakpoint_hit_ranged_breakpoint (const struct bp_location *bl,
10132                                   struct address_space *aspace,
10133                                   CORE_ADDR bp_addr,
10134                                   const struct target_waitstatus *ws)
10135 {
10136   if (ws->kind != TARGET_WAITKIND_STOPPED
10137       || ws->value.sig != GDB_SIGNAL_TRAP)
10138     return 0;
10139
10140   return breakpoint_address_match_range (bl->pspace->aspace, bl->address,
10141                                          bl->length, aspace, bp_addr);
10142 }
10143
10144 /* Implement the "resources_needed" breakpoint_ops method for
10145    ranged breakpoints.  */
10146
10147 static int
10148 resources_needed_ranged_breakpoint (const struct bp_location *bl)
10149 {
10150   return target_ranged_break_num_registers ();
10151 }
10152
10153 /* Implement the "print_it" breakpoint_ops method for
10154    ranged breakpoints.  */
10155
10156 static enum print_stop_action
10157 print_it_ranged_breakpoint (bpstat bs)
10158 {
10159   struct breakpoint *b = bs->breakpoint_at;
10160   struct bp_location *bl = b->loc;
10161   struct ui_out *uiout = current_uiout;
10162
10163   gdb_assert (b->type == bp_hardware_breakpoint);
10164
10165   /* Ranged breakpoints have only one location.  */
10166   gdb_assert (bl && bl->next == NULL);
10167
10168   annotate_breakpoint (b->number);
10169   if (b->disposition == disp_del)
10170     ui_out_text (uiout, "\nTemporary ranged breakpoint ");
10171   else
10172     ui_out_text (uiout, "\nRanged breakpoint ");
10173   if (ui_out_is_mi_like_p (uiout))
10174     {
10175       ui_out_field_string (uiout, "reason",
10176                       async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT));
10177       ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
10178     }
10179   ui_out_field_int (uiout, "bkptno", b->number);
10180   ui_out_text (uiout, ", ");
10181
10182   return PRINT_SRC_AND_LOC;
10183 }
10184
10185 /* Implement the "print_one" breakpoint_ops method for
10186    ranged breakpoints.  */
10187
10188 static void
10189 print_one_ranged_breakpoint (struct breakpoint *b,
10190                              struct bp_location **last_loc)
10191 {
10192   struct bp_location *bl = b->loc;
10193   struct value_print_options opts;
10194   struct ui_out *uiout = current_uiout;
10195
10196   /* Ranged breakpoints have only one location.  */
10197   gdb_assert (bl && bl->next == NULL);
10198
10199   get_user_print_options (&opts);
10200
10201   if (opts.addressprint)
10202     /* We don't print the address range here, it will be printed later
10203        by print_one_detail_ranged_breakpoint.  */
10204     ui_out_field_skip (uiout, "addr");
10205   annotate_field (5);
10206   print_breakpoint_location (b, bl);
10207   *last_loc = bl;
10208 }
10209
10210 /* Implement the "print_one_detail" breakpoint_ops method for
10211    ranged breakpoints.  */
10212
10213 static void
10214 print_one_detail_ranged_breakpoint (const struct breakpoint *b,
10215                                     struct ui_out *uiout)
10216 {
10217   CORE_ADDR address_start, address_end;
10218   struct bp_location *bl = b->loc;
10219   struct ui_file *stb = mem_fileopen ();
10220   struct cleanup *cleanup = make_cleanup_ui_file_delete (stb);
10221
10222   gdb_assert (bl);
10223
10224   address_start = bl->address;
10225   address_end = address_start + bl->length - 1;
10226
10227   ui_out_text (uiout, "\taddress range: ");
10228   fprintf_unfiltered (stb, "[%s, %s]",
10229                       print_core_address (bl->gdbarch, address_start),
10230                       print_core_address (bl->gdbarch, address_end));
10231   ui_out_field_stream (uiout, "addr", stb);
10232   ui_out_text (uiout, "\n");
10233
10234   do_cleanups (cleanup);
10235 }
10236
10237 /* Implement the "print_mention" breakpoint_ops method for
10238    ranged breakpoints.  */
10239
10240 static void
10241 print_mention_ranged_breakpoint (struct breakpoint *b)
10242 {
10243   struct bp_location *bl = b->loc;
10244   struct ui_out *uiout = current_uiout;
10245
10246   gdb_assert (bl);
10247   gdb_assert (b->type == bp_hardware_breakpoint);
10248
10249   if (ui_out_is_mi_like_p (uiout))
10250     return;
10251
10252   printf_filtered (_("Hardware assisted ranged breakpoint %d from %s to %s."),
10253                    b->number, paddress (bl->gdbarch, bl->address),
10254                    paddress (bl->gdbarch, bl->address + bl->length - 1));
10255 }
10256
10257 /* Implement the "print_recreate" breakpoint_ops method for
10258    ranged breakpoints.  */
10259
10260 static void
10261 print_recreate_ranged_breakpoint (struct breakpoint *b, struct ui_file *fp)
10262 {
10263   fprintf_unfiltered (fp, "break-range %s, %s", b->addr_string,
10264                       b->addr_string_range_end);
10265   print_recreate_thread (b, fp);
10266 }
10267
10268 /* The breakpoint_ops structure to be used in ranged breakpoints.  */
10269
10270 static struct breakpoint_ops ranged_breakpoint_ops;
10271
10272 /* Find the address where the end of the breakpoint range should be
10273    placed, given the SAL of the end of the range.  This is so that if
10274    the user provides a line number, the end of the range is set to the
10275    last instruction of the given line.  */
10276
10277 static CORE_ADDR
10278 find_breakpoint_range_end (struct symtab_and_line sal)
10279 {
10280   CORE_ADDR end;
10281
10282   /* If the user provided a PC value, use it.  Otherwise,
10283      find the address of the end of the given location.  */
10284   if (sal.explicit_pc)
10285     end = sal.pc;
10286   else
10287     {
10288       int ret;
10289       CORE_ADDR start;
10290
10291       ret = find_line_pc_range (sal, &start, &end);
10292       if (!ret)
10293         error (_("Could not find location of the end of the range."));
10294
10295       /* find_line_pc_range returns the start of the next line.  */
10296       end--;
10297     }
10298
10299   return end;
10300 }
10301
10302 /* Implement the "break-range" CLI command.  */
10303
10304 static void
10305 break_range_command (char *arg, int from_tty)
10306 {
10307   char *arg_start, *addr_string_start, *addr_string_end;
10308   struct linespec_result canonical_start, canonical_end;
10309   int bp_count, can_use_bp, length;
10310   CORE_ADDR end;
10311   struct breakpoint *b;
10312   struct symtab_and_line sal_start, sal_end;
10313   struct cleanup *cleanup_bkpt;
10314   struct linespec_sals *lsal_start, *lsal_end;
10315
10316   /* We don't support software ranged breakpoints.  */
10317   if (target_ranged_break_num_registers () < 0)
10318     error (_("This target does not support hardware ranged breakpoints."));
10319
10320   bp_count = hw_breakpoint_used_count ();
10321   bp_count += target_ranged_break_num_registers ();
10322   can_use_bp = target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
10323                                                    bp_count, 0);
10324   if (can_use_bp < 0)
10325     error (_("Hardware breakpoints used exceeds limit."));
10326
10327   arg = skip_spaces (arg);
10328   if (arg == NULL || arg[0] == '\0')
10329     error(_("No address range specified."));
10330
10331   init_linespec_result (&canonical_start);
10332
10333   arg_start = arg;
10334   parse_breakpoint_sals (&arg, &canonical_start);
10335
10336   cleanup_bkpt = make_cleanup_destroy_linespec_result (&canonical_start);
10337
10338   if (arg[0] != ',')
10339     error (_("Too few arguments."));
10340   else if (VEC_empty (linespec_sals, canonical_start.sals))
10341     error (_("Could not find location of the beginning of the range."));
10342
10343   lsal_start = VEC_index (linespec_sals, canonical_start.sals, 0);
10344
10345   if (VEC_length (linespec_sals, canonical_start.sals) > 1
10346       || lsal_start->sals.nelts != 1)
10347     error (_("Cannot create a ranged breakpoint with multiple locations."));
10348
10349   sal_start = lsal_start->sals.sals[0];
10350   addr_string_start = savestring (arg_start, arg - arg_start);
10351   make_cleanup (xfree, addr_string_start);
10352
10353   arg++;        /* Skip the comma.  */
10354   arg = skip_spaces (arg);
10355
10356   /* Parse the end location.  */
10357
10358   init_linespec_result (&canonical_end);
10359   arg_start = arg;
10360
10361   /* We call decode_line_full directly here instead of using
10362      parse_breakpoint_sals because we need to specify the start location's
10363      symtab and line as the default symtab and line for the end of the
10364      range.  This makes it possible to have ranges like "foo.c:27, +14",
10365      where +14 means 14 lines from the start location.  */
10366   decode_line_full (&arg, DECODE_LINE_FUNFIRSTLINE,
10367                     sal_start.symtab, sal_start.line,
10368                     &canonical_end, NULL, NULL);
10369
10370   make_cleanup_destroy_linespec_result (&canonical_end);
10371
10372   if (VEC_empty (linespec_sals, canonical_end.sals))
10373     error (_("Could not find location of the end of the range."));
10374
10375   lsal_end = VEC_index (linespec_sals, canonical_end.sals, 0);
10376   if (VEC_length (linespec_sals, canonical_end.sals) > 1
10377       || lsal_end->sals.nelts != 1)
10378     error (_("Cannot create a ranged breakpoint with multiple locations."));
10379
10380   sal_end = lsal_end->sals.sals[0];
10381   addr_string_end = savestring (arg_start, arg - arg_start);
10382   make_cleanup (xfree, addr_string_end);
10383
10384   end = find_breakpoint_range_end (sal_end);
10385   if (sal_start.pc > end)
10386     error (_("Invalid address range, end precedes start."));
10387
10388   length = end - sal_start.pc + 1;
10389   if (length < 0)
10390     /* Length overflowed.  */
10391     error (_("Address range too large."));
10392   else if (length == 1)
10393     {
10394       /* This range is simple enough to be handled by
10395          the `hbreak' command.  */
10396       hbreak_command (addr_string_start, 1);
10397
10398       do_cleanups (cleanup_bkpt);
10399
10400       return;
10401     }
10402
10403   /* Now set up the breakpoint.  */
10404   b = set_raw_breakpoint (get_current_arch (), sal_start,
10405                           bp_hardware_breakpoint, &ranged_breakpoint_ops);
10406   set_breakpoint_count (breakpoint_count + 1);
10407   b->number = breakpoint_count;
10408   b->disposition = disp_donttouch;
10409   b->addr_string = xstrdup (addr_string_start);
10410   b->addr_string_range_end = xstrdup (addr_string_end);
10411   b->loc->length = length;
10412
10413   do_cleanups (cleanup_bkpt);
10414
10415   mention (b);
10416   observer_notify_breakpoint_created (b);
10417   update_global_location_list (1);
10418 }
10419
10420 /*  Return non-zero if EXP is verified as constant.  Returned zero
10421     means EXP is variable.  Also the constant detection may fail for
10422     some constant expressions and in such case still falsely return
10423     zero.  */
10424
10425 static int
10426 watchpoint_exp_is_const (const struct expression *exp)
10427 {
10428   int i = exp->nelts;
10429
10430   while (i > 0)
10431     {
10432       int oplenp, argsp;
10433
10434       /* We are only interested in the descriptor of each element.  */
10435       operator_length (exp, i, &oplenp, &argsp);
10436       i -= oplenp;
10437
10438       switch (exp->elts[i].opcode)
10439         {
10440         case BINOP_ADD:
10441         case BINOP_SUB:
10442         case BINOP_MUL:
10443         case BINOP_DIV:
10444         case BINOP_REM:
10445         case BINOP_MOD:
10446         case BINOP_LSH:
10447         case BINOP_RSH:
10448         case BINOP_LOGICAL_AND:
10449         case BINOP_LOGICAL_OR:
10450         case BINOP_BITWISE_AND:
10451         case BINOP_BITWISE_IOR:
10452         case BINOP_BITWISE_XOR:
10453         case BINOP_EQUAL:
10454         case BINOP_NOTEQUAL:
10455         case BINOP_LESS:
10456         case BINOP_GTR:
10457         case BINOP_LEQ:
10458         case BINOP_GEQ:
10459         case BINOP_REPEAT:
10460         case BINOP_COMMA:
10461         case BINOP_EXP:
10462         case BINOP_MIN:
10463         case BINOP_MAX:
10464         case BINOP_INTDIV:
10465         case BINOP_CONCAT:
10466         case BINOP_IN:
10467         case BINOP_RANGE:
10468         case TERNOP_COND:
10469         case TERNOP_SLICE:
10470
10471         case OP_LONG:
10472         case OP_DOUBLE:
10473         case OP_DECFLOAT:
10474         case OP_LAST:
10475         case OP_COMPLEX:
10476         case OP_STRING:
10477         case OP_ARRAY:
10478         case OP_TYPE:
10479         case OP_TYPEOF:
10480         case OP_DECLTYPE:
10481         case OP_TYPEID:
10482         case OP_NAME:
10483         case OP_OBJC_NSSTRING:
10484
10485         case UNOP_NEG:
10486         case UNOP_LOGICAL_NOT:
10487         case UNOP_COMPLEMENT:
10488         case UNOP_ADDR:
10489         case UNOP_HIGH:
10490         case UNOP_CAST:
10491
10492         case UNOP_CAST_TYPE:
10493         case UNOP_REINTERPRET_CAST:
10494         case UNOP_DYNAMIC_CAST:
10495           /* Unary, binary and ternary operators: We have to check
10496              their operands.  If they are constant, then so is the
10497              result of that operation.  For instance, if A and B are
10498              determined to be constants, then so is "A + B".
10499
10500              UNOP_IND is one exception to the rule above, because the
10501              value of *ADDR is not necessarily a constant, even when
10502              ADDR is.  */
10503           break;
10504
10505         case OP_VAR_VALUE:
10506           /* Check whether the associated symbol is a constant.
10507
10508              We use SYMBOL_CLASS rather than TYPE_CONST because it's
10509              possible that a buggy compiler could mark a variable as
10510              constant even when it is not, and TYPE_CONST would return
10511              true in this case, while SYMBOL_CLASS wouldn't.
10512
10513              We also have to check for function symbols because they
10514              are always constant.  */
10515           {
10516             struct symbol *s = exp->elts[i + 2].symbol;
10517
10518             if (SYMBOL_CLASS (s) != LOC_BLOCK
10519                 && SYMBOL_CLASS (s) != LOC_CONST
10520                 && SYMBOL_CLASS (s) != LOC_CONST_BYTES)
10521               return 0;
10522             break;
10523           }
10524
10525         /* The default action is to return 0 because we are using
10526            the optimistic approach here: If we don't know something,
10527            then it is not a constant.  */
10528         default:
10529           return 0;
10530         }
10531     }
10532
10533   return 1;
10534 }
10535
10536 /* Implement the "dtor" breakpoint_ops method for watchpoints.  */
10537
10538 static void
10539 dtor_watchpoint (struct breakpoint *self)
10540 {
10541   struct watchpoint *w = (struct watchpoint *) self;
10542
10543   xfree (w->cond_exp);
10544   xfree (w->exp);
10545   xfree (w->exp_string);
10546   xfree (w->exp_string_reparse);
10547   value_free (w->val);
10548
10549   base_breakpoint_ops.dtor (self);
10550 }
10551
10552 /* Implement the "re_set" breakpoint_ops method for watchpoints.  */
10553
10554 static void
10555 re_set_watchpoint (struct breakpoint *b)
10556 {
10557   struct watchpoint *w = (struct watchpoint *) b;
10558
10559   /* Watchpoint can be either on expression using entirely global
10560      variables, or it can be on local variables.
10561
10562      Watchpoints of the first kind are never auto-deleted, and even
10563      persist across program restarts.  Since they can use variables
10564      from shared libraries, we need to reparse expression as libraries
10565      are loaded and unloaded.
10566
10567      Watchpoints on local variables can also change meaning as result
10568      of solib event.  For example, if a watchpoint uses both a local
10569      and a global variables in expression, it's a local watchpoint,
10570      but unloading of a shared library will make the expression
10571      invalid.  This is not a very common use case, but we still
10572      re-evaluate expression, to avoid surprises to the user.
10573
10574      Note that for local watchpoints, we re-evaluate it only if
10575      watchpoints frame id is still valid.  If it's not, it means the
10576      watchpoint is out of scope and will be deleted soon.  In fact,
10577      I'm not sure we'll ever be called in this case.
10578
10579      If a local watchpoint's frame id is still valid, then
10580      w->exp_valid_block is likewise valid, and we can safely use it.
10581
10582      Don't do anything about disabled watchpoints, since they will be
10583      reevaluated again when enabled.  */
10584   update_watchpoint (w, 1 /* reparse */);
10585 }
10586
10587 /* Implement the "insert" breakpoint_ops method for hardware watchpoints.  */
10588
10589 static int
10590 insert_watchpoint (struct bp_location *bl)
10591 {
10592   struct watchpoint *w = (struct watchpoint *) bl->owner;
10593   int length = w->exact ? 1 : bl->length;
10594
10595   return target_insert_watchpoint (bl->address, length, bl->watchpoint_type,
10596                                    w->cond_exp);
10597 }
10598
10599 /* Implement the "remove" breakpoint_ops method for hardware watchpoints.  */
10600
10601 static int
10602 remove_watchpoint (struct bp_location *bl)
10603 {
10604   struct watchpoint *w = (struct watchpoint *) bl->owner;
10605   int length = w->exact ? 1 : bl->length;
10606
10607   return target_remove_watchpoint (bl->address, length, bl->watchpoint_type,
10608                                    w->cond_exp);
10609 }
10610
10611 static int
10612 breakpoint_hit_watchpoint (const struct bp_location *bl,
10613                            struct address_space *aspace, CORE_ADDR bp_addr,
10614                            const struct target_waitstatus *ws)
10615 {
10616   struct breakpoint *b = bl->owner;
10617   struct watchpoint *w = (struct watchpoint *) b;
10618
10619   /* Continuable hardware watchpoints are treated as non-existent if the
10620      reason we stopped wasn't a hardware watchpoint (we didn't stop on
10621      some data address).  Otherwise gdb won't stop on a break instruction
10622      in the code (not from a breakpoint) when a hardware watchpoint has
10623      been defined.  Also skip watchpoints which we know did not trigger
10624      (did not match the data address).  */
10625   if (is_hardware_watchpoint (b)
10626       && w->watchpoint_triggered == watch_triggered_no)
10627     return 0;
10628
10629   return 1;
10630 }
10631
10632 static void
10633 check_status_watchpoint (bpstat bs)
10634 {
10635   gdb_assert (is_watchpoint (bs->breakpoint_at));
10636
10637   bpstat_check_watchpoint (bs);
10638 }
10639
10640 /* Implement the "resources_needed" breakpoint_ops method for
10641    hardware watchpoints.  */
10642
10643 static int
10644 resources_needed_watchpoint (const struct bp_location *bl)
10645 {
10646   struct watchpoint *w = (struct watchpoint *) bl->owner;
10647   int length = w->exact? 1 : bl->length;
10648
10649   return target_region_ok_for_hw_watchpoint (bl->address, length);
10650 }
10651
10652 /* Implement the "works_in_software_mode" breakpoint_ops method for
10653    hardware watchpoints.  */
10654
10655 static int
10656 works_in_software_mode_watchpoint (const struct breakpoint *b)
10657 {
10658   /* Read and access watchpoints only work with hardware support.  */
10659   return b->type == bp_watchpoint || b->type == bp_hardware_watchpoint;
10660 }
10661
10662 static enum print_stop_action
10663 print_it_watchpoint (bpstat bs)
10664 {
10665   struct cleanup *old_chain;
10666   struct breakpoint *b;
10667   struct ui_file *stb;
10668   enum print_stop_action result;
10669   struct watchpoint *w;
10670   struct ui_out *uiout = current_uiout;
10671
10672   gdb_assert (bs->bp_location_at != NULL);
10673
10674   b = bs->breakpoint_at;
10675   w = (struct watchpoint *) b;
10676
10677   stb = mem_fileopen ();
10678   old_chain = make_cleanup_ui_file_delete (stb);
10679
10680   switch (b->type)
10681     {
10682     case bp_watchpoint:
10683     case bp_hardware_watchpoint:
10684       annotate_watchpoint (b->number);
10685       if (ui_out_is_mi_like_p (uiout))
10686         ui_out_field_string
10687           (uiout, "reason",
10688            async_reason_lookup (EXEC_ASYNC_WATCHPOINT_TRIGGER));
10689       mention (b);
10690       make_cleanup_ui_out_tuple_begin_end (uiout, "value");
10691       ui_out_text (uiout, "\nOld value = ");
10692       watchpoint_value_print (bs->old_val, stb);
10693       ui_out_field_stream (uiout, "old", stb);
10694       ui_out_text (uiout, "\nNew value = ");
10695       watchpoint_value_print (w->val, stb);
10696       ui_out_field_stream (uiout, "new", stb);
10697       ui_out_text (uiout, "\n");
10698       /* More than one watchpoint may have been triggered.  */
10699       result = PRINT_UNKNOWN;
10700       break;
10701
10702     case bp_read_watchpoint:
10703       if (ui_out_is_mi_like_p (uiout))
10704         ui_out_field_string
10705           (uiout, "reason",
10706            async_reason_lookup (EXEC_ASYNC_READ_WATCHPOINT_TRIGGER));
10707       mention (b);
10708       make_cleanup_ui_out_tuple_begin_end (uiout, "value");
10709       ui_out_text (uiout, "\nValue = ");
10710       watchpoint_value_print (w->val, stb);
10711       ui_out_field_stream (uiout, "value", stb);
10712       ui_out_text (uiout, "\n");
10713       result = PRINT_UNKNOWN;
10714       break;
10715
10716     case bp_access_watchpoint:
10717       if (bs->old_val != NULL)
10718         {
10719           annotate_watchpoint (b->number);
10720           if (ui_out_is_mi_like_p (uiout))
10721             ui_out_field_string
10722               (uiout, "reason",
10723                async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
10724           mention (b);
10725           make_cleanup_ui_out_tuple_begin_end (uiout, "value");
10726           ui_out_text (uiout, "\nOld value = ");
10727           watchpoint_value_print (bs->old_val, stb);
10728           ui_out_field_stream (uiout, "old", stb);
10729           ui_out_text (uiout, "\nNew value = ");
10730         }
10731       else
10732         {
10733           mention (b);
10734           if (ui_out_is_mi_like_p (uiout))
10735             ui_out_field_string
10736               (uiout, "reason",
10737                async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
10738           make_cleanup_ui_out_tuple_begin_end (uiout, "value");
10739           ui_out_text (uiout, "\nValue = ");
10740         }
10741       watchpoint_value_print (w->val, stb);
10742       ui_out_field_stream (uiout, "new", stb);
10743       ui_out_text (uiout, "\n");
10744       result = PRINT_UNKNOWN;
10745       break;
10746     default:
10747       result = PRINT_UNKNOWN;
10748     }
10749
10750   do_cleanups (old_chain);
10751   return result;
10752 }
10753
10754 /* Implement the "print_mention" breakpoint_ops method for hardware
10755    watchpoints.  */
10756
10757 static void
10758 print_mention_watchpoint (struct breakpoint *b)
10759 {
10760   struct cleanup *ui_out_chain;
10761   struct watchpoint *w = (struct watchpoint *) b;
10762   struct ui_out *uiout = current_uiout;
10763
10764   switch (b->type)
10765     {
10766     case bp_watchpoint:
10767       ui_out_text (uiout, "Watchpoint ");
10768       ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "wpt");
10769       break;
10770     case bp_hardware_watchpoint:
10771       ui_out_text (uiout, "Hardware watchpoint ");
10772       ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "wpt");
10773       break;
10774     case bp_read_watchpoint:
10775       ui_out_text (uiout, "Hardware read watchpoint ");
10776       ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "hw-rwpt");
10777       break;
10778     case bp_access_watchpoint:
10779       ui_out_text (uiout, "Hardware access (read/write) watchpoint ");
10780       ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "hw-awpt");
10781       break;
10782     default:
10783       internal_error (__FILE__, __LINE__,
10784                       _("Invalid hardware watchpoint type."));
10785     }
10786
10787   ui_out_field_int (uiout, "number", b->number);
10788   ui_out_text (uiout, ": ");
10789   ui_out_field_string (uiout, "exp", w->exp_string);
10790   do_cleanups (ui_out_chain);
10791 }
10792
10793 /* Implement the "print_recreate" breakpoint_ops method for
10794    watchpoints.  */
10795
10796 static void
10797 print_recreate_watchpoint (struct breakpoint *b, struct ui_file *fp)
10798 {
10799   struct watchpoint *w = (struct watchpoint *) b;
10800
10801   switch (b->type)
10802     {
10803     case bp_watchpoint:
10804     case bp_hardware_watchpoint:
10805       fprintf_unfiltered (fp, "watch");
10806       break;
10807     case bp_read_watchpoint:
10808       fprintf_unfiltered (fp, "rwatch");
10809       break;
10810     case bp_access_watchpoint:
10811       fprintf_unfiltered (fp, "awatch");
10812       break;
10813     default:
10814       internal_error (__FILE__, __LINE__,
10815                       _("Invalid watchpoint type."));
10816     }
10817
10818   fprintf_unfiltered (fp, " %s", w->exp_string);
10819   print_recreate_thread (b, fp);
10820 }
10821
10822 /* Implement the "explains_signal" breakpoint_ops method for
10823    watchpoints.  */
10824
10825 static int
10826 explains_signal_watchpoint (struct breakpoint *b, enum gdb_signal sig)
10827 {
10828   /* A software watchpoint cannot cause a signal other than
10829      GDB_SIGNAL_TRAP.  */
10830   if (b->type == bp_watchpoint && sig != GDB_SIGNAL_TRAP)
10831     return 0;
10832
10833   return 1;
10834 }
10835
10836 /* The breakpoint_ops structure to be used in hardware watchpoints.  */
10837
10838 static struct breakpoint_ops watchpoint_breakpoint_ops;
10839
10840 /* Implement the "insert" breakpoint_ops method for
10841    masked hardware watchpoints.  */
10842
10843 static int
10844 insert_masked_watchpoint (struct bp_location *bl)
10845 {
10846   struct watchpoint *w = (struct watchpoint *) bl->owner;
10847
10848   return target_insert_mask_watchpoint (bl->address, w->hw_wp_mask,
10849                                         bl->watchpoint_type);
10850 }
10851
10852 /* Implement the "remove" breakpoint_ops method for
10853    masked hardware watchpoints.  */
10854
10855 static int
10856 remove_masked_watchpoint (struct bp_location *bl)
10857 {
10858   struct watchpoint *w = (struct watchpoint *) bl->owner;
10859
10860   return target_remove_mask_watchpoint (bl->address, w->hw_wp_mask,
10861                                         bl->watchpoint_type);
10862 }
10863
10864 /* Implement the "resources_needed" breakpoint_ops method for
10865    masked hardware watchpoints.  */
10866
10867 static int
10868 resources_needed_masked_watchpoint (const struct bp_location *bl)
10869 {
10870   struct watchpoint *w = (struct watchpoint *) bl->owner;
10871
10872   return target_masked_watch_num_registers (bl->address, w->hw_wp_mask);
10873 }
10874
10875 /* Implement the "works_in_software_mode" breakpoint_ops method for
10876    masked hardware watchpoints.  */
10877
10878 static int
10879 works_in_software_mode_masked_watchpoint (const struct breakpoint *b)
10880 {
10881   return 0;
10882 }
10883
10884 /* Implement the "print_it" breakpoint_ops method for
10885    masked hardware watchpoints.  */
10886
10887 static enum print_stop_action
10888 print_it_masked_watchpoint (bpstat bs)
10889 {
10890   struct breakpoint *b = bs->breakpoint_at;
10891   struct ui_out *uiout = current_uiout;
10892
10893   /* Masked watchpoints have only one location.  */
10894   gdb_assert (b->loc && b->loc->next == NULL);
10895
10896   switch (b->type)
10897     {
10898     case bp_hardware_watchpoint:
10899       annotate_watchpoint (b->number);
10900       if (ui_out_is_mi_like_p (uiout))
10901         ui_out_field_string
10902           (uiout, "reason",
10903            async_reason_lookup (EXEC_ASYNC_WATCHPOINT_TRIGGER));
10904       break;
10905
10906     case bp_read_watchpoint:
10907       if (ui_out_is_mi_like_p (uiout))
10908         ui_out_field_string
10909           (uiout, "reason",
10910            async_reason_lookup (EXEC_ASYNC_READ_WATCHPOINT_TRIGGER));
10911       break;
10912
10913     case bp_access_watchpoint:
10914       if (ui_out_is_mi_like_p (uiout))
10915         ui_out_field_string
10916           (uiout, "reason",
10917            async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
10918       break;
10919     default:
10920       internal_error (__FILE__, __LINE__,
10921                       _("Invalid hardware watchpoint type."));
10922     }
10923
10924   mention (b);
10925   ui_out_text (uiout, _("\n\
10926 Check the underlying instruction at PC for the memory\n\
10927 address and value which triggered this watchpoint.\n"));
10928   ui_out_text (uiout, "\n");
10929
10930   /* More than one watchpoint may have been triggered.  */
10931   return PRINT_UNKNOWN;
10932 }
10933
10934 /* Implement the "print_one_detail" breakpoint_ops method for
10935    masked hardware watchpoints.  */
10936
10937 static void
10938 print_one_detail_masked_watchpoint (const struct breakpoint *b,
10939                                     struct ui_out *uiout)
10940 {
10941   struct watchpoint *w = (struct watchpoint *) b;
10942
10943   /* Masked watchpoints have only one location.  */
10944   gdb_assert (b->loc && b->loc->next == NULL);
10945
10946   ui_out_text (uiout, "\tmask ");
10947   ui_out_field_core_addr (uiout, "mask", b->loc->gdbarch, w->hw_wp_mask);
10948   ui_out_text (uiout, "\n");
10949 }
10950
10951 /* Implement the "print_mention" breakpoint_ops method for
10952    masked hardware watchpoints.  */
10953
10954 static void
10955 print_mention_masked_watchpoint (struct breakpoint *b)
10956 {
10957   struct watchpoint *w = (struct watchpoint *) b;
10958   struct ui_out *uiout = current_uiout;
10959   struct cleanup *ui_out_chain;
10960
10961   switch (b->type)
10962     {
10963     case bp_hardware_watchpoint:
10964       ui_out_text (uiout, "Masked hardware watchpoint ");
10965       ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "wpt");
10966       break;
10967     case bp_read_watchpoint:
10968       ui_out_text (uiout, "Masked hardware read watchpoint ");
10969       ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "hw-rwpt");
10970       break;
10971     case bp_access_watchpoint:
10972       ui_out_text (uiout, "Masked hardware access (read/write) watchpoint ");
10973       ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "hw-awpt");
10974       break;
10975     default:
10976       internal_error (__FILE__, __LINE__,
10977                       _("Invalid hardware watchpoint type."));
10978     }
10979
10980   ui_out_field_int (uiout, "number", b->number);
10981   ui_out_text (uiout, ": ");
10982   ui_out_field_string (uiout, "exp", w->exp_string);
10983   do_cleanups (ui_out_chain);
10984 }
10985
10986 /* Implement the "print_recreate" breakpoint_ops method for
10987    masked hardware watchpoints.  */
10988
10989 static void
10990 print_recreate_masked_watchpoint (struct breakpoint *b, struct ui_file *fp)
10991 {
10992   struct watchpoint *w = (struct watchpoint *) b;
10993   char tmp[40];
10994
10995   switch (b->type)
10996     {
10997     case bp_hardware_watchpoint:
10998       fprintf_unfiltered (fp, "watch");
10999       break;
11000     case bp_read_watchpoint:
11001       fprintf_unfiltered (fp, "rwatch");
11002       break;
11003     case bp_access_watchpoint:
11004       fprintf_unfiltered (fp, "awatch");
11005       break;
11006     default:
11007       internal_error (__FILE__, __LINE__,
11008                       _("Invalid hardware watchpoint type."));
11009     }
11010
11011   sprintf_vma (tmp, w->hw_wp_mask);
11012   fprintf_unfiltered (fp, " %s mask 0x%s", w->exp_string, tmp);
11013   print_recreate_thread (b, fp);
11014 }
11015
11016 /* The breakpoint_ops structure to be used in masked hardware watchpoints.  */
11017
11018 static struct breakpoint_ops masked_watchpoint_breakpoint_ops;
11019
11020 /* Tell whether the given watchpoint is a masked hardware watchpoint.  */
11021
11022 static int
11023 is_masked_watchpoint (const struct breakpoint *b)
11024 {
11025   return b->ops == &masked_watchpoint_breakpoint_ops;
11026 }
11027
11028 /* accessflag:  hw_write:  watch write, 
11029                 hw_read:   watch read, 
11030                 hw_access: watch access (read or write) */
11031 static void
11032 watch_command_1 (const char *arg, int accessflag, int from_tty,
11033                  int just_location, int internal)
11034 {
11035   volatile struct gdb_exception e;
11036   struct breakpoint *b, *scope_breakpoint = NULL;
11037   struct expression *exp;
11038   const struct block *exp_valid_block = NULL, *cond_exp_valid_block = NULL;
11039   struct value *val, *mark, *result;
11040   struct frame_info *frame;
11041   const char *exp_start = NULL;
11042   const char *exp_end = NULL;
11043   const char *tok, *end_tok;
11044   int toklen = -1;
11045   const char *cond_start = NULL;
11046   const char *cond_end = NULL;
11047   enum bptype bp_type;
11048   int thread = -1;
11049   int pc = 0;
11050   /* Flag to indicate whether we are going to use masks for
11051      the hardware watchpoint.  */
11052   int use_mask = 0;
11053   CORE_ADDR mask = 0;
11054   struct watchpoint *w;
11055   char *expression;
11056   struct cleanup *back_to;
11057
11058   /* Make sure that we actually have parameters to parse.  */
11059   if (arg != NULL && arg[0] != '\0')
11060     {
11061       const char *value_start;
11062
11063       exp_end = arg + strlen (arg);
11064
11065       /* Look for "parameter value" pairs at the end
11066          of the arguments string.  */
11067       for (tok = exp_end - 1; tok > arg; tok--)
11068         {
11069           /* Skip whitespace at the end of the argument list.  */
11070           while (tok > arg && (*tok == ' ' || *tok == '\t'))
11071             tok--;
11072
11073           /* Find the beginning of the last token.
11074              This is the value of the parameter.  */
11075           while (tok > arg && (*tok != ' ' && *tok != '\t'))
11076             tok--;
11077           value_start = tok + 1;
11078
11079           /* Skip whitespace.  */
11080           while (tok > arg && (*tok == ' ' || *tok == '\t'))
11081             tok--;
11082
11083           end_tok = tok;
11084
11085           /* Find the beginning of the second to last token.
11086              This is the parameter itself.  */
11087           while (tok > arg && (*tok != ' ' && *tok != '\t'))
11088             tok--;
11089           tok++;
11090           toklen = end_tok - tok + 1;
11091
11092           if (toklen == 6 && !strncmp (tok, "thread", 6))
11093             {
11094               /* At this point we've found a "thread" token, which means
11095                  the user is trying to set a watchpoint that triggers
11096                  only in a specific thread.  */
11097               char *endp;
11098
11099               if (thread != -1)
11100                 error(_("You can specify only one thread."));
11101
11102               /* Extract the thread ID from the next token.  */
11103               thread = strtol (value_start, &endp, 0);
11104
11105               /* Check if the user provided a valid numeric value for the
11106                  thread ID.  */
11107               if (*endp != ' ' && *endp != '\t' && *endp != '\0')
11108                 error (_("Invalid thread ID specification %s."), value_start);
11109
11110               /* Check if the thread actually exists.  */
11111               if (!valid_thread_id (thread))
11112                 invalid_thread_id_error (thread);
11113             }
11114           else if (toklen == 4 && !strncmp (tok, "mask", 4))
11115             {
11116               /* We've found a "mask" token, which means the user wants to
11117                  create a hardware watchpoint that is going to have the mask
11118                  facility.  */
11119               struct value *mask_value, *mark;
11120
11121               if (use_mask)
11122                 error(_("You can specify only one mask."));
11123
11124               use_mask = just_location = 1;
11125
11126               mark = value_mark ();
11127               mask_value = parse_to_comma_and_eval (&value_start);
11128               mask = value_as_address (mask_value);
11129               value_free_to_mark (mark);
11130             }
11131           else
11132             /* We didn't recognize what we found.  We should stop here.  */
11133             break;
11134
11135           /* Truncate the string and get rid of the "parameter value" pair before
11136              the arguments string is parsed by the parse_exp_1 function.  */
11137           exp_end = tok;
11138         }
11139     }
11140   else
11141     exp_end = arg;
11142
11143   /* Parse the rest of the arguments.  From here on out, everything
11144      is in terms of a newly allocated string instead of the original
11145      ARG.  */
11146   innermost_block = NULL;
11147   expression = savestring (arg, exp_end - arg);
11148   back_to = make_cleanup (xfree, expression);
11149   exp_start = arg = expression;
11150   exp = parse_exp_1 (&arg, 0, 0, 0);
11151   exp_end = arg;
11152   /* Remove trailing whitespace from the expression before saving it.
11153      This makes the eventual display of the expression string a bit
11154      prettier.  */
11155   while (exp_end > exp_start && (exp_end[-1] == ' ' || exp_end[-1] == '\t'))
11156     --exp_end;
11157
11158   /* Checking if the expression is not constant.  */
11159   if (watchpoint_exp_is_const (exp))
11160     {
11161       int len;
11162
11163       len = exp_end - exp_start;
11164       while (len > 0 && isspace (exp_start[len - 1]))
11165         len--;
11166       error (_("Cannot watch constant value `%.*s'."), len, exp_start);
11167     }
11168
11169   exp_valid_block = innermost_block;
11170   mark = value_mark ();
11171   fetch_subexp_value (exp, &pc, &val, &result, NULL, just_location);
11172
11173   if (just_location)
11174     {
11175       int ret;
11176
11177       exp_valid_block = NULL;
11178       val = value_addr (result);
11179       release_value (val);
11180       value_free_to_mark (mark);
11181
11182       if (use_mask)
11183         {
11184           ret = target_masked_watch_num_registers (value_as_address (val),
11185                                                    mask);
11186           if (ret == -1)
11187             error (_("This target does not support masked watchpoints."));
11188           else if (ret == -2)
11189             error (_("Invalid mask or memory region."));
11190         }
11191     }
11192   else if (val != NULL)
11193     release_value (val);
11194
11195   tok = skip_spaces_const (arg);
11196   end_tok = skip_to_space_const (tok);
11197
11198   toklen = end_tok - tok;
11199   if (toklen >= 1 && strncmp (tok, "if", toklen) == 0)
11200     {
11201       struct expression *cond;
11202
11203       innermost_block = NULL;
11204       tok = cond_start = end_tok + 1;
11205       cond = parse_exp_1 (&tok, 0, 0, 0);
11206
11207       /* The watchpoint expression may not be local, but the condition
11208          may still be.  E.g.: `watch global if local > 0'.  */
11209       cond_exp_valid_block = innermost_block;
11210
11211       xfree (cond);
11212       cond_end = tok;
11213     }
11214   if (*tok)
11215     error (_("Junk at end of command."));
11216
11217   frame = block_innermost_frame (exp_valid_block);
11218
11219   /* If the expression is "local", then set up a "watchpoint scope"
11220      breakpoint at the point where we've left the scope of the watchpoint
11221      expression.  Create the scope breakpoint before the watchpoint, so
11222      that we will encounter it first in bpstat_stop_status.  */
11223   if (exp_valid_block && frame)
11224     {
11225       if (frame_id_p (frame_unwind_caller_id (frame)))
11226         {
11227           scope_breakpoint
11228             = create_internal_breakpoint (frame_unwind_caller_arch (frame),
11229                                           frame_unwind_caller_pc (frame),
11230                                           bp_watchpoint_scope,
11231                                           &momentary_breakpoint_ops);
11232
11233           scope_breakpoint->enable_state = bp_enabled;
11234
11235           /* Automatically delete the breakpoint when it hits.  */
11236           scope_breakpoint->disposition = disp_del;
11237
11238           /* Only break in the proper frame (help with recursion).  */
11239           scope_breakpoint->frame_id = frame_unwind_caller_id (frame);
11240
11241           /* Set the address at which we will stop.  */
11242           scope_breakpoint->loc->gdbarch
11243             = frame_unwind_caller_arch (frame);
11244           scope_breakpoint->loc->requested_address
11245             = frame_unwind_caller_pc (frame);
11246           scope_breakpoint->loc->address
11247             = adjust_breakpoint_address (scope_breakpoint->loc->gdbarch,
11248                                          scope_breakpoint->loc->requested_address,
11249                                          scope_breakpoint->type);
11250         }
11251     }
11252
11253   /* Now set up the breakpoint.  We create all watchpoints as hardware
11254      watchpoints here even if hardware watchpoints are turned off, a call
11255      to update_watchpoint later in this function will cause the type to
11256      drop back to bp_watchpoint (software watchpoint) if required.  */
11257
11258   if (accessflag == hw_read)
11259     bp_type = bp_read_watchpoint;
11260   else if (accessflag == hw_access)
11261     bp_type = bp_access_watchpoint;
11262   else
11263     bp_type = bp_hardware_watchpoint;
11264
11265   w = XCNEW (struct watchpoint);
11266   b = &w->base;
11267   if (use_mask)
11268     init_raw_breakpoint_without_location (b, NULL, bp_type,
11269                                           &masked_watchpoint_breakpoint_ops);
11270   else
11271     init_raw_breakpoint_without_location (b, NULL, bp_type,
11272                                           &watchpoint_breakpoint_ops);
11273   b->thread = thread;
11274   b->disposition = disp_donttouch;
11275   b->pspace = current_program_space;
11276   w->exp = exp;
11277   w->exp_valid_block = exp_valid_block;
11278   w->cond_exp_valid_block = cond_exp_valid_block;
11279   if (just_location)
11280     {
11281       struct type *t = value_type (val);
11282       CORE_ADDR addr = value_as_address (val);
11283       char *name;
11284
11285       t = check_typedef (TYPE_TARGET_TYPE (check_typedef (t)));
11286       name = type_to_string (t);
11287
11288       w->exp_string_reparse = xstrprintf ("* (%s *) %s", name,
11289                                           core_addr_to_string (addr));
11290       xfree (name);
11291
11292       w->exp_string = xstrprintf ("-location %.*s",
11293                                   (int) (exp_end - exp_start), exp_start);
11294
11295       /* The above expression is in C.  */
11296       b->language = language_c;
11297     }
11298   else
11299     w->exp_string = savestring (exp_start, exp_end - exp_start);
11300
11301   if (use_mask)
11302     {
11303       w->hw_wp_mask = mask;
11304     }
11305   else
11306     {
11307       w->val = val;
11308       w->val_valid = 1;
11309     }
11310
11311   if (cond_start)
11312     b->cond_string = savestring (cond_start, cond_end - cond_start);
11313   else
11314     b->cond_string = 0;
11315
11316   if (frame)
11317     {
11318       w->watchpoint_frame = get_frame_id (frame);
11319       w->watchpoint_thread = inferior_ptid;
11320     }
11321   else
11322     {
11323       w->watchpoint_frame = null_frame_id;
11324       w->watchpoint_thread = null_ptid;
11325     }
11326
11327   if (scope_breakpoint != NULL)
11328     {
11329       /* The scope breakpoint is related to the watchpoint.  We will
11330          need to act on them together.  */
11331       b->related_breakpoint = scope_breakpoint;
11332       scope_breakpoint->related_breakpoint = b;
11333     }
11334
11335   if (!just_location)
11336     value_free_to_mark (mark);
11337
11338   TRY_CATCH (e, RETURN_MASK_ALL)
11339     {
11340       /* Finally update the new watchpoint.  This creates the locations
11341          that should be inserted.  */
11342       update_watchpoint (w, 1);
11343     }
11344   if (e.reason < 0)
11345     {
11346       delete_breakpoint (b);
11347       throw_exception (e);
11348     }
11349
11350   install_breakpoint (internal, b, 1);
11351   do_cleanups (back_to);
11352 }
11353
11354 /* Return count of debug registers needed to watch the given expression.
11355    If the watchpoint cannot be handled in hardware return zero.  */
11356
11357 static int
11358 can_use_hardware_watchpoint (struct value *v)
11359 {
11360   int found_memory_cnt = 0;
11361   struct value *head = v;
11362
11363   /* Did the user specifically forbid us to use hardware watchpoints? */
11364   if (!can_use_hw_watchpoints)
11365     return 0;
11366
11367   /* Make sure that the value of the expression depends only upon
11368      memory contents, and values computed from them within GDB.  If we
11369      find any register references or function calls, we can't use a
11370      hardware watchpoint.
11371
11372      The idea here is that evaluating an expression generates a series
11373      of values, one holding the value of every subexpression.  (The
11374      expression a*b+c has five subexpressions: a, b, a*b, c, and
11375      a*b+c.)  GDB's values hold almost enough information to establish
11376      the criteria given above --- they identify memory lvalues,
11377      register lvalues, computed values, etcetera.  So we can evaluate
11378      the expression, and then scan the chain of values that leaves
11379      behind to decide whether we can detect any possible change to the
11380      expression's final value using only hardware watchpoints.
11381
11382      However, I don't think that the values returned by inferior
11383      function calls are special in any way.  So this function may not
11384      notice that an expression involving an inferior function call
11385      can't be watched with hardware watchpoints.  FIXME.  */
11386   for (; v; v = value_next (v))
11387     {
11388       if (VALUE_LVAL (v) == lval_memory)
11389         {
11390           if (v != head && value_lazy (v))
11391             /* A lazy memory lvalue in the chain is one that GDB never
11392                needed to fetch; we either just used its address (e.g.,
11393                `a' in `a.b') or we never needed it at all (e.g., `a'
11394                in `a,b').  This doesn't apply to HEAD; if that is
11395                lazy then it was not readable, but watch it anyway.  */
11396             ;
11397           else
11398             {
11399               /* Ahh, memory we actually used!  Check if we can cover
11400                  it with hardware watchpoints.  */
11401               struct type *vtype = check_typedef (value_type (v));
11402
11403               /* We only watch structs and arrays if user asked for it
11404                  explicitly, never if they just happen to appear in a
11405                  middle of some value chain.  */
11406               if (v == head
11407                   || (TYPE_CODE (vtype) != TYPE_CODE_STRUCT
11408                       && TYPE_CODE (vtype) != TYPE_CODE_ARRAY))
11409                 {
11410                   CORE_ADDR vaddr = value_address (v);
11411                   int len;
11412                   int num_regs;
11413
11414                   len = (target_exact_watchpoints
11415                          && is_scalar_type_recursive (vtype))?
11416                     1 : TYPE_LENGTH (value_type (v));
11417
11418                   num_regs = target_region_ok_for_hw_watchpoint (vaddr, len);
11419                   if (!num_regs)
11420                     return 0;
11421                   else
11422                     found_memory_cnt += num_regs;
11423                 }
11424             }
11425         }
11426       else if (VALUE_LVAL (v) != not_lval
11427                && deprecated_value_modifiable (v) == 0)
11428         return 0;       /* These are values from the history (e.g., $1).  */
11429       else if (VALUE_LVAL (v) == lval_register)
11430         return 0;       /* Cannot watch a register with a HW watchpoint.  */
11431     }
11432
11433   /* The expression itself looks suitable for using a hardware
11434      watchpoint, but give the target machine a chance to reject it.  */
11435   return found_memory_cnt;
11436 }
11437
11438 void
11439 watch_command_wrapper (char *arg, int from_tty, int internal)
11440 {
11441   watch_command_1 (arg, hw_write, from_tty, 0, internal);
11442 }
11443
11444 /* A helper function that looks for the "-location" argument and then
11445    calls watch_command_1.  */
11446
11447 static void
11448 watch_maybe_just_location (char *arg, int accessflag, int from_tty)
11449 {
11450   int just_location = 0;
11451
11452   if (arg
11453       && (check_for_argument (&arg, "-location", sizeof ("-location") - 1)
11454           || check_for_argument (&arg, "-l", sizeof ("-l") - 1)))
11455     {
11456       arg = skip_spaces (arg);
11457       just_location = 1;
11458     }
11459
11460   watch_command_1 (arg, accessflag, from_tty, just_location, 0);
11461 }
11462
11463 static void
11464 watch_command (char *arg, int from_tty)
11465 {
11466   watch_maybe_just_location (arg, hw_write, from_tty);
11467 }
11468
11469 void
11470 rwatch_command_wrapper (char *arg, int from_tty, int internal)
11471 {
11472   watch_command_1 (arg, hw_read, from_tty, 0, internal);
11473 }
11474
11475 static void
11476 rwatch_command (char *arg, int from_tty)
11477 {
11478   watch_maybe_just_location (arg, hw_read, from_tty);
11479 }
11480
11481 void
11482 awatch_command_wrapper (char *arg, int from_tty, int internal)
11483 {
11484   watch_command_1 (arg, hw_access, from_tty, 0, internal);
11485 }
11486
11487 static void
11488 awatch_command (char *arg, int from_tty)
11489 {
11490   watch_maybe_just_location (arg, hw_access, from_tty);
11491 }
11492 \f
11493
11494 /* Helper routines for the until_command routine in infcmd.c.  Here
11495    because it uses the mechanisms of breakpoints.  */
11496
11497 struct until_break_command_continuation_args
11498 {
11499   struct breakpoint *breakpoint;
11500   struct breakpoint *breakpoint2;
11501   int thread_num;
11502 };
11503
11504 /* This function is called by fetch_inferior_event via the
11505    cmd_continuation pointer, to complete the until command.  It takes
11506    care of cleaning up the temporary breakpoints set up by the until
11507    command.  */
11508 static void
11509 until_break_command_continuation (void *arg, int err)
11510 {
11511   struct until_break_command_continuation_args *a = arg;
11512
11513   delete_breakpoint (a->breakpoint);
11514   if (a->breakpoint2)
11515     delete_breakpoint (a->breakpoint2);
11516   delete_longjmp_breakpoint (a->thread_num);
11517 }
11518
11519 void
11520 until_break_command (char *arg, int from_tty, int anywhere)
11521 {
11522   struct symtabs_and_lines sals;
11523   struct symtab_and_line sal;
11524   struct frame_info *frame;
11525   struct gdbarch *frame_gdbarch;
11526   struct frame_id stack_frame_id;
11527   struct frame_id caller_frame_id;
11528   struct breakpoint *breakpoint;
11529   struct breakpoint *breakpoint2 = NULL;
11530   struct cleanup *old_chain;
11531   int thread;
11532   struct thread_info *tp;
11533
11534   clear_proceed_status ();
11535
11536   /* Set a breakpoint where the user wants it and at return from
11537      this function.  */
11538
11539   if (last_displayed_sal_is_valid ())
11540     sals = decode_line_1 (&arg, DECODE_LINE_FUNFIRSTLINE,
11541                           get_last_displayed_symtab (),
11542                           get_last_displayed_line ());
11543   else
11544     sals = decode_line_1 (&arg, DECODE_LINE_FUNFIRSTLINE,
11545                           (struct symtab *) NULL, 0);
11546
11547   if (sals.nelts != 1)
11548     error (_("Couldn't get information on specified line."));
11549
11550   sal = sals.sals[0];
11551   xfree (sals.sals);    /* malloc'd, so freed.  */
11552
11553   if (*arg)
11554     error (_("Junk at end of arguments."));
11555
11556   resolve_sal_pc (&sal);
11557
11558   tp = inferior_thread ();
11559   thread = tp->num;
11560
11561   old_chain = make_cleanup (null_cleanup, NULL);
11562
11563   /* Note linespec handling above invalidates the frame chain.
11564      Installing a breakpoint also invalidates the frame chain (as it
11565      may need to switch threads), so do any frame handling before
11566      that.  */
11567
11568   frame = get_selected_frame (NULL);
11569   frame_gdbarch = get_frame_arch (frame);
11570   stack_frame_id = get_stack_frame_id (frame);
11571   caller_frame_id = frame_unwind_caller_id (frame);
11572
11573   /* Keep within the current frame, or in frames called by the current
11574      one.  */
11575
11576   if (frame_id_p (caller_frame_id))
11577     {
11578       struct symtab_and_line sal2;
11579
11580       sal2 = find_pc_line (frame_unwind_caller_pc (frame), 0);
11581       sal2.pc = frame_unwind_caller_pc (frame);
11582       breakpoint2 = set_momentary_breakpoint (frame_unwind_caller_arch (frame),
11583                                               sal2,
11584                                               caller_frame_id,
11585                                               bp_until);
11586       make_cleanup_delete_breakpoint (breakpoint2);
11587
11588       set_longjmp_breakpoint (tp, caller_frame_id);
11589       make_cleanup (delete_longjmp_breakpoint_cleanup, &thread);
11590     }
11591
11592   /* set_momentary_breakpoint could invalidate FRAME.  */
11593   frame = NULL;
11594
11595   if (anywhere)
11596     /* If the user told us to continue until a specified location,
11597        we don't specify a frame at which we need to stop.  */
11598     breakpoint = set_momentary_breakpoint (frame_gdbarch, sal,
11599                                            null_frame_id, bp_until);
11600   else
11601     /* Otherwise, specify the selected frame, because we want to stop
11602        only at the very same frame.  */
11603     breakpoint = set_momentary_breakpoint (frame_gdbarch, sal,
11604                                            stack_frame_id, bp_until);
11605   make_cleanup_delete_breakpoint (breakpoint);
11606
11607   proceed (-1, GDB_SIGNAL_DEFAULT, 0);
11608
11609   /* If we are running asynchronously, and proceed call above has
11610      actually managed to start the target, arrange for breakpoints to
11611      be deleted when the target stops.  Otherwise, we're already
11612      stopped and delete breakpoints via cleanup chain.  */
11613
11614   if (target_can_async_p () && is_running (inferior_ptid))
11615     {
11616       struct until_break_command_continuation_args *args;
11617       args = xmalloc (sizeof (*args));
11618
11619       args->breakpoint = breakpoint;
11620       args->breakpoint2 = breakpoint2;
11621       args->thread_num = thread;
11622
11623       discard_cleanups (old_chain);
11624       add_continuation (inferior_thread (),
11625                         until_break_command_continuation, args,
11626                         xfree);
11627     }
11628   else
11629     do_cleanups (old_chain);
11630 }
11631
11632 /* This function attempts to parse an optional "if <cond>" clause
11633    from the arg string.  If one is not found, it returns NULL.
11634
11635    Else, it returns a pointer to the condition string.  (It does not
11636    attempt to evaluate the string against a particular block.)  And,
11637    it updates arg to point to the first character following the parsed
11638    if clause in the arg string.  */
11639
11640 char *
11641 ep_parse_optional_if_clause (char **arg)
11642 {
11643   char *cond_string;
11644
11645   if (((*arg)[0] != 'i') || ((*arg)[1] != 'f') || !isspace ((*arg)[2]))
11646     return NULL;
11647
11648   /* Skip the "if" keyword.  */
11649   (*arg) += 2;
11650
11651   /* Skip any extra leading whitespace, and record the start of the
11652      condition string.  */
11653   *arg = skip_spaces (*arg);
11654   cond_string = *arg;
11655
11656   /* Assume that the condition occupies the remainder of the arg
11657      string.  */
11658   (*arg) += strlen (cond_string);
11659
11660   return cond_string;
11661 }
11662
11663 /* Commands to deal with catching events, such as signals, exceptions,
11664    process start/exit, etc.  */
11665
11666 typedef enum
11667 {
11668   catch_fork_temporary, catch_vfork_temporary,
11669   catch_fork_permanent, catch_vfork_permanent
11670 }
11671 catch_fork_kind;
11672
11673 static void
11674 catch_fork_command_1 (char *arg, int from_tty, 
11675                       struct cmd_list_element *command)
11676 {
11677   struct gdbarch *gdbarch = get_current_arch ();
11678   char *cond_string = NULL;
11679   catch_fork_kind fork_kind;
11680   int tempflag;
11681
11682   fork_kind = (catch_fork_kind) (uintptr_t) get_cmd_context (command);
11683   tempflag = (fork_kind == catch_fork_temporary
11684               || fork_kind == catch_vfork_temporary);
11685
11686   if (!arg)
11687     arg = "";
11688   arg = skip_spaces (arg);
11689
11690   /* The allowed syntax is:
11691      catch [v]fork
11692      catch [v]fork if <cond>
11693
11694      First, check if there's an if clause.  */
11695   cond_string = ep_parse_optional_if_clause (&arg);
11696
11697   if ((*arg != '\0') && !isspace (*arg))
11698     error (_("Junk at end of arguments."));
11699
11700   /* If this target supports it, create a fork or vfork catchpoint
11701      and enable reporting of such events.  */
11702   switch (fork_kind)
11703     {
11704     case catch_fork_temporary:
11705     case catch_fork_permanent:
11706       create_fork_vfork_event_catchpoint (gdbarch, tempflag, cond_string,
11707                                           &catch_fork_breakpoint_ops);
11708       break;
11709     case catch_vfork_temporary:
11710     case catch_vfork_permanent:
11711       create_fork_vfork_event_catchpoint (gdbarch, tempflag, cond_string,
11712                                           &catch_vfork_breakpoint_ops);
11713       break;
11714     default:
11715       error (_("unsupported or unknown fork kind; cannot catch it"));
11716       break;
11717     }
11718 }
11719
11720 static void
11721 catch_exec_command_1 (char *arg, int from_tty, 
11722                       struct cmd_list_element *command)
11723 {
11724   struct exec_catchpoint *c;
11725   struct gdbarch *gdbarch = get_current_arch ();
11726   int tempflag;
11727   char *cond_string = NULL;
11728
11729   tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
11730
11731   if (!arg)
11732     arg = "";
11733   arg = skip_spaces (arg);
11734
11735   /* The allowed syntax is:
11736      catch exec
11737      catch exec if <cond>
11738
11739      First, check if there's an if clause.  */
11740   cond_string = ep_parse_optional_if_clause (&arg);
11741
11742   if ((*arg != '\0') && !isspace (*arg))
11743     error (_("Junk at end of arguments."));
11744
11745   c = XNEW (struct exec_catchpoint);
11746   init_catchpoint (&c->base, gdbarch, tempflag, cond_string,
11747                    &catch_exec_breakpoint_ops);
11748   c->exec_pathname = NULL;
11749
11750   install_breakpoint (0, &c->base, 1);
11751 }
11752
11753 void
11754 init_ada_exception_breakpoint (struct breakpoint *b,
11755                                struct gdbarch *gdbarch,
11756                                struct symtab_and_line sal,
11757                                char *addr_string,
11758                                const struct breakpoint_ops *ops,
11759                                int tempflag,
11760                                int enabled,
11761                                int from_tty)
11762 {
11763   if (from_tty)
11764     {
11765       struct gdbarch *loc_gdbarch = get_sal_arch (sal);
11766       if (!loc_gdbarch)
11767         loc_gdbarch = gdbarch;
11768
11769       describe_other_breakpoints (loc_gdbarch,
11770                                   sal.pspace, sal.pc, sal.section, -1);
11771       /* FIXME: brobecker/2006-12-28: Actually, re-implement a special
11772          version for exception catchpoints, because two catchpoints
11773          used for different exception names will use the same address.
11774          In this case, a "breakpoint ... also set at..." warning is
11775          unproductive.  Besides, the warning phrasing is also a bit
11776          inappropriate, we should use the word catchpoint, and tell
11777          the user what type of catchpoint it is.  The above is good
11778          enough for now, though.  */
11779     }
11780
11781   init_raw_breakpoint (b, gdbarch, sal, bp_breakpoint, ops);
11782
11783   b->enable_state = enabled ? bp_enabled : bp_disabled;
11784   b->disposition = tempflag ? disp_del : disp_donttouch;
11785   b->addr_string = addr_string;
11786   b->language = language_ada;
11787 }
11788
11789 /* Splits the argument using space as delimiter.  Returns an xmalloc'd
11790    filter list, or NULL if no filtering is required.  */
11791 static VEC(int) *
11792 catch_syscall_split_args (char *arg)
11793 {
11794   VEC(int) *result = NULL;
11795   struct cleanup *cleanup = make_cleanup (VEC_cleanup (int), &result);
11796
11797   while (*arg != '\0')
11798     {
11799       int i, syscall_number;
11800       char *endptr;
11801       char cur_name[128];
11802       struct syscall s;
11803
11804       /* Skip whitespace.  */
11805       arg = skip_spaces (arg);
11806
11807       for (i = 0; i < 127 && arg[i] && !isspace (arg[i]); ++i)
11808         cur_name[i] = arg[i];
11809       cur_name[i] = '\0';
11810       arg += i;
11811
11812       /* Check if the user provided a syscall name or a number.  */
11813       syscall_number = (int) strtol (cur_name, &endptr, 0);
11814       if (*endptr == '\0')
11815         get_syscall_by_number (syscall_number, &s);
11816       else
11817         {
11818           /* We have a name.  Let's check if it's valid and convert it
11819              to a number.  */
11820           get_syscall_by_name (cur_name, &s);
11821
11822           if (s.number == UNKNOWN_SYSCALL)
11823             /* Here we have to issue an error instead of a warning,
11824                because GDB cannot do anything useful if there's no
11825                syscall number to be caught.  */
11826             error (_("Unknown syscall name '%s'."), cur_name);
11827         }
11828
11829       /* Ok, it's valid.  */
11830       VEC_safe_push (int, result, s.number);
11831     }
11832
11833   discard_cleanups (cleanup);
11834   return result;
11835 }
11836
11837 /* Implement the "catch syscall" command.  */
11838
11839 static void
11840 catch_syscall_command_1 (char *arg, int from_tty, 
11841                          struct cmd_list_element *command)
11842 {
11843   int tempflag;
11844   VEC(int) *filter;
11845   struct syscall s;
11846   struct gdbarch *gdbarch = get_current_arch ();
11847
11848   /* Checking if the feature if supported.  */
11849   if (gdbarch_get_syscall_number_p (gdbarch) == 0)
11850     error (_("The feature 'catch syscall' is not supported on \
11851 this architecture yet."));
11852
11853   tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
11854
11855   arg = skip_spaces (arg);
11856
11857   /* We need to do this first "dummy" translation in order
11858      to get the syscall XML file loaded or, most important,
11859      to display a warning to the user if there's no XML file
11860      for his/her architecture.  */
11861   get_syscall_by_number (0, &s);
11862
11863   /* The allowed syntax is:
11864      catch syscall
11865      catch syscall <name | number> [<name | number> ... <name | number>]
11866
11867      Let's check if there's a syscall name.  */
11868
11869   if (arg != NULL)
11870     filter = catch_syscall_split_args (arg);
11871   else
11872     filter = NULL;
11873
11874   create_syscall_event_catchpoint (tempflag, filter,
11875                                    &catch_syscall_breakpoint_ops);
11876 }
11877
11878 static void
11879 catch_command (char *arg, int from_tty)
11880 {
11881   error (_("Catch requires an event name."));
11882 }
11883 \f
11884
11885 static void
11886 tcatch_command (char *arg, int from_tty)
11887 {
11888   error (_("Catch requires an event name."));
11889 }
11890
11891 /* A qsort comparison function that sorts breakpoints in order.  */
11892
11893 static int
11894 compare_breakpoints (const void *a, const void *b)
11895 {
11896   const breakpoint_p *ba = a;
11897   uintptr_t ua = (uintptr_t) *ba;
11898   const breakpoint_p *bb = b;
11899   uintptr_t ub = (uintptr_t) *bb;
11900
11901   if ((*ba)->number < (*bb)->number)
11902     return -1;
11903   else if ((*ba)->number > (*bb)->number)
11904     return 1;
11905
11906   /* Now sort by address, in case we see, e..g, two breakpoints with
11907      the number 0.  */
11908   if (ua < ub)
11909     return -1;
11910   return ua > ub ? 1 : 0;
11911 }
11912
11913 /* Delete breakpoints by address or line.  */
11914
11915 static void
11916 clear_command (char *arg, int from_tty)
11917 {
11918   struct breakpoint *b, *prev;
11919   VEC(breakpoint_p) *found = 0;
11920   int ix;
11921   int default_match;
11922   struct symtabs_and_lines sals;
11923   struct symtab_and_line sal;
11924   int i;
11925   struct cleanup *cleanups = make_cleanup (null_cleanup, NULL);
11926
11927   if (arg)
11928     {
11929       sals = decode_line_with_current_source (arg,
11930                                               (DECODE_LINE_FUNFIRSTLINE
11931                                                | DECODE_LINE_LIST_MODE));
11932       make_cleanup (xfree, sals.sals);
11933       default_match = 0;
11934     }
11935   else
11936     {
11937       sals.sals = (struct symtab_and_line *)
11938         xmalloc (sizeof (struct symtab_and_line));
11939       make_cleanup (xfree, sals.sals);
11940       init_sal (&sal);          /* Initialize to zeroes.  */
11941
11942       /* Set sal's line, symtab, pc, and pspace to the values
11943          corresponding to the last call to print_frame_info.  If the
11944          codepoint is not valid, this will set all the fields to 0.  */
11945       get_last_displayed_sal (&sal);
11946       if (sal.symtab == 0)
11947         error (_("No source file specified."));
11948
11949       sals.sals[0] = sal;
11950       sals.nelts = 1;
11951
11952       default_match = 1;
11953     }
11954
11955   /* We don't call resolve_sal_pc here.  That's not as bad as it
11956      seems, because all existing breakpoints typically have both
11957      file/line and pc set.  So, if clear is given file/line, we can
11958      match this to existing breakpoint without obtaining pc at all.
11959
11960      We only support clearing given the address explicitly 
11961      present in breakpoint table.  Say, we've set breakpoint 
11962      at file:line.  There were several PC values for that file:line,
11963      due to optimization, all in one block.
11964
11965      We've picked one PC value.  If "clear" is issued with another
11966      PC corresponding to the same file:line, the breakpoint won't
11967      be cleared.  We probably can still clear the breakpoint, but 
11968      since the other PC value is never presented to user, user
11969      can only find it by guessing, and it does not seem important
11970      to support that.  */
11971
11972   /* For each line spec given, delete bps which correspond to it.  Do
11973      it in two passes, solely to preserve the current behavior that
11974      from_tty is forced true if we delete more than one
11975      breakpoint.  */
11976
11977   found = NULL;
11978   make_cleanup (VEC_cleanup (breakpoint_p), &found);
11979   for (i = 0; i < sals.nelts; i++)
11980     {
11981       const char *sal_fullname;
11982
11983       /* If exact pc given, clear bpts at that pc.
11984          If line given (pc == 0), clear all bpts on specified line.
11985          If defaulting, clear all bpts on default line
11986          or at default pc.
11987
11988          defaulting    sal.pc != 0    tests to do
11989
11990          0              1             pc
11991          1              1             pc _and_ line
11992          0              0             line
11993          1              0             <can't happen> */
11994
11995       sal = sals.sals[i];
11996       sal_fullname = (sal.symtab == NULL
11997                       ? NULL : symtab_to_fullname (sal.symtab));
11998
11999       /* Find all matching breakpoints and add them to 'found'.  */
12000       ALL_BREAKPOINTS (b)
12001         {
12002           int match = 0;
12003           /* Are we going to delete b?  */
12004           if (b->type != bp_none && !is_watchpoint (b))
12005             {
12006               struct bp_location *loc = b->loc;
12007               for (; loc; loc = loc->next)
12008                 {
12009                   /* If the user specified file:line, don't allow a PC
12010                      match.  This matches historical gdb behavior.  */
12011                   int pc_match = (!sal.explicit_line
12012                                   && sal.pc
12013                                   && (loc->pspace == sal.pspace)
12014                                   && (loc->address == sal.pc)
12015                                   && (!section_is_overlay (loc->section)
12016                                       || loc->section == sal.section));
12017                   int line_match = 0;
12018
12019                   if ((default_match || sal.explicit_line)
12020                       && loc->symtab != NULL
12021                       && sal_fullname != NULL
12022                       && sal.pspace == loc->pspace
12023                       && loc->line_number == sal.line
12024                       && filename_cmp (symtab_to_fullname (loc->symtab),
12025                                        sal_fullname) == 0)
12026                     line_match = 1;
12027
12028                   if (pc_match || line_match)
12029                     {
12030                       match = 1;
12031                       break;
12032                     }
12033                 }
12034             }
12035
12036           if (match)
12037             VEC_safe_push(breakpoint_p, found, b);
12038         }
12039     }
12040
12041   /* Now go thru the 'found' chain and delete them.  */
12042   if (VEC_empty(breakpoint_p, found))
12043     {
12044       if (arg)
12045         error (_("No breakpoint at %s."), arg);
12046       else
12047         error (_("No breakpoint at this line."));
12048     }
12049
12050   /* Remove duplicates from the vec.  */
12051   qsort (VEC_address (breakpoint_p, found),
12052          VEC_length (breakpoint_p, found),
12053          sizeof (breakpoint_p),
12054          compare_breakpoints);
12055   prev = VEC_index (breakpoint_p, found, 0);
12056   for (ix = 1; VEC_iterate (breakpoint_p, found, ix, b); ++ix)
12057     {
12058       if (b == prev)
12059         {
12060           VEC_ordered_remove (breakpoint_p, found, ix);
12061           --ix;
12062         }
12063     }
12064
12065   if (VEC_length(breakpoint_p, found) > 1)
12066     from_tty = 1;       /* Always report if deleted more than one.  */
12067   if (from_tty)
12068     {
12069       if (VEC_length(breakpoint_p, found) == 1)
12070         printf_unfiltered (_("Deleted breakpoint "));
12071       else
12072         printf_unfiltered (_("Deleted breakpoints "));
12073     }
12074
12075   for (ix = 0; VEC_iterate(breakpoint_p, found, ix, b); ix++)
12076     {
12077       if (from_tty)
12078         printf_unfiltered ("%d ", b->number);
12079       delete_breakpoint (b);
12080     }
12081   if (from_tty)
12082     putchar_unfiltered ('\n');
12083
12084   do_cleanups (cleanups);
12085 }
12086 \f
12087 /* Delete breakpoint in BS if they are `delete' breakpoints and
12088    all breakpoints that are marked for deletion, whether hit or not.
12089    This is called after any breakpoint is hit, or after errors.  */
12090
12091 void
12092 breakpoint_auto_delete (bpstat bs)
12093 {
12094   struct breakpoint *b, *b_tmp;
12095
12096   for (; bs; bs = bs->next)
12097     if (bs->breakpoint_at
12098         && bs->breakpoint_at->disposition == disp_del
12099         && bs->stop)
12100       delete_breakpoint (bs->breakpoint_at);
12101
12102   ALL_BREAKPOINTS_SAFE (b, b_tmp)
12103   {
12104     if (b->disposition == disp_del_at_next_stop)
12105       delete_breakpoint (b);
12106   }
12107 }
12108
12109 /* A comparison function for bp_location AP and BP being interfaced to
12110    qsort.  Sort elements primarily by their ADDRESS (no matter what
12111    does breakpoint_address_is_meaningful say for its OWNER),
12112    secondarily by ordering first bp_permanent OWNERed elements and
12113    terciarily just ensuring the array is sorted stable way despite
12114    qsort being an unstable algorithm.  */
12115
12116 static int
12117 bp_location_compare (const void *ap, const void *bp)
12118 {
12119   struct bp_location *a = *(void **) ap;
12120   struct bp_location *b = *(void **) bp;
12121   /* A and B come from existing breakpoints having non-NULL OWNER.  */
12122   int a_perm = a->owner->enable_state == bp_permanent;
12123   int b_perm = b->owner->enable_state == bp_permanent;
12124
12125   if (a->address != b->address)
12126     return (a->address > b->address) - (a->address < b->address);
12127
12128   /* Sort locations at the same address by their pspace number, keeping
12129      locations of the same inferior (in a multi-inferior environment)
12130      grouped.  */
12131
12132   if (a->pspace->num != b->pspace->num)
12133     return ((a->pspace->num > b->pspace->num)
12134             - (a->pspace->num < b->pspace->num));
12135
12136   /* Sort permanent breakpoints first.  */
12137   if (a_perm != b_perm)
12138     return (a_perm < b_perm) - (a_perm > b_perm);
12139
12140   /* Make the internal GDB representation stable across GDB runs
12141      where A and B memory inside GDB can differ.  Breakpoint locations of
12142      the same type at the same address can be sorted in arbitrary order.  */
12143
12144   if (a->owner->number != b->owner->number)
12145     return ((a->owner->number > b->owner->number)
12146             - (a->owner->number < b->owner->number));
12147
12148   return (a > b) - (a < b);
12149 }
12150
12151 /* Set bp_location_placed_address_before_address_max and
12152    bp_location_shadow_len_after_address_max according to the current
12153    content of the bp_location array.  */
12154
12155 static void
12156 bp_location_target_extensions_update (void)
12157 {
12158   struct bp_location *bl, **blp_tmp;
12159
12160   bp_location_placed_address_before_address_max = 0;
12161   bp_location_shadow_len_after_address_max = 0;
12162
12163   ALL_BP_LOCATIONS (bl, blp_tmp)
12164     {
12165       CORE_ADDR start, end, addr;
12166
12167       if (!bp_location_has_shadow (bl))
12168         continue;
12169
12170       start = bl->target_info.placed_address;
12171       end = start + bl->target_info.shadow_len;
12172
12173       gdb_assert (bl->address >= start);
12174       addr = bl->address - start;
12175       if (addr > bp_location_placed_address_before_address_max)
12176         bp_location_placed_address_before_address_max = addr;
12177
12178       /* Zero SHADOW_LEN would not pass bp_location_has_shadow.  */
12179
12180       gdb_assert (bl->address < end);
12181       addr = end - bl->address;
12182       if (addr > bp_location_shadow_len_after_address_max)
12183         bp_location_shadow_len_after_address_max = addr;
12184     }
12185 }
12186
12187 /* Download tracepoint locations if they haven't been.  */
12188
12189 static void
12190 download_tracepoint_locations (void)
12191 {
12192   struct breakpoint *b;
12193   struct cleanup *old_chain;
12194
12195   if (!target_can_download_tracepoint ())
12196     return;
12197
12198   old_chain = save_current_space_and_thread ();
12199
12200   ALL_TRACEPOINTS (b)
12201     {
12202       struct bp_location *bl;
12203       struct tracepoint *t;
12204       int bp_location_downloaded = 0;
12205
12206       if ((b->type == bp_fast_tracepoint
12207            ? !may_insert_fast_tracepoints
12208            : !may_insert_tracepoints))
12209         continue;
12210
12211       for (bl = b->loc; bl; bl = bl->next)
12212         {
12213           /* In tracepoint, locations are _never_ duplicated, so
12214              should_be_inserted is equivalent to
12215              unduplicated_should_be_inserted.  */
12216           if (!should_be_inserted (bl) || bl->inserted)
12217             continue;
12218
12219           switch_to_program_space_and_thread (bl->pspace);
12220
12221           target_download_tracepoint (bl);
12222
12223           bl->inserted = 1;
12224           bp_location_downloaded = 1;
12225         }
12226       t = (struct tracepoint *) b;
12227       t->number_on_target = b->number;
12228       if (bp_location_downloaded)
12229         observer_notify_breakpoint_modified (b);
12230     }
12231
12232   do_cleanups (old_chain);
12233 }
12234
12235 /* Swap the insertion/duplication state between two locations.  */
12236
12237 static void
12238 swap_insertion (struct bp_location *left, struct bp_location *right)
12239 {
12240   const int left_inserted = left->inserted;
12241   const int left_duplicate = left->duplicate;
12242   const int left_needs_update = left->needs_update;
12243   const struct bp_target_info left_target_info = left->target_info;
12244
12245   /* Locations of tracepoints can never be duplicated.  */
12246   if (is_tracepoint (left->owner))
12247     gdb_assert (!left->duplicate);
12248   if (is_tracepoint (right->owner))
12249     gdb_assert (!right->duplicate);
12250
12251   left->inserted = right->inserted;
12252   left->duplicate = right->duplicate;
12253   left->needs_update = right->needs_update;
12254   left->target_info = right->target_info;
12255   right->inserted = left_inserted;
12256   right->duplicate = left_duplicate;
12257   right->needs_update = left_needs_update;
12258   right->target_info = left_target_info;
12259 }
12260
12261 /* Force the re-insertion of the locations at ADDRESS.  This is called
12262    once a new/deleted/modified duplicate location is found and we are evaluating
12263    conditions on the target's side.  Such conditions need to be updated on
12264    the target.  */
12265
12266 static void
12267 force_breakpoint_reinsertion (struct bp_location *bl)
12268 {
12269   struct bp_location **locp = NULL, **loc2p;
12270   struct bp_location *loc;
12271   CORE_ADDR address = 0;
12272   int pspace_num;
12273
12274   address = bl->address;
12275   pspace_num = bl->pspace->num;
12276
12277   /* This is only meaningful if the target is
12278      evaluating conditions and if the user has
12279      opted for condition evaluation on the target's
12280      side.  */
12281   if (gdb_evaluates_breakpoint_condition_p ()
12282       || !target_supports_evaluation_of_breakpoint_conditions ())
12283     return;
12284
12285   /* Flag all breakpoint locations with this address and
12286      the same program space as the location
12287      as "its condition has changed".  We need to
12288      update the conditions on the target's side.  */
12289   ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, address)
12290     {
12291       loc = *loc2p;
12292
12293       if (!is_breakpoint (loc->owner)
12294           || pspace_num != loc->pspace->num)
12295         continue;
12296
12297       /* Flag the location appropriately.  We use a different state to
12298          let everyone know that we already updated the set of locations
12299          with addr bl->address and program space bl->pspace.  This is so
12300          we don't have to keep calling these functions just to mark locations
12301          that have already been marked.  */
12302       loc->condition_changed = condition_updated;
12303
12304       /* Free the agent expression bytecode as well.  We will compute
12305          it later on.  */
12306       if (loc->cond_bytecode)
12307         {
12308           free_agent_expr (loc->cond_bytecode);
12309           loc->cond_bytecode = NULL;
12310         }
12311     }
12312 }
12313
12314 /* If SHOULD_INSERT is false, do not insert any breakpoint locations
12315    into the inferior, only remove already-inserted locations that no
12316    longer should be inserted.  Functions that delete a breakpoint or
12317    breakpoints should pass false, so that deleting a breakpoint
12318    doesn't have the side effect of inserting the locations of other
12319    breakpoints that are marked not-inserted, but should_be_inserted
12320    returns true on them.
12321
12322    This behaviour is useful is situations close to tear-down -- e.g.,
12323    after an exec, while the target still has execution, but breakpoint
12324    shadows of the previous executable image should *NOT* be restored
12325    to the new image; or before detaching, where the target still has
12326    execution and wants to delete breakpoints from GDB's lists, and all
12327    breakpoints had already been removed from the inferior.  */
12328
12329 static void
12330 update_global_location_list (int should_insert)
12331 {
12332   struct breakpoint *b;
12333   struct bp_location **locp, *loc;
12334   struct cleanup *cleanups;
12335   /* Last breakpoint location address that was marked for update.  */
12336   CORE_ADDR last_addr = 0;
12337   /* Last breakpoint location program space that was marked for update.  */
12338   int last_pspace_num = -1;
12339
12340   /* Used in the duplicates detection below.  When iterating over all
12341      bp_locations, points to the first bp_location of a given address.
12342      Breakpoints and watchpoints of different types are never
12343      duplicates of each other.  Keep one pointer for each type of
12344      breakpoint/watchpoint, so we only need to loop over all locations
12345      once.  */
12346   struct bp_location *bp_loc_first;  /* breakpoint */
12347   struct bp_location *wp_loc_first;  /* hardware watchpoint */
12348   struct bp_location *awp_loc_first; /* access watchpoint */
12349   struct bp_location *rwp_loc_first; /* read watchpoint */
12350
12351   /* Saved former bp_location array which we compare against the newly
12352      built bp_location from the current state of ALL_BREAKPOINTS.  */
12353   struct bp_location **old_location, **old_locp;
12354   unsigned old_location_count;
12355
12356   old_location = bp_location;
12357   old_location_count = bp_location_count;
12358   bp_location = NULL;
12359   bp_location_count = 0;
12360   cleanups = make_cleanup (xfree, old_location);
12361
12362   ALL_BREAKPOINTS (b)
12363     for (loc = b->loc; loc; loc = loc->next)
12364       bp_location_count++;
12365
12366   bp_location = xmalloc (sizeof (*bp_location) * bp_location_count);
12367   locp = bp_location;
12368   ALL_BREAKPOINTS (b)
12369     for (loc = b->loc; loc; loc = loc->next)
12370       *locp++ = loc;
12371   qsort (bp_location, bp_location_count, sizeof (*bp_location),
12372          bp_location_compare);
12373
12374   bp_location_target_extensions_update ();
12375
12376   /* Identify bp_location instances that are no longer present in the
12377      new list, and therefore should be freed.  Note that it's not
12378      necessary that those locations should be removed from inferior --
12379      if there's another location at the same address (previously
12380      marked as duplicate), we don't need to remove/insert the
12381      location.
12382      
12383      LOCP is kept in sync with OLD_LOCP, each pointing to the current
12384      and former bp_location array state respectively.  */
12385
12386   locp = bp_location;
12387   for (old_locp = old_location; old_locp < old_location + old_location_count;
12388        old_locp++)
12389     {
12390       struct bp_location *old_loc = *old_locp;
12391       struct bp_location **loc2p;
12392
12393       /* Tells if 'old_loc' is found among the new locations.  If
12394          not, we have to free it.  */
12395       int found_object = 0;
12396       /* Tells if the location should remain inserted in the target.  */
12397       int keep_in_target = 0;
12398       int removed = 0;
12399
12400       /* Skip LOCP entries which will definitely never be needed.
12401          Stop either at or being the one matching OLD_LOC.  */
12402       while (locp < bp_location + bp_location_count
12403              && (*locp)->address < old_loc->address)
12404         locp++;
12405
12406       for (loc2p = locp;
12407            (loc2p < bp_location + bp_location_count
12408             && (*loc2p)->address == old_loc->address);
12409            loc2p++)
12410         {
12411           /* Check if this is a new/duplicated location or a duplicated
12412              location that had its condition modified.  If so, we want to send
12413              its condition to the target if evaluation of conditions is taking
12414              place there.  */
12415           if ((*loc2p)->condition_changed == condition_modified
12416               && (last_addr != old_loc->address
12417                   || last_pspace_num != old_loc->pspace->num))
12418             {
12419               force_breakpoint_reinsertion (*loc2p);
12420               last_pspace_num = old_loc->pspace->num;
12421             }
12422
12423           if (*loc2p == old_loc)
12424             found_object = 1;
12425         }
12426
12427       /* We have already handled this address, update it so that we don't
12428          have to go through updates again.  */
12429       last_addr = old_loc->address;
12430
12431       /* Target-side condition evaluation: Handle deleted locations.  */
12432       if (!found_object)
12433         force_breakpoint_reinsertion (old_loc);
12434
12435       /* If this location is no longer present, and inserted, look if
12436          there's maybe a new location at the same address.  If so,
12437          mark that one inserted, and don't remove this one.  This is
12438          needed so that we don't have a time window where a breakpoint
12439          at certain location is not inserted.  */
12440
12441       if (old_loc->inserted)
12442         {
12443           /* If the location is inserted now, we might have to remove
12444              it.  */
12445
12446           if (found_object && should_be_inserted (old_loc))
12447             {
12448               /* The location is still present in the location list,
12449                  and still should be inserted.  Don't do anything.  */
12450               keep_in_target = 1;
12451             }
12452           else
12453             {
12454               /* This location still exists, but it won't be kept in the
12455                  target since it may have been disabled.  We proceed to
12456                  remove its target-side condition.  */
12457
12458               /* The location is either no longer present, or got
12459                  disabled.  See if there's another location at the
12460                  same address, in which case we don't need to remove
12461                  this one from the target.  */
12462
12463               /* OLD_LOC comes from existing struct breakpoint.  */
12464               if (breakpoint_address_is_meaningful (old_loc->owner))
12465                 {
12466                   for (loc2p = locp;
12467                        (loc2p < bp_location + bp_location_count
12468                         && (*loc2p)->address == old_loc->address);
12469                        loc2p++)
12470                     {
12471                       struct bp_location *loc2 = *loc2p;
12472
12473                       if (breakpoint_locations_match (loc2, old_loc))
12474                         {
12475                           /* Read watchpoint locations are switched to
12476                              access watchpoints, if the former are not
12477                              supported, but the latter are.  */
12478                           if (is_hardware_watchpoint (old_loc->owner))
12479                             {
12480                               gdb_assert (is_hardware_watchpoint (loc2->owner));
12481                               loc2->watchpoint_type = old_loc->watchpoint_type;
12482                             }
12483
12484                           /* loc2 is a duplicated location. We need to check
12485                              if it should be inserted in case it will be
12486                              unduplicated.  */
12487                           if (loc2 != old_loc
12488                               && unduplicated_should_be_inserted (loc2))
12489                             {
12490                               swap_insertion (old_loc, loc2);
12491                               keep_in_target = 1;
12492                               break;
12493                             }
12494                         }
12495                     }
12496                 }
12497             }
12498
12499           if (!keep_in_target)
12500             {
12501               if (remove_breakpoint (old_loc, mark_uninserted))
12502                 {
12503                   /* This is just about all we can do.  We could keep
12504                      this location on the global list, and try to
12505                      remove it next time, but there's no particular
12506                      reason why we will succeed next time.
12507                      
12508                      Note that at this point, old_loc->owner is still
12509                      valid, as delete_breakpoint frees the breakpoint
12510                      only after calling us.  */
12511                   printf_filtered (_("warning: Error removing "
12512                                      "breakpoint %d\n"), 
12513                                    old_loc->owner->number);
12514                 }
12515               removed = 1;
12516             }
12517         }
12518
12519       if (!found_object)
12520         {
12521           if (removed && non_stop
12522               && breakpoint_address_is_meaningful (old_loc->owner)
12523               && !is_hardware_watchpoint (old_loc->owner))
12524             {
12525               /* This location was removed from the target.  In
12526                  non-stop mode, a race condition is possible where
12527                  we've removed a breakpoint, but stop events for that
12528                  breakpoint are already queued and will arrive later.
12529                  We apply an heuristic to be able to distinguish such
12530                  SIGTRAPs from other random SIGTRAPs: we keep this
12531                  breakpoint location for a bit, and will retire it
12532                  after we see some number of events.  The theory here
12533                  is that reporting of events should, "on the average",
12534                  be fair, so after a while we'll see events from all
12535                  threads that have anything of interest, and no longer
12536                  need to keep this breakpoint location around.  We
12537                  don't hold locations forever so to reduce chances of
12538                  mistaking a non-breakpoint SIGTRAP for a breakpoint
12539                  SIGTRAP.
12540
12541                  The heuristic failing can be disastrous on
12542                  decr_pc_after_break targets.
12543
12544                  On decr_pc_after_break targets, like e.g., x86-linux,
12545                  if we fail to recognize a late breakpoint SIGTRAP,
12546                  because events_till_retirement has reached 0 too
12547                  soon, we'll fail to do the PC adjustment, and report
12548                  a random SIGTRAP to the user.  When the user resumes
12549                  the inferior, it will most likely immediately crash
12550                  with SIGILL/SIGBUS/SIGSEGV, or worse, get silently
12551                  corrupted, because of being resumed e.g., in the
12552                  middle of a multi-byte instruction, or skipped a
12553                  one-byte instruction.  This was actually seen happen
12554                  on native x86-linux, and should be less rare on
12555                  targets that do not support new thread events, like
12556                  remote, due to the heuristic depending on
12557                  thread_count.
12558
12559                  Mistaking a random SIGTRAP for a breakpoint trap
12560                  causes similar symptoms (PC adjustment applied when
12561                  it shouldn't), but then again, playing with SIGTRAPs
12562                  behind the debugger's back is asking for trouble.
12563
12564                  Since hardware watchpoint traps are always
12565                  distinguishable from other traps, so we don't need to
12566                  apply keep hardware watchpoint moribund locations
12567                  around.  We simply always ignore hardware watchpoint
12568                  traps we can no longer explain.  */
12569
12570               old_loc->events_till_retirement = 3 * (thread_count () + 1);
12571               old_loc->owner = NULL;
12572
12573               VEC_safe_push (bp_location_p, moribund_locations, old_loc);
12574             }
12575           else
12576             {
12577               old_loc->owner = NULL;
12578               decref_bp_location (&old_loc);
12579             }
12580         }
12581     }
12582
12583   /* Rescan breakpoints at the same address and section, marking the
12584      first one as "first" and any others as "duplicates".  This is so
12585      that the bpt instruction is only inserted once.  If we have a
12586      permanent breakpoint at the same place as BPT, make that one the
12587      official one, and the rest as duplicates.  Permanent breakpoints
12588      are sorted first for the same address.
12589
12590      Do the same for hardware watchpoints, but also considering the
12591      watchpoint's type (regular/access/read) and length.  */
12592
12593   bp_loc_first = NULL;
12594   wp_loc_first = NULL;
12595   awp_loc_first = NULL;
12596   rwp_loc_first = NULL;
12597   ALL_BP_LOCATIONS (loc, locp)
12598     {
12599       /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always
12600          non-NULL.  */
12601       struct bp_location **loc_first_p;
12602       b = loc->owner;
12603
12604       if (!unduplicated_should_be_inserted (loc)
12605           || !breakpoint_address_is_meaningful (b)
12606           /* Don't detect duplicate for tracepoint locations because they are
12607            never duplicated.  See the comments in field `duplicate' of
12608            `struct bp_location'.  */
12609           || is_tracepoint (b))
12610         {
12611           /* Clear the condition modification flag.  */
12612           loc->condition_changed = condition_unchanged;
12613           continue;
12614         }
12615
12616       /* Permanent breakpoint should always be inserted.  */
12617       if (b->enable_state == bp_permanent && ! loc->inserted)
12618         internal_error (__FILE__, __LINE__,
12619                         _("allegedly permanent breakpoint is not "
12620                         "actually inserted"));
12621
12622       if (b->type == bp_hardware_watchpoint)
12623         loc_first_p = &wp_loc_first;
12624       else if (b->type == bp_read_watchpoint)
12625         loc_first_p = &rwp_loc_first;
12626       else if (b->type == bp_access_watchpoint)
12627         loc_first_p = &awp_loc_first;
12628       else
12629         loc_first_p = &bp_loc_first;
12630
12631       if (*loc_first_p == NULL
12632           || (overlay_debugging && loc->section != (*loc_first_p)->section)
12633           || !breakpoint_locations_match (loc, *loc_first_p))
12634         {
12635           *loc_first_p = loc;
12636           loc->duplicate = 0;
12637
12638           if (is_breakpoint (loc->owner) && loc->condition_changed)
12639             {
12640               loc->needs_update = 1;
12641               /* Clear the condition modification flag.  */
12642               loc->condition_changed = condition_unchanged;
12643             }
12644           continue;
12645         }
12646
12647
12648       /* This and the above ensure the invariant that the first location
12649          is not duplicated, and is the inserted one.
12650          All following are marked as duplicated, and are not inserted.  */
12651       if (loc->inserted)
12652         swap_insertion (loc, *loc_first_p);
12653       loc->duplicate = 1;
12654
12655       /* Clear the condition modification flag.  */
12656       loc->condition_changed = condition_unchanged;
12657
12658       if ((*loc_first_p)->owner->enable_state == bp_permanent && loc->inserted
12659           && b->enable_state != bp_permanent)
12660         internal_error (__FILE__, __LINE__,
12661                         _("another breakpoint was inserted on top of "
12662                         "a permanent breakpoint"));
12663     }
12664
12665   if (breakpoints_always_inserted_mode ()
12666       && (have_live_inferiors ()
12667           || (gdbarch_has_global_breakpoints (target_gdbarch ()))))
12668     {
12669       if (should_insert)
12670         insert_breakpoint_locations ();
12671       else
12672         {
12673           /* Though should_insert is false, we may need to update conditions
12674              on the target's side if it is evaluating such conditions.  We
12675              only update conditions for locations that are marked
12676              "needs_update".  */
12677           update_inserted_breakpoint_locations ();
12678         }
12679     }
12680
12681   if (should_insert)
12682     download_tracepoint_locations ();
12683
12684   do_cleanups (cleanups);
12685 }
12686
12687 void
12688 breakpoint_retire_moribund (void)
12689 {
12690   struct bp_location *loc;
12691   int ix;
12692
12693   for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, loc); ++ix)
12694     if (--(loc->events_till_retirement) == 0)
12695       {
12696         decref_bp_location (&loc);
12697         VEC_unordered_remove (bp_location_p, moribund_locations, ix);
12698         --ix;
12699       }
12700 }
12701
12702 static void
12703 update_global_location_list_nothrow (int inserting)
12704 {
12705   volatile struct gdb_exception e;
12706
12707   TRY_CATCH (e, RETURN_MASK_ERROR)
12708     update_global_location_list (inserting);
12709 }
12710
12711 /* Clear BKP from a BPS.  */
12712
12713 static void
12714 bpstat_remove_bp_location (bpstat bps, struct breakpoint *bpt)
12715 {
12716   bpstat bs;
12717
12718   for (bs = bps; bs; bs = bs->next)
12719     if (bs->breakpoint_at == bpt)
12720       {
12721         bs->breakpoint_at = NULL;
12722         bs->old_val = NULL;
12723         /* bs->commands will be freed later.  */
12724       }
12725 }
12726
12727 /* Callback for iterate_over_threads.  */
12728 static int
12729 bpstat_remove_breakpoint_callback (struct thread_info *th, void *data)
12730 {
12731   struct breakpoint *bpt = data;
12732
12733   bpstat_remove_bp_location (th->control.stop_bpstat, bpt);
12734   return 0;
12735 }
12736
12737 /* Helper for breakpoint and tracepoint breakpoint_ops->mention
12738    callbacks.  */
12739
12740 static void
12741 say_where (struct breakpoint *b)
12742 {
12743   struct value_print_options opts;
12744
12745   get_user_print_options (&opts);
12746
12747   /* i18n: cagney/2005-02-11: Below needs to be merged into a
12748      single string.  */
12749   if (b->loc == NULL)
12750     {
12751       printf_filtered (_(" (%s) pending."), b->addr_string);
12752     }
12753   else
12754     {
12755       if (opts.addressprint || b->loc->symtab == NULL)
12756         {
12757           printf_filtered (" at ");
12758           fputs_filtered (paddress (b->loc->gdbarch, b->loc->address),
12759                           gdb_stdout);
12760         }
12761       if (b->loc->symtab != NULL)
12762         {
12763           /* If there is a single location, we can print the location
12764              more nicely.  */
12765           if (b->loc->next == NULL)
12766             printf_filtered (": file %s, line %d.",
12767                              symtab_to_filename_for_display (b->loc->symtab),
12768                              b->loc->line_number);
12769           else
12770             /* This is not ideal, but each location may have a
12771                different file name, and this at least reflects the
12772                real situation somewhat.  */
12773             printf_filtered (": %s.", b->addr_string);
12774         }
12775
12776       if (b->loc->next)
12777         {
12778           struct bp_location *loc = b->loc;
12779           int n = 0;
12780           for (; loc; loc = loc->next)
12781             ++n;
12782           printf_filtered (" (%d locations)", n);
12783         }
12784     }
12785 }
12786
12787 /* Default bp_location_ops methods.  */
12788
12789 static void
12790 bp_location_dtor (struct bp_location *self)
12791 {
12792   xfree (self->cond);
12793   if (self->cond_bytecode)
12794     free_agent_expr (self->cond_bytecode);
12795   xfree (self->function_name);
12796
12797   VEC_free (agent_expr_p, self->target_info.conditions);
12798   VEC_free (agent_expr_p, self->target_info.tcommands);
12799 }
12800
12801 static const struct bp_location_ops bp_location_ops =
12802 {
12803   bp_location_dtor
12804 };
12805
12806 /* Default breakpoint_ops methods all breakpoint_ops ultimately
12807    inherit from.  */
12808
12809 static void
12810 base_breakpoint_dtor (struct breakpoint *self)
12811 {
12812   decref_counted_command_line (&self->commands);
12813   xfree (self->cond_string);
12814   xfree (self->extra_string);
12815   xfree (self->addr_string);
12816   xfree (self->filter);
12817   xfree (self->addr_string_range_end);
12818 }
12819
12820 static struct bp_location *
12821 base_breakpoint_allocate_location (struct breakpoint *self)
12822 {
12823   struct bp_location *loc;
12824
12825   loc = XNEW (struct bp_location);
12826   init_bp_location (loc, &bp_location_ops, self);
12827   return loc;
12828 }
12829
12830 static void
12831 base_breakpoint_re_set (struct breakpoint *b)
12832 {
12833   /* Nothing to re-set. */
12834 }
12835
12836 #define internal_error_pure_virtual_called() \
12837   gdb_assert_not_reached ("pure virtual function called")
12838
12839 static int
12840 base_breakpoint_insert_location (struct bp_location *bl)
12841 {
12842   internal_error_pure_virtual_called ();
12843 }
12844
12845 static int
12846 base_breakpoint_remove_location (struct bp_location *bl)
12847 {
12848   internal_error_pure_virtual_called ();
12849 }
12850
12851 static int
12852 base_breakpoint_breakpoint_hit (const struct bp_location *bl,
12853                                 struct address_space *aspace,
12854                                 CORE_ADDR bp_addr,
12855                                 const struct target_waitstatus *ws)
12856 {
12857   internal_error_pure_virtual_called ();
12858 }
12859
12860 static void
12861 base_breakpoint_check_status (bpstat bs)
12862 {
12863   /* Always stop.   */
12864 }
12865
12866 /* A "works_in_software_mode" breakpoint_ops method that just internal
12867    errors.  */
12868
12869 static int
12870 base_breakpoint_works_in_software_mode (const struct breakpoint *b)
12871 {
12872   internal_error_pure_virtual_called ();
12873 }
12874
12875 /* A "resources_needed" breakpoint_ops method that just internal
12876    errors.  */
12877
12878 static int
12879 base_breakpoint_resources_needed (const struct bp_location *bl)
12880 {
12881   internal_error_pure_virtual_called ();
12882 }
12883
12884 static enum print_stop_action
12885 base_breakpoint_print_it (bpstat bs)
12886 {
12887   internal_error_pure_virtual_called ();
12888 }
12889
12890 static void
12891 base_breakpoint_print_one_detail (const struct breakpoint *self,
12892                                   struct ui_out *uiout)
12893 {
12894   /* nothing */
12895 }
12896
12897 static void
12898 base_breakpoint_print_mention (struct breakpoint *b)
12899 {
12900   internal_error_pure_virtual_called ();
12901 }
12902
12903 static void
12904 base_breakpoint_print_recreate (struct breakpoint *b, struct ui_file *fp)
12905 {
12906   internal_error_pure_virtual_called ();
12907 }
12908
12909 static void
12910 base_breakpoint_create_sals_from_address (char **arg,
12911                                           struct linespec_result *canonical,
12912                                           enum bptype type_wanted,
12913                                           char *addr_start,
12914                                           char **copy_arg)
12915 {
12916   internal_error_pure_virtual_called ();
12917 }
12918
12919 static void
12920 base_breakpoint_create_breakpoints_sal (struct gdbarch *gdbarch,
12921                                         struct linespec_result *c,
12922                                         char *cond_string,
12923                                         char *extra_string,
12924                                         enum bptype type_wanted,
12925                                         enum bpdisp disposition,
12926                                         int thread,
12927                                         int task, int ignore_count,
12928                                         const struct breakpoint_ops *o,
12929                                         int from_tty, int enabled,
12930                                         int internal, unsigned flags)
12931 {
12932   internal_error_pure_virtual_called ();
12933 }
12934
12935 static void
12936 base_breakpoint_decode_linespec (struct breakpoint *b, char **s,
12937                                  struct symtabs_and_lines *sals)
12938 {
12939   internal_error_pure_virtual_called ();
12940 }
12941
12942 /* The default 'explains_signal' method.  */
12943
12944 static int
12945 base_breakpoint_explains_signal (struct breakpoint *b, enum gdb_signal sig)
12946 {
12947   return 1;
12948 }
12949
12950 /* The default "after_condition_true" method.  */
12951
12952 static void
12953 base_breakpoint_after_condition_true (struct bpstats *bs)
12954 {
12955   /* Nothing to do.   */
12956 }
12957
12958 struct breakpoint_ops base_breakpoint_ops =
12959 {
12960   base_breakpoint_dtor,
12961   base_breakpoint_allocate_location,
12962   base_breakpoint_re_set,
12963   base_breakpoint_insert_location,
12964   base_breakpoint_remove_location,
12965   base_breakpoint_breakpoint_hit,
12966   base_breakpoint_check_status,
12967   base_breakpoint_resources_needed,
12968   base_breakpoint_works_in_software_mode,
12969   base_breakpoint_print_it,
12970   NULL,
12971   base_breakpoint_print_one_detail,
12972   base_breakpoint_print_mention,
12973   base_breakpoint_print_recreate,
12974   base_breakpoint_create_sals_from_address,
12975   base_breakpoint_create_breakpoints_sal,
12976   base_breakpoint_decode_linespec,
12977   base_breakpoint_explains_signal,
12978   base_breakpoint_after_condition_true,
12979 };
12980
12981 /* Default breakpoint_ops methods.  */
12982
12983 static void
12984 bkpt_re_set (struct breakpoint *b)
12985 {
12986   /* FIXME: is this still reachable?  */
12987   if (b->addr_string == NULL)
12988     {
12989       /* Anything without a string can't be re-set.  */
12990       delete_breakpoint (b);
12991       return;
12992     }
12993
12994   breakpoint_re_set_default (b);
12995 }
12996
12997 static int
12998 bkpt_insert_location (struct bp_location *bl)
12999 {
13000   if (bl->loc_type == bp_loc_hardware_breakpoint)
13001     return target_insert_hw_breakpoint (bl->gdbarch,
13002                                         &bl->target_info);
13003   else
13004     return target_insert_breakpoint (bl->gdbarch,
13005                                      &bl->target_info);
13006 }
13007
13008 static int
13009 bkpt_remove_location (struct bp_location *bl)
13010 {
13011   if (bl->loc_type == bp_loc_hardware_breakpoint)
13012     return target_remove_hw_breakpoint (bl->gdbarch, &bl->target_info);
13013   else
13014     return target_remove_breakpoint (bl->gdbarch, &bl->target_info);
13015 }
13016
13017 static int
13018 bkpt_breakpoint_hit (const struct bp_location *bl,
13019                      struct address_space *aspace, CORE_ADDR bp_addr,
13020                      const struct target_waitstatus *ws)
13021 {
13022   if (ws->kind != TARGET_WAITKIND_STOPPED
13023       || ws->value.sig != GDB_SIGNAL_TRAP)
13024     return 0;
13025
13026   if (!breakpoint_address_match (bl->pspace->aspace, bl->address,
13027                                  aspace, bp_addr))
13028     return 0;
13029
13030   if (overlay_debugging         /* unmapped overlay section */
13031       && section_is_overlay (bl->section)
13032       && !section_is_mapped (bl->section))
13033     return 0;
13034
13035   return 1;
13036 }
13037
13038 static int
13039 bkpt_resources_needed (const struct bp_location *bl)
13040 {
13041   gdb_assert (bl->owner->type == bp_hardware_breakpoint);
13042
13043   return 1;
13044 }
13045
13046 static enum print_stop_action
13047 bkpt_print_it (bpstat bs)
13048 {
13049   struct breakpoint *b;
13050   const struct bp_location *bl;
13051   int bp_temp;
13052   struct ui_out *uiout = current_uiout;
13053
13054   gdb_assert (bs->bp_location_at != NULL);
13055
13056   bl = bs->bp_location_at;
13057   b = bs->breakpoint_at;
13058
13059   bp_temp = b->disposition == disp_del;
13060   if (bl->address != bl->requested_address)
13061     breakpoint_adjustment_warning (bl->requested_address,
13062                                    bl->address,
13063                                    b->number, 1);
13064   annotate_breakpoint (b->number);
13065   if (bp_temp)
13066     ui_out_text (uiout, "\nTemporary breakpoint ");
13067   else
13068     ui_out_text (uiout, "\nBreakpoint ");
13069   if (ui_out_is_mi_like_p (uiout))
13070     {
13071       ui_out_field_string (uiout, "reason",
13072                            async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT));
13073       ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
13074     }
13075   ui_out_field_int (uiout, "bkptno", b->number);
13076   ui_out_text (uiout, ", ");
13077
13078   return PRINT_SRC_AND_LOC;
13079 }
13080
13081 static void
13082 bkpt_print_mention (struct breakpoint *b)
13083 {
13084   if (ui_out_is_mi_like_p (current_uiout))
13085     return;
13086
13087   switch (b->type)
13088     {
13089     case bp_breakpoint:
13090     case bp_gnu_ifunc_resolver:
13091       if (b->disposition == disp_del)
13092         printf_filtered (_("Temporary breakpoint"));
13093       else
13094         printf_filtered (_("Breakpoint"));
13095       printf_filtered (_(" %d"), b->number);
13096       if (b->type == bp_gnu_ifunc_resolver)
13097         printf_filtered (_(" at gnu-indirect-function resolver"));
13098       break;
13099     case bp_hardware_breakpoint:
13100       printf_filtered (_("Hardware assisted breakpoint %d"), b->number);
13101       break;
13102     case bp_dprintf:
13103       printf_filtered (_("Dprintf %d"), b->number);
13104       break;
13105     }
13106
13107   say_where (b);
13108 }
13109
13110 static void
13111 bkpt_print_recreate (struct breakpoint *tp, struct ui_file *fp)
13112 {
13113   if (tp->type == bp_breakpoint && tp->disposition == disp_del)
13114     fprintf_unfiltered (fp, "tbreak");
13115   else if (tp->type == bp_breakpoint)
13116     fprintf_unfiltered (fp, "break");
13117   else if (tp->type == bp_hardware_breakpoint
13118            && tp->disposition == disp_del)
13119     fprintf_unfiltered (fp, "thbreak");
13120   else if (tp->type == bp_hardware_breakpoint)
13121     fprintf_unfiltered (fp, "hbreak");
13122   else
13123     internal_error (__FILE__, __LINE__,
13124                     _("unhandled breakpoint type %d"), (int) tp->type);
13125
13126   fprintf_unfiltered (fp, " %s", tp->addr_string);
13127   print_recreate_thread (tp, fp);
13128 }
13129
13130 static void
13131 bkpt_create_sals_from_address (char **arg,
13132                                struct linespec_result *canonical,
13133                                enum bptype type_wanted,
13134                                char *addr_start, char **copy_arg)
13135 {
13136   create_sals_from_address_default (arg, canonical, type_wanted,
13137                                     addr_start, copy_arg);
13138 }
13139
13140 static void
13141 bkpt_create_breakpoints_sal (struct gdbarch *gdbarch,
13142                              struct linespec_result *canonical,
13143                              char *cond_string,
13144                              char *extra_string,
13145                              enum bptype type_wanted,
13146                              enum bpdisp disposition,
13147                              int thread,
13148                              int task, int ignore_count,
13149                              const struct breakpoint_ops *ops,
13150                              int from_tty, int enabled,
13151                              int internal, unsigned flags)
13152 {
13153   create_breakpoints_sal_default (gdbarch, canonical,
13154                                   cond_string, extra_string,
13155                                   type_wanted,
13156                                   disposition, thread, task,
13157                                   ignore_count, ops, from_tty,
13158                                   enabled, internal, flags);
13159 }
13160
13161 static void
13162 bkpt_decode_linespec (struct breakpoint *b, char **s,
13163                       struct symtabs_and_lines *sals)
13164 {
13165   decode_linespec_default (b, s, sals);
13166 }
13167
13168 /* Virtual table for internal breakpoints.  */
13169
13170 static void
13171 internal_bkpt_re_set (struct breakpoint *b)
13172 {
13173   switch (b->type)
13174     {
13175       /* Delete overlay event and longjmp master breakpoints; they
13176          will be reset later by breakpoint_re_set.  */
13177     case bp_overlay_event:
13178     case bp_longjmp_master:
13179     case bp_std_terminate_master:
13180     case bp_exception_master:
13181       delete_breakpoint (b);
13182       break;
13183
13184       /* This breakpoint is special, it's set up when the inferior
13185          starts and we really don't want to touch it.  */
13186     case bp_shlib_event:
13187
13188       /* Like bp_shlib_event, this breakpoint type is special.  Once
13189          it is set up, we do not want to touch it.  */
13190     case bp_thread_event:
13191       break;
13192     }
13193 }
13194
13195 static void
13196 internal_bkpt_check_status (bpstat bs)
13197 {
13198   if (bs->breakpoint_at->type == bp_shlib_event)
13199     {
13200       /* If requested, stop when the dynamic linker notifies GDB of
13201          events.  This allows the user to get control and place
13202          breakpoints in initializer routines for dynamically loaded
13203          objects (among other things).  */
13204       bs->stop = stop_on_solib_events;
13205       bs->print = stop_on_solib_events;
13206     }
13207   else
13208     bs->stop = 0;
13209 }
13210
13211 static enum print_stop_action
13212 internal_bkpt_print_it (bpstat bs)
13213 {
13214   struct breakpoint *b;
13215
13216   b = bs->breakpoint_at;
13217
13218   switch (b->type)
13219     {
13220     case bp_shlib_event:
13221       /* Did we stop because the user set the stop_on_solib_events
13222          variable?  (If so, we report this as a generic, "Stopped due
13223          to shlib event" message.) */
13224       print_solib_event (0);
13225       break;
13226
13227     case bp_thread_event:
13228       /* Not sure how we will get here.
13229          GDB should not stop for these breakpoints.  */
13230       printf_filtered (_("Thread Event Breakpoint: gdb should not stop!\n"));
13231       break;
13232
13233     case bp_overlay_event:
13234       /* By analogy with the thread event, GDB should not stop for these.  */
13235       printf_filtered (_("Overlay Event Breakpoint: gdb should not stop!\n"));
13236       break;
13237
13238     case bp_longjmp_master:
13239       /* These should never be enabled.  */
13240       printf_filtered (_("Longjmp Master Breakpoint: gdb should not stop!\n"));
13241       break;
13242
13243     case bp_std_terminate_master:
13244       /* These should never be enabled.  */
13245       printf_filtered (_("std::terminate Master Breakpoint: "
13246                          "gdb should not stop!\n"));
13247       break;
13248
13249     case bp_exception_master:
13250       /* These should never be enabled.  */
13251       printf_filtered (_("Exception Master Breakpoint: "
13252                          "gdb should not stop!\n"));
13253       break;
13254     }
13255
13256   return PRINT_NOTHING;
13257 }
13258
13259 static void
13260 internal_bkpt_print_mention (struct breakpoint *b)
13261 {
13262   /* Nothing to mention.  These breakpoints are internal.  */
13263 }
13264
13265 /* Virtual table for momentary breakpoints  */
13266
13267 static void
13268 momentary_bkpt_re_set (struct breakpoint *b)
13269 {
13270   /* Keep temporary breakpoints, which can be encountered when we step
13271      over a dlopen call and solib_add is resetting the breakpoints.
13272      Otherwise these should have been blown away via the cleanup chain
13273      or by breakpoint_init_inferior when we rerun the executable.  */
13274 }
13275
13276 static void
13277 momentary_bkpt_check_status (bpstat bs)
13278 {
13279   /* Nothing.  The point of these breakpoints is causing a stop.  */
13280 }
13281
13282 static enum print_stop_action
13283 momentary_bkpt_print_it (bpstat bs)
13284 {
13285   struct ui_out *uiout = current_uiout;
13286
13287   if (ui_out_is_mi_like_p (uiout))
13288     {
13289       struct breakpoint *b = bs->breakpoint_at;
13290
13291       switch (b->type)
13292         {
13293         case bp_finish:
13294           ui_out_field_string
13295             (uiout, "reason",
13296              async_reason_lookup (EXEC_ASYNC_FUNCTION_FINISHED));
13297           break;
13298
13299         case bp_until:
13300           ui_out_field_string
13301             (uiout, "reason",
13302              async_reason_lookup (EXEC_ASYNC_LOCATION_REACHED));
13303           break;
13304         }
13305     }
13306
13307   return PRINT_UNKNOWN;
13308 }
13309
13310 static void
13311 momentary_bkpt_print_mention (struct breakpoint *b)
13312 {
13313   /* Nothing to mention.  These breakpoints are internal.  */
13314 }
13315
13316 /* Ensure INITIATING_FRAME is cleared when no such breakpoint exists.
13317
13318    It gets cleared already on the removal of the first one of such placed
13319    breakpoints.  This is OK as they get all removed altogether.  */
13320
13321 static void
13322 longjmp_bkpt_dtor (struct breakpoint *self)
13323 {
13324   struct thread_info *tp = find_thread_id (self->thread);
13325
13326   if (tp)
13327     tp->initiating_frame = null_frame_id;
13328
13329   momentary_breakpoint_ops.dtor (self);
13330 }
13331
13332 /* Specific methods for probe breakpoints.  */
13333
13334 static int
13335 bkpt_probe_insert_location (struct bp_location *bl)
13336 {
13337   int v = bkpt_insert_location (bl);
13338
13339   if (v == 0)
13340     {
13341       /* The insertion was successful, now let's set the probe's semaphore
13342          if needed.  */
13343       bl->probe->pops->set_semaphore (bl->probe, bl->gdbarch);
13344     }
13345
13346   return v;
13347 }
13348
13349 static int
13350 bkpt_probe_remove_location (struct bp_location *bl)
13351 {
13352   /* Let's clear the semaphore before removing the location.  */
13353   bl->probe->pops->clear_semaphore (bl->probe, bl->gdbarch);
13354
13355   return bkpt_remove_location (bl);
13356 }
13357
13358 static void
13359 bkpt_probe_create_sals_from_address (char **arg,
13360                                      struct linespec_result *canonical,
13361                                      enum bptype type_wanted,
13362                                      char *addr_start, char **copy_arg)
13363 {
13364   struct linespec_sals lsal;
13365
13366   lsal.sals = parse_probes (arg, canonical);
13367
13368   *copy_arg = xstrdup (canonical->addr_string);
13369   lsal.canonical = xstrdup (*copy_arg);
13370
13371   VEC_safe_push (linespec_sals, canonical->sals, &lsal);
13372 }
13373
13374 static void
13375 bkpt_probe_decode_linespec (struct breakpoint *b, char **s,
13376                             struct symtabs_and_lines *sals)
13377 {
13378   *sals = parse_probes (s, NULL);
13379   if (!sals->sals)
13380     error (_("probe not found"));
13381 }
13382
13383 /* The breakpoint_ops structure to be used in tracepoints.  */
13384
13385 static void
13386 tracepoint_re_set (struct breakpoint *b)
13387 {
13388   breakpoint_re_set_default (b);
13389 }
13390
13391 static int
13392 tracepoint_breakpoint_hit (const struct bp_location *bl,
13393                            struct address_space *aspace, CORE_ADDR bp_addr,
13394                            const struct target_waitstatus *ws)
13395 {
13396   /* By definition, the inferior does not report stops at
13397      tracepoints.  */
13398   return 0;
13399 }
13400
13401 static void
13402 tracepoint_print_one_detail (const struct breakpoint *self,
13403                              struct ui_out *uiout)
13404 {
13405   struct tracepoint *tp = (struct tracepoint *) self;
13406   if (tp->static_trace_marker_id)
13407     {
13408       gdb_assert (self->type == bp_static_tracepoint);
13409
13410       ui_out_text (uiout, "\tmarker id is ");
13411       ui_out_field_string (uiout, "static-tracepoint-marker-string-id",
13412                            tp->static_trace_marker_id);
13413       ui_out_text (uiout, "\n");
13414     }
13415 }
13416
13417 static void
13418 tracepoint_print_mention (struct breakpoint *b)
13419 {
13420   if (ui_out_is_mi_like_p (current_uiout))
13421     return;
13422
13423   switch (b->type)
13424     {
13425     case bp_tracepoint:
13426       printf_filtered (_("Tracepoint"));
13427       printf_filtered (_(" %d"), b->number);
13428       break;
13429     case bp_fast_tracepoint:
13430       printf_filtered (_("Fast tracepoint"));
13431       printf_filtered (_(" %d"), b->number);
13432       break;
13433     case bp_static_tracepoint:
13434       printf_filtered (_("Static tracepoint"));
13435       printf_filtered (_(" %d"), b->number);
13436       break;
13437     default:
13438       internal_error (__FILE__, __LINE__,
13439                       _("unhandled tracepoint type %d"), (int) b->type);
13440     }
13441
13442   say_where (b);
13443 }
13444
13445 static void
13446 tracepoint_print_recreate (struct breakpoint *self, struct ui_file *fp)
13447 {
13448   struct tracepoint *tp = (struct tracepoint *) self;
13449
13450   if (self->type == bp_fast_tracepoint)
13451     fprintf_unfiltered (fp, "ftrace");
13452   if (self->type == bp_static_tracepoint)
13453     fprintf_unfiltered (fp, "strace");
13454   else if (self->type == bp_tracepoint)
13455     fprintf_unfiltered (fp, "trace");
13456   else
13457     internal_error (__FILE__, __LINE__,
13458                     _("unhandled tracepoint type %d"), (int) self->type);
13459
13460   fprintf_unfiltered (fp, " %s", self->addr_string);
13461   print_recreate_thread (self, fp);
13462
13463   if (tp->pass_count)
13464     fprintf_unfiltered (fp, "  passcount %d\n", tp->pass_count);
13465 }
13466
13467 static void
13468 tracepoint_create_sals_from_address (char **arg,
13469                                      struct linespec_result *canonical,
13470                                      enum bptype type_wanted,
13471                                      char *addr_start, char **copy_arg)
13472 {
13473   create_sals_from_address_default (arg, canonical, type_wanted,
13474                                     addr_start, copy_arg);
13475 }
13476
13477 static void
13478 tracepoint_create_breakpoints_sal (struct gdbarch *gdbarch,
13479                                    struct linespec_result *canonical,
13480                                    char *cond_string,
13481                                    char *extra_string,
13482                                    enum bptype type_wanted,
13483                                    enum bpdisp disposition,
13484                                    int thread,
13485                                    int task, int ignore_count,
13486                                    const struct breakpoint_ops *ops,
13487                                    int from_tty, int enabled,
13488                                    int internal, unsigned flags)
13489 {
13490   create_breakpoints_sal_default (gdbarch, canonical,
13491                                   cond_string, extra_string,
13492                                   type_wanted,
13493                                   disposition, thread, task,
13494                                   ignore_count, ops, from_tty,
13495                                   enabled, internal, flags);
13496 }
13497
13498 static void
13499 tracepoint_decode_linespec (struct breakpoint *b, char **s,
13500                             struct symtabs_and_lines *sals)
13501 {
13502   decode_linespec_default (b, s, sals);
13503 }
13504
13505 struct breakpoint_ops tracepoint_breakpoint_ops;
13506
13507 /* The breakpoint_ops structure to be use on tracepoints placed in a
13508    static probe.  */
13509
13510 static void
13511 tracepoint_probe_create_sals_from_address (char **arg,
13512                                            struct linespec_result *canonical,
13513                                            enum bptype type_wanted,
13514                                            char *addr_start, char **copy_arg)
13515 {
13516   /* We use the same method for breakpoint on probes.  */
13517   bkpt_probe_create_sals_from_address (arg, canonical, type_wanted,
13518                                        addr_start, copy_arg);
13519 }
13520
13521 static void
13522 tracepoint_probe_decode_linespec (struct breakpoint *b, char **s,
13523                                   struct symtabs_and_lines *sals)
13524 {
13525   /* We use the same method for breakpoint on probes.  */
13526   bkpt_probe_decode_linespec (b, s, sals);
13527 }
13528
13529 static struct breakpoint_ops tracepoint_probe_breakpoint_ops;
13530
13531 /* Dprintf breakpoint_ops methods.  */
13532
13533 static void
13534 dprintf_re_set (struct breakpoint *b)
13535 {
13536   breakpoint_re_set_default (b);
13537
13538   /* This breakpoint could have been pending, and be resolved now, and
13539      if so, we should now have the extra string.  If we don't, the
13540      dprintf was malformed when created, but we couldn't tell because
13541      we can't extract the extra string until the location is
13542      resolved.  */
13543   if (b->loc != NULL && b->extra_string == NULL)
13544     error (_("Format string required"));
13545
13546   /* 1 - connect to target 1, that can run breakpoint commands.
13547      2 - create a dprintf, which resolves fine.
13548      3 - disconnect from target 1
13549      4 - connect to target 2, that can NOT run breakpoint commands.
13550
13551      After steps #3/#4, you'll want the dprintf command list to
13552      be updated, because target 1 and 2 may well return different
13553      answers for target_can_run_breakpoint_commands().
13554      Given absence of finer grained resetting, we get to do
13555      it all the time.  */
13556   if (b->extra_string != NULL)
13557     update_dprintf_command_list (b);
13558 }
13559
13560 /* Implement the "print_recreate" breakpoint_ops method for dprintf.  */
13561
13562 static void
13563 dprintf_print_recreate (struct breakpoint *tp, struct ui_file *fp)
13564 {
13565   fprintf_unfiltered (fp, "dprintf %s%s", tp->addr_string,
13566                       tp->extra_string);
13567   print_recreate_thread (tp, fp);
13568 }
13569
13570 /* Implement the "after_condition_true" breakpoint_ops method for
13571    dprintf.
13572
13573    dprintf's are implemented with regular commands in their command
13574    list, but we run the commands here instead of before presenting the
13575    stop to the user, as dprintf's don't actually cause a stop.  This
13576    also makes it so that the commands of multiple dprintfs at the same
13577    address are all handled.  */
13578
13579 static void
13580 dprintf_after_condition_true (struct bpstats *bs)
13581 {
13582   struct cleanup *old_chain;
13583   struct bpstats tmp_bs = { NULL };
13584   struct bpstats *tmp_bs_p = &tmp_bs;
13585
13586   /* dprintf's never cause a stop.  This wasn't set in the
13587      check_status hook instead because that would make the dprintf's
13588      condition not be evaluated.  */
13589   bs->stop = 0;
13590
13591   /* Run the command list here.  Take ownership of it instead of
13592      copying.  We never want these commands to run later in
13593      bpstat_do_actions, if a breakpoint that causes a stop happens to
13594      be set at same address as this dprintf, or even if running the
13595      commands here throws.  */
13596   tmp_bs.commands = bs->commands;
13597   bs->commands = NULL;
13598   old_chain = make_cleanup_decref_counted_command_line (&tmp_bs.commands);
13599
13600   bpstat_do_actions_1 (&tmp_bs_p);
13601
13602   /* 'tmp_bs.commands' will usually be NULL by now, but
13603      bpstat_do_actions_1 may return early without processing the whole
13604      list.  */
13605   do_cleanups (old_chain);
13606 }
13607
13608 /* The breakpoint_ops structure to be used on static tracepoints with
13609    markers (`-m').  */
13610
13611 static void
13612 strace_marker_create_sals_from_address (char **arg,
13613                                         struct linespec_result *canonical,
13614                                         enum bptype type_wanted,
13615                                         char *addr_start, char **copy_arg)
13616 {
13617   struct linespec_sals lsal;
13618
13619   lsal.sals = decode_static_tracepoint_spec (arg);
13620
13621   *copy_arg = savestring (addr_start, *arg - addr_start);
13622
13623   canonical->addr_string = xstrdup (*copy_arg);
13624   lsal.canonical = xstrdup (*copy_arg);
13625   VEC_safe_push (linespec_sals, canonical->sals, &lsal);
13626 }
13627
13628 static void
13629 strace_marker_create_breakpoints_sal (struct gdbarch *gdbarch,
13630                                       struct linespec_result *canonical,
13631                                       char *cond_string,
13632                                       char *extra_string,
13633                                       enum bptype type_wanted,
13634                                       enum bpdisp disposition,
13635                                       int thread,
13636                                       int task, int ignore_count,
13637                                       const struct breakpoint_ops *ops,
13638                                       int from_tty, int enabled,
13639                                       int internal, unsigned flags)
13640 {
13641   int i;
13642   struct linespec_sals *lsal = VEC_index (linespec_sals,
13643                                           canonical->sals, 0);
13644
13645   /* If the user is creating a static tracepoint by marker id
13646      (strace -m MARKER_ID), then store the sals index, so that
13647      breakpoint_re_set can try to match up which of the newly
13648      found markers corresponds to this one, and, don't try to
13649      expand multiple locations for each sal, given than SALS
13650      already should contain all sals for MARKER_ID.  */
13651
13652   for (i = 0; i < lsal->sals.nelts; ++i)
13653     {
13654       struct symtabs_and_lines expanded;
13655       struct tracepoint *tp;
13656       struct cleanup *old_chain;
13657       char *addr_string;
13658
13659       expanded.nelts = 1;
13660       expanded.sals = &lsal->sals.sals[i];
13661
13662       addr_string = xstrdup (canonical->addr_string);
13663       old_chain = make_cleanup (xfree, addr_string);
13664
13665       tp = XCNEW (struct tracepoint);
13666       init_breakpoint_sal (&tp->base, gdbarch, expanded,
13667                            addr_string, NULL,
13668                            cond_string, extra_string,
13669                            type_wanted, disposition,
13670                            thread, task, ignore_count, ops,
13671                            from_tty, enabled, internal, flags,
13672                            canonical->special_display);
13673       /* Given that its possible to have multiple markers with
13674          the same string id, if the user is creating a static
13675          tracepoint by marker id ("strace -m MARKER_ID"), then
13676          store the sals index, so that breakpoint_re_set can
13677          try to match up which of the newly found markers
13678          corresponds to this one  */
13679       tp->static_trace_marker_id_idx = i;
13680
13681       install_breakpoint (internal, &tp->base, 0);
13682
13683       discard_cleanups (old_chain);
13684     }
13685 }
13686
13687 static void
13688 strace_marker_decode_linespec (struct breakpoint *b, char **s,
13689                                struct symtabs_and_lines *sals)
13690 {
13691   struct tracepoint *tp = (struct tracepoint *) b;
13692
13693   *sals = decode_static_tracepoint_spec (s);
13694   if (sals->nelts > tp->static_trace_marker_id_idx)
13695     {
13696       sals->sals[0] = sals->sals[tp->static_trace_marker_id_idx];
13697       sals->nelts = 1;
13698     }
13699   else
13700     error (_("marker %s not found"), tp->static_trace_marker_id);
13701 }
13702
13703 static struct breakpoint_ops strace_marker_breakpoint_ops;
13704
13705 static int
13706 strace_marker_p (struct breakpoint *b)
13707 {
13708   return b->ops == &strace_marker_breakpoint_ops;
13709 }
13710
13711 /* Delete a breakpoint and clean up all traces of it in the data
13712    structures.  */
13713
13714 void
13715 delete_breakpoint (struct breakpoint *bpt)
13716 {
13717   struct breakpoint *b;
13718
13719   gdb_assert (bpt != NULL);
13720
13721   /* Has this bp already been deleted?  This can happen because
13722      multiple lists can hold pointers to bp's.  bpstat lists are
13723      especial culprits.
13724
13725      One example of this happening is a watchpoint's scope bp.  When
13726      the scope bp triggers, we notice that the watchpoint is out of
13727      scope, and delete it.  We also delete its scope bp.  But the
13728      scope bp is marked "auto-deleting", and is already on a bpstat.
13729      That bpstat is then checked for auto-deleting bp's, which are
13730      deleted.
13731
13732      A real solution to this problem might involve reference counts in
13733      bp's, and/or giving them pointers back to their referencing
13734      bpstat's, and teaching delete_breakpoint to only free a bp's
13735      storage when no more references were extent.  A cheaper bandaid
13736      was chosen.  */
13737   if (bpt->type == bp_none)
13738     return;
13739
13740   /* At least avoid this stale reference until the reference counting
13741      of breakpoints gets resolved.  */
13742   if (bpt->related_breakpoint != bpt)
13743     {
13744       struct breakpoint *related;
13745       struct watchpoint *w;
13746
13747       if (bpt->type == bp_watchpoint_scope)
13748         w = (struct watchpoint *) bpt->related_breakpoint;
13749       else if (bpt->related_breakpoint->type == bp_watchpoint_scope)
13750         w = (struct watchpoint *) bpt;
13751       else
13752         w = NULL;
13753       if (w != NULL)
13754         watchpoint_del_at_next_stop (w);
13755
13756       /* Unlink bpt from the bpt->related_breakpoint ring.  */
13757       for (related = bpt; related->related_breakpoint != bpt;
13758            related = related->related_breakpoint);
13759       related->related_breakpoint = bpt->related_breakpoint;
13760       bpt->related_breakpoint = bpt;
13761     }
13762
13763   /* watch_command_1 creates a watchpoint but only sets its number if
13764      update_watchpoint succeeds in creating its bp_locations.  If there's
13765      a problem in that process, we'll be asked to delete the half-created
13766      watchpoint.  In that case, don't announce the deletion.  */
13767   if (bpt->number)
13768     observer_notify_breakpoint_deleted (bpt);
13769
13770   if (breakpoint_chain == bpt)
13771     breakpoint_chain = bpt->next;
13772
13773   ALL_BREAKPOINTS (b)
13774     if (b->next == bpt)
13775     {
13776       b->next = bpt->next;
13777       break;
13778     }
13779
13780   /* Be sure no bpstat's are pointing at the breakpoint after it's
13781      been freed.  */
13782   /* FIXME, how can we find all bpstat's?  We just check stop_bpstat
13783      in all threads for now.  Note that we cannot just remove bpstats
13784      pointing at bpt from the stop_bpstat list entirely, as breakpoint
13785      commands are associated with the bpstat; if we remove it here,
13786      then the later call to bpstat_do_actions (&stop_bpstat); in
13787      event-top.c won't do anything, and temporary breakpoints with
13788      commands won't work.  */
13789
13790   iterate_over_threads (bpstat_remove_breakpoint_callback, bpt);
13791
13792   /* Now that breakpoint is removed from breakpoint list, update the
13793      global location list.  This will remove locations that used to
13794      belong to this breakpoint.  Do this before freeing the breakpoint
13795      itself, since remove_breakpoint looks at location's owner.  It
13796      might be better design to have location completely
13797      self-contained, but it's not the case now.  */
13798   update_global_location_list (0);
13799
13800   bpt->ops->dtor (bpt);
13801   /* On the chance that someone will soon try again to delete this
13802      same bp, we mark it as deleted before freeing its storage.  */
13803   bpt->type = bp_none;
13804   xfree (bpt);
13805 }
13806
13807 static void
13808 do_delete_breakpoint_cleanup (void *b)
13809 {
13810   delete_breakpoint (b);
13811 }
13812
13813 struct cleanup *
13814 make_cleanup_delete_breakpoint (struct breakpoint *b)
13815 {
13816   return make_cleanup (do_delete_breakpoint_cleanup, b);
13817 }
13818
13819 /* Iterator function to call a user-provided callback function once
13820    for each of B and its related breakpoints.  */
13821
13822 static void
13823 iterate_over_related_breakpoints (struct breakpoint *b,
13824                                   void (*function) (struct breakpoint *,
13825                                                     void *),
13826                                   void *data)
13827 {
13828   struct breakpoint *related;
13829
13830   related = b;
13831   do
13832     {
13833       struct breakpoint *next;
13834
13835       /* FUNCTION may delete RELATED.  */
13836       next = related->related_breakpoint;
13837
13838       if (next == related)
13839         {
13840           /* RELATED is the last ring entry.  */
13841           function (related, data);
13842
13843           /* FUNCTION may have deleted it, so we'd never reach back to
13844              B.  There's nothing left to do anyway, so just break
13845              out.  */
13846           break;
13847         }
13848       else
13849         function (related, data);
13850
13851       related = next;
13852     }
13853   while (related != b);
13854 }
13855
13856 static void
13857 do_delete_breakpoint (struct breakpoint *b, void *ignore)
13858 {
13859   delete_breakpoint (b);
13860 }
13861
13862 /* A callback for map_breakpoint_numbers that calls
13863    delete_breakpoint.  */
13864
13865 static void
13866 do_map_delete_breakpoint (struct breakpoint *b, void *ignore)
13867 {
13868   iterate_over_related_breakpoints (b, do_delete_breakpoint, NULL);
13869 }
13870
13871 void
13872 delete_command (char *arg, int from_tty)
13873 {
13874   struct breakpoint *b, *b_tmp;
13875
13876   dont_repeat ();
13877
13878   if (arg == 0)
13879     {
13880       int breaks_to_delete = 0;
13881
13882       /* Delete all breakpoints if no argument.  Do not delete
13883          internal breakpoints, these have to be deleted with an
13884          explicit breakpoint number argument.  */
13885       ALL_BREAKPOINTS (b)
13886         if (user_breakpoint_p (b))
13887           {
13888             breaks_to_delete = 1;
13889             break;
13890           }
13891
13892       /* Ask user only if there are some breakpoints to delete.  */
13893       if (!from_tty
13894           || (breaks_to_delete && query (_("Delete all breakpoints? "))))
13895         {
13896           ALL_BREAKPOINTS_SAFE (b, b_tmp)
13897             if (user_breakpoint_p (b))
13898               delete_breakpoint (b);
13899         }
13900     }
13901   else
13902     map_breakpoint_numbers (arg, do_map_delete_breakpoint, NULL);
13903 }
13904
13905 static int
13906 all_locations_are_pending (struct bp_location *loc)
13907 {
13908   for (; loc; loc = loc->next)
13909     if (!loc->shlib_disabled
13910         && !loc->pspace->executing_startup)
13911       return 0;
13912   return 1;
13913 }
13914
13915 /* Subroutine of update_breakpoint_locations to simplify it.
13916    Return non-zero if multiple fns in list LOC have the same name.
13917    Null names are ignored.  */
13918
13919 static int
13920 ambiguous_names_p (struct bp_location *loc)
13921 {
13922   struct bp_location *l;
13923   htab_t htab = htab_create_alloc (13, htab_hash_string,
13924                                    (int (*) (const void *, 
13925                                              const void *)) streq,
13926                                    NULL, xcalloc, xfree);
13927
13928   for (l = loc; l != NULL; l = l->next)
13929     {
13930       const char **slot;
13931       const char *name = l->function_name;
13932
13933       /* Allow for some names to be NULL, ignore them.  */
13934       if (name == NULL)
13935         continue;
13936
13937       slot = (const char **) htab_find_slot (htab, (const void *) name,
13938                                              INSERT);
13939       /* NOTE: We can assume slot != NULL here because xcalloc never
13940          returns NULL.  */
13941       if (*slot != NULL)
13942         {
13943           htab_delete (htab);
13944           return 1;
13945         }
13946       *slot = name;
13947     }
13948
13949   htab_delete (htab);
13950   return 0;
13951 }
13952
13953 /* When symbols change, it probably means the sources changed as well,
13954    and it might mean the static tracepoint markers are no longer at
13955    the same address or line numbers they used to be at last we
13956    checked.  Losing your static tracepoints whenever you rebuild is
13957    undesirable.  This function tries to resync/rematch gdb static
13958    tracepoints with the markers on the target, for static tracepoints
13959    that have not been set by marker id.  Static tracepoint that have
13960    been set by marker id are reset by marker id in breakpoint_re_set.
13961    The heuristic is:
13962
13963    1) For a tracepoint set at a specific address, look for a marker at
13964    the old PC.  If one is found there, assume to be the same marker.
13965    If the name / string id of the marker found is different from the
13966    previous known name, assume that means the user renamed the marker
13967    in the sources, and output a warning.
13968
13969    2) For a tracepoint set at a given line number, look for a marker
13970    at the new address of the old line number.  If one is found there,
13971    assume to be the same marker.  If the name / string id of the
13972    marker found is different from the previous known name, assume that
13973    means the user renamed the marker in the sources, and output a
13974    warning.
13975
13976    3) If a marker is no longer found at the same address or line, it
13977    may mean the marker no longer exists.  But it may also just mean
13978    the code changed a bit.  Maybe the user added a few lines of code
13979    that made the marker move up or down (in line number terms).  Ask
13980    the target for info about the marker with the string id as we knew
13981    it.  If found, update line number and address in the matching
13982    static tracepoint.  This will get confused if there's more than one
13983    marker with the same ID (possible in UST, although unadvised
13984    precisely because it confuses tools).  */
13985
13986 static struct symtab_and_line
13987 update_static_tracepoint (struct breakpoint *b, struct symtab_and_line sal)
13988 {
13989   struct tracepoint *tp = (struct tracepoint *) b;
13990   struct static_tracepoint_marker marker;
13991   CORE_ADDR pc;
13992
13993   pc = sal.pc;
13994   if (sal.line)
13995     find_line_pc (sal.symtab, sal.line, &pc);
13996
13997   if (target_static_tracepoint_marker_at (pc, &marker))
13998     {
13999       if (strcmp (tp->static_trace_marker_id, marker.str_id) != 0)
14000         warning (_("static tracepoint %d changed probed marker from %s to %s"),
14001                  b->number,
14002                  tp->static_trace_marker_id, marker.str_id);
14003
14004       xfree (tp->static_trace_marker_id);
14005       tp->static_trace_marker_id = xstrdup (marker.str_id);
14006       release_static_tracepoint_marker (&marker);
14007
14008       return sal;
14009     }
14010
14011   /* Old marker wasn't found on target at lineno.  Try looking it up
14012      by string ID.  */
14013   if (!sal.explicit_pc
14014       && sal.line != 0
14015       && sal.symtab != NULL
14016       && tp->static_trace_marker_id != NULL)
14017     {
14018       VEC(static_tracepoint_marker_p) *markers;
14019
14020       markers
14021         = target_static_tracepoint_markers_by_strid (tp->static_trace_marker_id);
14022
14023       if (!VEC_empty(static_tracepoint_marker_p, markers))
14024         {
14025           struct symtab_and_line sal2;
14026           struct symbol *sym;
14027           struct static_tracepoint_marker *tpmarker;
14028           struct ui_out *uiout = current_uiout;
14029
14030           tpmarker = VEC_index (static_tracepoint_marker_p, markers, 0);
14031
14032           xfree (tp->static_trace_marker_id);
14033           tp->static_trace_marker_id = xstrdup (tpmarker->str_id);
14034
14035           warning (_("marker for static tracepoint %d (%s) not "
14036                      "found at previous line number"),
14037                    b->number, tp->static_trace_marker_id);
14038
14039           init_sal (&sal2);
14040
14041           sal2.pc = tpmarker->address;
14042
14043           sal2 = find_pc_line (tpmarker->address, 0);
14044           sym = find_pc_sect_function (tpmarker->address, NULL);
14045           ui_out_text (uiout, "Now in ");
14046           if (sym)
14047             {
14048               ui_out_field_string (uiout, "func",
14049                                    SYMBOL_PRINT_NAME (sym));
14050               ui_out_text (uiout, " at ");
14051             }
14052           ui_out_field_string (uiout, "file",
14053                                symtab_to_filename_for_display (sal2.symtab));
14054           ui_out_text (uiout, ":");
14055
14056           if (ui_out_is_mi_like_p (uiout))
14057             {
14058               const char *fullname = symtab_to_fullname (sal2.symtab);
14059
14060               ui_out_field_string (uiout, "fullname", fullname);
14061             }
14062
14063           ui_out_field_int (uiout, "line", sal2.line);
14064           ui_out_text (uiout, "\n");
14065
14066           b->loc->line_number = sal2.line;
14067           b->loc->symtab = sym != NULL ? sal2.symtab : NULL;
14068
14069           xfree (b->addr_string);
14070           b->addr_string = xstrprintf ("%s:%d",
14071                                    symtab_to_filename_for_display (sal2.symtab),
14072                                        b->loc->line_number);
14073
14074           /* Might be nice to check if function changed, and warn if
14075              so.  */
14076
14077           release_static_tracepoint_marker (tpmarker);
14078         }
14079     }
14080   return sal;
14081 }
14082
14083 /* Returns 1 iff locations A and B are sufficiently same that
14084    we don't need to report breakpoint as changed.  */
14085
14086 static int
14087 locations_are_equal (struct bp_location *a, struct bp_location *b)
14088 {
14089   while (a && b)
14090     {
14091       if (a->address != b->address)
14092         return 0;
14093
14094       if (a->shlib_disabled != b->shlib_disabled)
14095         return 0;
14096
14097       if (a->enabled != b->enabled)
14098         return 0;
14099
14100       a = a->next;
14101       b = b->next;
14102     }
14103
14104   if ((a == NULL) != (b == NULL))
14105     return 0;
14106
14107   return 1;
14108 }
14109
14110 /* Create new breakpoint locations for B (a hardware or software breakpoint)
14111    based on SALS and SALS_END.  If SALS_END.NELTS is not zero, then B is
14112    a ranged breakpoint.  */
14113
14114 void
14115 update_breakpoint_locations (struct breakpoint *b,
14116                              struct symtabs_and_lines sals,
14117                              struct symtabs_and_lines sals_end)
14118 {
14119   int i;
14120   struct bp_location *existing_locations = b->loc;
14121
14122   if (sals_end.nelts != 0 && (sals.nelts != 1 || sals_end.nelts != 1))
14123     {
14124       /* Ranged breakpoints have only one start location and one end
14125          location.  */
14126       b->enable_state = bp_disabled;
14127       update_global_location_list (1);
14128       printf_unfiltered (_("Could not reset ranged breakpoint %d: "
14129                            "multiple locations found\n"),
14130                          b->number);
14131       return;
14132     }
14133
14134   /* If there's no new locations, and all existing locations are
14135      pending, don't do anything.  This optimizes the common case where
14136      all locations are in the same shared library, that was unloaded.
14137      We'd like to retain the location, so that when the library is
14138      loaded again, we don't loose the enabled/disabled status of the
14139      individual locations.  */
14140   if (all_locations_are_pending (existing_locations) && sals.nelts == 0)
14141     return;
14142
14143   b->loc = NULL;
14144
14145   for (i = 0; i < sals.nelts; ++i)
14146     {
14147       struct bp_location *new_loc;
14148
14149       switch_to_program_space_and_thread (sals.sals[i].pspace);
14150
14151       new_loc = add_location_to_breakpoint (b, &(sals.sals[i]));
14152
14153       /* Reparse conditions, they might contain references to the
14154          old symtab.  */
14155       if (b->cond_string != NULL)
14156         {
14157           const char *s;
14158           volatile struct gdb_exception e;
14159
14160           s = b->cond_string;
14161           TRY_CATCH (e, RETURN_MASK_ERROR)
14162             {
14163               new_loc->cond = parse_exp_1 (&s, sals.sals[i].pc,
14164                                            block_for_pc (sals.sals[i].pc), 
14165                                            0);
14166             }
14167           if (e.reason < 0)
14168             {
14169               warning (_("failed to reevaluate condition "
14170                          "for breakpoint %d: %s"), 
14171                        b->number, e.message);
14172               new_loc->enabled = 0;
14173             }
14174         }
14175
14176       if (sals_end.nelts)
14177         {
14178           CORE_ADDR end = find_breakpoint_range_end (sals_end.sals[0]);
14179
14180           new_loc->length = end - sals.sals[0].pc + 1;
14181         }
14182     }
14183
14184   /* Update locations of permanent breakpoints.  */
14185   if (b->enable_state == bp_permanent)
14186     make_breakpoint_permanent (b);
14187
14188   /* If possible, carry over 'disable' status from existing
14189      breakpoints.  */
14190   {
14191     struct bp_location *e = existing_locations;
14192     /* If there are multiple breakpoints with the same function name,
14193        e.g. for inline functions, comparing function names won't work.
14194        Instead compare pc addresses; this is just a heuristic as things
14195        may have moved, but in practice it gives the correct answer
14196        often enough until a better solution is found.  */
14197     int have_ambiguous_names = ambiguous_names_p (b->loc);
14198
14199     for (; e; e = e->next)
14200       {
14201         if (!e->enabled && e->function_name)
14202           {
14203             struct bp_location *l = b->loc;
14204             if (have_ambiguous_names)
14205               {
14206                 for (; l; l = l->next)
14207                   if (breakpoint_locations_match (e, l))
14208                     {
14209                       l->enabled = 0;
14210                       break;
14211                     }
14212               }
14213             else
14214               {
14215                 for (; l; l = l->next)
14216                   if (l->function_name
14217                       && strcmp (e->function_name, l->function_name) == 0)
14218                     {
14219                       l->enabled = 0;
14220                       break;
14221                     }
14222               }
14223           }
14224       }
14225   }
14226
14227   if (!locations_are_equal (existing_locations, b->loc))
14228     observer_notify_breakpoint_modified (b);
14229
14230   update_global_location_list (1);
14231 }
14232
14233 /* Find the SaL locations corresponding to the given ADDR_STRING.
14234    On return, FOUND will be 1 if any SaL was found, zero otherwise.  */
14235
14236 static struct symtabs_and_lines
14237 addr_string_to_sals (struct breakpoint *b, char *addr_string, int *found)
14238 {
14239   char *s;
14240   struct symtabs_and_lines sals = {0};
14241   volatile struct gdb_exception e;
14242
14243   gdb_assert (b->ops != NULL);
14244   s = addr_string;
14245
14246   TRY_CATCH (e, RETURN_MASK_ERROR)
14247     {
14248       b->ops->decode_linespec (b, &s, &sals);
14249     }
14250   if (e.reason < 0)
14251     {
14252       int not_found_and_ok = 0;
14253       /* For pending breakpoints, it's expected that parsing will
14254          fail until the right shared library is loaded.  User has
14255          already told to create pending breakpoints and don't need
14256          extra messages.  If breakpoint is in bp_shlib_disabled
14257          state, then user already saw the message about that
14258          breakpoint being disabled, and don't want to see more
14259          errors.  */
14260       if (e.error == NOT_FOUND_ERROR
14261           && (b->condition_not_parsed 
14262               || (b->loc && b->loc->shlib_disabled)
14263               || (b->loc && b->loc->pspace->executing_startup)
14264               || b->enable_state == bp_disabled))
14265         not_found_and_ok = 1;
14266
14267       if (!not_found_and_ok)
14268         {
14269           /* We surely don't want to warn about the same breakpoint
14270              10 times.  One solution, implemented here, is disable
14271              the breakpoint on error.  Another solution would be to
14272              have separate 'warning emitted' flag.  Since this
14273              happens only when a binary has changed, I don't know
14274              which approach is better.  */
14275           b->enable_state = bp_disabled;
14276           throw_exception (e);
14277         }
14278     }
14279
14280   if (e.reason == 0 || e.error != NOT_FOUND_ERROR)
14281     {
14282       int i;
14283
14284       for (i = 0; i < sals.nelts; ++i)
14285         resolve_sal_pc (&sals.sals[i]);
14286       if (b->condition_not_parsed && s && s[0])
14287         {
14288           char *cond_string, *extra_string;
14289           int thread, task;
14290
14291           find_condition_and_thread (s, sals.sals[0].pc,
14292                                      &cond_string, &thread, &task,
14293                                      &extra_string);
14294           if (cond_string)
14295             b->cond_string = cond_string;
14296           b->thread = thread;
14297           b->task = task;
14298           if (extra_string)
14299             b->extra_string = extra_string;
14300           b->condition_not_parsed = 0;
14301         }
14302
14303       if (b->type == bp_static_tracepoint && !strace_marker_p (b))
14304         sals.sals[0] = update_static_tracepoint (b, sals.sals[0]);
14305
14306       *found = 1;
14307     }
14308   else
14309     *found = 0;
14310
14311   return sals;
14312 }
14313
14314 /* The default re_set method, for typical hardware or software
14315    breakpoints.  Reevaluate the breakpoint and recreate its
14316    locations.  */
14317
14318 static void
14319 breakpoint_re_set_default (struct breakpoint *b)
14320 {
14321   int found;
14322   struct symtabs_and_lines sals, sals_end;
14323   struct symtabs_and_lines expanded = {0};
14324   struct symtabs_and_lines expanded_end = {0};
14325
14326   sals = addr_string_to_sals (b, b->addr_string, &found);
14327   if (found)
14328     {
14329       make_cleanup (xfree, sals.sals);
14330       expanded = sals;
14331     }
14332
14333   if (b->addr_string_range_end)
14334     {
14335       sals_end = addr_string_to_sals (b, b->addr_string_range_end, &found);
14336       if (found)
14337         {
14338           make_cleanup (xfree, sals_end.sals);
14339           expanded_end = sals_end;
14340         }
14341     }
14342
14343   update_breakpoint_locations (b, expanded, expanded_end);
14344 }
14345
14346 /* Default method for creating SALs from an address string.  It basically
14347    calls parse_breakpoint_sals.  Return 1 for success, zero for failure.  */
14348
14349 static void
14350 create_sals_from_address_default (char **arg,
14351                                   struct linespec_result *canonical,
14352                                   enum bptype type_wanted,
14353                                   char *addr_start, char **copy_arg)
14354 {
14355   parse_breakpoint_sals (arg, canonical);
14356 }
14357
14358 /* Call create_breakpoints_sal for the given arguments.  This is the default
14359    function for the `create_breakpoints_sal' method of
14360    breakpoint_ops.  */
14361
14362 static void
14363 create_breakpoints_sal_default (struct gdbarch *gdbarch,
14364                                 struct linespec_result *canonical,
14365                                 char *cond_string,
14366                                 char *extra_string,
14367                                 enum bptype type_wanted,
14368                                 enum bpdisp disposition,
14369                                 int thread,
14370                                 int task, int ignore_count,
14371                                 const struct breakpoint_ops *ops,
14372                                 int from_tty, int enabled,
14373                                 int internal, unsigned flags)
14374 {
14375   create_breakpoints_sal (gdbarch, canonical, cond_string,
14376                           extra_string,
14377                           type_wanted, disposition,
14378                           thread, task, ignore_count, ops, from_tty,
14379                           enabled, internal, flags);
14380 }
14381
14382 /* Decode the line represented by S by calling decode_line_full.  This is the
14383    default function for the `decode_linespec' method of breakpoint_ops.  */
14384
14385 static void
14386 decode_linespec_default (struct breakpoint *b, char **s,
14387                          struct symtabs_and_lines *sals)
14388 {
14389   struct linespec_result canonical;
14390
14391   init_linespec_result (&canonical);
14392   decode_line_full (s, DECODE_LINE_FUNFIRSTLINE,
14393                     (struct symtab *) NULL, 0,
14394                     &canonical, multiple_symbols_all,
14395                     b->filter);
14396
14397   /* We should get 0 or 1 resulting SALs.  */
14398   gdb_assert (VEC_length (linespec_sals, canonical.sals) < 2);
14399
14400   if (VEC_length (linespec_sals, canonical.sals) > 0)
14401     {
14402       struct linespec_sals *lsal;
14403
14404       lsal = VEC_index (linespec_sals, canonical.sals, 0);
14405       *sals = lsal->sals;
14406       /* Arrange it so the destructor does not free the
14407          contents.  */
14408       lsal->sals.sals = NULL;
14409     }
14410
14411   destroy_linespec_result (&canonical);
14412 }
14413
14414 /* Prepare the global context for a re-set of breakpoint B.  */
14415
14416 static struct cleanup *
14417 prepare_re_set_context (struct breakpoint *b)
14418 {
14419   struct cleanup *cleanups;
14420
14421   input_radix = b->input_radix;
14422   cleanups = save_current_space_and_thread ();
14423   if (b->pspace != NULL)
14424     switch_to_program_space_and_thread (b->pspace);
14425   set_language (b->language);
14426
14427   return cleanups;
14428 }
14429
14430 /* Reset a breakpoint given it's struct breakpoint * BINT.
14431    The value we return ends up being the return value from catch_errors.
14432    Unused in this case.  */
14433
14434 static int
14435 breakpoint_re_set_one (void *bint)
14436 {
14437   /* Get past catch_errs.  */
14438   struct breakpoint *b = (struct breakpoint *) bint;
14439   struct cleanup *cleanups;
14440
14441   cleanups = prepare_re_set_context (b);
14442   b->ops->re_set (b);
14443   do_cleanups (cleanups);
14444   return 0;
14445 }
14446
14447 /* Re-set all breakpoints after symbols have been re-loaded.  */
14448 void
14449 breakpoint_re_set (void)
14450 {
14451   struct breakpoint *b, *b_tmp;
14452   enum language save_language;
14453   int save_input_radix;
14454   struct cleanup *old_chain;
14455
14456   save_language = current_language->la_language;
14457   save_input_radix = input_radix;
14458   old_chain = save_current_program_space ();
14459
14460   ALL_BREAKPOINTS_SAFE (b, b_tmp)
14461   {
14462     /* Format possible error msg.  */
14463     char *message = xstrprintf ("Error in re-setting breakpoint %d: ",
14464                                 b->number);
14465     struct cleanup *cleanups = make_cleanup (xfree, message);
14466     catch_errors (breakpoint_re_set_one, b, message, RETURN_MASK_ALL);
14467     do_cleanups (cleanups);
14468   }
14469   set_language (save_language);
14470   input_radix = save_input_radix;
14471
14472   jit_breakpoint_re_set ();
14473
14474   do_cleanups (old_chain);
14475
14476   create_overlay_event_breakpoint ();
14477   create_longjmp_master_breakpoint ();
14478   create_std_terminate_master_breakpoint ();
14479   create_exception_master_breakpoint ();
14480 }
14481 \f
14482 /* Reset the thread number of this breakpoint:
14483
14484    - If the breakpoint is for all threads, leave it as-is.
14485    - Else, reset it to the current thread for inferior_ptid.  */
14486 void
14487 breakpoint_re_set_thread (struct breakpoint *b)
14488 {
14489   if (b->thread != -1)
14490     {
14491       if (in_thread_list (inferior_ptid))
14492         b->thread = pid_to_thread_id (inferior_ptid);
14493
14494       /* We're being called after following a fork.  The new fork is
14495          selected as current, and unless this was a vfork will have a
14496          different program space from the original thread.  Reset that
14497          as well.  */
14498       b->loc->pspace = current_program_space;
14499     }
14500 }
14501
14502 /* Set ignore-count of breakpoint number BPTNUM to COUNT.
14503    If from_tty is nonzero, it prints a message to that effect,
14504    which ends with a period (no newline).  */
14505
14506 void
14507 set_ignore_count (int bptnum, int count, int from_tty)
14508 {
14509   struct breakpoint *b;
14510
14511   if (count < 0)
14512     count = 0;
14513
14514   ALL_BREAKPOINTS (b)
14515     if (b->number == bptnum)
14516     {
14517       if (is_tracepoint (b))
14518         {
14519           if (from_tty && count != 0)
14520             printf_filtered (_("Ignore count ignored for tracepoint %d."),
14521                              bptnum);
14522           return;
14523         }
14524       
14525       b->ignore_count = count;
14526       if (from_tty)
14527         {
14528           if (count == 0)
14529             printf_filtered (_("Will stop next time "
14530                                "breakpoint %d is reached."),
14531                              bptnum);
14532           else if (count == 1)
14533             printf_filtered (_("Will ignore next crossing of breakpoint %d."),
14534                              bptnum);
14535           else
14536             printf_filtered (_("Will ignore next %d "
14537                                "crossings of breakpoint %d."),
14538                              count, bptnum);
14539         }
14540       observer_notify_breakpoint_modified (b);
14541       return;
14542     }
14543
14544   error (_("No breakpoint number %d."), bptnum);
14545 }
14546
14547 /* Command to set ignore-count of breakpoint N to COUNT.  */
14548
14549 static void
14550 ignore_command (char *args, int from_tty)
14551 {
14552   char *p = args;
14553   int num;
14554
14555   if (p == 0)
14556     error_no_arg (_("a breakpoint number"));
14557
14558   num = get_number (&p);
14559   if (num == 0)
14560     error (_("bad breakpoint number: '%s'"), args);
14561   if (*p == 0)
14562     error (_("Second argument (specified ignore-count) is missing."));
14563
14564   set_ignore_count (num,
14565                     longest_to_int (value_as_long (parse_and_eval (p))),
14566                     from_tty);
14567   if (from_tty)
14568     printf_filtered ("\n");
14569 }
14570 \f
14571 /* Call FUNCTION on each of the breakpoints
14572    whose numbers are given in ARGS.  */
14573
14574 static void
14575 map_breakpoint_numbers (char *args, void (*function) (struct breakpoint *,
14576                                                       void *),
14577                         void *data)
14578 {
14579   int num;
14580   struct breakpoint *b, *tmp;
14581   int match;
14582   struct get_number_or_range_state state;
14583
14584   if (args == 0)
14585     error_no_arg (_("one or more breakpoint numbers"));
14586
14587   init_number_or_range (&state, args);
14588
14589   while (!state.finished)
14590     {
14591       char *p = state.string;
14592
14593       match = 0;
14594
14595       num = get_number_or_range (&state);
14596       if (num == 0)
14597         {
14598           warning (_("bad breakpoint number at or near '%s'"), p);
14599         }
14600       else
14601         {
14602           ALL_BREAKPOINTS_SAFE (b, tmp)
14603             if (b->number == num)
14604               {
14605                 match = 1;
14606                 function (b, data);
14607                 break;
14608               }
14609           if (match == 0)
14610             printf_unfiltered (_("No breakpoint number %d.\n"), num);
14611         }
14612     }
14613 }
14614
14615 static struct bp_location *
14616 find_location_by_number (char *number)
14617 {
14618   char *dot = strchr (number, '.');
14619   char *p1;
14620   int bp_num;
14621   int loc_num;
14622   struct breakpoint *b;
14623   struct bp_location *loc;  
14624
14625   *dot = '\0';
14626
14627   p1 = number;
14628   bp_num = get_number (&p1);
14629   if (bp_num == 0)
14630     error (_("Bad breakpoint number '%s'"), number);
14631
14632   ALL_BREAKPOINTS (b)
14633     if (b->number == bp_num)
14634       {
14635         break;
14636       }
14637
14638   if (!b || b->number != bp_num)
14639     error (_("Bad breakpoint number '%s'"), number);
14640   
14641   p1 = dot+1;
14642   loc_num = get_number (&p1);
14643   if (loc_num == 0)
14644     error (_("Bad breakpoint location number '%s'"), number);
14645
14646   --loc_num;
14647   loc = b->loc;
14648   for (;loc_num && loc; --loc_num, loc = loc->next)
14649     ;
14650   if (!loc)
14651     error (_("Bad breakpoint location number '%s'"), dot+1);
14652     
14653   return loc;  
14654 }
14655
14656
14657 /* Set ignore-count of breakpoint number BPTNUM to COUNT.
14658    If from_tty is nonzero, it prints a message to that effect,
14659    which ends with a period (no newline).  */
14660
14661 void
14662 disable_breakpoint (struct breakpoint *bpt)
14663 {
14664   /* Never disable a watchpoint scope breakpoint; we want to
14665      hit them when we leave scope so we can delete both the
14666      watchpoint and its scope breakpoint at that time.  */
14667   if (bpt->type == bp_watchpoint_scope)
14668     return;
14669
14670   /* You can't disable permanent breakpoints.  */
14671   if (bpt->enable_state == bp_permanent)
14672     return;
14673
14674   bpt->enable_state = bp_disabled;
14675
14676   /* Mark breakpoint locations modified.  */
14677   mark_breakpoint_modified (bpt);
14678
14679   if (target_supports_enable_disable_tracepoint ()
14680       && current_trace_status ()->running && is_tracepoint (bpt))
14681     {
14682       struct bp_location *location;
14683      
14684       for (location = bpt->loc; location; location = location->next)
14685         target_disable_tracepoint (location);
14686     }
14687
14688   update_global_location_list (0);
14689
14690   observer_notify_breakpoint_modified (bpt);
14691 }
14692
14693 /* A callback for iterate_over_related_breakpoints.  */
14694
14695 static void
14696 do_disable_breakpoint (struct breakpoint *b, void *ignore)
14697 {
14698   disable_breakpoint (b);
14699 }
14700
14701 /* A callback for map_breakpoint_numbers that calls
14702    disable_breakpoint.  */
14703
14704 static void
14705 do_map_disable_breakpoint (struct breakpoint *b, void *ignore)
14706 {
14707   iterate_over_related_breakpoints (b, do_disable_breakpoint, NULL);
14708 }
14709
14710 static void
14711 disable_command (char *args, int from_tty)
14712 {
14713   if (args == 0)
14714     {
14715       struct breakpoint *bpt;
14716
14717       ALL_BREAKPOINTS (bpt)
14718         if (user_breakpoint_p (bpt))
14719           disable_breakpoint (bpt);
14720     }
14721   else
14722     {
14723       char *num = extract_arg (&args);
14724
14725       while (num)
14726         {
14727           if (strchr (num, '.'))
14728             {
14729               struct bp_location *loc = find_location_by_number (num);
14730
14731               if (loc)
14732                 {
14733                   if (loc->enabled)
14734                     {
14735                       loc->enabled = 0;
14736                       mark_breakpoint_location_modified (loc);
14737                     }
14738                   if (target_supports_enable_disable_tracepoint ()
14739                       && current_trace_status ()->running && loc->owner
14740                       && is_tracepoint (loc->owner))
14741                     target_disable_tracepoint (loc);
14742                 }
14743               update_global_location_list (0);
14744             }
14745           else
14746             map_breakpoint_numbers (num, do_map_disable_breakpoint, NULL);
14747           num = extract_arg (&args);
14748         }
14749     }
14750 }
14751
14752 static void
14753 enable_breakpoint_disp (struct breakpoint *bpt, enum bpdisp disposition,
14754                         int count)
14755 {
14756   int target_resources_ok;
14757
14758   if (bpt->type == bp_hardware_breakpoint)
14759     {
14760       int i;
14761       i = hw_breakpoint_used_count ();
14762       target_resources_ok = 
14763         target_can_use_hardware_watchpoint (bp_hardware_breakpoint, 
14764                                             i + 1, 0);
14765       if (target_resources_ok == 0)
14766         error (_("No hardware breakpoint support in the target."));
14767       else if (target_resources_ok < 0)
14768         error (_("Hardware breakpoints used exceeds limit."));
14769     }
14770
14771   if (is_watchpoint (bpt))
14772     {
14773       /* Initialize it just to avoid a GCC false warning.  */
14774       enum enable_state orig_enable_state = 0;
14775       volatile struct gdb_exception e;
14776
14777       TRY_CATCH (e, RETURN_MASK_ALL)
14778         {
14779           struct watchpoint *w = (struct watchpoint *) bpt;
14780
14781           orig_enable_state = bpt->enable_state;
14782           bpt->enable_state = bp_enabled;
14783           update_watchpoint (w, 1 /* reparse */);
14784         }
14785       if (e.reason < 0)
14786         {
14787           bpt->enable_state = orig_enable_state;
14788           exception_fprintf (gdb_stderr, e, _("Cannot enable watchpoint %d: "),
14789                              bpt->number);
14790           return;
14791         }
14792     }
14793
14794   if (bpt->enable_state != bp_permanent)
14795     bpt->enable_state = bp_enabled;
14796
14797   bpt->enable_state = bp_enabled;
14798
14799   /* Mark breakpoint locations modified.  */
14800   mark_breakpoint_modified (bpt);
14801
14802   if (target_supports_enable_disable_tracepoint ()
14803       && current_trace_status ()->running && is_tracepoint (bpt))
14804     {
14805       struct bp_location *location;
14806
14807       for (location = bpt->loc; location; location = location->next)
14808         target_enable_tracepoint (location);
14809     }
14810
14811   bpt->disposition = disposition;
14812   bpt->enable_count = count;
14813   update_global_location_list (1);
14814
14815   observer_notify_breakpoint_modified (bpt);
14816 }
14817
14818
14819 void
14820 enable_breakpoint (struct breakpoint *bpt)
14821 {
14822   enable_breakpoint_disp (bpt, bpt->disposition, 0);
14823 }
14824
14825 static void
14826 do_enable_breakpoint (struct breakpoint *bpt, void *arg)
14827 {
14828   enable_breakpoint (bpt);
14829 }
14830
14831 /* A callback for map_breakpoint_numbers that calls
14832    enable_breakpoint.  */
14833
14834 static void
14835 do_map_enable_breakpoint (struct breakpoint *b, void *ignore)
14836 {
14837   iterate_over_related_breakpoints (b, do_enable_breakpoint, NULL);
14838 }
14839
14840 /* The enable command enables the specified breakpoints (or all defined
14841    breakpoints) so they once again become (or continue to be) effective
14842    in stopping the inferior.  */
14843
14844 static void
14845 enable_command (char *args, int from_tty)
14846 {
14847   if (args == 0)
14848     {
14849       struct breakpoint *bpt;
14850
14851       ALL_BREAKPOINTS (bpt)
14852         if (user_breakpoint_p (bpt))
14853           enable_breakpoint (bpt);
14854     }
14855   else
14856     {
14857       char *num = extract_arg (&args);
14858
14859       while (num)
14860         {
14861           if (strchr (num, '.'))
14862             {
14863               struct bp_location *loc = find_location_by_number (num);
14864
14865               if (loc)
14866                 {
14867                   if (!loc->enabled)
14868                     {
14869                       loc->enabled = 1;
14870                       mark_breakpoint_location_modified (loc);
14871                     }
14872                   if (target_supports_enable_disable_tracepoint ()
14873                       && current_trace_status ()->running && loc->owner
14874                       && is_tracepoint (loc->owner))
14875                     target_enable_tracepoint (loc);
14876                 }
14877               update_global_location_list (1);
14878             }
14879           else
14880             map_breakpoint_numbers (num, do_map_enable_breakpoint, NULL);
14881           num = extract_arg (&args);
14882         }
14883     }
14884 }
14885
14886 /* This struct packages up disposition data for application to multiple
14887    breakpoints.  */
14888
14889 struct disp_data
14890 {
14891   enum bpdisp disp;
14892   int count;
14893 };
14894
14895 static void
14896 do_enable_breakpoint_disp (struct breakpoint *bpt, void *arg)
14897 {
14898   struct disp_data disp_data = *(struct disp_data *) arg;
14899
14900   enable_breakpoint_disp (bpt, disp_data.disp, disp_data.count);
14901 }
14902
14903 static void
14904 do_map_enable_once_breakpoint (struct breakpoint *bpt, void *ignore)
14905 {
14906   struct disp_data disp = { disp_disable, 1 };
14907
14908   iterate_over_related_breakpoints (bpt, do_enable_breakpoint_disp, &disp);
14909 }
14910
14911 static void
14912 enable_once_command (char *args, int from_tty)
14913 {
14914   map_breakpoint_numbers (args, do_map_enable_once_breakpoint, NULL);
14915 }
14916
14917 static void
14918 do_map_enable_count_breakpoint (struct breakpoint *bpt, void *countptr)
14919 {
14920   struct disp_data disp = { disp_disable, *(int *) countptr };
14921
14922   iterate_over_related_breakpoints (bpt, do_enable_breakpoint_disp, &disp);
14923 }
14924
14925 static void
14926 enable_count_command (char *args, int from_tty)
14927 {
14928   int count = get_number (&args);
14929
14930   map_breakpoint_numbers (args, do_map_enable_count_breakpoint, &count);
14931 }
14932
14933 static void
14934 do_map_enable_delete_breakpoint (struct breakpoint *bpt, void *ignore)
14935 {
14936   struct disp_data disp = { disp_del, 1 };
14937
14938   iterate_over_related_breakpoints (bpt, do_enable_breakpoint_disp, &disp);
14939 }
14940
14941 static void
14942 enable_delete_command (char *args, int from_tty)
14943 {
14944   map_breakpoint_numbers (args, do_map_enable_delete_breakpoint, NULL);
14945 }
14946 \f
14947 static void
14948 set_breakpoint_cmd (char *args, int from_tty)
14949 {
14950 }
14951
14952 static void
14953 show_breakpoint_cmd (char *args, int from_tty)
14954 {
14955 }
14956
14957 /* Invalidate last known value of any hardware watchpoint if
14958    the memory which that value represents has been written to by
14959    GDB itself.  */
14960
14961 static void
14962 invalidate_bp_value_on_memory_change (struct inferior *inferior,
14963                                       CORE_ADDR addr, ssize_t len,
14964                                       const bfd_byte *data)
14965 {
14966   struct breakpoint *bp;
14967
14968   ALL_BREAKPOINTS (bp)
14969     if (bp->enable_state == bp_enabled
14970         && bp->type == bp_hardware_watchpoint)
14971       {
14972         struct watchpoint *wp = (struct watchpoint *) bp;
14973
14974         if (wp->val_valid && wp->val)
14975           {
14976             struct bp_location *loc;
14977
14978             for (loc = bp->loc; loc != NULL; loc = loc->next)
14979               if (loc->loc_type == bp_loc_hardware_watchpoint
14980                   && loc->address + loc->length > addr
14981                   && addr + len > loc->address)
14982                 {
14983                   value_free (wp->val);
14984                   wp->val = NULL;
14985                   wp->val_valid = 0;
14986                 }
14987           }
14988       }
14989 }
14990
14991 /* Create and insert a raw software breakpoint at PC.  Return an
14992    identifier, which should be used to remove the breakpoint later.
14993    In general, places which call this should be using something on the
14994    breakpoint chain instead; this function should be eliminated
14995    someday.  */
14996
14997 void *
14998 deprecated_insert_raw_breakpoint (struct gdbarch *gdbarch,
14999                                   struct address_space *aspace, CORE_ADDR pc)
15000 {
15001   struct bp_target_info *bp_tgt;
15002
15003   bp_tgt = XCNEW (struct bp_target_info);
15004
15005   bp_tgt->placed_address_space = aspace;
15006   bp_tgt->placed_address = pc;
15007
15008   if (target_insert_breakpoint (gdbarch, bp_tgt) != 0)
15009     {
15010       /* Could not insert the breakpoint.  */
15011       xfree (bp_tgt);
15012       return NULL;
15013     }
15014
15015   return bp_tgt;
15016 }
15017
15018 /* Remove a breakpoint BP inserted by
15019    deprecated_insert_raw_breakpoint.  */
15020
15021 int
15022 deprecated_remove_raw_breakpoint (struct gdbarch *gdbarch, void *bp)
15023 {
15024   struct bp_target_info *bp_tgt = bp;
15025   int ret;
15026
15027   ret = target_remove_breakpoint (gdbarch, bp_tgt);
15028   xfree (bp_tgt);
15029
15030   return ret;
15031 }
15032
15033 /* One (or perhaps two) breakpoints used for software single
15034    stepping.  */
15035
15036 static void *single_step_breakpoints[2];
15037 static struct gdbarch *single_step_gdbarch[2];
15038
15039 /* Create and insert a breakpoint for software single step.  */
15040
15041 void
15042 insert_single_step_breakpoint (struct gdbarch *gdbarch,
15043                                struct address_space *aspace, 
15044                                CORE_ADDR next_pc)
15045 {
15046   void **bpt_p;
15047
15048   if (single_step_breakpoints[0] == NULL)
15049     {
15050       bpt_p = &single_step_breakpoints[0];
15051       single_step_gdbarch[0] = gdbarch;
15052     }
15053   else
15054     {
15055       gdb_assert (single_step_breakpoints[1] == NULL);
15056       bpt_p = &single_step_breakpoints[1];
15057       single_step_gdbarch[1] = gdbarch;
15058     }
15059
15060   /* NOTE drow/2006-04-11: A future improvement to this function would
15061      be to only create the breakpoints once, and actually put them on
15062      the breakpoint chain.  That would let us use set_raw_breakpoint.
15063      We could adjust the addresses each time they were needed.  Doing
15064      this requires corresponding changes elsewhere where single step
15065      breakpoints are handled, however.  So, for now, we use this.  */
15066
15067   *bpt_p = deprecated_insert_raw_breakpoint (gdbarch, aspace, next_pc);
15068   if (*bpt_p == NULL)
15069     error (_("Could not insert single-step breakpoint at %s"),
15070              paddress (gdbarch, next_pc));
15071 }
15072
15073 /* Check if the breakpoints used for software single stepping
15074    were inserted or not.  */
15075
15076 int
15077 single_step_breakpoints_inserted (void)
15078 {
15079   return (single_step_breakpoints[0] != NULL
15080           || single_step_breakpoints[1] != NULL);
15081 }
15082
15083 /* Remove and delete any breakpoints used for software single step.  */
15084
15085 void
15086 remove_single_step_breakpoints (void)
15087 {
15088   gdb_assert (single_step_breakpoints[0] != NULL);
15089
15090   /* See insert_single_step_breakpoint for more about this deprecated
15091      call.  */
15092   deprecated_remove_raw_breakpoint (single_step_gdbarch[0],
15093                                     single_step_breakpoints[0]);
15094   single_step_gdbarch[0] = NULL;
15095   single_step_breakpoints[0] = NULL;
15096
15097   if (single_step_breakpoints[1] != NULL)
15098     {
15099       deprecated_remove_raw_breakpoint (single_step_gdbarch[1],
15100                                         single_step_breakpoints[1]);
15101       single_step_gdbarch[1] = NULL;
15102       single_step_breakpoints[1] = NULL;
15103     }
15104 }
15105
15106 /* Delete software single step breakpoints without removing them from
15107    the inferior.  This is intended to be used if the inferior's address
15108    space where they were inserted is already gone, e.g. after exit or
15109    exec.  */
15110
15111 void
15112 cancel_single_step_breakpoints (void)
15113 {
15114   int i;
15115
15116   for (i = 0; i < 2; i++)
15117     if (single_step_breakpoints[i])
15118       {
15119         xfree (single_step_breakpoints[i]);
15120         single_step_breakpoints[i] = NULL;
15121         single_step_gdbarch[i] = NULL;
15122       }
15123 }
15124
15125 /* Detach software single-step breakpoints from INFERIOR_PTID without
15126    removing them.  */
15127
15128 static void
15129 detach_single_step_breakpoints (void)
15130 {
15131   int i;
15132
15133   for (i = 0; i < 2; i++)
15134     if (single_step_breakpoints[i])
15135       target_remove_breakpoint (single_step_gdbarch[i],
15136                                 single_step_breakpoints[i]);
15137 }
15138
15139 /* Check whether a software single-step breakpoint is inserted at
15140    PC.  */
15141
15142 static int
15143 single_step_breakpoint_inserted_here_p (struct address_space *aspace, 
15144                                         CORE_ADDR pc)
15145 {
15146   int i;
15147
15148   for (i = 0; i < 2; i++)
15149     {
15150       struct bp_target_info *bp_tgt = single_step_breakpoints[i];
15151       if (bp_tgt
15152           && breakpoint_address_match (bp_tgt->placed_address_space,
15153                                        bp_tgt->placed_address,
15154                                        aspace, pc))
15155         return 1;
15156     }
15157
15158   return 0;
15159 }
15160
15161 /* Returns 0 if 'bp' is NOT a syscall catchpoint,
15162    non-zero otherwise.  */
15163 static int
15164 is_syscall_catchpoint_enabled (struct breakpoint *bp)
15165 {
15166   if (syscall_catchpoint_p (bp)
15167       && bp->enable_state != bp_disabled
15168       && bp->enable_state != bp_call_disabled)
15169     return 1;
15170   else
15171     return 0;
15172 }
15173
15174 int
15175 catch_syscall_enabled (void)
15176 {
15177   struct catch_syscall_inferior_data *inf_data
15178     = get_catch_syscall_inferior_data (current_inferior ());
15179
15180   return inf_data->total_syscalls_count != 0;
15181 }
15182
15183 int
15184 catching_syscall_number (int syscall_number)
15185 {
15186   struct breakpoint *bp;
15187
15188   ALL_BREAKPOINTS (bp)
15189     if (is_syscall_catchpoint_enabled (bp))
15190       {
15191         struct syscall_catchpoint *c = (struct syscall_catchpoint *) bp;
15192
15193         if (c->syscalls_to_be_caught)
15194           {
15195             int i, iter;
15196             for (i = 0;
15197                  VEC_iterate (int, c->syscalls_to_be_caught, i, iter);
15198                  i++)
15199               if (syscall_number == iter)
15200                 return 1;
15201           }
15202         else
15203           return 1;
15204       }
15205
15206   return 0;
15207 }
15208
15209 /* Complete syscall names.  Used by "catch syscall".  */
15210 static VEC (char_ptr) *
15211 catch_syscall_completer (struct cmd_list_element *cmd,
15212                          const char *text, const char *word)
15213 {
15214   const char **list = get_syscall_names ();
15215   VEC (char_ptr) *retlist
15216     = (list == NULL) ? NULL : complete_on_enum (list, word, word);
15217
15218   xfree (list);
15219   return retlist;
15220 }
15221
15222 /* Tracepoint-specific operations.  */
15223
15224 /* Set tracepoint count to NUM.  */
15225 static void
15226 set_tracepoint_count (int num)
15227 {
15228   tracepoint_count = num;
15229   set_internalvar_integer (lookup_internalvar ("tpnum"), num);
15230 }
15231
15232 static void
15233 trace_command (char *arg, int from_tty)
15234 {
15235   struct breakpoint_ops *ops;
15236   const char *arg_cp = arg;
15237
15238   if (arg && probe_linespec_to_ops (&arg_cp))
15239     ops = &tracepoint_probe_breakpoint_ops;
15240   else
15241     ops = &tracepoint_breakpoint_ops;
15242
15243   create_breakpoint (get_current_arch (),
15244                      arg,
15245                      NULL, 0, NULL, 1 /* parse arg */,
15246                      0 /* tempflag */,
15247                      bp_tracepoint /* type_wanted */,
15248                      0 /* Ignore count */,
15249                      pending_break_support,
15250                      ops,
15251                      from_tty,
15252                      1 /* enabled */,
15253                      0 /* internal */, 0);
15254 }
15255
15256 static void
15257 ftrace_command (char *arg, int from_tty)
15258 {
15259   create_breakpoint (get_current_arch (),
15260                      arg,
15261                      NULL, 0, NULL, 1 /* parse arg */,
15262                      0 /* tempflag */,
15263                      bp_fast_tracepoint /* type_wanted */,
15264                      0 /* Ignore count */,
15265                      pending_break_support,
15266                      &tracepoint_breakpoint_ops,
15267                      from_tty,
15268                      1 /* enabled */,
15269                      0 /* internal */, 0);
15270 }
15271
15272 /* strace command implementation.  Creates a static tracepoint.  */
15273
15274 static void
15275 strace_command (char *arg, int from_tty)
15276 {
15277   struct breakpoint_ops *ops;
15278
15279   /* Decide if we are dealing with a static tracepoint marker (`-m'),
15280      or with a normal static tracepoint.  */
15281   if (arg && strncmp (arg, "-m", 2) == 0 && isspace (arg[2]))
15282     ops = &strace_marker_breakpoint_ops;
15283   else
15284     ops = &tracepoint_breakpoint_ops;
15285
15286   create_breakpoint (get_current_arch (),
15287                      arg,
15288                      NULL, 0, NULL, 1 /* parse arg */,
15289                      0 /* tempflag */,
15290                      bp_static_tracepoint /* type_wanted */,
15291                      0 /* Ignore count */,
15292                      pending_break_support,
15293                      ops,
15294                      from_tty,
15295                      1 /* enabled */,
15296                      0 /* internal */, 0);
15297 }
15298
15299 /* Set up a fake reader function that gets command lines from a linked
15300    list that was acquired during tracepoint uploading.  */
15301
15302 static struct uploaded_tp *this_utp;
15303 static int next_cmd;
15304
15305 static char *
15306 read_uploaded_action (void)
15307 {
15308   char *rslt;
15309
15310   VEC_iterate (char_ptr, this_utp->cmd_strings, next_cmd, rslt);
15311
15312   next_cmd++;
15313
15314   return rslt;
15315 }
15316
15317 /* Given information about a tracepoint as recorded on a target (which
15318    can be either a live system or a trace file), attempt to create an
15319    equivalent GDB tracepoint.  This is not a reliable process, since
15320    the target does not necessarily have all the information used when
15321    the tracepoint was originally defined.  */
15322   
15323 struct tracepoint *
15324 create_tracepoint_from_upload (struct uploaded_tp *utp)
15325 {
15326   char *addr_str, small_buf[100];
15327   struct tracepoint *tp;
15328
15329   if (utp->at_string)
15330     addr_str = utp->at_string;
15331   else
15332     {
15333       /* In the absence of a source location, fall back to raw
15334          address.  Since there is no way to confirm that the address
15335          means the same thing as when the trace was started, warn the
15336          user.  */
15337       warning (_("Uploaded tracepoint %d has no "
15338                  "source location, using raw address"),
15339                utp->number);
15340       xsnprintf (small_buf, sizeof (small_buf), "*%s", hex_string (utp->addr));
15341       addr_str = small_buf;
15342     }
15343
15344   /* There's not much we can do with a sequence of bytecodes.  */
15345   if (utp->cond && !utp->cond_string)
15346     warning (_("Uploaded tracepoint %d condition "
15347                "has no source form, ignoring it"),
15348              utp->number);
15349
15350   if (!create_breakpoint (get_current_arch (),
15351                           addr_str,
15352                           utp->cond_string, -1, NULL,
15353                           0 /* parse cond/thread */,
15354                           0 /* tempflag */,
15355                           utp->type /* type_wanted */,
15356                           0 /* Ignore count */,
15357                           pending_break_support,
15358                           &tracepoint_breakpoint_ops,
15359                           0 /* from_tty */,
15360                           utp->enabled /* enabled */,
15361                           0 /* internal */,
15362                           CREATE_BREAKPOINT_FLAGS_INSERTED))
15363     return NULL;
15364
15365   /* Get the tracepoint we just created.  */
15366   tp = get_tracepoint (tracepoint_count);
15367   gdb_assert (tp != NULL);
15368
15369   if (utp->pass > 0)
15370     {
15371       xsnprintf (small_buf, sizeof (small_buf), "%d %d", utp->pass,
15372                  tp->base.number);
15373
15374       trace_pass_command (small_buf, 0);
15375     }
15376
15377   /* If we have uploaded versions of the original commands, set up a
15378      special-purpose "reader" function and call the usual command line
15379      reader, then pass the result to the breakpoint command-setting
15380      function.  */
15381   if (!VEC_empty (char_ptr, utp->cmd_strings))
15382     {
15383       struct command_line *cmd_list;
15384
15385       this_utp = utp;
15386       next_cmd = 0;
15387
15388       cmd_list = read_command_lines_1 (read_uploaded_action, 1, NULL, NULL);
15389
15390       breakpoint_set_commands (&tp->base, cmd_list);
15391     }
15392   else if (!VEC_empty (char_ptr, utp->actions)
15393            || !VEC_empty (char_ptr, utp->step_actions))
15394     warning (_("Uploaded tracepoint %d actions "
15395                "have no source form, ignoring them"),
15396              utp->number);
15397
15398   /* Copy any status information that might be available.  */
15399   tp->base.hit_count = utp->hit_count;
15400   tp->traceframe_usage = utp->traceframe_usage;
15401
15402   return tp;
15403 }
15404   
15405 /* Print information on tracepoint number TPNUM_EXP, or all if
15406    omitted.  */
15407
15408 static void
15409 tracepoints_info (char *args, int from_tty)
15410 {
15411   struct ui_out *uiout = current_uiout;
15412   int num_printed;
15413
15414   num_printed = breakpoint_1 (args, 0, is_tracepoint);
15415
15416   if (num_printed == 0)
15417     {
15418       if (args == NULL || *args == '\0')
15419         ui_out_message (uiout, 0, "No tracepoints.\n");
15420       else
15421         ui_out_message (uiout, 0, "No tracepoint matching '%s'.\n", args);
15422     }
15423
15424   default_collect_info ();
15425 }
15426
15427 /* The 'enable trace' command enables tracepoints.
15428    Not supported by all targets.  */
15429 static void
15430 enable_trace_command (char *args, int from_tty)
15431 {
15432   enable_command (args, from_tty);
15433 }
15434
15435 /* The 'disable trace' command disables tracepoints.
15436    Not supported by all targets.  */
15437 static void
15438 disable_trace_command (char *args, int from_tty)
15439 {
15440   disable_command (args, from_tty);
15441 }
15442
15443 /* Remove a tracepoint (or all if no argument).  */
15444 static void
15445 delete_trace_command (char *arg, int from_tty)
15446 {
15447   struct breakpoint *b, *b_tmp;
15448
15449   dont_repeat ();
15450
15451   if (arg == 0)
15452     {
15453       int breaks_to_delete = 0;
15454
15455       /* Delete all breakpoints if no argument.
15456          Do not delete internal or call-dummy breakpoints, these
15457          have to be deleted with an explicit breakpoint number 
15458          argument.  */
15459       ALL_TRACEPOINTS (b)
15460         if (is_tracepoint (b) && user_breakpoint_p (b))
15461           {
15462             breaks_to_delete = 1;
15463             break;
15464           }
15465
15466       /* Ask user only if there are some breakpoints to delete.  */
15467       if (!from_tty
15468           || (breaks_to_delete && query (_("Delete all tracepoints? "))))
15469         {
15470           ALL_BREAKPOINTS_SAFE (b, b_tmp)
15471             if (is_tracepoint (b) && user_breakpoint_p (b))
15472               delete_breakpoint (b);
15473         }
15474     }
15475   else
15476     map_breakpoint_numbers (arg, do_map_delete_breakpoint, NULL);
15477 }
15478
15479 /* Helper function for trace_pass_command.  */
15480
15481 static void
15482 trace_pass_set_count (struct tracepoint *tp, int count, int from_tty)
15483 {
15484   tp->pass_count = count;
15485   observer_notify_breakpoint_modified (&tp->base);
15486   if (from_tty)
15487     printf_filtered (_("Setting tracepoint %d's passcount to %d\n"),
15488                      tp->base.number, count);
15489 }
15490
15491 /* Set passcount for tracepoint.
15492
15493    First command argument is passcount, second is tracepoint number.
15494    If tracepoint number omitted, apply to most recently defined.
15495    Also accepts special argument "all".  */
15496
15497 static void
15498 trace_pass_command (char *args, int from_tty)
15499 {
15500   struct tracepoint *t1;
15501   unsigned int count;
15502
15503   if (args == 0 || *args == 0)
15504     error (_("passcount command requires an "
15505              "argument (count + optional TP num)"));
15506
15507   count = strtoul (args, &args, 10);    /* Count comes first, then TP num.  */
15508
15509   args = skip_spaces (args);
15510   if (*args && strncasecmp (args, "all", 3) == 0)
15511     {
15512       struct breakpoint *b;
15513
15514       args += 3;                        /* Skip special argument "all".  */
15515       if (*args)
15516         error (_("Junk at end of arguments."));
15517
15518       ALL_TRACEPOINTS (b)
15519       {
15520         t1 = (struct tracepoint *) b;
15521         trace_pass_set_count (t1, count, from_tty);
15522       }
15523     }
15524   else if (*args == '\0')
15525     {
15526       t1 = get_tracepoint_by_number (&args, NULL, 1);
15527       if (t1)
15528         trace_pass_set_count (t1, count, from_tty);
15529     }
15530   else
15531     {
15532       struct get_number_or_range_state state;
15533
15534       init_number_or_range (&state, args);
15535       while (!state.finished)
15536         {
15537           t1 = get_tracepoint_by_number (&args, &state, 1);
15538           if (t1)
15539             trace_pass_set_count (t1, count, from_tty);
15540         }
15541     }
15542 }
15543
15544 struct tracepoint *
15545 get_tracepoint (int num)
15546 {
15547   struct breakpoint *t;
15548
15549   ALL_TRACEPOINTS (t)
15550     if (t->number == num)
15551       return (struct tracepoint *) t;
15552
15553   return NULL;
15554 }
15555
15556 /* Find the tracepoint with the given target-side number (which may be
15557    different from the tracepoint number after disconnecting and
15558    reconnecting).  */
15559
15560 struct tracepoint *
15561 get_tracepoint_by_number_on_target (int num)
15562 {
15563   struct breakpoint *b;
15564
15565   ALL_TRACEPOINTS (b)
15566     {
15567       struct tracepoint *t = (struct tracepoint *) b;
15568
15569       if (t->number_on_target == num)
15570         return t;
15571     }
15572
15573   return NULL;
15574 }
15575
15576 /* Utility: parse a tracepoint number and look it up in the list.
15577    If STATE is not NULL, use, get_number_or_range_state and ignore ARG.
15578    If OPTIONAL_P is true, then if the argument is missing, the most
15579    recent tracepoint (tracepoint_count) is returned.  */
15580 struct tracepoint *
15581 get_tracepoint_by_number (char **arg,
15582                           struct get_number_or_range_state *state,
15583                           int optional_p)
15584 {
15585   struct breakpoint *t;
15586   int tpnum;
15587   char *instring = arg == NULL ? NULL : *arg;
15588
15589   if (state)
15590     {
15591       gdb_assert (!state->finished);
15592       tpnum = get_number_or_range (state);
15593     }
15594   else if (arg == NULL || *arg == NULL || ! **arg)
15595     {
15596       if (optional_p)
15597         tpnum = tracepoint_count;
15598       else
15599         error_no_arg (_("tracepoint number"));
15600     }
15601   else
15602     tpnum = get_number (arg);
15603
15604   if (tpnum <= 0)
15605     {
15606       if (instring && *instring)
15607         printf_filtered (_("bad tracepoint number at or near '%s'\n"), 
15608                          instring);
15609       else
15610         printf_filtered (_("Tracepoint argument missing "
15611                            "and no previous tracepoint\n"));
15612       return NULL;
15613     }
15614
15615   ALL_TRACEPOINTS (t)
15616     if (t->number == tpnum)
15617     {
15618       return (struct tracepoint *) t;
15619     }
15620
15621   printf_unfiltered ("No tracepoint number %d.\n", tpnum);
15622   return NULL;
15623 }
15624
15625 void
15626 print_recreate_thread (struct breakpoint *b, struct ui_file *fp)
15627 {
15628   if (b->thread != -1)
15629     fprintf_unfiltered (fp, " thread %d", b->thread);
15630
15631   if (b->task != 0)
15632     fprintf_unfiltered (fp, " task %d", b->task);
15633
15634   fprintf_unfiltered (fp, "\n");
15635 }
15636
15637 /* Save information on user settable breakpoints (watchpoints, etc) to
15638    a new script file named FILENAME.  If FILTER is non-NULL, call it
15639    on each breakpoint and only include the ones for which it returns
15640    non-zero.  */
15641
15642 static void
15643 save_breakpoints (char *filename, int from_tty,
15644                   int (*filter) (const struct breakpoint *))
15645 {
15646   struct breakpoint *tp;
15647   int any = 0;
15648   struct cleanup *cleanup;
15649   struct ui_file *fp;
15650   int extra_trace_bits = 0;
15651
15652   if (filename == 0 || *filename == 0)
15653     error (_("Argument required (file name in which to save)"));
15654
15655   /* See if we have anything to save.  */
15656   ALL_BREAKPOINTS (tp)
15657   {
15658     /* Skip internal and momentary breakpoints.  */
15659     if (!user_breakpoint_p (tp))
15660       continue;
15661
15662     /* If we have a filter, only save the breakpoints it accepts.  */
15663     if (filter && !filter (tp))
15664       continue;
15665
15666     any = 1;
15667
15668     if (is_tracepoint (tp))
15669       {
15670         extra_trace_bits = 1;
15671
15672         /* We can stop searching.  */
15673         break;
15674       }
15675   }
15676
15677   if (!any)
15678     {
15679       warning (_("Nothing to save."));
15680       return;
15681     }
15682
15683   filename = tilde_expand (filename);
15684   cleanup = make_cleanup (xfree, filename);
15685   fp = gdb_fopen (filename, "w");
15686   if (!fp)
15687     error (_("Unable to open file '%s' for saving (%s)"),
15688            filename, safe_strerror (errno));
15689   make_cleanup_ui_file_delete (fp);
15690
15691   if (extra_trace_bits)
15692     save_trace_state_variables (fp);
15693
15694   ALL_BREAKPOINTS (tp)
15695   {
15696     /* Skip internal and momentary breakpoints.  */
15697     if (!user_breakpoint_p (tp))
15698       continue;
15699
15700     /* If we have a filter, only save the breakpoints it accepts.  */
15701     if (filter && !filter (tp))
15702       continue;
15703
15704     tp->ops->print_recreate (tp, fp);
15705
15706     /* Note, we can't rely on tp->number for anything, as we can't
15707        assume the recreated breakpoint numbers will match.  Use $bpnum
15708        instead.  */
15709
15710     if (tp->cond_string)
15711       fprintf_unfiltered (fp, "  condition $bpnum %s\n", tp->cond_string);
15712
15713     if (tp->ignore_count)
15714       fprintf_unfiltered (fp, "  ignore $bpnum %d\n", tp->ignore_count);
15715
15716     if (tp->type != bp_dprintf && tp->commands)
15717       {
15718         volatile struct gdb_exception ex;       
15719
15720         fprintf_unfiltered (fp, "  commands\n");
15721         
15722         ui_out_redirect (current_uiout, fp);
15723         TRY_CATCH (ex, RETURN_MASK_ALL)
15724           {
15725             print_command_lines (current_uiout, tp->commands->commands, 2);
15726           }
15727         ui_out_redirect (current_uiout, NULL);
15728
15729         if (ex.reason < 0)
15730           throw_exception (ex);
15731
15732         fprintf_unfiltered (fp, "  end\n");
15733       }
15734
15735     if (tp->enable_state == bp_disabled)
15736       fprintf_unfiltered (fp, "disable\n");
15737
15738     /* If this is a multi-location breakpoint, check if the locations
15739        should be individually disabled.  Watchpoint locations are
15740        special, and not user visible.  */
15741     if (!is_watchpoint (tp) && tp->loc && tp->loc->next)
15742       {
15743         struct bp_location *loc;
15744         int n = 1;
15745
15746         for (loc = tp->loc; loc != NULL; loc = loc->next, n++)
15747           if (!loc->enabled)
15748             fprintf_unfiltered (fp, "disable $bpnum.%d\n", n);
15749       }
15750   }
15751
15752   if (extra_trace_bits && *default_collect)
15753     fprintf_unfiltered (fp, "set default-collect %s\n", default_collect);
15754
15755   if (from_tty)
15756     printf_filtered (_("Saved to file '%s'.\n"), filename);
15757   do_cleanups (cleanup);
15758 }
15759
15760 /* The `save breakpoints' command.  */
15761
15762 static void
15763 save_breakpoints_command (char *args, int from_tty)
15764 {
15765   save_breakpoints (args, from_tty, NULL);
15766 }
15767
15768 /* The `save tracepoints' command.  */
15769
15770 static void
15771 save_tracepoints_command (char *args, int from_tty)
15772 {
15773   save_breakpoints (args, from_tty, is_tracepoint);
15774 }
15775
15776 /* Create a vector of all tracepoints.  */
15777
15778 VEC(breakpoint_p) *
15779 all_tracepoints (void)
15780 {
15781   VEC(breakpoint_p) *tp_vec = 0;
15782   struct breakpoint *tp;
15783
15784   ALL_TRACEPOINTS (tp)
15785   {
15786     VEC_safe_push (breakpoint_p, tp_vec, tp);
15787   }
15788
15789   return tp_vec;
15790 }
15791
15792 \f
15793 /* This help string is used for the break, hbreak, tbreak and thbreak
15794    commands.  It is defined as a macro to prevent duplication.
15795    COMMAND should be a string constant containing the name of the
15796    command.  */
15797 #define BREAK_ARGS_HELP(command) \
15798 command" [PROBE_MODIFIER] [LOCATION] [thread THREADNUM] [if CONDITION]\n\
15799 PROBE_MODIFIER shall be present if the command is to be placed in a\n\
15800 probe point.  Accepted values are `-probe' (for a generic, automatically\n\
15801 guessed probe type) or `-probe-stap' (for a SystemTap probe).\n\
15802 LOCATION may be a line number, function name, or \"*\" and an address.\n\
15803 If a line number is specified, break at start of code for that line.\n\
15804 If a function is specified, break at start of code for that function.\n\
15805 If an address is specified, break at that exact address.\n\
15806 With no LOCATION, uses current execution address of the selected\n\
15807 stack frame.  This is useful for breaking on return to a stack frame.\n\
15808 \n\
15809 THREADNUM is the number from \"info threads\".\n\
15810 CONDITION is a boolean expression.\n\
15811 \n\
15812 Multiple breakpoints at one place are permitted, and useful if their\n\
15813 conditions are different.\n\
15814 \n\
15815 Do \"help breakpoints\" for info on other commands dealing with breakpoints."
15816
15817 /* List of subcommands for "catch".  */
15818 static struct cmd_list_element *catch_cmdlist;
15819
15820 /* List of subcommands for "tcatch".  */
15821 static struct cmd_list_element *tcatch_cmdlist;
15822
15823 void
15824 add_catch_command (char *name, char *docstring,
15825                    void (*sfunc) (char *args, int from_tty,
15826                                   struct cmd_list_element *command),
15827                    completer_ftype *completer,
15828                    void *user_data_catch,
15829                    void *user_data_tcatch)
15830 {
15831   struct cmd_list_element *command;
15832
15833   command = add_cmd (name, class_breakpoint, NULL, docstring,
15834                      &catch_cmdlist);
15835   set_cmd_sfunc (command, sfunc);
15836   set_cmd_context (command, user_data_catch);
15837   set_cmd_completer (command, completer);
15838
15839   command = add_cmd (name, class_breakpoint, NULL, docstring,
15840                      &tcatch_cmdlist);
15841   set_cmd_sfunc (command, sfunc);
15842   set_cmd_context (command, user_data_tcatch);
15843   set_cmd_completer (command, completer);
15844 }
15845
15846 static void
15847 clear_syscall_counts (struct inferior *inf)
15848 {
15849   struct catch_syscall_inferior_data *inf_data
15850     = get_catch_syscall_inferior_data (inf);
15851
15852   inf_data->total_syscalls_count = 0;
15853   inf_data->any_syscall_count = 0;
15854   VEC_free (int, inf_data->syscalls_counts);
15855 }
15856
15857 static void
15858 save_command (char *arg, int from_tty)
15859 {
15860   printf_unfiltered (_("\"save\" must be followed by "
15861                        "the name of a save subcommand.\n"));
15862   help_list (save_cmdlist, "save ", -1, gdb_stdout);
15863 }
15864
15865 struct breakpoint *
15866 iterate_over_breakpoints (int (*callback) (struct breakpoint *, void *),
15867                           void *data)
15868 {
15869   struct breakpoint *b, *b_tmp;
15870
15871   ALL_BREAKPOINTS_SAFE (b, b_tmp)
15872     {
15873       if ((*callback) (b, data))
15874         return b;
15875     }
15876
15877   return NULL;
15878 }
15879
15880 /* Zero if any of the breakpoint's locations could be a location where
15881    functions have been inlined, nonzero otherwise.  */
15882
15883 static int
15884 is_non_inline_function (struct breakpoint *b)
15885 {
15886   /* The shared library event breakpoint is set on the address of a
15887      non-inline function.  */
15888   if (b->type == bp_shlib_event)
15889     return 1;
15890
15891   return 0;
15892 }
15893
15894 /* Nonzero if the specified PC cannot be a location where functions
15895    have been inlined.  */
15896
15897 int
15898 pc_at_non_inline_function (struct address_space *aspace, CORE_ADDR pc,
15899                            const struct target_waitstatus *ws)
15900 {
15901   struct breakpoint *b;
15902   struct bp_location *bl;
15903
15904   ALL_BREAKPOINTS (b)
15905     {
15906       if (!is_non_inline_function (b))
15907         continue;
15908
15909       for (bl = b->loc; bl != NULL; bl = bl->next)
15910         {
15911           if (!bl->shlib_disabled
15912               && bpstat_check_location (bl, aspace, pc, ws))
15913             return 1;
15914         }
15915     }
15916
15917   return 0;
15918 }
15919
15920 /* Remove any references to OBJFILE which is going to be freed.  */
15921
15922 void
15923 breakpoint_free_objfile (struct objfile *objfile)
15924 {
15925   struct bp_location **locp, *loc;
15926
15927   ALL_BP_LOCATIONS (loc, locp)
15928     if (loc->symtab != NULL && loc->symtab->objfile == objfile)
15929       loc->symtab = NULL;
15930 }
15931
15932 void
15933 initialize_breakpoint_ops (void)
15934 {
15935   static int initialized = 0;
15936
15937   struct breakpoint_ops *ops;
15938
15939   if (initialized)
15940     return;
15941   initialized = 1;
15942
15943   /* The breakpoint_ops structure to be inherit by all kinds of
15944      breakpoints (real breakpoints, i.e., user "break" breakpoints,
15945      internal and momentary breakpoints, etc.).  */
15946   ops = &bkpt_base_breakpoint_ops;
15947   *ops = base_breakpoint_ops;
15948   ops->re_set = bkpt_re_set;
15949   ops->insert_location = bkpt_insert_location;
15950   ops->remove_location = bkpt_remove_location;
15951   ops->breakpoint_hit = bkpt_breakpoint_hit;
15952   ops->create_sals_from_address = bkpt_create_sals_from_address;
15953   ops->create_breakpoints_sal = bkpt_create_breakpoints_sal;
15954   ops->decode_linespec = bkpt_decode_linespec;
15955
15956   /* The breakpoint_ops structure to be used in regular breakpoints.  */
15957   ops = &bkpt_breakpoint_ops;
15958   *ops = bkpt_base_breakpoint_ops;
15959   ops->re_set = bkpt_re_set;
15960   ops->resources_needed = bkpt_resources_needed;
15961   ops->print_it = bkpt_print_it;
15962   ops->print_mention = bkpt_print_mention;
15963   ops->print_recreate = bkpt_print_recreate;
15964
15965   /* Ranged breakpoints.  */
15966   ops = &ranged_breakpoint_ops;
15967   *ops = bkpt_breakpoint_ops;
15968   ops->breakpoint_hit = breakpoint_hit_ranged_breakpoint;
15969   ops->resources_needed = resources_needed_ranged_breakpoint;
15970   ops->print_it = print_it_ranged_breakpoint;
15971   ops->print_one = print_one_ranged_breakpoint;
15972   ops->print_one_detail = print_one_detail_ranged_breakpoint;
15973   ops->print_mention = print_mention_ranged_breakpoint;
15974   ops->print_recreate = print_recreate_ranged_breakpoint;
15975
15976   /* Internal breakpoints.  */
15977   ops = &internal_breakpoint_ops;
15978   *ops = bkpt_base_breakpoint_ops;
15979   ops->re_set = internal_bkpt_re_set;
15980   ops->check_status = internal_bkpt_check_status;
15981   ops->print_it = internal_bkpt_print_it;
15982   ops->print_mention = internal_bkpt_print_mention;
15983
15984   /* Momentary breakpoints.  */
15985   ops = &momentary_breakpoint_ops;
15986   *ops = bkpt_base_breakpoint_ops;
15987   ops->re_set = momentary_bkpt_re_set;
15988   ops->check_status = momentary_bkpt_check_status;
15989   ops->print_it = momentary_bkpt_print_it;
15990   ops->print_mention = momentary_bkpt_print_mention;
15991
15992   /* Momentary breakpoints for bp_longjmp and bp_exception.  */
15993   ops = &longjmp_breakpoint_ops;
15994   *ops = momentary_breakpoint_ops;
15995   ops->dtor = longjmp_bkpt_dtor;
15996
15997   /* Probe breakpoints.  */
15998   ops = &bkpt_probe_breakpoint_ops;
15999   *ops = bkpt_breakpoint_ops;
16000   ops->insert_location = bkpt_probe_insert_location;
16001   ops->remove_location = bkpt_probe_remove_location;
16002   ops->create_sals_from_address = bkpt_probe_create_sals_from_address;
16003   ops->decode_linespec = bkpt_probe_decode_linespec;
16004
16005   /* Watchpoints.  */
16006   ops = &watchpoint_breakpoint_ops;
16007   *ops = base_breakpoint_ops;
16008   ops->dtor = dtor_watchpoint;
16009   ops->re_set = re_set_watchpoint;
16010   ops->insert_location = insert_watchpoint;
16011   ops->remove_location = remove_watchpoint;
16012   ops->breakpoint_hit = breakpoint_hit_watchpoint;
16013   ops->check_status = check_status_watchpoint;
16014   ops->resources_needed = resources_needed_watchpoint;
16015   ops->works_in_software_mode = works_in_software_mode_watchpoint;
16016   ops->print_it = print_it_watchpoint;
16017   ops->print_mention = print_mention_watchpoint;
16018   ops->print_recreate = print_recreate_watchpoint;
16019   ops->explains_signal = explains_signal_watchpoint;
16020
16021   /* Masked watchpoints.  */
16022   ops = &masked_watchpoint_breakpoint_ops;
16023   *ops = watchpoint_breakpoint_ops;
16024   ops->insert_location = insert_masked_watchpoint;
16025   ops->remove_location = remove_masked_watchpoint;
16026   ops->resources_needed = resources_needed_masked_watchpoint;
16027   ops->works_in_software_mode = works_in_software_mode_masked_watchpoint;
16028   ops->print_it = print_it_masked_watchpoint;
16029   ops->print_one_detail = print_one_detail_masked_watchpoint;
16030   ops->print_mention = print_mention_masked_watchpoint;
16031   ops->print_recreate = print_recreate_masked_watchpoint;
16032
16033   /* Tracepoints.  */
16034   ops = &tracepoint_breakpoint_ops;
16035   *ops = base_breakpoint_ops;
16036   ops->re_set = tracepoint_re_set;
16037   ops->breakpoint_hit = tracepoint_breakpoint_hit;
16038   ops->print_one_detail = tracepoint_print_one_detail;
16039   ops->print_mention = tracepoint_print_mention;
16040   ops->print_recreate = tracepoint_print_recreate;
16041   ops->create_sals_from_address = tracepoint_create_sals_from_address;
16042   ops->create_breakpoints_sal = tracepoint_create_breakpoints_sal;
16043   ops->decode_linespec = tracepoint_decode_linespec;
16044
16045   /* Probe tracepoints.  */
16046   ops = &tracepoint_probe_breakpoint_ops;
16047   *ops = tracepoint_breakpoint_ops;
16048   ops->create_sals_from_address = tracepoint_probe_create_sals_from_address;
16049   ops->decode_linespec = tracepoint_probe_decode_linespec;
16050
16051   /* Static tracepoints with marker (`-m').  */
16052   ops = &strace_marker_breakpoint_ops;
16053   *ops = tracepoint_breakpoint_ops;
16054   ops->create_sals_from_address = strace_marker_create_sals_from_address;
16055   ops->create_breakpoints_sal = strace_marker_create_breakpoints_sal;
16056   ops->decode_linespec = strace_marker_decode_linespec;
16057
16058   /* Fork catchpoints.  */
16059   ops = &catch_fork_breakpoint_ops;
16060   *ops = base_breakpoint_ops;
16061   ops->insert_location = insert_catch_fork;
16062   ops->remove_location = remove_catch_fork;
16063   ops->breakpoint_hit = breakpoint_hit_catch_fork;
16064   ops->print_it = print_it_catch_fork;
16065   ops->print_one = print_one_catch_fork;
16066   ops->print_mention = print_mention_catch_fork;
16067   ops->print_recreate = print_recreate_catch_fork;
16068
16069   /* Vfork catchpoints.  */
16070   ops = &catch_vfork_breakpoint_ops;
16071   *ops = base_breakpoint_ops;
16072   ops->insert_location = insert_catch_vfork;
16073   ops->remove_location = remove_catch_vfork;
16074   ops->breakpoint_hit = breakpoint_hit_catch_vfork;
16075   ops->print_it = print_it_catch_vfork;
16076   ops->print_one = print_one_catch_vfork;
16077   ops->print_mention = print_mention_catch_vfork;
16078   ops->print_recreate = print_recreate_catch_vfork;
16079
16080   /* Exec catchpoints.  */
16081   ops = &catch_exec_breakpoint_ops;
16082   *ops = base_breakpoint_ops;
16083   ops->dtor = dtor_catch_exec;
16084   ops->insert_location = insert_catch_exec;
16085   ops->remove_location = remove_catch_exec;
16086   ops->breakpoint_hit = breakpoint_hit_catch_exec;
16087   ops->print_it = print_it_catch_exec;
16088   ops->print_one = print_one_catch_exec;
16089   ops->print_mention = print_mention_catch_exec;
16090   ops->print_recreate = print_recreate_catch_exec;
16091
16092   /* Syscall catchpoints.  */
16093   ops = &catch_syscall_breakpoint_ops;
16094   *ops = base_breakpoint_ops;
16095   ops->dtor = dtor_catch_syscall;
16096   ops->insert_location = insert_catch_syscall;
16097   ops->remove_location = remove_catch_syscall;
16098   ops->breakpoint_hit = breakpoint_hit_catch_syscall;
16099   ops->print_it = print_it_catch_syscall;
16100   ops->print_one = print_one_catch_syscall;
16101   ops->print_mention = print_mention_catch_syscall;
16102   ops->print_recreate = print_recreate_catch_syscall;
16103
16104   /* Solib-related catchpoints.  */
16105   ops = &catch_solib_breakpoint_ops;
16106   *ops = base_breakpoint_ops;
16107   ops->dtor = dtor_catch_solib;
16108   ops->insert_location = insert_catch_solib;
16109   ops->remove_location = remove_catch_solib;
16110   ops->breakpoint_hit = breakpoint_hit_catch_solib;
16111   ops->check_status = check_status_catch_solib;
16112   ops->print_it = print_it_catch_solib;
16113   ops->print_one = print_one_catch_solib;
16114   ops->print_mention = print_mention_catch_solib;
16115   ops->print_recreate = print_recreate_catch_solib;
16116
16117   ops = &dprintf_breakpoint_ops;
16118   *ops = bkpt_base_breakpoint_ops;
16119   ops->re_set = dprintf_re_set;
16120   ops->resources_needed = bkpt_resources_needed;
16121   ops->print_it = bkpt_print_it;
16122   ops->print_mention = bkpt_print_mention;
16123   ops->print_recreate = dprintf_print_recreate;
16124   ops->after_condition_true = dprintf_after_condition_true;
16125 }
16126
16127 /* Chain containing all defined "enable breakpoint" subcommands.  */
16128
16129 static struct cmd_list_element *enablebreaklist = NULL;
16130
16131 void
16132 _initialize_breakpoint (void)
16133 {
16134   struct cmd_list_element *c;
16135
16136   initialize_breakpoint_ops ();
16137
16138   observer_attach_solib_unloaded (disable_breakpoints_in_unloaded_shlib);
16139   observer_attach_free_objfile (disable_breakpoints_in_freed_objfile);
16140   observer_attach_inferior_exit (clear_syscall_counts);
16141   observer_attach_memory_changed (invalidate_bp_value_on_memory_change);
16142
16143   breakpoint_objfile_key
16144     = register_objfile_data_with_cleanup (NULL, free_breakpoint_probes);
16145
16146   catch_syscall_inferior_data
16147     = register_inferior_data_with_cleanup (NULL,
16148                                            catch_syscall_inferior_data_cleanup);
16149
16150   breakpoint_chain = 0;
16151   /* Don't bother to call set_breakpoint_count.  $bpnum isn't useful
16152      before a breakpoint is set.  */
16153   breakpoint_count = 0;
16154
16155   tracepoint_count = 0;
16156
16157   add_com ("ignore", class_breakpoint, ignore_command, _("\
16158 Set ignore-count of breakpoint number N to COUNT.\n\
16159 Usage is `ignore N COUNT'."));
16160   if (xdb_commands)
16161     add_com_alias ("bc", "ignore", class_breakpoint, 1);
16162
16163   add_com ("commands", class_breakpoint, commands_command, _("\
16164 Set commands to be executed when a breakpoint is hit.\n\
16165 Give breakpoint number as argument after \"commands\".\n\
16166 With no argument, the targeted breakpoint is the last one set.\n\
16167 The commands themselves follow starting on the next line.\n\
16168 Type a line containing \"end\" to indicate the end of them.\n\
16169 Give \"silent\" as the first line to make the breakpoint silent;\n\
16170 then no output is printed when it is hit, except what the commands print."));
16171
16172   c = add_com ("condition", class_breakpoint, condition_command, _("\
16173 Specify breakpoint number N to break only if COND is true.\n\
16174 Usage is `condition N COND', where N is an integer and COND is an\n\
16175 expression to be evaluated whenever breakpoint N is reached."));
16176   set_cmd_completer (c, condition_completer);
16177
16178   c = add_com ("tbreak", class_breakpoint, tbreak_command, _("\
16179 Set a temporary breakpoint.\n\
16180 Like \"break\" except the breakpoint is only temporary,\n\
16181 so it will be deleted when hit.  Equivalent to \"break\" followed\n\
16182 by using \"enable delete\" on the breakpoint number.\n\
16183 \n"
16184 BREAK_ARGS_HELP ("tbreak")));
16185   set_cmd_completer (c, location_completer);
16186
16187   c = add_com ("hbreak", class_breakpoint, hbreak_command, _("\
16188 Set a hardware assisted breakpoint.\n\
16189 Like \"break\" except the breakpoint requires hardware support,\n\
16190 some target hardware may not have this support.\n\
16191 \n"
16192 BREAK_ARGS_HELP ("hbreak")));
16193   set_cmd_completer (c, location_completer);
16194
16195   c = add_com ("thbreak", class_breakpoint, thbreak_command, _("\
16196 Set a temporary hardware assisted breakpoint.\n\
16197 Like \"hbreak\" except the breakpoint is only temporary,\n\
16198 so it will be deleted when hit.\n\
16199 \n"
16200 BREAK_ARGS_HELP ("thbreak")));
16201   set_cmd_completer (c, location_completer);
16202
16203   add_prefix_cmd ("enable", class_breakpoint, enable_command, _("\
16204 Enable some breakpoints.\n\
16205 Give breakpoint numbers (separated by spaces) as arguments.\n\
16206 With no subcommand, breakpoints are enabled until you command otherwise.\n\
16207 This is used to cancel the effect of the \"disable\" command.\n\
16208 With a subcommand you can enable temporarily."),
16209                   &enablelist, "enable ", 1, &cmdlist);
16210   if (xdb_commands)
16211     add_com ("ab", class_breakpoint, enable_command, _("\
16212 Enable some breakpoints.\n\
16213 Give breakpoint numbers (separated by spaces) as arguments.\n\
16214 With no subcommand, breakpoints are enabled until you command otherwise.\n\
16215 This is used to cancel the effect of the \"disable\" command.\n\
16216 With a subcommand you can enable temporarily."));
16217
16218   add_com_alias ("en", "enable", class_breakpoint, 1);
16219
16220   add_prefix_cmd ("breakpoints", class_breakpoint, enable_command, _("\
16221 Enable some breakpoints.\n\
16222 Give breakpoint numbers (separated by spaces) as arguments.\n\
16223 This is used to cancel the effect of the \"disable\" command.\n\
16224 May be abbreviated to simply \"enable\".\n"),
16225                    &enablebreaklist, "enable breakpoints ", 1, &enablelist);
16226
16227   add_cmd ("once", no_class, enable_once_command, _("\
16228 Enable breakpoints for one hit.  Give breakpoint numbers.\n\
16229 If a breakpoint is hit while enabled in this fashion, it becomes disabled."),
16230            &enablebreaklist);
16231
16232   add_cmd ("delete", no_class, enable_delete_command, _("\
16233 Enable breakpoints and delete when hit.  Give breakpoint numbers.\n\
16234 If a breakpoint is hit while enabled in this fashion, it is deleted."),
16235            &enablebreaklist);
16236
16237   add_cmd ("count", no_class, enable_count_command, _("\
16238 Enable breakpoints for COUNT hits.  Give count and then breakpoint numbers.\n\
16239 If a breakpoint is hit while enabled in this fashion,\n\
16240 the count is decremented; when it reaches zero, the breakpoint is disabled."),
16241            &enablebreaklist);
16242
16243   add_cmd ("delete", no_class, enable_delete_command, _("\
16244 Enable breakpoints and delete when hit.  Give breakpoint numbers.\n\
16245 If a breakpoint is hit while enabled in this fashion, it is deleted."),
16246            &enablelist);
16247
16248   add_cmd ("once", no_class, enable_once_command, _("\
16249 Enable breakpoints for one hit.  Give breakpoint numbers.\n\
16250 If a breakpoint is hit while enabled in this fashion, it becomes disabled."),
16251            &enablelist);
16252
16253   add_cmd ("count", no_class, enable_count_command, _("\
16254 Enable breakpoints for COUNT hits.  Give count and then breakpoint numbers.\n\
16255 If a breakpoint is hit while enabled in this fashion,\n\
16256 the count is decremented; when it reaches zero, the breakpoint is disabled."),
16257            &enablelist);
16258
16259   add_prefix_cmd ("disable", class_breakpoint, disable_command, _("\
16260 Disable some breakpoints.\n\
16261 Arguments are breakpoint numbers with spaces in between.\n\
16262 To disable all breakpoints, give no argument.\n\
16263 A disabled breakpoint is not forgotten, but has no effect until re-enabled."),
16264                   &disablelist, "disable ", 1, &cmdlist);
16265   add_com_alias ("dis", "disable", class_breakpoint, 1);
16266   add_com_alias ("disa", "disable", class_breakpoint, 1);
16267   if (xdb_commands)
16268     add_com ("sb", class_breakpoint, disable_command, _("\
16269 Disable some breakpoints.\n\
16270 Arguments are breakpoint numbers with spaces in between.\n\
16271 To disable all breakpoints, give no argument.\n\
16272 A disabled breakpoint is not forgotten, but has no effect until re-enabled."));
16273
16274   add_cmd ("breakpoints", class_alias, disable_command, _("\
16275 Disable some breakpoints.\n\
16276 Arguments are breakpoint numbers with spaces in between.\n\
16277 To disable all breakpoints, give no argument.\n\
16278 A disabled breakpoint is not forgotten, but has no effect until re-enabled.\n\
16279 This command may be abbreviated \"disable\"."),
16280            &disablelist);
16281
16282   add_prefix_cmd ("delete", class_breakpoint, delete_command, _("\
16283 Delete some breakpoints or auto-display expressions.\n\
16284 Arguments are breakpoint numbers with spaces in between.\n\
16285 To delete all breakpoints, give no argument.\n\
16286 \n\
16287 Also a prefix command for deletion of other GDB objects.\n\
16288 The \"unset\" command is also an alias for \"delete\"."),
16289                   &deletelist, "delete ", 1, &cmdlist);
16290   add_com_alias ("d", "delete", class_breakpoint, 1);
16291   add_com_alias ("del", "delete", class_breakpoint, 1);
16292   if (xdb_commands)
16293     add_com ("db", class_breakpoint, delete_command, _("\
16294 Delete some breakpoints.\n\
16295 Arguments are breakpoint numbers with spaces in between.\n\
16296 To delete all breakpoints, give no argument.\n"));
16297
16298   add_cmd ("breakpoints", class_alias, delete_command, _("\
16299 Delete some breakpoints or auto-display expressions.\n\
16300 Arguments are breakpoint numbers with spaces in between.\n\
16301 To delete all breakpoints, give no argument.\n\
16302 This command may be abbreviated \"delete\"."),
16303            &deletelist);
16304
16305   add_com ("clear", class_breakpoint, clear_command, _("\
16306 Clear breakpoint at specified line or function.\n\
16307 Argument may be line number, function name, or \"*\" and an address.\n\
16308 If line number is specified, all breakpoints in that line are cleared.\n\
16309 If function is specified, breakpoints at beginning of function are cleared.\n\
16310 If an address is specified, breakpoints at that address are cleared.\n\
16311 \n\
16312 With no argument, clears all breakpoints in the line that the selected frame\n\
16313 is executing in.\n\
16314 \n\
16315 See also the \"delete\" command which clears breakpoints by number."));
16316   add_com_alias ("cl", "clear", class_breakpoint, 1);
16317
16318   c = add_com ("break", class_breakpoint, break_command, _("\
16319 Set breakpoint at specified line or function.\n"
16320 BREAK_ARGS_HELP ("break")));
16321   set_cmd_completer (c, location_completer);
16322
16323   add_com_alias ("b", "break", class_run, 1);
16324   add_com_alias ("br", "break", class_run, 1);
16325   add_com_alias ("bre", "break", class_run, 1);
16326   add_com_alias ("brea", "break", class_run, 1);
16327
16328   if (xdb_commands)
16329    add_com_alias ("ba", "break", class_breakpoint, 1);
16330
16331   if (dbx_commands)
16332     {
16333       add_abbrev_prefix_cmd ("stop", class_breakpoint, stop_command, _("\
16334 Break in function/address or break at a line in the current file."),
16335                              &stoplist, "stop ", 1, &cmdlist);
16336       add_cmd ("in", class_breakpoint, stopin_command,
16337                _("Break in function or address."), &stoplist);
16338       add_cmd ("at", class_breakpoint, stopat_command,
16339                _("Break at a line in the current file."), &stoplist);
16340       add_com ("status", class_info, breakpoints_info, _("\
16341 Status of user-settable breakpoints, or breakpoint number NUMBER.\n\
16342 The \"Type\" column indicates one of:\n\
16343 \tbreakpoint     - normal breakpoint\n\
16344 \twatchpoint     - watchpoint\n\
16345 The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
16346 the disposition of the breakpoint after it gets hit.  \"dis\" means that the\n\
16347 breakpoint will be disabled.  The \"Address\" and \"What\" columns indicate the\n\
16348 address and file/line number respectively.\n\
16349 \n\
16350 Convenience variable \"$_\" and default examine address for \"x\"\n\
16351 are set to the address of the last breakpoint listed unless the command\n\
16352 is prefixed with \"server \".\n\n\
16353 Convenience variable \"$bpnum\" contains the number of the last\n\
16354 breakpoint set."));
16355     }
16356
16357   add_info ("breakpoints", breakpoints_info, _("\
16358 Status of specified breakpoints (all user-settable breakpoints if no argument).\n\
16359 The \"Type\" column indicates one of:\n\
16360 \tbreakpoint     - normal breakpoint\n\
16361 \twatchpoint     - watchpoint\n\
16362 The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
16363 the disposition of the breakpoint after it gets hit.  \"dis\" means that the\n\
16364 breakpoint will be disabled.  The \"Address\" and \"What\" columns indicate the\n\
16365 address and file/line number respectively.\n\
16366 \n\
16367 Convenience variable \"$_\" and default examine address for \"x\"\n\
16368 are set to the address of the last breakpoint listed unless the command\n\
16369 is prefixed with \"server \".\n\n\
16370 Convenience variable \"$bpnum\" contains the number of the last\n\
16371 breakpoint set."));
16372
16373   add_info_alias ("b", "breakpoints", 1);
16374
16375   if (xdb_commands)
16376     add_com ("lb", class_breakpoint, breakpoints_info, _("\
16377 Status of user-settable breakpoints, or breakpoint number NUMBER.\n\
16378 The \"Type\" column indicates one of:\n\
16379 \tbreakpoint     - normal breakpoint\n\
16380 \twatchpoint     - watchpoint\n\
16381 The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
16382 the disposition of the breakpoint after it gets hit.  \"dis\" means that the\n\
16383 breakpoint will be disabled.  The \"Address\" and \"What\" columns indicate the\n\
16384 address and file/line number respectively.\n\
16385 \n\
16386 Convenience variable \"$_\" and default examine address for \"x\"\n\
16387 are set to the address of the last breakpoint listed unless the command\n\
16388 is prefixed with \"server \".\n\n\
16389 Convenience variable \"$bpnum\" contains the number of the last\n\
16390 breakpoint set."));
16391
16392   add_cmd ("breakpoints", class_maintenance, maintenance_info_breakpoints, _("\
16393 Status of all breakpoints, or breakpoint number NUMBER.\n\
16394 The \"Type\" column indicates one of:\n\
16395 \tbreakpoint     - normal breakpoint\n\
16396 \twatchpoint     - watchpoint\n\
16397 \tlongjmp        - internal breakpoint used to step through longjmp()\n\
16398 \tlongjmp resume - internal breakpoint at the target of longjmp()\n\
16399 \tuntil          - internal breakpoint used by the \"until\" command\n\
16400 \tfinish         - internal breakpoint used by the \"finish\" command\n\
16401 The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
16402 the disposition of the breakpoint after it gets hit.  \"dis\" means that the\n\
16403 breakpoint will be disabled.  The \"Address\" and \"What\" columns indicate the\n\
16404 address and file/line number respectively.\n\
16405 \n\
16406 Convenience variable \"$_\" and default examine address for \"x\"\n\
16407 are set to the address of the last breakpoint listed unless the command\n\
16408 is prefixed with \"server \".\n\n\
16409 Convenience variable \"$bpnum\" contains the number of the last\n\
16410 breakpoint set."),
16411            &maintenanceinfolist);
16412
16413   add_prefix_cmd ("catch", class_breakpoint, catch_command, _("\
16414 Set catchpoints to catch events."),
16415                   &catch_cmdlist, "catch ",
16416                   0/*allow-unknown*/, &cmdlist);
16417
16418   add_prefix_cmd ("tcatch", class_breakpoint, tcatch_command, _("\
16419 Set temporary catchpoints to catch events."),
16420                   &tcatch_cmdlist, "tcatch ",
16421                   0/*allow-unknown*/, &cmdlist);
16422
16423   add_catch_command ("fork", _("Catch calls to fork."),
16424                      catch_fork_command_1,
16425                      NULL,
16426                      (void *) (uintptr_t) catch_fork_permanent,
16427                      (void *) (uintptr_t) catch_fork_temporary);
16428   add_catch_command ("vfork", _("Catch calls to vfork."),
16429                      catch_fork_command_1,
16430                      NULL,
16431                      (void *) (uintptr_t) catch_vfork_permanent,
16432                      (void *) (uintptr_t) catch_vfork_temporary);
16433   add_catch_command ("exec", _("Catch calls to exec."),
16434                      catch_exec_command_1,
16435                      NULL,
16436                      CATCH_PERMANENT,
16437                      CATCH_TEMPORARY);
16438   add_catch_command ("load", _("Catch loads of shared libraries.\n\
16439 Usage: catch load [REGEX]\n\
16440 If REGEX is given, only stop for libraries matching the regular expression."),
16441                      catch_load_command_1,
16442                      NULL,
16443                      CATCH_PERMANENT,
16444                      CATCH_TEMPORARY);
16445   add_catch_command ("unload", _("Catch unloads of shared libraries.\n\
16446 Usage: catch unload [REGEX]\n\
16447 If REGEX is given, only stop for libraries matching the regular expression."),
16448                      catch_unload_command_1,
16449                      NULL,
16450                      CATCH_PERMANENT,
16451                      CATCH_TEMPORARY);
16452   add_catch_command ("syscall", _("\
16453 Catch system calls by their names and/or numbers.\n\
16454 Arguments say which system calls to catch.  If no arguments\n\
16455 are given, every system call will be caught.\n\
16456 Arguments, if given, should be one or more system call names\n\
16457 (if your system supports that), or system call numbers."),
16458                      catch_syscall_command_1,
16459                      catch_syscall_completer,
16460                      CATCH_PERMANENT,
16461                      CATCH_TEMPORARY);
16462
16463   c = add_com ("watch", class_breakpoint, watch_command, _("\
16464 Set a watchpoint for an expression.\n\
16465 Usage: watch [-l|-location] EXPRESSION\n\
16466 A watchpoint stops execution of your program whenever the value of\n\
16467 an expression changes.\n\
16468 If -l or -location is given, this evaluates EXPRESSION and watches\n\
16469 the memory to which it refers."));
16470   set_cmd_completer (c, expression_completer);
16471
16472   c = add_com ("rwatch", class_breakpoint, rwatch_command, _("\
16473 Set a read watchpoint for an expression.\n\
16474 Usage: rwatch [-l|-location] EXPRESSION\n\
16475 A watchpoint stops execution of your program whenever the value of\n\
16476 an expression is read.\n\
16477 If -l or -location is given, this evaluates EXPRESSION and watches\n\
16478 the memory to which it refers."));
16479   set_cmd_completer (c, expression_completer);
16480
16481   c = add_com ("awatch", class_breakpoint, awatch_command, _("\
16482 Set a watchpoint for an expression.\n\
16483 Usage: awatch [-l|-location] EXPRESSION\n\
16484 A watchpoint stops execution of your program whenever the value of\n\
16485 an expression is either read or written.\n\
16486 If -l or -location is given, this evaluates EXPRESSION and watches\n\
16487 the memory to which it refers."));
16488   set_cmd_completer (c, expression_completer);
16489
16490   add_info ("watchpoints", watchpoints_info, _("\
16491 Status of specified watchpoints (all watchpoints if no argument)."));
16492
16493   /* XXX: cagney/2005-02-23: This should be a boolean, and should
16494      respond to changes - contrary to the description.  */
16495   add_setshow_zinteger_cmd ("can-use-hw-watchpoints", class_support,
16496                             &can_use_hw_watchpoints, _("\
16497 Set debugger's willingness to use watchpoint hardware."), _("\
16498 Show debugger's willingness to use watchpoint hardware."), _("\
16499 If zero, gdb will not use hardware for new watchpoints, even if\n\
16500 such is available.  (However, any hardware watchpoints that were\n\
16501 created before setting this to nonzero, will continue to use watchpoint\n\
16502 hardware.)"),
16503                             NULL,
16504                             show_can_use_hw_watchpoints,
16505                             &setlist, &showlist);
16506
16507   can_use_hw_watchpoints = 1;
16508
16509   /* Tracepoint manipulation commands.  */
16510
16511   c = add_com ("trace", class_breakpoint, trace_command, _("\
16512 Set a tracepoint at specified line or function.\n\
16513 \n"
16514 BREAK_ARGS_HELP ("trace") "\n\
16515 Do \"help tracepoints\" for info on other tracepoint commands."));
16516   set_cmd_completer (c, location_completer);
16517
16518   add_com_alias ("tp", "trace", class_alias, 0);
16519   add_com_alias ("tr", "trace", class_alias, 1);
16520   add_com_alias ("tra", "trace", class_alias, 1);
16521   add_com_alias ("trac", "trace", class_alias, 1);
16522
16523   c = add_com ("ftrace", class_breakpoint, ftrace_command, _("\
16524 Set a fast tracepoint at specified line or function.\n\
16525 \n"
16526 BREAK_ARGS_HELP ("ftrace") "\n\
16527 Do \"help tracepoints\" for info on other tracepoint commands."));
16528   set_cmd_completer (c, location_completer);
16529
16530   c = add_com ("strace", class_breakpoint, strace_command, _("\
16531 Set a static tracepoint at specified line, function or marker.\n\
16532 \n\
16533 strace [LOCATION] [if CONDITION]\n\
16534 LOCATION may be a line number, function name, \"*\" and an address,\n\
16535 or -m MARKER_ID.\n\
16536 If a line number is specified, probe the marker at start of code\n\
16537 for that line.  If a function is specified, probe the marker at start\n\
16538 of code for that function.  If an address is specified, probe the marker\n\
16539 at that exact address.  If a marker id is specified, probe the marker\n\
16540 with that name.  With no LOCATION, uses current execution address of\n\
16541 the selected stack frame.\n\
16542 Static tracepoints accept an extra collect action -- ``collect $_sdata''.\n\
16543 This collects arbitrary user data passed in the probe point call to the\n\
16544 tracing library.  You can inspect it when analyzing the trace buffer,\n\
16545 by printing the $_sdata variable like any other convenience variable.\n\
16546 \n\
16547 CONDITION is a boolean expression.\n\
16548 \n\
16549 Multiple tracepoints at one place are permitted, and useful if their\n\
16550 conditions are different.\n\
16551 \n\
16552 Do \"help breakpoints\" for info on other commands dealing with breakpoints.\n\
16553 Do \"help tracepoints\" for info on other tracepoint commands."));
16554   set_cmd_completer (c, location_completer);
16555
16556   add_info ("tracepoints", tracepoints_info, _("\
16557 Status of specified tracepoints (all tracepoints if no argument).\n\
16558 Convenience variable \"$tpnum\" contains the number of the\n\
16559 last tracepoint set."));
16560
16561   add_info_alias ("tp", "tracepoints", 1);
16562
16563   add_cmd ("tracepoints", class_trace, delete_trace_command, _("\
16564 Delete specified tracepoints.\n\
16565 Arguments are tracepoint numbers, separated by spaces.\n\
16566 No argument means delete all tracepoints."),
16567            &deletelist);
16568   add_alias_cmd ("tr", "tracepoints", class_trace, 1, &deletelist);
16569
16570   c = add_cmd ("tracepoints", class_trace, disable_trace_command, _("\
16571 Disable specified tracepoints.\n\
16572 Arguments are tracepoint numbers, separated by spaces.\n\
16573 No argument means disable all tracepoints."),
16574            &disablelist);
16575   deprecate_cmd (c, "disable");
16576
16577   c = add_cmd ("tracepoints", class_trace, enable_trace_command, _("\
16578 Enable specified tracepoints.\n\
16579 Arguments are tracepoint numbers, separated by spaces.\n\
16580 No argument means enable all tracepoints."),
16581            &enablelist);
16582   deprecate_cmd (c, "enable");
16583
16584   add_com ("passcount", class_trace, trace_pass_command, _("\
16585 Set the passcount for a tracepoint.\n\
16586 The trace will end when the tracepoint has been passed 'count' times.\n\
16587 Usage: passcount COUNT TPNUM, where TPNUM may also be \"all\";\n\
16588 if TPNUM is omitted, passcount refers to the last tracepoint defined."));
16589
16590   add_prefix_cmd ("save", class_breakpoint, save_command,
16591                   _("Save breakpoint definitions as a script."),
16592                   &save_cmdlist, "save ",
16593                   0/*allow-unknown*/, &cmdlist);
16594
16595   c = add_cmd ("breakpoints", class_breakpoint, save_breakpoints_command, _("\
16596 Save current breakpoint definitions as a script.\n\
16597 This includes all types of breakpoints (breakpoints, watchpoints,\n\
16598 catchpoints, tracepoints).  Use the 'source' command in another debug\n\
16599 session to restore them."),
16600                &save_cmdlist);
16601   set_cmd_completer (c, filename_completer);
16602
16603   c = add_cmd ("tracepoints", class_trace, save_tracepoints_command, _("\
16604 Save current tracepoint definitions as a script.\n\
16605 Use the 'source' command in another debug session to restore them."),
16606                &save_cmdlist);
16607   set_cmd_completer (c, filename_completer);
16608
16609   c = add_com_alias ("save-tracepoints", "save tracepoints", class_trace, 0);
16610   deprecate_cmd (c, "save tracepoints");
16611
16612   add_prefix_cmd ("breakpoint", class_maintenance, set_breakpoint_cmd, _("\
16613 Breakpoint specific settings\n\
16614 Configure various breakpoint-specific variables such as\n\
16615 pending breakpoint behavior"),
16616                   &breakpoint_set_cmdlist, "set breakpoint ",
16617                   0/*allow-unknown*/, &setlist);
16618   add_prefix_cmd ("breakpoint", class_maintenance, show_breakpoint_cmd, _("\
16619 Breakpoint specific settings\n\
16620 Configure various breakpoint-specific variables such as\n\
16621 pending breakpoint behavior"),
16622                   &breakpoint_show_cmdlist, "show breakpoint ",
16623                   0/*allow-unknown*/, &showlist);
16624
16625   add_setshow_auto_boolean_cmd ("pending", no_class,
16626                                 &pending_break_support, _("\
16627 Set debugger's behavior regarding pending breakpoints."), _("\
16628 Show debugger's behavior regarding pending breakpoints."), _("\
16629 If on, an unrecognized breakpoint location will cause gdb to create a\n\
16630 pending breakpoint.  If off, an unrecognized breakpoint location results in\n\
16631 an error.  If auto, an unrecognized breakpoint location results in a\n\
16632 user-query to see if a pending breakpoint should be created."),
16633                                 NULL,
16634                                 show_pending_break_support,
16635                                 &breakpoint_set_cmdlist,
16636                                 &breakpoint_show_cmdlist);
16637
16638   pending_break_support = AUTO_BOOLEAN_AUTO;
16639
16640   add_setshow_boolean_cmd ("auto-hw", no_class,
16641                            &automatic_hardware_breakpoints, _("\
16642 Set automatic usage of hardware breakpoints."), _("\
16643 Show automatic usage of hardware breakpoints."), _("\
16644 If set, the debugger will automatically use hardware breakpoints for\n\
16645 breakpoints set with \"break\" but falling in read-only memory.  If not set,\n\
16646 a warning will be emitted for such breakpoints."),
16647                            NULL,
16648                            show_automatic_hardware_breakpoints,
16649                            &breakpoint_set_cmdlist,
16650                            &breakpoint_show_cmdlist);
16651
16652   add_setshow_auto_boolean_cmd ("always-inserted", class_support,
16653                                 &always_inserted_mode, _("\
16654 Set mode for inserting breakpoints."), _("\
16655 Show mode for inserting breakpoints."), _("\
16656 When this mode is off, breakpoints are inserted in inferior when it is\n\
16657 resumed, and removed when execution stops.  When this mode is on,\n\
16658 breakpoints are inserted immediately and removed only when the user\n\
16659 deletes the breakpoint.  When this mode is auto (which is the default),\n\
16660 the behaviour depends on the non-stop setting (see help set non-stop).\n\
16661 In this case, if gdb is controlling the inferior in non-stop mode, gdb\n\
16662 behaves as if always-inserted mode is on; if gdb is controlling the\n\
16663 inferior in all-stop mode, gdb behaves as if always-inserted mode is off."),
16664                                 NULL,
16665                                 &show_always_inserted_mode,
16666                                 &breakpoint_set_cmdlist,
16667                                 &breakpoint_show_cmdlist);
16668
16669   add_setshow_enum_cmd ("condition-evaluation", class_breakpoint,
16670                         condition_evaluation_enums,
16671                         &condition_evaluation_mode_1, _("\
16672 Set mode of breakpoint condition evaluation."), _("\
16673 Show mode of breakpoint condition evaluation."), _("\
16674 When this is set to \"host\", breakpoint conditions will be\n\
16675 evaluated on the host's side by GDB.  When it is set to \"target\",\n\
16676 breakpoint conditions will be downloaded to the target (if the target\n\
16677 supports such feature) and conditions will be evaluated on the target's side.\n\
16678 If this is set to \"auto\" (default), this will be automatically set to\n\
16679 \"target\" if it supports condition evaluation, otherwise it will\n\
16680 be set to \"gdb\""),
16681                            &set_condition_evaluation_mode,
16682                            &show_condition_evaluation_mode,
16683                            &breakpoint_set_cmdlist,
16684                            &breakpoint_show_cmdlist);
16685
16686   add_com ("break-range", class_breakpoint, break_range_command, _("\
16687 Set a breakpoint for an address range.\n\
16688 break-range START-LOCATION, END-LOCATION\n\
16689 where START-LOCATION and END-LOCATION can be one of the following:\n\
16690   LINENUM, for that line in the current file,\n\
16691   FILE:LINENUM, for that line in that file,\n\
16692   +OFFSET, for that number of lines after the current line\n\
16693            or the start of the range\n\
16694   FUNCTION, for the first line in that function,\n\
16695   FILE:FUNCTION, to distinguish among like-named static functions.\n\
16696   *ADDRESS, for the instruction at that address.\n\
16697 \n\
16698 The breakpoint will stop execution of the inferior whenever it executes\n\
16699 an instruction at any address within the [START-LOCATION, END-LOCATION]\n\
16700 range (including START-LOCATION and END-LOCATION)."));
16701
16702   c = add_com ("dprintf", class_breakpoint, dprintf_command, _("\
16703 Set a dynamic printf at specified line or function.\n\
16704 dprintf location,format string,arg1,arg2,...\n\
16705 location may be a line number, function name, or \"*\" and an address.\n\
16706 If a line number is specified, break at start of code for that line.\n\
16707 If a function is specified, break at start of code for that function."));
16708   set_cmd_completer (c, location_completer);
16709
16710   add_setshow_enum_cmd ("dprintf-style", class_support,
16711                         dprintf_style_enums, &dprintf_style, _("\
16712 Set the style of usage for dynamic printf."), _("\
16713 Show the style of usage for dynamic printf."), _("\
16714 This setting chooses how GDB will do a dynamic printf.\n\
16715 If the value is \"gdb\", then the printing is done by GDB to its own\n\
16716 console, as with the \"printf\" command.\n\
16717 If the value is \"call\", the print is done by calling a function in your\n\
16718 program; by default printf(), but you can choose a different function or\n\
16719 output stream by setting dprintf-function and dprintf-channel."),
16720                         update_dprintf_commands, NULL,
16721                         &setlist, &showlist);
16722
16723   dprintf_function = xstrdup ("printf");
16724   add_setshow_string_cmd ("dprintf-function", class_support,
16725                           &dprintf_function, _("\
16726 Set the function to use for dynamic printf"), _("\
16727 Show the function to use for dynamic printf"), NULL,
16728                           update_dprintf_commands, NULL,
16729                           &setlist, &showlist);
16730
16731   dprintf_channel = xstrdup ("");
16732   add_setshow_string_cmd ("dprintf-channel", class_support,
16733                           &dprintf_channel, _("\
16734 Set the channel to use for dynamic printf"), _("\
16735 Show the channel to use for dynamic printf"), NULL,
16736                           update_dprintf_commands, NULL,
16737                           &setlist, &showlist);
16738
16739   add_setshow_boolean_cmd ("disconnected-dprintf", no_class,
16740                            &disconnected_dprintf, _("\
16741 Set whether dprintf continues after GDB disconnects."), _("\
16742 Show whether dprintf continues after GDB disconnects."), _("\
16743 Use this to let dprintf commands continue to hit and produce output\n\
16744 even if GDB disconnects or detaches from the target."),
16745                            NULL,
16746                            NULL,
16747                            &setlist, &showlist);
16748
16749   add_com ("agent-printf", class_vars, agent_printf_command, _("\
16750 agent-printf \"printf format string\", arg1, arg2, arg3, ..., argn\n\
16751 (target agent only) This is useful for formatted output in user-defined commands."));
16752
16753   automatic_hardware_breakpoints = 1;
16754
16755   observer_attach_about_to_proceed (breakpoint_about_to_proceed);
16756   observer_attach_thread_exit (remove_threaded_breakpoints);
16757 }