PR c++/9065:
[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                                             struct linespec_sals *,
123                                             char *, char *, enum bptype,
124                                             enum bpdisp, int, int,
125                                             int,
126                                             const struct breakpoint_ops *,
127                                             int, int, int, unsigned);
128
129 static void decode_linespec_default (struct breakpoint *, char **,
130                                      struct symtabs_and_lines *);
131
132 static void clear_command (char *, int);
133
134 static void catch_command (char *, int);
135
136 static int can_use_hardware_watchpoint (struct value *);
137
138 static void break_command_1 (char *, int, int);
139
140 static void mention (struct breakpoint *);
141
142 static struct breakpoint *set_raw_breakpoint_without_location (struct gdbarch *,
143                                                                enum bptype,
144                                                                const struct breakpoint_ops *);
145 static struct bp_location *add_location_to_breakpoint (struct breakpoint *,
146                                                        const struct symtab_and_line *);
147
148 /* This function is used in gdbtk sources and thus can not be made
149    static.  */
150 struct breakpoint *set_raw_breakpoint (struct gdbarch *gdbarch,
151                                        struct symtab_and_line,
152                                        enum bptype,
153                                        const struct breakpoint_ops *);
154
155 static struct breakpoint *
156   momentary_breakpoint_from_master (struct breakpoint *orig,
157                                     enum bptype type,
158                                     const struct breakpoint_ops *ops);
159
160 static void breakpoint_adjustment_warning (CORE_ADDR, CORE_ADDR, int, int);
161
162 static CORE_ADDR adjust_breakpoint_address (struct gdbarch *gdbarch,
163                                             CORE_ADDR bpaddr,
164                                             enum bptype bptype);
165
166 static void describe_other_breakpoints (struct gdbarch *,
167                                         struct program_space *, CORE_ADDR,
168                                         struct obj_section *, int);
169
170 static int breakpoint_address_match (struct address_space *aspace1,
171                                      CORE_ADDR addr1,
172                                      struct address_space *aspace2,
173                                      CORE_ADDR addr2);
174
175 static int watchpoint_locations_match (struct bp_location *loc1,
176                                        struct bp_location *loc2);
177
178 static int breakpoint_location_address_match (struct bp_location *bl,
179                                               struct address_space *aspace,
180                                               CORE_ADDR addr);
181
182 static void breakpoints_info (char *, int);
183
184 static void watchpoints_info (char *, int);
185
186 static int breakpoint_1 (char *, int, 
187                          int (*) (const struct breakpoint *));
188
189 static int breakpoint_cond_eval (void *);
190
191 static void cleanup_executing_breakpoints (void *);
192
193 static void commands_command (char *, int);
194
195 static void condition_command (char *, int);
196
197 typedef enum
198   {
199     mark_inserted,
200     mark_uninserted
201   }
202 insertion_state_t;
203
204 static int remove_breakpoint (struct bp_location *, insertion_state_t);
205 static int remove_breakpoint_1 (struct bp_location *, insertion_state_t);
206
207 static enum print_stop_action print_bp_stop_message (bpstat bs);
208
209 static int watchpoint_check (void *);
210
211 static void maintenance_info_breakpoints (char *, int);
212
213 static int hw_breakpoint_used_count (void);
214
215 static int hw_watchpoint_use_count (struct breakpoint *);
216
217 static int hw_watchpoint_used_count_others (struct breakpoint *except,
218                                             enum bptype type,
219                                             int *other_type_used);
220
221 static void hbreak_command (char *, int);
222
223 static void thbreak_command (char *, int);
224
225 static void enable_breakpoint_disp (struct breakpoint *, enum bpdisp,
226                                     int count);
227
228 static void stop_command (char *arg, int from_tty);
229
230 static void stopin_command (char *arg, int from_tty);
231
232 static void stopat_command (char *arg, int from_tty);
233
234 static char *ep_parse_optional_if_clause (char **arg);
235
236 static void catch_exception_command_1 (enum exception_event_kind ex_event, 
237                                        char *arg, int tempflag, int from_tty);
238
239 static void tcatch_command (char *arg, int from_tty);
240
241 static void detach_single_step_breakpoints (void);
242
243 static int single_step_breakpoint_inserted_here_p (struct address_space *,
244                                                    CORE_ADDR pc);
245
246 static void free_bp_location (struct bp_location *loc);
247 static void incref_bp_location (struct bp_location *loc);
248 static void decref_bp_location (struct bp_location **loc);
249
250 static struct bp_location *allocate_bp_location (struct breakpoint *bpt);
251
252 static void update_global_location_list (int);
253
254 static void update_global_location_list_nothrow (int);
255
256 static int is_hardware_watchpoint (const struct breakpoint *bpt);
257
258 static void insert_breakpoint_locations (void);
259
260 static int syscall_catchpoint_p (struct breakpoint *b);
261
262 static void tracepoints_info (char *, int);
263
264 static void delete_trace_command (char *, int);
265
266 static void enable_trace_command (char *, int);
267
268 static void disable_trace_command (char *, int);
269
270 static void trace_pass_command (char *, int);
271
272 static void set_tracepoint_count (int num);
273
274 static int is_masked_watchpoint (const struct breakpoint *b);
275
276 static struct bp_location **get_first_locp_gte_addr (CORE_ADDR address);
277
278 /* Return 1 if B refers to a static tracepoint set by marker ("-m"), zero
279    otherwise.  */
280
281 static int strace_marker_p (struct breakpoint *b);
282
283 /* The abstract base class all breakpoint_ops structures inherit
284    from.  */
285 struct breakpoint_ops base_breakpoint_ops;
286
287 /* The breakpoint_ops structure to be inherited by all breakpoint_ops
288    that are implemented on top of software or hardware breakpoints
289    (user breakpoints, internal and momentary breakpoints, etc.).  */
290 static struct breakpoint_ops bkpt_base_breakpoint_ops;
291
292 /* Internal breakpoints class type.  */
293 static struct breakpoint_ops internal_breakpoint_ops;
294
295 /* Momentary breakpoints class type.  */
296 static struct breakpoint_ops momentary_breakpoint_ops;
297
298 /* Momentary breakpoints for bp_longjmp and bp_exception class type.  */
299 static struct breakpoint_ops longjmp_breakpoint_ops;
300
301 /* The breakpoint_ops structure to be used in regular user created
302    breakpoints.  */
303 struct breakpoint_ops bkpt_breakpoint_ops;
304
305 /* Breakpoints set on probes.  */
306 static struct breakpoint_ops bkpt_probe_breakpoint_ops;
307
308 /* Dynamic printf class type.  */
309 static struct breakpoint_ops dprintf_breakpoint_ops;
310
311 /* The style in which to perform a dynamic printf.  This is a user
312    option because different output options have different tradeoffs;
313    if GDB does the printing, there is better error handling if there
314    is a problem with any of the arguments, but using an inferior
315    function lets you have special-purpose printers and sending of
316    output to the same place as compiled-in print functions.  */
317
318 static const char dprintf_style_gdb[] = "gdb";
319 static const char dprintf_style_call[] = "call";
320 static const char dprintf_style_agent[] = "agent";
321 static const char *const dprintf_style_enums[] = {
322   dprintf_style_gdb,
323   dprintf_style_call,
324   dprintf_style_agent,
325   NULL
326 };
327 static const char *dprintf_style = dprintf_style_gdb;
328
329 /* The function to use for dynamic printf if the preferred style is to
330    call into the inferior.  The value is simply a string that is
331    copied into the command, so it can be anything that GDB can
332    evaluate to a callable address, not necessarily a function name.  */
333
334 static char *dprintf_function = "";
335
336 /* The channel to use for dynamic printf if the preferred style is to
337    call into the inferior; if a nonempty string, it will be passed to
338    the call as the first argument, with the format string as the
339    second.  As with the dprintf function, this can be anything that
340    GDB knows how to evaluate, so in addition to common choices like
341    "stderr", this could be an app-specific expression like
342    "mystreams[curlogger]".  */
343
344 static char *dprintf_channel = "";
345
346 /* True if dprintf commands should continue to operate even if GDB
347    has disconnected.  */
348 static int disconnected_dprintf = 1;
349
350 /* A reference-counted struct command_line.  This lets multiple
351    breakpoints share a single command list.  */
352 struct counted_command_line
353 {
354   /* The reference count.  */
355   int refc;
356
357   /* The command list.  */
358   struct command_line *commands;
359 };
360
361 struct command_line *
362 breakpoint_commands (struct breakpoint *b)
363 {
364   return b->commands ? b->commands->commands : NULL;
365 }
366
367 /* Flag indicating that a command has proceeded the inferior past the
368    current breakpoint.  */
369
370 static int breakpoint_proceeded;
371
372 const char *
373 bpdisp_text (enum bpdisp disp)
374 {
375   /* NOTE: the following values are a part of MI protocol and
376      represent values of 'disp' field returned when inferior stops at
377      a breakpoint.  */
378   static const char * const bpdisps[] = {"del", "dstp", "dis", "keep"};
379
380   return bpdisps[(int) disp];
381 }
382
383 /* Prototypes for exported functions.  */
384 /* If FALSE, gdb will not use hardware support for watchpoints, even
385    if such is available.  */
386 static int can_use_hw_watchpoints;
387
388 static void
389 show_can_use_hw_watchpoints (struct ui_file *file, int from_tty,
390                              struct cmd_list_element *c,
391                              const char *value)
392 {
393   fprintf_filtered (file,
394                     _("Debugger's willingness to use "
395                       "watchpoint hardware is %s.\n"),
396                     value);
397 }
398
399 /* If AUTO_BOOLEAN_FALSE, gdb will not attempt to create pending breakpoints.
400    If AUTO_BOOLEAN_TRUE, gdb will automatically create pending breakpoints
401    for unrecognized breakpoint locations.
402    If AUTO_BOOLEAN_AUTO, gdb will query when breakpoints are unrecognized.  */
403 static enum auto_boolean pending_break_support;
404 static void
405 show_pending_break_support (struct ui_file *file, int from_tty,
406                             struct cmd_list_element *c,
407                             const char *value)
408 {
409   fprintf_filtered (file,
410                     _("Debugger's behavior regarding "
411                       "pending breakpoints is %s.\n"),
412                     value);
413 }
414
415 /* If 1, gdb will automatically use hardware breakpoints for breakpoints
416    set with "break" but falling in read-only memory.
417    If 0, gdb will warn about such breakpoints, but won't automatically
418    use hardware breakpoints.  */
419 static int automatic_hardware_breakpoints;
420 static void
421 show_automatic_hardware_breakpoints (struct ui_file *file, int from_tty,
422                                      struct cmd_list_element *c,
423                                      const char *value)
424 {
425   fprintf_filtered (file,
426                     _("Automatic usage of hardware breakpoints is %s.\n"),
427                     value);
428 }
429
430 /* If on, gdb will keep breakpoints inserted even as inferior is
431    stopped, and immediately insert any new breakpoints.  If off, gdb
432    will insert breakpoints into inferior only when resuming it, and
433    will remove breakpoints upon stop.  If auto, GDB will behave as ON
434    if in non-stop mode, and as OFF if all-stop mode.*/
435
436 static enum auto_boolean always_inserted_mode = AUTO_BOOLEAN_AUTO;
437
438 static void
439 show_always_inserted_mode (struct ui_file *file, int from_tty,
440                      struct cmd_list_element *c, const char *value)
441 {
442   if (always_inserted_mode == AUTO_BOOLEAN_AUTO)
443     fprintf_filtered (file,
444                       _("Always inserted breakpoint "
445                         "mode is %s (currently %s).\n"),
446                       value,
447                       breakpoints_always_inserted_mode () ? "on" : "off");
448   else
449     fprintf_filtered (file, _("Always inserted breakpoint mode is %s.\n"),
450                       value);
451 }
452
453 int
454 breakpoints_always_inserted_mode (void)
455 {
456   return (always_inserted_mode == AUTO_BOOLEAN_TRUE
457           || (always_inserted_mode == AUTO_BOOLEAN_AUTO && non_stop));
458 }
459
460 static const char condition_evaluation_both[] = "host or target";
461
462 /* Modes for breakpoint condition evaluation.  */
463 static const char condition_evaluation_auto[] = "auto";
464 static const char condition_evaluation_host[] = "host";
465 static const char condition_evaluation_target[] = "target";
466 static const char *const condition_evaluation_enums[] = {
467   condition_evaluation_auto,
468   condition_evaluation_host,
469   condition_evaluation_target,
470   NULL
471 };
472
473 /* Global that holds the current mode for breakpoint condition evaluation.  */
474 static const char *condition_evaluation_mode_1 = condition_evaluation_auto;
475
476 /* Global that we use to display information to the user (gets its value from
477    condition_evaluation_mode_1.  */
478 static const char *condition_evaluation_mode = condition_evaluation_auto;
479
480 /* Translate a condition evaluation mode MODE into either "host"
481    or "target".  This is used mostly to translate from "auto" to the
482    real setting that is being used.  It returns the translated
483    evaluation mode.  */
484
485 static const char *
486 translate_condition_evaluation_mode (const char *mode)
487 {
488   if (mode == condition_evaluation_auto)
489     {
490       if (target_supports_evaluation_of_breakpoint_conditions ())
491         return condition_evaluation_target;
492       else
493         return condition_evaluation_host;
494     }
495   else
496     return mode;
497 }
498
499 /* Discovers what condition_evaluation_auto translates to.  */
500
501 static const char *
502 breakpoint_condition_evaluation_mode (void)
503 {
504   return translate_condition_evaluation_mode (condition_evaluation_mode);
505 }
506
507 /* Return true if GDB should evaluate breakpoint conditions or false
508    otherwise.  */
509
510 static int
511 gdb_evaluates_breakpoint_condition_p (void)
512 {
513   const char *mode = breakpoint_condition_evaluation_mode ();
514
515   return (mode == condition_evaluation_host);
516 }
517
518 void _initialize_breakpoint (void);
519
520 /* Are we executing breakpoint commands?  */
521 static int executing_breakpoint_commands;
522
523 /* Are overlay event breakpoints enabled? */
524 static int overlay_events_enabled;
525
526 /* See description in breakpoint.h. */
527 int target_exact_watchpoints = 0;
528
529 /* Walk the following statement or block through all breakpoints.
530    ALL_BREAKPOINTS_SAFE does so even if the statement deletes the
531    current breakpoint.  */
532
533 #define ALL_BREAKPOINTS(B)  for (B = breakpoint_chain; B; B = B->next)
534
535 #define ALL_BREAKPOINTS_SAFE(B,TMP)     \
536         for (B = breakpoint_chain;      \
537              B ? (TMP=B->next, 1): 0;   \
538              B = TMP)
539
540 /* Similar iterator for the low-level breakpoints.  SAFE variant is
541    not provided so update_global_location_list must not be called
542    while executing the block of ALL_BP_LOCATIONS.  */
543
544 #define ALL_BP_LOCATIONS(B,BP_TMP)                                      \
545         for (BP_TMP = bp_location;                                      \
546              BP_TMP < bp_location + bp_location_count && (B = *BP_TMP); \
547              BP_TMP++)
548
549 /* Iterates through locations with address ADDRESS for the currently selected
550    program space.  BP_LOCP_TMP points to each object.  BP_LOCP_START points
551    to where the loop should start from.
552    If BP_LOCP_START is a NULL pointer, the macro automatically seeks the
553    appropriate location to start with.  */
554
555 #define ALL_BP_LOCATIONS_AT_ADDR(BP_LOCP_TMP, BP_LOCP_START, ADDRESS)   \
556         for (BP_LOCP_START = BP_LOCP_START == NULL ? get_first_locp_gte_addr (ADDRESS) : BP_LOCP_START, \
557              BP_LOCP_TMP = BP_LOCP_START;                               \
558              BP_LOCP_START                                              \
559              && (BP_LOCP_TMP < bp_location + bp_location_count          \
560              && (*BP_LOCP_TMP)->address == ADDRESS);                    \
561              BP_LOCP_TMP++)
562
563 /* Iterator for tracepoints only.  */
564
565 #define ALL_TRACEPOINTS(B)  \
566   for (B = breakpoint_chain; B; B = B->next)  \
567     if (is_tracepoint (B))
568
569 /* Chains of all breakpoints defined.  */
570
571 struct breakpoint *breakpoint_chain;
572
573 /* Array is sorted by bp_location_compare - primarily by the ADDRESS.  */
574
575 static struct bp_location **bp_location;
576
577 /* Number of elements of BP_LOCATION.  */
578
579 static unsigned bp_location_count;
580
581 /* Maximum alignment offset between bp_target_info.PLACED_ADDRESS and
582    ADDRESS for the current elements of BP_LOCATION which get a valid
583    result from bp_location_has_shadow.  You can use it for roughly
584    limiting the subrange of BP_LOCATION to scan for shadow bytes for
585    an address you need to read.  */
586
587 static CORE_ADDR bp_location_placed_address_before_address_max;
588
589 /* Maximum offset plus alignment between bp_target_info.PLACED_ADDRESS
590    + bp_target_info.SHADOW_LEN and ADDRESS for the current elements of
591    BP_LOCATION which get a valid result from bp_location_has_shadow.
592    You can use it for roughly limiting the subrange of BP_LOCATION to
593    scan for shadow bytes for an address you need to read.  */
594
595 static CORE_ADDR bp_location_shadow_len_after_address_max;
596
597 /* The locations that no longer correspond to any breakpoint, unlinked
598    from bp_location array, but for which a hit may still be reported
599    by a target.  */
600 VEC(bp_location_p) *moribund_locations = NULL;
601
602 /* Number of last breakpoint made.  */
603
604 static int breakpoint_count;
605
606 /* The value of `breakpoint_count' before the last command that
607    created breakpoints.  If the last (break-like) command created more
608    than one breakpoint, then the difference between BREAKPOINT_COUNT
609    and PREV_BREAKPOINT_COUNT is more than one.  */
610 static int prev_breakpoint_count;
611
612 /* Number of last tracepoint made.  */
613
614 static int tracepoint_count;
615
616 static struct cmd_list_element *breakpoint_set_cmdlist;
617 static struct cmd_list_element *breakpoint_show_cmdlist;
618 struct cmd_list_element *save_cmdlist;
619
620 /* Return whether a breakpoint is an active enabled breakpoint.  */
621 static int
622 breakpoint_enabled (struct breakpoint *b)
623 {
624   return (b->enable_state == bp_enabled);
625 }
626
627 /* Set breakpoint count to NUM.  */
628
629 static void
630 set_breakpoint_count (int num)
631 {
632   prev_breakpoint_count = breakpoint_count;
633   breakpoint_count = num;
634   set_internalvar_integer (lookup_internalvar ("bpnum"), num);
635 }
636
637 /* Used by `start_rbreak_breakpoints' below, to record the current
638    breakpoint count before "rbreak" creates any breakpoint.  */
639 static int rbreak_start_breakpoint_count;
640
641 /* Called at the start an "rbreak" command to record the first
642    breakpoint made.  */
643
644 void
645 start_rbreak_breakpoints (void)
646 {
647   rbreak_start_breakpoint_count = breakpoint_count;
648 }
649
650 /* Called at the end of an "rbreak" command to record the last
651    breakpoint made.  */
652
653 void
654 end_rbreak_breakpoints (void)
655 {
656   prev_breakpoint_count = rbreak_start_breakpoint_count;
657 }
658
659 /* Used in run_command to zero the hit count when a new run starts.  */
660
661 void
662 clear_breakpoint_hit_counts (void)
663 {
664   struct breakpoint *b;
665
666   ALL_BREAKPOINTS (b)
667     b->hit_count = 0;
668 }
669
670 /* Allocate a new counted_command_line with reference count of 1.
671    The new structure owns COMMANDS.  */
672
673 static struct counted_command_line *
674 alloc_counted_command_line (struct command_line *commands)
675 {
676   struct counted_command_line *result
677     = xmalloc (sizeof (struct counted_command_line));
678
679   result->refc = 1;
680   result->commands = commands;
681   return result;
682 }
683
684 /* Increment reference count.  This does nothing if CMD is NULL.  */
685
686 static void
687 incref_counted_command_line (struct counted_command_line *cmd)
688 {
689   if (cmd)
690     ++cmd->refc;
691 }
692
693 /* Decrement reference count.  If the reference count reaches 0,
694    destroy the counted_command_line.  Sets *CMDP to NULL.  This does
695    nothing if *CMDP is NULL.  */
696
697 static void
698 decref_counted_command_line (struct counted_command_line **cmdp)
699 {
700   if (*cmdp)
701     {
702       if (--(*cmdp)->refc == 0)
703         {
704           free_command_lines (&(*cmdp)->commands);
705           xfree (*cmdp);
706         }
707       *cmdp = NULL;
708     }
709 }
710
711 /* A cleanup function that calls decref_counted_command_line.  */
712
713 static void
714 do_cleanup_counted_command_line (void *arg)
715 {
716   decref_counted_command_line (arg);
717 }
718
719 /* Create a cleanup that calls decref_counted_command_line on the
720    argument.  */
721
722 static struct cleanup *
723 make_cleanup_decref_counted_command_line (struct counted_command_line **cmdp)
724 {
725   return make_cleanup (do_cleanup_counted_command_line, cmdp);
726 }
727
728 \f
729 /* Return the breakpoint with the specified number, or NULL
730    if the number does not refer to an existing breakpoint.  */
731
732 struct breakpoint *
733 get_breakpoint (int num)
734 {
735   struct breakpoint *b;
736
737   ALL_BREAKPOINTS (b)
738     if (b->number == num)
739       return b;
740   
741   return NULL;
742 }
743
744 \f
745
746 /* Mark locations as "conditions have changed" in case the target supports
747    evaluating conditions on its side.  */
748
749 static void
750 mark_breakpoint_modified (struct breakpoint *b)
751 {
752   struct bp_location *loc;
753
754   /* This is only meaningful if the target is
755      evaluating conditions and if the user has
756      opted for condition evaluation on the target's
757      side.  */
758   if (gdb_evaluates_breakpoint_condition_p ()
759       || !target_supports_evaluation_of_breakpoint_conditions ())
760     return;
761
762   if (!is_breakpoint (b))
763     return;
764
765   for (loc = b->loc; loc; loc = loc->next)
766     loc->condition_changed = condition_modified;
767 }
768
769 /* Mark location as "conditions have changed" in case the target supports
770    evaluating conditions on its side.  */
771
772 static void
773 mark_breakpoint_location_modified (struct bp_location *loc)
774 {
775   /* This is only meaningful if the target is
776      evaluating conditions and if the user has
777      opted for condition evaluation on the target's
778      side.  */
779   if (gdb_evaluates_breakpoint_condition_p ()
780       || !target_supports_evaluation_of_breakpoint_conditions ())
781
782     return;
783
784   if (!is_breakpoint (loc->owner))
785     return;
786
787   loc->condition_changed = condition_modified;
788 }
789
790 /* Sets the condition-evaluation mode using the static global
791    condition_evaluation_mode.  */
792
793 static void
794 set_condition_evaluation_mode (char *args, int from_tty,
795                                struct cmd_list_element *c)
796 {
797   const char *old_mode, *new_mode;
798
799   if ((condition_evaluation_mode_1 == condition_evaluation_target)
800       && !target_supports_evaluation_of_breakpoint_conditions ())
801     {
802       condition_evaluation_mode_1 = condition_evaluation_mode;
803       warning (_("Target does not support breakpoint condition evaluation.\n"
804                  "Using host evaluation mode instead."));
805       return;
806     }
807
808   new_mode = translate_condition_evaluation_mode (condition_evaluation_mode_1);
809   old_mode = translate_condition_evaluation_mode (condition_evaluation_mode);
810
811   /* Flip the switch.  Flip it even if OLD_MODE == NEW_MODE as one of the
812      settings was "auto".  */
813   condition_evaluation_mode = condition_evaluation_mode_1;
814
815   /* Only update the mode if the user picked a different one.  */
816   if (new_mode != old_mode)
817     {
818       struct bp_location *loc, **loc_tmp;
819       /* If the user switched to a different evaluation mode, we
820          need to synch the changes with the target as follows:
821
822          "host" -> "target": Send all (valid) conditions to the target.
823          "target" -> "host": Remove all the conditions from the target.
824       */
825
826       if (new_mode == condition_evaluation_target)
827         {
828           /* Mark everything modified and synch conditions with the
829              target.  */
830           ALL_BP_LOCATIONS (loc, loc_tmp)
831             mark_breakpoint_location_modified (loc);
832         }
833       else
834         {
835           /* Manually mark non-duplicate locations to synch conditions
836              with the target.  We do this to remove all the conditions the
837              target knows about.  */
838           ALL_BP_LOCATIONS (loc, loc_tmp)
839             if (is_breakpoint (loc->owner) && loc->inserted)
840               loc->needs_update = 1;
841         }
842
843       /* Do the update.  */
844       update_global_location_list (1);
845     }
846
847   return;
848 }
849
850 /* Shows the current mode of breakpoint condition evaluation.  Explicitly shows
851    what "auto" is translating to.  */
852
853 static void
854 show_condition_evaluation_mode (struct ui_file *file, int from_tty,
855                                 struct cmd_list_element *c, const char *value)
856 {
857   if (condition_evaluation_mode == condition_evaluation_auto)
858     fprintf_filtered (file,
859                       _("Breakpoint condition evaluation "
860                         "mode is %s (currently %s).\n"),
861                       value,
862                       breakpoint_condition_evaluation_mode ());
863   else
864     fprintf_filtered (file, _("Breakpoint condition evaluation mode is %s.\n"),
865                       value);
866 }
867
868 /* A comparison function for bp_location AP and BP that is used by
869    bsearch.  This comparison function only cares about addresses, unlike
870    the more general bp_location_compare function.  */
871
872 static int
873 bp_location_compare_addrs (const void *ap, const void *bp)
874 {
875   struct bp_location *a = *(void **) ap;
876   struct bp_location *b = *(void **) bp;
877
878   if (a->address == b->address)
879     return 0;
880   else
881     return ((a->address > b->address) - (a->address < b->address));
882 }
883
884 /* Helper function to skip all bp_locations with addresses
885    less than ADDRESS.  It returns the first bp_location that
886    is greater than or equal to ADDRESS.  If none is found, just
887    return NULL.  */
888
889 static struct bp_location **
890 get_first_locp_gte_addr (CORE_ADDR address)
891 {
892   struct bp_location dummy_loc;
893   struct bp_location *dummy_locp = &dummy_loc;
894   struct bp_location **locp_found = NULL;
895
896   /* Initialize the dummy location's address field.  */
897   memset (&dummy_loc, 0, sizeof (struct bp_location));
898   dummy_loc.address = address;
899
900   /* Find a close match to the first location at ADDRESS.  */
901   locp_found = bsearch (&dummy_locp, bp_location, bp_location_count,
902                         sizeof (struct bp_location **),
903                         bp_location_compare_addrs);
904
905   /* Nothing was found, nothing left to do.  */
906   if (locp_found == NULL)
907     return NULL;
908
909   /* We may have found a location that is at ADDRESS but is not the first in the
910      location's list.  Go backwards (if possible) and locate the first one.  */
911   while ((locp_found - 1) >= bp_location
912          && (*(locp_found - 1))->address == address)
913     locp_found--;
914
915   return locp_found;
916 }
917
918 void
919 set_breakpoint_condition (struct breakpoint *b, char *exp,
920                           int from_tty)
921 {
922   xfree (b->cond_string);
923   b->cond_string = NULL;
924
925   if (is_watchpoint (b))
926     {
927       struct watchpoint *w = (struct watchpoint *) b;
928
929       xfree (w->cond_exp);
930       w->cond_exp = NULL;
931     }
932   else
933     {
934       struct bp_location *loc;
935
936       for (loc = b->loc; loc; loc = loc->next)
937         {
938           xfree (loc->cond);
939           loc->cond = NULL;
940
941           /* No need to free the condition agent expression
942              bytecode (if we have one).  We will handle this
943              when we go through update_global_location_list.  */
944         }
945     }
946
947   if (*exp == 0)
948     {
949       if (from_tty)
950         printf_filtered (_("Breakpoint %d now unconditional.\n"), b->number);
951     }
952   else
953     {
954       const char *arg = exp;
955
956       /* I don't know if it matters whether this is the string the user
957          typed in or the decompiled expression.  */
958       b->cond_string = xstrdup (arg);
959       b->condition_not_parsed = 0;
960
961       if (is_watchpoint (b))
962         {
963           struct watchpoint *w = (struct watchpoint *) b;
964
965           innermost_block = NULL;
966           arg = exp;
967           w->cond_exp = parse_exp_1 (&arg, 0, 0, 0);
968           if (*arg)
969             error (_("Junk at end of expression"));
970           w->cond_exp_valid_block = innermost_block;
971         }
972       else
973         {
974           struct bp_location *loc;
975
976           for (loc = b->loc; loc; loc = loc->next)
977             {
978               arg = exp;
979               loc->cond =
980                 parse_exp_1 (&arg, loc->address,
981                              block_for_pc (loc->address), 0);
982               if (*arg)
983                 error (_("Junk at end of expression"));
984             }
985         }
986     }
987   mark_breakpoint_modified (b);
988
989   observer_notify_breakpoint_modified (b);
990 }
991
992 /* Completion for the "condition" command.  */
993
994 static VEC (char_ptr) *
995 condition_completer (struct cmd_list_element *cmd,
996                      const char *text, const char *word)
997 {
998   const char *space;
999
1000   text = skip_spaces_const (text);
1001   space = skip_to_space_const (text);
1002   if (*space == '\0')
1003     {
1004       int len;
1005       struct breakpoint *b;
1006       VEC (char_ptr) *result = NULL;
1007
1008       if (text[0] == '$')
1009         {
1010           /* We don't support completion of history indices.  */
1011           if (isdigit (text[1]))
1012             return NULL;
1013           return complete_internalvar (&text[1]);
1014         }
1015
1016       /* We're completing the breakpoint number.  */
1017       len = strlen (text);
1018
1019       ALL_BREAKPOINTS (b)
1020       {
1021         int single = b->loc->next == NULL;
1022         struct bp_location *loc;
1023         int count = 1;
1024
1025         for (loc = b->loc; loc; loc = loc->next)
1026           {
1027             char location[50];
1028
1029             if (single)
1030               xsnprintf (location, sizeof (location), "%d", b->number);
1031             else
1032               xsnprintf (location, sizeof (location),  "%d.%d", b->number,
1033                          count);
1034
1035             if (strncmp (location, text, len) == 0)
1036               VEC_safe_push (char_ptr, result, xstrdup (location));
1037
1038             ++count;
1039           }
1040       }
1041
1042       return result;
1043     }
1044
1045   /* We're completing the expression part.  */
1046   text = skip_spaces_const (space);
1047   return expression_completer (cmd, text, word);
1048 }
1049
1050 /* condition N EXP -- set break condition of breakpoint N to EXP.  */
1051
1052 static void
1053 condition_command (char *arg, int from_tty)
1054 {
1055   struct breakpoint *b;
1056   char *p;
1057   int bnum;
1058
1059   if (arg == 0)
1060     error_no_arg (_("breakpoint number"));
1061
1062   p = arg;
1063   bnum = get_number (&p);
1064   if (bnum == 0)
1065     error (_("Bad breakpoint argument: '%s'"), arg);
1066
1067   ALL_BREAKPOINTS (b)
1068     if (b->number == bnum)
1069       {
1070         /* Check if this breakpoint has a Python object assigned to
1071            it, and if it has a definition of the "stop"
1072            method.  This method and conditions entered into GDB from
1073            the CLI are mutually exclusive.  */
1074         if (b->py_bp_object
1075             && gdbpy_breakpoint_has_py_cond (b->py_bp_object))
1076           error (_("Cannot set a condition where a Python 'stop' "
1077                    "method has been defined in the breakpoint."));
1078         set_breakpoint_condition (b, p, from_tty);
1079
1080         if (is_breakpoint (b))
1081           update_global_location_list (1);
1082
1083         return;
1084       }
1085
1086   error (_("No breakpoint number %d."), bnum);
1087 }
1088
1089 /* Check that COMMAND do not contain commands that are suitable
1090    only for tracepoints and not suitable for ordinary breakpoints.
1091    Throw if any such commands is found.  */
1092
1093 static void
1094 check_no_tracepoint_commands (struct command_line *commands)
1095 {
1096   struct command_line *c;
1097
1098   for (c = commands; c; c = c->next)
1099     {
1100       int i;
1101
1102       if (c->control_type == while_stepping_control)
1103         error (_("The 'while-stepping' command can "
1104                  "only be used for tracepoints"));
1105
1106       for (i = 0; i < c->body_count; ++i)
1107         check_no_tracepoint_commands ((c->body_list)[i]);
1108
1109       /* Not that command parsing removes leading whitespace and comment
1110          lines and also empty lines.  So, we only need to check for
1111          command directly.  */
1112       if (strstr (c->line, "collect ") == c->line)
1113         error (_("The 'collect' command can only be used for tracepoints"));
1114
1115       if (strstr (c->line, "teval ") == c->line)
1116         error (_("The 'teval' command can only be used for tracepoints"));
1117     }
1118 }
1119
1120 /* Encapsulate tests for different types of tracepoints.  */
1121
1122 static int
1123 is_tracepoint_type (enum bptype type)
1124 {
1125   return (type == bp_tracepoint
1126           || type == bp_fast_tracepoint
1127           || type == bp_static_tracepoint);
1128 }
1129
1130 int
1131 is_tracepoint (const struct breakpoint *b)
1132 {
1133   return is_tracepoint_type (b->type);
1134 }
1135
1136 /* A helper function that validates that COMMANDS are valid for a
1137    breakpoint.  This function will throw an exception if a problem is
1138    found.  */
1139
1140 static void
1141 validate_commands_for_breakpoint (struct breakpoint *b,
1142                                   struct command_line *commands)
1143 {
1144   if (is_tracepoint (b))
1145     {
1146       struct tracepoint *t = (struct tracepoint *) b;
1147       struct command_line *c;
1148       struct command_line *while_stepping = 0;
1149
1150       /* Reset the while-stepping step count.  The previous commands
1151          might have included a while-stepping action, while the new
1152          ones might not.  */
1153       t->step_count = 0;
1154
1155       /* We need to verify that each top-level element of commands is
1156          valid for tracepoints, that there's at most one
1157          while-stepping element, and that the while-stepping's body
1158          has valid tracing commands excluding nested while-stepping.
1159          We also need to validate the tracepoint action line in the
1160          context of the tracepoint --- validate_actionline actually
1161          has side effects, like setting the tracepoint's
1162          while-stepping STEP_COUNT, in addition to checking if the
1163          collect/teval actions parse and make sense in the
1164          tracepoint's context.  */
1165       for (c = commands; c; c = c->next)
1166         {
1167           if (c->control_type == while_stepping_control)
1168             {
1169               if (b->type == bp_fast_tracepoint)
1170                 error (_("The 'while-stepping' command "
1171                          "cannot be used for fast tracepoint"));
1172               else if (b->type == bp_static_tracepoint)
1173                 error (_("The 'while-stepping' command "
1174                          "cannot be used for static tracepoint"));
1175
1176               if (while_stepping)
1177                 error (_("The 'while-stepping' command "
1178                          "can be used only once"));
1179               else
1180                 while_stepping = c;
1181             }
1182
1183           validate_actionline (c->line, b);
1184         }
1185       if (while_stepping)
1186         {
1187           struct command_line *c2;
1188
1189           gdb_assert (while_stepping->body_count == 1);
1190           c2 = while_stepping->body_list[0];
1191           for (; c2; c2 = c2->next)
1192             {
1193               if (c2->control_type == while_stepping_control)
1194                 error (_("The 'while-stepping' command cannot be nested"));
1195             }
1196         }
1197     }
1198   else
1199     {
1200       check_no_tracepoint_commands (commands);
1201     }
1202 }
1203
1204 /* Return a vector of all the static tracepoints set at ADDR.  The
1205    caller is responsible for releasing the vector.  */
1206
1207 VEC(breakpoint_p) *
1208 static_tracepoints_here (CORE_ADDR addr)
1209 {
1210   struct breakpoint *b;
1211   VEC(breakpoint_p) *found = 0;
1212   struct bp_location *loc;
1213
1214   ALL_BREAKPOINTS (b)
1215     if (b->type == bp_static_tracepoint)
1216       {
1217         for (loc = b->loc; loc; loc = loc->next)
1218           if (loc->address == addr)
1219             VEC_safe_push(breakpoint_p, found, b);
1220       }
1221
1222   return found;
1223 }
1224
1225 /* Set the command list of B to COMMANDS.  If breakpoint is tracepoint,
1226    validate that only allowed commands are included.  */
1227
1228 void
1229 breakpoint_set_commands (struct breakpoint *b, 
1230                          struct command_line *commands)
1231 {
1232   validate_commands_for_breakpoint (b, commands);
1233
1234   decref_counted_command_line (&b->commands);
1235   b->commands = alloc_counted_command_line (commands);
1236   observer_notify_breakpoint_modified (b);
1237 }
1238
1239 /* Set the internal `silent' flag on the breakpoint.  Note that this
1240    is not the same as the "silent" that may appear in the breakpoint's
1241    commands.  */
1242
1243 void
1244 breakpoint_set_silent (struct breakpoint *b, int silent)
1245 {
1246   int old_silent = b->silent;
1247
1248   b->silent = silent;
1249   if (old_silent != silent)
1250     observer_notify_breakpoint_modified (b);
1251 }
1252
1253 /* Set the thread for this breakpoint.  If THREAD is -1, make the
1254    breakpoint work for any thread.  */
1255
1256 void
1257 breakpoint_set_thread (struct breakpoint *b, int thread)
1258 {
1259   int old_thread = b->thread;
1260
1261   b->thread = thread;
1262   if (old_thread != thread)
1263     observer_notify_breakpoint_modified (b);
1264 }
1265
1266 /* Set the task for this breakpoint.  If TASK is 0, make the
1267    breakpoint work for any task.  */
1268
1269 void
1270 breakpoint_set_task (struct breakpoint *b, int task)
1271 {
1272   int old_task = b->task;
1273
1274   b->task = task;
1275   if (old_task != task)
1276     observer_notify_breakpoint_modified (b);
1277 }
1278
1279 void
1280 check_tracepoint_command (char *line, void *closure)
1281 {
1282   struct breakpoint *b = closure;
1283
1284   validate_actionline (line, b);
1285 }
1286
1287 /* A structure used to pass information through
1288    map_breakpoint_numbers.  */
1289
1290 struct commands_info
1291 {
1292   /* True if the command was typed at a tty.  */
1293   int from_tty;
1294
1295   /* The breakpoint range spec.  */
1296   char *arg;
1297
1298   /* Non-NULL if the body of the commands are being read from this
1299      already-parsed command.  */
1300   struct command_line *control;
1301
1302   /* The command lines read from the user, or NULL if they have not
1303      yet been read.  */
1304   struct counted_command_line *cmd;
1305 };
1306
1307 /* A callback for map_breakpoint_numbers that sets the commands for
1308    commands_command.  */
1309
1310 static void
1311 do_map_commands_command (struct breakpoint *b, void *data)
1312 {
1313   struct commands_info *info = data;
1314
1315   if (info->cmd == NULL)
1316     {
1317       struct command_line *l;
1318
1319       if (info->control != NULL)
1320         l = copy_command_lines (info->control->body_list[0]);
1321       else
1322         {
1323           struct cleanup *old_chain;
1324           char *str;
1325
1326           str = xstrprintf (_("Type commands for breakpoint(s) "
1327                               "%s, one per line."),
1328                             info->arg);
1329
1330           old_chain = make_cleanup (xfree, str);
1331
1332           l = read_command_lines (str,
1333                                   info->from_tty, 1,
1334                                   (is_tracepoint (b)
1335                                    ? check_tracepoint_command : 0),
1336                                   b);
1337
1338           do_cleanups (old_chain);
1339         }
1340
1341       info->cmd = alloc_counted_command_line (l);
1342     }
1343
1344   /* If a breakpoint was on the list more than once, we don't need to
1345      do anything.  */
1346   if (b->commands != info->cmd)
1347     {
1348       validate_commands_for_breakpoint (b, info->cmd->commands);
1349       incref_counted_command_line (info->cmd);
1350       decref_counted_command_line (&b->commands);
1351       b->commands = info->cmd;
1352       observer_notify_breakpoint_modified (b);
1353     }
1354 }
1355
1356 static void
1357 commands_command_1 (char *arg, int from_tty, 
1358                     struct command_line *control)
1359 {
1360   struct cleanup *cleanups;
1361   struct commands_info info;
1362
1363   info.from_tty = from_tty;
1364   info.control = control;
1365   info.cmd = NULL;
1366   /* If we read command lines from the user, then `info' will hold an
1367      extra reference to the commands that we must clean up.  */
1368   cleanups = make_cleanup_decref_counted_command_line (&info.cmd);
1369
1370   if (arg == NULL || !*arg)
1371     {
1372       if (breakpoint_count - prev_breakpoint_count > 1)
1373         arg = xstrprintf ("%d-%d", prev_breakpoint_count + 1, 
1374                           breakpoint_count);
1375       else if (breakpoint_count > 0)
1376         arg = xstrprintf ("%d", breakpoint_count);
1377       else
1378         {
1379           /* So that we don't try to free the incoming non-NULL
1380              argument in the cleanup below.  Mapping breakpoint
1381              numbers will fail in this case.  */
1382           arg = NULL;
1383         }
1384     }
1385   else
1386     /* The command loop has some static state, so we need to preserve
1387        our argument.  */
1388     arg = xstrdup (arg);
1389
1390   if (arg != NULL)
1391     make_cleanup (xfree, arg);
1392
1393   info.arg = arg;
1394
1395   map_breakpoint_numbers (arg, do_map_commands_command, &info);
1396
1397   if (info.cmd == NULL)
1398     error (_("No breakpoints specified."));
1399
1400   do_cleanups (cleanups);
1401 }
1402
1403 static void
1404 commands_command (char *arg, int from_tty)
1405 {
1406   commands_command_1 (arg, from_tty, NULL);
1407 }
1408
1409 /* Like commands_command, but instead of reading the commands from
1410    input stream, takes them from an already parsed command structure.
1411
1412    This is used by cli-script.c to DTRT with breakpoint commands
1413    that are part of if and while bodies.  */
1414 enum command_control_type
1415 commands_from_control_command (char *arg, struct command_line *cmd)
1416 {
1417   commands_command_1 (arg, 0, cmd);
1418   return simple_control;
1419 }
1420
1421 /* Return non-zero if BL->TARGET_INFO contains valid information.  */
1422
1423 static int
1424 bp_location_has_shadow (struct bp_location *bl)
1425 {
1426   if (bl->loc_type != bp_loc_software_breakpoint)
1427     return 0;
1428   if (!bl->inserted)
1429     return 0;
1430   if (bl->target_info.shadow_len == 0)
1431     /* BL isn't valid, or doesn't shadow memory.  */
1432     return 0;
1433   return 1;
1434 }
1435
1436 /* Update BUF, which is LEN bytes read from the target address MEMADDR,
1437    by replacing any memory breakpoints with their shadowed contents.
1438
1439    If READBUF is not NULL, this buffer must not overlap with any of
1440    the breakpoint location's shadow_contents buffers.  Otherwise,
1441    a failed assertion internal error will be raised.
1442
1443    The range of shadowed area by each bp_location is:
1444      bl->address - bp_location_placed_address_before_address_max
1445      up to bl->address + bp_location_shadow_len_after_address_max
1446    The range we were requested to resolve shadows for is:
1447      memaddr ... memaddr + len
1448    Thus the safe cutoff boundaries for performance optimization are
1449      memaddr + len <= (bl->address
1450                        - bp_location_placed_address_before_address_max)
1451    and:
1452      bl->address + bp_location_shadow_len_after_address_max <= memaddr  */
1453
1454 void
1455 breakpoint_xfer_memory (gdb_byte *readbuf, gdb_byte *writebuf,
1456                         const gdb_byte *writebuf_org,
1457                         ULONGEST memaddr, LONGEST len)
1458 {
1459   /* Left boundary, right boundary and median element of our binary
1460      search.  */
1461   unsigned bc_l, bc_r, bc;
1462
1463   /* Find BC_L which is a leftmost element which may affect BUF
1464      content.  It is safe to report lower value but a failure to
1465      report higher one.  */
1466
1467   bc_l = 0;
1468   bc_r = bp_location_count;
1469   while (bc_l + 1 < bc_r)
1470     {
1471       struct bp_location *bl;
1472
1473       bc = (bc_l + bc_r) / 2;
1474       bl = bp_location[bc];
1475
1476       /* Check first BL->ADDRESS will not overflow due to the added
1477          constant.  Then advance the left boundary only if we are sure
1478          the BC element can in no way affect the BUF content (MEMADDR
1479          to MEMADDR + LEN range).
1480
1481          Use the BP_LOCATION_SHADOW_LEN_AFTER_ADDRESS_MAX safety
1482          offset so that we cannot miss a breakpoint with its shadow
1483          range tail still reaching MEMADDR.  */
1484
1485       if ((bl->address + bp_location_shadow_len_after_address_max
1486            >= bl->address)
1487           && (bl->address + bp_location_shadow_len_after_address_max
1488               <= memaddr))
1489         bc_l = bc;
1490       else
1491         bc_r = bc;
1492     }
1493
1494   /* Due to the binary search above, we need to make sure we pick the
1495      first location that's at BC_L's address.  E.g., if there are
1496      multiple locations at the same address, BC_L may end up pointing
1497      at a duplicate location, and miss the "master"/"inserted"
1498      location.  Say, given locations L1, L2 and L3 at addresses A and
1499      B:
1500
1501       L1@A, L2@A, L3@B, ...
1502
1503      BC_L could end up pointing at location L2, while the "master"
1504      location could be L1.  Since the `loc->inserted' flag is only set
1505      on "master" locations, we'd forget to restore the shadow of L1
1506      and L2.  */
1507   while (bc_l > 0
1508          && bp_location[bc_l]->address == bp_location[bc_l - 1]->address)
1509     bc_l--;
1510
1511   /* Now do full processing of the found relevant range of elements.  */
1512
1513   for (bc = bc_l; bc < bp_location_count; bc++)
1514   {
1515     struct bp_location *bl = bp_location[bc];
1516     CORE_ADDR bp_addr = 0;
1517     int bp_size = 0;
1518     int bptoffset = 0;
1519
1520     /* bp_location array has BL->OWNER always non-NULL.  */
1521     if (bl->owner->type == bp_none)
1522       warning (_("reading through apparently deleted breakpoint #%d?"),
1523                bl->owner->number);
1524
1525     /* Performance optimization: any further element can no longer affect BUF
1526        content.  */
1527
1528     if (bl->address >= bp_location_placed_address_before_address_max
1529         && memaddr + len <= (bl->address
1530                              - bp_location_placed_address_before_address_max))
1531       break;
1532
1533     if (!bp_location_has_shadow (bl))
1534       continue;
1535     if (!breakpoint_address_match (bl->target_info.placed_address_space, 0,
1536                                    current_program_space->aspace, 0))
1537       continue;
1538
1539     /* Addresses and length of the part of the breakpoint that
1540        we need to copy.  */
1541     bp_addr = bl->target_info.placed_address;
1542     bp_size = bl->target_info.shadow_len;
1543
1544     if (bp_addr + bp_size <= memaddr)
1545       /* The breakpoint is entirely before the chunk of memory we
1546          are reading.  */
1547       continue;
1548
1549     if (bp_addr >= memaddr + len)
1550       /* The breakpoint is entirely after the chunk of memory we are
1551          reading.  */
1552       continue;
1553
1554     /* Offset within shadow_contents.  */
1555     if (bp_addr < memaddr)
1556       {
1557         /* Only copy the second part of the breakpoint.  */
1558         bp_size -= memaddr - bp_addr;
1559         bptoffset = memaddr - bp_addr;
1560         bp_addr = memaddr;
1561       }
1562
1563     if (bp_addr + bp_size > memaddr + len)
1564       {
1565         /* Only copy the first part of the breakpoint.  */
1566         bp_size -= (bp_addr + bp_size) - (memaddr + len);
1567       }
1568
1569     if (readbuf != NULL)
1570       {
1571         /* Verify that the readbuf buffer does not overlap with
1572            the shadow_contents buffer.  */
1573         gdb_assert (bl->target_info.shadow_contents >= readbuf + len
1574                     || readbuf >= (bl->target_info.shadow_contents
1575                                    + bl->target_info.shadow_len));
1576
1577         /* Update the read buffer with this inserted breakpoint's
1578            shadow.  */
1579         memcpy (readbuf + bp_addr - memaddr,
1580                 bl->target_info.shadow_contents + bptoffset, bp_size);
1581       }
1582     else
1583       {
1584         struct gdbarch *gdbarch = bl->gdbarch;
1585         const unsigned char *bp;
1586         CORE_ADDR placed_address = bl->target_info.placed_address;
1587         int placed_size = bl->target_info.placed_size;
1588
1589         /* Update the shadow with what we want to write to memory.  */
1590         memcpy (bl->target_info.shadow_contents + bptoffset,
1591                 writebuf_org + bp_addr - memaddr, bp_size);
1592
1593         /* Determine appropriate breakpoint contents and size for this
1594            address.  */
1595         bp = gdbarch_breakpoint_from_pc (gdbarch, &placed_address, &placed_size);
1596
1597         /* Update the final write buffer with this inserted
1598            breakpoint's INSN.  */
1599         memcpy (writebuf + bp_addr - memaddr, bp + bptoffset, bp_size);
1600       }
1601   }
1602 }
1603 \f
1604
1605 /* Return true if BPT is either a software breakpoint or a hardware
1606    breakpoint.  */
1607
1608 int
1609 is_breakpoint (const struct breakpoint *bpt)
1610 {
1611   return (bpt->type == bp_breakpoint
1612           || bpt->type == bp_hardware_breakpoint
1613           || bpt->type == bp_dprintf);
1614 }
1615
1616 /* Return true if BPT is of any hardware watchpoint kind.  */
1617
1618 static int
1619 is_hardware_watchpoint (const struct breakpoint *bpt)
1620 {
1621   return (bpt->type == bp_hardware_watchpoint
1622           || bpt->type == bp_read_watchpoint
1623           || bpt->type == bp_access_watchpoint);
1624 }
1625
1626 /* Return true if BPT is of any watchpoint kind, hardware or
1627    software.  */
1628
1629 int
1630 is_watchpoint (const struct breakpoint *bpt)
1631 {
1632   return (is_hardware_watchpoint (bpt)
1633           || bpt->type == bp_watchpoint);
1634 }
1635
1636 /* Returns true if the current thread and its running state are safe
1637    to evaluate or update watchpoint B.  Watchpoints on local
1638    expressions need to be evaluated in the context of the thread that
1639    was current when the watchpoint was created, and, that thread needs
1640    to be stopped to be able to select the correct frame context.
1641    Watchpoints on global expressions can be evaluated on any thread,
1642    and in any state.  It is presently left to the target allowing
1643    memory accesses when threads are running.  */
1644
1645 static int
1646 watchpoint_in_thread_scope (struct watchpoint *b)
1647 {
1648   return (b->base.pspace == current_program_space
1649           && (ptid_equal (b->watchpoint_thread, null_ptid)
1650               || (ptid_equal (inferior_ptid, b->watchpoint_thread)
1651                   && !is_executing (inferior_ptid))));
1652 }
1653
1654 /* Set watchpoint B to disp_del_at_next_stop, even including its possible
1655    associated bp_watchpoint_scope breakpoint.  */
1656
1657 static void
1658 watchpoint_del_at_next_stop (struct watchpoint *w)
1659 {
1660   struct breakpoint *b = &w->base;
1661
1662   if (b->related_breakpoint != b)
1663     {
1664       gdb_assert (b->related_breakpoint->type == bp_watchpoint_scope);
1665       gdb_assert (b->related_breakpoint->related_breakpoint == b);
1666       b->related_breakpoint->disposition = disp_del_at_next_stop;
1667       b->related_breakpoint->related_breakpoint = b->related_breakpoint;
1668       b->related_breakpoint = b;
1669     }
1670   b->disposition = disp_del_at_next_stop;
1671 }
1672
1673 /* Assuming that B is a watchpoint:
1674    - Reparse watchpoint expression, if REPARSE is non-zero
1675    - Evaluate expression and store the result in B->val
1676    - Evaluate the condition if there is one, and store the result
1677      in b->loc->cond.
1678    - Update the list of values that must be watched in B->loc.
1679
1680    If the watchpoint disposition is disp_del_at_next_stop, then do
1681    nothing.  If this is local watchpoint that is out of scope, delete
1682    it.
1683
1684    Even with `set breakpoint always-inserted on' the watchpoints are
1685    removed + inserted on each stop here.  Normal breakpoints must
1686    never be removed because they might be missed by a running thread
1687    when debugging in non-stop mode.  On the other hand, hardware
1688    watchpoints (is_hardware_watchpoint; processed here) are specific
1689    to each LWP since they are stored in each LWP's hardware debug
1690    registers.  Therefore, such LWP must be stopped first in order to
1691    be able to modify its hardware watchpoints.
1692
1693    Hardware watchpoints must be reset exactly once after being
1694    presented to the user.  It cannot be done sooner, because it would
1695    reset the data used to present the watchpoint hit to the user.  And
1696    it must not be done later because it could display the same single
1697    watchpoint hit during multiple GDB stops.  Note that the latter is
1698    relevant only to the hardware watchpoint types bp_read_watchpoint
1699    and bp_access_watchpoint.  False hit by bp_hardware_watchpoint is
1700    not user-visible - its hit is suppressed if the memory content has
1701    not changed.
1702
1703    The following constraints influence the location where we can reset
1704    hardware watchpoints:
1705
1706    * target_stopped_by_watchpoint and target_stopped_data_address are
1707      called several times when GDB stops.
1708
1709    [linux] 
1710    * Multiple hardware watchpoints can be hit at the same time,
1711      causing GDB to stop.  GDB only presents one hardware watchpoint
1712      hit at a time as the reason for stopping, and all the other hits
1713      are presented later, one after the other, each time the user
1714      requests the execution to be resumed.  Execution is not resumed
1715      for the threads still having pending hit event stored in
1716      LWP_INFO->STATUS.  While the watchpoint is already removed from
1717      the inferior on the first stop the thread hit event is kept being
1718      reported from its cached value by linux_nat_stopped_data_address
1719      until the real thread resume happens after the watchpoint gets
1720      presented and thus its LWP_INFO->STATUS gets reset.
1721
1722    Therefore the hardware watchpoint hit can get safely reset on the
1723    watchpoint removal from inferior.  */
1724
1725 static void
1726 update_watchpoint (struct watchpoint *b, int reparse)
1727 {
1728   int within_current_scope;
1729   struct frame_id saved_frame_id;
1730   int frame_saved;
1731
1732   /* If this is a local watchpoint, we only want to check if the
1733      watchpoint frame is in scope if the current thread is the thread
1734      that was used to create the watchpoint.  */
1735   if (!watchpoint_in_thread_scope (b))
1736     return;
1737
1738   if (b->base.disposition == disp_del_at_next_stop)
1739     return;
1740  
1741   frame_saved = 0;
1742
1743   /* Determine if the watchpoint is within scope.  */
1744   if (b->exp_valid_block == NULL)
1745     within_current_scope = 1;
1746   else
1747     {
1748       struct frame_info *fi = get_current_frame ();
1749       struct gdbarch *frame_arch = get_frame_arch (fi);
1750       CORE_ADDR frame_pc = get_frame_pc (fi);
1751
1752       /* If we're in a function epilogue, unwinding may not work
1753          properly, so do not attempt to recreate locations at this
1754          point.  See similar comments in watchpoint_check.  */
1755       if (gdbarch_in_function_epilogue_p (frame_arch, frame_pc))
1756         return;
1757
1758       /* Save the current frame's ID so we can restore it after
1759          evaluating the watchpoint expression on its own frame.  */
1760       /* FIXME drow/2003-09-09: It would be nice if evaluate_expression
1761          took a frame parameter, so that we didn't have to change the
1762          selected frame.  */
1763       frame_saved = 1;
1764       saved_frame_id = get_frame_id (get_selected_frame (NULL));
1765
1766       fi = frame_find_by_id (b->watchpoint_frame);
1767       within_current_scope = (fi != NULL);
1768       if (within_current_scope)
1769         select_frame (fi);
1770     }
1771
1772   /* We don't free locations.  They are stored in the bp_location array
1773      and update_global_location_list will eventually delete them and
1774      remove breakpoints if needed.  */
1775   b->base.loc = NULL;
1776
1777   if (within_current_scope && reparse)
1778     {
1779       const char *s;
1780
1781       if (b->exp)
1782         {
1783           xfree (b->exp);
1784           b->exp = NULL;
1785         }
1786       s = b->exp_string_reparse ? b->exp_string_reparse : b->exp_string;
1787       b->exp = parse_exp_1 (&s, 0, b->exp_valid_block, 0);
1788       /* If the meaning of expression itself changed, the old value is
1789          no longer relevant.  We don't want to report a watchpoint hit
1790          to the user when the old value and the new value may actually
1791          be completely different objects.  */
1792       value_free (b->val);
1793       b->val = NULL;
1794       b->val_valid = 0;
1795
1796       /* Note that unlike with breakpoints, the watchpoint's condition
1797          expression is stored in the breakpoint object, not in the
1798          locations (re)created below.  */
1799       if (b->base.cond_string != NULL)
1800         {
1801           if (b->cond_exp != NULL)
1802             {
1803               xfree (b->cond_exp);
1804               b->cond_exp = NULL;
1805             }
1806
1807           s = b->base.cond_string;
1808           b->cond_exp = parse_exp_1 (&s, 0, b->cond_exp_valid_block, 0);
1809         }
1810     }
1811
1812   /* If we failed to parse the expression, for example because
1813      it refers to a global variable in a not-yet-loaded shared library,
1814      don't try to insert watchpoint.  We don't automatically delete
1815      such watchpoint, though, since failure to parse expression
1816      is different from out-of-scope watchpoint.  */
1817   if ( !target_has_execution)
1818     {
1819       /* Without execution, memory can't change.  No use to try and
1820          set watchpoint locations.  The watchpoint will be reset when
1821          the target gains execution, through breakpoint_re_set.  */
1822     }
1823   else if (within_current_scope && b->exp)
1824     {
1825       int pc = 0;
1826       struct value *val_chain, *v, *result, *next;
1827       struct program_space *frame_pspace;
1828
1829       fetch_subexp_value (b->exp, &pc, &v, &result, &val_chain);
1830
1831       /* Avoid setting b->val if it's already set.  The meaning of
1832          b->val is 'the last value' user saw, and we should update
1833          it only if we reported that last value to user.  As it
1834          happens, the code that reports it updates b->val directly.
1835          We don't keep track of the memory value for masked
1836          watchpoints.  */
1837       if (!b->val_valid && !is_masked_watchpoint (&b->base))
1838         {
1839           b->val = v;
1840           b->val_valid = 1;
1841         }
1842
1843       frame_pspace = get_frame_program_space (get_selected_frame (NULL));
1844
1845       /* Look at each value on the value chain.  */
1846       for (v = val_chain; v; v = value_next (v))
1847         {
1848           /* If it's a memory location, and GDB actually needed
1849              its contents to evaluate the expression, then we
1850              must watch it.  If the first value returned is
1851              still lazy, that means an error occurred reading it;
1852              watch it anyway in case it becomes readable.  */
1853           if (VALUE_LVAL (v) == lval_memory
1854               && (v == val_chain || ! value_lazy (v)))
1855             {
1856               struct type *vtype = check_typedef (value_type (v));
1857
1858               /* We only watch structs and arrays if user asked
1859                  for it explicitly, never if they just happen to
1860                  appear in the middle of some value chain.  */
1861               if (v == result
1862                   || (TYPE_CODE (vtype) != TYPE_CODE_STRUCT
1863                       && TYPE_CODE (vtype) != TYPE_CODE_ARRAY))
1864                 {
1865                   CORE_ADDR addr;
1866                   int type;
1867                   struct bp_location *loc, **tmp;
1868
1869                   addr = value_address (v);
1870                   type = hw_write;
1871                   if (b->base.type == bp_read_watchpoint)
1872                     type = hw_read;
1873                   else if (b->base.type == bp_access_watchpoint)
1874                     type = hw_access;
1875
1876                   loc = allocate_bp_location (&b->base);
1877                   for (tmp = &(b->base.loc); *tmp != NULL; tmp = &((*tmp)->next))
1878                     ;
1879                   *tmp = loc;
1880                   loc->gdbarch = get_type_arch (value_type (v));
1881
1882                   loc->pspace = frame_pspace;
1883                   loc->address = addr;
1884                   loc->length = TYPE_LENGTH (value_type (v));
1885                   loc->watchpoint_type = type;
1886                 }
1887             }
1888         }
1889
1890       /* Change the type of breakpoint between hardware assisted or
1891          an ordinary watchpoint depending on the hardware support
1892          and free hardware slots.  REPARSE is set when the inferior
1893          is started.  */
1894       if (reparse)
1895         {
1896           int reg_cnt;
1897           enum bp_loc_type loc_type;
1898           struct bp_location *bl;
1899
1900           reg_cnt = can_use_hardware_watchpoint (val_chain);
1901
1902           if (reg_cnt)
1903             {
1904               int i, target_resources_ok, other_type_used;
1905               enum bptype type;
1906
1907               /* Use an exact watchpoint when there's only one memory region to be
1908                  watched, and only one debug register is needed to watch it.  */
1909               b->exact = target_exact_watchpoints && reg_cnt == 1;
1910
1911               /* We need to determine how many resources are already
1912                  used for all other hardware watchpoints plus this one
1913                  to see if we still have enough resources to also fit
1914                  this watchpoint in as well.  */
1915
1916               /* If this is a software watchpoint, we try to turn it
1917                  to a hardware one -- count resources as if B was of
1918                  hardware watchpoint type.  */
1919               type = b->base.type;
1920               if (type == bp_watchpoint)
1921                 type = bp_hardware_watchpoint;
1922
1923               /* This watchpoint may or may not have been placed on
1924                  the list yet at this point (it won't be in the list
1925                  if we're trying to create it for the first time,
1926                  through watch_command), so always account for it
1927                  manually.  */
1928
1929               /* Count resources used by all watchpoints except B.  */
1930               i = hw_watchpoint_used_count_others (&b->base, type, &other_type_used);
1931
1932               /* Add in the resources needed for B.  */
1933               i += hw_watchpoint_use_count (&b->base);
1934
1935               target_resources_ok
1936                 = target_can_use_hardware_watchpoint (type, i, other_type_used);
1937               if (target_resources_ok <= 0)
1938                 {
1939                   int sw_mode = b->base.ops->works_in_software_mode (&b->base);
1940
1941                   if (target_resources_ok == 0 && !sw_mode)
1942                     error (_("Target does not support this type of "
1943                              "hardware watchpoint."));
1944                   else if (target_resources_ok < 0 && !sw_mode)
1945                     error (_("There are not enough available hardware "
1946                              "resources for this watchpoint."));
1947
1948                   /* Downgrade to software watchpoint.  */
1949                   b->base.type = bp_watchpoint;
1950                 }
1951               else
1952                 {
1953                   /* If this was a software watchpoint, we've just
1954                      found we have enough resources to turn it to a
1955                      hardware watchpoint.  Otherwise, this is a
1956                      nop.  */
1957                   b->base.type = type;
1958                 }
1959             }
1960           else if (!b->base.ops->works_in_software_mode (&b->base))
1961             error (_("Expression cannot be implemented with "
1962                      "read/access watchpoint."));
1963           else
1964             b->base.type = bp_watchpoint;
1965
1966           loc_type = (b->base.type == bp_watchpoint? bp_loc_other
1967                       : bp_loc_hardware_watchpoint);
1968           for (bl = b->base.loc; bl; bl = bl->next)
1969             bl->loc_type = loc_type;
1970         }
1971
1972       for (v = val_chain; v; v = next)
1973         {
1974           next = value_next (v);
1975           if (v != b->val)
1976             value_free (v);
1977         }
1978
1979       /* If a software watchpoint is not watching any memory, then the
1980          above left it without any location set up.  But,
1981          bpstat_stop_status requires a location to be able to report
1982          stops, so make sure there's at least a dummy one.  */
1983       if (b->base.type == bp_watchpoint && b->base.loc == NULL)
1984         {
1985           struct breakpoint *base = &b->base;
1986           base->loc = allocate_bp_location (base);
1987           base->loc->pspace = frame_pspace;
1988           base->loc->address = -1;
1989           base->loc->length = -1;
1990           base->loc->watchpoint_type = -1;
1991         }
1992     }
1993   else if (!within_current_scope)
1994     {
1995       printf_filtered (_("\
1996 Watchpoint %d deleted because the program has left the block\n\
1997 in which its expression is valid.\n"),
1998                        b->base.number);
1999       watchpoint_del_at_next_stop (b);
2000     }
2001
2002   /* Restore the selected frame.  */
2003   if (frame_saved)
2004     select_frame (frame_find_by_id (saved_frame_id));
2005 }
2006
2007
2008 /* Returns 1 iff breakpoint location should be
2009    inserted in the inferior.  We don't differentiate the type of BL's owner
2010    (breakpoint vs. tracepoint), although insert_location in tracepoint's
2011    breakpoint_ops is not defined, because in insert_bp_location,
2012    tracepoint's insert_location will not be called.  */
2013 static int
2014 should_be_inserted (struct bp_location *bl)
2015 {
2016   if (bl->owner == NULL || !breakpoint_enabled (bl->owner))
2017     return 0;
2018
2019   if (bl->owner->disposition == disp_del_at_next_stop)
2020     return 0;
2021
2022   if (!bl->enabled || bl->shlib_disabled || bl->duplicate)
2023     return 0;
2024
2025   if (user_breakpoint_p (bl->owner) && bl->pspace->executing_startup)
2026     return 0;
2027
2028   /* This is set for example, when we're attached to the parent of a
2029      vfork, and have detached from the child.  The child is running
2030      free, and we expect it to do an exec or exit, at which point the
2031      OS makes the parent schedulable again (and the target reports
2032      that the vfork is done).  Until the child is done with the shared
2033      memory region, do not insert breakpoints in the parent, otherwise
2034      the child could still trip on the parent's breakpoints.  Since
2035      the parent is blocked anyway, it won't miss any breakpoint.  */
2036   if (bl->pspace->breakpoints_not_allowed)
2037     return 0;
2038
2039   return 1;
2040 }
2041
2042 /* Same as should_be_inserted but does the check assuming
2043    that the location is not duplicated.  */
2044
2045 static int
2046 unduplicated_should_be_inserted (struct bp_location *bl)
2047 {
2048   int result;
2049   const int save_duplicate = bl->duplicate;
2050
2051   bl->duplicate = 0;
2052   result = should_be_inserted (bl);
2053   bl->duplicate = save_duplicate;
2054   return result;
2055 }
2056
2057 /* Parses a conditional described by an expression COND into an
2058    agent expression bytecode suitable for evaluation
2059    by the bytecode interpreter.  Return NULL if there was
2060    any error during parsing.  */
2061
2062 static struct agent_expr *
2063 parse_cond_to_aexpr (CORE_ADDR scope, struct expression *cond)
2064 {
2065   struct agent_expr *aexpr = NULL;
2066   volatile struct gdb_exception ex;
2067
2068   if (!cond)
2069     return NULL;
2070
2071   /* We don't want to stop processing, so catch any errors
2072      that may show up.  */
2073   TRY_CATCH (ex, RETURN_MASK_ERROR)
2074     {
2075       aexpr = gen_eval_for_expr (scope, cond);
2076     }
2077
2078   if (ex.reason < 0)
2079     {
2080       /* If we got here, it means the condition could not be parsed to a valid
2081          bytecode expression and thus can't be evaluated on the target's side.
2082          It's no use iterating through the conditions.  */
2083       return NULL;
2084     }
2085
2086   /* We have a valid agent expression.  */
2087   return aexpr;
2088 }
2089
2090 /* Based on location BL, create a list of breakpoint conditions to be
2091    passed on to the target.  If we have duplicated locations with different
2092    conditions, we will add such conditions to the list.  The idea is that the
2093    target will evaluate the list of conditions and will only notify GDB when
2094    one of them is true.  */
2095
2096 static void
2097 build_target_condition_list (struct bp_location *bl)
2098 {
2099   struct bp_location **locp = NULL, **loc2p;
2100   int null_condition_or_parse_error = 0;
2101   int modified = bl->needs_update;
2102   struct bp_location *loc;
2103
2104   /* This is only meaningful if the target is
2105      evaluating conditions and if the user has
2106      opted for condition evaluation on the target's
2107      side.  */
2108   if (gdb_evaluates_breakpoint_condition_p ()
2109       || !target_supports_evaluation_of_breakpoint_conditions ())
2110     return;
2111
2112   /* Do a first pass to check for locations with no assigned
2113      conditions or conditions that fail to parse to a valid agent expression
2114      bytecode.  If any of these happen, then it's no use to send conditions
2115      to the target since this location will always trigger and generate a
2116      response back to GDB.  */
2117   ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2118     {
2119       loc = (*loc2p);
2120       if (is_breakpoint (loc->owner) && loc->pspace->num == bl->pspace->num)
2121         {
2122           if (modified)
2123             {
2124               struct agent_expr *aexpr;
2125
2126               /* Re-parse the conditions since something changed.  In that
2127                  case we already freed the condition bytecodes (see
2128                  force_breakpoint_reinsertion).  We just
2129                  need to parse the condition to bytecodes again.  */
2130               aexpr = parse_cond_to_aexpr (bl->address, loc->cond);
2131               loc->cond_bytecode = aexpr;
2132
2133               /* Check if we managed to parse the conditional expression
2134                  correctly.  If not, we will not send this condition
2135                  to the target.  */
2136               if (aexpr)
2137                 continue;
2138             }
2139
2140           /* If we have a NULL bytecode expression, it means something
2141              went wrong or we have a null condition expression.  */
2142           if (!loc->cond_bytecode)
2143             {
2144               null_condition_or_parse_error = 1;
2145               break;
2146             }
2147         }
2148     }
2149
2150   /* If any of these happened, it means we will have to evaluate the conditions
2151      for the location's address on gdb's side.  It is no use keeping bytecodes
2152      for all the other duplicate locations, thus we free all of them here.
2153
2154      This is so we have a finer control over which locations' conditions are
2155      being evaluated by GDB or the remote stub.  */
2156   if (null_condition_or_parse_error)
2157     {
2158       ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2159         {
2160           loc = (*loc2p);
2161           if (is_breakpoint (loc->owner) && loc->pspace->num == bl->pspace->num)
2162             {
2163               /* Only go as far as the first NULL bytecode is
2164                  located.  */
2165               if (!loc->cond_bytecode)
2166                 return;
2167
2168               free_agent_expr (loc->cond_bytecode);
2169               loc->cond_bytecode = NULL;
2170             }
2171         }
2172     }
2173
2174   /* No NULL conditions or failed bytecode generation.  Build a condition list
2175      for this location's address.  */
2176   ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2177     {
2178       loc = (*loc2p);
2179       if (loc->cond
2180           && is_breakpoint (loc->owner)
2181           && loc->pspace->num == bl->pspace->num
2182           && loc->owner->enable_state == bp_enabled
2183           && loc->enabled)
2184         /* Add the condition to the vector.  This will be used later to send the
2185            conditions to the target.  */
2186         VEC_safe_push (agent_expr_p, bl->target_info.conditions,
2187                        loc->cond_bytecode);
2188     }
2189
2190   return;
2191 }
2192
2193 /* Parses a command described by string CMD into an agent expression
2194    bytecode suitable for evaluation by the bytecode interpreter.
2195    Return NULL if there was any error during parsing.  */
2196
2197 static struct agent_expr *
2198 parse_cmd_to_aexpr (CORE_ADDR scope, char *cmd)
2199 {
2200   struct cleanup *old_cleanups = 0;
2201   struct expression *expr, **argvec;
2202   struct agent_expr *aexpr = NULL;
2203   volatile struct gdb_exception ex;
2204   const char *cmdrest;
2205   const char *format_start, *format_end;
2206   struct format_piece *fpieces;
2207   int nargs;
2208   struct gdbarch *gdbarch = get_current_arch ();
2209
2210   if (!cmd)
2211     return NULL;
2212
2213   cmdrest = cmd;
2214
2215   if (*cmdrest == ',')
2216     ++cmdrest;
2217   cmdrest = skip_spaces_const (cmdrest);
2218
2219   if (*cmdrest++ != '"')
2220     error (_("No format string following the location"));
2221
2222   format_start = cmdrest;
2223
2224   fpieces = parse_format_string (&cmdrest);
2225
2226   old_cleanups = make_cleanup (free_format_pieces_cleanup, &fpieces);
2227
2228   format_end = cmdrest;
2229
2230   if (*cmdrest++ != '"')
2231     error (_("Bad format string, non-terminated '\"'."));
2232   
2233   cmdrest = skip_spaces_const (cmdrest);
2234
2235   if (!(*cmdrest == ',' || *cmdrest == '\0'))
2236     error (_("Invalid argument syntax"));
2237
2238   if (*cmdrest == ',')
2239     cmdrest++;
2240   cmdrest = skip_spaces_const (cmdrest);
2241
2242   /* For each argument, make an expression.  */
2243
2244   argvec = (struct expression **) alloca (strlen (cmd)
2245                                          * sizeof (struct expression *));
2246
2247   nargs = 0;
2248   while (*cmdrest != '\0')
2249     {
2250       const char *cmd1;
2251
2252       cmd1 = cmdrest;
2253       expr = parse_exp_1 (&cmd1, scope, block_for_pc (scope), 1);
2254       argvec[nargs++] = expr;
2255       cmdrest = cmd1;
2256       if (*cmdrest == ',')
2257         ++cmdrest;
2258     }
2259
2260   /* We don't want to stop processing, so catch any errors
2261      that may show up.  */
2262   TRY_CATCH (ex, RETURN_MASK_ERROR)
2263     {
2264       aexpr = gen_printf (scope, gdbarch, 0, 0,
2265                           format_start, format_end - format_start,
2266                           fpieces, nargs, argvec);
2267     }
2268
2269   if (ex.reason < 0)
2270     {
2271       /* If we got here, it means the command could not be parsed to a valid
2272          bytecode expression and thus can't be evaluated on the target's side.
2273          It's no use iterating through the other commands.  */
2274       return NULL;
2275     }
2276
2277   do_cleanups (old_cleanups);
2278
2279   /* We have a valid agent expression, return it.  */
2280   return aexpr;
2281 }
2282
2283 /* Based on location BL, create a list of breakpoint commands to be
2284    passed on to the target.  If we have duplicated locations with
2285    different commands, we will add any such to the list.  */
2286
2287 static void
2288 build_target_command_list (struct bp_location *bl)
2289 {
2290   struct bp_location **locp = NULL, **loc2p;
2291   int null_command_or_parse_error = 0;
2292   int modified = bl->needs_update;
2293   struct bp_location *loc;
2294
2295   /* For now, limit to agent-style dprintf breakpoints.  */
2296   if (bl->owner->type != bp_dprintf
2297       || strcmp (dprintf_style, dprintf_style_agent) != 0)
2298     return;
2299
2300   if (!target_can_run_breakpoint_commands ())
2301     return;
2302
2303   /* Do a first pass to check for locations with no assigned
2304      conditions or conditions that fail to parse to a valid agent expression
2305      bytecode.  If any of these happen, then it's no use to send conditions
2306      to the target since this location will always trigger and generate a
2307      response back to GDB.  */
2308   ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2309     {
2310       loc = (*loc2p);
2311       if (is_breakpoint (loc->owner) && loc->pspace->num == bl->pspace->num)
2312         {
2313           if (modified)
2314             {
2315               struct agent_expr *aexpr;
2316
2317               /* Re-parse the commands since something changed.  In that
2318                  case we already freed the command bytecodes (see
2319                  force_breakpoint_reinsertion).  We just
2320                  need to parse the command to bytecodes again.  */
2321               aexpr = parse_cmd_to_aexpr (bl->address,
2322                                           loc->owner->extra_string);
2323               loc->cmd_bytecode = aexpr;
2324
2325               if (!aexpr)
2326                 continue;
2327             }
2328
2329           /* If we have a NULL bytecode expression, it means something
2330              went wrong or we have a null command expression.  */
2331           if (!loc->cmd_bytecode)
2332             {
2333               null_command_or_parse_error = 1;
2334               break;
2335             }
2336         }
2337     }
2338
2339   /* If anything failed, then we're not doing target-side commands,
2340      and so clean up.  */
2341   if (null_command_or_parse_error)
2342     {
2343       ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2344         {
2345           loc = (*loc2p);
2346           if (is_breakpoint (loc->owner)
2347               && loc->pspace->num == bl->pspace->num)
2348             {
2349               /* Only go as far as the first NULL bytecode is
2350                  located.  */
2351               if (!loc->cond_bytecode)
2352                 return;
2353
2354               free_agent_expr (loc->cond_bytecode);
2355               loc->cond_bytecode = NULL;
2356             }
2357         }
2358     }
2359
2360   /* No NULL commands or failed bytecode generation.  Build a command list
2361      for this location's address.  */
2362   ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2363     {
2364       loc = (*loc2p);
2365       if (loc->owner->extra_string
2366           && is_breakpoint (loc->owner)
2367           && loc->pspace->num == bl->pspace->num
2368           && loc->owner->enable_state == bp_enabled
2369           && loc->enabled)
2370         /* Add the command to the vector.  This will be used later
2371            to send the commands to the target.  */
2372         VEC_safe_push (agent_expr_p, bl->target_info.tcommands,
2373                        loc->cmd_bytecode);
2374     }
2375
2376   bl->target_info.persist = 0;
2377   /* Maybe flag this location as persistent.  */
2378   if (bl->owner->type == bp_dprintf && disconnected_dprintf)
2379     bl->target_info.persist = 1;
2380 }
2381
2382 /* Insert a low-level "breakpoint" of some type.  BL is the breakpoint
2383    location.  Any error messages are printed to TMP_ERROR_STREAM; and
2384    DISABLED_BREAKS, and HW_BREAKPOINT_ERROR are used to report problems.
2385    Returns 0 for success, 1 if the bp_location type is not supported or
2386    -1 for failure.
2387
2388    NOTE drow/2003-09-09: This routine could be broken down to an
2389    object-style method for each breakpoint or catchpoint type.  */
2390 static int
2391 insert_bp_location (struct bp_location *bl,
2392                     struct ui_file *tmp_error_stream,
2393                     int *disabled_breaks,
2394                     int *hw_breakpoint_error,
2395                     int *hw_bp_error_explained_already)
2396 {
2397   int val = 0;
2398   char *hw_bp_err_string = NULL;
2399   struct gdb_exception e;
2400
2401   if (!should_be_inserted (bl) || (bl->inserted && !bl->needs_update))
2402     return 0;
2403
2404   /* Note we don't initialize bl->target_info, as that wipes out
2405      the breakpoint location's shadow_contents if the breakpoint
2406      is still inserted at that location.  This in turn breaks
2407      target_read_memory which depends on these buffers when
2408      a memory read is requested at the breakpoint location:
2409      Once the target_info has been wiped, we fail to see that
2410      we have a breakpoint inserted at that address and thus
2411      read the breakpoint instead of returning the data saved in
2412      the breakpoint location's shadow contents.  */
2413   bl->target_info.placed_address = bl->address;
2414   bl->target_info.placed_address_space = bl->pspace->aspace;
2415   bl->target_info.length = bl->length;
2416
2417   /* When working with target-side conditions, we must pass all the conditions
2418      for the same breakpoint address down to the target since GDB will not
2419      insert those locations.  With a list of breakpoint conditions, the target
2420      can decide when to stop and notify GDB.  */
2421
2422   if (is_breakpoint (bl->owner))
2423     {
2424       build_target_condition_list (bl);
2425       build_target_command_list (bl);
2426       /* Reset the modification marker.  */
2427       bl->needs_update = 0;
2428     }
2429
2430   if (bl->loc_type == bp_loc_software_breakpoint
2431       || bl->loc_type == bp_loc_hardware_breakpoint)
2432     {
2433       if (bl->owner->type != bp_hardware_breakpoint)
2434         {
2435           /* If the explicitly specified breakpoint type
2436              is not hardware breakpoint, check the memory map to see
2437              if the breakpoint address is in read only memory or not.
2438
2439              Two important cases are:
2440              - location type is not hardware breakpoint, memory
2441              is readonly.  We change the type of the location to
2442              hardware breakpoint.
2443              - location type is hardware breakpoint, memory is
2444              read-write.  This means we've previously made the
2445              location hardware one, but then the memory map changed,
2446              so we undo.
2447              
2448              When breakpoints are removed, remove_breakpoints will use
2449              location types we've just set here, the only possible
2450              problem is that memory map has changed during running
2451              program, but it's not going to work anyway with current
2452              gdb.  */
2453           struct mem_region *mr 
2454             = lookup_mem_region (bl->target_info.placed_address);
2455           
2456           if (mr)
2457             {
2458               if (automatic_hardware_breakpoints)
2459                 {
2460                   enum bp_loc_type new_type;
2461                   
2462                   if (mr->attrib.mode != MEM_RW)
2463                     new_type = bp_loc_hardware_breakpoint;
2464                   else 
2465                     new_type = bp_loc_software_breakpoint;
2466                   
2467                   if (new_type != bl->loc_type)
2468                     {
2469                       static int said = 0;
2470
2471                       bl->loc_type = new_type;
2472                       if (!said)
2473                         {
2474                           fprintf_filtered (gdb_stdout,
2475                                             _("Note: automatically using "
2476                                               "hardware breakpoints for "
2477                                               "read-only addresses.\n"));
2478                           said = 1;
2479                         }
2480                     }
2481                 }
2482               else if (bl->loc_type == bp_loc_software_breakpoint
2483                        && mr->attrib.mode != MEM_RW)        
2484                 warning (_("cannot set software breakpoint "
2485                            "at readonly address %s"),
2486                          paddress (bl->gdbarch, bl->address));
2487             }
2488         }
2489         
2490       /* First check to see if we have to handle an overlay.  */
2491       if (overlay_debugging == ovly_off
2492           || bl->section == NULL
2493           || !(section_is_overlay (bl->section)))
2494         {
2495           /* No overlay handling: just set the breakpoint.  */
2496           TRY_CATCH (e, RETURN_MASK_ALL)
2497             {
2498               val = bl->owner->ops->insert_location (bl);
2499             }
2500           if (e.reason < 0)
2501             {
2502               val = 1;
2503               hw_bp_err_string = (char *) e.message;
2504             }
2505         }
2506       else
2507         {
2508           /* This breakpoint is in an overlay section.
2509              Shall we set a breakpoint at the LMA?  */
2510           if (!overlay_events_enabled)
2511             {
2512               /* Yes -- overlay event support is not active, 
2513                  so we must try to set a breakpoint at the LMA.
2514                  This will not work for a hardware breakpoint.  */
2515               if (bl->loc_type == bp_loc_hardware_breakpoint)
2516                 warning (_("hardware breakpoint %d not supported in overlay!"),
2517                          bl->owner->number);
2518               else
2519                 {
2520                   CORE_ADDR addr = overlay_unmapped_address (bl->address,
2521                                                              bl->section);
2522                   /* Set a software (trap) breakpoint at the LMA.  */
2523                   bl->overlay_target_info = bl->target_info;
2524                   bl->overlay_target_info.placed_address = addr;
2525                   val = target_insert_breakpoint (bl->gdbarch,
2526                                                   &bl->overlay_target_info);
2527                   if (val != 0)
2528                     fprintf_unfiltered (tmp_error_stream,
2529                                         "Overlay breakpoint %d "
2530                                         "failed: in ROM?\n",
2531                                         bl->owner->number);
2532                 }
2533             }
2534           /* Shall we set a breakpoint at the VMA? */
2535           if (section_is_mapped (bl->section))
2536             {
2537               /* Yes.  This overlay section is mapped into memory.  */
2538               TRY_CATCH (e, RETURN_MASK_ALL)
2539                 {
2540                   val = bl->owner->ops->insert_location (bl);
2541                 }
2542               if (e.reason < 0)
2543                 {
2544                   val = 1;
2545                   hw_bp_err_string = (char *) e.message;
2546                 }
2547             }
2548           else
2549             {
2550               /* No.  This breakpoint will not be inserted.  
2551                  No error, but do not mark the bp as 'inserted'.  */
2552               return 0;
2553             }
2554         }
2555
2556       if (val)
2557         {
2558           /* Can't set the breakpoint.  */
2559           if (solib_name_from_address (bl->pspace, bl->address))
2560             {
2561               /* See also: disable_breakpoints_in_shlibs.  */
2562               val = 0;
2563               bl->shlib_disabled = 1;
2564               observer_notify_breakpoint_modified (bl->owner);
2565               if (!*disabled_breaks)
2566                 {
2567                   fprintf_unfiltered (tmp_error_stream, 
2568                                       "Cannot insert breakpoint %d.\n", 
2569                                       bl->owner->number);
2570                   fprintf_unfiltered (tmp_error_stream, 
2571                                       "Temporarily disabling shared "
2572                                       "library breakpoints:\n");
2573                 }
2574               *disabled_breaks = 1;
2575               fprintf_unfiltered (tmp_error_stream,
2576                                   "breakpoint #%d\n", bl->owner->number);
2577             }
2578           else
2579             {
2580               if (bl->loc_type == bp_loc_hardware_breakpoint)
2581                 {
2582                   *hw_breakpoint_error = 1;
2583                   *hw_bp_error_explained_already = hw_bp_err_string != NULL;
2584                   fprintf_unfiltered (tmp_error_stream,
2585                                       "Cannot insert hardware breakpoint %d%s",
2586                                       bl->owner->number, hw_bp_err_string ? ":" : ".\n");
2587                   if (hw_bp_err_string)
2588                     fprintf_unfiltered (tmp_error_stream, "%s.\n", hw_bp_err_string);
2589                 }
2590               else
2591                 {
2592                   fprintf_unfiltered (tmp_error_stream, 
2593                                       "Cannot insert breakpoint %d.\n", 
2594                                       bl->owner->number);
2595                   fprintf_filtered (tmp_error_stream, 
2596                                     "Error accessing memory address ");
2597                   fputs_filtered (paddress (bl->gdbarch, bl->address),
2598                                   tmp_error_stream);
2599                   fprintf_filtered (tmp_error_stream, ": %s.\n",
2600                                     safe_strerror (val));
2601                 }
2602
2603             }
2604         }
2605       else
2606         bl->inserted = 1;
2607
2608       return val;
2609     }
2610
2611   else if (bl->loc_type == bp_loc_hardware_watchpoint
2612            /* NOTE drow/2003-09-08: This state only exists for removing
2613               watchpoints.  It's not clear that it's necessary...  */
2614            && bl->owner->disposition != disp_del_at_next_stop)
2615     {
2616       gdb_assert (bl->owner->ops != NULL
2617                   && bl->owner->ops->insert_location != NULL);
2618
2619       val = bl->owner->ops->insert_location (bl);
2620
2621       /* If trying to set a read-watchpoint, and it turns out it's not
2622          supported, try emulating one with an access watchpoint.  */
2623       if (val == 1 && bl->watchpoint_type == hw_read)
2624         {
2625           struct bp_location *loc, **loc_temp;
2626
2627           /* But don't try to insert it, if there's already another
2628              hw_access location that would be considered a duplicate
2629              of this one.  */
2630           ALL_BP_LOCATIONS (loc, loc_temp)
2631             if (loc != bl
2632                 && loc->watchpoint_type == hw_access
2633                 && watchpoint_locations_match (bl, loc))
2634               {
2635                 bl->duplicate = 1;
2636                 bl->inserted = 1;
2637                 bl->target_info = loc->target_info;
2638                 bl->watchpoint_type = hw_access;
2639                 val = 0;
2640                 break;
2641               }
2642
2643           if (val == 1)
2644             {
2645               bl->watchpoint_type = hw_access;
2646               val = bl->owner->ops->insert_location (bl);
2647
2648               if (val)
2649                 /* Back to the original value.  */
2650                 bl->watchpoint_type = hw_read;
2651             }
2652         }
2653
2654       bl->inserted = (val == 0);
2655     }
2656
2657   else if (bl->owner->type == bp_catchpoint)
2658     {
2659       gdb_assert (bl->owner->ops != NULL
2660                   && bl->owner->ops->insert_location != NULL);
2661
2662       val = bl->owner->ops->insert_location (bl);
2663       if (val)
2664         {
2665           bl->owner->enable_state = bp_disabled;
2666
2667           if (val == 1)
2668             warning (_("\
2669 Error inserting catchpoint %d: Your system does not support this type\n\
2670 of catchpoint."), bl->owner->number);
2671           else
2672             warning (_("Error inserting catchpoint %d."), bl->owner->number);
2673         }
2674
2675       bl->inserted = (val == 0);
2676
2677       /* We've already printed an error message if there was a problem
2678          inserting this catchpoint, and we've disabled the catchpoint,
2679          so just return success.  */
2680       return 0;
2681     }
2682
2683   return 0;
2684 }
2685
2686 /* This function is called when program space PSPACE is about to be
2687    deleted.  It takes care of updating breakpoints to not reference
2688    PSPACE anymore.  */
2689
2690 void
2691 breakpoint_program_space_exit (struct program_space *pspace)
2692 {
2693   struct breakpoint *b, *b_temp;
2694   struct bp_location *loc, **loc_temp;
2695
2696   /* Remove any breakpoint that was set through this program space.  */
2697   ALL_BREAKPOINTS_SAFE (b, b_temp)
2698     {
2699       if (b->pspace == pspace)
2700         delete_breakpoint (b);
2701     }
2702
2703   /* Breakpoints set through other program spaces could have locations
2704      bound to PSPACE as well.  Remove those.  */
2705   ALL_BP_LOCATIONS (loc, loc_temp)
2706     {
2707       struct bp_location *tmp;
2708
2709       if (loc->pspace == pspace)
2710         {
2711           /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL.  */
2712           if (loc->owner->loc == loc)
2713             loc->owner->loc = loc->next;
2714           else
2715             for (tmp = loc->owner->loc; tmp->next != NULL; tmp = tmp->next)
2716               if (tmp->next == loc)
2717                 {
2718                   tmp->next = loc->next;
2719                   break;
2720                 }
2721         }
2722     }
2723
2724   /* Now update the global location list to permanently delete the
2725      removed locations above.  */
2726   update_global_location_list (0);
2727 }
2728
2729 /* Make sure all breakpoints are inserted in inferior.
2730    Throws exception on any error.
2731    A breakpoint that is already inserted won't be inserted
2732    again, so calling this function twice is safe.  */
2733 void
2734 insert_breakpoints (void)
2735 {
2736   struct breakpoint *bpt;
2737
2738   ALL_BREAKPOINTS (bpt)
2739     if (is_hardware_watchpoint (bpt))
2740       {
2741         struct watchpoint *w = (struct watchpoint *) bpt;
2742
2743         update_watchpoint (w, 0 /* don't reparse.  */);
2744       }
2745
2746   update_global_location_list (1);
2747
2748   /* update_global_location_list does not insert breakpoints when
2749      always_inserted_mode is not enabled.  Explicitly insert them
2750      now.  */
2751   if (!breakpoints_always_inserted_mode ())
2752     insert_breakpoint_locations ();
2753 }
2754
2755 /* Invoke CALLBACK for each of bp_location.  */
2756
2757 void
2758 iterate_over_bp_locations (walk_bp_location_callback callback)
2759 {
2760   struct bp_location *loc, **loc_tmp;
2761
2762   ALL_BP_LOCATIONS (loc, loc_tmp)
2763     {
2764       callback (loc, NULL);
2765     }
2766 }
2767
2768 /* This is used when we need to synch breakpoint conditions between GDB and the
2769    target.  It is the case with deleting and disabling of breakpoints when using
2770    always-inserted mode.  */
2771
2772 static void
2773 update_inserted_breakpoint_locations (void)
2774 {
2775   struct bp_location *bl, **blp_tmp;
2776   int error_flag = 0;
2777   int val = 0;
2778   int disabled_breaks = 0;
2779   int hw_breakpoint_error = 0;
2780   int hw_bp_details_reported = 0;
2781
2782   struct ui_file *tmp_error_stream = mem_fileopen ();
2783   struct cleanup *cleanups = make_cleanup_ui_file_delete (tmp_error_stream);
2784
2785   /* Explicitly mark the warning -- this will only be printed if
2786      there was an error.  */
2787   fprintf_unfiltered (tmp_error_stream, "Warning:\n");
2788
2789   save_current_space_and_thread ();
2790
2791   ALL_BP_LOCATIONS (bl, blp_tmp)
2792     {
2793       /* We only want to update software breakpoints and hardware
2794          breakpoints.  */
2795       if (!is_breakpoint (bl->owner))
2796         continue;
2797
2798       /* We only want to update locations that are already inserted
2799          and need updating.  This is to avoid unwanted insertion during
2800          deletion of breakpoints.  */
2801       if (!bl->inserted || (bl->inserted && !bl->needs_update))
2802         continue;
2803
2804       switch_to_program_space_and_thread (bl->pspace);
2805
2806       /* For targets that support global breakpoints, there's no need
2807          to select an inferior to insert breakpoint to.  In fact, even
2808          if we aren't attached to any process yet, we should still
2809          insert breakpoints.  */
2810       if (!gdbarch_has_global_breakpoints (target_gdbarch ())
2811           && ptid_equal (inferior_ptid, null_ptid))
2812         continue;
2813
2814       val = insert_bp_location (bl, tmp_error_stream, &disabled_breaks,
2815                                     &hw_breakpoint_error, &hw_bp_details_reported);
2816       if (val)
2817         error_flag = val;
2818     }
2819
2820   if (error_flag)
2821     {
2822       target_terminal_ours_for_output ();
2823       error_stream (tmp_error_stream);
2824     }
2825
2826   do_cleanups (cleanups);
2827 }
2828
2829 /* Used when starting or continuing the program.  */
2830
2831 static void
2832 insert_breakpoint_locations (void)
2833 {
2834   struct breakpoint *bpt;
2835   struct bp_location *bl, **blp_tmp;
2836   int error_flag = 0;
2837   int val = 0;
2838   int disabled_breaks = 0;
2839   int hw_breakpoint_error = 0;
2840   int hw_bp_error_explained_already = 0;
2841
2842   struct ui_file *tmp_error_stream = mem_fileopen ();
2843   struct cleanup *cleanups = make_cleanup_ui_file_delete (tmp_error_stream);
2844   
2845   /* Explicitly mark the warning -- this will only be printed if
2846      there was an error.  */
2847   fprintf_unfiltered (tmp_error_stream, "Warning:\n");
2848
2849   save_current_space_and_thread ();
2850
2851   ALL_BP_LOCATIONS (bl, blp_tmp)
2852     {
2853       if (!should_be_inserted (bl) || (bl->inserted && !bl->needs_update))
2854         continue;
2855
2856       /* There is no point inserting thread-specific breakpoints if
2857          the thread no longer exists.  ALL_BP_LOCATIONS bp_location
2858          has BL->OWNER always non-NULL.  */
2859       if (bl->owner->thread != -1
2860           && !valid_thread_id (bl->owner->thread))
2861         continue;
2862
2863       switch_to_program_space_and_thread (bl->pspace);
2864
2865       /* For targets that support global breakpoints, there's no need
2866          to select an inferior to insert breakpoint to.  In fact, even
2867          if we aren't attached to any process yet, we should still
2868          insert breakpoints.  */
2869       if (!gdbarch_has_global_breakpoints (target_gdbarch ())
2870           && ptid_equal (inferior_ptid, null_ptid))
2871         continue;
2872
2873       val = insert_bp_location (bl, tmp_error_stream, &disabled_breaks,
2874                                     &hw_breakpoint_error, &hw_bp_error_explained_already);
2875       if (val)
2876         error_flag = val;
2877     }
2878
2879   /* If we failed to insert all locations of a watchpoint, remove
2880      them, as half-inserted watchpoint is of limited use.  */
2881   ALL_BREAKPOINTS (bpt)  
2882     {
2883       int some_failed = 0;
2884       struct bp_location *loc;
2885
2886       if (!is_hardware_watchpoint (bpt))
2887         continue;
2888
2889       if (!breakpoint_enabled (bpt))
2890         continue;
2891
2892       if (bpt->disposition == disp_del_at_next_stop)
2893         continue;
2894       
2895       for (loc = bpt->loc; loc; loc = loc->next)
2896         if (!loc->inserted && should_be_inserted (loc))
2897           {
2898             some_failed = 1;
2899             break;
2900           }
2901       if (some_failed)
2902         {
2903           for (loc = bpt->loc; loc; loc = loc->next)
2904             if (loc->inserted)
2905               remove_breakpoint (loc, mark_uninserted);
2906
2907           hw_breakpoint_error = 1;
2908           fprintf_unfiltered (tmp_error_stream,
2909                               "Could not insert hardware watchpoint %d.\n", 
2910                               bpt->number);
2911           error_flag = -1;
2912         }
2913     }
2914
2915   if (error_flag)
2916     {
2917       /* If a hardware breakpoint or watchpoint was inserted, add a
2918          message about possibly exhausted resources.  */
2919       if (hw_breakpoint_error && !hw_bp_error_explained_already)
2920         {
2921           fprintf_unfiltered (tmp_error_stream, 
2922                               "Could not insert hardware breakpoints:\n\
2923 You may have requested too many hardware breakpoints/watchpoints.\n");
2924         }
2925       target_terminal_ours_for_output ();
2926       error_stream (tmp_error_stream);
2927     }
2928
2929   do_cleanups (cleanups);
2930 }
2931
2932 /* Used when the program stops.
2933    Returns zero if successful, or non-zero if there was a problem
2934    removing a breakpoint location.  */
2935
2936 int
2937 remove_breakpoints (void)
2938 {
2939   struct bp_location *bl, **blp_tmp;
2940   int val = 0;
2941
2942   ALL_BP_LOCATIONS (bl, blp_tmp)
2943   {
2944     if (bl->inserted && !is_tracepoint (bl->owner))
2945       val |= remove_breakpoint (bl, mark_uninserted);
2946   }
2947   return val;
2948 }
2949
2950 /* Remove breakpoints of process PID.  */
2951
2952 int
2953 remove_breakpoints_pid (int pid)
2954 {
2955   struct bp_location *bl, **blp_tmp;
2956   int val;
2957   struct inferior *inf = find_inferior_pid (pid);
2958
2959   ALL_BP_LOCATIONS (bl, blp_tmp)
2960   {
2961     if (bl->pspace != inf->pspace)
2962       continue;
2963
2964     if (bl->owner->type == bp_dprintf)
2965       continue;
2966
2967     if (bl->inserted)
2968       {
2969         val = remove_breakpoint (bl, mark_uninserted);
2970         if (val != 0)
2971           return val;
2972       }
2973   }
2974   return 0;
2975 }
2976
2977 int
2978 reattach_breakpoints (int pid)
2979 {
2980   struct cleanup *old_chain;
2981   struct bp_location *bl, **blp_tmp;
2982   int val;
2983   struct ui_file *tmp_error_stream;
2984   int dummy1 = 0, dummy2 = 0, dummy3 = 0;
2985   struct inferior *inf;
2986   struct thread_info *tp;
2987
2988   tp = any_live_thread_of_process (pid);
2989   if (tp == NULL)
2990     return 1;
2991
2992   inf = find_inferior_pid (pid);
2993   old_chain = save_inferior_ptid ();
2994
2995   inferior_ptid = tp->ptid;
2996
2997   tmp_error_stream = mem_fileopen ();
2998   make_cleanup_ui_file_delete (tmp_error_stream);
2999
3000   ALL_BP_LOCATIONS (bl, blp_tmp)
3001   {
3002     if (bl->pspace != inf->pspace)
3003       continue;
3004
3005     if (bl->inserted)
3006       {
3007         bl->inserted = 0;
3008         val = insert_bp_location (bl, tmp_error_stream, &dummy1, &dummy2, &dummy3);
3009         if (val != 0)
3010           {
3011             do_cleanups (old_chain);
3012             return val;
3013           }
3014       }
3015   }
3016   do_cleanups (old_chain);
3017   return 0;
3018 }
3019
3020 static int internal_breakpoint_number = -1;
3021
3022 /* Set the breakpoint number of B, depending on the value of INTERNAL.
3023    If INTERNAL is non-zero, the breakpoint number will be populated
3024    from internal_breakpoint_number and that variable decremented.
3025    Otherwise the breakpoint number will be populated from
3026    breakpoint_count and that value incremented.  Internal breakpoints
3027    do not set the internal var bpnum.  */
3028 static void
3029 set_breakpoint_number (int internal, struct breakpoint *b)
3030 {
3031   if (internal)
3032     b->number = internal_breakpoint_number--;
3033   else
3034     {
3035       set_breakpoint_count (breakpoint_count + 1);
3036       b->number = breakpoint_count;
3037     }
3038 }
3039
3040 static struct breakpoint *
3041 create_internal_breakpoint (struct gdbarch *gdbarch,
3042                             CORE_ADDR address, enum bptype type,
3043                             const struct breakpoint_ops *ops)
3044 {
3045   struct symtab_and_line sal;
3046   struct breakpoint *b;
3047
3048   init_sal (&sal);              /* Initialize to zeroes.  */
3049
3050   sal.pc = address;
3051   sal.section = find_pc_overlay (sal.pc);
3052   sal.pspace = current_program_space;
3053
3054   b = set_raw_breakpoint (gdbarch, sal, type, ops);
3055   b->number = internal_breakpoint_number--;
3056   b->disposition = disp_donttouch;
3057
3058   return b;
3059 }
3060
3061 static const char *const longjmp_names[] =
3062   {
3063     "longjmp", "_longjmp", "siglongjmp", "_siglongjmp"
3064   };
3065 #define NUM_LONGJMP_NAMES ARRAY_SIZE(longjmp_names)
3066
3067 /* Per-objfile data private to breakpoint.c.  */
3068 struct breakpoint_objfile_data
3069 {
3070   /* Minimal symbol for "_ovly_debug_event" (if any).  */
3071   struct minimal_symbol *overlay_msym;
3072
3073   /* Minimal symbol(s) for "longjmp", "siglongjmp", etc. (if any).  */
3074   struct minimal_symbol *longjmp_msym[NUM_LONGJMP_NAMES];
3075
3076   /* True if we have looked for longjmp probes.  */
3077   int longjmp_searched;
3078
3079   /* SystemTap probe points for longjmp (if any).  */
3080   VEC (probe_p) *longjmp_probes;
3081
3082   /* Minimal symbol for "std::terminate()" (if any).  */
3083   struct minimal_symbol *terminate_msym;
3084
3085   /* Minimal symbol for "_Unwind_DebugHook" (if any).  */
3086   struct minimal_symbol *exception_msym;
3087
3088   /* True if we have looked for exception probes.  */
3089   int exception_searched;
3090
3091   /* SystemTap probe points for unwinding (if any).  */
3092   VEC (probe_p) *exception_probes;
3093 };
3094
3095 static const struct objfile_data *breakpoint_objfile_key;
3096
3097 /* Minimal symbol not found sentinel.  */
3098 static struct minimal_symbol msym_not_found;
3099
3100 /* Returns TRUE if MSYM point to the "not found" sentinel.  */
3101
3102 static int
3103 msym_not_found_p (const struct minimal_symbol *msym)
3104 {
3105   return msym == &msym_not_found;
3106 }
3107
3108 /* Return per-objfile data needed by breakpoint.c.
3109    Allocate the data if necessary.  */
3110
3111 static struct breakpoint_objfile_data *
3112 get_breakpoint_objfile_data (struct objfile *objfile)
3113 {
3114   struct breakpoint_objfile_data *bp_objfile_data;
3115
3116   bp_objfile_data = objfile_data (objfile, breakpoint_objfile_key);
3117   if (bp_objfile_data == NULL)
3118     {
3119       bp_objfile_data = obstack_alloc (&objfile->objfile_obstack,
3120                                        sizeof (*bp_objfile_data));
3121
3122       memset (bp_objfile_data, 0, sizeof (*bp_objfile_data));
3123       set_objfile_data (objfile, breakpoint_objfile_key, bp_objfile_data);
3124     }
3125   return bp_objfile_data;
3126 }
3127
3128 static void
3129 free_breakpoint_probes (struct objfile *obj, void *data)
3130 {
3131   struct breakpoint_objfile_data *bp_objfile_data = data;
3132
3133   VEC_free (probe_p, bp_objfile_data->longjmp_probes);
3134   VEC_free (probe_p, bp_objfile_data->exception_probes);
3135 }
3136
3137 static void
3138 create_overlay_event_breakpoint (void)
3139 {
3140   struct objfile *objfile;
3141   const char *const func_name = "_ovly_debug_event";
3142
3143   ALL_OBJFILES (objfile)
3144     {
3145       struct breakpoint *b;
3146       struct breakpoint_objfile_data *bp_objfile_data;
3147       CORE_ADDR addr;
3148
3149       bp_objfile_data = get_breakpoint_objfile_data (objfile);
3150
3151       if (msym_not_found_p (bp_objfile_data->overlay_msym))
3152         continue;
3153
3154       if (bp_objfile_data->overlay_msym == NULL)
3155         {
3156           struct minimal_symbol *m;
3157
3158           m = lookup_minimal_symbol_text (func_name, objfile);
3159           if (m == NULL)
3160             {
3161               /* Avoid future lookups in this objfile.  */
3162               bp_objfile_data->overlay_msym = &msym_not_found;
3163               continue;
3164             }
3165           bp_objfile_data->overlay_msym = m;
3166         }
3167
3168       addr = SYMBOL_VALUE_ADDRESS (bp_objfile_data->overlay_msym);
3169       b = create_internal_breakpoint (get_objfile_arch (objfile), addr,
3170                                       bp_overlay_event,
3171                                       &internal_breakpoint_ops);
3172       b->addr_string = xstrdup (func_name);
3173
3174       if (overlay_debugging == ovly_auto)
3175         {
3176           b->enable_state = bp_enabled;
3177           overlay_events_enabled = 1;
3178         }
3179       else
3180        {
3181          b->enable_state = bp_disabled;
3182          overlay_events_enabled = 0;
3183        }
3184     }
3185   update_global_location_list (1);
3186 }
3187
3188 static void
3189 create_longjmp_master_breakpoint (void)
3190 {
3191   struct program_space *pspace;
3192   struct cleanup *old_chain;
3193
3194   old_chain = save_current_program_space ();
3195
3196   ALL_PSPACES (pspace)
3197   {
3198     struct objfile *objfile;
3199
3200     set_current_program_space (pspace);
3201
3202     ALL_OBJFILES (objfile)
3203     {
3204       int i;
3205       struct gdbarch *gdbarch;
3206       struct breakpoint_objfile_data *bp_objfile_data;
3207
3208       gdbarch = get_objfile_arch (objfile);
3209       if (!gdbarch_get_longjmp_target_p (gdbarch))
3210         continue;
3211
3212       bp_objfile_data = get_breakpoint_objfile_data (objfile);
3213
3214       if (!bp_objfile_data->longjmp_searched)
3215         {
3216           bp_objfile_data->longjmp_probes
3217             = find_probes_in_objfile (objfile, "libc", "longjmp");
3218           bp_objfile_data->longjmp_searched = 1;
3219         }
3220
3221       if (bp_objfile_data->longjmp_probes != NULL)
3222         {
3223           int i;
3224           struct probe *probe;
3225           struct gdbarch *gdbarch = get_objfile_arch (objfile);
3226
3227           for (i = 0;
3228                VEC_iterate (probe_p,
3229                             bp_objfile_data->longjmp_probes,
3230                             i, probe);
3231                ++i)
3232             {
3233               struct breakpoint *b;
3234
3235               b = create_internal_breakpoint (gdbarch, probe->address,
3236                                               bp_longjmp_master,
3237                                               &internal_breakpoint_ops);
3238               b->addr_string = xstrdup ("-probe-stap libc:longjmp");
3239               b->enable_state = bp_disabled;
3240             }
3241
3242           continue;
3243         }
3244
3245       for (i = 0; i < NUM_LONGJMP_NAMES; i++)
3246         {
3247           struct breakpoint *b;
3248           const char *func_name;
3249           CORE_ADDR addr;
3250
3251           if (msym_not_found_p (bp_objfile_data->longjmp_msym[i]))
3252             continue;
3253
3254           func_name = longjmp_names[i];
3255           if (bp_objfile_data->longjmp_msym[i] == NULL)
3256             {
3257               struct minimal_symbol *m;
3258
3259               m = lookup_minimal_symbol_text (func_name, objfile);
3260               if (m == NULL)
3261                 {
3262                   /* Prevent future lookups in this objfile.  */
3263                   bp_objfile_data->longjmp_msym[i] = &msym_not_found;
3264                   continue;
3265                 }
3266               bp_objfile_data->longjmp_msym[i] = m;
3267             }
3268
3269           addr = SYMBOL_VALUE_ADDRESS (bp_objfile_data->longjmp_msym[i]);
3270           b = create_internal_breakpoint (gdbarch, addr, bp_longjmp_master,
3271                                           &internal_breakpoint_ops);
3272           b->addr_string = xstrdup (func_name);
3273           b->enable_state = bp_disabled;
3274         }
3275     }
3276   }
3277   update_global_location_list (1);
3278
3279   do_cleanups (old_chain);
3280 }
3281
3282 /* Create a master std::terminate breakpoint.  */
3283 static void
3284 create_std_terminate_master_breakpoint (void)
3285 {
3286   struct program_space *pspace;
3287   struct cleanup *old_chain;
3288   const char *const func_name = "std::terminate()";
3289
3290   old_chain = save_current_program_space ();
3291
3292   ALL_PSPACES (pspace)
3293   {
3294     struct objfile *objfile;
3295     CORE_ADDR addr;
3296
3297     set_current_program_space (pspace);
3298
3299     ALL_OBJFILES (objfile)
3300     {
3301       struct breakpoint *b;
3302       struct breakpoint_objfile_data *bp_objfile_data;
3303
3304       bp_objfile_data = get_breakpoint_objfile_data (objfile);
3305
3306       if (msym_not_found_p (bp_objfile_data->terminate_msym))
3307         continue;
3308
3309       if (bp_objfile_data->terminate_msym == NULL)
3310         {
3311           struct minimal_symbol *m;
3312
3313           m = lookup_minimal_symbol (func_name, NULL, objfile);
3314           if (m == NULL || (MSYMBOL_TYPE (m) != mst_text
3315                             && MSYMBOL_TYPE (m) != mst_file_text))
3316             {
3317               /* Prevent future lookups in this objfile.  */
3318               bp_objfile_data->terminate_msym = &msym_not_found;
3319               continue;
3320             }
3321           bp_objfile_data->terminate_msym = m;
3322         }
3323
3324       addr = SYMBOL_VALUE_ADDRESS (bp_objfile_data->terminate_msym);
3325       b = create_internal_breakpoint (get_objfile_arch (objfile), addr,
3326                                       bp_std_terminate_master,
3327                                       &internal_breakpoint_ops);
3328       b->addr_string = xstrdup (func_name);
3329       b->enable_state = bp_disabled;
3330     }
3331   }
3332
3333   update_global_location_list (1);
3334
3335   do_cleanups (old_chain);
3336 }
3337
3338 /* Install a master breakpoint on the unwinder's debug hook.  */
3339
3340 static void
3341 create_exception_master_breakpoint (void)
3342 {
3343   struct objfile *objfile;
3344   const char *const func_name = "_Unwind_DebugHook";
3345
3346   ALL_OBJFILES (objfile)
3347     {
3348       struct breakpoint *b;
3349       struct gdbarch *gdbarch;
3350       struct breakpoint_objfile_data *bp_objfile_data;
3351       CORE_ADDR addr;
3352
3353       bp_objfile_data = get_breakpoint_objfile_data (objfile);
3354
3355       /* We prefer the SystemTap probe point if it exists.  */
3356       if (!bp_objfile_data->exception_searched)
3357         {
3358           bp_objfile_data->exception_probes
3359             = find_probes_in_objfile (objfile, "libgcc", "unwind");
3360           bp_objfile_data->exception_searched = 1;
3361         }
3362
3363       if (bp_objfile_data->exception_probes != NULL)
3364         {
3365           struct gdbarch *gdbarch = get_objfile_arch (objfile);
3366           int i;
3367           struct probe *probe;
3368
3369           for (i = 0;
3370                VEC_iterate (probe_p,
3371                             bp_objfile_data->exception_probes,
3372                             i, probe);
3373                ++i)
3374             {
3375               struct breakpoint *b;
3376
3377               b = create_internal_breakpoint (gdbarch, probe->address,
3378                                               bp_exception_master,
3379                                               &internal_breakpoint_ops);
3380               b->addr_string = xstrdup ("-probe-stap libgcc:unwind");
3381               b->enable_state = bp_disabled;
3382             }
3383
3384           continue;
3385         }
3386
3387       /* Otherwise, try the hook function.  */
3388
3389       if (msym_not_found_p (bp_objfile_data->exception_msym))
3390         continue;
3391
3392       gdbarch = get_objfile_arch (objfile);
3393
3394       if (bp_objfile_data->exception_msym == NULL)
3395         {
3396           struct minimal_symbol *debug_hook;
3397
3398           debug_hook = lookup_minimal_symbol (func_name, NULL, objfile);
3399           if (debug_hook == NULL)
3400             {
3401               bp_objfile_data->exception_msym = &msym_not_found;
3402               continue;
3403             }
3404
3405           bp_objfile_data->exception_msym = debug_hook;
3406         }
3407
3408       addr = SYMBOL_VALUE_ADDRESS (bp_objfile_data->exception_msym);
3409       addr = gdbarch_convert_from_func_ptr_addr (gdbarch, addr,
3410                                                  &current_target);
3411       b = create_internal_breakpoint (gdbarch, addr, bp_exception_master,
3412                                       &internal_breakpoint_ops);
3413       b->addr_string = xstrdup (func_name);
3414       b->enable_state = bp_disabled;
3415     }
3416
3417   update_global_location_list (1);
3418 }
3419
3420 void
3421 update_breakpoints_after_exec (void)
3422 {
3423   struct breakpoint *b, *b_tmp;
3424   struct bp_location *bploc, **bplocp_tmp;
3425
3426   /* We're about to delete breakpoints from GDB's lists.  If the
3427      INSERTED flag is true, GDB will try to lift the breakpoints by
3428      writing the breakpoints' "shadow contents" back into memory.  The
3429      "shadow contents" are NOT valid after an exec, so GDB should not
3430      do that.  Instead, the target is responsible from marking
3431      breakpoints out as soon as it detects an exec.  We don't do that
3432      here instead, because there may be other attempts to delete
3433      breakpoints after detecting an exec and before reaching here.  */
3434   ALL_BP_LOCATIONS (bploc, bplocp_tmp)
3435     if (bploc->pspace == current_program_space)
3436       gdb_assert (!bploc->inserted);
3437
3438   ALL_BREAKPOINTS_SAFE (b, b_tmp)
3439   {
3440     if (b->pspace != current_program_space)
3441       continue;
3442
3443     /* Solib breakpoints must be explicitly reset after an exec().  */
3444     if (b->type == bp_shlib_event)
3445       {
3446         delete_breakpoint (b);
3447         continue;
3448       }
3449
3450     /* JIT breakpoints must be explicitly reset after an exec().  */
3451     if (b->type == bp_jit_event)
3452       {
3453         delete_breakpoint (b);
3454         continue;
3455       }
3456
3457     /* Thread event breakpoints must be set anew after an exec(),
3458        as must overlay event and longjmp master breakpoints.  */
3459     if (b->type == bp_thread_event || b->type == bp_overlay_event
3460         || b->type == bp_longjmp_master || b->type == bp_std_terminate_master
3461         || b->type == bp_exception_master)
3462       {
3463         delete_breakpoint (b);
3464         continue;
3465       }
3466
3467     /* Step-resume breakpoints are meaningless after an exec().  */
3468     if (b->type == bp_step_resume || b->type == bp_hp_step_resume)
3469       {
3470         delete_breakpoint (b);
3471         continue;
3472       }
3473
3474     /* Longjmp and longjmp-resume breakpoints are also meaningless
3475        after an exec.  */
3476     if (b->type == bp_longjmp || b->type == bp_longjmp_resume
3477         || b->type == bp_longjmp_call_dummy
3478         || b->type == bp_exception || b->type == bp_exception_resume)
3479       {
3480         delete_breakpoint (b);
3481         continue;
3482       }
3483
3484     if (b->type == bp_catchpoint)
3485       {
3486         /* For now, none of the bp_catchpoint breakpoints need to
3487            do anything at this point.  In the future, if some of
3488            the catchpoints need to something, we will need to add
3489            a new method, and call this method from here.  */
3490         continue;
3491       }
3492
3493     /* bp_finish is a special case.  The only way we ought to be able
3494        to see one of these when an exec() has happened, is if the user
3495        caught a vfork, and then said "finish".  Ordinarily a finish just
3496        carries them to the call-site of the current callee, by setting
3497        a temporary bp there and resuming.  But in this case, the finish
3498        will carry them entirely through the vfork & exec.
3499
3500        We don't want to allow a bp_finish to remain inserted now.  But
3501        we can't safely delete it, 'cause finish_command has a handle to
3502        the bp on a bpstat, and will later want to delete it.  There's a
3503        chance (and I've seen it happen) that if we delete the bp_finish
3504        here, that its storage will get reused by the time finish_command
3505        gets 'round to deleting the "use to be a bp_finish" breakpoint.
3506        We really must allow finish_command to delete a bp_finish.
3507
3508        In the absence of a general solution for the "how do we know
3509        it's safe to delete something others may have handles to?"
3510        problem, what we'll do here is just uninsert the bp_finish, and
3511        let finish_command delete it.
3512
3513        (We know the bp_finish is "doomed" in the sense that it's
3514        momentary, and will be deleted as soon as finish_command sees
3515        the inferior stopped.  So it doesn't matter that the bp's
3516        address is probably bogus in the new a.out, unlike e.g., the
3517        solib breakpoints.)  */
3518
3519     if (b->type == bp_finish)
3520       {
3521         continue;
3522       }
3523
3524     /* Without a symbolic address, we have little hope of the
3525        pre-exec() address meaning the same thing in the post-exec()
3526        a.out.  */
3527     if (b->addr_string == NULL)
3528       {
3529         delete_breakpoint (b);
3530         continue;
3531       }
3532   }
3533   /* FIXME what about longjmp breakpoints?  Re-create them here?  */
3534   create_overlay_event_breakpoint ();
3535   create_longjmp_master_breakpoint ();
3536   create_std_terminate_master_breakpoint ();
3537   create_exception_master_breakpoint ();
3538 }
3539
3540 int
3541 detach_breakpoints (ptid_t ptid)
3542 {
3543   struct bp_location *bl, **blp_tmp;
3544   int val = 0;
3545   struct cleanup *old_chain = save_inferior_ptid ();
3546   struct inferior *inf = current_inferior ();
3547
3548   if (PIDGET (ptid) == PIDGET (inferior_ptid))
3549     error (_("Cannot detach breakpoints of inferior_ptid"));
3550
3551   /* Set inferior_ptid; remove_breakpoint_1 uses this global.  */
3552   inferior_ptid = ptid;
3553   ALL_BP_LOCATIONS (bl, blp_tmp)
3554   {
3555     if (bl->pspace != inf->pspace)
3556       continue;
3557
3558     if (bl->inserted)
3559       val |= remove_breakpoint_1 (bl, mark_inserted);
3560   }
3561
3562   /* Detach single-step breakpoints as well.  */
3563   detach_single_step_breakpoints ();
3564
3565   do_cleanups (old_chain);
3566   return val;
3567 }
3568
3569 /* Remove the breakpoint location BL from the current address space.
3570    Note that this is used to detach breakpoints from a child fork.
3571    When we get here, the child isn't in the inferior list, and neither
3572    do we have objects to represent its address space --- we should
3573    *not* look at bl->pspace->aspace here.  */
3574
3575 static int
3576 remove_breakpoint_1 (struct bp_location *bl, insertion_state_t is)
3577 {
3578   int val;
3579
3580   /* BL is never in moribund_locations by our callers.  */
3581   gdb_assert (bl->owner != NULL);
3582
3583   if (bl->owner->enable_state == bp_permanent)
3584     /* Permanent breakpoints cannot be inserted or removed.  */
3585     return 0;
3586
3587   /* The type of none suggests that owner is actually deleted.
3588      This should not ever happen.  */
3589   gdb_assert (bl->owner->type != bp_none);
3590
3591   if (bl->loc_type == bp_loc_software_breakpoint
3592       || bl->loc_type == bp_loc_hardware_breakpoint)
3593     {
3594       /* "Normal" instruction breakpoint: either the standard
3595          trap-instruction bp (bp_breakpoint), or a
3596          bp_hardware_breakpoint.  */
3597
3598       /* First check to see if we have to handle an overlay.  */
3599       if (overlay_debugging == ovly_off
3600           || bl->section == NULL
3601           || !(section_is_overlay (bl->section)))
3602         {
3603           /* No overlay handling: just remove the breakpoint.  */
3604           val = bl->owner->ops->remove_location (bl);
3605         }
3606       else
3607         {
3608           /* This breakpoint is in an overlay section.
3609              Did we set a breakpoint at the LMA?  */
3610           if (!overlay_events_enabled)
3611               {
3612                 /* Yes -- overlay event support is not active, so we
3613                    should have set a breakpoint at the LMA.  Remove it.  
3614                 */
3615                 /* Ignore any failures: if the LMA is in ROM, we will
3616                    have already warned when we failed to insert it.  */
3617                 if (bl->loc_type == bp_loc_hardware_breakpoint)
3618                   target_remove_hw_breakpoint (bl->gdbarch,
3619                                                &bl->overlay_target_info);
3620                 else
3621                   target_remove_breakpoint (bl->gdbarch,
3622                                             &bl->overlay_target_info);
3623               }
3624           /* Did we set a breakpoint at the VMA? 
3625              If so, we will have marked the breakpoint 'inserted'.  */
3626           if (bl->inserted)
3627             {
3628               /* Yes -- remove it.  Previously we did not bother to
3629                  remove the breakpoint if the section had been
3630                  unmapped, but let's not rely on that being safe.  We
3631                  don't know what the overlay manager might do.  */
3632
3633               /* However, we should remove *software* breakpoints only
3634                  if the section is still mapped, or else we overwrite
3635                  wrong code with the saved shadow contents.  */
3636               if (bl->loc_type == bp_loc_hardware_breakpoint
3637                   || section_is_mapped (bl->section))
3638                 val = bl->owner->ops->remove_location (bl);
3639               else
3640                 val = 0;
3641             }
3642           else
3643             {
3644               /* No -- not inserted, so no need to remove.  No error.  */
3645               val = 0;
3646             }
3647         }
3648
3649       /* In some cases, we might not be able to remove a breakpoint
3650          in a shared library that has already been removed, but we
3651          have not yet processed the shlib unload event.  */
3652       if (val && solib_name_from_address (bl->pspace, bl->address))
3653         val = 0;
3654
3655       if (val)
3656         return val;
3657       bl->inserted = (is == mark_inserted);
3658     }
3659   else if (bl->loc_type == bp_loc_hardware_watchpoint)
3660     {
3661       gdb_assert (bl->owner->ops != NULL
3662                   && bl->owner->ops->remove_location != NULL);
3663
3664       bl->inserted = (is == mark_inserted);
3665       bl->owner->ops->remove_location (bl);
3666
3667       /* Failure to remove any of the hardware watchpoints comes here.  */
3668       if ((is == mark_uninserted) && (bl->inserted))
3669         warning (_("Could not remove hardware watchpoint %d."),
3670                  bl->owner->number);
3671     }
3672   else if (bl->owner->type == bp_catchpoint
3673            && breakpoint_enabled (bl->owner)
3674            && !bl->duplicate)
3675     {
3676       gdb_assert (bl->owner->ops != NULL
3677                   && bl->owner->ops->remove_location != NULL);
3678
3679       val = bl->owner->ops->remove_location (bl);
3680       if (val)
3681         return val;
3682
3683       bl->inserted = (is == mark_inserted);
3684     }
3685
3686   return 0;
3687 }
3688
3689 static int
3690 remove_breakpoint (struct bp_location *bl, insertion_state_t is)
3691 {
3692   int ret;
3693   struct cleanup *old_chain;
3694
3695   /* BL is never in moribund_locations by our callers.  */
3696   gdb_assert (bl->owner != NULL);
3697
3698   if (bl->owner->enable_state == bp_permanent)
3699     /* Permanent breakpoints cannot be inserted or removed.  */
3700     return 0;
3701
3702   /* The type of none suggests that owner is actually deleted.
3703      This should not ever happen.  */
3704   gdb_assert (bl->owner->type != bp_none);
3705
3706   old_chain = save_current_space_and_thread ();
3707
3708   switch_to_program_space_and_thread (bl->pspace);
3709
3710   ret = remove_breakpoint_1 (bl, is);
3711
3712   do_cleanups (old_chain);
3713   return ret;
3714 }
3715
3716 /* Clear the "inserted" flag in all breakpoints.  */
3717
3718 void
3719 mark_breakpoints_out (void)
3720 {
3721   struct bp_location *bl, **blp_tmp;
3722
3723   ALL_BP_LOCATIONS (bl, blp_tmp)
3724     if (bl->pspace == current_program_space)
3725       bl->inserted = 0;
3726 }
3727
3728 /* Clear the "inserted" flag in all breakpoints and delete any
3729    breakpoints which should go away between runs of the program.
3730
3731    Plus other such housekeeping that has to be done for breakpoints
3732    between runs.
3733
3734    Note: this function gets called at the end of a run (by
3735    generic_mourn_inferior) and when a run begins (by
3736    init_wait_for_inferior).  */
3737
3738
3739
3740 void
3741 breakpoint_init_inferior (enum inf_context context)
3742 {
3743   struct breakpoint *b, *b_tmp;
3744   struct bp_location *bl, **blp_tmp;
3745   int ix;
3746   struct program_space *pspace = current_program_space;
3747
3748   /* If breakpoint locations are shared across processes, then there's
3749      nothing to do.  */
3750   if (gdbarch_has_global_breakpoints (target_gdbarch ()))
3751     return;
3752
3753   ALL_BP_LOCATIONS (bl, blp_tmp)
3754   {
3755     /* ALL_BP_LOCATIONS bp_location has BL->OWNER always non-NULL.  */
3756     if (bl->pspace == pspace
3757         && bl->owner->enable_state != bp_permanent)
3758       bl->inserted = 0;
3759   }
3760
3761   ALL_BREAKPOINTS_SAFE (b, b_tmp)
3762   {
3763     if (b->loc && b->loc->pspace != pspace)
3764       continue;
3765
3766     switch (b->type)
3767       {
3768       case bp_call_dummy:
3769       case bp_longjmp_call_dummy:
3770
3771         /* If the call dummy breakpoint is at the entry point it will
3772            cause problems when the inferior is rerun, so we better get
3773            rid of it.  */
3774
3775       case bp_watchpoint_scope:
3776
3777         /* Also get rid of scope breakpoints.  */
3778
3779       case bp_shlib_event:
3780
3781         /* Also remove solib event breakpoints.  Their addresses may
3782            have changed since the last time we ran the program.
3783            Actually we may now be debugging against different target;
3784            and so the solib backend that installed this breakpoint may
3785            not be used in by the target.  E.g.,
3786
3787            (gdb) file prog-linux
3788            (gdb) run               # native linux target
3789            ...
3790            (gdb) kill
3791            (gdb) file prog-win.exe
3792            (gdb) tar rem :9999     # remote Windows gdbserver.
3793         */
3794
3795       case bp_step_resume:
3796
3797         /* Also remove step-resume breakpoints.  */
3798
3799         delete_breakpoint (b);
3800         break;
3801
3802       case bp_watchpoint:
3803       case bp_hardware_watchpoint:
3804       case bp_read_watchpoint:
3805       case bp_access_watchpoint:
3806         {
3807           struct watchpoint *w = (struct watchpoint *) b;
3808
3809           /* Likewise for watchpoints on local expressions.  */
3810           if (w->exp_valid_block != NULL)
3811             delete_breakpoint (b);
3812           else if (context == inf_starting)
3813             {
3814               /* Reset val field to force reread of starting value in
3815                  insert_breakpoints.  */
3816               if (w->val)
3817                 value_free (w->val);
3818               w->val = NULL;
3819               w->val_valid = 0;
3820           }
3821         }
3822         break;
3823       default:
3824         break;
3825       }
3826   }
3827
3828   /* Get rid of the moribund locations.  */
3829   for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, bl); ++ix)
3830     decref_bp_location (&bl);
3831   VEC_free (bp_location_p, moribund_locations);
3832 }
3833
3834 /* These functions concern about actual breakpoints inserted in the
3835    target --- to e.g. check if we need to do decr_pc adjustment or if
3836    we need to hop over the bkpt --- so we check for address space
3837    match, not program space.  */
3838
3839 /* breakpoint_here_p (PC) returns non-zero if an enabled breakpoint
3840    exists at PC.  It returns ordinary_breakpoint_here if it's an
3841    ordinary breakpoint, or permanent_breakpoint_here if it's a
3842    permanent breakpoint.
3843    - When continuing from a location with an ordinary breakpoint, we
3844      actually single step once before calling insert_breakpoints.
3845    - When continuing from a location with a permanent breakpoint, we
3846      need to use the `SKIP_PERMANENT_BREAKPOINT' macro, provided by
3847      the target, to advance the PC past the breakpoint.  */
3848
3849 enum breakpoint_here
3850 breakpoint_here_p (struct address_space *aspace, CORE_ADDR pc)
3851 {
3852   struct bp_location *bl, **blp_tmp;
3853   int any_breakpoint_here = 0;
3854
3855   ALL_BP_LOCATIONS (bl, blp_tmp)
3856     {
3857       if (bl->loc_type != bp_loc_software_breakpoint
3858           && bl->loc_type != bp_loc_hardware_breakpoint)
3859         continue;
3860
3861       /* ALL_BP_LOCATIONS bp_location has BL->OWNER always non-NULL.  */
3862       if ((breakpoint_enabled (bl->owner)
3863            || bl->owner->enable_state == bp_permanent)
3864           && breakpoint_location_address_match (bl, aspace, pc))
3865         {
3866           if (overlay_debugging 
3867               && section_is_overlay (bl->section)
3868               && !section_is_mapped (bl->section))
3869             continue;           /* unmapped overlay -- can't be a match */
3870           else if (bl->owner->enable_state == bp_permanent)
3871             return permanent_breakpoint_here;
3872           else
3873             any_breakpoint_here = 1;
3874         }
3875     }
3876
3877   return any_breakpoint_here ? ordinary_breakpoint_here : 0;
3878 }
3879
3880 /* Return true if there's a moribund breakpoint at PC.  */
3881
3882 int
3883 moribund_breakpoint_here_p (struct address_space *aspace, CORE_ADDR pc)
3884 {
3885   struct bp_location *loc;
3886   int ix;
3887
3888   for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, loc); ++ix)
3889     if (breakpoint_location_address_match (loc, aspace, pc))
3890       return 1;
3891
3892   return 0;
3893 }
3894
3895 /* Returns non-zero if there's a breakpoint inserted at PC, which is
3896    inserted using regular breakpoint_chain / bp_location array
3897    mechanism.  This does not check for single-step breakpoints, which
3898    are inserted and removed using direct target manipulation.  */
3899
3900 int
3901 regular_breakpoint_inserted_here_p (struct address_space *aspace, 
3902                                     CORE_ADDR pc)
3903 {
3904   struct bp_location *bl, **blp_tmp;
3905
3906   ALL_BP_LOCATIONS (bl, blp_tmp)
3907     {
3908       if (bl->loc_type != bp_loc_software_breakpoint
3909           && bl->loc_type != bp_loc_hardware_breakpoint)
3910         continue;
3911
3912       if (bl->inserted
3913           && breakpoint_location_address_match (bl, aspace, pc))
3914         {
3915           if (overlay_debugging 
3916               && section_is_overlay (bl->section)
3917               && !section_is_mapped (bl->section))
3918             continue;           /* unmapped overlay -- can't be a match */
3919           else
3920             return 1;
3921         }
3922     }
3923   return 0;
3924 }
3925
3926 /* Returns non-zero iff there's either regular breakpoint
3927    or a single step breakpoint inserted at PC.  */
3928
3929 int
3930 breakpoint_inserted_here_p (struct address_space *aspace, CORE_ADDR pc)
3931 {
3932   if (regular_breakpoint_inserted_here_p (aspace, pc))
3933     return 1;
3934
3935   if (single_step_breakpoint_inserted_here_p (aspace, pc))
3936     return 1;
3937
3938   return 0;
3939 }
3940
3941 /* This function returns non-zero iff there is a software breakpoint
3942    inserted at PC.  */
3943
3944 int
3945 software_breakpoint_inserted_here_p (struct address_space *aspace,
3946                                      CORE_ADDR pc)
3947 {
3948   struct bp_location *bl, **blp_tmp;
3949
3950   ALL_BP_LOCATIONS (bl, blp_tmp)
3951     {
3952       if (bl->loc_type != bp_loc_software_breakpoint)
3953         continue;
3954
3955       if (bl->inserted
3956           && breakpoint_address_match (bl->pspace->aspace, bl->address,
3957                                        aspace, pc))
3958         {
3959           if (overlay_debugging 
3960               && section_is_overlay (bl->section)
3961               && !section_is_mapped (bl->section))
3962             continue;           /* unmapped overlay -- can't be a match */
3963           else
3964             return 1;
3965         }
3966     }
3967
3968   /* Also check for software single-step breakpoints.  */
3969   if (single_step_breakpoint_inserted_here_p (aspace, pc))
3970     return 1;
3971
3972   return 0;
3973 }
3974
3975 int
3976 hardware_watchpoint_inserted_in_range (struct address_space *aspace,
3977                                        CORE_ADDR addr, ULONGEST len)
3978 {
3979   struct breakpoint *bpt;
3980
3981   ALL_BREAKPOINTS (bpt)
3982     {
3983       struct bp_location *loc;
3984
3985       if (bpt->type != bp_hardware_watchpoint
3986           && bpt->type != bp_access_watchpoint)
3987         continue;
3988
3989       if (!breakpoint_enabled (bpt))
3990         continue;
3991
3992       for (loc = bpt->loc; loc; loc = loc->next)
3993         if (loc->pspace->aspace == aspace && loc->inserted)
3994           {
3995             CORE_ADDR l, h;
3996
3997             /* Check for intersection.  */
3998             l = max (loc->address, addr);
3999             h = min (loc->address + loc->length, addr + len);
4000             if (l < h)
4001               return 1;
4002           }
4003     }
4004   return 0;
4005 }
4006
4007 /* breakpoint_thread_match (PC, PTID) returns true if the breakpoint at
4008    PC is valid for process/thread PTID.  */
4009
4010 int
4011 breakpoint_thread_match (struct address_space *aspace, CORE_ADDR pc,
4012                          ptid_t ptid)
4013 {
4014   struct bp_location *bl, **blp_tmp;
4015   /* The thread and task IDs associated to PTID, computed lazily.  */
4016   int thread = -1;
4017   int task = 0;
4018   
4019   ALL_BP_LOCATIONS (bl, blp_tmp)
4020     {
4021       if (bl->loc_type != bp_loc_software_breakpoint
4022           && bl->loc_type != bp_loc_hardware_breakpoint)
4023         continue;
4024
4025       /* ALL_BP_LOCATIONS bp_location has bl->OWNER always non-NULL.  */
4026       if (!breakpoint_enabled (bl->owner)
4027           && bl->owner->enable_state != bp_permanent)
4028         continue;
4029
4030       if (!breakpoint_location_address_match (bl, aspace, pc))
4031         continue;
4032
4033       if (bl->owner->thread != -1)
4034         {
4035           /* This is a thread-specific breakpoint.  Check that ptid
4036              matches that thread.  If thread hasn't been computed yet,
4037              it is now time to do so.  */
4038           if (thread == -1)
4039             thread = pid_to_thread_id (ptid);
4040           if (bl->owner->thread != thread)
4041             continue;
4042         }
4043
4044       if (bl->owner->task != 0)
4045         {
4046           /* This is a task-specific breakpoint.  Check that ptid
4047              matches that task.  If task hasn't been computed yet,
4048              it is now time to do so.  */
4049           if (task == 0)
4050             task = ada_get_task_number (ptid);
4051           if (bl->owner->task != task)
4052             continue;
4053         }
4054
4055       if (overlay_debugging 
4056           && section_is_overlay (bl->section)
4057           && !section_is_mapped (bl->section))
4058         continue;           /* unmapped overlay -- can't be a match */
4059
4060       return 1;
4061     }
4062
4063   return 0;
4064 }
4065 \f
4066
4067 /* bpstat stuff.  External routines' interfaces are documented
4068    in breakpoint.h.  */
4069
4070 int
4071 is_catchpoint (struct breakpoint *ep)
4072 {
4073   return (ep->type == bp_catchpoint);
4074 }
4075
4076 /* Frees any storage that is part of a bpstat.  Does not walk the
4077    'next' chain.  */
4078
4079 static void
4080 bpstat_free (bpstat bs)
4081 {
4082   if (bs->old_val != NULL)
4083     value_free (bs->old_val);
4084   decref_counted_command_line (&bs->commands);
4085   decref_bp_location (&bs->bp_location_at);
4086   xfree (bs);
4087 }
4088
4089 /* Clear a bpstat so that it says we are not at any breakpoint.
4090    Also free any storage that is part of a bpstat.  */
4091
4092 void
4093 bpstat_clear (bpstat *bsp)
4094 {
4095   bpstat p;
4096   bpstat q;
4097
4098   if (bsp == 0)
4099     return;
4100   p = *bsp;
4101   while (p != NULL)
4102     {
4103       q = p->next;
4104       bpstat_free (p);
4105       p = q;
4106     }
4107   *bsp = NULL;
4108 }
4109
4110 /* Return a copy of a bpstat.  Like "bs1 = bs2" but all storage that
4111    is part of the bpstat is copied as well.  */
4112
4113 bpstat
4114 bpstat_copy (bpstat bs)
4115 {
4116   bpstat p = NULL;
4117   bpstat tmp;
4118   bpstat retval = NULL;
4119
4120   if (bs == NULL)
4121     return bs;
4122
4123   for (; bs != NULL; bs = bs->next)
4124     {
4125       tmp = (bpstat) xmalloc (sizeof (*tmp));
4126       memcpy (tmp, bs, sizeof (*tmp));
4127       incref_counted_command_line (tmp->commands);
4128       incref_bp_location (tmp->bp_location_at);
4129       if (bs->old_val != NULL)
4130         {
4131           tmp->old_val = value_copy (bs->old_val);
4132           release_value (tmp->old_val);
4133         }
4134
4135       if (p == NULL)
4136         /* This is the first thing in the chain.  */
4137         retval = tmp;
4138       else
4139         p->next = tmp;
4140       p = tmp;
4141     }
4142   p->next = NULL;
4143   return retval;
4144 }
4145
4146 /* Find the bpstat associated with this breakpoint.  */
4147
4148 bpstat
4149 bpstat_find_breakpoint (bpstat bsp, struct breakpoint *breakpoint)
4150 {
4151   if (bsp == NULL)
4152     return NULL;
4153
4154   for (; bsp != NULL; bsp = bsp->next)
4155     {
4156       if (bsp->breakpoint_at == breakpoint)
4157         return bsp;
4158     }
4159   return NULL;
4160 }
4161
4162 /* See breakpoint.h.  */
4163
4164 enum bpstat_signal_value
4165 bpstat_explains_signal (bpstat bsp)
4166 {
4167   enum bpstat_signal_value result = BPSTAT_SIGNAL_NO;
4168
4169   for (; bsp != NULL; bsp = bsp->next)
4170     {
4171       /* Ensure that, if we ever entered this loop, then we at least
4172          return BPSTAT_SIGNAL_HIDE.  */
4173       enum bpstat_signal_value newval = BPSTAT_SIGNAL_HIDE;
4174
4175       if (bsp->breakpoint_at != NULL)
4176         newval = bsp->breakpoint_at->ops->explains_signal (bsp->breakpoint_at);
4177
4178       if (newval > result)
4179         result = newval;
4180     }
4181
4182   return result;
4183 }
4184
4185 /* Put in *NUM the breakpoint number of the first breakpoint we are
4186    stopped at.  *BSP upon return is a bpstat which points to the
4187    remaining breakpoints stopped at (but which is not guaranteed to be
4188    good for anything but further calls to bpstat_num).
4189
4190    Return 0 if passed a bpstat which does not indicate any breakpoints.
4191    Return -1 if stopped at a breakpoint that has been deleted since
4192    we set it.
4193    Return 1 otherwise.  */
4194
4195 int
4196 bpstat_num (bpstat *bsp, int *num)
4197 {
4198   struct breakpoint *b;
4199
4200   if ((*bsp) == NULL)
4201     return 0;                   /* No more breakpoint values */
4202
4203   /* We assume we'll never have several bpstats that correspond to a
4204      single breakpoint -- otherwise, this function might return the
4205      same number more than once and this will look ugly.  */
4206   b = (*bsp)->breakpoint_at;
4207   *bsp = (*bsp)->next;
4208   if (b == NULL)
4209     return -1;                  /* breakpoint that's been deleted since */
4210
4211   *num = b->number;             /* We have its number */
4212   return 1;
4213 }
4214
4215 /* See breakpoint.h.  */
4216
4217 void
4218 bpstat_clear_actions (void)
4219 {
4220   struct thread_info *tp;
4221   bpstat bs;
4222
4223   if (ptid_equal (inferior_ptid, null_ptid))
4224     return;
4225
4226   tp = find_thread_ptid (inferior_ptid);
4227   if (tp == NULL)
4228     return;
4229
4230   for (bs = tp->control.stop_bpstat; bs != NULL; bs = bs->next)
4231     {
4232       decref_counted_command_line (&bs->commands);
4233
4234       if (bs->old_val != NULL)
4235         {
4236           value_free (bs->old_val);
4237           bs->old_val = NULL;
4238         }
4239     }
4240 }
4241
4242 /* Called when a command is about to proceed the inferior.  */
4243
4244 static void
4245 breakpoint_about_to_proceed (void)
4246 {
4247   if (!ptid_equal (inferior_ptid, null_ptid))
4248     {
4249       struct thread_info *tp = inferior_thread ();
4250
4251       /* Allow inferior function calls in breakpoint commands to not
4252          interrupt the command list.  When the call finishes
4253          successfully, the inferior will be standing at the same
4254          breakpoint as if nothing happened.  */
4255       if (tp->control.in_infcall)
4256         return;
4257     }
4258
4259   breakpoint_proceeded = 1;
4260 }
4261
4262 /* Stub for cleaning up our state if we error-out of a breakpoint
4263    command.  */
4264 static void
4265 cleanup_executing_breakpoints (void *ignore)
4266 {
4267   executing_breakpoint_commands = 0;
4268 }
4269
4270 /* Return non-zero iff CMD as the first line of a command sequence is `silent'
4271    or its equivalent.  */
4272
4273 static int
4274 command_line_is_silent (struct command_line *cmd)
4275 {
4276   return cmd && (strcmp ("silent", cmd->line) == 0
4277                  || (xdb_commands && strcmp ("Q", cmd->line) == 0));
4278 }
4279
4280 /* Execute all the commands associated with all the breakpoints at
4281    this location.  Any of these commands could cause the process to
4282    proceed beyond this point, etc.  We look out for such changes by
4283    checking the global "breakpoint_proceeded" after each command.
4284
4285    Returns true if a breakpoint command resumed the inferior.  In that
4286    case, it is the caller's responsibility to recall it again with the
4287    bpstat of the current thread.  */
4288
4289 static int
4290 bpstat_do_actions_1 (bpstat *bsp)
4291 {
4292   bpstat bs;
4293   struct cleanup *old_chain;
4294   int again = 0;
4295
4296   /* Avoid endless recursion if a `source' command is contained
4297      in bs->commands.  */
4298   if (executing_breakpoint_commands)
4299     return 0;
4300
4301   executing_breakpoint_commands = 1;
4302   old_chain = make_cleanup (cleanup_executing_breakpoints, 0);
4303
4304   prevent_dont_repeat ();
4305
4306   /* This pointer will iterate over the list of bpstat's.  */
4307   bs = *bsp;
4308
4309   breakpoint_proceeded = 0;
4310   for (; bs != NULL; bs = bs->next)
4311     {
4312       struct counted_command_line *ccmd;
4313       struct command_line *cmd;
4314       struct cleanup *this_cmd_tree_chain;
4315
4316       /* Take ownership of the BSP's command tree, if it has one.
4317
4318          The command tree could legitimately contain commands like
4319          'step' and 'next', which call clear_proceed_status, which
4320          frees stop_bpstat's command tree.  To make sure this doesn't
4321          free the tree we're executing out from under us, we need to
4322          take ownership of the tree ourselves.  Since a given bpstat's
4323          commands are only executed once, we don't need to copy it; we
4324          can clear the pointer in the bpstat, and make sure we free
4325          the tree when we're done.  */
4326       ccmd = bs->commands;
4327       bs->commands = NULL;
4328       this_cmd_tree_chain = make_cleanup_decref_counted_command_line (&ccmd);
4329       cmd = ccmd ? ccmd->commands : NULL;
4330       if (command_line_is_silent (cmd))
4331         {
4332           /* The action has been already done by bpstat_stop_status.  */
4333           cmd = cmd->next;
4334         }
4335
4336       while (cmd != NULL)
4337         {
4338           execute_control_command (cmd);
4339
4340           if (breakpoint_proceeded)
4341             break;
4342           else
4343             cmd = cmd->next;
4344         }
4345
4346       /* We can free this command tree now.  */
4347       do_cleanups (this_cmd_tree_chain);
4348
4349       if (breakpoint_proceeded)
4350         {
4351           if (target_can_async_p ())
4352             /* If we are in async mode, then the target might be still
4353                running, not stopped at any breakpoint, so nothing for
4354                us to do here -- just return to the event loop.  */
4355             ;
4356           else
4357             /* In sync mode, when execute_control_command returns
4358                we're already standing on the next breakpoint.
4359                Breakpoint commands for that stop were not run, since
4360                execute_command does not run breakpoint commands --
4361                only command_line_handler does, but that one is not
4362                involved in execution of breakpoint commands.  So, we
4363                can now execute breakpoint commands.  It should be
4364                noted that making execute_command do bpstat actions is
4365                not an option -- in this case we'll have recursive
4366                invocation of bpstat for each breakpoint with a
4367                command, and can easily blow up GDB stack.  Instead, we
4368                return true, which will trigger the caller to recall us
4369                with the new stop_bpstat.  */
4370             again = 1;
4371           break;
4372         }
4373     }
4374   do_cleanups (old_chain);
4375   return again;
4376 }
4377
4378 void
4379 bpstat_do_actions (void)
4380 {
4381   struct cleanup *cleanup_if_error = make_bpstat_clear_actions_cleanup ();
4382
4383   /* Do any commands attached to breakpoint we are stopped at.  */
4384   while (!ptid_equal (inferior_ptid, null_ptid)
4385          && target_has_execution
4386          && !is_exited (inferior_ptid)
4387          && !is_executing (inferior_ptid))
4388     /* Since in sync mode, bpstat_do_actions may resume the inferior,
4389        and only return when it is stopped at the next breakpoint, we
4390        keep doing breakpoint actions until it returns false to
4391        indicate the inferior was not resumed.  */
4392     if (!bpstat_do_actions_1 (&inferior_thread ()->control.stop_bpstat))
4393       break;
4394
4395   discard_cleanups (cleanup_if_error);
4396 }
4397
4398 /* Print out the (old or new) value associated with a watchpoint.  */
4399
4400 static void
4401 watchpoint_value_print (struct value *val, struct ui_file *stream)
4402 {
4403   if (val == NULL)
4404     fprintf_unfiltered (stream, _("<unreadable>"));
4405   else
4406     {
4407       struct value_print_options opts;
4408       get_user_print_options (&opts);
4409       value_print (val, stream, &opts);
4410     }
4411 }
4412
4413 /* Generic routine for printing messages indicating why we
4414    stopped.  The behavior of this function depends on the value
4415    'print_it' in the bpstat structure.  Under some circumstances we
4416    may decide not to print anything here and delegate the task to
4417    normal_stop().  */
4418
4419 static enum print_stop_action
4420 print_bp_stop_message (bpstat bs)
4421 {
4422   switch (bs->print_it)
4423     {
4424     case print_it_noop:
4425       /* Nothing should be printed for this bpstat entry.  */
4426       return PRINT_UNKNOWN;
4427       break;
4428
4429     case print_it_done:
4430       /* We still want to print the frame, but we already printed the
4431          relevant messages.  */
4432       return PRINT_SRC_AND_LOC;
4433       break;
4434
4435     case print_it_normal:
4436       {
4437         struct breakpoint *b = bs->breakpoint_at;
4438
4439         /* bs->breakpoint_at can be NULL if it was a momentary breakpoint
4440            which has since been deleted.  */
4441         if (b == NULL)
4442           return PRINT_UNKNOWN;
4443
4444         /* Normal case.  Call the breakpoint's print_it method.  */
4445         return b->ops->print_it (bs);
4446       }
4447       break;
4448
4449     default:
4450       internal_error (__FILE__, __LINE__,
4451                       _("print_bp_stop_message: unrecognized enum value"));
4452       break;
4453     }
4454 }
4455
4456 /* A helper function that prints a shared library stopped event.  */
4457
4458 static void
4459 print_solib_event (int is_catchpoint)
4460 {
4461   int any_deleted
4462     = !VEC_empty (char_ptr, current_program_space->deleted_solibs);
4463   int any_added
4464     = !VEC_empty (so_list_ptr, current_program_space->added_solibs);
4465
4466   if (!is_catchpoint)
4467     {
4468       if (any_added || any_deleted)
4469         ui_out_text (current_uiout,
4470                      _("Stopped due to shared library event:\n"));
4471       else
4472         ui_out_text (current_uiout,
4473                      _("Stopped due to shared library event (no "
4474                        "libraries added or removed)\n"));
4475     }
4476
4477   if (ui_out_is_mi_like_p (current_uiout))
4478     ui_out_field_string (current_uiout, "reason",
4479                          async_reason_lookup (EXEC_ASYNC_SOLIB_EVENT));
4480
4481   if (any_deleted)
4482     {
4483       struct cleanup *cleanup;
4484       char *name;
4485       int ix;
4486
4487       ui_out_text (current_uiout, _("  Inferior unloaded "));
4488       cleanup = make_cleanup_ui_out_list_begin_end (current_uiout,
4489                                                     "removed");
4490       for (ix = 0;
4491            VEC_iterate (char_ptr, current_program_space->deleted_solibs,
4492                         ix, name);
4493            ++ix)
4494         {
4495           if (ix > 0)
4496             ui_out_text (current_uiout, "    ");
4497           ui_out_field_string (current_uiout, "library", name);
4498           ui_out_text (current_uiout, "\n");
4499         }
4500
4501       do_cleanups (cleanup);
4502     }
4503
4504   if (any_added)
4505     {
4506       struct so_list *iter;
4507       int ix;
4508       struct cleanup *cleanup;
4509
4510       ui_out_text (current_uiout, _("  Inferior loaded "));
4511       cleanup = make_cleanup_ui_out_list_begin_end (current_uiout,
4512                                                     "added");
4513       for (ix = 0;
4514            VEC_iterate (so_list_ptr, current_program_space->added_solibs,
4515                         ix, iter);
4516            ++ix)
4517         {
4518           if (ix > 0)
4519             ui_out_text (current_uiout, "    ");
4520           ui_out_field_string (current_uiout, "library", iter->so_name);
4521           ui_out_text (current_uiout, "\n");
4522         }
4523
4524       do_cleanups (cleanup);
4525     }
4526 }
4527
4528 /* Print a message indicating what happened.  This is called from
4529    normal_stop().  The input to this routine is the head of the bpstat
4530    list - a list of the eventpoints that caused this stop.  KIND is
4531    the target_waitkind for the stopping event.  This
4532    routine calls the generic print routine for printing a message
4533    about reasons for stopping.  This will print (for example) the
4534    "Breakpoint n," part of the output.  The return value of this
4535    routine is one of:
4536
4537    PRINT_UNKNOWN: Means we printed nothing.
4538    PRINT_SRC_AND_LOC: Means we printed something, and expect subsequent
4539    code to print the location.  An example is 
4540    "Breakpoint 1, " which should be followed by
4541    the location.
4542    PRINT_SRC_ONLY: Means we printed something, but there is no need
4543    to also print the location part of the message.
4544    An example is the catch/throw messages, which
4545    don't require a location appended to the end.
4546    PRINT_NOTHING: We have done some printing and we don't need any 
4547    further info to be printed.  */
4548
4549 enum print_stop_action
4550 bpstat_print (bpstat bs, int kind)
4551 {
4552   int val;
4553
4554   /* Maybe another breakpoint in the chain caused us to stop.
4555      (Currently all watchpoints go on the bpstat whether hit or not.
4556      That probably could (should) be changed, provided care is taken
4557      with respect to bpstat_explains_signal).  */
4558   for (; bs; bs = bs->next)
4559     {
4560       val = print_bp_stop_message (bs);
4561       if (val == PRINT_SRC_ONLY 
4562           || val == PRINT_SRC_AND_LOC 
4563           || val == PRINT_NOTHING)
4564         return val;
4565     }
4566
4567   /* If we had hit a shared library event breakpoint,
4568      print_bp_stop_message would print out this message.  If we hit an
4569      OS-level shared library event, do the same thing.  */
4570   if (kind == TARGET_WAITKIND_LOADED)
4571     {
4572       print_solib_event (0);
4573       return PRINT_NOTHING;
4574     }
4575
4576   /* We reached the end of the chain, or we got a null BS to start
4577      with and nothing was printed.  */
4578   return PRINT_UNKNOWN;
4579 }
4580
4581 /* Evaluate the expression EXP and return 1 if value is zero.  This is
4582    used inside a catch_errors to evaluate the breakpoint condition.
4583    The argument is a "struct expression *" that has been cast to a
4584    "char *" to make it pass through catch_errors.  */
4585
4586 static int
4587 breakpoint_cond_eval (void *exp)
4588 {
4589   struct value *mark = value_mark ();
4590   int i = !value_true (evaluate_expression ((struct expression *) exp));
4591
4592   value_free_to_mark (mark);
4593   return i;
4594 }
4595
4596 /* Allocate a new bpstat.  Link it to the FIFO list by BS_LINK_POINTER.  */
4597
4598 static bpstat
4599 bpstat_alloc (struct bp_location *bl, bpstat **bs_link_pointer)
4600 {
4601   bpstat bs;
4602
4603   bs = (bpstat) xmalloc (sizeof (*bs));
4604   bs->next = NULL;
4605   **bs_link_pointer = bs;
4606   *bs_link_pointer = &bs->next;
4607   bs->breakpoint_at = bl->owner;
4608   bs->bp_location_at = bl;
4609   incref_bp_location (bl);
4610   /* If the condition is false, etc., don't do the commands.  */
4611   bs->commands = NULL;
4612   bs->old_val = NULL;
4613   bs->print_it = print_it_normal;
4614   return bs;
4615 }
4616 \f
4617 /* The target has stopped with waitstatus WS.  Check if any hardware
4618    watchpoints have triggered, according to the target.  */
4619
4620 int
4621 watchpoints_triggered (struct target_waitstatus *ws)
4622 {
4623   int stopped_by_watchpoint = target_stopped_by_watchpoint ();
4624   CORE_ADDR addr;
4625   struct breakpoint *b;
4626
4627   if (!stopped_by_watchpoint)
4628     {
4629       /* We were not stopped by a watchpoint.  Mark all watchpoints
4630          as not triggered.  */
4631       ALL_BREAKPOINTS (b)
4632         if (is_hardware_watchpoint (b))
4633           {
4634             struct watchpoint *w = (struct watchpoint *) b;
4635
4636             w->watchpoint_triggered = watch_triggered_no;
4637           }
4638
4639       return 0;
4640     }
4641
4642   if (!target_stopped_data_address (&current_target, &addr))
4643     {
4644       /* We were stopped by a watchpoint, but we don't know where.
4645          Mark all watchpoints as unknown.  */
4646       ALL_BREAKPOINTS (b)
4647         if (is_hardware_watchpoint (b))
4648           {
4649             struct watchpoint *w = (struct watchpoint *) b;
4650
4651             w->watchpoint_triggered = watch_triggered_unknown;
4652           }
4653
4654       return stopped_by_watchpoint;
4655     }
4656
4657   /* The target could report the data address.  Mark watchpoints
4658      affected by this data address as triggered, and all others as not
4659      triggered.  */
4660
4661   ALL_BREAKPOINTS (b)
4662     if (is_hardware_watchpoint (b))
4663       {
4664         struct watchpoint *w = (struct watchpoint *) b;
4665         struct bp_location *loc;
4666
4667         w->watchpoint_triggered = watch_triggered_no;
4668         for (loc = b->loc; loc; loc = loc->next)
4669           {
4670             if (is_masked_watchpoint (b))
4671               {
4672                 CORE_ADDR newaddr = addr & w->hw_wp_mask;
4673                 CORE_ADDR start = loc->address & w->hw_wp_mask;
4674
4675                 if (newaddr == start)
4676                   {
4677                     w->watchpoint_triggered = watch_triggered_yes;
4678                     break;
4679                   }
4680               }
4681             /* Exact match not required.  Within range is sufficient.  */
4682             else if (target_watchpoint_addr_within_range (&current_target,
4683                                                          addr, loc->address,
4684                                                          loc->length))
4685               {
4686                 w->watchpoint_triggered = watch_triggered_yes;
4687                 break;
4688               }
4689           }
4690       }
4691
4692   return 1;
4693 }
4694
4695 /* Possible return values for watchpoint_check (this can't be an enum
4696    because of check_errors).  */
4697 /* The watchpoint has been deleted.  */
4698 #define WP_DELETED 1
4699 /* The value has changed.  */
4700 #define WP_VALUE_CHANGED 2
4701 /* The value has not changed.  */
4702 #define WP_VALUE_NOT_CHANGED 3
4703 /* Ignore this watchpoint, no matter if the value changed or not.  */
4704 #define WP_IGNORE 4
4705
4706 #define BP_TEMPFLAG 1
4707 #define BP_HARDWAREFLAG 2
4708
4709 /* Evaluate watchpoint condition expression and check if its value
4710    changed.
4711
4712    P should be a pointer to struct bpstat, but is defined as a void *
4713    in order for this function to be usable with catch_errors.  */
4714
4715 static int
4716 watchpoint_check (void *p)
4717 {
4718   bpstat bs = (bpstat) p;
4719   struct watchpoint *b;
4720   struct frame_info *fr;
4721   int within_current_scope;
4722
4723   /* BS is built from an existing struct breakpoint.  */
4724   gdb_assert (bs->breakpoint_at != NULL);
4725   b = (struct watchpoint *) bs->breakpoint_at;
4726
4727   /* If this is a local watchpoint, we only want to check if the
4728      watchpoint frame is in scope if the current thread is the thread
4729      that was used to create the watchpoint.  */
4730   if (!watchpoint_in_thread_scope (b))
4731     return WP_IGNORE;
4732
4733   if (b->exp_valid_block == NULL)
4734     within_current_scope = 1;
4735   else
4736     {
4737       struct frame_info *frame = get_current_frame ();
4738       struct gdbarch *frame_arch = get_frame_arch (frame);
4739       CORE_ADDR frame_pc = get_frame_pc (frame);
4740
4741       /* in_function_epilogue_p() returns a non-zero value if we're
4742          still in the function but the stack frame has already been
4743          invalidated.  Since we can't rely on the values of local
4744          variables after the stack has been destroyed, we are treating
4745          the watchpoint in that state as `not changed' without further
4746          checking.  Don't mark watchpoints as changed if the current
4747          frame is in an epilogue - even if they are in some other
4748          frame, our view of the stack is likely to be wrong and
4749          frame_find_by_id could error out.  */
4750       if (gdbarch_in_function_epilogue_p (frame_arch, frame_pc))
4751         return WP_IGNORE;
4752
4753       fr = frame_find_by_id (b->watchpoint_frame);
4754       within_current_scope = (fr != NULL);
4755
4756       /* If we've gotten confused in the unwinder, we might have
4757          returned a frame that can't describe this variable.  */
4758       if (within_current_scope)
4759         {
4760           struct symbol *function;
4761
4762           function = get_frame_function (fr);
4763           if (function == NULL
4764               || !contained_in (b->exp_valid_block,
4765                                 SYMBOL_BLOCK_VALUE (function)))
4766             within_current_scope = 0;
4767         }
4768
4769       if (within_current_scope)
4770         /* If we end up stopping, the current frame will get selected
4771            in normal_stop.  So this call to select_frame won't affect
4772            the user.  */
4773         select_frame (fr);
4774     }
4775
4776   if (within_current_scope)
4777     {
4778       /* We use value_{,free_to_}mark because it could be a *long*
4779          time before we return to the command level and call
4780          free_all_values.  We can't call free_all_values because we
4781          might be in the middle of evaluating a function call.  */
4782
4783       int pc = 0;
4784       struct value *mark;
4785       struct value *new_val;
4786
4787       if (is_masked_watchpoint (&b->base))
4788         /* Since we don't know the exact trigger address (from
4789            stopped_data_address), just tell the user we've triggered
4790            a mask watchpoint.  */
4791         return WP_VALUE_CHANGED;
4792
4793       mark = value_mark ();
4794       fetch_subexp_value (b->exp, &pc, &new_val, NULL, NULL);
4795
4796       /* We use value_equal_contents instead of value_equal because
4797          the latter coerces an array to a pointer, thus comparing just
4798          the address of the array instead of its contents.  This is
4799          not what we want.  */
4800       if ((b->val != NULL) != (new_val != NULL)
4801           || (b->val != NULL && !value_equal_contents (b->val, new_val)))
4802         {
4803           if (new_val != NULL)
4804             {
4805               release_value (new_val);
4806               value_free_to_mark (mark);
4807             }
4808           bs->old_val = b->val;
4809           b->val = new_val;
4810           b->val_valid = 1;
4811           return WP_VALUE_CHANGED;
4812         }
4813       else
4814         {
4815           /* Nothing changed.  */
4816           value_free_to_mark (mark);
4817           return WP_VALUE_NOT_CHANGED;
4818         }
4819     }
4820   else
4821     {
4822       struct ui_out *uiout = current_uiout;
4823
4824       /* This seems like the only logical thing to do because
4825          if we temporarily ignored the watchpoint, then when
4826          we reenter the block in which it is valid it contains
4827          garbage (in the case of a function, it may have two
4828          garbage values, one before and one after the prologue).
4829          So we can't even detect the first assignment to it and
4830          watch after that (since the garbage may or may not equal
4831          the first value assigned).  */
4832       /* We print all the stop information in
4833          breakpoint_ops->print_it, but in this case, by the time we
4834          call breakpoint_ops->print_it this bp will be deleted
4835          already.  So we have no choice but print the information
4836          here.  */
4837       if (ui_out_is_mi_like_p (uiout))
4838         ui_out_field_string
4839           (uiout, "reason", async_reason_lookup (EXEC_ASYNC_WATCHPOINT_SCOPE));
4840       ui_out_text (uiout, "\nWatchpoint ");
4841       ui_out_field_int (uiout, "wpnum", b->base.number);
4842       ui_out_text (uiout,
4843                    " deleted because the program has left the block in\n\
4844 which its expression is valid.\n");     
4845
4846       /* Make sure the watchpoint's commands aren't executed.  */
4847       decref_counted_command_line (&b->base.commands);
4848       watchpoint_del_at_next_stop (b);
4849
4850       return WP_DELETED;
4851     }
4852 }
4853
4854 /* Return true if it looks like target has stopped due to hitting
4855    breakpoint location BL.  This function does not check if we should
4856    stop, only if BL explains the stop.  */
4857
4858 static int
4859 bpstat_check_location (const struct bp_location *bl,
4860                        struct address_space *aspace, CORE_ADDR bp_addr,
4861                        const struct target_waitstatus *ws)
4862 {
4863   struct breakpoint *b = bl->owner;
4864
4865   /* BL is from an existing breakpoint.  */
4866   gdb_assert (b != NULL);
4867
4868   return b->ops->breakpoint_hit (bl, aspace, bp_addr, ws);
4869 }
4870
4871 /* Determine if the watched values have actually changed, and we
4872    should stop.  If not, set BS->stop to 0.  */
4873
4874 static void
4875 bpstat_check_watchpoint (bpstat bs)
4876 {
4877   const struct bp_location *bl;
4878   struct watchpoint *b;
4879
4880   /* BS is built for existing struct breakpoint.  */
4881   bl = bs->bp_location_at;
4882   gdb_assert (bl != NULL);
4883   b = (struct watchpoint *) bs->breakpoint_at;
4884   gdb_assert (b != NULL);
4885
4886     {
4887       int must_check_value = 0;
4888       
4889       if (b->base.type == bp_watchpoint)
4890         /* For a software watchpoint, we must always check the
4891            watched value.  */
4892         must_check_value = 1;
4893       else if (b->watchpoint_triggered == watch_triggered_yes)
4894         /* We have a hardware watchpoint (read, write, or access)
4895            and the target earlier reported an address watched by
4896            this watchpoint.  */
4897         must_check_value = 1;
4898       else if (b->watchpoint_triggered == watch_triggered_unknown
4899                && b->base.type == bp_hardware_watchpoint)
4900         /* We were stopped by a hardware watchpoint, but the target could
4901            not report the data address.  We must check the watchpoint's
4902            value.  Access and read watchpoints are out of luck; without
4903            a data address, we can't figure it out.  */
4904         must_check_value = 1;
4905
4906       if (must_check_value)
4907         {
4908           char *message
4909             = xstrprintf ("Error evaluating expression for watchpoint %d\n",
4910                           b->base.number);
4911           struct cleanup *cleanups = make_cleanup (xfree, message);
4912           int e = catch_errors (watchpoint_check, bs, message,
4913                                 RETURN_MASK_ALL);
4914           do_cleanups (cleanups);
4915           switch (e)
4916             {
4917             case WP_DELETED:
4918               /* We've already printed what needs to be printed.  */
4919               bs->print_it = print_it_done;
4920               /* Stop.  */
4921               break;
4922             case WP_IGNORE:
4923               bs->print_it = print_it_noop;
4924               bs->stop = 0;
4925               break;
4926             case WP_VALUE_CHANGED:
4927               if (b->base.type == bp_read_watchpoint)
4928                 {
4929                   /* There are two cases to consider here:
4930
4931                      1. We're watching the triggered memory for reads.
4932                      In that case, trust the target, and always report
4933                      the watchpoint hit to the user.  Even though
4934                      reads don't cause value changes, the value may
4935                      have changed since the last time it was read, and
4936                      since we're not trapping writes, we will not see
4937                      those, and as such we should ignore our notion of
4938                      old value.
4939
4940                      2. We're watching the triggered memory for both
4941                      reads and writes.  There are two ways this may
4942                      happen:
4943
4944                      2.1. This is a target that can't break on data
4945                      reads only, but can break on accesses (reads or
4946                      writes), such as e.g., x86.  We detect this case
4947                      at the time we try to insert read watchpoints.
4948
4949                      2.2. Otherwise, the target supports read
4950                      watchpoints, but, the user set an access or write
4951                      watchpoint watching the same memory as this read
4952                      watchpoint.
4953
4954                      If we're watching memory writes as well as reads,
4955                      ignore watchpoint hits when we find that the
4956                      value hasn't changed, as reads don't cause
4957                      changes.  This still gives false positives when
4958                      the program writes the same value to memory as
4959                      what there was already in memory (we will confuse
4960                      it for a read), but it's much better than
4961                      nothing.  */
4962
4963                   int other_write_watchpoint = 0;
4964
4965                   if (bl->watchpoint_type == hw_read)
4966                     {
4967                       struct breakpoint *other_b;
4968
4969                       ALL_BREAKPOINTS (other_b)
4970                         if (other_b->type == bp_hardware_watchpoint
4971                             || other_b->type == bp_access_watchpoint)
4972                           {
4973                             struct watchpoint *other_w =
4974                               (struct watchpoint *) other_b;
4975
4976                             if (other_w->watchpoint_triggered
4977                                 == watch_triggered_yes)
4978                               {
4979                                 other_write_watchpoint = 1;
4980                                 break;
4981                               }
4982                           }
4983                     }
4984
4985                   if (other_write_watchpoint
4986                       || bl->watchpoint_type == hw_access)
4987                     {
4988                       /* We're watching the same memory for writes,
4989                          and the value changed since the last time we
4990                          updated it, so this trap must be for a write.
4991                          Ignore it.  */
4992                       bs->print_it = print_it_noop;
4993                       bs->stop = 0;
4994                     }
4995                 }
4996               break;
4997             case WP_VALUE_NOT_CHANGED:
4998               if (b->base.type == bp_hardware_watchpoint
4999                   || b->base.type == bp_watchpoint)
5000                 {
5001                   /* Don't stop: write watchpoints shouldn't fire if
5002                      the value hasn't changed.  */
5003                   bs->print_it = print_it_noop;
5004                   bs->stop = 0;
5005                 }
5006               /* Stop.  */
5007               break;
5008             default:
5009               /* Can't happen.  */
5010             case 0:
5011               /* Error from catch_errors.  */
5012               printf_filtered (_("Watchpoint %d deleted.\n"), b->base.number);
5013               watchpoint_del_at_next_stop (b);
5014               /* We've already printed what needs to be printed.  */
5015               bs->print_it = print_it_done;
5016               break;
5017             }
5018         }
5019       else      /* must_check_value == 0 */
5020         {
5021           /* This is a case where some watchpoint(s) triggered, but
5022              not at the address of this watchpoint, or else no
5023              watchpoint triggered after all.  So don't print
5024              anything for this watchpoint.  */
5025           bs->print_it = print_it_noop;
5026           bs->stop = 0;
5027         }
5028     }
5029 }
5030
5031
5032 /* Check conditions (condition proper, frame, thread and ignore count)
5033    of breakpoint referred to by BS.  If we should not stop for this
5034    breakpoint, set BS->stop to 0.  */
5035
5036 static void
5037 bpstat_check_breakpoint_conditions (bpstat bs, ptid_t ptid)
5038 {
5039   int thread_id = pid_to_thread_id (ptid);
5040   const struct bp_location *bl;
5041   struct breakpoint *b;
5042
5043   /* BS is built for existing struct breakpoint.  */
5044   bl = bs->bp_location_at;
5045   gdb_assert (bl != NULL);
5046   b = bs->breakpoint_at;
5047   gdb_assert (b != NULL);
5048
5049   /* Even if the target evaluated the condition on its end and notified GDB, we
5050      need to do so again since GDB does not know if we stopped due to a
5051      breakpoint or a single step breakpoint.  */
5052
5053   if (frame_id_p (b->frame_id)
5054       && !frame_id_eq (b->frame_id, get_stack_frame_id (get_current_frame ())))
5055     bs->stop = 0;
5056   else if (bs->stop)
5057     {
5058       int value_is_zero = 0;
5059       struct expression *cond;
5060
5061       /* Evaluate Python breakpoints that have a "stop"
5062          method implemented.  */
5063       if (b->py_bp_object)
5064         bs->stop = gdbpy_should_stop (b->py_bp_object);
5065
5066       if (is_watchpoint (b))
5067         {
5068           struct watchpoint *w = (struct watchpoint *) b;
5069
5070           cond = w->cond_exp;
5071         }
5072       else
5073         cond = bl->cond;
5074
5075       if (cond && b->disposition != disp_del_at_next_stop)
5076         {
5077           int within_current_scope = 1;
5078           struct watchpoint * w;
5079
5080           /* We use value_mark and value_free_to_mark because it could
5081              be a long time before we return to the command level and
5082              call free_all_values.  We can't call free_all_values
5083              because we might be in the middle of evaluating a
5084              function call.  */
5085           struct value *mark = value_mark ();
5086
5087           if (is_watchpoint (b))
5088             w = (struct watchpoint *) b;
5089           else
5090             w = NULL;
5091
5092           /* Need to select the frame, with all that implies so that
5093              the conditions will have the right context.  Because we
5094              use the frame, we will not see an inlined function's
5095              variables when we arrive at a breakpoint at the start
5096              of the inlined function; the current frame will be the
5097              call site.  */
5098           if (w == NULL || w->cond_exp_valid_block == NULL)
5099             select_frame (get_current_frame ());
5100           else
5101             {
5102               struct frame_info *frame;
5103
5104               /* For local watchpoint expressions, which particular
5105                  instance of a local is being watched matters, so we
5106                  keep track of the frame to evaluate the expression
5107                  in.  To evaluate the condition however, it doesn't
5108                  really matter which instantiation of the function
5109                  where the condition makes sense triggers the
5110                  watchpoint.  This allows an expression like "watch
5111                  global if q > 10" set in `func', catch writes to
5112                  global on all threads that call `func', or catch
5113                  writes on all recursive calls of `func' by a single
5114                  thread.  We simply always evaluate the condition in
5115                  the innermost frame that's executing where it makes
5116                  sense to evaluate the condition.  It seems
5117                  intuitive.  */
5118               frame = block_innermost_frame (w->cond_exp_valid_block);
5119               if (frame != NULL)
5120                 select_frame (frame);
5121               else
5122                 within_current_scope = 0;
5123             }
5124           if (within_current_scope)
5125             value_is_zero
5126               = catch_errors (breakpoint_cond_eval, cond,
5127                               "Error in testing breakpoint condition:\n",
5128                               RETURN_MASK_ALL);
5129           else
5130             {
5131               warning (_("Watchpoint condition cannot be tested "
5132                          "in the current scope"));
5133               /* If we failed to set the right context for this
5134                  watchpoint, unconditionally report it.  */
5135               value_is_zero = 0;
5136             }
5137           /* FIXME-someday, should give breakpoint #.  */
5138           value_free_to_mark (mark);
5139         }
5140
5141       if (cond && value_is_zero)
5142         {
5143           bs->stop = 0;
5144         }
5145       else if (b->thread != -1 && b->thread != thread_id)
5146         {
5147           bs->stop = 0;
5148         }
5149       else if (b->ignore_count > 0)
5150         {
5151           b->ignore_count--;
5152           bs->stop = 0;
5153           /* Increase the hit count even though we don't stop.  */
5154           ++(b->hit_count);
5155           observer_notify_breakpoint_modified (b);
5156         }       
5157     }
5158 }
5159
5160
5161 /* Get a bpstat associated with having just stopped at address
5162    BP_ADDR in thread PTID.
5163
5164    Determine whether we stopped at a breakpoint, etc, or whether we
5165    don't understand this stop.  Result is a chain of bpstat's such
5166    that:
5167
5168    if we don't understand the stop, the result is a null pointer.
5169
5170    if we understand why we stopped, the result is not null.
5171
5172    Each element of the chain refers to a particular breakpoint or
5173    watchpoint at which we have stopped.  (We may have stopped for
5174    several reasons concurrently.)
5175
5176    Each element of the chain has valid next, breakpoint_at,
5177    commands, FIXME??? fields.  */
5178
5179 bpstat
5180 bpstat_stop_status (struct address_space *aspace,
5181                     CORE_ADDR bp_addr, ptid_t ptid,
5182                     const struct target_waitstatus *ws)
5183 {
5184   struct breakpoint *b = NULL;
5185   struct bp_location *bl;
5186   struct bp_location *loc;
5187   /* First item of allocated bpstat's.  */
5188   bpstat bs_head = NULL, *bs_link = &bs_head;
5189   /* Pointer to the last thing in the chain currently.  */
5190   bpstat bs;
5191   int ix;
5192   int need_remove_insert;
5193   int removed_any;
5194
5195   /* First, build the bpstat chain with locations that explain a
5196      target stop, while being careful to not set the target running,
5197      as that may invalidate locations (in particular watchpoint
5198      locations are recreated).  Resuming will happen here with
5199      breakpoint conditions or watchpoint expressions that include
5200      inferior function calls.  */
5201
5202   ALL_BREAKPOINTS (b)
5203     {
5204       if (!breakpoint_enabled (b) && b->enable_state != bp_permanent)
5205         continue;
5206
5207       for (bl = b->loc; bl != NULL; bl = bl->next)
5208         {
5209           /* For hardware watchpoints, we look only at the first
5210              location.  The watchpoint_check function will work on the
5211              entire expression, not the individual locations.  For
5212              read watchpoints, the watchpoints_triggered function has
5213              checked all locations already.  */
5214           if (b->type == bp_hardware_watchpoint && bl != b->loc)
5215             break;
5216
5217           if (!bl->enabled || bl->shlib_disabled)
5218             continue;
5219
5220           if (!bpstat_check_location (bl, aspace, bp_addr, ws))
5221             continue;
5222
5223           /* Come here if it's a watchpoint, or if the break address
5224              matches.  */
5225
5226           bs = bpstat_alloc (bl, &bs_link);     /* Alloc a bpstat to
5227                                                    explain stop.  */
5228
5229           /* Assume we stop.  Should we find a watchpoint that is not
5230              actually triggered, or if the condition of the breakpoint
5231              evaluates as false, we'll reset 'stop' to 0.  */
5232           bs->stop = 1;
5233           bs->print = 1;
5234
5235           /* If this is a scope breakpoint, mark the associated
5236              watchpoint as triggered so that we will handle the
5237              out-of-scope event.  We'll get to the watchpoint next
5238              iteration.  */
5239           if (b->type == bp_watchpoint_scope && b->related_breakpoint != b)
5240             {
5241               struct watchpoint *w = (struct watchpoint *) b->related_breakpoint;
5242
5243               w->watchpoint_triggered = watch_triggered_yes;
5244             }
5245         }
5246     }
5247
5248   for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, loc); ++ix)
5249     {
5250       if (breakpoint_location_address_match (loc, aspace, bp_addr))
5251         {
5252           bs = bpstat_alloc (loc, &bs_link);
5253           /* For hits of moribund locations, we should just proceed.  */
5254           bs->stop = 0;
5255           bs->print = 0;
5256           bs->print_it = print_it_noop;
5257         }
5258     }
5259
5260   /* A bit of special processing for shlib breakpoints.  We need to
5261      process solib loading here, so that the lists of loaded and
5262      unloaded libraries are correct before we handle "catch load" and
5263      "catch unload".  */
5264   for (bs = bs_head; bs != NULL; bs = bs->next)
5265     {
5266       if (bs->breakpoint_at && bs->breakpoint_at->type == bp_shlib_event)
5267         {
5268           handle_solib_event ();
5269           break;
5270         }
5271     }
5272
5273   /* Now go through the locations that caused the target to stop, and
5274      check whether we're interested in reporting this stop to higher
5275      layers, or whether we should resume the target transparently.  */
5276
5277   removed_any = 0;
5278
5279   for (bs = bs_head; bs != NULL; bs = bs->next)
5280     {
5281       if (!bs->stop)
5282         continue;
5283
5284       b = bs->breakpoint_at;
5285       b->ops->check_status (bs);
5286       if (bs->stop)
5287         {
5288           bpstat_check_breakpoint_conditions (bs, ptid);
5289
5290           if (bs->stop)
5291             {
5292               ++(b->hit_count);
5293               observer_notify_breakpoint_modified (b);
5294
5295               /* We will stop here.  */
5296               if (b->disposition == disp_disable)
5297                 {
5298                   --(b->enable_count);
5299                   if (b->enable_count <= 0
5300                       && b->enable_state != bp_permanent)
5301                     b->enable_state = bp_disabled;
5302                   removed_any = 1;
5303                 }
5304               if (b->silent)
5305                 bs->print = 0;
5306               bs->commands = b->commands;
5307               incref_counted_command_line (bs->commands);
5308               if (command_line_is_silent (bs->commands
5309                                           ? bs->commands->commands : NULL))
5310                 bs->print = 0;
5311             }
5312
5313         }
5314
5315       /* Print nothing for this entry if we don't stop or don't
5316          print.  */
5317       if (!bs->stop || !bs->print)
5318         bs->print_it = print_it_noop;
5319     }
5320
5321   /* If we aren't stopping, the value of some hardware watchpoint may
5322      not have changed, but the intermediate memory locations we are
5323      watching may have.  Don't bother if we're stopping; this will get
5324      done later.  */
5325   need_remove_insert = 0;
5326   if (! bpstat_causes_stop (bs_head))
5327     for (bs = bs_head; bs != NULL; bs = bs->next)
5328       if (!bs->stop
5329           && bs->breakpoint_at
5330           && is_hardware_watchpoint (bs->breakpoint_at))
5331         {
5332           struct watchpoint *w = (struct watchpoint *) bs->breakpoint_at;
5333
5334           update_watchpoint (w, 0 /* don't reparse.  */);
5335           need_remove_insert = 1;
5336         }
5337
5338   if (need_remove_insert)
5339     update_global_location_list (1);
5340   else if (removed_any)
5341     update_global_location_list (0);
5342
5343   return bs_head;
5344 }
5345
5346 static void
5347 handle_jit_event (void)
5348 {
5349   struct frame_info *frame;
5350   struct gdbarch *gdbarch;
5351
5352   /* Switch terminal for any messages produced by
5353      breakpoint_re_set.  */
5354   target_terminal_ours_for_output ();
5355
5356   frame = get_current_frame ();
5357   gdbarch = get_frame_arch (frame);
5358
5359   jit_event_handler (gdbarch);
5360
5361   target_terminal_inferior ();
5362 }
5363
5364 /* Handle an solib event by calling solib_add.  */
5365
5366 void
5367 handle_solib_event (void)
5368 {
5369   clear_program_space_solib_cache (current_inferior ()->pspace);
5370
5371   /* Check for any newly added shared libraries if we're supposed to
5372      be adding them automatically.  Switch terminal for any messages
5373      produced by breakpoint_re_set.  */
5374   target_terminal_ours_for_output ();
5375 #ifdef SOLIB_ADD
5376   SOLIB_ADD (NULL, 0, &current_target, auto_solib_add);
5377 #else
5378   solib_add (NULL, 0, &current_target, auto_solib_add);
5379 #endif
5380   target_terminal_inferior ();
5381 }
5382
5383 /* Prepare WHAT final decision for infrun.  */
5384
5385 /* Decide what infrun needs to do with this bpstat.  */
5386
5387 struct bpstat_what
5388 bpstat_what (bpstat bs_head)
5389 {
5390   struct bpstat_what retval;
5391   int jit_event = 0;
5392   bpstat bs;
5393
5394   retval.main_action = BPSTAT_WHAT_KEEP_CHECKING;
5395   retval.call_dummy = STOP_NONE;
5396   retval.is_longjmp = 0;
5397
5398   for (bs = bs_head; bs != NULL; bs = bs->next)
5399     {
5400       /* Extract this BS's action.  After processing each BS, we check
5401          if its action overrides all we've seem so far.  */
5402       enum bpstat_what_main_action this_action = BPSTAT_WHAT_KEEP_CHECKING;
5403       enum bptype bptype;
5404
5405       if (bs->breakpoint_at == NULL)
5406         {
5407           /* I suspect this can happen if it was a momentary
5408              breakpoint which has since been deleted.  */
5409           bptype = bp_none;
5410         }
5411       else
5412         bptype = bs->breakpoint_at->type;
5413
5414       switch (bptype)
5415         {
5416         case bp_none:
5417           break;
5418         case bp_breakpoint:
5419         case bp_hardware_breakpoint:
5420         case bp_until:
5421         case bp_finish:
5422         case bp_shlib_event:
5423           if (bs->stop)
5424             {
5425               if (bs->print)
5426                 this_action = BPSTAT_WHAT_STOP_NOISY;
5427               else
5428                 this_action = BPSTAT_WHAT_STOP_SILENT;
5429             }
5430           else
5431             this_action = BPSTAT_WHAT_SINGLE;
5432           break;
5433         case bp_watchpoint:
5434         case bp_hardware_watchpoint:
5435         case bp_read_watchpoint:
5436         case bp_access_watchpoint:
5437           if (bs->stop)
5438             {
5439               if (bs->print)
5440                 this_action = BPSTAT_WHAT_STOP_NOISY;
5441               else
5442                 this_action = BPSTAT_WHAT_STOP_SILENT;
5443             }
5444           else
5445             {
5446               /* There was a watchpoint, but we're not stopping.
5447                  This requires no further action.  */
5448             }
5449           break;
5450         case bp_longjmp:
5451         case bp_longjmp_call_dummy:
5452         case bp_exception:
5453           this_action = BPSTAT_WHAT_SET_LONGJMP_RESUME;
5454           retval.is_longjmp = bptype != bp_exception;
5455           break;
5456         case bp_longjmp_resume:
5457         case bp_exception_resume:
5458           this_action = BPSTAT_WHAT_CLEAR_LONGJMP_RESUME;
5459           retval.is_longjmp = bptype == bp_longjmp_resume;
5460           break;
5461         case bp_step_resume:
5462           if (bs->stop)
5463             this_action = BPSTAT_WHAT_STEP_RESUME;
5464           else
5465             {
5466               /* It is for the wrong frame.  */
5467               this_action = BPSTAT_WHAT_SINGLE;
5468             }
5469           break;
5470         case bp_hp_step_resume:
5471           if (bs->stop)
5472             this_action = BPSTAT_WHAT_HP_STEP_RESUME;
5473           else
5474             {
5475               /* It is for the wrong frame.  */
5476               this_action = BPSTAT_WHAT_SINGLE;
5477             }
5478           break;
5479         case bp_watchpoint_scope:
5480         case bp_thread_event:
5481         case bp_overlay_event:
5482         case bp_longjmp_master:
5483         case bp_std_terminate_master:
5484         case bp_exception_master:
5485           this_action = BPSTAT_WHAT_SINGLE;
5486           break;
5487         case bp_catchpoint:
5488           if (bs->stop)
5489             {
5490               if (bs->print)
5491                 this_action = BPSTAT_WHAT_STOP_NOISY;
5492               else
5493                 this_action = BPSTAT_WHAT_STOP_SILENT;
5494             }
5495           else
5496             {
5497               /* There was a catchpoint, but we're not stopping.
5498                  This requires no further action.  */
5499             }
5500           break;
5501         case bp_jit_event:
5502           jit_event = 1;
5503           this_action = BPSTAT_WHAT_SINGLE;
5504           break;
5505         case bp_call_dummy:
5506           /* Make sure the action is stop (silent or noisy),
5507              so infrun.c pops the dummy frame.  */
5508           retval.call_dummy = STOP_STACK_DUMMY;
5509           this_action = BPSTAT_WHAT_STOP_SILENT;
5510           break;
5511         case bp_std_terminate:
5512           /* Make sure the action is stop (silent or noisy),
5513              so infrun.c pops the dummy frame.  */
5514           retval.call_dummy = STOP_STD_TERMINATE;
5515           this_action = BPSTAT_WHAT_STOP_SILENT;
5516           break;
5517         case bp_tracepoint:
5518         case bp_fast_tracepoint:
5519         case bp_static_tracepoint:
5520           /* Tracepoint hits should not be reported back to GDB, and
5521              if one got through somehow, it should have been filtered
5522              out already.  */
5523           internal_error (__FILE__, __LINE__,
5524                           _("bpstat_what: tracepoint encountered"));
5525           break;
5526         case bp_gnu_ifunc_resolver:
5527           /* Step over it (and insert bp_gnu_ifunc_resolver_return).  */
5528           this_action = BPSTAT_WHAT_SINGLE;
5529           break;
5530         case bp_gnu_ifunc_resolver_return:
5531           /* The breakpoint will be removed, execution will restart from the
5532              PC of the former breakpoint.  */
5533           this_action = BPSTAT_WHAT_KEEP_CHECKING;
5534           break;
5535
5536         case bp_dprintf:
5537           this_action = BPSTAT_WHAT_STOP_SILENT;
5538           break;
5539
5540         default:
5541           internal_error (__FILE__, __LINE__,
5542                           _("bpstat_what: unhandled bptype %d"), (int) bptype);
5543         }
5544
5545       retval.main_action = max (retval.main_action, this_action);
5546     }
5547
5548   /* These operations may affect the bs->breakpoint_at state so they are
5549      delayed after MAIN_ACTION is decided above.  */
5550
5551   if (jit_event)
5552     {
5553       if (debug_infrun)
5554         fprintf_unfiltered (gdb_stdlog, "bpstat_what: bp_jit_event\n");
5555
5556       handle_jit_event ();
5557     }
5558
5559   for (bs = bs_head; bs != NULL; bs = bs->next)
5560     {
5561       struct breakpoint *b = bs->breakpoint_at;
5562
5563       if (b == NULL)
5564         continue;
5565       switch (b->type)
5566         {
5567         case bp_gnu_ifunc_resolver:
5568           gnu_ifunc_resolver_stop (b);
5569           break;
5570         case bp_gnu_ifunc_resolver_return:
5571           gnu_ifunc_resolver_return_stop (b);
5572           break;
5573         }
5574     }
5575
5576   return retval;
5577 }
5578
5579 /* Nonzero if we should step constantly (e.g. watchpoints on machines
5580    without hardware support).  This isn't related to a specific bpstat,
5581    just to things like whether watchpoints are set.  */
5582
5583 int
5584 bpstat_should_step (void)
5585 {
5586   struct breakpoint *b;
5587
5588   ALL_BREAKPOINTS (b)
5589     if (breakpoint_enabled (b) && b->type == bp_watchpoint && b->loc != NULL)
5590       return 1;
5591   return 0;
5592 }
5593
5594 int
5595 bpstat_causes_stop (bpstat bs)
5596 {
5597   for (; bs != NULL; bs = bs->next)
5598     if (bs->stop)
5599       return 1;
5600
5601   return 0;
5602 }
5603
5604 \f
5605
5606 /* Compute a string of spaces suitable to indent the next line
5607    so it starts at the position corresponding to the table column
5608    named COL_NAME in the currently active table of UIOUT.  */
5609
5610 static char *
5611 wrap_indent_at_field (struct ui_out *uiout, const char *col_name)
5612 {
5613   static char wrap_indent[80];
5614   int i, total_width, width, align;
5615   char *text;
5616
5617   total_width = 0;
5618   for (i = 1; ui_out_query_field (uiout, i, &width, &align, &text); i++)
5619     {
5620       if (strcmp (text, col_name) == 0)
5621         {
5622           gdb_assert (total_width < sizeof wrap_indent);
5623           memset (wrap_indent, ' ', total_width);
5624           wrap_indent[total_width] = 0;
5625
5626           return wrap_indent;
5627         }
5628
5629       total_width += width + 1;
5630     }
5631
5632   return NULL;
5633 }
5634
5635 /* Determine if the locations of this breakpoint will have their conditions
5636    evaluated by the target, host or a mix of both.  Returns the following:
5637
5638     "host": Host evals condition.
5639     "host or target": Host or Target evals condition.
5640     "target": Target evals condition.
5641 */
5642
5643 static const char *
5644 bp_condition_evaluator (struct breakpoint *b)
5645 {
5646   struct bp_location *bl;
5647   char host_evals = 0;
5648   char target_evals = 0;
5649
5650   if (!b)
5651     return NULL;
5652
5653   if (!is_breakpoint (b))
5654     return NULL;
5655
5656   if (gdb_evaluates_breakpoint_condition_p ()
5657       || !target_supports_evaluation_of_breakpoint_conditions ())
5658     return condition_evaluation_host;
5659
5660   for (bl = b->loc; bl; bl = bl->next)
5661     {
5662       if (bl->cond_bytecode)
5663         target_evals++;
5664       else
5665         host_evals++;
5666     }
5667
5668   if (host_evals && target_evals)
5669     return condition_evaluation_both;
5670   else if (target_evals)
5671     return condition_evaluation_target;
5672   else
5673     return condition_evaluation_host;
5674 }
5675
5676 /* Determine the breakpoint location's condition evaluator.  This is
5677    similar to bp_condition_evaluator, but for locations.  */
5678
5679 static const char *
5680 bp_location_condition_evaluator (struct bp_location *bl)
5681 {
5682   if (bl && !is_breakpoint (bl->owner))
5683     return NULL;
5684
5685   if (gdb_evaluates_breakpoint_condition_p ()
5686       || !target_supports_evaluation_of_breakpoint_conditions ())
5687     return condition_evaluation_host;
5688
5689   if (bl && bl->cond_bytecode)
5690     return condition_evaluation_target;
5691   else
5692     return condition_evaluation_host;
5693 }
5694
5695 /* Print the LOC location out of the list of B->LOC locations.  */
5696
5697 static void
5698 print_breakpoint_location (struct breakpoint *b,
5699                            struct bp_location *loc)
5700 {
5701   struct ui_out *uiout = current_uiout;
5702   struct cleanup *old_chain = save_current_program_space ();
5703
5704   if (loc != NULL && loc->shlib_disabled)
5705     loc = NULL;
5706
5707   if (loc != NULL)
5708     set_current_program_space (loc->pspace);
5709
5710   if (b->display_canonical)
5711     ui_out_field_string (uiout, "what", b->addr_string);
5712   else if (loc && loc->symtab)
5713     {
5714       struct symbol *sym 
5715         = find_pc_sect_function (loc->address, loc->section);
5716       if (sym)
5717         {
5718           ui_out_text (uiout, "in ");
5719           ui_out_field_string (uiout, "func",
5720                                SYMBOL_PRINT_NAME (sym));
5721           ui_out_text (uiout, " ");
5722           ui_out_wrap_hint (uiout, wrap_indent_at_field (uiout, "what"));
5723           ui_out_text (uiout, "at ");
5724         }
5725       ui_out_field_string (uiout, "file",
5726                            symtab_to_filename_for_display (loc->symtab));
5727       ui_out_text (uiout, ":");
5728
5729       if (ui_out_is_mi_like_p (uiout))
5730         ui_out_field_string (uiout, "fullname",
5731                              symtab_to_fullname (loc->symtab));
5732       
5733       ui_out_field_int (uiout, "line", loc->line_number);
5734     }
5735   else if (loc)
5736     {
5737       struct ui_file *stb = mem_fileopen ();
5738       struct cleanup *stb_chain = make_cleanup_ui_file_delete (stb);
5739
5740       print_address_symbolic (loc->gdbarch, loc->address, stb,
5741                               demangle, "");
5742       ui_out_field_stream (uiout, "at", stb);
5743
5744       do_cleanups (stb_chain);
5745     }
5746   else
5747     ui_out_field_string (uiout, "pending", b->addr_string);
5748
5749   if (loc && is_breakpoint (b)
5750       && breakpoint_condition_evaluation_mode () == condition_evaluation_target
5751       && bp_condition_evaluator (b) == condition_evaluation_both)
5752     {
5753       ui_out_text (uiout, " (");
5754       ui_out_field_string (uiout, "evaluated-by",
5755                            bp_location_condition_evaluator (loc));
5756       ui_out_text (uiout, ")");
5757     }
5758
5759   do_cleanups (old_chain);
5760 }
5761
5762 static const char *
5763 bptype_string (enum bptype type)
5764 {
5765   struct ep_type_description
5766     {
5767       enum bptype type;
5768       char *description;
5769     };
5770   static struct ep_type_description bptypes[] =
5771   {
5772     {bp_none, "?deleted?"},
5773     {bp_breakpoint, "breakpoint"},
5774     {bp_hardware_breakpoint, "hw breakpoint"},
5775     {bp_until, "until"},
5776     {bp_finish, "finish"},
5777     {bp_watchpoint, "watchpoint"},
5778     {bp_hardware_watchpoint, "hw watchpoint"},
5779     {bp_read_watchpoint, "read watchpoint"},
5780     {bp_access_watchpoint, "acc watchpoint"},
5781     {bp_longjmp, "longjmp"},
5782     {bp_longjmp_resume, "longjmp resume"},
5783     {bp_longjmp_call_dummy, "longjmp for call dummy"},
5784     {bp_exception, "exception"},
5785     {bp_exception_resume, "exception resume"},
5786     {bp_step_resume, "step resume"},
5787     {bp_hp_step_resume, "high-priority step resume"},
5788     {bp_watchpoint_scope, "watchpoint scope"},
5789     {bp_call_dummy, "call dummy"},
5790     {bp_std_terminate, "std::terminate"},
5791     {bp_shlib_event, "shlib events"},
5792     {bp_thread_event, "thread events"},
5793     {bp_overlay_event, "overlay events"},
5794     {bp_longjmp_master, "longjmp master"},
5795     {bp_std_terminate_master, "std::terminate master"},
5796     {bp_exception_master, "exception master"},
5797     {bp_catchpoint, "catchpoint"},
5798     {bp_tracepoint, "tracepoint"},
5799     {bp_fast_tracepoint, "fast tracepoint"},
5800     {bp_static_tracepoint, "static tracepoint"},
5801     {bp_dprintf, "dprintf"},
5802     {bp_jit_event, "jit events"},
5803     {bp_gnu_ifunc_resolver, "STT_GNU_IFUNC resolver"},
5804     {bp_gnu_ifunc_resolver_return, "STT_GNU_IFUNC resolver return"},
5805   };
5806
5807   if (((int) type >= (sizeof (bptypes) / sizeof (bptypes[0])))
5808       || ((int) type != bptypes[(int) type].type))
5809     internal_error (__FILE__, __LINE__,
5810                     _("bptypes table does not describe type #%d."),
5811                     (int) type);
5812
5813   return bptypes[(int) type].description;
5814 }
5815
5816 DEF_VEC_I(int);
5817
5818 /* For MI, output a field named 'thread-groups' with a list as the value.
5819    For CLI, prefix the list with the string 'inf'. */
5820
5821 static void
5822 output_thread_groups (struct ui_out *uiout,
5823                       const char *field_name,
5824                       VEC(int) *inf_num,
5825                       int mi_only)
5826 {
5827   struct cleanup *back_to = make_cleanup_ui_out_list_begin_end (uiout,
5828                                                                 field_name);
5829   int is_mi = ui_out_is_mi_like_p (uiout);
5830   int inf;
5831   int i;
5832
5833   /* For backward compatibility, don't display inferiors in CLI unless
5834      there are several.  Always display them for MI. */
5835   if (!is_mi && mi_only)
5836     return;
5837
5838   for (i = 0; VEC_iterate (int, inf_num, i, inf); ++i)
5839     {
5840       if (is_mi)
5841         {
5842           char mi_group[10];
5843
5844           xsnprintf (mi_group, sizeof (mi_group), "i%d", inf);
5845           ui_out_field_string (uiout, NULL, mi_group);
5846         }
5847       else
5848         {
5849           if (i == 0)
5850             ui_out_text (uiout, " inf ");
5851           else
5852             ui_out_text (uiout, ", ");
5853         
5854           ui_out_text (uiout, plongest (inf));
5855         }
5856     }
5857
5858   do_cleanups (back_to);
5859 }
5860
5861 /* Print B to gdb_stdout.  */
5862
5863 static void
5864 print_one_breakpoint_location (struct breakpoint *b,
5865                                struct bp_location *loc,
5866                                int loc_number,
5867                                struct bp_location **last_loc,
5868                                int allflag)
5869 {
5870   struct command_line *l;
5871   static char bpenables[] = "nynny";
5872
5873   struct ui_out *uiout = current_uiout;
5874   int header_of_multiple = 0;
5875   int part_of_multiple = (loc != NULL);
5876   struct value_print_options opts;
5877
5878   get_user_print_options (&opts);
5879
5880   gdb_assert (!loc || loc_number != 0);
5881   /* See comment in print_one_breakpoint concerning treatment of
5882      breakpoints with single disabled location.  */
5883   if (loc == NULL 
5884       && (b->loc != NULL 
5885           && (b->loc->next != NULL || !b->loc->enabled)))
5886     header_of_multiple = 1;
5887   if (loc == NULL)
5888     loc = b->loc;
5889
5890   annotate_record ();
5891
5892   /* 1 */
5893   annotate_field (0);
5894   if (part_of_multiple)
5895     {
5896       char *formatted;
5897       formatted = xstrprintf ("%d.%d", b->number, loc_number);
5898       ui_out_field_string (uiout, "number", formatted);
5899       xfree (formatted);
5900     }
5901   else
5902     {
5903       ui_out_field_int (uiout, "number", b->number);
5904     }
5905
5906   /* 2 */
5907   annotate_field (1);
5908   if (part_of_multiple)
5909     ui_out_field_skip (uiout, "type");
5910   else
5911     ui_out_field_string (uiout, "type", bptype_string (b->type));
5912
5913   /* 3 */
5914   annotate_field (2);
5915   if (part_of_multiple)
5916     ui_out_field_skip (uiout, "disp");
5917   else
5918     ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
5919
5920
5921   /* 4 */
5922   annotate_field (3);
5923   if (part_of_multiple)
5924     ui_out_field_string (uiout, "enabled", loc->enabled ? "y" : "n");
5925   else
5926     ui_out_field_fmt (uiout, "enabled", "%c", 
5927                       bpenables[(int) b->enable_state]);
5928   ui_out_spaces (uiout, 2);
5929
5930   
5931   /* 5 and 6 */
5932   if (b->ops != NULL && b->ops->print_one != NULL)
5933     {
5934       /* Although the print_one can possibly print all locations,
5935          calling it here is not likely to get any nice result.  So,
5936          make sure there's just one location.  */
5937       gdb_assert (b->loc == NULL || b->loc->next == NULL);
5938       b->ops->print_one (b, last_loc);
5939     }
5940   else
5941     switch (b->type)
5942       {
5943       case bp_none:
5944         internal_error (__FILE__, __LINE__,
5945                         _("print_one_breakpoint: bp_none encountered\n"));
5946         break;
5947
5948       case bp_watchpoint:
5949       case bp_hardware_watchpoint:
5950       case bp_read_watchpoint:
5951       case bp_access_watchpoint:
5952         {
5953           struct watchpoint *w = (struct watchpoint *) b;
5954
5955           /* Field 4, the address, is omitted (which makes the columns
5956              not line up too nicely with the headers, but the effect
5957              is relatively readable).  */
5958           if (opts.addressprint)
5959             ui_out_field_skip (uiout, "addr");
5960           annotate_field (5);
5961           ui_out_field_string (uiout, "what", w->exp_string);
5962         }
5963         break;
5964
5965       case bp_breakpoint:
5966       case bp_hardware_breakpoint:
5967       case bp_until:
5968       case bp_finish:
5969       case bp_longjmp:
5970       case bp_longjmp_resume:
5971       case bp_longjmp_call_dummy:
5972       case bp_exception:
5973       case bp_exception_resume:
5974       case bp_step_resume:
5975       case bp_hp_step_resume:
5976       case bp_watchpoint_scope:
5977       case bp_call_dummy:
5978       case bp_std_terminate:
5979       case bp_shlib_event:
5980       case bp_thread_event:
5981       case bp_overlay_event:
5982       case bp_longjmp_master:
5983       case bp_std_terminate_master:
5984       case bp_exception_master:
5985       case bp_tracepoint:
5986       case bp_fast_tracepoint:
5987       case bp_static_tracepoint:
5988       case bp_dprintf:
5989       case bp_jit_event:
5990       case bp_gnu_ifunc_resolver:
5991       case bp_gnu_ifunc_resolver_return:
5992         if (opts.addressprint)
5993           {
5994             annotate_field (4);
5995             if (header_of_multiple)
5996               ui_out_field_string (uiout, "addr", "<MULTIPLE>");
5997             else if (b->loc == NULL || loc->shlib_disabled)
5998               ui_out_field_string (uiout, "addr", "<PENDING>");
5999             else
6000               ui_out_field_core_addr (uiout, "addr",
6001                                       loc->gdbarch, loc->address);
6002           }
6003         annotate_field (5);
6004         if (!header_of_multiple)
6005           print_breakpoint_location (b, loc);
6006         if (b->loc)
6007           *last_loc = b->loc;
6008         break;
6009       }
6010
6011
6012   if (loc != NULL && !header_of_multiple)
6013     {
6014       struct inferior *inf;
6015       VEC(int) *inf_num = NULL;
6016       int mi_only = 1;
6017
6018       ALL_INFERIORS (inf)
6019         {
6020           if (inf->pspace == loc->pspace)
6021             VEC_safe_push (int, inf_num, inf->num);
6022         }
6023
6024         /* For backward compatibility, don't display inferiors in CLI unless
6025            there are several.  Always display for MI. */
6026         if (allflag
6027             || (!gdbarch_has_global_breakpoints (target_gdbarch ())
6028                 && (number_of_program_spaces () > 1
6029                     || number_of_inferiors () > 1)
6030                 /* LOC is for existing B, it cannot be in
6031                    moribund_locations and thus having NULL OWNER.  */
6032                 && loc->owner->type != bp_catchpoint))
6033         mi_only = 0;
6034       output_thread_groups (uiout, "thread-groups", inf_num, mi_only);
6035       VEC_free (int, inf_num);
6036     }
6037
6038   if (!part_of_multiple)
6039     {
6040       if (b->thread != -1)
6041         {
6042           /* FIXME: This seems to be redundant and lost here; see the
6043              "stop only in" line a little further down.  */
6044           ui_out_text (uiout, " thread ");
6045           ui_out_field_int (uiout, "thread", b->thread);
6046         }
6047       else if (b->task != 0)
6048         {
6049           ui_out_text (uiout, " task ");
6050           ui_out_field_int (uiout, "task", b->task);
6051         }
6052     }
6053
6054   ui_out_text (uiout, "\n");
6055
6056   if (!part_of_multiple)
6057     b->ops->print_one_detail (b, uiout);
6058
6059   if (part_of_multiple && frame_id_p (b->frame_id))
6060     {
6061       annotate_field (6);
6062       ui_out_text (uiout, "\tstop only in stack frame at ");
6063       /* FIXME: cagney/2002-12-01: Shouldn't be poking around inside
6064          the frame ID.  */
6065       ui_out_field_core_addr (uiout, "frame",
6066                               b->gdbarch, b->frame_id.stack_addr);
6067       ui_out_text (uiout, "\n");
6068     }
6069   
6070   if (!part_of_multiple && b->cond_string)
6071     {
6072       annotate_field (7);
6073       if (is_tracepoint (b))
6074         ui_out_text (uiout, "\ttrace only if ");
6075       else
6076         ui_out_text (uiout, "\tstop only if ");
6077       ui_out_field_string (uiout, "cond", b->cond_string);
6078
6079       /* Print whether the target is doing the breakpoint's condition
6080          evaluation.  If GDB is doing the evaluation, don't print anything.  */
6081       if (is_breakpoint (b)
6082           && breakpoint_condition_evaluation_mode ()
6083           == condition_evaluation_target)
6084         {
6085           ui_out_text (uiout, " (");
6086           ui_out_field_string (uiout, "evaluated-by",
6087                                bp_condition_evaluator (b));
6088           ui_out_text (uiout, " evals)");
6089         }
6090       ui_out_text (uiout, "\n");
6091     }
6092
6093   if (!part_of_multiple && b->thread != -1)
6094     {
6095       /* FIXME should make an annotation for this.  */
6096       ui_out_text (uiout, "\tstop only in thread ");
6097       ui_out_field_int (uiout, "thread", b->thread);
6098       ui_out_text (uiout, "\n");
6099     }
6100   
6101   if (!part_of_multiple)
6102     {
6103       if (b->hit_count)
6104         {
6105           /* FIXME should make an annotation for this.  */
6106           if (is_catchpoint (b))
6107             ui_out_text (uiout, "\tcatchpoint");
6108           else if (is_tracepoint (b))
6109             ui_out_text (uiout, "\ttracepoint");
6110           else
6111             ui_out_text (uiout, "\tbreakpoint");
6112           ui_out_text (uiout, " already hit ");
6113           ui_out_field_int (uiout, "times", b->hit_count);
6114           if (b->hit_count == 1)
6115             ui_out_text (uiout, " time\n");
6116           else
6117             ui_out_text (uiout, " times\n");
6118         }
6119       else
6120         {
6121           /* Output the count also if it is zero, but only if this is mi.  */
6122           if (ui_out_is_mi_like_p (uiout))
6123             ui_out_field_int (uiout, "times", b->hit_count);
6124         }
6125     }
6126
6127   if (!part_of_multiple && b->ignore_count)
6128     {
6129       annotate_field (8);
6130       ui_out_text (uiout, "\tignore next ");
6131       ui_out_field_int (uiout, "ignore", b->ignore_count);
6132       ui_out_text (uiout, " hits\n");
6133     }
6134
6135   /* Note that an enable count of 1 corresponds to "enable once"
6136      behavior, which is reported by the combination of enablement and
6137      disposition, so we don't need to mention it here.  */
6138   if (!part_of_multiple && b->enable_count > 1)
6139     {
6140       annotate_field (8);
6141       ui_out_text (uiout, "\tdisable after ");
6142       /* Tweak the wording to clarify that ignore and enable counts
6143          are distinct, and have additive effect.  */
6144       if (b->ignore_count)
6145         ui_out_text (uiout, "additional ");
6146       else
6147         ui_out_text (uiout, "next ");
6148       ui_out_field_int (uiout, "enable", b->enable_count);
6149       ui_out_text (uiout, " hits\n");
6150     }
6151
6152   if (!part_of_multiple && is_tracepoint (b))
6153     {
6154       struct tracepoint *tp = (struct tracepoint *) b;
6155
6156       if (tp->traceframe_usage)
6157         {
6158           ui_out_text (uiout, "\ttrace buffer usage ");
6159           ui_out_field_int (uiout, "traceframe-usage", tp->traceframe_usage);
6160           ui_out_text (uiout, " bytes\n");
6161         }
6162     }
6163
6164   l = b->commands ? b->commands->commands : NULL;
6165   if (!part_of_multiple && l)
6166     {
6167       struct cleanup *script_chain;
6168
6169       annotate_field (9);
6170       script_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "script");
6171       print_command_lines (uiout, l, 4);
6172       do_cleanups (script_chain);
6173     }
6174
6175   if (is_tracepoint (b))
6176     {
6177       struct tracepoint *t = (struct tracepoint *) b;
6178
6179       if (!part_of_multiple && t->pass_count)
6180         {
6181           annotate_field (10);
6182           ui_out_text (uiout, "\tpass count ");
6183           ui_out_field_int (uiout, "pass", t->pass_count);
6184           ui_out_text (uiout, " \n");
6185         }
6186
6187       /* Don't display it when tracepoint or tracepoint location is
6188          pending.   */
6189       if (!header_of_multiple && loc != NULL && !loc->shlib_disabled)
6190         {
6191           annotate_field (11);
6192
6193           if (ui_out_is_mi_like_p (uiout))
6194             ui_out_field_string (uiout, "installed",
6195                                  loc->inserted ? "y" : "n");
6196           else
6197             {
6198               if (loc->inserted)
6199                 ui_out_text (uiout, "\t");
6200               else
6201                 ui_out_text (uiout, "\tnot ");
6202               ui_out_text (uiout, "installed on target\n");
6203             }
6204         }
6205     }
6206
6207   if (ui_out_is_mi_like_p (uiout) && !part_of_multiple)
6208     {
6209       if (is_watchpoint (b))
6210         {
6211           struct watchpoint *w = (struct watchpoint *) b;
6212
6213           ui_out_field_string (uiout, "original-location", w->exp_string);
6214         }
6215       else if (b->addr_string)
6216         ui_out_field_string (uiout, "original-location", b->addr_string);
6217     }
6218 }
6219
6220 static void
6221 print_one_breakpoint (struct breakpoint *b,
6222                       struct bp_location **last_loc, 
6223                       int allflag)
6224 {
6225   struct cleanup *bkpt_chain;
6226   struct ui_out *uiout = current_uiout;
6227
6228   bkpt_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "bkpt");
6229
6230   print_one_breakpoint_location (b, NULL, 0, last_loc, allflag);
6231   do_cleanups (bkpt_chain);
6232
6233   /* If this breakpoint has custom print function,
6234      it's already printed.  Otherwise, print individual
6235      locations, if any.  */
6236   if (b->ops == NULL || b->ops->print_one == NULL)
6237     {
6238       /* If breakpoint has a single location that is disabled, we
6239          print it as if it had several locations, since otherwise it's
6240          hard to represent "breakpoint enabled, location disabled"
6241          situation.
6242
6243          Note that while hardware watchpoints have several locations
6244          internally, that's not a property exposed to user.  */
6245       if (b->loc 
6246           && !is_hardware_watchpoint (b)
6247           && (b->loc->next || !b->loc->enabled))
6248         {
6249           struct bp_location *loc;
6250           int n = 1;
6251
6252           for (loc = b->loc; loc; loc = loc->next, ++n)
6253             {
6254               struct cleanup *inner2 =
6255                 make_cleanup_ui_out_tuple_begin_end (uiout, NULL);
6256               print_one_breakpoint_location (b, loc, n, last_loc, allflag);
6257               do_cleanups (inner2);
6258             }
6259         }
6260     }
6261 }
6262
6263 static int
6264 breakpoint_address_bits (struct breakpoint *b)
6265 {
6266   int print_address_bits = 0;
6267   struct bp_location *loc;
6268
6269   for (loc = b->loc; loc; loc = loc->next)
6270     {
6271       int addr_bit;
6272
6273       /* Software watchpoints that aren't watching memory don't have
6274          an address to print.  */
6275       if (b->type == bp_watchpoint && loc->watchpoint_type == -1)
6276         continue;
6277
6278       addr_bit = gdbarch_addr_bit (loc->gdbarch);
6279       if (addr_bit > print_address_bits)
6280         print_address_bits = addr_bit;
6281     }
6282
6283   return print_address_bits;
6284 }
6285
6286 struct captured_breakpoint_query_args
6287   {
6288     int bnum;
6289   };
6290
6291 static int
6292 do_captured_breakpoint_query (struct ui_out *uiout, void *data)
6293 {
6294   struct captured_breakpoint_query_args *args = data;
6295   struct breakpoint *b;
6296   struct bp_location *dummy_loc = NULL;
6297
6298   ALL_BREAKPOINTS (b)
6299     {
6300       if (args->bnum == b->number)
6301         {
6302           print_one_breakpoint (b, &dummy_loc, 0);
6303           return GDB_RC_OK;
6304         }
6305     }
6306   return GDB_RC_NONE;
6307 }
6308
6309 enum gdb_rc
6310 gdb_breakpoint_query (struct ui_out *uiout, int bnum, 
6311                       char **error_message)
6312 {
6313   struct captured_breakpoint_query_args args;
6314
6315   args.bnum = bnum;
6316   /* For the moment we don't trust print_one_breakpoint() to not throw
6317      an error.  */
6318   if (catch_exceptions_with_msg (uiout, do_captured_breakpoint_query, &args,
6319                                  error_message, RETURN_MASK_ALL) < 0)
6320     return GDB_RC_FAIL;
6321   else
6322     return GDB_RC_OK;
6323 }
6324
6325 /* Return true if this breakpoint was set by the user, false if it is
6326    internal or momentary.  */
6327
6328 int
6329 user_breakpoint_p (struct breakpoint *b)
6330 {
6331   return b->number > 0;
6332 }
6333
6334 /* Print information on user settable breakpoint (watchpoint, etc)
6335    number BNUM.  If BNUM is -1 print all user-settable breakpoints.
6336    If ALLFLAG is non-zero, include non-user-settable breakpoints.  If
6337    FILTER is non-NULL, call it on each breakpoint and only include the
6338    ones for which it returns non-zero.  Return the total number of
6339    breakpoints listed.  */
6340
6341 static int
6342 breakpoint_1 (char *args, int allflag, 
6343               int (*filter) (const struct breakpoint *))
6344 {
6345   struct breakpoint *b;
6346   struct bp_location *last_loc = NULL;
6347   int nr_printable_breakpoints;
6348   struct cleanup *bkpttbl_chain;
6349   struct value_print_options opts;
6350   int print_address_bits = 0;
6351   int print_type_col_width = 14;
6352   struct ui_out *uiout = current_uiout;
6353
6354   get_user_print_options (&opts);
6355
6356   /* Compute the number of rows in the table, as well as the size
6357      required for address fields.  */
6358   nr_printable_breakpoints = 0;
6359   ALL_BREAKPOINTS (b)
6360     {
6361       /* If we have a filter, only list the breakpoints it accepts.  */
6362       if (filter && !filter (b))
6363         continue;
6364
6365       /* If we have an "args" string, it is a list of breakpoints to 
6366          accept.  Skip the others.  */
6367       if (args != NULL && *args != '\0')
6368         {
6369           if (allflag && parse_and_eval_long (args) != b->number)
6370             continue;
6371           if (!allflag && !number_is_in_list (args, b->number))
6372             continue;
6373         }
6374
6375       if (allflag || user_breakpoint_p (b))
6376         {
6377           int addr_bit, type_len;
6378
6379           addr_bit = breakpoint_address_bits (b);
6380           if (addr_bit > print_address_bits)
6381             print_address_bits = addr_bit;
6382
6383           type_len = strlen (bptype_string (b->type));
6384           if (type_len > print_type_col_width)
6385             print_type_col_width = type_len;
6386
6387           nr_printable_breakpoints++;
6388         }
6389     }
6390
6391   if (opts.addressprint)
6392     bkpttbl_chain 
6393       = make_cleanup_ui_out_table_begin_end (uiout, 6,
6394                                              nr_printable_breakpoints,
6395                                              "BreakpointTable");
6396   else
6397     bkpttbl_chain 
6398       = make_cleanup_ui_out_table_begin_end (uiout, 5,
6399                                              nr_printable_breakpoints,
6400                                              "BreakpointTable");
6401
6402   if (nr_printable_breakpoints > 0)
6403     annotate_breakpoints_headers ();
6404   if (nr_printable_breakpoints > 0)
6405     annotate_field (0);
6406   ui_out_table_header (uiout, 7, ui_left, "number", "Num");     /* 1 */
6407   if (nr_printable_breakpoints > 0)
6408     annotate_field (1);
6409   ui_out_table_header (uiout, print_type_col_width, ui_left,
6410                        "type", "Type");                         /* 2 */
6411   if (nr_printable_breakpoints > 0)
6412     annotate_field (2);
6413   ui_out_table_header (uiout, 4, ui_left, "disp", "Disp");      /* 3 */
6414   if (nr_printable_breakpoints > 0)
6415     annotate_field (3);
6416   ui_out_table_header (uiout, 3, ui_left, "enabled", "Enb");    /* 4 */
6417   if (opts.addressprint)
6418     {
6419       if (nr_printable_breakpoints > 0)
6420         annotate_field (4);
6421       if (print_address_bits <= 32)
6422         ui_out_table_header (uiout, 10, ui_left, 
6423                              "addr", "Address");                /* 5 */
6424       else
6425         ui_out_table_header (uiout, 18, ui_left, 
6426                              "addr", "Address");                /* 5 */
6427     }
6428   if (nr_printable_breakpoints > 0)
6429     annotate_field (5);
6430   ui_out_table_header (uiout, 40, ui_noalign, "what", "What");  /* 6 */
6431   ui_out_table_body (uiout);
6432   if (nr_printable_breakpoints > 0)
6433     annotate_breakpoints_table ();
6434
6435   ALL_BREAKPOINTS (b)
6436     {
6437       QUIT;
6438       /* If we have a filter, only list the breakpoints it accepts.  */
6439       if (filter && !filter (b))
6440         continue;
6441
6442       /* If we have an "args" string, it is a list of breakpoints to 
6443          accept.  Skip the others.  */
6444
6445       if (args != NULL && *args != '\0')
6446         {
6447           if (allflag)  /* maintenance info breakpoint */
6448             {
6449               if (parse_and_eval_long (args) != b->number)
6450                 continue;
6451             }
6452           else          /* all others */
6453             {
6454               if (!number_is_in_list (args, b->number))
6455                 continue;
6456             }
6457         }
6458       /* We only print out user settable breakpoints unless the
6459          allflag is set.  */
6460       if (allflag || user_breakpoint_p (b))
6461         print_one_breakpoint (b, &last_loc, allflag);
6462     }
6463
6464   do_cleanups (bkpttbl_chain);
6465
6466   if (nr_printable_breakpoints == 0)
6467     {
6468       /* If there's a filter, let the caller decide how to report
6469          empty list.  */
6470       if (!filter)
6471         {
6472           if (args == NULL || *args == '\0')
6473             ui_out_message (uiout, 0, "No breakpoints or watchpoints.\n");
6474           else
6475             ui_out_message (uiout, 0, 
6476                             "No breakpoint or watchpoint matching '%s'.\n",
6477                             args);
6478         }
6479     }
6480   else
6481     {
6482       if (last_loc && !server_command)
6483         set_next_address (last_loc->gdbarch, last_loc->address);
6484     }
6485
6486   /* FIXME?  Should this be moved up so that it is only called when
6487      there have been breakpoints? */
6488   annotate_breakpoints_table_end ();
6489
6490   return nr_printable_breakpoints;
6491 }
6492
6493 /* Display the value of default-collect in a way that is generally
6494    compatible with the breakpoint list.  */
6495
6496 static void
6497 default_collect_info (void)
6498 {
6499   struct ui_out *uiout = current_uiout;
6500
6501   /* If it has no value (which is frequently the case), say nothing; a
6502      message like "No default-collect." gets in user's face when it's
6503      not wanted.  */
6504   if (!*default_collect)
6505     return;
6506
6507   /* The following phrase lines up nicely with per-tracepoint collect
6508      actions.  */
6509   ui_out_text (uiout, "default collect ");
6510   ui_out_field_string (uiout, "default-collect", default_collect);
6511   ui_out_text (uiout, " \n");
6512 }
6513   
6514 static void
6515 breakpoints_info (char *args, int from_tty)
6516 {
6517   breakpoint_1 (args, 0, NULL);
6518
6519   default_collect_info ();
6520 }
6521
6522 static void
6523 watchpoints_info (char *args, int from_tty)
6524 {
6525   int num_printed = breakpoint_1 (args, 0, is_watchpoint);
6526   struct ui_out *uiout = current_uiout;
6527
6528   if (num_printed == 0)
6529     {
6530       if (args == NULL || *args == '\0')
6531         ui_out_message (uiout, 0, "No watchpoints.\n");
6532       else
6533         ui_out_message (uiout, 0, "No watchpoint matching '%s'.\n", args);
6534     }
6535 }
6536
6537 static void
6538 maintenance_info_breakpoints (char *args, int from_tty)
6539 {
6540   breakpoint_1 (args, 1, NULL);
6541
6542   default_collect_info ();
6543 }
6544
6545 static int
6546 breakpoint_has_pc (struct breakpoint *b,
6547                    struct program_space *pspace,
6548                    CORE_ADDR pc, struct obj_section *section)
6549 {
6550   struct bp_location *bl = b->loc;
6551
6552   for (; bl; bl = bl->next)
6553     {
6554       if (bl->pspace == pspace
6555           && bl->address == pc
6556           && (!overlay_debugging || bl->section == section))
6557         return 1;         
6558     }
6559   return 0;
6560 }
6561
6562 /* Print a message describing any user-breakpoints set at PC.  This
6563    concerns with logical breakpoints, so we match program spaces, not
6564    address spaces.  */
6565
6566 static void
6567 describe_other_breakpoints (struct gdbarch *gdbarch,
6568                             struct program_space *pspace, CORE_ADDR pc,
6569                             struct obj_section *section, int thread)
6570 {
6571   int others = 0;
6572   struct breakpoint *b;
6573
6574   ALL_BREAKPOINTS (b)
6575     others += (user_breakpoint_p (b)
6576                && breakpoint_has_pc (b, pspace, pc, section));
6577   if (others > 0)
6578     {
6579       if (others == 1)
6580         printf_filtered (_("Note: breakpoint "));
6581       else /* if (others == ???) */
6582         printf_filtered (_("Note: breakpoints "));
6583       ALL_BREAKPOINTS (b)
6584         if (user_breakpoint_p (b) && breakpoint_has_pc (b, pspace, pc, section))
6585           {
6586             others--;
6587             printf_filtered ("%d", b->number);
6588             if (b->thread == -1 && thread != -1)
6589               printf_filtered (" (all threads)");
6590             else if (b->thread != -1)
6591               printf_filtered (" (thread %d)", b->thread);
6592             printf_filtered ("%s%s ",
6593                              ((b->enable_state == bp_disabled
6594                                || b->enable_state == bp_call_disabled)
6595                               ? " (disabled)"
6596                               : b->enable_state == bp_permanent 
6597                               ? " (permanent)"
6598                               : ""),
6599                              (others > 1) ? "," 
6600                              : ((others == 1) ? " and" : ""));
6601           }
6602       printf_filtered (_("also set at pc "));
6603       fputs_filtered (paddress (gdbarch, pc), gdb_stdout);
6604       printf_filtered (".\n");
6605     }
6606 }
6607 \f
6608
6609 /* Return true iff it is meaningful to use the address member of
6610    BPT.  For some breakpoint types, the address member is irrelevant
6611    and it makes no sense to attempt to compare it to other addresses
6612    (or use it for any other purpose either).
6613
6614    More specifically, each of the following breakpoint types will
6615    always have a zero valued address and we don't want to mark
6616    breakpoints of any of these types to be a duplicate of an actual
6617    breakpoint at address zero:
6618
6619       bp_watchpoint
6620       bp_catchpoint
6621
6622 */
6623
6624 static int
6625 breakpoint_address_is_meaningful (struct breakpoint *bpt)
6626 {
6627   enum bptype type = bpt->type;
6628
6629   return (type != bp_watchpoint && type != bp_catchpoint);
6630 }
6631
6632 /* Assuming LOC1 and LOC2's owners are hardware watchpoints, returns
6633    true if LOC1 and LOC2 represent the same watchpoint location.  */
6634
6635 static int
6636 watchpoint_locations_match (struct bp_location *loc1, 
6637                             struct bp_location *loc2)
6638 {
6639   struct watchpoint *w1 = (struct watchpoint *) loc1->owner;
6640   struct watchpoint *w2 = (struct watchpoint *) loc2->owner;
6641
6642   /* Both of them must exist.  */
6643   gdb_assert (w1 != NULL);
6644   gdb_assert (w2 != NULL);
6645
6646   /* If the target can evaluate the condition expression in hardware,
6647      then we we need to insert both watchpoints even if they are at
6648      the same place.  Otherwise the watchpoint will only trigger when
6649      the condition of whichever watchpoint was inserted evaluates to
6650      true, not giving a chance for GDB to check the condition of the
6651      other watchpoint.  */
6652   if ((w1->cond_exp
6653        && target_can_accel_watchpoint_condition (loc1->address, 
6654                                                  loc1->length,
6655                                                  loc1->watchpoint_type,
6656                                                  w1->cond_exp))
6657       || (w2->cond_exp
6658           && target_can_accel_watchpoint_condition (loc2->address, 
6659                                                     loc2->length,
6660                                                     loc2->watchpoint_type,
6661                                                     w2->cond_exp)))
6662     return 0;
6663
6664   /* Note that this checks the owner's type, not the location's.  In
6665      case the target does not support read watchpoints, but does
6666      support access watchpoints, we'll have bp_read_watchpoint
6667      watchpoints with hw_access locations.  Those should be considered
6668      duplicates of hw_read locations.  The hw_read locations will
6669      become hw_access locations later.  */
6670   return (loc1->owner->type == loc2->owner->type
6671           && loc1->pspace->aspace == loc2->pspace->aspace
6672           && loc1->address == loc2->address
6673           && loc1->length == loc2->length);
6674 }
6675
6676 /* Returns true if {ASPACE1,ADDR1} and {ASPACE2,ADDR2} represent the
6677    same breakpoint location.  In most targets, this can only be true
6678    if ASPACE1 matches ASPACE2.  On targets that have global
6679    breakpoints, the address space doesn't really matter.  */
6680
6681 static int
6682 breakpoint_address_match (struct address_space *aspace1, CORE_ADDR addr1,
6683                           struct address_space *aspace2, CORE_ADDR addr2)
6684 {
6685   return ((gdbarch_has_global_breakpoints (target_gdbarch ())
6686            || aspace1 == aspace2)
6687           && addr1 == addr2);
6688 }
6689
6690 /* Returns true if {ASPACE2,ADDR2} falls within the range determined by
6691    {ASPACE1,ADDR1,LEN1}.  In most targets, this can only be true if ASPACE1
6692    matches ASPACE2.  On targets that have global breakpoints, the address
6693    space doesn't really matter.  */
6694
6695 static int
6696 breakpoint_address_match_range (struct address_space *aspace1, CORE_ADDR addr1,
6697                                 int len1, struct address_space *aspace2,
6698                                 CORE_ADDR addr2)
6699 {
6700   return ((gdbarch_has_global_breakpoints (target_gdbarch ())
6701            || aspace1 == aspace2)
6702           && addr2 >= addr1 && addr2 < addr1 + len1);
6703 }
6704
6705 /* Returns true if {ASPACE,ADDR} matches the breakpoint BL.  BL may be
6706    a ranged breakpoint.  In most targets, a match happens only if ASPACE
6707    matches the breakpoint's address space.  On targets that have global
6708    breakpoints, the address space doesn't really matter.  */
6709
6710 static int
6711 breakpoint_location_address_match (struct bp_location *bl,
6712                                    struct address_space *aspace,
6713                                    CORE_ADDR addr)
6714 {
6715   return (breakpoint_address_match (bl->pspace->aspace, bl->address,
6716                                     aspace, addr)
6717           || (bl->length
6718               && breakpoint_address_match_range (bl->pspace->aspace,
6719                                                  bl->address, bl->length,
6720                                                  aspace, addr)));
6721 }
6722
6723 /* If LOC1 and LOC2's owners are not tracepoints, returns false directly.
6724    Then, if LOC1 and LOC2 represent the same tracepoint location, returns
6725    true, otherwise returns false.  */
6726
6727 static int
6728 tracepoint_locations_match (struct bp_location *loc1,
6729                             struct bp_location *loc2)
6730 {
6731   if (is_tracepoint (loc1->owner) && is_tracepoint (loc2->owner))
6732     /* Since tracepoint locations are never duplicated with others', tracepoint
6733        locations at the same address of different tracepoints are regarded as
6734        different locations.  */
6735     return (loc1->address == loc2->address && loc1->owner == loc2->owner);
6736   else
6737     return 0;
6738 }
6739
6740 /* Assuming LOC1 and LOC2's types' have meaningful target addresses
6741    (breakpoint_address_is_meaningful), returns true if LOC1 and LOC2
6742    represent the same location.  */
6743
6744 static int
6745 breakpoint_locations_match (struct bp_location *loc1, 
6746                             struct bp_location *loc2)
6747 {
6748   int hw_point1, hw_point2;
6749
6750   /* Both of them must not be in moribund_locations.  */
6751   gdb_assert (loc1->owner != NULL);
6752   gdb_assert (loc2->owner != NULL);
6753
6754   hw_point1 = is_hardware_watchpoint (loc1->owner);
6755   hw_point2 = is_hardware_watchpoint (loc2->owner);
6756
6757   if (hw_point1 != hw_point2)
6758     return 0;
6759   else if (hw_point1)
6760     return watchpoint_locations_match (loc1, loc2);
6761   else if (is_tracepoint (loc1->owner) || is_tracepoint (loc2->owner))
6762     return tracepoint_locations_match (loc1, loc2);
6763   else
6764     /* We compare bp_location.length in order to cover ranged breakpoints.  */
6765     return (breakpoint_address_match (loc1->pspace->aspace, loc1->address,
6766                                      loc2->pspace->aspace, loc2->address)
6767             && loc1->length == loc2->length);
6768 }
6769
6770 static void
6771 breakpoint_adjustment_warning (CORE_ADDR from_addr, CORE_ADDR to_addr,
6772                                int bnum, int have_bnum)
6773 {
6774   /* The longest string possibly returned by hex_string_custom
6775      is 50 chars.  These must be at least that big for safety.  */
6776   char astr1[64];
6777   char astr2[64];
6778
6779   strcpy (astr1, hex_string_custom ((unsigned long) from_addr, 8));
6780   strcpy (astr2, hex_string_custom ((unsigned long) to_addr, 8));
6781   if (have_bnum)
6782     warning (_("Breakpoint %d address previously adjusted from %s to %s."),
6783              bnum, astr1, astr2);
6784   else
6785     warning (_("Breakpoint address adjusted from %s to %s."), astr1, astr2);
6786 }
6787
6788 /* Adjust a breakpoint's address to account for architectural
6789    constraints on breakpoint placement.  Return the adjusted address.
6790    Note: Very few targets require this kind of adjustment.  For most
6791    targets, this function is simply the identity function.  */
6792
6793 static CORE_ADDR
6794 adjust_breakpoint_address (struct gdbarch *gdbarch,
6795                            CORE_ADDR bpaddr, enum bptype bptype)
6796 {
6797   if (!gdbarch_adjust_breakpoint_address_p (gdbarch))
6798     {
6799       /* Very few targets need any kind of breakpoint adjustment.  */
6800       return bpaddr;
6801     }
6802   else if (bptype == bp_watchpoint
6803            || bptype == bp_hardware_watchpoint
6804            || bptype == bp_read_watchpoint
6805            || bptype == bp_access_watchpoint
6806            || bptype == bp_catchpoint)
6807     {
6808       /* Watchpoints and the various bp_catch_* eventpoints should not
6809          have their addresses modified.  */
6810       return bpaddr;
6811     }
6812   else
6813     {
6814       CORE_ADDR adjusted_bpaddr;
6815
6816       /* Some targets have architectural constraints on the placement
6817          of breakpoint instructions.  Obtain the adjusted address.  */
6818       adjusted_bpaddr = gdbarch_adjust_breakpoint_address (gdbarch, bpaddr);
6819
6820       /* An adjusted breakpoint address can significantly alter
6821          a user's expectations.  Print a warning if an adjustment
6822          is required.  */
6823       if (adjusted_bpaddr != bpaddr)
6824         breakpoint_adjustment_warning (bpaddr, adjusted_bpaddr, 0, 0);
6825
6826       return adjusted_bpaddr;
6827     }
6828 }
6829
6830 void
6831 init_bp_location (struct bp_location *loc, const struct bp_location_ops *ops,
6832                   struct breakpoint *owner)
6833 {
6834   memset (loc, 0, sizeof (*loc));
6835
6836   gdb_assert (ops != NULL);
6837
6838   loc->ops = ops;
6839   loc->owner = owner;
6840   loc->cond = NULL;
6841   loc->cond_bytecode = NULL;
6842   loc->shlib_disabled = 0;
6843   loc->enabled = 1;
6844
6845   switch (owner->type)
6846     {
6847     case bp_breakpoint:
6848     case bp_until:
6849     case bp_finish:
6850     case bp_longjmp:
6851     case bp_longjmp_resume:
6852     case bp_longjmp_call_dummy:
6853     case bp_exception:
6854     case bp_exception_resume:
6855     case bp_step_resume:
6856     case bp_hp_step_resume:
6857     case bp_watchpoint_scope:
6858     case bp_call_dummy:
6859     case bp_std_terminate:
6860     case bp_shlib_event:
6861     case bp_thread_event:
6862     case bp_overlay_event:
6863     case bp_jit_event:
6864     case bp_longjmp_master:
6865     case bp_std_terminate_master:
6866     case bp_exception_master:
6867     case bp_gnu_ifunc_resolver:
6868     case bp_gnu_ifunc_resolver_return:
6869     case bp_dprintf:
6870       loc->loc_type = bp_loc_software_breakpoint;
6871       mark_breakpoint_location_modified (loc);
6872       break;
6873     case bp_hardware_breakpoint:
6874       loc->loc_type = bp_loc_hardware_breakpoint;
6875       mark_breakpoint_location_modified (loc);
6876       break;
6877     case bp_hardware_watchpoint:
6878     case bp_read_watchpoint:
6879     case bp_access_watchpoint:
6880       loc->loc_type = bp_loc_hardware_watchpoint;
6881       break;
6882     case bp_watchpoint:
6883     case bp_catchpoint:
6884     case bp_tracepoint:
6885     case bp_fast_tracepoint:
6886     case bp_static_tracepoint:
6887       loc->loc_type = bp_loc_other;
6888       break;
6889     default:
6890       internal_error (__FILE__, __LINE__, _("unknown breakpoint type"));
6891     }
6892
6893   loc->refc = 1;
6894 }
6895
6896 /* Allocate a struct bp_location.  */
6897
6898 static struct bp_location *
6899 allocate_bp_location (struct breakpoint *bpt)
6900 {
6901   return bpt->ops->allocate_location (bpt);
6902 }
6903
6904 static void
6905 free_bp_location (struct bp_location *loc)
6906 {
6907   loc->ops->dtor (loc);
6908   xfree (loc);
6909 }
6910
6911 /* Increment reference count.  */
6912
6913 static void
6914 incref_bp_location (struct bp_location *bl)
6915 {
6916   ++bl->refc;
6917 }
6918
6919 /* Decrement reference count.  If the reference count reaches 0,
6920    destroy the bp_location.  Sets *BLP to NULL.  */
6921
6922 static void
6923 decref_bp_location (struct bp_location **blp)
6924 {
6925   gdb_assert ((*blp)->refc > 0);
6926
6927   if (--(*blp)->refc == 0)
6928     free_bp_location (*blp);
6929   *blp = NULL;
6930 }
6931
6932 /* Add breakpoint B at the end of the global breakpoint chain.  */
6933
6934 static void
6935 add_to_breakpoint_chain (struct breakpoint *b)
6936 {
6937   struct breakpoint *b1;
6938
6939   /* Add this breakpoint to the end of the chain so that a list of
6940      breakpoints will come out in order of increasing numbers.  */
6941
6942   b1 = breakpoint_chain;
6943   if (b1 == 0)
6944     breakpoint_chain = b;
6945   else
6946     {
6947       while (b1->next)
6948         b1 = b1->next;
6949       b1->next = b;
6950     }
6951 }
6952
6953 /* Initializes breakpoint B with type BPTYPE and no locations yet.  */
6954
6955 static void
6956 init_raw_breakpoint_without_location (struct breakpoint *b,
6957                                       struct gdbarch *gdbarch,
6958                                       enum bptype bptype,
6959                                       const struct breakpoint_ops *ops)
6960 {
6961   memset (b, 0, sizeof (*b));
6962
6963   gdb_assert (ops != NULL);
6964
6965   b->ops = ops;
6966   b->type = bptype;
6967   b->gdbarch = gdbarch;
6968   b->language = current_language->la_language;
6969   b->input_radix = input_radix;
6970   b->thread = -1;
6971   b->enable_state = bp_enabled;
6972   b->next = 0;
6973   b->silent = 0;
6974   b->ignore_count = 0;
6975   b->commands = NULL;
6976   b->frame_id = null_frame_id;
6977   b->condition_not_parsed = 0;
6978   b->py_bp_object = NULL;
6979   b->related_breakpoint = b;
6980 }
6981
6982 /* Helper to set_raw_breakpoint below.  Creates a breakpoint
6983    that has type BPTYPE and has no locations as yet.  */
6984
6985 static struct breakpoint *
6986 set_raw_breakpoint_without_location (struct gdbarch *gdbarch,
6987                                      enum bptype bptype,
6988                                      const struct breakpoint_ops *ops)
6989 {
6990   struct breakpoint *b = XNEW (struct breakpoint);
6991
6992   init_raw_breakpoint_without_location (b, gdbarch, bptype, ops);
6993   add_to_breakpoint_chain (b);
6994   return b;
6995 }
6996
6997 /* Initialize loc->function_name.  EXPLICIT_LOC says no indirect function
6998    resolutions should be made as the user specified the location explicitly
6999    enough.  */
7000
7001 static void
7002 set_breakpoint_location_function (struct bp_location *loc, int explicit_loc)
7003 {
7004   gdb_assert (loc->owner != NULL);
7005
7006   if (loc->owner->type == bp_breakpoint
7007       || loc->owner->type == bp_hardware_breakpoint
7008       || is_tracepoint (loc->owner))
7009     {
7010       int is_gnu_ifunc;
7011       const char *function_name;
7012       CORE_ADDR func_addr;
7013
7014       find_pc_partial_function_gnu_ifunc (loc->address, &function_name,
7015                                           &func_addr, NULL, &is_gnu_ifunc);
7016
7017       if (is_gnu_ifunc && !explicit_loc)
7018         {
7019           struct breakpoint *b = loc->owner;
7020
7021           gdb_assert (loc->pspace == current_program_space);
7022           if (gnu_ifunc_resolve_name (function_name,
7023                                       &loc->requested_address))
7024             {
7025               /* Recalculate ADDRESS based on new REQUESTED_ADDRESS.  */
7026               loc->address = adjust_breakpoint_address (loc->gdbarch,
7027                                                         loc->requested_address,
7028                                                         b->type);
7029             }
7030           else if (b->type == bp_breakpoint && b->loc == loc
7031                    && loc->next == NULL && b->related_breakpoint == b)
7032             {
7033               /* Create only the whole new breakpoint of this type but do not
7034                  mess more complicated breakpoints with multiple locations.  */
7035               b->type = bp_gnu_ifunc_resolver;
7036               /* Remember the resolver's address for use by the return
7037                  breakpoint.  */
7038               loc->related_address = func_addr;
7039             }
7040         }
7041
7042       if (function_name)
7043         loc->function_name = xstrdup (function_name);
7044     }
7045 }
7046
7047 /* Attempt to determine architecture of location identified by SAL.  */
7048 struct gdbarch *
7049 get_sal_arch (struct symtab_and_line sal)
7050 {
7051   if (sal.section)
7052     return get_objfile_arch (sal.section->objfile);
7053   if (sal.symtab)
7054     return get_objfile_arch (sal.symtab->objfile);
7055
7056   return NULL;
7057 }
7058
7059 /* Low level routine for partially initializing a breakpoint of type
7060    BPTYPE.  The newly created breakpoint's address, section, source
7061    file name, and line number are provided by SAL.
7062
7063    It is expected that the caller will complete the initialization of
7064    the newly created breakpoint struct as well as output any status
7065    information regarding the creation of a new breakpoint.  */
7066
7067 static void
7068 init_raw_breakpoint (struct breakpoint *b, struct gdbarch *gdbarch,
7069                      struct symtab_and_line sal, enum bptype bptype,
7070                      const struct breakpoint_ops *ops)
7071 {
7072   init_raw_breakpoint_without_location (b, gdbarch, bptype, ops);
7073
7074   add_location_to_breakpoint (b, &sal);
7075
7076   if (bptype != bp_catchpoint)
7077     gdb_assert (sal.pspace != NULL);
7078
7079   /* Store the program space that was used to set the breakpoint,
7080      except for ordinary breakpoints, which are independent of the
7081      program space.  */
7082   if (bptype != bp_breakpoint && bptype != bp_hardware_breakpoint)
7083     b->pspace = sal.pspace;
7084 }
7085
7086 /* set_raw_breakpoint is a low level routine for allocating and
7087    partially initializing a breakpoint of type BPTYPE.  The newly
7088    created breakpoint's address, section, source file name, and line
7089    number are provided by SAL.  The newly created and partially
7090    initialized breakpoint is added to the breakpoint chain and
7091    is also returned as the value of this function.
7092
7093    It is expected that the caller will complete the initialization of
7094    the newly created breakpoint struct as well as output any status
7095    information regarding the creation of a new breakpoint.  In
7096    particular, set_raw_breakpoint does NOT set the breakpoint
7097    number!  Care should be taken to not allow an error to occur
7098    prior to completing the initialization of the breakpoint.  If this
7099    should happen, a bogus breakpoint will be left on the chain.  */
7100
7101 struct breakpoint *
7102 set_raw_breakpoint (struct gdbarch *gdbarch,
7103                     struct symtab_and_line sal, enum bptype bptype,
7104                     const struct breakpoint_ops *ops)
7105 {
7106   struct breakpoint *b = XNEW (struct breakpoint);
7107
7108   init_raw_breakpoint (b, gdbarch, sal, bptype, ops);
7109   add_to_breakpoint_chain (b);
7110   return b;
7111 }
7112
7113
7114 /* Note that the breakpoint object B describes a permanent breakpoint
7115    instruction, hard-wired into the inferior's code.  */
7116 void
7117 make_breakpoint_permanent (struct breakpoint *b)
7118 {
7119   struct bp_location *bl;
7120
7121   b->enable_state = bp_permanent;
7122
7123   /* By definition, permanent breakpoints are already present in the
7124      code.  Mark all locations as inserted.  For now,
7125      make_breakpoint_permanent is called in just one place, so it's
7126      hard to say if it's reasonable to have permanent breakpoint with
7127      multiple locations or not, but it's easy to implement.  */
7128   for (bl = b->loc; bl; bl = bl->next)
7129     bl->inserted = 1;
7130 }
7131
7132 /* Call this routine when stepping and nexting to enable a breakpoint
7133    if we do a longjmp() or 'throw' in TP.  FRAME is the frame which
7134    initiated the operation.  */
7135
7136 void
7137 set_longjmp_breakpoint (struct thread_info *tp, struct frame_id frame)
7138 {
7139   struct breakpoint *b, *b_tmp;
7140   int thread = tp->num;
7141
7142   /* To avoid having to rescan all objfile symbols at every step,
7143      we maintain a list of continually-inserted but always disabled
7144      longjmp "master" breakpoints.  Here, we simply create momentary
7145      clones of those and enable them for the requested thread.  */
7146   ALL_BREAKPOINTS_SAFE (b, b_tmp)
7147     if (b->pspace == current_program_space
7148         && (b->type == bp_longjmp_master
7149             || b->type == bp_exception_master))
7150       {
7151         enum bptype type = b->type == bp_longjmp_master ? bp_longjmp : bp_exception;
7152         struct breakpoint *clone;
7153
7154         /* longjmp_breakpoint_ops ensures INITIATING_FRAME is cleared again
7155            after their removal.  */
7156         clone = momentary_breakpoint_from_master (b, type,
7157                                                   &longjmp_breakpoint_ops);
7158         clone->thread = thread;
7159       }
7160
7161   tp->initiating_frame = frame;
7162 }
7163
7164 /* Delete all longjmp breakpoints from THREAD.  */
7165 void
7166 delete_longjmp_breakpoint (int thread)
7167 {
7168   struct breakpoint *b, *b_tmp;
7169
7170   ALL_BREAKPOINTS_SAFE (b, b_tmp)
7171     if (b->type == bp_longjmp || b->type == bp_exception)
7172       {
7173         if (b->thread == thread)
7174           delete_breakpoint (b);
7175       }
7176 }
7177
7178 void
7179 delete_longjmp_breakpoint_at_next_stop (int thread)
7180 {
7181   struct breakpoint *b, *b_tmp;
7182
7183   ALL_BREAKPOINTS_SAFE (b, b_tmp)
7184     if (b->type == bp_longjmp || b->type == bp_exception)
7185       {
7186         if (b->thread == thread)
7187           b->disposition = disp_del_at_next_stop;
7188       }
7189 }
7190
7191 /* Place breakpoints of type bp_longjmp_call_dummy to catch longjmp for
7192    INFERIOR_PTID thread.  Chain them all by RELATED_BREAKPOINT and return
7193    pointer to any of them.  Return NULL if this system cannot place longjmp
7194    breakpoints.  */
7195
7196 struct breakpoint *
7197 set_longjmp_breakpoint_for_call_dummy (void)
7198 {
7199   struct breakpoint *b, *retval = NULL;
7200
7201   ALL_BREAKPOINTS (b)
7202     if (b->pspace == current_program_space && b->type == bp_longjmp_master)
7203       {
7204         struct breakpoint *new_b;
7205
7206         new_b = momentary_breakpoint_from_master (b, bp_longjmp_call_dummy,
7207                                                   &momentary_breakpoint_ops);
7208         new_b->thread = pid_to_thread_id (inferior_ptid);
7209
7210         /* Link NEW_B into the chain of RETVAL breakpoints.  */
7211
7212         gdb_assert (new_b->related_breakpoint == new_b);
7213         if (retval == NULL)
7214           retval = new_b;
7215         new_b->related_breakpoint = retval;
7216         while (retval->related_breakpoint != new_b->related_breakpoint)
7217           retval = retval->related_breakpoint;
7218         retval->related_breakpoint = new_b;
7219       }
7220
7221   return retval;
7222 }
7223
7224 /* Verify all existing dummy frames and their associated breakpoints for
7225    THREAD.  Remove those which can no longer be found in the current frame
7226    stack.
7227
7228    You should call this function only at places where it is safe to currently
7229    unwind the whole stack.  Failed stack unwind would discard live dummy
7230    frames.  */
7231
7232 void
7233 check_longjmp_breakpoint_for_call_dummy (int thread)
7234 {
7235   struct breakpoint *b, *b_tmp;
7236
7237   ALL_BREAKPOINTS_SAFE (b, b_tmp)
7238     if (b->type == bp_longjmp_call_dummy && b->thread == thread)
7239       {
7240         struct breakpoint *dummy_b = b->related_breakpoint;
7241
7242         while (dummy_b != b && dummy_b->type != bp_call_dummy)
7243           dummy_b = dummy_b->related_breakpoint;
7244         if (dummy_b->type != bp_call_dummy
7245             || frame_find_by_id (dummy_b->frame_id) != NULL)
7246           continue;
7247         
7248         dummy_frame_discard (dummy_b->frame_id);
7249
7250         while (b->related_breakpoint != b)
7251           {
7252             if (b_tmp == b->related_breakpoint)
7253               b_tmp = b->related_breakpoint->next;
7254             delete_breakpoint (b->related_breakpoint);
7255           }
7256         delete_breakpoint (b);
7257       }
7258 }
7259
7260 void
7261 enable_overlay_breakpoints (void)
7262 {
7263   struct breakpoint *b;
7264
7265   ALL_BREAKPOINTS (b)
7266     if (b->type == bp_overlay_event)
7267     {
7268       b->enable_state = bp_enabled;
7269       update_global_location_list (1);
7270       overlay_events_enabled = 1;
7271     }
7272 }
7273
7274 void
7275 disable_overlay_breakpoints (void)
7276 {
7277   struct breakpoint *b;
7278
7279   ALL_BREAKPOINTS (b)
7280     if (b->type == bp_overlay_event)
7281     {
7282       b->enable_state = bp_disabled;
7283       update_global_location_list (0);
7284       overlay_events_enabled = 0;
7285     }
7286 }
7287
7288 /* Set an active std::terminate breakpoint for each std::terminate
7289    master breakpoint.  */
7290 void
7291 set_std_terminate_breakpoint (void)
7292 {
7293   struct breakpoint *b, *b_tmp;
7294
7295   ALL_BREAKPOINTS_SAFE (b, b_tmp)
7296     if (b->pspace == current_program_space
7297         && b->type == bp_std_terminate_master)
7298       {
7299         momentary_breakpoint_from_master (b, bp_std_terminate,
7300                                           &momentary_breakpoint_ops);
7301       }
7302 }
7303
7304 /* Delete all the std::terminate breakpoints.  */
7305 void
7306 delete_std_terminate_breakpoint (void)
7307 {
7308   struct breakpoint *b, *b_tmp;
7309
7310   ALL_BREAKPOINTS_SAFE (b, b_tmp)
7311     if (b->type == bp_std_terminate)
7312       delete_breakpoint (b);
7313 }
7314
7315 struct breakpoint *
7316 create_thread_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7317 {
7318   struct breakpoint *b;
7319
7320   b = create_internal_breakpoint (gdbarch, address, bp_thread_event,
7321                                   &internal_breakpoint_ops);
7322
7323   b->enable_state = bp_enabled;
7324   /* addr_string has to be used or breakpoint_re_set will delete me.  */
7325   b->addr_string
7326     = xstrprintf ("*%s", paddress (b->loc->gdbarch, b->loc->address));
7327
7328   update_global_location_list_nothrow (1);
7329
7330   return b;
7331 }
7332
7333 void
7334 remove_thread_event_breakpoints (void)
7335 {
7336   struct breakpoint *b, *b_tmp;
7337
7338   ALL_BREAKPOINTS_SAFE (b, b_tmp)
7339     if (b->type == bp_thread_event
7340         && b->loc->pspace == current_program_space)
7341       delete_breakpoint (b);
7342 }
7343
7344 struct lang_and_radix
7345   {
7346     enum language lang;
7347     int radix;
7348   };
7349
7350 /* Create a breakpoint for JIT code registration and unregistration.  */
7351
7352 struct breakpoint *
7353 create_jit_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7354 {
7355   struct breakpoint *b;
7356
7357   b = create_internal_breakpoint (gdbarch, address, bp_jit_event,
7358                                   &internal_breakpoint_ops);
7359   update_global_location_list_nothrow (1);
7360   return b;
7361 }
7362
7363 /* Remove JIT code registration and unregistration breakpoint(s).  */
7364
7365 void
7366 remove_jit_event_breakpoints (void)
7367 {
7368   struct breakpoint *b, *b_tmp;
7369
7370   ALL_BREAKPOINTS_SAFE (b, b_tmp)
7371     if (b->type == bp_jit_event
7372         && b->loc->pspace == current_program_space)
7373       delete_breakpoint (b);
7374 }
7375
7376 void
7377 remove_solib_event_breakpoints (void)
7378 {
7379   struct breakpoint *b, *b_tmp;
7380
7381   ALL_BREAKPOINTS_SAFE (b, b_tmp)
7382     if (b->type == bp_shlib_event
7383         && b->loc->pspace == current_program_space)
7384       delete_breakpoint (b);
7385 }
7386
7387 struct breakpoint *
7388 create_solib_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7389 {
7390   struct breakpoint *b;
7391
7392   b = create_internal_breakpoint (gdbarch, address, bp_shlib_event,
7393                                   &internal_breakpoint_ops);
7394   update_global_location_list_nothrow (1);
7395   return b;
7396 }
7397
7398 /* Disable any breakpoints that are on code in shared libraries.  Only
7399    apply to enabled breakpoints, disabled ones can just stay disabled.  */
7400
7401 void
7402 disable_breakpoints_in_shlibs (void)
7403 {
7404   struct bp_location *loc, **locp_tmp;
7405
7406   ALL_BP_LOCATIONS (loc, locp_tmp)
7407   {
7408     /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL.  */
7409     struct breakpoint *b = loc->owner;
7410
7411     /* We apply the check to all breakpoints, including disabled for
7412        those with loc->duplicate set.  This is so that when breakpoint
7413        becomes enabled, or the duplicate is removed, gdb will try to
7414        insert all breakpoints.  If we don't set shlib_disabled here,
7415        we'll try to insert those breakpoints and fail.  */
7416     if (((b->type == bp_breakpoint)
7417          || (b->type == bp_jit_event)
7418          || (b->type == bp_hardware_breakpoint)
7419          || (is_tracepoint (b)))
7420         && loc->pspace == current_program_space
7421         && !loc->shlib_disabled
7422 #ifdef PC_SOLIB
7423         && PC_SOLIB (loc->address)
7424 #else
7425         && solib_name_from_address (loc->pspace, loc->address)
7426 #endif
7427         )
7428       {
7429         loc->shlib_disabled = 1;
7430       }
7431   }
7432 }
7433
7434 /* Disable any breakpoints and tracepoints that are in an unloaded shared
7435    library.  Only apply to enabled breakpoints, disabled ones can just stay
7436    disabled.  */
7437
7438 static void
7439 disable_breakpoints_in_unloaded_shlib (struct so_list *solib)
7440 {
7441   struct bp_location *loc, **locp_tmp;
7442   int disabled_shlib_breaks = 0;
7443
7444   /* SunOS a.out shared libraries are always mapped, so do not
7445      disable breakpoints; they will only be reported as unloaded
7446      through clear_solib when GDB discards its shared library
7447      list.  See clear_solib for more information.  */
7448   if (exec_bfd != NULL
7449       && bfd_get_flavour (exec_bfd) == bfd_target_aout_flavour)
7450     return;
7451
7452   ALL_BP_LOCATIONS (loc, locp_tmp)
7453   {
7454     /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL.  */
7455     struct breakpoint *b = loc->owner;
7456
7457     if (solib->pspace == loc->pspace
7458         && !loc->shlib_disabled
7459         && (((b->type == bp_breakpoint
7460               || b->type == bp_jit_event
7461               || b->type == bp_hardware_breakpoint)
7462              && (loc->loc_type == bp_loc_hardware_breakpoint
7463                  || loc->loc_type == bp_loc_software_breakpoint))
7464             || is_tracepoint (b))
7465         && solib_contains_address_p (solib, loc->address))
7466       {
7467         loc->shlib_disabled = 1;
7468         /* At this point, we cannot rely on remove_breakpoint
7469            succeeding so we must mark the breakpoint as not inserted
7470            to prevent future errors occurring in remove_breakpoints.  */
7471         loc->inserted = 0;
7472
7473         /* This may cause duplicate notifications for the same breakpoint.  */
7474         observer_notify_breakpoint_modified (b);
7475
7476         if (!disabled_shlib_breaks)
7477           {
7478             target_terminal_ours_for_output ();
7479             warning (_("Temporarily disabling breakpoints "
7480                        "for unloaded shared library \"%s\""),
7481                      solib->so_name);
7482           }
7483         disabled_shlib_breaks = 1;
7484       }
7485   }
7486 }
7487
7488 /* FORK & VFORK catchpoints.  */
7489
7490 /* An instance of this type is used to represent a fork or vfork
7491    catchpoint.  It includes a "struct breakpoint" as a kind of base
7492    class; users downcast to "struct breakpoint *" when needed.  A
7493    breakpoint is really of this type iff its ops pointer points to
7494    CATCH_FORK_BREAKPOINT_OPS.  */
7495
7496 struct fork_catchpoint
7497 {
7498   /* The base class.  */
7499   struct breakpoint base;
7500
7501   /* Process id of a child process whose forking triggered this
7502      catchpoint.  This field is only valid immediately after this
7503      catchpoint has triggered.  */
7504   ptid_t forked_inferior_pid;
7505 };
7506
7507 /* Implement the "insert" breakpoint_ops method for fork
7508    catchpoints.  */
7509
7510 static int
7511 insert_catch_fork (struct bp_location *bl)
7512 {
7513   return target_insert_fork_catchpoint (PIDGET (inferior_ptid));
7514 }
7515
7516 /* Implement the "remove" breakpoint_ops method for fork
7517    catchpoints.  */
7518
7519 static int
7520 remove_catch_fork (struct bp_location *bl)
7521 {
7522   return target_remove_fork_catchpoint (PIDGET (inferior_ptid));
7523 }
7524
7525 /* Implement the "breakpoint_hit" breakpoint_ops method for fork
7526    catchpoints.  */
7527
7528 static int
7529 breakpoint_hit_catch_fork (const struct bp_location *bl,
7530                            struct address_space *aspace, CORE_ADDR bp_addr,
7531                            const struct target_waitstatus *ws)
7532 {
7533   struct fork_catchpoint *c = (struct fork_catchpoint *) bl->owner;
7534
7535   if (ws->kind != TARGET_WAITKIND_FORKED)
7536     return 0;
7537
7538   c->forked_inferior_pid = ws->value.related_pid;
7539   return 1;
7540 }
7541
7542 /* Implement the "print_it" breakpoint_ops method for fork
7543    catchpoints.  */
7544
7545 static enum print_stop_action
7546 print_it_catch_fork (bpstat bs)
7547 {
7548   struct ui_out *uiout = current_uiout;
7549   struct breakpoint *b = bs->breakpoint_at;
7550   struct fork_catchpoint *c = (struct fork_catchpoint *) bs->breakpoint_at;
7551
7552   annotate_catchpoint (b->number);
7553   if (b->disposition == disp_del)
7554     ui_out_text (uiout, "\nTemporary catchpoint ");
7555   else
7556     ui_out_text (uiout, "\nCatchpoint ");
7557   if (ui_out_is_mi_like_p (uiout))
7558     {
7559       ui_out_field_string (uiout, "reason",
7560                            async_reason_lookup (EXEC_ASYNC_FORK));
7561       ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
7562     }
7563   ui_out_field_int (uiout, "bkptno", b->number);
7564   ui_out_text (uiout, " (forked process ");
7565   ui_out_field_int (uiout, "newpid", ptid_get_pid (c->forked_inferior_pid));
7566   ui_out_text (uiout, "), ");
7567   return PRINT_SRC_AND_LOC;
7568 }
7569
7570 /* Implement the "print_one" breakpoint_ops method for fork
7571    catchpoints.  */
7572
7573 static void
7574 print_one_catch_fork (struct breakpoint *b, struct bp_location **last_loc)
7575 {
7576   struct fork_catchpoint *c = (struct fork_catchpoint *) b;
7577   struct value_print_options opts;
7578   struct ui_out *uiout = current_uiout;
7579
7580   get_user_print_options (&opts);
7581
7582   /* Field 4, the address, is omitted (which makes the columns not
7583      line up too nicely with the headers, but the effect is relatively
7584      readable).  */
7585   if (opts.addressprint)
7586     ui_out_field_skip (uiout, "addr");
7587   annotate_field (5);
7588   ui_out_text (uiout, "fork");
7589   if (!ptid_equal (c->forked_inferior_pid, null_ptid))
7590     {
7591       ui_out_text (uiout, ", process ");
7592       ui_out_field_int (uiout, "what",
7593                         ptid_get_pid (c->forked_inferior_pid));
7594       ui_out_spaces (uiout, 1);
7595     }
7596
7597   if (ui_out_is_mi_like_p (uiout))
7598     ui_out_field_string (uiout, "catch-type", "fork");
7599 }
7600
7601 /* Implement the "print_mention" breakpoint_ops method for fork
7602    catchpoints.  */
7603
7604 static void
7605 print_mention_catch_fork (struct breakpoint *b)
7606 {
7607   printf_filtered (_("Catchpoint %d (fork)"), b->number);
7608 }
7609
7610 /* Implement the "print_recreate" breakpoint_ops method for fork
7611    catchpoints.  */
7612
7613 static void
7614 print_recreate_catch_fork (struct breakpoint *b, struct ui_file *fp)
7615 {
7616   fprintf_unfiltered (fp, "catch fork");
7617   print_recreate_thread (b, fp);
7618 }
7619
7620 /* The breakpoint_ops structure to be used in fork catchpoints.  */
7621
7622 static struct breakpoint_ops catch_fork_breakpoint_ops;
7623
7624 /* Implement the "insert" breakpoint_ops method for vfork
7625    catchpoints.  */
7626
7627 static int
7628 insert_catch_vfork (struct bp_location *bl)
7629 {
7630   return target_insert_vfork_catchpoint (PIDGET (inferior_ptid));
7631 }
7632
7633 /* Implement the "remove" breakpoint_ops method for vfork
7634    catchpoints.  */
7635
7636 static int
7637 remove_catch_vfork (struct bp_location *bl)
7638 {
7639   return target_remove_vfork_catchpoint (PIDGET (inferior_ptid));
7640 }
7641
7642 /* Implement the "breakpoint_hit" breakpoint_ops method for vfork
7643    catchpoints.  */
7644
7645 static int
7646 breakpoint_hit_catch_vfork (const struct bp_location *bl,
7647                             struct address_space *aspace, CORE_ADDR bp_addr,
7648                             const struct target_waitstatus *ws)
7649 {
7650   struct fork_catchpoint *c = (struct fork_catchpoint *) bl->owner;
7651
7652   if (ws->kind != TARGET_WAITKIND_VFORKED)
7653     return 0;
7654
7655   c->forked_inferior_pid = ws->value.related_pid;
7656   return 1;
7657 }
7658
7659 /* Implement the "print_it" breakpoint_ops method for vfork
7660    catchpoints.  */
7661
7662 static enum print_stop_action
7663 print_it_catch_vfork (bpstat bs)
7664 {
7665   struct ui_out *uiout = current_uiout;
7666   struct breakpoint *b = bs->breakpoint_at;
7667   struct fork_catchpoint *c = (struct fork_catchpoint *) b;
7668
7669   annotate_catchpoint (b->number);
7670   if (b->disposition == disp_del)
7671     ui_out_text (uiout, "\nTemporary catchpoint ");
7672   else
7673     ui_out_text (uiout, "\nCatchpoint ");
7674   if (ui_out_is_mi_like_p (uiout))
7675     {
7676       ui_out_field_string (uiout, "reason",
7677                            async_reason_lookup (EXEC_ASYNC_VFORK));
7678       ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
7679     }
7680   ui_out_field_int (uiout, "bkptno", b->number);
7681   ui_out_text (uiout, " (vforked process ");
7682   ui_out_field_int (uiout, "newpid", ptid_get_pid (c->forked_inferior_pid));
7683   ui_out_text (uiout, "), ");
7684   return PRINT_SRC_AND_LOC;
7685 }
7686
7687 /* Implement the "print_one" breakpoint_ops method for vfork
7688    catchpoints.  */
7689
7690 static void
7691 print_one_catch_vfork (struct breakpoint *b, struct bp_location **last_loc)
7692 {
7693   struct fork_catchpoint *c = (struct fork_catchpoint *) b;
7694   struct value_print_options opts;
7695   struct ui_out *uiout = current_uiout;
7696
7697   get_user_print_options (&opts);
7698   /* Field 4, the address, is omitted (which makes the columns not
7699      line up too nicely with the headers, but the effect is relatively
7700      readable).  */
7701   if (opts.addressprint)
7702     ui_out_field_skip (uiout, "addr");
7703   annotate_field (5);
7704   ui_out_text (uiout, "vfork");
7705   if (!ptid_equal (c->forked_inferior_pid, null_ptid))
7706     {
7707       ui_out_text (uiout, ", process ");
7708       ui_out_field_int (uiout, "what",
7709                         ptid_get_pid (c->forked_inferior_pid));
7710       ui_out_spaces (uiout, 1);
7711     }
7712
7713   if (ui_out_is_mi_like_p (uiout))
7714     ui_out_field_string (uiout, "catch-type", "vfork");
7715 }
7716
7717 /* Implement the "print_mention" breakpoint_ops method for vfork
7718    catchpoints.  */
7719
7720 static void
7721 print_mention_catch_vfork (struct breakpoint *b)
7722 {
7723   printf_filtered (_("Catchpoint %d (vfork)"), b->number);
7724 }
7725
7726 /* Implement the "print_recreate" breakpoint_ops method for vfork
7727    catchpoints.  */
7728
7729 static void
7730 print_recreate_catch_vfork (struct breakpoint *b, struct ui_file *fp)
7731 {
7732   fprintf_unfiltered (fp, "catch vfork");
7733   print_recreate_thread (b, fp);
7734 }
7735
7736 /* The breakpoint_ops structure to be used in vfork catchpoints.  */
7737
7738 static struct breakpoint_ops catch_vfork_breakpoint_ops;
7739
7740 /* An instance of this type is used to represent an solib catchpoint.
7741    It includes a "struct breakpoint" as a kind of base class; users
7742    downcast to "struct breakpoint *" when needed.  A breakpoint is
7743    really of this type iff its ops pointer points to
7744    CATCH_SOLIB_BREAKPOINT_OPS.  */
7745
7746 struct solib_catchpoint
7747 {
7748   /* The base class.  */
7749   struct breakpoint base;
7750
7751   /* True for "catch load", false for "catch unload".  */
7752   unsigned char is_load;
7753
7754   /* Regular expression to match, if any.  COMPILED is only valid when
7755      REGEX is non-NULL.  */
7756   char *regex;
7757   regex_t compiled;
7758 };
7759
7760 static void
7761 dtor_catch_solib (struct breakpoint *b)
7762 {
7763   struct solib_catchpoint *self = (struct solib_catchpoint *) b;
7764
7765   if (self->regex)
7766     regfree (&self->compiled);
7767   xfree (self->regex);
7768
7769   base_breakpoint_ops.dtor (b);
7770 }
7771
7772 static int
7773 insert_catch_solib (struct bp_location *ignore)
7774 {
7775   return 0;
7776 }
7777
7778 static int
7779 remove_catch_solib (struct bp_location *ignore)
7780 {
7781   return 0;
7782 }
7783
7784 static int
7785 breakpoint_hit_catch_solib (const struct bp_location *bl,
7786                             struct address_space *aspace,
7787                             CORE_ADDR bp_addr,
7788                             const struct target_waitstatus *ws)
7789 {
7790   struct solib_catchpoint *self = (struct solib_catchpoint *) bl->owner;
7791   struct breakpoint *other;
7792
7793   if (ws->kind == TARGET_WAITKIND_LOADED)
7794     return 1;
7795
7796   ALL_BREAKPOINTS (other)
7797   {
7798     struct bp_location *other_bl;
7799
7800     if (other == bl->owner)
7801       continue;
7802
7803     if (other->type != bp_shlib_event)
7804       continue;
7805
7806     if (self->base.pspace != NULL && other->pspace != self->base.pspace)
7807       continue;
7808
7809     for (other_bl = other->loc; other_bl != NULL; other_bl = other_bl->next)
7810       {
7811         if (other->ops->breakpoint_hit (other_bl, aspace, bp_addr, ws))
7812           return 1;
7813       }
7814   }
7815
7816   return 0;
7817 }
7818
7819 static void
7820 check_status_catch_solib (struct bpstats *bs)
7821 {
7822   struct solib_catchpoint *self
7823     = (struct solib_catchpoint *) bs->breakpoint_at;
7824   int ix;
7825
7826   if (self->is_load)
7827     {
7828       struct so_list *iter;
7829
7830       for (ix = 0;
7831            VEC_iterate (so_list_ptr, current_program_space->added_solibs,
7832                         ix, iter);
7833            ++ix)
7834         {
7835           if (!self->regex
7836               || regexec (&self->compiled, iter->so_name, 0, NULL, 0) == 0)
7837             return;
7838         }
7839     }
7840   else
7841     {
7842       char *iter;
7843
7844       for (ix = 0;
7845            VEC_iterate (char_ptr, current_program_space->deleted_solibs,
7846                         ix, iter);
7847            ++ix)
7848         {
7849           if (!self->regex
7850               || regexec (&self->compiled, iter, 0, NULL, 0) == 0)
7851             return;
7852         }
7853     }
7854
7855   bs->stop = 0;
7856   bs->print_it = print_it_noop;
7857 }
7858
7859 static enum print_stop_action
7860 print_it_catch_solib (bpstat bs)
7861 {
7862   struct breakpoint *b = bs->breakpoint_at;
7863   struct ui_out *uiout = current_uiout;
7864
7865   annotate_catchpoint (b->number);
7866   if (b->disposition == disp_del)
7867     ui_out_text (uiout, "\nTemporary catchpoint ");
7868   else
7869     ui_out_text (uiout, "\nCatchpoint ");
7870   ui_out_field_int (uiout, "bkptno", b->number);
7871   ui_out_text (uiout, "\n");
7872   if (ui_out_is_mi_like_p (uiout))
7873     ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
7874   print_solib_event (1);
7875   return PRINT_SRC_AND_LOC;
7876 }
7877
7878 static void
7879 print_one_catch_solib (struct breakpoint *b, struct bp_location **locs)
7880 {
7881   struct solib_catchpoint *self = (struct solib_catchpoint *) b;
7882   struct value_print_options opts;
7883   struct ui_out *uiout = current_uiout;
7884   char *msg;
7885
7886   get_user_print_options (&opts);
7887   /* Field 4, the address, is omitted (which makes the columns not
7888      line up too nicely with the headers, but the effect is relatively
7889      readable).  */
7890   if (opts.addressprint)
7891     {
7892       annotate_field (4);
7893       ui_out_field_skip (uiout, "addr");
7894     }
7895
7896   annotate_field (5);
7897   if (self->is_load)
7898     {
7899       if (self->regex)
7900         msg = xstrprintf (_("load of library matching %s"), self->regex);
7901       else
7902         msg = xstrdup (_("load of library"));
7903     }
7904   else
7905     {
7906       if (self->regex)
7907         msg = xstrprintf (_("unload of library matching %s"), self->regex);
7908       else
7909         msg = xstrdup (_("unload of library"));
7910     }
7911   ui_out_field_string (uiout, "what", msg);
7912   xfree (msg);
7913
7914   if (ui_out_is_mi_like_p (uiout))
7915     ui_out_field_string (uiout, "catch-type",
7916                          self->is_load ? "load" : "unload");
7917 }
7918
7919 static void
7920 print_mention_catch_solib (struct breakpoint *b)
7921 {
7922   struct solib_catchpoint *self = (struct solib_catchpoint *) b;
7923
7924   printf_filtered (_("Catchpoint %d (%s)"), b->number,
7925                    self->is_load ? "load" : "unload");
7926 }
7927
7928 static void
7929 print_recreate_catch_solib (struct breakpoint *b, struct ui_file *fp)
7930 {
7931   struct solib_catchpoint *self = (struct solib_catchpoint *) b;
7932
7933   fprintf_unfiltered (fp, "%s %s",
7934                       b->disposition == disp_del ? "tcatch" : "catch",
7935                       self->is_load ? "load" : "unload");
7936   if (self->regex)
7937     fprintf_unfiltered (fp, " %s", self->regex);
7938   fprintf_unfiltered (fp, "\n");
7939 }
7940
7941 static struct breakpoint_ops catch_solib_breakpoint_ops;
7942
7943 /* Shared helper function (MI and CLI) for creating and installing
7944    a shared object event catchpoint.  If IS_LOAD is non-zero then
7945    the events to be caught are load events, otherwise they are
7946    unload events.  If IS_TEMP is non-zero the catchpoint is a
7947    temporary one.  If ENABLED is non-zero the catchpoint is
7948    created in an enabled state.  */
7949
7950 void
7951 add_solib_catchpoint (char *arg, int is_load, int is_temp, int enabled)
7952 {
7953   struct solib_catchpoint *c;
7954   struct gdbarch *gdbarch = get_current_arch ();
7955   struct cleanup *cleanup;
7956
7957   if (!arg)
7958     arg = "";
7959   arg = skip_spaces (arg);
7960
7961   c = XCNEW (struct solib_catchpoint);
7962   cleanup = make_cleanup (xfree, c);
7963
7964   if (*arg != '\0')
7965     {
7966       int errcode;
7967
7968       errcode = regcomp (&c->compiled, arg, REG_NOSUB);
7969       if (errcode != 0)
7970         {
7971           char *err = get_regcomp_error (errcode, &c->compiled);
7972
7973           make_cleanup (xfree, err);
7974           error (_("Invalid regexp (%s): %s"), err, arg);
7975         }
7976       c->regex = xstrdup (arg);
7977     }
7978
7979   c->is_load = is_load;
7980   init_catchpoint (&c->base, gdbarch, is_temp, NULL,
7981                    &catch_solib_breakpoint_ops);
7982
7983   c->base.enable_state = enabled ? bp_enabled : bp_disabled;
7984
7985   discard_cleanups (cleanup);
7986   install_breakpoint (0, &c->base, 1);
7987 }
7988
7989 /* A helper function that does all the work for "catch load" and
7990    "catch unload".  */
7991
7992 static void
7993 catch_load_or_unload (char *arg, int from_tty, int is_load,
7994                       struct cmd_list_element *command)
7995 {
7996   int tempflag;
7997   const int enabled = 1;
7998
7999   tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
8000
8001   add_solib_catchpoint (arg, is_load, tempflag, enabled);
8002 }
8003
8004 static void
8005 catch_load_command_1 (char *arg, int from_tty,
8006                       struct cmd_list_element *command)
8007 {
8008   catch_load_or_unload (arg, from_tty, 1, command);
8009 }
8010
8011 static void
8012 catch_unload_command_1 (char *arg, int from_tty,
8013                         struct cmd_list_element *command)
8014 {
8015   catch_load_or_unload (arg, from_tty, 0, command);
8016 }
8017
8018 /* An instance of this type is used to represent a syscall catchpoint.
8019    It includes a "struct breakpoint" as a kind of base class; users
8020    downcast to "struct breakpoint *" when needed.  A breakpoint is
8021    really of this type iff its ops pointer points to
8022    CATCH_SYSCALL_BREAKPOINT_OPS.  */
8023
8024 struct syscall_catchpoint
8025 {
8026   /* The base class.  */
8027   struct breakpoint base;
8028
8029   /* Syscall numbers used for the 'catch syscall' feature.  If no
8030      syscall has been specified for filtering, its value is NULL.
8031      Otherwise, it holds a list of all syscalls to be caught.  The
8032      list elements are allocated with xmalloc.  */
8033   VEC(int) *syscalls_to_be_caught;
8034 };
8035
8036 /* Implement the "dtor" breakpoint_ops method for syscall
8037    catchpoints.  */
8038
8039 static void
8040 dtor_catch_syscall (struct breakpoint *b)
8041 {
8042   struct syscall_catchpoint *c = (struct syscall_catchpoint *) b;
8043
8044   VEC_free (int, c->syscalls_to_be_caught);
8045
8046   base_breakpoint_ops.dtor (b);
8047 }
8048
8049 static const struct inferior_data *catch_syscall_inferior_data = NULL;
8050
8051 struct catch_syscall_inferior_data
8052 {
8053   /* We keep a count of the number of times the user has requested a
8054      particular syscall to be tracked, and pass this information to the
8055      target.  This lets capable targets implement filtering directly.  */
8056
8057   /* Number of times that "any" syscall is requested.  */
8058   int any_syscall_count;
8059
8060   /* Count of each system call.  */
8061   VEC(int) *syscalls_counts;
8062
8063   /* This counts all syscall catch requests, so we can readily determine
8064      if any catching is necessary.  */
8065   int total_syscalls_count;
8066 };
8067
8068 static struct catch_syscall_inferior_data*
8069 get_catch_syscall_inferior_data (struct inferior *inf)
8070 {
8071   struct catch_syscall_inferior_data *inf_data;
8072
8073   inf_data = inferior_data (inf, catch_syscall_inferior_data);
8074   if (inf_data == NULL)
8075     {
8076       inf_data = XZALLOC (struct catch_syscall_inferior_data);
8077       set_inferior_data (inf, catch_syscall_inferior_data, inf_data);
8078     }
8079
8080   return inf_data;
8081 }
8082
8083 static void
8084 catch_syscall_inferior_data_cleanup (struct inferior *inf, void *arg)
8085 {
8086   xfree (arg);
8087 }
8088
8089
8090 /* Implement the "insert" breakpoint_ops method for syscall
8091    catchpoints.  */
8092
8093 static int
8094 insert_catch_syscall (struct bp_location *bl)
8095 {
8096   struct syscall_catchpoint *c = (struct syscall_catchpoint *) bl->owner;
8097   struct inferior *inf = current_inferior ();
8098   struct catch_syscall_inferior_data *inf_data
8099     = get_catch_syscall_inferior_data (inf);
8100
8101   ++inf_data->total_syscalls_count;
8102   if (!c->syscalls_to_be_caught)
8103     ++inf_data->any_syscall_count;
8104   else
8105     {
8106       int i, iter;
8107
8108       for (i = 0;
8109            VEC_iterate (int, c->syscalls_to_be_caught, i, iter);
8110            i++)
8111         {
8112           int elem;
8113
8114           if (iter >= VEC_length (int, inf_data->syscalls_counts))
8115             {
8116               int old_size = VEC_length (int, inf_data->syscalls_counts);
8117               uintptr_t vec_addr_offset
8118                 = old_size * ((uintptr_t) sizeof (int));
8119               uintptr_t vec_addr;
8120               VEC_safe_grow (int, inf_data->syscalls_counts, iter + 1);
8121               vec_addr = ((uintptr_t) VEC_address (int,
8122                                                   inf_data->syscalls_counts)
8123                           + vec_addr_offset);
8124               memset ((void *) vec_addr, 0,
8125                       (iter + 1 - old_size) * sizeof (int));
8126             }
8127           elem = VEC_index (int, inf_data->syscalls_counts, iter);
8128           VEC_replace (int, inf_data->syscalls_counts, iter, ++elem);
8129         }
8130     }
8131
8132   return target_set_syscall_catchpoint (PIDGET (inferior_ptid),
8133                                         inf_data->total_syscalls_count != 0,
8134                                         inf_data->any_syscall_count,
8135                                         VEC_length (int,
8136                                                     inf_data->syscalls_counts),
8137                                         VEC_address (int,
8138                                                      inf_data->syscalls_counts));
8139 }
8140
8141 /* Implement the "remove" breakpoint_ops method for syscall
8142    catchpoints.  */
8143
8144 static int
8145 remove_catch_syscall (struct bp_location *bl)
8146 {
8147   struct syscall_catchpoint *c = (struct syscall_catchpoint *) bl->owner;
8148   struct inferior *inf = current_inferior ();
8149   struct catch_syscall_inferior_data *inf_data
8150     = get_catch_syscall_inferior_data (inf);
8151
8152   --inf_data->total_syscalls_count;
8153   if (!c->syscalls_to_be_caught)
8154     --inf_data->any_syscall_count;
8155   else
8156     {
8157       int i, iter;
8158
8159       for (i = 0;
8160            VEC_iterate (int, c->syscalls_to_be_caught, i, iter);
8161            i++)
8162         {
8163           int elem;
8164           if (iter >= VEC_length (int, inf_data->syscalls_counts))
8165             /* Shouldn't happen.  */
8166             continue;
8167           elem = VEC_index (int, inf_data->syscalls_counts, iter);
8168           VEC_replace (int, inf_data->syscalls_counts, iter, --elem);
8169         }
8170     }
8171
8172   return target_set_syscall_catchpoint (PIDGET (inferior_ptid),
8173                                         inf_data->total_syscalls_count != 0,
8174                                         inf_data->any_syscall_count,
8175                                         VEC_length (int,
8176                                                     inf_data->syscalls_counts),
8177                                         VEC_address (int,
8178                                                      inf_data->syscalls_counts));
8179 }
8180
8181 /* Implement the "breakpoint_hit" breakpoint_ops method for syscall
8182    catchpoints.  */
8183
8184 static int
8185 breakpoint_hit_catch_syscall (const struct bp_location *bl,
8186                               struct address_space *aspace, CORE_ADDR bp_addr,
8187                               const struct target_waitstatus *ws)
8188 {
8189   /* We must check if we are catching specific syscalls in this
8190      breakpoint.  If we are, then we must guarantee that the called
8191      syscall is the same syscall we are catching.  */
8192   int syscall_number = 0;
8193   const struct syscall_catchpoint *c
8194     = (const struct syscall_catchpoint *) bl->owner;
8195
8196   if (ws->kind != TARGET_WAITKIND_SYSCALL_ENTRY
8197       && ws->kind != TARGET_WAITKIND_SYSCALL_RETURN)
8198     return 0;
8199
8200   syscall_number = ws->value.syscall_number;
8201
8202   /* Now, checking if the syscall is the same.  */
8203   if (c->syscalls_to_be_caught)
8204     {
8205       int i, iter;
8206
8207       for (i = 0;
8208            VEC_iterate (int, c->syscalls_to_be_caught, i, iter);
8209            i++)
8210         if (syscall_number == iter)
8211           break;
8212       /* Not the same.  */
8213       if (!iter)
8214         return 0;
8215     }
8216
8217   return 1;
8218 }
8219
8220 /* Implement the "print_it" breakpoint_ops method for syscall
8221    catchpoints.  */
8222
8223 static enum print_stop_action
8224 print_it_catch_syscall (bpstat bs)
8225 {
8226   struct ui_out *uiout = current_uiout;
8227   struct breakpoint *b = bs->breakpoint_at;
8228   /* These are needed because we want to know in which state a
8229      syscall is.  It can be in the TARGET_WAITKIND_SYSCALL_ENTRY
8230      or TARGET_WAITKIND_SYSCALL_RETURN, and depending on it we
8231      must print "called syscall" or "returned from syscall".  */
8232   ptid_t ptid;
8233   struct target_waitstatus last;
8234   struct syscall s;
8235
8236   get_last_target_status (&ptid, &last);
8237
8238   get_syscall_by_number (last.value.syscall_number, &s);
8239
8240   annotate_catchpoint (b->number);
8241
8242   if (b->disposition == disp_del)
8243     ui_out_text (uiout, "\nTemporary catchpoint ");
8244   else
8245     ui_out_text (uiout, "\nCatchpoint ");
8246   if (ui_out_is_mi_like_p (uiout))
8247     {
8248       ui_out_field_string (uiout, "reason",
8249                            async_reason_lookup (last.kind == TARGET_WAITKIND_SYSCALL_ENTRY
8250                                                 ? EXEC_ASYNC_SYSCALL_ENTRY
8251                                                 : EXEC_ASYNC_SYSCALL_RETURN));
8252       ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
8253     }
8254   ui_out_field_int (uiout, "bkptno", b->number);
8255
8256   if (last.kind == TARGET_WAITKIND_SYSCALL_ENTRY)
8257     ui_out_text (uiout, " (call to syscall ");
8258   else
8259     ui_out_text (uiout, " (returned from syscall ");
8260
8261   if (s.name == NULL || ui_out_is_mi_like_p (uiout))
8262     ui_out_field_int (uiout, "syscall-number", last.value.syscall_number);
8263   if (s.name != NULL)
8264     ui_out_field_string (uiout, "syscall-name", s.name);
8265
8266   ui_out_text (uiout, "), ");
8267
8268   return PRINT_SRC_AND_LOC;
8269 }
8270
8271 /* Implement the "print_one" breakpoint_ops method for syscall
8272    catchpoints.  */
8273
8274 static void
8275 print_one_catch_syscall (struct breakpoint *b,
8276                          struct bp_location **last_loc)
8277 {
8278   struct syscall_catchpoint *c = (struct syscall_catchpoint *) b;
8279   struct value_print_options opts;
8280   struct ui_out *uiout = current_uiout;
8281
8282   get_user_print_options (&opts);
8283   /* Field 4, the address, is omitted (which makes the columns not
8284      line up too nicely with the headers, but the effect is relatively
8285      readable).  */
8286   if (opts.addressprint)
8287     ui_out_field_skip (uiout, "addr");
8288   annotate_field (5);
8289
8290   if (c->syscalls_to_be_caught
8291       && VEC_length (int, c->syscalls_to_be_caught) > 1)
8292     ui_out_text (uiout, "syscalls \"");
8293   else
8294     ui_out_text (uiout, "syscall \"");
8295
8296   if (c->syscalls_to_be_caught)
8297     {
8298       int i, iter;
8299       char *text = xstrprintf ("%s", "");
8300
8301       for (i = 0;
8302            VEC_iterate (int, c->syscalls_to_be_caught, i, iter);
8303            i++)
8304         {
8305           char *x = text;
8306           struct syscall s;
8307           get_syscall_by_number (iter, &s);
8308
8309           if (s.name != NULL)
8310             text = xstrprintf ("%s%s, ", text, s.name);
8311           else
8312             text = xstrprintf ("%s%d, ", text, iter);
8313
8314           /* We have to xfree the last 'text' (now stored at 'x')
8315              because xstrprintf dynamically allocates new space for it
8316              on every call.  */
8317           xfree (x);
8318         }
8319       /* Remove the last comma.  */
8320       text[strlen (text) - 2] = '\0';
8321       ui_out_field_string (uiout, "what", text);
8322     }
8323   else
8324     ui_out_field_string (uiout, "what", "<any syscall>");
8325   ui_out_text (uiout, "\" ");
8326
8327   if (ui_out_is_mi_like_p (uiout))
8328     ui_out_field_string (uiout, "catch-type", "syscall");
8329 }
8330
8331 /* Implement the "print_mention" breakpoint_ops method for syscall
8332    catchpoints.  */
8333
8334 static void
8335 print_mention_catch_syscall (struct breakpoint *b)
8336 {
8337   struct syscall_catchpoint *c = (struct syscall_catchpoint *) b;
8338
8339   if (c->syscalls_to_be_caught)
8340     {
8341       int i, iter;
8342
8343       if (VEC_length (int, c->syscalls_to_be_caught) > 1)
8344         printf_filtered (_("Catchpoint %d (syscalls"), b->number);
8345       else
8346         printf_filtered (_("Catchpoint %d (syscall"), b->number);
8347
8348       for (i = 0;
8349            VEC_iterate (int, c->syscalls_to_be_caught, i, iter);
8350            i++)
8351         {
8352           struct syscall s;
8353           get_syscall_by_number (iter, &s);
8354
8355           if (s.name)
8356             printf_filtered (" '%s' [%d]", s.name, s.number);
8357           else
8358             printf_filtered (" %d", s.number);
8359         }
8360       printf_filtered (")");
8361     }
8362   else
8363     printf_filtered (_("Catchpoint %d (any syscall)"),
8364                      b->number);
8365 }
8366
8367 /* Implement the "print_recreate" breakpoint_ops method for syscall
8368    catchpoints.  */
8369
8370 static void
8371 print_recreate_catch_syscall (struct breakpoint *b, struct ui_file *fp)
8372 {
8373   struct syscall_catchpoint *c = (struct syscall_catchpoint *) b;
8374
8375   fprintf_unfiltered (fp, "catch syscall");
8376
8377   if (c->syscalls_to_be_caught)
8378     {
8379       int i, iter;
8380
8381       for (i = 0;
8382            VEC_iterate (int, c->syscalls_to_be_caught, i, iter);
8383            i++)
8384         {
8385           struct syscall s;
8386
8387           get_syscall_by_number (iter, &s);
8388           if (s.name)
8389             fprintf_unfiltered (fp, " %s", s.name);
8390           else
8391             fprintf_unfiltered (fp, " %d", s.number);
8392         }
8393     }
8394   print_recreate_thread (b, fp);
8395 }
8396
8397 /* The breakpoint_ops structure to be used in syscall catchpoints.  */
8398
8399 static struct breakpoint_ops catch_syscall_breakpoint_ops;
8400
8401 /* Returns non-zero if 'b' is a syscall catchpoint.  */
8402
8403 static int
8404 syscall_catchpoint_p (struct breakpoint *b)
8405 {
8406   return (b->ops == &catch_syscall_breakpoint_ops);
8407 }
8408
8409 /* Initialize a new breakpoint of the bp_catchpoint kind.  If TEMPFLAG
8410    is non-zero, then make the breakpoint temporary.  If COND_STRING is
8411    not NULL, then store it in the breakpoint.  OPS, if not NULL, is
8412    the breakpoint_ops structure associated to the catchpoint.  */
8413
8414 void
8415 init_catchpoint (struct breakpoint *b,
8416                  struct gdbarch *gdbarch, int tempflag,
8417                  char *cond_string,
8418                  const struct breakpoint_ops *ops)
8419 {
8420   struct symtab_and_line sal;
8421
8422   init_sal (&sal);
8423   sal.pspace = current_program_space;
8424
8425   init_raw_breakpoint (b, gdbarch, sal, bp_catchpoint, ops);
8426
8427   b->cond_string = (cond_string == NULL) ? NULL : xstrdup (cond_string);
8428   b->disposition = tempflag ? disp_del : disp_donttouch;
8429 }
8430
8431 void
8432 install_breakpoint (int internal, struct breakpoint *b, int update_gll)
8433 {
8434   add_to_breakpoint_chain (b);
8435   set_breakpoint_number (internal, b);
8436   if (is_tracepoint (b))
8437     set_tracepoint_count (breakpoint_count);
8438   if (!internal)
8439     mention (b);
8440   observer_notify_breakpoint_created (b);
8441
8442   if (update_gll)
8443     update_global_location_list (1);
8444 }
8445
8446 static void
8447 create_fork_vfork_event_catchpoint (struct gdbarch *gdbarch,
8448                                     int tempflag, char *cond_string,
8449                                     const struct breakpoint_ops *ops)
8450 {
8451   struct fork_catchpoint *c = XNEW (struct fork_catchpoint);
8452
8453   init_catchpoint (&c->base, gdbarch, tempflag, cond_string, ops);
8454
8455   c->forked_inferior_pid = null_ptid;
8456
8457   install_breakpoint (0, &c->base, 1);
8458 }
8459
8460 /* Exec catchpoints.  */
8461
8462 /* An instance of this type is used to represent an exec catchpoint.
8463    It includes a "struct breakpoint" as a kind of base class; users
8464    downcast to "struct breakpoint *" when needed.  A breakpoint is
8465    really of this type iff its ops pointer points to
8466    CATCH_EXEC_BREAKPOINT_OPS.  */
8467
8468 struct exec_catchpoint
8469 {
8470   /* The base class.  */
8471   struct breakpoint base;
8472
8473   /* Filename of a program whose exec triggered this catchpoint.
8474      This field is only valid immediately after this catchpoint has
8475      triggered.  */
8476   char *exec_pathname;
8477 };
8478
8479 /* Implement the "dtor" breakpoint_ops method for exec
8480    catchpoints.  */
8481
8482 static void
8483 dtor_catch_exec (struct breakpoint *b)
8484 {
8485   struct exec_catchpoint *c = (struct exec_catchpoint *) b;
8486
8487   xfree (c->exec_pathname);
8488
8489   base_breakpoint_ops.dtor (b);
8490 }
8491
8492 static int
8493 insert_catch_exec (struct bp_location *bl)
8494 {
8495   return target_insert_exec_catchpoint (PIDGET (inferior_ptid));
8496 }
8497
8498 static int
8499 remove_catch_exec (struct bp_location *bl)
8500 {
8501   return target_remove_exec_catchpoint (PIDGET (inferior_ptid));
8502 }
8503
8504 static int
8505 breakpoint_hit_catch_exec (const struct bp_location *bl,
8506                            struct address_space *aspace, CORE_ADDR bp_addr,
8507                            const struct target_waitstatus *ws)
8508 {
8509   struct exec_catchpoint *c = (struct exec_catchpoint *) bl->owner;
8510
8511   if (ws->kind != TARGET_WAITKIND_EXECD)
8512     return 0;
8513
8514   c->exec_pathname = xstrdup (ws->value.execd_pathname);
8515   return 1;
8516 }
8517
8518 static enum print_stop_action
8519 print_it_catch_exec (bpstat bs)
8520 {
8521   struct ui_out *uiout = current_uiout;
8522   struct breakpoint *b = bs->breakpoint_at;
8523   struct exec_catchpoint *c = (struct exec_catchpoint *) b;
8524
8525   annotate_catchpoint (b->number);
8526   if (b->disposition == disp_del)
8527     ui_out_text (uiout, "\nTemporary catchpoint ");
8528   else
8529     ui_out_text (uiout, "\nCatchpoint ");
8530   if (ui_out_is_mi_like_p (uiout))
8531     {
8532       ui_out_field_string (uiout, "reason",
8533                            async_reason_lookup (EXEC_ASYNC_EXEC));
8534       ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
8535     }
8536   ui_out_field_int (uiout, "bkptno", b->number);
8537   ui_out_text (uiout, " (exec'd ");
8538   ui_out_field_string (uiout, "new-exec", c->exec_pathname);
8539   ui_out_text (uiout, "), ");
8540
8541   return PRINT_SRC_AND_LOC;
8542 }
8543
8544 static void
8545 print_one_catch_exec (struct breakpoint *b, struct bp_location **last_loc)
8546 {
8547   struct exec_catchpoint *c = (struct exec_catchpoint *) b;
8548   struct value_print_options opts;
8549   struct ui_out *uiout = current_uiout;
8550
8551   get_user_print_options (&opts);
8552
8553   /* Field 4, the address, is omitted (which makes the columns
8554      not line up too nicely with the headers, but the effect
8555      is relatively readable).  */
8556   if (opts.addressprint)
8557     ui_out_field_skip (uiout, "addr");
8558   annotate_field (5);
8559   ui_out_text (uiout, "exec");
8560   if (c->exec_pathname != NULL)
8561     {
8562       ui_out_text (uiout, ", program \"");
8563       ui_out_field_string (uiout, "what", c->exec_pathname);
8564       ui_out_text (uiout, "\" ");
8565     }
8566
8567   if (ui_out_is_mi_like_p (uiout))
8568     ui_out_field_string (uiout, "catch-type", "exec");
8569 }
8570
8571 static void
8572 print_mention_catch_exec (struct breakpoint *b)
8573 {
8574   printf_filtered (_("Catchpoint %d (exec)"), b->number);
8575 }
8576
8577 /* Implement the "print_recreate" breakpoint_ops method for exec
8578    catchpoints.  */
8579
8580 static void
8581 print_recreate_catch_exec (struct breakpoint *b, struct ui_file *fp)
8582 {
8583   fprintf_unfiltered (fp, "catch exec");
8584   print_recreate_thread (b, fp);
8585 }
8586
8587 static struct breakpoint_ops catch_exec_breakpoint_ops;
8588
8589 static void
8590 create_syscall_event_catchpoint (int tempflag, VEC(int) *filter,
8591                                  const struct breakpoint_ops *ops)
8592 {
8593   struct syscall_catchpoint *c;
8594   struct gdbarch *gdbarch = get_current_arch ();
8595
8596   c = XNEW (struct syscall_catchpoint);
8597   init_catchpoint (&c->base, gdbarch, tempflag, NULL, ops);
8598   c->syscalls_to_be_caught = filter;
8599
8600   install_breakpoint (0, &c->base, 1);
8601 }
8602
8603 static int
8604 hw_breakpoint_used_count (void)
8605 {
8606   int i = 0;
8607   struct breakpoint *b;
8608   struct bp_location *bl;
8609
8610   ALL_BREAKPOINTS (b)
8611   {
8612     if (b->type == bp_hardware_breakpoint && breakpoint_enabled (b))
8613       for (bl = b->loc; bl; bl = bl->next)
8614         {
8615           /* Special types of hardware breakpoints may use more than
8616              one register.  */
8617           i += b->ops->resources_needed (bl);
8618         }
8619   }
8620
8621   return i;
8622 }
8623
8624 /* Returns the resources B would use if it were a hardware
8625    watchpoint.  */
8626
8627 static int
8628 hw_watchpoint_use_count (struct breakpoint *b)
8629 {
8630   int i = 0;
8631   struct bp_location *bl;
8632
8633   if (!breakpoint_enabled (b))
8634     return 0;
8635
8636   for (bl = b->loc; bl; bl = bl->next)
8637     {
8638       /* Special types of hardware watchpoints may use more than
8639          one register.  */
8640       i += b->ops->resources_needed (bl);
8641     }
8642
8643   return i;
8644 }
8645
8646 /* Returns the sum the used resources of all hardware watchpoints of
8647    type TYPE in the breakpoints list.  Also returns in OTHER_TYPE_USED
8648    the sum of the used resources of all hardware watchpoints of other
8649    types _not_ TYPE.  */
8650
8651 static int
8652 hw_watchpoint_used_count_others (struct breakpoint *except,
8653                                  enum bptype type, int *other_type_used)
8654 {
8655   int i = 0;
8656   struct breakpoint *b;
8657
8658   *other_type_used = 0;
8659   ALL_BREAKPOINTS (b)
8660     {
8661       if (b == except)
8662         continue;
8663       if (!breakpoint_enabled (b))
8664         continue;
8665
8666       if (b->type == type)
8667         i += hw_watchpoint_use_count (b);
8668       else if (is_hardware_watchpoint (b))
8669         *other_type_used = 1;
8670     }
8671
8672   return i;
8673 }
8674
8675 void
8676 disable_watchpoints_before_interactive_call_start (void)
8677 {
8678   struct breakpoint *b;
8679
8680   ALL_BREAKPOINTS (b)
8681   {
8682     if (is_watchpoint (b) && breakpoint_enabled (b))
8683       {
8684         b->enable_state = bp_call_disabled;
8685         update_global_location_list (0);
8686       }
8687   }
8688 }
8689
8690 void
8691 enable_watchpoints_after_interactive_call_stop (void)
8692 {
8693   struct breakpoint *b;
8694
8695   ALL_BREAKPOINTS (b)
8696   {
8697     if (is_watchpoint (b) && b->enable_state == bp_call_disabled)
8698       {
8699         b->enable_state = bp_enabled;
8700         update_global_location_list (1);
8701       }
8702   }
8703 }
8704
8705 void
8706 disable_breakpoints_before_startup (void)
8707 {
8708   current_program_space->executing_startup = 1;
8709   update_global_location_list (0);
8710 }
8711
8712 void
8713 enable_breakpoints_after_startup (void)
8714 {
8715   current_program_space->executing_startup = 0;
8716   breakpoint_re_set ();
8717 }
8718
8719
8720 /* Set a breakpoint that will evaporate an end of command
8721    at address specified by SAL.
8722    Restrict it to frame FRAME if FRAME is nonzero.  */
8723
8724 struct breakpoint *
8725 set_momentary_breakpoint (struct gdbarch *gdbarch, struct symtab_and_line sal,
8726                           struct frame_id frame_id, enum bptype type)
8727 {
8728   struct breakpoint *b;
8729
8730   /* If FRAME_ID is valid, it should be a real frame, not an inlined or
8731      tail-called one.  */
8732   gdb_assert (!frame_id_artificial_p (frame_id));
8733
8734   b = set_raw_breakpoint (gdbarch, sal, type, &momentary_breakpoint_ops);
8735   b->enable_state = bp_enabled;
8736   b->disposition = disp_donttouch;
8737   b->frame_id = frame_id;
8738
8739   /* If we're debugging a multi-threaded program, then we want
8740      momentary breakpoints to be active in only a single thread of
8741      control.  */
8742   if (in_thread_list (inferior_ptid))
8743     b->thread = pid_to_thread_id (inferior_ptid);
8744
8745   update_global_location_list_nothrow (1);
8746
8747   return b;
8748 }
8749
8750 /* Make a momentary breakpoint based on the master breakpoint ORIG.
8751    The new breakpoint will have type TYPE, and use OPS as it
8752    breakpoint_ops.  */
8753
8754 static struct breakpoint *
8755 momentary_breakpoint_from_master (struct breakpoint *orig,
8756                                   enum bptype type,
8757                                   const struct breakpoint_ops *ops)
8758 {
8759   struct breakpoint *copy;
8760
8761   copy = set_raw_breakpoint_without_location (orig->gdbarch, type, ops);
8762   copy->loc = allocate_bp_location (copy);
8763   set_breakpoint_location_function (copy->loc, 1);
8764
8765   copy->loc->gdbarch = orig->loc->gdbarch;
8766   copy->loc->requested_address = orig->loc->requested_address;
8767   copy->loc->address = orig->loc->address;
8768   copy->loc->section = orig->loc->section;
8769   copy->loc->pspace = orig->loc->pspace;
8770   copy->loc->probe = orig->loc->probe;
8771   copy->loc->line_number = orig->loc->line_number;
8772   copy->loc->symtab = orig->loc->symtab;
8773   copy->frame_id = orig->frame_id;
8774   copy->thread = orig->thread;
8775   copy->pspace = orig->pspace;
8776
8777   copy->enable_state = bp_enabled;
8778   copy->disposition = disp_donttouch;
8779   copy->number = internal_breakpoint_number--;
8780
8781   update_global_location_list_nothrow (0);
8782   return copy;
8783 }
8784
8785 /* Make a deep copy of momentary breakpoint ORIG.  Returns NULL if
8786    ORIG is NULL.  */
8787
8788 struct breakpoint *
8789 clone_momentary_breakpoint (struct breakpoint *orig)
8790 {
8791   /* If there's nothing to clone, then return nothing.  */
8792   if (orig == NULL)
8793     return NULL;
8794
8795   return momentary_breakpoint_from_master (orig, orig->type, orig->ops);
8796 }
8797
8798 struct breakpoint *
8799 set_momentary_breakpoint_at_pc (struct gdbarch *gdbarch, CORE_ADDR pc,
8800                                 enum bptype type)
8801 {
8802   struct symtab_and_line sal;
8803
8804   sal = find_pc_line (pc, 0);
8805   sal.pc = pc;
8806   sal.section = find_pc_overlay (pc);
8807   sal.explicit_pc = 1;
8808
8809   return set_momentary_breakpoint (gdbarch, sal, null_frame_id, type);
8810 }
8811 \f
8812
8813 /* Tell the user we have just set a breakpoint B.  */
8814
8815 static void
8816 mention (struct breakpoint *b)
8817 {
8818   b->ops->print_mention (b);
8819   if (ui_out_is_mi_like_p (current_uiout))
8820     return;
8821   printf_filtered ("\n");
8822 }
8823 \f
8824
8825 static struct bp_location *
8826 add_location_to_breakpoint (struct breakpoint *b,
8827                             const struct symtab_and_line *sal)
8828 {
8829   struct bp_location *loc, **tmp;
8830   CORE_ADDR adjusted_address;
8831   struct gdbarch *loc_gdbarch = get_sal_arch (*sal);
8832
8833   if (loc_gdbarch == NULL)
8834     loc_gdbarch = b->gdbarch;
8835
8836   /* Adjust the breakpoint's address prior to allocating a location.
8837      Once we call allocate_bp_location(), that mostly uninitialized
8838      location will be placed on the location chain.  Adjustment of the
8839      breakpoint may cause target_read_memory() to be called and we do
8840      not want its scan of the location chain to find a breakpoint and
8841      location that's only been partially initialized.  */
8842   adjusted_address = adjust_breakpoint_address (loc_gdbarch,
8843                                                 sal->pc, b->type);
8844
8845   /* Sort the locations by their ADDRESS.  */
8846   loc = allocate_bp_location (b);
8847   for (tmp = &(b->loc); *tmp != NULL && (*tmp)->address <= adjusted_address;
8848        tmp = &((*tmp)->next))
8849     ;
8850   loc->next = *tmp;
8851   *tmp = loc;
8852
8853   loc->requested_address = sal->pc;
8854   loc->address = adjusted_address;
8855   loc->pspace = sal->pspace;
8856   loc->probe = sal->probe;
8857   gdb_assert (loc->pspace != NULL);
8858   loc->section = sal->section;
8859   loc->gdbarch = loc_gdbarch;
8860   loc->line_number = sal->line;
8861   loc->symtab = sal->symtab;
8862
8863   set_breakpoint_location_function (loc,
8864                                     sal->explicit_pc || sal->explicit_line);
8865   return loc;
8866 }
8867 \f
8868
8869 /* Return 1 if LOC is pointing to a permanent breakpoint, 
8870    return 0 otherwise.  */
8871
8872 static int
8873 bp_loc_is_permanent (struct bp_location *loc)
8874 {
8875   int len;
8876   CORE_ADDR addr;
8877   const gdb_byte *bpoint;
8878   gdb_byte *target_mem;
8879   struct cleanup *cleanup;
8880   int retval = 0;
8881
8882   gdb_assert (loc != NULL);
8883
8884   addr = loc->address;
8885   bpoint = gdbarch_breakpoint_from_pc (loc->gdbarch, &addr, &len);
8886
8887   /* Software breakpoints unsupported?  */
8888   if (bpoint == NULL)
8889     return 0;
8890
8891   target_mem = alloca (len);
8892
8893   /* Enable the automatic memory restoration from breakpoints while
8894      we read the memory.  Otherwise we could say about our temporary
8895      breakpoints they are permanent.  */
8896   cleanup = save_current_space_and_thread ();
8897
8898   switch_to_program_space_and_thread (loc->pspace);
8899   make_show_memory_breakpoints_cleanup (0);
8900
8901   if (target_read_memory (loc->address, target_mem, len) == 0
8902       && memcmp (target_mem, bpoint, len) == 0)
8903     retval = 1;
8904
8905   do_cleanups (cleanup);
8906
8907   return retval;
8908 }
8909
8910 /* Build a command list for the dprintf corresponding to the current
8911    settings of the dprintf style options.  */
8912
8913 static void
8914 update_dprintf_command_list (struct breakpoint *b)
8915 {
8916   char *dprintf_args = b->extra_string;
8917   char *printf_line = NULL;
8918
8919   if (!dprintf_args)
8920     return;
8921
8922   dprintf_args = skip_spaces (dprintf_args);
8923
8924   /* Allow a comma, as it may have terminated a location, but don't
8925      insist on it.  */
8926   if (*dprintf_args == ',')
8927     ++dprintf_args;
8928   dprintf_args = skip_spaces (dprintf_args);
8929
8930   if (*dprintf_args != '"')
8931     error (_("Bad format string, missing '\"'."));
8932
8933   if (strcmp (dprintf_style, dprintf_style_gdb) == 0)
8934     printf_line = xstrprintf ("printf %s", dprintf_args);
8935   else if (strcmp (dprintf_style, dprintf_style_call) == 0)
8936     {
8937       if (!dprintf_function)
8938         error (_("No function supplied for dprintf call"));
8939
8940       if (dprintf_channel && strlen (dprintf_channel) > 0)
8941         printf_line = xstrprintf ("call (void) %s (%s,%s)",
8942                                   dprintf_function,
8943                                   dprintf_channel,
8944                                   dprintf_args);
8945       else
8946         printf_line = xstrprintf ("call (void) %s (%s)",
8947                                   dprintf_function,
8948                                   dprintf_args);
8949     }
8950   else if (strcmp (dprintf_style, dprintf_style_agent) == 0)
8951     {
8952       if (target_can_run_breakpoint_commands ())
8953         printf_line = xstrprintf ("agent-printf %s", dprintf_args);
8954       else
8955         {
8956           warning (_("Target cannot run dprintf commands, falling back to GDB printf"));
8957           printf_line = xstrprintf ("printf %s", dprintf_args);
8958         }
8959     }
8960   else
8961     internal_error (__FILE__, __LINE__,
8962                     _("Invalid dprintf style."));
8963
8964   gdb_assert (printf_line != NULL);
8965   /* Manufacture a printf/continue sequence.  */
8966   {
8967     struct command_line *printf_cmd_line, *cont_cmd_line = NULL;
8968
8969     if (strcmp (dprintf_style, dprintf_style_agent) != 0)
8970       {
8971         cont_cmd_line = xmalloc (sizeof (struct command_line));
8972         cont_cmd_line->control_type = simple_control;
8973         cont_cmd_line->body_count = 0;
8974         cont_cmd_line->body_list = NULL;
8975         cont_cmd_line->next = NULL;
8976         cont_cmd_line->line = xstrdup ("continue");
8977       }
8978
8979     printf_cmd_line = xmalloc (sizeof (struct command_line));
8980     printf_cmd_line->control_type = simple_control;
8981     printf_cmd_line->body_count = 0;
8982     printf_cmd_line->body_list = NULL;
8983     printf_cmd_line->next = cont_cmd_line;
8984     printf_cmd_line->line = printf_line;
8985
8986     breakpoint_set_commands (b, printf_cmd_line);
8987   }
8988 }
8989
8990 /* Update all dprintf commands, making their command lists reflect
8991    current style settings.  */
8992
8993 static void
8994 update_dprintf_commands (char *args, int from_tty,
8995                          struct cmd_list_element *c)
8996 {
8997   struct breakpoint *b;
8998
8999   ALL_BREAKPOINTS (b)
9000     {
9001       if (b->type == bp_dprintf)
9002         update_dprintf_command_list (b);
9003     }
9004 }
9005
9006 /* Create a breakpoint with SAL as location.  Use ADDR_STRING
9007    as textual description of the location, and COND_STRING
9008    as condition expression.  */
9009
9010 static void
9011 init_breakpoint_sal (struct breakpoint *b, struct gdbarch *gdbarch,
9012                      struct symtabs_and_lines sals, char *addr_string,
9013                      char *filter, char *cond_string,
9014                      char *extra_string,
9015                      enum bptype type, enum bpdisp disposition,
9016                      int thread, int task, int ignore_count,
9017                      const struct breakpoint_ops *ops, int from_tty,
9018                      int enabled, int internal, unsigned flags,
9019                      int display_canonical)
9020 {
9021   int i;
9022
9023   if (type == bp_hardware_breakpoint)
9024     {
9025       int target_resources_ok;
9026
9027       i = hw_breakpoint_used_count ();
9028       target_resources_ok =
9029         target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
9030                                             i + 1, 0);
9031       if (target_resources_ok == 0)
9032         error (_("No hardware breakpoint support in the target."));
9033       else if (target_resources_ok < 0)
9034         error (_("Hardware breakpoints used exceeds limit."));
9035     }
9036
9037   gdb_assert (sals.nelts > 0);
9038
9039   for (i = 0; i < sals.nelts; ++i)
9040     {
9041       struct symtab_and_line sal = sals.sals[i];
9042       struct bp_location *loc;
9043
9044       if (from_tty)
9045         {
9046           struct gdbarch *loc_gdbarch = get_sal_arch (sal);
9047           if (!loc_gdbarch)
9048             loc_gdbarch = gdbarch;
9049
9050           describe_other_breakpoints (loc_gdbarch,
9051                                       sal.pspace, sal.pc, sal.section, thread);
9052         }
9053
9054       if (i == 0)
9055         {
9056           init_raw_breakpoint (b, gdbarch, sal, type, ops);
9057           b->thread = thread;
9058           b->task = task;
9059
9060           b->cond_string = cond_string;
9061           b->extra_string = extra_string;
9062           b->ignore_count = ignore_count;
9063           b->enable_state = enabled ? bp_enabled : bp_disabled;
9064           b->disposition = disposition;
9065
9066           if ((flags & CREATE_BREAKPOINT_FLAGS_INSERTED) != 0)
9067             b->loc->inserted = 1;
9068
9069           if (type == bp_static_tracepoint)
9070             {
9071               struct tracepoint *t = (struct tracepoint *) b;
9072               struct static_tracepoint_marker marker;
9073
9074               if (strace_marker_p (b))
9075                 {
9076                   /* We already know the marker exists, otherwise, we
9077                      wouldn't see a sal for it.  */
9078                   char *p = &addr_string[3];
9079                   char *endp;
9080                   char *marker_str;
9081
9082                   p = skip_spaces (p);
9083
9084                   endp = skip_to_space (p);
9085
9086                   marker_str = savestring (p, endp - p);
9087                   t->static_trace_marker_id = marker_str;
9088
9089                   printf_filtered (_("Probed static tracepoint "
9090                                      "marker \"%s\"\n"),
9091                                    t->static_trace_marker_id);
9092                 }
9093               else if (target_static_tracepoint_marker_at (sal.pc, &marker))
9094                 {
9095                   t->static_trace_marker_id = xstrdup (marker.str_id);
9096                   release_static_tracepoint_marker (&marker);
9097
9098                   printf_filtered (_("Probed static tracepoint "
9099                                      "marker \"%s\"\n"),
9100                                    t->static_trace_marker_id);
9101                 }
9102               else
9103                 warning (_("Couldn't determine the static "
9104                            "tracepoint marker to probe"));
9105             }
9106
9107           loc = b->loc;
9108         }
9109       else
9110         {
9111           loc = add_location_to_breakpoint (b, &sal);
9112           if ((flags & CREATE_BREAKPOINT_FLAGS_INSERTED) != 0)
9113             loc->inserted = 1;
9114         }
9115
9116       if (bp_loc_is_permanent (loc))
9117         make_breakpoint_permanent (b);
9118
9119       if (b->cond_string)
9120         {
9121           const char *arg = b->cond_string;
9122
9123           loc->cond = parse_exp_1 (&arg, loc->address,
9124                                    block_for_pc (loc->address), 0);
9125           if (*arg)
9126               error (_("Garbage '%s' follows condition"), arg);
9127         }
9128
9129       /* Dynamic printf requires and uses additional arguments on the
9130          command line, otherwise it's an error.  */
9131       if (type == bp_dprintf)
9132         {
9133           if (b->extra_string)
9134             update_dprintf_command_list (b);
9135           else
9136             error (_("Format string required"));
9137         }
9138       else if (b->extra_string)
9139         error (_("Garbage '%s' at end of command"), b->extra_string);
9140     }
9141
9142   b->display_canonical = display_canonical;
9143   if (addr_string)
9144     b->addr_string = addr_string;
9145   else
9146     /* addr_string has to be used or breakpoint_re_set will delete
9147        me.  */
9148     b->addr_string
9149       = xstrprintf ("*%s", paddress (b->loc->gdbarch, b->loc->address));
9150   b->filter = filter;
9151 }
9152
9153 static void
9154 create_breakpoint_sal (struct gdbarch *gdbarch,
9155                        struct symtabs_and_lines sals, char *addr_string,
9156                        char *filter, char *cond_string,
9157                        char *extra_string,
9158                        enum bptype type, enum bpdisp disposition,
9159                        int thread, int task, int ignore_count,
9160                        const struct breakpoint_ops *ops, int from_tty,
9161                        int enabled, int internal, unsigned flags,
9162                        int display_canonical)
9163 {
9164   struct breakpoint *b;
9165   struct cleanup *old_chain;
9166
9167   if (is_tracepoint_type (type))
9168     {
9169       struct tracepoint *t;
9170
9171       t = XCNEW (struct tracepoint);
9172       b = &t->base;
9173     }
9174   else
9175     b = XNEW (struct breakpoint);
9176
9177   old_chain = make_cleanup (xfree, b);
9178
9179   init_breakpoint_sal (b, gdbarch,
9180                        sals, addr_string,
9181                        filter, cond_string, extra_string,
9182                        type, disposition,
9183                        thread, task, ignore_count,
9184                        ops, from_tty,
9185                        enabled, internal, flags,
9186                        display_canonical);
9187   discard_cleanups (old_chain);
9188
9189   install_breakpoint (internal, b, 0);
9190 }
9191
9192 /* Add SALS.nelts breakpoints to the breakpoint table.  For each
9193    SALS.sal[i] breakpoint, include the corresponding ADDR_STRING[i]
9194    value.  COND_STRING, if not NULL, specified the condition to be
9195    used for all breakpoints.  Essentially the only case where
9196    SALS.nelts is not 1 is when we set a breakpoint on an overloaded
9197    function.  In that case, it's still not possible to specify
9198    separate conditions for different overloaded functions, so
9199    we take just a single condition string.
9200    
9201    NOTE: If the function succeeds, the caller is expected to cleanup
9202    the arrays ADDR_STRING, COND_STRING, and SALS (but not the
9203    array contents).  If the function fails (error() is called), the
9204    caller is expected to cleanups both the ADDR_STRING, COND_STRING,
9205    COND and SALS arrays and each of those arrays contents.  */
9206
9207 static void
9208 create_breakpoints_sal (struct gdbarch *gdbarch,
9209                         struct linespec_result *canonical,
9210                         char *cond_string, char *extra_string,
9211                         enum bptype type, enum bpdisp disposition,
9212                         int thread, int task, int ignore_count,
9213                         const struct breakpoint_ops *ops, int from_tty,
9214                         int enabled, int internal, unsigned flags)
9215 {
9216   int i;
9217   struct linespec_sals *lsal;
9218
9219   if (canonical->pre_expanded)
9220     gdb_assert (VEC_length (linespec_sals, canonical->sals) == 1);
9221
9222   for (i = 0; VEC_iterate (linespec_sals, canonical->sals, i, lsal); ++i)
9223     {
9224       /* Note that 'addr_string' can be NULL in the case of a plain
9225          'break', without arguments.  */
9226       char *addr_string = (canonical->addr_string
9227                            ? xstrdup (canonical->addr_string)
9228                            : NULL);
9229       char *filter_string = lsal->canonical ? xstrdup (lsal->canonical) : NULL;
9230       struct cleanup *inner = make_cleanup (xfree, addr_string);
9231
9232       make_cleanup (xfree, filter_string);
9233       create_breakpoint_sal (gdbarch, lsal->sals,
9234                              addr_string,
9235                              filter_string,
9236                              cond_string, extra_string,
9237                              type, disposition,
9238                              thread, task, ignore_count, ops,
9239                              from_tty, enabled, internal, flags,
9240                              canonical->special_display);
9241       discard_cleanups (inner);
9242     }
9243 }
9244
9245 /* Parse ADDRESS which is assumed to be a SAL specification possibly
9246    followed by conditionals.  On return, SALS contains an array of SAL
9247    addresses found.  ADDR_STRING contains a vector of (canonical)
9248    address strings.  ADDRESS points to the end of the SAL.
9249
9250    The array and the line spec strings are allocated on the heap, it is
9251    the caller's responsibility to free them.  */
9252
9253 static void
9254 parse_breakpoint_sals (char **address,
9255                        struct linespec_result *canonical)
9256 {
9257   /* If no arg given, or if first arg is 'if ', use the default
9258      breakpoint.  */
9259   if ((*address) == NULL
9260       || (strncmp ((*address), "if", 2) == 0 && isspace ((*address)[2])))
9261     {
9262       /* The last displayed codepoint, if it's valid, is our default breakpoint
9263          address.  */
9264       if (last_displayed_sal_is_valid ())
9265         {
9266           struct linespec_sals lsal;
9267           struct symtab_and_line sal;
9268           CORE_ADDR pc;
9269
9270           init_sal (&sal);              /* Initialize to zeroes.  */
9271           lsal.sals.sals = (struct symtab_and_line *)
9272             xmalloc (sizeof (struct symtab_and_line));
9273
9274           /* Set sal's pspace, pc, symtab, and line to the values
9275              corresponding to the last call to print_frame_info.
9276              Be sure to reinitialize LINE with NOTCURRENT == 0
9277              as the breakpoint line number is inappropriate otherwise.
9278              find_pc_line would adjust PC, re-set it back.  */
9279           get_last_displayed_sal (&sal);
9280           pc = sal.pc;
9281           sal = find_pc_line (pc, 0);
9282
9283           /* "break" without arguments is equivalent to "break *PC"
9284              where PC is the last displayed codepoint's address.  So
9285              make sure to set sal.explicit_pc to prevent GDB from
9286              trying to expand the list of sals to include all other
9287              instances with the same symtab and line.  */
9288           sal.pc = pc;
9289           sal.explicit_pc = 1;
9290
9291           lsal.sals.sals[0] = sal;
9292           lsal.sals.nelts = 1;
9293           lsal.canonical = NULL;
9294
9295           VEC_safe_push (linespec_sals, canonical->sals, &lsal);
9296         }
9297       else
9298         error (_("No default breakpoint address now."));
9299     }
9300   else
9301     {
9302       struct symtab_and_line cursal = get_current_source_symtab_and_line ();
9303
9304       /* Force almost all breakpoints to be in terms of the
9305          current_source_symtab (which is decode_line_1's default).
9306          This should produce the results we want almost all of the
9307          time while leaving default_breakpoint_* alone.
9308
9309          ObjC: However, don't match an Objective-C method name which
9310          may have a '+' or '-' succeeded by a '['.  */
9311       if (last_displayed_sal_is_valid ()
9312           && (!cursal.symtab
9313               || ((strchr ("+-", (*address)[0]) != NULL)
9314                   && ((*address)[1] != '['))))
9315         decode_line_full (address, DECODE_LINE_FUNFIRSTLINE,
9316                           get_last_displayed_symtab (),
9317                           get_last_displayed_line (),
9318                           canonical, NULL, NULL);
9319       else
9320         decode_line_full (address, DECODE_LINE_FUNFIRSTLINE,
9321                           cursal.symtab, cursal.line, canonical, NULL, NULL);
9322     }
9323 }
9324
9325
9326 /* Convert each SAL into a real PC.  Verify that the PC can be
9327    inserted as a breakpoint.  If it can't throw an error.  */
9328
9329 static void
9330 breakpoint_sals_to_pc (struct symtabs_and_lines *sals)
9331 {    
9332   int i;
9333
9334   for (i = 0; i < sals->nelts; i++)
9335     resolve_sal_pc (&sals->sals[i]);
9336 }
9337
9338 /* Fast tracepoints may have restrictions on valid locations.  For
9339    instance, a fast tracepoint using a jump instead of a trap will
9340    likely have to overwrite more bytes than a trap would, and so can
9341    only be placed where the instruction is longer than the jump, or a
9342    multi-instruction sequence does not have a jump into the middle of
9343    it, etc.  */
9344
9345 static void
9346 check_fast_tracepoint_sals (struct gdbarch *gdbarch,
9347                             struct symtabs_and_lines *sals)
9348 {
9349   int i, rslt;
9350   struct symtab_and_line *sal;
9351   char *msg;
9352   struct cleanup *old_chain;
9353
9354   for (i = 0; i < sals->nelts; i++)
9355     {
9356       struct gdbarch *sarch;
9357
9358       sal = &sals->sals[i];
9359
9360       sarch = get_sal_arch (*sal);
9361       /* We fall back to GDBARCH if there is no architecture
9362          associated with SAL.  */
9363       if (sarch == NULL)
9364         sarch = gdbarch;
9365       rslt = gdbarch_fast_tracepoint_valid_at (sarch, sal->pc,
9366                                                NULL, &msg);
9367       old_chain = make_cleanup (xfree, msg);
9368
9369       if (!rslt)
9370         error (_("May not have a fast tracepoint at 0x%s%s"),
9371                paddress (sarch, sal->pc), (msg ? msg : ""));
9372
9373       do_cleanups (old_chain);
9374     }
9375 }
9376
9377 /* Issue an invalid thread ID error.  */
9378
9379 static void ATTRIBUTE_NORETURN
9380 invalid_thread_id_error (int id)
9381 {
9382   error (_("Unknown thread %d."), id);
9383 }
9384
9385 /* Given TOK, a string specification of condition and thread, as
9386    accepted by the 'break' command, extract the condition
9387    string and thread number and set *COND_STRING and *THREAD.
9388    PC identifies the context at which the condition should be parsed.
9389    If no condition is found, *COND_STRING is set to NULL.
9390    If no thread is found, *THREAD is set to -1.  */
9391
9392 static void
9393 find_condition_and_thread (const char *tok, CORE_ADDR pc,
9394                            char **cond_string, int *thread, int *task,
9395                            char **rest)
9396 {
9397   *cond_string = NULL;
9398   *thread = -1;
9399   *task = 0;
9400   *rest = NULL;
9401
9402   while (tok && *tok)
9403     {
9404       const char *end_tok;
9405       int toklen;
9406       const char *cond_start = NULL;
9407       const char *cond_end = NULL;
9408
9409       tok = skip_spaces_const (tok);
9410
9411       if ((*tok == '"' || *tok == ',') && rest)
9412         {
9413           *rest = savestring (tok, strlen (tok));
9414           return;
9415         }
9416
9417       end_tok = skip_to_space_const (tok);
9418
9419       toklen = end_tok - tok;
9420
9421       if (toklen >= 1 && strncmp (tok, "if", toklen) == 0)
9422         {
9423           struct expression *expr;
9424
9425           tok = cond_start = end_tok + 1;
9426           expr = parse_exp_1 (&tok, pc, block_for_pc (pc), 0);
9427           xfree (expr);
9428           cond_end = tok;
9429           *cond_string = savestring (cond_start, cond_end - cond_start);
9430         }
9431       else if (toklen >= 1 && strncmp (tok, "thread", toklen) == 0)
9432         {
9433           char *tmptok;
9434
9435           tok = end_tok + 1;
9436           *thread = strtol (tok, &tmptok, 0);
9437           if (tok == tmptok)
9438             error (_("Junk after thread keyword."));
9439           if (!valid_thread_id (*thread))
9440             invalid_thread_id_error (*thread);
9441           tok = tmptok;
9442         }
9443       else if (toklen >= 1 && strncmp (tok, "task", toklen) == 0)
9444         {
9445           char *tmptok;
9446
9447           tok = end_tok + 1;
9448           *task = strtol (tok, &tmptok, 0);
9449           if (tok == tmptok)
9450             error (_("Junk after task keyword."));
9451           if (!valid_task_id (*task))
9452             error (_("Unknown task %d."), *task);
9453           tok = tmptok;
9454         }
9455       else if (rest)
9456         {
9457           *rest = savestring (tok, strlen (tok));
9458           return;
9459         }
9460       else
9461         error (_("Junk at end of arguments."));
9462     }
9463 }
9464
9465 /* Decode a static tracepoint marker spec.  */
9466
9467 static struct symtabs_and_lines
9468 decode_static_tracepoint_spec (char **arg_p)
9469 {
9470   VEC(static_tracepoint_marker_p) *markers = NULL;
9471   struct symtabs_and_lines sals;
9472   struct cleanup *old_chain;
9473   char *p = &(*arg_p)[3];
9474   char *endp;
9475   char *marker_str;
9476   int i;
9477
9478   p = skip_spaces (p);
9479
9480   endp = skip_to_space (p);
9481
9482   marker_str = savestring (p, endp - p);
9483   old_chain = make_cleanup (xfree, marker_str);
9484
9485   markers = target_static_tracepoint_markers_by_strid (marker_str);
9486   if (VEC_empty(static_tracepoint_marker_p, markers))
9487     error (_("No known static tracepoint marker named %s"), marker_str);
9488
9489   sals.nelts = VEC_length(static_tracepoint_marker_p, markers);
9490   sals.sals = xmalloc (sizeof *sals.sals * sals.nelts);
9491
9492   for (i = 0; i < sals.nelts; i++)
9493     {
9494       struct static_tracepoint_marker *marker;
9495
9496       marker = VEC_index (static_tracepoint_marker_p, markers, i);
9497
9498       init_sal (&sals.sals[i]);
9499
9500       sals.sals[i] = find_pc_line (marker->address, 0);
9501       sals.sals[i].pc = marker->address;
9502
9503       release_static_tracepoint_marker (marker);
9504     }
9505
9506   do_cleanups (old_chain);
9507
9508   *arg_p = endp;
9509   return sals;
9510 }
9511
9512 /* Set a breakpoint.  This function is shared between CLI and MI
9513    functions for setting a breakpoint.  This function has two major
9514    modes of operations, selected by the PARSE_ARG parameter.  If
9515    non-zero, the function will parse ARG, extracting location,
9516    condition, thread and extra string.  Otherwise, ARG is just the
9517    breakpoint's location, with condition, thread, and extra string
9518    specified by the COND_STRING, THREAD and EXTRA_STRING parameters.
9519    If INTERNAL is non-zero, the breakpoint number will be allocated
9520    from the internal breakpoint count.  Returns true if any breakpoint
9521    was created; false otherwise.  */
9522
9523 int
9524 create_breakpoint (struct gdbarch *gdbarch,
9525                    char *arg, char *cond_string,
9526                    int thread, char *extra_string,
9527                    int parse_arg,
9528                    int tempflag, enum bptype type_wanted,
9529                    int ignore_count,
9530                    enum auto_boolean pending_break_support,
9531                    const struct breakpoint_ops *ops,
9532                    int from_tty, int enabled, int internal,
9533                    unsigned flags)
9534 {
9535   volatile struct gdb_exception e;
9536   char *copy_arg = NULL;
9537   char *addr_start = arg;
9538   struct linespec_result canonical;
9539   struct cleanup *old_chain;
9540   struct cleanup *bkpt_chain = NULL;
9541   int pending = 0;
9542   int task = 0;
9543   int prev_bkpt_count = breakpoint_count;
9544
9545   gdb_assert (ops != NULL);
9546
9547   init_linespec_result (&canonical);
9548
9549   TRY_CATCH (e, RETURN_MASK_ALL)
9550     {
9551       ops->create_sals_from_address (&arg, &canonical, type_wanted,
9552                                      addr_start, &copy_arg);
9553     }
9554
9555   /* If caller is interested in rc value from parse, set value.  */
9556   switch (e.reason)
9557     {
9558     case GDB_NO_ERROR:
9559       if (VEC_empty (linespec_sals, canonical.sals))
9560         return 0;
9561       break;
9562     case RETURN_ERROR:
9563       switch (e.error)
9564         {
9565         case NOT_FOUND_ERROR:
9566
9567           /* If pending breakpoint support is turned off, throw
9568              error.  */
9569
9570           if (pending_break_support == AUTO_BOOLEAN_FALSE)
9571             throw_exception (e);
9572
9573           exception_print (gdb_stderr, e);
9574
9575           /* If pending breakpoint support is auto query and the user
9576              selects no, then simply return the error code.  */
9577           if (pending_break_support == AUTO_BOOLEAN_AUTO
9578               && !nquery (_("Make %s pending on future shared library load? "),
9579                           bptype_string (type_wanted)))
9580             return 0;
9581
9582           /* At this point, either the user was queried about setting
9583              a pending breakpoint and selected yes, or pending
9584              breakpoint behavior is on and thus a pending breakpoint
9585              is defaulted on behalf of the user.  */
9586           {
9587             struct linespec_sals lsal;
9588
9589             copy_arg = xstrdup (addr_start);
9590             lsal.canonical = xstrdup (copy_arg);
9591             lsal.sals.nelts = 1;
9592             lsal.sals.sals = XNEW (struct symtab_and_line);
9593             init_sal (&lsal.sals.sals[0]);
9594             pending = 1;
9595             VEC_safe_push (linespec_sals, canonical.sals, &lsal);
9596           }
9597           break;
9598         default:
9599           throw_exception (e);
9600         }
9601       break;
9602     default:
9603       throw_exception (e);
9604     }
9605
9606   /* Create a chain of things that always need to be cleaned up.  */
9607   old_chain = make_cleanup_destroy_linespec_result (&canonical);
9608
9609   /* ----------------------------- SNIP -----------------------------
9610      Anything added to the cleanup chain beyond this point is assumed
9611      to be part of a breakpoint.  If the breakpoint create succeeds
9612      then the memory is not reclaimed.  */
9613   bkpt_chain = make_cleanup (null_cleanup, 0);
9614
9615   /* Resolve all line numbers to PC's and verify that the addresses
9616      are ok for the target.  */
9617   if (!pending)
9618     {
9619       int ix;
9620       struct linespec_sals *iter;
9621
9622       for (ix = 0; VEC_iterate (linespec_sals, canonical.sals, ix, iter); ++ix)
9623         breakpoint_sals_to_pc (&iter->sals);
9624     }
9625
9626   /* Fast tracepoints may have additional restrictions on location.  */
9627   if (!pending && type_wanted == bp_fast_tracepoint)
9628     {
9629       int ix;
9630       struct linespec_sals *iter;
9631
9632       for (ix = 0; VEC_iterate (linespec_sals, canonical.sals, ix, iter); ++ix)
9633         check_fast_tracepoint_sals (gdbarch, &iter->sals);
9634     }
9635
9636   /* Verify that condition can be parsed, before setting any
9637      breakpoints.  Allocate a separate condition expression for each
9638      breakpoint.  */
9639   if (!pending)
9640     {
9641       struct linespec_sals *lsal;
9642
9643       lsal = VEC_index (linespec_sals, canonical.sals, 0);
9644
9645       if (parse_arg)
9646         {
9647             char *rest;
9648             /* Here we only parse 'arg' to separate condition
9649                from thread number, so parsing in context of first
9650                sal is OK.  When setting the breakpoint we'll 
9651                re-parse it in context of each sal.  */
9652
9653             find_condition_and_thread (arg, lsal->sals.sals[0].pc, &cond_string,
9654                                        &thread, &task, &rest);
9655             if (cond_string)
9656                 make_cleanup (xfree, cond_string);
9657             if (rest)
9658               make_cleanup (xfree, rest);
9659             if (rest)
9660               extra_string = rest;
9661         }
9662       else
9663         {
9664             if (*arg != '\0')
9665               error (_("Garbage '%s' at end of location"), arg);
9666
9667             /* Create a private copy of condition string.  */
9668             if (cond_string)
9669             {
9670                 cond_string = xstrdup (cond_string);
9671                 make_cleanup (xfree, cond_string);
9672             }
9673             /* Create a private copy of any extra string.  */
9674             if (extra_string)
9675               {
9676                 extra_string = xstrdup (extra_string);
9677                 make_cleanup (xfree, extra_string);
9678               }
9679         }
9680
9681       ops->create_breakpoints_sal (gdbarch, &canonical, lsal,
9682                                    cond_string, extra_string, type_wanted,
9683                                    tempflag ? disp_del : disp_donttouch,
9684                                    thread, task, ignore_count, ops,
9685                                    from_tty, enabled, internal, flags);
9686     }
9687   else
9688     {
9689       struct breakpoint *b;
9690
9691       make_cleanup (xfree, copy_arg);
9692
9693       if (is_tracepoint_type (type_wanted))
9694         {
9695           struct tracepoint *t;
9696
9697           t = XCNEW (struct tracepoint);
9698           b = &t->base;
9699         }
9700       else
9701         b = XNEW (struct breakpoint);
9702
9703       init_raw_breakpoint_without_location (b, gdbarch, type_wanted, ops);
9704
9705       b->addr_string = copy_arg;
9706       if (parse_arg)
9707         b->cond_string = NULL;
9708       else
9709         {
9710           /* Create a private copy of condition string.  */
9711           if (cond_string)
9712             {
9713               cond_string = xstrdup (cond_string);
9714               make_cleanup (xfree, cond_string);
9715             }
9716           b->cond_string = cond_string;
9717         }
9718       b->extra_string = NULL;
9719       b->ignore_count = ignore_count;
9720       b->disposition = tempflag ? disp_del : disp_donttouch;
9721       b->condition_not_parsed = 1;
9722       b->enable_state = enabled ? bp_enabled : bp_disabled;
9723       if ((type_wanted != bp_breakpoint
9724            && type_wanted != bp_hardware_breakpoint) || thread != -1)
9725         b->pspace = current_program_space;
9726
9727       install_breakpoint (internal, b, 0);
9728     }
9729   
9730   if (VEC_length (linespec_sals, canonical.sals) > 1)
9731     {
9732       warning (_("Multiple breakpoints were set.\nUse the "
9733                  "\"delete\" command to delete unwanted breakpoints."));
9734       prev_breakpoint_count = prev_bkpt_count;
9735     }
9736
9737   /* That's it.  Discard the cleanups for data inserted into the
9738      breakpoint.  */
9739   discard_cleanups (bkpt_chain);
9740   /* But cleanup everything else.  */
9741   do_cleanups (old_chain);
9742
9743   /* error call may happen here - have BKPT_CHAIN already discarded.  */
9744   update_global_location_list (1);
9745
9746   return 1;
9747 }
9748
9749 /* Set a breakpoint.
9750    ARG is a string describing breakpoint address,
9751    condition, and thread.
9752    FLAG specifies if a breakpoint is hardware on,
9753    and if breakpoint is temporary, using BP_HARDWARE_FLAG
9754    and BP_TEMPFLAG.  */
9755
9756 static void
9757 break_command_1 (char *arg, int flag, int from_tty)
9758 {
9759   int tempflag = flag & BP_TEMPFLAG;
9760   enum bptype type_wanted = (flag & BP_HARDWAREFLAG
9761                              ? bp_hardware_breakpoint
9762                              : bp_breakpoint);
9763   struct breakpoint_ops *ops;
9764   const char *arg_cp = arg;
9765
9766   /* Matching breakpoints on probes.  */
9767   if (arg && probe_linespec_to_ops (&arg_cp) != NULL)
9768     ops = &bkpt_probe_breakpoint_ops;
9769   else
9770     ops = &bkpt_breakpoint_ops;
9771
9772   create_breakpoint (get_current_arch (),
9773                      arg,
9774                      NULL, 0, NULL, 1 /* parse arg */,
9775                      tempflag, type_wanted,
9776                      0 /* Ignore count */,
9777                      pending_break_support,
9778                      ops,
9779                      from_tty,
9780                      1 /* enabled */,
9781                      0 /* internal */,
9782                      0);
9783 }
9784
9785 /* Helper function for break_command_1 and disassemble_command.  */
9786
9787 void
9788 resolve_sal_pc (struct symtab_and_line *sal)
9789 {
9790   CORE_ADDR pc;
9791
9792   if (sal->pc == 0 && sal->symtab != NULL)
9793     {
9794       if (!find_line_pc (sal->symtab, sal->line, &pc))
9795         error (_("No line %d in file \"%s\"."),
9796                sal->line, symtab_to_filename_for_display (sal->symtab));
9797       sal->pc = pc;
9798
9799       /* If this SAL corresponds to a breakpoint inserted using a line
9800          number, then skip the function prologue if necessary.  */
9801       if (sal->explicit_line)
9802         skip_prologue_sal (sal);
9803     }
9804
9805   if (sal->section == 0 && sal->symtab != NULL)
9806     {
9807       struct blockvector *bv;
9808       struct block *b;
9809       struct symbol *sym;
9810
9811       bv = blockvector_for_pc_sect (sal->pc, 0, &b, sal->symtab);
9812       if (bv != NULL)
9813         {
9814           sym = block_linkage_function (b);
9815           if (sym != NULL)
9816             {
9817               fixup_symbol_section (sym, sal->symtab->objfile);
9818               sal->section = SYMBOL_OBJ_SECTION (sal->symtab->objfile, sym);
9819             }
9820           else
9821             {
9822               /* It really is worthwhile to have the section, so we'll
9823                  just have to look harder. This case can be executed
9824                  if we have line numbers but no functions (as can
9825                  happen in assembly source).  */
9826
9827               struct bound_minimal_symbol msym;
9828               struct cleanup *old_chain = save_current_space_and_thread ();
9829
9830               switch_to_program_space_and_thread (sal->pspace);
9831
9832               msym = lookup_minimal_symbol_by_pc (sal->pc);
9833               if (msym.minsym)
9834                 sal->section = SYMBOL_OBJ_SECTION (msym.objfile, msym.minsym);
9835
9836               do_cleanups (old_chain);
9837             }
9838         }
9839     }
9840 }
9841
9842 void
9843 break_command (char *arg, int from_tty)
9844 {
9845   break_command_1 (arg, 0, from_tty);
9846 }
9847
9848 void
9849 tbreak_command (char *arg, int from_tty)
9850 {
9851   break_command_1 (arg, BP_TEMPFLAG, from_tty);
9852 }
9853
9854 static void
9855 hbreak_command (char *arg, int from_tty)
9856 {
9857   break_command_1 (arg, BP_HARDWAREFLAG, from_tty);
9858 }
9859
9860 static void
9861 thbreak_command (char *arg, int from_tty)
9862 {
9863   break_command_1 (arg, (BP_TEMPFLAG | BP_HARDWAREFLAG), from_tty);
9864 }
9865
9866 static void
9867 stop_command (char *arg, int from_tty)
9868 {
9869   printf_filtered (_("Specify the type of breakpoint to set.\n\
9870 Usage: stop in <function | address>\n\
9871        stop at <line>\n"));
9872 }
9873
9874 static void
9875 stopin_command (char *arg, int from_tty)
9876 {
9877   int badInput = 0;
9878
9879   if (arg == (char *) NULL)
9880     badInput = 1;
9881   else if (*arg != '*')
9882     {
9883       char *argptr = arg;
9884       int hasColon = 0;
9885
9886       /* Look for a ':'.  If this is a line number specification, then
9887          say it is bad, otherwise, it should be an address or
9888          function/method name.  */
9889       while (*argptr && !hasColon)
9890         {
9891           hasColon = (*argptr == ':');
9892           argptr++;
9893         }
9894
9895       if (hasColon)
9896         badInput = (*argptr != ':');    /* Not a class::method */
9897       else
9898         badInput = isdigit (*arg);      /* a simple line number */
9899     }
9900
9901   if (badInput)
9902     printf_filtered (_("Usage: stop in <function | address>\n"));
9903   else
9904     break_command_1 (arg, 0, from_tty);
9905 }
9906
9907 static void
9908 stopat_command (char *arg, int from_tty)
9909 {
9910   int badInput = 0;
9911
9912   if (arg == (char *) NULL || *arg == '*')      /* no line number */
9913     badInput = 1;
9914   else
9915     {
9916       char *argptr = arg;
9917       int hasColon = 0;
9918
9919       /* Look for a ':'.  If there is a '::' then get out, otherwise
9920          it is probably a line number.  */
9921       while (*argptr && !hasColon)
9922         {
9923           hasColon = (*argptr == ':');
9924           argptr++;
9925         }
9926
9927       if (hasColon)
9928         badInput = (*argptr == ':');    /* we have class::method */
9929       else
9930         badInput = !isdigit (*arg);     /* not a line number */
9931     }
9932
9933   if (badInput)
9934     printf_filtered (_("Usage: stop at <line>\n"));
9935   else
9936     break_command_1 (arg, 0, from_tty);
9937 }
9938
9939 /* The dynamic printf command is mostly like a regular breakpoint, but
9940    with a prewired command list consisting of a single output command,
9941    built from extra arguments supplied on the dprintf command
9942    line.  */
9943
9944 static void
9945 dprintf_command (char *arg, int from_tty)
9946 {
9947   create_breakpoint (get_current_arch (),
9948                      arg,
9949                      NULL, 0, NULL, 1 /* parse arg */,
9950                      0, bp_dprintf,
9951                      0 /* Ignore count */,
9952                      pending_break_support,
9953                      &dprintf_breakpoint_ops,
9954                      from_tty,
9955                      1 /* enabled */,
9956                      0 /* internal */,
9957                      0);
9958 }
9959
9960 static void
9961 agent_printf_command (char *arg, int from_tty)
9962 {
9963   error (_("May only run agent-printf on the target"));
9964 }
9965
9966 /* Implement the "breakpoint_hit" breakpoint_ops method for
9967    ranged breakpoints.  */
9968
9969 static int
9970 breakpoint_hit_ranged_breakpoint (const struct bp_location *bl,
9971                                   struct address_space *aspace,
9972                                   CORE_ADDR bp_addr,
9973                                   const struct target_waitstatus *ws)
9974 {
9975   if (ws->kind != TARGET_WAITKIND_STOPPED
9976       || ws->value.sig != GDB_SIGNAL_TRAP)
9977     return 0;
9978
9979   return breakpoint_address_match_range (bl->pspace->aspace, bl->address,
9980                                          bl->length, aspace, bp_addr);
9981 }
9982
9983 /* Implement the "resources_needed" breakpoint_ops method for
9984    ranged breakpoints.  */
9985
9986 static int
9987 resources_needed_ranged_breakpoint (const struct bp_location *bl)
9988 {
9989   return target_ranged_break_num_registers ();
9990 }
9991
9992 /* Implement the "print_it" breakpoint_ops method for
9993    ranged breakpoints.  */
9994
9995 static enum print_stop_action
9996 print_it_ranged_breakpoint (bpstat bs)
9997 {
9998   struct breakpoint *b = bs->breakpoint_at;
9999   struct bp_location *bl = b->loc;
10000   struct ui_out *uiout = current_uiout;
10001
10002   gdb_assert (b->type == bp_hardware_breakpoint);
10003
10004   /* Ranged breakpoints have only one location.  */
10005   gdb_assert (bl && bl->next == NULL);
10006
10007   annotate_breakpoint (b->number);
10008   if (b->disposition == disp_del)
10009     ui_out_text (uiout, "\nTemporary ranged breakpoint ");
10010   else
10011     ui_out_text (uiout, "\nRanged breakpoint ");
10012   if (ui_out_is_mi_like_p (uiout))
10013     {
10014       ui_out_field_string (uiout, "reason",
10015                       async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT));
10016       ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
10017     }
10018   ui_out_field_int (uiout, "bkptno", b->number);
10019   ui_out_text (uiout, ", ");
10020
10021   return PRINT_SRC_AND_LOC;
10022 }
10023
10024 /* Implement the "print_one" breakpoint_ops method for
10025    ranged breakpoints.  */
10026
10027 static void
10028 print_one_ranged_breakpoint (struct breakpoint *b,
10029                              struct bp_location **last_loc)
10030 {
10031   struct bp_location *bl = b->loc;
10032   struct value_print_options opts;
10033   struct ui_out *uiout = current_uiout;
10034
10035   /* Ranged breakpoints have only one location.  */
10036   gdb_assert (bl && bl->next == NULL);
10037
10038   get_user_print_options (&opts);
10039
10040   if (opts.addressprint)
10041     /* We don't print the address range here, it will be printed later
10042        by print_one_detail_ranged_breakpoint.  */
10043     ui_out_field_skip (uiout, "addr");
10044   annotate_field (5);
10045   print_breakpoint_location (b, bl);
10046   *last_loc = bl;
10047 }
10048
10049 /* Implement the "print_one_detail" breakpoint_ops method for
10050    ranged breakpoints.  */
10051
10052 static void
10053 print_one_detail_ranged_breakpoint (const struct breakpoint *b,
10054                                     struct ui_out *uiout)
10055 {
10056   CORE_ADDR address_start, address_end;
10057   struct bp_location *bl = b->loc;
10058   struct ui_file *stb = mem_fileopen ();
10059   struct cleanup *cleanup = make_cleanup_ui_file_delete (stb);
10060
10061   gdb_assert (bl);
10062
10063   address_start = bl->address;
10064   address_end = address_start + bl->length - 1;
10065
10066   ui_out_text (uiout, "\taddress range: ");
10067   fprintf_unfiltered (stb, "[%s, %s]",
10068                       print_core_address (bl->gdbarch, address_start),
10069                       print_core_address (bl->gdbarch, address_end));
10070   ui_out_field_stream (uiout, "addr", stb);
10071   ui_out_text (uiout, "\n");
10072
10073   do_cleanups (cleanup);
10074 }
10075
10076 /* Implement the "print_mention" breakpoint_ops method for
10077    ranged breakpoints.  */
10078
10079 static void
10080 print_mention_ranged_breakpoint (struct breakpoint *b)
10081 {
10082   struct bp_location *bl = b->loc;
10083   struct ui_out *uiout = current_uiout;
10084
10085   gdb_assert (bl);
10086   gdb_assert (b->type == bp_hardware_breakpoint);
10087
10088   if (ui_out_is_mi_like_p (uiout))
10089     return;
10090
10091   printf_filtered (_("Hardware assisted ranged breakpoint %d from %s to %s."),
10092                    b->number, paddress (bl->gdbarch, bl->address),
10093                    paddress (bl->gdbarch, bl->address + bl->length - 1));
10094 }
10095
10096 /* Implement the "print_recreate" breakpoint_ops method for
10097    ranged breakpoints.  */
10098
10099 static void
10100 print_recreate_ranged_breakpoint (struct breakpoint *b, struct ui_file *fp)
10101 {
10102   fprintf_unfiltered (fp, "break-range %s, %s", b->addr_string,
10103                       b->addr_string_range_end);
10104   print_recreate_thread (b, fp);
10105 }
10106
10107 /* The breakpoint_ops structure to be used in ranged breakpoints.  */
10108
10109 static struct breakpoint_ops ranged_breakpoint_ops;
10110
10111 /* Find the address where the end of the breakpoint range should be
10112    placed, given the SAL of the end of the range.  This is so that if
10113    the user provides a line number, the end of the range is set to the
10114    last instruction of the given line.  */
10115
10116 static CORE_ADDR
10117 find_breakpoint_range_end (struct symtab_and_line sal)
10118 {
10119   CORE_ADDR end;
10120
10121   /* If the user provided a PC value, use it.  Otherwise,
10122      find the address of the end of the given location.  */
10123   if (sal.explicit_pc)
10124     end = sal.pc;
10125   else
10126     {
10127       int ret;
10128       CORE_ADDR start;
10129
10130       ret = find_line_pc_range (sal, &start, &end);
10131       if (!ret)
10132         error (_("Could not find location of the end of the range."));
10133
10134       /* find_line_pc_range returns the start of the next line.  */
10135       end--;
10136     }
10137
10138   return end;
10139 }
10140
10141 /* Implement the "break-range" CLI command.  */
10142
10143 static void
10144 break_range_command (char *arg, int from_tty)
10145 {
10146   char *arg_start, *addr_string_start, *addr_string_end;
10147   struct linespec_result canonical_start, canonical_end;
10148   int bp_count, can_use_bp, length;
10149   CORE_ADDR end;
10150   struct breakpoint *b;
10151   struct symtab_and_line sal_start, sal_end;
10152   struct cleanup *cleanup_bkpt;
10153   struct linespec_sals *lsal_start, *lsal_end;
10154
10155   /* We don't support software ranged breakpoints.  */
10156   if (target_ranged_break_num_registers () < 0)
10157     error (_("This target does not support hardware ranged breakpoints."));
10158
10159   bp_count = hw_breakpoint_used_count ();
10160   bp_count += target_ranged_break_num_registers ();
10161   can_use_bp = target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
10162                                                    bp_count, 0);
10163   if (can_use_bp < 0)
10164     error (_("Hardware breakpoints used exceeds limit."));
10165
10166   arg = skip_spaces (arg);
10167   if (arg == NULL || arg[0] == '\0')
10168     error(_("No address range specified."));
10169
10170   init_linespec_result (&canonical_start);
10171
10172   arg_start = arg;
10173   parse_breakpoint_sals (&arg, &canonical_start);
10174
10175   cleanup_bkpt = make_cleanup_destroy_linespec_result (&canonical_start);
10176
10177   if (arg[0] != ',')
10178     error (_("Too few arguments."));
10179   else if (VEC_empty (linespec_sals, canonical_start.sals))
10180     error (_("Could not find location of the beginning of the range."));
10181
10182   lsal_start = VEC_index (linespec_sals, canonical_start.sals, 0);
10183
10184   if (VEC_length (linespec_sals, canonical_start.sals) > 1
10185       || lsal_start->sals.nelts != 1)
10186     error (_("Cannot create a ranged breakpoint with multiple locations."));
10187
10188   sal_start = lsal_start->sals.sals[0];
10189   addr_string_start = savestring (arg_start, arg - arg_start);
10190   make_cleanup (xfree, addr_string_start);
10191
10192   arg++;        /* Skip the comma.  */
10193   arg = skip_spaces (arg);
10194
10195   /* Parse the end location.  */
10196
10197   init_linespec_result (&canonical_end);
10198   arg_start = arg;
10199
10200   /* We call decode_line_full directly here instead of using
10201      parse_breakpoint_sals because we need to specify the start location's
10202      symtab and line as the default symtab and line for the end of the
10203      range.  This makes it possible to have ranges like "foo.c:27, +14",
10204      where +14 means 14 lines from the start location.  */
10205   decode_line_full (&arg, DECODE_LINE_FUNFIRSTLINE,
10206                     sal_start.symtab, sal_start.line,
10207                     &canonical_end, NULL, NULL);
10208
10209   make_cleanup_destroy_linespec_result (&canonical_end);
10210
10211   if (VEC_empty (linespec_sals, canonical_end.sals))
10212     error (_("Could not find location of the end of the range."));
10213
10214   lsal_end = VEC_index (linespec_sals, canonical_end.sals, 0);
10215   if (VEC_length (linespec_sals, canonical_end.sals) > 1
10216       || lsal_end->sals.nelts != 1)
10217     error (_("Cannot create a ranged breakpoint with multiple locations."));
10218
10219   sal_end = lsal_end->sals.sals[0];
10220   addr_string_end = savestring (arg_start, arg - arg_start);
10221   make_cleanup (xfree, addr_string_end);
10222
10223   end = find_breakpoint_range_end (sal_end);
10224   if (sal_start.pc > end)
10225     error (_("Invalid address range, end precedes start."));
10226
10227   length = end - sal_start.pc + 1;
10228   if (length < 0)
10229     /* Length overflowed.  */
10230     error (_("Address range too large."));
10231   else if (length == 1)
10232     {
10233       /* This range is simple enough to be handled by
10234          the `hbreak' command.  */
10235       hbreak_command (addr_string_start, 1);
10236
10237       do_cleanups (cleanup_bkpt);
10238
10239       return;
10240     }
10241
10242   /* Now set up the breakpoint.  */
10243   b = set_raw_breakpoint (get_current_arch (), sal_start,
10244                           bp_hardware_breakpoint, &ranged_breakpoint_ops);
10245   set_breakpoint_count (breakpoint_count + 1);
10246   b->number = breakpoint_count;
10247   b->disposition = disp_donttouch;
10248   b->addr_string = xstrdup (addr_string_start);
10249   b->addr_string_range_end = xstrdup (addr_string_end);
10250   b->loc->length = length;
10251
10252   do_cleanups (cleanup_bkpt);
10253
10254   mention (b);
10255   observer_notify_breakpoint_created (b);
10256   update_global_location_list (1);
10257 }
10258
10259 /*  Return non-zero if EXP is verified as constant.  Returned zero
10260     means EXP is variable.  Also the constant detection may fail for
10261     some constant expressions and in such case still falsely return
10262     zero.  */
10263
10264 static int
10265 watchpoint_exp_is_const (const struct expression *exp)
10266 {
10267   int i = exp->nelts;
10268
10269   while (i > 0)
10270     {
10271       int oplenp, argsp;
10272
10273       /* We are only interested in the descriptor of each element.  */
10274       operator_length (exp, i, &oplenp, &argsp);
10275       i -= oplenp;
10276
10277       switch (exp->elts[i].opcode)
10278         {
10279         case BINOP_ADD:
10280         case BINOP_SUB:
10281         case BINOP_MUL:
10282         case BINOP_DIV:
10283         case BINOP_REM:
10284         case BINOP_MOD:
10285         case BINOP_LSH:
10286         case BINOP_RSH:
10287         case BINOP_LOGICAL_AND:
10288         case BINOP_LOGICAL_OR:
10289         case BINOP_BITWISE_AND:
10290         case BINOP_BITWISE_IOR:
10291         case BINOP_BITWISE_XOR:
10292         case BINOP_EQUAL:
10293         case BINOP_NOTEQUAL:
10294         case BINOP_LESS:
10295         case BINOP_GTR:
10296         case BINOP_LEQ:
10297         case BINOP_GEQ:
10298         case BINOP_REPEAT:
10299         case BINOP_COMMA:
10300         case BINOP_EXP:
10301         case BINOP_MIN:
10302         case BINOP_MAX:
10303         case BINOP_INTDIV:
10304         case BINOP_CONCAT:
10305         case BINOP_IN:
10306         case BINOP_RANGE:
10307         case TERNOP_COND:
10308         case TERNOP_SLICE:
10309
10310         case OP_LONG:
10311         case OP_DOUBLE:
10312         case OP_DECFLOAT:
10313         case OP_LAST:
10314         case OP_COMPLEX:
10315         case OP_STRING:
10316         case OP_ARRAY:
10317         case OP_TYPE:
10318         case OP_TYPEOF:
10319         case OP_DECLTYPE:
10320         case OP_TYPEID:
10321         case OP_NAME:
10322         case OP_OBJC_NSSTRING:
10323
10324         case UNOP_NEG:
10325         case UNOP_LOGICAL_NOT:
10326         case UNOP_COMPLEMENT:
10327         case UNOP_ADDR:
10328         case UNOP_HIGH:
10329         case UNOP_CAST:
10330
10331         case UNOP_CAST_TYPE:
10332         case UNOP_REINTERPRET_CAST:
10333         case UNOP_DYNAMIC_CAST:
10334           /* Unary, binary and ternary operators: We have to check
10335              their operands.  If they are constant, then so is the
10336              result of that operation.  For instance, if A and B are
10337              determined to be constants, then so is "A + B".
10338
10339              UNOP_IND is one exception to the rule above, because the
10340              value of *ADDR is not necessarily a constant, even when
10341              ADDR is.  */
10342           break;
10343
10344         case OP_VAR_VALUE:
10345           /* Check whether the associated symbol is a constant.
10346
10347              We use SYMBOL_CLASS rather than TYPE_CONST because it's
10348              possible that a buggy compiler could mark a variable as
10349              constant even when it is not, and TYPE_CONST would return
10350              true in this case, while SYMBOL_CLASS wouldn't.
10351
10352              We also have to check for function symbols because they
10353              are always constant.  */
10354           {
10355             struct symbol *s = exp->elts[i + 2].symbol;
10356
10357             if (SYMBOL_CLASS (s) != LOC_BLOCK
10358                 && SYMBOL_CLASS (s) != LOC_CONST
10359                 && SYMBOL_CLASS (s) != LOC_CONST_BYTES)
10360               return 0;
10361             break;
10362           }
10363
10364         /* The default action is to return 0 because we are using
10365            the optimistic approach here: If we don't know something,
10366            then it is not a constant.  */
10367         default:
10368           return 0;
10369         }
10370     }
10371
10372   return 1;
10373 }
10374
10375 /* Implement the "dtor" breakpoint_ops method for watchpoints.  */
10376
10377 static void
10378 dtor_watchpoint (struct breakpoint *self)
10379 {
10380   struct watchpoint *w = (struct watchpoint *) self;
10381
10382   xfree (w->cond_exp);
10383   xfree (w->exp);
10384   xfree (w->exp_string);
10385   xfree (w->exp_string_reparse);
10386   value_free (w->val);
10387
10388   base_breakpoint_ops.dtor (self);
10389 }
10390
10391 /* Implement the "re_set" breakpoint_ops method for watchpoints.  */
10392
10393 static void
10394 re_set_watchpoint (struct breakpoint *b)
10395 {
10396   struct watchpoint *w = (struct watchpoint *) b;
10397
10398   /* Watchpoint can be either on expression using entirely global
10399      variables, or it can be on local variables.
10400
10401      Watchpoints of the first kind are never auto-deleted, and even
10402      persist across program restarts.  Since they can use variables
10403      from shared libraries, we need to reparse expression as libraries
10404      are loaded and unloaded.
10405
10406      Watchpoints on local variables can also change meaning as result
10407      of solib event.  For example, if a watchpoint uses both a local
10408      and a global variables in expression, it's a local watchpoint,
10409      but unloading of a shared library will make the expression
10410      invalid.  This is not a very common use case, but we still
10411      re-evaluate expression, to avoid surprises to the user.
10412
10413      Note that for local watchpoints, we re-evaluate it only if
10414      watchpoints frame id is still valid.  If it's not, it means the
10415      watchpoint is out of scope and will be deleted soon.  In fact,
10416      I'm not sure we'll ever be called in this case.
10417
10418      If a local watchpoint's frame id is still valid, then
10419      w->exp_valid_block is likewise valid, and we can safely use it.
10420
10421      Don't do anything about disabled watchpoints, since they will be
10422      reevaluated again when enabled.  */
10423   update_watchpoint (w, 1 /* reparse */);
10424 }
10425
10426 /* Implement the "insert" breakpoint_ops method for hardware watchpoints.  */
10427
10428 static int
10429 insert_watchpoint (struct bp_location *bl)
10430 {
10431   struct watchpoint *w = (struct watchpoint *) bl->owner;
10432   int length = w->exact ? 1 : bl->length;
10433
10434   return target_insert_watchpoint (bl->address, length, bl->watchpoint_type,
10435                                    w->cond_exp);
10436 }
10437
10438 /* Implement the "remove" breakpoint_ops method for hardware watchpoints.  */
10439
10440 static int
10441 remove_watchpoint (struct bp_location *bl)
10442 {
10443   struct watchpoint *w = (struct watchpoint *) bl->owner;
10444   int length = w->exact ? 1 : bl->length;
10445
10446   return target_remove_watchpoint (bl->address, length, bl->watchpoint_type,
10447                                    w->cond_exp);
10448 }
10449
10450 static int
10451 breakpoint_hit_watchpoint (const struct bp_location *bl,
10452                            struct address_space *aspace, CORE_ADDR bp_addr,
10453                            const struct target_waitstatus *ws)
10454 {
10455   struct breakpoint *b = bl->owner;
10456   struct watchpoint *w = (struct watchpoint *) b;
10457
10458   /* Continuable hardware watchpoints are treated as non-existent if the
10459      reason we stopped wasn't a hardware watchpoint (we didn't stop on
10460      some data address).  Otherwise gdb won't stop on a break instruction
10461      in the code (not from a breakpoint) when a hardware watchpoint has
10462      been defined.  Also skip watchpoints which we know did not trigger
10463      (did not match the data address).  */
10464   if (is_hardware_watchpoint (b)
10465       && w->watchpoint_triggered == watch_triggered_no)
10466     return 0;
10467
10468   return 1;
10469 }
10470
10471 static void
10472 check_status_watchpoint (bpstat bs)
10473 {
10474   gdb_assert (is_watchpoint (bs->breakpoint_at));
10475
10476   bpstat_check_watchpoint (bs);
10477 }
10478
10479 /* Implement the "resources_needed" breakpoint_ops method for
10480    hardware watchpoints.  */
10481
10482 static int
10483 resources_needed_watchpoint (const struct bp_location *bl)
10484 {
10485   struct watchpoint *w = (struct watchpoint *) bl->owner;
10486   int length = w->exact? 1 : bl->length;
10487
10488   return target_region_ok_for_hw_watchpoint (bl->address, length);
10489 }
10490
10491 /* Implement the "works_in_software_mode" breakpoint_ops method for
10492    hardware watchpoints.  */
10493
10494 static int
10495 works_in_software_mode_watchpoint (const struct breakpoint *b)
10496 {
10497   /* Read and access watchpoints only work with hardware support.  */
10498   return b->type == bp_watchpoint || b->type == bp_hardware_watchpoint;
10499 }
10500
10501 static enum print_stop_action
10502 print_it_watchpoint (bpstat bs)
10503 {
10504   struct cleanup *old_chain;
10505   struct breakpoint *b;
10506   struct ui_file *stb;
10507   enum print_stop_action result;
10508   struct watchpoint *w;
10509   struct ui_out *uiout = current_uiout;
10510
10511   gdb_assert (bs->bp_location_at != NULL);
10512
10513   b = bs->breakpoint_at;
10514   w = (struct watchpoint *) b;
10515
10516   stb = mem_fileopen ();
10517   old_chain = make_cleanup_ui_file_delete (stb);
10518
10519   switch (b->type)
10520     {
10521     case bp_watchpoint:
10522     case bp_hardware_watchpoint:
10523       annotate_watchpoint (b->number);
10524       if (ui_out_is_mi_like_p (uiout))
10525         ui_out_field_string
10526           (uiout, "reason",
10527            async_reason_lookup (EXEC_ASYNC_WATCHPOINT_TRIGGER));
10528       mention (b);
10529       make_cleanup_ui_out_tuple_begin_end (uiout, "value");
10530       ui_out_text (uiout, "\nOld value = ");
10531       watchpoint_value_print (bs->old_val, stb);
10532       ui_out_field_stream (uiout, "old", stb);
10533       ui_out_text (uiout, "\nNew value = ");
10534       watchpoint_value_print (w->val, stb);
10535       ui_out_field_stream (uiout, "new", stb);
10536       ui_out_text (uiout, "\n");
10537       /* More than one watchpoint may have been triggered.  */
10538       result = PRINT_UNKNOWN;
10539       break;
10540
10541     case bp_read_watchpoint:
10542       if (ui_out_is_mi_like_p (uiout))
10543         ui_out_field_string
10544           (uiout, "reason",
10545            async_reason_lookup (EXEC_ASYNC_READ_WATCHPOINT_TRIGGER));
10546       mention (b);
10547       make_cleanup_ui_out_tuple_begin_end (uiout, "value");
10548       ui_out_text (uiout, "\nValue = ");
10549       watchpoint_value_print (w->val, stb);
10550       ui_out_field_stream (uiout, "value", stb);
10551       ui_out_text (uiout, "\n");
10552       result = PRINT_UNKNOWN;
10553       break;
10554
10555     case bp_access_watchpoint:
10556       if (bs->old_val != NULL)
10557         {
10558           annotate_watchpoint (b->number);
10559           if (ui_out_is_mi_like_p (uiout))
10560             ui_out_field_string
10561               (uiout, "reason",
10562                async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
10563           mention (b);
10564           make_cleanup_ui_out_tuple_begin_end (uiout, "value");
10565           ui_out_text (uiout, "\nOld value = ");
10566           watchpoint_value_print (bs->old_val, stb);
10567           ui_out_field_stream (uiout, "old", stb);
10568           ui_out_text (uiout, "\nNew value = ");
10569         }
10570       else
10571         {
10572           mention (b);
10573           if (ui_out_is_mi_like_p (uiout))
10574             ui_out_field_string
10575               (uiout, "reason",
10576                async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
10577           make_cleanup_ui_out_tuple_begin_end (uiout, "value");
10578           ui_out_text (uiout, "\nValue = ");
10579         }
10580       watchpoint_value_print (w->val, stb);
10581       ui_out_field_stream (uiout, "new", stb);
10582       ui_out_text (uiout, "\n");
10583       result = PRINT_UNKNOWN;
10584       break;
10585     default:
10586       result = PRINT_UNKNOWN;
10587     }
10588
10589   do_cleanups (old_chain);
10590   return result;
10591 }
10592
10593 /* Implement the "print_mention" breakpoint_ops method for hardware
10594    watchpoints.  */
10595
10596 static void
10597 print_mention_watchpoint (struct breakpoint *b)
10598 {
10599   struct cleanup *ui_out_chain;
10600   struct watchpoint *w = (struct watchpoint *) b;
10601   struct ui_out *uiout = current_uiout;
10602
10603   switch (b->type)
10604     {
10605     case bp_watchpoint:
10606       ui_out_text (uiout, "Watchpoint ");
10607       ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "wpt");
10608       break;
10609     case bp_hardware_watchpoint:
10610       ui_out_text (uiout, "Hardware watchpoint ");
10611       ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "wpt");
10612       break;
10613     case bp_read_watchpoint:
10614       ui_out_text (uiout, "Hardware read watchpoint ");
10615       ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "hw-rwpt");
10616       break;
10617     case bp_access_watchpoint:
10618       ui_out_text (uiout, "Hardware access (read/write) watchpoint ");
10619       ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "hw-awpt");
10620       break;
10621     default:
10622       internal_error (__FILE__, __LINE__,
10623                       _("Invalid hardware watchpoint type."));
10624     }
10625
10626   ui_out_field_int (uiout, "number", b->number);
10627   ui_out_text (uiout, ": ");
10628   ui_out_field_string (uiout, "exp", w->exp_string);
10629   do_cleanups (ui_out_chain);
10630 }
10631
10632 /* Implement the "print_recreate" breakpoint_ops method for
10633    watchpoints.  */
10634
10635 static void
10636 print_recreate_watchpoint (struct breakpoint *b, struct ui_file *fp)
10637 {
10638   struct watchpoint *w = (struct watchpoint *) b;
10639
10640   switch (b->type)
10641     {
10642     case bp_watchpoint:
10643     case bp_hardware_watchpoint:
10644       fprintf_unfiltered (fp, "watch");
10645       break;
10646     case bp_read_watchpoint:
10647       fprintf_unfiltered (fp, "rwatch");
10648       break;
10649     case bp_access_watchpoint:
10650       fprintf_unfiltered (fp, "awatch");
10651       break;
10652     default:
10653       internal_error (__FILE__, __LINE__,
10654                       _("Invalid watchpoint type."));
10655     }
10656
10657   fprintf_unfiltered (fp, " %s", w->exp_string);
10658   print_recreate_thread (b, fp);
10659 }
10660
10661 /* The breakpoint_ops structure to be used in hardware watchpoints.  */
10662
10663 static struct breakpoint_ops watchpoint_breakpoint_ops;
10664
10665 /* Implement the "insert" breakpoint_ops method for
10666    masked hardware watchpoints.  */
10667
10668 static int
10669 insert_masked_watchpoint (struct bp_location *bl)
10670 {
10671   struct watchpoint *w = (struct watchpoint *) bl->owner;
10672
10673   return target_insert_mask_watchpoint (bl->address, w->hw_wp_mask,
10674                                         bl->watchpoint_type);
10675 }
10676
10677 /* Implement the "remove" breakpoint_ops method for
10678    masked hardware watchpoints.  */
10679
10680 static int
10681 remove_masked_watchpoint (struct bp_location *bl)
10682 {
10683   struct watchpoint *w = (struct watchpoint *) bl->owner;
10684
10685   return target_remove_mask_watchpoint (bl->address, w->hw_wp_mask,
10686                                         bl->watchpoint_type);
10687 }
10688
10689 /* Implement the "resources_needed" breakpoint_ops method for
10690    masked hardware watchpoints.  */
10691
10692 static int
10693 resources_needed_masked_watchpoint (const struct bp_location *bl)
10694 {
10695   struct watchpoint *w = (struct watchpoint *) bl->owner;
10696
10697   return target_masked_watch_num_registers (bl->address, w->hw_wp_mask);
10698 }
10699
10700 /* Implement the "works_in_software_mode" breakpoint_ops method for
10701    masked hardware watchpoints.  */
10702
10703 static int
10704 works_in_software_mode_masked_watchpoint (const struct breakpoint *b)
10705 {
10706   return 0;
10707 }
10708
10709 /* Implement the "print_it" breakpoint_ops method for
10710    masked hardware watchpoints.  */
10711
10712 static enum print_stop_action
10713 print_it_masked_watchpoint (bpstat bs)
10714 {
10715   struct breakpoint *b = bs->breakpoint_at;
10716   struct ui_out *uiout = current_uiout;
10717
10718   /* Masked watchpoints have only one location.  */
10719   gdb_assert (b->loc && b->loc->next == NULL);
10720
10721   switch (b->type)
10722     {
10723     case bp_hardware_watchpoint:
10724       annotate_watchpoint (b->number);
10725       if (ui_out_is_mi_like_p (uiout))
10726         ui_out_field_string
10727           (uiout, "reason",
10728            async_reason_lookup (EXEC_ASYNC_WATCHPOINT_TRIGGER));
10729       break;
10730
10731     case bp_read_watchpoint:
10732       if (ui_out_is_mi_like_p (uiout))
10733         ui_out_field_string
10734           (uiout, "reason",
10735            async_reason_lookup (EXEC_ASYNC_READ_WATCHPOINT_TRIGGER));
10736       break;
10737
10738     case bp_access_watchpoint:
10739       if (ui_out_is_mi_like_p (uiout))
10740         ui_out_field_string
10741           (uiout, "reason",
10742            async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
10743       break;
10744     default:
10745       internal_error (__FILE__, __LINE__,
10746                       _("Invalid hardware watchpoint type."));
10747     }
10748
10749   mention (b);
10750   ui_out_text (uiout, _("\n\
10751 Check the underlying instruction at PC for the memory\n\
10752 address and value which triggered this watchpoint.\n"));
10753   ui_out_text (uiout, "\n");
10754
10755   /* More than one watchpoint may have been triggered.  */
10756   return PRINT_UNKNOWN;
10757 }
10758
10759 /* Implement the "print_one_detail" breakpoint_ops method for
10760    masked hardware watchpoints.  */
10761
10762 static void
10763 print_one_detail_masked_watchpoint (const struct breakpoint *b,
10764                                     struct ui_out *uiout)
10765 {
10766   struct watchpoint *w = (struct watchpoint *) b;
10767
10768   /* Masked watchpoints have only one location.  */
10769   gdb_assert (b->loc && b->loc->next == NULL);
10770
10771   ui_out_text (uiout, "\tmask ");
10772   ui_out_field_core_addr (uiout, "mask", b->loc->gdbarch, w->hw_wp_mask);
10773   ui_out_text (uiout, "\n");
10774 }
10775
10776 /* Implement the "print_mention" breakpoint_ops method for
10777    masked hardware watchpoints.  */
10778
10779 static void
10780 print_mention_masked_watchpoint (struct breakpoint *b)
10781 {
10782   struct watchpoint *w = (struct watchpoint *) b;
10783   struct ui_out *uiout = current_uiout;
10784   struct cleanup *ui_out_chain;
10785
10786   switch (b->type)
10787     {
10788     case bp_hardware_watchpoint:
10789       ui_out_text (uiout, "Masked hardware watchpoint ");
10790       ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "wpt");
10791       break;
10792     case bp_read_watchpoint:
10793       ui_out_text (uiout, "Masked hardware read watchpoint ");
10794       ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "hw-rwpt");
10795       break;
10796     case bp_access_watchpoint:
10797       ui_out_text (uiout, "Masked hardware access (read/write) watchpoint ");
10798       ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "hw-awpt");
10799       break;
10800     default:
10801       internal_error (__FILE__, __LINE__,
10802                       _("Invalid hardware watchpoint type."));
10803     }
10804
10805   ui_out_field_int (uiout, "number", b->number);
10806   ui_out_text (uiout, ": ");
10807   ui_out_field_string (uiout, "exp", w->exp_string);
10808   do_cleanups (ui_out_chain);
10809 }
10810
10811 /* Implement the "print_recreate" breakpoint_ops method for
10812    masked hardware watchpoints.  */
10813
10814 static void
10815 print_recreate_masked_watchpoint (struct breakpoint *b, struct ui_file *fp)
10816 {
10817   struct watchpoint *w = (struct watchpoint *) b;
10818   char tmp[40];
10819
10820   switch (b->type)
10821     {
10822     case bp_hardware_watchpoint:
10823       fprintf_unfiltered (fp, "watch");
10824       break;
10825     case bp_read_watchpoint:
10826       fprintf_unfiltered (fp, "rwatch");
10827       break;
10828     case bp_access_watchpoint:
10829       fprintf_unfiltered (fp, "awatch");
10830       break;
10831     default:
10832       internal_error (__FILE__, __LINE__,
10833                       _("Invalid hardware watchpoint type."));
10834     }
10835
10836   sprintf_vma (tmp, w->hw_wp_mask);
10837   fprintf_unfiltered (fp, " %s mask 0x%s", w->exp_string, tmp);
10838   print_recreate_thread (b, fp);
10839 }
10840
10841 /* The breakpoint_ops structure to be used in masked hardware watchpoints.  */
10842
10843 static struct breakpoint_ops masked_watchpoint_breakpoint_ops;
10844
10845 /* Tell whether the given watchpoint is a masked hardware watchpoint.  */
10846
10847 static int
10848 is_masked_watchpoint (const struct breakpoint *b)
10849 {
10850   return b->ops == &masked_watchpoint_breakpoint_ops;
10851 }
10852
10853 /* accessflag:  hw_write:  watch write, 
10854                 hw_read:   watch read, 
10855                 hw_access: watch access (read or write) */
10856 static void
10857 watch_command_1 (const char *arg, int accessflag, int from_tty,
10858                  int just_location, int internal)
10859 {
10860   volatile struct gdb_exception e;
10861   struct breakpoint *b, *scope_breakpoint = NULL;
10862   struct expression *exp;
10863   const struct block *exp_valid_block = NULL, *cond_exp_valid_block = NULL;
10864   struct value *val, *mark, *result;
10865   struct frame_info *frame;
10866   const char *exp_start = NULL;
10867   const char *exp_end = NULL;
10868   const char *tok, *end_tok;
10869   int toklen = -1;
10870   const char *cond_start = NULL;
10871   const char *cond_end = NULL;
10872   enum bptype bp_type;
10873   int thread = -1;
10874   int pc = 0;
10875   /* Flag to indicate whether we are going to use masks for
10876      the hardware watchpoint.  */
10877   int use_mask = 0;
10878   CORE_ADDR mask = 0;
10879   struct watchpoint *w;
10880   char *expression;
10881   struct cleanup *back_to;
10882
10883   /* Make sure that we actually have parameters to parse.  */
10884   if (arg != NULL && arg[0] != '\0')
10885     {
10886       const char *value_start;
10887
10888       exp_end = arg + strlen (arg);
10889
10890       /* Look for "parameter value" pairs at the end
10891          of the arguments string.  */
10892       for (tok = exp_end - 1; tok > arg; tok--)
10893         {
10894           /* Skip whitespace at the end of the argument list.  */
10895           while (tok > arg && (*tok == ' ' || *tok == '\t'))
10896             tok--;
10897
10898           /* Find the beginning of the last token.
10899              This is the value of the parameter.  */
10900           while (tok > arg && (*tok != ' ' && *tok != '\t'))
10901             tok--;
10902           value_start = tok + 1;
10903
10904           /* Skip whitespace.  */
10905           while (tok > arg && (*tok == ' ' || *tok == '\t'))
10906             tok--;
10907
10908           end_tok = tok;
10909
10910           /* Find the beginning of the second to last token.
10911              This is the parameter itself.  */
10912           while (tok > arg && (*tok != ' ' && *tok != '\t'))
10913             tok--;
10914           tok++;
10915           toklen = end_tok - tok + 1;
10916
10917           if (toklen == 6 && !strncmp (tok, "thread", 6))
10918             {
10919               /* At this point we've found a "thread" token, which means
10920                  the user is trying to set a watchpoint that triggers
10921                  only in a specific thread.  */
10922               char *endp;
10923
10924               if (thread != -1)
10925                 error(_("You can specify only one thread."));
10926
10927               /* Extract the thread ID from the next token.  */
10928               thread = strtol (value_start, &endp, 0);
10929
10930               /* Check if the user provided a valid numeric value for the
10931                  thread ID.  */
10932               if (*endp != ' ' && *endp != '\t' && *endp != '\0')
10933                 error (_("Invalid thread ID specification %s."), value_start);
10934
10935               /* Check if the thread actually exists.  */
10936               if (!valid_thread_id (thread))
10937                 invalid_thread_id_error (thread);
10938             }
10939           else if (toklen == 4 && !strncmp (tok, "mask", 4))
10940             {
10941               /* We've found a "mask" token, which means the user wants to
10942                  create a hardware watchpoint that is going to have the mask
10943                  facility.  */
10944               struct value *mask_value, *mark;
10945
10946               if (use_mask)
10947                 error(_("You can specify only one mask."));
10948
10949               use_mask = just_location = 1;
10950
10951               mark = value_mark ();
10952               mask_value = parse_to_comma_and_eval (&value_start);
10953               mask = value_as_address (mask_value);
10954               value_free_to_mark (mark);
10955             }
10956           else
10957             /* We didn't recognize what we found.  We should stop here.  */
10958             break;
10959
10960           /* Truncate the string and get rid of the "parameter value" pair before
10961              the arguments string is parsed by the parse_exp_1 function.  */
10962           exp_end = tok;
10963         }
10964     }
10965   else
10966     exp_end = arg;
10967
10968   /* Parse the rest of the arguments.  From here on out, everything
10969      is in terms of a newly allocated string instead of the original
10970      ARG.  */
10971   innermost_block = NULL;
10972   expression = savestring (arg, exp_end - arg);
10973   back_to = make_cleanup (xfree, expression);
10974   exp_start = arg = expression;
10975   exp = parse_exp_1 (&arg, 0, 0, 0);
10976   exp_end = arg;
10977   /* Remove trailing whitespace from the expression before saving it.
10978      This makes the eventual display of the expression string a bit
10979      prettier.  */
10980   while (exp_end > exp_start && (exp_end[-1] == ' ' || exp_end[-1] == '\t'))
10981     --exp_end;
10982
10983   /* Checking if the expression is not constant.  */
10984   if (watchpoint_exp_is_const (exp))
10985     {
10986       int len;
10987
10988       len = exp_end - exp_start;
10989       while (len > 0 && isspace (exp_start[len - 1]))
10990         len--;
10991       error (_("Cannot watch constant value `%.*s'."), len, exp_start);
10992     }
10993
10994   exp_valid_block = innermost_block;
10995   mark = value_mark ();
10996   fetch_subexp_value (exp, &pc, &val, &result, NULL);
10997
10998   if (just_location)
10999     {
11000       int ret;
11001
11002       exp_valid_block = NULL;
11003       val = value_addr (result);
11004       release_value (val);
11005       value_free_to_mark (mark);
11006
11007       if (use_mask)
11008         {
11009           ret = target_masked_watch_num_registers (value_as_address (val),
11010                                                    mask);
11011           if (ret == -1)
11012             error (_("This target does not support masked watchpoints."));
11013           else if (ret == -2)
11014             error (_("Invalid mask or memory region."));
11015         }
11016     }
11017   else if (val != NULL)
11018     release_value (val);
11019
11020   tok = skip_spaces_const (arg);
11021   end_tok = skip_to_space_const (tok);
11022
11023   toklen = end_tok - tok;
11024   if (toklen >= 1 && strncmp (tok, "if", toklen) == 0)
11025     {
11026       struct expression *cond;
11027
11028       innermost_block = NULL;
11029       tok = cond_start = end_tok + 1;
11030       cond = parse_exp_1 (&tok, 0, 0, 0);
11031
11032       /* The watchpoint expression may not be local, but the condition
11033          may still be.  E.g.: `watch global if local > 0'.  */
11034       cond_exp_valid_block = innermost_block;
11035
11036       xfree (cond);
11037       cond_end = tok;
11038     }
11039   if (*tok)
11040     error (_("Junk at end of command."));
11041
11042   if (accessflag == hw_read)
11043     bp_type = bp_read_watchpoint;
11044   else if (accessflag == hw_access)
11045     bp_type = bp_access_watchpoint;
11046   else
11047     bp_type = bp_hardware_watchpoint;
11048
11049   frame = block_innermost_frame (exp_valid_block);
11050
11051   /* If the expression is "local", then set up a "watchpoint scope"
11052      breakpoint at the point where we've left the scope of the watchpoint
11053      expression.  Create the scope breakpoint before the watchpoint, so
11054      that we will encounter it first in bpstat_stop_status.  */
11055   if (exp_valid_block && frame)
11056     {
11057       if (frame_id_p (frame_unwind_caller_id (frame)))
11058         {
11059           scope_breakpoint
11060             = create_internal_breakpoint (frame_unwind_caller_arch (frame),
11061                                           frame_unwind_caller_pc (frame),
11062                                           bp_watchpoint_scope,
11063                                           &momentary_breakpoint_ops);
11064
11065           scope_breakpoint->enable_state = bp_enabled;
11066
11067           /* Automatically delete the breakpoint when it hits.  */
11068           scope_breakpoint->disposition = disp_del;
11069
11070           /* Only break in the proper frame (help with recursion).  */
11071           scope_breakpoint->frame_id = frame_unwind_caller_id (frame);
11072
11073           /* Set the address at which we will stop.  */
11074           scope_breakpoint->loc->gdbarch
11075             = frame_unwind_caller_arch (frame);
11076           scope_breakpoint->loc->requested_address
11077             = frame_unwind_caller_pc (frame);
11078           scope_breakpoint->loc->address
11079             = adjust_breakpoint_address (scope_breakpoint->loc->gdbarch,
11080                                          scope_breakpoint->loc->requested_address,
11081                                          scope_breakpoint->type);
11082         }
11083     }
11084
11085   /* Now set up the breakpoint.  */
11086
11087   w = XCNEW (struct watchpoint);
11088   b = &w->base;
11089   if (use_mask)
11090     init_raw_breakpoint_without_location (b, NULL, bp_type,
11091                                           &masked_watchpoint_breakpoint_ops);
11092   else
11093     init_raw_breakpoint_without_location (b, NULL, bp_type,
11094                                           &watchpoint_breakpoint_ops);
11095   b->thread = thread;
11096   b->disposition = disp_donttouch;
11097   b->pspace = current_program_space;
11098   w->exp = exp;
11099   w->exp_valid_block = exp_valid_block;
11100   w->cond_exp_valid_block = cond_exp_valid_block;
11101   if (just_location)
11102     {
11103       struct type *t = value_type (val);
11104       CORE_ADDR addr = value_as_address (val);
11105       char *name;
11106
11107       t = check_typedef (TYPE_TARGET_TYPE (check_typedef (t)));
11108       name = type_to_string (t);
11109
11110       w->exp_string_reparse = xstrprintf ("* (%s *) %s", name,
11111                                           core_addr_to_string (addr));
11112       xfree (name);
11113
11114       w->exp_string = xstrprintf ("-location %.*s",
11115                                   (int) (exp_end - exp_start), exp_start);
11116
11117       /* The above expression is in C.  */
11118       b->language = language_c;
11119     }
11120   else
11121     w->exp_string = savestring (exp_start, exp_end - exp_start);
11122
11123   if (use_mask)
11124     {
11125       w->hw_wp_mask = mask;
11126     }
11127   else
11128     {
11129       w->val = val;
11130       w->val_valid = 1;
11131     }
11132
11133   if (cond_start)
11134     b->cond_string = savestring (cond_start, cond_end - cond_start);
11135   else
11136     b->cond_string = 0;
11137
11138   if (frame)
11139     {
11140       w->watchpoint_frame = get_frame_id (frame);
11141       w->watchpoint_thread = inferior_ptid;
11142     }
11143   else
11144     {
11145       w->watchpoint_frame = null_frame_id;
11146       w->watchpoint_thread = null_ptid;
11147     }
11148
11149   if (scope_breakpoint != NULL)
11150     {
11151       /* The scope breakpoint is related to the watchpoint.  We will
11152          need to act on them together.  */
11153       b->related_breakpoint = scope_breakpoint;
11154       scope_breakpoint->related_breakpoint = b;
11155     }
11156
11157   if (!just_location)
11158     value_free_to_mark (mark);
11159
11160   TRY_CATCH (e, RETURN_MASK_ALL)
11161     {
11162       /* Finally update the new watchpoint.  This creates the locations
11163          that should be inserted.  */
11164       update_watchpoint (w, 1);
11165     }
11166   if (e.reason < 0)
11167     {
11168       delete_breakpoint (b);
11169       throw_exception (e);
11170     }
11171
11172   install_breakpoint (internal, b, 1);
11173   do_cleanups (back_to);
11174 }
11175
11176 /* Return count of debug registers needed to watch the given expression.
11177    If the watchpoint cannot be handled in hardware return zero.  */
11178
11179 static int
11180 can_use_hardware_watchpoint (struct value *v)
11181 {
11182   int found_memory_cnt = 0;
11183   struct value *head = v;
11184
11185   /* Did the user specifically forbid us to use hardware watchpoints? */
11186   if (!can_use_hw_watchpoints)
11187     return 0;
11188
11189   /* Make sure that the value of the expression depends only upon
11190      memory contents, and values computed from them within GDB.  If we
11191      find any register references or function calls, we can't use a
11192      hardware watchpoint.
11193
11194      The idea here is that evaluating an expression generates a series
11195      of values, one holding the value of every subexpression.  (The
11196      expression a*b+c has five subexpressions: a, b, a*b, c, and
11197      a*b+c.)  GDB's values hold almost enough information to establish
11198      the criteria given above --- they identify memory lvalues,
11199      register lvalues, computed values, etcetera.  So we can evaluate
11200      the expression, and then scan the chain of values that leaves
11201      behind to decide whether we can detect any possible change to the
11202      expression's final value using only hardware watchpoints.
11203
11204      However, I don't think that the values returned by inferior
11205      function calls are special in any way.  So this function may not
11206      notice that an expression involving an inferior function call
11207      can't be watched with hardware watchpoints.  FIXME.  */
11208   for (; v; v = value_next (v))
11209     {
11210       if (VALUE_LVAL (v) == lval_memory)
11211         {
11212           if (v != head && value_lazy (v))
11213             /* A lazy memory lvalue in the chain is one that GDB never
11214                needed to fetch; we either just used its address (e.g.,
11215                `a' in `a.b') or we never needed it at all (e.g., `a'
11216                in `a,b').  This doesn't apply to HEAD; if that is
11217                lazy then it was not readable, but watch it anyway.  */
11218             ;
11219           else
11220             {
11221               /* Ahh, memory we actually used!  Check if we can cover
11222                  it with hardware watchpoints.  */
11223               struct type *vtype = check_typedef (value_type (v));
11224
11225               /* We only watch structs and arrays if user asked for it
11226                  explicitly, never if they just happen to appear in a
11227                  middle of some value chain.  */
11228               if (v == head
11229                   || (TYPE_CODE (vtype) != TYPE_CODE_STRUCT
11230                       && TYPE_CODE (vtype) != TYPE_CODE_ARRAY))
11231                 {
11232                   CORE_ADDR vaddr = value_address (v);
11233                   int len;
11234                   int num_regs;
11235
11236                   len = (target_exact_watchpoints
11237                          && is_scalar_type_recursive (vtype))?
11238                     1 : TYPE_LENGTH (value_type (v));
11239
11240                   num_regs = target_region_ok_for_hw_watchpoint (vaddr, len);
11241                   if (!num_regs)
11242                     return 0;
11243                   else
11244                     found_memory_cnt += num_regs;
11245                 }
11246             }
11247         }
11248       else if (VALUE_LVAL (v) != not_lval
11249                && deprecated_value_modifiable (v) == 0)
11250         return 0;       /* These are values from the history (e.g., $1).  */
11251       else if (VALUE_LVAL (v) == lval_register)
11252         return 0;       /* Cannot watch a register with a HW watchpoint.  */
11253     }
11254
11255   /* The expression itself looks suitable for using a hardware
11256      watchpoint, but give the target machine a chance to reject it.  */
11257   return found_memory_cnt;
11258 }
11259
11260 void
11261 watch_command_wrapper (char *arg, int from_tty, int internal)
11262 {
11263   watch_command_1 (arg, hw_write, from_tty, 0, internal);
11264 }
11265
11266 /* A helper function that looks for the "-location" argument and then
11267    calls watch_command_1.  */
11268
11269 static void
11270 watch_maybe_just_location (char *arg, int accessflag, int from_tty)
11271 {
11272   int just_location = 0;
11273
11274   if (arg
11275       && (check_for_argument (&arg, "-location", sizeof ("-location") - 1)
11276           || check_for_argument (&arg, "-l", sizeof ("-l") - 1)))
11277     {
11278       arg = skip_spaces (arg);
11279       just_location = 1;
11280     }
11281
11282   watch_command_1 (arg, accessflag, from_tty, just_location, 0);
11283 }
11284
11285 static void
11286 watch_command (char *arg, int from_tty)
11287 {
11288   watch_maybe_just_location (arg, hw_write, from_tty);
11289 }
11290
11291 void
11292 rwatch_command_wrapper (char *arg, int from_tty, int internal)
11293 {
11294   watch_command_1 (arg, hw_read, from_tty, 0, internal);
11295 }
11296
11297 static void
11298 rwatch_command (char *arg, int from_tty)
11299 {
11300   watch_maybe_just_location (arg, hw_read, from_tty);
11301 }
11302
11303 void
11304 awatch_command_wrapper (char *arg, int from_tty, int internal)
11305 {
11306   watch_command_1 (arg, hw_access, from_tty, 0, internal);
11307 }
11308
11309 static void
11310 awatch_command (char *arg, int from_tty)
11311 {
11312   watch_maybe_just_location (arg, hw_access, from_tty);
11313 }
11314 \f
11315
11316 /* Helper routines for the until_command routine in infcmd.c.  Here
11317    because it uses the mechanisms of breakpoints.  */
11318
11319 struct until_break_command_continuation_args
11320 {
11321   struct breakpoint *breakpoint;
11322   struct breakpoint *breakpoint2;
11323   int thread_num;
11324 };
11325
11326 /* This function is called by fetch_inferior_event via the
11327    cmd_continuation pointer, to complete the until command.  It takes
11328    care of cleaning up the temporary breakpoints set up by the until
11329    command.  */
11330 static void
11331 until_break_command_continuation (void *arg, int err)
11332 {
11333   struct until_break_command_continuation_args *a = arg;
11334
11335   delete_breakpoint (a->breakpoint);
11336   if (a->breakpoint2)
11337     delete_breakpoint (a->breakpoint2);
11338   delete_longjmp_breakpoint (a->thread_num);
11339 }
11340
11341 void
11342 until_break_command (char *arg, int from_tty, int anywhere)
11343 {
11344   struct symtabs_and_lines sals;
11345   struct symtab_and_line sal;
11346   struct frame_info *frame;
11347   struct gdbarch *frame_gdbarch;
11348   struct frame_id stack_frame_id;
11349   struct frame_id caller_frame_id;
11350   struct breakpoint *breakpoint;
11351   struct breakpoint *breakpoint2 = NULL;
11352   struct cleanup *old_chain;
11353   int thread;
11354   struct thread_info *tp;
11355
11356   clear_proceed_status ();
11357
11358   /* Set a breakpoint where the user wants it and at return from
11359      this function.  */
11360
11361   if (last_displayed_sal_is_valid ())
11362     sals = decode_line_1 (&arg, DECODE_LINE_FUNFIRSTLINE,
11363                           get_last_displayed_symtab (),
11364                           get_last_displayed_line ());
11365   else
11366     sals = decode_line_1 (&arg, DECODE_LINE_FUNFIRSTLINE,
11367                           (struct symtab *) NULL, 0);
11368
11369   if (sals.nelts != 1)
11370     error (_("Couldn't get information on specified line."));
11371
11372   sal = sals.sals[0];
11373   xfree (sals.sals);    /* malloc'd, so freed.  */
11374
11375   if (*arg)
11376     error (_("Junk at end of arguments."));
11377
11378   resolve_sal_pc (&sal);
11379
11380   tp = inferior_thread ();
11381   thread = tp->num;
11382
11383   old_chain = make_cleanup (null_cleanup, NULL);
11384
11385   /* Note linespec handling above invalidates the frame chain.
11386      Installing a breakpoint also invalidates the frame chain (as it
11387      may need to switch threads), so do any frame handling before
11388      that.  */
11389
11390   frame = get_selected_frame (NULL);
11391   frame_gdbarch = get_frame_arch (frame);
11392   stack_frame_id = get_stack_frame_id (frame);
11393   caller_frame_id = frame_unwind_caller_id (frame);
11394
11395   /* Keep within the current frame, or in frames called by the current
11396      one.  */
11397
11398   if (frame_id_p (caller_frame_id))
11399     {
11400       struct symtab_and_line sal2;
11401
11402       sal2 = find_pc_line (frame_unwind_caller_pc (frame), 0);
11403       sal2.pc = frame_unwind_caller_pc (frame);
11404       breakpoint2 = set_momentary_breakpoint (frame_unwind_caller_arch (frame),
11405                                               sal2,
11406                                               caller_frame_id,
11407                                               bp_until);
11408       make_cleanup_delete_breakpoint (breakpoint2);
11409
11410       set_longjmp_breakpoint (tp, caller_frame_id);
11411       make_cleanup (delete_longjmp_breakpoint_cleanup, &thread);
11412     }
11413
11414   /* set_momentary_breakpoint could invalidate FRAME.  */
11415   frame = NULL;
11416
11417   if (anywhere)
11418     /* If the user told us to continue until a specified location,
11419        we don't specify a frame at which we need to stop.  */
11420     breakpoint = set_momentary_breakpoint (frame_gdbarch, sal,
11421                                            null_frame_id, bp_until);
11422   else
11423     /* Otherwise, specify the selected frame, because we want to stop
11424        only at the very same frame.  */
11425     breakpoint = set_momentary_breakpoint (frame_gdbarch, sal,
11426                                            stack_frame_id, bp_until);
11427   make_cleanup_delete_breakpoint (breakpoint);
11428
11429   proceed (-1, GDB_SIGNAL_DEFAULT, 0);
11430
11431   /* If we are running asynchronously, and proceed call above has
11432      actually managed to start the target, arrange for breakpoints to
11433      be deleted when the target stops.  Otherwise, we're already
11434      stopped and delete breakpoints via cleanup chain.  */
11435
11436   if (target_can_async_p () && is_running (inferior_ptid))
11437     {
11438       struct until_break_command_continuation_args *args;
11439       args = xmalloc (sizeof (*args));
11440
11441       args->breakpoint = breakpoint;
11442       args->breakpoint2 = breakpoint2;
11443       args->thread_num = thread;
11444
11445       discard_cleanups (old_chain);
11446       add_continuation (inferior_thread (),
11447                         until_break_command_continuation, args,
11448                         xfree);
11449     }
11450   else
11451     do_cleanups (old_chain);
11452 }
11453
11454 /* This function attempts to parse an optional "if <cond>" clause
11455    from the arg string.  If one is not found, it returns NULL.
11456
11457    Else, it returns a pointer to the condition string.  (It does not
11458    attempt to evaluate the string against a particular block.)  And,
11459    it updates arg to point to the first character following the parsed
11460    if clause in the arg string.  */
11461
11462 static char *
11463 ep_parse_optional_if_clause (char **arg)
11464 {
11465   char *cond_string;
11466
11467   if (((*arg)[0] != 'i') || ((*arg)[1] != 'f') || !isspace ((*arg)[2]))
11468     return NULL;
11469
11470   /* Skip the "if" keyword.  */
11471   (*arg) += 2;
11472
11473   /* Skip any extra leading whitespace, and record the start of the
11474      condition string.  */
11475   *arg = skip_spaces (*arg);
11476   cond_string = *arg;
11477
11478   /* Assume that the condition occupies the remainder of the arg
11479      string.  */
11480   (*arg) += strlen (cond_string);
11481
11482   return cond_string;
11483 }
11484
11485 /* Commands to deal with catching events, such as signals, exceptions,
11486    process start/exit, etc.  */
11487
11488 typedef enum
11489 {
11490   catch_fork_temporary, catch_vfork_temporary,
11491   catch_fork_permanent, catch_vfork_permanent
11492 }
11493 catch_fork_kind;
11494
11495 static void
11496 catch_fork_command_1 (char *arg, int from_tty, 
11497                       struct cmd_list_element *command)
11498 {
11499   struct gdbarch *gdbarch = get_current_arch ();
11500   char *cond_string = NULL;
11501   catch_fork_kind fork_kind;
11502   int tempflag;
11503
11504   fork_kind = (catch_fork_kind) (uintptr_t) get_cmd_context (command);
11505   tempflag = (fork_kind == catch_fork_temporary
11506               || fork_kind == catch_vfork_temporary);
11507
11508   if (!arg)
11509     arg = "";
11510   arg = skip_spaces (arg);
11511
11512   /* The allowed syntax is:
11513      catch [v]fork
11514      catch [v]fork if <cond>
11515
11516      First, check if there's an if clause.  */
11517   cond_string = ep_parse_optional_if_clause (&arg);
11518
11519   if ((*arg != '\0') && !isspace (*arg))
11520     error (_("Junk at end of arguments."));
11521
11522   /* If this target supports it, create a fork or vfork catchpoint
11523      and enable reporting of such events.  */
11524   switch (fork_kind)
11525     {
11526     case catch_fork_temporary:
11527     case catch_fork_permanent:
11528       create_fork_vfork_event_catchpoint (gdbarch, tempflag, cond_string,
11529                                           &catch_fork_breakpoint_ops);
11530       break;
11531     case catch_vfork_temporary:
11532     case catch_vfork_permanent:
11533       create_fork_vfork_event_catchpoint (gdbarch, tempflag, cond_string,
11534                                           &catch_vfork_breakpoint_ops);
11535       break;
11536     default:
11537       error (_("unsupported or unknown fork kind; cannot catch it"));
11538       break;
11539     }
11540 }
11541
11542 static void
11543 catch_exec_command_1 (char *arg, int from_tty, 
11544                       struct cmd_list_element *command)
11545 {
11546   struct exec_catchpoint *c;
11547   struct gdbarch *gdbarch = get_current_arch ();
11548   int tempflag;
11549   char *cond_string = NULL;
11550
11551   tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
11552
11553   if (!arg)
11554     arg = "";
11555   arg = skip_spaces (arg);
11556
11557   /* The allowed syntax is:
11558      catch exec
11559      catch exec if <cond>
11560
11561      First, check if there's an if clause.  */
11562   cond_string = ep_parse_optional_if_clause (&arg);
11563
11564   if ((*arg != '\0') && !isspace (*arg))
11565     error (_("Junk at end of arguments."));
11566
11567   c = XNEW (struct exec_catchpoint);
11568   init_catchpoint (&c->base, gdbarch, tempflag, cond_string,
11569                    &catch_exec_breakpoint_ops);
11570   c->exec_pathname = NULL;
11571
11572   install_breakpoint (0, &c->base, 1);
11573 }
11574
11575 /* A helper function that returns a value indicating the kind of the
11576    exception catchpoint B.  */
11577
11578 static enum exception_event_kind
11579 classify_exception_breakpoint (struct breakpoint *b)
11580 {
11581   if (strstr (b->addr_string, "catch") != NULL)
11582     return EX_EVENT_CATCH;
11583   else if (strstr (b->addr_string, "rethrow") != NULL)
11584     return EX_EVENT_RETHROW;
11585   else
11586     return EX_EVENT_THROW;
11587 }
11588
11589 static enum print_stop_action
11590 print_it_exception_catchpoint (bpstat bs)
11591 {
11592   struct ui_out *uiout = current_uiout;
11593   struct breakpoint *b = bs->breakpoint_at;
11594   int bp_temp;
11595   enum exception_event_kind kind = classify_exception_breakpoint (b);
11596
11597   annotate_catchpoint (b->number);
11598
11599   if (b->loc->address != b->loc->requested_address)
11600     breakpoint_adjustment_warning (b->loc->requested_address,
11601                                    b->loc->address,
11602                                    b->number, 1);
11603   bp_temp = b->disposition == disp_del;
11604   ui_out_text (uiout, 
11605                bp_temp ? "Temporary catchpoint "
11606                        : "Catchpoint ");
11607   if (!ui_out_is_mi_like_p (uiout))
11608     ui_out_field_int (uiout, "bkptno", b->number);
11609   ui_out_text (uiout,
11610                (kind == EX_EVENT_THROW ? " (exception thrown), "
11611                 : (kind == EX_EVENT_CATCH ? " (exception caught), "
11612                    : " (exception rethrown), ")));
11613   if (ui_out_is_mi_like_p (uiout))
11614     {
11615       ui_out_field_string (uiout, "reason", 
11616                            async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT));
11617       ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
11618       ui_out_field_int (uiout, "bkptno", b->number);
11619     }
11620   return PRINT_SRC_AND_LOC;
11621 }
11622
11623 static void
11624 print_one_exception_catchpoint (struct breakpoint *b, 
11625                                 struct bp_location **last_loc)
11626 {
11627   struct value_print_options opts;
11628   struct ui_out *uiout = current_uiout;
11629   enum exception_event_kind kind = classify_exception_breakpoint (b);
11630
11631   get_user_print_options (&opts);
11632   if (opts.addressprint)
11633     {
11634       annotate_field (4);
11635       if (b->loc == NULL || b->loc->shlib_disabled)
11636         ui_out_field_string (uiout, "addr", "<PENDING>");
11637       else
11638         ui_out_field_core_addr (uiout, "addr",
11639                                 b->loc->gdbarch, b->loc->address);
11640     }
11641   annotate_field (5);
11642   if (b->loc)
11643     *last_loc = b->loc;
11644
11645   switch (kind)
11646     {
11647     case EX_EVENT_THROW:
11648       ui_out_field_string (uiout, "what", "exception throw");
11649       if (ui_out_is_mi_like_p (uiout))
11650         ui_out_field_string (uiout, "catch-type", "throw");
11651       break;
11652
11653     case EX_EVENT_RETHROW:
11654       ui_out_field_string (uiout, "what", "exception rethrow");
11655       if (ui_out_is_mi_like_p (uiout))
11656         ui_out_field_string (uiout, "catch-type", "rethrow");
11657       break;
11658
11659     case EX_EVENT_CATCH:
11660       ui_out_field_string (uiout, "what", "exception catch");
11661       if (ui_out_is_mi_like_p (uiout))
11662         ui_out_field_string (uiout, "catch-type", "catch");
11663       break;
11664     }
11665 }
11666
11667 static void
11668 print_mention_exception_catchpoint (struct breakpoint *b)
11669 {
11670   struct ui_out *uiout = current_uiout;
11671   int bp_temp;
11672   enum exception_event_kind kind = classify_exception_breakpoint (b);
11673
11674   bp_temp = b->disposition == disp_del;
11675   ui_out_text (uiout, bp_temp ? _("Temporary catchpoint ")
11676                               : _("Catchpoint "));
11677   ui_out_field_int (uiout, "bkptno", b->number);
11678   ui_out_text (uiout, (kind == EX_EVENT_THROW ? _(" (throw)")
11679                        : (kind == EX_EVENT_CATCH ? _(" (catch)")
11680                           : _(" (rethrow)"))));
11681 }
11682
11683 /* Implement the "print_recreate" breakpoint_ops method for throw and
11684    catch catchpoints.  */
11685
11686 static void
11687 print_recreate_exception_catchpoint (struct breakpoint *b, 
11688                                      struct ui_file *fp)
11689 {
11690   int bp_temp;
11691   enum exception_event_kind kind = classify_exception_breakpoint (b);
11692
11693   bp_temp = b->disposition == disp_del;
11694   fprintf_unfiltered (fp, bp_temp ? "tcatch " : "catch ");
11695   switch (kind)
11696     {
11697     case EX_EVENT_THROW:
11698       fprintf_unfiltered (fp, "throw");
11699       break;
11700     case EX_EVENT_CATCH:
11701       fprintf_unfiltered (fp, "catch");
11702       break;
11703     case EX_EVENT_RETHROW:
11704       fprintf_unfiltered (fp, "rethrow");
11705       break;
11706     }
11707   print_recreate_thread (b, fp);
11708 }
11709
11710 static struct breakpoint_ops gnu_v3_exception_catchpoint_ops;
11711
11712 static int
11713 handle_gnu_v3_exceptions (int tempflag, char *cond_string,
11714                           enum exception_event_kind ex_event, int from_tty)
11715 {
11716   char *trigger_func_name;
11717  
11718   if (ex_event == EX_EVENT_CATCH)
11719     trigger_func_name = "__cxa_begin_catch";
11720   else if (ex_event == EX_EVENT_RETHROW)
11721     trigger_func_name = "__cxa_rethrow";
11722   else
11723     {
11724       gdb_assert (ex_event == EX_EVENT_THROW);
11725       trigger_func_name = "__cxa_throw";
11726     }
11727
11728   create_breakpoint (get_current_arch (),
11729                      trigger_func_name, cond_string, -1, NULL,
11730                      0 /* condition and thread are valid.  */,
11731                      tempflag, bp_breakpoint,
11732                      0,
11733                      AUTO_BOOLEAN_TRUE /* pending */,
11734                      &gnu_v3_exception_catchpoint_ops, from_tty,
11735                      1 /* enabled */,
11736                      0 /* internal */,
11737                      0);
11738
11739   return 1;
11740 }
11741
11742 /* Deal with "catch catch" and "catch throw" commands.  */
11743
11744 static void
11745 catch_exception_command_1 (enum exception_event_kind ex_event, char *arg,
11746                            int tempflag, int from_tty)
11747 {
11748   char *cond_string = NULL;
11749
11750   if (!arg)
11751     arg = "";
11752   arg = skip_spaces (arg);
11753
11754   cond_string = ep_parse_optional_if_clause (&arg);
11755
11756   if ((*arg != '\0') && !isspace (*arg))
11757     error (_("Junk at end of arguments."));
11758
11759   if (ex_event != EX_EVENT_THROW
11760       && ex_event != EX_EVENT_CATCH
11761       && ex_event != EX_EVENT_RETHROW)
11762     error (_("Unsupported or unknown exception event; cannot catch it"));
11763
11764   if (handle_gnu_v3_exceptions (tempflag, cond_string, ex_event, from_tty))
11765     return;
11766
11767   warning (_("Unsupported with this platform/compiler combination."));
11768 }
11769
11770 /* Implementation of "catch catch" command.  */
11771
11772 static void
11773 catch_catch_command (char *arg, int from_tty, struct cmd_list_element *command)
11774 {
11775   int tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
11776
11777   catch_exception_command_1 (EX_EVENT_CATCH, arg, tempflag, from_tty);
11778 }
11779
11780 /* Implementation of "catch throw" command.  */
11781
11782 static void
11783 catch_throw_command (char *arg, int from_tty, struct cmd_list_element *command)
11784 {
11785   int tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
11786
11787   catch_exception_command_1 (EX_EVENT_THROW, arg, tempflag, from_tty);
11788 }
11789
11790 /* Implementation of "catch rethrow" command.  */
11791
11792 static void
11793 catch_rethrow_command (char *arg, int from_tty,
11794                        struct cmd_list_element *command)
11795 {
11796   int tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
11797
11798   catch_exception_command_1 (EX_EVENT_RETHROW, arg, tempflag, from_tty);
11799 }
11800
11801 void
11802 init_ada_exception_breakpoint (struct breakpoint *b,
11803                                struct gdbarch *gdbarch,
11804                                struct symtab_and_line sal,
11805                                char *addr_string,
11806                                const struct breakpoint_ops *ops,
11807                                int tempflag,
11808                                int from_tty)
11809 {
11810   if (from_tty)
11811     {
11812       struct gdbarch *loc_gdbarch = get_sal_arch (sal);
11813       if (!loc_gdbarch)
11814         loc_gdbarch = gdbarch;
11815
11816       describe_other_breakpoints (loc_gdbarch,
11817                                   sal.pspace, sal.pc, sal.section, -1);
11818       /* FIXME: brobecker/2006-12-28: Actually, re-implement a special
11819          version for exception catchpoints, because two catchpoints
11820          used for different exception names will use the same address.
11821          In this case, a "breakpoint ... also set at..." warning is
11822          unproductive.  Besides, the warning phrasing is also a bit
11823          inappropriate, we should use the word catchpoint, and tell
11824          the user what type of catchpoint it is.  The above is good
11825          enough for now, though.  */
11826     }
11827
11828   init_raw_breakpoint (b, gdbarch, sal, bp_breakpoint, ops);
11829
11830   b->enable_state = bp_enabled;
11831   b->disposition = tempflag ? disp_del : disp_donttouch;
11832   b->addr_string = addr_string;
11833   b->language = language_ada;
11834 }
11835
11836 /* Splits the argument using space as delimiter.  Returns an xmalloc'd
11837    filter list, or NULL if no filtering is required.  */
11838 static VEC(int) *
11839 catch_syscall_split_args (char *arg)
11840 {
11841   VEC(int) *result = NULL;
11842   struct cleanup *cleanup = make_cleanup (VEC_cleanup (int), &result);
11843
11844   while (*arg != '\0')
11845     {
11846       int i, syscall_number;
11847       char *endptr;
11848       char cur_name[128];
11849       struct syscall s;
11850
11851       /* Skip whitespace.  */
11852       arg = skip_spaces (arg);
11853
11854       for (i = 0; i < 127 && arg[i] && !isspace (arg[i]); ++i)
11855         cur_name[i] = arg[i];
11856       cur_name[i] = '\0';
11857       arg += i;
11858
11859       /* Check if the user provided a syscall name or a number.  */
11860       syscall_number = (int) strtol (cur_name, &endptr, 0);
11861       if (*endptr == '\0')
11862         get_syscall_by_number (syscall_number, &s);
11863       else
11864         {
11865           /* We have a name.  Let's check if it's valid and convert it
11866              to a number.  */
11867           get_syscall_by_name (cur_name, &s);
11868
11869           if (s.number == UNKNOWN_SYSCALL)
11870             /* Here we have to issue an error instead of a warning,
11871                because GDB cannot do anything useful if there's no
11872                syscall number to be caught.  */
11873             error (_("Unknown syscall name '%s'."), cur_name);
11874         }
11875
11876       /* Ok, it's valid.  */
11877       VEC_safe_push (int, result, s.number);
11878     }
11879
11880   discard_cleanups (cleanup);
11881   return result;
11882 }
11883
11884 /* Implement the "catch syscall" command.  */
11885
11886 static void
11887 catch_syscall_command_1 (char *arg, int from_tty, 
11888                          struct cmd_list_element *command)
11889 {
11890   int tempflag;
11891   VEC(int) *filter;
11892   struct syscall s;
11893   struct gdbarch *gdbarch = get_current_arch ();
11894
11895   /* Checking if the feature if supported.  */
11896   if (gdbarch_get_syscall_number_p (gdbarch) == 0)
11897     error (_("The feature 'catch syscall' is not supported on \
11898 this architecture yet."));
11899
11900   tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
11901
11902   arg = skip_spaces (arg);
11903
11904   /* We need to do this first "dummy" translation in order
11905      to get the syscall XML file loaded or, most important,
11906      to display a warning to the user if there's no XML file
11907      for his/her architecture.  */
11908   get_syscall_by_number (0, &s);
11909
11910   /* The allowed syntax is:
11911      catch syscall
11912      catch syscall <name | number> [<name | number> ... <name | number>]
11913
11914      Let's check if there's a syscall name.  */
11915
11916   if (arg != NULL)
11917     filter = catch_syscall_split_args (arg);
11918   else
11919     filter = NULL;
11920
11921   create_syscall_event_catchpoint (tempflag, filter,
11922                                    &catch_syscall_breakpoint_ops);
11923 }
11924
11925 static void
11926 catch_command (char *arg, int from_tty)
11927 {
11928   error (_("Catch requires an event name."));
11929 }
11930 \f
11931
11932 static void
11933 tcatch_command (char *arg, int from_tty)
11934 {
11935   error (_("Catch requires an event name."));
11936 }
11937
11938 /* A qsort comparison function that sorts breakpoints in order.  */
11939
11940 static int
11941 compare_breakpoints (const void *a, const void *b)
11942 {
11943   const breakpoint_p *ba = a;
11944   uintptr_t ua = (uintptr_t) *ba;
11945   const breakpoint_p *bb = b;
11946   uintptr_t ub = (uintptr_t) *bb;
11947
11948   if ((*ba)->number < (*bb)->number)
11949     return -1;
11950   else if ((*ba)->number > (*bb)->number)
11951     return 1;
11952
11953   /* Now sort by address, in case we see, e..g, two breakpoints with
11954      the number 0.  */
11955   if (ua < ub)
11956     return -1;
11957   return ua > ub ? 1 : 0;
11958 }
11959
11960 /* Delete breakpoints by address or line.  */
11961
11962 static void
11963 clear_command (char *arg, int from_tty)
11964 {
11965   struct breakpoint *b, *prev;
11966   VEC(breakpoint_p) *found = 0;
11967   int ix;
11968   int default_match;
11969   struct symtabs_and_lines sals;
11970   struct symtab_and_line sal;
11971   int i;
11972   struct cleanup *cleanups = make_cleanup (null_cleanup, NULL);
11973
11974   if (arg)
11975     {
11976       sals = decode_line_with_current_source (arg,
11977                                               (DECODE_LINE_FUNFIRSTLINE
11978                                                | DECODE_LINE_LIST_MODE));
11979       make_cleanup (xfree, sals.sals);
11980       default_match = 0;
11981     }
11982   else
11983     {
11984       sals.sals = (struct symtab_and_line *)
11985         xmalloc (sizeof (struct symtab_and_line));
11986       make_cleanup (xfree, sals.sals);
11987       init_sal (&sal);          /* Initialize to zeroes.  */
11988
11989       /* Set sal's line, symtab, pc, and pspace to the values
11990          corresponding to the last call to print_frame_info.  If the
11991          codepoint is not valid, this will set all the fields to 0.  */
11992       get_last_displayed_sal (&sal);
11993       if (sal.symtab == 0)
11994         error (_("No source file specified."));
11995
11996       sals.sals[0] = sal;
11997       sals.nelts = 1;
11998
11999       default_match = 1;
12000     }
12001
12002   /* We don't call resolve_sal_pc here.  That's not as bad as it
12003      seems, because all existing breakpoints typically have both
12004      file/line and pc set.  So, if clear is given file/line, we can
12005      match this to existing breakpoint without obtaining pc at all.
12006
12007      We only support clearing given the address explicitly 
12008      present in breakpoint table.  Say, we've set breakpoint 
12009      at file:line.  There were several PC values for that file:line,
12010      due to optimization, all in one block.
12011
12012      We've picked one PC value.  If "clear" is issued with another
12013      PC corresponding to the same file:line, the breakpoint won't
12014      be cleared.  We probably can still clear the breakpoint, but 
12015      since the other PC value is never presented to user, user
12016      can only find it by guessing, and it does not seem important
12017      to support that.  */
12018
12019   /* For each line spec given, delete bps which correspond to it.  Do
12020      it in two passes, solely to preserve the current behavior that
12021      from_tty is forced true if we delete more than one
12022      breakpoint.  */
12023
12024   found = NULL;
12025   make_cleanup (VEC_cleanup (breakpoint_p), &found);
12026   for (i = 0; i < sals.nelts; i++)
12027     {
12028       const char *sal_fullname;
12029
12030       /* If exact pc given, clear bpts at that pc.
12031          If line given (pc == 0), clear all bpts on specified line.
12032          If defaulting, clear all bpts on default line
12033          or at default pc.
12034
12035          defaulting    sal.pc != 0    tests to do
12036
12037          0              1             pc
12038          1              1             pc _and_ line
12039          0              0             line
12040          1              0             <can't happen> */
12041
12042       sal = sals.sals[i];
12043       sal_fullname = (sal.symtab == NULL
12044                       ? NULL : symtab_to_fullname (sal.symtab));
12045
12046       /* Find all matching breakpoints and add them to 'found'.  */
12047       ALL_BREAKPOINTS (b)
12048         {
12049           int match = 0;
12050           /* Are we going to delete b?  */
12051           if (b->type != bp_none && !is_watchpoint (b))
12052             {
12053               struct bp_location *loc = b->loc;
12054               for (; loc; loc = loc->next)
12055                 {
12056                   /* If the user specified file:line, don't allow a PC
12057                      match.  This matches historical gdb behavior.  */
12058                   int pc_match = (!sal.explicit_line
12059                                   && sal.pc
12060                                   && (loc->pspace == sal.pspace)
12061                                   && (loc->address == sal.pc)
12062                                   && (!section_is_overlay (loc->section)
12063                                       || loc->section == sal.section));
12064                   int line_match = 0;
12065
12066                   if ((default_match || sal.explicit_line)
12067                       && loc->symtab != NULL
12068                       && sal_fullname != NULL
12069                       && sal.pspace == loc->pspace
12070                       && loc->line_number == sal.line
12071                       && filename_cmp (symtab_to_fullname (loc->symtab),
12072                                        sal_fullname) == 0)
12073                     line_match = 1;
12074
12075                   if (pc_match || line_match)
12076                     {
12077                       match = 1;
12078                       break;
12079                     }
12080                 }
12081             }
12082
12083           if (match)
12084             VEC_safe_push(breakpoint_p, found, b);
12085         }
12086     }
12087
12088   /* Now go thru the 'found' chain and delete them.  */
12089   if (VEC_empty(breakpoint_p, found))
12090     {
12091       if (arg)
12092         error (_("No breakpoint at %s."), arg);
12093       else
12094         error (_("No breakpoint at this line."));
12095     }
12096
12097   /* Remove duplicates from the vec.  */
12098   qsort (VEC_address (breakpoint_p, found),
12099          VEC_length (breakpoint_p, found),
12100          sizeof (breakpoint_p),
12101          compare_breakpoints);
12102   prev = VEC_index (breakpoint_p, found, 0);
12103   for (ix = 1; VEC_iterate (breakpoint_p, found, ix, b); ++ix)
12104     {
12105       if (b == prev)
12106         {
12107           VEC_ordered_remove (breakpoint_p, found, ix);
12108           --ix;
12109         }
12110     }
12111
12112   if (VEC_length(breakpoint_p, found) > 1)
12113     from_tty = 1;       /* Always report if deleted more than one.  */
12114   if (from_tty)
12115     {
12116       if (VEC_length(breakpoint_p, found) == 1)
12117         printf_unfiltered (_("Deleted breakpoint "));
12118       else
12119         printf_unfiltered (_("Deleted breakpoints "));
12120     }
12121
12122   for (ix = 0; VEC_iterate(breakpoint_p, found, ix, b); ix++)
12123     {
12124       if (from_tty)
12125         printf_unfiltered ("%d ", b->number);
12126       delete_breakpoint (b);
12127     }
12128   if (from_tty)
12129     putchar_unfiltered ('\n');
12130
12131   do_cleanups (cleanups);
12132 }
12133 \f
12134 /* Delete breakpoint in BS if they are `delete' breakpoints and
12135    all breakpoints that are marked for deletion, whether hit or not.
12136    This is called after any breakpoint is hit, or after errors.  */
12137
12138 void
12139 breakpoint_auto_delete (bpstat bs)
12140 {
12141   struct breakpoint *b, *b_tmp;
12142
12143   for (; bs; bs = bs->next)
12144     if (bs->breakpoint_at
12145         && bs->breakpoint_at->disposition == disp_del
12146         && bs->stop)
12147       delete_breakpoint (bs->breakpoint_at);
12148
12149   ALL_BREAKPOINTS_SAFE (b, b_tmp)
12150   {
12151     if (b->disposition == disp_del_at_next_stop)
12152       delete_breakpoint (b);
12153   }
12154 }
12155
12156 /* A comparison function for bp_location AP and BP being interfaced to
12157    qsort.  Sort elements primarily by their ADDRESS (no matter what
12158    does breakpoint_address_is_meaningful say for its OWNER),
12159    secondarily by ordering first bp_permanent OWNERed elements and
12160    terciarily just ensuring the array is sorted stable way despite
12161    qsort being an unstable algorithm.  */
12162
12163 static int
12164 bp_location_compare (const void *ap, const void *bp)
12165 {
12166   struct bp_location *a = *(void **) ap;
12167   struct bp_location *b = *(void **) bp;
12168   /* A and B come from existing breakpoints having non-NULL OWNER.  */
12169   int a_perm = a->owner->enable_state == bp_permanent;
12170   int b_perm = b->owner->enable_state == bp_permanent;
12171
12172   if (a->address != b->address)
12173     return (a->address > b->address) - (a->address < b->address);
12174
12175   /* Sort locations at the same address by their pspace number, keeping
12176      locations of the same inferior (in a multi-inferior environment)
12177      grouped.  */
12178
12179   if (a->pspace->num != b->pspace->num)
12180     return ((a->pspace->num > b->pspace->num)
12181             - (a->pspace->num < b->pspace->num));
12182
12183   /* Sort permanent breakpoints first.  */
12184   if (a_perm != b_perm)
12185     return (a_perm < b_perm) - (a_perm > b_perm);
12186
12187   /* Make the internal GDB representation stable across GDB runs
12188      where A and B memory inside GDB can differ.  Breakpoint locations of
12189      the same type at the same address can be sorted in arbitrary order.  */
12190
12191   if (a->owner->number != b->owner->number)
12192     return ((a->owner->number > b->owner->number)
12193             - (a->owner->number < b->owner->number));
12194
12195   return (a > b) - (a < b);
12196 }
12197
12198 /* Set bp_location_placed_address_before_address_max and
12199    bp_location_shadow_len_after_address_max according to the current
12200    content of the bp_location array.  */
12201
12202 static void
12203 bp_location_target_extensions_update (void)
12204 {
12205   struct bp_location *bl, **blp_tmp;
12206
12207   bp_location_placed_address_before_address_max = 0;
12208   bp_location_shadow_len_after_address_max = 0;
12209
12210   ALL_BP_LOCATIONS (bl, blp_tmp)
12211     {
12212       CORE_ADDR start, end, addr;
12213
12214       if (!bp_location_has_shadow (bl))
12215         continue;
12216
12217       start = bl->target_info.placed_address;
12218       end = start + bl->target_info.shadow_len;
12219
12220       gdb_assert (bl->address >= start);
12221       addr = bl->address - start;
12222       if (addr > bp_location_placed_address_before_address_max)
12223         bp_location_placed_address_before_address_max = addr;
12224
12225       /* Zero SHADOW_LEN would not pass bp_location_has_shadow.  */
12226
12227       gdb_assert (bl->address < end);
12228       addr = end - bl->address;
12229       if (addr > bp_location_shadow_len_after_address_max)
12230         bp_location_shadow_len_after_address_max = addr;
12231     }
12232 }
12233
12234 /* Download tracepoint locations if they haven't been.  */
12235
12236 static void
12237 download_tracepoint_locations (void)
12238 {
12239   struct breakpoint *b;
12240   struct cleanup *old_chain;
12241
12242   if (!target_can_download_tracepoint ())
12243     return;
12244
12245   old_chain = save_current_space_and_thread ();
12246
12247   ALL_TRACEPOINTS (b)
12248     {
12249       struct bp_location *bl;
12250       struct tracepoint *t;
12251       int bp_location_downloaded = 0;
12252
12253       if ((b->type == bp_fast_tracepoint
12254            ? !may_insert_fast_tracepoints
12255            : !may_insert_tracepoints))
12256         continue;
12257
12258       for (bl = b->loc; bl; bl = bl->next)
12259         {
12260           /* In tracepoint, locations are _never_ duplicated, so
12261              should_be_inserted is equivalent to
12262              unduplicated_should_be_inserted.  */
12263           if (!should_be_inserted (bl) || bl->inserted)
12264             continue;
12265
12266           switch_to_program_space_and_thread (bl->pspace);
12267
12268           target_download_tracepoint (bl);
12269
12270           bl->inserted = 1;
12271           bp_location_downloaded = 1;
12272         }
12273       t = (struct tracepoint *) b;
12274       t->number_on_target = b->number;
12275       if (bp_location_downloaded)
12276         observer_notify_breakpoint_modified (b);
12277     }
12278
12279   do_cleanups (old_chain);
12280 }
12281
12282 /* Swap the insertion/duplication state between two locations.  */
12283
12284 static void
12285 swap_insertion (struct bp_location *left, struct bp_location *right)
12286 {
12287   const int left_inserted = left->inserted;
12288   const int left_duplicate = left->duplicate;
12289   const int left_needs_update = left->needs_update;
12290   const struct bp_target_info left_target_info = left->target_info;
12291
12292   /* Locations of tracepoints can never be duplicated.  */
12293   if (is_tracepoint (left->owner))
12294     gdb_assert (!left->duplicate);
12295   if (is_tracepoint (right->owner))
12296     gdb_assert (!right->duplicate);
12297
12298   left->inserted = right->inserted;
12299   left->duplicate = right->duplicate;
12300   left->needs_update = right->needs_update;
12301   left->target_info = right->target_info;
12302   right->inserted = left_inserted;
12303   right->duplicate = left_duplicate;
12304   right->needs_update = left_needs_update;
12305   right->target_info = left_target_info;
12306 }
12307
12308 /* Force the re-insertion of the locations at ADDRESS.  This is called
12309    once a new/deleted/modified duplicate location is found and we are evaluating
12310    conditions on the target's side.  Such conditions need to be updated on
12311    the target.  */
12312
12313 static void
12314 force_breakpoint_reinsertion (struct bp_location *bl)
12315 {
12316   struct bp_location **locp = NULL, **loc2p;
12317   struct bp_location *loc;
12318   CORE_ADDR address = 0;
12319   int pspace_num;
12320
12321   address = bl->address;
12322   pspace_num = bl->pspace->num;
12323
12324   /* This is only meaningful if the target is
12325      evaluating conditions and if the user has
12326      opted for condition evaluation on the target's
12327      side.  */
12328   if (gdb_evaluates_breakpoint_condition_p ()
12329       || !target_supports_evaluation_of_breakpoint_conditions ())
12330     return;
12331
12332   /* Flag all breakpoint locations with this address and
12333      the same program space as the location
12334      as "its condition has changed".  We need to
12335      update the conditions on the target's side.  */
12336   ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, address)
12337     {
12338       loc = *loc2p;
12339
12340       if (!is_breakpoint (loc->owner)
12341           || pspace_num != loc->pspace->num)
12342         continue;
12343
12344       /* Flag the location appropriately.  We use a different state to
12345          let everyone know that we already updated the set of locations
12346          with addr bl->address and program space bl->pspace.  This is so
12347          we don't have to keep calling these functions just to mark locations
12348          that have already been marked.  */
12349       loc->condition_changed = condition_updated;
12350
12351       /* Free the agent expression bytecode as well.  We will compute
12352          it later on.  */
12353       if (loc->cond_bytecode)
12354         {
12355           free_agent_expr (loc->cond_bytecode);
12356           loc->cond_bytecode = NULL;
12357         }
12358     }
12359 }
12360
12361 /* If SHOULD_INSERT is false, do not insert any breakpoint locations
12362    into the inferior, only remove already-inserted locations that no
12363    longer should be inserted.  Functions that delete a breakpoint or
12364    breakpoints should pass false, so that deleting a breakpoint
12365    doesn't have the side effect of inserting the locations of other
12366    breakpoints that are marked not-inserted, but should_be_inserted
12367    returns true on them.
12368
12369    This behaviour is useful is situations close to tear-down -- e.g.,
12370    after an exec, while the target still has execution, but breakpoint
12371    shadows of the previous executable image should *NOT* be restored
12372    to the new image; or before detaching, where the target still has
12373    execution and wants to delete breakpoints from GDB's lists, and all
12374    breakpoints had already been removed from the inferior.  */
12375
12376 static void
12377 update_global_location_list (int should_insert)
12378 {
12379   struct breakpoint *b;
12380   struct bp_location **locp, *loc;
12381   struct cleanup *cleanups;
12382   /* Last breakpoint location address that was marked for update.  */
12383   CORE_ADDR last_addr = 0;
12384   /* Last breakpoint location program space that was marked for update.  */
12385   int last_pspace_num = -1;
12386
12387   /* Used in the duplicates detection below.  When iterating over all
12388      bp_locations, points to the first bp_location of a given address.
12389      Breakpoints and watchpoints of different types are never
12390      duplicates of each other.  Keep one pointer for each type of
12391      breakpoint/watchpoint, so we only need to loop over all locations
12392      once.  */
12393   struct bp_location *bp_loc_first;  /* breakpoint */
12394   struct bp_location *wp_loc_first;  /* hardware watchpoint */
12395   struct bp_location *awp_loc_first; /* access watchpoint */
12396   struct bp_location *rwp_loc_first; /* read watchpoint */
12397
12398   /* Saved former bp_location array which we compare against the newly
12399      built bp_location from the current state of ALL_BREAKPOINTS.  */
12400   struct bp_location **old_location, **old_locp;
12401   unsigned old_location_count;
12402
12403   old_location = bp_location;
12404   old_location_count = bp_location_count;
12405   bp_location = NULL;
12406   bp_location_count = 0;
12407   cleanups = make_cleanup (xfree, old_location);
12408
12409   ALL_BREAKPOINTS (b)
12410     for (loc = b->loc; loc; loc = loc->next)
12411       bp_location_count++;
12412
12413   bp_location = xmalloc (sizeof (*bp_location) * bp_location_count);
12414   locp = bp_location;
12415   ALL_BREAKPOINTS (b)
12416     for (loc = b->loc; loc; loc = loc->next)
12417       *locp++ = loc;
12418   qsort (bp_location, bp_location_count, sizeof (*bp_location),
12419          bp_location_compare);
12420
12421   bp_location_target_extensions_update ();
12422
12423   /* Identify bp_location instances that are no longer present in the
12424      new list, and therefore should be freed.  Note that it's not
12425      necessary that those locations should be removed from inferior --
12426      if there's another location at the same address (previously
12427      marked as duplicate), we don't need to remove/insert the
12428      location.
12429      
12430      LOCP is kept in sync with OLD_LOCP, each pointing to the current
12431      and former bp_location array state respectively.  */
12432
12433   locp = bp_location;
12434   for (old_locp = old_location; old_locp < old_location + old_location_count;
12435        old_locp++)
12436     {
12437       struct bp_location *old_loc = *old_locp;
12438       struct bp_location **loc2p;
12439
12440       /* Tells if 'old_loc' is found among the new locations.  If
12441          not, we have to free it.  */
12442       int found_object = 0;
12443       /* Tells if the location should remain inserted in the target.  */
12444       int keep_in_target = 0;
12445       int removed = 0;
12446
12447       /* Skip LOCP entries which will definitely never be needed.
12448          Stop either at or being the one matching OLD_LOC.  */
12449       while (locp < bp_location + bp_location_count
12450              && (*locp)->address < old_loc->address)
12451         locp++;
12452
12453       for (loc2p = locp;
12454            (loc2p < bp_location + bp_location_count
12455             && (*loc2p)->address == old_loc->address);
12456            loc2p++)
12457         {
12458           /* Check if this is a new/duplicated location or a duplicated
12459              location that had its condition modified.  If so, we want to send
12460              its condition to the target if evaluation of conditions is taking
12461              place there.  */
12462           if ((*loc2p)->condition_changed == condition_modified
12463               && (last_addr != old_loc->address
12464                   || last_pspace_num != old_loc->pspace->num))
12465             {
12466               force_breakpoint_reinsertion (*loc2p);
12467               last_pspace_num = old_loc->pspace->num;
12468             }
12469
12470           if (*loc2p == old_loc)
12471             found_object = 1;
12472         }
12473
12474       /* We have already handled this address, update it so that we don't
12475          have to go through updates again.  */
12476       last_addr = old_loc->address;
12477
12478       /* Target-side condition evaluation: Handle deleted locations.  */
12479       if (!found_object)
12480         force_breakpoint_reinsertion (old_loc);
12481
12482       /* If this location is no longer present, and inserted, look if
12483          there's maybe a new location at the same address.  If so,
12484          mark that one inserted, and don't remove this one.  This is
12485          needed so that we don't have a time window where a breakpoint
12486          at certain location is not inserted.  */
12487
12488       if (old_loc->inserted)
12489         {
12490           /* If the location is inserted now, we might have to remove
12491              it.  */
12492
12493           if (found_object && should_be_inserted (old_loc))
12494             {
12495               /* The location is still present in the location list,
12496                  and still should be inserted.  Don't do anything.  */
12497               keep_in_target = 1;
12498             }
12499           else
12500             {
12501               /* This location still exists, but it won't be kept in the
12502                  target since it may have been disabled.  We proceed to
12503                  remove its target-side condition.  */
12504
12505               /* The location is either no longer present, or got
12506                  disabled.  See if there's another location at the
12507                  same address, in which case we don't need to remove
12508                  this one from the target.  */
12509
12510               /* OLD_LOC comes from existing struct breakpoint.  */
12511               if (breakpoint_address_is_meaningful (old_loc->owner))
12512                 {
12513                   for (loc2p = locp;
12514                        (loc2p < bp_location + bp_location_count
12515                         && (*loc2p)->address == old_loc->address);
12516                        loc2p++)
12517                     {
12518                       struct bp_location *loc2 = *loc2p;
12519
12520                       if (breakpoint_locations_match (loc2, old_loc))
12521                         {
12522                           /* Read watchpoint locations are switched to
12523                              access watchpoints, if the former are not
12524                              supported, but the latter are.  */
12525                           if (is_hardware_watchpoint (old_loc->owner))
12526                             {
12527                               gdb_assert (is_hardware_watchpoint (loc2->owner));
12528                               loc2->watchpoint_type = old_loc->watchpoint_type;
12529                             }
12530
12531                           /* loc2 is a duplicated location. We need to check
12532                              if it should be inserted in case it will be
12533                              unduplicated.  */
12534                           if (loc2 != old_loc
12535                               && unduplicated_should_be_inserted (loc2))
12536                             {
12537                               swap_insertion (old_loc, loc2);
12538                               keep_in_target = 1;
12539                               break;
12540                             }
12541                         }
12542                     }
12543                 }
12544             }
12545
12546           if (!keep_in_target)
12547             {
12548               if (remove_breakpoint (old_loc, mark_uninserted))
12549                 {
12550                   /* This is just about all we can do.  We could keep
12551                      this location on the global list, and try to
12552                      remove it next time, but there's no particular
12553                      reason why we will succeed next time.
12554                      
12555                      Note that at this point, old_loc->owner is still
12556                      valid, as delete_breakpoint frees the breakpoint
12557                      only after calling us.  */
12558                   printf_filtered (_("warning: Error removing "
12559                                      "breakpoint %d\n"), 
12560                                    old_loc->owner->number);
12561                 }
12562               removed = 1;
12563             }
12564         }
12565
12566       if (!found_object)
12567         {
12568           if (removed && non_stop
12569               && breakpoint_address_is_meaningful (old_loc->owner)
12570               && !is_hardware_watchpoint (old_loc->owner))
12571             {
12572               /* This location was removed from the target.  In
12573                  non-stop mode, a race condition is possible where
12574                  we've removed a breakpoint, but stop events for that
12575                  breakpoint are already queued and will arrive later.
12576                  We apply an heuristic to be able to distinguish such
12577                  SIGTRAPs from other random SIGTRAPs: we keep this
12578                  breakpoint location for a bit, and will retire it
12579                  after we see some number of events.  The theory here
12580                  is that reporting of events should, "on the average",
12581                  be fair, so after a while we'll see events from all
12582                  threads that have anything of interest, and no longer
12583                  need to keep this breakpoint location around.  We
12584                  don't hold locations forever so to reduce chances of
12585                  mistaking a non-breakpoint SIGTRAP for a breakpoint
12586                  SIGTRAP.
12587
12588                  The heuristic failing can be disastrous on
12589                  decr_pc_after_break targets.
12590
12591                  On decr_pc_after_break targets, like e.g., x86-linux,
12592                  if we fail to recognize a late breakpoint SIGTRAP,
12593                  because events_till_retirement has reached 0 too
12594                  soon, we'll fail to do the PC adjustment, and report
12595                  a random SIGTRAP to the user.  When the user resumes
12596                  the inferior, it will most likely immediately crash
12597                  with SIGILL/SIGBUS/SIGSEGV, or worse, get silently
12598                  corrupted, because of being resumed e.g., in the
12599                  middle of a multi-byte instruction, or skipped a
12600                  one-byte instruction.  This was actually seen happen
12601                  on native x86-linux, and should be less rare on
12602                  targets that do not support new thread events, like
12603                  remote, due to the heuristic depending on
12604                  thread_count.
12605
12606                  Mistaking a random SIGTRAP for a breakpoint trap
12607                  causes similar symptoms (PC adjustment applied when
12608                  it shouldn't), but then again, playing with SIGTRAPs
12609                  behind the debugger's back is asking for trouble.
12610
12611                  Since hardware watchpoint traps are always
12612                  distinguishable from other traps, so we don't need to
12613                  apply keep hardware watchpoint moribund locations
12614                  around.  We simply always ignore hardware watchpoint
12615                  traps we can no longer explain.  */
12616
12617               old_loc->events_till_retirement = 3 * (thread_count () + 1);
12618               old_loc->owner = NULL;
12619
12620               VEC_safe_push (bp_location_p, moribund_locations, old_loc);
12621             }
12622           else
12623             {
12624               old_loc->owner = NULL;
12625               decref_bp_location (&old_loc);
12626             }
12627         }
12628     }
12629
12630   /* Rescan breakpoints at the same address and section, marking the
12631      first one as "first" and any others as "duplicates".  This is so
12632      that the bpt instruction is only inserted once.  If we have a
12633      permanent breakpoint at the same place as BPT, make that one the
12634      official one, and the rest as duplicates.  Permanent breakpoints
12635      are sorted first for the same address.
12636
12637      Do the same for hardware watchpoints, but also considering the
12638      watchpoint's type (regular/access/read) and length.  */
12639
12640   bp_loc_first = NULL;
12641   wp_loc_first = NULL;
12642   awp_loc_first = NULL;
12643   rwp_loc_first = NULL;
12644   ALL_BP_LOCATIONS (loc, locp)
12645     {
12646       /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always
12647          non-NULL.  */
12648       struct bp_location **loc_first_p;
12649       b = loc->owner;
12650
12651       if (!unduplicated_should_be_inserted (loc)
12652           || !breakpoint_address_is_meaningful (b)
12653           /* Don't detect duplicate for tracepoint locations because they are
12654            never duplicated.  See the comments in field `duplicate' of
12655            `struct bp_location'.  */
12656           || is_tracepoint (b))
12657         {
12658           /* Clear the condition modification flag.  */
12659           loc->condition_changed = condition_unchanged;
12660           continue;
12661         }
12662
12663       /* Permanent breakpoint should always be inserted.  */
12664       if (b->enable_state == bp_permanent && ! loc->inserted)
12665         internal_error (__FILE__, __LINE__,
12666                         _("allegedly permanent breakpoint is not "
12667                         "actually inserted"));
12668
12669       if (b->type == bp_hardware_watchpoint)
12670         loc_first_p = &wp_loc_first;
12671       else if (b->type == bp_read_watchpoint)
12672         loc_first_p = &rwp_loc_first;
12673       else if (b->type == bp_access_watchpoint)
12674         loc_first_p = &awp_loc_first;
12675       else
12676         loc_first_p = &bp_loc_first;
12677
12678       if (*loc_first_p == NULL
12679           || (overlay_debugging && loc->section != (*loc_first_p)->section)
12680           || !breakpoint_locations_match (loc, *loc_first_p))
12681         {
12682           *loc_first_p = loc;
12683           loc->duplicate = 0;
12684
12685           if (is_breakpoint (loc->owner) && loc->condition_changed)
12686             {
12687               loc->needs_update = 1;
12688               /* Clear the condition modification flag.  */
12689               loc->condition_changed = condition_unchanged;
12690             }
12691           continue;
12692         }
12693
12694
12695       /* This and the above ensure the invariant that the first location
12696          is not duplicated, and is the inserted one.
12697          All following are marked as duplicated, and are not inserted.  */
12698       if (loc->inserted)
12699         swap_insertion (loc, *loc_first_p);
12700       loc->duplicate = 1;
12701
12702       /* Clear the condition modification flag.  */
12703       loc->condition_changed = condition_unchanged;
12704
12705       if ((*loc_first_p)->owner->enable_state == bp_permanent && loc->inserted
12706           && b->enable_state != bp_permanent)
12707         internal_error (__FILE__, __LINE__,
12708                         _("another breakpoint was inserted on top of "
12709                         "a permanent breakpoint"));
12710     }
12711
12712   if (breakpoints_always_inserted_mode ()
12713       && (have_live_inferiors ()
12714           || (gdbarch_has_global_breakpoints (target_gdbarch ()))))
12715     {
12716       if (should_insert)
12717         insert_breakpoint_locations ();
12718       else
12719         {
12720           /* Though should_insert is false, we may need to update conditions
12721              on the target's side if it is evaluating such conditions.  We
12722              only update conditions for locations that are marked
12723              "needs_update".  */
12724           update_inserted_breakpoint_locations ();
12725         }
12726     }
12727
12728   if (should_insert)
12729     download_tracepoint_locations ();
12730
12731   do_cleanups (cleanups);
12732 }
12733
12734 void
12735 breakpoint_retire_moribund (void)
12736 {
12737   struct bp_location *loc;
12738   int ix;
12739
12740   for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, loc); ++ix)
12741     if (--(loc->events_till_retirement) == 0)
12742       {
12743         decref_bp_location (&loc);
12744         VEC_unordered_remove (bp_location_p, moribund_locations, ix);
12745         --ix;
12746       }
12747 }
12748
12749 static void
12750 update_global_location_list_nothrow (int inserting)
12751 {
12752   volatile struct gdb_exception e;
12753
12754   TRY_CATCH (e, RETURN_MASK_ERROR)
12755     update_global_location_list (inserting);
12756 }
12757
12758 /* Clear BKP from a BPS.  */
12759
12760 static void
12761 bpstat_remove_bp_location (bpstat bps, struct breakpoint *bpt)
12762 {
12763   bpstat bs;
12764
12765   for (bs = bps; bs; bs = bs->next)
12766     if (bs->breakpoint_at == bpt)
12767       {
12768         bs->breakpoint_at = NULL;
12769         bs->old_val = NULL;
12770         /* bs->commands will be freed later.  */
12771       }
12772 }
12773
12774 /* Callback for iterate_over_threads.  */
12775 static int
12776 bpstat_remove_breakpoint_callback (struct thread_info *th, void *data)
12777 {
12778   struct breakpoint *bpt = data;
12779
12780   bpstat_remove_bp_location (th->control.stop_bpstat, bpt);
12781   return 0;
12782 }
12783
12784 /* Helper for breakpoint and tracepoint breakpoint_ops->mention
12785    callbacks.  */
12786
12787 static void
12788 say_where (struct breakpoint *b)
12789 {
12790   struct value_print_options opts;
12791
12792   get_user_print_options (&opts);
12793
12794   /* i18n: cagney/2005-02-11: Below needs to be merged into a
12795      single string.  */
12796   if (b->loc == NULL)
12797     {
12798       printf_filtered (_(" (%s) pending."), b->addr_string);
12799     }
12800   else
12801     {
12802       if (opts.addressprint || b->loc->symtab == NULL)
12803         {
12804           printf_filtered (" at ");
12805           fputs_filtered (paddress (b->loc->gdbarch, b->loc->address),
12806                           gdb_stdout);
12807         }
12808       if (b->loc->symtab != NULL)
12809         {
12810           /* If there is a single location, we can print the location
12811              more nicely.  */
12812           if (b->loc->next == NULL)
12813             printf_filtered (": file %s, line %d.",
12814                              symtab_to_filename_for_display (b->loc->symtab),
12815                              b->loc->line_number);
12816           else
12817             /* This is not ideal, but each location may have a
12818                different file name, and this at least reflects the
12819                real situation somewhat.  */
12820             printf_filtered (": %s.", b->addr_string);
12821         }
12822
12823       if (b->loc->next)
12824         {
12825           struct bp_location *loc = b->loc;
12826           int n = 0;
12827           for (; loc; loc = loc->next)
12828             ++n;
12829           printf_filtered (" (%d locations)", n);
12830         }
12831     }
12832 }
12833
12834 /* Default bp_location_ops methods.  */
12835
12836 static void
12837 bp_location_dtor (struct bp_location *self)
12838 {
12839   xfree (self->cond);
12840   if (self->cond_bytecode)
12841     free_agent_expr (self->cond_bytecode);
12842   xfree (self->function_name);
12843 }
12844
12845 static const struct bp_location_ops bp_location_ops =
12846 {
12847   bp_location_dtor
12848 };
12849
12850 /* Default breakpoint_ops methods all breakpoint_ops ultimately
12851    inherit from.  */
12852
12853 static void
12854 base_breakpoint_dtor (struct breakpoint *self)
12855 {
12856   decref_counted_command_line (&self->commands);
12857   xfree (self->cond_string);
12858   xfree (self->extra_string);
12859   xfree (self->addr_string);
12860   xfree (self->filter);
12861   xfree (self->addr_string_range_end);
12862 }
12863
12864 static struct bp_location *
12865 base_breakpoint_allocate_location (struct breakpoint *self)
12866 {
12867   struct bp_location *loc;
12868
12869   loc = XNEW (struct bp_location);
12870   init_bp_location (loc, &bp_location_ops, self);
12871   return loc;
12872 }
12873
12874 static void
12875 base_breakpoint_re_set (struct breakpoint *b)
12876 {
12877   /* Nothing to re-set. */
12878 }
12879
12880 #define internal_error_pure_virtual_called() \
12881   gdb_assert_not_reached ("pure virtual function called")
12882
12883 static int
12884 base_breakpoint_insert_location (struct bp_location *bl)
12885 {
12886   internal_error_pure_virtual_called ();
12887 }
12888
12889 static int
12890 base_breakpoint_remove_location (struct bp_location *bl)
12891 {
12892   internal_error_pure_virtual_called ();
12893 }
12894
12895 static int
12896 base_breakpoint_breakpoint_hit (const struct bp_location *bl,
12897                                 struct address_space *aspace,
12898                                 CORE_ADDR bp_addr,
12899                                 const struct target_waitstatus *ws)
12900 {
12901   internal_error_pure_virtual_called ();
12902 }
12903
12904 static void
12905 base_breakpoint_check_status (bpstat bs)
12906 {
12907   /* Always stop.   */
12908 }
12909
12910 /* A "works_in_software_mode" breakpoint_ops method that just internal
12911    errors.  */
12912
12913 static int
12914 base_breakpoint_works_in_software_mode (const struct breakpoint *b)
12915 {
12916   internal_error_pure_virtual_called ();
12917 }
12918
12919 /* A "resources_needed" breakpoint_ops method that just internal
12920    errors.  */
12921
12922 static int
12923 base_breakpoint_resources_needed (const struct bp_location *bl)
12924 {
12925   internal_error_pure_virtual_called ();
12926 }
12927
12928 static enum print_stop_action
12929 base_breakpoint_print_it (bpstat bs)
12930 {
12931   internal_error_pure_virtual_called ();
12932 }
12933
12934 static void
12935 base_breakpoint_print_one_detail (const struct breakpoint *self,
12936                                   struct ui_out *uiout)
12937 {
12938   /* nothing */
12939 }
12940
12941 static void
12942 base_breakpoint_print_mention (struct breakpoint *b)
12943 {
12944   internal_error_pure_virtual_called ();
12945 }
12946
12947 static void
12948 base_breakpoint_print_recreate (struct breakpoint *b, struct ui_file *fp)
12949 {
12950   internal_error_pure_virtual_called ();
12951 }
12952
12953 static void
12954 base_breakpoint_create_sals_from_address (char **arg,
12955                                           struct linespec_result *canonical,
12956                                           enum bptype type_wanted,
12957                                           char *addr_start,
12958                                           char **copy_arg)
12959 {
12960   internal_error_pure_virtual_called ();
12961 }
12962
12963 static void
12964 base_breakpoint_create_breakpoints_sal (struct gdbarch *gdbarch,
12965                                         struct linespec_result *c,
12966                                         struct linespec_sals *lsal,
12967                                         char *cond_string,
12968                                         char *extra_string,
12969                                         enum bptype type_wanted,
12970                                         enum bpdisp disposition,
12971                                         int thread,
12972                                         int task, int ignore_count,
12973                                         const struct breakpoint_ops *o,
12974                                         int from_tty, int enabled,
12975                                         int internal, unsigned flags)
12976 {
12977   internal_error_pure_virtual_called ();
12978 }
12979
12980 static void
12981 base_breakpoint_decode_linespec (struct breakpoint *b, char **s,
12982                                  struct symtabs_and_lines *sals)
12983 {
12984   internal_error_pure_virtual_called ();
12985 }
12986
12987 /* The default 'explains_signal' method.  */
12988
12989 static enum bpstat_signal_value
12990 base_breakpoint_explains_signal (struct breakpoint *b)
12991 {
12992   return BPSTAT_SIGNAL_HIDE;
12993 }
12994
12995 struct breakpoint_ops base_breakpoint_ops =
12996 {
12997   base_breakpoint_dtor,
12998   base_breakpoint_allocate_location,
12999   base_breakpoint_re_set,
13000   base_breakpoint_insert_location,
13001   base_breakpoint_remove_location,
13002   base_breakpoint_breakpoint_hit,
13003   base_breakpoint_check_status,
13004   base_breakpoint_resources_needed,
13005   base_breakpoint_works_in_software_mode,
13006   base_breakpoint_print_it,
13007   NULL,
13008   base_breakpoint_print_one_detail,
13009   base_breakpoint_print_mention,
13010   base_breakpoint_print_recreate,
13011   base_breakpoint_create_sals_from_address,
13012   base_breakpoint_create_breakpoints_sal,
13013   base_breakpoint_decode_linespec,
13014   base_breakpoint_explains_signal
13015 };
13016
13017 /* Default breakpoint_ops methods.  */
13018
13019 static void
13020 bkpt_re_set (struct breakpoint *b)
13021 {
13022   /* FIXME: is this still reachable?  */
13023   if (b->addr_string == NULL)
13024     {
13025       /* Anything without a string can't be re-set.  */
13026       delete_breakpoint (b);
13027       return;
13028     }
13029
13030   breakpoint_re_set_default (b);
13031 }
13032
13033 static int
13034 bkpt_insert_location (struct bp_location *bl)
13035 {
13036   if (bl->loc_type == bp_loc_hardware_breakpoint)
13037     return target_insert_hw_breakpoint (bl->gdbarch,
13038                                         &bl->target_info);
13039   else
13040     return target_insert_breakpoint (bl->gdbarch,
13041                                      &bl->target_info);
13042 }
13043
13044 static int
13045 bkpt_remove_location (struct bp_location *bl)
13046 {
13047   if (bl->loc_type == bp_loc_hardware_breakpoint)
13048     return target_remove_hw_breakpoint (bl->gdbarch, &bl->target_info);
13049   else
13050     return target_remove_breakpoint (bl->gdbarch, &bl->target_info);
13051 }
13052
13053 static int
13054 bkpt_breakpoint_hit (const struct bp_location *bl,
13055                      struct address_space *aspace, CORE_ADDR bp_addr,
13056                      const struct target_waitstatus *ws)
13057 {
13058   if (ws->kind != TARGET_WAITKIND_STOPPED
13059       || ws->value.sig != GDB_SIGNAL_TRAP)
13060     return 0;
13061
13062   if (!breakpoint_address_match (bl->pspace->aspace, bl->address,
13063                                  aspace, bp_addr))
13064     return 0;
13065
13066   if (overlay_debugging         /* unmapped overlay section */
13067       && section_is_overlay (bl->section)
13068       && !section_is_mapped (bl->section))
13069     return 0;
13070
13071   return 1;
13072 }
13073
13074 static int
13075 bkpt_resources_needed (const struct bp_location *bl)
13076 {
13077   gdb_assert (bl->owner->type == bp_hardware_breakpoint);
13078
13079   return 1;
13080 }
13081
13082 static enum print_stop_action
13083 bkpt_print_it (bpstat bs)
13084 {
13085   struct breakpoint *b;
13086   const struct bp_location *bl;
13087   int bp_temp;
13088   struct ui_out *uiout = current_uiout;
13089
13090   gdb_assert (bs->bp_location_at != NULL);
13091
13092   bl = bs->bp_location_at;
13093   b = bs->breakpoint_at;
13094
13095   bp_temp = b->disposition == disp_del;
13096   if (bl->address != bl->requested_address)
13097     breakpoint_adjustment_warning (bl->requested_address,
13098                                    bl->address,
13099                                    b->number, 1);
13100   annotate_breakpoint (b->number);
13101   if (bp_temp)
13102     ui_out_text (uiout, "\nTemporary breakpoint ");
13103   else
13104     ui_out_text (uiout, "\nBreakpoint ");
13105   if (ui_out_is_mi_like_p (uiout))
13106     {
13107       ui_out_field_string (uiout, "reason",
13108                            async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT));
13109       ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
13110     }
13111   ui_out_field_int (uiout, "bkptno", b->number);
13112   ui_out_text (uiout, ", ");
13113
13114   return PRINT_SRC_AND_LOC;
13115 }
13116
13117 static void
13118 bkpt_print_mention (struct breakpoint *b)
13119 {
13120   if (ui_out_is_mi_like_p (current_uiout))
13121     return;
13122
13123   switch (b->type)
13124     {
13125     case bp_breakpoint:
13126     case bp_gnu_ifunc_resolver:
13127       if (b->disposition == disp_del)
13128         printf_filtered (_("Temporary breakpoint"));
13129       else
13130         printf_filtered (_("Breakpoint"));
13131       printf_filtered (_(" %d"), b->number);
13132       if (b->type == bp_gnu_ifunc_resolver)
13133         printf_filtered (_(" at gnu-indirect-function resolver"));
13134       break;
13135     case bp_hardware_breakpoint:
13136       printf_filtered (_("Hardware assisted breakpoint %d"), b->number);
13137       break;
13138     case bp_dprintf:
13139       printf_filtered (_("Dprintf %d"), b->number);
13140       break;
13141     }
13142
13143   say_where (b);
13144 }
13145
13146 static void
13147 bkpt_print_recreate (struct breakpoint *tp, struct ui_file *fp)
13148 {
13149   if (tp->type == bp_breakpoint && tp->disposition == disp_del)
13150     fprintf_unfiltered (fp, "tbreak");
13151   else if (tp->type == bp_breakpoint)
13152     fprintf_unfiltered (fp, "break");
13153   else if (tp->type == bp_hardware_breakpoint
13154            && tp->disposition == disp_del)
13155     fprintf_unfiltered (fp, "thbreak");
13156   else if (tp->type == bp_hardware_breakpoint)
13157     fprintf_unfiltered (fp, "hbreak");
13158   else
13159     internal_error (__FILE__, __LINE__,
13160                     _("unhandled breakpoint type %d"), (int) tp->type);
13161
13162   fprintf_unfiltered (fp, " %s", tp->addr_string);
13163   print_recreate_thread (tp, fp);
13164 }
13165
13166 static void
13167 bkpt_create_sals_from_address (char **arg,
13168                                struct linespec_result *canonical,
13169                                enum bptype type_wanted,
13170                                char *addr_start, char **copy_arg)
13171 {
13172   create_sals_from_address_default (arg, canonical, type_wanted,
13173                                     addr_start, copy_arg);
13174 }
13175
13176 static void
13177 bkpt_create_breakpoints_sal (struct gdbarch *gdbarch,
13178                              struct linespec_result *canonical,
13179                              struct linespec_sals *lsal,
13180                              char *cond_string,
13181                              char *extra_string,
13182                              enum bptype type_wanted,
13183                              enum bpdisp disposition,
13184                              int thread,
13185                              int task, int ignore_count,
13186                              const struct breakpoint_ops *ops,
13187                              int from_tty, int enabled,
13188                              int internal, unsigned flags)
13189 {
13190   create_breakpoints_sal_default (gdbarch, canonical, lsal,
13191                                   cond_string, extra_string,
13192                                   type_wanted,
13193                                   disposition, thread, task,
13194                                   ignore_count, ops, from_tty,
13195                                   enabled, internal, flags);
13196 }
13197
13198 static void
13199 bkpt_decode_linespec (struct breakpoint *b, char **s,
13200                       struct symtabs_and_lines *sals)
13201 {
13202   decode_linespec_default (b, s, sals);
13203 }
13204
13205 /* Virtual table for internal breakpoints.  */
13206
13207 static void
13208 internal_bkpt_re_set (struct breakpoint *b)
13209 {
13210   switch (b->type)
13211     {
13212       /* Delete overlay event and longjmp master breakpoints; they
13213          will be reset later by breakpoint_re_set.  */
13214     case bp_overlay_event:
13215     case bp_longjmp_master:
13216     case bp_std_terminate_master:
13217     case bp_exception_master:
13218       delete_breakpoint (b);
13219       break;
13220
13221       /* This breakpoint is special, it's set up when the inferior
13222          starts and we really don't want to touch it.  */
13223     case bp_shlib_event:
13224
13225       /* Like bp_shlib_event, this breakpoint type is special.  Once
13226          it is set up, we do not want to touch it.  */
13227     case bp_thread_event:
13228       break;
13229     }
13230 }
13231
13232 static void
13233 internal_bkpt_check_status (bpstat bs)
13234 {
13235   if (bs->breakpoint_at->type == bp_shlib_event)
13236     {
13237       /* If requested, stop when the dynamic linker notifies GDB of
13238          events.  This allows the user to get control and place
13239          breakpoints in initializer routines for dynamically loaded
13240          objects (among other things).  */
13241       bs->stop = stop_on_solib_events;
13242       bs->print = stop_on_solib_events;
13243     }
13244   else
13245     bs->stop = 0;
13246 }
13247
13248 static enum print_stop_action
13249 internal_bkpt_print_it (bpstat bs)
13250 {
13251   struct breakpoint *b;
13252
13253   b = bs->breakpoint_at;
13254
13255   switch (b->type)
13256     {
13257     case bp_shlib_event:
13258       /* Did we stop because the user set the stop_on_solib_events
13259          variable?  (If so, we report this as a generic, "Stopped due
13260          to shlib event" message.) */
13261       print_solib_event (0);
13262       break;
13263
13264     case bp_thread_event:
13265       /* Not sure how we will get here.
13266          GDB should not stop for these breakpoints.  */
13267       printf_filtered (_("Thread Event Breakpoint: gdb should not stop!\n"));
13268       break;
13269
13270     case bp_overlay_event:
13271       /* By analogy with the thread event, GDB should not stop for these.  */
13272       printf_filtered (_("Overlay Event Breakpoint: gdb should not stop!\n"));
13273       break;
13274
13275     case bp_longjmp_master:
13276       /* These should never be enabled.  */
13277       printf_filtered (_("Longjmp Master Breakpoint: gdb should not stop!\n"));
13278       break;
13279
13280     case bp_std_terminate_master:
13281       /* These should never be enabled.  */
13282       printf_filtered (_("std::terminate Master Breakpoint: "
13283                          "gdb should not stop!\n"));
13284       break;
13285
13286     case bp_exception_master:
13287       /* These should never be enabled.  */
13288       printf_filtered (_("Exception Master Breakpoint: "
13289                          "gdb should not stop!\n"));
13290       break;
13291     }
13292
13293   return PRINT_NOTHING;
13294 }
13295
13296 static void
13297 internal_bkpt_print_mention (struct breakpoint *b)
13298 {
13299   /* Nothing to mention.  These breakpoints are internal.  */
13300 }
13301
13302 /* Virtual table for momentary breakpoints  */
13303
13304 static void
13305 momentary_bkpt_re_set (struct breakpoint *b)
13306 {
13307   /* Keep temporary breakpoints, which can be encountered when we step
13308      over a dlopen call and SOLIB_ADD is resetting the breakpoints.
13309      Otherwise these should have been blown away via the cleanup chain
13310      or by breakpoint_init_inferior when we rerun the executable.  */
13311 }
13312
13313 static void
13314 momentary_bkpt_check_status (bpstat bs)
13315 {
13316   /* Nothing.  The point of these breakpoints is causing a stop.  */
13317 }
13318
13319 static enum print_stop_action
13320 momentary_bkpt_print_it (bpstat bs)
13321 {
13322   struct ui_out *uiout = current_uiout;
13323
13324   if (ui_out_is_mi_like_p (uiout))
13325     {
13326       struct breakpoint *b = bs->breakpoint_at;
13327
13328       switch (b->type)
13329         {
13330         case bp_finish:
13331           ui_out_field_string
13332             (uiout, "reason",
13333              async_reason_lookup (EXEC_ASYNC_FUNCTION_FINISHED));
13334           break;
13335
13336         case bp_until:
13337           ui_out_field_string
13338             (uiout, "reason",
13339              async_reason_lookup (EXEC_ASYNC_LOCATION_REACHED));
13340           break;
13341         }
13342     }
13343
13344   return PRINT_UNKNOWN;
13345 }
13346
13347 static void
13348 momentary_bkpt_print_mention (struct breakpoint *b)
13349 {
13350   /* Nothing to mention.  These breakpoints are internal.  */
13351 }
13352
13353 /* Ensure INITIATING_FRAME is cleared when no such breakpoint exists.
13354
13355    It gets cleared already on the removal of the first one of such placed
13356    breakpoints.  This is OK as they get all removed altogether.  */
13357
13358 static void
13359 longjmp_bkpt_dtor (struct breakpoint *self)
13360 {
13361   struct thread_info *tp = find_thread_id (self->thread);
13362
13363   if (tp)
13364     tp->initiating_frame = null_frame_id;
13365
13366   momentary_breakpoint_ops.dtor (self);
13367 }
13368
13369 /* Specific methods for probe breakpoints.  */
13370
13371 static int
13372 bkpt_probe_insert_location (struct bp_location *bl)
13373 {
13374   int v = bkpt_insert_location (bl);
13375
13376   if (v == 0)
13377     {
13378       /* The insertion was successful, now let's set the probe's semaphore
13379          if needed.  */
13380       bl->probe->pops->set_semaphore (bl->probe, bl->gdbarch);
13381     }
13382
13383   return v;
13384 }
13385
13386 static int
13387 bkpt_probe_remove_location (struct bp_location *bl)
13388 {
13389   /* Let's clear the semaphore before removing the location.  */
13390   bl->probe->pops->clear_semaphore (bl->probe, bl->gdbarch);
13391
13392   return bkpt_remove_location (bl);
13393 }
13394
13395 static void
13396 bkpt_probe_create_sals_from_address (char **arg,
13397                                      struct linespec_result *canonical,
13398                                      enum bptype type_wanted,
13399                                      char *addr_start, char **copy_arg)
13400 {
13401   struct linespec_sals lsal;
13402
13403   lsal.sals = parse_probes (arg, canonical);
13404
13405   *copy_arg = xstrdup (canonical->addr_string);
13406   lsal.canonical = xstrdup (*copy_arg);
13407
13408   VEC_safe_push (linespec_sals, canonical->sals, &lsal);
13409 }
13410
13411 static void
13412 bkpt_probe_decode_linespec (struct breakpoint *b, char **s,
13413                             struct symtabs_and_lines *sals)
13414 {
13415   *sals = parse_probes (s, NULL);
13416   if (!sals->sals)
13417     error (_("probe not found"));
13418 }
13419
13420 /* The breakpoint_ops structure to be used in tracepoints.  */
13421
13422 static void
13423 tracepoint_re_set (struct breakpoint *b)
13424 {
13425   breakpoint_re_set_default (b);
13426 }
13427
13428 static int
13429 tracepoint_breakpoint_hit (const struct bp_location *bl,
13430                            struct address_space *aspace, CORE_ADDR bp_addr,
13431                            const struct target_waitstatus *ws)
13432 {
13433   /* By definition, the inferior does not report stops at
13434      tracepoints.  */
13435   return 0;
13436 }
13437
13438 static void
13439 tracepoint_print_one_detail (const struct breakpoint *self,
13440                              struct ui_out *uiout)
13441 {
13442   struct tracepoint *tp = (struct tracepoint *) self;
13443   if (tp->static_trace_marker_id)
13444     {
13445       gdb_assert (self->type == bp_static_tracepoint);
13446
13447       ui_out_text (uiout, "\tmarker id is ");
13448       ui_out_field_string (uiout, "static-tracepoint-marker-string-id",
13449                            tp->static_trace_marker_id);
13450       ui_out_text (uiout, "\n");
13451     }
13452 }
13453
13454 static void
13455 tracepoint_print_mention (struct breakpoint *b)
13456 {
13457   if (ui_out_is_mi_like_p (current_uiout))
13458     return;
13459
13460   switch (b->type)
13461     {
13462     case bp_tracepoint:
13463       printf_filtered (_("Tracepoint"));
13464       printf_filtered (_(" %d"), b->number);
13465       break;
13466     case bp_fast_tracepoint:
13467       printf_filtered (_("Fast tracepoint"));
13468       printf_filtered (_(" %d"), b->number);
13469       break;
13470     case bp_static_tracepoint:
13471       printf_filtered (_("Static tracepoint"));
13472       printf_filtered (_(" %d"), b->number);
13473       break;
13474     default:
13475       internal_error (__FILE__, __LINE__,
13476                       _("unhandled tracepoint type %d"), (int) b->type);
13477     }
13478
13479   say_where (b);
13480 }
13481
13482 static void
13483 tracepoint_print_recreate (struct breakpoint *self, struct ui_file *fp)
13484 {
13485   struct tracepoint *tp = (struct tracepoint *) self;
13486
13487   if (self->type == bp_fast_tracepoint)
13488     fprintf_unfiltered (fp, "ftrace");
13489   if (self->type == bp_static_tracepoint)
13490     fprintf_unfiltered (fp, "strace");
13491   else if (self->type == bp_tracepoint)
13492     fprintf_unfiltered (fp, "trace");
13493   else
13494     internal_error (__FILE__, __LINE__,
13495                     _("unhandled tracepoint type %d"), (int) self->type);
13496
13497   fprintf_unfiltered (fp, " %s", self->addr_string);
13498   print_recreate_thread (self, fp);
13499
13500   if (tp->pass_count)
13501     fprintf_unfiltered (fp, "  passcount %d\n", tp->pass_count);
13502 }
13503
13504 static void
13505 tracepoint_create_sals_from_address (char **arg,
13506                                      struct linespec_result *canonical,
13507                                      enum bptype type_wanted,
13508                                      char *addr_start, char **copy_arg)
13509 {
13510   create_sals_from_address_default (arg, canonical, type_wanted,
13511                                     addr_start, copy_arg);
13512 }
13513
13514 static void
13515 tracepoint_create_breakpoints_sal (struct gdbarch *gdbarch,
13516                                    struct linespec_result *canonical,
13517                                    struct linespec_sals *lsal,
13518                                    char *cond_string,
13519                                    char *extra_string,
13520                                    enum bptype type_wanted,
13521                                    enum bpdisp disposition,
13522                                    int thread,
13523                                    int task, int ignore_count,
13524                                    const struct breakpoint_ops *ops,
13525                                    int from_tty, int enabled,
13526                                    int internal, unsigned flags)
13527 {
13528   create_breakpoints_sal_default (gdbarch, canonical, lsal,
13529                                   cond_string, extra_string,
13530                                   type_wanted,
13531                                   disposition, thread, task,
13532                                   ignore_count, ops, from_tty,
13533                                   enabled, internal, flags);
13534 }
13535
13536 static void
13537 tracepoint_decode_linespec (struct breakpoint *b, char **s,
13538                             struct symtabs_and_lines *sals)
13539 {
13540   decode_linespec_default (b, s, sals);
13541 }
13542
13543 struct breakpoint_ops tracepoint_breakpoint_ops;
13544
13545 /* The breakpoint_ops structure to be use on tracepoints placed in a
13546    static probe.  */
13547
13548 static void
13549 tracepoint_probe_create_sals_from_address (char **arg,
13550                                            struct linespec_result *canonical,
13551                                            enum bptype type_wanted,
13552                                            char *addr_start, char **copy_arg)
13553 {
13554   /* We use the same method for breakpoint on probes.  */
13555   bkpt_probe_create_sals_from_address (arg, canonical, type_wanted,
13556                                        addr_start, copy_arg);
13557 }
13558
13559 static void
13560 tracepoint_probe_decode_linespec (struct breakpoint *b, char **s,
13561                                   struct symtabs_and_lines *sals)
13562 {
13563   /* We use the same method for breakpoint on probes.  */
13564   bkpt_probe_decode_linespec (b, s, sals);
13565 }
13566
13567 static struct breakpoint_ops tracepoint_probe_breakpoint_ops;
13568
13569 /* Dprintf breakpoint_ops methods.  */
13570
13571 static void
13572 dprintf_re_set (struct breakpoint *b)
13573 {
13574   breakpoint_re_set_default (b);
13575
13576   /* This breakpoint could have been pending, and be resolved now, and
13577      if so, we should now have the extra string.  If we don't, the
13578      dprintf was malformed when created, but we couldn't tell because
13579      we can't extract the extra string until the location is
13580      resolved.  */
13581   if (b->loc != NULL && b->extra_string == NULL)
13582     error (_("Format string required"));
13583
13584   /* 1 - connect to target 1, that can run breakpoint commands.
13585      2 - create a dprintf, which resolves fine.
13586      3 - disconnect from target 1
13587      4 - connect to target 2, that can NOT run breakpoint commands.
13588
13589      After steps #3/#4, you'll want the dprintf command list to
13590      be updated, because target 1 and 2 may well return different
13591      answers for target_can_run_breakpoint_commands().
13592      Given absence of finer grained resetting, we get to do
13593      it all the time.  */
13594   if (b->extra_string != NULL)
13595     update_dprintf_command_list (b);
13596 }
13597
13598 /* The breakpoint_ops structure to be used on static tracepoints with
13599    markers (`-m').  */
13600
13601 static void
13602 strace_marker_create_sals_from_address (char **arg,
13603                                         struct linespec_result *canonical,
13604                                         enum bptype type_wanted,
13605                                         char *addr_start, char **copy_arg)
13606 {
13607   struct linespec_sals lsal;
13608
13609   lsal.sals = decode_static_tracepoint_spec (arg);
13610
13611   *copy_arg = savestring (addr_start, *arg - addr_start);
13612
13613   canonical->addr_string = xstrdup (*copy_arg);
13614   lsal.canonical = xstrdup (*copy_arg);
13615   VEC_safe_push (linespec_sals, canonical->sals, &lsal);
13616 }
13617
13618 static void
13619 strace_marker_create_breakpoints_sal (struct gdbarch *gdbarch,
13620                                       struct linespec_result *canonical,
13621                                       struct linespec_sals *lsal,
13622                                       char *cond_string,
13623                                       char *extra_string,
13624                                       enum bptype type_wanted,
13625                                       enum bpdisp disposition,
13626                                       int thread,
13627                                       int task, int ignore_count,
13628                                       const struct breakpoint_ops *ops,
13629                                       int from_tty, int enabled,
13630                                       int internal, unsigned flags)
13631 {
13632   int i;
13633
13634   /* If the user is creating a static tracepoint by marker id
13635      (strace -m MARKER_ID), then store the sals index, so that
13636      breakpoint_re_set can try to match up which of the newly
13637      found markers corresponds to this one, and, don't try to
13638      expand multiple locations for each sal, given than SALS
13639      already should contain all sals for MARKER_ID.  */
13640
13641   for (i = 0; i < lsal->sals.nelts; ++i)
13642     {
13643       struct symtabs_and_lines expanded;
13644       struct tracepoint *tp;
13645       struct cleanup *old_chain;
13646       char *addr_string;
13647
13648       expanded.nelts = 1;
13649       expanded.sals = &lsal->sals.sals[i];
13650
13651       addr_string = xstrdup (canonical->addr_string);
13652       old_chain = make_cleanup (xfree, addr_string);
13653
13654       tp = XCNEW (struct tracepoint);
13655       init_breakpoint_sal (&tp->base, gdbarch, expanded,
13656                            addr_string, NULL,
13657                            cond_string, extra_string,
13658                            type_wanted, disposition,
13659                            thread, task, ignore_count, ops,
13660                            from_tty, enabled, internal, flags,
13661                            canonical->special_display);
13662       /* Given that its possible to have multiple markers with
13663          the same string id, if the user is creating a static
13664          tracepoint by marker id ("strace -m MARKER_ID"), then
13665          store the sals index, so that breakpoint_re_set can
13666          try to match up which of the newly found markers
13667          corresponds to this one  */
13668       tp->static_trace_marker_id_idx = i;
13669
13670       install_breakpoint (internal, &tp->base, 0);
13671
13672       discard_cleanups (old_chain);
13673     }
13674 }
13675
13676 static void
13677 strace_marker_decode_linespec (struct breakpoint *b, char **s,
13678                                struct symtabs_and_lines *sals)
13679 {
13680   struct tracepoint *tp = (struct tracepoint *) b;
13681
13682   *sals = decode_static_tracepoint_spec (s);
13683   if (sals->nelts > tp->static_trace_marker_id_idx)
13684     {
13685       sals->sals[0] = sals->sals[tp->static_trace_marker_id_idx];
13686       sals->nelts = 1;
13687     }
13688   else
13689     error (_("marker %s not found"), tp->static_trace_marker_id);
13690 }
13691
13692 static struct breakpoint_ops strace_marker_breakpoint_ops;
13693
13694 static int
13695 strace_marker_p (struct breakpoint *b)
13696 {
13697   return b->ops == &strace_marker_breakpoint_ops;
13698 }
13699
13700 /* Delete a breakpoint and clean up all traces of it in the data
13701    structures.  */
13702
13703 void
13704 delete_breakpoint (struct breakpoint *bpt)
13705 {
13706   struct breakpoint *b;
13707
13708   gdb_assert (bpt != NULL);
13709
13710   /* Has this bp already been deleted?  This can happen because
13711      multiple lists can hold pointers to bp's.  bpstat lists are
13712      especial culprits.
13713
13714      One example of this happening is a watchpoint's scope bp.  When
13715      the scope bp triggers, we notice that the watchpoint is out of
13716      scope, and delete it.  We also delete its scope bp.  But the
13717      scope bp is marked "auto-deleting", and is already on a bpstat.
13718      That bpstat is then checked for auto-deleting bp's, which are
13719      deleted.
13720
13721      A real solution to this problem might involve reference counts in
13722      bp's, and/or giving them pointers back to their referencing
13723      bpstat's, and teaching delete_breakpoint to only free a bp's
13724      storage when no more references were extent.  A cheaper bandaid
13725      was chosen.  */
13726   if (bpt->type == bp_none)
13727     return;
13728
13729   /* At least avoid this stale reference until the reference counting
13730      of breakpoints gets resolved.  */
13731   if (bpt->related_breakpoint != bpt)
13732     {
13733       struct breakpoint *related;
13734       struct watchpoint *w;
13735
13736       if (bpt->type == bp_watchpoint_scope)
13737         w = (struct watchpoint *) bpt->related_breakpoint;
13738       else if (bpt->related_breakpoint->type == bp_watchpoint_scope)
13739         w = (struct watchpoint *) bpt;
13740       else
13741         w = NULL;
13742       if (w != NULL)
13743         watchpoint_del_at_next_stop (w);
13744
13745       /* Unlink bpt from the bpt->related_breakpoint ring.  */
13746       for (related = bpt; related->related_breakpoint != bpt;
13747            related = related->related_breakpoint);
13748       related->related_breakpoint = bpt->related_breakpoint;
13749       bpt->related_breakpoint = bpt;
13750     }
13751
13752   /* watch_command_1 creates a watchpoint but only sets its number if
13753      update_watchpoint succeeds in creating its bp_locations.  If there's
13754      a problem in that process, we'll be asked to delete the half-created
13755      watchpoint.  In that case, don't announce the deletion.  */
13756   if (bpt->number)
13757     observer_notify_breakpoint_deleted (bpt);
13758
13759   if (breakpoint_chain == bpt)
13760     breakpoint_chain = bpt->next;
13761
13762   ALL_BREAKPOINTS (b)
13763     if (b->next == bpt)
13764     {
13765       b->next = bpt->next;
13766       break;
13767     }
13768
13769   /* Be sure no bpstat's are pointing at the breakpoint after it's
13770      been freed.  */
13771   /* FIXME, how can we find all bpstat's?  We just check stop_bpstat
13772      in all threads for now.  Note that we cannot just remove bpstats
13773      pointing at bpt from the stop_bpstat list entirely, as breakpoint
13774      commands are associated with the bpstat; if we remove it here,
13775      then the later call to bpstat_do_actions (&stop_bpstat); in
13776      event-top.c won't do anything, and temporary breakpoints with
13777      commands won't work.  */
13778
13779   iterate_over_threads (bpstat_remove_breakpoint_callback, bpt);
13780
13781   /* Now that breakpoint is removed from breakpoint list, update the
13782      global location list.  This will remove locations that used to
13783      belong to this breakpoint.  Do this before freeing the breakpoint
13784      itself, since remove_breakpoint looks at location's owner.  It
13785      might be better design to have location completely
13786      self-contained, but it's not the case now.  */
13787   update_global_location_list (0);
13788
13789   bpt->ops->dtor (bpt);
13790   /* On the chance that someone will soon try again to delete this
13791      same bp, we mark it as deleted before freeing its storage.  */
13792   bpt->type = bp_none;
13793   xfree (bpt);
13794 }
13795
13796 static void
13797 do_delete_breakpoint_cleanup (void *b)
13798 {
13799   delete_breakpoint (b);
13800 }
13801
13802 struct cleanup *
13803 make_cleanup_delete_breakpoint (struct breakpoint *b)
13804 {
13805   return make_cleanup (do_delete_breakpoint_cleanup, b);
13806 }
13807
13808 /* Iterator function to call a user-provided callback function once
13809    for each of B and its related breakpoints.  */
13810
13811 static void
13812 iterate_over_related_breakpoints (struct breakpoint *b,
13813                                   void (*function) (struct breakpoint *,
13814                                                     void *),
13815                                   void *data)
13816 {
13817   struct breakpoint *related;
13818
13819   related = b;
13820   do
13821     {
13822       struct breakpoint *next;
13823
13824       /* FUNCTION may delete RELATED.  */
13825       next = related->related_breakpoint;
13826
13827       if (next == related)
13828         {
13829           /* RELATED is the last ring entry.  */
13830           function (related, data);
13831
13832           /* FUNCTION may have deleted it, so we'd never reach back to
13833              B.  There's nothing left to do anyway, so just break
13834              out.  */
13835           break;
13836         }
13837       else
13838         function (related, data);
13839
13840       related = next;
13841     }
13842   while (related != b);
13843 }
13844
13845 static void
13846 do_delete_breakpoint (struct breakpoint *b, void *ignore)
13847 {
13848   delete_breakpoint (b);
13849 }
13850
13851 /* A callback for map_breakpoint_numbers that calls
13852    delete_breakpoint.  */
13853
13854 static void
13855 do_map_delete_breakpoint (struct breakpoint *b, void *ignore)
13856 {
13857   iterate_over_related_breakpoints (b, do_delete_breakpoint, NULL);
13858 }
13859
13860 void
13861 delete_command (char *arg, int from_tty)
13862 {
13863   struct breakpoint *b, *b_tmp;
13864
13865   dont_repeat ();
13866
13867   if (arg == 0)
13868     {
13869       int breaks_to_delete = 0;
13870
13871       /* Delete all breakpoints if no argument.  Do not delete
13872          internal breakpoints, these have to be deleted with an
13873          explicit breakpoint number argument.  */
13874       ALL_BREAKPOINTS (b)
13875         if (user_breakpoint_p (b))
13876           {
13877             breaks_to_delete = 1;
13878             break;
13879           }
13880
13881       /* Ask user only if there are some breakpoints to delete.  */
13882       if (!from_tty
13883           || (breaks_to_delete && query (_("Delete all breakpoints? "))))
13884         {
13885           ALL_BREAKPOINTS_SAFE (b, b_tmp)
13886             if (user_breakpoint_p (b))
13887               delete_breakpoint (b);
13888         }
13889     }
13890   else
13891     map_breakpoint_numbers (arg, do_map_delete_breakpoint, NULL);
13892 }
13893
13894 static int
13895 all_locations_are_pending (struct bp_location *loc)
13896 {
13897   for (; loc; loc = loc->next)
13898     if (!loc->shlib_disabled
13899         && !loc->pspace->executing_startup)
13900       return 0;
13901   return 1;
13902 }
13903
13904 /* Subroutine of update_breakpoint_locations to simplify it.
13905    Return non-zero if multiple fns in list LOC have the same name.
13906    Null names are ignored.  */
13907
13908 static int
13909 ambiguous_names_p (struct bp_location *loc)
13910 {
13911   struct bp_location *l;
13912   htab_t htab = htab_create_alloc (13, htab_hash_string,
13913                                    (int (*) (const void *, 
13914                                              const void *)) streq,
13915                                    NULL, xcalloc, xfree);
13916
13917   for (l = loc; l != NULL; l = l->next)
13918     {
13919       const char **slot;
13920       const char *name = l->function_name;
13921
13922       /* Allow for some names to be NULL, ignore them.  */
13923       if (name == NULL)
13924         continue;
13925
13926       slot = (const char **) htab_find_slot (htab, (const void *) name,
13927                                              INSERT);
13928       /* NOTE: We can assume slot != NULL here because xcalloc never
13929          returns NULL.  */
13930       if (*slot != NULL)
13931         {
13932           htab_delete (htab);
13933           return 1;
13934         }
13935       *slot = name;
13936     }
13937
13938   htab_delete (htab);
13939   return 0;
13940 }
13941
13942 /* When symbols change, it probably means the sources changed as well,
13943    and it might mean the static tracepoint markers are no longer at
13944    the same address or line numbers they used to be at last we
13945    checked.  Losing your static tracepoints whenever you rebuild is
13946    undesirable.  This function tries to resync/rematch gdb static
13947    tracepoints with the markers on the target, for static tracepoints
13948    that have not been set by marker id.  Static tracepoint that have
13949    been set by marker id are reset by marker id in breakpoint_re_set.
13950    The heuristic is:
13951
13952    1) For a tracepoint set at a specific address, look for a marker at
13953    the old PC.  If one is found there, assume to be the same marker.
13954    If the name / string id of the marker found is different from the
13955    previous known name, assume that means the user renamed the marker
13956    in the sources, and output a warning.
13957
13958    2) For a tracepoint set at a given line number, look for a marker
13959    at the new address of the old line number.  If one is found there,
13960    assume to be the same marker.  If the name / string id of the
13961    marker found is different from the previous known name, assume that
13962    means the user renamed the marker in the sources, and output a
13963    warning.
13964
13965    3) If a marker is no longer found at the same address or line, it
13966    may mean the marker no longer exists.  But it may also just mean
13967    the code changed a bit.  Maybe the user added a few lines of code
13968    that made the marker move up or down (in line number terms).  Ask
13969    the target for info about the marker with the string id as we knew
13970    it.  If found, update line number and address in the matching
13971    static tracepoint.  This will get confused if there's more than one
13972    marker with the same ID (possible in UST, although unadvised
13973    precisely because it confuses tools).  */
13974
13975 static struct symtab_and_line
13976 update_static_tracepoint (struct breakpoint *b, struct symtab_and_line sal)
13977 {
13978   struct tracepoint *tp = (struct tracepoint *) b;
13979   struct static_tracepoint_marker marker;
13980   CORE_ADDR pc;
13981
13982   pc = sal.pc;
13983   if (sal.line)
13984     find_line_pc (sal.symtab, sal.line, &pc);
13985
13986   if (target_static_tracepoint_marker_at (pc, &marker))
13987     {
13988       if (strcmp (tp->static_trace_marker_id, marker.str_id) != 0)
13989         warning (_("static tracepoint %d changed probed marker from %s to %s"),
13990                  b->number,
13991                  tp->static_trace_marker_id, marker.str_id);
13992
13993       xfree (tp->static_trace_marker_id);
13994       tp->static_trace_marker_id = xstrdup (marker.str_id);
13995       release_static_tracepoint_marker (&marker);
13996
13997       return sal;
13998     }
13999
14000   /* Old marker wasn't found on target at lineno.  Try looking it up
14001      by string ID.  */
14002   if (!sal.explicit_pc
14003       && sal.line != 0
14004       && sal.symtab != NULL
14005       && tp->static_trace_marker_id != NULL)
14006     {
14007       VEC(static_tracepoint_marker_p) *markers;
14008
14009       markers
14010         = target_static_tracepoint_markers_by_strid (tp->static_trace_marker_id);
14011
14012       if (!VEC_empty(static_tracepoint_marker_p, markers))
14013         {
14014           struct symtab_and_line sal2;
14015           struct symbol *sym;
14016           struct static_tracepoint_marker *tpmarker;
14017           struct ui_out *uiout = current_uiout;
14018
14019           tpmarker = VEC_index (static_tracepoint_marker_p, markers, 0);
14020
14021           xfree (tp->static_trace_marker_id);
14022           tp->static_trace_marker_id = xstrdup (tpmarker->str_id);
14023
14024           warning (_("marker for static tracepoint %d (%s) not "
14025                      "found at previous line number"),
14026                    b->number, tp->static_trace_marker_id);
14027
14028           init_sal (&sal2);
14029
14030           sal2.pc = tpmarker->address;
14031
14032           sal2 = find_pc_line (tpmarker->address, 0);
14033           sym = find_pc_sect_function (tpmarker->address, NULL);
14034           ui_out_text (uiout, "Now in ");
14035           if (sym)
14036             {
14037               ui_out_field_string (uiout, "func",
14038                                    SYMBOL_PRINT_NAME (sym));
14039               ui_out_text (uiout, " at ");
14040             }
14041           ui_out_field_string (uiout, "file",
14042                                symtab_to_filename_for_display (sal2.symtab));
14043           ui_out_text (uiout, ":");
14044
14045           if (ui_out_is_mi_like_p (uiout))
14046             {
14047               const char *fullname = symtab_to_fullname (sal2.symtab);
14048
14049               ui_out_field_string (uiout, "fullname", fullname);
14050             }
14051
14052           ui_out_field_int (uiout, "line", sal2.line);
14053           ui_out_text (uiout, "\n");
14054
14055           b->loc->line_number = sal2.line;
14056           b->loc->symtab = sym != NULL ? sal2.symtab : NULL;
14057
14058           xfree (b->addr_string);
14059           b->addr_string = xstrprintf ("%s:%d",
14060                                    symtab_to_filename_for_display (sal2.symtab),
14061                                        b->loc->line_number);
14062
14063           /* Might be nice to check if function changed, and warn if
14064              so.  */
14065
14066           release_static_tracepoint_marker (tpmarker);
14067         }
14068     }
14069   return sal;
14070 }
14071
14072 /* Returns 1 iff locations A and B are sufficiently same that
14073    we don't need to report breakpoint as changed.  */
14074
14075 static int
14076 locations_are_equal (struct bp_location *a, struct bp_location *b)
14077 {
14078   while (a && b)
14079     {
14080       if (a->address != b->address)
14081         return 0;
14082
14083       if (a->shlib_disabled != b->shlib_disabled)
14084         return 0;
14085
14086       if (a->enabled != b->enabled)
14087         return 0;
14088
14089       a = a->next;
14090       b = b->next;
14091     }
14092
14093   if ((a == NULL) != (b == NULL))
14094     return 0;
14095
14096   return 1;
14097 }
14098
14099 /* Create new breakpoint locations for B (a hardware or software breakpoint)
14100    based on SALS and SALS_END.  If SALS_END.NELTS is not zero, then B is
14101    a ranged breakpoint.  */
14102
14103 void
14104 update_breakpoint_locations (struct breakpoint *b,
14105                              struct symtabs_and_lines sals,
14106                              struct symtabs_and_lines sals_end)
14107 {
14108   int i;
14109   struct bp_location *existing_locations = b->loc;
14110
14111   if (sals_end.nelts != 0 && (sals.nelts != 1 || sals_end.nelts != 1))
14112     {
14113       /* Ranged breakpoints have only one start location and one end
14114          location.  */
14115       b->enable_state = bp_disabled;
14116       update_global_location_list (1);
14117       printf_unfiltered (_("Could not reset ranged breakpoint %d: "
14118                            "multiple locations found\n"),
14119                          b->number);
14120       return;
14121     }
14122
14123   /* If there's no new locations, and all existing locations are
14124      pending, don't do anything.  This optimizes the common case where
14125      all locations are in the same shared library, that was unloaded.
14126      We'd like to retain the location, so that when the library is
14127      loaded again, we don't loose the enabled/disabled status of the
14128      individual locations.  */
14129   if (all_locations_are_pending (existing_locations) && sals.nelts == 0)
14130     return;
14131
14132   b->loc = NULL;
14133
14134   for (i = 0; i < sals.nelts; ++i)
14135     {
14136       struct bp_location *new_loc;
14137
14138       switch_to_program_space_and_thread (sals.sals[i].pspace);
14139
14140       new_loc = add_location_to_breakpoint (b, &(sals.sals[i]));
14141
14142       /* Reparse conditions, they might contain references to the
14143          old symtab.  */
14144       if (b->cond_string != NULL)
14145         {
14146           const char *s;
14147           volatile struct gdb_exception e;
14148
14149           s = b->cond_string;
14150           TRY_CATCH (e, RETURN_MASK_ERROR)
14151             {
14152               new_loc->cond = parse_exp_1 (&s, sals.sals[i].pc,
14153                                            block_for_pc (sals.sals[i].pc), 
14154                                            0);
14155             }
14156           if (e.reason < 0)
14157             {
14158               warning (_("failed to reevaluate condition "
14159                          "for breakpoint %d: %s"), 
14160                        b->number, e.message);
14161               new_loc->enabled = 0;
14162             }
14163         }
14164
14165       if (sals_end.nelts)
14166         {
14167           CORE_ADDR end = find_breakpoint_range_end (sals_end.sals[0]);
14168
14169           new_loc->length = end - sals.sals[0].pc + 1;
14170         }
14171     }
14172
14173   /* Update locations of permanent breakpoints.  */
14174   if (b->enable_state == bp_permanent)
14175     make_breakpoint_permanent (b);
14176
14177   /* If possible, carry over 'disable' status from existing
14178      breakpoints.  */
14179   {
14180     struct bp_location *e = existing_locations;
14181     /* If there are multiple breakpoints with the same function name,
14182        e.g. for inline functions, comparing function names won't work.
14183        Instead compare pc addresses; this is just a heuristic as things
14184        may have moved, but in practice it gives the correct answer
14185        often enough until a better solution is found.  */
14186     int have_ambiguous_names = ambiguous_names_p (b->loc);
14187
14188     for (; e; e = e->next)
14189       {
14190         if (!e->enabled && e->function_name)
14191           {
14192             struct bp_location *l = b->loc;
14193             if (have_ambiguous_names)
14194               {
14195                 for (; l; l = l->next)
14196                   if (breakpoint_locations_match (e, l))
14197                     {
14198                       l->enabled = 0;
14199                       break;
14200                     }
14201               }
14202             else
14203               {
14204                 for (; l; l = l->next)
14205                   if (l->function_name
14206                       && strcmp (e->function_name, l->function_name) == 0)
14207                     {
14208                       l->enabled = 0;
14209                       break;
14210                     }
14211               }
14212           }
14213       }
14214   }
14215
14216   if (!locations_are_equal (existing_locations, b->loc))
14217     observer_notify_breakpoint_modified (b);
14218
14219   update_global_location_list (1);
14220 }
14221
14222 /* Find the SaL locations corresponding to the given ADDR_STRING.
14223    On return, FOUND will be 1 if any SaL was found, zero otherwise.  */
14224
14225 static struct symtabs_and_lines
14226 addr_string_to_sals (struct breakpoint *b, char *addr_string, int *found)
14227 {
14228   char *s;
14229   struct symtabs_and_lines sals = {0};
14230   volatile struct gdb_exception e;
14231
14232   gdb_assert (b->ops != NULL);
14233   s = addr_string;
14234
14235   TRY_CATCH (e, RETURN_MASK_ERROR)
14236     {
14237       b->ops->decode_linespec (b, &s, &sals);
14238     }
14239   if (e.reason < 0)
14240     {
14241       int not_found_and_ok = 0;
14242       /* For pending breakpoints, it's expected that parsing will
14243          fail until the right shared library is loaded.  User has
14244          already told to create pending breakpoints and don't need
14245          extra messages.  If breakpoint is in bp_shlib_disabled
14246          state, then user already saw the message about that
14247          breakpoint being disabled, and don't want to see more
14248          errors.  */
14249       if (e.error == NOT_FOUND_ERROR
14250           && (b->condition_not_parsed 
14251               || (b->loc && b->loc->shlib_disabled)
14252               || (b->loc && b->loc->pspace->executing_startup)
14253               || b->enable_state == bp_disabled))
14254         not_found_and_ok = 1;
14255
14256       if (!not_found_and_ok)
14257         {
14258           /* We surely don't want to warn about the same breakpoint
14259              10 times.  One solution, implemented here, is disable
14260              the breakpoint on error.  Another solution would be to
14261              have separate 'warning emitted' flag.  Since this
14262              happens only when a binary has changed, I don't know
14263              which approach is better.  */
14264           b->enable_state = bp_disabled;
14265           throw_exception (e);
14266         }
14267     }
14268
14269   if (e.reason == 0 || e.error != NOT_FOUND_ERROR)
14270     {
14271       int i;
14272
14273       for (i = 0; i < sals.nelts; ++i)
14274         resolve_sal_pc (&sals.sals[i]);
14275       if (b->condition_not_parsed && s && s[0])
14276         {
14277           char *cond_string, *extra_string;
14278           int thread, task;
14279
14280           find_condition_and_thread (s, sals.sals[0].pc,
14281                                      &cond_string, &thread, &task,
14282                                      &extra_string);
14283           if (cond_string)
14284             b->cond_string = cond_string;
14285           b->thread = thread;
14286           b->task = task;
14287           if (extra_string)
14288             b->extra_string = extra_string;
14289           b->condition_not_parsed = 0;
14290         }
14291
14292       if (b->type == bp_static_tracepoint && !strace_marker_p (b))
14293         sals.sals[0] = update_static_tracepoint (b, sals.sals[0]);
14294
14295       *found = 1;
14296     }
14297   else
14298     *found = 0;
14299
14300   return sals;
14301 }
14302
14303 /* The default re_set method, for typical hardware or software
14304    breakpoints.  Reevaluate the breakpoint and recreate its
14305    locations.  */
14306
14307 static void
14308 breakpoint_re_set_default (struct breakpoint *b)
14309 {
14310   int found;
14311   struct symtabs_and_lines sals, sals_end;
14312   struct symtabs_and_lines expanded = {0};
14313   struct symtabs_and_lines expanded_end = {0};
14314
14315   sals = addr_string_to_sals (b, b->addr_string, &found);
14316   if (found)
14317     {
14318       make_cleanup (xfree, sals.sals);
14319       expanded = sals;
14320     }
14321
14322   if (b->addr_string_range_end)
14323     {
14324       sals_end = addr_string_to_sals (b, b->addr_string_range_end, &found);
14325       if (found)
14326         {
14327           make_cleanup (xfree, sals_end.sals);
14328           expanded_end = sals_end;
14329         }
14330     }
14331
14332   update_breakpoint_locations (b, expanded, expanded_end);
14333 }
14334
14335 /* Default method for creating SALs from an address string.  It basically
14336    calls parse_breakpoint_sals.  Return 1 for success, zero for failure.  */
14337
14338 static void
14339 create_sals_from_address_default (char **arg,
14340                                   struct linespec_result *canonical,
14341                                   enum bptype type_wanted,
14342                                   char *addr_start, char **copy_arg)
14343 {
14344   parse_breakpoint_sals (arg, canonical);
14345 }
14346
14347 /* Call create_breakpoints_sal for the given arguments.  This is the default
14348    function for the `create_breakpoints_sal' method of
14349    breakpoint_ops.  */
14350
14351 static void
14352 create_breakpoints_sal_default (struct gdbarch *gdbarch,
14353                                 struct linespec_result *canonical,
14354                                 struct linespec_sals *lsal,
14355                                 char *cond_string,
14356                                 char *extra_string,
14357                                 enum bptype type_wanted,
14358                                 enum bpdisp disposition,
14359                                 int thread,
14360                                 int task, int ignore_count,
14361                                 const struct breakpoint_ops *ops,
14362                                 int from_tty, int enabled,
14363                                 int internal, unsigned flags)
14364 {
14365   create_breakpoints_sal (gdbarch, canonical, cond_string,
14366                           extra_string,
14367                           type_wanted, disposition,
14368                           thread, task, ignore_count, ops, from_tty,
14369                           enabled, internal, flags);
14370 }
14371
14372 /* Decode the line represented by S by calling decode_line_full.  This is the
14373    default function for the `decode_linespec' method of breakpoint_ops.  */
14374
14375 static void
14376 decode_linespec_default (struct breakpoint *b, char **s,
14377                          struct symtabs_and_lines *sals)
14378 {
14379   struct linespec_result canonical;
14380
14381   init_linespec_result (&canonical);
14382   decode_line_full (s, DECODE_LINE_FUNFIRSTLINE,
14383                     (struct symtab *) NULL, 0,
14384                     &canonical, multiple_symbols_all,
14385                     b->filter);
14386
14387   /* We should get 0 or 1 resulting SALs.  */
14388   gdb_assert (VEC_length (linespec_sals, canonical.sals) < 2);
14389
14390   if (VEC_length (linespec_sals, canonical.sals) > 0)
14391     {
14392       struct linespec_sals *lsal;
14393
14394       lsal = VEC_index (linespec_sals, canonical.sals, 0);
14395       *sals = lsal->sals;
14396       /* Arrange it so the destructor does not free the
14397          contents.  */
14398       lsal->sals.sals = NULL;
14399     }
14400
14401   destroy_linespec_result (&canonical);
14402 }
14403
14404 /* Prepare the global context for a re-set of breakpoint B.  */
14405
14406 static struct cleanup *
14407 prepare_re_set_context (struct breakpoint *b)
14408 {
14409   struct cleanup *cleanups;
14410
14411   input_radix = b->input_radix;
14412   cleanups = save_current_space_and_thread ();
14413   if (b->pspace != NULL)
14414     switch_to_program_space_and_thread (b->pspace);
14415   set_language (b->language);
14416
14417   return cleanups;
14418 }
14419
14420 /* Reset a breakpoint given it's struct breakpoint * BINT.
14421    The value we return ends up being the return value from catch_errors.
14422    Unused in this case.  */
14423
14424 static int
14425 breakpoint_re_set_one (void *bint)
14426 {
14427   /* Get past catch_errs.  */
14428   struct breakpoint *b = (struct breakpoint *) bint;
14429   struct cleanup *cleanups;
14430
14431   cleanups = prepare_re_set_context (b);
14432   b->ops->re_set (b);
14433   do_cleanups (cleanups);
14434   return 0;
14435 }
14436
14437 /* Re-set all breakpoints after symbols have been re-loaded.  */
14438 void
14439 breakpoint_re_set (void)
14440 {
14441   struct breakpoint *b, *b_tmp;
14442   enum language save_language;
14443   int save_input_radix;
14444   struct cleanup *old_chain;
14445
14446   save_language = current_language->la_language;
14447   save_input_radix = input_radix;
14448   old_chain = save_current_program_space ();
14449
14450   ALL_BREAKPOINTS_SAFE (b, b_tmp)
14451   {
14452     /* Format possible error msg.  */
14453     char *message = xstrprintf ("Error in re-setting breakpoint %d: ",
14454                                 b->number);
14455     struct cleanup *cleanups = make_cleanup (xfree, message);
14456     catch_errors (breakpoint_re_set_one, b, message, RETURN_MASK_ALL);
14457     do_cleanups (cleanups);
14458   }
14459   set_language (save_language);
14460   input_radix = save_input_radix;
14461
14462   jit_breakpoint_re_set ();
14463
14464   do_cleanups (old_chain);
14465
14466   create_overlay_event_breakpoint ();
14467   create_longjmp_master_breakpoint ();
14468   create_std_terminate_master_breakpoint ();
14469   create_exception_master_breakpoint ();
14470 }
14471 \f
14472 /* Reset the thread number of this breakpoint:
14473
14474    - If the breakpoint is for all threads, leave it as-is.
14475    - Else, reset it to the current thread for inferior_ptid.  */
14476 void
14477 breakpoint_re_set_thread (struct breakpoint *b)
14478 {
14479   if (b->thread != -1)
14480     {
14481       if (in_thread_list (inferior_ptid))
14482         b->thread = pid_to_thread_id (inferior_ptid);
14483
14484       /* We're being called after following a fork.  The new fork is
14485          selected as current, and unless this was a vfork will have a
14486          different program space from the original thread.  Reset that
14487          as well.  */
14488       b->loc->pspace = current_program_space;
14489     }
14490 }
14491
14492 /* Set ignore-count of breakpoint number BPTNUM to COUNT.
14493    If from_tty is nonzero, it prints a message to that effect,
14494    which ends with a period (no newline).  */
14495
14496 void
14497 set_ignore_count (int bptnum, int count, int from_tty)
14498 {
14499   struct breakpoint *b;
14500
14501   if (count < 0)
14502     count = 0;
14503
14504   ALL_BREAKPOINTS (b)
14505     if (b->number == bptnum)
14506     {
14507       if (is_tracepoint (b))
14508         {
14509           if (from_tty && count != 0)
14510             printf_filtered (_("Ignore count ignored for tracepoint %d."),
14511                              bptnum);
14512           return;
14513         }
14514       
14515       b->ignore_count = count;
14516       if (from_tty)
14517         {
14518           if (count == 0)
14519             printf_filtered (_("Will stop next time "
14520                                "breakpoint %d is reached."),
14521                              bptnum);
14522           else if (count == 1)
14523             printf_filtered (_("Will ignore next crossing of breakpoint %d."),
14524                              bptnum);
14525           else
14526             printf_filtered (_("Will ignore next %d "
14527                                "crossings of breakpoint %d."),
14528                              count, bptnum);
14529         }
14530       observer_notify_breakpoint_modified (b);
14531       return;
14532     }
14533
14534   error (_("No breakpoint number %d."), bptnum);
14535 }
14536
14537 /* Command to set ignore-count of breakpoint N to COUNT.  */
14538
14539 static void
14540 ignore_command (char *args, int from_tty)
14541 {
14542   char *p = args;
14543   int num;
14544
14545   if (p == 0)
14546     error_no_arg (_("a breakpoint number"));
14547
14548   num = get_number (&p);
14549   if (num == 0)
14550     error (_("bad breakpoint number: '%s'"), args);
14551   if (*p == 0)
14552     error (_("Second argument (specified ignore-count) is missing."));
14553
14554   set_ignore_count (num,
14555                     longest_to_int (value_as_long (parse_and_eval (p))),
14556                     from_tty);
14557   if (from_tty)
14558     printf_filtered ("\n");
14559 }
14560 \f
14561 /* Call FUNCTION on each of the breakpoints
14562    whose numbers are given in ARGS.  */
14563
14564 static void
14565 map_breakpoint_numbers (char *args, void (*function) (struct breakpoint *,
14566                                                       void *),
14567                         void *data)
14568 {
14569   int num;
14570   struct breakpoint *b, *tmp;
14571   int match;
14572   struct get_number_or_range_state state;
14573
14574   if (args == 0)
14575     error_no_arg (_("one or more breakpoint numbers"));
14576
14577   init_number_or_range (&state, args);
14578
14579   while (!state.finished)
14580     {
14581       char *p = state.string;
14582
14583       match = 0;
14584
14585       num = get_number_or_range (&state);
14586       if (num == 0)
14587         {
14588           warning (_("bad breakpoint number at or near '%s'"), p);
14589         }
14590       else
14591         {
14592           ALL_BREAKPOINTS_SAFE (b, tmp)
14593             if (b->number == num)
14594               {
14595                 match = 1;
14596                 function (b, data);
14597                 break;
14598               }
14599           if (match == 0)
14600             printf_unfiltered (_("No breakpoint number %d.\n"), num);
14601         }
14602     }
14603 }
14604
14605 static struct bp_location *
14606 find_location_by_number (char *number)
14607 {
14608   char *dot = strchr (number, '.');
14609   char *p1;
14610   int bp_num;
14611   int loc_num;
14612   struct breakpoint *b;
14613   struct bp_location *loc;  
14614
14615   *dot = '\0';
14616
14617   p1 = number;
14618   bp_num = get_number (&p1);
14619   if (bp_num == 0)
14620     error (_("Bad breakpoint number '%s'"), number);
14621
14622   ALL_BREAKPOINTS (b)
14623     if (b->number == bp_num)
14624       {
14625         break;
14626       }
14627
14628   if (!b || b->number != bp_num)
14629     error (_("Bad breakpoint number '%s'"), number);
14630   
14631   p1 = dot+1;
14632   loc_num = get_number (&p1);
14633   if (loc_num == 0)
14634     error (_("Bad breakpoint location number '%s'"), number);
14635
14636   --loc_num;
14637   loc = b->loc;
14638   for (;loc_num && loc; --loc_num, loc = loc->next)
14639     ;
14640   if (!loc)
14641     error (_("Bad breakpoint location number '%s'"), dot+1);
14642     
14643   return loc;  
14644 }
14645
14646
14647 /* Set ignore-count of breakpoint number BPTNUM to COUNT.
14648    If from_tty is nonzero, it prints a message to that effect,
14649    which ends with a period (no newline).  */
14650
14651 void
14652 disable_breakpoint (struct breakpoint *bpt)
14653 {
14654   /* Never disable a watchpoint scope breakpoint; we want to
14655      hit them when we leave scope so we can delete both the
14656      watchpoint and its scope breakpoint at that time.  */
14657   if (bpt->type == bp_watchpoint_scope)
14658     return;
14659
14660   /* You can't disable permanent breakpoints.  */
14661   if (bpt->enable_state == bp_permanent)
14662     return;
14663
14664   bpt->enable_state = bp_disabled;
14665
14666   /* Mark breakpoint locations modified.  */
14667   mark_breakpoint_modified (bpt);
14668
14669   if (target_supports_enable_disable_tracepoint ()
14670       && current_trace_status ()->running && is_tracepoint (bpt))
14671     {
14672       struct bp_location *location;
14673      
14674       for (location = bpt->loc; location; location = location->next)
14675         target_disable_tracepoint (location);
14676     }
14677
14678   update_global_location_list (0);
14679
14680   observer_notify_breakpoint_modified (bpt);
14681 }
14682
14683 /* A callback for iterate_over_related_breakpoints.  */
14684
14685 static void
14686 do_disable_breakpoint (struct breakpoint *b, void *ignore)
14687 {
14688   disable_breakpoint (b);
14689 }
14690
14691 /* A callback for map_breakpoint_numbers that calls
14692    disable_breakpoint.  */
14693
14694 static void
14695 do_map_disable_breakpoint (struct breakpoint *b, void *ignore)
14696 {
14697   iterate_over_related_breakpoints (b, do_disable_breakpoint, NULL);
14698 }
14699
14700 static void
14701 disable_command (char *args, int from_tty)
14702 {
14703   if (args == 0)
14704     {
14705       struct breakpoint *bpt;
14706
14707       ALL_BREAKPOINTS (bpt)
14708         if (user_breakpoint_p (bpt))
14709           disable_breakpoint (bpt);
14710     }
14711   else if (strchr (args, '.'))
14712     {
14713       struct bp_location *loc = find_location_by_number (args);
14714       if (loc)
14715         {
14716           if (loc->enabled)
14717             {
14718               loc->enabled = 0;
14719               mark_breakpoint_location_modified (loc);
14720             }
14721           if (target_supports_enable_disable_tracepoint ()
14722               && current_trace_status ()->running && loc->owner
14723               && is_tracepoint (loc->owner))
14724             target_disable_tracepoint (loc);
14725         }
14726       update_global_location_list (0);
14727     }
14728   else
14729     map_breakpoint_numbers (args, do_map_disable_breakpoint, NULL);
14730 }
14731
14732 static void
14733 enable_breakpoint_disp (struct breakpoint *bpt, enum bpdisp disposition,
14734                         int count)
14735 {
14736   int target_resources_ok;
14737
14738   if (bpt->type == bp_hardware_breakpoint)
14739     {
14740       int i;
14741       i = hw_breakpoint_used_count ();
14742       target_resources_ok = 
14743         target_can_use_hardware_watchpoint (bp_hardware_breakpoint, 
14744                                             i + 1, 0);
14745       if (target_resources_ok == 0)
14746         error (_("No hardware breakpoint support in the target."));
14747       else if (target_resources_ok < 0)
14748         error (_("Hardware breakpoints used exceeds limit."));
14749     }
14750
14751   if (is_watchpoint (bpt))
14752     {
14753       /* Initialize it just to avoid a GCC false warning.  */
14754       enum enable_state orig_enable_state = 0;
14755       volatile struct gdb_exception e;
14756
14757       TRY_CATCH (e, RETURN_MASK_ALL)
14758         {
14759           struct watchpoint *w = (struct watchpoint *) bpt;
14760
14761           orig_enable_state = bpt->enable_state;
14762           bpt->enable_state = bp_enabled;
14763           update_watchpoint (w, 1 /* reparse */);
14764         }
14765       if (e.reason < 0)
14766         {
14767           bpt->enable_state = orig_enable_state;
14768           exception_fprintf (gdb_stderr, e, _("Cannot enable watchpoint %d: "),
14769                              bpt->number);
14770           return;
14771         }
14772     }
14773
14774   if (bpt->enable_state != bp_permanent)
14775     bpt->enable_state = bp_enabled;
14776
14777   bpt->enable_state = bp_enabled;
14778
14779   /* Mark breakpoint locations modified.  */
14780   mark_breakpoint_modified (bpt);
14781
14782   if (target_supports_enable_disable_tracepoint ()
14783       && current_trace_status ()->running && is_tracepoint (bpt))
14784     {
14785       struct bp_location *location;
14786
14787       for (location = bpt->loc; location; location = location->next)
14788         target_enable_tracepoint (location);
14789     }
14790
14791   bpt->disposition = disposition;
14792   bpt->enable_count = count;
14793   update_global_location_list (1);
14794
14795   observer_notify_breakpoint_modified (bpt);
14796 }
14797
14798
14799 void
14800 enable_breakpoint (struct breakpoint *bpt)
14801 {
14802   enable_breakpoint_disp (bpt, bpt->disposition, 0);
14803 }
14804
14805 static void
14806 do_enable_breakpoint (struct breakpoint *bpt, void *arg)
14807 {
14808   enable_breakpoint (bpt);
14809 }
14810
14811 /* A callback for map_breakpoint_numbers that calls
14812    enable_breakpoint.  */
14813
14814 static void
14815 do_map_enable_breakpoint (struct breakpoint *b, void *ignore)
14816 {
14817   iterate_over_related_breakpoints (b, do_enable_breakpoint, NULL);
14818 }
14819
14820 /* The enable command enables the specified breakpoints (or all defined
14821    breakpoints) so they once again become (or continue to be) effective
14822    in stopping the inferior.  */
14823
14824 static void
14825 enable_command (char *args, int from_tty)
14826 {
14827   if (args == 0)
14828     {
14829       struct breakpoint *bpt;
14830
14831       ALL_BREAKPOINTS (bpt)
14832         if (user_breakpoint_p (bpt))
14833           enable_breakpoint (bpt);
14834     }
14835   else if (strchr (args, '.'))
14836     {
14837       struct bp_location *loc = find_location_by_number (args);
14838       if (loc)
14839         {
14840           if (!loc->enabled)
14841             {
14842               loc->enabled = 1;
14843               mark_breakpoint_location_modified (loc);
14844             }
14845           if (target_supports_enable_disable_tracepoint ()
14846               && current_trace_status ()->running && loc->owner
14847               && is_tracepoint (loc->owner))
14848             target_enable_tracepoint (loc);
14849         }
14850       update_global_location_list (1);
14851     }
14852   else
14853     map_breakpoint_numbers (args, do_map_enable_breakpoint, NULL);
14854 }
14855
14856 /* This struct packages up disposition data for application to multiple
14857    breakpoints.  */
14858
14859 struct disp_data
14860 {
14861   enum bpdisp disp;
14862   int count;
14863 };
14864
14865 static void
14866 do_enable_breakpoint_disp (struct breakpoint *bpt, void *arg)
14867 {
14868   struct disp_data disp_data = *(struct disp_data *) arg;
14869
14870   enable_breakpoint_disp (bpt, disp_data.disp, disp_data.count);
14871 }
14872
14873 static void
14874 do_map_enable_once_breakpoint (struct breakpoint *bpt, void *ignore)
14875 {
14876   struct disp_data disp = { disp_disable, 1 };
14877
14878   iterate_over_related_breakpoints (bpt, do_enable_breakpoint_disp, &disp);
14879 }
14880
14881 static void
14882 enable_once_command (char *args, int from_tty)
14883 {
14884   map_breakpoint_numbers (args, do_map_enable_once_breakpoint, NULL);
14885 }
14886
14887 static void
14888 do_map_enable_count_breakpoint (struct breakpoint *bpt, void *countptr)
14889 {
14890   struct disp_data disp = { disp_disable, *(int *) countptr };
14891
14892   iterate_over_related_breakpoints (bpt, do_enable_breakpoint_disp, &disp);
14893 }
14894
14895 static void
14896 enable_count_command (char *args, int from_tty)
14897 {
14898   int count = get_number (&args);
14899
14900   map_breakpoint_numbers (args, do_map_enable_count_breakpoint, &count);
14901 }
14902
14903 static void
14904 do_map_enable_delete_breakpoint (struct breakpoint *bpt, void *ignore)
14905 {
14906   struct disp_data disp = { disp_del, 1 };
14907
14908   iterate_over_related_breakpoints (bpt, do_enable_breakpoint_disp, &disp);
14909 }
14910
14911 static void
14912 enable_delete_command (char *args, int from_tty)
14913 {
14914   map_breakpoint_numbers (args, do_map_enable_delete_breakpoint, NULL);
14915 }
14916 \f
14917 static void
14918 set_breakpoint_cmd (char *args, int from_tty)
14919 {
14920 }
14921
14922 static void
14923 show_breakpoint_cmd (char *args, int from_tty)
14924 {
14925 }
14926
14927 /* Invalidate last known value of any hardware watchpoint if
14928    the memory which that value represents has been written to by
14929    GDB itself.  */
14930
14931 static void
14932 invalidate_bp_value_on_memory_change (struct inferior *inferior,
14933                                       CORE_ADDR addr, ssize_t len,
14934                                       const bfd_byte *data)
14935 {
14936   struct breakpoint *bp;
14937
14938   ALL_BREAKPOINTS (bp)
14939     if (bp->enable_state == bp_enabled
14940         && bp->type == bp_hardware_watchpoint)
14941       {
14942         struct watchpoint *wp = (struct watchpoint *) bp;
14943
14944         if (wp->val_valid && wp->val)
14945           {
14946             struct bp_location *loc;
14947
14948             for (loc = bp->loc; loc != NULL; loc = loc->next)
14949               if (loc->loc_type == bp_loc_hardware_watchpoint
14950                   && loc->address + loc->length > addr
14951                   && addr + len > loc->address)
14952                 {
14953                   value_free (wp->val);
14954                   wp->val = NULL;
14955                   wp->val_valid = 0;
14956                 }
14957           }
14958       }
14959 }
14960
14961 /* Create and insert a raw software breakpoint at PC.  Return an
14962    identifier, which should be used to remove the breakpoint later.
14963    In general, places which call this should be using something on the
14964    breakpoint chain instead; this function should be eliminated
14965    someday.  */
14966
14967 void *
14968 deprecated_insert_raw_breakpoint (struct gdbarch *gdbarch,
14969                                   struct address_space *aspace, CORE_ADDR pc)
14970 {
14971   struct bp_target_info *bp_tgt;
14972
14973   bp_tgt = XZALLOC (struct bp_target_info);
14974
14975   bp_tgt->placed_address_space = aspace;
14976   bp_tgt->placed_address = pc;
14977
14978   if (target_insert_breakpoint (gdbarch, bp_tgt) != 0)
14979     {
14980       /* Could not insert the breakpoint.  */
14981       xfree (bp_tgt);
14982       return NULL;
14983     }
14984
14985   return bp_tgt;
14986 }
14987
14988 /* Remove a breakpoint BP inserted by
14989    deprecated_insert_raw_breakpoint.  */
14990
14991 int
14992 deprecated_remove_raw_breakpoint (struct gdbarch *gdbarch, void *bp)
14993 {
14994   struct bp_target_info *bp_tgt = bp;
14995   int ret;
14996
14997   ret = target_remove_breakpoint (gdbarch, bp_tgt);
14998   xfree (bp_tgt);
14999
15000   return ret;
15001 }
15002
15003 /* One (or perhaps two) breakpoints used for software single
15004    stepping.  */
15005
15006 static void *single_step_breakpoints[2];
15007 static struct gdbarch *single_step_gdbarch[2];
15008
15009 /* Create and insert a breakpoint for software single step.  */
15010
15011 void
15012 insert_single_step_breakpoint (struct gdbarch *gdbarch,
15013                                struct address_space *aspace, 
15014                                CORE_ADDR next_pc)
15015 {
15016   void **bpt_p;
15017
15018   if (single_step_breakpoints[0] == NULL)
15019     {
15020       bpt_p = &single_step_breakpoints[0];
15021       single_step_gdbarch[0] = gdbarch;
15022     }
15023   else
15024     {
15025       gdb_assert (single_step_breakpoints[1] == NULL);
15026       bpt_p = &single_step_breakpoints[1];
15027       single_step_gdbarch[1] = gdbarch;
15028     }
15029
15030   /* NOTE drow/2006-04-11: A future improvement to this function would
15031      be to only create the breakpoints once, and actually put them on
15032      the breakpoint chain.  That would let us use set_raw_breakpoint.
15033      We could adjust the addresses each time they were needed.  Doing
15034      this requires corresponding changes elsewhere where single step
15035      breakpoints are handled, however.  So, for now, we use this.  */
15036
15037   *bpt_p = deprecated_insert_raw_breakpoint (gdbarch, aspace, next_pc);
15038   if (*bpt_p == NULL)
15039     error (_("Could not insert single-step breakpoint at %s"),
15040              paddress (gdbarch, next_pc));
15041 }
15042
15043 /* Check if the breakpoints used for software single stepping
15044    were inserted or not.  */
15045
15046 int
15047 single_step_breakpoints_inserted (void)
15048 {
15049   return (single_step_breakpoints[0] != NULL
15050           || single_step_breakpoints[1] != NULL);
15051 }
15052
15053 /* Remove and delete any breakpoints used for software single step.  */
15054
15055 void
15056 remove_single_step_breakpoints (void)
15057 {
15058   gdb_assert (single_step_breakpoints[0] != NULL);
15059
15060   /* See insert_single_step_breakpoint for more about this deprecated
15061      call.  */
15062   deprecated_remove_raw_breakpoint (single_step_gdbarch[0],
15063                                     single_step_breakpoints[0]);
15064   single_step_gdbarch[0] = NULL;
15065   single_step_breakpoints[0] = NULL;
15066
15067   if (single_step_breakpoints[1] != NULL)
15068     {
15069       deprecated_remove_raw_breakpoint (single_step_gdbarch[1],
15070                                         single_step_breakpoints[1]);
15071       single_step_gdbarch[1] = NULL;
15072       single_step_breakpoints[1] = NULL;
15073     }
15074 }
15075
15076 /* Delete software single step breakpoints without removing them from
15077    the inferior.  This is intended to be used if the inferior's address
15078    space where they were inserted is already gone, e.g. after exit or
15079    exec.  */
15080
15081 void
15082 cancel_single_step_breakpoints (void)
15083 {
15084   int i;
15085
15086   for (i = 0; i < 2; i++)
15087     if (single_step_breakpoints[i])
15088       {
15089         xfree (single_step_breakpoints[i]);
15090         single_step_breakpoints[i] = NULL;
15091         single_step_gdbarch[i] = NULL;
15092       }
15093 }
15094
15095 /* Detach software single-step breakpoints from INFERIOR_PTID without
15096    removing them.  */
15097
15098 static void
15099 detach_single_step_breakpoints (void)
15100 {
15101   int i;
15102
15103   for (i = 0; i < 2; i++)
15104     if (single_step_breakpoints[i])
15105       target_remove_breakpoint (single_step_gdbarch[i],
15106                                 single_step_breakpoints[i]);
15107 }
15108
15109 /* Check whether a software single-step breakpoint is inserted at
15110    PC.  */
15111
15112 static int
15113 single_step_breakpoint_inserted_here_p (struct address_space *aspace, 
15114                                         CORE_ADDR pc)
15115 {
15116   int i;
15117
15118   for (i = 0; i < 2; i++)
15119     {
15120       struct bp_target_info *bp_tgt = single_step_breakpoints[i];
15121       if (bp_tgt
15122           && breakpoint_address_match (bp_tgt->placed_address_space,
15123                                        bp_tgt->placed_address,
15124                                        aspace, pc))
15125         return 1;
15126     }
15127
15128   return 0;
15129 }
15130
15131 /* Returns 0 if 'bp' is NOT a syscall catchpoint,
15132    non-zero otherwise.  */
15133 static int
15134 is_syscall_catchpoint_enabled (struct breakpoint *bp)
15135 {
15136   if (syscall_catchpoint_p (bp)
15137       && bp->enable_state != bp_disabled
15138       && bp->enable_state != bp_call_disabled)
15139     return 1;
15140   else
15141     return 0;
15142 }
15143
15144 int
15145 catch_syscall_enabled (void)
15146 {
15147   struct catch_syscall_inferior_data *inf_data
15148     = get_catch_syscall_inferior_data (current_inferior ());
15149
15150   return inf_data->total_syscalls_count != 0;
15151 }
15152
15153 int
15154 catching_syscall_number (int syscall_number)
15155 {
15156   struct breakpoint *bp;
15157
15158   ALL_BREAKPOINTS (bp)
15159     if (is_syscall_catchpoint_enabled (bp))
15160       {
15161         struct syscall_catchpoint *c = (struct syscall_catchpoint *) bp;
15162
15163         if (c->syscalls_to_be_caught)
15164           {
15165             int i, iter;
15166             for (i = 0;
15167                  VEC_iterate (int, c->syscalls_to_be_caught, i, iter);
15168                  i++)
15169               if (syscall_number == iter)
15170                 return 1;
15171           }
15172         else
15173           return 1;
15174       }
15175
15176   return 0;
15177 }
15178
15179 /* Complete syscall names.  Used by "catch syscall".  */
15180 static VEC (char_ptr) *
15181 catch_syscall_completer (struct cmd_list_element *cmd,
15182                          const char *text, const char *word)
15183 {
15184   const char **list = get_syscall_names ();
15185   VEC (char_ptr) *retlist
15186     = (list == NULL) ? NULL : complete_on_enum (list, word, word);
15187
15188   xfree (list);
15189   return retlist;
15190 }
15191
15192 /* Tracepoint-specific operations.  */
15193
15194 /* Set tracepoint count to NUM.  */
15195 static void
15196 set_tracepoint_count (int num)
15197 {
15198   tracepoint_count = num;
15199   set_internalvar_integer (lookup_internalvar ("tpnum"), num);
15200 }
15201
15202 static void
15203 trace_command (char *arg, int from_tty)
15204 {
15205   struct breakpoint_ops *ops;
15206   const char *arg_cp = arg;
15207
15208   if (arg && probe_linespec_to_ops (&arg_cp))
15209     ops = &tracepoint_probe_breakpoint_ops;
15210   else
15211     ops = &tracepoint_breakpoint_ops;
15212
15213   create_breakpoint (get_current_arch (),
15214                      arg,
15215                      NULL, 0, NULL, 1 /* parse arg */,
15216                      0 /* tempflag */,
15217                      bp_tracepoint /* type_wanted */,
15218                      0 /* Ignore count */,
15219                      pending_break_support,
15220                      ops,
15221                      from_tty,
15222                      1 /* enabled */,
15223                      0 /* internal */, 0);
15224 }
15225
15226 static void
15227 ftrace_command (char *arg, int from_tty)
15228 {
15229   create_breakpoint (get_current_arch (),
15230                      arg,
15231                      NULL, 0, NULL, 1 /* parse arg */,
15232                      0 /* tempflag */,
15233                      bp_fast_tracepoint /* type_wanted */,
15234                      0 /* Ignore count */,
15235                      pending_break_support,
15236                      &tracepoint_breakpoint_ops,
15237                      from_tty,
15238                      1 /* enabled */,
15239                      0 /* internal */, 0);
15240 }
15241
15242 /* strace command implementation.  Creates a static tracepoint.  */
15243
15244 static void
15245 strace_command (char *arg, int from_tty)
15246 {
15247   struct breakpoint_ops *ops;
15248
15249   /* Decide if we are dealing with a static tracepoint marker (`-m'),
15250      or with a normal static tracepoint.  */
15251   if (arg && strncmp (arg, "-m", 2) == 0 && isspace (arg[2]))
15252     ops = &strace_marker_breakpoint_ops;
15253   else
15254     ops = &tracepoint_breakpoint_ops;
15255
15256   create_breakpoint (get_current_arch (),
15257                      arg,
15258                      NULL, 0, NULL, 1 /* parse arg */,
15259                      0 /* tempflag */,
15260                      bp_static_tracepoint /* type_wanted */,
15261                      0 /* Ignore count */,
15262                      pending_break_support,
15263                      ops,
15264                      from_tty,
15265                      1 /* enabled */,
15266                      0 /* internal */, 0);
15267 }
15268
15269 /* Set up a fake reader function that gets command lines from a linked
15270    list that was acquired during tracepoint uploading.  */
15271
15272 static struct uploaded_tp *this_utp;
15273 static int next_cmd;
15274
15275 static char *
15276 read_uploaded_action (void)
15277 {
15278   char *rslt;
15279
15280   VEC_iterate (char_ptr, this_utp->cmd_strings, next_cmd, rslt);
15281
15282   next_cmd++;
15283
15284   return rslt;
15285 }
15286
15287 /* Given information about a tracepoint as recorded on a target (which
15288    can be either a live system or a trace file), attempt to create an
15289    equivalent GDB tracepoint.  This is not a reliable process, since
15290    the target does not necessarily have all the information used when
15291    the tracepoint was originally defined.  */
15292   
15293 struct tracepoint *
15294 create_tracepoint_from_upload (struct uploaded_tp *utp)
15295 {
15296   char *addr_str, small_buf[100];
15297   struct tracepoint *tp;
15298
15299   if (utp->at_string)
15300     addr_str = utp->at_string;
15301   else
15302     {
15303       /* In the absence of a source location, fall back to raw
15304          address.  Since there is no way to confirm that the address
15305          means the same thing as when the trace was started, warn the
15306          user.  */
15307       warning (_("Uploaded tracepoint %d has no "
15308                  "source location, using raw address"),
15309                utp->number);
15310       xsnprintf (small_buf, sizeof (small_buf), "*%s", hex_string (utp->addr));
15311       addr_str = small_buf;
15312     }
15313
15314   /* There's not much we can do with a sequence of bytecodes.  */
15315   if (utp->cond && !utp->cond_string)
15316     warning (_("Uploaded tracepoint %d condition "
15317                "has no source form, ignoring it"),
15318              utp->number);
15319
15320   if (!create_breakpoint (get_current_arch (),
15321                           addr_str,
15322                           utp->cond_string, -1, NULL,
15323                           0 /* parse cond/thread */,
15324                           0 /* tempflag */,
15325                           utp->type /* type_wanted */,
15326                           0 /* Ignore count */,
15327                           pending_break_support,
15328                           &tracepoint_breakpoint_ops,
15329                           0 /* from_tty */,
15330                           utp->enabled /* enabled */,
15331                           0 /* internal */,
15332                           CREATE_BREAKPOINT_FLAGS_INSERTED))
15333     return NULL;
15334
15335   /* Get the tracepoint we just created.  */
15336   tp = get_tracepoint (tracepoint_count);
15337   gdb_assert (tp != NULL);
15338
15339   if (utp->pass > 0)
15340     {
15341       xsnprintf (small_buf, sizeof (small_buf), "%d %d", utp->pass,
15342                  tp->base.number);
15343
15344       trace_pass_command (small_buf, 0);
15345     }
15346
15347   /* If we have uploaded versions of the original commands, set up a
15348      special-purpose "reader" function and call the usual command line
15349      reader, then pass the result to the breakpoint command-setting
15350      function.  */
15351   if (!VEC_empty (char_ptr, utp->cmd_strings))
15352     {
15353       struct command_line *cmd_list;
15354
15355       this_utp = utp;
15356       next_cmd = 0;
15357
15358       cmd_list = read_command_lines_1 (read_uploaded_action, 1, NULL, NULL);
15359
15360       breakpoint_set_commands (&tp->base, cmd_list);
15361     }
15362   else if (!VEC_empty (char_ptr, utp->actions)
15363            || !VEC_empty (char_ptr, utp->step_actions))
15364     warning (_("Uploaded tracepoint %d actions "
15365                "have no source form, ignoring them"),
15366              utp->number);
15367
15368   /* Copy any status information that might be available.  */
15369   tp->base.hit_count = utp->hit_count;
15370   tp->traceframe_usage = utp->traceframe_usage;
15371
15372   return tp;
15373 }
15374   
15375 /* Print information on tracepoint number TPNUM_EXP, or all if
15376    omitted.  */
15377
15378 static void
15379 tracepoints_info (char *args, int from_tty)
15380 {
15381   struct ui_out *uiout = current_uiout;
15382   int num_printed;
15383
15384   num_printed = breakpoint_1 (args, 0, is_tracepoint);
15385
15386   if (num_printed == 0)
15387     {
15388       if (args == NULL || *args == '\0')
15389         ui_out_message (uiout, 0, "No tracepoints.\n");
15390       else
15391         ui_out_message (uiout, 0, "No tracepoint matching '%s'.\n", args);
15392     }
15393
15394   default_collect_info ();
15395 }
15396
15397 /* The 'enable trace' command enables tracepoints.
15398    Not supported by all targets.  */
15399 static void
15400 enable_trace_command (char *args, int from_tty)
15401 {
15402   enable_command (args, from_tty);
15403 }
15404
15405 /* The 'disable trace' command disables tracepoints.
15406    Not supported by all targets.  */
15407 static void
15408 disable_trace_command (char *args, int from_tty)
15409 {
15410   disable_command (args, from_tty);
15411 }
15412
15413 /* Remove a tracepoint (or all if no argument).  */
15414 static void
15415 delete_trace_command (char *arg, int from_tty)
15416 {
15417   struct breakpoint *b, *b_tmp;
15418
15419   dont_repeat ();
15420
15421   if (arg == 0)
15422     {
15423       int breaks_to_delete = 0;
15424
15425       /* Delete all breakpoints if no argument.
15426          Do not delete internal or call-dummy breakpoints, these
15427          have to be deleted with an explicit breakpoint number 
15428          argument.  */
15429       ALL_TRACEPOINTS (b)
15430         if (is_tracepoint (b) && user_breakpoint_p (b))
15431           {
15432             breaks_to_delete = 1;
15433             break;
15434           }
15435
15436       /* Ask user only if there are some breakpoints to delete.  */
15437       if (!from_tty
15438           || (breaks_to_delete && query (_("Delete all tracepoints? "))))
15439         {
15440           ALL_BREAKPOINTS_SAFE (b, b_tmp)
15441             if (is_tracepoint (b) && user_breakpoint_p (b))
15442               delete_breakpoint (b);
15443         }
15444     }
15445   else
15446     map_breakpoint_numbers (arg, do_map_delete_breakpoint, NULL);
15447 }
15448
15449 /* Helper function for trace_pass_command.  */
15450
15451 static void
15452 trace_pass_set_count (struct tracepoint *tp, int count, int from_tty)
15453 {
15454   tp->pass_count = count;
15455   observer_notify_breakpoint_modified (&tp->base);
15456   if (from_tty)
15457     printf_filtered (_("Setting tracepoint %d's passcount to %d\n"),
15458                      tp->base.number, count);
15459 }
15460
15461 /* Set passcount for tracepoint.
15462
15463    First command argument is passcount, second is tracepoint number.
15464    If tracepoint number omitted, apply to most recently defined.
15465    Also accepts special argument "all".  */
15466
15467 static void
15468 trace_pass_command (char *args, int from_tty)
15469 {
15470   struct tracepoint *t1;
15471   unsigned int count;
15472
15473   if (args == 0 || *args == 0)
15474     error (_("passcount command requires an "
15475              "argument (count + optional TP num)"));
15476
15477   count = strtoul (args, &args, 10);    /* Count comes first, then TP num.  */
15478
15479   args = skip_spaces (args);
15480   if (*args && strncasecmp (args, "all", 3) == 0)
15481     {
15482       struct breakpoint *b;
15483
15484       args += 3;                        /* Skip special argument "all".  */
15485       if (*args)
15486         error (_("Junk at end of arguments."));
15487
15488       ALL_TRACEPOINTS (b)
15489       {
15490         t1 = (struct tracepoint *) b;
15491         trace_pass_set_count (t1, count, from_tty);
15492       }
15493     }
15494   else if (*args == '\0')
15495     {
15496       t1 = get_tracepoint_by_number (&args, NULL, 1);
15497       if (t1)
15498         trace_pass_set_count (t1, count, from_tty);
15499     }
15500   else
15501     {
15502       struct get_number_or_range_state state;
15503
15504       init_number_or_range (&state, args);
15505       while (!state.finished)
15506         {
15507           t1 = get_tracepoint_by_number (&args, &state, 1);
15508           if (t1)
15509             trace_pass_set_count (t1, count, from_tty);
15510         }
15511     }
15512 }
15513
15514 struct tracepoint *
15515 get_tracepoint (int num)
15516 {
15517   struct breakpoint *t;
15518
15519   ALL_TRACEPOINTS (t)
15520     if (t->number == num)
15521       return (struct tracepoint *) t;
15522
15523   return NULL;
15524 }
15525
15526 /* Find the tracepoint with the given target-side number (which may be
15527    different from the tracepoint number after disconnecting and
15528    reconnecting).  */
15529
15530 struct tracepoint *
15531 get_tracepoint_by_number_on_target (int num)
15532 {
15533   struct breakpoint *b;
15534
15535   ALL_TRACEPOINTS (b)
15536     {
15537       struct tracepoint *t = (struct tracepoint *) b;
15538
15539       if (t->number_on_target == num)
15540         return t;
15541     }
15542
15543   return NULL;
15544 }
15545
15546 /* Utility: parse a tracepoint number and look it up in the list.
15547    If STATE is not NULL, use, get_number_or_range_state and ignore ARG.
15548    If OPTIONAL_P is true, then if the argument is missing, the most
15549    recent tracepoint (tracepoint_count) is returned.  */
15550 struct tracepoint *
15551 get_tracepoint_by_number (char **arg,
15552                           struct get_number_or_range_state *state,
15553                           int optional_p)
15554 {
15555   struct breakpoint *t;
15556   int tpnum;
15557   char *instring = arg == NULL ? NULL : *arg;
15558
15559   if (state)
15560     {
15561       gdb_assert (!state->finished);
15562       tpnum = get_number_or_range (state);
15563     }
15564   else if (arg == NULL || *arg == NULL || ! **arg)
15565     {
15566       if (optional_p)
15567         tpnum = tracepoint_count;
15568       else
15569         error_no_arg (_("tracepoint number"));
15570     }
15571   else
15572     tpnum = get_number (arg);
15573
15574   if (tpnum <= 0)
15575     {
15576       if (instring && *instring)
15577         printf_filtered (_("bad tracepoint number at or near '%s'\n"), 
15578                          instring);
15579       else
15580         printf_filtered (_("Tracepoint argument missing "
15581                            "and no previous tracepoint\n"));
15582       return NULL;
15583     }
15584
15585   ALL_TRACEPOINTS (t)
15586     if (t->number == tpnum)
15587     {
15588       return (struct tracepoint *) t;
15589     }
15590
15591   printf_unfiltered ("No tracepoint number %d.\n", tpnum);
15592   return NULL;
15593 }
15594
15595 void
15596 print_recreate_thread (struct breakpoint *b, struct ui_file *fp)
15597 {
15598   if (b->thread != -1)
15599     fprintf_unfiltered (fp, " thread %d", b->thread);
15600
15601   if (b->task != 0)
15602     fprintf_unfiltered (fp, " task %d", b->task);
15603
15604   fprintf_unfiltered (fp, "\n");
15605 }
15606
15607 /* Save information on user settable breakpoints (watchpoints, etc) to
15608    a new script file named FILENAME.  If FILTER is non-NULL, call it
15609    on each breakpoint and only include the ones for which it returns
15610    non-zero.  */
15611
15612 static void
15613 save_breakpoints (char *filename, int from_tty,
15614                   int (*filter) (const struct breakpoint *))
15615 {
15616   struct breakpoint *tp;
15617   int any = 0;
15618   char *pathname;
15619   struct cleanup *cleanup;
15620   struct ui_file *fp;
15621   int extra_trace_bits = 0;
15622
15623   if (filename == 0 || *filename == 0)
15624     error (_("Argument required (file name in which to save)"));
15625
15626   /* See if we have anything to save.  */
15627   ALL_BREAKPOINTS (tp)
15628   {
15629     /* Skip internal and momentary breakpoints.  */
15630     if (!user_breakpoint_p (tp))
15631       continue;
15632
15633     /* If we have a filter, only save the breakpoints it accepts.  */
15634     if (filter && !filter (tp))
15635       continue;
15636
15637     any = 1;
15638
15639     if (is_tracepoint (tp))
15640       {
15641         extra_trace_bits = 1;
15642
15643         /* We can stop searching.  */
15644         break;
15645       }
15646   }
15647
15648   if (!any)
15649     {
15650       warning (_("Nothing to save."));
15651       return;
15652     }
15653
15654   pathname = tilde_expand (filename);
15655   cleanup = make_cleanup (xfree, pathname);
15656   fp = gdb_fopen (pathname, "w");
15657   if (!fp)
15658     error (_("Unable to open file '%s' for saving (%s)"),
15659            filename, safe_strerror (errno));
15660   make_cleanup_ui_file_delete (fp);
15661
15662   if (extra_trace_bits)
15663     save_trace_state_variables (fp);
15664
15665   ALL_BREAKPOINTS (tp)
15666   {
15667     /* Skip internal and momentary breakpoints.  */
15668     if (!user_breakpoint_p (tp))
15669       continue;
15670
15671     /* If we have a filter, only save the breakpoints it accepts.  */
15672     if (filter && !filter (tp))
15673       continue;
15674
15675     tp->ops->print_recreate (tp, fp);
15676
15677     /* Note, we can't rely on tp->number for anything, as we can't
15678        assume the recreated breakpoint numbers will match.  Use $bpnum
15679        instead.  */
15680
15681     if (tp->cond_string)
15682       fprintf_unfiltered (fp, "  condition $bpnum %s\n", tp->cond_string);
15683
15684     if (tp->ignore_count)
15685       fprintf_unfiltered (fp, "  ignore $bpnum %d\n", tp->ignore_count);
15686
15687     if (tp->commands)
15688       {
15689         volatile struct gdb_exception ex;       
15690
15691         fprintf_unfiltered (fp, "  commands\n");
15692         
15693         ui_out_redirect (current_uiout, fp);
15694         TRY_CATCH (ex, RETURN_MASK_ALL)
15695           {
15696             print_command_lines (current_uiout, tp->commands->commands, 2);
15697           }
15698         ui_out_redirect (current_uiout, NULL);
15699
15700         if (ex.reason < 0)
15701           throw_exception (ex);
15702
15703         fprintf_unfiltered (fp, "  end\n");
15704       }
15705
15706     if (tp->enable_state == bp_disabled)
15707       fprintf_unfiltered (fp, "disable\n");
15708
15709     /* If this is a multi-location breakpoint, check if the locations
15710        should be individually disabled.  Watchpoint locations are
15711        special, and not user visible.  */
15712     if (!is_watchpoint (tp) && tp->loc && tp->loc->next)
15713       {
15714         struct bp_location *loc;
15715         int n = 1;
15716
15717         for (loc = tp->loc; loc != NULL; loc = loc->next, n++)
15718           if (!loc->enabled)
15719             fprintf_unfiltered (fp, "disable $bpnum.%d\n", n);
15720       }
15721   }
15722
15723   if (extra_trace_bits && *default_collect)
15724     fprintf_unfiltered (fp, "set default-collect %s\n", default_collect);
15725
15726   do_cleanups (cleanup);
15727   if (from_tty)
15728     printf_filtered (_("Saved to file '%s'.\n"), filename);
15729 }
15730
15731 /* The `save breakpoints' command.  */
15732
15733 static void
15734 save_breakpoints_command (char *args, int from_tty)
15735 {
15736   save_breakpoints (args, from_tty, NULL);
15737 }
15738
15739 /* The `save tracepoints' command.  */
15740
15741 static void
15742 save_tracepoints_command (char *args, int from_tty)
15743 {
15744   save_breakpoints (args, from_tty, is_tracepoint);
15745 }
15746
15747 /* Create a vector of all tracepoints.  */
15748
15749 VEC(breakpoint_p) *
15750 all_tracepoints (void)
15751 {
15752   VEC(breakpoint_p) *tp_vec = 0;
15753   struct breakpoint *tp;
15754
15755   ALL_TRACEPOINTS (tp)
15756   {
15757     VEC_safe_push (breakpoint_p, tp_vec, tp);
15758   }
15759
15760   return tp_vec;
15761 }
15762
15763 \f
15764 /* This help string is used for the break, hbreak, tbreak and thbreak
15765    commands.  It is defined as a macro to prevent duplication.
15766    COMMAND should be a string constant containing the name of the
15767    command.  */
15768 #define BREAK_ARGS_HELP(command) \
15769 command" [PROBE_MODIFIER] [LOCATION] [thread THREADNUM] [if CONDITION]\n\
15770 PROBE_MODIFIER shall be present if the command is to be placed in a\n\
15771 probe point.  Accepted values are `-probe' (for a generic, automatically\n\
15772 guessed probe type) or `-probe-stap' (for a SystemTap probe).\n\
15773 LOCATION may be a line number, function name, or \"*\" and an address.\n\
15774 If a line number is specified, break at start of code for that line.\n\
15775 If a function is specified, break at start of code for that function.\n\
15776 If an address is specified, break at that exact address.\n\
15777 With no LOCATION, uses current execution address of the selected\n\
15778 stack frame.  This is useful for breaking on return to a stack frame.\n\
15779 \n\
15780 THREADNUM is the number from \"info threads\".\n\
15781 CONDITION is a boolean expression.\n\
15782 \n\
15783 Multiple breakpoints at one place are permitted, and useful if their\n\
15784 conditions are different.\n\
15785 \n\
15786 Do \"help breakpoints\" for info on other commands dealing with breakpoints."
15787
15788 /* List of subcommands for "catch".  */
15789 static struct cmd_list_element *catch_cmdlist;
15790
15791 /* List of subcommands for "tcatch".  */
15792 static struct cmd_list_element *tcatch_cmdlist;
15793
15794 void
15795 add_catch_command (char *name, char *docstring,
15796                    void (*sfunc) (char *args, int from_tty,
15797                                   struct cmd_list_element *command),
15798                    completer_ftype *completer,
15799                    void *user_data_catch,
15800                    void *user_data_tcatch)
15801 {
15802   struct cmd_list_element *command;
15803
15804   command = add_cmd (name, class_breakpoint, NULL, docstring,
15805                      &catch_cmdlist);
15806   set_cmd_sfunc (command, sfunc);
15807   set_cmd_context (command, user_data_catch);
15808   set_cmd_completer (command, completer);
15809
15810   command = add_cmd (name, class_breakpoint, NULL, docstring,
15811                      &tcatch_cmdlist);
15812   set_cmd_sfunc (command, sfunc);
15813   set_cmd_context (command, user_data_tcatch);
15814   set_cmd_completer (command, completer);
15815 }
15816
15817 static void
15818 clear_syscall_counts (struct inferior *inf)
15819 {
15820   struct catch_syscall_inferior_data *inf_data
15821     = get_catch_syscall_inferior_data (inf);
15822
15823   inf_data->total_syscalls_count = 0;
15824   inf_data->any_syscall_count = 0;
15825   VEC_free (int, inf_data->syscalls_counts);
15826 }
15827
15828 static void
15829 save_command (char *arg, int from_tty)
15830 {
15831   printf_unfiltered (_("\"save\" must be followed by "
15832                        "the name of a save subcommand.\n"));
15833   help_list (save_cmdlist, "save ", -1, gdb_stdout);
15834 }
15835
15836 struct breakpoint *
15837 iterate_over_breakpoints (int (*callback) (struct breakpoint *, void *),
15838                           void *data)
15839 {
15840   struct breakpoint *b, *b_tmp;
15841
15842   ALL_BREAKPOINTS_SAFE (b, b_tmp)
15843     {
15844       if ((*callback) (b, data))
15845         return b;
15846     }
15847
15848   return NULL;
15849 }
15850
15851 /* Zero if any of the breakpoint's locations could be a location where
15852    functions have been inlined, nonzero otherwise.  */
15853
15854 static int
15855 is_non_inline_function (struct breakpoint *b)
15856 {
15857   /* The shared library event breakpoint is set on the address of a
15858      non-inline function.  */
15859   if (b->type == bp_shlib_event)
15860     return 1;
15861
15862   return 0;
15863 }
15864
15865 /* Nonzero if the specified PC cannot be a location where functions
15866    have been inlined.  */
15867
15868 int
15869 pc_at_non_inline_function (struct address_space *aspace, CORE_ADDR pc,
15870                            const struct target_waitstatus *ws)
15871 {
15872   struct breakpoint *b;
15873   struct bp_location *bl;
15874
15875   ALL_BREAKPOINTS (b)
15876     {
15877       if (!is_non_inline_function (b))
15878         continue;
15879
15880       for (bl = b->loc; bl != NULL; bl = bl->next)
15881         {
15882           if (!bl->shlib_disabled
15883               && bpstat_check_location (bl, aspace, pc, ws))
15884             return 1;
15885         }
15886     }
15887
15888   return 0;
15889 }
15890
15891 /* Remove any references to OBJFILE which is going to be freed.  */
15892
15893 void
15894 breakpoint_free_objfile (struct objfile *objfile)
15895 {
15896   struct bp_location **locp, *loc;
15897
15898   ALL_BP_LOCATIONS (loc, locp)
15899     if (loc->symtab != NULL && loc->symtab->objfile == objfile)
15900       loc->symtab = NULL;
15901 }
15902
15903 void
15904 initialize_breakpoint_ops (void)
15905 {
15906   static int initialized = 0;
15907
15908   struct breakpoint_ops *ops;
15909
15910   if (initialized)
15911     return;
15912   initialized = 1;
15913
15914   /* The breakpoint_ops structure to be inherit by all kinds of
15915      breakpoints (real breakpoints, i.e., user "break" breakpoints,
15916      internal and momentary breakpoints, etc.).  */
15917   ops = &bkpt_base_breakpoint_ops;
15918   *ops = base_breakpoint_ops;
15919   ops->re_set = bkpt_re_set;
15920   ops->insert_location = bkpt_insert_location;
15921   ops->remove_location = bkpt_remove_location;
15922   ops->breakpoint_hit = bkpt_breakpoint_hit;
15923   ops->create_sals_from_address = bkpt_create_sals_from_address;
15924   ops->create_breakpoints_sal = bkpt_create_breakpoints_sal;
15925   ops->decode_linespec = bkpt_decode_linespec;
15926
15927   /* The breakpoint_ops structure to be used in regular breakpoints.  */
15928   ops = &bkpt_breakpoint_ops;
15929   *ops = bkpt_base_breakpoint_ops;
15930   ops->re_set = bkpt_re_set;
15931   ops->resources_needed = bkpt_resources_needed;
15932   ops->print_it = bkpt_print_it;
15933   ops->print_mention = bkpt_print_mention;
15934   ops->print_recreate = bkpt_print_recreate;
15935
15936   /* Ranged breakpoints.  */
15937   ops = &ranged_breakpoint_ops;
15938   *ops = bkpt_breakpoint_ops;
15939   ops->breakpoint_hit = breakpoint_hit_ranged_breakpoint;
15940   ops->resources_needed = resources_needed_ranged_breakpoint;
15941   ops->print_it = print_it_ranged_breakpoint;
15942   ops->print_one = print_one_ranged_breakpoint;
15943   ops->print_one_detail = print_one_detail_ranged_breakpoint;
15944   ops->print_mention = print_mention_ranged_breakpoint;
15945   ops->print_recreate = print_recreate_ranged_breakpoint;
15946
15947   /* Internal breakpoints.  */
15948   ops = &internal_breakpoint_ops;
15949   *ops = bkpt_base_breakpoint_ops;
15950   ops->re_set = internal_bkpt_re_set;
15951   ops->check_status = internal_bkpt_check_status;
15952   ops->print_it = internal_bkpt_print_it;
15953   ops->print_mention = internal_bkpt_print_mention;
15954
15955   /* Momentary breakpoints.  */
15956   ops = &momentary_breakpoint_ops;
15957   *ops = bkpt_base_breakpoint_ops;
15958   ops->re_set = momentary_bkpt_re_set;
15959   ops->check_status = momentary_bkpt_check_status;
15960   ops->print_it = momentary_bkpt_print_it;
15961   ops->print_mention = momentary_bkpt_print_mention;
15962
15963   /* Momentary breakpoints for bp_longjmp and bp_exception.  */
15964   ops = &longjmp_breakpoint_ops;
15965   *ops = momentary_breakpoint_ops;
15966   ops->dtor = longjmp_bkpt_dtor;
15967
15968   /* Probe breakpoints.  */
15969   ops = &bkpt_probe_breakpoint_ops;
15970   *ops = bkpt_breakpoint_ops;
15971   ops->insert_location = bkpt_probe_insert_location;
15972   ops->remove_location = bkpt_probe_remove_location;
15973   ops->create_sals_from_address = bkpt_probe_create_sals_from_address;
15974   ops->decode_linespec = bkpt_probe_decode_linespec;
15975
15976   /* GNU v3 exception catchpoints.  */
15977   ops = &gnu_v3_exception_catchpoint_ops;
15978   *ops = bkpt_breakpoint_ops;
15979   ops->print_it = print_it_exception_catchpoint;
15980   ops->print_one = print_one_exception_catchpoint;
15981   ops->print_mention = print_mention_exception_catchpoint;
15982   ops->print_recreate = print_recreate_exception_catchpoint;
15983
15984   /* Watchpoints.  */
15985   ops = &watchpoint_breakpoint_ops;
15986   *ops = base_breakpoint_ops;
15987   ops->dtor = dtor_watchpoint;
15988   ops->re_set = re_set_watchpoint;
15989   ops->insert_location = insert_watchpoint;
15990   ops->remove_location = remove_watchpoint;
15991   ops->breakpoint_hit = breakpoint_hit_watchpoint;
15992   ops->check_status = check_status_watchpoint;
15993   ops->resources_needed = resources_needed_watchpoint;
15994   ops->works_in_software_mode = works_in_software_mode_watchpoint;
15995   ops->print_it = print_it_watchpoint;
15996   ops->print_mention = print_mention_watchpoint;
15997   ops->print_recreate = print_recreate_watchpoint;
15998
15999   /* Masked watchpoints.  */
16000   ops = &masked_watchpoint_breakpoint_ops;
16001   *ops = watchpoint_breakpoint_ops;
16002   ops->insert_location = insert_masked_watchpoint;
16003   ops->remove_location = remove_masked_watchpoint;
16004   ops->resources_needed = resources_needed_masked_watchpoint;
16005   ops->works_in_software_mode = works_in_software_mode_masked_watchpoint;
16006   ops->print_it = print_it_masked_watchpoint;
16007   ops->print_one_detail = print_one_detail_masked_watchpoint;
16008   ops->print_mention = print_mention_masked_watchpoint;
16009   ops->print_recreate = print_recreate_masked_watchpoint;
16010
16011   /* Tracepoints.  */
16012   ops = &tracepoint_breakpoint_ops;
16013   *ops = base_breakpoint_ops;
16014   ops->re_set = tracepoint_re_set;
16015   ops->breakpoint_hit = tracepoint_breakpoint_hit;
16016   ops->print_one_detail = tracepoint_print_one_detail;
16017   ops->print_mention = tracepoint_print_mention;
16018   ops->print_recreate = tracepoint_print_recreate;
16019   ops->create_sals_from_address = tracepoint_create_sals_from_address;
16020   ops->create_breakpoints_sal = tracepoint_create_breakpoints_sal;
16021   ops->decode_linespec = tracepoint_decode_linespec;
16022
16023   /* Probe tracepoints.  */
16024   ops = &tracepoint_probe_breakpoint_ops;
16025   *ops = tracepoint_breakpoint_ops;
16026   ops->create_sals_from_address = tracepoint_probe_create_sals_from_address;
16027   ops->decode_linespec = tracepoint_probe_decode_linespec;
16028
16029   /* Static tracepoints with marker (`-m').  */
16030   ops = &strace_marker_breakpoint_ops;
16031   *ops = tracepoint_breakpoint_ops;
16032   ops->create_sals_from_address = strace_marker_create_sals_from_address;
16033   ops->create_breakpoints_sal = strace_marker_create_breakpoints_sal;
16034   ops->decode_linespec = strace_marker_decode_linespec;
16035
16036   /* Fork catchpoints.  */
16037   ops = &catch_fork_breakpoint_ops;
16038   *ops = base_breakpoint_ops;
16039   ops->insert_location = insert_catch_fork;
16040   ops->remove_location = remove_catch_fork;
16041   ops->breakpoint_hit = breakpoint_hit_catch_fork;
16042   ops->print_it = print_it_catch_fork;
16043   ops->print_one = print_one_catch_fork;
16044   ops->print_mention = print_mention_catch_fork;
16045   ops->print_recreate = print_recreate_catch_fork;
16046
16047   /* Vfork catchpoints.  */
16048   ops = &catch_vfork_breakpoint_ops;
16049   *ops = base_breakpoint_ops;
16050   ops->insert_location = insert_catch_vfork;
16051   ops->remove_location = remove_catch_vfork;
16052   ops->breakpoint_hit = breakpoint_hit_catch_vfork;
16053   ops->print_it = print_it_catch_vfork;
16054   ops->print_one = print_one_catch_vfork;
16055   ops->print_mention = print_mention_catch_vfork;
16056   ops->print_recreate = print_recreate_catch_vfork;
16057
16058   /* Exec catchpoints.  */
16059   ops = &catch_exec_breakpoint_ops;
16060   *ops = base_breakpoint_ops;
16061   ops->dtor = dtor_catch_exec;
16062   ops->insert_location = insert_catch_exec;
16063   ops->remove_location = remove_catch_exec;
16064   ops->breakpoint_hit = breakpoint_hit_catch_exec;
16065   ops->print_it = print_it_catch_exec;
16066   ops->print_one = print_one_catch_exec;
16067   ops->print_mention = print_mention_catch_exec;
16068   ops->print_recreate = print_recreate_catch_exec;
16069
16070   /* Syscall catchpoints.  */
16071   ops = &catch_syscall_breakpoint_ops;
16072   *ops = base_breakpoint_ops;
16073   ops->dtor = dtor_catch_syscall;
16074   ops->insert_location = insert_catch_syscall;
16075   ops->remove_location = remove_catch_syscall;
16076   ops->breakpoint_hit = breakpoint_hit_catch_syscall;
16077   ops->print_it = print_it_catch_syscall;
16078   ops->print_one = print_one_catch_syscall;
16079   ops->print_mention = print_mention_catch_syscall;
16080   ops->print_recreate = print_recreate_catch_syscall;
16081
16082   /* Solib-related catchpoints.  */
16083   ops = &catch_solib_breakpoint_ops;
16084   *ops = base_breakpoint_ops;
16085   ops->dtor = dtor_catch_solib;
16086   ops->insert_location = insert_catch_solib;
16087   ops->remove_location = remove_catch_solib;
16088   ops->breakpoint_hit = breakpoint_hit_catch_solib;
16089   ops->check_status = check_status_catch_solib;
16090   ops->print_it = print_it_catch_solib;
16091   ops->print_one = print_one_catch_solib;
16092   ops->print_mention = print_mention_catch_solib;
16093   ops->print_recreate = print_recreate_catch_solib;
16094
16095   ops = &dprintf_breakpoint_ops;
16096   *ops = bkpt_base_breakpoint_ops;
16097   ops->re_set = dprintf_re_set;
16098   ops->resources_needed = bkpt_resources_needed;
16099   ops->print_it = bkpt_print_it;
16100   ops->print_mention = bkpt_print_mention;
16101   ops->print_recreate = bkpt_print_recreate;
16102 }
16103
16104 /* Chain containing all defined "enable breakpoint" subcommands.  */
16105
16106 static struct cmd_list_element *enablebreaklist = NULL;
16107
16108 void
16109 _initialize_breakpoint (void)
16110 {
16111   struct cmd_list_element *c;
16112
16113   initialize_breakpoint_ops ();
16114
16115   observer_attach_solib_unloaded (disable_breakpoints_in_unloaded_shlib);
16116   observer_attach_inferior_exit (clear_syscall_counts);
16117   observer_attach_memory_changed (invalidate_bp_value_on_memory_change);
16118
16119   breakpoint_objfile_key
16120     = register_objfile_data_with_cleanup (NULL, free_breakpoint_probes);
16121
16122   catch_syscall_inferior_data
16123     = register_inferior_data_with_cleanup (NULL,
16124                                            catch_syscall_inferior_data_cleanup);
16125
16126   breakpoint_chain = 0;
16127   /* Don't bother to call set_breakpoint_count.  $bpnum isn't useful
16128      before a breakpoint is set.  */
16129   breakpoint_count = 0;
16130
16131   tracepoint_count = 0;
16132
16133   add_com ("ignore", class_breakpoint, ignore_command, _("\
16134 Set ignore-count of breakpoint number N to COUNT.\n\
16135 Usage is `ignore N COUNT'."));
16136   if (xdb_commands)
16137     add_com_alias ("bc", "ignore", class_breakpoint, 1);
16138
16139   add_com ("commands", class_breakpoint, commands_command, _("\
16140 Set commands to be executed when a breakpoint is hit.\n\
16141 Give breakpoint number as argument after \"commands\".\n\
16142 With no argument, the targeted breakpoint is the last one set.\n\
16143 The commands themselves follow starting on the next line.\n\
16144 Type a line containing \"end\" to indicate the end of them.\n\
16145 Give \"silent\" as the first line to make the breakpoint silent;\n\
16146 then no output is printed when it is hit, except what the commands print."));
16147
16148   c = add_com ("condition", class_breakpoint, condition_command, _("\
16149 Specify breakpoint number N to break only if COND is true.\n\
16150 Usage is `condition N COND', where N is an integer and COND is an\n\
16151 expression to be evaluated whenever breakpoint N is reached."));
16152   set_cmd_completer (c, condition_completer);
16153
16154   c = add_com ("tbreak", class_breakpoint, tbreak_command, _("\
16155 Set a temporary breakpoint.\n\
16156 Like \"break\" except the breakpoint is only temporary,\n\
16157 so it will be deleted when hit.  Equivalent to \"break\" followed\n\
16158 by using \"enable delete\" on the breakpoint number.\n\
16159 \n"
16160 BREAK_ARGS_HELP ("tbreak")));
16161   set_cmd_completer (c, location_completer);
16162
16163   c = add_com ("hbreak", class_breakpoint, hbreak_command, _("\
16164 Set a hardware assisted breakpoint.\n\
16165 Like \"break\" except the breakpoint requires hardware support,\n\
16166 some target hardware may not have this support.\n\
16167 \n"
16168 BREAK_ARGS_HELP ("hbreak")));
16169   set_cmd_completer (c, location_completer);
16170
16171   c = add_com ("thbreak", class_breakpoint, thbreak_command, _("\
16172 Set a temporary hardware assisted breakpoint.\n\
16173 Like \"hbreak\" except the breakpoint is only temporary,\n\
16174 so it will be deleted when hit.\n\
16175 \n"
16176 BREAK_ARGS_HELP ("thbreak")));
16177   set_cmd_completer (c, location_completer);
16178
16179   add_prefix_cmd ("enable", class_breakpoint, enable_command, _("\
16180 Enable some breakpoints.\n\
16181 Give breakpoint numbers (separated by spaces) as arguments.\n\
16182 With no subcommand, breakpoints are enabled until you command otherwise.\n\
16183 This is used to cancel the effect of the \"disable\" command.\n\
16184 With a subcommand you can enable temporarily."),
16185                   &enablelist, "enable ", 1, &cmdlist);
16186   if (xdb_commands)
16187     add_com ("ab", class_breakpoint, enable_command, _("\
16188 Enable some breakpoints.\n\
16189 Give breakpoint numbers (separated by spaces) as arguments.\n\
16190 With no subcommand, breakpoints are enabled until you command otherwise.\n\
16191 This is used to cancel the effect of the \"disable\" command.\n\
16192 With a subcommand you can enable temporarily."));
16193
16194   add_com_alias ("en", "enable", class_breakpoint, 1);
16195
16196   add_prefix_cmd ("breakpoints", class_breakpoint, enable_command, _("\
16197 Enable some breakpoints.\n\
16198 Give breakpoint numbers (separated by spaces) as arguments.\n\
16199 This is used to cancel the effect of the \"disable\" command.\n\
16200 May be abbreviated to simply \"enable\".\n"),
16201                    &enablebreaklist, "enable breakpoints ", 1, &enablelist);
16202
16203   add_cmd ("once", no_class, enable_once_command, _("\
16204 Enable breakpoints for one hit.  Give breakpoint numbers.\n\
16205 If a breakpoint is hit while enabled in this fashion, it becomes disabled."),
16206            &enablebreaklist);
16207
16208   add_cmd ("delete", no_class, enable_delete_command, _("\
16209 Enable breakpoints and delete when hit.  Give breakpoint numbers.\n\
16210 If a breakpoint is hit while enabled in this fashion, it is deleted."),
16211            &enablebreaklist);
16212
16213   add_cmd ("count", no_class, enable_count_command, _("\
16214 Enable breakpoints for COUNT hits.  Give count and then breakpoint numbers.\n\
16215 If a breakpoint is hit while enabled in this fashion,\n\
16216 the count is decremented; when it reaches zero, the breakpoint is disabled."),
16217            &enablebreaklist);
16218
16219   add_cmd ("delete", no_class, enable_delete_command, _("\
16220 Enable breakpoints and delete when hit.  Give breakpoint numbers.\n\
16221 If a breakpoint is hit while enabled in this fashion, it is deleted."),
16222            &enablelist);
16223
16224   add_cmd ("once", no_class, enable_once_command, _("\
16225 Enable breakpoints for one hit.  Give breakpoint numbers.\n\
16226 If a breakpoint is hit while enabled in this fashion, it becomes disabled."),
16227            &enablelist);
16228
16229   add_cmd ("count", no_class, enable_count_command, _("\
16230 Enable breakpoints for COUNT hits.  Give count and then breakpoint numbers.\n\
16231 If a breakpoint is hit while enabled in this fashion,\n\
16232 the count is decremented; when it reaches zero, the breakpoint is disabled."),
16233            &enablelist);
16234
16235   add_prefix_cmd ("disable", class_breakpoint, disable_command, _("\
16236 Disable some breakpoints.\n\
16237 Arguments are breakpoint numbers with spaces in between.\n\
16238 To disable all breakpoints, give no argument.\n\
16239 A disabled breakpoint is not forgotten, but has no effect until re-enabled."),
16240                   &disablelist, "disable ", 1, &cmdlist);
16241   add_com_alias ("dis", "disable", class_breakpoint, 1);
16242   add_com_alias ("disa", "disable", class_breakpoint, 1);
16243   if (xdb_commands)
16244     add_com ("sb", class_breakpoint, disable_command, _("\
16245 Disable some breakpoints.\n\
16246 Arguments are breakpoint numbers with spaces in between.\n\
16247 To disable all breakpoints, give no argument.\n\
16248 A disabled breakpoint is not forgotten, but has no effect until re-enabled."));
16249
16250   add_cmd ("breakpoints", class_alias, disable_command, _("\
16251 Disable some breakpoints.\n\
16252 Arguments are breakpoint numbers with spaces in between.\n\
16253 To disable all breakpoints, give no argument.\n\
16254 A disabled breakpoint is not forgotten, but has no effect until re-enabled.\n\
16255 This command may be abbreviated \"disable\"."),
16256            &disablelist);
16257
16258   add_prefix_cmd ("delete", class_breakpoint, delete_command, _("\
16259 Delete some breakpoints or auto-display expressions.\n\
16260 Arguments are breakpoint numbers with spaces in between.\n\
16261 To delete all breakpoints, give no argument.\n\
16262 \n\
16263 Also a prefix command for deletion of other GDB objects.\n\
16264 The \"unset\" command is also an alias for \"delete\"."),
16265                   &deletelist, "delete ", 1, &cmdlist);
16266   add_com_alias ("d", "delete", class_breakpoint, 1);
16267   add_com_alias ("del", "delete", class_breakpoint, 1);
16268   if (xdb_commands)
16269     add_com ("db", class_breakpoint, delete_command, _("\
16270 Delete some breakpoints.\n\
16271 Arguments are breakpoint numbers with spaces in between.\n\
16272 To delete all breakpoints, give no argument.\n"));
16273
16274   add_cmd ("breakpoints", class_alias, delete_command, _("\
16275 Delete some breakpoints or auto-display expressions.\n\
16276 Arguments are breakpoint numbers with spaces in between.\n\
16277 To delete all breakpoints, give no argument.\n\
16278 This command may be abbreviated \"delete\"."),
16279            &deletelist);
16280
16281   add_com ("clear", class_breakpoint, clear_command, _("\
16282 Clear breakpoint at specified line or function.\n\
16283 Argument may be line number, function name, or \"*\" and an address.\n\
16284 If line number is specified, all breakpoints in that line are cleared.\n\
16285 If function is specified, breakpoints at beginning of function are cleared.\n\
16286 If an address is specified, breakpoints at that address are cleared.\n\
16287 \n\
16288 With no argument, clears all breakpoints in the line that the selected frame\n\
16289 is executing in.\n\
16290 \n\
16291 See also the \"delete\" command which clears breakpoints by number."));
16292   add_com_alias ("cl", "clear", class_breakpoint, 1);
16293
16294   c = add_com ("break", class_breakpoint, break_command, _("\
16295 Set breakpoint at specified line or function.\n"
16296 BREAK_ARGS_HELP ("break")));
16297   set_cmd_completer (c, location_completer);
16298
16299   add_com_alias ("b", "break", class_run, 1);
16300   add_com_alias ("br", "break", class_run, 1);
16301   add_com_alias ("bre", "break", class_run, 1);
16302   add_com_alias ("brea", "break", class_run, 1);
16303
16304   if (xdb_commands)
16305    add_com_alias ("ba", "break", class_breakpoint, 1);
16306
16307   if (dbx_commands)
16308     {
16309       add_abbrev_prefix_cmd ("stop", class_breakpoint, stop_command, _("\
16310 Break in function/address or break at a line in the current file."),
16311                              &stoplist, "stop ", 1, &cmdlist);
16312       add_cmd ("in", class_breakpoint, stopin_command,
16313                _("Break in function or address."), &stoplist);
16314       add_cmd ("at", class_breakpoint, stopat_command,
16315                _("Break at a line in the current file."), &stoplist);
16316       add_com ("status", class_info, breakpoints_info, _("\
16317 Status of user-settable breakpoints, or breakpoint number NUMBER.\n\
16318 The \"Type\" column indicates one of:\n\
16319 \tbreakpoint     - normal breakpoint\n\
16320 \twatchpoint     - watchpoint\n\
16321 The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
16322 the disposition of the breakpoint after it gets hit.  \"dis\" means that the\n\
16323 breakpoint will be disabled.  The \"Address\" and \"What\" columns indicate the\n\
16324 address and file/line number respectively.\n\
16325 \n\
16326 Convenience variable \"$_\" and default examine address for \"x\"\n\
16327 are set to the address of the last breakpoint listed unless the command\n\
16328 is prefixed with \"server \".\n\n\
16329 Convenience variable \"$bpnum\" contains the number of the last\n\
16330 breakpoint set."));
16331     }
16332
16333   add_info ("breakpoints", breakpoints_info, _("\
16334 Status of specified breakpoints (all user-settable breakpoints if no argument).\n\
16335 The \"Type\" column indicates one of:\n\
16336 \tbreakpoint     - normal breakpoint\n\
16337 \twatchpoint     - watchpoint\n\
16338 The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
16339 the disposition of the breakpoint after it gets hit.  \"dis\" means that the\n\
16340 breakpoint will be disabled.  The \"Address\" and \"What\" columns indicate the\n\
16341 address and file/line number respectively.\n\
16342 \n\
16343 Convenience variable \"$_\" and default examine address for \"x\"\n\
16344 are set to the address of the last breakpoint listed unless the command\n\
16345 is prefixed with \"server \".\n\n\
16346 Convenience variable \"$bpnum\" contains the number of the last\n\
16347 breakpoint set."));
16348
16349   add_info_alias ("b", "breakpoints", 1);
16350
16351   if (xdb_commands)
16352     add_com ("lb", class_breakpoint, breakpoints_info, _("\
16353 Status of user-settable breakpoints, or breakpoint number NUMBER.\n\
16354 The \"Type\" column indicates one of:\n\
16355 \tbreakpoint     - normal breakpoint\n\
16356 \twatchpoint     - watchpoint\n\
16357 The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
16358 the disposition of the breakpoint after it gets hit.  \"dis\" means that the\n\
16359 breakpoint will be disabled.  The \"Address\" and \"What\" columns indicate the\n\
16360 address and file/line number respectively.\n\
16361 \n\
16362 Convenience variable \"$_\" and default examine address for \"x\"\n\
16363 are set to the address of the last breakpoint listed unless the command\n\
16364 is prefixed with \"server \".\n\n\
16365 Convenience variable \"$bpnum\" contains the number of the last\n\
16366 breakpoint set."));
16367
16368   add_cmd ("breakpoints", class_maintenance, maintenance_info_breakpoints, _("\
16369 Status of all breakpoints, or breakpoint number NUMBER.\n\
16370 The \"Type\" column indicates one of:\n\
16371 \tbreakpoint     - normal breakpoint\n\
16372 \twatchpoint     - watchpoint\n\
16373 \tlongjmp        - internal breakpoint used to step through longjmp()\n\
16374 \tlongjmp resume - internal breakpoint at the target of longjmp()\n\
16375 \tuntil          - internal breakpoint used by the \"until\" command\n\
16376 \tfinish         - internal breakpoint used by the \"finish\" command\n\
16377 The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
16378 the disposition of the breakpoint after it gets hit.  \"dis\" means that the\n\
16379 breakpoint will be disabled.  The \"Address\" and \"What\" columns indicate the\n\
16380 address and file/line number respectively.\n\
16381 \n\
16382 Convenience variable \"$_\" and default examine address for \"x\"\n\
16383 are set to the address of the last breakpoint listed unless the command\n\
16384 is prefixed with \"server \".\n\n\
16385 Convenience variable \"$bpnum\" contains the number of the last\n\
16386 breakpoint set."),
16387            &maintenanceinfolist);
16388
16389   add_prefix_cmd ("catch", class_breakpoint, catch_command, _("\
16390 Set catchpoints to catch events."),
16391                   &catch_cmdlist, "catch ",
16392                   0/*allow-unknown*/, &cmdlist);
16393
16394   add_prefix_cmd ("tcatch", class_breakpoint, tcatch_command, _("\
16395 Set temporary catchpoints to catch events."),
16396                   &tcatch_cmdlist, "tcatch ",
16397                   0/*allow-unknown*/, &cmdlist);
16398
16399   /* Add catch and tcatch sub-commands.  */
16400   add_catch_command ("catch", _("\
16401 Catch an exception, when caught."),
16402                      catch_catch_command,
16403                      NULL,
16404                      CATCH_PERMANENT,
16405                      CATCH_TEMPORARY);
16406   add_catch_command ("throw", _("\
16407 Catch an exception, when thrown."),
16408                      catch_throw_command,
16409                      NULL,
16410                      CATCH_PERMANENT,
16411                      CATCH_TEMPORARY);
16412   add_catch_command ("rethrow", _("\
16413 Catch an exception, when rethrown."),
16414                      catch_rethrow_command,
16415                      NULL,
16416                      CATCH_PERMANENT,
16417                      CATCH_TEMPORARY);
16418   add_catch_command ("fork", _("Catch calls to fork."),
16419                      catch_fork_command_1,
16420                      NULL,
16421                      (void *) (uintptr_t) catch_fork_permanent,
16422                      (void *) (uintptr_t) catch_fork_temporary);
16423   add_catch_command ("vfork", _("Catch calls to vfork."),
16424                      catch_fork_command_1,
16425                      NULL,
16426                      (void *) (uintptr_t) catch_vfork_permanent,
16427                      (void *) (uintptr_t) catch_vfork_temporary);
16428   add_catch_command ("exec", _("Catch calls to exec."),
16429                      catch_exec_command_1,
16430                      NULL,
16431                      CATCH_PERMANENT,
16432                      CATCH_TEMPORARY);
16433   add_catch_command ("load", _("Catch loads of shared libraries.\n\
16434 Usage: catch load [REGEX]\n\
16435 If REGEX is given, only stop for libraries matching the regular expression."),
16436                      catch_load_command_1,
16437                      NULL,
16438                      CATCH_PERMANENT,
16439                      CATCH_TEMPORARY);
16440   add_catch_command ("unload", _("Catch unloads of shared libraries.\n\
16441 Usage: catch unload [REGEX]\n\
16442 If REGEX is given, only stop for libraries matching the regular expression."),
16443                      catch_unload_command_1,
16444                      NULL,
16445                      CATCH_PERMANENT,
16446                      CATCH_TEMPORARY);
16447   add_catch_command ("syscall", _("\
16448 Catch system calls by their names and/or numbers.\n\
16449 Arguments say which system calls to catch.  If no arguments\n\
16450 are given, every system call will be caught.\n\
16451 Arguments, if given, should be one or more system call names\n\
16452 (if your system supports that), or system call numbers."),
16453                      catch_syscall_command_1,
16454                      catch_syscall_completer,
16455                      CATCH_PERMANENT,
16456                      CATCH_TEMPORARY);
16457
16458   c = add_com ("watch", class_breakpoint, watch_command, _("\
16459 Set a watchpoint for an expression.\n\
16460 Usage: watch [-l|-location] EXPRESSION\n\
16461 A watchpoint stops execution of your program whenever the value of\n\
16462 an expression changes.\n\
16463 If -l or -location is given, this evaluates EXPRESSION and watches\n\
16464 the memory to which it refers."));
16465   set_cmd_completer (c, expression_completer);
16466
16467   c = add_com ("rwatch", class_breakpoint, rwatch_command, _("\
16468 Set a read watchpoint for an expression.\n\
16469 Usage: rwatch [-l|-location] EXPRESSION\n\
16470 A watchpoint stops execution of your program whenever the value of\n\
16471 an expression is read.\n\
16472 If -l or -location is given, this evaluates EXPRESSION and watches\n\
16473 the memory to which it refers."));
16474   set_cmd_completer (c, expression_completer);
16475
16476   c = add_com ("awatch", class_breakpoint, awatch_command, _("\
16477 Set a watchpoint for an expression.\n\
16478 Usage: awatch [-l|-location] EXPRESSION\n\
16479 A watchpoint stops execution of your program whenever the value of\n\
16480 an expression is either read or written.\n\
16481 If -l or -location is given, this evaluates EXPRESSION and watches\n\
16482 the memory to which it refers."));
16483   set_cmd_completer (c, expression_completer);
16484
16485   add_info ("watchpoints", watchpoints_info, _("\
16486 Status of specified watchpoints (all watchpoints if no argument)."));
16487
16488   /* XXX: cagney/2005-02-23: This should be a boolean, and should
16489      respond to changes - contrary to the description.  */
16490   add_setshow_zinteger_cmd ("can-use-hw-watchpoints", class_support,
16491                             &can_use_hw_watchpoints, _("\
16492 Set debugger's willingness to use watchpoint hardware."), _("\
16493 Show debugger's willingness to use watchpoint hardware."), _("\
16494 If zero, gdb will not use hardware for new watchpoints, even if\n\
16495 such is available.  (However, any hardware watchpoints that were\n\
16496 created before setting this to nonzero, will continue to use watchpoint\n\
16497 hardware.)"),
16498                             NULL,
16499                             show_can_use_hw_watchpoints,
16500                             &setlist, &showlist);
16501
16502   can_use_hw_watchpoints = 1;
16503
16504   /* Tracepoint manipulation commands.  */
16505
16506   c = add_com ("trace", class_breakpoint, trace_command, _("\
16507 Set a tracepoint at specified line or function.\n\
16508 \n"
16509 BREAK_ARGS_HELP ("trace") "\n\
16510 Do \"help tracepoints\" for info on other tracepoint commands."));
16511   set_cmd_completer (c, location_completer);
16512
16513   add_com_alias ("tp", "trace", class_alias, 0);
16514   add_com_alias ("tr", "trace", class_alias, 1);
16515   add_com_alias ("tra", "trace", class_alias, 1);
16516   add_com_alias ("trac", "trace", class_alias, 1);
16517
16518   c = add_com ("ftrace", class_breakpoint, ftrace_command, _("\
16519 Set a fast tracepoint at specified line or function.\n\
16520 \n"
16521 BREAK_ARGS_HELP ("ftrace") "\n\
16522 Do \"help tracepoints\" for info on other tracepoint commands."));
16523   set_cmd_completer (c, location_completer);
16524
16525   c = add_com ("strace", class_breakpoint, strace_command, _("\
16526 Set a static tracepoint at specified line, function or marker.\n\
16527 \n\
16528 strace [LOCATION] [if CONDITION]\n\
16529 LOCATION may be a line number, function name, \"*\" and an address,\n\
16530 or -m MARKER_ID.\n\
16531 If a line number is specified, probe the marker at start of code\n\
16532 for that line.  If a function is specified, probe the marker at start\n\
16533 of code for that function.  If an address is specified, probe the marker\n\
16534 at that exact address.  If a marker id is specified, probe the marker\n\
16535 with that name.  With no LOCATION, uses current execution address of\n\
16536 the selected stack frame.\n\
16537 Static tracepoints accept an extra collect action -- ``collect $_sdata''.\n\
16538 This collects arbitrary user data passed in the probe point call to the\n\
16539 tracing library.  You can inspect it when analyzing the trace buffer,\n\
16540 by printing the $_sdata variable like any other convenience variable.\n\
16541 \n\
16542 CONDITION is a boolean expression.\n\
16543 \n\
16544 Multiple tracepoints at one place are permitted, and useful if their\n\
16545 conditions are different.\n\
16546 \n\
16547 Do \"help breakpoints\" for info on other commands dealing with breakpoints.\n\
16548 Do \"help tracepoints\" for info on other tracepoint commands."));
16549   set_cmd_completer (c, location_completer);
16550
16551   add_info ("tracepoints", tracepoints_info, _("\
16552 Status of specified tracepoints (all tracepoints if no argument).\n\
16553 Convenience variable \"$tpnum\" contains the number of the\n\
16554 last tracepoint set."));
16555
16556   add_info_alias ("tp", "tracepoints", 1);
16557
16558   add_cmd ("tracepoints", class_trace, delete_trace_command, _("\
16559 Delete specified tracepoints.\n\
16560 Arguments are tracepoint numbers, separated by spaces.\n\
16561 No argument means delete all tracepoints."),
16562            &deletelist);
16563   add_alias_cmd ("tr", "tracepoints", class_trace, 1, &deletelist);
16564
16565   c = add_cmd ("tracepoints", class_trace, disable_trace_command, _("\
16566 Disable specified tracepoints.\n\
16567 Arguments are tracepoint numbers, separated by spaces.\n\
16568 No argument means disable all tracepoints."),
16569            &disablelist);
16570   deprecate_cmd (c, "disable");
16571
16572   c = add_cmd ("tracepoints", class_trace, enable_trace_command, _("\
16573 Enable specified tracepoints.\n\
16574 Arguments are tracepoint numbers, separated by spaces.\n\
16575 No argument means enable all tracepoints."),
16576            &enablelist);
16577   deprecate_cmd (c, "enable");
16578
16579   add_com ("passcount", class_trace, trace_pass_command, _("\
16580 Set the passcount for a tracepoint.\n\
16581 The trace will end when the tracepoint has been passed 'count' times.\n\
16582 Usage: passcount COUNT TPNUM, where TPNUM may also be \"all\";\n\
16583 if TPNUM is omitted, passcount refers to the last tracepoint defined."));
16584
16585   add_prefix_cmd ("save", class_breakpoint, save_command,
16586                   _("Save breakpoint definitions as a script."),
16587                   &save_cmdlist, "save ",
16588                   0/*allow-unknown*/, &cmdlist);
16589
16590   c = add_cmd ("breakpoints", class_breakpoint, save_breakpoints_command, _("\
16591 Save current breakpoint definitions as a script.\n\
16592 This includes all types of breakpoints (breakpoints, watchpoints,\n\
16593 catchpoints, tracepoints).  Use the 'source' command in another debug\n\
16594 session to restore them."),
16595                &save_cmdlist);
16596   set_cmd_completer (c, filename_completer);
16597
16598   c = add_cmd ("tracepoints", class_trace, save_tracepoints_command, _("\
16599 Save current tracepoint definitions as a script.\n\
16600 Use the 'source' command in another debug session to restore them."),
16601                &save_cmdlist);
16602   set_cmd_completer (c, filename_completer);
16603
16604   c = add_com_alias ("save-tracepoints", "save tracepoints", class_trace, 0);
16605   deprecate_cmd (c, "save tracepoints");
16606
16607   add_prefix_cmd ("breakpoint", class_maintenance, set_breakpoint_cmd, _("\
16608 Breakpoint specific settings\n\
16609 Configure various breakpoint-specific variables such as\n\
16610 pending breakpoint behavior"),
16611                   &breakpoint_set_cmdlist, "set breakpoint ",
16612                   0/*allow-unknown*/, &setlist);
16613   add_prefix_cmd ("breakpoint", class_maintenance, show_breakpoint_cmd, _("\
16614 Breakpoint specific settings\n\
16615 Configure various breakpoint-specific variables such as\n\
16616 pending breakpoint behavior"),
16617                   &breakpoint_show_cmdlist, "show breakpoint ",
16618                   0/*allow-unknown*/, &showlist);
16619
16620   add_setshow_auto_boolean_cmd ("pending", no_class,
16621                                 &pending_break_support, _("\
16622 Set debugger's behavior regarding pending breakpoints."), _("\
16623 Show debugger's behavior regarding pending breakpoints."), _("\
16624 If on, an unrecognized breakpoint location will cause gdb to create a\n\
16625 pending breakpoint.  If off, an unrecognized breakpoint location results in\n\
16626 an error.  If auto, an unrecognized breakpoint location results in a\n\
16627 user-query to see if a pending breakpoint should be created."),
16628                                 NULL,
16629                                 show_pending_break_support,
16630                                 &breakpoint_set_cmdlist,
16631                                 &breakpoint_show_cmdlist);
16632
16633   pending_break_support = AUTO_BOOLEAN_AUTO;
16634
16635   add_setshow_boolean_cmd ("auto-hw", no_class,
16636                            &automatic_hardware_breakpoints, _("\
16637 Set automatic usage of hardware breakpoints."), _("\
16638 Show automatic usage of hardware breakpoints."), _("\
16639 If set, the debugger will automatically use hardware breakpoints for\n\
16640 breakpoints set with \"break\" but falling in read-only memory.  If not set,\n\
16641 a warning will be emitted for such breakpoints."),
16642                            NULL,
16643                            show_automatic_hardware_breakpoints,
16644                            &breakpoint_set_cmdlist,
16645                            &breakpoint_show_cmdlist);
16646
16647   add_setshow_auto_boolean_cmd ("always-inserted", class_support,
16648                                 &always_inserted_mode, _("\
16649 Set mode for inserting breakpoints."), _("\
16650 Show mode for inserting breakpoints."), _("\
16651 When this mode is off, breakpoints are inserted in inferior when it is\n\
16652 resumed, and removed when execution stops.  When this mode is on,\n\
16653 breakpoints are inserted immediately and removed only when the user\n\
16654 deletes the breakpoint.  When this mode is auto (which is the default),\n\
16655 the behaviour depends on the non-stop setting (see help set non-stop).\n\
16656 In this case, if gdb is controlling the inferior in non-stop mode, gdb\n\
16657 behaves as if always-inserted mode is on; if gdb is controlling the\n\
16658 inferior in all-stop mode, gdb behaves as if always-inserted mode is off."),
16659                                 NULL,
16660                                 &show_always_inserted_mode,
16661                                 &breakpoint_set_cmdlist,
16662                                 &breakpoint_show_cmdlist);
16663
16664   add_setshow_enum_cmd ("condition-evaluation", class_breakpoint,
16665                         condition_evaluation_enums,
16666                         &condition_evaluation_mode_1, _("\
16667 Set mode of breakpoint condition evaluation."), _("\
16668 Show mode of breakpoint condition evaluation."), _("\
16669 When this is set to \"host\", breakpoint conditions will be\n\
16670 evaluated on the host's side by GDB.  When it is set to \"target\",\n\
16671 breakpoint conditions will be downloaded to the target (if the target\n\
16672 supports such feature) and conditions will be evaluated on the target's side.\n\
16673 If this is set to \"auto\" (default), this will be automatically set to\n\
16674 \"target\" if it supports condition evaluation, otherwise it will\n\
16675 be set to \"gdb\""),
16676                            &set_condition_evaluation_mode,
16677                            &show_condition_evaluation_mode,
16678                            &breakpoint_set_cmdlist,
16679                            &breakpoint_show_cmdlist);
16680
16681   add_com ("break-range", class_breakpoint, break_range_command, _("\
16682 Set a breakpoint for an address range.\n\
16683 break-range START-LOCATION, END-LOCATION\n\
16684 where START-LOCATION and END-LOCATION can be one of the following:\n\
16685   LINENUM, for that line in the current file,\n\
16686   FILE:LINENUM, for that line in that file,\n\
16687   +OFFSET, for that number of lines after the current line\n\
16688            or the start of the range\n\
16689   FUNCTION, for the first line in that function,\n\
16690   FILE:FUNCTION, to distinguish among like-named static functions.\n\
16691   *ADDRESS, for the instruction at that address.\n\
16692 \n\
16693 The breakpoint will stop execution of the inferior whenever it executes\n\
16694 an instruction at any address within the [START-LOCATION, END-LOCATION]\n\
16695 range (including START-LOCATION and END-LOCATION)."));
16696
16697   c = add_com ("dprintf", class_breakpoint, dprintf_command, _("\
16698 Set a dynamic printf at specified line or function.\n\
16699 dprintf location,format string,arg1,arg2,...\n\
16700 location may be a line number, function name, or \"*\" and an address.\n\
16701 If a line number is specified, break at start of code for that line.\n\
16702 If a function is specified, break at start of code for that function.\n\
16703 "));
16704   set_cmd_completer (c, location_completer);
16705
16706   add_setshow_enum_cmd ("dprintf-style", class_support,
16707                         dprintf_style_enums, &dprintf_style, _("\
16708 Set the style of usage for dynamic printf."), _("\
16709 Show the style of usage for dynamic printf."), _("\
16710 This setting chooses how GDB will do a dynamic printf.\n\
16711 If the value is \"gdb\", then the printing is done by GDB to its own\n\
16712 console, as with the \"printf\" command.\n\
16713 If the value is \"call\", the print is done by calling a function in your\n\
16714 program; by default printf(), but you can choose a different function or\n\
16715 output stream by setting dprintf-function and dprintf-channel."),
16716                         update_dprintf_commands, NULL,
16717                         &setlist, &showlist);
16718
16719   dprintf_function = xstrdup ("printf");
16720   add_setshow_string_cmd ("dprintf-function", class_support,
16721                           &dprintf_function, _("\
16722 Set the function to use for dynamic printf"), _("\
16723 Show the function to use for dynamic printf"), NULL,
16724                           update_dprintf_commands, NULL,
16725                           &setlist, &showlist);
16726
16727   dprintf_channel = xstrdup ("");
16728   add_setshow_string_cmd ("dprintf-channel", class_support,
16729                           &dprintf_channel, _("\
16730 Set the channel to use for dynamic printf"), _("\
16731 Show the channel to use for dynamic printf"), NULL,
16732                           update_dprintf_commands, NULL,
16733                           &setlist, &showlist);
16734
16735   add_setshow_boolean_cmd ("disconnected-dprintf", no_class,
16736                            &disconnected_dprintf, _("\
16737 Set whether dprintf continues after GDB disconnects."), _("\
16738 Show whether dprintf continues after GDB disconnects."), _("\
16739 Use this to let dprintf commands continue to hit and produce output\n\
16740 even if GDB disconnects or detaches from the target."),
16741                            NULL,
16742                            NULL,
16743                            &setlist, &showlist);
16744
16745   add_com ("agent-printf", class_vars, agent_printf_command, _("\
16746 agent-printf \"printf format string\", arg1, arg2, arg3, ..., argn\n\
16747 (target agent only) This is useful for formatted output in user-defined commands."));
16748
16749   automatic_hardware_breakpoints = 1;
16750
16751   observer_attach_about_to_proceed (breakpoint_about_to_proceed);
16752 }