* breakpoint.c (bpstat_check_breakpoint_conditions): For thread
[platform/upstream/binutils.git] / gdb / breakpoint.c
1 /* Everything about breakpoints, for GDB.
2
3    Copyright (C) 1986-2013 Free Software Foundation, Inc.
4
5    This file is part of GDB.
6
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 3 of the License, or
10    (at your option) any later version.
11
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16
17    You should have received a copy of the GNU General Public License
18    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
19
20 #include "defs.h"
21 #include "arch-utils.h"
22 #include <ctype.h>
23 #include "hashtab.h"
24 #include "symtab.h"
25 #include "frame.h"
26 #include "breakpoint.h"
27 #include "tracepoint.h"
28 #include "gdbtypes.h"
29 #include "expression.h"
30 #include "gdbcore.h"
31 #include "gdbcmd.h"
32 #include "value.h"
33 #include "command.h"
34 #include "inferior.h"
35 #include "gdbthread.h"
36 #include "target.h"
37 #include "language.h"
38 #include "gdb_string.h"
39 #include "gdb-demangle.h"
40 #include "filenames.h"
41 #include "annotate.h"
42 #include "symfile.h"
43 #include "objfiles.h"
44 #include "source.h"
45 #include "linespec.h"
46 #include "completer.h"
47 #include "gdb.h"
48 #include "ui-out.h"
49 #include "cli/cli-script.h"
50 #include "gdb_assert.h"
51 #include "block.h"
52 #include "solib.h"
53 #include "solist.h"
54 #include "observer.h"
55 #include "exceptions.h"
56 #include "memattr.h"
57 #include "ada-lang.h"
58 #include "top.h"
59 #include "valprint.h"
60 #include "jit.h"
61 #include "xml-syscall.h"
62 #include "parser-defs.h"
63 #include "gdb_regex.h"
64 #include "probe.h"
65 #include "cli/cli-utils.h"
66 #include "continuations.h"
67 #include "stack.h"
68 #include "skip.h"
69 #include "gdb_regex.h"
70 #include "ax-gdb.h"
71 #include "dummy-frame.h"
72
73 #include "format.h"
74
75 /* readline include files */
76 #include "readline/readline.h"
77 #include "readline/history.h"
78
79 /* readline defines this.  */
80 #undef savestring
81
82 #include "mi/mi-common.h"
83 #include "python/python.h"
84
85 /* Enums for exception-handling support.  */
86 enum exception_event_kind
87 {
88   EX_EVENT_THROW,
89   EX_EVENT_RETHROW,
90   EX_EVENT_CATCH
91 };
92
93 /* Prototypes for local functions.  */
94
95 static void enable_delete_command (char *, int);
96
97 static void enable_once_command (char *, int);
98
99 static void enable_count_command (char *, int);
100
101 static void disable_command (char *, int);
102
103 static void enable_command (char *, int);
104
105 static void map_breakpoint_numbers (char *, void (*) (struct breakpoint *,
106                                                       void *),
107                                     void *);
108
109 static void ignore_command (char *, int);
110
111 static int breakpoint_re_set_one (void *);
112
113 static void breakpoint_re_set_default (struct breakpoint *);
114
115 static void create_sals_from_address_default (char **,
116                                               struct linespec_result *,
117                                               enum bptype, char *,
118                                               char **);
119
120 static void create_breakpoints_sal_default (struct gdbarch *,
121                                             struct linespec_result *,
122                                             char *, char *, enum bptype,
123                                             enum bpdisp, int, int,
124                                             int,
125                                             const struct breakpoint_ops *,
126                                             int, int, int, unsigned);
127
128 static void decode_linespec_default (struct breakpoint *, char **,
129                                      struct symtabs_and_lines *);
130
131 static void clear_command (char *, int);
132
133 static void catch_command (char *, int);
134
135 static int can_use_hardware_watchpoint (struct value *);
136
137 static void break_command_1 (char *, int, int);
138
139 static void mention (struct breakpoint *);
140
141 static struct breakpoint *set_raw_breakpoint_without_location (struct gdbarch *,
142                                                                enum bptype,
143                                                                const struct breakpoint_ops *);
144 static struct bp_location *add_location_to_breakpoint (struct breakpoint *,
145                                                        const struct symtab_and_line *);
146
147 /* This function is used in gdbtk sources and thus can not be made
148    static.  */
149 struct breakpoint *set_raw_breakpoint (struct gdbarch *gdbarch,
150                                        struct symtab_and_line,
151                                        enum bptype,
152                                        const struct breakpoint_ops *);
153
154 static struct breakpoint *
155   momentary_breakpoint_from_master (struct breakpoint *orig,
156                                     enum bptype type,
157                                     const struct breakpoint_ops *ops);
158
159 static void breakpoint_adjustment_warning (CORE_ADDR, CORE_ADDR, int, int);
160
161 static CORE_ADDR adjust_breakpoint_address (struct gdbarch *gdbarch,
162                                             CORE_ADDR bpaddr,
163                                             enum bptype bptype);
164
165 static void describe_other_breakpoints (struct gdbarch *,
166                                         struct program_space *, CORE_ADDR,
167                                         struct obj_section *, int);
168
169 static int breakpoint_address_match (struct address_space *aspace1,
170                                      CORE_ADDR addr1,
171                                      struct address_space *aspace2,
172                                      CORE_ADDR addr2);
173
174 static int watchpoint_locations_match (struct bp_location *loc1,
175                                        struct bp_location *loc2);
176
177 static int breakpoint_location_address_match (struct bp_location *bl,
178                                               struct address_space *aspace,
179                                               CORE_ADDR addr);
180
181 static void breakpoints_info (char *, int);
182
183 static void watchpoints_info (char *, int);
184
185 static int breakpoint_1 (char *, int, 
186                          int (*) (const struct breakpoint *));
187
188 static int breakpoint_cond_eval (void *);
189
190 static void cleanup_executing_breakpoints (void *);
191
192 static void commands_command (char *, int);
193
194 static void condition_command (char *, int);
195
196 typedef enum
197   {
198     mark_inserted,
199     mark_uninserted
200   }
201 insertion_state_t;
202
203 static int remove_breakpoint (struct bp_location *, insertion_state_t);
204 static int remove_breakpoint_1 (struct bp_location *, insertion_state_t);
205
206 static enum print_stop_action print_bp_stop_message (bpstat bs);
207
208 static int watchpoint_check (void *);
209
210 static void maintenance_info_breakpoints (char *, int);
211
212 static int hw_breakpoint_used_count (void);
213
214 static int hw_watchpoint_use_count (struct breakpoint *);
215
216 static int hw_watchpoint_used_count_others (struct breakpoint *except,
217                                             enum bptype type,
218                                             int *other_type_used);
219
220 static void hbreak_command (char *, int);
221
222 static void thbreak_command (char *, int);
223
224 static void enable_breakpoint_disp (struct breakpoint *, enum bpdisp,
225                                     int count);
226
227 static void stop_command (char *arg, int from_tty);
228
229 static void stopin_command (char *arg, int from_tty);
230
231 static void stopat_command (char *arg, int from_tty);
232
233 static void tcatch_command (char *arg, int from_tty);
234
235 static void detach_single_step_breakpoints (void);
236
237 static int single_step_breakpoint_inserted_here_p (struct address_space *,
238                                                    CORE_ADDR pc);
239
240 static void free_bp_location (struct bp_location *loc);
241 static void incref_bp_location (struct bp_location *loc);
242 static void decref_bp_location (struct bp_location **loc);
243
244 static struct bp_location *allocate_bp_location (struct breakpoint *bpt);
245
246 static void update_global_location_list (int);
247
248 static void update_global_location_list_nothrow (int);
249
250 static int is_hardware_watchpoint (const struct breakpoint *bpt);
251
252 static void insert_breakpoint_locations (void);
253
254 static int syscall_catchpoint_p (struct breakpoint *b);
255
256 static void tracepoints_info (char *, int);
257
258 static void delete_trace_command (char *, int);
259
260 static void enable_trace_command (char *, int);
261
262 static void disable_trace_command (char *, int);
263
264 static void trace_pass_command (char *, int);
265
266 static void set_tracepoint_count (int num);
267
268 static int is_masked_watchpoint (const struct breakpoint *b);
269
270 static struct bp_location **get_first_locp_gte_addr (CORE_ADDR address);
271
272 /* Return 1 if B refers to a static tracepoint set by marker ("-m"), zero
273    otherwise.  */
274
275 static int strace_marker_p (struct breakpoint *b);
276
277 /* The abstract base class all breakpoint_ops structures inherit
278    from.  */
279 struct breakpoint_ops base_breakpoint_ops;
280
281 /* The breakpoint_ops structure to be inherited by all breakpoint_ops
282    that are implemented on top of software or hardware breakpoints
283    (user breakpoints, internal and momentary breakpoints, etc.).  */
284 static struct breakpoint_ops bkpt_base_breakpoint_ops;
285
286 /* Internal breakpoints class type.  */
287 static struct breakpoint_ops internal_breakpoint_ops;
288
289 /* Momentary breakpoints class type.  */
290 static struct breakpoint_ops momentary_breakpoint_ops;
291
292 /* Momentary breakpoints for bp_longjmp and bp_exception class type.  */
293 static struct breakpoint_ops longjmp_breakpoint_ops;
294
295 /* The breakpoint_ops structure to be used in regular user created
296    breakpoints.  */
297 struct breakpoint_ops bkpt_breakpoint_ops;
298
299 /* Breakpoints set on probes.  */
300 static struct breakpoint_ops bkpt_probe_breakpoint_ops;
301
302 /* Dynamic printf class type.  */
303 struct breakpoint_ops dprintf_breakpoint_ops;
304
305 /* The style in which to perform a dynamic printf.  This is a user
306    option because different output options have different tradeoffs;
307    if GDB does the printing, there is better error handling if there
308    is a problem with any of the arguments, but using an inferior
309    function lets you have special-purpose printers and sending of
310    output to the same place as compiled-in print functions.  */
311
312 static const char dprintf_style_gdb[] = "gdb";
313 static const char dprintf_style_call[] = "call";
314 static const char dprintf_style_agent[] = "agent";
315 static const char *const dprintf_style_enums[] = {
316   dprintf_style_gdb,
317   dprintf_style_call,
318   dprintf_style_agent,
319   NULL
320 };
321 static const char *dprintf_style = dprintf_style_gdb;
322
323 /* The function to use for dynamic printf if the preferred style is to
324    call into the inferior.  The value is simply a string that is
325    copied into the command, so it can be anything that GDB can
326    evaluate to a callable address, not necessarily a function name.  */
327
328 static char *dprintf_function = "";
329
330 /* The channel to use for dynamic printf if the preferred style is to
331    call into the inferior; if a nonempty string, it will be passed to
332    the call as the first argument, with the format string as the
333    second.  As with the dprintf function, this can be anything that
334    GDB knows how to evaluate, so in addition to common choices like
335    "stderr", this could be an app-specific expression like
336    "mystreams[curlogger]".  */
337
338 static char *dprintf_channel = "";
339
340 /* True if dprintf commands should continue to operate even if GDB
341    has disconnected.  */
342 static int disconnected_dprintf = 1;
343
344 /* A reference-counted struct command_line.  This lets multiple
345    breakpoints share a single command list.  */
346 struct counted_command_line
347 {
348   /* The reference count.  */
349   int refc;
350
351   /* The command list.  */
352   struct command_line *commands;
353 };
354
355 struct command_line *
356 breakpoint_commands (struct breakpoint *b)
357 {
358   return b->commands ? b->commands->commands : NULL;
359 }
360
361 /* Flag indicating that a command has proceeded the inferior past the
362    current breakpoint.  */
363
364 static int breakpoint_proceeded;
365
366 const char *
367 bpdisp_text (enum bpdisp disp)
368 {
369   /* NOTE: the following values are a part of MI protocol and
370      represent values of 'disp' field returned when inferior stops at
371      a breakpoint.  */
372   static const char * const bpdisps[] = {"del", "dstp", "dis", "keep"};
373
374   return bpdisps[(int) disp];
375 }
376
377 /* Prototypes for exported functions.  */
378 /* If FALSE, gdb will not use hardware support for watchpoints, even
379    if such is available.  */
380 static int can_use_hw_watchpoints;
381
382 static void
383 show_can_use_hw_watchpoints (struct ui_file *file, int from_tty,
384                              struct cmd_list_element *c,
385                              const char *value)
386 {
387   fprintf_filtered (file,
388                     _("Debugger's willingness to use "
389                       "watchpoint hardware is %s.\n"),
390                     value);
391 }
392
393 /* If AUTO_BOOLEAN_FALSE, gdb will not attempt to create pending breakpoints.
394    If AUTO_BOOLEAN_TRUE, gdb will automatically create pending breakpoints
395    for unrecognized breakpoint locations.
396    If AUTO_BOOLEAN_AUTO, gdb will query when breakpoints are unrecognized.  */
397 static enum auto_boolean pending_break_support;
398 static void
399 show_pending_break_support (struct ui_file *file, int from_tty,
400                             struct cmd_list_element *c,
401                             const char *value)
402 {
403   fprintf_filtered (file,
404                     _("Debugger's behavior regarding "
405                       "pending breakpoints is %s.\n"),
406                     value);
407 }
408
409 /* If 1, gdb will automatically use hardware breakpoints for breakpoints
410    set with "break" but falling in read-only memory.
411    If 0, gdb will warn about such breakpoints, but won't automatically
412    use hardware breakpoints.  */
413 static int automatic_hardware_breakpoints;
414 static void
415 show_automatic_hardware_breakpoints (struct ui_file *file, int from_tty,
416                                      struct cmd_list_element *c,
417                                      const char *value)
418 {
419   fprintf_filtered (file,
420                     _("Automatic usage of hardware breakpoints is %s.\n"),
421                     value);
422 }
423
424 /* If on, gdb will keep breakpoints inserted even as inferior is
425    stopped, and immediately insert any new breakpoints.  If off, gdb
426    will insert breakpoints into inferior only when resuming it, and
427    will remove breakpoints upon stop.  If auto, GDB will behave as ON
428    if in non-stop mode, and as OFF if all-stop mode.*/
429
430 static enum auto_boolean always_inserted_mode = AUTO_BOOLEAN_AUTO;
431
432 static void
433 show_always_inserted_mode (struct ui_file *file, int from_tty,
434                      struct cmd_list_element *c, const char *value)
435 {
436   if (always_inserted_mode == AUTO_BOOLEAN_AUTO)
437     fprintf_filtered (file,
438                       _("Always inserted breakpoint "
439                         "mode is %s (currently %s).\n"),
440                       value,
441                       breakpoints_always_inserted_mode () ? "on" : "off");
442   else
443     fprintf_filtered (file, _("Always inserted breakpoint mode is %s.\n"),
444                       value);
445 }
446
447 int
448 breakpoints_always_inserted_mode (void)
449 {
450   return (always_inserted_mode == AUTO_BOOLEAN_TRUE
451           || (always_inserted_mode == AUTO_BOOLEAN_AUTO && non_stop));
452 }
453
454 static const char condition_evaluation_both[] = "host or target";
455
456 /* Modes for breakpoint condition evaluation.  */
457 static const char condition_evaluation_auto[] = "auto";
458 static const char condition_evaluation_host[] = "host";
459 static const char condition_evaluation_target[] = "target";
460 static const char *const condition_evaluation_enums[] = {
461   condition_evaluation_auto,
462   condition_evaluation_host,
463   condition_evaluation_target,
464   NULL
465 };
466
467 /* Global that holds the current mode for breakpoint condition evaluation.  */
468 static const char *condition_evaluation_mode_1 = condition_evaluation_auto;
469
470 /* Global that we use to display information to the user (gets its value from
471    condition_evaluation_mode_1.  */
472 static const char *condition_evaluation_mode = condition_evaluation_auto;
473
474 /* Translate a condition evaluation mode MODE into either "host"
475    or "target".  This is used mostly to translate from "auto" to the
476    real setting that is being used.  It returns the translated
477    evaluation mode.  */
478
479 static const char *
480 translate_condition_evaluation_mode (const char *mode)
481 {
482   if (mode == condition_evaluation_auto)
483     {
484       if (target_supports_evaluation_of_breakpoint_conditions ())
485         return condition_evaluation_target;
486       else
487         return condition_evaluation_host;
488     }
489   else
490     return mode;
491 }
492
493 /* Discovers what condition_evaluation_auto translates to.  */
494
495 static const char *
496 breakpoint_condition_evaluation_mode (void)
497 {
498   return translate_condition_evaluation_mode (condition_evaluation_mode);
499 }
500
501 /* Return true if GDB should evaluate breakpoint conditions or false
502    otherwise.  */
503
504 static int
505 gdb_evaluates_breakpoint_condition_p (void)
506 {
507   const char *mode = breakpoint_condition_evaluation_mode ();
508
509   return (mode == condition_evaluation_host);
510 }
511
512 void _initialize_breakpoint (void);
513
514 /* Are we executing breakpoint commands?  */
515 static int executing_breakpoint_commands;
516
517 /* Are overlay event breakpoints enabled? */
518 static int overlay_events_enabled;
519
520 /* See description in breakpoint.h. */
521 int target_exact_watchpoints = 0;
522
523 /* Walk the following statement or block through all breakpoints.
524    ALL_BREAKPOINTS_SAFE does so even if the statement deletes the
525    current breakpoint.  */
526
527 #define ALL_BREAKPOINTS(B)  for (B = breakpoint_chain; B; B = B->next)
528
529 #define ALL_BREAKPOINTS_SAFE(B,TMP)     \
530         for (B = breakpoint_chain;      \
531              B ? (TMP=B->next, 1): 0;   \
532              B = TMP)
533
534 /* Similar iterator for the low-level breakpoints.  SAFE variant is
535    not provided so update_global_location_list must not be called
536    while executing the block of ALL_BP_LOCATIONS.  */
537
538 #define ALL_BP_LOCATIONS(B,BP_TMP)                                      \
539         for (BP_TMP = bp_location;                                      \
540              BP_TMP < bp_location + bp_location_count && (B = *BP_TMP); \
541              BP_TMP++)
542
543 /* Iterates through locations with address ADDRESS for the currently selected
544    program space.  BP_LOCP_TMP points to each object.  BP_LOCP_START points
545    to where the loop should start from.
546    If BP_LOCP_START is a NULL pointer, the macro automatically seeks the
547    appropriate location to start with.  */
548
549 #define ALL_BP_LOCATIONS_AT_ADDR(BP_LOCP_TMP, BP_LOCP_START, ADDRESS)   \
550         for (BP_LOCP_START = BP_LOCP_START == NULL ? get_first_locp_gte_addr (ADDRESS) : BP_LOCP_START, \
551              BP_LOCP_TMP = BP_LOCP_START;                               \
552              BP_LOCP_START                                              \
553              && (BP_LOCP_TMP < bp_location + bp_location_count          \
554              && (*BP_LOCP_TMP)->address == ADDRESS);                    \
555              BP_LOCP_TMP++)
556
557 /* Iterator for tracepoints only.  */
558
559 #define ALL_TRACEPOINTS(B)  \
560   for (B = breakpoint_chain; B; B = B->next)  \
561     if (is_tracepoint (B))
562
563 /* Chains of all breakpoints defined.  */
564
565 struct breakpoint *breakpoint_chain;
566
567 /* Array is sorted by bp_location_compare - primarily by the ADDRESS.  */
568
569 static struct bp_location **bp_location;
570
571 /* Number of elements of BP_LOCATION.  */
572
573 static unsigned bp_location_count;
574
575 /* Maximum alignment offset between bp_target_info.PLACED_ADDRESS and
576    ADDRESS for the current elements of BP_LOCATION which get a valid
577    result from bp_location_has_shadow.  You can use it for roughly
578    limiting the subrange of BP_LOCATION to scan for shadow bytes for
579    an address you need to read.  */
580
581 static CORE_ADDR bp_location_placed_address_before_address_max;
582
583 /* Maximum offset plus alignment between bp_target_info.PLACED_ADDRESS
584    + bp_target_info.SHADOW_LEN and ADDRESS for the current elements of
585    BP_LOCATION which get a valid result from bp_location_has_shadow.
586    You can use it for roughly limiting the subrange of BP_LOCATION to
587    scan for shadow bytes for an address you need to read.  */
588
589 static CORE_ADDR bp_location_shadow_len_after_address_max;
590
591 /* The locations that no longer correspond to any breakpoint, unlinked
592    from bp_location array, but for which a hit may still be reported
593    by a target.  */
594 VEC(bp_location_p) *moribund_locations = NULL;
595
596 /* Number of last breakpoint made.  */
597
598 static int breakpoint_count;
599
600 /* The value of `breakpoint_count' before the last command that
601    created breakpoints.  If the last (break-like) command created more
602    than one breakpoint, then the difference between BREAKPOINT_COUNT
603    and PREV_BREAKPOINT_COUNT is more than one.  */
604 static int prev_breakpoint_count;
605
606 /* Number of last tracepoint made.  */
607
608 static int tracepoint_count;
609
610 static struct cmd_list_element *breakpoint_set_cmdlist;
611 static struct cmd_list_element *breakpoint_show_cmdlist;
612 struct cmd_list_element *save_cmdlist;
613
614 /* Return whether a breakpoint is an active enabled breakpoint.  */
615 static int
616 breakpoint_enabled (struct breakpoint *b)
617 {
618   return (b->enable_state == bp_enabled);
619 }
620
621 /* Set breakpoint count to NUM.  */
622
623 static void
624 set_breakpoint_count (int num)
625 {
626   prev_breakpoint_count = breakpoint_count;
627   breakpoint_count = num;
628   set_internalvar_integer (lookup_internalvar ("bpnum"), num);
629 }
630
631 /* Used by `start_rbreak_breakpoints' below, to record the current
632    breakpoint count before "rbreak" creates any breakpoint.  */
633 static int rbreak_start_breakpoint_count;
634
635 /* Called at the start an "rbreak" command to record the first
636    breakpoint made.  */
637
638 void
639 start_rbreak_breakpoints (void)
640 {
641   rbreak_start_breakpoint_count = breakpoint_count;
642 }
643
644 /* Called at the end of an "rbreak" command to record the last
645    breakpoint made.  */
646
647 void
648 end_rbreak_breakpoints (void)
649 {
650   prev_breakpoint_count = rbreak_start_breakpoint_count;
651 }
652
653 /* Used in run_command to zero the hit count when a new run starts.  */
654
655 void
656 clear_breakpoint_hit_counts (void)
657 {
658   struct breakpoint *b;
659
660   ALL_BREAKPOINTS (b)
661     b->hit_count = 0;
662 }
663
664 /* Allocate a new counted_command_line with reference count of 1.
665    The new structure owns COMMANDS.  */
666
667 static struct counted_command_line *
668 alloc_counted_command_line (struct command_line *commands)
669 {
670   struct counted_command_line *result
671     = xmalloc (sizeof (struct counted_command_line));
672
673   result->refc = 1;
674   result->commands = commands;
675   return result;
676 }
677
678 /* Increment reference count.  This does nothing if CMD is NULL.  */
679
680 static void
681 incref_counted_command_line (struct counted_command_line *cmd)
682 {
683   if (cmd)
684     ++cmd->refc;
685 }
686
687 /* Decrement reference count.  If the reference count reaches 0,
688    destroy the counted_command_line.  Sets *CMDP to NULL.  This does
689    nothing if *CMDP is NULL.  */
690
691 static void
692 decref_counted_command_line (struct counted_command_line **cmdp)
693 {
694   if (*cmdp)
695     {
696       if (--(*cmdp)->refc == 0)
697         {
698           free_command_lines (&(*cmdp)->commands);
699           xfree (*cmdp);
700         }
701       *cmdp = NULL;
702     }
703 }
704
705 /* A cleanup function that calls decref_counted_command_line.  */
706
707 static void
708 do_cleanup_counted_command_line (void *arg)
709 {
710   decref_counted_command_line (arg);
711 }
712
713 /* Create a cleanup that calls decref_counted_command_line on the
714    argument.  */
715
716 static struct cleanup *
717 make_cleanup_decref_counted_command_line (struct counted_command_line **cmdp)
718 {
719   return make_cleanup (do_cleanup_counted_command_line, cmdp);
720 }
721
722 \f
723 /* Return the breakpoint with the specified number, or NULL
724    if the number does not refer to an existing breakpoint.  */
725
726 struct breakpoint *
727 get_breakpoint (int num)
728 {
729   struct breakpoint *b;
730
731   ALL_BREAKPOINTS (b)
732     if (b->number == num)
733       return b;
734   
735   return NULL;
736 }
737
738 \f
739
740 /* Mark locations as "conditions have changed" in case the target supports
741    evaluating conditions on its side.  */
742
743 static void
744 mark_breakpoint_modified (struct breakpoint *b)
745 {
746   struct bp_location *loc;
747
748   /* This is only meaningful if the target is
749      evaluating conditions and if the user has
750      opted for condition evaluation on the target's
751      side.  */
752   if (gdb_evaluates_breakpoint_condition_p ()
753       || !target_supports_evaluation_of_breakpoint_conditions ())
754     return;
755
756   if (!is_breakpoint (b))
757     return;
758
759   for (loc = b->loc; loc; loc = loc->next)
760     loc->condition_changed = condition_modified;
761 }
762
763 /* Mark location as "conditions have changed" in case the target supports
764    evaluating conditions on its side.  */
765
766 static void
767 mark_breakpoint_location_modified (struct bp_location *loc)
768 {
769   /* This is only meaningful if the target is
770      evaluating conditions and if the user has
771      opted for condition evaluation on the target's
772      side.  */
773   if (gdb_evaluates_breakpoint_condition_p ()
774       || !target_supports_evaluation_of_breakpoint_conditions ())
775
776     return;
777
778   if (!is_breakpoint (loc->owner))
779     return;
780
781   loc->condition_changed = condition_modified;
782 }
783
784 /* Sets the condition-evaluation mode using the static global
785    condition_evaluation_mode.  */
786
787 static void
788 set_condition_evaluation_mode (char *args, int from_tty,
789                                struct cmd_list_element *c)
790 {
791   const char *old_mode, *new_mode;
792
793   if ((condition_evaluation_mode_1 == condition_evaluation_target)
794       && !target_supports_evaluation_of_breakpoint_conditions ())
795     {
796       condition_evaluation_mode_1 = condition_evaluation_mode;
797       warning (_("Target does not support breakpoint condition evaluation.\n"
798                  "Using host evaluation mode instead."));
799       return;
800     }
801
802   new_mode = translate_condition_evaluation_mode (condition_evaluation_mode_1);
803   old_mode = translate_condition_evaluation_mode (condition_evaluation_mode);
804
805   /* Flip the switch.  Flip it even if OLD_MODE == NEW_MODE as one of the
806      settings was "auto".  */
807   condition_evaluation_mode = condition_evaluation_mode_1;
808
809   /* Only update the mode if the user picked a different one.  */
810   if (new_mode != old_mode)
811     {
812       struct bp_location *loc, **loc_tmp;
813       /* If the user switched to a different evaluation mode, we
814          need to synch the changes with the target as follows:
815
816          "host" -> "target": Send all (valid) conditions to the target.
817          "target" -> "host": Remove all the conditions from the target.
818       */
819
820       if (new_mode == condition_evaluation_target)
821         {
822           /* Mark everything modified and synch conditions with the
823              target.  */
824           ALL_BP_LOCATIONS (loc, loc_tmp)
825             mark_breakpoint_location_modified (loc);
826         }
827       else
828         {
829           /* Manually mark non-duplicate locations to synch conditions
830              with the target.  We do this to remove all the conditions the
831              target knows about.  */
832           ALL_BP_LOCATIONS (loc, loc_tmp)
833             if (is_breakpoint (loc->owner) && loc->inserted)
834               loc->needs_update = 1;
835         }
836
837       /* Do the update.  */
838       update_global_location_list (1);
839     }
840
841   return;
842 }
843
844 /* Shows the current mode of breakpoint condition evaluation.  Explicitly shows
845    what "auto" is translating to.  */
846
847 static void
848 show_condition_evaluation_mode (struct ui_file *file, int from_tty,
849                                 struct cmd_list_element *c, const char *value)
850 {
851   if (condition_evaluation_mode == condition_evaluation_auto)
852     fprintf_filtered (file,
853                       _("Breakpoint condition evaluation "
854                         "mode is %s (currently %s).\n"),
855                       value,
856                       breakpoint_condition_evaluation_mode ());
857   else
858     fprintf_filtered (file, _("Breakpoint condition evaluation mode is %s.\n"),
859                       value);
860 }
861
862 /* A comparison function for bp_location AP and BP that is used by
863    bsearch.  This comparison function only cares about addresses, unlike
864    the more general bp_location_compare function.  */
865
866 static int
867 bp_location_compare_addrs (const void *ap, const void *bp)
868 {
869   struct bp_location *a = *(void **) ap;
870   struct bp_location *b = *(void **) bp;
871
872   if (a->address == b->address)
873     return 0;
874   else
875     return ((a->address > b->address) - (a->address < b->address));
876 }
877
878 /* Helper function to skip all bp_locations with addresses
879    less than ADDRESS.  It returns the first bp_location that
880    is greater than or equal to ADDRESS.  If none is found, just
881    return NULL.  */
882
883 static struct bp_location **
884 get_first_locp_gte_addr (CORE_ADDR address)
885 {
886   struct bp_location dummy_loc;
887   struct bp_location *dummy_locp = &dummy_loc;
888   struct bp_location **locp_found = NULL;
889
890   /* Initialize the dummy location's address field.  */
891   memset (&dummy_loc, 0, sizeof (struct bp_location));
892   dummy_loc.address = address;
893
894   /* Find a close match to the first location at ADDRESS.  */
895   locp_found = bsearch (&dummy_locp, bp_location, bp_location_count,
896                         sizeof (struct bp_location **),
897                         bp_location_compare_addrs);
898
899   /* Nothing was found, nothing left to do.  */
900   if (locp_found == NULL)
901     return NULL;
902
903   /* We may have found a location that is at ADDRESS but is not the first in the
904      location's list.  Go backwards (if possible) and locate the first one.  */
905   while ((locp_found - 1) >= bp_location
906          && (*(locp_found - 1))->address == address)
907     locp_found--;
908
909   return locp_found;
910 }
911
912 void
913 set_breakpoint_condition (struct breakpoint *b, char *exp,
914                           int from_tty)
915 {
916   xfree (b->cond_string);
917   b->cond_string = NULL;
918
919   if (is_watchpoint (b))
920     {
921       struct watchpoint *w = (struct watchpoint *) b;
922
923       xfree (w->cond_exp);
924       w->cond_exp = NULL;
925     }
926   else
927     {
928       struct bp_location *loc;
929
930       for (loc = b->loc; loc; loc = loc->next)
931         {
932           xfree (loc->cond);
933           loc->cond = NULL;
934
935           /* No need to free the condition agent expression
936              bytecode (if we have one).  We will handle this
937              when we go through update_global_location_list.  */
938         }
939     }
940
941   if (*exp == 0)
942     {
943       if (from_tty)
944         printf_filtered (_("Breakpoint %d now unconditional.\n"), b->number);
945     }
946   else
947     {
948       const char *arg = exp;
949
950       /* I don't know if it matters whether this is the string the user
951          typed in or the decompiled expression.  */
952       b->cond_string = xstrdup (arg);
953       b->condition_not_parsed = 0;
954
955       if (is_watchpoint (b))
956         {
957           struct watchpoint *w = (struct watchpoint *) b;
958
959           innermost_block = NULL;
960           arg = exp;
961           w->cond_exp = parse_exp_1 (&arg, 0, 0, 0);
962           if (*arg)
963             error (_("Junk at end of expression"));
964           w->cond_exp_valid_block = innermost_block;
965         }
966       else
967         {
968           struct bp_location *loc;
969
970           for (loc = b->loc; loc; loc = loc->next)
971             {
972               arg = exp;
973               loc->cond =
974                 parse_exp_1 (&arg, loc->address,
975                              block_for_pc (loc->address), 0);
976               if (*arg)
977                 error (_("Junk at end of expression"));
978             }
979         }
980     }
981   mark_breakpoint_modified (b);
982
983   observer_notify_breakpoint_modified (b);
984 }
985
986 /* Completion for the "condition" command.  */
987
988 static VEC (char_ptr) *
989 condition_completer (struct cmd_list_element *cmd,
990                      const char *text, const char *word)
991 {
992   const char *space;
993
994   text = skip_spaces_const (text);
995   space = skip_to_space_const (text);
996   if (*space == '\0')
997     {
998       int len;
999       struct breakpoint *b;
1000       VEC (char_ptr) *result = NULL;
1001
1002       if (text[0] == '$')
1003         {
1004           /* We don't support completion of history indices.  */
1005           if (isdigit (text[1]))
1006             return NULL;
1007           return complete_internalvar (&text[1]);
1008         }
1009
1010       /* We're completing the breakpoint number.  */
1011       len = strlen (text);
1012
1013       ALL_BREAKPOINTS (b)
1014         {
1015           char number[50];
1016
1017           xsnprintf (number, sizeof (number), "%d", b->number);
1018
1019           if (strncmp (number, text, len) == 0)
1020             VEC_safe_push (char_ptr, result, xstrdup (number));
1021         }
1022
1023       return result;
1024     }
1025
1026   /* We're completing the expression part.  */
1027   text = skip_spaces_const (space);
1028   return expression_completer (cmd, text, word);
1029 }
1030
1031 /* condition N EXP -- set break condition of breakpoint N to EXP.  */
1032
1033 static void
1034 condition_command (char *arg, int from_tty)
1035 {
1036   struct breakpoint *b;
1037   char *p;
1038   int bnum;
1039
1040   if (arg == 0)
1041     error_no_arg (_("breakpoint number"));
1042
1043   p = arg;
1044   bnum = get_number (&p);
1045   if (bnum == 0)
1046     error (_("Bad breakpoint argument: '%s'"), arg);
1047
1048   ALL_BREAKPOINTS (b)
1049     if (b->number == bnum)
1050       {
1051         /* Check if this breakpoint has a Python object assigned to
1052            it, and if it has a definition of the "stop"
1053            method.  This method and conditions entered into GDB from
1054            the CLI are mutually exclusive.  */
1055         if (b->py_bp_object
1056             && gdbpy_breakpoint_has_py_cond (b->py_bp_object))
1057           error (_("Cannot set a condition where a Python 'stop' "
1058                    "method has been defined in the breakpoint."));
1059         set_breakpoint_condition (b, p, from_tty);
1060
1061         if (is_breakpoint (b))
1062           update_global_location_list (1);
1063
1064         return;
1065       }
1066
1067   error (_("No breakpoint number %d."), bnum);
1068 }
1069
1070 /* Check that COMMAND do not contain commands that are suitable
1071    only for tracepoints and not suitable for ordinary breakpoints.
1072    Throw if any such commands is found.  */
1073
1074 static void
1075 check_no_tracepoint_commands (struct command_line *commands)
1076 {
1077   struct command_line *c;
1078
1079   for (c = commands; c; c = c->next)
1080     {
1081       int i;
1082
1083       if (c->control_type == while_stepping_control)
1084         error (_("The 'while-stepping' command can "
1085                  "only be used for tracepoints"));
1086
1087       for (i = 0; i < c->body_count; ++i)
1088         check_no_tracepoint_commands ((c->body_list)[i]);
1089
1090       /* Not that command parsing removes leading whitespace and comment
1091          lines and also empty lines.  So, we only need to check for
1092          command directly.  */
1093       if (strstr (c->line, "collect ") == c->line)
1094         error (_("The 'collect' command can only be used for tracepoints"));
1095
1096       if (strstr (c->line, "teval ") == c->line)
1097         error (_("The 'teval' command can only be used for tracepoints"));
1098     }
1099 }
1100
1101 /* Encapsulate tests for different types of tracepoints.  */
1102
1103 static int
1104 is_tracepoint_type (enum bptype type)
1105 {
1106   return (type == bp_tracepoint
1107           || type == bp_fast_tracepoint
1108           || type == bp_static_tracepoint);
1109 }
1110
1111 int
1112 is_tracepoint (const struct breakpoint *b)
1113 {
1114   return is_tracepoint_type (b->type);
1115 }
1116
1117 /* A helper function that validates that COMMANDS are valid for a
1118    breakpoint.  This function will throw an exception if a problem is
1119    found.  */
1120
1121 static void
1122 validate_commands_for_breakpoint (struct breakpoint *b,
1123                                   struct command_line *commands)
1124 {
1125   if (is_tracepoint (b))
1126     {
1127       struct tracepoint *t = (struct tracepoint *) b;
1128       struct command_line *c;
1129       struct command_line *while_stepping = 0;
1130
1131       /* Reset the while-stepping step count.  The previous commands
1132          might have included a while-stepping action, while the new
1133          ones might not.  */
1134       t->step_count = 0;
1135
1136       /* We need to verify that each top-level element of commands is
1137          valid for tracepoints, that there's at most one
1138          while-stepping element, and that the while-stepping's body
1139          has valid tracing commands excluding nested while-stepping.
1140          We also need to validate the tracepoint action line in the
1141          context of the tracepoint --- validate_actionline actually
1142          has side effects, like setting the tracepoint's
1143          while-stepping STEP_COUNT, in addition to checking if the
1144          collect/teval actions parse and make sense in the
1145          tracepoint's context.  */
1146       for (c = commands; c; c = c->next)
1147         {
1148           if (c->control_type == while_stepping_control)
1149             {
1150               if (b->type == bp_fast_tracepoint)
1151                 error (_("The 'while-stepping' command "
1152                          "cannot be used for fast tracepoint"));
1153               else if (b->type == bp_static_tracepoint)
1154                 error (_("The 'while-stepping' command "
1155                          "cannot be used for static tracepoint"));
1156
1157               if (while_stepping)
1158                 error (_("The 'while-stepping' command "
1159                          "can be used only once"));
1160               else
1161                 while_stepping = c;
1162             }
1163
1164           validate_actionline (c->line, b);
1165         }
1166       if (while_stepping)
1167         {
1168           struct command_line *c2;
1169
1170           gdb_assert (while_stepping->body_count == 1);
1171           c2 = while_stepping->body_list[0];
1172           for (; c2; c2 = c2->next)
1173             {
1174               if (c2->control_type == while_stepping_control)
1175                 error (_("The 'while-stepping' command cannot be nested"));
1176             }
1177         }
1178     }
1179   else
1180     {
1181       check_no_tracepoint_commands (commands);
1182     }
1183 }
1184
1185 /* Return a vector of all the static tracepoints set at ADDR.  The
1186    caller is responsible for releasing the vector.  */
1187
1188 VEC(breakpoint_p) *
1189 static_tracepoints_here (CORE_ADDR addr)
1190 {
1191   struct breakpoint *b;
1192   VEC(breakpoint_p) *found = 0;
1193   struct bp_location *loc;
1194
1195   ALL_BREAKPOINTS (b)
1196     if (b->type == bp_static_tracepoint)
1197       {
1198         for (loc = b->loc; loc; loc = loc->next)
1199           if (loc->address == addr)
1200             VEC_safe_push(breakpoint_p, found, b);
1201       }
1202
1203   return found;
1204 }
1205
1206 /* Set the command list of B to COMMANDS.  If breakpoint is tracepoint,
1207    validate that only allowed commands are included.  */
1208
1209 void
1210 breakpoint_set_commands (struct breakpoint *b, 
1211                          struct command_line *commands)
1212 {
1213   validate_commands_for_breakpoint (b, commands);
1214
1215   decref_counted_command_line (&b->commands);
1216   b->commands = alloc_counted_command_line (commands);
1217   observer_notify_breakpoint_modified (b);
1218 }
1219
1220 /* Set the internal `silent' flag on the breakpoint.  Note that this
1221    is not the same as the "silent" that may appear in the breakpoint's
1222    commands.  */
1223
1224 void
1225 breakpoint_set_silent (struct breakpoint *b, int silent)
1226 {
1227   int old_silent = b->silent;
1228
1229   b->silent = silent;
1230   if (old_silent != silent)
1231     observer_notify_breakpoint_modified (b);
1232 }
1233
1234 /* Set the thread for this breakpoint.  If THREAD is -1, make the
1235    breakpoint work for any thread.  */
1236
1237 void
1238 breakpoint_set_thread (struct breakpoint *b, int thread)
1239 {
1240   int old_thread = b->thread;
1241
1242   b->thread = thread;
1243   if (old_thread != thread)
1244     observer_notify_breakpoint_modified (b);
1245 }
1246
1247 /* Set the task for this breakpoint.  If TASK is 0, make the
1248    breakpoint work for any task.  */
1249
1250 void
1251 breakpoint_set_task (struct breakpoint *b, int task)
1252 {
1253   int old_task = b->task;
1254
1255   b->task = task;
1256   if (old_task != task)
1257     observer_notify_breakpoint_modified (b);
1258 }
1259
1260 void
1261 check_tracepoint_command (char *line, void *closure)
1262 {
1263   struct breakpoint *b = closure;
1264
1265   validate_actionline (line, b);
1266 }
1267
1268 /* A structure used to pass information through
1269    map_breakpoint_numbers.  */
1270
1271 struct commands_info
1272 {
1273   /* True if the command was typed at a tty.  */
1274   int from_tty;
1275
1276   /* The breakpoint range spec.  */
1277   char *arg;
1278
1279   /* Non-NULL if the body of the commands are being read from this
1280      already-parsed command.  */
1281   struct command_line *control;
1282
1283   /* The command lines read from the user, or NULL if they have not
1284      yet been read.  */
1285   struct counted_command_line *cmd;
1286 };
1287
1288 /* A callback for map_breakpoint_numbers that sets the commands for
1289    commands_command.  */
1290
1291 static void
1292 do_map_commands_command (struct breakpoint *b, void *data)
1293 {
1294   struct commands_info *info = data;
1295
1296   if (info->cmd == NULL)
1297     {
1298       struct command_line *l;
1299
1300       if (info->control != NULL)
1301         l = copy_command_lines (info->control->body_list[0]);
1302       else
1303         {
1304           struct cleanup *old_chain;
1305           char *str;
1306
1307           str = xstrprintf (_("Type commands for breakpoint(s) "
1308                               "%s, one per line."),
1309                             info->arg);
1310
1311           old_chain = make_cleanup (xfree, str);
1312
1313           l = read_command_lines (str,
1314                                   info->from_tty, 1,
1315                                   (is_tracepoint (b)
1316                                    ? check_tracepoint_command : 0),
1317                                   b);
1318
1319           do_cleanups (old_chain);
1320         }
1321
1322       info->cmd = alloc_counted_command_line (l);
1323     }
1324
1325   /* If a breakpoint was on the list more than once, we don't need to
1326      do anything.  */
1327   if (b->commands != info->cmd)
1328     {
1329       validate_commands_for_breakpoint (b, info->cmd->commands);
1330       incref_counted_command_line (info->cmd);
1331       decref_counted_command_line (&b->commands);
1332       b->commands = info->cmd;
1333       observer_notify_breakpoint_modified (b);
1334     }
1335 }
1336
1337 static void
1338 commands_command_1 (char *arg, int from_tty, 
1339                     struct command_line *control)
1340 {
1341   struct cleanup *cleanups;
1342   struct commands_info info;
1343
1344   info.from_tty = from_tty;
1345   info.control = control;
1346   info.cmd = NULL;
1347   /* If we read command lines from the user, then `info' will hold an
1348      extra reference to the commands that we must clean up.  */
1349   cleanups = make_cleanup_decref_counted_command_line (&info.cmd);
1350
1351   if (arg == NULL || !*arg)
1352     {
1353       if (breakpoint_count - prev_breakpoint_count > 1)
1354         arg = xstrprintf ("%d-%d", prev_breakpoint_count + 1, 
1355                           breakpoint_count);
1356       else if (breakpoint_count > 0)
1357         arg = xstrprintf ("%d", breakpoint_count);
1358       else
1359         {
1360           /* So that we don't try to free the incoming non-NULL
1361              argument in the cleanup below.  Mapping breakpoint
1362              numbers will fail in this case.  */
1363           arg = NULL;
1364         }
1365     }
1366   else
1367     /* The command loop has some static state, so we need to preserve
1368        our argument.  */
1369     arg = xstrdup (arg);
1370
1371   if (arg != NULL)
1372     make_cleanup (xfree, arg);
1373
1374   info.arg = arg;
1375
1376   map_breakpoint_numbers (arg, do_map_commands_command, &info);
1377
1378   if (info.cmd == NULL)
1379     error (_("No breakpoints specified."));
1380
1381   do_cleanups (cleanups);
1382 }
1383
1384 static void
1385 commands_command (char *arg, int from_tty)
1386 {
1387   commands_command_1 (arg, from_tty, NULL);
1388 }
1389
1390 /* Like commands_command, but instead of reading the commands from
1391    input stream, takes them from an already parsed command structure.
1392
1393    This is used by cli-script.c to DTRT with breakpoint commands
1394    that are part of if and while bodies.  */
1395 enum command_control_type
1396 commands_from_control_command (char *arg, struct command_line *cmd)
1397 {
1398   commands_command_1 (arg, 0, cmd);
1399   return simple_control;
1400 }
1401
1402 /* Return non-zero if BL->TARGET_INFO contains valid information.  */
1403
1404 static int
1405 bp_location_has_shadow (struct bp_location *bl)
1406 {
1407   if (bl->loc_type != bp_loc_software_breakpoint)
1408     return 0;
1409   if (!bl->inserted)
1410     return 0;
1411   if (bl->target_info.shadow_len == 0)
1412     /* BL isn't valid, or doesn't shadow memory.  */
1413     return 0;
1414   return 1;
1415 }
1416
1417 /* Update BUF, which is LEN bytes read from the target address MEMADDR,
1418    by replacing any memory breakpoints with their shadowed contents.
1419
1420    If READBUF is not NULL, this buffer must not overlap with any of
1421    the breakpoint location's shadow_contents buffers.  Otherwise,
1422    a failed assertion internal error will be raised.
1423
1424    The range of shadowed area by each bp_location is:
1425      bl->address - bp_location_placed_address_before_address_max
1426      up to bl->address + bp_location_shadow_len_after_address_max
1427    The range we were requested to resolve shadows for is:
1428      memaddr ... memaddr + len
1429    Thus the safe cutoff boundaries for performance optimization are
1430      memaddr + len <= (bl->address
1431                        - bp_location_placed_address_before_address_max)
1432    and:
1433      bl->address + bp_location_shadow_len_after_address_max <= memaddr  */
1434
1435 void
1436 breakpoint_xfer_memory (gdb_byte *readbuf, gdb_byte *writebuf,
1437                         const gdb_byte *writebuf_org,
1438                         ULONGEST memaddr, LONGEST len)
1439 {
1440   /* Left boundary, right boundary and median element of our binary
1441      search.  */
1442   unsigned bc_l, bc_r, bc;
1443
1444   /* Find BC_L which is a leftmost element which may affect BUF
1445      content.  It is safe to report lower value but a failure to
1446      report higher one.  */
1447
1448   bc_l = 0;
1449   bc_r = bp_location_count;
1450   while (bc_l + 1 < bc_r)
1451     {
1452       struct bp_location *bl;
1453
1454       bc = (bc_l + bc_r) / 2;
1455       bl = bp_location[bc];
1456
1457       /* Check first BL->ADDRESS will not overflow due to the added
1458          constant.  Then advance the left boundary only if we are sure
1459          the BC element can in no way affect the BUF content (MEMADDR
1460          to MEMADDR + LEN range).
1461
1462          Use the BP_LOCATION_SHADOW_LEN_AFTER_ADDRESS_MAX safety
1463          offset so that we cannot miss a breakpoint with its shadow
1464          range tail still reaching MEMADDR.  */
1465
1466       if ((bl->address + bp_location_shadow_len_after_address_max
1467            >= bl->address)
1468           && (bl->address + bp_location_shadow_len_after_address_max
1469               <= memaddr))
1470         bc_l = bc;
1471       else
1472         bc_r = bc;
1473     }
1474
1475   /* Due to the binary search above, we need to make sure we pick the
1476      first location that's at BC_L's address.  E.g., if there are
1477      multiple locations at the same address, BC_L may end up pointing
1478      at a duplicate location, and miss the "master"/"inserted"
1479      location.  Say, given locations L1, L2 and L3 at addresses A and
1480      B:
1481
1482       L1@A, L2@A, L3@B, ...
1483
1484      BC_L could end up pointing at location L2, while the "master"
1485      location could be L1.  Since the `loc->inserted' flag is only set
1486      on "master" locations, we'd forget to restore the shadow of L1
1487      and L2.  */
1488   while (bc_l > 0
1489          && bp_location[bc_l]->address == bp_location[bc_l - 1]->address)
1490     bc_l--;
1491
1492   /* Now do full processing of the found relevant range of elements.  */
1493
1494   for (bc = bc_l; bc < bp_location_count; bc++)
1495   {
1496     struct bp_location *bl = bp_location[bc];
1497     CORE_ADDR bp_addr = 0;
1498     int bp_size = 0;
1499     int bptoffset = 0;
1500
1501     /* bp_location array has BL->OWNER always non-NULL.  */
1502     if (bl->owner->type == bp_none)
1503       warning (_("reading through apparently deleted breakpoint #%d?"),
1504                bl->owner->number);
1505
1506     /* Performance optimization: any further element can no longer affect BUF
1507        content.  */
1508
1509     if (bl->address >= bp_location_placed_address_before_address_max
1510         && memaddr + len <= (bl->address
1511                              - bp_location_placed_address_before_address_max))
1512       break;
1513
1514     if (!bp_location_has_shadow (bl))
1515       continue;
1516     if (!breakpoint_address_match (bl->target_info.placed_address_space, 0,
1517                                    current_program_space->aspace, 0))
1518       continue;
1519
1520     /* Addresses and length of the part of the breakpoint that
1521        we need to copy.  */
1522     bp_addr = bl->target_info.placed_address;
1523     bp_size = bl->target_info.shadow_len;
1524
1525     if (bp_addr + bp_size <= memaddr)
1526       /* The breakpoint is entirely before the chunk of memory we
1527          are reading.  */
1528       continue;
1529
1530     if (bp_addr >= memaddr + len)
1531       /* The breakpoint is entirely after the chunk of memory we are
1532          reading.  */
1533       continue;
1534
1535     /* Offset within shadow_contents.  */
1536     if (bp_addr < memaddr)
1537       {
1538         /* Only copy the second part of the breakpoint.  */
1539         bp_size -= memaddr - bp_addr;
1540         bptoffset = memaddr - bp_addr;
1541         bp_addr = memaddr;
1542       }
1543
1544     if (bp_addr + bp_size > memaddr + len)
1545       {
1546         /* Only copy the first part of the breakpoint.  */
1547         bp_size -= (bp_addr + bp_size) - (memaddr + len);
1548       }
1549
1550     if (readbuf != NULL)
1551       {
1552         /* Verify that the readbuf buffer does not overlap with
1553            the shadow_contents buffer.  */
1554         gdb_assert (bl->target_info.shadow_contents >= readbuf + len
1555                     || readbuf >= (bl->target_info.shadow_contents
1556                                    + bl->target_info.shadow_len));
1557
1558         /* Update the read buffer with this inserted breakpoint's
1559            shadow.  */
1560         memcpy (readbuf + bp_addr - memaddr,
1561                 bl->target_info.shadow_contents + bptoffset, bp_size);
1562       }
1563     else
1564       {
1565         struct gdbarch *gdbarch = bl->gdbarch;
1566         const unsigned char *bp;
1567         CORE_ADDR placed_address = bl->target_info.placed_address;
1568         int placed_size = bl->target_info.placed_size;
1569
1570         /* Update the shadow with what we want to write to memory.  */
1571         memcpy (bl->target_info.shadow_contents + bptoffset,
1572                 writebuf_org + bp_addr - memaddr, bp_size);
1573
1574         /* Determine appropriate breakpoint contents and size for this
1575            address.  */
1576         bp = gdbarch_breakpoint_from_pc (gdbarch, &placed_address, &placed_size);
1577
1578         /* Update the final write buffer with this inserted
1579            breakpoint's INSN.  */
1580         memcpy (writebuf + bp_addr - memaddr, bp + bptoffset, bp_size);
1581       }
1582   }
1583 }
1584 \f
1585
1586 /* Return true if BPT is either a software breakpoint or a hardware
1587    breakpoint.  */
1588
1589 int
1590 is_breakpoint (const struct breakpoint *bpt)
1591 {
1592   return (bpt->type == bp_breakpoint
1593           || bpt->type == bp_hardware_breakpoint
1594           || bpt->type == bp_dprintf);
1595 }
1596
1597 /* Return true if BPT is of any hardware watchpoint kind.  */
1598
1599 static int
1600 is_hardware_watchpoint (const struct breakpoint *bpt)
1601 {
1602   return (bpt->type == bp_hardware_watchpoint
1603           || bpt->type == bp_read_watchpoint
1604           || bpt->type == bp_access_watchpoint);
1605 }
1606
1607 /* Return true if BPT is of any watchpoint kind, hardware or
1608    software.  */
1609
1610 int
1611 is_watchpoint (const struct breakpoint *bpt)
1612 {
1613   return (is_hardware_watchpoint (bpt)
1614           || bpt->type == bp_watchpoint);
1615 }
1616
1617 /* Returns true if the current thread and its running state are safe
1618    to evaluate or update watchpoint B.  Watchpoints on local
1619    expressions need to be evaluated in the context of the thread that
1620    was current when the watchpoint was created, and, that thread needs
1621    to be stopped to be able to select the correct frame context.
1622    Watchpoints on global expressions can be evaluated on any thread,
1623    and in any state.  It is presently left to the target allowing
1624    memory accesses when threads are running.  */
1625
1626 static int
1627 watchpoint_in_thread_scope (struct watchpoint *b)
1628 {
1629   return (b->base.pspace == current_program_space
1630           && (ptid_equal (b->watchpoint_thread, null_ptid)
1631               || (ptid_equal (inferior_ptid, b->watchpoint_thread)
1632                   && !is_executing (inferior_ptid))));
1633 }
1634
1635 /* Set watchpoint B to disp_del_at_next_stop, even including its possible
1636    associated bp_watchpoint_scope breakpoint.  */
1637
1638 static void
1639 watchpoint_del_at_next_stop (struct watchpoint *w)
1640 {
1641   struct breakpoint *b = &w->base;
1642
1643   if (b->related_breakpoint != b)
1644     {
1645       gdb_assert (b->related_breakpoint->type == bp_watchpoint_scope);
1646       gdb_assert (b->related_breakpoint->related_breakpoint == b);
1647       b->related_breakpoint->disposition = disp_del_at_next_stop;
1648       b->related_breakpoint->related_breakpoint = b->related_breakpoint;
1649       b->related_breakpoint = b;
1650     }
1651   b->disposition = disp_del_at_next_stop;
1652 }
1653
1654 /* Assuming that B is a watchpoint:
1655    - Reparse watchpoint expression, if REPARSE is non-zero
1656    - Evaluate expression and store the result in B->val
1657    - Evaluate the condition if there is one, and store the result
1658      in b->loc->cond.
1659    - Update the list of values that must be watched in B->loc.
1660
1661    If the watchpoint disposition is disp_del_at_next_stop, then do
1662    nothing.  If this is local watchpoint that is out of scope, delete
1663    it.
1664
1665    Even with `set breakpoint always-inserted on' the watchpoints are
1666    removed + inserted on each stop here.  Normal breakpoints must
1667    never be removed because they might be missed by a running thread
1668    when debugging in non-stop mode.  On the other hand, hardware
1669    watchpoints (is_hardware_watchpoint; processed here) are specific
1670    to each LWP since they are stored in each LWP's hardware debug
1671    registers.  Therefore, such LWP must be stopped first in order to
1672    be able to modify its hardware watchpoints.
1673
1674    Hardware watchpoints must be reset exactly once after being
1675    presented to the user.  It cannot be done sooner, because it would
1676    reset the data used to present the watchpoint hit to the user.  And
1677    it must not be done later because it could display the same single
1678    watchpoint hit during multiple GDB stops.  Note that the latter is
1679    relevant only to the hardware watchpoint types bp_read_watchpoint
1680    and bp_access_watchpoint.  False hit by bp_hardware_watchpoint is
1681    not user-visible - its hit is suppressed if the memory content has
1682    not changed.
1683
1684    The following constraints influence the location where we can reset
1685    hardware watchpoints:
1686
1687    * target_stopped_by_watchpoint and target_stopped_data_address are
1688      called several times when GDB stops.
1689
1690    [linux] 
1691    * Multiple hardware watchpoints can be hit at the same time,
1692      causing GDB to stop.  GDB only presents one hardware watchpoint
1693      hit at a time as the reason for stopping, and all the other hits
1694      are presented later, one after the other, each time the user
1695      requests the execution to be resumed.  Execution is not resumed
1696      for the threads still having pending hit event stored in
1697      LWP_INFO->STATUS.  While the watchpoint is already removed from
1698      the inferior on the first stop the thread hit event is kept being
1699      reported from its cached value by linux_nat_stopped_data_address
1700      until the real thread resume happens after the watchpoint gets
1701      presented and thus its LWP_INFO->STATUS gets reset.
1702
1703    Therefore the hardware watchpoint hit can get safely reset on the
1704    watchpoint removal from inferior.  */
1705
1706 static void
1707 update_watchpoint (struct watchpoint *b, int reparse)
1708 {
1709   int within_current_scope;
1710   struct frame_id saved_frame_id;
1711   int frame_saved;
1712
1713   /* If this is a local watchpoint, we only want to check if the
1714      watchpoint frame is in scope if the current thread is the thread
1715      that was used to create the watchpoint.  */
1716   if (!watchpoint_in_thread_scope (b))
1717     return;
1718
1719   if (b->base.disposition == disp_del_at_next_stop)
1720     return;
1721  
1722   frame_saved = 0;
1723
1724   /* Determine if the watchpoint is within scope.  */
1725   if (b->exp_valid_block == NULL)
1726     within_current_scope = 1;
1727   else
1728     {
1729       struct frame_info *fi = get_current_frame ();
1730       struct gdbarch *frame_arch = get_frame_arch (fi);
1731       CORE_ADDR frame_pc = get_frame_pc (fi);
1732
1733       /* If we're in a function epilogue, unwinding may not work
1734          properly, so do not attempt to recreate locations at this
1735          point.  See similar comments in watchpoint_check.  */
1736       if (gdbarch_in_function_epilogue_p (frame_arch, frame_pc))
1737         return;
1738
1739       /* Save the current frame's ID so we can restore it after
1740          evaluating the watchpoint expression on its own frame.  */
1741       /* FIXME drow/2003-09-09: It would be nice if evaluate_expression
1742          took a frame parameter, so that we didn't have to change the
1743          selected frame.  */
1744       frame_saved = 1;
1745       saved_frame_id = get_frame_id (get_selected_frame (NULL));
1746
1747       fi = frame_find_by_id (b->watchpoint_frame);
1748       within_current_scope = (fi != NULL);
1749       if (within_current_scope)
1750         select_frame (fi);
1751     }
1752
1753   /* We don't free locations.  They are stored in the bp_location array
1754      and update_global_location_list will eventually delete them and
1755      remove breakpoints if needed.  */
1756   b->base.loc = NULL;
1757
1758   if (within_current_scope && reparse)
1759     {
1760       const char *s;
1761
1762       if (b->exp)
1763         {
1764           xfree (b->exp);
1765           b->exp = NULL;
1766         }
1767       s = b->exp_string_reparse ? b->exp_string_reparse : b->exp_string;
1768       b->exp = parse_exp_1 (&s, 0, b->exp_valid_block, 0);
1769       /* If the meaning of expression itself changed, the old value is
1770          no longer relevant.  We don't want to report a watchpoint hit
1771          to the user when the old value and the new value may actually
1772          be completely different objects.  */
1773       value_free (b->val);
1774       b->val = NULL;
1775       b->val_valid = 0;
1776
1777       /* Note that unlike with breakpoints, the watchpoint's condition
1778          expression is stored in the breakpoint object, not in the
1779          locations (re)created below.  */
1780       if (b->base.cond_string != NULL)
1781         {
1782           if (b->cond_exp != NULL)
1783             {
1784               xfree (b->cond_exp);
1785               b->cond_exp = NULL;
1786             }
1787
1788           s = b->base.cond_string;
1789           b->cond_exp = parse_exp_1 (&s, 0, b->cond_exp_valid_block, 0);
1790         }
1791     }
1792
1793   /* If we failed to parse the expression, for example because
1794      it refers to a global variable in a not-yet-loaded shared library,
1795      don't try to insert watchpoint.  We don't automatically delete
1796      such watchpoint, though, since failure to parse expression
1797      is different from out-of-scope watchpoint.  */
1798   if (!target_has_execution)
1799     {
1800       /* Without execution, memory can't change.  No use to try and
1801          set watchpoint locations.  The watchpoint will be reset when
1802          the target gains execution, through breakpoint_re_set.  */
1803       if (!can_use_hw_watchpoints)
1804         {
1805           if (b->base.ops->works_in_software_mode (&b->base))
1806             b->base.type = bp_watchpoint;
1807           else
1808             error (_("Can't set read/access watchpoint when "
1809                      "hardware watchpoints are disabled."));
1810         }
1811     }
1812   else if (within_current_scope && b->exp)
1813     {
1814       int pc = 0;
1815       struct value *val_chain, *v, *result, *next;
1816       struct program_space *frame_pspace;
1817
1818       fetch_subexp_value (b->exp, &pc, &v, &result, &val_chain, 0);
1819
1820       /* Avoid setting b->val if it's already set.  The meaning of
1821          b->val is 'the last value' user saw, and we should update
1822          it only if we reported that last value to user.  As it
1823          happens, the code that reports it updates b->val directly.
1824          We don't keep track of the memory value for masked
1825          watchpoints.  */
1826       if (!b->val_valid && !is_masked_watchpoint (&b->base))
1827         {
1828           b->val = v;
1829           b->val_valid = 1;
1830         }
1831
1832       frame_pspace = get_frame_program_space (get_selected_frame (NULL));
1833
1834       /* Look at each value on the value chain.  */
1835       for (v = val_chain; v; v = value_next (v))
1836         {
1837           /* If it's a memory location, and GDB actually needed
1838              its contents to evaluate the expression, then we
1839              must watch it.  If the first value returned is
1840              still lazy, that means an error occurred reading it;
1841              watch it anyway in case it becomes readable.  */
1842           if (VALUE_LVAL (v) == lval_memory
1843               && (v == val_chain || ! value_lazy (v)))
1844             {
1845               struct type *vtype = check_typedef (value_type (v));
1846
1847               /* We only watch structs and arrays if user asked
1848                  for it explicitly, never if they just happen to
1849                  appear in the middle of some value chain.  */
1850               if (v == result
1851                   || (TYPE_CODE (vtype) != TYPE_CODE_STRUCT
1852                       && TYPE_CODE (vtype) != TYPE_CODE_ARRAY))
1853                 {
1854                   CORE_ADDR addr;
1855                   int type;
1856                   struct bp_location *loc, **tmp;
1857
1858                   addr = value_address (v);
1859                   type = hw_write;
1860                   if (b->base.type == bp_read_watchpoint)
1861                     type = hw_read;
1862                   else if (b->base.type == bp_access_watchpoint)
1863                     type = hw_access;
1864
1865                   loc = allocate_bp_location (&b->base);
1866                   for (tmp = &(b->base.loc); *tmp != NULL; tmp = &((*tmp)->next))
1867                     ;
1868                   *tmp = loc;
1869                   loc->gdbarch = get_type_arch (value_type (v));
1870
1871                   loc->pspace = frame_pspace;
1872                   loc->address = addr;
1873                   loc->length = TYPE_LENGTH (value_type (v));
1874                   loc->watchpoint_type = type;
1875                 }
1876             }
1877         }
1878
1879       /* Change the type of breakpoint between hardware assisted or
1880          an ordinary watchpoint depending on the hardware support
1881          and free hardware slots.  REPARSE is set when the inferior
1882          is started.  */
1883       if (reparse)
1884         {
1885           int reg_cnt;
1886           enum bp_loc_type loc_type;
1887           struct bp_location *bl;
1888
1889           reg_cnt = can_use_hardware_watchpoint (val_chain);
1890
1891           if (reg_cnt)
1892             {
1893               int i, target_resources_ok, other_type_used;
1894               enum bptype type;
1895
1896               /* Use an exact watchpoint when there's only one memory region to be
1897                  watched, and only one debug register is needed to watch it.  */
1898               b->exact = target_exact_watchpoints && reg_cnt == 1;
1899
1900               /* We need to determine how many resources are already
1901                  used for all other hardware watchpoints plus this one
1902                  to see if we still have enough resources to also fit
1903                  this watchpoint in as well.  */
1904
1905               /* If this is a software watchpoint, we try to turn it
1906                  to a hardware one -- count resources as if B was of
1907                  hardware watchpoint type.  */
1908               type = b->base.type;
1909               if (type == bp_watchpoint)
1910                 type = bp_hardware_watchpoint;
1911
1912               /* This watchpoint may or may not have been placed on
1913                  the list yet at this point (it won't be in the list
1914                  if we're trying to create it for the first time,
1915                  through watch_command), so always account for it
1916                  manually.  */
1917
1918               /* Count resources used by all watchpoints except B.  */
1919               i = hw_watchpoint_used_count_others (&b->base, type, &other_type_used);
1920
1921               /* Add in the resources needed for B.  */
1922               i += hw_watchpoint_use_count (&b->base);
1923
1924               target_resources_ok
1925                 = target_can_use_hardware_watchpoint (type, i, other_type_used);
1926               if (target_resources_ok <= 0)
1927                 {
1928                   int sw_mode = b->base.ops->works_in_software_mode (&b->base);
1929
1930                   if (target_resources_ok == 0 && !sw_mode)
1931                     error (_("Target does not support this type of "
1932                              "hardware watchpoint."));
1933                   else if (target_resources_ok < 0 && !sw_mode)
1934                     error (_("There are not enough available hardware "
1935                              "resources for this watchpoint."));
1936
1937                   /* Downgrade to software watchpoint.  */
1938                   b->base.type = bp_watchpoint;
1939                 }
1940               else
1941                 {
1942                   /* If this was a software watchpoint, we've just
1943                      found we have enough resources to turn it to a
1944                      hardware watchpoint.  Otherwise, this is a
1945                      nop.  */
1946                   b->base.type = type;
1947                 }
1948             }
1949           else if (!b->base.ops->works_in_software_mode (&b->base))
1950             {
1951               if (!can_use_hw_watchpoints)
1952                 error (_("Can't set read/access watchpoint when "
1953                          "hardware watchpoints are disabled."));
1954               else
1955                 error (_("Expression cannot be implemented with "
1956                          "read/access watchpoint."));
1957             }
1958           else
1959             b->base.type = bp_watchpoint;
1960
1961           loc_type = (b->base.type == bp_watchpoint? bp_loc_other
1962                       : bp_loc_hardware_watchpoint);
1963           for (bl = b->base.loc; bl; bl = bl->next)
1964             bl->loc_type = loc_type;
1965         }
1966
1967       for (v = val_chain; v; v = next)
1968         {
1969           next = value_next (v);
1970           if (v != b->val)
1971             value_free (v);
1972         }
1973
1974       /* If a software watchpoint is not watching any memory, then the
1975          above left it without any location set up.  But,
1976          bpstat_stop_status requires a location to be able to report
1977          stops, so make sure there's at least a dummy one.  */
1978       if (b->base.type == bp_watchpoint && b->base.loc == NULL)
1979         {
1980           struct breakpoint *base = &b->base;
1981           base->loc = allocate_bp_location (base);
1982           base->loc->pspace = frame_pspace;
1983           base->loc->address = -1;
1984           base->loc->length = -1;
1985           base->loc->watchpoint_type = -1;
1986         }
1987     }
1988   else if (!within_current_scope)
1989     {
1990       printf_filtered (_("\
1991 Watchpoint %d deleted because the program has left the block\n\
1992 in which its expression is valid.\n"),
1993                        b->base.number);
1994       watchpoint_del_at_next_stop (b);
1995     }
1996
1997   /* Restore the selected frame.  */
1998   if (frame_saved)
1999     select_frame (frame_find_by_id (saved_frame_id));
2000 }
2001
2002
2003 /* Returns 1 iff breakpoint location should be
2004    inserted in the inferior.  We don't differentiate the type of BL's owner
2005    (breakpoint vs. tracepoint), although insert_location in tracepoint's
2006    breakpoint_ops is not defined, because in insert_bp_location,
2007    tracepoint's insert_location will not be called.  */
2008 static int
2009 should_be_inserted (struct bp_location *bl)
2010 {
2011   if (bl->owner == NULL || !breakpoint_enabled (bl->owner))
2012     return 0;
2013
2014   if (bl->owner->disposition == disp_del_at_next_stop)
2015     return 0;
2016
2017   if (!bl->enabled || bl->shlib_disabled || bl->duplicate)
2018     return 0;
2019
2020   if (user_breakpoint_p (bl->owner) && bl->pspace->executing_startup)
2021     return 0;
2022
2023   /* This is set for example, when we're attached to the parent of a
2024      vfork, and have detached from the child.  The child is running
2025      free, and we expect it to do an exec or exit, at which point the
2026      OS makes the parent schedulable again (and the target reports
2027      that the vfork is done).  Until the child is done with the shared
2028      memory region, do not insert breakpoints in the parent, otherwise
2029      the child could still trip on the parent's breakpoints.  Since
2030      the parent is blocked anyway, it won't miss any breakpoint.  */
2031   if (bl->pspace->breakpoints_not_allowed)
2032     return 0;
2033
2034   return 1;
2035 }
2036
2037 /* Same as should_be_inserted but does the check assuming
2038    that the location is not duplicated.  */
2039
2040 static int
2041 unduplicated_should_be_inserted (struct bp_location *bl)
2042 {
2043   int result;
2044   const int save_duplicate = bl->duplicate;
2045
2046   bl->duplicate = 0;
2047   result = should_be_inserted (bl);
2048   bl->duplicate = save_duplicate;
2049   return result;
2050 }
2051
2052 /* Parses a conditional described by an expression COND into an
2053    agent expression bytecode suitable for evaluation
2054    by the bytecode interpreter.  Return NULL if there was
2055    any error during parsing.  */
2056
2057 static struct agent_expr *
2058 parse_cond_to_aexpr (CORE_ADDR scope, struct expression *cond)
2059 {
2060   struct agent_expr *aexpr = NULL;
2061   volatile struct gdb_exception ex;
2062
2063   if (!cond)
2064     return NULL;
2065
2066   /* We don't want to stop processing, so catch any errors
2067      that may show up.  */
2068   TRY_CATCH (ex, RETURN_MASK_ERROR)
2069     {
2070       aexpr = gen_eval_for_expr (scope, cond);
2071     }
2072
2073   if (ex.reason < 0)
2074     {
2075       /* If we got here, it means the condition could not be parsed to a valid
2076          bytecode expression and thus can't be evaluated on the target's side.
2077          It's no use iterating through the conditions.  */
2078       return NULL;
2079     }
2080
2081   /* We have a valid agent expression.  */
2082   return aexpr;
2083 }
2084
2085 /* Based on location BL, create a list of breakpoint conditions to be
2086    passed on to the target.  If we have duplicated locations with different
2087    conditions, we will add such conditions to the list.  The idea is that the
2088    target will evaluate the list of conditions and will only notify GDB when
2089    one of them is true.  */
2090
2091 static void
2092 build_target_condition_list (struct bp_location *bl)
2093 {
2094   struct bp_location **locp = NULL, **loc2p;
2095   int null_condition_or_parse_error = 0;
2096   int modified = bl->needs_update;
2097   struct bp_location *loc;
2098
2099   /* This is only meaningful if the target is
2100      evaluating conditions and if the user has
2101      opted for condition evaluation on the target's
2102      side.  */
2103   if (gdb_evaluates_breakpoint_condition_p ()
2104       || !target_supports_evaluation_of_breakpoint_conditions ())
2105     return;
2106
2107   /* Do a first pass to check for locations with no assigned
2108      conditions or conditions that fail to parse to a valid agent expression
2109      bytecode.  If any of these happen, then it's no use to send conditions
2110      to the target since this location will always trigger and generate a
2111      response back to GDB.  */
2112   ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2113     {
2114       loc = (*loc2p);
2115       if (is_breakpoint (loc->owner) && loc->pspace->num == bl->pspace->num)
2116         {
2117           if (modified)
2118             {
2119               struct agent_expr *aexpr;
2120
2121               /* Re-parse the conditions since something changed.  In that
2122                  case we already freed the condition bytecodes (see
2123                  force_breakpoint_reinsertion).  We just
2124                  need to parse the condition to bytecodes again.  */
2125               aexpr = parse_cond_to_aexpr (bl->address, loc->cond);
2126               loc->cond_bytecode = aexpr;
2127
2128               /* Check if we managed to parse the conditional expression
2129                  correctly.  If not, we will not send this condition
2130                  to the target.  */
2131               if (aexpr)
2132                 continue;
2133             }
2134
2135           /* If we have a NULL bytecode expression, it means something
2136              went wrong or we have a null condition expression.  */
2137           if (!loc->cond_bytecode)
2138             {
2139               null_condition_or_parse_error = 1;
2140               break;
2141             }
2142         }
2143     }
2144
2145   /* If any of these happened, it means we will have to evaluate the conditions
2146      for the location's address on gdb's side.  It is no use keeping bytecodes
2147      for all the other duplicate locations, thus we free all of them here.
2148
2149      This is so we have a finer control over which locations' conditions are
2150      being evaluated by GDB or the remote stub.  */
2151   if (null_condition_or_parse_error)
2152     {
2153       ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2154         {
2155           loc = (*loc2p);
2156           if (is_breakpoint (loc->owner) && loc->pspace->num == bl->pspace->num)
2157             {
2158               /* Only go as far as the first NULL bytecode is
2159                  located.  */
2160               if (!loc->cond_bytecode)
2161                 return;
2162
2163               free_agent_expr (loc->cond_bytecode);
2164               loc->cond_bytecode = NULL;
2165             }
2166         }
2167     }
2168
2169   /* No NULL conditions or failed bytecode generation.  Build a condition list
2170      for this location's address.  */
2171   ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2172     {
2173       loc = (*loc2p);
2174       if (loc->cond
2175           && is_breakpoint (loc->owner)
2176           && loc->pspace->num == bl->pspace->num
2177           && loc->owner->enable_state == bp_enabled
2178           && loc->enabled)
2179         /* Add the condition to the vector.  This will be used later to send the
2180            conditions to the target.  */
2181         VEC_safe_push (agent_expr_p, bl->target_info.conditions,
2182                        loc->cond_bytecode);
2183     }
2184
2185   return;
2186 }
2187
2188 /* Parses a command described by string CMD into an agent expression
2189    bytecode suitable for evaluation by the bytecode interpreter.
2190    Return NULL if there was any error during parsing.  */
2191
2192 static struct agent_expr *
2193 parse_cmd_to_aexpr (CORE_ADDR scope, char *cmd)
2194 {
2195   struct cleanup *old_cleanups = 0;
2196   struct expression *expr, **argvec;
2197   struct agent_expr *aexpr = NULL;
2198   volatile struct gdb_exception ex;
2199   const char *cmdrest;
2200   const char *format_start, *format_end;
2201   struct format_piece *fpieces;
2202   int nargs;
2203   struct gdbarch *gdbarch = get_current_arch ();
2204
2205   if (!cmd)
2206     return NULL;
2207
2208   cmdrest = cmd;
2209
2210   if (*cmdrest == ',')
2211     ++cmdrest;
2212   cmdrest = skip_spaces_const (cmdrest);
2213
2214   if (*cmdrest++ != '"')
2215     error (_("No format string following the location"));
2216
2217   format_start = cmdrest;
2218
2219   fpieces = parse_format_string (&cmdrest);
2220
2221   old_cleanups = make_cleanup (free_format_pieces_cleanup, &fpieces);
2222
2223   format_end = cmdrest;
2224
2225   if (*cmdrest++ != '"')
2226     error (_("Bad format string, non-terminated '\"'."));
2227   
2228   cmdrest = skip_spaces_const (cmdrest);
2229
2230   if (!(*cmdrest == ',' || *cmdrest == '\0'))
2231     error (_("Invalid argument syntax"));
2232
2233   if (*cmdrest == ',')
2234     cmdrest++;
2235   cmdrest = skip_spaces_const (cmdrest);
2236
2237   /* For each argument, make an expression.  */
2238
2239   argvec = (struct expression **) alloca (strlen (cmd)
2240                                          * sizeof (struct expression *));
2241
2242   nargs = 0;
2243   while (*cmdrest != '\0')
2244     {
2245       const char *cmd1;
2246
2247       cmd1 = cmdrest;
2248       expr = parse_exp_1 (&cmd1, scope, block_for_pc (scope), 1);
2249       argvec[nargs++] = expr;
2250       cmdrest = cmd1;
2251       if (*cmdrest == ',')
2252         ++cmdrest;
2253     }
2254
2255   /* We don't want to stop processing, so catch any errors
2256      that may show up.  */
2257   TRY_CATCH (ex, RETURN_MASK_ERROR)
2258     {
2259       aexpr = gen_printf (scope, gdbarch, 0, 0,
2260                           format_start, format_end - format_start,
2261                           fpieces, nargs, argvec);
2262     }
2263
2264   do_cleanups (old_cleanups);
2265
2266   if (ex.reason < 0)
2267     {
2268       /* If we got here, it means the command could not be parsed to a valid
2269          bytecode expression and thus can't be evaluated on the target's side.
2270          It's no use iterating through the other commands.  */
2271       return NULL;
2272     }
2273
2274   /* We have a valid agent expression, return it.  */
2275   return aexpr;
2276 }
2277
2278 /* Based on location BL, create a list of breakpoint commands to be
2279    passed on to the target.  If we have duplicated locations with
2280    different commands, we will add any such to the list.  */
2281
2282 static void
2283 build_target_command_list (struct bp_location *bl)
2284 {
2285   struct bp_location **locp = NULL, **loc2p;
2286   int null_command_or_parse_error = 0;
2287   int modified = bl->needs_update;
2288   struct bp_location *loc;
2289
2290   /* For now, limit to agent-style dprintf breakpoints.  */
2291   if (bl->owner->type != bp_dprintf
2292       || strcmp (dprintf_style, dprintf_style_agent) != 0)
2293     return;
2294
2295   if (!target_can_run_breakpoint_commands ())
2296     return;
2297
2298   /* Do a first pass to check for locations with no assigned
2299      conditions or conditions that fail to parse to a valid agent expression
2300      bytecode.  If any of these happen, then it's no use to send conditions
2301      to the target since this location will always trigger and generate a
2302      response back to GDB.  */
2303   ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2304     {
2305       loc = (*loc2p);
2306       if (is_breakpoint (loc->owner) && loc->pspace->num == bl->pspace->num)
2307         {
2308           if (modified)
2309             {
2310               struct agent_expr *aexpr;
2311
2312               /* Re-parse the commands since something changed.  In that
2313                  case we already freed the command bytecodes (see
2314                  force_breakpoint_reinsertion).  We just
2315                  need to parse the command to bytecodes again.  */
2316               aexpr = parse_cmd_to_aexpr (bl->address,
2317                                           loc->owner->extra_string);
2318               loc->cmd_bytecode = aexpr;
2319
2320               if (!aexpr)
2321                 continue;
2322             }
2323
2324           /* If we have a NULL bytecode expression, it means something
2325              went wrong or we have a null command expression.  */
2326           if (!loc->cmd_bytecode)
2327             {
2328               null_command_or_parse_error = 1;
2329               break;
2330             }
2331         }
2332     }
2333
2334   /* If anything failed, then we're not doing target-side commands,
2335      and so clean up.  */
2336   if (null_command_or_parse_error)
2337     {
2338       ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2339         {
2340           loc = (*loc2p);
2341           if (is_breakpoint (loc->owner)
2342               && loc->pspace->num == bl->pspace->num)
2343             {
2344               /* Only go as far as the first NULL bytecode is
2345                  located.  */
2346               if (loc->cmd_bytecode == NULL)
2347                 return;
2348
2349               free_agent_expr (loc->cmd_bytecode);
2350               loc->cmd_bytecode = NULL;
2351             }
2352         }
2353     }
2354
2355   /* No NULL commands or failed bytecode generation.  Build a command list
2356      for this location's address.  */
2357   ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2358     {
2359       loc = (*loc2p);
2360       if (loc->owner->extra_string
2361           && is_breakpoint (loc->owner)
2362           && loc->pspace->num == bl->pspace->num
2363           && loc->owner->enable_state == bp_enabled
2364           && loc->enabled)
2365         /* Add the command to the vector.  This will be used later
2366            to send the commands to the target.  */
2367         VEC_safe_push (agent_expr_p, bl->target_info.tcommands,
2368                        loc->cmd_bytecode);
2369     }
2370
2371   bl->target_info.persist = 0;
2372   /* Maybe flag this location as persistent.  */
2373   if (bl->owner->type == bp_dprintf && disconnected_dprintf)
2374     bl->target_info.persist = 1;
2375 }
2376
2377 /* Insert a low-level "breakpoint" of some type.  BL is the breakpoint
2378    location.  Any error messages are printed to TMP_ERROR_STREAM; and
2379    DISABLED_BREAKS, and HW_BREAKPOINT_ERROR are used to report problems.
2380    Returns 0 for success, 1 if the bp_location type is not supported or
2381    -1 for failure.
2382
2383    NOTE drow/2003-09-09: This routine could be broken down to an
2384    object-style method for each breakpoint or catchpoint type.  */
2385 static int
2386 insert_bp_location (struct bp_location *bl,
2387                     struct ui_file *tmp_error_stream,
2388                     int *disabled_breaks,
2389                     int *hw_breakpoint_error,
2390                     int *hw_bp_error_explained_already)
2391 {
2392   int val = 0;
2393   char *hw_bp_err_string = NULL;
2394   struct gdb_exception e;
2395
2396   if (!should_be_inserted (bl) || (bl->inserted && !bl->needs_update))
2397     return 0;
2398
2399   /* Note we don't initialize bl->target_info, as that wipes out
2400      the breakpoint location's shadow_contents if the breakpoint
2401      is still inserted at that location.  This in turn breaks
2402      target_read_memory which depends on these buffers when
2403      a memory read is requested at the breakpoint location:
2404      Once the target_info has been wiped, we fail to see that
2405      we have a breakpoint inserted at that address and thus
2406      read the breakpoint instead of returning the data saved in
2407      the breakpoint location's shadow contents.  */
2408   bl->target_info.placed_address = bl->address;
2409   bl->target_info.placed_address_space = bl->pspace->aspace;
2410   bl->target_info.length = bl->length;
2411
2412   /* When working with target-side conditions, we must pass all the conditions
2413      for the same breakpoint address down to the target since GDB will not
2414      insert those locations.  With a list of breakpoint conditions, the target
2415      can decide when to stop and notify GDB.  */
2416
2417   if (is_breakpoint (bl->owner))
2418     {
2419       build_target_condition_list (bl);
2420       build_target_command_list (bl);
2421       /* Reset the modification marker.  */
2422       bl->needs_update = 0;
2423     }
2424
2425   if (bl->loc_type == bp_loc_software_breakpoint
2426       || bl->loc_type == bp_loc_hardware_breakpoint)
2427     {
2428       if (bl->owner->type != bp_hardware_breakpoint)
2429         {
2430           /* If the explicitly specified breakpoint type
2431              is not hardware breakpoint, check the memory map to see
2432              if the breakpoint address is in read only memory or not.
2433
2434              Two important cases are:
2435              - location type is not hardware breakpoint, memory
2436              is readonly.  We change the type of the location to
2437              hardware breakpoint.
2438              - location type is hardware breakpoint, memory is
2439              read-write.  This means we've previously made the
2440              location hardware one, but then the memory map changed,
2441              so we undo.
2442              
2443              When breakpoints are removed, remove_breakpoints will use
2444              location types we've just set here, the only possible
2445              problem is that memory map has changed during running
2446              program, but it's not going to work anyway with current
2447              gdb.  */
2448           struct mem_region *mr 
2449             = lookup_mem_region (bl->target_info.placed_address);
2450           
2451           if (mr)
2452             {
2453               if (automatic_hardware_breakpoints)
2454                 {
2455                   enum bp_loc_type new_type;
2456                   
2457                   if (mr->attrib.mode != MEM_RW)
2458                     new_type = bp_loc_hardware_breakpoint;
2459                   else 
2460                     new_type = bp_loc_software_breakpoint;
2461                   
2462                   if (new_type != bl->loc_type)
2463                     {
2464                       static int said = 0;
2465
2466                       bl->loc_type = new_type;
2467                       if (!said)
2468                         {
2469                           fprintf_filtered (gdb_stdout,
2470                                             _("Note: automatically using "
2471                                               "hardware breakpoints for "
2472                                               "read-only addresses.\n"));
2473                           said = 1;
2474                         }
2475                     }
2476                 }
2477               else if (bl->loc_type == bp_loc_software_breakpoint
2478                        && mr->attrib.mode != MEM_RW)        
2479                 warning (_("cannot set software breakpoint "
2480                            "at readonly address %s"),
2481                          paddress (bl->gdbarch, bl->address));
2482             }
2483         }
2484         
2485       /* First check to see if we have to handle an overlay.  */
2486       if (overlay_debugging == ovly_off
2487           || bl->section == NULL
2488           || !(section_is_overlay (bl->section)))
2489         {
2490           /* No overlay handling: just set the breakpoint.  */
2491           TRY_CATCH (e, RETURN_MASK_ALL)
2492             {
2493               val = bl->owner->ops->insert_location (bl);
2494             }
2495           if (e.reason < 0)
2496             {
2497               val = 1;
2498               hw_bp_err_string = (char *) e.message;
2499             }
2500         }
2501       else
2502         {
2503           /* This breakpoint is in an overlay section.
2504              Shall we set a breakpoint at the LMA?  */
2505           if (!overlay_events_enabled)
2506             {
2507               /* Yes -- overlay event support is not active, 
2508                  so we must try to set a breakpoint at the LMA.
2509                  This will not work for a hardware breakpoint.  */
2510               if (bl->loc_type == bp_loc_hardware_breakpoint)
2511                 warning (_("hardware breakpoint %d not supported in overlay!"),
2512                          bl->owner->number);
2513               else
2514                 {
2515                   CORE_ADDR addr = overlay_unmapped_address (bl->address,
2516                                                              bl->section);
2517                   /* Set a software (trap) breakpoint at the LMA.  */
2518                   bl->overlay_target_info = bl->target_info;
2519                   bl->overlay_target_info.placed_address = addr;
2520                   val = target_insert_breakpoint (bl->gdbarch,
2521                                                   &bl->overlay_target_info);
2522                   if (val != 0)
2523                     fprintf_unfiltered (tmp_error_stream,
2524                                         "Overlay breakpoint %d "
2525                                         "failed: in ROM?\n",
2526                                         bl->owner->number);
2527                 }
2528             }
2529           /* Shall we set a breakpoint at the VMA? */
2530           if (section_is_mapped (bl->section))
2531             {
2532               /* Yes.  This overlay section is mapped into memory.  */
2533               TRY_CATCH (e, RETURN_MASK_ALL)
2534                 {
2535                   val = bl->owner->ops->insert_location (bl);
2536                 }
2537               if (e.reason < 0)
2538                 {
2539                   val = 1;
2540                   hw_bp_err_string = (char *) e.message;
2541                 }
2542             }
2543           else
2544             {
2545               /* No.  This breakpoint will not be inserted.  
2546                  No error, but do not mark the bp as 'inserted'.  */
2547               return 0;
2548             }
2549         }
2550
2551       if (val)
2552         {
2553           /* Can't set the breakpoint.  */
2554           if (solib_name_from_address (bl->pspace, bl->address))
2555             {
2556               /* See also: disable_breakpoints_in_shlibs.  */
2557               val = 0;
2558               bl->shlib_disabled = 1;
2559               observer_notify_breakpoint_modified (bl->owner);
2560               if (!*disabled_breaks)
2561                 {
2562                   fprintf_unfiltered (tmp_error_stream, 
2563                                       "Cannot insert breakpoint %d.\n", 
2564                                       bl->owner->number);
2565                   fprintf_unfiltered (tmp_error_stream, 
2566                                       "Temporarily disabling shared "
2567                                       "library breakpoints:\n");
2568                 }
2569               *disabled_breaks = 1;
2570               fprintf_unfiltered (tmp_error_stream,
2571                                   "breakpoint #%d\n", bl->owner->number);
2572             }
2573           else
2574             {
2575               if (bl->loc_type == bp_loc_hardware_breakpoint)
2576                 {
2577                   *hw_breakpoint_error = 1;
2578                   *hw_bp_error_explained_already = hw_bp_err_string != NULL;
2579                   fprintf_unfiltered (tmp_error_stream,
2580                                       "Cannot insert hardware breakpoint %d%s",
2581                                       bl->owner->number, hw_bp_err_string ? ":" : ".\n");
2582                   if (hw_bp_err_string)
2583                     fprintf_unfiltered (tmp_error_stream, "%s.\n", hw_bp_err_string);
2584                 }
2585               else
2586                 {
2587                   char *message = memory_error_message (TARGET_XFER_E_IO,
2588                                                         bl->gdbarch, bl->address);
2589                   struct cleanup *old_chain = make_cleanup (xfree, message);
2590
2591                   fprintf_unfiltered (tmp_error_stream, 
2592                                       "Cannot insert breakpoint %d.\n"
2593                                       "%s\n",
2594                                       bl->owner->number, message);
2595
2596                   do_cleanups (old_chain);
2597                 }
2598
2599             }
2600         }
2601       else
2602         bl->inserted = 1;
2603
2604       return val;
2605     }
2606
2607   else if (bl->loc_type == bp_loc_hardware_watchpoint
2608            /* NOTE drow/2003-09-08: This state only exists for removing
2609               watchpoints.  It's not clear that it's necessary...  */
2610            && bl->owner->disposition != disp_del_at_next_stop)
2611     {
2612       gdb_assert (bl->owner->ops != NULL
2613                   && bl->owner->ops->insert_location != NULL);
2614
2615       val = bl->owner->ops->insert_location (bl);
2616
2617       /* If trying to set a read-watchpoint, and it turns out it's not
2618          supported, try emulating one with an access watchpoint.  */
2619       if (val == 1 && bl->watchpoint_type == hw_read)
2620         {
2621           struct bp_location *loc, **loc_temp;
2622
2623           /* But don't try to insert it, if there's already another
2624              hw_access location that would be considered a duplicate
2625              of this one.  */
2626           ALL_BP_LOCATIONS (loc, loc_temp)
2627             if (loc != bl
2628                 && loc->watchpoint_type == hw_access
2629                 && watchpoint_locations_match (bl, loc))
2630               {
2631                 bl->duplicate = 1;
2632                 bl->inserted = 1;
2633                 bl->target_info = loc->target_info;
2634                 bl->watchpoint_type = hw_access;
2635                 val = 0;
2636                 break;
2637               }
2638
2639           if (val == 1)
2640             {
2641               bl->watchpoint_type = hw_access;
2642               val = bl->owner->ops->insert_location (bl);
2643
2644               if (val)
2645                 /* Back to the original value.  */
2646                 bl->watchpoint_type = hw_read;
2647             }
2648         }
2649
2650       bl->inserted = (val == 0);
2651     }
2652
2653   else if (bl->owner->type == bp_catchpoint)
2654     {
2655       gdb_assert (bl->owner->ops != NULL
2656                   && bl->owner->ops->insert_location != NULL);
2657
2658       val = bl->owner->ops->insert_location (bl);
2659       if (val)
2660         {
2661           bl->owner->enable_state = bp_disabled;
2662
2663           if (val == 1)
2664             warning (_("\
2665 Error inserting catchpoint %d: Your system does not support this type\n\
2666 of catchpoint."), bl->owner->number);
2667           else
2668             warning (_("Error inserting catchpoint %d."), bl->owner->number);
2669         }
2670
2671       bl->inserted = (val == 0);
2672
2673       /* We've already printed an error message if there was a problem
2674          inserting this catchpoint, and we've disabled the catchpoint,
2675          so just return success.  */
2676       return 0;
2677     }
2678
2679   return 0;
2680 }
2681
2682 /* This function is called when program space PSPACE is about to be
2683    deleted.  It takes care of updating breakpoints to not reference
2684    PSPACE anymore.  */
2685
2686 void
2687 breakpoint_program_space_exit (struct program_space *pspace)
2688 {
2689   struct breakpoint *b, *b_temp;
2690   struct bp_location *loc, **loc_temp;
2691
2692   /* Remove any breakpoint that was set through this program space.  */
2693   ALL_BREAKPOINTS_SAFE (b, b_temp)
2694     {
2695       if (b->pspace == pspace)
2696         delete_breakpoint (b);
2697     }
2698
2699   /* Breakpoints set through other program spaces could have locations
2700      bound to PSPACE as well.  Remove those.  */
2701   ALL_BP_LOCATIONS (loc, loc_temp)
2702     {
2703       struct bp_location *tmp;
2704
2705       if (loc->pspace == pspace)
2706         {
2707           /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL.  */
2708           if (loc->owner->loc == loc)
2709             loc->owner->loc = loc->next;
2710           else
2711             for (tmp = loc->owner->loc; tmp->next != NULL; tmp = tmp->next)
2712               if (tmp->next == loc)
2713                 {
2714                   tmp->next = loc->next;
2715                   break;
2716                 }
2717         }
2718     }
2719
2720   /* Now update the global location list to permanently delete the
2721      removed locations above.  */
2722   update_global_location_list (0);
2723 }
2724
2725 /* Make sure all breakpoints are inserted in inferior.
2726    Throws exception on any error.
2727    A breakpoint that is already inserted won't be inserted
2728    again, so calling this function twice is safe.  */
2729 void
2730 insert_breakpoints (void)
2731 {
2732   struct breakpoint *bpt;
2733
2734   ALL_BREAKPOINTS (bpt)
2735     if (is_hardware_watchpoint (bpt))
2736       {
2737         struct watchpoint *w = (struct watchpoint *) bpt;
2738
2739         update_watchpoint (w, 0 /* don't reparse.  */);
2740       }
2741
2742   update_global_location_list (1);
2743
2744   /* update_global_location_list does not insert breakpoints when
2745      always_inserted_mode is not enabled.  Explicitly insert them
2746      now.  */
2747   if (!breakpoints_always_inserted_mode ())
2748     insert_breakpoint_locations ();
2749 }
2750
2751 /* Invoke CALLBACK for each of bp_location.  */
2752
2753 void
2754 iterate_over_bp_locations (walk_bp_location_callback callback)
2755 {
2756   struct bp_location *loc, **loc_tmp;
2757
2758   ALL_BP_LOCATIONS (loc, loc_tmp)
2759     {
2760       callback (loc, NULL);
2761     }
2762 }
2763
2764 /* This is used when we need to synch breakpoint conditions between GDB and the
2765    target.  It is the case with deleting and disabling of breakpoints when using
2766    always-inserted mode.  */
2767
2768 static void
2769 update_inserted_breakpoint_locations (void)
2770 {
2771   struct bp_location *bl, **blp_tmp;
2772   int error_flag = 0;
2773   int val = 0;
2774   int disabled_breaks = 0;
2775   int hw_breakpoint_error = 0;
2776   int hw_bp_details_reported = 0;
2777
2778   struct ui_file *tmp_error_stream = mem_fileopen ();
2779   struct cleanup *cleanups = make_cleanup_ui_file_delete (tmp_error_stream);
2780
2781   /* Explicitly mark the warning -- this will only be printed if
2782      there was an error.  */
2783   fprintf_unfiltered (tmp_error_stream, "Warning:\n");
2784
2785   save_current_space_and_thread ();
2786
2787   ALL_BP_LOCATIONS (bl, blp_tmp)
2788     {
2789       /* We only want to update software breakpoints and hardware
2790          breakpoints.  */
2791       if (!is_breakpoint (bl->owner))
2792         continue;
2793
2794       /* We only want to update locations that are already inserted
2795          and need updating.  This is to avoid unwanted insertion during
2796          deletion of breakpoints.  */
2797       if (!bl->inserted || (bl->inserted && !bl->needs_update))
2798         continue;
2799
2800       switch_to_program_space_and_thread (bl->pspace);
2801
2802       /* For targets that support global breakpoints, there's no need
2803          to select an inferior to insert breakpoint to.  In fact, even
2804          if we aren't attached to any process yet, we should still
2805          insert breakpoints.  */
2806       if (!gdbarch_has_global_breakpoints (target_gdbarch ())
2807           && ptid_equal (inferior_ptid, null_ptid))
2808         continue;
2809
2810       val = insert_bp_location (bl, tmp_error_stream, &disabled_breaks,
2811                                     &hw_breakpoint_error, &hw_bp_details_reported);
2812       if (val)
2813         error_flag = val;
2814     }
2815
2816   if (error_flag)
2817     {
2818       target_terminal_ours_for_output ();
2819       error_stream (tmp_error_stream);
2820     }
2821
2822   do_cleanups (cleanups);
2823 }
2824
2825 /* Used when starting or continuing the program.  */
2826
2827 static void
2828 insert_breakpoint_locations (void)
2829 {
2830   struct breakpoint *bpt;
2831   struct bp_location *bl, **blp_tmp;
2832   int error_flag = 0;
2833   int val = 0;
2834   int disabled_breaks = 0;
2835   int hw_breakpoint_error = 0;
2836   int hw_bp_error_explained_already = 0;
2837
2838   struct ui_file *tmp_error_stream = mem_fileopen ();
2839   struct cleanup *cleanups = make_cleanup_ui_file_delete (tmp_error_stream);
2840   
2841   /* Explicitly mark the warning -- this will only be printed if
2842      there was an error.  */
2843   fprintf_unfiltered (tmp_error_stream, "Warning:\n");
2844
2845   save_current_space_and_thread ();
2846
2847   ALL_BP_LOCATIONS (bl, blp_tmp)
2848     {
2849       if (!should_be_inserted (bl) || (bl->inserted && !bl->needs_update))
2850         continue;
2851
2852       /* There is no point inserting thread-specific breakpoints if
2853          the thread no longer exists.  ALL_BP_LOCATIONS bp_location
2854          has BL->OWNER always non-NULL.  */
2855       if (bl->owner->thread != -1
2856           && !valid_thread_id (bl->owner->thread))
2857         continue;
2858
2859       switch_to_program_space_and_thread (bl->pspace);
2860
2861       /* For targets that support global breakpoints, there's no need
2862          to select an inferior to insert breakpoint to.  In fact, even
2863          if we aren't attached to any process yet, we should still
2864          insert breakpoints.  */
2865       if (!gdbarch_has_global_breakpoints (target_gdbarch ())
2866           && ptid_equal (inferior_ptid, null_ptid))
2867         continue;
2868
2869       val = insert_bp_location (bl, tmp_error_stream, &disabled_breaks,
2870                                     &hw_breakpoint_error, &hw_bp_error_explained_already);
2871       if (val)
2872         error_flag = val;
2873     }
2874
2875   /* If we failed to insert all locations of a watchpoint, remove
2876      them, as half-inserted watchpoint is of limited use.  */
2877   ALL_BREAKPOINTS (bpt)  
2878     {
2879       int some_failed = 0;
2880       struct bp_location *loc;
2881
2882       if (!is_hardware_watchpoint (bpt))
2883         continue;
2884
2885       if (!breakpoint_enabled (bpt))
2886         continue;
2887
2888       if (bpt->disposition == disp_del_at_next_stop)
2889         continue;
2890       
2891       for (loc = bpt->loc; loc; loc = loc->next)
2892         if (!loc->inserted && should_be_inserted (loc))
2893           {
2894             some_failed = 1;
2895             break;
2896           }
2897       if (some_failed)
2898         {
2899           for (loc = bpt->loc; loc; loc = loc->next)
2900             if (loc->inserted)
2901               remove_breakpoint (loc, mark_uninserted);
2902
2903           hw_breakpoint_error = 1;
2904           fprintf_unfiltered (tmp_error_stream,
2905                               "Could not insert hardware watchpoint %d.\n", 
2906                               bpt->number);
2907           error_flag = -1;
2908         }
2909     }
2910
2911   if (error_flag)
2912     {
2913       /* If a hardware breakpoint or watchpoint was inserted, add a
2914          message about possibly exhausted resources.  */
2915       if (hw_breakpoint_error && !hw_bp_error_explained_already)
2916         {
2917           fprintf_unfiltered (tmp_error_stream, 
2918                               "Could not insert hardware breakpoints:\n\
2919 You may have requested too many hardware breakpoints/watchpoints.\n");
2920         }
2921       target_terminal_ours_for_output ();
2922       error_stream (tmp_error_stream);
2923     }
2924
2925   do_cleanups (cleanups);
2926 }
2927
2928 /* Used when the program stops.
2929    Returns zero if successful, or non-zero if there was a problem
2930    removing a breakpoint location.  */
2931
2932 int
2933 remove_breakpoints (void)
2934 {
2935   struct bp_location *bl, **blp_tmp;
2936   int val = 0;
2937
2938   ALL_BP_LOCATIONS (bl, blp_tmp)
2939   {
2940     if (bl->inserted && !is_tracepoint (bl->owner))
2941       val |= remove_breakpoint (bl, mark_uninserted);
2942   }
2943   return val;
2944 }
2945
2946 /* When a thread exits, remove breakpoints that are related to
2947    that thread.  */
2948
2949 static void
2950 remove_threaded_breakpoints (struct thread_info *tp, int silent)
2951 {
2952   struct breakpoint *b, *b_tmp;
2953
2954   ALL_BREAKPOINTS_SAFE (b, b_tmp)
2955     {
2956       if (b->thread == tp->num && user_breakpoint_p (b))
2957         {
2958           b->disposition = disp_del_at_next_stop;
2959
2960           printf_filtered (_("\
2961 Thread-specific breakpoint %d deleted - thread %d no longer in the thread list.\n"),
2962                           b->number, tp->num);
2963
2964           /* Hide it from the user.  */
2965           b->number = 0;
2966        }
2967     }
2968 }
2969
2970 /* Remove breakpoints of process PID.  */
2971
2972 int
2973 remove_breakpoints_pid (int pid)
2974 {
2975   struct bp_location *bl, **blp_tmp;
2976   int val;
2977   struct inferior *inf = find_inferior_pid (pid);
2978
2979   ALL_BP_LOCATIONS (bl, blp_tmp)
2980   {
2981     if (bl->pspace != inf->pspace)
2982       continue;
2983
2984     if (bl->owner->type == bp_dprintf)
2985       continue;
2986
2987     if (bl->inserted)
2988       {
2989         val = remove_breakpoint (bl, mark_uninserted);
2990         if (val != 0)
2991           return val;
2992       }
2993   }
2994   return 0;
2995 }
2996
2997 int
2998 reattach_breakpoints (int pid)
2999 {
3000   struct cleanup *old_chain;
3001   struct bp_location *bl, **blp_tmp;
3002   int val;
3003   struct ui_file *tmp_error_stream;
3004   int dummy1 = 0, dummy2 = 0, dummy3 = 0;
3005   struct inferior *inf;
3006   struct thread_info *tp;
3007
3008   tp = any_live_thread_of_process (pid);
3009   if (tp == NULL)
3010     return 1;
3011
3012   inf = find_inferior_pid (pid);
3013   old_chain = save_inferior_ptid ();
3014
3015   inferior_ptid = tp->ptid;
3016
3017   tmp_error_stream = mem_fileopen ();
3018   make_cleanup_ui_file_delete (tmp_error_stream);
3019
3020   ALL_BP_LOCATIONS (bl, blp_tmp)
3021   {
3022     if (bl->pspace != inf->pspace)
3023       continue;
3024
3025     if (bl->inserted)
3026       {
3027         bl->inserted = 0;
3028         val = insert_bp_location (bl, tmp_error_stream, &dummy1, &dummy2, &dummy3);
3029         if (val != 0)
3030           {
3031             do_cleanups (old_chain);
3032             return val;
3033           }
3034       }
3035   }
3036   do_cleanups (old_chain);
3037   return 0;
3038 }
3039
3040 static int internal_breakpoint_number = -1;
3041
3042 /* Set the breakpoint number of B, depending on the value of INTERNAL.
3043    If INTERNAL is non-zero, the breakpoint number will be populated
3044    from internal_breakpoint_number and that variable decremented.
3045    Otherwise the breakpoint number will be populated from
3046    breakpoint_count and that value incremented.  Internal breakpoints
3047    do not set the internal var bpnum.  */
3048 static void
3049 set_breakpoint_number (int internal, struct breakpoint *b)
3050 {
3051   if (internal)
3052     b->number = internal_breakpoint_number--;
3053   else
3054     {
3055       set_breakpoint_count (breakpoint_count + 1);
3056       b->number = breakpoint_count;
3057     }
3058 }
3059
3060 static struct breakpoint *
3061 create_internal_breakpoint (struct gdbarch *gdbarch,
3062                             CORE_ADDR address, enum bptype type,
3063                             const struct breakpoint_ops *ops)
3064 {
3065   struct symtab_and_line sal;
3066   struct breakpoint *b;
3067
3068   init_sal (&sal);              /* Initialize to zeroes.  */
3069
3070   sal.pc = address;
3071   sal.section = find_pc_overlay (sal.pc);
3072   sal.pspace = current_program_space;
3073
3074   b = set_raw_breakpoint (gdbarch, sal, type, ops);
3075   b->number = internal_breakpoint_number--;
3076   b->disposition = disp_donttouch;
3077
3078   return b;
3079 }
3080
3081 static const char *const longjmp_names[] =
3082   {
3083     "longjmp", "_longjmp", "siglongjmp", "_siglongjmp"
3084   };
3085 #define NUM_LONGJMP_NAMES ARRAY_SIZE(longjmp_names)
3086
3087 /* Per-objfile data private to breakpoint.c.  */
3088 struct breakpoint_objfile_data
3089 {
3090   /* Minimal symbol for "_ovly_debug_event" (if any).  */
3091   struct minimal_symbol *overlay_msym;
3092
3093   /* Minimal symbol(s) for "longjmp", "siglongjmp", etc. (if any).  */
3094   struct minimal_symbol *longjmp_msym[NUM_LONGJMP_NAMES];
3095
3096   /* True if we have looked for longjmp probes.  */
3097   int longjmp_searched;
3098
3099   /* SystemTap probe points for longjmp (if any).  */
3100   VEC (probe_p) *longjmp_probes;
3101
3102   /* Minimal symbol for "std::terminate()" (if any).  */
3103   struct minimal_symbol *terminate_msym;
3104
3105   /* Minimal symbol for "_Unwind_DebugHook" (if any).  */
3106   struct minimal_symbol *exception_msym;
3107
3108   /* True if we have looked for exception probes.  */
3109   int exception_searched;
3110
3111   /* SystemTap probe points for unwinding (if any).  */
3112   VEC (probe_p) *exception_probes;
3113 };
3114
3115 static const struct objfile_data *breakpoint_objfile_key;
3116
3117 /* Minimal symbol not found sentinel.  */
3118 static struct minimal_symbol msym_not_found;
3119
3120 /* Returns TRUE if MSYM point to the "not found" sentinel.  */
3121
3122 static int
3123 msym_not_found_p (const struct minimal_symbol *msym)
3124 {
3125   return msym == &msym_not_found;
3126 }
3127
3128 /* Return per-objfile data needed by breakpoint.c.
3129    Allocate the data if necessary.  */
3130
3131 static struct breakpoint_objfile_data *
3132 get_breakpoint_objfile_data (struct objfile *objfile)
3133 {
3134   struct breakpoint_objfile_data *bp_objfile_data;
3135
3136   bp_objfile_data = objfile_data (objfile, breakpoint_objfile_key);
3137   if (bp_objfile_data == NULL)
3138     {
3139       bp_objfile_data = obstack_alloc (&objfile->objfile_obstack,
3140                                        sizeof (*bp_objfile_data));
3141
3142       memset (bp_objfile_data, 0, sizeof (*bp_objfile_data));
3143       set_objfile_data (objfile, breakpoint_objfile_key, bp_objfile_data);
3144     }
3145   return bp_objfile_data;
3146 }
3147
3148 static void
3149 free_breakpoint_probes (struct objfile *obj, void *data)
3150 {
3151   struct breakpoint_objfile_data *bp_objfile_data = data;
3152
3153   VEC_free (probe_p, bp_objfile_data->longjmp_probes);
3154   VEC_free (probe_p, bp_objfile_data->exception_probes);
3155 }
3156
3157 static void
3158 create_overlay_event_breakpoint (void)
3159 {
3160   struct objfile *objfile;
3161   const char *const func_name = "_ovly_debug_event";
3162
3163   ALL_OBJFILES (objfile)
3164     {
3165       struct breakpoint *b;
3166       struct breakpoint_objfile_data *bp_objfile_data;
3167       CORE_ADDR addr;
3168
3169       bp_objfile_data = get_breakpoint_objfile_data (objfile);
3170
3171       if (msym_not_found_p (bp_objfile_data->overlay_msym))
3172         continue;
3173
3174       if (bp_objfile_data->overlay_msym == NULL)
3175         {
3176           struct minimal_symbol *m;
3177
3178           m = lookup_minimal_symbol_text (func_name, objfile);
3179           if (m == NULL)
3180             {
3181               /* Avoid future lookups in this objfile.  */
3182               bp_objfile_data->overlay_msym = &msym_not_found;
3183               continue;
3184             }
3185           bp_objfile_data->overlay_msym = m;
3186         }
3187
3188       addr = SYMBOL_VALUE_ADDRESS (bp_objfile_data->overlay_msym);
3189       b = create_internal_breakpoint (get_objfile_arch (objfile), addr,
3190                                       bp_overlay_event,
3191                                       &internal_breakpoint_ops);
3192       b->addr_string = xstrdup (func_name);
3193
3194       if (overlay_debugging == ovly_auto)
3195         {
3196           b->enable_state = bp_enabled;
3197           overlay_events_enabled = 1;
3198         }
3199       else
3200        {
3201          b->enable_state = bp_disabled;
3202          overlay_events_enabled = 0;
3203        }
3204     }
3205   update_global_location_list (1);
3206 }
3207
3208 static void
3209 create_longjmp_master_breakpoint (void)
3210 {
3211   struct program_space *pspace;
3212   struct cleanup *old_chain;
3213
3214   old_chain = save_current_program_space ();
3215
3216   ALL_PSPACES (pspace)
3217   {
3218     struct objfile *objfile;
3219
3220     set_current_program_space (pspace);
3221
3222     ALL_OBJFILES (objfile)
3223     {
3224       int i;
3225       struct gdbarch *gdbarch;
3226       struct breakpoint_objfile_data *bp_objfile_data;
3227
3228       gdbarch = get_objfile_arch (objfile);
3229
3230       bp_objfile_data = get_breakpoint_objfile_data (objfile);
3231
3232       if (!bp_objfile_data->longjmp_searched)
3233         {
3234           VEC (probe_p) *ret;
3235
3236           ret = find_probes_in_objfile (objfile, "libc", "longjmp");
3237           if (ret != NULL)
3238             {
3239               /* We are only interested in checking one element.  */
3240               struct probe *p = VEC_index (probe_p, ret, 0);
3241
3242               if (!can_evaluate_probe_arguments (p))
3243                 {
3244                   /* We cannot use the probe interface here, because it does
3245                      not know how to evaluate arguments.  */
3246                   VEC_free (probe_p, ret);
3247                   ret = NULL;
3248                 }
3249             }
3250           bp_objfile_data->longjmp_probes = ret;
3251           bp_objfile_data->longjmp_searched = 1;
3252         }
3253
3254       if (bp_objfile_data->longjmp_probes != NULL)
3255         {
3256           int i;
3257           struct probe *probe;
3258           struct gdbarch *gdbarch = get_objfile_arch (objfile);
3259
3260           for (i = 0;
3261                VEC_iterate (probe_p,
3262                             bp_objfile_data->longjmp_probes,
3263                             i, probe);
3264                ++i)
3265             {
3266               struct breakpoint *b;
3267
3268               b = create_internal_breakpoint (gdbarch, probe->address,
3269                                               bp_longjmp_master,
3270                                               &internal_breakpoint_ops);
3271               b->addr_string = xstrdup ("-probe-stap libc:longjmp");
3272               b->enable_state = bp_disabled;
3273             }
3274
3275           continue;
3276         }
3277
3278       if (!gdbarch_get_longjmp_target_p (gdbarch))
3279         continue;
3280
3281       for (i = 0; i < NUM_LONGJMP_NAMES; i++)
3282         {
3283           struct breakpoint *b;
3284           const char *func_name;
3285           CORE_ADDR addr;
3286
3287           if (msym_not_found_p (bp_objfile_data->longjmp_msym[i]))
3288             continue;
3289
3290           func_name = longjmp_names[i];
3291           if (bp_objfile_data->longjmp_msym[i] == NULL)
3292             {
3293               struct minimal_symbol *m;
3294
3295               m = lookup_minimal_symbol_text (func_name, objfile);
3296               if (m == NULL)
3297                 {
3298                   /* Prevent future lookups in this objfile.  */
3299                   bp_objfile_data->longjmp_msym[i] = &msym_not_found;
3300                   continue;
3301                 }
3302               bp_objfile_data->longjmp_msym[i] = m;
3303             }
3304
3305           addr = SYMBOL_VALUE_ADDRESS (bp_objfile_data->longjmp_msym[i]);
3306           b = create_internal_breakpoint (gdbarch, addr, bp_longjmp_master,
3307                                           &internal_breakpoint_ops);
3308           b->addr_string = xstrdup (func_name);
3309           b->enable_state = bp_disabled;
3310         }
3311     }
3312   }
3313   update_global_location_list (1);
3314
3315   do_cleanups (old_chain);
3316 }
3317
3318 /* Create a master std::terminate breakpoint.  */
3319 static void
3320 create_std_terminate_master_breakpoint (void)
3321 {
3322   struct program_space *pspace;
3323   struct cleanup *old_chain;
3324   const char *const func_name = "std::terminate()";
3325
3326   old_chain = save_current_program_space ();
3327
3328   ALL_PSPACES (pspace)
3329   {
3330     struct objfile *objfile;
3331     CORE_ADDR addr;
3332
3333     set_current_program_space (pspace);
3334
3335     ALL_OBJFILES (objfile)
3336     {
3337       struct breakpoint *b;
3338       struct breakpoint_objfile_data *bp_objfile_data;
3339
3340       bp_objfile_data = get_breakpoint_objfile_data (objfile);
3341
3342       if (msym_not_found_p (bp_objfile_data->terminate_msym))
3343         continue;
3344
3345       if (bp_objfile_data->terminate_msym == NULL)
3346         {
3347           struct minimal_symbol *m;
3348
3349           m = lookup_minimal_symbol (func_name, NULL, objfile);
3350           if (m == NULL || (MSYMBOL_TYPE (m) != mst_text
3351                             && MSYMBOL_TYPE (m) != mst_file_text))
3352             {
3353               /* Prevent future lookups in this objfile.  */
3354               bp_objfile_data->terminate_msym = &msym_not_found;
3355               continue;
3356             }
3357           bp_objfile_data->terminate_msym = m;
3358         }
3359
3360       addr = SYMBOL_VALUE_ADDRESS (bp_objfile_data->terminate_msym);
3361       b = create_internal_breakpoint (get_objfile_arch (objfile), addr,
3362                                       bp_std_terminate_master,
3363                                       &internal_breakpoint_ops);
3364       b->addr_string = xstrdup (func_name);
3365       b->enable_state = bp_disabled;
3366     }
3367   }
3368
3369   update_global_location_list (1);
3370
3371   do_cleanups (old_chain);
3372 }
3373
3374 /* Install a master breakpoint on the unwinder's debug hook.  */
3375
3376 static void
3377 create_exception_master_breakpoint (void)
3378 {
3379   struct objfile *objfile;
3380   const char *const func_name = "_Unwind_DebugHook";
3381
3382   ALL_OBJFILES (objfile)
3383     {
3384       struct breakpoint *b;
3385       struct gdbarch *gdbarch;
3386       struct breakpoint_objfile_data *bp_objfile_data;
3387       CORE_ADDR addr;
3388
3389       bp_objfile_data = get_breakpoint_objfile_data (objfile);
3390
3391       /* We prefer the SystemTap probe point if it exists.  */
3392       if (!bp_objfile_data->exception_searched)
3393         {
3394           VEC (probe_p) *ret;
3395
3396           ret = find_probes_in_objfile (objfile, "libgcc", "unwind");
3397
3398           if (ret != NULL)
3399             {
3400               /* We are only interested in checking one element.  */
3401               struct probe *p = VEC_index (probe_p, ret, 0);
3402
3403               if (!can_evaluate_probe_arguments (p))
3404                 {
3405                   /* We cannot use the probe interface here, because it does
3406                      not know how to evaluate arguments.  */
3407                   VEC_free (probe_p, ret);
3408                   ret = NULL;
3409                 }
3410             }
3411           bp_objfile_data->exception_probes = ret;
3412           bp_objfile_data->exception_searched = 1;
3413         }
3414
3415       if (bp_objfile_data->exception_probes != NULL)
3416         {
3417           struct gdbarch *gdbarch = get_objfile_arch (objfile);
3418           int i;
3419           struct probe *probe;
3420
3421           for (i = 0;
3422                VEC_iterate (probe_p,
3423                             bp_objfile_data->exception_probes,
3424                             i, probe);
3425                ++i)
3426             {
3427               struct breakpoint *b;
3428
3429               b = create_internal_breakpoint (gdbarch, probe->address,
3430                                               bp_exception_master,
3431                                               &internal_breakpoint_ops);
3432               b->addr_string = xstrdup ("-probe-stap libgcc:unwind");
3433               b->enable_state = bp_disabled;
3434             }
3435
3436           continue;
3437         }
3438
3439       /* Otherwise, try the hook function.  */
3440
3441       if (msym_not_found_p (bp_objfile_data->exception_msym))
3442         continue;
3443
3444       gdbarch = get_objfile_arch (objfile);
3445
3446       if (bp_objfile_data->exception_msym == NULL)
3447         {
3448           struct minimal_symbol *debug_hook;
3449
3450           debug_hook = lookup_minimal_symbol (func_name, NULL, objfile);
3451           if (debug_hook == NULL)
3452             {
3453               bp_objfile_data->exception_msym = &msym_not_found;
3454               continue;
3455             }
3456
3457           bp_objfile_data->exception_msym = debug_hook;
3458         }
3459
3460       addr = SYMBOL_VALUE_ADDRESS (bp_objfile_data->exception_msym);
3461       addr = gdbarch_convert_from_func_ptr_addr (gdbarch, addr,
3462                                                  &current_target);
3463       b = create_internal_breakpoint (gdbarch, addr, bp_exception_master,
3464                                       &internal_breakpoint_ops);
3465       b->addr_string = xstrdup (func_name);
3466       b->enable_state = bp_disabled;
3467     }
3468
3469   update_global_location_list (1);
3470 }
3471
3472 void
3473 update_breakpoints_after_exec (void)
3474 {
3475   struct breakpoint *b, *b_tmp;
3476   struct bp_location *bploc, **bplocp_tmp;
3477
3478   /* We're about to delete breakpoints from GDB's lists.  If the
3479      INSERTED flag is true, GDB will try to lift the breakpoints by
3480      writing the breakpoints' "shadow contents" back into memory.  The
3481      "shadow contents" are NOT valid after an exec, so GDB should not
3482      do that.  Instead, the target is responsible from marking
3483      breakpoints out as soon as it detects an exec.  We don't do that
3484      here instead, because there may be other attempts to delete
3485      breakpoints after detecting an exec and before reaching here.  */
3486   ALL_BP_LOCATIONS (bploc, bplocp_tmp)
3487     if (bploc->pspace == current_program_space)
3488       gdb_assert (!bploc->inserted);
3489
3490   ALL_BREAKPOINTS_SAFE (b, b_tmp)
3491   {
3492     if (b->pspace != current_program_space)
3493       continue;
3494
3495     /* Solib breakpoints must be explicitly reset after an exec().  */
3496     if (b->type == bp_shlib_event)
3497       {
3498         delete_breakpoint (b);
3499         continue;
3500       }
3501
3502     /* JIT breakpoints must be explicitly reset after an exec().  */
3503     if (b->type == bp_jit_event)
3504       {
3505         delete_breakpoint (b);
3506         continue;
3507       }
3508
3509     /* Thread event breakpoints must be set anew after an exec(),
3510        as must overlay event and longjmp master breakpoints.  */
3511     if (b->type == bp_thread_event || b->type == bp_overlay_event
3512         || b->type == bp_longjmp_master || b->type == bp_std_terminate_master
3513         || b->type == bp_exception_master)
3514       {
3515         delete_breakpoint (b);
3516         continue;
3517       }
3518
3519     /* Step-resume breakpoints are meaningless after an exec().  */
3520     if (b->type == bp_step_resume || b->type == bp_hp_step_resume)
3521       {
3522         delete_breakpoint (b);
3523         continue;
3524       }
3525
3526     /* Longjmp and longjmp-resume breakpoints are also meaningless
3527        after an exec.  */
3528     if (b->type == bp_longjmp || b->type == bp_longjmp_resume
3529         || b->type == bp_longjmp_call_dummy
3530         || b->type == bp_exception || b->type == bp_exception_resume)
3531       {
3532         delete_breakpoint (b);
3533         continue;
3534       }
3535
3536     if (b->type == bp_catchpoint)
3537       {
3538         /* For now, none of the bp_catchpoint breakpoints need to
3539            do anything at this point.  In the future, if some of
3540            the catchpoints need to something, we will need to add
3541            a new method, and call this method from here.  */
3542         continue;
3543       }
3544
3545     /* bp_finish is a special case.  The only way we ought to be able
3546        to see one of these when an exec() has happened, is if the user
3547        caught a vfork, and then said "finish".  Ordinarily a finish just
3548        carries them to the call-site of the current callee, by setting
3549        a temporary bp there and resuming.  But in this case, the finish
3550        will carry them entirely through the vfork & exec.
3551
3552        We don't want to allow a bp_finish to remain inserted now.  But
3553        we can't safely delete it, 'cause finish_command has a handle to
3554        the bp on a bpstat, and will later want to delete it.  There's a
3555        chance (and I've seen it happen) that if we delete the bp_finish
3556        here, that its storage will get reused by the time finish_command
3557        gets 'round to deleting the "use to be a bp_finish" breakpoint.
3558        We really must allow finish_command to delete a bp_finish.
3559
3560        In the absence of a general solution for the "how do we know
3561        it's safe to delete something others may have handles to?"
3562        problem, what we'll do here is just uninsert the bp_finish, and
3563        let finish_command delete it.
3564
3565        (We know the bp_finish is "doomed" in the sense that it's
3566        momentary, and will be deleted as soon as finish_command sees
3567        the inferior stopped.  So it doesn't matter that the bp's
3568        address is probably bogus in the new a.out, unlike e.g., the
3569        solib breakpoints.)  */
3570
3571     if (b->type == bp_finish)
3572       {
3573         continue;
3574       }
3575
3576     /* Without a symbolic address, we have little hope of the
3577        pre-exec() address meaning the same thing in the post-exec()
3578        a.out.  */
3579     if (b->addr_string == NULL)
3580       {
3581         delete_breakpoint (b);
3582         continue;
3583       }
3584   }
3585   /* FIXME what about longjmp breakpoints?  Re-create them here?  */
3586   create_overlay_event_breakpoint ();
3587   create_longjmp_master_breakpoint ();
3588   create_std_terminate_master_breakpoint ();
3589   create_exception_master_breakpoint ();
3590 }
3591
3592 int
3593 detach_breakpoints (ptid_t ptid)
3594 {
3595   struct bp_location *bl, **blp_tmp;
3596   int val = 0;
3597   struct cleanup *old_chain = save_inferior_ptid ();
3598   struct inferior *inf = current_inferior ();
3599
3600   if (ptid_get_pid (ptid) == ptid_get_pid (inferior_ptid))
3601     error (_("Cannot detach breakpoints of inferior_ptid"));
3602
3603   /* Set inferior_ptid; remove_breakpoint_1 uses this global.  */
3604   inferior_ptid = ptid;
3605   ALL_BP_LOCATIONS (bl, blp_tmp)
3606   {
3607     if (bl->pspace != inf->pspace)
3608       continue;
3609
3610     /* This function must physically remove breakpoints locations
3611        from the specified ptid, without modifying the breakpoint
3612        package's state.  Locations of type bp_loc_other are only
3613        maintained at GDB side.  So, there is no need to remove
3614        these bp_loc_other locations.  Moreover, removing these
3615        would modify the breakpoint package's state.  */
3616     if (bl->loc_type == bp_loc_other)
3617       continue;
3618
3619     if (bl->inserted)
3620       val |= remove_breakpoint_1 (bl, mark_inserted);
3621   }
3622
3623   /* Detach single-step breakpoints as well.  */
3624   detach_single_step_breakpoints ();
3625
3626   do_cleanups (old_chain);
3627   return val;
3628 }
3629
3630 /* Remove the breakpoint location BL from the current address space.
3631    Note that this is used to detach breakpoints from a child fork.
3632    When we get here, the child isn't in the inferior list, and neither
3633    do we have objects to represent its address space --- we should
3634    *not* look at bl->pspace->aspace here.  */
3635
3636 static int
3637 remove_breakpoint_1 (struct bp_location *bl, insertion_state_t is)
3638 {
3639   int val;
3640
3641   /* BL is never in moribund_locations by our callers.  */
3642   gdb_assert (bl->owner != NULL);
3643
3644   if (bl->owner->enable_state == bp_permanent)
3645     /* Permanent breakpoints cannot be inserted or removed.  */
3646     return 0;
3647
3648   /* The type of none suggests that owner is actually deleted.
3649      This should not ever happen.  */
3650   gdb_assert (bl->owner->type != bp_none);
3651
3652   if (bl->loc_type == bp_loc_software_breakpoint
3653       || bl->loc_type == bp_loc_hardware_breakpoint)
3654     {
3655       /* "Normal" instruction breakpoint: either the standard
3656          trap-instruction bp (bp_breakpoint), or a
3657          bp_hardware_breakpoint.  */
3658
3659       /* First check to see if we have to handle an overlay.  */
3660       if (overlay_debugging == ovly_off
3661           || bl->section == NULL
3662           || !(section_is_overlay (bl->section)))
3663         {
3664           /* No overlay handling: just remove the breakpoint.  */
3665           val = bl->owner->ops->remove_location (bl);
3666         }
3667       else
3668         {
3669           /* This breakpoint is in an overlay section.
3670              Did we set a breakpoint at the LMA?  */
3671           if (!overlay_events_enabled)
3672               {
3673                 /* Yes -- overlay event support is not active, so we
3674                    should have set a breakpoint at the LMA.  Remove it.  
3675                 */
3676                 /* Ignore any failures: if the LMA is in ROM, we will
3677                    have already warned when we failed to insert it.  */
3678                 if (bl->loc_type == bp_loc_hardware_breakpoint)
3679                   target_remove_hw_breakpoint (bl->gdbarch,
3680                                                &bl->overlay_target_info);
3681                 else
3682                   target_remove_breakpoint (bl->gdbarch,
3683                                             &bl->overlay_target_info);
3684               }
3685           /* Did we set a breakpoint at the VMA? 
3686              If so, we will have marked the breakpoint 'inserted'.  */
3687           if (bl->inserted)
3688             {
3689               /* Yes -- remove it.  Previously we did not bother to
3690                  remove the breakpoint if the section had been
3691                  unmapped, but let's not rely on that being safe.  We
3692                  don't know what the overlay manager might do.  */
3693
3694               /* However, we should remove *software* breakpoints only
3695                  if the section is still mapped, or else we overwrite
3696                  wrong code with the saved shadow contents.  */
3697               if (bl->loc_type == bp_loc_hardware_breakpoint
3698                   || section_is_mapped (bl->section))
3699                 val = bl->owner->ops->remove_location (bl);
3700               else
3701                 val = 0;
3702             }
3703           else
3704             {
3705               /* No -- not inserted, so no need to remove.  No error.  */
3706               val = 0;
3707             }
3708         }
3709
3710       /* In some cases, we might not be able to remove a breakpoint
3711          in a shared library that has already been removed, but we
3712          have not yet processed the shlib unload event.  */
3713       if (val && solib_name_from_address (bl->pspace, bl->address))
3714         val = 0;
3715
3716       if (val)
3717         return val;
3718       bl->inserted = (is == mark_inserted);
3719     }
3720   else if (bl->loc_type == bp_loc_hardware_watchpoint)
3721     {
3722       gdb_assert (bl->owner->ops != NULL
3723                   && bl->owner->ops->remove_location != NULL);
3724
3725       bl->inserted = (is == mark_inserted);
3726       bl->owner->ops->remove_location (bl);
3727
3728       /* Failure to remove any of the hardware watchpoints comes here.  */
3729       if ((is == mark_uninserted) && (bl->inserted))
3730         warning (_("Could not remove hardware watchpoint %d."),
3731                  bl->owner->number);
3732     }
3733   else if (bl->owner->type == bp_catchpoint
3734            && breakpoint_enabled (bl->owner)
3735            && !bl->duplicate)
3736     {
3737       gdb_assert (bl->owner->ops != NULL
3738                   && bl->owner->ops->remove_location != NULL);
3739
3740       val = bl->owner->ops->remove_location (bl);
3741       if (val)
3742         return val;
3743
3744       bl->inserted = (is == mark_inserted);
3745     }
3746
3747   return 0;
3748 }
3749
3750 static int
3751 remove_breakpoint (struct bp_location *bl, insertion_state_t is)
3752 {
3753   int ret;
3754   struct cleanup *old_chain;
3755
3756   /* BL is never in moribund_locations by our callers.  */
3757   gdb_assert (bl->owner != NULL);
3758
3759   if (bl->owner->enable_state == bp_permanent)
3760     /* Permanent breakpoints cannot be inserted or removed.  */
3761     return 0;
3762
3763   /* The type of none suggests that owner is actually deleted.
3764      This should not ever happen.  */
3765   gdb_assert (bl->owner->type != bp_none);
3766
3767   old_chain = save_current_space_and_thread ();
3768
3769   switch_to_program_space_and_thread (bl->pspace);
3770
3771   ret = remove_breakpoint_1 (bl, is);
3772
3773   do_cleanups (old_chain);
3774   return ret;
3775 }
3776
3777 /* Clear the "inserted" flag in all breakpoints.  */
3778
3779 void
3780 mark_breakpoints_out (void)
3781 {
3782   struct bp_location *bl, **blp_tmp;
3783
3784   ALL_BP_LOCATIONS (bl, blp_tmp)
3785     if (bl->pspace == current_program_space)
3786       bl->inserted = 0;
3787 }
3788
3789 /* Clear the "inserted" flag in all breakpoints and delete any
3790    breakpoints which should go away between runs of the program.
3791
3792    Plus other such housekeeping that has to be done for breakpoints
3793    between runs.
3794
3795    Note: this function gets called at the end of a run (by
3796    generic_mourn_inferior) and when a run begins (by
3797    init_wait_for_inferior).  */
3798
3799
3800
3801 void
3802 breakpoint_init_inferior (enum inf_context context)
3803 {
3804   struct breakpoint *b, *b_tmp;
3805   struct bp_location *bl, **blp_tmp;
3806   int ix;
3807   struct program_space *pspace = current_program_space;
3808
3809   /* If breakpoint locations are shared across processes, then there's
3810      nothing to do.  */
3811   if (gdbarch_has_global_breakpoints (target_gdbarch ()))
3812     return;
3813
3814   ALL_BP_LOCATIONS (bl, blp_tmp)
3815   {
3816     /* ALL_BP_LOCATIONS bp_location has BL->OWNER always non-NULL.  */
3817     if (bl->pspace == pspace
3818         && bl->owner->enable_state != bp_permanent)
3819       bl->inserted = 0;
3820   }
3821
3822   ALL_BREAKPOINTS_SAFE (b, b_tmp)
3823   {
3824     if (b->loc && b->loc->pspace != pspace)
3825       continue;
3826
3827     switch (b->type)
3828       {
3829       case bp_call_dummy:
3830       case bp_longjmp_call_dummy:
3831
3832         /* If the call dummy breakpoint is at the entry point it will
3833            cause problems when the inferior is rerun, so we better get
3834            rid of it.  */
3835
3836       case bp_watchpoint_scope:
3837
3838         /* Also get rid of scope breakpoints.  */
3839
3840       case bp_shlib_event:
3841
3842         /* Also remove solib event breakpoints.  Their addresses may
3843            have changed since the last time we ran the program.
3844            Actually we may now be debugging against different target;
3845            and so the solib backend that installed this breakpoint may
3846            not be used in by the target.  E.g.,
3847
3848            (gdb) file prog-linux
3849            (gdb) run               # native linux target
3850            ...
3851            (gdb) kill
3852            (gdb) file prog-win.exe
3853            (gdb) tar rem :9999     # remote Windows gdbserver.
3854         */
3855
3856       case bp_step_resume:
3857
3858         /* Also remove step-resume breakpoints.  */
3859
3860         delete_breakpoint (b);
3861         break;
3862
3863       case bp_watchpoint:
3864       case bp_hardware_watchpoint:
3865       case bp_read_watchpoint:
3866       case bp_access_watchpoint:
3867         {
3868           struct watchpoint *w = (struct watchpoint *) b;
3869
3870           /* Likewise for watchpoints on local expressions.  */
3871           if (w->exp_valid_block != NULL)
3872             delete_breakpoint (b);
3873           else if (context == inf_starting)
3874             {
3875               /* Reset val field to force reread of starting value in
3876                  insert_breakpoints.  */
3877               if (w->val)
3878                 value_free (w->val);
3879               w->val = NULL;
3880               w->val_valid = 0;
3881           }
3882         }
3883         break;
3884       default:
3885         break;
3886       }
3887   }
3888
3889   /* Get rid of the moribund locations.  */
3890   for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, bl); ++ix)
3891     decref_bp_location (&bl);
3892   VEC_free (bp_location_p, moribund_locations);
3893 }
3894
3895 /* These functions concern about actual breakpoints inserted in the
3896    target --- to e.g. check if we need to do decr_pc adjustment or if
3897    we need to hop over the bkpt --- so we check for address space
3898    match, not program space.  */
3899
3900 /* breakpoint_here_p (PC) returns non-zero if an enabled breakpoint
3901    exists at PC.  It returns ordinary_breakpoint_here if it's an
3902    ordinary breakpoint, or permanent_breakpoint_here if it's a
3903    permanent breakpoint.
3904    - When continuing from a location with an ordinary breakpoint, we
3905      actually single step once before calling insert_breakpoints.
3906    - When continuing from a location with a permanent breakpoint, we
3907      need to use the `SKIP_PERMANENT_BREAKPOINT' macro, provided by
3908      the target, to advance the PC past the breakpoint.  */
3909
3910 enum breakpoint_here
3911 breakpoint_here_p (struct address_space *aspace, CORE_ADDR pc)
3912 {
3913   struct bp_location *bl, **blp_tmp;
3914   int any_breakpoint_here = 0;
3915
3916   ALL_BP_LOCATIONS (bl, blp_tmp)
3917     {
3918       if (bl->loc_type != bp_loc_software_breakpoint
3919           && bl->loc_type != bp_loc_hardware_breakpoint)
3920         continue;
3921
3922       /* ALL_BP_LOCATIONS bp_location has BL->OWNER always non-NULL.  */
3923       if ((breakpoint_enabled (bl->owner)
3924            || bl->owner->enable_state == bp_permanent)
3925           && breakpoint_location_address_match (bl, aspace, pc))
3926         {
3927           if (overlay_debugging 
3928               && section_is_overlay (bl->section)
3929               && !section_is_mapped (bl->section))
3930             continue;           /* unmapped overlay -- can't be a match */
3931           else if (bl->owner->enable_state == bp_permanent)
3932             return permanent_breakpoint_here;
3933           else
3934             any_breakpoint_here = 1;
3935         }
3936     }
3937
3938   return any_breakpoint_here ? ordinary_breakpoint_here : 0;
3939 }
3940
3941 /* Return true if there's a moribund breakpoint at PC.  */
3942
3943 int
3944 moribund_breakpoint_here_p (struct address_space *aspace, CORE_ADDR pc)
3945 {
3946   struct bp_location *loc;
3947   int ix;
3948
3949   for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, loc); ++ix)
3950     if (breakpoint_location_address_match (loc, aspace, pc))
3951       return 1;
3952
3953   return 0;
3954 }
3955
3956 /* Returns non-zero if there's a breakpoint inserted at PC, which is
3957    inserted using regular breakpoint_chain / bp_location array
3958    mechanism.  This does not check for single-step breakpoints, which
3959    are inserted and removed using direct target manipulation.  */
3960
3961 int
3962 regular_breakpoint_inserted_here_p (struct address_space *aspace, 
3963                                     CORE_ADDR pc)
3964 {
3965   struct bp_location *bl, **blp_tmp;
3966
3967   ALL_BP_LOCATIONS (bl, blp_tmp)
3968     {
3969       if (bl->loc_type != bp_loc_software_breakpoint
3970           && bl->loc_type != bp_loc_hardware_breakpoint)
3971         continue;
3972
3973       if (bl->inserted
3974           && breakpoint_location_address_match (bl, aspace, pc))
3975         {
3976           if (overlay_debugging 
3977               && section_is_overlay (bl->section)
3978               && !section_is_mapped (bl->section))
3979             continue;           /* unmapped overlay -- can't be a match */
3980           else
3981             return 1;
3982         }
3983     }
3984   return 0;
3985 }
3986
3987 /* Returns non-zero iff there's either regular breakpoint
3988    or a single step breakpoint inserted at PC.  */
3989
3990 int
3991 breakpoint_inserted_here_p (struct address_space *aspace, CORE_ADDR pc)
3992 {
3993   if (regular_breakpoint_inserted_here_p (aspace, pc))
3994     return 1;
3995
3996   if (single_step_breakpoint_inserted_here_p (aspace, pc))
3997     return 1;
3998
3999   return 0;
4000 }
4001
4002 /* This function returns non-zero iff there is a software breakpoint
4003    inserted at PC.  */
4004
4005 int
4006 software_breakpoint_inserted_here_p (struct address_space *aspace,
4007                                      CORE_ADDR pc)
4008 {
4009   struct bp_location *bl, **blp_tmp;
4010
4011   ALL_BP_LOCATIONS (bl, blp_tmp)
4012     {
4013       if (bl->loc_type != bp_loc_software_breakpoint)
4014         continue;
4015
4016       if (bl->inserted
4017           && breakpoint_address_match (bl->pspace->aspace, bl->address,
4018                                        aspace, pc))
4019         {
4020           if (overlay_debugging 
4021               && section_is_overlay (bl->section)
4022               && !section_is_mapped (bl->section))
4023             continue;           /* unmapped overlay -- can't be a match */
4024           else
4025             return 1;
4026         }
4027     }
4028
4029   /* Also check for software single-step breakpoints.  */
4030   if (single_step_breakpoint_inserted_here_p (aspace, pc))
4031     return 1;
4032
4033   return 0;
4034 }
4035
4036 int
4037 hardware_watchpoint_inserted_in_range (struct address_space *aspace,
4038                                        CORE_ADDR addr, ULONGEST len)
4039 {
4040   struct breakpoint *bpt;
4041
4042   ALL_BREAKPOINTS (bpt)
4043     {
4044       struct bp_location *loc;
4045
4046       if (bpt->type != bp_hardware_watchpoint
4047           && bpt->type != bp_access_watchpoint)
4048         continue;
4049
4050       if (!breakpoint_enabled (bpt))
4051         continue;
4052
4053       for (loc = bpt->loc; loc; loc = loc->next)
4054         if (loc->pspace->aspace == aspace && loc->inserted)
4055           {
4056             CORE_ADDR l, h;
4057
4058             /* Check for intersection.  */
4059             l = max (loc->address, addr);
4060             h = min (loc->address + loc->length, addr + len);
4061             if (l < h)
4062               return 1;
4063           }
4064     }
4065   return 0;
4066 }
4067
4068 /* breakpoint_thread_match (PC, PTID) returns true if the breakpoint at
4069    PC is valid for process/thread PTID.  */
4070
4071 int
4072 breakpoint_thread_match (struct address_space *aspace, CORE_ADDR pc,
4073                          ptid_t ptid)
4074 {
4075   struct bp_location *bl, **blp_tmp;
4076   /* The thread and task IDs associated to PTID, computed lazily.  */
4077   int thread = -1;
4078   int task = 0;
4079   
4080   ALL_BP_LOCATIONS (bl, blp_tmp)
4081     {
4082       if (bl->loc_type != bp_loc_software_breakpoint
4083           && bl->loc_type != bp_loc_hardware_breakpoint)
4084         continue;
4085
4086       /* ALL_BP_LOCATIONS bp_location has bl->OWNER always non-NULL.  */
4087       if (!breakpoint_enabled (bl->owner)
4088           && bl->owner->enable_state != bp_permanent)
4089         continue;
4090
4091       if (!breakpoint_location_address_match (bl, aspace, pc))
4092         continue;
4093
4094       if (bl->owner->thread != -1)
4095         {
4096           /* This is a thread-specific breakpoint.  Check that ptid
4097              matches that thread.  If thread hasn't been computed yet,
4098              it is now time to do so.  */
4099           if (thread == -1)
4100             thread = pid_to_thread_id (ptid);
4101           if (bl->owner->thread != thread)
4102             continue;
4103         }
4104
4105       if (bl->owner->task != 0)
4106         {
4107           /* This is a task-specific breakpoint.  Check that ptid
4108              matches that task.  If task hasn't been computed yet,
4109              it is now time to do so.  */
4110           if (task == 0)
4111             task = ada_get_task_number (ptid);
4112           if (bl->owner->task != task)
4113             continue;
4114         }
4115
4116       if (overlay_debugging 
4117           && section_is_overlay (bl->section)
4118           && !section_is_mapped (bl->section))
4119         continue;           /* unmapped overlay -- can't be a match */
4120
4121       return 1;
4122     }
4123
4124   return 0;
4125 }
4126 \f
4127
4128 /* bpstat stuff.  External routines' interfaces are documented
4129    in breakpoint.h.  */
4130
4131 int
4132 is_catchpoint (struct breakpoint *ep)
4133 {
4134   return (ep->type == bp_catchpoint);
4135 }
4136
4137 /* Frees any storage that is part of a bpstat.  Does not walk the
4138    'next' chain.  */
4139
4140 static void
4141 bpstat_free (bpstat bs)
4142 {
4143   if (bs->old_val != NULL)
4144     value_free (bs->old_val);
4145   decref_counted_command_line (&bs->commands);
4146   decref_bp_location (&bs->bp_location_at);
4147   xfree (bs);
4148 }
4149
4150 /* Clear a bpstat so that it says we are not at any breakpoint.
4151    Also free any storage that is part of a bpstat.  */
4152
4153 void
4154 bpstat_clear (bpstat *bsp)
4155 {
4156   bpstat p;
4157   bpstat q;
4158
4159   if (bsp == 0)
4160     return;
4161   p = *bsp;
4162   while (p != NULL)
4163     {
4164       q = p->next;
4165       bpstat_free (p);
4166       p = q;
4167     }
4168   *bsp = NULL;
4169 }
4170
4171 /* Return a copy of a bpstat.  Like "bs1 = bs2" but all storage that
4172    is part of the bpstat is copied as well.  */
4173
4174 bpstat
4175 bpstat_copy (bpstat bs)
4176 {
4177   bpstat p = NULL;
4178   bpstat tmp;
4179   bpstat retval = NULL;
4180
4181   if (bs == NULL)
4182     return bs;
4183
4184   for (; bs != NULL; bs = bs->next)
4185     {
4186       tmp = (bpstat) xmalloc (sizeof (*tmp));
4187       memcpy (tmp, bs, sizeof (*tmp));
4188       incref_counted_command_line (tmp->commands);
4189       incref_bp_location (tmp->bp_location_at);
4190       if (bs->old_val != NULL)
4191         {
4192           tmp->old_val = value_copy (bs->old_val);
4193           release_value (tmp->old_val);
4194         }
4195
4196       if (p == NULL)
4197         /* This is the first thing in the chain.  */
4198         retval = tmp;
4199       else
4200         p->next = tmp;
4201       p = tmp;
4202     }
4203   p->next = NULL;
4204   return retval;
4205 }
4206
4207 /* Find the bpstat associated with this breakpoint.  */
4208
4209 bpstat
4210 bpstat_find_breakpoint (bpstat bsp, struct breakpoint *breakpoint)
4211 {
4212   if (bsp == NULL)
4213     return NULL;
4214
4215   for (; bsp != NULL; bsp = bsp->next)
4216     {
4217       if (bsp->breakpoint_at == breakpoint)
4218         return bsp;
4219     }
4220   return NULL;
4221 }
4222
4223 /* See breakpoint.h.  */
4224
4225 enum bpstat_signal_value
4226 bpstat_explains_signal (bpstat bsp, enum gdb_signal sig)
4227 {
4228   enum bpstat_signal_value result = BPSTAT_SIGNAL_NO;
4229
4230   for (; bsp != NULL; bsp = bsp->next)
4231     {
4232       /* Ensure that, if we ever entered this loop, then we at least
4233          return BPSTAT_SIGNAL_HIDE.  */
4234       enum bpstat_signal_value newval;
4235
4236       if (bsp->breakpoint_at == NULL)
4237         {
4238           /* A moribund location can never explain a signal other than
4239              GDB_SIGNAL_TRAP.  */
4240           if (sig == GDB_SIGNAL_TRAP)
4241             newval = BPSTAT_SIGNAL_HIDE;
4242           else
4243             newval = BPSTAT_SIGNAL_NO;
4244         }
4245       else
4246         newval = bsp->breakpoint_at->ops->explains_signal (bsp->breakpoint_at,
4247                                                            sig);
4248
4249       if (newval > result)
4250         result = newval;
4251     }
4252
4253   return result;
4254 }
4255
4256 /* Put in *NUM the breakpoint number of the first breakpoint we are
4257    stopped at.  *BSP upon return is a bpstat which points to the
4258    remaining breakpoints stopped at (but which is not guaranteed to be
4259    good for anything but further calls to bpstat_num).
4260
4261    Return 0 if passed a bpstat which does not indicate any breakpoints.
4262    Return -1 if stopped at a breakpoint that has been deleted since
4263    we set it.
4264    Return 1 otherwise.  */
4265
4266 int
4267 bpstat_num (bpstat *bsp, int *num)
4268 {
4269   struct breakpoint *b;
4270
4271   if ((*bsp) == NULL)
4272     return 0;                   /* No more breakpoint values */
4273
4274   /* We assume we'll never have several bpstats that correspond to a
4275      single breakpoint -- otherwise, this function might return the
4276      same number more than once and this will look ugly.  */
4277   b = (*bsp)->breakpoint_at;
4278   *bsp = (*bsp)->next;
4279   if (b == NULL)
4280     return -1;                  /* breakpoint that's been deleted since */
4281
4282   *num = b->number;             /* We have its number */
4283   return 1;
4284 }
4285
4286 /* See breakpoint.h.  */
4287
4288 void
4289 bpstat_clear_actions (void)
4290 {
4291   struct thread_info *tp;
4292   bpstat bs;
4293
4294   if (ptid_equal (inferior_ptid, null_ptid))
4295     return;
4296
4297   tp = find_thread_ptid (inferior_ptid);
4298   if (tp == NULL)
4299     return;
4300
4301   for (bs = tp->control.stop_bpstat; bs != NULL; bs = bs->next)
4302     {
4303       decref_counted_command_line (&bs->commands);
4304
4305       if (bs->old_val != NULL)
4306         {
4307           value_free (bs->old_val);
4308           bs->old_val = NULL;
4309         }
4310     }
4311 }
4312
4313 /* Called when a command is about to proceed the inferior.  */
4314
4315 static void
4316 breakpoint_about_to_proceed (void)
4317 {
4318   if (!ptid_equal (inferior_ptid, null_ptid))
4319     {
4320       struct thread_info *tp = inferior_thread ();
4321
4322       /* Allow inferior function calls in breakpoint commands to not
4323          interrupt the command list.  When the call finishes
4324          successfully, the inferior will be standing at the same
4325          breakpoint as if nothing happened.  */
4326       if (tp->control.in_infcall)
4327         return;
4328     }
4329
4330   breakpoint_proceeded = 1;
4331 }
4332
4333 /* Stub for cleaning up our state if we error-out of a breakpoint
4334    command.  */
4335 static void
4336 cleanup_executing_breakpoints (void *ignore)
4337 {
4338   executing_breakpoint_commands = 0;
4339 }
4340
4341 /* Return non-zero iff CMD as the first line of a command sequence is `silent'
4342    or its equivalent.  */
4343
4344 static int
4345 command_line_is_silent (struct command_line *cmd)
4346 {
4347   return cmd && (strcmp ("silent", cmd->line) == 0
4348                  || (xdb_commands && strcmp ("Q", cmd->line) == 0));
4349 }
4350
4351 /* Execute all the commands associated with all the breakpoints at
4352    this location.  Any of these commands could cause the process to
4353    proceed beyond this point, etc.  We look out for such changes by
4354    checking the global "breakpoint_proceeded" after each command.
4355
4356    Returns true if a breakpoint command resumed the inferior.  In that
4357    case, it is the caller's responsibility to recall it again with the
4358    bpstat of the current thread.  */
4359
4360 static int
4361 bpstat_do_actions_1 (bpstat *bsp)
4362 {
4363   bpstat bs;
4364   struct cleanup *old_chain;
4365   int again = 0;
4366
4367   /* Avoid endless recursion if a `source' command is contained
4368      in bs->commands.  */
4369   if (executing_breakpoint_commands)
4370     return 0;
4371
4372   executing_breakpoint_commands = 1;
4373   old_chain = make_cleanup (cleanup_executing_breakpoints, 0);
4374
4375   prevent_dont_repeat ();
4376
4377   /* This pointer will iterate over the list of bpstat's.  */
4378   bs = *bsp;
4379
4380   breakpoint_proceeded = 0;
4381   for (; bs != NULL; bs = bs->next)
4382     {
4383       struct counted_command_line *ccmd;
4384       struct command_line *cmd;
4385       struct cleanup *this_cmd_tree_chain;
4386
4387       /* Take ownership of the BSP's command tree, if it has one.
4388
4389          The command tree could legitimately contain commands like
4390          'step' and 'next', which call clear_proceed_status, which
4391          frees stop_bpstat's command tree.  To make sure this doesn't
4392          free the tree we're executing out from under us, we need to
4393          take ownership of the tree ourselves.  Since a given bpstat's
4394          commands are only executed once, we don't need to copy it; we
4395          can clear the pointer in the bpstat, and make sure we free
4396          the tree when we're done.  */
4397       ccmd = bs->commands;
4398       bs->commands = NULL;
4399       this_cmd_tree_chain = make_cleanup_decref_counted_command_line (&ccmd);
4400       cmd = ccmd ? ccmd->commands : NULL;
4401       if (command_line_is_silent (cmd))
4402         {
4403           /* The action has been already done by bpstat_stop_status.  */
4404           cmd = cmd->next;
4405         }
4406
4407       while (cmd != NULL)
4408         {
4409           execute_control_command (cmd);
4410
4411           if (breakpoint_proceeded)
4412             break;
4413           else
4414             cmd = cmd->next;
4415         }
4416
4417       /* We can free this command tree now.  */
4418       do_cleanups (this_cmd_tree_chain);
4419
4420       if (breakpoint_proceeded)
4421         {
4422           if (target_can_async_p ())
4423             /* If we are in async mode, then the target might be still
4424                running, not stopped at any breakpoint, so nothing for
4425                us to do here -- just return to the event loop.  */
4426             ;
4427           else
4428             /* In sync mode, when execute_control_command returns
4429                we're already standing on the next breakpoint.
4430                Breakpoint commands for that stop were not run, since
4431                execute_command does not run breakpoint commands --
4432                only command_line_handler does, but that one is not
4433                involved in execution of breakpoint commands.  So, we
4434                can now execute breakpoint commands.  It should be
4435                noted that making execute_command do bpstat actions is
4436                not an option -- in this case we'll have recursive
4437                invocation of bpstat for each breakpoint with a
4438                command, and can easily blow up GDB stack.  Instead, we
4439                return true, which will trigger the caller to recall us
4440                with the new stop_bpstat.  */
4441             again = 1;
4442           break;
4443         }
4444     }
4445   do_cleanups (old_chain);
4446   return again;
4447 }
4448
4449 void
4450 bpstat_do_actions (void)
4451 {
4452   struct cleanup *cleanup_if_error = make_bpstat_clear_actions_cleanup ();
4453
4454   /* Do any commands attached to breakpoint we are stopped at.  */
4455   while (!ptid_equal (inferior_ptid, null_ptid)
4456          && target_has_execution
4457          && !is_exited (inferior_ptid)
4458          && !is_executing (inferior_ptid))
4459     /* Since in sync mode, bpstat_do_actions may resume the inferior,
4460        and only return when it is stopped at the next breakpoint, we
4461        keep doing breakpoint actions until it returns false to
4462        indicate the inferior was not resumed.  */
4463     if (!bpstat_do_actions_1 (&inferior_thread ()->control.stop_bpstat))
4464       break;
4465
4466   discard_cleanups (cleanup_if_error);
4467 }
4468
4469 /* Print out the (old or new) value associated with a watchpoint.  */
4470
4471 static void
4472 watchpoint_value_print (struct value *val, struct ui_file *stream)
4473 {
4474   if (val == NULL)
4475     fprintf_unfiltered (stream, _("<unreadable>"));
4476   else
4477     {
4478       struct value_print_options opts;
4479       get_user_print_options (&opts);
4480       value_print (val, stream, &opts);
4481     }
4482 }
4483
4484 /* Generic routine for printing messages indicating why we
4485    stopped.  The behavior of this function depends on the value
4486    'print_it' in the bpstat structure.  Under some circumstances we
4487    may decide not to print anything here and delegate the task to
4488    normal_stop().  */
4489
4490 static enum print_stop_action
4491 print_bp_stop_message (bpstat bs)
4492 {
4493   switch (bs->print_it)
4494     {
4495     case print_it_noop:
4496       /* Nothing should be printed for this bpstat entry.  */
4497       return PRINT_UNKNOWN;
4498       break;
4499
4500     case print_it_done:
4501       /* We still want to print the frame, but we already printed the
4502          relevant messages.  */
4503       return PRINT_SRC_AND_LOC;
4504       break;
4505
4506     case print_it_normal:
4507       {
4508         struct breakpoint *b = bs->breakpoint_at;
4509
4510         /* bs->breakpoint_at can be NULL if it was a momentary breakpoint
4511            which has since been deleted.  */
4512         if (b == NULL)
4513           return PRINT_UNKNOWN;
4514
4515         /* Normal case.  Call the breakpoint's print_it method.  */
4516         return b->ops->print_it (bs);
4517       }
4518       break;
4519
4520     default:
4521       internal_error (__FILE__, __LINE__,
4522                       _("print_bp_stop_message: unrecognized enum value"));
4523       break;
4524     }
4525 }
4526
4527 /* A helper function that prints a shared library stopped event.  */
4528
4529 static void
4530 print_solib_event (int is_catchpoint)
4531 {
4532   int any_deleted
4533     = !VEC_empty (char_ptr, current_program_space->deleted_solibs);
4534   int any_added
4535     = !VEC_empty (so_list_ptr, current_program_space->added_solibs);
4536
4537   if (!is_catchpoint)
4538     {
4539       if (any_added || any_deleted)
4540         ui_out_text (current_uiout,
4541                      _("Stopped due to shared library event:\n"));
4542       else
4543         ui_out_text (current_uiout,
4544                      _("Stopped due to shared library event (no "
4545                        "libraries added or removed)\n"));
4546     }
4547
4548   if (ui_out_is_mi_like_p (current_uiout))
4549     ui_out_field_string (current_uiout, "reason",
4550                          async_reason_lookup (EXEC_ASYNC_SOLIB_EVENT));
4551
4552   if (any_deleted)
4553     {
4554       struct cleanup *cleanup;
4555       char *name;
4556       int ix;
4557
4558       ui_out_text (current_uiout, _("  Inferior unloaded "));
4559       cleanup = make_cleanup_ui_out_list_begin_end (current_uiout,
4560                                                     "removed");
4561       for (ix = 0;
4562            VEC_iterate (char_ptr, current_program_space->deleted_solibs,
4563                         ix, name);
4564            ++ix)
4565         {
4566           if (ix > 0)
4567             ui_out_text (current_uiout, "    ");
4568           ui_out_field_string (current_uiout, "library", name);
4569           ui_out_text (current_uiout, "\n");
4570         }
4571
4572       do_cleanups (cleanup);
4573     }
4574
4575   if (any_added)
4576     {
4577       struct so_list *iter;
4578       int ix;
4579       struct cleanup *cleanup;
4580
4581       ui_out_text (current_uiout, _("  Inferior loaded "));
4582       cleanup = make_cleanup_ui_out_list_begin_end (current_uiout,
4583                                                     "added");
4584       for (ix = 0;
4585            VEC_iterate (so_list_ptr, current_program_space->added_solibs,
4586                         ix, iter);
4587            ++ix)
4588         {
4589           if (ix > 0)
4590             ui_out_text (current_uiout, "    ");
4591           ui_out_field_string (current_uiout, "library", iter->so_name);
4592           ui_out_text (current_uiout, "\n");
4593         }
4594
4595       do_cleanups (cleanup);
4596     }
4597 }
4598
4599 /* Print a message indicating what happened.  This is called from
4600    normal_stop().  The input to this routine is the head of the bpstat
4601    list - a list of the eventpoints that caused this stop.  KIND is
4602    the target_waitkind for the stopping event.  This
4603    routine calls the generic print routine for printing a message
4604    about reasons for stopping.  This will print (for example) the
4605    "Breakpoint n," part of the output.  The return value of this
4606    routine is one of:
4607
4608    PRINT_UNKNOWN: Means we printed nothing.
4609    PRINT_SRC_AND_LOC: Means we printed something, and expect subsequent
4610    code to print the location.  An example is 
4611    "Breakpoint 1, " which should be followed by
4612    the location.
4613    PRINT_SRC_ONLY: Means we printed something, but there is no need
4614    to also print the location part of the message.
4615    An example is the catch/throw messages, which
4616    don't require a location appended to the end.
4617    PRINT_NOTHING: We have done some printing and we don't need any 
4618    further info to be printed.  */
4619
4620 enum print_stop_action
4621 bpstat_print (bpstat bs, int kind)
4622 {
4623   int val;
4624
4625   /* Maybe another breakpoint in the chain caused us to stop.
4626      (Currently all watchpoints go on the bpstat whether hit or not.
4627      That probably could (should) be changed, provided care is taken
4628      with respect to bpstat_explains_signal).  */
4629   for (; bs; bs = bs->next)
4630     {
4631       val = print_bp_stop_message (bs);
4632       if (val == PRINT_SRC_ONLY 
4633           || val == PRINT_SRC_AND_LOC 
4634           || val == PRINT_NOTHING)
4635         return val;
4636     }
4637
4638   /* If we had hit a shared library event breakpoint,
4639      print_bp_stop_message would print out this message.  If we hit an
4640      OS-level shared library event, do the same thing.  */
4641   if (kind == TARGET_WAITKIND_LOADED)
4642     {
4643       print_solib_event (0);
4644       return PRINT_NOTHING;
4645     }
4646
4647   /* We reached the end of the chain, or we got a null BS to start
4648      with and nothing was printed.  */
4649   return PRINT_UNKNOWN;
4650 }
4651
4652 /* Evaluate the expression EXP and return 1 if value is zero.
4653    This returns the inverse of the condition because it is called
4654    from catch_errors which returns 0 if an exception happened, and if an
4655    exception happens we want execution to stop.
4656    The argument is a "struct expression *" that has been cast to a
4657    "void *" to make it pass through catch_errors.  */
4658
4659 static int
4660 breakpoint_cond_eval (void *exp)
4661 {
4662   struct value *mark = value_mark ();
4663   int i = !value_true (evaluate_expression ((struct expression *) exp));
4664
4665   value_free_to_mark (mark);
4666   return i;
4667 }
4668
4669 /* Allocate a new bpstat.  Link it to the FIFO list by BS_LINK_POINTER.  */
4670
4671 static bpstat
4672 bpstat_alloc (struct bp_location *bl, bpstat **bs_link_pointer)
4673 {
4674   bpstat bs;
4675
4676   bs = (bpstat) xmalloc (sizeof (*bs));
4677   bs->next = NULL;
4678   **bs_link_pointer = bs;
4679   *bs_link_pointer = &bs->next;
4680   bs->breakpoint_at = bl->owner;
4681   bs->bp_location_at = bl;
4682   incref_bp_location (bl);
4683   /* If the condition is false, etc., don't do the commands.  */
4684   bs->commands = NULL;
4685   bs->old_val = NULL;
4686   bs->print_it = print_it_normal;
4687   return bs;
4688 }
4689 \f
4690 /* The target has stopped with waitstatus WS.  Check if any hardware
4691    watchpoints have triggered, according to the target.  */
4692
4693 int
4694 watchpoints_triggered (struct target_waitstatus *ws)
4695 {
4696   int stopped_by_watchpoint = target_stopped_by_watchpoint ();
4697   CORE_ADDR addr;
4698   struct breakpoint *b;
4699
4700   if (!stopped_by_watchpoint)
4701     {
4702       /* We were not stopped by a watchpoint.  Mark all watchpoints
4703          as not triggered.  */
4704       ALL_BREAKPOINTS (b)
4705         if (is_hardware_watchpoint (b))
4706           {
4707             struct watchpoint *w = (struct watchpoint *) b;
4708
4709             w->watchpoint_triggered = watch_triggered_no;
4710           }
4711
4712       return 0;
4713     }
4714
4715   if (!target_stopped_data_address (&current_target, &addr))
4716     {
4717       /* We were stopped by a watchpoint, but we don't know where.
4718          Mark all watchpoints as unknown.  */
4719       ALL_BREAKPOINTS (b)
4720         if (is_hardware_watchpoint (b))
4721           {
4722             struct watchpoint *w = (struct watchpoint *) b;
4723
4724             w->watchpoint_triggered = watch_triggered_unknown;
4725           }
4726
4727       return 1;
4728     }
4729
4730   /* The target could report the data address.  Mark watchpoints
4731      affected by this data address as triggered, and all others as not
4732      triggered.  */
4733
4734   ALL_BREAKPOINTS (b)
4735     if (is_hardware_watchpoint (b))
4736       {
4737         struct watchpoint *w = (struct watchpoint *) b;
4738         struct bp_location *loc;
4739
4740         w->watchpoint_triggered = watch_triggered_no;
4741         for (loc = b->loc; loc; loc = loc->next)
4742           {
4743             if (is_masked_watchpoint (b))
4744               {
4745                 CORE_ADDR newaddr = addr & w->hw_wp_mask;
4746                 CORE_ADDR start = loc->address & w->hw_wp_mask;
4747
4748                 if (newaddr == start)
4749                   {
4750                     w->watchpoint_triggered = watch_triggered_yes;
4751                     break;
4752                   }
4753               }
4754             /* Exact match not required.  Within range is sufficient.  */
4755             else if (target_watchpoint_addr_within_range (&current_target,
4756                                                          addr, loc->address,
4757                                                          loc->length))
4758               {
4759                 w->watchpoint_triggered = watch_triggered_yes;
4760                 break;
4761               }
4762           }
4763       }
4764
4765   return 1;
4766 }
4767
4768 /* Possible return values for watchpoint_check (this can't be an enum
4769    because of check_errors).  */
4770 /* The watchpoint has been deleted.  */
4771 #define WP_DELETED 1
4772 /* The value has changed.  */
4773 #define WP_VALUE_CHANGED 2
4774 /* The value has not changed.  */
4775 #define WP_VALUE_NOT_CHANGED 3
4776 /* Ignore this watchpoint, no matter if the value changed or not.  */
4777 #define WP_IGNORE 4
4778
4779 #define BP_TEMPFLAG 1
4780 #define BP_HARDWAREFLAG 2
4781
4782 /* Evaluate watchpoint condition expression and check if its value
4783    changed.
4784
4785    P should be a pointer to struct bpstat, but is defined as a void *
4786    in order for this function to be usable with catch_errors.  */
4787
4788 static int
4789 watchpoint_check (void *p)
4790 {
4791   bpstat bs = (bpstat) p;
4792   struct watchpoint *b;
4793   struct frame_info *fr;
4794   int within_current_scope;
4795
4796   /* BS is built from an existing struct breakpoint.  */
4797   gdb_assert (bs->breakpoint_at != NULL);
4798   b = (struct watchpoint *) bs->breakpoint_at;
4799
4800   /* If this is a local watchpoint, we only want to check if the
4801      watchpoint frame is in scope if the current thread is the thread
4802      that was used to create the watchpoint.  */
4803   if (!watchpoint_in_thread_scope (b))
4804     return WP_IGNORE;
4805
4806   if (b->exp_valid_block == NULL)
4807     within_current_scope = 1;
4808   else
4809     {
4810       struct frame_info *frame = get_current_frame ();
4811       struct gdbarch *frame_arch = get_frame_arch (frame);
4812       CORE_ADDR frame_pc = get_frame_pc (frame);
4813
4814       /* in_function_epilogue_p() returns a non-zero value if we're
4815          still in the function but the stack frame has already been
4816          invalidated.  Since we can't rely on the values of local
4817          variables after the stack has been destroyed, we are treating
4818          the watchpoint in that state as `not changed' without further
4819          checking.  Don't mark watchpoints as changed if the current
4820          frame is in an epilogue - even if they are in some other
4821          frame, our view of the stack is likely to be wrong and
4822          frame_find_by_id could error out.  */
4823       if (gdbarch_in_function_epilogue_p (frame_arch, frame_pc))
4824         return WP_IGNORE;
4825
4826       fr = frame_find_by_id (b->watchpoint_frame);
4827       within_current_scope = (fr != NULL);
4828
4829       /* If we've gotten confused in the unwinder, we might have
4830          returned a frame that can't describe this variable.  */
4831       if (within_current_scope)
4832         {
4833           struct symbol *function;
4834
4835           function = get_frame_function (fr);
4836           if (function == NULL
4837               || !contained_in (b->exp_valid_block,
4838                                 SYMBOL_BLOCK_VALUE (function)))
4839             within_current_scope = 0;
4840         }
4841
4842       if (within_current_scope)
4843         /* If we end up stopping, the current frame will get selected
4844            in normal_stop.  So this call to select_frame won't affect
4845            the user.  */
4846         select_frame (fr);
4847     }
4848
4849   if (within_current_scope)
4850     {
4851       /* We use value_{,free_to_}mark because it could be a *long*
4852          time before we return to the command level and call
4853          free_all_values.  We can't call free_all_values because we
4854          might be in the middle of evaluating a function call.  */
4855
4856       int pc = 0;
4857       struct value *mark;
4858       struct value *new_val;
4859
4860       if (is_masked_watchpoint (&b->base))
4861         /* Since we don't know the exact trigger address (from
4862            stopped_data_address), just tell the user we've triggered
4863            a mask watchpoint.  */
4864         return WP_VALUE_CHANGED;
4865
4866       mark = value_mark ();
4867       fetch_subexp_value (b->exp, &pc, &new_val, NULL, NULL, 0);
4868
4869       /* We use value_equal_contents instead of value_equal because
4870          the latter coerces an array to a pointer, thus comparing just
4871          the address of the array instead of its contents.  This is
4872          not what we want.  */
4873       if ((b->val != NULL) != (new_val != NULL)
4874           || (b->val != NULL && !value_equal_contents (b->val, new_val)))
4875         {
4876           if (new_val != NULL)
4877             {
4878               release_value (new_val);
4879               value_free_to_mark (mark);
4880             }
4881           bs->old_val = b->val;
4882           b->val = new_val;
4883           b->val_valid = 1;
4884           return WP_VALUE_CHANGED;
4885         }
4886       else
4887         {
4888           /* Nothing changed.  */
4889           value_free_to_mark (mark);
4890           return WP_VALUE_NOT_CHANGED;
4891         }
4892     }
4893   else
4894     {
4895       struct ui_out *uiout = current_uiout;
4896
4897       /* This seems like the only logical thing to do because
4898          if we temporarily ignored the watchpoint, then when
4899          we reenter the block in which it is valid it contains
4900          garbage (in the case of a function, it may have two
4901          garbage values, one before and one after the prologue).
4902          So we can't even detect the first assignment to it and
4903          watch after that (since the garbage may or may not equal
4904          the first value assigned).  */
4905       /* We print all the stop information in
4906          breakpoint_ops->print_it, but in this case, by the time we
4907          call breakpoint_ops->print_it this bp will be deleted
4908          already.  So we have no choice but print the information
4909          here.  */
4910       if (ui_out_is_mi_like_p (uiout))
4911         ui_out_field_string
4912           (uiout, "reason", async_reason_lookup (EXEC_ASYNC_WATCHPOINT_SCOPE));
4913       ui_out_text (uiout, "\nWatchpoint ");
4914       ui_out_field_int (uiout, "wpnum", b->base.number);
4915       ui_out_text (uiout,
4916                    " deleted because the program has left the block in\n\
4917 which its expression is valid.\n");     
4918
4919       /* Make sure the watchpoint's commands aren't executed.  */
4920       decref_counted_command_line (&b->base.commands);
4921       watchpoint_del_at_next_stop (b);
4922
4923       return WP_DELETED;
4924     }
4925 }
4926
4927 /* Return true if it looks like target has stopped due to hitting
4928    breakpoint location BL.  This function does not check if we should
4929    stop, only if BL explains the stop.  */
4930
4931 static int
4932 bpstat_check_location (const struct bp_location *bl,
4933                        struct address_space *aspace, CORE_ADDR bp_addr,
4934                        const struct target_waitstatus *ws)
4935 {
4936   struct breakpoint *b = bl->owner;
4937
4938   /* BL is from an existing breakpoint.  */
4939   gdb_assert (b != NULL);
4940
4941   return b->ops->breakpoint_hit (bl, aspace, bp_addr, ws);
4942 }
4943
4944 /* Determine if the watched values have actually changed, and we
4945    should stop.  If not, set BS->stop to 0.  */
4946
4947 static void
4948 bpstat_check_watchpoint (bpstat bs)
4949 {
4950   const struct bp_location *bl;
4951   struct watchpoint *b;
4952
4953   /* BS is built for existing struct breakpoint.  */
4954   bl = bs->bp_location_at;
4955   gdb_assert (bl != NULL);
4956   b = (struct watchpoint *) bs->breakpoint_at;
4957   gdb_assert (b != NULL);
4958
4959     {
4960       int must_check_value = 0;
4961       
4962       if (b->base.type == bp_watchpoint)
4963         /* For a software watchpoint, we must always check the
4964            watched value.  */
4965         must_check_value = 1;
4966       else if (b->watchpoint_triggered == watch_triggered_yes)
4967         /* We have a hardware watchpoint (read, write, or access)
4968            and the target earlier reported an address watched by
4969            this watchpoint.  */
4970         must_check_value = 1;
4971       else if (b->watchpoint_triggered == watch_triggered_unknown
4972                && b->base.type == bp_hardware_watchpoint)
4973         /* We were stopped by a hardware watchpoint, but the target could
4974            not report the data address.  We must check the watchpoint's
4975            value.  Access and read watchpoints are out of luck; without
4976            a data address, we can't figure it out.  */
4977         must_check_value = 1;
4978
4979       if (must_check_value)
4980         {
4981           char *message
4982             = xstrprintf ("Error evaluating expression for watchpoint %d\n",
4983                           b->base.number);
4984           struct cleanup *cleanups = make_cleanup (xfree, message);
4985           int e = catch_errors (watchpoint_check, bs, message,
4986                                 RETURN_MASK_ALL);
4987           do_cleanups (cleanups);
4988           switch (e)
4989             {
4990             case WP_DELETED:
4991               /* We've already printed what needs to be printed.  */
4992               bs->print_it = print_it_done;
4993               /* Stop.  */
4994               break;
4995             case WP_IGNORE:
4996               bs->print_it = print_it_noop;
4997               bs->stop = 0;
4998               break;
4999             case WP_VALUE_CHANGED:
5000               if (b->base.type == bp_read_watchpoint)
5001                 {
5002                   /* There are two cases to consider here:
5003
5004                      1. We're watching the triggered memory for reads.
5005                      In that case, trust the target, and always report
5006                      the watchpoint hit to the user.  Even though
5007                      reads don't cause value changes, the value may
5008                      have changed since the last time it was read, and
5009                      since we're not trapping writes, we will not see
5010                      those, and as such we should ignore our notion of
5011                      old value.
5012
5013                      2. We're watching the triggered memory for both
5014                      reads and writes.  There are two ways this may
5015                      happen:
5016
5017                      2.1. This is a target that can't break on data
5018                      reads only, but can break on accesses (reads or
5019                      writes), such as e.g., x86.  We detect this case
5020                      at the time we try to insert read watchpoints.
5021
5022                      2.2. Otherwise, the target supports read
5023                      watchpoints, but, the user set an access or write
5024                      watchpoint watching the same memory as this read
5025                      watchpoint.
5026
5027                      If we're watching memory writes as well as reads,
5028                      ignore watchpoint hits when we find that the
5029                      value hasn't changed, as reads don't cause
5030                      changes.  This still gives false positives when
5031                      the program writes the same value to memory as
5032                      what there was already in memory (we will confuse
5033                      it for a read), but it's much better than
5034                      nothing.  */
5035
5036                   int other_write_watchpoint = 0;
5037
5038                   if (bl->watchpoint_type == hw_read)
5039                     {
5040                       struct breakpoint *other_b;
5041
5042                       ALL_BREAKPOINTS (other_b)
5043                         if (other_b->type == bp_hardware_watchpoint
5044                             || other_b->type == bp_access_watchpoint)
5045                           {
5046                             struct watchpoint *other_w =
5047                               (struct watchpoint *) other_b;
5048
5049                             if (other_w->watchpoint_triggered
5050                                 == watch_triggered_yes)
5051                               {
5052                                 other_write_watchpoint = 1;
5053                                 break;
5054                               }
5055                           }
5056                     }
5057
5058                   if (other_write_watchpoint
5059                       || bl->watchpoint_type == hw_access)
5060                     {
5061                       /* We're watching the same memory for writes,
5062                          and the value changed since the last time we
5063                          updated it, so this trap must be for a write.
5064                          Ignore it.  */
5065                       bs->print_it = print_it_noop;
5066                       bs->stop = 0;
5067                     }
5068                 }
5069               break;
5070             case WP_VALUE_NOT_CHANGED:
5071               if (b->base.type == bp_hardware_watchpoint
5072                   || b->base.type == bp_watchpoint)
5073                 {
5074                   /* Don't stop: write watchpoints shouldn't fire if
5075                      the value hasn't changed.  */
5076                   bs->print_it = print_it_noop;
5077                   bs->stop = 0;
5078                 }
5079               /* Stop.  */
5080               break;
5081             default:
5082               /* Can't happen.  */
5083             case 0:
5084               /* Error from catch_errors.  */
5085               printf_filtered (_("Watchpoint %d deleted.\n"), b->base.number);
5086               watchpoint_del_at_next_stop (b);
5087               /* We've already printed what needs to be printed.  */
5088               bs->print_it = print_it_done;
5089               break;
5090             }
5091         }
5092       else      /* must_check_value == 0 */
5093         {
5094           /* This is a case where some watchpoint(s) triggered, but
5095              not at the address of this watchpoint, or else no
5096              watchpoint triggered after all.  So don't print
5097              anything for this watchpoint.  */
5098           bs->print_it = print_it_noop;
5099           bs->stop = 0;
5100         }
5101     }
5102 }
5103
5104 /* For breakpoints that are currently marked as telling gdb to stop,
5105    check conditions (condition proper, frame, thread and ignore count)
5106    of breakpoint referred to by BS.  If we should not stop for this
5107    breakpoint, set BS->stop to 0.  */
5108
5109 static void
5110 bpstat_check_breakpoint_conditions (bpstat bs, ptid_t ptid)
5111 {
5112   int thread_id = pid_to_thread_id (ptid);
5113   const struct bp_location *bl;
5114   struct breakpoint *b;
5115   int value_is_zero = 0;
5116   struct expression *cond;
5117
5118   gdb_assert (bs->stop);
5119
5120   /* BS is built for existing struct breakpoint.  */
5121   bl = bs->bp_location_at;
5122   gdb_assert (bl != NULL);
5123   b = bs->breakpoint_at;
5124   gdb_assert (b != NULL);
5125
5126   /* Even if the target evaluated the condition on its end and notified GDB, we
5127      need to do so again since GDB does not know if we stopped due to a
5128      breakpoint or a single step breakpoint.  */
5129
5130   if (frame_id_p (b->frame_id)
5131       && !frame_id_eq (b->frame_id, get_stack_frame_id (get_current_frame ())))
5132     {
5133       bs->stop = 0;
5134       return;
5135     }
5136
5137   /* If this is a thread-specific breakpoint, don't waste cpu evaluating the
5138      condition if this isn't the specified thread.  */
5139   if (b->thread != -1 && b->thread != thread_id)
5140     {
5141       bs->stop = 0;
5142       return;
5143     }
5144
5145   /* Evaluate Python breakpoints that have a "stop" method implemented.  */
5146   if (b->py_bp_object)
5147     bs->stop = gdbpy_should_stop (b->py_bp_object);
5148
5149   if (is_watchpoint (b))
5150     {
5151       struct watchpoint *w = (struct watchpoint *) b;
5152
5153       cond = w->cond_exp;
5154     }
5155   else
5156     cond = bl->cond;
5157
5158   if (cond && b->disposition != disp_del_at_next_stop)
5159     {
5160       int within_current_scope = 1;
5161       struct watchpoint * w;
5162
5163       /* We use value_mark and value_free_to_mark because it could
5164          be a long time before we return to the command level and
5165          call free_all_values.  We can't call free_all_values
5166          because we might be in the middle of evaluating a
5167          function call.  */
5168       struct value *mark = value_mark ();
5169
5170       if (is_watchpoint (b))
5171         w = (struct watchpoint *) b;
5172       else
5173         w = NULL;
5174
5175       /* Need to select the frame, with all that implies so that
5176          the conditions will have the right context.  Because we
5177          use the frame, we will not see an inlined function's
5178          variables when we arrive at a breakpoint at the start
5179          of the inlined function; the current frame will be the
5180          call site.  */
5181       if (w == NULL || w->cond_exp_valid_block == NULL)
5182         select_frame (get_current_frame ());
5183       else
5184         {
5185           struct frame_info *frame;
5186
5187           /* For local watchpoint expressions, which particular
5188              instance of a local is being watched matters, so we
5189              keep track of the frame to evaluate the expression
5190              in.  To evaluate the condition however, it doesn't
5191              really matter which instantiation of the function
5192              where the condition makes sense triggers the
5193              watchpoint.  This allows an expression like "watch
5194              global if q > 10" set in `func', catch writes to
5195              global on all threads that call `func', or catch
5196              writes on all recursive calls of `func' by a single
5197              thread.  We simply always evaluate the condition in
5198              the innermost frame that's executing where it makes
5199              sense to evaluate the condition.  It seems
5200              intuitive.  */
5201           frame = block_innermost_frame (w->cond_exp_valid_block);
5202           if (frame != NULL)
5203             select_frame (frame);
5204           else
5205             within_current_scope = 0;
5206         }
5207       if (within_current_scope)
5208         value_is_zero
5209           = catch_errors (breakpoint_cond_eval, cond,
5210                           "Error in testing breakpoint condition:\n",
5211                           RETURN_MASK_ALL);
5212       else
5213         {
5214           warning (_("Watchpoint condition cannot be tested "
5215                      "in the current scope"));
5216           /* If we failed to set the right context for this
5217              watchpoint, unconditionally report it.  */
5218           value_is_zero = 0;
5219         }
5220       /* FIXME-someday, should give breakpoint #.  */
5221       value_free_to_mark (mark);
5222     }
5223
5224   if (cond && value_is_zero)
5225     {
5226       bs->stop = 0;
5227     }
5228   else if (b->ignore_count > 0)
5229     {
5230       b->ignore_count--;
5231       bs->stop = 0;
5232       /* Increase the hit count even though we don't stop.  */
5233       ++(b->hit_count);
5234       observer_notify_breakpoint_modified (b);
5235     }   
5236 }
5237
5238
5239 /* Get a bpstat associated with having just stopped at address
5240    BP_ADDR in thread PTID.
5241
5242    Determine whether we stopped at a breakpoint, etc, or whether we
5243    don't understand this stop.  Result is a chain of bpstat's such
5244    that:
5245
5246    if we don't understand the stop, the result is a null pointer.
5247
5248    if we understand why we stopped, the result is not null.
5249
5250    Each element of the chain refers to a particular breakpoint or
5251    watchpoint at which we have stopped.  (We may have stopped for
5252    several reasons concurrently.)
5253
5254    Each element of the chain has valid next, breakpoint_at,
5255    commands, FIXME??? fields.  */
5256
5257 bpstat
5258 bpstat_stop_status (struct address_space *aspace,
5259                     CORE_ADDR bp_addr, ptid_t ptid,
5260                     const struct target_waitstatus *ws)
5261 {
5262   struct breakpoint *b = NULL;
5263   struct bp_location *bl;
5264   struct bp_location *loc;
5265   /* First item of allocated bpstat's.  */
5266   bpstat bs_head = NULL, *bs_link = &bs_head;
5267   /* Pointer to the last thing in the chain currently.  */
5268   bpstat bs;
5269   int ix;
5270   int need_remove_insert;
5271   int removed_any;
5272
5273   /* First, build the bpstat chain with locations that explain a
5274      target stop, while being careful to not set the target running,
5275      as that may invalidate locations (in particular watchpoint
5276      locations are recreated).  Resuming will happen here with
5277      breakpoint conditions or watchpoint expressions that include
5278      inferior function calls.  */
5279
5280   ALL_BREAKPOINTS (b)
5281     {
5282       if (!breakpoint_enabled (b) && b->enable_state != bp_permanent)
5283         continue;
5284
5285       for (bl = b->loc; bl != NULL; bl = bl->next)
5286         {
5287           /* For hardware watchpoints, we look only at the first
5288              location.  The watchpoint_check function will work on the
5289              entire expression, not the individual locations.  For
5290              read watchpoints, the watchpoints_triggered function has
5291              checked all locations already.  */
5292           if (b->type == bp_hardware_watchpoint && bl != b->loc)
5293             break;
5294
5295           if (!bl->enabled || bl->shlib_disabled)
5296             continue;
5297
5298           if (!bpstat_check_location (bl, aspace, bp_addr, ws))
5299             continue;
5300
5301           /* Come here if it's a watchpoint, or if the break address
5302              matches.  */
5303
5304           bs = bpstat_alloc (bl, &bs_link);     /* Alloc a bpstat to
5305                                                    explain stop.  */
5306
5307           /* Assume we stop.  Should we find a watchpoint that is not
5308              actually triggered, or if the condition of the breakpoint
5309              evaluates as false, we'll reset 'stop' to 0.  */
5310           bs->stop = 1;
5311           bs->print = 1;
5312
5313           /* If this is a scope breakpoint, mark the associated
5314              watchpoint as triggered so that we will handle the
5315              out-of-scope event.  We'll get to the watchpoint next
5316              iteration.  */
5317           if (b->type == bp_watchpoint_scope && b->related_breakpoint != b)
5318             {
5319               struct watchpoint *w = (struct watchpoint *) b->related_breakpoint;
5320
5321               w->watchpoint_triggered = watch_triggered_yes;
5322             }
5323         }
5324     }
5325
5326   for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, loc); ++ix)
5327     {
5328       if (breakpoint_location_address_match (loc, aspace, bp_addr))
5329         {
5330           bs = bpstat_alloc (loc, &bs_link);
5331           /* For hits of moribund locations, we should just proceed.  */
5332           bs->stop = 0;
5333           bs->print = 0;
5334           bs->print_it = print_it_noop;
5335         }
5336     }
5337
5338   /* A bit of special processing for shlib breakpoints.  We need to
5339      process solib loading here, so that the lists of loaded and
5340      unloaded libraries are correct before we handle "catch load" and
5341      "catch unload".  */
5342   for (bs = bs_head; bs != NULL; bs = bs->next)
5343     {
5344       if (bs->breakpoint_at && bs->breakpoint_at->type == bp_shlib_event)
5345         {
5346           handle_solib_event ();
5347           break;
5348         }
5349     }
5350
5351   /* Now go through the locations that caused the target to stop, and
5352      check whether we're interested in reporting this stop to higher
5353      layers, or whether we should resume the target transparently.  */
5354
5355   removed_any = 0;
5356
5357   for (bs = bs_head; bs != NULL; bs = bs->next)
5358     {
5359       if (!bs->stop)
5360         continue;
5361
5362       b = bs->breakpoint_at;
5363       b->ops->check_status (bs);
5364       if (bs->stop)
5365         {
5366           bpstat_check_breakpoint_conditions (bs, ptid);
5367
5368           if (bs->stop)
5369             {
5370               ++(b->hit_count);
5371               observer_notify_breakpoint_modified (b);
5372
5373               /* We will stop here.  */
5374               if (b->disposition == disp_disable)
5375                 {
5376                   --(b->enable_count);
5377                   if (b->enable_count <= 0
5378                       && b->enable_state != bp_permanent)
5379                     b->enable_state = bp_disabled;
5380                   removed_any = 1;
5381                 }
5382               if (b->silent)
5383                 bs->print = 0;
5384               bs->commands = b->commands;
5385               incref_counted_command_line (bs->commands);
5386               if (command_line_is_silent (bs->commands
5387                                           ? bs->commands->commands : NULL))
5388                 bs->print = 0;
5389
5390               b->ops->after_condition_true (bs);
5391             }
5392
5393         }
5394
5395       /* Print nothing for this entry if we don't stop or don't
5396          print.  */
5397       if (!bs->stop || !bs->print)
5398         bs->print_it = print_it_noop;
5399     }
5400
5401   /* If we aren't stopping, the value of some hardware watchpoint may
5402      not have changed, but the intermediate memory locations we are
5403      watching may have.  Don't bother if we're stopping; this will get
5404      done later.  */
5405   need_remove_insert = 0;
5406   if (! bpstat_causes_stop (bs_head))
5407     for (bs = bs_head; bs != NULL; bs = bs->next)
5408       if (!bs->stop
5409           && bs->breakpoint_at
5410           && is_hardware_watchpoint (bs->breakpoint_at))
5411         {
5412           struct watchpoint *w = (struct watchpoint *) bs->breakpoint_at;
5413
5414           update_watchpoint (w, 0 /* don't reparse.  */);
5415           need_remove_insert = 1;
5416         }
5417
5418   if (need_remove_insert)
5419     update_global_location_list (1);
5420   else if (removed_any)
5421     update_global_location_list (0);
5422
5423   return bs_head;
5424 }
5425
5426 static void
5427 handle_jit_event (void)
5428 {
5429   struct frame_info *frame;
5430   struct gdbarch *gdbarch;
5431
5432   /* Switch terminal for any messages produced by
5433      breakpoint_re_set.  */
5434   target_terminal_ours_for_output ();
5435
5436   frame = get_current_frame ();
5437   gdbarch = get_frame_arch (frame);
5438
5439   jit_event_handler (gdbarch);
5440
5441   target_terminal_inferior ();
5442 }
5443
5444 /* Prepare WHAT final decision for infrun.  */
5445
5446 /* Decide what infrun needs to do with this bpstat.  */
5447
5448 struct bpstat_what
5449 bpstat_what (bpstat bs_head)
5450 {
5451   struct bpstat_what retval;
5452   int jit_event = 0;
5453   bpstat bs;
5454
5455   retval.main_action = BPSTAT_WHAT_KEEP_CHECKING;
5456   retval.call_dummy = STOP_NONE;
5457   retval.is_longjmp = 0;
5458
5459   for (bs = bs_head; bs != NULL; bs = bs->next)
5460     {
5461       /* Extract this BS's action.  After processing each BS, we check
5462          if its action overrides all we've seem so far.  */
5463       enum bpstat_what_main_action this_action = BPSTAT_WHAT_KEEP_CHECKING;
5464       enum bptype bptype;
5465
5466       if (bs->breakpoint_at == NULL)
5467         {
5468           /* I suspect this can happen if it was a momentary
5469              breakpoint which has since been deleted.  */
5470           bptype = bp_none;
5471         }
5472       else
5473         bptype = bs->breakpoint_at->type;
5474
5475       switch (bptype)
5476         {
5477         case bp_none:
5478           break;
5479         case bp_breakpoint:
5480         case bp_hardware_breakpoint:
5481         case bp_until:
5482         case bp_finish:
5483         case bp_shlib_event:
5484           if (bs->stop)
5485             {
5486               if (bs->print)
5487                 this_action = BPSTAT_WHAT_STOP_NOISY;
5488               else
5489                 this_action = BPSTAT_WHAT_STOP_SILENT;
5490             }
5491           else
5492             this_action = BPSTAT_WHAT_SINGLE;
5493           break;
5494         case bp_watchpoint:
5495         case bp_hardware_watchpoint:
5496         case bp_read_watchpoint:
5497         case bp_access_watchpoint:
5498           if (bs->stop)
5499             {
5500               if (bs->print)
5501                 this_action = BPSTAT_WHAT_STOP_NOISY;
5502               else
5503                 this_action = BPSTAT_WHAT_STOP_SILENT;
5504             }
5505           else
5506             {
5507               /* There was a watchpoint, but we're not stopping.
5508                  This requires no further action.  */
5509             }
5510           break;
5511         case bp_longjmp:
5512         case bp_longjmp_call_dummy:
5513         case bp_exception:
5514           this_action = BPSTAT_WHAT_SET_LONGJMP_RESUME;
5515           retval.is_longjmp = bptype != bp_exception;
5516           break;
5517         case bp_longjmp_resume:
5518         case bp_exception_resume:
5519           this_action = BPSTAT_WHAT_CLEAR_LONGJMP_RESUME;
5520           retval.is_longjmp = bptype == bp_longjmp_resume;
5521           break;
5522         case bp_step_resume:
5523           if (bs->stop)
5524             this_action = BPSTAT_WHAT_STEP_RESUME;
5525           else
5526             {
5527               /* It is for the wrong frame.  */
5528               this_action = BPSTAT_WHAT_SINGLE;
5529             }
5530           break;
5531         case bp_hp_step_resume:
5532           if (bs->stop)
5533             this_action = BPSTAT_WHAT_HP_STEP_RESUME;
5534           else
5535             {
5536               /* It is for the wrong frame.  */
5537               this_action = BPSTAT_WHAT_SINGLE;
5538             }
5539           break;
5540         case bp_watchpoint_scope:
5541         case bp_thread_event:
5542         case bp_overlay_event:
5543         case bp_longjmp_master:
5544         case bp_std_terminate_master:
5545         case bp_exception_master:
5546           this_action = BPSTAT_WHAT_SINGLE;
5547           break;
5548         case bp_catchpoint:
5549           if (bs->stop)
5550             {
5551               if (bs->print)
5552                 this_action = BPSTAT_WHAT_STOP_NOISY;
5553               else
5554                 this_action = BPSTAT_WHAT_STOP_SILENT;
5555             }
5556           else
5557             {
5558               /* There was a catchpoint, but we're not stopping.
5559                  This requires no further action.  */
5560             }
5561           break;
5562         case bp_jit_event:
5563           jit_event = 1;
5564           this_action = BPSTAT_WHAT_SINGLE;
5565           break;
5566         case bp_call_dummy:
5567           /* Make sure the action is stop (silent or noisy),
5568              so infrun.c pops the dummy frame.  */
5569           retval.call_dummy = STOP_STACK_DUMMY;
5570           this_action = BPSTAT_WHAT_STOP_SILENT;
5571           break;
5572         case bp_std_terminate:
5573           /* Make sure the action is stop (silent or noisy),
5574              so infrun.c pops the dummy frame.  */
5575           retval.call_dummy = STOP_STD_TERMINATE;
5576           this_action = BPSTAT_WHAT_STOP_SILENT;
5577           break;
5578         case bp_tracepoint:
5579         case bp_fast_tracepoint:
5580         case bp_static_tracepoint:
5581           /* Tracepoint hits should not be reported back to GDB, and
5582              if one got through somehow, it should have been filtered
5583              out already.  */
5584           internal_error (__FILE__, __LINE__,
5585                           _("bpstat_what: tracepoint encountered"));
5586           break;
5587         case bp_gnu_ifunc_resolver:
5588           /* Step over it (and insert bp_gnu_ifunc_resolver_return).  */
5589           this_action = BPSTAT_WHAT_SINGLE;
5590           break;
5591         case bp_gnu_ifunc_resolver_return:
5592           /* The breakpoint will be removed, execution will restart from the
5593              PC of the former breakpoint.  */
5594           this_action = BPSTAT_WHAT_KEEP_CHECKING;
5595           break;
5596
5597         case bp_dprintf:
5598           if (bs->stop)
5599             this_action = BPSTAT_WHAT_STOP_SILENT;
5600           else
5601             this_action = BPSTAT_WHAT_SINGLE;
5602           break;
5603
5604         default:
5605           internal_error (__FILE__, __LINE__,
5606                           _("bpstat_what: unhandled bptype %d"), (int) bptype);
5607         }
5608
5609       retval.main_action = max (retval.main_action, this_action);
5610     }
5611
5612   /* These operations may affect the bs->breakpoint_at state so they are
5613      delayed after MAIN_ACTION is decided above.  */
5614
5615   if (jit_event)
5616     {
5617       if (debug_infrun)
5618         fprintf_unfiltered (gdb_stdlog, "bpstat_what: bp_jit_event\n");
5619
5620       handle_jit_event ();
5621     }
5622
5623   for (bs = bs_head; bs != NULL; bs = bs->next)
5624     {
5625       struct breakpoint *b = bs->breakpoint_at;
5626
5627       if (b == NULL)
5628         continue;
5629       switch (b->type)
5630         {
5631         case bp_gnu_ifunc_resolver:
5632           gnu_ifunc_resolver_stop (b);
5633           break;
5634         case bp_gnu_ifunc_resolver_return:
5635           gnu_ifunc_resolver_return_stop (b);
5636           break;
5637         }
5638     }
5639
5640   return retval;
5641 }
5642
5643 /* Nonzero if we should step constantly (e.g. watchpoints on machines
5644    without hardware support).  This isn't related to a specific bpstat,
5645    just to things like whether watchpoints are set.  */
5646
5647 int
5648 bpstat_should_step (void)
5649 {
5650   struct breakpoint *b;
5651
5652   ALL_BREAKPOINTS (b)
5653     if (breakpoint_enabled (b) && b->type == bp_watchpoint && b->loc != NULL)
5654       return 1;
5655   return 0;
5656 }
5657
5658 int
5659 bpstat_causes_stop (bpstat bs)
5660 {
5661   for (; bs != NULL; bs = bs->next)
5662     if (bs->stop)
5663       return 1;
5664
5665   return 0;
5666 }
5667
5668 \f
5669
5670 /* Compute a string of spaces suitable to indent the next line
5671    so it starts at the position corresponding to the table column
5672    named COL_NAME in the currently active table of UIOUT.  */
5673
5674 static char *
5675 wrap_indent_at_field (struct ui_out *uiout, const char *col_name)
5676 {
5677   static char wrap_indent[80];
5678   int i, total_width, width, align;
5679   char *text;
5680
5681   total_width = 0;
5682   for (i = 1; ui_out_query_field (uiout, i, &width, &align, &text); i++)
5683     {
5684       if (strcmp (text, col_name) == 0)
5685         {
5686           gdb_assert (total_width < sizeof wrap_indent);
5687           memset (wrap_indent, ' ', total_width);
5688           wrap_indent[total_width] = 0;
5689
5690           return wrap_indent;
5691         }
5692
5693       total_width += width + 1;
5694     }
5695
5696   return NULL;
5697 }
5698
5699 /* Determine if the locations of this breakpoint will have their conditions
5700    evaluated by the target, host or a mix of both.  Returns the following:
5701
5702     "host": Host evals condition.
5703     "host or target": Host or Target evals condition.
5704     "target": Target evals condition.
5705 */
5706
5707 static const char *
5708 bp_condition_evaluator (struct breakpoint *b)
5709 {
5710   struct bp_location *bl;
5711   char host_evals = 0;
5712   char target_evals = 0;
5713
5714   if (!b)
5715     return NULL;
5716
5717   if (!is_breakpoint (b))
5718     return NULL;
5719
5720   if (gdb_evaluates_breakpoint_condition_p ()
5721       || !target_supports_evaluation_of_breakpoint_conditions ())
5722     return condition_evaluation_host;
5723
5724   for (bl = b->loc; bl; bl = bl->next)
5725     {
5726       if (bl->cond_bytecode)
5727         target_evals++;
5728       else
5729         host_evals++;
5730     }
5731
5732   if (host_evals && target_evals)
5733     return condition_evaluation_both;
5734   else if (target_evals)
5735     return condition_evaluation_target;
5736   else
5737     return condition_evaluation_host;
5738 }
5739
5740 /* Determine the breakpoint location's condition evaluator.  This is
5741    similar to bp_condition_evaluator, but for locations.  */
5742
5743 static const char *
5744 bp_location_condition_evaluator (struct bp_location *bl)
5745 {
5746   if (bl && !is_breakpoint (bl->owner))
5747     return NULL;
5748
5749   if (gdb_evaluates_breakpoint_condition_p ()
5750       || !target_supports_evaluation_of_breakpoint_conditions ())
5751     return condition_evaluation_host;
5752
5753   if (bl && bl->cond_bytecode)
5754     return condition_evaluation_target;
5755   else
5756     return condition_evaluation_host;
5757 }
5758
5759 /* Print the LOC location out of the list of B->LOC locations.  */
5760
5761 static void
5762 print_breakpoint_location (struct breakpoint *b,
5763                            struct bp_location *loc)
5764 {
5765   struct ui_out *uiout = current_uiout;
5766   struct cleanup *old_chain = save_current_program_space ();
5767
5768   if (loc != NULL && loc->shlib_disabled)
5769     loc = NULL;
5770
5771   if (loc != NULL)
5772     set_current_program_space (loc->pspace);
5773
5774   if (b->display_canonical)
5775     ui_out_field_string (uiout, "what", b->addr_string);
5776   else if (loc && loc->symtab)
5777     {
5778       struct symbol *sym 
5779         = find_pc_sect_function (loc->address, loc->section);
5780       if (sym)
5781         {
5782           ui_out_text (uiout, "in ");
5783           ui_out_field_string (uiout, "func",
5784                                SYMBOL_PRINT_NAME (sym));
5785           ui_out_text (uiout, " ");
5786           ui_out_wrap_hint (uiout, wrap_indent_at_field (uiout, "what"));
5787           ui_out_text (uiout, "at ");
5788         }
5789       ui_out_field_string (uiout, "file",
5790                            symtab_to_filename_for_display (loc->symtab));
5791       ui_out_text (uiout, ":");
5792
5793       if (ui_out_is_mi_like_p (uiout))
5794         ui_out_field_string (uiout, "fullname",
5795                              symtab_to_fullname (loc->symtab));
5796       
5797       ui_out_field_int (uiout, "line", loc->line_number);
5798     }
5799   else if (loc)
5800     {
5801       struct ui_file *stb = mem_fileopen ();
5802       struct cleanup *stb_chain = make_cleanup_ui_file_delete (stb);
5803
5804       print_address_symbolic (loc->gdbarch, loc->address, stb,
5805                               demangle, "");
5806       ui_out_field_stream (uiout, "at", stb);
5807
5808       do_cleanups (stb_chain);
5809     }
5810   else
5811     ui_out_field_string (uiout, "pending", b->addr_string);
5812
5813   if (loc && is_breakpoint (b)
5814       && breakpoint_condition_evaluation_mode () == condition_evaluation_target
5815       && bp_condition_evaluator (b) == condition_evaluation_both)
5816     {
5817       ui_out_text (uiout, " (");
5818       ui_out_field_string (uiout, "evaluated-by",
5819                            bp_location_condition_evaluator (loc));
5820       ui_out_text (uiout, ")");
5821     }
5822
5823   do_cleanups (old_chain);
5824 }
5825
5826 static const char *
5827 bptype_string (enum bptype type)
5828 {
5829   struct ep_type_description
5830     {
5831       enum bptype type;
5832       char *description;
5833     };
5834   static struct ep_type_description bptypes[] =
5835   {
5836     {bp_none, "?deleted?"},
5837     {bp_breakpoint, "breakpoint"},
5838     {bp_hardware_breakpoint, "hw breakpoint"},
5839     {bp_until, "until"},
5840     {bp_finish, "finish"},
5841     {bp_watchpoint, "watchpoint"},
5842     {bp_hardware_watchpoint, "hw watchpoint"},
5843     {bp_read_watchpoint, "read watchpoint"},
5844     {bp_access_watchpoint, "acc watchpoint"},
5845     {bp_longjmp, "longjmp"},
5846     {bp_longjmp_resume, "longjmp resume"},
5847     {bp_longjmp_call_dummy, "longjmp for call dummy"},
5848     {bp_exception, "exception"},
5849     {bp_exception_resume, "exception resume"},
5850     {bp_step_resume, "step resume"},
5851     {bp_hp_step_resume, "high-priority step resume"},
5852     {bp_watchpoint_scope, "watchpoint scope"},
5853     {bp_call_dummy, "call dummy"},
5854     {bp_std_terminate, "std::terminate"},
5855     {bp_shlib_event, "shlib events"},
5856     {bp_thread_event, "thread events"},
5857     {bp_overlay_event, "overlay events"},
5858     {bp_longjmp_master, "longjmp master"},
5859     {bp_std_terminate_master, "std::terminate master"},
5860     {bp_exception_master, "exception master"},
5861     {bp_catchpoint, "catchpoint"},
5862     {bp_tracepoint, "tracepoint"},
5863     {bp_fast_tracepoint, "fast tracepoint"},
5864     {bp_static_tracepoint, "static tracepoint"},
5865     {bp_dprintf, "dprintf"},
5866     {bp_jit_event, "jit events"},
5867     {bp_gnu_ifunc_resolver, "STT_GNU_IFUNC resolver"},
5868     {bp_gnu_ifunc_resolver_return, "STT_GNU_IFUNC resolver return"},
5869   };
5870
5871   if (((int) type >= (sizeof (bptypes) / sizeof (bptypes[0])))
5872       || ((int) type != bptypes[(int) type].type))
5873     internal_error (__FILE__, __LINE__,
5874                     _("bptypes table does not describe type #%d."),
5875                     (int) type);
5876
5877   return bptypes[(int) type].description;
5878 }
5879
5880 /* For MI, output a field named 'thread-groups' with a list as the value.
5881    For CLI, prefix the list with the string 'inf'. */
5882
5883 static void
5884 output_thread_groups (struct ui_out *uiout,
5885                       const char *field_name,
5886                       VEC(int) *inf_num,
5887                       int mi_only)
5888 {
5889   struct cleanup *back_to;
5890   int is_mi = ui_out_is_mi_like_p (uiout);
5891   int inf;
5892   int i;
5893
5894   /* For backward compatibility, don't display inferiors in CLI unless
5895      there are several.  Always display them for MI. */
5896   if (!is_mi && mi_only)
5897     return;
5898
5899   back_to = make_cleanup_ui_out_list_begin_end (uiout, field_name);
5900
5901   for (i = 0; VEC_iterate (int, inf_num, i, inf); ++i)
5902     {
5903       if (is_mi)
5904         {
5905           char mi_group[10];
5906
5907           xsnprintf (mi_group, sizeof (mi_group), "i%d", inf);
5908           ui_out_field_string (uiout, NULL, mi_group);
5909         }
5910       else
5911         {
5912           if (i == 0)
5913             ui_out_text (uiout, " inf ");
5914           else
5915             ui_out_text (uiout, ", ");
5916         
5917           ui_out_text (uiout, plongest (inf));
5918         }
5919     }
5920
5921   do_cleanups (back_to);
5922 }
5923
5924 /* Print B to gdb_stdout.  */
5925
5926 static void
5927 print_one_breakpoint_location (struct breakpoint *b,
5928                                struct bp_location *loc,
5929                                int loc_number,
5930                                struct bp_location **last_loc,
5931                                int allflag)
5932 {
5933   struct command_line *l;
5934   static char bpenables[] = "nynny";
5935
5936   struct ui_out *uiout = current_uiout;
5937   int header_of_multiple = 0;
5938   int part_of_multiple = (loc != NULL);
5939   struct value_print_options opts;
5940
5941   get_user_print_options (&opts);
5942
5943   gdb_assert (!loc || loc_number != 0);
5944   /* See comment in print_one_breakpoint concerning treatment of
5945      breakpoints with single disabled location.  */
5946   if (loc == NULL 
5947       && (b->loc != NULL 
5948           && (b->loc->next != NULL || !b->loc->enabled)))
5949     header_of_multiple = 1;
5950   if (loc == NULL)
5951     loc = b->loc;
5952
5953   annotate_record ();
5954
5955   /* 1 */
5956   annotate_field (0);
5957   if (part_of_multiple)
5958     {
5959       char *formatted;
5960       formatted = xstrprintf ("%d.%d", b->number, loc_number);
5961       ui_out_field_string (uiout, "number", formatted);
5962       xfree (formatted);
5963     }
5964   else
5965     {
5966       ui_out_field_int (uiout, "number", b->number);
5967     }
5968
5969   /* 2 */
5970   annotate_field (1);
5971   if (part_of_multiple)
5972     ui_out_field_skip (uiout, "type");
5973   else
5974     ui_out_field_string (uiout, "type", bptype_string (b->type));
5975
5976   /* 3 */
5977   annotate_field (2);
5978   if (part_of_multiple)
5979     ui_out_field_skip (uiout, "disp");
5980   else
5981     ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
5982
5983
5984   /* 4 */
5985   annotate_field (3);
5986   if (part_of_multiple)
5987     ui_out_field_string (uiout, "enabled", loc->enabled ? "y" : "n");
5988   else
5989     ui_out_field_fmt (uiout, "enabled", "%c", 
5990                       bpenables[(int) b->enable_state]);
5991   ui_out_spaces (uiout, 2);
5992
5993   
5994   /* 5 and 6 */
5995   if (b->ops != NULL && b->ops->print_one != NULL)
5996     {
5997       /* Although the print_one can possibly print all locations,
5998          calling it here is not likely to get any nice result.  So,
5999          make sure there's just one location.  */
6000       gdb_assert (b->loc == NULL || b->loc->next == NULL);
6001       b->ops->print_one (b, last_loc);
6002     }
6003   else
6004     switch (b->type)
6005       {
6006       case bp_none:
6007         internal_error (__FILE__, __LINE__,
6008                         _("print_one_breakpoint: bp_none encountered\n"));
6009         break;
6010
6011       case bp_watchpoint:
6012       case bp_hardware_watchpoint:
6013       case bp_read_watchpoint:
6014       case bp_access_watchpoint:
6015         {
6016           struct watchpoint *w = (struct watchpoint *) b;
6017
6018           /* Field 4, the address, is omitted (which makes the columns
6019              not line up too nicely with the headers, but the effect
6020              is relatively readable).  */
6021           if (opts.addressprint)
6022             ui_out_field_skip (uiout, "addr");
6023           annotate_field (5);
6024           ui_out_field_string (uiout, "what", w->exp_string);
6025         }
6026         break;
6027
6028       case bp_breakpoint:
6029       case bp_hardware_breakpoint:
6030       case bp_until:
6031       case bp_finish:
6032       case bp_longjmp:
6033       case bp_longjmp_resume:
6034       case bp_longjmp_call_dummy:
6035       case bp_exception:
6036       case bp_exception_resume:
6037       case bp_step_resume:
6038       case bp_hp_step_resume:
6039       case bp_watchpoint_scope:
6040       case bp_call_dummy:
6041       case bp_std_terminate:
6042       case bp_shlib_event:
6043       case bp_thread_event:
6044       case bp_overlay_event:
6045       case bp_longjmp_master:
6046       case bp_std_terminate_master:
6047       case bp_exception_master:
6048       case bp_tracepoint:
6049       case bp_fast_tracepoint:
6050       case bp_static_tracepoint:
6051       case bp_dprintf:
6052       case bp_jit_event:
6053       case bp_gnu_ifunc_resolver:
6054       case bp_gnu_ifunc_resolver_return:
6055         if (opts.addressprint)
6056           {
6057             annotate_field (4);
6058             if (header_of_multiple)
6059               ui_out_field_string (uiout, "addr", "<MULTIPLE>");
6060             else if (b->loc == NULL || loc->shlib_disabled)
6061               ui_out_field_string (uiout, "addr", "<PENDING>");
6062             else
6063               ui_out_field_core_addr (uiout, "addr",
6064                                       loc->gdbarch, loc->address);
6065           }
6066         annotate_field (5);
6067         if (!header_of_multiple)
6068           print_breakpoint_location (b, loc);
6069         if (b->loc)
6070           *last_loc = b->loc;
6071         break;
6072       }
6073
6074
6075   if (loc != NULL && !header_of_multiple)
6076     {
6077       struct inferior *inf;
6078       VEC(int) *inf_num = NULL;
6079       int mi_only = 1;
6080
6081       ALL_INFERIORS (inf)
6082         {
6083           if (inf->pspace == loc->pspace)
6084             VEC_safe_push (int, inf_num, inf->num);
6085         }
6086
6087         /* For backward compatibility, don't display inferiors in CLI unless
6088            there are several.  Always display for MI. */
6089         if (allflag
6090             || (!gdbarch_has_global_breakpoints (target_gdbarch ())
6091                 && (number_of_program_spaces () > 1
6092                     || number_of_inferiors () > 1)
6093                 /* LOC is for existing B, it cannot be in
6094                    moribund_locations and thus having NULL OWNER.  */
6095                 && loc->owner->type != bp_catchpoint))
6096         mi_only = 0;
6097       output_thread_groups (uiout, "thread-groups", inf_num, mi_only);
6098       VEC_free (int, inf_num);
6099     }
6100
6101   if (!part_of_multiple)
6102     {
6103       if (b->thread != -1)
6104         {
6105           /* FIXME: This seems to be redundant and lost here; see the
6106              "stop only in" line a little further down.  */
6107           ui_out_text (uiout, " thread ");
6108           ui_out_field_int (uiout, "thread", b->thread);
6109         }
6110       else if (b->task != 0)
6111         {
6112           ui_out_text (uiout, " task ");
6113           ui_out_field_int (uiout, "task", b->task);
6114         }
6115     }
6116
6117   ui_out_text (uiout, "\n");
6118
6119   if (!part_of_multiple)
6120     b->ops->print_one_detail (b, uiout);
6121
6122   if (part_of_multiple && frame_id_p (b->frame_id))
6123     {
6124       annotate_field (6);
6125       ui_out_text (uiout, "\tstop only in stack frame at ");
6126       /* FIXME: cagney/2002-12-01: Shouldn't be poking around inside
6127          the frame ID.  */
6128       ui_out_field_core_addr (uiout, "frame",
6129                               b->gdbarch, b->frame_id.stack_addr);
6130       ui_out_text (uiout, "\n");
6131     }
6132   
6133   if (!part_of_multiple && b->cond_string)
6134     {
6135       annotate_field (7);
6136       if (is_tracepoint (b))
6137         ui_out_text (uiout, "\ttrace only if ");
6138       else
6139         ui_out_text (uiout, "\tstop only if ");
6140       ui_out_field_string (uiout, "cond", b->cond_string);
6141
6142       /* Print whether the target is doing the breakpoint's condition
6143          evaluation.  If GDB is doing the evaluation, don't print anything.  */
6144       if (is_breakpoint (b)
6145           && breakpoint_condition_evaluation_mode ()
6146           == condition_evaluation_target)
6147         {
6148           ui_out_text (uiout, " (");
6149           ui_out_field_string (uiout, "evaluated-by",
6150                                bp_condition_evaluator (b));
6151           ui_out_text (uiout, " evals)");
6152         }
6153       ui_out_text (uiout, "\n");
6154     }
6155
6156   if (!part_of_multiple && b->thread != -1)
6157     {
6158       /* FIXME should make an annotation for this.  */
6159       ui_out_text (uiout, "\tstop only in thread ");
6160       ui_out_field_int (uiout, "thread", b->thread);
6161       ui_out_text (uiout, "\n");
6162     }
6163   
6164   if (!part_of_multiple)
6165     {
6166       if (b->hit_count)
6167         {
6168           /* FIXME should make an annotation for this.  */
6169           if (is_catchpoint (b))
6170             ui_out_text (uiout, "\tcatchpoint");
6171           else if (is_tracepoint (b))
6172             ui_out_text (uiout, "\ttracepoint");
6173           else
6174             ui_out_text (uiout, "\tbreakpoint");
6175           ui_out_text (uiout, " already hit ");
6176           ui_out_field_int (uiout, "times", b->hit_count);
6177           if (b->hit_count == 1)
6178             ui_out_text (uiout, " time\n");
6179           else
6180             ui_out_text (uiout, " times\n");
6181         }
6182       else
6183         {
6184           /* Output the count also if it is zero, but only if this is mi.  */
6185           if (ui_out_is_mi_like_p (uiout))
6186             ui_out_field_int (uiout, "times", b->hit_count);
6187         }
6188     }
6189
6190   if (!part_of_multiple && b->ignore_count)
6191     {
6192       annotate_field (8);
6193       ui_out_text (uiout, "\tignore next ");
6194       ui_out_field_int (uiout, "ignore", b->ignore_count);
6195       ui_out_text (uiout, " hits\n");
6196     }
6197
6198   /* Note that an enable count of 1 corresponds to "enable once"
6199      behavior, which is reported by the combination of enablement and
6200      disposition, so we don't need to mention it here.  */
6201   if (!part_of_multiple && b->enable_count > 1)
6202     {
6203       annotate_field (8);
6204       ui_out_text (uiout, "\tdisable after ");
6205       /* Tweak the wording to clarify that ignore and enable counts
6206          are distinct, and have additive effect.  */
6207       if (b->ignore_count)
6208         ui_out_text (uiout, "additional ");
6209       else
6210         ui_out_text (uiout, "next ");
6211       ui_out_field_int (uiout, "enable", b->enable_count);
6212       ui_out_text (uiout, " hits\n");
6213     }
6214
6215   if (!part_of_multiple && is_tracepoint (b))
6216     {
6217       struct tracepoint *tp = (struct tracepoint *) b;
6218
6219       if (tp->traceframe_usage)
6220         {
6221           ui_out_text (uiout, "\ttrace buffer usage ");
6222           ui_out_field_int (uiout, "traceframe-usage", tp->traceframe_usage);
6223           ui_out_text (uiout, " bytes\n");
6224         }
6225     }
6226
6227   l = b->commands ? b->commands->commands : NULL;
6228   if (!part_of_multiple && l)
6229     {
6230       struct cleanup *script_chain;
6231
6232       annotate_field (9);
6233       script_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "script");
6234       print_command_lines (uiout, l, 4);
6235       do_cleanups (script_chain);
6236     }
6237
6238   if (is_tracepoint (b))
6239     {
6240       struct tracepoint *t = (struct tracepoint *) b;
6241
6242       if (!part_of_multiple && t->pass_count)
6243         {
6244           annotate_field (10);
6245           ui_out_text (uiout, "\tpass count ");
6246           ui_out_field_int (uiout, "pass", t->pass_count);
6247           ui_out_text (uiout, " \n");
6248         }
6249
6250       /* Don't display it when tracepoint or tracepoint location is
6251          pending.   */
6252       if (!header_of_multiple && loc != NULL && !loc->shlib_disabled)
6253         {
6254           annotate_field (11);
6255
6256           if (ui_out_is_mi_like_p (uiout))
6257             ui_out_field_string (uiout, "installed",
6258                                  loc->inserted ? "y" : "n");
6259           else
6260             {
6261               if (loc->inserted)
6262                 ui_out_text (uiout, "\t");
6263               else
6264                 ui_out_text (uiout, "\tnot ");
6265               ui_out_text (uiout, "installed on target\n");
6266             }
6267         }
6268     }
6269
6270   if (ui_out_is_mi_like_p (uiout) && !part_of_multiple)
6271     {
6272       if (is_watchpoint (b))
6273         {
6274           struct watchpoint *w = (struct watchpoint *) b;
6275
6276           ui_out_field_string (uiout, "original-location", w->exp_string);
6277         }
6278       else if (b->addr_string)
6279         ui_out_field_string (uiout, "original-location", b->addr_string);
6280     }
6281 }
6282
6283 static void
6284 print_one_breakpoint (struct breakpoint *b,
6285                       struct bp_location **last_loc, 
6286                       int allflag)
6287 {
6288   struct cleanup *bkpt_chain;
6289   struct ui_out *uiout = current_uiout;
6290
6291   bkpt_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "bkpt");
6292
6293   print_one_breakpoint_location (b, NULL, 0, last_loc, allflag);
6294   do_cleanups (bkpt_chain);
6295
6296   /* If this breakpoint has custom print function,
6297      it's already printed.  Otherwise, print individual
6298      locations, if any.  */
6299   if (b->ops == NULL || b->ops->print_one == NULL)
6300     {
6301       /* If breakpoint has a single location that is disabled, we
6302          print it as if it had several locations, since otherwise it's
6303          hard to represent "breakpoint enabled, location disabled"
6304          situation.
6305
6306          Note that while hardware watchpoints have several locations
6307          internally, that's not a property exposed to user.  */
6308       if (b->loc 
6309           && !is_hardware_watchpoint (b)
6310           && (b->loc->next || !b->loc->enabled))
6311         {
6312           struct bp_location *loc;
6313           int n = 1;
6314
6315           for (loc = b->loc; loc; loc = loc->next, ++n)
6316             {
6317               struct cleanup *inner2 =
6318                 make_cleanup_ui_out_tuple_begin_end (uiout, NULL);
6319               print_one_breakpoint_location (b, loc, n, last_loc, allflag);
6320               do_cleanups (inner2);
6321             }
6322         }
6323     }
6324 }
6325
6326 static int
6327 breakpoint_address_bits (struct breakpoint *b)
6328 {
6329   int print_address_bits = 0;
6330   struct bp_location *loc;
6331
6332   for (loc = b->loc; loc; loc = loc->next)
6333     {
6334       int addr_bit;
6335
6336       /* Software watchpoints that aren't watching memory don't have
6337          an address to print.  */
6338       if (b->type == bp_watchpoint && loc->watchpoint_type == -1)
6339         continue;
6340
6341       addr_bit = gdbarch_addr_bit (loc->gdbarch);
6342       if (addr_bit > print_address_bits)
6343         print_address_bits = addr_bit;
6344     }
6345
6346   return print_address_bits;
6347 }
6348
6349 struct captured_breakpoint_query_args
6350   {
6351     int bnum;
6352   };
6353
6354 static int
6355 do_captured_breakpoint_query (struct ui_out *uiout, void *data)
6356 {
6357   struct captured_breakpoint_query_args *args = data;
6358   struct breakpoint *b;
6359   struct bp_location *dummy_loc = NULL;
6360
6361   ALL_BREAKPOINTS (b)
6362     {
6363       if (args->bnum == b->number)
6364         {
6365           print_one_breakpoint (b, &dummy_loc, 0);
6366           return GDB_RC_OK;
6367         }
6368     }
6369   return GDB_RC_NONE;
6370 }
6371
6372 enum gdb_rc
6373 gdb_breakpoint_query (struct ui_out *uiout, int bnum, 
6374                       char **error_message)
6375 {
6376   struct captured_breakpoint_query_args args;
6377
6378   args.bnum = bnum;
6379   /* For the moment we don't trust print_one_breakpoint() to not throw
6380      an error.  */
6381   if (catch_exceptions_with_msg (uiout, do_captured_breakpoint_query, &args,
6382                                  error_message, RETURN_MASK_ALL) < 0)
6383     return GDB_RC_FAIL;
6384   else
6385     return GDB_RC_OK;
6386 }
6387
6388 /* Return true if this breakpoint was set by the user, false if it is
6389    internal or momentary.  */
6390
6391 int
6392 user_breakpoint_p (struct breakpoint *b)
6393 {
6394   return b->number > 0;
6395 }
6396
6397 /* Print information on user settable breakpoint (watchpoint, etc)
6398    number BNUM.  If BNUM is -1 print all user-settable breakpoints.
6399    If ALLFLAG is non-zero, include non-user-settable breakpoints.  If
6400    FILTER is non-NULL, call it on each breakpoint and only include the
6401    ones for which it returns non-zero.  Return the total number of
6402    breakpoints listed.  */
6403
6404 static int
6405 breakpoint_1 (char *args, int allflag, 
6406               int (*filter) (const struct breakpoint *))
6407 {
6408   struct breakpoint *b;
6409   struct bp_location *last_loc = NULL;
6410   int nr_printable_breakpoints;
6411   struct cleanup *bkpttbl_chain;
6412   struct value_print_options opts;
6413   int print_address_bits = 0;
6414   int print_type_col_width = 14;
6415   struct ui_out *uiout = current_uiout;
6416
6417   get_user_print_options (&opts);
6418
6419   /* Compute the number of rows in the table, as well as the size
6420      required for address fields.  */
6421   nr_printable_breakpoints = 0;
6422   ALL_BREAKPOINTS (b)
6423     {
6424       /* If we have a filter, only list the breakpoints it accepts.  */
6425       if (filter && !filter (b))
6426         continue;
6427
6428       /* If we have an "args" string, it is a list of breakpoints to 
6429          accept.  Skip the others.  */
6430       if (args != NULL && *args != '\0')
6431         {
6432           if (allflag && parse_and_eval_long (args) != b->number)
6433             continue;
6434           if (!allflag && !number_is_in_list (args, b->number))
6435             continue;
6436         }
6437
6438       if (allflag || user_breakpoint_p (b))
6439         {
6440           int addr_bit, type_len;
6441
6442           addr_bit = breakpoint_address_bits (b);
6443           if (addr_bit > print_address_bits)
6444             print_address_bits = addr_bit;
6445
6446           type_len = strlen (bptype_string (b->type));
6447           if (type_len > print_type_col_width)
6448             print_type_col_width = type_len;
6449
6450           nr_printable_breakpoints++;
6451         }
6452     }
6453
6454   if (opts.addressprint)
6455     bkpttbl_chain 
6456       = make_cleanup_ui_out_table_begin_end (uiout, 6,
6457                                              nr_printable_breakpoints,
6458                                              "BreakpointTable");
6459   else
6460     bkpttbl_chain 
6461       = make_cleanup_ui_out_table_begin_end (uiout, 5,
6462                                              nr_printable_breakpoints,
6463                                              "BreakpointTable");
6464
6465   if (nr_printable_breakpoints > 0)
6466     annotate_breakpoints_headers ();
6467   if (nr_printable_breakpoints > 0)
6468     annotate_field (0);
6469   ui_out_table_header (uiout, 7, ui_left, "number", "Num");     /* 1 */
6470   if (nr_printable_breakpoints > 0)
6471     annotate_field (1);
6472   ui_out_table_header (uiout, print_type_col_width, ui_left,
6473                        "type", "Type");                         /* 2 */
6474   if (nr_printable_breakpoints > 0)
6475     annotate_field (2);
6476   ui_out_table_header (uiout, 4, ui_left, "disp", "Disp");      /* 3 */
6477   if (nr_printable_breakpoints > 0)
6478     annotate_field (3);
6479   ui_out_table_header (uiout, 3, ui_left, "enabled", "Enb");    /* 4 */
6480   if (opts.addressprint)
6481     {
6482       if (nr_printable_breakpoints > 0)
6483         annotate_field (4);
6484       if (print_address_bits <= 32)
6485         ui_out_table_header (uiout, 10, ui_left, 
6486                              "addr", "Address");                /* 5 */
6487       else
6488         ui_out_table_header (uiout, 18, ui_left, 
6489                              "addr", "Address");                /* 5 */
6490     }
6491   if (nr_printable_breakpoints > 0)
6492     annotate_field (5);
6493   ui_out_table_header (uiout, 40, ui_noalign, "what", "What");  /* 6 */
6494   ui_out_table_body (uiout);
6495   if (nr_printable_breakpoints > 0)
6496     annotate_breakpoints_table ();
6497
6498   ALL_BREAKPOINTS (b)
6499     {
6500       QUIT;
6501       /* If we have a filter, only list the breakpoints it accepts.  */
6502       if (filter && !filter (b))
6503         continue;
6504
6505       /* If we have an "args" string, it is a list of breakpoints to 
6506          accept.  Skip the others.  */
6507
6508       if (args != NULL && *args != '\0')
6509         {
6510           if (allflag)  /* maintenance info breakpoint */
6511             {
6512               if (parse_and_eval_long (args) != b->number)
6513                 continue;
6514             }
6515           else          /* all others */
6516             {
6517               if (!number_is_in_list (args, b->number))
6518                 continue;
6519             }
6520         }
6521       /* We only print out user settable breakpoints unless the
6522          allflag is set.  */
6523       if (allflag || user_breakpoint_p (b))
6524         print_one_breakpoint (b, &last_loc, allflag);
6525     }
6526
6527   do_cleanups (bkpttbl_chain);
6528
6529   if (nr_printable_breakpoints == 0)
6530     {
6531       /* If there's a filter, let the caller decide how to report
6532          empty list.  */
6533       if (!filter)
6534         {
6535           if (args == NULL || *args == '\0')
6536             ui_out_message (uiout, 0, "No breakpoints or watchpoints.\n");
6537           else
6538             ui_out_message (uiout, 0, 
6539                             "No breakpoint or watchpoint matching '%s'.\n",
6540                             args);
6541         }
6542     }
6543   else
6544     {
6545       if (last_loc && !server_command)
6546         set_next_address (last_loc->gdbarch, last_loc->address);
6547     }
6548
6549   /* FIXME?  Should this be moved up so that it is only called when
6550      there have been breakpoints? */
6551   annotate_breakpoints_table_end ();
6552
6553   return nr_printable_breakpoints;
6554 }
6555
6556 /* Display the value of default-collect in a way that is generally
6557    compatible with the breakpoint list.  */
6558
6559 static void
6560 default_collect_info (void)
6561 {
6562   struct ui_out *uiout = current_uiout;
6563
6564   /* If it has no value (which is frequently the case), say nothing; a
6565      message like "No default-collect." gets in user's face when it's
6566      not wanted.  */
6567   if (!*default_collect)
6568     return;
6569
6570   /* The following phrase lines up nicely with per-tracepoint collect
6571      actions.  */
6572   ui_out_text (uiout, "default collect ");
6573   ui_out_field_string (uiout, "default-collect", default_collect);
6574   ui_out_text (uiout, " \n");
6575 }
6576   
6577 static void
6578 breakpoints_info (char *args, int from_tty)
6579 {
6580   breakpoint_1 (args, 0, NULL);
6581
6582   default_collect_info ();
6583 }
6584
6585 static void
6586 watchpoints_info (char *args, int from_tty)
6587 {
6588   int num_printed = breakpoint_1 (args, 0, is_watchpoint);
6589   struct ui_out *uiout = current_uiout;
6590
6591   if (num_printed == 0)
6592     {
6593       if (args == NULL || *args == '\0')
6594         ui_out_message (uiout, 0, "No watchpoints.\n");
6595       else
6596         ui_out_message (uiout, 0, "No watchpoint matching '%s'.\n", args);
6597     }
6598 }
6599
6600 static void
6601 maintenance_info_breakpoints (char *args, int from_tty)
6602 {
6603   breakpoint_1 (args, 1, NULL);
6604
6605   default_collect_info ();
6606 }
6607
6608 static int
6609 breakpoint_has_pc (struct breakpoint *b,
6610                    struct program_space *pspace,
6611                    CORE_ADDR pc, struct obj_section *section)
6612 {
6613   struct bp_location *bl = b->loc;
6614
6615   for (; bl; bl = bl->next)
6616     {
6617       if (bl->pspace == pspace
6618           && bl->address == pc
6619           && (!overlay_debugging || bl->section == section))
6620         return 1;         
6621     }
6622   return 0;
6623 }
6624
6625 /* Print a message describing any user-breakpoints set at PC.  This
6626    concerns with logical breakpoints, so we match program spaces, not
6627    address spaces.  */
6628
6629 static void
6630 describe_other_breakpoints (struct gdbarch *gdbarch,
6631                             struct program_space *pspace, CORE_ADDR pc,
6632                             struct obj_section *section, int thread)
6633 {
6634   int others = 0;
6635   struct breakpoint *b;
6636
6637   ALL_BREAKPOINTS (b)
6638     others += (user_breakpoint_p (b)
6639                && breakpoint_has_pc (b, pspace, pc, section));
6640   if (others > 0)
6641     {
6642       if (others == 1)
6643         printf_filtered (_("Note: breakpoint "));
6644       else /* if (others == ???) */
6645         printf_filtered (_("Note: breakpoints "));
6646       ALL_BREAKPOINTS (b)
6647         if (user_breakpoint_p (b) && breakpoint_has_pc (b, pspace, pc, section))
6648           {
6649             others--;
6650             printf_filtered ("%d", b->number);
6651             if (b->thread == -1 && thread != -1)
6652               printf_filtered (" (all threads)");
6653             else if (b->thread != -1)
6654               printf_filtered (" (thread %d)", b->thread);
6655             printf_filtered ("%s%s ",
6656                              ((b->enable_state == bp_disabled
6657                                || b->enable_state == bp_call_disabled)
6658                               ? " (disabled)"
6659                               : b->enable_state == bp_permanent 
6660                               ? " (permanent)"
6661                               : ""),
6662                              (others > 1) ? "," 
6663                              : ((others == 1) ? " and" : ""));
6664           }
6665       printf_filtered (_("also set at pc "));
6666       fputs_filtered (paddress (gdbarch, pc), gdb_stdout);
6667       printf_filtered (".\n");
6668     }
6669 }
6670 \f
6671
6672 /* Return true iff it is meaningful to use the address member of
6673    BPT.  For some breakpoint types, the address member is irrelevant
6674    and it makes no sense to attempt to compare it to other addresses
6675    (or use it for any other purpose either).
6676
6677    More specifically, each of the following breakpoint types will
6678    always have a zero valued address and we don't want to mark
6679    breakpoints of any of these types to be a duplicate of an actual
6680    breakpoint at address zero:
6681
6682       bp_watchpoint
6683       bp_catchpoint
6684
6685 */
6686
6687 static int
6688 breakpoint_address_is_meaningful (struct breakpoint *bpt)
6689 {
6690   enum bptype type = bpt->type;
6691
6692   return (type != bp_watchpoint && type != bp_catchpoint);
6693 }
6694
6695 /* Assuming LOC1 and LOC2's owners are hardware watchpoints, returns
6696    true if LOC1 and LOC2 represent the same watchpoint location.  */
6697
6698 static int
6699 watchpoint_locations_match (struct bp_location *loc1, 
6700                             struct bp_location *loc2)
6701 {
6702   struct watchpoint *w1 = (struct watchpoint *) loc1->owner;
6703   struct watchpoint *w2 = (struct watchpoint *) loc2->owner;
6704
6705   /* Both of them must exist.  */
6706   gdb_assert (w1 != NULL);
6707   gdb_assert (w2 != NULL);
6708
6709   /* If the target can evaluate the condition expression in hardware,
6710      then we we need to insert both watchpoints even if they are at
6711      the same place.  Otherwise the watchpoint will only trigger when
6712      the condition of whichever watchpoint was inserted evaluates to
6713      true, not giving a chance for GDB to check the condition of the
6714      other watchpoint.  */
6715   if ((w1->cond_exp
6716        && target_can_accel_watchpoint_condition (loc1->address, 
6717                                                  loc1->length,
6718                                                  loc1->watchpoint_type,
6719                                                  w1->cond_exp))
6720       || (w2->cond_exp
6721           && target_can_accel_watchpoint_condition (loc2->address, 
6722                                                     loc2->length,
6723                                                     loc2->watchpoint_type,
6724                                                     w2->cond_exp)))
6725     return 0;
6726
6727   /* Note that this checks the owner's type, not the location's.  In
6728      case the target does not support read watchpoints, but does
6729      support access watchpoints, we'll have bp_read_watchpoint
6730      watchpoints with hw_access locations.  Those should be considered
6731      duplicates of hw_read locations.  The hw_read locations will
6732      become hw_access locations later.  */
6733   return (loc1->owner->type == loc2->owner->type
6734           && loc1->pspace->aspace == loc2->pspace->aspace
6735           && loc1->address == loc2->address
6736           && loc1->length == loc2->length);
6737 }
6738
6739 /* Returns true if {ASPACE1,ADDR1} and {ASPACE2,ADDR2} represent the
6740    same breakpoint location.  In most targets, this can only be true
6741    if ASPACE1 matches ASPACE2.  On targets that have global
6742    breakpoints, the address space doesn't really matter.  */
6743
6744 static int
6745 breakpoint_address_match (struct address_space *aspace1, CORE_ADDR addr1,
6746                           struct address_space *aspace2, CORE_ADDR addr2)
6747 {
6748   return ((gdbarch_has_global_breakpoints (target_gdbarch ())
6749            || aspace1 == aspace2)
6750           && addr1 == addr2);
6751 }
6752
6753 /* Returns true if {ASPACE2,ADDR2} falls within the range determined by
6754    {ASPACE1,ADDR1,LEN1}.  In most targets, this can only be true if ASPACE1
6755    matches ASPACE2.  On targets that have global breakpoints, the address
6756    space doesn't really matter.  */
6757
6758 static int
6759 breakpoint_address_match_range (struct address_space *aspace1, CORE_ADDR addr1,
6760                                 int len1, struct address_space *aspace2,
6761                                 CORE_ADDR addr2)
6762 {
6763   return ((gdbarch_has_global_breakpoints (target_gdbarch ())
6764            || aspace1 == aspace2)
6765           && addr2 >= addr1 && addr2 < addr1 + len1);
6766 }
6767
6768 /* Returns true if {ASPACE,ADDR} matches the breakpoint BL.  BL may be
6769    a ranged breakpoint.  In most targets, a match happens only if ASPACE
6770    matches the breakpoint's address space.  On targets that have global
6771    breakpoints, the address space doesn't really matter.  */
6772
6773 static int
6774 breakpoint_location_address_match (struct bp_location *bl,
6775                                    struct address_space *aspace,
6776                                    CORE_ADDR addr)
6777 {
6778   return (breakpoint_address_match (bl->pspace->aspace, bl->address,
6779                                     aspace, addr)
6780           || (bl->length
6781               && breakpoint_address_match_range (bl->pspace->aspace,
6782                                                  bl->address, bl->length,
6783                                                  aspace, addr)));
6784 }
6785
6786 /* If LOC1 and LOC2's owners are not tracepoints, returns false directly.
6787    Then, if LOC1 and LOC2 represent the same tracepoint location, returns
6788    true, otherwise returns false.  */
6789
6790 static int
6791 tracepoint_locations_match (struct bp_location *loc1,
6792                             struct bp_location *loc2)
6793 {
6794   if (is_tracepoint (loc1->owner) && is_tracepoint (loc2->owner))
6795     /* Since tracepoint locations are never duplicated with others', tracepoint
6796        locations at the same address of different tracepoints are regarded as
6797        different locations.  */
6798     return (loc1->address == loc2->address && loc1->owner == loc2->owner);
6799   else
6800     return 0;
6801 }
6802
6803 /* Assuming LOC1 and LOC2's types' have meaningful target addresses
6804    (breakpoint_address_is_meaningful), returns true if LOC1 and LOC2
6805    represent the same location.  */
6806
6807 static int
6808 breakpoint_locations_match (struct bp_location *loc1, 
6809                             struct bp_location *loc2)
6810 {
6811   int hw_point1, hw_point2;
6812
6813   /* Both of them must not be in moribund_locations.  */
6814   gdb_assert (loc1->owner != NULL);
6815   gdb_assert (loc2->owner != NULL);
6816
6817   hw_point1 = is_hardware_watchpoint (loc1->owner);
6818   hw_point2 = is_hardware_watchpoint (loc2->owner);
6819
6820   if (hw_point1 != hw_point2)
6821     return 0;
6822   else if (hw_point1)
6823     return watchpoint_locations_match (loc1, loc2);
6824   else if (is_tracepoint (loc1->owner) || is_tracepoint (loc2->owner))
6825     return tracepoint_locations_match (loc1, loc2);
6826   else
6827     /* We compare bp_location.length in order to cover ranged breakpoints.  */
6828     return (breakpoint_address_match (loc1->pspace->aspace, loc1->address,
6829                                      loc2->pspace->aspace, loc2->address)
6830             && loc1->length == loc2->length);
6831 }
6832
6833 static void
6834 breakpoint_adjustment_warning (CORE_ADDR from_addr, CORE_ADDR to_addr,
6835                                int bnum, int have_bnum)
6836 {
6837   /* The longest string possibly returned by hex_string_custom
6838      is 50 chars.  These must be at least that big for safety.  */
6839   char astr1[64];
6840   char astr2[64];
6841
6842   strcpy (astr1, hex_string_custom ((unsigned long) from_addr, 8));
6843   strcpy (astr2, hex_string_custom ((unsigned long) to_addr, 8));
6844   if (have_bnum)
6845     warning (_("Breakpoint %d address previously adjusted from %s to %s."),
6846              bnum, astr1, astr2);
6847   else
6848     warning (_("Breakpoint address adjusted from %s to %s."), astr1, astr2);
6849 }
6850
6851 /* Adjust a breakpoint's address to account for architectural
6852    constraints on breakpoint placement.  Return the adjusted address.
6853    Note: Very few targets require this kind of adjustment.  For most
6854    targets, this function is simply the identity function.  */
6855
6856 static CORE_ADDR
6857 adjust_breakpoint_address (struct gdbarch *gdbarch,
6858                            CORE_ADDR bpaddr, enum bptype bptype)
6859 {
6860   if (!gdbarch_adjust_breakpoint_address_p (gdbarch))
6861     {
6862       /* Very few targets need any kind of breakpoint adjustment.  */
6863       return bpaddr;
6864     }
6865   else if (bptype == bp_watchpoint
6866            || bptype == bp_hardware_watchpoint
6867            || bptype == bp_read_watchpoint
6868            || bptype == bp_access_watchpoint
6869            || bptype == bp_catchpoint)
6870     {
6871       /* Watchpoints and the various bp_catch_* eventpoints should not
6872          have their addresses modified.  */
6873       return bpaddr;
6874     }
6875   else
6876     {
6877       CORE_ADDR adjusted_bpaddr;
6878
6879       /* Some targets have architectural constraints on the placement
6880          of breakpoint instructions.  Obtain the adjusted address.  */
6881       adjusted_bpaddr = gdbarch_adjust_breakpoint_address (gdbarch, bpaddr);
6882
6883       /* An adjusted breakpoint address can significantly alter
6884          a user's expectations.  Print a warning if an adjustment
6885          is required.  */
6886       if (adjusted_bpaddr != bpaddr)
6887         breakpoint_adjustment_warning (bpaddr, adjusted_bpaddr, 0, 0);
6888
6889       return adjusted_bpaddr;
6890     }
6891 }
6892
6893 void
6894 init_bp_location (struct bp_location *loc, const struct bp_location_ops *ops,
6895                   struct breakpoint *owner)
6896 {
6897   memset (loc, 0, sizeof (*loc));
6898
6899   gdb_assert (ops != NULL);
6900
6901   loc->ops = ops;
6902   loc->owner = owner;
6903   loc->cond = NULL;
6904   loc->cond_bytecode = NULL;
6905   loc->shlib_disabled = 0;
6906   loc->enabled = 1;
6907
6908   switch (owner->type)
6909     {
6910     case bp_breakpoint:
6911     case bp_until:
6912     case bp_finish:
6913     case bp_longjmp:
6914     case bp_longjmp_resume:
6915     case bp_longjmp_call_dummy:
6916     case bp_exception:
6917     case bp_exception_resume:
6918     case bp_step_resume:
6919     case bp_hp_step_resume:
6920     case bp_watchpoint_scope:
6921     case bp_call_dummy:
6922     case bp_std_terminate:
6923     case bp_shlib_event:
6924     case bp_thread_event:
6925     case bp_overlay_event:
6926     case bp_jit_event:
6927     case bp_longjmp_master:
6928     case bp_std_terminate_master:
6929     case bp_exception_master:
6930     case bp_gnu_ifunc_resolver:
6931     case bp_gnu_ifunc_resolver_return:
6932     case bp_dprintf:
6933       loc->loc_type = bp_loc_software_breakpoint;
6934       mark_breakpoint_location_modified (loc);
6935       break;
6936     case bp_hardware_breakpoint:
6937       loc->loc_type = bp_loc_hardware_breakpoint;
6938       mark_breakpoint_location_modified (loc);
6939       break;
6940     case bp_hardware_watchpoint:
6941     case bp_read_watchpoint:
6942     case bp_access_watchpoint:
6943       loc->loc_type = bp_loc_hardware_watchpoint;
6944       break;
6945     case bp_watchpoint:
6946     case bp_catchpoint:
6947     case bp_tracepoint:
6948     case bp_fast_tracepoint:
6949     case bp_static_tracepoint:
6950       loc->loc_type = bp_loc_other;
6951       break;
6952     default:
6953       internal_error (__FILE__, __LINE__, _("unknown breakpoint type"));
6954     }
6955
6956   loc->refc = 1;
6957 }
6958
6959 /* Allocate a struct bp_location.  */
6960
6961 static struct bp_location *
6962 allocate_bp_location (struct breakpoint *bpt)
6963 {
6964   return bpt->ops->allocate_location (bpt);
6965 }
6966
6967 static void
6968 free_bp_location (struct bp_location *loc)
6969 {
6970   loc->ops->dtor (loc);
6971   xfree (loc);
6972 }
6973
6974 /* Increment reference count.  */
6975
6976 static void
6977 incref_bp_location (struct bp_location *bl)
6978 {
6979   ++bl->refc;
6980 }
6981
6982 /* Decrement reference count.  If the reference count reaches 0,
6983    destroy the bp_location.  Sets *BLP to NULL.  */
6984
6985 static void
6986 decref_bp_location (struct bp_location **blp)
6987 {
6988   gdb_assert ((*blp)->refc > 0);
6989
6990   if (--(*blp)->refc == 0)
6991     free_bp_location (*blp);
6992   *blp = NULL;
6993 }
6994
6995 /* Add breakpoint B at the end of the global breakpoint chain.  */
6996
6997 static void
6998 add_to_breakpoint_chain (struct breakpoint *b)
6999 {
7000   struct breakpoint *b1;
7001
7002   /* Add this breakpoint to the end of the chain so that a list of
7003      breakpoints will come out in order of increasing numbers.  */
7004
7005   b1 = breakpoint_chain;
7006   if (b1 == 0)
7007     breakpoint_chain = b;
7008   else
7009     {
7010       while (b1->next)
7011         b1 = b1->next;
7012       b1->next = b;
7013     }
7014 }
7015
7016 /* Initializes breakpoint B with type BPTYPE and no locations yet.  */
7017
7018 static void
7019 init_raw_breakpoint_without_location (struct breakpoint *b,
7020                                       struct gdbarch *gdbarch,
7021                                       enum bptype bptype,
7022                                       const struct breakpoint_ops *ops)
7023 {
7024   memset (b, 0, sizeof (*b));
7025
7026   gdb_assert (ops != NULL);
7027
7028   b->ops = ops;
7029   b->type = bptype;
7030   b->gdbarch = gdbarch;
7031   b->language = current_language->la_language;
7032   b->input_radix = input_radix;
7033   b->thread = -1;
7034   b->enable_state = bp_enabled;
7035   b->next = 0;
7036   b->silent = 0;
7037   b->ignore_count = 0;
7038   b->commands = NULL;
7039   b->frame_id = null_frame_id;
7040   b->condition_not_parsed = 0;
7041   b->py_bp_object = NULL;
7042   b->related_breakpoint = b;
7043 }
7044
7045 /* Helper to set_raw_breakpoint below.  Creates a breakpoint
7046    that has type BPTYPE and has no locations as yet.  */
7047
7048 static struct breakpoint *
7049 set_raw_breakpoint_without_location (struct gdbarch *gdbarch,
7050                                      enum bptype bptype,
7051                                      const struct breakpoint_ops *ops)
7052 {
7053   struct breakpoint *b = XNEW (struct breakpoint);
7054
7055   init_raw_breakpoint_without_location (b, gdbarch, bptype, ops);
7056   add_to_breakpoint_chain (b);
7057   return b;
7058 }
7059
7060 /* Initialize loc->function_name.  EXPLICIT_LOC says no indirect function
7061    resolutions should be made as the user specified the location explicitly
7062    enough.  */
7063
7064 static void
7065 set_breakpoint_location_function (struct bp_location *loc, int explicit_loc)
7066 {
7067   gdb_assert (loc->owner != NULL);
7068
7069   if (loc->owner->type == bp_breakpoint
7070       || loc->owner->type == bp_hardware_breakpoint
7071       || is_tracepoint (loc->owner))
7072     {
7073       int is_gnu_ifunc;
7074       const char *function_name;
7075       CORE_ADDR func_addr;
7076
7077       find_pc_partial_function_gnu_ifunc (loc->address, &function_name,
7078                                           &func_addr, NULL, &is_gnu_ifunc);
7079
7080       if (is_gnu_ifunc && !explicit_loc)
7081         {
7082           struct breakpoint *b = loc->owner;
7083
7084           gdb_assert (loc->pspace == current_program_space);
7085           if (gnu_ifunc_resolve_name (function_name,
7086                                       &loc->requested_address))
7087             {
7088               /* Recalculate ADDRESS based on new REQUESTED_ADDRESS.  */
7089               loc->address = adjust_breakpoint_address (loc->gdbarch,
7090                                                         loc->requested_address,
7091                                                         b->type);
7092             }
7093           else if (b->type == bp_breakpoint && b->loc == loc
7094                    && loc->next == NULL && b->related_breakpoint == b)
7095             {
7096               /* Create only the whole new breakpoint of this type but do not
7097                  mess more complicated breakpoints with multiple locations.  */
7098               b->type = bp_gnu_ifunc_resolver;
7099               /* Remember the resolver's address for use by the return
7100                  breakpoint.  */
7101               loc->related_address = func_addr;
7102             }
7103         }
7104
7105       if (function_name)
7106         loc->function_name = xstrdup (function_name);
7107     }
7108 }
7109
7110 /* Attempt to determine architecture of location identified by SAL.  */
7111 struct gdbarch *
7112 get_sal_arch (struct symtab_and_line sal)
7113 {
7114   if (sal.section)
7115     return get_objfile_arch (sal.section->objfile);
7116   if (sal.symtab)
7117     return get_objfile_arch (sal.symtab->objfile);
7118
7119   return NULL;
7120 }
7121
7122 /* Low level routine for partially initializing a breakpoint of type
7123    BPTYPE.  The newly created breakpoint's address, section, source
7124    file name, and line number are provided by SAL.
7125
7126    It is expected that the caller will complete the initialization of
7127    the newly created breakpoint struct as well as output any status
7128    information regarding the creation of a new breakpoint.  */
7129
7130 static void
7131 init_raw_breakpoint (struct breakpoint *b, struct gdbarch *gdbarch,
7132                      struct symtab_and_line sal, enum bptype bptype,
7133                      const struct breakpoint_ops *ops)
7134 {
7135   init_raw_breakpoint_without_location (b, gdbarch, bptype, ops);
7136
7137   add_location_to_breakpoint (b, &sal);
7138
7139   if (bptype != bp_catchpoint)
7140     gdb_assert (sal.pspace != NULL);
7141
7142   /* Store the program space that was used to set the breakpoint,
7143      except for ordinary breakpoints, which are independent of the
7144      program space.  */
7145   if (bptype != bp_breakpoint && bptype != bp_hardware_breakpoint)
7146     b->pspace = sal.pspace;
7147 }
7148
7149 /* set_raw_breakpoint is a low level routine for allocating and
7150    partially initializing a breakpoint of type BPTYPE.  The newly
7151    created breakpoint's address, section, source file name, and line
7152    number are provided by SAL.  The newly created and partially
7153    initialized breakpoint is added to the breakpoint chain and
7154    is also returned as the value of this function.
7155
7156    It is expected that the caller will complete the initialization of
7157    the newly created breakpoint struct as well as output any status
7158    information regarding the creation of a new breakpoint.  In
7159    particular, set_raw_breakpoint does NOT set the breakpoint
7160    number!  Care should be taken to not allow an error to occur
7161    prior to completing the initialization of the breakpoint.  If this
7162    should happen, a bogus breakpoint will be left on the chain.  */
7163
7164 struct breakpoint *
7165 set_raw_breakpoint (struct gdbarch *gdbarch,
7166                     struct symtab_and_line sal, enum bptype bptype,
7167                     const struct breakpoint_ops *ops)
7168 {
7169   struct breakpoint *b = XNEW (struct breakpoint);
7170
7171   init_raw_breakpoint (b, gdbarch, sal, bptype, ops);
7172   add_to_breakpoint_chain (b);
7173   return b;
7174 }
7175
7176
7177 /* Note that the breakpoint object B describes a permanent breakpoint
7178    instruction, hard-wired into the inferior's code.  */
7179 void
7180 make_breakpoint_permanent (struct breakpoint *b)
7181 {
7182   struct bp_location *bl;
7183
7184   b->enable_state = bp_permanent;
7185
7186   /* By definition, permanent breakpoints are already present in the
7187      code.  Mark all locations as inserted.  For now,
7188      make_breakpoint_permanent is called in just one place, so it's
7189      hard to say if it's reasonable to have permanent breakpoint with
7190      multiple locations or not, but it's easy to implement.  */
7191   for (bl = b->loc; bl; bl = bl->next)
7192     bl->inserted = 1;
7193 }
7194
7195 /* Call this routine when stepping and nexting to enable a breakpoint
7196    if we do a longjmp() or 'throw' in TP.  FRAME is the frame which
7197    initiated the operation.  */
7198
7199 void
7200 set_longjmp_breakpoint (struct thread_info *tp, struct frame_id frame)
7201 {
7202   struct breakpoint *b, *b_tmp;
7203   int thread = tp->num;
7204
7205   /* To avoid having to rescan all objfile symbols at every step,
7206      we maintain a list of continually-inserted but always disabled
7207      longjmp "master" breakpoints.  Here, we simply create momentary
7208      clones of those and enable them for the requested thread.  */
7209   ALL_BREAKPOINTS_SAFE (b, b_tmp)
7210     if (b->pspace == current_program_space
7211         && (b->type == bp_longjmp_master
7212             || b->type == bp_exception_master))
7213       {
7214         enum bptype type = b->type == bp_longjmp_master ? bp_longjmp : bp_exception;
7215         struct breakpoint *clone;
7216
7217         /* longjmp_breakpoint_ops ensures INITIATING_FRAME is cleared again
7218            after their removal.  */
7219         clone = momentary_breakpoint_from_master (b, type,
7220                                                   &longjmp_breakpoint_ops);
7221         clone->thread = thread;
7222       }
7223
7224   tp->initiating_frame = frame;
7225 }
7226
7227 /* Delete all longjmp breakpoints from THREAD.  */
7228 void
7229 delete_longjmp_breakpoint (int thread)
7230 {
7231   struct breakpoint *b, *b_tmp;
7232
7233   ALL_BREAKPOINTS_SAFE (b, b_tmp)
7234     if (b->type == bp_longjmp || b->type == bp_exception)
7235       {
7236         if (b->thread == thread)
7237           delete_breakpoint (b);
7238       }
7239 }
7240
7241 void
7242 delete_longjmp_breakpoint_at_next_stop (int thread)
7243 {
7244   struct breakpoint *b, *b_tmp;
7245
7246   ALL_BREAKPOINTS_SAFE (b, b_tmp)
7247     if (b->type == bp_longjmp || b->type == bp_exception)
7248       {
7249         if (b->thread == thread)
7250           b->disposition = disp_del_at_next_stop;
7251       }
7252 }
7253
7254 /* Place breakpoints of type bp_longjmp_call_dummy to catch longjmp for
7255    INFERIOR_PTID thread.  Chain them all by RELATED_BREAKPOINT and return
7256    pointer to any of them.  Return NULL if this system cannot place longjmp
7257    breakpoints.  */
7258
7259 struct breakpoint *
7260 set_longjmp_breakpoint_for_call_dummy (void)
7261 {
7262   struct breakpoint *b, *retval = NULL;
7263
7264   ALL_BREAKPOINTS (b)
7265     if (b->pspace == current_program_space && b->type == bp_longjmp_master)
7266       {
7267         struct breakpoint *new_b;
7268
7269         new_b = momentary_breakpoint_from_master (b, bp_longjmp_call_dummy,
7270                                                   &momentary_breakpoint_ops);
7271         new_b->thread = pid_to_thread_id (inferior_ptid);
7272
7273         /* Link NEW_B into the chain of RETVAL breakpoints.  */
7274
7275         gdb_assert (new_b->related_breakpoint == new_b);
7276         if (retval == NULL)
7277           retval = new_b;
7278         new_b->related_breakpoint = retval;
7279         while (retval->related_breakpoint != new_b->related_breakpoint)
7280           retval = retval->related_breakpoint;
7281         retval->related_breakpoint = new_b;
7282       }
7283
7284   return retval;
7285 }
7286
7287 /* Verify all existing dummy frames and their associated breakpoints for
7288    THREAD.  Remove those which can no longer be found in the current frame
7289    stack.
7290
7291    You should call this function only at places where it is safe to currently
7292    unwind the whole stack.  Failed stack unwind would discard live dummy
7293    frames.  */
7294
7295 void
7296 check_longjmp_breakpoint_for_call_dummy (int thread)
7297 {
7298   struct breakpoint *b, *b_tmp;
7299
7300   ALL_BREAKPOINTS_SAFE (b, b_tmp)
7301     if (b->type == bp_longjmp_call_dummy && b->thread == thread)
7302       {
7303         struct breakpoint *dummy_b = b->related_breakpoint;
7304
7305         while (dummy_b != b && dummy_b->type != bp_call_dummy)
7306           dummy_b = dummy_b->related_breakpoint;
7307         if (dummy_b->type != bp_call_dummy
7308             || frame_find_by_id (dummy_b->frame_id) != NULL)
7309           continue;
7310         
7311         dummy_frame_discard (dummy_b->frame_id);
7312
7313         while (b->related_breakpoint != b)
7314           {
7315             if (b_tmp == b->related_breakpoint)
7316               b_tmp = b->related_breakpoint->next;
7317             delete_breakpoint (b->related_breakpoint);
7318           }
7319         delete_breakpoint (b);
7320       }
7321 }
7322
7323 void
7324 enable_overlay_breakpoints (void)
7325 {
7326   struct breakpoint *b;
7327
7328   ALL_BREAKPOINTS (b)
7329     if (b->type == bp_overlay_event)
7330     {
7331       b->enable_state = bp_enabled;
7332       update_global_location_list (1);
7333       overlay_events_enabled = 1;
7334     }
7335 }
7336
7337 void
7338 disable_overlay_breakpoints (void)
7339 {
7340   struct breakpoint *b;
7341
7342   ALL_BREAKPOINTS (b)
7343     if (b->type == bp_overlay_event)
7344     {
7345       b->enable_state = bp_disabled;
7346       update_global_location_list (0);
7347       overlay_events_enabled = 0;
7348     }
7349 }
7350
7351 /* Set an active std::terminate breakpoint for each std::terminate
7352    master breakpoint.  */
7353 void
7354 set_std_terminate_breakpoint (void)
7355 {
7356   struct breakpoint *b, *b_tmp;
7357
7358   ALL_BREAKPOINTS_SAFE (b, b_tmp)
7359     if (b->pspace == current_program_space
7360         && b->type == bp_std_terminate_master)
7361       {
7362         momentary_breakpoint_from_master (b, bp_std_terminate,
7363                                           &momentary_breakpoint_ops);
7364       }
7365 }
7366
7367 /* Delete all the std::terminate breakpoints.  */
7368 void
7369 delete_std_terminate_breakpoint (void)
7370 {
7371   struct breakpoint *b, *b_tmp;
7372
7373   ALL_BREAKPOINTS_SAFE (b, b_tmp)
7374     if (b->type == bp_std_terminate)
7375       delete_breakpoint (b);
7376 }
7377
7378 struct breakpoint *
7379 create_thread_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7380 {
7381   struct breakpoint *b;
7382
7383   b = create_internal_breakpoint (gdbarch, address, bp_thread_event,
7384                                   &internal_breakpoint_ops);
7385
7386   b->enable_state = bp_enabled;
7387   /* addr_string has to be used or breakpoint_re_set will delete me.  */
7388   b->addr_string
7389     = xstrprintf ("*%s", paddress (b->loc->gdbarch, b->loc->address));
7390
7391   update_global_location_list_nothrow (1);
7392
7393   return b;
7394 }
7395
7396 void
7397 remove_thread_event_breakpoints (void)
7398 {
7399   struct breakpoint *b, *b_tmp;
7400
7401   ALL_BREAKPOINTS_SAFE (b, b_tmp)
7402     if (b->type == bp_thread_event
7403         && b->loc->pspace == current_program_space)
7404       delete_breakpoint (b);
7405 }
7406
7407 struct lang_and_radix
7408   {
7409     enum language lang;
7410     int radix;
7411   };
7412
7413 /* Create a breakpoint for JIT code registration and unregistration.  */
7414
7415 struct breakpoint *
7416 create_jit_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7417 {
7418   struct breakpoint *b;
7419
7420   b = create_internal_breakpoint (gdbarch, address, bp_jit_event,
7421                                   &internal_breakpoint_ops);
7422   update_global_location_list_nothrow (1);
7423   return b;
7424 }
7425
7426 /* Remove JIT code registration and unregistration breakpoint(s).  */
7427
7428 void
7429 remove_jit_event_breakpoints (void)
7430 {
7431   struct breakpoint *b, *b_tmp;
7432
7433   ALL_BREAKPOINTS_SAFE (b, b_tmp)
7434     if (b->type == bp_jit_event
7435         && b->loc->pspace == current_program_space)
7436       delete_breakpoint (b);
7437 }
7438
7439 void
7440 remove_solib_event_breakpoints (void)
7441 {
7442   struct breakpoint *b, *b_tmp;
7443
7444   ALL_BREAKPOINTS_SAFE (b, b_tmp)
7445     if (b->type == bp_shlib_event
7446         && b->loc->pspace == current_program_space)
7447       delete_breakpoint (b);
7448 }
7449
7450 struct breakpoint *
7451 create_solib_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7452 {
7453   struct breakpoint *b;
7454
7455   b = create_internal_breakpoint (gdbarch, address, bp_shlib_event,
7456                                   &internal_breakpoint_ops);
7457   update_global_location_list_nothrow (1);
7458   return b;
7459 }
7460
7461 /* Disable any breakpoints that are on code in shared libraries.  Only
7462    apply to enabled breakpoints, disabled ones can just stay disabled.  */
7463
7464 void
7465 disable_breakpoints_in_shlibs (void)
7466 {
7467   struct bp_location *loc, **locp_tmp;
7468
7469   ALL_BP_LOCATIONS (loc, locp_tmp)
7470   {
7471     /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL.  */
7472     struct breakpoint *b = loc->owner;
7473
7474     /* We apply the check to all breakpoints, including disabled for
7475        those with loc->duplicate set.  This is so that when breakpoint
7476        becomes enabled, or the duplicate is removed, gdb will try to
7477        insert all breakpoints.  If we don't set shlib_disabled here,
7478        we'll try to insert those breakpoints and fail.  */
7479     if (((b->type == bp_breakpoint)
7480          || (b->type == bp_jit_event)
7481          || (b->type == bp_hardware_breakpoint)
7482          || (is_tracepoint (b)))
7483         && loc->pspace == current_program_space
7484         && !loc->shlib_disabled
7485         && solib_name_from_address (loc->pspace, loc->address)
7486         )
7487       {
7488         loc->shlib_disabled = 1;
7489       }
7490   }
7491 }
7492
7493 /* Disable any breakpoints and tracepoints that are in SOLIB upon
7494    notification of unloaded_shlib.  Only apply to enabled breakpoints,
7495    disabled ones can just stay disabled.  */
7496
7497 static void
7498 disable_breakpoints_in_unloaded_shlib (struct so_list *solib)
7499 {
7500   struct bp_location *loc, **locp_tmp;
7501   int disabled_shlib_breaks = 0;
7502
7503   /* SunOS a.out shared libraries are always mapped, so do not
7504      disable breakpoints; they will only be reported as unloaded
7505      through clear_solib when GDB discards its shared library
7506      list.  See clear_solib for more information.  */
7507   if (exec_bfd != NULL
7508       && bfd_get_flavour (exec_bfd) == bfd_target_aout_flavour)
7509     return;
7510
7511   ALL_BP_LOCATIONS (loc, locp_tmp)
7512   {
7513     /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL.  */
7514     struct breakpoint *b = loc->owner;
7515
7516     if (solib->pspace == loc->pspace
7517         && !loc->shlib_disabled
7518         && (((b->type == bp_breakpoint
7519               || b->type == bp_jit_event
7520               || b->type == bp_hardware_breakpoint)
7521              && (loc->loc_type == bp_loc_hardware_breakpoint
7522                  || loc->loc_type == bp_loc_software_breakpoint))
7523             || is_tracepoint (b))
7524         && solib_contains_address_p (solib, loc->address))
7525       {
7526         loc->shlib_disabled = 1;
7527         /* At this point, we cannot rely on remove_breakpoint
7528            succeeding so we must mark the breakpoint as not inserted
7529            to prevent future errors occurring in remove_breakpoints.  */
7530         loc->inserted = 0;
7531
7532         /* This may cause duplicate notifications for the same breakpoint.  */
7533         observer_notify_breakpoint_modified (b);
7534
7535         if (!disabled_shlib_breaks)
7536           {
7537             target_terminal_ours_for_output ();
7538             warning (_("Temporarily disabling breakpoints "
7539                        "for unloaded shared library \"%s\""),
7540                      solib->so_name);
7541           }
7542         disabled_shlib_breaks = 1;
7543       }
7544   }
7545 }
7546
7547 /* Disable any breakpoints and tracepoints in OBJFILE upon
7548    notification of free_objfile.  Only apply to enabled breakpoints,
7549    disabled ones can just stay disabled.  */
7550
7551 static void
7552 disable_breakpoints_in_freed_objfile (struct objfile *objfile)
7553 {
7554   struct breakpoint *b;
7555
7556   if (objfile == NULL)
7557     return;
7558
7559   /* If the file is a shared library not loaded by the user then
7560      solib_unloaded was notified and disable_breakpoints_in_unloaded_shlib
7561      was called.  In that case there is no need to take action again.  */
7562   if ((objfile->flags & OBJF_SHARED) && !(objfile->flags & OBJF_USERLOADED))
7563     return;
7564
7565   ALL_BREAKPOINTS (b)
7566     {
7567       struct bp_location *loc;
7568       int bp_modified = 0;
7569
7570       if (!is_breakpoint (b) && !is_tracepoint (b))
7571         continue;
7572
7573       for (loc = b->loc; loc != NULL; loc = loc->next)
7574         {
7575           CORE_ADDR loc_addr = loc->address;
7576
7577           if (loc->loc_type != bp_loc_hardware_breakpoint
7578               && loc->loc_type != bp_loc_software_breakpoint)
7579             continue;
7580
7581           if (loc->shlib_disabled != 0)
7582             continue;
7583
7584           if (objfile->pspace != loc->pspace)
7585             continue;
7586
7587           if (loc->loc_type != bp_loc_hardware_breakpoint
7588               && loc->loc_type != bp_loc_software_breakpoint)
7589             continue;
7590
7591           if (is_addr_in_objfile (loc_addr, objfile))
7592             {
7593               loc->shlib_disabled = 1;
7594               loc->inserted = 0;
7595
7596               mark_breakpoint_location_modified (loc);
7597
7598               bp_modified = 1;
7599             }
7600         }
7601
7602       if (bp_modified)
7603         observer_notify_breakpoint_modified (b);
7604     }
7605 }
7606
7607 /* FORK & VFORK catchpoints.  */
7608
7609 /* An instance of this type is used to represent a fork or vfork
7610    catchpoint.  It includes a "struct breakpoint" as a kind of base
7611    class; users downcast to "struct breakpoint *" when needed.  A
7612    breakpoint is really of this type iff its ops pointer points to
7613    CATCH_FORK_BREAKPOINT_OPS.  */
7614
7615 struct fork_catchpoint
7616 {
7617   /* The base class.  */
7618   struct breakpoint base;
7619
7620   /* Process id of a child process whose forking triggered this
7621      catchpoint.  This field is only valid immediately after this
7622      catchpoint has triggered.  */
7623   ptid_t forked_inferior_pid;
7624 };
7625
7626 /* Implement the "insert" breakpoint_ops method for fork
7627    catchpoints.  */
7628
7629 static int
7630 insert_catch_fork (struct bp_location *bl)
7631 {
7632   return target_insert_fork_catchpoint (ptid_get_pid (inferior_ptid));
7633 }
7634
7635 /* Implement the "remove" breakpoint_ops method for fork
7636    catchpoints.  */
7637
7638 static int
7639 remove_catch_fork (struct bp_location *bl)
7640 {
7641   return target_remove_fork_catchpoint (ptid_get_pid (inferior_ptid));
7642 }
7643
7644 /* Implement the "breakpoint_hit" breakpoint_ops method for fork
7645    catchpoints.  */
7646
7647 static int
7648 breakpoint_hit_catch_fork (const struct bp_location *bl,
7649                            struct address_space *aspace, CORE_ADDR bp_addr,
7650                            const struct target_waitstatus *ws)
7651 {
7652   struct fork_catchpoint *c = (struct fork_catchpoint *) bl->owner;
7653
7654   if (ws->kind != TARGET_WAITKIND_FORKED)
7655     return 0;
7656
7657   c->forked_inferior_pid = ws->value.related_pid;
7658   return 1;
7659 }
7660
7661 /* Implement the "print_it" breakpoint_ops method for fork
7662    catchpoints.  */
7663
7664 static enum print_stop_action
7665 print_it_catch_fork (bpstat bs)
7666 {
7667   struct ui_out *uiout = current_uiout;
7668   struct breakpoint *b = bs->breakpoint_at;
7669   struct fork_catchpoint *c = (struct fork_catchpoint *) bs->breakpoint_at;
7670
7671   annotate_catchpoint (b->number);
7672   if (b->disposition == disp_del)
7673     ui_out_text (uiout, "\nTemporary catchpoint ");
7674   else
7675     ui_out_text (uiout, "\nCatchpoint ");
7676   if (ui_out_is_mi_like_p (uiout))
7677     {
7678       ui_out_field_string (uiout, "reason",
7679                            async_reason_lookup (EXEC_ASYNC_FORK));
7680       ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
7681     }
7682   ui_out_field_int (uiout, "bkptno", b->number);
7683   ui_out_text (uiout, " (forked process ");
7684   ui_out_field_int (uiout, "newpid", ptid_get_pid (c->forked_inferior_pid));
7685   ui_out_text (uiout, "), ");
7686   return PRINT_SRC_AND_LOC;
7687 }
7688
7689 /* Implement the "print_one" breakpoint_ops method for fork
7690    catchpoints.  */
7691
7692 static void
7693 print_one_catch_fork (struct breakpoint *b, struct bp_location **last_loc)
7694 {
7695   struct fork_catchpoint *c = (struct fork_catchpoint *) b;
7696   struct value_print_options opts;
7697   struct ui_out *uiout = current_uiout;
7698
7699   get_user_print_options (&opts);
7700
7701   /* Field 4, the address, is omitted (which makes the columns not
7702      line up too nicely with the headers, but the effect is relatively
7703      readable).  */
7704   if (opts.addressprint)
7705     ui_out_field_skip (uiout, "addr");
7706   annotate_field (5);
7707   ui_out_text (uiout, "fork");
7708   if (!ptid_equal (c->forked_inferior_pid, null_ptid))
7709     {
7710       ui_out_text (uiout, ", process ");
7711       ui_out_field_int (uiout, "what",
7712                         ptid_get_pid (c->forked_inferior_pid));
7713       ui_out_spaces (uiout, 1);
7714     }
7715
7716   if (ui_out_is_mi_like_p (uiout))
7717     ui_out_field_string (uiout, "catch-type", "fork");
7718 }
7719
7720 /* Implement the "print_mention" breakpoint_ops method for fork
7721    catchpoints.  */
7722
7723 static void
7724 print_mention_catch_fork (struct breakpoint *b)
7725 {
7726   printf_filtered (_("Catchpoint %d (fork)"), b->number);
7727 }
7728
7729 /* Implement the "print_recreate" breakpoint_ops method for fork
7730    catchpoints.  */
7731
7732 static void
7733 print_recreate_catch_fork (struct breakpoint *b, struct ui_file *fp)
7734 {
7735   fprintf_unfiltered (fp, "catch fork");
7736   print_recreate_thread (b, fp);
7737 }
7738
7739 /* The breakpoint_ops structure to be used in fork catchpoints.  */
7740
7741 static struct breakpoint_ops catch_fork_breakpoint_ops;
7742
7743 /* Implement the "insert" breakpoint_ops method for vfork
7744    catchpoints.  */
7745
7746 static int
7747 insert_catch_vfork (struct bp_location *bl)
7748 {
7749   return target_insert_vfork_catchpoint (ptid_get_pid (inferior_ptid));
7750 }
7751
7752 /* Implement the "remove" breakpoint_ops method for vfork
7753    catchpoints.  */
7754
7755 static int
7756 remove_catch_vfork (struct bp_location *bl)
7757 {
7758   return target_remove_vfork_catchpoint (ptid_get_pid (inferior_ptid));
7759 }
7760
7761 /* Implement the "breakpoint_hit" breakpoint_ops method for vfork
7762    catchpoints.  */
7763
7764 static int
7765 breakpoint_hit_catch_vfork (const struct bp_location *bl,
7766                             struct address_space *aspace, CORE_ADDR bp_addr,
7767                             const struct target_waitstatus *ws)
7768 {
7769   struct fork_catchpoint *c = (struct fork_catchpoint *) bl->owner;
7770
7771   if (ws->kind != TARGET_WAITKIND_VFORKED)
7772     return 0;
7773
7774   c->forked_inferior_pid = ws->value.related_pid;
7775   return 1;
7776 }
7777
7778 /* Implement the "print_it" breakpoint_ops method for vfork
7779    catchpoints.  */
7780
7781 static enum print_stop_action
7782 print_it_catch_vfork (bpstat bs)
7783 {
7784   struct ui_out *uiout = current_uiout;
7785   struct breakpoint *b = bs->breakpoint_at;
7786   struct fork_catchpoint *c = (struct fork_catchpoint *) b;
7787
7788   annotate_catchpoint (b->number);
7789   if (b->disposition == disp_del)
7790     ui_out_text (uiout, "\nTemporary catchpoint ");
7791   else
7792     ui_out_text (uiout, "\nCatchpoint ");
7793   if (ui_out_is_mi_like_p (uiout))
7794     {
7795       ui_out_field_string (uiout, "reason",
7796                            async_reason_lookup (EXEC_ASYNC_VFORK));
7797       ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
7798     }
7799   ui_out_field_int (uiout, "bkptno", b->number);
7800   ui_out_text (uiout, " (vforked process ");
7801   ui_out_field_int (uiout, "newpid", ptid_get_pid (c->forked_inferior_pid));
7802   ui_out_text (uiout, "), ");
7803   return PRINT_SRC_AND_LOC;
7804 }
7805
7806 /* Implement the "print_one" breakpoint_ops method for vfork
7807    catchpoints.  */
7808
7809 static void
7810 print_one_catch_vfork (struct breakpoint *b, struct bp_location **last_loc)
7811 {
7812   struct fork_catchpoint *c = (struct fork_catchpoint *) b;
7813   struct value_print_options opts;
7814   struct ui_out *uiout = current_uiout;
7815
7816   get_user_print_options (&opts);
7817   /* Field 4, the address, is omitted (which makes the columns not
7818      line up too nicely with the headers, but the effect is relatively
7819      readable).  */
7820   if (opts.addressprint)
7821     ui_out_field_skip (uiout, "addr");
7822   annotate_field (5);
7823   ui_out_text (uiout, "vfork");
7824   if (!ptid_equal (c->forked_inferior_pid, null_ptid))
7825     {
7826       ui_out_text (uiout, ", process ");
7827       ui_out_field_int (uiout, "what",
7828                         ptid_get_pid (c->forked_inferior_pid));
7829       ui_out_spaces (uiout, 1);
7830     }
7831
7832   if (ui_out_is_mi_like_p (uiout))
7833     ui_out_field_string (uiout, "catch-type", "vfork");
7834 }
7835
7836 /* Implement the "print_mention" breakpoint_ops method for vfork
7837    catchpoints.  */
7838
7839 static void
7840 print_mention_catch_vfork (struct breakpoint *b)
7841 {
7842   printf_filtered (_("Catchpoint %d (vfork)"), b->number);
7843 }
7844
7845 /* Implement the "print_recreate" breakpoint_ops method for vfork
7846    catchpoints.  */
7847
7848 static void
7849 print_recreate_catch_vfork (struct breakpoint *b, struct ui_file *fp)
7850 {
7851   fprintf_unfiltered (fp, "catch vfork");
7852   print_recreate_thread (b, fp);
7853 }
7854
7855 /* The breakpoint_ops structure to be used in vfork catchpoints.  */
7856
7857 static struct breakpoint_ops catch_vfork_breakpoint_ops;
7858
7859 /* An instance of this type is used to represent an solib catchpoint.
7860    It includes a "struct breakpoint" as a kind of base class; users
7861    downcast to "struct breakpoint *" when needed.  A breakpoint is
7862    really of this type iff its ops pointer points to
7863    CATCH_SOLIB_BREAKPOINT_OPS.  */
7864
7865 struct solib_catchpoint
7866 {
7867   /* The base class.  */
7868   struct breakpoint base;
7869
7870   /* True for "catch load", false for "catch unload".  */
7871   unsigned char is_load;
7872
7873   /* Regular expression to match, if any.  COMPILED is only valid when
7874      REGEX is non-NULL.  */
7875   char *regex;
7876   regex_t compiled;
7877 };
7878
7879 static void
7880 dtor_catch_solib (struct breakpoint *b)
7881 {
7882   struct solib_catchpoint *self = (struct solib_catchpoint *) b;
7883
7884   if (self->regex)
7885     regfree (&self->compiled);
7886   xfree (self->regex);
7887
7888   base_breakpoint_ops.dtor (b);
7889 }
7890
7891 static int
7892 insert_catch_solib (struct bp_location *ignore)
7893 {
7894   return 0;
7895 }
7896
7897 static int
7898 remove_catch_solib (struct bp_location *ignore)
7899 {
7900   return 0;
7901 }
7902
7903 static int
7904 breakpoint_hit_catch_solib (const struct bp_location *bl,
7905                             struct address_space *aspace,
7906                             CORE_ADDR bp_addr,
7907                             const struct target_waitstatus *ws)
7908 {
7909   struct solib_catchpoint *self = (struct solib_catchpoint *) bl->owner;
7910   struct breakpoint *other;
7911
7912   if (ws->kind == TARGET_WAITKIND_LOADED)
7913     return 1;
7914
7915   ALL_BREAKPOINTS (other)
7916   {
7917     struct bp_location *other_bl;
7918
7919     if (other == bl->owner)
7920       continue;
7921
7922     if (other->type != bp_shlib_event)
7923       continue;
7924
7925     if (self->base.pspace != NULL && other->pspace != self->base.pspace)
7926       continue;
7927
7928     for (other_bl = other->loc; other_bl != NULL; other_bl = other_bl->next)
7929       {
7930         if (other->ops->breakpoint_hit (other_bl, aspace, bp_addr, ws))
7931           return 1;
7932       }
7933   }
7934
7935   return 0;
7936 }
7937
7938 static void
7939 check_status_catch_solib (struct bpstats *bs)
7940 {
7941   struct solib_catchpoint *self
7942     = (struct solib_catchpoint *) bs->breakpoint_at;
7943   int ix;
7944
7945   if (self->is_load)
7946     {
7947       struct so_list *iter;
7948
7949       for (ix = 0;
7950            VEC_iterate (so_list_ptr, current_program_space->added_solibs,
7951                         ix, iter);
7952            ++ix)
7953         {
7954           if (!self->regex
7955               || regexec (&self->compiled, iter->so_name, 0, NULL, 0) == 0)
7956             return;
7957         }
7958     }
7959   else
7960     {
7961       char *iter;
7962
7963       for (ix = 0;
7964            VEC_iterate (char_ptr, current_program_space->deleted_solibs,
7965                         ix, iter);
7966            ++ix)
7967         {
7968           if (!self->regex
7969               || regexec (&self->compiled, iter, 0, NULL, 0) == 0)
7970             return;
7971         }
7972     }
7973
7974   bs->stop = 0;
7975   bs->print_it = print_it_noop;
7976 }
7977
7978 static enum print_stop_action
7979 print_it_catch_solib (bpstat bs)
7980 {
7981   struct breakpoint *b = bs->breakpoint_at;
7982   struct ui_out *uiout = current_uiout;
7983
7984   annotate_catchpoint (b->number);
7985   if (b->disposition == disp_del)
7986     ui_out_text (uiout, "\nTemporary catchpoint ");
7987   else
7988     ui_out_text (uiout, "\nCatchpoint ");
7989   ui_out_field_int (uiout, "bkptno", b->number);
7990   ui_out_text (uiout, "\n");
7991   if (ui_out_is_mi_like_p (uiout))
7992     ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
7993   print_solib_event (1);
7994   return PRINT_SRC_AND_LOC;
7995 }
7996
7997 static void
7998 print_one_catch_solib (struct breakpoint *b, struct bp_location **locs)
7999 {
8000   struct solib_catchpoint *self = (struct solib_catchpoint *) b;
8001   struct value_print_options opts;
8002   struct ui_out *uiout = current_uiout;
8003   char *msg;
8004
8005   get_user_print_options (&opts);
8006   /* Field 4, the address, is omitted (which makes the columns not
8007      line up too nicely with the headers, but the effect is relatively
8008      readable).  */
8009   if (opts.addressprint)
8010     {
8011       annotate_field (4);
8012       ui_out_field_skip (uiout, "addr");
8013     }
8014
8015   annotate_field (5);
8016   if (self->is_load)
8017     {
8018       if (self->regex)
8019         msg = xstrprintf (_("load of library matching %s"), self->regex);
8020       else
8021         msg = xstrdup (_("load of library"));
8022     }
8023   else
8024     {
8025       if (self->regex)
8026         msg = xstrprintf (_("unload of library matching %s"), self->regex);
8027       else
8028         msg = xstrdup (_("unload of library"));
8029     }
8030   ui_out_field_string (uiout, "what", msg);
8031   xfree (msg);
8032
8033   if (ui_out_is_mi_like_p (uiout))
8034     ui_out_field_string (uiout, "catch-type",
8035                          self->is_load ? "load" : "unload");
8036 }
8037
8038 static void
8039 print_mention_catch_solib (struct breakpoint *b)
8040 {
8041   struct solib_catchpoint *self = (struct solib_catchpoint *) b;
8042
8043   printf_filtered (_("Catchpoint %d (%s)"), b->number,
8044                    self->is_load ? "load" : "unload");
8045 }
8046
8047 static void
8048 print_recreate_catch_solib (struct breakpoint *b, struct ui_file *fp)
8049 {
8050   struct solib_catchpoint *self = (struct solib_catchpoint *) b;
8051
8052   fprintf_unfiltered (fp, "%s %s",
8053                       b->disposition == disp_del ? "tcatch" : "catch",
8054                       self->is_load ? "load" : "unload");
8055   if (self->regex)
8056     fprintf_unfiltered (fp, " %s", self->regex);
8057   fprintf_unfiltered (fp, "\n");
8058 }
8059
8060 static struct breakpoint_ops catch_solib_breakpoint_ops;
8061
8062 /* Shared helper function (MI and CLI) for creating and installing
8063    a shared object event catchpoint.  If IS_LOAD is non-zero then
8064    the events to be caught are load events, otherwise they are
8065    unload events.  If IS_TEMP is non-zero the catchpoint is a
8066    temporary one.  If ENABLED is non-zero the catchpoint is
8067    created in an enabled state.  */
8068
8069 void
8070 add_solib_catchpoint (char *arg, int is_load, int is_temp, int enabled)
8071 {
8072   struct solib_catchpoint *c;
8073   struct gdbarch *gdbarch = get_current_arch ();
8074   struct cleanup *cleanup;
8075
8076   if (!arg)
8077     arg = "";
8078   arg = skip_spaces (arg);
8079
8080   c = XCNEW (struct solib_catchpoint);
8081   cleanup = make_cleanup (xfree, c);
8082
8083   if (*arg != '\0')
8084     {
8085       int errcode;
8086
8087       errcode = regcomp (&c->compiled, arg, REG_NOSUB);
8088       if (errcode != 0)
8089         {
8090           char *err = get_regcomp_error (errcode, &c->compiled);
8091
8092           make_cleanup (xfree, err);
8093           error (_("Invalid regexp (%s): %s"), err, arg);
8094         }
8095       c->regex = xstrdup (arg);
8096     }
8097
8098   c->is_load = is_load;
8099   init_catchpoint (&c->base, gdbarch, is_temp, NULL,
8100                    &catch_solib_breakpoint_ops);
8101
8102   c->base.enable_state = enabled ? bp_enabled : bp_disabled;
8103
8104   discard_cleanups (cleanup);
8105   install_breakpoint (0, &c->base, 1);
8106 }
8107
8108 /* A helper function that does all the work for "catch load" and
8109    "catch unload".  */
8110
8111 static void
8112 catch_load_or_unload (char *arg, int from_tty, int is_load,
8113                       struct cmd_list_element *command)
8114 {
8115   int tempflag;
8116   const int enabled = 1;
8117
8118   tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
8119
8120   add_solib_catchpoint (arg, is_load, tempflag, enabled);
8121 }
8122
8123 static void
8124 catch_load_command_1 (char *arg, int from_tty,
8125                       struct cmd_list_element *command)
8126 {
8127   catch_load_or_unload (arg, from_tty, 1, command);
8128 }
8129
8130 static void
8131 catch_unload_command_1 (char *arg, int from_tty,
8132                         struct cmd_list_element *command)
8133 {
8134   catch_load_or_unload (arg, from_tty, 0, command);
8135 }
8136
8137 /* An instance of this type is used to represent a syscall catchpoint.
8138    It includes a "struct breakpoint" as a kind of base class; users
8139    downcast to "struct breakpoint *" when needed.  A breakpoint is
8140    really of this type iff its ops pointer points to
8141    CATCH_SYSCALL_BREAKPOINT_OPS.  */
8142
8143 struct syscall_catchpoint
8144 {
8145   /* The base class.  */
8146   struct breakpoint base;
8147
8148   /* Syscall numbers used for the 'catch syscall' feature.  If no
8149      syscall has been specified for filtering, its value is NULL.
8150      Otherwise, it holds a list of all syscalls to be caught.  The
8151      list elements are allocated with xmalloc.  */
8152   VEC(int) *syscalls_to_be_caught;
8153 };
8154
8155 /* Implement the "dtor" breakpoint_ops method for syscall
8156    catchpoints.  */
8157
8158 static void
8159 dtor_catch_syscall (struct breakpoint *b)
8160 {
8161   struct syscall_catchpoint *c = (struct syscall_catchpoint *) b;
8162
8163   VEC_free (int, c->syscalls_to_be_caught);
8164
8165   base_breakpoint_ops.dtor (b);
8166 }
8167
8168 static const struct inferior_data *catch_syscall_inferior_data = NULL;
8169
8170 struct catch_syscall_inferior_data
8171 {
8172   /* We keep a count of the number of times the user has requested a
8173      particular syscall to be tracked, and pass this information to the
8174      target.  This lets capable targets implement filtering directly.  */
8175
8176   /* Number of times that "any" syscall is requested.  */
8177   int any_syscall_count;
8178
8179   /* Count of each system call.  */
8180   VEC(int) *syscalls_counts;
8181
8182   /* This counts all syscall catch requests, so we can readily determine
8183      if any catching is necessary.  */
8184   int total_syscalls_count;
8185 };
8186
8187 static struct catch_syscall_inferior_data*
8188 get_catch_syscall_inferior_data (struct inferior *inf)
8189 {
8190   struct catch_syscall_inferior_data *inf_data;
8191
8192   inf_data = inferior_data (inf, catch_syscall_inferior_data);
8193   if (inf_data == NULL)
8194     {
8195       inf_data = XZALLOC (struct catch_syscall_inferior_data);
8196       set_inferior_data (inf, catch_syscall_inferior_data, inf_data);
8197     }
8198
8199   return inf_data;
8200 }
8201
8202 static void
8203 catch_syscall_inferior_data_cleanup (struct inferior *inf, void *arg)
8204 {
8205   xfree (arg);
8206 }
8207
8208
8209 /* Implement the "insert" breakpoint_ops method for syscall
8210    catchpoints.  */
8211
8212 static int
8213 insert_catch_syscall (struct bp_location *bl)
8214 {
8215   struct syscall_catchpoint *c = (struct syscall_catchpoint *) bl->owner;
8216   struct inferior *inf = current_inferior ();
8217   struct catch_syscall_inferior_data *inf_data
8218     = get_catch_syscall_inferior_data (inf);
8219
8220   ++inf_data->total_syscalls_count;
8221   if (!c->syscalls_to_be_caught)
8222     ++inf_data->any_syscall_count;
8223   else
8224     {
8225       int i, iter;
8226
8227       for (i = 0;
8228            VEC_iterate (int, c->syscalls_to_be_caught, i, iter);
8229            i++)
8230         {
8231           int elem;
8232
8233           if (iter >= VEC_length (int, inf_data->syscalls_counts))
8234             {
8235               int old_size = VEC_length (int, inf_data->syscalls_counts);
8236               uintptr_t vec_addr_offset
8237                 = old_size * ((uintptr_t) sizeof (int));
8238               uintptr_t vec_addr;
8239               VEC_safe_grow (int, inf_data->syscalls_counts, iter + 1);
8240               vec_addr = ((uintptr_t) VEC_address (int,
8241                                                   inf_data->syscalls_counts)
8242                           + vec_addr_offset);
8243               memset ((void *) vec_addr, 0,
8244                       (iter + 1 - old_size) * sizeof (int));
8245             }
8246           elem = VEC_index (int, inf_data->syscalls_counts, iter);
8247           VEC_replace (int, inf_data->syscalls_counts, iter, ++elem);
8248         }
8249     }
8250
8251   return target_set_syscall_catchpoint (ptid_get_pid (inferior_ptid),
8252                                         inf_data->total_syscalls_count != 0,
8253                                         inf_data->any_syscall_count,
8254                                         VEC_length (int,
8255                                                     inf_data->syscalls_counts),
8256                                         VEC_address (int,
8257                                                      inf_data->syscalls_counts));
8258 }
8259
8260 /* Implement the "remove" breakpoint_ops method for syscall
8261    catchpoints.  */
8262
8263 static int
8264 remove_catch_syscall (struct bp_location *bl)
8265 {
8266   struct syscall_catchpoint *c = (struct syscall_catchpoint *) bl->owner;
8267   struct inferior *inf = current_inferior ();
8268   struct catch_syscall_inferior_data *inf_data
8269     = get_catch_syscall_inferior_data (inf);
8270
8271   --inf_data->total_syscalls_count;
8272   if (!c->syscalls_to_be_caught)
8273     --inf_data->any_syscall_count;
8274   else
8275     {
8276       int i, iter;
8277
8278       for (i = 0;
8279            VEC_iterate (int, c->syscalls_to_be_caught, i, iter);
8280            i++)
8281         {
8282           int elem;
8283           if (iter >= VEC_length (int, inf_data->syscalls_counts))
8284             /* Shouldn't happen.  */
8285             continue;
8286           elem = VEC_index (int, inf_data->syscalls_counts, iter);
8287           VEC_replace (int, inf_data->syscalls_counts, iter, --elem);
8288         }
8289     }
8290
8291   return target_set_syscall_catchpoint (ptid_get_pid (inferior_ptid),
8292                                         inf_data->total_syscalls_count != 0,
8293                                         inf_data->any_syscall_count,
8294                                         VEC_length (int,
8295                                                     inf_data->syscalls_counts),
8296                                         VEC_address (int,
8297                                                      inf_data->syscalls_counts));
8298 }
8299
8300 /* Implement the "breakpoint_hit" breakpoint_ops method for syscall
8301    catchpoints.  */
8302
8303 static int
8304 breakpoint_hit_catch_syscall (const struct bp_location *bl,
8305                               struct address_space *aspace, CORE_ADDR bp_addr,
8306                               const struct target_waitstatus *ws)
8307 {
8308   /* We must check if we are catching specific syscalls in this
8309      breakpoint.  If we are, then we must guarantee that the called
8310      syscall is the same syscall we are catching.  */
8311   int syscall_number = 0;
8312   const struct syscall_catchpoint *c
8313     = (const struct syscall_catchpoint *) bl->owner;
8314
8315   if (ws->kind != TARGET_WAITKIND_SYSCALL_ENTRY
8316       && ws->kind != TARGET_WAITKIND_SYSCALL_RETURN)
8317     return 0;
8318
8319   syscall_number = ws->value.syscall_number;
8320
8321   /* Now, checking if the syscall is the same.  */
8322   if (c->syscalls_to_be_caught)
8323     {
8324       int i, iter;
8325
8326       for (i = 0;
8327            VEC_iterate (int, c->syscalls_to_be_caught, i, iter);
8328            i++)
8329         if (syscall_number == iter)
8330           break;
8331       /* Not the same.  */
8332       if (!iter)
8333         return 0;
8334     }
8335
8336   return 1;
8337 }
8338
8339 /* Implement the "print_it" breakpoint_ops method for syscall
8340    catchpoints.  */
8341
8342 static enum print_stop_action
8343 print_it_catch_syscall (bpstat bs)
8344 {
8345   struct ui_out *uiout = current_uiout;
8346   struct breakpoint *b = bs->breakpoint_at;
8347   /* These are needed because we want to know in which state a
8348      syscall is.  It can be in the TARGET_WAITKIND_SYSCALL_ENTRY
8349      or TARGET_WAITKIND_SYSCALL_RETURN, and depending on it we
8350      must print "called syscall" or "returned from syscall".  */
8351   ptid_t ptid;
8352   struct target_waitstatus last;
8353   struct syscall s;
8354
8355   get_last_target_status (&ptid, &last);
8356
8357   get_syscall_by_number (last.value.syscall_number, &s);
8358
8359   annotate_catchpoint (b->number);
8360
8361   if (b->disposition == disp_del)
8362     ui_out_text (uiout, "\nTemporary catchpoint ");
8363   else
8364     ui_out_text (uiout, "\nCatchpoint ");
8365   if (ui_out_is_mi_like_p (uiout))
8366     {
8367       ui_out_field_string (uiout, "reason",
8368                            async_reason_lookup (last.kind == TARGET_WAITKIND_SYSCALL_ENTRY
8369                                                 ? EXEC_ASYNC_SYSCALL_ENTRY
8370                                                 : EXEC_ASYNC_SYSCALL_RETURN));
8371       ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
8372     }
8373   ui_out_field_int (uiout, "bkptno", b->number);
8374
8375   if (last.kind == TARGET_WAITKIND_SYSCALL_ENTRY)
8376     ui_out_text (uiout, " (call to syscall ");
8377   else
8378     ui_out_text (uiout, " (returned from syscall ");
8379
8380   if (s.name == NULL || ui_out_is_mi_like_p (uiout))
8381     ui_out_field_int (uiout, "syscall-number", last.value.syscall_number);
8382   if (s.name != NULL)
8383     ui_out_field_string (uiout, "syscall-name", s.name);
8384
8385   ui_out_text (uiout, "), ");
8386
8387   return PRINT_SRC_AND_LOC;
8388 }
8389
8390 /* Implement the "print_one" breakpoint_ops method for syscall
8391    catchpoints.  */
8392
8393 static void
8394 print_one_catch_syscall (struct breakpoint *b,
8395                          struct bp_location **last_loc)
8396 {
8397   struct syscall_catchpoint *c = (struct syscall_catchpoint *) b;
8398   struct value_print_options opts;
8399   struct ui_out *uiout = current_uiout;
8400
8401   get_user_print_options (&opts);
8402   /* Field 4, the address, is omitted (which makes the columns not
8403      line up too nicely with the headers, but the effect is relatively
8404      readable).  */
8405   if (opts.addressprint)
8406     ui_out_field_skip (uiout, "addr");
8407   annotate_field (5);
8408
8409   if (c->syscalls_to_be_caught
8410       && VEC_length (int, c->syscalls_to_be_caught) > 1)
8411     ui_out_text (uiout, "syscalls \"");
8412   else
8413     ui_out_text (uiout, "syscall \"");
8414
8415   if (c->syscalls_to_be_caught)
8416     {
8417       int i, iter;
8418       char *text = xstrprintf ("%s", "");
8419
8420       for (i = 0;
8421            VEC_iterate (int, c->syscalls_to_be_caught, i, iter);
8422            i++)
8423         {
8424           char *x = text;
8425           struct syscall s;
8426           get_syscall_by_number (iter, &s);
8427
8428           if (s.name != NULL)
8429             text = xstrprintf ("%s%s, ", text, s.name);
8430           else
8431             text = xstrprintf ("%s%d, ", text, iter);
8432
8433           /* We have to xfree the last 'text' (now stored at 'x')
8434              because xstrprintf dynamically allocates new space for it
8435              on every call.  */
8436           xfree (x);
8437         }
8438       /* Remove the last comma.  */
8439       text[strlen (text) - 2] = '\0';
8440       ui_out_field_string (uiout, "what", text);
8441     }
8442   else
8443     ui_out_field_string (uiout, "what", "<any syscall>");
8444   ui_out_text (uiout, "\" ");
8445
8446   if (ui_out_is_mi_like_p (uiout))
8447     ui_out_field_string (uiout, "catch-type", "syscall");
8448 }
8449
8450 /* Implement the "print_mention" breakpoint_ops method for syscall
8451    catchpoints.  */
8452
8453 static void
8454 print_mention_catch_syscall (struct breakpoint *b)
8455 {
8456   struct syscall_catchpoint *c = (struct syscall_catchpoint *) b;
8457
8458   if (c->syscalls_to_be_caught)
8459     {
8460       int i, iter;
8461
8462       if (VEC_length (int, c->syscalls_to_be_caught) > 1)
8463         printf_filtered (_("Catchpoint %d (syscalls"), b->number);
8464       else
8465         printf_filtered (_("Catchpoint %d (syscall"), b->number);
8466
8467       for (i = 0;
8468            VEC_iterate (int, c->syscalls_to_be_caught, i, iter);
8469            i++)
8470         {
8471           struct syscall s;
8472           get_syscall_by_number (iter, &s);
8473
8474           if (s.name)
8475             printf_filtered (" '%s' [%d]", s.name, s.number);
8476           else
8477             printf_filtered (" %d", s.number);
8478         }
8479       printf_filtered (")");
8480     }
8481   else
8482     printf_filtered (_("Catchpoint %d (any syscall)"),
8483                      b->number);
8484 }
8485
8486 /* Implement the "print_recreate" breakpoint_ops method for syscall
8487    catchpoints.  */
8488
8489 static void
8490 print_recreate_catch_syscall (struct breakpoint *b, struct ui_file *fp)
8491 {
8492   struct syscall_catchpoint *c = (struct syscall_catchpoint *) b;
8493
8494   fprintf_unfiltered (fp, "catch syscall");
8495
8496   if (c->syscalls_to_be_caught)
8497     {
8498       int i, iter;
8499
8500       for (i = 0;
8501            VEC_iterate (int, c->syscalls_to_be_caught, i, iter);
8502            i++)
8503         {
8504           struct syscall s;
8505
8506           get_syscall_by_number (iter, &s);
8507           if (s.name)
8508             fprintf_unfiltered (fp, " %s", s.name);
8509           else
8510             fprintf_unfiltered (fp, " %d", s.number);
8511         }
8512     }
8513   print_recreate_thread (b, fp);
8514 }
8515
8516 /* The breakpoint_ops structure to be used in syscall catchpoints.  */
8517
8518 static struct breakpoint_ops catch_syscall_breakpoint_ops;
8519
8520 /* Returns non-zero if 'b' is a syscall catchpoint.  */
8521
8522 static int
8523 syscall_catchpoint_p (struct breakpoint *b)
8524 {
8525   return (b->ops == &catch_syscall_breakpoint_ops);
8526 }
8527
8528 /* Initialize a new breakpoint of the bp_catchpoint kind.  If TEMPFLAG
8529    is non-zero, then make the breakpoint temporary.  If COND_STRING is
8530    not NULL, then store it in the breakpoint.  OPS, if not NULL, is
8531    the breakpoint_ops structure associated to the catchpoint.  */
8532
8533 void
8534 init_catchpoint (struct breakpoint *b,
8535                  struct gdbarch *gdbarch, int tempflag,
8536                  char *cond_string,
8537                  const struct breakpoint_ops *ops)
8538 {
8539   struct symtab_and_line sal;
8540
8541   init_sal (&sal);
8542   sal.pspace = current_program_space;
8543
8544   init_raw_breakpoint (b, gdbarch, sal, bp_catchpoint, ops);
8545
8546   b->cond_string = (cond_string == NULL) ? NULL : xstrdup (cond_string);
8547   b->disposition = tempflag ? disp_del : disp_donttouch;
8548 }
8549
8550 void
8551 install_breakpoint (int internal, struct breakpoint *b, int update_gll)
8552 {
8553   add_to_breakpoint_chain (b);
8554   set_breakpoint_number (internal, b);
8555   if (is_tracepoint (b))
8556     set_tracepoint_count (breakpoint_count);
8557   if (!internal)
8558     mention (b);
8559   observer_notify_breakpoint_created (b);
8560
8561   if (update_gll)
8562     update_global_location_list (1);
8563 }
8564
8565 static void
8566 create_fork_vfork_event_catchpoint (struct gdbarch *gdbarch,
8567                                     int tempflag, char *cond_string,
8568                                     const struct breakpoint_ops *ops)
8569 {
8570   struct fork_catchpoint *c = XNEW (struct fork_catchpoint);
8571
8572   init_catchpoint (&c->base, gdbarch, tempflag, cond_string, ops);
8573
8574   c->forked_inferior_pid = null_ptid;
8575
8576   install_breakpoint (0, &c->base, 1);
8577 }
8578
8579 /* Exec catchpoints.  */
8580
8581 /* An instance of this type is used to represent an exec catchpoint.
8582    It includes a "struct breakpoint" as a kind of base class; users
8583    downcast to "struct breakpoint *" when needed.  A breakpoint is
8584    really of this type iff its ops pointer points to
8585    CATCH_EXEC_BREAKPOINT_OPS.  */
8586
8587 struct exec_catchpoint
8588 {
8589   /* The base class.  */
8590   struct breakpoint base;
8591
8592   /* Filename of a program whose exec triggered this catchpoint.
8593      This field is only valid immediately after this catchpoint has
8594      triggered.  */
8595   char *exec_pathname;
8596 };
8597
8598 /* Implement the "dtor" breakpoint_ops method for exec
8599    catchpoints.  */
8600
8601 static void
8602 dtor_catch_exec (struct breakpoint *b)
8603 {
8604   struct exec_catchpoint *c = (struct exec_catchpoint *) b;
8605
8606   xfree (c->exec_pathname);
8607
8608   base_breakpoint_ops.dtor (b);
8609 }
8610
8611 static int
8612 insert_catch_exec (struct bp_location *bl)
8613 {
8614   return target_insert_exec_catchpoint (ptid_get_pid (inferior_ptid));
8615 }
8616
8617 static int
8618 remove_catch_exec (struct bp_location *bl)
8619 {
8620   return target_remove_exec_catchpoint (ptid_get_pid (inferior_ptid));
8621 }
8622
8623 static int
8624 breakpoint_hit_catch_exec (const struct bp_location *bl,
8625                            struct address_space *aspace, CORE_ADDR bp_addr,
8626                            const struct target_waitstatus *ws)
8627 {
8628   struct exec_catchpoint *c = (struct exec_catchpoint *) bl->owner;
8629
8630   if (ws->kind != TARGET_WAITKIND_EXECD)
8631     return 0;
8632
8633   c->exec_pathname = xstrdup (ws->value.execd_pathname);
8634   return 1;
8635 }
8636
8637 static enum print_stop_action
8638 print_it_catch_exec (bpstat bs)
8639 {
8640   struct ui_out *uiout = current_uiout;
8641   struct breakpoint *b = bs->breakpoint_at;
8642   struct exec_catchpoint *c = (struct exec_catchpoint *) b;
8643
8644   annotate_catchpoint (b->number);
8645   if (b->disposition == disp_del)
8646     ui_out_text (uiout, "\nTemporary catchpoint ");
8647   else
8648     ui_out_text (uiout, "\nCatchpoint ");
8649   if (ui_out_is_mi_like_p (uiout))
8650     {
8651       ui_out_field_string (uiout, "reason",
8652                            async_reason_lookup (EXEC_ASYNC_EXEC));
8653       ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
8654     }
8655   ui_out_field_int (uiout, "bkptno", b->number);
8656   ui_out_text (uiout, " (exec'd ");
8657   ui_out_field_string (uiout, "new-exec", c->exec_pathname);
8658   ui_out_text (uiout, "), ");
8659
8660   return PRINT_SRC_AND_LOC;
8661 }
8662
8663 static void
8664 print_one_catch_exec (struct breakpoint *b, struct bp_location **last_loc)
8665 {
8666   struct exec_catchpoint *c = (struct exec_catchpoint *) b;
8667   struct value_print_options opts;
8668   struct ui_out *uiout = current_uiout;
8669
8670   get_user_print_options (&opts);
8671
8672   /* Field 4, the address, is omitted (which makes the columns
8673      not line up too nicely with the headers, but the effect
8674      is relatively readable).  */
8675   if (opts.addressprint)
8676     ui_out_field_skip (uiout, "addr");
8677   annotate_field (5);
8678   ui_out_text (uiout, "exec");
8679   if (c->exec_pathname != NULL)
8680     {
8681       ui_out_text (uiout, ", program \"");
8682       ui_out_field_string (uiout, "what", c->exec_pathname);
8683       ui_out_text (uiout, "\" ");
8684     }
8685
8686   if (ui_out_is_mi_like_p (uiout))
8687     ui_out_field_string (uiout, "catch-type", "exec");
8688 }
8689
8690 static void
8691 print_mention_catch_exec (struct breakpoint *b)
8692 {
8693   printf_filtered (_("Catchpoint %d (exec)"), b->number);
8694 }
8695
8696 /* Implement the "print_recreate" breakpoint_ops method for exec
8697    catchpoints.  */
8698
8699 static void
8700 print_recreate_catch_exec (struct breakpoint *b, struct ui_file *fp)
8701 {
8702   fprintf_unfiltered (fp, "catch exec");
8703   print_recreate_thread (b, fp);
8704 }
8705
8706 static struct breakpoint_ops catch_exec_breakpoint_ops;
8707
8708 static void
8709 create_syscall_event_catchpoint (int tempflag, VEC(int) *filter,
8710                                  const struct breakpoint_ops *ops)
8711 {
8712   struct syscall_catchpoint *c;
8713   struct gdbarch *gdbarch = get_current_arch ();
8714
8715   c = XNEW (struct syscall_catchpoint);
8716   init_catchpoint (&c->base, gdbarch, tempflag, NULL, ops);
8717   c->syscalls_to_be_caught = filter;
8718
8719   install_breakpoint (0, &c->base, 1);
8720 }
8721
8722 static int
8723 hw_breakpoint_used_count (void)
8724 {
8725   int i = 0;
8726   struct breakpoint *b;
8727   struct bp_location *bl;
8728
8729   ALL_BREAKPOINTS (b)
8730   {
8731     if (b->type == bp_hardware_breakpoint && breakpoint_enabled (b))
8732       for (bl = b->loc; bl; bl = bl->next)
8733         {
8734           /* Special types of hardware breakpoints may use more than
8735              one register.  */
8736           i += b->ops->resources_needed (bl);
8737         }
8738   }
8739
8740   return i;
8741 }
8742
8743 /* Returns the resources B would use if it were a hardware
8744    watchpoint.  */
8745
8746 static int
8747 hw_watchpoint_use_count (struct breakpoint *b)
8748 {
8749   int i = 0;
8750   struct bp_location *bl;
8751
8752   if (!breakpoint_enabled (b))
8753     return 0;
8754
8755   for (bl = b->loc; bl; bl = bl->next)
8756     {
8757       /* Special types of hardware watchpoints may use more than
8758          one register.  */
8759       i += b->ops->resources_needed (bl);
8760     }
8761
8762   return i;
8763 }
8764
8765 /* Returns the sum the used resources of all hardware watchpoints of
8766    type TYPE in the breakpoints list.  Also returns in OTHER_TYPE_USED
8767    the sum of the used resources of all hardware watchpoints of other
8768    types _not_ TYPE.  */
8769
8770 static int
8771 hw_watchpoint_used_count_others (struct breakpoint *except,
8772                                  enum bptype type, int *other_type_used)
8773 {
8774   int i = 0;
8775   struct breakpoint *b;
8776
8777   *other_type_used = 0;
8778   ALL_BREAKPOINTS (b)
8779     {
8780       if (b == except)
8781         continue;
8782       if (!breakpoint_enabled (b))
8783         continue;
8784
8785       if (b->type == type)
8786         i += hw_watchpoint_use_count (b);
8787       else if (is_hardware_watchpoint (b))
8788         *other_type_used = 1;
8789     }
8790
8791   return i;
8792 }
8793
8794 void
8795 disable_watchpoints_before_interactive_call_start (void)
8796 {
8797   struct breakpoint *b;
8798
8799   ALL_BREAKPOINTS (b)
8800   {
8801     if (is_watchpoint (b) && breakpoint_enabled (b))
8802       {
8803         b->enable_state = bp_call_disabled;
8804         update_global_location_list (0);
8805       }
8806   }
8807 }
8808
8809 void
8810 enable_watchpoints_after_interactive_call_stop (void)
8811 {
8812   struct breakpoint *b;
8813
8814   ALL_BREAKPOINTS (b)
8815   {
8816     if (is_watchpoint (b) && b->enable_state == bp_call_disabled)
8817       {
8818         b->enable_state = bp_enabled;
8819         update_global_location_list (1);
8820       }
8821   }
8822 }
8823
8824 void
8825 disable_breakpoints_before_startup (void)
8826 {
8827   current_program_space->executing_startup = 1;
8828   update_global_location_list (0);
8829 }
8830
8831 void
8832 enable_breakpoints_after_startup (void)
8833 {
8834   current_program_space->executing_startup = 0;
8835   breakpoint_re_set ();
8836 }
8837
8838
8839 /* Set a breakpoint that will evaporate an end of command
8840    at address specified by SAL.
8841    Restrict it to frame FRAME if FRAME is nonzero.  */
8842
8843 struct breakpoint *
8844 set_momentary_breakpoint (struct gdbarch *gdbarch, struct symtab_and_line sal,
8845                           struct frame_id frame_id, enum bptype type)
8846 {
8847   struct breakpoint *b;
8848
8849   /* If FRAME_ID is valid, it should be a real frame, not an inlined or
8850      tail-called one.  */
8851   gdb_assert (!frame_id_artificial_p (frame_id));
8852
8853   b = set_raw_breakpoint (gdbarch, sal, type, &momentary_breakpoint_ops);
8854   b->enable_state = bp_enabled;
8855   b->disposition = disp_donttouch;
8856   b->frame_id = frame_id;
8857
8858   /* If we're debugging a multi-threaded program, then we want
8859      momentary breakpoints to be active in only a single thread of
8860      control.  */
8861   if (in_thread_list (inferior_ptid))
8862     b->thread = pid_to_thread_id (inferior_ptid);
8863
8864   update_global_location_list_nothrow (1);
8865
8866   return b;
8867 }
8868
8869 /* Make a momentary breakpoint based on the master breakpoint ORIG.
8870    The new breakpoint will have type TYPE, and use OPS as it
8871    breakpoint_ops.  */
8872
8873 static struct breakpoint *
8874 momentary_breakpoint_from_master (struct breakpoint *orig,
8875                                   enum bptype type,
8876                                   const struct breakpoint_ops *ops)
8877 {
8878   struct breakpoint *copy;
8879
8880   copy = set_raw_breakpoint_without_location (orig->gdbarch, type, ops);
8881   copy->loc = allocate_bp_location (copy);
8882   set_breakpoint_location_function (copy->loc, 1);
8883
8884   copy->loc->gdbarch = orig->loc->gdbarch;
8885   copy->loc->requested_address = orig->loc->requested_address;
8886   copy->loc->address = orig->loc->address;
8887   copy->loc->section = orig->loc->section;
8888   copy->loc->pspace = orig->loc->pspace;
8889   copy->loc->probe = orig->loc->probe;
8890   copy->loc->line_number = orig->loc->line_number;
8891   copy->loc->symtab = orig->loc->symtab;
8892   copy->frame_id = orig->frame_id;
8893   copy->thread = orig->thread;
8894   copy->pspace = orig->pspace;
8895
8896   copy->enable_state = bp_enabled;
8897   copy->disposition = disp_donttouch;
8898   copy->number = internal_breakpoint_number--;
8899
8900   update_global_location_list_nothrow (0);
8901   return copy;
8902 }
8903
8904 /* Make a deep copy of momentary breakpoint ORIG.  Returns NULL if
8905    ORIG is NULL.  */
8906
8907 struct breakpoint *
8908 clone_momentary_breakpoint (struct breakpoint *orig)
8909 {
8910   /* If there's nothing to clone, then return nothing.  */
8911   if (orig == NULL)
8912     return NULL;
8913
8914   return momentary_breakpoint_from_master (orig, orig->type, orig->ops);
8915 }
8916
8917 struct breakpoint *
8918 set_momentary_breakpoint_at_pc (struct gdbarch *gdbarch, CORE_ADDR pc,
8919                                 enum bptype type)
8920 {
8921   struct symtab_and_line sal;
8922
8923   sal = find_pc_line (pc, 0);
8924   sal.pc = pc;
8925   sal.section = find_pc_overlay (pc);
8926   sal.explicit_pc = 1;
8927
8928   return set_momentary_breakpoint (gdbarch, sal, null_frame_id, type);
8929 }
8930 \f
8931
8932 /* Tell the user we have just set a breakpoint B.  */
8933
8934 static void
8935 mention (struct breakpoint *b)
8936 {
8937   b->ops->print_mention (b);
8938   if (ui_out_is_mi_like_p (current_uiout))
8939     return;
8940   printf_filtered ("\n");
8941 }
8942 \f
8943
8944 static struct bp_location *
8945 add_location_to_breakpoint (struct breakpoint *b,
8946                             const struct symtab_and_line *sal)
8947 {
8948   struct bp_location *loc, **tmp;
8949   CORE_ADDR adjusted_address;
8950   struct gdbarch *loc_gdbarch = get_sal_arch (*sal);
8951
8952   if (loc_gdbarch == NULL)
8953     loc_gdbarch = b->gdbarch;
8954
8955   /* Adjust the breakpoint's address prior to allocating a location.
8956      Once we call allocate_bp_location(), that mostly uninitialized
8957      location will be placed on the location chain.  Adjustment of the
8958      breakpoint may cause target_read_memory() to be called and we do
8959      not want its scan of the location chain to find a breakpoint and
8960      location that's only been partially initialized.  */
8961   adjusted_address = adjust_breakpoint_address (loc_gdbarch,
8962                                                 sal->pc, b->type);
8963
8964   /* Sort the locations by their ADDRESS.  */
8965   loc = allocate_bp_location (b);
8966   for (tmp = &(b->loc); *tmp != NULL && (*tmp)->address <= adjusted_address;
8967        tmp = &((*tmp)->next))
8968     ;
8969   loc->next = *tmp;
8970   *tmp = loc;
8971
8972   loc->requested_address = sal->pc;
8973   loc->address = adjusted_address;
8974   loc->pspace = sal->pspace;
8975   loc->probe = sal->probe;
8976   gdb_assert (loc->pspace != NULL);
8977   loc->section = sal->section;
8978   loc->gdbarch = loc_gdbarch;
8979   loc->line_number = sal->line;
8980   loc->symtab = sal->symtab;
8981
8982   set_breakpoint_location_function (loc,
8983                                     sal->explicit_pc || sal->explicit_line);
8984   return loc;
8985 }
8986 \f
8987
8988 /* Return 1 if LOC is pointing to a permanent breakpoint, 
8989    return 0 otherwise.  */
8990
8991 static int
8992 bp_loc_is_permanent (struct bp_location *loc)
8993 {
8994   int len;
8995   CORE_ADDR addr;
8996   const gdb_byte *bpoint;
8997   gdb_byte *target_mem;
8998   struct cleanup *cleanup;
8999   int retval = 0;
9000
9001   gdb_assert (loc != NULL);
9002
9003   addr = loc->address;
9004   bpoint = gdbarch_breakpoint_from_pc (loc->gdbarch, &addr, &len);
9005
9006   /* Software breakpoints unsupported?  */
9007   if (bpoint == NULL)
9008     return 0;
9009
9010   target_mem = alloca (len);
9011
9012   /* Enable the automatic memory restoration from breakpoints while
9013      we read the memory.  Otherwise we could say about our temporary
9014      breakpoints they are permanent.  */
9015   cleanup = save_current_space_and_thread ();
9016
9017   switch_to_program_space_and_thread (loc->pspace);
9018   make_show_memory_breakpoints_cleanup (0);
9019
9020   if (target_read_memory (loc->address, target_mem, len) == 0
9021       && memcmp (target_mem, bpoint, len) == 0)
9022     retval = 1;
9023
9024   do_cleanups (cleanup);
9025
9026   return retval;
9027 }
9028
9029 /* Build a command list for the dprintf corresponding to the current
9030    settings of the dprintf style options.  */
9031
9032 static void
9033 update_dprintf_command_list (struct breakpoint *b)
9034 {
9035   char *dprintf_args = b->extra_string;
9036   char *printf_line = NULL;
9037
9038   if (!dprintf_args)
9039     return;
9040
9041   dprintf_args = skip_spaces (dprintf_args);
9042
9043   /* Allow a comma, as it may have terminated a location, but don't
9044      insist on it.  */
9045   if (*dprintf_args == ',')
9046     ++dprintf_args;
9047   dprintf_args = skip_spaces (dprintf_args);
9048
9049   if (*dprintf_args != '"')
9050     error (_("Bad format string, missing '\"'."));
9051
9052   if (strcmp (dprintf_style, dprintf_style_gdb) == 0)
9053     printf_line = xstrprintf ("printf %s", dprintf_args);
9054   else if (strcmp (dprintf_style, dprintf_style_call) == 0)
9055     {
9056       if (!dprintf_function)
9057         error (_("No function supplied for dprintf call"));
9058
9059       if (dprintf_channel && strlen (dprintf_channel) > 0)
9060         printf_line = xstrprintf ("call (void) %s (%s,%s)",
9061                                   dprintf_function,
9062                                   dprintf_channel,
9063                                   dprintf_args);
9064       else
9065         printf_line = xstrprintf ("call (void) %s (%s)",
9066                                   dprintf_function,
9067                                   dprintf_args);
9068     }
9069   else if (strcmp (dprintf_style, dprintf_style_agent) == 0)
9070     {
9071       if (target_can_run_breakpoint_commands ())
9072         printf_line = xstrprintf ("agent-printf %s", dprintf_args);
9073       else
9074         {
9075           warning (_("Target cannot run dprintf commands, falling back to GDB printf"));
9076           printf_line = xstrprintf ("printf %s", dprintf_args);
9077         }
9078     }
9079   else
9080     internal_error (__FILE__, __LINE__,
9081                     _("Invalid dprintf style."));
9082
9083   gdb_assert (printf_line != NULL);
9084   /* Manufacture a printf sequence.  */
9085   {
9086     struct command_line *printf_cmd_line
9087       = xmalloc (sizeof (struct command_line));
9088
9089     printf_cmd_line = xmalloc (sizeof (struct command_line));
9090     printf_cmd_line->control_type = simple_control;
9091     printf_cmd_line->body_count = 0;
9092     printf_cmd_line->body_list = NULL;
9093     printf_cmd_line->next = NULL;
9094     printf_cmd_line->line = printf_line;
9095
9096     breakpoint_set_commands (b, printf_cmd_line);
9097   }
9098 }
9099
9100 /* Update all dprintf commands, making their command lists reflect
9101    current style settings.  */
9102
9103 static void
9104 update_dprintf_commands (char *args, int from_tty,
9105                          struct cmd_list_element *c)
9106 {
9107   struct breakpoint *b;
9108
9109   ALL_BREAKPOINTS (b)
9110     {
9111       if (b->type == bp_dprintf)
9112         update_dprintf_command_list (b);
9113     }
9114 }
9115
9116 /* Create a breakpoint with SAL as location.  Use ADDR_STRING
9117    as textual description of the location, and COND_STRING
9118    as condition expression.  */
9119
9120 static void
9121 init_breakpoint_sal (struct breakpoint *b, struct gdbarch *gdbarch,
9122                      struct symtabs_and_lines sals, char *addr_string,
9123                      char *filter, char *cond_string,
9124                      char *extra_string,
9125                      enum bptype type, enum bpdisp disposition,
9126                      int thread, int task, int ignore_count,
9127                      const struct breakpoint_ops *ops, int from_tty,
9128                      int enabled, int internal, unsigned flags,
9129                      int display_canonical)
9130 {
9131   int i;
9132
9133   if (type == bp_hardware_breakpoint)
9134     {
9135       int target_resources_ok;
9136
9137       i = hw_breakpoint_used_count ();
9138       target_resources_ok =
9139         target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
9140                                             i + 1, 0);
9141       if (target_resources_ok == 0)
9142         error (_("No hardware breakpoint support in the target."));
9143       else if (target_resources_ok < 0)
9144         error (_("Hardware breakpoints used exceeds limit."));
9145     }
9146
9147   gdb_assert (sals.nelts > 0);
9148
9149   for (i = 0; i < sals.nelts; ++i)
9150     {
9151       struct symtab_and_line sal = sals.sals[i];
9152       struct bp_location *loc;
9153
9154       if (from_tty)
9155         {
9156           struct gdbarch *loc_gdbarch = get_sal_arch (sal);
9157           if (!loc_gdbarch)
9158             loc_gdbarch = gdbarch;
9159
9160           describe_other_breakpoints (loc_gdbarch,
9161                                       sal.pspace, sal.pc, sal.section, thread);
9162         }
9163
9164       if (i == 0)
9165         {
9166           init_raw_breakpoint (b, gdbarch, sal, type, ops);
9167           b->thread = thread;
9168           b->task = task;
9169
9170           b->cond_string = cond_string;
9171           b->extra_string = extra_string;
9172           b->ignore_count = ignore_count;
9173           b->enable_state = enabled ? bp_enabled : bp_disabled;
9174           b->disposition = disposition;
9175
9176           if ((flags & CREATE_BREAKPOINT_FLAGS_INSERTED) != 0)
9177             b->loc->inserted = 1;
9178
9179           if (type == bp_static_tracepoint)
9180             {
9181               struct tracepoint *t = (struct tracepoint *) b;
9182               struct static_tracepoint_marker marker;
9183
9184               if (strace_marker_p (b))
9185                 {
9186                   /* We already know the marker exists, otherwise, we
9187                      wouldn't see a sal for it.  */
9188                   char *p = &addr_string[3];
9189                   char *endp;
9190                   char *marker_str;
9191
9192                   p = skip_spaces (p);
9193
9194                   endp = skip_to_space (p);
9195
9196                   marker_str = savestring (p, endp - p);
9197                   t->static_trace_marker_id = marker_str;
9198
9199                   printf_filtered (_("Probed static tracepoint "
9200                                      "marker \"%s\"\n"),
9201                                    t->static_trace_marker_id);
9202                 }
9203               else if (target_static_tracepoint_marker_at (sal.pc, &marker))
9204                 {
9205                   t->static_trace_marker_id = xstrdup (marker.str_id);
9206                   release_static_tracepoint_marker (&marker);
9207
9208                   printf_filtered (_("Probed static tracepoint "
9209                                      "marker \"%s\"\n"),
9210                                    t->static_trace_marker_id);
9211                 }
9212               else
9213                 warning (_("Couldn't determine the static "
9214                            "tracepoint marker to probe"));
9215             }
9216
9217           loc = b->loc;
9218         }
9219       else
9220         {
9221           loc = add_location_to_breakpoint (b, &sal);
9222           if ((flags & CREATE_BREAKPOINT_FLAGS_INSERTED) != 0)
9223             loc->inserted = 1;
9224         }
9225
9226       if (bp_loc_is_permanent (loc))
9227         make_breakpoint_permanent (b);
9228
9229       if (b->cond_string)
9230         {
9231           const char *arg = b->cond_string;
9232
9233           loc->cond = parse_exp_1 (&arg, loc->address,
9234                                    block_for_pc (loc->address), 0);
9235           if (*arg)
9236               error (_("Garbage '%s' follows condition"), arg);
9237         }
9238
9239       /* Dynamic printf requires and uses additional arguments on the
9240          command line, otherwise it's an error.  */
9241       if (type == bp_dprintf)
9242         {
9243           if (b->extra_string)
9244             update_dprintf_command_list (b);
9245           else
9246             error (_("Format string required"));
9247         }
9248       else if (b->extra_string)
9249         error (_("Garbage '%s' at end of command"), b->extra_string);
9250     }
9251
9252   b->display_canonical = display_canonical;
9253   if (addr_string)
9254     b->addr_string = addr_string;
9255   else
9256     /* addr_string has to be used or breakpoint_re_set will delete
9257        me.  */
9258     b->addr_string
9259       = xstrprintf ("*%s", paddress (b->loc->gdbarch, b->loc->address));
9260   b->filter = filter;
9261 }
9262
9263 static void
9264 create_breakpoint_sal (struct gdbarch *gdbarch,
9265                        struct symtabs_and_lines sals, char *addr_string,
9266                        char *filter, char *cond_string,
9267                        char *extra_string,
9268                        enum bptype type, enum bpdisp disposition,
9269                        int thread, int task, int ignore_count,
9270                        const struct breakpoint_ops *ops, int from_tty,
9271                        int enabled, int internal, unsigned flags,
9272                        int display_canonical)
9273 {
9274   struct breakpoint *b;
9275   struct cleanup *old_chain;
9276
9277   if (is_tracepoint_type (type))
9278     {
9279       struct tracepoint *t;
9280
9281       t = XCNEW (struct tracepoint);
9282       b = &t->base;
9283     }
9284   else
9285     b = XNEW (struct breakpoint);
9286
9287   old_chain = make_cleanup (xfree, b);
9288
9289   init_breakpoint_sal (b, gdbarch,
9290                        sals, addr_string,
9291                        filter, cond_string, extra_string,
9292                        type, disposition,
9293                        thread, task, ignore_count,
9294                        ops, from_tty,
9295                        enabled, internal, flags,
9296                        display_canonical);
9297   discard_cleanups (old_chain);
9298
9299   install_breakpoint (internal, b, 0);
9300 }
9301
9302 /* Add SALS.nelts breakpoints to the breakpoint table.  For each
9303    SALS.sal[i] breakpoint, include the corresponding ADDR_STRING[i]
9304    value.  COND_STRING, if not NULL, specified the condition to be
9305    used for all breakpoints.  Essentially the only case where
9306    SALS.nelts is not 1 is when we set a breakpoint on an overloaded
9307    function.  In that case, it's still not possible to specify
9308    separate conditions for different overloaded functions, so
9309    we take just a single condition string.
9310    
9311    NOTE: If the function succeeds, the caller is expected to cleanup
9312    the arrays ADDR_STRING, COND_STRING, and SALS (but not the
9313    array contents).  If the function fails (error() is called), the
9314    caller is expected to cleanups both the ADDR_STRING, COND_STRING,
9315    COND and SALS arrays and each of those arrays contents.  */
9316
9317 static void
9318 create_breakpoints_sal (struct gdbarch *gdbarch,
9319                         struct linespec_result *canonical,
9320                         char *cond_string, char *extra_string,
9321                         enum bptype type, enum bpdisp disposition,
9322                         int thread, int task, int ignore_count,
9323                         const struct breakpoint_ops *ops, int from_tty,
9324                         int enabled, int internal, unsigned flags)
9325 {
9326   int i;
9327   struct linespec_sals *lsal;
9328
9329   if (canonical->pre_expanded)
9330     gdb_assert (VEC_length (linespec_sals, canonical->sals) == 1);
9331
9332   for (i = 0; VEC_iterate (linespec_sals, canonical->sals, i, lsal); ++i)
9333     {
9334       /* Note that 'addr_string' can be NULL in the case of a plain
9335          'break', without arguments.  */
9336       char *addr_string = (canonical->addr_string
9337                            ? xstrdup (canonical->addr_string)
9338                            : NULL);
9339       char *filter_string = lsal->canonical ? xstrdup (lsal->canonical) : NULL;
9340       struct cleanup *inner = make_cleanup (xfree, addr_string);
9341
9342       make_cleanup (xfree, filter_string);
9343       create_breakpoint_sal (gdbarch, lsal->sals,
9344                              addr_string,
9345                              filter_string,
9346                              cond_string, extra_string,
9347                              type, disposition,
9348                              thread, task, ignore_count, ops,
9349                              from_tty, enabled, internal, flags,
9350                              canonical->special_display);
9351       discard_cleanups (inner);
9352     }
9353 }
9354
9355 /* Parse ADDRESS which is assumed to be a SAL specification possibly
9356    followed by conditionals.  On return, SALS contains an array of SAL
9357    addresses found.  ADDR_STRING contains a vector of (canonical)
9358    address strings.  ADDRESS points to the end of the SAL.
9359
9360    The array and the line spec strings are allocated on the heap, it is
9361    the caller's responsibility to free them.  */
9362
9363 static void
9364 parse_breakpoint_sals (char **address,
9365                        struct linespec_result *canonical)
9366 {
9367   /* If no arg given, or if first arg is 'if ', use the default
9368      breakpoint.  */
9369   if ((*address) == NULL
9370       || (strncmp ((*address), "if", 2) == 0 && isspace ((*address)[2])))
9371     {
9372       /* The last displayed codepoint, if it's valid, is our default breakpoint
9373          address.  */
9374       if (last_displayed_sal_is_valid ())
9375         {
9376           struct linespec_sals lsal;
9377           struct symtab_and_line sal;
9378           CORE_ADDR pc;
9379
9380           init_sal (&sal);              /* Initialize to zeroes.  */
9381           lsal.sals.sals = (struct symtab_and_line *)
9382             xmalloc (sizeof (struct symtab_and_line));
9383
9384           /* Set sal's pspace, pc, symtab, and line to the values
9385              corresponding to the last call to print_frame_info.
9386              Be sure to reinitialize LINE with NOTCURRENT == 0
9387              as the breakpoint line number is inappropriate otherwise.
9388              find_pc_line would adjust PC, re-set it back.  */
9389           get_last_displayed_sal (&sal);
9390           pc = sal.pc;
9391           sal = find_pc_line (pc, 0);
9392
9393           /* "break" without arguments is equivalent to "break *PC"
9394              where PC is the last displayed codepoint's address.  So
9395              make sure to set sal.explicit_pc to prevent GDB from
9396              trying to expand the list of sals to include all other
9397              instances with the same symtab and line.  */
9398           sal.pc = pc;
9399           sal.explicit_pc = 1;
9400
9401           lsal.sals.sals[0] = sal;
9402           lsal.sals.nelts = 1;
9403           lsal.canonical = NULL;
9404
9405           VEC_safe_push (linespec_sals, canonical->sals, &lsal);
9406         }
9407       else
9408         error (_("No default breakpoint address now."));
9409     }
9410   else
9411     {
9412       struct symtab_and_line cursal = get_current_source_symtab_and_line ();
9413
9414       /* Force almost all breakpoints to be in terms of the
9415          current_source_symtab (which is decode_line_1's default).
9416          This should produce the results we want almost all of the
9417          time while leaving default_breakpoint_* alone.
9418
9419          ObjC: However, don't match an Objective-C method name which
9420          may have a '+' or '-' succeeded by a '['.  */
9421       if (last_displayed_sal_is_valid ()
9422           && (!cursal.symtab
9423               || ((strchr ("+-", (*address)[0]) != NULL)
9424                   && ((*address)[1] != '['))))
9425         decode_line_full (address, DECODE_LINE_FUNFIRSTLINE,
9426                           get_last_displayed_symtab (),
9427                           get_last_displayed_line (),
9428                           canonical, NULL, NULL);
9429       else
9430         decode_line_full (address, DECODE_LINE_FUNFIRSTLINE,
9431                           cursal.symtab, cursal.line, canonical, NULL, NULL);
9432     }
9433 }
9434
9435
9436 /* Convert each SAL into a real PC.  Verify that the PC can be
9437    inserted as a breakpoint.  If it can't throw an error.  */
9438
9439 static void
9440 breakpoint_sals_to_pc (struct symtabs_and_lines *sals)
9441 {    
9442   int i;
9443
9444   for (i = 0; i < sals->nelts; i++)
9445     resolve_sal_pc (&sals->sals[i]);
9446 }
9447
9448 /* Fast tracepoints may have restrictions on valid locations.  For
9449    instance, a fast tracepoint using a jump instead of a trap will
9450    likely have to overwrite more bytes than a trap would, and so can
9451    only be placed where the instruction is longer than the jump, or a
9452    multi-instruction sequence does not have a jump into the middle of
9453    it, etc.  */
9454
9455 static void
9456 check_fast_tracepoint_sals (struct gdbarch *gdbarch,
9457                             struct symtabs_and_lines *sals)
9458 {
9459   int i, rslt;
9460   struct symtab_and_line *sal;
9461   char *msg;
9462   struct cleanup *old_chain;
9463
9464   for (i = 0; i < sals->nelts; i++)
9465     {
9466       struct gdbarch *sarch;
9467
9468       sal = &sals->sals[i];
9469
9470       sarch = get_sal_arch (*sal);
9471       /* We fall back to GDBARCH if there is no architecture
9472          associated with SAL.  */
9473       if (sarch == NULL)
9474         sarch = gdbarch;
9475       rslt = gdbarch_fast_tracepoint_valid_at (sarch, sal->pc,
9476                                                NULL, &msg);
9477       old_chain = make_cleanup (xfree, msg);
9478
9479       if (!rslt)
9480         error (_("May not have a fast tracepoint at 0x%s%s"),
9481                paddress (sarch, sal->pc), (msg ? msg : ""));
9482
9483       do_cleanups (old_chain);
9484     }
9485 }
9486
9487 /* Issue an invalid thread ID error.  */
9488
9489 static void ATTRIBUTE_NORETURN
9490 invalid_thread_id_error (int id)
9491 {
9492   error (_("Unknown thread %d."), id);
9493 }
9494
9495 /* Given TOK, a string specification of condition and thread, as
9496    accepted by the 'break' command, extract the condition
9497    string and thread number and set *COND_STRING and *THREAD.
9498    PC identifies the context at which the condition should be parsed.
9499    If no condition is found, *COND_STRING is set to NULL.
9500    If no thread is found, *THREAD is set to -1.  */
9501
9502 static void
9503 find_condition_and_thread (const char *tok, CORE_ADDR pc,
9504                            char **cond_string, int *thread, int *task,
9505                            char **rest)
9506 {
9507   *cond_string = NULL;
9508   *thread = -1;
9509   *task = 0;
9510   *rest = NULL;
9511
9512   while (tok && *tok)
9513     {
9514       const char *end_tok;
9515       int toklen;
9516       const char *cond_start = NULL;
9517       const char *cond_end = NULL;
9518
9519       tok = skip_spaces_const (tok);
9520
9521       if ((*tok == '"' || *tok == ',') && rest)
9522         {
9523           *rest = savestring (tok, strlen (tok));
9524           return;
9525         }
9526
9527       end_tok = skip_to_space_const (tok);
9528
9529       toklen = end_tok - tok;
9530
9531       if (toklen >= 1 && strncmp (tok, "if", toklen) == 0)
9532         {
9533           struct expression *expr;
9534
9535           tok = cond_start = end_tok + 1;
9536           expr = parse_exp_1 (&tok, pc, block_for_pc (pc), 0);
9537           xfree (expr);
9538           cond_end = tok;
9539           *cond_string = savestring (cond_start, cond_end - cond_start);
9540         }
9541       else if (toklen >= 1 && strncmp (tok, "thread", toklen) == 0)
9542         {
9543           char *tmptok;
9544
9545           tok = end_tok + 1;
9546           *thread = strtol (tok, &tmptok, 0);
9547           if (tok == tmptok)
9548             error (_("Junk after thread keyword."));
9549           if (!valid_thread_id (*thread))
9550             invalid_thread_id_error (*thread);
9551           tok = tmptok;
9552         }
9553       else if (toklen >= 1 && strncmp (tok, "task", toklen) == 0)
9554         {
9555           char *tmptok;
9556
9557           tok = end_tok + 1;
9558           *task = strtol (tok, &tmptok, 0);
9559           if (tok == tmptok)
9560             error (_("Junk after task keyword."));
9561           if (!valid_task_id (*task))
9562             error (_("Unknown task %d."), *task);
9563           tok = tmptok;
9564         }
9565       else if (rest)
9566         {
9567           *rest = savestring (tok, strlen (tok));
9568           return;
9569         }
9570       else
9571         error (_("Junk at end of arguments."));
9572     }
9573 }
9574
9575 /* Decode a static tracepoint marker spec.  */
9576
9577 static struct symtabs_and_lines
9578 decode_static_tracepoint_spec (char **arg_p)
9579 {
9580   VEC(static_tracepoint_marker_p) *markers = NULL;
9581   struct symtabs_and_lines sals;
9582   struct cleanup *old_chain;
9583   char *p = &(*arg_p)[3];
9584   char *endp;
9585   char *marker_str;
9586   int i;
9587
9588   p = skip_spaces (p);
9589
9590   endp = skip_to_space (p);
9591
9592   marker_str = savestring (p, endp - p);
9593   old_chain = make_cleanup (xfree, marker_str);
9594
9595   markers = target_static_tracepoint_markers_by_strid (marker_str);
9596   if (VEC_empty(static_tracepoint_marker_p, markers))
9597     error (_("No known static tracepoint marker named %s"), marker_str);
9598
9599   sals.nelts = VEC_length(static_tracepoint_marker_p, markers);
9600   sals.sals = xmalloc (sizeof *sals.sals * sals.nelts);
9601
9602   for (i = 0; i < sals.nelts; i++)
9603     {
9604       struct static_tracepoint_marker *marker;
9605
9606       marker = VEC_index (static_tracepoint_marker_p, markers, i);
9607
9608       init_sal (&sals.sals[i]);
9609
9610       sals.sals[i] = find_pc_line (marker->address, 0);
9611       sals.sals[i].pc = marker->address;
9612
9613       release_static_tracepoint_marker (marker);
9614     }
9615
9616   do_cleanups (old_chain);
9617
9618   *arg_p = endp;
9619   return sals;
9620 }
9621
9622 /* Set a breakpoint.  This function is shared between CLI and MI
9623    functions for setting a breakpoint.  This function has two major
9624    modes of operations, selected by the PARSE_ARG parameter.  If
9625    non-zero, the function will parse ARG, extracting location,
9626    condition, thread and extra string.  Otherwise, ARG is just the
9627    breakpoint's location, with condition, thread, and extra string
9628    specified by the COND_STRING, THREAD and EXTRA_STRING parameters.
9629    If INTERNAL is non-zero, the breakpoint number will be allocated
9630    from the internal breakpoint count.  Returns true if any breakpoint
9631    was created; false otherwise.  */
9632
9633 int
9634 create_breakpoint (struct gdbarch *gdbarch,
9635                    char *arg, char *cond_string,
9636                    int thread, char *extra_string,
9637                    int parse_arg,
9638                    int tempflag, enum bptype type_wanted,
9639                    int ignore_count,
9640                    enum auto_boolean pending_break_support,
9641                    const struct breakpoint_ops *ops,
9642                    int from_tty, int enabled, int internal,
9643                    unsigned flags)
9644 {
9645   volatile struct gdb_exception e;
9646   char *copy_arg = NULL;
9647   char *addr_start = arg;
9648   struct linespec_result canonical;
9649   struct cleanup *old_chain;
9650   struct cleanup *bkpt_chain = NULL;
9651   int pending = 0;
9652   int task = 0;
9653   int prev_bkpt_count = breakpoint_count;
9654
9655   gdb_assert (ops != NULL);
9656
9657   init_linespec_result (&canonical);
9658
9659   TRY_CATCH (e, RETURN_MASK_ALL)
9660     {
9661       ops->create_sals_from_address (&arg, &canonical, type_wanted,
9662                                      addr_start, &copy_arg);
9663     }
9664
9665   /* If caller is interested in rc value from parse, set value.  */
9666   switch (e.reason)
9667     {
9668     case GDB_NO_ERROR:
9669       if (VEC_empty (linespec_sals, canonical.sals))
9670         return 0;
9671       break;
9672     case RETURN_ERROR:
9673       switch (e.error)
9674         {
9675         case NOT_FOUND_ERROR:
9676
9677           /* If pending breakpoint support is turned off, throw
9678              error.  */
9679
9680           if (pending_break_support == AUTO_BOOLEAN_FALSE)
9681             throw_exception (e);
9682
9683           exception_print (gdb_stderr, e);
9684
9685           /* If pending breakpoint support is auto query and the user
9686              selects no, then simply return the error code.  */
9687           if (pending_break_support == AUTO_BOOLEAN_AUTO
9688               && !nquery (_("Make %s pending on future shared library load? "),
9689                           bptype_string (type_wanted)))
9690             return 0;
9691
9692           /* At this point, either the user was queried about setting
9693              a pending breakpoint and selected yes, or pending
9694              breakpoint behavior is on and thus a pending breakpoint
9695              is defaulted on behalf of the user.  */
9696           {
9697             struct linespec_sals lsal;
9698
9699             copy_arg = xstrdup (addr_start);
9700             lsal.canonical = xstrdup (copy_arg);
9701             lsal.sals.nelts = 1;
9702             lsal.sals.sals = XNEW (struct symtab_and_line);
9703             init_sal (&lsal.sals.sals[0]);
9704             pending = 1;
9705             VEC_safe_push (linespec_sals, canonical.sals, &lsal);
9706           }
9707           break;
9708         default:
9709           throw_exception (e);
9710         }
9711       break;
9712     default:
9713       throw_exception (e);
9714     }
9715
9716   /* Create a chain of things that always need to be cleaned up.  */
9717   old_chain = make_cleanup_destroy_linespec_result (&canonical);
9718
9719   /* ----------------------------- SNIP -----------------------------
9720      Anything added to the cleanup chain beyond this point is assumed
9721      to be part of a breakpoint.  If the breakpoint create succeeds
9722      then the memory is not reclaimed.  */
9723   bkpt_chain = make_cleanup (null_cleanup, 0);
9724
9725   /* Resolve all line numbers to PC's and verify that the addresses
9726      are ok for the target.  */
9727   if (!pending)
9728     {
9729       int ix;
9730       struct linespec_sals *iter;
9731
9732       for (ix = 0; VEC_iterate (linespec_sals, canonical.sals, ix, iter); ++ix)
9733         breakpoint_sals_to_pc (&iter->sals);
9734     }
9735
9736   /* Fast tracepoints may have additional restrictions on location.  */
9737   if (!pending && type_wanted == bp_fast_tracepoint)
9738     {
9739       int ix;
9740       struct linespec_sals *iter;
9741
9742       for (ix = 0; VEC_iterate (linespec_sals, canonical.sals, ix, iter); ++ix)
9743         check_fast_tracepoint_sals (gdbarch, &iter->sals);
9744     }
9745
9746   /* Verify that condition can be parsed, before setting any
9747      breakpoints.  Allocate a separate condition expression for each
9748      breakpoint.  */
9749   if (!pending)
9750     {
9751       if (parse_arg)
9752         {
9753           char *rest;
9754           struct linespec_sals *lsal;
9755
9756           lsal = VEC_index (linespec_sals, canonical.sals, 0);
9757
9758           /* Here we only parse 'arg' to separate condition
9759              from thread number, so parsing in context of first
9760              sal is OK.  When setting the breakpoint we'll
9761              re-parse it in context of each sal.  */
9762
9763           find_condition_and_thread (arg, lsal->sals.sals[0].pc, &cond_string,
9764                                      &thread, &task, &rest);
9765           if (cond_string)
9766             make_cleanup (xfree, cond_string);
9767           if (rest)
9768             make_cleanup (xfree, rest);
9769           if (rest)
9770             extra_string = rest;
9771         }
9772       else
9773         {
9774           if (*arg != '\0')
9775             error (_("Garbage '%s' at end of location"), arg);
9776
9777           /* Create a private copy of condition string.  */
9778           if (cond_string)
9779             {
9780               cond_string = xstrdup (cond_string);
9781               make_cleanup (xfree, cond_string);
9782             }
9783           /* Create a private copy of any extra string.  */
9784           if (extra_string)
9785             {
9786               extra_string = xstrdup (extra_string);
9787               make_cleanup (xfree, extra_string);
9788             }
9789         }
9790
9791       ops->create_breakpoints_sal (gdbarch, &canonical,
9792                                    cond_string, extra_string, type_wanted,
9793                                    tempflag ? disp_del : disp_donttouch,
9794                                    thread, task, ignore_count, ops,
9795                                    from_tty, enabled, internal, flags);
9796     }
9797   else
9798     {
9799       struct breakpoint *b;
9800
9801       make_cleanup (xfree, copy_arg);
9802
9803       if (is_tracepoint_type (type_wanted))
9804         {
9805           struct tracepoint *t;
9806
9807           t = XCNEW (struct tracepoint);
9808           b = &t->base;
9809         }
9810       else
9811         b = XNEW (struct breakpoint);
9812
9813       init_raw_breakpoint_without_location (b, gdbarch, type_wanted, ops);
9814
9815       b->addr_string = copy_arg;
9816       if (parse_arg)
9817         b->cond_string = NULL;
9818       else
9819         {
9820           /* Create a private copy of condition string.  */
9821           if (cond_string)
9822             {
9823               cond_string = xstrdup (cond_string);
9824               make_cleanup (xfree, cond_string);
9825             }
9826           b->cond_string = cond_string;
9827         }
9828       b->extra_string = NULL;
9829       b->ignore_count = ignore_count;
9830       b->disposition = tempflag ? disp_del : disp_donttouch;
9831       b->condition_not_parsed = 1;
9832       b->enable_state = enabled ? bp_enabled : bp_disabled;
9833       if ((type_wanted != bp_breakpoint
9834            && type_wanted != bp_hardware_breakpoint) || thread != -1)
9835         b->pspace = current_program_space;
9836
9837       install_breakpoint (internal, b, 0);
9838     }
9839   
9840   if (VEC_length (linespec_sals, canonical.sals) > 1)
9841     {
9842       warning (_("Multiple breakpoints were set.\nUse the "
9843                  "\"delete\" command to delete unwanted breakpoints."));
9844       prev_breakpoint_count = prev_bkpt_count;
9845     }
9846
9847   /* That's it.  Discard the cleanups for data inserted into the
9848      breakpoint.  */
9849   discard_cleanups (bkpt_chain);
9850   /* But cleanup everything else.  */
9851   do_cleanups (old_chain);
9852
9853   /* error call may happen here - have BKPT_CHAIN already discarded.  */
9854   update_global_location_list (1);
9855
9856   return 1;
9857 }
9858
9859 /* Set a breakpoint.
9860    ARG is a string describing breakpoint address,
9861    condition, and thread.
9862    FLAG specifies if a breakpoint is hardware on,
9863    and if breakpoint is temporary, using BP_HARDWARE_FLAG
9864    and BP_TEMPFLAG.  */
9865
9866 static void
9867 break_command_1 (char *arg, int flag, int from_tty)
9868 {
9869   int tempflag = flag & BP_TEMPFLAG;
9870   enum bptype type_wanted = (flag & BP_HARDWAREFLAG
9871                              ? bp_hardware_breakpoint
9872                              : bp_breakpoint);
9873   struct breakpoint_ops *ops;
9874   const char *arg_cp = arg;
9875
9876   /* Matching breakpoints on probes.  */
9877   if (arg && probe_linespec_to_ops (&arg_cp) != NULL)
9878     ops = &bkpt_probe_breakpoint_ops;
9879   else
9880     ops = &bkpt_breakpoint_ops;
9881
9882   create_breakpoint (get_current_arch (),
9883                      arg,
9884                      NULL, 0, NULL, 1 /* parse arg */,
9885                      tempflag, type_wanted,
9886                      0 /* Ignore count */,
9887                      pending_break_support,
9888                      ops,
9889                      from_tty,
9890                      1 /* enabled */,
9891                      0 /* internal */,
9892                      0);
9893 }
9894
9895 /* Helper function for break_command_1 and disassemble_command.  */
9896
9897 void
9898 resolve_sal_pc (struct symtab_and_line *sal)
9899 {
9900   CORE_ADDR pc;
9901
9902   if (sal->pc == 0 && sal->symtab != NULL)
9903     {
9904       if (!find_line_pc (sal->symtab, sal->line, &pc))
9905         error (_("No line %d in file \"%s\"."),
9906                sal->line, symtab_to_filename_for_display (sal->symtab));
9907       sal->pc = pc;
9908
9909       /* If this SAL corresponds to a breakpoint inserted using a line
9910          number, then skip the function prologue if necessary.  */
9911       if (sal->explicit_line)
9912         skip_prologue_sal (sal);
9913     }
9914
9915   if (sal->section == 0 && sal->symtab != NULL)
9916     {
9917       struct blockvector *bv;
9918       struct block *b;
9919       struct symbol *sym;
9920
9921       bv = blockvector_for_pc_sect (sal->pc, 0, &b, sal->symtab);
9922       if (bv != NULL)
9923         {
9924           sym = block_linkage_function (b);
9925           if (sym != NULL)
9926             {
9927               fixup_symbol_section (sym, sal->symtab->objfile);
9928               sal->section = SYMBOL_OBJ_SECTION (sal->symtab->objfile, sym);
9929             }
9930           else
9931             {
9932               /* It really is worthwhile to have the section, so we'll
9933                  just have to look harder. This case can be executed
9934                  if we have line numbers but no functions (as can
9935                  happen in assembly source).  */
9936
9937               struct bound_minimal_symbol msym;
9938               struct cleanup *old_chain = save_current_space_and_thread ();
9939
9940               switch_to_program_space_and_thread (sal->pspace);
9941
9942               msym = lookup_minimal_symbol_by_pc (sal->pc);
9943               if (msym.minsym)
9944                 sal->section = SYMBOL_OBJ_SECTION (msym.objfile, msym.minsym);
9945
9946               do_cleanups (old_chain);
9947             }
9948         }
9949     }
9950 }
9951
9952 void
9953 break_command (char *arg, int from_tty)
9954 {
9955   break_command_1 (arg, 0, from_tty);
9956 }
9957
9958 void
9959 tbreak_command (char *arg, int from_tty)
9960 {
9961   break_command_1 (arg, BP_TEMPFLAG, from_tty);
9962 }
9963
9964 static void
9965 hbreak_command (char *arg, int from_tty)
9966 {
9967   break_command_1 (arg, BP_HARDWAREFLAG, from_tty);
9968 }
9969
9970 static void
9971 thbreak_command (char *arg, int from_tty)
9972 {
9973   break_command_1 (arg, (BP_TEMPFLAG | BP_HARDWAREFLAG), from_tty);
9974 }
9975
9976 static void
9977 stop_command (char *arg, int from_tty)
9978 {
9979   printf_filtered (_("Specify the type of breakpoint to set.\n\
9980 Usage: stop in <function | address>\n\
9981        stop at <line>\n"));
9982 }
9983
9984 static void
9985 stopin_command (char *arg, int from_tty)
9986 {
9987   int badInput = 0;
9988
9989   if (arg == (char *) NULL)
9990     badInput = 1;
9991   else if (*arg != '*')
9992     {
9993       char *argptr = arg;
9994       int hasColon = 0;
9995
9996       /* Look for a ':'.  If this is a line number specification, then
9997          say it is bad, otherwise, it should be an address or
9998          function/method name.  */
9999       while (*argptr && !hasColon)
10000         {
10001           hasColon = (*argptr == ':');
10002           argptr++;
10003         }
10004
10005       if (hasColon)
10006         badInput = (*argptr != ':');    /* Not a class::method */
10007       else
10008         badInput = isdigit (*arg);      /* a simple line number */
10009     }
10010
10011   if (badInput)
10012     printf_filtered (_("Usage: stop in <function | address>\n"));
10013   else
10014     break_command_1 (arg, 0, from_tty);
10015 }
10016
10017 static void
10018 stopat_command (char *arg, int from_tty)
10019 {
10020   int badInput = 0;
10021
10022   if (arg == (char *) NULL || *arg == '*')      /* no line number */
10023     badInput = 1;
10024   else
10025     {
10026       char *argptr = arg;
10027       int hasColon = 0;
10028
10029       /* Look for a ':'.  If there is a '::' then get out, otherwise
10030          it is probably a line number.  */
10031       while (*argptr && !hasColon)
10032         {
10033           hasColon = (*argptr == ':');
10034           argptr++;
10035         }
10036
10037       if (hasColon)
10038         badInput = (*argptr == ':');    /* we have class::method */
10039       else
10040         badInput = !isdigit (*arg);     /* not a line number */
10041     }
10042
10043   if (badInput)
10044     printf_filtered (_("Usage: stop at <line>\n"));
10045   else
10046     break_command_1 (arg, 0, from_tty);
10047 }
10048
10049 /* The dynamic printf command is mostly like a regular breakpoint, but
10050    with a prewired command list consisting of a single output command,
10051    built from extra arguments supplied on the dprintf command
10052    line.  */
10053
10054 static void
10055 dprintf_command (char *arg, int from_tty)
10056 {
10057   create_breakpoint (get_current_arch (),
10058                      arg,
10059                      NULL, 0, NULL, 1 /* parse arg */,
10060                      0, bp_dprintf,
10061                      0 /* Ignore count */,
10062                      pending_break_support,
10063                      &dprintf_breakpoint_ops,
10064                      from_tty,
10065                      1 /* enabled */,
10066                      0 /* internal */,
10067                      0);
10068 }
10069
10070 static void
10071 agent_printf_command (char *arg, int from_tty)
10072 {
10073   error (_("May only run agent-printf on the target"));
10074 }
10075
10076 /* Implement the "breakpoint_hit" breakpoint_ops method for
10077    ranged breakpoints.  */
10078
10079 static int
10080 breakpoint_hit_ranged_breakpoint (const struct bp_location *bl,
10081                                   struct address_space *aspace,
10082                                   CORE_ADDR bp_addr,
10083                                   const struct target_waitstatus *ws)
10084 {
10085   if (ws->kind != TARGET_WAITKIND_STOPPED
10086       || ws->value.sig != GDB_SIGNAL_TRAP)
10087     return 0;
10088
10089   return breakpoint_address_match_range (bl->pspace->aspace, bl->address,
10090                                          bl->length, aspace, bp_addr);
10091 }
10092
10093 /* Implement the "resources_needed" breakpoint_ops method for
10094    ranged breakpoints.  */
10095
10096 static int
10097 resources_needed_ranged_breakpoint (const struct bp_location *bl)
10098 {
10099   return target_ranged_break_num_registers ();
10100 }
10101
10102 /* Implement the "print_it" breakpoint_ops method for
10103    ranged breakpoints.  */
10104
10105 static enum print_stop_action
10106 print_it_ranged_breakpoint (bpstat bs)
10107 {
10108   struct breakpoint *b = bs->breakpoint_at;
10109   struct bp_location *bl = b->loc;
10110   struct ui_out *uiout = current_uiout;
10111
10112   gdb_assert (b->type == bp_hardware_breakpoint);
10113
10114   /* Ranged breakpoints have only one location.  */
10115   gdb_assert (bl && bl->next == NULL);
10116
10117   annotate_breakpoint (b->number);
10118   if (b->disposition == disp_del)
10119     ui_out_text (uiout, "\nTemporary ranged breakpoint ");
10120   else
10121     ui_out_text (uiout, "\nRanged breakpoint ");
10122   if (ui_out_is_mi_like_p (uiout))
10123     {
10124       ui_out_field_string (uiout, "reason",
10125                       async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT));
10126       ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
10127     }
10128   ui_out_field_int (uiout, "bkptno", b->number);
10129   ui_out_text (uiout, ", ");
10130
10131   return PRINT_SRC_AND_LOC;
10132 }
10133
10134 /* Implement the "print_one" breakpoint_ops method for
10135    ranged breakpoints.  */
10136
10137 static void
10138 print_one_ranged_breakpoint (struct breakpoint *b,
10139                              struct bp_location **last_loc)
10140 {
10141   struct bp_location *bl = b->loc;
10142   struct value_print_options opts;
10143   struct ui_out *uiout = current_uiout;
10144
10145   /* Ranged breakpoints have only one location.  */
10146   gdb_assert (bl && bl->next == NULL);
10147
10148   get_user_print_options (&opts);
10149
10150   if (opts.addressprint)
10151     /* We don't print the address range here, it will be printed later
10152        by print_one_detail_ranged_breakpoint.  */
10153     ui_out_field_skip (uiout, "addr");
10154   annotate_field (5);
10155   print_breakpoint_location (b, bl);
10156   *last_loc = bl;
10157 }
10158
10159 /* Implement the "print_one_detail" breakpoint_ops method for
10160    ranged breakpoints.  */
10161
10162 static void
10163 print_one_detail_ranged_breakpoint (const struct breakpoint *b,
10164                                     struct ui_out *uiout)
10165 {
10166   CORE_ADDR address_start, address_end;
10167   struct bp_location *bl = b->loc;
10168   struct ui_file *stb = mem_fileopen ();
10169   struct cleanup *cleanup = make_cleanup_ui_file_delete (stb);
10170
10171   gdb_assert (bl);
10172
10173   address_start = bl->address;
10174   address_end = address_start + bl->length - 1;
10175
10176   ui_out_text (uiout, "\taddress range: ");
10177   fprintf_unfiltered (stb, "[%s, %s]",
10178                       print_core_address (bl->gdbarch, address_start),
10179                       print_core_address (bl->gdbarch, address_end));
10180   ui_out_field_stream (uiout, "addr", stb);
10181   ui_out_text (uiout, "\n");
10182
10183   do_cleanups (cleanup);
10184 }
10185
10186 /* Implement the "print_mention" breakpoint_ops method for
10187    ranged breakpoints.  */
10188
10189 static void
10190 print_mention_ranged_breakpoint (struct breakpoint *b)
10191 {
10192   struct bp_location *bl = b->loc;
10193   struct ui_out *uiout = current_uiout;
10194
10195   gdb_assert (bl);
10196   gdb_assert (b->type == bp_hardware_breakpoint);
10197
10198   if (ui_out_is_mi_like_p (uiout))
10199     return;
10200
10201   printf_filtered (_("Hardware assisted ranged breakpoint %d from %s to %s."),
10202                    b->number, paddress (bl->gdbarch, bl->address),
10203                    paddress (bl->gdbarch, bl->address + bl->length - 1));
10204 }
10205
10206 /* Implement the "print_recreate" breakpoint_ops method for
10207    ranged breakpoints.  */
10208
10209 static void
10210 print_recreate_ranged_breakpoint (struct breakpoint *b, struct ui_file *fp)
10211 {
10212   fprintf_unfiltered (fp, "break-range %s, %s", b->addr_string,
10213                       b->addr_string_range_end);
10214   print_recreate_thread (b, fp);
10215 }
10216
10217 /* The breakpoint_ops structure to be used in ranged breakpoints.  */
10218
10219 static struct breakpoint_ops ranged_breakpoint_ops;
10220
10221 /* Find the address where the end of the breakpoint range should be
10222    placed, given the SAL of the end of the range.  This is so that if
10223    the user provides a line number, the end of the range is set to the
10224    last instruction of the given line.  */
10225
10226 static CORE_ADDR
10227 find_breakpoint_range_end (struct symtab_and_line sal)
10228 {
10229   CORE_ADDR end;
10230
10231   /* If the user provided a PC value, use it.  Otherwise,
10232      find the address of the end of the given location.  */
10233   if (sal.explicit_pc)
10234     end = sal.pc;
10235   else
10236     {
10237       int ret;
10238       CORE_ADDR start;
10239
10240       ret = find_line_pc_range (sal, &start, &end);
10241       if (!ret)
10242         error (_("Could not find location of the end of the range."));
10243
10244       /* find_line_pc_range returns the start of the next line.  */
10245       end--;
10246     }
10247
10248   return end;
10249 }
10250
10251 /* Implement the "break-range" CLI command.  */
10252
10253 static void
10254 break_range_command (char *arg, int from_tty)
10255 {
10256   char *arg_start, *addr_string_start, *addr_string_end;
10257   struct linespec_result canonical_start, canonical_end;
10258   int bp_count, can_use_bp, length;
10259   CORE_ADDR end;
10260   struct breakpoint *b;
10261   struct symtab_and_line sal_start, sal_end;
10262   struct cleanup *cleanup_bkpt;
10263   struct linespec_sals *lsal_start, *lsal_end;
10264
10265   /* We don't support software ranged breakpoints.  */
10266   if (target_ranged_break_num_registers () < 0)
10267     error (_("This target does not support hardware ranged breakpoints."));
10268
10269   bp_count = hw_breakpoint_used_count ();
10270   bp_count += target_ranged_break_num_registers ();
10271   can_use_bp = target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
10272                                                    bp_count, 0);
10273   if (can_use_bp < 0)
10274     error (_("Hardware breakpoints used exceeds limit."));
10275
10276   arg = skip_spaces (arg);
10277   if (arg == NULL || arg[0] == '\0')
10278     error(_("No address range specified."));
10279
10280   init_linespec_result (&canonical_start);
10281
10282   arg_start = arg;
10283   parse_breakpoint_sals (&arg, &canonical_start);
10284
10285   cleanup_bkpt = make_cleanup_destroy_linespec_result (&canonical_start);
10286
10287   if (arg[0] != ',')
10288     error (_("Too few arguments."));
10289   else if (VEC_empty (linespec_sals, canonical_start.sals))
10290     error (_("Could not find location of the beginning of the range."));
10291
10292   lsal_start = VEC_index (linespec_sals, canonical_start.sals, 0);
10293
10294   if (VEC_length (linespec_sals, canonical_start.sals) > 1
10295       || lsal_start->sals.nelts != 1)
10296     error (_("Cannot create a ranged breakpoint with multiple locations."));
10297
10298   sal_start = lsal_start->sals.sals[0];
10299   addr_string_start = savestring (arg_start, arg - arg_start);
10300   make_cleanup (xfree, addr_string_start);
10301
10302   arg++;        /* Skip the comma.  */
10303   arg = skip_spaces (arg);
10304
10305   /* Parse the end location.  */
10306
10307   init_linespec_result (&canonical_end);
10308   arg_start = arg;
10309
10310   /* We call decode_line_full directly here instead of using
10311      parse_breakpoint_sals because we need to specify the start location's
10312      symtab and line as the default symtab and line for the end of the
10313      range.  This makes it possible to have ranges like "foo.c:27, +14",
10314      where +14 means 14 lines from the start location.  */
10315   decode_line_full (&arg, DECODE_LINE_FUNFIRSTLINE,
10316                     sal_start.symtab, sal_start.line,
10317                     &canonical_end, NULL, NULL);
10318
10319   make_cleanup_destroy_linespec_result (&canonical_end);
10320
10321   if (VEC_empty (linespec_sals, canonical_end.sals))
10322     error (_("Could not find location of the end of the range."));
10323
10324   lsal_end = VEC_index (linespec_sals, canonical_end.sals, 0);
10325   if (VEC_length (linespec_sals, canonical_end.sals) > 1
10326       || lsal_end->sals.nelts != 1)
10327     error (_("Cannot create a ranged breakpoint with multiple locations."));
10328
10329   sal_end = lsal_end->sals.sals[0];
10330   addr_string_end = savestring (arg_start, arg - arg_start);
10331   make_cleanup (xfree, addr_string_end);
10332
10333   end = find_breakpoint_range_end (sal_end);
10334   if (sal_start.pc > end)
10335     error (_("Invalid address range, end precedes start."));
10336
10337   length = end - sal_start.pc + 1;
10338   if (length < 0)
10339     /* Length overflowed.  */
10340     error (_("Address range too large."));
10341   else if (length == 1)
10342     {
10343       /* This range is simple enough to be handled by
10344          the `hbreak' command.  */
10345       hbreak_command (addr_string_start, 1);
10346
10347       do_cleanups (cleanup_bkpt);
10348
10349       return;
10350     }
10351
10352   /* Now set up the breakpoint.  */
10353   b = set_raw_breakpoint (get_current_arch (), sal_start,
10354                           bp_hardware_breakpoint, &ranged_breakpoint_ops);
10355   set_breakpoint_count (breakpoint_count + 1);
10356   b->number = breakpoint_count;
10357   b->disposition = disp_donttouch;
10358   b->addr_string = xstrdup (addr_string_start);
10359   b->addr_string_range_end = xstrdup (addr_string_end);
10360   b->loc->length = length;
10361
10362   do_cleanups (cleanup_bkpt);
10363
10364   mention (b);
10365   observer_notify_breakpoint_created (b);
10366   update_global_location_list (1);
10367 }
10368
10369 /*  Return non-zero if EXP is verified as constant.  Returned zero
10370     means EXP is variable.  Also the constant detection may fail for
10371     some constant expressions and in such case still falsely return
10372     zero.  */
10373
10374 static int
10375 watchpoint_exp_is_const (const struct expression *exp)
10376 {
10377   int i = exp->nelts;
10378
10379   while (i > 0)
10380     {
10381       int oplenp, argsp;
10382
10383       /* We are only interested in the descriptor of each element.  */
10384       operator_length (exp, i, &oplenp, &argsp);
10385       i -= oplenp;
10386
10387       switch (exp->elts[i].opcode)
10388         {
10389         case BINOP_ADD:
10390         case BINOP_SUB:
10391         case BINOP_MUL:
10392         case BINOP_DIV:
10393         case BINOP_REM:
10394         case BINOP_MOD:
10395         case BINOP_LSH:
10396         case BINOP_RSH:
10397         case BINOP_LOGICAL_AND:
10398         case BINOP_LOGICAL_OR:
10399         case BINOP_BITWISE_AND:
10400         case BINOP_BITWISE_IOR:
10401         case BINOP_BITWISE_XOR:
10402         case BINOP_EQUAL:
10403         case BINOP_NOTEQUAL:
10404         case BINOP_LESS:
10405         case BINOP_GTR:
10406         case BINOP_LEQ:
10407         case BINOP_GEQ:
10408         case BINOP_REPEAT:
10409         case BINOP_COMMA:
10410         case BINOP_EXP:
10411         case BINOP_MIN:
10412         case BINOP_MAX:
10413         case BINOP_INTDIV:
10414         case BINOP_CONCAT:
10415         case BINOP_IN:
10416         case BINOP_RANGE:
10417         case TERNOP_COND:
10418         case TERNOP_SLICE:
10419
10420         case OP_LONG:
10421         case OP_DOUBLE:
10422         case OP_DECFLOAT:
10423         case OP_LAST:
10424         case OP_COMPLEX:
10425         case OP_STRING:
10426         case OP_ARRAY:
10427         case OP_TYPE:
10428         case OP_TYPEOF:
10429         case OP_DECLTYPE:
10430         case OP_TYPEID:
10431         case OP_NAME:
10432         case OP_OBJC_NSSTRING:
10433
10434         case UNOP_NEG:
10435         case UNOP_LOGICAL_NOT:
10436         case UNOP_COMPLEMENT:
10437         case UNOP_ADDR:
10438         case UNOP_HIGH:
10439         case UNOP_CAST:
10440
10441         case UNOP_CAST_TYPE:
10442         case UNOP_REINTERPRET_CAST:
10443         case UNOP_DYNAMIC_CAST:
10444           /* Unary, binary and ternary operators: We have to check
10445              their operands.  If they are constant, then so is the
10446              result of that operation.  For instance, if A and B are
10447              determined to be constants, then so is "A + B".
10448
10449              UNOP_IND is one exception to the rule above, because the
10450              value of *ADDR is not necessarily a constant, even when
10451              ADDR is.  */
10452           break;
10453
10454         case OP_VAR_VALUE:
10455           /* Check whether the associated symbol is a constant.
10456
10457              We use SYMBOL_CLASS rather than TYPE_CONST because it's
10458              possible that a buggy compiler could mark a variable as
10459              constant even when it is not, and TYPE_CONST would return
10460              true in this case, while SYMBOL_CLASS wouldn't.
10461
10462              We also have to check for function symbols because they
10463              are always constant.  */
10464           {
10465             struct symbol *s = exp->elts[i + 2].symbol;
10466
10467             if (SYMBOL_CLASS (s) != LOC_BLOCK
10468                 && SYMBOL_CLASS (s) != LOC_CONST
10469                 && SYMBOL_CLASS (s) != LOC_CONST_BYTES)
10470               return 0;
10471             break;
10472           }
10473
10474         /* The default action is to return 0 because we are using
10475            the optimistic approach here: If we don't know something,
10476            then it is not a constant.  */
10477         default:
10478           return 0;
10479         }
10480     }
10481
10482   return 1;
10483 }
10484
10485 /* Implement the "dtor" breakpoint_ops method for watchpoints.  */
10486
10487 static void
10488 dtor_watchpoint (struct breakpoint *self)
10489 {
10490   struct watchpoint *w = (struct watchpoint *) self;
10491
10492   xfree (w->cond_exp);
10493   xfree (w->exp);
10494   xfree (w->exp_string);
10495   xfree (w->exp_string_reparse);
10496   value_free (w->val);
10497
10498   base_breakpoint_ops.dtor (self);
10499 }
10500
10501 /* Implement the "re_set" breakpoint_ops method for watchpoints.  */
10502
10503 static void
10504 re_set_watchpoint (struct breakpoint *b)
10505 {
10506   struct watchpoint *w = (struct watchpoint *) b;
10507
10508   /* Watchpoint can be either on expression using entirely global
10509      variables, or it can be on local variables.
10510
10511      Watchpoints of the first kind are never auto-deleted, and even
10512      persist across program restarts.  Since they can use variables
10513      from shared libraries, we need to reparse expression as libraries
10514      are loaded and unloaded.
10515
10516      Watchpoints on local variables can also change meaning as result
10517      of solib event.  For example, if a watchpoint uses both a local
10518      and a global variables in expression, it's a local watchpoint,
10519      but unloading of a shared library will make the expression
10520      invalid.  This is not a very common use case, but we still
10521      re-evaluate expression, to avoid surprises to the user.
10522
10523      Note that for local watchpoints, we re-evaluate it only if
10524      watchpoints frame id is still valid.  If it's not, it means the
10525      watchpoint is out of scope and will be deleted soon.  In fact,
10526      I'm not sure we'll ever be called in this case.
10527
10528      If a local watchpoint's frame id is still valid, then
10529      w->exp_valid_block is likewise valid, and we can safely use it.
10530
10531      Don't do anything about disabled watchpoints, since they will be
10532      reevaluated again when enabled.  */
10533   update_watchpoint (w, 1 /* reparse */);
10534 }
10535
10536 /* Implement the "insert" breakpoint_ops method for hardware watchpoints.  */
10537
10538 static int
10539 insert_watchpoint (struct bp_location *bl)
10540 {
10541   struct watchpoint *w = (struct watchpoint *) bl->owner;
10542   int length = w->exact ? 1 : bl->length;
10543
10544   return target_insert_watchpoint (bl->address, length, bl->watchpoint_type,
10545                                    w->cond_exp);
10546 }
10547
10548 /* Implement the "remove" breakpoint_ops method for hardware watchpoints.  */
10549
10550 static int
10551 remove_watchpoint (struct bp_location *bl)
10552 {
10553   struct watchpoint *w = (struct watchpoint *) bl->owner;
10554   int length = w->exact ? 1 : bl->length;
10555
10556   return target_remove_watchpoint (bl->address, length, bl->watchpoint_type,
10557                                    w->cond_exp);
10558 }
10559
10560 static int
10561 breakpoint_hit_watchpoint (const struct bp_location *bl,
10562                            struct address_space *aspace, CORE_ADDR bp_addr,
10563                            const struct target_waitstatus *ws)
10564 {
10565   struct breakpoint *b = bl->owner;
10566   struct watchpoint *w = (struct watchpoint *) b;
10567
10568   /* Continuable hardware watchpoints are treated as non-existent if the
10569      reason we stopped wasn't a hardware watchpoint (we didn't stop on
10570      some data address).  Otherwise gdb won't stop on a break instruction
10571      in the code (not from a breakpoint) when a hardware watchpoint has
10572      been defined.  Also skip watchpoints which we know did not trigger
10573      (did not match the data address).  */
10574   if (is_hardware_watchpoint (b)
10575       && w->watchpoint_triggered == watch_triggered_no)
10576     return 0;
10577
10578   return 1;
10579 }
10580
10581 static void
10582 check_status_watchpoint (bpstat bs)
10583 {
10584   gdb_assert (is_watchpoint (bs->breakpoint_at));
10585
10586   bpstat_check_watchpoint (bs);
10587 }
10588
10589 /* Implement the "resources_needed" breakpoint_ops method for
10590    hardware watchpoints.  */
10591
10592 static int
10593 resources_needed_watchpoint (const struct bp_location *bl)
10594 {
10595   struct watchpoint *w = (struct watchpoint *) bl->owner;
10596   int length = w->exact? 1 : bl->length;
10597
10598   return target_region_ok_for_hw_watchpoint (bl->address, length);
10599 }
10600
10601 /* Implement the "works_in_software_mode" breakpoint_ops method for
10602    hardware watchpoints.  */
10603
10604 static int
10605 works_in_software_mode_watchpoint (const struct breakpoint *b)
10606 {
10607   /* Read and access watchpoints only work with hardware support.  */
10608   return b->type == bp_watchpoint || b->type == bp_hardware_watchpoint;
10609 }
10610
10611 static enum print_stop_action
10612 print_it_watchpoint (bpstat bs)
10613 {
10614   struct cleanup *old_chain;
10615   struct breakpoint *b;
10616   struct ui_file *stb;
10617   enum print_stop_action result;
10618   struct watchpoint *w;
10619   struct ui_out *uiout = current_uiout;
10620
10621   gdb_assert (bs->bp_location_at != NULL);
10622
10623   b = bs->breakpoint_at;
10624   w = (struct watchpoint *) b;
10625
10626   stb = mem_fileopen ();
10627   old_chain = make_cleanup_ui_file_delete (stb);
10628
10629   switch (b->type)
10630     {
10631     case bp_watchpoint:
10632     case bp_hardware_watchpoint:
10633       annotate_watchpoint (b->number);
10634       if (ui_out_is_mi_like_p (uiout))
10635         ui_out_field_string
10636           (uiout, "reason",
10637            async_reason_lookup (EXEC_ASYNC_WATCHPOINT_TRIGGER));
10638       mention (b);
10639       make_cleanup_ui_out_tuple_begin_end (uiout, "value");
10640       ui_out_text (uiout, "\nOld value = ");
10641       watchpoint_value_print (bs->old_val, stb);
10642       ui_out_field_stream (uiout, "old", stb);
10643       ui_out_text (uiout, "\nNew value = ");
10644       watchpoint_value_print (w->val, stb);
10645       ui_out_field_stream (uiout, "new", stb);
10646       ui_out_text (uiout, "\n");
10647       /* More than one watchpoint may have been triggered.  */
10648       result = PRINT_UNKNOWN;
10649       break;
10650
10651     case bp_read_watchpoint:
10652       if (ui_out_is_mi_like_p (uiout))
10653         ui_out_field_string
10654           (uiout, "reason",
10655            async_reason_lookup (EXEC_ASYNC_READ_WATCHPOINT_TRIGGER));
10656       mention (b);
10657       make_cleanup_ui_out_tuple_begin_end (uiout, "value");
10658       ui_out_text (uiout, "\nValue = ");
10659       watchpoint_value_print (w->val, stb);
10660       ui_out_field_stream (uiout, "value", stb);
10661       ui_out_text (uiout, "\n");
10662       result = PRINT_UNKNOWN;
10663       break;
10664
10665     case bp_access_watchpoint:
10666       if (bs->old_val != NULL)
10667         {
10668           annotate_watchpoint (b->number);
10669           if (ui_out_is_mi_like_p (uiout))
10670             ui_out_field_string
10671               (uiout, "reason",
10672                async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
10673           mention (b);
10674           make_cleanup_ui_out_tuple_begin_end (uiout, "value");
10675           ui_out_text (uiout, "\nOld value = ");
10676           watchpoint_value_print (bs->old_val, stb);
10677           ui_out_field_stream (uiout, "old", stb);
10678           ui_out_text (uiout, "\nNew value = ");
10679         }
10680       else
10681         {
10682           mention (b);
10683           if (ui_out_is_mi_like_p (uiout))
10684             ui_out_field_string
10685               (uiout, "reason",
10686                async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
10687           make_cleanup_ui_out_tuple_begin_end (uiout, "value");
10688           ui_out_text (uiout, "\nValue = ");
10689         }
10690       watchpoint_value_print (w->val, stb);
10691       ui_out_field_stream (uiout, "new", stb);
10692       ui_out_text (uiout, "\n");
10693       result = PRINT_UNKNOWN;
10694       break;
10695     default:
10696       result = PRINT_UNKNOWN;
10697     }
10698
10699   do_cleanups (old_chain);
10700   return result;
10701 }
10702
10703 /* Implement the "print_mention" breakpoint_ops method for hardware
10704    watchpoints.  */
10705
10706 static void
10707 print_mention_watchpoint (struct breakpoint *b)
10708 {
10709   struct cleanup *ui_out_chain;
10710   struct watchpoint *w = (struct watchpoint *) b;
10711   struct ui_out *uiout = current_uiout;
10712
10713   switch (b->type)
10714     {
10715     case bp_watchpoint:
10716       ui_out_text (uiout, "Watchpoint ");
10717       ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "wpt");
10718       break;
10719     case bp_hardware_watchpoint:
10720       ui_out_text (uiout, "Hardware watchpoint ");
10721       ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "wpt");
10722       break;
10723     case bp_read_watchpoint:
10724       ui_out_text (uiout, "Hardware read watchpoint ");
10725       ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "hw-rwpt");
10726       break;
10727     case bp_access_watchpoint:
10728       ui_out_text (uiout, "Hardware access (read/write) watchpoint ");
10729       ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "hw-awpt");
10730       break;
10731     default:
10732       internal_error (__FILE__, __LINE__,
10733                       _("Invalid hardware watchpoint type."));
10734     }
10735
10736   ui_out_field_int (uiout, "number", b->number);
10737   ui_out_text (uiout, ": ");
10738   ui_out_field_string (uiout, "exp", w->exp_string);
10739   do_cleanups (ui_out_chain);
10740 }
10741
10742 /* Implement the "print_recreate" breakpoint_ops method for
10743    watchpoints.  */
10744
10745 static void
10746 print_recreate_watchpoint (struct breakpoint *b, struct ui_file *fp)
10747 {
10748   struct watchpoint *w = (struct watchpoint *) b;
10749
10750   switch (b->type)
10751     {
10752     case bp_watchpoint:
10753     case bp_hardware_watchpoint:
10754       fprintf_unfiltered (fp, "watch");
10755       break;
10756     case bp_read_watchpoint:
10757       fprintf_unfiltered (fp, "rwatch");
10758       break;
10759     case bp_access_watchpoint:
10760       fprintf_unfiltered (fp, "awatch");
10761       break;
10762     default:
10763       internal_error (__FILE__, __LINE__,
10764                       _("Invalid watchpoint type."));
10765     }
10766
10767   fprintf_unfiltered (fp, " %s", w->exp_string);
10768   print_recreate_thread (b, fp);
10769 }
10770
10771 /* Implement the "explains_signal" breakpoint_ops method for
10772    watchpoints.  */
10773
10774 static enum bpstat_signal_value
10775 explains_signal_watchpoint (struct breakpoint *b, enum gdb_signal sig)
10776 {
10777   /* A software watchpoint cannot cause a signal other than
10778      GDB_SIGNAL_TRAP.  */
10779   if (b->type == bp_watchpoint && sig != GDB_SIGNAL_TRAP)
10780     return BPSTAT_SIGNAL_NO;
10781
10782   return BPSTAT_SIGNAL_HIDE;
10783 }
10784
10785 /* The breakpoint_ops structure to be used in hardware watchpoints.  */
10786
10787 static struct breakpoint_ops watchpoint_breakpoint_ops;
10788
10789 /* Implement the "insert" breakpoint_ops method for
10790    masked hardware watchpoints.  */
10791
10792 static int
10793 insert_masked_watchpoint (struct bp_location *bl)
10794 {
10795   struct watchpoint *w = (struct watchpoint *) bl->owner;
10796
10797   return target_insert_mask_watchpoint (bl->address, w->hw_wp_mask,
10798                                         bl->watchpoint_type);
10799 }
10800
10801 /* Implement the "remove" breakpoint_ops method for
10802    masked hardware watchpoints.  */
10803
10804 static int
10805 remove_masked_watchpoint (struct bp_location *bl)
10806 {
10807   struct watchpoint *w = (struct watchpoint *) bl->owner;
10808
10809   return target_remove_mask_watchpoint (bl->address, w->hw_wp_mask,
10810                                         bl->watchpoint_type);
10811 }
10812
10813 /* Implement the "resources_needed" breakpoint_ops method for
10814    masked hardware watchpoints.  */
10815
10816 static int
10817 resources_needed_masked_watchpoint (const struct bp_location *bl)
10818 {
10819   struct watchpoint *w = (struct watchpoint *) bl->owner;
10820
10821   return target_masked_watch_num_registers (bl->address, w->hw_wp_mask);
10822 }
10823
10824 /* Implement the "works_in_software_mode" breakpoint_ops method for
10825    masked hardware watchpoints.  */
10826
10827 static int
10828 works_in_software_mode_masked_watchpoint (const struct breakpoint *b)
10829 {
10830   return 0;
10831 }
10832
10833 /* Implement the "print_it" breakpoint_ops method for
10834    masked hardware watchpoints.  */
10835
10836 static enum print_stop_action
10837 print_it_masked_watchpoint (bpstat bs)
10838 {
10839   struct breakpoint *b = bs->breakpoint_at;
10840   struct ui_out *uiout = current_uiout;
10841
10842   /* Masked watchpoints have only one location.  */
10843   gdb_assert (b->loc && b->loc->next == NULL);
10844
10845   switch (b->type)
10846     {
10847     case bp_hardware_watchpoint:
10848       annotate_watchpoint (b->number);
10849       if (ui_out_is_mi_like_p (uiout))
10850         ui_out_field_string
10851           (uiout, "reason",
10852            async_reason_lookup (EXEC_ASYNC_WATCHPOINT_TRIGGER));
10853       break;
10854
10855     case bp_read_watchpoint:
10856       if (ui_out_is_mi_like_p (uiout))
10857         ui_out_field_string
10858           (uiout, "reason",
10859            async_reason_lookup (EXEC_ASYNC_READ_WATCHPOINT_TRIGGER));
10860       break;
10861
10862     case bp_access_watchpoint:
10863       if (ui_out_is_mi_like_p (uiout))
10864         ui_out_field_string
10865           (uiout, "reason",
10866            async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
10867       break;
10868     default:
10869       internal_error (__FILE__, __LINE__,
10870                       _("Invalid hardware watchpoint type."));
10871     }
10872
10873   mention (b);
10874   ui_out_text (uiout, _("\n\
10875 Check the underlying instruction at PC for the memory\n\
10876 address and value which triggered this watchpoint.\n"));
10877   ui_out_text (uiout, "\n");
10878
10879   /* More than one watchpoint may have been triggered.  */
10880   return PRINT_UNKNOWN;
10881 }
10882
10883 /* Implement the "print_one_detail" breakpoint_ops method for
10884    masked hardware watchpoints.  */
10885
10886 static void
10887 print_one_detail_masked_watchpoint (const struct breakpoint *b,
10888                                     struct ui_out *uiout)
10889 {
10890   struct watchpoint *w = (struct watchpoint *) b;
10891
10892   /* Masked watchpoints have only one location.  */
10893   gdb_assert (b->loc && b->loc->next == NULL);
10894
10895   ui_out_text (uiout, "\tmask ");
10896   ui_out_field_core_addr (uiout, "mask", b->loc->gdbarch, w->hw_wp_mask);
10897   ui_out_text (uiout, "\n");
10898 }
10899
10900 /* Implement the "print_mention" breakpoint_ops method for
10901    masked hardware watchpoints.  */
10902
10903 static void
10904 print_mention_masked_watchpoint (struct breakpoint *b)
10905 {
10906   struct watchpoint *w = (struct watchpoint *) b;
10907   struct ui_out *uiout = current_uiout;
10908   struct cleanup *ui_out_chain;
10909
10910   switch (b->type)
10911     {
10912     case bp_hardware_watchpoint:
10913       ui_out_text (uiout, "Masked hardware watchpoint ");
10914       ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "wpt");
10915       break;
10916     case bp_read_watchpoint:
10917       ui_out_text (uiout, "Masked hardware read watchpoint ");
10918       ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "hw-rwpt");
10919       break;
10920     case bp_access_watchpoint:
10921       ui_out_text (uiout, "Masked hardware access (read/write) watchpoint ");
10922       ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "hw-awpt");
10923       break;
10924     default:
10925       internal_error (__FILE__, __LINE__,
10926                       _("Invalid hardware watchpoint type."));
10927     }
10928
10929   ui_out_field_int (uiout, "number", b->number);
10930   ui_out_text (uiout, ": ");
10931   ui_out_field_string (uiout, "exp", w->exp_string);
10932   do_cleanups (ui_out_chain);
10933 }
10934
10935 /* Implement the "print_recreate" breakpoint_ops method for
10936    masked hardware watchpoints.  */
10937
10938 static void
10939 print_recreate_masked_watchpoint (struct breakpoint *b, struct ui_file *fp)
10940 {
10941   struct watchpoint *w = (struct watchpoint *) b;
10942   char tmp[40];
10943
10944   switch (b->type)
10945     {
10946     case bp_hardware_watchpoint:
10947       fprintf_unfiltered (fp, "watch");
10948       break;
10949     case bp_read_watchpoint:
10950       fprintf_unfiltered (fp, "rwatch");
10951       break;
10952     case bp_access_watchpoint:
10953       fprintf_unfiltered (fp, "awatch");
10954       break;
10955     default:
10956       internal_error (__FILE__, __LINE__,
10957                       _("Invalid hardware watchpoint type."));
10958     }
10959
10960   sprintf_vma (tmp, w->hw_wp_mask);
10961   fprintf_unfiltered (fp, " %s mask 0x%s", w->exp_string, tmp);
10962   print_recreate_thread (b, fp);
10963 }
10964
10965 /* The breakpoint_ops structure to be used in masked hardware watchpoints.  */
10966
10967 static struct breakpoint_ops masked_watchpoint_breakpoint_ops;
10968
10969 /* Tell whether the given watchpoint is a masked hardware watchpoint.  */
10970
10971 static int
10972 is_masked_watchpoint (const struct breakpoint *b)
10973 {
10974   return b->ops == &masked_watchpoint_breakpoint_ops;
10975 }
10976
10977 /* accessflag:  hw_write:  watch write, 
10978                 hw_read:   watch read, 
10979                 hw_access: watch access (read or write) */
10980 static void
10981 watch_command_1 (const char *arg, int accessflag, int from_tty,
10982                  int just_location, int internal)
10983 {
10984   volatile struct gdb_exception e;
10985   struct breakpoint *b, *scope_breakpoint = NULL;
10986   struct expression *exp;
10987   const struct block *exp_valid_block = NULL, *cond_exp_valid_block = NULL;
10988   struct value *val, *mark, *result;
10989   struct frame_info *frame;
10990   const char *exp_start = NULL;
10991   const char *exp_end = NULL;
10992   const char *tok, *end_tok;
10993   int toklen = -1;
10994   const char *cond_start = NULL;
10995   const char *cond_end = NULL;
10996   enum bptype bp_type;
10997   int thread = -1;
10998   int pc = 0;
10999   /* Flag to indicate whether we are going to use masks for
11000      the hardware watchpoint.  */
11001   int use_mask = 0;
11002   CORE_ADDR mask = 0;
11003   struct watchpoint *w;
11004   char *expression;
11005   struct cleanup *back_to;
11006
11007   /* Make sure that we actually have parameters to parse.  */
11008   if (arg != NULL && arg[0] != '\0')
11009     {
11010       const char *value_start;
11011
11012       exp_end = arg + strlen (arg);
11013
11014       /* Look for "parameter value" pairs at the end
11015          of the arguments string.  */
11016       for (tok = exp_end - 1; tok > arg; tok--)
11017         {
11018           /* Skip whitespace at the end of the argument list.  */
11019           while (tok > arg && (*tok == ' ' || *tok == '\t'))
11020             tok--;
11021
11022           /* Find the beginning of the last token.
11023              This is the value of the parameter.  */
11024           while (tok > arg && (*tok != ' ' && *tok != '\t'))
11025             tok--;
11026           value_start = tok + 1;
11027
11028           /* Skip whitespace.  */
11029           while (tok > arg && (*tok == ' ' || *tok == '\t'))
11030             tok--;
11031
11032           end_tok = tok;
11033
11034           /* Find the beginning of the second to last token.
11035              This is the parameter itself.  */
11036           while (tok > arg && (*tok != ' ' && *tok != '\t'))
11037             tok--;
11038           tok++;
11039           toklen = end_tok - tok + 1;
11040
11041           if (toklen == 6 && !strncmp (tok, "thread", 6))
11042             {
11043               /* At this point we've found a "thread" token, which means
11044                  the user is trying to set a watchpoint that triggers
11045                  only in a specific thread.  */
11046               char *endp;
11047
11048               if (thread != -1)
11049                 error(_("You can specify only one thread."));
11050
11051               /* Extract the thread ID from the next token.  */
11052               thread = strtol (value_start, &endp, 0);
11053
11054               /* Check if the user provided a valid numeric value for the
11055                  thread ID.  */
11056               if (*endp != ' ' && *endp != '\t' && *endp != '\0')
11057                 error (_("Invalid thread ID specification %s."), value_start);
11058
11059               /* Check if the thread actually exists.  */
11060               if (!valid_thread_id (thread))
11061                 invalid_thread_id_error (thread);
11062             }
11063           else if (toklen == 4 && !strncmp (tok, "mask", 4))
11064             {
11065               /* We've found a "mask" token, which means the user wants to
11066                  create a hardware watchpoint that is going to have the mask
11067                  facility.  */
11068               struct value *mask_value, *mark;
11069
11070               if (use_mask)
11071                 error(_("You can specify only one mask."));
11072
11073               use_mask = just_location = 1;
11074
11075               mark = value_mark ();
11076               mask_value = parse_to_comma_and_eval (&value_start);
11077               mask = value_as_address (mask_value);
11078               value_free_to_mark (mark);
11079             }
11080           else
11081             /* We didn't recognize what we found.  We should stop here.  */
11082             break;
11083
11084           /* Truncate the string and get rid of the "parameter value" pair before
11085              the arguments string is parsed by the parse_exp_1 function.  */
11086           exp_end = tok;
11087         }
11088     }
11089   else
11090     exp_end = arg;
11091
11092   /* Parse the rest of the arguments.  From here on out, everything
11093      is in terms of a newly allocated string instead of the original
11094      ARG.  */
11095   innermost_block = NULL;
11096   expression = savestring (arg, exp_end - arg);
11097   back_to = make_cleanup (xfree, expression);
11098   exp_start = arg = expression;
11099   exp = parse_exp_1 (&arg, 0, 0, 0);
11100   exp_end = arg;
11101   /* Remove trailing whitespace from the expression before saving it.
11102      This makes the eventual display of the expression string a bit
11103      prettier.  */
11104   while (exp_end > exp_start && (exp_end[-1] == ' ' || exp_end[-1] == '\t'))
11105     --exp_end;
11106
11107   /* Checking if the expression is not constant.  */
11108   if (watchpoint_exp_is_const (exp))
11109     {
11110       int len;
11111
11112       len = exp_end - exp_start;
11113       while (len > 0 && isspace (exp_start[len - 1]))
11114         len--;
11115       error (_("Cannot watch constant value `%.*s'."), len, exp_start);
11116     }
11117
11118   exp_valid_block = innermost_block;
11119   mark = value_mark ();
11120   fetch_subexp_value (exp, &pc, &val, &result, NULL, just_location);
11121
11122   if (just_location)
11123     {
11124       int ret;
11125
11126       exp_valid_block = NULL;
11127       val = value_addr (result);
11128       release_value (val);
11129       value_free_to_mark (mark);
11130
11131       if (use_mask)
11132         {
11133           ret = target_masked_watch_num_registers (value_as_address (val),
11134                                                    mask);
11135           if (ret == -1)
11136             error (_("This target does not support masked watchpoints."));
11137           else if (ret == -2)
11138             error (_("Invalid mask or memory region."));
11139         }
11140     }
11141   else if (val != NULL)
11142     release_value (val);
11143
11144   tok = skip_spaces_const (arg);
11145   end_tok = skip_to_space_const (tok);
11146
11147   toklen = end_tok - tok;
11148   if (toklen >= 1 && strncmp (tok, "if", toklen) == 0)
11149     {
11150       struct expression *cond;
11151
11152       innermost_block = NULL;
11153       tok = cond_start = end_tok + 1;
11154       cond = parse_exp_1 (&tok, 0, 0, 0);
11155
11156       /* The watchpoint expression may not be local, but the condition
11157          may still be.  E.g.: `watch global if local > 0'.  */
11158       cond_exp_valid_block = innermost_block;
11159
11160       xfree (cond);
11161       cond_end = tok;
11162     }
11163   if (*tok)
11164     error (_("Junk at end of command."));
11165
11166   frame = block_innermost_frame (exp_valid_block);
11167
11168   /* If the expression is "local", then set up a "watchpoint scope"
11169      breakpoint at the point where we've left the scope of the watchpoint
11170      expression.  Create the scope breakpoint before the watchpoint, so
11171      that we will encounter it first in bpstat_stop_status.  */
11172   if (exp_valid_block && frame)
11173     {
11174       if (frame_id_p (frame_unwind_caller_id (frame)))
11175         {
11176           scope_breakpoint
11177             = create_internal_breakpoint (frame_unwind_caller_arch (frame),
11178                                           frame_unwind_caller_pc (frame),
11179                                           bp_watchpoint_scope,
11180                                           &momentary_breakpoint_ops);
11181
11182           scope_breakpoint->enable_state = bp_enabled;
11183
11184           /* Automatically delete the breakpoint when it hits.  */
11185           scope_breakpoint->disposition = disp_del;
11186
11187           /* Only break in the proper frame (help with recursion).  */
11188           scope_breakpoint->frame_id = frame_unwind_caller_id (frame);
11189
11190           /* Set the address at which we will stop.  */
11191           scope_breakpoint->loc->gdbarch
11192             = frame_unwind_caller_arch (frame);
11193           scope_breakpoint->loc->requested_address
11194             = frame_unwind_caller_pc (frame);
11195           scope_breakpoint->loc->address
11196             = adjust_breakpoint_address (scope_breakpoint->loc->gdbarch,
11197                                          scope_breakpoint->loc->requested_address,
11198                                          scope_breakpoint->type);
11199         }
11200     }
11201
11202   /* Now set up the breakpoint.  We create all watchpoints as hardware
11203      watchpoints here even if hardware watchpoints are turned off, a call
11204      to update_watchpoint later in this function will cause the type to
11205      drop back to bp_watchpoint (software watchpoint) if required.  */
11206
11207   if (accessflag == hw_read)
11208     bp_type = bp_read_watchpoint;
11209   else if (accessflag == hw_access)
11210     bp_type = bp_access_watchpoint;
11211   else
11212     bp_type = bp_hardware_watchpoint;
11213
11214   w = XCNEW (struct watchpoint);
11215   b = &w->base;
11216   if (use_mask)
11217     init_raw_breakpoint_without_location (b, NULL, bp_type,
11218                                           &masked_watchpoint_breakpoint_ops);
11219   else
11220     init_raw_breakpoint_without_location (b, NULL, bp_type,
11221                                           &watchpoint_breakpoint_ops);
11222   b->thread = thread;
11223   b->disposition = disp_donttouch;
11224   b->pspace = current_program_space;
11225   w->exp = exp;
11226   w->exp_valid_block = exp_valid_block;
11227   w->cond_exp_valid_block = cond_exp_valid_block;
11228   if (just_location)
11229     {
11230       struct type *t = value_type (val);
11231       CORE_ADDR addr = value_as_address (val);
11232       char *name;
11233
11234       t = check_typedef (TYPE_TARGET_TYPE (check_typedef (t)));
11235       name = type_to_string (t);
11236
11237       w->exp_string_reparse = xstrprintf ("* (%s *) %s", name,
11238                                           core_addr_to_string (addr));
11239       xfree (name);
11240
11241       w->exp_string = xstrprintf ("-location %.*s",
11242                                   (int) (exp_end - exp_start), exp_start);
11243
11244       /* The above expression is in C.  */
11245       b->language = language_c;
11246     }
11247   else
11248     w->exp_string = savestring (exp_start, exp_end - exp_start);
11249
11250   if (use_mask)
11251     {
11252       w->hw_wp_mask = mask;
11253     }
11254   else
11255     {
11256       w->val = val;
11257       w->val_valid = 1;
11258     }
11259
11260   if (cond_start)
11261     b->cond_string = savestring (cond_start, cond_end - cond_start);
11262   else
11263     b->cond_string = 0;
11264
11265   if (frame)
11266     {
11267       w->watchpoint_frame = get_frame_id (frame);
11268       w->watchpoint_thread = inferior_ptid;
11269     }
11270   else
11271     {
11272       w->watchpoint_frame = null_frame_id;
11273       w->watchpoint_thread = null_ptid;
11274     }
11275
11276   if (scope_breakpoint != NULL)
11277     {
11278       /* The scope breakpoint is related to the watchpoint.  We will
11279          need to act on them together.  */
11280       b->related_breakpoint = scope_breakpoint;
11281       scope_breakpoint->related_breakpoint = b;
11282     }
11283
11284   if (!just_location)
11285     value_free_to_mark (mark);
11286
11287   TRY_CATCH (e, RETURN_MASK_ALL)
11288     {
11289       /* Finally update the new watchpoint.  This creates the locations
11290          that should be inserted.  */
11291       update_watchpoint (w, 1);
11292     }
11293   if (e.reason < 0)
11294     {
11295       delete_breakpoint (b);
11296       throw_exception (e);
11297     }
11298
11299   install_breakpoint (internal, b, 1);
11300   do_cleanups (back_to);
11301 }
11302
11303 /* Return count of debug registers needed to watch the given expression.
11304    If the watchpoint cannot be handled in hardware return zero.  */
11305
11306 static int
11307 can_use_hardware_watchpoint (struct value *v)
11308 {
11309   int found_memory_cnt = 0;
11310   struct value *head = v;
11311
11312   /* Did the user specifically forbid us to use hardware watchpoints? */
11313   if (!can_use_hw_watchpoints)
11314     return 0;
11315
11316   /* Make sure that the value of the expression depends only upon
11317      memory contents, and values computed from them within GDB.  If we
11318      find any register references or function calls, we can't use a
11319      hardware watchpoint.
11320
11321      The idea here is that evaluating an expression generates a series
11322      of values, one holding the value of every subexpression.  (The
11323      expression a*b+c has five subexpressions: a, b, a*b, c, and
11324      a*b+c.)  GDB's values hold almost enough information to establish
11325      the criteria given above --- they identify memory lvalues,
11326      register lvalues, computed values, etcetera.  So we can evaluate
11327      the expression, and then scan the chain of values that leaves
11328      behind to decide whether we can detect any possible change to the
11329      expression's final value using only hardware watchpoints.
11330
11331      However, I don't think that the values returned by inferior
11332      function calls are special in any way.  So this function may not
11333      notice that an expression involving an inferior function call
11334      can't be watched with hardware watchpoints.  FIXME.  */
11335   for (; v; v = value_next (v))
11336     {
11337       if (VALUE_LVAL (v) == lval_memory)
11338         {
11339           if (v != head && value_lazy (v))
11340             /* A lazy memory lvalue in the chain is one that GDB never
11341                needed to fetch; we either just used its address (e.g.,
11342                `a' in `a.b') or we never needed it at all (e.g., `a'
11343                in `a,b').  This doesn't apply to HEAD; if that is
11344                lazy then it was not readable, but watch it anyway.  */
11345             ;
11346           else
11347             {
11348               /* Ahh, memory we actually used!  Check if we can cover
11349                  it with hardware watchpoints.  */
11350               struct type *vtype = check_typedef (value_type (v));
11351
11352               /* We only watch structs and arrays if user asked for it
11353                  explicitly, never if they just happen to appear in a
11354                  middle of some value chain.  */
11355               if (v == head
11356                   || (TYPE_CODE (vtype) != TYPE_CODE_STRUCT
11357                       && TYPE_CODE (vtype) != TYPE_CODE_ARRAY))
11358                 {
11359                   CORE_ADDR vaddr = value_address (v);
11360                   int len;
11361                   int num_regs;
11362
11363                   len = (target_exact_watchpoints
11364                          && is_scalar_type_recursive (vtype))?
11365                     1 : TYPE_LENGTH (value_type (v));
11366
11367                   num_regs = target_region_ok_for_hw_watchpoint (vaddr, len);
11368                   if (!num_regs)
11369                     return 0;
11370                   else
11371                     found_memory_cnt += num_regs;
11372                 }
11373             }
11374         }
11375       else if (VALUE_LVAL (v) != not_lval
11376                && deprecated_value_modifiable (v) == 0)
11377         return 0;       /* These are values from the history (e.g., $1).  */
11378       else if (VALUE_LVAL (v) == lval_register)
11379         return 0;       /* Cannot watch a register with a HW watchpoint.  */
11380     }
11381
11382   /* The expression itself looks suitable for using a hardware
11383      watchpoint, but give the target machine a chance to reject it.  */
11384   return found_memory_cnt;
11385 }
11386
11387 void
11388 watch_command_wrapper (char *arg, int from_tty, int internal)
11389 {
11390   watch_command_1 (arg, hw_write, from_tty, 0, internal);
11391 }
11392
11393 /* A helper function that looks for the "-location" argument and then
11394    calls watch_command_1.  */
11395
11396 static void
11397 watch_maybe_just_location (char *arg, int accessflag, int from_tty)
11398 {
11399   int just_location = 0;
11400
11401   if (arg
11402       && (check_for_argument (&arg, "-location", sizeof ("-location") - 1)
11403           || check_for_argument (&arg, "-l", sizeof ("-l") - 1)))
11404     {
11405       arg = skip_spaces (arg);
11406       just_location = 1;
11407     }
11408
11409   watch_command_1 (arg, accessflag, from_tty, just_location, 0);
11410 }
11411
11412 static void
11413 watch_command (char *arg, int from_tty)
11414 {
11415   watch_maybe_just_location (arg, hw_write, from_tty);
11416 }
11417
11418 void
11419 rwatch_command_wrapper (char *arg, int from_tty, int internal)
11420 {
11421   watch_command_1 (arg, hw_read, from_tty, 0, internal);
11422 }
11423
11424 static void
11425 rwatch_command (char *arg, int from_tty)
11426 {
11427   watch_maybe_just_location (arg, hw_read, from_tty);
11428 }
11429
11430 void
11431 awatch_command_wrapper (char *arg, int from_tty, int internal)
11432 {
11433   watch_command_1 (arg, hw_access, from_tty, 0, internal);
11434 }
11435
11436 static void
11437 awatch_command (char *arg, int from_tty)
11438 {
11439   watch_maybe_just_location (arg, hw_access, from_tty);
11440 }
11441 \f
11442
11443 /* Helper routines for the until_command routine in infcmd.c.  Here
11444    because it uses the mechanisms of breakpoints.  */
11445
11446 struct until_break_command_continuation_args
11447 {
11448   struct breakpoint *breakpoint;
11449   struct breakpoint *breakpoint2;
11450   int thread_num;
11451 };
11452
11453 /* This function is called by fetch_inferior_event via the
11454    cmd_continuation pointer, to complete the until command.  It takes
11455    care of cleaning up the temporary breakpoints set up by the until
11456    command.  */
11457 static void
11458 until_break_command_continuation (void *arg, int err)
11459 {
11460   struct until_break_command_continuation_args *a = arg;
11461
11462   delete_breakpoint (a->breakpoint);
11463   if (a->breakpoint2)
11464     delete_breakpoint (a->breakpoint2);
11465   delete_longjmp_breakpoint (a->thread_num);
11466 }
11467
11468 void
11469 until_break_command (char *arg, int from_tty, int anywhere)
11470 {
11471   struct symtabs_and_lines sals;
11472   struct symtab_and_line sal;
11473   struct frame_info *frame;
11474   struct gdbarch *frame_gdbarch;
11475   struct frame_id stack_frame_id;
11476   struct frame_id caller_frame_id;
11477   struct breakpoint *breakpoint;
11478   struct breakpoint *breakpoint2 = NULL;
11479   struct cleanup *old_chain;
11480   int thread;
11481   struct thread_info *tp;
11482
11483   clear_proceed_status ();
11484
11485   /* Set a breakpoint where the user wants it and at return from
11486      this function.  */
11487
11488   if (last_displayed_sal_is_valid ())
11489     sals = decode_line_1 (&arg, DECODE_LINE_FUNFIRSTLINE,
11490                           get_last_displayed_symtab (),
11491                           get_last_displayed_line ());
11492   else
11493     sals = decode_line_1 (&arg, DECODE_LINE_FUNFIRSTLINE,
11494                           (struct symtab *) NULL, 0);
11495
11496   if (sals.nelts != 1)
11497     error (_("Couldn't get information on specified line."));
11498
11499   sal = sals.sals[0];
11500   xfree (sals.sals);    /* malloc'd, so freed.  */
11501
11502   if (*arg)
11503     error (_("Junk at end of arguments."));
11504
11505   resolve_sal_pc (&sal);
11506
11507   tp = inferior_thread ();
11508   thread = tp->num;
11509
11510   old_chain = make_cleanup (null_cleanup, NULL);
11511
11512   /* Note linespec handling above invalidates the frame chain.
11513      Installing a breakpoint also invalidates the frame chain (as it
11514      may need to switch threads), so do any frame handling before
11515      that.  */
11516
11517   frame = get_selected_frame (NULL);
11518   frame_gdbarch = get_frame_arch (frame);
11519   stack_frame_id = get_stack_frame_id (frame);
11520   caller_frame_id = frame_unwind_caller_id (frame);
11521
11522   /* Keep within the current frame, or in frames called by the current
11523      one.  */
11524
11525   if (frame_id_p (caller_frame_id))
11526     {
11527       struct symtab_and_line sal2;
11528
11529       sal2 = find_pc_line (frame_unwind_caller_pc (frame), 0);
11530       sal2.pc = frame_unwind_caller_pc (frame);
11531       breakpoint2 = set_momentary_breakpoint (frame_unwind_caller_arch (frame),
11532                                               sal2,
11533                                               caller_frame_id,
11534                                               bp_until);
11535       make_cleanup_delete_breakpoint (breakpoint2);
11536
11537       set_longjmp_breakpoint (tp, caller_frame_id);
11538       make_cleanup (delete_longjmp_breakpoint_cleanup, &thread);
11539     }
11540
11541   /* set_momentary_breakpoint could invalidate FRAME.  */
11542   frame = NULL;
11543
11544   if (anywhere)
11545     /* If the user told us to continue until a specified location,
11546        we don't specify a frame at which we need to stop.  */
11547     breakpoint = set_momentary_breakpoint (frame_gdbarch, sal,
11548                                            null_frame_id, bp_until);
11549   else
11550     /* Otherwise, specify the selected frame, because we want to stop
11551        only at the very same frame.  */
11552     breakpoint = set_momentary_breakpoint (frame_gdbarch, sal,
11553                                            stack_frame_id, bp_until);
11554   make_cleanup_delete_breakpoint (breakpoint);
11555
11556   proceed (-1, GDB_SIGNAL_DEFAULT, 0);
11557
11558   /* If we are running asynchronously, and proceed call above has
11559      actually managed to start the target, arrange for breakpoints to
11560      be deleted when the target stops.  Otherwise, we're already
11561      stopped and delete breakpoints via cleanup chain.  */
11562
11563   if (target_can_async_p () && is_running (inferior_ptid))
11564     {
11565       struct until_break_command_continuation_args *args;
11566       args = xmalloc (sizeof (*args));
11567
11568       args->breakpoint = breakpoint;
11569       args->breakpoint2 = breakpoint2;
11570       args->thread_num = thread;
11571
11572       discard_cleanups (old_chain);
11573       add_continuation (inferior_thread (),
11574                         until_break_command_continuation, args,
11575                         xfree);
11576     }
11577   else
11578     do_cleanups (old_chain);
11579 }
11580
11581 /* This function attempts to parse an optional "if <cond>" clause
11582    from the arg string.  If one is not found, it returns NULL.
11583
11584    Else, it returns a pointer to the condition string.  (It does not
11585    attempt to evaluate the string against a particular block.)  And,
11586    it updates arg to point to the first character following the parsed
11587    if clause in the arg string.  */
11588
11589 char *
11590 ep_parse_optional_if_clause (char **arg)
11591 {
11592   char *cond_string;
11593
11594   if (((*arg)[0] != 'i') || ((*arg)[1] != 'f') || !isspace ((*arg)[2]))
11595     return NULL;
11596
11597   /* Skip the "if" keyword.  */
11598   (*arg) += 2;
11599
11600   /* Skip any extra leading whitespace, and record the start of the
11601      condition string.  */
11602   *arg = skip_spaces (*arg);
11603   cond_string = *arg;
11604
11605   /* Assume that the condition occupies the remainder of the arg
11606      string.  */
11607   (*arg) += strlen (cond_string);
11608
11609   return cond_string;
11610 }
11611
11612 /* Commands to deal with catching events, such as signals, exceptions,
11613    process start/exit, etc.  */
11614
11615 typedef enum
11616 {
11617   catch_fork_temporary, catch_vfork_temporary,
11618   catch_fork_permanent, catch_vfork_permanent
11619 }
11620 catch_fork_kind;
11621
11622 static void
11623 catch_fork_command_1 (char *arg, int from_tty, 
11624                       struct cmd_list_element *command)
11625 {
11626   struct gdbarch *gdbarch = get_current_arch ();
11627   char *cond_string = NULL;
11628   catch_fork_kind fork_kind;
11629   int tempflag;
11630
11631   fork_kind = (catch_fork_kind) (uintptr_t) get_cmd_context (command);
11632   tempflag = (fork_kind == catch_fork_temporary
11633               || fork_kind == catch_vfork_temporary);
11634
11635   if (!arg)
11636     arg = "";
11637   arg = skip_spaces (arg);
11638
11639   /* The allowed syntax is:
11640      catch [v]fork
11641      catch [v]fork if <cond>
11642
11643      First, check if there's an if clause.  */
11644   cond_string = ep_parse_optional_if_clause (&arg);
11645
11646   if ((*arg != '\0') && !isspace (*arg))
11647     error (_("Junk at end of arguments."));
11648
11649   /* If this target supports it, create a fork or vfork catchpoint
11650      and enable reporting of such events.  */
11651   switch (fork_kind)
11652     {
11653     case catch_fork_temporary:
11654     case catch_fork_permanent:
11655       create_fork_vfork_event_catchpoint (gdbarch, tempflag, cond_string,
11656                                           &catch_fork_breakpoint_ops);
11657       break;
11658     case catch_vfork_temporary:
11659     case catch_vfork_permanent:
11660       create_fork_vfork_event_catchpoint (gdbarch, tempflag, cond_string,
11661                                           &catch_vfork_breakpoint_ops);
11662       break;
11663     default:
11664       error (_("unsupported or unknown fork kind; cannot catch it"));
11665       break;
11666     }
11667 }
11668
11669 static void
11670 catch_exec_command_1 (char *arg, int from_tty, 
11671                       struct cmd_list_element *command)
11672 {
11673   struct exec_catchpoint *c;
11674   struct gdbarch *gdbarch = get_current_arch ();
11675   int tempflag;
11676   char *cond_string = NULL;
11677
11678   tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
11679
11680   if (!arg)
11681     arg = "";
11682   arg = skip_spaces (arg);
11683
11684   /* The allowed syntax is:
11685      catch exec
11686      catch exec if <cond>
11687
11688      First, check if there's an if clause.  */
11689   cond_string = ep_parse_optional_if_clause (&arg);
11690
11691   if ((*arg != '\0') && !isspace (*arg))
11692     error (_("Junk at end of arguments."));
11693
11694   c = XNEW (struct exec_catchpoint);
11695   init_catchpoint (&c->base, gdbarch, tempflag, cond_string,
11696                    &catch_exec_breakpoint_ops);
11697   c->exec_pathname = NULL;
11698
11699   install_breakpoint (0, &c->base, 1);
11700 }
11701
11702 void
11703 init_ada_exception_breakpoint (struct breakpoint *b,
11704                                struct gdbarch *gdbarch,
11705                                struct symtab_and_line sal,
11706                                char *addr_string,
11707                                const struct breakpoint_ops *ops,
11708                                int tempflag,
11709                                int enabled,
11710                                int from_tty)
11711 {
11712   if (from_tty)
11713     {
11714       struct gdbarch *loc_gdbarch = get_sal_arch (sal);
11715       if (!loc_gdbarch)
11716         loc_gdbarch = gdbarch;
11717
11718       describe_other_breakpoints (loc_gdbarch,
11719                                   sal.pspace, sal.pc, sal.section, -1);
11720       /* FIXME: brobecker/2006-12-28: Actually, re-implement a special
11721          version for exception catchpoints, because two catchpoints
11722          used for different exception names will use the same address.
11723          In this case, a "breakpoint ... also set at..." warning is
11724          unproductive.  Besides, the warning phrasing is also a bit
11725          inappropriate, we should use the word catchpoint, and tell
11726          the user what type of catchpoint it is.  The above is good
11727          enough for now, though.  */
11728     }
11729
11730   init_raw_breakpoint (b, gdbarch, sal, bp_breakpoint, ops);
11731
11732   b->enable_state = enabled ? bp_enabled : bp_disabled;
11733   b->disposition = tempflag ? disp_del : disp_donttouch;
11734   b->addr_string = addr_string;
11735   b->language = language_ada;
11736 }
11737
11738 /* Splits the argument using space as delimiter.  Returns an xmalloc'd
11739    filter list, or NULL if no filtering is required.  */
11740 static VEC(int) *
11741 catch_syscall_split_args (char *arg)
11742 {
11743   VEC(int) *result = NULL;
11744   struct cleanup *cleanup = make_cleanup (VEC_cleanup (int), &result);
11745
11746   while (*arg != '\0')
11747     {
11748       int i, syscall_number;
11749       char *endptr;
11750       char cur_name[128];
11751       struct syscall s;
11752
11753       /* Skip whitespace.  */
11754       arg = skip_spaces (arg);
11755
11756       for (i = 0; i < 127 && arg[i] && !isspace (arg[i]); ++i)
11757         cur_name[i] = arg[i];
11758       cur_name[i] = '\0';
11759       arg += i;
11760
11761       /* Check if the user provided a syscall name or a number.  */
11762       syscall_number = (int) strtol (cur_name, &endptr, 0);
11763       if (*endptr == '\0')
11764         get_syscall_by_number (syscall_number, &s);
11765       else
11766         {
11767           /* We have a name.  Let's check if it's valid and convert it
11768              to a number.  */
11769           get_syscall_by_name (cur_name, &s);
11770
11771           if (s.number == UNKNOWN_SYSCALL)
11772             /* Here we have to issue an error instead of a warning,
11773                because GDB cannot do anything useful if there's no
11774                syscall number to be caught.  */
11775             error (_("Unknown syscall name '%s'."), cur_name);
11776         }
11777
11778       /* Ok, it's valid.  */
11779       VEC_safe_push (int, result, s.number);
11780     }
11781
11782   discard_cleanups (cleanup);
11783   return result;
11784 }
11785
11786 /* Implement the "catch syscall" command.  */
11787
11788 static void
11789 catch_syscall_command_1 (char *arg, int from_tty, 
11790                          struct cmd_list_element *command)
11791 {
11792   int tempflag;
11793   VEC(int) *filter;
11794   struct syscall s;
11795   struct gdbarch *gdbarch = get_current_arch ();
11796
11797   /* Checking if the feature if supported.  */
11798   if (gdbarch_get_syscall_number_p (gdbarch) == 0)
11799     error (_("The feature 'catch syscall' is not supported on \
11800 this architecture yet."));
11801
11802   tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
11803
11804   arg = skip_spaces (arg);
11805
11806   /* We need to do this first "dummy" translation in order
11807      to get the syscall XML file loaded or, most important,
11808      to display a warning to the user if there's no XML file
11809      for his/her architecture.  */
11810   get_syscall_by_number (0, &s);
11811
11812   /* The allowed syntax is:
11813      catch syscall
11814      catch syscall <name | number> [<name | number> ... <name | number>]
11815
11816      Let's check if there's a syscall name.  */
11817
11818   if (arg != NULL)
11819     filter = catch_syscall_split_args (arg);
11820   else
11821     filter = NULL;
11822
11823   create_syscall_event_catchpoint (tempflag, filter,
11824                                    &catch_syscall_breakpoint_ops);
11825 }
11826
11827 static void
11828 catch_command (char *arg, int from_tty)
11829 {
11830   error (_("Catch requires an event name."));
11831 }
11832 \f
11833
11834 static void
11835 tcatch_command (char *arg, int from_tty)
11836 {
11837   error (_("Catch requires an event name."));
11838 }
11839
11840 /* A qsort comparison function that sorts breakpoints in order.  */
11841
11842 static int
11843 compare_breakpoints (const void *a, const void *b)
11844 {
11845   const breakpoint_p *ba = a;
11846   uintptr_t ua = (uintptr_t) *ba;
11847   const breakpoint_p *bb = b;
11848   uintptr_t ub = (uintptr_t) *bb;
11849
11850   if ((*ba)->number < (*bb)->number)
11851     return -1;
11852   else if ((*ba)->number > (*bb)->number)
11853     return 1;
11854
11855   /* Now sort by address, in case we see, e..g, two breakpoints with
11856      the number 0.  */
11857   if (ua < ub)
11858     return -1;
11859   return ua > ub ? 1 : 0;
11860 }
11861
11862 /* Delete breakpoints by address or line.  */
11863
11864 static void
11865 clear_command (char *arg, int from_tty)
11866 {
11867   struct breakpoint *b, *prev;
11868   VEC(breakpoint_p) *found = 0;
11869   int ix;
11870   int default_match;
11871   struct symtabs_and_lines sals;
11872   struct symtab_and_line sal;
11873   int i;
11874   struct cleanup *cleanups = make_cleanup (null_cleanup, NULL);
11875
11876   if (arg)
11877     {
11878       sals = decode_line_with_current_source (arg,
11879                                               (DECODE_LINE_FUNFIRSTLINE
11880                                                | DECODE_LINE_LIST_MODE));
11881       make_cleanup (xfree, sals.sals);
11882       default_match = 0;
11883     }
11884   else
11885     {
11886       sals.sals = (struct symtab_and_line *)
11887         xmalloc (sizeof (struct symtab_and_line));
11888       make_cleanup (xfree, sals.sals);
11889       init_sal (&sal);          /* Initialize to zeroes.  */
11890
11891       /* Set sal's line, symtab, pc, and pspace to the values
11892          corresponding to the last call to print_frame_info.  If the
11893          codepoint is not valid, this will set all the fields to 0.  */
11894       get_last_displayed_sal (&sal);
11895       if (sal.symtab == 0)
11896         error (_("No source file specified."));
11897
11898       sals.sals[0] = sal;
11899       sals.nelts = 1;
11900
11901       default_match = 1;
11902     }
11903
11904   /* We don't call resolve_sal_pc here.  That's not as bad as it
11905      seems, because all existing breakpoints typically have both
11906      file/line and pc set.  So, if clear is given file/line, we can
11907      match this to existing breakpoint without obtaining pc at all.
11908
11909      We only support clearing given the address explicitly 
11910      present in breakpoint table.  Say, we've set breakpoint 
11911      at file:line.  There were several PC values for that file:line,
11912      due to optimization, all in one block.
11913
11914      We've picked one PC value.  If "clear" is issued with another
11915      PC corresponding to the same file:line, the breakpoint won't
11916      be cleared.  We probably can still clear the breakpoint, but 
11917      since the other PC value is never presented to user, user
11918      can only find it by guessing, and it does not seem important
11919      to support that.  */
11920
11921   /* For each line spec given, delete bps which correspond to it.  Do
11922      it in two passes, solely to preserve the current behavior that
11923      from_tty is forced true if we delete more than one
11924      breakpoint.  */
11925
11926   found = NULL;
11927   make_cleanup (VEC_cleanup (breakpoint_p), &found);
11928   for (i = 0; i < sals.nelts; i++)
11929     {
11930       const char *sal_fullname;
11931
11932       /* If exact pc given, clear bpts at that pc.
11933          If line given (pc == 0), clear all bpts on specified line.
11934          If defaulting, clear all bpts on default line
11935          or at default pc.
11936
11937          defaulting    sal.pc != 0    tests to do
11938
11939          0              1             pc
11940          1              1             pc _and_ line
11941          0              0             line
11942          1              0             <can't happen> */
11943
11944       sal = sals.sals[i];
11945       sal_fullname = (sal.symtab == NULL
11946                       ? NULL : symtab_to_fullname (sal.symtab));
11947
11948       /* Find all matching breakpoints and add them to 'found'.  */
11949       ALL_BREAKPOINTS (b)
11950         {
11951           int match = 0;
11952           /* Are we going to delete b?  */
11953           if (b->type != bp_none && !is_watchpoint (b))
11954             {
11955               struct bp_location *loc = b->loc;
11956               for (; loc; loc = loc->next)
11957                 {
11958                   /* If the user specified file:line, don't allow a PC
11959                      match.  This matches historical gdb behavior.  */
11960                   int pc_match = (!sal.explicit_line
11961                                   && sal.pc
11962                                   && (loc->pspace == sal.pspace)
11963                                   && (loc->address == sal.pc)
11964                                   && (!section_is_overlay (loc->section)
11965                                       || loc->section == sal.section));
11966                   int line_match = 0;
11967
11968                   if ((default_match || sal.explicit_line)
11969                       && loc->symtab != NULL
11970                       && sal_fullname != NULL
11971                       && sal.pspace == loc->pspace
11972                       && loc->line_number == sal.line
11973                       && filename_cmp (symtab_to_fullname (loc->symtab),
11974                                        sal_fullname) == 0)
11975                     line_match = 1;
11976
11977                   if (pc_match || line_match)
11978                     {
11979                       match = 1;
11980                       break;
11981                     }
11982                 }
11983             }
11984
11985           if (match)
11986             VEC_safe_push(breakpoint_p, found, b);
11987         }
11988     }
11989
11990   /* Now go thru the 'found' chain and delete them.  */
11991   if (VEC_empty(breakpoint_p, found))
11992     {
11993       if (arg)
11994         error (_("No breakpoint at %s."), arg);
11995       else
11996         error (_("No breakpoint at this line."));
11997     }
11998
11999   /* Remove duplicates from the vec.  */
12000   qsort (VEC_address (breakpoint_p, found),
12001          VEC_length (breakpoint_p, found),
12002          sizeof (breakpoint_p),
12003          compare_breakpoints);
12004   prev = VEC_index (breakpoint_p, found, 0);
12005   for (ix = 1; VEC_iterate (breakpoint_p, found, ix, b); ++ix)
12006     {
12007       if (b == prev)
12008         {
12009           VEC_ordered_remove (breakpoint_p, found, ix);
12010           --ix;
12011         }
12012     }
12013
12014   if (VEC_length(breakpoint_p, found) > 1)
12015     from_tty = 1;       /* Always report if deleted more than one.  */
12016   if (from_tty)
12017     {
12018       if (VEC_length(breakpoint_p, found) == 1)
12019         printf_unfiltered (_("Deleted breakpoint "));
12020       else
12021         printf_unfiltered (_("Deleted breakpoints "));
12022     }
12023
12024   for (ix = 0; VEC_iterate(breakpoint_p, found, ix, b); ix++)
12025     {
12026       if (from_tty)
12027         printf_unfiltered ("%d ", b->number);
12028       delete_breakpoint (b);
12029     }
12030   if (from_tty)
12031     putchar_unfiltered ('\n');
12032
12033   do_cleanups (cleanups);
12034 }
12035 \f
12036 /* Delete breakpoint in BS if they are `delete' breakpoints and
12037    all breakpoints that are marked for deletion, whether hit or not.
12038    This is called after any breakpoint is hit, or after errors.  */
12039
12040 void
12041 breakpoint_auto_delete (bpstat bs)
12042 {
12043   struct breakpoint *b, *b_tmp;
12044
12045   for (; bs; bs = bs->next)
12046     if (bs->breakpoint_at
12047         && bs->breakpoint_at->disposition == disp_del
12048         && bs->stop)
12049       delete_breakpoint (bs->breakpoint_at);
12050
12051   ALL_BREAKPOINTS_SAFE (b, b_tmp)
12052   {
12053     if (b->disposition == disp_del_at_next_stop)
12054       delete_breakpoint (b);
12055   }
12056 }
12057
12058 /* A comparison function for bp_location AP and BP being interfaced to
12059    qsort.  Sort elements primarily by their ADDRESS (no matter what
12060    does breakpoint_address_is_meaningful say for its OWNER),
12061    secondarily by ordering first bp_permanent OWNERed elements and
12062    terciarily just ensuring the array is sorted stable way despite
12063    qsort being an unstable algorithm.  */
12064
12065 static int
12066 bp_location_compare (const void *ap, const void *bp)
12067 {
12068   struct bp_location *a = *(void **) ap;
12069   struct bp_location *b = *(void **) bp;
12070   /* A and B come from existing breakpoints having non-NULL OWNER.  */
12071   int a_perm = a->owner->enable_state == bp_permanent;
12072   int b_perm = b->owner->enable_state == bp_permanent;
12073
12074   if (a->address != b->address)
12075     return (a->address > b->address) - (a->address < b->address);
12076
12077   /* Sort locations at the same address by their pspace number, keeping
12078      locations of the same inferior (in a multi-inferior environment)
12079      grouped.  */
12080
12081   if (a->pspace->num != b->pspace->num)
12082     return ((a->pspace->num > b->pspace->num)
12083             - (a->pspace->num < b->pspace->num));
12084
12085   /* Sort permanent breakpoints first.  */
12086   if (a_perm != b_perm)
12087     return (a_perm < b_perm) - (a_perm > b_perm);
12088
12089   /* Make the internal GDB representation stable across GDB runs
12090      where A and B memory inside GDB can differ.  Breakpoint locations of
12091      the same type at the same address can be sorted in arbitrary order.  */
12092
12093   if (a->owner->number != b->owner->number)
12094     return ((a->owner->number > b->owner->number)
12095             - (a->owner->number < b->owner->number));
12096
12097   return (a > b) - (a < b);
12098 }
12099
12100 /* Set bp_location_placed_address_before_address_max and
12101    bp_location_shadow_len_after_address_max according to the current
12102    content of the bp_location array.  */
12103
12104 static void
12105 bp_location_target_extensions_update (void)
12106 {
12107   struct bp_location *bl, **blp_tmp;
12108
12109   bp_location_placed_address_before_address_max = 0;
12110   bp_location_shadow_len_after_address_max = 0;
12111
12112   ALL_BP_LOCATIONS (bl, blp_tmp)
12113     {
12114       CORE_ADDR start, end, addr;
12115
12116       if (!bp_location_has_shadow (bl))
12117         continue;
12118
12119       start = bl->target_info.placed_address;
12120       end = start + bl->target_info.shadow_len;
12121
12122       gdb_assert (bl->address >= start);
12123       addr = bl->address - start;
12124       if (addr > bp_location_placed_address_before_address_max)
12125         bp_location_placed_address_before_address_max = addr;
12126
12127       /* Zero SHADOW_LEN would not pass bp_location_has_shadow.  */
12128
12129       gdb_assert (bl->address < end);
12130       addr = end - bl->address;
12131       if (addr > bp_location_shadow_len_after_address_max)
12132         bp_location_shadow_len_after_address_max = addr;
12133     }
12134 }
12135
12136 /* Download tracepoint locations if they haven't been.  */
12137
12138 static void
12139 download_tracepoint_locations (void)
12140 {
12141   struct breakpoint *b;
12142   struct cleanup *old_chain;
12143
12144   if (!target_can_download_tracepoint ())
12145     return;
12146
12147   old_chain = save_current_space_and_thread ();
12148
12149   ALL_TRACEPOINTS (b)
12150     {
12151       struct bp_location *bl;
12152       struct tracepoint *t;
12153       int bp_location_downloaded = 0;
12154
12155       if ((b->type == bp_fast_tracepoint
12156            ? !may_insert_fast_tracepoints
12157            : !may_insert_tracepoints))
12158         continue;
12159
12160       for (bl = b->loc; bl; bl = bl->next)
12161         {
12162           /* In tracepoint, locations are _never_ duplicated, so
12163              should_be_inserted is equivalent to
12164              unduplicated_should_be_inserted.  */
12165           if (!should_be_inserted (bl) || bl->inserted)
12166             continue;
12167
12168           switch_to_program_space_and_thread (bl->pspace);
12169
12170           target_download_tracepoint (bl);
12171
12172           bl->inserted = 1;
12173           bp_location_downloaded = 1;
12174         }
12175       t = (struct tracepoint *) b;
12176       t->number_on_target = b->number;
12177       if (bp_location_downloaded)
12178         observer_notify_breakpoint_modified (b);
12179     }
12180
12181   do_cleanups (old_chain);
12182 }
12183
12184 /* Swap the insertion/duplication state between two locations.  */
12185
12186 static void
12187 swap_insertion (struct bp_location *left, struct bp_location *right)
12188 {
12189   const int left_inserted = left->inserted;
12190   const int left_duplicate = left->duplicate;
12191   const int left_needs_update = left->needs_update;
12192   const struct bp_target_info left_target_info = left->target_info;
12193
12194   /* Locations of tracepoints can never be duplicated.  */
12195   if (is_tracepoint (left->owner))
12196     gdb_assert (!left->duplicate);
12197   if (is_tracepoint (right->owner))
12198     gdb_assert (!right->duplicate);
12199
12200   left->inserted = right->inserted;
12201   left->duplicate = right->duplicate;
12202   left->needs_update = right->needs_update;
12203   left->target_info = right->target_info;
12204   right->inserted = left_inserted;
12205   right->duplicate = left_duplicate;
12206   right->needs_update = left_needs_update;
12207   right->target_info = left_target_info;
12208 }
12209
12210 /* Force the re-insertion of the locations at ADDRESS.  This is called
12211    once a new/deleted/modified duplicate location is found and we are evaluating
12212    conditions on the target's side.  Such conditions need to be updated on
12213    the target.  */
12214
12215 static void
12216 force_breakpoint_reinsertion (struct bp_location *bl)
12217 {
12218   struct bp_location **locp = NULL, **loc2p;
12219   struct bp_location *loc;
12220   CORE_ADDR address = 0;
12221   int pspace_num;
12222
12223   address = bl->address;
12224   pspace_num = bl->pspace->num;
12225
12226   /* This is only meaningful if the target is
12227      evaluating conditions and if the user has
12228      opted for condition evaluation on the target's
12229      side.  */
12230   if (gdb_evaluates_breakpoint_condition_p ()
12231       || !target_supports_evaluation_of_breakpoint_conditions ())
12232     return;
12233
12234   /* Flag all breakpoint locations with this address and
12235      the same program space as the location
12236      as "its condition has changed".  We need to
12237      update the conditions on the target's side.  */
12238   ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, address)
12239     {
12240       loc = *loc2p;
12241
12242       if (!is_breakpoint (loc->owner)
12243           || pspace_num != loc->pspace->num)
12244         continue;
12245
12246       /* Flag the location appropriately.  We use a different state to
12247          let everyone know that we already updated the set of locations
12248          with addr bl->address and program space bl->pspace.  This is so
12249          we don't have to keep calling these functions just to mark locations
12250          that have already been marked.  */
12251       loc->condition_changed = condition_updated;
12252
12253       /* Free the agent expression bytecode as well.  We will compute
12254          it later on.  */
12255       if (loc->cond_bytecode)
12256         {
12257           free_agent_expr (loc->cond_bytecode);
12258           loc->cond_bytecode = NULL;
12259         }
12260     }
12261 }
12262
12263 /* If SHOULD_INSERT is false, do not insert any breakpoint locations
12264    into the inferior, only remove already-inserted locations that no
12265    longer should be inserted.  Functions that delete a breakpoint or
12266    breakpoints should pass false, so that deleting a breakpoint
12267    doesn't have the side effect of inserting the locations of other
12268    breakpoints that are marked not-inserted, but should_be_inserted
12269    returns true on them.
12270
12271    This behaviour is useful is situations close to tear-down -- e.g.,
12272    after an exec, while the target still has execution, but breakpoint
12273    shadows of the previous executable image should *NOT* be restored
12274    to the new image; or before detaching, where the target still has
12275    execution and wants to delete breakpoints from GDB's lists, and all
12276    breakpoints had already been removed from the inferior.  */
12277
12278 static void
12279 update_global_location_list (int should_insert)
12280 {
12281   struct breakpoint *b;
12282   struct bp_location **locp, *loc;
12283   struct cleanup *cleanups;
12284   /* Last breakpoint location address that was marked for update.  */
12285   CORE_ADDR last_addr = 0;
12286   /* Last breakpoint location program space that was marked for update.  */
12287   int last_pspace_num = -1;
12288
12289   /* Used in the duplicates detection below.  When iterating over all
12290      bp_locations, points to the first bp_location of a given address.
12291      Breakpoints and watchpoints of different types are never
12292      duplicates of each other.  Keep one pointer for each type of
12293      breakpoint/watchpoint, so we only need to loop over all locations
12294      once.  */
12295   struct bp_location *bp_loc_first;  /* breakpoint */
12296   struct bp_location *wp_loc_first;  /* hardware watchpoint */
12297   struct bp_location *awp_loc_first; /* access watchpoint */
12298   struct bp_location *rwp_loc_first; /* read watchpoint */
12299
12300   /* Saved former bp_location array which we compare against the newly
12301      built bp_location from the current state of ALL_BREAKPOINTS.  */
12302   struct bp_location **old_location, **old_locp;
12303   unsigned old_location_count;
12304
12305   old_location = bp_location;
12306   old_location_count = bp_location_count;
12307   bp_location = NULL;
12308   bp_location_count = 0;
12309   cleanups = make_cleanup (xfree, old_location);
12310
12311   ALL_BREAKPOINTS (b)
12312     for (loc = b->loc; loc; loc = loc->next)
12313       bp_location_count++;
12314
12315   bp_location = xmalloc (sizeof (*bp_location) * bp_location_count);
12316   locp = bp_location;
12317   ALL_BREAKPOINTS (b)
12318     for (loc = b->loc; loc; loc = loc->next)
12319       *locp++ = loc;
12320   qsort (bp_location, bp_location_count, sizeof (*bp_location),
12321          bp_location_compare);
12322
12323   bp_location_target_extensions_update ();
12324
12325   /* Identify bp_location instances that are no longer present in the
12326      new list, and therefore should be freed.  Note that it's not
12327      necessary that those locations should be removed from inferior --
12328      if there's another location at the same address (previously
12329      marked as duplicate), we don't need to remove/insert the
12330      location.
12331      
12332      LOCP is kept in sync with OLD_LOCP, each pointing to the current
12333      and former bp_location array state respectively.  */
12334
12335   locp = bp_location;
12336   for (old_locp = old_location; old_locp < old_location + old_location_count;
12337        old_locp++)
12338     {
12339       struct bp_location *old_loc = *old_locp;
12340       struct bp_location **loc2p;
12341
12342       /* Tells if 'old_loc' is found among the new locations.  If
12343          not, we have to free it.  */
12344       int found_object = 0;
12345       /* Tells if the location should remain inserted in the target.  */
12346       int keep_in_target = 0;
12347       int removed = 0;
12348
12349       /* Skip LOCP entries which will definitely never be needed.
12350          Stop either at or being the one matching OLD_LOC.  */
12351       while (locp < bp_location + bp_location_count
12352              && (*locp)->address < old_loc->address)
12353         locp++;
12354
12355       for (loc2p = locp;
12356            (loc2p < bp_location + bp_location_count
12357             && (*loc2p)->address == old_loc->address);
12358            loc2p++)
12359         {
12360           /* Check if this is a new/duplicated location or a duplicated
12361              location that had its condition modified.  If so, we want to send
12362              its condition to the target if evaluation of conditions is taking
12363              place there.  */
12364           if ((*loc2p)->condition_changed == condition_modified
12365               && (last_addr != old_loc->address
12366                   || last_pspace_num != old_loc->pspace->num))
12367             {
12368               force_breakpoint_reinsertion (*loc2p);
12369               last_pspace_num = old_loc->pspace->num;
12370             }
12371
12372           if (*loc2p == old_loc)
12373             found_object = 1;
12374         }
12375
12376       /* We have already handled this address, update it so that we don't
12377          have to go through updates again.  */
12378       last_addr = old_loc->address;
12379
12380       /* Target-side condition evaluation: Handle deleted locations.  */
12381       if (!found_object)
12382         force_breakpoint_reinsertion (old_loc);
12383
12384       /* If this location is no longer present, and inserted, look if
12385          there's maybe a new location at the same address.  If so,
12386          mark that one inserted, and don't remove this one.  This is
12387          needed so that we don't have a time window where a breakpoint
12388          at certain location is not inserted.  */
12389
12390       if (old_loc->inserted)
12391         {
12392           /* If the location is inserted now, we might have to remove
12393              it.  */
12394
12395           if (found_object && should_be_inserted (old_loc))
12396             {
12397               /* The location is still present in the location list,
12398                  and still should be inserted.  Don't do anything.  */
12399               keep_in_target = 1;
12400             }
12401           else
12402             {
12403               /* This location still exists, but it won't be kept in the
12404                  target since it may have been disabled.  We proceed to
12405                  remove its target-side condition.  */
12406
12407               /* The location is either no longer present, or got
12408                  disabled.  See if there's another location at the
12409                  same address, in which case we don't need to remove
12410                  this one from the target.  */
12411
12412               /* OLD_LOC comes from existing struct breakpoint.  */
12413               if (breakpoint_address_is_meaningful (old_loc->owner))
12414                 {
12415                   for (loc2p = locp;
12416                        (loc2p < bp_location + bp_location_count
12417                         && (*loc2p)->address == old_loc->address);
12418                        loc2p++)
12419                     {
12420                       struct bp_location *loc2 = *loc2p;
12421
12422                       if (breakpoint_locations_match (loc2, old_loc))
12423                         {
12424                           /* Read watchpoint locations are switched to
12425                              access watchpoints, if the former are not
12426                              supported, but the latter are.  */
12427                           if (is_hardware_watchpoint (old_loc->owner))
12428                             {
12429                               gdb_assert (is_hardware_watchpoint (loc2->owner));
12430                               loc2->watchpoint_type = old_loc->watchpoint_type;
12431                             }
12432
12433                           /* loc2 is a duplicated location. We need to check
12434                              if it should be inserted in case it will be
12435                              unduplicated.  */
12436                           if (loc2 != old_loc
12437                               && unduplicated_should_be_inserted (loc2))
12438                             {
12439                               swap_insertion (old_loc, loc2);
12440                               keep_in_target = 1;
12441                               break;
12442                             }
12443                         }
12444                     }
12445                 }
12446             }
12447
12448           if (!keep_in_target)
12449             {
12450               if (remove_breakpoint (old_loc, mark_uninserted))
12451                 {
12452                   /* This is just about all we can do.  We could keep
12453                      this location on the global list, and try to
12454                      remove it next time, but there's no particular
12455                      reason why we will succeed next time.
12456                      
12457                      Note that at this point, old_loc->owner is still
12458                      valid, as delete_breakpoint frees the breakpoint
12459                      only after calling us.  */
12460                   printf_filtered (_("warning: Error removing "
12461                                      "breakpoint %d\n"), 
12462                                    old_loc->owner->number);
12463                 }
12464               removed = 1;
12465             }
12466         }
12467
12468       if (!found_object)
12469         {
12470           if (removed && non_stop
12471               && breakpoint_address_is_meaningful (old_loc->owner)
12472               && !is_hardware_watchpoint (old_loc->owner))
12473             {
12474               /* This location was removed from the target.  In
12475                  non-stop mode, a race condition is possible where
12476                  we've removed a breakpoint, but stop events for that
12477                  breakpoint are already queued and will arrive later.
12478                  We apply an heuristic to be able to distinguish such
12479                  SIGTRAPs from other random SIGTRAPs: we keep this
12480                  breakpoint location for a bit, and will retire it
12481                  after we see some number of events.  The theory here
12482                  is that reporting of events should, "on the average",
12483                  be fair, so after a while we'll see events from all
12484                  threads that have anything of interest, and no longer
12485                  need to keep this breakpoint location around.  We
12486                  don't hold locations forever so to reduce chances of
12487                  mistaking a non-breakpoint SIGTRAP for a breakpoint
12488                  SIGTRAP.
12489
12490                  The heuristic failing can be disastrous on
12491                  decr_pc_after_break targets.
12492
12493                  On decr_pc_after_break targets, like e.g., x86-linux,
12494                  if we fail to recognize a late breakpoint SIGTRAP,
12495                  because events_till_retirement has reached 0 too
12496                  soon, we'll fail to do the PC adjustment, and report
12497                  a random SIGTRAP to the user.  When the user resumes
12498                  the inferior, it will most likely immediately crash
12499                  with SIGILL/SIGBUS/SIGSEGV, or worse, get silently
12500                  corrupted, because of being resumed e.g., in the
12501                  middle of a multi-byte instruction, or skipped a
12502                  one-byte instruction.  This was actually seen happen
12503                  on native x86-linux, and should be less rare on
12504                  targets that do not support new thread events, like
12505                  remote, due to the heuristic depending on
12506                  thread_count.
12507
12508                  Mistaking a random SIGTRAP for a breakpoint trap
12509                  causes similar symptoms (PC adjustment applied when
12510                  it shouldn't), but then again, playing with SIGTRAPs
12511                  behind the debugger's back is asking for trouble.
12512
12513                  Since hardware watchpoint traps are always
12514                  distinguishable from other traps, so we don't need to
12515                  apply keep hardware watchpoint moribund locations
12516                  around.  We simply always ignore hardware watchpoint
12517                  traps we can no longer explain.  */
12518
12519               old_loc->events_till_retirement = 3 * (thread_count () + 1);
12520               old_loc->owner = NULL;
12521
12522               VEC_safe_push (bp_location_p, moribund_locations, old_loc);
12523             }
12524           else
12525             {
12526               old_loc->owner = NULL;
12527               decref_bp_location (&old_loc);
12528             }
12529         }
12530     }
12531
12532   /* Rescan breakpoints at the same address and section, marking the
12533      first one as "first" and any others as "duplicates".  This is so
12534      that the bpt instruction is only inserted once.  If we have a
12535      permanent breakpoint at the same place as BPT, make that one the
12536      official one, and the rest as duplicates.  Permanent breakpoints
12537      are sorted first for the same address.
12538
12539      Do the same for hardware watchpoints, but also considering the
12540      watchpoint's type (regular/access/read) and length.  */
12541
12542   bp_loc_first = NULL;
12543   wp_loc_first = NULL;
12544   awp_loc_first = NULL;
12545   rwp_loc_first = NULL;
12546   ALL_BP_LOCATIONS (loc, locp)
12547     {
12548       /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always
12549          non-NULL.  */
12550       struct bp_location **loc_first_p;
12551       b = loc->owner;
12552
12553       if (!unduplicated_should_be_inserted (loc)
12554           || !breakpoint_address_is_meaningful (b)
12555           /* Don't detect duplicate for tracepoint locations because they are
12556            never duplicated.  See the comments in field `duplicate' of
12557            `struct bp_location'.  */
12558           || is_tracepoint (b))
12559         {
12560           /* Clear the condition modification flag.  */
12561           loc->condition_changed = condition_unchanged;
12562           continue;
12563         }
12564
12565       /* Permanent breakpoint should always be inserted.  */
12566       if (b->enable_state == bp_permanent && ! loc->inserted)
12567         internal_error (__FILE__, __LINE__,
12568                         _("allegedly permanent breakpoint is not "
12569                         "actually inserted"));
12570
12571       if (b->type == bp_hardware_watchpoint)
12572         loc_first_p = &wp_loc_first;
12573       else if (b->type == bp_read_watchpoint)
12574         loc_first_p = &rwp_loc_first;
12575       else if (b->type == bp_access_watchpoint)
12576         loc_first_p = &awp_loc_first;
12577       else
12578         loc_first_p = &bp_loc_first;
12579
12580       if (*loc_first_p == NULL
12581           || (overlay_debugging && loc->section != (*loc_first_p)->section)
12582           || !breakpoint_locations_match (loc, *loc_first_p))
12583         {
12584           *loc_first_p = loc;
12585           loc->duplicate = 0;
12586
12587           if (is_breakpoint (loc->owner) && loc->condition_changed)
12588             {
12589               loc->needs_update = 1;
12590               /* Clear the condition modification flag.  */
12591               loc->condition_changed = condition_unchanged;
12592             }
12593           continue;
12594         }
12595
12596
12597       /* This and the above ensure the invariant that the first location
12598          is not duplicated, and is the inserted one.
12599          All following are marked as duplicated, and are not inserted.  */
12600       if (loc->inserted)
12601         swap_insertion (loc, *loc_first_p);
12602       loc->duplicate = 1;
12603
12604       /* Clear the condition modification flag.  */
12605       loc->condition_changed = condition_unchanged;
12606
12607       if ((*loc_first_p)->owner->enable_state == bp_permanent && loc->inserted
12608           && b->enable_state != bp_permanent)
12609         internal_error (__FILE__, __LINE__,
12610                         _("another breakpoint was inserted on top of "
12611                         "a permanent breakpoint"));
12612     }
12613
12614   if (breakpoints_always_inserted_mode ()
12615       && (have_live_inferiors ()
12616           || (gdbarch_has_global_breakpoints (target_gdbarch ()))))
12617     {
12618       if (should_insert)
12619         insert_breakpoint_locations ();
12620       else
12621         {
12622           /* Though should_insert is false, we may need to update conditions
12623              on the target's side if it is evaluating such conditions.  We
12624              only update conditions for locations that are marked
12625              "needs_update".  */
12626           update_inserted_breakpoint_locations ();
12627         }
12628     }
12629
12630   if (should_insert)
12631     download_tracepoint_locations ();
12632
12633   do_cleanups (cleanups);
12634 }
12635
12636 void
12637 breakpoint_retire_moribund (void)
12638 {
12639   struct bp_location *loc;
12640   int ix;
12641
12642   for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, loc); ++ix)
12643     if (--(loc->events_till_retirement) == 0)
12644       {
12645         decref_bp_location (&loc);
12646         VEC_unordered_remove (bp_location_p, moribund_locations, ix);
12647         --ix;
12648       }
12649 }
12650
12651 static void
12652 update_global_location_list_nothrow (int inserting)
12653 {
12654   volatile struct gdb_exception e;
12655
12656   TRY_CATCH (e, RETURN_MASK_ERROR)
12657     update_global_location_list (inserting);
12658 }
12659
12660 /* Clear BKP from a BPS.  */
12661
12662 static void
12663 bpstat_remove_bp_location (bpstat bps, struct breakpoint *bpt)
12664 {
12665   bpstat bs;
12666
12667   for (bs = bps; bs; bs = bs->next)
12668     if (bs->breakpoint_at == bpt)
12669       {
12670         bs->breakpoint_at = NULL;
12671         bs->old_val = NULL;
12672         /* bs->commands will be freed later.  */
12673       }
12674 }
12675
12676 /* Callback for iterate_over_threads.  */
12677 static int
12678 bpstat_remove_breakpoint_callback (struct thread_info *th, void *data)
12679 {
12680   struct breakpoint *bpt = data;
12681
12682   bpstat_remove_bp_location (th->control.stop_bpstat, bpt);
12683   return 0;
12684 }
12685
12686 /* Helper for breakpoint and tracepoint breakpoint_ops->mention
12687    callbacks.  */
12688
12689 static void
12690 say_where (struct breakpoint *b)
12691 {
12692   struct value_print_options opts;
12693
12694   get_user_print_options (&opts);
12695
12696   /* i18n: cagney/2005-02-11: Below needs to be merged into a
12697      single string.  */
12698   if (b->loc == NULL)
12699     {
12700       printf_filtered (_(" (%s) pending."), b->addr_string);
12701     }
12702   else
12703     {
12704       if (opts.addressprint || b->loc->symtab == NULL)
12705         {
12706           printf_filtered (" at ");
12707           fputs_filtered (paddress (b->loc->gdbarch, b->loc->address),
12708                           gdb_stdout);
12709         }
12710       if (b->loc->symtab != NULL)
12711         {
12712           /* If there is a single location, we can print the location
12713              more nicely.  */
12714           if (b->loc->next == NULL)
12715             printf_filtered (": file %s, line %d.",
12716                              symtab_to_filename_for_display (b->loc->symtab),
12717                              b->loc->line_number);
12718           else
12719             /* This is not ideal, but each location may have a
12720                different file name, and this at least reflects the
12721                real situation somewhat.  */
12722             printf_filtered (": %s.", b->addr_string);
12723         }
12724
12725       if (b->loc->next)
12726         {
12727           struct bp_location *loc = b->loc;
12728           int n = 0;
12729           for (; loc; loc = loc->next)
12730             ++n;
12731           printf_filtered (" (%d locations)", n);
12732         }
12733     }
12734 }
12735
12736 /* Default bp_location_ops methods.  */
12737
12738 static void
12739 bp_location_dtor (struct bp_location *self)
12740 {
12741   xfree (self->cond);
12742   if (self->cond_bytecode)
12743     free_agent_expr (self->cond_bytecode);
12744   xfree (self->function_name);
12745 }
12746
12747 static const struct bp_location_ops bp_location_ops =
12748 {
12749   bp_location_dtor
12750 };
12751
12752 /* Default breakpoint_ops methods all breakpoint_ops ultimately
12753    inherit from.  */
12754
12755 static void
12756 base_breakpoint_dtor (struct breakpoint *self)
12757 {
12758   decref_counted_command_line (&self->commands);
12759   xfree (self->cond_string);
12760   xfree (self->extra_string);
12761   xfree (self->addr_string);
12762   xfree (self->filter);
12763   xfree (self->addr_string_range_end);
12764 }
12765
12766 static struct bp_location *
12767 base_breakpoint_allocate_location (struct breakpoint *self)
12768 {
12769   struct bp_location *loc;
12770
12771   loc = XNEW (struct bp_location);
12772   init_bp_location (loc, &bp_location_ops, self);
12773   return loc;
12774 }
12775
12776 static void
12777 base_breakpoint_re_set (struct breakpoint *b)
12778 {
12779   /* Nothing to re-set. */
12780 }
12781
12782 #define internal_error_pure_virtual_called() \
12783   gdb_assert_not_reached ("pure virtual function called")
12784
12785 static int
12786 base_breakpoint_insert_location (struct bp_location *bl)
12787 {
12788   internal_error_pure_virtual_called ();
12789 }
12790
12791 static int
12792 base_breakpoint_remove_location (struct bp_location *bl)
12793 {
12794   internal_error_pure_virtual_called ();
12795 }
12796
12797 static int
12798 base_breakpoint_breakpoint_hit (const struct bp_location *bl,
12799                                 struct address_space *aspace,
12800                                 CORE_ADDR bp_addr,
12801                                 const struct target_waitstatus *ws)
12802 {
12803   internal_error_pure_virtual_called ();
12804 }
12805
12806 static void
12807 base_breakpoint_check_status (bpstat bs)
12808 {
12809   /* Always stop.   */
12810 }
12811
12812 /* A "works_in_software_mode" breakpoint_ops method that just internal
12813    errors.  */
12814
12815 static int
12816 base_breakpoint_works_in_software_mode (const struct breakpoint *b)
12817 {
12818   internal_error_pure_virtual_called ();
12819 }
12820
12821 /* A "resources_needed" breakpoint_ops method that just internal
12822    errors.  */
12823
12824 static int
12825 base_breakpoint_resources_needed (const struct bp_location *bl)
12826 {
12827   internal_error_pure_virtual_called ();
12828 }
12829
12830 static enum print_stop_action
12831 base_breakpoint_print_it (bpstat bs)
12832 {
12833   internal_error_pure_virtual_called ();
12834 }
12835
12836 static void
12837 base_breakpoint_print_one_detail (const struct breakpoint *self,
12838                                   struct ui_out *uiout)
12839 {
12840   /* nothing */
12841 }
12842
12843 static void
12844 base_breakpoint_print_mention (struct breakpoint *b)
12845 {
12846   internal_error_pure_virtual_called ();
12847 }
12848
12849 static void
12850 base_breakpoint_print_recreate (struct breakpoint *b, struct ui_file *fp)
12851 {
12852   internal_error_pure_virtual_called ();
12853 }
12854
12855 static void
12856 base_breakpoint_create_sals_from_address (char **arg,
12857                                           struct linespec_result *canonical,
12858                                           enum bptype type_wanted,
12859                                           char *addr_start,
12860                                           char **copy_arg)
12861 {
12862   internal_error_pure_virtual_called ();
12863 }
12864
12865 static void
12866 base_breakpoint_create_breakpoints_sal (struct gdbarch *gdbarch,
12867                                         struct linespec_result *c,
12868                                         char *cond_string,
12869                                         char *extra_string,
12870                                         enum bptype type_wanted,
12871                                         enum bpdisp disposition,
12872                                         int thread,
12873                                         int task, int ignore_count,
12874                                         const struct breakpoint_ops *o,
12875                                         int from_tty, int enabled,
12876                                         int internal, unsigned flags)
12877 {
12878   internal_error_pure_virtual_called ();
12879 }
12880
12881 static void
12882 base_breakpoint_decode_linespec (struct breakpoint *b, char **s,
12883                                  struct symtabs_and_lines *sals)
12884 {
12885   internal_error_pure_virtual_called ();
12886 }
12887
12888 /* The default 'explains_signal' method.  */
12889
12890 static enum bpstat_signal_value
12891 base_breakpoint_explains_signal (struct breakpoint *b, enum gdb_signal sig)
12892 {
12893   return BPSTAT_SIGNAL_HIDE;
12894 }
12895
12896 /* The default "after_condition_true" method.  */
12897
12898 static void
12899 base_breakpoint_after_condition_true (struct bpstats *bs)
12900 {
12901   /* Nothing to do.   */
12902 }
12903
12904 struct breakpoint_ops base_breakpoint_ops =
12905 {
12906   base_breakpoint_dtor,
12907   base_breakpoint_allocate_location,
12908   base_breakpoint_re_set,
12909   base_breakpoint_insert_location,
12910   base_breakpoint_remove_location,
12911   base_breakpoint_breakpoint_hit,
12912   base_breakpoint_check_status,
12913   base_breakpoint_resources_needed,
12914   base_breakpoint_works_in_software_mode,
12915   base_breakpoint_print_it,
12916   NULL,
12917   base_breakpoint_print_one_detail,
12918   base_breakpoint_print_mention,
12919   base_breakpoint_print_recreate,
12920   base_breakpoint_create_sals_from_address,
12921   base_breakpoint_create_breakpoints_sal,
12922   base_breakpoint_decode_linespec,
12923   base_breakpoint_explains_signal,
12924   base_breakpoint_after_condition_true,
12925 };
12926
12927 /* Default breakpoint_ops methods.  */
12928
12929 static void
12930 bkpt_re_set (struct breakpoint *b)
12931 {
12932   /* FIXME: is this still reachable?  */
12933   if (b->addr_string == NULL)
12934     {
12935       /* Anything without a string can't be re-set.  */
12936       delete_breakpoint (b);
12937       return;
12938     }
12939
12940   breakpoint_re_set_default (b);
12941 }
12942
12943 static int
12944 bkpt_insert_location (struct bp_location *bl)
12945 {
12946   if (bl->loc_type == bp_loc_hardware_breakpoint)
12947     return target_insert_hw_breakpoint (bl->gdbarch,
12948                                         &bl->target_info);
12949   else
12950     return target_insert_breakpoint (bl->gdbarch,
12951                                      &bl->target_info);
12952 }
12953
12954 static int
12955 bkpt_remove_location (struct bp_location *bl)
12956 {
12957   if (bl->loc_type == bp_loc_hardware_breakpoint)
12958     return target_remove_hw_breakpoint (bl->gdbarch, &bl->target_info);
12959   else
12960     return target_remove_breakpoint (bl->gdbarch, &bl->target_info);
12961 }
12962
12963 static int
12964 bkpt_breakpoint_hit (const struct bp_location *bl,
12965                      struct address_space *aspace, CORE_ADDR bp_addr,
12966                      const struct target_waitstatus *ws)
12967 {
12968   if (ws->kind != TARGET_WAITKIND_STOPPED
12969       || ws->value.sig != GDB_SIGNAL_TRAP)
12970     return 0;
12971
12972   if (!breakpoint_address_match (bl->pspace->aspace, bl->address,
12973                                  aspace, bp_addr))
12974     return 0;
12975
12976   if (overlay_debugging         /* unmapped overlay section */
12977       && section_is_overlay (bl->section)
12978       && !section_is_mapped (bl->section))
12979     return 0;
12980
12981   return 1;
12982 }
12983
12984 static int
12985 bkpt_resources_needed (const struct bp_location *bl)
12986 {
12987   gdb_assert (bl->owner->type == bp_hardware_breakpoint);
12988
12989   return 1;
12990 }
12991
12992 static enum print_stop_action
12993 bkpt_print_it (bpstat bs)
12994 {
12995   struct breakpoint *b;
12996   const struct bp_location *bl;
12997   int bp_temp;
12998   struct ui_out *uiout = current_uiout;
12999
13000   gdb_assert (bs->bp_location_at != NULL);
13001
13002   bl = bs->bp_location_at;
13003   b = bs->breakpoint_at;
13004
13005   bp_temp = b->disposition == disp_del;
13006   if (bl->address != bl->requested_address)
13007     breakpoint_adjustment_warning (bl->requested_address,
13008                                    bl->address,
13009                                    b->number, 1);
13010   annotate_breakpoint (b->number);
13011   if (bp_temp)
13012     ui_out_text (uiout, "\nTemporary breakpoint ");
13013   else
13014     ui_out_text (uiout, "\nBreakpoint ");
13015   if (ui_out_is_mi_like_p (uiout))
13016     {
13017       ui_out_field_string (uiout, "reason",
13018                            async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT));
13019       ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
13020     }
13021   ui_out_field_int (uiout, "bkptno", b->number);
13022   ui_out_text (uiout, ", ");
13023
13024   return PRINT_SRC_AND_LOC;
13025 }
13026
13027 static void
13028 bkpt_print_mention (struct breakpoint *b)
13029 {
13030   if (ui_out_is_mi_like_p (current_uiout))
13031     return;
13032
13033   switch (b->type)
13034     {
13035     case bp_breakpoint:
13036     case bp_gnu_ifunc_resolver:
13037       if (b->disposition == disp_del)
13038         printf_filtered (_("Temporary breakpoint"));
13039       else
13040         printf_filtered (_("Breakpoint"));
13041       printf_filtered (_(" %d"), b->number);
13042       if (b->type == bp_gnu_ifunc_resolver)
13043         printf_filtered (_(" at gnu-indirect-function resolver"));
13044       break;
13045     case bp_hardware_breakpoint:
13046       printf_filtered (_("Hardware assisted breakpoint %d"), b->number);
13047       break;
13048     case bp_dprintf:
13049       printf_filtered (_("Dprintf %d"), b->number);
13050       break;
13051     }
13052
13053   say_where (b);
13054 }
13055
13056 static void
13057 bkpt_print_recreate (struct breakpoint *tp, struct ui_file *fp)
13058 {
13059   if (tp->type == bp_breakpoint && tp->disposition == disp_del)
13060     fprintf_unfiltered (fp, "tbreak");
13061   else if (tp->type == bp_breakpoint)
13062     fprintf_unfiltered (fp, "break");
13063   else if (tp->type == bp_hardware_breakpoint
13064            && tp->disposition == disp_del)
13065     fprintf_unfiltered (fp, "thbreak");
13066   else if (tp->type == bp_hardware_breakpoint)
13067     fprintf_unfiltered (fp, "hbreak");
13068   else
13069     internal_error (__FILE__, __LINE__,
13070                     _("unhandled breakpoint type %d"), (int) tp->type);
13071
13072   fprintf_unfiltered (fp, " %s", tp->addr_string);
13073   print_recreate_thread (tp, fp);
13074 }
13075
13076 static void
13077 bkpt_create_sals_from_address (char **arg,
13078                                struct linespec_result *canonical,
13079                                enum bptype type_wanted,
13080                                char *addr_start, char **copy_arg)
13081 {
13082   create_sals_from_address_default (arg, canonical, type_wanted,
13083                                     addr_start, copy_arg);
13084 }
13085
13086 static void
13087 bkpt_create_breakpoints_sal (struct gdbarch *gdbarch,
13088                              struct linespec_result *canonical,
13089                              char *cond_string,
13090                              char *extra_string,
13091                              enum bptype type_wanted,
13092                              enum bpdisp disposition,
13093                              int thread,
13094                              int task, int ignore_count,
13095                              const struct breakpoint_ops *ops,
13096                              int from_tty, int enabled,
13097                              int internal, unsigned flags)
13098 {
13099   create_breakpoints_sal_default (gdbarch, canonical,
13100                                   cond_string, extra_string,
13101                                   type_wanted,
13102                                   disposition, thread, task,
13103                                   ignore_count, ops, from_tty,
13104                                   enabled, internal, flags);
13105 }
13106
13107 static void
13108 bkpt_decode_linespec (struct breakpoint *b, char **s,
13109                       struct symtabs_and_lines *sals)
13110 {
13111   decode_linespec_default (b, s, sals);
13112 }
13113
13114 /* Virtual table for internal breakpoints.  */
13115
13116 static void
13117 internal_bkpt_re_set (struct breakpoint *b)
13118 {
13119   switch (b->type)
13120     {
13121       /* Delete overlay event and longjmp master breakpoints; they
13122          will be reset later by breakpoint_re_set.  */
13123     case bp_overlay_event:
13124     case bp_longjmp_master:
13125     case bp_std_terminate_master:
13126     case bp_exception_master:
13127       delete_breakpoint (b);
13128       break;
13129
13130       /* This breakpoint is special, it's set up when the inferior
13131          starts and we really don't want to touch it.  */
13132     case bp_shlib_event:
13133
13134       /* Like bp_shlib_event, this breakpoint type is special.  Once
13135          it is set up, we do not want to touch it.  */
13136     case bp_thread_event:
13137       break;
13138     }
13139 }
13140
13141 static void
13142 internal_bkpt_check_status (bpstat bs)
13143 {
13144   if (bs->breakpoint_at->type == bp_shlib_event)
13145     {
13146       /* If requested, stop when the dynamic linker notifies GDB of
13147          events.  This allows the user to get control and place
13148          breakpoints in initializer routines for dynamically loaded
13149          objects (among other things).  */
13150       bs->stop = stop_on_solib_events;
13151       bs->print = stop_on_solib_events;
13152     }
13153   else
13154     bs->stop = 0;
13155 }
13156
13157 static enum print_stop_action
13158 internal_bkpt_print_it (bpstat bs)
13159 {
13160   struct breakpoint *b;
13161
13162   b = bs->breakpoint_at;
13163
13164   switch (b->type)
13165     {
13166     case bp_shlib_event:
13167       /* Did we stop because the user set the stop_on_solib_events
13168          variable?  (If so, we report this as a generic, "Stopped due
13169          to shlib event" message.) */
13170       print_solib_event (0);
13171       break;
13172
13173     case bp_thread_event:
13174       /* Not sure how we will get here.
13175          GDB should not stop for these breakpoints.  */
13176       printf_filtered (_("Thread Event Breakpoint: gdb should not stop!\n"));
13177       break;
13178
13179     case bp_overlay_event:
13180       /* By analogy with the thread event, GDB should not stop for these.  */
13181       printf_filtered (_("Overlay Event Breakpoint: gdb should not stop!\n"));
13182       break;
13183
13184     case bp_longjmp_master:
13185       /* These should never be enabled.  */
13186       printf_filtered (_("Longjmp Master Breakpoint: gdb should not stop!\n"));
13187       break;
13188
13189     case bp_std_terminate_master:
13190       /* These should never be enabled.  */
13191       printf_filtered (_("std::terminate Master Breakpoint: "
13192                          "gdb should not stop!\n"));
13193       break;
13194
13195     case bp_exception_master:
13196       /* These should never be enabled.  */
13197       printf_filtered (_("Exception Master Breakpoint: "
13198                          "gdb should not stop!\n"));
13199       break;
13200     }
13201
13202   return PRINT_NOTHING;
13203 }
13204
13205 static void
13206 internal_bkpt_print_mention (struct breakpoint *b)
13207 {
13208   /* Nothing to mention.  These breakpoints are internal.  */
13209 }
13210
13211 /* Virtual table for momentary breakpoints  */
13212
13213 static void
13214 momentary_bkpt_re_set (struct breakpoint *b)
13215 {
13216   /* Keep temporary breakpoints, which can be encountered when we step
13217      over a dlopen call and solib_add is resetting the breakpoints.
13218      Otherwise these should have been blown away via the cleanup chain
13219      or by breakpoint_init_inferior when we rerun the executable.  */
13220 }
13221
13222 static void
13223 momentary_bkpt_check_status (bpstat bs)
13224 {
13225   /* Nothing.  The point of these breakpoints is causing a stop.  */
13226 }
13227
13228 static enum print_stop_action
13229 momentary_bkpt_print_it (bpstat bs)
13230 {
13231   struct ui_out *uiout = current_uiout;
13232
13233   if (ui_out_is_mi_like_p (uiout))
13234     {
13235       struct breakpoint *b = bs->breakpoint_at;
13236
13237       switch (b->type)
13238         {
13239         case bp_finish:
13240           ui_out_field_string
13241             (uiout, "reason",
13242              async_reason_lookup (EXEC_ASYNC_FUNCTION_FINISHED));
13243           break;
13244
13245         case bp_until:
13246           ui_out_field_string
13247             (uiout, "reason",
13248              async_reason_lookup (EXEC_ASYNC_LOCATION_REACHED));
13249           break;
13250         }
13251     }
13252
13253   return PRINT_UNKNOWN;
13254 }
13255
13256 static void
13257 momentary_bkpt_print_mention (struct breakpoint *b)
13258 {
13259   /* Nothing to mention.  These breakpoints are internal.  */
13260 }
13261
13262 /* Ensure INITIATING_FRAME is cleared when no such breakpoint exists.
13263
13264    It gets cleared already on the removal of the first one of such placed
13265    breakpoints.  This is OK as they get all removed altogether.  */
13266
13267 static void
13268 longjmp_bkpt_dtor (struct breakpoint *self)
13269 {
13270   struct thread_info *tp = find_thread_id (self->thread);
13271
13272   if (tp)
13273     tp->initiating_frame = null_frame_id;
13274
13275   momentary_breakpoint_ops.dtor (self);
13276 }
13277
13278 /* Specific methods for probe breakpoints.  */
13279
13280 static int
13281 bkpt_probe_insert_location (struct bp_location *bl)
13282 {
13283   int v = bkpt_insert_location (bl);
13284
13285   if (v == 0)
13286     {
13287       /* The insertion was successful, now let's set the probe's semaphore
13288          if needed.  */
13289       bl->probe->pops->set_semaphore (bl->probe, bl->gdbarch);
13290     }
13291
13292   return v;
13293 }
13294
13295 static int
13296 bkpt_probe_remove_location (struct bp_location *bl)
13297 {
13298   /* Let's clear the semaphore before removing the location.  */
13299   bl->probe->pops->clear_semaphore (bl->probe, bl->gdbarch);
13300
13301   return bkpt_remove_location (bl);
13302 }
13303
13304 static void
13305 bkpt_probe_create_sals_from_address (char **arg,
13306                                      struct linespec_result *canonical,
13307                                      enum bptype type_wanted,
13308                                      char *addr_start, char **copy_arg)
13309 {
13310   struct linespec_sals lsal;
13311
13312   lsal.sals = parse_probes (arg, canonical);
13313
13314   *copy_arg = xstrdup (canonical->addr_string);
13315   lsal.canonical = xstrdup (*copy_arg);
13316
13317   VEC_safe_push (linespec_sals, canonical->sals, &lsal);
13318 }
13319
13320 static void
13321 bkpt_probe_decode_linespec (struct breakpoint *b, char **s,
13322                             struct symtabs_and_lines *sals)
13323 {
13324   *sals = parse_probes (s, NULL);
13325   if (!sals->sals)
13326     error (_("probe not found"));
13327 }
13328
13329 /* The breakpoint_ops structure to be used in tracepoints.  */
13330
13331 static void
13332 tracepoint_re_set (struct breakpoint *b)
13333 {
13334   breakpoint_re_set_default (b);
13335 }
13336
13337 static int
13338 tracepoint_breakpoint_hit (const struct bp_location *bl,
13339                            struct address_space *aspace, CORE_ADDR bp_addr,
13340                            const struct target_waitstatus *ws)
13341 {
13342   /* By definition, the inferior does not report stops at
13343      tracepoints.  */
13344   return 0;
13345 }
13346
13347 static void
13348 tracepoint_print_one_detail (const struct breakpoint *self,
13349                              struct ui_out *uiout)
13350 {
13351   struct tracepoint *tp = (struct tracepoint *) self;
13352   if (tp->static_trace_marker_id)
13353     {
13354       gdb_assert (self->type == bp_static_tracepoint);
13355
13356       ui_out_text (uiout, "\tmarker id is ");
13357       ui_out_field_string (uiout, "static-tracepoint-marker-string-id",
13358                            tp->static_trace_marker_id);
13359       ui_out_text (uiout, "\n");
13360     }
13361 }
13362
13363 static void
13364 tracepoint_print_mention (struct breakpoint *b)
13365 {
13366   if (ui_out_is_mi_like_p (current_uiout))
13367     return;
13368
13369   switch (b->type)
13370     {
13371     case bp_tracepoint:
13372       printf_filtered (_("Tracepoint"));
13373       printf_filtered (_(" %d"), b->number);
13374       break;
13375     case bp_fast_tracepoint:
13376       printf_filtered (_("Fast tracepoint"));
13377       printf_filtered (_(" %d"), b->number);
13378       break;
13379     case bp_static_tracepoint:
13380       printf_filtered (_("Static tracepoint"));
13381       printf_filtered (_(" %d"), b->number);
13382       break;
13383     default:
13384       internal_error (__FILE__, __LINE__,
13385                       _("unhandled tracepoint type %d"), (int) b->type);
13386     }
13387
13388   say_where (b);
13389 }
13390
13391 static void
13392 tracepoint_print_recreate (struct breakpoint *self, struct ui_file *fp)
13393 {
13394   struct tracepoint *tp = (struct tracepoint *) self;
13395
13396   if (self->type == bp_fast_tracepoint)
13397     fprintf_unfiltered (fp, "ftrace");
13398   if (self->type == bp_static_tracepoint)
13399     fprintf_unfiltered (fp, "strace");
13400   else if (self->type == bp_tracepoint)
13401     fprintf_unfiltered (fp, "trace");
13402   else
13403     internal_error (__FILE__, __LINE__,
13404                     _("unhandled tracepoint type %d"), (int) self->type);
13405
13406   fprintf_unfiltered (fp, " %s", self->addr_string);
13407   print_recreate_thread (self, fp);
13408
13409   if (tp->pass_count)
13410     fprintf_unfiltered (fp, "  passcount %d\n", tp->pass_count);
13411 }
13412
13413 static void
13414 tracepoint_create_sals_from_address (char **arg,
13415                                      struct linespec_result *canonical,
13416                                      enum bptype type_wanted,
13417                                      char *addr_start, char **copy_arg)
13418 {
13419   create_sals_from_address_default (arg, canonical, type_wanted,
13420                                     addr_start, copy_arg);
13421 }
13422
13423 static void
13424 tracepoint_create_breakpoints_sal (struct gdbarch *gdbarch,
13425                                    struct linespec_result *canonical,
13426                                    char *cond_string,
13427                                    char *extra_string,
13428                                    enum bptype type_wanted,
13429                                    enum bpdisp disposition,
13430                                    int thread,
13431                                    int task, int ignore_count,
13432                                    const struct breakpoint_ops *ops,
13433                                    int from_tty, int enabled,
13434                                    int internal, unsigned flags)
13435 {
13436   create_breakpoints_sal_default (gdbarch, canonical,
13437                                   cond_string, extra_string,
13438                                   type_wanted,
13439                                   disposition, thread, task,
13440                                   ignore_count, ops, from_tty,
13441                                   enabled, internal, flags);
13442 }
13443
13444 static void
13445 tracepoint_decode_linespec (struct breakpoint *b, char **s,
13446                             struct symtabs_and_lines *sals)
13447 {
13448   decode_linespec_default (b, s, sals);
13449 }
13450
13451 struct breakpoint_ops tracepoint_breakpoint_ops;
13452
13453 /* The breakpoint_ops structure to be use on tracepoints placed in a
13454    static probe.  */
13455
13456 static void
13457 tracepoint_probe_create_sals_from_address (char **arg,
13458                                            struct linespec_result *canonical,
13459                                            enum bptype type_wanted,
13460                                            char *addr_start, char **copy_arg)
13461 {
13462   /* We use the same method for breakpoint on probes.  */
13463   bkpt_probe_create_sals_from_address (arg, canonical, type_wanted,
13464                                        addr_start, copy_arg);
13465 }
13466
13467 static void
13468 tracepoint_probe_decode_linespec (struct breakpoint *b, char **s,
13469                                   struct symtabs_and_lines *sals)
13470 {
13471   /* We use the same method for breakpoint on probes.  */
13472   bkpt_probe_decode_linespec (b, s, sals);
13473 }
13474
13475 static struct breakpoint_ops tracepoint_probe_breakpoint_ops;
13476
13477 /* Dprintf breakpoint_ops methods.  */
13478
13479 static void
13480 dprintf_re_set (struct breakpoint *b)
13481 {
13482   breakpoint_re_set_default (b);
13483
13484   /* This breakpoint could have been pending, and be resolved now, and
13485      if so, we should now have the extra string.  If we don't, the
13486      dprintf was malformed when created, but we couldn't tell because
13487      we can't extract the extra string until the location is
13488      resolved.  */
13489   if (b->loc != NULL && b->extra_string == NULL)
13490     error (_("Format string required"));
13491
13492   /* 1 - connect to target 1, that can run breakpoint commands.
13493      2 - create a dprintf, which resolves fine.
13494      3 - disconnect from target 1
13495      4 - connect to target 2, that can NOT run breakpoint commands.
13496
13497      After steps #3/#4, you'll want the dprintf command list to
13498      be updated, because target 1 and 2 may well return different
13499      answers for target_can_run_breakpoint_commands().
13500      Given absence of finer grained resetting, we get to do
13501      it all the time.  */
13502   if (b->extra_string != NULL)
13503     update_dprintf_command_list (b);
13504 }
13505
13506 /* Implement the "print_recreate" breakpoint_ops method for dprintf.  */
13507
13508 static void
13509 dprintf_print_recreate (struct breakpoint *tp, struct ui_file *fp)
13510 {
13511   fprintf_unfiltered (fp, "dprintf %s%s", tp->addr_string,
13512                       tp->extra_string);
13513   print_recreate_thread (tp, fp);
13514 }
13515
13516 /* Implement the "after_condition_true" breakpoint_ops method for
13517    dprintf.
13518
13519    dprintf's are implemented with regular commands in their command
13520    list, but we run the commands here instead of before presenting the
13521    stop to the user, as dprintf's don't actually cause a stop.  This
13522    also makes it so that the commands of multiple dprintfs at the same
13523    address are all handled.  */
13524
13525 static void
13526 dprintf_after_condition_true (struct bpstats *bs)
13527 {
13528   struct cleanup *old_chain;
13529   struct bpstats tmp_bs = { NULL };
13530   struct bpstats *tmp_bs_p = &tmp_bs;
13531
13532   /* dprintf's never cause a stop.  This wasn't set in the
13533      check_status hook instead because that would make the dprintf's
13534      condition not be evaluated.  */
13535   bs->stop = 0;
13536
13537   /* Run the command list here.  Take ownership of it instead of
13538      copying.  We never want these commands to run later in
13539      bpstat_do_actions, if a breakpoint that causes a stop happens to
13540      be set at same address as this dprintf, or even if running the
13541      commands here throws.  */
13542   tmp_bs.commands = bs->commands;
13543   bs->commands = NULL;
13544   old_chain = make_cleanup_decref_counted_command_line (&tmp_bs.commands);
13545
13546   bpstat_do_actions_1 (&tmp_bs_p);
13547
13548   /* 'tmp_bs.commands' will usually be NULL by now, but
13549      bpstat_do_actions_1 may return early without processing the whole
13550      list.  */
13551   do_cleanups (old_chain);
13552 }
13553
13554 /* The breakpoint_ops structure to be used on static tracepoints with
13555    markers (`-m').  */
13556
13557 static void
13558 strace_marker_create_sals_from_address (char **arg,
13559                                         struct linespec_result *canonical,
13560                                         enum bptype type_wanted,
13561                                         char *addr_start, char **copy_arg)
13562 {
13563   struct linespec_sals lsal;
13564
13565   lsal.sals = decode_static_tracepoint_spec (arg);
13566
13567   *copy_arg = savestring (addr_start, *arg - addr_start);
13568
13569   canonical->addr_string = xstrdup (*copy_arg);
13570   lsal.canonical = xstrdup (*copy_arg);
13571   VEC_safe_push (linespec_sals, canonical->sals, &lsal);
13572 }
13573
13574 static void
13575 strace_marker_create_breakpoints_sal (struct gdbarch *gdbarch,
13576                                       struct linespec_result *canonical,
13577                                       char *cond_string,
13578                                       char *extra_string,
13579                                       enum bptype type_wanted,
13580                                       enum bpdisp disposition,
13581                                       int thread,
13582                                       int task, int ignore_count,
13583                                       const struct breakpoint_ops *ops,
13584                                       int from_tty, int enabled,
13585                                       int internal, unsigned flags)
13586 {
13587   int i;
13588   struct linespec_sals *lsal = VEC_index (linespec_sals,
13589                                           canonical->sals, 0);
13590
13591   /* If the user is creating a static tracepoint by marker id
13592      (strace -m MARKER_ID), then store the sals index, so that
13593      breakpoint_re_set can try to match up which of the newly
13594      found markers corresponds to this one, and, don't try to
13595      expand multiple locations for each sal, given than SALS
13596      already should contain all sals for MARKER_ID.  */
13597
13598   for (i = 0; i < lsal->sals.nelts; ++i)
13599     {
13600       struct symtabs_and_lines expanded;
13601       struct tracepoint *tp;
13602       struct cleanup *old_chain;
13603       char *addr_string;
13604
13605       expanded.nelts = 1;
13606       expanded.sals = &lsal->sals.sals[i];
13607
13608       addr_string = xstrdup (canonical->addr_string);
13609       old_chain = make_cleanup (xfree, addr_string);
13610
13611       tp = XCNEW (struct tracepoint);
13612       init_breakpoint_sal (&tp->base, gdbarch, expanded,
13613                            addr_string, NULL,
13614                            cond_string, extra_string,
13615                            type_wanted, disposition,
13616                            thread, task, ignore_count, ops,
13617                            from_tty, enabled, internal, flags,
13618                            canonical->special_display);
13619       /* Given that its possible to have multiple markers with
13620          the same string id, if the user is creating a static
13621          tracepoint by marker id ("strace -m MARKER_ID"), then
13622          store the sals index, so that breakpoint_re_set can
13623          try to match up which of the newly found markers
13624          corresponds to this one  */
13625       tp->static_trace_marker_id_idx = i;
13626
13627       install_breakpoint (internal, &tp->base, 0);
13628
13629       discard_cleanups (old_chain);
13630     }
13631 }
13632
13633 static void
13634 strace_marker_decode_linespec (struct breakpoint *b, char **s,
13635                                struct symtabs_and_lines *sals)
13636 {
13637   struct tracepoint *tp = (struct tracepoint *) b;
13638
13639   *sals = decode_static_tracepoint_spec (s);
13640   if (sals->nelts > tp->static_trace_marker_id_idx)
13641     {
13642       sals->sals[0] = sals->sals[tp->static_trace_marker_id_idx];
13643       sals->nelts = 1;
13644     }
13645   else
13646     error (_("marker %s not found"), tp->static_trace_marker_id);
13647 }
13648
13649 static struct breakpoint_ops strace_marker_breakpoint_ops;
13650
13651 static int
13652 strace_marker_p (struct breakpoint *b)
13653 {
13654   return b->ops == &strace_marker_breakpoint_ops;
13655 }
13656
13657 /* Delete a breakpoint and clean up all traces of it in the data
13658    structures.  */
13659
13660 void
13661 delete_breakpoint (struct breakpoint *bpt)
13662 {
13663   struct breakpoint *b;
13664
13665   gdb_assert (bpt != NULL);
13666
13667   /* Has this bp already been deleted?  This can happen because
13668      multiple lists can hold pointers to bp's.  bpstat lists are
13669      especial culprits.
13670
13671      One example of this happening is a watchpoint's scope bp.  When
13672      the scope bp triggers, we notice that the watchpoint is out of
13673      scope, and delete it.  We also delete its scope bp.  But the
13674      scope bp is marked "auto-deleting", and is already on a bpstat.
13675      That bpstat is then checked for auto-deleting bp's, which are
13676      deleted.
13677
13678      A real solution to this problem might involve reference counts in
13679      bp's, and/or giving them pointers back to their referencing
13680      bpstat's, and teaching delete_breakpoint to only free a bp's
13681      storage when no more references were extent.  A cheaper bandaid
13682      was chosen.  */
13683   if (bpt->type == bp_none)
13684     return;
13685
13686   /* At least avoid this stale reference until the reference counting
13687      of breakpoints gets resolved.  */
13688   if (bpt->related_breakpoint != bpt)
13689     {
13690       struct breakpoint *related;
13691       struct watchpoint *w;
13692
13693       if (bpt->type == bp_watchpoint_scope)
13694         w = (struct watchpoint *) bpt->related_breakpoint;
13695       else if (bpt->related_breakpoint->type == bp_watchpoint_scope)
13696         w = (struct watchpoint *) bpt;
13697       else
13698         w = NULL;
13699       if (w != NULL)
13700         watchpoint_del_at_next_stop (w);
13701
13702       /* Unlink bpt from the bpt->related_breakpoint ring.  */
13703       for (related = bpt; related->related_breakpoint != bpt;
13704            related = related->related_breakpoint);
13705       related->related_breakpoint = bpt->related_breakpoint;
13706       bpt->related_breakpoint = bpt;
13707     }
13708
13709   /* watch_command_1 creates a watchpoint but only sets its number if
13710      update_watchpoint succeeds in creating its bp_locations.  If there's
13711      a problem in that process, we'll be asked to delete the half-created
13712      watchpoint.  In that case, don't announce the deletion.  */
13713   if (bpt->number)
13714     observer_notify_breakpoint_deleted (bpt);
13715
13716   if (breakpoint_chain == bpt)
13717     breakpoint_chain = bpt->next;
13718
13719   ALL_BREAKPOINTS (b)
13720     if (b->next == bpt)
13721     {
13722       b->next = bpt->next;
13723       break;
13724     }
13725
13726   /* Be sure no bpstat's are pointing at the breakpoint after it's
13727      been freed.  */
13728   /* FIXME, how can we find all bpstat's?  We just check stop_bpstat
13729      in all threads for now.  Note that we cannot just remove bpstats
13730      pointing at bpt from the stop_bpstat list entirely, as breakpoint
13731      commands are associated with the bpstat; if we remove it here,
13732      then the later call to bpstat_do_actions (&stop_bpstat); in
13733      event-top.c won't do anything, and temporary breakpoints with
13734      commands won't work.  */
13735
13736   iterate_over_threads (bpstat_remove_breakpoint_callback, bpt);
13737
13738   /* Now that breakpoint is removed from breakpoint list, update the
13739      global location list.  This will remove locations that used to
13740      belong to this breakpoint.  Do this before freeing the breakpoint
13741      itself, since remove_breakpoint looks at location's owner.  It
13742      might be better design to have location completely
13743      self-contained, but it's not the case now.  */
13744   update_global_location_list (0);
13745
13746   bpt->ops->dtor (bpt);
13747   /* On the chance that someone will soon try again to delete this
13748      same bp, we mark it as deleted before freeing its storage.  */
13749   bpt->type = bp_none;
13750   xfree (bpt);
13751 }
13752
13753 static void
13754 do_delete_breakpoint_cleanup (void *b)
13755 {
13756   delete_breakpoint (b);
13757 }
13758
13759 struct cleanup *
13760 make_cleanup_delete_breakpoint (struct breakpoint *b)
13761 {
13762   return make_cleanup (do_delete_breakpoint_cleanup, b);
13763 }
13764
13765 /* Iterator function to call a user-provided callback function once
13766    for each of B and its related breakpoints.  */
13767
13768 static void
13769 iterate_over_related_breakpoints (struct breakpoint *b,
13770                                   void (*function) (struct breakpoint *,
13771                                                     void *),
13772                                   void *data)
13773 {
13774   struct breakpoint *related;
13775
13776   related = b;
13777   do
13778     {
13779       struct breakpoint *next;
13780
13781       /* FUNCTION may delete RELATED.  */
13782       next = related->related_breakpoint;
13783
13784       if (next == related)
13785         {
13786           /* RELATED is the last ring entry.  */
13787           function (related, data);
13788
13789           /* FUNCTION may have deleted it, so we'd never reach back to
13790              B.  There's nothing left to do anyway, so just break
13791              out.  */
13792           break;
13793         }
13794       else
13795         function (related, data);
13796
13797       related = next;
13798     }
13799   while (related != b);
13800 }
13801
13802 static void
13803 do_delete_breakpoint (struct breakpoint *b, void *ignore)
13804 {
13805   delete_breakpoint (b);
13806 }
13807
13808 /* A callback for map_breakpoint_numbers that calls
13809    delete_breakpoint.  */
13810
13811 static void
13812 do_map_delete_breakpoint (struct breakpoint *b, void *ignore)
13813 {
13814   iterate_over_related_breakpoints (b, do_delete_breakpoint, NULL);
13815 }
13816
13817 void
13818 delete_command (char *arg, int from_tty)
13819 {
13820   struct breakpoint *b, *b_tmp;
13821
13822   dont_repeat ();
13823
13824   if (arg == 0)
13825     {
13826       int breaks_to_delete = 0;
13827
13828       /* Delete all breakpoints if no argument.  Do not delete
13829          internal breakpoints, these have to be deleted with an
13830          explicit breakpoint number argument.  */
13831       ALL_BREAKPOINTS (b)
13832         if (user_breakpoint_p (b))
13833           {
13834             breaks_to_delete = 1;
13835             break;
13836           }
13837
13838       /* Ask user only if there are some breakpoints to delete.  */
13839       if (!from_tty
13840           || (breaks_to_delete && query (_("Delete all breakpoints? "))))
13841         {
13842           ALL_BREAKPOINTS_SAFE (b, b_tmp)
13843             if (user_breakpoint_p (b))
13844               delete_breakpoint (b);
13845         }
13846     }
13847   else
13848     map_breakpoint_numbers (arg, do_map_delete_breakpoint, NULL);
13849 }
13850
13851 static int
13852 all_locations_are_pending (struct bp_location *loc)
13853 {
13854   for (; loc; loc = loc->next)
13855     if (!loc->shlib_disabled
13856         && !loc->pspace->executing_startup)
13857       return 0;
13858   return 1;
13859 }
13860
13861 /* Subroutine of update_breakpoint_locations to simplify it.
13862    Return non-zero if multiple fns in list LOC have the same name.
13863    Null names are ignored.  */
13864
13865 static int
13866 ambiguous_names_p (struct bp_location *loc)
13867 {
13868   struct bp_location *l;
13869   htab_t htab = htab_create_alloc (13, htab_hash_string,
13870                                    (int (*) (const void *, 
13871                                              const void *)) streq,
13872                                    NULL, xcalloc, xfree);
13873
13874   for (l = loc; l != NULL; l = l->next)
13875     {
13876       const char **slot;
13877       const char *name = l->function_name;
13878
13879       /* Allow for some names to be NULL, ignore them.  */
13880       if (name == NULL)
13881         continue;
13882
13883       slot = (const char **) htab_find_slot (htab, (const void *) name,
13884                                              INSERT);
13885       /* NOTE: We can assume slot != NULL here because xcalloc never
13886          returns NULL.  */
13887       if (*slot != NULL)
13888         {
13889           htab_delete (htab);
13890           return 1;
13891         }
13892       *slot = name;
13893     }
13894
13895   htab_delete (htab);
13896   return 0;
13897 }
13898
13899 /* When symbols change, it probably means the sources changed as well,
13900    and it might mean the static tracepoint markers are no longer at
13901    the same address or line numbers they used to be at last we
13902    checked.  Losing your static tracepoints whenever you rebuild is
13903    undesirable.  This function tries to resync/rematch gdb static
13904    tracepoints with the markers on the target, for static tracepoints
13905    that have not been set by marker id.  Static tracepoint that have
13906    been set by marker id are reset by marker id in breakpoint_re_set.
13907    The heuristic is:
13908
13909    1) For a tracepoint set at a specific address, look for a marker at
13910    the old PC.  If one is found there, assume to be the same marker.
13911    If the name / string id of the marker found is different from the
13912    previous known name, assume that means the user renamed the marker
13913    in the sources, and output a warning.
13914
13915    2) For a tracepoint set at a given line number, look for a marker
13916    at the new address of the old line number.  If one is found there,
13917    assume to be the same marker.  If the name / string id of the
13918    marker found is different from the previous known name, assume that
13919    means the user renamed the marker in the sources, and output a
13920    warning.
13921
13922    3) If a marker is no longer found at the same address or line, it
13923    may mean the marker no longer exists.  But it may also just mean
13924    the code changed a bit.  Maybe the user added a few lines of code
13925    that made the marker move up or down (in line number terms).  Ask
13926    the target for info about the marker with the string id as we knew
13927    it.  If found, update line number and address in the matching
13928    static tracepoint.  This will get confused if there's more than one
13929    marker with the same ID (possible in UST, although unadvised
13930    precisely because it confuses tools).  */
13931
13932 static struct symtab_and_line
13933 update_static_tracepoint (struct breakpoint *b, struct symtab_and_line sal)
13934 {
13935   struct tracepoint *tp = (struct tracepoint *) b;
13936   struct static_tracepoint_marker marker;
13937   CORE_ADDR pc;
13938
13939   pc = sal.pc;
13940   if (sal.line)
13941     find_line_pc (sal.symtab, sal.line, &pc);
13942
13943   if (target_static_tracepoint_marker_at (pc, &marker))
13944     {
13945       if (strcmp (tp->static_trace_marker_id, marker.str_id) != 0)
13946         warning (_("static tracepoint %d changed probed marker from %s to %s"),
13947                  b->number,
13948                  tp->static_trace_marker_id, marker.str_id);
13949
13950       xfree (tp->static_trace_marker_id);
13951       tp->static_trace_marker_id = xstrdup (marker.str_id);
13952       release_static_tracepoint_marker (&marker);
13953
13954       return sal;
13955     }
13956
13957   /* Old marker wasn't found on target at lineno.  Try looking it up
13958      by string ID.  */
13959   if (!sal.explicit_pc
13960       && sal.line != 0
13961       && sal.symtab != NULL
13962       && tp->static_trace_marker_id != NULL)
13963     {
13964       VEC(static_tracepoint_marker_p) *markers;
13965
13966       markers
13967         = target_static_tracepoint_markers_by_strid (tp->static_trace_marker_id);
13968
13969       if (!VEC_empty(static_tracepoint_marker_p, markers))
13970         {
13971           struct symtab_and_line sal2;
13972           struct symbol *sym;
13973           struct static_tracepoint_marker *tpmarker;
13974           struct ui_out *uiout = current_uiout;
13975
13976           tpmarker = VEC_index (static_tracepoint_marker_p, markers, 0);
13977
13978           xfree (tp->static_trace_marker_id);
13979           tp->static_trace_marker_id = xstrdup (tpmarker->str_id);
13980
13981           warning (_("marker for static tracepoint %d (%s) not "
13982                      "found at previous line number"),
13983                    b->number, tp->static_trace_marker_id);
13984
13985           init_sal (&sal2);
13986
13987           sal2.pc = tpmarker->address;
13988
13989           sal2 = find_pc_line (tpmarker->address, 0);
13990           sym = find_pc_sect_function (tpmarker->address, NULL);
13991           ui_out_text (uiout, "Now in ");
13992           if (sym)
13993             {
13994               ui_out_field_string (uiout, "func",
13995                                    SYMBOL_PRINT_NAME (sym));
13996               ui_out_text (uiout, " at ");
13997             }
13998           ui_out_field_string (uiout, "file",
13999                                symtab_to_filename_for_display (sal2.symtab));
14000           ui_out_text (uiout, ":");
14001
14002           if (ui_out_is_mi_like_p (uiout))
14003             {
14004               const char *fullname = symtab_to_fullname (sal2.symtab);
14005
14006               ui_out_field_string (uiout, "fullname", fullname);
14007             }
14008
14009           ui_out_field_int (uiout, "line", sal2.line);
14010           ui_out_text (uiout, "\n");
14011
14012           b->loc->line_number = sal2.line;
14013           b->loc->symtab = sym != NULL ? sal2.symtab : NULL;
14014
14015           xfree (b->addr_string);
14016           b->addr_string = xstrprintf ("%s:%d",
14017                                    symtab_to_filename_for_display (sal2.symtab),
14018                                        b->loc->line_number);
14019
14020           /* Might be nice to check if function changed, and warn if
14021              so.  */
14022
14023           release_static_tracepoint_marker (tpmarker);
14024         }
14025     }
14026   return sal;
14027 }
14028
14029 /* Returns 1 iff locations A and B are sufficiently same that
14030    we don't need to report breakpoint as changed.  */
14031
14032 static int
14033 locations_are_equal (struct bp_location *a, struct bp_location *b)
14034 {
14035   while (a && b)
14036     {
14037       if (a->address != b->address)
14038         return 0;
14039
14040       if (a->shlib_disabled != b->shlib_disabled)
14041         return 0;
14042
14043       if (a->enabled != b->enabled)
14044         return 0;
14045
14046       a = a->next;
14047       b = b->next;
14048     }
14049
14050   if ((a == NULL) != (b == NULL))
14051     return 0;
14052
14053   return 1;
14054 }
14055
14056 /* Create new breakpoint locations for B (a hardware or software breakpoint)
14057    based on SALS and SALS_END.  If SALS_END.NELTS is not zero, then B is
14058    a ranged breakpoint.  */
14059
14060 void
14061 update_breakpoint_locations (struct breakpoint *b,
14062                              struct symtabs_and_lines sals,
14063                              struct symtabs_and_lines sals_end)
14064 {
14065   int i;
14066   struct bp_location *existing_locations = b->loc;
14067
14068   if (sals_end.nelts != 0 && (sals.nelts != 1 || sals_end.nelts != 1))
14069     {
14070       /* Ranged breakpoints have only one start location and one end
14071          location.  */
14072       b->enable_state = bp_disabled;
14073       update_global_location_list (1);
14074       printf_unfiltered (_("Could not reset ranged breakpoint %d: "
14075                            "multiple locations found\n"),
14076                          b->number);
14077       return;
14078     }
14079
14080   /* If there's no new locations, and all existing locations are
14081      pending, don't do anything.  This optimizes the common case where
14082      all locations are in the same shared library, that was unloaded.
14083      We'd like to retain the location, so that when the library is
14084      loaded again, we don't loose the enabled/disabled status of the
14085      individual locations.  */
14086   if (all_locations_are_pending (existing_locations) && sals.nelts == 0)
14087     return;
14088
14089   b->loc = NULL;
14090
14091   for (i = 0; i < sals.nelts; ++i)
14092     {
14093       struct bp_location *new_loc;
14094
14095       switch_to_program_space_and_thread (sals.sals[i].pspace);
14096
14097       new_loc = add_location_to_breakpoint (b, &(sals.sals[i]));
14098
14099       /* Reparse conditions, they might contain references to the
14100          old symtab.  */
14101       if (b->cond_string != NULL)
14102         {
14103           const char *s;
14104           volatile struct gdb_exception e;
14105
14106           s = b->cond_string;
14107           TRY_CATCH (e, RETURN_MASK_ERROR)
14108             {
14109               new_loc->cond = parse_exp_1 (&s, sals.sals[i].pc,
14110                                            block_for_pc (sals.sals[i].pc), 
14111                                            0);
14112             }
14113           if (e.reason < 0)
14114             {
14115               warning (_("failed to reevaluate condition "
14116                          "for breakpoint %d: %s"), 
14117                        b->number, e.message);
14118               new_loc->enabled = 0;
14119             }
14120         }
14121
14122       if (sals_end.nelts)
14123         {
14124           CORE_ADDR end = find_breakpoint_range_end (sals_end.sals[0]);
14125
14126           new_loc->length = end - sals.sals[0].pc + 1;
14127         }
14128     }
14129
14130   /* Update locations of permanent breakpoints.  */
14131   if (b->enable_state == bp_permanent)
14132     make_breakpoint_permanent (b);
14133
14134   /* If possible, carry over 'disable' status from existing
14135      breakpoints.  */
14136   {
14137     struct bp_location *e = existing_locations;
14138     /* If there are multiple breakpoints with the same function name,
14139        e.g. for inline functions, comparing function names won't work.
14140        Instead compare pc addresses; this is just a heuristic as things
14141        may have moved, but in practice it gives the correct answer
14142        often enough until a better solution is found.  */
14143     int have_ambiguous_names = ambiguous_names_p (b->loc);
14144
14145     for (; e; e = e->next)
14146       {
14147         if (!e->enabled && e->function_name)
14148           {
14149             struct bp_location *l = b->loc;
14150             if (have_ambiguous_names)
14151               {
14152                 for (; l; l = l->next)
14153                   if (breakpoint_locations_match (e, l))
14154                     {
14155                       l->enabled = 0;
14156                       break;
14157                     }
14158               }
14159             else
14160               {
14161                 for (; l; l = l->next)
14162                   if (l->function_name
14163                       && strcmp (e->function_name, l->function_name) == 0)
14164                     {
14165                       l->enabled = 0;
14166                       break;
14167                     }
14168               }
14169           }
14170       }
14171   }
14172
14173   if (!locations_are_equal (existing_locations, b->loc))
14174     observer_notify_breakpoint_modified (b);
14175
14176   update_global_location_list (1);
14177 }
14178
14179 /* Find the SaL locations corresponding to the given ADDR_STRING.
14180    On return, FOUND will be 1 if any SaL was found, zero otherwise.  */
14181
14182 static struct symtabs_and_lines
14183 addr_string_to_sals (struct breakpoint *b, char *addr_string, int *found)
14184 {
14185   char *s;
14186   struct symtabs_and_lines sals = {0};
14187   volatile struct gdb_exception e;
14188
14189   gdb_assert (b->ops != NULL);
14190   s = addr_string;
14191
14192   TRY_CATCH (e, RETURN_MASK_ERROR)
14193     {
14194       b->ops->decode_linespec (b, &s, &sals);
14195     }
14196   if (e.reason < 0)
14197     {
14198       int not_found_and_ok = 0;
14199       /* For pending breakpoints, it's expected that parsing will
14200          fail until the right shared library is loaded.  User has
14201          already told to create pending breakpoints and don't need
14202          extra messages.  If breakpoint is in bp_shlib_disabled
14203          state, then user already saw the message about that
14204          breakpoint being disabled, and don't want to see more
14205          errors.  */
14206       if (e.error == NOT_FOUND_ERROR
14207           && (b->condition_not_parsed 
14208               || (b->loc && b->loc->shlib_disabled)
14209               || (b->loc && b->loc->pspace->executing_startup)
14210               || b->enable_state == bp_disabled))
14211         not_found_and_ok = 1;
14212
14213       if (!not_found_and_ok)
14214         {
14215           /* We surely don't want to warn about the same breakpoint
14216              10 times.  One solution, implemented here, is disable
14217              the breakpoint on error.  Another solution would be to
14218              have separate 'warning emitted' flag.  Since this
14219              happens only when a binary has changed, I don't know
14220              which approach is better.  */
14221           b->enable_state = bp_disabled;
14222           throw_exception (e);
14223         }
14224     }
14225
14226   if (e.reason == 0 || e.error != NOT_FOUND_ERROR)
14227     {
14228       int i;
14229
14230       for (i = 0; i < sals.nelts; ++i)
14231         resolve_sal_pc (&sals.sals[i]);
14232       if (b->condition_not_parsed && s && s[0])
14233         {
14234           char *cond_string, *extra_string;
14235           int thread, task;
14236
14237           find_condition_and_thread (s, sals.sals[0].pc,
14238                                      &cond_string, &thread, &task,
14239                                      &extra_string);
14240           if (cond_string)
14241             b->cond_string = cond_string;
14242           b->thread = thread;
14243           b->task = task;
14244           if (extra_string)
14245             b->extra_string = extra_string;
14246           b->condition_not_parsed = 0;
14247         }
14248
14249       if (b->type == bp_static_tracepoint && !strace_marker_p (b))
14250         sals.sals[0] = update_static_tracepoint (b, sals.sals[0]);
14251
14252       *found = 1;
14253     }
14254   else
14255     *found = 0;
14256
14257   return sals;
14258 }
14259
14260 /* The default re_set method, for typical hardware or software
14261    breakpoints.  Reevaluate the breakpoint and recreate its
14262    locations.  */
14263
14264 static void
14265 breakpoint_re_set_default (struct breakpoint *b)
14266 {
14267   int found;
14268   struct symtabs_and_lines sals, sals_end;
14269   struct symtabs_and_lines expanded = {0};
14270   struct symtabs_and_lines expanded_end = {0};
14271
14272   sals = addr_string_to_sals (b, b->addr_string, &found);
14273   if (found)
14274     {
14275       make_cleanup (xfree, sals.sals);
14276       expanded = sals;
14277     }
14278
14279   if (b->addr_string_range_end)
14280     {
14281       sals_end = addr_string_to_sals (b, b->addr_string_range_end, &found);
14282       if (found)
14283         {
14284           make_cleanup (xfree, sals_end.sals);
14285           expanded_end = sals_end;
14286         }
14287     }
14288
14289   update_breakpoint_locations (b, expanded, expanded_end);
14290 }
14291
14292 /* Default method for creating SALs from an address string.  It basically
14293    calls parse_breakpoint_sals.  Return 1 for success, zero for failure.  */
14294
14295 static void
14296 create_sals_from_address_default (char **arg,
14297                                   struct linespec_result *canonical,
14298                                   enum bptype type_wanted,
14299                                   char *addr_start, char **copy_arg)
14300 {
14301   parse_breakpoint_sals (arg, canonical);
14302 }
14303
14304 /* Call create_breakpoints_sal for the given arguments.  This is the default
14305    function for the `create_breakpoints_sal' method of
14306    breakpoint_ops.  */
14307
14308 static void
14309 create_breakpoints_sal_default (struct gdbarch *gdbarch,
14310                                 struct linespec_result *canonical,
14311                                 char *cond_string,
14312                                 char *extra_string,
14313                                 enum bptype type_wanted,
14314                                 enum bpdisp disposition,
14315                                 int thread,
14316                                 int task, int ignore_count,
14317                                 const struct breakpoint_ops *ops,
14318                                 int from_tty, int enabled,
14319                                 int internal, unsigned flags)
14320 {
14321   create_breakpoints_sal (gdbarch, canonical, cond_string,
14322                           extra_string,
14323                           type_wanted, disposition,
14324                           thread, task, ignore_count, ops, from_tty,
14325                           enabled, internal, flags);
14326 }
14327
14328 /* Decode the line represented by S by calling decode_line_full.  This is the
14329    default function for the `decode_linespec' method of breakpoint_ops.  */
14330
14331 static void
14332 decode_linespec_default (struct breakpoint *b, char **s,
14333                          struct symtabs_and_lines *sals)
14334 {
14335   struct linespec_result canonical;
14336
14337   init_linespec_result (&canonical);
14338   decode_line_full (s, DECODE_LINE_FUNFIRSTLINE,
14339                     (struct symtab *) NULL, 0,
14340                     &canonical, multiple_symbols_all,
14341                     b->filter);
14342
14343   /* We should get 0 or 1 resulting SALs.  */
14344   gdb_assert (VEC_length (linespec_sals, canonical.sals) < 2);
14345
14346   if (VEC_length (linespec_sals, canonical.sals) > 0)
14347     {
14348       struct linespec_sals *lsal;
14349
14350       lsal = VEC_index (linespec_sals, canonical.sals, 0);
14351       *sals = lsal->sals;
14352       /* Arrange it so the destructor does not free the
14353          contents.  */
14354       lsal->sals.sals = NULL;
14355     }
14356
14357   destroy_linespec_result (&canonical);
14358 }
14359
14360 /* Prepare the global context for a re-set of breakpoint B.  */
14361
14362 static struct cleanup *
14363 prepare_re_set_context (struct breakpoint *b)
14364 {
14365   struct cleanup *cleanups;
14366
14367   input_radix = b->input_radix;
14368   cleanups = save_current_space_and_thread ();
14369   if (b->pspace != NULL)
14370     switch_to_program_space_and_thread (b->pspace);
14371   set_language (b->language);
14372
14373   return cleanups;
14374 }
14375
14376 /* Reset a breakpoint given it's struct breakpoint * BINT.
14377    The value we return ends up being the return value from catch_errors.
14378    Unused in this case.  */
14379
14380 static int
14381 breakpoint_re_set_one (void *bint)
14382 {
14383   /* Get past catch_errs.  */
14384   struct breakpoint *b = (struct breakpoint *) bint;
14385   struct cleanup *cleanups;
14386
14387   cleanups = prepare_re_set_context (b);
14388   b->ops->re_set (b);
14389   do_cleanups (cleanups);
14390   return 0;
14391 }
14392
14393 /* Re-set all breakpoints after symbols have been re-loaded.  */
14394 void
14395 breakpoint_re_set (void)
14396 {
14397   struct breakpoint *b, *b_tmp;
14398   enum language save_language;
14399   int save_input_radix;
14400   struct cleanup *old_chain;
14401
14402   save_language = current_language->la_language;
14403   save_input_radix = input_radix;
14404   old_chain = save_current_program_space ();
14405
14406   ALL_BREAKPOINTS_SAFE (b, b_tmp)
14407   {
14408     /* Format possible error msg.  */
14409     char *message = xstrprintf ("Error in re-setting breakpoint %d: ",
14410                                 b->number);
14411     struct cleanup *cleanups = make_cleanup (xfree, message);
14412     catch_errors (breakpoint_re_set_one, b, message, RETURN_MASK_ALL);
14413     do_cleanups (cleanups);
14414   }
14415   set_language (save_language);
14416   input_radix = save_input_radix;
14417
14418   jit_breakpoint_re_set ();
14419
14420   do_cleanups (old_chain);
14421
14422   create_overlay_event_breakpoint ();
14423   create_longjmp_master_breakpoint ();
14424   create_std_terminate_master_breakpoint ();
14425   create_exception_master_breakpoint ();
14426 }
14427 \f
14428 /* Reset the thread number of this breakpoint:
14429
14430    - If the breakpoint is for all threads, leave it as-is.
14431    - Else, reset it to the current thread for inferior_ptid.  */
14432 void
14433 breakpoint_re_set_thread (struct breakpoint *b)
14434 {
14435   if (b->thread != -1)
14436     {
14437       if (in_thread_list (inferior_ptid))
14438         b->thread = pid_to_thread_id (inferior_ptid);
14439
14440       /* We're being called after following a fork.  The new fork is
14441          selected as current, and unless this was a vfork will have a
14442          different program space from the original thread.  Reset that
14443          as well.  */
14444       b->loc->pspace = current_program_space;
14445     }
14446 }
14447
14448 /* Set ignore-count of breakpoint number BPTNUM to COUNT.
14449    If from_tty is nonzero, it prints a message to that effect,
14450    which ends with a period (no newline).  */
14451
14452 void
14453 set_ignore_count (int bptnum, int count, int from_tty)
14454 {
14455   struct breakpoint *b;
14456
14457   if (count < 0)
14458     count = 0;
14459
14460   ALL_BREAKPOINTS (b)
14461     if (b->number == bptnum)
14462     {
14463       if (is_tracepoint (b))
14464         {
14465           if (from_tty && count != 0)
14466             printf_filtered (_("Ignore count ignored for tracepoint %d."),
14467                              bptnum);
14468           return;
14469         }
14470       
14471       b->ignore_count = count;
14472       if (from_tty)
14473         {
14474           if (count == 0)
14475             printf_filtered (_("Will stop next time "
14476                                "breakpoint %d is reached."),
14477                              bptnum);
14478           else if (count == 1)
14479             printf_filtered (_("Will ignore next crossing of breakpoint %d."),
14480                              bptnum);
14481           else
14482             printf_filtered (_("Will ignore next %d "
14483                                "crossings of breakpoint %d."),
14484                              count, bptnum);
14485         }
14486       observer_notify_breakpoint_modified (b);
14487       return;
14488     }
14489
14490   error (_("No breakpoint number %d."), bptnum);
14491 }
14492
14493 /* Command to set ignore-count of breakpoint N to COUNT.  */
14494
14495 static void
14496 ignore_command (char *args, int from_tty)
14497 {
14498   char *p = args;
14499   int num;
14500
14501   if (p == 0)
14502     error_no_arg (_("a breakpoint number"));
14503
14504   num = get_number (&p);
14505   if (num == 0)
14506     error (_("bad breakpoint number: '%s'"), args);
14507   if (*p == 0)
14508     error (_("Second argument (specified ignore-count) is missing."));
14509
14510   set_ignore_count (num,
14511                     longest_to_int (value_as_long (parse_and_eval (p))),
14512                     from_tty);
14513   if (from_tty)
14514     printf_filtered ("\n");
14515 }
14516 \f
14517 /* Call FUNCTION on each of the breakpoints
14518    whose numbers are given in ARGS.  */
14519
14520 static void
14521 map_breakpoint_numbers (char *args, void (*function) (struct breakpoint *,
14522                                                       void *),
14523                         void *data)
14524 {
14525   int num;
14526   struct breakpoint *b, *tmp;
14527   int match;
14528   struct get_number_or_range_state state;
14529
14530   if (args == 0)
14531     error_no_arg (_("one or more breakpoint numbers"));
14532
14533   init_number_or_range (&state, args);
14534
14535   while (!state.finished)
14536     {
14537       char *p = state.string;
14538
14539       match = 0;
14540
14541       num = get_number_or_range (&state);
14542       if (num == 0)
14543         {
14544           warning (_("bad breakpoint number at or near '%s'"), p);
14545         }
14546       else
14547         {
14548           ALL_BREAKPOINTS_SAFE (b, tmp)
14549             if (b->number == num)
14550               {
14551                 match = 1;
14552                 function (b, data);
14553                 break;
14554               }
14555           if (match == 0)
14556             printf_unfiltered (_("No breakpoint number %d.\n"), num);
14557         }
14558     }
14559 }
14560
14561 static struct bp_location *
14562 find_location_by_number (char *number)
14563 {
14564   char *dot = strchr (number, '.');
14565   char *p1;
14566   int bp_num;
14567   int loc_num;
14568   struct breakpoint *b;
14569   struct bp_location *loc;  
14570
14571   *dot = '\0';
14572
14573   p1 = number;
14574   bp_num = get_number (&p1);
14575   if (bp_num == 0)
14576     error (_("Bad breakpoint number '%s'"), number);
14577
14578   ALL_BREAKPOINTS (b)
14579     if (b->number == bp_num)
14580       {
14581         break;
14582       }
14583
14584   if (!b || b->number != bp_num)
14585     error (_("Bad breakpoint number '%s'"), number);
14586   
14587   p1 = dot+1;
14588   loc_num = get_number (&p1);
14589   if (loc_num == 0)
14590     error (_("Bad breakpoint location number '%s'"), number);
14591
14592   --loc_num;
14593   loc = b->loc;
14594   for (;loc_num && loc; --loc_num, loc = loc->next)
14595     ;
14596   if (!loc)
14597     error (_("Bad breakpoint location number '%s'"), dot+1);
14598     
14599   return loc;  
14600 }
14601
14602
14603 /* Set ignore-count of breakpoint number BPTNUM to COUNT.
14604    If from_tty is nonzero, it prints a message to that effect,
14605    which ends with a period (no newline).  */
14606
14607 void
14608 disable_breakpoint (struct breakpoint *bpt)
14609 {
14610   /* Never disable a watchpoint scope breakpoint; we want to
14611      hit them when we leave scope so we can delete both the
14612      watchpoint and its scope breakpoint at that time.  */
14613   if (bpt->type == bp_watchpoint_scope)
14614     return;
14615
14616   /* You can't disable permanent breakpoints.  */
14617   if (bpt->enable_state == bp_permanent)
14618     return;
14619
14620   bpt->enable_state = bp_disabled;
14621
14622   /* Mark breakpoint locations modified.  */
14623   mark_breakpoint_modified (bpt);
14624
14625   if (target_supports_enable_disable_tracepoint ()
14626       && current_trace_status ()->running && is_tracepoint (bpt))
14627     {
14628       struct bp_location *location;
14629      
14630       for (location = bpt->loc; location; location = location->next)
14631         target_disable_tracepoint (location);
14632     }
14633
14634   update_global_location_list (0);
14635
14636   observer_notify_breakpoint_modified (bpt);
14637 }
14638
14639 /* A callback for iterate_over_related_breakpoints.  */
14640
14641 static void
14642 do_disable_breakpoint (struct breakpoint *b, void *ignore)
14643 {
14644   disable_breakpoint (b);
14645 }
14646
14647 /* A callback for map_breakpoint_numbers that calls
14648    disable_breakpoint.  */
14649
14650 static void
14651 do_map_disable_breakpoint (struct breakpoint *b, void *ignore)
14652 {
14653   iterate_over_related_breakpoints (b, do_disable_breakpoint, NULL);
14654 }
14655
14656 static void
14657 disable_command (char *args, int from_tty)
14658 {
14659   if (args == 0)
14660     {
14661       struct breakpoint *bpt;
14662
14663       ALL_BREAKPOINTS (bpt)
14664         if (user_breakpoint_p (bpt))
14665           disable_breakpoint (bpt);
14666     }
14667   else
14668     {
14669       char *num = extract_arg (&args);
14670
14671       while (num)
14672         {
14673           if (strchr (num, '.'))
14674             {
14675               struct bp_location *loc = find_location_by_number (num);
14676
14677               if (loc)
14678                 {
14679                   if (loc->enabled)
14680                     {
14681                       loc->enabled = 0;
14682                       mark_breakpoint_location_modified (loc);
14683                     }
14684                   if (target_supports_enable_disable_tracepoint ()
14685                       && current_trace_status ()->running && loc->owner
14686                       && is_tracepoint (loc->owner))
14687                     target_disable_tracepoint (loc);
14688                 }
14689               update_global_location_list (0);
14690             }
14691           else
14692             map_breakpoint_numbers (num, do_map_disable_breakpoint, NULL);
14693           num = extract_arg (&args);
14694         }
14695     }
14696 }
14697
14698 static void
14699 enable_breakpoint_disp (struct breakpoint *bpt, enum bpdisp disposition,
14700                         int count)
14701 {
14702   int target_resources_ok;
14703
14704   if (bpt->type == bp_hardware_breakpoint)
14705     {
14706       int i;
14707       i = hw_breakpoint_used_count ();
14708       target_resources_ok = 
14709         target_can_use_hardware_watchpoint (bp_hardware_breakpoint, 
14710                                             i + 1, 0);
14711       if (target_resources_ok == 0)
14712         error (_("No hardware breakpoint support in the target."));
14713       else if (target_resources_ok < 0)
14714         error (_("Hardware breakpoints used exceeds limit."));
14715     }
14716
14717   if (is_watchpoint (bpt))
14718     {
14719       /* Initialize it just to avoid a GCC false warning.  */
14720       enum enable_state orig_enable_state = 0;
14721       volatile struct gdb_exception e;
14722
14723       TRY_CATCH (e, RETURN_MASK_ALL)
14724         {
14725           struct watchpoint *w = (struct watchpoint *) bpt;
14726
14727           orig_enable_state = bpt->enable_state;
14728           bpt->enable_state = bp_enabled;
14729           update_watchpoint (w, 1 /* reparse */);
14730         }
14731       if (e.reason < 0)
14732         {
14733           bpt->enable_state = orig_enable_state;
14734           exception_fprintf (gdb_stderr, e, _("Cannot enable watchpoint %d: "),
14735                              bpt->number);
14736           return;
14737         }
14738     }
14739
14740   if (bpt->enable_state != bp_permanent)
14741     bpt->enable_state = bp_enabled;
14742
14743   bpt->enable_state = bp_enabled;
14744
14745   /* Mark breakpoint locations modified.  */
14746   mark_breakpoint_modified (bpt);
14747
14748   if (target_supports_enable_disable_tracepoint ()
14749       && current_trace_status ()->running && is_tracepoint (bpt))
14750     {
14751       struct bp_location *location;
14752
14753       for (location = bpt->loc; location; location = location->next)
14754         target_enable_tracepoint (location);
14755     }
14756
14757   bpt->disposition = disposition;
14758   bpt->enable_count = count;
14759   update_global_location_list (1);
14760
14761   observer_notify_breakpoint_modified (bpt);
14762 }
14763
14764
14765 void
14766 enable_breakpoint (struct breakpoint *bpt)
14767 {
14768   enable_breakpoint_disp (bpt, bpt->disposition, 0);
14769 }
14770
14771 static void
14772 do_enable_breakpoint (struct breakpoint *bpt, void *arg)
14773 {
14774   enable_breakpoint (bpt);
14775 }
14776
14777 /* A callback for map_breakpoint_numbers that calls
14778    enable_breakpoint.  */
14779
14780 static void
14781 do_map_enable_breakpoint (struct breakpoint *b, void *ignore)
14782 {
14783   iterate_over_related_breakpoints (b, do_enable_breakpoint, NULL);
14784 }
14785
14786 /* The enable command enables the specified breakpoints (or all defined
14787    breakpoints) so they once again become (or continue to be) effective
14788    in stopping the inferior.  */
14789
14790 static void
14791 enable_command (char *args, int from_tty)
14792 {
14793   if (args == 0)
14794     {
14795       struct breakpoint *bpt;
14796
14797       ALL_BREAKPOINTS (bpt)
14798         if (user_breakpoint_p (bpt))
14799           enable_breakpoint (bpt);
14800     }
14801   else
14802     {
14803       char *num = extract_arg (&args);
14804
14805       while (num)
14806         {
14807           if (strchr (num, '.'))
14808             {
14809               struct bp_location *loc = find_location_by_number (num);
14810
14811               if (loc)
14812                 {
14813                   if (!loc->enabled)
14814                     {
14815                       loc->enabled = 1;
14816                       mark_breakpoint_location_modified (loc);
14817                     }
14818                   if (target_supports_enable_disable_tracepoint ()
14819                       && current_trace_status ()->running && loc->owner
14820                       && is_tracepoint (loc->owner))
14821                     target_enable_tracepoint (loc);
14822                 }
14823               update_global_location_list (1);
14824             }
14825           else
14826             map_breakpoint_numbers (num, do_map_enable_breakpoint, NULL);
14827           num = extract_arg (&args);
14828         }
14829     }
14830 }
14831
14832 /* This struct packages up disposition data for application to multiple
14833    breakpoints.  */
14834
14835 struct disp_data
14836 {
14837   enum bpdisp disp;
14838   int count;
14839 };
14840
14841 static void
14842 do_enable_breakpoint_disp (struct breakpoint *bpt, void *arg)
14843 {
14844   struct disp_data disp_data = *(struct disp_data *) arg;
14845
14846   enable_breakpoint_disp (bpt, disp_data.disp, disp_data.count);
14847 }
14848
14849 static void
14850 do_map_enable_once_breakpoint (struct breakpoint *bpt, void *ignore)
14851 {
14852   struct disp_data disp = { disp_disable, 1 };
14853
14854   iterate_over_related_breakpoints (bpt, do_enable_breakpoint_disp, &disp);
14855 }
14856
14857 static void
14858 enable_once_command (char *args, int from_tty)
14859 {
14860   map_breakpoint_numbers (args, do_map_enable_once_breakpoint, NULL);
14861 }
14862
14863 static void
14864 do_map_enable_count_breakpoint (struct breakpoint *bpt, void *countptr)
14865 {
14866   struct disp_data disp = { disp_disable, *(int *) countptr };
14867
14868   iterate_over_related_breakpoints (bpt, do_enable_breakpoint_disp, &disp);
14869 }
14870
14871 static void
14872 enable_count_command (char *args, int from_tty)
14873 {
14874   int count = get_number (&args);
14875
14876   map_breakpoint_numbers (args, do_map_enable_count_breakpoint, &count);
14877 }
14878
14879 static void
14880 do_map_enable_delete_breakpoint (struct breakpoint *bpt, void *ignore)
14881 {
14882   struct disp_data disp = { disp_del, 1 };
14883
14884   iterate_over_related_breakpoints (bpt, do_enable_breakpoint_disp, &disp);
14885 }
14886
14887 static void
14888 enable_delete_command (char *args, int from_tty)
14889 {
14890   map_breakpoint_numbers (args, do_map_enable_delete_breakpoint, NULL);
14891 }
14892 \f
14893 static void
14894 set_breakpoint_cmd (char *args, int from_tty)
14895 {
14896 }
14897
14898 static void
14899 show_breakpoint_cmd (char *args, int from_tty)
14900 {
14901 }
14902
14903 /* Invalidate last known value of any hardware watchpoint if
14904    the memory which that value represents has been written to by
14905    GDB itself.  */
14906
14907 static void
14908 invalidate_bp_value_on_memory_change (struct inferior *inferior,
14909                                       CORE_ADDR addr, ssize_t len,
14910                                       const bfd_byte *data)
14911 {
14912   struct breakpoint *bp;
14913
14914   ALL_BREAKPOINTS (bp)
14915     if (bp->enable_state == bp_enabled
14916         && bp->type == bp_hardware_watchpoint)
14917       {
14918         struct watchpoint *wp = (struct watchpoint *) bp;
14919
14920         if (wp->val_valid && wp->val)
14921           {
14922             struct bp_location *loc;
14923
14924             for (loc = bp->loc; loc != NULL; loc = loc->next)
14925               if (loc->loc_type == bp_loc_hardware_watchpoint
14926                   && loc->address + loc->length > addr
14927                   && addr + len > loc->address)
14928                 {
14929                   value_free (wp->val);
14930                   wp->val = NULL;
14931                   wp->val_valid = 0;
14932                 }
14933           }
14934       }
14935 }
14936
14937 /* Create and insert a raw software breakpoint at PC.  Return an
14938    identifier, which should be used to remove the breakpoint later.
14939    In general, places which call this should be using something on the
14940    breakpoint chain instead; this function should be eliminated
14941    someday.  */
14942
14943 void *
14944 deprecated_insert_raw_breakpoint (struct gdbarch *gdbarch,
14945                                   struct address_space *aspace, CORE_ADDR pc)
14946 {
14947   struct bp_target_info *bp_tgt;
14948
14949   bp_tgt = XZALLOC (struct bp_target_info);
14950
14951   bp_tgt->placed_address_space = aspace;
14952   bp_tgt->placed_address = pc;
14953
14954   if (target_insert_breakpoint (gdbarch, bp_tgt) != 0)
14955     {
14956       /* Could not insert the breakpoint.  */
14957       xfree (bp_tgt);
14958       return NULL;
14959     }
14960
14961   return bp_tgt;
14962 }
14963
14964 /* Remove a breakpoint BP inserted by
14965    deprecated_insert_raw_breakpoint.  */
14966
14967 int
14968 deprecated_remove_raw_breakpoint (struct gdbarch *gdbarch, void *bp)
14969 {
14970   struct bp_target_info *bp_tgt = bp;
14971   int ret;
14972
14973   ret = target_remove_breakpoint (gdbarch, bp_tgt);
14974   xfree (bp_tgt);
14975
14976   return ret;
14977 }
14978
14979 /* One (or perhaps two) breakpoints used for software single
14980    stepping.  */
14981
14982 static void *single_step_breakpoints[2];
14983 static struct gdbarch *single_step_gdbarch[2];
14984
14985 /* Create and insert a breakpoint for software single step.  */
14986
14987 void
14988 insert_single_step_breakpoint (struct gdbarch *gdbarch,
14989                                struct address_space *aspace, 
14990                                CORE_ADDR next_pc)
14991 {
14992   void **bpt_p;
14993
14994   if (single_step_breakpoints[0] == NULL)
14995     {
14996       bpt_p = &single_step_breakpoints[0];
14997       single_step_gdbarch[0] = gdbarch;
14998     }
14999   else
15000     {
15001       gdb_assert (single_step_breakpoints[1] == NULL);
15002       bpt_p = &single_step_breakpoints[1];
15003       single_step_gdbarch[1] = gdbarch;
15004     }
15005
15006   /* NOTE drow/2006-04-11: A future improvement to this function would
15007      be to only create the breakpoints once, and actually put them on
15008      the breakpoint chain.  That would let us use set_raw_breakpoint.
15009      We could adjust the addresses each time they were needed.  Doing
15010      this requires corresponding changes elsewhere where single step
15011      breakpoints are handled, however.  So, for now, we use this.  */
15012
15013   *bpt_p = deprecated_insert_raw_breakpoint (gdbarch, aspace, next_pc);
15014   if (*bpt_p == NULL)
15015     error (_("Could not insert single-step breakpoint at %s"),
15016              paddress (gdbarch, next_pc));
15017 }
15018
15019 /* Check if the breakpoints used for software single stepping
15020    were inserted or not.  */
15021
15022 int
15023 single_step_breakpoints_inserted (void)
15024 {
15025   return (single_step_breakpoints[0] != NULL
15026           || single_step_breakpoints[1] != NULL);
15027 }
15028
15029 /* Remove and delete any breakpoints used for software single step.  */
15030
15031 void
15032 remove_single_step_breakpoints (void)
15033 {
15034   gdb_assert (single_step_breakpoints[0] != NULL);
15035
15036   /* See insert_single_step_breakpoint for more about this deprecated
15037      call.  */
15038   deprecated_remove_raw_breakpoint (single_step_gdbarch[0],
15039                                     single_step_breakpoints[0]);
15040   single_step_gdbarch[0] = NULL;
15041   single_step_breakpoints[0] = NULL;
15042
15043   if (single_step_breakpoints[1] != NULL)
15044     {
15045       deprecated_remove_raw_breakpoint (single_step_gdbarch[1],
15046                                         single_step_breakpoints[1]);
15047       single_step_gdbarch[1] = NULL;
15048       single_step_breakpoints[1] = NULL;
15049     }
15050 }
15051
15052 /* Delete software single step breakpoints without removing them from
15053    the inferior.  This is intended to be used if the inferior's address
15054    space where they were inserted is already gone, e.g. after exit or
15055    exec.  */
15056
15057 void
15058 cancel_single_step_breakpoints (void)
15059 {
15060   int i;
15061
15062   for (i = 0; i < 2; i++)
15063     if (single_step_breakpoints[i])
15064       {
15065         xfree (single_step_breakpoints[i]);
15066         single_step_breakpoints[i] = NULL;
15067         single_step_gdbarch[i] = NULL;
15068       }
15069 }
15070
15071 /* Detach software single-step breakpoints from INFERIOR_PTID without
15072    removing them.  */
15073
15074 static void
15075 detach_single_step_breakpoints (void)
15076 {
15077   int i;
15078
15079   for (i = 0; i < 2; i++)
15080     if (single_step_breakpoints[i])
15081       target_remove_breakpoint (single_step_gdbarch[i],
15082                                 single_step_breakpoints[i]);
15083 }
15084
15085 /* Check whether a software single-step breakpoint is inserted at
15086    PC.  */
15087
15088 static int
15089 single_step_breakpoint_inserted_here_p (struct address_space *aspace, 
15090                                         CORE_ADDR pc)
15091 {
15092   int i;
15093
15094   for (i = 0; i < 2; i++)
15095     {
15096       struct bp_target_info *bp_tgt = single_step_breakpoints[i];
15097       if (bp_tgt
15098           && breakpoint_address_match (bp_tgt->placed_address_space,
15099                                        bp_tgt->placed_address,
15100                                        aspace, pc))
15101         return 1;
15102     }
15103
15104   return 0;
15105 }
15106
15107 /* Returns 0 if 'bp' is NOT a syscall catchpoint,
15108    non-zero otherwise.  */
15109 static int
15110 is_syscall_catchpoint_enabled (struct breakpoint *bp)
15111 {
15112   if (syscall_catchpoint_p (bp)
15113       && bp->enable_state != bp_disabled
15114       && bp->enable_state != bp_call_disabled)
15115     return 1;
15116   else
15117     return 0;
15118 }
15119
15120 int
15121 catch_syscall_enabled (void)
15122 {
15123   struct catch_syscall_inferior_data *inf_data
15124     = get_catch_syscall_inferior_data (current_inferior ());
15125
15126   return inf_data->total_syscalls_count != 0;
15127 }
15128
15129 int
15130 catching_syscall_number (int syscall_number)
15131 {
15132   struct breakpoint *bp;
15133
15134   ALL_BREAKPOINTS (bp)
15135     if (is_syscall_catchpoint_enabled (bp))
15136       {
15137         struct syscall_catchpoint *c = (struct syscall_catchpoint *) bp;
15138
15139         if (c->syscalls_to_be_caught)
15140           {
15141             int i, iter;
15142             for (i = 0;
15143                  VEC_iterate (int, c->syscalls_to_be_caught, i, iter);
15144                  i++)
15145               if (syscall_number == iter)
15146                 return 1;
15147           }
15148         else
15149           return 1;
15150       }
15151
15152   return 0;
15153 }
15154
15155 /* Complete syscall names.  Used by "catch syscall".  */
15156 static VEC (char_ptr) *
15157 catch_syscall_completer (struct cmd_list_element *cmd,
15158                          const char *text, const char *word)
15159 {
15160   const char **list = get_syscall_names ();
15161   VEC (char_ptr) *retlist
15162     = (list == NULL) ? NULL : complete_on_enum (list, word, word);
15163
15164   xfree (list);
15165   return retlist;
15166 }
15167
15168 /* Tracepoint-specific operations.  */
15169
15170 /* Set tracepoint count to NUM.  */
15171 static void
15172 set_tracepoint_count (int num)
15173 {
15174   tracepoint_count = num;
15175   set_internalvar_integer (lookup_internalvar ("tpnum"), num);
15176 }
15177
15178 static void
15179 trace_command (char *arg, int from_tty)
15180 {
15181   struct breakpoint_ops *ops;
15182   const char *arg_cp = arg;
15183
15184   if (arg && probe_linespec_to_ops (&arg_cp))
15185     ops = &tracepoint_probe_breakpoint_ops;
15186   else
15187     ops = &tracepoint_breakpoint_ops;
15188
15189   create_breakpoint (get_current_arch (),
15190                      arg,
15191                      NULL, 0, NULL, 1 /* parse arg */,
15192                      0 /* tempflag */,
15193                      bp_tracepoint /* type_wanted */,
15194                      0 /* Ignore count */,
15195                      pending_break_support,
15196                      ops,
15197                      from_tty,
15198                      1 /* enabled */,
15199                      0 /* internal */, 0);
15200 }
15201
15202 static void
15203 ftrace_command (char *arg, int from_tty)
15204 {
15205   create_breakpoint (get_current_arch (),
15206                      arg,
15207                      NULL, 0, NULL, 1 /* parse arg */,
15208                      0 /* tempflag */,
15209                      bp_fast_tracepoint /* type_wanted */,
15210                      0 /* Ignore count */,
15211                      pending_break_support,
15212                      &tracepoint_breakpoint_ops,
15213                      from_tty,
15214                      1 /* enabled */,
15215                      0 /* internal */, 0);
15216 }
15217
15218 /* strace command implementation.  Creates a static tracepoint.  */
15219
15220 static void
15221 strace_command (char *arg, int from_tty)
15222 {
15223   struct breakpoint_ops *ops;
15224
15225   /* Decide if we are dealing with a static tracepoint marker (`-m'),
15226      or with a normal static tracepoint.  */
15227   if (arg && strncmp (arg, "-m", 2) == 0 && isspace (arg[2]))
15228     ops = &strace_marker_breakpoint_ops;
15229   else
15230     ops = &tracepoint_breakpoint_ops;
15231
15232   create_breakpoint (get_current_arch (),
15233                      arg,
15234                      NULL, 0, NULL, 1 /* parse arg */,
15235                      0 /* tempflag */,
15236                      bp_static_tracepoint /* type_wanted */,
15237                      0 /* Ignore count */,
15238                      pending_break_support,
15239                      ops,
15240                      from_tty,
15241                      1 /* enabled */,
15242                      0 /* internal */, 0);
15243 }
15244
15245 /* Set up a fake reader function that gets command lines from a linked
15246    list that was acquired during tracepoint uploading.  */
15247
15248 static struct uploaded_tp *this_utp;
15249 static int next_cmd;
15250
15251 static char *
15252 read_uploaded_action (void)
15253 {
15254   char *rslt;
15255
15256   VEC_iterate (char_ptr, this_utp->cmd_strings, next_cmd, rslt);
15257
15258   next_cmd++;
15259
15260   return rslt;
15261 }
15262
15263 /* Given information about a tracepoint as recorded on a target (which
15264    can be either a live system or a trace file), attempt to create an
15265    equivalent GDB tracepoint.  This is not a reliable process, since
15266    the target does not necessarily have all the information used when
15267    the tracepoint was originally defined.  */
15268   
15269 struct tracepoint *
15270 create_tracepoint_from_upload (struct uploaded_tp *utp)
15271 {
15272   char *addr_str, small_buf[100];
15273   struct tracepoint *tp;
15274
15275   if (utp->at_string)
15276     addr_str = utp->at_string;
15277   else
15278     {
15279       /* In the absence of a source location, fall back to raw
15280          address.  Since there is no way to confirm that the address
15281          means the same thing as when the trace was started, warn the
15282          user.  */
15283       warning (_("Uploaded tracepoint %d has no "
15284                  "source location, using raw address"),
15285                utp->number);
15286       xsnprintf (small_buf, sizeof (small_buf), "*%s", hex_string (utp->addr));
15287       addr_str = small_buf;
15288     }
15289
15290   /* There's not much we can do with a sequence of bytecodes.  */
15291   if (utp->cond && !utp->cond_string)
15292     warning (_("Uploaded tracepoint %d condition "
15293                "has no source form, ignoring it"),
15294              utp->number);
15295
15296   if (!create_breakpoint (get_current_arch (),
15297                           addr_str,
15298                           utp->cond_string, -1, NULL,
15299                           0 /* parse cond/thread */,
15300                           0 /* tempflag */,
15301                           utp->type /* type_wanted */,
15302                           0 /* Ignore count */,
15303                           pending_break_support,
15304                           &tracepoint_breakpoint_ops,
15305                           0 /* from_tty */,
15306                           utp->enabled /* enabled */,
15307                           0 /* internal */,
15308                           CREATE_BREAKPOINT_FLAGS_INSERTED))
15309     return NULL;
15310
15311   /* Get the tracepoint we just created.  */
15312   tp = get_tracepoint (tracepoint_count);
15313   gdb_assert (tp != NULL);
15314
15315   if (utp->pass > 0)
15316     {
15317       xsnprintf (small_buf, sizeof (small_buf), "%d %d", utp->pass,
15318                  tp->base.number);
15319
15320       trace_pass_command (small_buf, 0);
15321     }
15322
15323   /* If we have uploaded versions of the original commands, set up a
15324      special-purpose "reader" function and call the usual command line
15325      reader, then pass the result to the breakpoint command-setting
15326      function.  */
15327   if (!VEC_empty (char_ptr, utp->cmd_strings))
15328     {
15329       struct command_line *cmd_list;
15330
15331       this_utp = utp;
15332       next_cmd = 0;
15333
15334       cmd_list = read_command_lines_1 (read_uploaded_action, 1, NULL, NULL);
15335
15336       breakpoint_set_commands (&tp->base, cmd_list);
15337     }
15338   else if (!VEC_empty (char_ptr, utp->actions)
15339            || !VEC_empty (char_ptr, utp->step_actions))
15340     warning (_("Uploaded tracepoint %d actions "
15341                "have no source form, ignoring them"),
15342              utp->number);
15343
15344   /* Copy any status information that might be available.  */
15345   tp->base.hit_count = utp->hit_count;
15346   tp->traceframe_usage = utp->traceframe_usage;
15347
15348   return tp;
15349 }
15350   
15351 /* Print information on tracepoint number TPNUM_EXP, or all if
15352    omitted.  */
15353
15354 static void
15355 tracepoints_info (char *args, int from_tty)
15356 {
15357   struct ui_out *uiout = current_uiout;
15358   int num_printed;
15359
15360   num_printed = breakpoint_1 (args, 0, is_tracepoint);
15361
15362   if (num_printed == 0)
15363     {
15364       if (args == NULL || *args == '\0')
15365         ui_out_message (uiout, 0, "No tracepoints.\n");
15366       else
15367         ui_out_message (uiout, 0, "No tracepoint matching '%s'.\n", args);
15368     }
15369
15370   default_collect_info ();
15371 }
15372
15373 /* The 'enable trace' command enables tracepoints.
15374    Not supported by all targets.  */
15375 static void
15376 enable_trace_command (char *args, int from_tty)
15377 {
15378   enable_command (args, from_tty);
15379 }
15380
15381 /* The 'disable trace' command disables tracepoints.
15382    Not supported by all targets.  */
15383 static void
15384 disable_trace_command (char *args, int from_tty)
15385 {
15386   disable_command (args, from_tty);
15387 }
15388
15389 /* Remove a tracepoint (or all if no argument).  */
15390 static void
15391 delete_trace_command (char *arg, int from_tty)
15392 {
15393   struct breakpoint *b, *b_tmp;
15394
15395   dont_repeat ();
15396
15397   if (arg == 0)
15398     {
15399       int breaks_to_delete = 0;
15400
15401       /* Delete all breakpoints if no argument.
15402          Do not delete internal or call-dummy breakpoints, these
15403          have to be deleted with an explicit breakpoint number 
15404          argument.  */
15405       ALL_TRACEPOINTS (b)
15406         if (is_tracepoint (b) && user_breakpoint_p (b))
15407           {
15408             breaks_to_delete = 1;
15409             break;
15410           }
15411
15412       /* Ask user only if there are some breakpoints to delete.  */
15413       if (!from_tty
15414           || (breaks_to_delete && query (_("Delete all tracepoints? "))))
15415         {
15416           ALL_BREAKPOINTS_SAFE (b, b_tmp)
15417             if (is_tracepoint (b) && user_breakpoint_p (b))
15418               delete_breakpoint (b);
15419         }
15420     }
15421   else
15422     map_breakpoint_numbers (arg, do_map_delete_breakpoint, NULL);
15423 }
15424
15425 /* Helper function for trace_pass_command.  */
15426
15427 static void
15428 trace_pass_set_count (struct tracepoint *tp, int count, int from_tty)
15429 {
15430   tp->pass_count = count;
15431   observer_notify_breakpoint_modified (&tp->base);
15432   if (from_tty)
15433     printf_filtered (_("Setting tracepoint %d's passcount to %d\n"),
15434                      tp->base.number, count);
15435 }
15436
15437 /* Set passcount for tracepoint.
15438
15439    First command argument is passcount, second is tracepoint number.
15440    If tracepoint number omitted, apply to most recently defined.
15441    Also accepts special argument "all".  */
15442
15443 static void
15444 trace_pass_command (char *args, int from_tty)
15445 {
15446   struct tracepoint *t1;
15447   unsigned int count;
15448
15449   if (args == 0 || *args == 0)
15450     error (_("passcount command requires an "
15451              "argument (count + optional TP num)"));
15452
15453   count = strtoul (args, &args, 10);    /* Count comes first, then TP num.  */
15454
15455   args = skip_spaces (args);
15456   if (*args && strncasecmp (args, "all", 3) == 0)
15457     {
15458       struct breakpoint *b;
15459
15460       args += 3;                        /* Skip special argument "all".  */
15461       if (*args)
15462         error (_("Junk at end of arguments."));
15463
15464       ALL_TRACEPOINTS (b)
15465       {
15466         t1 = (struct tracepoint *) b;
15467         trace_pass_set_count (t1, count, from_tty);
15468       }
15469     }
15470   else if (*args == '\0')
15471     {
15472       t1 = get_tracepoint_by_number (&args, NULL, 1);
15473       if (t1)
15474         trace_pass_set_count (t1, count, from_tty);
15475     }
15476   else
15477     {
15478       struct get_number_or_range_state state;
15479
15480       init_number_or_range (&state, args);
15481       while (!state.finished)
15482         {
15483           t1 = get_tracepoint_by_number (&args, &state, 1);
15484           if (t1)
15485             trace_pass_set_count (t1, count, from_tty);
15486         }
15487     }
15488 }
15489
15490 struct tracepoint *
15491 get_tracepoint (int num)
15492 {
15493   struct breakpoint *t;
15494
15495   ALL_TRACEPOINTS (t)
15496     if (t->number == num)
15497       return (struct tracepoint *) t;
15498
15499   return NULL;
15500 }
15501
15502 /* Find the tracepoint with the given target-side number (which may be
15503    different from the tracepoint number after disconnecting and
15504    reconnecting).  */
15505
15506 struct tracepoint *
15507 get_tracepoint_by_number_on_target (int num)
15508 {
15509   struct breakpoint *b;
15510
15511   ALL_TRACEPOINTS (b)
15512     {
15513       struct tracepoint *t = (struct tracepoint *) b;
15514
15515       if (t->number_on_target == num)
15516         return t;
15517     }
15518
15519   return NULL;
15520 }
15521
15522 /* Utility: parse a tracepoint number and look it up in the list.
15523    If STATE is not NULL, use, get_number_or_range_state and ignore ARG.
15524    If OPTIONAL_P is true, then if the argument is missing, the most
15525    recent tracepoint (tracepoint_count) is returned.  */
15526 struct tracepoint *
15527 get_tracepoint_by_number (char **arg,
15528                           struct get_number_or_range_state *state,
15529                           int optional_p)
15530 {
15531   struct breakpoint *t;
15532   int tpnum;
15533   char *instring = arg == NULL ? NULL : *arg;
15534
15535   if (state)
15536     {
15537       gdb_assert (!state->finished);
15538       tpnum = get_number_or_range (state);
15539     }
15540   else if (arg == NULL || *arg == NULL || ! **arg)
15541     {
15542       if (optional_p)
15543         tpnum = tracepoint_count;
15544       else
15545         error_no_arg (_("tracepoint number"));
15546     }
15547   else
15548     tpnum = get_number (arg);
15549
15550   if (tpnum <= 0)
15551     {
15552       if (instring && *instring)
15553         printf_filtered (_("bad tracepoint number at or near '%s'\n"), 
15554                          instring);
15555       else
15556         printf_filtered (_("Tracepoint argument missing "
15557                            "and no previous tracepoint\n"));
15558       return NULL;
15559     }
15560
15561   ALL_TRACEPOINTS (t)
15562     if (t->number == tpnum)
15563     {
15564       return (struct tracepoint *) t;
15565     }
15566
15567   printf_unfiltered ("No tracepoint number %d.\n", tpnum);
15568   return NULL;
15569 }
15570
15571 void
15572 print_recreate_thread (struct breakpoint *b, struct ui_file *fp)
15573 {
15574   if (b->thread != -1)
15575     fprintf_unfiltered (fp, " thread %d", b->thread);
15576
15577   if (b->task != 0)
15578     fprintf_unfiltered (fp, " task %d", b->task);
15579
15580   fprintf_unfiltered (fp, "\n");
15581 }
15582
15583 /* Save information on user settable breakpoints (watchpoints, etc) to
15584    a new script file named FILENAME.  If FILTER is non-NULL, call it
15585    on each breakpoint and only include the ones for which it returns
15586    non-zero.  */
15587
15588 static void
15589 save_breakpoints (char *filename, int from_tty,
15590                   int (*filter) (const struct breakpoint *))
15591 {
15592   struct breakpoint *tp;
15593   int any = 0;
15594   struct cleanup *cleanup;
15595   struct ui_file *fp;
15596   int extra_trace_bits = 0;
15597
15598   if (filename == 0 || *filename == 0)
15599     error (_("Argument required (file name in which to save)"));
15600
15601   /* See if we have anything to save.  */
15602   ALL_BREAKPOINTS (tp)
15603   {
15604     /* Skip internal and momentary breakpoints.  */
15605     if (!user_breakpoint_p (tp))
15606       continue;
15607
15608     /* If we have a filter, only save the breakpoints it accepts.  */
15609     if (filter && !filter (tp))
15610       continue;
15611
15612     any = 1;
15613
15614     if (is_tracepoint (tp))
15615       {
15616         extra_trace_bits = 1;
15617
15618         /* We can stop searching.  */
15619         break;
15620       }
15621   }
15622
15623   if (!any)
15624     {
15625       warning (_("Nothing to save."));
15626       return;
15627     }
15628
15629   filename = tilde_expand (filename);
15630   cleanup = make_cleanup (xfree, filename);
15631   fp = gdb_fopen (filename, "w");
15632   if (!fp)
15633     error (_("Unable to open file '%s' for saving (%s)"),
15634            filename, safe_strerror (errno));
15635   make_cleanup_ui_file_delete (fp);
15636
15637   if (extra_trace_bits)
15638     save_trace_state_variables (fp);
15639
15640   ALL_BREAKPOINTS (tp)
15641   {
15642     /* Skip internal and momentary breakpoints.  */
15643     if (!user_breakpoint_p (tp))
15644       continue;
15645
15646     /* If we have a filter, only save the breakpoints it accepts.  */
15647     if (filter && !filter (tp))
15648       continue;
15649
15650     tp->ops->print_recreate (tp, fp);
15651
15652     /* Note, we can't rely on tp->number for anything, as we can't
15653        assume the recreated breakpoint numbers will match.  Use $bpnum
15654        instead.  */
15655
15656     if (tp->cond_string)
15657       fprintf_unfiltered (fp, "  condition $bpnum %s\n", tp->cond_string);
15658
15659     if (tp->ignore_count)
15660       fprintf_unfiltered (fp, "  ignore $bpnum %d\n", tp->ignore_count);
15661
15662     if (tp->type != bp_dprintf && tp->commands)
15663       {
15664         volatile struct gdb_exception ex;       
15665
15666         fprintf_unfiltered (fp, "  commands\n");
15667         
15668         ui_out_redirect (current_uiout, fp);
15669         TRY_CATCH (ex, RETURN_MASK_ALL)
15670           {
15671             print_command_lines (current_uiout, tp->commands->commands, 2);
15672           }
15673         ui_out_redirect (current_uiout, NULL);
15674
15675         if (ex.reason < 0)
15676           throw_exception (ex);
15677
15678         fprintf_unfiltered (fp, "  end\n");
15679       }
15680
15681     if (tp->enable_state == bp_disabled)
15682       fprintf_unfiltered (fp, "disable\n");
15683
15684     /* If this is a multi-location breakpoint, check if the locations
15685        should be individually disabled.  Watchpoint locations are
15686        special, and not user visible.  */
15687     if (!is_watchpoint (tp) && tp->loc && tp->loc->next)
15688       {
15689         struct bp_location *loc;
15690         int n = 1;
15691
15692         for (loc = tp->loc; loc != NULL; loc = loc->next, n++)
15693           if (!loc->enabled)
15694             fprintf_unfiltered (fp, "disable $bpnum.%d\n", n);
15695       }
15696   }
15697
15698   if (extra_trace_bits && *default_collect)
15699     fprintf_unfiltered (fp, "set default-collect %s\n", default_collect);
15700
15701   if (from_tty)
15702     printf_filtered (_("Saved to file '%s'.\n"), filename);
15703   do_cleanups (cleanup);
15704 }
15705
15706 /* The `save breakpoints' command.  */
15707
15708 static void
15709 save_breakpoints_command (char *args, int from_tty)
15710 {
15711   save_breakpoints (args, from_tty, NULL);
15712 }
15713
15714 /* The `save tracepoints' command.  */
15715
15716 static void
15717 save_tracepoints_command (char *args, int from_tty)
15718 {
15719   save_breakpoints (args, from_tty, is_tracepoint);
15720 }
15721
15722 /* Create a vector of all tracepoints.  */
15723
15724 VEC(breakpoint_p) *
15725 all_tracepoints (void)
15726 {
15727   VEC(breakpoint_p) *tp_vec = 0;
15728   struct breakpoint *tp;
15729
15730   ALL_TRACEPOINTS (tp)
15731   {
15732     VEC_safe_push (breakpoint_p, tp_vec, tp);
15733   }
15734
15735   return tp_vec;
15736 }
15737
15738 \f
15739 /* This help string is used for the break, hbreak, tbreak and thbreak
15740    commands.  It is defined as a macro to prevent duplication.
15741    COMMAND should be a string constant containing the name of the
15742    command.  */
15743 #define BREAK_ARGS_HELP(command) \
15744 command" [PROBE_MODIFIER] [LOCATION] [thread THREADNUM] [if CONDITION]\n\
15745 PROBE_MODIFIER shall be present if the command is to be placed in a\n\
15746 probe point.  Accepted values are `-probe' (for a generic, automatically\n\
15747 guessed probe type) or `-probe-stap' (for a SystemTap probe).\n\
15748 LOCATION may be a line number, function name, or \"*\" and an address.\n\
15749 If a line number is specified, break at start of code for that line.\n\
15750 If a function is specified, break at start of code for that function.\n\
15751 If an address is specified, break at that exact address.\n\
15752 With no LOCATION, uses current execution address of the selected\n\
15753 stack frame.  This is useful for breaking on return to a stack frame.\n\
15754 \n\
15755 THREADNUM is the number from \"info threads\".\n\
15756 CONDITION is a boolean expression.\n\
15757 \n\
15758 Multiple breakpoints at one place are permitted, and useful if their\n\
15759 conditions are different.\n\
15760 \n\
15761 Do \"help breakpoints\" for info on other commands dealing with breakpoints."
15762
15763 /* List of subcommands for "catch".  */
15764 static struct cmd_list_element *catch_cmdlist;
15765
15766 /* List of subcommands for "tcatch".  */
15767 static struct cmd_list_element *tcatch_cmdlist;
15768
15769 void
15770 add_catch_command (char *name, char *docstring,
15771                    void (*sfunc) (char *args, int from_tty,
15772                                   struct cmd_list_element *command),
15773                    completer_ftype *completer,
15774                    void *user_data_catch,
15775                    void *user_data_tcatch)
15776 {
15777   struct cmd_list_element *command;
15778
15779   command = add_cmd (name, class_breakpoint, NULL, docstring,
15780                      &catch_cmdlist);
15781   set_cmd_sfunc (command, sfunc);
15782   set_cmd_context (command, user_data_catch);
15783   set_cmd_completer (command, completer);
15784
15785   command = add_cmd (name, class_breakpoint, NULL, docstring,
15786                      &tcatch_cmdlist);
15787   set_cmd_sfunc (command, sfunc);
15788   set_cmd_context (command, user_data_tcatch);
15789   set_cmd_completer (command, completer);
15790 }
15791
15792 static void
15793 clear_syscall_counts (struct inferior *inf)
15794 {
15795   struct catch_syscall_inferior_data *inf_data
15796     = get_catch_syscall_inferior_data (inf);
15797
15798   inf_data->total_syscalls_count = 0;
15799   inf_data->any_syscall_count = 0;
15800   VEC_free (int, inf_data->syscalls_counts);
15801 }
15802
15803 static void
15804 save_command (char *arg, int from_tty)
15805 {
15806   printf_unfiltered (_("\"save\" must be followed by "
15807                        "the name of a save subcommand.\n"));
15808   help_list (save_cmdlist, "save ", -1, gdb_stdout);
15809 }
15810
15811 struct breakpoint *
15812 iterate_over_breakpoints (int (*callback) (struct breakpoint *, void *),
15813                           void *data)
15814 {
15815   struct breakpoint *b, *b_tmp;
15816
15817   ALL_BREAKPOINTS_SAFE (b, b_tmp)
15818     {
15819       if ((*callback) (b, data))
15820         return b;
15821     }
15822
15823   return NULL;
15824 }
15825
15826 /* Zero if any of the breakpoint's locations could be a location where
15827    functions have been inlined, nonzero otherwise.  */
15828
15829 static int
15830 is_non_inline_function (struct breakpoint *b)
15831 {
15832   /* The shared library event breakpoint is set on the address of a
15833      non-inline function.  */
15834   if (b->type == bp_shlib_event)
15835     return 1;
15836
15837   return 0;
15838 }
15839
15840 /* Nonzero if the specified PC cannot be a location where functions
15841    have been inlined.  */
15842
15843 int
15844 pc_at_non_inline_function (struct address_space *aspace, CORE_ADDR pc,
15845                            const struct target_waitstatus *ws)
15846 {
15847   struct breakpoint *b;
15848   struct bp_location *bl;
15849
15850   ALL_BREAKPOINTS (b)
15851     {
15852       if (!is_non_inline_function (b))
15853         continue;
15854
15855       for (bl = b->loc; bl != NULL; bl = bl->next)
15856         {
15857           if (!bl->shlib_disabled
15858               && bpstat_check_location (bl, aspace, pc, ws))
15859             return 1;
15860         }
15861     }
15862
15863   return 0;
15864 }
15865
15866 /* Remove any references to OBJFILE which is going to be freed.  */
15867
15868 void
15869 breakpoint_free_objfile (struct objfile *objfile)
15870 {
15871   struct bp_location **locp, *loc;
15872
15873   ALL_BP_LOCATIONS (loc, locp)
15874     if (loc->symtab != NULL && loc->symtab->objfile == objfile)
15875       loc->symtab = NULL;
15876 }
15877
15878 void
15879 initialize_breakpoint_ops (void)
15880 {
15881   static int initialized = 0;
15882
15883   struct breakpoint_ops *ops;
15884
15885   if (initialized)
15886     return;
15887   initialized = 1;
15888
15889   /* The breakpoint_ops structure to be inherit by all kinds of
15890      breakpoints (real breakpoints, i.e., user "break" breakpoints,
15891      internal and momentary breakpoints, etc.).  */
15892   ops = &bkpt_base_breakpoint_ops;
15893   *ops = base_breakpoint_ops;
15894   ops->re_set = bkpt_re_set;
15895   ops->insert_location = bkpt_insert_location;
15896   ops->remove_location = bkpt_remove_location;
15897   ops->breakpoint_hit = bkpt_breakpoint_hit;
15898   ops->create_sals_from_address = bkpt_create_sals_from_address;
15899   ops->create_breakpoints_sal = bkpt_create_breakpoints_sal;
15900   ops->decode_linespec = bkpt_decode_linespec;
15901
15902   /* The breakpoint_ops structure to be used in regular breakpoints.  */
15903   ops = &bkpt_breakpoint_ops;
15904   *ops = bkpt_base_breakpoint_ops;
15905   ops->re_set = bkpt_re_set;
15906   ops->resources_needed = bkpt_resources_needed;
15907   ops->print_it = bkpt_print_it;
15908   ops->print_mention = bkpt_print_mention;
15909   ops->print_recreate = bkpt_print_recreate;
15910
15911   /* Ranged breakpoints.  */
15912   ops = &ranged_breakpoint_ops;
15913   *ops = bkpt_breakpoint_ops;
15914   ops->breakpoint_hit = breakpoint_hit_ranged_breakpoint;
15915   ops->resources_needed = resources_needed_ranged_breakpoint;
15916   ops->print_it = print_it_ranged_breakpoint;
15917   ops->print_one = print_one_ranged_breakpoint;
15918   ops->print_one_detail = print_one_detail_ranged_breakpoint;
15919   ops->print_mention = print_mention_ranged_breakpoint;
15920   ops->print_recreate = print_recreate_ranged_breakpoint;
15921
15922   /* Internal breakpoints.  */
15923   ops = &internal_breakpoint_ops;
15924   *ops = bkpt_base_breakpoint_ops;
15925   ops->re_set = internal_bkpt_re_set;
15926   ops->check_status = internal_bkpt_check_status;
15927   ops->print_it = internal_bkpt_print_it;
15928   ops->print_mention = internal_bkpt_print_mention;
15929
15930   /* Momentary breakpoints.  */
15931   ops = &momentary_breakpoint_ops;
15932   *ops = bkpt_base_breakpoint_ops;
15933   ops->re_set = momentary_bkpt_re_set;
15934   ops->check_status = momentary_bkpt_check_status;
15935   ops->print_it = momentary_bkpt_print_it;
15936   ops->print_mention = momentary_bkpt_print_mention;
15937
15938   /* Momentary breakpoints for bp_longjmp and bp_exception.  */
15939   ops = &longjmp_breakpoint_ops;
15940   *ops = momentary_breakpoint_ops;
15941   ops->dtor = longjmp_bkpt_dtor;
15942
15943   /* Probe breakpoints.  */
15944   ops = &bkpt_probe_breakpoint_ops;
15945   *ops = bkpt_breakpoint_ops;
15946   ops->insert_location = bkpt_probe_insert_location;
15947   ops->remove_location = bkpt_probe_remove_location;
15948   ops->create_sals_from_address = bkpt_probe_create_sals_from_address;
15949   ops->decode_linespec = bkpt_probe_decode_linespec;
15950
15951   /* Watchpoints.  */
15952   ops = &watchpoint_breakpoint_ops;
15953   *ops = base_breakpoint_ops;
15954   ops->dtor = dtor_watchpoint;
15955   ops->re_set = re_set_watchpoint;
15956   ops->insert_location = insert_watchpoint;
15957   ops->remove_location = remove_watchpoint;
15958   ops->breakpoint_hit = breakpoint_hit_watchpoint;
15959   ops->check_status = check_status_watchpoint;
15960   ops->resources_needed = resources_needed_watchpoint;
15961   ops->works_in_software_mode = works_in_software_mode_watchpoint;
15962   ops->print_it = print_it_watchpoint;
15963   ops->print_mention = print_mention_watchpoint;
15964   ops->print_recreate = print_recreate_watchpoint;
15965   ops->explains_signal = explains_signal_watchpoint;
15966
15967   /* Masked watchpoints.  */
15968   ops = &masked_watchpoint_breakpoint_ops;
15969   *ops = watchpoint_breakpoint_ops;
15970   ops->insert_location = insert_masked_watchpoint;
15971   ops->remove_location = remove_masked_watchpoint;
15972   ops->resources_needed = resources_needed_masked_watchpoint;
15973   ops->works_in_software_mode = works_in_software_mode_masked_watchpoint;
15974   ops->print_it = print_it_masked_watchpoint;
15975   ops->print_one_detail = print_one_detail_masked_watchpoint;
15976   ops->print_mention = print_mention_masked_watchpoint;
15977   ops->print_recreate = print_recreate_masked_watchpoint;
15978
15979   /* Tracepoints.  */
15980   ops = &tracepoint_breakpoint_ops;
15981   *ops = base_breakpoint_ops;
15982   ops->re_set = tracepoint_re_set;
15983   ops->breakpoint_hit = tracepoint_breakpoint_hit;
15984   ops->print_one_detail = tracepoint_print_one_detail;
15985   ops->print_mention = tracepoint_print_mention;
15986   ops->print_recreate = tracepoint_print_recreate;
15987   ops->create_sals_from_address = tracepoint_create_sals_from_address;
15988   ops->create_breakpoints_sal = tracepoint_create_breakpoints_sal;
15989   ops->decode_linespec = tracepoint_decode_linespec;
15990
15991   /* Probe tracepoints.  */
15992   ops = &tracepoint_probe_breakpoint_ops;
15993   *ops = tracepoint_breakpoint_ops;
15994   ops->create_sals_from_address = tracepoint_probe_create_sals_from_address;
15995   ops->decode_linespec = tracepoint_probe_decode_linespec;
15996
15997   /* Static tracepoints with marker (`-m').  */
15998   ops = &strace_marker_breakpoint_ops;
15999   *ops = tracepoint_breakpoint_ops;
16000   ops->create_sals_from_address = strace_marker_create_sals_from_address;
16001   ops->create_breakpoints_sal = strace_marker_create_breakpoints_sal;
16002   ops->decode_linespec = strace_marker_decode_linespec;
16003
16004   /* Fork catchpoints.  */
16005   ops = &catch_fork_breakpoint_ops;
16006   *ops = base_breakpoint_ops;
16007   ops->insert_location = insert_catch_fork;
16008   ops->remove_location = remove_catch_fork;
16009   ops->breakpoint_hit = breakpoint_hit_catch_fork;
16010   ops->print_it = print_it_catch_fork;
16011   ops->print_one = print_one_catch_fork;
16012   ops->print_mention = print_mention_catch_fork;
16013   ops->print_recreate = print_recreate_catch_fork;
16014
16015   /* Vfork catchpoints.  */
16016   ops = &catch_vfork_breakpoint_ops;
16017   *ops = base_breakpoint_ops;
16018   ops->insert_location = insert_catch_vfork;
16019   ops->remove_location = remove_catch_vfork;
16020   ops->breakpoint_hit = breakpoint_hit_catch_vfork;
16021   ops->print_it = print_it_catch_vfork;
16022   ops->print_one = print_one_catch_vfork;
16023   ops->print_mention = print_mention_catch_vfork;
16024   ops->print_recreate = print_recreate_catch_vfork;
16025
16026   /* Exec catchpoints.  */
16027   ops = &catch_exec_breakpoint_ops;
16028   *ops = base_breakpoint_ops;
16029   ops->dtor = dtor_catch_exec;
16030   ops->insert_location = insert_catch_exec;
16031   ops->remove_location = remove_catch_exec;
16032   ops->breakpoint_hit = breakpoint_hit_catch_exec;
16033   ops->print_it = print_it_catch_exec;
16034   ops->print_one = print_one_catch_exec;
16035   ops->print_mention = print_mention_catch_exec;
16036   ops->print_recreate = print_recreate_catch_exec;
16037
16038   /* Syscall catchpoints.  */
16039   ops = &catch_syscall_breakpoint_ops;
16040   *ops = base_breakpoint_ops;
16041   ops->dtor = dtor_catch_syscall;
16042   ops->insert_location = insert_catch_syscall;
16043   ops->remove_location = remove_catch_syscall;
16044   ops->breakpoint_hit = breakpoint_hit_catch_syscall;
16045   ops->print_it = print_it_catch_syscall;
16046   ops->print_one = print_one_catch_syscall;
16047   ops->print_mention = print_mention_catch_syscall;
16048   ops->print_recreate = print_recreate_catch_syscall;
16049
16050   /* Solib-related catchpoints.  */
16051   ops = &catch_solib_breakpoint_ops;
16052   *ops = base_breakpoint_ops;
16053   ops->dtor = dtor_catch_solib;
16054   ops->insert_location = insert_catch_solib;
16055   ops->remove_location = remove_catch_solib;
16056   ops->breakpoint_hit = breakpoint_hit_catch_solib;
16057   ops->check_status = check_status_catch_solib;
16058   ops->print_it = print_it_catch_solib;
16059   ops->print_one = print_one_catch_solib;
16060   ops->print_mention = print_mention_catch_solib;
16061   ops->print_recreate = print_recreate_catch_solib;
16062
16063   ops = &dprintf_breakpoint_ops;
16064   *ops = bkpt_base_breakpoint_ops;
16065   ops->re_set = dprintf_re_set;
16066   ops->resources_needed = bkpt_resources_needed;
16067   ops->print_it = bkpt_print_it;
16068   ops->print_mention = bkpt_print_mention;
16069   ops->print_recreate = dprintf_print_recreate;
16070   ops->after_condition_true = dprintf_after_condition_true;
16071 }
16072
16073 /* Chain containing all defined "enable breakpoint" subcommands.  */
16074
16075 static struct cmd_list_element *enablebreaklist = NULL;
16076
16077 void
16078 _initialize_breakpoint (void)
16079 {
16080   struct cmd_list_element *c;
16081
16082   initialize_breakpoint_ops ();
16083
16084   observer_attach_solib_unloaded (disable_breakpoints_in_unloaded_shlib);
16085   observer_attach_free_objfile (disable_breakpoints_in_freed_objfile);
16086   observer_attach_inferior_exit (clear_syscall_counts);
16087   observer_attach_memory_changed (invalidate_bp_value_on_memory_change);
16088
16089   breakpoint_objfile_key
16090     = register_objfile_data_with_cleanup (NULL, free_breakpoint_probes);
16091
16092   catch_syscall_inferior_data
16093     = register_inferior_data_with_cleanup (NULL,
16094                                            catch_syscall_inferior_data_cleanup);
16095
16096   breakpoint_chain = 0;
16097   /* Don't bother to call set_breakpoint_count.  $bpnum isn't useful
16098      before a breakpoint is set.  */
16099   breakpoint_count = 0;
16100
16101   tracepoint_count = 0;
16102
16103   add_com ("ignore", class_breakpoint, ignore_command, _("\
16104 Set ignore-count of breakpoint number N to COUNT.\n\
16105 Usage is `ignore N COUNT'."));
16106   if (xdb_commands)
16107     add_com_alias ("bc", "ignore", class_breakpoint, 1);
16108
16109   add_com ("commands", class_breakpoint, commands_command, _("\
16110 Set commands to be executed when a breakpoint is hit.\n\
16111 Give breakpoint number as argument after \"commands\".\n\
16112 With no argument, the targeted breakpoint is the last one set.\n\
16113 The commands themselves follow starting on the next line.\n\
16114 Type a line containing \"end\" to indicate the end of them.\n\
16115 Give \"silent\" as the first line to make the breakpoint silent;\n\
16116 then no output is printed when it is hit, except what the commands print."));
16117
16118   c = add_com ("condition", class_breakpoint, condition_command, _("\
16119 Specify breakpoint number N to break only if COND is true.\n\
16120 Usage is `condition N COND', where N is an integer and COND is an\n\
16121 expression to be evaluated whenever breakpoint N is reached."));
16122   set_cmd_completer (c, condition_completer);
16123
16124   c = add_com ("tbreak", class_breakpoint, tbreak_command, _("\
16125 Set a temporary breakpoint.\n\
16126 Like \"break\" except the breakpoint is only temporary,\n\
16127 so it will be deleted when hit.  Equivalent to \"break\" followed\n\
16128 by using \"enable delete\" on the breakpoint number.\n\
16129 \n"
16130 BREAK_ARGS_HELP ("tbreak")));
16131   set_cmd_completer (c, location_completer);
16132
16133   c = add_com ("hbreak", class_breakpoint, hbreak_command, _("\
16134 Set a hardware assisted breakpoint.\n\
16135 Like \"break\" except the breakpoint requires hardware support,\n\
16136 some target hardware may not have this support.\n\
16137 \n"
16138 BREAK_ARGS_HELP ("hbreak")));
16139   set_cmd_completer (c, location_completer);
16140
16141   c = add_com ("thbreak", class_breakpoint, thbreak_command, _("\
16142 Set a temporary hardware assisted breakpoint.\n\
16143 Like \"hbreak\" except the breakpoint is only temporary,\n\
16144 so it will be deleted when hit.\n\
16145 \n"
16146 BREAK_ARGS_HELP ("thbreak")));
16147   set_cmd_completer (c, location_completer);
16148
16149   add_prefix_cmd ("enable", class_breakpoint, enable_command, _("\
16150 Enable some breakpoints.\n\
16151 Give breakpoint numbers (separated by spaces) as arguments.\n\
16152 With no subcommand, breakpoints are enabled until you command otherwise.\n\
16153 This is used to cancel the effect of the \"disable\" command.\n\
16154 With a subcommand you can enable temporarily."),
16155                   &enablelist, "enable ", 1, &cmdlist);
16156   if (xdb_commands)
16157     add_com ("ab", class_breakpoint, enable_command, _("\
16158 Enable some breakpoints.\n\
16159 Give breakpoint numbers (separated by spaces) as arguments.\n\
16160 With no subcommand, breakpoints are enabled until you command otherwise.\n\
16161 This is used to cancel the effect of the \"disable\" command.\n\
16162 With a subcommand you can enable temporarily."));
16163
16164   add_com_alias ("en", "enable", class_breakpoint, 1);
16165
16166   add_prefix_cmd ("breakpoints", class_breakpoint, enable_command, _("\
16167 Enable some breakpoints.\n\
16168 Give breakpoint numbers (separated by spaces) as arguments.\n\
16169 This is used to cancel the effect of the \"disable\" command.\n\
16170 May be abbreviated to simply \"enable\".\n"),
16171                    &enablebreaklist, "enable breakpoints ", 1, &enablelist);
16172
16173   add_cmd ("once", no_class, enable_once_command, _("\
16174 Enable breakpoints for one hit.  Give breakpoint numbers.\n\
16175 If a breakpoint is hit while enabled in this fashion, it becomes disabled."),
16176            &enablebreaklist);
16177
16178   add_cmd ("delete", no_class, enable_delete_command, _("\
16179 Enable breakpoints and delete when hit.  Give breakpoint numbers.\n\
16180 If a breakpoint is hit while enabled in this fashion, it is deleted."),
16181            &enablebreaklist);
16182
16183   add_cmd ("count", no_class, enable_count_command, _("\
16184 Enable breakpoints for COUNT hits.  Give count and then breakpoint numbers.\n\
16185 If a breakpoint is hit while enabled in this fashion,\n\
16186 the count is decremented; when it reaches zero, the breakpoint is disabled."),
16187            &enablebreaklist);
16188
16189   add_cmd ("delete", no_class, enable_delete_command, _("\
16190 Enable breakpoints and delete when hit.  Give breakpoint numbers.\n\
16191 If a breakpoint is hit while enabled in this fashion, it is deleted."),
16192            &enablelist);
16193
16194   add_cmd ("once", no_class, enable_once_command, _("\
16195 Enable breakpoints for one hit.  Give breakpoint numbers.\n\
16196 If a breakpoint is hit while enabled in this fashion, it becomes disabled."),
16197            &enablelist);
16198
16199   add_cmd ("count", no_class, enable_count_command, _("\
16200 Enable breakpoints for COUNT hits.  Give count and then breakpoint numbers.\n\
16201 If a breakpoint is hit while enabled in this fashion,\n\
16202 the count is decremented; when it reaches zero, the breakpoint is disabled."),
16203            &enablelist);
16204
16205   add_prefix_cmd ("disable", class_breakpoint, disable_command, _("\
16206 Disable some breakpoints.\n\
16207 Arguments are breakpoint numbers with spaces in between.\n\
16208 To disable all breakpoints, give no argument.\n\
16209 A disabled breakpoint is not forgotten, but has no effect until re-enabled."),
16210                   &disablelist, "disable ", 1, &cmdlist);
16211   add_com_alias ("dis", "disable", class_breakpoint, 1);
16212   add_com_alias ("disa", "disable", class_breakpoint, 1);
16213   if (xdb_commands)
16214     add_com ("sb", class_breakpoint, disable_command, _("\
16215 Disable some breakpoints.\n\
16216 Arguments are breakpoint numbers with spaces in between.\n\
16217 To disable all breakpoints, give no argument.\n\
16218 A disabled breakpoint is not forgotten, but has no effect until re-enabled."));
16219
16220   add_cmd ("breakpoints", class_alias, disable_command, _("\
16221 Disable some breakpoints.\n\
16222 Arguments are breakpoint numbers with spaces in between.\n\
16223 To disable all breakpoints, give no argument.\n\
16224 A disabled breakpoint is not forgotten, but has no effect until re-enabled.\n\
16225 This command may be abbreviated \"disable\"."),
16226            &disablelist);
16227
16228   add_prefix_cmd ("delete", class_breakpoint, delete_command, _("\
16229 Delete some breakpoints or auto-display expressions.\n\
16230 Arguments are breakpoint numbers with spaces in between.\n\
16231 To delete all breakpoints, give no argument.\n\
16232 \n\
16233 Also a prefix command for deletion of other GDB objects.\n\
16234 The \"unset\" command is also an alias for \"delete\"."),
16235                   &deletelist, "delete ", 1, &cmdlist);
16236   add_com_alias ("d", "delete", class_breakpoint, 1);
16237   add_com_alias ("del", "delete", class_breakpoint, 1);
16238   if (xdb_commands)
16239     add_com ("db", class_breakpoint, delete_command, _("\
16240 Delete some breakpoints.\n\
16241 Arguments are breakpoint numbers with spaces in between.\n\
16242 To delete all breakpoints, give no argument.\n"));
16243
16244   add_cmd ("breakpoints", class_alias, delete_command, _("\
16245 Delete some breakpoints or auto-display expressions.\n\
16246 Arguments are breakpoint numbers with spaces in between.\n\
16247 To delete all breakpoints, give no argument.\n\
16248 This command may be abbreviated \"delete\"."),
16249            &deletelist);
16250
16251   add_com ("clear", class_breakpoint, clear_command, _("\
16252 Clear breakpoint at specified line or function.\n\
16253 Argument may be line number, function name, or \"*\" and an address.\n\
16254 If line number is specified, all breakpoints in that line are cleared.\n\
16255 If function is specified, breakpoints at beginning of function are cleared.\n\
16256 If an address is specified, breakpoints at that address are cleared.\n\
16257 \n\
16258 With no argument, clears all breakpoints in the line that the selected frame\n\
16259 is executing in.\n\
16260 \n\
16261 See also the \"delete\" command which clears breakpoints by number."));
16262   add_com_alias ("cl", "clear", class_breakpoint, 1);
16263
16264   c = add_com ("break", class_breakpoint, break_command, _("\
16265 Set breakpoint at specified line or function.\n"
16266 BREAK_ARGS_HELP ("break")));
16267   set_cmd_completer (c, location_completer);
16268
16269   add_com_alias ("b", "break", class_run, 1);
16270   add_com_alias ("br", "break", class_run, 1);
16271   add_com_alias ("bre", "break", class_run, 1);
16272   add_com_alias ("brea", "break", class_run, 1);
16273
16274   if (xdb_commands)
16275    add_com_alias ("ba", "break", class_breakpoint, 1);
16276
16277   if (dbx_commands)
16278     {
16279       add_abbrev_prefix_cmd ("stop", class_breakpoint, stop_command, _("\
16280 Break in function/address or break at a line in the current file."),
16281                              &stoplist, "stop ", 1, &cmdlist);
16282       add_cmd ("in", class_breakpoint, stopin_command,
16283                _("Break in function or address."), &stoplist);
16284       add_cmd ("at", class_breakpoint, stopat_command,
16285                _("Break at a line in the current file."), &stoplist);
16286       add_com ("status", class_info, breakpoints_info, _("\
16287 Status of user-settable breakpoints, or breakpoint number NUMBER.\n\
16288 The \"Type\" column indicates one of:\n\
16289 \tbreakpoint     - normal breakpoint\n\
16290 \twatchpoint     - watchpoint\n\
16291 The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
16292 the disposition of the breakpoint after it gets hit.  \"dis\" means that the\n\
16293 breakpoint will be disabled.  The \"Address\" and \"What\" columns indicate the\n\
16294 address and file/line number respectively.\n\
16295 \n\
16296 Convenience variable \"$_\" and default examine address for \"x\"\n\
16297 are set to the address of the last breakpoint listed unless the command\n\
16298 is prefixed with \"server \".\n\n\
16299 Convenience variable \"$bpnum\" contains the number of the last\n\
16300 breakpoint set."));
16301     }
16302
16303   add_info ("breakpoints", breakpoints_info, _("\
16304 Status of specified breakpoints (all user-settable breakpoints if no argument).\n\
16305 The \"Type\" column indicates one of:\n\
16306 \tbreakpoint     - normal breakpoint\n\
16307 \twatchpoint     - watchpoint\n\
16308 The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
16309 the disposition of the breakpoint after it gets hit.  \"dis\" means that the\n\
16310 breakpoint will be disabled.  The \"Address\" and \"What\" columns indicate the\n\
16311 address and file/line number respectively.\n\
16312 \n\
16313 Convenience variable \"$_\" and default examine address for \"x\"\n\
16314 are set to the address of the last breakpoint listed unless the command\n\
16315 is prefixed with \"server \".\n\n\
16316 Convenience variable \"$bpnum\" contains the number of the last\n\
16317 breakpoint set."));
16318
16319   add_info_alias ("b", "breakpoints", 1);
16320
16321   if (xdb_commands)
16322     add_com ("lb", class_breakpoint, breakpoints_info, _("\
16323 Status of user-settable breakpoints, or breakpoint number NUMBER.\n\
16324 The \"Type\" column indicates one of:\n\
16325 \tbreakpoint     - normal breakpoint\n\
16326 \twatchpoint     - watchpoint\n\
16327 The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
16328 the disposition of the breakpoint after it gets hit.  \"dis\" means that the\n\
16329 breakpoint will be disabled.  The \"Address\" and \"What\" columns indicate the\n\
16330 address and file/line number respectively.\n\
16331 \n\
16332 Convenience variable \"$_\" and default examine address for \"x\"\n\
16333 are set to the address of the last breakpoint listed unless the command\n\
16334 is prefixed with \"server \".\n\n\
16335 Convenience variable \"$bpnum\" contains the number of the last\n\
16336 breakpoint set."));
16337
16338   add_cmd ("breakpoints", class_maintenance, maintenance_info_breakpoints, _("\
16339 Status of all breakpoints, or breakpoint number NUMBER.\n\
16340 The \"Type\" column indicates one of:\n\
16341 \tbreakpoint     - normal breakpoint\n\
16342 \twatchpoint     - watchpoint\n\
16343 \tlongjmp        - internal breakpoint used to step through longjmp()\n\
16344 \tlongjmp resume - internal breakpoint at the target of longjmp()\n\
16345 \tuntil          - internal breakpoint used by the \"until\" command\n\
16346 \tfinish         - internal breakpoint used by the \"finish\" command\n\
16347 The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
16348 the disposition of the breakpoint after it gets hit.  \"dis\" means that the\n\
16349 breakpoint will be disabled.  The \"Address\" and \"What\" columns indicate the\n\
16350 address and file/line number respectively.\n\
16351 \n\
16352 Convenience variable \"$_\" and default examine address for \"x\"\n\
16353 are set to the address of the last breakpoint listed unless the command\n\
16354 is prefixed with \"server \".\n\n\
16355 Convenience variable \"$bpnum\" contains the number of the last\n\
16356 breakpoint set."),
16357            &maintenanceinfolist);
16358
16359   add_prefix_cmd ("catch", class_breakpoint, catch_command, _("\
16360 Set catchpoints to catch events."),
16361                   &catch_cmdlist, "catch ",
16362                   0/*allow-unknown*/, &cmdlist);
16363
16364   add_prefix_cmd ("tcatch", class_breakpoint, tcatch_command, _("\
16365 Set temporary catchpoints to catch events."),
16366                   &tcatch_cmdlist, "tcatch ",
16367                   0/*allow-unknown*/, &cmdlist);
16368
16369   add_catch_command ("fork", _("Catch calls to fork."),
16370                      catch_fork_command_1,
16371                      NULL,
16372                      (void *) (uintptr_t) catch_fork_permanent,
16373                      (void *) (uintptr_t) catch_fork_temporary);
16374   add_catch_command ("vfork", _("Catch calls to vfork."),
16375                      catch_fork_command_1,
16376                      NULL,
16377                      (void *) (uintptr_t) catch_vfork_permanent,
16378                      (void *) (uintptr_t) catch_vfork_temporary);
16379   add_catch_command ("exec", _("Catch calls to exec."),
16380                      catch_exec_command_1,
16381                      NULL,
16382                      CATCH_PERMANENT,
16383                      CATCH_TEMPORARY);
16384   add_catch_command ("load", _("Catch loads of shared libraries.\n\
16385 Usage: catch load [REGEX]\n\
16386 If REGEX is given, only stop for libraries matching the regular expression."),
16387                      catch_load_command_1,
16388                      NULL,
16389                      CATCH_PERMANENT,
16390                      CATCH_TEMPORARY);
16391   add_catch_command ("unload", _("Catch unloads of shared libraries.\n\
16392 Usage: catch unload [REGEX]\n\
16393 If REGEX is given, only stop for libraries matching the regular expression."),
16394                      catch_unload_command_1,
16395                      NULL,
16396                      CATCH_PERMANENT,
16397                      CATCH_TEMPORARY);
16398   add_catch_command ("syscall", _("\
16399 Catch system calls by their names and/or numbers.\n\
16400 Arguments say which system calls to catch.  If no arguments\n\
16401 are given, every system call will be caught.\n\
16402 Arguments, if given, should be one or more system call names\n\
16403 (if your system supports that), or system call numbers."),
16404                      catch_syscall_command_1,
16405                      catch_syscall_completer,
16406                      CATCH_PERMANENT,
16407                      CATCH_TEMPORARY);
16408
16409   c = add_com ("watch", class_breakpoint, watch_command, _("\
16410 Set a watchpoint for an expression.\n\
16411 Usage: watch [-l|-location] EXPRESSION\n\
16412 A watchpoint stops execution of your program whenever the value of\n\
16413 an expression changes.\n\
16414 If -l or -location is given, this evaluates EXPRESSION and watches\n\
16415 the memory to which it refers."));
16416   set_cmd_completer (c, expression_completer);
16417
16418   c = add_com ("rwatch", class_breakpoint, rwatch_command, _("\
16419 Set a read watchpoint for an expression.\n\
16420 Usage: rwatch [-l|-location] EXPRESSION\n\
16421 A watchpoint stops execution of your program whenever the value of\n\
16422 an expression is read.\n\
16423 If -l or -location is given, this evaluates EXPRESSION and watches\n\
16424 the memory to which it refers."));
16425   set_cmd_completer (c, expression_completer);
16426
16427   c = add_com ("awatch", class_breakpoint, awatch_command, _("\
16428 Set a watchpoint for an expression.\n\
16429 Usage: awatch [-l|-location] EXPRESSION\n\
16430 A watchpoint stops execution of your program whenever the value of\n\
16431 an expression is either read or written.\n\
16432 If -l or -location is given, this evaluates EXPRESSION and watches\n\
16433 the memory to which it refers."));
16434   set_cmd_completer (c, expression_completer);
16435
16436   add_info ("watchpoints", watchpoints_info, _("\
16437 Status of specified watchpoints (all watchpoints if no argument)."));
16438
16439   /* XXX: cagney/2005-02-23: This should be a boolean, and should
16440      respond to changes - contrary to the description.  */
16441   add_setshow_zinteger_cmd ("can-use-hw-watchpoints", class_support,
16442                             &can_use_hw_watchpoints, _("\
16443 Set debugger's willingness to use watchpoint hardware."), _("\
16444 Show debugger's willingness to use watchpoint hardware."), _("\
16445 If zero, gdb will not use hardware for new watchpoints, even if\n\
16446 such is available.  (However, any hardware watchpoints that were\n\
16447 created before setting this to nonzero, will continue to use watchpoint\n\
16448 hardware.)"),
16449                             NULL,
16450                             show_can_use_hw_watchpoints,
16451                             &setlist, &showlist);
16452
16453   can_use_hw_watchpoints = 1;
16454
16455   /* Tracepoint manipulation commands.  */
16456
16457   c = add_com ("trace", class_breakpoint, trace_command, _("\
16458 Set a tracepoint at specified line or function.\n\
16459 \n"
16460 BREAK_ARGS_HELP ("trace") "\n\
16461 Do \"help tracepoints\" for info on other tracepoint commands."));
16462   set_cmd_completer (c, location_completer);
16463
16464   add_com_alias ("tp", "trace", class_alias, 0);
16465   add_com_alias ("tr", "trace", class_alias, 1);
16466   add_com_alias ("tra", "trace", class_alias, 1);
16467   add_com_alias ("trac", "trace", class_alias, 1);
16468
16469   c = add_com ("ftrace", class_breakpoint, ftrace_command, _("\
16470 Set a fast tracepoint at specified line or function.\n\
16471 \n"
16472 BREAK_ARGS_HELP ("ftrace") "\n\
16473 Do \"help tracepoints\" for info on other tracepoint commands."));
16474   set_cmd_completer (c, location_completer);
16475
16476   c = add_com ("strace", class_breakpoint, strace_command, _("\
16477 Set a static tracepoint at specified line, function or marker.\n\
16478 \n\
16479 strace [LOCATION] [if CONDITION]\n\
16480 LOCATION may be a line number, function name, \"*\" and an address,\n\
16481 or -m MARKER_ID.\n\
16482 If a line number is specified, probe the marker at start of code\n\
16483 for that line.  If a function is specified, probe the marker at start\n\
16484 of code for that function.  If an address is specified, probe the marker\n\
16485 at that exact address.  If a marker id is specified, probe the marker\n\
16486 with that name.  With no LOCATION, uses current execution address of\n\
16487 the selected stack frame.\n\
16488 Static tracepoints accept an extra collect action -- ``collect $_sdata''.\n\
16489 This collects arbitrary user data passed in the probe point call to the\n\
16490 tracing library.  You can inspect it when analyzing the trace buffer,\n\
16491 by printing the $_sdata variable like any other convenience variable.\n\
16492 \n\
16493 CONDITION is a boolean expression.\n\
16494 \n\
16495 Multiple tracepoints at one place are permitted, and useful if their\n\
16496 conditions are different.\n\
16497 \n\
16498 Do \"help breakpoints\" for info on other commands dealing with breakpoints.\n\
16499 Do \"help tracepoints\" for info on other tracepoint commands."));
16500   set_cmd_completer (c, location_completer);
16501
16502   add_info ("tracepoints", tracepoints_info, _("\
16503 Status of specified tracepoints (all tracepoints if no argument).\n\
16504 Convenience variable \"$tpnum\" contains the number of the\n\
16505 last tracepoint set."));
16506
16507   add_info_alias ("tp", "tracepoints", 1);
16508
16509   add_cmd ("tracepoints", class_trace, delete_trace_command, _("\
16510 Delete specified tracepoints.\n\
16511 Arguments are tracepoint numbers, separated by spaces.\n\
16512 No argument means delete all tracepoints."),
16513            &deletelist);
16514   add_alias_cmd ("tr", "tracepoints", class_trace, 1, &deletelist);
16515
16516   c = add_cmd ("tracepoints", class_trace, disable_trace_command, _("\
16517 Disable specified tracepoints.\n\
16518 Arguments are tracepoint numbers, separated by spaces.\n\
16519 No argument means disable all tracepoints."),
16520            &disablelist);
16521   deprecate_cmd (c, "disable");
16522
16523   c = add_cmd ("tracepoints", class_trace, enable_trace_command, _("\
16524 Enable specified tracepoints.\n\
16525 Arguments are tracepoint numbers, separated by spaces.\n\
16526 No argument means enable all tracepoints."),
16527            &enablelist);
16528   deprecate_cmd (c, "enable");
16529
16530   add_com ("passcount", class_trace, trace_pass_command, _("\
16531 Set the passcount for a tracepoint.\n\
16532 The trace will end when the tracepoint has been passed 'count' times.\n\
16533 Usage: passcount COUNT TPNUM, where TPNUM may also be \"all\";\n\
16534 if TPNUM is omitted, passcount refers to the last tracepoint defined."));
16535
16536   add_prefix_cmd ("save", class_breakpoint, save_command,
16537                   _("Save breakpoint definitions as a script."),
16538                   &save_cmdlist, "save ",
16539                   0/*allow-unknown*/, &cmdlist);
16540
16541   c = add_cmd ("breakpoints", class_breakpoint, save_breakpoints_command, _("\
16542 Save current breakpoint definitions as a script.\n\
16543 This includes all types of breakpoints (breakpoints, watchpoints,\n\
16544 catchpoints, tracepoints).  Use the 'source' command in another debug\n\
16545 session to restore them."),
16546                &save_cmdlist);
16547   set_cmd_completer (c, filename_completer);
16548
16549   c = add_cmd ("tracepoints", class_trace, save_tracepoints_command, _("\
16550 Save current tracepoint definitions as a script.\n\
16551 Use the 'source' command in another debug session to restore them."),
16552                &save_cmdlist);
16553   set_cmd_completer (c, filename_completer);
16554
16555   c = add_com_alias ("save-tracepoints", "save tracepoints", class_trace, 0);
16556   deprecate_cmd (c, "save tracepoints");
16557
16558   add_prefix_cmd ("breakpoint", class_maintenance, set_breakpoint_cmd, _("\
16559 Breakpoint specific settings\n\
16560 Configure various breakpoint-specific variables such as\n\
16561 pending breakpoint behavior"),
16562                   &breakpoint_set_cmdlist, "set breakpoint ",
16563                   0/*allow-unknown*/, &setlist);
16564   add_prefix_cmd ("breakpoint", class_maintenance, show_breakpoint_cmd, _("\
16565 Breakpoint specific settings\n\
16566 Configure various breakpoint-specific variables such as\n\
16567 pending breakpoint behavior"),
16568                   &breakpoint_show_cmdlist, "show breakpoint ",
16569                   0/*allow-unknown*/, &showlist);
16570
16571   add_setshow_auto_boolean_cmd ("pending", no_class,
16572                                 &pending_break_support, _("\
16573 Set debugger's behavior regarding pending breakpoints."), _("\
16574 Show debugger's behavior regarding pending breakpoints."), _("\
16575 If on, an unrecognized breakpoint location will cause gdb to create a\n\
16576 pending breakpoint.  If off, an unrecognized breakpoint location results in\n\
16577 an error.  If auto, an unrecognized breakpoint location results in a\n\
16578 user-query to see if a pending breakpoint should be created."),
16579                                 NULL,
16580                                 show_pending_break_support,
16581                                 &breakpoint_set_cmdlist,
16582                                 &breakpoint_show_cmdlist);
16583
16584   pending_break_support = AUTO_BOOLEAN_AUTO;
16585
16586   add_setshow_boolean_cmd ("auto-hw", no_class,
16587                            &automatic_hardware_breakpoints, _("\
16588 Set automatic usage of hardware breakpoints."), _("\
16589 Show automatic usage of hardware breakpoints."), _("\
16590 If set, the debugger will automatically use hardware breakpoints for\n\
16591 breakpoints set with \"break\" but falling in read-only memory.  If not set,\n\
16592 a warning will be emitted for such breakpoints."),
16593                            NULL,
16594                            show_automatic_hardware_breakpoints,
16595                            &breakpoint_set_cmdlist,
16596                            &breakpoint_show_cmdlist);
16597
16598   add_setshow_auto_boolean_cmd ("always-inserted", class_support,
16599                                 &always_inserted_mode, _("\
16600 Set mode for inserting breakpoints."), _("\
16601 Show mode for inserting breakpoints."), _("\
16602 When this mode is off, breakpoints are inserted in inferior when it is\n\
16603 resumed, and removed when execution stops.  When this mode is on,\n\
16604 breakpoints are inserted immediately and removed only when the user\n\
16605 deletes the breakpoint.  When this mode is auto (which is the default),\n\
16606 the behaviour depends on the non-stop setting (see help set non-stop).\n\
16607 In this case, if gdb is controlling the inferior in non-stop mode, gdb\n\
16608 behaves as if always-inserted mode is on; if gdb is controlling the\n\
16609 inferior in all-stop mode, gdb behaves as if always-inserted mode is off."),
16610                                 NULL,
16611                                 &show_always_inserted_mode,
16612                                 &breakpoint_set_cmdlist,
16613                                 &breakpoint_show_cmdlist);
16614
16615   add_setshow_enum_cmd ("condition-evaluation", class_breakpoint,
16616                         condition_evaluation_enums,
16617                         &condition_evaluation_mode_1, _("\
16618 Set mode of breakpoint condition evaluation."), _("\
16619 Show mode of breakpoint condition evaluation."), _("\
16620 When this is set to \"host\", breakpoint conditions will be\n\
16621 evaluated on the host's side by GDB.  When it is set to \"target\",\n\
16622 breakpoint conditions will be downloaded to the target (if the target\n\
16623 supports such feature) and conditions will be evaluated on the target's side.\n\
16624 If this is set to \"auto\" (default), this will be automatically set to\n\
16625 \"target\" if it supports condition evaluation, otherwise it will\n\
16626 be set to \"gdb\""),
16627                            &set_condition_evaluation_mode,
16628                            &show_condition_evaluation_mode,
16629                            &breakpoint_set_cmdlist,
16630                            &breakpoint_show_cmdlist);
16631
16632   add_com ("break-range", class_breakpoint, break_range_command, _("\
16633 Set a breakpoint for an address range.\n\
16634 break-range START-LOCATION, END-LOCATION\n\
16635 where START-LOCATION and END-LOCATION can be one of the following:\n\
16636   LINENUM, for that line in the current file,\n\
16637   FILE:LINENUM, for that line in that file,\n\
16638   +OFFSET, for that number of lines after the current line\n\
16639            or the start of the range\n\
16640   FUNCTION, for the first line in that function,\n\
16641   FILE:FUNCTION, to distinguish among like-named static functions.\n\
16642   *ADDRESS, for the instruction at that address.\n\
16643 \n\
16644 The breakpoint will stop execution of the inferior whenever it executes\n\
16645 an instruction at any address within the [START-LOCATION, END-LOCATION]\n\
16646 range (including START-LOCATION and END-LOCATION)."));
16647
16648   c = add_com ("dprintf", class_breakpoint, dprintf_command, _("\
16649 Set a dynamic printf at specified line or function.\n\
16650 dprintf location,format string,arg1,arg2,...\n\
16651 location may be a line number, function name, or \"*\" and an address.\n\
16652 If a line number is specified, break at start of code for that line.\n\
16653 If a function is specified, break at start of code for that function."));
16654   set_cmd_completer (c, location_completer);
16655
16656   add_setshow_enum_cmd ("dprintf-style", class_support,
16657                         dprintf_style_enums, &dprintf_style, _("\
16658 Set the style of usage for dynamic printf."), _("\
16659 Show the style of usage for dynamic printf."), _("\
16660 This setting chooses how GDB will do a dynamic printf.\n\
16661 If the value is \"gdb\", then the printing is done by GDB to its own\n\
16662 console, as with the \"printf\" command.\n\
16663 If the value is \"call\", the print is done by calling a function in your\n\
16664 program; by default printf(), but you can choose a different function or\n\
16665 output stream by setting dprintf-function and dprintf-channel."),
16666                         update_dprintf_commands, NULL,
16667                         &setlist, &showlist);
16668
16669   dprintf_function = xstrdup ("printf");
16670   add_setshow_string_cmd ("dprintf-function", class_support,
16671                           &dprintf_function, _("\
16672 Set the function to use for dynamic printf"), _("\
16673 Show the function to use for dynamic printf"), NULL,
16674                           update_dprintf_commands, NULL,
16675                           &setlist, &showlist);
16676
16677   dprintf_channel = xstrdup ("");
16678   add_setshow_string_cmd ("dprintf-channel", class_support,
16679                           &dprintf_channel, _("\
16680 Set the channel to use for dynamic printf"), _("\
16681 Show the channel to use for dynamic printf"), NULL,
16682                           update_dprintf_commands, NULL,
16683                           &setlist, &showlist);
16684
16685   add_setshow_boolean_cmd ("disconnected-dprintf", no_class,
16686                            &disconnected_dprintf, _("\
16687 Set whether dprintf continues after GDB disconnects."), _("\
16688 Show whether dprintf continues after GDB disconnects."), _("\
16689 Use this to let dprintf commands continue to hit and produce output\n\
16690 even if GDB disconnects or detaches from the target."),
16691                            NULL,
16692                            NULL,
16693                            &setlist, &showlist);
16694
16695   add_com ("agent-printf", class_vars, agent_printf_command, _("\
16696 agent-printf \"printf format string\", arg1, arg2, arg3, ..., argn\n\
16697 (target agent only) This is useful for formatted output in user-defined commands."));
16698
16699   automatic_hardware_breakpoints = 1;
16700
16701   observer_attach_about_to_proceed (breakpoint_about_to_proceed);
16702   observer_attach_thread_exit (remove_threaded_breakpoints);
16703 }